3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Michael Niedermayer <michaelni@gmx.at>
30 #include "channel_layout.h"
36 #include "parseutils.h"
38 #include "mathematics.h"
39 #include "samplefmt.h"
44 #if FF_API_OLD_AVOPTIONS
45 const AVOption *av_next_option(FF_CONST_AVUTIL55 void *obj, const AVOption *last)
47 return av_opt_next(obj, last);
51 const AVOption *av_opt_next(FF_CONST_AVUTIL55 void *obj, const AVOption *last)
56 class = *(const AVClass**)obj;
57 if (!last && class && class->option && class->option[0].name)
59 if (last && last[1].name)
64 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
67 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
68 case AV_OPT_TYPE_PIXEL_FMT: *intnum = *(enum AVPixelFormat *)dst;return 0;
69 case AV_OPT_TYPE_SAMPLE_FMT:*intnum = *(enum AVSampleFormat*)dst;return 0;
70 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
71 case AV_OPT_TYPE_CHANNEL_LAYOUT:
72 case AV_OPT_TYPE_DURATION:
73 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
74 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
75 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
76 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
77 *den = ((AVRational*)dst)->den;
79 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
81 return AVERROR(EINVAL);
84 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
86 if (o->type != AV_OPT_TYPE_FLAGS &&
87 (o->max * den < num * intnum || o->min * den > num * intnum)) {
88 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
89 num*intnum/den, o->name, o->min, o->max);
90 return AVERROR(ERANGE);
92 if (o->type == AV_OPT_TYPE_FLAGS) {
93 double d = num*intnum/den;
94 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
95 av_log(obj, AV_LOG_ERROR,
96 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
97 num*intnum/den, o->name);
98 return AVERROR(ERANGE);
103 case AV_OPT_TYPE_PIXEL_FMT: *(enum AVPixelFormat *)dst = llrint(num/den) * intnum; break;
104 case AV_OPT_TYPE_SAMPLE_FMT:*(enum AVSampleFormat*)dst = llrint(num/den) * intnum; break;
105 case AV_OPT_TYPE_FLAGS:
106 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
107 case AV_OPT_TYPE_DURATION:
108 case AV_OPT_TYPE_CHANNEL_LAYOUT:
109 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
110 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
111 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
112 case AV_OPT_TYPE_RATIONAL:
113 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
114 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
117 return AVERROR(EINVAL);
122 static int hexchar2int(char c) {
123 if (c >= '0' && c <= '9') return c - '0';
124 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
125 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
129 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
131 int *lendst = (int *)(dst + 1);
138 if (!val || !(len = strlen(val)))
142 return AVERROR(EINVAL);
145 ptr = bin = av_malloc(len);
147 return AVERROR(ENOMEM);
149 int a = hexchar2int(*val++);
150 int b = hexchar2int(*val++);
151 if (a < 0 || b < 0) {
153 return AVERROR(EINVAL);
155 *ptr++ = (a << 4) | b;
163 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
166 *dst = av_strdup(val);
167 return *dst ? 0 : AVERROR(ENOMEM);
170 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
171 opt->type == AV_OPT_TYPE_CONST || \
172 opt->type == AV_OPT_TYPE_FLAGS || \
173 opt->type == AV_OPT_TYPE_INT) ? \
174 opt->default_val.i64 : opt->default_val.dbl)
176 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
182 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
183 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
195 if (o->type == AV_OPT_TYPE_FLAGS) {
196 if (*val == '+' || *val == '-')
198 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
204 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
207 double const_values[64];
208 const char * const_names[64];
209 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
210 d = DEFAULT_NUMVAL(o_named);
213 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
214 if (o_named->type == AV_OPT_TYPE_CONST &&
216 !strcmp(o_named->unit, o->unit)) {
217 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
218 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
219 return AVERROR_PATCHWELCOME;
221 const_names [ci ] = o_named->name;
222 const_values[ci++] = DEFAULT_NUMVAL(o_named);
226 const_names [ci ] = "default";
227 const_values[ci++] = DEFAULT_NUMVAL(o);
228 const_names [ci ] = "max";
229 const_values[ci++] = o->max;
230 const_names [ci ] = "min";
231 const_values[ci++] = o->min;
232 const_names [ci ] = "none";
233 const_values[ci++] = 0;
234 const_names [ci ] = "all";
235 const_values[ci++] = ~0;
236 const_names [ci] = NULL;
237 const_values[ci] = 0;
239 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
240 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
242 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
247 if (o->type == AV_OPT_TYPE_FLAGS) {
248 read_number(o, dst, NULL, NULL, &intnum);
249 if (cmd == '+') d = intnum | (int64_t)d;
250 else if (cmd == '-') d = intnum &~(int64_t)d;
253 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
263 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
267 if (!val || !strcmp(val, "none")) {
272 ret = av_parse_video_size(dst, dst + 1, val);
274 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
278 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
282 ret = AVERROR(EINVAL);
284 ret = av_parse_video_rate(dst, val);
287 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
291 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
298 ret = av_parse_color(dst, val, -1, obj);
300 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
306 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
307 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
311 if (!val || !strcmp(val, "none")) {
317 fmt = strtol(val, &tail, 0);
318 if (*tail || (unsigned)fmt >= fmt_nb) {
319 av_log(obj, AV_LOG_ERROR,
320 "Unable to parse option value \"%s\" as %s\n", val, desc);
321 return AVERROR(EINVAL);
326 min = FFMAX(o->min, -1);
327 max = FFMIN(o->max, fmt_nb-1);
329 // hack for compatibility with old ffmpeg
330 if(min == 0 && max == 0) {
335 if (fmt < min || fmt > max) {
336 av_log(obj, AV_LOG_ERROR,
337 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
338 fmt, o->name, desc, min, max);
339 return AVERROR(ERANGE);
346 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
348 return set_string_fmt(obj, o, val, dst,
349 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
352 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
354 return set_string_fmt(obj, o, val, dst,
355 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
358 #if FF_API_OLD_AVOPTIONS
359 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
361 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
364 return av_opt_set(obj, name, val, 0);
368 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
371 void *dst, *target_obj;
372 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
373 if (!o || !target_obj)
374 return AVERROR_OPTION_NOT_FOUND;
375 if (!val && (o->type != AV_OPT_TYPE_STRING &&
376 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
377 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
378 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
379 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT))
380 return AVERROR(EINVAL);
382 if (o->flags & AV_OPT_FLAG_READONLY)
383 return AVERROR(EINVAL);
385 dst = ((uint8_t*)target_obj) + o->offset;
387 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
388 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
389 case AV_OPT_TYPE_FLAGS:
390 case AV_OPT_TYPE_INT:
391 case AV_OPT_TYPE_INT64:
392 case AV_OPT_TYPE_FLOAT:
393 case AV_OPT_TYPE_DOUBLE:
394 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
395 case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
396 case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
397 case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
398 case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
399 case AV_OPT_TYPE_DURATION:
404 if ((ret = av_parse_time(dst, val, 1)) < 0)
405 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
409 case AV_OPT_TYPE_COLOR: return set_string_color(obj, o, val, dst);
410 case AV_OPT_TYPE_CHANNEL_LAYOUT:
411 if (!val || !strcmp(val, "none")) {
414 #if FF_API_GET_CHANNEL_LAYOUT_COMPAT
415 int64_t cl = ff_get_channel_layout(val, 0);
417 int64_t cl = av_get_channel_layout(val);
420 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
421 ret = AVERROR(EINVAL);
423 *(int64_t *)dst = cl;
429 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
430 return AVERROR(EINVAL);
433 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
434 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
436 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
437 return AVERROR(EINVAL);\
438 return set_string_number(obj, obj, o, val, name ## _out);\
441 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
442 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
443 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
444 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
445 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
446 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
448 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
451 void *dst, *target_obj;
452 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
454 if (!o || !target_obj)
455 return AVERROR_OPTION_NOT_FOUND;
457 if (o->flags & AV_OPT_FLAG_READONLY)
458 return AVERROR(EINVAL);
460 dst = ((uint8_t*)target_obj) + o->offset;
461 return write_number(obj, o, dst, num, den, intnum);
464 #if FF_API_OLD_AVOPTIONS
465 const AVOption *av_set_double(void *obj, const char *name, double n)
467 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
468 if (set_number(obj, name, n, 1, 1, 0) < 0)
473 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
475 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
476 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
481 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
483 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
484 if (set_number(obj, name, 1, 1, n, 0) < 0)
490 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
492 return set_number(obj, name, 1, 1, val, search_flags);
495 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
497 return set_number(obj, name, val, 1, 1, search_flags);
500 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
502 return set_number(obj, name, val.num, val.den, 1, search_flags);
505 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
508 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
513 if (!o || !target_obj)
514 return AVERROR_OPTION_NOT_FOUND;
516 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
517 return AVERROR(EINVAL);
519 ptr = len ? av_malloc(len) : NULL;
521 return AVERROR(ENOMEM);
523 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
524 lendst = (int *)(dst + 1);
530 memcpy(ptr, val, len);
535 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
538 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
540 if (!o || !target_obj)
541 return AVERROR_OPTION_NOT_FOUND;
542 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
543 av_log(obj, AV_LOG_ERROR,
544 "The value set by option '%s' is not an image size.\n", o->name);
545 return AVERROR(EINVAL);
548 av_log(obj, AV_LOG_ERROR,
549 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
550 return AVERROR(EINVAL);
552 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
553 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
557 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
560 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
562 if (!o || !target_obj)
563 return AVERROR_OPTION_NOT_FOUND;
564 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
565 av_log(obj, AV_LOG_ERROR,
566 "The value set by option '%s' is not a video rate.\n", o->name);
567 return AVERROR(EINVAL);
569 if (val.num <= 0 || val.den <= 0)
570 return AVERROR(EINVAL);
571 return set_number(obj, name, val.num, val.den, 1, search_flags);
574 static int set_format(void *obj, const char *name, int fmt, int search_flags,
575 enum AVOptionType type, const char *desc, int nb_fmts)
578 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
579 search_flags, &target_obj);
582 if (!o || !target_obj)
583 return AVERROR_OPTION_NOT_FOUND;
584 if (o->type != type) {
585 av_log(obj, AV_LOG_ERROR,
586 "The value set by option '%s' is not a %s format", name, desc);
587 return AVERROR(EINVAL);
590 min = FFMAX(o->min, -1);
591 max = FFMIN(o->max, nb_fmts-1);
593 if (fmt < min || fmt > max) {
594 av_log(obj, AV_LOG_ERROR,
595 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
596 fmt, name, desc, min, max);
597 return AVERROR(ERANGE);
599 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
603 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
605 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
608 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
610 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
613 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
616 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
618 if (!o || !target_obj)
619 return AVERROR_OPTION_NOT_FOUND;
620 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
621 av_log(obj, AV_LOG_ERROR,
622 "The value set by option '%s' is not a channel layout.\n", o->name);
623 return AVERROR(EINVAL);
625 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
629 #if FF_API_OLD_AVOPTIONS
632 * @param buf a buffer which is used for returning non string values as strings, can be NULL
633 * @param buf_len allocated length in bytes of buf
635 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
637 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
643 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
646 dst= ((uint8_t*)obj) + o->offset;
647 if (o_out) *o_out= o;
650 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
651 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
652 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
653 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
654 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
655 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
656 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
657 case AV_OPT_TYPE_STRING: return *(void**)dst;
658 case AV_OPT_TYPE_BINARY:
659 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
660 if (len >= (buf_len + 1)/2) return NULL;
661 bin = *(uint8_t**)dst;
662 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
664 default: return NULL;
670 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
674 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
676 if (!o || !target_obj)
677 return AVERROR_OPTION_NOT_FOUND;
678 if (o->flags & AV_OPT_FLAG_READONLY)
679 return AVERROR(EINVAL);
681 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
683 av_dict_copy(dst, val, 0);
688 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
690 void *dst, *target_obj;
691 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
692 uint8_t *bin, buf[128];
696 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
697 return AVERROR_OPTION_NOT_FOUND;
699 dst = (uint8_t*)target_obj + o->offset;
703 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
704 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
705 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
706 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
707 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
708 case AV_OPT_TYPE_VIDEO_RATE:
709 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
710 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
711 case AV_OPT_TYPE_STRING:
713 *out_val = av_strdup(*(uint8_t**)dst);
715 *out_val = av_strdup("");
716 return *out_val ? 0 : AVERROR(ENOMEM);
717 case AV_OPT_TYPE_BINARY:
718 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
719 if ((uint64_t)len*2 + 1 > INT_MAX)
720 return AVERROR(EINVAL);
721 if (!(*out_val = av_malloc(len*2 + 1)))
722 return AVERROR(ENOMEM);
727 bin = *(uint8_t**)dst;
728 for (i = 0; i < len; i++)
729 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
731 case AV_OPT_TYPE_IMAGE_SIZE:
732 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
734 case AV_OPT_TYPE_PIXEL_FMT:
735 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
737 case AV_OPT_TYPE_SAMPLE_FMT:
738 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
740 case AV_OPT_TYPE_DURATION:
741 i64 = *(int64_t *)dst;
742 ret = snprintf(buf, sizeof(buf), "%"PRIi64":%02d:%02d.%06d",
743 i64 / 3600000000, (int)((i64 / 60000000) % 60),
744 (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
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 #if FF_API_OLD_AVOPTIONS
785 double av_get_double(void *obj, const char *name, const AVOption **o_out)
791 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
793 return num*intnum/den;
796 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
802 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
803 return (AVRational){0, 0};
804 if (num == 1.0 && (int)intnum == intnum)
805 return (AVRational){intnum, den};
807 return av_d2q(num*intnum/den, 1<<24);
810 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
816 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
818 return num*intnum/den;
822 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
828 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
830 *out_val = num*intnum/den;
834 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
840 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
842 *out_val = num*intnum/den;
846 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
852 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
855 if (num == 1.0 && (int)intnum == intnum)
856 *out_val = (AVRational){intnum, den};
858 *out_val = av_d2q(num*intnum/den, 1<<24);
862 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
864 void *dst, *target_obj;
865 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
866 if (!o || !target_obj)
867 return AVERROR_OPTION_NOT_FOUND;
868 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
869 av_log(obj, AV_LOG_ERROR,
870 "The value for option '%s' is not an image size.\n", name);
871 return AVERROR(EINVAL);
874 dst = ((uint8_t*)target_obj) + o->offset;
875 if (w_out) *w_out = *(int *)dst;
876 if (h_out) *h_out = *((int *)dst+1);
880 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
886 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
889 if (num == 1.0 && (int)intnum == intnum)
890 *out_val = (AVRational){intnum, den};
892 *out_val = av_d2q(num*intnum/den, 1<<24);
896 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
897 enum AVOptionType type, const char *desc)
899 void *dst, *target_obj;
900 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
901 if (!o || !target_obj)
902 return AVERROR_OPTION_NOT_FOUND;
903 if (o->type != type) {
904 av_log(obj, AV_LOG_ERROR,
905 "The value for option '%s' is not a %s format.\n", desc, name);
906 return AVERROR(EINVAL);
909 dst = ((uint8_t*)target_obj) + o->offset;
910 *out_fmt = *(int *)dst;
914 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
916 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
919 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
921 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
924 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
926 void *dst, *target_obj;
927 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
928 if (!o || !target_obj)
929 return AVERROR_OPTION_NOT_FOUND;
930 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
931 av_log(obj, AV_LOG_ERROR,
932 "The value for option '%s' is not a channel layout.\n", name);
933 return AVERROR(EINVAL);
936 dst = ((uint8_t*)target_obj) + o->offset;
937 *cl = *(int64_t *)dst;
941 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
945 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
947 if (!o || !target_obj)
948 return AVERROR_OPTION_NOT_FOUND;
949 if (o->type != AV_OPT_TYPE_DICT)
950 return AVERROR(EINVAL);
952 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
953 av_dict_copy(out_val, src, 0);
958 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
960 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
961 const AVOption *flag = av_opt_find(obj, flag_name,
962 field ? field->unit : NULL, 0, 0);
965 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
966 av_opt_get_int(obj, field_name, 0, &res) < 0)
968 return res & flag->default_val.i64;
971 static void log_value(void *av_log_obj, int level, double d)
974 av_log(av_log_obj, level, "INT_MAX");
975 } else if (d == INT_MIN) {
976 av_log(av_log_obj, level, "INT_MIN");
977 } else if (d == UINT32_MAX) {
978 av_log(av_log_obj, level, "UINT32_MAX");
979 } else if (d == (double)INT64_MAX) {
980 av_log(av_log_obj, level, "I64_MAX");
981 } else if (d == INT64_MIN) {
982 av_log(av_log_obj, level, "I64_MIN");
983 } else if (d == FLT_MAX) {
984 av_log(av_log_obj, level, "FLT_MAX");
985 } else if (d == FLT_MIN) {
986 av_log(av_log_obj, level, "FLT_MIN");
987 } else if (d == -FLT_MAX) {
988 av_log(av_log_obj, level, "-FLT_MAX");
989 } else if (d == -FLT_MIN) {
990 av_log(av_log_obj, level, "-FLT_MIN");
991 } else if (d == DBL_MAX) {
992 av_log(av_log_obj, level, "DBL_MAX");
993 } else if (d == DBL_MIN) {
994 av_log(av_log_obj, level, "DBL_MIN");
995 } else if (d == -DBL_MAX) {
996 av_log(av_log_obj, level, "-DBL_MAX");
997 } else if (d == -DBL_MIN) {
998 av_log(av_log_obj, level, "-DBL_MIN");
1000 av_log(av_log_obj, level, "%g", d);
1004 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1005 int req_flags, int rej_flags)
1007 const AVOption *opt=NULL;
1011 while ((opt = av_opt_next(obj, opt))) {
1012 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1015 /* Don't print CONST's on level one.
1016 * Don't print anything but CONST's on level two.
1017 * Only print items from the requested unit.
1019 if (!unit && opt->type==AV_OPT_TYPE_CONST)
1021 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
1023 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1025 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1026 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1028 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1029 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1032 switch (opt->type) {
1033 case AV_OPT_TYPE_FLAGS:
1034 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1036 case AV_OPT_TYPE_INT:
1037 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1039 case AV_OPT_TYPE_INT64:
1040 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1042 case AV_OPT_TYPE_DOUBLE:
1043 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1045 case AV_OPT_TYPE_FLOAT:
1046 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1048 case AV_OPT_TYPE_STRING:
1049 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1051 case AV_OPT_TYPE_RATIONAL:
1052 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1054 case AV_OPT_TYPE_BINARY:
1055 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1057 case AV_OPT_TYPE_IMAGE_SIZE:
1058 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1060 case AV_OPT_TYPE_VIDEO_RATE:
1061 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1063 case AV_OPT_TYPE_PIXEL_FMT:
1064 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1066 case AV_OPT_TYPE_SAMPLE_FMT:
1067 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1069 case AV_OPT_TYPE_DURATION:
1070 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1072 case AV_OPT_TYPE_COLOR:
1073 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1075 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1076 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1078 case AV_OPT_TYPE_CONST:
1080 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1083 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1084 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1085 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1086 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1087 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1088 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1089 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1090 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1093 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1095 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1096 switch (opt->type) {
1097 case AV_OPT_TYPE_INT:
1098 case AV_OPT_TYPE_INT64:
1099 case AV_OPT_TYPE_DOUBLE:
1100 case AV_OPT_TYPE_FLOAT:
1101 case AV_OPT_TYPE_RATIONAL:
1102 for (i = 0; i < r->nb_ranges; i++) {
1103 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1104 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1105 av_log(av_log_obj, AV_LOG_INFO, " to ");
1106 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1107 av_log(av_log_obj, AV_LOG_INFO, ")");
1111 av_opt_freep_ranges(&r);
1114 if (opt->type != AV_OPT_TYPE_CONST &&
1115 opt->type != AV_OPT_TYPE_BINARY &&
1116 !((opt->type == AV_OPT_TYPE_COLOR ||
1117 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1118 opt->type == AV_OPT_TYPE_STRING ||
1119 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1120 !opt->default_val.str)) {
1121 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1122 switch (opt->type) {
1123 case AV_OPT_TYPE_FLAGS:
1124 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1126 case AV_OPT_TYPE_DURATION:
1127 case AV_OPT_TYPE_INT:
1128 case AV_OPT_TYPE_INT64:
1129 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1131 case AV_OPT_TYPE_DOUBLE:
1132 case AV_OPT_TYPE_FLOAT:
1133 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1135 case AV_OPT_TYPE_RATIONAL: {
1136 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1137 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1139 case AV_OPT_TYPE_PIXEL_FMT:
1140 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1142 case AV_OPT_TYPE_SAMPLE_FMT:
1143 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1145 case AV_OPT_TYPE_COLOR:
1146 case AV_OPT_TYPE_IMAGE_SIZE:
1147 case AV_OPT_TYPE_STRING:
1148 case AV_OPT_TYPE_VIDEO_RATE:
1149 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1151 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1152 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1155 av_log(av_log_obj, AV_LOG_INFO, ")");
1158 av_log(av_log_obj, AV_LOG_INFO, "\n");
1159 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1160 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1165 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1170 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1172 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1177 void av_opt_set_defaults(void *s)
1179 #if FF_API_OLD_AVOPTIONS
1180 av_opt_set_defaults2(s, 0, 0);
1183 void av_opt_set_defaults2(void *s, int mask, int flags)
1186 const AVOption *opt = NULL;
1187 while ((opt = av_opt_next(s, opt))) {
1188 void *dst = ((uint8_t*)s) + opt->offset;
1189 #if FF_API_OLD_AVOPTIONS
1190 if ((opt->flags & mask) != flags)
1194 if (opt->flags & AV_OPT_FLAG_READONLY)
1197 switch (opt->type) {
1198 case AV_OPT_TYPE_CONST:
1199 /* Nothing to be done here */
1201 case AV_OPT_TYPE_FLAGS:
1202 case AV_OPT_TYPE_INT:
1203 case AV_OPT_TYPE_INT64:
1204 case AV_OPT_TYPE_DURATION:
1205 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1206 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1208 case AV_OPT_TYPE_DOUBLE:
1209 case AV_OPT_TYPE_FLOAT: {
1211 val = opt->default_val.dbl;
1212 write_number(s, opt, dst, val, 1, 1);
1215 case AV_OPT_TYPE_RATIONAL: {
1217 val = av_d2q(opt->default_val.dbl, INT_MAX);
1218 write_number(s, opt, dst, 1, val.den, val.num);
1221 case AV_OPT_TYPE_COLOR:
1222 set_string_color(s, opt, opt->default_val.str, dst);
1224 case AV_OPT_TYPE_STRING:
1225 set_string(s, opt, opt->default_val.str, dst);
1227 case AV_OPT_TYPE_IMAGE_SIZE:
1228 set_string_image_size(s, opt, opt->default_val.str, dst);
1230 case AV_OPT_TYPE_VIDEO_RATE:
1231 set_string_video_rate(s, opt, opt->default_val.str, dst);
1233 case AV_OPT_TYPE_PIXEL_FMT:
1234 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1236 case AV_OPT_TYPE_SAMPLE_FMT:
1237 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1239 case AV_OPT_TYPE_BINARY:
1240 set_string_binary(s, opt, opt->default_val.str, dst);
1242 case AV_OPT_TYPE_DICT:
1243 /* Cannot set defaults for these types */
1246 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1252 * Store the value in the field in ctx that is named like key.
1253 * ctx must be an AVClass context, storing is done using AVOptions.
1255 * @param buf the string to parse, buf will be updated to point at the
1256 * separator just after the parsed key/value pair
1257 * @param key_val_sep a 0-terminated list of characters used to
1258 * separate key from value
1259 * @param pairs_sep a 0-terminated list of characters used to separate
1260 * two pairs from each other
1261 * @return 0 if the key/value pair has been successfully parsed and
1262 * set, or a negative value corresponding to an AVERROR code in case
1264 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1265 * the error code issued by av_opt_set() if the key/value pair
1268 static int parse_key_value_pair(void *ctx, const char **buf,
1269 const char *key_val_sep, const char *pairs_sep)
1271 char *key = av_get_token(buf, key_val_sep);
1276 return AVERROR(ENOMEM);
1278 if (*key && strspn(*buf, key_val_sep)) {
1280 val = av_get_token(buf, pairs_sep);
1283 return AVERROR(ENOMEM);
1286 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1288 return AVERROR(EINVAL);
1291 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1293 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1294 if (ret == AVERROR_OPTION_NOT_FOUND)
1295 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1302 int av_set_options_string(void *ctx, const char *opts,
1303 const char *key_val_sep, const char *pairs_sep)
1311 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1322 #define WHITESPACES " \n\t"
1324 static int is_key_char(char c)
1326 return (unsigned)((c | 32) - 'a') < 26 ||
1327 (unsigned)(c - '0') < 10 ||
1328 c == '-' || c == '_' || c == '/' || c == '.';
1332 * Read a key from a string.
1334 * The key consists of is_key_char characters and must be terminated by a
1335 * character from the delim string; spaces are ignored.
1337 * @return 0 for success (even with ellipsis), <0 for failure
1339 static int get_key(const char **ropts, const char *delim, char **rkey)
1341 const char *opts = *ropts;
1342 const char *key_start, *key_end;
1344 key_start = opts += strspn(opts, WHITESPACES);
1345 while (is_key_char(*opts))
1348 opts += strspn(opts, WHITESPACES);
1349 if (!*opts || !strchr(delim, *opts))
1350 return AVERROR(EINVAL);
1352 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1353 return AVERROR(ENOMEM);
1354 memcpy(*rkey, key_start, key_end - key_start);
1355 (*rkey)[key_end - key_start] = 0;
1360 int av_opt_get_key_value(const char **ropts,
1361 const char *key_val_sep, const char *pairs_sep,
1363 char **rkey, char **rval)
1366 char *key = NULL, *val;
1367 const char *opts = *ropts;
1369 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1370 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1371 return AVERROR(EINVAL);
1372 if (!(val = av_get_token(&opts, pairs_sep))) {
1374 return AVERROR(ENOMEM);
1382 int av_opt_set_from_string(void *ctx, const char *opts,
1383 const char *const *shorthand,
1384 const char *key_val_sep, const char *pairs_sep)
1387 const char *dummy_shorthand = NULL;
1388 char *av_uninit(parsed_key), *av_uninit(value);
1394 shorthand = &dummy_shorthand;
1397 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1398 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1399 &parsed_key, &value);
1401 if (ret == AVERROR(EINVAL))
1402 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1404 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1412 while (*shorthand) /* discard all remaining shorthand */
1415 key = *(shorthand++);
1418 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1419 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1420 if (ret == AVERROR_OPTION_NOT_FOUND)
1421 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1423 av_free(parsed_key);
1428 av_free(parsed_key);
1434 void av_opt_free(void *obj)
1436 const AVOption *o = NULL;
1437 while ((o = av_opt_next(obj, o))) {
1439 case AV_OPT_TYPE_STRING:
1440 case AV_OPT_TYPE_BINARY:
1441 av_freep((uint8_t *)obj + o->offset);
1444 case AV_OPT_TYPE_DICT:
1445 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1454 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1456 AVDictionaryEntry *t = NULL;
1457 AVDictionary *tmp = NULL;
1463 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1464 ret = av_opt_set(obj, t->key, t->value, search_flags);
1465 if (ret == AVERROR_OPTION_NOT_FOUND)
1466 av_dict_set(&tmp, t->key, t->value, 0);
1468 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1473 av_dict_free(options);
1478 int av_opt_set_dict(void *obj, AVDictionary **options)
1480 return av_opt_set_dict2(obj, options, 0);
1483 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1484 int opt_flags, int search_flags)
1486 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1489 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1490 int opt_flags, int search_flags, void **target_obj)
1493 const AVOption *o = NULL;
1503 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1504 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1505 const AVClass *child = NULL;
1506 while (child = av_opt_child_class_next(c, child))
1507 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1511 while (child = av_opt_child_next(obj, child))
1512 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1517 while (o = av_opt_next(obj, o)) {
1518 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1519 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1520 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1522 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1533 void *av_opt_child_next(void *obj, void *prev)
1535 const AVClass *c = *(AVClass**)obj;
1537 return c->child_next(obj, prev);
1541 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1543 if (parent->child_class_next)
1544 return parent->child_class_next(prev);
1548 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1550 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1553 return (uint8_t*)obj + opt->offset;
1556 static int opt_size(enum AVOptionType type)
1559 case AV_OPT_TYPE_INT:
1560 case AV_OPT_TYPE_FLAGS: return sizeof(int);
1561 case AV_OPT_TYPE_DURATION:
1562 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1563 case AV_OPT_TYPE_INT64: return sizeof(int64_t);
1564 case AV_OPT_TYPE_DOUBLE: return sizeof(double);
1565 case AV_OPT_TYPE_FLOAT: return sizeof(float);
1566 case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
1567 case AV_OPT_TYPE_VIDEO_RATE:
1568 case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
1569 case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
1570 case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
1571 case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
1572 case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
1573 case AV_OPT_TYPE_COLOR: return 4;
1578 int av_opt_copy(void *dst, FF_CONST_AVUTIL55 void *src)
1580 const AVOption *o = NULL;
1587 c = *(AVClass**)src;
1588 if (*(AVClass**)dst && c != *(AVClass**)dst)
1589 return AVERROR(EINVAL);
1591 while ((o = av_opt_next(src, o))) {
1592 void *field_dst = ((uint8_t*)dst) + o->offset;
1593 void *field_src = ((uint8_t*)src) + o->offset;
1594 uint8_t **field_dst8 = (uint8_t**)field_dst;
1595 uint8_t **field_src8 = (uint8_t**)field_src;
1597 if (o->type == AV_OPT_TYPE_STRING) {
1598 if (*field_dst8 != *field_src8)
1599 av_freep(field_dst8);
1600 *field_dst8 = av_strdup(*field_src8);
1601 if (*field_src8 && !*field_dst8)
1602 ret = AVERROR(ENOMEM);
1603 } else if (o->type == AV_OPT_TYPE_BINARY) {
1604 int len = *(int*)(field_src8 + 1);
1605 if (*field_dst8 != *field_src8)
1606 av_freep(field_dst8);
1607 *field_dst8 = av_memdup(*field_src8, len);
1608 if (len && !*field_dst8) {
1609 ret = AVERROR(ENOMEM);
1612 *(int*)(field_dst8 + 1) = len;
1613 } else if (o->type == AV_OPT_TYPE_CONST) {
1615 } else if (o->type == AV_OPT_TYPE_DICT) {
1616 AVDictionary **sdict = (AVDictionary **) field_src;
1617 AVDictionary **ddict = (AVDictionary **) field_dst;
1618 if (*sdict != *ddict)
1619 av_dict_free(ddict);
1621 av_dict_copy(ddict, *sdict, 0);
1622 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1623 ret = AVERROR(ENOMEM);
1625 memcpy(field_dst, field_src, opt_size(o->type));
1631 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1634 const AVClass *c = *(AVClass**)obj;
1635 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1637 if (c->version > (52 << 16 | 11 << 8))
1638 callback = c->query_ranges;
1641 callback = av_opt_query_ranges_default;
1643 ret = callback(ranges_arg, obj, key, flags);
1645 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1647 (*ranges_arg)->nb_components = ret;
1652 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1654 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1655 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1656 AVOptionRange *range = av_mallocz(sizeof(*range));
1657 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1662 if (!ranges || !range || !range_array || !field) {
1663 ret = AVERROR(ENOMEM);
1667 ranges->range = range_array;
1668 ranges->range[0] = range;
1669 ranges->nb_ranges = 1;
1670 ranges->nb_components = 1;
1671 range->is_range = 1;
1672 range->value_min = field->min;
1673 range->value_max = field->max;
1675 switch (field->type) {
1676 case AV_OPT_TYPE_INT:
1677 case AV_OPT_TYPE_INT64:
1678 case AV_OPT_TYPE_PIXEL_FMT:
1679 case AV_OPT_TYPE_SAMPLE_FMT:
1680 case AV_OPT_TYPE_FLOAT:
1681 case AV_OPT_TYPE_DOUBLE:
1682 case AV_OPT_TYPE_DURATION:
1683 case AV_OPT_TYPE_COLOR:
1684 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1686 case AV_OPT_TYPE_STRING:
1687 range->component_min = 0;
1688 range->component_max = 0x10FFFF; // max unicode value
1689 range->value_min = -1;
1690 range->value_max = INT_MAX;
1692 case AV_OPT_TYPE_RATIONAL:
1693 range->component_min = INT_MIN;
1694 range->component_max = INT_MAX;
1696 case AV_OPT_TYPE_IMAGE_SIZE:
1697 range->component_min = 0;
1698 range->component_max = INT_MAX/128/8;
1699 range->value_min = 0;
1700 range->value_max = INT_MAX/8;
1702 case AV_OPT_TYPE_VIDEO_RATE:
1703 range->component_min = 1;
1704 range->component_max = INT_MAX;
1705 range->value_min = 1;
1706 range->value_max = INT_MAX;
1709 ret = AVERROR(ENOSYS);
1713 *ranges_arg = ranges;
1718 av_free(range_array);
1722 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1725 AVOptionRanges *ranges = *rangesp;
1730 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1731 AVOptionRange *range = ranges->range[i];
1733 av_freep(&range->str);
1734 av_freep(&ranges->range[i]);
1737 av_freep(&ranges->range);
1741 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1752 return AVERROR(EINVAL);
1754 dst = ((uint8_t*)obj) + o->offset;
1757 case AV_OPT_TYPE_CONST:
1759 case AV_OPT_TYPE_FLAGS:
1760 case AV_OPT_TYPE_PIXEL_FMT:
1761 case AV_OPT_TYPE_SAMPLE_FMT:
1762 case AV_OPT_TYPE_INT:
1763 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1764 case AV_OPT_TYPE_DURATION:
1765 case AV_OPT_TYPE_INT64:
1766 read_number(o, dst, NULL, NULL, &i64);
1767 return o->default_val.i64 == i64;
1768 case AV_OPT_TYPE_STRING:
1769 str = *(char **)dst;
1770 if (str == o->default_val.str) //2 NULLs
1772 if (!str || !o->default_val.str) //1 NULL
1774 return !strcmp(str, o->default_val.str);
1775 case AV_OPT_TYPE_DOUBLE:
1776 read_number(o, dst, &d, NULL, NULL);
1777 return o->default_val.dbl == d;
1778 case AV_OPT_TYPE_FLOAT:
1779 read_number(o, dst, &d, NULL, NULL);
1780 f = o->default_val.dbl;
1783 case AV_OPT_TYPE_RATIONAL:
1784 q = av_d2q(o->default_val.dbl, INT_MAX);
1785 return !av_cmp_q(*(AVRational*)dst, q);
1786 case AV_OPT_TYPE_BINARY: {
1791 int opt_size = *(int *)((void **)dst + 1);
1792 void *opt_ptr = *(void **)dst;
1793 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1795 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1797 if (opt_size != strlen(o->default_val.str) / 2)
1799 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1801 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1805 case AV_OPT_TYPE_DICT:
1806 /* Binary and dict have not default support yet. Any pointer is not default. */
1807 return !!(*(void **)dst);
1808 case AV_OPT_TYPE_IMAGE_SIZE:
1809 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1811 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1813 return (w == *(int *)dst) && (h == *((int *)dst+1));
1814 case AV_OPT_TYPE_VIDEO_RATE:
1815 q = (AVRational){0, 0};
1816 if (o->default_val.str) {
1817 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1820 return !av_cmp_q(*(AVRational*)dst, q);
1821 case AV_OPT_TYPE_COLOR: {
1822 uint8_t color[4] = {0, 0, 0, 0};
1823 if (o->default_val.str) {
1824 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1827 return !memcmp(color, dst, sizeof(color));
1830 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1833 return AVERROR_PATCHWELCOME;
1836 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1841 return AVERROR(EINVAL);
1842 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1844 return AVERROR_OPTION_NOT_FOUND;
1845 return av_opt_is_set_to_default(target, o);
1848 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1849 const char key_val_sep, const char pairs_sep)
1851 const AVOption *o = NULL;
1855 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1857 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1858 pairs_sep == '\\' || key_val_sep == '\\') {
1859 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1860 return AVERROR(EINVAL);
1863 if (!obj || !buffer)
1864 return AVERROR(EINVAL);
1867 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
1869 while (o = av_opt_next(obj, o)) {
1870 if (o->type == AV_OPT_TYPE_CONST)
1872 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1874 else if (((o->flags & opt_flags) != opt_flags))
1876 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1878 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1879 av_bprint_finalize(&bprint, NULL);
1884 av_bprint_append_data(&bprint, &pairs_sep, 1);
1885 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1886 av_bprint_append_data(&bprint, &key_val_sep, 1);
1887 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1891 av_bprint_finalize(&bprint, buffer);
1897 typedef struct TestContext
1899 const AVClass *class;
1904 AVRational rational;
1905 AVRational video_rate;
1907 enum AVPixelFormat pix_fmt;
1908 enum AVSampleFormat sample_fmt;
1911 int64_t channel_layout;
1924 #define OFFSET(x) offsetof(TestContext, x)
1926 #define TEST_FLAG_COOL 01
1927 #define TEST_FLAG_LAME 02
1928 #define TEST_FLAG_MU 04
1930 static const AVOption test_options[]= {
1931 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1932 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1 },
1933 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, 10 },
1934 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
1935 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, {.str = "\\=,"}, CHAR_MIN, CHAR_MAX },
1936 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 1}, 0, INT_MAX, 0, "flags" },
1937 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1938 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1939 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1940 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{.str="200x300"}, 0, 0 },
1941 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_0BGR}, -1, INT_MAX},
1942 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_S16}, -1, INT_MAX},
1943 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
1944 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 1000}, 0, INT64_MAX},
1945 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1946 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
1947 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, {.str="62696e00"}, 0, 0 },
1948 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, {.str=NULL}, 0, 0 },
1949 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, {.str=""}, 0, 0 },
1950 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100 },
1951 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100 },
1952 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0/3}, 0, 100 },
1956 static const char *test_get_name(void *ctx)
1961 static const AVClass test_class = {
1971 printf("Testing default values\n");
1973 TestContext test_ctx = { 0 };
1974 test_ctx.class = &test_class;
1975 av_opt_set_defaults(&test_ctx);
1977 printf("num=%d\n", test_ctx.num);
1978 printf("toggle=%d\n", test_ctx.toggle);
1979 printf("string=%s\n", test_ctx.string);
1980 printf("escape=%s\n", test_ctx.escape);
1981 printf("flags=%d\n", test_ctx.flags);
1982 printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
1983 printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
1984 printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
1985 printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
1986 printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
1987 printf("duration=%"PRId64"\n", test_ctx.duration);
1988 printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
1989 printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
1990 if (test_ctx.binary)
1991 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]);
1992 printf("binary_size=%d\n", test_ctx.binary_size);
1993 printf("num64=%"PRId64"\n", test_ctx.num64);
1994 printf("flt=%.6f\n", test_ctx.flt);
1995 printf("dbl=%.6f\n", test_ctx.dbl);
1996 av_opt_free(&test_ctx);
1999 printf("\nTesting av_opt_is_set_to_default()\n");
2002 TestContext test_ctx = { 0 };
2003 const AVOption *o = NULL;
2004 test_ctx.class = &test_class;
2006 av_log_set_level(AV_LOG_QUIET);
2008 while (o = av_opt_next(&test_ctx, o)) {
2009 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2010 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2012 av_opt_set_defaults(&test_ctx);
2013 while (o = av_opt_next(&test_ctx, o)) {
2014 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2015 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2017 av_opt_free(&test_ctx);
2020 printf("\nTest av_opt_serialize()\n");
2022 TestContext test_ctx = { 0 };
2024 test_ctx.class = &test_class;
2026 av_log_set_level(AV_LOG_QUIET);
2028 av_opt_set_defaults(&test_ctx);
2029 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2030 printf("%s\n", buf);
2031 av_opt_free(&test_ctx);
2032 memset(&test_ctx, 0, sizeof(test_ctx));
2033 test_ctx.class = &test_class;
2034 av_set_options_string(&test_ctx, buf, "=", ",");
2036 if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2037 printf("%s\n", buf);
2041 av_opt_free(&test_ctx);
2044 printf("\nTesting av_set_options_string()\n");
2046 TestContext test_ctx = { 0 };
2047 static const char * const options[] = {
2063 "flags=+mu-lame : num=42: toggle=0",
2064 "num=42 : string=blahblah",
2065 "rational=0 : rational=1/2 : rational=1/-1",
2078 "video_rate=30000/1001",
2079 "video_rate=30/1.001",
2083 "duration=1\\:23\\:45.67",
2087 "cl=stereo+downmix",
2109 test_ctx.class = &test_class;
2110 av_opt_set_defaults(&test_ctx);
2112 av_log_set_level(AV_LOG_QUIET);
2114 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2115 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2116 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
2117 printf("Error '%s'\n", options[i]);
2119 printf("OK '%s'\n", options[i]);
2121 av_opt_free(&test_ctx);
2124 printf("\nTesting av_opt_set_from_string()\n");
2126 TestContext test_ctx = { 0 };
2127 static const char * const options[] = {
2135 " 5 : hello : size = pal ",
2136 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
2138 static const char * const shorthand[] = { "num", "string", NULL };
2140 test_ctx.class = &test_class;
2141 av_opt_set_defaults(&test_ctx);
2143 av_log_set_level(AV_LOG_QUIET);
2145 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2146 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2147 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
2148 printf("Error '%s'\n", options[i]);
2150 printf("OK '%s'\n", options[i]);
2152 av_opt_free(&test_ctx);