Browse code

avfilter: add BobWeaver deinterlacing filter

Thomas Mundt authored on 2016/02/08 09:00:42
Showing 6 changed files
... ...
@@ -5,6 +5,7 @@ version <next>:
5 5
 - DXVA2-accelerated HEVC Main10 decoding
6 6
 - fieldhint filter
7 7
 - loop video filter and aloop audio filter
8
+- Bob Weaver deinterlacing filter
8 9
 
9 10
 
10 11
 version 3.0:
... ...
@@ -4256,6 +4256,59 @@ tblend=all_mode=difference128
4256 4256
 @end example
4257 4257
 @end itemize
4258 4258
 
4259
+@section bwdif
4260
+
4261
+Deinterlace the input video ("bwdif" stands for "Bob Weaver
4262
+Deinterlacing Filter").
4263
+
4264
+Motion adaptive deinterlacing based on yadif with the use of w3fdif and cubic
4265
+interpolation algorithms.
4266
+It accepts the following parameters:
4267
+
4268
+@table @option
4269
+@item mode
4270
+The interlacing mode to adopt. It accepts one of the following values:
4271
+
4272
+@table @option
4273
+@item 0, send_frame
4274
+Output one frame for each frame.
4275
+@item 1, send_field
4276
+Output one frame for each field.
4277
+@end table
4278
+
4279
+The default value is @code{send_field}.
4280
+
4281
+@item parity
4282
+The picture field parity assumed for the input interlaced video. It accepts one
4283
+of the following values:
4284
+
4285
+@table @option
4286
+@item 0, tff
4287
+Assume the top field is first.
4288
+@item 1, bff
4289
+Assume the bottom field is first.
4290
+@item -1, auto
4291
+Enable automatic detection of field parity.
4292
+@end table
4293
+
4294
+The default value is @code{auto}.
4295
+If the interlacing is unknown or the decoder does not export this information,
4296
+top field first will be assumed.
4297
+
4298
+@item deint
4299
+Specify which frames to deinterlace. Accept one of the following
4300
+values:
4301
+
4302
+@table @option
4303
+@item 0, all
4304
+Deinterlace all frames.
4305
+@item 1, interlaced
4306
+Only deinterlace frames marked as interlaced.
4307
+@end table
4308
+
4309
+The default value is @code{all}.
4310
+@end table
4311
+
4259 4312
 @section boxblur
4260 4313
 
4261 4314
 Apply a boxblur algorithm to the input video.
... ...
@@ -119,6 +119,7 @@ OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
119 119
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
120 120
 OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o dualinput.o framesync.o
121 121
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
122
+OBJS-$(CONFIG_BWDIF_FILTER)                  += vf_bwdif.o
122 123
 OBJS-$(CONFIG_CHROMAKEY_FILTER)              += vf_chromakey.o
123 124
 OBJS-$(CONFIG_CODECVIEW_FILTER)              += vf_codecview.o
124 125
 OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
... ...
@@ -140,6 +140,7 @@ void avfilter_register_all(void)
140 140
     REGISTER_FILTER(BLACKFRAME,     blackframe,     vf);
141 141
     REGISTER_FILTER(BLEND,          blend,          vf);
142 142
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
143
+    REGISTER_FILTER(BWDIF,          bwdif,          vf);
143 144
     REGISTER_FILTER(CHROMAKEY,      chromakey,      vf);
144 145
     REGISTER_FILTER(CODECVIEW,      codecview,      vf);
145 146
     REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  33
33
+#define LIBAVFILTER_VERSION_MINOR  34
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,625 @@
0
+/*
1
+ * BobWeaver Deinterlacing Filter
2
+ * Copyright (C) 2016 Thomas Mundt <loudmax@yahoo.de>
3
+ *
4
+ * Based on YADIF (Yet Another Deinterlacing Filter)
5
+ * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
6
+ *               2010      James Darnley <james.darnley@gmail.com>
7
+ *
8
+ * With use of Weston 3 Field Deinterlacing Filter algorithm
9
+ * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
10
+ * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
11
+ * Based on the process described by Martin Weston for BBC R&D
12
+ *
13
+ * This file is part of FFmpeg.
14
+ *
15
+ * FFmpeg is free software; you can redistribute it and/or
16
+ * modify it under the terms of the GNU Lesser General Public
17
+ * License as published by the Free Software Foundation; either
18
+ * version 2.1 of the License, or (at your option) any later version.
19
+ *
20
+ * FFmpeg is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
+ * Lesser General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU Lesser General Public
26
+ * License along with FFmpeg; if not, write to the Free Software
27
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28
+ */
29
+
30
+#include "libavutil/avassert.h"
31
+#include "libavutil/common.h"
32
+#include "libavutil/opt.h"
33
+#include "libavutil/pixdesc.h"
34
+#include "libavutil/imgutils.h"
35
+#include "avfilter.h"
36
+#include "formats.h"
37
+#include "internal.h"
38
+#include "video.h"
39
+
40
+/*
41
+ * Filter coefficients coef_lf and coef_hf taken from BBC PH-2071 (Weston 3 Field Deinterlacer).
42
+ * Used when there is spatial and temporal interpolation.
43
+ * Filter coefficients coef_sp are used when there is spatial interpolation only.
44
+ * Adjusted for matching visual sharpness impression of spatial and temporal interpolation.
45
+ */
46
+static const uint16_t coef_lf[2] = { 4309, 213 };
47
+static const uint16_t coef_hf[3] = { 5570, 3801, 1016 };
48
+static const uint16_t coef_sp[2] = { 5077, 981 };
49
+
50
+enum BWDIFMode {
51
+    BWDIF_MODE_SEND_FRAME = 0, ///< send 1 frame for each frame
52
+    BWDIF_MODE_SEND_FIELD = 1, ///< send 1 frame for each field
53
+};
54
+
55
+enum BWDIFParity {
56
+    BWDIF_PARITY_TFF  =  0, ///< top field first
57
+    BWDIF_PARITY_BFF  =  1, ///< bottom field first
58
+    BWDIF_PARITY_AUTO = -1, ///< auto detection
59
+};
60
+
61
+enum BWDIFDeint {
62
+    BWDIF_DEINT_ALL        = 0, ///< deinterlace all frames
63
+    BWDIF_DEINT_INTERLACED = 1, ///< only deinterlace frames marked as interlaced
64
+};
65
+
66
+typedef struct BWDIFContext {
67
+    const AVClass *class;
68
+
69
+    int mode;           ///< BWDIFMode
70
+    int parity;         ///< BWDIFParity
71
+    int deint;          ///< BWDIFDeint
72
+
73
+    int frame_pending;
74
+
75
+    AVFrame *cur;
76
+    AVFrame *next;
77
+    AVFrame *prev;
78
+    AVFrame *out;
79
+
80
+    void (*filter_intra)(void *dst1, void *cur1, int w, int prefs, int mrefs,
81
+                         int prefs3, int mrefs3, int parity, int clip_max);
82
+    void (*filter_line)(void *dst, void *prev, void *cur, void *next,
83
+                        int w, int prefs, int mrefs, int prefs2, int mrefs2,
84
+                        int prefs3, int mrefs3, int prefs4, int mrefs4,
85
+                        int parity, int clip_max);
86
+    void (*filter_edge)(void *dst, void *prev, void *cur, void *next,
87
+                        int w, int prefs, int mrefs, int prefs2, int mrefs2,
88
+                        int parity, int clip_max, int spat);
89
+
90
+    const AVPixFmtDescriptor *csp;
91
+    int inter_field;
92
+    int eof;
93
+} BWDIFContext;
94
+
95
+typedef struct ThreadData {
96
+    AVFrame *frame;
97
+    int plane;
98
+    int w, h;
99
+    int parity;
100
+    int tff;
101
+} ThreadData;
102
+
103
+#define FILTER_INTRA() \
104
+    for (x = 0; x < w; x++) { \
105
+        interpol = (coef_sp[0] * (cur[mrefs] + cur[prefs]) - coef_sp[1] * (cur[mrefs3] + cur[prefs3])) >> 13; \
106
+        dst[0] = av_clip(interpol, 0, clip_max); \
107
+ \
108
+        dst++; \
109
+        cur++; \
110
+    }
111
+
112
+#define FILTER1() \
113
+    for (x = 0; x < w; x++) { \
114
+        int c = cur[mrefs]; \
115
+        int d = (prev2[0] + next2[0]) >> 1; \
116
+        int e = cur[prefs]; \
117
+        int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
118
+        int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e)) >> 1; \
119
+        int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e)) >> 1; \
120
+        int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
121
+ \
122
+        if (!diff) { \
123
+            dst[0] = d; \
124
+        } else {
125
+
126
+#define SPAT_CHECK() \
127
+            int b = ((prev2[mrefs2] + next2[mrefs2]) >> 1) - c; \
128
+            int f = ((prev2[prefs2] + next2[prefs2]) >> 1) - e; \
129
+            int dc = d - c; \
130
+            int de = d - e; \
131
+            int max = FFMAX3(de, dc, FFMIN(b, f)); \
132
+            int min = FFMIN3(de, dc, FFMAX(b, f)); \
133
+            diff = FFMAX3(diff, min, -max);
134
+
135
+#define FILTER_LINE() \
136
+            SPAT_CHECK() \
137
+            if (FFABS(c - e) > temporal_diff0) { \
138
+                interpol = (((coef_hf[0] * (prev2[0] + next2[0]) \
139
+                    - coef_hf[1] * (prev2[mrefs2] + next2[mrefs2] + prev2[prefs2] + next2[prefs2]) \
140
+                    + coef_hf[2] * (prev2[mrefs4] + next2[mrefs4] + prev2[prefs4] + next2[prefs4])) >> 2) \
141
+                    + coef_lf[0] * (c + e) - coef_lf[1] * (cur[mrefs3] + cur[prefs3])) >> 13; \
142
+            } else { \
143
+                interpol = (coef_sp[0] * (c + e) - coef_sp[1] * (cur[mrefs3] + cur[prefs3])) >> 13; \
144
+            }
145
+
146
+#define FILTER_EDGE() \
147
+            if (spat) { \
148
+                SPAT_CHECK() \
149
+            } \
150
+            interpol = (c + e) >> 1;
151
+
152
+#define FILTER2() \
153
+            if (interpol > d + diff) \
154
+                interpol = d + diff; \
155
+            else if (interpol < d - diff) \
156
+                interpol = d - diff; \
157
+ \
158
+            dst[0] = av_clip(interpol, 0, clip_max); \
159
+        } \
160
+ \
161
+        dst++; \
162
+        cur++; \
163
+        prev++; \
164
+        next++; \
165
+        prev2++; \
166
+        next2++; \
167
+    }
168
+
169
+static void filter_intra(void *dst1, void *cur1, int w, int prefs, int mrefs,
170
+                         int prefs3, int mrefs3, int parity, int clip_max)
171
+{
172
+    uint8_t *dst = dst1;
173
+    uint8_t *cur = cur1;
174
+    int interpol, x;
175
+
176
+    FILTER_INTRA()
177
+}
178
+
179
+static void filter_line(void *dst1, void *prev1, void *cur1, void *next1,
180
+                        int w, int prefs, int mrefs, int prefs2, int mrefs2,
181
+                        int prefs3, int mrefs3, int prefs4, int mrefs4,
182
+                        int parity, int clip_max)
183
+{
184
+    uint8_t *dst   = dst1;
185
+    uint8_t *prev  = prev1;
186
+    uint8_t *cur   = cur1;
187
+    uint8_t *next  = next1;
188
+    uint8_t *prev2 = parity ? prev : cur ;
189
+    uint8_t *next2 = parity ? cur  : next;
190
+    int interpol, x;
191
+
192
+    FILTER1()
193
+    FILTER_LINE()
194
+    FILTER2()
195
+}
196
+
197
+static void filter_edge(void *dst1, void *prev1, void *cur1, void *next1,
198
+                        int w, int prefs, int mrefs, int prefs2, int mrefs2,
199
+                        int parity, int clip_max, int spat)
200
+{
201
+    uint8_t *dst   = dst1;
202
+    uint8_t *prev  = prev1;
203
+    uint8_t *cur   = cur1;
204
+    uint8_t *next  = next1;
205
+    uint8_t *prev2 = parity ? prev : cur ;
206
+    uint8_t *next2 = parity ? cur  : next;
207
+    int interpol, x;
208
+
209
+    FILTER1()
210
+    FILTER_EDGE()
211
+    FILTER2()
212
+}
213
+
214
+static void filter_intra_16bit(void *dst1, void *cur1, int w, int prefs, int mrefs,
215
+                               int prefs3, int mrefs3, int parity, int clip_max)
216
+{
217
+    uint16_t *dst = dst1;
218
+    uint16_t *cur = cur1;
219
+    int interpol, x;
220
+
221
+    FILTER_INTRA()
222
+}
223
+
224
+static void filter_line_16bit(void *dst1, void *prev1, void *cur1, void *next1,
225
+                              int w, int prefs, int mrefs, int prefs2, int mrefs2,
226
+                              int prefs3, int mrefs3, int prefs4, int mrefs4,
227
+                              int parity, int clip_max)
228
+{
229
+    uint16_t *dst   = dst1;
230
+    uint16_t *prev  = prev1;
231
+    uint16_t *cur   = cur1;
232
+    uint16_t *next  = next1;
233
+    uint16_t *prev2 = parity ? prev : cur ;
234
+    uint16_t *next2 = parity ? cur  : next;
235
+    int interpol, x;
236
+
237
+    FILTER1()
238
+    FILTER_LINE()
239
+    FILTER2()
240
+}
241
+
242
+static void filter_edge_16bit(void *dst1, void *prev1, void *cur1, void *next1,
243
+                              int w, int prefs, int mrefs, int prefs2, int mrefs2,
244
+                              int parity, int clip_max, int spat)
245
+{
246
+    uint16_t *dst   = dst1;
247
+    uint16_t *prev  = prev1;
248
+    uint16_t *cur   = cur1;
249
+    uint16_t *next  = next1;
250
+    uint16_t *prev2 = parity ? prev : cur ;
251
+    uint16_t *next2 = parity ? cur  : next;
252
+    int interpol, x;
253
+
254
+    FILTER1()
255
+    FILTER_EDGE()
256
+    FILTER2()
257
+}
258
+
259
+static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
260
+{
261
+    BWDIFContext *s = ctx->priv;
262
+    ThreadData *td  = arg;
263
+    int linesize = s->cur->linesize[td->plane];
264
+    int clip_max = (1 << (s->csp->comp[td->plane].depth)) - 1;
265
+    int df = (s->csp->comp[td->plane].depth + 7) / 8;
266
+    int refs = linesize / df;
267
+    int slice_start = (td->h *  jobnr   ) / nb_jobs;
268
+    int slice_end   = (td->h * (jobnr+1)) / nb_jobs;
269
+    int y;
270
+
271
+    for (y = slice_start; y < slice_end; y++) {
272
+        if ((y ^ td->parity) & 1) {
273
+            uint8_t *prev = &s->prev->data[td->plane][y * linesize];
274
+            uint8_t *cur  = &s->cur ->data[td->plane][y * linesize];
275
+            uint8_t *next = &s->next->data[td->plane][y * linesize];
276
+            uint8_t *dst  = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
277
+            if (!s->inter_field) {
278
+                s->filter_intra(dst, cur, td->w, (y + df) < td->h ? refs : -refs,
279
+                                y > (df - 1) ? -refs : refs,
280
+                                (y + 3*df) < td->h ? 3 * refs : -refs,
281
+                                y > (3*df - 1) ? -3 * refs : refs,
282
+                                td->parity ^ td->tff, clip_max);
283
+            } else if ((y < 4) || ((y + 5) > td->h)) {
284
+                s->filter_edge(dst, prev, cur, next, td->w,
285
+                               (y + df) < td->h ? refs : -refs,
286
+                               y > (df - 1) ? -refs : refs,
287
+                               refs << 1, -(refs << 1),
288
+                               td->parity ^ td->tff, clip_max,
289
+                               (y < 2) || ((y + 3) > td->h) ? 0 : 1);
290
+            } else {
291
+                s->filter_line(dst, prev, cur, next, td->w,
292
+                               refs, -refs, refs << 1, -(refs << 1),
293
+                               3 * refs, -3 * refs, refs << 2, -(refs << 2),
294
+                               td->parity ^ td->tff, clip_max);
295
+            }
296
+        } else {
297
+            memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
298
+                   &s->cur->data[td->plane][y * linesize], td->w * df);
299
+        }
300
+    }
301
+    return 0;
302
+}
303
+
304
+static void filter(AVFilterContext *ctx, AVFrame *dstpic,
305
+                   int parity, int tff)
306
+{
307
+    BWDIFContext *bwdif = ctx->priv;
308
+    ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
309
+    int i;
310
+
311
+    for (i = 0; i < bwdif->csp->nb_components; i++) {
312
+        int w = dstpic->width;
313
+        int h = dstpic->height;
314
+
315
+        if (i == 1 || i == 2) {
316
+            w = AV_CEIL_RSHIFT(w, bwdif->csp->log2_chroma_w);
317
+            h = AV_CEIL_RSHIFT(h, bwdif->csp->log2_chroma_h);
318
+        }
319
+
320
+        td.w     = w;
321
+        td.h     = h;
322
+        td.plane = i;
323
+
324
+        ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
325
+    }
326
+    if (!bwdif->inter_field) {
327
+        bwdif->inter_field = 1;
328
+    }
329
+
330
+    emms_c();
331
+}
332
+
333
+static int return_frame(AVFilterContext *ctx, int is_second)
334
+{
335
+    BWDIFContext *bwdif = ctx->priv;
336
+    AVFilterLink *link  = ctx->outputs[0];
337
+    int tff, ret;
338
+
339
+    if (bwdif->parity == -1) {
340
+        tff = bwdif->cur->interlaced_frame ?
341
+              bwdif->cur->top_field_first : 1;
342
+    } else {
343
+        tff = bwdif->parity ^ 1;
344
+    }
345
+
346
+    if (is_second) {
347
+        bwdif->out = ff_get_video_buffer(link, link->w, link->h);
348
+        if (!bwdif->out)
349
+            return AVERROR(ENOMEM);
350
+
351
+        av_frame_copy_props(bwdif->out, bwdif->cur);
352
+        bwdif->out->interlaced_frame = 0;
353
+        if (bwdif->inter_field < 0)
354
+            bwdif->inter_field = 0;
355
+    }
356
+
357
+    filter(ctx, bwdif->out, tff ^ !is_second, tff);
358
+
359
+    if (is_second) {
360
+        int64_t cur_pts  = bwdif->cur->pts;
361
+        int64_t next_pts = bwdif->next->pts;
362
+
363
+        if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
364
+            bwdif->out->pts = cur_pts + next_pts;
365
+        } else {
366
+            bwdif->out->pts = AV_NOPTS_VALUE;
367
+        }
368
+    }
369
+    ret = ff_filter_frame(ctx->outputs[0], bwdif->out);
370
+
371
+    bwdif->frame_pending = (bwdif->mode&1) && !is_second;
372
+    return ret;
373
+}
374
+
375
+static int checkstride(BWDIFContext *bwdif, const AVFrame *a, const AVFrame *b)
376
+{
377
+    int i;
378
+    for (i = 0; i < bwdif->csp->nb_components; i++)
379
+        if (a->linesize[i] != b->linesize[i])
380
+            return 1;
381
+    return 0;
382
+}
383
+
384
+static void fixstride(AVFilterLink *link, AVFrame *f)
385
+{
386
+    AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
387
+    if(!dst)
388
+        return;
389
+    av_frame_copy_props(dst, f);
390
+    av_image_copy(dst->data, dst->linesize,
391
+                  (const uint8_t **)f->data, f->linesize,
392
+                  dst->format, dst->width, dst->height);
393
+    av_frame_unref(f);
394
+    av_frame_move_ref(f, dst);
395
+    av_frame_free(&dst);
396
+}
397
+
398
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
399
+{
400
+    AVFilterContext *ctx = link->dst;
401
+    BWDIFContext *bwdif = ctx->priv;
402
+
403
+    av_assert0(frame);
404
+
405
+    if (bwdif->frame_pending)
406
+        return_frame(ctx, 1);
407
+
408
+    if (bwdif->prev)
409
+        av_frame_free(&bwdif->prev);
410
+    bwdif->prev = bwdif->cur;
411
+    bwdif->cur  = bwdif->next;
412
+    bwdif->next = frame;
413
+
414
+    if (!bwdif->cur) {
415
+        bwdif->cur = av_frame_clone(bwdif->next);
416
+        if (!bwdif->cur)
417
+            return AVERROR(ENOMEM);
418
+        bwdif->inter_field = 0;
419
+    }
420
+
421
+    if (checkstride(bwdif, bwdif->next, bwdif->cur)) {
422
+        av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
423
+        fixstride(link, bwdif->next);
424
+    }
425
+    if (checkstride(bwdif, bwdif->next, bwdif->cur))
426
+        fixstride(link, bwdif->cur);
427
+    if (bwdif->prev && checkstride(bwdif, bwdif->next, bwdif->prev))
428
+        fixstride(link, bwdif->prev);
429
+    if (checkstride(bwdif, bwdif->next, bwdif->cur) || (bwdif->prev && checkstride(bwdif, bwdif->next, bwdif->prev))) {
430
+        av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
431
+        return -1;
432
+    }
433
+
434
+    if (!bwdif->prev)
435
+        return 0;
436
+
437
+    if ((bwdif->deint && !bwdif->cur->interlaced_frame) ||
438
+        ctx->is_disabled ||
439
+        (bwdif->deint && !bwdif->prev->interlaced_frame && bwdif->prev->repeat_pict) ||
440
+        (bwdif->deint && !bwdif->next->interlaced_frame && bwdif->next->repeat_pict)
441
+    ) {
442
+        bwdif->out  = av_frame_clone(bwdif->cur);
443
+        if (!bwdif->out)
444
+            return AVERROR(ENOMEM);
445
+
446
+        av_frame_free(&bwdif->prev);
447
+        if (bwdif->out->pts != AV_NOPTS_VALUE)
448
+            bwdif->out->pts *= 2;
449
+        return ff_filter_frame(ctx->outputs[0], bwdif->out);
450
+    }
451
+
452
+    bwdif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
453
+    if (!bwdif->out)
454
+        return AVERROR(ENOMEM);
455
+
456
+    av_frame_copy_props(bwdif->out, bwdif->cur);
457
+    bwdif->out->interlaced_frame = 0;
458
+
459
+    if (bwdif->out->pts != AV_NOPTS_VALUE)
460
+        bwdif->out->pts *= 2;
461
+
462
+    return return_frame(ctx, 0);
463
+}
464
+
465
+static int request_frame(AVFilterLink *link)
466
+{
467
+    AVFilterContext *ctx = link->src;
468
+    BWDIFContext *bwdif = ctx->priv;
469
+    int ret;
470
+
471
+    if (bwdif->frame_pending) {
472
+        return_frame(ctx, 1);
473
+        return 0;
474
+    }
475
+
476
+    if (bwdif->eof)
477
+        return AVERROR_EOF;
478
+
479
+    ret  = ff_request_frame(link->src->inputs[0]);
480
+
481
+    if (ret == AVERROR_EOF && bwdif->cur) {
482
+        AVFrame *next = av_frame_clone(bwdif->next);
483
+
484
+        if (!next)
485
+            return AVERROR(ENOMEM);
486
+
487
+        bwdif->inter_field = -1;
488
+        next->pts = bwdif->next->pts * 2 - bwdif->cur->pts;
489
+
490
+        filter_frame(link->src->inputs[0], next);
491
+        bwdif->eof = 1;
492
+    } else if (ret < 0) {
493
+        return ret;
494
+    }
495
+
496
+    return 0;
497
+}
498
+
499
+static av_cold void uninit(AVFilterContext *ctx)
500
+{
501
+    BWDIFContext *bwdif = ctx->priv;
502
+
503
+    av_frame_free(&bwdif->prev);
504
+    av_frame_free(&bwdif->cur );
505
+    av_frame_free(&bwdif->next);
506
+}
507
+
508
+static int query_formats(AVFilterContext *ctx)
509
+{
510
+    static const enum AVPixelFormat pix_fmts[] = {
511
+        AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV420P,
512
+        AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
513
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
514
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
515
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
516
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
517
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
518
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
519
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
520
+        AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
521
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
522
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
523
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
524
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
525
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
526
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
527
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
528
+        AV_PIX_FMT_NONE
529
+    };
530
+
531
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
532
+    if (!fmts_list)
533
+        return AVERROR(ENOMEM);
534
+
535
+    return ff_set_common_formats(ctx, fmts_list);
536
+}
537
+
538
+static int config_props(AVFilterLink *link)
539
+{
540
+    AVFilterContext *ctx = link->src;
541
+    BWDIFContext *s = link->src->priv;
542
+
543
+    link->time_base.num = link->src->inputs[0]->time_base.num;
544
+    link->time_base.den = link->src->inputs[0]->time_base.den * 2;
545
+    link->w             = link->src->inputs[0]->w;
546
+    link->h             = link->src->inputs[0]->h;
547
+
548
+    if(s->mode&1)
549
+        link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
550
+
551
+    if (link->w < 3 || link->h < 3) {
552
+        av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
553
+        return AVERROR(EINVAL);
554
+    }
555
+
556
+    s->csp = av_pix_fmt_desc_get(link->format);
557
+    if (s->csp->comp[0].depth > 8) {
558
+        s->filter_intra = filter_intra_16bit;
559
+        s->filter_line  = filter_line_16bit;
560
+        s->filter_edge  = filter_edge_16bit;
561
+    } else {
562
+        s->filter_intra = filter_intra;
563
+        s->filter_line  = filter_line;
564
+        s->filter_edge  = filter_edge;
565
+    }
566
+
567
+    return 0;
568
+}
569
+
570
+
571
+#define OFFSET(x) offsetof(BWDIFContext, x)
572
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
573
+
574
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
575
+
576
+static const AVOption bwdif_options[] = {
577
+    { "mode",   "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BWDIF_MODE_SEND_FIELD}, 0, 1, FLAGS, "mode"},
578
+    CONST("send_frame", "send one frame for each frame", BWDIF_MODE_SEND_FRAME, "mode"),
579
+    CONST("send_field", "send one frame for each field", BWDIF_MODE_SEND_FIELD, "mode"),
580
+
581
+    { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=BWDIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
582
+    CONST("tff",  "assume top field first",    BWDIF_PARITY_TFF,  "parity"),
583
+    CONST("bff",  "assume bottom field first", BWDIF_PARITY_BFF,  "parity"),
584
+    CONST("auto", "auto detect parity",        BWDIF_PARITY_AUTO, "parity"),
585
+
586
+    { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=BWDIF_DEINT_INTERLACED}, 0, 1, FLAGS, "deint" },
587
+    CONST("all",        "deinterlace all frames",                       BWDIF_DEINT_ALL,        "deint"),
588
+    CONST("interlaced", "only deinterlace frames marked as interlaced", BWDIF_DEINT_INTERLACED, "deint"),
589
+
590
+    { NULL }
591
+};
592
+
593
+AVFILTER_DEFINE_CLASS(bwdif);
594
+
595
+static const AVFilterPad avfilter_vf_bwdif_inputs[] = {
596
+    {
597
+        .name          = "default",
598
+        .type          = AVMEDIA_TYPE_VIDEO,
599
+        .filter_frame  = filter_frame,
600
+    },
601
+    { NULL }
602
+};
603
+
604
+static const AVFilterPad avfilter_vf_bwdif_outputs[] = {
605
+    {
606
+        .name          = "default",
607
+        .type          = AVMEDIA_TYPE_VIDEO,
608
+        .request_frame = request_frame,
609
+        .config_props  = config_props,
610
+    },
611
+    { NULL }
612
+};
613
+
614
+AVFilter ff_vf_bwdif = {
615
+    .name          = "bwdif",
616
+    .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
617
+    .priv_size     = sizeof(BWDIFContext),
618
+    .priv_class    = &bwdif_class,
619
+    .uninit        = uninit,
620
+    .query_formats = query_formats,
621
+    .inputs        = avfilter_vf_bwdif_inputs,
622
+    .outputs       = avfilter_vf_bwdif_outputs,
623
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
624
+};