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

Stefano Sabatini stefasab at gmail.com
Sun Nov 10 12:29:22 CET 2013


On date Saturday 2013-11-09 21:04:58 +0100, Clément Bœsch encoded:
> On Sat, Nov 09, 2013 at 07:47:19PM +0100, Stefano Sabatini wrote:
[...]
> New patch attached.
> 
> -- 
> Clément B.

> From 863c7f6925e2580a387209afe87cd9c3748d12c2 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <u at pkh.me>
> Date: Fri, 8 Nov 2013 23:59:40 +0100
> Subject: [PATCH] avfilter/fade: add color option.
> 
> Fixes Ticket #1822.
> ---
>  doc/filters.texi      | 13 +++++++----
>  libavfilter/vf_fade.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++--
>  2 files changed, 71 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..3b54d6d 100644
> --- a/libavfilter/vf_fade.c
> +++ b/libavfilter/vf_fade.c
> @@ -25,6 +25,7 @@
>   * based heavily on vf_negate.c by Bobby Bingham
>   */
>  
> +#include "libavutil/avassert.h"
>  #include "libavutil/avstring.h"
>  #include "libavutil/common.h"
>  #include "libavutil/eval.h"
> @@ -61,6 +62,8 @@ 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];  ///< fade color
> +    int black_fade;         ///< if color_rgba is black
>  } FadeContext;
>  
>  static av_cold int init(AVFilterContext *ctx)
> @@ -89,11 +92,13 @@ static av_cold int init(AVFilterContext *ctx)
>                 (s->duration / (double)AV_TIME_BASE),s->alpha);
>      }
>  
> +    s->black_fade = !memcmp(s->color_rgba, "\x00\x00\x00\xff", 4);
>      return 0;
>  }
>  
>  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 +110,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 (s->black_fade)
> +        ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +    else
> +        ff_set_common_formats(ctx, ff_make_format_list(pix_fmts_rgb));
>      return 0;
>  }
>  
> @@ -138,6 +152,47 @@ static int config_props(AVFilterLink *inlink)
>      return 0;
>  }
>  
> +static av_always_inline void filter_rgb(FadeContext *s, const AVFrame *frame,
> +                                        int slice_start, int slice_end,
> +                                        int do_alpha, int step)
> +{
> +    int i, j;
> +    const uint8_t r_idx  = s->rgba_map[R];
> +    const uint8_t g_idx  = s->rgba_map[G];
> +    const uint8_t b_idx  = s->rgba_map[B];
> +    const uint8_t a_idx  = s->rgba_map[A];
> +    const uint8_t *c = s->color_rgba;
> +
> +    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(cname, cidx) av_clip_uint8(((c[cidx]<<16) + ((int)p[cname] - (int)c[cidx]) * s->factor + (1<<15)) >> 16)

nittt: c_idx (or ridx, gidx, ... for consistency)

> +            p[r_idx] = INTERP(r_idx, 0);
> +            p[g_idx] = INTERP(g_idx, 1);
> +            p[b_idx] = INTERP(b_idx, 2);
> +            if (do_alpha)
> +                p[a_idx] = INTERP(a_idx, 3);
> +            p += step;
> +        }
> +    }
> +}
> +
> +static int filter_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr,
> +                            int nb_jobs)
> +{
> +    FadeContext *s = ctx->priv;
> +    AVFrame *frame = arg;
> +    int slice_start = (frame->height *  jobnr   ) / nb_jobs;
> +    int slice_end   = (frame->height * (jobnr+1)) / nb_jobs;
> +
> +    if      (s->alpha)    filter_rgb(s, frame, slice_start, slice_end, 1, 4);
> +    else if (s->bpp == 3) filter_rgb(s, frame, slice_start, slice_end, 0, 3);
> +    else if (s->bpp == 4) filter_rgb(s, frame, slice_start, slice_end, 0, 4);
> +    else                  av_assert0(0);
> +
> +    return 0;
> +}
> +
>  static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
>                               int nb_jobs)
>  {
> @@ -271,8 +326,11 @@ 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 && !s->black_fade) {
> +            ctx->internal->execute(ctx, filter_slice_rgb, frame, NULL,
> +                                   FFMIN(frame->height, ctx->graph->nb_threads));
>          } else {
> -            /* luma or rgb plane */
> +            /* luma, or rgb plane in case of black */
>              ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
>                                  FFMIN(frame->height, ctx->graph->nb_threads));
>  
> @@ -315,6 +373,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 }

LGTM, thanks.
-- 
FFmpeg = Fabulous and Faithful Merciless Pure Experimenting Gadget


More information about the ffmpeg-devel mailing list