]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_yadif.c
internal.h: remove start/end_frame from AVFilterPad
[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 #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(uint8_t *dst,
85                           uint8_t *prev, uint8_t *cur, uint8_t *next,
86                           int w, int prefs, int mrefs, int parity, int mode)
87 {
88     int x;
89     uint8_t *prev2 = parity ? prev : cur ;
90     uint8_t *next2 = parity ? cur  : next;
91
92     FILTER
93 }
94
95 static void filter_line_c_16bit(uint16_t *dst,
96                                 uint16_t *prev, uint16_t *cur, uint16_t *next,
97                                 int w, int prefs, int mrefs, int parity,
98                                 int mode)
99 {
100     int x;
101     uint16_t *prev2 = parity ? prev : cur ;
102     uint16_t *next2 = parity ? cur  : next;
103     mrefs /= 2;
104     prefs /= 2;
105
106     FILTER
107 }
108
109 static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
110                    int parity, int tff)
111 {
112     YADIFContext *yadif = ctx->priv;
113     int y, i;
114
115     for (i = 0; i < yadif->csp->nb_components; i++) {
116         int w = dstpic->video->w;
117         int h = dstpic->video->h;
118         int refs = yadif->cur->linesize[i];
119         int absrefs = FFABS(refs);
120         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
121
122         if (i == 1 || i == 2) {
123         /* Why is this not part of the per-plane description thing? */
124             w >>= yadif->csp->log2_chroma_w;
125             h >>= yadif->csp->log2_chroma_h;
126         }
127
128         if(yadif->temp_line_size < absrefs) {
129             av_free(yadif->temp_line);
130             yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
131             yadif->temp_line_size = absrefs;
132         }
133
134         for (y = 0; y < h; y++) {
135             if ((y ^ parity) & 1) {
136                 uint8_t *prev = &yadif->prev->data[i][y * refs];
137                 uint8_t *cur  = &yadif->cur ->data[i][y * refs];
138                 uint8_t *next = &yadif->next->data[i][y * refs];
139                 uint8_t *dst  = &dstpic->data[i][y * dstpic->linesize[i]];
140                 int     mode  = y == 1 || y + 2 == h ? 2 : yadif->mode;
141                 int     prefs = y+1<h ? refs : -refs;
142                 int     mrefs =     y ?-refs :  refs;
143
144                 if(y<=1 || y+2>=h) {
145                     uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
146                     if(mode<2)
147                         memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
148                     memcpy(tmp+mrefs, cur+mrefs, w*df);
149                     memcpy(tmp      , cur      , w*df);
150                     if(prefs != mrefs) {
151                         memcpy(tmp+prefs, cur+prefs, w*df);
152                         if(mode<2)
153                             memcpy(tmp+2*prefs, cur+2*prefs, w*df);
154                     }
155                     cur = tmp;
156                 }
157
158                 yadif->filter_line(dst, prev, cur, next, w,
159                                    prefs, mrefs,
160                                    parity ^ tff, mode);
161             } else {
162                 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
163                        &yadif->cur->data[i][y * refs], w * df);
164             }
165         }
166     }
167
168     emms_c();
169 }
170
171 static int return_frame(AVFilterContext *ctx, int is_second)
172 {
173     YADIFContext *yadif = ctx->priv;
174     AVFilterLink *link  = ctx->outputs[0];
175     int tff, ret;
176
177     if (yadif->parity == -1) {
178         tff = yadif->cur->video->interlaced ?
179               yadif->cur->video->top_field_first : 1;
180     } else {
181         tff = yadif->parity ^ 1;
182     }
183
184     if (is_second) {
185         yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
186         if (!yadif->out)
187             return AVERROR(ENOMEM);
188
189         avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
190         yadif->out->video->interlaced = 0;
191     }
192
193     if (!yadif->csp)
194         yadif->csp = av_pix_fmt_desc_get(link->format);
195     if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
196         yadif->filter_line = (void*)filter_line_c_16bit;
197
198     filter(ctx, yadif->out, tff ^ !is_second, tff);
199
200     if (is_second) {
201         int64_t cur_pts  = yadif->cur->pts;
202         int64_t next_pts = yadif->next->pts;
203
204         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
205             yadif->out->pts = cur_pts + next_pts;
206         } else {
207             yadif->out->pts = AV_NOPTS_VALUE;
208         }
209     }
210     ret = ff_filter_frame(ctx->outputs[0], yadif->out);
211
212     yadif->frame_pending = (yadif->mode&1) && !is_second;
213     return ret;
214 }
215
216 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
217 {
218     AVFilterContext *ctx = link->dst;
219     YADIFContext *yadif = ctx->priv;
220
221     av_assert0(picref);
222
223     if (yadif->frame_pending)
224         return_frame(ctx, 1);
225
226     if (yadif->prev)
227         avfilter_unref_buffer(yadif->prev);
228     yadif->prev = yadif->cur;
229     yadif->cur  = yadif->next;
230     yadif->next = picref;
231
232     if (!yadif->cur)
233         return 0;
234
235     if (yadif->deint && !yadif->cur->video->interlaced) {
236         yadif->out  = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
237         if (!yadif->out)
238             return AVERROR(ENOMEM);
239
240         avfilter_unref_bufferp(&yadif->prev);
241         if (yadif->out->pts != AV_NOPTS_VALUE)
242             yadif->out->pts *= 2;
243         return ff_filter_frame(ctx->outputs[0], yadif->out);
244     }
245
246     if (!yadif->prev &&
247         !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
248         return AVERROR(ENOMEM);
249
250     yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
251                                      link->w, link->h);
252     if (!yadif->out)
253         return AVERROR(ENOMEM);
254
255     avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
256     yadif->out->video->interlaced = 0;
257
258     if (yadif->out->pts != AV_NOPTS_VALUE)
259         yadif->out->pts *= 2;
260
261     return return_frame(ctx, 0);
262 }
263
264 static int request_frame(AVFilterLink *link)
265 {
266     AVFilterContext *ctx = link->src;
267     YADIFContext *yadif = ctx->priv;
268
269     if (yadif->frame_pending) {
270         return_frame(ctx, 1);
271         return 0;
272     }
273
274     do {
275         int ret;
276
277         if (yadif->eof)
278             return AVERROR_EOF;
279
280         ret  = ff_request_frame(link->src->inputs[0]);
281
282         if (ret == AVERROR_EOF && yadif->cur) {
283             AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
284
285             if (!next)
286                 return AVERROR(ENOMEM);
287
288             next->pts = yadif->next->pts * 2 - yadif->cur->pts;
289
290             filter_frame(link->src->inputs[0], next);
291             yadif->eof = 1;
292         } else if (ret < 0) {
293             return ret;
294         }
295     } while (!yadif->cur);
296
297     return 0;
298 }
299
300 #define OFFSET(x) offsetof(YADIFContext, x)
301 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
302
303 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
304
305 static const AVOption yadif_options[] = {
306     { "mode",   "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
307     CONST("send_frame",           "send one frame for each frame",                                     YADIF_MODE_SEND_FRAME,           "mode"),
308     CONST("send_field",           "send one frame for each field",                                     YADIF_MODE_SEND_FIELD,           "mode"),
309     CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
310     CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
311
312     { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
313     CONST("tff",  "assume top field first",    YADIF_PARITY_TFF,  "parity"),
314     CONST("bff",  "assume bottom field first", YADIF_PARITY_BFF,  "parity"),
315     CONST("auto", "auto detect parity",        YADIF_PARITY_AUTO, "parity"),
316
317     { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
318     CONST("all",        "deinterlace all frames",                       YADIF_DEINT_ALL,         "deint"),
319     CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED,  "deint"),
320
321     {NULL},
322 };
323
324 AVFILTER_DEFINE_CLASS(yadif);
325
326 static av_cold void uninit(AVFilterContext *ctx)
327 {
328     YADIFContext *yadif = ctx->priv;
329
330     avfilter_unref_bufferp(&yadif->prev);
331     avfilter_unref_bufferp(&yadif->cur );
332     avfilter_unref_bufferp(&yadif->next);
333     av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
334     av_opt_free(yadif);
335 }
336
337 static int query_formats(AVFilterContext *ctx)
338 {
339     static const enum AVPixelFormat pix_fmts[] = {
340         AV_PIX_FMT_YUV420P,
341         AV_PIX_FMT_YUV422P,
342         AV_PIX_FMT_YUV444P,
343         AV_PIX_FMT_YUV410P,
344         AV_PIX_FMT_YUV411P,
345         AV_PIX_FMT_GRAY8,
346         AV_PIX_FMT_YUVJ420P,
347         AV_PIX_FMT_YUVJ422P,
348         AV_PIX_FMT_YUVJ444P,
349         AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
350         AV_PIX_FMT_YUV440P,
351         AV_PIX_FMT_YUVJ440P,
352         AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
353         AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
354         AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
355         AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
356         AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
357         AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
358         AV_PIX_FMT_YUVA420P,
359         AV_PIX_FMT_YUVA422P,
360         AV_PIX_FMT_YUVA444P,
361         AV_PIX_FMT_NONE
362     };
363
364     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
365
366     return 0;
367 }
368
369 static av_cold int init(AVFilterContext *ctx, const char *args)
370 {
371     YADIFContext *yadif = ctx->priv;
372     static const char *shorthand[] = { "mode", "parity", "enable", NULL };
373     int ret;
374
375     yadif->csp = NULL;
376
377     yadif->class = &yadif_class;
378     av_opt_set_defaults(yadif);
379
380     if ((ret = av_opt_set_from_string(yadif, args, shorthand, "=", ":")) < 0)
381         return ret;
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 deint:%d\n",
389            yadif->mode, yadif->parity, yadif->deint);
390
391     return 0;
392 }
393
394 static int config_props(AVFilterLink *link)
395 {
396     AVFilterContext *ctx = link->src;
397     YADIFContext *yadif = ctx->priv;
398
399     link->time_base.num = link->src->inputs[0]->time_base.num;
400     link->time_base.den = link->src->inputs[0]->time_base.den * 2;
401     link->w             = link->src->inputs[0]->w;
402     link->h             = link->src->inputs[0]->h;
403
404     if(yadif->mode&1)
405         link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
406
407     if (link->w < 3 || link->h < 3) {
408         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
409         return AVERROR(EINVAL);
410     }
411
412     return 0;
413 }
414
415 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
416     {
417         .name             = "default",
418         .type             = AVMEDIA_TYPE_VIDEO,
419         .filter_frame     = filter_frame,
420         .min_perms        = AV_PERM_PRESERVE,
421     },
422     { NULL }
423 };
424
425 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
426     {
427         .name          = "default",
428         .type          = AVMEDIA_TYPE_VIDEO,
429         .request_frame = request_frame,
430         .config_props  = config_props,
431     },
432     { NULL }
433 };
434
435 AVFilter avfilter_vf_yadif = {
436     .name          = "yadif",
437     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
438
439     .priv_size     = sizeof(YADIFContext),
440     .init          = init,
441     .uninit        = uninit,
442     .query_formats = query_formats,
443
444     .inputs    = avfilter_vf_yadif_inputs,
445     .outputs   = avfilter_vf_yadif_outputs,
446
447     .priv_class = &yadif_class,
448 };