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"
35 { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
36 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
37 + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
38 if (score < spatial_score) {\
39 spatial_score= score;\
40 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
42 /* The is_not_edge argument here controls when the code will enter a branch
43 * which reads up to and including x-3 and x+3. */
45 #define FILTER(start, end, is_not_edge) \
46 for (x = start; x < end; x++) { \
48 int d = (prev2[0] + next2[0])>>1; \
50 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
51 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
52 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
53 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
54 int spatial_pred = (c+e) >> 1; \
57 int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
58 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
59 CHECK(-1) CHECK(-2) }} }} \
60 CHECK( 1) CHECK( 2) }} }} \
64 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
65 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
66 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
67 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
69 diff = FFMAX3(diff, min, -max); \
72 if (spatial_pred > d + diff) \
73 spatial_pred = d + diff; \
74 else if (spatial_pred < d - diff) \
75 spatial_pred = d - diff; \
77 dst[0] = spatial_pred; \
87 static void filter_line_c(void *dst1,
88 void *prev1, void *cur1, void *next1,
89 int w, int prefs, int mrefs, int parity, int mode)
92 uint8_t *prev = prev1;
94 uint8_t *next = next1;
96 uint8_t *prev2 = parity ? prev : cur ;
97 uint8_t *next2 = parity ? cur : next;
99 /* The function is called with the pointers already pointing to data[3] and
100 * with 6 subtracted from the width. This allows the FILTER macro to be
101 * called so that it processes all the pixels normally. A constant value of
102 * true for is_not_edge lets the compiler ignore the if statement. */
106 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
107 int w, int prefs, int mrefs, int parity, int mode)
110 uint8_t *prev = prev1;
112 uint8_t *next = next1;
114 uint8_t *prev2 = parity ? prev : cur ;
115 uint8_t *next2 = parity ? cur : next;
117 /* Only edge pixels need to be processed here. A constant value of false
118 * for is_not_edge should let the compiler ignore the whole branch. */
121 dst = (uint8_t*)dst1 + w - 3;
122 prev = (uint8_t*)prev1 + w - 3;
123 cur = (uint8_t*)cur1 + w - 3;
124 next = (uint8_t*)next1 + w - 3;
125 prev2 = (uint8_t*)(parity ? prev : cur);
126 next2 = (uint8_t*)(parity ? cur : next);
132 static void filter_line_c_16bit(void *dst1,
133 void *prev1, void *cur1, void *next1,
134 int w, int prefs, int mrefs, int parity,
137 uint16_t *dst = dst1;
138 uint16_t *prev = prev1;
139 uint16_t *cur = cur1;
140 uint16_t *next = next1;
142 uint16_t *prev2 = parity ? prev : cur ;
143 uint16_t *next2 = parity ? cur : next;
150 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
151 int w, int prefs, int mrefs, int parity, int mode)
153 uint16_t *dst = dst1;
154 uint16_t *prev = prev1;
155 uint16_t *cur = cur1;
156 uint16_t *next = next1;
158 uint16_t *prev2 = parity ? prev : cur ;
159 uint16_t *next2 = parity ? cur : next;
165 dst = (uint16_t*)dst1 + w - 3;
166 prev = (uint16_t*)prev1 + w - 3;
167 cur = (uint16_t*)cur1 + w - 3;
168 next = (uint16_t*)next1 + w - 3;
169 prev2 = (uint16_t*)(parity ? prev : cur);
170 next2 = (uint16_t*)(parity ? cur : next);
175 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
178 YADIFContext *yadif = ctx->priv;
181 for (i = 0; i < yadif->csp->nb_components; i++) {
182 int w = dstpic->width;
183 int h = dstpic->height;
184 int refs = yadif->cur->linesize[i];
185 int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
188 if (i == 1 || i == 2) {
189 /* Why is this not part of the per-plane description thing? */
190 w >>= yadif->csp->log2_chroma_w;
191 h >>= yadif->csp->log2_chroma_h;
194 /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
195 * we need to call the c variant which avoids this for border pixels
198 for (y = 0; y < h; y++) {
199 if ((y ^ parity) & 1) {
200 uint8_t *prev = &yadif->prev->data[i][y * refs];
201 uint8_t *cur = &yadif->cur ->data[i][y * refs];
202 uint8_t *next = &yadif->next->data[i][y * refs];
203 uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
204 int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
205 yadif->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
207 y + 1 < h ? refs : -refs,
210 yadif->filter_edges(dst, prev, cur, next, w,
211 y + 1 < h ? refs : -refs,
215 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
216 &yadif->cur->data[i][y * refs], w * df);
224 static int return_frame(AVFilterContext *ctx, int is_second)
226 YADIFContext *yadif = ctx->priv;
227 AVFilterLink *link = ctx->outputs[0];
230 if (yadif->parity == -1) {
231 tff = yadif->cur->interlaced_frame ?
232 yadif->cur->top_field_first : 1;
234 tff = yadif->parity ^ 1;
238 yadif->out = ff_get_video_buffer(link, link->w, link->h);
240 return AVERROR(ENOMEM);
242 av_frame_copy_props(yadif->out, yadif->cur);
243 yadif->out->interlaced_frame = 0;
246 filter(ctx, yadif->out, tff ^ !is_second, tff);
249 int64_t cur_pts = yadif->cur->pts;
250 int64_t next_pts = yadif->next->pts;
252 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
253 yadif->out->pts = cur_pts + next_pts;
255 yadif->out->pts = AV_NOPTS_VALUE;
258 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
260 yadif->frame_pending = (yadif->mode&1) && !is_second;
264 static int filter_frame(AVFilterLink *link, AVFrame *frame)
266 AVFilterContext *ctx = link->dst;
267 YADIFContext *yadif = ctx->priv;
271 if (yadif->frame_pending)
272 return_frame(ctx, 1);
275 av_frame_free(&yadif->prev);
276 yadif->prev = yadif->cur;
277 yadif->cur = yadif->next;
283 if (yadif->deint && !yadif->cur->interlaced_frame) {
284 yadif->out = av_frame_clone(yadif->cur);
286 return AVERROR(ENOMEM);
288 av_frame_free(&yadif->prev);
289 if (yadif->out->pts != AV_NOPTS_VALUE)
290 yadif->out->pts *= 2;
291 return ff_filter_frame(ctx->outputs[0], yadif->out);
295 !(yadif->prev = av_frame_clone(yadif->cur)))
296 return AVERROR(ENOMEM);
298 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
300 return AVERROR(ENOMEM);
302 av_frame_copy_props(yadif->out, yadif->cur);
303 yadif->out->interlaced_frame = 0;
305 if (yadif->out->pts != AV_NOPTS_VALUE)
306 yadif->out->pts *= 2;
308 return return_frame(ctx, 0);
311 static int request_frame(AVFilterLink *link)
313 AVFilterContext *ctx = link->src;
314 YADIFContext *yadif = ctx->priv;
316 if (yadif->frame_pending) {
317 return_frame(ctx, 1);
327 ret = ff_request_frame(link->src->inputs[0]);
329 if (ret == AVERROR_EOF && yadif->cur) {
330 AVFrame *next = av_frame_clone(yadif->next);
333 return AVERROR(ENOMEM);
335 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
337 filter_frame(link->src->inputs[0], next);
339 } else if (ret < 0) {
342 } while (!yadif->cur);
347 static av_cold void uninit(AVFilterContext *ctx)
349 YADIFContext *yadif = ctx->priv;
351 av_frame_free(&yadif->prev);
352 av_frame_free(&yadif->cur );
353 av_frame_free(&yadif->next);
356 static int query_formats(AVFilterContext *ctx)
358 static const enum AVPixelFormat pix_fmts[] = {
368 AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
371 AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
372 AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
373 AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
374 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
375 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
376 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
377 AV_NE( AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE ),
378 AV_NE( AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE ),
379 AV_NE( AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE ),
380 AV_NE( AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE ),
381 AV_NE( AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE ),
382 AV_NE( AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE ),
383 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
384 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
385 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
392 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
397 static int config_props(AVFilterLink *link)
399 AVFilterContext *ctx = link->src;
400 YADIFContext *s = link->src->priv;
402 link->time_base.num = link->src->inputs[0]->time_base.num;
403 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
404 link->w = link->src->inputs[0]->w;
405 link->h = link->src->inputs[0]->h;
408 link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
410 if (link->w < 3 || link->h < 3) {
411 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
412 return AVERROR(EINVAL);
415 s->csp = av_pix_fmt_desc_get(link->format);
416 if (s->csp->comp[0].depth_minus1 / 8 == 1) {
417 s->filter_line = filter_line_c_16bit;
418 s->filter_edges = filter_edges_16bit;
420 s->filter_line = filter_line_c;
421 s->filter_edges = filter_edges;
425 ff_yadif_init_x86(s);
431 #define OFFSET(x) offsetof(YADIFContext, x)
432 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
434 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
436 static const AVOption yadif_options[] = {
437 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
438 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
439 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
440 CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
441 CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
443 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
444 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
445 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
446 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
448 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
449 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
450 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
455 AVFILTER_DEFINE_CLASS(yadif);
457 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
460 .type = AVMEDIA_TYPE_VIDEO,
461 .filter_frame = filter_frame,
466 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
469 .type = AVMEDIA_TYPE_VIDEO,
470 .request_frame = request_frame,
471 .config_props = config_props,
476 AVFilter avfilter_vf_yadif = {
478 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
480 .priv_size = sizeof(YADIFContext),
481 .priv_class = &yadif_class,
483 .query_formats = query_formats,
485 .inputs = avfilter_vf_yadif_inputs,
486 .outputs = avfilter_vf_yadif_outputs,