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
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 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"
35 typedef struct ThreadData {
44 { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
45 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
46 + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
47 if (score < spatial_score) {\
48 spatial_score= score;\
49 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
51 /* The is_not_edge argument here controls when the code will enter a branch
52 * which reads up to and including x-3 and x+3. */
54 #define FILTER(start, end, is_not_edge) \
55 for (x = start; x < end; x++) { \
57 int d = (prev2[0] + next2[0])>>1; \
59 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
60 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
61 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
62 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
63 int spatial_pred = (c+e) >> 1; \
66 int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
67 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
68 CHECK(-1) CHECK(-2) }} }} \
69 CHECK( 1) CHECK( 2) }} }} \
73 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
74 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
75 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
76 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
78 diff = FFMAX3(diff, min, -max); \
81 if (spatial_pred > d + diff) \
82 spatial_pred = d + diff; \
83 else if (spatial_pred < d - diff) \
84 spatial_pred = d - diff; \
86 dst[0] = spatial_pred; \
96 static void filter_line_c(void *dst1,
97 void *prev1, void *cur1, void *next1,
98 int w, int prefs, int mrefs, int parity, int mode)
101 uint8_t *prev = prev1;
103 uint8_t *next = next1;
105 uint8_t *prev2 = parity ? prev : cur ;
106 uint8_t *next2 = parity ? cur : next;
108 /* The function is called with the pointers already pointing to data[3] and
109 * with 6 subtracted from the width. This allows the FILTER macro to be
110 * called so that it processes all the pixels normally. A constant value of
111 * true for is_not_edge lets the compiler ignore the if statement. */
115 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
116 int w, int prefs, int mrefs, int parity, int mode)
119 uint8_t *prev = prev1;
121 uint8_t *next = next1;
123 uint8_t *prev2 = parity ? prev : cur ;
124 uint8_t *next2 = parity ? cur : next;
126 /* Only edge pixels need to be processed here. A constant value of false
127 * for is_not_edge should let the compiler ignore the whole branch. */
130 dst = (uint8_t*)dst1 + w - 3;
131 prev = (uint8_t*)prev1 + w - 3;
132 cur = (uint8_t*)cur1 + w - 3;
133 next = (uint8_t*)next1 + w - 3;
134 prev2 = (uint8_t*)(parity ? prev : cur);
135 next2 = (uint8_t*)(parity ? cur : next);
141 static void filter_line_c_16bit(void *dst1,
142 void *prev1, void *cur1, void *next1,
143 int w, int prefs, int mrefs, int parity,
146 uint16_t *dst = dst1;
147 uint16_t *prev = prev1;
148 uint16_t *cur = cur1;
149 uint16_t *next = next1;
151 uint16_t *prev2 = parity ? prev : cur ;
152 uint16_t *next2 = parity ? cur : next;
159 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
160 int w, int prefs, int mrefs, int parity, int mode)
162 uint16_t *dst = dst1;
163 uint16_t *prev = prev1;
164 uint16_t *cur = cur1;
165 uint16_t *next = next1;
167 uint16_t *prev2 = parity ? prev : cur ;
168 uint16_t *next2 = parity ? cur : next;
174 dst = (uint16_t*)dst1 + w - 3;
175 prev = (uint16_t*)prev1 + w - 3;
176 cur = (uint16_t*)cur1 + w - 3;
177 next = (uint16_t*)next1 + w - 3;
178 prev2 = (uint16_t*)(parity ? prev : cur);
179 next2 = (uint16_t*)(parity ? cur : next);
184 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
186 YADIFContext *s = ctx->priv;
187 ThreadData *td = arg;
188 int refs = s->cur->linesize[td->plane];
189 int df = (s->csp->comp[td->plane].depth_minus1 + 8) / 8;
191 int slice_h = td->h / nb_jobs;
192 int slice_start = jobnr * slice_h;
193 int slice_end = (jobnr == nb_jobs - 1) ? td->h : (jobnr + 1) * slice_h;
196 /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
197 * we need to call the c variant which avoids this for border pixels
199 for (y = slice_start; y < slice_end; y++) {
200 if ((y ^ td->parity) & 1) {
201 uint8_t *prev = &s->prev->data[td->plane][y * refs];
202 uint8_t *cur = &s->cur ->data[td->plane][y * refs];
203 uint8_t *next = &s->next->data[td->plane][y * refs];
204 uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
205 int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
206 s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
207 next + pix_3, td->w - 6,
208 y + 1 < td->h ? refs : -refs,
210 td->parity ^ td->tff, mode);
211 s->filter_edges(dst, prev, cur, next, td->w,
212 y + 1 < td->h ? refs : -refs,
214 td->parity ^ td->tff, mode);
216 memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
217 &s->cur->data[td->plane][y * refs], td->w * df);
223 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
226 YADIFContext *yadif = ctx->priv;
227 ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
230 for (i = 0; i < yadif->csp->nb_components; i++) {
231 int w = dstpic->width;
232 int h = dstpic->height;
234 if (i == 1 || i == 2) {
235 w >>= yadif->csp->log2_chroma_w;
236 h >>= yadif->csp->log2_chroma_h;
244 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
250 static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
253 int width = FFALIGN(w, 32);
254 int height = FFALIGN(h + 2, 32);
257 frame = ff_default_get_video_buffer(link, width, height);
262 for (i = 0; i < 3; i++)
263 frame->data[i] += frame->linesize[i];
268 static int return_frame(AVFilterContext *ctx, int is_second)
270 YADIFContext *yadif = ctx->priv;
271 AVFilterLink *link = ctx->outputs[0];
274 if (yadif->parity == -1) {
275 tff = yadif->cur->interlaced_frame ?
276 yadif->cur->top_field_first : 1;
278 tff = yadif->parity ^ 1;
282 yadif->out = ff_get_video_buffer(link, link->w, link->h);
284 return AVERROR(ENOMEM);
286 av_frame_copy_props(yadif->out, yadif->cur);
287 yadif->out->interlaced_frame = 0;
290 filter(ctx, yadif->out, tff ^ !is_second, tff);
293 int64_t cur_pts = yadif->cur->pts;
294 int64_t next_pts = yadif->next->pts;
296 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
297 yadif->out->pts = cur_pts + next_pts;
299 yadif->out->pts = AV_NOPTS_VALUE;
302 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
304 yadif->frame_pending = (yadif->mode&1) && !is_second;
308 static int filter_frame(AVFilterLink *link, AVFrame *frame)
310 AVFilterContext *ctx = link->dst;
311 YADIFContext *yadif = ctx->priv;
313 if (yadif->frame_pending)
314 return_frame(ctx, 1);
317 av_frame_free(&yadif->prev);
318 yadif->prev = yadif->cur;
319 yadif->cur = yadif->next;
325 if (yadif->auto_enable && !yadif->cur->interlaced_frame) {
326 yadif->out = av_frame_clone(yadif->cur);
328 return AVERROR(ENOMEM);
330 av_frame_free(&yadif->prev);
331 if (yadif->out->pts != AV_NOPTS_VALUE)
332 yadif->out->pts *= 2;
333 return ff_filter_frame(ctx->outputs[0], yadif->out);
337 !(yadif->prev = av_frame_clone(yadif->cur)))
338 return AVERROR(ENOMEM);
340 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
342 return AVERROR(ENOMEM);
344 av_frame_copy_props(yadif->out, yadif->cur);
345 yadif->out->interlaced_frame = 0;
347 if (yadif->out->pts != AV_NOPTS_VALUE)
348 yadif->out->pts *= 2;
350 return return_frame(ctx, 0);
353 static int request_frame(AVFilterLink *link)
355 AVFilterContext *ctx = link->src;
356 YADIFContext *yadif = ctx->priv;
358 if (yadif->frame_pending) {
359 return_frame(ctx, 1);
369 ret = ff_request_frame(link->src->inputs[0]);
371 if (ret == AVERROR_EOF && yadif->next) {
372 AVFrame *next = av_frame_clone(yadif->next);
375 return AVERROR(ENOMEM);
377 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
379 filter_frame(link->src->inputs[0], next);
381 } else if (ret < 0) {
384 } while (!yadif->cur);
389 static int poll_frame(AVFilterLink *link)
391 YADIFContext *yadif = link->src->priv;
394 if (yadif->frame_pending)
397 val = ff_poll_frame(link->src->inputs[0]);
401 //FIXME change API to not requre this red tape
402 if (val == 1 && !yadif->next) {
403 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
405 val = ff_poll_frame(link->src->inputs[0]);
409 assert(yadif->next || !val);
411 if (yadif->auto_enable && yadif->next && !yadif->next->interlaced_frame)
414 return val * ((yadif->mode&1)+1);
417 static av_cold void uninit(AVFilterContext *ctx)
419 YADIFContext *yadif = ctx->priv;
421 if (yadif->prev) av_frame_free(&yadif->prev);
422 if (yadif->cur ) av_frame_free(&yadif->cur );
423 if (yadif->next) av_frame_free(&yadif->next);
426 static int query_formats(AVFilterContext *ctx)
428 static const enum AVPixelFormat pix_fmts[] = {
438 AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
441 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
442 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
443 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
444 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
445 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
446 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
451 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
456 static int config_props(AVFilterLink *link)
458 YADIFContext *s = link->src->priv;
460 link->time_base.num = link->src->inputs[0]->time_base.num;
461 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
462 link->w = link->src->inputs[0]->w;
463 link->h = link->src->inputs[0]->h;
465 s->csp = av_pix_fmt_desc_get(link->format);
466 if (s->csp->comp[0].depth_minus1 / 8 == 1) {
467 s->filter_line = filter_line_c_16bit;
468 s->filter_edges = filter_edges_16bit;
470 s->filter_line = filter_line_c;
471 s->filter_edges = filter_edges;
474 ff_yadif_init_x86(s);
480 #define OFFSET(x) offsetof(YADIFContext, x)
481 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
482 static const AVOption options[] = {
483 { "mode", NULL, OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
484 { "parity", NULL, OFFSET(parity), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "parity" },
485 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, .unit = "parity" },
486 { "tff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "parity" },
487 { "bff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "parity" },
488 { "auto", NULL, OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
492 static const AVClass yadif_class = {
493 .class_name = "yadif",
494 .item_name = av_default_item_name,
496 .version = LIBAVUTIL_VERSION_INT,
499 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
502 .type = AVMEDIA_TYPE_VIDEO,
503 .get_video_buffer = get_video_buffer,
504 .filter_frame = filter_frame,
509 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
512 .type = AVMEDIA_TYPE_VIDEO,
513 .poll_frame = poll_frame,
514 .request_frame = request_frame,
515 .config_props = config_props,
520 AVFilter ff_vf_yadif = {
522 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
524 .priv_size = sizeof(YADIFContext),
525 .priv_class = &yadif_class,
527 .query_formats = query_formats,
529 .inputs = avfilter_vf_yadif_inputs,
531 .outputs = avfilter_vf_yadif_outputs,
533 .flags = AVFILTER_FLAG_SLICE_THREADS,