]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_w3fdif.c
avfilter/vf_w3fdif: fix parity in frame mode
[ffmpeg] / libavfilter / vf_w3fdif.c
1 /*
2  * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
3  * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
4  * Based on the process described by Martin Weston for BBC R&D
5  * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (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 GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "libavutil/common.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32 #include "w3fdif.h"
33
34 typedef struct W3FDIFContext {
35     const AVClass *class;
36     int filter;           ///< 0 is simple, 1 is more complex
37     int mode;             ///< 0 is frame, 1 is field
38     int parity;           ///< frame field parity
39     int deint;            ///< which frames to deinterlace
40     int linesize[4];      ///< bytes of pixel data per line for each plane
41     int planeheight[4];   ///< height of each plane
42     int field;            ///< which field are we on, 0 or 1
43     int eof;
44     int nb_planes;
45     AVFrame *prev, *cur, *next;  ///< previous, current, next frames
46     int32_t **work_line;  ///< lines we are calculating
47     int nb_threads;
48     int max;
49
50     W3FDIFDSPContext dsp;
51 } W3FDIFContext;
52
53 #define OFFSET(x) offsetof(W3FDIFContext, x)
54 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
55 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
56
57 static const AVOption w3fdif_options[] = {
58     { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
59     CONST("simple",  NULL, 0, "filter"),
60     CONST("complex", NULL, 1, "filter"),
61     { "mode",   "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode"},
62     CONST("frame", "send one frame for each frame", 0, "mode"),
63     CONST("field", "send one frame for each field", 1, "mode"),
64     { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
65     CONST("tff",  "assume top field first",     0, "parity"),
66     CONST("bff",  "assume bottom field first",  1, "parity"),
67     CONST("auto", "auto detect parity",        -1, "parity"),
68     { "deint",  "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
69     CONST("all",        "deinterlace all frames",                       0, "deint"),
70     CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
71     { NULL }
72 };
73
74 AVFILTER_DEFINE_CLASS(w3fdif);
75
76 static int query_formats(AVFilterContext *ctx)
77 {
78     static const enum AVPixelFormat pix_fmts[] = {
79         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
80         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
81         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
82         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
83         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
84         AV_PIX_FMT_YUVJ411P,
85         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
86         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
87         AV_PIX_FMT_GRAY8,
88         AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
89         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
90         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
91         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
92         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
93         AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
94         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
95         AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
96         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
97         AV_PIX_FMT_GBRAP10,   AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
98         AV_PIX_FMT_NONE
99     };
100
101     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
102     if (!fmts_list)
103         return AVERROR(ENOMEM);
104     return ff_set_common_formats(ctx, fmts_list);
105 }
106
107 static void filter_simple_low(int32_t *work_line,
108                               uint8_t *in_lines_cur[2],
109                               const int16_t *coef, int linesize)
110 {
111     int i;
112
113     for (i = 0; i < linesize; i++) {
114         *work_line    = *in_lines_cur[0]++ * coef[0];
115         *work_line++ += *in_lines_cur[1]++ * coef[1];
116     }
117 }
118
119 static void filter_complex_low(int32_t *work_line,
120                                uint8_t *in_lines_cur[4],
121                                const int16_t *coef, int linesize)
122 {
123     int i;
124
125     for (i = 0; i < linesize; i++) {
126         *work_line    = *in_lines_cur[0]++ * coef[0];
127         *work_line   += *in_lines_cur[1]++ * coef[1];
128         *work_line   += *in_lines_cur[2]++ * coef[2];
129         *work_line++ += *in_lines_cur[3]++ * coef[3];
130     }
131 }
132
133 static void filter_simple_high(int32_t *work_line,
134                                uint8_t *in_lines_cur[3],
135                                uint8_t *in_lines_adj[3],
136                                const int16_t *coef, int linesize)
137 {
138     int i;
139
140     for (i = 0; i < linesize; i++) {
141         *work_line   += *in_lines_cur[0]++ * coef[0];
142         *work_line   += *in_lines_adj[0]++ * coef[0];
143         *work_line   += *in_lines_cur[1]++ * coef[1];
144         *work_line   += *in_lines_adj[1]++ * coef[1];
145         *work_line   += *in_lines_cur[2]++ * coef[2];
146         *work_line++ += *in_lines_adj[2]++ * coef[2];
147     }
148 }
149
150 static void filter_complex_high(int32_t *work_line,
151                                 uint8_t *in_lines_cur[5],
152                                 uint8_t *in_lines_adj[5],
153                                 const int16_t *coef, int linesize)
154 {
155     int i;
156
157     for (i = 0; i < linesize; i++) {
158         *work_line   += *in_lines_cur[0]++ * coef[0];
159         *work_line   += *in_lines_adj[0]++ * coef[0];
160         *work_line   += *in_lines_cur[1]++ * coef[1];
161         *work_line   += *in_lines_adj[1]++ * coef[1];
162         *work_line   += *in_lines_cur[2]++ * coef[2];
163         *work_line   += *in_lines_adj[2]++ * coef[2];
164         *work_line   += *in_lines_cur[3]++ * coef[3];
165         *work_line   += *in_lines_adj[3]++ * coef[3];
166         *work_line   += *in_lines_cur[4]++ * coef[4];
167         *work_line++ += *in_lines_adj[4]++ * coef[4];
168     }
169 }
170
171 static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
172 {
173     int j;
174
175     for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
176         *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
177 }
178
179 static void filter16_simple_low(int32_t *work_line,
180                                 uint8_t *in_lines_cur8[2],
181                                 const int16_t *coef, int linesize)
182 {
183     uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] };
184     int i;
185
186     linesize /= 2;
187     for (i = 0; i < linesize; i++) {
188         *work_line    = *in_lines_cur[0]++ * coef[0];
189         *work_line++ += *in_lines_cur[1]++ * coef[1];
190     }
191 }
192
193 static void filter16_complex_low(int32_t *work_line,
194                                  uint8_t *in_lines_cur8[4],
195                                  const int16_t *coef, int linesize)
196 {
197     uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0],
198                                   (uint16_t *)in_lines_cur8[1],
199                                   (uint16_t *)in_lines_cur8[2],
200                                   (uint16_t *)in_lines_cur8[3] };
201     int i;
202
203     linesize /= 2;
204     for (i = 0; i < linesize; i++) {
205         *work_line    = *in_lines_cur[0]++ * coef[0];
206         *work_line   += *in_lines_cur[1]++ * coef[1];
207         *work_line   += *in_lines_cur[2]++ * coef[2];
208         *work_line++ += *in_lines_cur[3]++ * coef[3];
209     }
210 }
211
212 static void filter16_simple_high(int32_t *work_line,
213                                  uint8_t *in_lines_cur8[3],
214                                  uint8_t *in_lines_adj8[3],
215                                  const int16_t *coef, int linesize)
216 {
217     uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0],
218                                   (uint16_t *)in_lines_cur8[1],
219                                   (uint16_t *)in_lines_cur8[2] };
220     uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0],
221                                   (uint16_t *)in_lines_adj8[1],
222                                   (uint16_t *)in_lines_adj8[2] };
223     int i;
224
225     linesize /= 2;
226     for (i = 0; i < linesize; i++) {
227         *work_line   += *in_lines_cur[0]++ * coef[0];
228         *work_line   += *in_lines_adj[0]++ * coef[0];
229         *work_line   += *in_lines_cur[1]++ * coef[1];
230         *work_line   += *in_lines_adj[1]++ * coef[1];
231         *work_line   += *in_lines_cur[2]++ * coef[2];
232         *work_line++ += *in_lines_adj[2]++ * coef[2];
233     }
234 }
235
236 static void filter16_complex_high(int32_t *work_line,
237                                   uint8_t *in_lines_cur8[5],
238                                   uint8_t *in_lines_adj8[5],
239                                   const int16_t *coef, int linesize)
240 {
241     uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0],
242                                   (uint16_t *)in_lines_cur8[1],
243                                   (uint16_t *)in_lines_cur8[2],
244                                   (uint16_t *)in_lines_cur8[3],
245                                   (uint16_t *)in_lines_cur8[4] };
246     uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0],
247                                   (uint16_t *)in_lines_adj8[1],
248                                   (uint16_t *)in_lines_adj8[2],
249                                   (uint16_t *)in_lines_adj8[3],
250                                   (uint16_t *)in_lines_adj8[4] };
251     int i;
252
253     linesize /= 2;
254     for (i = 0; i < linesize; i++) {
255         *work_line   += *in_lines_cur[0]++ * coef[0];
256         *work_line   += *in_lines_adj[0]++ * coef[0];
257         *work_line   += *in_lines_cur[1]++ * coef[1];
258         *work_line   += *in_lines_adj[1]++ * coef[1];
259         *work_line   += *in_lines_cur[2]++ * coef[2];
260         *work_line   += *in_lines_adj[2]++ * coef[2];
261         *work_line   += *in_lines_cur[3]++ * coef[3];
262         *work_line   += *in_lines_adj[3]++ * coef[3];
263         *work_line   += *in_lines_cur[4]++ * coef[4];
264         *work_line++ += *in_lines_adj[4]++ * coef[4];
265     }
266 }
267
268 static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max)
269 {
270     uint16_t *out_pixel = (uint16_t *)out_pixel8;
271     int j;
272
273     linesize /= 2;
274     for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
275         *out_pixel = av_clip(*work_pixel, 0, max) >> 15;
276 }
277
278 static int config_input(AVFilterLink *inlink)
279 {
280     AVFilterContext *ctx = inlink->dst;
281     W3FDIFContext *s = ctx->priv;
282     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
283     int ret, i, depth;
284
285     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
286         return ret;
287
288     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
289     s->planeheight[0] = s->planeheight[3] = inlink->h;
290
291     if (inlink->h < 3) {
292         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
293         return AVERROR(EINVAL);
294     }
295
296     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
297     s->nb_threads = ff_filter_get_nb_threads(ctx);
298     s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
299     if (!s->work_line)
300         return AVERROR(ENOMEM);
301
302     for (i = 0; i < s->nb_threads; i++) {
303         s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
304         if (!s->work_line[i])
305             return AVERROR(ENOMEM);
306     }
307
308     depth = desc->comp[0].depth;
309     s->max = ((1 << depth) - 1) * 256 * 128;
310     if (depth <= 8) {
311         s->dsp.filter_simple_low   = filter_simple_low;
312         s->dsp.filter_complex_low  = filter_complex_low;
313         s->dsp.filter_simple_high  = filter_simple_high;
314         s->dsp.filter_complex_high = filter_complex_high;
315         s->dsp.filter_scale        = filter_scale;
316     } else {
317         s->dsp.filter_simple_low   = filter16_simple_low;
318         s->dsp.filter_complex_low  = filter16_complex_low;
319         s->dsp.filter_simple_high  = filter16_simple_high;
320         s->dsp.filter_complex_high = filter16_complex_high;
321         s->dsp.filter_scale        = filter16_scale;
322     }
323
324     if (ARCH_X86)
325         ff_w3fdif_init_x86(&s->dsp, depth);
326
327     return 0;
328 }
329
330 static int config_output(AVFilterLink *outlink)
331 {
332     AVFilterLink *inlink = outlink->src->inputs[0];
333
334     outlink->time_base.num = inlink->time_base.num;
335     outlink->time_base.den = inlink->time_base.den * 2;
336     outlink->frame_rate.num = inlink->frame_rate.num * 2;
337     outlink->frame_rate.den = inlink->frame_rate.den;
338
339     return 0;
340 }
341
342 /*
343  * Filter coefficients from PH-2071, scaled by 256 * 128.
344  * Each set of coefficients has a set for low-frequencies and high-frequencies.
345  * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
346  * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
347  * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
348  * and high-frequencies for simple and more-complex mode.
349  */
350 static const int8_t   n_coef_lf[2] = { 2, 4 };
351 static const int16_t coef_lf[2][4] = {{ 16384, 16384,     0,    0},
352                                       {  -852, 17236, 17236, -852}};
353 static const int8_t   n_coef_hf[2] = { 3, 5 };
354 static const int16_t coef_hf[2][5] = {{ -2048,  4096, -2048,     0,    0},
355                                       {  1016, -3801,  5570, -3801, 1016}};
356
357 typedef struct ThreadData {
358     AVFrame *out, *cur, *adj;
359     int plane;
360 } ThreadData;
361
362 static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
363 {
364     W3FDIFContext *s = ctx->priv;
365     ThreadData *td = arg;
366     AVFrame *out = td->out;
367     AVFrame *cur = td->cur;
368     AVFrame *adj = td->adj;
369     const int plane = td->plane;
370     const int filter = s->filter;
371     uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
372     uint8_t *out_line, *out_pixel;
373     int32_t *work_line, *work_pixel;
374     uint8_t *cur_data = cur->data[plane];
375     uint8_t *adj_data = adj->data[plane];
376     uint8_t *dst_data = out->data[plane];
377     const int linesize = s->linesize[plane];
378     const int height   = s->planeheight[plane];
379     const int cur_line_stride = cur->linesize[plane];
380     const int adj_line_stride = adj->linesize[plane];
381     const int dst_line_stride = out->linesize[plane];
382     const int start = (height * jobnr) / nb_jobs;
383     const int end = (height * (jobnr+1)) / nb_jobs;
384     const int max = s->max;
385     const int interlaced = cur->interlaced_frame;
386     const int tff = s->field == (s->parity == -1 ? interlaced ? cur->top_field_first : 1 :
387                                  s->parity ^ 1);
388     int j, y_in, y_out;
389
390     /* copy unchanged the lines of the field */
391     y_out = start + (tff ^ (start & 1));
392
393     in_line  = cur_data + (y_out * cur_line_stride);
394     out_line = dst_data + (y_out * dst_line_stride);
395
396     while (y_out < end) {
397         memcpy(out_line, in_line, linesize);
398         y_out += 2;
399         in_line  += cur_line_stride * 2;
400         out_line += dst_line_stride * 2;
401     }
402
403     /* interpolate other lines of the field */
404     y_out = start + ((!tff) ^ (start & 1));
405
406     out_line = dst_data + (y_out * dst_line_stride);
407
408     while (y_out < end) {
409         /* get low vertical frequencies from current field */
410         for (j = 0; j < n_coef_lf[filter]; j++) {
411             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
412
413             while (y_in < 0)
414                 y_in += 2;
415             while (y_in >= height)
416                 y_in -= 2;
417
418             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
419         }
420
421         work_line = s->work_line[jobnr];
422         switch (n_coef_lf[filter]) {
423         case 2:
424             s->dsp.filter_simple_low(work_line, in_lines_cur,
425                                      coef_lf[filter], linesize);
426             break;
427         case 4:
428             s->dsp.filter_complex_low(work_line, in_lines_cur,
429                                       coef_lf[filter], linesize);
430         }
431
432         /* get high vertical frequencies from adjacent fields */
433         for (j = 0; j < n_coef_hf[filter]; j++) {
434             y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
435
436             while (y_in < 0)
437                 y_in += 2;
438             while (y_in >= height)
439                 y_in -= 2;
440
441             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
442             in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
443         }
444
445         work_line = s->work_line[jobnr];
446         switch (n_coef_hf[filter]) {
447         case 3:
448             s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
449                                       coef_hf[filter], linesize);
450             break;
451         case 5:
452             s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
453                                        coef_hf[filter], linesize);
454         }
455
456         /* save scaled result to the output frame, scaling down by 256 * 128 */
457         work_pixel = s->work_line[jobnr];
458         out_pixel = out_line;
459
460         s->dsp.filter_scale(out_pixel, work_pixel, linesize, max);
461
462         /* move on to next line */
463         y_out += 2;
464         out_line += dst_line_stride * 2;
465     }
466
467     return 0;
468 }
469
470 static int filter(AVFilterContext *ctx, int is_second)
471 {
472     W3FDIFContext *s = ctx->priv;
473     AVFilterLink *outlink = ctx->outputs[0];
474     AVFrame *out, *adj;
475     ThreadData td;
476     int plane;
477
478     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
479     if (!out)
480         return AVERROR(ENOMEM);
481     av_frame_copy_props(out, s->cur);
482     out->interlaced_frame = 0;
483
484     if (!is_second) {
485         if (out->pts != AV_NOPTS_VALUE)
486             out->pts *= 2;
487     } else {
488         int64_t cur_pts  = s->cur->pts;
489         int64_t next_pts = s->next->pts;
490
491         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
492             out->pts = cur_pts + next_pts;
493         } else {
494             out->pts = AV_NOPTS_VALUE;
495         }
496     }
497
498     adj = s->field ? s->next : s->prev;
499     td.out = out; td.cur = s->cur; td.adj = adj;
500     for (plane = 0; plane < s->nb_planes; plane++) {
501         td.plane = plane;
502         ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
503     }
504
505     if (s->mode)
506         s->field = !s->field;
507
508     return ff_filter_frame(outlink, out);
509 }
510
511 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
512 {
513     AVFilterContext *ctx = inlink->dst;
514     W3FDIFContext *s = ctx->priv;
515     int ret;
516
517     av_frame_free(&s->prev);
518     s->prev = s->cur;
519     s->cur  = s->next;
520     s->next = frame;
521
522     if (!s->cur) {
523         s->cur = av_frame_clone(s->next);
524         if (!s->cur)
525             return AVERROR(ENOMEM);
526     }
527
528     if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
529         AVFrame *out = av_frame_clone(s->cur);
530         if (!out)
531             return AVERROR(ENOMEM);
532
533         av_frame_free(&s->prev);
534         if (out->pts != AV_NOPTS_VALUE)
535             out->pts *= 2;
536         return ff_filter_frame(ctx->outputs[0], out);
537     }
538
539     if (!s->prev)
540         return 0;
541
542     ret = filter(ctx, 0);
543     if (ret < 0 || s->mode == 0)
544         return ret;
545
546     return filter(ctx, 1);
547 }
548
549 static int request_frame(AVFilterLink *outlink)
550 {
551     AVFilterContext *ctx = outlink->src;
552     W3FDIFContext *s = ctx->priv;
553     int ret;
554
555     if (s->eof)
556         return AVERROR_EOF;
557
558     ret = ff_request_frame(ctx->inputs[0]);
559
560     if (ret == AVERROR_EOF && s->cur) {
561         AVFrame *next = av_frame_clone(s->next);
562         if (!next)
563             return AVERROR(ENOMEM);
564         next->pts = s->next->pts * 2 - s->cur->pts;
565         filter_frame(ctx->inputs[0], next);
566         s->eof = 1;
567     } else if (ret < 0) {
568         return ret;
569     }
570
571     return 0;
572 }
573
574 static av_cold void uninit(AVFilterContext *ctx)
575 {
576     W3FDIFContext *s = ctx->priv;
577     int i;
578
579     av_frame_free(&s->prev);
580     av_frame_free(&s->cur );
581     av_frame_free(&s->next);
582
583     for (i = 0; i < s->nb_threads; i++)
584         av_freep(&s->work_line[i]);
585
586     av_freep(&s->work_line);
587 }
588
589 static const AVFilterPad w3fdif_inputs[] = {
590     {
591         .name          = "default",
592         .type          = AVMEDIA_TYPE_VIDEO,
593         .filter_frame  = filter_frame,
594         .config_props  = config_input,
595     },
596     { NULL }
597 };
598
599 static const AVFilterPad w3fdif_outputs[] = {
600     {
601         .name          = "default",
602         .type          = AVMEDIA_TYPE_VIDEO,
603         .config_props  = config_output,
604         .request_frame = request_frame,
605     },
606     { NULL }
607 };
608
609 AVFilter ff_vf_w3fdif = {
610     .name          = "w3fdif",
611     .description   = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
612     .priv_size     = sizeof(W3FDIFContext),
613     .priv_class    = &w3fdif_class,
614     .uninit        = uninit,
615     .query_formats = query_formats,
616     .inputs        = w3fdif_inputs,
617     .outputs       = w3fdif_outputs,
618     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
619     .process_command = ff_filter_process_command,
620 };