2015-03-27 16:06:05 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Himangi Saraogi <himangi774@gmail.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 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 detelecine filter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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 20:00:49 +02:00
|
|
|
typedef struct DetelecineContext {
|
2015-03-27 16:06:05 +02:00
|
|
|
const AVClass *class;
|
|
|
|
int first_field;
|
|
|
|
char *pattern;
|
|
|
|
int start_frame;
|
2016-03-18 00:10:00 +02:00
|
|
|
int init_len;
|
2015-03-27 16:06:05 +02:00
|
|
|
unsigned int pattern_pos;
|
|
|
|
unsigned int nskip_fields;
|
|
|
|
int64_t start_time;
|
|
|
|
|
|
|
|
AVRational pts;
|
|
|
|
AVRational ts_unit;
|
|
|
|
int occupied;
|
|
|
|
|
|
|
|
int nb_planes;
|
|
|
|
int planeheight[4];
|
|
|
|
int stride[4];
|
|
|
|
|
2016-03-18 00:09:59 +02:00
|
|
|
AVFrame *frame[2];
|
2015-03-27 16:06:05 +02:00
|
|
|
AVFrame *temp;
|
|
|
|
} DetelecineContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(DetelecineContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption detelecine_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},
|
|
|
|
{"start_frame", "position of first frame with respect to the pattern if stream is cut", OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64=0}, 0, 13, FLAGS},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(detelecine);
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
DetelecineContext *s = ctx->priv;
|
|
|
|
const char *p;
|
|
|
|
int max = 0;
|
2016-03-18 00:10:00 +02:00
|
|
|
int sum = 0;
|
2015-03-27 16:06:05 +02:00
|
|
|
|
|
|
|
if (!strlen(s->pattern)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = s->pattern; *p; p++) {
|
|
|
|
if (!av_isdigit(*p)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2016-03-18 00:10:00 +02:00
|
|
|
sum += *p - '0';
|
2015-03-27 16:06:05 +02:00
|
|
|
max = FFMAX(*p - '0', max);
|
|
|
|
s->pts.num += *p - '0';
|
|
|
|
s->pts.den += 2;
|
|
|
|
}
|
|
|
|
|
2016-03-18 00:10:00 +02:00
|
|
|
if (s->start_frame >= sum) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Provided start_frame is too big.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-03-27 16:06:05 +02:00
|
|
|
s->nskip_fields = 0;
|
|
|
|
s->pattern_pos = 0;
|
|
|
|
s->start_time = AV_NOPTS_VALUE;
|
2016-03-18 00:10:00 +02:00
|
|
|
s->init_len = 0;
|
2015-03-27 16:06:05 +02:00
|
|
|
|
|
|
|
if (s->start_frame != 0) {
|
|
|
|
int nfields = 0;
|
|
|
|
for (p = s->pattern; *p; p++) {
|
|
|
|
nfields += *p - '0';
|
|
|
|
s->pattern_pos++;
|
|
|
|
if (nfields >= 2*s->start_frame) {
|
2016-03-18 00:10:00 +02:00
|
|
|
s->init_len = nfields - 2*s->start_frame;
|
2015-03-27 16:06:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_INFO, "Detelecine pattern %s removes up to %d frames per frame, pts advance factor: %d/%d\n",
|
|
|
|
s->pattern, (max + 1) / 2, s->pts.num, s->pts.den);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *pix_fmts = NULL;
|
2015-10-05 05:39:25 +02:00
|
|
|
int fmt, ret;
|
2015-03-27 16:06:05 +02:00
|
|
|
|
|
|
|
for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
|
|
|
|
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
|
|
|
|
desc->flags & AV_PIX_FMT_FLAG_PAL ||
|
2015-10-05 05:39:25 +02:00
|
|
|
desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
|
|
|
|
(ret = ff_add_format(&pix_fmts, fmt)) < 0)
|
|
|
|
return ret;
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 19:55:18 +02:00
|
|
|
return ff_set_common_formats(ctx, pix_fmts);
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
DetelecineContext *s = inlink->dst->priv;
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
s->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
|
|
|
|
if (!s->temp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-03-18 00:09:59 +02:00
|
|
|
s->frame[0] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
|
|
|
|
if (!s->frame[0])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->frame[1] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
|
|
|
|
if (!s->frame[1])
|
2015-03-27 16:06:05 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if ((ret = av_image_fill_linesizes(s->stride, inlink->format, inlink->w)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-01-27 17:19:38 +02:00
|
|
|
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
|
2015-03-27 16:06:05 +02:00
|
|
|
s->planeheight[0] = s->planeheight[3] = inlink->h;
|
|
|
|
|
|
|
|
s->nb_planes = av_pix_fmt_count_planes(inlink->format);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
DetelecineContext *s = ctx->priv;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
fps = av_mul_q(fps, av_inv_q(s->pts));
|
|
|
|
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;
|
|
|
|
outlink->time_base = av_mul_q(inlink->time_base, s->pts);
|
|
|
|
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);
|
|
|
|
|
|
|
|
s->ts_unit = av_inv_q(av_mul_q(fps, outlink->time_base));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
DetelecineContext *s = ctx->priv;
|
|
|
|
int i, len = 0, ret = 0, out = 0;
|
|
|
|
|
|
|
|
if (s->start_time == AV_NOPTS_VALUE)
|
|
|
|
s->start_time = inpicref->pts;
|
|
|
|
|
|
|
|
if (s->nskip_fields >= 2) {
|
|
|
|
s->nskip_fields -= 2;
|
2019-01-02 16:34:41 +02:00
|
|
|
av_frame_free(&inpicref);
|
2015-03-27 16:06:05 +02:00
|
|
|
return 0;
|
|
|
|
} else if (s->nskip_fields >= 1) {
|
2016-03-14 14:44:24 +02:00
|
|
|
for (i = 0; i < s->nb_planes; i++) {
|
|
|
|
av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
|
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
2016-03-14 14:44:24 +02:00
|
|
|
s->occupied = 1;
|
|
|
|
s->nskip_fields--;
|
2019-01-02 16:34:41 +02:00
|
|
|
av_frame_free(&inpicref);
|
2016-03-14 14:44:24 +02:00
|
|
|
return 0;
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->nskip_fields == 0) {
|
2016-03-18 00:10:00 +02:00
|
|
|
len = s->init_len;
|
|
|
|
s->init_len = 0;
|
2015-03-27 16:06:05 +02:00
|
|
|
while(!len && s->pattern[s->pattern_pos]) {
|
|
|
|
len = s->pattern[s->pattern_pos] - '0';
|
|
|
|
s->pattern_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->pattern[s->pattern_pos])
|
|
|
|
s->pattern_pos = 0;
|
|
|
|
|
|
|
|
if(!len) { // do not output any field as the entire pattern is zero
|
|
|
|
av_frame_free(&inpicref);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-18 00:09:59 +02:00
|
|
|
if (len == 1 && s->occupied) {
|
|
|
|
s->occupied = 0;
|
|
|
|
// output THIS image as-is
|
|
|
|
for (i = 0; i < s->nb_planes; i++)
|
|
|
|
av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
|
|
|
|
s->temp->data[i], s->temp->linesize[i],
|
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
|
|
|
len = 0;
|
|
|
|
while(!len && s->pattern[s->pattern_pos]) {
|
|
|
|
len = s->pattern[s->pattern_pos] - '0';
|
|
|
|
s->pattern_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->pattern[s->pattern_pos])
|
|
|
|
s->pattern_pos = 0;
|
|
|
|
|
|
|
|
s->occupied = 0;
|
|
|
|
++out;
|
|
|
|
}
|
|
|
|
|
2015-03-27 16:06:05 +02:00
|
|
|
if (s->occupied) {
|
|
|
|
for (i = 0; i < s->nb_planes; i++) {
|
|
|
|
// fill in the EARLIER field from the new pic
|
2016-03-18 00:09:59 +02:00
|
|
|
av_image_copy_plane(s->frame[out]->data[i] + s->frame[out]->linesize[i] * s->first_field,
|
|
|
|
s->frame[out]->linesize[i] * 2,
|
2015-03-27 16:06:05 +02:00
|
|
|
inpicref->data[i] + inpicref->linesize[i] * s->first_field,
|
|
|
|
inpicref->linesize[i] * 2,
|
|
|
|
s->stride[i],
|
|
|
|
(s->planeheight[i] - s->first_field + 1) / 2);
|
|
|
|
// fill in the LATER field from the buffered pic
|
2016-03-18 00:09:59 +02:00
|
|
|
av_image_copy_plane(s->frame[out]->data[i] + s->frame[out]->linesize[i] * !s->first_field,
|
|
|
|
s->frame[out]->linesize[i] * 2,
|
2015-03-27 16:06:05 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-03-14 14:44:24 +02:00
|
|
|
|
|
|
|
s->occupied = 0;
|
|
|
|
if (len <= 2) {
|
|
|
|
for (i = 0; i < s->nb_planes; i++) {
|
|
|
|
av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
|
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
|
|
|
}
|
|
|
|
s->occupied = 1;
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
2016-03-18 00:09:59 +02:00
|
|
|
++out;
|
2016-03-14 14:44:24 +02:00
|
|
|
len = (len >= 3) ? len - 3 : 0;
|
2015-03-27 16:06:05 +02:00
|
|
|
} else {
|
|
|
|
if (len >= 2) {
|
|
|
|
// output THIS image as-is
|
|
|
|
for (i = 0; i < s->nb_planes; i++)
|
2016-03-18 00:09:59 +02:00
|
|
|
av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
|
2015-03-27 16:06:05 +02:00
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
|
|
|
len -= 2;
|
2016-03-18 00:09:59 +02:00
|
|
|
++out;
|
2015-03-27 16:06:05 +02:00
|
|
|
} else if (len == 1) {
|
2016-03-18 00:09:59 +02:00
|
|
|
// output THIS image as-is
|
|
|
|
for (i = 0; i < s->nb_planes; i++)
|
|
|
|
av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
|
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
2015-03-31 09:13:46 +02:00
|
|
|
|
2016-03-18 00:09:59 +02:00
|
|
|
for (i = 0; i < s->nb_planes; i++) {
|
|
|
|
av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
|
|
|
|
inpicref->data[i], inpicref->linesize[i],
|
|
|
|
s->stride[i],
|
|
|
|
s->planeheight[i]);
|
|
|
|
}
|
|
|
|
s->occupied = 1;
|
2015-03-27 16:06:05 +02:00
|
|
|
|
|
|
|
len--;
|
2016-03-18 00:09:59 +02:00
|
|
|
++out;
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 1 && s->occupied)
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
s->occupied = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->nskip_fields = len;
|
|
|
|
|
2016-03-19 13:26:14 +02:00
|
|
|
for (i = 0; i < out; ++i) {
|
2016-03-18 00:09:59 +02:00
|
|
|
AVFrame *frame = av_frame_clone(s->frame[i]);
|
2015-03-27 16:06:05 +02:00
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
av_frame_free(&inpicref);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_copy_props(frame, inpicref);
|
|
|
|
frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
|
2016-08-30 15:28:41 +02:00
|
|
|
av_rescale(outlink->frame_count_in, s->ts_unit.num,
|
2015-03-27 16:06:05 +02:00
|
|
|
s->ts_unit.den);
|
|
|
|
ret = ff_filter_frame(outlink, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&inpicref);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
DetelecineContext *s = ctx->priv;
|
|
|
|
|
|
|
|
av_frame_free(&s->temp);
|
2016-03-18 00:09:59 +02:00
|
|
|
av_frame_free(&s->frame[0]);
|
|
|
|
av_frame_free(&s->frame[1]);
|
2015-03-27 16:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad detelecine_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad detelecine_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter ff_vf_detelecine = {
|
|
|
|
.name = "detelecine",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply an inverse telecine pattern."),
|
|
|
|
.priv_size = sizeof(DetelecineContext),
|
|
|
|
.priv_class = &detelecine_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.inputs = detelecine_inputs,
|
|
|
|
.outputs = detelecine_outputs,
|
|
|
|
};
|