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"
41 //FIXME order them and do a bin search
42 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
44 const AVOption *o = NULL;
46 while ((o = av_next_option(v, o))) {
47 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
54 #if FF_API_OLD_AVOPTIONS
55 const AVOption *av_next_option(void *obj, const AVOption *last)
57 return av_opt_next(obj, last);
61 const AVOption *av_opt_next(void *obj, const AVOption *last)
63 AVClass *class = *(AVClass**)obj;
64 if (!last && class->option && class->option[0].name)
66 if (last && last[1].name) return ++last;
70 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
73 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
74 case AV_OPT_TYPE_PIXEL_FMT:
75 case AV_OPT_TYPE_SAMPLE_FMT:
76 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
77 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
78 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
79 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
80 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
81 *den = ((AVRational*)dst)->den;
83 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
85 return AVERROR(EINVAL);
88 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
90 if (o->max*den < num*intnum || o->min*den > num*intnum) {
91 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
92 num*intnum/den, o->name, o->min, o->max);
93 return AVERROR(ERANGE);
97 case AV_OPT_TYPE_FLAGS:
98 case AV_OPT_TYPE_PIXEL_FMT:
99 case AV_OPT_TYPE_SAMPLE_FMT:
100 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
101 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
102 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
103 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
104 case AV_OPT_TYPE_RATIONAL:
105 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
106 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
109 return AVERROR(EINVAL);
114 static const double const_values[] = {
121 static const char * const const_names[] = {
128 static int hexchar2int(char c) {
129 if (c >= '0' && c <= '9') return c - '0';
130 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
131 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
135 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
137 int *lendst = (int *)(dst + 1);
139 int len = strlen(val);
145 return AVERROR(EINVAL);
148 ptr = bin = av_malloc(len);
150 int a = hexchar2int(*val++);
151 int b = hexchar2int(*val++);
152 if (a < 0 || b < 0) {
154 return AVERROR(EINVAL);
156 *ptr++ = (a << 4) | b;
164 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
167 *dst = av_strdup(val);
171 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
172 opt->type == AV_OPT_TYPE_CONST || \
173 opt->type == AV_OPT_TYPE_FLAGS || \
174 opt->type == AV_OPT_TYPE_INT) ? \
175 opt->default_val.i64 : opt->default_val.dbl)
177 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
179 int ret = 0, notfirst = 0;
188 if (*val == '+' || *val == '-') {
189 if (o->type == AV_OPT_TYPE_FLAGS)
195 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
200 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
201 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
202 d = DEFAULT_NUMVAL(o_named);
203 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
204 else if (!strcmp(buf, "max" )) d = o->max;
205 else if (!strcmp(buf, "min" )) d = o->min;
206 else if (!strcmp(buf, "none" )) d = 0;
207 else if (!strcmp(buf, "all" )) d = ~0;
209 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
211 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
216 if (o->type == AV_OPT_TYPE_FLAGS) {
217 read_number(o, dst, NULL, NULL, &intnum);
218 if (cmd == '+') d = intnum | (int64_t)d;
219 else if (cmd == '-') d = intnum &~(int64_t)d;
221 read_number(o, dst, &num, &den, &intnum);
222 if (cmd == '+') d = notfirst*num*intnum/den + d;
223 else if (cmd == '-') d = notfirst*num*intnum/den - d;
226 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
237 #if FF_API_OLD_AVOPTIONS
238 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
240 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
243 return av_opt_set(obj, name, val, 0);
247 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
250 void *dst, *target_obj;
251 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
252 if (!o || !target_obj)
253 return AVERROR_OPTION_NOT_FOUND;
254 if (!val && (o->type != AV_OPT_TYPE_STRING &&
255 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
256 o->type != AV_OPT_TYPE_IMAGE_SIZE))
257 return AVERROR(EINVAL);
259 dst = ((uint8_t*)target_obj) + o->offset;
261 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
262 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
263 case AV_OPT_TYPE_FLAGS:
264 case AV_OPT_TYPE_INT:
265 case AV_OPT_TYPE_INT64:
266 case AV_OPT_TYPE_FLOAT:
267 case AV_OPT_TYPE_DOUBLE:
268 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
269 case AV_OPT_TYPE_IMAGE_SIZE:
270 if (!val || !strcmp(val, "none")) {
271 *(int *)dst = *((int *)dst + 1) = 0;
274 ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
276 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
278 case AV_OPT_TYPE_PIXEL_FMT:
279 if (!val || !strcmp(val, "none")) {
280 ret = AV_PIX_FMT_NONE;
282 ret = av_get_pix_fmt(val);
283 if (ret == AV_PIX_FMT_NONE) {
285 ret = strtol(val, &tail, 0);
286 if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
287 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
288 return AVERROR(EINVAL);
292 *(enum AVPixelFormat *)dst = ret;
294 case AV_OPT_TYPE_SAMPLE_FMT:
295 if (!val || !strcmp(val, "none")) {
296 ret = AV_SAMPLE_FMT_NONE;
298 ret = av_get_sample_fmt(val);
299 if (ret == AV_SAMPLE_FMT_NONE) {
301 ret = strtol(val, &tail, 0);
302 if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
303 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
304 return AVERROR(EINVAL);
308 *(enum AVSampleFormat *)dst = ret;
312 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
313 return AVERROR(EINVAL);
316 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
317 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
319 if (!o || o->type != opttype)\
320 return AVERROR(EINVAL);\
321 return set_string_number(obj, o, val, name ## _out);\
324 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
325 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
326 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
327 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
328 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
329 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
331 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
334 void *dst, *target_obj;
335 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
337 if (!o || !target_obj)
338 return AVERROR_OPTION_NOT_FOUND;
340 dst = ((uint8_t*)target_obj) + o->offset;
341 return write_number(obj, o, dst, num, den, intnum);
344 #if FF_API_OLD_AVOPTIONS
345 const AVOption *av_set_double(void *obj, const char *name, double n)
347 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
348 if (set_number(obj, name, n, 1, 1, 0) < 0)
353 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
355 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
356 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
361 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
363 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
364 if (set_number(obj, name, 1, 1, n, 0) < 0)
370 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
372 return set_number(obj, name, 1, 1, val, search_flags);
375 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
377 return set_number(obj, name, val, 1, 1, search_flags);
380 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
382 return set_number(obj, name, val.num, val.den, 1, search_flags);
385 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
388 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
393 if (!o || !target_obj)
394 return AVERROR_OPTION_NOT_FOUND;
396 if (o->type != AV_OPT_TYPE_BINARY)
397 return AVERROR(EINVAL);
399 ptr = av_malloc(len);
401 return AVERROR(ENOMEM);
403 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
404 lendst = (int *)(dst + 1);
409 memcpy(ptr, val, len);
414 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
417 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
419 if (!o || !target_obj)
420 return AVERROR_OPTION_NOT_FOUND;
421 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
422 av_log(obj, AV_LOG_ERROR,
423 "The value set by option '%s' is not an image size.\n", o->name);
424 return AVERROR(EINVAL);
427 av_log(obj, AV_LOG_ERROR,
428 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
429 return AVERROR(EINVAL);
431 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
432 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
436 static int set_format(void *obj, const char *name, int fmt, int search_flags,
437 enum AVOptionType type, const char *desc, int nb_fmts)
440 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
441 search_flags, &target_obj);
443 const AVClass *class = *(AVClass **)obj;
445 if (!o || !target_obj)
446 return AVERROR_OPTION_NOT_FOUND;
447 if (o->type != type) {
448 av_log(obj, AV_LOG_ERROR,
449 "The value set by option '%s' is not a %s format", name, desc);
450 return AVERROR(EINVAL);
453 #if LIBAVUTIL_VERSION_MAJOR < 53
454 if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
460 min = FFMIN(o->min, -1);
461 max = FFMAX(o->max, nb_fmts-1);
463 if (fmt < min || fmt > max) {
464 av_log(obj, AV_LOG_ERROR,
465 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
466 fmt, name, desc, min, max);
467 return AVERROR(ERANGE);
469 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
473 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
475 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
478 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
480 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
483 #if FF_API_OLD_AVOPTIONS
486 * @param buf a buffer which is used for returning non string values as strings, can be NULL
487 * @param buf_len allocated length in bytes of buf
489 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
491 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
497 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
500 dst= ((uint8_t*)obj) + o->offset;
501 if (o_out) *o_out= o;
504 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
505 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
506 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
507 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
508 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
509 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
510 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
511 case AV_OPT_TYPE_STRING: return *(void**)dst;
512 case AV_OPT_TYPE_BINARY:
513 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
514 if (len >= (buf_len + 1)/2) return NULL;
515 bin = *(uint8_t**)dst;
516 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
518 default: return NULL;
524 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
526 void *dst, *target_obj;
527 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
528 uint8_t *bin, buf[128];
531 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
532 return AVERROR_OPTION_NOT_FOUND;
534 dst = (uint8_t*)target_obj + o->offset;
538 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
539 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
540 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
541 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
542 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
543 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
544 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
545 case AV_OPT_TYPE_STRING:
547 *out_val = av_strdup(*(uint8_t**)dst);
549 *out_val = av_strdup("");
551 case AV_OPT_TYPE_BINARY:
552 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
553 if ((uint64_t)len*2 + 1 > INT_MAX)
554 return AVERROR(EINVAL);
555 if (!(*out_val = av_malloc(len*2 + 1)))
556 return AVERROR(ENOMEM);
557 bin = *(uint8_t**)dst;
558 for (i = 0; i < len; i++)
559 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
561 case AV_OPT_TYPE_IMAGE_SIZE:
562 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
564 case AV_OPT_TYPE_PIXEL_FMT:
565 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
567 case AV_OPT_TYPE_SAMPLE_FMT:
568 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
571 return AVERROR(EINVAL);
574 if (ret >= sizeof(buf))
575 return AVERROR(EINVAL);
576 *out_val = av_strdup(buf);
580 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
583 void *dst, *target_obj;
584 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
585 if (!o || !target_obj)
588 dst = ((uint8_t*)target_obj) + o->offset;
590 if (o_out) *o_out= o;
592 return read_number(o, dst, num, den, intnum);
599 #if FF_API_OLD_AVOPTIONS
600 double av_get_double(void *obj, const char *name, const AVOption **o_out)
606 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
608 return num*intnum/den;
611 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
617 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
618 return (AVRational){0, 0};
619 if (num == 1.0 && (int)intnum == intnum)
620 return (AVRational){intnum, den};
622 return av_d2q(num*intnum/den, 1<<24);
625 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
631 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
633 return num*intnum/den;
637 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
643 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
645 *out_val = num*intnum/den;
649 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
655 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
657 *out_val = num*intnum/den;
661 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
667 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
670 if (num == 1.0 && (int)intnum == intnum)
671 *out_val = (AVRational){intnum, den};
673 *out_val = av_d2q(num*intnum/den, 1<<24);
677 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
679 void *dst, *target_obj;
680 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
681 if (!o || !target_obj)
682 return AVERROR_OPTION_NOT_FOUND;
683 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
684 av_log(obj, AV_LOG_ERROR,
685 "The value for option '%s' is not an image size.\n", name);
686 return AVERROR(EINVAL);
689 dst = ((uint8_t*)target_obj) + o->offset;
690 if (w_out) *w_out = *(int *)dst;
691 if (h_out) *h_out = *((int *)dst+1);
695 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
696 enum AVOptionType type, const char *desc)
698 void *dst, *target_obj;
699 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
700 if (!o || !target_obj)
701 return AVERROR_OPTION_NOT_FOUND;
702 if (o->type != type) {
703 av_log(obj, AV_LOG_ERROR,
704 "The value for option '%s' is not a %s format.\n", desc, name);
705 return AVERROR(EINVAL);
708 dst = ((uint8_t*)target_obj) + o->offset;
709 *out_fmt = *(int *)dst;
713 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
715 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
718 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
720 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
723 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
725 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
726 const AVOption *flag = av_opt_find(obj, flag_name,
727 field ? field->unit : NULL, 0, 0);
730 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
731 av_opt_get_int(obj, field_name, 0, &res) < 0)
733 return res & flag->default_val.i64;
736 static void opt_list(void *obj, void *av_log_obj, const char *unit,
737 int req_flags, int rej_flags)
739 const AVOption *opt=NULL;
741 while ((opt = av_opt_next(obj, opt))) {
742 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
745 /* Don't print CONST's on level one.
746 * Don't print anything but CONST's on level two.
747 * Only print items from the requested unit.
749 if (!unit && opt->type==AV_OPT_TYPE_CONST)
751 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
753 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
755 else if (unit && opt->type == AV_OPT_TYPE_CONST)
756 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
758 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
761 case AV_OPT_TYPE_FLAGS:
762 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
764 case AV_OPT_TYPE_INT:
765 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
767 case AV_OPT_TYPE_INT64:
768 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
770 case AV_OPT_TYPE_DOUBLE:
771 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
773 case AV_OPT_TYPE_FLOAT:
774 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
776 case AV_OPT_TYPE_STRING:
777 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
779 case AV_OPT_TYPE_RATIONAL:
780 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
782 case AV_OPT_TYPE_BINARY:
783 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
785 case AV_OPT_TYPE_IMAGE_SIZE:
786 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
788 case AV_OPT_TYPE_PIXEL_FMT:
789 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<pix_fmt>");
791 case AV_OPT_TYPE_SAMPLE_FMT:
792 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<sample_fmt>");
794 case AV_OPT_TYPE_CONST:
796 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
799 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
800 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
801 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
802 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
803 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
804 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
807 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
808 av_log(av_log_obj, AV_LOG_INFO, "\n");
809 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
810 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
815 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
820 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
822 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
827 void av_opt_set_defaults(void *s)
829 #if FF_API_OLD_AVOPTIONS
830 av_opt_set_defaults2(s, 0, 0);
833 void av_opt_set_defaults2(void *s, int mask, int flags)
836 const AVClass *class = *(AVClass **)s;
837 const AVOption *opt = NULL;
838 while ((opt = av_opt_next(s, opt)) != NULL) {
839 #if FF_API_OLD_AVOPTIONS
840 if ((opt->flags & mask) != flags)
844 case AV_OPT_TYPE_CONST:
845 /* Nothing to be done here */
847 case AV_OPT_TYPE_FLAGS:
848 case AV_OPT_TYPE_INT:
849 case AV_OPT_TYPE_INT64:
850 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
852 case AV_OPT_TYPE_DOUBLE:
853 case AV_OPT_TYPE_FLOAT: {
855 val = opt->default_val.dbl;
856 av_opt_set_double(s, opt->name, val, 0);
859 case AV_OPT_TYPE_RATIONAL: {
861 val = av_d2q(opt->default_val.dbl, INT_MAX);
862 av_opt_set_q(s, opt->name, val, 0);
865 case AV_OPT_TYPE_STRING:
866 case AV_OPT_TYPE_IMAGE_SIZE:
867 av_opt_set(s, opt->name, opt->default_val.str, 0);
869 case AV_OPT_TYPE_PIXEL_FMT:
870 #if LIBAVUTIL_VERSION_MAJOR < 53
871 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
872 av_opt_set(s, opt->name, opt->default_val.str, 0);
875 av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);
877 case AV_OPT_TYPE_SAMPLE_FMT:
878 #if LIBAVUTIL_VERSION_MAJOR < 53
879 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
880 av_opt_set(s, opt->name, opt->default_val.str, 0);
883 av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);
885 case AV_OPT_TYPE_BINARY:
886 /* Cannot set default for binary */
889 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
895 * Store the value in the field in ctx that is named like key.
896 * ctx must be an AVClass context, storing is done using AVOptions.
898 * @param buf the string to parse, buf will be updated to point at the
899 * separator just after the parsed key/value pair
900 * @param key_val_sep a 0-terminated list of characters used to
901 * separate key from value
902 * @param pairs_sep a 0-terminated list of characters used to separate
903 * two pairs from each other
904 * @return 0 if the key/value pair has been successfully parsed and
905 * set, or a negative value corresponding to an AVERROR code in case
907 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
908 * the error code issued by av_opt_set() if the key/value pair
911 static int parse_key_value_pair(void *ctx, const char **buf,
912 const char *key_val_sep, const char *pairs_sep)
914 char *key = av_get_token(buf, key_val_sep);
918 if (*key && strspn(*buf, key_val_sep)) {
920 val = av_get_token(buf, pairs_sep);
922 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
924 return AVERROR(EINVAL);
927 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
929 ret = av_opt_set(ctx, key, val, 0);
930 if (ret == AVERROR_OPTION_NOT_FOUND)
931 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
938 int av_set_options_string(void *ctx, const char *opts,
939 const char *key_val_sep, const char *pairs_sep)
947 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
958 #define WHITESPACES " \n\t"
960 static int is_key_char(char c)
962 return (unsigned)((c | 32) - 'a') < 26 ||
963 (unsigned)(c - '0') < 10 ||
964 c == '-' || c == '_' || c == '/' || c == '.';
968 * Read a key from a string.
970 * The key consists of is_key_char characters and must be terminated by a
971 * character from the delim string; spaces are ignored.
973 * @return 0 for success (even with ellipsis), <0 for failure
975 static int get_key(const char **ropts, const char *delim, char **rkey)
977 const char *opts = *ropts;
978 const char *key_start, *key_end;
980 key_start = opts += strspn(opts, WHITESPACES);
981 while (is_key_char(*opts))
984 opts += strspn(opts, WHITESPACES);
985 if (!*opts || !strchr(delim, *opts))
986 return AVERROR(EINVAL);
988 if (!(*rkey = av_malloc(key_end - key_start + 1)))
989 return AVERROR(ENOMEM);
990 memcpy(*rkey, key_start, key_end - key_start);
991 (*rkey)[key_end - key_start] = 0;
996 int av_opt_get_key_value(const char **ropts,
997 const char *key_val_sep, const char *pairs_sep,
999 char **rkey, char **rval)
1002 char *key = NULL, *val;
1003 const char *opts = *ropts;
1005 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1006 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1007 return AVERROR(EINVAL);
1008 if (!(val = av_get_token(&opts, pairs_sep))) {
1010 return AVERROR(ENOMEM);
1018 int av_opt_set_from_string(void *ctx, const char *opts,
1019 const char *const *shorthand,
1020 const char *key_val_sep, const char *pairs_sep)
1023 const char *dummy_shorthand = NULL;
1024 char *av_uninit(parsed_key), *av_uninit(value);
1030 shorthand = &dummy_shorthand;
1033 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1034 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1035 &parsed_key, &value);
1037 if (ret == AVERROR(EINVAL))
1038 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1040 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1048 while (*shorthand) /* discard all remaining shorthand */
1051 key = *(shorthand++);
1054 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1055 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1056 if (ret == AVERROR_OPTION_NOT_FOUND)
1057 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1059 av_free(parsed_key);
1064 av_free(parsed_key);
1070 void av_opt_free(void *obj)
1072 const AVOption *o = NULL;
1073 while ((o = av_opt_next(obj, o)))
1074 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1075 av_freep((uint8_t *)obj + o->offset);
1078 int av_opt_set_dict(void *obj, AVDictionary **options)
1080 AVDictionaryEntry *t = NULL;
1081 AVDictionary *tmp = NULL;
1084 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1085 ret = av_opt_set(obj, t->key, t->value, 0);
1086 if (ret == AVERROR_OPTION_NOT_FOUND)
1087 av_dict_set(&tmp, t->key, t->value, 0);
1089 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1094 av_dict_free(options);
1099 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1100 int opt_flags, int search_flags)
1102 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1105 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1106 int opt_flags, int search_flags, void **target_obj)
1109 const AVOption *o = NULL;
1116 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1117 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1118 const AVClass *child = NULL;
1119 while (child = av_opt_child_class_next(c, child))
1120 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1124 while (child = av_opt_child_next(obj, child))
1125 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1130 while (o = av_opt_next(obj, o)) {
1131 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1132 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1133 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1135 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1146 void *av_opt_child_next(void *obj, void *prev)
1148 const AVClass *c = *(AVClass**)obj;
1150 return c->child_next(obj, prev);
1154 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1156 if (parent->child_class_next)
1157 return parent->child_class_next(prev);
1161 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1163 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1166 return (uint8_t*)obj + opt->offset;
1171 typedef struct TestContext
1173 const AVClass *class;
1178 AVRational rational;
1180 enum AVPixelFormat pix_fmt;
1181 enum AVSampleFormat sample_fmt;
1184 #define OFFSET(x) offsetof(TestContext, x)
1186 #define TEST_FLAG_COOL 01
1187 #define TEST_FLAG_LAME 02
1188 #define TEST_FLAG_MU 04
1190 static const AVOption test_options[]= {
1191 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1192 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1193 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1194 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
1195 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1196 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1197 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1198 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1199 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1200 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
1201 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
1205 static const char *test_get_name(void *ctx)
1210 static const AVClass test_class = {
1220 printf("\nTesting av_set_options_string()\n");
1222 TestContext test_ctx = { 0 };
1223 const char *options[] = {
1239 "flags=+mu-lame : num=42: toggle=0",
1240 "num=42 : string=blahblah",
1241 "rational=0 : rational=1/2 : rational=1/-1",
1254 test_ctx.class = &test_class;
1255 av_opt_set_defaults(&test_ctx);
1256 test_ctx.string = av_strdup("default");
1258 av_log_set_level(AV_LOG_DEBUG);
1260 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1261 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1262 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1263 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1266 av_freep(&test_ctx.string);
1269 printf("\nTesting av_opt_set_from_string()\n");
1271 TestContext test_ctx = { 0 };
1272 const char *options[] = {
1280 " 5 : hello : size = pal ",
1281 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1283 const char *shorthand[] = { "num", "string", NULL };
1285 test_ctx.class = &test_class;
1286 av_opt_set_defaults(&test_ctx);
1287 test_ctx.string = av_strdup("default");
1289 av_log_set_level(AV_LOG_DEBUG);
1291 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1292 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1293 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1294 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1297 av_freep(&test_ctx.string);