mp3: Properly use AVCodecContext API
[platform/upstream/libav.git] / 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/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 #define FADE_IN  0
37 #define FADE_OUT 1
38
39 typedef struct FadeContext {
40     const AVClass *class;
41     int type;
42     int factor, fade_per_frame;
43     int start_frame, nb_frames;
44     unsigned int frame_index, stop_frame;
45     int hsub, vsub, bpp;
46 } FadeContext;
47
48 static av_cold int init(AVFilterContext *ctx)
49 {
50     FadeContext *s = ctx->priv;
51
52     s->fade_per_frame = (1 << 16) / s->nb_frames;
53     if (s->type == FADE_IN) {
54         s->factor = 0;
55     } else if (s->type == FADE_OUT) {
56         s->fade_per_frame = -s->fade_per_frame;
57         s->factor = (1 << 16);
58     }
59     s->stop_frame = s->start_frame + s->nb_frames;
60
61     av_log(ctx, AV_LOG_VERBOSE,
62            "type:%s start_frame:%d nb_frames:%d\n",
63            s->type == FADE_IN ? "in" : "out", s->start_frame,
64            s->nb_frames);
65     return 0;
66 }
67
68 static int query_formats(AVFilterContext *ctx)
69 {
70     static const enum AVPixelFormat pix_fmts[] = {
71         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
72         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
73         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
74         AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUVJ440P,
75         AV_PIX_FMT_RGB24,    AV_PIX_FMT_BGR24,
76         AV_PIX_FMT_NONE
77     };
78
79     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
80     return 0;
81 }
82
83 static int config_props(AVFilterLink *inlink)
84 {
85     FadeContext *s = inlink->dst->priv;
86     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
87
88     s->hsub = pixdesc->log2_chroma_w;
89     s->vsub = pixdesc->log2_chroma_h;
90
91     s->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
92     return 0;
93 }
94
95 static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
96                              int nb_jobs)
97 {
98     FadeContext *s = ctx->priv;
99     AVFrame *frame = arg;
100     int slice_h     = frame->height / nb_jobs;
101     int slice_start = jobnr * slice_h;
102     int slice_end   = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h;
103     int i, j;
104
105     for (i = slice_start; i < slice_end; i++) {
106         uint8_t *p = frame->data[0] + i * frame->linesize[0];
107         for (j = 0; j < frame->width * s->bpp; j++) {
108             /* s->factor is using 16 lower-order bits for decimal
109              * places. 32768 = 1 << 15, it is an integer representation
110              * of 0.5 and is for rounding. */
111             *p = (*p * s->factor + 32768) >> 16;
112             p++;
113         }
114     }
115
116     return 0;
117 }
118
119 static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
120                                int nb_jobs)
121 {
122     FadeContext *s = ctx->priv;
123     AVFrame *frame = arg;
124     int slice_h     = FFALIGN(frame->height / nb_jobs, 1 << s->vsub);
125     int slice_start = jobnr * slice_h;
126     int slice_end   = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h;
127     int i, j, plane;
128
129     for (plane = 1; plane < 3; plane++) {
130         for (i = slice_start; i < slice_end; i++) {
131             uint8_t *p = frame->data[plane] + (i >> s->vsub) * frame->linesize[plane];
132             for (j = 0; j < frame->width >> s->hsub; j++) {
133                 /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
134                  * representation of 128.5. The .5 is for rounding
135                  * purposes. */
136                 *p = ((*p - 128) * s->factor + 8421367) >> 16;
137                 p++;
138             }
139         }
140     }
141
142     return 0;
143 }
144
145 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
146 {
147     AVFilterContext *ctx = inlink->dst;
148     FadeContext *s       = ctx->priv;
149
150     if (s->factor < UINT16_MAX) {
151         /* luma or rgb plane */
152         ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
153                                FFMIN(frame->height, ctx->graph->nb_threads));
154
155         if (frame->data[1] && frame->data[2]) {
156             /* chroma planes */
157             ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
158                                    FFMIN(frame->height, ctx->graph->nb_threads));
159         }
160     }
161
162     if (s->frame_index >= s->start_frame &&
163         s->frame_index <= s->stop_frame)
164         s->factor += s->fade_per_frame;
165     s->factor = av_clip_uint16(s->factor);
166     s->frame_index++;
167
168     return ff_filter_frame(inlink->dst->outputs[0], frame);
169 }
170
171 #define OFFSET(x) offsetof(FadeContext, x)
172 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
173 static const AVOption options[] = {
174     { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
175         { "in",  "fade-in",  0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN },  .unit = "type" },
176         { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
177     { "start_frame", "Number of the first frame to which to apply the effect.",
178                                                     OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
179     { "nb_frames",   "Number of frames to which the effect should be applied.",
180                                                     OFFSET(nb_frames),   AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
181     { NULL },
182 };
183
184 static const AVClass fade_class = {
185     .class_name = "fade",
186     .item_name  = av_default_item_name,
187     .option     = options,
188     .version    = LIBAVUTIL_VERSION_INT,
189 };
190
191 static const AVFilterPad avfilter_vf_fade_inputs[] = {
192     {
193         .name             = "default",
194         .type             = AVMEDIA_TYPE_VIDEO,
195         .config_props     = config_props,
196         .get_video_buffer = ff_null_get_video_buffer,
197         .filter_frame     = filter_frame,
198         .needs_writable   = 1,
199     },
200     { NULL }
201 };
202
203 static const AVFilterPad avfilter_vf_fade_outputs[] = {
204     {
205         .name = "default",
206         .type = AVMEDIA_TYPE_VIDEO,
207     },
208     { NULL }
209 };
210
211 AVFilter ff_vf_fade = {
212     .name          = "fade",
213     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
214     .init          = init,
215     .priv_size     = sizeof(FadeContext),
216     .priv_class    = &fade_class,
217     .query_formats = query_formats,
218
219     .inputs    = avfilter_vf_fade_inputs,
220     .outputs   = avfilter_vf_fade_outputs,
221     .flags     = AVFILTER_FLAG_SLICE_THREADS,
222 };