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>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/avassert.h"
51 enum ScanMode scan; // top or bottom field first scanning
52 int lowpass; // enable or disable low pass filterning
53 AVFrame *cur, *next; // the two frames from which the new one is obtained
56 #define OFFSET(x) offsetof(InterlaceContext, x)
57 #define V AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption interlace_options[] = {
59 { "scan", "scanning mode", OFFSET(scan),
60 AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
61 { "tff", "top field first", 0,
62 AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
63 { "bff", "bottom field first", 0,
64 AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
65 { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
66 AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
70 AVFILTER_DEFINE_CLASS(interlace);
72 static const enum AVPixelFormat formats_supported[] = {
73 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
74 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
75 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
76 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
79 static int query_formats(AVFilterContext *ctx)
81 ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
85 static av_cold void uninit(AVFilterContext *ctx)
87 InterlaceContext *s = ctx->priv;
89 av_frame_free(&s->cur);
90 av_frame_free(&s->next);
93 static int config_out_props(AVFilterLink *outlink)
95 AVFilterContext *ctx = outlink->src;
96 AVFilterLink *inlink = outlink->src->inputs[0];
97 InterlaceContext *s = ctx->priv;
100 av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
101 return AVERROR_INVALIDDATA;
104 outlink->w = inlink->w;
105 outlink->h = inlink->h;
106 outlink->time_base = inlink->time_base;
107 outlink->frame_rate = inlink->frame_rate;
109 outlink->time_base.num *= 2;
110 outlink->frame_rate.den *= 2;
111 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
113 av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
114 s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
119 static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
120 AVFilterLink *inlink, enum FieldType field_type,
123 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
124 int vsub = desc->log2_chroma_h;
127 for (plane = 0; plane < desc->nb_components; plane++) {
128 int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
129 int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
130 uint8_t *dstp = dst_frame->data[plane];
131 const uint8_t *srcp = src_frame->data[plane];
133 av_assert0(linesize >= 0);
136 if (field_type == FIELD_LOWER)
137 srcp += src_frame->linesize[plane];
138 if (field_type == FIELD_LOWER)
139 dstp += dst_frame->linesize[plane];
141 int srcp_linesize = src_frame->linesize[plane] * 2;
142 int dstp_linesize = dst_frame->linesize[plane] * 2;
143 for (j = lines; j > 0; j--) {
144 const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
145 const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
147 srcp_above = srcp; // there is no line above
149 srcp_below = srcp; // there is no line below
150 for (i = 0; i < linesize; i++) {
151 // this calculation is an integer representation of
152 // '0.5 * current + 0.25 * above + 0.25 + below'
153 // '1 +' is for rounding.
154 dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
156 dstp += dstp_linesize;
157 srcp += srcp_linesize;
160 av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
161 srcp, src_frame->linesize[plane] * 2,
167 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
169 AVFilterContext *ctx = inlink->dst;
170 AVFilterLink *outlink = ctx->outputs[0];
171 InterlaceContext *s = ctx->priv;
175 av_frame_free(&s->cur);
179 /* we need at least two frames */
180 if (!s->cur || !s->next)
183 tff = (s->scan == MODE_TFF);
184 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
186 return AVERROR(ENOMEM);
188 av_frame_copy_props(out, s->cur);
189 out->interlaced_frame = 1;
190 out->top_field_first = tff;
191 out->pts /= 2; // adjust pts to new framerate
193 /* copy upper/lower field from cur */
194 copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
195 av_frame_free(&s->cur);
197 /* copy lower/upper field from next */
198 copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
199 av_frame_free(&s->next);
201 ret = ff_filter_frame(outlink, out);
206 static const AVFilterPad inputs[] = {
209 .type = AVMEDIA_TYPE_VIDEO,
210 .filter_frame = filter_frame,
215 static const AVFilterPad outputs[] = {
218 .type = AVMEDIA_TYPE_VIDEO,
219 .config_props = config_out_props,
224 AVFilter avfilter_vf_interlace = {
226 .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
229 .priv_class = &interlace_class,
230 .priv_size = sizeof(InterlaceContext),
231 .query_formats = query_formats,