2010-11-02 23:29:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Stefano Sabatini
|
|
|
|
* Copyright (c) 2008 Victor Paesa
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* video presentation timestamp (PTS) modification filter
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#include "libavutil/eval.h"
|
2012-08-06 15:49:32 +02:00
|
|
|
#include "libavutil/internal.h"
|
2011-06-04 13:58:23 +02:00
|
|
|
#include "libavutil/mathematics.h"
|
2010-11-02 23:29:17 +01:00
|
|
|
#include "avfilter.h"
|
2012-06-12 20:12:42 +02:00
|
|
|
#include "internal.h"
|
2012-08-08 13:44:11 +02:00
|
|
|
#include "audio.h"
|
2012-05-19 10:37:56 +02:00
|
|
|
#include "video.h"
|
2010-11-02 23:29:17 +01:00
|
|
|
|
2012-02-20 09:42:33 +01:00
|
|
|
static const char *const var_names[] = {
|
2010-11-02 23:29:17 +01:00
|
|
|
"INTERLACED", ///< tell if the current frame is interlaced
|
|
|
|
"N", ///< frame number (starting at zero)
|
2012-08-08 13:44:11 +02:00
|
|
|
"NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
|
|
|
|
"NB_SAMPLES", ///< number of samples in the current frame (only audio)
|
2010-11-02 23:29:17 +01:00
|
|
|
"POS", ///< original position in the file of the frame
|
|
|
|
"PREV_INPTS", ///< previous input PTS
|
2012-08-23 12:29:20 +02:00
|
|
|
"PREV_INT", ///< previous input time in seconds
|
2010-11-02 23:29:17 +01:00
|
|
|
"PREV_OUTPTS", ///< previous output PTS
|
2012-08-23 12:29:20 +02:00
|
|
|
"PREV_OUTT", ///< previous output time in seconds
|
2010-11-02 23:29:17 +01:00
|
|
|
"PTS", ///< original pts in the file of the frame
|
2012-08-08 13:44:11 +02:00
|
|
|
"SAMPLE_RATE", ///< sample rate (only audio)
|
|
|
|
"STARTPTS", ///< PTS at start of movie
|
2012-08-23 12:29:20 +02:00
|
|
|
"STARTT", ///< time at start of movie
|
|
|
|
"T", ///< original time in the file of the frame
|
2010-11-02 23:29:17 +01:00
|
|
|
"TB", ///< timebase
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum var_name {
|
|
|
|
VAR_INTERLACED,
|
|
|
|
VAR_N,
|
2012-08-08 13:44:11 +02:00
|
|
|
VAR_NB_CONSUMED_SAMPLES,
|
|
|
|
VAR_NB_SAMPLES,
|
2010-11-02 23:29:17 +01:00
|
|
|
VAR_POS,
|
|
|
|
VAR_PREV_INPTS,
|
2012-08-23 12:29:20 +02:00
|
|
|
VAR_PREV_INT,
|
2010-11-02 23:29:17 +01:00
|
|
|
VAR_PREV_OUTPTS,
|
2012-08-23 12:29:20 +02:00
|
|
|
VAR_PREV_OUTT,
|
2010-11-02 23:29:17 +01:00
|
|
|
VAR_PTS,
|
2012-08-08 13:44:11 +02:00
|
|
|
VAR_SAMPLE_RATE,
|
2010-11-02 23:29:17 +01:00
|
|
|
VAR_STARTPTS,
|
2012-08-23 12:29:20 +02:00
|
|
|
VAR_STARTT,
|
|
|
|
VAR_T,
|
2010-11-02 23:29:17 +01:00
|
|
|
VAR_TB,
|
|
|
|
VAR_VARS_NB
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
AVExpr *expr;
|
|
|
|
double var_values[VAR_VARS_NB];
|
2012-08-08 13:44:11 +02:00
|
|
|
enum AVMediaType type;
|
2010-11-02 23:29:17 +01:00
|
|
|
} SetPTSContext;
|
|
|
|
|
2012-06-21 07:55:56 +02:00
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args)
|
2010-11-02 23:29:17 +01:00
|
|
|
{
|
|
|
|
SetPTSContext *setpts = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
2010-11-08 15:06:49 +01:00
|
|
|
if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
|
2010-11-02 23:29:17 +01:00
|
|
|
var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
setpts->var_values[VAR_N ] = 0.0;
|
2012-08-23 12:29:20 +02:00
|
|
|
setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
|
|
|
|
setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
|
|
|
|
setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
|
2010-11-02 23:29:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
2012-08-08 13:44:11 +02:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
SetPTSContext *setpts = ctx->priv;
|
2010-11-02 23:29:17 +01:00
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
setpts->type = inlink->type;
|
2010-11-02 23:29:17 +01:00
|
|
|
setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
|
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
if (setpts->type == AVMEDIA_TYPE_AUDIO)
|
|
|
|
setpts->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
|
|
|
|
|
|
|
|
av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f SAMPLE_RATE:%f\n",
|
|
|
|
setpts->var_values[VAR_TB], setpts->var_values[VAR_SAMPLE_RATE]);
|
2010-11-02 23:29:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
|
|
|
|
#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
|
2012-08-23 12:29:20 +02:00
|
|
|
#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
|
2010-11-02 23:29:17 +01:00
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
|
2010-11-02 23:29:17 +01:00
|
|
|
{
|
|
|
|
SetPTSContext *setpts = inlink->dst->priv;
|
|
|
|
double d;
|
|
|
|
AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
|
|
|
|
|
2012-07-08 17:29:42 +02:00
|
|
|
if (!outpicref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-08-23 12:29:20 +02:00
|
|
|
if (isnan(setpts->var_values[VAR_STARTPTS])) {
|
2010-11-02 23:29:17 +01:00
|
|
|
setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
|
2012-08-23 12:29:20 +02:00
|
|
|
setpts->var_values[VAR_STARTT ] = TS2T(inpicref->pts, inlink->time_base);
|
|
|
|
}
|
2010-11-02 23:29:17 +01:00
|
|
|
setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
|
2012-08-23 12:29:20 +02:00
|
|
|
setpts->var_values[VAR_T ] = TS2T(inpicref->pts, inlink->time_base);
|
2010-11-02 23:29:17 +01:00
|
|
|
setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
|
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
setpts->var_values[VAR_NB_SAMPLES] = inpicref->audio->nb_samples;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-08 15:06:49 +01:00
|
|
|
d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
|
2010-11-02 23:29:17 +01:00
|
|
|
outpicref->pts = D2TS(d);
|
|
|
|
|
|
|
|
setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
|
2012-08-23 12:29:20 +02:00
|
|
|
setpts->var_values[VAR_PREV_INT ] = TS2T(inpicref ->pts, inlink->time_base);
|
2010-11-02 23:29:17 +01:00
|
|
|
setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
|
2012-08-23 12:29:20 +02:00
|
|
|
setpts->var_values[VAR_PREV_OUTT] = TS2T(outpicref->pts, inlink->time_base);
|
2012-08-08 13:44:11 +02:00
|
|
|
|
2012-08-23 12:31:47 +02:00
|
|
|
av_dlog(inlink->dst,
|
|
|
|
"n:%"PRId64" interlaced:%d nb_samples:%d nb_consumed_samples:%d "
|
|
|
|
"pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
|
|
|
|
(int64_t)setpts->var_values[VAR_N],
|
|
|
|
(int)setpts->var_values[VAR_INTERLACED],
|
|
|
|
(int)setpts->var_values[VAR_NB_SAMPLES],
|
|
|
|
(int)setpts->var_values[VAR_NB_CONSUMED_SAMPLES],
|
|
|
|
(int64_t)setpts->var_values[VAR_POS],
|
|
|
|
(int64_t)setpts->var_values[VAR_PREV_INPTS],
|
|
|
|
setpts->var_values[VAR_PREV_INT],
|
|
|
|
(int64_t)setpts->var_values[VAR_PREV_OUTPTS],
|
|
|
|
setpts->var_values[VAR_PREV_OUTT]);
|
|
|
|
|
|
|
|
setpts->var_values[VAR_N] += 1.0;
|
2012-08-08 13:44:11 +02:00
|
|
|
if (setpts->type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += inpicref->audio->nb_samples;
|
|
|
|
return ff_filter_samples(inlink->dst->outputs[0], outpicref);
|
|
|
|
} else
|
|
|
|
return ff_start_frame (inlink->dst->outputs[0], outpicref);
|
2010-11-02 23:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SetPTSContext *setpts = ctx->priv;
|
2010-11-08 15:06:49 +01:00
|
|
|
av_expr_free(setpts->expr);
|
2010-11-02 23:29:17 +01:00
|
|
|
setpts->expr = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
#if CONFIG_ASETPTS_FILTER
|
|
|
|
AVFilter avfilter_af_asetpts = {
|
|
|
|
.name = "asetpts",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.priv_size = sizeof(SetPTSContext),
|
|
|
|
|
|
|
|
.inputs = (const AVFilterPad[]) {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.get_audio_buffer = ff_null_get_audio_buffer,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_samples = filter_frame,
|
|
|
|
},
|
|
|
|
{ .name = NULL }
|
|
|
|
},
|
|
|
|
.outputs = (const AVFilterPad[]) {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ .name = NULL }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
#endif /* CONFIG_ASETPTS_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_SETPTS_FILTER
|
2010-11-02 23:29:17 +01:00
|
|
|
AVFilter avfilter_vf_setpts = {
|
|
|
|
.name = "setpts",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.priv_size = sizeof(SetPTSContext),
|
|
|
|
|
2012-07-21 18:45:52 +02:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
|
|
|
.config_props = config_input,
|
2012-08-08 13:44:11 +02:00
|
|
|
.start_frame = filter_frame, },
|
2012-07-21 18:45:52 +02:00
|
|
|
{ .name = NULL }},
|
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO, },
|
|
|
|
{ .name = NULL}},
|
2010-11-02 23:29:17 +01:00
|
|
|
};
|
2012-08-08 13:44:11 +02:00
|
|
|
#endif /* CONFIG_SETPTS_FILTER */
|