3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
25 * @author Michael Niedermayer <michaelni@gmx.at>
35 #include "parseutils.h"
37 #include "mathematics.h"
38 #include "samplefmt.h"
43 //FIXME order them and do a bin search
44 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
46 const AVOption *o = NULL;
48 while ((o = av_next_option(v, o))) {
49 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
56 #if FF_API_OLD_AVOPTIONS
57 const AVOption *av_next_option(void *obj, const AVOption *last)
59 return av_opt_next(obj, last);
63 const AVOption *av_opt_next(void *obj, const AVOption *last)
65 AVClass *class = *(AVClass**)obj;
66 if (!last && class->option && class->option[0].name)
68 if (last && last[1].name) return ++last;
72 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
75 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
76 case AV_OPT_TYPE_PIXEL_FMT:
77 case AV_OPT_TYPE_SAMPLE_FMT:
78 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
79 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
80 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
81 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
82 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
83 *den = ((AVRational*)dst)->den;
85 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
87 return AVERROR(EINVAL);
90 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
92 if (o->max*den < num*intnum || o->min*den > num*intnum) {
93 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
94 num*intnum/den, o->name, o->min, o->max);
95 return AVERROR(ERANGE);
99 case AV_OPT_TYPE_FLAGS:
100 case AV_OPT_TYPE_PIXEL_FMT:
101 case AV_OPT_TYPE_SAMPLE_FMT:
102 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
103 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
104 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
105 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
106 case AV_OPT_TYPE_RATIONAL:
107 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
108 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
111 return AVERROR(EINVAL);
116 static const double const_values[] = {
123 static const char * const const_names[] = {
130 static int hexchar2int(char c) {
131 if (c >= '0' && c <= '9') return c - '0';
132 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
133 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
137 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
139 int *lendst = (int *)(dst + 1);
141 int len = strlen(val);
147 return AVERROR(EINVAL);
150 ptr = bin = av_malloc(len);
152 int a = hexchar2int(*val++);
153 int b = hexchar2int(*val++);
154 if (a < 0 || b < 0) {
156 return AVERROR(EINVAL);
158 *ptr++ = (a << 4) | b;
166 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
169 *dst = av_strdup(val);
173 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
174 opt->type == AV_OPT_TYPE_CONST || \
175 opt->type == AV_OPT_TYPE_FLAGS || \
176 opt->type == AV_OPT_TYPE_INT) ? \
177 opt->default_val.i64 : opt->default_val.dbl)
179 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
181 int ret = 0, notfirst = 0;
190 if (*val == '+' || *val == '-') {
191 if (o->type == AV_OPT_TYPE_FLAGS)
197 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
202 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
203 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
204 d = DEFAULT_NUMVAL(o_named);
205 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
206 else if (!strcmp(buf, "max" )) d = o->max;
207 else if (!strcmp(buf, "min" )) d = o->min;
208 else if (!strcmp(buf, "none" )) d = 0;
209 else if (!strcmp(buf, "all" )) d = ~0;
211 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
213 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
218 if (o->type == AV_OPT_TYPE_FLAGS) {
219 read_number(o, dst, NULL, NULL, &intnum);
220 if (cmd == '+') d = intnum | (int64_t)d;
221 else if (cmd == '-') d = intnum &~(int64_t)d;
223 read_number(o, dst, &num, &den, &intnum);
224 if (cmd == '+') d = notfirst*num*intnum/den + d;
225 else if (cmd == '-') d = notfirst*num*intnum/den - d;
228 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
239 #if FF_API_OLD_AVOPTIONS
240 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
242 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
245 return av_opt_set(obj, name, val, 0);
249 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
252 void *dst, *target_obj;
253 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
254 if (!o || !target_obj)
255 return AVERROR_OPTION_NOT_FOUND;
256 if (!val && (o->type != AV_OPT_TYPE_STRING &&
257 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
258 o->type != AV_OPT_TYPE_IMAGE_SIZE))
259 return AVERROR(EINVAL);
261 dst = ((uint8_t*)target_obj) + o->offset;
263 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
264 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
265 case AV_OPT_TYPE_FLAGS:
266 case AV_OPT_TYPE_INT:
267 case AV_OPT_TYPE_INT64:
268 case AV_OPT_TYPE_FLOAT:
269 case AV_OPT_TYPE_DOUBLE:
270 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
271 case AV_OPT_TYPE_IMAGE_SIZE:
272 if (!val || !strcmp(val, "none")) {
273 *(int *)dst = *((int *)dst + 1) = 0;
276 ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
278 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
280 case AV_OPT_TYPE_PIXEL_FMT:
281 if (!val || !strcmp(val, "none")) {
282 ret = AV_PIX_FMT_NONE;
284 ret = av_get_pix_fmt(val);
285 if (ret == AV_PIX_FMT_NONE) {
287 ret = strtol(val, &tail, 0);
288 if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
289 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
290 return AVERROR(EINVAL);
294 *(enum AVPixelFormat *)dst = ret;
296 case AV_OPT_TYPE_SAMPLE_FMT:
297 if (!val || !strcmp(val, "none")) {
298 ret = AV_SAMPLE_FMT_NONE;
300 ret = av_get_sample_fmt(val);
301 if (ret == AV_SAMPLE_FMT_NONE) {
303 ret = strtol(val, &tail, 0);
304 if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
305 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
306 return AVERROR(EINVAL);
310 *(enum AVSampleFormat *)dst = ret;
314 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
315 return AVERROR(EINVAL);
318 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
319 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
321 if (!o || o->type != opttype)\
322 return AVERROR(EINVAL);\
323 return set_string_number(obj, o, val, name ## _out);\
326 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
327 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
328 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
329 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
330 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
331 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
333 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
336 void *dst, *target_obj;
337 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
339 if (!o || !target_obj)
340 return AVERROR_OPTION_NOT_FOUND;
342 dst = ((uint8_t*)target_obj) + o->offset;
343 return write_number(obj, o, dst, num, den, intnum);
346 #if FF_API_OLD_AVOPTIONS
347 const AVOption *av_set_double(void *obj, const char *name, double n)
349 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
350 if (set_number(obj, name, n, 1, 1, 0) < 0)
355 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
357 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
358 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
363 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
365 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
366 if (set_number(obj, name, 1, 1, n, 0) < 0)
372 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
374 return set_number(obj, name, 1, 1, val, search_flags);
377 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
379 return set_number(obj, name, val, 1, 1, search_flags);
382 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
384 return set_number(obj, name, val.num, val.den, 1, search_flags);
387 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
390 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
395 if (!o || !target_obj)
396 return AVERROR_OPTION_NOT_FOUND;
398 if (o->type != AV_OPT_TYPE_BINARY)
399 return AVERROR(EINVAL);
401 ptr = av_malloc(len);
403 return AVERROR(ENOMEM);
405 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
406 lendst = (int *)(dst + 1);
411 memcpy(ptr, val, len);
416 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
419 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
421 if (!o || !target_obj)
422 return AVERROR_OPTION_NOT_FOUND;
423 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
424 av_log(obj, AV_LOG_ERROR,
425 "The value set by option '%s' is not an image size.\n", o->name);
426 return AVERROR(EINVAL);
429 av_log(obj, AV_LOG_ERROR,
430 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
431 return AVERROR(EINVAL);
433 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
434 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
438 static int set_format(void *obj, const char *name, int fmt, int search_flags,
439 enum AVOptionType type, const char *desc, int nb_fmts)
442 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
443 search_flags, &target_obj);
445 const AVClass *class = *(AVClass **)obj;
447 if (!o || !target_obj)
448 return AVERROR_OPTION_NOT_FOUND;
449 if (o->type != type) {
450 av_log(obj, AV_LOG_ERROR,
451 "The value set by option '%s' is not a %s format", name, desc);
452 return AVERROR(EINVAL);
455 #if LIBAVUTIL_VERSION_MAJOR < 53
456 if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
462 min = FFMIN(o->min, -1);
463 max = FFMAX(o->max, nb_fmts-1);
465 if (fmt < min || fmt > max) {
466 av_log(obj, AV_LOG_ERROR,
467 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
468 fmt, name, desc, min, max);
469 return AVERROR(ERANGE);
471 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
475 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
477 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
480 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
482 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
485 #if FF_API_OLD_AVOPTIONS
488 * @param buf a buffer which is used for returning non string values as strings, can be NULL
489 * @param buf_len allocated length in bytes of buf
491 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
493 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
499 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
502 dst= ((uint8_t*)obj) + o->offset;
503 if (o_out) *o_out= o;
506 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
507 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
508 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
509 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
510 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
511 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
512 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
513 case AV_OPT_TYPE_STRING: return *(void**)dst;
514 case AV_OPT_TYPE_BINARY:
515 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
516 if (len >= (buf_len + 1)/2) return NULL;
517 bin = *(uint8_t**)dst;
518 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
520 default: return NULL;
526 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
528 void *dst, *target_obj;
529 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
530 uint8_t *bin, buf[128];
533 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
534 return AVERROR_OPTION_NOT_FOUND;
536 dst = (uint8_t*)target_obj + o->offset;
540 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
541 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
542 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
543 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
544 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
545 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
546 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
547 case AV_OPT_TYPE_STRING:
549 *out_val = av_strdup(*(uint8_t**)dst);
551 *out_val = av_strdup("");
553 case AV_OPT_TYPE_BINARY:
554 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
555 if ((uint64_t)len*2 + 1 > INT_MAX)
556 return AVERROR(EINVAL);
557 if (!(*out_val = av_malloc(len*2 + 1)))
558 return AVERROR(ENOMEM);
559 bin = *(uint8_t**)dst;
560 for (i = 0; i < len; i++)
561 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
563 case AV_OPT_TYPE_IMAGE_SIZE:
564 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
566 case AV_OPT_TYPE_PIXEL_FMT:
567 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
569 case AV_OPT_TYPE_SAMPLE_FMT:
570 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
573 return AVERROR(EINVAL);
576 if (ret >= sizeof(buf))
577 return AVERROR(EINVAL);
578 *out_val = av_strdup(buf);
582 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
585 void *dst, *target_obj;
586 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
587 if (!o || !target_obj)
590 dst = ((uint8_t*)target_obj) + o->offset;
592 if (o_out) *o_out= o;
594 return read_number(o, dst, num, den, intnum);
601 #if FF_API_OLD_AVOPTIONS
602 double av_get_double(void *obj, const char *name, const AVOption **o_out)
608 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
610 return num*intnum/den;
613 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
619 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
620 return (AVRational){0, 0};
621 if (num == 1.0 && (int)intnum == intnum)
622 return (AVRational){intnum, den};
624 return av_d2q(num*intnum/den, 1<<24);
627 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
633 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
635 return num*intnum/den;
639 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
645 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
647 *out_val = num*intnum/den;
651 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
657 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
659 *out_val = num*intnum/den;
663 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
669 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
672 if (num == 1.0 && (int)intnum == intnum)
673 *out_val = (AVRational){intnum, den};
675 *out_val = av_d2q(num*intnum/den, 1<<24);
679 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
681 void *dst, *target_obj;
682 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
683 if (!o || !target_obj)
684 return AVERROR_OPTION_NOT_FOUND;
685 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
686 av_log(obj, AV_LOG_ERROR,
687 "The value for option '%s' is not an image size.\n", name);
688 return AVERROR(EINVAL);
691 dst = ((uint8_t*)target_obj) + o->offset;
692 if (w_out) *w_out = *(int *)dst;
693 if (h_out) *h_out = *((int *)dst+1);
697 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
698 enum AVOptionType type, const char *desc)
700 void *dst, *target_obj;
701 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
702 if (!o || !target_obj)
703 return AVERROR_OPTION_NOT_FOUND;
704 if (o->type != type) {
705 av_log(obj, AV_LOG_ERROR,
706 "The value for option '%s' is not a %s format.\n", desc, name);
707 return AVERROR(EINVAL);
710 dst = ((uint8_t*)target_obj) + o->offset;
711 *out_fmt = *(int *)dst;
715 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
717 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
720 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
722 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
725 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
727 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
728 const AVOption *flag = av_opt_find(obj, flag_name,
729 field ? field->unit : NULL, 0, 0);
732 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
733 av_opt_get_int(obj, field_name, 0, &res) < 0)
735 return res & flag->default_val.i64;
738 static void log_value(void *av_log_obj, int level, double d)
741 av_log(av_log_obj, level, "INT_MAX");
742 } else if (d == INT_MIN) {
743 av_log(av_log_obj, level, "INT_MIN");
744 } else if (d == (double)INT64_MAX) {
745 av_log(av_log_obj, level, "I64_MAX");
746 } else if (d == INT64_MIN) {
747 av_log(av_log_obj, level, "I64_MIN");
748 } else if (d == FLT_MAX) {
749 av_log(av_log_obj, level, "FLT_MAX");
750 } else if (d == FLT_MIN) {
751 av_log(av_log_obj, level, "FLT_MIN");
753 av_log(av_log_obj, level, "%7.2g", d);
757 static void opt_list(void *obj, void *av_log_obj, const char *unit,
758 int req_flags, int rej_flags)
760 const AVOption *opt=NULL;
764 while ((opt = av_opt_next(obj, opt))) {
765 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
768 /* Don't print CONST's on level one.
769 * Don't print anything but CONST's on level two.
770 * Only print items from the requested unit.
772 if (!unit && opt->type==AV_OPT_TYPE_CONST)
774 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
776 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
778 else if (unit && opt->type == AV_OPT_TYPE_CONST)
779 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
781 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
784 case AV_OPT_TYPE_FLAGS:
785 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
787 case AV_OPT_TYPE_INT:
788 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
790 case AV_OPT_TYPE_INT64:
791 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
793 case AV_OPT_TYPE_DOUBLE:
794 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
796 case AV_OPT_TYPE_FLOAT:
797 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
799 case AV_OPT_TYPE_STRING:
800 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
802 case AV_OPT_TYPE_RATIONAL:
803 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
805 case AV_OPT_TYPE_BINARY:
806 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
808 case AV_OPT_TYPE_IMAGE_SIZE:
809 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
811 case AV_OPT_TYPE_PIXEL_FMT:
812 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
814 case AV_OPT_TYPE_SAMPLE_FMT:
815 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
817 case AV_OPT_TYPE_CONST:
819 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
822 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
823 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
824 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
825 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
826 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
827 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
829 if(av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
831 case AV_OPT_TYPE_INT:
832 case AV_OPT_TYPE_INT64:
833 case AV_OPT_TYPE_DOUBLE:
834 case AV_OPT_TYPE_FLOAT:
835 case AV_OPT_TYPE_RATIONAL:
836 for (i=0; i<r->nb_ranges; i++) {
837 av_log(av_log_obj, AV_LOG_INFO, "[");
838 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
839 av_log(av_log_obj, AV_LOG_INFO, ", ");
840 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
841 av_log(av_log_obj, AV_LOG_INFO, "]");
845 av_opt_freep_ranges(&r);
849 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
850 av_log(av_log_obj, AV_LOG_INFO, "\n");
851 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
852 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
857 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
862 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
864 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
869 void av_opt_set_defaults(void *s)
871 #if FF_API_OLD_AVOPTIONS
872 av_opt_set_defaults2(s, 0, 0);
875 void av_opt_set_defaults2(void *s, int mask, int flags)
878 const AVClass *class = *(AVClass **)s;
879 const AVOption *opt = NULL;
880 while ((opt = av_opt_next(s, opt)) != NULL) {
881 #if FF_API_OLD_AVOPTIONS
882 if ((opt->flags & mask) != flags)
886 case AV_OPT_TYPE_CONST:
887 /* Nothing to be done here */
889 case AV_OPT_TYPE_FLAGS:
890 case AV_OPT_TYPE_INT:
891 case AV_OPT_TYPE_INT64:
892 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
894 case AV_OPT_TYPE_DOUBLE:
895 case AV_OPT_TYPE_FLOAT: {
897 val = opt->default_val.dbl;
898 av_opt_set_double(s, opt->name, val, 0);
901 case AV_OPT_TYPE_RATIONAL: {
903 val = av_d2q(opt->default_val.dbl, INT_MAX);
904 av_opt_set_q(s, opt->name, val, 0);
907 case AV_OPT_TYPE_STRING:
908 case AV_OPT_TYPE_IMAGE_SIZE:
909 av_opt_set(s, opt->name, opt->default_val.str, 0);
911 case AV_OPT_TYPE_PIXEL_FMT:
912 #if LIBAVUTIL_VERSION_MAJOR < 53
913 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
914 av_opt_set(s, opt->name, opt->default_val.str, 0);
917 av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);
919 case AV_OPT_TYPE_SAMPLE_FMT:
920 #if LIBAVUTIL_VERSION_MAJOR < 53
921 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
922 av_opt_set(s, opt->name, opt->default_val.str, 0);
925 av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);
927 case AV_OPT_TYPE_BINARY:
928 /* Cannot set default for binary */
931 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
937 * Store the value in the field in ctx that is named like key.
938 * ctx must be an AVClass context, storing is done using AVOptions.
940 * @param buf the string to parse, buf will be updated to point at the
941 * separator just after the parsed key/value pair
942 * @param key_val_sep a 0-terminated list of characters used to
943 * separate key from value
944 * @param pairs_sep a 0-terminated list of characters used to separate
945 * two pairs from each other
946 * @return 0 if the key/value pair has been successfully parsed and
947 * set, or a negative value corresponding to an AVERROR code in case
949 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
950 * the error code issued by av_opt_set() if the key/value pair
953 static int parse_key_value_pair(void *ctx, const char **buf,
954 const char *key_val_sep, const char *pairs_sep)
956 char *key = av_get_token(buf, key_val_sep);
960 if (*key && strspn(*buf, key_val_sep)) {
962 val = av_get_token(buf, pairs_sep);
964 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
966 return AVERROR(EINVAL);
969 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
971 ret = av_opt_set(ctx, key, val, 0);
972 if (ret == AVERROR_OPTION_NOT_FOUND)
973 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
980 int av_set_options_string(void *ctx, const char *opts,
981 const char *key_val_sep, const char *pairs_sep)
989 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1000 #define WHITESPACES " \n\t"
1002 static int is_key_char(char c)
1004 return (unsigned)((c | 32) - 'a') < 26 ||
1005 (unsigned)(c - '0') < 10 ||
1006 c == '-' || c == '_' || c == '/' || c == '.';
1010 * Read a key from a string.
1012 * The key consists of is_key_char characters and must be terminated by a
1013 * character from the delim string; spaces are ignored.
1015 * @return 0 for success (even with ellipsis), <0 for failure
1017 static int get_key(const char **ropts, const char *delim, char **rkey)
1019 const char *opts = *ropts;
1020 const char *key_start, *key_end;
1022 key_start = opts += strspn(opts, WHITESPACES);
1023 while (is_key_char(*opts))
1026 opts += strspn(opts, WHITESPACES);
1027 if (!*opts || !strchr(delim, *opts))
1028 return AVERROR(EINVAL);
1030 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1031 return AVERROR(ENOMEM);
1032 memcpy(*rkey, key_start, key_end - key_start);
1033 (*rkey)[key_end - key_start] = 0;
1038 int av_opt_get_key_value(const char **ropts,
1039 const char *key_val_sep, const char *pairs_sep,
1041 char **rkey, char **rval)
1044 char *key = NULL, *val;
1045 const char *opts = *ropts;
1047 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1048 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1049 return AVERROR(EINVAL);
1050 if (!(val = av_get_token(&opts, pairs_sep))) {
1052 return AVERROR(ENOMEM);
1060 int av_opt_set_from_string(void *ctx, const char *opts,
1061 const char *const *shorthand,
1062 const char *key_val_sep, const char *pairs_sep)
1065 const char *dummy_shorthand = NULL;
1066 char *av_uninit(parsed_key), *av_uninit(value);
1072 shorthand = &dummy_shorthand;
1075 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1076 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1077 &parsed_key, &value);
1079 if (ret == AVERROR(EINVAL))
1080 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1082 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1090 while (*shorthand) /* discard all remaining shorthand */
1093 key = *(shorthand++);
1096 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1097 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1098 if (ret == AVERROR_OPTION_NOT_FOUND)
1099 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1101 av_free(parsed_key);
1106 av_free(parsed_key);
1112 void av_opt_free(void *obj)
1114 const AVOption *o = NULL;
1115 while ((o = av_opt_next(obj, o)))
1116 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1117 av_freep((uint8_t *)obj + o->offset);
1120 int av_opt_set_dict(void *obj, AVDictionary **options)
1122 AVDictionaryEntry *t = NULL;
1123 AVDictionary *tmp = NULL;
1126 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1127 ret = av_opt_set(obj, t->key, t->value, 0);
1128 if (ret == AVERROR_OPTION_NOT_FOUND)
1129 av_dict_set(&tmp, t->key, t->value, 0);
1131 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1136 av_dict_free(options);
1141 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1142 int opt_flags, int search_flags)
1144 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1147 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1148 int opt_flags, int search_flags, void **target_obj)
1151 const AVOption *o = NULL;
1158 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1159 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1160 const AVClass *child = NULL;
1161 while (child = av_opt_child_class_next(c, child))
1162 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1166 while (child = av_opt_child_next(obj, child))
1167 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1172 while (o = av_opt_next(obj, o)) {
1173 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1174 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1175 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1177 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1188 void *av_opt_child_next(void *obj, void *prev)
1190 const AVClass *c = *(AVClass**)obj;
1192 return c->child_next(obj, prev);
1196 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1198 if (parent->child_class_next)
1199 return parent->child_class_next(prev);
1203 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1205 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1208 return (uint8_t*)obj + opt->offset;
1211 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags) {
1212 const AVClass *c = *(AVClass**)obj;
1213 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1215 if(c->version > (52 << 16 | 11 << 8))
1216 callback = c->query_ranges;
1219 callback = av_opt_query_ranges_default;
1221 return callback(ranges_arg, obj, key, flags);
1224 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags) {
1225 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1226 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1227 AVOptionRange *range = av_mallocz(sizeof(*range));
1228 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1232 if(!ranges || !range || !range_array || !field)
1235 ranges->range = range_array;
1236 ranges->range[0] = range;
1237 ranges->nb_ranges = 1;
1238 range->is_range = 1;
1239 range->value_min = field->min;
1240 range->value_max = field->max;
1242 switch(field->type){
1243 case AV_OPT_TYPE_INT:
1244 case AV_OPT_TYPE_INT64:
1245 case AV_OPT_TYPE_PIXEL_FMT:
1246 case AV_OPT_TYPE_SAMPLE_FMT:
1247 case AV_OPT_TYPE_FLOAT:
1248 case AV_OPT_TYPE_DOUBLE:
1250 case AV_OPT_TYPE_STRING:
1251 range->component_min = 0;
1252 range->component_max = 0x10FFFF; // max unicode value
1253 range->value_min = -1;
1254 range->value_max = INT_MAX;
1256 case AV_OPT_TYPE_RATIONAL:
1257 range->component_min = INT_MIN;
1258 range->component_max = INT_MAX;
1260 case AV_OPT_TYPE_IMAGE_SIZE:
1261 range->component_min = 0;
1262 range->component_max = INT_MAX/128/8;
1263 range->value_min = 0;
1264 range->value_max = INT_MAX/8;
1270 *ranges_arg = ranges;
1278 void av_opt_freep_ranges(AVOptionRanges **rangesp) {
1280 AVOptionRanges *ranges = *rangesp;
1282 for(i=0; i<ranges->nb_ranges; i++){
1283 AVOptionRange *range = ranges->range[i];
1284 av_freep(&range->str);
1285 av_freep(&ranges->range[i]);
1287 av_freep(&ranges->range);
1293 typedef struct TestContext
1295 const AVClass *class;
1300 AVRational rational;
1302 enum AVPixelFormat pix_fmt;
1303 enum AVSampleFormat sample_fmt;
1306 #define OFFSET(x) offsetof(TestContext, x)
1308 #define TEST_FLAG_COOL 01
1309 #define TEST_FLAG_LAME 02
1310 #define TEST_FLAG_MU 04
1312 static const AVOption test_options[]= {
1313 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1314 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1315 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1316 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
1317 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1318 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1319 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1320 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1321 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1322 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
1323 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
1327 static const char *test_get_name(void *ctx)
1332 static const AVClass test_class = {
1342 printf("\nTesting av_set_options_string()\n");
1344 TestContext test_ctx = { 0 };
1345 const char *options[] = {
1361 "flags=+mu-lame : num=42: toggle=0",
1362 "num=42 : string=blahblah",
1363 "rational=0 : rational=1/2 : rational=1/-1",
1376 test_ctx.class = &test_class;
1377 av_opt_set_defaults(&test_ctx);
1378 test_ctx.string = av_strdup("default");
1380 av_log_set_level(AV_LOG_DEBUG);
1382 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1383 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1384 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1385 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1388 av_freep(&test_ctx.string);
1391 printf("\nTesting av_opt_set_from_string()\n");
1393 TestContext test_ctx = { 0 };
1394 const char *options[] = {
1402 " 5 : hello : size = pal ",
1403 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1405 const char *shorthand[] = { "num", "string", NULL };
1407 test_ctx.class = &test_class;
1408 av_opt_set_defaults(&test_ctx);
1409 test_ctx.string = av_strdup("default");
1411 av_log_set_level(AV_LOG_DEBUG);
1413 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1414 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1415 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1416 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1419 av_freep(&test_ctx.string);