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"
37 #include "parseutils.h"
39 #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: *intnum = *(unsigned int*)dst;return 0;
62 case AV_OPT_TYPE_PIXEL_FMT: *intnum = *(enum AVPixelFormat *)dst;return 0;
63 case AV_OPT_TYPE_SAMPLE_FMT:*intnum = *(enum AVSampleFormat*)dst;return 0;
64 case AV_OPT_TYPE_BOOL:
65 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
66 case AV_OPT_TYPE_CHANNEL_LAYOUT:
67 case AV_OPT_TYPE_DURATION:
68 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
69 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
70 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
71 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
72 *den = ((AVRational*)dst)->den;
74 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
76 return AVERROR(EINVAL);
79 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
81 if (o->type != AV_OPT_TYPE_FLAGS &&
82 (o->max * den < num * intnum || o->min * den > num * intnum)) {
83 num = den ? num*intnum/den : (num*intnum ? INFINITY : NAN);
84 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
85 num, o->name, o->min, o->max);
86 return AVERROR(ERANGE);
88 if (o->type == AV_OPT_TYPE_FLAGS) {
89 double d = num*intnum/den;
90 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
91 av_log(obj, AV_LOG_ERROR,
92 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
93 num*intnum/den, o->name);
94 return AVERROR(ERANGE);
99 case AV_OPT_TYPE_PIXEL_FMT: *(enum AVPixelFormat *)dst = llrint(num/den) * intnum; break;
100 case AV_OPT_TYPE_SAMPLE_FMT:*(enum AVSampleFormat*)dst = llrint(num/den) * intnum; break;
101 case AV_OPT_TYPE_BOOL:
102 case AV_OPT_TYPE_FLAGS:
103 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
104 case AV_OPT_TYPE_DURATION:
105 case AV_OPT_TYPE_CHANNEL_LAYOUT:
106 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
107 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
108 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
109 case AV_OPT_TYPE_RATIONAL:
110 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
111 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
114 return AVERROR(EINVAL);
119 static int hexchar2int(char c) {
120 if (c >= '0' && c <= '9') return c - '0';
121 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
122 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
126 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
128 int *lendst = (int *)(dst + 1);
135 if (!val || !(len = strlen(val)))
139 return AVERROR(EINVAL);
142 ptr = bin = av_malloc(len);
144 return AVERROR(ENOMEM);
146 int a = hexchar2int(*val++);
147 int b = hexchar2int(*val++);
148 if (a < 0 || b < 0) {
150 return AVERROR(EINVAL);
152 *ptr++ = (a << 4) | b;
160 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
163 *dst = av_strdup(val);
164 return *dst ? 0 : AVERROR(ENOMEM);
167 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
168 opt->type == AV_OPT_TYPE_CONST || \
169 opt->type == AV_OPT_TYPE_FLAGS || \
170 opt->type == AV_OPT_TYPE_INT) ? \
171 opt->default_val.i64 : opt->default_val.dbl)
173 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
179 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
180 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
192 if (o->type == AV_OPT_TYPE_FLAGS) {
193 if (*val == '+' || *val == '-')
195 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
201 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
204 double const_values[64];
205 const char * const_names[64];
206 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
207 d = DEFAULT_NUMVAL(o_named);
210 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
211 if (o_named->type == AV_OPT_TYPE_CONST &&
213 !strcmp(o_named->unit, o->unit)) {
214 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
215 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
216 return AVERROR_PATCHWELCOME;
218 const_names [ci ] = o_named->name;
219 const_values[ci++] = DEFAULT_NUMVAL(o_named);
223 const_names [ci ] = "default";
224 const_values[ci++] = DEFAULT_NUMVAL(o);
225 const_names [ci ] = "max";
226 const_values[ci++] = o->max;
227 const_names [ci ] = "min";
228 const_values[ci++] = o->min;
229 const_names [ci ] = "none";
230 const_values[ci++] = 0;
231 const_names [ci ] = "all";
232 const_values[ci++] = ~0;
233 const_names [ci] = NULL;
234 const_values[ci] = 0;
236 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
237 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
239 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
244 if (o->type == AV_OPT_TYPE_FLAGS) {
245 read_number(o, dst, NULL, NULL, &intnum);
246 if (cmd == '+') d = intnum | (int64_t)d;
247 else if (cmd == '-') d = intnum &~(int64_t)d;
250 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
260 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
264 if (!val || !strcmp(val, "none")) {
269 ret = av_parse_video_size(dst, dst + 1, val);
271 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
275 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
279 ret = AVERROR(EINVAL);
281 ret = av_parse_video_rate(dst, val);
284 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
288 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
295 ret = av_parse_color(dst, val, -1, obj);
297 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
303 static const char *get_bool_name(int val)
307 return val ? "true" : "false";
310 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
317 if (!strcmp(val, "auto")) {
319 } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
321 } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
325 n = strtol(val, &end, 10);
326 if (val + strlen(val) != end)
330 if (n < o->min || n > o->max)
337 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
338 return AVERROR(EINVAL);
341 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
342 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
346 if (!val || !strcmp(val, "none")) {
352 fmt = strtol(val, &tail, 0);
353 if (*tail || (unsigned)fmt >= fmt_nb) {
354 av_log(obj, AV_LOG_ERROR,
355 "Unable to parse option value \"%s\" as %s\n", val, desc);
356 return AVERROR(EINVAL);
361 min = FFMAX(o->min, -1);
362 max = FFMIN(o->max, fmt_nb-1);
364 // hack for compatibility with old ffmpeg
365 if(min == 0 && max == 0) {
370 if (fmt < min || fmt > max) {
371 av_log(obj, AV_LOG_ERROR,
372 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
373 fmt, o->name, desc, min, max);
374 return AVERROR(ERANGE);
381 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
383 return set_string_fmt(obj, o, val, dst,
384 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
387 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
389 return set_string_fmt(obj, o, val, dst,
390 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
393 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
396 void *dst, *target_obj;
397 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
398 if (!o || !target_obj)
399 return AVERROR_OPTION_NOT_FOUND;
400 if (!val && (o->type != AV_OPT_TYPE_STRING &&
401 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
402 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
403 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
404 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
405 return AVERROR(EINVAL);
407 if (o->flags & AV_OPT_FLAG_READONLY)
408 return AVERROR(EINVAL);
410 dst = ((uint8_t*)target_obj) + o->offset;
412 case AV_OPT_TYPE_BOOL: return set_string_bool(obj, o, val, dst);
413 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
414 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
415 case AV_OPT_TYPE_FLAGS:
416 case AV_OPT_TYPE_INT:
417 case AV_OPT_TYPE_INT64:
418 case AV_OPT_TYPE_FLOAT:
419 case AV_OPT_TYPE_DOUBLE:
420 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
421 case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
422 case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
423 case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
424 case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
425 case AV_OPT_TYPE_DURATION:
430 if ((ret = av_parse_time(dst, val, 1)) < 0)
431 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
435 case AV_OPT_TYPE_COLOR: return set_string_color(obj, o, val, dst);
436 case AV_OPT_TYPE_CHANNEL_LAYOUT:
437 if (!val || !strcmp(val, "none")) {
440 int64_t cl = av_get_channel_layout(val);
442 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
443 ret = AVERROR(EINVAL);
445 *(int64_t *)dst = cl;
451 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
452 return AVERROR(EINVAL);
455 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
456 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
458 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
459 return AVERROR(EINVAL);\
460 return set_string_number(obj, obj, o, val, name ## _out);\
463 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
464 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
465 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
466 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
467 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
468 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
470 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
473 void *dst, *target_obj;
474 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
476 if (!o || !target_obj)
477 return AVERROR_OPTION_NOT_FOUND;
479 if (o->flags & AV_OPT_FLAG_READONLY)
480 return AVERROR(EINVAL);
482 dst = ((uint8_t*)target_obj) + o->offset;
483 return write_number(obj, o, dst, num, den, intnum);
486 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
488 return set_number(obj, name, 1, 1, val, search_flags);
491 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
493 return set_number(obj, name, val, 1, 1, search_flags);
496 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
498 return set_number(obj, name, val.num, val.den, 1, search_flags);
501 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
504 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
509 if (!o || !target_obj)
510 return AVERROR_OPTION_NOT_FOUND;
512 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
513 return AVERROR(EINVAL);
515 ptr = len ? av_malloc(len) : NULL;
517 return AVERROR(ENOMEM);
519 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
520 lendst = (int *)(dst + 1);
526 memcpy(ptr, val, len);
531 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
534 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
536 if (!o || !target_obj)
537 return AVERROR_OPTION_NOT_FOUND;
538 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
539 av_log(obj, AV_LOG_ERROR,
540 "The value set by option '%s' is not an image size.\n", o->name);
541 return AVERROR(EINVAL);
544 av_log(obj, AV_LOG_ERROR,
545 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
546 return AVERROR(EINVAL);
548 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
549 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
553 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
556 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
558 if (!o || !target_obj)
559 return AVERROR_OPTION_NOT_FOUND;
560 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
561 av_log(obj, AV_LOG_ERROR,
562 "The value set by option '%s' is not a video rate.\n", o->name);
563 return AVERROR(EINVAL);
565 if (val.num <= 0 || val.den <= 0)
566 return AVERROR(EINVAL);
567 return set_number(obj, name, val.num, val.den, 1, search_flags);
570 static int set_format(void *obj, const char *name, int fmt, int search_flags,
571 enum AVOptionType type, const char *desc, int nb_fmts)
574 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
575 search_flags, &target_obj);
578 if (!o || !target_obj)
579 return AVERROR_OPTION_NOT_FOUND;
580 if (o->type != type) {
581 av_log(obj, AV_LOG_ERROR,
582 "The value set by option '%s' is not a %s format", name, desc);
583 return AVERROR(EINVAL);
586 min = FFMAX(o->min, -1);
587 max = FFMIN(o->max, nb_fmts-1);
589 if (fmt < min || fmt > max) {
590 av_log(obj, AV_LOG_ERROR,
591 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
592 fmt, name, desc, min, max);
593 return AVERROR(ERANGE);
595 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
599 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
601 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
604 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
606 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
609 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
612 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
614 if (!o || !target_obj)
615 return AVERROR_OPTION_NOT_FOUND;
616 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
617 av_log(obj, AV_LOG_ERROR,
618 "The value set by option '%s' is not a channel layout.\n", o->name);
619 return AVERROR(EINVAL);
621 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
625 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
629 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
631 if (!o || !target_obj)
632 return AVERROR_OPTION_NOT_FOUND;
633 if (o->flags & AV_OPT_FLAG_READONLY)
634 return AVERROR(EINVAL);
636 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
638 av_dict_copy(dst, val, 0);
643 static void format_duration(char *buf, size_t size, int64_t d)
647 av_assert0(size >= 25);
648 if (d < 0 && d != INT64_MIN) {
654 snprintf(buf, size, "INT64_MAX");
655 else if (d == INT64_MIN)
656 snprintf(buf, size, "INT64_MIN");
657 else if (d > (int64_t)3600*1000000)
658 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
659 (int)((d / 60000000) % 60),
660 (int)((d / 1000000) % 60),
662 else if (d > 60*1000000)
663 snprintf(buf, size, "%d:%02d.%06d",
665 (int)((d / 1000000) % 60),
668 snprintf(buf, size, "%d.%06d",
671 e = buf + strlen(buf);
672 while (e > buf && e[-1] == '0')
674 if (e > buf && e[-1] == '.')
678 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
680 void *dst, *target_obj;
681 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
682 uint8_t *bin, buf[128];
686 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
687 return AVERROR_OPTION_NOT_FOUND;
689 dst = (uint8_t*)target_obj + o->offset;
693 case AV_OPT_TYPE_BOOL:
694 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
696 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
697 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
698 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
699 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
700 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
701 case AV_OPT_TYPE_VIDEO_RATE:
702 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
703 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
704 case AV_OPT_TYPE_STRING:
705 if (*(uint8_t**)dst) {
706 *out_val = av_strdup(*(uint8_t**)dst);
707 } else if (search_flags & AV_OPT_ALLOW_NULL) {
711 *out_val = av_strdup("");
713 return *out_val ? 0 : AVERROR(ENOMEM);
714 case AV_OPT_TYPE_BINARY:
715 if (!*(uint8_t**)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
719 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
720 if ((uint64_t)len*2 + 1 > INT_MAX)
721 return AVERROR(EINVAL);
722 if (!(*out_val = av_malloc(len*2 + 1)))
723 return AVERROR(ENOMEM);
728 bin = *(uint8_t**)dst;
729 for (i = 0; i < len; i++)
730 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
732 case AV_OPT_TYPE_IMAGE_SIZE:
733 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
735 case AV_OPT_TYPE_PIXEL_FMT:
736 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
738 case AV_OPT_TYPE_SAMPLE_FMT:
739 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
741 case AV_OPT_TYPE_DURATION:
742 i64 = *(int64_t *)dst;
743 format_duration(buf, sizeof(buf), i64);
744 ret = strlen(buf); // no overflow possible, checked by an assert
746 case AV_OPT_TYPE_COLOR:
747 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
748 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
749 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
751 case AV_OPT_TYPE_CHANNEL_LAYOUT:
752 i64 = *(int64_t *)dst;
753 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
756 return AVERROR(EINVAL);
759 if (ret >= sizeof(buf))
760 return AVERROR(EINVAL);
761 *out_val = av_strdup(buf);
762 return *out_val ? 0 : AVERROR(ENOMEM);
765 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
768 void *dst, *target_obj;
769 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
770 if (!o || !target_obj)
773 dst = ((uint8_t*)target_obj) + o->offset;
775 if (o_out) *o_out= o;
777 return read_number(o, dst, num, den, intnum);
784 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
790 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
792 *out_val = num*intnum/den;
796 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
802 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
804 *out_val = num*intnum/den;
808 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
814 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
817 if (num == 1.0 && (int)intnum == intnum)
818 *out_val = (AVRational){intnum, den};
820 *out_val = av_d2q(num*intnum/den, 1<<24);
824 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
826 void *dst, *target_obj;
827 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
828 if (!o || !target_obj)
829 return AVERROR_OPTION_NOT_FOUND;
830 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
831 av_log(obj, AV_LOG_ERROR,
832 "The value for option '%s' is not an image size.\n", name);
833 return AVERROR(EINVAL);
836 dst = ((uint8_t*)target_obj) + o->offset;
837 if (w_out) *w_out = *(int *)dst;
838 if (h_out) *h_out = *((int *)dst+1);
842 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
848 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
851 if (num == 1.0 && (int)intnum == intnum)
852 *out_val = (AVRational){intnum, den};
854 *out_val = av_d2q(num*intnum/den, 1<<24);
858 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
859 enum AVOptionType type, const char *desc)
861 void *dst, *target_obj;
862 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
863 if (!o || !target_obj)
864 return AVERROR_OPTION_NOT_FOUND;
865 if (o->type != type) {
866 av_log(obj, AV_LOG_ERROR,
867 "The value for option '%s' is not a %s format.\n", desc, name);
868 return AVERROR(EINVAL);
871 dst = ((uint8_t*)target_obj) + o->offset;
872 *out_fmt = *(int *)dst;
876 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
878 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
881 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
883 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
886 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
888 void *dst, *target_obj;
889 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
890 if (!o || !target_obj)
891 return AVERROR_OPTION_NOT_FOUND;
892 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
893 av_log(obj, AV_LOG_ERROR,
894 "The value for option '%s' is not a channel layout.\n", name);
895 return AVERROR(EINVAL);
898 dst = ((uint8_t*)target_obj) + o->offset;
899 *cl = *(int64_t *)dst;
903 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
907 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
909 if (!o || !target_obj)
910 return AVERROR_OPTION_NOT_FOUND;
911 if (o->type != AV_OPT_TYPE_DICT)
912 return AVERROR(EINVAL);
914 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
915 av_dict_copy(out_val, src, 0);
920 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
922 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
923 const AVOption *flag = av_opt_find(obj, flag_name,
924 field ? field->unit : NULL, 0, 0);
927 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
928 av_opt_get_int(obj, field_name, 0, &res) < 0)
930 return res & flag->default_val.i64;
933 static void log_value(void *av_log_obj, int level, double d)
936 av_log(av_log_obj, level, "INT_MAX");
937 } else if (d == INT_MIN) {
938 av_log(av_log_obj, level, "INT_MIN");
939 } else if (d == UINT32_MAX) {
940 av_log(av_log_obj, level, "UINT32_MAX");
941 } else if (d == (double)INT64_MAX) {
942 av_log(av_log_obj, level, "I64_MAX");
943 } else if (d == INT64_MIN) {
944 av_log(av_log_obj, level, "I64_MIN");
945 } else if (d == FLT_MAX) {
946 av_log(av_log_obj, level, "FLT_MAX");
947 } else if (d == FLT_MIN) {
948 av_log(av_log_obj, level, "FLT_MIN");
949 } else if (d == -FLT_MAX) {
950 av_log(av_log_obj, level, "-FLT_MAX");
951 } else if (d == -FLT_MIN) {
952 av_log(av_log_obj, level, "-FLT_MIN");
953 } else if (d == DBL_MAX) {
954 av_log(av_log_obj, level, "DBL_MAX");
955 } else if (d == DBL_MIN) {
956 av_log(av_log_obj, level, "DBL_MIN");
957 } else if (d == -DBL_MAX) {
958 av_log(av_log_obj, level, "-DBL_MAX");
959 } else if (d == -DBL_MIN) {
960 av_log(av_log_obj, level, "-DBL_MIN");
962 av_log(av_log_obj, level, "%g", d);
966 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
968 const AVOption *opt = NULL;
972 while ((opt = av_opt_next(obj, opt)))
973 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
974 opt->default_val.i64 == value)
979 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
981 const AVOption *opt = NULL;
987 while ((opt = av_opt_next(obj, opt))) {
988 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
989 opt->default_val.i64 & value) {
991 av_strlcatf(flags, sizeof(flags), "+");
992 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
996 return av_strdup(flags);
1000 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1001 int req_flags, int rej_flags)
1003 const AVOption *opt=NULL;
1007 while ((opt = av_opt_next(obj, opt))) {
1008 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1011 /* Don't print CONST's on level one.
1012 * Don't print anything but CONST's on level two.
1013 * Only print items from the requested unit.
1015 if (!unit && opt->type==AV_OPT_TYPE_CONST)
1017 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
1019 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1021 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1022 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1024 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1025 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1028 switch (opt->type) {
1029 case AV_OPT_TYPE_FLAGS:
1030 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1032 case AV_OPT_TYPE_INT:
1033 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1035 case AV_OPT_TYPE_INT64:
1036 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1038 case AV_OPT_TYPE_DOUBLE:
1039 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1041 case AV_OPT_TYPE_FLOAT:
1042 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1044 case AV_OPT_TYPE_STRING:
1045 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1047 case AV_OPT_TYPE_RATIONAL:
1048 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1050 case AV_OPT_TYPE_BINARY:
1051 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1053 case AV_OPT_TYPE_IMAGE_SIZE:
1054 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1056 case AV_OPT_TYPE_VIDEO_RATE:
1057 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1059 case AV_OPT_TYPE_PIXEL_FMT:
1060 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1062 case AV_OPT_TYPE_SAMPLE_FMT:
1063 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1065 case AV_OPT_TYPE_DURATION:
1066 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1068 case AV_OPT_TYPE_COLOR:
1069 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1071 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1072 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1074 case AV_OPT_TYPE_BOOL:
1075 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1077 case AV_OPT_TYPE_CONST:
1079 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1082 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1083 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1084 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1085 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1086 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1087 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1088 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1089 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1092 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1094 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1095 switch (opt->type) {
1096 case AV_OPT_TYPE_INT:
1097 case AV_OPT_TYPE_INT64:
1098 case AV_OPT_TYPE_DOUBLE:
1099 case AV_OPT_TYPE_FLOAT:
1100 case AV_OPT_TYPE_RATIONAL:
1101 for (i = 0; i < r->nb_ranges; i++) {
1102 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1103 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1104 av_log(av_log_obj, AV_LOG_INFO, " to ");
1105 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1106 av_log(av_log_obj, AV_LOG_INFO, ")");
1110 av_opt_freep_ranges(&r);
1113 if (opt->type != AV_OPT_TYPE_CONST &&
1114 opt->type != AV_OPT_TYPE_BINARY &&
1115 !((opt->type == AV_OPT_TYPE_COLOR ||
1116 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1117 opt->type == AV_OPT_TYPE_STRING ||
1118 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1119 !opt->default_val.str)) {
1120 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1121 switch (opt->type) {
1122 case AV_OPT_TYPE_BOOL:
1123 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1125 case AV_OPT_TYPE_FLAGS: {
1126 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1128 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1129 av_freep(&def_flags);
1131 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1135 case AV_OPT_TYPE_DURATION: {
1137 format_duration(buf, sizeof(buf), opt->default_val.i64);
1138 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1141 case AV_OPT_TYPE_INT:
1142 case AV_OPT_TYPE_INT64: {
1143 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1145 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1147 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1150 case AV_OPT_TYPE_DOUBLE:
1151 case AV_OPT_TYPE_FLOAT:
1152 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1154 case AV_OPT_TYPE_RATIONAL: {
1155 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1156 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1158 case AV_OPT_TYPE_PIXEL_FMT:
1159 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1161 case AV_OPT_TYPE_SAMPLE_FMT:
1162 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1164 case AV_OPT_TYPE_COLOR:
1165 case AV_OPT_TYPE_IMAGE_SIZE:
1166 case AV_OPT_TYPE_STRING:
1167 case AV_OPT_TYPE_VIDEO_RATE:
1168 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1170 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1171 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1174 av_log(av_log_obj, AV_LOG_INFO, ")");
1177 av_log(av_log_obj, AV_LOG_INFO, "\n");
1178 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1179 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1184 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1189 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1191 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1196 void av_opt_set_defaults(void *s)
1198 av_opt_set_defaults2(s, 0, 0);
1201 void av_opt_set_defaults2(void *s, int mask, int flags)
1203 const AVOption *opt = NULL;
1204 while ((opt = av_opt_next(s, opt))) {
1205 void *dst = ((uint8_t*)s) + opt->offset;
1207 if ((opt->flags & mask) != flags)
1210 if (opt->flags & AV_OPT_FLAG_READONLY)
1213 switch (opt->type) {
1214 case AV_OPT_TYPE_CONST:
1215 /* Nothing to be done here */
1217 case AV_OPT_TYPE_BOOL:
1218 case AV_OPT_TYPE_FLAGS:
1219 case AV_OPT_TYPE_INT:
1220 case AV_OPT_TYPE_INT64:
1221 case AV_OPT_TYPE_DURATION:
1222 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1223 case AV_OPT_TYPE_PIXEL_FMT:
1224 case AV_OPT_TYPE_SAMPLE_FMT:
1225 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1227 case AV_OPT_TYPE_DOUBLE:
1228 case AV_OPT_TYPE_FLOAT: {
1230 val = opt->default_val.dbl;
1231 write_number(s, opt, dst, val, 1, 1);
1234 case AV_OPT_TYPE_RATIONAL: {
1236 val = av_d2q(opt->default_val.dbl, INT_MAX);
1237 write_number(s, opt, dst, 1, val.den, val.num);
1240 case AV_OPT_TYPE_COLOR:
1241 set_string_color(s, opt, opt->default_val.str, dst);
1243 case AV_OPT_TYPE_STRING:
1244 set_string(s, opt, opt->default_val.str, dst);
1246 case AV_OPT_TYPE_IMAGE_SIZE:
1247 set_string_image_size(s, opt, opt->default_val.str, dst);
1249 case AV_OPT_TYPE_VIDEO_RATE:
1250 set_string_video_rate(s, opt, opt->default_val.str, dst);
1252 case AV_OPT_TYPE_BINARY:
1253 set_string_binary(s, opt, opt->default_val.str, dst);
1255 case AV_OPT_TYPE_DICT:
1256 /* Cannot set defaults for these types */
1259 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1265 * Store the value in the field in ctx that is named like key.
1266 * ctx must be an AVClass context, storing is done using AVOptions.
1268 * @param buf the string to parse, buf will be updated to point at the
1269 * separator just after the parsed key/value pair
1270 * @param key_val_sep a 0-terminated list of characters used to
1271 * separate key from value
1272 * @param pairs_sep a 0-terminated list of characters used to separate
1273 * two pairs from each other
1274 * @return 0 if the key/value pair has been successfully parsed and
1275 * set, or a negative value corresponding to an AVERROR code in case
1277 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1278 * the error code issued by av_opt_set() if the key/value pair
1281 static int parse_key_value_pair(void *ctx, const char **buf,
1282 const char *key_val_sep, const char *pairs_sep)
1284 char *key = av_get_token(buf, key_val_sep);
1289 return AVERROR(ENOMEM);
1291 if (*key && strspn(*buf, key_val_sep)) {
1293 val = av_get_token(buf, pairs_sep);
1296 return AVERROR(ENOMEM);
1299 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1301 return AVERROR(EINVAL);
1304 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1306 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1307 if (ret == AVERROR_OPTION_NOT_FOUND)
1308 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1315 int av_set_options_string(void *ctx, const char *opts,
1316 const char *key_val_sep, const char *pairs_sep)
1324 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1335 #define WHITESPACES " \n\t"
1337 static int is_key_char(char c)
1339 return (unsigned)((c | 32) - 'a') < 26 ||
1340 (unsigned)(c - '0') < 10 ||
1341 c == '-' || c == '_' || c == '/' || c == '.';
1345 * Read a key from a string.
1347 * The key consists of is_key_char characters and must be terminated by a
1348 * character from the delim string; spaces are ignored.
1350 * @return 0 for success (even with ellipsis), <0 for failure
1352 static int get_key(const char **ropts, const char *delim, char **rkey)
1354 const char *opts = *ropts;
1355 const char *key_start, *key_end;
1357 key_start = opts += strspn(opts, WHITESPACES);
1358 while (is_key_char(*opts))
1361 opts += strspn(opts, WHITESPACES);
1362 if (!*opts || !strchr(delim, *opts))
1363 return AVERROR(EINVAL);
1365 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1366 return AVERROR(ENOMEM);
1367 memcpy(*rkey, key_start, key_end - key_start);
1368 (*rkey)[key_end - key_start] = 0;
1373 int av_opt_get_key_value(const char **ropts,
1374 const char *key_val_sep, const char *pairs_sep,
1376 char **rkey, char **rval)
1379 char *key = NULL, *val;
1380 const char *opts = *ropts;
1382 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1383 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1384 return AVERROR(EINVAL);
1385 if (!(val = av_get_token(&opts, pairs_sep))) {
1387 return AVERROR(ENOMEM);
1395 int av_opt_set_from_string(void *ctx, const char *opts,
1396 const char *const *shorthand,
1397 const char *key_val_sep, const char *pairs_sep)
1400 const char *dummy_shorthand = NULL;
1401 char *av_uninit(parsed_key), *av_uninit(value);
1407 shorthand = &dummy_shorthand;
1410 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1411 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1412 &parsed_key, &value);
1414 if (ret == AVERROR(EINVAL))
1415 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1417 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1425 while (*shorthand) /* discard all remaining shorthand */
1428 key = *(shorthand++);
1431 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1432 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1433 if (ret == AVERROR_OPTION_NOT_FOUND)
1434 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1436 av_free(parsed_key);
1441 av_free(parsed_key);
1447 void av_opt_free(void *obj)
1449 const AVOption *o = NULL;
1450 while ((o = av_opt_next(obj, o))) {
1452 case AV_OPT_TYPE_STRING:
1453 case AV_OPT_TYPE_BINARY:
1454 av_freep((uint8_t *)obj + o->offset);
1457 case AV_OPT_TYPE_DICT:
1458 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1467 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1469 AVDictionaryEntry *t = NULL;
1470 AVDictionary *tmp = NULL;
1476 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1477 ret = av_opt_set(obj, t->key, t->value, search_flags);
1478 if (ret == AVERROR_OPTION_NOT_FOUND)
1479 ret = av_dict_set(&tmp, t->key, t->value, 0);
1481 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1487 av_dict_free(options);
1492 int av_opt_set_dict(void *obj, AVDictionary **options)
1494 return av_opt_set_dict2(obj, options, 0);
1497 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1498 int opt_flags, int search_flags)
1500 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1503 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1504 int opt_flags, int search_flags, void **target_obj)
1507 const AVOption *o = NULL;
1517 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1518 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1519 const AVClass *child = NULL;
1520 while (child = av_opt_child_class_next(c, child))
1521 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1525 while (child = av_opt_child_next(obj, child))
1526 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1531 while (o = av_opt_next(obj, o)) {
1532 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1533 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1534 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1536 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1547 void *av_opt_child_next(void *obj, void *prev)
1549 const AVClass *c = *(AVClass**)obj;
1551 return c->child_next(obj, prev);
1555 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1557 if (parent->child_class_next)
1558 return parent->child_class_next(prev);
1562 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1564 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1567 return (uint8_t*)obj + opt->offset;
1570 static int opt_size(enum AVOptionType type)
1573 case AV_OPT_TYPE_BOOL:
1574 case AV_OPT_TYPE_INT:
1575 case AV_OPT_TYPE_FLAGS: return sizeof(int);
1576 case AV_OPT_TYPE_DURATION:
1577 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1578 case AV_OPT_TYPE_INT64: return sizeof(int64_t);
1579 case AV_OPT_TYPE_DOUBLE: return sizeof(double);
1580 case AV_OPT_TYPE_FLOAT: return sizeof(float);
1581 case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
1582 case AV_OPT_TYPE_VIDEO_RATE:
1583 case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
1584 case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
1585 case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
1586 case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
1587 case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
1588 case AV_OPT_TYPE_COLOR: return 4;
1593 int av_opt_copy(void *dst, const void *src)
1595 const AVOption *o = NULL;
1602 c = *(AVClass**)src;
1603 if (*(AVClass**)dst && c != *(AVClass**)dst)
1604 return AVERROR(EINVAL);
1606 while ((o = av_opt_next(src, o))) {
1607 void *field_dst = ((uint8_t*)dst) + o->offset;
1608 void *field_src = ((uint8_t*)src) + o->offset;
1609 uint8_t **field_dst8 = (uint8_t**)field_dst;
1610 uint8_t **field_src8 = (uint8_t**)field_src;
1612 if (o->type == AV_OPT_TYPE_STRING) {
1613 if (*field_dst8 != *field_src8)
1614 av_freep(field_dst8);
1615 *field_dst8 = av_strdup(*field_src8);
1616 if (*field_src8 && !*field_dst8)
1617 ret = AVERROR(ENOMEM);
1618 } else if (o->type == AV_OPT_TYPE_BINARY) {
1619 int len = *(int*)(field_src8 + 1);
1620 if (*field_dst8 != *field_src8)
1621 av_freep(field_dst8);
1622 *field_dst8 = av_memdup(*field_src8, len);
1623 if (len && !*field_dst8) {
1624 ret = AVERROR(ENOMEM);
1627 *(int*)(field_dst8 + 1) = len;
1628 } else if (o->type == AV_OPT_TYPE_CONST) {
1630 } else if (o->type == AV_OPT_TYPE_DICT) {
1631 AVDictionary **sdict = (AVDictionary **) field_src;
1632 AVDictionary **ddict = (AVDictionary **) field_dst;
1633 if (*sdict != *ddict)
1634 av_dict_free(ddict);
1636 av_dict_copy(ddict, *sdict, 0);
1637 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1638 ret = AVERROR(ENOMEM);
1640 memcpy(field_dst, field_src, opt_size(o->type));
1646 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1649 const AVClass *c = *(AVClass**)obj;
1650 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1652 if (c->version > (52 << 16 | 11 << 8))
1653 callback = c->query_ranges;
1656 callback = av_opt_query_ranges_default;
1658 ret = callback(ranges_arg, obj, key, flags);
1660 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1662 (*ranges_arg)->nb_components = ret;
1667 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1669 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1670 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1671 AVOptionRange *range = av_mallocz(sizeof(*range));
1672 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1677 if (!ranges || !range || !range_array || !field) {
1678 ret = AVERROR(ENOMEM);
1682 ranges->range = range_array;
1683 ranges->range[0] = range;
1684 ranges->nb_ranges = 1;
1685 ranges->nb_components = 1;
1686 range->is_range = 1;
1687 range->value_min = field->min;
1688 range->value_max = field->max;
1690 switch (field->type) {
1691 case AV_OPT_TYPE_BOOL:
1692 case AV_OPT_TYPE_INT:
1693 case AV_OPT_TYPE_INT64:
1694 case AV_OPT_TYPE_PIXEL_FMT:
1695 case AV_OPT_TYPE_SAMPLE_FMT:
1696 case AV_OPT_TYPE_FLOAT:
1697 case AV_OPT_TYPE_DOUBLE:
1698 case AV_OPT_TYPE_DURATION:
1699 case AV_OPT_TYPE_COLOR:
1700 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1702 case AV_OPT_TYPE_STRING:
1703 range->component_min = 0;
1704 range->component_max = 0x10FFFF; // max unicode value
1705 range->value_min = -1;
1706 range->value_max = INT_MAX;
1708 case AV_OPT_TYPE_RATIONAL:
1709 range->component_min = INT_MIN;
1710 range->component_max = INT_MAX;
1712 case AV_OPT_TYPE_IMAGE_SIZE:
1713 range->component_min = 0;
1714 range->component_max = INT_MAX/128/8;
1715 range->value_min = 0;
1716 range->value_max = INT_MAX/8;
1718 case AV_OPT_TYPE_VIDEO_RATE:
1719 range->component_min = 1;
1720 range->component_max = INT_MAX;
1721 range->value_min = 1;
1722 range->value_max = INT_MAX;
1725 ret = AVERROR(ENOSYS);
1729 *ranges_arg = ranges;
1734 av_free(range_array);
1738 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1741 AVOptionRanges *ranges = *rangesp;
1746 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1747 AVOptionRange *range = ranges->range[i];
1749 av_freep(&range->str);
1750 av_freep(&ranges->range[i]);
1753 av_freep(&ranges->range);
1757 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1768 return AVERROR(EINVAL);
1770 dst = ((uint8_t*)obj) + o->offset;
1773 case AV_OPT_TYPE_CONST:
1775 case AV_OPT_TYPE_BOOL:
1776 case AV_OPT_TYPE_FLAGS:
1777 case AV_OPT_TYPE_PIXEL_FMT:
1778 case AV_OPT_TYPE_SAMPLE_FMT:
1779 case AV_OPT_TYPE_INT:
1780 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1781 case AV_OPT_TYPE_DURATION:
1782 case AV_OPT_TYPE_INT64:
1783 read_number(o, dst, NULL, NULL, &i64);
1784 return o->default_val.i64 == i64;
1785 case AV_OPT_TYPE_STRING:
1786 str = *(char **)dst;
1787 if (str == o->default_val.str) //2 NULLs
1789 if (!str || !o->default_val.str) //1 NULL
1791 return !strcmp(str, o->default_val.str);
1792 case AV_OPT_TYPE_DOUBLE:
1793 read_number(o, dst, &d, NULL, NULL);
1794 return o->default_val.dbl == d;
1795 case AV_OPT_TYPE_FLOAT:
1796 read_number(o, dst, &d, NULL, NULL);
1797 f = o->default_val.dbl;
1800 case AV_OPT_TYPE_RATIONAL:
1801 q = av_d2q(o->default_val.dbl, INT_MAX);
1802 return !av_cmp_q(*(AVRational*)dst, q);
1803 case AV_OPT_TYPE_BINARY: {
1808 int opt_size = *(int *)((void **)dst + 1);
1809 void *opt_ptr = *(void **)dst;
1810 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1812 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1814 if (opt_size != strlen(o->default_val.str) / 2)
1816 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1818 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1822 case AV_OPT_TYPE_DICT:
1823 /* Binary and dict have not default support yet. Any pointer is not default. */
1824 return !!(*(void **)dst);
1825 case AV_OPT_TYPE_IMAGE_SIZE:
1826 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1828 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1830 return (w == *(int *)dst) && (h == *((int *)dst+1));
1831 case AV_OPT_TYPE_VIDEO_RATE:
1832 q = (AVRational){0, 0};
1833 if (o->default_val.str) {
1834 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1837 return !av_cmp_q(*(AVRational*)dst, q);
1838 case AV_OPT_TYPE_COLOR: {
1839 uint8_t color[4] = {0, 0, 0, 0};
1840 if (o->default_val.str) {
1841 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1844 return !memcmp(color, dst, sizeof(color));
1847 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1850 return AVERROR_PATCHWELCOME;
1853 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1858 return AVERROR(EINVAL);
1859 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1861 return AVERROR_OPTION_NOT_FOUND;
1862 return av_opt_is_set_to_default(target, o);
1865 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1866 const char key_val_sep, const char pairs_sep)
1868 const AVOption *o = NULL;
1872 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1874 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1875 pairs_sep == '\\' || key_val_sep == '\\') {
1876 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1877 return AVERROR(EINVAL);
1880 if (!obj || !buffer)
1881 return AVERROR(EINVAL);
1884 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
1886 while (o = av_opt_next(obj, o)) {
1887 if (o->type == AV_OPT_TYPE_CONST)
1889 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1891 else if (((o->flags & opt_flags) != opt_flags))
1893 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1895 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1896 av_bprint_finalize(&bprint, NULL);
1901 av_bprint_append_data(&bprint, &pairs_sep, 1);
1902 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1903 av_bprint_append_data(&bprint, &key_val_sep, 1);
1904 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1908 av_bprint_finalize(&bprint, buffer);
1914 typedef struct TestContext
1916 const AVClass *class;
1921 AVRational rational;
1922 AVRational video_rate;
1924 enum AVPixelFormat pix_fmt;
1925 enum AVSampleFormat sample_fmt;
1928 int64_t channel_layout;
1944 #define OFFSET(x) offsetof(TestContext, x)
1946 #define TEST_FLAG_COOL 01
1947 #define TEST_FLAG_LAME 02
1948 #define TEST_FLAG_MU 04
1950 static const AVOption test_options[]= {
1951 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, 1 },
1952 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, 1 },
1953 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, 10, 1 },
1954 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX, 1 },
1955 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, {.str = "\\=,"}, CHAR_MIN, CHAR_MAX, 1 },
1956 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 1}, 0, INT_MAX, 1, "flags" },
1957 {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 1, "flags" },
1958 {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 1, "flags" },
1959 {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 1, "flags" },
1960 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{.str="200x300"}, 0, 0, 1},
1961 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_0BGR}, -1, INT_MAX, 1},
1962 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_S16}, -1, INT_MAX, 1},
1963 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 , 1},
1964 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 1000}, 0, INT64_MAX, 1},
1965 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0, 1},
1966 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX, 1},
1967 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, {.str="62696e00"}, 0, 0, 1 },
1968 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, {.str=NULL}, 0, 0, 1 },
1969 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, {.str=""}, 0, 0, 1 },
1970 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100, 1 },
1971 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100, 1},
1972 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0/3}, 0, 100, 1 },
1973 {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, 1 },
1974 {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, 1 },
1975 {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 1 },
1979 static const char *test_get_name(void *ctx)
1984 static const AVClass test_class = {
1990 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
1992 vfprintf(stdout, fmt, vl);
1999 av_log_set_level(AV_LOG_DEBUG);
2000 av_log_set_callback(log_callback_help);
2002 printf("Testing default values\n");
2004 TestContext test_ctx = { 0 };
2005 test_ctx.class = &test_class;
2006 av_opt_set_defaults(&test_ctx);
2008 printf("num=%d\n", test_ctx.num);
2009 printf("toggle=%d\n", test_ctx.toggle);
2010 printf("string=%s\n", test_ctx.string);
2011 printf("escape=%s\n", test_ctx.escape);
2012 printf("flags=%d\n", test_ctx.flags);
2013 printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
2014 printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
2015 printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
2016 printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
2017 printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
2018 printf("duration=%"PRId64"\n", test_ctx.duration);
2019 printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
2020 printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
2021 if (test_ctx.binary)
2022 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]);
2023 printf("binary_size=%d\n", test_ctx.binary_size);
2024 printf("num64=%"PRId64"\n", test_ctx.num64);
2025 printf("flt=%.6f\n", test_ctx.flt);
2026 printf("dbl=%.6f\n", test_ctx.dbl);
2028 av_opt_show2(&test_ctx, NULL, -1, 0);
2030 av_opt_free(&test_ctx);
2033 printf("\nTesting av_opt_is_set_to_default()\n");
2036 TestContext test_ctx = { 0 };
2037 const AVOption *o = NULL;
2038 test_ctx.class = &test_class;
2040 av_log_set_level(AV_LOG_QUIET);
2042 while (o = av_opt_next(&test_ctx, o)) {
2043 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2044 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2046 av_opt_set_defaults(&test_ctx);
2047 while (o = av_opt_next(&test_ctx, o)) {
2048 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2049 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2051 av_opt_free(&test_ctx);
2054 printf("\nTest av_opt_serialize()\n");
2056 TestContext test_ctx = { 0 };
2058 test_ctx.class = &test_class;
2060 av_log_set_level(AV_LOG_QUIET);
2062 av_opt_set_defaults(&test_ctx);
2063 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2064 printf("%s\n", buf);
2065 av_opt_free(&test_ctx);
2066 memset(&test_ctx, 0, sizeof(test_ctx));
2067 test_ctx.class = &test_class;
2068 av_set_options_string(&test_ctx, buf, "=", ",");
2070 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2071 printf("%s\n", buf);
2075 av_opt_free(&test_ctx);
2078 printf("\nTesting av_set_options_string()\n");
2080 TestContext test_ctx = { 0 };
2081 static const char * const options[] = {
2097 "flags=+mu-lame : num=42: toggle=0",
2098 "num=42 : string=blahblah",
2099 "rational=0 : rational=1/2 : rational=1/-1",
2112 "video_rate=30000/1001",
2113 "video_rate=30/1.001",
2117 "duration=1\\:23\\:45.67",
2121 "cl=stereo+downmix",
2145 test_ctx.class = &test_class;
2146 av_opt_set_defaults(&test_ctx);
2148 av_log_set_level(AV_LOG_QUIET);
2150 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2151 int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
2152 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2154 av_log_set_callback(NULL);
2155 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
2156 printf("Error '%s'\n", options[i]);
2158 printf("OK '%s'\n", options[i]);
2159 av_log_set_callback(log_callback_help);
2161 av_opt_free(&test_ctx);
2164 printf("\nTesting av_opt_set_from_string()\n");
2166 TestContext test_ctx = { 0 };
2167 static const char * const options[] = {
2175 " 5 : hello : size = pal ",
2176 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
2178 static const char * const shorthand[] = { "num", "string", NULL };
2180 test_ctx.class = &test_class;
2181 av_opt_set_defaults(&test_ctx);
2183 av_log_set_level(AV_LOG_QUIET);
2185 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2186 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2187 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
2188 printf("Error '%s'\n", options[i]);
2190 printf("OK '%s'\n", options[i]);
2192 av_opt_free(&test_ctx);