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