• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/common.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct ESTDIFContext {
31     const AVClass *class;
32 
33     int mode;             ///< 0 is frame, 1 is field
34     int parity;           ///< frame field parity
35     int deint;            ///< which frames to deinterlace
36     int rslope;           ///< best edge slope search radius
37     int redge;            ///< best edge match search radius
38     float ecost;          ///< edge cost for edge matching
39     float mcost;          ///< middle cost for edge matching
40     float dcost;          ///< distance cost for edge matching
41     int interp;           ///< type of interpolation
42     int linesize[4];      ///< bytes of pixel data per line for each plane
43     int planewidth[4];    ///< width of each plane
44     int planeheight[4];   ///< height of each plane
45     int field;            ///< which field are we on, 0 or 1
46     int eof;
47     int depth;
48     int max;
49     int nb_planes;
50     int nb_threads;
51     int64_t pts;
52     AVFrame *prev;
53 
54     void (*interpolate)(struct ESTDIFContext *s, uint8_t *dst,
55                         const uint8_t *prev_line,  const uint8_t *next_line,
56                         const uint8_t *prev2_line, const uint8_t *next2_line,
57                         const uint8_t *prev3_line, const uint8_t *next3_line,
58                         int x, int width, int rslope, int redge,
59                         int depth, int *K);
60 
61     unsigned (*mid_8[3])(const uint8_t *const prev,
62                          const uint8_t *const next,
63                          const uint8_t *const prev2,
64                          const uint8_t *const next2,
65                          const uint8_t *const prev3,
66                          const uint8_t *const next3,
67                          int end, int x, int k, int depth);
68 
69     unsigned (*mid_16[3])(const uint16_t *const prev,
70                           const uint16_t *const next,
71                           const uint16_t *const prev2,
72                           const uint16_t *const next2,
73                           const uint16_t *const prev3,
74                           const uint16_t *const next3,
75                           int end, int x, int k, int depth);
76 } ESTDIFContext;
77 
78 #define MAX_R 15
79 #define S     (MAX_R * 2 + 1)
80 
81 #define OFFSET(x) offsetof(ESTDIFContext, x)
82 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
83 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
84 
85 static const AVOption estdif_options[] = {
86     { "mode", "specify the mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
87     CONST("frame", "send one frame for each frame", 0, "mode"),
88     CONST("field", "send one frame for each field", 1, "mode"),
89     { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
90     CONST("tff",  "assume top field first",    0, "parity"),
91     CONST("bff",  "assume bottom field first", 1, "parity"),
92     CONST("auto", "auto detect parity",       -1, "parity"),
93     { "deint",  "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
94     CONST("all",        "deinterlace all frames",                       0, "deint"),
95     CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
96     { "rslope", "specify the search radius for edge slope tracing", OFFSET(rslope), AV_OPT_TYPE_INT, {.i64=1}, 1, MAX_R, FLAGS, },
97     { "redge",  "specify the search radius for best edge matching", OFFSET(redge),  AV_OPT_TYPE_INT, {.i64=2}, 0, MAX_R, FLAGS, },
98     { "ecost",  "specify the edge cost for edge matching",          OFFSET(ecost),  AV_OPT_TYPE_FLOAT,{.dbl=1},0,9,FLAGS, },
99     { "mcost",  "specify the middle cost for edge matching",        OFFSET(mcost),  AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1,  FLAGS, },
100     { "dcost",  "specify the distance cost for edge matching",      OFFSET(dcost),  AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1,  FLAGS, },
101     { "interp", "specify the type of interpolation",                OFFSET(interp), AV_OPT_TYPE_INT, {.i64=1}, 0, 2,     FLAGS, "interp" },
102     CONST("2p", "two-point interpolation",  0, "interp"),
103     CONST("4p", "four-point interpolation", 1, "interp"),
104     CONST("6p", "six-point interpolation",  2, "interp"),
105     { NULL }
106 };
107 
108 AVFILTER_DEFINE_CLASS(estdif);
109 
110 static const enum AVPixelFormat pix_fmts[] = {
111     AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
112     AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
113     AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
114     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
115     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
116     AV_PIX_FMT_YUVJ411P,
117     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
118     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
119     AV_PIX_FMT_GRAY8,
120     AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
121     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
122     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
123     AV_PIX_FMT_YUV440P10,
124     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
125     AV_PIX_FMT_YUV440P12,
126     AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
127     AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
128     AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
129     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
130     AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
131     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
132     AV_PIX_FMT_GBRAP10,   AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
133     AV_PIX_FMT_NONE
134 };
135 
config_output(AVFilterLink * outlink)136 static int config_output(AVFilterLink *outlink)
137 {
138     AVFilterContext *ctx = outlink->src;
139     AVFilterLink *inlink = ctx->inputs[0];
140     ESTDIFContext *s = ctx->priv;
141 
142     outlink->time_base = av_mul_q(inlink->time_base, (AVRational){1, 2});
143     if (s->mode)
144         outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2, 1});
145 
146     return 0;
147 }
148 
149 typedef struct ThreadData {
150     AVFrame *out, *in;
151 } ThreadData;
152 
153 #define MIDL(type, ss)                                         \
154 static unsigned midl_##ss(const type *const prev,              \
155                           const type *const next,              \
156                           int end, int x, int k)               \
157 {                                                              \
158     return (prev[av_clip(x + k, 0, end)] +                     \
159             next[av_clip(x - k, 0, end)] + 1) >> 1;            \
160 }
161 
162 MIDL(uint8_t, 8)
163 MIDL(uint16_t, 16)
164 
165 #define MID2(type, ss)                                         \
166 static unsigned mid2_##ss(const type *const prev,              \
167                           const type *const next,              \
168                           const type *const prev2,             \
169                           const type *const next2,             \
170                           const type *const prev3,             \
171                           const type *const next3,             \
172                           int end, int x, int k, int depth)    \
173 {                                                              \
174     return (prev[av_clip(x + k, 0, end)] +                     \
175             next[av_clip(x - k, 0, end)] + 1) >> 1;            \
176 }
177 
178 MID2(uint8_t, 8)
179 MID2(uint16_t, 16)
180 
181 #define MID4(type, ss)                                         \
182 static unsigned mid4_##ss(const type *const prev,              \
183                           const type *const next,              \
184                           const type *const prev2,             \
185                           const type *const next2,             \
186                           const type *const prev3,             \
187                           const type *const next3,             \
188                           int end, int x, int k, int depth)    \
189 {                                                              \
190     return av_clip_uintp2_c((                                  \
191             9 * (prev[av_clip(x + k, 0, end)] +                \
192                  next[av_clip(x - k, 0, end)]) -               \
193             1 * (prev2[av_clip(x + k*3, 0, end)] +             \
194                  next2[av_clip(x - k*3, 0, end)]) + 8) >> 4,   \
195                  depth);                                       \
196 }
197 
198 MID4(uint8_t, 8)
199 MID4(uint16_t, 16)
200 
201 #define MID6(type, ss)                                         \
202 static unsigned mid6_##ss(const type *const prev,              \
203                           const type *const next,              \
204                           const type *const prev2,             \
205                           const type *const next2,             \
206                           const type *const prev3,             \
207                           const type *const next3,             \
208                           int end, int x, int k, int depth)    \
209 {                                                              \
210     return av_clip_uintp2_c((                                  \
211            20 * (prev[av_clip(x + k, 0, end)] +                \
212                  next[av_clip(x - k, 0, end)]) -               \
213             5 * (prev2[av_clip(x + k*3, 0, end)] +             \
214                  next2[av_clip(x - k*3, 0, end)]) +            \
215             1 * (prev3[av_clip(x + k*5, 0, end)] +             \
216                  next3[av_clip(x - k*5, 0, end)]) + 16) >> 5,  \
217                  depth);                                       \
218 }
219 
220 MID6(uint8_t, 8)
221 MID6(uint16_t, 16)
222 
223 #define DIFF(type, ss)                                         \
224 static unsigned diff_##ss(const type *const prev,              \
225                           const type *const next,              \
226                           int x, int y)                        \
227 {                                                              \
228     return FFABS(prev[x] -  next[y]);                          \
229 }
230 
231 DIFF(uint8_t, 8)
232 DIFF(uint16_t, 16)
233 
234 #define COST(type, ss)                                         \
235 static unsigned cost_##ss(const type *const prev,              \
236                           const type *const next,              \
237                           int end, int x, int k)               \
238 {                                                              \
239     const int m = midl_##ss(prev, next, end, x, k);            \
240     const int p = prev[x];                                     \
241     const int n = next[x];                                     \
242                                                                \
243     return FFABS(p - m) + FFABS(n - m);                        \
244 }
245 
246 COST(uint8_t, 8)
247 COST(uint16_t, 16)
248 
249 #define INTERPOLATE(type, atype, amax, ss)                                     \
250 static void interpolate_##ss(ESTDIFContext *s, uint8_t *ddst,                  \
251                              const uint8_t *const pprev_line,                  \
252                              const uint8_t *const nnext_line,                  \
253                              const uint8_t *const pprev2_line,                 \
254                              const uint8_t *const nnext2_line,                 \
255                              const uint8_t *const pprev3_line,                 \
256                              const uint8_t *const nnext3_line,                 \
257                              int x, int width, int rslope,                     \
258                              int redge, int depth,                             \
259                              int *K)                                           \
260 {                                                                              \
261     type *dst = (type *)ddst;                                                  \
262     const type *const prev_line = (const type *const)pprev_line;               \
263     const type *const prev2_line = (const type *const)pprev2_line;             \
264     const type *const prev3_line = (const type *const)pprev3_line;             \
265     const type *const next_line = (const type *const)nnext_line;               \
266     const type *const next2_line = (const type *const)nnext2_line;             \
267     const type *const next3_line = (const type *const)nnext3_line;             \
268     const int interp = s->interp;                                              \
269     const int ecost = s->ecost * 32.f;                                         \
270     const int dcost = s->dcost * s->max;                                       \
271     const int end = width - 1;                                                 \
272     const atype mcost = s->mcost * s->redge * 4.f;                             \
273     atype sd[S], sD[S], di = 0;                                                \
274     atype dmin = amax;                                                         \
275     int k = *K;                                                                \
276                                                                                \
277     for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) {               \
278         atype sum = 0;                                                         \
279                                                                                \
280         for (int j = -redge; j <= redge; j++) {                                \
281             const int xx = av_clip(x + i + j, 0, end);                         \
282             const int yy = av_clip(x - i + j, 0, end);                         \
283             sum += diff_##ss(prev_line,  next_line,  xx, yy);                  \
284             sum += diff_##ss(prev2_line, prev_line,  xx, yy);                  \
285             sum += diff_##ss(next_line,  next2_line, xx, yy);                  \
286         }                                                                      \
287                                                                                \
288         sD[i + rslope]  = ecost * sum;                                         \
289         sD[i + rslope] += mcost * cost_##ss(prev_line,  next_line,  end, x, i);\
290         sD[i + rslope] += dcost * abs(i);                                      \
291                                                                                \
292         dmin = FFMIN(sD[i + rslope], dmin);                                    \
293     }                                                                          \
294                                                                                \
295     for (int i = -rslope; i <= rslope; i++) {                                  \
296         atype sum = 0;                                                         \
297                                                                                \
298         for (int j = -redge; j <= redge; j++) {                                \
299             const int xx = av_clip(x + k + i + j, 0, end);                     \
300             const int yy = av_clip(x - k - i + j, 0, end);                     \
301             sum += diff_##ss(prev_line,  next_line,  xx, yy);                  \
302             sum += diff_##ss(prev2_line, prev_line,  xx, yy);                  \
303             sum += diff_##ss(next_line,  next2_line, xx, yy);                  \
304         }                                                                      \
305                                                                                \
306         sd[i + rslope]  = ecost * sum;                                         \
307         sd[i + rslope] += mcost * cost_##ss(prev_line, next_line, end, x, k+i);\
308         sd[i + rslope] += dcost * abs(k + i);                                  \
309                                                                                \
310         dmin = FFMIN(sd[i + rslope], dmin);                                    \
311     }                                                                          \
312                                                                                \
313     for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) {               \
314         if (dmin == sD[i + rslope]) {                                          \
315             di = 1;                                                            \
316             k = i;                                                             \
317             break;                                                             \
318         }                                                                      \
319     }                                                                          \
320                                                                                \
321     for (int i = -rslope; i <= rslope && !di; i++) {                           \
322         if (dmin == sd[i + rslope]) {                                          \
323             k += i;                                                            \
324             break;                                                             \
325         }                                                                      \
326     }                                                                          \
327                                                                                \
328     dst[x] = s->mid_##ss[interp](prev_line, next_line,                         \
329                                  prev2_line, next2_line,                       \
330                                  prev3_line, next3_line,                       \
331                                  end, x, k, depth);                            \
332                                                                                \
333     *K = k;                                                                    \
334 }
335 
336 INTERPOLATE(uint8_t,  unsigned, UINT_MAX, 8)
337 INTERPOLATE(uint16_t, uint64_t, UINT64_MAX, 16)
338 
deinterlace_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)339 static int deinterlace_slice(AVFilterContext *ctx, void *arg,
340                              int jobnr, int nb_jobs)
341 {
342     ESTDIFContext *s = ctx->priv;
343     ThreadData *td = arg;
344     AVFrame *out = td->out;
345     AVFrame *in = td->in;
346     const int rslope = s->rslope;
347     const int redge = s->redge;
348     const int depth = s->depth;
349     const int interlaced = in->interlaced_frame;
350     const int tff = (s->field == (s->parity == -1 ? interlaced ? in->top_field_first : 1 :
351                                   s->parity ^ 1));
352 
353     for (int plane = 0; plane < s->nb_planes; plane++) {
354         const uint8_t *src_data = in->data[plane];
355         uint8_t *dst_data = out->data[plane];
356         const int linesize = s->linesize[plane];
357         const int width = s->planewidth[plane];
358         const int height = s->planeheight[plane];
359         const int src_linesize = in->linesize[plane];
360         const int dst_linesize = out->linesize[plane];
361         const int start = (height * jobnr) / nb_jobs;
362         const int end = (height * (jobnr+1)) / nb_jobs;
363         const uint8_t *prev_line, *prev2_line, *next_line, *next2_line, *in_line;
364         const uint8_t *prev3_line, *next3_line;
365         uint8_t *out_line;
366         int y_out;
367 
368         y_out = start + (tff ^ (start & 1));
369 
370         in_line  = src_data + (y_out * src_linesize);
371         out_line = dst_data + (y_out * dst_linesize);
372 
373         while (y_out < end) {
374             memcpy(out_line, in_line, linesize);
375             y_out += 2;
376             in_line  += src_linesize * 2;
377             out_line += dst_linesize * 2;
378         }
379 
380         y_out = start + ((!tff) ^ (start & 1));
381         out_line = dst_data + (y_out * dst_linesize);
382 
383         for (int y = y_out; y < end; y += 2) {
384             int y_prev3_in = y - 5;
385             int y_next3_in = y + 5;
386             int y_prev2_in = y - 3;
387             int y_next2_in = y + 3;
388             int y_prev_in = y - 1;
389             int y_next_in = y + 1;
390             int k;
391 
392             while (y_prev3_in < 0)
393                 y_prev3_in += 2;
394 
395             while (y_next3_in >= height)
396                 y_next3_in -= 2;
397 
398             while (y_prev2_in < 0)
399                 y_prev2_in += 2;
400 
401             while (y_next2_in >= height)
402                 y_next2_in -= 2;
403 
404             while (y_prev_in < 0)
405                 y_prev_in += 2;
406 
407             while (y_next_in >= height)
408                 y_next_in -= 2;
409 
410             prev3_line = src_data + (y_prev3_in * src_linesize);
411             next3_line = src_data + (y_next3_in * src_linesize);
412 
413             prev2_line = src_data + (y_prev2_in * src_linesize);
414             next2_line = src_data + (y_next2_in * src_linesize);
415 
416             prev_line = src_data + (y_prev_in * src_linesize);
417             next_line = src_data + (y_next_in * src_linesize);
418 
419             k = 0;
420 
421             for (int x = 0; x < width; x++) {
422                 s->interpolate(s, out_line,
423                                prev_line, next_line,
424                                prev2_line, next2_line,
425                                prev3_line, next3_line,
426                                x, width, rslope, redge, depth, &k);
427             }
428 
429             out_line += 2 * dst_linesize;
430         }
431     }
432 
433     return 0;
434 }
435 
filter(AVFilterContext * ctx,int is_second,AVFrame * in)436 static int filter(AVFilterContext *ctx, int is_second, AVFrame *in)
437 {
438     ESTDIFContext *s = ctx->priv;
439     AVFilterLink *outlink = ctx->outputs[0];
440     AVFrame *out;
441     ThreadData td;
442 
443     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
444     if (!out)
445         return AVERROR(ENOMEM);
446     av_frame_copy_props(out, in);
447     out->interlaced_frame = 0;
448     out->pts = s->pts;
449 
450     td.out = out; td.in = in;
451     ff_filter_execute(ctx, deinterlace_slice, &td, NULL,
452                       FFMIN(s->planeheight[1] / 2, s->nb_threads));
453 
454     if (s->mode)
455         s->field = !s->field;
456 
457     return ff_filter_frame(outlink, out);
458 }
459 
config_input(AVFilterLink * inlink)460 static int config_input(AVFilterLink *inlink)
461 {
462     AVFilterContext *ctx = inlink->dst;
463     ESTDIFContext *s = ctx->priv;
464     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
465     int ret;
466 
467     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
468         return ret;
469 
470     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
471     s->planeheight[0] = s->planeheight[3] = inlink->h;
472     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
473     s->planewidth[0] = s->planewidth[3] = inlink->w;
474 
475     if (inlink->h < 3) {
476         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
477         return AVERROR(EINVAL);
478     }
479 
480     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
481     s->nb_threads = ff_filter_get_nb_threads(ctx);
482     s->depth = desc->comp[0].depth;
483     s->interpolate = s->depth <= 8 ? interpolate_8 : interpolate_16;
484     s->mid_8[0] = mid2_8;
485     s->mid_8[1] = mid4_8;
486     s->mid_8[2] = mid6_8;
487     s->mid_16[0] = mid2_16;
488     s->mid_16[1] = mid4_16;
489     s->mid_16[2] = mid6_16;
490     s->max = (1 << (s->depth)) - 1;
491 
492     return 0;
493 }
filter_frame(AVFilterLink * inlink,AVFrame * in)494  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
495 {
496     AVFilterContext *ctx = inlink->dst;
497     ESTDIFContext *s = ctx->priv;
498     int ret;
499 
500     if (!s->prev) {
501         s->prev = in;
502         return 0;
503     }
504 
505     if ((s->deint && !s->prev->interlaced_frame) || ctx->is_disabled) {
506         s->prev->pts *= 2;
507         ret = ff_filter_frame(ctx->outputs[0], s->prev);
508         s->prev = in;
509         return ret;
510     }
511 
512     s->pts = s->prev->pts * 2;
513     ret = filter(ctx, 0, s->prev);
514     if (ret < 0 || s->mode == 0) {
515         av_frame_free(&s->prev);
516         s->prev = in;
517         return ret;
518     }
519 
520     s->pts = s->prev->pts + in->pts;
521     ret = filter(ctx, 1, s->prev);
522     av_frame_free(&s->prev);
523     s->prev = in;
524     return ret;
525 }
526 
request_frame(AVFilterLink * link)527 static int request_frame(AVFilterLink *link)
528 {
529     AVFilterContext *ctx = link->src;
530     ESTDIFContext *s = ctx->priv;
531     int ret;
532 
533     if (s->eof)
534         return AVERROR_EOF;
535 
536     ret = ff_request_frame(ctx->inputs[0]);
537 
538     if (ret == AVERROR_EOF && s->prev) {
539         AVFrame *next = av_frame_clone(s->prev);
540 
541         if (!next)
542             return AVERROR(ENOMEM);
543 
544         next->pts = s->prev->pts + av_rescale_q(1, av_inv_q(ctx->outputs[0]->frame_rate),
545                                                 ctx->outputs[0]->time_base);
546         s->eof = 1;
547         ret = filter_frame(ctx->inputs[0], next);
548     } else if (ret < 0) {
549         return ret;
550     }
551 
552     return ret;
553 }
554 
uninit(AVFilterContext * ctx)555 static av_cold void uninit(AVFilterContext *ctx)
556 {
557     ESTDIFContext *s = ctx->priv;
558 
559     av_frame_free(&s->prev);
560 }
561 
562 static const AVFilterPad estdif_inputs[] = {
563     {
564         .name          = "default",
565         .type          = AVMEDIA_TYPE_VIDEO,
566         .filter_frame  = filter_frame,
567         .config_props  = config_input,
568     },
569 };
570 
571 static const AVFilterPad estdif_outputs[] = {
572     {
573         .name          = "default",
574         .type          = AVMEDIA_TYPE_VIDEO,
575         .config_props  = config_output,
576         .request_frame = request_frame,
577     },
578 };
579 
580 const AVFilter ff_vf_estdif = {
581     .name          = "estdif",
582     .description   = NULL_IF_CONFIG_SMALL("Apply Edge Slope Tracing deinterlace."),
583     .priv_size     = sizeof(ESTDIFContext),
584     .priv_class    = &estdif_class,
585     .uninit        = uninit,
586     FILTER_INPUTS(estdif_inputs),
587     FILTER_OUTPUTS(estdif_outputs),
588     FILTER_PIXFMTS_ARRAY(pix_fmts),
589     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
590     .process_command = ff_filter_process_command,
591 };
592