FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/opt.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 163 175 93.1%
Functions: 3 5 60.0%
Branches: 52 74 70.3%

Line Branch Exec Source
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"
23 #include "libavutil/channel_layout.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
31 typedef struct TestContext {
32 const AVClass *class;
33 struct ChildContext *child;
34 int num;
35 int toggle;
36 char *string;
37 int flags;
38 AVRational rational;
39 AVRational video_rate;
40 int w, h;
41 enum AVPixelFormat pix_fmt;
42 enum AVSampleFormat sample_fmt;
43 int64_t duration;
44 uint8_t color[4];
45 AVChannelLayout channel_layout;
46 void *binary;
47 int binary_size;
48 void *binary1;
49 int binary_size1;
50 void *binary2;
51 int binary_size2;
52 int64_t num64;
53 float flt;
54 double dbl;
55 char *escape;
56 int bool1;
57 int bool2;
58 int bool3;
59 AVDictionary *dict1;
60 AVDictionary *dict2;
61
62 int **array_int;
63 unsigned nb_array_int;
64
65 char **array_str;
66 unsigned nb_array_str;
67
68 AVDictionary **array_dict;
69 unsigned nb_array_dict;
70 } TestContext;
71
72 #define OFFSET(x) offsetof(TestContext, x)
73
74 #define TEST_FLAG_COOL 01
75 #define TEST_FLAG_LAME 02
76 #define TEST_FLAG_MU 04
77
78 static const AVOptionArrayDef array_str = {
79 .sep = '|',
80 .def = "str0|str\\|1|str\\\\2",
81 };
82
83 static const AVOptionArrayDef array_dict = {
84 // 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
85 .def = "k00=v\\\\\\\\00:k01=v\\,01,k10=v\\\\=1\\\\:0",
86 };
87
88 static const AVOption test_options[]= {
89 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, 1 },
90 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
91 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
92 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
93 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 },
94 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, .unit = "flags" },
95 {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, .unit = "flags" },
96 {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, .unit = "flags" },
97 {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, .unit = "flags" },
98 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 },
99 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 },
100 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 },
101 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, 1 },
102 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 },
103 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 },
104 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHLAYOUT, { .str = "hexagonal" }, 0, 0, 1 },
105 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 },
106 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 },
107 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 },
108 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1 }, 0, 100, 1 },
109 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 },
110 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 },
111 {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 },
112 {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 },
113 {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 },
114 {"dict1", "set dictionary value", OFFSET(dict1), AV_OPT_TYPE_DICT, { .str = NULL}, 0, 0, 1 },
115 {"dict2", "set dictionary value", OFFSET(dict2), AV_OPT_TYPE_DICT, { .str = "happy=':-)'"}, 0, 0, 1 },
116 {"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 },
117 {"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 },
118 {"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 },
119 { NULL },
120 };
121
122 static const char *test_get_name(void *ctx)
123 {
124 return "test";
125 }
126
127 typedef struct ChildContext {
128 const AVClass *class;
129 int64_t child_num64;
130 int child_num;
131 } ChildContext;
132
133 #undef OFFSET
134 #define OFFSET(x) offsetof(ChildContext, x)
135
136 static const AVOption child_options[]= {
137 {"child_num64", "set num 64bit", OFFSET(child_num64), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, 100, 1 },
138 {"child_num", "set child_num", OFFSET(child_num), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 100, 1 },
139 { NULL },
140 };
141
142 static const char *child_get_name(void *ctx)
143 {
144 return "child";
145 }
146
147 static const AVClass child_class = {
148 .class_name = "ChildContext",
149 .item_name = child_get_name,
150 .option = child_options,
151 .version = LIBAVUTIL_VERSION_INT,
152 };
153
154 94 static void *test_child_next(void *obj, void *prev)
155 {
156 94 TestContext *test_ctx = obj;
157
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1 times.
94 if (!prev)
158 93 return test_ctx->child;
159 1 return NULL;
160 }
161
162 static const AVClass test_class = {
163 .class_name = "TestContext",
164 .item_name = test_get_name,
165 .option = test_options,
166 .child_next = test_child_next,
167 .version = LIBAVUTIL_VERSION_INT,
168 };
169
170 462 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
171 {
172 462 vfprintf(stdout, fmt, vl);
173 462 }
174
175 1 int main(void)
176 {
177 int i;
178
179 1 av_log_set_level(AV_LOG_DEBUG);
180 1 av_log_set_callback(log_callback_help);
181
182 1 printf("Testing default values\n");
183 {
184 1 TestContext test_ctx = { 0 };
185 1 test_ctx.class = &test_class;
186 1 av_opt_set_defaults(&test_ctx);
187
188 1 printf("num=%d\n", test_ctx.num);
189 1 printf("toggle=%d\n", test_ctx.toggle);
190 1 printf("string=%s\n", test_ctx.string);
191 1 printf("escape=%s\n", test_ctx.escape);
192 1 printf("flags=%d\n", test_ctx.flags);
193 1 printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
194 1 printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
195 1 printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
196 1 printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
197 1 printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
198 1 printf("duration=%"PRId64"\n", test_ctx.duration);
199 1 printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
200 1 printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout.u.mask, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
201
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (test_ctx.binary)
202 1 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]);
203 1 printf("binary_size=%d\n", test_ctx.binary_size);
204 1 printf("num64=%"PRId64"\n", test_ctx.num64);
205 1 printf("flt=%.6f\n", test_ctx.flt);
206 1 printf("dbl=%.6f\n", test_ctx.dbl);
207
208
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (unsigned i = 0; i < test_ctx.nb_array_str; i++)
209 3 printf("array_str[%u]=%s\n", i, test_ctx.array_str[i]);
210
211
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (unsigned i = 0; i < test_ctx.nb_array_dict; i++) {
212 2 AVDictionary *d = test_ctx.array_dict[i];
213 2 const AVDictionaryEntry *e = NULL;
214
215
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
5 while ((e = av_dict_iterate(d, e)))
216 3 printf("array_dict[%u]: %s\t%s\n", i, e->key, e->value);
217 }
218
219 1 av_opt_show2(&test_ctx, NULL, -1, 0);
220
221 1 av_opt_free(&test_ctx);
222 }
223
224 1 printf("\nTesting av_opt_is_set_to_default()\n");
225 {
226 int ret;
227 1 TestContext test_ctx = { 0 };
228 1 const AVOption *o = NULL;
229 1 test_ctx.class = &test_class;
230
231 1 av_log_set_level(AV_LOG_QUIET);
232
233
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 1 times.
31 while (o = av_opt_next(&test_ctx, o)) {
234 30 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
235
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
30 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
236 }
237 1 av_opt_set_defaults(&test_ctx);
238
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 1 times.
31 while (o = av_opt_next(&test_ctx, o)) {
239 30 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
240
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
30 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
241 }
242 1 av_opt_free(&test_ctx);
243 }
244
245 1 printf("\nTesting av_opt_get/av_opt_set()\n");
246 {
247 1 TestContext test_ctx = { 0 };
248 1 TestContext test2_ctx = { 0 };
249 1 const AVOption *o = NULL;
250 1 char *val = NULL;
251 int ret;
252
253 1 test_ctx.class = &test_class;
254 1 test2_ctx.class = &test_class;
255
256 1 av_log_set_level(AV_LOG_QUIET);
257
258 1 av_opt_set_defaults(&test_ctx);
259
260
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 1 times.
31 while (o = av_opt_next(&test_ctx, o)) {
261 30 char *value1 = NULL;
262 30 char *value2 = NULL;
263 30 int ret1 = AVERROR_BUG;
264 30 int ret2 = AVERROR_BUG;
265 30 int ret3 = AVERROR_BUG;
266
267
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
30 if (o->type == AV_OPT_TYPE_CONST)
268 3 continue;
269
270 27 ret1 = av_opt_get(&test_ctx, o->name, 0, (uint8_t **)&value1);
271
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (ret1 >= 0) {
272 27 ret2 = av_opt_set(&test2_ctx, o->name, value1, 0);
273
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (ret2 >= 0)
274 27 ret3 = av_opt_get(&test2_ctx, o->name, 0, (uint8_t **)&value2);
275 }
276
277
4/8
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 27 times.
54 printf("name: %-11s get: %-16s set: %-16s get: %-16s %s\n", o->name,
278 ret1 >= 0 ? value1 : av_err2str(ret1),
279 ret2 >= 0 ? "OK" : av_err2str(ret2),
280 ret3 >= 0 ? value2 : av_err2str(ret3),
281
3/6
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
27 ret1 >= 0 && ret2 >= 0 && ret3 >= 0 && !strcmp(value1, value2) ? "OK" : "Mismatch");
282 27 av_free(value1);
283 27 av_free(value2);
284 }
285
286 // av_opt_set(NULL) with an array option resets it
287 1 ret = av_opt_set(&test_ctx, "array_dict", NULL, 0);
288 1 printf("av_opt_set(\"array_dict\", NULL) -> %d\n", ret);
289 1 printf("array_dict=%sNULL; nb_array_dict=%u\n",
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 test_ctx.array_dict ? "non-" : "", test_ctx.nb_array_dict);
291
292 // av_opt_get() on an empty array should return a NULL string
293 1 ret = av_opt_get(&test_ctx, "array_dict", AV_OPT_ALLOW_NULL, (uint8_t**)&val);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("av_opt_get(\"array_dict\") -> %s\n", val ? val : "NULL");
295
296 1 av_opt_free(&test_ctx);
297 1 av_opt_free(&test2_ctx);
298 }
299
300 1 printf("\nTest av_opt_serialize()\n");
301 {
302 1 TestContext test_ctx = { 0 };
303 char *buf;
304 1 test_ctx.class = &test_class;
305
306 1 av_log_set_level(AV_LOG_QUIET);
307
308 1 av_opt_set_defaults(&test_ctx);
309
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
310 1 printf("%s\n", buf);
311 1 av_opt_free(&test_ctx);
312 1 memset(&test_ctx, 0, sizeof(test_ctx));
313 1 test_ctx.class = &test_class;
314 1 av_set_options_string(&test_ctx, buf, "=", ",");
315 1 av_free(buf);
316
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
317 1 ChildContext child_ctx = { 0 };
318 1 printf("%s\n", buf);
319 1 av_free(buf);
320 1 child_ctx.class = &child_class;
321 1 test_ctx.child = &child_ctx;
322
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_opt_serialize(&test_ctx, 0,
323 AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN,
324 &buf, '=', ',') >= 0) {
325 1 printf("%s\n", buf);
326 1 av_free(buf);
327 }
328 1 av_opt_free(&child_ctx);
329 1 test_ctx.child = NULL;
330 }
331 }
332 1 av_opt_free(&test_ctx);
333 }
334
335 1 printf("\nTesting av_set_options_string()\n");
336 {
337 1 TestContext test_ctx = { 0 };
338 static const char * const options[] = {
339 "",
340 ":",
341 "=",
342 "foo=:",
343 ":=foo",
344 "=foo",
345 "foo=",
346 "foo",
347 "foo=val",
348 "foo==val",
349 "toggle=:",
350 "string=:",
351 "toggle=1 : foo",
352 "toggle=100",
353 "toggle==1",
354 "flags=+mu-lame : num=42: toggle=0",
355 "num=42 : string=blahblah",
356 "rational=0 : rational=1/2 : rational=1/-1",
357 "rational=-1/0",
358 "size=1024x768",
359 "size=pal",
360 "size=bogus",
361 "pix_fmt=yuv420p",
362 "pix_fmt=2",
363 "pix_fmt=bogus",
364 "sample_fmt=s16",
365 "sample_fmt=2",
366 "sample_fmt=bogus",
367 "video_rate=pal",
368 "video_rate=25",
369 "video_rate=30000/1001",
370 "video_rate=30/1.001",
371 "video_rate=bogus",
372 "duration=bogus",
373 "duration=123.45",
374 "duration=1\\:23\\:45.67",
375 "color=blue",
376 "color=0x223300",
377 "color=0x42FF07AA",
378 "cl=FL+FR",
379 "cl=foo",
380 "bin=boguss",
381 "bin=111",
382 "bin=ffff",
383 "num64=bogus",
384 "num64=44",
385 "num64=44.4",
386 "num64=-1",
387 "num64=101",
388 "flt=bogus",
389 "flt=2",
390 "flt=2.2",
391 "flt=-1",
392 "flt=101",
393 "dbl=bogus",
394 "dbl=2",
395 "dbl=2.2",
396 "dbl=-1",
397 "dbl=101",
398 "bool1=true",
399 "bool2=auto",
400 "dict1='happy=\\:-):sad=\\:-('",
401 "array_int=0,32,2147483647",
402 "array_int=2147483648", // out of range, should fail
403 };
404
405 1 test_ctx.class = &test_class;
406 1 av_opt_set_defaults(&test_ctx);
407
408 1 av_log_set_level(AV_LOG_QUIET);
409
410
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1 times.
65 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
411 64 int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
412 64 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
413
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
64 if (silence_log)
414 1 av_log_set_callback(NULL);
415
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 31 times.
64 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
416 33 printf("Error '%s'\n", options[i]);
417 else
418 31 printf("OK '%s'\n", options[i]);
419 64 av_log_set_callback(log_callback_help);
420 }
421 1 av_opt_free(&test_ctx);
422 }
423
424 1 printf("\nTesting av_opt_set_from_string()\n");
425 {
426 1 TestContext test_ctx = { 0 };
427 static const char * const options[] = {
428 "",
429 "5",
430 "5:hello",
431 "5:hello:size=pal",
432 "5:size=pal:hello",
433 ":",
434 "=",
435 " 5 : hello : size = pal ",
436 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
437 };
438 static const char * const shorthand[] = { "num", "string", NULL };
439
440 1 test_ctx.class = &test_class;
441 1 av_opt_set_defaults(&test_ctx);
442
443 1 av_log_set_level(AV_LOG_QUIET);
444
445
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
446 9 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
447
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
9 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
448 4 printf("Error '%s'\n", options[i]);
449 else
450 5 printf("OK '%s'\n", options[i]);
451 }
452 1 av_opt_free(&test_ctx);
453 }
454
455 1 printf("\nTesting av_opt_find2()\n");
456 {
457 1 TestContext test_ctx = { 0 };
458 1 ChildContext child_ctx = { 0 };
459 void *target;
460 const AVOption *opt;
461
462 1 test_ctx.class = &test_class;
463 1 child_ctx.class = &child_class;
464 1 test_ctx.child = &child_ctx;
465
466 1 av_log_set_level(AV_LOG_QUIET);
467
468 // Should succeed. num exists and has opt_flags 1
469 1 opt = av_opt_find2(&test_ctx, "num", NULL, 1, 0, &target);
470
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (opt && target == &test_ctx)
471 1 printf("OK '%s'\n", opt->name);
472 else
473 printf("Error 'num'\n");
474
475 // Should fail. num64 exists but has opt_flags 1, not 2
476 1 opt = av_opt_find(&test_ctx, "num64", NULL, 2, 0);
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (opt)
478 printf("OK '%s'\n", opt->name);
479 else
480 1 printf("Error 'num64'\n");
481
482 // Should fail. child_num exists but in a child object we're not searching
483 1 opt = av_opt_find(&test_ctx, "child_num", NULL, 0, 0);
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (opt)
485 printf("OK '%s'\n", opt->name);
486 else
487 1 printf("Error 'child_num'\n");
488
489 // Should succeed. child_num exists in a child object we're searching
490 1 opt = av_opt_find2(&test_ctx, "child_num", NULL, 0, AV_OPT_SEARCH_CHILDREN, &target);
491
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (opt && target == &child_ctx)
492 1 printf("OK '%s'\n", opt->name);
493 else
494 printf("Error 'child_num'\n");
495
496 // Should fail. foo doesn't exist
497 1 opt = av_opt_find(&test_ctx, "foo", NULL, 0, 0);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (opt)
499 printf("OK '%s'\n", opt->name);
500 else
501 1 printf("Error 'foo'\n");
502 }
503
504 1 return 0;
505 }
506