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