[FFmpeg-devel,4/4] avformat/mpegtsenc: get rid of packet counting for sdt/pat/pmt

Submitted by Marton Balint on Aug. 14, 2019, 11:51 p.m.

Details

Message ID 20190814235130.5973-4-cus@passwd.hu
State New
Headers show

Commit Message

Marton Balint Aug. 14, 2019, 11:51 p.m.
The packet counting based approach caused excessive sdt/pat/pmt for VBR, so
let's use a timestamp based approach instead similar to how we emit PCRs.
SDT/PAT/PMT period should be consistent for both VBR and CBR from now on.

Also change the type of sdt_period and pat_period to AV_OPT_TYPE_DURATION so no
floating point math is necessary.

Fixes ticket #3714.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 doc/muxers.texi                               |   8 +-
 libavformat/mpegtsenc.c                       |  94 ++++++++-----------
 tests/ref/acodec/s302m                        |   4 +-
 tests/ref/fate/concat-demuxer-simple2-lavf-ts | 130 +++++++++++++-------------
 tests/ref/lavf/ts                             |   4 +-
 tests/ref/seek/lavf-ts                        |  26 +++---
 6 files changed, 124 insertions(+), 142 deletions(-)

Patch hide | download patch | download mbox

diff --git a/doc/muxers.texi b/doc/muxers.texi
index c27bfee4f5..20d31b279c 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1624,11 +1624,11 @@  Override the default PCR retransmission time in milliseconds. Default is
 20 ms is used for CBR streams, the highest multiple of the frame duration which
 is less than 100 ms is used for VBR streams.
 
-@item pat_period @var{double}
-Maximum time in seconds between PAT/PMT tables.
+@item pat_period @var{duration}
+Maximum time in seconds between PAT/PMT tables. Default is @code{0.1}.
 
