FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/opt.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 1258 1682 74.8%
Functions: 81 102 79.4%
Branches: 818 1310 62.4%

Line Branch Exec Source
1 /*
2 * AVOptions
3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * AVOptions
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "avutil.h"
29 #include "avassert.h"
30 #include "avstring.h"
31 #include "channel_layout.h"
32 #include "dict.h"
33 #include "eval.h"
34 #include "log.h"
35 #include "mem.h"
36 #include "parseutils.h"
37 #include "pixdesc.h"
38 #include "mathematics.h"
39 #include "opt.h"
40 #include "samplefmt.h"
41 #include "bprint.h"
42 #include "version.h"
43
44 #include <float.h>
45
46 #define TYPE_BASE(type) ((type) & ~AV_OPT_TYPE_FLAG_ARRAY)
47
48 573795763 const AVOption *av_opt_next(const void *obj, const AVOption *last)
49 {
50 const AVClass *class;
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 573795763 times.
573795763 if (!obj)
52 return NULL;
53 573795763 class = *(const AVClass**)obj;
54
7/8
✓ Branch 0 taken 45848455 times.
✓ Branch 1 taken 527947308 times.
✓ Branch 2 taken 45848455 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45518632 times.
✓ Branch 5 taken 329823 times.
✓ Branch 6 taken 45408551 times.
✓ Branch 7 taken 110081 times.
573795763 if (!last && class && class->option && class->option[0].name)
55 45408551 return class->option;
56
4/4
✓ Branch 0 taken 527947308 times.
✓ Branch 1 taken 439904 times.
✓ Branch 2 taken 483166012 times.
✓ Branch 3 taken 44781296 times.
528387212 if (last && last[1].name)
57 483166012 return ++last;
58 45221200 return NULL;
59 }
60
61 static const struct {
62 size_t size;
63 const char *name;
64 } opt_type_desc[] = {
65 [AV_OPT_TYPE_FLAGS] = { sizeof(unsigned), "<flags>" },
66 [AV_OPT_TYPE_INT] = { sizeof(int), "<int>" },
67 [AV_OPT_TYPE_INT64] = { sizeof(int64_t), "<int64>" },
68 [AV_OPT_TYPE_UINT] = { sizeof(unsigned), "<unsigned>" },
69 [AV_OPT_TYPE_UINT64] = { sizeof(uint64_t), "<uint64>" },
70 [AV_OPT_TYPE_DOUBLE] = { sizeof(double), "<double>" },
71 [AV_OPT_TYPE_FLOAT] = { sizeof(float), "<float>" },
72 [AV_OPT_TYPE_STRING] = { sizeof(char *), "<string>" },
73 [AV_OPT_TYPE_RATIONAL] = { sizeof(AVRational), "<rational>" },
74 [AV_OPT_TYPE_BINARY] = { sizeof(uint8_t *), "<binary>" },
75 [AV_OPT_TYPE_DICT] = { sizeof(AVDictionary *), "<dictionary>" },
76 [AV_OPT_TYPE_IMAGE_SIZE] = { sizeof(int[2]), "<image_size>" },
77 [AV_OPT_TYPE_VIDEO_RATE] = { sizeof(AVRational), "<video_rate>" },
78 [AV_OPT_TYPE_PIXEL_FMT] = { sizeof(int), "<pix_fmt>" },
79 [AV_OPT_TYPE_SAMPLE_FMT] = { sizeof(int), "<sample_fmt>" },
80 [AV_OPT_TYPE_DURATION] = { sizeof(int64_t), "<duration>" },
81 [AV_OPT_TYPE_COLOR] = { sizeof(uint8_t[4]), "<color>" },
82 [AV_OPT_TYPE_CHLAYOUT] = { sizeof(AVChannelLayout),"<channel_layout>" },
83 [AV_OPT_TYPE_BOOL] = { sizeof(int), "<boolean>" },
84 };
85
86 // option is plain old data
87 1785403 static int opt_is_pod(enum AVOptionType type)
88 {
89
1/2
✓ Branch 0 taken 1785403 times.
✗ Branch 1 not taken.
1785403 switch (type) {
90 1785403 case AV_OPT_TYPE_FLAGS:
91 case AV_OPT_TYPE_INT:
92 case AV_OPT_TYPE_INT64:
93 case AV_OPT_TYPE_DOUBLE:
94 case AV_OPT_TYPE_FLOAT:
95 case AV_OPT_TYPE_RATIONAL:
96 case AV_OPT_TYPE_UINT64:
97 case AV_OPT_TYPE_IMAGE_SIZE:
98 case AV_OPT_TYPE_PIXEL_FMT:
99 case AV_OPT_TYPE_SAMPLE_FMT:
100 case AV_OPT_TYPE_VIDEO_RATE:
101 case AV_OPT_TYPE_DURATION:
102 case AV_OPT_TYPE_COLOR:
103 case AV_OPT_TYPE_BOOL:
104 case AV_OPT_TYPE_UINT:
105 1785403 return 1;
106 }
107 return 0;
108 }
109
110 99460 static uint8_t opt_array_sep(const AVOption *o)
111 {
112 99460 const AVOptionArrayDef *d = o->default_val.arr;
113 av_assert1(o->type & AV_OPT_TYPE_FLAG_ARRAY);
114
4/4
✓ Branch 0 taken 13877 times.
✓ Branch 1 taken 85583 times.
✓ Branch 2 taken 13852 times.
✓ Branch 3 taken 25 times.
99460 return (d && d->sep) ? d->sep : ',';
115 }
116
117 5770 static void *opt_array_pelem(const AVOption *o, void *array, unsigned idx)
118 {
119 av_assert1(o->type & AV_OPT_TYPE_FLAG_ARRAY);
120 5770 return (uint8_t *)array + idx * opt_type_desc[TYPE_BASE(o->type)].size;
121 }
122
123 155315 static unsigned *opt_array_pcount(const void *parray)
124 {
125 155315 return (unsigned *)((const void * const *)parray + 1);
126 }
127
128 25401791 static void opt_free_elem(enum AVOptionType type, void *ptr)
129 {
130
4/4
✓ Branch 0 taken 955426 times.
✓ Branch 1 taken 243 times.
✓ Branch 2 taken 62643 times.
✓ Branch 3 taken 24383479 times.
25401791 switch (TYPE_BASE(type)) {
131 955426 case AV_OPT_TYPE_STRING:
132 case AV_OPT_TYPE_BINARY:
133 955426 av_freep(ptr);
134 955426 break;
135
136 243 case AV_OPT_TYPE_DICT:
137 243 av_dict_free((AVDictionary **)ptr);
138 243 break;
139
140 62643 case AV_OPT_TYPE_CHLAYOUT:
141 62643 av_channel_layout_uninit((AVChannelLayout *)ptr);
142 62643 break;
143
144 24383479 default:
145 24383479 break;
146 }
147 25401791 }
148
149 113915 static void opt_free_array(const AVOption *o, void *parray, unsigned *count)
150 {
151
2/2
✓ Branch 0 taken 2876 times.
✓ Branch 1 taken 113915 times.
116791 for (unsigned i = 0; i < *count; i++)
152 2876 opt_free_elem(o->type, opt_array_pelem(o, *(void **)parray, i));
153
154 113915 av_freep(parray);
155 113915 *count = 0;
156 113915 }
157
158 /**
159 * Perform common setup for option-setting functions.
160 *
161 * @param require_type when non-0, require the option to be of this type
162 * @param ptgt target object is written here
163 * @param po the option is written here
164 * @param pdst pointer to option value is written here
165 */
166 345251 static int opt_set_init(void *obj, const char *name, int search_flags,
167 int require_type,
168 void **ptgt, const AVOption **po, void **pdst)
169 {
170 const AVOption *o;
171 void *tgt;
172
173 345251 o = av_opt_find2(obj, name, NULL, 0, search_flags, &tgt);
174
3/4
✓ Branch 0 taken 282363 times.
✓ Branch 1 taken 62888 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 282363 times.
345251 if (!o || !tgt)
175 62888 return AVERROR_OPTION_NOT_FOUND;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 282363 times.
282363 if (o->flags & AV_OPT_FLAG_READONLY)
178 return AVERROR(EINVAL);
179
180
3/4
✓ Branch 0 taken 3188 times.
✓ Branch 1 taken 279175 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3188 times.
282363 if (require_type && (o->type != require_type)) {
181 av_log(obj, AV_LOG_ERROR,
182 "Tried to set option '%s' of type %s from value of type %s, "
183 "this is not supported\n", o->name, opt_type_desc[o->type].name,
184 opt_type_desc[require_type].name);
185 return AVERROR(EINVAL);
186 }
187
188
2/2
✓ Branch 0 taken 258679 times.
✓ Branch 1 taken 23684 times.
282363 if (!(o->flags & AV_OPT_FLAG_RUNTIME_PARAM)) {
189 258679 unsigned *state_flags = NULL;
190 const AVClass *class;
191
192 // try state flags first from the target (child), then from its parent
193 258679 class = *(const AVClass**)tgt;
194 258679 if (
195 #if LIBAVUTIL_VERSION_MAJOR < 60
196
2/2
✓ Branch 0 taken 258649 times.
✓ Branch 1 taken 30 times.
258679 class->version >= AV_VERSION_INT(59, 41, 100) &&
197 #endif
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258649 times.
258649 class->state_flags_offset)
199 state_flags = (unsigned*)((uint8_t*)tgt + class->state_flags_offset);
200
201
3/4
✓ Branch 0 taken 258679 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44050 times.
✓ Branch 3 taken 214629 times.
258679 if (!state_flags && obj != tgt) {
202 44050 class = *(const AVClass**)obj;
203 44050 if (
204 #if LIBAVUTIL_VERSION_MAJOR < 60
205
1/2
✓ Branch 0 taken 44050 times.
✗ Branch 1 not taken.
44050 class->version >= AV_VERSION_INT(59, 41, 100) &&
206 #endif
207
2/2
✓ Branch 0 taken 43300 times.
✓ Branch 1 taken 750 times.
44050 class->state_flags_offset)
208 43300 state_flags = (unsigned*)((uint8_t*)obj + class->state_flags_offset);
209 }
210
211
3/4
✓ Branch 0 taken 43300 times.
✓ Branch 1 taken 215379 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 43300 times.
258679 if (state_flags && (*state_flags & AV_CLASS_STATE_INITIALIZED)) {
212 av_log(obj, AV_LOG_ERROR, "Option '%s' is not a runtime option and "
213 "so cannot be set after the object has been initialized\n",
214 o->name);
215 #if LIBAVUTIL_VERSION_MAJOR >= 60
216 return AVERROR(EINVAL);
217 #endif
218 }
219 }
220
221
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 282313 times.
282363 if (o->flags & AV_OPT_FLAG_DEPRECATED)
222 50 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
223
224
2/2
✓ Branch 0 taken 279175 times.
✓ Branch 1 taken 3188 times.
282363 if (po)
225 279175 *po = o;
226
2/2
✓ Branch 0 taken 186247 times.
✓ Branch 1 taken 96116 times.
282363 if (ptgt)
227 186247 *ptgt = tgt;
228
1/2
✓ Branch 0 taken 282363 times.
✗ Branch 1 not taken.
282363 if (pdst)
229 282363 *pdst = ((uint8_t *)tgt) + o->offset;
230
231 282363 return 0;
232 }
233
234 static AVRational double_to_rational(double d)
235 {
236 AVRational r = av_d2q(d, 1 << 24);
237 if ((!r.num || !r.den) && d)
238 r = av_d2q(d, INT_MAX);
239 return r;
240 }
241
242 22003 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
243 {
244
6/11
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 21956 times.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
22003 switch (TYPE_BASE(o->type)) {
245 9 case AV_OPT_TYPE_FLAGS:
246 9 *intnum = *(unsigned int*)dst;
247 9 return 0;
248 3 case AV_OPT_TYPE_PIXEL_FMT:
249 3 *intnum = *(enum AVPixelFormat *)dst;
250 3 return 0;
251 3 case AV_OPT_TYPE_SAMPLE_FMT:
252 3 *intnum = *(enum AVSampleFormat *)dst;
253 3 return 0;
254 21956 case AV_OPT_TYPE_BOOL:
255 case AV_OPT_TYPE_INT:
256 21956 *intnum = *(int *)dst;
257 21956 return 0;
258 23 case AV_OPT_TYPE_UINT:
259 23 *intnum = *(unsigned int *)dst;
260 23 return 0;
261 9 case AV_OPT_TYPE_DURATION:
262 case AV_OPT_TYPE_INT64:
263 case AV_OPT_TYPE_UINT64:
264 9 *intnum = *(int64_t *)dst;
265 9 return 0;
266 case AV_OPT_TYPE_FLOAT:
267 *num = *(float *)dst;
268 return 0;
269 case AV_OPT_TYPE_DOUBLE:
270 *num = *(double *)dst;
271 return 0;
272 case AV_OPT_TYPE_RATIONAL:
273 *intnum = ((AVRational *)dst)->num;
274 *den = ((AVRational *)dst)->den;
275 return 0;
276 case AV_OPT_TYPE_CONST:
277 *intnum = o->default_val.i64;
278 return 0;
279 }
280 return AVERROR(EINVAL);
281 }
282
283 8054098 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
284 {
285 8054098 const enum AVOptionType type = TYPE_BASE(o->type);
286
287
4/4
✓ Branch 0 taken 7194681 times.
✓ Branch 1 taken 859417 times.
✓ Branch 2 taken 7194680 times.
✓ Branch 3 taken 1 times.
8054098 if (type != AV_OPT_TYPE_FLAGS &&
288
4/4
✓ Branch 0 taken 7194672 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7194665 times.
7194680 (!den || o->max * den < num * intnum || o->min * den > num * intnum)) {
289
4/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
16 num = den ? num * intnum / den : (num && intnum ? INFINITY : NAN);
290 16 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
291 16 num, o->name, o->min, o->max);
292 16 return AVERROR(ERANGE);
293 }
294
2/2
✓ Branch 0 taken 859417 times.
✓ Branch 1 taken 7194665 times.
8054082 if (type == AV_OPT_TYPE_FLAGS) {
295 859417 double d = num*intnum/den;
296
3/6
✓ Branch 0 taken 859417 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 859417 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 859417 times.
859417 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
297 av_log(obj, AV_LOG_ERROR,
298 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
299 num*intnum/den, o->name);
300 return AVERROR(ERANGE);
301 }
302 }
303
304
7/9
✓ Branch 0 taken 111124 times.
✓ Branch 1 taken 49091 times.
✓ Branch 2 taken 6360669 times.
✓ Branch 3 taken 466124 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 774938 times.
✓ Branch 6 taken 99757 times.
✓ Branch 7 taken 192379 times.
✗ Branch 8 not taken.
8054082 switch (type) {
305 111124 case AV_OPT_TYPE_PIXEL_FMT:
306 111124 *(enum AVPixelFormat *)dst = llrint(num / den) * intnum;
307 111124 break;
308 49091 case AV_OPT_TYPE_SAMPLE_FMT:
309 49091 *(enum AVSampleFormat *)dst = llrint(num / den) * intnum;
310 49091 break;
311 6360669 case AV_OPT_TYPE_BOOL:
312 case AV_OPT_TYPE_FLAGS:
313 case AV_OPT_TYPE_INT:
314 case AV_OPT_TYPE_UINT:
315 6360669 *(int *)dst = llrint(num / den) * intnum;
316 6360669 break;
317 466124 case AV_OPT_TYPE_DURATION:
318 case AV_OPT_TYPE_INT64:{
319 466124 double d = num / den;
320
3/4
✓ Branch 0 taken 1156 times.
✓ Branch 1 taken 464968 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1156 times.
466124 if (intnum == 1 && d == (double)INT64_MAX) {
321 *(int64_t *)dst = INT64_MAX;
322 } else
323 466124 *(int64_t *)dst = llrint(d) * intnum;
324 466124 break;}
325 case AV_OPT_TYPE_UINT64:{
326 double d = num / den;
327 // We must special case uint64_t here as llrint() does not support values
328 // outside the int64_t range and there is no portable function which does
329 // "INT64_MAX + 1ULL" is used as it is representable exactly as IEEE double
330 // while INT64_MAX is not
331 if (intnum == 1 && d == (double)UINT64_MAX) {
332 *(uint64_t *)dst = UINT64_MAX;
333 } else if (d > INT64_MAX + 1ULL) {
334 *(uint64_t *)dst = (llrint(d - (INT64_MAX + 1ULL)) + (INT64_MAX + 1ULL))*intnum;
335 } else {
336 *(uint64_t *)dst = llrint(d) * intnum;
337 }
338 break;}
339 774938 case AV_OPT_TYPE_FLOAT:
340 774938 *(float *)dst = num * intnum / den;
341 774938 break;
342 99757 case AV_OPT_TYPE_DOUBLE:
343 99757 *(double *)dst = num * intnum / den;
344 99757 break;
345 192379 case AV_OPT_TYPE_RATIONAL:
346 case AV_OPT_TYPE_VIDEO_RATE:
347
1/2
✓ Branch 0 taken 192379 times.
✗ Branch 1 not taken.
192379 if ((int) num == num)
348 192379 *(AVRational *)dst = (AVRational) { num *intnum, den };
349 else
350 *(AVRational *)dst = double_to_rational(num * intnum / den);
351 192379 break;
352 default:
353 return AVERROR(EINVAL);
354 }
355 8054082 return 0;
356 }
357
358 16046 static int hexchar2int(char c) {
359
3/4
✓ Branch 0 taken 16046 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11576 times.
✓ Branch 3 taken 4470 times.
16046 if (c >= '0' && c <= '9')
360 11576 return c - '0';
361
4/4
✓ Branch 0 taken 4468 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4467 times.
✓ Branch 3 taken 1 times.
4470 if (c >= 'a' && c <= 'f')
362 4467 return c - 'a' + 10;
363
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 if (c >= 'A' && c <= 'F')
364 2 return c - 'A' + 10;
365 1 return -1;
366 }
367
368 40344 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
369 {
370 40344 int *lendst = (int *)(dst + 1);
371 uint8_t *bin, *ptr;
372 int len;
373
374 40344 av_freep(dst);
375 40344 *lendst = 0;
376
377
4/4
✓ Branch 0 taken 523 times.
✓ Branch 1 taken 39821 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 512 times.
40344 if (!val || !(len = strlen(val)))
378 39832 return 0;
379
380
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 511 times.
512 if (len & 1)
381 1 return AVERROR(EINVAL);
382 511 len /= 2;
383
384 511 ptr = bin = av_malloc(len);
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 511 times.
511 if (!ptr)
386 return AVERROR(ENOMEM);
387
2/2
✓ Branch 0 taken 8023 times.
✓ Branch 1 taken 510 times.
8533 while (*val) {
388 8023 int a = hexchar2int(*val++);
389 8023 int b = hexchar2int(*val++);
390
3/4
✓ Branch 0 taken 8023 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8022 times.
8023 if (a < 0 || b < 0) {
391 1 av_free(bin);
392 1 return AVERROR(EINVAL);
393 }
394 8022 *ptr++ = (a << 4) | b;
395 }
396 510 *dst = bin;
397 510 *lendst = len;
398
399 510 return 0;
400 }
401
402 499946 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
403 {
404 499946 av_freep(dst);
405
2/2
✓ Branch 0 taken 407499 times.
✓ Branch 1 taken 92447 times.
499946 if (!val)
406 407499 return 0;
407 92447 *dst = av_strdup(val);
408
1/2
✓ Branch 0 taken 92447 times.
✗ Branch 1 not taken.
92447 return *dst ? 0 : AVERROR(ENOMEM);
409 }
410
411 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
412 opt->type == AV_OPT_TYPE_UINT64 || \
413 opt->type == AV_OPT_TYPE_CONST || \
414 opt->type == AV_OPT_TYPE_FLAGS || \
415 opt->type == AV_OPT_TYPE_UINT || \
416 opt->type == AV_OPT_TYPE_INT) \
417 ? opt->default_val.i64 \
418 : opt->default_val.dbl)
419
420 123300 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
421 {
422 123300 const enum AVOptionType type = TYPE_BASE(o->type);
423 123300 int ret = 0;
424
425
3/4
✓ Branch 0 taken 122001 times.
✓ Branch 1 taken 1299 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 122001 times.
123300 if (type == AV_OPT_TYPE_RATIONAL || type == AV_OPT_TYPE_VIDEO_RATE) {
426 int num, den;
427 char c;
428
2/2
✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 18 times.
1299 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
429
2/2
✓ Branch 1 taken 1279 times.
✓ Branch 2 taken 2 times.
1281 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
430 1279 return ret;
431 2 ret = 0;
432 }
433 }
434
435 43000 for (;;) {
436 165021 int i = 0;
437 char buf[256];
438 165021 int cmd = 0;
439 double d;
440 165021 int64_t intnum = 1;
441
442
2/2
✓ Branch 0 taken 100219 times.
✓ Branch 1 taken 64802 times.
165021 if (type == AV_OPT_TYPE_FLAGS) {
443
4/4
✓ Branch 0 taken 21771 times.
✓ Branch 1 taken 78448 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 21748 times.
100219 if (*val == '+' || *val == '-')
444 78471 cmd = *(val++);
445
7/8
✓ Branch 0 taken 831403 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 774184 times.
✓ Branch 3 taken 57219 times.
✓ Branch 4 taken 731185 times.
✓ Branch 5 taken 42999 times.
✓ Branch 6 taken 731184 times.
✓ Branch 7 taken 1 times.
831403 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
446 731184 buf[i] = val[i];
447 100219 buf[i] = 0;
448 }
449
450 {
451 int res;
452 165021 int ci = 0;
453 double const_values[64];
454 const char * const_names[64];
455 165021 int search_flags = (o->flags & AV_OPT_FLAG_CHILD_CONSTS) ? AV_OPT_SEARCH_CHILDREN : 0;
456
2/2
✓ Branch 0 taken 100219 times.
✓ Branch 1 taken 64802 times.
165021 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, search_flags);
457
3/4
✓ Branch 0 taken 116952 times.
✓ Branch 1 taken 48069 times.
✓ Branch 2 taken 116952 times.
✗ Branch 3 not taken.
165021 if (o_named && o_named->type == AV_OPT_TYPE_CONST) {
458
3/12
✓ Branch 0 taken 116952 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116952 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 116952 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
116952 d = DEFAULT_NUMVAL(o_named);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116952 times.
116952 if (o_named->flags & AV_OPT_FLAG_DEPRECATED)
460 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n",
461 o_named->name, o_named->help);
462 } else {
463
2/2
✓ Branch 0 taken 34120 times.
✓ Branch 1 taken 13949 times.
48069 if (o->unit) {
464
2/2
✓ Branch 1 taken 7539001 times.
✓ Branch 2 taken 34120 times.
7573121 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
465
2/2
✓ Branch 0 taken 5266595 times.
✓ Branch 1 taken 2272406 times.
7539001 if (o_named->type == AV_OPT_TYPE_CONST &&
466
1/2
✓ Branch 0 taken 5266595 times.
✗ Branch 1 not taken.
5266595 o_named->unit &&
467
2/2
✓ Branch 0 taken 309718 times.
✓ Branch 1 taken 4956877 times.
5266595 !strcmp(o_named->unit, o->unit)) {
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 309718 times.
309718 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
469 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
470 8 return AVERROR_PATCHWELCOME;
471 }
472 309718 const_names [ci ] = o_named->name;
473
3/12
✓ Branch 0 taken 309718 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 309718 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 309718 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
309718 const_values[ci++] = DEFAULT_NUMVAL(o_named);
474 }
475 }
476 }
477 48069 const_names [ci ] = "default";
478
10/12
✓ Branch 0 taken 46910 times.
✓ Branch 1 taken 1159 times.
✓ Branch 2 taken 46910 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46910 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 39000 times.
✓ Branch 7 taken 7910 times.
✓ Branch 8 taken 38918 times.
✓ Branch 9 taken 82 times.
✓ Branch 10 taken 38041 times.
✓ Branch 11 taken 877 times.
48069 const_values[ci++] = DEFAULT_NUMVAL(o);
479 48069 const_names [ci ] = "max";
480 48069 const_values[ci++] = o->max;
481 48069 const_names [ci ] = "min";
482 48069 const_values[ci++] = o->min;
483 48069 const_names [ci ] = "none";
484 48069 const_values[ci++] = 0;
485 48069 const_names [ci ] = "all";
486 48069 const_values[ci++] = ~0;
487 48069 const_names [ci] = NULL;
488 48069 const_values[ci] = 0;
489
490
2/2
✓ Branch 0 taken 7910 times.
✓ Branch 1 taken 40159 times.
48069 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
491 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
492
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 48061 times.
48069 if (res < 0) {
493 8 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
494 8 return res;
495 }
496 }
497 }
498
2/2
✓ Branch 0 taken 100219 times.
✓ Branch 1 taken 64794 times.
165013 if (type == AV_OPT_TYPE_FLAGS) {
499 100219 intnum = *(unsigned int*)dst;
500
2/2
✓ Branch 0 taken 78448 times.
✓ Branch 1 taken 21771 times.
100219 if (cmd == '+')
501 78448 d = intnum | (int64_t)d;
502
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 21748 times.
21771 else if (cmd == '-')
503 23 d = intnum &~(int64_t)d;
504 }
505
506
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 164999 times.
165013 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
507 14 return ret;
508 164999 val += i;
509
4/4
✓ Branch 0 taken 100219 times.
✓ Branch 1 taken 64780 times.
✓ Branch 2 taken 57219 times.
✓ Branch 3 taken 43000 times.
164999 if (!i || !*val)
510 121999 return 0;
511 }
512 }
513
514 40555 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
515 {
516 int ret;
517
518
3/4
✓ Branch 0 taken 5603 times.
✓ Branch 1 taken 34952 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5603 times.
40555 if (!val || !strcmp(val, "none")) {
519 34952 dst[0] =
520 34952 dst[1] = 0;
521 34952 return 0;
522 }
523 5603 ret = av_parse_video_size(dst, dst + 1, val);
524
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5602 times.
5603 if (ret < 0)
525 1 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
526 5603 return ret;
527 }
528
529 8154 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
530 {
531 8154 int ret = av_parse_video_rate(dst, val);
532
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8153 times.
8154 if (ret < 0)
533 1 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
534 8154 return ret;
535 }
536
537 346 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
538 {
539 int ret;
540
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
346 if (!val) {
542 return 0;
543 } else {
544 346 ret = av_parse_color(dst, val, -1, obj);
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
346 if (ret < 0)
546 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
547 346 return ret;
548 }
549 return 0;
550 }
551
552 37 static const char *get_bool_name(int val)
553 {
554
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 32 times.
37 if (val < 0)
555 5 return "auto";
556
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 11 times.
32 return val ? "true" : "false";
557 }
558
559 9573 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
560 {
561 int n;
562
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9573 times.
9573 if (!val)
564 return 0;
565
566
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9570 times.
9573 if (!strcmp(val, "auto")) {
567 3 n = -1;
568
2/2
✓ Branch 1 taken 1269 times.
✓ Branch 2 taken 8301 times.
9570 } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
569 1269 n = 1;
570
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 8289 times.
8301 } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
571 12 n = 0;
572 } else {
573 8289 char *end = NULL;
574 8289 n = strtol(val, &end, 10);
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8289 times.
8289 if (val + strlen(val) != end)
576 goto fail;
577 }
578
579
2/4
✓ Branch 0 taken 9573 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9573 times.
9573 if (n < o->min || n > o->max)
580 goto fail;
581
582 9573 *dst = n;
583 9573 return 0;
584
585 fail:
586 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
587 return AVERROR(EINVAL);
588 }
589
590 4588 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
591 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
592 {
593 int fmt, min, max;
594
595
2/4
✓ Branch 0 taken 4588 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4588 times.
4588 if (!val || !strcmp(val, "none")) {
596 fmt = -1;
597 } else {
598 4588 fmt = get_fmt(val);
599
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4584 times.
4588 if (fmt == -1) {
600 char *tail;
601 4 fmt = strtol(val, &tail, 0);
602
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if (*tail || (unsigned)fmt >= fmt_nb) {
603 2 av_log(obj, AV_LOG_ERROR,
604 "Unable to parse option value \"%s\" as %s\n", val, desc);
605 2 return AVERROR(EINVAL);
606 }
607 }
608 }
609
610
2/2
✓ Branch 0 taken 2047 times.
✓ Branch 1 taken 2539 times.
4586 min = FFMAX(o->min, -1);
611
2/2
✓ Branch 0 taken 2541 times.
✓ Branch 1 taken 2045 times.
4586 max = FFMIN(o->max, fmt_nb-1);
612
613 // hack for compatibility with old ffmpeg
614
4/4
✓ Branch 0 taken 2047 times.
✓ Branch 1 taken 2539 times.
✓ Branch 2 taken 2045 times.
✓ Branch 3 taken 2 times.
4586 if(min == 0 && max == 0) {
615 2045 min = -1;
616 2045 max = fmt_nb-1;
617 }
618
619
2/4
✓ Branch 0 taken 4586 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4586 times.
4586 if (fmt < min || fmt > max) {
620 av_log(obj, AV_LOG_ERROR,
621 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
622 fmt, o->name, desc, min, max);
623 return AVERROR(ERANGE);
624 }
625
626 4586 *(int *)dst = fmt;
627 4586 return 0;
628 }
629
630 7 static int get_pix_fmt(const char *name)
631 {
632 7 return av_get_pix_fmt(name);
633 }
634
635 7 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
636 {
637 7 return set_string_fmt(obj, o, val, dst,
638 AV_PIX_FMT_NB, get_pix_fmt, "pixel format");
639 }
640
641 4581 static int get_sample_fmt(const char *name)
642 {
643 4581 return av_get_sample_fmt(name);
644 }
645
646 4581 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
647 {
648 4581 return set_string_fmt(obj, o, val, dst,
649 AV_SAMPLE_FMT_NB, get_sample_fmt, "sample format");
650 }
651
652 272 static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
653 {
654 272 AVDictionary *options = NULL;
655
656
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 214 times.
272 if (val) {
657 58 int ret = av_dict_parse_string(&options, val, "=", ":", 0);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0) {
659 av_dict_free(&options);
660 return ret;
661 }
662 }
663
664 272 av_dict_free((AVDictionary **)dst);
665 272 *dst = (uint8_t *)options;
666
667 272 return 0;
668 }
669
670 45984 static int set_string_channel_layout(void *obj, const AVOption *o,
671 const char *val, void *dst)
672 {
673 45984 AVChannelLayout *channel_layout = dst;
674 45984 av_channel_layout_uninit(channel_layout);
675
2/2
✓ Branch 0 taken 45290 times.
✓ Branch 1 taken 694 times.
45984 if (!val)
676 45290 return 0;
677 694 return av_channel_layout_from_string(channel_layout, val);
678 }
679
680 186921 static int opt_set_elem(void *obj, void *target_obj, const AVOption *o,
681 const char *val, void *dst)
682 {
683 186921 const enum AVOptionType type = TYPE_BASE(o->type);
684 int ret;
685
686
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 186921 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
186921 if (!val && (type != AV_OPT_TYPE_STRING &&
687 type != AV_OPT_TYPE_PIXEL_FMT && type != AV_OPT_TYPE_SAMPLE_FMT &&
688 type != AV_OPT_TYPE_IMAGE_SIZE &&
689 type != AV_OPT_TYPE_DURATION && type != AV_OPT_TYPE_COLOR &&
690 type != AV_OPT_TYPE_BOOL))
691 return AVERROR(EINVAL);
692
693
12/13
✓ Branch 0 taken 9573 times.
✓ Branch 1 taken 59157 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 110246 times.
✓ Branch 4 taken 2167 times.
✓ Branch 5 taken 202 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 4581 times.
✓ Branch 8 taken 277 times.
✓ Branch 9 taken 38 times.
✓ Branch 10 taken 608 times.
✓ Branch 11 taken 51 times.
✗ Branch 12 not taken.
186921 switch (type) {
694 9573 case AV_OPT_TYPE_BOOL:
695 9573 return set_string_bool(obj, o, val, dst);
696 59157 case AV_OPT_TYPE_STRING:
697 59157 return set_string(obj, o, val, dst);
698 14 case AV_OPT_TYPE_BINARY:
699 14 return set_string_binary(obj, o, val, dst);
700 110246 case AV_OPT_TYPE_FLAGS:
701 case AV_OPT_TYPE_INT:
702 case AV_OPT_TYPE_UINT:
703 case AV_OPT_TYPE_INT64:
704 case AV_OPT_TYPE_UINT64:
705 case AV_OPT_TYPE_FLOAT:
706 case AV_OPT_TYPE_DOUBLE:
707 case AV_OPT_TYPE_RATIONAL:
708 110246 return set_string_number(obj, target_obj, o, val, dst);
709 2167 case AV_OPT_TYPE_IMAGE_SIZE:
710 2167 return set_string_image_size(obj, o, val, dst);
711 202 case AV_OPT_TYPE_VIDEO_RATE: {
712 AVRational tmp;
713 202 ret = set_string_video_rate(obj, o, val, &tmp);
714
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 201 times.
202 if (ret < 0)
715 1 return ret;
716 201 return write_number(obj, o, dst, 1, tmp.den, tmp.num);
717 }
718 7 case AV_OPT_TYPE_PIXEL_FMT:
719 7 return set_string_pixel_fmt(obj, o, val, dst);
720 4581 case AV_OPT_TYPE_SAMPLE_FMT:
721 4581 return set_string_sample_fmt(obj, o, val, dst);
722 277 case AV_OPT_TYPE_DURATION:
723 {
724 277 int64_t usecs = 0;
725
1/2
✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
277 if (val) {
726
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 276 times.
277 if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
727 1 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
728 1 return ret;
729 }
730 }
731
2/4
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 276 times.
276 if (usecs < o->min || usecs > o->max) {
732 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
733 usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
734 return AVERROR(ERANGE);
735 }
736 276 *(int64_t *)dst = usecs;
737 276 return 0;
738 }
739 38 case AV_OPT_TYPE_COLOR:
740 38 return set_string_color(obj, o, val, dst);
741 608 case AV_OPT_TYPE_CHLAYOUT:
742 608 ret = set_string_channel_layout(obj, o, val, dst);
743
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 607 times.
608 if (ret < 0) {
744 1 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
745 1 ret = AVERROR(EINVAL);
746 }
747 608 return ret;
748 51 case AV_OPT_TYPE_DICT:
749 51 return set_string_dict(obj, o, val, dst);
750 }
751
752 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
753 return AVERROR(EINVAL);
754 }
755
756 2146 static int opt_set_array(void *obj, void *target_obj, const AVOption *o,
757 const char *val, void *dst)
758 {
759 2146 const AVOptionArrayDef *arr = o->default_val.arr;
760 2146 const size_t elem_size = opt_type_desc[TYPE_BASE(o->type)].size;
761 2146 const uint8_t sep = opt_array_sep(o);
762 2146 uint8_t *str = NULL;
763
764 2146 void *elems = NULL;
765 2146 unsigned nb_elems = 0;
766 int ret;
767
768
4/4
✓ Branch 0 taken 2135 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2133 times.
✓ Branch 3 taken 2 times.
2146 if (val && *val) {
769 2133 str = av_malloc(strlen(val) + 1);
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2133 times.
2133 if (!str)
771 return AVERROR(ENOMEM);
772 }
773
774 // split and unescape the string
775
4/4
✓ Branch 0 taken 4950 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2816 times.
✓ Branch 3 taken 2134 times.
4961 while (val && *val) {
776 2816 uint8_t *p = str;
777 void *tmp;
778
779
3/6
✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2808 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2816 if (arr && arr->size_max && nb_elems >= arr->size_max) {
780 av_log(obj, AV_LOG_ERROR,
781 "Cannot assign more than %u elements to array option %s\n",
782 arr->size_max, o->name);
783 ret = AVERROR(EINVAL);
784 goto fail;
785 }
786
787
2/2
✓ Branch 0 taken 11958 times.
✓ Branch 1 taken 2133 times.
14091 for (; *val; val++, p++) {
788
3/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 11895 times.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
11958 if (*val == '\\' && val[1])
789 63 val++;
790
2/2
✓ Branch 0 taken 683 times.
✓ Branch 1 taken 11212 times.
11895 else if (*val == sep) {
791 683 val++;
792 683 break;
793 }
794 11275 *p = *val;
795 }
796 2816 *p = 0;
797
798 2816 tmp = av_realloc_array(elems, nb_elems + 1, elem_size);
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2816 times.
2816 if (!tmp) {
800 ret = AVERROR(ENOMEM);
801 goto fail;
802 }
803 2816 elems = tmp;
804
805 2816 tmp = opt_array_pelem(o, elems, nb_elems);
806 2816 memset(tmp, 0, elem_size);
807
808 2816 ret = opt_set_elem(obj, target_obj, o, str, tmp);
809
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2815 times.
2816 if (ret < 0)
810 1 goto fail;
811 2815 nb_elems++;
812 }
813 2145 av_freep(&str);
814
815 2145 opt_free_array(o, dst, opt_array_pcount(dst));
816
817
3/4
✓ Branch 0 taken 2130 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2130 times.
2145 if (arr && nb_elems < arr->size_min) {
818 av_log(obj, AV_LOG_ERROR,
819 "Cannot assign fewer than %u elements to array option %s\n",
820 arr->size_min, o->name);
821 ret = AVERROR(EINVAL);
822 goto fail;
823 }
824
825 2145 *((void **)dst) = elems;
826 2145 *opt_array_pcount(dst) = nb_elems;
827
828 2145 return 0;
829 1 fail:
830 1 av_freep(&str);
831 1 opt_free_array(o, &elems, &nb_elems);
832 1 return ret;
833 }
834
835 232627 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
836 {
837 void *dst, *target_obj;
838 const AVOption *o;
839 int ret;
840
841 232627 ret = opt_set_init(obj, name, search_flags, 0, &target_obj, &o, &dst);
842
2/2
✓ Branch 0 taken 46390 times.
✓ Branch 1 taken 186237 times.
232627 if (ret < 0)
843 46390 return ret;
844
845 186237 return ((o->type & AV_OPT_TYPE_FLAG_ARRAY) ?
846
2/2
✓ Branch 0 taken 2132 times.
✓ Branch 1 taken 184105 times.
186237 opt_set_array : opt_set_elem)(obj, target_obj, o, val, dst);
847 }
848
849 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
850 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
851 const char *val, vartype *name ## _out) \
852 { \
853 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
854 return AVERROR(EINVAL); \
855 return set_string_number(obj, obj, o, val, name ## _out); \
856 }
857
858
3/6
✓ Branch 0 taken 13034 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13034 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 13034 times.
13034 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
859
3/6
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
20 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
860 OPT_EVAL_NUMBER(uint, AV_OPT_TYPE_UINT, unsigned)
861 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
862 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
863 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
864 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
865
866 109426 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
867 int search_flags, int require_type)
868 {
869 void *dst;
870 const AVOption *o;
871 int ret;
872
873 109426 ret = opt_set_init(obj, name, search_flags, require_type, NULL, &o, &dst);
874
2/2
✓ Branch 0 taken 16498 times.
✓ Branch 1 taken 92928 times.
109426 if (ret < 0)
875 16498 return ret;
876
877 92928 return write_number(obj, o, dst, num, den, intnum);
878 }
879
880 109426 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
881 {
882 109426 return set_number(obj, name, 1, 1, val, search_flags, 0);
883 }
884
885 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
886 {
887 return set_number(obj, name, val, 1, 1, search_flags, 0);
888 }
889
890 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
891 {
892 return set_number(obj, name, val.num, val.den, 1, search_flags, 0);
893 }
894
895 24 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
896 {
897 uint8_t *ptr;
898 uint8_t **dst;
899 int *lendst;
900 int ret;
901
902 24 ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_BINARY,
903 NULL, NULL, (void**)&dst);
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
905 return ret;
906
907
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 ptr = len ? av_malloc(len) : NULL;
908
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (len && !ptr)
909 return AVERROR(ENOMEM);
910
911 24 lendst = (int *)(dst + 1);
912
913 24 av_free(*dst);
914 24 *dst = ptr;
915 24 *lendst = len;
916
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (len)
917 24 memcpy(ptr, val, len);
918
919 24 return 0;
920 }
921
922 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
923 {
924 const AVOption *o;
925 int *dst;
926 int ret;
927
928 ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_IMAGE_SIZE,
929 NULL, &o, (void**)&dst);
930 if (ret < 0)
931 return ret;
932
933 if (w<0 || h<0) {
934 av_log(obj, AV_LOG_ERROR,
935 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
936 return AVERROR(EINVAL);
937 }
938
939 dst[0] = w;
940 dst[1] = h;
941
942 return 0;
943 }
944
945 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
946 {
947 return set_number(obj, name, val.num, val.den, 1, search_flags, AV_OPT_TYPE_VIDEO_RATE);
948 }
949
950 static int set_format(void *obj, const char *name, int fmt, int search_flags,
951 enum AVOptionType type, const char *desc, int nb_fmts)
952 {
953 const AVOption *o;
954 int *dst;
955 int min, max, ret;
956
957 ret = opt_set_init(obj, name, search_flags, type, NULL, &o, (void**)&dst);
958 if (ret < 0)
959 return ret;
960
961 min = FFMAX(o->min, -1);
962 max = FFMIN(o->max, nb_fmts-1);
963
964 if (fmt < min || fmt > max) {
965 av_log(obj, AV_LOG_ERROR,
966 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
967 fmt, name, desc, min, max);
968 return AVERROR(ERANGE);
969 }
970 *dst = fmt;
971 return 0;
972 }
973
974 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
975 {
976 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
977 }
978
979 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
980 {
981 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
982 }
983
984 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
985 int search_flags)
986 {
987 AVDictionary **dst;
988 int ret;
989
990 ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_DICT, NULL, NULL,
991 (void**)&dst);
992 if (ret < 0)
993 return ret;
994
995 av_dict_free(dst);
996
997 return av_dict_copy(dst, val, 0);
998 }
999
1000 3164 int av_opt_set_chlayout(void *obj, const char *name,
1001 const AVChannelLayout *channel_layout,
1002 int search_flags)
1003 {
1004 AVChannelLayout *dst;
1005 int ret;
1006
1007 3164 ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_CHLAYOUT, NULL, NULL,
1008 (void**)&dst);
1009
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3164 times.
3164 if (ret < 0)
1010 return ret;
1011
1012 3164 return av_channel_layout_copy(dst, channel_layout);
1013 }
1014
1015 5 static void format_duration(char *buf, size_t size, int64_t d)
1016 {
1017 char *e;
1018
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 av_assert0(size >= 25);
1020
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (d < 0 && d != INT64_MIN) {
1021 *(buf++) = '-';
1022 size--;
1023 d = -d;
1024 }
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (d == INT64_MAX)
1026 snprintf(buf, size, "INT64_MAX");
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 else if (d == INT64_MIN)
1028 snprintf(buf, size, "INT64_MIN");
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 else if (d > (int64_t)3600*1000000)
1030 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
1031 (int)((d / 60000000) % 60),
1032 (int)((d / 1000000) % 60),
1033 (int)(d % 1000000));
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 else if (d > 60*1000000)
1035 snprintf(buf, size, "%d:%02d.%06d",
1036 (int)(d / 60000000),
1037 (int)((d / 1000000) % 60),
1038 (int)(d % 1000000));
1039 else
1040 5 snprintf(buf, size, "%d.%06d",
1041 5 (int)(d / 1000000),
1042 5 (int)(d % 1000000));
1043 5 e = buf + strlen(buf);
1044
3/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 5 times.
20 while (e > buf && e[-1] == '0')
1045 15 *(--e) = 0;
1046
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (e > buf && e[-1] == '.')
1047 *(--e) = 0;
1048 5 }
1049
1050 10764 static int opt_get_elem(const AVOption *o, uint8_t **pbuf, size_t buf_len,
1051 const void *dst, int search_flags)
1052 {
1053 int ret;
1054
1055
17/20
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 5557 times.
✓ Branch 2 taken 5026 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 26 times.
✓ Branch 11 taken 12 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 4 times.
✓ Branch 14 taken 4 times.
✓ Branch 15 taken 4 times.
✓ Branch 16 taken 4 times.
✓ Branch 17 taken 8 times.
✓ Branch 18 taken 22 times.
✗ Branch 19 not taken.
10764 switch (TYPE_BASE(o->type)) {
1056 34 case AV_OPT_TYPE_BOOL:
1057 34 ret = snprintf(*pbuf, buf_len, "%s", get_bool_name(*(int *)dst));
1058 34 break;
1059 5557 case AV_OPT_TYPE_FLAGS:
1060 5557 ret = snprintf(*pbuf, buf_len, "0x%08X", *(int *)dst);
1061 5557 break;
1062 5026 case AV_OPT_TYPE_INT:
1063 5026 ret = snprintf(*pbuf, buf_len, "%d", *(int *)dst);
1064 5026 break;
1065 19 case AV_OPT_TYPE_UINT:
1066 19 ret = snprintf(*pbuf, buf_len, "%u", *(unsigned *)dst);
1067 19 break;
1068 18 case AV_OPT_TYPE_INT64:
1069 18 ret = snprintf(*pbuf, buf_len, "%"PRId64, *(int64_t *)dst);
1070 18 break;
1071 case AV_OPT_TYPE_UINT64:
1072 ret = snprintf(*pbuf, buf_len, "%"PRIu64, *(uint64_t *)dst);
1073 break;
1074 5 case AV_OPT_TYPE_FLOAT:
1075 5 ret = snprintf(*pbuf, buf_len, "%f", *(float *)dst);
1076 5 break;
1077 5 case AV_OPT_TYPE_DOUBLE:
1078 5 ret = snprintf(*pbuf, buf_len, "%f", *(double *)dst);
1079 5 break;
1080 12 case AV_OPT_TYPE_VIDEO_RATE:
1081 case AV_OPT_TYPE_RATIONAL:
1082 12 ret = snprintf(*pbuf, buf_len, "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
1083 12 break;
1084 case AV_OPT_TYPE_CONST:
1085 ret = snprintf(*pbuf, buf_len, "%"PRId64, o->default_val.i64);
1086 break;
1087 26 case AV_OPT_TYPE_STRING:
1088
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (*(uint8_t **)dst) {
1089 26 *pbuf = av_strdup(*(uint8_t **)dst);
1090 } else if (search_flags & AV_OPT_ALLOW_NULL) {
1091 *pbuf = NULL;
1092 return 0;
1093 } else {
1094 *pbuf = av_strdup("");
1095 }
1096
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 return *pbuf ? 0 : AVERROR(ENOMEM);
1097 12 case AV_OPT_TYPE_BINARY: {
1098 const uint8_t *bin;
1099 int len;
1100
1101
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
12 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
1102 *pbuf = NULL;
1103 return 0;
1104 }
1105 12 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if ((uint64_t)len * 2 + 1 > INT_MAX)
1107 return AVERROR(EINVAL);
1108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (!(*pbuf = av_malloc(len * 2 + 1)))
1109 return AVERROR(ENOMEM);
1110
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (!len) {
1111 8 *pbuf[0] = '\0';
1112 8 return 0;
1113 }
1114 4 bin = *(uint8_t **)dst;
1115
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < len; i++)
1116 16 snprintf(*pbuf + i * 2, 3, "%02X", bin[i]);
1117 4 return 0;
1118 }
1119 4 case AV_OPT_TYPE_IMAGE_SIZE:
1120 4 ret = snprintf(*pbuf, buf_len, "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
1121 4 break;
1122 4 case AV_OPT_TYPE_PIXEL_FMT:
1123 4 ret = snprintf(*pbuf, buf_len, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
1124 4 break;
1125 4 case AV_OPT_TYPE_SAMPLE_FMT:
1126 4 ret = snprintf(*pbuf, buf_len, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
1127 4 break;
1128 4 case AV_OPT_TYPE_DURATION: {
1129 4 int64_t i64 = *(int64_t *)dst;
1130 4 format_duration(*pbuf, buf_len, i64);
1131 4 ret = strlen(*pbuf); // no overflow possible, checked by an assert
1132 4 break;
1133 }
1134 4 case AV_OPT_TYPE_COLOR:
1135 4 ret = snprintf(*pbuf, buf_len, "0x%02x%02x%02x%02x",
1136 4 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
1137 4 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
1138 4 break;
1139 8 case AV_OPT_TYPE_CHLAYOUT:
1140 8 ret = av_channel_layout_describe(dst, *pbuf, buf_len);
1141 8 break;
1142 22 case AV_OPT_TYPE_DICT:
1143
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
22 if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
1144 *pbuf = NULL;
1145 return 0;
1146 }
1147 22 return av_dict_get_string(*(AVDictionary **)dst, (char **)pbuf, '=', ':');
1148 default:
1149 return AVERROR(EINVAL);
1150 }
1151
1152 10704 return ret;
1153 }
1154
1155 61 static int opt_get_array(const AVOption *o, void *dst, uint8_t **out_val)
1156 {
1157 61 const unsigned count = *opt_array_pcount(dst);
1158 61 const uint8_t sep = opt_array_sep(o);
1159
1160 61 uint8_t *str = NULL;
1161 61 size_t str_len = 0;
1162 int ret;
1163
1164 61 *out_val = NULL;
1165
1166
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 61 times.
91 for (unsigned i = 0; i < count; i++) {
1167 30 uint8_t buf[128], *out = buf;
1168 size_t out_len;
1169
1170 30 ret = opt_get_elem(o, &out, sizeof(buf),
1171 30 opt_array_pelem(o, *(void **)dst, i), 0);
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
1173 goto fail;
1174
1175 30 out_len = strlen(out);
1176
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 if (out_len > SIZE_MAX / 2 - !!i ||
1177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 !!i + out_len * 2 > SIZE_MAX - str_len - 1) {
1178 ret = AVERROR(ERANGE);
1179 goto fail;
1180 }
1181
1182 // terminator escaping separator
1183 // ↓ ↓ ↓
1184 30 ret = av_reallocp(&str, str_len + 1 + out_len * 2 + !!i);
1185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
1186 goto fail;
1187
1188 // add separator if needed
1189
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
30 if (i)
1190 18 str[str_len++] = sep;
1191
1192 // escape the element
1193
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 30 times.
288 for (unsigned j = 0; j < out_len; j++) {
1194 258 uint8_t val = out[j];
1195
4/4
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 216 times.
258 if (val == sep || val == '\\')
1196 42 str[str_len++] = '\\';
1197 258 str[str_len++] = val;
1198 }
1199 30 str[str_len] = 0;
1200
1201 30 fail:
1202
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (out != buf)
1203 30 av_freep(&out);
1204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0) {
1205 av_freep(&str);
1206 return ret;
1207 }
1208 }
1209
1210 61 *out_val = str;
1211
1212 61 return 0;
1213 }
1214
1215 15012 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
1216 {
1217 void *dst, *target_obj;
1218 15012 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1219 uint8_t *out, buf[128];
1220 int ret;
1221
1222
4/8
✓ Branch 0 taken 10781 times.
✓ Branch 1 taken 4231 times.
✓ Branch 2 taken 10781 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10781 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
15012 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
1223 4231 return AVERROR_OPTION_NOT_FOUND;
1224
1225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10781 times.
10781 if (o->flags & AV_OPT_FLAG_DEPRECATED)
1226 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
1227
1228 10781 dst = (uint8_t *)target_obj + o->offset;
1229
1230
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 10729 times.
10781 if (o->type & AV_OPT_TYPE_FLAG_ARRAY) {
1231 52 ret = opt_get_array(o, dst, out_val);
1232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
1233 return ret;
1234
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 1 times.
52 if (!*out_val && !(search_flags & AV_OPT_ALLOW_NULL)) {
1235 43 *out_val = av_strdup("");
1236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (!*out_val)
1237 return AVERROR(ENOMEM);
1238 }
1239 52 return 0;
1240 }
1241
1242 10729 buf[0] = 0;
1243 10729 out = buf;
1244 10729 ret = opt_get_elem(o, &out, sizeof(buf), dst, search_flags);
1245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10729 times.
10729 if (ret < 0)
1246 return ret;
1247
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10699 times.
10729 if (out != buf) {
1248 30 *out_val = out;
1249 30 return 0;
1250 }
1251
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10699 times.
10699 if (ret >= sizeof(buf))
1253 return AVERROR(EINVAL);
1254 10699 *out_val = av_strdup(out);
1255
1/2
✓ Branch 0 taken 10699 times.
✗ Branch 1 not taken.
10699 return *out_val ? 0 : AVERROR(ENOMEM);
1256 }
1257
1258 21928 static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
1259 int search_flags)
1260 {
1261 void *dst, *target_obj;
1262 21928 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1263
3/4
✓ Branch 0 taken 21914 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21914 times.
21928 if (!o || !target_obj)
1264 14 return AVERROR_OPTION_NOT_FOUND;
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21914 times.
21914 if (o->type & AV_OPT_TYPE_FLAG_ARRAY)
1266 return AVERROR(EINVAL);
1267
1268 21914 dst = ((uint8_t *)target_obj) + o->offset;
1269
1270 21914 return read_number(o, dst, num, den, intnum);
1271 }
1272
1273 21928 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
1274 {
1275 21928 int64_t intnum = 1;
1276 21928 double num = 1;
1277 21928 int ret, den = 1;
1278
1279
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 21914 times.
21928 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
1280 14 return ret;
1281
1/2
✓ Branch 0 taken 21914 times.
✗ Branch 1 not taken.
21914 if (num == den)
1282 21914 *out_val = intnum;
1283 else
1284 *out_val = num * intnum / den;
1285 21914 return 0;
1286 }
1287
1288 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
1289 {
1290 int64_t intnum = 1;
1291 double num = 1;
1292 int ret, den = 1;
1293
1294 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
1295 return ret;
1296 *out_val = num * intnum / den;
1297 return 0;
1298 }
1299
1300 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
1301 {
1302 int64_t intnum = 1;
1303 double num = 1;
1304 int ret, den = 1;
1305
1306 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
1307 return ret;
1308
1309 if (num == 1.0 && (int)intnum == intnum)
1310 *out_val = (AVRational){intnum, den};
1311 else
1312 *out_val = double_to_rational(num*intnum/den);
1313 return 0;
1314 }
1315
1316 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
1317 {
1318 void *dst, *target_obj;
1319 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1320 if (!o || !target_obj)
1321 return AVERROR_OPTION_NOT_FOUND;
1322 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
1323 av_log(obj, AV_LOG_ERROR,
1324 "The value for option '%s' is not a image size.\n", name);
1325 return AVERROR(EINVAL);
1326 }
1327
1328 dst = ((uint8_t*)target_obj) + o->offset;
1329 if (w_out) *w_out = *(int *)dst;
1330 if (h_out) *h_out = *((int *)dst+1);
1331 return 0;
1332 }
1333
1334 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
1335 {
1336 return av_opt_get_q(obj, name, search_flags, out_val);
1337 }
1338
1339 2928 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
1340 enum AVOptionType type, const char *desc)
1341 {
1342 void *dst, *target_obj;
1343 2928 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1344
2/4
✓ Branch 0 taken 2928 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2928 times.
2928 if (!o || !target_obj)
1345 return AVERROR_OPTION_NOT_FOUND;
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2928 times.
2928 if (o->type != type) {
1347 av_log(obj, AV_LOG_ERROR,
1348 "The value for option '%s' is not a %s format.\n", desc, name);
1349 return AVERROR(EINVAL);
1350 }
1351
1352 2928 dst = ((uint8_t*)target_obj) + o->offset;
1353 2928 *out_fmt = *(int *)dst;
1354 2928 return 0;
1355 }
1356
1357 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
1358 {
1359 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
1360 }
1361
1362 2928 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
1363 {
1364 2928 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
1365 }
1366
1367 2928 int av_opt_get_chlayout(void *obj, const char *name, int search_flags, AVChannelLayout *cl)
1368 {
1369 void *dst, *target_obj;
1370 2928 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1371
2/4
✓ Branch 0 taken 2928 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2928 times.
2928 if (!o || !target_obj)
1372 return AVERROR_OPTION_NOT_FOUND;
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2928 times.
2928 if (o->type != AV_OPT_TYPE_CHLAYOUT) {
1374 av_log(obj, AV_LOG_ERROR,
1375 "The value for option '%s' is not a channel layout.\n", name);
1376 return AVERROR(EINVAL);
1377 }
1378
1379 2928 dst = ((uint8_t*)target_obj) + o->offset;
1380 2928 return av_channel_layout_copy(cl, dst);
1381 }
1382
1383 7533 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1384 {
1385 void *target_obj;
1386 AVDictionary *src;
1387 7533 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1388
1389
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7533 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7533 if (!o || !target_obj)
1390 7533 return AVERROR_OPTION_NOT_FOUND;
1391 if (o->type != AV_OPT_TYPE_DICT)
1392 return AVERROR(EINVAL);
1393
1394 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1395
1396 return av_dict_copy(out_val, src, 0);
1397 }
1398
1399 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1400 {
1401 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1402 const AVOption *flag = av_opt_find(obj, flag_name,
1403 field ? field->unit : NULL, 0, 0);
1404 int64_t res;
1405
1406 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1407 av_opt_get_int(obj, field_name, 0, &res) < 0)
1408 return 0;
1409 return res & flag->default_val.i64;
1410 }
1411
1412 4 static void log_int_value(void *av_log_obj, int level, int64_t i)
1413 {
1414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (i == INT_MAX) {
1415 av_log(av_log_obj, level, "INT_MAX");
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (i == INT_MIN) {
1417 av_log(av_log_obj, level, "INT_MIN");
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (i == UINT32_MAX) {
1419 av_log(av_log_obj, level, "UINT32_MAX");
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (i == INT64_MAX) {
1421 av_log(av_log_obj, level, "I64_MAX");
1422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (i == INT64_MIN) {
1423 av_log(av_log_obj, level, "I64_MIN");
1424 } else {
1425 4 av_log(av_log_obj, level, "%"PRId64, i);
1426 }
1427 4 }
1428
1429 16 static void log_value(void *av_log_obj, int level, double d)
1430 {
1431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (d == INT_MAX) {
1432 av_log(av_log_obj, level, "INT_MAX");
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == INT_MIN) {
1434 av_log(av_log_obj, level, "INT_MIN");
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == UINT32_MAX) {
1436 av_log(av_log_obj, level, "UINT32_MAX");
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == (double)INT64_MAX) {
1438 av_log(av_log_obj, level, "I64_MAX");
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == INT64_MIN) {
1440 av_log(av_log_obj, level, "I64_MIN");
1441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == FLT_MAX) {
1442 av_log(av_log_obj, level, "FLT_MAX");
1443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == FLT_MIN) {
1444 av_log(av_log_obj, level, "FLT_MIN");
1445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == -FLT_MAX) {
1446 av_log(av_log_obj, level, "-FLT_MAX");
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == -FLT_MIN) {
1448 av_log(av_log_obj, level, "-FLT_MIN");
1449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == DBL_MAX) {
1450 av_log(av_log_obj, level, "DBL_MAX");
1451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == DBL_MIN) {
1452 av_log(av_log_obj, level, "DBL_MIN");
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == -DBL_MAX) {
1454 av_log(av_log_obj, level, "-DBL_MAX");
1455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (d == -DBL_MIN) {
1456 av_log(av_log_obj, level, "-DBL_MIN");
1457 } else {
1458 16 av_log(av_log_obj, level, "%g", d);
1459 }
1460 16 }
1461
1462 4 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1463 {
1464 4 const AVOption *opt = NULL;
1465
1466
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!unit)
1467 4 return NULL;
1468 while ((opt = av_opt_next(obj, opt)))
1469 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1470 opt->default_val.i64 == value)
1471 return opt->name;
1472 return NULL;
1473 }
1474
1475 1 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1476 {
1477 1 const AVOption *opt = NULL;
1478 char flags[512];
1479
1480 1 flags[0] = 0;
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!unit)
1482 return NULL;
1483
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
32 while ((opt = av_opt_next(obj, opt))) {
1484
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
31 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1485
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 opt->default_val.i64 & value) {
1486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (flags[0])
1487 av_strlcatf(flags, sizeof(flags), "+");
1488 1 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1489 }
1490 }
1491
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (flags[0])
1492 1 return av_strdup(flags);
1493 return NULL;
1494 }
1495
1496 31 static void log_type(void *av_log_obj, const AVOption *o,
1497 enum AVOptionType parent_type)
1498 {
1499 31 const enum AVOptionType type = TYPE_BASE(o->type);
1500
1501
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
31 if (o->type == AV_OPT_TYPE_CONST && TYPE_BASE(parent_type) == AV_OPT_TYPE_INT)
1502 av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", o->default_val.i64);
1503
3/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 3 times.
31 else if (type < FF_ARRAY_ELEMS(opt_type_desc) && opt_type_desc[type].name) {
1504
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 25 times.
28 if (o->type & AV_OPT_TYPE_FLAG_ARRAY)
1505 3 av_log(av_log_obj, AV_LOG_INFO, "[%-10s]", opt_type_desc[type].name);
1506 else
1507 25 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", opt_type_desc[type].name);
1508 }
1509 else
1510 3 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1511 31 }
1512
1513 31 static void log_default(void *obj, void *av_log_obj, const AVOption *opt)
1514 {
1515
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 25 times.
31 if (opt->type == AV_OPT_TYPE_CONST || opt->type == AV_OPT_TYPE_BINARY)
1516 6 return;
1517
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 if ((opt->type == AV_OPT_TYPE_COLOR ||
1518
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 times.
24 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1519
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
23 opt->type == AV_OPT_TYPE_STRING ||
1520
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
21 opt->type == AV_OPT_TYPE_DICT ||
1521
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 opt->type == AV_OPT_TYPE_CHLAYOUT ||
1522
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
18 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1523
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 !opt->default_val.str)
1524 1 return;
1525
1526
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 21 times.
24 if (opt->type & AV_OPT_TYPE_FLAG_ARRAY) {
1527 3 const AVOptionArrayDef *arr = opt->default_val.arr;
1528
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
3 if (arr && arr->def)
1529 2 av_log(av_log_obj, AV_LOG_INFO, " (default %s)", arr->def);
1530 3 return;
1531 }
1532
1533 21 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1534
9/10
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
21 switch (opt->type) {
1535 3 case AV_OPT_TYPE_BOOL:
1536 3 av_log(av_log_obj, AV_LOG_INFO, "%s", get_bool_name(opt->default_val.i64));
1537 3 break;
1538 1 case AV_OPT_TYPE_FLAGS: {
1539 1 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1540
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (def_flags) {
1541 1 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1542 1 av_freep(&def_flags);
1543 } else {
1544 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1545 }
1546 1 break;
1547 }
1548 1 case AV_OPT_TYPE_DURATION: {
1549 char buf[25];
1550 1 format_duration(buf, sizeof(buf), opt->default_val.i64);
1551 1 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1552 1 break;
1553 }
1554 4 case AV_OPT_TYPE_UINT:
1555 case AV_OPT_TYPE_INT:
1556 case AV_OPT_TYPE_UINT64:
1557 case AV_OPT_TYPE_INT64: {
1558 4 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (def_const)
1560 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1561 else
1562 4 log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1563 4 break;
1564 }
1565 2 case AV_OPT_TYPE_DOUBLE:
1566 case AV_OPT_TYPE_FLOAT:
1567 2 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1568 2 break;
1569 1 case AV_OPT_TYPE_RATIONAL: {
1570 1 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1571 1 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1572 1 break;
1573 1 case AV_OPT_TYPE_PIXEL_FMT:
1574 1 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1575 1 break;
1576 1 case AV_OPT_TYPE_SAMPLE_FMT:
1577 1 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1578 1 break;
1579 7 case AV_OPT_TYPE_COLOR:
1580 case AV_OPT_TYPE_IMAGE_SIZE:
1581 case AV_OPT_TYPE_STRING:
1582 case AV_OPT_TYPE_DICT:
1583 case AV_OPT_TYPE_VIDEO_RATE:
1584 case AV_OPT_TYPE_CHLAYOUT:
1585 7 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1586 7 break;
1587 }
1588 21 av_log(av_log_obj, AV_LOG_INFO, ")");
1589 }
1590
1591 2 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1592 int req_flags, int rej_flags, enum AVOptionType parent_type)
1593 {
1594 2 const AVOption *opt = NULL;
1595 AVOptionRanges *r;
1596 int i;
1597
1598
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 2 times.
64 while ((opt = av_opt_next(obj, opt))) {
1599
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1600 continue;
1601
1602 /* Don't print CONST's on level one.
1603 * Don't print anything but CONST's on level two.
1604 * Only print items from the requested unit.
1605 */
1606
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 28 times.
62 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1607 3 continue;
1608
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 3 times.
59 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1609 28 continue;
1610
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
31 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1611 continue;
1612
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
31 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1613 3 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1614 else
1615 28 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1616 28 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? " " : "-",
1617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 opt->name);
1618
1619 31 log_type(av_log_obj, opt, parent_type);
1620
1621 341 av_log(av_log_obj, AV_LOG_INFO, "%c%c%c%c%c%c%c%c%c%c%c",
1622
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3 times.
31 (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.',
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.',
1624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? 'F' : '.',
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) ? 'V' : '.',
1626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_AUDIO_PARAM) ? 'A' : '.',
1627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.',
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.',
1629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.',
1630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.',
1631
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
31 (opt->flags & AV_OPT_FLAG_RUNTIME_PARAM) ? 'T' : '.',
1632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (opt->flags & AV_OPT_FLAG_DEPRECATED) ? 'P' : '.');
1633
1634
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (opt->help)
1635 31 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1636
1637
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 13 times.
31 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1638
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 11 times.
18 switch (opt->type) {
1639 7 case AV_OPT_TYPE_INT:
1640 case AV_OPT_TYPE_UINT:
1641 case AV_OPT_TYPE_INT64:
1642 case AV_OPT_TYPE_UINT64:
1643 case AV_OPT_TYPE_DOUBLE:
1644 case AV_OPT_TYPE_FLOAT:
1645 case AV_OPT_TYPE_RATIONAL:
1646
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 for (i = 0; i < r->nb_ranges; i++) {
1647 7 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1648 7 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1649 7 av_log(av_log_obj, AV_LOG_INFO, " to ");
1650 7 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1651 7 av_log(av_log_obj, AV_LOG_INFO, ")");
1652 }
1653 7 break;
1654 }
1655 18 av_opt_freep_ranges(&r);
1656 }
1657
1658 31 log_default(obj, av_log_obj, opt);
1659
1660 31 av_log(av_log_obj, AV_LOG_INFO, "\n");
1661
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
31 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1662 1 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
1663 }
1664 2 }
1665
1666 1 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1667 {
1668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!obj)
1669 return -1;
1670
1671 1 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1672
1673 1 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
1674
1675 1 return 0;
1676 }
1677
1678 366328 void av_opt_set_defaults(void *s)
1679 {
1680 366328 av_opt_set_defaults2(s, 0, 0);
1681 366328 }
1682
1683 427886 void av_opt_set_defaults2(void *s, int mask, int flags)
1684 {
1685 427886 const AVOption *opt = NULL;
1686
2/2
✓ Branch 1 taken 26697320 times.
✓ Branch 2 taken 427886 times.
27125206 while ((opt = av_opt_next(s, opt))) {
1687 26697320 void *dst = ((uint8_t*)s) + opt->offset;
1688
1689
2/2
✓ Branch 0 taken 3042427 times.
✓ Branch 1 taken 23654893 times.
26697320 if ((opt->flags & mask) != flags)
1690 3042427 continue;
1691
1692
2/2
✓ Branch 0 taken 2098 times.
✓ Branch 1 taken 23652795 times.
23654893 if (opt->flags & AV_OPT_FLAG_READONLY)
1693 2098 continue;
1694
1695
2/2
✓ Branch 0 taken 97253 times.
✓ Branch 1 taken 23555542 times.
23652795 if (opt->type & AV_OPT_TYPE_FLAG_ARRAY) {
1696 97253 const AVOptionArrayDef *arr = opt->default_val.arr;
1697 97253 const char sep = opt_array_sep(opt);
1698
1699
11/16
✓ Branch 0 taken 97253 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97253 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11725 times.
✓ Branch 5 taken 85528 times.
✓ Branch 6 taken 11725 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 11725 times.
✓ Branch 9 taken 85528 times.
✓ Branch 10 taken 11725 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 11725 times.
✓ Branch 13 taken 85528 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 11725 times.
97253 av_assert0(sep && sep != '\\' &&
1700 (sep < 'a' || sep > 'z') &&
1701 (sep < 'A' || sep > 'Z') &&
1702 (sep < '0' || sep > '9'));
1703
1704
4/4
✓ Branch 0 taken 11732 times.
✓ Branch 1 taken 85521 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 11718 times.
97253 if (arr && arr->def)
1705 14 opt_set_array(s, s, opt, arr->def, dst);
1706
1707 97253 continue;
1708 }
1709
1710
11/12
✓ Branch 0 taken 15187505 times.
✓ Branch 1 taken 6729610 times.
✓ Branch 2 taken 874184 times.
✓ Branch 3 taken 190881 times.
✓ Branch 4 taken 308 times.
✓ Branch 5 taken 440789 times.
✓ Branch 6 taken 38388 times.
✓ Branch 7 taken 7952 times.
✓ Branch 8 taken 40328 times.
✓ Branch 9 taken 45376 times.
✓ Branch 10 taken 221 times.
✗ Branch 11 not taken.
23555542 switch (opt->type) {
1711 15187505 case AV_OPT_TYPE_CONST:
1712 /* Nothing to be done here */
1713 15187505 break;
1714 6729610 case AV_OPT_TYPE_BOOL:
1715 case AV_OPT_TYPE_FLAGS:
1716 case AV_OPT_TYPE_INT:
1717 case AV_OPT_TYPE_UINT:
1718 case AV_OPT_TYPE_INT64:
1719 case AV_OPT_TYPE_UINT64:
1720 case AV_OPT_TYPE_DURATION:
1721 case AV_OPT_TYPE_PIXEL_FMT:
1722 case AV_OPT_TYPE_SAMPLE_FMT:
1723 6729610 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1724 6729610 break;
1725 874184 case AV_OPT_TYPE_DOUBLE:
1726 case AV_OPT_TYPE_FLOAT: {
1727 double val;
1728 874184 val = opt->default_val.dbl;
1729 874184 write_number(s, opt, dst, val, 1, 1);
1730 }
1731 874184 break;
1732 190881 case AV_OPT_TYPE_RATIONAL: {
1733 AVRational val;
1734 190881 val = av_d2q(opt->default_val.dbl, INT_MAX);
1735 190881 write_number(s, opt, dst, 1, val.den, val.num);
1736 }
1737 190881 break;
1738 308 case AV_OPT_TYPE_COLOR:
1739 308 set_string_color(s, opt, opt->default_val.str, dst);
1740 308 break;
1741 440789 case AV_OPT_TYPE_STRING:
1742 440789 set_string(s, opt, opt->default_val.str, dst);
1743 440789 break;
1744 38388 case AV_OPT_TYPE_IMAGE_SIZE:
1745 38388 set_string_image_size(s, opt, opt->default_val.str, dst);
1746 38388 break;
1747 7952 case AV_OPT_TYPE_VIDEO_RATE:
1748 7952 set_string_video_rate(s, opt, opt->default_val.str, dst);
1749 7952 break;
1750 40328 case AV_OPT_TYPE_BINARY:
1751 40328 set_string_binary(s, opt, opt->default_val.str, dst);
1752 40328 break;
1753 45376 case AV_OPT_TYPE_CHLAYOUT:
1754 45376 set_string_channel_layout(s, opt, opt->default_val.str, dst);
1755 45376 break;
1756 221 case AV_OPT_TYPE_DICT:
1757 221 set_string_dict(s, opt, opt->default_val.str, dst);
1758 221 break;
1759 default:
1760 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1761 opt->type, opt->name);
1762 }
1763 }
1764 427886 }
1765
1766 /**
1767 * Store the value in the field in ctx that is named like key.
1768 * ctx must be an AVClass context, storing is done using AVOptions.
1769 *
1770 * @param buf the string to parse, buf will be updated to point at the
1771 * separator just after the parsed key/value pair
1772 * @param key_val_sep a 0-terminated list of characters used to
1773 * separate key from value
1774 * @param pairs_sep a 0-terminated list of characters used to separate
1775 * two pairs from each other
1776 * @return 0 if the key/value pair has been successfully parsed and
1777 * set, or a negative value corresponding to an AVERROR code in case
1778 * of error:
1779 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1780 * the error code issued by av_opt_set() if the key/value pair
1781 * cannot be set
1782 */
1783 3114 static int parse_key_value_pair(void *ctx, const char **buf,
1784 const char *key_val_sep, const char *pairs_sep)
1785 {
1786 3114 char *key = av_get_token(buf, key_val_sep);
1787 char *val;
1788 int ret;
1789
1790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3114 times.
3114 if (!key)
1791 return AVERROR(ENOMEM);
1792
1793
4/4
✓ Branch 0 taken 3112 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3109 times.
✓ Branch 3 taken 3 times.
3114 if (*key && strspn(*buf, key_val_sep)) {
1794 3109 (*buf)++;
1795 3109 val = av_get_token(buf, pairs_sep);
1796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3109 times.
3109 if (!val) {
1797 av_freep(&key);
1798 return AVERROR(ENOMEM);
1799 }
1800 } else {
1801 5 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1802 5 av_free(key);
1803 5 return AVERROR(EINVAL);
1804 }
1805
1806 3109 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1807
1808 3109 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1809
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3104 times.
3109 if (ret == AVERROR_OPTION_NOT_FOUND)
1810 5 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1811
1812 3109 av_free(key);
1813 3109 av_free(val);
1814 3109 return ret;
1815 }
1816
1817 3081 int av_set_options_string(void *ctx, const char *opts,
1818 const char *key_val_sep, const char *pairs_sep)
1819 {
1820 3081 int ret, count = 0;
1821
1822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3081 times.
3081 if (!opts)
1823 return 0;
1824
1825
2/2
✓ Branch 0 taken 3114 times.
✓ Branch 1 taken 3042 times.
6156 while (*opts) {
1826
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 3075 times.
3114 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1827 39 return ret;
1828 3075 count++;
1829
1830
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 3040 times.
3075 if (*opts)
1831 35 opts++;
1832 }
1833
1834 3042 return count;
1835 }
1836
1837 #define WHITESPACES " \n\t\r"
1838
1839 409981 static int is_key_char(char c)
1840 {
1841 529955 return (unsigned)((c | 32) - 'a') < 26 ||
1842
4/4
✓ Branch 0 taken 70962 times.
✓ Branch 1 taken 49012 times.
✓ Branch 2 taken 70888 times.
✓ Branch 3 taken 74 times.
119974 (unsigned)(c - '0') < 10 ||
1843
8/8
✓ Branch 0 taken 119974 times.
✓ Branch 1 taken 290007 times.
✓ Branch 2 taken 46639 times.
✓ Branch 3 taken 24249 times.
✓ Branch 4 taken 46516 times.
✓ Branch 5 taken 123 times.
✓ Branch 6 taken 79 times.
✓ Branch 7 taken 46437 times.
529955 c == '-' || c == '_' || c == '/' || c == '.';
1844 }
1845
1846 /**
1847 * Read a key from a string.
1848 *
1849 * The key consists of is_key_char characters and must be terminated by a
1850 * character from the delim string; spaces are ignored.
1851 *
1852 * @return 0 for success (even with ellipsis), <0 for failure
1853 */
1854 46437 static int get_key(const char **ropts, const char *delim, char **rkey)
1855 {
1856 46437 const char *opts = *ropts;
1857 const char *key_start, *key_end;
1858
1859 46437 key_start = opts += strspn(opts, WHITESPACES);
1860
2/2
✓ Branch 1 taken 363544 times.
✓ Branch 2 taken 46437 times.
409981 while (is_key_char(*opts))
1861 363544 opts++;
1862 46437 key_end = opts;
1863 46437 opts += strspn(opts, WHITESPACES);
1864
4/4
✓ Branch 0 taken 34316 times.
✓ Branch 1 taken 12121 times.
✓ Branch 2 taken 6590 times.
✓ Branch 3 taken 27726 times.
46437 if (!*opts || !strchr(delim, *opts))
1865 18711 return AVERROR(EINVAL);
1866 27726 opts++;
1867
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27726 times.
27726 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1868 return AVERROR(ENOMEM);
1869 27726 memcpy(*rkey, key_start, key_end - key_start);
1870 27726 (*rkey)[key_end - key_start] = 0;
1871 27726 *ropts = opts;
1872 27726 return 0;
1873 }
1874
1875 46437 int av_opt_get_key_value(const char **ropts,
1876 const char *key_val_sep, const char *pairs_sep,
1877 unsigned flags,
1878 char **rkey, char **rval)
1879 {
1880 int ret;
1881 46437 char *key = NULL, *val;
1882 46437 const char *opts = *ropts;
1883
1884
2/2
✓ Branch 1 taken 18711 times.
✓ Branch 2 taken 27726 times.
46437 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1885
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18710 times.
18711 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1886 1 return AVERROR(EINVAL);
1887
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46436 times.
46436 if (!(val = av_get_token(&opts, pairs_sep))) {
1888 av_free(key);
1889 return AVERROR(ENOMEM);
1890 }
1891 46436 *ropts = opts;
1892 46436 *rkey = key;
1893 46436 *rval = val;
1894 46436 return 0;
1895 }
1896
1897 36 int av_opt_set_from_string(void *ctx, const char *opts,
1898 const char *const *shorthand,
1899 const char *key_val_sep, const char *pairs_sep)
1900 {
1901 36 int ret, count = 0;
1902 36 const char *dummy_shorthand = NULL;
1903 const char *key;
1904
1905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!opts)
1906 return 0;
1907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!shorthand)
1908 shorthand = &dummy_shorthand;
1909
1910
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 32 times.
81 while (*opts) {
1911 char *parsed_key, *value;
1912 49 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1913 49 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1914 &parsed_key, &value);
1915
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 48 times.
49 if (ret < 0) {
1916
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret == AVERROR(EINVAL))
1917 1 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1918 else
1919 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1920 av_err2str(ret));
1921 4 return ret;
1922 }
1923
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 33 times.
48 if (*opts)
1924 15 opts++;
1925
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 14 times.
48 if (parsed_key) {
1926 34 key = parsed_key;
1927
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 34 times.
61 while (*shorthand) /* discard all remaining shorthand */
1928 27 shorthand++;
1929 } else {
1930 14 key = *(shorthand++);
1931 }
1932
1933 48 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1934
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 45 times.
48 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1935
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (ret == AVERROR_OPTION_NOT_FOUND)
1936 2 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1937 3 av_free(value);
1938 3 av_free(parsed_key);
1939 3 return ret;
1940 }
1941
1942 45 av_free(value);
1943 45 av_free(parsed_key);
1944 45 count++;
1945 }
1946 32 return count;
1947 }
1948
1949 655851 void av_opt_free(void *obj)
1950 {
1951 655851 const AVOption *o = NULL;
1952
2/2
✓ Branch 1 taken 25497606 times.
✓ Branch 2 taken 655851 times.
26153457 while ((o = av_opt_next(obj, o))) {
1953 25497606 void *pitem = (uint8_t *)obj + o->offset;
1954
1955
2/2
✓ Branch 0 taken 98691 times.
✓ Branch 1 taken 25398915 times.
25497606 if (o->type & AV_OPT_TYPE_FLAG_ARRAY)
1956 98691 opt_free_array(o, pitem, opt_array_pcount(pitem));
1957 else
1958 25398915 opt_free_elem(o->type, pitem);
1959 }
1960 655851 }
1961
1962 271926 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1963 {
1964 271926 const AVDictionaryEntry *t = NULL;
1965 271926 AVDictionary *tmp = NULL;
1966 int ret;
1967
1968
2/2
✓ Branch 0 taken 66565 times.
✓ Branch 1 taken 205361 times.
271926 if (!options)
1969 66565 return 0;
1970
1971
2/2
✓ Branch 1 taken 161244 times.
✓ Branch 2 taken 205361 times.
366605 while ((t = av_dict_iterate(*options, t))) {
1972 161244 ret = av_opt_set(obj, t->key, t->value, search_flags);
1973
2/2
✓ Branch 0 taken 46379 times.
✓ Branch 1 taken 114865 times.
161244 if (ret == AVERROR_OPTION_NOT_FOUND)
1974 46379 ret = av_dict_set(&tmp, t->key, t->value, AV_DICT_MULTIKEY);
1975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161244 times.
161244 if (ret < 0) {
1976 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1977 av_dict_free(&tmp);
1978 return ret;
1979 }
1980 }
1981 205361 av_dict_free(options);
1982 205361 *options = tmp;
1983 205361 return 0;
1984 }
1985
1986 159074 int av_opt_set_dict(void *obj, AVDictionary **options)
1987 {
1988 159074 return av_opt_set_dict2(obj, options, 0);
1989 }
1990
1991 540902 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1992 int opt_flags, int search_flags)
1993 {
1994 540902 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1995 }
1996
1997 44727521 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1998 int opt_flags, int search_flags, void **target_obj)
1999 {
2000 const AVClass *c;
2001 44727521 const AVOption *o = NULL;
2002
2003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44727521 times.
44727521 if(!obj)
2004 return NULL;
2005
2006 44727521 c= *(AVClass**)obj;
2007
2008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44727521 times.
44727521 if (!c)
2009 return NULL;
2010
2011
2/2
✓ Branch 0 taken 44235112 times.
✓ Branch 1 taken 492409 times.
44727521 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
2012
2/2
✓ Branch 0 taken 43913830 times.
✓ Branch 1 taken 321282 times.
44235112 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
2013 43913830 void *iter = NULL;
2014 const AVClass *child;
2015
2/2
✓ Branch 1 taken 43631674 times.
✓ Branch 2 taken 43913312 times.
87544986 while (child = av_opt_child_class_iterate(c, &iter))
2016
2/2
✓ Branch 1 taken 518 times.
✓ Branch 2 taken 43631156 times.
43631674 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
2017 518 return o;
2018 } else {
2019 321282 void *child = NULL;
2020
2/2
✓ Branch 1 taken 159270 times.
✓ Branch 2 taken 260124 times.
419394 while (child = av_opt_child_next(obj, child))
2021
2/2
✓ Branch 1 taken 61158 times.
✓ Branch 2 taken 98112 times.
159270 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
2022 61158 return o;
2023 }
2024 }
2025
2026
2/2
✓ Branch 1 taken 463190025 times.
✓ Branch 2 taken 44065510 times.
507255535 while (o = av_opt_next(obj, o)) {
2027
6/6
✓ Branch 0 taken 689714 times.
✓ Branch 1 taken 462500311 times.
✓ Branch 2 taken 686946 times.
✓ Branch 3 taken 2768 times.
✓ Branch 4 taken 567168 times.
✓ Branch 5 taken 119778 times.
463190025 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
2028
4/4
✓ Branch 0 taken 83785 times.
✓ Branch 1 taken 483383 times.
✓ Branch 2 taken 119778 times.
✓ Branch 3 taken 83785 times.
686946 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
2029
4/6
✓ Branch 0 taken 119778 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119778 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 116952 times.
✓ Branch 5 taken 2826 times.
119778 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
2030
2/2
✓ Branch 0 taken 321003 times.
✓ Branch 1 taken 279332 times.
600335 if (target_obj) {
2031
1/2
✓ Branch 0 taken 321003 times.
✗ Branch 1 not taken.
321003 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
2032 321003 *target_obj = obj;
2033 else
2034 *target_obj = NULL;
2035 }
2036 600335 return o;
2037 }
2038 }
2039 44065510 return NULL;
2040 }
2041
2042 419417 void *av_opt_child_next(void *obj, void *prev)
2043 {
2044 419417 const AVClass *c = *(AVClass **)obj;
2045
2/2
✓ Branch 0 taken 307070 times.
✓ Branch 1 taken 112347 times.
419417 if (c->child_next)
2046 307070 return c->child_next(obj, prev);
2047 112347 return NULL;
2048 }
2049
2050 87544986 const AVClass *av_opt_child_class_iterate(const AVClass *parent, void **iter)
2051 {
2052
2/2
✓ Branch 0 taken 44071416 times.
✓ Branch 1 taken 43473570 times.
87544986 if (parent->child_class_iterate)
2053 44071416 return parent->child_class_iterate(iter);
2054 43473570 return NULL;
2055 }
2056
2057 #if FF_API_OPT_PTR
2058 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
2059 {
2060 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
2061
2062 // no direct access to array-type options
2063 if (!opt || (opt->type & AV_OPT_TYPE_FLAG_ARRAY))
2064 return NULL;
2065 return (uint8_t*)obj + opt->offset;
2066 }
2067 #endif
2068
2069 5595264 static int opt_copy_elem(void *logctx, enum AVOptionType type,
2070 void *dst, const void *src)
2071 {
2072
2/2
✓ Branch 0 taken 39943 times.
✓ Branch 1 taken 5555321 times.
5595264 if (type == AV_OPT_TYPE_STRING) {
2073 39943 const char *src_str = *(const char *const *)src;
2074 39943 char **dstp = (char **)dst;
2075
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39939 times.
39943 if (*dstp != src_str)
2076 4 av_freep(dstp);
2077
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39939 times.
39943 if (src_str) {
2078 4 *dstp = av_strdup(src_str);
2079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!*dstp)
2080 return AVERROR(ENOMEM);
2081 }
2082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5555321 times.
5555321 } else if (type == AV_OPT_TYPE_BINARY) {
2083 const uint8_t *const *src8 = (const uint8_t *const *)src;
2084 uint8_t **dst8 = (uint8_t **)dst;
2085 int len = *(const int *)(src8 + 1);
2086 if (*dst8 != *src8)
2087 av_freep(dst8);
2088 *dst8 = av_memdup(*src8, len);
2089 if (len && !*dst8) {
2090 *(int *)(dst8 + 1) = 0;
2091 return AVERROR(ENOMEM);
2092 }
2093 *(int *)(dst8 + 1) = len;
2094
2/2
✓ Branch 0 taken 1798457 times.
✓ Branch 1 taken 3756864 times.
5555321 } else if (type == AV_OPT_TYPE_CONST) {
2095 // do nothing
2096
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1798455 times.
1798457 } else if (type == AV_OPT_TYPE_DICT) {
2097 2 const AVDictionary *sdict = *(const AVDictionary * const *)src;
2098 2 AVDictionary **ddictp = (AVDictionary **)dst;
2099
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (sdict != *ddictp)
2100 2 av_dict_free(ddictp);
2101 2 *ddictp = NULL;
2102 2 return av_dict_copy(ddictp, sdict, 0);
2103
2/2
✓ Branch 0 taken 13052 times.
✓ Branch 1 taken 1785403 times.
1798455 } else if (type == AV_OPT_TYPE_CHLAYOUT) {
2104
1/2
✓ Branch 0 taken 13052 times.
✗ Branch 1 not taken.
13052 if (dst != src)
2105 13052 return av_channel_layout_copy(dst, src);
2106
1/2
✓ Branch 1 taken 1785403 times.
✗ Branch 2 not taken.
1785403 } else if (opt_is_pod(type)) {
2107 1785403 size_t size = opt_type_desc[type].size;
2108 1785403 memcpy(dst, src, size);
2109 } else {
2110 av_log(logctx, AV_LOG_ERROR, "Unhandled option type: %d\n", type);
2111 return AVERROR(EINVAL);
2112 }
2113
2114 5582210 return 0;
2115 }
2116
2117 13058 static int opt_copy_array(void *logctx, const AVOption *o,
2118 void **pdst, const void * const *psrc)
2119 {
2120 13058 unsigned nb_elems = *opt_array_pcount(psrc);
2121 13058 void *dst = NULL;
2122 int ret;
2123
2124
1/2
✓ Branch 0 taken 13058 times.
✗ Branch 1 not taken.
13058 if (*pdst == *psrc) {
2125 13058 *pdst = NULL;
2126 13058 *opt_array_pcount(pdst) = 0;
2127 }
2128
2129 13058 opt_free_array(o, pdst, opt_array_pcount(pdst));
2130
2131 13058 dst = av_calloc(nb_elems, opt_type_desc[TYPE_BASE(o->type)].size);
2132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13058 times.
13058 if (!dst)
2133 return AVERROR(ENOMEM);
2134
2135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13058 times.
13058 for (unsigned i = 0; i < nb_elems; i++) {
2136 ret = opt_copy_elem(logctx, TYPE_BASE(o->type),
2137 opt_array_pelem(o, dst, i),
2138 opt_array_pelem(o, *(void**)psrc, i));
2139 if (ret < 0) {
2140 opt_free_array(o, &dst, &nb_elems);
2141 return ret;
2142 }
2143 }
2144
2145 13058 *pdst = dst;
2146 13058 *opt_array_pcount(pdst) = nb_elems;
2147
2148 13058 return 0;
2149 }
2150
2151 36126 int av_opt_copy(void *dst, const void *src)
2152 {
2153 36126 const AVOption *o = NULL;
2154 const AVClass *c;
2155 36126 int ret = 0;
2156
2157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36126 times.
36126 if (!src)
2158 return AVERROR(EINVAL);
2159
2160 36126 c = *(AVClass **)src;
2161
2/4
✓ Branch 0 taken 36126 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36126 times.
36126 if (!c || c != *(AVClass **)dst)
2162 return AVERROR(EINVAL);
2163
2164
2/2
✓ Branch 1 taken 5608281 times.
✓ Branch 2 taken 36126 times.
5644407 while ((o = av_opt_next(src, o))) {
2165 5608281 void *field_dst = (uint8_t *)dst + o->offset;
2166 5608281 void *field_src = (uint8_t *)src + o->offset;
2167
2168 11216562 int err = (o->type & AV_OPT_TYPE_FLAG_ARRAY) ?
2169
2/2
✓ Branch 0 taken 13058 times.
✓ Branch 1 taken 5595223 times.
5608281 opt_copy_array(dst, o, field_dst, field_src) :
2170 5595223 opt_copy_elem (dst, o->type, field_dst, field_src);
2171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5608281 times.
5608281 if (err < 0)
2172 ret = err;
2173 }
2174 36126 return ret;
2175 }
2176
2177 18 int av_opt_get_array_size(void *obj, const char *name, int search_flags,
2178 unsigned int *out_val)
2179 {
2180 void *target_obj, *parray;
2181 const AVOption *o;
2182
2183 18 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
2184
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (!o || !target_obj)
2185 return AVERROR_OPTION_NOT_FOUND;
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!(o->type & AV_OPT_TYPE_FLAG_ARRAY))
2187 return AVERROR(EINVAL);
2188
2189 18 parray = (uint8_t *)target_obj + o->offset;
2190 18 *out_val = *opt_array_pcount(parray);
2191
2192 18 return 0;
2193 }
2194
2195 13 int av_opt_get_array(void *obj, const char *name, int search_flags,
2196 unsigned int start_elem, unsigned int nb_elems,
2197 enum AVOptionType out_type, void *out_val)
2198 {
2199 13 const size_t elem_size_out = opt_type_desc[TYPE_BASE(out_type)].size;
2200
2201 const AVOption *o;
2202 void *target_obj;
2203
2204 const void *parray;
2205 unsigned array_size;
2206
2207 int ret;
2208
2209 13 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
2210
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (!o || !target_obj)
2211 return AVERROR_OPTION_NOT_FOUND;
2212
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (!(o->type & AV_OPT_TYPE_FLAG_ARRAY) ||
2213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 (out_type & AV_OPT_TYPE_FLAG_ARRAY))
2214 return AVERROR(EINVAL);
2215
2216 13 parray = (uint8_t *)target_obj + o->offset;
2217 13 array_size = *opt_array_pcount(parray);
2218
2219
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (start_elem >= array_size ||
2220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 array_size - start_elem < nb_elems)
2221 return AVERROR(EINVAL);
2222
2223
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 13 times.
43 for (unsigned i = 0; i < nb_elems; i++) {
2224 30 const void *src = opt_array_pelem(o, *(void**)parray, start_elem + i);
2225 30 void *dst = (uint8_t*)out_val + i * elem_size_out;
2226
2227
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 7 times.
30 if (out_type == TYPE_BASE(o->type)) {
2228 23 ret = opt_copy_elem(obj, out_type, dst, src);
2229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
2230 goto fail;
2231
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 } else if (out_type == AV_OPT_TYPE_STRING) {
2232 5 uint8_t buf[128], *out = buf;
2233
2234 5 ret = opt_get_elem(o, &out, sizeof(buf), src, search_flags);
2235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
2236 goto fail;
2237
2238
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (out == buf) {
2239 5 out = av_strdup(buf);
2240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!out) {
2241 ret = AVERROR(ENOMEM);
2242 goto fail;
2243 }
2244 }
2245
2246 5 *(uint8_t**)dst = out;
2247
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 } else if (out_type == AV_OPT_TYPE_INT64 ||
2248 out_type == AV_OPT_TYPE_DOUBLE ||
2249 2 out_type == AV_OPT_TYPE_RATIONAL) {
2250 2 double num = 1.0;
2251 2 int den = 1;
2252 2 int64_t intnum = 1;
2253
2254 2 ret = read_number(o, src, &num, &den, &intnum);
2255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
2256 goto fail;
2257
2258
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 switch (out_type) {
2259 case AV_OPT_TYPE_INT64:
2260 *(int64_t*)dst = (num == den) ? intnum : num * intnum / den;
2261 2 break;
2262 2 case AV_OPT_TYPE_DOUBLE:
2263 2 *(double*)dst = num * intnum / den;
2264 2 break;
2265 case AV_OPT_TYPE_RATIONAL:
2266 *(AVRational*)dst = (num == 1.0 && (int)intnum == intnum) ?
2267 (AVRational){ intnum, den } :
2268 double_to_rational(num * intnum / den);
2269 break;
2270 default: av_assert0(0);
2271 }
2272 } else
2273 return AVERROR(ENOSYS);
2274 }
2275
2276 13 return 0;
2277 fail:
2278 for (unsigned i = 0; i < nb_elems; i++)
2279 opt_free_elem(out_type, (uint8_t*)out_val + i * elem_size_out);
2280 return ret;
2281 }
2282
2283 10 int av_opt_set_array(void *obj, const char *name, int search_flags,
2284 unsigned int start_elem, unsigned int nb_elems,
2285 enum AVOptionType val_type, const void *val)
2286 {
2287 10 const size_t elem_size_val = opt_type_desc[TYPE_BASE(val_type)].size;
2288
2289 const AVOption *o;
2290 const AVOptionArrayDef *arr;
2291 void *target_obj;
2292
2293 void *parray;
2294 void *new_elems;
2295 unsigned *array_size, new_size;
2296 size_t elem_size;
2297
2298 10 int ret = 0;
2299
2300 10 ret = opt_set_init(obj, name, search_flags, 0, &target_obj, &o, &parray);
2301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
2302 return ret;
2303
2304
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!(o->type & AV_OPT_TYPE_FLAG_ARRAY) ||
2305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 (val_type & AV_OPT_TYPE_FLAG_ARRAY))
2306 return AVERROR(EINVAL);
2307
2308 10 arr = o->default_val.arr;
2309 10 array_size = opt_array_pcount(parray);
2310 10 elem_size = opt_type_desc[TYPE_BASE(o->type)].size;
2311
2312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (start_elem > *array_size)
2313 return AVERROR(EINVAL);
2314
2315 // compute new array size
2316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!val) {
2317 if (*array_size - start_elem < nb_elems)
2318 return AVERROR(EINVAL);
2319
2320 new_size = *array_size - nb_elems;
2321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 } else if (search_flags & AV_OPT_ARRAY_REPLACE) {
2322 if (start_elem >= UINT_MAX - nb_elems)
2323 return AVERROR(EINVAL);
2324
2325 new_size = FFMAX(*array_size, start_elem + nb_elems);
2326 } else {
2327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (nb_elems >= UINT_MAX - *array_size)
2328 return AVERROR(EINVAL);
2329
2330 10 new_size = *array_size + nb_elems;
2331 }
2332
2333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (arr &&
2334 ((arr->size_max && new_size > arr->size_max) ||
2335 (arr->size_min && new_size < arr->size_min)))
2336 return AVERROR(EINVAL);
2337
2338 // desired operation is shrinking the array
2339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!val) {
2340 void *array = *(void**)parray;
2341
2342 for (unsigned i = 0; i < nb_elems; i++) {
2343 opt_free_elem(o->type,
2344 opt_array_pelem(o, array, start_elem + i));
2345 }
2346
2347 if (new_size > 0) {
2348 memmove(opt_array_pelem(o, array, start_elem),
2349 opt_array_pelem(o, array, start_elem + nb_elems),
2350 elem_size * (*array_size - start_elem - nb_elems));
2351
2352 array = av_realloc_array(array, new_size, elem_size);
2353 if (!array)
2354 return AVERROR(ENOMEM);
2355
2356 *(void**)parray = array;
2357 } else
2358 av_freep(parray);
2359
2360 *array_size = new_size;
2361
2362 return 0;
2363 }
2364
2365 // otherwise, desired operation is insert/replace;
2366 // first, store new elements in a separate array to simplify
2367 // rollback on failure
2368 10 new_elems = av_calloc(nb_elems, elem_size);
2369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!new_elems)
2370 return AVERROR(ENOMEM);
2371
2372 // convert/validate each new element
2373
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 for (unsigned i = 0; i < nb_elems; i++) {
2374 18 void *dst = opt_array_pelem(o, new_elems, i);
2375 18 const void *src = (uint8_t*)val + i * elem_size_val;
2376
2377 18 double num = 1.0;
2378 18 int den = 1;
2379 18 int64_t intnum = 1;
2380
2381
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (val_type == TYPE_BASE(o->type)) {
2382 int err;
2383
2384 18 ret = opt_copy_elem(obj, val_type, dst, src);
2385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
2386 goto fail;
2387
2388 // validate the range for numeric options
2389 18 err = read_number(o, dst, &num, &den, &intnum);
2390
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 if (err >= 0 && TYPE_BASE(o->type) != AV_OPT_TYPE_FLAGS &&
2391
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
18 (!den || o->max * den < num * intnum || o->min * den > num * intnum)) {
2392 num = den ? num * intnum / den : (num && intnum ? INFINITY : NAN);
2393 av_log(obj, AV_LOG_ERROR, "Cannot set array element %u for "
2394 "parameter '%s': value %f out of range [%g - %g]\n",
2395 start_elem + i, o->name, num, o->min, o->max);
2396 ret = AVERROR(ERANGE);
2397 goto fail;
2398 }
2399 } else if (val_type == AV_OPT_TYPE_STRING) {
2400 ret = opt_set_elem(obj, target_obj, o, *(const char **)src, dst);
2401 if (ret < 0)
2402 goto fail;
2403 } else if (val_type == AV_OPT_TYPE_INT ||
2404 val_type == AV_OPT_TYPE_INT64 ||
2405 val_type == AV_OPT_TYPE_FLOAT ||
2406 val_type == AV_OPT_TYPE_DOUBLE ||
2407 val_type == AV_OPT_TYPE_RATIONAL) {
2408
2409 switch (val_type) {
2410 case AV_OPT_TYPE_INT: intnum = *(int*)src; break;
2411 case AV_OPT_TYPE_INT64: intnum = *(int64_t*)src; break;
2412 case AV_OPT_TYPE_FLOAT: num = *(float*)src; break;
2413 case AV_OPT_TYPE_DOUBLE: num = *(double*)src; break;
2414 case AV_OPT_TYPE_RATIONAL: intnum = ((AVRational*)src)->num;
2415 den = ((AVRational*)src)->den; break;
2416 default: av_assert0(0);
2417 }
2418
2419 ret = write_number(obj, o, dst, num, den, intnum);
2420 if (ret < 0)
2421 goto fail;
2422 } else {
2423 ret = AVERROR(ENOSYS);
2424 goto fail;
2425 }
2426 }
2427
2428 // commit new elements to the array
2429
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if (start_elem == 0 && nb_elems == new_size) {
2430 // replacing the existing array entirely
2431 10 opt_free_array(o, parray, array_size);
2432 10 *(void**)parray = new_elems;
2433 10 *array_size = nb_elems;
2434
2435 10 new_elems = NULL;
2436 10 nb_elems = 0;
2437 } else {
2438 void *array = av_realloc_array(*(void**)parray, new_size, elem_size);
2439 if (!array) {
2440 ret = AVERROR(ENOMEM);
2441 goto fail;
2442 }
2443
2444 if (search_flags & AV_OPT_ARRAY_REPLACE) {
2445 // free the elements being overwritten
2446 for (unsigned i = start_elem; i < FFMIN(start_elem + nb_elems, *array_size); i++)
2447 opt_free_elem(o->type, opt_array_pelem(o, array, i));
2448 } else {
2449 // shift existing elements to the end
2450 memmove(opt_array_pelem(o, array, start_elem + nb_elems),
2451 opt_array_pelem(o, array, start_elem),
2452 elem_size * (*array_size - start_elem));
2453 }
2454
2455 memcpy((uint8_t*)array + elem_size * start_elem, new_elems, elem_size * nb_elems);
2456
2457 av_freep(&new_elems);
2458 nb_elems = 0;
2459
2460 *(void**)parray = array;
2461 *array_size = new_size;
2462 }
2463
2464 10 fail:
2465 10 opt_free_array(o, &new_elems, &nb_elems);
2466
2467 10 return ret;
2468 }
2469
2470 31 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
2471 {
2472 int ret;
2473 31 const AVClass *c = *(AVClass**)obj;
2474 31 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = c->query_ranges;
2475
2476
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (!callback)
2477 31 callback = av_opt_query_ranges_default;
2478
2479 31 ret = callback(ranges_arg, obj, key, flags);
2480
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 if (ret >= 0) {
2481
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
2482 18 ret = 1;
2483 18 (*ranges_arg)->nb_components = ret;
2484 }
2485 31 return ret;
2486 }
2487
2488 31 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
2489 {
2490 31 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
2491 31 AVOptionRange **range_array = av_mallocz(sizeof(void*));
2492 31 AVOptionRange *range = av_mallocz(sizeof(*range));
2493 31 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
2494 int ret;
2495
2496 31 *ranges_arg = NULL;
2497
2498
5/8
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 28 times.
31 if (!ranges || !range || !range_array || !field) {
2499 3 ret = AVERROR(ENOMEM);
2500 3 goto fail;
2501 }
2502
2503 28 ranges->range = range_array;
2504 28 ranges->range[0] = range;
2505 28 ranges->nb_ranges = 1;
2506 28 ranges->nb_components = 1;
2507 28 range->is_range = 1;
2508 28 range->value_min = field->min;
2509 28 range->value_max = field->max;
2510
2511
6/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 10 times.
28 switch (field->type) {
2512 13 case AV_OPT_TYPE_BOOL:
2513 case AV_OPT_TYPE_INT:
2514 case AV_OPT_TYPE_UINT:
2515 case AV_OPT_TYPE_INT64:
2516 case AV_OPT_TYPE_UINT64:
2517 case AV_OPT_TYPE_PIXEL_FMT:
2518 case AV_OPT_TYPE_SAMPLE_FMT:
2519 case AV_OPT_TYPE_FLOAT:
2520 case AV_OPT_TYPE_DOUBLE:
2521 case AV_OPT_TYPE_DURATION:
2522 case AV_OPT_TYPE_COLOR:
2523 13 break;
2524 2 case AV_OPT_TYPE_STRING:
2525 2 range->component_min = 0;
2526 2 range->component_max = 0x10FFFF; // max unicode value
2527 2 range->value_min = -1;
2528 2 range->value_max = INT_MAX;
2529 2 break;
2530 1 case AV_OPT_TYPE_RATIONAL:
2531 1 range->component_min = INT_MIN;
2532 1 range->component_max = INT_MAX;
2533 1 break;
2534 1 case AV_OPT_TYPE_IMAGE_SIZE:
2535 1 range->component_min = 0;
2536 1 range->component_max = INT_MAX/128/8;
2537 1 range->value_min = 0;
2538 1 range->value_max = INT_MAX/8;
2539 1 break;
2540 1 case AV_OPT_TYPE_VIDEO_RATE:
2541 1 range->component_min = 1;
2542 1 range->component_max = INT_MAX;
2543 1 range->value_min = 1;
2544 1 range->value_max = INT_MAX;
2545 1 break;
2546 10 default:
2547 10 ret = AVERROR(ENOSYS);
2548 10 goto fail;
2549 }
2550
2551 18 *ranges_arg = ranges;
2552 18 return 1;
2553 13 fail:
2554 13 av_free(ranges);
2555 13 av_free(range);
2556 13 av_free(range_array);
2557 13 return ret;
2558 }
2559
2560 18 void av_opt_freep_ranges(AVOptionRanges **rangesp)
2561 {
2562 int i;
2563 18 AVOptionRanges *ranges = *rangesp;
2564
2565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!ranges)
2566 return;
2567
2568
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
2569 18 AVOptionRange *range = ranges->range[i];
2570
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (range) {
2571 18 av_freep(&range->str);
2572 18 av_freep(&ranges->range[i]);
2573 }
2574 }
2575 18 av_freep(&ranges->range);
2576 18 av_freep(rangesp);
2577 }
2578
2579 140 int av_opt_is_set_to_default(void *obj, const AVOption *o)
2580 {
2581 int64_t i64;
2582 double d;
2583 AVRational q;
2584 int ret, w, h;
2585 char *str;
2586 void *dst;
2587
2588
2/4
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
140 if (!o || !obj)
2589 return AVERROR(EINVAL);
2590
2591 140 dst = ((uint8_t*)obj) + o->offset;
2592
2593
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 131 times.
140 if (o->type & AV_OPT_TYPE_FLAG_ARRAY) {
2594
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 const char *def = o->default_val.arr ? o->default_val.arr->def : NULL;
2595 uint8_t *val;
2596
2597 9 ret = opt_get_array(o, dst, &val);
2598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
2599 return ret;
2600
2601
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (!!val != !!def)
2602 2 ret = 0;
2603
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 else if (val)
2604 4 ret = !strcmp(val, def);
2605
2606 9 av_freep(&val);
2607
2608 9 return ret;
2609 }
2610
2611
11/13
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 17 times.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 8 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
131 switch (o->type) {
2612 case AV_OPT_TYPE_CONST:
2613 131 return 1;
2614 69 case AV_OPT_TYPE_BOOL:
2615 case AV_OPT_TYPE_FLAGS:
2616 case AV_OPT_TYPE_PIXEL_FMT:
2617 case AV_OPT_TYPE_SAMPLE_FMT:
2618 case AV_OPT_TYPE_INT:
2619 case AV_OPT_TYPE_UINT:
2620 case AV_OPT_TYPE_DURATION:
2621 case AV_OPT_TYPE_INT64:
2622 case AV_OPT_TYPE_UINT64:
2623 69 read_number(o, dst, NULL, NULL, &i64);
2624 69 return o->default_val.i64 == i64;
2625 7 case AV_OPT_TYPE_CHLAYOUT: {
2626 7 AVChannelLayout ch_layout = { 0 };
2627
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (o->default_val.str) {
2628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_channel_layout_from_string(&ch_layout, o->default_val.str)) < 0)
2629 return ret;
2630 }
2631 7 ret = !av_channel_layout_compare((AVChannelLayout *)dst, &ch_layout);
2632 7 av_channel_layout_uninit(&ch_layout);
2633 7 return ret;
2634 }
2635 6 case AV_OPT_TYPE_STRING:
2636 6 str = *(char **)dst;
2637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (str == o->default_val.str) //2 NULLs
2638 return 1;
2639
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
6 if (!str || !o->default_val.str) //1 NULL
2640 2 return 0;
2641 4 return !strcmp(str, o->default_val.str);
2642 3 case AV_OPT_TYPE_DOUBLE:
2643 3 d = *(double *)dst;
2644 3 return o->default_val.dbl == d;
2645 3 case AV_OPT_TYPE_FLOAT:
2646 3 d = *(float *)dst;
2647 3 return (float)o->default_val.dbl == d;
2648 17 case AV_OPT_TYPE_RATIONAL:
2649 17 q = av_d2q(o->default_val.dbl, INT_MAX);
2650 17 return !av_cmp_q(*(AVRational*)dst, q);
2651 9 case AV_OPT_TYPE_BINARY: {
2652 struct {
2653 uint8_t *data;
2654 int size;
2655 9 } tmp = {0};
2656 9 int opt_size = *(int *)((void **)dst + 1);
2657 9 void *opt_ptr = *(void **)dst;
2658
6/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
9 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
2659 6 return 1;
2660
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
3 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
2661 1 return 0;
2662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (opt_size != strlen(o->default_val.str) / 2)
2663 return 0;
2664 2 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
2665
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!ret)
2666 2 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
2667 2 av_free(tmp.data);
2668 2 return ret;
2669 }
2670 8 case AV_OPT_TYPE_DICT: {
2671 8 AVDictionary *dict1 = NULL;
2672 8 AVDictionary *dict2 = *(AVDictionary **)dst;
2673 8 const AVDictionaryEntry *en1 = NULL;
2674 8 const AVDictionaryEntry *en2 = NULL;
2675 8 ret = av_dict_parse_string(&dict1, o->default_val.str, "=", ":", 0);
2676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0) {
2677 av_dict_free(&dict1);
2678 return ret;
2679 }
2680 do {
2681 10 en1 = av_dict_iterate(dict1, en1);
2682 10 en2 = av_dict_iterate(dict2, en2);
2683
6/8
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
10 } while (en1 && en2 && !strcmp(en1->key, en2->key) && !strcmp(en1->value, en2->value));
2684 8 av_dict_free(&dict1);
2685
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
8 return (!en1 && !en2);
2686 }
2687 3 case AV_OPT_TYPE_IMAGE_SIZE:
2688
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
2689 w = h = 0;
2690
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
2691 return ret;
2692
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
3 return (w == *(int *)dst) && (h == *((int *)dst+1));
2693 3 case AV_OPT_TYPE_VIDEO_RATE:
2694 3 q = (AVRational){0, 0};
2695
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (o->default_val.str) {
2696
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
2697 return ret;
2698 }
2699 3 return !av_cmp_q(*(AVRational*)dst, q);
2700 3 case AV_OPT_TYPE_COLOR: {
2701 3 uint8_t color[4] = {0, 0, 0, 0};
2702
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (o->default_val.str) {
2703
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
2704 return ret;
2705 }
2706 3 return !memcmp(color, dst, sizeof(color));
2707 }
2708 default:
2709 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
2710 break;
2711 }
2712 return AVERROR_PATCHWELCOME;
2713 }
2714
2715 62 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
2716 {
2717 const AVOption *o;
2718 void *target;
2719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!obj)
2720 return AVERROR(EINVAL);
2721 62 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
2722
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 56 times.
62 if (!o)
2723 6 return AVERROR_OPTION_NOT_FOUND;
2724 56 return av_opt_is_set_to_default(target, o);
2725 }
2726
2727 20 static int opt_serialize(void *obj, int opt_flags, int flags, int *cnt,
2728 AVBPrint *bprint, const char key_val_sep, const char pairs_sep)
2729 {
2730 20 const AVOption *o = NULL;
2731 20 void *child = NULL;
2732 uint8_t *buf;
2733 int ret;
2734 20 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
2735
2736
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 if (flags & AV_OPT_SERIALIZE_SEARCH_CHILDREN)
2737
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 18 times.
23 while (child = av_opt_child_next(obj, child)) {
2738 5 ret = opt_serialize(child, opt_flags, flags, cnt, bprint,
2739 key_val_sep, pairs_sep);
2740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
2741 return ret;
2742 }
2743
2744
2/2
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 20 times.
233 while (o = av_opt_next(obj, o)) {
2745
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 140 times.
213 if (o->type == AV_OPT_TYPE_CONST)
2746 73 continue;
2747
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
140 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2748 continue;
2749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 else if (((o->flags & opt_flags) != opt_flags))
2750 continue;
2751
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 56 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 32 times.
140 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
2752 52 continue;
2753
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2754 av_bprint_finalize(bprint, NULL);
2755 return ret;
2756 }
2757
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 if (buf) {
2758
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 15 times.
88 if ((*cnt)++)
2759 73 av_bprint_append_data(bprint, &pairs_sep, 1);
2760 88 av_bprint_escape(bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2761 88 av_bprint_append_data(bprint, &key_val_sep, 1);
2762 88 av_bprint_escape(bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2763 88 av_freep(&buf);
2764 }
2765 }
2766
2767 20 return 0;
2768 }
2769
2770 15 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
2771 const char key_val_sep, const char pairs_sep)
2772 {
2773 AVBPrint bprint;
2774 15 int ret, cnt = 0;
2775
2776
4/8
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
15 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
2777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 pairs_sep == '\\' || key_val_sep == '\\') {
2778 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
2779 return AVERROR(EINVAL);
2780 }
2781
2782
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (!obj || !buffer)
2783 return AVERROR(EINVAL);
2784
2785 15 *buffer = NULL;
2786 15 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
2787
2788 15 ret = opt_serialize(obj, opt_flags, flags, &cnt, &bprint,
2789 key_val_sep, pairs_sep);
2790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
2791 return ret;
2792
2793 15 ret = av_bprint_finalize(&bprint, buffer);
2794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
2795 return ret;
2796 15 return 0;
2797 }
2798