]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tinterlace.c
lavfi: port tinterlace filter from MPlayer
[ffmpeg] / libavfilter / vf_tinterlace.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with FFmpeg if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * @file
25  * temporal field interlace filter, ported from MPlayer/libmpcodecs
26  */
27
28 #include "libavutil/imgutils.h"
29 #include "avfilter.h"
30 #include "internal.h"
31
32 typedef struct {
33     int mode;                   ///< interlace mode selected
34     int frame;                  ///< number of the output frame
35     int vsub;                   ///< chroma vertical subsampling
36     AVFilterBufferRef *cur;
37     AVFilterBufferRef *next;
38     uint8_t *black_data[4];     ///< buffer used to fill padded lines
39     int black_linesize[4];
40 } TInterlaceContext;
41
42 #define FULL_SCALE_YUVJ_FORMATS \
43     PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
44
45 static enum PixelFormat full_scale_yuvj_pix_fmts[] = {
46     FULL_SCALE_YUVJ_FORMATS, PIX_FMT_NONE
47 };
48
49 static int query_formats(AVFilterContext *ctx)
50 {
51     static const enum PixelFormat pix_fmts[] = {
52         PIX_FMT_YUV420P,  PIX_FMT_YUV422P,  PIX_FMT_YUV444P,
53         PIX_FMT_YUV444P,  PIX_FMT_YUV410P,  PIX_FMT_YUVA420P,
54         PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
55         PIX_FMT_NONE
56     };
57
58     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
59     return 0;
60 }
61
62 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
63 {
64     TInterlaceContext *tinterlace = ctx->priv;
65     int n;
66     tinterlace->mode = 0;
67
68     if (args) {
69         n = sscanf(args, "%d", &tinterlace->mode);
70
71         if (n != 1 || tinterlace->mode < 0 || tinterlace->mode > 4) {
72             av_log(ctx, AV_LOG_ERROR,
73                    "Invalid mode '%s', use an integer between 0 and 4\n", args);
74             return AVERROR(EINVAL);
75         }
76     }
77
78     return 0;
79 }
80
81 static av_cold void uninit(AVFilterContext *ctx)
82 {
83     TInterlaceContext *tinterlace = ctx->priv;
84
85     if (tinterlace->cur ) avfilter_unref_buffer(tinterlace->cur );
86     if (tinterlace->next) avfilter_unref_buffer(tinterlace->next);
87
88     av_freep(&tinterlace->black_data[0]);
89 }
90
91 static int config_out_props(AVFilterLink *outlink)
92 {
93     AVFilterContext *ctx = outlink->src;
94     AVFilterLink *inlink = outlink->src->inputs[0];
95     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[outlink->format];
96     TInterlaceContext *tinterlace = ctx->priv;
97
98     tinterlace->vsub = desc->log2_chroma_h;
99     outlink->w = inlink->w;
100     outlink->h = tinterlace->mode == 0 || tinterlace->mode == 3 ?
101         inlink->h*2 : inlink->h;
102
103     if (tinterlace->mode == 3) {
104         uint8_t black[4] = { 16, 128, 128, 16 };
105         int i, ret;
106         if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
107             black[0] = black[3] = 0;
108         ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
109                              outlink->w, outlink->h, outlink->format, 1);
110         if (ret < 0)
111             return ret;
112
113         /* fill black picture with black */
114         for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
115             int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
116             memset(tinterlace->black_data[i], black[i],
117                    tinterlace->black_linesize[i] * h);
118         }
119     }
120     av_log(ctx, AV_LOG_INFO, "mode:%d h:%d -> h:%d\n",
121            tinterlace->mode, inlink->h, outlink->h);
122
123     return 0;
124 }
125
126 #define FIELD_UPPER           0
127 #define FIELD_LOWER           1
128 #define FIELD_UPPER_AND_LOWER 2
129
130 /**
131  * Copy picture field from src to dst.
132  *
133  * @param src_field copy from upper, lower field or both
134  * @param interleave leave a padding line between each copied field
135  * @param dst_field copy to upper or lower field,
136  *        only meaningful when interleave is selected
137  */
138 static inline
139 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
140                         uint8_t *src[4], int src_linesize[4],
141                         enum PixelFormat format, int w, int src_h,
142                         int src_field, int interleave, int dst_field)
143 {
144     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
145     int plane, vsub = desc->log2_chroma_h;
146     int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
147
148     for (plane = 0; plane < desc->nb_components; plane++) {
149         int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
150         int linesize = av_image_get_linesize(format, w, plane);
151         uint8_t *dstp = dst[plane];
152         uint8_t *srcp = src[plane];
153         lines /= k;
154         if (src_field == FIELD_LOWER)
155             srcp += src_linesize[plane];
156         if (interleave && dst_field == FIELD_LOWER)
157             dstp += dst_linesize[plane];
158         av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
159                             srcp, src_linesize[plane]*k, linesize, lines);
160     }
161 }
162
163 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
164 {
165     AVFilterContext *ctx = inlink->dst;
166     TInterlaceContext *tinterlace = ctx->priv;
167
168     if (tinterlace->cur)
169         avfilter_unref_buffer(tinterlace->cur);
170     tinterlace->cur  = tinterlace->next;
171     tinterlace->next = picref;
172 }
173
174 static void end_frame(AVFilterLink *inlink)
175 {
176     AVFilterContext *ctx = inlink->dst;
177     AVFilterLink *outlink = ctx->outputs[0];
178     TInterlaceContext *tinterlace = ctx->priv;
179     AVFilterBufferRef *cur  = tinterlace->cur;
180     AVFilterBufferRef *next = tinterlace->next;
181     AVFilterBufferRef *out  = NULL;
182     int field;
183
184     /* we need at least two frames */
185     if (!tinterlace->cur)
186         return;
187
188     switch (tinterlace->mode) {
189     case 0: /* move the odd frame into the upper field of the new image, even into
190              * the lower field, generating a double-height video at half framerate */
191         out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
192         avfilter_copy_buffer_ref_props(out, cur);
193         out->video->h = outlink->h;
194         out->video->interlaced = 1;
195         out->video->top_field_first = 1;
196
197         /* write odd frame lines into the upper field of the new frame */
198         copy_picture_field(out->data, out->linesize,
199                            cur->data, cur->linesize,
200                            inlink->format, inlink->w, inlink->h,
201                            FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
202         /* write even frame lines into the lower field of the new frame */
203         copy_picture_field(out->data, out->linesize,
204                            next->data, next->linesize,
205                            inlink->format, inlink->w, inlink->h,
206                            FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
207         avfilter_unref_buffer(tinterlace->next);
208         tinterlace->next = NULL;
209         break;
210
211     case 1: /* only output even frames, odd  frames are dropped; height unchanged, half framerate */
212     case 2: /* only output odd  frames, even frames are dropped; height unchanged, half framerate */
213         out = avfilter_ref_buffer(tinterlace->mode == 2 ? cur : next, AV_PERM_READ);
214         avfilter_unref_buffer(tinterlace->next);
215         tinterlace->next = NULL;
216         break;
217
218     case 3: /* expand each frame to double height, but pad alternate
219              * lines with black; framerate unchanged */
220         out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
221         avfilter_copy_buffer_ref_props(out, cur);
222         out->video->h = outlink->h;
223
224         field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
225         /* copy upper and lower fields */
226         copy_picture_field(out->data, out->linesize,
227                            cur->data, cur->linesize,
228                            inlink->format, inlink->w, inlink->h,
229                            FIELD_UPPER_AND_LOWER, 1, field);
230         /* pad with black the other field */
231         copy_picture_field(out->data, out->linesize,
232                            tinterlace->black_data, tinterlace->black_linesize,
233                            inlink->format, inlink->w, inlink->h,
234                            FIELD_UPPER_AND_LOWER, 1, !field);
235         break;
236
237     case 4: /* interleave upper lines from odd frames with lower lines from even frames,
238              * halving the frame rate and preserving image height */
239         out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
240         avfilter_copy_buffer_ref_props(out, cur);
241         out->video->interlaced = 1;
242         out->video->top_field_first = 1;
243
244         /* copy upper field from cur */
245         copy_picture_field(out->data, out->linesize,
246                            cur->data, cur->linesize,
247                            inlink->format, inlink->w, inlink->h,
248                            FIELD_UPPER, 1, FIELD_UPPER);
249         /* copy lower fields from next */
250         copy_picture_field(out->data, out->linesize,
251                            next->data, next->linesize,
252                            inlink->format, inlink->w, inlink->h,
253                            FIELD_LOWER, 1, FIELD_LOWER);
254         avfilter_unref_buffer(tinterlace->next);
255         tinterlace->next = NULL;
256         break;
257     }
258
259     avfilter_start_frame(outlink, out);
260     avfilter_draw_slice(outlink, 0, outlink->h, 1);
261     avfilter_end_frame(outlink);
262
263     tinterlace->frame++;
264 }
265
266 static int poll_frame(AVFilterLink *outlink)
267 {
268     TInterlaceContext *tinterlace = outlink->src->priv;
269     AVFilterLink *inlink = outlink->src->inputs[0];
270     int ret, val;
271
272     val = avfilter_poll_frame(inlink);
273
274     if (val == 1 && !tinterlace->next) {
275         if ((ret = avfilter_request_frame(inlink)) < 0)
276             return ret;
277         val = avfilter_poll_frame(inlink);
278     }
279     assert(tinterlace->next);
280
281     return val;
282 }
283
284 static int request_frame(AVFilterLink *outlink)
285 {
286     TInterlaceContext *tinterlace = outlink->src->priv;
287     AVFilterLink *inlink = outlink->src->inputs[0];
288
289     do {
290         int ret;
291
292         if ((ret = avfilter_request_frame(inlink)) < 0)
293             return ret;
294     } while (!tinterlace->cur);
295
296     return 0;
297 }
298
299 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
300
301 AVFilter avfilter_vf_tinterlace = {
302     .name          = "tinterlace",
303     .description   = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
304     .priv_size     = sizeof(TInterlaceContext),
305     .init          = init,
306     .uninit        = uninit,
307     .query_formats = query_formats,
308
309     .inputs = (const AVFilterPad[]) {
310         { .name          = "default",
311           .type          = AVMEDIA_TYPE_VIDEO,
312           .start_frame   = start_frame,
313           .draw_slice    = null_draw_slice,
314           .end_frame     = end_frame, },
315         { .name = NULL}
316     },
317     .outputs = (const AVFilterPad[]) {
318         { .name          = "default",
319           .type          = AVMEDIA_TYPE_VIDEO,
320           .config_props  = config_out_props,
321           .poll_frame    = poll_frame,
322           .request_frame = request_frame },
323         { .name = NULL}
324     },
325 };