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"
43 #if FF_API_OLD_AVOPTIONS
44 const AVOption *av_next_option(void *obj, const AVOption *last)
46 return av_opt_next(obj, last);
50 const AVOption *av_opt_next(void *obj, const AVOption *last)
52 AVClass *class = *(AVClass**)obj;
53 if (!last && class && class->option && class->option[0].name)
55 if (last && last[1].name)
60 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
63 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
64 case AV_OPT_TYPE_PIXEL_FMT:
65 case AV_OPT_TYPE_SAMPLE_FMT:
66 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
67 case AV_OPT_TYPE_CHANNEL_LAYOUT:
68 case AV_OPT_TYPE_DURATION:
69 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
70 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
71 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
72 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
73 *den = ((AVRational*)dst)->den;
75 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
77 return AVERROR(EINVAL);
80 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
82 if (o->type != AV_OPT_TYPE_FLAGS &&
83 (o->max * den < num * intnum || o->min * den > num * intnum)) {
84 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
85 num*intnum/den, 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_FLAGS:
100 case AV_OPT_TYPE_PIXEL_FMT:
101 case AV_OPT_TYPE_SAMPLE_FMT:
102 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
103 case AV_OPT_TYPE_DURATION:
104 case AV_OPT_TYPE_CHANNEL_LAYOUT:
105 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
106 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
107 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
108 case AV_OPT_TYPE_RATIONAL:
109 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
110 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
113 return AVERROR(EINVAL);
118 static int hexchar2int(char c) {
119 if (c >= '0' && c <= '9') return c - '0';
120 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
121 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
125 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
127 int *lendst = (int *)(dst + 1);
129 int len = strlen(val);
135 return AVERROR(EINVAL);
138 ptr = bin = av_malloc(len);
140 int a = hexchar2int(*val++);
141 int b = hexchar2int(*val++);
142 if (a < 0 || b < 0) {
144 return AVERROR(EINVAL);
146 *ptr++ = (a << 4) | b;
154 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
157 *dst = av_strdup(val);
161 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
162 opt->type == AV_OPT_TYPE_CONST || \
163 opt->type == AV_OPT_TYPE_FLAGS || \
164 opt->type == AV_OPT_TYPE_INT) ? \
165 opt->default_val.i64 : opt->default_val.dbl)
167 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
173 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
174 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
186 if (o->type == AV_OPT_TYPE_FLAGS) {
187 if (*val == '+' || *val == '-')
189 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
195 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
198 double const_values[64];
199 const char * const_names[64];
200 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
201 d = DEFAULT_NUMVAL(o_named);
204 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
205 if (o_named->type == AV_OPT_TYPE_CONST &&
207 !strcmp(o_named->unit, o->unit)) {
208 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
209 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
210 return AVERROR_PATCHWELCOME;
212 const_names [ci ] = o_named->name;
213 const_values[ci++] = DEFAULT_NUMVAL(o_named);
217 const_names [ci ] = "default";
218 const_values[ci++] = DEFAULT_NUMVAL(o);
219 const_names [ci ] = "max";
220 const_values[ci++] = o->max;
221 const_names [ci ] = "min";
222 const_values[ci++] = o->min;
223 const_names [ci ] = "none";
224 const_values[ci++] = 0;
225 const_names [ci ] = "all";
226 const_values[ci++] = ~0;
227 const_names [ci] = NULL;
228 const_values[ci] = 0;
230 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
231 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
233 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
238 if (o->type == AV_OPT_TYPE_FLAGS) {
239 read_number(o, dst, NULL, NULL, &intnum);
240 if (cmd == '+') d = intnum | (int64_t)d;
241 else if (cmd == '-') d = intnum &~(int64_t)d;
244 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
254 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
258 if (!val || !strcmp(val, "none")) {
263 ret = av_parse_video_size(dst, dst + 1, val);
265 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
269 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
273 ret = AVERROR(EINVAL);
275 ret = av_parse_video_rate(dst, val);
278 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
282 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
289 ret = av_parse_color(dst, val, -1, obj);
291 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
297 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
298 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
302 if (!val || !strcmp(val, "none")) {
308 fmt = strtol(val, &tail, 0);
309 if (*tail || (unsigned)fmt >= fmt_nb) {
310 av_log(obj, AV_LOG_ERROR,
311 "Unable to parse option value \"%s\" as %s\n", val, desc);
312 return AVERROR(EINVAL);
317 min = FFMAX(o->min, -1);
318 max = FFMIN(o->max, fmt_nb-1);
320 // hack for compatibility with old ffmpeg
321 if(min == 0 && max == 0) {
326 if (fmt < min || fmt > max) {
327 av_log(obj, AV_LOG_ERROR,
328 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
329 fmt, o->name, desc, min, max);
330 return AVERROR(ERANGE);
337 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
339 return set_string_fmt(obj, o, val, dst,
340 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
343 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
345 return set_string_fmt(obj, o, val, dst,
346 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
349 #if FF_API_OLD_AVOPTIONS
350 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
352 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
355 return av_opt_set(obj, name, val, 0);
359 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
362 void *dst, *target_obj;
363 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
364 if (!o || !target_obj)
365 return AVERROR_OPTION_NOT_FOUND;
366 if (!val && (o->type != AV_OPT_TYPE_STRING &&
367 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
368 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
369 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
370 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT))
371 return AVERROR(EINVAL);
373 if (o->flags & AV_OPT_FLAG_READONLY)
374 return AVERROR(EINVAL);
376 dst = ((uint8_t*)target_obj) + o->offset;
378 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
379 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
380 case AV_OPT_TYPE_FLAGS:
381 case AV_OPT_TYPE_INT:
382 case AV_OPT_TYPE_INT64:
383 case AV_OPT_TYPE_FLOAT:
384 case AV_OPT_TYPE_DOUBLE:
385 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
386 case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
387 case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
388 case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
389 case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
390 case AV_OPT_TYPE_DURATION:
395 if ((ret = av_parse_time(dst, val, 1)) < 0)
396 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
400 case AV_OPT_TYPE_COLOR: return set_string_color(obj, o, val, dst);
401 case AV_OPT_TYPE_CHANNEL_LAYOUT:
402 if (!val || !strcmp(val, "none")) {
405 #if FF_API_GET_CHANNEL_LAYOUT_COMPAT
406 int64_t cl = ff_get_channel_layout(val, 0);
408 int64_t cl = av_get_channel_layout(val);
411 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
412 ret = AVERROR(EINVAL);
414 *(int64_t *)dst = cl;
420 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
421 return AVERROR(EINVAL);
424 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
425 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
427 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
428 return AVERROR(EINVAL);\
429 return set_string_number(obj, obj, o, val, name ## _out);\
432 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
433 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
434 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
435 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
436 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
437 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
439 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
442 void *dst, *target_obj;
443 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
445 if (!o || !target_obj)
446 return AVERROR_OPTION_NOT_FOUND;
448 if (o->flags & AV_OPT_FLAG_READONLY)
449 return AVERROR(EINVAL);
451 dst = ((uint8_t*)target_obj) + o->offset;
452 return write_number(obj, o, dst, num, den, intnum);
455 #if FF_API_OLD_AVOPTIONS
456 const AVOption *av_set_double(void *obj, const char *name, double n)
458 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
459 if (set_number(obj, name, n, 1, 1, 0) < 0)
464 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
466 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
467 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
472 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
474 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
475 if (set_number(obj, name, 1, 1, n, 0) < 0)
481 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
483 return set_number(obj, name, 1, 1, val, search_flags);
486 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
488 return set_number(obj, name, val, 1, 1, search_flags);
491 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
493 return set_number(obj, name, val.num, val.den, 1, search_flags);
496 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
499 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
504 if (!o || !target_obj)
505 return AVERROR_OPTION_NOT_FOUND;
507 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
508 return AVERROR(EINVAL);
510 ptr = len ? av_malloc(len) : NULL;
512 return AVERROR(ENOMEM);
514 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
515 lendst = (int *)(dst + 1);
521 memcpy(ptr, val, len);
526 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
529 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
531 if (!o || !target_obj)
532 return AVERROR_OPTION_NOT_FOUND;
533 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
534 av_log(obj, AV_LOG_ERROR,
535 "The value set by option '%s' is not an image size.\n", o->name);
536 return AVERROR(EINVAL);
539 av_log(obj, AV_LOG_ERROR,
540 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
541 return AVERROR(EINVAL);
543 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
544 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
548 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
551 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
553 if (!o || !target_obj)
554 return AVERROR_OPTION_NOT_FOUND;
555 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
556 av_log(obj, AV_LOG_ERROR,
557 "The value set by option '%s' is not a video rate.\n", o->name);
558 return AVERROR(EINVAL);
560 if (val.num <= 0 || val.den <= 0)
561 return AVERROR(EINVAL);
562 return set_number(obj, name, val.num, val.den, 1, search_flags);
565 static int set_format(void *obj, const char *name, int fmt, int search_flags,
566 enum AVOptionType type, const char *desc, int nb_fmts)
569 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
570 search_flags, &target_obj);
573 if (!o || !target_obj)
574 return AVERROR_OPTION_NOT_FOUND;
575 if (o->type != type) {
576 av_log(obj, AV_LOG_ERROR,
577 "The value set by option '%s' is not a %s format", name, desc);
578 return AVERROR(EINVAL);
581 min = FFMAX(o->min, -1);
582 max = FFMIN(o->max, nb_fmts-1);
584 if (fmt < min || fmt > max) {
585 av_log(obj, AV_LOG_ERROR,
586 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
587 fmt, name, desc, min, max);
588 return AVERROR(ERANGE);
590 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
594 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
596 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
599 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
601 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
604 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
607 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
609 if (!o || !target_obj)
610 return AVERROR_OPTION_NOT_FOUND;
611 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
612 av_log(obj, AV_LOG_ERROR,
613 "The value set by option '%s' is not a channel layout.\n", o->name);
614 return AVERROR(EINVAL);
616 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
620 #if FF_API_OLD_AVOPTIONS
623 * @param buf a buffer which is used for returning non string values as strings, can be NULL
624 * @param buf_len allocated length in bytes of buf
626 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
628 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
634 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
637 dst= ((uint8_t*)obj) + o->offset;
638 if (o_out) *o_out= o;
641 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
642 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
643 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
644 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
645 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
646 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
647 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
648 case AV_OPT_TYPE_STRING: return *(void**)dst;
649 case AV_OPT_TYPE_BINARY:
650 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
651 if (len >= (buf_len + 1)/2) return NULL;
652 bin = *(uint8_t**)dst;
653 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
655 default: return NULL;
661 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
665 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
667 if (!o || !target_obj)
668 return AVERROR_OPTION_NOT_FOUND;
669 if (o->flags & AV_OPT_FLAG_READONLY)
670 return AVERROR(EINVAL);
672 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
674 av_dict_copy(dst, val, 0);
679 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
681 void *dst, *target_obj;
682 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
683 uint8_t *bin, buf[128];
687 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
688 return AVERROR_OPTION_NOT_FOUND;
690 dst = (uint8_t*)target_obj + o->offset;
694 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
695 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
696 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
697 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
698 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
699 case AV_OPT_TYPE_VIDEO_RATE:
700 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
701 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
702 case AV_OPT_TYPE_STRING:
704 *out_val = av_strdup(*(uint8_t**)dst);
706 *out_val = av_strdup("");
708 case AV_OPT_TYPE_BINARY:
709 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
710 if ((uint64_t)len*2 + 1 > INT_MAX)
711 return AVERROR(EINVAL);
712 if (!(*out_val = av_malloc(len*2 + 1)))
713 return AVERROR(ENOMEM);
714 bin = *(uint8_t**)dst;
715 for (i = 0; i < len; i++)
716 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
718 case AV_OPT_TYPE_IMAGE_SIZE:
719 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
721 case AV_OPT_TYPE_PIXEL_FMT:
722 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
724 case AV_OPT_TYPE_SAMPLE_FMT:
725 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
727 case AV_OPT_TYPE_DURATION:
728 i64 = *(int64_t *)dst;
729 ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
730 i64 / 3600000000, (int)((i64 / 60000000) % 60),
731 (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
733 case AV_OPT_TYPE_COLOR:
734 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", ((int *)dst)[0], ((int *)dst)[1], ((int *)dst)[2], ((int *)dst)[3]);
736 case AV_OPT_TYPE_CHANNEL_LAYOUT:
737 i64 = *(int64_t *)dst;
738 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
741 return AVERROR(EINVAL);
744 if (ret >= sizeof(buf))
745 return AVERROR(EINVAL);
746 *out_val = av_strdup(buf);
750 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
753 void *dst, *target_obj;
754 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
755 if (!o || !target_obj)
758 dst = ((uint8_t*)target_obj) + o->offset;
760 if (o_out) *o_out= o;
762 return read_number(o, dst, num, den, intnum);
769 #if FF_API_OLD_AVOPTIONS
770 double av_get_double(void *obj, const char *name, const AVOption **o_out)
776 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
778 return num*intnum/den;
781 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
787 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
788 return (AVRational){0, 0};
789 if (num == 1.0 && (int)intnum == intnum)
790 return (AVRational){intnum, den};
792 return av_d2q(num*intnum/den, 1<<24);
795 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
801 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
803 return num*intnum/den;
807 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
813 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
815 *out_val = num*intnum/den;
819 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
825 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
827 *out_val = num*intnum/den;
831 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
837 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
840 if (num == 1.0 && (int)intnum == intnum)
841 *out_val = (AVRational){intnum, den};
843 *out_val = av_d2q(num*intnum/den, 1<<24);
847 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
849 void *dst, *target_obj;
850 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
851 if (!o || !target_obj)
852 return AVERROR_OPTION_NOT_FOUND;
853 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
854 av_log(obj, AV_LOG_ERROR,
855 "The value for option '%s' is not an image size.\n", name);
856 return AVERROR(EINVAL);
859 dst = ((uint8_t*)target_obj) + o->offset;
860 if (w_out) *w_out = *(int *)dst;
861 if (h_out) *h_out = *((int *)dst+1);
865 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
871 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
874 if (num == 1.0 && (int)intnum == intnum)
875 *out_val = (AVRational){intnum, den};
877 *out_val = av_d2q(num*intnum/den, 1<<24);
881 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
882 enum AVOptionType type, const char *desc)
884 void *dst, *target_obj;
885 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
886 if (!o || !target_obj)
887 return AVERROR_OPTION_NOT_FOUND;
888 if (o->type != type) {
889 av_log(obj, AV_LOG_ERROR,
890 "The value for option '%s' is not a %s format.\n", desc, name);
891 return AVERROR(EINVAL);
894 dst = ((uint8_t*)target_obj) + o->offset;
895 *out_fmt = *(int *)dst;
899 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
901 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
904 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
906 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
909 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
911 void *dst, *target_obj;
912 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
913 if (!o || !target_obj)
914 return AVERROR_OPTION_NOT_FOUND;
915 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
916 av_log(obj, AV_LOG_ERROR,
917 "The value for option '%s' is not a channel layout.\n", name);
918 return AVERROR(EINVAL);
921 dst = ((uint8_t*)target_obj) + o->offset;
922 *cl = *(int64_t *)dst;
926 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
930 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
932 if (!o || !target_obj)
933 return AVERROR_OPTION_NOT_FOUND;
934 if (o->type != AV_OPT_TYPE_DICT)
935 return AVERROR(EINVAL);
937 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
938 av_dict_copy(out_val, src, 0);
943 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
945 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
946 const AVOption *flag = av_opt_find(obj, flag_name,
947 field ? field->unit : NULL, 0, 0);
950 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
951 av_opt_get_int(obj, field_name, 0, &res) < 0)
953 return res & flag->default_val.i64;
956 static void log_value(void *av_log_obj, int level, double d)
959 av_log(av_log_obj, level, "INT_MAX");
960 } else if (d == INT_MIN) {
961 av_log(av_log_obj, level, "INT_MIN");
962 } else if (d == UINT32_MAX) {
963 av_log(av_log_obj, level, "UINT32_MAX");
964 } else if (d == (double)INT64_MAX) {
965 av_log(av_log_obj, level, "I64_MAX");
966 } else if (d == INT64_MIN) {
967 av_log(av_log_obj, level, "I64_MIN");
968 } else if (d == FLT_MAX) {
969 av_log(av_log_obj, level, "FLT_MAX");
970 } else if (d == FLT_MIN) {
971 av_log(av_log_obj, level, "FLT_MIN");
972 } else if (d == -FLT_MAX) {
973 av_log(av_log_obj, level, "-FLT_MAX");
974 } else if (d == -FLT_MIN) {
975 av_log(av_log_obj, level, "-FLT_MIN");
976 } else if (d == DBL_MAX) {
977 av_log(av_log_obj, level, "DBL_MAX");
978 } else if (d == DBL_MIN) {
979 av_log(av_log_obj, level, "DBL_MIN");
980 } else if (d == -DBL_MAX) {
981 av_log(av_log_obj, level, "-DBL_MAX");
982 } else if (d == -DBL_MIN) {
983 av_log(av_log_obj, level, "-DBL_MIN");
985 av_log(av_log_obj, level, "%g", d);
989 static void opt_list(void *obj, void *av_log_obj, const char *unit,
990 int req_flags, int rej_flags)
992 const AVOption *opt=NULL;
996 while ((opt = av_opt_next(obj, opt))) {
997 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1000 /* Don't print CONST's on level one.
1001 * Don't print anything but CONST's on level two.
1002 * Only print items from the requested unit.
1004 if (!unit && opt->type==AV_OPT_TYPE_CONST)
1006 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
1008 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1010 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1011 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1013 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1014 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1017 switch (opt->type) {
1018 case AV_OPT_TYPE_FLAGS:
1019 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1021 case AV_OPT_TYPE_INT:
1022 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1024 case AV_OPT_TYPE_INT64:
1025 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1027 case AV_OPT_TYPE_DOUBLE:
1028 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1030 case AV_OPT_TYPE_FLOAT:
1031 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1033 case AV_OPT_TYPE_STRING:
1034 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1036 case AV_OPT_TYPE_RATIONAL:
1037 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1039 case AV_OPT_TYPE_BINARY:
1040 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1042 case AV_OPT_TYPE_IMAGE_SIZE:
1043 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1045 case AV_OPT_TYPE_VIDEO_RATE:
1046 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1048 case AV_OPT_TYPE_PIXEL_FMT:
1049 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1051 case AV_OPT_TYPE_SAMPLE_FMT:
1052 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1054 case AV_OPT_TYPE_DURATION:
1055 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1057 case AV_OPT_TYPE_COLOR:
1058 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1060 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1061 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1063 case AV_OPT_TYPE_CONST:
1065 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1068 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1069 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1070 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1071 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1072 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1073 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1074 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1075 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1078 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1080 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1081 switch (opt->type) {
1082 case AV_OPT_TYPE_INT:
1083 case AV_OPT_TYPE_INT64:
1084 case AV_OPT_TYPE_DOUBLE:
1085 case AV_OPT_TYPE_FLOAT:
1086 case AV_OPT_TYPE_RATIONAL:
1087 for (i = 0; i < r->nb_ranges; i++) {
1088 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1089 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1090 av_log(av_log_obj, AV_LOG_INFO, " to ");
1091 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1092 av_log(av_log_obj, AV_LOG_INFO, ")");
1096 av_opt_freep_ranges(&r);
1099 if (opt->type != AV_OPT_TYPE_CONST &&
1100 opt->type != AV_OPT_TYPE_BINARY &&
1101 !((opt->type == AV_OPT_TYPE_COLOR ||
1102 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1103 opt->type == AV_OPT_TYPE_STRING ||
1104 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1105 !opt->default_val.str)) {
1106 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1107 switch (opt->type) {
1108 case AV_OPT_TYPE_FLAGS:
1109 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1111 case AV_OPT_TYPE_DURATION:
1112 case AV_OPT_TYPE_INT:
1113 case AV_OPT_TYPE_INT64:
1114 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1116 case AV_OPT_TYPE_DOUBLE:
1117 case AV_OPT_TYPE_FLOAT:
1118 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1120 case AV_OPT_TYPE_RATIONAL: {
1121 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1122 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1124 case AV_OPT_TYPE_PIXEL_FMT:
1125 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1127 case AV_OPT_TYPE_SAMPLE_FMT:
1128 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1130 case AV_OPT_TYPE_COLOR:
1131 case AV_OPT_TYPE_IMAGE_SIZE:
1132 case AV_OPT_TYPE_STRING:
1133 case AV_OPT_TYPE_VIDEO_RATE:
1134 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1136 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1137 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1140 av_log(av_log_obj, AV_LOG_INFO, ")");
1143 av_log(av_log_obj, AV_LOG_INFO, "\n");
1144 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1145 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1150 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1155 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1157 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1162 void av_opt_set_defaults(void *s)
1164 #if FF_API_OLD_AVOPTIONS
1165 av_opt_set_defaults2(s, 0, 0);
1168 void av_opt_set_defaults2(void *s, int mask, int flags)
1171 const AVOption *opt = NULL;
1172 while ((opt = av_opt_next(s, opt))) {
1173 void *dst = ((uint8_t*)s) + opt->offset;
1174 #if FF_API_OLD_AVOPTIONS
1175 if ((opt->flags & mask) != flags)
1179 if (opt->flags & AV_OPT_FLAG_READONLY)
1182 switch (opt->type) {
1183 case AV_OPT_TYPE_CONST:
1184 /* Nothing to be done here */
1186 case AV_OPT_TYPE_FLAGS:
1187 case AV_OPT_TYPE_INT:
1188 case AV_OPT_TYPE_INT64:
1189 case AV_OPT_TYPE_DURATION:
1190 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1191 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1193 case AV_OPT_TYPE_DOUBLE:
1194 case AV_OPT_TYPE_FLOAT: {
1196 val = opt->default_val.dbl;
1197 write_number(s, opt, dst, val, 1, 1);
1200 case AV_OPT_TYPE_RATIONAL: {
1202 val = av_d2q(opt->default_val.dbl, INT_MAX);
1203 write_number(s, opt, dst, 1, val.den, val.num);
1206 case AV_OPT_TYPE_COLOR:
1207 set_string_color(s, opt, opt->default_val.str, dst);
1209 case AV_OPT_TYPE_STRING:
1210 set_string(s, opt, opt->default_val.str, dst);
1212 case AV_OPT_TYPE_IMAGE_SIZE:
1213 set_string_image_size(s, opt, opt->default_val.str, dst);
1215 case AV_OPT_TYPE_VIDEO_RATE:
1216 set_string_video_rate(s, opt, opt->default_val.str, dst);
1218 case AV_OPT_TYPE_PIXEL_FMT:
1219 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1221 case AV_OPT_TYPE_SAMPLE_FMT:
1222 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1224 case AV_OPT_TYPE_BINARY:
1225 case AV_OPT_TYPE_DICT:
1226 /* Cannot set defaults for these types */
1229 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1235 * Store the value in the field in ctx that is named like key.
1236 * ctx must be an AVClass context, storing is done using AVOptions.
1238 * @param buf the string to parse, buf will be updated to point at the
1239 * separator just after the parsed key/value pair
1240 * @param key_val_sep a 0-terminated list of characters used to
1241 * separate key from value
1242 * @param pairs_sep a 0-terminated list of characters used to separate
1243 * two pairs from each other
1244 * @return 0 if the key/value pair has been successfully parsed and
1245 * set, or a negative value corresponding to an AVERROR code in case
1247 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1248 * the error code issued by av_opt_set() if the key/value pair
1251 static int parse_key_value_pair(void *ctx, const char **buf,
1252 const char *key_val_sep, const char *pairs_sep)
1254 char *key = av_get_token(buf, key_val_sep);
1259 return AVERROR(ENOMEM);
1261 if (*key && strspn(*buf, key_val_sep)) {
1263 val = av_get_token(buf, pairs_sep);
1266 return AVERROR(ENOMEM);
1269 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1271 return AVERROR(EINVAL);
1274 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1276 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1277 if (ret == AVERROR_OPTION_NOT_FOUND)
1278 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1285 int av_set_options_string(void *ctx, const char *opts,
1286 const char *key_val_sep, const char *pairs_sep)
1294 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1305 #define WHITESPACES " \n\t"
1307 static int is_key_char(char c)
1309 return (unsigned)((c | 32) - 'a') < 26 ||
1310 (unsigned)(c - '0') < 10 ||
1311 c == '-' || c == '_' || c == '/' || c == '.';
1315 * Read a key from a string.
1317 * The key consists of is_key_char characters and must be terminated by a
1318 * character from the delim string; spaces are ignored.
1320 * @return 0 for success (even with ellipsis), <0 for failure
1322 static int get_key(const char **ropts, const char *delim, char **rkey)
1324 const char *opts = *ropts;
1325 const char *key_start, *key_end;
1327 key_start = opts += strspn(opts, WHITESPACES);
1328 while (is_key_char(*opts))
1331 opts += strspn(opts, WHITESPACES);
1332 if (!*opts || !strchr(delim, *opts))
1333 return AVERROR(EINVAL);
1335 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1336 return AVERROR(ENOMEM);
1337 memcpy(*rkey, key_start, key_end - key_start);
1338 (*rkey)[key_end - key_start] = 0;
1343 int av_opt_get_key_value(const char **ropts,
1344 const char *key_val_sep, const char *pairs_sep,
1346 char **rkey, char **rval)
1349 char *key = NULL, *val;
1350 const char *opts = *ropts;
1352 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1353 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1354 return AVERROR(EINVAL);
1355 if (!(val = av_get_token(&opts, pairs_sep))) {
1357 return AVERROR(ENOMEM);
1365 int av_opt_set_from_string(void *ctx, const char *opts,
1366 const char *const *shorthand,
1367 const char *key_val_sep, const char *pairs_sep)
1370 const char *dummy_shorthand = NULL;
1371 char *av_uninit(parsed_key), *av_uninit(value);
1377 shorthand = &dummy_shorthand;
1380 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1381 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1382 &parsed_key, &value);
1384 if (ret == AVERROR(EINVAL))
1385 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1387 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1395 while (*shorthand) /* discard all remaining shorthand */
1398 key = *(shorthand++);
1401 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1402 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1403 if (ret == AVERROR_OPTION_NOT_FOUND)
1404 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1406 av_free(parsed_key);
1411 av_free(parsed_key);
1417 void av_opt_free(void *obj)
1419 const AVOption *o = NULL;
1420 while ((o = av_opt_next(obj, o))) {
1422 case AV_OPT_TYPE_STRING:
1423 case AV_OPT_TYPE_BINARY:
1424 av_freep((uint8_t *)obj + o->offset);
1427 case AV_OPT_TYPE_DICT:
1428 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1437 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1439 AVDictionaryEntry *t = NULL;
1440 AVDictionary *tmp = NULL;
1446 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1447 ret = av_opt_set(obj, t->key, t->value, search_flags);
1448 if (ret == AVERROR_OPTION_NOT_FOUND)
1449 av_dict_set(&tmp, t->key, t->value, 0);
1451 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1456 av_dict_free(options);
1461 int av_opt_set_dict(void *obj, AVDictionary **options)
1463 return av_opt_set_dict2(obj, options, 0);
1466 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1467 int opt_flags, int search_flags)
1469 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1472 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1473 int opt_flags, int search_flags, void **target_obj)
1476 const AVOption *o = NULL;
1486 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1487 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1488 const AVClass *child = NULL;
1489 while (child = av_opt_child_class_next(c, child))
1490 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1494 while (child = av_opt_child_next(obj, child))
1495 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1500 while (o = av_opt_next(obj, o)) {
1501 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1502 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1503 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1505 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1516 void *av_opt_child_next(void *obj, void *prev)
1518 const AVClass *c = *(AVClass**)obj;
1520 return c->child_next(obj, prev);
1524 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1526 if (parent->child_class_next)
1527 return parent->child_class_next(prev);
1531 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1533 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1536 return (uint8_t*)obj + opt->offset;
1539 static int opt_size(enum AVOptionType type)
1542 case AV_OPT_TYPE_INT:
1543 case AV_OPT_TYPE_FLAGS: return sizeof(int);
1544 case AV_OPT_TYPE_DURATION:
1545 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1546 case AV_OPT_TYPE_INT64: return sizeof(int64_t);
1547 case AV_OPT_TYPE_DOUBLE: return sizeof(double);
1548 case AV_OPT_TYPE_FLOAT: return sizeof(float);
1549 case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
1550 case AV_OPT_TYPE_VIDEO_RATE:
1551 case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
1552 case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
1553 case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
1554 case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
1555 case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
1556 case AV_OPT_TYPE_COLOR: return 4;
1561 int av_opt_copy(void *dst, void *src)
1563 const AVOption *o = NULL;
1570 c = *(AVClass**)src;
1571 if (*(AVClass**)dst && c != *(AVClass**)dst)
1572 return AVERROR(EINVAL);
1574 while ((o = av_opt_next(src, o))) {
1575 void *field_dst = ((uint8_t*)dst) + o->offset;
1576 void *field_src = ((uint8_t*)src) + o->offset;
1577 uint8_t **field_dst8 = (uint8_t**)field_dst;
1578 uint8_t **field_src8 = (uint8_t**)field_src;
1580 if (o->type == AV_OPT_TYPE_STRING) {
1581 if (*field_dst8 != *field_src8)
1582 av_freep(field_dst8);
1583 *field_dst8 = av_strdup(*field_src8);
1584 if (*field_src8 && !*field_dst8)
1585 ret = AVERROR(ENOMEM);
1586 } else if (o->type == AV_OPT_TYPE_BINARY) {
1587 int len = *(int*)(field_src8 + 1);
1588 if (*field_dst8 != *field_src8)
1589 av_freep(field_dst8);
1590 *field_dst8 = av_memdup(*field_src8, len);
1591 if (len && !*field_dst8) {
1592 ret = AVERROR(ENOMEM);
1595 *(int*)(field_dst8 + 1) = len;
1596 } else if (o->type == AV_OPT_TYPE_CONST) {
1599 memcpy(field_dst, field_src, opt_size(o->type));
1605 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1608 const AVClass *c = *(AVClass**)obj;
1609 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1611 if (c->version > (52 << 16 | 11 << 8))
1612 callback = c->query_ranges;
1615 callback = av_opt_query_ranges_default;
1617 ret = callback(ranges_arg, obj, key, flags);
1619 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1621 (*ranges_arg)->nb_components = ret;
1626 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1628 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1629 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1630 AVOptionRange *range = av_mallocz(sizeof(*range));
1631 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1636 if (!ranges || !range || !range_array || !field) {
1637 ret = AVERROR(ENOMEM);
1641 ranges->range = range_array;
1642 ranges->range[0] = range;
1643 ranges->nb_ranges = 1;
1644 ranges->nb_components = 1;
1645 range->is_range = 1;
1646 range->value_min = field->min;
1647 range->value_max = field->max;
1649 switch (field->type) {
1650 case AV_OPT_TYPE_INT:
1651 case AV_OPT_TYPE_INT64:
1652 case AV_OPT_TYPE_PIXEL_FMT:
1653 case AV_OPT_TYPE_SAMPLE_FMT:
1654 case AV_OPT_TYPE_FLOAT:
1655 case AV_OPT_TYPE_DOUBLE:
1656 case AV_OPT_TYPE_DURATION:
1657 case AV_OPT_TYPE_COLOR:
1658 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1660 case AV_OPT_TYPE_STRING:
1661 range->component_min = 0;
1662 range->component_max = 0x10FFFF; // max unicode value
1663 range->value_min = -1;
1664 range->value_max = INT_MAX;
1666 case AV_OPT_TYPE_RATIONAL:
1667 range->component_min = INT_MIN;
1668 range->component_max = INT_MAX;
1670 case AV_OPT_TYPE_IMAGE_SIZE:
1671 range->component_min = 0;
1672 range->component_max = INT_MAX/128/8;
1673 range->value_min = 0;
1674 range->value_max = INT_MAX/8;
1676 case AV_OPT_TYPE_VIDEO_RATE:
1677 range->component_min = 1;
1678 range->component_max = INT_MAX;
1679 range->value_min = 1;
1680 range->value_max = INT_MAX;
1683 ret = AVERROR(ENOSYS);
1687 *ranges_arg = ranges;
1692 av_free(range_array);
1696 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1699 AVOptionRanges *ranges = *rangesp;
1704 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1705 AVOptionRange *range = ranges->range[i];
1707 av_freep(&range->str);
1708 av_freep(&ranges->range[i]);
1711 av_freep(&ranges->range);
1717 typedef struct TestContext
1719 const AVClass *class;
1724 AVRational rational;
1725 AVRational video_rate;
1727 enum AVPixelFormat pix_fmt;
1728 enum AVSampleFormat sample_fmt;
1731 int64_t channel_layout;
1734 #define OFFSET(x) offsetof(TestContext, x)
1736 #define TEST_FLAG_COOL 01
1737 #define TEST_FLAG_LAME 02
1738 #define TEST_FLAG_MU 04
1740 static const AVOption test_options[]= {
1741 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1742 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1743 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1744 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
1745 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1746 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1747 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1748 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1749 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1750 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
1751 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
1752 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
1753 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1754 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1755 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
1759 static const char *test_get_name(void *ctx)
1764 static const AVClass test_class = {
1774 printf("\nTesting av_set_options_string()\n");
1776 TestContext test_ctx = { 0 };
1777 static const char * const options[] = {
1793 "flags=+mu-lame : num=42: toggle=0",
1794 "num=42 : string=blahblah",
1795 "rational=0 : rational=1/2 : rational=1/-1",
1808 "video_rate=30000/1001",
1809 "video_rate=30/1.001",
1813 "duration=1\\:23\\:45.67",
1817 "cl=stereo+downmix",
1821 test_ctx.class = &test_class;
1822 av_opt_set_defaults(&test_ctx);
1824 av_log_set_level(AV_LOG_DEBUG);
1826 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1827 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1828 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1829 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1832 av_opt_free(&test_ctx);
1835 printf("\nTesting av_opt_set_from_string()\n");
1837 TestContext test_ctx = { 0 };
1838 static const char * const options[] = {
1846 " 5 : hello : size = pal ",
1847 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1849 static const char * const shorthand[] = { "num", "string", NULL };
1851 test_ctx.class = &test_class;
1852 av_opt_set_defaults(&test_ctx);
1854 av_log_set_level(AV_LOG_DEBUG);
1856 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1857 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1858 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1859 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1862 av_opt_free(&test_ctx);