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"
36 #include "interlace.h"
40 #define OFFSET(x) offsetof(InterlaceContext, x)
41 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
42 static const AVOption interlace_options[] = {
43 { "scan", "scanning mode", OFFSET(scan),
44 AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" },
45 { "tff", "top field first", 0,
46 AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
47 { "bff", "bottom field first", 0,
48 AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
49 { "lowpass", "set vertical low-pass filter", OFFSET(lowpass),
50 AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, .flags = FLAGS },
54 AVFILTER_DEFINE_CLASS(interlace);
56 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
58 ptrdiff_t mref, ptrdiff_t pref)
60 const uint8_t *srcp_above = srcp + mref;
61 const uint8_t *srcp_below = srcp + pref;
63 for (i = 0; i < linesize; i++) {
64 // this calculation is an integer representation of
65 // '0.5 * current + 0.25 * above + 0.25 * below'
66 // '1 +' is for rounding.
67 dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
71 static const enum AVPixelFormat formats_supported[] = {
72 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
73 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
74 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
75 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
78 static int query_formats(AVFilterContext *ctx)
80 AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
82 return AVERROR(ENOMEM);
83 return ff_set_common_formats(ctx, fmts_list);
86 static av_cold void uninit(AVFilterContext *ctx)
88 InterlaceContext *s = ctx->priv;
90 av_frame_free(&s->cur);
91 av_frame_free(&s->next);
94 static int config_out_props(AVFilterLink *outlink)
96 AVFilterContext *ctx = outlink->src;
97 AVFilterLink *inlink = outlink->src->inputs[0];
98 InterlaceContext *s = ctx->priv;
101 av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
102 return AVERROR_INVALIDDATA;
106 av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
107 "the resulting video will be aliased rather than interlaced.\n");
110 outlink->w = inlink->w;
111 outlink->h = inlink->h;
112 outlink->time_base = inlink->time_base;
113 outlink->frame_rate = inlink->frame_rate;
115 outlink->time_base.num *= 2;
116 outlink->frame_rate.den *= 2;
120 s->lowpass_line = lowpass_line_c;
122 ff_interlace_init_x86(s);
125 av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
126 s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
131 static void copy_picture_field(InterlaceContext *s,
132 AVFrame *src_frame, AVFrame *dst_frame,
133 AVFilterLink *inlink, enum FieldType field_type,
136 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
137 int hsub = desc->log2_chroma_w;
138 int vsub = desc->log2_chroma_h;
141 for (plane = 0; plane < desc->nb_components; plane++) {
142 int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
143 int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
144 uint8_t *dstp = dst_frame->data[plane];
145 const uint8_t *srcp = src_frame->data[plane];
147 av_assert0(cols >= 0 || lines >= 0);
149 lines = (lines + (field_type == FIELD_UPPER)) / 2;
150 if (field_type == FIELD_LOWER) {
151 srcp += src_frame->linesize[plane];
152 dstp += dst_frame->linesize[plane];
155 int srcp_linesize = src_frame->linesize[plane] * 2;
156 int dstp_linesize = dst_frame->linesize[plane] * 2;
157 for (j = lines; j > 0; j--) {
158 ptrdiff_t pref = src_frame->linesize[plane];
159 ptrdiff_t mref = -pref;
161 mref = 0; // there is no line above
163 pref = 0; // there is no line below
164 s->lowpass_line(dstp, cols, srcp, mref, pref);
165 dstp += dstp_linesize;
166 srcp += srcp_linesize;
169 av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
170 srcp, src_frame->linesize[plane] * 2,
176 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
178 AVFilterContext *ctx = inlink->dst;
179 AVFilterLink *outlink = ctx->outputs[0];
180 InterlaceContext *s = ctx->priv;
184 av_frame_free(&s->cur);
188 /* we need at least two frames */
189 if (!s->cur || !s->next)
192 if (s->cur->interlaced_frame) {
193 av_log(ctx, AV_LOG_WARNING,
194 "video is already interlaced, adjusting framerate only\n");
195 out = av_frame_clone(s->cur);
197 return AVERROR(ENOMEM);
198 out->pts /= 2; // adjust pts to new framerate
199 ret = ff_filter_frame(outlink, out);
203 tff = (s->scan == MODE_TFF);
204 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
206 return AVERROR(ENOMEM);
208 av_frame_copy_props(out, s->cur);
209 out->interlaced_frame = 1;
210 out->top_field_first = tff;
211 out->pts /= 2; // adjust pts to new framerate
213 /* copy upper/lower field from cur */
214 copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
215 av_frame_free(&s->cur);
217 /* copy lower/upper field from next */
218 copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
219 av_frame_free(&s->next);
221 ret = ff_filter_frame(outlink, out);
226 static const AVFilterPad inputs[] = {
229 .type = AVMEDIA_TYPE_VIDEO,
230 .filter_frame = filter_frame,
235 static const AVFilterPad outputs[] = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .config_props = config_out_props,
244 AVFilter ff_vf_interlace = {
246 .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
248 .priv_class = &interlace_class,
249 .priv_size = sizeof(InterlaceContext),
250 .query_formats = query_formats,