2 * Copyright (c) 2010 Brandon Mintern
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
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.
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.
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
25 * based heavily on vf_negate.c by Bobby Bingham
28 #include "libavutil/pixdesc.h"
33 int factor, fade_per_frame;
34 unsigned int frame_index, start_frame, stop_frame;
38 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
40 FadeContext *fade = ctx->priv;
41 unsigned int nb_frames;
45 sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
46 av_log(ctx, AV_LOG_ERROR,
47 "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
48 return AVERROR(EINVAL);
51 nb_frames = nb_frames ? nb_frames : 1;
52 fade->fade_per_frame = (1 << 16) / nb_frames;
53 if (!strcmp(in_out, "in"))
55 else if (!strcmp(in_out, "out")) {
56 fade->fade_per_frame = -fade->fade_per_frame;
57 fade->factor = (1 << 16);
59 av_log(ctx, AV_LOG_ERROR,
60 "first argument must be 'in' or 'out':'%s'\n", in_out);
61 return AVERROR(EINVAL);
63 fade->stop_frame = fade->start_frame + nb_frames;
65 av_log(ctx, AV_LOG_INFO,
66 "type:%s start_frame:%d nb_frames:%d\n",
67 in_out, fade->start_frame, nb_frames);
71 static int query_formats(AVFilterContext *ctx)
73 static const enum PixelFormat pix_fmts[] = {
74 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
75 PIX_FMT_YUV411P, PIX_FMT_YUV410P,
76 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
77 PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
78 PIX_FMT_RGB24, PIX_FMT_BGR24,
82 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
86 static int config_props(AVFilterLink *inlink)
88 FadeContext *fade = inlink->dst->priv;
89 const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
91 fade->hsub = pixdesc->log2_chroma_w;
92 fade->vsub = pixdesc->log2_chroma_h;
94 fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
98 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
100 FadeContext *fade = inlink->dst->priv;
101 AVFilterBufferRef *outpic = inlink->cur_buf;
105 if (fade->factor < UINT16_MAX) {
106 /* luma or rgb plane */
107 for (i = 0; i < h; i++) {
108 p = outpic->data[0] + (y+i) * outpic->linesize[0];
109 for (j = 0; j < inlink->w * fade->bpp; j++) {
110 /* fade->factor is using 16 lower-order bits for decimal
111 * places. 32768 = 1 << 15, it is an integer representation
112 * of 0.5 and is for rounding. */
113 *p = (*p * fade->factor + 32768) >> 16;
118 if (outpic->data[1] && outpic->data[2]) {
120 for (plane = 1; plane < 3; plane++) {
121 for (i = 0; i < h; i++) {
122 p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
123 for (j = 0; j < inlink->w >> fade->hsub; j++) {
124 /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
125 * representation of 128.5. The .5 is for rounding
127 *p = ((*p - 128) * fade->factor + 8421367) >> 16;
135 avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
138 static void end_frame(AVFilterLink *inlink)
140 FadeContext *fade = inlink->dst->priv;
142 avfilter_end_frame(inlink->dst->outputs[0]);
144 if (fade->frame_index >= fade->start_frame &&
145 fade->frame_index <= fade->stop_frame)
146 fade->factor += fade->fade_per_frame;
147 fade->factor = av_clip_uint16(fade->factor);
151 AVFilter avfilter_vf_fade = {
153 .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
155 .priv_size = sizeof(FadeContext),
156 .query_formats = query_formats,
158 .inputs = (AVFilterPad[]) {{ .name = "default",
159 .type = AVMEDIA_TYPE_VIDEO,
160 .config_props = config_props,
161 .get_video_buffer = ff_null_get_video_buffer,
162 .start_frame = ff_null_start_frame,
163 .draw_slice = draw_slice,
164 .end_frame = end_frame,
165 .min_perms = AV_PERM_READ | AV_PERM_WRITE,
166 .rej_perms = AV_PERM_PRESERVE, },
168 .outputs = (AVFilterPad[]) {{ .name = "default",
169 .type = AVMEDIA_TYPE_VIDEO, },