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