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/imgutils.h"
29 #include "libavutil/avassert.h"
39 MODE_INTERLEAVE_BOTTOM,
43 static const char *tinterlace_mode_str[] = {
55 enum TInterlaceMode mode; ///< interlace mode selected
56 int frame; ///< number of the output frame
57 int vsub; ///< chroma vertical subsampling
58 AVFilterBufferRef *cur;
59 AVFilterBufferRef *next;
60 uint8_t *black_data[4]; ///< buffer used to fill padded lines
61 int black_linesize[4];
64 #define FULL_SCALE_YUVJ_FORMATS \
65 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
67 static enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
68 FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
71 static int query_formats(AVFilterContext *ctx)
73 static const enum AVPixelFormat pix_fmts[] = {
74 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
75 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
76 AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
80 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
84 static av_cold int init(AVFilterContext *ctx, const char *args)
86 TInterlaceContext *tinterlace = ctx->priv;
90 tinterlace->mode = MODE_MERGE;
93 if (sscanf(args, "%d%c", (int *)&tinterlace->mode, &c) == 1) {
94 if (tinterlace->mode > 6) {
95 av_log(ctx, AV_LOG_ERROR,
96 "Invalid mode '%s', use an integer between 0 and 6\n", args);
97 return AVERROR(EINVAL);
100 av_log(ctx, AV_LOG_WARNING,
101 "Using numeric constant is deprecated, use symbolic values\n");
103 for (i = 0; tinterlace_mode_str[i]; i++) {
104 if (!strcmp(tinterlace_mode_str[i], args)) {
105 tinterlace->mode = i;
109 if (!tinterlace_mode_str[i]) {
110 av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'\n", args);
111 return AVERROR(EINVAL);
119 static av_cold void uninit(AVFilterContext *ctx)
121 TInterlaceContext *tinterlace = ctx->priv;
123 if (tinterlace->cur ) avfilter_unref_bufferp(&tinterlace->cur );
124 if (tinterlace->next) avfilter_unref_bufferp(&tinterlace->next);
126 av_freep(&tinterlace->black_data[0]);
129 static int config_out_props(AVFilterLink *outlink)
131 AVFilterContext *ctx = outlink->src;
132 AVFilterLink *inlink = outlink->src->inputs[0];
133 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
134 TInterlaceContext *tinterlace = ctx->priv;
136 tinterlace->vsub = desc->log2_chroma_h;
137 outlink->w = inlink->w;
138 outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
139 inlink->h*2 : inlink->h;
141 if (tinterlace->mode == MODE_PAD) {
142 uint8_t black[4] = { 16, 128, 128, 16 };
144 if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
145 black[0] = black[3] = 0;
146 ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
147 outlink->w, outlink->h, outlink->format, 1);
151 /* fill black picture with black */
152 for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
153 int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
154 memset(tinterlace->black_data[i], black[i],
155 tinterlace->black_linesize[i] * h);
158 av_log(ctx, AV_LOG_VERBOSE, "mode:%s h:%d -> h:%d\n",
159 tinterlace_mode_str[tinterlace->mode], inlink->h, outlink->h);
164 #define FIELD_UPPER 0
165 #define FIELD_LOWER 1
166 #define FIELD_UPPER_AND_LOWER 2
169 * Copy picture field from src to dst.
171 * @param src_field copy from upper, lower field or both
172 * @param interleave leave a padding line between each copied line
173 * @param dst_field copy to upper or lower field,
174 * only meaningful when interleave is selected
177 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
178 const uint8_t *src[4], int src_linesize[4],
179 enum AVPixelFormat format, int w, int src_h,
180 int src_field, int interleave, int dst_field)
182 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
183 int plane, vsub = desc->log2_chroma_h;
184 int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
186 for (plane = 0; plane < desc->nb_components; plane++) {
187 int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
188 int linesize = av_image_get_linesize(format, w, plane);
189 uint8_t *dstp = dst[plane];
190 const uint8_t *srcp = src[plane];
196 if (src_field == FIELD_LOWER)
197 srcp += src_linesize[plane];
198 if (interleave && dst_field == FIELD_LOWER)
199 dstp += dst_linesize[plane];
200 av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
201 srcp, src_linesize[plane]*k, linesize, lines);
205 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
207 AVFilterContext *ctx = inlink->dst;
208 TInterlaceContext *tinterlace = ctx->priv;
210 avfilter_unref_buffer(tinterlace->cur);
211 tinterlace->cur = tinterlace->next;
212 tinterlace->next = picref;
213 inlink->cur_buf = NULL;
217 static int end_frame(AVFilterLink *inlink)
219 AVFilterContext *ctx = inlink->dst;
220 AVFilterLink *outlink = ctx->outputs[0];
221 TInterlaceContext *tinterlace = ctx->priv;
222 AVFilterBufferRef *cur = tinterlace->cur;
223 AVFilterBufferRef *next = tinterlace->next;
224 AVFilterBufferRef *out = NULL;
227 /* we need at least two frames */
228 if (!tinterlace->cur)
231 switch (tinterlace->mode) {
232 case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
233 * the lower field, generating a double-height video at half framerate */
234 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
235 avfilter_copy_buffer_ref_props(out, cur);
236 out->video->h = outlink->h;
237 out->video->interlaced = 1;
238 out->video->top_field_first = 1;
240 /* write odd frame lines into the upper field of the new frame */
241 copy_picture_field(out->data, out->linesize,
242 (const uint8_t **)cur->data, cur->linesize,
243 inlink->format, inlink->w, inlink->h,
244 FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
245 /* write even frame lines into the lower field of the new frame */
246 copy_picture_field(out->data, out->linesize,
247 (const uint8_t **)next->data, next->linesize,
248 inlink->format, inlink->w, inlink->h,
249 FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
250 avfilter_unref_bufferp(&tinterlace->next);
253 case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
254 case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
255 out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
256 avfilter_unref_bufferp(&tinterlace->next);
259 case MODE_PAD: /* expand each frame to double height, but pad alternate
260 * lines with black; framerate unchanged */
261 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
262 avfilter_copy_buffer_ref_props(out, cur);
263 out->video->h = outlink->h;
265 field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
266 /* copy upper and lower fields */
267 copy_picture_field(out->data, out->linesize,
268 (const uint8_t **)cur->data, cur->linesize,
269 inlink->format, inlink->w, inlink->h,
270 FIELD_UPPER_AND_LOWER, 1, field);
271 /* pad with black the other field */
272 copy_picture_field(out->data, out->linesize,
273 (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
274 inlink->format, inlink->w, inlink->h,
275 FIELD_UPPER_AND_LOWER, 1, !field);
278 /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
279 * halving the frame rate and preserving image height */
280 case MODE_INTERLEAVE_TOP: /* top field first */
281 case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
282 tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
283 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
284 avfilter_copy_buffer_ref_props(out, cur);
285 out->video->interlaced = 1;
286 out->video->top_field_first = tff;
288 /* copy upper/lower field from cur */
289 copy_picture_field(out->data, out->linesize,
290 (const uint8_t **)cur->data, cur->linesize,
291 inlink->format, inlink->w, inlink->h,
292 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
293 /* copy lower/upper field from next */
294 copy_picture_field(out->data, out->linesize,
295 (const uint8_t **)next->data, next->linesize,
296 inlink->format, inlink->w, inlink->h,
297 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
298 avfilter_unref_bufferp(&tinterlace->next);
300 case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
301 /* output current frame first */
302 out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
303 out->video->interlaced = 1;
305 ff_start_frame(outlink, out);
306 ff_draw_slice(outlink, 0, outlink->h, 1);
307 ff_end_frame(outlink);
309 /* output mix of current and next frame */
310 tff = next->video->top_field_first;
311 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
312 avfilter_copy_buffer_ref_props(out, next);
313 out->video->interlaced = 1;
315 /* write current frame second field lines into the second field of the new frame */
316 copy_picture_field(out->data, out->linesize,
317 (const uint8_t **)cur->data, cur->linesize,
318 inlink->format, inlink->w, inlink->h,
319 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
320 /* write next frame first field lines into the first field of the new frame */
321 copy_picture_field(out->data, out->linesize,
322 (const uint8_t **)next->data, next->linesize,
323 inlink->format, inlink->w, inlink->h,
324 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
328 ff_start_frame(outlink, out);
329 ff_draw_slice(outlink, 0, outlink->h, 1);
330 ff_end_frame(outlink);
337 static int request_frame(AVFilterLink *outlink)
339 TInterlaceContext *tinterlace = outlink->src->priv;
340 AVFilterLink *inlink = outlink->src->inputs[0];
345 if ((ret = ff_request_frame(inlink)) < 0)
347 } while (!tinterlace->cur);
352 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
354 static const AVFilterPad tinterlace_inputs[] = {
357 .type = AVMEDIA_TYPE_VIDEO,
358 .start_frame = start_frame,
359 .draw_slice = null_draw_slice,
360 .end_frame = end_frame,
365 static const AVFilterPad tinterlace_outputs[] = {
368 .type = AVMEDIA_TYPE_VIDEO,
369 .config_props = config_out_props,
370 .request_frame = request_frame,
375 AVFilter avfilter_vf_tinterlace = {
376 .name = "tinterlace",
377 .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
378 .priv_size = sizeof(TInterlaceContext),
381 .query_formats = query_formats,
382 .inputs = tinterlace_inputs,
383 .outputs = tinterlace_outputs,