FFmpeg
Loading...
Searching...
No Matches
opt.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <limits.h>
20#include <stdio.h>
21
22#include "libavutil/common.h"
24#include "libavutil/error.h"
25#include "libavutil/log.h"
26#include "libavutil/mem.h"
27#include "libavutil/rational.h"
28#include "libavutil/opt.h"
29#include "libavutil/pixdesc.h"
30
72
73#define OFFSET(x) offsetof(TestContext, x)
74
75#define TEST_FLAG_COOL 01
76#define TEST_FLAG_LAME 02
77#define TEST_FLAG_MU 04
78
80 .sep = '|',
81 .def = "str0|str\\|1|str\\\\2",
82};
83
85 // there are three levels of escaping - C string, array option, dict - so 8 backslashes are needed to get a literal one inside a dict key/val
86 .def = "k00=v\\\\\\\\00:k01=v\\,01,k10=v\\\\=1\\\\:0",
87};
88
89static const AVOption test_options[]= {
90 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 100, 1 },
91 {"unum", "set unum", OFFSET(unum), AV_OPT_TYPE_UINT, { .i64 = 1U << 31 }, 0, 1U << 31, 1 },
92 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
93 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
94 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
95 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 },
96 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, .unit = "flags" },
97 {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, .unit = "flags" },
98 {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, .unit = "flags" },
99 {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, .unit = "flags" },
100 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 },
101 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 },
102 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 },
103 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, 1 },
104 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 },
105 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 },
106 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHLAYOUT, { .str = "hexagonal" }, 0, 0, 1 },
107 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 },
108 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 },
109 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 },
110 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1LL << 32 }, -1, 1LL << 32, 1 },
111 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 },
112 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 },
113 {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 },
114 {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 },
115 {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 },
116 {"dict1", "set dictionary value", OFFSET(dict1), AV_OPT_TYPE_DICT, { .str = NULL}, 0, 0, 1 },
117 {"dict2", "set dictionary value", OFFSET(dict2), AV_OPT_TYPE_DICT, { .str = "happy=':-)'"}, 0, 0, 1 },
118 {"array_int", "array of ints", OFFSET(array_int), AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY, .max = INT_MAX, .flags = AV_OPT_FLAG_RUNTIME_PARAM },
119 {"array_str", "array of strings", OFFSET(array_str), AV_OPT_TYPE_STRING | AV_OPT_TYPE_FLAG_ARRAY, { .arr = &array_str }, .flags = AV_OPT_FLAG_RUNTIME_PARAM },
120 {"array_dict", "array of dicts", OFFSET(array_dict), AV_OPT_TYPE_DICT | AV_OPT_TYPE_FLAG_ARRAY, { .arr = &array_dict }, .flags = AV_OPT_FLAG_RUNTIME_PARAM },
121 { NULL },
122};
123
124static const char *test_get_name(void *ctx)
125{
126 return "test";
127}
128
129typedef struct ChildContext {
130 const AVClass *class;
133 unsigned int child_unum;
135
136#undef OFFSET
137#define OFFSET(x) offsetof(ChildContext, x)
138
139static const AVOption child_options[]= {
140 {"child_num64", "set num 64bit", OFFSET(child_num64), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, 100, 1 | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
141 {"child_num", "set child_num", OFFSET(child_num), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 100, 1 },
142 {"child_unum", "set child_unum", OFFSET(child_unum), AV_OPT_TYPE_UINT, { .i64 = 0 }, 0, 100, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
143 { NULL },
144};
145
146static const char *child_get_name(void *ctx)
147{
148 return "child";
149}
150
151static const AVClass child_class = {
152 .class_name = "ChildContext",
153 .item_name = child_get_name,
154 .option = child_options,
155 .version = LIBAVUTIL_VERSION_INT,
156};
157
158static void *test_child_next(void *obj, void *prev)
159{
160 TestContext *test_ctx = obj;
161 if (!prev)
162 return test_ctx->child;
163 return NULL;
164}
165
166static const AVClass test_class = {
167 .class_name = "TestContext",
168 .item_name = test_get_name,
169 .option = test_options,
170 .child_next = test_child_next,
171 .version = LIBAVUTIL_VERSION_INT,
172};
173
174static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
175{
176 vfprintf(stdout, fmt, vl);
177}
178
179int main(void)
180{
181 int i;
182
185
186 printf("Testing default values\n");
187 {
188 TestContext test_ctx = { 0 };
189 test_ctx.class = &test_class;
190 av_opt_set_defaults(&test_ctx);
191
192 printf("num=%d\n", test_ctx.num);
193 printf("unum=%u\n", test_ctx.unum);
194 printf("toggle=%d\n", test_ctx.toggle);
195 printf("string=%s\n", test_ctx.string);
196 printf("escape=%s\n", test_ctx.escape);
197 printf("flags=%d\n", test_ctx.flags);
198 printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
199 printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
200 printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
201 printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
202 printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
203 printf("duration=%"PRId64"\n", test_ctx.duration);
204 printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
205 printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout.u.mask, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
206 if (test_ctx.binary)
207 printf("binary=%x %x %x %x\n", ((uint8_t*)test_ctx.binary)[0], ((uint8_t*)test_ctx.binary)[1], ((uint8_t*)test_ctx.binary)[2], ((uint8_t*)test_ctx.binary)[3]);
208 printf("binary_size=%d\n", test_ctx.binary_size);
209 printf("num64=%"PRId64"\n", test_ctx.num64);
210 printf("flt=%.6f\n", test_ctx.flt);
211 printf("dbl=%.6f\n", test_ctx.dbl);
212
213 for (unsigned i = 0; i < test_ctx.nb_array_str; i++)
214 printf("array_str[%u]=%s\n", i, test_ctx.array_str[i]);
215
216 for (unsigned i = 0; i < test_ctx.nb_array_dict; i++) {
217 AVDictionary *d = test_ctx.array_dict[i];
218 const AVDictionaryEntry *e = NULL;
219
220 while ((e = av_dict_iterate(d, e)))
221 printf("array_dict[%u]: %s\t%s\n", i, e->key, e->value);
222 }
223
224 av_opt_show2(&test_ctx, NULL, -1, 0);
225
226 av_opt_free(&test_ctx);
227 }
228
229 printf("\nTesting av_opt_is_set_to_default()\n");
230 {
231 int ret;
232 TestContext test_ctx = { 0 };
233 const AVOption *o = NULL;
234 test_ctx.class = &test_class;
235
237
238 while (o = av_opt_next(&test_ctx, o)) {
239 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
240 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
241 }
242 av_opt_set_defaults(&test_ctx);
243 while (o = av_opt_next(&test_ctx, o)) {
244 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
245 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
246 }
247 av_opt_free(&test_ctx);
248 }
249
250 printf("\nTesting av_opt_get/av_opt_set()\n");
251 {
252 TestContext test_ctx = { 0 };
253 TestContext test2_ctx = { 0 };
254 const AVOption *o = NULL;
255 char *val = NULL;
256 int ret;
257
258 test_ctx.class = &test_class;
259 test2_ctx.class = &test_class;
260
262
263 av_opt_set_defaults(&test_ctx);
264
265 while (o = av_opt_next(&test_ctx, o)) {
266 char *value1 = NULL;
267 char *value2 = NULL;
268 int ret1 = AVERROR_BUG;
269 int ret2 = AVERROR_BUG;
270 int ret3 = AVERROR_BUG;
271
272 if (o->type == AV_OPT_TYPE_CONST)
273 continue;
274
275 ret1 = av_opt_get(&test_ctx, o->name, 0, (uint8_t **)&value1);
276 if (ret1 >= 0) {
277 ret2 = av_opt_set(&test2_ctx, o->name, value1, 0);
278 if (ret2 >= 0)
279 ret3 = av_opt_get(&test2_ctx, o->name, 0, (uint8_t **)&value2);
280 }
281
282 printf("name: %-11s get: %-16s set: %-16s get: %-16s %s\n", o->name,
283 ret1 >= 0 ? value1 : av_err2str(ret1),
284 ret2 >= 0 ? "OK" : av_err2str(ret2),
285 ret3 >= 0 ? value2 : av_err2str(ret3),
286 ret1 >= 0 && ret2 >= 0 && ret3 >= 0 && !strcmp(value1, value2) ? "OK" : "Mismatch");
287 av_free(value1);
288 av_free(value2);
289 }
290
291 // av_opt_set(NULL) with an array option resets it
292 ret = av_opt_set(&test_ctx, "array_dict", NULL, 0);
293 printf("av_opt_set(\"array_dict\", NULL) -> %d\n", ret);
294 printf("array_dict=%sNULL; nb_array_dict=%u\n",
295 test_ctx.array_dict ? "non-" : "", test_ctx.nb_array_dict);
296
297 // av_opt_get() on an empty array should return a NULL string
298 ret = av_opt_get(&test_ctx, "array_dict", AV_OPT_ALLOW_NULL, (uint8_t**)&val);
299 printf("av_opt_get(\"array_dict\") -> %s\n", val ? val : "NULL");
300
301 av_opt_free(&test_ctx);
302 av_opt_free(&test2_ctx);
303 }
304
305 printf("\nTesting av_opt_get_array()\n");
306 {
307 static const int int_array[] = { 5, 0, 42, 137, INT_MAX };
308
309 TestContext test_ctx = { 0 };
310
311 int out_int [FF_ARRAY_ELEMS(int_array)] = { 0 };
312 double out_double[FF_ARRAY_ELEMS(int_array)] = { 0. };
313 char *out_str [FF_ARRAY_ELEMS(int_array)] = { NULL };
314 AVDictionary *out_dict[2] = { NULL };
315
316 int ret;
317
318 test_ctx.class = &test_class;
319
321
322 av_opt_set_defaults(&test_ctx);
323
324 test_ctx.array_int = av_memdup(int_array, sizeof(int_array));
325 test_ctx.nb_array_int = FF_ARRAY_ELEMS(int_array);
326
327 // retrieve as int
328 ret = av_opt_get_array(&test_ctx, "array_int", 0,
329 1, 3, AV_OPT_TYPE_INT, out_int);
330 printf("av_opt_get_array(\"array_int\", 1, 3, INT)=%d -> [ %d, %d, %d ]\n",
331 ret, out_int[0], out_int[1], out_int[2]);
332
333 // retrieve as double
334 ret = av_opt_get_array(&test_ctx, "array_int", 0,
335 3, 2, AV_OPT_TYPE_DOUBLE, out_double);
336 printf("av_opt_get_array(\"array_int\", 3, 2, DOUBLE)=%d -> [ %.2f, %.2f ]\n",
337 ret, out_double[0], out_double[1]);
338
339 // retrieve as str
340 ret = av_opt_get_array(&test_ctx, "array_int", 0,
341 0, 5, AV_OPT_TYPE_STRING, out_str);
342 printf("av_opt_get_array(\"array_int\", 0, 5, STRING)=%d -> "
343 "[ %s, %s, %s, %s, %s ]\n", ret,
344 out_str[0], out_str[1], out_str[2], out_str[3], out_str[4]);
345
346 for (int i = 0; i < FF_ARRAY_ELEMS(out_str); i++)
347 av_freep(&out_str[i]);
348
349 ret = av_opt_get_array(&test_ctx, "array_dict", 0, 0, 2,
350 AV_OPT_TYPE_DICT, out_dict);
351 printf("av_opt_get_array(\"array_dict\", 0, 2, DICT)=%d\n", ret);
352
353 for (int i = 0; i < test_ctx.nb_array_dict; i++) {
354 const AVDictionaryEntry *e = NULL;
355 while ((e = av_dict_iterate(test_ctx.array_dict[i], e))) {
356 const AVDictionaryEntry *e1 = av_dict_get(out_dict[i], e->key, NULL, 0);
357 if (!e1 || strcmp(e->value, e1->value)) {
358 printf("mismatching dict entry %s: %s/%s\n",
359 e->key, e->value, e1 ? e1->value : "<missing>");
360 }
361 }
362 av_dict_free(&out_dict[i]);
363 }
364
365 av_opt_free(&test_ctx);
366 }
367
368 printf("\nTest av_opt_serialize()\n");
369 {
370 TestContext test_ctx = { 0 };
371 char *buf;
372 int ret;
373 test_ctx.class = &test_class;
374
376
377 av_opt_set_defaults(&test_ctx);
378 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
379 printf("%s\n", buf);
380 av_opt_free(&test_ctx);
381 memset(&test_ctx, 0, sizeof(test_ctx));
382 test_ctx.class = &test_class;
383 ret = av_set_options_string(&test_ctx, buf, "=", ",");
384 av_free(buf);
385 if (ret < 0)
386 printf("Error ret '%d'\n", ret);
387 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
388 ChildContext child_ctx = { .child_num64 = 1, .child_unum = 1 };
389 printf("%s\n", buf);
390 av_free(buf);
391 child_ctx.class = &child_class;
392 test_ctx.child = &child_ctx;
393 printf("Exclude flag EXPORT:\n");
397 &buf, '=', ',') >= 0) {
398 printf("%s\n", buf);
399 av_free(buf);
400 }
401 printf("Include flag EXPORT:\n");
404 &buf, '=', ',') >= 0) {
405 printf("%s\n", buf);
406 av_free(buf);
407 }
408 printf("Exclude flag 1:\n");
409 if (av_opt_serialize(&test_ctx, 1,
412 &buf, '=', ',') >= 0) {
413 printf("%s\n", buf);
414 av_free(buf);
415 }
416 printf("Include flag 1:\n");
417 if (av_opt_serialize(&test_ctx, 1,
419 &buf, '=', ',') >= 0) {
420 printf("%s\n", buf);
421 av_free(buf);
422 }
423 printf("Exclude exact match flag 1:\n");
424 if (av_opt_serialize(&test_ctx, 1,
427 &buf, '=', ',') >= 0) {
428 printf("%s\n", buf);
429 av_free(buf);
430 }
431 printf("Include exact match flag 1:\n");
432 if (av_opt_serialize(&test_ctx, 1,
435 &buf, '=', ',') >= 0) {
436 printf("%s\n", buf);
437 av_free(buf);
438 }
439 printf("Exclude exact match flag EXPORT|READONLY:\n");
443 &buf, '=', ',') >= 0) {
444 printf("%s\n", buf);
445 av_free(buf);
446 }
447 printf("Include exact match flag EXPORT|READONLY:\n");
451 &buf, '=', ',') >= 0) {
452 printf("%s\n", buf);
453 av_free(buf);
454 }
455 av_opt_free(&child_ctx);
456 test_ctx.child = NULL;
457 }
458 }
459 av_opt_free(&test_ctx);
460 }
461
462 printf("\nTesting av_set_options_string()\n");
463 {
464 TestContext test_ctx = { 0 };
465 static const char * const options[] = {
466 "",
467 ":",
468 "=",
469 "foo=:",
470 ":=foo",
471 "=foo",
472 "foo=",
473 "foo",
474 "foo=val",
475 "foo==val",
476 "toggle=:",
477 "string=:",
478 "toggle=1 : foo",
479 "toggle=100",
480 "toggle==1",
481 "flags=+mu-lame : num=42: toggle=0",
482 "flags=+1.5",
483 "flags=-1.5",
484 "flags=+100000000000000000000",
485 "flags=-100000000000000000000",
486 "num=42 : string=blahblah",
487 "rational=0 : rational=1/2 : rational=1/-1",
488 "rational=-1/0",
489 "size=1024x768",
490 "size=pal",
491 "size=bogus",
492 "pix_fmt=yuv420p",
493 "pix_fmt=2",
494 "pix_fmt=bogus",
495 "sample_fmt=s16",
496 "sample_fmt=2",
497 "sample_fmt=bogus",
498 "video_rate=pal",
499 "video_rate=25",
500 "video_rate=30000/1001",
501 "video_rate=30/1.001",
502 "video_rate=bogus",
503 "duration=bogus",
504 "duration=123.45",
505 "duration=1\\:23\\:45.67",
506 "color=blue",
507 "color=0x223300",
508 "color=0x42FF07AA",
509 "cl=FL+FR",
510 "cl=foo",
511 "bin=boguss",
512 "bin=111",
513 "bin=ffff",
514 "num=bogus",
515 "num=44",
516 "num=44.4",
517 "num=-1",
518 "num=-2",
519 "num=101",
520 "unum=bogus",
521 "unum=44",
522 "unum=44.4",
523 "unum=-1",
524 "unum=2147483648",
525 "unum=2147483649",
526 "num64=bogus",
527 "num64=44",
528 "num64=44.4",
529 "num64=-1",
530 "num64=-2",
531 "num64=4294967296",
532 "num64=4294967297",
533 "flt=bogus",
534 "flt=2",
535 "flt=2.2",
536 "flt=-1",
537 "flt=101",
538 "dbl=bogus",
539 "dbl=2",
540 "dbl=2.2",
541 "dbl=-1",
542 "dbl=101",
543 "bool1=true",
544 "bool2=auto",
545 "dict1='happy=\\:-):sad=\\:-('",
546 "array_int=0,32,2147483647",
547 "array_int=2147483648", // out of range, should fail
548 };
549
550 test_ctx.class = &test_class;
551 av_opt_set_defaults(&test_ctx);
552
554
555 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
556 // inf formatting differs between platforms, and so may %f of 1e20
557 int silence_log = !strcmp(options[i], "rational=-1/0") ||
558 strstr(options[i], "100000000000000000000");
559 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
560 if (silence_log)
562 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
563 printf("Error '%s'\n", options[i]);
564 else
565 printf("OK '%s'\n", options[i]);
567 }
568 // a rejected +x/-x flags update must leave the value untouched
569 printf("flags=%d\n", test_ctx.flags);
570 av_opt_free(&test_ctx);
571 }
572
573 printf("\nTesting av_opt_set_from_string()\n");
574 {
575 TestContext test_ctx = { 0 };
576 static const char * const options[] = {
577 "",
578 "5",
579 "5:hello",
580 "5:hello:size=pal",
581 "5:size=pal:hello",
582 ":",
583 "=",
584 " 5 : hello : size = pal ",
585 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
586 };
587 static const char * const shorthand[] = { "num", "string", NULL };
588
589 test_ctx.class = &test_class;
590 av_opt_set_defaults(&test_ctx);
591
593
594 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
595 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
596 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
597 printf("Error '%s'\n", options[i]);
598 else
599 printf("OK '%s'\n", options[i]);
600 }
601 av_opt_free(&test_ctx);
602 }
603
604 printf("\nTesting av_opt_find2()\n");
605 {
606 TestContext test_ctx = { 0 };
607 ChildContext child_ctx = { 0 };
608 void *target;
609 const AVOption *opt;
610
611 test_ctx.class = &test_class;
612 child_ctx.class = &child_class;
613 test_ctx.child = &child_ctx;
614
616
617 // Should succeed. num exists and has opt_flags 1
618 opt = av_opt_find2(&test_ctx, "num", NULL, 1, 0, &target);
619 if (opt && target == &test_ctx)
620 printf("OK '%s'\n", opt->name);
621 else
622 printf("Error 'num'\n");
623
624 // Should fail. num64 exists but has opt_flags 1, not 2
625 opt = av_opt_find(&test_ctx, "num64", NULL, 2, 0);
626 if (opt)
627 printf("OK '%s'\n", opt->name);
628 else
629 printf("Error 'num64'\n");
630
631 // Should fail. child_num exists but in a child object we're not searching
632 opt = av_opt_find(&test_ctx, "child_num", NULL, 0, 0);
633 if (opt)
634 printf("OK '%s'\n", opt->name);
635 else
636 printf("Error 'child_num'\n");
637
638 // Should succeed. child_num exists in a child object we're searching
639 opt = av_opt_find2(&test_ctx, "child_num", NULL, 0, AV_OPT_SEARCH_CHILDREN, &target);
640 if (opt && target == &child_ctx)
641 printf("OK '%s'\n", opt->name);
642 else
643 printf("Error 'child_num'\n");
644
645 // Should fail. foo doesn't exist
646 opt = av_opt_find(&test_ctx, "foo", NULL, 0, 0);
647 if (opt)
648 printf("OK '%s'\n", opt->name);
649 else
650 printf("Error 'foo'\n");
651 }
652
653 printf("\nTesting av_opt_copy() into a memdup'ed object\n");
654 {
655 TestContext test_ctx = { 0 };
657 uint8_t *layout_name = NULL;
658
659 test_ctx.class = &test_class;
660 av_opt_set_defaults(&test_ctx);
661 av_opt_set(&test_ctx, "cl", "FL@Left+FR@Right", 0);
662
663 copy = av_memdup(&test_ctx, sizeof(test_ctx));
664 if (!copy)
665 return 1;
666 av_opt_copy(copy, &test_ctx);
667 av_opt_free(&test_ctx);
668
669 av_opt_get(copy, "cl", 0, &layout_name);
670 printf("cl=%s\n", layout_name);
671 av_free(layout_name);
673 av_free(copy);
674 }
675
676 printf("\nTesting av_opt_copy() of a native layout into a custom layout\n");
677 {
678 TestContext src = { 0 }, dst = { 0 };
680
681 src.class = dst.class = &test_class;
684 av_opt_set(&dst, "cl", "FL@Left+FR@Right", 0);
685
686 native.u.mask = (uintptr_t)dst.channel_layout.u.map;
687 native.nb_channels = av_popcount64(native.u.mask);
688 av_opt_set_chlayout(&src, "cl", &native, 0);
689
690 printf("copy: %d\n", av_opt_copy(&dst, &src));
691 printf("same layout: %d\n", !av_channel_layout_compare(&src.channel_layout, &dst.channel_layout));
694 }
695
696 return 0;
697}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static double val(void *priv, double ch)
Definition aeval.c:77
static AVFormatContext * ctx
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Trivial log callback.
Definition cmdutils.c:68
common internal and external API header
#define av_popcount64
Definition common.h:157
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
static enum AVPixelFormat pix_fmt
error code definitions
static int64_t duration
Definition ffplay.c:359
int ret
Definition ffplay.c:391
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition opt.h:367
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
Definition opt.h:362
#define AV_OPT_FLAG_RUNTIME_PARAM
A generic parameter which can be set by the user at runtime.
Definition opt.h:376
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition opt.h:306
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
Definition opt.h:285
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_FLAG_ARRAY
May be combined with another regular option type to declare an array option.
Definition opt.h:345
@ AV_OPT_TYPE_SAMPLE_FMT
Underlying C type is enum AVSampleFormat.
Definition opt.h:310
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition opt.h:279
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition opt.h:330
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
@ AV_OPT_TYPE_UINT
Underlying C type is unsigned int.
Definition opt.h:334
#define AV_CH_LAYOUT_HEXAGONAL
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AV_LOG_QUIET
Print no output.
Definition log.h:192
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition log.c:497
void av_log_set_level(int level)
Set the log level.
Definition log.c:482
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition samplefmt.c:52
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int av_opt_get_array(void *obj, const char *name, int search_flags, unsigned int start_elem, unsigned int nb_elems, enum AVOptionType out_type, void *out_val)
For an array-type option, retrieve the values of one or more array elements.
Definition opt.c:2283
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition opt.c:1304
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
const AVOption * av_opt_find2(void *obj, const char *name, const char *unit, int opt_flags, int search_flags, void **target_obj)
Look for an option in an object.
Definition opt.c:2090
#define AV_OPT_ALLOW_NULL
In av_opt_get, return NULL if the option has a pointer type and is set to NULL, rather than returning...
Definition opt.h:618
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition opt.c:2084
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition opt.c:2042
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition opt.c:1771
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition opt.c:1759
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition opt.c:47
#define AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
Match only options with the exact AVOption::flags flags in opt_flags, for exclusion or inclusion in s...
Definition opt.h:1097
int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer, const char key_val_sep, const char pairs_sep)
Serialize object's options.
Definition opt.c:2870
#define AV_OPT_SERIALIZE_OPT_FLAGS_EXCLUDE
Serialize options with AVOption::flags flags not in opt_flags.
Definition opt.h:1103
#define AV_OPT_SERIALIZE_SKIP_DEFAULTS
Serialize options that are not set to default values only.
Definition opt.h:1092
int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
Check if given option is set to its default value.
Definition opt.c:2807
#define AV_OPT_SERIALIZE_SEARCH_CHILDREN
Serialize options in possible children of the given object.
Definition opt.h:1098
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition opt.c:904
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition opt.c:1086
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition opt.c:2239
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition opt.c:1990
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition opt.c:1910
static void binary(char *buf, uint32_t value, int bits)
Definition apv.c:210
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
static const char * obj
Definition mscl.c:57
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition pixfmt.h:264
Utilities for rational number calculation.
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
char * key
Definition dict.h:91
char * value
Definition dict.h:92
May be set as default_val for AV_OPT_TYPE_FLAG_ARRAY options.
Definition opt.h:394
AVOption.
Definition opt.h:428
const char * name
Definition opt.h:429
enum AVOptionType type
Definition opt.h:444
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int64_t child_num64
Definition opt.c:131
int child_num
Definition opt.c:132
unsigned int child_unum
Definition opt.c:133
const AVClass * class
Definition opt.c:130
AVDictionary * dict1
Definition opt.c:60
int h
Definition opt.c:41
int flags
Definition opt.c:38
struct ChildContext * child
Definition opt.c:33
unsigned nb_array_str
Definition opt.c:67
int num
Definition opt.c:34
int unum
Definition opt.c:35
AVRational rational
Definition opt.c:39
AVDictionary * dict2
Definition opt.c:61
int binary_size2
Definition opt.c:52
void * binary1
Definition opt.c:49
unsigned nb_array_dict
Definition opt.c:70
AVRational video_rate
Definition opt.c:40
enum AVPixelFormat pix_fmt
Definition opt.c:42
const AVClass * class
Definition opt.c:32
int64_t duration
Definition opt.c:44
uint8_t color[4]
Definition opt.c:45
int binary_size
Definition opt.c:48
int ** array_int
Definition opt.c:63
unsigned nb_array_int
Definition opt.c:64
void * binary
Definition opt.c:47
void * binary2
Definition opt.c:51
int binary_size1
Definition opt.c:50
char ** array_str
Definition opt.c:66
double dbl
Definition opt.c:55
int64_t num64
Definition opt.c:53
enum AVSampleFormat sample_fmt
Definition opt.c:43
int w
Definition opt.c:41
int bool3
Definition opt.c:59
int toggle
Definition opt.c:36
int bool2
Definition opt.c:58
int bool1
Definition opt.c:57
AVChannelLayout channel_layout
Definition opt.c:46
char * string
Definition opt.c:37
AVDictionary ** array_dict
Definition opt.c:69
char * escape
Definition opt.c:56
float flt
Definition opt.c:54
uint8_t level
Definition svq3.c:208
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
int num64
Definition utils.c:186
#define src
Definition vp8dsp.c:248
int num
Definition error.c:23
static const AVClass child_class
Definition opt.c:151
static void * test_child_next(void *obj, void *prev)
Definition opt.c:158
static const AVOption test_options[]
Definition opt.c:89
static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Definition opt.c:174
static const AVClass test_class
Definition opt.c:166
static const char * child_get_name(void *ctx)
Definition opt.c:146
int main(void)
Definition opt.c:179
#define TEST_FLAG_LAME
Definition opt.c:76
static const AVOptionArrayDef array_dict
Definition opt.c:84
static const AVOption child_options[]
Definition opt.c:139
static const char * test_get_name(void *ctx)
Definition opt.c:124
#define TEST_FLAG_MU
Definition opt.c:77
#define OFFSET(x)
Definition opt.c:73
static const AVOptionArrayDef array_str
Definition opt.c:79
#define TEST_FLAG_COOL
Definition opt.c:75
static void copy(const float *p1, float *p2, const int length)