]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.c
Merge commit 'a1bcc76e6036e78f25cbb7323c145056cfca9d93'
[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 "common.h"
31 #include "opt.h"
32 #include "eval.h"
33 #include "dict.h"
34 #include "log.h"
35 #include "parseutils.h"
36 #include "pixdesc.h"
37 #include "mathematics.h"
38
39 #if FF_API_FIND_OPT
40 //FIXME order them and do a bin search
41 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
42 {
43     const AVOption *o = NULL;
44
45     while ((o = av_next_option(v, o))) {
46         if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
47             return o;
48     }
49     return NULL;
50 }
51 #endif
52
53 #if FF_API_OLD_AVOPTIONS
54 const AVOption *av_next_option(void *obj, const AVOption *last)
55 {
56     return av_opt_next(obj, last);
57 }
58 #endif
59
60 const AVOption *av_opt_next(void *obj, const AVOption *last)
61 {
62     AVClass *class = *(AVClass**)obj;
63     if (!last && class->option && class->option[0].name)
64         return class->option;
65     if (last && last[1].name)           return ++last;
66     return NULL;
67 }
68
69 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
70 {
71     switch (o->type) {
72     case AV_OPT_TYPE_FLAGS:     *intnum = *(unsigned int*)dst;return 0;
73     case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
74     case AV_OPT_TYPE_INT64:     *intnum = *(int64_t     *)dst;return 0;
75     case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
76     case AV_OPT_TYPE_DOUBLE:    *num    = *(double      *)dst;return 0;
77     case AV_OPT_TYPE_RATIONAL:  *intnum = ((AVRational*)dst)->num;
78                                 *den    = ((AVRational*)dst)->den;
79                                                         return 0;
80     case AV_OPT_TYPE_CONST:     *num    = o->default_val.dbl; return 0;
81     }
82     return AVERROR(EINVAL);
83 }
84
85 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
86 {
87     if (o->max*den < num*intnum || o->min*den > num*intnum) {
88         av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
89                num*intnum/den, o->name);
90         return AVERROR(ERANGE);
91     }
92
93     switch (o->type) {
94     case AV_OPT_TYPE_FLAGS:
95     case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
96     case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
97     case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
98     case AV_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
99     case AV_OPT_TYPE_RATIONAL:
100         if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
101         else                 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
102         break;
103     default:
104         return AVERROR(EINVAL);
105     }
106     return 0;
107 }
108
109 static const double const_values[] = {
110     M_PI,
111     M_E,
112     FF_QP2LAMBDA,
113     0
114 };
115
116 static const char * const const_names[] = {
117     "PI",
118     "E",
119     "QP2LAMBDA",
120     0
121 };
122
123 static int hexchar2int(char c) {
124     if (c >= '0' && c <= '9') return c - '0';
125     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
126     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
127     return -1;
128 }
129
130 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
131 {
132     int *lendst = (int *)(dst + 1);
133     uint8_t *bin, *ptr;
134     int len = strlen(val);
135
136     av_freep(dst);
137     *lendst = 0;
138
139     if (len & 1)
140         return AVERROR(EINVAL);
141     len /= 2;
142
143     ptr = bin = av_malloc(len);
144     while (*val) {
145         int a = hexchar2int(*val++);
146         int b = hexchar2int(*val++);
147         if (a < 0 || b < 0) {
148             av_free(bin);
149             return AVERROR(EINVAL);
150         }
151         *ptr++ = (a << 4) | b;
152     }
153     *dst = bin;
154     *lendst = len;
155
156     return 0;
157 }
158
159 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
160 {
161     av_freep(dst);
162     *dst = av_strdup(val);
163     return 0;
164 }
165
166 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
167 {
168     int ret = 0, notfirst = 0;
169     for (;;) {
170         int i, den = 1;
171         char buf[256];
172         int cmd = 0;
173         double d, num = 1;
174         int64_t intnum = 1;
175
176         if (*val == '+' || *val == '-')
177             cmd = *(val++);
178
179         for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
180             buf[i] = val[i];
181         buf[i] = 0;
182
183         {
184             const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
185             if (o_named && o_named->type == AV_OPT_TYPE_CONST)
186                 d = o_named->default_val.dbl;
187             else if (!strcmp(buf, "default")) d = o->default_val.dbl;
188             else if (!strcmp(buf, "max"    )) d = o->max;
189             else if (!strcmp(buf, "min"    )) d = o->min;
190             else if (!strcmp(buf, "none"   )) d = 0;
191             else if (!strcmp(buf, "all"    )) d = ~0;
192             else {
193                 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
194                 if (res < 0) {
195                     av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
196                     return res;
197                 }
198             }
199         }
200         if (o->type == AV_OPT_TYPE_FLAGS) {
201             read_number(o, dst, NULL, NULL, &intnum);
202             if      (cmd == '+') d = intnum | (int64_t)d;
203             else if (cmd == '-') d = intnum &~(int64_t)d;
204         } else {
205             read_number(o, dst, &num, &den, &intnum);
206             if      (cmd == '+') d = notfirst*num*intnum/den + d;
207             else if (cmd == '-') d = notfirst*num*intnum/den - d;
208         }
209
210         if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
211             return ret;
212         val += i;
213         if (!*val)
214             return 0;
215         notfirst = 1;
216     }
217
218     return 0;
219 }
220
221 #if FF_API_OLD_AVOPTIONS
222 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
223 {
224     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
225     if (o_out)
226         *o_out = o;
227     return av_opt_set(obj, name, val, 0);
228 }
229 #endif
230
231 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
232 {
233     int ret;
234     void *dst, *target_obj;
235     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
236     if (!o || !target_obj)
237         return AVERROR_OPTION_NOT_FOUND;
238     if (!val && o->type != AV_OPT_TYPE_STRING)
239         return AVERROR(EINVAL);
240
241     dst = ((uint8_t*)target_obj) + o->offset;
242     switch (o->type) {
243     case AV_OPT_TYPE_STRING:   return set_string(obj, o, val, dst);
244     case AV_OPT_TYPE_BINARY:   return set_string_binary(obj, o, val, dst);
245     case AV_OPT_TYPE_FLAGS:
246     case AV_OPT_TYPE_INT:
247     case AV_OPT_TYPE_INT64:
248     case AV_OPT_TYPE_FLOAT:
249     case AV_OPT_TYPE_DOUBLE:
250     case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
251     case AV_OPT_TYPE_IMAGE_SIZE:
252         ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
253         if (ret < 0)
254             av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
255         return ret;
256     case AV_OPT_TYPE_PIXEL_FMT:
257         ret = av_get_pix_fmt(val);
258         if (ret == PIX_FMT_NONE) {
259             char *tail;
260             ret = strtol(val, &tail, 0);
261             if (*tail || (unsigned)ret >= PIX_FMT_NB) {
262                 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
263                 return AVERROR(EINVAL);
264             }
265         }
266         *(enum PixelFormat *)dst = ret;
267         return 0;
268     }
269
270     av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
271     return AVERROR(EINVAL);
272 }
273
274 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
275     int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
276     {\
277         if (!o || o->type != opttype)\
278             return AVERROR(EINVAL);\
279         return set_string_number(obj, o, val, name ## _out);\
280     }
281
282 OPT_EVAL_NUMBER(flags,  AV_OPT_TYPE_FLAGS,    int)
283 OPT_EVAL_NUMBER(int,    AV_OPT_TYPE_INT,      int)
284 OPT_EVAL_NUMBER(int64,  AV_OPT_TYPE_INT64,    int64_t)
285 OPT_EVAL_NUMBER(float,  AV_OPT_TYPE_FLOAT,    float)
286 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE,   double)
287 OPT_EVAL_NUMBER(q,      AV_OPT_TYPE_RATIONAL, AVRational)
288
289 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
290                                   int search_flags)
291 {
292     void *dst, *target_obj;
293     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
294
295     if (!o || !target_obj)
296         return AVERROR_OPTION_NOT_FOUND;
297
298     dst = ((uint8_t*)target_obj) + o->offset;
299     return write_number(obj, o, dst, num, den, intnum);
300 }
301
302 #if FF_API_OLD_AVOPTIONS
303 const AVOption *av_set_double(void *obj, const char *name, double n)
304 {
305     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
306     if (set_number(obj, name, n, 1, 1, 0) < 0)
307         return NULL;
308     return o;
309 }
310
311 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
312 {
313     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
314     if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
315         return NULL;
316     return o;
317 }
318
319 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
320 {
321     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
322     if (set_number(obj, name, 1, 1, n, 0) < 0)
323         return NULL;
324     return o;
325 }
326 #endif
327
328 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
329 {
330     return set_number(obj, name, 1, 1, val, search_flags);
331 }
332
333 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
334 {
335     return set_number(obj, name, val, 1, 1, search_flags);
336 }
337
338 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
339 {
340     return set_number(obj, name, val.num, val.den, 1, search_flags);
341 }
342
343 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
344 {
345     void *target_obj;
346     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
347     uint8_t *ptr;
348     uint8_t **dst;
349     int *lendst;
350
351     if (!o || !target_obj)
352         return AVERROR_OPTION_NOT_FOUND;
353
354     if (o->type != AV_OPT_TYPE_BINARY)
355         return AVERROR(EINVAL);
356
357     ptr = av_malloc(len);
358     if (!ptr)
359         return AVERROR(ENOMEM);
360
361     dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
362     lendst = (int *)(dst + 1);
363
364     av_free(*dst);
365     *dst = ptr;
366     *lendst = len;
367     memcpy(ptr, val, len);
368
369     return 0;
370 }
371
372 #if FF_API_OLD_AVOPTIONS
373 /**
374  *
375  * @param buf a buffer which is used for returning non string values as strings, can be NULL
376  * @param buf_len allocated length in bytes of buf
377  */
378 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
379 {
380     const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
381     void *dst;
382     uint8_t *bin;
383     int len, i;
384     if (!o)
385         return NULL;
386     if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
387         return NULL;
388
389     dst= ((uint8_t*)obj) + o->offset;
390     if (o_out) *o_out= o;
391
392     switch (o->type) {
393     case AV_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
394     case AV_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
395     case AV_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
396     case AV_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
397     case AV_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
398     case AV_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
399     case AV_OPT_TYPE_CONST:     snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
400     case AV_OPT_TYPE_STRING:    return *(void**)dst;
401     case AV_OPT_TYPE_BINARY:
402         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
403         if (len >= (buf_len + 1)/2) return NULL;
404         bin = *(uint8_t**)dst;
405         for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
406         break;
407     default: return NULL;
408     }
409     return buf;
410 }
411 #endif
412
413 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
414 {
415     void *dst, *target_obj;
416     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
417     uint8_t *bin, buf[128];
418     int len, i, ret;
419
420     if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
421         return AVERROR_OPTION_NOT_FOUND;
422
423     dst = (uint8_t*)target_obj + o->offset;
424
425     buf[0] = 0;
426     switch (o->type) {
427     case AV_OPT_TYPE_FLAGS:     ret = snprintf(buf, sizeof(buf), "0x%08X",  *(int    *)dst);break;
428     case AV_OPT_TYPE_INT:       ret = snprintf(buf, sizeof(buf), "%d" ,     *(int    *)dst);break;
429     case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
430     case AV_OPT_TYPE_FLOAT:     ret = snprintf(buf, sizeof(buf), "%f" ,     *(float  *)dst);break;
431     case AV_OPT_TYPE_DOUBLE:    ret = snprintf(buf, sizeof(buf), "%f" ,     *(double *)dst);break;
432     case AV_OPT_TYPE_RATIONAL:  ret = snprintf(buf, sizeof(buf), "%d/%d",   ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
433     case AV_OPT_TYPE_CONST:     ret = snprintf(buf, sizeof(buf), "%f" ,     o->default_val.dbl);break;
434     case AV_OPT_TYPE_STRING:
435         if (*(uint8_t**)dst)
436             *out_val = av_strdup(*(uint8_t**)dst);
437         else
438             *out_val = av_strdup("");
439         return 0;
440     case AV_OPT_TYPE_BINARY:
441         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
442         if ((uint64_t)len*2 + 1 > INT_MAX)
443             return AVERROR(EINVAL);
444         if (!(*out_val = av_malloc(len*2 + 1)))
445             return AVERROR(ENOMEM);
446         bin = *(uint8_t**)dst;
447         for (i = 0; i < len; i++)
448             snprintf(*out_val + i*2, 3, "%02X", bin[i]);
449         return 0;
450     case AV_OPT_TYPE_IMAGE_SIZE:
451         ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
452         break;
453     case AV_OPT_TYPE_PIXEL_FMT:
454         ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum PixelFormat *)dst), "?"));
455         break;
456     default:
457         return AVERROR(EINVAL);
458     }
459
460     if (ret >= sizeof(buf))
461         return AVERROR(EINVAL);
462     *out_val = av_strdup(buf);
463     return 0;
464 }
465
466 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
467                       int search_flags)
468 {
469     void *dst, *target_obj;
470     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
471     if (!o || !target_obj)
472         goto error;
473
474     dst = ((uint8_t*)target_obj) + o->offset;
475
476     if (o_out) *o_out= o;
477
478     return read_number(o, dst, num, den, intnum);
479
480 error:
481     *den=*intnum=0;
482     return -1;
483 }
484
485 #if FF_API_OLD_AVOPTIONS
486 double av_get_double(void *obj, const char *name, const AVOption **o_out)
487 {
488     int64_t intnum=1;
489     double num=1;
490     int den=1;
491
492     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
493         return NAN;
494     return num*intnum/den;
495 }
496
497 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
498 {
499     int64_t intnum=1;
500     double num=1;
501     int den=1;
502
503     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
504         return (AVRational){0, 0};
505     if (num == 1.0 && (int)intnum == intnum)
506         return (AVRational){intnum, den};
507     else
508         return av_d2q(num*intnum/den, 1<<24);
509 }
510
511 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
512 {
513     int64_t intnum=1;
514     double num=1;
515     int den=1;
516
517     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
518         return -1;
519     return num*intnum/den;
520 }
521 #endif
522
523 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
524 {
525     int64_t intnum = 1;
526     double     num = 1;
527     int   ret, den = 1;
528
529     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
530         return ret;
531     *out_val = num*intnum/den;
532     return 0;
533 }
534
535 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
536 {
537     int64_t intnum = 1;
538     double     num = 1;
539     int   ret, den = 1;
540
541     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
542         return ret;
543     *out_val = num*intnum/den;
544     return 0;
545 }
546
547 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
548 {
549     int64_t intnum = 1;
550     double     num = 1;
551     int   ret, den = 1;
552
553     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
554         return ret;
555
556     if (num == 1.0 && (int)intnum == intnum)
557         *out_val = (AVRational){intnum, den};
558     else
559         *out_val = av_d2q(num*intnum/den, 1<<24);
560     return 0;
561 }
562
563 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
564 {
565     const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
566     const AVOption *flag  = av_opt_find(obj, flag_name,
567                                         field ? field->unit : NULL, 0, 0);
568     int64_t res;
569
570     if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
571         av_opt_get_int(obj, field_name, 0, &res) < 0)
572         return 0;
573     return res & (int) flag->default_val.dbl;
574 }
575
576 static void opt_list(void *obj, void *av_log_obj, const char *unit,
577                      int req_flags, int rej_flags)
578 {
579     const AVOption *opt=NULL;
580
581     while ((opt = av_opt_next(obj, opt))) {
582         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
583             continue;
584
585         /* Don't print CONST's on level one.
586          * Don't print anything but CONST's on level two.
587          * Only print items from the requested unit.
588          */
589         if (!unit && opt->type==AV_OPT_TYPE_CONST)
590             continue;
591         else if (unit && opt->type!=AV_OPT_TYPE_CONST)
592             continue;
593         else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
594             continue;
595         else if (unit && opt->type == AV_OPT_TYPE_CONST)
596             av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
597         else
598             av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
599
600         switch (opt->type) {
601             case AV_OPT_TYPE_FLAGS:
602                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
603                 break;
604             case AV_OPT_TYPE_INT:
605                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
606                 break;
607             case AV_OPT_TYPE_INT64:
608                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
609                 break;
610             case AV_OPT_TYPE_DOUBLE:
611                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
612                 break;
613             case AV_OPT_TYPE_FLOAT:
614                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
615                 break;
616             case AV_OPT_TYPE_STRING:
617                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
618                 break;
619             case AV_OPT_TYPE_RATIONAL:
620                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
621                 break;
622             case AV_OPT_TYPE_BINARY:
623                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
624                 break;
625             case AV_OPT_TYPE_IMAGE_SIZE:
626                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
627                 break;
628             case AV_OPT_TYPE_PIXEL_FMT:
629                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<pix_fmt>");
630                 break;
631             case AV_OPT_TYPE_CONST:
632             default:
633                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
634                 break;
635         }
636         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
637         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
638         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
639         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
640         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
641         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
642
643         if (opt->help)
644             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
645         av_log(av_log_obj, AV_LOG_INFO, "\n");
646         if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
647             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
648         }
649     }
650 }
651
652 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
653 {
654     if (!obj)
655         return -1;
656
657     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
658
659     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
660
661     return 0;
662 }
663
664 void av_opt_set_defaults(void *s)
665 {
666 #if FF_API_OLD_AVOPTIONS
667     av_opt_set_defaults2(s, 0, 0);
668 }
669
670 void av_opt_set_defaults2(void *s, int mask, int flags)
671 {
672 #endif
673     const AVOption *opt = NULL;
674     while ((opt = av_opt_next(s, opt)) != NULL) {
675 #if FF_API_OLD_AVOPTIONS
676         if ((opt->flags & mask) != flags)
677             continue;
678 #endif
679         switch (opt->type) {
680             case AV_OPT_TYPE_CONST:
681                 /* Nothing to be done here */
682             break;
683             case AV_OPT_TYPE_FLAGS:
684             case AV_OPT_TYPE_INT: {
685                 int val;
686                 val = opt->default_val.dbl;
687                 av_opt_set_int(s, opt->name, val, 0);
688             }
689             break;
690             case AV_OPT_TYPE_INT64:
691                 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
692                     av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
693                 av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
694             break;
695             case AV_OPT_TYPE_DOUBLE:
696             case AV_OPT_TYPE_FLOAT: {
697                 double val;
698                 val = opt->default_val.dbl;
699                 av_opt_set_double(s, opt->name, val, 0);
700             }
701             break;
702             case AV_OPT_TYPE_RATIONAL: {
703                 AVRational val;
704                 val = av_d2q(opt->default_val.dbl, INT_MAX);
705                 av_opt_set_q(s, opt->name, val, 0);
706             }
707             break;
708             case AV_OPT_TYPE_STRING:
709             case AV_OPT_TYPE_IMAGE_SIZE:
710             case AV_OPT_TYPE_PIXEL_FMT:
711                 av_opt_set(s, opt->name, opt->default_val.str, 0);
712                 break;
713             case AV_OPT_TYPE_BINARY:
714                 /* Cannot set default for binary */
715             break;
716             default:
717                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
718         }
719     }
720 }
721
722 /**
723  * Store the value in the field in ctx that is named like key.
724  * ctx must be an AVClass context, storing is done using AVOptions.
725  *
726  * @param buf the string to parse, buf will be updated to point at the
727  * separator just after the parsed key/value pair
728  * @param key_val_sep a 0-terminated list of characters used to
729  * separate key from value
730  * @param pairs_sep a 0-terminated list of characters used to separate
731  * two pairs from each other
732  * @return 0 if the key/value pair has been successfully parsed and
733  * set, or a negative value corresponding to an AVERROR code in case
734  * of error:
735  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
736  * the error code issued by av_opt_set() if the key/value pair
737  * cannot be set
738  */
739 static int parse_key_value_pair(void *ctx, const char **buf,
740                                 const char *key_val_sep, const char *pairs_sep)
741 {
742     char *key = av_get_token(buf, key_val_sep);
743     char *val;
744     int ret;
745
746     if (*key && strspn(*buf, key_val_sep)) {
747         (*buf)++;
748         val = av_get_token(buf, pairs_sep);
749     } else {
750         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
751         av_free(key);
752         return AVERROR(EINVAL);
753     }
754
755     av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
756
757     ret = av_opt_set(ctx, key, val, 0);
758     if (ret == AVERROR_OPTION_NOT_FOUND)
759         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
760
761     av_free(key);
762     av_free(val);
763     return ret;
764 }
765
766 int av_set_options_string(void *ctx, const char *opts,
767                           const char *key_val_sep, const char *pairs_sep)
768 {
769     int ret, count = 0;
770
771     if (!opts)
772         return 0;
773
774     while (*opts) {
775         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
776             return ret;
777         count++;
778
779         if (*opts)
780             opts++;
781     }
782
783     return count;
784 }
785
786 void av_opt_free(void *obj)
787 {
788     const AVOption *o = NULL;
789     while ((o = av_opt_next(obj, o)))
790         if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
791             av_freep((uint8_t *)obj + o->offset);
792 }
793
794 int av_opt_set_dict(void *obj, AVDictionary **options)
795 {
796     AVDictionaryEntry *t = NULL;
797     AVDictionary    *tmp = NULL;
798     int ret = 0;
799
800     while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
801         ret = av_opt_set(obj, t->key, t->value, 0);
802         if (ret == AVERROR_OPTION_NOT_FOUND)
803             av_dict_set(&tmp, t->key, t->value, 0);
804         else if (ret < 0) {
805             av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
806             break;
807         }
808         ret = 0;
809     }
810     av_dict_free(options);
811     *options = tmp;
812     return ret;
813 }
814
815 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
816                             int opt_flags, int search_flags)
817 {
818     return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
819 }
820
821 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
822                              int opt_flags, int search_flags, void **target_obj)
823 {
824     const AVClass  *c;
825     const AVOption *o = NULL;
826
827     if(!obj)
828         return NULL;
829
830     c= *(AVClass**)obj;
831
832     if (search_flags & AV_OPT_SEARCH_CHILDREN) {
833         if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
834             const AVClass *child = NULL;
835             while (child = av_opt_child_class_next(c, child))
836                 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
837                     return o;
838         } else {
839             void *child = NULL;
840             while (child = av_opt_child_next(obj, child))
841                 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
842                     return o;
843         }
844     }
845
846     while (o = av_opt_next(obj, o)) {
847         if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
848             ((!unit && o->type != AV_OPT_TYPE_CONST) ||
849              (unit  && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
850             if (target_obj) {
851                 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
852                     *target_obj = obj;
853                 else
854                     *target_obj = NULL;
855             }
856             return o;
857         }
858     }
859     return NULL;
860 }
861
862 void *av_opt_child_next(void *obj, void *prev)
863 {
864     const AVClass *c = *(AVClass**)obj;
865     if (c->child_next)
866         return c->child_next(obj, prev);
867     return NULL;
868 }
869
870 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
871 {
872     if (parent->child_class_next)
873         return parent->child_class_next(prev);
874     return NULL;
875 }
876
877 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
878 {
879     const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
880     if(!opt)
881         return NULL;
882     return (uint8_t*)obj + opt->offset;
883 }
884
885 #ifdef TEST
886
887 #undef printf
888
889 typedef struct TestContext
890 {
891     const AVClass *class;
892     int num;
893     int toggle;
894     char *string;
895     int flags;
896     AVRational rational;
897     int w, h;
898     enum PixelFormat pix_fmt;
899 } TestContext;
900
901 #define OFFSET(x) offsetof(TestContext, x)
902
903 #define TEST_FLAG_COOL 01
904 #define TEST_FLAG_LAME 02
905 #define TEST_FLAG_MU   04
906
907 static const AVOption test_options[]= {
908 {"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {0},              0,        100                 },
909 {"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {0},              0,        1                   },
910 {"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0},              0,        10                  },
911 {"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {0},              CHAR_MIN, CHAR_MAX            },
912 {"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {0},              0,        INT_MAX, 0, "flags" },
913 {"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
914 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
915 {"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
916 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
917 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT,{0},              0,        0                   },
918 {NULL},
919 };
920
921 static const char *test_get_name(void *ctx)
922 {
923     return "test";
924 }
925
926 static const AVClass test_class = {
927     "TestContext",
928     test_get_name,
929     test_options
930 };
931
932 int main(void)
933 {
934     int i;
935
936     printf("\nTesting av_set_options_string()\n");
937     {
938         TestContext test_ctx = { 0 };
939         const char *options[] = {
940             "",
941             ":",
942             "=",
943             "foo=:",
944             ":=foo",
945             "=foo",
946             "foo=",
947             "foo",
948             "foo=val",
949             "foo==val",
950             "toggle=:",
951             "string=:",
952             "toggle=1 : foo",
953             "toggle=100",
954             "toggle==1",
955             "flags=+mu-lame : num=42: toggle=0",
956             "num=42 : string=blahblah",
957             "rational=0 : rational=1/2 : rational=1/-1",
958             "rational=-1/0",
959             "size=1024x768",
960             "size=pal",
961             "size=bogus",
962             "pix_fmt=yuv420p",
963             "pix_fmt=2",
964             "pix_fmt=bogus",
965         };
966
967         test_ctx.class = &test_class;
968         av_opt_set_defaults(&test_ctx);
969         test_ctx.string = av_strdup("default");
970
971         av_log_set_level(AV_LOG_DEBUG);
972
973         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
974             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
975             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
976                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
977             printf("\n");
978         }
979         av_freep(&test_ctx.string);
980     }
981
982     return 0;
983 }
984
985 #endif