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];
192 if (src_field == FIELD_LOWER)
193 srcp += src_linesize[plane];
194 if (interleave && dst_field == FIELD_LOWER)
195 dstp += dst_linesize[plane];
196 av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
197 srcp, src_linesize[plane]*k, linesize, lines);
201 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
203 AVFilterContext *ctx = inlink->dst;
204 TInterlaceContext *tinterlace = ctx->priv;
206 avfilter_unref_buffer(tinterlace->cur);
207 tinterlace->cur = tinterlace->next;
208 tinterlace->next = picref;
209 inlink->cur_buf = NULL;
213 static int end_frame(AVFilterLink *inlink)
215 AVFilterContext *ctx = inlink->dst;
216 AVFilterLink *outlink = ctx->outputs[0];
217 TInterlaceContext *tinterlace = ctx->priv;
218 AVFilterBufferRef *cur = tinterlace->cur;
219 AVFilterBufferRef *next = tinterlace->next;
220 AVFilterBufferRef *out = NULL;
223 /* we need at least two frames */
224 if (!tinterlace->cur)
227 switch (tinterlace->mode) {
228 case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
229 * the lower field, generating a double-height video at half framerate */
230 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
231 avfilter_copy_buffer_ref_props(out, cur);
232 out->video->h = outlink->h;
233 out->video->interlaced = 1;
234 out->video->top_field_first = 1;
236 /* write odd frame lines into the upper field of the new frame */
237 copy_picture_field(out->data, out->linesize,
238 (const uint8_t **)cur->data, cur->linesize,
239 inlink->format, inlink->w, inlink->h,
240 FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
241 /* write even frame lines into the lower field of the new frame */
242 copy_picture_field(out->data, out->linesize,
243 (const uint8_t **)next->data, next->linesize,
244 inlink->format, inlink->w, inlink->h,
245 FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
246 avfilter_unref_bufferp(&tinterlace->next);
249 case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
250 case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
251 out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
252 avfilter_unref_bufferp(&tinterlace->next);
255 case MODE_PAD: /* expand each frame to double height, but pad alternate
256 * lines with black; framerate unchanged */
257 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
258 avfilter_copy_buffer_ref_props(out, cur);
259 out->video->h = outlink->h;
261 field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
262 /* copy upper and lower fields */
263 copy_picture_field(out->data, out->linesize,
264 (const uint8_t **)cur->data, cur->linesize,
265 inlink->format, inlink->w, inlink->h,
266 FIELD_UPPER_AND_LOWER, 1, field);
267 /* pad with black the other field */
268 copy_picture_field(out->data, out->linesize,
269 (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
270 inlink->format, inlink->w, inlink->h,
271 FIELD_UPPER_AND_LOWER, 1, !field);
274 /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
275 * halving the frame rate and preserving image height */
276 case MODE_INTERLEAVE_TOP: /* top field first */
277 case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
278 tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
279 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
280 avfilter_copy_buffer_ref_props(out, cur);
281 out->video->interlaced = 1;
282 out->video->top_field_first = tff;
284 /* copy upper/lower field from cur */
285 copy_picture_field(out->data, out->linesize,
286 (const uint8_t **)cur->data, cur->linesize,
287 inlink->format, inlink->w, inlink->h,
288 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
289 /* copy lower/upper field from next */
290 copy_picture_field(out->data, out->linesize,
291 (const uint8_t **)next->data, next->linesize,
292 inlink->format, inlink->w, inlink->h,
293 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
294 avfilter_unref_bufferp(&tinterlace->next);
296 case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
297 /* output current frame first */
298 out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
299 out->video->interlaced = 1;
301 ff_start_frame(outlink, out);
302 ff_draw_slice(outlink, 0, outlink->h, 1);
303 ff_end_frame(outlink);
305 /* output mix of current and next frame */
306 tff = next->video->top_field_first;
307 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
308 avfilter_copy_buffer_ref_props(out, next);
309 out->video->interlaced = 1;
311 /* write current frame second field lines into the second field of the new frame */
312 copy_picture_field(out->data, out->linesize,
313 (const uint8_t **)cur->data, cur->linesize,
314 inlink->format, inlink->w, inlink->h,
315 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
316 /* write next frame first field lines into the first field of the new frame */
317 copy_picture_field(out->data, out->linesize,
318 (const uint8_t **)next->data, next->linesize,
319 inlink->format, inlink->w, inlink->h,
320 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
324 ff_start_frame(outlink, out);
325 ff_draw_slice(outlink, 0, outlink->h, 1);
326 ff_end_frame(outlink);
333 static int poll_frame(AVFilterLink *outlink)
335 TInterlaceContext *tinterlace = outlink->src->priv;
336 AVFilterLink *inlink = outlink->src->inputs[0];
339 val = ff_poll_frame(inlink);
341 if (val == 1 && !tinterlace->next) {
342 if ((ret = ff_request_frame(inlink)) < 0)
344 val = ff_poll_frame(inlink);
346 av_assert0(tinterlace->next);
351 static int request_frame(AVFilterLink *outlink)
353 TInterlaceContext *tinterlace = outlink->src->priv;
354 AVFilterLink *inlink = outlink->src->inputs[0];
359 if ((ret = ff_request_frame(inlink)) < 0)
361 } while (!tinterlace->cur);
366 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
368 AVFilter avfilter_vf_tinterlace = {
369 .name = "tinterlace",
370 .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
371 .priv_size = sizeof(TInterlaceContext),
374 .query_formats = query_formats,
376 .inputs = (const AVFilterPad[]) {
378 .type = AVMEDIA_TYPE_VIDEO,
379 .start_frame = start_frame,
380 .draw_slice = null_draw_slice,
381 .end_frame = end_frame, },
384 .outputs = (const AVFilterPad[]) {
386 .type = AVMEDIA_TYPE_VIDEO,
387 .config_props = config_out_props,
388 .poll_frame = poll_frame,
389 .request_frame = request_frame },