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