forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOption.cpp
More file actions
314 lines (261 loc) · 8.63 KB
/
Option.cpp
File metadata and controls
314 lines (261 loc) · 8.63 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "Option.hpp"
extern "C" {
#include <libavutil/error.h>
#include <libavutil/mem.h>
}
#include <sstream>
#include <stdexcept>
namespace avtranscoder
{
Option::Option(AVOption& avOption, void* avContext)
: _avOption(&avOption)
, _avContext(avContext)
, _childOptions()
, _defaultChildIndex(0)
{
_type = getTypeFromAVOption(getUnit(), _avOption->type);
}
EOptionBaseType Option::getTypeFromAVOption(const std::string& unit, const AVOptionType avType)
{
if(!unit.empty() && avType == AV_OPT_TYPE_FLAGS)
return eOptionBaseTypeGroup;
else if(!unit.empty() && (avType == AV_OPT_TYPE_INT || avType == AV_OPT_TYPE_INT64))
return eOptionBaseTypeChoice;
else if(!unit.empty() && avType == AV_OPT_TYPE_CONST)
return eOptionBaseTypeChild;
switch(avType)
{
case AV_OPT_TYPE_FLAGS:
{
return eOptionBaseTypeBool;
}
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_INT64:
{
return eOptionBaseTypeInt;
}
case AV_OPT_TYPE_DOUBLE:
case AV_OPT_TYPE_FLOAT:
{
return eOptionBaseTypeDouble;
}
case AV_OPT_TYPE_STRING:
case AV_OPT_TYPE_BINARY:
{
return eOptionBaseTypeString;
}
case AV_OPT_TYPE_RATIONAL:
{
return eOptionBaseTypeRatio;
}
default:
{
return eOptionBaseTypeUnknown;
}
}
return eOptionBaseTypeUnknown;
}
EOptionBaseType Option::getType() const
{
return _type;
}
bool Option::getDefaultBool() const
{
return _avOption->default_val.i64;
}
int Option::getDefaultInt() const
{
return _avOption->default_val.i64;
}
double Option::getDefaultDouble() const
{
return _avOption->default_val.dbl;
}
std::string Option::getDefaultString() const
{
return std::string(_avOption->default_val.str ? _avOption->default_val.str : "");
}
std::pair<int, int> Option::getDefaultRatio() const
{
return std::make_pair(_avOption->default_val.q.num, _avOption->default_val.q.den);
}
bool Option::getBool() const
{
int64_t out_val;
int error = av_opt_get_int(_avContext, getName().c_str(), AV_OPT_SEARCH_CHILDREN, &out_val);
checkFFmpegGetOption(error);
return out_val ? true : false;
}
int Option::getInt() const
{
int64_t out_val;
int error = av_opt_get_int(_avContext, getName().c_str(), AV_OPT_SEARCH_CHILDREN, &out_val);
checkFFmpegGetOption(error);
return out_val;
}
double Option::getDouble() const
{
double out_val;
int error = av_opt_get_double(_avContext, getName().c_str(), AV_OPT_SEARCH_CHILDREN, &out_val);
checkFFmpegGetOption(error);
return out_val;
}
std::string Option::getString() const
{
void* out_val = av_malloc(128);
int error = av_opt_get(_avContext, getName().c_str(), AV_OPT_SEARCH_CHILDREN, (uint8_t**)&out_val);
std::string strValue(out_val ? reinterpret_cast<const char*>(out_val) : "");
av_free(out_val);
checkFFmpegGetOption(error);
return strValue;
}
std::pair<int, int> Option::getRatio() const
{
Rational out_val;
int error = av_opt_get_q(_avContext, getName().c_str(), AV_OPT_SEARCH_CHILDREN, &out_val);
checkFFmpegGetOption(error);
return std::make_pair(out_val.num, out_val.den);
}
void Option::setFlag(const std::string& flag, const bool enable)
{
int64_t optVal;
int error = av_opt_get_int(_avContext, getName().c_str(), AV_OPT_SEARCH_CHILDREN, &optVal);
checkFFmpegGetOption(error);
if(enable)
optVal = optVal | _avOption->default_val.i64;
else
optVal = optVal & ~_avOption->default_val.i64;
error = av_opt_set_int(_avContext, getName().c_str(), optVal, AV_OPT_SEARCH_CHILDREN);
checkFFmpegSetOption(error, flag);
}
void Option::setBool(const bool value)
{
int error = av_opt_set_int(_avContext, getName().c_str(), value, AV_OPT_SEARCH_CHILDREN);
std::ostringstream os;
os << (value ? "true" : "false");
checkFFmpegSetOption(error, os.str());
}
void Option::setInt(const int value)
{
int error = av_opt_set_int(_avContext, getName().c_str(), value, AV_OPT_SEARCH_CHILDREN);
std::ostringstream os;
os << value;
checkFFmpegSetOption(error, os.str());
}
void Option::setDouble(const double value)
{
int error = av_opt_set_double(_avContext, getName().c_str(), value, AV_OPT_SEARCH_CHILDREN);
std::ostringstream os;
os << value;
checkFFmpegSetOption(error, os.str());
}
void Option::setString(const std::string& value)
{
int error = av_opt_set(_avContext, getName().c_str(), value.c_str(), AV_OPT_SEARCH_CHILDREN);
checkFFmpegSetOption(error, value);
}
void Option::setRatio(const int num, const int den)
{
Rational ratio;
ratio.num = num;
ratio.den = den;
int error = av_opt_set_q(_avContext, getName().c_str(), ratio, AV_OPT_SEARCH_CHILDREN);
std::ostringstream os;
os << num << "/" << den;
checkFFmpegSetOption(error, os.str());
}
void Option::appendChild(const Option& child)
{
_childOptions.push_back(child);
}
void Option::checkFFmpegGetOption(const int ffmpegReturnCode) const
{
if(ffmpegReturnCode)
{
throw std::runtime_error("unknown key " + getName() + ": " + getDescriptionFromErrorCode(ffmpegReturnCode));
}
}
void Option::checkFFmpegSetOption(const int ffmpegReturnCode, const std::string& optionValue)
{
if(ffmpegReturnCode)
{
throw std::runtime_error("setting " + getName() + " parameter to " + optionValue + ": " +
getDescriptionFromErrorCode(ffmpegReturnCode));
}
}
void loadOptions(OptionMap& outOptions, void* av_class, int req_flags)
{
if(!av_class)
return;
// Use multimap because it is possible to have several parent options with the same unit
std::multimap<std::string, std::string> optionUnitToParentName;
std::vector<Option> childOptions;
const AVOption* avOption = NULL;
// iterate on options
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(51, 12, 0)
while((avOption = av_next_option(av_class, avOption)))
#else
while((avOption = av_opt_next(av_class, avOption)))
#endif
{
if(!avOption || !avOption->name || (avOption->flags & req_flags) != req_flags)
{
continue;
}
Option option(*const_cast<AVOption*>(avOption), av_class);
if(option.getType() == eOptionBaseTypeChild)
{
childOptions.push_back(option);
}
else
{
outOptions.insert(std::make_pair(option.getName(), option));
if(! option.getUnit().empty())
{
optionUnitToParentName.insert(std::make_pair(option.getUnit(), option.getName()));
}
}
}
// iterate on child options
for(std::vector<Option>::iterator itChild = childOptions.begin(); itChild != childOptions.end(); ++itChild)
{
bool parentFound = false;
for(std::multimap<std::string, std::string>::iterator itUnitToParents = optionUnitToParentName.begin();
itUnitToParents != optionUnitToParentName.end(); ++itUnitToParents)
{
const std::string parentUnit = itUnitToParents->first;
if(parentUnit == itChild->getUnit())
{
// Get all the parent options with the same unit
std::pair<std::multimap<std::string, std::string>::iterator, std::multimap<std::string, std::string>::iterator> parentOptions = optionUnitToParentName.equal_range(parentUnit);
for(std::multimap<std::string, std::string>::iterator itParent = parentOptions.first; itParent != parentOptions.second; ++itParent)
{
const std::string parentName = itParent->second;
Option& parentOption = outOptions.at(parentName);
parentOption.appendChild(*itChild);
// child of a Choice
if(parentOption.getType() == eOptionBaseTypeChoice)
{
if(itChild->getDefaultInt() == parentOption.getDefaultInt())
parentOption.setDefaultChildIndex(parentOption.getChilds().size() - 1);
}
parentFound = true;
}
if(parentFound)
break;
}
}
if(!parentFound)
{
LOG_WARN("Can't find a choice option for " << itChild->getName())
}
}
}
void loadOptions(OptionArray& outOptions, void* av_class, int req_flags)
{
OptionMap optionMap;
loadOptions(optionMap, av_class, req_flags);
for(OptionMap::iterator it = optionMap.begin(); it != optionMap.end(); ++it)
outOptions.push_back(it->second);
}
}