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>
30 #include "channel_layout.h"
36 #include "parseutils.h"
38 #include "mathematics.h"
39 #include "samplefmt.h"
44 //FIXME order them and do a bin search
45 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
47 const AVOption *o = NULL;
49 while ((o = av_next_option(v, o))) {
50 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
57 #if FF_API_OLD_AVOPTIONS
58 const AVOption *av_next_option(void *obj, const AVOption *last)
60 return av_opt_next(obj, last);
64 const AVOption *av_opt_next(void *obj, const AVOption *last)
66 AVClass *class = *(AVClass**)obj;
67 if (!last && class && class->option && class->option[0].name)
69 if (last && last[1].name)
74 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
77 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
78 case AV_OPT_TYPE_PIXEL_FMT:
79 case AV_OPT_TYPE_SAMPLE_FMT:
80 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
81 case AV_OPT_TYPE_CHANNEL_LAYOUT:
82 case AV_OPT_TYPE_DURATION:
83 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
84 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
85 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
86 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
87 *den = ((AVRational*)dst)->den;
89 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
91 return AVERROR(EINVAL);
94 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
96 if (o->type != AV_OPT_TYPE_FLAGS &&
97 (o->max * den < num * intnum || o->min * den > num * intnum)) {
98 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
99 num*intnum/den, o->name, o->min, o->max);
100 return AVERROR(ERANGE);
102 if (o->type == AV_OPT_TYPE_FLAGS) {
103 double d = num*intnum/den;
104 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
105 av_log(obj, AV_LOG_ERROR,
106 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
107 num*intnum/den, o->name);
108 return AVERROR(ERANGE);
113 case AV_OPT_TYPE_FLAGS:
114 case AV_OPT_TYPE_PIXEL_FMT:
115 case AV_OPT_TYPE_SAMPLE_FMT:
116 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
117 case AV_OPT_TYPE_DURATION:
118 case AV_OPT_TYPE_CHANNEL_LAYOUT:
119 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
120 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
121 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
122 case AV_OPT_TYPE_RATIONAL:
123 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
124 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
127 return AVERROR(EINVAL);
132 static const double const_values[] = {
139 static const char * const const_names[] = {
146 static int hexchar2int(char c) {
147 if (c >= '0' && c <= '9') return c - '0';
148 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
149 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
153 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
155 int *lendst = (int *)(dst + 1);
157 int len = strlen(val);
163 return AVERROR(EINVAL);
166 ptr = bin = av_malloc(len);
168 int a = hexchar2int(*val++);
169 int b = hexchar2int(*val++);
170 if (a < 0 || b < 0) {
172 return AVERROR(EINVAL);
174 *ptr++ = (a << 4) | b;
182 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
185 *dst = av_strdup(val);
189 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
190 opt->type == AV_OPT_TYPE_CONST || \
191 opt->type == AV_OPT_TYPE_FLAGS || \
192 opt->type == AV_OPT_TYPE_INT) ? \
193 opt->default_val.i64 : opt->default_val.dbl)
195 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
197 int ret = 0, notfirst = 0;
206 if (*val == '+' || *val == '-') {
207 if (o->type == AV_OPT_TYPE_FLAGS)
213 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
218 const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
219 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
220 d = DEFAULT_NUMVAL(o_named);
221 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
222 else if (!strcmp(buf, "max" )) d = o->max;
223 else if (!strcmp(buf, "min" )) d = o->min;
224 else if (!strcmp(buf, "none" )) d = 0;
225 else if (!strcmp(buf, "all" )) d = ~0;
227 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
229 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
234 if (o->type == AV_OPT_TYPE_FLAGS) {
235 read_number(o, dst, NULL, NULL, &intnum);
236 if (cmd == '+') d = intnum | (int64_t)d;
237 else if (cmd == '-') d = intnum &~(int64_t)d;
239 read_number(o, dst, &num, &den, &intnum);
240 if (cmd == '+') d = notfirst*num*intnum/den + d;
241 else if (cmd == '-') d = notfirst*num*intnum/den - d;
244 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
255 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
259 if (!val || !strcmp(val, "none")) {
264 ret = av_parse_video_size(dst, dst + 1, val);
266 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
270 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
274 ret = AVERROR(EINVAL);
276 ret = av_parse_video_rate(dst, val);
279 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
283 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
290 ret = av_parse_color(dst, val, -1, obj);
292 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
298 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
299 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
303 if (!val || !strcmp(val, "none")) {
309 fmt = strtol(val, &tail, 0);
310 if (*tail || (unsigned)fmt >= fmt_nb) {
311 av_log(obj, AV_LOG_ERROR,
312 "Unable to parse option value \"%s\" as %s\n", val, desc);
313 return AVERROR(EINVAL);
318 min = FFMAX(o->min, -1);
319 max = FFMIN(o->max, fmt_nb-1);
321 if (fmt < min || fmt > max) {
322 av_log(obj, AV_LOG_ERROR,
323 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
324 fmt, o->name, desc, min, max);
325 return AVERROR(ERANGE);
332 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
334 return set_string_fmt(obj, o, val, dst,
335 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
338 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
340 return set_string_fmt(obj, o, val, dst,
341 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
344 #if FF_API_OLD_AVOPTIONS
345 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
347 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
350 return av_opt_set(obj, name, val, 0);
354 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
357 void *dst, *target_obj;
358 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
359 if (!o || !target_obj)
360 return AVERROR_OPTION_NOT_FOUND;
361 if (!val && (o->type != AV_OPT_TYPE_STRING &&
362 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
363 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
364 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
365 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT))
366 return AVERROR(EINVAL);
368 dst = ((uint8_t*)target_obj) + o->offset;
370 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
371 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
372 case AV_OPT_TYPE_FLAGS:
373 case AV_OPT_TYPE_INT:
374 case AV_OPT_TYPE_INT64:
375 case AV_OPT_TYPE_FLOAT:
376 case AV_OPT_TYPE_DOUBLE:
377 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
378 case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
379 case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
380 case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
381 case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
382 case AV_OPT_TYPE_DURATION:
387 if ((ret = av_parse_time(dst, val, 1)) < 0)
388 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
392 case AV_OPT_TYPE_COLOR: return set_string_color(obj, o, val, dst);
393 case AV_OPT_TYPE_CHANNEL_LAYOUT:
394 if (!val || !strcmp(val, "none")) {
397 #if FF_API_GET_CHANNEL_LAYOUT_COMPAT
398 int64_t cl = ff_get_channel_layout(val, 0);
400 int64_t cl = av_get_channel_layout(val);
403 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
404 ret = AVERROR(EINVAL);
406 *(int64_t *)dst = cl;
412 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
413 return AVERROR(EINVAL);
416 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
417 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
419 if (!o || o->type != opttype)\
420 return AVERROR(EINVAL);\
421 return set_string_number(obj, obj, o, val, name ## _out);\
424 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
425 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
426 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
427 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
428 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
429 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
431 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
434 void *dst, *target_obj;
435 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
437 if (!o || !target_obj)
438 return AVERROR_OPTION_NOT_FOUND;
440 dst = ((uint8_t*)target_obj) + o->offset;
441 return write_number(obj, o, dst, num, den, intnum);
444 #if FF_API_OLD_AVOPTIONS
445 const AVOption *av_set_double(void *obj, const char *name, double n)
447 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
448 if (set_number(obj, name, n, 1, 1, 0) < 0)
453 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
455 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
456 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
461 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
463 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
464 if (set_number(obj, name, 1, 1, n, 0) < 0)
470 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
472 return set_number(obj, name, 1, 1, val, search_flags);
475 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
477 return set_number(obj, name, val, 1, 1, search_flags);
480 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
482 return set_number(obj, name, val.num, val.den, 1, search_flags);
485 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
488 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
493 if (!o || !target_obj)
494 return AVERROR_OPTION_NOT_FOUND;
496 if (o->type != AV_OPT_TYPE_BINARY)
497 return AVERROR(EINVAL);
499 ptr = len ? av_malloc(len) : NULL;
501 return AVERROR(ENOMEM);
503 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
504 lendst = (int *)(dst + 1);
510 memcpy(ptr, val, len);
515 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
518 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
520 if (!o || !target_obj)
521 return AVERROR_OPTION_NOT_FOUND;
522 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
523 av_log(obj, AV_LOG_ERROR,
524 "The value set by option '%s' is not an image size.\n", o->name);
525 return AVERROR(EINVAL);
528 av_log(obj, AV_LOG_ERROR,
529 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
530 return AVERROR(EINVAL);
532 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
533 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
537 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
540 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
542 if (!o || !target_obj)
543 return AVERROR_OPTION_NOT_FOUND;
544 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
545 av_log(obj, AV_LOG_ERROR,
546 "The value set by option '%s' is not a video rate.\n", o->name);
547 return AVERROR(EINVAL);
549 if (val.num <= 0 || val.den <= 0)
550 return AVERROR(EINVAL);
551 return set_number(obj, name, val.num, val.den, 1, search_flags);
554 static int set_format(void *obj, const char *name, int fmt, int search_flags,
555 enum AVOptionType type, const char *desc, int nb_fmts)
558 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
559 search_flags, &target_obj);
561 const AVClass *class = *(AVClass **)obj;
563 if (!o || !target_obj)
564 return AVERROR_OPTION_NOT_FOUND;
565 if (o->type != type) {
566 av_log(obj, AV_LOG_ERROR,
567 "The value set by option '%s' is not a %s format", name, desc);
568 return AVERROR(EINVAL);
571 #if LIBAVUTIL_VERSION_MAJOR < 54
572 if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
578 min = FFMAX(o->min, -1);
579 max = FFMIN(o->max, nb_fmts-1);
581 if (fmt < min || fmt > max) {
582 av_log(obj, AV_LOG_ERROR,
583 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
584 fmt, name, desc, min, max);
585 return AVERROR(ERANGE);
587 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
591 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
593 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
596 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
598 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
601 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
604 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
606 if (!o || !target_obj)
607 return AVERROR_OPTION_NOT_FOUND;
608 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
609 av_log(obj, AV_LOG_ERROR,
610 "The value set by option '%s' is not a channel layout.\n", o->name);
611 return AVERROR(EINVAL);
613 *(int *)(((int64_t *)target_obj) + o->offset) = cl;
617 #if FF_API_OLD_AVOPTIONS
620 * @param buf a buffer which is used for returning non string values as strings, can be NULL
621 * @param buf_len allocated length in bytes of buf
623 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
625 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
631 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
634 dst= ((uint8_t*)obj) + o->offset;
635 if (o_out) *o_out= o;
638 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
639 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
640 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
641 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
642 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
643 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
644 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
645 case AV_OPT_TYPE_STRING: return *(void**)dst;
646 case AV_OPT_TYPE_BINARY:
647 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
648 if (len >= (buf_len + 1)/2) return NULL;
649 bin = *(uint8_t**)dst;
650 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
652 default: return NULL;
658 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
660 void *dst, *target_obj;
661 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
662 uint8_t *bin, buf[128];
666 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
667 return AVERROR_OPTION_NOT_FOUND;
669 dst = (uint8_t*)target_obj + o->offset;
673 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
674 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
675 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
676 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
677 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
678 case AV_OPT_TYPE_VIDEO_RATE:
679 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
680 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
681 case AV_OPT_TYPE_STRING:
683 *out_val = av_strdup(*(uint8_t**)dst);
685 *out_val = av_strdup("");
687 case AV_OPT_TYPE_BINARY:
688 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
689 if ((uint64_t)len*2 + 1 > INT_MAX)
690 return AVERROR(EINVAL);
691 if (!(*out_val = av_malloc(len*2 + 1)))
692 return AVERROR(ENOMEM);
693 bin = *(uint8_t**)dst;
694 for (i = 0; i < len; i++)
695 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
697 case AV_OPT_TYPE_IMAGE_SIZE:
698 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
700 case AV_OPT_TYPE_PIXEL_FMT:
701 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
703 case AV_OPT_TYPE_SAMPLE_FMT:
704 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
706 case AV_OPT_TYPE_DURATION:
707 i64 = *(int64_t *)dst;
708 ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
709 i64 / 3600000000, (int)((i64 / 60000000) % 60),
710 (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
712 case AV_OPT_TYPE_COLOR:
713 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", ((int *)dst)[0], ((int *)dst)[1], ((int *)dst)[2], ((int *)dst)[3]);
715 case AV_OPT_TYPE_CHANNEL_LAYOUT:
716 i64 = *(int64_t *)dst;
717 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
720 return AVERROR(EINVAL);
723 if (ret >= sizeof(buf))
724 return AVERROR(EINVAL);
725 *out_val = av_strdup(buf);
729 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
732 void *dst, *target_obj;
733 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
734 if (!o || !target_obj)
737 dst = ((uint8_t*)target_obj) + o->offset;
739 if (o_out) *o_out= o;
741 return read_number(o, dst, num, den, intnum);
748 #if FF_API_OLD_AVOPTIONS
749 double av_get_double(void *obj, const char *name, const AVOption **o_out)
755 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
757 return num*intnum/den;
760 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
766 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
767 return (AVRational){0, 0};
768 if (num == 1.0 && (int)intnum == intnum)
769 return (AVRational){intnum, den};
771 return av_d2q(num*intnum/den, 1<<24);
774 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
780 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
782 return num*intnum/den;
786 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
792 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
794 *out_val = num*intnum/den;
798 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
804 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
806 *out_val = num*intnum/den;
810 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
816 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
819 if (num == 1.0 && (int)intnum == intnum)
820 *out_val = (AVRational){intnum, den};
822 *out_val = av_d2q(num*intnum/den, 1<<24);
826 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
828 void *dst, *target_obj;
829 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
830 if (!o || !target_obj)
831 return AVERROR_OPTION_NOT_FOUND;
832 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
833 av_log(obj, AV_LOG_ERROR,
834 "The value for option '%s' is not an image size.\n", name);
835 return AVERROR(EINVAL);
838 dst = ((uint8_t*)target_obj) + o->offset;
839 if (w_out) *w_out = *(int *)dst;
840 if (h_out) *h_out = *((int *)dst+1);
844 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
850 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
853 if (num == 1.0 && (int)intnum == intnum)
854 *out_val = (AVRational){intnum, den};
856 *out_val = av_d2q(num*intnum/den, 1<<24);
860 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
861 enum AVOptionType type, const char *desc)
863 void *dst, *target_obj;
864 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
865 if (!o || !target_obj)
866 return AVERROR_OPTION_NOT_FOUND;
867 if (o->type != type) {
868 av_log(obj, AV_LOG_ERROR,
869 "The value for option '%s' is not a %s format.\n", desc, name);
870 return AVERROR(EINVAL);
873 dst = ((uint8_t*)target_obj) + o->offset;
874 *out_fmt = *(int *)dst;
878 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
880 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
883 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
885 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
888 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
890 void *dst, *target_obj;
891 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
892 if (!o || !target_obj)
893 return AVERROR_OPTION_NOT_FOUND;
894 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
895 av_log(obj, AV_LOG_ERROR,
896 "The value for option '%s' is not a channel layout.\n", name);
897 return AVERROR(EINVAL);
900 dst = ((uint8_t*)target_obj) + o->offset;
901 *cl = *(int64_t *)dst;
905 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
907 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
908 const AVOption *flag = av_opt_find(obj, flag_name,
909 field ? field->unit : NULL, 0, 0);
912 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
913 av_opt_get_int(obj, field_name, 0, &res) < 0)
915 return res & flag->default_val.i64;
918 static void log_value(void *av_log_obj, int level, double d)
921 av_log(av_log_obj, level, "INT_MAX");
922 } else if (d == INT_MIN) {
923 av_log(av_log_obj, level, "INT_MIN");
924 } else if (d == (double)INT64_MAX) {
925 av_log(av_log_obj, level, "I64_MAX");
926 } else if (d == INT64_MIN) {
927 av_log(av_log_obj, level, "I64_MIN");
928 } else if (d == FLT_MAX) {
929 av_log(av_log_obj, level, "FLT_MAX");
930 } else if (d == FLT_MIN) {
931 av_log(av_log_obj, level, "FLT_MIN");
933 av_log(av_log_obj, level, "%g", d);
937 static void opt_list(void *obj, void *av_log_obj, const char *unit,
938 int req_flags, int rej_flags)
940 const AVOption *opt=NULL;
944 while ((opt = av_opt_next(obj, opt))) {
945 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
948 /* Don't print CONST's on level one.
949 * Don't print anything but CONST's on level two.
950 * Only print items from the requested unit.
952 if (!unit && opt->type==AV_OPT_TYPE_CONST)
954 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
956 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
958 else if (unit && opt->type == AV_OPT_TYPE_CONST)
959 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
961 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
962 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
966 case AV_OPT_TYPE_FLAGS:
967 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
969 case AV_OPT_TYPE_INT:
970 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
972 case AV_OPT_TYPE_INT64:
973 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
975 case AV_OPT_TYPE_DOUBLE:
976 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
978 case AV_OPT_TYPE_FLOAT:
979 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
981 case AV_OPT_TYPE_STRING:
982 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
984 case AV_OPT_TYPE_RATIONAL:
985 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
987 case AV_OPT_TYPE_BINARY:
988 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
990 case AV_OPT_TYPE_IMAGE_SIZE:
991 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
993 case AV_OPT_TYPE_VIDEO_RATE:
994 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
996 case AV_OPT_TYPE_PIXEL_FMT:
997 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
999 case AV_OPT_TYPE_SAMPLE_FMT:
1000 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1002 case AV_OPT_TYPE_DURATION:
1003 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1005 case AV_OPT_TYPE_COLOR:
1006 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1008 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1009 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1011 case AV_OPT_TYPE_CONST:
1013 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1016 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1017 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1018 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1019 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1020 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1021 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1024 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1026 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1027 switch (opt->type) {
1028 case AV_OPT_TYPE_INT:
1029 case AV_OPT_TYPE_INT64:
1030 case AV_OPT_TYPE_DOUBLE:
1031 case AV_OPT_TYPE_FLOAT:
1032 case AV_OPT_TYPE_RATIONAL:
1033 for (i = 0; i < r->nb_ranges; i++) {
1034 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1035 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1036 av_log(av_log_obj, AV_LOG_INFO, " to ");
1037 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1038 av_log(av_log_obj, AV_LOG_INFO, ")");
1042 av_opt_freep_ranges(&r);
1045 if (opt->type != AV_OPT_TYPE_CONST &&
1046 opt->type != AV_OPT_TYPE_BINARY &&
1047 !((opt->type == AV_OPT_TYPE_COLOR ||
1048 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1049 opt->type == AV_OPT_TYPE_STRING ||
1050 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1051 !opt->default_val.str)) {
1052 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1053 switch (opt->type) {
1054 case AV_OPT_TYPE_FLAGS:
1055 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1057 case AV_OPT_TYPE_DURATION:
1058 case AV_OPT_TYPE_INT:
1059 case AV_OPT_TYPE_INT64:
1060 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1062 case AV_OPT_TYPE_DOUBLE:
1063 case AV_OPT_TYPE_FLOAT:
1064 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1066 case AV_OPT_TYPE_RATIONAL: {
1067 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1068 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1070 case AV_OPT_TYPE_PIXEL_FMT:
1071 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1073 case AV_OPT_TYPE_SAMPLE_FMT:
1074 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1076 case AV_OPT_TYPE_COLOR:
1077 case AV_OPT_TYPE_IMAGE_SIZE:
1078 case AV_OPT_TYPE_STRING:
1079 case AV_OPT_TYPE_VIDEO_RATE:
1080 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1082 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1083 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1086 av_log(av_log_obj, AV_LOG_INFO, ")");
1089 av_log(av_log_obj, AV_LOG_INFO, "\n");
1090 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1091 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1096 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1101 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1103 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1108 void av_opt_set_defaults(void *s)
1110 #if FF_API_OLD_AVOPTIONS
1111 av_opt_set_defaults2(s, 0, 0);
1114 void av_opt_set_defaults2(void *s, int mask, int flags)
1117 const AVClass *class = *(AVClass **)s;
1118 const AVOption *opt = NULL;
1119 while ((opt = av_opt_next(s, opt)) != NULL) {
1120 void *dst = ((uint8_t*)s) + opt->offset;
1121 #if FF_API_OLD_AVOPTIONS
1122 if ((opt->flags & mask) != flags)
1125 switch (opt->type) {
1126 case AV_OPT_TYPE_CONST:
1127 /* Nothing to be done here */
1129 case AV_OPT_TYPE_FLAGS:
1130 case AV_OPT_TYPE_INT:
1131 case AV_OPT_TYPE_INT64:
1132 case AV_OPT_TYPE_DURATION:
1133 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1134 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1136 case AV_OPT_TYPE_DOUBLE:
1137 case AV_OPT_TYPE_FLOAT: {
1139 val = opt->default_val.dbl;
1140 write_number(s, opt, dst, val, 1, 1);
1143 case AV_OPT_TYPE_RATIONAL: {
1145 val = av_d2q(opt->default_val.dbl, INT_MAX);
1146 write_number(s, opt, dst, 1, val.den, val.num);
1149 case AV_OPT_TYPE_COLOR:
1150 set_string_color(s, opt, opt->default_val.str, dst);
1152 case AV_OPT_TYPE_STRING:
1153 set_string(s, opt, opt->default_val.str, dst);
1155 case AV_OPT_TYPE_IMAGE_SIZE:
1156 set_string_image_size(s, opt, opt->default_val.str, dst);
1158 case AV_OPT_TYPE_VIDEO_RATE:
1159 set_string_video_rate(s, opt, opt->default_val.str, dst);
1161 case AV_OPT_TYPE_PIXEL_FMT:
1162 #if LIBAVUTIL_VERSION_MAJOR < 54
1163 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1164 av_opt_set(s, opt->name, opt->default_val.str, 0);
1167 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1169 case AV_OPT_TYPE_SAMPLE_FMT:
1170 #if LIBAVUTIL_VERSION_MAJOR < 54
1171 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1172 av_opt_set(s, opt->name, opt->default_val.str, 0);
1175 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1177 case AV_OPT_TYPE_BINARY:
1178 /* Cannot set default for binary */
1181 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1187 * Store the value in the field in ctx that is named like key.
1188 * ctx must be an AVClass context, storing is done using AVOptions.
1190 * @param buf the string to parse, buf will be updated to point at the
1191 * separator just after the parsed key/value pair
1192 * @param key_val_sep a 0-terminated list of characters used to
1193 * separate key from value
1194 * @param pairs_sep a 0-terminated list of characters used to separate
1195 * two pairs from each other
1196 * @return 0 if the key/value pair has been successfully parsed and
1197 * set, or a negative value corresponding to an AVERROR code in case
1199 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1200 * the error code issued by av_opt_set() if the key/value pair
1203 static int parse_key_value_pair(void *ctx, const char **buf,
1204 const char *key_val_sep, const char *pairs_sep)
1206 char *key = av_get_token(buf, key_val_sep);
1211 return AVERROR(ENOMEM);
1213 if (*key && strspn(*buf, key_val_sep)) {
1215 val = av_get_token(buf, pairs_sep);
1218 return AVERROR(ENOMEM);
1221 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1223 return AVERROR(EINVAL);
1226 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1228 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1229 if (ret == AVERROR_OPTION_NOT_FOUND)
1230 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1237 int av_set_options_string(void *ctx, const char *opts,
1238 const char *key_val_sep, const char *pairs_sep)
1246 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1257 #define WHITESPACES " \n\t"
1259 static int is_key_char(char c)
1261 return (unsigned)((c | 32) - 'a') < 26 ||
1262 (unsigned)(c - '0') < 10 ||
1263 c == '-' || c == '_' || c == '/' || c == '.';
1267 * Read a key from a string.
1269 * The key consists of is_key_char characters and must be terminated by a
1270 * character from the delim string; spaces are ignored.
1272 * @return 0 for success (even with ellipsis), <0 for failure
1274 static int get_key(const char **ropts, const char *delim, char **rkey)
1276 const char *opts = *ropts;
1277 const char *key_start, *key_end;
1279 key_start = opts += strspn(opts, WHITESPACES);
1280 while (is_key_char(*opts))
1283 opts += strspn(opts, WHITESPACES);
1284 if (!*opts || !strchr(delim, *opts))
1285 return AVERROR(EINVAL);
1287 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1288 return AVERROR(ENOMEM);
1289 memcpy(*rkey, key_start, key_end - key_start);
1290 (*rkey)[key_end - key_start] = 0;
1295 int av_opt_get_key_value(const char **ropts,
1296 const char *key_val_sep, const char *pairs_sep,
1298 char **rkey, char **rval)
1301 char *key = NULL, *val;
1302 const char *opts = *ropts;
1304 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1305 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1306 return AVERROR(EINVAL);
1307 if (!(val = av_get_token(&opts, pairs_sep))) {
1309 return AVERROR(ENOMEM);
1317 int av_opt_set_from_string(void *ctx, const char *opts,
1318 const char *const *shorthand,
1319 const char *key_val_sep, const char *pairs_sep)
1322 const char *dummy_shorthand = NULL;
1323 char *av_uninit(parsed_key), *av_uninit(value);
1329 shorthand = &dummy_shorthand;
1332 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1333 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1334 &parsed_key, &value);
1336 if (ret == AVERROR(EINVAL))
1337 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1339 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1347 while (*shorthand) /* discard all remaining shorthand */
1350 key = *(shorthand++);
1353 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1354 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1355 if (ret == AVERROR_OPTION_NOT_FOUND)
1356 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1358 av_free(parsed_key);
1363 av_free(parsed_key);
1369 void av_opt_free(void *obj)
1371 const AVOption *o = NULL;
1372 while ((o = av_opt_next(obj, o)))
1373 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1374 av_freep((uint8_t *)obj + o->offset);
1377 int av_opt_set_dict(void *obj, AVDictionary **options)
1379 AVDictionaryEntry *t = NULL;
1380 AVDictionary *tmp = NULL;
1383 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1384 ret = av_opt_set(obj, t->key, t->value, 0);
1385 if (ret == AVERROR_OPTION_NOT_FOUND)
1386 av_dict_set(&tmp, t->key, t->value, 0);
1388 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1393 av_dict_free(options);
1398 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1399 int opt_flags, int search_flags)
1401 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1404 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1405 int opt_flags, int search_flags, void **target_obj)
1408 const AVOption *o = NULL;
1418 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1419 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1420 const AVClass *child = NULL;
1421 while (child = av_opt_child_class_next(c, child))
1422 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1426 while (child = av_opt_child_next(obj, child))
1427 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1432 while (o = av_opt_next(obj, o)) {
1433 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1434 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1435 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1437 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1448 void *av_opt_child_next(void *obj, void *prev)
1450 const AVClass *c = *(AVClass**)obj;
1452 return c->child_next(obj, prev);
1456 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1458 if (parent->child_class_next)
1459 return parent->child_class_next(prev);
1463 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1465 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1468 return (uint8_t*)obj + opt->offset;
1471 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1473 const AVClass *c = *(AVClass**)obj;
1474 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1476 if (c->version > (52 << 16 | 11 << 8))
1477 callback = c->query_ranges;
1480 callback = av_opt_query_ranges_default;
1482 return callback(ranges_arg, obj, key, flags);
1485 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1487 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1488 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1489 AVOptionRange *range = av_mallocz(sizeof(*range));
1490 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1495 if (!ranges || !range || !range_array || !field) {
1496 ret = AVERROR(ENOMEM);
1500 ranges->range = range_array;
1501 ranges->range[0] = range;
1502 ranges->nb_ranges = 1;
1503 range->is_range = 1;
1504 range->value_min = field->min;
1505 range->value_max = field->max;
1507 switch (field->type) {
1508 case AV_OPT_TYPE_INT:
1509 case AV_OPT_TYPE_INT64:
1510 case AV_OPT_TYPE_PIXEL_FMT:
1511 case AV_OPT_TYPE_SAMPLE_FMT:
1512 case AV_OPT_TYPE_FLOAT:
1513 case AV_OPT_TYPE_DOUBLE:
1514 case AV_OPT_TYPE_DURATION:
1515 case AV_OPT_TYPE_COLOR:
1516 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1518 case AV_OPT_TYPE_STRING:
1519 range->component_min = 0;
1520 range->component_max = 0x10FFFF; // max unicode value
1521 range->value_min = -1;
1522 range->value_max = INT_MAX;
1524 case AV_OPT_TYPE_RATIONAL:
1525 range->component_min = INT_MIN;
1526 range->component_max = INT_MAX;
1528 case AV_OPT_TYPE_IMAGE_SIZE:
1529 range->component_min = 0;
1530 range->component_max = INT_MAX/128/8;
1531 range->value_min = 0;
1532 range->value_max = INT_MAX/8;
1534 case AV_OPT_TYPE_VIDEO_RATE:
1535 range->component_min = 1;
1536 range->component_max = INT_MAX;
1537 range->value_min = 1;
1538 range->value_max = INT_MAX;
1541 ret = AVERROR(ENOSYS);
1545 *ranges_arg = ranges;
1550 av_free(range_array);
1554 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1557 AVOptionRanges *ranges = *rangesp;
1559 for (i = 0; i < ranges->nb_ranges; i++) {
1560 AVOptionRange *range = ranges->range[i];
1561 av_freep(&range->str);
1562 av_freep(&ranges->range[i]);
1564 av_freep(&ranges->range);
1570 typedef struct TestContext
1572 const AVClass *class;
1577 AVRational rational;
1578 AVRational video_rate;
1580 enum AVPixelFormat pix_fmt;
1581 enum AVSampleFormat sample_fmt;
1584 int64_t channel_layout;
1587 #define OFFSET(x) offsetof(TestContext, x)
1589 #define TEST_FLAG_COOL 01
1590 #define TEST_FLAG_LAME 02
1591 #define TEST_FLAG_MU 04
1593 static const AVOption test_options[]= {
1594 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1595 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1596 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1597 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
1598 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1599 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1600 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1601 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1602 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1603 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
1604 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
1605 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
1606 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1607 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1608 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
1612 static const char *test_get_name(void *ctx)
1617 static const AVClass test_class = {
1627 printf("\nTesting av_set_options_string()\n");
1629 TestContext test_ctx = { 0 };
1630 static const char * const options[] = {
1646 "flags=+mu-lame : num=42: toggle=0",
1647 "num=42 : string=blahblah",
1648 "rational=0 : rational=1/2 : rational=1/-1",
1661 "video_rate=30000/1001",
1662 "video_rate=30/1.001",
1666 "duration=1\\:23\\:45.67",
1670 "cl=stereo+downmix",
1674 test_ctx.class = &test_class;
1675 av_opt_set_defaults(&test_ctx);
1677 av_log_set_level(AV_LOG_DEBUG);
1679 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1680 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1681 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1682 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1685 av_opt_free(&test_ctx);
1688 printf("\nTesting av_opt_set_from_string()\n");
1690 TestContext test_ctx = { 0 };
1691 static const char * const options[] = {
1699 " 5 : hello : size = pal ",
1700 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1702 static const char * const shorthand[] = { "num", "string", NULL };
1704 test_ctx.class = &test_class;
1705 av_opt_set_defaults(&test_ctx);
1707 av_log_set_level(AV_LOG_DEBUG);
1709 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1710 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1711 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1712 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1715 av_opt_free(&test_ctx);