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     int num;
34     int toggle;
35     char *string;
36     int flags;
37     AVRational rational;
38     AVRational video_rate;
39     int w, h;
40     enum AVPixelFormat pix_fmt;
41     enum AVSampleFormat sample_fmt;
42     int64_t duration;
43     uint8_t color[4];
44     AVChannelLayout channel_layout;
45     void *binary;
46     int binary_size;
47     void *binary1;
48     int binary_size1;
49     void *binary2;
50     int binary_size2;
51     int64_t num64;
52     float flt;
53     double dbl;
54     char *escape;
55     int bool1;
56     int bool2;
57     int bool3;
58     AVDictionary *dict1;
59     AVDictionary *dict2;
60 } TestContext;
61 
62 #define OFFSET(x) offsetof(TestContext, x)
63 
64 #define TEST_FLAG_COOL 01
65 #define TEST_FLAG_LAME 02
66 #define TEST_FLAG_MU   04
67 
68 static const AVOption test_options[]= {
69     {"num",        "set num",            OFFSET(num),            AV_OPT_TYPE_INT,            { .i64 = 0 },                      0,       100, 1 },
70     {"toggle",     "set toggle",         OFFSET(toggle),         AV_OPT_TYPE_INT,            { .i64 = 1 },                      0,         1, 1 },
71     {"rational",   "set rational",       OFFSET(rational),       AV_OPT_TYPE_RATIONAL,       { .dbl = 1 },                      0,        10, 1 },
72     {"string",     "set string",         OFFSET(string),         AV_OPT_TYPE_STRING,         { .str = "default" },       CHAR_MIN,  CHAR_MAX, 1 },
73     {"escape",     "set escape str",     OFFSET(escape),         AV_OPT_TYPE_STRING,         { .str = "\\=," },          CHAR_MIN,  CHAR_MAX, 1 },
74     {"flags",      "set flags",          OFFSET(flags),          AV_OPT_TYPE_FLAGS,          { .i64 = 1 },                      0,   INT_MAX, 1, "flags" },
75     {"cool",       "set cool flag",      0,                      AV_OPT_TYPE_CONST,          { .i64 = TEST_FLAG_COOL },   INT_MIN,   INT_MAX, 1, "flags" },
76     {"lame",       "set lame flag",      0,                      AV_OPT_TYPE_CONST,          { .i64 = TEST_FLAG_LAME },   INT_MIN,   INT_MAX, 1, "flags" },
77     {"mu",         "set mu flag",        0,                      AV_OPT_TYPE_CONST,          { .i64 = TEST_FLAG_MU },     INT_MIN,   INT_MAX, 1, "flags" },
78     {"size",       "set size",           OFFSET(w),              AV_OPT_TYPE_IMAGE_SIZE,     { .str="200x300" },                0,         0, 1 },
79     {"pix_fmt",    "set pixfmt",         OFFSET(pix_fmt),        AV_OPT_TYPE_PIXEL_FMT,      { .i64 = AV_PIX_FMT_0BGR },       -1,   INT_MAX, 1 },
80     {"sample_fmt", "set samplefmt",      OFFSET(sample_fmt),     AV_OPT_TYPE_SAMPLE_FMT,     { .i64 = AV_SAMPLE_FMT_S16 },     -1,   INT_MAX, 1 },
81     {"video_rate", "set videorate",      OFFSET(video_rate),     AV_OPT_TYPE_VIDEO_RATE,     { .str = "25" },                   0,         INT_MAX, 1 },
82     {"duration",   "set duration",       OFFSET(duration),       AV_OPT_TYPE_DURATION,       { .i64 = 1000 },                   0, INT64_MAX, 1 },
83     {"color",      "set color",          OFFSET(color),          AV_OPT_TYPE_COLOR,          { .str = "pink" },                 0,         0, 1 },
84     {"cl",         "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHLAYOUT,       { .str = "hexagonal" },            0,         0, 1 },
85     {"bin",        "set binary value",   OFFSET(binary),         AV_OPT_TYPE_BINARY,         { .str="62696e00" },               0,         0, 1 },
86     {"bin1",       "set binary value",   OFFSET(binary1),        AV_OPT_TYPE_BINARY,         { .str=NULL },                     0,         0, 1 },
87     {"bin2",       "set binary value",   OFFSET(binary2),        AV_OPT_TYPE_BINARY,         { .str="" },                       0,         0, 1 },
88     {"num64",      "set num 64bit",      OFFSET(num64),          AV_OPT_TYPE_INT64,          { .i64 = 1 },                      0,       100, 1 },
89     {"flt",        "set float",          OFFSET(flt),            AV_OPT_TYPE_FLOAT,          { .dbl = 1.0 / 3 },                0,       100, 1 },
90     {"dbl",        "set double",         OFFSET(dbl),            AV_OPT_TYPE_DOUBLE,         { .dbl = 1.0 / 3 },                0,       100, 1 },
91     {"bool1",      "set boolean value",  OFFSET(bool1),          AV_OPT_TYPE_BOOL,           { .i64 = -1 },                    -1,         1, 1 },
92     {"bool2",      "set boolean value",  OFFSET(bool2),          AV_OPT_TYPE_BOOL,           { .i64 = 1 },                     -1,         1, 1 },
93     {"bool3",      "set boolean value",  OFFSET(bool3),          AV_OPT_TYPE_BOOL,           { .i64 = 0 },                      0,         1, 1 },
94     {"dict1",      "set dictionary value", OFFSET(dict1),        AV_OPT_TYPE_DICT,           { .str = NULL},                    0,         0, 1 },
95     {"dict2",      "set dictionary value", OFFSET(dict2),        AV_OPT_TYPE_DICT,           { .str = "happy=':-)'"},           0,         0, 1 },
96     { NULL },
97 };
98 
test_get_name(void *ctx)99 static const char *test_get_name(void *ctx)
100 {
101     return "test";
102 }
103 
104 static const AVClass test_class = {
105     .class_name = "TestContext",
106     .item_name  = test_get_name,
107     .option     = test_options,
108     .version    = LIBAVUTIL_VERSION_INT,
109 };
110 
log_callback_help(void *ptr, int level, const char *fmt, va_list vl)111 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
112 {
113     vfprintf(stdout, fmt, vl);
114 }
115 
main(void)116 int main(void)
117 {
118     int i;
119 
120     av_log_set_level(AV_LOG_DEBUG);
121     av_log_set_callback(log_callback_help);
122 
123     printf("Testing default values\n");
124     {
125         TestContext test_ctx = { 0 };
126         test_ctx.class = &test_class;
127         av_opt_set_defaults(&test_ctx);
128 
129         printf("num=%d\n", test_ctx.num);
130         printf("toggle=%d\n", test_ctx.toggle);
131         printf("string=%s\n", test_ctx.string);
132         printf("escape=%s\n", test_ctx.escape);
133         printf("flags=%d\n", test_ctx.flags);
134         printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
135         printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
136         printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
137         printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
138         printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
139         printf("duration=%"PRId64"\n", test_ctx.duration);
140         printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
141         printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout.u.mask, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
142         if (test_ctx.binary)
143             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]);
144         printf("binary_size=%d\n", test_ctx.binary_size);
145         printf("num64=%"PRId64"\n", test_ctx.num64);
146         printf("flt=%.6f\n", test_ctx.flt);
147         printf("dbl=%.6f\n", test_ctx.dbl);
148 
149         av_opt_show2(&test_ctx, NULL, -1, 0);
150 
151         av_opt_free(&test_ctx);
152     }
153 
154     printf("\nTesting av_opt_is_set_to_default()\n");
155     {
156         int ret;
157         TestContext test_ctx = { 0 };
158         const AVOption *o = NULL;
159         test_ctx.class = &test_class;
160 
161         av_log_set_level(AV_LOG_QUIET);
162 
163         while (o = av_opt_next(&test_ctx, o)) {
164             ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
165             printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
166         }
167         av_opt_set_defaults(&test_ctx);
168         while (o = av_opt_next(&test_ctx, o)) {
169             ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
170             printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
171         }
172         av_opt_free(&test_ctx);
173     }
174 
175     printf("\nTesting av_opt_get/av_opt_set()\n");
176     {
177         TestContext test_ctx = { 0 };
178         TestContext test2_ctx = { 0 };
179         const AVOption *o = NULL;
180         test_ctx.class = &test_class;
181         test2_ctx.class = &test_class;
182 
183         av_log_set_level(AV_LOG_QUIET);
184 
185         av_opt_set_defaults(&test_ctx);
186 
187         while (o = av_opt_next(&test_ctx, o)) {
188             char *value1 = NULL;
189             char *value2 = NULL;
190             int ret1 = AVERROR_BUG;
191             int ret2 = AVERROR_BUG;
192             int ret3 = AVERROR_BUG;
193 
194             if (o->type == AV_OPT_TYPE_CONST)
195                 continue;
196 
197             ret1 = av_opt_get(&test_ctx, o->name, 0, (uint8_t **)&value1);
198             if (ret1 >= 0) {
199                 ret2 = av_opt_set(&test2_ctx, o->name, value1, 0);
200                 if (ret2 >= 0)
201                     ret3 = av_opt_get(&test2_ctx, o->name, 0, (uint8_t **)&value2);
202             }
203 
204             printf("name: %-11s get: %-16s set: %-16s get: %-16s %s\n", o->name,
205                     ret1 >= 0 ? value1 : av_err2str(ret1),
206                     ret2 >= 0 ? "OK" : av_err2str(ret2),
207                     ret3 >= 0 ? value2 : av_err2str(ret3),
208                     ret1 >= 0 && ret2 >= 0 && ret3 >= 0 && !strcmp(value1, value2) ? "OK" : "Mismatch");
209             av_free(value1);
210             av_free(value2);
211         }
212         av_opt_free(&test_ctx);
213         av_opt_free(&test2_ctx);
214     }
215 
216     printf("\nTest av_opt_serialize()\n");
217     {
218         TestContext test_ctx = { 0 };
219         char *buf;
220         test_ctx.class = &test_class;
221 
222         av_log_set_level(AV_LOG_QUIET);
223 
224         av_opt_set_defaults(&test_ctx);
225         if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
226             printf("%s\n", buf);
227             av_opt_free(&test_ctx);
228             memset(&test_ctx, 0, sizeof(test_ctx));
229             test_ctx.class = &test_class;
230             av_set_options_string(&test_ctx, buf, "=", ",");
231             av_free(buf);
232             if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
233                 printf("%s\n", buf);
234                 av_free(buf);
235             }
236         }
237         av_opt_free(&test_ctx);
238     }
239 
240     printf("\nTesting av_set_options_string()\n");
241     {
242         TestContext test_ctx = { 0 };
243         static const char * const options[] = {
244             "",
245             ":",
246             "=",
247             "foo=:",
248             ":=foo",
249             "=foo",
250             "foo=",
251             "foo",
252             "foo=val",
253             "foo==val",
254             "toggle=:",
255             "string=:",
256             "toggle=1 : foo",
257             "toggle=100",
258             "toggle==1",
259             "flags=+mu-lame : num=42: toggle=0",
260             "num=42 : string=blahblah",
261             "rational=0 : rational=1/2 : rational=1/-1",
262             "rational=-1/0",
263             "size=1024x768",
264             "size=pal",
265             "size=bogus",
266             "pix_fmt=yuv420p",
267             "pix_fmt=2",
268             "pix_fmt=bogus",
269             "sample_fmt=s16",
270             "sample_fmt=2",
271             "sample_fmt=bogus",
272             "video_rate=pal",
273             "video_rate=25",
274             "video_rate=30000/1001",
275             "video_rate=30/1.001",
276             "video_rate=bogus",
277             "duration=bogus",
278             "duration=123.45",
279             "duration=1\\:23\\:45.67",
280             "color=blue",
281             "color=0x223300",
282             "color=0x42FF07AA",
283             "cl=FL+FR",
284             "cl=foo",
285             "bin=boguss",
286             "bin=111",
287             "bin=ffff",
288             "num64=bogus",
289             "num64=44",
290             "num64=44.4",
291             "num64=-1",
292             "num64=101",
293             "flt=bogus",
294             "flt=2",
295             "flt=2.2",
296             "flt=-1",
297             "flt=101",
298             "dbl=bogus",
299             "dbl=2",
300             "dbl=2.2",
301             "dbl=-1",
302             "dbl=101",
303             "bool1=true",
304             "bool2=auto",
305             "dict1='happy=\\:-):sad=\\:-('",
306         };
307 
308         test_ctx.class = &test_class;
309         av_opt_set_defaults(&test_ctx);
310 
311         av_log_set_level(AV_LOG_QUIET);
312 
313         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
314             int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
315             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
316             if (silence_log)
317                 av_log_set_callback(NULL);
318             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
319                 printf("Error '%s'\n", options[i]);
320             else
321                 printf("OK    '%s'\n", options[i]);
322             av_log_set_callback(log_callback_help);
323         }
324         av_opt_free(&test_ctx);
325     }
326 
327     printf("\nTesting av_opt_set_from_string()\n");
328     {
329         TestContext test_ctx = { 0 };
330         static const char * const options[] = {
331             "",
332             "5",
333             "5:hello",
334             "5:hello:size=pal",
335             "5:size=pal:hello",
336             ":",
337             "=",
338             " 5 : hello : size = pal ",
339             "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
340         };
341         static const char * const shorthand[] = { "num", "string", NULL };
342 
343         test_ctx.class = &test_class;
344         av_opt_set_defaults(&test_ctx);
345 
346         av_log_set_level(AV_LOG_QUIET);
347 
348         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
349             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
350             if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
351                 printf("Error '%s'\n", options[i]);
352             else
353                 printf("OK    '%s'\n", options[i]);
354         }
355         av_opt_free(&test_ctx);
356     }
357 
358     return 0;
359 }
360