]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.c
ARM: fix av_clipl_int32_arm()
[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
33 //FIXME order them and do a bin search
34 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
35 {
36     AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
37     const AVOption *o= c->option;
38
39     for (; o && o->name; o++) {
40         if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
41             return o;
42     }
43     return NULL;
44 }
45
46 const AVOption *av_next_option(void *obj, const AVOption *last)
47 {
48     if (last && last[1].name) return ++last;
49     else if (last)            return NULL;
50     else                      return (*(AVClass**)obj)->option;
51 }
52
53 static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
54 {
55     const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
56     void *dst;
57     if (o_out)
58         *o_out= o;
59     if (!o || o->offset<=0)
60         return AVERROR_OPTION_NOT_FOUND;
61
62     if (o->max*den < num*intnum || o->min*den > num*intnum) {
63         av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
64         return AVERROR(ERANGE);
65     }
66
67     dst= ((uint8_t*)obj) + o->offset;
68
69     switch (o->type) {
70     case FF_OPT_TYPE_FLAGS:
71     case FF_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
72     case FF_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
73     case FF_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
74     case FF_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
75     case FF_OPT_TYPE_RATIONAL:
76         if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
77         else                 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
78         break;
79     default:
80         return AVERROR(EINVAL);
81     }
82     return 0;
83 }
84
85 static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
86 {
87     const AVOption *o = NULL;
88     if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
89         return NULL;
90     else
91         return o;
92 }
93
94 static const double const_values[] = {
95     M_PI,
96     M_E,
97     FF_QP2LAMBDA,
98     0
99 };
100
101 static const char * const const_names[] = {
102     "PI",
103     "E",
104     "QP2LAMBDA",
105     0
106 };
107
108 static int hexchar2int(char c) {
109     if (c >= '0' && c <= '9') return c - '0';
110     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
111     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
112     return -1;
113 }
114
115 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
116 {
117     int ret;
118     const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
119     if (o_out)
120         *o_out = o;
121     if (!o)
122         return AVERROR_OPTION_NOT_FOUND;
123     if (!val || o->offset<=0)
124         return AVERROR(EINVAL);
125
126     if (o->type == FF_OPT_TYPE_BINARY) {
127         uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
128         int *lendst = (int *)(dst + 1);
129         uint8_t *bin, *ptr;
130         int len = strlen(val);
131         av_freep(dst);
132         *lendst = 0;
133         if (len & 1) return AVERROR(EINVAL);
134         len /= 2;
135         ptr = bin = av_malloc(len);
136         while (*val) {
137             int a = hexchar2int(*val++);
138             int b = hexchar2int(*val++);
139             if (a < 0 || b < 0) {
140                 av_free(bin);
141                 return AVERROR(EINVAL);
142             }
143             *ptr++ = (a << 4) | b;
144         }
145         *dst = bin;
146         *lendst = len;
147         return 0;
148     }
149     if (o->type != FF_OPT_TYPE_STRING) {
150         int notfirst=0;
151         for (;;) {
152             int i;
153             char buf[256];
154             int cmd=0;
155             double d;
156
157             if (*val == '+' || *val == '-')
158                 cmd= *(val++);
159
160             for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
161                 buf[i]= val[i];
162             buf[i]=0;
163
164             {
165                 const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
166                 if (o_named && o_named->type == FF_OPT_TYPE_CONST)
167                     d= o_named->default_val.dbl;
168                 else if (!strcmp(buf, "default")) d= o->default_val.dbl;
169                 else if (!strcmp(buf, "max"    )) d= o->max;
170                 else if (!strcmp(buf, "min"    )) d= o->min;
171                 else if (!strcmp(buf, "none"   )) d= 0;
172                 else if (!strcmp(buf, "all"    )) d= ~0;
173                 else {
174                     int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
175                     if (res < 0) {
176                         av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
177                         return res;
178                     }
179                 }
180             }
181             if (o->type == FF_OPT_TYPE_FLAGS) {
182                 if      (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
183                 else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
184             } else {
185                 if      (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
186                 else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
187             }
188
189             if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
190                 return ret;
191             val+= i;
192             if (!*val)
193                 return 0;
194             notfirst=1;
195         }
196         return AVERROR(EINVAL);
197     }
198
199     if (alloc) {
200         av_free(*(void**)(((uint8_t*)obj) + o->offset));
201         val= av_strdup(val);
202     }
203
204     memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
205     return 0;
206 }
207
208 const AVOption *av_set_double(void *obj, const char *name, double n)
209 {
210     return av_set_number(obj, name, n, 1, 1);
211 }
212
213 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
214 {
215     return av_set_number(obj, name, n.num, n.den, 1);
216 }
217
218 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
219 {
220     return av_set_number(obj, name, 1, 1, n);
221 }
222
223 /**
224  *
225  * @param buf a buffer which is used for returning non string values as strings, can be NULL
226  * @param buf_len allocated length in bytes of buf
227  */
228 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
229 {
230     const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
231     void *dst;
232     uint8_t *bin;
233     int len, i;
234     if (!o || o->offset<=0)
235         return NULL;
236     if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
237         return NULL;
238
239     dst= ((uint8_t*)obj) + o->offset;
240     if (o_out) *o_out= o;
241
242     switch (o->type) {
243     case FF_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
244     case FF_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
245     case FF_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
246     case FF_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
247     case FF_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
248     case FF_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
249     case FF_OPT_TYPE_STRING:    return *(void**)dst;
250     case FF_OPT_TYPE_BINARY:
251         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
252         if (len >= (buf_len + 1)/2) return NULL;
253         bin = *(uint8_t**)dst;
254         for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
255         break;
256     default: return NULL;
257     }
258     return buf;
259 }
260
261 static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
262 {
263     const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
264     void *dst;
265     if (!o || o->offset<=0)
266         goto error;
267
268     dst= ((uint8_t*)obj) + o->offset;
269
270     if (o_out) *o_out= o;
271
272     switch (o->type) {
273     case FF_OPT_TYPE_FLAGS:     *intnum= *(unsigned int*)dst;return 0;
274     case FF_OPT_TYPE_INT:       *intnum= *(int    *)dst;return 0;
275     case FF_OPT_TYPE_INT64:     *intnum= *(int64_t*)dst;return 0;
276     case FF_OPT_TYPE_FLOAT:     *num=    *(float  *)dst;return 0;
277     case FF_OPT_TYPE_DOUBLE:    *num=    *(double *)dst;return 0;
278     case FF_OPT_TYPE_RATIONAL:  *intnum= ((AVRational*)dst)->num;
279                                 *den   = ((AVRational*)dst)->den;
280                                                         return 0;
281     }
282 error:
283     *den=*intnum=0;
284     return -1;
285 }
286
287 double av_get_double(void *obj, const char *name, const AVOption **o_out)
288 {
289     int64_t intnum=1;
290     double num=1;
291     int den=1;
292
293     if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
294         return NAN;
295     return num*intnum/den;
296 }
297
298 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
299 {
300     int64_t intnum=1;
301     double num=1;
302     int den=1;
303
304     if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
305         return (AVRational){0, 0};
306     if (num == 1.0 && (int)intnum == intnum)
307         return (AVRational){intnum, den};
308     else
309         return av_d2q(num*intnum/den, 1<<24);
310 }
311
312 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
313 {
314     int64_t intnum=1;
315     double num=1;
316     int den=1;
317
318     if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
319         return -1;
320     return num*intnum/den;
321 }
322
323 static void opt_list(void *obj, void *av_log_obj, const char *unit,
324                      int req_flags, int rej_flags)
325 {
326     const AVOption *opt=NULL;
327
328     while ((opt= av_next_option(obj, opt))) {
329         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
330             continue;
331
332         /* Don't print CONST's on level one.
333          * Don't print anything but CONST's on level two.
334          * Only print items from the requested unit.
335          */
336         if (!unit && opt->type==FF_OPT_TYPE_CONST)
337             continue;
338         else if (unit && opt->type!=FF_OPT_TYPE_CONST)
339             continue;
340         else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
341             continue;
342         else if (unit && opt->type == FF_OPT_TYPE_CONST)
343             av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
344         else
345             av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
346
347         switch (opt->type) {
348             case FF_OPT_TYPE_FLAGS:
349                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
350                 break;
351             case FF_OPT_TYPE_INT:
352                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
353                 break;
354             case FF_OPT_TYPE_INT64:
355                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
356                 break;
357             case FF_OPT_TYPE_DOUBLE:
358                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
359                 break;
360             case FF_OPT_TYPE_FLOAT:
361                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
362                 break;
363             case FF_OPT_TYPE_STRING:
364                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
365                 break;
366             case FF_OPT_TYPE_RATIONAL:
367                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
368                 break;
369             case FF_OPT_TYPE_BINARY:
370                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
371                 break;
372             case FF_OPT_TYPE_CONST:
373             default:
374                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
375                 break;
376         }
377         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
378         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
379         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
380         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
381         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
382
383         if (opt->help)
384             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
385         av_log(av_log_obj, AV_LOG_INFO, "\n");
386         if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
387             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
388         }
389     }
390 }
391
392 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
393 {
394     if (!obj)
395         return -1;
396
397     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
398
399     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
400
401     return 0;
402 }
403
404 /** Set the values of the AVCodecContext or AVFormatContext structure.
405  * They are set to the defaults specified in the according AVOption options
406  * array default_val field.
407  *
408  * @param s AVCodecContext or AVFormatContext for which the defaults will be set
409  */
410 void av_opt_set_defaults2(void *s, int mask, int flags)
411 {
412     const AVOption *opt = NULL;
413     while ((opt = av_next_option(s, opt)) != NULL) {
414         if ((opt->flags & mask) != flags)
415             continue;
416         switch (opt->type) {
417             case FF_OPT_TYPE_CONST:
418                 /* Nothing to be done here */
419             break;
420             case FF_OPT_TYPE_FLAGS:
421             case FF_OPT_TYPE_INT: {
422                 int val;
423                 val = opt->default_val.dbl;
424                 av_set_int(s, opt->name, val);
425             }
426             break;
427             case FF_OPT_TYPE_INT64:
428                 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
429                     av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
430                 av_set_int(s, opt->name, opt->default_val.dbl);
431             break;
432             case FF_OPT_TYPE_DOUBLE:
433             case FF_OPT_TYPE_FLOAT: {
434                 double val;
435                 val = opt->default_val.dbl;
436                 av_set_double(s, opt->name, val);
437             }
438             break;
439             case FF_OPT_TYPE_RATIONAL: {
440                 AVRational val;
441                 val = av_d2q(opt->default_val.dbl, INT_MAX);
442                 av_set_q(s, opt->name, val);
443             }
444             break;
445             case FF_OPT_TYPE_STRING:
446                 av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
447                 break;
448             case FF_OPT_TYPE_BINARY:
449                 /* Cannot set default for binary */
450             break;
451             default:
452                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
453         }
454     }
455 }
456
457 void av_opt_set_defaults(void *s)
458 {
459     av_opt_set_defaults2(s, 0, 0);
460 }
461
462 /**
463  * Store the value in the field in ctx that is named like key.
464  * ctx must be an AVClass context, storing is done using AVOptions.
465  *
466  * @param buf the string to parse, buf will be updated to point at the
467  * separator just after the parsed key/value pair
468  * @param key_val_sep a 0-terminated list of characters used to
469  * separate key from value
470  * @param pairs_sep a 0-terminated list of characters used to separate
471  * two pairs from each other
472  * @return 0 if the key/value pair has been successfully parsed and
473  * set, or a negative value corresponding to an AVERROR code in case
474  * of error:
475  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
476  * the error code issued by av_set_string3() if the key/value pair
477  * cannot be set
478  */
479 static int parse_key_value_pair(void *ctx, const char **buf,
480                                 const char *key_val_sep, const char *pairs_sep)
481 {
482     char *key = av_get_token(buf, key_val_sep);
483     char *val;
484     int ret;
485
486     if (*key && strspn(*buf, key_val_sep)) {
487         (*buf)++;
488         val = av_get_token(buf, pairs_sep);
489     } else {
490         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
491         av_free(key);
492         return AVERROR(EINVAL);
493     }
494
495     av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
496
497     ret = av_set_string3(ctx, key, val, 1, NULL);
498     if (ret == AVERROR_OPTION_NOT_FOUND)
499         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
500
501     av_free(key);
502     av_free(val);
503     return ret;
504 }
505
506 int av_set_options_string(void *ctx, const char *opts,
507                           const char *key_val_sep, const char *pairs_sep)
508 {
509     int ret, count = 0;
510
511     while (*opts) {
512         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
513             return ret;
514         count++;
515
516         if (*opts)
517             opts++;
518     }
519
520     return count;
521 }
522
523 #ifdef TEST
524
525 #undef printf
526
527 typedef struct TestContext
528 {
529     const AVClass *class;
530     int num;
531     int toggle;
532     char *string;
533     int flags;
534     AVRational rational;
535 } TestContext;
536
537 #define OFFSET(x) offsetof(TestContext, x)
538
539 #define TEST_FLAG_COOL 01
540 #define TEST_FLAG_LAME 02
541 #define TEST_FLAG_MU   04
542
543 static const AVOption test_options[]= {
544 {"num",      "set num",        OFFSET(num),      FF_OPT_TYPE_INT,      0,              0,        100                 },
545 {"toggle",   "set toggle",     OFFSET(toggle),   FF_OPT_TYPE_INT,      0,              0,        1                   },
546 {"rational", "set rational",   OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0,              0,        10                  },
547 {"string",   "set string",     OFFSET(string),   FF_OPT_TYPE_STRING,   0,              CHAR_MIN, CHAR_MAX            },
548 {"flags",    "set flags",      OFFSET(flags),    FF_OPT_TYPE_FLAGS,    0,              0,        INT_MAX, 0, "flags" },
549 {"cool",     "set cool flag ", 0,                FF_OPT_TYPE_CONST,    TEST_FLAG_COOL, INT_MIN,  INT_MAX, 0, "flags" },
550 {"lame",     "set lame flag ", 0,                FF_OPT_TYPE_CONST,    TEST_FLAG_LAME, INT_MIN,  INT_MAX, 0, "flags" },
551 {"mu",       "set mu flag ",   0,                FF_OPT_TYPE_CONST,    TEST_FLAG_MU,   INT_MIN,  INT_MAX, 0, "flags" },
552 {NULL},
553 };
554
555 static const char *test_get_name(void *ctx)
556 {
557     return "test";
558 }
559
560 static const AVClass test_class = {
561     "TestContext",
562     test_get_name,
563     test_options
564 };
565
566 int main(void)
567 {
568     int i;
569
570     printf("\nTesting av_set_options_string()\n");
571     {
572         TestContext test_ctx;
573         const char *options[] = {
574             "",
575             ":",
576             "=",
577             "foo=:",
578             ":=foo",
579             "=foo",
580             "foo=",
581             "foo",
582             "foo=val",
583             "foo==val",
584             "toggle=:",
585             "string=:",
586             "toggle=1 : foo",
587             "toggle=100",
588             "toggle==1",
589             "flags=+mu-lame : num=42: toggle=0",
590             "num=42 : string=blahblah",
591             "rational=0 : rational=1/2 : rational=1/-1",
592             "rational=-1/0",
593         };
594
595         test_ctx.class = &test_class;
596         av_opt_set_defaults2(&test_ctx, 0, 0);
597         test_ctx.string = av_strdup("default");
598
599         av_log_set_level(AV_LOG_DEBUG);
600
601         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
602             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
603             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
604                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
605             printf("\n");
606         }
607     }
608
609     return 0;
610 }
611
612 #endif