]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_w3fdif.c
avfilter/vf_w3fdif: not interlaced frames are filtered like tff
[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_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
89         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
90         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
91         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
92         AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
93         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
94         AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
95         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
96         AV_PIX_FMT_GBRAP10,   AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
97         AV_PIX_FMT_NONE
98     };
99
100     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
101     if (!fmts_list)
102         return AVERROR(ENOMEM);
103     return ff_set_common_formats(ctx, fmts_list);
104 }
105
106 static void filter_simple_low(int32_t *work_line,
107                               uint8_t *in_lines_cur[2],
108                               const int16_t *coef, int linesize)
109 {
110     int i;
111
112     for (i = 0; i < linesize; i++) {
113         *work_line    = *in_lines_cur[0]++ * coef[0];
114         *work_line++ += *in_lines_cur[1]++ * coef[1];
115     }
116 }
117
118 static void filter_complex_low(int32_t *work_line,
119                                uint8_t *in_lines_cur[4],
120                                const int16_t *coef, int linesize)
121 {
122     int i;
123
124     for (i = 0; i < linesize; i++) {
125         *work_line    = *in_lines_cur[0]++ * coef[0];
126         *work_line   += *in_lines_cur[1]++ * coef[1];
127         *work_line   += *in_lines_cur[2]++ * coef[2];
128         *work_line++ += *in_lines_cur[3]++ * coef[3];
129     }
130 }
131
132 static void filter_simple_high(int32_t *work_line,
133                                uint8_t *in_lines_cur[3],
134                                uint8_t *in_lines_adj[3],
135                                const int16_t *coef, int linesize)
136 {
137     int i;
138
139     for (i = 0; i < linesize; i++) {
140         *work_line   += *in_lines_cur[0]++ * coef[0];
141         *work_line   += *in_lines_adj[0]++ * coef[0];
142         *work_line   += *in_lines_cur[1]++ * coef[1];
143         *work_line   += *in_lines_adj[1]++ * coef[1];
144         *work_line   += *in_lines_cur[2]++ * coef[2];
145         *work_line++ += *in_lines_adj[2]++ * coef[2];
146     }
147 }
148
149 static void filter_complex_high(int32_t *work_line,
150                                 uint8_t *in_lines_cur[5],
151                                 uint8_t *in_lines_adj[5],
152                                 const int16_t *coef, int linesize)
153 {
154     int i;
155
156     for (i = 0; i < linesize; i++) {
157         *work_line   += *in_lines_cur[0]++ * coef[0];
158         *work_line   += *in_lines_adj[0]++ * coef[0];
159         *work_line   += *in_lines_cur[1]++ * coef[1];
160         *work_line   += *in_lines_adj[1]++ * coef[1];
161         *work_line   += *in_lines_cur[2]++ * coef[2];
162         *work_line   += *in_lines_adj[2]++ * coef[2];
163         *work_line   += *in_lines_cur[3]++ * coef[3];
164         *work_line   += *in_lines_adj[3]++ * coef[3];
165         *work_line   += *in_lines_cur[4]++ * coef[4];
166         *work_line++ += *in_lines_adj[4]++ * coef[4];
167     }
168 }
169
170 static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
171 {
172     int j;
173
174     for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
175         *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
176 }
177
178 static void filter16_simple_low(int32_t *work_line,
179                                 uint8_t *in_lines_cur8[2],
180                                 const int16_t *coef, int linesize)
181 {
182     uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] };
183     int i;
184
185     linesize /= 2;
186     for (i = 0; i < linesize; i++) {
187         *work_line    = *in_lines_cur[0]++ * coef[0];
188         *work_line++ += *in_lines_cur[1]++ * coef[1];
189     }
190 }
191
192 static void filter16_complex_low(int32_t *work_line,
193                                  uint8_t *in_lines_cur8[4],
194                                  const int16_t *coef, int linesize)
195 {
196     uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0],
197                                   (uint16_t *)in_lines_cur8[1],
198                                   (uint16_t *)in_lines_cur8[2],
199                                   (uint16_t *)in_lines_cur8[3] };
200     int i;
201
202     linesize /= 2;
203     for (i = 0; i < linesize; i++) {
204         *work_line    = *in_lines_cur[0]++ * coef[0];
205         *work_line   += *in_lines_cur[1]++ * coef[1];
206         *work_line   += *in_lines_cur[2]++ * coef[2];
207         *work_line++ += *in_lines_cur[3]++ * coef[3];
208     }
209 }
210
211 static void filter16_simple_high(int32_t *work_line,
212                                  uint8_t *in_lines_cur8[3],
213                                  uint8_t *in_lines_adj8[3],
214                                  const int16_t *coef, int linesize)
215 {
216     uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0],
217                                   (uint16_t *)in_lines_cur8[1],
218                                   (uint16_t *)in_lines_cur8[2] };
219     uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0],
220                                   (uint16_t *)in_lines_adj8[1],
221                                   (uint16_t *)in_lines_adj8[2] };
222     int i;
223
224     linesize /= 2;
225     for (i = 0; i < linesize; i++) {
226         *work_line   += *in_lines_cur[0]++ * coef[0];
227         *work_line   += *in_lines_adj[0]++ * coef[0];
228         *work_line   += *in_lines_cur[1]++ * coef[1];
229         *work_line   += *in_lines_adj[1]++ * coef[1];
230         *work_line   += *in_lines_cur[2]++ * coef[2];
231         *work_line++ += *in_lines_adj[2]++ * coef[2];
232     }
233 }
234
235 static void filter16_complex_high(int32_t *work_line,
236                                   uint8_t *in_lines_cur8[5],
237                                   uint8_t *in_lines_adj8[5],
238                                   const int16_t *coef, int linesize)
239 {
240     uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0],
241                                   (uint16_t *)in_lines_cur8[1],
242                                   (uint16_t *)in_lines_cur8[2],
243                                   (uint16_t *)in_lines_cur8[3],
244                                   (uint16_t *)in_lines_cur8[4] };
245     uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0],
246                                   (uint16_t *)in_lines_adj8[1],
247                                   (uint16_t *)in_lines_adj8[2],
248                                   (uint16_t *)in_lines_adj8[3],
249                                   (uint16_t *)in_lines_adj8[4] };
250     int i;
251
252     linesize /= 2;
253     for (i = 0; i < linesize; i++) {
254         *work_line   += *in_lines_cur[0]++ * coef[0];
255         *work_line   += *in_lines_adj[0]++ * coef[0];
256         *work_line   += *in_lines_cur[1]++ * coef[1];
257         *work_line   += *in_lines_adj[1]++ * coef[1];
258         *work_line   += *in_lines_cur[2]++ * coef[2];
259         *work_line   += *in_lines_adj[2]++ * coef[2];
260         *work_line   += *in_lines_cur[3]++ * coef[3];
261         *work_line   += *in_lines_adj[3]++ * coef[3];
262         *work_line   += *in_lines_cur[4]++ * coef[4];
263         *work_line++ += *in_lines_adj[4]++ * coef[4];
264     }
265 }
266
267 static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max)
268 {
269     uint16_t *out_pixel = (uint16_t *)out_pixel8;
270     int j;
271
272     linesize /= 2;
273     for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
274         *out_pixel = av_clip(*work_pixel, 0, max) >> 15;
275 }
276
277 static int config_input(AVFilterLink *inlink)
278 {
279     AVFilterContext *ctx = inlink->dst;
280     W3FDIFContext *s = ctx->priv;
281     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
282     int ret, i, depth;
283
284     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
285         return ret;
286
287     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
288     s->planeheight[0] = s->planeheight[3] = inlink->h;
289
290     if (inlink->h < 3) {
291         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
292         return AVERROR(EINVAL);
293     }
294
295     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
296     s->nb_threads = ff_filter_get_nb_threads(ctx);
297     s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
298     if (!s->work_line)
299         return AVERROR(ENOMEM);
300
301     for (i = 0; i < s->nb_threads; i++) {
302         s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
303         if (!s->work_line[i])
304             return AVERROR(ENOMEM);
305     }
306
307     depth = desc->comp[0].depth;
308     s->max = ((1 << depth) - 1) * 256 * 128;
309     if (depth <= 8) {
310         s->dsp.filter_simple_low   = filter_simple_low;
311         s->dsp.filter_complex_low  = filter_complex_low;
312         s->dsp.filter_simple_high  = filter_simple_high;
313         s->dsp.filter_complex_high = filter_complex_high;
314         s->dsp.filter_scale        = filter_scale;
315     } else {
316         s->dsp.filter_simple_low   = filter16_simple_low;
317         s->dsp.filter_complex_low  = filter16_complex_low;
318         s->dsp.filter_simple_high  = filter16_simple_high;
319         s->dsp.filter_complex_high = filter16_complex_high;
320         s->dsp.filter_scale        = filter16_scale;
321     }
322
323     if (ARCH_X86)
324         ff_w3fdif_init_x86(&s->dsp, depth);
325
326     return 0;
327 }
328
329 static int config_output(AVFilterLink *outlink)
330 {
331     AVFilterLink *inlink = outlink->src->inputs[0];
332
333     outlink->time_base.num = inlink->time_base.num;
334     outlink->time_base.den = inlink->time_base.den * 2;
335     outlink->frame_rate.num = inlink->frame_rate.num * 2;
336     outlink->frame_rate.den = inlink->frame_rate.den;
337
338     return 0;
339 }
340
341 /*
342  * Filter coefficients from PH-2071, scaled by 256 * 128.
343  * Each set of coefficients has a set for low-frequencies and high-frequencies.
344  * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
345  * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
346  * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
347  * and high-frequencies for simple and more-complex mode.
348  */
349 static const int8_t   n_coef_lf[2] = { 2, 4 };
350 static const int16_t coef_lf[2][4] = {{ 16384, 16384,     0,    0},
351                                       {  -852, 17236, 17236, -852}};
352 static const int8_t   n_coef_hf[2] = { 3, 5 };
353 static const int16_t coef_hf[2][5] = {{ -2048,  4096, -2048,     0,    0},
354                                       {  1016, -3801,  5570, -3801, 1016}};
355
356 typedef struct ThreadData {
357     AVFrame *out, *cur, *adj;
358     int plane;
359 } ThreadData;
360
361 static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
362 {
363     W3FDIFContext *s = ctx->priv;
364     ThreadData *td = arg;
365     AVFrame *out = td->out;
366     AVFrame *cur = td->cur;
367     AVFrame *adj = td->adj;
368     const int plane = td->plane;
369     const int filter = s->filter;
370     uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
371     uint8_t *out_line, *out_pixel;
372     int32_t *work_line, *work_pixel;
373     uint8_t *cur_data = cur->data[plane];
374     uint8_t *adj_data = adj->data[plane];
375     uint8_t *dst_data = out->data[plane];
376     const int linesize = s->linesize[plane];
377     const int height   = s->planeheight[plane];
378     const int cur_line_stride = cur->linesize[plane];
379     const int adj_line_stride = adj->linesize[plane];
380     const int dst_line_stride = out->linesize[plane];
381     const int start = (height * jobnr) / nb_jobs;
382     const int end = (height * (jobnr+1)) / nb_jobs;
383     const int max = s->max;
384     const int tff = (s->field == (s->parity == -1 ? cur->top_field_first == cur->interlaced_frame : s->parity == 0 ? !cur->interlaced_frame : cur->interlaced_frame));
385     int j, y_in, y_out;
386
387     /* copy unchanged the lines of the field */
388     y_out = start + (tff ^ (start & 1));
389
390     in_line  = cur_data + (y_out * cur_line_stride);
391     out_line = dst_data + (y_out * dst_line_stride);
392
393     while (y_out < end) {
394         memcpy(out_line, in_line, linesize);
395         y_out += 2;
396         in_line  += cur_line_stride * 2;
397         out_line += dst_line_stride * 2;
398     }
399
400     /* interpolate other lines of the field */
401     y_out = start + ((!tff) ^ (start & 1));
402
403     out_line = dst_data + (y_out * dst_line_stride);
404
405     while (y_out < end) {
406         /* get low vertical frequencies from current field */
407         for (j = 0; j < n_coef_lf[filter]; j++) {
408             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
409
410             while (y_in < 0)
411                 y_in += 2;
412             while (y_in >= height)
413                 y_in -= 2;
414
415             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
416         }
417
418         work_line = s->work_line[jobnr];
419         switch (n_coef_lf[filter]) {
420         case 2:
421             s->dsp.filter_simple_low(work_line, in_lines_cur,
422                                      coef_lf[filter], linesize);
423             break;
424         case 4:
425             s->dsp.filter_complex_low(work_line, in_lines_cur,
426                                       coef_lf[filter], linesize);
427         }
428
429         /* get high vertical frequencies from adjacent fields */
430         for (j = 0; j < n_coef_hf[filter]; j++) {
431             y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
432
433             while (y_in < 0)
434                 y_in += 2;
435             while (y_in >= height)
436                 y_in -= 2;
437
438             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
439             in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
440         }
441
442         work_line = s->work_line[jobnr];
443         switch (n_coef_hf[filter]) {
444         case 3:
445             s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
446                                       coef_hf[filter], linesize);
447             break;
448         case 5:
449             s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
450                                        coef_hf[filter], linesize);
451         }
452
453         /* save scaled result to the output frame, scaling down by 256 * 128 */
454         work_pixel = s->work_line[jobnr];
455         out_pixel = out_line;
456
457         s->dsp.filter_scale(out_pixel, work_pixel, linesize, max);
458
459         /* move on to next line */
460         y_out += 2;
461         out_line += dst_line_stride * 2;
462     }
463
464     return 0;
465 }
466
467 static int filter(AVFilterContext *ctx, int is_second)
468 {
469     W3FDIFContext *s = ctx->priv;
470     AVFilterLink *outlink = ctx->outputs[0];
471     AVFrame *out, *adj;
472     ThreadData td;
473     int plane;
474
475     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
476     if (!out)
477         return AVERROR(ENOMEM);
478     av_frame_copy_props(out, s->cur);
479     out->interlaced_frame = 0;
480
481     if (!is_second) {
482         if (out->pts != AV_NOPTS_VALUE)
483             out->pts *= 2;
484     } else {
485         int64_t cur_pts  = s->cur->pts;
486         int64_t next_pts = s->next->pts;
487
488         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
489             out->pts = cur_pts + next_pts;
490         } else {
491             out->pts = AV_NOPTS_VALUE;
492         }
493     }
494
495     adj = s->field ? s->next : s->prev;
496     td.out = out; td.cur = s->cur; td.adj = adj;
497     for (plane = 0; plane < s->nb_planes; plane++) {
498         td.plane = plane;
499         ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
500     }
501
502     if (s->mode)
503         s->field = !s->field;
504
505     return ff_filter_frame(outlink, out);
506 }
507
508 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
509 {
510     AVFilterContext *ctx = inlink->dst;
511     W3FDIFContext *s = ctx->priv;
512     int ret;
513
514     av_frame_free(&s->prev);
515     s->prev = s->cur;
516     s->cur  = s->next;
517     s->next = frame;
518
519     if (!s->cur) {
520         s->cur = av_frame_clone(s->next);
521         if (!s->cur)
522             return AVERROR(ENOMEM);
523     }
524
525     if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
526         AVFrame *out = av_frame_clone(s->cur);
527         if (!out)
528             return AVERROR(ENOMEM);
529
530         av_frame_free(&s->prev);
531         if (out->pts != AV_NOPTS_VALUE)
532             out->pts *= 2;
533         return ff_filter_frame(ctx->outputs[0], out);
534     }
535
536     if (!s->prev)
537         return 0;
538
539     ret = filter(ctx, 0);
540     if (ret < 0 || s->mode == 0)
541         return ret;
542
543     return filter(ctx, 1);
544 }
545
546 static int request_frame(AVFilterLink *outlink)
547 {
548     AVFilterContext *ctx = outlink->src;
549     W3FDIFContext *s = ctx->priv;
550     int ret;
551
552     if (s->eof)
553         return AVERROR_EOF;
554
555     ret = ff_request_frame(ctx->inputs[0]);
556
557     if (ret == AVERROR_EOF && s->cur) {
558         AVFrame *next = av_frame_clone(s->next);
559         if (!next)
560             return AVERROR(ENOMEM);
561         next->pts = s->next->pts * 2 - s->cur->pts;
562         filter_frame(ctx->inputs[0], next);
563         s->eof = 1;
564     } else if (ret < 0) {
565         return ret;
566     }
567
568     return 0;
569 }
570
571 static av_cold void uninit(AVFilterContext *ctx)
572 {
573     W3FDIFContext *s = ctx->priv;
574     int i;
575
576     av_frame_free(&s->prev);
577     av_frame_free(&s->cur );
578     av_frame_free(&s->next);
579
580     for (i = 0; i < s->nb_threads; i++)
581         av_freep(&s->work_line[i]);
582
583     av_freep(&s->work_line);
584 }
585
586 static const AVFilterPad w3fdif_inputs[] = {
587     {
588         .name          = "default",
589         .type          = AVMEDIA_TYPE_VIDEO,
590         .filter_frame  = filter_frame,
591         .config_props  = config_input,
592     },
593     { NULL }
594 };
595
596 static const AVFilterPad w3fdif_outputs[] = {
597     {
598         .name          = "default",
599         .type          = AVMEDIA_TYPE_VIDEO,
600         .config_props  = config_output,
601         .request_frame = request_frame,
602     },
603     { NULL }
604 };
605
606 AVFilter ff_vf_w3fdif = {
607     .name          = "w3fdif",
608     .description   = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
609     .priv_size     = sizeof(W3FDIFContext),
610     .priv_class    = &w3fdif_class,
611     .uninit        = uninit,
612     .query_formats = query_formats,
613     .inputs        = w3fdif_inputs,
614     .outputs       = w3fdif_outputs,
615     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
616     .process_command = ff_filter_process_command,
617 };