2 * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
3 * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
4 * Based on the process described by Martin Weston for BBC R&D
5 * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/common.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
34 typedef struct W3FDIFContext {
36 int filter; ///< 0 is simple, 1 is more complex
37 int mode; ///< 0 is frame, 1 is field
38 int parity; ///< frame field parity
39 int deint; ///< which frames to deinterlace
40 int linesize[4]; ///< bytes of pixel data per line for each plane
41 int planeheight[4]; ///< height of each plane
42 int field; ///< which field are we on, 0 or 1
45 AVFrame *prev, *cur, *next; ///< previous, current, next frames
46 int32_t **work_line; ///< lines we are calculating
53 #define OFFSET(x) offsetof(W3FDIFContext, x)
54 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
55 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
57 static const AVOption w3fdif_options[] = {
58 { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
59 CONST("simple", NULL, 0, "filter"),
60 CONST("complex", NULL, 1, "filter"),
61 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode"},
62 CONST("frame", "send one frame for each frame", 0, "mode"),
63 CONST("field", "send one frame for each field", 1, "mode"),
64 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
65 CONST("tff", "assume top field first", 0, "parity"),
66 CONST("bff", "assume bottom field first", 1, "parity"),
67 CONST("auto", "auto detect parity", -1, "parity"),
68 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
69 CONST("all", "deinterlace all frames", 0, "deint"),
70 CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
74 AVFILTER_DEFINE_CLASS(w3fdif);
76 static int query_formats(AVFilterContext *ctx)
78 static const enum AVPixelFormat pix_fmts[] = {
79 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
80 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
81 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
82 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
83 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
85 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
86 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
88 AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
89 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
90 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
91 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
92 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
93 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
94 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
95 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
96 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
97 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
101 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
103 return AVERROR(ENOMEM);
104 return ff_set_common_formats(ctx, fmts_list);
107 static void filter_simple_low(int32_t *work_line,
108 uint8_t *in_lines_cur[2],
109 const int16_t *coef, int linesize)
113 for (i = 0; i < linesize; i++) {
114 *work_line = *in_lines_cur[0]++ * coef[0];
115 *work_line++ += *in_lines_cur[1]++ * coef[1];
119 static void filter_complex_low(int32_t *work_line,
120 uint8_t *in_lines_cur[4],
121 const int16_t *coef, int linesize)
125 for (i = 0; i < linesize; i++) {
126 *work_line = *in_lines_cur[0]++ * coef[0];
127 *work_line += *in_lines_cur[1]++ * coef[1];
128 *work_line += *in_lines_cur[2]++ * coef[2];
129 *work_line++ += *in_lines_cur[3]++ * coef[3];
133 static void filter_simple_high(int32_t *work_line,
134 uint8_t *in_lines_cur[3],
135 uint8_t *in_lines_adj[3],
136 const int16_t *coef, int linesize)
140 for (i = 0; i < linesize; i++) {
141 *work_line += *in_lines_cur[0]++ * coef[0];
142 *work_line += *in_lines_adj[0]++ * coef[0];
143 *work_line += *in_lines_cur[1]++ * coef[1];
144 *work_line += *in_lines_adj[1]++ * coef[1];
145 *work_line += *in_lines_cur[2]++ * coef[2];
146 *work_line++ += *in_lines_adj[2]++ * coef[2];
150 static void filter_complex_high(int32_t *work_line,
151 uint8_t *in_lines_cur[5],
152 uint8_t *in_lines_adj[5],
153 const int16_t *coef, int linesize)
157 for (i = 0; i < linesize; i++) {
158 *work_line += *in_lines_cur[0]++ * coef[0];
159 *work_line += *in_lines_adj[0]++ * coef[0];
160 *work_line += *in_lines_cur[1]++ * coef[1];
161 *work_line += *in_lines_adj[1]++ * coef[1];
162 *work_line += *in_lines_cur[2]++ * coef[2];
163 *work_line += *in_lines_adj[2]++ * coef[2];
164 *work_line += *in_lines_cur[3]++ * coef[3];
165 *work_line += *in_lines_adj[3]++ * coef[3];
166 *work_line += *in_lines_cur[4]++ * coef[4];
167 *work_line++ += *in_lines_adj[4]++ * coef[4];
171 static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max)
175 for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
176 *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
179 static void filter16_simple_low(int32_t *work_line,
180 uint8_t *in_lines_cur8[2],
181 const int16_t *coef, int linesize)
183 uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] };
187 for (i = 0; i < linesize; i++) {
188 *work_line = *in_lines_cur[0]++ * coef[0];
189 *work_line++ += *in_lines_cur[1]++ * coef[1];
193 static void filter16_complex_low(int32_t *work_line,
194 uint8_t *in_lines_cur8[4],
195 const int16_t *coef, int linesize)
197 uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0],
198 (uint16_t *)in_lines_cur8[1],
199 (uint16_t *)in_lines_cur8[2],
200 (uint16_t *)in_lines_cur8[3] };
204 for (i = 0; i < linesize; i++) {
205 *work_line = *in_lines_cur[0]++ * coef[0];
206 *work_line += *in_lines_cur[1]++ * coef[1];
207 *work_line += *in_lines_cur[2]++ * coef[2];
208 *work_line++ += *in_lines_cur[3]++ * coef[3];
212 static void filter16_simple_high(int32_t *work_line,
213 uint8_t *in_lines_cur8[3],
214 uint8_t *in_lines_adj8[3],
215 const int16_t *coef, int linesize)
217 uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0],
218 (uint16_t *)in_lines_cur8[1],
219 (uint16_t *)in_lines_cur8[2] };
220 uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0],
221 (uint16_t *)in_lines_adj8[1],
222 (uint16_t *)in_lines_adj8[2] };
226 for (i = 0; i < linesize; i++) {
227 *work_line += *in_lines_cur[0]++ * coef[0];
228 *work_line += *in_lines_adj[0]++ * coef[0];
229 *work_line += *in_lines_cur[1]++ * coef[1];
230 *work_line += *in_lines_adj[1]++ * coef[1];
231 *work_line += *in_lines_cur[2]++ * coef[2];
232 *work_line++ += *in_lines_adj[2]++ * coef[2];
236 static void filter16_complex_high(int32_t *work_line,
237 uint8_t *in_lines_cur8[5],
238 uint8_t *in_lines_adj8[5],
239 const int16_t *coef, int linesize)
241 uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0],
242 (uint16_t *)in_lines_cur8[1],
243 (uint16_t *)in_lines_cur8[2],
244 (uint16_t *)in_lines_cur8[3],
245 (uint16_t *)in_lines_cur8[4] };
246 uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0],
247 (uint16_t *)in_lines_adj8[1],
248 (uint16_t *)in_lines_adj8[2],
249 (uint16_t *)in_lines_adj8[3],
250 (uint16_t *)in_lines_adj8[4] };
254 for (i = 0; i < linesize; i++) {
255 *work_line += *in_lines_cur[0]++ * coef[0];
256 *work_line += *in_lines_adj[0]++ * coef[0];
257 *work_line += *in_lines_cur[1]++ * coef[1];
258 *work_line += *in_lines_adj[1]++ * coef[1];
259 *work_line += *in_lines_cur[2]++ * coef[2];
260 *work_line += *in_lines_adj[2]++ * coef[2];
261 *work_line += *in_lines_cur[3]++ * coef[3];
262 *work_line += *in_lines_adj[3]++ * coef[3];
263 *work_line += *in_lines_cur[4]++ * coef[4];
264 *work_line++ += *in_lines_adj[4]++ * coef[4];
268 static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max)
270 uint16_t *out_pixel = (uint16_t *)out_pixel8;
274 for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
275 *out_pixel = av_clip(*work_pixel, 0, max) >> 15;
278 static int config_input(AVFilterLink *inlink)
280 AVFilterContext *ctx = inlink->dst;
281 W3FDIFContext *s = ctx->priv;
282 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
285 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
288 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
289 s->planeheight[0] = s->planeheight[3] = inlink->h;
292 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
293 return AVERROR(EINVAL);
296 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
297 s->nb_threads = ff_filter_get_nb_threads(ctx);
298 s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
300 return AVERROR(ENOMEM);
302 for (i = 0; i < s->nb_threads; i++) {
303 s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
304 if (!s->work_line[i])
305 return AVERROR(ENOMEM);
308 depth = desc->comp[0].depth;
309 s->max = ((1 << depth) - 1) * 256 * 128;
311 s->dsp.filter_simple_low = filter_simple_low;
312 s->dsp.filter_complex_low = filter_complex_low;
313 s->dsp.filter_simple_high = filter_simple_high;
314 s->dsp.filter_complex_high = filter_complex_high;
315 s->dsp.filter_scale = filter_scale;
317 s->dsp.filter_simple_low = filter16_simple_low;
318 s->dsp.filter_complex_low = filter16_complex_low;
319 s->dsp.filter_simple_high = filter16_simple_high;
320 s->dsp.filter_complex_high = filter16_complex_high;
321 s->dsp.filter_scale = filter16_scale;
325 ff_w3fdif_init_x86(&s->dsp, depth);
330 static int config_output(AVFilterLink *outlink)
332 AVFilterLink *inlink = outlink->src->inputs[0];
334 outlink->time_base.num = inlink->time_base.num;
335 outlink->time_base.den = inlink->time_base.den * 2;
336 outlink->frame_rate.num = inlink->frame_rate.num * 2;
337 outlink->frame_rate.den = inlink->frame_rate.den;
343 * Filter coefficients from PH-2071, scaled by 256 * 128.
344 * Each set of coefficients has a set for low-frequencies and high-frequencies.
345 * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
346 * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
347 * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
348 * and high-frequencies for simple and more-complex mode.
350 static const int8_t n_coef_lf[2] = { 2, 4 };
351 static const int16_t coef_lf[2][4] = {{ 16384, 16384, 0, 0},
352 { -852, 17236, 17236, -852}};
353 static const int8_t n_coef_hf[2] = { 3, 5 };
354 static const int16_t coef_hf[2][5] = {{ -2048, 4096, -2048, 0, 0},
355 { 1016, -3801, 5570, -3801, 1016}};
357 typedef struct ThreadData {
358 AVFrame *out, *cur, *adj;
362 static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
364 W3FDIFContext *s = ctx->priv;
365 ThreadData *td = arg;
366 AVFrame *out = td->out;
367 AVFrame *cur = td->cur;
368 AVFrame *adj = td->adj;
369 const int plane = td->plane;
370 const int filter = s->filter;
371 uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
372 uint8_t *out_line, *out_pixel;
373 int32_t *work_line, *work_pixel;
374 uint8_t *cur_data = cur->data[plane];
375 uint8_t *adj_data = adj->data[plane];
376 uint8_t *dst_data = out->data[plane];
377 const int linesize = s->linesize[plane];
378 const int height = s->planeheight[plane];
379 const int cur_line_stride = cur->linesize[plane];
380 const int adj_line_stride = adj->linesize[plane];
381 const int dst_line_stride = out->linesize[plane];
382 const int start = (height * jobnr) / nb_jobs;
383 const int end = (height * (jobnr+1)) / nb_jobs;
384 const int max = s->max;
385 const int interlaced = cur->interlaced_frame;
386 const int tff = s->field == (s->parity == -1 ? interlaced ? cur->top_field_first : 1 :
390 /* copy unchanged the lines of the field */
391 y_out = start + (tff ^ (start & 1));
393 in_line = cur_data + (y_out * cur_line_stride);
394 out_line = dst_data + (y_out * dst_line_stride);
396 while (y_out < end) {
397 memcpy(out_line, in_line, linesize);
399 in_line += cur_line_stride * 2;
400 out_line += dst_line_stride * 2;
403 /* interpolate other lines of the field */
404 y_out = start + ((!tff) ^ (start & 1));
406 out_line = dst_data + (y_out * dst_line_stride);
408 while (y_out < end) {
409 /* get low vertical frequencies from current field */
410 for (j = 0; j < n_coef_lf[filter]; j++) {
411 y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
415 while (y_in >= height)
418 in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
421 work_line = s->work_line[jobnr];
422 switch (n_coef_lf[filter]) {
424 s->dsp.filter_simple_low(work_line, in_lines_cur,
425 coef_lf[filter], linesize);
428 s->dsp.filter_complex_low(work_line, in_lines_cur,
429 coef_lf[filter], linesize);
432 /* get high vertical frequencies from adjacent fields */
433 for (j = 0; j < n_coef_hf[filter]; j++) {
434 y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
438 while (y_in >= height)
441 in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
442 in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
445 work_line = s->work_line[jobnr];
446 switch (n_coef_hf[filter]) {
448 s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
449 coef_hf[filter], linesize);
452 s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
453 coef_hf[filter], linesize);
456 /* save scaled result to the output frame, scaling down by 256 * 128 */
457 work_pixel = s->work_line[jobnr];
458 out_pixel = out_line;
460 s->dsp.filter_scale(out_pixel, work_pixel, linesize, max);
462 /* move on to next line */
464 out_line += dst_line_stride * 2;
470 static int filter(AVFilterContext *ctx, int is_second)
472 W3FDIFContext *s = ctx->priv;
473 AVFilterLink *outlink = ctx->outputs[0];
478 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
480 return AVERROR(ENOMEM);
481 av_frame_copy_props(out, s->cur);
482 out->interlaced_frame = 0;
485 if (out->pts != AV_NOPTS_VALUE)
488 int64_t cur_pts = s->cur->pts;
489 int64_t next_pts = s->next->pts;
491 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
492 out->pts = cur_pts + next_pts;
494 out->pts = AV_NOPTS_VALUE;
498 adj = s->field ? s->next : s->prev;
499 td.out = out; td.cur = s->cur; td.adj = adj;
500 for (plane = 0; plane < s->nb_planes; plane++) {
502 ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
506 s->field = !s->field;
508 return ff_filter_frame(outlink, out);
511 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
513 AVFilterContext *ctx = inlink->dst;
514 W3FDIFContext *s = ctx->priv;
517 av_frame_free(&s->prev);
523 s->cur = av_frame_clone(s->next);
525 return AVERROR(ENOMEM);
528 if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
529 AVFrame *out = av_frame_clone(s->cur);
531 return AVERROR(ENOMEM);
533 av_frame_free(&s->prev);
534 if (out->pts != AV_NOPTS_VALUE)
536 return ff_filter_frame(ctx->outputs[0], out);
542 ret = filter(ctx, 0);
543 if (ret < 0 || s->mode == 0)
546 return filter(ctx, 1);
549 static int request_frame(AVFilterLink *outlink)
551 AVFilterContext *ctx = outlink->src;
552 W3FDIFContext *s = ctx->priv;
558 ret = ff_request_frame(ctx->inputs[0]);
560 if (ret == AVERROR_EOF && s->cur) {
561 AVFrame *next = av_frame_clone(s->next);
563 return AVERROR(ENOMEM);
564 next->pts = s->next->pts * 2 - s->cur->pts;
565 filter_frame(ctx->inputs[0], next);
567 } else if (ret < 0) {
574 static av_cold void uninit(AVFilterContext *ctx)
576 W3FDIFContext *s = ctx->priv;
579 av_frame_free(&s->prev);
580 av_frame_free(&s->cur );
581 av_frame_free(&s->next);
583 for (i = 0; i < s->nb_threads; i++)
584 av_freep(&s->work_line[i]);
586 av_freep(&s->work_line);
589 static const AVFilterPad w3fdif_inputs[] = {
592 .type = AVMEDIA_TYPE_VIDEO,
593 .filter_frame = filter_frame,
594 .config_props = config_input,
599 static const AVFilterPad w3fdif_outputs[] = {
602 .type = AVMEDIA_TYPE_VIDEO,
603 .config_props = config_output,
604 .request_frame = request_frame,
609 AVFilter ff_vf_w3fdif = {
611 .description = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
612 .priv_size = sizeof(W3FDIFContext),
613 .priv_class = &w3fdif_class,
615 .query_formats = query_formats,
616 .inputs = w3fdif_inputs,
617 .outputs = w3fdif_outputs,
618 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
619 .process_command = ff_filter_process_command,