]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fade.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_fade.c
1 /*
2  * Copyright (c) 2010 Brandon Mintern
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * video fade filter
25  * based heavily on vf_negate.c by Bobby Bingham
26  */
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "avfilter.h"
34 #include "drawutils.h"
35 #include "internal.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39
40 #define R 0
41 #define G 1
42 #define B 2
43 #define A 3
44
45 #define Y 0
46 #define U 1
47 #define V 2
48
49 typedef struct {
50     const AVClass *class;
51     int factor, fade_per_frame;
52     unsigned int frame_index, start_frame, stop_frame, nb_frames;
53     int hsub, vsub, bpp;
54     unsigned int black_level, black_level_scaled;
55     uint8_t is_packed_rgb;
56     uint8_t rgba_map[4];
57     int alpha;
58
59     char *type;
60 } FadeContext;
61
62 #define OFFSET(x) offsetof(FadeContext, x)
63
64 static const AVOption fade_options[] = {
65     { "type",        "set the fade direction",                     OFFSET(type),        AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX },
66     { "t",           "set the fade direction",                     OFFSET(type),        AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX },
67     { "start_frame", "set expression of frame to start fading",    OFFSET(start_frame), AV_OPT_TYPE_INT, {.dbl = 0    }, 0, INT_MAX },
68     { "s",           "set expression of frame to start fading",    OFFSET(start_frame), AV_OPT_TYPE_INT, {.dbl = 0    }, 0, INT_MAX },
69     { "nb_frames",   "set expression for fade duration in frames", OFFSET(nb_frames),   AV_OPT_TYPE_INT, {.dbl = 25   }, 0, INT_MAX },
70     { "n",           "set expression for fade duration in frames", OFFSET(nb_frames),   AV_OPT_TYPE_INT, {.dbl = 25   }, 0, INT_MAX },
71     { "alpha",       "fade alpha if it is available on the input", OFFSET(alpha),       AV_OPT_TYPE_INT, {.dbl = 0    }, 0,       1 },
72     {NULL},
73 };
74
75 AVFILTER_DEFINE_CLASS(fade);
76
77 static av_cold int init(AVFilterContext *ctx, const char *args)
78 {
79     FadeContext *fade = ctx->priv;
80     int ret = 0;
81     char *args1, *expr, *bufptr = NULL;
82
83     fade->class = &fade_class;
84     av_opt_set_defaults(fade);
85
86     if (!(args1 = av_strdup(args))) {
87         ret = AVERROR(ENOMEM);
88         goto end;
89     }
90
91     if (expr = av_strtok(args1, ":", &bufptr)) {
92         av_free(fade->type);
93         if (!(fade->type = av_strdup(expr))) {
94             ret = AVERROR(ENOMEM);
95             goto end;
96         }
97     }
98     if (expr = av_strtok(NULL, ":", &bufptr)) {
99         if ((ret = av_opt_set(fade, "start_frame", expr, 0)) < 0) {
100             av_log(ctx, AV_LOG_ERROR,
101                    "Invalid value '%s' for start_frame option\n", expr);
102             return ret;
103         }
104     }
105     if (expr = av_strtok(NULL, ":", &bufptr)) {
106         if ((ret = av_opt_set(fade, "nb_frames", expr, 0)) < 0) {
107             av_log(ctx, AV_LOG_ERROR,
108                    "Invalid value '%s' for nb_frames option\n", expr);
109             return ret;
110         }
111     }
112
113     if (bufptr && (ret = av_set_options_string(fade, bufptr, "=", ":")) < 0)
114         goto end;
115
116     fade->fade_per_frame = (1 << 16) / fade->nb_frames;
117     if (!strcmp(fade->type, "in"))
118         fade->factor = 0;
119     else if (!strcmp(fade->type, "out")) {
120         fade->fade_per_frame = -fade->fade_per_frame;
121         fade->factor = (1 << 16);
122     } else {
123         av_log(ctx, AV_LOG_ERROR,
124                "Type argument must be 'in' or 'out' but '%s' was specified\n", fade->type);
125         ret = AVERROR(EINVAL);
126         goto end;
127     }
128     fade->stop_frame = fade->start_frame + fade->nb_frames;
129
130     av_log(ctx, AV_LOG_VERBOSE,
131            "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
132            fade->type, fade->start_frame, fade->nb_frames, fade->alpha);
133
134 end:
135     av_free(args1);
136     return ret;
137 }
138
139 static av_cold void uninit(AVFilterContext *ctx)
140 {
141     FadeContext *fade = ctx->priv;
142
143     av_freep(&fade->type);
144 }
145
146 static int query_formats(AVFilterContext *ctx)
147 {
148     static const enum PixelFormat pix_fmts[] = {
149         PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
150         PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
151         PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
152         PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
153         PIX_FMT_YUVA420P,
154         PIX_FMT_RGB24,    PIX_FMT_BGR24,
155         PIX_FMT_ARGB,     PIX_FMT_ABGR,
156         PIX_FMT_RGBA,     PIX_FMT_BGRA,
157         PIX_FMT_NONE
158     };
159
160     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
161     return 0;
162 }
163
164 const static enum PixelFormat studio_level_pix_fmts[] = {
165     PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
166     PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
167     PIX_FMT_YUV440P,
168     PIX_FMT_NONE
169 };
170
171 static enum PixelFormat alpha_pix_fmts[] = {
172     PIX_FMT_YUVA420P,
173     PIX_FMT_ARGB, PIX_FMT_ABGR,
174     PIX_FMT_RGBA, PIX_FMT_BGRA,
175     PIX_FMT_NONE
176 };
177
178 static int config_props(AVFilterLink *inlink)
179 {
180     FadeContext *fade = inlink->dst->priv;
181     const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
182
183     fade->hsub = pixdesc->log2_chroma_w;
184     fade->vsub = pixdesc->log2_chroma_h;
185
186     fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
187     fade->alpha = fade->alpha ? ff_fmt_is_in(inlink->format, alpha_pix_fmts) : 0;
188     fade->is_packed_rgb = ff_fill_rgba_map(fade->rgba_map, inlink->format) >= 0;
189
190     /* use CCIR601/709 black level for studio-level pixel non-alpha components */
191     fade->black_level =
192             ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !fade->alpha ? 16 : 0;
193     /* 32768 = 1 << 15, it is an integer representation
194      * of 0.5 and is for rounding. */
195     fade->black_level_scaled = (fade->black_level << 16) + 32768;
196     return 0;
197 }
198
199 static void fade_plane(int y, int h, int w,
200                        int fade_factor, int black_level, int black_level_scaled,
201                        uint8_t offset, uint8_t step, int bytes_per_plane,
202                        uint8_t *data, int line_size)
203 {
204     uint8_t *p;
205     int i, j;
206
207     /* luma, alpha or rgb plane */
208     for (i = 0; i < h; i++) {
209         p = data + offset + (y+i) * line_size;
210         for (j = 0; j < w * bytes_per_plane; j++) {
211             /* fade->factor is using 16 lower-order bits for decimal places. */
212             *p = ((*p - black_level) * fade_factor + black_level_scaled) >> 16;
213             p+=step;
214         }
215     }
216 }
217
218 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
219 {
220     FadeContext *fade = inlink->dst->priv;
221     AVFilterBufferRef *outpic = inlink->cur_buf;
222     uint8_t *p;
223     int i, j, plane;
224
225     if (fade->factor < UINT16_MAX) {
226         if (fade->alpha) {
227             // alpha only
228             plane = fade->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats
229                                                  // or plane 3 for planar formats
230             fade_plane(y, h, inlink->w,
231                        fade->factor, fade->black_level, fade->black_level_scaled,
232                        fade->is_packed_rgb ? fade->rgba_map[A] : 0, // alpha offset for packed formats
233                        fade->is_packed_rgb ? 4 : 1,                 // pixstep for 8 bit packed formats
234                        1, outpic->data[plane], outpic->linesize[plane]);
235         } else {
236             /* luma or rgb plane */
237             fade_plane(y, h, inlink->w,
238                        fade->factor, fade->black_level, fade->black_level_scaled,
239                        0, 1, // offset & pixstep for Y plane or RGB packed format
240                        fade->bpp, outpic->data[0], outpic->linesize[0]);
241             if (outpic->data[1] && outpic->data[2]) {
242                 /* chroma planes */
243                 for (plane = 1; plane < 3; plane++) {
244                     for (i = 0; i < h; i++) {
245                         p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
246                         for (j = 0; j < inlink->w >> fade->hsub; j++) {
247                             /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
248                              * representation of 128.5. The .5 is for rounding
249                              * purposes. */
250                             *p = ((*p - 128) * fade->factor + 8421367) >> 16;
251                             p++;
252                         }
253                     }
254                 }
255             }
256         }
257     }
258
259     return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
260 }
261
262 static int end_frame(AVFilterLink *inlink)
263 {
264     FadeContext *fade = inlink->dst->priv;
265     int ret;
266
267     ret = ff_end_frame(inlink->dst->outputs[0]);
268
269     if (fade->frame_index >= fade->start_frame &&
270         fade->frame_index <= fade->stop_frame)
271         fade->factor += fade->fade_per_frame;
272     fade->factor = av_clip_uint16(fade->factor);
273     fade->frame_index++;
274
275     return ret;
276 }
277
278 AVFilter avfilter_vf_fade = {
279     .name          = "fade",
280     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
281     .init          = init,
282     .uninit        = uninit,
283     .priv_size     = sizeof(FadeContext),
284     .query_formats = query_formats,
285
286     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
287                                           .type            = AVMEDIA_TYPE_VIDEO,
288                                           .config_props    = config_props,
289                                           .get_video_buffer = ff_null_get_video_buffer,
290                                           .start_frame      = ff_null_start_frame,
291                                           .draw_slice      = draw_slice,
292                                           .end_frame       = end_frame,
293                                           .min_perms       = AV_PERM_READ | AV_PERM_WRITE,
294                                           .rej_perms       = AV_PERM_PRESERVE, },
295                                         { .name = NULL}},
296     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
297                                           .type            = AVMEDIA_TYPE_VIDEO, },
298                                         { .name = NULL}},
299 };