]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.c
Merge commit '2b8dd371e4d276ca0d342e82b8b4cc281be0630a'
[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 "common.h"
31 #include "opt.h"
32 #include "eval.h"
33 #include "dict.h"
34 #include "log.h"
35 #include "parseutils.h"
36 #include "pixdesc.h"
37 #include "mathematics.h"
38
39 #if FF_API_FIND_OPT
40 //FIXME order them and do a bin search
41 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
42 {
43     const AVOption *o = NULL;
44
45     while ((o = av_next_option(v, o))) {
46         if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
47             return o;
48     }
49     return NULL;
50 }
51 #endif
52
53 #if FF_API_OLD_AVOPTIONS
54 const AVOption *av_next_option(void *obj, const AVOption *last)
55 {
56     return av_opt_next(obj, last);
57 }
58 #endif
59
60 const AVOption *av_opt_next(void *obj, const AVOption *last)
61 {
62     AVClass *class = *(AVClass**)obj;
63     if (!last && class->option && class->option[0].name)
64         return class->option;
65     if (last && last[1].name)           return ++last;
66     return NULL;
67 }
68
69 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
70 {
71     switch (o->type) {
72     case AV_OPT_TYPE_FLAGS:     *intnum = *(unsigned int*)dst;return 0;
73     case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
74     case AV_OPT_TYPE_INT64:     *intnum = *(int64_t     *)dst;return 0;
75     case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
76     case AV_OPT_TYPE_DOUBLE:    *num    = *(double      *)dst;return 0;
77     case AV_OPT_TYPE_RATIONAL:  *intnum = ((AVRational*)dst)->num;
78                                 *den    = ((AVRational*)dst)->den;
79                                                         return 0;
80     case AV_OPT_TYPE_CONST:     *num    = o->default_val.dbl; return 0;
81     }
82     return AVERROR(EINVAL);
83 }
84
85 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
86 {
87     if (o->max*den < num*intnum || o->min*den > num*intnum) {
88         av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
89                num*intnum/den, o->name);
90         return AVERROR(ERANGE);
91     }
92
93     switch (o->type) {
94     case AV_OPT_TYPE_FLAGS:
95     case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
96     case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
97     case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
98     case AV_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
99     case AV_OPT_TYPE_RATIONAL:
100         if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
101         else                 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
102         break;
103     default:
104         return AVERROR(EINVAL);
105     }
106     return 0;
107 }
108
109 static const double const_values[] = {
110     M_PI,
111     M_E,
112     FF_QP2LAMBDA,
113     0
114 };
115
116 static const char * const const_names[] = {
117     "PI",
118     "E",
119     "QP2LAMBDA",
120     0
121 };
122
123 static int hexchar2int(char c) {
124     if (c >= '0' && c <= '9') return c - '0';
125     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
126     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
127     return -1;
128 }
129
130 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
131 {
132     int *lendst = (int *)(dst + 1);
133     uint8_t *bin, *ptr;
134     int len = strlen(val);
135
136     av_freep(dst);
137     *lendst = 0;
138
139     if (len & 1)
140         return AVERROR(EINVAL);
141     len /= 2;
142
143     ptr = bin = av_malloc(len);
144     while (*val) {
145         int a = hexchar2int(*val++);
146         int b = hexchar2int(*val++);
147         if (a < 0 || b < 0) {
148             av_free(bin);
149             return AVERROR(EINVAL);
150         }
151         *ptr++ = (a << 4) | b;
152     }
153     *dst = bin;
154     *lendst = len;
155
156     return 0;
157 }
158
159 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
160 {
161     av_freep(dst);
162     *dst = av_strdup(val);
163     return 0;
164 }
165
166 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
167                               opt->type == AV_OPT_TYPE_CONST || \
168                               opt->type == AV_OPT_TYPE_FLAGS || \
169                               opt->type == AV_OPT_TYPE_INT) ? \
170                              opt->default_val.i64 : opt->default_val.dbl)
171
172 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
173 {
174     int ret = 0, notfirst = 0;
175     for (;;) {
176         int i, den = 1;
177         char buf[256];
178         int cmd = 0;
179         double d, num = 1;
180         int64_t intnum = 1;
181
182         if (*val == '+' || *val == '-')
183             cmd = *(val++);
184
185         for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
186             buf[i] = val[i];
187         buf[i] = 0;
188
189         {
190             const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
191             if (o_named && o_named->type == AV_OPT_TYPE_CONST)
192                 d = DEFAULT_NUMVAL(o_named);
193             else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
194             else if (!strcmp(buf, "max"    )) d = o->max;
195             else if (!strcmp(buf, "min"    )) d = o->min;
196             else if (!strcmp(buf, "none"   )) d = 0;
197             else if (!strcmp(buf, "all"    )) d = ~0;
198             else {
199                 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
200                 if (res < 0) {
201                     av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
202                     return res;
203                 }
204             }
205         }
206         if (o->type == AV_OPT_TYPE_FLAGS) {
207             read_number(o, dst, NULL, NULL, &intnum);
208             if      (cmd == '+') d = intnum | (int64_t)d;
209             else if (cmd == '-') d = intnum &~(int64_t)d;
210         } else {
211             read_number(o, dst, &num, &den, &intnum);
212             if      (cmd == '+') d = notfirst*num*intnum/den + d;
213             else if (cmd == '-') d = notfirst*num*intnum/den - d;
214         }
215
216         if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
217             return ret;
218         val += i;
219         if (!*val)
220             return 0;
221         notfirst = 1;
222     }
223
224     return 0;
225 }
226
227 #if FF_API_OLD_AVOPTIONS
228 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
229 {
230     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
231     if (o_out)
232         *o_out = o;
233     return av_opt_set(obj, name, val, 0);
234 }
235 #endif
236
237 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
238 {
239     int ret;
240     void *dst, *target_obj;
241     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
242     if (!o || !target_obj)
243         return AVERROR_OPTION_NOT_FOUND;
244     if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_IMAGE_SIZE))
245         return AVERROR(EINVAL);
246
247     dst = ((uint8_t*)target_obj) + o->offset;
248     switch (o->type) {
249     case AV_OPT_TYPE_STRING:   return set_string(obj, o, val, dst);
250     case AV_OPT_TYPE_BINARY:   return set_string_binary(obj, o, val, dst);
251     case AV_OPT_TYPE_FLAGS:
252     case AV_OPT_TYPE_INT:
253     case AV_OPT_TYPE_INT64:
254     case AV_OPT_TYPE_FLOAT:
255     case AV_OPT_TYPE_DOUBLE:
256     case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
257     case AV_OPT_TYPE_IMAGE_SIZE:
258         if (!val || !strcmp(val, "none")) {
259             *(int *)dst = *((int *)dst + 1) = 0;
260             return 0;
261         }
262         ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
263         if (ret < 0)
264             av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
265         return ret;
266     case AV_OPT_TYPE_PIXEL_FMT:
267         if (!val || !strcmp(val, "none"))
268             ret = AV_PIX_FMT_NONE;
269         else {
270             ret = av_get_pix_fmt(val);
271             if (ret == AV_PIX_FMT_NONE) {
272                 char *tail;
273                 ret = strtol(val, &tail, 0);
274                 if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
275                     av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
276                     return AVERROR(EINVAL);
277                 }
278             }
279         }
280         *(enum AVPixelFormat *)dst = ret;
281         return 0;
282     }
283
284     av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
285     return AVERROR(EINVAL);
286 }
287
288 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
289     int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
290     {\
291         if (!o || o->type != opttype)\
292             return AVERROR(EINVAL);\
293         return set_string_number(obj, o, val, name ## _out);\
294     }
295
296 OPT_EVAL_NUMBER(flags,  AV_OPT_TYPE_FLAGS,    int)
297 OPT_EVAL_NUMBER(int,    AV_OPT_TYPE_INT,      int)
298 OPT_EVAL_NUMBER(int64,  AV_OPT_TYPE_INT64,    int64_t)
299 OPT_EVAL_NUMBER(float,  AV_OPT_TYPE_FLOAT,    float)
300 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE,   double)
301 OPT_EVAL_NUMBER(q,      AV_OPT_TYPE_RATIONAL, AVRational)
302
303 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
304                                   int search_flags)
305 {
306     void *dst, *target_obj;
307     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
308
309     if (!o || !target_obj)
310         return AVERROR_OPTION_NOT_FOUND;
311
312     dst = ((uint8_t*)target_obj) + o->offset;
313     return write_number(obj, o, dst, num, den, intnum);
314 }
315
316 #if FF_API_OLD_AVOPTIONS
317 const AVOption *av_set_double(void *obj, const char *name, double n)
318 {
319     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
320     if (set_number(obj, name, n, 1, 1, 0) < 0)
321         return NULL;
322     return o;
323 }
324
325 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
326 {
327     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
328     if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
329         return NULL;
330     return o;
331 }
332
333 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
334 {
335     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
336     if (set_number(obj, name, 1, 1, n, 0) < 0)
337         return NULL;
338     return o;
339 }
340 #endif
341
342 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
343 {
344     return set_number(obj, name, 1, 1, val, search_flags);
345 }
346
347 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
348 {
349     return set_number(obj, name, val, 1, 1, search_flags);
350 }
351
352 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
353 {
354     return set_number(obj, name, val.num, val.den, 1, search_flags);
355 }
356
357 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
358 {
359     void *target_obj;
360     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
361     uint8_t *ptr;
362     uint8_t **dst;
363     int *lendst;
364
365     if (!o || !target_obj)
366         return AVERROR_OPTION_NOT_FOUND;
367
368     if (o->type != AV_OPT_TYPE_BINARY)
369         return AVERROR(EINVAL);
370
371     ptr = av_malloc(len);
372     if (!ptr)
373         return AVERROR(ENOMEM);
374
375     dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
376     lendst = (int *)(dst + 1);
377
378     av_free(*dst);
379     *dst = ptr;
380     *lendst = len;
381     memcpy(ptr, val, len);
382
383     return 0;
384 }
385
386 #if FF_API_OLD_AVOPTIONS
387 /**
388  *
389  * @param buf a buffer which is used for returning non string values as strings, can be NULL
390  * @param buf_len allocated length in bytes of buf
391  */
392 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
393 {
394     const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
395     void *dst;
396     uint8_t *bin;
397     int len, i;
398     if (!o)
399         return NULL;
400     if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
401         return NULL;
402
403     dst= ((uint8_t*)obj) + o->offset;
404     if (o_out) *o_out= o;
405
406     switch (o->type) {
407     case AV_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
408     case AV_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
409     case AV_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
410     case AV_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
411     case AV_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
412     case AV_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
413     case AV_OPT_TYPE_CONST:     snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
414     case AV_OPT_TYPE_STRING:    return *(void**)dst;
415     case AV_OPT_TYPE_BINARY:
416         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
417         if (len >= (buf_len + 1)/2) return NULL;
418         bin = *(uint8_t**)dst;
419         for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
420         break;
421     default: return NULL;
422     }
423     return buf;
424 }
425 #endif
426
427 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
428 {
429     void *dst, *target_obj;
430     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
431     uint8_t *bin, buf[128];
432     int len, i, ret;
433
434     if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
435         return AVERROR_OPTION_NOT_FOUND;
436
437     dst = (uint8_t*)target_obj + o->offset;
438
439     buf[0] = 0;
440     switch (o->type) {
441     case AV_OPT_TYPE_FLAGS:     ret = snprintf(buf, sizeof(buf), "0x%08X",  *(int    *)dst);break;
442     case AV_OPT_TYPE_INT:       ret = snprintf(buf, sizeof(buf), "%d" ,     *(int    *)dst);break;
443     case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
444     case AV_OPT_TYPE_FLOAT:     ret = snprintf(buf, sizeof(buf), "%f" ,     *(float  *)dst);break;
445     case AV_OPT_TYPE_DOUBLE:    ret = snprintf(buf, sizeof(buf), "%f" ,     *(double *)dst);break;
446     case AV_OPT_TYPE_RATIONAL:  ret = snprintf(buf, sizeof(buf), "%d/%d",   ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
447     case AV_OPT_TYPE_CONST:     ret = snprintf(buf, sizeof(buf), "%f" ,     o->default_val.dbl);break;
448     case AV_OPT_TYPE_STRING:
449         if (*(uint8_t**)dst)
450             *out_val = av_strdup(*(uint8_t**)dst);
451         else
452             *out_val = av_strdup("");
453         return 0;
454     case AV_OPT_TYPE_BINARY:
455         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
456         if ((uint64_t)len*2 + 1 > INT_MAX)
457             return AVERROR(EINVAL);
458         if (!(*out_val = av_malloc(len*2 + 1)))
459             return AVERROR(ENOMEM);
460         bin = *(uint8_t**)dst;
461         for (i = 0; i < len; i++)
462             snprintf(*out_val + i*2, 3, "%02X", bin[i]);
463         return 0;
464     case AV_OPT_TYPE_IMAGE_SIZE:
465         ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
466         break;
467     case AV_OPT_TYPE_PIXEL_FMT:
468         ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
469         break;
470     default:
471         return AVERROR(EINVAL);
472     }
473
474     if (ret >= sizeof(buf))
475         return AVERROR(EINVAL);
476     *out_val = av_strdup(buf);
477     return 0;
478 }
479
480 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
481                       int search_flags)
482 {
483     void *dst, *target_obj;
484     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
485     if (!o || !target_obj)
486         goto error;
487
488     dst = ((uint8_t*)target_obj) + o->offset;
489
490     if (o_out) *o_out= o;
491
492     return read_number(o, dst, num, den, intnum);
493
494 error:
495     *den=*intnum=0;
496     return -1;
497 }
498
499 #if FF_API_OLD_AVOPTIONS
500 double av_get_double(void *obj, const char *name, const AVOption **o_out)
501 {
502     int64_t intnum=1;
503     double num=1;
504     int den=1;
505
506     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
507         return NAN;
508     return num*intnum/den;
509 }
510
511 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
512 {
513     int64_t intnum=1;
514     double num=1;
515     int den=1;
516
517     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
518         return (AVRational){0, 0};
519     if (num == 1.0 && (int)intnum == intnum)
520         return (AVRational){intnum, den};
521     else
522         return av_d2q(num*intnum/den, 1<<24);
523 }
524
525 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
526 {
527     int64_t intnum=1;
528     double num=1;
529     int den=1;
530
531     if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
532         return -1;
533     return num*intnum/den;
534 }
535 #endif
536
537 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
538 {
539     int64_t intnum = 1;
540     double     num = 1;
541     int   ret, den = 1;
542
543     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
544         return ret;
545     *out_val = num*intnum/den;
546     return 0;
547 }
548
549 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
550 {
551     int64_t intnum = 1;
552     double     num = 1;
553     int   ret, den = 1;
554
555     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
556         return ret;
557     *out_val = num*intnum/den;
558     return 0;
559 }
560
561 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
562 {
563     int64_t intnum = 1;
564     double     num = 1;
565     int   ret, den = 1;
566
567     if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
568         return ret;
569
570     if (num == 1.0 && (int)intnum == intnum)
571         *out_val = (AVRational){intnum, den};
572     else
573         *out_val = av_d2q(num*intnum/den, 1<<24);
574     return 0;
575 }
576
577 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
578 {
579     const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
580     const AVOption *flag  = av_opt_find(obj, flag_name,
581                                         field ? field->unit : NULL, 0, 0);
582     int64_t res;
583
584     if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
585         av_opt_get_int(obj, field_name, 0, &res) < 0)
586         return 0;
587     return res & flag->default_val.i64;
588 }
589
590 static void opt_list(void *obj, void *av_log_obj, const char *unit,
591                      int req_flags, int rej_flags)
592 {
593     const AVOption *opt=NULL;
594
595     while ((opt = av_opt_next(obj, opt))) {
596         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
597             continue;
598
599         /* Don't print CONST's on level one.
600          * Don't print anything but CONST's on level two.
601          * Only print items from the requested unit.
602          */
603         if (!unit && opt->type==AV_OPT_TYPE_CONST)
604             continue;
605         else if (unit && opt->type!=AV_OPT_TYPE_CONST)
606             continue;
607         else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
608             continue;
609         else if (unit && opt->type == AV_OPT_TYPE_CONST)
610             av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
611         else
612             av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
613
614         switch (opt->type) {
615             case AV_OPT_TYPE_FLAGS:
616                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
617                 break;
618             case AV_OPT_TYPE_INT:
619                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
620                 break;
621             case AV_OPT_TYPE_INT64:
622                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
623                 break;
624             case AV_OPT_TYPE_DOUBLE:
625                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
626                 break;
627             case AV_OPT_TYPE_FLOAT:
628                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
629                 break;
630             case AV_OPT_TYPE_STRING:
631                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
632                 break;
633             case AV_OPT_TYPE_RATIONAL:
634                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
635                 break;
636             case AV_OPT_TYPE_BINARY:
637                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
638                 break;
639             case AV_OPT_TYPE_IMAGE_SIZE:
640                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
641                 break;
642             case AV_OPT_TYPE_PIXEL_FMT:
643                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<pix_fmt>");
644                 break;
645             case AV_OPT_TYPE_CONST:
646             default:
647                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
648                 break;
649         }
650         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
651         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
652         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
653         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
654         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
655         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
656
657         if (opt->help)
658             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
659         av_log(av_log_obj, AV_LOG_INFO, "\n");
660         if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
661             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
662         }
663     }
664 }
665
666 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
667 {
668     if (!obj)
669         return -1;
670
671     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
672
673     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
674
675     return 0;
676 }
677
678 void av_opt_set_defaults(void *s)
679 {
680 #if FF_API_OLD_AVOPTIONS
681     av_opt_set_defaults2(s, 0, 0);
682 }
683
684 void av_opt_set_defaults2(void *s, int mask, int flags)
685 {
686 #endif
687     const AVOption *opt = NULL;
688     while ((opt = av_opt_next(s, opt)) != NULL) {
689 #if FF_API_OLD_AVOPTIONS
690         if ((opt->flags & mask) != flags)
691             continue;
692 #endif
693         switch (opt->type) {
694             case AV_OPT_TYPE_CONST:
695                 /* Nothing to be done here */
696             break;
697             case AV_OPT_TYPE_FLAGS:
698             case AV_OPT_TYPE_INT:
699             case AV_OPT_TYPE_INT64:
700                 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
701             break;
702             case AV_OPT_TYPE_DOUBLE:
703             case AV_OPT_TYPE_FLOAT: {
704                 double val;
705                 val = opt->default_val.dbl;
706                 av_opt_set_double(s, opt->name, val, 0);
707             }
708             break;
709             case AV_OPT_TYPE_RATIONAL: {
710                 AVRational val;
711                 val = av_d2q(opt->default_val.dbl, INT_MAX);
712                 av_opt_set_q(s, opt->name, val, 0);
713             }
714             break;
715             case AV_OPT_TYPE_STRING:
716             case AV_OPT_TYPE_IMAGE_SIZE:
717             case AV_OPT_TYPE_PIXEL_FMT:
718                 av_opt_set(s, opt->name, opt->default_val.str, 0);
719                 break;
720             case AV_OPT_TYPE_BINARY:
721                 /* Cannot set default for binary */
722             break;
723             default:
724                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
725         }
726     }
727 }
728
729 /**
730  * Store the value in the field in ctx that is named like key.
731  * ctx must be an AVClass context, storing is done using AVOptions.
732  *
733  * @param buf the string to parse, buf will be updated to point at the
734  * separator just after the parsed key/value pair
735  * @param key_val_sep a 0-terminated list of characters used to
736  * separate key from value
737  * @param pairs_sep a 0-terminated list of characters used to separate
738  * two pairs from each other
739  * @return 0 if the key/value pair has been successfully parsed and
740  * set, or a negative value corresponding to an AVERROR code in case
741  * of error:
742  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
743  * the error code issued by av_opt_set() if the key/value pair
744  * cannot be set
745  */
746 static int parse_key_value_pair(void *ctx, const char **buf,
747                                 const char *key_val_sep, const char *pairs_sep)
748 {
749     char *key = av_get_token(buf, key_val_sep);
750     char *val;
751     int ret;
752
753     if (*key && strspn(*buf, key_val_sep)) {
754         (*buf)++;
755         val = av_get_token(buf, pairs_sep);
756     } else {
757         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
758         av_free(key);
759         return AVERROR(EINVAL);
760     }
761
762     av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
763
764     ret = av_opt_set(ctx, key, val, 0);
765     if (ret == AVERROR_OPTION_NOT_FOUND)
766         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
767
768     av_free(key);
769     av_free(val);
770     return ret;
771 }
772
773 int av_set_options_string(void *ctx, const char *opts,
774                           const char *key_val_sep, const char *pairs_sep)
775 {
776     int ret, count = 0;
777
778     if (!opts)
779         return 0;
780
781     while (*opts) {
782         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
783             return ret;
784         count++;
785
786         if (*opts)
787             opts++;
788     }
789
790     return count;
791 }
792
793 #define WHITESPACES " \n\t"
794
795 static int is_key_char(char c)
796 {
797     return (unsigned)((c | 32) - 'a') < 26 ||
798            (unsigned)(c - '0') < 10 ||
799            c == '-' || c == '_' || c == '/' || c == '.';
800 }
801
802 /**
803  * Read a key from a string.
804  *
805  * The key consists of is_key_char characters and must be terminated by a
806  * character from the delim string; spaces are ignored. The key buffer must
807  * be 4 bytes larger than the longest acceptable key. If the key is too
808  * long, an ellipsis will be written at the end.
809  *
810  * @return  0 for success (even with ellipsis), <0 for failure
811  */
812 static int get_key(const char **ropts, const char *delim, char *key, unsigned key_size)
813 {
814     unsigned key_pos = 0;
815     const char *opts = *ropts;
816
817     opts += strspn(opts, WHITESPACES);
818     while (is_key_char(*opts)) {
819         key[key_pos++] = *opts;
820         if (key_pos == key_size)
821             key_pos--;
822         (opts)++;
823     }
824     opts += strspn(opts, WHITESPACES);
825     if (!*opts || !strchr(delim, *opts))
826         return AVERROR(EINVAL);
827     opts++;
828     key[key_pos++] = 0;
829     if (key_pos == key_size)
830         key[key_pos - 4] = key[key_pos - 3] = key[key_pos - 2] = '.';
831     *ropts = opts;
832     return 0;
833 }
834
835 int av_opt_set_from_string(void *ctx, const char *opts,
836                            const char *const *shorthand,
837                            const char *key_val_sep, const char *pairs_sep)
838 {
839     int ret, count = 0;
840     const char *dummy_shorthand = NULL;
841     char key_buf[68], *value;
842     const char *key;
843
844     if (!opts)
845         return 0;
846     if (!shorthand)
847         shorthand = &dummy_shorthand;
848
849     while (*opts) {
850         if ((ret = get_key(&opts, key_val_sep, key_buf, sizeof(key_buf))) < 0) {
851             if (*shorthand) {
852                 key = *(shorthand++);
853             } else {
854                 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
855                 return AVERROR(EINVAL);
856             }
857         } else {
858             key = key_buf;
859             while (*shorthand) /* discard all remaining shorthand */
860                 shorthand++;
861         }
862
863         if (!(value = av_get_token(&opts, pairs_sep)))
864             return AVERROR(ENOMEM);
865         if (*opts && strchr(pairs_sep, *opts))
866             opts++;
867
868         av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
869         if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
870             if (ret == AVERROR_OPTION_NOT_FOUND)
871                 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
872             av_free(value);
873             return ret;
874         }
875
876         av_free(value);
877         count++;
878     }
879     return count;
880 }
881
882 void av_opt_free(void *obj)
883 {
884     const AVOption *o = NULL;
885     while ((o = av_opt_next(obj, o)))
886         if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
887             av_freep((uint8_t *)obj + o->offset);
888 }
889
890 int av_opt_set_dict(void *obj, AVDictionary **options)
891 {
892     AVDictionaryEntry *t = NULL;
893     AVDictionary    *tmp = NULL;
894     int ret = 0;
895
896     while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
897         ret = av_opt_set(obj, t->key, t->value, 0);
898         if (ret == AVERROR_OPTION_NOT_FOUND)
899             av_dict_set(&tmp, t->key, t->value, 0);
900         else if (ret < 0) {
901             av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
902             break;
903         }
904         ret = 0;
905     }
906     av_dict_free(options);
907     *options = tmp;
908     return ret;
909 }
910
911 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
912                             int opt_flags, int search_flags)
913 {
914     return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
915 }
916
917 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
918                              int opt_flags, int search_flags, void **target_obj)
919 {
920     const AVClass  *c;
921     const AVOption *o = NULL;
922
923     if(!obj)
924         return NULL;
925
926     c= *(AVClass**)obj;
927
928     if (search_flags & AV_OPT_SEARCH_CHILDREN) {
929         if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
930             const AVClass *child = NULL;
931             while (child = av_opt_child_class_next(c, child))
932                 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
933                     return o;
934         } else {
935             void *child = NULL;
936             while (child = av_opt_child_next(obj, child))
937                 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
938                     return o;
939         }
940     }
941
942     while (o = av_opt_next(obj, o)) {
943         if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
944             ((!unit && o->type != AV_OPT_TYPE_CONST) ||
945              (unit  && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
946             if (target_obj) {
947                 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
948                     *target_obj = obj;
949                 else
950                     *target_obj = NULL;
951             }
952             return o;
953         }
954     }
955     return NULL;
956 }
957
958 void *av_opt_child_next(void *obj, void *prev)
959 {
960     const AVClass *c = *(AVClass**)obj;
961     if (c->child_next)
962         return c->child_next(obj, prev);
963     return NULL;
964 }
965
966 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
967 {
968     if (parent->child_class_next)
969         return parent->child_class_next(prev);
970     return NULL;
971 }
972
973 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
974 {
975     const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
976     if(!opt)
977         return NULL;
978     return (uint8_t*)obj + opt->offset;
979 }
980
981 #ifdef TEST
982
983 #undef printf
984
985 typedef struct TestContext
986 {
987     const AVClass *class;
988     int num;
989     int toggle;
990     char *string;
991     int flags;
992     AVRational rational;
993     int w, h;
994     enum AVPixelFormat pix_fmt;
995 } TestContext;
996
997 #define OFFSET(x) offsetof(TestContext, x)
998
999 #define TEST_FLAG_COOL 01
1000 #define TEST_FLAG_LAME 02
1001 #define TEST_FLAG_MU   04
1002
1003 static const AVOption test_options[]= {
1004 {"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {.i64 = 0},       0,        100                 },
1005 {"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {.i64 = 0},       0,        1                   },
1006 {"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0},  0,        10                  },
1007 {"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {0},              CHAR_MIN, CHAR_MAX            },
1008 {"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {.i64 = 0},       0,        INT_MAX, 0, "flags" },
1009 {"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
1010 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
1011 {"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
1012 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
1013 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT,{0},              0,        0                   },
1014 {NULL},
1015 };
1016
1017 static const char *test_get_name(void *ctx)
1018 {
1019     return "test";
1020 }
1021
1022 static const AVClass test_class = {
1023     "TestContext",
1024     test_get_name,
1025     test_options
1026 };
1027
1028 int main(void)
1029 {
1030     int i;
1031
1032     printf("\nTesting av_set_options_string()\n");
1033     {
1034         TestContext test_ctx = { 0 };
1035         const char *options[] = {
1036             "",
1037             ":",
1038             "=",
1039             "foo=:",
1040             ":=foo",
1041             "=foo",
1042             "foo=",
1043             "foo",
1044             "foo=val",
1045             "foo==val",
1046             "toggle=:",
1047             "string=:",
1048             "toggle=1 : foo",
1049             "toggle=100",
1050             "toggle==1",
1051             "flags=+mu-lame : num=42: toggle=0",
1052             "num=42 : string=blahblah",
1053             "rational=0 : rational=1/2 : rational=1/-1",
1054             "rational=-1/0",
1055             "size=1024x768",
1056             "size=pal",
1057             "size=bogus",
1058             "pix_fmt=yuv420p",
1059             "pix_fmt=2",
1060             "pix_fmt=bogus",
1061         };
1062
1063         test_ctx.class = &test_class;
1064         av_opt_set_defaults(&test_ctx);
1065         test_ctx.string = av_strdup("default");
1066
1067         av_log_set_level(AV_LOG_DEBUG);
1068
1069         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1070             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1071             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1072                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1073             printf("\n");
1074         }
1075         av_freep(&test_ctx.string);
1076     }
1077
1078     printf("\nTesting av_opt_set_from_string()\n");
1079     {
1080         TestContext test_ctx = { 0 };
1081         const char *options[] = {
1082             "",
1083             "5",
1084             "5:hello",
1085             "5:hello:size=pal",
1086             "5:size=pal:hello",
1087             ":",
1088             "=",
1089             " 5 : hello : size = pal ",
1090             "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1091         };
1092         const char *shorthand[] = { "num", "string", NULL };
1093
1094         test_ctx.class = &test_class;
1095         av_opt_set_defaults(&test_ctx);
1096         test_ctx.string = av_strdup("default");
1097
1098         av_log_set_level(AV_LOG_DEBUG);
1099
1100         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1101             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1102             if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1103                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1104             printf("\n");
1105         }
1106         av_freep(&test_ctx.string);
1107     }
1108
1109     return 0;
1110 }
1111
1112 #endif