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"
33 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
36 { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
37 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
38 + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
39 if (score < spatial_score) {\
40 spatial_score= score;\
41 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
44 for (x = 0; x < w; 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 spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
54 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
56 CHECK(-1) CHECK(-2) }} }} \
57 CHECK( 1) CHECK( 2) }} }} \
60 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
61 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
62 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
63 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
65 diff = FFMAX3(diff, min, -max); \
68 if (spatial_pred > d + diff) \
69 spatial_pred = d + diff; \
70 else if (spatial_pred < d - diff) \
71 spatial_pred = d - diff; \
73 dst[0] = spatial_pred; \
83 static void filter_line_c(uint8_t *dst,
84 uint8_t *prev, uint8_t *cur, uint8_t *next,
85 int w, int prefs, int mrefs, int parity, int mode)
88 uint8_t *prev2 = parity ? prev : cur ;
89 uint8_t *next2 = parity ? cur : next;
94 static void filter_line_c_16bit(uint16_t *dst,
95 uint16_t *prev, uint16_t *cur, uint16_t *next,
96 int w, int prefs, int mrefs, int parity,
100 uint16_t *prev2 = parity ? prev : cur ;
101 uint16_t *next2 = parity ? cur : next;
108 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
111 YADIFContext *yadif = ctx->priv;
114 for (i = 0; i < yadif->csp->nb_components; i++) {
115 int w = dstpic->video->w;
116 int h = dstpic->video->h;
117 int refs = yadif->cur->linesize[i];
118 int absrefs = FFABS(refs);
119 int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
121 if (i == 1 || i == 2) {
122 /* Why is this not part of the per-plane description thing? */
123 w >>= yadif->csp->log2_chroma_w;
124 h >>= yadif->csp->log2_chroma_h;
127 if(yadif->temp_line_size < absrefs) {
128 av_free(yadif->temp_line);
129 yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
130 yadif->temp_line_size = absrefs;
133 for (y = 0; y < h; y++) {
134 if ((y ^ parity) & 1) {
135 uint8_t *prev = &yadif->prev->data[i][y * refs];
136 uint8_t *cur = &yadif->cur ->data[i][y * refs];
137 uint8_t *next = &yadif->next->data[i][y * refs];
138 uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
139 int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
140 int prefs = y+1<h ? refs : -refs;
141 int mrefs = y ?-refs : refs;
144 uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
146 memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
147 memcpy(tmp+mrefs, cur+mrefs, w*df);
148 memcpy(tmp , cur , w*df);
150 memcpy(tmp+prefs, cur+prefs, w*df);
152 memcpy(tmp+2*prefs, cur+2*prefs, w*df);
157 yadif->filter_line(dst, prev, cur, next, w,
161 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
162 &yadif->cur->data[i][y * refs], w * df);
170 static int return_frame(AVFilterContext *ctx, int is_second)
172 YADIFContext *yadif = ctx->priv;
173 AVFilterLink *link = ctx->outputs[0];
176 if (yadif->parity == -1) {
177 tff = yadif->cur->video->interlaced ?
178 yadif->cur->video->top_field_first : 1;
180 tff = yadif->parity ^ 1;
184 yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
186 return AVERROR(ENOMEM);
188 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
189 yadif->out->video->interlaced = 0;
193 yadif->csp = av_pix_fmt_desc_get(link->format);
194 if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
195 yadif->filter_line = (void*)filter_line_c_16bit;
197 filter(ctx, yadif->out, tff ^ !is_second, tff);
200 int64_t cur_pts = yadif->cur->pts;
201 int64_t next_pts = yadif->next->pts;
203 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
204 yadif->out->pts = cur_pts + next_pts;
206 yadif->out->pts = AV_NOPTS_VALUE;
209 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
211 yadif->frame_pending = (yadif->mode&1) && !is_second;
215 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
217 AVFilterContext *ctx = link->dst;
218 YADIFContext *yadif = ctx->priv;
222 if (picref->video->h < 3 || picref->video->w < 3) {
223 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
224 return AVERROR(EINVAL);
227 if (yadif->frame_pending)
228 return_frame(ctx, 1);
231 avfilter_unref_buffer(yadif->prev);
232 yadif->prev = yadif->cur;
233 yadif->cur = yadif->next;
234 yadif->next = picref;
239 if (yadif->auto_enable && !yadif->cur->video->interlaced) {
240 yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
242 return AVERROR(ENOMEM);
244 avfilter_unref_bufferp(&yadif->prev);
245 if (yadif->out->pts != AV_NOPTS_VALUE)
246 yadif->out->pts *= 2;
247 return ff_filter_frame(ctx->outputs[0], yadif->out);
251 !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
252 return AVERROR(ENOMEM);
254 yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
257 return AVERROR(ENOMEM);
259 avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
260 yadif->out->video->interlaced = 0;
262 if (yadif->out->pts != AV_NOPTS_VALUE)
263 yadif->out->pts *= 2;
265 return return_frame(ctx, 0);
268 static int request_frame(AVFilterLink *link)
270 AVFilterContext *ctx = link->src;
271 YADIFContext *yadif = ctx->priv;
273 if (yadif->frame_pending) {
274 return_frame(ctx, 1);
284 ret = ff_request_frame(link->src->inputs[0]);
286 if (ret == AVERROR_EOF && yadif->cur) {
287 AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
290 return AVERROR(ENOMEM);
292 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
294 filter_frame(link->src->inputs[0], next);
296 } else if (ret < 0) {
299 } while (!yadif->cur);
304 static int poll_frame(AVFilterLink *link)
306 YADIFContext *yadif = link->src->priv;
309 if (yadif->frame_pending)
312 val = ff_poll_frame(link->src->inputs[0]);
316 //FIXME change API to not requre this red tape
317 if (val >= 1 && !yadif->next) {
318 if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
320 val = ff_poll_frame(link->src->inputs[0]);
324 assert(yadif->next || !val);
326 if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
329 return val * ((yadif->mode&1)+1);
332 static av_cold void uninit(AVFilterContext *ctx)
334 YADIFContext *yadif = ctx->priv;
336 if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
337 if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
338 if (yadif->next) avfilter_unref_bufferp(&yadif->next);
339 av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
342 static int query_formats(AVFilterContext *ctx)
344 static const enum AVPixelFormat pix_fmts[] = {
354 AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
357 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
358 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
359 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
360 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
361 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
362 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
369 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
374 static av_cold int init(AVFilterContext *ctx, const char *args)
376 YADIFContext *yadif = ctx->priv;
380 yadif->auto_enable = 0;
384 sscanf(args, "%d:%d:%d",
385 &yadif->mode, &yadif->parity, &yadif->auto_enable);
387 yadif->filter_line = filter_line_c;
390 ff_yadif_init_x86(yadif);
392 av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
393 yadif->mode, yadif->parity, yadif->auto_enable);
398 static int config_props(AVFilterLink *link)
400 YADIFContext *yadif = link->src->priv;
402 link->time_base.num = link->src->inputs[0]->time_base.num;
403 link->time_base.den = link->src->inputs[0]->time_base.den * 2;
404 link->w = link->src->inputs[0]->w;
405 link->h = link->src->inputs[0]->h;
408 link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
413 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
416 .type = AVMEDIA_TYPE_VIDEO,
417 .filter_frame = filter_frame,
418 .min_perms = AV_PERM_PRESERVE,
423 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
426 .type = AVMEDIA_TYPE_VIDEO,
427 .poll_frame = poll_frame,
428 .request_frame = request_frame,
429 .config_props = config_props,
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 = avfilter_vf_yadif_inputs,
445 .outputs = avfilter_vf_yadif_outputs,