]> 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 #if FF_API_OLD_AVOPTIONS
327 /**
328  *
329  * @param buf a buffer which is used for returning non string values as strings, can be NULL
330  * @param buf_len allocated length in bytes of buf
331  */
332 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
333 {
334     const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
335     void *dst;
336     uint8_t *bin;
337     int len, i;
338     if (!o)
339         return NULL;
340     if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
341         return NULL;
342
343     dst= ((uint8_t*)obj) + o->offset;
344     if (o_out) *o_out= o;
345
346     switch (o->type) {
347     case AV_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
348     case AV_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
349     case AV_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
350     case AV_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
351     case AV_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
352     case AV_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
353     case AV_OPT_TYPE_CONST:     snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
354     case AV_OPT_TYPE_STRING:    return *(void**)dst;
355     case AV_OPT_TYPE_BINARY:
356         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
357         if (len >= (buf_len + 1)/2) return NULL;
358         bin = *(uint8_t**)dst;
359         for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
360         break;
361     default: return NULL;
362     }
363     return buf;
364 }
365 #endif
366
367 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
368 {
369     void *dst, *target_obj;
370     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
371     uint8_t *bin, buf[128];
372     int len, i, ret;
373
374     if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
375         return AVERROR_OPTION_NOT_FOUND;
376
377     dst = (uint8_t*)target_obj + o->offset;
378
379     buf[0] = 0;
380     switch (o->type) {
381     case AV_OPT_TYPE_FLAGS:     ret = snprintf(buf, sizeof(buf), "0x%08X",  *(int    *)dst);break;
382     case AV_OPT_TYPE_INT:       ret = snprintf(buf, sizeof(buf), "%d" ,     *(int    *)dst);break;
383     case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
384     case AV_OPT_TYPE_FLOAT:     ret = snprintf(buf, sizeof(buf), "%f" ,     *(float  *)dst);break;
385     case AV_OPT_TYPE_DOUBLE:    ret = snprintf(buf, sizeof(buf), "%f" ,     *(double *)dst);break;
386     case AV_OPT_TYPE_RATIONAL:  ret = snprintf(buf, sizeof(buf), "%d/%d",   ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
387     case AV_OPT_TYPE_CONST:     ret = snprintf(buf, sizeof(buf), "%f" ,     o->default_val.dbl);break;
388     case AV_OPT_TYPE_STRING:
389         if (*(uint8_t**)dst)
390             *out_val = av_strdup(*(uint8_t**)dst);
391         else
392             *out_val = av_strdup("");
393         return 0;
394     case AV_OPT_TYPE_BINARY:
395         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
396         if ((uint64_t)len*2 + 1 > INT_MAX)
397             return AVERROR(EINVAL);
398         if (!(*out_val = av_malloc(len*2 + 1)))
399             return AVERROR(ENOMEM);
400         bin = *(uint8_t**)dst;
401         for (i = 0; i < len; i++)
402             snprintf(*out_val + i*2, 3, "%02X", bin[i]);
403         return 0;
404     case AV_OPT_TYPE_IMAGE_SIZE:
405         ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
406         break;
407     default:
408         return AVERROR(EINVAL);
409     }
410
411     if (ret >= sizeof(buf))
412         return AVERROR(EINVAL);
413     *out_val = av_strdup(buf);
414     return 0;
415 }
416
417 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
418                       int search_flags)
419 {
420     void *dst, *target_obj;
421     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
422     if (!o || !target_obj)
423         goto error;
424
425     dst = ((uint8_t*)target_obj) + o->offset;
426
427     if (o_out) *o_out= o;
428
429     return read_number(o, dst, num, den, intnum);
430
431 error:
432     *den=*intnum=0;
433     return -1;
434 }
435
436 #if FF_API_OLD_AVOPTIONS
437 double av_get_double(void *obj, const char *name, const AVOption **o_out)
438 {
439     int64_t intnum=1;
440     double num=1;
441     int den=1;
442
443     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
444         return NAN;
445     return num*intnum/den;
446 }
447
448 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
449 {
450     int64_t intnum=1;
451     double num=1;
452     int den=1;
453
454     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
455         return (AVRational){0, 0};
456     if (num == 1.0 && (int)intnum == intnum)
457         return (AVRational){intnum, den};
458     else
459         return av_d2q(num*intnum/den, 1<<24);
460 }
461
462 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
463 {
464     int64_t intnum=1;
465     double num=1;
466     int den=1;
467
468     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
469         return -1;
470     return num*intnum/den;
471 }
472 #endif
473
474 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
475 {
476     int64_t intnum = 1;
477     double     num = 1;
478     int   ret, den = 1;
479
480     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
481         return ret;
482     *out_val = num*intnum/den;
483     return 0;
484 }
485
486 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
487 {
488     int64_t intnum = 1;
489     double     num = 1;
490     int   ret, den = 1;
491
492     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
493         return ret;
494     *out_val = num*intnum/den;
495     return 0;
496 }
497
498 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
499 {
500     int64_t intnum = 1;
501     double     num = 1;
502     int   ret, den = 1;
503
504     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
505         return ret;
506
507     if (num == 1.0 && (int)intnum == intnum)
508         *out_val = (AVRational){intnum, den};
509     else
510         *out_val = av_d2q(num*intnum/den, 1<<24);
511     return 0;
512 }
513
514 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
515 {
516     const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
517     const AVOption *flag  = av_opt_find(obj, flag_name,
518                                         field ? field->unit : NULL, 0, 0);
519     int64_t res;
520
521     if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
522         av_opt_get_int(obj, field_name, 0, &res) < 0)
523         return 0;
524     return res & (int) flag->default_val.dbl;
525 }
526
527 static void opt_list(void *obj, void *av_log_obj, const char *unit,
528                      int req_flags, int rej_flags)
529 {
530     const AVOption *opt=NULL;
531
532     while ((opt = av_opt_next(obj, opt))) {
533         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
534             continue;
535
536         /* Don't print CONST's on level one.
537          * Don't print anything but CONST's on level two.
538          * Only print items from the requested unit.
539          */
540         if (!unit && opt->type==AV_OPT_TYPE_CONST)
541             continue;
542         else if (unit && opt->type!=AV_OPT_TYPE_CONST)
543             continue;
544         else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
545             continue;
546         else if (unit && opt->type == AV_OPT_TYPE_CONST)
547             av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
548         else
549             av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
550
551         switch (opt->type) {
552             case AV_OPT_TYPE_FLAGS:
553                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
554                 break;
555             case AV_OPT_TYPE_INT:
556                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
557                 break;
558             case AV_OPT_TYPE_INT64:
559                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
560                 break;
561             case AV_OPT_TYPE_DOUBLE:
562                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
563                 break;
564             case AV_OPT_TYPE_FLOAT:
565                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
566                 break;
567             case AV_OPT_TYPE_STRING:
568                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
569                 break;
570             case AV_OPT_TYPE_RATIONAL:
571                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
572                 break;
573             case AV_OPT_TYPE_BINARY:
574                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
575                 break;
576             case AV_OPT_TYPE_IMAGE_SIZE:
577                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
578                 break;
579             case AV_OPT_TYPE_CONST:
580             default:
581                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
582                 break;
583         }
584         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
585         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
586         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
587         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
588         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
589
590         if (opt->help)
591             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
592         av_log(av_log_obj, AV_LOG_INFO, "\n");
593         if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
594             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
595         }
596     }
597 }
598
599 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
600 {
601     if (!obj)
602         return -1;
603
604     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
605
606     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
607
608     return 0;
609 }
610
611 void av_opt_set_defaults(void *s)
612 {
613 #if FF_API_OLD_AVOPTIONS
614     av_opt_set_defaults2(s, 0, 0);
615 }
616
617 void av_opt_set_defaults2(void *s, int mask, int flags)
618 {
619 #endif
620     const AVOption *opt = NULL;
621     while ((opt = av_opt_next(s, opt)) != NULL) {
622 #if FF_API_OLD_AVOPTIONS
623         if ((opt->flags & mask) != flags)
624             continue;
625 #endif
626         switch (opt->type) {
627             case AV_OPT_TYPE_CONST:
628                 /* Nothing to be done here */
629             break;
630             case AV_OPT_TYPE_FLAGS:
631             case AV_OPT_TYPE_INT: {
632                 int val;
633                 val = opt->default_val.dbl;
634                 av_opt_set_int(s, opt->name, val, 0);
635             }
636             break;
637             case AV_OPT_TYPE_INT64:
638                 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
639                     av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
640                 av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
641             break;
642             case AV_OPT_TYPE_DOUBLE:
643             case AV_OPT_TYPE_FLOAT: {
644                 double val;
645                 val = opt->default_val.dbl;
646                 av_opt_set_double(s, opt->name, val, 0);
647             }
648             break;
649             case AV_OPT_TYPE_RATIONAL: {
650                 AVRational val;
651                 val = av_d2q(opt->default_val.dbl, INT_MAX);
652                 av_opt_set_q(s, opt->name, val, 0);
653             }
654             break;
655             case AV_OPT_TYPE_STRING:
656             case AV_OPT_TYPE_IMAGE_SIZE:
657                 av_opt_set(s, opt->name, opt->default_val.str, 0);
658                 break;
659             case AV_OPT_TYPE_BINARY:
660                 /* Cannot set default for binary */
661             break;
662             default:
663                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
664         }
665     }
666 }
667
668 /**
669  * Store the value in the field in ctx that is named like key.
670  * ctx must be an AVClass context, storing is done using AVOptions.
671  *
672  * @param buf the string to parse, buf will be updated to point at the
673  * separator just after the parsed key/value pair
674  * @param key_val_sep a 0-terminated list of characters used to
675  * separate key from value
676  * @param pairs_sep a 0-terminated list of characters used to separate
677  * two pairs from each other
678  * @return 0 if the key/value pair has been successfully parsed and
679  * set, or a negative value corresponding to an AVERROR code in case
680  * of error:
681  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
682  * the error code issued by av_opt_set() if the key/value pair
683  * cannot be set
684  */
685 static int parse_key_value_pair(void *ctx, const char **buf,
686                                 const char *key_val_sep, const char *pairs_sep)
687 {
688     char *key = av_get_token(buf, key_val_sep);
689     char *val;
690     int ret;
691
692     if (*key && strspn(*buf, key_val_sep)) {
693         (*buf)++;
694         val = av_get_token(buf, pairs_sep);
695     } else {
696         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
697         av_free(key);
698         return AVERROR(EINVAL);
699     }
700
701     av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
702
703     ret = av_opt_set(ctx, key, val, 0);
704     if (ret == AVERROR_OPTION_NOT_FOUND)
705         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
706
707     av_free(key);
708     av_free(val);
709     return ret;
710 }
711
712 int av_set_options_string(void *ctx, const char *opts,
713                           const char *key_val_sep, const char *pairs_sep)
714 {
715     int ret, count = 0;
716
717     if (!opts)
718         return 0;
719
720     while (*opts) {
721         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
722             return ret;
723         count++;
724
725         if (*opts)
726             opts++;
727     }
728
729     return count;
730 }
731
732 void av_opt_free(void *obj)
733 {
734     const AVOption *o = NULL;
735     while ((o = av_opt_next(obj, o)))
736         if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
737             av_freep((uint8_t *)obj + o->offset);
738 }
739
740 int av_opt_set_dict(void *obj, AVDictionary **options)
741 {
742     AVDictionaryEntry *t = NULL;
743     AVDictionary    *tmp = NULL;
744     int ret = 0;
745
746     while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
747         ret = av_opt_set(obj, t->key, t->value, 0);
748         if (ret == AVERROR_OPTION_NOT_FOUND)
749             av_dict_set(&tmp, t->key, t->value, 0);
750         else if (ret < 0) {
751             av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
752             break;
753         }
754         ret = 0;
755     }
756     av_dict_free(options);
757     *options = tmp;
758     return ret;
759 }
760
761 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
762                             int opt_flags, int search_flags)
763 {
764     return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
765 }
766
767 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
768                              int opt_flags, int search_flags, void **target_obj)
769 {
770     const AVClass  *c;
771     const AVOption *o = NULL;
772
773     if(!obj)
774         return NULL;
775
776     c= *(AVClass**)obj;
777
778     if (search_flags & AV_OPT_SEARCH_CHILDREN) {
779         if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
780             const AVClass *child = NULL;
781             while (child = av_opt_child_class_next(c, child))
782                 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
783                     return o;
784         } else {
785             void *child = NULL;
786             while (child = av_opt_child_next(obj, child))
787                 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
788                     return o;
789         }
790     }
791
792     while (o = av_opt_next(obj, o)) {
793         if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
794             ((!unit && o->type != AV_OPT_TYPE_CONST) ||
795              (unit  && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
796             if (target_obj) {
797                 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
798                     *target_obj = obj;
799                 else
800                     *target_obj = NULL;
801             }
802             return o;
803         }
804     }
805     return NULL;
806 }
807
808 void *av_opt_child_next(void *obj, void *prev)
809 {
810     const AVClass *c = *(AVClass**)obj;
811     if (c->child_next)
812         return c->child_next(obj, prev);
813     return NULL;
814 }
815
816 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
817 {
818     if (parent->child_class_next)
819         return parent->child_class_next(prev);
820     return NULL;
821 }
822
823 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
824 {
825     const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
826     if(!opt)
827         return NULL;
828     return (uint8_t*)obj + opt->offset;
829 }
830
831 #ifdef TEST
832
833 #undef printf
834
835 typedef struct TestContext
836 {
837     const AVClass *class;
838     int num;
839     int toggle;
840     char *string;
841     int flags;
842     AVRational rational;
843 } TestContext;
844
845 #define OFFSET(x) offsetof(TestContext, x)
846
847 #define TEST_FLAG_COOL 01
848 #define TEST_FLAG_LAME 02
849 #define TEST_FLAG_MU   04
850
851 static const AVOption test_options[]= {
852 {"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {0},              0,        100                 },
853 {"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {0},              0,        1                   },
854 {"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0},              0,        10                  },
855 {"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {0},              CHAR_MIN, CHAR_MAX            },
856 {"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {0},              0,        INT_MAX, 0, "flags" },
857 {"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
858 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
859 {"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
860 {NULL},
861 };
862
863 static const char *test_get_name(void *ctx)
864 {
865     return "test";
866 }
867
868 static const AVClass test_class = {
869     "TestContext",
870     test_get_name,
871     test_options
872 };
873
874 int main(void)
875 {
876     int i;
877
878     printf("\nTesting av_set_options_string()\n");
879     {
880         TestContext test_ctx;
881         const char *options[] = {
882             "",
883             ":",
884             "=",
885             "foo=:",
886             ":=foo",
887             "=foo",
888             "foo=",
889             "foo",
890             "foo=val",
891             "foo==val",
892             "toggle=:",
893             "string=:",
894             "toggle=1 : foo",
895             "toggle=100",
896             "toggle==1",
897             "flags=+mu-lame : num=42: toggle=0",
898             "num=42 : string=blahblah",
899             "rational=0 : rational=1/2 : rational=1/-1",
900             "rational=-1/0",
901         };
902
903         test_ctx.class = &test_class;
904         av_opt_set_defaults(&test_ctx);
905         test_ctx.string = av_strdup("default");
906
907         av_log_set_level(AV_LOG_DEBUG);
908
909         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
910             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
911             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
912                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
913             printf("\n");
914         }
915     }
916
917     return 0;
918 }
919
920 #endif