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