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