2010-11-02 22:29:17 +00: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
|
|
|
|
*/
|
|
|
|
|
2014-08-20 16:54:50 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2010-11-02 22:29:17 +00:00
|
|
|
#include "libavutil/eval.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/internal.h"
|
2011-06-04 11:58:23 +00:00
|
|
|
#include "libavutil/mathematics.h"
|
2013-04-10 21:48:26 +00:00
|
|
|
#include "libavutil/opt.h"
|
2013-01-25 14:04:41 +00:00
|
|
|
#include "libavutil/time.h"
|
2013-04-10 14:28:38 +00:00
|
|
|
#include "audio.h"
|
2010-11-02 22:29:17 +00:00
|
|
|
#include "avfilter.h"
|
2012-06-12 18:12:42 +00:00
|
|
|
#include "internal.h"
|
2012-05-19 08:37:56 +00:00
|
|
|
#include "video.h"
|
2010-11-02 22:29:17 +00:00
|
|
|
|
2012-02-20 08:42:33 +00:00
|
|
|
static const char *const var_names[] = {
|
2012-09-10 09:58:53 +00:00
|
|
|
"FRAME_RATE", ///< defined only for constant frame-rate video
|
2010-11-02 22:29:17 +00:00
|
|
|
"INTERLACED", ///< tell if the current frame is interlaced
|
2013-04-10 14:28:38 +00:00
|
|
|
"N", ///< frame / sample number (starting at zero)
|
2012-08-08 11:44:11 +00: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 22:29:17 +00:00
|
|
|
"POS", ///< original position in the file of the frame
|
|
|
|
"PREV_INPTS", ///< previous input PTS
|
2012-08-23 10:29:20 +00:00
|
|
|
"PREV_INT", ///< previous input time in seconds
|
2010-11-02 22:29:17 +00:00
|
|
|
"PREV_OUTPTS", ///< previous output PTS
|
2012-08-23 10:29:20 +00:00
|
|
|
"PREV_OUTT", ///< previous output time in seconds
|
2010-11-02 22:29:17 +00:00
|
|
|
"PTS", ///< original pts in the file of the frame
|
2012-08-08 11:44:11 +00:00
|
|
|
"SAMPLE_RATE", ///< sample rate (only audio)
|
|
|
|
"STARTPTS", ///< PTS at start of movie
|
2012-08-23 10:29:20 +00:00
|
|
|
"STARTT", ///< time at start of movie
|
|
|
|
"T", ///< original time in the file of the frame
|
2010-11-02 22:29:17 +00:00
|
|
|
"TB", ///< timebase
|
2013-01-25 14:04:41 +00:00
|
|
|
"RTCTIME", ///< wallclock (RTC) time in micro seconds
|
|
|
|
"RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
|
2013-04-10 14:28:38 +00:00
|
|
|
"S", // Number of samples in the current frame
|
|
|
|
"SR", // Audio sample rate
|
2010-11-02 22:29:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum var_name {
|
2012-09-10 09:58:53 +00:00
|
|
|
VAR_FRAME_RATE,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_INTERLACED,
|
|
|
|
VAR_N,
|
2012-08-08 11:44:11 +00:00
|
|
|
VAR_NB_CONSUMED_SAMPLES,
|
|
|
|
VAR_NB_SAMPLES,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_POS,
|
|
|
|
VAR_PREV_INPTS,
|
2012-08-23 10:29:20 +00:00
|
|
|
VAR_PREV_INT,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_PREV_OUTPTS,
|
2012-08-23 10:29:20 +00:00
|
|
|
VAR_PREV_OUTT,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_PTS,
|
2012-08-08 11:44:11 +00:00
|
|
|
VAR_SAMPLE_RATE,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_STARTPTS,
|
2012-08-23 10:29:20 +00:00
|
|
|
VAR_STARTT,
|
|
|
|
VAR_T,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_TB,
|
2013-01-25 14:04:41 +00:00
|
|
|
VAR_RTCTIME,
|
|
|
|
VAR_RTCSTART,
|
2013-04-10 14:28:38 +00:00
|
|
|
VAR_S,
|
|
|
|
VAR_SR,
|
2010-11-02 22:29:17 +00:00
|
|
|
VAR_VARS_NB
|
|
|
|
};
|
|
|
|
|
2014-04-11 09:54:15 +00:00
|
|
|
typedef struct SetPTSContext {
|
2013-04-10 21:48:26 +00:00
|
|
|
const AVClass *class;
|
|
|
|
char *expr_str;
|
2010-11-02 22:29:17 +00:00
|
|
|
AVExpr *expr;
|
|
|
|
double var_values[VAR_VARS_NB];
|
2012-08-08 11:44:11 +00:00
|
|
|
enum AVMediaType type;
|
2010-11-02 22:29:17 +00:00
|
|
|
} SetPTSContext;
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2010-11-02 22:29:17 +00:00
|
|
|
{
|
|
|
|
SetPTSContext *setpts = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
2013-04-10 21:48:26 +00:00
|
|
|
if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
|
2010-11-02 22:29:17 +00:00
|
|
|
var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
|
2013-04-10 21:48:26 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
|
2010-11-02 22:29:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:24:45 +00:00
|
|
|
setpts->var_values[VAR_N] = 0.0;
|
2013-04-10 14:28:38 +00:00
|
|
|
setpts->var_values[VAR_S] = 0.0;
|
2013-05-01 08:24:45 +00:00
|
|
|
setpts->var_values[VAR_PREV_INPTS] = NAN;
|
|
|
|
setpts->var_values[VAR_PREV_INT] = NAN;
|
|
|
|
setpts->var_values[VAR_PREV_OUTPTS] = NAN;
|
|
|
|
setpts->var_values[VAR_PREV_OUTT] = NAN;
|
|
|
|
setpts->var_values[VAR_STARTPTS] = NAN;
|
|
|
|
setpts->var_values[VAR_STARTT] = NAN;
|
2010-11-02 22:29:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
2012-08-08 11:44:11 +00:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
SetPTSContext *setpts = ctx->priv;
|
2010-11-02 22:29:17 +00:00
|
|
|
|
2012-08-08 11:44:11 +00:00
|
|
|
setpts->type = inlink->type;
|
2010-11-02 22:29:17 +00:00
|
|
|
setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
|
2013-01-25 14:04:41 +00:00
|
|
|
setpts->var_values[VAR_RTCSTART] = av_gettime();
|
2010-11-02 22:29:17 +00:00
|
|
|
|
2013-05-01 08:59:21 +00:00
|
|
|
setpts->var_values[VAR_SR] =
|
2012-09-10 10:02:30 +00:00
|
|
|
setpts->var_values[VAR_SAMPLE_RATE] =
|
|
|
|
setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
|
2012-08-08 11:44:11 +00:00
|
|
|
|
2015-11-03 17:57:23 +00:00
|
|
|
setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num &&
|
|
|
|
inlink->frame_rate.den ?
|
|
|
|
av_q2d(inlink->frame_rate) : NAN;
|
2012-09-10 09:58:53 +00:00
|
|
|
|
|
|
|
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 22:29:17 +00: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 10:29:20 +00:00
|
|
|
#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
|
2010-11-02 22:29:17 +00:00
|
|
|
|
2012-12-18 18:00:37 +00: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)
|
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2010-11-02 22:29:17 +00:00
|
|
|
{
|
|
|
|
SetPTSContext *setpts = inlink->dst->priv;
|
2012-11-28 21:27:51 +00:00
|
|
|
int64_t in_pts = frame->pts;
|
2010-11-02 22:29:17 +00:00
|
|
|
double d;
|
2012-07-08 15:29:42 +00:00
|
|
|
|
2012-08-23 10:29:20 +00:00
|
|
|
if (isnan(setpts->var_values[VAR_STARTPTS])) {
|
2012-11-28 21:27:51 +00:00
|
|
|
setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
|
|
|
|
setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
|
2012-08-23 10:29:20 +00:00
|
|
|
}
|
2012-11-28 21:27:51 +00:00
|
|
|
setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
|
|
|
|
setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
|
2017-04-22 08:57:18 +00:00
|
|
|
setpts->var_values[VAR_POS ] = frame->pkt_pos == -1 ? NAN : frame->pkt_pos;
|
2013-01-25 14:04:41 +00:00
|
|
|
setpts->var_values[VAR_RTCTIME ] = av_gettime();
|
2010-11-02 22:29:17 +00:00
|
|
|
|
2013-04-10 14:28:38 +00:00
|
|
|
if (inlink->type == AVMEDIA_TYPE_VIDEO) {
|
2013-03-10 00:30:30 +00:00
|
|
|
setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
|
2013-05-01 08:59:21 +00:00
|
|
|
} else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
|
2013-04-10 14:28:38 +00:00
|
|
|
setpts->var_values[VAR_S] = frame->nb_samples;
|
2013-03-10 00:30:30 +00:00
|
|
|
setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
|
2012-08-08 11:44:11 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 14:06:49 +00:00
|
|
|
d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
|
2012-11-27 06:49:45 +00:00
|
|
|
frame->pts = D2TS(d);
|
2012-12-18 18:00:37 +00:00
|
|
|
|
2015-03-16 08:57:35 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_TRACE,
|
2014-08-21 19:47:43 +00:00
|
|
|
"N:%"PRId64" PTS:%s T:%f POS:%s",
|
2014-08-20 16:54:50 +00:00
|
|
|
(int64_t)setpts->var_values[VAR_N],
|
2014-08-21 19:47:43 +00:00
|
|
|
d2istr(setpts->var_values[VAR_PTS]),
|
|
|
|
setpts->var_values[VAR_T],
|
|
|
|
d2istr(setpts->var_values[VAR_POS]));
|
2012-12-18 18:00:37 +00:00
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2015-04-20 01:19:29 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
|
2014-08-21 19:47:43 +00:00
|
|
|
(int64_t)setpts->var_values[VAR_INTERLACED]);
|
2012-12-18 18:00:37 +00:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2015-04-20 01:19:29 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
|
2014-08-21 19:47:43 +00:00
|
|
|
(int64_t)setpts->var_values[VAR_NB_SAMPLES],
|
|
|
|
(int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
|
2012-12-18 18:00:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-04-20 01:19:29 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
|
2012-12-18 18:00:37 +00:00
|
|
|
|
2013-04-10 14:28:38 +00:00
|
|
|
if (inlink->type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
setpts->var_values[VAR_N] += 1.0;
|
|
|
|
} else {
|
|
|
|
setpts->var_values[VAR_N] += frame->nb_samples;
|
|
|
|
}
|
2010-11-02 22:29:17 +00:00
|
|
|
|
2012-11-28 21:27:51 +00: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-08 11:44:11 +00:00
|
|
|
if (setpts->type == AVMEDIA_TYPE_AUDIO) {
|
2013-03-10 00:30:30 +00:00
|
|
|
setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
|
2012-11-28 21:27:51 +00:00
|
|
|
}
|
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], frame);
|
2010-11-02 22:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SetPTSContext *setpts = ctx->priv;
|
2010-11-08 14:06:49 +00:00
|
|
|
av_expr_free(setpts->expr);
|
2010-11-02 22:29:17 +00:00
|
|
|
setpts->expr = NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-12 13:46:52 +00:00
|
|
|
#define OFFSET(x) offsetof(SetPTSContext, x)
|
2013-05-01 08:59:21 +00:00
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
|
2013-02-25 20:21:29 +00:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
|
2013-09-07 12:13:50 +00:00
|
|
|
{ NULL }
|
2013-04-12 13:46:52 +00:00
|
|
|
};
|
|
|
|
|
2013-04-10 14:28:38 +00:00
|
|
|
#if CONFIG_SETPTS_FILTER
|
2013-05-27 11:14:43 +00:00
|
|
|
#define setpts_options options
|
|
|
|
AVFILTER_DEFINE_CLASS(setpts);
|
2013-04-12 13:46:52 +00:00
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_vf_setpts_inputs[] = {
|
2012-11-28 19:01:59 +00:00
|
|
|
{
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 19:01:59 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_vf_setpts_outputs[] = {
|
2012-11-28 19:01:59 +00:00
|
|
|
{
|
|
|
|
.name = "default",
|
2012-07-24 13:14:01 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-11-28 19:01:59 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 06:44:24 +00:00
|
|
|
AVFilter ff_vf_setpts = {
|
2010-11-02 22:29:17 +00:00
|
|
|
.name = "setpts",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
|
2012-08-08 11:44:11 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
2010-11-02 22:29:17 +00:00
|
|
|
.priv_size = sizeof(SetPTSContext),
|
2013-02-25 20:21:29 +00:00
|
|
|
.priv_class = &setpts_class,
|
2013-04-10 21:48:26 +00:00
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
.inputs = avfilter_vf_setpts_inputs,
|
|
|
|
.outputs = avfilter_vf_setpts_outputs,
|
2013-04-10 21:48:26 +00:00
|
|
|
};
|
2013-05-01 08:59:21 +00:00
|
|
|
#endif /* CONFIG_SETPTS_FILTER */
|
2013-04-10 21:48:26 +00:00
|
|
|
|
2013-04-10 14:28:38 +00:00
|
|
|
#if CONFIG_ASETPTS_FILTER
|
2013-05-01 08:59:21 +00:00
|
|
|
|
2013-05-27 11:14:43 +00:00
|
|
|
#define asetpts_options options
|
|
|
|
AVFILTER_DEFINE_CLASS(asetpts);
|
2013-04-10 21:48:26 +00:00
|
|
|
|
2013-04-10 14:28:38 +00:00
|
|
|
static const AVFilterPad asetpts_inputs[] = {
|
2012-10-11 13:41:56 +00:00
|
|
|
{
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
2012-10-11 13:41:56 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-04-10 14:28:38 +00:00
|
|
|
static const AVFilterPad asetpts_outputs[] = {
|
2012-10-11 13:41:56 +00:00
|
|
|
{
|
|
|
|
.name = "default",
|
2013-04-10 14:28:38 +00:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-10-11 13:41:56 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 06:44:24 +00:00
|
|
|
AVFilter ff_af_asetpts = {
|
2013-04-10 14:28:38 +00:00
|
|
|
.name = "asetpts",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2013-05-01 08:59:21 +00:00
|
|
|
.priv_size = sizeof(SetPTSContext),
|
|
|
|
.priv_class = &asetpts_class,
|
|
|
|
.inputs = asetpts_inputs,
|
|
|
|
.outputs = asetpts_outputs,
|
2010-11-02 22:29:17 +00:00
|
|
|
};
|
2013-05-01 08:59:21 +00:00
|
|
|
#endif /* CONFIG_ASETPTS_FILTER */
|