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