]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.c
Merge commit '19d3127867f001d007f98bc8c5a85c5409abf788'
[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 == UINT32_MAX) {
925         av_log(av_log_obj, level, "UINT32_MAX");
926     } else if (d == (double)INT64_MAX) {
927         av_log(av_log_obj, level, "I64_MAX");
928     } else if (d == INT64_MIN) {
929         av_log(av_log_obj, level, "I64_MIN");
930     } else if (d == FLT_MAX) {
931         av_log(av_log_obj, level, "FLT_MAX");
932     } else if (d == FLT_MIN) {
933         av_log(av_log_obj, level, "FLT_MIN");
934     } else if (d == -FLT_MAX) {
935         av_log(av_log_obj, level, "-FLT_MAX");
936     } else if (d == -FLT_MIN) {
937         av_log(av_log_obj, level, "-FLT_MIN");
938     } else if (d == DBL_MAX) {
939         av_log(av_log_obj, level, "DBL_MAX");
940     } else if (d == DBL_MIN) {
941         av_log(av_log_obj, level, "DBL_MIN");
942     } else if (d == -DBL_MAX) {
943         av_log(av_log_obj, level, "-DBL_MAX");
944     } else if (d == -DBL_MIN) {
945         av_log(av_log_obj, level, "-DBL_MIN");
946     } else {
947         av_log(av_log_obj, level, "%g", d);
948     }
949 }
950
951 static void opt_list(void *obj, void *av_log_obj, const char *unit,
952                      int req_flags, int rej_flags)
953 {
954     const AVOption *opt=NULL;
955     AVOptionRanges *r;
956     int i;
957
958     while ((opt = av_opt_next(obj, opt))) {
959         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
960             continue;
961
962         /* Don't print CONST's on level one.
963          * Don't print anything but CONST's on level two.
964          * Only print items from the requested unit.
965          */
966         if (!unit && opt->type==AV_OPT_TYPE_CONST)
967             continue;
968         else if (unit && opt->type!=AV_OPT_TYPE_CONST)
969             continue;
970         else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
971             continue;
972         else if (unit && opt->type == AV_OPT_TYPE_CONST)
973             av_log(av_log_obj, AV_LOG_INFO, "     %-15s ", opt->name);
974         else
975             av_log(av_log_obj, AV_LOG_INFO, "  %s%-17s ",
976                    (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
977                    opt->name);
978
979         switch (opt->type) {
980             case AV_OPT_TYPE_FLAGS:
981                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
982                 break;
983             case AV_OPT_TYPE_INT:
984                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
985                 break;
986             case AV_OPT_TYPE_INT64:
987                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
988                 break;
989             case AV_OPT_TYPE_DOUBLE:
990                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
991                 break;
992             case AV_OPT_TYPE_FLOAT:
993                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
994                 break;
995             case AV_OPT_TYPE_STRING:
996                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
997                 break;
998             case AV_OPT_TYPE_RATIONAL:
999                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1000                 break;
1001             case AV_OPT_TYPE_BINARY:
1002                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1003                 break;
1004             case AV_OPT_TYPE_IMAGE_SIZE:
1005                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1006                 break;
1007             case AV_OPT_TYPE_VIDEO_RATE:
1008                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1009                 break;
1010             case AV_OPT_TYPE_PIXEL_FMT:
1011                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1012                 break;
1013             case AV_OPT_TYPE_SAMPLE_FMT:
1014                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1015                 break;
1016             case AV_OPT_TYPE_DURATION:
1017                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1018                 break;
1019             case AV_OPT_TYPE_COLOR:
1020                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1021                 break;
1022             case AV_OPT_TYPE_CHANNEL_LAYOUT:
1023                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1024                 break;
1025             case AV_OPT_TYPE_CONST:
1026             default:
1027                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1028                 break;
1029         }
1030         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1031         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1032         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1033         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
1034         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
1035         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1036
1037         if (opt->help)
1038             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1039
1040         if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1041             switch (opt->type) {
1042             case AV_OPT_TYPE_INT:
1043             case AV_OPT_TYPE_INT64:
1044             case AV_OPT_TYPE_DOUBLE:
1045             case AV_OPT_TYPE_FLOAT:
1046             case AV_OPT_TYPE_RATIONAL:
1047                 for (i = 0; i < r->nb_ranges; i++) {
1048                     av_log(av_log_obj, AV_LOG_INFO, " (from ");
1049                     log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1050                     av_log(av_log_obj, AV_LOG_INFO, " to ");
1051                     log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1052                     av_log(av_log_obj, AV_LOG_INFO, ")");
1053                 }
1054                 break;
1055             }
1056             av_opt_freep_ranges(&r);
1057         }
1058
1059         if (opt->type != AV_OPT_TYPE_CONST  &&
1060             opt->type != AV_OPT_TYPE_BINARY &&
1061                 !((opt->type == AV_OPT_TYPE_COLOR      ||
1062                    opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1063                    opt->type == AV_OPT_TYPE_STRING     ||
1064                    opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1065                   !opt->default_val.str)) {
1066             av_log(av_log_obj, AV_LOG_INFO, " (default ");
1067             switch (opt->type) {
1068             case AV_OPT_TYPE_FLAGS:
1069                 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1070                 break;
1071             case AV_OPT_TYPE_DURATION:
1072             case AV_OPT_TYPE_INT:
1073             case AV_OPT_TYPE_INT64:
1074                 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1075                 break;
1076             case AV_OPT_TYPE_DOUBLE:
1077             case AV_OPT_TYPE_FLOAT:
1078                 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1079                 break;
1080             case AV_OPT_TYPE_RATIONAL: {
1081                 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1082                 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1083                 break;
1084             case AV_OPT_TYPE_PIXEL_FMT:
1085                 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1086                 break;
1087             case AV_OPT_TYPE_SAMPLE_FMT:
1088                 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1089                 break;
1090             case AV_OPT_TYPE_COLOR:
1091             case AV_OPT_TYPE_IMAGE_SIZE:
1092             case AV_OPT_TYPE_STRING:
1093             case AV_OPT_TYPE_VIDEO_RATE:
1094                 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1095                 break;
1096             case AV_OPT_TYPE_CHANNEL_LAYOUT:
1097                 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1098                 break;
1099             }
1100             av_log(av_log_obj, AV_LOG_INFO, ")");
1101         }
1102
1103         av_log(av_log_obj, AV_LOG_INFO, "\n");
1104         if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1105             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1106         }
1107     }
1108 }
1109
1110 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1111 {
1112     if (!obj)
1113         return -1;
1114
1115     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1116
1117     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1118
1119     return 0;
1120 }
1121
1122 void av_opt_set_defaults(void *s)
1123 {
1124 #if FF_API_OLD_AVOPTIONS
1125     av_opt_set_defaults2(s, 0, 0);
1126 }
1127
1128 void av_opt_set_defaults2(void *s, int mask, int flags)
1129 {
1130 #endif
1131     const AVClass *class = *(AVClass **)s;
1132     const AVOption *opt = NULL;
1133     while ((opt = av_opt_next(s, opt)) != NULL) {
1134         void *dst = ((uint8_t*)s) + opt->offset;
1135 #if FF_API_OLD_AVOPTIONS
1136         if ((opt->flags & mask) != flags)
1137             continue;
1138 #endif
1139         switch (opt->type) {
1140             case AV_OPT_TYPE_CONST:
1141                 /* Nothing to be done here */
1142             break;
1143             case AV_OPT_TYPE_FLAGS:
1144             case AV_OPT_TYPE_INT:
1145             case AV_OPT_TYPE_INT64:
1146             case AV_OPT_TYPE_DURATION:
1147             case AV_OPT_TYPE_CHANNEL_LAYOUT:
1148                 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1149             break;
1150             case AV_OPT_TYPE_DOUBLE:
1151             case AV_OPT_TYPE_FLOAT: {
1152                 double val;
1153                 val = opt->default_val.dbl;
1154                 write_number(s, opt, dst, val, 1, 1);
1155             }
1156             break;
1157             case AV_OPT_TYPE_RATIONAL: {
1158                 AVRational val;
1159                 val = av_d2q(opt->default_val.dbl, INT_MAX);
1160                 write_number(s, opt, dst, 1, val.den, val.num);
1161             }
1162             break;
1163             case AV_OPT_TYPE_COLOR:
1164                 set_string_color(s, opt, opt->default_val.str, dst);
1165                 break;
1166             case AV_OPT_TYPE_STRING:
1167                 set_string(s, opt, opt->default_val.str, dst);
1168                 break;
1169             case AV_OPT_TYPE_IMAGE_SIZE:
1170                 set_string_image_size(s, opt, opt->default_val.str, dst);
1171                 break;
1172             case AV_OPT_TYPE_VIDEO_RATE:
1173                 set_string_video_rate(s, opt, opt->default_val.str, dst);
1174                 break;
1175             case AV_OPT_TYPE_PIXEL_FMT:
1176 #if LIBAVUTIL_VERSION_MAJOR < 54
1177                 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1178                     av_opt_set(s, opt->name, opt->default_val.str, 0);
1179                 else
1180 #endif
1181                     write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1182                 break;
1183             case AV_OPT_TYPE_SAMPLE_FMT:
1184 #if LIBAVUTIL_VERSION_MAJOR < 54
1185                 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1186                     av_opt_set(s, opt->name, opt->default_val.str, 0);
1187                 else
1188 #endif
1189                     write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1190                 break;
1191             case AV_OPT_TYPE_BINARY:
1192                 /* Cannot set default for binary */
1193             break;
1194             default:
1195                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1196         }
1197     }
1198 }
1199
1200 /**
1201  * Store the value in the field in ctx that is named like key.
1202  * ctx must be an AVClass context, storing is done using AVOptions.
1203  *
1204  * @param buf the string to parse, buf will be updated to point at the
1205  * separator just after the parsed key/value pair
1206  * @param key_val_sep a 0-terminated list of characters used to
1207  * separate key from value
1208  * @param pairs_sep a 0-terminated list of characters used to separate
1209  * two pairs from each other
1210  * @return 0 if the key/value pair has been successfully parsed and
1211  * set, or a negative value corresponding to an AVERROR code in case
1212  * of error:
1213  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1214  * the error code issued by av_opt_set() if the key/value pair
1215  * cannot be set
1216  */
1217 static int parse_key_value_pair(void *ctx, const char **buf,
1218                                 const char *key_val_sep, const char *pairs_sep)
1219 {
1220     char *key = av_get_token(buf, key_val_sep);
1221     char *val;
1222     int ret;
1223
1224     if (!key)
1225         return AVERROR(ENOMEM);
1226
1227     if (*key && strspn(*buf, key_val_sep)) {
1228         (*buf)++;
1229         val = av_get_token(buf, pairs_sep);
1230         if (!val) {
1231             av_freep(&key);
1232             return AVERROR(ENOMEM);
1233         }
1234     } else {
1235         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1236         av_free(key);
1237         return AVERROR(EINVAL);
1238     }
1239
1240     av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1241
1242     ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1243     if (ret == AVERROR_OPTION_NOT_FOUND)
1244         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1245
1246     av_free(key);
1247     av_free(val);
1248     return ret;
1249 }
1250
1251 int av_set_options_string(void *ctx, const char *opts,
1252                           const char *key_val_sep, const char *pairs_sep)
1253 {
1254     int ret, count = 0;
1255
1256     if (!opts)
1257         return 0;
1258
1259     while (*opts) {
1260         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1261             return ret;
1262         count++;
1263
1264         if (*opts)
1265             opts++;
1266     }
1267
1268     return count;
1269 }
1270
1271 #define WHITESPACES " \n\t"
1272
1273 static int is_key_char(char c)
1274 {
1275     return (unsigned)((c | 32) - 'a') < 26 ||
1276            (unsigned)(c - '0') < 10 ||
1277            c == '-' || c == '_' || c == '/' || c == '.';
1278 }
1279
1280 /**
1281  * Read a key from a string.
1282  *
1283  * The key consists of is_key_char characters and must be terminated by a
1284  * character from the delim string; spaces are ignored.
1285  *
1286  * @return  0 for success (even with ellipsis), <0 for failure
1287  */
1288 static int get_key(const char **ropts, const char *delim, char **rkey)
1289 {
1290     const char *opts = *ropts;
1291     const char *key_start, *key_end;
1292
1293     key_start = opts += strspn(opts, WHITESPACES);
1294     while (is_key_char(*opts))
1295         opts++;
1296     key_end = opts;
1297     opts += strspn(opts, WHITESPACES);
1298     if (!*opts || !strchr(delim, *opts))
1299         return AVERROR(EINVAL);
1300     opts++;
1301     if (!(*rkey = av_malloc(key_end - key_start + 1)))
1302         return AVERROR(ENOMEM);
1303     memcpy(*rkey, key_start, key_end - key_start);
1304     (*rkey)[key_end - key_start] = 0;
1305     *ropts = opts;
1306     return 0;
1307 }
1308
1309 int av_opt_get_key_value(const char **ropts,
1310                          const char *key_val_sep, const char *pairs_sep,
1311                          unsigned flags,
1312                          char **rkey, char **rval)
1313 {
1314     int ret;
1315     char *key = NULL, *val;
1316     const char *opts = *ropts;
1317
1318     if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1319         !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1320         return AVERROR(EINVAL);
1321     if (!(val = av_get_token(&opts, pairs_sep))) {
1322         av_free(key);
1323         return AVERROR(ENOMEM);
1324     }
1325     *ropts = opts;
1326     *rkey  = key;
1327     *rval  = val;
1328     return 0;
1329 }
1330
1331 int av_opt_set_from_string(void *ctx, const char *opts,
1332                            const char *const *shorthand,
1333                            const char *key_val_sep, const char *pairs_sep)
1334 {
1335     int ret, count = 0;
1336     const char *dummy_shorthand = NULL;
1337     char *av_uninit(parsed_key), *av_uninit(value);
1338     const char *key;
1339
1340     if (!opts)
1341         return 0;
1342     if (!shorthand)
1343         shorthand = &dummy_shorthand;
1344
1345     while (*opts) {
1346         ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1347                                    *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1348                                    &parsed_key, &value);
1349         if (ret < 0) {
1350             if (ret == AVERROR(EINVAL))
1351                 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1352             else
1353                 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1354                        av_err2str(ret));
1355             return ret;
1356         }
1357         if (*opts)
1358             opts++;
1359         if (parsed_key) {
1360             key = parsed_key;
1361             while (*shorthand) /* discard all remaining shorthand */
1362                 shorthand++;
1363         } else {
1364             key = *(shorthand++);
1365         }
1366
1367         av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1368         if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1369             if (ret == AVERROR_OPTION_NOT_FOUND)
1370                 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1371             av_free(value);
1372             av_free(parsed_key);
1373             return ret;
1374         }
1375
1376         av_free(value);
1377         av_free(parsed_key);
1378         count++;
1379     }
1380     return count;
1381 }
1382
1383 void av_opt_free(void *obj)
1384 {
1385     const AVOption *o = NULL;
1386     while ((o = av_opt_next(obj, o)))
1387         if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1388             av_freep((uint8_t *)obj + o->offset);
1389 }
1390
1391 int av_opt_set_dict(void *obj, AVDictionary **options)
1392 {
1393     AVDictionaryEntry *t = NULL;
1394     AVDictionary    *tmp = NULL;
1395     int ret = 0;
1396
1397     while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1398         ret = av_opt_set(obj, t->key, t->value, 0);
1399         if (ret == AVERROR_OPTION_NOT_FOUND)
1400             av_dict_set(&tmp, t->key, t->value, 0);
1401         else if (ret < 0) {
1402             av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1403             break;
1404         }
1405         ret = 0;
1406     }
1407     av_dict_free(options);
1408     *options = tmp;
1409     return ret;
1410 }
1411
1412 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1413                             int opt_flags, int search_flags)
1414 {
1415     return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1416 }
1417
1418 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1419                              int opt_flags, int search_flags, void **target_obj)
1420 {
1421     const AVClass  *c;
1422     const AVOption *o = NULL;
1423
1424     if(!obj)
1425         return NULL;
1426
1427     c= *(AVClass**)obj;
1428
1429     if (!c)
1430         return NULL;
1431
1432     if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1433         if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1434             const AVClass *child = NULL;
1435             while (child = av_opt_child_class_next(c, child))
1436                 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1437                     return o;
1438         } else {
1439             void *child = NULL;
1440             while (child = av_opt_child_next(obj, child))
1441                 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1442                     return o;
1443         }
1444     }
1445
1446     while (o = av_opt_next(obj, o)) {
1447         if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1448             ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1449              (unit  && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1450             if (target_obj) {
1451                 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1452                     *target_obj = obj;
1453                 else
1454                     *target_obj = NULL;
1455             }
1456             return o;
1457         }
1458     }
1459     return NULL;
1460 }
1461
1462 void *av_opt_child_next(void *obj, void *prev)
1463 {
1464     const AVClass *c = *(AVClass**)obj;
1465     if (c->child_next)
1466         return c->child_next(obj, prev);
1467     return NULL;
1468 }
1469
1470 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1471 {
1472     if (parent->child_class_next)
1473         return parent->child_class_next(prev);
1474     return NULL;
1475 }
1476
1477 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1478 {
1479     const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1480     if(!opt)
1481         return NULL;
1482     return (uint8_t*)obj + opt->offset;
1483 }
1484
1485 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1486 {
1487     const AVClass *c = *(AVClass**)obj;
1488     int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1489
1490     if (c->version > (52 << 16 | 11 << 8))
1491         callback = c->query_ranges;
1492
1493     if (!callback)
1494         callback = av_opt_query_ranges_default;
1495
1496     return callback(ranges_arg, obj, key, flags);
1497 }
1498
1499 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1500 {
1501     AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1502     AVOptionRange **range_array = av_mallocz(sizeof(void*));
1503     AVOptionRange *range = av_mallocz(sizeof(*range));
1504     const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1505     int ret;
1506
1507     *ranges_arg = NULL;
1508
1509     if (!ranges || !range || !range_array || !field) {
1510         ret = AVERROR(ENOMEM);
1511         goto fail;
1512     }
1513
1514     ranges->range = range_array;
1515     ranges->range[0] = range;
1516     ranges->nb_ranges = 1;
1517     range->is_range = 1;
1518     range->value_min = field->min;
1519     range->value_max = field->max;
1520
1521     switch (field->type) {
1522     case AV_OPT_TYPE_INT:
1523     case AV_OPT_TYPE_INT64:
1524     case AV_OPT_TYPE_PIXEL_FMT:
1525     case AV_OPT_TYPE_SAMPLE_FMT:
1526     case AV_OPT_TYPE_FLOAT:
1527     case AV_OPT_TYPE_DOUBLE:
1528     case AV_OPT_TYPE_DURATION:
1529     case AV_OPT_TYPE_COLOR:
1530     case AV_OPT_TYPE_CHANNEL_LAYOUT:
1531         break;
1532     case AV_OPT_TYPE_STRING:
1533         range->component_min = 0;
1534         range->component_max = 0x10FFFF; // max unicode value
1535         range->value_min = -1;
1536         range->value_max = INT_MAX;
1537         break;
1538     case AV_OPT_TYPE_RATIONAL:
1539         range->component_min = INT_MIN;
1540         range->component_max = INT_MAX;
1541         break;
1542     case AV_OPT_TYPE_IMAGE_SIZE:
1543         range->component_min = 0;
1544         range->component_max = INT_MAX/128/8;
1545         range->value_min = 0;
1546         range->value_max = INT_MAX/8;
1547         break;
1548     case AV_OPT_TYPE_VIDEO_RATE:
1549         range->component_min = 1;
1550         range->component_max = INT_MAX;
1551         range->value_min = 1;
1552         range->value_max = INT_MAX;
1553         break;
1554     default:
1555         ret = AVERROR(ENOSYS);
1556         goto fail;
1557     }
1558
1559     *ranges_arg = ranges;
1560     return 0;
1561 fail:
1562     av_free(ranges);
1563     av_free(range);
1564     av_free(range_array);
1565     return ret;
1566 }
1567
1568 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1569 {
1570     int i;
1571     AVOptionRanges *ranges = *rangesp;
1572
1573     for (i = 0; i < ranges->nb_ranges; i++) {
1574         AVOptionRange *range = ranges->range[i];
1575         av_freep(&range->str);
1576         av_freep(&ranges->range[i]);
1577     }
1578     av_freep(&ranges->range);
1579     av_freep(rangesp);
1580 }
1581
1582 #ifdef TEST
1583
1584 typedef struct TestContext
1585 {
1586     const AVClass *class;
1587     int num;
1588     int toggle;
1589     char *string;
1590     int flags;
1591     AVRational rational;
1592     AVRational video_rate;
1593     int w, h;
1594     enum AVPixelFormat pix_fmt;
1595     enum AVSampleFormat sample_fmt;
1596     int64_t duration;
1597     uint8_t color[4];
1598     int64_t channel_layout;
1599 } TestContext;
1600
1601 #define OFFSET(x) offsetof(TestContext, x)
1602
1603 #define TEST_FLAG_COOL 01
1604 #define TEST_FLAG_LAME 02
1605 #define TEST_FLAG_MU   04
1606
1607 static const AVOption test_options[]= {
1608 {"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {.i64 = 0},       0,        100                 },
1609 {"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {.i64 = 0},       0,        1                   },
1610 {"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0},       0,        10                  },
1611 {"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {.str = "default"}, CHAR_MIN, CHAR_MAX          },
1612 {"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {.i64 = 0},       0,        INT_MAX, 0, "flags" },
1613 {"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
1614 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
1615 {"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
1616 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
1617 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
1618 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
1619 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE,  {.str = "25"}, 0,     0                   },
1620 {"duration", "set duration",   OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1621 {"color", "set color",   OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1622 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
1623 {NULL},
1624 };
1625
1626 static const char *test_get_name(void *ctx)
1627 {
1628     return "test";
1629 }
1630
1631 static const AVClass test_class = {
1632     "TestContext",
1633     test_get_name,
1634     test_options
1635 };
1636
1637 int main(void)
1638 {
1639     int i;
1640
1641     printf("\nTesting av_set_options_string()\n");
1642     {
1643         TestContext test_ctx = { 0 };
1644         static const char * const options[] = {
1645             "",
1646             ":",
1647             "=",
1648             "foo=:",
1649             ":=foo",
1650             "=foo",
1651             "foo=",
1652             "foo",
1653             "foo=val",
1654             "foo==val",
1655             "toggle=:",
1656             "string=:",
1657             "toggle=1 : foo",
1658             "toggle=100",
1659             "toggle==1",
1660             "flags=+mu-lame : num=42: toggle=0",
1661             "num=42 : string=blahblah",
1662             "rational=0 : rational=1/2 : rational=1/-1",
1663             "rational=-1/0",
1664             "size=1024x768",
1665             "size=pal",
1666             "size=bogus",
1667             "pix_fmt=yuv420p",
1668             "pix_fmt=2",
1669             "pix_fmt=bogus",
1670             "sample_fmt=s16",
1671             "sample_fmt=2",
1672             "sample_fmt=bogus",
1673             "video_rate=pal",
1674             "video_rate=25",
1675             "video_rate=30000/1001",
1676             "video_rate=30/1.001",
1677             "video_rate=bogus",
1678             "duration=bogus",
1679             "duration=123.45",
1680             "duration=1\\:23\\:45.67",
1681             "color=blue",
1682             "color=0x223300",
1683             "color=0x42FF07AA",
1684             "cl=stereo+downmix",
1685             "cl=foo",
1686         };
1687
1688         test_ctx.class = &test_class;
1689         av_opt_set_defaults(&test_ctx);
1690
1691         av_log_set_level(AV_LOG_DEBUG);
1692
1693         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1694             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1695             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1696                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1697             printf("\n");
1698         }
1699         av_opt_free(&test_ctx);
1700     }
1701
1702     printf("\nTesting av_opt_set_from_string()\n");
1703     {
1704         TestContext test_ctx = { 0 };
1705         static const char * const options[] = {
1706             "",
1707             "5",
1708             "5:hello",
1709             "5:hello:size=pal",
1710             "5:size=pal:hello",
1711             ":",
1712             "=",
1713             " 5 : hello : size = pal ",
1714             "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1715         };
1716         static const char * const shorthand[] = { "num", "string", NULL };
1717
1718         test_ctx.class = &test_class;
1719         av_opt_set_defaults(&test_ctx);
1720
1721         av_log_set_level(AV_LOG_DEBUG);
1722
1723         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1724             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1725             if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1726                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1727             printf("\n");
1728         }
1729         av_opt_free(&test_ctx);
1730     }
1731
1732     return 0;
1733 }
1734
1735 #endif