diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 2211d99ae8..404fbdf579 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -216,6 +216,13 @@ typedef struct MatroskaMuxContext {
 
     BlockContext        cur_block;
 
+    /* Used as temporary buffer to use the minimal amount of bytes
+     * to write the length field of EBML Masters.
+     * Every user has to reset the buffer after using it and
+     * different uses may not overlap. It is currently used in
+     * mkv_write_tag(). */
+    AVIOContext        *tmp_bc;
+
     AVPacket           *cur_audio_pkt;
 
     unsigned            nb_attachments;
@@ -247,6 +254,9 @@ typedef struct MatroskaMuxContext {
 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
 #define MAX_CUETRACKPOS_SIZE 40
 
+/** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */
+#define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23)
+
 /** Seek preroll value for opus */
 #define OPUS_SEEK_PREROLL 80000000
 
@@ -814,6 +824,7 @@ static void mkv_deinit(AVFormatContext *s)
     ffio_free_dyn_buf(&mkv->info.bc);
     ffio_free_dyn_buf(&mkv->track.bc);
     ffio_free_dyn_buf(&mkv->tags.bc);
+    ffio_free_dyn_buf(&mkv->tmp_bc);
 
     av_freep(&mkv->cur_block.h2645_nalu_list.nalus);
     av_freep(&mkv->cues.entries);
@@ -1911,24 +1922,14 @@ static int mkv_write_simpletag(AVIOContext *pb, const AVDictionaryEntry *t)
     return ret;
 }
 
-static int mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext **pb,
-                                 ebml_master *tag, uint32_t elementid, uint64_t uid)
+static void mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext *pb,
+                                  uint32_t elementid, uint64_t uid)
 {
-    ebml_master targets;
-    int ret;
-
-    if (!*pb) {
-        ret = start_ebml_master_crc32(pb, mkv);
-        if (ret < 0)
-            return ret;
-    }
-
-    *tag    = start_ebml_master(*pb, MATROSKA_ID_TAG,        0);
-    targets = start_ebml_master(*pb, MATROSKA_ID_TAGTARGETS, 4 + 1 + 8);
+    ebml_master targets = start_ebml_master(pb, MATROSKA_ID_TAGTARGETS,
+                                            4 + 1 + 8);
     if (elementid)
-        put_ebml_uid(*pb, elementid, uid);
-    end_ebml_master(*pb, targets);
-    return 0;
+        put_ebml_uid(pb, elementid, uid);
+    end_ebml_master(pb, targets);
 }
 
 static int mkv_check_tag_name(const char *name, uint32_t elementid)
@@ -1946,29 +1947,41 @@ static int mkv_check_tag_name(const char *name, uint32_t elementid)
 }
 
 static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m,
-                         AVIOContext **pb, ebml_master *tag,
+                         AVIOContext **pb, unsigned reserved_size,
                          uint32_t elementid, uint64_t uid)
 {
     const AVDictionaryEntry *t = NULL;
-    ebml_master tag2;
-    int ret;
+    AVIOContext *const tmp_bc = mkv->tmp_bc;
+    uint8_t *buf;
+    int ret, size;
 
-    ret = mkv_write_tag_targets(mkv, pb, tag ? tag : &tag2, elementid, uid);
-    if (ret < 0)
-        return ret;
+    mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
 
     while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
         if (mkv_check_tag_name(t->key, elementid)) {
-            ret = mkv_write_simpletag(*pb, t);
+            ret = mkv_write_simpletag(tmp_bc, t);
             if (ret < 0)
-                return ret;
+                goto end;
         }
     }
+    if (reserved_size)
+        put_ebml_void(tmp_bc, reserved_size);
 
-    if (!tag)
-        end_ebml_master(*pb, tag2);
+    size = avio_get_dyn_buf(tmp_bc, &buf);
+    if (tmp_bc->error) {
+        ret = tmp_bc->error;
+        goto end;
+    }
+    if (!*pb) {
+        ret = start_ebml_master_crc32(pb, mkv);
+        if (ret < 0)
+            goto end;
+    }
+    put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size);
 
