]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_yadif.c
vf_yadif: Fixed potential access violation on EOF.
[ffmpeg] / libavfilter / vf_yadif.c
1 /*
2  * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3  *               2010      James Darnley <james.darnley@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "libavutil/avassert.h"
21 #include "libavutil/cpu.h"
22 #include "libavutil/common.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "yadif.h"
26
27 #undef NDEBUG
28 #include <assert.h>
29
30 typedef struct {
31     /**
32      * 0: send 1 frame for each frame
33      * 1: send 1 frame for each field
34      * 2: like 0 but skips spatial interlacing check
35      * 3: like 1 but skips spatial interlacing check
36      */
37     int mode;
38
39     /**
40      *  0: top field first
41      *  1: bottom field first
42      * -1: auto-detection
43      */
44     int parity;
45
46     int frame_pending;
47
48     /**
49      *  0: deinterlace all frames
50      *  1: only deinterlace frames marked as interlaced
51      */
52     int auto_enable;
53
54     AVFilterBufferRef *cur;
55     AVFilterBufferRef *next;
56     AVFilterBufferRef *prev;
57     AVFilterBufferRef *out;
58     void (*filter_line)(uint8_t *dst,
59                         uint8_t *prev, uint8_t *cur, uint8_t *next,
60                         int w, int prefs, int mrefs, int parity, int mode);
61
62     const AVPixFmtDescriptor *csp;
63     int eof;
64 } YADIFContext;
65
66 #define CHECK(j)\
67     {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
68                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
69                   + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
70         if (score < spatial_score) {\
71             spatial_score= score;\
72             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
73
74 #define FILTER \
75     for (x = 0;  x < w; x++) { \
76         int c = cur[mrefs]; \
77         int d = (prev2[0] + next2[0])>>1; \
78         int e = cur[prefs]; \
79         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
80         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
81         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
82         int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
83         int spatial_pred = (c+e)>>1; \
84         int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
85                           + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
86  \
87         CHECK(-1) CHECK(-2) }} }} \
88         CHECK( 1) CHECK( 2) }} }} \
89  \
90         if (mode < 2) { \
91             int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
92             int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
93             int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
94             int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
95  \
96             diff = FFMAX3(diff, min, -max); \
97         } \
98  \
99         if (spatial_pred > d + diff) \
100            spatial_pred = d + diff; \
101         else if (spatial_pred < d - diff) \
102            spatial_pred = d - diff; \
103  \
104         dst[0] = spatial_pred; \
105  \
106         dst++; \
107         cur++; \
108         prev++; \
109         next++; \
110         prev2++; \
111         next2++; \
112     }
113
114 static void filter_line_c(uint8_t *dst,
115                           uint8_t *prev, uint8_t *cur, uint8_t *next,
116                           int w, int prefs, int mrefs, int parity, int mode)
117 {
118     int x;
119     uint8_t *prev2 = parity ? prev : cur ;
120     uint8_t *next2 = parity ? cur  : next;
121
122     FILTER
123 }
124
125 static void filter_line_c_16bit(uint16_t *dst,
126                                 uint16_t *prev, uint16_t *cur, uint16_t *next,
127                                 int w, int prefs, int mrefs, int parity, int mode)
128 {
129     int x;
130     uint16_t *prev2 = parity ? prev : cur ;
131     uint16_t *next2 = parity ? cur  : next;
132     mrefs /= 2;
133     prefs /= 2;
134
135     FILTER
136 }
137
138 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
139                    int parity, int tff)
140 {
141     YADIFContext *yadif = ctx->priv;
142     int y, i;
143
144     for (i = 0; i < yadif->csp->nb_components; i++) {
145         int w = dstpic->video->w;
146         int h = dstpic->video->h;
147         int refs = yadif->cur->linesize[i];
148         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
149
150         if (i == 1 || i == 2) {
151         /* Why is this not part of the per-plane description thing? */
152             w >>= yadif->csp->log2_chroma_w;
153             h >>= yadif->csp->log2_chroma_h;
154         }
155
156         for (y = 0; y < h; y++) {
157             if ((y ^ parity) & 1) {
158                 uint8_t *prev = &yadif->prev->data[i][y*refs];
159                 uint8_t *cur  = &yadif->cur ->data[i][y*refs];
160                 uint8_t *next = &yadif->next->data[i][y*refs];
161                 uint8_t *dst  = &dstpic->data[i][y*dstpic->linesize[i]];
162                 int     mode  = y==1 || y+2==h ? 2 : yadif->mode;
163                 yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
164             } else {
165                 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
166                        &yadif->cur->data[i][y*refs], w*df);
167             }
168         }
169     }
170 #if HAVE_MMX
171     __asm__ volatile("emms \n\t" : : : "memory");
172 #endif
173 }
174
175 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
176 {
177     AVFilterBufferRef *picref;
178     int width = FFALIGN(w, 32);
179     int height= FFALIGN(h+2, 32);
180     int i;
181
182     picref = avfilter_default_get_video_buffer(link, perms, width, height);
183
184     picref->video->w = w;
185     picref->video->h = h;
186
187     for (i = 0; i < 3; i++)
188         picref->data[i] += picref->linesize[i];
189
190     return picref;
191 }
192
193 static void return_frame(AVFilterContext *ctx, int is_second)
194 {
195     YADIFContext *yadif = ctx->priv;
196     AVFilterLink *link= ctx->outputs[0];
197     int tff;
198
199     if (yadif->parity == -1) {
200         tff = yadif->cur->video->interlaced ?
201             yadif->cur->video->top_field_first : 1;
202     } else {
203         tff = yadif->parity^1;
204     }
205
206     if (is_second) {
207         yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
208                                                AV_PERM_REUSE, link->w, link->h);
209         avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
210         yadif->out->video->interlaced = 0;
211     }
212
213     if (!yadif->csp)
214         yadif->csp = &av_pix_fmt_descriptors[link->format];
215     if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
216         yadif->filter_line = (void*)filter_line_c_16bit;
217
218     filter(ctx, yadif->out, tff ^ !is_second, tff);
219
220     if (is_second) {
221         int64_t cur_pts  = yadif->cur->pts;
222         int64_t next_pts = yadif->next->pts;
223
224         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
225             yadif->out->pts = cur_pts + next_pts;
226         } else {
227             yadif->out->pts = AV_NOPTS_VALUE;
228         }
229         avfilter_start_frame(ctx->outputs[0], yadif->out);
230     }
231     avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
232     avfilter_end_frame(ctx->outputs[0]);
233
234     yadif->frame_pending = (yadif->mode&1) && !is_second;
235 }
236
237 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
238 {
239     AVFilterContext *ctx = link->dst;
240     YADIFContext *yadif = ctx->priv;
241
242     av_assert0(picref);
243
244     if (yadif->frame_pending)
245         return_frame(ctx, 1);
246
247     if (yadif->prev)
248         avfilter_unref_buffer(yadif->prev);
249     yadif->prev = yadif->cur;
250     yadif->cur  = yadif->next;
251     yadif->next = picref;
252
253     if (!yadif->cur)
254         return;
255
256     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
257         yadif->out  = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
258         avfilter_unref_buffer(yadif->prev);
259         yadif->prev = NULL;
260         if (yadif->out->pts != AV_NOPTS_VALUE)
261             yadif->out->pts *= 2;
262         avfilter_start_frame(ctx->outputs[0], yadif->out);
263         return;
264     }
265
266     if (!yadif->prev)
267         yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
268
269     yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
270                                        AV_PERM_REUSE, link->w, link->h);
271
272     avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
273     yadif->out->video->interlaced = 0;
274     if (yadif->out->pts != AV_NOPTS_VALUE)
275         yadif->out->pts *= 2;
276     avfilter_start_frame(ctx->outputs[0], yadif->out);
277 }
278
279 static void end_frame(AVFilterLink *link)
280 {
281     AVFilterContext *ctx = link->dst;
282     YADIFContext *yadif = ctx->priv;
283
284     if (!yadif->out)
285         return;
286
287     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
288         avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
289         avfilter_end_frame(ctx->outputs[0]);
290         return;
291     }
292
293     return_frame(ctx, 0);
294 }
295
296 static int request_frame(AVFilterLink *link)
297 {
298     AVFilterContext *ctx = link->src;
299     YADIFContext *yadif = ctx->priv;
300
301     if (yadif->frame_pending) {
302         return_frame(ctx, 1);
303         return 0;
304     }
305
306     do {
307         int ret;
308
309         if (yadif->eof)
310             return AVERROR_EOF;
311
312         ret  = avfilter_request_frame(link->src->inputs[0]);
313
314         if (ret == AVERROR_EOF && yadif->cur) {
315             AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, AV_PERM_READ);
316             next->pts = yadif->next->pts * 2 - yadif->cur->pts;
317
318             start_frame(link->src->inputs[0], next);
319             end_frame(link->src->inputs[0]);
320             yadif->eof = 1;
321         } else if (ret < 0) {
322             return ret;
323         }
324     } while (!yadif->cur);
325
326     return 0;
327 }
328
329 static int poll_frame(AVFilterLink *link)
330 {
331     YADIFContext *yadif = link->src->priv;
332     int ret, val;
333
334     if (yadif->frame_pending)
335         return 1;
336
337     val = avfilter_poll_frame(link->src->inputs[0]);
338     if (val <= 0)
339         return val;
340
341     if (val >= 1 && !yadif->next) { //FIXME change API to not requre this red tape
342         if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
343             return ret;
344         val = avfilter_poll_frame(link->src->inputs[0]);
345         if (val <= 0)
346             return val;
347     }
348     assert(yadif->next || !val);
349
350     if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
351         return val;
352
353     return val * ((yadif->mode&1)+1);
354 }
355
356 static av_cold void uninit(AVFilterContext *ctx)
357 {
358     YADIFContext *yadif = ctx->priv;
359
360     if (yadif->prev) avfilter_unref_buffer(yadif->prev);
361     if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
362     if (yadif->next) avfilter_unref_buffer(yadif->next);
363 }
364
365 static int query_formats(AVFilterContext *ctx)
366 {
367     static const enum PixelFormat pix_fmts[] = {
368         PIX_FMT_YUV420P,
369         PIX_FMT_YUV422P,
370         PIX_FMT_YUV444P,
371         PIX_FMT_YUV410P,
372         PIX_FMT_YUV411P,
373         PIX_FMT_GRAY8,
374         PIX_FMT_YUVJ420P,
375         PIX_FMT_YUVJ422P,
376         PIX_FMT_YUVJ444P,
377         AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
378         PIX_FMT_YUV440P,
379         PIX_FMT_YUVJ440P,
380         AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
381         AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
382         AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
383         AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
384         AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
385         AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
386         PIX_FMT_YUVA420P,
387         PIX_FMT_YUVA422P,
388         PIX_FMT_YUVA444P,
389         PIX_FMT_NONE
390     };
391
392     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
393
394     return 0;
395 }
396
397 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
398 {
399     YADIFContext *yadif = ctx->priv;
400     int cpu_flags = av_get_cpu_flags();
401
402     yadif->mode = 0;
403     yadif->parity = -1;
404     yadif->auto_enable = 0;
405     yadif->csp = NULL;
406
407     if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
408
409     yadif->filter_line = filter_line_c;
410     if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
411         yadif->filter_line = ff_yadif_filter_line_ssse3;
412     else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
413         yadif->filter_line = ff_yadif_filter_line_sse2;
414     else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
415         yadif->filter_line = ff_yadif_filter_line_mmx;
416
417     av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
418
419     return 0;
420 }
421
422 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
423
424 static int config_props(AVFilterLink *link)
425 {
426     link->time_base.num = link->src->inputs[0]->time_base.num;
427     link->time_base.den = link->src->inputs[0]->time_base.den * 2;
428     link->w             = link->src->inputs[0]->w;
429     link->h             = link->src->inputs[0]->h;
430
431     return 0;
432 }
433
434 AVFilter avfilter_vf_yadif = {
435     .name          = "yadif",
436     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
437
438     .priv_size     = sizeof(YADIFContext),
439     .init          = init,
440     .uninit        = uninit,
441     .query_formats = query_formats,
442
443     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
444                                     .type             = AVMEDIA_TYPE_VIDEO,
445                                     .start_frame      = start_frame,
446                                     .get_video_buffer = get_video_buffer,
447                                     .draw_slice       = null_draw_slice,
448                                     .end_frame        = end_frame,
449                                     .rej_perms        = AV_PERM_REUSE2, },
450                                   { .name = NULL}},
451
452     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
453                                     .type             = AVMEDIA_TYPE_VIDEO,
454                                     .poll_frame       = poll_frame,
455                                     .request_frame    = request_frame,
456                                     .config_props     = config_props, },
457                                   { .name = NULL}},
458 };