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