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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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[] = {
|
2012-09-10 11:58:53 +02:00
|
|
|
"FRAME_RATE", ///< defined only for constant frame-rate video
|
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 {
|
2012-09-10 11:58:53 +02:00
|
|
|
VAR_FRAME_RATE,
|
2010-11-02 23:29:17 +01:00
|
|
|
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-09-10 12:02:30 +02:00
|
|
|
setpts->var_values[VAR_SAMPLE_RATE] =
|
|
|
|
setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
|
2012-08-08 13:44:11 +02:00
|
|
|
|
2012-09-10 11:58:53 +02:00
|
|
|
setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
|
|
|
|
av_q2d(inlink->frame_rate) : NAN;
|
|
|
|
|
|
|
|
av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
|
|
|
|
setpts->var_values[VAR_TB],
|
|
|
|
setpts->var_values[VAR_FRAME_RATE],
|
|
|
|
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-12-18 19:00:37 +01:00
|
|
|
#define BUF_SIZE 64
|
|
|
|
|
|
|
|
static inline char *double2int64str(char *buf, double v)
|
|
|
|
{
|
|
|
|
if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
|
|
|
|
else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
|
|
|
|
|
2012-11-28 22:27:51 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
|
2010-11-02 23:29:17 +01:00
|
|
|
{
|
|
|
|
SetPTSContext *setpts = inlink->dst->priv;
|
2012-11-28 22:27:51 +01:00
|
|
|
int64_t in_pts = frame->pts;
|
2010-11-02 23:29:17 +01:00
|
|
|
double d;
|
2012-07-08 17:29:42 +02:00
|
|
|
|
2012-08-23 12:29:20 +02:00
|
|
|
if (isnan(setpts->var_values[VAR_STARTPTS])) {
|
2012-11-28 22:27:51 +01:00
|
|
|
setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
|
|
|
|
setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
|
2012-08-23 12:29:20 +02:00
|
|
|
}
|
2012-11-28 22:27:51 +01:00
|
|
|
setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
|
|
|
|
setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
|
|
|
|
setpts->var_values[VAR_POS ] = frame->pos == -1 ? NAN : frame->pos;
|
2010-11-02 23:29:17 +01:00
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2012-11-28 22:27:51 +01:00
|
|
|
setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
|
2012-08-08 13:44:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2012-11-28 22:27:51 +01:00
|
|
|
setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
|
2012-08-08 13:44:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-08 15:06:49 +01:00
|
|
|
d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
|
2012-12-18 19:00:37 +01:00
|
|
|
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG,
|
|
|
|
"N:%"PRId64" PTS:%s T:%f POS:%s",
|
|
|
|
(int64_t)setpts->var_values[VAR_N],
|
|
|
|
d2istr(setpts->var_values[VAR_PTS]),
|
|
|
|
setpts->var_values[VAR_T],
|
|
|
|
d2istr(setpts->var_values[VAR_POS]));
|
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
|
|
|
|
(int64_t)setpts->var_values[VAR_INTERLACED]);
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
|
|
|
|
(int64_t)setpts->var_values[VAR_NB_SAMPLES],
|
|
|
|
(int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
|
|
|
|
|
2012-11-28 22:27:51 +01:00
|
|
|
frame->pts = D2TS(d);
|
2010-11-02 23:29:17 +01:00
|
|
|
|
2012-11-28 22:27:51 +01:00
|
|
|
setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
|
|
|
|
setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
|
|
|
|
setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
|
|
|
|
setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
|
2012-08-23 12:31:47 +02:00
|
|
|
setpts->var_values[VAR_N] += 1.0;
|
2012-08-08 13:44:11 +02:00
|
|
|
if (setpts->type == AVMEDIA_TYPE_AUDIO) {
|
2012-11-28 22:27:51 +01:00
|
|
|
setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->audio->nb_samples;
|
|
|
|
}
|
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], frame);
|
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
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad avfilter_af_asetpts_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.get_audio_buffer = ff_null_get_audio_buffer,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_asetpts_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2012-08-08 13:44:11 +02:00
|
|
|
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),
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = avfilter_af_asetpts_inputs,
|
|
|
|
.outputs = avfilter_af_asetpts_outputs,
|
2012-08-08 13:44:11 +02:00
|
|
|
};
|
|
|
|
#endif /* CONFIG_ASETPTS_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_SETPTS_FILTER
|
2012-10-11 15:41:56 +02:00
|
|
|
static const AVFilterPad avfilter_vf_setpts_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
|
|
|
.config_props = config_input,
|
2012-11-28 22:27:51 +01:00
|
|
|
.filter_frame = filter_frame,
|
2012-10-11 15:41:56 +02:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_setpts_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
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-10-11 15:41:56 +02:00
|
|
|
.inputs = avfilter_vf_setpts_inputs,
|
|
|
|
.outputs = avfilter_vf_setpts_outputs,
|
2010-11-02 23:29:17 +01:00
|
|
|
};
|
2012-08-08 13:44:11 +02:00
|
|
|
#endif /* CONFIG_SETPTS_FILTER */
|