/* * AVOptions * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at> * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * AVOptions * @author Michael Niedermayer <michaelni@gmx.at> */ #include "avutil.h"#include "avassert.h"#include "avstring.h"#include "channel_layout.h"#include "common.h"#include "dict.h"#include "eval.h"#include "log.h"#include "parseutils.h"#include "pixdesc.h"#include "mathematics.h"#include "opt.h"#include "samplefmt.h"#include "bprint.h" #include <float.h> const AVOption *av_opt_next(const void *obj, const AVOption *last){ const AVClass *class; if (!obj) return NULL; class = *(const AVClass**)obj; if (!last && class && class->option && class->option[0].name) return class->option; if (last && last[1].name) return ++last; return NULL;} static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum){ switch (o->type) { case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst; return 0; case AV_OPT_TYPE_PIXEL_FMT: *intnum = *(enum AVPixelFormat *)dst; return 0; case AV_OPT_TYPE_SAMPLE_FMT: *intnum = *(enum AVSampleFormat *)dst; return 0; case AV_OPT_TYPE_BOOL: case AV_OPT_TYPE_INT: *intnum = *(int *)dst; return 0; case AV_OPT_TYPE_CHANNEL_LAYOUT: case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: case AV_OPT_TYPE_UINT64: *intnum = *(int64_t *)dst; return 0; case AV_OPT_TYPE_FLOAT: *num = *(float *)dst; return 0; case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst; return 0; case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational *)dst)->num; *den = ((AVRational *)dst)->den; return 0; case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0; } return AVERROR(EINVAL);} static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum){ if (o->type != AV_OPT_TYPE_FLAGS && (!den || o->max * den < num * intnum || o->min * den > num * intnum)) { num = den ? num * intnum / den : (num * intnum ? INFINITY : NAN); av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n", num, o->name, o->min, o->max); return AVERROR(ERANGE); } if (o->type == AV_OPT_TYPE_FLAGS) { double d = num*intnum/den; if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) { av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n", num*intnum/den, o->name); return AVERROR(ERANGE); } } switch (o->type) { case AV_OPT_TYPE_PIXEL_FMT: *(enum AVPixelFormat *)dst = llrint(num / den) * intnum; break; case AV_OPT_TYPE_SAMPLE_FMT: *(enum AVSampleFormat *)dst = llrint(num / den) * intnum; break; case AV_OPT_TYPE_BOOL: case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: *(int *)dst = llrint(num / den) * intnum; break; case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_CHANNEL_LAYOUT: case AV_OPT_TYPE_INT64:{ double d = num / den; if (intnum == 1 && d == (double)INT64_MAX) { *(int64_t *)dst = INT64_MAX; } else *(int64_t *)dst = llrint(d) * intnum; break;} case AV_OPT_TYPE_UINT64:{ double d = num / den; // We must special case uint64_t here as llrint() does not support values // outside the int64_t range and there is no portable function which does // "INT64_MAX + 1ULL" is used as it is representable exactly as IEEE double // while INT64_MAX is not if (intnum == 1 && d == (double)UINT64_MAX) { *(uint64_t *)dst = UINT64_MAX; } else if (d > INT64_MAX + 1ULL) { *(uint64_t *)dst = (llrint(d - (INT64_MAX + 1ULL)) + (INT64_MAX + 1ULL))*intnum; } else { *(uint64_t *)dst = llrint(d) * intnum; } break;} case AV_OPT_TYPE_FLOAT: *(float *)dst = num * intnum / den; break; case AV_OPT_TYPE_DOUBLE: *(double *)dst = num * intnum / den; break; case AV_OPT_TYPE_RATIONAL: case AV_OPT_TYPE_VIDEO_RATE: if ((int) num == num) *(AVRational *)dst = (AVRational) { num *intnum, den }; else *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24); break; default: return AVERROR(EINVAL); } return 0;} static int hexchar2int(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1;} static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst){ int *lendst = (int *)(dst + 1); uint8_t *bin, *ptr; int len; av_freep(dst); *lendst = 0; if (!val || !(len = strlen(val))) return 0; if (len & 1) return AVERROR(EINVAL); len /= 2; ptr = bin = av_malloc(len); if (!ptr) return AVERROR(ENOMEM); while (*val) { int a = hexchar2int(*val++); int b = hexchar2int(*val++); if (a < 0 || b < 0) { av_free(bin); return AVERROR(EINVAL); } *ptr++ = (a << 4) | b; } *dst = bin; *lendst = len; return 0;} static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst){ av_freep(dst); *dst = av_strdup(val); return *dst ? 0 : AVERROR(ENOMEM);} #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \ opt->type == AV_OPT_TYPE_UINT64 || \ opt->type == AV_OPT_TYPE_CONST || \ opt->type == AV_OPT_TYPE_FLAGS || \ opt->type == AV_OPT_TYPE_INT) \ ? opt->default_val.i64 \ : opt->default_val.dbl) static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst){ int ret = 0; int num, den; char c; if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) { if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0) return ret; ret = 0; } for (;;) { int i = 0; char buf[256]; int cmd = 0; double d; int64_t intnum = 1; if (o->type == AV_OPT_TYPE_FLAGS) { if (*val == '+' || *val == '-') cmd = *(val++); for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++) buf[i] = val[i]; buf[i] = 0; } { const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0); int res; int ci = 0; double const_values[64]; const char * const_names[64]; if (o_named && o_named->type == AV_OPT_TYPE_CONST) d = DEFAULT_NUMVAL(o_named); else { if (o->unit) { for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) { if (o_named->type == AV_OPT_TYPE_CONST && o_named->unit && !strcmp(o_named->unit, o->unit)) { if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) { av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit); return AVERROR_PATCHWELCOME; } const_names [ci ] = o_named->name; const_values[ci++] = DEFAULT_NUMVAL(o_named); } } } const_names [ci ] = "default"; const_values[ci++] = DEFAULT_NUMVAL(o); const_names [ci ] = "max"; const_values[ci++] = o->max; const_names [ci ] = "min"; const_values[ci++] = o->min; const_names [ci ] = "none"; const_values[ci++] = 0; const_names [ci ] = "all"; const_values[ci++] = ~0; const_names [ci] = NULL; const_values[ci] = 0; res = av_expr_parse_and_eval(&d, i ? buf : val, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj); if (res < 0) { av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val); return res; } } } if (o->type == AV_OPT_TYPE_FLAGS) { read_number(o, dst, NULL, NULL, &intnum); if (cmd == '+') d = intnum | (int64_t)d; else if (cmd == '-') d = intnum &~(int64_t)d; } if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0) return ret; val += i; if (!i || !*val) return 0; }} static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst){ int ret; if (!val || !strcmp(val, "none")) { dst[0] = dst[1] = 0; return 0; } ret = av_parse_video_size(dst, dst + 1, val); if (ret < 0) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val); return ret;} static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst){ int ret; if (!val) { ret = AVERROR(EINVAL); } else { ret = av_parse_video_rate(dst, val); } if (ret < 0) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val); return ret;} static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst){ int ret; if (!val) { return 0; } else { ret = av_parse_color(dst, val, -1, obj); if (ret < 0) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val); return ret; } return 0;} static const char *get_bool_name(int val){ if (val < 0) return "auto"; return val ? "true" : "false";} static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst){ int n; if (!val) return 0; if (!strcmp(val, "auto")) { n = -1; } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) { n = 1; } else if (av_match_name(val, "false,n,no,disable,disabled,off")) { n = 0; } else { char *end = NULL; n = strtol(val, &end, 10); if (val + strlen(val) != end) goto fail; } if (n < o->min || n > o->max) goto fail; *dst = n; return 0; fail: av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val); return AVERROR(EINVAL);} static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst, int fmt_nb, int ((*get_fmt)(const char *)), const char *desc){ int fmt, min, max; if (!val || !strcmp(val, "none")) { fmt = -1; } else { fmt = get_fmt(val); if (fmt == -1) { char *tail; fmt = strtol(val, &tail, 0); if (*tail || (unsigned)fmt >= fmt_nb) { av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as %s\n", val, desc); return AVERROR(EINVAL); } } } min = FFMAX(o->min, -1); max = FFMIN(o->max, fmt_nb-1); // hack for compatibility with old ffmpeg if(min == 0 && max == 0) { min = -1; max = fmt_nb-1; } if (fmt < min || fmt > max) { av_log(obj, AV_LOG_ERROR, "Value %d for parameter '%s' out of %s format range [%d - %d]\n", fmt, o->name, desc, min, max); return AVERROR(ERANGE); } *(int *)dst = fmt; return 0;} static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst){ return set_string_fmt(obj, o, val, dst, AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");} static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst){ return set_string_fmt(obj, o, val, dst, AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");} int av_opt_set(void *obj, const char *name, const char *val, int search_flags){ int ret = 0; void *dst, *target_obj; const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); if (!o || !target_obj) return AVERROR_OPTION_NOT_FOUND; if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT && o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE && o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR && o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL)) return AVERROR(EINVAL); if (o->flags & AV_OPT_FLAG_READONLY) return AVERROR(EINVAL); dst = ((uint8_t *)target_obj) + o->offset; switch (o->type) { case AV_OPT_TYPE_BOOL: return set_string_bool(obj, o, val, dst); case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst); case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst); case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: case AV_OPT_TYPE_UINT64: case AV_OPT_TYPE_FLOAT: case AV_OPT_TYPE_DOUBLE: case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst); case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst); case AV_OPT_TYPE_VIDEO_RATE: { AVRational tmp; ret = set_string_video_rate(obj, o, val, &tmp); if (ret < 0) return ret; return write_number(obj, o, dst, 1, tmp.den, tmp.num); } case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst); case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst); case AV_OPT_TYPE_DURATION: if (!val) { *(int64_t *)dst = 0; return 0; } else { if ((ret = av_parse_time(dst, val, 1)) < 0)Lines 1–500 of 2025. Load more
/* * AVOptions * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at> * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * AVOptions * @author Michael Niedermayer <michaelni@gmx.at> */ #include "avutil.h" #include "avassert.h" #include "avstring.h" #include "channel_layout.h" #include "common.h" #include "dict.h" #include "eval.h" #include "log.h" #include "parseutils.h" #include "pixdesc.h" #include "mathematics.h" #include "opt.h" #include "samplefmt.h" #include "bprint.h" #include <float.h> const AVOption *av_opt_next(const void *obj, const AVOption *last) { const AVClass *class; if (!obj) return NULL; class = *(const AVClass**)obj; if (!last && class && class->option && class->option[0].name) return class->option; if (last && last[1].name) return ++last; return NULL; } static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum) { switch (o->type) { case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst; return 0; case AV_OPT_TYPE_PIXEL_FMT: *intnum = *(enum AVPixelFormat *)dst; return 0; case AV_OPT_TYPE_SAMPLE_FMT: *intnum = *(enum AVSampleFormat *)dst; return 0; case AV_OPT_TYPE_BOOL: case AV_OPT_TYPE_INT: *intnum = *(int *)dst; return 0; case AV_OPT_TYPE_CHANNEL_LAYOUT: case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: case AV_OPT_TYPE_UINT64: *intnum = *(int64_t *)dst; return 0; case AV_OPT_TYPE_FLOAT: *num = *(float *)dst; return 0; case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst; return 0; case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational *)dst)->num; *den = ((AVRational *)dst)->den; return 0; case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0; } return AVERROR(EINVAL); } static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum) { if (o->type != AV_OPT_TYPE_FLAGS && (!den || o->max * den < num * intnum || o->min * den > num * intnum)) { num = den ? num * intnum / den : (num * intnum ? INFINITY : NAN); av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n", num, o->name, o->min, o->max); return AVERROR(ERANGE); } if (o->type == AV_OPT_TYPE_FLAGS) { double d = num*intnum/den; if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) { av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n", num*intnum/den, o->name); return AVERROR(ERANGE); } } switch (o->type) { case AV_OPT_TYPE_PIXEL_FMT: *(enum AVPixelFormat *)dst = llrint(num / den) * intnum; break; case AV_OPT_TYPE_SAMPLE_FMT: *(enum AVSampleFormat *)dst = llrint(num / den) * intnum; break; case AV_OPT_TYPE_BOOL: case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: *(int *)dst = llrint(num / den) * intnum; break; case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_CHANNEL_LAYOUT: case AV_OPT_TYPE_INT64:{ double d = num / den; if (intnum == 1 && d == (double)INT64_MAX) { *(int64_t *)dst = INT64_MAX; } else *(int64_t *)dst = llrint(d) * intnum; break;} case AV_OPT_TYPE_UINT64:{ double d = num / den; // We must special case uint64_t here as llrint() does not support values // outside the int64_t range and there is no portable function which does // "INT64_MAX + 1ULL" is used as it is representable exactly as IEEE double // while INT64_MAX is not if (intnum == 1 && d == (double)UINT64_MAX) { *(uint64_t *)dst = UINT64_MAX; } else if (d > INT64_MAX + 1ULL) { *(uint64_t *)dst = (llrint(d - (INT64_MAX + 1ULL)) + (INT64_MAX + 1ULL))*intnum; } else { *(uint64_t *)dst = llrint(d) * intnum; } break;} case AV_OPT_TYPE_FLOAT: *(float *)dst = num * intnum / den; break; case AV_OPT_TYPE_DOUBLE: *(double *)dst = num * intnum / den; break; case AV_OPT_TYPE_RATIONAL: case AV_OPT_TYPE_VIDEO_RATE: if ((int) num == num) *(AVRational *)dst = (AVRational) { num *intnum, den }; else *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24); break; default: return AVERROR(EINVAL); } return 0; } static int hexchar2int(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1; } static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst) { int *lendst = (int *)(dst + 1); uint8_t *bin, *ptr; int len; av_freep(dst); *lendst = 0; if (!val || !(len = strlen(val))) return 0; if (len & 1) return AVERROR(EINVAL); len /= 2; ptr = bin = av_malloc(len); if (!ptr) return AVERROR(ENOMEM); while (*val) { int a = hexchar2int(*val++); int b = hexchar2int(*val++); if (a < 0 || b < 0) { av_free(bin); return AVERROR(EINVAL); } *ptr++ = (a << 4) | b; } *dst = bin; *lendst = len; return 0; } static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst) { av_freep(dst); *dst = av_strdup(val); return *dst ? 0 : AVERROR(ENOMEM); } #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \ opt->type == AV_OPT_TYPE_UINT64 || \ opt->type == AV_OPT_TYPE_CONST || \ opt->type == AV_OPT_TYPE_FLAGS || \ opt->type == AV_OPT_TYPE_INT) \ ? opt->default_val.i64 \ : opt->default_val.dbl) static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst) { int ret = 0; int num, den; char c; if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) { if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0) return ret; ret = 0; } for (;;) { int i = 0; char buf[256]; int cmd = 0; double d; int64_t intnum = 1; if (o->type == AV_OPT_TYPE_FLAGS) { if (*val == '+' || *val == '-') cmd = *(val++); for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++) buf[i] = val[i]; buf[i] = 0; } { const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0); int res; int ci = 0; double const_values[64]; const char * const_names[64]; if (o_named && o_named->type == AV_OPT_TYPE_CONST) d = DEFAULT_NUMVAL(o_named); else { if (o->unit) { for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) { if (o_named->type == AV_OPT_TYPE_CONST && o_named->unit && !strcmp(o_named->unit, o->unit)) { if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) { av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit); return AVERROR_PATCHWELCOME; } const_names [ci ] = o_named->name; const_values[ci++] = DEFAULT_NUMVAL(o_named); } } } const_names [ci ] = "default"; const_values[ci++] = DEFAULT_NUMVAL(o); const_names [ci ] = "max"; const_values[ci++] = o->max; const_names [ci ] = "min"; const_values[ci++] = o->min; const_names [ci ] = "none"; const_values[ci++] = 0; const_names [ci ] = "all"; const_values[ci++] = ~0; const_names [ci] = NULL; const_values[ci] = 0; res = av_expr_parse_and_eval(&d, i ? buf : val, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj); if (res < 0) { av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val); return res; } } } if (o->type == AV_OPT_TYPE_FLAGS) { read_number(o, dst, NULL, NULL, &intnum); if (cmd == '+') d = intnum | (int64_t)d; else if (cmd == '-') d = intnum &~(int64_t)d; } if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0) return ret; val += i; if (!i || !*val) return 0; } } static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst) { int ret; if (!val || !strcmp(val, "none")) { dst[0] = dst[1] = 0; return 0; } ret = av_parse_video_size(dst, dst + 1, val); if (ret < 0) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val); return ret; } static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst) { int ret; if (!val) { ret = AVERROR(EINVAL); } else { ret = av_parse_video_rate(dst, val); } if (ret < 0) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val); return ret; } static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst) { int ret; if (!val) { return 0; } else { ret = av_parse_color(dst, val, -1, obj); if (ret < 0) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val); return ret; } return 0; } static const char *get_bool_name(int val) { if (val < 0) return "auto"; return val ? "true" : "false"; } static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst) { int n; if (!val) return 0; if (!strcmp(val, "auto")) { n = -1; } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) { n = 1; } else if (av_match_name(val, "false,n,no,disable,disabled,off")) { n = 0; } else { char *end = NULL; n = strtol(val, &end, 10); if (val + strlen(val) != end) goto fail; } if (n < o->min || n > o->max) goto fail; *dst = n; return 0; fail: av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val); return AVERROR(EINVAL); } static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst, int fmt_nb, int ((*get_fmt)(const char *)), const char *desc) { int fmt, min, max; if (!val || !strcmp(val, "none")) { fmt = -1; } else { fmt = get_fmt(val); if (fmt == -1) { char *tail; fmt = strtol(val, &tail, 0); if (*tail || (unsigned)fmt >= fmt_nb) { av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as %s\n", val, desc); return AVERROR(EINVAL); } } } min = FFMAX(o->min, -1); max = FFMIN(o->max, fmt_nb-1); // hack for compatibility with old ffmpeg if(min == 0 && max == 0) { min = -1; max = fmt_nb-1; } if (fmt < min || fmt > max) { av_log(obj, AV_LOG_ERROR, "Value %d for parameter '%s' out of %s format range [%d - %d]\n", fmt, o->name, desc, min, max); return AVERROR(ERANGE); } *(int *)dst = fmt; return 0; } static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst) { return set_string_fmt(obj, o, val, dst, AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format"); } static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst) { return set_string_fmt(obj, o, val, dst, AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format"); } int av_opt_set(void *obj, const char *name, const char *val, int search_flags) { int ret = 0; void *dst, *target_obj; const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); if (!o || !target_obj) return AVERROR_OPTION_NOT_FOUND; if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT && o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE && o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR && o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL)) return AVERROR(EINVAL); if (o->flags & AV_OPT_FLAG_READONLY) return AVERROR(EINVAL); dst = ((uint8_t *)target_obj) + o->offset; switch (o->type) { case AV_OPT_TYPE_BOOL: return set_string_bool(obj, o, val, dst); case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst); case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst); case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: case AV_OPT_TYPE_UINT64: case AV_OPT_TYPE_FLOAT: case AV_OPT_TYPE_DOUBLE: case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst); case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst); case AV_OPT_TYPE_VIDEO_RATE: { AVRational tmp; ret = set_string_video_rate(obj, o, val, &tmp); if (ret < 0) return ret; return write_number(obj, o, dst, 1, tmp.den, tmp.num); } case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst); case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst); case AV_OPT_TYPE_DURATION: if (!val) { *(int64_t *)dst = 0; return 0; } else { if ((ret = av_parse_time(dst, val, 1)) < 0)