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/pixdesc.h"
33 * 0: send 1 frame for each frame
34 * 1: send 1 frame for each field
35 * 2: like 0 but skips spatial interlacing check
36 * 3: like 1 but skips spatial interlacing check
41 * 0: bottom field first
50 * 0: deinterlace all frames
51 * 1: only deinterlace frames marked as interlaced
55 AVFilterBufferRef *cur;
56 AVFilterBufferRef *next;
57 AVFilterBufferRef *prev;
58 AVFilterBufferRef *out;
59 void (*filter_line)(uint8_t *dst,
60 uint8_t *prev, uint8_t *cur, uint8_t *next,
61 int w, int prefs, int mrefs, int parity, int mode);
63 const AVPixFmtDescriptor *csp;
67 { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
68 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
69 + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
70 if (score < spatial_score) {\
71 spatial_score= score;\
72 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
75 for (x = 0; x < w; x++) { \
77 int d = (prev2[0] + next2[0])>>1; \
79 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
80 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
81 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
82 int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
83 int spatial_pred = (c+e)>>1; \
84 int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
85 + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
87 CHECK(-1) CHECK(-2) }} }} \
88 CHECK( 1) CHECK( 2) }} }} \
91 int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
92 int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
93 int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
94 int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
96 diff = FFMAX3(diff, min, -max); \
99 if (spatial_pred > d + diff) \
100 spatial_pred = d + diff; \
101 else if (spatial_pred < d - diff) \
102 spatial_pred = d - diff; \
104 dst[0] = spatial_pred; \
114 static void filter_line_c(uint8_t *dst,
115 uint8_t *prev, uint8_t *cur, uint8_t *next,
116 int w, int prefs, int mrefs, int parity, int mode)
119 uint8_t *prev2 = parity ? prev : cur ;
120 uint8_t *next2 = parity ? cur : next;
125 static void filter_line_c_16bit(uint16_t *dst,
126 uint16_t *prev, uint16_t *cur, uint16_t *next,
127 int w, int prefs, int mrefs, int parity, int mode)
130 uint16_t *prev2 = parity ? prev : cur ;
131 uint16_t *next2 = parity ? cur : next;
138 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
141 YADIFContext *yadif = ctx->priv;
144 for (i = 0; i < yadif->csp->nb_components; i++) {
145 int w = dstpic->video->w;
146 int h = dstpic->video->h;
147 int refs = yadif->cur->linesize[i];
148 int df = (yadif->csp->comp[i].depth_minus1+1) / 8;
151 /* Why is this not part of the per-plane description thing? */
152 w >>= yadif->csp->log2_chroma_w;
153 h >>= yadif->csp->log2_chroma_h;
156 for (y = 0; y < h; y++) {
157 if ((y ^ parity) & 1) {
158 uint8_t *prev = &yadif->prev->data[i][y*refs];
159 uint8_t *cur = &yadif->cur ->data[i][y*refs];
160 uint8_t *next = &yadif->next->data[i][y*refs];
161 uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
162 int mode = y==1 || y+2==h ? 2 : yadif->mode;
163 yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
165 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
166 &yadif->cur->data[i][y*refs], w*df);
171 __asm__ volatile("emms \n\t" : : : "memory");
175 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
177 AVFilterBufferRef *picref;
178 int width = FFALIGN(w, 32);
179 int height= FFALIGN(h+2, 32);
182 picref = avfilter_default_get_video_buffer(link, perms, width, height);
184 picref->video->w = w;
185 picref->video->h = h;
187 for (i = 0; i < 3; i++)
188 picref->data[i] += picref->linesize[i];
193 static void return_frame(AVFilterContext *ctx, int is_second)
195 YADIFContext *yadif = ctx->priv;
196 AVFilterLink *link= ctx->outputs[0];
199 if (yadif->parity == -1) {
200 tff = yadif->cur->video->interlaced ?
201 yadif->cur->video->top_field_first : 1;
203 tff = yadif->parity^1;
207 yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
208 AV_PERM_REUSE, link->w, link->h);
211 yadif->csp = &av_pix_fmt_descriptors[link->format];
212 if (yadif->csp->comp[0].depth_minus1 == 15)
213 yadif->filter_line = filter_line_c_16bit;
215 filter(ctx, yadif->out, tff ^ !is_second, tff);
218 if (yadif->next->pts != AV_NOPTS_VALUE &&
219 yadif->cur->pts != AV_NOPTS_VALUE) {
221 (yadif->next->pts&yadif->cur->pts) +
222 ((yadif->next->pts^yadif->cur->pts)>>1);
224 yadif->out->pts = AV_NOPTS_VALUE;
226 avfilter_start_frame(ctx->outputs[0], yadif->out);
228 avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
229 avfilter_end_frame(ctx->outputs[0]);
231 yadif->frame_pending = (yadif->mode&1) && !is_second;
234 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
236 AVFilterContext *ctx = link->dst;
237 YADIFContext *yadif = ctx->priv;
239 if (yadif->frame_pending)
240 return_frame(ctx, 1);
243 avfilter_unref_buffer(yadif->prev);
244 yadif->prev = yadif->cur;
245 yadif->cur = yadif->next;
246 yadif->next = picref;
251 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
252 yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
253 avfilter_unref_buffer(yadif->prev);
255 avfilter_start_frame(ctx->outputs[0], yadif->out);
260 yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
262 yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
263 AV_PERM_REUSE, link->w, link->h);
265 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
266 yadif->out->video->interlaced = 0;
267 avfilter_start_frame(ctx->outputs[0], yadif->out);
270 static void end_frame(AVFilterLink *link)
272 AVFilterContext *ctx = link->dst;
273 YADIFContext *yadif = ctx->priv;
278 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
279 avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
280 avfilter_end_frame(ctx->outputs[0]);
284 return_frame(ctx, 0);
287 static int request_frame(AVFilterLink *link)
289 AVFilterContext *ctx = link->src;
290 YADIFContext *yadif = ctx->priv;
292 if (yadif->frame_pending) {
293 return_frame(ctx, 1);
300 if ((ret = avfilter_request_frame(link->src->inputs[0])))
302 } while (!yadif->cur);
307 static int poll_frame(AVFilterLink *link)
309 YADIFContext *yadif = link->src->priv;
312 if (yadif->frame_pending)
315 val = avfilter_poll_frame(link->src->inputs[0]);
317 if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
318 if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
320 val = avfilter_poll_frame(link->src->inputs[0]);
322 assert(yadif->next || !val);
324 if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
327 return val * ((yadif->mode&1)+1);
330 static av_cold void uninit(AVFilterContext *ctx)
332 YADIFContext *yadif = ctx->priv;
334 if (yadif->prev) avfilter_unref_buffer(yadif->prev);
335 if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
336 if (yadif->next) avfilter_unref_buffer(yadif->next);
339 static int query_formats(AVFilterContext *ctx)
341 static const enum PixelFormat pix_fmts[] = {
351 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
354 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
355 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
356 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
360 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
365 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
367 YADIFContext *yadif = ctx->priv;
368 av_unused int cpu_flags = av_get_cpu_flags();
372 yadif->auto_enable = 0;
375 if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
377 yadif->filter_line = filter_line_c;
378 if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
379 yadif->filter_line = ff_yadif_filter_line_ssse3;
380 else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
381 yadif->filter_line = ff_yadif_filter_line_sse2;
382 else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
383 yadif->filter_line = ff_yadif_filter_line_mmx;
385 av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
390 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
392 AVFilter avfilter_vf_yadif = {
394 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
396 .priv_size = sizeof(YADIFContext),
399 .query_formats = query_formats,
401 .inputs = (AVFilterPad[]) {{ .name = "default",
402 .type = AVMEDIA_TYPE_VIDEO,
403 .start_frame = start_frame,
404 .get_video_buffer = get_video_buffer,
405 .draw_slice = null_draw_slice,
406 .end_frame = end_frame, },
409 .outputs = (AVFilterPad[]) {{ .name = "default",
410 .type = AVMEDIA_TYPE_VIDEO,
411 .poll_frame = poll_frame,
412 .request_frame = request_frame, },