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