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"
33 int mode; ///< interlace mode selected
34 int frame; ///< number of the output frame
35 int vsub; ///< chroma vertical subsampling
36 AVFilterBufferRef *cur;
37 AVFilterBufferRef *next;
38 uint8_t *black_data[4]; ///< buffer used to fill padded lines
39 int black_linesize[4];
42 #define FULL_SCALE_YUVJ_FORMATS \
43 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
45 static enum PixelFormat full_scale_yuvj_pix_fmts[] = {
46 FULL_SCALE_YUVJ_FORMATS, PIX_FMT_NONE
49 static int query_formats(AVFilterContext *ctx)
51 static const enum PixelFormat pix_fmts[] = {
52 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P,
53 PIX_FMT_YUV444P, PIX_FMT_YUV410P, PIX_FMT_YUVA420P,
54 PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
58 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
62 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
64 TInterlaceContext *tinterlace = ctx->priv;
69 n = sscanf(args, "%d", &tinterlace->mode);
71 if (n != 1 || tinterlace->mode < 0 || tinterlace->mode > 5) {
72 av_log(ctx, AV_LOG_ERROR,
73 "Invalid mode '%s', use an integer between 0 and 5\n", args);
74 return AVERROR(EINVAL);
81 static av_cold void uninit(AVFilterContext *ctx)
83 TInterlaceContext *tinterlace = ctx->priv;
85 if (tinterlace->cur ) avfilter_unref_buffer(tinterlace->cur );
86 if (tinterlace->next) avfilter_unref_buffer(tinterlace->next);
88 av_freep(&tinterlace->black_data[0]);
91 static int config_out_props(AVFilterLink *outlink)
93 AVFilterContext *ctx = outlink->src;
94 AVFilterLink *inlink = outlink->src->inputs[0];
95 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[outlink->format];
96 TInterlaceContext *tinterlace = ctx->priv;
98 tinterlace->vsub = desc->log2_chroma_h;
99 outlink->w = inlink->w;
100 outlink->h = tinterlace->mode == 0 || tinterlace->mode == 3 ?
101 inlink->h*2 : inlink->h;
103 if (tinterlace->mode == 3) {
104 uint8_t black[4] = { 16, 128, 128, 16 };
106 if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
107 black[0] = black[3] = 0;
108 ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
109 outlink->w, outlink->h, outlink->format, 1);
113 /* fill black picture with black */
114 for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
115 int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
116 memset(tinterlace->black_data[i], black[i],
117 tinterlace->black_linesize[i] * h);
120 av_log(ctx, AV_LOG_INFO, "mode:%d h:%d -> h:%d\n",
121 tinterlace->mode, inlink->h, outlink->h);
126 #define FIELD_UPPER 0
127 #define FIELD_LOWER 1
128 #define FIELD_UPPER_AND_LOWER 2
131 * Copy picture field from src to dst.
133 * @param src_field copy from upper, lower field or both
134 * @param interleave leave a padding line between each copied field
135 * @param dst_field copy to upper or lower field,
136 * only meaningful when interleave is selected
139 void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
140 uint8_t *src[4], int src_linesize[4],
141 enum PixelFormat format, int w, int src_h,
142 int src_field, int interleave, int dst_field)
144 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
145 int plane, vsub = desc->log2_chroma_h;
146 int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
148 for (plane = 0; plane < desc->nb_components; plane++) {
149 int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
150 int linesize = av_image_get_linesize(format, w, plane);
151 uint8_t *dstp = dst[plane];
152 uint8_t *srcp = src[plane];
154 if (src_field == FIELD_LOWER)
155 srcp += src_linesize[plane];
156 if (interleave && dst_field == FIELD_LOWER)
157 dstp += dst_linesize[plane];
158 av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
159 srcp, src_linesize[plane]*k, linesize, lines);
163 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
165 AVFilterContext *ctx = inlink->dst;
166 TInterlaceContext *tinterlace = ctx->priv;
169 avfilter_unref_buffer(tinterlace->cur);
170 tinterlace->cur = tinterlace->next;
171 tinterlace->next = picref;
174 static void end_frame(AVFilterLink *inlink)
176 AVFilterContext *ctx = inlink->dst;
177 AVFilterLink *outlink = ctx->outputs[0];
178 TInterlaceContext *tinterlace = ctx->priv;
179 AVFilterBufferRef *cur = tinterlace->cur;
180 AVFilterBufferRef *next = tinterlace->next;
181 AVFilterBufferRef *out = NULL;
184 /* we need at least two frames */
185 if (!tinterlace->cur)
188 switch (tinterlace->mode) {
189 case 0: /* move the odd frame into the upper field of the new image, even into
190 * the lower field, generating a double-height video at half framerate */
191 out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
192 avfilter_copy_buffer_ref_props(out, cur);
193 out->video->h = outlink->h;
194 out->video->interlaced = 1;
195 out->video->top_field_first = 1;
197 /* write odd frame lines into the upper field of the new frame */
198 copy_picture_field(out->data, out->linesize,
199 cur->data, cur->linesize,
200 inlink->format, inlink->w, inlink->h,
201 FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
202 /* write even frame lines into the lower field of the new frame */
203 copy_picture_field(out->data, out->linesize,
204 next->data, next->linesize,
205 inlink->format, inlink->w, inlink->h,
206 FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
207 avfilter_unref_buffer(tinterlace->next);
208 tinterlace->next = NULL;
211 case 1: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
212 case 2: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
213 out = avfilter_ref_buffer(tinterlace->mode == 2 ? cur : next, AV_PERM_READ);
214 avfilter_unref_buffer(tinterlace->next);
215 tinterlace->next = NULL;
218 case 3: /* expand each frame to double height, but pad alternate
219 * lines with black; framerate unchanged */
220 out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
221 avfilter_copy_buffer_ref_props(out, cur);
222 out->video->h = outlink->h;
224 field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
225 /* copy upper and lower fields */
226 copy_picture_field(out->data, out->linesize,
227 cur->data, cur->linesize,
228 inlink->format, inlink->w, inlink->h,
229 FIELD_UPPER_AND_LOWER, 1, field);
230 /* pad with black the other field */
231 copy_picture_field(out->data, out->linesize,
232 tinterlace->black_data, tinterlace->black_linesize,
233 inlink->format, inlink->w, inlink->h,
234 FIELD_UPPER_AND_LOWER, 1, !field);
237 /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
238 * halving the frame rate and preserving image height */
239 case 4: /* top field first */
240 case 5: /* bottom field first */
241 tff = tinterlace->mode == 4;
242 out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
243 avfilter_copy_buffer_ref_props(out, cur);
244 out->video->interlaced = 1;
245 out->video->top_field_first = tff;
247 /* copy upper/lower field from cur */
248 copy_picture_field(out->data, out->linesize,
249 cur->data, cur->linesize,
250 inlink->format, inlink->w, inlink->h,
251 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
252 /* copy lower/upper field from next */
253 copy_picture_field(out->data, out->linesize,
254 next->data, next->linesize,
255 inlink->format, inlink->w, inlink->h,
256 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
257 avfilter_unref_buffer(tinterlace->next);
258 tinterlace->next = NULL;
262 avfilter_start_frame(outlink, out);
263 avfilter_draw_slice(outlink, 0, outlink->h, 1);
264 avfilter_end_frame(outlink);
269 static int poll_frame(AVFilterLink *outlink)
271 TInterlaceContext *tinterlace = outlink->src->priv;
272 AVFilterLink *inlink = outlink->src->inputs[0];
275 val = avfilter_poll_frame(inlink);
277 if (val == 1 && !tinterlace->next) {
278 if ((ret = avfilter_request_frame(inlink)) < 0)
280 val = avfilter_poll_frame(inlink);
282 assert(tinterlace->next);
287 static int request_frame(AVFilterLink *outlink)
289 TInterlaceContext *tinterlace = outlink->src->priv;
290 AVFilterLink *inlink = outlink->src->inputs[0];
295 if ((ret = avfilter_request_frame(inlink)) < 0)
297 } while (!tinterlace->cur);
302 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
304 AVFilter avfilter_vf_tinterlace = {
305 .name = "tinterlace",
306 .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
307 .priv_size = sizeof(TInterlaceContext),
310 .query_formats = query_formats,
312 .inputs = (const AVFilterPad[]) {
314 .type = AVMEDIA_TYPE_VIDEO,
315 .start_frame = start_frame,
316 .draw_slice = null_draw_slice,
317 .end_frame = end_frame, },
320 .outputs = (const AVFilterPad[]) {
322 .type = AVMEDIA_TYPE_VIDEO,
323 .config_props = config_out_props,
324 .poll_frame = poll_frame,
325 .request_frame = request_frame },