2797 lines · 86.0 KB · C
RawDownload
/* * 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 "dict.h"#include "eval.h"#include "log.h"#include "mem.h"#include "parseutils.h"#include "pixdesc.h"#include "mathematics.h"#include "opt.h"#include "samplefmt.h"#include "bprint.h"#include "version.h" #include <float.h> #define TYPE_BASE(type) ((type) & ~AV_OPT_TYPE_FLAG_ARRAY) 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 const struct {    size_t      size;    const char *name;} opt_type_desc[] = {    [AV_OPT_TYPE_FLAGS]         = { sizeof(unsigned),       "<flags>" },    [AV_OPT_TYPE_INT]           = { sizeof(int),            "<int>" },    [AV_OPT_TYPE_INT64]         = { sizeof(int64_t),        "<int64>" },    [AV_OPT_TYPE_UINT]          = { sizeof(unsigned),       "<unsigned>" },    [AV_OPT_TYPE_UINT64]        = { sizeof(uint64_t),       "<uint64>" },    [AV_OPT_TYPE_DOUBLE]        = { sizeof(double),         "<double>" },    [AV_OPT_TYPE_FLOAT]         = { sizeof(float),          "<float>" },    [AV_OPT_TYPE_STRING]        = { sizeof(char *),         "<string>" },    [AV_OPT_TYPE_RATIONAL]      = { sizeof(AVRational),     "<rational>" },    [AV_OPT_TYPE_BINARY]        = { sizeof(uint8_t *),      "<binary>" },    [AV_OPT_TYPE_DICT]          = { sizeof(AVDictionary *), "<dictionary>" },    [AV_OPT_TYPE_IMAGE_SIZE]    = { sizeof(int[2]),         "<image_size>" },    [AV_OPT_TYPE_VIDEO_RATE]    = { sizeof(AVRational),     "<video_rate>" },    [AV_OPT_TYPE_PIXEL_FMT]     = { sizeof(int),            "<pix_fmt>" },    [AV_OPT_TYPE_SAMPLE_FMT]    = { sizeof(int),            "<sample_fmt>" },    [AV_OPT_TYPE_DURATION]      = { sizeof(int64_t),        "<duration>" },    [AV_OPT_TYPE_COLOR]         = { sizeof(uint8_t[4]),     "<color>" },    [AV_OPT_TYPE_CHLAYOUT]      = { sizeof(AVChannelLayout),"<channel_layout>" },    [AV_OPT_TYPE_BOOL]          = { sizeof(int),            "<boolean>" },}; // option is plain old datastatic int opt_is_pod(enum AVOptionType type){    switch (type) {    case AV_OPT_TYPE_FLAGS:    case AV_OPT_TYPE_INT:    case AV_OPT_TYPE_INT64:    case AV_OPT_TYPE_DOUBLE:    case AV_OPT_TYPE_FLOAT:    case AV_OPT_TYPE_RATIONAL:    case AV_OPT_TYPE_UINT64:    case AV_OPT_TYPE_IMAGE_SIZE:    case AV_OPT_TYPE_PIXEL_FMT:    case AV_OPT_TYPE_SAMPLE_FMT:    case AV_OPT_TYPE_VIDEO_RATE:    case AV_OPT_TYPE_DURATION:    case AV_OPT_TYPE_COLOR:    case AV_OPT_TYPE_BOOL:    case AV_OPT_TYPE_UINT:        return 1;    }    return 0;} static uint8_t opt_array_sep(const AVOption *o){    const AVOptionArrayDef *d = o->default_val.arr;    av_assert1(o->type & AV_OPT_TYPE_FLAG_ARRAY);    return (d && d->sep) ? d->sep : ',';} static void *opt_array_pelem(const AVOption *o, void *array, unsigned idx){    av_assert1(o->type & AV_OPT_TYPE_FLAG_ARRAY);    return (uint8_t *)array + idx * opt_type_desc[TYPE_BASE(o->type)].size;} static unsigned *opt_array_pcount(const void *parray){    return (unsigned *)((const void * const *)parray + 1);} static void opt_free_elem(enum AVOptionType type, void *ptr){    switch (TYPE_BASE(type)) {    case AV_OPT_TYPE_STRING:    case AV_OPT_TYPE_BINARY:        av_freep(ptr);        break;     case AV_OPT_TYPE_DICT:        av_dict_free((AVDictionary **)ptr);        break;     case AV_OPT_TYPE_CHLAYOUT:        av_channel_layout_uninit((AVChannelLayout *)ptr);        break;     default:        break;    }} static void opt_free_array(const AVOption *o, void *parray, unsigned *count){    for (unsigned i = 0; i < *count; i++)        opt_free_elem(o->type, opt_array_pelem(o, *(void **)parray, i));     av_freep(parray);    *count = 0;} /** * Perform common setup for option-setting functions. * * @param require_type when non-0, require the option to be of this type * @param ptgt         target object is written here * @param po           the option is written here * @param pdst         pointer to option value is written here */static int opt_set_init(void *obj, const char *name, int search_flags,                        int require_type,                        void **ptgt, const AVOption **po, void **pdst){    const AVOption *o;    void *tgt;     o = av_opt_find2(obj, name, NULL, 0, search_flags, &tgt);    if (!o || !tgt)        return AVERROR_OPTION_NOT_FOUND;     if (o->flags & AV_OPT_FLAG_READONLY)        return AVERROR(EINVAL);     if (require_type && (o->type != require_type)) {        av_log(obj, AV_LOG_ERROR,               "Tried to set option '%s' of type %s from value of type %s, "               "this is not supported\n", o->name, opt_type_desc[o->type].name,               opt_type_desc[require_type].name);        return AVERROR(EINVAL);    }     if (!(o->flags & AV_OPT_FLAG_RUNTIME_PARAM)) {        unsigned *state_flags = NULL;        const AVClass *class;         // try state flags first from the target (child), then from its parent        class = *(const AVClass**)tgt;        if (#if LIBAVUTIL_VERSION_MAJOR < 60            class->version >= AV_VERSION_INT(59, 41, 100) &&#endif            class->state_flags_offset)            state_flags = (unsigned*)((uint8_t*)tgt + class->state_flags_offset);         if (!state_flags && obj != tgt) {            class = *(const AVClass**)obj;            if (#if LIBAVUTIL_VERSION_MAJOR < 60                class->version >= AV_VERSION_INT(59, 41, 100) &&#endif                class->state_flags_offset)                state_flags = (unsigned*)((uint8_t*)obj + class->state_flags_offset);        }         if (state_flags && (*state_flags & AV_CLASS_STATE_INITIALIZED)) {            av_log(obj, AV_LOG_ERROR, "Option '%s' is not a runtime option and "                   "so cannot be set after the object has been initialized\n",                   o->name);#if LIBAVUTIL_VERSION_MAJOR >= 60            return AVERROR(EINVAL);#endif        }    }     if (o->flags & AV_OPT_FLAG_DEPRECATED)        av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);     if (po)        *po   = o;    if (ptgt)        *ptgt = tgt;    if (pdst)        *pdst = ((uint8_t *)tgt) + o->offset;     return 0;} static AVRational double_to_rational(double d){     AVRational r = av_d2q(d, 1 << 24);     if ((!r.num || !r.den) && d)         r = av_d2q(d, INT_MAX);     return r;} static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum){    switch (TYPE_BASE(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_UINT:        *intnum = *(unsigned int *)dst;        return 0;    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:        *intnum = o->default_val.i64;        return 0;    }    return AVERROR(EINVAL);} static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum){    const enum AVOptionType type = TYPE_BASE(o->type);     if (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 (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 (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:    case AV_OPT_TYPE_UINT:        *(int *)dst = llrint(num / den) * intnum;        break;    case AV_OPT_TYPE_DURATION:    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 = double_to_rational(num * intnum / den);        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);    if (!val)        return 0;    *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_UINT  || \                              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){    const enum AVOptionType type = TYPE_BASE(o->type);    int ret = 0;     if (type == AV_OPT_TYPE_RATIONAL || type == AV_OPT_TYPE_VIDEO_RATE) {        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 (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;        }         {            int res;            int ci = 0;            double const_values[64];            const char * const_names[64];            int search_flags = (o->flags & AV_OPT_FLAG_CHILD_CONSTS) ? AV_OPT_SEARCH_CHILDREN : 0;            const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, search_flags);            if (o_named && o_named->type == AV_OPT_TYPE_CONST) {                d = DEFAULT_NUMVAL(o_named);                if (o_named->flags & AV_OPT_FLAG_DEPRECATED)                    av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n",                           o_named->name, o_named->help);            } 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 (type == AV_OPT_TYPE_FLAGS) {            intnum = *(unsigned int*)dst;            if (cmd == '+')

Lines 1–500 of 2797.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t