Browse code

lavfi: add fade filter

Port fade filter from libavfilter soc repo, with minor fixes by
Stefano.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Brandon Mintern authored on 2011/02/21 20:02:57
Showing 4 changed files
... ...
@@ -353,6 +353,39 @@ drawbox
353 353
 drawbox=10:20:200:60:red@@0.5"
354 354
 @end example
355 355
 
356
+@section fade
357
+
358
+Apply fade-in/out effect to input video.
359
+
360
+It accepts the parameters:
361
+@var{type}:@var{start_frame}:@var{nb_frames}
362
+
363
+@var{type} specifies if the effect type, can be either "in" for
364
+fade-in, or "out" for a fade-out effect.
365
+
366
+@var{start_frame} specifies the number of the start frame for starting
367
+to apply the fade effect.
368
+
369
+@var{nb_frames} specifies the number of frames for which the fade
370
+effect has to last. At the end of the fade-in effect the output video
371
+will have the same intensity as the input video, at the end of the
372
+fade-out transition the output video will be completely black.
373
+
374
+A few usage examples follow, usable too as test scenarios.
375
+@example
376
+# fade in first 30 frames of video
377
+fade=in:0:30
378
+
379
+# fade out last 45 frames of a 200-frame video
380
+fade=out:155:45
381
+
382
+# fade in first 25 frames and fade out last 25 frames of a 1000-frame video
383
+fade=in:0:25, fade=out:975:25
384
+
385
+# make first 5 frames black, then fade in from frame 5-24
386
+fade=in:5:20
387
+@end example
388
+
356 389
 @section fifo
357 390
 
358 391
 Buffer input images and send them when they are requested.
... ...
@@ -27,6 +27,7 @@ OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
27 27
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
28 28
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
29 29
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
30
+OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
30 31
 OBJS-$(CONFIG_FIFO_FILTER)                   += vf_fifo.o
31 32
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
32 33
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
... ...
@@ -45,6 +45,7 @@ void avfilter_register_all(void)
45 45
     REGISTER_FILTER (CROP,        crop,        vf);
46 46
     REGISTER_FILTER (CROPDETECT,  cropdetect,  vf);
47 47
     REGISTER_FILTER (DRAWBOX,     drawbox,     vf);
48
+    REGISTER_FILTER (FADE,        fade,        vf);
48 49
     REGISTER_FILTER (FIFO,        fifo,        vf);
49 50
     REGISTER_FILTER (FORMAT,      format,      vf);
50 51
     REGISTER_FILTER (FREI0R,      frei0r,      vf);
