2 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3 * 2010 James Darnley <james.darnley@gmail.com>
5 * FFmpeg is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * FFmpeg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "libavutil/avassert.h"
21 #include "libavutil/cpu.h"
22 #include "libavutil/common.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
34 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
37 { int score = FFABS(cur[mrefs + off_left + (j)] - cur[prefs + off_left - (j)])\
38 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
39 + FFABS(cur[mrefs + off_right + (j)] - cur[prefs + off_right - (j)]);\
40 if (score < spatial_score) {\
41 spatial_score= score;\
42 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
44 #define FILTER(start, end) \
45 for (x = start; x < end; x++) { \
47 int d = (prev2[0] + next2[0])>>1; \
49 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
50 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
51 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
52 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
53 int spatial_pred = (c+e) >> 1; \
54 int off_right = (x < w - 1) ? 1 : -1;\
55 int off_left = x ? -1 : 1;\
56 int spatial_score = FFABS(cur[mrefs + off_left] - cur[prefs + off_left]) + FFABS(c-e) \
57 + FFABS(cur[mrefs + off_right] - cur[prefs + off_right]) - 1; \
59 if (x > 2 && x < w - 3) {\
60 CHECK(-1) CHECK(-2) }} }} \
61 CHECK( 1) CHECK( 2) }} }} \
65 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
66 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
67 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
68 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
70 diff = FFMAX3(diff, min, -max); \
73 if (spatial_pred > d + diff) \
74 spatial_pred = d + diff; \
75 else if (spatial_pred < d - diff) \
76 spatial_pred = d - diff; \
78 dst[0] = spatial_pred; \
88 static void filter_line_c(void *dst1,
89 void *prev1, void *cur1, void *next1,
90 int w, int prefs, int mrefs, int parity, int mode)
93 uint8_t *prev = prev1;
95 uint8_t *next = next1;
97 uint8_t *prev2 = parity ? prev : cur ;
98 uint8_t *next2 = parity ? cur : next;
103 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
104 int w, int prefs, int mrefs, int parity, int mode,
108 uint8_t *prev = prev1;
110 uint8_t *next = next1;
112 uint8_t *prev2 = parity ? prev : cur ;
113 uint8_t *next2 = parity ? cur : next;
117 dst = (uint8_t*)dst1 + w - 3;
118 prev = (uint8_t*)prev1 + w - 3;
119 cur = (uint8_t*)cur1 + w - 3;
120 next = (uint8_t*)next1 + w - 3;
121 prev2 = (uint8_t*)(parity ? prev : cur);
122 next2 = (uint8_t*)(parity ? cur : next);
128 static void filter_line_c_16bit(void *dst1,
129 void *prev1, void *cur1, void *next1,
130 int w, int prefs, int mrefs, int parity,
133 uint16_t *dst = dst1;
134 uint16_t *prev = prev1;
135 uint16_t *cur = cur1;
136 uint16_t *next = next1;
138 uint16_t *prev2 = parity ? prev : cur ;
139 uint16_t *next2 = parity ? cur : next;
146 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
147 int w, int prefs, int mrefs, int parity, int mode,
150 uint16_t *dst = dst1;
151 uint16_t *prev = prev1;
152 uint16_t *cur = cur1;
153 uint16_t *next = next1;
155 uint16_t *prev2 = parity ? prev : cur ;
156 uint16_t *next2 = parity ? cur : next;
160 dst = (uint16_t*)dst1 + w - 3;
161 prev = (uint16_t*)prev1 + w - 3;
162 cur = (uint16_t*)cur1 + w - 3;
163 next = (uint16_t*)next1 + w - 3;
164 prev2 = (uint16_t*)(parity ? prev : cur);
165 next2 = (uint16_t*)(parity ? cur : next);
170 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
173 YADIFContext *yadif = ctx->priv;
176 for (i = 0; i < yadif->csp->nb_components; i++) {
177 int w = dstpic->video->w;
178 int h = dstpic->video->h;
179 int refs = yadif->cur->linesize[i];
180 int absrefs = FFABS(refs);
181 int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
182 int l_edge, l_edge_pix;
184 if (i == 1 || i == 2) {
185 /* Why is this not part of the per-plane description thing? */
186 w >>= yadif->csp->log2_chroma_w;
187 h >>= yadif->csp->log2_chroma_h;
190 /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
191 * we need to call the c variant which avoids this for border pixels
193 l_edge = yadif->req_align;
194 l_edge_pix = l_edge / df;
196 for (y = 0; y < h; y++) {
197 if ((y ^ parity) & 1) {
198 uint8_t *prev = &yadif->prev->data[i][y * refs];
199 uint8_t *cur = &yadif->cur ->data[i][y * refs];
200 uint8_t *next = &yadif->next->data[i][y * refs];
201 uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
202 int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
203 if (yadif->req_align) {
204 yadif->filter_line(dst + l_edge, prev + l_edge, cur + l_edge,
205 next + l_edge, w - l_edge_pix - 3,
206 y + 1 < h ? refs : -refs,
209 yadif->filter_edges(dst, prev, cur, next, w,
210 y + 1 < h ? refs : -refs,
212 parity ^ tff, mode, l_edge_pix);
214 yadif->filter_line(dst, prev, cur, next + l_edge, w,
215 y + 1 < h ? refs : -refs,
220 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
221 &yadif->cur->data[i][y * refs], w * df);
229 static int return_frame(AVFilterContext *ctx, int is_second)
231 YADIFContext *yadif = ctx->priv;
232 AVFilterLink *link = ctx->outputs[0];
235 if (yadif->parity == -1) {
236 tff = yadif->cur->video->interlaced ?
237 yadif->cur->video->top_field_first : 1;
239 tff = yadif->parity ^ 1;
243 yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
245 return AVERROR(ENOMEM);
247 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
248 yadif->out->video->interlaced = 0;
251 filter(ctx, yadif->out, tff ^ !is_second, tff);
254 int64_t cur_pts = yadif->cur->pts;
255 int64_t next_pts = yadif->next->pts;
257 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
258 yadif->out->pts = cur_pts + next_pts;
260 yadif->out->pts = AV_NOPTS_VALUE;
263 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
265 yadif->frame_pending = (yadif->mode&1) && !is_second;
269 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
271 AVFilterContext *ctx = link->dst;
272 YADIFContext *yadif = ctx->priv;
276 if (yadif->frame_pending)
277 return_frame(ctx, 1);
280 avfilter_unref_buffer(yadif->prev);
281 yadif->prev = yadif->cur;
282 yadif->cur = yadif->next;
283 yadif->next = picref;
288 if (yadif->deint && !yadif->cur->video->interlaced) {
289 yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
291 return AVERROR(ENOMEM);
293 avfilter_unref_bufferp(&yadif->prev);
294 if (yadif->out->pts != AV_NOPTS_VALUE)
295 yadif->out->pts *= 2;
296 return ff_filter_frame(ctx->outputs[0], yadif->out);
300 !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
301 return AVERROR(ENOMEM);
303 yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
306 return AVERROR(ENOMEM);
308 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
309 yadif->out->video->interlaced = 0;
311 if (yadif->out->pts != AV_NOPTS_VALUE)
312 yadif->out->pts *= 2;
314 return return_frame(ctx, 0);
317 static int request_frame(AVFilterLink *link)
319 AVFilterContext *ctx = link->src;
320 YADIFContext *yadif = ctx->priv;
322 if (yadif->frame_pending) {
323 return_frame(ctx, 1);
333 ret = ff_request_frame(link->src->inputs[0]);
335 if (ret == AVERROR_EOF && yadif->cur) {
336 AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
339 return AVERROR(ENOMEM);
341 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
343 filter_frame(link->src->inputs[0], next);
345 } else if (ret < 0) {
348 } while (!yadif->cur);
353 #define OFFSET(x) offsetof(YADIFContext, x)
354 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
356 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
358 static const AVOption yadif_options[] = {
359 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
360 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
361 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
362 CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
363 CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
365 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
366 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
367 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
368 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
370 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
371 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
372 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
377 AVFILTER_DEFINE_CLASS(yadif);
379 static av_cold void uninit(AVFilterContext *ctx)
381 YADIFContext *yadif = ctx->priv;
383 avfilter_unref_bufferp(&yadif->prev);
384 avfilter_unref_bufferp(&yadif->cur );
385 avfilter_unref_bufferp(&yadif->next);
389 static int query_formats(AVFilterContext *ctx)
391 static const enum AVPixelFormat pix_fmts[] = {
401 AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
404 AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
405 AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
406 AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
407 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
408 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
409 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
410 AV_NE( AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE ),
411 AV_NE( AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE ),
412 AV_NE( AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE ),
413 AV_NE( AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE ),
414 AV_NE( AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE ),
415 AV_NE( AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE ),
416 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
417 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
418 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
425 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
430 static av_cold int init(AVFilterContext *ctx, const char *args)
432 YADIFContext *yadif = ctx->priv;
433 static const char *shorthand[] = { "mode", "parity", "deint", NULL };
436 yadif->class = &yadif_class;
437 av_opt_set_defaults(yadif);
439 if ((ret = av_opt_set_from_string(yadif, args, shorthand, "=", ":")) < 0)
442 av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d deint:%d\n",
443 yadif->mode, yadif->parity, yadif->deint);
448 static int config_props(AVFilterLink *link)
450 AVFilterContext *ctx = link->src;
451 YADIFContext *s = link->src->priv;
453 link->time_base.num = link->src->inputs[0]->time_base.num;
454 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
455 link->w = link->src->inputs[0]->w;
456 link->h = link->src->inputs[0]->h;
459 link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
461 if (link->w < 3 || link->h < 3) {
462 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
463 return AVERROR(EINVAL);
466 s->csp = av_pix_fmt_desc_get(link->format);
467 if (s->csp->comp[0].depth_minus1 / 8 == 1) {
468 s->filter_line = filter_line_c_16bit;
469 s->filter_edges = filter_edges_16bit;
471 s->filter_line = filter_line_c;
472 s->filter_edges = filter_edges;
475 ff_yadif_init_x86(s);
481 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
484 .type = AVMEDIA_TYPE_VIDEO,
485 .filter_frame = filter_frame,
486 .min_perms = AV_PERM_PRESERVE,
491 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
494 .type = AVMEDIA_TYPE_VIDEO,
495 .request_frame = request_frame,
496 .config_props = config_props,
501 AVFilter avfilter_vf_yadif = {
503 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
505 .priv_size = sizeof(YADIFContext),
508 .query_formats = query_formats,
510 .inputs = avfilter_vf_yadif_inputs,
511 .outputs = avfilter_vf_yadif_outputs,
513 .priv_class = &yadif_class,