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