| 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 | unsigned int child_unum; | ||
| 134 | } ChildContext; | ||
| 135 | |||
| 136 | #undef OFFSET | ||
| 137 | #define OFFSET(x) offsetof(ChildContext, x) | ||
| 138 | |||
| 139 | static 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 | |||
| 146 | ✗ | static const char *child_get_name(void *ctx) | |
| 147 | { | ||
| 148 | ✗ | return "child"; | |
| 149 | } | ||
| 150 | |||
| 151 | static 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 | |||
| 158 | 123 | static void *test_child_next(void *obj, void *prev) | |
| 159 | { | ||
| 160 | 123 | TestContext *test_ctx = obj; | |
| 161 |
2/2✓ Branch 0 taken 115 times.
✓ Branch 1 taken 8 times.
|
123 | if (!prev) |
| 162 | 115 | return test_ctx->child; | |
| 163 | 8 | return NULL; | |
| 164 | } | ||
| 165 | |||
| 166 | static 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 | |||
| 174 | 512 | static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl) | |
| 175 | { | ||
| 176 | 512 | vfprintf(stdout, fmt, vl); | |
| 177 | 512 | } | |
| 178 | |||
| 179 | 1 | int main(void) | |
| 180 | { | ||
| 181 | int i; | ||
| 182 | |||
| 183 | 1 | av_log_set_level(AV_LOG_DEBUG); | |
| 184 | 1 | av_log_set_callback(log_callback_help); | |
| 185 | |||
| 186 | 1 | printf("Testing default values\n"); | |
| 187 | { | ||
| 188 | 1 | TestContext test_ctx = { 0 }; | |
| 189 | 1 | test_ctx.class = &test_class; | |
| 190 | 1 | av_opt_set_defaults(&test_ctx); | |
| 191 | |||
| 192 | 1 | printf("num=%d\n", test_ctx.num); | |
| 193 | 1 | printf("unum=%u\n", test_ctx.unum); | |
| 194 | 1 | printf("toggle=%d\n", test_ctx.toggle); | |
| 195 | 1 | printf("string=%s\n", test_ctx.string); | |
| 196 | 1 | printf("escape=%s\n", test_ctx.escape); | |
| 197 | 1 | printf("flags=%d\n", test_ctx.flags); | |
| 198 | 1 | printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den); | |
| 199 | 1 | printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den); | |
| 200 | 1 | printf("width=%d height=%d\n", test_ctx.w, test_ctx.h); | |
| 201 | 1 | printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt)); | |
| 202 | 1 | printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt)); | |
| 203 | 1 | printf("duration=%"PRId64"\n", test_ctx.duration); | |
| 204 | 1 | printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]); | |
| 205 | 1 | printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout.u.mask, (int64_t)AV_CH_LAYOUT_HEXAGONAL); | |
| 206 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (test_ctx.binary) |
| 207 | 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]); | |
| 208 | 1 | printf("binary_size=%d\n", test_ctx.binary_size); | |
| 209 | 1 | printf("num64=%"PRId64"\n", test_ctx.num64); | |
| 210 | 1 | printf("flt=%.6f\n", test_ctx.flt); | |
| 211 | 1 | printf("dbl=%.6f\n", test_ctx.dbl); | |
| 212 | |||
| 213 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (unsigned i = 0; i < test_ctx.nb_array_str; i++) |
| 214 | 3 | printf("array_str[%u]=%s\n", i, test_ctx.array_str[i]); | |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (unsigned i = 0; i < test_ctx.nb_array_dict; i++) { |
| 217 | 2 | AVDictionary *d = test_ctx.array_dict[i]; | |
| 218 | 2 | const AVDictionaryEntry *e = NULL; | |
| 219 | |||
| 220 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
|
5 | while ((e = av_dict_iterate(d, e))) |
| 221 | 3 | printf("array_dict[%u]: %s\t%s\n", i, e->key, e->value); | |
| 222 | } | ||
| 223 | |||
| 224 | 1 | av_opt_show2(&test_ctx, NULL, -1, 0); | |
| 225 | |||
| 226 | 1 | av_opt_free(&test_ctx); | |
| 227 | } | ||
| 228 | |||
| 229 | 1 | printf("\nTesting av_opt_is_set_to_default()\n"); | |
| 230 | { | ||
| 231 | int ret; | ||
| 232 | 1 | TestContext test_ctx = { 0 }; | |
| 233 | 1 | const AVOption *o = NULL; | |
| 234 | 1 | test_ctx.class = &test_class; | |
| 235 | |||
| 236 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 237 | |||
| 238 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
|
32 | while (o = av_opt_next(&test_ctx, o)) { |
| 239 | 31 | 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 28 times.
|
31 | printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : ""); |
| 241 | } | ||
| 242 | 1 | av_opt_set_defaults(&test_ctx); | |
| 243 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
|
32 | while (o = av_opt_next(&test_ctx, o)) { |
| 244 | 31 | ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0); | |
| 245 |
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) : ""); |
| 246 | } | ||
| 247 | 1 | av_opt_free(&test_ctx); | |
| 248 | } | ||
| 249 | |||
| 250 | 1 | printf("\nTesting av_opt_get/av_opt_set()\n"); | |
| 251 | { | ||
| 252 | 1 | TestContext test_ctx = { 0 }; | |
| 253 | 1 | TestContext test2_ctx = { 0 }; | |
| 254 | 1 | const AVOption *o = NULL; | |
| 255 | 1 | char *val = NULL; | |
| 256 | int ret; | ||
| 257 | |||
| 258 | 1 | test_ctx.class = &test_class; | |
| 259 | 1 | test2_ctx.class = &test_class; | |
| 260 | |||
| 261 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 262 | |||
| 263 | 1 | av_opt_set_defaults(&test_ctx); | |
| 264 | |||
| 265 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
|
32 | while (o = av_opt_next(&test_ctx, o)) { |
| 266 | 31 | char *value1 = NULL; | |
| 267 | 31 | char *value2 = NULL; | |
| 268 | 31 | int ret1 = AVERROR_BUG; | |
| 269 | 31 | int ret2 = AVERROR_BUG; | |
| 270 | 31 | int ret3 = AVERROR_BUG; | |
| 271 | |||
| 272 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
|
31 | if (o->type == AV_OPT_TYPE_CONST) |
| 273 | 3 | continue; | |
| 274 | |||
| 275 | 28 | ret1 = av_opt_get(&test_ctx, o->name, 0, (uint8_t **)&value1); | |
| 276 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (ret1 >= 0) { |
| 277 | 28 | ret2 = av_opt_set(&test2_ctx, o->name, value1, 0); | |
| 278 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (ret2 >= 0) |
| 279 | 28 | ret3 = av_opt_get(&test2_ctx, o->name, 0, (uint8_t **)&value2); | |
| 280 | } | ||
| 281 | |||
| 282 |
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, |
| 283 | ✗ | ret1 >= 0 ? value1 : av_err2str(ret1), | |
| 284 | ✗ | ret2 >= 0 ? "OK" : av_err2str(ret2), | |
| 285 | ✗ | ret3 >= 0 ? value2 : av_err2str(ret3), | |
| 286 |
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"); |
| 287 | 28 | av_free(value1); | |
| 288 | 28 | av_free(value2); | |
| 289 | } | ||
| 290 | |||
| 291 | // av_opt_set(NULL) with an array option resets it | ||
| 292 | 1 | ret = av_opt_set(&test_ctx, "array_dict", NULL, 0); | |
| 293 | 1 | printf("av_opt_set(\"array_dict\", NULL) -> %d\n", ret); | |
| 294 | 1 | printf("array_dict=%sNULL; nb_array_dict=%u\n", | |
| 295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | 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 | 1 | ret = av_opt_get(&test_ctx, "array_dict", AV_OPT_ALLOW_NULL, (uint8_t**)&val); | |
| 299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | printf("av_opt_get(\"array_dict\") -> %s\n", val ? val : "NULL"); |
| 300 | |||
| 301 | 1 | av_opt_free(&test_ctx); | |
| 302 | 1 | av_opt_free(&test2_ctx); | |
| 303 | } | ||
| 304 | |||
| 305 | 1 | printf("\nTesting av_opt_get_array()\n"); | |
| 306 | { | ||
| 307 | static const int int_array[] = { 5, 0, 42, 137, INT_MAX }; | ||
| 308 | |||
| 309 | 1 | TestContext test_ctx = { 0 }; | |
| 310 | |||
| 311 | 1 | int out_int [FF_ARRAY_ELEMS(int_array)] = { 0 }; | |
| 312 | 1 | double out_double[FF_ARRAY_ELEMS(int_array)] = { 0. }; | |
| 313 | 1 | char *out_str [FF_ARRAY_ELEMS(int_array)] = { NULL }; | |
| 314 | 1 | AVDictionary *out_dict[2] = { NULL }; | |
| 315 | |||
| 316 | int ret; | ||
| 317 | |||
| 318 | 1 | test_ctx.class = &test_class; | |
| 319 | |||
| 320 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 321 | |||
| 322 | 1 | av_opt_set_defaults(&test_ctx); | |
| 323 | |||
| 324 | 1 | test_ctx.array_int = av_memdup(int_array, sizeof(int_array)); | |
| 325 | 1 | test_ctx.nb_array_int = FF_ARRAY_ELEMS(int_array); | |
| 326 | |||
| 327 | // retrieve as int | ||
| 328 | 1 | ret = av_opt_get_array(&test_ctx, "array_int", 0, | |
| 329 | 1, 3, AV_OPT_TYPE_INT, out_int); | ||
| 330 | 1 | 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 | 1 | ret = av_opt_get_array(&test_ctx, "array_int", 0, | |
| 335 | 3, 2, AV_OPT_TYPE_DOUBLE, out_double); | ||
| 336 | 1 | 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 | 1 | ret = av_opt_get_array(&test_ctx, "array_int", 0, | |
| 341 | 0, 5, AV_OPT_TYPE_STRING, out_str); | ||
| 342 | 1 | 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 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (int i = 0; i < FF_ARRAY_ELEMS(out_str); i++) |
| 347 | 5 | av_freep(&out_str[i]); | |
| 348 | |||
| 349 | 1 | ret = av_opt_get_array(&test_ctx, "array_dict", 0, 0, 2, | |
| 350 | AV_OPT_TYPE_DICT, out_dict); | ||
| 351 | 1 | printf("av_opt_get_array(\"array_dict\", 0, 2, DICT)=%d\n", ret); | |
| 352 | |||
| 353 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (int i = 0; i < test_ctx.nb_array_dict; i++) { |
| 354 | 2 | const AVDictionaryEntry *e = NULL; | |
| 355 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
|
5 | while ((e = av_dict_iterate(test_ctx.array_dict[i], e))) { |
| 356 | 3 | const AVDictionaryEntry *e1 = av_dict_get(out_dict[i], e->key, NULL, 0); | |
| 357 |
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)) { |
| 358 | ✗ | printf("mismatching dict entry %s: %s/%s\n", | |
| 359 | ✗ | e->key, e->value, e1 ? e1->value : "<missing>"); | |
| 360 | } | ||
| 361 | } | ||
| 362 | 2 | av_dict_free(&out_dict[i]); | |
| 363 | } | ||
| 364 | |||
| 365 | 1 | av_opt_free(&test_ctx); | |
| 366 | } | ||
| 367 | |||
| 368 | 1 | printf("\nTest av_opt_serialize()\n"); | |
| 369 | { | ||
| 370 | 1 | TestContext test_ctx = { 0 }; | |
| 371 | char *buf; | ||
| 372 | int ret; | ||
| 373 | 1 | test_ctx.class = &test_class; | |
| 374 | |||
| 375 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 376 | |||
| 377 | 1 | av_opt_set_defaults(&test_ctx); | |
| 378 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) { |
| 379 | 1 | printf("%s\n", buf); | |
| 380 | 1 | av_opt_free(&test_ctx); | |
| 381 | 1 | memset(&test_ctx, 0, sizeof(test_ctx)); | |
| 382 | 1 | test_ctx.class = &test_class; | |
| 383 | 1 | ret = av_set_options_string(&test_ctx, buf, "=", ","); | |
| 384 | 1 | av_free(buf); | |
| 385 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 386 | ✗ | printf("Error ret '%d'\n", ret); | |
| 387 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) { |
| 388 | 1 | ChildContext child_ctx = { .child_num64 = 1, .child_unum = 1 }; | |
| 389 | 1 | printf("%s\n", buf); | |
| 390 | 1 | av_free(buf); | |
| 391 | 1 | child_ctx.class = &child_class; | |
| 392 | 1 | test_ctx.child = &child_ctx; | |
| 393 | 1 | printf("Exclude flag EXPORT:\n"); | |
| 394 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, AV_OPT_FLAG_EXPORT, |
| 395 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN| | ||
| 396 | AV_OPT_SERIALIZE_OPT_FLAGS_EXCLUDE, | ||
| 397 | &buf, '=', ',') >= 0) { | ||
| 398 | 1 | printf("%s\n", buf); | |
| 399 | 1 | av_free(buf); | |
| 400 | } | ||
| 401 | 1 | printf("Include flag EXPORT:\n"); | |
| 402 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, AV_OPT_FLAG_EXPORT, |
| 403 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN, | ||
| 404 | &buf, '=', ',') >= 0) { | ||
| 405 | 1 | printf("%s\n", buf); | |
| 406 | 1 | av_free(buf); | |
| 407 | } | ||
| 408 | 1 | printf("Exclude flag 1:\n"); | |
| 409 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, 1, |
| 410 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN| | ||
| 411 | AV_OPT_SERIALIZE_OPT_FLAGS_EXCLUDE, | ||
| 412 | &buf, '=', ',') >= 0) { | ||
| 413 | 1 | printf("%s\n", buf); | |
| 414 | 1 | av_free(buf); | |
| 415 | } | ||
| 416 | 1 | printf("Include flag 1:\n"); | |
| 417 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, 1, |
| 418 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN, | ||
| 419 | &buf, '=', ',') >= 0) { | ||
| 420 | 1 | printf("%s\n", buf); | |
| 421 | 1 | av_free(buf); | |
| 422 | } | ||
| 423 | 1 | printf("Exclude exact match flag 1:\n"); | |
| 424 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, 1, |
| 425 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN| | ||
| 426 | AV_OPT_SERIALIZE_OPT_FLAGS_EXACT|AV_OPT_SERIALIZE_OPT_FLAGS_EXCLUDE, | ||
| 427 | &buf, '=', ',') >= 0) { | ||
| 428 | 1 | printf("%s\n", buf); | |
| 429 | 1 | av_free(buf); | |
| 430 | } | ||
| 431 | 1 | printf("Include exact match flag 1:\n"); | |
| 432 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, 1, |
| 433 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN| | ||
| 434 | AV_OPT_SERIALIZE_OPT_FLAGS_EXACT, | ||
| 435 | &buf, '=', ',') >= 0) { | ||
| 436 | 1 | printf("%s\n", buf); | |
| 437 | 1 | av_free(buf); | |
| 438 | } | ||
| 439 | 1 | printf("Exclude exact match flag EXPORT|READONLY:\n"); | |
| 440 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY, |
| 441 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN| | ||
| 442 | AV_OPT_SERIALIZE_OPT_FLAGS_EXACT|AV_OPT_SERIALIZE_OPT_FLAGS_EXCLUDE, | ||
| 443 | &buf, '=', ',') >= 0) { | ||
| 444 | 1 | printf("%s\n", buf); | |
| 445 | 1 | av_free(buf); | |
| 446 | } | ||
| 447 | 1 | printf("Include exact match flag EXPORT|READONLY:\n"); | |
| 448 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (av_opt_serialize(&test_ctx, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY, |
| 449 | AV_OPT_SERIALIZE_SKIP_DEFAULTS|AV_OPT_SERIALIZE_SEARCH_CHILDREN| | ||
| 450 | AV_OPT_SERIALIZE_OPT_FLAGS_EXACT, | ||
| 451 | &buf, '=', ',') >= 0) { | ||
| 452 | 1 | printf("%s\n", buf); | |
| 453 | 1 | av_free(buf); | |
| 454 | } | ||
| 455 | 1 | av_opt_free(&child_ctx); | |
| 456 | 1 | test_ctx.child = NULL; | |
| 457 | } | ||
| 458 | } | ||
| 459 | 1 | av_opt_free(&test_ctx); | |
| 460 | } | ||
| 461 | |||
| 462 | 1 | printf("\nTesting av_set_options_string()\n"); | |
| 463 | { | ||
| 464 | 1 | 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 | "num=42 : string=blahblah", | ||
| 483 | "rational=0 : rational=1/2 : rational=1/-1", | ||
| 484 | "rational=-1/0", | ||
| 485 | "size=1024x768", | ||
| 486 | "size=pal", | ||
| 487 | "size=bogus", | ||
| 488 | "pix_fmt=yuv420p", | ||
| 489 | "pix_fmt=2", | ||
| 490 | "pix_fmt=bogus", | ||
| 491 | "sample_fmt=s16", | ||
| 492 | "sample_fmt=2", | ||
| 493 | "sample_fmt=bogus", | ||
| 494 | "video_rate=pal", | ||
| 495 | "video_rate=25", | ||
| 496 | "video_rate=30000/1001", | ||
| 497 | "video_rate=30/1.001", | ||
| 498 | "video_rate=bogus", | ||
| 499 | "duration=bogus", | ||
| 500 | "duration=123.45", | ||
| 501 | "duration=1\\:23\\:45.67", | ||
| 502 | "color=blue", | ||
| 503 | "color=0x223300", | ||
| 504 | "color=0x42FF07AA", | ||
| 505 | "cl=FL+FR", | ||
| 506 | "cl=foo", | ||
| 507 | "bin=boguss", | ||
| 508 | "bin=111", | ||
| 509 | "bin=ffff", | ||
| 510 | "num=bogus", | ||
| 511 | "num=44", | ||
| 512 | "num=44.4", | ||
| 513 | "num=-1", | ||
| 514 | "num=-2", | ||
| 515 | "num=101", | ||
| 516 | "unum=bogus", | ||
| 517 | "unum=44", | ||
| 518 | "unum=44.4", | ||
| 519 | "unum=-1", | ||
| 520 | "unum=2147483648", | ||
| 521 | "unum=2147483649", | ||
| 522 | "num64=bogus", | ||
| 523 | "num64=44", | ||
| 524 | "num64=44.4", | ||
| 525 | "num64=-1", | ||
| 526 | "num64=-2", | ||
| 527 | "num64=4294967296", | ||
| 528 | "num64=4294967297", | ||
| 529 | "flt=bogus", | ||
| 530 | "flt=2", | ||
| 531 | "flt=2.2", | ||
| 532 | "flt=-1", | ||
| 533 | "flt=101", | ||
| 534 | "dbl=bogus", | ||
| 535 | "dbl=2", | ||
| 536 | "dbl=2.2", | ||
| 537 | "dbl=-1", | ||
| 538 | "dbl=101", | ||
| 539 | "bool1=true", | ||
| 540 | "bool2=auto", | ||
| 541 | "dict1='happy=\\:-):sad=\\:-('", | ||
| 542 | "array_int=0,32,2147483647", | ||
| 543 | "array_int=2147483648", // out of range, should fail | ||
| 544 | }; | ||
| 545 | |||
| 546 | 1 | test_ctx.class = &test_class; | |
| 547 | 1 | av_opt_set_defaults(&test_ctx); | |
| 548 | |||
| 549 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 550 | |||
| 551 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1 times.
|
79 | for (i=0; i < FF_ARRAY_ELEMS(options); i++) { |
| 552 | 78 | int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formatting differs between platforms | |
| 553 | 78 | av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]); | |
| 554 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77 times.
|
78 | if (silence_log) |
| 555 | 1 | av_log_set_callback(NULL); | |
| 556 |
2/2✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39 times.
|
78 | if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0) |
| 557 | 39 | printf("Error '%s'\n", options[i]); | |
| 558 | else | ||
| 559 | 39 | printf("OK '%s'\n", options[i]); | |
| 560 | 78 | av_log_set_callback(log_callback_help); | |
| 561 | } | ||
| 562 | 1 | av_opt_free(&test_ctx); | |
| 563 | } | ||
| 564 | |||
| 565 | 1 | printf("\nTesting av_opt_set_from_string()\n"); | |
| 566 | { | ||
| 567 | 1 | TestContext test_ctx = { 0 }; | |
| 568 | static const char * const options[] = { | ||
| 569 | "", | ||
| 570 | "5", | ||
| 571 | "5:hello", | ||
| 572 | "5:hello:size=pal", | ||
| 573 | "5:size=pal:hello", | ||
| 574 | ":", | ||
| 575 | "=", | ||
| 576 | " 5 : hello : size = pal ", | ||
| 577 | "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42" | ||
| 578 | }; | ||
| 579 | static const char * const shorthand[] = { "num", "string", NULL }; | ||
| 580 | |||
| 581 | 1 | test_ctx.class = &test_class; | |
| 582 | 1 | av_opt_set_defaults(&test_ctx); | |
| 583 | |||
| 584 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 585 | |||
| 586 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 | for (i=0; i < FF_ARRAY_ELEMS(options); i++) { |
| 587 | 9 | av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]); | |
| 588 |
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) |
| 589 | 4 | printf("Error '%s'\n", options[i]); | |
| 590 | else | ||
| 591 | 5 | printf("OK '%s'\n", options[i]); | |
| 592 | } | ||
| 593 | 1 | av_opt_free(&test_ctx); | |
| 594 | } | ||
| 595 | |||
| 596 | 1 | printf("\nTesting av_opt_find2()\n"); | |
| 597 | { | ||
| 598 | 1 | TestContext test_ctx = { 0 }; | |
| 599 | 1 | ChildContext child_ctx = { 0 }; | |
| 600 | void *target; | ||
| 601 | const AVOption *opt; | ||
| 602 | |||
| 603 | 1 | test_ctx.class = &test_class; | |
| 604 | 1 | child_ctx.class = &child_class; | |
| 605 | 1 | test_ctx.child = &child_ctx; | |
| 606 | |||
| 607 | 1 | av_log_set_level(AV_LOG_QUIET); | |
| 608 | |||
| 609 | // Should succeed. num exists and has opt_flags 1 | ||
| 610 | 1 | opt = av_opt_find2(&test_ctx, "num", NULL, 1, 0, &target); | |
| 611 |
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) |
| 612 | 1 | printf("OK '%s'\n", opt->name); | |
| 613 | else | ||
| 614 | ✗ | printf("Error 'num'\n"); | |
| 615 | |||
| 616 | // Should fail. num64 exists but has opt_flags 1, not 2 | ||
| 617 | 1 | opt = av_opt_find(&test_ctx, "num64", NULL, 2, 0); | |
| 618 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (opt) |
| 619 | ✗ | printf("OK '%s'\n", opt->name); | |
| 620 | else | ||
| 621 | 1 | printf("Error 'num64'\n"); | |
| 622 | |||
| 623 | // Should fail. child_num exists but in a child object we're not searching | ||
| 624 | 1 | opt = av_opt_find(&test_ctx, "child_num", NULL, 0, 0); | |
| 625 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (opt) |
| 626 | ✗ | printf("OK '%s'\n", opt->name); | |
| 627 | else | ||
| 628 | 1 | printf("Error 'child_num'\n"); | |
| 629 | |||
| 630 | // Should succeed. child_num exists in a child object we're searching | ||
| 631 | 1 | opt = av_opt_find2(&test_ctx, "child_num", NULL, 0, AV_OPT_SEARCH_CHILDREN, &target); | |
| 632 |
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) |
| 633 | 1 | printf("OK '%s'\n", opt->name); | |
| 634 | else | ||
| 635 | ✗ | printf("Error 'child_num'\n"); | |
| 636 | |||
| 637 | // Should fail. foo doesn't exist | ||
| 638 | 1 | opt = av_opt_find(&test_ctx, "foo", NULL, 0, 0); | |
| 639 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (opt) |
| 640 | ✗ | printf("OK '%s'\n", opt->name); | |
| 641 | else | ||
| 642 | 1 | printf("Error 'foo'\n"); | |
| 643 | } | ||
| 644 | |||
| 645 | 1 | printf("\nTesting av_opt_copy() into a memdup'ed object\n"); | |
| 646 | { | ||
| 647 | 1 | TestContext test_ctx = { 0 }; | |
| 648 | TestContext *copy; | ||
| 649 | 1 | uint8_t *layout_name = NULL; | |
| 650 | |||
| 651 | 1 | test_ctx.class = &test_class; | |
| 652 | 1 | av_opt_set_defaults(&test_ctx); | |
| 653 | 1 | av_opt_set(&test_ctx, "cl", "FL@Left+FR@Right", 0); | |
| 654 | |||
| 655 | 1 | copy = av_memdup(&test_ctx, sizeof(test_ctx)); | |
| 656 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!copy) |
| 657 | ✗ | return 1; | |
| 658 | 1 | av_opt_copy(copy, &test_ctx); | |
| 659 | 1 | av_opt_free(&test_ctx); | |
| 660 | |||
| 661 | 1 | av_opt_get(copy, "cl", 0, &layout_name); | |
| 662 | 1 | printf("cl=%s\n", layout_name); | |
| 663 | 1 | av_free(layout_name); | |
| 664 | 1 | av_opt_free(copy); | |
| 665 | 1 | av_free(copy); | |
| 666 | } | ||
| 667 | |||
| 668 | 1 | printf("\nTesting av_opt_copy() of a native layout into a custom layout\n"); | |
| 669 | { | ||
| 670 | 1 | TestContext src = { 0 }, dst = { 0 }; | |
| 671 | 1 | AVChannelLayout native = { AV_CHANNEL_ORDER_NATIVE }; | |
| 672 | |||
| 673 | 1 | src.class = dst.class = &test_class; | |
| 674 | 1 | av_opt_set_defaults(&src); | |
| 675 | 1 | av_opt_set_defaults(&dst); | |
| 676 | 1 | av_opt_set(&dst, "cl", "FL@Left+FR@Right", 0); | |
| 677 | |||
| 678 | 1 | native.u.mask = (uintptr_t)dst.channel_layout.u.map; | |
| 679 | 1 | native.nb_channels = av_popcount64(native.u.mask); | |
| 680 | 1 | av_opt_set_chlayout(&src, "cl", &native, 0); | |
| 681 | |||
| 682 | 1 | printf("copy: %d\n", av_opt_copy(&dst, &src)); | |
| 683 | 1 | printf("same layout: %d\n", !av_channel_layout_compare(&src.channel_layout, &dst.channel_layout)); | |
| 684 | 1 | av_opt_free(&src); | |
| 685 | 1 | av_opt_free(&dst); | |
| 686 | } | ||
| 687 | |||
| 688 | 1 | return 0; | |
| 689 | } | ||
| 690 |