[FFmpeg-devel] [PATCH] lavfi/boxblur: add support to named options

Clément Bœsch ubitux at gmail.com
Thu Feb 21 21:04:26 CET 2013


On Wed, Feb 20, 2013 at 03:45:32PM +0100, Stefano Sabatini wrote:
> TODO: bump micro
> ---
>  doc/filters.texi         |   50 +++++++++++++++++++----------
>  libavfilter/vf_boxblur.c |   80 +++++++++++++++++++++++++++++++---------------
>  2 files changed, 88 insertions(+), 42 deletions(-)
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 3be8363..2a03d69 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -1782,17 +1782,30 @@ considered black, and defaults to 32.
>  
>  Apply boxblur algorithm to the input video.
>  
> -This filter accepts the parameters:
> - at var{luma_radius}:@var{luma_power}:@var{chroma_radius}:@var{chroma_power}:@var{alpha_radius}:@var{alpha_power}
> +The filter accepts parameters as a list of @var{key}=@var{value}
> +pairs, separated by ":". If the key of the first options is omitted,
> +the arguments are interpreted according to the syntax
> + at option{luma_radius}:@option{luma_power}:@option{chroma_radius}:@option{chroma_power}:@option{alpha_radius}:@option{alpha_power}.
>  
> -Chroma and alpha parameters are optional, if not specified they default
> -to the corresponding values set for @var{luma_radius} and
> - at var{luma_power}.
> +A description of the accepted parameters follows.
>  
> - at var{luma_radius}, @var{chroma_radius}, and @var{alpha_radius} represent
> -the radius in pixels of the box used for blurring the corresponding
> -input plane. They are expressions, and can contain the following
> -constants:
> + at table @option
> + at item luma_radius, lr
> + at item chroma_radius, cr
> + at item alpha_radius, ar
> +Set an expression for the box radius in pixels used for blurring the
> +corresponding input plane.
> +
> +The radius value must be a non-negative number, and must not be
> +greater than the value of the expression @code{min(w,h)/2} for the
> +luma and alpha planes, and of @code{min(cw,ch)/2} for the chroma
> +planes.
> +
> +Default value for @option{luma_radius} is "2".  If not specified,
> + at option{chroma_radius} and @option{alpha_radius} default to the
> +corresponding value set for @option{luma_radius}.
> +
> +The expressions can contain the following constants:
>  @table @option
>  @item w, h
>  the input width and height in pixels
> @@ -1805,13 +1818,18 @@ horizontal and vertical chroma subsample values. For example for the
>  pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
>  @end table
>  
> -The radius must be a non-negative number, and must not be greater than
> -the value of the expression @code{min(w,h)/2} for the luma and alpha planes,
> -and of @code{min(cw,ch)/2} for the chroma planes.
> + at item luma_power, lp
> + at item chroma_power, cp
> + at item alpha_power, ap
> +Specify how many times the boxblur filter is applied to the
> +corresponding plane.
>  
> - at var{luma_power}, @var{chroma_power}, and @var{alpha_power} represent
> -how many times the boxblur filter is applied to the corresponding
> -plane.
> +Default value for @option{luma_power} is 2.  If not specified,
> + at option{chroma_power} and @option{alpha_power} default to the
> +corresponding value set for @option{luma_power}.
> +
> +A value of 0 will disable the effect.
> + at end table
>  
>  Some examples follow:
>  
> @@ -1827,7 +1845,7 @@ boxblur=2:1
>  @item
>  Set luma radius to 2, alpha and chroma radius to 0
>  @example
> -boxblur=2:1:0:0:0:0
> +boxblur=2:1:cr=0:ar=0
>  @end example
>  
>  @item
> diff --git a/libavfilter/vf_boxblur.c b/libavfilter/vf_boxblur.c
> index 9ee3ea5..4ba4157 100644
> --- a/libavfilter/vf_boxblur.c
> +++ b/libavfilter/vf_boxblur.c
> @@ -28,6 +28,7 @@
>  #include "libavutil/avstring.h"
>  #include "libavutil/common.h"
>  #include "libavutil/eval.h"
> +#include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
>  #include "formats.h"
> @@ -57,15 +58,14 @@ enum var_name {
>  typedef struct {
>      int radius;
>      int power;
> +    char *radius_expr;
>  } FilterParam;
>  
>  typedef struct {
> +    const AVClass *class;
>      FilterParam luma_param;
>      FilterParam chroma_param;
>      FilterParam alpha_param;
> -    char luma_radius_expr  [256];
> -    char chroma_radius_expr[256];
> -    char alpha_radius_expr [256];
>  
>      int hsub, vsub;
>      int radius[4];
> @@ -73,6 +73,30 @@ typedef struct {
>      uint8_t *temp[2]; ///< temporary buffer used in blur_power()
>  } BoxBlurContext;
>  
> +#define OFFSET(x) offsetof(BoxBlurContext, x)
> +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
> +
> +static const AVOption boxblur_options[] = {
> +    { "luma_radius", "set luma radius", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
> +    { "lr",          "set luma radius", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
> +    { "luma_power",  "set luma power",  OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
> +    { "lp",          "set luma power",  OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
> +
> +    { "chroma_radius", "set chroma radius", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> +    { "cr",            "set chroma radius", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> +    { "chroma_power",  "set chroma power",  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
> +    { "cp",            "set chroma power",  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
> +
> +    { "alpha_radius", "set alpha radius", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> +    { "ar",           "set alpha radius", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> +    { "alpha_power",  "set alpha power",  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
> +    { "ap",           "set alpha power",  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
> +
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(boxblur);
> +
>  #define Y 0
>  #define U 1
>  #define V 2
> @@ -81,35 +105,36 @@ typedef struct {
>  static av_cold int init(AVFilterContext *ctx, const char *args)
>  {
>      BoxBlurContext *boxblur = ctx->priv;
> -    int e;
> +    static const char *shorthand[] = {
> +        "luma_radius",   "luma_power",
> +        "chroma_radius", "chroma_power",
> +        "alpha_radius",  "alpha_power",
> +        NULL
> +    };
> +    int ret;
>  
> -    if (!args) {
> -        av_log(ctx, AV_LOG_ERROR,
> -               "Filter expects 2 or 4 or 6 arguments, none provided\n");
> -        return AVERROR(EINVAL);
> -    }
> +    boxblur->class = &boxblur_class;
> +    av_opt_set_defaults(boxblur);
>  
> -    e = sscanf(args, "%255[^:]:%d:%255[^:]:%d:%255[^:]:%d",
> -               boxblur->luma_radius_expr,   &boxblur->luma_param  .power,
> -               boxblur->chroma_radius_expr, &boxblur->chroma_param.power,
> -               boxblur->alpha_radius_expr,  &boxblur->alpha_param .power);
> +    if ((ret = av_opt_set_from_string(boxblur, args, shorthand, "=", ":")) < 0)
> +        return ret;
>  
> -    if (e != 2 && e != 4 && e != 6) {
> -        av_log(ctx, AV_LOG_ERROR,
> -               "Filter expects 2 or 4 or 6 params, provided %d\n", e);
> -        return AVERROR(EINVAL);
> +    /* fill missing params */
> +    if (!boxblur->chroma_param.radius_expr) {
> +        boxblur->chroma_param.radius_expr = strdup(boxblur->luma_param.radius_expr);

av_strdup()

> +        if (!boxblur->chroma_param.radius_expr)
> +            return AVERROR(ENOMEM);
>      }
> -
> -    if (e < 4) {
> +    if (boxblur->chroma_param.power < 0)
>          boxblur->chroma_param.power = boxblur->luma_param.power;
> -        av_strlcpy(boxblur->chroma_radius_expr, boxblur->luma_radius_expr,
> -                   sizeof(boxblur->chroma_radius_expr));
> +
> +    if (!boxblur->alpha_param.radius_expr) {
> +        boxblur->alpha_param.radius_expr = strdup(boxblur->luma_param.radius_expr);

ditto

Note: is the expression freed in this case? I'm asking because an
av_free() would have fail.

> +        if (!boxblur->alpha_param.radius_expr)
> +            return AVERROR(ENOMEM);
>      }
> -    if (e < 6) {
> +    if (boxblur->alpha_param.power < 0)
>          boxblur->alpha_param.power = boxblur->luma_param.power;
> -        av_strlcpy(boxblur->alpha_radius_expr, boxblur->luma_radius_expr,
> -                   sizeof(boxblur->alpha_radius_expr));
> -    }
>  
>      return 0;
>  }
> @@ -120,6 +145,7 @@ static av_cold void uninit(AVFilterContext *ctx)
>  
>      av_freep(&boxblur->temp[0]);
>      av_freep(&boxblur->temp[1]);
> +    av_opt_free(boxblur);
>  }
>  
>  static int query_formats(AVFilterContext *ctx)
> @@ -163,7 +189,7 @@ static int config_input(AVFilterLink *inlink)
>      var_values[VAR_VSUB]    = 1<<boxblur->vsub;
>  
>  #define EVAL_RADIUS_EXPR(comp)                                          \
> -    expr = boxblur->comp##_radius_expr;                                 \
> +    expr = boxblur->comp##_param.radius_expr;                           \
>      ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,     \
>                                   NULL, NULL, NULL, NULL, NULL, 0, ctx); \
>      boxblur->comp##_param.radius = res;                                 \
> @@ -366,4 +392,6 @@ AVFilter avfilter_vf_boxblur = {
>  
>      .inputs    = avfilter_vf_boxblur_inputs,
>      .outputs   = avfilter_vf_boxblur_outputs,
> +
> +    .priv_class = &boxblur_class,
>  };

LGTM if tested otherwise

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130221/ff9499e3/attachment.asc>


More information about the ffmpeg-devel mailing list