3fce1367 |
/* |
ebbd4fd5 |
* Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
* Copyright (c) 2010 Baptiste Coudurier
* Copyright (c) 2011 Stefano Sabatini
* Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.com> |
2da5bf4c |
* Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com> |
3fce1367 |
* |
ebbd4fd5 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or modify |
3fce1367 |
* 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.
* |
ebbd4fd5 |
* FFmpeg is distributed in the hope that it will be useful, |
3fce1367 |
* 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 |
ebbd4fd5 |
* with FFmpeg; if not, write to the Free Software Foundation, Inc., |
3fce1367 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file
* progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
*/
#include "libavutil/common.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavutil/avassert.h"
#include "formats.h"
#include "avfilter.h" |
2e170405 |
#include "interlace.h" |
3fce1367 |
#include "internal.h"
#include "video.h"
#define OFFSET(x) offsetof(InterlaceContext, x) |
0b93c6d8 |
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
d349704e |
static const AVOption interlace_options[] = { |
3fce1367 |
{ "scan", "scanning mode", OFFSET(scan), |
0b93c6d8 |
AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" }, |
3fce1367 |
{ "tff", "top field first", 0, |
0b93c6d8 |
AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" }, |
3fce1367 |
{ "bff", "bottom field first", 0, |
0b93c6d8 |
AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" }, |
f790b54d |
{ "lowpass", "set vertical low-pass filter", OFFSET(lowpass), |
2da5bf4c |
AV_OPT_TYPE_INT, {.i64 = VLPF_LIN }, 0, 2, .flags = FLAGS, .unit = "lowpass" },
{ "off", "disable vertical low-pass filter", 0,
AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
{ "linear", "linear vertical low-pass filter", 0,
AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
{ "complex", "complex vertical low-pass filter", 0,
AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" }, |
3fce1367 |
{ NULL }
};
|
d349704e |
AVFILTER_DEFINE_CLASS(interlace); |
3fce1367 |
|
2e170405 |
static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize, |
40bfaa19 |
const uint8_t *srcp, ptrdiff_t mref,
ptrdiff_t pref, int clip_max) |
2e170405 |
{ |
207e6deb |
const uint8_t *srcp_above = srcp + mref;
const uint8_t *srcp_below = srcp + pref; |
2e170405 |
int i;
for (i = 0; i < linesize; 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;
}
} |
3fce1367 |
|
40bfaa19 |
static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t linesize,
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 < linesize; 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 linesize, |
40bfaa19 |
const uint8_t *srcp, 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 < linesize; 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 linesize,
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 < linesize; 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);
}
}
|
3fce1367 |
static const enum AVPixelFormat formats_supported[] = { |
40bfaa19 |
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV444P10LE,
AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE, AV_PIX_FMT_YUV444P12LE,
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE |
3fce1367 |
};
static int query_formats(AVFilterContext *ctx)
{ |
a0854c08 |
AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
if (!fmts_list)
return AVERROR(ENOMEM);
return ff_set_common_formats(ctx, fmts_list); |
3fce1367 |
}
static av_cold void uninit(AVFilterContext *ctx)
{
InterlaceContext *s = ctx->priv;
av_frame_free(&s->cur);
av_frame_free(&s->next);
}
|
d31770d9 |
void ff_interlace_init(InterlaceContext *s, int depth)
{
if (s->lowpass) {
if (s->lowpass == VLPF_LIN) {
if (depth > 8)
s->lowpass_line = lowpass_line_c_16;
else
s->lowpass_line = lowpass_line_c;
} else if (s->lowpass == VLPF_CMP) {
if (depth > 8)
s->lowpass_line = lowpass_line_complex_c_16;
else
s->lowpass_line = lowpass_line_complex_c;
}
if (ARCH_X86)
ff_interlace_init_x86(s, depth);
}
}
|
3fce1367 |
static int config_out_props(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = outlink->src->inputs[0];
InterlaceContext *s = ctx->priv;
if (inlink->h < 2) {
av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
return AVERROR_INVALIDDATA;
} |
52269f48 |
if (!s->lowpass) |
4438c256 |
av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, " |
52269f48 |
"the resulting video will be aliased rather than interlaced.\n");
|
3fce1367 |
// same input size
outlink->w = inlink->w;
outlink->h = inlink->h;
outlink->time_base = inlink->time_base; |
227b4458 |
outlink->frame_rate = inlink->frame_rate; |
3fce1367 |
// half framerate |
46b64e30 |
outlink->time_base.num *= 2; |
227b4458 |
outlink->frame_rate.den *= 2; |
3fce1367 |
|
40bfaa19 |
s->csp = av_pix_fmt_desc_get(outlink->format); |
d31770d9 |
ff_interlace_init(s, s->csp->comp[0].depth); |
2e170405 |
|
52269f48 |
av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without"); |
3fce1367 |
return 0;
}
|
2e170405 |
static void copy_picture_field(InterlaceContext *s,
AVFrame *src_frame, AVFrame *dst_frame, |
52269f48 |
AVFilterLink *inlink, enum FieldType field_type,
int lowpass) |
3fce1367 |
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
696141e8 |
int hsub = desc->log2_chroma_w; |
3fce1367 |
int vsub = desc->log2_chroma_h; |
2e170405 |
int plane, j; |
3fce1367 |
for (plane = 0; plane < desc->nb_components; plane++) { |
696141e8 |
int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w; |
21f94684 |
int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h; |
3fce1367 |
uint8_t *dstp = dst_frame->data[plane];
const uint8_t *srcp = src_frame->data[plane]; |
ed48e227 |
int srcp_linesize = src_frame->linesize[plane] * 2;
int dstp_linesize = dst_frame->linesize[plane] * 2; |
40bfaa19 |
int clip_max = (1 << s->csp->comp[plane].depth) - 1; |
3fce1367 |
|
046f75a9 |
av_assert0(cols >= 0 || lines >= 0); |
3fce1367 |
|
bec509db |
lines = (lines + (field_type == FIELD_UPPER)) / 2; |
15ea2227 |
if (field_type == FIELD_LOWER) { |
3fce1367 |
srcp += src_frame->linesize[plane];
dstp += dst_frame->linesize[plane]; |
15ea2227 |
} |
ed48e227 |
if (lowpass) {
int x = 0;
if (lowpass == VLPF_CMP)
x = 1; |
52269f48 |
for (j = lines; j > 0; j--) { |
207e6deb |
ptrdiff_t pref = src_frame->linesize[plane];
ptrdiff_t mref = -pref; |
ed48e227 |
if (j >= (lines - x)) |
2da5bf4c |
mref = 0; |
ed48e227 |
else if (j <= (1 + x)) |
2da5bf4c |
pref = 0; |
40bfaa19 |
s->lowpass_line(dstp, cols, srcp, mref, pref, clip_max); |
2da5bf4c |
dstp += dstp_linesize;
srcp += srcp_linesize;
} |
52269f48 |
} else { |
40bfaa19 |
if (s->csp->comp[plane].depth > 8)
cols *= 2; |
ed48e227 |
av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines); |
3fce1367 |
}
}
}
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
{
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
InterlaceContext *s = ctx->priv;
AVFrame *out;
int tff, ret;
av_frame_free(&s->cur);
s->cur = s->next;
s->next = buf;
/* we need at least two frames */
if (!s->cur || !s->next)
return 0;
|
06e7301a |
if (s->cur->interlaced_frame) {
av_log(ctx, AV_LOG_WARNING,
"video is already interlaced, adjusting framerate only\n");
out = av_frame_clone(s->cur); |
3a16ec19 |
if (!out)
return AVERROR(ENOMEM); |
06e7301a |
out->pts /= 2; // adjust pts to new framerate
ret = ff_filter_frame(outlink, out);
return ret;
}
|
3fce1367 |
tff = (s->scan == MODE_TFF);
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!out)
return AVERROR(ENOMEM);
av_frame_copy_props(out, s->cur);
out->interlaced_frame = 1;
out->top_field_first = tff; |
46b64e30 |
out->pts /= 2; // adjust pts to new framerate |
3fce1367 |
/* copy upper/lower field from cur */ |
2e170405 |
copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass); |
3fce1367 |
av_frame_free(&s->cur);
/* copy lower/upper field from next */ |
2e170405 |
copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass); |
3fce1367 |
av_frame_free(&s->next);
ret = ff_filter_frame(outlink, out);
return ret;
}
static const AVFilterPad inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame,
},
{ NULL }
};
static const AVFilterPad outputs[] = {
{ |
b211607b |
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_out_props, |
3fce1367 |
},
{ NULL }
};
|
cd43ca04 |
AVFilter ff_vf_interlace = { |
3fce1367 |
.name = "interlace",
.description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
.uninit = uninit, |
d349704e |
.priv_class = &interlace_class, |
3fce1367 |
.priv_size = sizeof(InterlaceContext),
.query_formats = query_formats,
.inputs = inputs,
.outputs = outputs,
}; |