-    return 0;
+end:
+    ffio_reset_dyn_buf(tmp_bc);
+    return ret;
 }
 
 static int mkv_check_tag(const AVDictionary *m, uint32_t elementid)
@@ -1985,15 +1998,14 @@ static int mkv_check_tag(const AVDictionary *m, uint32_t elementid)
 static int mkv_write_tags(AVFormatContext *s)
 {
     MatroskaMuxContext *mkv = s->priv_data;
-    ebml_master tag, *tagp = IS_SEEKABLE(s->pb, mkv) ? &tag : NULL;
-    int i, ret;
+    int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
 
     mkv->wrote_tags = 1;
 
     ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
 
     if (mkv_check_tag(s->metadata, 0)) {
-        ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, NULL, 0, 0);
+        ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
         if (ret < 0)
             return ret;
     }
@@ -2005,29 +2017,16 @@ static int mkv_write_tags(AVFormatContext *s)
         if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT)
             continue;
 
-        if (!tagp && !mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID))
+        if (!seekable && !mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID))
             continue;
 
-        ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, tagp,
+        ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
+                            seekable ? DURATION_SIMPLETAG_SIZE : 0,
                             MATROSKA_ID_TAGTARGETS_TRACKUID, track->uid);
         if (ret < 0)
             return ret;
-
-        if (tagp) {
-            AVIOContext *pb = mkv->tags.bc;
-            ebml_master simpletag;
-
-            simpletag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG,
-                                          2 + 1 + 8 + 23);
-            put_ebml_string(pb, MATROSKA_ID_TAGNAME, "DURATION");
-            track->duration_offset = avio_tell(pb);
-
-            // Reserve space to write duration as a 20-byte string.
-            // 2 (ebml id) + 1 (data size) + 20 (data)
-            put_ebml_void(pb, 23);
-            end_ebml_master(pb, simpletag);
-            end_ebml_master(pb, tag);
-        }
+        if (seekable)
+            track->duration_offset = avio_tell(mkv->tags.bc) - DURATION_SIMPLETAG_SIZE;
     }
 
     if (mkv->nb_attachments && !IS_WEBM(mkv)) {
@@ -2041,7 +2040,7 @@ static int mkv_write_tags(AVFormatContext *s)
             if (!mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID))
                 continue;
 
-            ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, NULL,
+            ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
                                 MATROSKA_ID_TAGTARGETS_ATTACHUID, track->uid);
             if (ret < 0)
                 return ret;
