2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FFmpeg if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * temporal field interlace filter, ported from MPlayer/libmpcodecs
28 #include "libavutil/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/avassert.h"
40 MODE_INTERLEAVE_BOTTOM,
47 enum TInterlaceMode mode; ///< interlace mode selected
48 int frame; ///< number of the output frame
49 int vsub; ///< chroma vertical subsampling
50 AVFilterBufferRef *cur;
51 AVFilterBufferRef *next;
52 uint8_t *black_data[4]; ///< buffer used to fill padded lines
53 int black_linesize[4];
56 #define OFFSET(x) offsetof(TInterlaceContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59 static const AVOption tinterlace_options[] = {
60 {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
61 {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
62 {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
63 {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
64 {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
65 {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
66 {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
67 {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
72 AVFILTER_DEFINE_CLASS(tinterlace);
74 #define FULL_SCALE_YUVJ_FORMATS \
75 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
77 static enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
78 FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
81 static int query_formats(AVFilterContext *ctx)
83 static const enum AVPixelFormat pix_fmts[] = {
84 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
85 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
86 AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
90 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
94 static av_cold int init(AVFilterContext *ctx, const char *args)
96 TInterlaceContext *tinterlace = ctx->priv;
97 static const char *shorthand[] = { "mode", NULL };
99 tinterlace->class = &tinterlace_class;
100 av_opt_set_defaults(tinterlace);
102 return av_opt_set_from_string(tinterlace, args, shorthand, "=", ":");
105 static av_cold void uninit(AVFilterContext *ctx)
107 TInterlaceContext *tinterlace = ctx->priv;
109 avfilter_unref_bufferp(&tinterlace->cur );
110 avfilter_unref_bufferp(&tinterlace->next);
112 av_opt_free(tinterlace);
113 av_freep(&tinterlace->black_data[0]);
116 static int config_out_props(AVFilterLink *outlink)
118 AVFilterContext *ctx = outlink->src;
119 AVFilterLink *inlink = outlink->src->inputs[0];
120 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
121 TInterlaceContext *tinterlace = ctx->priv;
123 tinterlace->vsub = desc->log2_chroma_h;
124 outlink->w = inlink->w;
125 outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
126 inlink->h*2 : inlink->h;
128 if (tinterlace->mode == MODE_PAD) {
129 uint8_t black[4] = { 16, 128, 128, 16 };
131 if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
132 black[0] = black[3] = 0;
133 ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
134 outlink->w, outlink->h, outlink->format, 1);
138 /* fill black picture with black */
139 for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
140 int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
141 memset(tinterlace->black_data[i], black[i],
142 tinterlace->black_linesize[i] * h);
145 av_log(ctx, AV_LOG_VERBOSE, "mode:%d h:%d -> h:%d\n",
146 tinterlace->mode, inlink->h, outlink->h);
151 #define FIELD_UPPER 0
152 #define FIELD_LOWER 1
153 #define FIELD_UPPER_AND_LOWER 2
156 * Copy picture field from src to dst.
158 * @param src_field copy from upper, lower field or both
159 * @param interleave leave a padding line between each copied line
160 * @param dst_field copy to upper or lower field,
161 * only meaningful when interleave is selected
164 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
165 const uint8_t *src[4], int src_linesize[4],
166 enum AVPixelFormat format, int w, int src_h,
167 int src_field, int interleave, int dst_field)
169 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
170 int plane, vsub = desc->log2_chroma_h;
171 int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
173 for (plane = 0; plane < desc->nb_components; plane++) {
174 int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
175 int linesize = av_image_get_linesize(format, w, plane);
176 uint8_t *dstp = dst[plane];
177 const uint8_t *srcp = src[plane];
183 if (src_field == FIELD_LOWER)
184 srcp += src_linesize[plane];
185 if (interleave && dst_field == FIELD_LOWER)
186 dstp += dst_linesize[plane];
187 av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
188 srcp, src_linesize[plane]*k, linesize, lines);
192 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
194 AVFilterContext *ctx = inlink->dst;
195 AVFilterLink *outlink = ctx->outputs[0];
196 TInterlaceContext *tinterlace = ctx->priv;
197 AVFilterBufferRef *cur, *next, *out;
200 avfilter_unref_buffer(tinterlace->cur);
201 tinterlace->cur = tinterlace->next;
202 tinterlace->next = picref;
204 cur = tinterlace->cur;
205 next = tinterlace->next;
206 /* we need at least two frames */
207 if (!tinterlace->cur)
210 switch (tinterlace->mode) {
211 case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
212 * the lower field, generating a double-height video at half framerate */
213 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
215 return AVERROR(ENOMEM);
216 avfilter_copy_buffer_ref_props(out, cur);
217 out->video->h = outlink->h;
218 out->video->interlaced = 1;
219 out->video->top_field_first = 1;
221 /* write odd frame lines into the upper field of the new frame */
222 copy_picture_field(out->data, out->linesize,
223 (const uint8_t **)cur->data, cur->linesize,
224 inlink->format, inlink->w, inlink->h,
225 FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
226 /* write even frame lines into the lower field of the new frame */
227 copy_picture_field(out->data, out->linesize,
228 (const uint8_t **)next->data, next->linesize,
229 inlink->format, inlink->w, inlink->h,
230 FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
231 avfilter_unref_bufferp(&tinterlace->next);
234 case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
235 case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
236 out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
237 avfilter_unref_bufferp(&tinterlace->next);
240 case MODE_PAD: /* expand each frame to double height, but pad alternate
241 * lines with black; framerate unchanged */
242 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
243 avfilter_copy_buffer_ref_props(out, cur);
244 out->video->h = outlink->h;
246 field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
247 /* copy upper and lower fields */
248 copy_picture_field(out->data, out->linesize,
249 (const uint8_t **)cur->data, cur->linesize,
250 inlink->format, inlink->w, inlink->h,
251 FIELD_UPPER_AND_LOWER, 1, field);
252 /* pad with black the other field */
253 copy_picture_field(out->data, out->linesize,
254 (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
255 inlink->format, inlink->w, inlink->h,
256 FIELD_UPPER_AND_LOWER, 1, !field);
259 /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
260 * halving the frame rate and preserving image height */
261 case MODE_INTERLEAVE_TOP: /* top field first */
262 case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
263 tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
264 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
266 return AVERROR(ENOMEM);
267 avfilter_copy_buffer_ref_props(out, cur);
268 out->video->interlaced = 1;
269 out->video->top_field_first = tff;
271 /* copy upper/lower field from cur */
272 copy_picture_field(out->data, out->linesize,
273 (const uint8_t **)cur->data, cur->linesize,
274 inlink->format, inlink->w, inlink->h,
275 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
276 /* copy lower/upper field from next */
277 copy_picture_field(out->data, out->linesize,
278 (const uint8_t **)next->data, next->linesize,
279 inlink->format, inlink->w, inlink->h,
280 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
281 avfilter_unref_bufferp(&tinterlace->next);
283 case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
284 /* output current frame first */
285 out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
287 return AVERROR(ENOMEM);
288 out->video->interlaced = 1;
290 if ((ret = ff_filter_frame(outlink, out)) < 0)
293 /* output mix of current and next frame */
294 tff = next->video->top_field_first;
295 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
297 return AVERROR(ENOMEM);
298 avfilter_copy_buffer_ref_props(out, next);
299 out->video->interlaced = 1;
301 /* write current frame second field lines into the second field of the new frame */
302 copy_picture_field(out->data, out->linesize,
303 (const uint8_t **)cur->data, cur->linesize,
304 inlink->format, inlink->w, inlink->h,
305 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
306 /* write next frame first field lines into the first field of the new frame */
307 copy_picture_field(out->data, out->linesize,
308 (const uint8_t **)next->data, next->linesize,
309 inlink->format, inlink->w, inlink->h,
310 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
314 ret = ff_filter_frame(outlink, out);
320 static int request_frame(AVFilterLink *outlink)
322 TInterlaceContext *tinterlace = outlink->src->priv;
323 AVFilterLink *inlink = outlink->src->inputs[0];
328 if ((ret = ff_request_frame(inlink)) < 0)
330 } while (!tinterlace->cur);
335 static const AVFilterPad tinterlace_inputs[] = {
338 .type = AVMEDIA_TYPE_VIDEO,
339 .filter_frame = filter_frame,
344 static const AVFilterPad tinterlace_outputs[] = {
347 .type = AVMEDIA_TYPE_VIDEO,
348 .config_props = config_out_props,
349 .request_frame = request_frame,
354 AVFilter avfilter_vf_tinterlace = {
355 .name = "tinterlace",
356 .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
357 .priv_size = sizeof(TInterlaceContext),
360 .query_formats = query_formats,
361 .inputs = tinterlace_inputs,
362 .outputs = tinterlace_outputs,
363 .priv_class = &tinterlace_class,