diff --git a/doc/filters.texi b/doc/filters.texi index 0dc928889d..6bc07718b0 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -4166,6 +4166,14 @@ Alternatively, the options can be specified as a flat string: See also the @ref{setpts} filter. +@item start_time +Assume the first PTS should be the given value, in seconds. This allows for +padding/trimming at the start of stream. By default, no assumption is made +about the first frame's expected PTS, so no padding or trimming is done. +For example, this could be set to 0 to pad the beginning with duplicates of +the first frame if a video stream starts after the audio stream or to trim any +frames with a negative PTS. + @subsection Examples @itemize diff --git a/libavfilter/vf_fps.c b/libavfilter/vf_fps.c index 006c24577f..e30e90b394 100644 --- a/libavfilter/vf_fps.c +++ b/libavfilter/vf_fps.c @@ -44,6 +44,8 @@ typedef struct FPSContext { 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 + double start_time; ///< pts, in seconds, of the expected first frame + AVRational framerate; ///< target framerate int rounding; ///< AVRounding method for timestamps @@ -59,6 +61,7 @@ typedef struct FPSContext { #define F AV_OPT_FLAG_FILTERING_PARAM static const AVOption fps_options[] = { { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = V|F }, + { "start_time", "Assume the first PTS should be this value.", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX, V }, { "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" }, @@ -78,6 +81,7 @@ static av_cold int init(AVFilterContext *ctx) return AVERROR(ENOMEM); s->pts = AV_NOPTS_VALUE; + s->first_pts = AV_NOPTS_VALUE; av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den); return 0; @@ -178,7 +182,17 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf) if (ret < 0) return ret; - s->first_pts = s->pts = buf->pts; + if (s->start_time != AV_NOPTS_VALUE) { + double first_pts = s->start_time * AV_TIME_BASE; + first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX); + s->first_pts = s->pts = av_rescale_q(first_pts, AV_TIME_BASE_Q, + inlink->time_base); + av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64")\n", + s->first_pts, av_rescale_q(first_pts, AV_TIME_BASE_Q, + outlink->time_base)); + } else { + s->first_pts = s->pts = buf->pts; + } } else { av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no " "timestamp.\n");