2013-04-06 13:45:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Rudolf Polzer
|
|
|
|
* Copyright (c) 2013 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser 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 telecine filter, heavily based from mpv-player:TOOLS/vf_dlopen/telecine.c by
|
|
|
|
* Rudolf Polzer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
2017-05-12 18:00:49 +00:00
|
|
|
typedef struct TelecineContext {
|
2013-04-06 13:45:52 +00:00
|
|
|
const AVClass *class;
|
|
|
|
int first_field;
|
|
|
|
char *pattern;
|
|
|
|
unsigned int pattern_pos;
|
2015-03-25 23:05:28 +00:00
|
|
|
int64_t start_time;
|
2013-04-06 13:45:52 +00:00
|
|
|
|
|
|
|
AVRational pts;
|
2015-03-27 14:05:12 +00:00
|
|
|
AVRational ts_unit;
|
2013-04-06 13:45:52 +00:00
|
|
|
int out_cnt;
|
|
|
|
int occupied;
|
|
|
|
|
|
|
|
int nb_planes;
|
|
|
|
int planeheight[4];
|
|
|
|
int stride[4];
|
|
|
|
|
|
|
|
AVFrame *frame[5];
|
|
|
|
AVFrame *temp;
|
|
|
|
} TelecineContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(TelecineContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption telecine_options[] = {
|
|
|
|
{"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
|
|
|
|
{"top", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
|
|
|
|
{"t", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
|
|
|
|
{"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
|
|
|
|
{"b", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
|
|
|
|
{"pattern", "pattern that describe for how many fields a frame is to be displayed", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str="23"}, 0, 0, FLAGS},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(telecine);
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2013-04-06 13:45:52 +00:00
|
|
|
{
|
2015-01-28 13:51:18 +00:00
|
|
|
TelecineContext *s = ctx->priv;
|
2013-04-06 13:45:52 +00:00
|
|
|
const char *p;
|
|
|
|
int max = 0;
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
if (!strlen(s->pattern)) {
|
2013-04-06 13:45:52 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
for (p = s->pattern; *p; p++) {
|
2013-04-06 13:45:52 +00:00
|
|
|
if (!av_isdigit(*p)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
max = FFMAX(*p - '0', max);
|
2015-01-28 13:51:18 +00:00
|
|
|
s->pts.num += 2;
|
|
|
|
s->pts.den += *p - '0';
|
2013-04-06 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 23:05:28 +00:00
|
|
|
s->start_time = AV_NOPTS_VALUE;
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
s->out_cnt = (max + 1) / 2;
|
2013-04-06 13:45:52 +00:00
|
|
|
av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
|
2015-01-28 13:51:18 +00:00
|
|
|
s->pattern, s->out_cnt, s->pts.num, s->pts.den);
|
2013-04-06 13:45:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2021-01-16 20:20:31 +00:00
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_formats_pixdesc_filter(&formats, 0,
|
|
|
|
AV_PIX_FMT_FLAG_BITSTREAM |
|
|
|
|
AV_PIX_FMT_FLAG_PAL |
|
|
|
|
AV_PIX_FMT_FLAG_HWACCEL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return ff_set_common_formats(ctx, formats);
|
2013-04-06 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
2015-01-28 13:51:18 +00:00
|
|
|
TelecineContext *s = inlink->dst->priv;
|
2013-04-06 13:45:52 +00:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
|
|
|
int i, ret;
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
s->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
|
|
|
|
if (!s->temp)
|
2013-04-06 13:45:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2015-01-28 13:51:18 +00:00
|
|
|
for (i = 0; i < s->out_cnt; i++) {
|
|
|
|
s->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
|
|
|
|
if (!s->frame[i])
|
2013-04-06 13:45:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
if ((ret = av_image_fill_linesizes(s->stride, inlink->format, inlink->w)) < 0)
|
2013-04-06 13:45:52 +00:00
|
|
|
return ret;
|
|
|
|
|
2016-01-27 15:19:38 +00:00
|
|
|
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
|
2015-01-28 13:51:18 +00:00
|
|
|
s->planeheight[0] = s->planeheight[3] = inlink->h;
|
2013-04-06 13:45:52 +00:00
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
s->nb_planes = av_pix_fmt_count_planes(inlink->format);
|
2013-04-06 13:45:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2015-01-28 13:51:18 +00:00
|
|
|
TelecineContext *s = ctx->priv;
|
2013-04-06 13:45:52 +00:00
|
|
|
const AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVRational fps = inlink->frame_rate;
|
|
|
|
|
|
|
|
if (!fps.num || !fps.den) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
|
|
|
|
"current rate of %d/%d is invalid\n", fps.num, fps.den);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2015-01-28 13:51:18 +00:00
|
|
|
fps = av_mul_q(fps, av_inv_q(s->pts));
|
2013-04-06 13:45:52 +00:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
|
|
|
|
inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
|
|
|
|
|
|
|
|
outlink->frame_rate = fps;
|
2015-01-28 13:51:18 +00:00
|
|
|
outlink->time_base = av_mul_q(inlink->time_base, s->pts);
|
2013-04-25 16:52:29 +00:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "TB: %d/%d -> %d/%d\n",
|
|
|
|
inlink->time_base.num, inlink->time_base.den, outlink->time_base.num, outlink->time_base.den);
|
2013-04-06 13:45:52 +00:00
|
|
|
|
2015-03-27 14:05:12 +00:00
|
|
|
s->ts_unit = av_inv_q(av_mul_q(fps, outlink->time_base));
|
2013-04-06 13:45:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2015-01-28 13:51:18 +00:00
|
|
|
TelecineContext *s = ctx->priv;
|
2013-04-06 13:45:52 +00:00
|
|
|
int i, len, ret = 0, nout = 0;
|
|
|
|
|
2015-03-25 23:05:28 +00:00
|
|
|
if (s->start_time == AV_NOPTS_VALUE)
|
|
|
|
s->start_time = inpicref->pts;
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
len = s->pattern[s->pattern_pos] - '0';
|
2013-04-06 13:45:52 +00:00
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
s->pattern_pos++;
|
|
|
|
if (!s->pattern[s->pattern_pos])
|
|
|
|
s->pattern_pos = 0;
|
2013-04-06 13:45:52 +00:00
|
|
|
|
|
|
|
if (!len) { // do not output any field from this frame
|
|
|
|
av_frame_free(&inpicref);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
if (s->occupied) {
|
2016-06-14 15:13:51 +00:00
|
|
|
av_frame_make_writable(s->frame[nout]);
|
2015-01-28 13:51:18 +00:00
|
|
|
for (i = 0; i < s->nb_planes; i++) {
|
2013-04-06 13:45:52 +00:00
|
|
|
// fill in the EARLIER field from the buffered pic
|
2015-01-28 13:51:18 +00:00
|
|
|
av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * s->first_field,
|
|
|
|
s->frame[nout]->linesize[i] * 2,
|
|
|
|
s->temp->data[i] + s->temp->linesize[i] * s->first_field,
|
|
|
|
s->temp->linesize[i] * 2,
|
|
|
|
s->stride[i],
|
|
|
|
(s->planeheight[i] - s->first_field + 1) / 2);
|
2013-04-06 13:45:52 +00:00
|
|
|
// fill in the LATER field from the new pic
|
2015-01-28 13:51:18 +00:00
|
|
|
av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * !s->first_field,
|
|
|
|
s->frame[nout]->linesize[i] * 2,
|
|
|
|
inpicref->data[i] + inpicref->linesize[i] * !s->first_field,
|
2013-04-06 13:45:52 +00:00
|
|
|
inpicref->linesize[i] * 2,
|
2015-01-28 13:51:18 +00:00
|
|
|
s->stride[i],
|
|
|
|
(s->planeheight[i] - !s->first_field + 1) / 2);
|
2013-04-06 13:45:52 +00:00
|
|
|
}
|
2020-04-04 23:00:44 +00:00
|
|
|
s->frame[nout]->interlaced_frame = 1;
|
|
|
|
s->frame[nout]->top_field_first = !s->first_field;
|
2013-04-06 13:45:52 +00:00
|
|
|
nout++;
|
|
|
|
len--;
|
2015-01-28 13:51:18 +00:00
|
|
|
s->occupied = 0;
|
2013-04-06 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (len >= 2) {
|
|
|
|
// output THIS image as-is
|
2016-06-14 15:13:51 +00:00
|
|
|
av_frame_make_writable(s->frame[nout]);
|
2015-01-28 13:51:18 +00:00
|
|
|
for (i = 0; i < s->nb_planes; i++)
|
|
|
|
av_image_copy_plane(s->frame[nout]->data[i], s->frame[nout]->linesize[i],
|
2013-04-06 13:45:52 +00:00
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
2015-01-28 13:51:18 +00:00
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
2020-04-04 23:00:44 +00:00
|
|
|
s->frame[nout]->interlaced_frame = inpicref->interlaced_frame;
|
|
|
|
s->frame[nout]->top_field_first = inpicref->top_field_first;
|
2013-04-06 13:45:52 +00:00
|
|
|
nout++;
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len >= 1) {
|
|
|
|
// copy THIS image to the buffer, we need it later
|
2015-01-28 13:51:18 +00:00
|
|
|
for (i = 0; i < s->nb_planes; i++)
|
|
|
|
av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
|
2013-04-06 13:45:52 +00:00
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
2015-01-28 13:51:18 +00:00
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
|
|
|
s->occupied = 1;
|
2013-04-06 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nout; i++) {
|
2015-01-28 13:51:18 +00:00
|
|
|
AVFrame *frame = av_frame_clone(s->frame[i]);
|
2020-04-14 10:12:42 +00:00
|
|
|
int interlaced = frame ? frame->interlaced_frame : 0;
|
|
|
|
int tff = frame ? frame->top_field_first : 0;
|
2013-04-06 13:45:52 +00:00
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
av_frame_free(&inpicref);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:04:39 +00:00
|
|
|
av_frame_copy_props(frame, inpicref);
|
2020-04-04 23:00:44 +00:00
|
|
|
frame->interlaced_frame = interlaced;
|
|
|
|
frame->top_field_first = tff;
|
2015-03-25 23:05:28 +00:00
|
|
|
frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
|
2016-08-30 13:28:41 +00:00
|
|
|
av_rescale(outlink->frame_count_in, s->ts_unit.num,
|
2015-03-27 14:05:12 +00:00
|
|
|
s->ts_unit.den);
|
2013-04-06 13:45:52 +00:00
|
|
|
ret = ff_filter_frame(outlink, frame);
|
|
|
|
}
|
|
|
|
av_frame_free(&inpicref);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2015-01-28 13:51:18 +00:00
|
|
|
TelecineContext *s = ctx->priv;
|
2013-04-06 13:45:52 +00:00
|
|
|
int i;
|
|
|
|
|
2015-01-28 13:51:18 +00:00
|
|
|
av_frame_free(&s->temp);
|
|
|
|
for (i = 0; i < s->out_cnt; i++)
|
|
|
|
av_frame_free(&s->frame[i]);
|
2013-04-06 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad telecine_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad telecine_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_telecine = {
|
2013-04-06 13:45:52 +00:00
|
|
|
.name = "telecine",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
|
|
|
|
.priv_size = sizeof(TelecineContext),
|
|
|
|
.priv_class = &telecine_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.inputs = telecine_inputs,
|
|
|
|
.outputs = telecine_outputs,
|
|
|
|
};
|