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