]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_estdif.c
avfilter/vf_estdif: add some forgotten formats
[ffmpeg] / libavfilter / vf_estdif.c
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     int linesize[4];      ///< bytes of pixel data per line for each plane
39     int planewidth[4];    ///< width of each plane
40     int planeheight[4];   ///< height of each plane
41     int field;            ///< which field are we on, 0 or 1
42     int eof;
43     int depth;
44     int half;
45     int nb_planes;
46     int nb_threads;
47     int64_t pts;
48     AVFrame *prev;
49
50     void (*interpolate)(uint8_t *dst,
51                         const uint8_t *prev_line, const uint8_t *next_line,
52                         const uint8_t *prev2_line, const uint8_t *next2_line,
53                         int x, int width, int rslope, int redge, unsigned half,
54                         int depth, int *K);
55 } ESTDIFContext;
56
57 #define MAX_R 15
58 #define S     (MAX_R * 2 + 1)
59
60 #define OFFSET(x) offsetof(ESTDIFContext, x)
61 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
62 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
63
64 static const AVOption estdif_options[] = {
65     { "mode", "specify the mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
66     CONST("frame", "send one frame for each frame", 0, "mode"),
67     CONST("field", "send one frame for each field", 1, "mode"),
68     { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
69     CONST("tff",  "assume top field first",    0, "parity"),
70     CONST("bff",  "assume bottom field first", 1, "parity"),
71     CONST("auto", "auto detect parity",       -1, "parity"),
72     { "deint",  "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
73     CONST("all",        "deinterlace all frames",                       0, "deint"),
74     CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
75     { "rslope", "specify the search radius for edge slope tracing", OFFSET(rslope), AV_OPT_TYPE_INT, {.i64=1}, 1, MAX_R, FLAGS, },
76     { "redge",  "specify the search radius for best edge matching", OFFSET(redge),  AV_OPT_TYPE_INT, {.i64=2}, 0, MAX_R, FLAGS, },
77     { NULL }
78 };
79
80 AVFILTER_DEFINE_CLASS(estdif);
81
82 static int query_formats(AVFilterContext *ctx)
83 {
84     static const enum AVPixelFormat pix_fmts[] = {
85         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
86         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
87         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
88         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
89         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
90         AV_PIX_FMT_YUVJ411P,
91         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
92         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
93         AV_PIX_FMT_GRAY8,
94         AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
95         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
96         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
97         AV_PIX_FMT_YUV440P10,
98         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
99         AV_PIX_FMT_YUV440P12,
100         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
101         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
102         AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
103         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
104         AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
105         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
106         AV_PIX_FMT_GBRAP10,   AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
107         AV_PIX_FMT_NONE
108     };
109
110     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
111     if (!fmts_list)
112         return AVERROR(ENOMEM);
113     return ff_set_common_formats(ctx, fmts_list);
114 }
115
116 static int config_output(AVFilterLink *outlink)
117 {
118     AVFilterContext *ctx = outlink->src;
119     AVFilterLink *inlink = ctx->inputs[0];
120
121     outlink->time_base.num = inlink->time_base.num;
122     outlink->time_base.den = inlink->time_base.den * 2;
123     outlink->frame_rate.num = inlink->frame_rate.num * 2;
124     outlink->frame_rate.den = inlink->frame_rate.den;
125
126     return 0;
127 }
128
129 typedef struct ThreadData {
130     AVFrame *out, *in;
131 } ThreadData;
132
133 #define MIDL(type, ss)                                         \
134 static unsigned midl_##ss(const type *const prev,              \
135                           const type *const next,              \
136                           int end, int x, int k)               \
137 {                                                              \
138     return (prev[av_clip(x + k, 0, end)] +                     \
139             next[av_clip(x - k, 0, end)] + 1) >> 1;            \
140 }
141
142 MIDL(uint8_t, 8)
143 MIDL(uint16_t, 16)
144
145 #define MID4(type, ss)                                         \
146 static unsigned mid4_##ss(const type *const prev,              \
147                           const type *const next,              \
148                           const type *const prev2,             \
149                           const type *const next2,             \
150                           int end, int x, int k, int depth)    \
151 {                                                              \
152     return av_clip_uintp2_c((                                  \
153             9 * (prev[av_clip(x + k, 0, end)] +                \
154                  next[av_clip(x - k, 0, end)]) -               \
155             1 * (prev2[av_clip(x + k*3, 0, end)] +             \
156                  next2[av_clip(x - k*3, 0, end)]) + 8) >> 4,   \
157                  depth);                                       \
158 }
159
160 MID4(uint8_t, 8)
161 MID4(uint16_t, 16)
162
163 #define DIFF(type, ss)                                         \
164 static unsigned diff_##ss(const type *const prev,              \
165                           const type *const next,              \
166                           int end, int x, int k, int j)        \
167 {                                                              \
168     return FFABS(prev[av_clip(x + k + j, 0, end)] -            \
169                  next[av_clip(x - k + j, 0, end)]);            \
170 }
171
172 DIFF(uint8_t, 8)
173 DIFF(uint16_t, 16)
174
175 #define COST(type, ss)                                         \
176 static unsigned cost_##ss(const type *const prev,              \
177                           const type *const next,              \
178                           int end, int x, int k)               \
179 {                                                              \
180     const int m = midl_##ss(prev, next, end, x, k);            \
181     const int p = prev[x];                                     \
182     const int n = next[x];                                     \
183                                                                \
184     return FFABS(p - m) + FFABS(n - m);                        \
185 }
186
187 COST(uint8_t, 8)
188 COST(uint16_t, 16)
189
190 #define INTERPOLATE(type, atype, max, ss)                                      \
191 static void interpolate_##ss(uint8_t *ddst,                                    \
192                              const uint8_t *const pprev_line,                  \
193                              const uint8_t *const nnext_line,                  \
194                              const uint8_t *const pprev2_line,                 \
195                              const uint8_t *const nnext2_line,                 \
196                              int x, int width, int rslope,                     \
197                              int redge, unsigned h, int depth,                 \
198                              int *K)                                           \
199 {                                                                              \
200     type *dst = (type *)ddst;                                                  \
201     const type *const prev_line = (const type *const)pprev_line;               \
202     const type *const prev2_line = (const type *const)pprev2_line;             \
203     const type *const next_line = (const type *const)nnext_line;               \
204     const type *const next2_line = (const type *const)nnext2_line;             \
205     const int end = width - 1;                                                 \
206     const atype f = redge + 2;                                                 \
207     atype sd[S], sD[S], di = 0;                                                \
208     atype dmin = max;                                                          \
209     int k = *K;                                                                \
210                                                                                \
211     for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) {               \
212         atype sum = 0;                                                         \
213                                                                                \
214         for (int j = -redge; j <= redge; j++) {                                \
215             sum += diff_##ss(prev_line,  next_line,  end, x, i, j);            \
216             sum += diff_##ss(prev2_line, prev_line,  end, x, i, j);            \
217             sum += diff_##ss(next_line,  next2_line, end, x, i, j);            \
218         }                                                                      \
219                                                                                \
220         sD[i + rslope]  =     sum;                                             \
221         sD[i + rslope] += f * cost_##ss(prev_line,  next_line,  end, x, i);    \
222         sD[i + rslope] += h * abs(i);                                          \
223                                                                                \
224         dmin = FFMIN(sD[i + rslope], dmin);                                    \
225     }                                                                          \
226                                                                                \
227     for (int i = -rslope; i <= rslope; i++) {                                  \
228         atype sum = 0;                                                         \
229                                                                                \
230         for (int j = -redge; j <= redge; j++) {                                \
231             sum += diff_##ss(prev_line,  next_line,  end, x, k + i, j);        \
232             sum += diff_##ss(prev2_line, prev_line,  end, x, k + i, j);        \
233             sum += diff_##ss(next_line,  next2_line, end, x, k + i, j);        \
234         }                                                                      \
235                                                                                \
236         sd[i + rslope]  =     sum;                                             \
237         sd[i + rslope] += f * cost_##ss(prev_line, next_line, end, x, k + i);  \
238         sd[i + rslope] += h * abs(k + i);                                      \
239                                                                                \
240         dmin = FFMIN(sd[i + rslope], dmin);                                    \
241     }                                                                          \
242                                                                                \
243     for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) {               \
244         if (dmin == sD[i + rslope]) {                                          \
245             di = 1;                                                            \
246             k = i;                                                             \
247             break;                                                             \
248         }                                                                      \
249     }                                                                          \
250                                                                                \
251     for (int i = -rslope; i <= rslope && !di; i++) {                           \
252         if (dmin == sd[i + rslope]) {                                          \
253             k += i;                                                            \
254             break;                                                             \
255         }                                                                      \
256     }                                                                          \
257                                                                                \
258     dst[x] = mid4_##ss(prev_line, next_line, prev2_line, next2_line,           \
259                        end, x, k, depth);                                      \
260                                                                                \
261     *K = k;                                                                    \
262 }
263
264 INTERPOLATE(uint8_t,  unsigned, UINT_MAX, 8)
265 INTERPOLATE(uint16_t, uint64_t, UINT64_MAX, 16)
266
267 static int deinterlace_slice(AVFilterContext *ctx, void *arg,
268                              int jobnr, int nb_jobs)
269 {
270     ESTDIFContext *s = ctx->priv;
271     ThreadData *td = arg;
272     AVFrame *out = td->out;
273     AVFrame *in = td->in;
274     const int rslope = s->rslope;
275     const int redge = s->redge;
276     const int half = s->half;
277     const int depth = s->depth;
278     const int interlaced = in->interlaced_frame;
279     const int tff = (s->field == (s->parity == -1 ? interlaced ? in->top_field_first : 1 :
280                                   s->parity ^ 1));
281
282     for (int plane = 0; plane < s->nb_planes; plane++) {
283         const uint8_t *src_data = in->data[plane];
284         uint8_t *dst_data = out->data[plane];
285         const int linesize = s->linesize[plane];
286         const int width = s->planewidth[plane];
287         const int height = s->planeheight[plane];
288         const int src_linesize = in->linesize[plane];
289         const int dst_linesize = out->linesize[plane];
290         const int start = (height * jobnr) / nb_jobs;
291         const int end = (height * (jobnr+1)) / nb_jobs;
292         const uint8_t *prev_line, *prev2_line, *next_line, *next2_line, *in_line;
293         uint8_t *out_line;
294         int y_out;
295
296         y_out = start + (tff ^ (start & 1));
297
298         in_line  = src_data + (y_out * src_linesize);
299         out_line = dst_data + (y_out * dst_linesize);
300
301         while (y_out < end) {
302             memcpy(out_line, in_line, linesize);
303             y_out += 2;
304             in_line  += src_linesize * 2;
305             out_line += dst_linesize * 2;
306         }
307
308         y_out = start + ((!tff) ^ (start & 1));
309         out_line = dst_data + (y_out * dst_linesize);
310
311         for (int y = y_out; y < end; y += 2) {
312             int y_prev2_in = y - 3;
313             int y_next2_in = y + 3;
314             int y_prev_in = y - 1;
315             int y_next_in = y + 1;
316             int k;
317
318             while (y_prev2_in < 0)
319                 y_prev2_in += 2;
320
321             while (y_next2_in >= height)
322                 y_next2_in -= 2;
323
324             while (y_prev_in < 0)
325                 y_prev_in += 2;
326
327             while (y_next_in >= height)
328                 y_next_in -= 2;
329
330             prev2_line = src_data + (y_prev2_in * src_linesize);
331             next2_line = src_data + (y_next2_in * src_linesize);
332
333             prev_line = src_data + (y_prev_in * src_linesize);
334             next_line = src_data + (y_next_in * src_linesize);
335
336             k = 0;
337
338             for (int x = 0; x < width; x++) {
339                 s->interpolate(out_line,
340                                prev_line, next_line,
341                                prev2_line, next2_line,
342                                x, width, rslope, redge, half, depth, &k);
343             }
344
345             out_line += 2 * dst_linesize;
346         }
347     }
348
349     return 0;
350 }
351
352 static int filter(AVFilterContext *ctx, int is_second, AVFrame *in)
353 {
354     ESTDIFContext *s = ctx->priv;
355     AVFilterLink *outlink = ctx->outputs[0];
356     AVFrame *out;
357     ThreadData td;
358
359     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
360     if (!out)
361         return AVERROR(ENOMEM);
362     av_frame_copy_props(out, in);
363     out->interlaced_frame = 0;
364     out->pts = s->pts;
365
366     td.out = out; td.in = in;
367     ctx->internal->execute(ctx, deinterlace_slice, &td, NULL,
368                            FFMIN(s->planeheight[1] / 2, s->nb_threads));
369
370     if (s->mode)
371         s->field = !s->field;
372
373     return ff_filter_frame(outlink, out);
374 }
375
376 static int config_input(AVFilterLink *inlink)
377 {
378     AVFilterContext *ctx = inlink->dst;
379     ESTDIFContext *s = ctx->priv;
380     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
381     int ret;
382
383     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
384         return ret;
385
386     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
387     s->planeheight[0] = s->planeheight[3] = inlink->h;
388     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
389     s->planewidth[0] = s->planewidth[3] = inlink->w;
390
391     if (inlink->h < 3) {
392         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
393         return AVERROR(EINVAL);
394     }
395
396     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
397     s->nb_threads = ff_filter_get_nb_threads(ctx);
398     s->depth = desc->comp[0].depth;
399     s->interpolate = s->depth <= 8 ? interpolate_8 : interpolate_16;
400     s->half = 1 << (s->depth - 1);
401
402     return 0;
403 }
404  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
405 {
406     AVFilterContext *ctx = inlink->dst;
407     ESTDIFContext *s = ctx->priv;
408     int ret;
409
410     if (!s->prev) {
411         s->prev = in;
412         return 0;
413     }
414
415     if ((s->deint && !in->interlaced_frame) || ctx->is_disabled) {
416         s->prev->pts *= 2;
417         ret = ff_filter_frame(ctx->outputs[0], s->prev);
418         s->prev = in;
419         return ret;
420     }
421
422     s->pts = s->prev->pts * 2;
423     ret = filter(ctx, 0, s->prev);
424     if (ret < 0 || s->mode == 0) {
425         av_frame_free(&s->prev);
426         s->prev = in;
427         return ret;
428     }
429
430     s->pts = s->prev->pts + in->pts;
431     ret = filter(ctx, 1, s->prev);
432     av_frame_free(&s->prev);
433     s->prev = in;
434     return ret;
435 }
436
437 static int request_frame(AVFilterLink *link)
438 {
439     AVFilterContext *ctx = link->src;
440     ESTDIFContext *s = ctx->priv;
441     int ret;
442
443     if (s->eof)
444         return AVERROR_EOF;
445
446     ret = ff_request_frame(ctx->inputs[0]);
447
448     if (ret == AVERROR_EOF && s->prev) {
449         AVFrame *next = av_frame_clone(s->prev);
450
451         if (!next)
452             return AVERROR(ENOMEM);
453
454         next->pts = s->prev->pts + av_rescale_q(1, av_inv_q(ctx->outputs[0]->frame_rate),
455                                                 ctx->outputs[0]->time_base);
456         s->eof = 1;
457         ret = filter_frame(ctx->inputs[0], next);
458     } else if (ret < 0) {
459         return ret;
460     }
461
462     return ret;
463 }
464
465 static av_cold void uninit(AVFilterContext *ctx)
466 {
467     ESTDIFContext *s = ctx->priv;
468
469     av_frame_free(&s->prev);
470 }
471
472 static const AVFilterPad estdif_inputs[] = {
473     {
474         .name          = "default",
475         .type          = AVMEDIA_TYPE_VIDEO,
476         .filter_frame  = filter_frame,
477         .config_props  = config_input,
478     },
479     { NULL }
480 };
481
482 static const AVFilterPad estdif_outputs[] = {
483     {
484         .name          = "default",
485         .type          = AVMEDIA_TYPE_VIDEO,
486         .config_props  = config_output,
487         .request_frame = request_frame,
488     },
489     { NULL }
490 };
491
492 AVFilter ff_vf_estdif = {
493     .name          = "estdif",
494     .description   = NULL_IF_CONFIG_SMALL("Apply Edge Slope Tracing deinterlace."),
495     .priv_size     = sizeof(ESTDIFContext),
496     .priv_class    = &estdif_class,
497     .uninit        = uninit,
498     .query_formats = query_formats,
499     .inputs        = estdif_inputs,
500     .outputs       = estdif_outputs,
501     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
502     .process_command = ff_filter_process_command,
503 };