2 * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
3 * 2010 James Darnley <james.darnley@gmail.com>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with Libav; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "libavutil/cpu.h"
23 #include "libavutil/common.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
36 { int score = FFABS(cur[mrefs + off_left + (j)] - cur[prefs + off_left - (j)])\
37 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
38 + FFABS(cur[mrefs + off_right + (j)] - cur[prefs + off_right - (j)]);\
39 if (score < spatial_score) {\
40 spatial_score= score;\
41 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
43 #define FILTER(start, end) \
44 for (x = start; x < end; x++) { \
46 int d = (prev2[0] + next2[0])>>1; \
48 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
49 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
50 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
51 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
52 int spatial_pred = (c+e) >> 1; \
53 int off_right = (x < w - 1) ? 1 : -1;\
54 int off_left = x ? -1 : 1;\
55 int spatial_score = FFABS(cur[mrefs + off_left] - cur[prefs + off_left]) + FFABS(c-e) \
56 + FFABS(cur[mrefs + off_right] - cur[prefs + off_right]) - 1; \
58 if (x > 2 && x < w - 3) {\
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;
102 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
103 int w, int prefs, int mrefs, int parity, int mode,
107 uint8_t *prev = prev1;
109 uint8_t *next = next1;
111 uint8_t *prev2 = parity ? prev : cur ;
112 uint8_t *next2 = parity ? cur : next;
116 dst = (uint8_t*)dst1 + w - 3;
117 prev = (uint8_t*)prev1 + w - 3;
118 cur = (uint8_t*)cur1 + w - 3;
119 next = (uint8_t*)next1 + w - 3;
120 prev2 = (uint8_t*)(parity ? prev : cur);
121 next2 = (uint8_t*)(parity ? cur : next);
127 static void filter_line_c_16bit(void *dst1,
128 void *prev1, void *cur1, void *next1,
129 int w, int prefs, int mrefs, int parity,
132 uint16_t *dst = dst1;
133 uint16_t *prev = prev1;
134 uint16_t *cur = cur1;
135 uint16_t *next = next1;
137 uint16_t *prev2 = parity ? prev : cur ;
138 uint16_t *next2 = parity ? cur : next;
145 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
146 int w, int prefs, int mrefs, int parity, int mode,
149 uint16_t *dst = dst1;
150 uint16_t *prev = prev1;
151 uint16_t *cur = cur1;
152 uint16_t *next = next1;
154 uint16_t *prev2 = parity ? prev : cur ;
155 uint16_t *next2 = parity ? cur : next;
159 dst = (uint16_t*)dst1 + w - 3;
160 prev = (uint16_t*)prev1 + w - 3;
161 cur = (uint16_t*)cur1 + w - 3;
162 next = (uint16_t*)next1 + w - 3;
163 prev2 = (uint16_t*)(parity ? prev : cur);
164 next2 = (uint16_t*)(parity ? cur : next);
169 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
172 YADIFContext *yadif = ctx->priv;
175 for (i = 0; i < yadif->csp->nb_components; i++) {
176 int w = dstpic->width;
177 int h = dstpic->height;
178 int refs = yadif->cur->linesize[i];
179 int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
180 int l_edge, l_edge_pix;
182 if (i == 1 || i == 2) {
183 /* Why is this not part of the per-plane description thing? */
184 w >>= yadif->csp->log2_chroma_w;
185 h >>= yadif->csp->log2_chroma_h;
188 /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
189 * we need to call the c variant which avoids this for border pixels
191 l_edge = yadif->req_align;
192 l_edge_pix = l_edge / df;
194 for (y = 0; y < h; y++) {
195 if ((y ^ parity) & 1) {
196 uint8_t *prev = &yadif->prev->data[i][y * refs];
197 uint8_t *cur = &yadif->cur ->data[i][y * refs];
198 uint8_t *next = &yadif->next->data[i][y * refs];
199 uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
200 int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
201 if (yadif->req_align) {
202 yadif->filter_line(dst + l_edge, prev + l_edge, cur + l_edge,
203 next + l_edge, w - l_edge_pix - 3,
204 y + 1 < h ? refs : -refs,
207 yadif->filter_edges(dst, prev, cur, next, w,
208 y + 1 < h ? refs : -refs,
210 parity ^ tff, mode, l_edge_pix);
212 yadif->filter_line(dst, prev, cur, next + l_edge, w,
213 y + 1 < h ? refs : -refs,
218 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
219 &yadif->cur->data[i][y * refs], w * df);
227 static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
230 int width = FFALIGN(w, 32);
231 int height = FFALIGN(h + 2, 32);
234 frame = ff_default_get_video_buffer(link, width, height);
239 for (i = 0; i < 3; i++)
240 frame->data[i] += frame->linesize[i];
245 static int return_frame(AVFilterContext *ctx, int is_second)
247 YADIFContext *yadif = ctx->priv;
248 AVFilterLink *link = ctx->outputs[0];
251 if (yadif->parity == -1) {
252 tff = yadif->cur->interlaced_frame ?
253 yadif->cur->top_field_first : 1;
255 tff = yadif->parity ^ 1;
259 yadif->out = ff_get_video_buffer(link, link->w, link->h);
261 return AVERROR(ENOMEM);
263 av_frame_copy_props(yadif->out, yadif->cur);
264 yadif->out->interlaced_frame = 0;
267 filter(ctx, yadif->out, tff ^ !is_second, tff);
270 int64_t cur_pts = yadif->cur->pts;
271 int64_t next_pts = yadif->next->pts;
273 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
274 yadif->out->pts = cur_pts + next_pts;
276 yadif->out->pts = AV_NOPTS_VALUE;
279 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
281 yadif->frame_pending = (yadif->mode&1) && !is_second;
285 static int filter_frame(AVFilterLink *link, AVFrame *frame)
287 AVFilterContext *ctx = link->dst;
288 YADIFContext *yadif = ctx->priv;
290 if (yadif->frame_pending)
291 return_frame(ctx, 1);
294 av_frame_free(&yadif->prev);
295 yadif->prev = yadif->cur;
296 yadif->cur = yadif->next;
302 if (yadif->auto_enable && !yadif->cur->interlaced_frame) {
303 yadif->out = av_frame_clone(yadif->cur);
305 return AVERROR(ENOMEM);
307 av_frame_free(&yadif->prev);
308 if (yadif->out->pts != AV_NOPTS_VALUE)
309 yadif->out->pts *= 2;
310 return ff_filter_frame(ctx->outputs[0], yadif->out);
314 !(yadif->prev = av_frame_clone(yadif->cur)))
315 return AVERROR(ENOMEM);
317 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
319 return AVERROR(ENOMEM);
321 av_frame_copy_props(yadif->out, yadif->cur);
322 yadif->out->interlaced_frame = 0;
324 if (yadif->out->pts != AV_NOPTS_VALUE)
325 yadif->out->pts *= 2;
327 return return_frame(ctx, 0);
330 static int request_frame(AVFilterLink *link)
332 AVFilterContext *ctx = link->src;
333 YADIFContext *yadif = ctx->priv;
335 if (yadif->frame_pending) {
336 return_frame(ctx, 1);
346 ret = ff_request_frame(link->src->inputs[0]);
348 if (ret == AVERROR_EOF && yadif->next) {
349 AVFrame *next = av_frame_clone(yadif->next);
352 return AVERROR(ENOMEM);
354 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
356 filter_frame(link->src->inputs[0], next);
358 } else if (ret < 0) {
361 } while (!yadif->cur);
366 static int poll_frame(AVFilterLink *link)
368 YADIFContext *yadif = link->src->priv;
371 if (yadif->frame_pending)
374 val = ff_poll_frame(link->src->inputs[0]);
378 //FIXME change API to not requre this red tape
379 if (val == 1 && !yadif->next) {
380 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
382 val = ff_poll_frame(link->src->inputs[0]);
386 assert(yadif->next || !val);
388 if (yadif->auto_enable && yadif->next && !yadif->next->interlaced_frame)
391 return val * ((yadif->mode&1)+1);
394 static av_cold void uninit(AVFilterContext *ctx)
396 YADIFContext *yadif = ctx->priv;
398 if (yadif->prev) av_frame_free(&yadif->prev);
399 if (yadif->cur ) av_frame_free(&yadif->cur );
400 if (yadif->next) av_frame_free(&yadif->next);
403 static int query_formats(AVFilterContext *ctx)
405 static const enum AVPixelFormat pix_fmts[] = {
415 AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
418 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
419 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
420 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
421 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
422 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
423 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
428 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
433 static int config_props(AVFilterLink *link)
435 YADIFContext *s = link->src->priv;
437 link->time_base.num = link->src->inputs[0]->time_base.num;
438 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
439 link->w = link->src->inputs[0]->w;
440 link->h = link->src->inputs[0]->h;
442 s->csp = av_pix_fmt_desc_get(link->format);
443 if (s->csp->comp[0].depth_minus1 / 8 == 1) {
444 s->filter_line = filter_line_c_16bit;
445 s->filter_edges = filter_edges_16bit;
447 s->filter_line = filter_line_c;
448 s->filter_edges = filter_edges;
451 ff_yadif_init_x86(s);
457 #define OFFSET(x) offsetof(YADIFContext, x)
458 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
459 static const AVOption options[] = {
460 { "mode", NULL, OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
461 { "parity", NULL, OFFSET(parity), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "parity" },
462 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, .unit = "parity" },
463 { "tff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "parity" },
464 { "bff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "parity" },
465 { "auto", NULL, OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
469 static const AVClass yadif_class = {
470 .class_name = "yadif",
471 .item_name = av_default_item_name,
473 .version = LIBAVUTIL_VERSION_INT,
476 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
479 .type = AVMEDIA_TYPE_VIDEO,
480 .get_video_buffer = get_video_buffer,
481 .filter_frame = filter_frame,
486 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
489 .type = AVMEDIA_TYPE_VIDEO,
490 .poll_frame = poll_frame,
491 .request_frame = request_frame,
492 .config_props = config_props,
497 AVFilter avfilter_vf_yadif = {
499 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
501 .priv_size = sizeof(YADIFContext),
502 .priv_class = &yadif_class,
504 .query_formats = query_formats,
506 .inputs = avfilter_vf_yadif_inputs,
508 .outputs = avfilter_vf_yadif_outputs,