[FFmpeg-devel] [PATCH] avfilter/fade: add color option.

Stefano Sabatini stefasab at gmail.com
Sat Nov 9 19:47:19 CET 2013


Subject nit: lavfi/fade: add color option

On date Saturday 2013-11-09 00:08:05 +0100, Clément Bœsch encoded:
> Fixes Ticket #1822.
> ---
>  doc/filters.texi      | 13 +++++++++----
>  libavfilter/vf_fade.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
>  2 files changed, 57 insertions(+), 6 deletions(-)
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 9fae339..cd52680 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -3809,7 +3809,8 @@ effect. Default is 0.
>  @item nb_frames, n
>  The number of frames for which the fade effect has to last. At the end of the
>  fade-in effect the output video will have the same intensity as the input video,
> -at the end of the fade-out transition the output video will be completely black.
> +at the end of the fade-out transition the output video will be filled with the
> +selected @option{color}.
>  Default is 25.
>  
>  @item alpha
> @@ -3824,8 +3825,12 @@ whichever comes last.  Default is 0.
>  @item duration, d
>  The number of seconds for which the fade effect has to last. At the end of the
>  fade-in effect the output video will have the same intensity as the input video,
> -at the end of the fade-out transition the output video will be completely black.
> +at the end of the fade-out transition the output video will be filled with the
> +selected @option{color}.
>  If both duration and nb_frames are specified, duration is used. Default is 0.
> +
> + at item color, c
> +Specify the color of the fade. Default is "black".
>  @end table
>  
>  @subsection Examples
> @@ -3856,9 +3861,9 @@ fade=in:0:25, fade=out:975:25
>  @end example
>  
>  @item
> -Make first 5 frames black, then fade in from frame 5-24:
> +Make first 5 frames yellow, then fade in from frame 5-24:
>  @example
> -fade=in:5:20
> +fade=in:5:20:color=yellow
>  @end example
>  
>  @item
> diff --git a/libavfilter/vf_fade.c b/libavfilter/vf_fade.c
> index 088bd65..3a90724 100644
> --- a/libavfilter/vf_fade.c
> +++ b/libavfilter/vf_fade.c
> @@ -61,6 +61,7 @@ typedef struct {
>      int alpha;
>      uint64_t start_time, duration;
>      enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
> +    uint8_t color_rgba[4];
>  } FadeContext;
>  
>  static av_cold int init(AVFilterContext *ctx)
> @@ -94,6 +95,7 @@ static av_cold int init(AVFilterContext *ctx)
>  
>  static int query_formats(AVFilterContext *ctx)
>  {
> +    const FadeContext *s = ctx->priv;
>      static const enum AVPixelFormat pix_fmts[] = {
>          AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
>          AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
> @@ -105,8 +107,17 @@ static int query_formats(AVFilterContext *ctx)
>          AV_PIX_FMT_RGBA,     AV_PIX_FMT_BGRA,
>          AV_PIX_FMT_NONE
>      };
> +    static const enum AVPixelFormat pix_fmts_rgb[] = {
> +        AV_PIX_FMT_RGB24,    AV_PIX_FMT_BGR24,
> +        AV_PIX_FMT_ARGB,     AV_PIX_FMT_ABGR,
> +        AV_PIX_FMT_RGBA,     AV_PIX_FMT_BGRA,
> +        AV_PIX_FMT_NONE
> +    };
>  

> -    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +    if (memcmp(s->color_rgba, "\x00\x00\x00\xff", 4))
> +        ff_set_common_formats(ctx, ff_make_format_list(pix_fmts_rgb));
> +    else
> +        ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));

Add a comment since this is not obvious.

   if (!memcmp(s->color_rgba, "\x00\x00\x00\xff", 4))
       ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); // black, default
   else
       ff_set_common_formats(ctx, ff_make_format_list(pix_fmts_rgb));

>      return 0;
>  }
>  
> @@ -138,6 +149,37 @@ static int config_props(AVFilterLink *inlink)
>      return 0;
>  }
>  

> +static int filter_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr,
> +                            int nb_jobs)

less confusing/more descriptive: filter_slice_rgba_interp?

> +{
> +    FadeContext *s = ctx->priv;
> +    AVFrame *frame = arg;
> +    int slice_start = (frame->height *  jobnr   ) / nb_jobs;
> +    int slice_end   = (frame->height * (jobnr+1)) / nb_jobs;
> +    int i, j;
> +

> +    const uint8_t r  = s->rgba_map[R];
> +    const uint8_t g  = s->rgba_map[G];
> +    const uint8_t b  = s->rgba_map[B];
> +    const uint8_t a  = s->rgba_map[A];
> +    const uint8_t *c = s->color_rgba;

nit: r/g/b/a -> ridx, gidx, etc...

> +
> +    for (i = slice_start; i < slice_end; i++) {
> +        uint8_t *p = frame->data[0] + i * frame->linesize[0];
> +        for (j = 0; j < frame->width; j++) {

> +#define INTERP(layer, value) av_clip_uint8(((c[value]<<16) + ((int)p[layer] - (int)c[value]) * s->factor + (1<<15)) >> 16)

nit: "value" is confusing, I suggest cidx

> +            p[r] = INTERP(r, 0);
> +            p[g] = INTERP(g, 1);
> +            p[b] = INTERP(b, 2);
> +            if (s->alpha)
> +                p[a] = INTERP(a, 3);
> +            p += s->bpp;
> +        }
> +    }
> +
> +    return 0;
> +}
> +
>  static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
>                               int nb_jobs)
>  {
> @@ -271,8 +313,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
>          if (s->alpha) {
>              ctx->internal->execute(ctx, filter_slice_alpha, frame, NULL,
>                                  FFMIN(frame->height, ctx->graph->nb_threads));

> +        } else if (s->is_packed_rgb) {
> +            ctx->internal->execute(ctx, filter_slice_rgb, frame, NULL,
> +                                   FFMIN(frame->height, ctx->graph->nb_threads));

seems partially unrelated, shouldn't you use the other routine
(possible faster if color=black?)

>          } else {
> -            /* luma or rgb plane */
>              ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
>                                  FFMIN(frame->height, ctx->graph->nb_threads));
>  
> @@ -315,6 +359,8 @@ static const AVOption fade_options[] = {
>                                                      OFFSET(duration),    AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
>      { "d",           "Duration of the effect in seconds.",
>                                                      OFFSET(duration),    AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
> +    { "color",       "set color",                   OFFSET(color_rgba),  AV_OPT_TYPE_COLOR,    {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
> +    { "c",           "set color",                   OFFSET(color_rgba),  AV_OPT_TYPE_COLOR,    {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
>      { NULL }
>  };
-- 
FFmpeg = Frightening Fundamental Maxi Portable Emblematic Genius


More information about the ffmpeg-devel mailing list