@@ -2126,7 +2125,7 @@ static int mkv_write_chapters(AVFormatContext *s)
             ff_metadata_conv(&c->metadata, ff_mkv_metadata_conv, NULL);
 
             if (mkv_check_tag(c->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID)) {
-                ret = mkv_write_tag(mkv, c->metadata, tags, NULL,
+                ret = mkv_write_tag(mkv, c->metadata, tags, 0,
                                     MATROSKA_ID_TAGTARGETS_CHAPTERUID, uid);
                 if (ret < 0)
                     goto fail;
@@ -2326,6 +2325,10 @@ static int mkv_write_header(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     int ret, version = 2;
 
+    ret = avio_open_dyn_buf(&mkv->tmp_bc);
+    if (ret < 0)
+        return ret;
+
     if (!IS_WEBM(mkv) ||
         av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
         av_dict_get(s->metadata, "alpha_mode", NULL, 0))
@@ -3000,6 +3003,7 @@ after_cues:
 
     // update stream durations
     if (mkv->tags.bc) {
+        AVIOContext *tags_bc = mkv->tags.bc;
         int i;
         for (i = 0; i < s->nb_streams; ++i) {
             const AVStream     *st = s->streams[i];
@@ -3008,17 +3012,22 @@ after_cues:
             if (track->duration_offset > 0) {
                 double duration_sec = track->duration * av_q2d(st->time_base);
                 char duration_string[20] = "";
+                ebml_master simpletag;
 
                 av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
                        track->duration);
 
-                avio_seek(mkv->tags.bc, track->duration_offset, SEEK_SET);
+                avio_seek(tags_bc, track->duration_offset, SEEK_SET);
+                simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG,
+                                              2 + 1 + 8 + 23);
+                put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
 
                 snprintf(duration_string, 20, "%02d:%02d:%012.9f",
                          (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
                          fmod(duration_sec, 60));
 
-                put_ebml_binary(mkv->tags.bc, MATROSKA_ID_TAGSTRING, duration_string, 20);
+                put_ebml_binary(tags_bc, MATROSKA_ID_TAGSTRING, duration_string, 20);
+                end_ebml_master(tags_bc, simpletag);
             }
         }
 
diff --git a/tests/ref/fate/aac-autobsf-adtstoasc b/tests/ref/fate/aac-autobsf-adtstoasc
index b9bfb37142..8b6d9d96ee 100644
--- a/tests/ref/fate/aac-autobsf-adtstoasc
+++ b/tests/ref/fate/aac-autobsf-adtstoasc
@@ -1,5 +1,5 @@
-224607440d242dad567b8ee2ed3afeac *tests/data/fate/aac-autobsf-adtstoasc.matroska
-6651 tests/data/fate/aac-autobsf-adtstoasc.matroska
+29daa1e1985dac4baae0774a0b56149d *tests/data/fate/aac-autobsf-adtstoasc.matroska
+6644 tests/data/fate/aac-autobsf-adtstoasc.matroska
 #extradata 0:        2, 0x0030001c
 #tb 0: 1/1000
 #media_type 0: audio
diff --git a/tests/ref/fate/matroska-avoid-negative-ts b/tests/ref/fate/matroska-avoid-negative-ts
index f20918483c..129ea1be66 100644
--- a/tests/ref/fate/matroska-avoid-negative-ts
+++ b/tests/ref/fate/matroska-avoid-negative-ts
@@ -1,5 +1,5 @@
-804842437b2be0a1604ce33c6b08c800 *tests/data/fate/matroska-avoid-negative-ts.matroska
-973070 tests/data/fate/matroska-avoid-negative-ts.matroska
+fbe66be73d379073e0705891f290a6b2 *tests/data/fate/matroska-avoid-negative-ts.matroska
+973055 tests/data/fate/matroska-avoid-negative-ts.matroska
 #extradata 0:       22, 0x2885037c
 #tb 0: 1/1000
 #media_type 0: video
diff --git a/tests/ref/fate/matroska-dovi-write-config7 b/tests/ref/fate/matroska-dovi-write-config7
index 65c397b807..9207b14b5c 100644
--- a/tests/ref/fate/matroska-dovi-write-config7
+++ b/tests/ref/fate/matroska-dovi-write-config7
@@ -1,5 +1,5 @@
-7981e419c77ebd4180caafc0b45ade4d *tests/data/fate/matroska-dovi-write-config7.matroska
-72693 tests/data/fate/matroska-dovi-write-config7.matroska
+d23b8b0d0613a82ba36fdc27acf3ef5d *tests/data/fate/matroska-dovi-write-config7.matroska
+72672 tests/data/fate/matroska-dovi-write-config7.matroska
 #extradata 0:      116, 0x2b8d1669
 #extradata 1:      116, 0x2b8d1669
 #tb 0: 1/1000
diff --git a/tests/ref/fate/matroska-dovi-write-config8 b/tests/ref/fate/matroska-dovi-write-config8
index 9741921b4c..5feb7b8065 100644
--- a/tests/ref/fate/matroska-dovi-write-config8
+++ b/tests/ref/fate/matroska-dovi-write-config8
@@ -1,5 +1,5 @@
-a5f259b0d7590e1ef77e09c3a75d0801 *tests/data/fate/matroska-dovi-write-config8.matroska
-3600595 tests/data/fate/matroska-dovi-write-config8.matroska
+0047ca43aa8ab2948752e367f184dc1f *tests/data/fate/matroska-dovi-write-config8.matroska
+3600576 tests/data/fate/matroska-dovi-write-config8.matroska
 #extradata 0:      551, 0xa18acf66
 #extradata 1:        2, 0x00340022
 #tb 0: 1/1000
diff --git a/tests/ref/fate/matroska-dvbsub-remux b/tests/ref/fate/matroska-dvbsub-remux
index 7c543e5a7d..b7346b8f55 100644
--- a/tests/ref/fate/matroska-dvbsub-remux
+++ b/tests/ref/fate/matroska-dvbsub-remux
@@ -1,5 +1,5 @@
-e675d3a76a4720f3e65bf56ec6041fe1 *tests/data/fate/matroska-dvbsub-remux.matroska
-39025 tests/data/fate/matroska-dvbsub-remux.matroska
+77d210dc36d7a01988d159b3c902524c *tests/data/fate/matroska-dvbsub-remux.matroska
+39010 tests/data/fate/matroska-dvbsub-remux.matroska
 #extradata 0:        5, 0x00bb0064
 #extradata 1:        5, 0x00bb0064
 #tb 0: 1/1000
diff --git a/tests/ref/fate/matroska-flac-extradata-update b/tests/ref/fate/matroska-flac-extradata-update
index b0276f734d..e8812f51b5 100644
--- a/tests/ref/fate/matroska-flac-extradata-update
+++ b/tests/ref/fate/matroska-flac-extradata-update
@@ -1,5 +1,5 @@
-56ff5763fd81ad3bc02c22402cd685e2 *tests/data/fate/matroska-flac-extradata-update.matroska
-2008 tests/data/fate/matroska-flac-extradata-update.matroska
+732446e97bae29037ff0cd9963d4ac08 *tests/data/fate/matroska-flac-extradata-update.matroska
+1987 tests/data/fate/matroska-flac-extradata-update.matroska
 #extradata 0:       34, 0x7acb09e7
 #extradata 1:       34, 0x7acb09e7
 #extradata 2:       34, 0x443402dd
diff --git a/tests/ref/fate/matroska-h264-remux b/tests/ref/fate/matroska-h264-remux
index 4004afef9a..6edd88fba8 100644
--- a/tests/ref/fate/matroska-h264-remux
+++ b/tests/ref/fate/matroska-h264-remux
@@ -1,5 +1,5 @@
-e4b0303e7bfbc8394bf47e11ab991d48 *tests/data/fate/matroska-h264-remux.matroska
-2036061 tests/data/fate/matroska-h264-remux.matroska
+3c00191234d5c4d77151d38a86403101 *tests/data/fate/matroska-h264-remux.matroska
+2036033 tests/data/fate/matroska-h264-remux.matroska
 #tb 0: 1/25
 #media_type 0: video
 #codec_id 0: rawvideo
diff --git a/tests/ref/fate/matroska-mastering-display-metadata b/tests/ref/fate/matroska-mastering-display-metadata
index b5d8812ee4..5b23e9506c 100644
--- a/tests/ref/fate/matroska-mastering-display-metadata
+++ b/tests/ref/fate/matroska-mastering-display-metadata
@@ -1,5 +1,5 @@
-62866a79d93bbe699d6abb6592ceb50e *tests/data/fate/matroska-mastering-display-metadata.matroska
-1669585 tests/data/fate/matroska-mastering-display-metadata.matroska
+69a904789151abaee46033391a4fce46 *tests/data/fate/matroska-mastering-display-metadata.matroska
+1669555 tests/data/fate/matroska-mastering-display-metadata.matroska
 #extradata 0:        4, 0x040901a3
 #extradata 3:      200, 0x506463a8
 #tb 0: 1/1000
diff --git a/tests/ref/fate/matroska-move-cues-to-front b/tests/ref/fate/matroska-move-cues-to-front
index 4f627bfdfb..78697b2eae 100644
--- a/tests/ref/fate/matroska-move-cues-to-front
+++ b/tests/ref/fate/matroska-move-cues-to-front
@@ -1,5 +1,5 @@
-62064edeb4a621fd0097f04c410c165c *tests/data/fate/matroska-move-cues-to-front.matroska
-23210303 tests/data/fate/matroska-move-cues-to-front.matroska
+8ebfcf15768bbe66611e349383fbf26a *tests/data/fate/matroska-move-cues-to-front.matroska
+23210287 tests/data/fate/matroska-move-cues-to-front.matroska
 #tb 0: 1/1000
 #media_type 0: audio
 #codec_id 0: pcm_s24be
diff --git a/tests/ref/fate/matroska-mpegts-remux b/tests/ref/fate/matroska-mpegts-remux
index 148bc0ffbc..2faab0892f 100644
--- a/tests/ref/fate/matroska-mpegts-remux
+++ b/tests/ref/fate/matroska-mpegts-remux
@@ -1,5 +1,5 @@
-4e6253c1f5f96ff64ae855dea426547d *tests/data/fate/matroska-mpegts-remux.matroska
-6509 tests/data/fate/matroska-mpegts-remux.matroska
+acaf3ebe07afe9815e0a984921d3ab87 *tests/data/fate/matroska-mpegts-remux.matroska
+6494 tests/data/fate/matroska-mpegts-remux.matroska
 #tb 0: 1/1000
 #media_type 0: audio
 #codec_id 0: ac3
diff --git a/tests/ref/fate/matroska-ms-mode b/tests/ref/fate/matroska-ms-mode
index f911d744d1..f90a7e431f 100644
--- a/tests/ref/fate/matroska-ms-mode
+++ b/tests/ref/fate/matroska-ms-mode
@@ -1,5 +1,5 @@
-20e86febdfa8a2d5aefd83f89d5d99a7 *tests/data/fate/matroska-ms-mode.matroska
-413101 tests/data/fate/matroska-ms-mode.matroska
+afb4fc9b2ca6cafc03d8029fdf4da876 *tests/data/fate/matroska-ms-mode.matroska
+413086 tests/data/fate/matroska-ms-mode.matroska
 #extradata 0:       40, 0x54290c93
 #extradata 1:      114, 0xb6c80771
 #tb 0: 1/1000
diff --git a/tests/ref/fate/matroska-pgs-remux b/tests/ref/fate/matroska-pgs-remux
index 4fab0ffdd8..ed5beb7463 100644
--- a/tests/ref/fate/matroska-pgs-remux
+++ b/tests/ref/fate/matroska-pgs-remux
@@ -1,5 +1,5 @@
-9aa538611b5f3bd0455b2afd3dafe08d *tests/data/fate/matroska-pgs-remux.matroska
-49751 tests/data/fate/matroska-pgs-remux.matroska
+6703d4e9a905bc5a1fc529776e8ffb51 *tests/data/fate/matroska-pgs-remux.matroska
+49744 tests/data/fate/matroska-pgs-remux.matroska
 #tb 0: 1/1000
 #media_type 0: subtitle
 #codec_id 0: hdmv_pgs_subtitle
diff --git a/tests/ref/fate/matroska-pgs-remux-durations b/tests/ref/fate/matroska-pgs-remux-durations
index ba649d0ddf..a255640fa6 100644
--- a/tests/ref/fate/matroska-pgs-remux-durations
+++ b/tests/ref/fate/matroska-pgs-remux-durations
@@ -1,5 +1,5 @@
-20fa9d515b08718c42d3e3a8a4a8bf5d *tests/data/fate/matroska-pgs-remux-durations.matroska
-49763 tests/data/fate/matroska-pgs-remux-durations.matroska
+a547f8b6463a60e5ef2e9a2b117c4dfa *tests/data/fate/matroska-pgs-remux-durations.matroska
+49756 tests/data/fate/matroska-pgs-remux-durations.matroska
 #tb 0: 1/1000
 #media_type 0: subtitle
 #codec_id 0: hdmv_pgs_subtitle
diff --git a/tests/ref/fate/matroska-qt-mode b/tests/ref/fate/matroska-qt-mode
index f16382b9b3..17a8e6619e 100644
--- a/tests/ref/fate/matroska-qt-mode
+++ b/tests/ref/fate/matroska-qt-mode
@@ -1,5 +1,5 @@
-7f3a5c46ccb2b95074bdabc034ee71d1 *tests/data/fate/matroska-qt-mode.matroska
-1884236 tests/data/fate/matroska-qt-mode.matroska
+39a3d6faff69892c8a20301560b80b14 *tests/data/fate/matroska-qt-mode.matroska
+1884224 tests/data/fate/matroska-qt-mode.matroska
 #extradata 0:       90, 0x817d0185
 #tb 0: 1/1000
 #media_type 0: video
diff --git a/tests/ref/fate/matroska-spherical-mono-remux b/tests/ref/fate/matroska-spherical-mono-remux
index 6b7d64f6d5..6b975c1586 100644
--- a/tests/ref/fate/matroska-spherical-mono-remux
+++ b/tests/ref/fate/matroska-spherical-mono-remux
@@ -1,5 +1,5 @@
-28d9277e1992fa362e7eac7a16261fde *tests/data/fate/matroska-spherical-mono-remux.matroska
-161569 tests/data/fate/matroska-spherical-mono-remux.matroska
+31cdace875cb696973d5a493ce776eea *tests/data/fate/matroska-spherical-mono-remux.matroska
+161554 tests/data/fate/matroska-spherical-mono-remux.matroska
 #extradata 0:       43, 0x2b0e0d7b
 #extradata 1:       43, 0x2b0e0d7b
 #tb 0: 1/1000
diff --git a/tests/ref/fate/matroska-vp8-alpha-remux b/tests/ref/fate/matroska-vp8-alpha-remux
index c17e8d0587..17c15ec9df 100644
--- a/tests/ref/fate/matroska-vp8-alpha-remux
+++ b/tests/ref/fate/matroska-vp8-alpha-remux
@@ -1,5 +1,5 @@
-58147987d42f32d105d96b24b0755257 *tests/data/fate/matroska-vp8-alpha-remux.matroska
-235018 tests/data/fate/matroska-vp8-alpha-remux.matroska
+fd4f24bf776a2f84e01b0aa7fdfece38 *tests/data/fate/matroska-vp8-alpha-remux.matroska
+235011 tests/data/fate/matroska-vp8-alpha-remux.matroska
 #tb 0: 1/1000
 #media_type 0: video
 #codec_id 0: vp8
diff --git a/tests/ref/fate/matroska-zero-length-block b/tests/ref/fate/matroska-zero-length-block
index 924cec1e3f..bef887d58b 100644
--- a/tests/ref/fate/matroska-zero-length-block
+++ b/tests/ref/fate/matroska-zero-length-block
@@ -1,5 +1,5 @@
-f37ba7e8a30eaa33c1fd0ef77447fb41 *tests/data/fate/matroska-zero-length-block.matroska
-636 tests/data/fate/matroska-zero-length-block.matroska
+d9c8efb9d64addce3cac97e6c417d985 *tests/data/fate/matroska-zero-length-block.matroska
+630 tests/data/fate/matroska-zero-length-block.matroska
 #tb 0: 1/1000
 #media_type 0: subtitle
 #codec_id 0: subrip
diff --git a/tests/ref/fate/rgb24-mkv b/tests/ref/fate/rgb24-mkv
index a9461dfc1b..f2cff5ff96 100644
--- a/tests/ref/fate/rgb24-mkv
+++ b/tests/ref/fate/rgb24-mkv
@@ -1,5 +1,5 @@
-afc7b59819a8d69773b04757278fccef *tests/data/fate/rgb24-mkv.matroska
-58217 tests/data/fate/rgb24-mkv.matroska
+a46cb669137f18ac0a42012485e5e863 *tests/data/fate/rgb24-mkv.matroska
+58211 tests/data/fate/rgb24-mkv.matroska
 #tb 0: 1/10
 #media_type 0: video
 #codec_id 0: rawvideo
diff --git a/tests/ref/fate/webm-dash-chapters b/tests/ref/fate/webm-dash-chapters
index 840fc2d4a4..9444824c4c 100644
--- a/tests/ref/fate/webm-dash-chapters
+++ b/tests/ref/fate/webm-dash-chapters
@@ -1,5 +1,5 @@
-f97445ba73e182c888fa077348384083 *tests/data/fate/webm-dash-chapters.webm
-111156 tests/data/fate/webm-dash-chapters.webm
+aa3ca15fae0239d6bf67fa0658a8bc3b *tests/data/fate/webm-dash-chapters.webm
+111150 tests/data/fate/webm-dash-chapters.webm
 #extradata 0:     3469, 0xc6769ddc
 #tb 0: 1/1000
 #media_type 0: audio
diff --git a/tests/ref/fate/webm-webvtt-remux b/tests/ref/fate/webm-webvtt-remux
index d847bbee2b..88def03dbe 100644
--- a/tests/ref/fate/webm-webvtt-remux
+++ b/tests/ref/fate/webm-webvtt-remux
@@ -1,5 +1,5 @@
-8620a6614f149fc49ab7f4552373943e *tests/data/fate/webm-webvtt-remux.webm
-6556 tests/data/fate/webm-webvtt-remux.webm
+0b43695bf27bbe48ea44e969d8908e1f *tests/data/fate/webm-webvtt-remux.webm
+6528 tests/data/fate/webm-webvtt-remux.webm
 #tb 0: 1/1000
 #media_type 0: subtitle
 #codec_id 0: webvtt
diff --git a/tests/ref/lavf-fate/av1.mkv b/tests/ref/lavf-fate/av1.mkv
index 7d7acae135..2008e1a932 100644
--- a/tests/ref/lavf-fate/av1.mkv
+++ b/tests/ref/lavf-fate/av1.mkv
@@ -1,3 +1,3 @@
-33e58aee4132d60e7ae21ecf3ae9e35f *tests/data/lavf-fate/lavf.av1.mkv
-55649 tests/data/lavf-fate/lavf.av1.mkv
+72a4713f9165c73574d40e8b81c5d70f *tests/data/lavf-fate/lavf.av1.mkv
+55642 tests/data/lavf-fate/lavf.av1.mkv
 tests/data/lavf-fate/lavf.av1.mkv CRC=0x7c27cc15
diff --git a/tests/ref/lavf/mka b/tests/ref/lavf/mka
index 5d4ab243c0..7af63e0779 100644
--- a/tests/ref/lavf/mka
+++ b/tests/ref/lavf/mka
@@ -1,3 +1,3 @@
-036f852ae7ae18cf38ddc99ea39032fb *tests/data/lavf/lavf.mka
-43576 tests/data/lavf/lavf.mka
+e2d55cd3844fd7237a92181dbbcb08d9 *tests/data/lavf/lavf.mka
+43569 tests/data/lavf/lavf.mka
 tests/data/lavf/lavf.mka CRC=0x3a1da17e
diff --git a/tests/ref/lavf/mkv b/tests/ref/lavf/mkv
index e089bff61c..d9497a0a64 100644
--- a/tests/ref/lavf/mkv
+++ b/tests/ref/lavf/mkv
@@ -1,3 +1,3 @@
-c5e6db66eb81415c2a5128f4298850a8 *tests/data/lavf/lavf.mkv
-320417 tests/data/lavf/lavf.mkv
+17e637fc06015fea86428840418ffea2 *tests/data/lavf/lavf.mkv
+320403 tests/data/lavf/lavf.mkv
 tests/data/lavf/lavf.mkv CRC=0xec6c3c68
diff --git a/tests/ref/lavf/mkv_attachment b/tests/ref/lavf/mkv_attachment
index 18dc902ca7..a8cc075bf1 100644
--- a/tests/ref/lavf/mkv_attachment
+++ b/tests/ref/lavf/mkv_attachment
@@ -1,3 +1,3 @@
-a24aaf26dde6461f89acb7dd7c492015 *tests/data/lavf/lavf.mkv_attachment
-472567 tests/data/lavf/lavf.mkv_attachment
+3855fb336711517b32b0ec41e8505b4d *tests/data/lavf/lavf.mkv_attachment
+472553 tests/data/lavf/lavf.mkv_attachment
 tests/data/lavf/lavf.mkv_attachment CRC=0xec6c3c68
diff --git a/tests/ref/seek/lavf-mkv b/tests/ref/seek/lavf-mkv
index cbdbe9bfd1..6b64367802 100644
--- a/tests/ref/seek/lavf-mkv
+++ b/tests/ref/seek/lavf-mkv
@@ -1,48 +1,48 @@
-ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    665 size:   208
+ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    651 size:   208
 ret: 0         st:-1 flags:0  ts:-1.000000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292283 size: 27834
 ret: 0         st: 0 flags:0  ts: 0.788000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292283 size: 27834
 ret: 0         st: 0 flags:1  ts:-0.317000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
 ret:-1         st: 1 flags:0  ts: 2.577000
 ret: 0         st: 1 flags:1  ts: 1.471000
-ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320138 size:   209
+ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320124 size:   209
 ret: 0         st:-1 flags:0  ts: 0.365002
-ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146849 size: 27925
+ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146835 size: 27925
 ret: 0         st:-1 flags:1  ts:-0.740831
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
 ret:-1         st: 0 flags:0  ts: 2.153000
 ret: 0         st: 0 flags:1  ts: 1.048000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292283 size: 27834
 ret: 0         st: 1 flags:0  ts:-0.058000
-ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    665 size:   208
+ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    651 size:   208
 ret: 0         st: 1 flags:1  ts: 2.836000
-ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320138 size:   209
+ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320124 size:   209
 ret:-1         st:-1 flags:0  ts: 1.730004
 ret: 0         st:-1 flags:1  ts: 0.624171
-ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146849 size: 27925
+ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146835 size: 27925
 ret: 0         st: 0 flags:0  ts:-0.482000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
 ret: 0         st: 0 flags:1  ts: 2.413000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292283 size: 27834
 ret:-1         st: 1 flags:0  ts: 1.307000
 ret: 0         st: 1 flags:1  ts: 0.201000
-ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    665 size:   208
+ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    651 size:   208
 ret: 0         st:-1 flags:0  ts:-0.904994
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
 ret: 0         st:-1 flags:1  ts: 1.989173
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292283 size: 27834
 ret: 0         st: 0 flags:0  ts: 0.883000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292283 size: 27834
 ret: 0         st: 0 flags:1  ts:-0.222000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
 ret:-1         st: 1 flags:0  ts: 2.672000
 ret: 0         st: 1 flags:1  ts: 1.566000
-ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320138 size:   209
+ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320124 size:   209
 ret: 0         st:-1 flags:0  ts: 0.460008
-ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146849 size: 27925
+ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146835 size: 27925
 ret: 0         st:-1 flags:1  ts:-0.645825
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    867 size: 27837
