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/pixdesc.h"
34 { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
35 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
36 + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
37 if (score < spatial_score) {\
38 spatial_score= score;\
39 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
42 for (x = 0; x < w; x++) { \
44 int d = (prev2[0] + next2[0])>>1; \
46 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
47 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
48 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
49 int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
50 int spatial_pred = (c+e)>>1; \
51 int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
52 + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
54 CHECK(-1) CHECK(-2) }} }} \
55 CHECK( 1) CHECK( 2) }} }} \
58 int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
59 int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
60 int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
61 int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
63 diff = FFMAX3(diff, min, -max); \
66 if (spatial_pred > d + diff) \
67 spatial_pred = d + diff; \
68 else if (spatial_pred < d - diff) \
69 spatial_pred = d - diff; \
71 dst[0] = spatial_pred; \
81 static void filter_line_c(uint8_t *dst,
82 uint8_t *prev, uint8_t *cur, uint8_t *next,
83 int w, int prefs, int mrefs, int parity, int mode)
86 uint8_t *prev2 = parity ? prev : cur ;
87 uint8_t *next2 = parity ? cur : next;
92 static void filter_line_c_16bit(uint16_t *dst,
93 uint16_t *prev, uint16_t *cur, uint16_t *next,
94 int w, int prefs, int mrefs, int parity, int mode)
97 uint16_t *prev2 = parity ? prev : cur ;
98 uint16_t *next2 = parity ? cur : next;
105 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
108 YADIFContext *yadif = ctx->priv;
111 for (i = 0; i < yadif->csp->nb_components; i++) {
112 int w = dstpic->video->w;
113 int h = dstpic->video->h;
114 int refs = yadif->cur->linesize[i];
115 int absrefs = FFABS(refs);
116 int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
118 if (i == 1 || i == 2) {
119 /* Why is this not part of the per-plane description thing? */
120 w >>= yadif->csp->log2_chroma_w;
121 h >>= yadif->csp->log2_chroma_h;
124 if(yadif->temp_line_size < absrefs) {
125 av_free(yadif->temp_line);
126 yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
127 yadif->temp_line_size = absrefs;
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 int mode = y==1 || y+2==h ? 2 : yadif->mode;
137 int prefs = y+1<h ? refs : -refs;
138 int mrefs = y ?-refs : refs;
142 uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
144 memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
145 memcpy(tmp+mrefs, cur+mrefs, w*df);
146 memcpy(tmp , cur , w*df);
148 memcpy(tmp+prefs, cur+prefs, w*df);
150 memcpy(tmp+2*prefs, cur+2*prefs, w*df);
155 yadif->filter_line(dst, prev, cur, next, w, prefs, mrefs, parity ^ tff, mode);
157 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
158 &yadif->cur->data[i][y*refs], w*df);
166 static int return_frame(AVFilterContext *ctx, int is_second)
168 YADIFContext *yadif = ctx->priv;
169 AVFilterLink *link= ctx->outputs[0];
172 if (yadif->parity == -1) {
173 tff = yadif->cur->video->interlaced ?
174 yadif->cur->video->top_field_first : 1;
176 tff = yadif->parity^1;
180 yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
181 AV_PERM_REUSE, link->w, link->h);
183 return AVERROR(ENOMEM);
185 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
186 yadif->out->video->interlaced = 0;
190 yadif->csp = &av_pix_fmt_descriptors[link->format];
191 if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
192 yadif->filter_line = (void*)filter_line_c_16bit;
194 filter(ctx, yadif->out, tff ^ !is_second, tff);
197 int64_t cur_pts = yadif->cur->pts;
198 int64_t next_pts = yadif->next->pts;
200 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
201 yadif->out->pts = cur_pts + next_pts;
203 yadif->out->pts = AV_NOPTS_VALUE;
205 ret = ff_start_frame(ctx->outputs[0], yadif->out);
209 if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
210 (ret = ff_end_frame(ctx->outputs[0])) < 0)
213 yadif->frame_pending = (yadif->mode&1) && !is_second;
217 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
219 AVFilterContext *ctx = link->dst;
220 YADIFContext *yadif = ctx->priv;
224 if (picref->video->h < 3 || picref->video->w < 3) {
225 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
226 return AVERROR(EINVAL);
229 if (yadif->frame_pending)
230 return_frame(ctx, 1);
233 avfilter_unref_buffer(yadif->prev);
234 yadif->prev = yadif->cur;
235 yadif->cur = yadif->next;
236 yadif->next = picref;
237 link->cur_buf = NULL;
242 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
243 yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
245 return AVERROR(ENOMEM);
247 avfilter_unref_bufferp(&yadif->prev);
248 if (yadif->out->pts != AV_NOPTS_VALUE)
249 yadif->out->pts *= 2;
250 return ff_start_frame(ctx->outputs[0], yadif->out);
254 !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
255 return AVERROR(ENOMEM);
257 yadif->out = ff_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
258 AV_PERM_REUSE, link->w, link->h);
260 return AVERROR(ENOMEM);
262 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
263 yadif->out->video->interlaced = 0;
264 if (yadif->out->pts != AV_NOPTS_VALUE)
265 yadif->out->pts *= 2;
266 return ff_start_frame(ctx->outputs[0], yadif->out);
269 static int end_frame(AVFilterLink *link)
271 AVFilterContext *ctx = link->dst;
272 YADIFContext *yadif = ctx->priv;
277 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
278 int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
280 ret = ff_end_frame(ctx->outputs[0]);
284 return_frame(ctx, 0);
288 static int request_frame(AVFilterLink *link)
290 AVFilterContext *ctx = link->src;
291 YADIFContext *yadif = ctx->priv;
293 if (yadif->frame_pending) {
294 return_frame(ctx, 1);
304 ret = ff_request_frame(link->src->inputs[0]);
306 if (ret == AVERROR_EOF && yadif->cur) {
307 AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
309 return AVERROR(ENOMEM);
311 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
313 start_frame(link->src->inputs[0], next);
314 end_frame(link->src->inputs[0]);
316 } else if (ret < 0) {
319 } while (!yadif->cur);
324 static int poll_frame(AVFilterLink *link)
326 YADIFContext *yadif = link->src->priv;
329 if (yadif->frame_pending)
332 val = ff_poll_frame(link->src->inputs[0]);
336 if (val >= 1 && !yadif->next) { //FIXME change API to not requre this red tape
337 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
339 val = ff_poll_frame(link->src->inputs[0]);
343 assert(yadif->next || !val);
345 if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
348 return val * ((yadif->mode&1)+1);
351 static av_cold void uninit(AVFilterContext *ctx)
353 YADIFContext *yadif = ctx->priv;
355 if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
356 if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
357 if (yadif->next) avfilter_unref_bufferp(&yadif->next);
358 av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
361 static int query_formats(AVFilterContext *ctx)
363 static const enum PixelFormat pix_fmts[] = {
373 AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
376 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
377 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
378 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
379 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
380 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
381 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
388 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
393 static av_cold int init(AVFilterContext *ctx, const char *args)
395 YADIFContext *yadif = ctx->priv;
399 yadif->auto_enable = 0;
402 if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
404 yadif->filter_line = filter_line_c;
407 ff_yadif_init_x86(yadif);
409 av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
414 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
419 static int config_props(AVFilterLink *link)
421 YADIFContext *yadif = link->src->priv;
423 link->time_base.num = link->src->inputs[0]->time_base.num;
424 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
425 link->w = link->src->inputs[0]->w;
426 link->h = link->src->inputs[0]->h;
429 link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
434 AVFilter avfilter_vf_yadif = {
436 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
438 .priv_size = sizeof(YADIFContext),
441 .query_formats = query_formats,
443 .inputs = (const AVFilterPad[]) {{ .name = "default",
444 .type = AVMEDIA_TYPE_VIDEO,
445 .start_frame = start_frame,
446 .draw_slice = null_draw_slice,
447 .end_frame = end_frame,
448 .min_perms = AV_PERM_PRESERVE, },
451 .outputs = (const AVFilterPad[]) {{ .name = "default",
452 .type = AVMEDIA_TYPE_VIDEO,
453 .poll_frame = poll_frame,
454 .request_frame = request_frame,
455 .config_props = config_props, },