2 * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "libavutil/cpu.h"
22 #include "libavutil/common.h"
31 * 0: send 1 frame for each frame
32 * 1: send 1 frame for each field
33 * 2: like 0 but skips spatial interlacing check
34 * 3: like 1 but skips spatial interlacing check
39 * 0: bottom field first
47 AVFilterBufferRef *cur;
48 AVFilterBufferRef *next;
49 AVFilterBufferRef *prev;
50 AVFilterBufferRef *out;
51 void (*filter_line)(uint8_t *dst,
52 uint8_t *prev, uint8_t *cur, uint8_t *next,
53 int w, int refs, int parity, int mode);
56 static void filter_line_c(uint8_t *dst,
57 uint8_t *prev, uint8_t *cur, uint8_t *next,
58 int w, int refs, int parity, int mode)
61 uint8_t *prev2 = parity ? prev : cur ;
62 uint8_t *next2 = parity ? cur : next;
63 for (x = 0; x < w; x++) {
65 int d = (prev2[0] + next2[0])>>1;
67 int temporal_diff0 = FFABS(prev2[0] - next2[0]);
68 int temporal_diff1 =(FFABS(prev[-refs] - c) + FFABS(prev[+refs] - e) )>>1;
69 int temporal_diff2 =(FFABS(next[-refs] - c) + FFABS(next[+refs] - e) )>>1;
70 int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
71 int spatial_pred = (c+e)>>1;
72 int spatial_score = FFABS(cur[-refs-1] - cur[+refs-1]) + FFABS(c-e)
73 + FFABS(cur[-refs+1] - cur[+refs+1]) - 1;
76 { int score = FFABS(cur[-refs-1+j] - cur[+refs-1-j])\
77 + FFABS(cur[-refs +j] - cur[+refs -j])\
78 + FFABS(cur[-refs+1+j] - cur[+refs+1-j]);\
79 if (score < spatial_score) {\
80 spatial_score= score;\
81 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
83 CHECK(-1) CHECK(-2) }} }}
84 CHECK( 1) CHECK( 2) }} }}
87 int b = (prev2[-2*refs] + next2[-2*refs])>>1;
88 int f = (prev2[+2*refs] + next2[+2*refs])>>1;
92 int max = FFMAX3(d-e, d-c, FFMIN3(FFMAX(b-c,f-e),FFMAX(b-c,b-a),FFMAX(f-g,f-e)) );
93 int min = FFMIN3(d-e, d-c, FFMAX3(FFMIN(b-c,f-e),FFMIN(b-c,b-a),FFMIN(f-g,f-e)) );
95 int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e));
96 int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e));
99 diff = FFMAX3(diff, min, -max);
102 if (spatial_pred > d + diff)
103 spatial_pred = d + diff;
104 else if (spatial_pred < d - diff)
105 spatial_pred = d - diff;
107 dst[0] = spatial_pred;
118 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
121 YADIFContext *yadif = ctx->priv;
124 for (i = 0; i < 3; i++) {
126 int w = dstpic->video->w >> is_chroma;
127 int h = dstpic->video->h >> is_chroma;
128 int refs = yadif->cur->linesize[i];
130 for (y = 0; y < h; y++) {
131 if ((y ^ parity) & 1) {
132 uint8_t *prev = &yadif->prev->data[i][y*refs];
133 uint8_t *cur = &yadif->cur ->data[i][y*refs];
134 uint8_t *next = &yadif->next->data[i][y*refs];
135 uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
136 yadif->filter_line(dst, prev, cur, next, w, refs, parity ^ tff, yadif->mode);
138 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
139 &yadif->cur->data[i][y*refs], w);
144 __asm__ volatile("emms \n\t" : : : "memory");
148 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
150 AVFilterBufferRef *picref;
151 int width = FFALIGN(w, 32);
152 int height= FFALIGN(h+6, 32);
155 picref = avfilter_default_get_video_buffer(link, perms, width, height);
157 picref->video->w = w;
158 picref->video->h = h;
160 for (i = 0; i < 3; i++)
161 picref->data[i] += 3 * picref->linesize[i];
166 static void return_frame(AVFilterContext *ctx, int is_second)
168 YADIFContext *yadif = ctx->priv;
169 AVFilterLink *link= ctx->outputs[0];
170 int tff = yadif->parity == -1 ? yadif->cur->video->top_field_first : (yadif->parity^1);
173 yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
174 AV_PERM_REUSE, link->w, link->h);
176 filter(ctx, yadif->out, tff ^ !is_second, tff);
179 if (yadif->next->pts != AV_NOPTS_VALUE &&
180 yadif->cur->pts != AV_NOPTS_VALUE) {
182 (yadif->next->pts&yadif->cur->pts) +
183 ((yadif->next->pts^yadif->cur->pts)>>1);
185 yadif->out->pts = AV_NOPTS_VALUE;
187 avfilter_start_frame(ctx->outputs[0], yadif->out);
189 avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
190 avfilter_end_frame(ctx->outputs[0]);
192 yadif->frame_pending = (yadif->mode&1) && !is_second;
195 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
197 AVFilterContext *ctx = link->dst;
198 YADIFContext *yadif = ctx->priv;
200 if (yadif->frame_pending)
201 return_frame(ctx, 1);
204 avfilter_unref_buffer(yadif->prev);
205 yadif->prev = yadif->cur;
206 yadif->cur = yadif->next;
207 yadif->next = picref;
213 yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
215 yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
216 AV_PERM_REUSE, link->w, link->h);
218 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
219 yadif->out->video->interlaced = 0;
220 avfilter_start_frame(ctx->outputs[0], yadif->out);
223 static void end_frame(AVFilterLink *link)
225 AVFilterContext *ctx = link->dst;
226 YADIFContext *yadif = ctx->priv;
231 return_frame(ctx, 0);
234 static int request_frame(AVFilterLink *link)
236 AVFilterContext *ctx = link->src;
237 YADIFContext *yadif = ctx->priv;
239 if (yadif->frame_pending) {
240 return_frame(ctx, 1);
247 if ((ret = avfilter_request_frame(link->src->inputs[0])))
249 } while (!yadif->cur);
254 static int poll_frame(AVFilterLink *link)
256 YADIFContext *yadif = link->src->priv;
259 if (yadif->frame_pending)
262 val = avfilter_poll_frame(link->src->inputs[0]);
264 if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
265 if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
267 val = avfilter_poll_frame(link->src->inputs[0]);
271 return val * ((yadif->mode&1)+1);
274 static av_cold void uninit(AVFilterContext *ctx)
276 YADIFContext *yadif = ctx->priv;
278 if (yadif->prev) avfilter_unref_buffer(yadif->prev);
279 if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
280 if (yadif->next) avfilter_unref_buffer(yadif->next);
283 static int query_formats(AVFilterContext *ctx)
285 static const enum PixelFormat pix_fmts[] = {
291 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
296 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
298 YADIFContext *yadif = ctx->priv;
299 av_unused int cpu_flags = av_get_cpu_flags();
304 if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
306 yadif->filter_line = filter_line_c;
307 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
308 yadif->filter_line = ff_yadif_filter_line_mmx;
310 av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
315 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
317 AVFilter avfilter_vf_yadif = {
319 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
321 .priv_size = sizeof(YADIFContext),
324 .query_formats = query_formats,
326 .inputs = (AVFilterPad[]) {{ .name = "default",
327 .type = AVMEDIA_TYPE_VIDEO,
328 .start_frame = start_frame,
329 .get_video_buffer = get_video_buffer,
330 .draw_slice = null_draw_slice,
331 .end_frame = end_frame, },
334 .outputs = (AVFilterPad[]) {{ .name = "default",
335 .type = AVMEDIA_TYPE_VIDEO,
336 .poll_frame = poll_frame,
337 .request_frame = request_frame, },