]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fade.c
988cb42200e0dcbab8d11ef54546920f9488b80d
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/pixdesc.h"
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33
34 typedef struct {
35     int factor, fade_per_frame;
36     unsigned int frame_index, start_frame, stop_frame;
37     int hsub, vsub, bpp;
38 } FadeContext;
39
40 static av_cold int init(AVFilterContext *ctx, const char *args)
41 {
42     FadeContext *fade = ctx->priv;
43     unsigned int nb_frames;
44     char in_out[4];
45
46     if (!args ||
47         sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
48         av_log(ctx, AV_LOG_ERROR,
49                "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
50         return AVERROR(EINVAL);
51     }
52
53     nb_frames = nb_frames ? nb_frames : 1;
54     fade->fade_per_frame = (1 << 16) / nb_frames;
55     if (!strcmp(in_out, "in"))
56         fade->factor = 0;
57     else if (!strcmp(in_out, "out")) {
58         fade->fade_per_frame = -fade->fade_per_frame;
59         fade->factor = (1 << 16);
60     } else {
61         av_log(ctx, AV_LOG_ERROR,
62                "first argument must be 'in' or 'out':'%s'\n", in_out);
63         return AVERROR(EINVAL);
64     }
65     fade->stop_frame = fade->start_frame + nb_frames;
66
67     av_log(ctx, AV_LOG_INFO,
68            "type:%s start_frame:%d nb_frames:%d\n",
69            in_out, fade->start_frame, nb_frames);
70     return 0;
71 }
72
73 static int query_formats(AVFilterContext *ctx)
74 {
75     static const enum PixelFormat pix_fmts[] = {
76         PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
77         PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
78         PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
79         PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
80         PIX_FMT_RGB24,    PIX_FMT_BGR24,
81         PIX_FMT_NONE
82     };
83
84     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
85     return 0;
86 }
87
88 static int config_props(AVFilterLink *inlink)
89 {
90     FadeContext *fade = inlink->dst->priv;
91     const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
92
93     fade->hsub = pixdesc->log2_chroma_w;
94     fade->vsub = pixdesc->log2_chroma_h;
95
96     fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
97     return 0;
98 }
99
100 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
101 {
102     FadeContext *fade = inlink->dst->priv;
103     AVFilterBufferRef *outpic = inlink->cur_buf;
104     uint8_t *p;
105     int i, j, plane;
106
107     if (fade->factor < UINT16_MAX) {
108         /* luma or rgb plane */
109         for (i = 0; i < h; i++) {
110             p = outpic->data[0] + (y+i) * outpic->linesize[0];
111             for (j = 0; j < inlink->w * fade->bpp; j++) {
112                 /* fade->factor is using 16 lower-order bits for decimal
113                  * places. 32768 = 1 << 15, it is an integer representation
114                  * of 0.5 and is for rounding. */
115                 *p = (*p * fade->factor + 32768) >> 16;
116                 p++;
117             }
118         }
119
120         if (outpic->data[1] && outpic->data[2]) {
121             /* chroma planes */
122             for (plane = 1; plane < 3; plane++) {
123                 for (i = 0; i < h; i++) {
124                     p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
125                     for (j = 0; j < inlink->w >> fade->hsub; j++) {
126                         /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
127                          * representation of 128.5. The .5 is for rounding
128                          * purposes. */
129                         *p = ((*p - 128) * fade->factor + 8421367) >> 16;
130                         p++;
131                     }
132                 }
133             }
134         }
135     }
136
137     ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
138 }
139
140 static void end_frame(AVFilterLink *inlink)
141 {
142     FadeContext *fade = inlink->dst->priv;
143
144     ff_end_frame(inlink->dst->outputs[0]);
145
146     if (fade->frame_index >= fade->start_frame &&
147         fade->frame_index <= fade->stop_frame)
148         fade->factor += fade->fade_per_frame;
149     fade->factor = av_clip_uint16(fade->factor);
150     fade->frame_index++;
151 }
152
153 AVFilter avfilter_vf_fade = {
154     .name          = "fade",
155     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
156     .init          = init,
157     .priv_size     = sizeof(FadeContext),
158     .query_formats = query_formats,
159
160     .inputs    = (AVFilterPad[]) {{ .name            = "default",
161                                     .type            = AVMEDIA_TYPE_VIDEO,
162                                     .config_props    = config_props,
163                                     .get_video_buffer = ff_null_get_video_buffer,
164                                     .start_frame      = ff_null_start_frame,
165                                     .draw_slice      = draw_slice,
166                                     .end_frame       = end_frame,
167                                     .min_perms       = AV_PERM_READ | AV_PERM_WRITE,
168                                     .rej_perms       = AV_PERM_PRESERVE, },
169                                   { .name = NULL}},
170     .outputs   = (AVFilterPad[]) {{ .name            = "default",
171                                     .type            = AVMEDIA_TYPE_VIDEO, },
172                                   { .name = NULL}},
173 };