]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tinterlace.c
Merge commit '6a27ae28f9bde981e85c82cf5bf42c5f43fb6f13'
[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/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/avassert.h"
31 #include "avfilter.h"
32 #include "internal.h"
33
34 enum TInterlaceMode {
35     MODE_MERGE = 0,
36     MODE_DROP_EVEN,
37     MODE_DROP_ODD,
38     MODE_PAD,
39     MODE_INTERLEAVE_TOP,
40     MODE_INTERLEAVE_BOTTOM,
41     MODE_INTERLACEX2,
42     MODE_NB,
43 };
44
45 typedef struct {
46     const AVClass *class;
47     enum TInterlaceMode mode;   ///< interlace mode selected
48     int frame;                  ///< number of the output frame
49     int vsub;                   ///< chroma vertical subsampling
50     AVFilterBufferRef *cur;
51     AVFilterBufferRef *next;
52     uint8_t *black_data[4];     ///< buffer used to fill padded lines
53     int black_linesize[4];
54 } TInterlaceContext;
55
56 #define OFFSET(x) offsetof(TInterlaceContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58
59 static const AVOption tinterlace_options[] = {
60     {"mode",              "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
61     {"merge",             "merge fields",                                 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE},             INT_MIN, INT_MAX, FLAGS, "mode"},
62     {"drop_even",         "drop even fields",                             0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN},         INT_MIN, INT_MAX, FLAGS, "mode"},
63     {"drop_odd",          "drop odd fields",                              0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD},          INT_MIN, INT_MAX, FLAGS, "mode"},
64     {"pad",               "pad alternate lines with black",               0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD},               INT_MIN, INT_MAX, FLAGS, "mode"},
65     {"interleave_top",    "interleave top and bottom fields",             0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP},    INT_MIN, INT_MAX, FLAGS, "mode"},
66     {"interleave_bottom", "interleave bottom and top fields",             0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
67     {"interlacex2",       "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2},       INT_MIN, INT_MAX, FLAGS, "mode"},
68
69     {NULL}
70 };
71
72 AVFILTER_DEFINE_CLASS(tinterlace);
73
74 #define FULL_SCALE_YUVJ_FORMATS \
75     AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
76
77 static enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
78     FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
79 };
80
81 static int query_formats(AVFilterContext *ctx)
82 {
83     static const enum AVPixelFormat pix_fmts[] = {
84         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
85         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P,
86         AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
87         AV_PIX_FMT_NONE
88     };
89
90     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
91     return 0;
92 }
93
94 static av_cold int init(AVFilterContext *ctx, const char *args)
95 {
96     TInterlaceContext *tinterlace = ctx->priv;
97     static const char *shorthand[] = { "mode", NULL };
98
99     tinterlace->class = &tinterlace_class;
100     av_opt_set_defaults(tinterlace);
101
102     return av_opt_set_from_string(tinterlace, args, shorthand, "=", ":");
103 }
104
105 static av_cold void uninit(AVFilterContext *ctx)
106 {
107     TInterlaceContext *tinterlace = ctx->priv;
108
109     avfilter_unref_bufferp(&tinterlace->cur );
110     avfilter_unref_bufferp(&tinterlace->next);
111
112     av_opt_free(tinterlace);
113     av_freep(&tinterlace->black_data[0]);
114 }
115
116 static int config_out_props(AVFilterLink *outlink)
117 {
118     AVFilterContext *ctx = outlink->src;
119     AVFilterLink *inlink = outlink->src->inputs[0];
120     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
121     TInterlaceContext *tinterlace = ctx->priv;
122
123     tinterlace->vsub = desc->log2_chroma_h;
124     outlink->w = inlink->w;
125     outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
126         inlink->h*2 : inlink->h;
127
128     if (tinterlace->mode == MODE_PAD) {
129         uint8_t black[4] = { 16, 128, 128, 16 };
130         int i, ret;
131         if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
132             black[0] = black[3] = 0;
133         ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
134                              outlink->w, outlink->h, outlink->format, 1);
135         if (ret < 0)
136             return ret;
137
138         /* fill black picture with black */
139         for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
140             int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
141             memset(tinterlace->black_data[i], black[i],
142                    tinterlace->black_linesize[i] * h);
143         }
144     }
145     av_log(ctx, AV_LOG_VERBOSE, "mode:%d h:%d -> h:%d\n",
146            tinterlace->mode, inlink->h, outlink->h);
147
148     return 0;
149 }
150
151 #define FIELD_UPPER           0
152 #define FIELD_LOWER           1
153 #define FIELD_UPPER_AND_LOWER 2
154
155 /**
156  * Copy picture field from src to dst.
157  *
158  * @param src_field copy from upper, lower field or both
159  * @param interleave leave a padding line between each copied line
160  * @param dst_field copy to upper or lower field,
161  *        only meaningful when interleave is selected
162  */
163 static inline
164 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
165                         const uint8_t *src[4], int src_linesize[4],
166                         enum AVPixelFormat format, int w, int src_h,
167                         int src_field, int interleave, int dst_field)
168 {
169     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
170     int plane, vsub = desc->log2_chroma_h;
171     int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
172
173     for (plane = 0; plane < desc->nb_components; plane++) {
174         int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
175         int linesize = av_image_get_linesize(format, w, plane);
176         uint8_t *dstp = dst[plane];
177         const uint8_t *srcp = src[plane];
178
179         if (linesize < 0)
180             return;
181
182         lines /= k;
183         if (src_field == FIELD_LOWER)
184             srcp += src_linesize[plane];
185         if (interleave && dst_field == FIELD_LOWER)
186             dstp += dst_linesize[plane];
187         av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
188                             srcp, src_linesize[plane]*k, linesize, lines);
189     }
190 }
191
192 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
193 {
194     AVFilterContext *ctx = inlink->dst;
195     AVFilterLink *outlink = ctx->outputs[0];
196     TInterlaceContext *tinterlace = ctx->priv;
197     AVFilterBufferRef *cur, *next, *out;
198     int field, tff, ret;
199
200     avfilter_unref_buffer(tinterlace->cur);
201     tinterlace->cur  = tinterlace->next;
202     tinterlace->next = picref;
203
204     cur = tinterlace->cur;
205     next = tinterlace->next;
206     /* we need at least two frames */
207     if (!tinterlace->cur)
208         return 0;
209
210     switch (tinterlace->mode) {
211     case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
212              * the lower field, generating a double-height video at half framerate */
213         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
214         if (!out)
215             return AVERROR(ENOMEM);
216         avfilter_copy_buffer_ref_props(out, cur);
217         out->video->h = outlink->h;
218         out->video->interlaced = 1;
219         out->video->top_field_first = 1;
220
221         /* write odd frame lines into the upper field of the new frame */
222         copy_picture_field(out->data, out->linesize,
223                            (const uint8_t **)cur->data, cur->linesize,
224                            inlink->format, inlink->w, inlink->h,
225                            FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
226         /* write even frame lines into the lower field of the new frame */
227         copy_picture_field(out->data, out->linesize,
228                            (const uint8_t **)next->data, next->linesize,
229                            inlink->format, inlink->w, inlink->h,
230                            FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
231         avfilter_unref_bufferp(&tinterlace->next);
232         break;
233
234     case MODE_DROP_ODD:  /* only output even frames, odd  frames are dropped; height unchanged, half framerate */
235     case MODE_DROP_EVEN: /* only output odd  frames, even frames are dropped; height unchanged, half framerate */
236         out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
237         avfilter_unref_bufferp(&tinterlace->next);
238         break;
239
240     case MODE_PAD: /* expand each frame to double height, but pad alternate
241                     * lines with black; framerate unchanged */
242         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
243         avfilter_copy_buffer_ref_props(out, cur);
244         out->video->h = outlink->h;
245
246         field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
247         /* copy upper and lower fields */
248         copy_picture_field(out->data, out->linesize,
249                            (const uint8_t **)cur->data, cur->linesize,
250                            inlink->format, inlink->w, inlink->h,
251                            FIELD_UPPER_AND_LOWER, 1, field);
252         /* pad with black the other field */
253         copy_picture_field(out->data, out->linesize,
254                            (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
255                            inlink->format, inlink->w, inlink->h,
256                            FIELD_UPPER_AND_LOWER, 1, !field);
257         break;
258
259         /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
260          * halving the frame rate and preserving image height */
261     case MODE_INTERLEAVE_TOP:    /* top    field first */
262     case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
263         tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
264         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
265         if (!out)
266             return AVERROR(ENOMEM);
267         avfilter_copy_buffer_ref_props(out, cur);
268         out->video->interlaced = 1;
269         out->video->top_field_first = tff;
270
271         /* copy upper/lower field from cur */
272         copy_picture_field(out->data, out->linesize,
273                            (const uint8_t **)cur->data, cur->linesize,
274                            inlink->format, inlink->w, inlink->h,
275                            tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
276         /* copy lower/upper field from next */
277         copy_picture_field(out->data, out->linesize,
278                            (const uint8_t **)next->data, next->linesize,
279                            inlink->format, inlink->w, inlink->h,
280                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
281         avfilter_unref_bufferp(&tinterlace->next);
282         break;
283     case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
284         /* output current frame first */
285         out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
286         if (!out)
287             return AVERROR(ENOMEM);
288         out->video->interlaced = 1;
289
290         if ((ret = ff_filter_frame(outlink, out)) < 0)
291             return ret;
292
293         /* output mix of current and next frame */
294         tff = next->video->top_field_first;
295         out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
296         if (!out)
297             return AVERROR(ENOMEM);
298         avfilter_copy_buffer_ref_props(out, next);
299         out->video->interlaced = 1;
300
301         /* write current frame second field lines into the second field of the new frame */
302         copy_picture_field(out->data, out->linesize,
303                            (const uint8_t **)cur->data, cur->linesize,
304                            inlink->format, inlink->w, inlink->h,
305                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
306         /* write next frame first field lines into the first field of the new frame */
307         copy_picture_field(out->data, out->linesize,
308                            (const uint8_t **)next->data, next->linesize,
309                            inlink->format, inlink->w, inlink->h,
310                            tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
311         break;
312     default:
313         av_assert0(0);
314     }
315
316     ret = ff_filter_frame(outlink, out);
317     tinterlace->frame++;
318
319     return ret;
320 }
321
322 static int request_frame(AVFilterLink *outlink)
323 {
324     TInterlaceContext *tinterlace = outlink->src->priv;
325     AVFilterLink *inlink = outlink->src->inputs[0];
326
327     do {
328         int ret;
329
330         if ((ret = ff_request_frame(inlink)) < 0)
331             return ret;
332     } while (!tinterlace->cur);
333
334     return 0;
335 }
336
337 static const AVFilterPad tinterlace_inputs[] = {
338     {
339         .name         = "default",
340         .type         = AVMEDIA_TYPE_VIDEO,
341         .filter_frame = filter_frame,
342     },
343     { NULL }
344 };
345
346 static const AVFilterPad tinterlace_outputs[] = {
347     {
348         .name          = "default",
349         .type          = AVMEDIA_TYPE_VIDEO,
350         .config_props  = config_out_props,
351         .request_frame = request_frame,
352     },
353     { NULL }
354 };
355
356 AVFilter avfilter_vf_tinterlace = {
357     .name          = "tinterlace",
358     .description   = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
359     .priv_size     = sizeof(TInterlaceContext),
360     .init          = init,
361     .uninit        = uninit,
362     .query_formats = query_formats,
363     .inputs        = tinterlace_inputs,
364     .outputs       = tinterlace_outputs,
365     .priv_class    = &tinterlace_class,
366 };