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>
8 * This file is part of FFmpeg.
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.
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.
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.
27 * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/avassert.h"
37 #include "interlace.h"
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" },
61 AVFILTER_DEFINE_CLASS(interlace);
63 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
64 const uint8_t *srcp, ptrdiff_t mref,
65 ptrdiff_t pref, int clip_max)
67 const uint8_t *srcp_above = srcp + mref;
68 const uint8_t *srcp_below = srcp + pref;
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;
78 static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t linesize,
79 const uint8_t *src8, ptrdiff_t mref,
80 ptrdiff_t pref, int clip_max)
82 uint16_t *dstp = (uint16_t *)dst8;
83 const uint16_t *srcp = (const uint16_t *)src8;
84 const uint16_t *srcp_above = srcp + mref / 2;
85 const uint16_t *srcp_below = srcp + pref / 2;
87 for (i = 0; i < linesize; i++) {
88 // this calculation is an integer representation of
89 // '0.5 * current + 0.25 * above + 0.25 * below'
90 // '1 +' is for rounding.
91 src_x = av_le2ne16(srcp[i]) << 1;
92 dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
93 + av_le2ne16(srcp_below[i])) >> 2);
97 static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t linesize,
98 const uint8_t *srcp, ptrdiff_t mref,
99 ptrdiff_t pref, int clip_max)
101 const uint8_t *srcp_above = srcp + mref;
102 const uint8_t *srcp_below = srcp + pref;
103 const uint8_t *srcp_above2 = srcp + mref * 2;
104 const uint8_t *srcp_below2 = srcp + pref * 2;
105 int i, src_x, src_ab;
106 for (i = 0; i < linesize; i++) {
107 // this calculation is an integer representation of
108 // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
109 // '4 +' is for rounding.
110 src_x = srcp[i] << 1;
111 src_ab = srcp_above[i] + srcp_below[i];
112 dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1)
113 - srcp_above2[i] - srcp_below2[i]) >> 3);
114 // Prevent over-sharpening:
115 // dst must not exceed src when the average of above and below
116 // is less than src. And the other way around.
117 if (src_ab > src_x) {
118 if (dstp[i] < srcp[i])
120 } else if (dstp[i] > srcp[i])
125 static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t linesize,
126 const uint8_t *src8, ptrdiff_t mref,
127 ptrdiff_t pref, int clip_max)
129 uint16_t *dstp = (uint16_t *)dst8;
130 const uint16_t *srcp = (const uint16_t *)src8;
131 const uint16_t *srcp_above = srcp + mref / 2;
132 const uint16_t *srcp_below = srcp + pref / 2;
133 const uint16_t *srcp_above2 = srcp + mref;
134 const uint16_t *srcp_below2 = srcp + pref;
135 int i, dst_le, src_le, src_x, src_ab;
136 for (i = 0; i < linesize; i++) {
137 // this calculation is an integer representation of
138 // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
139 // '4 +' is for rounding.
140 src_le = av_le2ne16(srcp[i]);
142 src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
143 dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
144 - av_le2ne16(srcp_above2[i])
145 - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
146 // Prevent over-sharpening:
147 // dst must not exceed src when the average of above and below
148 // is less than src. And the other way around.
149 if (src_ab > src_x) {
151 dstp[i] = av_le2ne16(src_le);
153 dstp[i] = av_le2ne16(dst_le);
154 } else if (dst_le > src_le) {
155 dstp[i] = av_le2ne16(src_le);
157 dstp[i] = av_le2ne16(dst_le);
161 static const enum AVPixelFormat formats_supported[] = {
162 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
163 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
164 AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV444P10LE,
165 AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE, AV_PIX_FMT_YUV444P12LE,
166 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
167 AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
168 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
169 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
172 static int query_formats(AVFilterContext *ctx)
174 AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
176 return AVERROR(ENOMEM);
177 return ff_set_common_formats(ctx, fmts_list);
180 static av_cold void uninit(AVFilterContext *ctx)
182 InterlaceContext *s = ctx->priv;
184 av_frame_free(&s->cur);
185 av_frame_free(&s->next);
188 void ff_interlace_init(InterlaceContext *s, int depth)
191 if (s->lowpass == VLPF_LIN) {
193 s->lowpass_line = lowpass_line_c_16;
195 s->lowpass_line = lowpass_line_c;
196 } else if (s->lowpass == VLPF_CMP) {
198 s->lowpass_line = lowpass_line_complex_c_16;
200 s->lowpass_line = lowpass_line_complex_c;
203 ff_interlace_init_x86(s, depth);
207 static int config_out_props(AVFilterLink *outlink)
209 AVFilterContext *ctx = outlink->src;
210 AVFilterLink *inlink = outlink->src->inputs[0];
211 InterlaceContext *s = ctx->priv;
214 av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
215 return AVERROR_INVALIDDATA;
219 av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
220 "the resulting video will be aliased rather than interlaced.\n");
223 outlink->w = inlink->w;
224 outlink->h = inlink->h;
225 outlink->time_base = inlink->time_base;
226 outlink->frame_rate = inlink->frame_rate;
228 outlink->time_base.num *= 2;
229 outlink->frame_rate.den *= 2;
231 s->csp = av_pix_fmt_desc_get(outlink->format);
232 ff_interlace_init(s, s->csp->comp[0].depth);
234 av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
235 s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
240 static void copy_picture_field(InterlaceContext *s,
241 AVFrame *src_frame, AVFrame *dst_frame,
242 AVFilterLink *inlink, enum FieldType field_type,
245 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
246 int hsub = desc->log2_chroma_w;
247 int vsub = desc->log2_chroma_h;
250 for (plane = 0; plane < desc->nb_components; plane++) {
251 int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
252 int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
253 uint8_t *dstp = dst_frame->data[plane];
254 const uint8_t *srcp = src_frame->data[plane];
255 int srcp_linesize = src_frame->linesize[plane] * 2;
256 int dstp_linesize = dst_frame->linesize[plane] * 2;
257 int clip_max = (1 << s->csp->comp[plane].depth) - 1;
259 av_assert0(cols >= 0 || lines >= 0);
261 lines = (lines + (field_type == FIELD_UPPER)) / 2;
262 if (field_type == FIELD_LOWER) {
263 srcp += src_frame->linesize[plane];
264 dstp += dst_frame->linesize[plane];
268 if (lowpass == VLPF_CMP)
270 for (j = lines; j > 0; j--) {
271 ptrdiff_t pref = src_frame->linesize[plane];
272 ptrdiff_t mref = -pref;
273 if (j >= (lines - x))
275 else if (j <= (1 + x))
277 s->lowpass_line(dstp, cols, srcp, mref, pref, clip_max);
278 dstp += dstp_linesize;
279 srcp += srcp_linesize;
282 if (s->csp->comp[plane].depth > 8)
284 av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
289 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
291 AVFilterContext *ctx = inlink->dst;
292 AVFilterLink *outlink = ctx->outputs[0];
293 InterlaceContext *s = ctx->priv;
297 av_frame_free(&s->cur);
301 /* we need at least two frames */
302 if (!s->cur || !s->next)
305 if (s->cur->interlaced_frame) {
306 av_log(ctx, AV_LOG_WARNING,
307 "video is already interlaced, adjusting framerate only\n");
308 out = av_frame_clone(s->cur);
310 return AVERROR(ENOMEM);
311 out->pts /= 2; // adjust pts to new framerate
312 ret = ff_filter_frame(outlink, out);
316 tff = (s->scan == MODE_TFF);
317 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
319 return AVERROR(ENOMEM);
321 av_frame_copy_props(out, s->cur);
322 out->interlaced_frame = 1;
323 out->top_field_first = tff;
324 out->pts /= 2; // adjust pts to new framerate
326 /* copy upper/lower field from cur */
327 copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
328 av_frame_free(&s->cur);
330 /* copy lower/upper field from next */
331 copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
332 av_frame_free(&s->next);
334 ret = ff_filter_frame(outlink, out);
339 static const AVFilterPad inputs[] = {
342 .type = AVMEDIA_TYPE_VIDEO,
343 .filter_frame = filter_frame,
348 static const AVFilterPad outputs[] = {
351 .type = AVMEDIA_TYPE_VIDEO,
352 .config_props = config_out_props,
357 AVFilter ff_vf_interlace = {
359 .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
361 .priv_class = &interlace_class,
362 .priv_size = sizeof(InterlaceContext),
363 .query_formats = query_formats,