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