]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_yadif.c
vf_yadif: fix out-of line reads
[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/pixdesc.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 #include "yadif.h"
30
31 #undef NDEBUG
32 #include <assert.h>
33
34 #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
35
36 #define CHECK(j)\
37     {   int score = FFABS(cur[mrefs + off_left + (j)] - cur[prefs + off_left - (j)])\
38                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
39                   + FFABS(cur[mrefs + off_right + (j)] - cur[prefs + off_right - (j)]);\
40         if (score < spatial_score) {\
41             spatial_score= score;\
42             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
43
44 #define FILTER(start, end) \
45     for (x = start;  x < end; x++) { \
46         int c = cur[mrefs]; \
47         int d = (prev2[0] + next2[0])>>1; \
48         int e = cur[prefs]; \
49         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
50         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
51         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
52         int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
53         int spatial_pred = (c+e) >> 1; \
54         int off_right = (x < w - 1) ? 1 : -1;\
55         int off_left  = x ? -1 : 1;\
56         int spatial_score = FFABS(cur[mrefs + off_left]  - cur[prefs + off_left]) + FFABS(c-e) \
57                           + FFABS(cur[mrefs + off_right] - cur[prefs + off_right]) - 1; \
58  \
59         if (x > 2 && x < w - 3) {\
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     FILTER(0, w)
101 }
102
103 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
104                          int w, int prefs, int mrefs, int parity, int mode,
105                          int l_edge)
106 {
107     uint8_t *dst  = dst1;
108     uint8_t *prev = prev1;
109     uint8_t *cur  = cur1;
110     uint8_t *next = next1;
111     int x;
112     uint8_t *prev2 = parity ? prev : cur ;
113     uint8_t *next2 = parity ? cur  : next;
114
115     FILTER(0, l_edge)
116
117     dst  = (uint8_t*)dst1  + w - 3;
118     prev = (uint8_t*)prev1 + w - 3;
119     cur  = (uint8_t*)cur1  + w - 3;
120     next = (uint8_t*)next1 + w - 3;
121     prev2 = (uint8_t*)(parity ? prev : cur);
122     next2 = (uint8_t*)(parity ? cur  : next);
123
124     FILTER(w - 3, w)
125 }
126
127
128 static void filter_line_c_16bit(void *dst1,
129                                 void *prev1, void *cur1, void *next1,
130                                 int w, int prefs, int mrefs, int parity,
131                                 int mode)
132 {
133     uint16_t *dst  = dst1;
134     uint16_t *prev = prev1;
135     uint16_t *cur  = cur1;
136     uint16_t *next = next1;
137     int x;
138     uint16_t *prev2 = parity ? prev : cur ;
139     uint16_t *next2 = parity ? cur  : next;
140     mrefs /= 2;
141     prefs /= 2;
142
143     FILTER(0, w)
144 }
145
146 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
147                                int w, int prefs, int mrefs, int parity, int mode,
148                                int l_edge)
149 {
150     uint16_t *dst  = dst1;
151     uint16_t *prev = prev1;
152     uint16_t *cur  = cur1;
153     uint16_t *next = next1;
154     int x;
155     uint16_t *prev2 = parity ? prev : cur ;
156     uint16_t *next2 = parity ? cur  : next;
157
158     FILTER(0, l_edge)
159
160     dst   = (uint16_t*)dst1  + w - 3;
161     prev  = (uint16_t*)prev1 + w - 3;
162     cur   = (uint16_t*)cur1  + w - 3;
163     next  = (uint16_t*)next1 + w - 3;
164     prev2 = (uint16_t*)(parity ? prev : cur);
165     next2 = (uint16_t*)(parity ? cur  : next);
166
167     FILTER(w - 3, w)
168 }
169
170 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
171                    int parity, int tff)
172 {
173     YADIFContext *yadif = ctx->priv;
174     int y, i;
175
176     for (i = 0; i < yadif->csp->nb_components; i++) {
177         int w = dstpic->video->w;
178         int h = dstpic->video->h;
179         int refs = yadif->cur->linesize[i];
180         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
181         int l_edge, l_edge_pix;
182
183         if (i == 1 || i == 2) {
184         /* Why is this not part of the per-plane description thing? */
185             w >>= yadif->csp->log2_chroma_w;
186             h >>= yadif->csp->log2_chroma_h;
187         }
188
189         /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
190          * we need to call the c variant which avoids this for border pixels
191          */
192         l_edge     = yadif->req_align;
193         l_edge_pix = l_edge / df;
194
195         for (y = 0; y < h; y++) {
196             if ((y ^ parity) & 1) {
197                 uint8_t *prev = &yadif->prev->data[i][y * refs];
198                 uint8_t *cur  = &yadif->cur ->data[i][y * refs];
199                 uint8_t *next = &yadif->next->data[i][y * refs];
200                 uint8_t *dst  = &dstpic->data[i][y * dstpic->linesize[i]];
201                 int     mode  = y == 1 || y + 2 == h ? 2 : yadif->mode;
202                 if (yadif->req_align) {
203                     yadif->filter_line(dst + l_edge, prev + l_edge, cur + l_edge,
204                                        next + l_edge, w - l_edge_pix - 3,
205                                        y + 1 < h ? refs : -refs,
206                                        y ? -refs : refs,
207                                        parity ^ tff, mode);
208                     yadif->filter_edges(dst, prev, cur, next, w,
209                                          y + 1 < h ? refs : -refs,
210                                          y ? -refs : refs,
211                                          parity ^ tff, mode, l_edge_pix);
212                 } else {
213                     yadif->filter_line(dst, prev, cur, next + l_edge, w,
214                                        y + 1 < h ? refs : -refs,
215                                        y ? -refs : refs,
216                                        parity ^ tff, mode);
217                 }
218             } else {
219                 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
220                        &yadif->cur->data[i][y * refs], w * df);
221             }
222         }
223     }
224
225     emms_c();
226 }
227
228 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
229                                            int w, int h)
230 {
231     AVFilterBufferRef *picref;
232     int width  = FFALIGN(w, 32);
233     int height = FFALIGN(h + 2, 32);
234     int i;
235
236     picref = ff_default_get_video_buffer(link, perms, width, height);
237
238     picref->video->w = w;
239     picref->video->h = h;
240
241     for (i = 0; i < 3; i++)
242         picref->data[i] += picref->linesize[i];
243
244     return picref;
245 }
246
247 static int return_frame(AVFilterContext *ctx, int is_second)
248 {
249     YADIFContext *yadif = ctx->priv;
250     AVFilterLink *link  = ctx->outputs[0];
251     int tff, ret;
252
253     if (yadif->parity == -1) {
254         tff = yadif->cur->video->interlaced ?
255               yadif->cur->video->top_field_first : 1;
256     } else {
257         tff = yadif->parity ^ 1;
258     }
259
260     if (is_second) {
261         yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
262         if (!yadif->out)
263             return AVERROR(ENOMEM);
264
265         avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
266         yadif->out->video->interlaced = 0;
267     }
268
269     filter(ctx, yadif->out, tff ^ !is_second, tff);
270
271     if (is_second) {
272         int64_t cur_pts  = yadif->cur->pts;
273         int64_t next_pts = yadif->next->pts;
274
275         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
276             yadif->out->pts = cur_pts + next_pts;
277         } else {
278             yadif->out->pts = AV_NOPTS_VALUE;
279         }
280     }
281     ret = ff_filter_frame(ctx->outputs[0], yadif->out);
282
283     yadif->frame_pending = (yadif->mode&1) && !is_second;
284     return ret;
285 }
286
287 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
288 {
289     AVFilterContext *ctx = link->dst;
290     YADIFContext *yadif = ctx->priv;
291
292     if (yadif->frame_pending)
293         return_frame(ctx, 1);
294
295     if (yadif->prev)
296         avfilter_unref_buffer(yadif->prev);
297     yadif->prev = yadif->cur;
298     yadif->cur  = yadif->next;
299     yadif->next = picref;
300
301     if (!yadif->cur)
302         return 0;
303
304     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
305         yadif->out  = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
306         if (!yadif->out)
307             return AVERROR(ENOMEM);
308
309         avfilter_unref_bufferp(&yadif->prev);
310         if (yadif->out->pts != AV_NOPTS_VALUE)
311             yadif->out->pts *= 2;
312         return ff_filter_frame(ctx->outputs[0], yadif->out);
313     }
314
315     if (!yadif->prev &&
316         !(yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ)))
317         return AVERROR(ENOMEM);
318
319     yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
320                                      link->w, link->h);
321     if (!yadif->out)
322         return AVERROR(ENOMEM);
323
324     avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
325     yadif->out->video->interlaced = 0;
326
327     if (yadif->out->pts != AV_NOPTS_VALUE)
328         yadif->out->pts *= 2;
329
330     return return_frame(ctx, 0);
331 }
332
333 static int request_frame(AVFilterLink *link)
334 {
335     AVFilterContext *ctx = link->src;
336     YADIFContext *yadif = ctx->priv;
337
338     if (yadif->frame_pending) {
339         return_frame(ctx, 1);
340         return 0;
341     }
342
343     do {
344         int ret;
345
346         if (yadif->eof)
347             return AVERROR_EOF;
348
349         ret  = ff_request_frame(link->src->inputs[0]);
350
351         if (ret == AVERROR_EOF && yadif->next) {
352             AVFilterBufferRef *next =
353                 avfilter_ref_buffer(yadif->next, AV_PERM_READ);
354
355             if (!next)
356                 return AVERROR(ENOMEM);
357
358             next->pts = yadif->next->pts * 2 - yadif->cur->pts;
359
360             filter_frame(link->src->inputs[0], next);
361             yadif->eof = 1;
362         } else if (ret < 0) {
363             return ret;
364         }
365     } while (!yadif->cur);
366
367     return 0;
368 }
369
370 static int poll_frame(AVFilterLink *link)
371 {
372     YADIFContext *yadif = link->src->priv;
373     int ret, val;
374
375     if (yadif->frame_pending)
376         return 1;
377
378     val = ff_poll_frame(link->src->inputs[0]);
379     if (val <= 0)
380         return val;
381
382     //FIXME change API to not requre this red tape
383     if (val == 1 && !yadif->next) {
384         if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
385             return ret;
386         val = ff_poll_frame(link->src->inputs[0]);
387         if (val <= 0)
388             return val;
389     }
390     assert(yadif->next || !val);
391
392     if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
393         return val;
394
395     return val * ((yadif->mode&1)+1);
396 }
397
398 static av_cold void uninit(AVFilterContext *ctx)
399 {
400     YADIFContext *yadif = ctx->priv;
401
402     if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
403     if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
404     if (yadif->next) avfilter_unref_bufferp(&yadif->next);
405 }
406
407 static int query_formats(AVFilterContext *ctx)
408 {
409     static const enum AVPixelFormat pix_fmts[] = {
410         AV_PIX_FMT_YUV420P,
411         AV_PIX_FMT_YUV422P,
412         AV_PIX_FMT_YUV444P,
413         AV_PIX_FMT_YUV410P,
414         AV_PIX_FMT_YUV411P,
415         AV_PIX_FMT_GRAY8,
416         AV_PIX_FMT_YUVJ420P,
417         AV_PIX_FMT_YUVJ422P,
418         AV_PIX_FMT_YUVJ444P,
419         AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
420         AV_PIX_FMT_YUV440P,
421         AV_PIX_FMT_YUVJ440P,
422         AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
423         AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
424         AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
425         AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
426         AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
427         AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
428         AV_PIX_FMT_YUVA420P,
429         AV_PIX_FMT_NONE
430     };
431
432     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
433
434     return 0;
435 }
436
437 static av_cold int init(AVFilterContext *ctx, const char *args)
438 {
439     YADIFContext *yadif = ctx->priv;
440
441     yadif->mode = 0;
442     yadif->parity = -1;
443     yadif->auto_enable = 0;
444
445     if (args)
446         sscanf(args, "%d:%d:%d",
447                &yadif->mode, &yadif->parity, &yadif->auto_enable);
448
449     av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
450            yadif->mode, yadif->parity, yadif->auto_enable);
451
452     return 0;
453 }
454
455 static int config_props(AVFilterLink *link)
456 {
457     YADIFContext *s = link->src->priv;
458
459     link->time_base.num = link->src->inputs[0]->time_base.num;
460     link->time_base.den = link->src->inputs[0]->time_base.den * 2;
461     link->w             = link->src->inputs[0]->w;
462     link->h             = link->src->inputs[0]->h;
463
464     s->csp = av_pix_fmt_desc_get(link->format);
465     if (s->csp->comp[0].depth_minus1 / 8 == 1) {
466         s->filter_line  = filter_line_c_16bit;
467         s->filter_edges = filter_edges_16bit;
468     } else {
469         s->filter_line  = filter_line_c;
470         s->filter_edges = filter_edges;
471
472         if (ARCH_X86)
473             ff_yadif_init_x86(s);
474     }
475
476     return 0;
477 }
478
479 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
480     {
481         .name             = "default",
482         .type             = AVMEDIA_TYPE_VIDEO,
483         .get_video_buffer = get_video_buffer,
484         .filter_frame     = filter_frame,
485     },
486     { NULL }
487 };
488
489 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
490     {
491         .name          = "default",
492         .type          = AVMEDIA_TYPE_VIDEO,
493         .poll_frame    = poll_frame,
494         .request_frame = request_frame,
495         .config_props  = config_props,
496     },
497     { NULL }
498 };
499
500 AVFilter avfilter_vf_yadif = {
501     .name          = "yadif",
502     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
503
504     .priv_size     = sizeof(YADIFContext),
505     .init          = init,
506     .uninit        = uninit,
507     .query_formats = query_formats,
508
509     .inputs    = avfilter_vf_yadif_inputs,
510
511     .outputs   = avfilter_vf_yadif_outputs,
512 };