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