libavfilter/setpts.c
a532bb39
 /*
  * 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
  */
 
593aaee9
 #include <inttypes.h>
 
a532bb39
 #include "libavutil/eval.h"
1d9c2dc8
 #include "libavutil/internal.h"
0ebcdf5c
 #include "libavutil/mathematics.h"
f42635a5
 #include "libavutil/opt.h"
fc292283
 #include "libavutil/time.h"
b4729382
 #include "audio.h"
a532bb39
 #include "avfilter.h"
9d0bfc50
 #include "internal.h"
c04c533f
 #include "video.h"
a532bb39
 
b0f29db5
 static const char *const var_names[] = {
722762f7
     "FRAME_RATE",  ///< defined only for constant frame-rate video
a532bb39
     "INTERLACED",  ///< tell if the current frame is interlaced
b4729382
     "N",           ///< frame / sample number (starting at zero)
f6580b50
     "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
     "NB_SAMPLES",  ///< number of samples in the current frame (only audio)
a532bb39
     "POS",         ///< original position in the file of the frame
     "PREV_INPTS",  ///< previous  input PTS
1181461c
     "PREV_INT",    ///< previous  input time in seconds
a532bb39
     "PREV_OUTPTS", ///< previous output PTS
1181461c
     "PREV_OUTT",   ///< previous output time in seconds
a532bb39
     "PTS",         ///< original pts in the file of the frame
f6580b50
     "SAMPLE_RATE", ///< sample rate (only audio)
     "STARTPTS",    ///< PTS at start of movie
1181461c
     "STARTT",      ///< time at start of movie
     "T",           ///< original time in the file of the frame
a532bb39
     "TB",          ///< timebase
fc292283
     "RTCTIME",     ///< wallclock (RTC) time in micro seconds
     "RTCSTART",    ///< wallclock (RTC) time at the start of the movie in micro seconds
b4729382
     "S",           //   Number of samples in the current frame
     "SR",          //   Audio sample rate
a532bb39
     NULL
 };
 
 enum var_name {
722762f7
     VAR_FRAME_RATE,
a532bb39
     VAR_INTERLACED,
     VAR_N,
f6580b50
     VAR_NB_CONSUMED_SAMPLES,
     VAR_NB_SAMPLES,
a532bb39
     VAR_POS,
     VAR_PREV_INPTS,
1181461c
     VAR_PREV_INT,
a532bb39
     VAR_PREV_OUTPTS,
1181461c
     VAR_PREV_OUTT,
a532bb39
     VAR_PTS,
f6580b50
     VAR_SAMPLE_RATE,
a532bb39
     VAR_STARTPTS,
1181461c
     VAR_STARTT,
     VAR_T,
a532bb39
     VAR_TB,
fc292283
     VAR_RTCTIME,
     VAR_RTCSTART,
b4729382
     VAR_S,
     VAR_SR,
a532bb39
     VAR_VARS_NB
 };
 
58400ac1
 typedef struct SetPTSContext {
f42635a5
     const AVClass *class;
     char *expr_str;
a532bb39
     AVExpr *expr;
     double var_values[VAR_VARS_NB];
f6580b50
     enum AVMediaType type;
a532bb39
 } SetPTSContext;
 
fd6228e6
 static av_cold int init(AVFilterContext *ctx)
a532bb39
 {
     SetPTSContext *setpts = ctx->priv;
     int ret;
 
f42635a5
     if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
a532bb39
                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
f42635a5
         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
a532bb39
         return ret;
     }
 
0061ba04
     setpts->var_values[VAR_N]           = 0.0;
b4729382
     setpts->var_values[VAR_S]           = 0.0;
0061ba04
     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;
a532bb39
     return 0;
 }
 
 static int config_input(AVFilterLink *inlink)
 {
f6580b50
     AVFilterContext *ctx = inlink->dst;
     SetPTSContext *setpts = ctx->priv;
a532bb39
 
f6580b50
     setpts->type = inlink->type;
a532bb39
     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
fc292283
     setpts->var_values[VAR_RTCSTART] = av_gettime();
a532bb39
 
1cba7fa3
     setpts->var_values[VAR_SR] =
206c34e1
     setpts->var_values[VAR_SAMPLE_RATE] =
         setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
f6580b50
 
018bdaed
     setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num &&
                                          inlink->frame_rate.den ?
                                             av_q2d(inlink->frame_rate) : NAN;
722762f7
 
     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]);
a532bb39
     return 0;
 }
 
 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
1181461c
 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
a532bb39
 
022e118e
 #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)
 
