-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathICodec.cpp
More file actions
177 lines (149 loc) · 4.14 KB
/
Copy pathICodec.cpp
File metadata and controls
177 lines (149 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "ICodec.hpp"
extern "C" {
#include <libavutil/mem.h>
}
#include <stdexcept>
#include <cassert>
namespace avtranscoder
{
ICodec::ICodec(const ECodecType type, const std::string& codecName)
: _avCodecContext(NULL)
, _avCodec(NULL)
, _isCodecContextAllocated(true)
, _type(type)
{
setCodec(type, codecName);
allocateContext();
loadCodecOptions();
}
ICodec::ICodec(const ECodecType type, const AVCodecID codecId)
: _avCodecContext(NULL)
, _avCodec(NULL)
, _isCodecContextAllocated(true)
, _type(type)
{
setCodec(type, codecId);
allocateContext();
loadCodecOptions();
}
ICodec::ICodec(const ECodecType type, AVCodecContext& avCodecContext)
: _avCodecContext(&avCodecContext)
, _avCodec(NULL)
, _isCodecContextAllocated(false)
, _type(type)
{
setCodec(type, _avCodecContext->codec_id);
loadCodecOptions();
}
ICodec::~ICodec()
{
if(!_isCodecContextAllocated)
return;
avcodec_free_context(&_avCodecContext);
_avCodecContext = NULL;
}
void ICodec::openCodec()
{
if(!_avCodecContext)
throw std::runtime_error("Unable to open a codec without codec context");
const int ret = avcodec_open2(_avCodecContext, _avCodec, NULL);
if(ret < 0)
{
std::string msg = "Unable to open codec: ";
if(_avCodec && _avCodec->long_name)
msg += _avCodec->long_name;
if(_avCodec && _avCodec->name)
{
msg += " (";
msg += _avCodec->name;
msg += ") ";
}
closeCodec();
msg += getDescriptionFromErrorCode(ret);
throw std::runtime_error(msg);
}
}
void ICodec::closeCodec()
{
if(!_avCodecContext)
throw std::runtime_error("Unable to close a codec without codec context");
avcodec_close(_avCodecContext);
}
std::string ICodec::getCodecName() const
{
assert(_avCodecContext != NULL);
const AVCodecDescriptor* desc = avcodec_descriptor_get(_avCodecContext->codec_id);
if(!desc)
throw std::runtime_error("Codec Descriptor is not available.");
return desc->name;
}
AVCodecID ICodec::getCodecId() const
{
assert(_avCodecContext != NULL);
return _avCodecContext->codec_id;
}
int ICodec::getLatency() const
{
assert(_avCodecContext != NULL);
return _avCodecContext->delay;
}
std::vector<Option> ICodec::getOptions()
{
std::vector<Option> optionsArray;
for(OptionMap::iterator it = _options.begin(); it != _options.end(); ++it)
{
optionsArray.push_back(it->second);
}
return optionsArray;
}
void ICodec::setCodec(const ECodecType type, const std::string& codecName)
{
const AVCodecDescriptor* avCodecDescriptor = avcodec_descriptor_get_by_name(codecName.c_str());
if(!avCodecDescriptor)
{
std::string msg("Unable to find codec ");
msg += codecName;
throw std::runtime_error(msg);
}
setCodec(type, avCodecDescriptor->id);
}
void ICodec::setCodec(const ECodecType type, const AVCodecID codecId)
{
if(codecId == 0)
{
LOG_WARN("Unsupported codec with id 0")
return;
}
if(type == eCodecTypeEncoder)
_avCodec = avcodec_find_encoder(codecId);
else if(type == eCodecTypeDecoder)
_avCodec = avcodec_find_decoder(codecId);
if(_avCodecContext)
_avCodecContext->codec = _avCodec;
}
void ICodec::allocateContext()
{
_avCodecContext = avcodec_alloc_context3(_avCodec);
if(!_avCodecContext)
{
LOG_ERROR("Unable to allocate the codecContext and set its fields to default values.")
throw std::bad_alloc();
}
_avCodecContext->codec = _avCodec;
}
void ICodec::loadCodecOptions()
{
if(_type == eCodecTypeEncoder)
{
loadOptions(_options, _avCodecContext, AV_OPT_FLAG_ENCODING_PARAM);
// load specific options of the codec
loadOptions(_options, _avCodecContext->priv_data, AV_OPT_FLAG_ENCODING_PARAM);
}
else if(_type == eCodecTypeDecoder)
{
loadOptions(_options, _avCodecContext, AV_OPT_FLAG_DECODING_PARAM);
// load specific options of the codec
loadOptions(_options, _avCodecContext->priv_data, AV_OPT_FLAG_DECODING_PARAM);
}
}
}