]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.c
Merge commit '600d5ee6b12bad144756b0772319bb04796bc528'
[ffmpeg] / libavutil / opt.c
1 /*
2  * AVOptions
3  * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * AVOptions
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "avutil.h"
29 #include "avstring.h"
30 #include "channel_layout.h"
31 #include "common.h"
32 #include "opt.h"
33 #include "eval.h"
34 #include "dict.h"
35 #include "log.h"
36 #include "parseutils.h"
37 #include "pixdesc.h"
38 #include "mathematics.h"
39 #include "samplefmt.h"
40
41 #include <float.h>
42
43 #if FF_API_OLD_AVOPTIONS
44 const AVOption *av_next_option(void *obj, const AVOption *last)
45 {
46     return av_opt_next(obj, last);
47 }
48 #endif
49
50 const AVOption *av_opt_next(void *obj, const AVOption *last)
51 {
52     AVClass *class = *(AVClass**)obj;
53     if (!last && class && class->option && class->option[0].name)
54         return class->option;
55     if (last && last[1].name)
56         return ++last;
57     return NULL;
58 }
59
60 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
61 {
62     switch (o->type) {
63     case AV_OPT_TYPE_FLAGS:     *intnum = *(unsigned int*)dst;return 0;
64     case AV_OPT_TYPE_PIXEL_FMT:
65     case AV_OPT_TYPE_SAMPLE_FMT:
66     case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
67     case AV_OPT_TYPE_CHANNEL_LAYOUT:
68     case AV_OPT_TYPE_DURATION:
69     case AV_OPT_TYPE_INT64:     *intnum = *(int64_t     *)dst;return 0;
70     case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
71     case AV_OPT_TYPE_DOUBLE:    *num    = *(double      *)dst;return 0;
72     case AV_OPT_TYPE_RATIONAL:  *intnum = ((AVRational*)dst)->num;
73                                 *den    = ((AVRational*)dst)->den;
74                                                         return 0;
75     case AV_OPT_TYPE_CONST:     *num    = o->default_val.dbl; return 0;
76     }
77     return AVERROR(EINVAL);
78 }
79
80 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
81 {
82     if (o->type != AV_OPT_TYPE_FLAGS &&
83         (o->max * den < num * intnum || o->min * den > num * intnum)) {
84         av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
85                num*intnum/den, o->name, o->min, o->max);
86         return AVERROR(ERANGE);
87     }
88     if (o->type == AV_OPT_TYPE_FLAGS) {
89         double d = num*intnum/den;
90         if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
91             av_log(obj, AV_LOG_ERROR,
92                    "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
93                    num*intnum/den, o->name);
94             return AVERROR(ERANGE);
95         }
96     }
97
98     switch (o->type) {
99     case AV_OPT_TYPE_FLAGS:
100     case AV_OPT_TYPE_PIXEL_FMT:
101     case AV_OPT_TYPE_SAMPLE_FMT:
102     case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
103     case AV_OPT_TYPE_DURATION:
104     case AV_OPT_TYPE_CHANNEL_LAYOUT:
105     case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
106     case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
107     case AV_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
108     case AV_OPT_TYPE_RATIONAL:
109         if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
110         else                 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
111         break;
112     default:
113         return AVERROR(EINVAL);
114     }
115     return 0;
116 }
117
118 static int hexchar2int(char c) {
119     if (c >= '0' && c <= '9') return c - '0';
120     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
121     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
122     return -1;
123 }
124
125 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
126 {
127     int *lendst = (int *)(dst + 1);
128     uint8_t *bin, *ptr;
129     int len = strlen(val);
130
131     av_freep(dst);
132     *lendst = 0;
133
134     if (len & 1)
135         return AVERROR(EINVAL);
136     len /= 2;
137
138     ptr = bin = av_malloc(len);
139     while (*val) {
140         int a = hexchar2int(*val++);
141         int b = hexchar2int(*val++);
142         if (a < 0 || b < 0) {
143             av_free(bin);
144             return AVERROR(EINVAL);
145         }
146         *ptr++ = (a << 4) | b;
147     }
148     *dst = bin;
149     *lendst = len;
150
151     return 0;
152 }
153
154 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
155 {
156     av_freep(dst);
157     *dst = av_strdup(val);
158     return 0;
159 }
160
161 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
162                               opt->type == AV_OPT_TYPE_CONST || \
163                               opt->type == AV_OPT_TYPE_FLAGS || \
164                               opt->type == AV_OPT_TYPE_INT) ? \
165                              opt->default_val.i64 : opt->default_val.dbl)
166
167 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
168 {
169     int ret = 0;
170     int num, den;
171     char c;
172
173     if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
174         if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
175             return ret;
176         ret = 0;
177     }
178
179     for (;;) {
180         int i = 0;
181         char buf[256];
182         int cmd = 0;
183         double d;
184         int64_t intnum = 1;
185
186         if (o->type == AV_OPT_TYPE_FLAGS) {
187             if (*val == '+' || *val == '-')
188                 cmd = *(val++);
189             for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
190                 buf[i] = val[i];
191             buf[i] = 0;
192         }
193
194         {
195             const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
196             int res;
197             int ci = 0;
198             double const_values[64];
199             const char * const_names[64];
200             if (o_named && o_named->type == AV_OPT_TYPE_CONST)
201                 d = DEFAULT_NUMVAL(o_named);
202             else {
203                 if (o->unit) {
204                     for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
205                         if (o_named->type == AV_OPT_TYPE_CONST &&
206                             o_named->unit &&
207                             !strcmp(o_named->unit, o->unit)) {
208                             if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
209                                 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
210                                 return AVERROR_PATCHWELCOME;
211                             }
212                             const_names [ci  ] = o_named->name;
213                             const_values[ci++] = DEFAULT_NUMVAL(o_named);
214                         }
215                     }
216                 }
217                 const_names [ci  ] = "default";
218                 const_values[ci++] = DEFAULT_NUMVAL(o);
219                 const_names [ci  ] = "max";
220                 const_values[ci++] = o->max;
221                 const_names [ci  ] = "min";
222                 const_values[ci++] = o->min;
223                 const_names [ci  ] = "none";
224                 const_values[ci++] = 0;
225                 const_names [ci  ] = "all";
226                 const_values[ci++] = ~0;
227                 const_names [ci] = NULL;
228                 const_values[ci] = 0;
229
230                 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
231                                             const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
232                 if (res < 0) {
233                     av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
234                     return res;
235                 }
236             }
237         }
238         if (o->type == AV_OPT_TYPE_FLAGS) {
239             read_number(o, dst, NULL, NULL, &intnum);
240             if      (cmd == '+') d = intnum | (int64_t)d;
241             else if (cmd == '-') d = intnum &~(int64_t)d;
242         }
243
244         if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
245             return ret;
246         val += i;
247         if (!i || !*val)
248             return 0;
249     }
250
251     return 0;
252 }
253
254 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
255 {
256     int ret;
257
258     if (!val || !strcmp(val, "none")) {
259         dst[0] =
260         dst[1] = 0;
261         return 0;
262     }
263     ret = av_parse_video_size(dst, dst + 1, val);
264     if (ret < 0)
265         av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
266     return ret;
267 }
268
269 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
270 {
271     int ret;
272     if (!val) {
273         ret = AVERROR(EINVAL);
274     } else {
275         ret = av_parse_video_rate(dst, val);
276     }
277     if (ret < 0)
278         av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
279     return ret;
280 }
281
282 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
283 {
284     int ret;
285
286     if (!val) {
287         return 0;
288     } else {
289         ret = av_parse_color(dst, val, -1, obj);
290         if (ret < 0)
291             av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
292         return ret;
293     }
294     return 0;
295 }
296
297 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
298                           int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
299 {
300     int fmt, min, max;
301
302     if (!val || !strcmp(val, "none")) {
303         fmt = -1;
304     } else {
305         fmt = get_fmt(val);
306         if (fmt == -1) {
307             char *tail;
308             fmt = strtol(val, &tail, 0);
309             if (*tail || (unsigned)fmt >= fmt_nb) {
310                 av_log(obj, AV_LOG_ERROR,
311                        "Unable to parse option value \"%s\" as %s\n", val, desc);
312                 return AVERROR(EINVAL);
313             }
314         }
315     }
316
317     min = FFMAX(o->min, -1);
318     max = FFMIN(o->max, fmt_nb-1);
319
320     // hack for compatibility with old ffmpeg
321     if(min == 0 && max == 0) {
322         min = -1;
323         max = fmt_nb-1;
324     }
325
326     if (fmt < min || fmt > max) {
327         av_log(obj, AV_LOG_ERROR,
328                "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
329                fmt, o->name, desc, min, max);
330         return AVERROR(ERANGE);
331     }
332
333     *(int *)dst = fmt;
334     return 0;
335 }
336
337 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
338 {
339     return set_string_fmt(obj, o, val, dst,
340                           AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
341 }
342
343 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
344 {
345     return set_string_fmt(obj, o, val, dst,
346                           AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
347 }
348
349 #if FF_API_OLD_AVOPTIONS
350 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
351 {
352     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
353     if (o_out)
354         *o_out = o;
355     return av_opt_set(obj, name, val, 0);
356 }
357 #endif
358
359 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
360 {
361     int ret = 0;
362     void *dst, *target_obj;
363     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
364     if (!o || !target_obj)
365         return AVERROR_OPTION_NOT_FOUND;
366     if (!val && (o->type != AV_OPT_TYPE_STRING &&
367                  o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
368                  o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
369                  o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
370                  o->type != AV_OPT_TYPE_CHANNEL_LAYOUT))
371         return AVERROR(EINVAL);
372
373     if (o->flags & AV_OPT_FLAG_READONLY)
374         return AVERROR(EINVAL);
375
376     dst = ((uint8_t*)target_obj) + o->offset;
377     switch (o->type) {
378     case AV_OPT_TYPE_STRING:   return set_string(obj, o, val, dst);
379     case AV_OPT_TYPE_BINARY:   return set_string_binary(obj, o, val, dst);
380     case AV_OPT_TYPE_FLAGS:
381     case AV_OPT_TYPE_INT:
382     case AV_OPT_TYPE_INT64:
383     case AV_OPT_TYPE_FLOAT:
384     case AV_OPT_TYPE_DOUBLE:
385     case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
386     case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
387     case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
388     case AV_OPT_TYPE_PIXEL_FMT:  return set_string_pixel_fmt(obj, o, val, dst);
389     case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
390     case AV_OPT_TYPE_DURATION:
391         if (!val) {
392             *(int64_t *)dst = 0;
393             return 0;
394         } else {
395             if ((ret = av_parse_time(dst, val, 1)) < 0)
396                 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
397             return ret;
398         }
399         break;
400     case AV_OPT_TYPE_COLOR:      return set_string_color(obj, o, val, dst);
401     case AV_OPT_TYPE_CHANNEL_LAYOUT:
402         if (!val || !strcmp(val, "none")) {
403             *(int64_t *)dst = 0;
404         } else {
405 #if FF_API_GET_CHANNEL_LAYOUT_COMPAT
406             int64_t cl = ff_get_channel_layout(val, 0);
407 #else
408             int64_t cl = av_get_channel_layout(val);
409 #endif
410             if (!cl) {
411                 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
412                 ret = AVERROR(EINVAL);
413             }
414             *(int64_t *)dst = cl;
415             return ret;
416         }
417         break;
418     }
419
420     av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
421     return AVERROR(EINVAL);
422 }
423
424 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
425     int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
426     {\
427         if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
428             return AVERROR(EINVAL);\
429         return set_string_number(obj, obj, o, val, name ## _out);\
430     }
431
432 OPT_EVAL_NUMBER(flags,  AV_OPT_TYPE_FLAGS,    int)
433 OPT_EVAL_NUMBER(int,    AV_OPT_TYPE_INT,      int)
434 OPT_EVAL_NUMBER(int64,  AV_OPT_TYPE_INT64,    int64_t)
435 OPT_EVAL_NUMBER(float,  AV_OPT_TYPE_FLOAT,    float)
436 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE,   double)
437 OPT_EVAL_NUMBER(q,      AV_OPT_TYPE_RATIONAL, AVRational)
438
439 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
440                                   int search_flags)
441 {
442     void *dst, *target_obj;
443     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
444
445     if (!o || !target_obj)
446         return AVERROR_OPTION_NOT_FOUND;
447
448     if (o->flags & AV_OPT_FLAG_READONLY)
449         return AVERROR(EINVAL);
450
451     dst = ((uint8_t*)target_obj) + o->offset;
452     return write_number(obj, o, dst, num, den, intnum);
453 }
454
455 #if FF_API_OLD_AVOPTIONS
456 const AVOption *av_set_double(void *obj, const char *name, double n)
457 {
458     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
459     if (set_number(obj, name, n, 1, 1, 0) < 0)
460         return NULL;
461     return o;
462 }
463
464 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
465 {
466     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
467     if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
468         return NULL;
469     return o;
470 }
471
472 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
473 {
474     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
475     if (set_number(obj, name, 1, 1, n, 0) < 0)
476         return NULL;
477     return o;
478 }
479 #endif
480
481 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
482 {
483     return set_number(obj, name, 1, 1, val, search_flags);
484 }
485
486 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
487 {
488     return set_number(obj, name, val, 1, 1, search_flags);
489 }
490
491 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
492 {
493     return set_number(obj, name, val.num, val.den, 1, search_flags);
494 }
495
496 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
497 {
498     void *target_obj;
499     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
500     uint8_t *ptr;
501     uint8_t **dst;
502     int *lendst;
503
504     if (!o || !target_obj)
505         return AVERROR_OPTION_NOT_FOUND;
506
507     if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
508         return AVERROR(EINVAL);
509
510     ptr = len ? av_malloc(len) : NULL;
511     if (len && !ptr)
512         return AVERROR(ENOMEM);
513
514     dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
515     lendst = (int *)(dst + 1);
516
517     av_free(*dst);
518     *dst = ptr;
519     *lendst = len;
520     if (len)
521         memcpy(ptr, val, len);
522
523     return 0;
524 }
525
526 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
527 {
528     void *target_obj;
529     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
530
531     if (!o || !target_obj)
532         return AVERROR_OPTION_NOT_FOUND;
533     if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
534         av_log(obj, AV_LOG_ERROR,
535                "The value set by option '%s' is not an image size.\n", o->name);
536         return AVERROR(EINVAL);
537     }
538     if (w<0 || h<0) {
539         av_log(obj, AV_LOG_ERROR,
540                "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
541         return AVERROR(EINVAL);
542     }
543     *(int *)(((uint8_t *)target_obj)             + o->offset) = w;
544     *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
545     return 0;
546 }
547
548 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
549 {
550     void *target_obj;
551     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
552
553     if (!o || !target_obj)
554         return AVERROR_OPTION_NOT_FOUND;
555     if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
556         av_log(obj, AV_LOG_ERROR,
557                "The value set by option '%s' is not a video rate.\n", o->name);
558         return AVERROR(EINVAL);
559     }
560     if (val.num <= 0 || val.den <= 0)
561         return AVERROR(EINVAL);
562     return set_number(obj, name, val.num, val.den, 1, search_flags);
563 }
564
565 static int set_format(void *obj, const char *name, int fmt, int search_flags,
566                       enum AVOptionType type, const char *desc, int nb_fmts)
567 {
568     void *target_obj;
569     const AVOption *o = av_opt_find2(obj, name, NULL, 0,
570                                      search_flags, &target_obj);
571     int min, max;
572
573     if (!o || !target_obj)
574         return AVERROR_OPTION_NOT_FOUND;
575     if (o->type != type) {
576         av_log(obj, AV_LOG_ERROR,
577                "The value set by option '%s' is not a %s format", name, desc);
578         return AVERROR(EINVAL);
579     }
580
581     min = FFMAX(o->min, -1);
582     max = FFMIN(o->max, nb_fmts-1);
583
584     if (fmt < min || fmt > max) {
585         av_log(obj, AV_LOG_ERROR,
586                "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
587                fmt, name, desc, min, max);
588         return AVERROR(ERANGE);
589     }
590     *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
591     return 0;
592 }
593
594 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
595 {
596     return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
597 }
598
599 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
600 {
601     return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
602 }
603
604 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
605 {
606     void *target_obj;
607     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
608
609     if (!o || !target_obj)
610         return AVERROR_OPTION_NOT_FOUND;
611     if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
612         av_log(obj, AV_LOG_ERROR,
613                "The value set by option '%s' is not a channel layout.\n", o->name);
614         return AVERROR(EINVAL);
615     }
616     *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
617     return 0;
618 }
619
620 #if FF_API_OLD_AVOPTIONS
621 /**
622  *
623  * @param buf a buffer which is used for returning non string values as strings, can be NULL
624  * @param buf_len allocated length in bytes of buf
625  */
626 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
627 {
628     const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
629     void *dst;
630     uint8_t *bin;
631     int len, i;
632     if (!o)
633         return NULL;
634     if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
635         return NULL;
636
637     dst= ((uint8_t*)obj) + o->offset;
638     if (o_out) *o_out= o;
639
640     switch (o->type) {
641     case AV_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
642     case AV_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
643     case AV_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
644     case AV_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
645     case AV_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
646     case AV_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
647     case AV_OPT_TYPE_CONST:     snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
648     case AV_OPT_TYPE_STRING:    return *(void**)dst;
649     case AV_OPT_TYPE_BINARY:
650         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
651         if (len >= (buf_len + 1)/2) return NULL;
652         bin = *(uint8_t**)dst;
653         for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
654         break;
655     default: return NULL;
656     }
657     return buf;
658 }
659 #endif
660
661 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
662 {
663     void *target_obj;
664     AVDictionary **dst;
665     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
666
667     if (!o || !target_obj)
668         return AVERROR_OPTION_NOT_FOUND;
669     if (o->flags & AV_OPT_FLAG_READONLY)
670         return AVERROR(EINVAL);
671
672     dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
673     av_dict_free(dst);
674     av_dict_copy(dst, val, 0);
675
676     return 0;
677 }
678
679 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
680 {
681     void *dst, *target_obj;
682     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
683     uint8_t *bin, buf[128];
684     int len, i, ret;
685     int64_t i64;
686
687     if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
688         return AVERROR_OPTION_NOT_FOUND;
689
690     dst = (uint8_t*)target_obj + o->offset;
691
692     buf[0] = 0;
693     switch (o->type) {
694     case AV_OPT_TYPE_FLAGS:     ret = snprintf(buf, sizeof(buf), "0x%08X",  *(int    *)dst);break;
695     case AV_OPT_TYPE_INT:       ret = snprintf(buf, sizeof(buf), "%d" ,     *(int    *)dst);break;
696     case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
697     case AV_OPT_TYPE_FLOAT:     ret = snprintf(buf, sizeof(buf), "%f" ,     *(float  *)dst);break;
698     case AV_OPT_TYPE_DOUBLE:    ret = snprintf(buf, sizeof(buf), "%f" ,     *(double *)dst);break;
699     case AV_OPT_TYPE_VIDEO_RATE:
700     case AV_OPT_TYPE_RATIONAL:  ret = snprintf(buf, sizeof(buf), "%d/%d",   ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
701     case AV_OPT_TYPE_CONST:     ret = snprintf(buf, sizeof(buf), "%f" ,     o->default_val.dbl);break;
702     case AV_OPT_TYPE_STRING:
703         if (*(uint8_t**)dst)
704             *out_val = av_strdup(*(uint8_t**)dst);
705         else
706             *out_val = av_strdup("");
707         return 0;
708     case AV_OPT_TYPE_BINARY:
709         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
710         if ((uint64_t)len*2 + 1 > INT_MAX)
711             return AVERROR(EINVAL);
712         if (!(*out_val = av_malloc(len*2 + 1)))
713             return AVERROR(ENOMEM);
714         bin = *(uint8_t**)dst;
715         for (i = 0; i < len; i++)
716             snprintf(*out_val + i*2, 3, "%02X", bin[i]);
717         return 0;
718     case AV_OPT_TYPE_IMAGE_SIZE:
719         ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
720         break;
721     case AV_OPT_TYPE_PIXEL_FMT:
722         ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
723         break;
724     case AV_OPT_TYPE_SAMPLE_FMT:
725         ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
726         break;
727     case AV_OPT_TYPE_DURATION:
728         i64 = *(int64_t *)dst;
729         ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
730                        i64 / 3600000000, (int)((i64 / 60000000) % 60),
731                        (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
732         break;
733     case AV_OPT_TYPE_COLOR:
734         ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", ((int *)dst)[0], ((int *)dst)[1], ((int *)dst)[2], ((int *)dst)[3]);
735         break;
736     case AV_OPT_TYPE_CHANNEL_LAYOUT:
737         i64 = *(int64_t *)dst;
738         ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
739         break;
740     default:
741         return AVERROR(EINVAL);
742     }
743
744     if (ret >= sizeof(buf))
745         return AVERROR(EINVAL);
746     *out_val = av_strdup(buf);
747     return 0;
748 }
749
750 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
751                       int search_flags)
752 {
753     void *dst, *target_obj;
754     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
755     if (!o || !target_obj)
756         goto error;
757
758     dst = ((uint8_t*)target_obj) + o->offset;
759
760     if (o_out) *o_out= o;
761
762     return read_number(o, dst, num, den, intnum);
763
764 error:
765     *den=*intnum=0;
766     return -1;
767 }
768
769 #if FF_API_OLD_AVOPTIONS
770 double av_get_double(void *obj, const char *name, const AVOption **o_out)
771 {
772     int64_t intnum=1;
773     double num=1;
774     int den=1;
775
776     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
777         return NAN;
778     return num*intnum/den;
779 }
780
781 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
782 {
783     int64_t intnum=1;
784     double num=1;
785     int den=1;
786
787     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
788         return (AVRational){0, 0};
789     if (num == 1.0 && (int)intnum == intnum)
790         return (AVRational){intnum, den};
791     else
792         return av_d2q(num*intnum/den, 1<<24);
793 }
794
795 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
796 {
797     int64_t intnum=1;
798     double num=1;
799     int den=1;
800
801     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
802         return -1;
803     return num*intnum/den;
804 }
805 #endif
806
807 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
808 {
809     int64_t intnum = 1;
810     double     num = 1;
811     int   ret, den = 1;
812
813     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
814         return ret;
815     *out_val = num*intnum/den;
816     return 0;
817 }
818
819 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
820 {
821     int64_t intnum = 1;
822     double     num = 1;
823     int   ret, den = 1;
824
825     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
826         return ret;
827     *out_val = num*intnum/den;
828     return 0;
829 }
830
831 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
832 {
833     int64_t intnum = 1;
834     double     num = 1;
835     int   ret, den = 1;
836
837     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
838         return ret;
839
840     if (num == 1.0 && (int)intnum == intnum)
841         *out_val = (AVRational){intnum, den};
842     else
843         *out_val = av_d2q(num*intnum/den, 1<<24);
844     return 0;
845 }
846
847 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
848 {
849     void *dst, *target_obj;
850     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
851     if (!o || !target_obj)
852         return AVERROR_OPTION_NOT_FOUND;
853     if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
854         av_log(obj, AV_LOG_ERROR,
855                "The value for option '%s' is not an image size.\n", name);
856         return AVERROR(EINVAL);
857     }
858
859     dst = ((uint8_t*)target_obj) + o->offset;
860     if (w_out) *w_out = *(int *)dst;
861     if (h_out) *h_out = *((int *)dst+1);
862     return 0;
863 }
864
865 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
866 {
867     int64_t intnum = 1;
868     double     num = 1;
869     int   ret, den = 1;
870
871     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
872         return ret;
873
874     if (num == 1.0 && (int)intnum == intnum)
875         *out_val = (AVRational){intnum, den};
876     else
877         *out_val = av_d2q(num*intnum/den, 1<<24);
878     return 0;
879 }
880
881 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
882                       enum AVOptionType type, const char *desc)
883 {
884     void *dst, *target_obj;
885     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
886     if (!o || !target_obj)
887         return AVERROR_OPTION_NOT_FOUND;
888     if (o->type != type) {
889         av_log(obj, AV_LOG_ERROR,
890                "The value for option '%s' is not a %s format.\n", desc, name);
891         return AVERROR(EINVAL);
892     }
893
894     dst = ((uint8_t*)target_obj) + o->offset;
895     *out_fmt = *(int *)dst;
896     return 0;
897 }
898
899 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
900 {
901     return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
902 }
903
904 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
905 {
906     return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
907 }
908
909 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
910 {
911     void *dst, *target_obj;
912     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
913     if (!o || !target_obj)
914         return AVERROR_OPTION_NOT_FOUND;
915     if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
916         av_log(obj, AV_LOG_ERROR,
917                "The value for option '%s' is not a channel layout.\n", name);
918         return AVERROR(EINVAL);
919     }
920
921     dst = ((uint8_t*)target_obj) + o->offset;
922     *cl = *(int64_t *)dst;
923     return 0;
924 }
925
926 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
927 {
928     void *target_obj;
929     AVDictionary *src;
930     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
931
932     if (!o || !target_obj)
933         return AVERROR_OPTION_NOT_FOUND;
934     if (o->type != AV_OPT_TYPE_DICT)
935         return AVERROR(EINVAL);
936
937     src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
938     av_dict_copy(out_val, src, 0);
939
940     return 0;
941 }
942
943 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
944 {
945     const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
946     const AVOption *flag  = av_opt_find(obj, flag_name,
947                                         field ? field->unit : NULL, 0, 0);
948     int64_t res;
949
950     if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
951         av_opt_get_int(obj, field_name, 0, &res) < 0)
952         return 0;
953     return res & flag->default_val.i64;
954 }
955
956 static void log_value(void *av_log_obj, int level, double d)
957 {
958     if      (d == INT_MAX) {
959         av_log(av_log_obj, level, "INT_MAX");
960     } else if (d == INT_MIN) {
961         av_log(av_log_obj, level, "INT_MIN");
962     } else if (d == UINT32_MAX) {
963         av_log(av_log_obj, level, "UINT32_MAX");
964     } else if (d == (double)INT64_MAX) {
965         av_log(av_log_obj, level, "I64_MAX");
966     } else if (d == INT64_MIN) {
967         av_log(av_log_obj, level, "I64_MIN");
968     } else if (d == FLT_MAX) {
969         av_log(av_log_obj, level, "FLT_MAX");
970     } else if (d == FLT_MIN) {
971         av_log(av_log_obj, level, "FLT_MIN");
972     } else if (d == -FLT_MAX) {
973         av_log(av_log_obj, level, "-FLT_MAX");
974     } else if (d == -FLT_MIN) {
975         av_log(av_log_obj, level, "-FLT_MIN");
976     } else if (d == DBL_MAX) {
977         av_log(av_log_obj, level, "DBL_MAX");
978     } else if (d == DBL_MIN) {
979         av_log(av_log_obj, level, "DBL_MIN");
980     } else if (d == -DBL_MAX) {
981         av_log(av_log_obj, level, "-DBL_MAX");
982     } else if (d == -DBL_MIN) {
983         av_log(av_log_obj, level, "-DBL_MIN");
984     } else {
985         av_log(av_log_obj, level, "%g", d);
986     }
987 }
988
989 static void opt_list(void *obj, void *av_log_obj, const char *unit,
990                      int req_flags, int rej_flags)
991 {
992     const AVOption *opt=NULL;
993     AVOptionRanges *r;
994     int i;
995
996     while ((opt = av_opt_next(obj, opt))) {
997         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
998             continue;
999
1000         /* Don't print CONST's on level one.
1001          * Don't print anything but CONST's on level two.
1002          * Only print items from the requested unit.
1003          */
1004         if (!unit && opt->type==AV_OPT_TYPE_CONST)
1005             continue;
1006         else if (unit && opt->type!=AV_OPT_TYPE_CONST)
1007             continue;
1008         else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1009             continue;
1010         else if (unit && opt->type == AV_OPT_TYPE_CONST)
1011             av_log(av_log_obj, AV_LOG_INFO, "     %-15s ", opt->name);
1012         else
1013             av_log(av_log_obj, AV_LOG_INFO, "  %s%-17s ",
1014                    (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1015                    opt->name);
1016
1017         switch (opt->type) {
1018             case AV_OPT_TYPE_FLAGS:
1019                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1020                 break;
1021             case AV_OPT_TYPE_INT:
1022                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1023                 break;
1024             case AV_OPT_TYPE_INT64:
1025                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1026                 break;
1027             case AV_OPT_TYPE_DOUBLE:
1028                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1029                 break;
1030             case AV_OPT_TYPE_FLOAT:
1031                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1032                 break;
1033             case AV_OPT_TYPE_STRING:
1034                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1035                 break;
1036             case AV_OPT_TYPE_RATIONAL:
1037                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1038                 break;
1039             case AV_OPT_TYPE_BINARY:
1040                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1041                 break;
1042             case AV_OPT_TYPE_IMAGE_SIZE:
1043                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1044                 break;
1045             case AV_OPT_TYPE_VIDEO_RATE:
1046                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1047                 break;
1048             case AV_OPT_TYPE_PIXEL_FMT:
1049                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1050                 break;
1051             case AV_OPT_TYPE_SAMPLE_FMT:
1052                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1053                 break;
1054             case AV_OPT_TYPE_DURATION:
1055                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1056                 break;
1057             case AV_OPT_TYPE_COLOR:
1058                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1059                 break;
1060             case AV_OPT_TYPE_CHANNEL_LAYOUT:
1061                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1062                 break;
1063             case AV_OPT_TYPE_CONST:
1064             default:
1065                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1066                 break;
1067         }
1068         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1069         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1070         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1071         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
1072         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
1073         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1074         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT)         ? 'X' : '.');
1075         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY)       ? 'R' : '.');
1076
1077         if (opt->help)
1078             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1079
1080         if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1081             switch (opt->type) {
1082             case AV_OPT_TYPE_INT:
1083             case AV_OPT_TYPE_INT64:
1084             case AV_OPT_TYPE_DOUBLE:
1085             case AV_OPT_TYPE_FLOAT:
1086             case AV_OPT_TYPE_RATIONAL:
1087                 for (i = 0; i < r->nb_ranges; i++) {
1088                     av_log(av_log_obj, AV_LOG_INFO, " (from ");
1089                     log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1090                     av_log(av_log_obj, AV_LOG_INFO, " to ");
1091                     log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1092                     av_log(av_log_obj, AV_LOG_INFO, ")");
1093                 }
1094                 break;
1095             }
1096             av_opt_freep_ranges(&r);
1097         }
1098
1099         if (opt->type != AV_OPT_TYPE_CONST  &&
1100             opt->type != AV_OPT_TYPE_BINARY &&
1101                 !((opt->type == AV_OPT_TYPE_COLOR      ||
1102                    opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1103                    opt->type == AV_OPT_TYPE_STRING     ||
1104                    opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1105                   !opt->default_val.str)) {
1106             av_log(av_log_obj, AV_LOG_INFO, " (default ");
1107             switch (opt->type) {
1108             case AV_OPT_TYPE_FLAGS:
1109                 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1110                 break;
1111             case AV_OPT_TYPE_DURATION:
1112             case AV_OPT_TYPE_INT:
1113             case AV_OPT_TYPE_INT64:
1114                 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1115                 break;
1116             case AV_OPT_TYPE_DOUBLE:
1117             case AV_OPT_TYPE_FLOAT:
1118                 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1119                 break;
1120             case AV_OPT_TYPE_RATIONAL: {
1121                 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1122                 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1123                 break;
1124             case AV_OPT_TYPE_PIXEL_FMT:
1125                 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1126                 break;
1127             case AV_OPT_TYPE_SAMPLE_FMT:
1128                 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1129                 break;
1130             case AV_OPT_TYPE_COLOR:
1131             case AV_OPT_TYPE_IMAGE_SIZE:
1132             case AV_OPT_TYPE_STRING:
1133             case AV_OPT_TYPE_VIDEO_RATE:
1134                 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1135                 break;
1136             case AV_OPT_TYPE_CHANNEL_LAYOUT:
1137                 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1138                 break;
1139             }
1140             av_log(av_log_obj, AV_LOG_INFO, ")");
1141         }
1142
1143         av_log(av_log_obj, AV_LOG_INFO, "\n");
1144         if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1145             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1146         }
1147     }
1148 }
1149
1150 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1151 {
1152     if (!obj)
1153         return -1;
1154
1155     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1156
1157     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1158
1159     return 0;
1160 }
1161
1162 void av_opt_set_defaults(void *s)
1163 {
1164 #if FF_API_OLD_AVOPTIONS
1165     av_opt_set_defaults2(s, 0, 0);
1166 }
1167
1168 void av_opt_set_defaults2(void *s, int mask, int flags)
1169 {
1170 #endif
1171     const AVOption *opt = NULL;
1172     while ((opt = av_opt_next(s, opt))) {
1173         void *dst = ((uint8_t*)s) + opt->offset;
1174 #if FF_API_OLD_AVOPTIONS
1175         if ((opt->flags & mask) != flags)
1176             continue;
1177 #endif
1178
1179         if (opt->flags & AV_OPT_FLAG_READONLY)
1180             continue;
1181
1182         switch (opt->type) {
1183             case AV_OPT_TYPE_CONST:
1184                 /* Nothing to be done here */
1185             break;
1186             case AV_OPT_TYPE_FLAGS:
1187             case AV_OPT_TYPE_INT:
1188             case AV_OPT_TYPE_INT64:
1189             case AV_OPT_TYPE_DURATION:
1190             case AV_OPT_TYPE_CHANNEL_LAYOUT:
1191                 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1192             break;
1193             case AV_OPT_TYPE_DOUBLE:
1194             case AV_OPT_TYPE_FLOAT: {
1195                 double val;
1196                 val = opt->default_val.dbl;
1197                 write_number(s, opt, dst, val, 1, 1);
1198             }
1199             break;
1200             case AV_OPT_TYPE_RATIONAL: {
1201                 AVRational val;
1202                 val = av_d2q(opt->default_val.dbl, INT_MAX);
1203                 write_number(s, opt, dst, 1, val.den, val.num);
1204             }
1205             break;
1206             case AV_OPT_TYPE_COLOR:
1207                 set_string_color(s, opt, opt->default_val.str, dst);
1208                 break;
1209             case AV_OPT_TYPE_STRING:
1210                 set_string(s, opt, opt->default_val.str, dst);
1211                 break;
1212             case AV_OPT_TYPE_IMAGE_SIZE:
1213                 set_string_image_size(s, opt, opt->default_val.str, dst);
1214                 break;
1215             case AV_OPT_TYPE_VIDEO_RATE:
1216                 set_string_video_rate(s, opt, opt->default_val.str, dst);
1217                 break;
1218             case AV_OPT_TYPE_PIXEL_FMT:
1219                 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1220                 break;
1221             case AV_OPT_TYPE_SAMPLE_FMT:
1222                 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1223                 break;
1224             case AV_OPT_TYPE_BINARY:
1225             case AV_OPT_TYPE_DICT:
1226                 /* Cannot set defaults for these types */
1227             break;
1228             default:
1229                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1230         }
1231     }
1232 }
1233
1234 /**
1235  * Store the value in the field in ctx that is named like key.
1236  * ctx must be an AVClass context, storing is done using AVOptions.
1237  *
1238  * @param buf the string to parse, buf will be updated to point at the
1239  * separator just after the parsed key/value pair
1240  * @param key_val_sep a 0-terminated list of characters used to
1241  * separate key from value
1242  * @param pairs_sep a 0-terminated list of characters used to separate
1243  * two pairs from each other
1244  * @return 0 if the key/value pair has been successfully parsed and
1245  * set, or a negative value corresponding to an AVERROR code in case
1246  * of error:
1247  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1248  * the error code issued by av_opt_set() if the key/value pair
1249  * cannot be set
1250  */
1251 static int parse_key_value_pair(void *ctx, const char **buf,
1252                                 const char *key_val_sep, const char *pairs_sep)
1253 {
1254     char *key = av_get_token(buf, key_val_sep);
1255     char *val;
1256     int ret;
1257
1258     if (!key)
1259         return AVERROR(ENOMEM);
1260
1261     if (*key && strspn(*buf, key_val_sep)) {
1262         (*buf)++;
1263         val = av_get_token(buf, pairs_sep);
1264         if (!val) {
1265             av_freep(&key);
1266             return AVERROR(ENOMEM);
1267         }
1268     } else {
1269         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1270         av_free(key);
1271         return AVERROR(EINVAL);
1272     }
1273
1274     av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1275
1276     ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1277     if (ret == AVERROR_OPTION_NOT_FOUND)
1278         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1279
1280     av_free(key);
1281     av_free(val);
1282     return ret;
1283 }
1284
1285 int av_set_options_string(void *ctx, const char *opts,
1286                           const char *key_val_sep, const char *pairs_sep)
1287 {
1288     int ret, count = 0;
1289
1290     if (!opts)
1291         return 0;
1292
1293     while (*opts) {
1294         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1295             return ret;
1296         count++;
1297
1298         if (*opts)
1299             opts++;
1300     }
1301
1302     return count;
1303 }
1304
1305 #define WHITESPACES " \n\t"
1306
1307 static int is_key_char(char c)
1308 {
1309     return (unsigned)((c | 32) - 'a') < 26 ||
1310            (unsigned)(c - '0') < 10 ||
1311            c == '-' || c == '_' || c == '/' || c == '.';
1312 }
1313
1314 /**
1315  * Read a key from a string.
1316  *
1317  * The key consists of is_key_char characters and must be terminated by a
1318  * character from the delim string; spaces are ignored.
1319  *
1320  * @return  0 for success (even with ellipsis), <0 for failure
1321  */
1322 static int get_key(const char **ropts, const char *delim, char **rkey)
1323 {
1324     const char *opts = *ropts;
1325     const char *key_start, *key_end;
1326
1327     key_start = opts += strspn(opts, WHITESPACES);
1328     while (is_key_char(*opts))
1329         opts++;
1330     key_end = opts;
1331     opts += strspn(opts, WHITESPACES);
1332     if (!*opts || !strchr(delim, *opts))
1333         return AVERROR(EINVAL);
1334     opts++;
1335     if (!(*rkey = av_malloc(key_end - key_start + 1)))
1336         return AVERROR(ENOMEM);
1337     memcpy(*rkey, key_start, key_end - key_start);
1338     (*rkey)[key_end - key_start] = 0;
1339     *ropts = opts;
1340     return 0;
1341 }
1342
1343 int av_opt_get_key_value(const char **ropts,
1344                          const char *key_val_sep, const char *pairs_sep,
1345                          unsigned flags,
1346                          char **rkey, char **rval)
1347 {
1348     int ret;
1349     char *key = NULL, *val;
1350     const char *opts = *ropts;
1351
1352     if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1353         !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1354         return AVERROR(EINVAL);
1355     if (!(val = av_get_token(&opts, pairs_sep))) {
1356         av_free(key);
1357         return AVERROR(ENOMEM);
1358     }
1359     *ropts = opts;
1360     *rkey  = key;
1361     *rval  = val;
1362     return 0;
1363 }
1364
1365 int av_opt_set_from_string(void *ctx, const char *opts,
1366                            const char *const *shorthand,
1367                            const char *key_val_sep, const char *pairs_sep)
1368 {
1369     int ret, count = 0;
1370     const char *dummy_shorthand = NULL;
1371     char *av_uninit(parsed_key), *av_uninit(value);
1372     const char *key;
1373
1374     if (!opts)
1375         return 0;
1376     if (!shorthand)
1377         shorthand = &dummy_shorthand;
1378
1379     while (*opts) {
1380         ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1381                                    *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1382                                    &parsed_key, &value);
1383         if (ret < 0) {
1384             if (ret == AVERROR(EINVAL))
1385                 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1386             else
1387                 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1388                        av_err2str(ret));
1389             return ret;
1390         }
1391         if (*opts)
1392             opts++;
1393         if (parsed_key) {
1394             key = parsed_key;
1395             while (*shorthand) /* discard all remaining shorthand */
1396                 shorthand++;
1397         } else {
1398             key = *(shorthand++);
1399         }
1400
1401         av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1402         if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1403             if (ret == AVERROR_OPTION_NOT_FOUND)
1404                 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1405             av_free(value);
1406             av_free(parsed_key);
1407             return ret;
1408         }
1409
1410         av_free(value);
1411         av_free(parsed_key);
1412         count++;
1413     }
1414     return count;
1415 }
1416
1417 void av_opt_free(void *obj)
1418 {
1419     const AVOption *o = NULL;
1420     while ((o = av_opt_next(obj, o))) {
1421         switch (o->type) {
1422         case AV_OPT_TYPE_STRING:
1423         case AV_OPT_TYPE_BINARY:
1424             av_freep((uint8_t *)obj + o->offset);
1425             break;
1426
1427         case AV_OPT_TYPE_DICT:
1428             av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1429             break;
1430
1431         default:
1432             break;
1433         }
1434     }
1435 }
1436
1437 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1438 {
1439     AVDictionaryEntry *t = NULL;
1440     AVDictionary    *tmp = NULL;
1441     int ret = 0;
1442
1443     if (!options)
1444         return 0;
1445
1446     while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1447         ret = av_opt_set(obj, t->key, t->value, search_flags);
1448         if (ret == AVERROR_OPTION_NOT_FOUND)
1449             av_dict_set(&tmp, t->key, t->value, 0);
1450         else if (ret < 0) {
1451             av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1452             break;
1453         }
1454         ret = 0;
1455     }
1456     av_dict_free(options);
1457     *options = tmp;
1458     return ret;
1459 }
1460
1461 int av_opt_set_dict(void *obj, AVDictionary **options)
1462 {
1463     return av_opt_set_dict2(obj, options, 0);
1464 }
1465
1466 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1467                             int opt_flags, int search_flags)
1468 {
1469     return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1470 }
1471
1472 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1473                              int opt_flags, int search_flags, void **target_obj)
1474 {
1475     const AVClass  *c;
1476     const AVOption *o = NULL;
1477
1478     if(!obj)
1479         return NULL;
1480
1481     c= *(AVClass**)obj;
1482
1483     if (!c)
1484         return NULL;
1485
1486     if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1487         if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1488             const AVClass *child = NULL;
1489             while (child = av_opt_child_class_next(c, child))
1490                 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1491                     return o;
1492         } else {
1493             void *child = NULL;
1494             while (child = av_opt_child_next(obj, child))
1495                 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1496                     return o;
1497         }
1498     }
1499
1500     while (o = av_opt_next(obj, o)) {
1501         if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1502             ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1503              (unit  && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1504             if (target_obj) {
1505                 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1506                     *target_obj = obj;
1507                 else
1508                     *target_obj = NULL;
1509             }
1510             return o;
1511         }
1512     }
1513     return NULL;
1514 }
1515
1516 void *av_opt_child_next(void *obj, void *prev)
1517 {
1518     const AVClass *c = *(AVClass**)obj;
1519     if (c->child_next)
1520         return c->child_next(obj, prev);
1521     return NULL;
1522 }
1523
1524 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1525 {
1526     if (parent->child_class_next)
1527         return parent->child_class_next(prev);
1528     return NULL;
1529 }
1530
1531 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1532 {
1533     const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1534     if(!opt)
1535         return NULL;
1536     return (uint8_t*)obj + opt->offset;
1537 }
1538
1539 static int opt_size(enum AVOptionType type)
1540 {
1541     switch(type) {
1542     case AV_OPT_TYPE_INT:
1543     case AV_OPT_TYPE_FLAGS:     return sizeof(int);
1544     case AV_OPT_TYPE_DURATION:
1545     case AV_OPT_TYPE_CHANNEL_LAYOUT:
1546     case AV_OPT_TYPE_INT64:     return sizeof(int64_t);
1547     case AV_OPT_TYPE_DOUBLE:    return sizeof(double);
1548     case AV_OPT_TYPE_FLOAT:     return sizeof(float);
1549     case AV_OPT_TYPE_STRING:    return sizeof(uint8_t*);
1550     case AV_OPT_TYPE_VIDEO_RATE:
1551     case AV_OPT_TYPE_RATIONAL:  return sizeof(AVRational);
1552     case AV_OPT_TYPE_BINARY:    return sizeof(uint8_t*) + sizeof(int);
1553     case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
1554     case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
1555     case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
1556     case AV_OPT_TYPE_COLOR:     return 4;
1557     }
1558     return 0;
1559 }
1560
1561 int av_opt_copy(void *dst, void *src)
1562 {
1563     const AVOption *o = NULL;
1564     const AVClass *c;
1565     int ret = 0;
1566
1567     if (!src)
1568         return 0;
1569
1570     c = *(AVClass**)src;
1571     if (*(AVClass**)dst && c != *(AVClass**)dst)
1572         return AVERROR(EINVAL);
1573
1574     while ((o = av_opt_next(src, o))) {
1575         void *field_dst = ((uint8_t*)dst) + o->offset;
1576         void *field_src = ((uint8_t*)src) + o->offset;
1577         uint8_t **field_dst8 = (uint8_t**)field_dst;
1578         uint8_t **field_src8 = (uint8_t**)field_src;
1579
1580         if (o->type == AV_OPT_TYPE_STRING) {
1581             if (*field_dst8 != *field_src8)
1582                 av_freep(field_dst8);
1583             *field_dst8 = av_strdup(*field_src8);
1584             if (*field_src8 && !*field_dst8)
1585                 ret = AVERROR(ENOMEM);
1586         } else if (o->type == AV_OPT_TYPE_BINARY) {
1587             int len = *(int*)(field_src8 + 1);
1588             if (*field_dst8 != *field_src8)
1589                 av_freep(field_dst8);
1590             *field_dst8 = av_memdup(*field_src8, len);
1591             if (len && !*field_dst8) {
1592                 ret = AVERROR(ENOMEM);
1593                 len = 0;
1594             }
1595             *(int*)(field_dst8 + 1) = len;
1596         } else if (o->type == AV_OPT_TYPE_CONST) {
1597             // do nothing
1598         } else {
1599             memcpy(field_dst, field_src, opt_size(o->type));
1600         }
1601     }
1602     return ret;
1603 }
1604
1605 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1606 {
1607     int ret;
1608     const AVClass *c = *(AVClass**)obj;
1609     int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1610
1611     if (c->version > (52 << 16 | 11 << 8))
1612         callback = c->query_ranges;
1613
1614     if (!callback)
1615         callback = av_opt_query_ranges_default;
1616
1617     ret = callback(ranges_arg, obj, key, flags);
1618     if (ret >= 0) {
1619         if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1620             ret = 1;
1621         (*ranges_arg)->nb_components = ret;
1622     }
1623     return ret;
1624 }
1625
1626 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1627 {
1628     AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1629     AVOptionRange **range_array = av_mallocz(sizeof(void*));
1630     AVOptionRange *range = av_mallocz(sizeof(*range));
1631     const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1632     int ret;
1633
1634     *ranges_arg = NULL;
1635
1636     if (!ranges || !range || !range_array || !field) {
1637         ret = AVERROR(ENOMEM);
1638         goto fail;
1639     }
1640
1641     ranges->range = range_array;
1642     ranges->range[0] = range;
1643     ranges->nb_ranges = 1;
1644     ranges->nb_components = 1;
1645     range->is_range = 1;
1646     range->value_min = field->min;
1647     range->value_max = field->max;
1648
1649     switch (field->type) {
1650     case AV_OPT_TYPE_INT:
1651     case AV_OPT_TYPE_INT64:
1652     case AV_OPT_TYPE_PIXEL_FMT:
1653     case AV_OPT_TYPE_SAMPLE_FMT:
1654     case AV_OPT_TYPE_FLOAT:
1655     case AV_OPT_TYPE_DOUBLE:
1656     case AV_OPT_TYPE_DURATION:
1657     case AV_OPT_TYPE_COLOR:
1658     case AV_OPT_TYPE_CHANNEL_LAYOUT:
1659         break;
1660     case AV_OPT_TYPE_STRING:
1661         range->component_min = 0;
1662         range->component_max = 0x10FFFF; // max unicode value
1663         range->value_min = -1;
1664         range->value_max = INT_MAX;
1665         break;
1666     case AV_OPT_TYPE_RATIONAL:
1667         range->component_min = INT_MIN;
1668         range->component_max = INT_MAX;
1669         break;
1670     case AV_OPT_TYPE_IMAGE_SIZE:
1671         range->component_min = 0;
1672         range->component_max = INT_MAX/128/8;
1673         range->value_min = 0;
1674         range->value_max = INT_MAX/8;
1675         break;
1676     case AV_OPT_TYPE_VIDEO_RATE:
1677         range->component_min = 1;
1678         range->component_max = INT_MAX;
1679         range->value_min = 1;
1680         range->value_max = INT_MAX;
1681         break;
1682     default:
1683         ret = AVERROR(ENOSYS);
1684         goto fail;
1685     }
1686
1687     *ranges_arg = ranges;
1688     return 1;
1689 fail:
1690     av_free(ranges);
1691     av_free(range);
1692     av_free(range_array);
1693     return ret;
1694 }
1695
1696 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1697 {
1698     int i;
1699     AVOptionRanges *ranges = *rangesp;
1700
1701     if (!ranges)
1702         return;
1703
1704     for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1705         AVOptionRange *range = ranges->range[i];
1706         if (range) {
1707             av_freep(&range->str);
1708             av_freep(&ranges->range[i]);
1709         }
1710     }
1711     av_freep(&ranges->range);
1712     av_freep(rangesp);
1713 }
1714
1715 #ifdef TEST
1716
1717 typedef struct TestContext
1718 {
1719     const AVClass *class;
1720     int num;
1721     int toggle;
1722     char *string;
1723     int flags;
1724     AVRational rational;
1725     AVRational video_rate;
1726     int w, h;
1727     enum AVPixelFormat pix_fmt;
1728     enum AVSampleFormat sample_fmt;
1729     int64_t duration;
1730     uint8_t color[4];
1731     int64_t channel_layout;
1732 } TestContext;
1733
1734 #define OFFSET(x) offsetof(TestContext, x)
1735
1736 #define TEST_FLAG_COOL 01
1737 #define TEST_FLAG_LAME 02
1738 #define TEST_FLAG_MU   04
1739
1740 static const AVOption test_options[]= {
1741 {"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {.i64 = 0},       0,        100                 },
1742 {"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {.i64 = 0},       0,        1                   },
1743 {"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0},       0,        10                  },
1744 {"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {.str = "default"}, CHAR_MIN, CHAR_MAX          },
1745 {"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {.i64 = 0},       0,        INT_MAX, 0, "flags" },
1746 {"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
1747 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
1748 {"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
1749 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
1750 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
1751 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
1752 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE,  {.str = "25"}, 0,     0                   },
1753 {"duration", "set duration",   OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1754 {"color", "set color",   OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1755 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
1756 {NULL},
1757 };
1758
1759 static const char *test_get_name(void *ctx)
1760 {
1761     return "test";
1762 }
1763
1764 static const AVClass test_class = {
1765     "TestContext",
1766     test_get_name,
1767     test_options
1768 };
1769
1770 int main(void)
1771 {
1772     int i;
1773
1774     printf("\nTesting av_set_options_string()\n");
1775     {
1776         TestContext test_ctx = { 0 };
1777         static const char * const options[] = {
1778             "",
1779             ":",
1780             "=",
1781             "foo=:",
1782             ":=foo",
1783             "=foo",
1784             "foo=",
1785             "foo",
1786             "foo=val",
1787             "foo==val",
1788             "toggle=:",
1789             "string=:",
1790             "toggle=1 : foo",
1791             "toggle=100",
1792             "toggle==1",
1793             "flags=+mu-lame : num=42: toggle=0",
1794             "num=42 : string=blahblah",
1795             "rational=0 : rational=1/2 : rational=1/-1",
1796             "rational=-1/0",
1797             "size=1024x768",
1798             "size=pal",
1799             "size=bogus",
1800             "pix_fmt=yuv420p",
1801             "pix_fmt=2",
1802             "pix_fmt=bogus",
1803             "sample_fmt=s16",
1804             "sample_fmt=2",
1805             "sample_fmt=bogus",
1806             "video_rate=pal",
1807             "video_rate=25",
1808             "video_rate=30000/1001",
1809             "video_rate=30/1.001",
1810             "video_rate=bogus",
1811             "duration=bogus",
1812             "duration=123.45",
1813             "duration=1\\:23\\:45.67",
1814             "color=blue",
1815             "color=0x223300",
1816             "color=0x42FF07AA",
1817             "cl=stereo+downmix",
1818             "cl=foo",
1819         };
1820
1821         test_ctx.class = &test_class;
1822         av_opt_set_defaults(&test_ctx);
1823
1824         av_log_set_level(AV_LOG_DEBUG);
1825
1826         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1827             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1828             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1829                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1830             printf("\n");
1831         }
1832         av_opt_free(&test_ctx);
1833     }
1834
1835     printf("\nTesting av_opt_set_from_string()\n");
1836     {
1837         TestContext test_ctx = { 0 };
1838         static const char * const options[] = {
1839             "",
1840             "5",
1841             "5:hello",
1842             "5:hello:size=pal",
1843             "5:size=pal:hello",
1844             ":",
1845             "=",
1846             " 5 : hello : size = pal ",
1847             "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1848         };
1849         static const char * const shorthand[] = { "num", "string", NULL };
1850
1851         test_ctx.class = &test_class;
1852         av_opt_set_defaults(&test_ctx);
1853
1854         av_log_set_level(AV_LOG_DEBUG);
1855
1856         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1857             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1858             if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1859                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1860             printf("\n");
1861         }
1862         av_opt_free(&test_ctx);
1863     }
1864
1865     return 0;
1866 }
1867
1868 #endif