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>
35 #include "parseutils.h"
37 #include "mathematics.h"
38 #include "samplefmt.h"
43 //FIXME order them and do a bin search
44 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
46 const AVOption *o = NULL;
48 while ((o = av_next_option(v, o))) {
49 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
56 #if FF_API_OLD_AVOPTIONS
57 const AVOption *av_next_option(void *obj, const AVOption *last)
59 return av_opt_next(obj, last);
63 const AVOption *av_opt_next(void *obj, const AVOption *last)
65 AVClass *class = *(AVClass**)obj;
66 if (!last && class && class->option && class->option[0].name)
68 if (last && last[1].name)
73 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
76 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
77 case AV_OPT_TYPE_PIXEL_FMT:
78 case AV_OPT_TYPE_SAMPLE_FMT:
79 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
80 case AV_OPT_TYPE_DURATION:
81 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
82 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
83 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
84 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
85 *den = ((AVRational*)dst)->den;
87 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
89 return AVERROR(EINVAL);
92 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
94 if (o->max*den < num*intnum || o->min*den > num*intnum) {
95 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
96 num*intnum/den, o->name, o->min, o->max);
97 return AVERROR(ERANGE);
101 case AV_OPT_TYPE_FLAGS:
102 case AV_OPT_TYPE_PIXEL_FMT:
103 case AV_OPT_TYPE_SAMPLE_FMT:
104 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
105 case AV_OPT_TYPE_DURATION:
106 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
107 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
108 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
109 case AV_OPT_TYPE_RATIONAL:
110 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
111 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
114 return AVERROR(EINVAL);
119 static const double const_values[] = {
126 static const char * const const_names[] = {
133 static int hexchar2int(char c) {
134 if (c >= '0' && c <= '9') return c - '0';
135 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
136 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
140 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
142 int *lendst = (int *)(dst + 1);
144 int len = strlen(val);
150 return AVERROR(EINVAL);
153 ptr = bin = av_malloc(len);
155 int a = hexchar2int(*val++);
156 int b = hexchar2int(*val++);
157 if (a < 0 || b < 0) {
159 return AVERROR(EINVAL);
161 *ptr++ = (a << 4) | b;
169 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
172 *dst = av_strdup(val);
176 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
177 opt->type == AV_OPT_TYPE_CONST || \
178 opt->type == AV_OPT_TYPE_FLAGS || \
179 opt->type == AV_OPT_TYPE_INT) ? \
180 opt->default_val.i64 : opt->default_val.dbl)
182 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
184 int ret = 0, notfirst = 0;
193 if (*val == '+' || *val == '-') {
194 if (o->type == AV_OPT_TYPE_FLAGS)
200 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
205 const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
206 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
207 d = DEFAULT_NUMVAL(o_named);
208 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
209 else if (!strcmp(buf, "max" )) d = o->max;
210 else if (!strcmp(buf, "min" )) d = o->min;
211 else if (!strcmp(buf, "none" )) d = 0;
212 else if (!strcmp(buf, "all" )) d = ~0;
214 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
216 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
221 if (o->type == AV_OPT_TYPE_FLAGS) {
222 read_number(o, dst, NULL, NULL, &intnum);
223 if (cmd == '+') d = intnum | (int64_t)d;
224 else if (cmd == '-') d = intnum &~(int64_t)d;
226 read_number(o, dst, &num, &den, &intnum);
227 if (cmd == '+') d = notfirst*num*intnum/den + d;
228 else if (cmd == '-') d = notfirst*num*intnum/den - d;
231 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
242 #if FF_API_OLD_AVOPTIONS
243 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
245 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
248 return av_opt_set(obj, name, val, 0);
252 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
255 void *dst, *target_obj;
256 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
257 if (!o || !target_obj)
258 return AVERROR_OPTION_NOT_FOUND;
259 if (!val && (o->type != AV_OPT_TYPE_STRING &&
260 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
261 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
262 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR))
263 return AVERROR(EINVAL);
265 dst = ((uint8_t*)target_obj) + o->offset;
267 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
268 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
269 case AV_OPT_TYPE_FLAGS:
270 case AV_OPT_TYPE_INT:
271 case AV_OPT_TYPE_INT64:
272 case AV_OPT_TYPE_FLOAT:
273 case AV_OPT_TYPE_DOUBLE:
274 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
275 case AV_OPT_TYPE_IMAGE_SIZE:
276 if (!val || !strcmp(val, "none")) {
277 *(int *)dst = *((int *)dst + 1) = 0;
280 ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
282 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
284 case AV_OPT_TYPE_VIDEO_RATE:
286 ret = AVERROR(EINVAL);
288 ret = av_parse_video_rate(dst, val);
291 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
293 case AV_OPT_TYPE_PIXEL_FMT:
294 if (!val || !strcmp(val, "none")) {
295 ret = AV_PIX_FMT_NONE;
297 ret = av_get_pix_fmt(val);
298 if (ret == AV_PIX_FMT_NONE) {
300 ret = strtol(val, &tail, 0);
301 if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
302 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
303 return AVERROR(EINVAL);
307 *(enum AVPixelFormat *)dst = ret;
309 case AV_OPT_TYPE_SAMPLE_FMT:
310 if (!val || !strcmp(val, "none")) {
311 ret = AV_SAMPLE_FMT_NONE;
313 ret = av_get_sample_fmt(val);
314 if (ret == AV_SAMPLE_FMT_NONE) {
316 ret = strtol(val, &tail, 0);
317 if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
318 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
319 return AVERROR(EINVAL);
323 *(enum AVSampleFormat *)dst = ret;
325 case AV_OPT_TYPE_DURATION:
330 if ((ret = av_parse_time(dst, val, 1)) < 0)
331 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
335 case AV_OPT_TYPE_COLOR:
339 ret = av_parse_color(dst, val, -1, obj);
341 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
346 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
347 return AVERROR(EINVAL);
350 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
351 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
353 if (!o || o->type != opttype)\
354 return AVERROR(EINVAL);\
355 return set_string_number(obj, obj, o, val, name ## _out);\
358 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
359 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
360 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
361 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
362 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
363 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
365 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
368 void *dst, *target_obj;
369 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
371 if (!o || !target_obj)
372 return AVERROR_OPTION_NOT_FOUND;
374 dst = ((uint8_t*)target_obj) + o->offset;
375 return write_number(obj, o, dst, num, den, intnum);
378 #if FF_API_OLD_AVOPTIONS
379 const AVOption *av_set_double(void *obj, const char *name, double n)
381 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
382 if (set_number(obj, name, n, 1, 1, 0) < 0)
387 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
389 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
390 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
395 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
397 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
398 if (set_number(obj, name, 1, 1, n, 0) < 0)
404 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
406 return set_number(obj, name, 1, 1, val, search_flags);
409 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
411 return set_number(obj, name, val, 1, 1, search_flags);
414 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
416 return set_number(obj, name, val.num, val.den, 1, search_flags);
419 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
422 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
427 if (!o || !target_obj)
428 return AVERROR_OPTION_NOT_FOUND;
430 if (o->type != AV_OPT_TYPE_BINARY)
431 return AVERROR(EINVAL);
433 ptr = len ? av_malloc(len) : NULL;
435 return AVERROR(ENOMEM);
437 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
438 lendst = (int *)(dst + 1);
444 memcpy(ptr, val, len);
449 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
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;
456 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
457 av_log(obj, AV_LOG_ERROR,
458 "The value set by option '%s' is not an image size.\n", o->name);
459 return AVERROR(EINVAL);
462 av_log(obj, AV_LOG_ERROR,
463 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
464 return AVERROR(EINVAL);
466 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
467 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
471 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
474 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
476 if (!o || !target_obj)
477 return AVERROR_OPTION_NOT_FOUND;
478 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
479 av_log(obj, AV_LOG_ERROR,
480 "The value set by option '%s' is not a video rate.\n", o->name);
481 return AVERROR(EINVAL);
483 if (val.num <= 0 || val.den <= 0)
484 return AVERROR(EINVAL);
485 return set_number(obj, name, val.num, val.den, 1, search_flags);
488 static int set_format(void *obj, const char *name, int fmt, int search_flags,
489 enum AVOptionType type, const char *desc, int nb_fmts)
492 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
493 search_flags, &target_obj);
495 const AVClass *class = *(AVClass **)obj;
497 if (!o || !target_obj)
498 return AVERROR_OPTION_NOT_FOUND;
499 if (o->type != type) {
500 av_log(obj, AV_LOG_ERROR,
501 "The value set by option '%s' is not a %s format", name, desc);
502 return AVERROR(EINVAL);
505 #if LIBAVUTIL_VERSION_MAJOR < 53
506 if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
512 min = FFMIN(o->min, -1);
513 max = FFMAX(o->max, nb_fmts-1);
515 if (fmt < min || fmt > max) {
516 av_log(obj, AV_LOG_ERROR,
517 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
518 fmt, name, desc, min, max);
519 return AVERROR(ERANGE);
521 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
525 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
527 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
530 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
532 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
535 #if FF_API_OLD_AVOPTIONS
538 * @param buf a buffer which is used for returning non string values as strings, can be NULL
539 * @param buf_len allocated length in bytes of buf
541 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
543 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
549 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
552 dst= ((uint8_t*)obj) + o->offset;
553 if (o_out) *o_out= o;
556 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
557 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
558 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
559 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
560 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
561 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
562 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
563 case AV_OPT_TYPE_STRING: return *(void**)dst;
564 case AV_OPT_TYPE_BINARY:
565 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
566 if (len >= (buf_len + 1)/2) return NULL;
567 bin = *(uint8_t**)dst;
568 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
570 default: return NULL;
576 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
578 void *dst, *target_obj;
579 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
580 uint8_t *bin, buf[128];
584 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
585 return AVERROR_OPTION_NOT_FOUND;
587 dst = (uint8_t*)target_obj + o->offset;
591 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
592 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
593 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
594 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
595 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
596 case AV_OPT_TYPE_VIDEO_RATE:
597 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
598 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
599 case AV_OPT_TYPE_STRING:
601 *out_val = av_strdup(*(uint8_t**)dst);
603 *out_val = av_strdup("");
605 case AV_OPT_TYPE_BINARY:
606 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
607 if ((uint64_t)len*2 + 1 > INT_MAX)
608 return AVERROR(EINVAL);
609 if (!(*out_val = av_malloc(len*2 + 1)))
610 return AVERROR(ENOMEM);
611 bin = *(uint8_t**)dst;
612 for (i = 0; i < len; i++)
613 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
615 case AV_OPT_TYPE_IMAGE_SIZE:
616 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
618 case AV_OPT_TYPE_PIXEL_FMT:
619 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
621 case AV_OPT_TYPE_SAMPLE_FMT:
622 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
624 case AV_OPT_TYPE_DURATION:
625 i64 = *(int64_t *)dst;
626 ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
627 i64 / 3600000000, (int)((i64 / 60000000) % 60),
628 (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
630 case AV_OPT_TYPE_COLOR:
631 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", ((int *)dst)[0], ((int *)dst)[1], ((int *)dst)[2], ((int *)dst)[3]);
634 return AVERROR(EINVAL);
637 if (ret >= sizeof(buf))
638 return AVERROR(EINVAL);
639 *out_val = av_strdup(buf);
643 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
646 void *dst, *target_obj;
647 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
648 if (!o || !target_obj)
651 dst = ((uint8_t*)target_obj) + o->offset;
653 if (o_out) *o_out= o;
655 return read_number(o, dst, num, den, intnum);
662 #if FF_API_OLD_AVOPTIONS
663 double av_get_double(void *obj, const char *name, const AVOption **o_out)
669 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
671 return num*intnum/den;
674 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
680 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
681 return (AVRational){0, 0};
682 if (num == 1.0 && (int)intnum == intnum)
683 return (AVRational){intnum, den};
685 return av_d2q(num*intnum/den, 1<<24);
688 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
694 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
696 return num*intnum/den;
700 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
706 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
708 *out_val = num*intnum/den;
712 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
718 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
720 *out_val = num*intnum/den;
724 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
730 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
733 if (num == 1.0 && (int)intnum == intnum)
734 *out_val = (AVRational){intnum, den};
736 *out_val = av_d2q(num*intnum/den, 1<<24);
740 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
742 void *dst, *target_obj;
743 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
744 if (!o || !target_obj)
745 return AVERROR_OPTION_NOT_FOUND;
746 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
747 av_log(obj, AV_LOG_ERROR,
748 "The value for option '%s' is not an image size.\n", name);
749 return AVERROR(EINVAL);
752 dst = ((uint8_t*)target_obj) + o->offset;
753 if (w_out) *w_out = *(int *)dst;
754 if (h_out) *h_out = *((int *)dst+1);
758 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
764 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
767 if (num == 1.0 && (int)intnum == intnum)
768 *out_val = (AVRational){intnum, den};
770 *out_val = av_d2q(num*intnum/den, 1<<24);
774 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
775 enum AVOptionType type, const char *desc)
777 void *dst, *target_obj;
778 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
779 if (!o || !target_obj)
780 return AVERROR_OPTION_NOT_FOUND;
781 if (o->type != type) {
782 av_log(obj, AV_LOG_ERROR,
783 "The value for option '%s' is not a %s format.\n", desc, name);
784 return AVERROR(EINVAL);
787 dst = ((uint8_t*)target_obj) + o->offset;
788 *out_fmt = *(int *)dst;
792 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
794 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
797 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
799 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
802 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
804 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
805 const AVOption *flag = av_opt_find(obj, flag_name,
806 field ? field->unit : NULL, 0, 0);
809 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
810 av_opt_get_int(obj, field_name, 0, &res) < 0)
812 return res & flag->default_val.i64;
815 static void log_value(void *av_log_obj, int level, double d)
818 av_log(av_log_obj, level, "INT_MAX");
819 } else if (d == INT_MIN) {
820 av_log(av_log_obj, level, "INT_MIN");
821 } else if (d == (double)INT64_MAX) {
822 av_log(av_log_obj, level, "I64_MAX");
823 } else if (d == INT64_MIN) {
824 av_log(av_log_obj, level, "I64_MIN");
825 } else if (d == FLT_MAX) {
826 av_log(av_log_obj, level, "FLT_MAX");
827 } else if (d == FLT_MIN) {
828 av_log(av_log_obj, level, "FLT_MIN");
830 av_log(av_log_obj, level, "%g", d);
834 static void opt_list(void *obj, void *av_log_obj, const char *unit,
835 int req_flags, int rej_flags)
837 const AVOption *opt=NULL;
841 while ((opt = av_opt_next(obj, opt))) {
842 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
845 /* Don't print CONST's on level one.
846 * Don't print anything but CONST's on level two.
847 * Only print items from the requested unit.
849 if (!unit && opt->type==AV_OPT_TYPE_CONST)
851 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
853 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
855 else if (unit && opt->type == AV_OPT_TYPE_CONST)
856 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
858 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
859 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
863 case AV_OPT_TYPE_FLAGS:
864 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
866 case AV_OPT_TYPE_INT:
867 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
869 case AV_OPT_TYPE_INT64:
870 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
872 case AV_OPT_TYPE_DOUBLE:
873 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
875 case AV_OPT_TYPE_FLOAT:
876 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
878 case AV_OPT_TYPE_STRING:
879 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
881 case AV_OPT_TYPE_RATIONAL:
882 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
884 case AV_OPT_TYPE_BINARY:
885 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
887 case AV_OPT_TYPE_IMAGE_SIZE:
888 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
890 case AV_OPT_TYPE_VIDEO_RATE:
891 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
893 case AV_OPT_TYPE_PIXEL_FMT:
894 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
896 case AV_OPT_TYPE_SAMPLE_FMT:
897 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
899 case AV_OPT_TYPE_DURATION:
900 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
902 case AV_OPT_TYPE_COLOR:
903 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
905 case AV_OPT_TYPE_CONST:
907 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
910 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
911 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
912 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
913 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
914 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
915 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
918 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
920 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
922 case AV_OPT_TYPE_INT:
923 case AV_OPT_TYPE_INT64:
924 case AV_OPT_TYPE_DOUBLE:
925 case AV_OPT_TYPE_FLOAT:
926 case AV_OPT_TYPE_RATIONAL:
927 for (i = 0; i < r->nb_ranges; i++) {
928 av_log(av_log_obj, AV_LOG_INFO, " (from ");
929 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
930 av_log(av_log_obj, AV_LOG_INFO, " to ");
931 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
932 av_log(av_log_obj, AV_LOG_INFO, ")");
936 av_opt_freep_ranges(&r);
939 av_log(av_log_obj, AV_LOG_INFO, "\n");
940 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
941 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
946 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
951 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
953 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
958 void av_opt_set_defaults(void *s)
960 #if FF_API_OLD_AVOPTIONS
961 av_opt_set_defaults2(s, 0, 0);
964 void av_opt_set_defaults2(void *s, int mask, int flags)
967 const AVClass *class = *(AVClass **)s;
968 const AVOption *opt = NULL;
969 while ((opt = av_opt_next(s, opt)) != NULL) {
970 #if FF_API_OLD_AVOPTIONS
971 if ((opt->flags & mask) != flags)
975 case AV_OPT_TYPE_CONST:
976 /* Nothing to be done here */
978 case AV_OPT_TYPE_FLAGS:
979 case AV_OPT_TYPE_INT:
980 case AV_OPT_TYPE_INT64:
981 case AV_OPT_TYPE_DURATION:
982 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
984 case AV_OPT_TYPE_DOUBLE:
985 case AV_OPT_TYPE_FLOAT: {
987 val = opt->default_val.dbl;
988 av_opt_set_double(s, opt->name, val, 0);
991 case AV_OPT_TYPE_RATIONAL: {
993 val = av_d2q(opt->default_val.dbl, INT_MAX);
994 av_opt_set_q(s, opt->name, val, 0);
997 case AV_OPT_TYPE_COLOR:
998 case AV_OPT_TYPE_STRING:
999 case AV_OPT_TYPE_IMAGE_SIZE:
1000 case AV_OPT_TYPE_VIDEO_RATE:
1001 av_opt_set(s, opt->name, opt->default_val.str, 0);
1003 case AV_OPT_TYPE_PIXEL_FMT:
1004 #if LIBAVUTIL_VERSION_MAJOR < 53
1005 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1006 av_opt_set(s, opt->name, opt->default_val.str, 0);
1009 av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);
1011 case AV_OPT_TYPE_SAMPLE_FMT:
1012 #if LIBAVUTIL_VERSION_MAJOR < 53
1013 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1014 av_opt_set(s, opt->name, opt->default_val.str, 0);
1017 av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);
1019 case AV_OPT_TYPE_BINARY:
1020 /* Cannot set default for binary */
1023 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1029 * Store the value in the field in ctx that is named like key.
1030 * ctx must be an AVClass context, storing is done using AVOptions.
1032 * @param buf the string to parse, buf will be updated to point at the
1033 * separator just after the parsed key/value pair
1034 * @param key_val_sep a 0-terminated list of characters used to
1035 * separate key from value
1036 * @param pairs_sep a 0-terminated list of characters used to separate
1037 * two pairs from each other
1038 * @return 0 if the key/value pair has been successfully parsed and
1039 * set, or a negative value corresponding to an AVERROR code in case
1041 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1042 * the error code issued by av_opt_set() if the key/value pair
1045 static int parse_key_value_pair(void *ctx, const char **buf,
1046 const char *key_val_sep, const char *pairs_sep)
1048 char *key = av_get_token(buf, key_val_sep);
1053 return AVERROR(ENOMEM);
1055 if (*key && strspn(*buf, key_val_sep)) {
1057 val = av_get_token(buf, pairs_sep);
1060 return AVERROR(ENOMEM);
1063 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1065 return AVERROR(EINVAL);
1068 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1070 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1071 if (ret == AVERROR_OPTION_NOT_FOUND)
1072 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1079 int av_set_options_string(void *ctx, const char *opts,
1080 const char *key_val_sep, const char *pairs_sep)
1088 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1099 #define WHITESPACES " \n\t"
1101 static int is_key_char(char c)
1103 return (unsigned)((c | 32) - 'a') < 26 ||
1104 (unsigned)(c - '0') < 10 ||
1105 c == '-' || c == '_' || c == '/' || c == '.';
1109 * Read a key from a string.
1111 * The key consists of is_key_char characters and must be terminated by a
1112 * character from the delim string; spaces are ignored.
1114 * @return 0 for success (even with ellipsis), <0 for failure
1116 static int get_key(const char **ropts, const char *delim, char **rkey)
1118 const char *opts = *ropts;
1119 const char *key_start, *key_end;
1121 key_start = opts += strspn(opts, WHITESPACES);
1122 while (is_key_char(*opts))
1125 opts += strspn(opts, WHITESPACES);
1126 if (!*opts || !strchr(delim, *opts))
1127 return AVERROR(EINVAL);
1129 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1130 return AVERROR(ENOMEM);
1131 memcpy(*rkey, key_start, key_end - key_start);
1132 (*rkey)[key_end - key_start] = 0;
1137 int av_opt_get_key_value(const char **ropts,
1138 const char *key_val_sep, const char *pairs_sep,
1140 char **rkey, char **rval)
1143 char *key = NULL, *val;
1144 const char *opts = *ropts;
1146 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1147 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1148 return AVERROR(EINVAL);
1149 if (!(val = av_get_token(&opts, pairs_sep))) {
1151 return AVERROR(ENOMEM);
1159 int av_opt_set_from_string(void *ctx, const char *opts,
1160 const char *const *shorthand,
1161 const char *key_val_sep, const char *pairs_sep)
1164 const char *dummy_shorthand = NULL;
1165 char *av_uninit(parsed_key), *av_uninit(value);
1171 shorthand = &dummy_shorthand;
1174 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1175 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1176 &parsed_key, &value);
1178 if (ret == AVERROR(EINVAL))
1179 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1181 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1189 while (*shorthand) /* discard all remaining shorthand */
1192 key = *(shorthand++);
1195 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1196 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1197 if (ret == AVERROR_OPTION_NOT_FOUND)
1198 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1200 av_free(parsed_key);
1205 av_free(parsed_key);
1211 void av_opt_free(void *obj)
1213 const AVOption *o = NULL;
1214 while ((o = av_opt_next(obj, o)))
1215 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1216 av_freep((uint8_t *)obj + o->offset);
1219 int av_opt_set_dict(void *obj, AVDictionary **options)
1221 AVDictionaryEntry *t = NULL;
1222 AVDictionary *tmp = NULL;
1225 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1226 ret = av_opt_set(obj, t->key, t->value, 0);
1227 if (ret == AVERROR_OPTION_NOT_FOUND)
1228 av_dict_set(&tmp, t->key, t->value, 0);
1230 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1235 av_dict_free(options);
1240 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1241 int opt_flags, int search_flags)
1243 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1246 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1247 int opt_flags, int search_flags, void **target_obj)
1250 const AVOption *o = NULL;
1257 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1258 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1259 const AVClass *child = NULL;
1260 while (child = av_opt_child_class_next(c, child))
1261 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1265 while (child = av_opt_child_next(obj, child))
1266 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1271 while (o = av_opt_next(obj, o)) {
1272 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1273 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1274 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1276 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1287 void *av_opt_child_next(void *obj, void *prev)
1289 const AVClass *c = *(AVClass**)obj;
1291 return c->child_next(obj, prev);
1295 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1297 if (parent->child_class_next)
1298 return parent->child_class_next(prev);
1302 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1304 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1307 return (uint8_t*)obj + opt->offset;
1310 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1312 const AVClass *c = *(AVClass**)obj;
1313 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1315 if (c->version > (52 << 16 | 11 << 8))
1316 callback = c->query_ranges;
1319 callback = av_opt_query_ranges_default;
1321 return callback(ranges_arg, obj, key, flags);
1324 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1326 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1327 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1328 AVOptionRange *range = av_mallocz(sizeof(*range));
1329 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1334 if (!ranges || !range || !range_array || !field) {
1335 ret = AVERROR(ENOMEM);
1339 ranges->range = range_array;
1340 ranges->range[0] = range;
1341 ranges->nb_ranges = 1;
1342 range->is_range = 1;
1343 range->value_min = field->min;
1344 range->value_max = field->max;
1346 switch (field->type) {
1347 case AV_OPT_TYPE_INT:
1348 case AV_OPT_TYPE_INT64:
1349 case AV_OPT_TYPE_PIXEL_FMT:
1350 case AV_OPT_TYPE_SAMPLE_FMT:
1351 case AV_OPT_TYPE_FLOAT:
1352 case AV_OPT_TYPE_DOUBLE:
1353 case AV_OPT_TYPE_DURATION:
1354 case AV_OPT_TYPE_COLOR:
1356 case AV_OPT_TYPE_STRING:
1357 range->component_min = 0;
1358 range->component_max = 0x10FFFF; // max unicode value
1359 range->value_min = -1;
1360 range->value_max = INT_MAX;
1362 case AV_OPT_TYPE_RATIONAL:
1363 range->component_min = INT_MIN;
1364 range->component_max = INT_MAX;
1366 case AV_OPT_TYPE_IMAGE_SIZE:
1367 range->component_min = 0;
1368 range->component_max = INT_MAX/128/8;
1369 range->value_min = 0;
1370 range->value_max = INT_MAX/8;
1372 case AV_OPT_TYPE_VIDEO_RATE:
1373 range->component_min = 1;
1374 range->component_max = INT_MAX;
1375 range->value_min = 1;
1376 range->value_max = INT_MAX;
1379 ret = AVERROR(ENOSYS);
1383 *ranges_arg = ranges;
1388 av_free(range_array);
1392 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1395 AVOptionRanges *ranges = *rangesp;
1397 for (i = 0; i < ranges->nb_ranges; i++) {
1398 AVOptionRange *range = ranges->range[i];
1399 av_freep(&range->str);
1400 av_freep(&ranges->range[i]);
1402 av_freep(&ranges->range);
1408 typedef struct TestContext
1410 const AVClass *class;
1415 AVRational rational;
1416 AVRational video_rate;
1418 enum AVPixelFormat pix_fmt;
1419 enum AVSampleFormat sample_fmt;
1424 #define OFFSET(x) offsetof(TestContext, x)
1426 #define TEST_FLAG_COOL 01
1427 #define TEST_FLAG_LAME 02
1428 #define TEST_FLAG_MU 04
1430 static const AVOption test_options[]= {
1431 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1432 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1433 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1434 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
1435 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1436 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1437 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1438 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1439 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1440 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
1441 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
1442 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
1443 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1444 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1448 static const char *test_get_name(void *ctx)
1453 static const AVClass test_class = {
1463 printf("\nTesting av_set_options_string()\n");
1465 TestContext test_ctx = { 0 };
1466 const char *options[] = {
1482 "flags=+mu-lame : num=42: toggle=0",
1483 "num=42 : string=blahblah",
1484 "rational=0 : rational=1/2 : rational=1/-1",
1497 "video_rate=30000/1001",
1498 "video_rate=30/1.001",
1502 "duration=1\\:23\\:45.67",
1508 test_ctx.class = &test_class;
1509 av_opt_set_defaults(&test_ctx);
1511 av_log_set_level(AV_LOG_DEBUG);
1513 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1514 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1515 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1516 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1519 av_freep(&test_ctx.string);
1522 printf("\nTesting av_opt_set_from_string()\n");
1524 TestContext test_ctx = { 0 };
1525 const char *options[] = {
1533 " 5 : hello : size = pal ",
1534 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1536 const char *shorthand[] = { "num", "string", NULL };
1538 test_ctx.class = &test_class;
1539 av_opt_set_defaults(&test_ctx);
1540 test_ctx.string = av_strdup("default");
1542 av_log_set_level(AV_LOG_DEBUG);
1544 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1545 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1546 if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1547 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1550 av_freep(&test_ctx.string);