a05a44e2
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
a532bb39
 {
     SetPTSContext *setpts = inlink->dst->priv;
dabb993c
     int64_t in_pts = frame->pts;
a532bb39
     double d;
ebc8d974
 
1181461c
     if (isnan(setpts->var_values[VAR_STARTPTS])) {
dabb993c
         setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
         setpts->var_values[VAR_STARTT  ] = TS2T(frame->pts, inlink->time_base);
1181461c
     }
dabb993c
     setpts->var_values[VAR_PTS       ] = TS2D(frame->pts);
     setpts->var_values[VAR_T         ] = TS2T(frame->pts, inlink->time_base);
a05a44e2
     setpts->var_values[VAR_POS       ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
fc292283
     setpts->var_values[VAR_RTCTIME   ] = av_gettime();
a532bb39
 
b4729382
     if (inlink->type == AVMEDIA_TYPE_VIDEO) {
a05a44e2
         setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
1cba7fa3
     } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
b4729382
         setpts->var_values[VAR_S] = frame->nb_samples;
a05a44e2
         setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
f6580b50
     }
 
d2af7205
     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
565e4993
     frame->pts = D2TS(d);
022e118e
 
1a3eb042
     av_log(inlink->dst, AV_LOG_TRACE,
2bfd4ff1
             "N:%"PRId64" PTS:%s T:%f POS:%s",
593aaee9
             (int64_t)setpts->var_values[VAR_N],
2bfd4ff1
             d2istr(setpts->var_values[VAR_PTS]),
             setpts->var_values[VAR_T],
             d2istr(setpts->var_values[VAR_POS]));
022e118e
     switch (inlink->type) {
     case AVMEDIA_TYPE_VIDEO:
40d552da
         av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
2bfd4ff1
                 (int64_t)setpts->var_values[VAR_INTERLACED]);
022e118e
         break;
     case AVMEDIA_TYPE_AUDIO:
40d552da
         av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
2bfd4ff1
                 (int64_t)setpts->var_values[VAR_NB_SAMPLES],
                 (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
022e118e
         break;
     }
40d552da
     av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
022e118e
 
b4729382
     if (inlink->type == AVMEDIA_TYPE_VIDEO) {
         setpts->var_values[VAR_N] += 1.0;
     } else {
         setpts->var_values[VAR_N] += frame->nb_samples;
     }
a532bb39
 
dabb993c
     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);
f6580b50
     if (setpts->type == AVMEDIA_TYPE_AUDIO) {
a05a44e2
         setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
dabb993c
     }
     return ff_filter_frame(inlink->dst->outputs[0], frame);
a532bb39
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
     SetPTSContext *setpts = ctx->priv;
d2af7205
     av_expr_free(setpts->expr);
a532bb39
     setpts->expr = NULL;
 }
 
1007de70
 #define OFFSET(x) offsetof(SetPTSContext, x)
1cba7fa3
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
33b97faa
 static const AVOption options[] = {
     { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
b211607b
     { NULL }
1007de70
 };
 
b4729382
 #if CONFIG_SETPTS_FILTER
e457f2cf
 #define setpts_options options
 AVFILTER_DEFINE_CLASS(setpts);
1007de70
 
568c70e7
 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
2d9d4440
     {
b211607b
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
         .filter_frame = filter_frame,
2d9d4440
     },
     { NULL }
 };
 
568c70e7
 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
2d9d4440
     {
         .name = "default",
568c70e7
         .type = AVMEDIA_TYPE_VIDEO,
2d9d4440
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_setpts = {
a532bb39
     .name      = "setpts",
     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
f6580b50
     .init      = init,
     .uninit    = uninit,
 
a532bb39
     .priv_size = sizeof(SetPTSContext),
33b97faa
     .priv_class = &setpts_class,
f42635a5
 
568c70e7
     .inputs    = avfilter_vf_setpts_inputs,
     .outputs   = avfilter_vf_setpts_outputs,
f42635a5
 };
1cba7fa3
 #endif /* CONFIG_SETPTS_FILTER */
f42635a5
 
b4729382
 #if CONFIG_ASETPTS_FILTER
1cba7fa3
 
e457f2cf
 #define asetpts_options options
 AVFILTER_DEFINE_CLASS(asetpts);
f42635a5
 
b4729382
 static const AVFilterPad asetpts_inputs[] = {
526cb36e
     {
b211607b
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .config_props = config_input,
         .filter_frame = filter_frame,
526cb36e
     },
     { NULL }
 };
 
b4729382
 static const AVFilterPad asetpts_outputs[] = {
526cb36e
     {
         .name = "default",
b4729382
         .type = AVMEDIA_TYPE_AUDIO,
526cb36e
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_af_asetpts = {
b4729382
     .name        = "asetpts",
     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
     .init        = init,
     .uninit      = uninit,
1cba7fa3
     .priv_size   = sizeof(SetPTSContext),
     .priv_class  = &asetpts_class,
     .inputs      = asetpts_inputs,
     .outputs     = asetpts_outputs,
a532bb39
 };
1cba7fa3
 #endif /* CONFIG_ASETPTS_FILTER */