]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_yadif.c
Merge commit 'cb8f70c96e14c1b4824ef23d21d78d10fc5a4b93'
[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/opt.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 typedef struct ThreadData {
32     AVFrame *frame;
33     int plane;
34     int w, h;
35     int parity;
36     int tff;
37 } ThreadData;
38
39 #define CHECK(j)\
40     {   int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
41                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
42                   + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
43         if (score < spatial_score) {\
44             spatial_score= score;\
45             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
46
47 /* The is_not_edge argument here controls when the code will enter a branch
48  * which reads up to and including x-3 and x+3. */
49
50 #define FILTER(start, end, is_not_edge) \
51     for (x = start;  x < end; x++) { \
52         int c = cur[mrefs]; \
53         int d = (prev2[0] + next2[0])>>1; \
54         int e = cur[prefs]; \
55         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
56         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
57         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
58         int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
59         int spatial_pred = (c+e) >> 1; \
60  \
61         if (is_not_edge) {\
62             int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
63                               + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
64             CHECK(-1) CHECK(-2) }} }} \
65             CHECK( 1) CHECK( 2) }} }} \
66         }\
67  \
68         if (mode < 2) { \
69             int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
70             int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
71             int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
72             int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
73  \
74             diff = FFMAX3(diff, min, -max); \
75         } \
76  \
77         if (spatial_pred > d + diff) \
78            spatial_pred = d + diff; \
79         else if (spatial_pred < d - diff) \
80            spatial_pred = d - diff; \
81  \
82         dst[0] = spatial_pred; \
83  \
84         dst++; \
85         cur++; \
86         prev++; \
87         next++; \
88         prev2++; \
89         next2++; \
90     }
91
92 static void filter_line_c(void *dst1,
93                           void *prev1, void *cur1, void *next1,
94                           int w, int prefs, int mrefs, int parity, int mode)
95 {
96     uint8_t *dst  = dst1;
97     uint8_t *prev = prev1;
98     uint8_t *cur  = cur1;
99     uint8_t *next = next1;
100     int x;
101     uint8_t *prev2 = parity ? prev : cur ;
102     uint8_t *next2 = parity ? cur  : next;
103
104     /* The function is called with the pointers already pointing to data[3] and
105      * with 6 subtracted from the width.  This allows the FILTER macro to be
106      * called so that it processes all the pixels normally.  A constant value of
107      * true for is_not_edge lets the compiler ignore the if statement. */
108     FILTER(0, w, 1)
109 }
110
111 #define MAX_ALIGN 8
112 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
113                          int w, int prefs, int mrefs, int parity, int mode)
114 {
115     uint8_t *dst  = dst1;
116     uint8_t *prev = prev1;
117     uint8_t *cur  = cur1;
118     uint8_t *next = next1;
119     int x;
120     uint8_t *prev2 = parity ? prev : cur ;
121     uint8_t *next2 = parity ? cur  : next;
122
123     /* Only edge pixels need to be processed here.  A constant value of false
124      * for is_not_edge should let the compiler ignore the whole branch. */
125     FILTER(0, 3, 0)
126
127     dst  = (uint8_t*)dst1  + w - (MAX_ALIGN-1);
128     prev = (uint8_t*)prev1 + w - (MAX_ALIGN-1);
129     cur  = (uint8_t*)cur1  + w - (MAX_ALIGN-1);
130     next = (uint8_t*)next1 + w - (MAX_ALIGN-1);
131     prev2 = (uint8_t*)(parity ? prev : cur);
132     next2 = (uint8_t*)(parity ? cur  : next);
133
134     FILTER(w - (MAX_ALIGN-1), w - 3, 1)
135     FILTER(w - 3, w, 0)
136 }
137
138
139 static void filter_line_c_16bit(void *dst1,
140                                 void *prev1, void *cur1, void *next1,
141                                 int w, int prefs, int mrefs, int parity,
142                                 int mode)
143 {
144     uint16_t *dst  = dst1;
145     uint16_t *prev = prev1;
146     uint16_t *cur  = cur1;
147     uint16_t *next = next1;
148     int x;
149     uint16_t *prev2 = parity ? prev : cur ;
150     uint16_t *next2 = parity ? cur  : next;
151     mrefs /= 2;
152     prefs /= 2;
153
154     FILTER(0, w, 1)
155 }
156
157 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
158                                int w, int prefs, int mrefs, int parity, int mode)
159 {
160     uint16_t *dst  = dst1;
161     uint16_t *prev = prev1;
162     uint16_t *cur  = cur1;
163     uint16_t *next = next1;
164     int x;
165     uint16_t *prev2 = parity ? prev : cur ;
166     uint16_t *next2 = parity ? cur  : next;
167     mrefs /= 2;
168     prefs /= 2;
169
170     FILTER(0, 3, 0)
171
172     dst   = (uint16_t*)dst1  + w - (MAX_ALIGN/2-1);
173     prev  = (uint16_t*)prev1 + w - (MAX_ALIGN/2-1);
174     cur   = (uint16_t*)cur1  + w - (MAX_ALIGN/2-1);
175     next  = (uint16_t*)next1 + w - (MAX_ALIGN/2-1);
176     prev2 = (uint16_t*)(parity ? prev : cur);
177     next2 = (uint16_t*)(parity ? cur  : next);
178
179     FILTER(w - (MAX_ALIGN/2-1), w - 3, 1)
180     FILTER(w - 3, w, 0)
181 }
182
183 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
184 {
185     YADIFContext *s = ctx->priv;
186     ThreadData *td  = arg;
187     int refs = s->cur->linesize[td->plane];
188     int df = (s->csp->comp[td->plane].depth_minus1 + 8) / 8;
189     int pix_3 = 3 * df;
190     int slice_start = (td->h *  jobnr   ) / nb_jobs;
191     int slice_end   = (td->h * (jobnr+1)) / nb_jobs;
192     int y;
193
194     /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
195      * we need to call the c variant which avoids this for border pixels
196      */
197     for (y = slice_start; y < slice_end; y++) {
198         if ((y ^ td->parity) & 1) {
199             uint8_t *prev = &s->prev->data[td->plane][y * refs];
200             uint8_t *cur  = &s->cur ->data[td->plane][y * refs];
201             uint8_t *next = &s->next->data[td->plane][y * refs];
202             uint8_t *dst  = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
203             int     mode  = y == 1 || y + 2 == td->h ? 2 : s->mode;
204             s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
205                            next + pix_3, td->w - (3 + MAX_ALIGN/df-1),
206                            y + 1 < td->h ? refs : -refs,
207                            y ? -refs : refs,
208                            td->parity ^ td->tff, mode);
209             s->filter_edges(dst, prev, cur, next, td->w,
210                             y + 1 < td->h ? refs : -refs,
211                             y ? -refs : refs,
212                             td->parity ^ td->tff, mode);
213         } else {
214             memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
215                    &s->cur->data[td->plane][y * refs], td->w * df);
216         }
217     }
218     return 0;
219 }
220
221 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
222                    int parity, int tff)
223 {
224     YADIFContext *yadif = ctx->priv;
225     ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
226     int i;
227
228     for (i = 0; i < yadif->csp->nb_components; i++) {
229         int w = dstpic->width;
230         int h = dstpic->height;
231
232         if (i == 1 || i == 2) {
233             w = FF_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
234             h = FF_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
235         }
236
237
238         td.w       = w;
239         td.h       = h;
240         td.plane   = i;
241
242         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
243     }
244
245     emms_c();
246 }
247
248 static int return_frame(AVFilterContext *ctx, int is_second)
249 {
250     YADIFContext *yadif = ctx->priv;
251     AVFilterLink *link  = ctx->outputs[0];
252     int tff, ret;
253
254     if (yadif->parity == -1) {
255         tff = yadif->cur->interlaced_frame ?
256               yadif->cur->top_field_first : 1;
257     } else {
258         tff = yadif->parity ^ 1;
259     }
260
261     if (is_second) {
262         yadif->out = ff_get_video_buffer(link, link->w, link->h);
263         if (!yadif->out)
264             return AVERROR(ENOMEM);
265
266         av_frame_copy_props(yadif->out, yadif->cur);
267         yadif->out->interlaced_frame = 0;
268     }
269
270     filter(ctx, yadif->out, tff ^ !is_second, tff);
271
272     if (is_second) {
273         int64_t cur_pts  = yadif->cur->pts;
274         int64_t next_pts = yadif->next->pts;
275
276         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
277             yadif->out->pts = cur_pts + next_pts;
278         } else {
279             yadif->out->pts = AV_NOPTS_VALUE;
280         }
281     }
282     ret = ff_filter_frame(ctx->outputs[0], yadif->out);
283
284     yadif->frame_pending = (yadif->mode&1) && !is_second;
285     return ret;
286 }
287
288 static int filter_frame(AVFilterLink *link, AVFrame *frame)
289 {
290     AVFilterContext *ctx = link->dst;
291     YADIFContext *yadif = ctx->priv;
292
293     av_assert0(frame);
294
295     if (yadif->frame_pending)
296         return_frame(ctx, 1);
297
298     if (yadif->prev)
299         av_frame_free(&yadif->prev);
300     yadif->prev = yadif->cur;
301     yadif->cur  = yadif->next;
302     yadif->next = frame;
303
304     if (!yadif->cur)
305         return 0;
306
307     if ((yadif->deint && !yadif->cur->interlaced_frame) || ctx->is_disabled) {
308         yadif->out  = av_frame_clone(yadif->cur);
309         if (!yadif->out)
310             return AVERROR(ENOMEM);
311
312         av_frame_free(&yadif->prev);
313         if (yadif->out->pts != AV_NOPTS_VALUE)
314             yadif->out->pts *= 2;
315         return ff_filter_frame(ctx->outputs[0], yadif->out);
316     }
317
318     if (!yadif->prev &&
319         !(yadif->prev = av_frame_clone(yadif->cur)))
320         return AVERROR(ENOMEM);
321
322     yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
323     if (!yadif->out)
324         return AVERROR(ENOMEM);
325
326     av_frame_copy_props(yadif->out, yadif->cur);
327     yadif->out->interlaced_frame = 0;
328
329     if (yadif->out->pts != AV_NOPTS_VALUE)
330         yadif->out->pts *= 2;
331
332     return return_frame(ctx, 0);
333 }
334
335 static int request_frame(AVFilterLink *link)
336 {
337     AVFilterContext *ctx = link->src;
338     YADIFContext *yadif = ctx->priv;
339
340     if (yadif->frame_pending) {
341         return_frame(ctx, 1);
342         return 0;
343     }
344
345     do {
346         int ret;
347
348         if (yadif->eof)
349             return AVERROR_EOF;
350
351         ret  = ff_request_frame(link->src->inputs[0]);
352
353         if (ret == AVERROR_EOF && yadif->cur) {
354             AVFrame *next = av_frame_clone(yadif->next);
355
356             if (!next)
357                 return AVERROR(ENOMEM);
358
359             next->pts = yadif->next->pts * 2 - yadif->cur->pts;
360
361             filter_frame(link->src->inputs[0], next);
362             yadif->eof = 1;
363         } else if (ret < 0) {
364             return ret;
365         }
366     } while (!yadif->cur);
367
368     return 0;
369 }
370
371 static av_cold void uninit(AVFilterContext *ctx)
372 {
373     YADIFContext *yadif = ctx->priv;
374
375     av_frame_free(&yadif->prev);
376     av_frame_free(&yadif->cur );
377     av_frame_free(&yadif->next);
378 }
379
380 static int query_formats(AVFilterContext *ctx)
381 {
382     static const enum AVPixelFormat pix_fmts[] = {
383         AV_PIX_FMT_YUV420P,
384         AV_PIX_FMT_YUV422P,
385         AV_PIX_FMT_YUV444P,
386         AV_PIX_FMT_YUV410P,
387         AV_PIX_FMT_YUV411P,
388         AV_PIX_FMT_GRAY8,
389         AV_PIX_FMT_YUVJ420P,
390         AV_PIX_FMT_YUVJ422P,
391         AV_PIX_FMT_YUVJ444P,
392         AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
393         AV_PIX_FMT_YUV440P,
394         AV_PIX_FMT_YUVJ440P,
395         AV_NE( AV_PIX_FMT_YUV420P9BE,  AV_PIX_FMT_YUV420P9LE ),
396         AV_NE( AV_PIX_FMT_YUV422P9BE,  AV_PIX_FMT_YUV422P9LE ),
397         AV_NE( AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUV444P9LE ),
398         AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
399         AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
400         AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
401         AV_NE( AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE ),
402         AV_NE( AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE ),
403         AV_NE( AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE ),
404         AV_NE( AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE ),
405         AV_NE( AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE ),
406         AV_NE( AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE ),
407         AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
408         AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
409         AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
410         AV_PIX_FMT_YUVA420P,
411         AV_PIX_FMT_YUVA422P,
412         AV_PIX_FMT_YUVA444P,
413         AV_PIX_FMT_NONE
414     };
415
416     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
417
418     return 0;
419 }
420
421 static int config_props(AVFilterLink *link)
422 {
423     AVFilterContext *ctx = link->src;
424     YADIFContext *s = link->src->priv;
425
426     link->time_base.num = link->src->inputs[0]->time_base.num;
427     link->time_base.den = link->src->inputs[0]->time_base.den * 2;
428     link->w             = link->src->inputs[0]->w;
429     link->h             = link->src->inputs[0]->h;
430
431     if(s->mode&1)
432         link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
433
434     if (link->w < 3 || link->h < 3) {
435         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
436         return AVERROR(EINVAL);
437     }
438
439     s->csp = av_pix_fmt_desc_get(link->format);
440     if (s->csp->comp[0].depth_minus1 / 8 == 1) {
441         s->filter_line  = filter_line_c_16bit;
442         s->filter_edges = filter_edges_16bit;
443     } else {
444         s->filter_line  = filter_line_c;
445         s->filter_edges = filter_edges;
446     }
447
448     if (ARCH_X86)
449         ff_yadif_init_x86(s);
450
451     return 0;
452 }
453
454
455 #define OFFSET(x) offsetof(YADIFContext, x)
456 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
457
458 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
459
460 static const AVOption yadif_options[] = {
461     { "mode",   "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
462     CONST("send_frame",           "send one frame for each frame",                                     YADIF_MODE_SEND_FRAME,           "mode"),
463     CONST("send_field",           "send one frame for each field",                                     YADIF_MODE_SEND_FIELD,           "mode"),
464     CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
465     CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
466
467     { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
468     CONST("tff",  "assume top field first",    YADIF_PARITY_TFF,  "parity"),
469     CONST("bff",  "assume bottom field first", YADIF_PARITY_BFF,  "parity"),
470     CONST("auto", "auto detect parity",        YADIF_PARITY_AUTO, "parity"),
471
472     { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
473     CONST("all",        "deinterlace all frames",                       YADIF_DEINT_ALL,         "deint"),
474     CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED,  "deint"),
475
476     {NULL},
477 };
478
479 AVFILTER_DEFINE_CLASS(yadif);
480
481 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
482     {
483         .name             = "default",
484         .type             = AVMEDIA_TYPE_VIDEO,
485         .filter_frame     = filter_frame,
486     },
487     { NULL }
488 };
489
490 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
491     {
492         .name          = "default",
493         .type          = AVMEDIA_TYPE_VIDEO,
494         .request_frame = request_frame,
495         .config_props  = config_props,
496     },
497     { NULL }
498 };
499
500 AVFilter avfilter_vf_yadif = {
501     .name          = "yadif",
502     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
503
504     .priv_size     = sizeof(YADIFContext),
505     .priv_class    = &yadif_class,
506     .uninit        = uninit,
507     .query_formats = query_formats,
508
509     .inputs    = avfilter_vf_yadif_inputs,
510     .outputs   = avfilter_vf_yadif_outputs,
511     .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
512 };