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