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