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