]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_yadif.c
yadif: correct strides in filter_edges_16bit
[ffmpeg] / libavfilter / vf_yadif.c
1 /*
2  * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
3  *               2010      James Darnley <james.darnley@gmail.com>
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "libavutil/cpu.h"
23 #include "libavutil/common.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 #include "yadif.h"
31
32 #undef NDEBUG
33 #include <assert.h>
34
35 #define CHECK(j)\
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;\
42
43 /* The is_not_edge argument here controls when the code will enter a branch
44  * which reads up to and including x-3 and x+3. */
45
46 #define FILTER(start, end, is_not_edge) \
47     for (x = start;  x < end; x++) { \
48         int c = cur[mrefs]; \
49         int d = (prev2[0] + next2[0])>>1; \
50         int e = cur[prefs]; \
51         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
52         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
53         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
54         int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
55         int spatial_pred = (c+e) >> 1; \
56  \
57         if (is_not_edge) {\
58             int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
59                               + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
60             CHECK(-1) CHECK(-2) }} }} \
61             CHECK( 1) CHECK( 2) }} }} \
62         }\
63  \
64         if (mode < 2) { \
65             int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
66             int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
67             int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
68             int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
69  \
70             diff = FFMAX3(diff, min, -max); \
71         } \
72  \
73         if (spatial_pred > d + diff) \
74            spatial_pred = d + diff; \
75         else if (spatial_pred < d - diff) \
76            spatial_pred = d - diff; \
77  \
78         dst[0] = spatial_pred; \
79  \
80         dst++; \
81         cur++; \
82         prev++; \
83         next++; \
84         prev2++; \
85         next2++; \
86     }
87
88 static void filter_line_c(void *dst1,
89                           void *prev1, void *cur1, void *next1,
90                           int w, int prefs, int mrefs, int parity, int mode)
91 {
92     uint8_t *dst  = dst1;
93     uint8_t *prev = prev1;
94     uint8_t *cur  = cur1;
95     uint8_t *next = next1;
96     int x;
97     uint8_t *prev2 = parity ? prev : cur ;
98     uint8_t *next2 = parity ? cur  : next;
99
100     /* The function is called with the pointers already pointing to data[3] and
101      * with 6 subtracted from the width.  This allows the FILTER macro to be
102      * called so that it processes all the pixels normally.  A constant value of
103      * true for is_not_edge lets the compiler ignore the if statement. */
104     FILTER(0, w, 1)
105 }
106
107 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
108                          int w, int prefs, int mrefs, int parity, int mode)
109 {
110     uint8_t *dst  = dst1;
111     uint8_t *prev = prev1;
112     uint8_t *cur  = cur1;
113     uint8_t *next = next1;
114     int x;
115     uint8_t *prev2 = parity ? prev : cur ;
116     uint8_t *next2 = parity ? cur  : next;
117
118     /* Only edge pixels need to be processed here.  A constant value of false
119      * for is_not_edge should let the compiler ignore the whole branch. */
120     FILTER(0, 3, 0)
121
122     dst  = (uint8_t*)dst1  + w - 3;
123     prev = (uint8_t*)prev1 + w - 3;
124     cur  = (uint8_t*)cur1  + w - 3;
125     next = (uint8_t*)next1 + w - 3;
126     prev2 = (uint8_t*)(parity ? prev : cur);
127     next2 = (uint8_t*)(parity ? cur  : next);
128
129     FILTER(w - 3, w, 0)
130 }
131
132
133 static void filter_line_c_16bit(void *dst1,
134                                 void *prev1, void *cur1, void *next1,
135                                 int w, int prefs, int mrefs, int parity,
136                                 int mode)
137 {
138     uint16_t *dst  = dst1;
139     uint16_t *prev = prev1;
140     uint16_t *cur  = cur1;
141     uint16_t *next = next1;
142     int x;
143     uint16_t *prev2 = parity ? prev : cur ;
144     uint16_t *next2 = parity ? cur  : next;
145     mrefs /= 2;
146     prefs /= 2;
147
148     FILTER(0, w, 1)
149 }
150
151 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
152                                int w, int prefs, int mrefs, int parity, int mode)
153 {
154     uint16_t *dst  = dst1;
155     uint16_t *prev = prev1;
156     uint16_t *cur  = cur1;
157     uint16_t *next = next1;
158     int x;
159     uint16_t *prev2 = parity ? prev : cur ;
160     uint16_t *next2 = parity ? cur  : next;
161     mrefs /= 2;
162     prefs /= 2;
163
164     FILTER(0, 3, 0)
165
166     dst   = (uint16_t*)dst1  + w - 3;
167     prev  = (uint16_t*)prev1 + w - 3;
168     cur   = (uint16_t*)cur1  + w - 3;
169     next  = (uint16_t*)next1 + w - 3;
170     prev2 = (uint16_t*)(parity ? prev : cur);
171     next2 = (uint16_t*)(parity ? cur  : next);
172
173     FILTER(w - 3, w, 0)
174 }
175
176 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
177                    int parity, int tff)
178 {
179     YADIFContext *yadif = ctx->priv;
180     int y, i;
181
182     for (i = 0; i < yadif->csp->nb_components; i++) {
183         int w = dstpic->width;
184         int h = dstpic->height;
185         int refs = yadif->cur->linesize[i];
186         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
187         int pix_3 = 3 * df;
188
189         if (i == 1 || i == 2) {
190         /* Why is this not part of the per-plane description thing? */
191             w >>= yadif->csp->log2_chroma_w;
192             h >>= yadif->csp->log2_chroma_h;
193         }
194
195         /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
196          * we need to call the c variant which avoids this for border pixels
197          */
198
199         for (y = 0; y < h; y++) {
200             if ((y ^ parity) & 1) {
201                 uint8_t *prev = &yadif->prev->data[i][y * refs];
202                 uint8_t *cur  = &yadif->cur ->data[i][y * refs];
203                 uint8_t *next = &yadif->next->data[i][y * refs];
204                 uint8_t *dst  = &dstpic->data[i][y * dstpic->linesize[i]];
205                 int     mode  = y == 1 || y + 2 == h ? 2 : yadif->mode;
206                 yadif->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
207                                    next + pix_3, w - 6,
208                                    y + 1 < h ? refs : -refs,
209                                    y ? -refs : refs,
210                                    parity ^ tff, mode);
211                 yadif->filter_edges(dst, prev, cur, next, w,
212                                     y + 1 < h ? refs : -refs,
213                                     y ? -refs : refs,
214                                     parity ^ tff, mode);
215             } else {
216                 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
217                        &yadif->cur->data[i][y * refs], w * df);
218             }
219         }
220     }
221
222     emms_c();
223 }
224
225 static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
226 {
227     AVFrame *frame;
228     int width  = FFALIGN(w, 32);
229     int height = FFALIGN(h + 2, 32);
230     int i;
231
232     frame = ff_default_get_video_buffer(link, width, height);
233
234     frame->width  = w;
235     frame->height = h;
236
237     for (i = 0; i < 3; i++)
238         frame->data[i] += frame->linesize[i];
239
240     return frame;
241 }
242
243 static int return_frame(AVFilterContext *ctx, int is_second)
244 {
245     YADIFContext *yadif = ctx->priv;
246     AVFilterLink *link  = ctx->outputs[0];
247     int tff, ret;
248
249     if (yadif->parity == -1) {
250         tff = yadif->cur->interlaced_frame ?
251               yadif->cur->top_field_first : 1;
252     } else {
253         tff = yadif->parity ^ 1;
254     }
255
256     if (is_second) {
257         yadif->out = ff_get_video_buffer(link, link->w, link->h);
258         if (!yadif->out)
259             return AVERROR(ENOMEM);
260
261         av_frame_copy_props(yadif->out, yadif->cur);
262         yadif->out->interlaced_frame = 0;
263     }
264
265     filter(ctx, yadif->out, tff ^ !is_second, tff);
266
267     if (is_second) {
268         int64_t cur_pts  = yadif->cur->pts;
269         int64_t next_pts = yadif->next->pts;
270
271         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
272             yadif->out->pts = cur_pts + next_pts;
273         } else {
274             yadif->out->pts = AV_NOPTS_VALUE;
275         }
276     }
277     ret = ff_filter_frame(ctx->outputs[0], yadif->out);
278
279     yadif->frame_pending = (yadif->mode&1) && !is_second;
280     return ret;
281 }
282
283 static int filter_frame(AVFilterLink *link, AVFrame *frame)
284 {
285     AVFilterContext *ctx = link->dst;
286     YADIFContext *yadif = ctx->priv;
287
288     if (yadif->frame_pending)
289         return_frame(ctx, 1);
290
291     if (yadif->prev)
292         av_frame_free(&yadif->prev);
293     yadif->prev = yadif->cur;
294     yadif->cur  = yadif->next;
295     yadif->next = frame;
296
297     if (!yadif->cur)
298         return 0;
299
300     if (yadif->auto_enable && !yadif->cur->interlaced_frame) {
301         yadif->out  = av_frame_clone(yadif->cur);
302         if (!yadif->out)
303             return AVERROR(ENOMEM);
304
305         av_frame_free(&yadif->prev);
306         if (yadif->out->pts != AV_NOPTS_VALUE)
307             yadif->out->pts *= 2;
308         return ff_filter_frame(ctx->outputs[0], yadif->out);
309     }
310
311     if (!yadif->prev &&
312         !(yadif->prev = av_frame_clone(yadif->cur)))
313         return AVERROR(ENOMEM);
314
315     yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
316     if (!yadif->out)
317         return AVERROR(ENOMEM);
318
319     av_frame_copy_props(yadif->out, yadif->cur);
320     yadif->out->interlaced_frame = 0;
321
322     if (yadif->out->pts != AV_NOPTS_VALUE)
323         yadif->out->pts *= 2;
324
325     return return_frame(ctx, 0);
326 }
327
328 static int request_frame(AVFilterLink *link)
329 {
330     AVFilterContext *ctx = link->src;
331     YADIFContext *yadif = ctx->priv;
332
333     if (yadif->frame_pending) {
334         return_frame(ctx, 1);
335         return 0;
336     }
337
338     do {
339         int ret;
340
341         if (yadif->eof)
342             return AVERROR_EOF;
343
344         ret  = ff_request_frame(link->src->inputs[0]);
345
346         if (ret == AVERROR_EOF && yadif->next) {
347             AVFrame *next = av_frame_clone(yadif->next);
348
349             if (!next)
350                 return AVERROR(ENOMEM);
351
352             next->pts = yadif->next->pts * 2 - yadif->cur->pts;
353
354             filter_frame(link->src->inputs[0], next);
355             yadif->eof = 1;
356         } else if (ret < 0) {
357             return ret;
358         }
359     } while (!yadif->cur);
360
361     return 0;
362 }
363
364 static int poll_frame(AVFilterLink *link)
365 {
366     YADIFContext *yadif = link->src->priv;
367     int ret, val;
368
369     if (yadif->frame_pending)
370         return 1;
371
372     val = ff_poll_frame(link->src->inputs[0]);
373     if (val <= 0)
374         return val;
375
376     //FIXME change API to not requre this red tape
377     if (val == 1 && !yadif->next) {
378         if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
379             return ret;
380         val = ff_poll_frame(link->src->inputs[0]);
381         if (val <= 0)
382             return val;
383     }
384     assert(yadif->next || !val);
385
386     if (yadif->auto_enable && yadif->next && !yadif->next->interlaced_frame)
387         return val;
388
389     return val * ((yadif->mode&1)+1);
390 }
391
392 static av_cold void uninit(AVFilterContext *ctx)
393 {
394     YADIFContext *yadif = ctx->priv;
395
396     if (yadif->prev) av_frame_free(&yadif->prev);
397     if (yadif->cur ) av_frame_free(&yadif->cur );
398     if (yadif->next) av_frame_free(&yadif->next);
399 }
400
401 static int query_formats(AVFilterContext *ctx)
402 {
403     static const enum AVPixelFormat pix_fmts[] = {
404         AV_PIX_FMT_YUV420P,
405         AV_PIX_FMT_YUV422P,
406         AV_PIX_FMT_YUV444P,
407         AV_PIX_FMT_YUV410P,
408         AV_PIX_FMT_YUV411P,
409         AV_PIX_FMT_GRAY8,
410         AV_PIX_FMT_YUVJ420P,
411         AV_PIX_FMT_YUVJ422P,
412         AV_PIX_FMT_YUVJ444P,
413         AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
414         AV_PIX_FMT_YUV440P,
415         AV_PIX_FMT_YUVJ440P,
416         AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
417         AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
418         AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
419         AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
420         AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
421         AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
422         AV_PIX_FMT_YUVA420P,
423         AV_PIX_FMT_NONE
424     };
425
426     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
427
428     return 0;
429 }
430
431 static int config_props(AVFilterLink *link)
432 {
433     YADIFContext *s = link->src->priv;
434
435     link->time_base.num = link->src->inputs[0]->time_base.num;
436     link->time_base.den = link->src->inputs[0]->time_base.den * 2;
437     link->w             = link->src->inputs[0]->w;
438     link->h             = link->src->inputs[0]->h;
439
440     s->csp = av_pix_fmt_desc_get(link->format);
441     if (s->csp->comp[0].depth_minus1 / 8 == 1) {
442         s->filter_line  = filter_line_c_16bit;
443         s->filter_edges = filter_edges_16bit;
444     } else {
445         s->filter_line  = filter_line_c;
446         s->filter_edges = filter_edges;
447
448         if (ARCH_X86)
449             ff_yadif_init_x86(s);
450     }
451
452     return 0;
453 }
454
455 #define OFFSET(x) offsetof(YADIFContext, x)
456 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
457 static const AVOption options[] = {
458     { "mode",   NULL, OFFSET(mode),        AV_OPT_TYPE_INT, { .i64 = 0  },  0, 3, FLAGS },
459     { "parity", NULL, OFFSET(parity),      AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "parity" },
460         { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, .unit = "parity" },
461         { "tff",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0  }, .unit = "parity" },
462         { "bff",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1  }, .unit = "parity" },
463     { "auto",   NULL, OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0  },  0, 1, FLAGS },
464     { NULL },
465 };
466
467 static const AVClass yadif_class = {
468     .class_name = "yadif",
469     .item_name  = av_default_item_name,
470     .option     = options,
471     .version    = LIBAVUTIL_VERSION_INT,
472 };
473
474 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
475     {
476         .name             = "default",
477         .type             = AVMEDIA_TYPE_VIDEO,
478         .get_video_buffer = get_video_buffer,
479         .filter_frame     = filter_frame,
480     },
481     { NULL }
482 };
483
484 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
485     {
486         .name          = "default",
487         .type          = AVMEDIA_TYPE_VIDEO,
488         .poll_frame    = poll_frame,
489         .request_frame = request_frame,
490         .config_props  = config_props,
491     },
492     { NULL }
493 };
494
495 AVFilter avfilter_vf_yadif = {
496     .name          = "yadif",
497     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
498
499     .priv_size     = sizeof(YADIFContext),
500     .priv_class    = &yadif_class,
501     .uninit        = uninit,
502     .query_formats = query_formats,
503
504     .inputs    = avfilter_vf_yadif_inputs,
505
506     .outputs   = avfilter_vf_yadif_outputs,
507 };