2 * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2010 Baptiste Coudurier
5 * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FFmpeg if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * temporal field interlace filter, ported from MPlayer/libmpcodecs
29 #include "libavutil/opt.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/avassert.h"
34 #include "tinterlace.h"
36 #define OFFSET(x) offsetof(TInterlaceContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
39 static const AVOption tinterlace_options[] = {
40 {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
41 {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
42 {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
43 {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
44 {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
45 {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
46 {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
47 {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
48 {"mergex2", "merge fields keeping same frame rate", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
50 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
51 {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
52 {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
53 {"complex_filter", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
54 {"cvlpf", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
55 {"exact_tb", "force a timebase which can represent timestamps exactly", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_EXACT_TB}, INT_MIN, INT_MAX, FLAGS, "flags" },
60 AVFILTER_DEFINE_CLASS(tinterlace);
62 #define FULL_SCALE_YUVJ_FORMATS \
63 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
65 static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
66 FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
69 static const AVRational standard_tbs[] = {
75 static int query_formats(AVFilterContext *ctx)
77 static const enum AVPixelFormat pix_fmts[] = {
78 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
79 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
80 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
81 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
82 AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
86 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
88 return AVERROR(ENOMEM);
89 return ff_set_common_formats(ctx, fmts_list);
92 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
93 ptrdiff_t mref, ptrdiff_t pref)
95 const uint8_t *srcp_above = srcp + mref;
96 const uint8_t *srcp_below = srcp + pref;
98 for (i = 0; i < width; i++) {
99 // this calculation is an integer representation of
100 // '0.5 * current + 0.25 * above + 0.25 * below'
101 // '1 +' is for rounding.
102 dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
106 static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
107 ptrdiff_t mref, ptrdiff_t pref)
109 const uint8_t *srcp_above = srcp + mref;
110 const uint8_t *srcp_below = srcp + pref;
111 const uint8_t *srcp_above2 = srcp + mref * 2;
112 const uint8_t *srcp_below2 = srcp + pref * 2;
114 for (i = 0; i < width; i++) {
115 // this calculation is an integer representation of
116 // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
117 // '4 +' is for rounding.
118 dstp[i] = av_clip_uint8((4 + (srcp[i] << 2)
119 + ((srcp[i] + srcp_above[i] + srcp_below[i]) << 1)
120 - srcp_above2[i] - srcp_below2[i]) >> 3);
124 static av_cold void uninit(AVFilterContext *ctx)
126 TInterlaceContext *tinterlace = ctx->priv;
128 av_frame_free(&tinterlace->cur );
129 av_frame_free(&tinterlace->next);
130 av_freep(&tinterlace->black_data[0]);
133 static int config_out_props(AVFilterLink *outlink)
135 AVFilterContext *ctx = outlink->src;
136 AVFilterLink *inlink = outlink->src->inputs[0];
137 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
138 TInterlaceContext *tinterlace = ctx->priv;
141 tinterlace->vsub = desc->log2_chroma_h;
142 outlink->w = inlink->w;
143 outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2?
144 inlink->h*2 : inlink->h;
145 if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2)
146 outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio,
149 if (tinterlace->mode == MODE_PAD) {
150 uint8_t black[4] = { 16, 128, 128, 16 };
152 if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
153 black[0] = black[3] = 0;
154 ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
155 outlink->w, outlink->h, outlink->format, 16);
159 /* fill black picture with black */
160 for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
161 int h = i == 1 || i == 2 ? AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
162 memset(tinterlace->black_data[i], black[i],
163 tinterlace->black_linesize[i] * h);
166 if ((tinterlace->flags & TINTERLACE_FLAG_VLPF
167 || tinterlace->flags & TINTERLACE_FLAG_CVLPF)
168 && !(tinterlace->mode == MODE_INTERLEAVE_TOP
169 || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
170 av_log(ctx, AV_LOG_WARNING, "low_pass_filter flags ignored with mode %d\n",
172 tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
173 tinterlace->flags &= ~TINTERLACE_FLAG_CVLPF;
175 tinterlace->preout_time_base = inlink->time_base;
176 if (tinterlace->mode == MODE_INTERLACEX2) {
177 tinterlace->preout_time_base.den *= 2;
178 outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
179 outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2});
180 } else if (tinterlace->mode == MODE_MERGEX2) {
181 outlink->frame_rate = inlink->frame_rate;
182 outlink->time_base = inlink->time_base;
183 } else if (tinterlace->mode != MODE_PAD) {
184 outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
185 outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
188 for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
189 if (!av_cmp_q(standard_tbs[i], outlink->time_base))
192 if (i == FF_ARRAY_ELEMS(standard_tbs) ||
193 (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
194 outlink->time_base = tinterlace->preout_time_base;
196 if (tinterlace->flags & TINTERLACE_FLAG_CVLPF) {
197 tinterlace->lowpass_line = lowpass_line_complex_c;
199 ff_tinterlace_init_x86(tinterlace);
200 } else if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
201 tinterlace->lowpass_line = lowpass_line_c;
203 ff_tinterlace_init_x86(tinterlace);
206 av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
207 (tinterlace->flags & TINTERLACE_FLAG_CVLPF) ? "complex" :
208 (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "linear" : "off",
209 inlink->h, outlink->h);
214 #define FIELD_UPPER 0
215 #define FIELD_LOWER 1
216 #define FIELD_UPPER_AND_LOWER 2
219 * Copy picture field from src to dst.
221 * @param src_field copy from upper, lower field or both
222 * @param interleave leave a padding line between each copied line
223 * @param dst_field copy to upper or lower field,
224 * only meaningful when interleave is selected
225 * @param flags context flags
228 void copy_picture_field(TInterlaceContext *tinterlace,
229 uint8_t *dst[4], int dst_linesize[4],
230 const uint8_t *src[4], int src_linesize[4],
231 enum AVPixelFormat format, int w, int src_h,
232 int src_field, int interleave, int dst_field,
235 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
236 int hsub = desc->log2_chroma_w;
237 int plane, vsub = desc->log2_chroma_h;
238 int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
241 for (plane = 0; plane < desc->nb_components; plane++) {
242 int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;
243 int cols = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT( w, hsub) : w;
244 uint8_t *dstp = dst[plane];
245 const uint8_t *srcp = src[plane];
247 lines = (lines + (src_field == FIELD_UPPER)) / k;
248 if (src_field == FIELD_LOWER)
249 srcp += src_linesize[plane];
250 if (interleave && dst_field == FIELD_LOWER)
251 dstp += dst_linesize[plane];
252 // Low-pass filtering is required when creating an interlaced destination from
253 // a progressive source which contains high-frequency vertical detail.
254 // Filtering will reduce interlace 'twitter' and Moire patterning.
255 if (flags & TINTERLACE_FLAG_CVLPF) {
256 int srcp_linesize = src_linesize[plane] * k;
257 int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
258 for (h = lines; h > 0; h--) {
259 ptrdiff_t pref = src_linesize[plane];
260 ptrdiff_t mref = -pref;
261 if (h >= (lines - 1)) mref = 0;
262 else if (h <= 2) pref = 0;
264 tinterlace->lowpass_line(dstp, cols, srcp, mref, pref);
265 dstp += dstp_linesize;
266 srcp += srcp_linesize;
268 } else if (flags & TINTERLACE_FLAG_VLPF) {
269 int srcp_linesize = src_linesize[plane] * k;
270 int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
271 for (h = lines; h > 0; h--) {
272 ptrdiff_t pref = src_linesize[plane];
273 ptrdiff_t mref = -pref;
274 if (h == lines) mref = 0; // there is no line above
275 else if (h == 1) pref = 0; // there is no line below
277 tinterlace->lowpass_line(dstp, cols, srcp, mref, pref);
278 dstp += dstp_linesize;
279 srcp += srcp_linesize;
282 av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
283 srcp, src_linesize[plane]*k, cols, lines);
288 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
290 AVFilterContext *ctx = inlink->dst;
291 AVFilterLink *outlink = ctx->outputs[0];
292 TInterlaceContext *tinterlace = ctx->priv;
293 AVFrame *cur, *next, *out;
296 av_frame_free(&tinterlace->cur);
297 tinterlace->cur = tinterlace->next;
298 tinterlace->next = picref;
300 cur = tinterlace->cur;
301 next = tinterlace->next;
302 /* we need at least two frames */
303 if (!tinterlace->cur)
306 switch (tinterlace->mode) {
307 case MODE_MERGEX2: /* move the odd frame into the upper field of the new image, even into
308 * the lower field, generating a double-height video at same framerate */
309 case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
310 * the lower field, generating a double-height video at half framerate */
311 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
313 return AVERROR(ENOMEM);
314 av_frame_copy_props(out, cur);
315 out->height = outlink->h;
316 out->interlaced_frame = 1;
317 out->top_field_first = 1;
318 out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
320 /* write odd frame lines into the upper field of the new frame */
321 copy_picture_field(tinterlace, out->data, out->linesize,
322 (const uint8_t **)cur->data, cur->linesize,
323 inlink->format, inlink->w, inlink->h,
324 FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? inlink->frame_count_out & 1 ? FIELD_LOWER : FIELD_UPPER : FIELD_UPPER, tinterlace->flags);
325 /* write even frame lines into the lower field of the new frame */
326 copy_picture_field(tinterlace, out->data, out->linesize,
327 (const uint8_t **)next->data, next->linesize,
328 inlink->format, inlink->w, inlink->h,
329 FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? inlink->frame_count_out & 1 ? FIELD_UPPER : FIELD_LOWER : FIELD_LOWER, tinterlace->flags);
330 if (tinterlace->mode != MODE_MERGEX2)
331 av_frame_free(&tinterlace->next);
334 case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
335 case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
336 out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
338 return AVERROR(ENOMEM);
339 av_frame_free(&tinterlace->next);
342 case MODE_PAD: /* expand each frame to double height, but pad alternate
343 * lines with black; framerate unchanged */
344 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
346 return AVERROR(ENOMEM);
347 av_frame_copy_props(out, cur);
348 out->height = outlink->h;
349 out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
351 field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
352 /* copy upper and lower fields */
353 copy_picture_field(tinterlace, out->data, out->linesize,
354 (const uint8_t **)cur->data, cur->linesize,
355 inlink->format, inlink->w, inlink->h,
356 FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
357 /* pad with black the other field */
358 copy_picture_field(tinterlace, out->data, out->linesize,
359 (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
360 inlink->format, inlink->w, inlink->h,
361 FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
364 /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
365 * halving the frame rate and preserving image height */
366 case MODE_INTERLEAVE_TOP: /* top field first */
367 case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
368 tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
369 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
371 return AVERROR(ENOMEM);
372 av_frame_copy_props(out, cur);
373 out->interlaced_frame = 1;
374 out->top_field_first = tff;
376 /* copy upper/lower field from cur */
377 copy_picture_field(tinterlace, out->data, out->linesize,
378 (const uint8_t **)cur->data, cur->linesize,
379 inlink->format, inlink->w, inlink->h,
380 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
382 /* copy lower/upper field from next */
383 copy_picture_field(tinterlace, out->data, out->linesize,
384 (const uint8_t **)next->data, next->linesize,
385 inlink->format, inlink->w, inlink->h,
386 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
388 av_frame_free(&tinterlace->next);
390 case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
391 /* output current frame first */
392 out = av_frame_clone(cur);
394 return AVERROR(ENOMEM);
395 out->interlaced_frame = 1;
396 if (cur->pts != AV_NOPTS_VALUE)
397 out->pts = cur->pts*2;
399 out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
400 if ((ret = ff_filter_frame(outlink, out)) < 0)
403 /* output mix of current and next frame */
404 tff = next->top_field_first;
405 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
407 return AVERROR(ENOMEM);
408 av_frame_copy_props(out, next);
409 out->interlaced_frame = 1;
410 out->top_field_first = !tff;
412 if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
413 out->pts = cur->pts + next->pts;
415 out->pts = AV_NOPTS_VALUE;
416 /* write current frame second field lines into the second field of the new frame */
417 copy_picture_field(tinterlace, out->data, out->linesize,
418 (const uint8_t **)cur->data, cur->linesize,
419 inlink->format, inlink->w, inlink->h,
420 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
422 /* write next frame first field lines into the first field of the new frame */
423 copy_picture_field(tinterlace, out->data, out->linesize,
424 (const uint8_t **)next->data, next->linesize,
425 inlink->format, inlink->w, inlink->h,
426 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
433 out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
434 ret = ff_filter_frame(outlink, out);
440 static const AVFilterPad tinterlace_inputs[] = {
443 .type = AVMEDIA_TYPE_VIDEO,
444 .filter_frame = filter_frame,
449 static const AVFilterPad tinterlace_outputs[] = {
452 .type = AVMEDIA_TYPE_VIDEO,
453 .config_props = config_out_props,
458 AVFilter ff_vf_tinterlace = {
459 .name = "tinterlace",
460 .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
461 .priv_size = sizeof(TInterlaceContext),
463 .query_formats = query_formats,
464 .inputs = tinterlace_inputs,
465 .outputs = tinterlace_outputs,
466 .priv_class = &tinterlace_class,