51 52
new file mode 100644
... ...
@@ -0,0 +1,170 @@
0
+/*
1
+ * Copyright (c) 2010 Brandon Mintern
2
+ * Copyright (c) 2007 Bobby Bingham
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * video fade filter
24
+ * based heavily on vf_negate.c by Bobby Bingham
25
+ */
26
+
27
+#include "libavutil/pixdesc.h"
28
+#include "avfilter.h"
29
+
30
+typedef struct {
31
+    int factor, fade_per_frame;
32
+    unsigned int frame_index, start_frame, stop_frame;
33
+    int hsub, vsub, bpp;
34
+} FadeContext;
35
+
36
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
37
+{
38
+    FadeContext *fade = ctx->priv;
39
+    unsigned int nb_frames;
40
+    char in_out[4];
41
+
42
+    if (!args ||
43
+        sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
44
+        av_log(ctx, AV_LOG_ERROR,
45
+               "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
46
+        return AVERROR(EINVAL);
47
+    }
48
+
49
+    nb_frames = nb_frames ? nb_frames : 1;
50
+    fade->fade_per_frame = (1 << 16) / nb_frames;
51
+    if (!strcmp(in_out, "in"))
52
+        fade->factor = 0;
53
+    else if (!strcmp(in_out, "out")) {
54
+        fade->fade_per_frame = -fade->fade_per_frame;
55
+        fade->factor = (1 << 16);
56
+    } else {
57
+        av_log(ctx, AV_LOG_ERROR,
58
+               "first argument must be 'in' or 'out':'%s'\n", in_out);
59
+        return AVERROR(EINVAL);
60
+    }
61
+    fade->stop_frame = fade->start_frame + nb_frames;
62
+
63
+    av_log(ctx, AV_LOG_INFO,
64
+           "type:%s start_frame:%d nb_frames:%d\n",
65
+           in_out, fade->start_frame, nb_frames);
66
+    return 0;
67
+}
68
+
69
+static int query_formats(AVFilterContext *ctx)
70
+{
71
+    const static enum PixelFormat pix_fmts[] = {
72
+        PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
73
+        PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
74
+        PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
75
+        PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
76
+        PIX_FMT_RGB24,    PIX_FMT_BGR24,
77
+        PIX_FMT_NONE
78
+    };
79
+
80
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
81
+    return 0;
82
+}
83
+
84
+static int config_props(AVFilterLink *inlink)
85
+{
86
+    FadeContext *fade = inlink->dst->priv;
87
+    const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
88
+
89
+    fade->hsub = pixdesc->log2_chroma_w;
90
+    fade->vsub = pixdesc->log2_chroma_h;
91
+
92
+    fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
93
+    return 0;
94
+}
95
+
96
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
97
+{
98
+    FadeContext *fade = inlink->dst->priv;
99
+    AVFilterBufferRef *outpic = inlink->cur_buf;
100
+    uint8_t *p;
101
+    int i, j, plane;
102
+
103
+    if (fade->factor < 65536) {
104
+        /* luma or rgb plane */
105
+        for (i = 0; i < h; i++) {
106
+            p = outpic->data[0] + (y+i) * outpic->linesize[0];
107
+            for (j = 0; j < inlink->w * fade->bpp; j++) {
108
+                /* fade->factor is using 16 lower-order bits for decimal
109
+                 * places. 32768 = 1 << 15, it is an integer representation
110
+                 * of 0.5 and is for rounding. */
111
+                *p = (*p * fade->factor + 32768) >> 16;
112
+                p++;
113
+            }
114
+        }
115
+
116
+        if (outpic->data[0] && outpic->data[1]) {
117
+            /* chroma planes */
118
+            for (plane = 1; plane < 3; plane++) {
119
+                for (i = 0; i < h; i++) {
120
+                    p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
121
+                    for (j = 0; j < inlink->w >> fade->hsub; j++) {
122
+                        /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
123
+                         * representation of 128.5. The .5 is for rounding
124
+                         * purposes. */
125
+                        *p = ((*p - 128) * fade->factor + 8421367) >> 16;
126
+                        p++;
127
+                    }
128
+                }
129
+            }
130
+        }
131
+    }
132
+
133
+    avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
134
+}
135
+
136
+static void end_frame(AVFilterLink *inlink)
137
+{
138
+    FadeContext *fade = inlink->dst->priv;
139
+
140
+    avfilter_end_frame(inlink->dst->outputs[0]);
141
+
142
+    if (fade->frame_index >= fade->start_frame &&
143
+        fade->frame_index <= fade->stop_frame)
144
+        fade->factor += fade->fade_per_frame;
145
+    fade->factor = av_clip_uint16(fade->factor);
146
+    fade->frame_index++;
147
+}
148
+
149
+AVFilter avfilter_vf_fade = {
150
+    .name          = "fade",
151
+    .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
152
+    .init          = init,
153
+    .priv_size     = sizeof(FadeContext),
154
+    .query_formats = query_formats,
155
+
156
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
157
+                                    .type            = AVMEDIA_TYPE_VIDEO,
158
+                                    .config_props    = config_props,
159
+                                    .get_video_buffer = avfilter_null_get_video_buffer,
160
+                                    .start_frame      = avfilter_null_start_frame,
161
+                                    .draw_slice      = draw_slice,
162
+                                    .end_frame       = end_frame,
163
+                                    .min_perms       = AV_PERM_READ | AV_PERM_WRITE,
164
+                                    .rej_perms       = AV_PERM_PRESERVE, },
165
+                                  { .name = NULL}},
166
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
167
+                                    .type            = AVMEDIA_TYPE_VIDEO, },
168
+                                  { .name = NULL}},
169
+};