2012-05-09 14:08:21 +02:00
|
|
|
/*
|
2012-05-22 21:29:24 +02:00
|
|
|
* Copyright 2007 Bobby Bingham
|
|
|
|
* Copyright 2012 Robert Nagy <ronag89 gmail com>
|
|
|
|
* Copyright 2012 Anton Khirnov <anton khirnov net>
|
|
|
|
*
|
2012-05-22 21:27:54 +02:00
|
|
|
* This file is part of FFmpeg.
|
2012-05-09 14:08:21 +02:00
|
|
|
*
|
2012-05-22 21:27:54 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-05-09 14:08:21 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2012-05-22 21:27:54 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-05-09 14:08:21 +02:00
|
|
|
* 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
|
2012-05-22 21:27:54 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-05-09 14:08:21 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* a filter enforcing given constant framerate
|
|
|
|
*/
|
|
|
|
|
2012-08-06 15:49:32 +02:00
|
|
|
#include "libavutil/common.h"
|
2012-05-09 14:08:21 +02:00
|
|
|
#include "libavutil/fifo.h"
|
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
|
|
|
|
#include "avfilter.h"
|
2012-05-30 11:20:32 +02:00
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
typedef struct FPSContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps
|
|
|
|
|
|
|
|
/* timestamps in input timebase */
|
|
|
|
int64_t first_pts; ///< pts of the first frame that arrived on this filter
|
|
|
|
int64_t pts; ///< pts of the first frame currently in the fifo
|
|
|
|
|
|
|
|
AVRational framerate; ///< target framerate
|
lavfi/vf_fps: allow to set the rounding method.
By default, fps selects frames near PTS 0, 0.5, 1.5, 2.5, etc.
With round=down, it selects the frames near PTS 0, 1, 2, 3, etc.
2012-10-25 17:58:08 +02:00
|
|
|
int rounding; ///< AVRounding method for timestamps
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
/* statistics */
|
|
|
|
int frames_in; ///< number of frames on input
|
|
|
|
int frames_out; ///< number of frames on output
|
|
|
|
int dup; ///< number of frames duplicated
|
|
|
|
int drop; ///< number of framed dropped
|
|
|
|
} FPSContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(FPSContext, x)
|
|
|
|
#define V AV_OPT_FLAG_VIDEO_PARAM
|
2012-08-13 13:40:01 +02:00
|
|
|
#define F AV_OPT_FLAG_FILTERING_PARAM
|
2012-06-22 14:33:09 +02:00
|
|
|
static const AVOption fps_options[] = {
|
2013-03-26 13:01:18 +01:00
|
|
|
{ "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = V|F },
|
lavfi/vf_fps: allow to set the rounding method.
By default, fps selects frames near PTS 0, 0.5, 1.5, 2.5, etc.
With round=down, it selects the frames near PTS 0, 1, 2, 3, etc.
2012-10-25 17:58:08 +02:00
|
|
|
{ "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
|
|
|
|
{ "zero", "round towards 0", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO }, 0, 5, V|F, "round" },
|
|
|
|
{ "inf", "round away from 0", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF }, 0, 5, V|F, "round" },
|
|
|
|
{ "down", "round towards -infty", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN }, 0, 5, V|F, "round" },
|
|
|
|
{ "up", "round towards +infty", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP }, 0, 5, V|F, "round" },
|
|
|
|
{ "near", "round to nearest", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
|
2012-05-09 14:08:21 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2012-06-22 14:33:09 +02:00
|
|
|
AVFILTER_DEFINE_CLASS(fps);
|
2012-05-09 14:08:21 +02:00
|
|
|
|
2013-03-13 08:26:39 +01:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2012-05-09 14:08:21 +02:00
|
|
|
{
|
|
|
|
FPSContext *s = ctx->priv;
|
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFrame*))))
|
2012-05-09 14:08:21 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_fifo(AVFifoBuffer *fifo)
|
|
|
|
{
|
|
|
|
while (av_fifo_size(fifo)) {
|
2012-11-28 08:41:07 +01:00
|
|
|
AVFrame *tmp;
|
2012-05-09 14:08:21 +02:00
|
|
|
av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
|
2012-11-28 08:41:07 +01:00
|
|
|
av_frame_free(&tmp);
|
2012-05-09 14:08:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
FPSContext *s = ctx->priv;
|
|
|
|
if (s->fifo) {
|
2013-03-10 01:30:30 +01:00
|
|
|
s->drop += av_fifo_size(s->fifo) / sizeof(AVFrame*);
|
2012-05-09 14:08:21 +02:00
|
|
|
flush_fifo(s->fifo);
|
|
|
|
av_fifo_free(s->fifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
|
|
|
|
"%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink* link)
|
|
|
|
{
|
|
|
|
FPSContext *s = link->src->priv;
|
|
|
|
|
2012-08-05 23:25:27 +02:00
|
|
|
link->time_base = av_inv_q(s->framerate);
|
2012-06-10 10:53:03 +02:00
|
|
|
link->frame_rate= s->framerate;
|
2012-05-09 14:08:21 +02:00
|
|
|
link->w = link->src->inputs[0]->w;
|
|
|
|
link->h = link->src->inputs[0]->h;
|
|
|
|
s->pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
FPSContext *s = ctx->priv;
|
|
|
|
int frames_out = s->frames_out;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
while (ret >= 0 && s->frames_out == frames_out)
|
2012-05-30 11:20:32 +02:00
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
/* flush the fifo */
|
|
|
|
if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; av_fifo_size(s->fifo); i++) {
|
2012-11-28 08:41:07 +01:00
|
|
|
AVFrame *buf;
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
|
|
|
|
buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
|
|
|
|
outlink->time_base) + s->frames_out;
|
|
|
|
|
2012-11-28 19:28:41 +01:00
|
|
|
if ((ret = ff_filter_frame(outlink, buf)) < 0)
|
2012-07-15 09:47:01 +02:00
|
|
|
return ret;
|
|
|
|
|
2012-05-09 14:08:21 +02:00
|
|
|
s->frames_out++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf)
|
2012-05-09 14:08:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!av_fifo_space(fifo) &&
|
2012-07-15 09:50:49 +02:00
|
|
|
(ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
|
2012-11-28 08:41:07 +01:00
|
|
|
av_frame_free(&buf);
|
2012-05-09 14:08:21 +02:00
|
|
|
return ret;
|
2012-07-15 09:50:49 +02:00
|
|
|
}
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
|
2012-05-09 14:08:21 +02:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
FPSContext *s = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
int64_t delta;
|
2012-07-14 09:25:33 +02:00
|
|
|
int i, ret;
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
s->frames_in++;
|
|
|
|
/* discard frames until we get the first timestamp */
|
|
|
|
if (s->pts == AV_NOPTS_VALUE) {
|
|
|
|
if (buf->pts != AV_NOPTS_VALUE) {
|
2012-07-15 10:07:59 +02:00
|
|
|
ret = write_to_fifo(s->fifo, buf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-05-09 14:08:21 +02:00
|
|
|
s->first_pts = s->pts = buf->pts;
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
|
|
|
|
"timestamp.\n");
|
2012-11-28 08:41:07 +01:00
|
|
|
av_frame_free(&buf);
|
2012-05-09 14:08:21 +02:00
|
|
|
s->drop++;
|
|
|
|
}
|
2012-07-14 09:25:33 +02:00
|
|
|
return 0;
|
2012-05-09 14:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now wait for the next timestamp */
|
|
|
|
if (buf->pts == AV_NOPTS_VALUE) {
|
2012-07-14 09:25:33 +02:00
|
|
|
return write_to_fifo(s->fifo, buf);
|
2012-05-09 14:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* number of output frames */
|
lavfi/vf_fps: allow to set the rounding method.
By default, fps selects frames near PTS 0, 0.5, 1.5, 2.5, etc.
With round=down, it selects the frames near PTS 0, 1, 2, 3, etc.
2012-10-25 17:58:08 +02:00
|
|
|
delta = av_rescale_q_rnd(buf->pts - s->pts, inlink->time_base,
|
|
|
|
outlink->time_base, s->rounding);
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
if (delta < 1) {
|
|
|
|
/* drop the frame and everything buffered except the first */
|
2012-11-28 08:41:07 +01:00
|
|
|
AVFrame *tmp;
|
|
|
|
int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*);
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
|
|
|
|
s->drop += drop;
|
|
|
|
|
|
|
|
av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
|
|
|
|
flush_fifo(s->fifo);
|
2012-07-14 09:25:33 +02:00
|
|
|
ret = write_to_fifo(s->fifo, tmp);
|
2012-05-09 14:08:21 +02:00
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
av_frame_free(&buf);
|
2012-07-14 09:25:33 +02:00
|
|
|
return ret;
|
2012-05-09 14:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* can output >= 1 frames */
|
|
|
|
for (i = 0; i < delta; i++) {
|
2012-11-28 08:41:07 +01:00
|
|
|
AVFrame *buf_out;
|
2012-05-09 14:08:21 +02:00
|
|
|
av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
|
|
|
|
|
|
|
|
/* duplicate the frame if needed */
|
|
|
|
if (!av_fifo_size(s->fifo) && i < delta - 1) {
|
2012-11-28 08:41:07 +01:00
|
|
|
AVFrame *dup = av_frame_clone(buf_out);
|
2012-07-15 10:07:59 +02:00
|
|
|
|
2012-05-09 14:08:21 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
|
2012-07-15 10:07:59 +02:00
|
|
|
if (dup)
|
|
|
|
ret = write_to_fifo(s->fifo, dup);
|
|
|
|
else
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2012-11-28 08:41:07 +01:00
|
|
|
av_frame_free(&buf_out);
|
|
|
|
av_frame_free(&buf);
|
2012-07-15 10:07:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-09 14:08:21 +02:00
|
|
|
s->dup++;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
|
|
|
|
outlink->time_base) + s->frames_out;
|
|
|
|
|
2012-11-28 19:28:41 +01:00
|
|
|
if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
|
2012-11-28 08:41:07 +01:00
|
|
|
av_frame_free(&buf);
|
2012-07-15 09:47:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-09 14:08:21 +02:00
|
|
|
s->frames_out++;
|
|
|
|
}
|
|
|
|
flush_fifo(s->fifo);
|
|
|
|
|
2012-07-14 09:25:33 +02:00
|
|
|
ret = write_to_fifo(s->fifo, buf);
|
2012-05-09 14:08:21 +02:00
|
|
|
s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
|
2012-07-14 09:25:33 +02:00
|
|
|
|
|
|
|
return ret;
|
2012-05-09 14:08:21 +02:00
|
|
|
}
|
|
|
|
|
2012-07-24 15:14:01 +02:00
|
|
|
static const AVFilterPad avfilter_vf_fps_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-11-28 19:28:41 +01:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 15:14:01 +02:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fps_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
.config_props = config_props
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2012-05-09 14:08:21 +02:00
|
|
|
AVFilter avfilter_vf_fps = {
|
|
|
|
.name = "fps",
|
2013-04-14 20:58:31 +02:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
|
2012-05-09 14:08:21 +02:00
|
|
|
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.priv_size = sizeof(FPSContext),
|
2013-04-10 19:21:59 +02:00
|
|
|
.priv_class = &fps_class,
|
2012-05-09 14:08:21 +02:00
|
|
|
|
2012-07-24 15:14:01 +02:00
|
|
|
.inputs = avfilter_vf_fps_inputs,
|
|
|
|
.outputs = avfilter_vf_fps_outputs,
|
2012-05-09 14:08:21 +02:00
|
|
|
};
|