]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_w3fdif.c
avfilter/vf_w3fdif: add support for >8 depth gray formats
[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 tff = (s->field == (s->parity == -1 ? cur->top_field_first == cur->interlaced_frame : s->parity == 0 ? !cur->interlaced_frame : cur->interlaced_frame));
386     int j, y_in, y_out;
387
388     /* copy unchanged the lines of the field */
389     y_out = start + (tff ^ (start & 1));
390
391     in_line  = cur_data + (y_out * cur_line_stride);
392     out_line = dst_data + (y_out * dst_line_stride);
393
394     while (y_out < end) {
395         memcpy(out_line, in_line, linesize);
396         y_out += 2;
397         in_line  += cur_line_stride * 2;
398         out_line += dst_line_stride * 2;
399     }
400
401     /* interpolate other lines of the field */
402     y_out = start + ((!tff) ^ (start & 1));
403
404     out_line = dst_data + (y_out * dst_line_stride);
405
406     while (y_out < end) {
407         /* get low vertical frequencies from current field */
408         for (j = 0; j < n_coef_lf[filter]; j++) {
409             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
410
411             while (y_in < 0)
412                 y_in += 2;
413             while (y_in >= height)
414                 y_in -= 2;
415
416             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
417         }
418
419         work_line = s->work_line[jobnr];
420         switch (n_coef_lf[filter]) {
421         case 2:
422             s->dsp.filter_simple_low(work_line, in_lines_cur,
423                                      coef_lf[filter], linesize);
424             break;
425         case 4:
426             s->dsp.filter_complex_low(work_line, in_lines_cur,
427                                       coef_lf[filter], linesize);
428         }
429
430         /* get high vertical frequencies from adjacent fields */
431         for (j = 0; j < n_coef_hf[filter]; j++) {
432             y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
433
434             while (y_in < 0)
435                 y_in += 2;
436             while (y_in >= height)
437                 y_in -= 2;
438
439             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
440             in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
441         }
442
443         work_line = s->work_line[jobnr];
444         switch (n_coef_hf[filter]) {
445         case 3:
446             s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
447                                       coef_hf[filter], linesize);
448             break;
449         case 5:
450             s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
451                                        coef_hf[filter], linesize);
452         }
453
454         /* save scaled result to the output frame, scaling down by 256 * 128 */
455         work_pixel = s->work_line[jobnr];
456         out_pixel = out_line;
457
458         s->dsp.filter_scale(out_pixel, work_pixel, linesize, max);
459
460         /* move on to next line */
461         y_out += 2;
462         out_line += dst_line_stride * 2;
463     }
464
465     return 0;
466 }
467
468 static int filter(AVFilterContext *ctx, int is_second)
469 {
470     W3FDIFContext *s = ctx->priv;
471     AVFilterLink *outlink = ctx->outputs[0];
472     AVFrame *out, *adj;
473     ThreadData td;
474     int plane;
475
476     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
477     if (!out)
478         return AVERROR(ENOMEM);
479     av_frame_copy_props(out, s->cur);
480     out->interlaced_frame = 0;
481
482     if (!is_second) {
483         if (out->pts != AV_NOPTS_VALUE)
484             out->pts *= 2;
485     } else {
486         int64_t cur_pts  = s->cur->pts;
487         int64_t next_pts = s->next->pts;
488
489         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
490             out->pts = cur_pts + next_pts;
491         } else {
492             out->pts = AV_NOPTS_VALUE;
493         }
494     }
495
496     adj = s->field ? s->next : s->prev;
497     td.out = out; td.cur = s->cur; td.adj = adj;
498     for (plane = 0; plane < s->nb_planes; plane++) {
499         td.plane = plane;
500         ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
501     }
502
503     if (s->mode)
504         s->field = !s->field;
505
506     return ff_filter_frame(outlink, out);
507 }
508
509 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
510 {
511     AVFilterContext *ctx = inlink->dst;
512     W3FDIFContext *s = ctx->priv;
513     int ret;
514
515     av_frame_free(&s->prev);
516     s->prev = s->cur;
517     s->cur  = s->next;
518     s->next = frame;
519
520     if (!s->cur) {
521         s->cur = av_frame_clone(s->next);
522         if (!s->cur)
523             return AVERROR(ENOMEM);
524     }
525
526     if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
527         AVFrame *out = av_frame_clone(s->cur);
528         if (!out)
529             return AVERROR(ENOMEM);
530
531         av_frame_free(&s->prev);
532         if (out->pts != AV_NOPTS_VALUE)
533             out->pts *= 2;
534         return ff_filter_frame(ctx->outputs[0], out);
535     }
536
537     if (!s->prev)
538         return 0;
539
540     ret = filter(ctx, 0);
541     if (ret < 0 || s->mode == 0)
542         return ret;
543
544     return filter(ctx, 1);
545 }
546
547 static int request_frame(AVFilterLink *outlink)
548 {
549     AVFilterContext *ctx = outlink->src;
550     W3FDIFContext *s = ctx->priv;
551     int ret;
552
553     if (s->eof)
554         return AVERROR_EOF;
555
556     ret = ff_request_frame(ctx->inputs[0]);
557
558     if (ret == AVERROR_EOF && s->cur) {
559         AVFrame *next = av_frame_clone(s->next);
560         if (!next)
561             return AVERROR(ENOMEM);
562         next->pts = s->next->pts * 2 - s->cur->pts;
563         filter_frame(ctx->inputs[0], next);
564         s->eof = 1;
565     } else if (ret < 0) {
566         return ret;
567     }
568
569     return 0;
570 }
571
572 static av_cold void uninit(AVFilterContext *ctx)
573 {
574     W3FDIFContext *s = ctx->priv;
575     int i;
576
577     av_frame_free(&s->prev);
578     av_frame_free(&s->cur );
579     av_frame_free(&s->next);
580
581     for (i = 0; i < s->nb_threads; i++)
582         av_freep(&s->work_line[i]);
583
584     av_freep(&s->work_line);
585 }
586
587 static const AVFilterPad w3fdif_inputs[] = {
588     {
589         .name          = "default",
590         .type          = AVMEDIA_TYPE_VIDEO,
591         .filter_frame  = filter_frame,
592         .config_props  = config_input,
593     },
594     { NULL }
595 };
596
597 static const AVFilterPad w3fdif_outputs[] = {
598     {
599         .name          = "default",
600         .type          = AVMEDIA_TYPE_VIDEO,
601         .config_props  = config_output,
602         .request_frame = request_frame,
603     },
604     { NULL }
605 };
606
607 AVFilter ff_vf_w3fdif = {
608     .name          = "w3fdif",
609     .description   = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
610     .priv_size     = sizeof(W3FDIFContext),
611     .priv_class    = &w3fdif_class,
612     .uninit        = uninit,
613     .query_formats = query_formats,
614     .inputs        = w3fdif_inputs,
615     .outputs       = w3fdif_outputs,
616     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
617     .process_command = ff_filter_process_command,
618 };