8fb03b4d |
/* |
2da5bf4c |
* Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com> |
8fb03b4d |
* Copyright (c) 2011 Stefano Sabatini
* Copyright (c) 2010 Baptiste Coudurier
* Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with FFmpeg if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file
* temporal field interlace filter, ported from MPlayer/libmpcodecs
*/
|
c6a21677 |
#include "libavutil/opt.h" |
8fb03b4d |
#include "libavutil/imgutils.h" |
e145afb4 |
#include "libavutil/avassert.h" |
8fb03b4d |
#include "avfilter.h"
#include "internal.h" |
9d548fce |
#include "tinterlace.h" |
8fb03b4d |
|
c6a21677 |
#define OFFSET(x) offsetof(TInterlaceContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
static const AVOption tinterlace_options[] = {
{"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
{"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
{"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
{"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
{"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
{"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
{"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
{"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"}, |
13090895 |
{"mergex2", "merge fields keeping same frame rate", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGEX2}, INT_MIN, INT_MAX, FLAGS, "mode"}, |
c6a21677 |
|
8997a0fa |
{"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
{"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
{"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" }, |
2da5bf4c |
{"complex_filter", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
{"cvlpf", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" }, |
59f1f764 |
{"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" }, |
8997a0fa |
|
c6a21677 |
{NULL}
};
AVFILTER_DEFINE_CLASS(tinterlace);
|
8fb03b4d |
#define FULL_SCALE_YUVJ_FORMATS \ |
ac627b3d |
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P |
8fb03b4d |
|
c9a4ec79 |
static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = { |
ac627b3d |
FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE |
8fb03b4d |
};
|
59f1f764 |
static const AVRational standard_tbs[] = {
{1, 25},
{1, 30},
{1001, 30000},
};
|
8fb03b4d |
static int query_formats(AVFilterContext *ctx)
{ |
ac627b3d |
static const enum AVPixelFormat pix_fmts[] = { |
8ac0eb2c |
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
40bfaa19 |
AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE,
AV_PIX_FMT_YUV440P10LE, AV_PIX_FMT_YUV444P10LE,
AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE,
AV_PIX_FMT_YUV440P12LE, AV_PIX_FMT_YUV444P12LE, |
8ac0eb2c |
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
40bfaa19 |
AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE, |
ac627b3d |
AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
AV_PIX_FMT_NONE |
8fb03b4d |
};
|
a0854c08 |
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
if (!fmts_list)
return AVERROR(ENOMEM);
return ff_set_common_formats(ctx, fmts_list); |
8fb03b4d |
}
|
18b46ecc |
static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp, |
40bfaa19 |
ptrdiff_t mref, ptrdiff_t pref, int clip_max) |
18b46ecc |
{ |
207e6deb |
const uint8_t *srcp_above = srcp + mref;
const uint8_t *srcp_below = srcp + pref; |
18b46ecc |
int i;
for (i = 0; i < width; i++) {
// this calculation is an integer representation of
// '0.5 * current + 0.25 * above + 0.25 * below'
// '1 +' is for rounding.
dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
}
}
|
40bfaa19 |
static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
ptrdiff_t mref, ptrdiff_t pref, int clip_max)
{
uint16_t *dstp = (uint16_t *)dst8;
const uint16_t *srcp = (const uint16_t *)src8;
const uint16_t *srcp_above = srcp + mref / 2;
const uint16_t *srcp_below = srcp + pref / 2;
int i, src_x;
for (i = 0; i < width; i++) {
// this calculation is an integer representation of
// '0.5 * current + 0.25 * above + 0.25 * below'
// '1 +' is for rounding.
src_x = av_le2ne16(srcp[i]) << 1;
dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
+ av_le2ne16(srcp_below[i])) >> 2);
}
}
|
2da5bf4c |
static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp, |
40bfaa19 |
ptrdiff_t mref, ptrdiff_t pref, int clip_max) |
2da5bf4c |
{
const uint8_t *srcp_above = srcp + mref;
const uint8_t *srcp_below = srcp + pref;
const uint8_t *srcp_above2 = srcp + mref * 2;
const uint8_t *srcp_below2 = srcp + pref * 2; |
d491d6a0 |
int i, src_x, src_ab; |
2da5bf4c |
for (i = 0; i < width; i++) {
// this calculation is an integer representation of
// '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
// '4 +' is for rounding. |
d491d6a0 |
src_x = srcp[i] << 1;
src_ab = srcp_above[i] + srcp_below[i];
dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1) |
a7f6bfdc |
- srcp_above2[i] - srcp_below2[i]) >> 3);
// Prevent over-sharpening:
// dst must not exceed src when the average of above and below
// is less than src. And the other way around. |
d491d6a0 |
if (src_ab > src_x) { |
a7f6bfdc |
if (dstp[i] < srcp[i])
dstp[i] = srcp[i];
} else if (dstp[i] > srcp[i])
dstp[i] = srcp[i]; |
2da5bf4c |
}
}
|
40bfaa19 |
static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
ptrdiff_t mref, ptrdiff_t pref, int clip_max)
{
uint16_t *dstp = (uint16_t *)dst8;
const uint16_t *srcp = (const uint16_t *)src8;
const uint16_t *srcp_above = srcp + mref / 2;
const uint16_t *srcp_below = srcp + pref / 2;
const uint16_t *srcp_above2 = srcp + mref;
const uint16_t *srcp_below2 = srcp + pref;
int i, dst_le, src_le, src_x, src_ab;
for (i = 0; i < width; i++) {
// this calculation is an integer representation of
// '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
// '4 +' is for rounding.
src_le = av_le2ne16(srcp[i]);
src_x = src_le << 1;
src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
- av_le2ne16(srcp_above2[i])
- av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
// Prevent over-sharpening:
// dst must not exceed src when the average of above and below
// is less than src. And the other way around.
if (src_ab > src_x) {
if (dst_le < src_le)
dstp[i] = av_le2ne16(src_le);
else
dstp[i] = av_le2ne16(dst_le);
} else if (dst_le > src_le) {
dstp[i] = av_le2ne16(src_le);
} else
dstp[i] = av_le2ne16(dst_le);
}
}
|
8fb03b4d |
static av_cold void uninit(AVFilterContext *ctx)
{
TInterlaceContext *tinterlace = ctx->priv;
|
a05a44e2 |
av_frame_free(&tinterlace->cur );
av_frame_free(&tinterlace->next); |
8fb03b4d |
av_freep(&tinterlace->black_data[0]);
}
static int config_out_props(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = outlink->src->inputs[0]; |
79393a83 |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format); |
8fb03b4d |
TInterlaceContext *tinterlace = ctx->priv; |
59f1f764 |
int i; |
8fb03b4d |
tinterlace->vsub = desc->log2_chroma_h;
outlink->w = inlink->w; |
13090895 |
outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2? |
8fb03b4d |
inlink->h*2 : inlink->h; |
13090895 |
if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2) |
3b4e694e |
outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio,
av_make_q(2, 1)); |
8fb03b4d |
|
5f161c23 |
if (tinterlace->mode == MODE_PAD) { |
58ca4466 |
uint8_t black[4] = { 0, 0, 0, 16 };
int ret;
ff_draw_init(&tinterlace->draw, outlink->format, 0);
ff_draw_color(&tinterlace->draw, &tinterlace->color, black); |
8fb03b4d |
if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts)) |
58ca4466 |
tinterlace->color.comp[0].u8[0] = 0; |
8fb03b4d |
ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize, |
b65ea6ab |
outlink->w, outlink->h, outlink->format, 16); |
8fb03b4d |
if (ret < 0)
return ret;
|
58ca4466 |
ff_fill_rectangle(&tinterlace->draw, &tinterlace->color, tinterlace->black_data,
tinterlace->black_linesize, 0, 0, outlink->w, outlink->h); |
8fb03b4d |
} |
3af10603 |
if (tinterlace->flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF) |
8997a0fa |
&& !(tinterlace->mode == MODE_INTERLEAVE_TOP
|| tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) { |
2da5bf4c |
av_log(ctx, AV_LOG_WARNING, "low_pass_filter flags ignored with mode %d\n", |
8997a0fa |
tinterlace->mode); |
3af10603 |
tinterlace->flags &= ~(TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF); |
8997a0fa |
} |
59f1f764 |
tinterlace->preout_time_base = inlink->time_base; |
0d5ae023 |
if (tinterlace->mode == MODE_INTERLACEX2) { |
59f1f764 |
tinterlace->preout_time_base.den *= 2; |
0d5ae023 |
outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1}); |
59f1f764 |
outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2}); |
13090895 |
} else if (tinterlace->mode == MODE_MERGEX2) {
outlink->frame_rate = inlink->frame_rate;
outlink->time_base = inlink->time_base; |
83490016 |
} else if (tinterlace->mode != MODE_PAD) {
outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2}); |
59f1f764 |
outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
}
for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
if (!av_cmp_q(standard_tbs[i], outlink->time_base))
break; |
0d5ae023 |
} |
59f1f764 |
if (i == FF_ARRAY_ELEMS(standard_tbs) ||
(tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
outlink->time_base = tinterlace->preout_time_base; |
0d5ae023 |
|
40bfaa19 |
tinterlace->csp = av_pix_fmt_desc_get(outlink->format); |
2da5bf4c |
if (tinterlace->flags & TINTERLACE_FLAG_CVLPF) { |
40bfaa19 |
if (tinterlace->csp->comp[0].depth > 8)
tinterlace->lowpass_line = lowpass_line_complex_c_16;
else
tinterlace->lowpass_line = lowpass_line_complex_c; |
2da5bf4c |
if (ARCH_X86)
ff_tinterlace_init_x86(tinterlace);
} else if (tinterlace->flags & TINTERLACE_FLAG_VLPF) { |
40bfaa19 |
if (tinterlace->csp->comp[0].depth > 8)
tinterlace->lowpass_line = lowpass_line_c_16;
else
tinterlace->lowpass_line = lowpass_line_c; |
fb3eb573 |
if (ARCH_X86)
ff_tinterlace_init_x86(tinterlace); |
18b46ecc |
}
|
2da5bf4c |
av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
(tinterlace->flags & TINTERLACE_FLAG_CVLPF) ? "complex" :
(tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "linear" : "off", |
8997a0fa |
inlink->h, outlink->h); |
8fb03b4d |
return 0;
}
#define FIELD_UPPER 0
#define FIELD_LOWER 1
#define FIELD_UPPER_AND_LOWER 2
/**
* Copy picture field from src to dst.
*
* @param src_field copy from upper, lower field or both |
a853e88a |
* @param interleave leave a padding line between each copied line |
8fb03b4d |
* @param dst_field copy to upper or lower field,
* only meaningful when interleave is selected |
8997a0fa |
* @param flags context flags |
8fb03b4d |
*/
static inline |
18b46ecc |
void copy_picture_field(TInterlaceContext *tinterlace,
uint8_t *dst[4], int dst_linesize[4], |
9a2daef5 |
const uint8_t *src[4], int src_linesize[4], |
ac627b3d |
enum AVPixelFormat format, int w, int src_h, |
8997a0fa |
int src_field, int interleave, int dst_field,
int flags) |
8fb03b4d |
{ |
79393a83 |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format); |
ac3f6429 |
int hsub = desc->log2_chroma_w; |
8fb03b4d |
int plane, vsub = desc->log2_chroma_h;
int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2; |
08ee02de |
int h; |
8fb03b4d |
for (plane = 0; plane < desc->nb_components; plane++) { |
21f94684 |
int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;
int cols = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT( w, hsub) : w; |
8fb03b4d |
uint8_t *dstp = dst[plane]; |
9a2daef5 |
const uint8_t *srcp = src[plane]; |
ed48e227 |
int srcp_linesize = src_linesize[plane] * k;
int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1); |
40bfaa19 |
int clip_max = (1 << tinterlace->csp->comp[plane].depth) - 1; |
ac2a3a7a |
|
e92862e7 |
lines = (lines + (src_field == FIELD_UPPER)) / k; |
8fb03b4d |
if (src_field == FIELD_LOWER)
srcp += src_linesize[plane];
if (interleave && dst_field == FIELD_LOWER)
dstp += dst_linesize[plane]; |
2da5bf4c |
// Low-pass filtering is required when creating an interlaced destination from
// a progressive source which contains high-frequency vertical detail.
// Filtering will reduce interlace 'twitter' and Moire patterning. |
3af10603 |
if (flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)) {
int x = !!(flags & TINTERLACE_FLAG_CVLPF); |
2da5bf4c |
for (h = lines; h > 0; h--) {
ptrdiff_t pref = src_linesize[plane];
ptrdiff_t mref = -pref; |
ed48e227 |
if (h >= (lines - x)) mref = 0; // there is no line above
else if (h <= (1 + x)) pref = 0; // there is no line below |
18b46ecc |
|
40bfaa19 |
tinterlace->lowpass_line(dstp, cols, srcp, mref, pref, clip_max); |
8997a0fa |
dstp += dstp_linesize;
srcp += srcp_linesize;
}
} else { |
40bfaa19 |
if (tinterlace->csp->comp[plane].depth > 8)
cols *= 2; |
ed48e227 |
av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines); |
8997a0fa |
} |
8fb03b4d |
}
}
|
a05a44e2 |
static int filter_frame(AVFilterLink *inlink, AVFrame *picref) |
8fb03b4d |
{
AVFilterContext *ctx = inlink->dst; |
fef7b2e0 |
AVFilterLink *outlink = ctx->outputs[0]; |
8fb03b4d |
TInterlaceContext *tinterlace = ctx->priv; |
a05a44e2 |
AVFrame *cur, *next, *out; |
fef7b2e0 |
int field, tff, ret; |
8fb03b4d |
|
a05a44e2 |
av_frame_free(&tinterlace->cur); |
8fb03b4d |
tinterlace->cur = tinterlace->next;
tinterlace->next = picref;
|
fef7b2e0 |
cur = tinterlace->cur;
next = tinterlace->next; |
8fb03b4d |
/* we need at least two frames */
if (!tinterlace->cur) |
88beb2df |
return 0; |
8fb03b4d |
switch (tinterlace->mode) { |
13090895 |
case MODE_MERGEX2: /* move the odd frame into the upper field of the new image, even into
* the lower field, generating a double-height video at same framerate */ |
5f161c23 |
case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into |
8fb03b4d |
* the lower field, generating a double-height video at half framerate */ |
a05a44e2 |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
fef7b2e0 |
if (!out)
return AVERROR(ENOMEM); |
a05a44e2 |
av_frame_copy_props(out, cur);
out->height = outlink->h;
out->interlaced_frame = 1;
out->top_field_first = 1; |
3b4e694e |
out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1)); |
8fb03b4d |
/* write odd frame lines into the upper field of the new frame */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)cur->data, cur->linesize, |
8fb03b4d |
inlink->format, inlink->w, inlink->h, |
183ce55b |
FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? inlink->frame_count_out & 1 ? FIELD_LOWER : FIELD_UPPER : FIELD_UPPER, tinterlace->flags); |
8fb03b4d |
/* write even frame lines into the lower field of the new frame */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)next->data, next->linesize, |
8fb03b4d |
inlink->format, inlink->w, inlink->h, |
183ce55b |
FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? inlink->frame_count_out & 1 ? FIELD_UPPER : FIELD_LOWER : FIELD_LOWER, tinterlace->flags); |
13090895 |
if (tinterlace->mode != MODE_MERGEX2)
av_frame_free(&tinterlace->next); |
8fb03b4d |
break;
|
5f161c23 |
case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */ |
a05a44e2 |
out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next); |
6a71efff |
if (!out)
return AVERROR(ENOMEM); |
a05a44e2 |
av_frame_free(&tinterlace->next); |
8fb03b4d |
break;
|
5f161c23 |
case MODE_PAD: /* expand each frame to double height, but pad alternate
* lines with black; framerate unchanged */ |
a05a44e2 |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
ed8373e7 |
if (!out)
return AVERROR(ENOMEM); |
a05a44e2 |
av_frame_copy_props(out, cur);
out->height = outlink->h; |
3b4e694e |
out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1)); |
8fb03b4d |
field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
/* copy upper and lower fields */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)cur->data, cur->linesize, |
8fb03b4d |
inlink->format, inlink->w, inlink->h, |
8997a0fa |
FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags); |
8fb03b4d |
/* pad with black the other field */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)tinterlace->black_data, tinterlace->black_linesize, |
8fb03b4d |
inlink->format, inlink->w, inlink->h, |
8997a0fa |
FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags); |
8fb03b4d |
break;
|
42a8ac94 |
/* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
* halving the frame rate and preserving image height */ |
5f161c23 |
case MODE_INTERLEAVE_TOP: /* top field first */
case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
tff = tinterlace->mode == MODE_INTERLEAVE_TOP; |
a05a44e2 |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
fef7b2e0 |
if (!out)
return AVERROR(ENOMEM); |
a05a44e2 |
av_frame_copy_props(out, cur);
out->interlaced_frame = 1;
out->top_field_first = tff; |
8fb03b4d |
|
42a8ac94 |
/* copy upper/lower field from cur */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)cur->data, cur->linesize, |
8fb03b4d |
inlink->format, inlink->w, inlink->h, |
8997a0fa |
tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
tinterlace->flags); |
42a8ac94 |
/* copy lower/upper field from next */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)next->data, next->linesize, |
8fb03b4d |
inlink->format, inlink->w, inlink->h, |
8997a0fa |
tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
tinterlace->flags); |
a05a44e2 |
av_frame_free(&tinterlace->next); |
8fb03b4d |
break; |
5f161c23 |
case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */ |
820c0231 |
/* output current frame first */ |
a05a44e2 |
out = av_frame_clone(cur); |
fef7b2e0 |
if (!out)
return AVERROR(ENOMEM); |
a05a44e2 |
out->interlaced_frame = 1; |
0d5ae023 |
if (cur->pts != AV_NOPTS_VALUE)
out->pts = cur->pts*2; |
820c0231 |
|
59f1f764 |
out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base); |
fef7b2e0 |
if ((ret = ff_filter_frame(outlink, out)) < 0)
return ret; |
820c0231 |
/* output mix of current and next frame */ |
a05a44e2 |
tff = next->top_field_first;
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
fef7b2e0 |
if (!out)
return AVERROR(ENOMEM); |
a05a44e2 |
av_frame_copy_props(out, next);
out->interlaced_frame = 1; |
05e0ea60 |
out->top_field_first = !tff; |
820c0231 |
|
0d5ae023 |
if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
out->pts = cur->pts + next->pts;
else
out->pts = AV_NOPTS_VALUE; |
820c0231 |
/* write current frame second field lines into the second field of the new frame */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)cur->data, cur->linesize, |
820c0231 |
inlink->format, inlink->w, inlink->h, |
8997a0fa |
tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
tinterlace->flags); |
820c0231 |
/* write next frame first field lines into the first field of the new frame */ |
18b46ecc |
copy_picture_field(tinterlace, out->data, out->linesize, |
9a2daef5 |
(const uint8_t **)next->data, next->linesize, |
820c0231 |
inlink->format, inlink->w, inlink->h, |
8997a0fa |
tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
tinterlace->flags); |
820c0231 |
break; |
b11a8890 |
default:
av_assert0(0); |
8fb03b4d |
}
|
59f1f764 |
out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base); |
fef7b2e0 |
ret = ff_filter_frame(outlink, out); |
8fb03b4d |
tinterlace->frame++; |
88beb2df |
|
fef7b2e0 |
return ret; |
8fb03b4d |
}
|
2d9d4440 |
static const AVFilterPad tinterlace_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO, |
fef7b2e0 |
.filter_frame = filter_frame, |
2d9d4440 |
},
{ NULL }
};
static const AVFilterPad tinterlace_outputs[] = {
{ |
b211607b |
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_out_props, |
2d9d4440 |
},
{ NULL }
};
|
325f6e0a |
AVFilter ff_vf_tinterlace = { |
8fb03b4d |
.name = "tinterlace",
.description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
.priv_size = sizeof(TInterlaceContext),
.uninit = uninit,
.query_formats = query_formats, |
2d9d4440 |
.inputs = tinterlace_inputs,
.outputs = tinterlace_outputs, |
c6a21677 |
.priv_class = &tinterlace_class, |
8fb03b4d |
}; |