2 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3 * 2010 James Darnley <james.darnley@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/common.h"
23 #include "libavutil/pixdesc.h"
28 typedef struct ThreadData {
37 { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
38 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
39 + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
40 if (score < spatial_score) {\
41 spatial_score= score;\
42 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
44 /* The is_not_edge argument here controls when the code will enter a branch
45 * which reads up to and including x-3 and x+3. */
47 #define FILTER(start, end, is_not_edge) \
48 for (x = start; x < end; x++) { \
50 int d = (prev2[0] + next2[0])>>1; \
52 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
53 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
54 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
55 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
56 int spatial_pred = (c+e) >> 1; \
59 int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
60 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
61 CHECK(-1) CHECK(-2) }} }} \
62 CHECK( 1) CHECK( 2) }} }} \
66 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
67 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
68 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
69 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
71 diff = FFMAX3(diff, min, -max); \
74 if (spatial_pred > d + diff) \
75 spatial_pred = d + diff; \
76 else if (spatial_pred < d - diff) \
77 spatial_pred = d - diff; \
79 dst[0] = spatial_pred; \
89 static void filter_line_c(void *dst1,
90 void *prev1, void *cur1, void *next1,
91 int w, int prefs, int mrefs, int parity, int mode)
94 uint8_t *prev = prev1;
96 uint8_t *next = next1;
98 uint8_t *prev2 = parity ? prev : cur ;
99 uint8_t *next2 = parity ? cur : next;
101 /* The function is called with the pointers already pointing to data[3] and
102 * with 6 subtracted from the width. This allows the FILTER macro to be
103 * called so that it processes all the pixels normally. A constant value of
104 * true for is_not_edge lets the compiler ignore the if statement. */
109 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
110 int w, int prefs, int mrefs, int parity, int mode)
113 uint8_t *prev = prev1;
115 uint8_t *next = next1;
117 uint8_t *prev2 = parity ? prev : cur ;
118 uint8_t *next2 = parity ? cur : next;
120 const int edge = MAX_ALIGN - 1;
121 int offset = FFMAX(w - edge, 3);
123 /* Only edge pixels need to be processed here. A constant value of false
124 * for is_not_edge should let the compiler ignore the whole branch. */
125 FILTER(0, FFMIN(3, w), 0)
127 dst = (uint8_t*)dst1 + offset;
128 prev = (uint8_t*)prev1 + offset;
129 cur = (uint8_t*)cur1 + offset;
130 next = (uint8_t*)next1 + offset;
131 prev2 = (uint8_t*)(parity ? prev : cur);
132 next2 = (uint8_t*)(parity ? cur : next);
134 FILTER(offset, w - 3, 1)
135 offset = FFMAX(offset, w - 3);
140 static void filter_line_c_16bit(void *dst1,
141 void *prev1, void *cur1, void *next1,
142 int w, int prefs, int mrefs, int parity,
145 uint16_t *dst = dst1;
146 uint16_t *prev = prev1;
147 uint16_t *cur = cur1;
148 uint16_t *next = next1;
150 uint16_t *prev2 = parity ? prev : cur ;
151 uint16_t *next2 = parity ? cur : next;
158 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
159 int w, int prefs, int mrefs, int parity, int mode)
161 uint16_t *dst = dst1;
162 uint16_t *prev = prev1;
163 uint16_t *cur = cur1;
164 uint16_t *next = next1;
166 uint16_t *prev2 = parity ? prev : cur ;
167 uint16_t *next2 = parity ? cur : next;
169 const int edge = MAX_ALIGN / 2 - 1;
170 int offset = FFMAX(w - edge, 3);
175 FILTER(0, FFMIN(3, w), 0)
177 dst = (uint16_t*)dst1 + offset;
178 prev = (uint16_t*)prev1 + offset;
179 cur = (uint16_t*)cur1 + offset;
180 next = (uint16_t*)next1 + offset;
181 prev2 = (uint16_t*)(parity ? prev : cur);
182 next2 = (uint16_t*)(parity ? cur : next);
184 FILTER(offset, w - 3, 1)
185 offset = FFMAX(offset, w - 3);
189 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
191 YADIFContext *s = ctx->priv;
192 ThreadData *td = arg;
193 int refs = s->cur->linesize[td->plane];
194 int df = (s->csp->comp[td->plane].depth + 7) / 8;
196 int slice_start = (td->h * jobnr ) / nb_jobs;
197 int slice_end = (td->h * (jobnr+1)) / nb_jobs;
199 int edge = 3 + MAX_ALIGN / df - 1;
201 /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
202 * we need to call the c variant which avoids this for border pixels
204 for (y = slice_start; y < slice_end; y++) {
205 if ((y ^ td->parity) & 1) {
206 uint8_t *prev = &s->prev->data[td->plane][y * refs];
207 uint8_t *cur = &s->cur ->data[td->plane][y * refs];
208 uint8_t *next = &s->next->data[td->plane][y * refs];
209 uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
210 int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
211 s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
212 next + pix_3, td->w - edge,
213 y + 1 < td->h ? refs : -refs,
215 td->parity ^ td->tff, mode);
216 s->filter_edges(dst, prev, cur, next, td->w,
217 y + 1 < td->h ? refs : -refs,
219 td->parity ^ td->tff, mode);
221 memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
222 &s->cur->data[td->plane][y * refs], td->w * df);
228 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
231 YADIFContext *yadif = ctx->priv;
232 ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
235 for (i = 0; i < yadif->csp->nb_components; i++) {
236 int w = dstpic->width;
237 int h = dstpic->height;
239 if (i == 1 || i == 2) {
240 w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
241 h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
249 ff_filter_execute(ctx, filter_slice, &td, NULL,
250 FFMIN(h, ff_filter_get_nb_threads(ctx)));
254 static av_cold void uninit(AVFilterContext *ctx)
256 YADIFContext *yadif = ctx->priv;
258 av_frame_free(&yadif->prev);
259 av_frame_free(&yadif->cur );
260 av_frame_free(&yadif->next);
261 ff_ccfifo_uninit(&yadif->cc_fifo);
264 static const enum AVPixelFormat pix_fmts[] = {
265 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
266 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P,
267 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
268 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
270 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
271 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
272 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
273 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
274 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
275 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
276 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
277 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
282 static int config_output(AVFilterLink *outlink)
284 AVFilterContext *ctx = outlink->src;
285 YADIFContext *s = ctx->priv;
288 outlink->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
289 outlink->w = ctx->inputs[0]->w;
290 outlink->h = ctx->inputs[0]->h;
293 outlink->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
296 outlink->frame_rate = ctx->inputs[0]->frame_rate;
298 ret = ff_ccfifo_init(&s->cc_fifo, outlink->frame_rate, ctx);
300 av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
304 if (outlink->w < 3 || outlink->h < 3) {
305 av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
306 return AVERROR(EINVAL);
309 s->csp = av_pix_fmt_desc_get(outlink->format);
311 if (s->csp->comp[0].depth > 8) {
312 s->filter_line = filter_line_c_16bit;
313 s->filter_edges = filter_edges_16bit;
315 s->filter_line = filter_line_c;
316 s->filter_edges = filter_edges;
320 ff_yadif_init_x86(s);
327 static const AVClass yadif_class = {
328 .class_name = "yadif",
329 .item_name = av_default_item_name,
330 .option = ff_yadif_options,
331 .version = LIBAVUTIL_VERSION_INT,
332 .category = AV_CLASS_CATEGORY_FILTER,
335 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
338 .type = AVMEDIA_TYPE_VIDEO,
339 .filter_frame = ff_yadif_filter_frame,
343 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
346 .type = AVMEDIA_TYPE_VIDEO,
347 .request_frame = ff_yadif_request_frame,
348 .config_props = config_output,
352 const AVFilter ff_vf_yadif = {
354 .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
355 .priv_size = sizeof(YADIFContext),
356 .priv_class = &yadif_class,
358 FILTER_INPUTS(avfilter_vf_yadif_inputs),
359 FILTER_OUTPUTS(avfilter_vf_yadif_outputs),
360 FILTER_PIXFMTS_ARRAY(pix_fmts),
361 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,