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>
31 #include "channel_layout.h"
36 #include "parseutils.h"
38 #include "mathematics.h"
40 #include "samplefmt.h"
45 const AVOption *av_opt_next(const void *obj, const AVOption *last)
50 class = *(const AVClass**)obj;
51 if (!last && class && class->option && class->option[0].name)
53 if (last && last[1].name)
58 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
61 case AV_OPT_TYPE_FLAGS:
62 *intnum = *(unsigned int*)dst;
64 case AV_OPT_TYPE_PIXEL_FMT:
65 *intnum = *(enum AVPixelFormat *)dst;
67 case AV_OPT_TYPE_SAMPLE_FMT:
68 *intnum = *(enum AVSampleFormat *)dst;
70 case AV_OPT_TYPE_BOOL:
72 *intnum = *(int *)dst;
74 case AV_OPT_TYPE_CHANNEL_LAYOUT:
75 case AV_OPT_TYPE_DURATION:
76 case AV_OPT_TYPE_INT64:
77 *intnum = *(int64_t *)dst;
79 case AV_OPT_TYPE_FLOAT:
82 case AV_OPT_TYPE_DOUBLE:
83 *num = *(double *)dst;
85 case AV_OPT_TYPE_RATIONAL:
86 *intnum = ((AVRational *)dst)->num;
87 *den = ((AVRational *)dst)->den;
89 case AV_OPT_TYPE_CONST:
90 *num = o->default_val.dbl;
93 return AVERROR(EINVAL);
96 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
98 if (o->type != AV_OPT_TYPE_FLAGS &&
99 (o->max * den < num * intnum || o->min * den > num * intnum)) {
100 num = den ? num * intnum / den : (num * intnum ? INFINITY : NAN);
101 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
102 num, o->name, o->min, o->max);
103 return AVERROR(ERANGE);
105 if (o->type == AV_OPT_TYPE_FLAGS) {
106 double d = num*intnum/den;
107 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
108 av_log(obj, AV_LOG_ERROR,
109 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
110 num*intnum/den, o->name);
111 return AVERROR(ERANGE);
116 case AV_OPT_TYPE_PIXEL_FMT:
117 *(enum AVPixelFormat *)dst = llrint(num / den) * intnum;
119 case AV_OPT_TYPE_SAMPLE_FMT:
120 *(enum AVSampleFormat *)dst = llrint(num / den) * intnum;
122 case AV_OPT_TYPE_BOOL:
123 case AV_OPT_TYPE_FLAGS:
124 case AV_OPT_TYPE_INT:
125 *(int *)dst = llrint(num / den) * intnum;
127 case AV_OPT_TYPE_DURATION:
128 case AV_OPT_TYPE_CHANNEL_LAYOUT:
129 case AV_OPT_TYPE_INT64:
130 *(int64_t *)dst = llrint(num / den) * intnum;
132 case AV_OPT_TYPE_FLOAT:
133 *(float *)dst = num * intnum / den;
135 case AV_OPT_TYPE_DOUBLE:
136 *(double *)dst = num * intnum / den;
138 case AV_OPT_TYPE_RATIONAL:
139 case AV_OPT_TYPE_VIDEO_RATE:
140 if ((int) num == num)
141 *(AVRational *)dst = (AVRational) { num *intnum, den };
143 *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
146 return AVERROR(EINVAL);
151 static int hexchar2int(char c) {
152 if (c >= '0' && c <= '9')
154 if (c >= 'a' && c <= 'f')
156 if (c >= 'A' && c <= 'F')
161 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
163 int *lendst = (int *)(dst + 1);
170 if (!val || !(len = strlen(val)))
174 return AVERROR(EINVAL);
177 ptr = bin = av_malloc(len);
179 return AVERROR(ENOMEM);
181 int a = hexchar2int(*val++);
182 int b = hexchar2int(*val++);
183 if (a < 0 || b < 0) {
185 return AVERROR(EINVAL);
187 *ptr++ = (a << 4) | b;
195 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
198 *dst = av_strdup(val);
199 return *dst ? 0 : AVERROR(ENOMEM);
202 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
203 opt->type == AV_OPT_TYPE_CONST || \
204 opt->type == AV_OPT_TYPE_FLAGS || \
205 opt->type == AV_OPT_TYPE_INT) \
206 ? opt->default_val.i64 \
207 : opt->default_val.dbl)
209 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
215 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
216 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
228 if (o->type == AV_OPT_TYPE_FLAGS) {
229 if (*val == '+' || *val == '-')
231 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
237 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
240 double const_values[64];
241 const char * const_names[64];
242 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
243 d = DEFAULT_NUMVAL(o_named);
246 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
247 if (o_named->type == AV_OPT_TYPE_CONST &&
249 !strcmp(o_named->unit, o->unit)) {
250 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
251 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
252 return AVERROR_PATCHWELCOME;
254 const_names [ci ] = o_named->name;
255 const_values[ci++] = DEFAULT_NUMVAL(o_named);
259 const_names [ci ] = "default";
260 const_values[ci++] = DEFAULT_NUMVAL(o);
261 const_names [ci ] = "max";
262 const_values[ci++] = o->max;
263 const_names [ci ] = "min";
264 const_values[ci++] = o->min;
265 const_names [ci ] = "none";
266 const_values[ci++] = 0;
267 const_names [ci ] = "all";
268 const_values[ci++] = ~0;
269 const_names [ci] = NULL;
270 const_values[ci] = 0;
272 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
273 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
275 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
280 if (o->type == AV_OPT_TYPE_FLAGS) {
281 read_number(o, dst, NULL, NULL, &intnum);
283 d = intnum | (int64_t)d;
285 d = intnum &~(int64_t)d;
288 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
298 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
302 if (!val || !strcmp(val, "none")) {
307 ret = av_parse_video_size(dst, dst + 1, val);
309 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
313 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
317 ret = AVERROR(EINVAL);
319 ret = av_parse_video_rate(dst, val);
322 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
326 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
333 ret = av_parse_color(dst, val, -1, obj);
335 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
341 static const char *get_bool_name(int val)
345 return val ? "true" : "false";
348 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
355 if (!strcmp(val, "auto")) {
357 } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
359 } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
363 n = strtol(val, &end, 10);
364 if (val + strlen(val) != end)
368 if (n < o->min || n > o->max)
375 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
376 return AVERROR(EINVAL);
379 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
380 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
384 if (!val || !strcmp(val, "none")) {
390 fmt = strtol(val, &tail, 0);
391 if (*tail || (unsigned)fmt >= fmt_nb) {
392 av_log(obj, AV_LOG_ERROR,
393 "Unable to parse option value \"%s\" as %s\n", val, desc);
394 return AVERROR(EINVAL);
399 min = FFMAX(o->min, -1);
400 max = FFMIN(o->max, fmt_nb-1);
402 // hack for compatibility with old ffmpeg
403 if(min == 0 && max == 0) {
408 if (fmt < min || fmt > max) {
409 av_log(obj, AV_LOG_ERROR,
410 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
411 fmt, o->name, desc, min, max);
412 return AVERROR(ERANGE);
419 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
421 return set_string_fmt(obj, o, val, dst,
422 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
425 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
427 return set_string_fmt(obj, o, val, dst,
428 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
431 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
434 void *dst, *target_obj;
435 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
436 if (!o || !target_obj)
437 return AVERROR_OPTION_NOT_FOUND;
438 if (!val && (o->type != AV_OPT_TYPE_STRING &&
439 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
440 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
441 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
442 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
443 return AVERROR(EINVAL);
445 if (o->flags & AV_OPT_FLAG_READONLY)
446 return AVERROR(EINVAL);
448 dst = ((uint8_t *)target_obj) + o->offset;
450 case AV_OPT_TYPE_BOOL:
451 return set_string_bool(obj, o, val, dst);
452 case AV_OPT_TYPE_STRING:
453 return set_string(obj, o, val, dst);
454 case AV_OPT_TYPE_BINARY:
455 return set_string_binary(obj, o, val, dst);
456 case AV_OPT_TYPE_FLAGS:
457 case AV_OPT_TYPE_INT:
458 case AV_OPT_TYPE_INT64:
459 case AV_OPT_TYPE_FLOAT:
460 case AV_OPT_TYPE_DOUBLE:
461 case AV_OPT_TYPE_RATIONAL:
462 return set_string_number(obj, target_obj, o, val, dst);
463 case AV_OPT_TYPE_IMAGE_SIZE:
464 return set_string_image_size(obj, o, val, dst);
465 case AV_OPT_TYPE_VIDEO_RATE:
466 return set_string_video_rate(obj, o, val, dst);
467 case AV_OPT_TYPE_PIXEL_FMT:
468 return set_string_pixel_fmt(obj, o, val, dst);
469 case AV_OPT_TYPE_SAMPLE_FMT:
470 return set_string_sample_fmt(obj, o, val, dst);
471 case AV_OPT_TYPE_DURATION:
476 if ((ret = av_parse_time(dst, val, 1)) < 0)
477 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
481 case AV_OPT_TYPE_COLOR:
482 return set_string_color(obj, o, val, dst);
483 case AV_OPT_TYPE_CHANNEL_LAYOUT:
484 if (!val || !strcmp(val, "none")) {
487 int64_t cl = av_get_channel_layout(val);
489 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
490 ret = AVERROR(EINVAL);
492 *(int64_t *)dst = cl;
498 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
499 return AVERROR(EINVAL);
502 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
503 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
504 const char *val, vartype *name ## _out) \
506 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
507 return AVERROR(EINVAL); \
508 return set_string_number(obj, obj, o, val, name ## _out); \
511 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
512 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
513 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
514 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
515 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
516 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
518 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
521 void *dst, *target_obj;
522 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
524 if (!o || !target_obj)
525 return AVERROR_OPTION_NOT_FOUND;
527 if (o->flags & AV_OPT_FLAG_READONLY)
528 return AVERROR(EINVAL);
530 dst = ((uint8_t *)target_obj) + o->offset;
531 return write_number(obj, o, dst, num, den, intnum);
534 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
536 return set_number(obj, name, 1, 1, val, search_flags);
539 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
541 return set_number(obj, name, val, 1, 1, search_flags);
544 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
546 return set_number(obj, name, val.num, val.den, 1, search_flags);
549 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
552 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
557 if (!o || !target_obj)
558 return AVERROR_OPTION_NOT_FOUND;
560 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
561 return AVERROR(EINVAL);
563 ptr = len ? av_malloc(len) : NULL;
565 return AVERROR(ENOMEM);
567 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
568 lendst = (int *)(dst + 1);
574 memcpy(ptr, val, len);
579 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
582 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
584 if (!o || !target_obj)
585 return AVERROR_OPTION_NOT_FOUND;
586 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
587 av_log(obj, AV_LOG_ERROR,
588 "The value set by option '%s' is not an image size.\n", o->name);
589 return AVERROR(EINVAL);
592 av_log(obj, AV_LOG_ERROR,
593 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
594 return AVERROR(EINVAL);
596 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
597 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
601 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, 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_VIDEO_RATE) {
609 av_log(obj, AV_LOG_ERROR,
610 "The value set by option '%s' is not a video rate.\n", o->name);
611 return AVERROR(EINVAL);
613 if (val.num <= 0 || val.den <= 0)
614 return AVERROR(EINVAL);
615 return set_number(obj, name, val.num, val.den, 1, search_flags);
618 static int set_format(void *obj, const char *name, int fmt, int search_flags,
619 enum AVOptionType type, const char *desc, int nb_fmts)
622 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
623 search_flags, &target_obj);
626 if (!o || !target_obj)
627 return AVERROR_OPTION_NOT_FOUND;
628 if (o->type != type) {
629 av_log(obj, AV_LOG_ERROR,
630 "The value set by option '%s' is not a %s format", name, desc);
631 return AVERROR(EINVAL);
634 min = FFMAX(o->min, -1);
635 max = FFMIN(o->max, nb_fmts-1);
637 if (fmt < min || fmt > max) {
638 av_log(obj, AV_LOG_ERROR,
639 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
640 fmt, name, desc, min, max);
641 return AVERROR(ERANGE);
643 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
647 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
649 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
652 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
654 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
657 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
660 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
662 if (!o || !target_obj)
663 return AVERROR_OPTION_NOT_FOUND;
664 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
665 av_log(obj, AV_LOG_ERROR,
666 "The value set by option '%s' is not a channel layout.\n", o->name);
667 return AVERROR(EINVAL);
669 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
673 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
678 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
680 if (!o || !target_obj)
681 return AVERROR_OPTION_NOT_FOUND;
682 if (o->flags & AV_OPT_FLAG_READONLY)
683 return AVERROR(EINVAL);
685 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
687 av_dict_copy(dst, val, 0);
692 static void format_duration(char *buf, size_t size, int64_t d)
696 av_assert0(size >= 25);
697 if (d < 0 && d != INT64_MIN) {
703 snprintf(buf, size, "INT64_MAX");
704 else if (d == INT64_MIN)
705 snprintf(buf, size, "INT64_MIN");
706 else if (d > (int64_t)3600*1000000)
707 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
708 (int)((d / 60000000) % 60),
709 (int)((d / 1000000) % 60),
711 else if (d > 60*1000000)
712 snprintf(buf, size, "%d:%02d.%06d",
714 (int)((d / 1000000) % 60),
717 snprintf(buf, size, "%d.%06d",
720 e = buf + strlen(buf);
721 while (e > buf && e[-1] == '0')
723 if (e > buf && e[-1] == '.')
727 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
729 void *dst, *target_obj;
730 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
731 uint8_t *bin, buf[128];
735 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
736 return AVERROR_OPTION_NOT_FOUND;
738 dst = (uint8_t *)target_obj + o->offset;
742 case AV_OPT_TYPE_BOOL:
743 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
745 case AV_OPT_TYPE_FLAGS:
746 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
748 case AV_OPT_TYPE_INT:
749 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
751 case AV_OPT_TYPE_INT64:
752 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
754 case AV_OPT_TYPE_FLOAT:
755 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
757 case AV_OPT_TYPE_DOUBLE:
758 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
760 case AV_OPT_TYPE_VIDEO_RATE:
761 case AV_OPT_TYPE_RATIONAL:
762 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
764 case AV_OPT_TYPE_CONST:
765 ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
767 case AV_OPT_TYPE_STRING:
768 if (*(uint8_t **)dst) {
769 *out_val = av_strdup(*(uint8_t **)dst);
770 } else if (search_flags & AV_OPT_ALLOW_NULL) {
774 *out_val = av_strdup("");
776 return *out_val ? 0 : AVERROR(ENOMEM);
777 case AV_OPT_TYPE_BINARY:
778 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
782 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
783 if ((uint64_t)len * 2 + 1 > INT_MAX)
784 return AVERROR(EINVAL);
785 if (!(*out_val = av_malloc(len * 2 + 1)))
786 return AVERROR(ENOMEM);
791 bin = *(uint8_t **)dst;
792 for (i = 0; i < len; i++)
793 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
795 case AV_OPT_TYPE_IMAGE_SIZE:
796 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
798 case AV_OPT_TYPE_PIXEL_FMT:
799 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
801 case AV_OPT_TYPE_SAMPLE_FMT:
802 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
804 case AV_OPT_TYPE_DURATION:
805 i64 = *(int64_t *)dst;
806 format_duration(buf, sizeof(buf), i64);
807 ret = strlen(buf); // no overflow possible, checked by an assert
809 case AV_OPT_TYPE_COLOR:
810 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
811 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
812 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
814 case AV_OPT_TYPE_CHANNEL_LAYOUT:
815 i64 = *(int64_t *)dst;
816 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
819 return AVERROR(EINVAL);
822 if (ret >= sizeof(buf))
823 return AVERROR(EINVAL);
824 *out_val = av_strdup(buf);
825 return *out_val ? 0 : AVERROR(ENOMEM);
828 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
831 void *dst, *target_obj;
832 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
833 if (!o || !target_obj)
836 dst = ((uint8_t *)target_obj) + o->offset;
838 if (o_out) *o_out= o;
840 return read_number(o, dst, num, den, intnum);
848 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
854 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
856 *out_val = num * intnum / den;
860 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
866 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
868 *out_val = num * intnum / den;
872 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
878 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
881 if (num == 1.0 && (int)intnum == intnum)
882 *out_val = (AVRational){intnum, den};
884 *out_val = av_d2q(num*intnum/den, 1<<24);
888 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
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_IMAGE_SIZE) {
895 av_log(obj, AV_LOG_ERROR,
896 "The value for option '%s' is not an image size.\n", name);
897 return AVERROR(EINVAL);
900 dst = ((uint8_t*)target_obj) + o->offset;
901 if (w_out) *w_out = *(int *)dst;
902 if (h_out) *h_out = *((int *)dst+1);
906 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
912 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
915 if (num == 1.0 && (int)intnum == intnum)
916 *out_val = (AVRational) { intnum, den };
918 *out_val = av_d2q(num * intnum / den, 1 << 24);
922 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
923 enum AVOptionType type, const char *desc)
925 void *dst, *target_obj;
926 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
927 if (!o || !target_obj)
928 return AVERROR_OPTION_NOT_FOUND;
929 if (o->type != type) {
930 av_log(obj, AV_LOG_ERROR,
931 "The value for option '%s' is not a %s format.\n", desc, name);
932 return AVERROR(EINVAL);
935 dst = ((uint8_t*)target_obj) + o->offset;
936 *out_fmt = *(int *)dst;
940 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
942 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
945 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
947 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
950 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
952 void *dst, *target_obj;
953 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
954 if (!o || !target_obj)
955 return AVERROR_OPTION_NOT_FOUND;
956 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
957 av_log(obj, AV_LOG_ERROR,
958 "The value for option '%s' is not a channel layout.\n", name);
959 return AVERROR(EINVAL);
962 dst = ((uint8_t*)target_obj) + o->offset;
963 *cl = *(int64_t *)dst;
967 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
971 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
973 if (!o || !target_obj)
974 return AVERROR_OPTION_NOT_FOUND;
975 if (o->type != AV_OPT_TYPE_DICT)
976 return AVERROR(EINVAL);
978 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
979 av_dict_copy(out_val, src, 0);
984 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
986 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
987 const AVOption *flag = av_opt_find(obj, flag_name,
988 field ? field->unit : NULL, 0, 0);
991 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
992 av_opt_get_int(obj, field_name, 0, &res) < 0)
994 return res & flag->default_val.i64;
997 static void log_value(void *av_log_obj, int level, double d)
1000 av_log(av_log_obj, level, "INT_MAX");
1001 } else if (d == INT_MIN) {
1002 av_log(av_log_obj, level, "INT_MIN");
1003 } else if (d == UINT32_MAX) {
1004 av_log(av_log_obj, level, "UINT32_MAX");
1005 } else if (d == (double)INT64_MAX) {
1006 av_log(av_log_obj, level, "I64_MAX");
1007 } else if (d == INT64_MIN) {
1008 av_log(av_log_obj, level, "I64_MIN");
1009 } else if (d == FLT_MAX) {
1010 av_log(av_log_obj, level, "FLT_MAX");
1011 } else if (d == FLT_MIN) {
1012 av_log(av_log_obj, level, "FLT_MIN");
1013 } else if (d == -FLT_MAX) {
1014 av_log(av_log_obj, level, "-FLT_MAX");
1015 } else if (d == -FLT_MIN) {
1016 av_log(av_log_obj, level, "-FLT_MIN");
1017 } else if (d == DBL_MAX) {
1018 av_log(av_log_obj, level, "DBL_MAX");
1019 } else if (d == DBL_MIN) {
1020 av_log(av_log_obj, level, "DBL_MIN");
1021 } else if (d == -DBL_MAX) {
1022 av_log(av_log_obj, level, "-DBL_MAX");
1023 } else if (d == -DBL_MIN) {
1024 av_log(av_log_obj, level, "-DBL_MIN");
1026 av_log(av_log_obj, level, "%g", d);
1030 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1032 const AVOption *opt = NULL;
1036 while ((opt = av_opt_next(obj, opt)))
1037 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1038 opt->default_val.i64 == value)
1043 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1045 const AVOption *opt = NULL;
1051 while ((opt = av_opt_next(obj, opt))) {
1052 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1053 opt->default_val.i64 & value) {
1055 av_strlcatf(flags, sizeof(flags), "+");
1056 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1060 return av_strdup(flags);
1064 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1065 int req_flags, int rej_flags)
1067 const AVOption *opt = NULL;
1071 while ((opt = av_opt_next(obj, opt))) {
1072 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1075 /* Don't print CONST's on level one.
1076 * Don't print anything but CONST's on level two.
1077 * Only print items from the requested unit.
1079 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1081 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1083 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1085 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1086 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1088 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1089 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1092 switch (opt->type) {
1093 case AV_OPT_TYPE_FLAGS:
1094 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1096 case AV_OPT_TYPE_INT:
1097 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1099 case AV_OPT_TYPE_INT64:
1100 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1102 case AV_OPT_TYPE_DOUBLE:
1103 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1105 case AV_OPT_TYPE_FLOAT:
1106 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1108 case AV_OPT_TYPE_STRING:
1109 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1111 case AV_OPT_TYPE_RATIONAL:
1112 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1114 case AV_OPT_TYPE_BINARY:
1115 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1117 case AV_OPT_TYPE_IMAGE_SIZE:
1118 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1120 case AV_OPT_TYPE_VIDEO_RATE:
1121 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1123 case AV_OPT_TYPE_PIXEL_FMT:
1124 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1126 case AV_OPT_TYPE_SAMPLE_FMT:
1127 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1129 case AV_OPT_TYPE_DURATION:
1130 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1132 case AV_OPT_TYPE_COLOR:
1133 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1135 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1136 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1138 case AV_OPT_TYPE_BOOL:
1139 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1141 case AV_OPT_TYPE_CONST:
1143 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1146 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1147 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1148 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1149 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1150 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1151 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1152 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1153 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1156 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1158 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1159 switch (opt->type) {
1160 case AV_OPT_TYPE_INT:
1161 case AV_OPT_TYPE_INT64:
1162 case AV_OPT_TYPE_DOUBLE:
1163 case AV_OPT_TYPE_FLOAT:
1164 case AV_OPT_TYPE_RATIONAL:
1165 for (i = 0; i < r->nb_ranges; i++) {
1166 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1167 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1168 av_log(av_log_obj, AV_LOG_INFO, " to ");
1169 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1170 av_log(av_log_obj, AV_LOG_INFO, ")");
1174 av_opt_freep_ranges(&r);
1177 if (opt->type != AV_OPT_TYPE_CONST &&
1178 opt->type != AV_OPT_TYPE_BINARY &&
1179 !((opt->type == AV_OPT_TYPE_COLOR ||
1180 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1181 opt->type == AV_OPT_TYPE_STRING ||
1182 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1183 !opt->default_val.str)) {
1184 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1185 switch (opt->type) {
1186 case AV_OPT_TYPE_BOOL:
1187 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1189 case AV_OPT_TYPE_FLAGS: {
1190 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1192 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1193 av_freep(&def_flags);
1195 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1199 case AV_OPT_TYPE_DURATION: {
1201 format_duration(buf, sizeof(buf), opt->default_val.i64);
1202 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1205 case AV_OPT_TYPE_INT:
1206 case AV_OPT_TYPE_INT64: {
1207 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1209 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1211 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1214 case AV_OPT_TYPE_DOUBLE:
1215 case AV_OPT_TYPE_FLOAT:
1216 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1218 case AV_OPT_TYPE_RATIONAL: {
1219 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1220 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1222 case AV_OPT_TYPE_PIXEL_FMT:
1223 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1225 case AV_OPT_TYPE_SAMPLE_FMT:
1226 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1228 case AV_OPT_TYPE_COLOR:
1229 case AV_OPT_TYPE_IMAGE_SIZE:
1230 case AV_OPT_TYPE_STRING:
1231 case AV_OPT_TYPE_VIDEO_RATE:
1232 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1234 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1235 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1238 av_log(av_log_obj, AV_LOG_INFO, ")");
1241 av_log(av_log_obj, AV_LOG_INFO, "\n");
1242 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1243 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1247 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1252 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1254 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1259 void av_opt_set_defaults(void *s)
1261 av_opt_set_defaults2(s, 0, 0);
1264 void av_opt_set_defaults2(void *s, int mask, int flags)
1266 const AVOption *opt = NULL;
1267 while ((opt = av_opt_next(s, opt))) {
1268 void *dst = ((uint8_t*)s) + opt->offset;
1270 if ((opt->flags & mask) != flags)
1273 if (opt->flags & AV_OPT_FLAG_READONLY)
1276 switch (opt->type) {
1277 case AV_OPT_TYPE_CONST:
1278 /* Nothing to be done here */
1280 case AV_OPT_TYPE_BOOL:
1281 case AV_OPT_TYPE_FLAGS:
1282 case AV_OPT_TYPE_INT:
1283 case AV_OPT_TYPE_INT64:
1284 case AV_OPT_TYPE_DURATION:
1285 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1286 case AV_OPT_TYPE_PIXEL_FMT:
1287 case AV_OPT_TYPE_SAMPLE_FMT:
1288 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1290 case AV_OPT_TYPE_DOUBLE:
1291 case AV_OPT_TYPE_FLOAT: {
1293 val = opt->default_val.dbl;
1294 write_number(s, opt, dst, val, 1, 1);
1297 case AV_OPT_TYPE_RATIONAL: {
1299 val = av_d2q(opt->default_val.dbl, INT_MAX);
1300 write_number(s, opt, dst, 1, val.den, val.num);
1303 case AV_OPT_TYPE_COLOR:
1304 set_string_color(s, opt, opt->default_val.str, dst);
1306 case AV_OPT_TYPE_STRING:
1307 set_string(s, opt, opt->default_val.str, dst);
1309 case AV_OPT_TYPE_IMAGE_SIZE:
1310 set_string_image_size(s, opt, opt->default_val.str, dst);
1312 case AV_OPT_TYPE_VIDEO_RATE:
1313 set_string_video_rate(s, opt, opt->default_val.str, dst);
1315 case AV_OPT_TYPE_BINARY:
1316 set_string_binary(s, opt, opt->default_val.str, dst);
1318 case AV_OPT_TYPE_DICT:
1319 /* Cannot set defaults for these types */
1322 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1323 opt->type, opt->name);
1329 * Store the value in the field in ctx that is named like key.
1330 * ctx must be an AVClass context, storing is done using AVOptions.
1332 * @param buf the string to parse, buf will be updated to point at the
1333 * separator just after the parsed key/value pair
1334 * @param key_val_sep a 0-terminated list of characters used to
1335 * separate key from value
1336 * @param pairs_sep a 0-terminated list of characters used to separate
1337 * two pairs from each other
1338 * @return 0 if the key/value pair has been successfully parsed and
1339 * set, or a negative value corresponding to an AVERROR code in case
1341 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1342 * the error code issued by av_opt_set() if the key/value pair
1345 static int parse_key_value_pair(void *ctx, const char **buf,
1346 const char *key_val_sep, const char *pairs_sep)
1348 char *key = av_get_token(buf, key_val_sep);
1353 return AVERROR(ENOMEM);
1355 if (*key && strspn(*buf, key_val_sep)) {
1357 val = av_get_token(buf, pairs_sep);
1360 return AVERROR(ENOMEM);
1363 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1365 return AVERROR(EINVAL);
1368 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1370 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1371 if (ret == AVERROR_OPTION_NOT_FOUND)
1372 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1379 int av_set_options_string(void *ctx, const char *opts,
1380 const char *key_val_sep, const char *pairs_sep)
1388 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1399 #define WHITESPACES " \n\t\r"
1401 static int is_key_char(char c)
1403 return (unsigned)((c | 32) - 'a') < 26 ||
1404 (unsigned)(c - '0') < 10 ||
1405 c == '-' || c == '_' || c == '/' || c == '.';
1409 * Read a key from a string.
1411 * The key consists of is_key_char characters and must be terminated by a
1412 * character from the delim string; spaces are ignored.
1414 * @return 0 for success (even with ellipsis), <0 for failure
1416 static int get_key(const char **ropts, const char *delim, char **rkey)
1418 const char *opts = *ropts;
1419 const char *key_start, *key_end;
1421 key_start = opts += strspn(opts, WHITESPACES);
1422 while (is_key_char(*opts))
1425 opts += strspn(opts, WHITESPACES);
1426 if (!*opts || !strchr(delim, *opts))
1427 return AVERROR(EINVAL);
1429 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1430 return AVERROR(ENOMEM);
1431 memcpy(*rkey, key_start, key_end - key_start);
1432 (*rkey)[key_end - key_start] = 0;
1437 int av_opt_get_key_value(const char **ropts,
1438 const char *key_val_sep, const char *pairs_sep,
1440 char **rkey, char **rval)
1443 char *key = NULL, *val;
1444 const char *opts = *ropts;
1446 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1447 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1448 return AVERROR(EINVAL);
1449 if (!(val = av_get_token(&opts, pairs_sep))) {
1451 return AVERROR(ENOMEM);
1459 int av_opt_set_from_string(void *ctx, const char *opts,
1460 const char *const *shorthand,
1461 const char *key_val_sep, const char *pairs_sep)
1464 const char *dummy_shorthand = NULL;
1465 char *av_uninit(parsed_key), *av_uninit(value);
1471 shorthand = &dummy_shorthand;
1474 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1475 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1476 &parsed_key, &value);
1478 if (ret == AVERROR(EINVAL))
1479 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1481 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1489 while (*shorthand) /* discard all remaining shorthand */
1492 key = *(shorthand++);
1495 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1496 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1497 if (ret == AVERROR_OPTION_NOT_FOUND)
1498 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1500 av_free(parsed_key);
1505 av_free(parsed_key);
1511 void av_opt_free(void *obj)
1513 const AVOption *o = NULL;
1514 while ((o = av_opt_next(obj, o))) {
1516 case AV_OPT_TYPE_STRING:
1517 case AV_OPT_TYPE_BINARY:
1518 av_freep((uint8_t *)obj + o->offset);
1521 case AV_OPT_TYPE_DICT:
1522 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1531 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1533 AVDictionaryEntry *t = NULL;
1534 AVDictionary *tmp = NULL;
1540 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1541 ret = av_opt_set(obj, t->key, t->value, search_flags);
1542 if (ret == AVERROR_OPTION_NOT_FOUND)
1543 ret = av_dict_set(&tmp, t->key, t->value, 0);
1545 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1551 av_dict_free(options);
1556 int av_opt_set_dict(void *obj, AVDictionary **options)
1558 return av_opt_set_dict2(obj, options, 0);
1561 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1562 int opt_flags, int search_flags)
1564 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1567 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1568 int opt_flags, int search_flags, void **target_obj)
1571 const AVOption *o = NULL;
1581 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1582 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1583 const AVClass *child = NULL;
1584 while (child = av_opt_child_class_next(c, child))
1585 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1589 while (child = av_opt_child_next(obj, child))
1590 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1595 while (o = av_opt_next(obj, o)) {
1596 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1597 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1598 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1600 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1611 void *av_opt_child_next(void *obj, void *prev)
1613 const AVClass *c = *(AVClass **)obj;
1615 return c->child_next(obj, prev);
1619 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1621 if (parent->child_class_next)
1622 return parent->child_class_next(prev);
1626 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1628 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1631 return (uint8_t*)obj + opt->offset;
1634 static int opt_size(enum AVOptionType type)
1637 case AV_OPT_TYPE_BOOL:
1638 case AV_OPT_TYPE_INT:
1639 case AV_OPT_TYPE_FLAGS:
1641 case AV_OPT_TYPE_DURATION:
1642 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1643 case AV_OPT_TYPE_INT64:
1644 return sizeof(int64_t);
1645 case AV_OPT_TYPE_DOUBLE:
1646 return sizeof(double);
1647 case AV_OPT_TYPE_FLOAT:
1648 return sizeof(float);
1649 case AV_OPT_TYPE_STRING:
1650 return sizeof(uint8_t*);
1651 case AV_OPT_TYPE_VIDEO_RATE:
1652 case AV_OPT_TYPE_RATIONAL:
1653 return sizeof(AVRational);
1654 case AV_OPT_TYPE_BINARY:
1655 return sizeof(uint8_t*) + sizeof(int);
1656 case AV_OPT_TYPE_IMAGE_SIZE:
1657 return sizeof(int[2]);
1658 case AV_OPT_TYPE_PIXEL_FMT:
1659 return sizeof(enum AVPixelFormat);
1660 case AV_OPT_TYPE_SAMPLE_FMT:
1661 return sizeof(enum AVSampleFormat);
1662 case AV_OPT_TYPE_COLOR:
1665 return AVERROR(EINVAL);
1668 int av_opt_copy(void *dst, const void *src)
1670 const AVOption *o = NULL;
1675 return AVERROR(EINVAL);
1677 c = *(AVClass **)src;
1678 if (!c || c != *(AVClass **)dst)
1679 return AVERROR(EINVAL);
1681 while ((o = av_opt_next(src, o))) {
1682 void *field_dst = (uint8_t *)dst + o->offset;
1683 void *field_src = (uint8_t *)src + o->offset;
1684 uint8_t **field_dst8 = (uint8_t **)field_dst;
1685 uint8_t **field_src8 = (uint8_t **)field_src;
1687 if (o->type == AV_OPT_TYPE_STRING) {
1688 if (*field_dst8 != *field_src8)
1689 av_freep(field_dst8);
1690 *field_dst8 = av_strdup(*field_src8);
1691 if (*field_src8 && !*field_dst8)
1692 ret = AVERROR(ENOMEM);
1693 } else if (o->type == AV_OPT_TYPE_BINARY) {
1694 int len = *(int *)(field_src8 + 1);
1695 if (*field_dst8 != *field_src8)
1696 av_freep(field_dst8);
1697 *field_dst8 = av_memdup(*field_src8, len);
1698 if (len && !*field_dst8) {
1699 ret = AVERROR(ENOMEM);
1702 *(int *)(field_dst8 + 1) = len;
1703 } else if (o->type == AV_OPT_TYPE_CONST) {
1705 } else if (o->type == AV_OPT_TYPE_DICT) {
1706 AVDictionary **sdict = (AVDictionary **) field_src;
1707 AVDictionary **ddict = (AVDictionary **) field_dst;
1708 if (*sdict != *ddict)
1709 av_dict_free(ddict);
1711 av_dict_copy(ddict, *sdict, 0);
1712 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1713 ret = AVERROR(ENOMEM);
1715 int size = opt_size(o->type);
1719 memcpy(field_dst, field_src, size);
1725 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1728 const AVClass *c = *(AVClass**)obj;
1729 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1731 if (c->version > (52 << 16 | 11 << 8))
1732 callback = c->query_ranges;
1735 callback = av_opt_query_ranges_default;
1737 ret = callback(ranges_arg, obj, key, flags);
1739 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1741 (*ranges_arg)->nb_components = ret;
1746 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1748 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1749 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1750 AVOptionRange *range = av_mallocz(sizeof(*range));
1751 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1756 if (!ranges || !range || !range_array || !field) {
1757 ret = AVERROR(ENOMEM);
1761 ranges->range = range_array;
1762 ranges->range[0] = range;
1763 ranges->nb_ranges = 1;
1764 ranges->nb_components = 1;
1765 range->is_range = 1;
1766 range->value_min = field->min;
1767 range->value_max = field->max;
1769 switch (field->type) {
1770 case AV_OPT_TYPE_BOOL:
1771 case AV_OPT_TYPE_INT:
1772 case AV_OPT_TYPE_INT64:
1773 case AV_OPT_TYPE_PIXEL_FMT:
1774 case AV_OPT_TYPE_SAMPLE_FMT:
1775 case AV_OPT_TYPE_FLOAT:
1776 case AV_OPT_TYPE_DOUBLE:
1777 case AV_OPT_TYPE_DURATION:
1778 case AV_OPT_TYPE_COLOR:
1779 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1781 case AV_OPT_TYPE_STRING:
1782 range->component_min = 0;
1783 range->component_max = 0x10FFFF; // max unicode value
1784 range->value_min = -1;
1785 range->value_max = INT_MAX;
1787 case AV_OPT_TYPE_RATIONAL:
1788 range->component_min = INT_MIN;
1789 range->component_max = INT_MAX;
1791 case AV_OPT_TYPE_IMAGE_SIZE:
1792 range->component_min = 0;
1793 range->component_max = INT_MAX/128/8;
1794 range->value_min = 0;
1795 range->value_max = INT_MAX/8;
1797 case AV_OPT_TYPE_VIDEO_RATE:
1798 range->component_min = 1;
1799 range->component_max = INT_MAX;
1800 range->value_min = 1;
1801 range->value_max = INT_MAX;
1804 ret = AVERROR(ENOSYS);
1808 *ranges_arg = ranges;
1813 av_free(range_array);
1817 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1820 AVOptionRanges *ranges = *rangesp;
1825 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1826 AVOptionRange *range = ranges->range[i];
1828 av_freep(&range->str);
1829 av_freep(&ranges->range[i]);
1832 av_freep(&ranges->range);
1836 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1847 return AVERROR(EINVAL);
1849 dst = ((uint8_t*)obj) + o->offset;
1852 case AV_OPT_TYPE_CONST:
1854 case AV_OPT_TYPE_BOOL:
1855 case AV_OPT_TYPE_FLAGS:
1856 case AV_OPT_TYPE_PIXEL_FMT:
1857 case AV_OPT_TYPE_SAMPLE_FMT:
1858 case AV_OPT_TYPE_INT:
1859 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1860 case AV_OPT_TYPE_DURATION:
1861 case AV_OPT_TYPE_INT64:
1862 read_number(o, dst, NULL, NULL, &i64);
1863 return o->default_val.i64 == i64;
1864 case AV_OPT_TYPE_STRING:
1865 str = *(char **)dst;
1866 if (str == o->default_val.str) //2 NULLs
1868 if (!str || !o->default_val.str) //1 NULL
1870 return !strcmp(str, o->default_val.str);
1871 case AV_OPT_TYPE_DOUBLE:
1872 read_number(o, dst, &d, NULL, NULL);
1873 return o->default_val.dbl == d;
1874 case AV_OPT_TYPE_FLOAT:
1875 read_number(o, dst, &d, NULL, NULL);
1876 f = o->default_val.dbl;
1879 case AV_OPT_TYPE_RATIONAL:
1880 q = av_d2q(o->default_val.dbl, INT_MAX);
1881 return !av_cmp_q(*(AVRational*)dst, q);
1882 case AV_OPT_TYPE_BINARY: {
1887 int opt_size = *(int *)((void **)dst + 1);
1888 void *opt_ptr = *(void **)dst;
1889 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1891 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1893 if (opt_size != strlen(o->default_val.str) / 2)
1895 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1897 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1901 case AV_OPT_TYPE_DICT:
1902 /* Binary and dict have not default support yet. Any pointer is not default. */
1903 return !!(*(void **)dst);
1904 case AV_OPT_TYPE_IMAGE_SIZE:
1905 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1907 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1909 return (w == *(int *)dst) && (h == *((int *)dst+1));
1910 case AV_OPT_TYPE_VIDEO_RATE:
1911 q = (AVRational){0, 0};
1912 if (o->default_val.str) {
1913 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1916 return !av_cmp_q(*(AVRational*)dst, q);
1917 case AV_OPT_TYPE_COLOR: {
1918 uint8_t color[4] = {0, 0, 0, 0};
1919 if (o->default_val.str) {
1920 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1923 return !memcmp(color, dst, sizeof(color));
1926 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1929 return AVERROR_PATCHWELCOME;
1932 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1937 return AVERROR(EINVAL);
1938 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1940 return AVERROR_OPTION_NOT_FOUND;
1941 return av_opt_is_set_to_default(target, o);
1944 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1945 const char key_val_sep, const char pairs_sep)
1947 const AVOption *o = NULL;
1951 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1953 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1954 pairs_sep == '\\' || key_val_sep == '\\') {
1955 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1956 return AVERROR(EINVAL);
1959 if (!obj || !buffer)
1960 return AVERROR(EINVAL);
1963 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
1965 while (o = av_opt_next(obj, o)) {
1966 if (o->type == AV_OPT_TYPE_CONST)
1968 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1970 else if (((o->flags & opt_flags) != opt_flags))
1972 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1974 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1975 av_bprint_finalize(&bprint, NULL);
1980 av_bprint_append_data(&bprint, &pairs_sep, 1);
1981 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1982 av_bprint_append_data(&bprint, &key_val_sep, 1);
1983 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1987 av_bprint_finalize(&bprint, buffer);
1993 typedef struct TestContext {
1994 const AVClass *class;
1999 AVRational rational;
2000 AVRational video_rate;
2002 enum AVPixelFormat pix_fmt;
2003 enum AVSampleFormat sample_fmt;
2006 int64_t channel_layout;
2022 #define OFFSET(x) offsetof(TestContext, x)
2024 #define TEST_FLAG_COOL 01
2025 #define TEST_FLAG_LAME 02
2026 #define TEST_FLAG_MU 04
2028 static const AVOption test_options[]= {
2029 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, 1 },
2030 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
2031 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
2032 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
2033 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 },
2034 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, "flags" },
2035 {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, "flags" },
2036 {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, "flags" },
2037 {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, "flags" },
2038 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 },
2039 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 },
2040 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 },
2041 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, 0, 1 },
2042 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 },
2043 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 },
2044 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_HEXAGONAL }, 0, INT64_MAX, 1 },
2045 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 },
2046 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 },
2047 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 },
2048 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1 }, 0, 100, 1 },
2049 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 },
2050 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 },
2051 {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 },
2052 {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 },
2053 {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 },
2057 static const char *test_get_name(void *ctx)
2062 static const AVClass test_class = {
2068 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
2070 vfprintf(stdout, fmt, vl);
2077 av_log_set_level(AV_LOG_DEBUG);
2078 av_log_set_callback(log_callback_help);
2080 printf("Testing default values\n");
2082 TestContext test_ctx = { 0 };
2083 test_ctx.class = &test_class;
2084 av_opt_set_defaults(&test_ctx);
2086 printf("num=%d\n", test_ctx.num);
2087 printf("toggle=%d\n", test_ctx.toggle);
2088 printf("string=%s\n", test_ctx.string);
2089 printf("escape=%s\n", test_ctx.escape);
2090 printf("flags=%d\n", test_ctx.flags);
2091 printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
2092 printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
2093 printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
2094 printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
2095 printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
2096 printf("duration=%"PRId64"\n", test_ctx.duration);
2097 printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
2098 printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
2099 if (test_ctx.binary)
2100 printf("binary=%x %x %x %x\n", ((uint8_t*)test_ctx.binary)[0], ((uint8_t*)test_ctx.binary)[1], ((uint8_t*)test_ctx.binary)[2], ((uint8_t*)test_ctx.binary)[3]);
2101 printf("binary_size=%d\n", test_ctx.binary_size);
2102 printf("num64=%"PRId64"\n", test_ctx.num64);
2103 printf("flt=%.6f\n", test_ctx.flt);
2104 printf("dbl=%.6f\n", test_ctx.dbl);
2106 av_opt_show2(&test_ctx, NULL, -1, 0);
2108 av_opt_free(&test_ctx);
2111 printf("\nTesting av_opt_is_set_to_default()\n");
2114 TestContext test_ctx = { 0 };
2115 const AVOption *o = NULL;
2116 test_ctx.class = &test_class;
2118 av_log_set_level(AV_LOG_QUIET);
2120 while (o = av_opt_next(&test_ctx, o)) {
2121 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2122 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2124 av_opt_set_defaults(&test_ctx);
2125 while (o = av_opt_next(&test_ctx, o)) {
2126 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2127 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2129 av_opt_free(&test_ctx);
2132 printf("\nTest av_opt_serialize()\n");
2134 TestContext test_ctx = { 0 };
2136 test_ctx.class = &test_class;
2138 av_log_set_level(AV_LOG_QUIET);
2140 av_opt_set_defaults(&test_ctx);
2141 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2142 printf("%s\n", buf);
2143 av_opt_free(&test_ctx);
2144 memset(&test_ctx, 0, sizeof(test_ctx));
2145 test_ctx.class = &test_class;
2146 av_set_options_string(&test_ctx, buf, "=", ",");
2148 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2149 printf("%s\n", buf);
2153 av_opt_free(&test_ctx);
2156 printf("\nTesting av_set_options_string()\n");
2158 TestContext test_ctx = { 0 };
2159 static const char * const options[] = {
2175 "flags=+mu-lame : num=42: toggle=0",
2176 "num=42 : string=blahblah",
2177 "rational=0 : rational=1/2 : rational=1/-1",
2190 "video_rate=30000/1001",
2191 "video_rate=30/1.001",
2195 "duration=1\\:23\\:45.67",
2199 "cl=stereo+downmix",
2223 test_ctx.class = &test_class;
2224 av_opt_set_defaults(&test_ctx);
2226 av_log_set_level(AV_LOG_QUIET);
2228 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2229 int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
2230 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2232 av_log_set_callback(NULL);
2233 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
2234 printf("Error '%s'\n", options[i]);
2236 printf("OK '%s'\n", options[i]);
2237 av_log_set_callback(log_callback_help);
2239 av_opt_free(&test_ctx);
2242 printf("\nTesting av_opt_set_from_string()\n");
2244 TestContext test_ctx = { 0 };
2245 static const char * const options[] = {
2253 " 5 : hello : size = pal ",
2254 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
2256 static const char * const shorthand[] = { "num", "string", NULL };
2258 test_ctx.class = &test_class;
2259 av_opt_set_defaults(&test_ctx);
2261 av_log_set_level(AV_LOG_QUIET);
2263 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2264 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2265 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
2266 printf("Error '%s'\n", options[i]);
2268 printf("OK '%s'\n", options[i]);
2270 av_opt_free(&test_ctx);