]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_w3fdif.c
Merge commit '977f41e274a66c9d257186ca1df8373a09cc4d40'
[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;  ///< lines we are calculating
44     int nb_threads;
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     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
79     if (!fmts_list)
80         return AVERROR(ENOMEM);
81     return ff_set_common_formats(ctx, fmts_list);
82 }
83
84 static int config_input(AVFilterLink *inlink)
85 {
86     AVFilterContext *ctx = inlink->dst;
87     W3FDIFContext *s = ctx->priv;
88     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
89     int ret, i;
90
91     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
92         return ret;
93
94     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
95     s->planeheight[0] = s->planeheight[3] = inlink->h;
96
97     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
98     s->nb_threads = ctx->graph->nb_threads;
99     s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
100     if (!s->work_line)
101         return AVERROR(ENOMEM);
102
103     for (i = 0; i < s->nb_threads; i++) {
104         s->work_line[i] = av_calloc(s->linesize[0], sizeof(*s->work_line[0]));
105         if (!s->work_line[i])
106             return AVERROR(ENOMEM);
107     }
108
109     return 0;
110 }
111
112 static int config_output(AVFilterLink *outlink)
113 {
114     AVFilterLink *inlink = outlink->src->inputs[0];
115
116     outlink->time_base.num = inlink->time_base.num;
117     outlink->time_base.den = inlink->time_base.den * 2;
118     outlink->frame_rate.num = inlink->frame_rate.num * 2;
119     outlink->frame_rate.den = inlink->frame_rate.den;
120
121     return 0;
122 }
123
124 /*
125  * Filter coefficients from PH-2071, scaled by 256 * 256.
126  * Each set of coefficients has a set for low-frequencies and high-frequencies.
127  * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
128  * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
129  * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
130  * and high-frequencies for simple and more-complex mode.
131  */
132 static const int8_t   n_coef_lf[2] = { 2, 4 };
133 static const int32_t coef_lf[2][4] = {{ 32768, 32768,     0,     0},
134                                       { -1704, 34472, 34472, -1704}};
135 static const int8_t   n_coef_hf[2] = { 3, 5 };
136 static const int32_t coef_hf[2][5] = {{ -4096,  8192, -4096,     0,     0},
137                                       {  2032, -7602, 11140, -7602,  2032}};
138
139 typedef struct ThreadData {
140     AVFrame *out, *cur, *adj;
141     int plane;
142 } ThreadData;
143
144 static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
145 {
146     W3FDIFContext *s = ctx->priv;
147     ThreadData *td = arg;
148     AVFrame *out = td->out;
149     AVFrame *cur = td->cur;
150     AVFrame *adj = td->adj;
151     const int plane = td->plane;
152     const int filter = s->filter;
153     uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
154     uint8_t *out_line, *out_pixel;
155     int32_t *work_line, *work_pixel;
156     uint8_t *cur_data = cur->data[plane];
157     uint8_t *adj_data = adj->data[plane];
158     uint8_t *dst_data = out->data[plane];
159     const int linesize = s->linesize[plane];
160     const int height   = s->planeheight[plane];
161     const int cur_line_stride = cur->linesize[plane];
162     const int adj_line_stride = adj->linesize[plane];
163     const int dst_line_stride = out->linesize[plane];
164     const int start = (height * jobnr) / nb_jobs;
165     const int end = (height * (jobnr+1)) / nb_jobs;
166     int i, j, y_in, y_out;
167
168     /* copy unchanged the lines of the field */
169     y_out = start + (s->field == cur->top_field_first) - (start & 1);
170
171     in_line  = cur_data + (y_out * cur_line_stride);
172     out_line = dst_data + (y_out * dst_line_stride);
173
174     while (y_out < end) {
175         memcpy(out_line, in_line, linesize);
176         y_out += 2;
177         in_line  += cur_line_stride * 2;
178         out_line += dst_line_stride * 2;
179     }
180
181     /* interpolate other lines of the field */
182     y_out = start + (s->field != cur->top_field_first) - (start & 1);
183
184     out_line = dst_data + (y_out * dst_line_stride);
185
186     while (y_out < end) {
187         /* clear workspace */
188         memset(s->work_line[jobnr], 0, sizeof(*s->work_line[jobnr]) * linesize);
189
190         /* get low vertical frequencies from current field */
191         for (j = 0; j < n_coef_lf[filter]; j++) {
192             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
193
194             while (y_in < 0)
195                 y_in += 2;
196             while (y_in >= height)
197                 y_in -= 2;
198
199             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
200         }
201
202         work_line = s->work_line[jobnr];
203         switch (n_coef_lf[filter]) {
204         case 2:
205             for (i = 0; i < linesize; i++) {
206                 *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
207                 *work_line++ += *in_lines_cur[1]++ * coef_lf[filter][1];
208             }
209             break;
210         case 4:
211             for (i = 0; i < linesize; i++) {
212                 *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
213                 *work_line   += *in_lines_cur[1]++ * coef_lf[filter][1];
214                 *work_line   += *in_lines_cur[2]++ * coef_lf[filter][2];
215                 *work_line++ += *in_lines_cur[3]++ * coef_lf[filter][3];
216             }
217         }
218
219         /* get high vertical frequencies from adjacent fields */
220         for (j = 0; j < n_coef_hf[filter]; j++) {
221             y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
222
223             while (y_in < 0)
224                 y_in += 2;
225             while (y_in >= height)
226                 y_in -= 2;
227
228             in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
229             in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
230         }
231
232         work_line = s->work_line[jobnr];
233         switch (n_coef_hf[filter]) {
234         case 3:
235             for (i = 0; i < linesize; i++) {
236                 *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
237                 *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
238                 *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
239                 *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
240                 *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
241                 *work_line++ += *in_lines_adj[2]++ * coef_hf[filter][2];
242             }
243             break;
244         case 5:
245             for (i = 0; i < linesize; i++) {
246                 *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
247                 *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
248                 *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
249                 *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
250                 *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
251                 *work_line   += *in_lines_adj[2]++ * coef_hf[filter][2];
252                 *work_line   += *in_lines_cur[3]++ * coef_hf[filter][3];
253                 *work_line   += *in_lines_adj[3]++ * coef_hf[filter][3];
254                 *work_line   += *in_lines_cur[4]++ * coef_hf[filter][4];
255                 *work_line++ += *in_lines_adj[4]++ * coef_hf[filter][4];
256             }
257         }
258
259         /* save scaled result to the output frame, scaling down by 256 * 256 */
260         work_pixel = s->work_line[jobnr];
261         out_pixel = out_line;
262
263         for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
264              *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 256) >> 16;
265
266         /* move on to next line */
267         y_out += 2;
268         out_line += dst_line_stride * 2;
269     }
270
271     return 0;
272 }
273
274 static int filter(AVFilterContext *ctx, int is_second)
275 {
276     W3FDIFContext *s = ctx->priv;
277     AVFilterLink *outlink = ctx->outputs[0];
278     AVFrame *out, *adj;
279     ThreadData td;
280     int plane;
281
282     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
283     if (!out)
284         return AVERROR(ENOMEM);
285     av_frame_copy_props(out, s->cur);
286     out->interlaced_frame = 0;
287
288     if (!is_second) {
289         if (out->pts != AV_NOPTS_VALUE)
290             out->pts *= 2;
291     } else {
292         int64_t cur_pts  = s->cur->pts;
293         int64_t next_pts = s->next->pts;
294
295         if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
296             out->pts = cur_pts + next_pts;
297         } else {
298             out->pts = AV_NOPTS_VALUE;
299         }
300     }
301
302     adj = s->field ? s->next : s->prev;
303     td.out = out; td.cur = s->cur; td.adj = adj;
304     for (plane = 0; plane < s->nb_planes; plane++) {
305         td.plane = plane;
306         ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
307     }
308
309     s->field = !s->field;
310
311     return ff_filter_frame(outlink, out);
312 }
313
314 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
315 {
316     AVFilterContext *ctx = inlink->dst;
317     W3FDIFContext *s = ctx->priv;
318     int ret;
319
320     av_frame_free(&s->prev);
321     s->prev = s->cur;
322     s->cur  = s->next;
323     s->next = frame;
324
325     if (!s->cur) {
326         s->cur = av_frame_clone(s->next);
327         if (!s->cur)
328             return AVERROR(ENOMEM);
329     }
330
331     if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
332         AVFrame *out = av_frame_clone(s->cur);
333         if (!out)
334             return AVERROR(ENOMEM);
335
336         av_frame_free(&s->prev);
337         if (out->pts != AV_NOPTS_VALUE)
338             out->pts *= 2;
339         return ff_filter_frame(ctx->outputs[0], out);
340     }
341
342     if (!s->prev)
343         return 0;
344
345     ret = filter(ctx, 0);
346     if (ret < 0)
347         return ret;
348
349     return filter(ctx, 1);
350 }
351
352 static int request_frame(AVFilterLink *outlink)
353 {
354     AVFilterContext *ctx = outlink->src;
355     W3FDIFContext *s = ctx->priv;
356
357     do {
358         int ret;
359
360         if (s->eof)
361             return AVERROR_EOF;
362
363         ret = ff_request_frame(ctx->inputs[0]);
364
365         if (ret == AVERROR_EOF && s->cur) {
366             AVFrame *next = av_frame_clone(s->next);
367             if (!next)
368                 return AVERROR(ENOMEM);
369             next->pts = s->next->pts * 2 - s->cur->pts;
370             filter_frame(ctx->inputs[0], next);
371             s->eof = 1;
372         } else if (ret < 0) {
373             return ret;
374         }
375     } while (!s->cur);
376
377     return 0;
378 }
379
380 static av_cold void uninit(AVFilterContext *ctx)
381 {
382     W3FDIFContext *s = ctx->priv;
383     int i;
384
385     av_frame_free(&s->prev);
386     av_frame_free(&s->cur );
387     av_frame_free(&s->next);
388
389     for (i = 0; i < s->nb_threads; i++)
390         av_freep(&s->work_line[i]);
391
392     av_freep(&s->work_line);
393 }
394
395 static const AVFilterPad w3fdif_inputs[] = {
396     {
397         .name          = "default",
398         .type          = AVMEDIA_TYPE_VIDEO,
399         .filter_frame  = filter_frame,
400         .config_props  = config_input,
401     },
402     { NULL }
403 };
404
405 static const AVFilterPad w3fdif_outputs[] = {
406     {
407         .name          = "default",
408         .type          = AVMEDIA_TYPE_VIDEO,
409         .config_props  = config_output,
410         .request_frame = request_frame,
411     },
412     { NULL }
413 };
414
415 AVFilter ff_vf_w3fdif = {
416     .name          = "w3fdif",
417     .description   = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
418     .priv_size     = sizeof(W3FDIFContext),
419     .priv_class    = &w3fdif_class,
420     .uninit        = uninit,
421     .query_formats = query_formats,
422     .inputs        = w3fdif_inputs,
423     .outputs       = w3fdif_outputs,
424     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
425 };