]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_interlace.c
Merge commit 'f8f7ad758d0e1f36915467567f4d75541d98c12f'
[ffmpeg] / libavfilter / vf_interlace.c
1 /*
2  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2011 Stefano Sabatini
5  * Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.com>
6  * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 /**
26  * @file
27  * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
28  */
29
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/avassert.h"
34
35 #include "formats.h"
36 #include "avfilter.h"
37 #include "interlace.h"
38 #include "internal.h"
39 #include "video.h"
40
41 #define OFFSET(x) offsetof(InterlaceContext, x)
42 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
43 static const AVOption interlace_options[] = {
44     { "scan", "scanning mode", OFFSET(scan),
45         AV_OPT_TYPE_INT,   {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" },
46     { "tff", "top field first", 0,
47         AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
48     { "bff", "bottom field first", 0,
49         AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
50     { "lowpass", "set vertical low-pass filter", OFFSET(lowpass),
51         AV_OPT_TYPE_INT,   {.i64 = VLPF_LIN }, 0, 2, .flags = FLAGS, .unit = "lowpass" },
52     { "off",     "disable vertical low-pass filter", 0,
53         AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
54     { "linear",  "linear vertical low-pass filter",  0,
55         AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
56     { "complex", "complex vertical low-pass filter", 0,
57         AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
58     { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(interlace);
62
63 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
64                            const uint8_t *srcp,
65                            ptrdiff_t mref, ptrdiff_t pref)
66 {
67     const uint8_t *srcp_above = srcp + mref;
68     const uint8_t *srcp_below = srcp + pref;
69     int i;
70     for (i = 0; i < linesize; i++) {
71         // this calculation is an integer representation of
72         // '0.5 * current + 0.25 * above + 0.25 * below'
73         // '1 +' is for rounding.
74         dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
75     }
76 }
77
78 static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t linesize,
79                                    const uint8_t *srcp,
80                                    ptrdiff_t mref, ptrdiff_t pref)
81 {
82     const uint8_t *srcp_above = srcp + mref;
83     const uint8_t *srcp_below = srcp + pref;
84     const uint8_t *srcp_above2 = srcp + mref * 2;
85     const uint8_t *srcp_below2 = srcp + pref * 2;
86     int i;
87     for (i = 0; i < linesize; i++) {
88         // this calculation is an integer representation of
89         // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
90         // '4 +' is for rounding.
91         dstp[i] = av_clip_uint8((4 + (srcp[i] << 2)
92                   + ((srcp[i] + srcp_above[i] + srcp_below[i]) << 1)
93                   - srcp_above2[i] - srcp_below2[i]) >> 3);
94     }
95 }
96
97 static const enum AVPixelFormat formats_supported[] = {
98     AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
99     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P,
100     AV_PIX_FMT_GRAY8,    AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
101     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
102 };
103
104 static int query_formats(AVFilterContext *ctx)
105 {
106     AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
107     if (!fmts_list)
108         return AVERROR(ENOMEM);
109     return ff_set_common_formats(ctx, fmts_list);
110 }
111
112 static av_cold void uninit(AVFilterContext *ctx)
113 {
114     InterlaceContext *s = ctx->priv;
115
116     av_frame_free(&s->cur);
117     av_frame_free(&s->next);
118 }
119
120 static int config_out_props(AVFilterLink *outlink)
121 {
122     AVFilterContext *ctx = outlink->src;
123     AVFilterLink *inlink = outlink->src->inputs[0];
124     InterlaceContext *s = ctx->priv;
125
126     if (inlink->h < 2) {
127         av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
128         return AVERROR_INVALIDDATA;
129     }
130
131     if (!s->lowpass)
132         av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
133                "the resulting video will be aliased rather than interlaced.\n");
134
135     // same input size
136     outlink->w = inlink->w;
137     outlink->h = inlink->h;
138     outlink->time_base = inlink->time_base;
139     outlink->frame_rate = inlink->frame_rate;
140     // half framerate
141     outlink->time_base.num *= 2;
142     outlink->frame_rate.den *= 2;
143
144
145     if (s->lowpass) {
146         if (s->lowpass == VLPF_LIN)
147             s->lowpass_line = lowpass_line_c;
148         else if (s->lowpass == VLPF_CMP)
149             s->lowpass_line = lowpass_line_complex_c;
150         if (ARCH_X86)
151             ff_interlace_init_x86(s);
152     }
153
154     av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
155            s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
156
157     return 0;
158 }
159
160 static void copy_picture_field(InterlaceContext *s,
161                                AVFrame *src_frame, AVFrame *dst_frame,
162                                AVFilterLink *inlink, enum FieldType field_type,
163                                int lowpass)
164 {
165     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
166     int hsub = desc->log2_chroma_w;
167     int vsub = desc->log2_chroma_h;
168     int plane, j;
169
170     for (plane = 0; plane < desc->nb_components; plane++) {
171         int cols  = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
172         int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
173         uint8_t *dstp = dst_frame->data[plane];
174         const uint8_t *srcp = src_frame->data[plane];
175
176         av_assert0(cols >= 0 || lines >= 0);
177
178         lines = (lines + (field_type == FIELD_UPPER)) / 2;
179         if (field_type == FIELD_LOWER) {
180             srcp += src_frame->linesize[plane];
181             dstp += dst_frame->linesize[plane];
182         }
183         if (lowpass == VLPF_LIN) {
184             int srcp_linesize = src_frame->linesize[plane] * 2;
185             int dstp_linesize = dst_frame->linesize[plane] * 2;
186             for (j = lines; j > 0; j--) {
187                 ptrdiff_t pref = src_frame->linesize[plane];
188                 ptrdiff_t mref = -pref;
189                 if (j == lines)
190                     mref = 0;    // there is no line above
191                 else if (j == 1)
192                     pref = 0;    // there is no line below
193                 s->lowpass_line(dstp, cols, srcp, mref, pref);
194                 dstp += dstp_linesize;
195                 srcp += srcp_linesize;
196             }
197         } else if (lowpass == VLPF_CMP) {
198             int srcp_linesize = src_frame->linesize[plane] * 2;
199             int dstp_linesize = dst_frame->linesize[plane] * 2;
200             for (j = lines; j > 0; j--) {
201                 ptrdiff_t pref = src_frame->linesize[plane];
202                 ptrdiff_t mref = -pref;
203                 if (j >= (lines - 1))
204                     mref = 0;
205                 else if (j <= 2)
206                     pref = 0;
207                 s->lowpass_line(dstp, cols, srcp, mref, pref);
208                 dstp += dstp_linesize;
209                 srcp += srcp_linesize;
210             }
211         } else {
212             av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
213                                 srcp, src_frame->linesize[plane] * 2,
214                                 cols, lines);
215         }
216     }
217 }
218
219 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
220 {
221     AVFilterContext *ctx = inlink->dst;
222     AVFilterLink *outlink = ctx->outputs[0];
223     InterlaceContext *s = ctx->priv;
224     AVFrame *out;
225     int tff, ret;
226
227     av_frame_free(&s->cur);
228     s->cur  = s->next;
229     s->next = buf;
230
231     /* we need at least two frames */
232     if (!s->cur || !s->next)
233         return 0;
234
235     if (s->cur->interlaced_frame) {
236         av_log(ctx, AV_LOG_WARNING,
237                "video is already interlaced, adjusting framerate only\n");
238         out = av_frame_clone(s->cur);
239         if (!out)
240             return AVERROR(ENOMEM);
241         out->pts /= 2;  // adjust pts to new framerate
242         ret = ff_filter_frame(outlink, out);
243         return ret;
244     }
245
246     tff = (s->scan == MODE_TFF);
247     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
248     if (!out)
249         return AVERROR(ENOMEM);
250
251     av_frame_copy_props(out, s->cur);
252     out->interlaced_frame = 1;
253     out->top_field_first  = tff;
254     out->pts             /= 2;  // adjust pts to new framerate
255
256     /* copy upper/lower field from cur */
257     copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
258     av_frame_free(&s->cur);
259
260     /* copy lower/upper field from next */
261     copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
262     av_frame_free(&s->next);
263
264     ret = ff_filter_frame(outlink, out);
265
266     return ret;
267 }
268
269 static const AVFilterPad inputs[] = {
270     {
271         .name         = "default",
272         .type         = AVMEDIA_TYPE_VIDEO,
273         .filter_frame = filter_frame,
274     },
275     { NULL }
276 };
277
278 static const AVFilterPad outputs[] = {
279     {
280         .name         = "default",
281         .type         = AVMEDIA_TYPE_VIDEO,
282         .config_props = config_out_props,
283     },
284     { NULL }
285 };
286
287 AVFilter ff_vf_interlace = {
288     .name          = "interlace",
289     .description   = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
290     .uninit        = uninit,
291     .priv_class    = &interlace_class,
292     .priv_size     = sizeof(InterlaceContext),
293     .query_formats = query_formats,
294     .inputs        = inputs,
295     .outputs       = outputs,
296 };