-@item sdt_period @var{double}
-Maximum time in seconds between SDT tables.
+@item sdt_period @var{duration}
+Maximum time in seconds between SDT tables. Default is @code{0.5}.
 
 @item tables_version @var{integer}
 Set PAT, PMT and SDT version (default @code{0}, valid values are from 0 to 31, inclusively).
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 04da081ac0..1ed1b1cd22 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -76,10 +76,8 @@  typedef struct MpegTSWrite {
     MpegTSSection pat; /* MPEG-2 PAT table */
     MpegTSSection sdt; /* MPEG-2 SDT table context */
     MpegTSService **services;
-    int sdt_packet_count;
-    int sdt_packet_period;
-    int pat_packet_count;
-    int pat_packet_period;
+    int64_t sdt_period; /* SDT period in PCR time base */
+    int64_t pat_period; /* PAT/PMT period in PCR time base */
     int nb_services;
     int onid;
     int tsid;
@@ -106,8 +104,8 @@  typedef struct MpegTSWrite {
     int flags;
     int copyts;
     int tables_version;
-    double pat_period;
-    double sdt_period;
+    int64_t pat_period_us;
+    int64_t sdt_period_us;
     int64_t last_pat_ts;
     int64_t last_sdt_ts;
 
@@ -978,17 +976,6 @@  static int mpegts_init(AVFormatContext *s)
 
     av_freep(&pids);
 
-    if (ts->mux_rate > 1) {
-        ts->sdt_packet_period      = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
-                                     (TS_PACKET_SIZE * 8 * 1000);
-        ts->pat_packet_period      = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
-                                     (TS_PACKET_SIZE * 8 * 1000);
-    } else {
-        /* Arbitrary values, PAT/PMT will also be written on video key frames */
-        ts->sdt_packet_period = 200;
-        ts->pat_packet_period = 40;
-    }
-
     if (ts->copyts < 1)
         ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
 
@@ -996,24 +983,17 @@  static int mpegts_init(AVFormatContext *s)
 
     ts->last_pat_ts = AV_NOPTS_VALUE;
     ts->last_sdt_ts = AV_NOPTS_VALUE;
-    // The user specified a period, use only it
-    if (ts->pat_period < INT_MAX/2) {
-        ts->pat_packet_period = INT_MAX;
-    }
-    if (ts->sdt_period < INT_MAX/2) {
-        ts->sdt_packet_period = INT_MAX;
-    }
-
-    ts->pat_packet_count      = ts->pat_packet_period - 1;
-    ts->sdt_packet_count      = ts->sdt_packet_period - 1;
+    ts->pat_period = av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
+    ts->sdt_period = av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
 
     if (ts->mux_rate == 1)
         av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
     else
         av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
     av_log(s, AV_LOG_VERBOSE,
-           "sdt every %d, pat/pmt every %d pkts\n",
-           ts->sdt_packet_period, ts->pat_packet_period);
+           "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms\n",
+           av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
+           av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
 
     if (ts->m2ts_mode == -1) {
         if (av_match_ext(s->url, "m2ts")) {
@@ -1031,27 +1011,24 @@  fail:
 }
 
 /* send SDT, PAT and PMT tables regularly */
-static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
+static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int64_t pcr)
 {
     MpegTSWrite *ts = s->priv_data;
     int i;
 
-    if (++ts->sdt_packet_count == ts->sdt_packet_period ||
-        (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
-        (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
+    if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
+        (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
+        force_sdt
     ) {
-        ts->sdt_packet_count = 0;
-        if (dts != AV_NOPTS_VALUE)
-            ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
+        if (pcr != AV_NOPTS_VALUE)
+            ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
         mpegts_write_sdt(s);
     }
-    if (++ts->pat_packet_count == ts->pat_packet_period ||
-        (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
-        (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
+    if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
+        (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
         force_pat) {
-        ts->pat_packet_count = 0;
-        if (dts != AV_NOPTS_VALUE)
-            ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
+        if (pcr != AV_NOPTS_VALUE)
+            ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
         mpegts_write_pat(s);
         for (i = 0; i < ts->nb_services; i++)
             mpegts_write_pmt(s, ts->services[i]);
@@ -1181,18 +1158,30 @@  static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
     int afc_len, stuffing_len;
     int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
     int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
+    int force_sdt = 0;
 
     av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
     if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
         force_pat = 1;
     }
 
+    if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
+        force_pat = 1;
+        force_sdt = 1;
+        ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
+    }
+
     is_start = 1;
     while (payload_size > 0) {
-        int64_t pcr = -1; /* avoid warning */
+        int64_t pcr = AV_NOPTS_VALUE;
+        if (ts->mux_rate > 1)
+            pcr = get_pcr(ts, s->pb);
+        else if (dts != AV_NOPTS_VALUE)
+            pcr = (dts - delay) * 300;
 
-        retransmit_si_info(s, force_pat, dts);
+        retransmit_si_info(s, force_pat, force_sdt, pcr);
         force_pat = 0;
+        force_sdt = 0;
 
         write_pcr = 0;
         if (ts->mux_rate > 1) {
@@ -1230,8 +1219,7 @@  static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
                 /* recalculate write_pcr and possibly retransmit si_info */
                 continue;
             }
-        } else if (ts_st->pcr_period && dts != AV_NOPTS_VALUE) {
-            pcr = (dts - delay) * 300;
+        } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
             if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
                 ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
                 write_pcr = 1;
@@ -1555,12 +1543,6 @@  static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
     if (side_data)
         stream_id = side_data[0];
 
-    if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
-        ts->pat_packet_count = ts->pat_packet_period - 1;
-        ts->sdt_packet_count = ts->sdt_packet_period - 1;
-        ts->flags           &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
-    }
-
     if (ts->copyts < 1) {
         if (pts != AV_NOPTS_VALUE)
             pts += delay;
@@ -1965,11 +1947,11 @@  static const AVOption options[] = {
       offsetof(MpegTSWrite, pcr_period_ms), AV_OPT_TYPE_INT,
       { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     { "pat_period", "PAT/PMT retransmission time limit in seconds",
-      offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
-      { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+      offsetof(MpegTSWrite, pat_period_us), AV_OPT_TYPE_DURATION,
+      { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     { "sdt_period", "SDT retransmission time limit in seconds",
-      offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
-      { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+      offsetof(MpegTSWrite, sdt_period_us), AV_OPT_TYPE_DURATION,
+      { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     { NULL },
 };
 
diff --git a/tests/ref/acodec/s302m b/tests/ref/acodec/s302m
index 63e39edef9..2919ed6e55 100644
--- a/tests/ref/acodec/s302m
+++ b/tests/ref/acodec/s302m
@@ -1,4 +1,4 @@ 
-165d022ab86306d069797acff0c1e295 *tests/data/fate/acodec-s302m.mpegts
-1589164 tests/data/fate/acodec-s302m.mpegts
+0bf5457fd41a22fc5cdd99ae5ad4e273 *tests/data/fate/acodec-s302m.mpegts
+1527688 tests/data/fate/acodec-s302m.mpegts
 31f25a0020fd9017de9c3c608316854b *tests/data/fate/acodec-s302m.out.wav
 stddev:  986.94 PSNR: 36.44 MAXDIFF:18571 bytes:  1058400/  1056708
diff --git a/tests/ref/fate/concat-demuxer-simple2-lavf-ts b/tests/ref/fate/concat-demuxer-simple2-lavf-ts
index e5cf18bbce..0f03d6e06b 100644
--- a/tests/ref/fate/concat-demuxer-simple2-lavf-ts
+++ b/tests/ref/fate/concat-demuxer-simple2-lavf-ts
@@ -1,20 +1,20 @@ 
 video|1|982|0.010911|-2618|-0.029089|3600|0.040000|N/A|N/A|24801|564|K_MPEGTS Stream ID
 
-video|1|4582|0.050911|982|0.010911|3600|0.040000|N/A|N/A|16429|27072|__MPEGTS Stream ID
+video|1|4582|0.050911|982|0.010911|3600|0.040000|N/A|N/A|16429|25944|__MPEGTS Stream ID
 
-video|1|8182|0.090911|4582|0.050911|3600|0.040000|N/A|N/A|14508|44932|__MPEGTS Stream ID
+video|1|8182|0.090911|4582|0.050911|3600|0.040000|N/A|N/A|14508|42864|__MPEGTS Stream ID
 
-video|1|11782|0.130911|8182|0.090911|3600|0.040000|N/A|N/A|12622|60536|__MPEGTS Stream ID
+video|1|11782|0.130911|8182|0.090911|3600|0.040000|N/A|N/A|12622|58092|__MPEGTS Stream ID
 
-video|1|15382|0.170911|11782|0.130911|3600|0.040000|N/A|N/A|13393|74260|__MPEGTS Stream ID
+video|1|15382|0.170911|11782|0.130911|3600|0.040000|N/A|N/A|13393|71064|__MPEGTS Stream ID
 
-video|1|18982|0.210911|15382|0.170911|3600|0.040000|N/A|N/A|13092|88924|__MPEGTS Stream ID
+video|1|18982|0.210911|15382|0.170911|3600|0.040000|N/A|N/A|13092|84788|__MPEGTS Stream ID
 
-video|1|22582|0.250911|18982|0.210911|3600|0.040000|N/A|N/A|12755|102836|__MPEGTS Stream ID
+video|1|22582|0.250911|18982|0.210911|3600|0.040000|N/A|N/A|12755|98700|__MPEGTS Stream ID
 
-video|1|26182|0.290911|22582|0.250911|3600|0.040000|N/A|N/A|12023|116748|__MPEGTS Stream ID
+video|1|26182|0.290911|22582|0.250911|3600|0.040000|N/A|N/A|12023|111860|__MPEGTS Stream ID
 
-audio|0|0|0.000000|0|0.000000|2351|0.026122|N/A|N/A|208|159988|K_MPEGTS Stream ID
+audio|0|0|0.000000|0|0.000000|2351|0.026122|N/A|N/A|208|152844|K_MPEGTS Stream ID
 
 audio|0|2351|0.026122|2351|0.026122|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|4702|0.052244|4702|0.052244|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -29,27 +29,27 @@  audio|0|23510|0.261222|23510|0.261222|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|25861|0.287344|25861|0.287344|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|28212|0.313467|28212|0.313467|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|30563|0.339589|30563|0.339589|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|29782|0.330911|26182|0.290911|3600|0.040000|N/A|N/A|14098|130096|__MPEGTS Stream ID
+video|1|29782|0.330911|26182|0.290911|3600|0.040000|N/A|N/A|14098|124268|__MPEGTS Stream ID
 
-video|1|33382|0.370911|29782|0.330911|3600|0.040000|N/A|N/A|13329|145324|__MPEGTS Stream ID
+video|1|33382|0.370911|29782|0.330911|3600|0.040000|N/A|N/A|13329|139120|__MPEGTS Stream ID
 
-video|1|36982|0.410911|33382|0.370911|3600|0.040000|N/A|N/A|12135|162996|__MPEGTS Stream ID
+video|1|36982|0.410911|33382|0.370911|3600|0.040000|N/A|N/A|12135|155852|__MPEGTS Stream ID
 
-video|1|40582|0.450911|36982|0.410911|3600|0.040000|N/A|N/A|12282|176344|__MPEGTS Stream ID
+video|1|40582|0.450911|36982|0.410911|3600|0.040000|N/A|N/A|12282|168448|__MPEGTS Stream ID
 
-video|1|44182|0.490911|40582|0.450911|3600|0.040000|N/A|N/A|24786|189692|K_MPEGTS Stream ID
+video|1|44182|0.490911|40582|0.450911|3600|0.040000|N/A|N/A|24786|181420|K_MPEGTS Stream ID
 
-video|1|47782|0.530911|44182|0.490911|3600|0.040000|N/A|N/A|17440|216388|__MPEGTS Stream ID
+video|1|47782|0.530911|44182|0.490911|3600|0.040000|N/A|N/A|17440|206988|__MPEGTS Stream ID
 
-video|1|51382|0.570911|47782|0.530911|3600|0.040000|N/A|N/A|15019|235000|__MPEGTS Stream ID
+video|1|51382|0.570911|47782|0.530911|3600|0.040000|N/A|N/A|15019|224848|__MPEGTS Stream ID
 
-video|1|54982|0.610911|51382|0.570911|3600|0.040000|N/A|N/A|13449|251356|__MPEGTS Stream ID
+video|1|54982|0.610911|51382|0.570911|3600|0.040000|N/A|N/A|13449|240640|__MPEGTS Stream ID
 
-video|1|58582|0.650911|54982|0.610911|3600|0.040000|N/A|N/A|12398|266020|__MPEGTS Stream ID
+video|1|58582|0.650911|54982|0.610911|3600|0.040000|N/A|N/A|12398|254552|__MPEGTS Stream ID
 
-video|1|62182|0.690911|58582|0.650911|3600|0.040000|N/A|N/A|13455|279744|__MPEGTS Stream ID
+video|1|62182|0.690911|58582|0.650911|3600|0.040000|N/A|N/A|13455|267336|__MPEGTS Stream ID
 
-audio|0|32915|0.365722|32915|0.365722|2351|0.026122|N/A|N/A|209|322608|K_MPEGTS Stream ID
+audio|0|32915|0.365722|32915|0.365722|2351|0.026122|N/A|N/A|209|308508|K_MPEGTS Stream ID
 
 audio|0|35266|0.391844|35266|0.391844|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|37617|0.417967|37617|0.417967|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -64,17 +64,17 @@  audio|0|56425|0.626944|56425|0.626944|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|58776|0.653067|58776|0.653067|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|61127|0.679189|61127|0.679189|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|63478|0.705311|63478|0.705311|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|65782|0.730911|62182|0.690911|3600|0.040000|N/A|N/A|13836|294408|__MPEGTS Stream ID
+video|1|65782|0.730911|62182|0.690911|3600|0.040000|N/A|N/A|13836|281624|__MPEGTS Stream ID
 
-video|1|69382|0.770911|65782|0.730911|3600|0.040000|N/A|N/A|12163|309448|__MPEGTS Stream ID
+video|1|69382|0.770911|65782|0.730911|3600|0.040000|N/A|N/A|12163|295912|__MPEGTS Stream ID
 
-video|1|72982|0.810911|69382|0.770911|3600|0.040000|N/A|N/A|12692|325992|__MPEGTS Stream ID
+video|1|72982|0.810911|69382|0.770911|3600|0.040000|N/A|N/A|12692|311516|__MPEGTS Stream ID
 
-video|1|76582|0.850911|72982|0.810911|3600|0.040000|N/A|N/A|10824|339528|__MPEGTS Stream ID
+video|1|76582|0.850911|72982|0.810911|3600|0.040000|N/A|N/A|10824|325052|__MPEGTS Stream ID
 
-video|1|80182|0.890911|76582|0.850911|3600|0.040000|N/A|N/A|11286|351372|__MPEGTS Stream ID
+video|1|80182|0.890911|76582|0.850911|3600|0.040000|N/A|N/A|11286|336144|__MPEGTS Stream ID
 
-audio|0|65829|0.731433|65829|0.731433|2351|0.026122|N/A|N/A|209|404576|K_MPEGTS Stream ID
+audio|0|65829|0.731433|65829|0.731433|2351|0.026122|N/A|N/A|209|386716|K_MPEGTS Stream ID
 
 audio|0|68180|0.757556|68180|0.757556|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|70531|0.783678|70531|0.783678|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -86,26 +86,26 @@  audio|0|82286|0.914289|82286|0.914289|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|84637|0.940411|84637|0.940411|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|86988|0.966533|86988|0.966533|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|89339|0.992656|89339|0.992656|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|83782|0.930911|80182|0.890911|3600|0.040000|N/A|N/A|12678|363592|__MPEGTS Stream ID
+video|1|83782|0.930911|80182|0.890911|3600|0.040000|N/A|N/A|12678|347800|__MPEGTS Stream ID
 
-video|1|87382|0.970911|83782|0.930911|3600|0.040000|N/A|N/A|24711|377880|K_
+video|1|87382|0.970911|83782|0.930911|3600|0.040000|N/A|N/A|24711|361336|K_
 video|1|91964|1.021822|88364|0.981822|3600|0.040000|N/A|N/A|24801|564|K_MPEGTS Stream ID
 
-video|1|95564|1.061822|91964|1.021822|3600|0.040000|N/A|N/A|16429|27072|__MPEGTS Stream ID
+video|1|95564|1.061822|91964|1.021822|3600|0.040000|N/A|N/A|16429|25944|__MPEGTS Stream ID
 
-video|1|99164|1.101822|95564|1.061822|3600|0.040000|N/A|N/A|14508|44932|__MPEGTS Stream ID
+video|1|99164|1.101822|95564|1.061822|3600|0.040000|N/A|N/A|14508|42864|__MPEGTS Stream ID
 
-video|1|102764|1.141822|99164|1.101822|3600|0.040000|N/A|N/A|12622|60536|__MPEGTS Stream ID
+video|1|102764|1.141822|99164|1.101822|3600|0.040000|N/A|N/A|12622|58092|__MPEGTS Stream ID
 
-video|1|106364|1.181822|102764|1.141822|3600|0.040000|N/A|N/A|13393|74260|__MPEGTS Stream ID
+video|1|106364|1.181822|102764|1.141822|3600|0.040000|N/A|N/A|13393|71064|__MPEGTS Stream ID
 
-video|1|109964|1.221822|106364|1.181822|3600|0.040000|N/A|N/A|13092|88924|__MPEGTS Stream ID
+video|1|109964|1.221822|106364|1.181822|3600|0.040000|N/A|N/A|13092|84788|__MPEGTS Stream ID
 
-video|1|113564|1.261822|109964|1.221822|3600|0.040000|N/A|N/A|12755|102836|__MPEGTS Stream ID
+video|1|113564|1.261822|109964|1.221822|3600|0.040000|N/A|N/A|12755|98700|__MPEGTS Stream ID
 
-video|1|117164|1.301822|113564|1.261822|3600|0.040000|N/A|N/A|12023|116748|__MPEGTS Stream ID
+video|1|117164|1.301822|113564|1.261822|3600|0.040000|N/A|N/A|12023|111860|__MPEGTS Stream ID
 
-audio|0|90982|1.010911|90982|1.010911|2351|0.026122|N/A|N/A|208|159988|K_MPEGTS Stream ID
+audio|0|90982|1.010911|90982|1.010911|2351|0.026122|N/A|N/A|208|152844|K_MPEGTS Stream ID
 
 audio|0|93333|1.037033|93333|1.037033|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|95684|1.063156|95684|1.063156|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -120,27 +120,27 @@  audio|0|114492|1.272133|114492|1.272133|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|116843|1.298256|116843|1.298256|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|119194|1.324378|119194|1.324378|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|121545|1.350500|121545|1.350500|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|120764|1.341822|117164|1.301822|3600|0.040000|N/A|N/A|14098|130096|__MPEGTS Stream ID
+video|1|120764|1.341822|117164|1.301822|3600|0.040000|N/A|N/A|14098|124268|__MPEGTS Stream ID
 
-video|1|124364|1.381822|120764|1.341822|3600|0.040000|N/A|N/A|13329|145324|__MPEGTS Stream ID
+video|1|124364|1.381822|120764|1.341822|3600|0.040000|N/A|N/A|13329|139120|__MPEGTS Stream ID
 
-video|1|127964|1.421822|124364|1.381822|3600|0.040000|N/A|N/A|12135|162996|__MPEGTS Stream ID
+video|1|127964|1.421822|124364|1.381822|3600|0.040000|N/A|N/A|12135|155852|__MPEGTS Stream ID
 
-video|1|131564|1.461822|127964|1.421822|3600|0.040000|N/A|N/A|12282|176344|__MPEGTS Stream ID
+video|1|131564|1.461822|127964|1.421822|3600|0.040000|N/A|N/A|12282|168448|__MPEGTS Stream ID
 
-video|1|135164|1.501822|131564|1.461822|3600|0.040000|N/A|N/A|24786|189692|K_MPEGTS Stream ID
+video|1|135164|1.501822|131564|1.461822|3600|0.040000|N/A|N/A|24786|181420|K_MPEGTS Stream ID
 
-video|1|138764|1.541822|135164|1.501822|3600|0.040000|N/A|N/A|17440|216388|__MPEGTS Stream ID
+video|1|138764|1.541822|135164|1.501822|3600|0.040000|N/A|N/A|17440|206988|__MPEGTS Stream ID
 
-video|1|142364|1.581822|138764|1.541822|3600|0.040000|N/A|N/A|15019|235000|__MPEGTS Stream ID
+video|1|142364|1.581822|138764|1.541822|3600|0.040000|N/A|N/A|15019|224848|__MPEGTS Stream ID
 
-video|1|145964|1.621822|142364|1.581822|3600|0.040000|N/A|N/A|13449|251356|__MPEGTS Stream ID
+video|1|145964|1.621822|142364|1.581822|3600|0.040000|N/A|N/A|13449|240640|__MPEGTS Stream ID
 
-video|1|149564|1.661822|145964|1.621822|3600|0.040000|N/A|N/A|12398|266020|__MPEGTS Stream ID
+video|1|149564|1.661822|145964|1.621822|3600|0.040000|N/A|N/A|12398|254552|__MPEGTS Stream ID
 
-video|1|153164|1.701822|149564|1.661822|3600|0.040000|N/A|N/A|13455|279744|__MPEGTS Stream ID
+video|1|153164|1.701822|149564|1.661822|3600|0.040000|N/A|N/A|13455|267336|__MPEGTS Stream ID
 
-audio|0|123897|1.376633|123897|1.376633|2351|0.026122|N/A|N/A|209|322608|K_MPEGTS Stream ID
+audio|0|123897|1.376633|123897|1.376633|2351|0.026122|N/A|N/A|209|308508|K_MPEGTS Stream ID
 
 audio|0|126248|1.402756|126248|1.402756|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|128599|1.428878|128599|1.428878|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -155,17 +155,17 @@  audio|0|147407|1.637856|147407|1.637856|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|149758|1.663978|149758|1.663978|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|152109|1.690100|152109|1.690100|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|154460|1.716222|154460|1.716222|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|156764|1.741822|153164|1.701822|3600|0.040000|N/A|N/A|13836|294408|__MPEGTS Stream ID
+video|1|156764|1.741822|153164|1.701822|3600|0.040000|N/A|N/A|13836|281624|__MPEGTS Stream ID
 
-video|1|160364|1.781822|156764|1.741822|3600|0.040000|N/A|N/A|12163|309448|__MPEGTS Stream ID
+video|1|160364|1.781822|156764|1.741822|3600|0.040000|N/A|N/A|12163|295912|__MPEGTS Stream ID
 
-video|1|163964|1.821822|160364|1.781822|3600|0.040000|N/A|N/A|12692|325992|__MPEGTS Stream ID
+video|1|163964|1.821822|160364|1.781822|3600|0.040000|N/A|N/A|12692|311516|__MPEGTS Stream ID
 
-video|1|167564|1.861822|163964|1.821822|3600|0.040000|N/A|N/A|10824|339528|__MPEGTS Stream ID
+video|1|167564|1.861822|163964|1.821822|3600|0.040000|N/A|N/A|10824|325052|__MPEGTS Stream ID
 
-video|1|171164|1.901822|167564|1.861822|3600|0.040000|N/A|N/A|11286|351372|__MPEGTS Stream ID
+video|1|171164|1.901822|167564|1.861822|3600|0.040000|N/A|N/A|11286|336144|__MPEGTS Stream ID
 
-audio|0|156811|1.742344|156811|1.742344|2351|0.026122|N/A|N/A|209|404576|K_MPEGTS Stream ID
+audio|0|156811|1.742344|156811|1.742344|2351|0.026122|N/A|N/A|209|386716|K_MPEGTS Stream ID
 
 audio|0|159162|1.768467|159162|1.768467|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|161513|1.794589|161513|1.794589|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -177,16 +177,16 @@  audio|0|173268|1.925200|173268|1.925200|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|175619|1.951322|175619|1.951322|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|177970|1.977444|177970|1.977444|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|180321|2.003567|180321|2.003567|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|174764|1.941822|171164|1.901822|3600|0.040000|N/A|N/A|12678|363592|__MPEGTS Stream ID
+video|1|174764|1.941822|171164|1.901822|3600|0.040000|N/A|N/A|12678|347800|__MPEGTS Stream ID
 
-video|1|178364|1.981822|174764|1.941822|3600|0.040000|N/A|N/A|24711|377880|K_
-video|1|139582|1.550911|135982|1.510911|3600|0.040000|N/A|N/A|12692|325992|__MPEGTS Stream ID
+video|1|178364|1.981822|174764|1.941822|3600|0.040000|N/A|N/A|24711|361336|K_
+video|1|139582|1.550911|135982|1.510911|3600|0.040000|N/A|N/A|12692|311516|__MPEGTS Stream ID
 
-video|1|143182|1.590911|139582|1.550911|3600|0.040000|N/A|N/A|10824|339528|__MPEGTS Stream ID
+video|1|143182|1.590911|139582|1.550911|3600|0.040000|N/A|N/A|10824|325052|__MPEGTS Stream ID
 
-video|1|146782|1.630911|143182|1.590911|3600|0.040000|N/A|N/A|11286|351372|__MPEGTS Stream ID
+video|1|146782|1.630911|143182|1.590911|3600|0.040000|N/A|N/A|11286|336144|__MPEGTS Stream ID
 
-audio|0|132429|1.471433|132429|1.471433|2351|0.026122|N/A|N/A|209|404576|K_MPEGTS Stream ID
+audio|0|132429|1.471433|132429|1.471433|2351|0.026122|N/A|N/A|209|386716|K_MPEGTS Stream ID
 
 audio|0|134780|1.497556|134780|1.497556|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|137131|1.523678|137131|1.523678|2351|0.026122|N/A|N/A|209|N/A|K_
@@ -198,18 +198,18 @@  audio|0|148886|1.654289|148886|1.654289|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|151237|1.680411|151237|1.680411|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|153588|1.706533|153588|1.706533|2351|0.026122|N/A|N/A|209|N/A|K_
 audio|0|155939|1.732656|155939|1.732656|2351|0.026122|N/A|N/A|209|N/A|K_
-video|1|150382|1.670911|146782|1.630911|3600|0.040000|N/A|N/A|12678|363592|__MPEGTS Stream ID
+video|1|150382|1.670911|146782|1.630911|3600|0.040000|N/A|N/A|12678|347800|__MPEGTS Stream ID
 
-video|1|153982|1.710911|150382|1.670911|3600|0.040000|N/A|N/A|24711|377880|K_
-video|1|161182|1.790911|157582|1.750911|3600|0.040000|N/A|N/A|12135|162996|__MPEGTS Stream ID
+video|1|153982|1.710911|150382|1.670911|3600|0.040000|N/A|N/A|24711|361336|K_
+video|1|161182|1.790911|157582|1.750911|3600|0.040000|N/A|N/A|12135|155852|__MPEGTS Stream ID
 
-video|1|164782|1.830911|161182|1.790911|3600|0.040000|N/A|N/A|12282|176344|__MPEGTS Stream ID
+video|1|164782|1.830911|161182|1.790911|3600|0.040000|N/A|N/A|12282|168448|__MPEGTS Stream ID
 
-video|1|168382|1.870911|164782|1.830911|3600|0.040000|N/A|N/A|24786|189692|K_MPEGTS Stream ID
+video|1|168382|1.870911|164782|1.830911|3600|0.040000|N/A|N/A|24786|181420|K_MPEGTS Stream ID
 
-video|1|171982|1.910911|168382|1.870911|3600|0.040000|N/A|N/A|17440|216388|__MPEGTS Stream ID
+video|1|171982|1.910911|168382|1.870911|3600|0.040000|N/A|N/A|17440|206988|__MPEGTS Stream ID
 
-video|1|175582|1.950911|171982|1.910911|3600|0.040000|N/A|N/A|15019|235000|__MPEGTS Stream ID
+video|1|175582|1.950911|171982|1.910911|3600|0.040000|N/A|N/A|15019|224848|__MPEGTS Stream ID
 
 0|mp2|unknown|audio|1/44100|[3][0][0][0]|0x0003|s16p|44100|1|mono|0|N/A|0/0|0/0|1/90000|0|0.000000|N/A|N/A|64000|N/A|N/A|N/A|N/A|89|0|0|0|0|0|0|0|0|0|0|0|0
 1|mpeg2video|4|video|1/25|[2][0][0][0]|0x0002|352|288|0|0|1|1:1|11:9|yuv420p|8|tv|unknown|unknown|unknown|left|progressive|N/A|1|N/A|25/1|25/1|1/90000|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|60|0|0|0|0|0|0|0|0|0|0|0|0
diff --git a/tests/ref/lavf/ts b/tests/ref/lavf/ts
index 09960f84d3..b004fc1b1c 100644
--- a/tests/ref/lavf/ts
+++ b/tests/ref/lavf/ts
@@ -1,3 +1,3 @@ 
-38f4b14d43a0e416be8d598628997cbc *tests/data/lavf/lavf.ts
-407020 tests/data/lavf/lavf.ts
+371dc016eb3155116bea27e3b4eeb928 *tests/data/lavf/lavf.ts
+389160 tests/data/lavf/lavf.ts
 tests/data/lavf/lavf.ts CRC=0x71287e25
diff --git a/tests/ref/seek/lavf-ts b/tests/ref/seek/lavf-ts
index e57651ef9c..3347b7ead5 100644
--- a/tests/ref/seek/lavf-ts
+++ b/tests/ref/seek/lavf-ts
@@ -2,51 +2,51 @@  ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:0  ts:-1.000000
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 0 flags:1 dts: 1.880000 pts: 1.920000 pos: 189692 size: 24786
+ret: 0         st: 0 flags:1 dts: 1.880000 pts: 1.920000 pos: 181420 size: 24786
 ret: 0         st: 0 flags:0  ts: 0.788333
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts:-0.317500
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts: 2.576667
-ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 404576 size:   209
+ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 386716 size:   209
 ret: 0         st: 1 flags:1  ts: 1.470833
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 159988 size:   208
+ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st:-1 flags:0  ts: 0.365002
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts:-0.740831
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:0  ts: 2.153333
-ret: 0         st: 1 flags:1 dts: 1.794811 pts: 1.794811 pos: 322608 size:   209
+ret: 0         st: 1 flags:1 dts: 1.794811 pts: 1.794811 pos: 308508 size:   209
 ret: 0         st: 0 flags:1  ts: 1.047500
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts:-0.058333
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 159988 size:   208
+ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st: 1 flags:1  ts: 2.835833
-ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 404576 size:   209
+ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 386716 size:   209
 ret: 0         st:-1 flags:0  ts: 1.730004
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 159988 size:   208
+ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st:-1 flags:1  ts: 0.624171
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:0  ts:-0.481667
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts: 2.412500
-ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 404576 size:   209
+ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 386716 size:   209
 ret: 0         st: 1 flags:0  ts: 1.306667
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 159988 size:   208
+ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st: 1 flags:1  ts: 0.200844
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 159988 size:   208
+ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st:-1 flags:0  ts:-0.904994
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts: 1.989173
-ret: 0         st: 0 flags:0 dts: 1.960000 pts: 2.000000 pos: 235000 size: 15019
+ret: 0         st: 0 flags:0 dts: 1.960000 pts: 2.000000 pos: 224848 size: 15019
 ret: 0         st: 0 flags:0  ts: 0.883344
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts:-0.222489
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts: 2.671678
-ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 404576 size:   209
+ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 386716 size:   209
 ret: 0         st: 1 flags:1  ts: 1.565844
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 159988 size:   208
+ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st:-1 flags:0  ts: 0.460008
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts:-0.645825