FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/opt.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 193 208 92.8%
Functions: 3 5 60.0%
Branches: 61 88 69.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 unum;
36 int toggle;
37 char *string;
38 int flags;
39 AVRational rational;
40 AVRational video_rate;
41 int w, h;
42 enum AVPixelFormat pix_fmt;
43 enum AVSampleFormat sample_fmt;
44 int64_t duration;
45 uint8_t color[4];
46 AVChannelLayout channel_layout;
47 void *binary;
48 int binary_size;
49 void *binary1;
50 int binary_size1;
51 void *binary2;
52 int binary_size2;
53 int64_t num64;
54 float flt;
55 double dbl;
56 char *escape;
57 int bool1;
58 int bool2;
59 int bool3;
60 AVDictionary *dict1;
61 AVDictionary *dict2;
62
63 int **array_int;
64 unsigned nb_array_int;
65
66 char **array_str;
67 unsigned nb_array_str;
68
69 AVDictionary **array_dict;
70 unsigned nb_array_dict;
71 } TestContext;
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
79 static const AVOptionArrayDef array_str = {
80 .sep = '|',
81 .def = "str0|str\\|1|str\\\\2",
82 };
83
84 static const AVOptionArrayDef array_dict = {
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
89 static 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
124 static const char *test_get_name(void *ctx)
125 {
126 return "test";
127 }
128
129 typedef struct ChildContext {
130 const AVClass *class;
131 int64_t child_num64;
132 int child_num;
133 } ChildContext;
134
135 #undef OFFSET
136 #define OFFSET(x) offsetof(ChildContext, x)
137
138 static const AVOption child_options[]= {
139 {"child_num64", "set num 64bit", OFFSET(child_num64), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, 100, 1 },
140 {"child_num", "set child_num", OFFSET(child_num), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 100, 1 },
141 { NULL },
142 };
143
144 static const char *child_get_name(void *ctx)
145 {
146 return "child";
147 }
148
149 static const AVClass child_class = {
150 .class_name = "ChildContext",
151 .item_name = child_get_name,
152 .option = child_options,
153 .version = LIBAVUTIL_VERSION_INT,
154 };
155
156 109 static void *test_child_next(void *obj, void *prev)
157 {
158 109 TestContext *test_ctx = obj;
159
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1 times.
109 if (!prev)
160 108 return test_ctx->child;
161 1 return NULL;
162 }
163
164 static const AVClass test_class = {
165 .class_name = "TestContext",
166 .item_name = test_get_name,
167 .option = test_options,
168 .child_next = test_child_next,
169 .version = LIBAVUTIL_VERSION_INT,
170 };
171
172 512 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
173 {
174 512 vfprintf(stdout, fmt, vl);
175 512 }
176
177 1 int main(void)
178 {
179 int i;
180
181 1 av_log_set_level(AV_LOG_DEBUG);
182 1 av_log_set_callback(log_callback_help);
183
184 1 printf("Testing default values\n");
185 {
186 1 TestContext test_ctx = { 0 };
187 1 test_ctx.class = &test_class;
188 1 av_opt_set_defaults(&test_ctx);
189
190 1 printf("num=%d\n", test_ctx.num);
191 1 printf("unum=%u\n", test_ctx.unum);
192 1 printf("toggle=%d\n", test_ctx.toggle);
193 1 printf("string=%s\n", test_ctx.string);
194 1 printf("escape=%s\n", test_ctx.escape);
195 1 printf("flags=%d\n", test_ctx.flags);
196 1 printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
197 1 printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
198 1 printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
199 1 printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
200 1 printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
201 1 printf("duration=%"PRId64"\n", test_ctx.duration);
202 1 printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
203 1 printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout.u.mask, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
204
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (test_ctx.binary)
205 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]);
206 1 printf("binary_size=%d\n", test_ctx.binary_size);
207 1 printf("num64=%"PRId64"\n", test_ctx.num64);
208 1 printf("flt=%.6f\n", test_ctx.flt);
209 1 printf("dbl=%.6f\n", test_ctx.dbl);
210
211
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (unsigned i = 0; i < test_ctx.nb_array_str; i++)
212 3 printf("array_str[%u]=%s\n", i, test_ctx.array_str[i]);
213
214
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (unsigned i = 0; i < test_ctx.nb_array_dict; i++) {
215 2 AVDictionary *d = test_ctx.array_dict[i];
216 2 const AVDictionaryEntry *e = NULL;
217
218
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
5 while ((e = av_dict_iterate(d, e)))
219 3 printf("array_dict[%u]: %s\t%s\n", i, e->key, e->value);
220 }
221
222 1 av_opt_show2(&test_ctx, NULL, -1, 0);
223
224 1 av_opt_free(&test_ctx);
225 }
226
227 1 printf("\nTesting av_opt_is_set_to_default()\n");
228 {
229 int ret;
230 1 TestContext test_ctx = { 0 };
231 1 const AVOption *o = NULL;
232 1 test_ctx.class = &test_class;
233
234 1 av_log_set_level(AV_LOG_QUIET);
235
236
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
32 while (o = av_opt_next(&test_ctx, o)) {
237 31 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
238
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
31 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
239 }
240 1 av_opt_set_defaults(&test_ctx);
241
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
32 while (o = av_opt_next(&test_ctx, o)) {
242 31 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
243
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
31 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
244 }
245 1 av_opt_free(&test_ctx);
246 }
247
248 1 printf("\nTesting av_opt_get/av_opt_set()\n");
249 {
250 1 TestContext test_ctx = { 0 };
251 1 TestContext test2_ctx = { 0 };
252 1 const AVOption *o = NULL;
253 1 char *val = NULL;
254 int ret;
255
256 1 test_ctx.class = &test_class;
257 1 test2_ctx.class = &test_class;
258
259 1 av_log_set_level(AV_LOG_QUIET);
260
261 1 av_opt_set_defaults(&test_ctx);
262
263
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
32 while (o = av_opt_next(&test_ctx, o)) {
264 31 char *value1 = NULL;
265 31 char *value2 = NULL;
266 31 int ret1 = AVERROR_BUG;
267 31 int ret2 = AVERROR_BUG;
268 31 int ret3 = AVERROR_BUG;
269
270
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
31 if (o->type == AV_OPT_TYPE_CONST)
271 3 continue;
272
273 28 ret1 = av_opt_get(&test_ctx, o->name, 0, (uint8_t **)&value1);
274
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (ret1 >= 0) {
275 28 ret2 = av_opt_set(&test2_ctx, o->name, value1, 0);
276
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (ret2 >= 0)
277 28 ret3 = av_opt_get(&test2_ctx, o->name, 0, (uint8_t **)&value2);
278 }
279
280
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
56 printf("name: %-11s get: %-16s set: %-16s get: %-16s %s\n", o->name,
281 ret1 >= 0 ? value1 : av_err2str(ret1),
282 ret2 >= 0 ? "OK" : av_err2str(ret2),
283 ret3 >= 0 ? value2 : av_err2str(ret3),
284
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 ret1 >= 0 && ret2 >= 0 && ret3 >= 0 && !strcmp(value1, value2) ? "OK" : "Mismatch");
285 28 av_free(value1);
286 28 av_free(value2);
287 }
288
289 // av_opt_set(NULL) with an array option resets it
290 1 ret = av_opt_set(&test_ctx, "array_dict", NULL, 0);
291 1 printf("av_opt_set(\"array_dict\", NULL) -> %d\n", ret);
292 1 printf("array_dict=%sNULL; nb_array_dict=%u\n",
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 test_ctx.array_dict ? "non-" : "", test_ctx.nb_array_dict);
294
295 // av_opt_get() on an empty array should return a NULL string
296 1 ret = av_opt_get(&test_ctx, "array_dict", AV_OPT_ALLOW_NULL, (uint8_t**)&val);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("av_opt_get(\"array_dict\") -> %s\n", val ? val : "NULL");
298
299 1 av_opt_free(&test_ctx);
300 1 av_opt_free(&test2_ctx);
301 }
302
303 1 printf("\nTesting av_opt_get_array()\n");
304 {
305 static const int int_array[] = { 5, 0, 42, 137, INT_MAX };
306
307 1 TestContext test_ctx = { 0 };
308
309 1 int out_int [FF_ARRAY_ELEMS(int_array)] = { 0 };
310 1 double out_double[FF_ARRAY_ELEMS(int_array)] = { 0. };
311 1 char *out_str [FF_ARRAY_ELEMS(int_array)] = { NULL };
312 1 AVDictionary *out_dict[2] = { NULL };
313
314 int ret;
315
316 1 test_ctx.class = &test_class;
317
318 1 av_log_set_level(AV_LOG_QUIET);
319
320 1 av_opt_set_defaults(&test_ctx);
321
322 1 test_ctx.array_int = av_memdup(int_array, sizeof(int_array));
323 1 test_ctx.nb_array_int = FF_ARRAY_ELEMS(int_array);
324
325 // retrieve as int
326 1 ret = av_opt_get_array(&test_ctx, "array_int", 0,
327 1, 3, AV_OPT_TYPE_INT, out_int);
328 1 printf("av_opt_get_array(\"array_int\", 1, 3, INT)=%d -> [ %d, %d, %d ]\n",
329 ret, out_int[0], out_int[1], out_int[2]);
330
331 // retrieve as double
332 1 ret = av_opt_get_array(&test_ctx, "array_int", 0,
333 3, 2, AV_OPT_TYPE_DOUBLE, out_double);
334 1 printf("av_opt_get_array(\"array_int\", 3, 2, DOUBLE)=%d -> [ %.2f, %.2f ]\n",
335 ret, out_double[0], out_double[1]);
336
337 // retrieve as str
338 1 ret = av_opt_get_array(&test_ctx, "array_int", 0,
339 0, 5, AV_OPT_TYPE_STRING, out_str);
340 1 printf("av_opt_get_array(\"array_int\", 0, 5, STRING)=%d -> "
341 "[ %s, %s, %s, %s, %s ]\n", ret,
342 out_str[0], out_str[1], out_str[2], out_str[3], out_str[4]);
343
344
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (int i = 0; i < FF_ARRAY_ELEMS(out_str); i++)
345 5 av_freep(&out_str[i]);
346
347 1 ret = av_opt_get_array(&test_ctx, "array_dict", 0, 0, 2,
348 AV_OPT_TYPE_DICT, out_dict);
349 1 printf("av_opt_get_array(\"array_dict\", 0, 2, DICT)=%d\n", ret);
350
351
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < test_ctx.nb_array_dict; i++) {
352 2 const AVDictionaryEntry *e = NULL;
353
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
5 while ((e = av_dict_iterate(test_ctx.array_dict[i], e))) {
354 3 const AVDictionaryEntry *e1 = av_dict_get(out_dict[i], e->key, NULL, 0);
355
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (!e1 || strcmp(e->value, e1->value)) {
356 printf("mismatching dict entry %s: %s/%s\n",
357 e->key, e->value, e1 ? e1->value : "<missing>");
358 }
359 }
360 2 av_dict_free(&out_dict[i]);
361 }
362
363 1 av_opt_free(&test_ctx);
364 }
365
366 1 printf("\nTest av_opt_serialize()\n");
367 {
368 1 TestContext test_ctx = { 0 };
369 char *buf;
370 int ret;
371 1 test_ctx.class = &test_class;
372
373 1 av_log_set_level(AV_LOG_QUIET);
374
375 1 av_opt_set_defaults(&test_ctx);
376
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
377 1 printf("%s\n", buf);
378 1 av_opt_free(&test_ctx);
379 1 memset(&test_ctx, 0, sizeof(test_ctx));
380 1 test_ctx.class = &test_class;
381 1 ret = av_set_options_string(&test_ctx, buf, "=", ",");
382 1 av_free(buf);
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
384 printf("Error ret '%d'\n", ret);
385
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
386 1 ChildContext child_ctx = { 0 };
387 1 printf("%s\n", buf);
388 1 av_free(buf);
389 1 child_ctx.class = &child_class;
390 1 test_ctx.child = &child_ctx;
391
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_opt_serialize(&test_ctx, 0,
392 AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN,
393 &buf, '=', ',') >= 0) {
394 1 printf("%s\n", buf);
395 1 av_free(buf);
396 }
397 1 av_opt_free(&child_ctx);
398 1 test_ctx.child = NULL;
399 }
400 }
401 1 av_opt_free(&test_ctx);
402 }
403
404 1 printf("\nTesting av_set_options_string()\n");
405 {
406 1 TestContext test_ctx = { 0 };
407 static const char * const options[] = {
408 "",
409 ":",
410 "=",
411 "foo=:",
412 ":=foo",
413 "=foo",
414 "foo=",
415 "foo",
416 "foo=val",
417 "foo==val",
418 "toggle=:",
419 "string=:",
420 "toggle=1 : foo",
421 "toggle=100",
422 "toggle==1",
423 "flags=+mu-lame : num=42: toggle=0",
424 "num=42 : string=blahblah",
425 "rational=0 : rational=1/2 : rational=1/-1",
426 "rational=-1/0",
427 "size=1024x768",
428 "size=pal",
429 "size=bogus",
430 "pix_fmt=yuv420p",
431 "pix_fmt=2",
432 "pix_fmt=bogus",
433 "sample_fmt=s16",
434 "sample_fmt=2",
435 "sample_fmt=bogus",
436 "video_rate=pal",
437 "video_rate=25",
438 "video_rate=30000/1001",
439 "video_rate=30/1.001",
440 "video_rate=bogus",
441 "duration=bogus",
442 "duration=123.45",
443 "duration=1\\:23\\:45.67",
444 "color=blue",
445 "color=0x223300",
446 "color=0x42FF07AA",
447 "cl=FL+FR",
448 "cl=foo",
449 "bin=boguss",
450 "bin=111",
451 "bin=ffff",
452 "num=bogus",
453 "num=44",
454 "num=44.4",
455 "num=-1",
456 "num=-2",
457 "num=101",
458 "unum=bogus",
459 "unum=44",
460 "unum=44.4",
461 "unum=-1",
462 "unum=2147483648",
463 "unum=2147483649",
464 "num64=bogus",
465 "num64=44",
466 "num64=44.4",
467 "num64=-1",
468 "num64=-2",
469 "num64=4294967296",
470 "num64=4294967297",
471 "flt=bogus",
472 "flt=2",
473 "flt=2.2",
474 "flt=-1",
475 "flt=101",
476 "dbl=bogus",
477 "dbl=2",
478 "dbl=2.2",
479 "dbl=-1",
480 "dbl=101",
481 "bool1=true",
482 "bool2=auto",
483 "dict1='happy=\\:-):sad=\\:-('",
484 "array_int=0,32,2147483647",
485 "array_int=2147483648", // out of range, should fail
486 };
487
488 1 test_ctx.class = &test_class;
489 1 av_opt_set_defaults(&test_ctx);
490
491 1 av_log_set_level(AV_LOG_QUIET);
492
493
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1 times.
79 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
494 78 int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
495 78 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
496
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77 times.
78 if (silence_log)
497 1 av_log_set_callback(NULL);
498
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39 times.
78 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
499 39 printf("Error '%s'\n", options[i]);
500 else
501 39 printf("OK '%s'\n", options[i]);
502 78 av_log_set_callback(log_callback_help);
503 }
504 1 av_opt_free(&test_ctx);
505 }
506
507 1 printf("\nTesting av_opt_set_from_string()\n");
508 {
509 1 TestContext test_ctx = { 0 };
510 static const char * const options[] = {
511 "",
512 "5",
513 "5:hello",
514 "5:hello:size=pal",
515 "5:size=pal:hello",
516 ":",
517 "=",
518 " 5 : hello : size = pal ",
519 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
520 };
521 static const char * const shorthand[] = { "num", "string", NULL };
522
523 1 test_ctx.class = &test_class;
524 1 av_opt_set_defaults(&test_ctx);
525
526 1 av_log_set_level(AV_LOG_QUIET);
527
528
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
529 9 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
530
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)
531 4 printf("Error '%s'\n", options[i]);
532 else
533 5 printf("OK '%s'\n", options[i]);
534 }
535 1 av_opt_free(&test_ctx);
536 }
537
538 1 printf("\nTesting av_opt_find2()\n");
539 {
540 1 TestContext test_ctx = { 0 };
541 1 ChildContext child_ctx = { 0 };
542 void *target;
543 const AVOption *opt;
544
545 1 test_ctx.class = &test_class;
546 1 child_ctx.class = &child_class;
547 1 test_ctx.child = &child_ctx;
548
549 1 av_log_set_level(AV_LOG_QUIET);
550
551 // Should succeed. num exists and has opt_flags 1
552 1 opt = av_opt_find2(&test_ctx, "num", NULL, 1, 0, &target);
553
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)
554 1 printf("OK '%s'\n", opt->name);
555 else
556 printf("Error 'num'\n");
557
558 // Should fail. num64 exists but has opt_flags 1, not 2
559 1 opt = av_opt_find(&test_ctx, "num64", NULL, 2, 0);
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (opt)
561 printf("OK '%s'\n", opt->name);
562 else
563 1 printf("Error 'num64'\n");
564
565 // Should fail. child_num exists but in a child object we're not searching
566 1 opt = av_opt_find(&test_ctx, "child_num", NULL, 0, 0);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (opt)
568 printf("OK '%s'\n", opt->name);
569 else
570 1 printf("Error 'child_num'\n");
571
572 // Should succeed. child_num exists in a child object we're searching
573 1 opt = av_opt_find2(&test_ctx, "child_num", NULL, 0, AV_OPT_SEARCH_CHILDREN, &target);
574
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)
575 1 printf("OK '%s'\n", opt->name);
576 else
577 printf("Error 'child_num'\n");
578
579 // Should fail. foo doesn't exist
580 1 opt = av_opt_find(&test_ctx, "foo", NULL, 0, 0);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (opt)
582 printf("OK '%s'\n", opt->name);
583 else
584 1 printf("Error 'foo'\n");
585 }
586
587 1 return 0;
588 }
589