]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tinterlace.c
542b66898e7a39c4a796a19b53e042fce9dc218c
[ffmpeg] / libavfilter / vf_tinterlace.c
1 /*
2  * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2010 Baptiste Coudurier
5  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with FFmpeg if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * @file
26  * temporal field interlace filter, ported from MPlayer/libmpcodecs
27  */
28
29 #include "libavutil/opt.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/avassert.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "tinterlace.h"
35
36 #define OFFSET(x) offsetof(TInterlaceContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
38
39 static const AVOption tinterlace_options[] = {
40     {"mode",              "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
41     {"merge",             "merge fields",                                 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE},             INT_MIN, INT_MAX, FLAGS, "mode"},
42     {"drop_even",         "drop even fields",                             0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN},         INT_MIN, INT_MAX, FLAGS, "mode"},
43     {"drop_odd",          "drop odd fields",                              0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD},          INT_MIN, INT_MAX, FLAGS, "mode"},
44     {"pad",               "pad alternate lines with black",               0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD},               INT_MIN, INT_MAX, FLAGS, "mode"},
45     {"interleave_top",    "interleave top and bottom fields",             0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP},    INT_MIN, INT_MAX, FLAGS, "mode"},
46     {"interleave_bottom", "interleave bottom and top fields",             0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
47     {"interlacex2",       "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2},       INT_MIN, INT_MAX, FLAGS, "mode"},
48     {"mergex2",           "merge fields keeping same frame rate",         0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGEX2},           INT_MIN, INT_MAX, FLAGS, "mode"},
49
50     {"flags",             "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
51     {"low_pass_filter",   "enable vertical low-pass filter",              0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
52     {"vlpf",              "enable vertical low-pass filter",              0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
53     {"complex_filter",    "enable complex vertical low-pass filter",      0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
54     {"cvlpf",             "enable complex vertical low-pass filter",      0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
55     {"exact_tb",          "force a timebase which can represent timestamps exactly", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_EXACT_TB}, INT_MIN, INT_MAX, FLAGS, "flags" },
56     {"bypass_il",         "bypass already interlaced frames",             0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_BYPASS_IL}, INT_MIN, INT_MAX, FLAGS, "flags" },
57
58     {NULL}
59 };
60
61 AVFILTER_DEFINE_CLASS(tinterlace);
62
63 static const AVOption interlace_options[] = {
64    { "scan",              "scanning mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_TFF}, 0, 1, FLAGS, "mode"},
65    { "tff",               "top field first",                              0, AV_OPT_TYPE_CONST, {.i64 = MODE_TFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
66    { "bff",               "bottom field first",                           0, AV_OPT_TYPE_CONST, {.i64 = MODE_BFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
67    { "lowpass",           "set vertical low-pass filter", OFFSET(lowpass), AV_OPT_TYPE_INT,   {.i64 = VLPF_LIN}, 0, 2, FLAGS, "lowpass" },
68    {     "off",           "disable vertical low-pass filter",             0, AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
69    {     "linear",        "linear vertical low-pass filter",              0, AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
70    {     "complex",       "complex vertical low-pass filter",             0, AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
71
72    { NULL }
73 };
74
75 AVFILTER_DEFINE_CLASS(interlace);
76
77 #define FULL_SCALE_YUVJ_FORMATS \
78     AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
79
80 static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
81     FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
82 };
83
84 static const AVRational standard_tbs[] = {
85     {1, 25},
86     {1, 30},
87     {1001, 30000},
88 };
89
90 static int query_formats(AVFilterContext *ctx)
91 {
92     static const enum AVPixelFormat pix_fmts[] = {
93         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
94         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
95         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
96         AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE,
97         AV_PIX_FMT_YUV440P10LE, AV_PIX_FMT_YUV444P10LE,
98         AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE,
99         AV_PIX_FMT_YUV440P12LE, AV_PIX_FMT_YUV444P12LE,
100         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
101         AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
102         AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
103         AV_PIX_FMT_NONE
104     };
105
106     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
107     if (!fmts_list)
108         return AVERROR(ENOMEM);
109     return ff_set_common_formats(ctx, fmts_list);
110 }
111
112 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
113                            ptrdiff_t mref, ptrdiff_t pref, int clip_max)
114 {
115     const uint8_t *srcp_above = srcp + mref;
116     const uint8_t *srcp_below = srcp + pref;
117     int i;
118     for (i = 0; i < width; i++) {
119         // this calculation is an integer representation of
120         // '0.5 * current + 0.25 * above + 0.25 * below'
121         // '1 +' is for rounding.
122         dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
123     }
124 }
125
126 static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
127                               ptrdiff_t mref, ptrdiff_t pref, int clip_max)
128 {
129     uint16_t *dstp = (uint16_t *)dst8;
130     const uint16_t *srcp = (const uint16_t *)src8;
131     const uint16_t *srcp_above = srcp + mref / 2;
132     const uint16_t *srcp_below = srcp + pref / 2;
133     int i, src_x;
134     for (i = 0; i < width; i++) {
135         // this calculation is an integer representation of
136         // '0.5 * current + 0.25 * above + 0.25 * below'
137         // '1 +' is for rounding.
138         src_x   = av_le2ne16(srcp[i]) << 1;
139         dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
140                              + av_le2ne16(srcp_below[i])) >> 2);
141     }
142 }
143
144 static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
145                                    ptrdiff_t mref, ptrdiff_t pref, int clip_max)
146 {
147     const uint8_t *srcp_above = srcp + mref;
148     const uint8_t *srcp_below = srcp + pref;
149     const uint8_t *srcp_above2 = srcp + mref * 2;
150     const uint8_t *srcp_below2 = srcp + pref * 2;
151     int i, src_x, src_ab;
152     for (i = 0; i < width; i++) {
153         // this calculation is an integer representation of
154         // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
155         // '4 +' is for rounding.
156         src_x   = srcp[i] << 1;
157         src_ab  = srcp_above[i] + srcp_below[i];
158         dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1)
159                                 - srcp_above2[i] - srcp_below2[i]) >> 3);
160         // Prevent over-sharpening:
161         // dst must not exceed src when the average of above and below
162         // is less than src. And the other way around.
163         if (src_ab > src_x) {
164             if (dstp[i] < srcp[i])
165                 dstp[i] = srcp[i];
166         } else if (dstp[i] > srcp[i])
167             dstp[i] = srcp[i];
168     }
169 }
170
171 static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
172                                       ptrdiff_t mref, ptrdiff_t pref, int clip_max)
173 {
174     uint16_t *dstp = (uint16_t *)dst8;
175     const uint16_t *srcp = (const uint16_t *)src8;
176     const uint16_t *srcp_above = srcp + mref / 2;
177     const uint16_t *srcp_below = srcp + pref / 2;
178     const uint16_t *srcp_above2 = srcp + mref;
179     const uint16_t *srcp_below2 = srcp + pref;
180     int i, dst_le, src_le, src_x, src_ab;
181     for (i = 0; i < width; i++) {
182         // this calculation is an integer representation of
183         // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
184         // '4 +' is for rounding.
185         src_le = av_le2ne16(srcp[i]);
186         src_x  = src_le << 1;
187         src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
188         dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
189                          - av_le2ne16(srcp_above2[i])
190                          - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
191         // Prevent over-sharpening:
192         // dst must not exceed src when the average of above and below
193         // is less than src. And the other way around.
194         if (src_ab > src_x) {
195             if (dst_le < src_le)
196                 dstp[i] = av_le2ne16(src_le);
197             else
198                 dstp[i] = av_le2ne16(dst_le);
199         } else if (dst_le > src_le) {
200             dstp[i] = av_le2ne16(src_le);
201         } else
202             dstp[i] = av_le2ne16(dst_le);
203     }
204 }
205
206 static av_cold void uninit(AVFilterContext *ctx)
207 {
208     TInterlaceContext *tinterlace = ctx->priv;
209
210     av_frame_free(&tinterlace->cur );
211     av_frame_free(&tinterlace->next);
212     av_freep(&tinterlace->black_data[0]);
213 }
214
215 static int config_out_props(AVFilterLink *outlink)
216 {
217     AVFilterContext *ctx = outlink->src;
218     AVFilterLink *inlink = outlink->src->inputs[0];
219     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
220     TInterlaceContext *tinterlace = ctx->priv;
221     int i;
222
223     tinterlace->vsub = desc->log2_chroma_h;
224     outlink->w = inlink->w;
225     outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2?
226         inlink->h*2 : inlink->h;
227     if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2)
228         outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio,
229                                                 av_make_q(2, 1));
230
231     if (tinterlace->mode == MODE_PAD) {
232         uint8_t black[4] = { 0, 0, 0, 16 };
233         int ret;
234         ff_draw_init(&tinterlace->draw, outlink->format, 0);
235         ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
236         if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
237             tinterlace->color.comp[0].u8[0] = 0;
238         ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
239                              outlink->w, outlink->h, outlink->format, 16);
240         if (ret < 0)
241             return ret;
242
243         ff_fill_rectangle(&tinterlace->draw, &tinterlace->color, tinterlace->black_data,
244                           tinterlace->black_linesize, 0, 0, outlink->w, outlink->h);
245     }
246     if (tinterlace->flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)
247             && !(tinterlace->mode == MODE_INTERLEAVE_TOP
248               || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
249         av_log(ctx, AV_LOG_WARNING, "low_pass_filter flags ignored with mode %d\n",
250                 tinterlace->mode);
251         tinterlace->flags &= ~(TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF);
252     }
253     tinterlace->preout_time_base = inlink->time_base;
254     if (tinterlace->mode == MODE_INTERLACEX2) {
255         tinterlace->preout_time_base.den *= 2;
256         outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
257         outlink->time_base  = av_mul_q(inlink->time_base , (AVRational){1,2});
258     } else if (tinterlace->mode == MODE_MERGEX2) {
259         outlink->frame_rate = inlink->frame_rate;
260         outlink->time_base  = inlink->time_base;
261     } else if (tinterlace->mode != MODE_PAD) {
262         outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
263         outlink->time_base  = av_mul_q(inlink->time_base , (AVRational){2,1});
264     }
265
266     for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
267         if (!av_cmp_q(standard_tbs[i], outlink->time_base))
268             break;
269     }
270     if (i == FF_ARRAY_ELEMS(standard_tbs) ||
271         (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
272         outlink->time_base = tinterlace->preout_time_base;
273
274     tinterlace->csp = av_pix_fmt_desc_get(outlink->format);
275     if (tinterlace->flags & TINTERLACE_FLAG_CVLPF) {
276         if (tinterlace->csp->comp[0].depth > 8)
277             tinterlace->lowpass_line = lowpass_line_complex_c_16;
278         else
279             tinterlace->lowpass_line = lowpass_line_complex_c;
280         if (ARCH_X86)
281             ff_tinterlace_init_x86(tinterlace);
282     } else if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
283         if (tinterlace->csp->comp[0].depth > 8)
284             tinterlace->lowpass_line = lowpass_line_c_16;
285         else
286             tinterlace->lowpass_line = lowpass_line_c;
287         if (ARCH_X86)
288             ff_tinterlace_init_x86(tinterlace);
289     }
290
291     av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
292            (tinterlace->flags & TINTERLACE_FLAG_CVLPF) ? "complex" :
293            (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "linear" : "off",
294            inlink->h, outlink->h);
295
296     return 0;
297 }
298
299 #define FIELD_UPPER           0
300 #define FIELD_LOWER           1
301 #define FIELD_UPPER_AND_LOWER 2
302
303 /**
304  * Copy picture field from src to dst.
305  *
306  * @param src_field copy from upper, lower field or both
307  * @param interleave leave a padding line between each copied line
308  * @param dst_field copy to upper or lower field,
309  *        only meaningful when interleave is selected
310  * @param flags context flags
311  */
312 static inline
313 void copy_picture_field(TInterlaceContext *tinterlace,
314                         uint8_t *dst[4], int dst_linesize[4],
315                         const uint8_t *src[4], int src_linesize[4],
316                         enum AVPixelFormat format, int w, int src_h,
317                         int src_field, int interleave, int dst_field,
318                         int flags)
319 {
320     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
321     int hsub = desc->log2_chroma_w;
322     int plane, vsub = desc->log2_chroma_h;
323     int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
324     int h;
325
326     for (plane = 0; plane < desc->nb_components; plane++) {
327         int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;
328         int cols  = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(    w, hsub) : w;
329         uint8_t *dstp = dst[plane];
330         const uint8_t *srcp = src[plane];
331         int srcp_linesize = src_linesize[plane] * k;
332         int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
333         int clip_max = (1 << tinterlace->csp->comp[plane].depth) - 1;
334
335         lines = (lines + (src_field == FIELD_UPPER)) / k;
336         if (src_field == FIELD_LOWER)
337             srcp += src_linesize[plane];
338         if (interleave && dst_field == FIELD_LOWER)
339             dstp += dst_linesize[plane];
340         // Low-pass filtering is required when creating an interlaced destination from
341         // a progressive source which contains high-frequency vertical detail.
342         // Filtering will reduce interlace 'twitter' and Moire patterning.
343         if (flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)) {
344             int x = !!(flags & TINTERLACE_FLAG_CVLPF);
345             for (h = lines; h > 0; h--) {
346                 ptrdiff_t pref = src_linesize[plane];
347                 ptrdiff_t mref = -pref;
348                 if (h >= (lines - x))  mref = 0; // there is no line above
349                 else if (h <= (1 + x)) pref = 0; // there is no line below
350
351                 tinterlace->lowpass_line(dstp, cols, srcp, mref, pref, clip_max);
352                 dstp += dstp_linesize;
353                 srcp += srcp_linesize;
354             }
355         } else {
356             if (tinterlace->csp->comp[plane].depth > 8)
357                 cols *= 2;
358             av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
359         }
360     }
361 }
362
363 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
364 {
365     AVFilterContext *ctx = inlink->dst;
366     AVFilterLink *outlink = ctx->outputs[0];
367     TInterlaceContext *tinterlace = ctx->priv;
368     AVFrame *cur, *next, *out;
369     int field, tff, ret;
370
371     av_frame_free(&tinterlace->cur);
372     tinterlace->cur  = tinterlace->next;
373     tinterlace->next = picref;
374
375     cur = tinterlace->cur;
376     next = tinterlace->next;
377     /* we need at least two frames */
378     if (!tinterlace->cur)
379         return 0;
380
381     switch (tinterlace->mode) {
382     case MODE_MERGEX2: /* move the odd frame into the upper field of the new image, even into
383                         * the lower field, generating a double-height video at same framerate */
384     case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
385              * the lower field, generating a double-height video at half framerate */
386         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
387         if (!out)
388             return AVERROR(ENOMEM);
389         av_frame_copy_props(out, cur);
390         out->height = outlink->h;
391         out->interlaced_frame = 1;
392         out->top_field_first = 1;
393         out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
394
395         /* write odd frame lines into the upper field of the new frame */
396         copy_picture_field(tinterlace, out->data, out->linesize,
397                            (const uint8_t **)cur->data, cur->linesize,
398                            inlink->format, inlink->w, inlink->h,
399                            FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_LOWER : FIELD_UPPER : FIELD_UPPER, tinterlace->flags);
400         /* write even frame lines into the lower field of the new frame */
401         copy_picture_field(tinterlace, out->data, out->linesize,
402                            (const uint8_t **)next->data, next->linesize,
403                            inlink->format, inlink->w, inlink->h,
404                            FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_UPPER : FIELD_LOWER : FIELD_LOWER, tinterlace->flags);
405         if (tinterlace->mode != MODE_MERGEX2)
406             av_frame_free(&tinterlace->next);
407         break;
408
409     case MODE_DROP_ODD:  /* only output even frames, odd  frames are dropped; height unchanged, half framerate */
410     case MODE_DROP_EVEN: /* only output odd  frames, even frames are dropped; height unchanged, half framerate */
411         out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
412         if (!out)
413             return AVERROR(ENOMEM);
414         av_frame_free(&tinterlace->next);
415         break;
416
417     case MODE_PAD: /* expand each frame to double height, but pad alternate
418                     * lines with black; framerate unchanged */
419         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
420         if (!out)
421             return AVERROR(ENOMEM);
422         av_frame_copy_props(out, cur);
423         out->height = outlink->h;
424         out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
425
426         field = (1 + outlink->frame_count_in) & 1 ? FIELD_UPPER : FIELD_LOWER;
427         /* copy upper and lower fields */
428         copy_picture_field(tinterlace, out->data, out->linesize,
429                            (const uint8_t **)cur->data, cur->linesize,
430                            inlink->format, inlink->w, inlink->h,
431                            FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
432         /* pad with black the other field */
433         copy_picture_field(tinterlace, out->data, out->linesize,
434                            (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
435                            inlink->format, inlink->w, inlink->h,
436                            FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
437         break;
438
439         /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
440          * halving the frame rate and preserving image height */
441     case MODE_INTERLEAVE_TOP:    /* top    field first */
442     case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
443         if ((tinterlace->flags & TINTERLACE_FLAG_BYPASS_IL) && cur->interlaced_frame) {
444             av_log(ctx, AV_LOG_WARNING,
445                    "video is already interlaced, adjusting framerate only\n");
446             out = av_frame_clone(cur);
447             if (!out)
448                 return AVERROR(ENOMEM);
449             out->pts /= 2;  // adjust pts to new framerate
450             ret = ff_filter_frame(outlink, out);
451             return ret;
452         }
453         tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
454         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
455         if (!out)
456             return AVERROR(ENOMEM);
457         av_frame_copy_props(out, cur);
458         out->interlaced_frame = 1;
459         out->top_field_first = tff;
460
461         /* copy upper/lower field from cur */
462         copy_picture_field(tinterlace, out->data, out->linesize,
463                            (const uint8_t **)cur->data, cur->linesize,
464                            inlink->format, inlink->w, inlink->h,
465                            tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
466                            tinterlace->flags);
467         /* copy lower/upper field from next */
468         copy_picture_field(tinterlace, out->data, out->linesize,
469                            (const uint8_t **)next->data, next->linesize,
470                            inlink->format, inlink->w, inlink->h,
471                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
472                            tinterlace->flags);
473         av_frame_free(&tinterlace->next);
474         break;
475     case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
476         /* output current frame first */
477         out = av_frame_clone(cur);
478         if (!out)
479             return AVERROR(ENOMEM);
480         out->interlaced_frame = 1;
481         if (cur->pts != AV_NOPTS_VALUE)
482             out->pts = cur->pts*2;
483
484         out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
485         if ((ret = ff_filter_frame(outlink, out)) < 0)
486             return ret;
487
488         /* output mix of current and next frame */
489         tff = next->top_field_first;
490         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
491         if (!out)
492             return AVERROR(ENOMEM);
493         av_frame_copy_props(out, next);
494         out->interlaced_frame = 1;
495         out->top_field_first = !tff;
496
497         if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
498             out->pts = cur->pts + next->pts;
499         else
500             out->pts = AV_NOPTS_VALUE;
501         /* write current frame second field lines into the second field of the new frame */
502         copy_picture_field(tinterlace, out->data, out->linesize,
503                            (const uint8_t **)cur->data, cur->linesize,
504                            inlink->format, inlink->w, inlink->h,
505                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
506                            tinterlace->flags);
507         /* write next frame first field lines into the first field of the new frame */
508         copy_picture_field(tinterlace, out->data, out->linesize,
509                            (const uint8_t **)next->data, next->linesize,
510                            inlink->format, inlink->w, inlink->h,
511                            tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
512                            tinterlace->flags);
513         break;
514     default:
515         av_assert0(0);
516     }
517
518     out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
519     ret = ff_filter_frame(outlink, out);
520
521     return ret;
522 }
523
524 static int init_interlace(AVFilterContext *ctx)
525 {
526     TInterlaceContext *tinterlace = ctx->priv;
527
528     if (tinterlace->mode <= MODE_BFF)
529         tinterlace->mode += MODE_INTERLEAVE_TOP;
530
531     tinterlace->flags |= TINTERLACE_FLAG_BYPASS_IL;
532     if (tinterlace->lowpass == VLPF_LIN)
533         tinterlace->flags |= TINTERLACE_FLAG_VLPF;
534     if (tinterlace->lowpass == VLPF_CMP)
535         tinterlace->flags |= TINTERLACE_FLAG_CVLPF;
536
537     return 0;
538 }
539
540 static const AVFilterPad tinterlace_inputs[] = {
541     {
542         .name         = "default",
543         .type         = AVMEDIA_TYPE_VIDEO,
544         .filter_frame = filter_frame,
545     },
546     { NULL }
547 };
548
549 static const AVFilterPad tinterlace_outputs[] = {
550     {
551         .name         = "default",
552         .type         = AVMEDIA_TYPE_VIDEO,
553         .config_props = config_out_props,
554     },
555     { NULL }
556 };
557
558 AVFilter ff_vf_tinterlace = {
559     .name          = "tinterlace",
560     .description   = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
561     .priv_size     = sizeof(TInterlaceContext),
562     .uninit        = uninit,
563     .query_formats = query_formats,
564     .inputs        = tinterlace_inputs,
565     .outputs       = tinterlace_outputs,
566     .priv_class    = &tinterlace_class,
567 };
568
569
570 AVFilter ff_vf_interlace = {
571     .name          = "interlace",
572     .description   = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
573     .priv_size     = sizeof(TInterlaceContext),
574     .init          = init_interlace,
575     .uninit        = uninit,
576     .query_formats = query_formats,
577     .inputs        = tinterlace_inputs,
578     .outputs       = tinterlace_outputs,
579     .priv_class    = &interlace_class,
580 };