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