2013-03-12 13:35:21 +03:00
|
|
|
/*
|
2013-04-12 17:26:08 +03:00
|
|
|
* 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>
|
2013-03-12 13:35:21 +03:00
|
|
|
*
|
2013-04-12 17:26:08 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or modify
|
2013-03-12 13:35:21 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-04-12 17:26:08 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-03-12 13:35:21 +03:00
|
|
|
* 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
|
2013-04-12 17:26:08 +03:00
|
|
|
* with FFmpeg; if not, write to the Free Software Foundation, Inc.,
|
2013-03-12 13:35:21 +03:00
|
|
|
* 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"
|
2014-11-11 20:43:42 +02:00
|
|
|
#include "interlace.h"
|
2013-03-12 13:35:21 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(InterlaceContext, x)
|
2015-09-08 22:47:16 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
2013-05-27 12:57:30 +03:00
|
|
|
static const AVOption interlace_options[] = {
|
2013-03-12 13:35:21 +03:00
|
|
|
{ "scan", "scanning mode", OFFSET(scan),
|
2015-09-08 22:47:16 +02:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" },
|
2013-03-12 13:35:21 +03:00
|
|
|
{ "tff", "top field first", 0,
|
2015-09-08 22:47:16 +02:00
|
|
|
AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
|
2013-03-12 13:35:21 +03:00
|
|
|
{ "bff", "bottom field first", 0,
|
2015-09-08 22:47:16 +02:00
|
|
|
AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
|
2015-09-06 19:32:01 +02:00
|
|
|
{ "lowpass", "set vertical low-pass filter", OFFSET(lowpass),
|
2015-09-08 22:47:16 +02:00
|
|
|
AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, .flags = FLAGS },
|
2013-03-12 13:35:21 +03:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-05-27 12:57:30 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(interlace);
|
2013-03-12 13:35:21 +03:00
|
|
|
|
2014-11-11 20:43:42 +02:00
|
|
|
static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
|
|
|
|
const uint8_t *srcp,
|
2017-04-20 23:26:59 +02:00
|
|
|
ptrdiff_t mref, ptrdiff_t pref)
|
2014-11-11 20:43:42 +02:00
|
|
|
{
|
2017-04-20 23:26:59 +02:00
|
|
|
const uint8_t *srcp_above = srcp + mref;
|
|
|
|
const uint8_t *srcp_below = srcp + pref;
|
2014-11-11 20:43:42 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 13:35:21 +03:00
|
|
|
|
|
|
|
static const enum AVPixelFormat formats_supported[] = {
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
|
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
|
|
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
|
|
|
|
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2015-04-03 19:55:18 +02:00
|
|
|
AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
|
|
|
|
if (!fmts_list)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_set_common_formats(ctx, fmts_list);
|
2013-03-12 13:35:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
InterlaceContext *s = ctx->priv;
|
|
|
|
|
|
|
|
av_frame_free(&s->cur);
|
|
|
|
av_frame_free(&s->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-07-30 23:12:14 +03:00
|
|
|
|
|
|
|
if (!s->lowpass)
|
2014-12-02 16:58:42 +02:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
|
2014-07-30 23:12:14 +03:00
|
|
|
"the resulting video will be aliased rather than interlaced.\n");
|
|
|
|
|
2013-03-12 13:35:21 +03:00
|
|
|
// same input size
|
|
|
|
outlink->w = inlink->w;
|
|
|
|
outlink->h = inlink->h;
|
|
|
|
outlink->time_base = inlink->time_base;
|
2013-04-12 17:40:35 +03:00
|
|
|
outlink->frame_rate = inlink->frame_rate;
|
2013-03-12 13:35:21 +03:00
|
|
|
// half framerate
|
2014-12-02 19:48:03 +02:00
|
|
|
outlink->time_base.num *= 2;
|
2013-04-12 17:40:35 +03:00
|
|
|
outlink->frame_rate.den *= 2;
|
2013-03-12 13:35:21 +03:00
|
|
|
|
2014-11-11 20:43:42 +02:00
|
|
|
|
|
|
|
if (s->lowpass) {
|
|
|
|
s->lowpass_line = lowpass_line_c;
|
|
|
|
if (ARCH_X86)
|
|
|
|
ff_interlace_init_x86(s);
|
|
|
|
}
|
|
|
|
|
2014-07-30 23:12:14 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
|
|
|
|
s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
|
2013-03-12 13:35:21 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-11 20:43:42 +02:00
|
|
|
static void copy_picture_field(InterlaceContext *s,
|
|
|
|
AVFrame *src_frame, AVFrame *dst_frame,
|
2014-07-30 23:12:14 +03:00
|
|
|
AVFilterLink *inlink, enum FieldType field_type,
|
|
|
|
int lowpass)
|
2013-03-12 13:35:21 +03:00
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2014-12-02 16:58:38 +02:00
|
|
|
int hsub = desc->log2_chroma_w;
|
2013-03-12 13:35:21 +03:00
|
|
|
int vsub = desc->log2_chroma_h;
|
2014-11-11 20:43:42 +02:00
|
|
|
int plane, j;
|
2013-03-12 13:35:21 +03:00
|
|
|
|
|
|
|
for (plane = 0; plane < desc->nb_components; plane++) {
|
2014-12-02 16:58:38 +02:00
|
|
|
int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
|
2016-01-27 17:19:38 +02:00
|
|
|
int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
|
2013-03-12 13:35:21 +03:00
|
|
|
uint8_t *dstp = dst_frame->data[plane];
|
|
|
|
const uint8_t *srcp = src_frame->data[plane];
|
|
|
|
|
2014-12-02 16:58:39 +02:00
|
|
|
av_assert0(cols >= 0 || lines >= 0);
|
2013-03-12 13:35:21 +03:00
|
|
|
|
2013-07-09 18:57:49 +03:00
|
|
|
lines = (lines + (field_type == FIELD_UPPER)) / 2;
|
2014-12-02 16:58:41 +02:00
|
|
|
if (field_type == FIELD_LOWER) {
|
2013-03-12 13:35:21 +03:00
|
|
|
srcp += src_frame->linesize[plane];
|
|
|
|
dstp += dst_frame->linesize[plane];
|
2014-12-02 16:58:41 +02:00
|
|
|
}
|
2014-07-30 23:12:14 +03:00
|
|
|
if (lowpass) {
|
|
|
|
int srcp_linesize = src_frame->linesize[plane] * 2;
|
|
|
|
int dstp_linesize = dst_frame->linesize[plane] * 2;
|
|
|
|
for (j = lines; j > 0; j--) {
|
2017-04-20 23:26:59 +02:00
|
|
|
ptrdiff_t pref = src_frame->linesize[plane];
|
|
|
|
ptrdiff_t mref = -pref;
|
2014-07-30 23:12:14 +03:00
|
|
|
if (j == lines)
|
2017-04-20 23:26:59 +02:00
|
|
|
mref = 0; // there is no line above
|
|
|
|
else if (j == 1)
|
|
|
|
pref = 0; // there is no line below
|
|
|
|
s->lowpass_line(dstp, cols, srcp, mref, pref);
|
2014-07-30 23:12:14 +03:00
|
|
|
dstp += dstp_linesize;
|
|
|
|
srcp += srcp_linesize;
|
2013-03-12 13:35:21 +03:00
|
|
|
}
|
2014-07-30 23:12:14 +03:00
|
|
|
} else {
|
|
|
|
av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
|
|
|
|
srcp, src_frame->linesize[plane] * 2,
|
2014-12-02 16:58:38 +02:00
|
|
|
cols, lines);
|
2013-03-12 13:35:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-10-01 20:08:03 +03:00
|
|
|
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);
|
2013-11-11 03:04:17 +03:00
|
|
|
if (!out)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-10-01 20:08:03 +03:00
|
|
|
out->pts /= 2; // adjust pts to new framerate
|
|
|
|
ret = ff_filter_frame(outlink, out);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-12 13:35:21 +03:00
|
|
|
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;
|
2014-12-02 19:48:03 +02:00
|
|
|
out->pts /= 2; // adjust pts to new framerate
|
2013-03-12 13:35:21 +03:00
|
|
|
|
|
|
|
/* copy upper/lower field from cur */
|
2014-11-11 20:43:42 +02:00
|
|
|
copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
|
2013-03-12 13:35:21 +03:00
|
|
|
av_frame_free(&s->cur);
|
|
|
|
|
|
|
|
/* copy lower/upper field from next */
|
2014-11-11 20:43:42 +02:00
|
|
|
copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
|
2013-03-12 13:35:21 +03:00
|
|
|
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[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_out_props,
|
2013-03-12 13:35:21 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 09:44:24 +03:00
|
|
|
AVFilter ff_vf_interlace = {
|
2013-03-12 13:35:21 +03:00
|
|
|
.name = "interlace",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
|
|
|
|
.uninit = uninit,
|
2013-05-27 12:57:30 +03:00
|
|
|
.priv_class = &interlace_class,
|
2013-03-12 13:35:21 +03:00
|
|
|
.priv_size = sizeof(InterlaceContext),
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.inputs = inputs,
|
|
|
|
.outputs = outputs,
|
|
|
|
};
|