2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with Libav; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avassert.h"
44 typedef struct InterlaceContext {
46 enum ScanMode scan; // top or bottom field first scanning
47 int lowpass; // enable or disable low pass filterning
48 AVFrame *cur, *next; // the two frames from which the new one is obtained
49 int got_output; // signal an output frame is reday to request_frame()
52 #define OFFSET(x) offsetof(InterlaceContext, x)
53 #define V AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption options[] = {
55 { "scan", "scanning mode", OFFSET(scan),
56 AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
57 { "tff", "top field first", 0,
58 AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
59 { "bff", "bottom field first", 0,
60 AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
61 { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
62 AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
66 static const AVClass class = {
67 .class_name = "interlace filter",
68 .item_name = av_default_item_name,
70 .version = LIBAVUTIL_VERSION_INT,
74 static const enum AVPixelFormat formats_supported[] = {
75 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
76 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
77 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
78 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
81 static int query_formats(AVFilterContext *ctx)
83 ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
87 static av_cold void uninit(AVFilterContext *ctx)
89 InterlaceContext *s = ctx->priv;
91 av_frame_free(&s->cur);
92 av_frame_free(&s->next);
97 static int config_out_props(AVFilterLink *outlink)
99 AVFilterContext *ctx = outlink->src;
100 AVFilterLink *inlink = outlink->src->inputs[0];
101 InterlaceContext *s = ctx->priv;
104 av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
105 return AVERROR_INVALIDDATA;
109 av_log(ctx, AV_LOG_WARNING, "***warning*** Lowpass filter is disabled, "
110 "the resulting video will be aliased rather than interlaced.\n");
113 outlink->w = inlink->w;
114 outlink->h = inlink->h;
115 outlink->time_base = inlink->time_base;
117 outlink->time_base.num *= 2;
119 av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
120 s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
125 static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
126 AVFilterLink *inlink, enum FieldType field_type,
129 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
130 int vsub = desc->log2_chroma_h;
133 for (plane = 0; plane < desc->nb_components; plane++) {
134 int lines = (plane == 1 || plane == 2) ? -(-inlink->h) >> vsub : inlink->h;
135 int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
136 uint8_t *dstp = dst_frame->data[plane];
137 const uint8_t *srcp = src_frame->data[plane];
139 av_assert0(linesize >= 0);
141 lines = (lines + (field_type == FIELD_UPPER)) / 2;
142 if (field_type == FIELD_LOWER)
143 srcp += src_frame->linesize[plane];
144 if (field_type == FIELD_LOWER)
145 dstp += dst_frame->linesize[plane];
147 int srcp_linesize = src_frame->linesize[plane] * 2;
148 int dstp_linesize = dst_frame->linesize[plane] * 2;
149 for (j = lines; j > 0; j--) {
150 const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
151 const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
153 srcp_above = srcp; // there is no line above
155 srcp_below = srcp; // there is no line below
156 for (i = 0; i < linesize; i++) {
157 // this calculation is an integer representation of
158 // '0.5 * current + 0.25 * above + 0.25 * below'
159 // '1 +' is for rounding.
160 dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
162 dstp += dstp_linesize;
163 srcp += srcp_linesize;
166 av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
167 srcp, src_frame->linesize[plane] * 2,
173 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
175 AVFilterContext *ctx = inlink->dst;
176 AVFilterLink *outlink = ctx->outputs[0];
177 InterlaceContext *s = ctx->priv;
181 av_frame_free(&s->cur);
185 /* we need at least two frames */
186 if (!s->cur || !s->next)
189 if (s->cur->interlaced_frame) {
190 av_log(ctx, AV_LOG_WARNING,
191 "video is already interlaced, adjusting framerate only\n");
192 out = av_frame_clone(s->cur);
194 return AVERROR(ENOMEM);
195 out->pts /= 2; // adjust pts to new framerate
196 ret = ff_filter_frame(outlink, out);
201 tff = (s->scan == MODE_TFF);
202 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
204 return AVERROR(ENOMEM);
206 av_frame_copy_props(out, s->cur);
207 out->interlaced_frame = 1;
208 out->top_field_first = tff;
209 out->pts /= 2; // adjust pts to new framerate
211 /* copy upper/lower field from cur */
212 copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
213 av_frame_free(&s->cur);
215 /* copy lower/upper field from next */
216 copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
217 av_frame_free(&s->next);
219 ret = ff_filter_frame(outlink, out);
225 static int request_frame(AVFilterLink *outlink)
227 AVFilterContext *ctx = outlink->src;
228 InterlaceContext *s = ctx->priv;
232 while (ret >= 0 && !s->got_output)
233 ret = ff_request_frame(ctx->inputs[0]);
238 static const AVFilterPad inputs[] = {
241 .type = AVMEDIA_TYPE_VIDEO,
242 .filter_frame = filter_frame,
247 static const AVFilterPad outputs[] = {
250 .type = AVMEDIA_TYPE_VIDEO,
251 .config_props = config_out_props,
252 .request_frame = request_frame,
257 AVFilter ff_vf_interlace = {
259 .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
262 .priv_class = &class,
263 .priv_size = sizeof(InterlaceContext),
264 .query_formats = query_formats,