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