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