]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_w3fdif.c
Merge commit '93370d12164236d59645314871a1d6808b2a8ddb'
[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     double ts_unit;
43     AVFrame *prev, *cur, *next;  ///< previous, current, next frames
44     int32_t *work_line;   ///< line we are calculating
45 } W3FDIFContext;
46
47 #define OFFSET(x) offsetof(W3FDIFContext, x)
48 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
50
51 static const AVOption w3fdif_options[] = {
52     { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
53     CONST("simple",  NULL, 0, "filter"),
54     CONST("complex", NULL, 1, "filter"),
55     { "deint",  "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
56     CONST("all",        "deinterlace all frames",                       0, "deint"),
57     CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
58     { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(w3fdif);
62
63 static int query_formats(AVFilterContext *ctx)
64 {
65     static const enum AVPixelFormat pix_fmts[] = {
66         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
67         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
68         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
69         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
70         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
71         AV_PIX_FMT_YUVJ411P,
72         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
73         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
74         AV_PIX_FMT_GRAY8,
75         AV_PIX_FMT_NONE
76     };
77
78     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
79
80     return 0;
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     W3FDIFContext *s = outlink->src->priv;
107
108     outlink->time_base.num = inlink->time_base.num;
109     outlink->time_base.den = inlink->time_base.den * 2;
110     outlink->frame_rate.num = inlink->frame_rate.num * 2;
111     outlink->frame_rate.den = inlink->frame_rate.den;
112     outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
113     s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
114
115     return 0;
116 }
117
118 /*
119  * Filter coefficients from PH-2071, scaled by 256 * 256.
120  * Each set of coefficients has a set for low-frequencies and high-frequencies.
121  * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
122  * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
123  * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
124  * and high-frequencies for simple and more-complex mode.
125  */
126 static const int8_t   n_coef_lf[2] = { 2, 4 };
127 static const int32_t coef_lf[2][4] = {{ 32768, 32768,     0,     0},
128                                       { -1704, 34472, 34472, -1704}};
129 static const int8_t   n_coef_hf[2] = { 3, 5 };
130 static const int32_t coef_hf[2][5] = {{ -4096,  8192, -4096,     0,     0},
131                                       {  2032, -7602, 11140, -7602,  2032}};
132
133 static void deinterlace_plane(AVFilterContext *ctx, AVFrame *out,
134                               const AVFrame *cur, const AVFrame *adj,
135                               const int filter, const int plane)
136 {
137     W3FDIFContext *s = ctx->priv;
138     uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
139     uint8_t *out_line, *out_pixel;
140     int32_t *work_line, *work_pixel;
141     uint8_t *cur_data = cur->data[plane];
142     uint8_t *adj_data = adj->data[plane];
143     uint8_t *dst_data = out->data[plane];
144     const int linesize = s->linesize[plane];
145     const int height   = s->planeheight[plane];
146     const int cur_line_stride = cur->linesize[plane];
147     const int adj_line_stride = adj->linesize[plane];
148     const int dst_line_stride = out->linesize[plane];
149     int i, j, y_in, y_out;
150
151     /* copy unchanged the lines of the field */
152     y_out = s->field == cur->top_field_first;
153
154     in_line  = cur_data + (y_out * cur_line_stride);
155     out_line = dst_data + (y_out * dst_line_stride);
156
157     while (y_out < height) {
158         memcpy(out_line, in_line, linesize);
159         y_out += 2;
160         in_line  += cur_line_stride * 2;
161         out_line += dst_line_stride * 2;
162     }
163
164     /* interpolate other lines of the field */
165     y_out = s->field != cur->top_field_first;
166
167     out_line = dst_data + (y_out * dst_line_stride);
168
169     while (y_out < height) {
170         /* clear workspace */
171         memset(s->work_line, 0, sizeof(*s->work_line) * linesize);
172
173         /* get low vertical frequencies from current field */
174         for (j = 0; j < n_coef_lf[filter]; j++) {
175             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
176
177             while (y_in < 0)
178                 y_in += 2;
179             while (y_in >= height)
180                 y_in -= 2;
181
182             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
183         }
184
185         work_line = s->work_line;
186         switch (n_coef_lf[filter]) {
187         case 2:
188             for (i = 0; i < linesize; i++) {
189                 *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
190                 *work_line++ += *in_lines_cur[1]++ * coef_lf[filter][1];
191             }
192             break;
193         case 4:
194             for (i = 0; i < linesize; i++) {
195                 *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
196                 *work_line   += *in_lines_cur[1]++ * coef_lf[filter][1];
197                 *work_line   += *in_lines_cur[2]++ * coef_lf[filter][2];
198                 *work_line++ += *in_lines_cur[3]++ * coef_lf[filter][3];
199             }
200         }
201
202         /* get high vertical frequencies from adjacent fields */
203         for (j = 0; j < n_coef_hf[filter]; j++) {
204             y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
205
206             while (y_in < 0)
207                 y_in += 2;
208             while (y_in >= height)
209                 y_in -= 2;
210
211             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
212             in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
213         }
214
215         work_line = s->work_line;
216         switch (n_coef_hf[filter]) {
217         case 3:
218             for (i = 0; i < linesize; i++) {
219                 *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
220                 *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
221                 *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
222                 *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
223                 *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
224                 *work_line++ += *in_lines_adj[2]++ * coef_hf[filter][2];
225             }
226             break;
227         case 5:
228             for (i = 0; i < linesize; i++) {
229                 *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
230                 *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
231                 *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
232                 *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
233                 *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
234                 *work_line   += *in_lines_adj[2]++ * coef_hf[filter][2];
235                 *work_line   += *in_lines_cur[3]++ * coef_hf[filter][3];
236                 *work_line   += *in_lines_adj[3]++ * coef_hf[filter][3];
237                 *work_line   += *in_lines_cur[4]++ * coef_hf[filter][4];
238                 *work_line++ += *in_lines_adj[4]++ * coef_hf[filter][4];
239             }
240         }
241
242         /* save scaled result to the output frame, scaling down by 256 * 256 */
243         work_pixel = s->work_line;
244         out_pixel = out_line;
245
246         for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
247              *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 256) >> 16;
248
249         /* move on to next line */
250         y_out += 2;
251         out_line += dst_line_stride * 2;
252     }
253 }
254
255 static int filter(AVFilterContext *ctx)
256 {
257     W3FDIFContext *s = ctx->priv;
258     AVFilterLink *outlink = ctx->outputs[0];
259     AVFrame *out, *adj;
260     int plane;
261
262     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
263     if (!out)
264         return AVERROR(ENOMEM);
265     av_frame_copy_props(out, s->cur);
266     out->interlaced_frame = 0;
267     out->pts = outlink->frame_count * s->ts_unit;
268
269     adj = s->field ? s->next : s->prev;
270     for (plane = 0; plane < s->nb_planes; plane++)
271         deinterlace_plane(ctx, out, s->cur, adj, s->filter, plane);
272
273     s->field = !s->field;
274
275     return ff_filter_frame(outlink, out);
276 }
277
278 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
279 {
280     AVFilterContext *ctx = inlink->dst;
281     W3FDIFContext *s = ctx->priv;
282     int ret;
283
284     av_frame_free(&s->prev);
285     s->prev = s->cur;
286     s->cur  = s->next;
287     s->next = frame;
288
289     if (!s->cur) {
290         s->cur = av_frame_clone(s->next);
291         if (!s->cur)
292             return AVERROR(ENOMEM);
293     }
294
295     if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
296         AVFrame *out = av_frame_clone(s->cur);
297         if (!out)
298             return AVERROR(ENOMEM);
299
300         av_frame_free(&s->prev);
301         out->pts = ctx->outputs[0]->frame_count * s->ts_unit;
302         return ff_filter_frame(ctx->outputs[0], out);
303     }
304
305     if (!s->prev)
306         return 0;
307
308     ret = filter(ctx);
309     if (ret < 0)
310         return ret;
311
312     return filter(ctx);
313 }
314
315 static int request_frame(AVFilterLink *outlink)
316 {
317     AVFilterContext *ctx = outlink->src;
318     W3FDIFContext *s = ctx->priv;
319
320     do {
321         int ret;
322
323         if (s->eof)
324             return AVERROR_EOF;
325
326         ret = ff_request_frame(ctx->inputs[0]);
327
328         if (ret == AVERROR_EOF && s->cur) {
329             AVFrame *next = av_frame_clone(s->next);
330             if (!next)
331                 return AVERROR(ENOMEM);
332             filter_frame(ctx->inputs[0], next);
333             s->eof = 1;
334         } else if (ret < 0) {
335             return ret;
336         }
337     } while (!s->cur);
338
339     return 0;
340 }
341
342 static av_cold void uninit(AVFilterContext *ctx)
343 {
344     W3FDIFContext *s = ctx->priv;
345
346     av_frame_free(&s->prev);
347     av_frame_free(&s->cur );
348     av_frame_free(&s->next);
349     av_freep(&s->work_line);
350 }
351
352 static const AVFilterPad w3fdif_inputs[] = {
353     {
354         .name          = "default",
355         .type          = AVMEDIA_TYPE_VIDEO,
356         .filter_frame  = filter_frame,
357         .config_props  = config_input,
358     },
359     { NULL }
360 };
361
362 static const AVFilterPad w3fdif_outputs[] = {
363     {
364         .name          = "default",
365         .type          = AVMEDIA_TYPE_VIDEO,
366         .config_props  = config_output,
367         .request_frame = request_frame,
368     },
369     { NULL }
370 };
371
372 AVFilter avfilter_vf_w3fdif = {
373     .name          = "w3fdif",
374     .description   = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
375     .priv_size     = sizeof(W3FDIFContext),
376     .priv_class    = &w3fdif_class,
377     .uninit        = uninit,
378     .query_formats = query_formats,
379     .inputs        = w3fdif_inputs,
380     .outputs       = w3fdif_outputs,
381     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
382 };