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