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