]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_w3fdif.c
Merge commit 'c3418201307c6b7a2b3e3be10f33ab4a20a32c3b'
[ffmpeg] / libavfilter / vf_w3fdif.c
1 /*
2  * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
3  * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
4  * Based on the process described by Martin Weston for BBC R&D
5  * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "libavutil/common.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32
33 typedef struct W3FDIFContext {
34     const AVClass *class;
35     int filter;           ///< 0 is simple, 1 is more complex
36     int deint;            ///< which frames to deinterlace
37     int linesize[4];      ///< bytes of pixel data per line for each plane
38     int planeheight[4];   ///< height of each plane
39     int field;            ///< which field are we on, 0 or 1
40     int eof;
41     int nb_planes;
42     AVFrame *prev, *cur, *next;  ///< previous, current, next frames
43     int32_t *work_line;   ///< line we are calculating
44 } W3FDIFContext;
45
46 #define OFFSET(x) offsetof(W3FDIFContext, x)
47 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
49
50 static const AVOption w3fdif_options[] = {
51     { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
52     CONST("simple",  NULL, 0, "filter"),
53     CONST("complex", NULL, 1, "filter"),
54     { "deint",  "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
55     CONST("all",        "deinterlace all frames",                       0, "deint"),
56     CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
57     { NULL }
58 };
59
60 AVFILTER_DEFINE_CLASS(w3fdif);
61
62 static int query_formats(AVFilterContext *ctx)
63 {
64     static const enum AVPixelFormat pix_fmts[] = {
65         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
66         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
67         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
68         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
69         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
70         AV_PIX_FMT_YUVJ411P,
71         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
72         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
73         AV_PIX_FMT_GRAY8,
74         AV_PIX_FMT_NONE
75     };
76
77     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
78     if (!fmts_list)
79         return AVERROR(ENOMEM);
80     return ff_set_common_formats(ctx, fmts_list);
81 }
82
83 static int config_input(AVFilterLink *inlink)
84 {
85     W3FDIFContext *s = inlink->dst->priv;
86     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
87     int ret;
88
89     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
90         return ret;
91
92     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
93     s->planeheight[0] = s->planeheight[3] = inlink->h;
94
95     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
96     s->work_line = av_calloc(s->linesize[0], sizeof(*s->work_line));
97     if (!s->work_line)
98         return AVERROR(ENOMEM);
99
100     return 0;
101 }
102
103 static int config_output(AVFilterLink *outlink)
104 {
105     AVFilterLink *inlink = outlink->src->inputs[0];
106
107     outlink->time_base.num = inlink->time_base.num;
108     outlink->time_base.den = inlink->time_base.den * 2;
109     outlink->frame_rate.num = inlink->frame_rate.num * 2;
110     outlink->frame_rate.den = inlink->frame_rate.den;
111     outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
112
113     return 0;
114 }
115
116 /*
117  * Filter coefficients from PH-2071, scaled by 256 * 256.
118  * Each set of coefficients has a set for low-frequencies and high-frequencies.
119  * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
120  * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
121  * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
122  * and high-frequencies for simple and more-complex mode.
123  */
124 static const int8_t   n_coef_lf[2] = { 2, 4 };
125 static const int32_t coef_lf[2][4] = {{ 32768, 32768,     0,     0},
126                                       { -1704, 34472, 34472, -1704}};
127 static const int8_t   n_coef_hf[2] = { 3, 5 };
128 static const int32_t coef_hf[2][5] = {{ -4096,  8192, -4096,     0,     0},
129                                       {  2032, -7602, 11140, -7602,  2032}};
130
131 static void deinterlace_plane(AVFilterContext *ctx, AVFrame *out,
132                               const AVFrame *cur, const AVFrame *adj,
133                               const int filter, const int plane)
134 {
135     W3FDIFContext *s = ctx->priv;
136     uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
137     uint8_t *out_line, *out_pixel;
138     int32_t *work_line, *work_pixel;
139     uint8_t *cur_data = cur->data[plane];
140     uint8_t *adj_data = adj->data[plane];
141     uint8_t *dst_data = out->data[plane];
142     const int linesize = s->linesize[plane];
143     const int height   = s->planeheight[plane];
144     const int cur_line_stride = cur->linesize[plane];
145     const int adj_line_stride = adj->linesize[plane];
146     const int dst_line_stride = out->linesize[plane];
147     int i, j, y_in, y_out;
148
149     /* copy unchanged the lines of the field */
150     y_out = s->field == cur->top_field_first;
151
152     in_line  = cur_data + (y_out * cur_line_stride);
153     out_line = dst_data + (y_out * dst_line_stride);
154
155     while (y_out < height) {
156         memcpy(out_line, in_line, linesize);
157         y_out += 2;
158         in_line  += cur_line_stride * 2;
159         out_line += dst_line_stride * 2;
160     }
161
162     /* interpolate other lines of the field */
163     y_out = s->field != cur->top_field_first;
164
165     out_line = dst_data + (y_out * dst_line_stride);
166
167     while (y_out < height) {
168         /* clear workspace */
169         memset(s->work_line, 0, sizeof(*s->work_line) * linesize);
170
171         /* get low vertical frequencies from current field */
172         for (j = 0; j < n_coef_lf[filter]; j++) {
173             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
174
175             while (y_in < 0)
176                 y_in += 2;
177             while (y_in >= height)
178                 y_in -= 2;
179
180             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
181         }
182
183         work_line = s->work_line;
184         switch (n_coef_lf[filter]) {
185         case 2:
186             for (i = 0; i < linesize; i++) {
187                 *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
188                 *work_line++ += *in_lines_cur[1]++ * coef_lf[filter][1];
189             }
190             break;
191         case 4:
192             for (i = 0; i < linesize; i++) {
193                 *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
194                 *work_line   += *in_lines_cur[1]++ * coef_lf[filter][1];
195                 *work_line   += *in_lines_cur[2]++ * coef_lf[filter][2];
196                 *work_line++ += *in_lines_cur[3]++ * coef_lf[filter][3];
197             }
198         }
199
200         /* get high vertical frequencies from adjacent fields */
201         for (j = 0; j < n_coef_hf[filter]; j++) {
202             y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
203
204             while (y_in < 0)
205                 y_in += 2;
206             while (y_in >= height)
207                 y_in -= 2;
208
209             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
210             in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
211         }
212
213         work_line = s->work_line;
214         switch (n_coef_hf[filter]) {
215         case 3:
216             for (i = 0; i < linesize; i++) {
217                 *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
218                 *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
219                 *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
220                 *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
221                 *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
222                 *work_line++ += *in_lines_adj[2]++ * coef_hf[filter][2];
223             }
224             break;
225         case 5:
226             for (i = 0; i < linesize; i++) {
227                 *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
228                 *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
229                 *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
230                 *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
231                 *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
232                 *work_line   += *in_lines_adj[2]++ * coef_hf[filter][2];
233                 *work_line   += *in_lines_cur[3]++ * coef_hf[filter][3];
234                 *work_line   += *in_lines_adj[3]++ * coef_hf[filter][3];
235                 *work_line   += *in_lines_cur[4]++ * coef_hf[filter][4];
236                 *work_line++ += *in_lines_adj[4]++ * coef_hf[filter][4];
237             }
238         }
239
240         /* save scaled result to the output frame, scaling down by 256 * 256 */
241         work_pixel = s->work_line;
242         out_pixel = out_line;
243
244         for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
245              *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 256) >> 16;
246
247         /* move on to next line */
248         y_out += 2;
249         out_line += dst_line_stride * 2;
250     }
251 }
252
253 static int filter(AVFilterContext *ctx, int is_second)
254 {
255     W3FDIFContext *s = ctx->priv;
256     AVFilterLink *outlink = ctx->outputs[0];
257     AVFrame *out, *adj;
258     int plane;
259
260     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
261     if (!out)
262         return AVERROR(ENOMEM);
263     av_frame_copy_props(out, s->cur);
264     out->interlaced_frame = 0;
265
266     if (!is_second) {
267         if (out->pts != AV_NOPTS_VALUE)
268             out->pts *= 2;
269     } else {
270         int64_t cur_pts  = s->cur->pts;
271         int64_t next_pts = s->next->pts;
272
273         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
274             out->pts = cur_pts + next_pts;
275         } else {
276             out->pts = AV_NOPTS_VALUE;
277         }
278     }
279
280     adj = s->field ? s->next : s->prev;
281     for (plane = 0; plane < s->nb_planes; plane++)
282         deinterlace_plane(ctx, out, s->cur, adj, s->filter, plane);
283
284     s->field = !s->field;
285
286     return ff_filter_frame(outlink, out);
287 }
288
289 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
290 {
291     AVFilterContext *ctx = inlink->dst;
292     W3FDIFContext *s = ctx->priv;
293     int ret;
294
295     av_frame_free(&s->prev);
296     s->prev = s->cur;
297     s->cur  = s->next;
298     s->next = frame;
299
300     if (!s->cur) {
301         s->cur = av_frame_clone(s->next);
302         if (!s->cur)
303             return AVERROR(ENOMEM);
304     }
305
306     if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
307         AVFrame *out = av_frame_clone(s->cur);
308         if (!out)
309             return AVERROR(ENOMEM);
310
311         av_frame_free(&s->prev);
312         if (out->pts != AV_NOPTS_VALUE)
313             out->pts *= 2;
314         return ff_filter_frame(ctx->outputs[0], out);
315     }
316
317     if (!s->prev)
318         return 0;
319
320     ret = filter(ctx, 0);
321     if (ret < 0)
322         return ret;
323
324     return filter(ctx, 1);
325 }
326
327 static int request_frame(AVFilterLink *outlink)
328 {
329     AVFilterContext *ctx = outlink->src;
330     W3FDIFContext *s = ctx->priv;
331
332     do {
333         int ret;
334
335         if (s->eof)
336             return AVERROR_EOF;
337
338         ret = ff_request_frame(ctx->inputs[0]);
339
340         if (ret == AVERROR_EOF && s->cur) {
341             AVFrame *next = av_frame_clone(s->next);
342             if (!next)
343                 return AVERROR(ENOMEM);
344             next->pts = s->next->pts * 2 - s->cur->pts;
345             filter_frame(ctx->inputs[0], next);
346             s->eof = 1;
347         } else if (ret < 0) {
348             return ret;
349         }
350     } while (!s->cur);
351
352     return 0;
353 }
354
355 static av_cold void uninit(AVFilterContext *ctx)
356 {
357     W3FDIFContext *s = ctx->priv;
358
359     av_frame_free(&s->prev);
360     av_frame_free(&s->cur );
361     av_frame_free(&s->next);
362     av_freep(&s->work_line);
363 }
364
365 static const AVFilterPad w3fdif_inputs[] = {
366     {
367         .name          = "default",
368         .type          = AVMEDIA_TYPE_VIDEO,
369         .filter_frame  = filter_frame,
370         .config_props  = config_input,
371     },
372     { NULL }
373 };
374
375 static const AVFilterPad w3fdif_outputs[] = {
376     {
377         .name          = "default",
378         .type          = AVMEDIA_TYPE_VIDEO,
379         .config_props  = config_output,
380         .request_frame = request_frame,
381     },
382     { NULL }
383 };
384
385 AVFilter ff_vf_w3fdif = {
386     .name          = "w3fdif",
387     .description   = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
388     .priv_size     = sizeof(W3FDIFContext),
389     .priv_class    = &w3fdif_class,
390     .uninit        = uninit,
391     .query_formats = query_formats,
392     .inputs        = w3fdif_inputs,
393     .outputs       = w3fdif_outputs,
394     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
395 };