[FFmpeg-devel] [PATCH 06/29] lavu/opt: add array options

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Mon Mar 4 15:29:59 EET 2024


Anton Khirnov:
> ---
>  doc/APIchanges        |   3 +
>  libavutil/opt.c       | 362 +++++++++++++++++++++++++++++++++++++-----
>  libavutil/opt.h       |  62 +++++++-
>  libavutil/tests/opt.c |  51 ++++++
>  tests/ref/fate/opt    |  35 +++-
>  5 files changed, 468 insertions(+), 45 deletions(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 7d46ebb006..3209614ed6 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2023-02-09
>  
>  API changes, most recent first:
>  
> +2024-02-xx - xxxxxxxxxx - lavu 58.xx.100 - opt.h
> +  Add AV_OPT_TYPE_FLAG_ARRAY and AVOptionArrayDef.
> +
>  2024-02-28 - xxxxxxxxxx - swr   4.14.100 - swresample.h
>    swr_convert() now accepts arrays of const pointers (to input and output).
>  
> diff --git a/libavutil/opt.h b/libavutil/opt.h
> index e402f6a0a0..77797b3fbe 100644
> --- a/libavutil/opt.h
> +++ b/libavutil/opt.h
> @@ -253,6 +253,17 @@ enum AVOptionType{
>  #endif
>      AV_OPT_TYPE_BOOL,
>      AV_OPT_TYPE_CHLAYOUT,
> +
> +    /**
> +     * May be combined with another regular option type to declare an array
> +     * option.
> +     *
> +     * For array options, @ref AVOption.offset should refer to a pointer
> +     * corresponding to the option type. The pointer should be immediately
> +     * followed by an unsigned int that will store the number of elements in the
> +     * array.

How about we make this actually type-safe and use an actual struct for
this instead of relying on the compiler not adding padding between a
pointer and an unsigned int?

> +     */
> +    AV_OPT_TYPE_FLAG_ARRAY = (1 << 16),
>  };
>  
>  /**
> @@ -298,6 +309,46 @@ enum AVOptionType{
>   */
>  #define AV_OPT_FLAG_CHILD_CONSTS    (1 << 18)
>  
> +/**
> + * Must be set as default_val for AV_OPT_TYPE_FLAG_ARRAY options.
> + */
> +typedef struct AVOptionArrayDef {
> +    /**
> +     * Must be set to sizeof(AVOptionArrayDef), in order to allow extending this
> +     * struct without breaking ABI.
> +     */
> +    size_t              sizeof_self;

I do not really get the point of this field: It is not sufficient for
detecting whether a user used a version that set a certain field due to
trailing padding (i.e. an additional field need not increase
sizeof(AVOptionArrayDef); this is actually relevant for this structure,
because on x64 at least a new int/unsigned would fit into trailing padding).
Luckily we already have a way to know the user's lavu version, as it is
contained in the AVClass.

> +
> +    /**
> +     * Native access only.
> +     *
> +     * Default value of the option, as would be serialized by av_opt_get() (i.e.
> +     * using the value of sep as the separator).
> +     */
> +    const char         *def;
> +
> +    /**
> +     * Minimum number of elements in the array. When this field is non-zero, def
> +     * must be non-NULL and contain at least this number of elements.
> +     */
> +    unsigned            size_min;
> +    /**
> +     * Maximum number of elements in the array, 0 when unlimited.
> +     */
> +    unsigned            size_max;
> +
> +    /**
> +     * Separator between array elements in string representations of this
> +     * option, used by av_opt_set() and av_opt_get(). It must be a printable
> +     * ASCII character, excluding alphanumeric and the backslash. A comma is
> +     * used when sep=0.
> +     *
> +     * The separator and the backslash must be backslash-escaped in order to
> +     * appear in string representations of the option value.
> +     */
> +    uint8_t             sep;

If this is a printable ASCII character, then it should be a char.

> +} AVOptionArrayDef;
> +
>  /**
>   * AVOption
>   */
> @@ -320,8 +371,7 @@ typedef struct AVOption {
>      enum AVOptionType type;
>  
>      /**
> -     * Native access only.
> -     *
> +     * Native access only, except when documented otherwise.
>       * the default value for scalar options
>       */
>      union {
> @@ -330,6 +380,14 @@ typedef struct AVOption {
>          const char *str;
>          /* TODO those are unused now */
>          AVRational q;
> +
> +        /**
> +         * Used for AV_OPT_TYPE_FLAG_ARRAY options. May be NULL.
> +         *
> +         * Foreign access to some members allowed, as noted in AVOptionArrayDef
> +         * documentation.
> +         */
> +        const AVOptionArrayDef *arr;
>      } default_val;
>      double min;                 ///< minimum valid value for the option
>      double max;                 ///< maximum valid value for the option



More information about the ffmpeg-devel mailing list