]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_yadif.c
lavfi: remove avfilter_default_* from public API on next bump.
[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 "video.h"
27 #include "yadif.h"
28
29 #undef NDEBUG
30 #include <assert.h>
31
32 typedef struct {
33     /**
34      * 0: send 1 frame for each frame
35      * 1: send 1 frame for each field
36      * 2: like 0 but skips spatial interlacing check
37      * 3: like 1 but skips spatial interlacing check
38      */
39     int mode;
40
41     /**
42      *  0: top field first
43      *  1: bottom field first
44      * -1: auto-detection
45      */
46     int parity;
47
48     int frame_pending;
49
50     /**
51      *  0: deinterlace all frames
52      *  1: only deinterlace frames marked as interlaced
53      */
54     int auto_enable;
55
56     AVFilterBufferRef *cur;
57     AVFilterBufferRef *next;
58     AVFilterBufferRef *prev;
59     AVFilterBufferRef *out;
60     void (*filter_line)(uint8_t *dst,
61                         uint8_t *prev, uint8_t *cur, uint8_t *next,
62                         int w, int prefs, int mrefs, int parity, int mode);
63
64     const AVPixFmtDescriptor *csp;
65     int eof;
66 } YADIFContext;
67
68 #define CHECK(j)\
69     {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
70                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
71                   + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
72         if (score < spatial_score) {\
73             spatial_score= score;\
74             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
75
76 #define FILTER \
77     for (x = 0;  x < w; x++) { \
78         int c = cur[mrefs]; \
79         int d = (prev2[0] + next2[0])>>1; \
80         int e = cur[prefs]; \
81         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
82         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
83         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
84         int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
85         int spatial_pred = (c+e)>>1; \
86         int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
87                           + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
88  \
89         CHECK(-1) CHECK(-2) }} }} \
90         CHECK( 1) CHECK( 2) }} }} \
91  \
92         if (mode < 2) { \
93             int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
94             int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
95             int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
96             int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
97  \
98             diff = FFMAX3(diff, min, -max); \
99         } \
100  \
101         if (spatial_pred > d + diff) \
102            spatial_pred = d + diff; \
103         else if (spatial_pred < d - diff) \
104            spatial_pred = d - diff; \
105  \
106         dst[0] = spatial_pred; \
107  \
108         dst++; \
109         cur++; \
110         prev++; \
111         next++; \
112         prev2++; \
113         next2++; \
114     }
115
116 static void filter_line_c(uint8_t *dst,
117                           uint8_t *prev, uint8_t *cur, uint8_t *next,
118                           int w, int prefs, int mrefs, int parity, int mode)
119 {
120     int x;
121     uint8_t *prev2 = parity ? prev : cur ;
122     uint8_t *next2 = parity ? cur  : next;
123
124     FILTER
125 }
126
127 static void filter_line_c_16bit(uint16_t *dst,
128                                 uint16_t *prev, uint16_t *cur, uint16_t *next,
129                                 int w, int prefs, int mrefs, int parity, int mode)
130 {
131     int x;
132     uint16_t *prev2 = parity ? prev : cur ;
133     uint16_t *next2 = parity ? cur  : next;
134     mrefs /= 2;
135     prefs /= 2;
136
137     FILTER
138 }
139
140 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
141                    int parity, int tff)
142 {
143     YADIFContext *yadif = ctx->priv;
144     int y, i;
145
146     for (i = 0; i < yadif->csp->nb_components; i++) {
147         int w = dstpic->video->w;
148         int h = dstpic->video->h;
149         int refs = yadif->cur->linesize[i];
150         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
151
152         if (i == 1 || i == 2) {
153         /* Why is this not part of the per-plane description thing? */
154             w >>= yadif->csp->log2_chroma_w;
155             h >>= yadif->csp->log2_chroma_h;
156         }
157
158         for (y = 0; y < h; y++) {
159             if ((y ^ parity) & 1) {
160                 uint8_t *prev = &yadif->prev->data[i][y*refs];
161                 uint8_t *cur  = &yadif->cur ->data[i][y*refs];
162                 uint8_t *next = &yadif->next->data[i][y*refs];
163                 uint8_t *dst  = &dstpic->data[i][y*dstpic->linesize[i]];
164                 int     mode  = y==1 || y+2==h ? 2 : yadif->mode;
165                 yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
166             } else {
167                 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
168                        &yadif->cur->data[i][y*refs], w*df);
169             }
170         }
171     }
172 #if HAVE_MMX
173     __asm__ volatile("emms \n\t" : : : "memory");
174 #endif
175 }
176
177 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
178 {
179     AVFilterBufferRef *picref;
180     int width = FFALIGN(w, 32);
181     int height= FFALIGN(h+2, 32);
182     int i;
183
184     picref = ff_default_get_video_buffer(link, perms, width, height);
185
186     picref->video->w = w;
187     picref->video->h = h;
188
189     for (i = 0; i < 3; i++)
190         picref->data[i] += picref->linesize[i];
191
192     return picref;
193 }
194
195 static void return_frame(AVFilterContext *ctx, int is_second)
196 {
197     YADIFContext *yadif = ctx->priv;
198     AVFilterLink *link= ctx->outputs[0];
199     int tff;
200
201     if (yadif->parity == -1) {
202         tff = yadif->cur->video->interlaced ?
203             yadif->cur->video->top_field_first : 1;
204     } else {
205         tff = yadif->parity^1;
206     }
207
208     if (is_second) {
209         yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
210                                                AV_PERM_REUSE, link->w, link->h);
211         avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
212         yadif->out->video->interlaced = 0;
213     }
214
215     if (!yadif->csp)
216         yadif->csp = &av_pix_fmt_descriptors[link->format];
217     if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
218         yadif->filter_line = filter_line_c_16bit;
219
220     filter(ctx, yadif->out, tff ^ !is_second, tff);
221
222     if (is_second) {
223         int64_t cur_pts  = yadif->cur->pts;
224         int64_t next_pts = yadif->next->pts;
225
226         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
227             yadif->out->pts = cur_pts + next_pts;
228         } else {
229             yadif->out->pts = AV_NOPTS_VALUE;
230         }
231         avfilter_start_frame(ctx->outputs[0], yadif->out);
232     }
233     avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
234     avfilter_end_frame(ctx->outputs[0]);
235
236     yadif->frame_pending = (yadif->mode&1) && !is_second;
237 }
238
239 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
240 {
241     AVFilterContext *ctx = link->dst;
242     YADIFContext *yadif = ctx->priv;
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->next) {
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_NONE
388     };
389
390     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
391
392     return 0;
393 }
394
395 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
396 {
397     YADIFContext *yadif = ctx->priv;
398     int cpu_flags = av_get_cpu_flags();
399
400     yadif->mode = 0;
401     yadif->parity = -1;
402     yadif->auto_enable = 0;
403     yadif->csp = NULL;
404
405     if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
406
407     yadif->filter_line = filter_line_c;
408     if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
409         yadif->filter_line = ff_yadif_filter_line_ssse3;
410     else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
411         yadif->filter_line = ff_yadif_filter_line_sse2;
412     else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
413         yadif->filter_line = ff_yadif_filter_line_mmx;
414
415     av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
416
417     return 0;
418 }
419
420 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
421
422 static int config_props(AVFilterLink *link)
423 {
424     link->time_base.num = link->src->inputs[0]->time_base.num;
425     link->time_base.den = link->src->inputs[0]->time_base.den * 2;
426     link->w             = link->src->inputs[0]->w;
427     link->h             = link->src->inputs[0]->h;
428
429     return 0;
430 }
431
432 AVFilter avfilter_vf_yadif = {
433     .name          = "yadif",
434     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
435
436     .priv_size     = sizeof(YADIFContext),
437     .init          = init,
438     .uninit        = uninit,
439     .query_formats = query_formats,
440
441     .inputs    = (AVFilterPad[]) {{ .name             = "default",
442                                     .type             = AVMEDIA_TYPE_VIDEO,
443                                     .start_frame      = start_frame,
444                                     .get_video_buffer = get_video_buffer,
445                                     .draw_slice       = null_draw_slice,
446                                     .end_frame        = end_frame, },
447                                   { .name = NULL}},
448
449     .outputs   = (AVFilterPad[]) {{ .name             = "default",
450                                     .type             = AVMEDIA_TYPE_VIDEO,
451                                     .poll_frame       = poll_frame,
452                                     .request_frame    = request_frame,
453                                     .config_props     = config_props, },
454                                   { .name = NULL}},
455 };