@@ -1451,6 +1451,7 @@ typedef struct AVFormatContext {
#define AVFMT_FLAG_SHORTEST 0x100000 ///< Stop muxing when the shortest stream stops.
#define AVFMT_FLAG_AUTO_BSF 0x200000 ///< Add bitstream filters as requested by the muxer
#define AVFMT_FLAG_DISCARD_CORRUPT_TS 0x400000 ///< Discard timestamps of frames marked corrupt
+#define AVFMT_FLAG_FILL_WALLCLOCK_DTS 0x800000 ///< Fill missing or discarded DTS values from wallclock (for live streams)
/**
* Maximum size of the data read from input for determining
@@ -192,6 +192,11 @@ struct AVStreamInternal {
int need_context_update;
FFFrac *priv_pts;
+
+ /**
+ * The wallclock timestamp of the most recent read packet (if AVFMT_FLAG_FILL_WALLCLOCK_DTS is set)
+ */
+ int64_t cur_wallclock_time;
};
#ifdef __GNUC__
@@ -59,6 +59,7 @@ static const AVOption avformat_options[] = {
{"shortest", "stop muxing with the shortest stream", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_SHORTEST }, 0, 0, E, "fflags" },
{"autobsf", "add needed bsfs automatically", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_AUTO_BSF }, 0, 0, E, "fflags" },
{"discardcorruptts", "discard timestamps on corrupted frames", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_DISCARD_CORRUPT_TS }, 0, 0, E, "fflags" },
+{"fillwallclockdts", "fill missing or discarded DTS values from wallclock (for live streams)", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_FILL_WALLCLOCK_DTS }, 0, 0, E, "fflags" },
{"analyzeduration", "specify how many microseconds are analyzed to probe the input", OFFSET(max_analyze_duration), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D},
{"cryptokey", "decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, {.dbl = 0}, 0, 0, D},
{"indexmem", "max memory used for timestamp index (per stream)", OFFSET(max_index_size), AV_OPT_TYPE_INT, {.i64 = 1<<20 }, 0, INT_MAX, D},
@@ -881,6 +881,18 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
pkt->stream_index);
}
+ if (s->flags & AVFMT_FLAG_FILL_WALLCLOCK_DTS) {
+ int64_t cur_wallclock_time = av_gettime_relative();
+ if (pkt->dts == AV_NOPTS_VALUE && st->cur_dts != AV_NOPTS_VALUE && st->internal->cur_wallclock_time) {
+ int64_t wallclock_offset = av_rescale_q(st->internal->cur_wallclock_time - cur_wallclock_time, AV_TIME_BASE_Q, st->time_base);
+ pkt->dts = st->cur_dts + FFMAX(wallclock_offset, 1);
+ av_log(s, AV_LOG_VERBOSE,
+ "Filled timestamp from wallclock (stream = %d; last = %"PRId64"; val = %"PRId64")\n",
+ pkt->stream_index, st->cur_dts, pkt->dts);
+ }
+ st->internal->cur_wallclock_time = cur_wallclock_time;
+ }
+
pkt->dts = wrap_timestamp(st, pkt->dts);
pkt->pts = wrap_timestamp(st, pkt->pts);