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