[FFmpeg-devel,3/4] lavf/utils: add flag to discard timestamps on corrupted frames

Submitted by Rodger Combs on Dec. 13, 2017, 11:44 a.m.

Details

Message ID 20171213114453.22089-3-rodger.combs@gmail.com
State New
Headers show

Commit Message

Rodger Combs Dec. 13, 2017, 11:44 a.m.
---
 libavformat/avformat.h      | 1 +
 libavformat/options_table.h | 1 +
 libavformat/utils.c         | 8 ++++++++
 3 files changed, 10 insertions(+)

Patch hide | download patch | download mbox

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 4f2798a871..d10d583dff 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1450,6 +1450,7 @@  typedef struct AVFormatContext {
 #define AVFMT_FLAG_FAST_SEEK   0x80000 ///< Enable fast, but inaccurate seeks for some formats
 #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
 
     /**
      * Maximum size of the data read from input for determining
diff --git a/libavformat/options_table.h b/libavformat/options_table.h
index b8fa47c6fd..515574d3e0 100644
--- a/libavformat/options_table.h
+++ b/libavformat/options_table.h
@@ -58,6 +58,7 @@  static const AVOption avformat_options[] = {
 {"bitexact", "do not write random/volatile data", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_BITEXACT }, 0, 0, E, "fflags" },
 {"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" },
 {"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},
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 8d49348d9e..2e15f34469 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -873,6 +873,14 @@  int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
                 st->cur_dts = wrap_timestamp(st, st->cur_dts);
         }
 
+        if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT_TS) &&
+            (pkt->flags & AV_PKT_FLAG_HEADER_CORRUPT)) {
+            pkt->pts = pkt->dts = AV_NOPTS_VALUE;
+            av_log(s, AV_LOG_WARNING,
+                   "Discarded timestamp on corrupted packet (stream = %d)\n",
+                   pkt->stream_index);
+        }
+
         pkt->dts = wrap_timestamp(st, pkt->dts);
         pkt->pts = wrap_timestamp(st, pkt->pts);