2 * Copyright (c) 2021 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/common.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
30 typedef struct ESTDIFContext {
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
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,
58 #define S (MAX_R * 2 + 1)
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 }
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, },
80 AVFILTER_DEFINE_CLASS(estdif);
82 static int query_formats(AVFilterContext *ctx)
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,
91 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
92 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
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,
98 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
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,
110 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
112 return AVERROR(ENOMEM);
113 return ff_set_common_formats(ctx, fmts_list);
116 static int config_output(AVFilterLink *outlink)
118 AVFilterContext *ctx = outlink->src;
119 AVFilterLink *inlink = ctx->inputs[0];
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;
129 typedef struct ThreadData {
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) \
138 return (prev[av_clip(x + k, 0, end)] + \
139 next[av_clip(x - k, 0, end)] + 1) >> 1; \
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) \
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, \
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) \
168 return FFABS(prev[av_clip(x + k + j, 0, end)] - \
169 next[av_clip(x - k + j, 0, end)]); \
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) \
180 const int m = midl_##ss(prev, next, end, x, k); \
181 const int p = prev[x]; \
182 const int n = next[x]; \
184 return FFABS(p - m) + FFABS(n - m); \
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, \
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; \
211 for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) { \
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); \
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); \
224 dmin = FFMIN(sD[i + rslope], dmin); \
227 for (int i = -rslope; i <= rslope; i++) { \
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); \
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); \
240 dmin = FFMIN(sd[i + rslope], dmin); \
243 for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) { \
244 if (dmin == sD[i + rslope]) { \
251 for (int i = -rslope; i <= rslope && !di; i++) { \
252 if (dmin == sd[i + rslope]) { \
258 dst[x] = mid4_##ss(prev_line, next_line, prev2_line, next2_line, \
264 INTERPOLATE(uint8_t, unsigned, UINT_MAX, 8)
265 INTERPOLATE(uint16_t, uint64_t, UINT64_MAX, 16)
267 static int deinterlace_slice(AVFilterContext *ctx, void *arg,
268 int jobnr, int nb_jobs)
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 :
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;
296 y_out = start + (tff ^ (start & 1));
298 in_line = src_data + (y_out * src_linesize);
299 out_line = dst_data + (y_out * dst_linesize);
301 while (y_out < end) {
302 memcpy(out_line, in_line, linesize);
304 in_line += src_linesize * 2;
305 out_line += dst_linesize * 2;
308 y_out = start + ((!tff) ^ (start & 1));
309 out_line = dst_data + (y_out * dst_linesize);
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;
318 while (y_prev2_in < 0)
321 while (y_next2_in >= height)
324 while (y_prev_in < 0)
327 while (y_next_in >= height)
330 prev2_line = src_data + (y_prev2_in * src_linesize);
331 next2_line = src_data + (y_next2_in * src_linesize);
333 prev_line = src_data + (y_prev_in * src_linesize);
334 next_line = src_data + (y_next_in * src_linesize);
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);
345 out_line += 2 * dst_linesize;
352 static int filter(AVFilterContext *ctx, int is_second, AVFrame *in)
354 ESTDIFContext *s = ctx->priv;
355 AVFilterLink *outlink = ctx->outputs[0];
359 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
361 return AVERROR(ENOMEM);
362 av_frame_copy_props(out, in);
363 out->interlaced_frame = 0;
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));
371 s->field = !s->field;
373 return ff_filter_frame(outlink, out);
376 static int config_input(AVFilterLink *inlink)
378 AVFilterContext *ctx = inlink->dst;
379 ESTDIFContext *s = ctx->priv;
380 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
383 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
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;
392 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
393 return AVERROR(EINVAL);
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);
404 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
406 AVFilterContext *ctx = inlink->dst;
407 ESTDIFContext *s = ctx->priv;
415 if ((s->deint && !in->interlaced_frame) || ctx->is_disabled) {
417 ret = ff_filter_frame(ctx->outputs[0], s->prev);
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);
430 s->pts = s->prev->pts + in->pts;
431 ret = filter(ctx, 1, s->prev);
432 av_frame_free(&s->prev);
437 static int request_frame(AVFilterLink *link)
439 AVFilterContext *ctx = link->src;
440 ESTDIFContext *s = ctx->priv;
446 ret = ff_request_frame(ctx->inputs[0]);
448 if (ret == AVERROR_EOF && s->prev) {
449 AVFrame *next = av_frame_clone(s->prev);
452 return AVERROR(ENOMEM);
454 next->pts = s->prev->pts + av_rescale_q(1, av_inv_q(ctx->outputs[0]->frame_rate),
455 ctx->outputs[0]->time_base);
457 ret = filter_frame(ctx->inputs[0], next);
458 } else if (ret < 0) {
465 static av_cold void uninit(AVFilterContext *ctx)
467 ESTDIFContext *s = ctx->priv;
469 av_frame_free(&s->prev);
472 static const AVFilterPad estdif_inputs[] = {
475 .type = AVMEDIA_TYPE_VIDEO,
476 .filter_frame = filter_frame,
477 .config_props = config_input,
482 static const AVFilterPad estdif_outputs[] = {
485 .type = AVMEDIA_TYPE_VIDEO,
486 .config_props = config_output,
487 .request_frame = request_frame,
492 AVFilter ff_vf_estdif = {
494 .description = NULL_IF_CONFIG_SMALL("Apply Edge Slope Tracing deinterlace."),
495 .priv_size = sizeof(ESTDIFContext),
496 .priv_class = &estdif_class,
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,