diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 2f5f1cc56d..236dd3ae8d 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -424,6 +424,28 @@ static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
     writer->current_master_element = writer->nb_elements - 1;
 }
 
+static void ebml_writer_close_master(EbmlWriter *writer)
+{
+    EbmlElement *elem;
+    av_assert2(writer->current_master_element >= 0);
+    av_assert2(writer->current_master_element < writer->nb_elements);
+    elem = &writer->elements[writer->current_master_element];
+    av_assert2(elem->type == EBML_MASTER);
+    elem->priv.master.nb_elements = writer->nb_elements - writer->current_master_element - 1;
+    writer->current_master_element = elem->priv.master.containing_master;
+}
+
+static void ebml_writer_close_or_discard_master(EbmlWriter *writer)
+{
+    av_assert2(writer->nb_elements > 0);
+    if (writer->current_master_element == writer->nb_elements - 1) {
+        /* The master element has no children. Discard it. */
+        writer->nb_elements--;
+        return;
+    }
+    ebml_writer_close_master(writer);
+}
+
 static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
                                    const char *str)
 {
@@ -1101,98 +1123,93 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb,
     return ret;
 }
 
-static void mkv_write_video_color(AVIOContext *pb, const AVStream *st,
+#define MAX_VIDEO_COLOR_ELEMS 20
+static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
                                   const AVCodecParameters *par)
 {
-    /* 18 Elements with two bytes ID, one byte length field, 8 bytes payload
-     * a master element with two bytes ID and one byte length field
-     * plus another byte to stay clear of the end. */
-    uint8_t colour[(2 + 1 + 8) * 18 + (2 + 1) + 1];
-    FFIOContext buf;
-    AVIOContext *const dyn_cp = &buf.pub;
-    int colorinfo_size;
     const void *side_data;
 
-    ffio_init_context(&buf, colour, sizeof(colour), 1, NULL, NULL, NULL, NULL);
+    ebml_writer_open_master(writer, MATROSKA_ID_VIDEOCOLOR);
 
     if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
         par->color_trc < AVCOL_TRC_NB) {
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS,
-                      par->color_trc);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS,
+                             par->color_trc);
     }
     if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
         par->color_space < AVCOL_SPC_NB) {
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORMATRIXCOEFF, par->color_space);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMATRIXCOEFF,
+                             par->color_space);
     }
     if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
         par->color_primaries < AVCOL_PRI_NB) {
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORPRIMARIES, par->color_primaries);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORPRIMARIES,
+                             par->color_primaries);
     }
     if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
         par->color_range < AVCOL_RANGE_NB) {
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORRANGE, par->color_range);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORRANGE, par->color_range);
     }
     if (par->chroma_location != AVCHROMA_LOC_UNSPECIFIED &&
         par->chroma_location <= AVCHROMA_LOC_TOP) {
         int xpos, ypos;
 
         avcodec_enum_to_chroma_pos(&xpos, &ypos, par->chroma_location);
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ, (xpos >> 7) + 1);
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT, (ypos >> 7) + 1);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ,
+                             (xpos >> 7) + 1);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT,
+                             (ypos >> 7) + 1);
     }
 
     side_data = av_stream_get_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
                                         NULL);
     if (side_data) {
         const AVContentLightMetadata *metadata = side_data;
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORMAXCLL,  metadata->MaxCLL);
-        put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOCOLORMAXFALL, metadata->MaxFALL);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMAXCLL,
+                             metadata->MaxCLL);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMAXFALL,
+                             metadata->MaxFALL);
     }
 
     side_data = av_stream_get_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
                                         NULL);
     if (side_data) {
-        ebml_master meta_element = start_ebml_master(
-            dyn_cp, MATROSKA_ID_VIDEOCOLORMASTERINGMETA, 10 * (2 + 1 + 8));
         const AVMasteringDisplayMetadata *metadata = side_data;
+        ebml_writer_open_master(writer, MATROSKA_ID_VIDEOCOLORMASTERINGMETA);
         if (metadata->has_primaries) {
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_RX,
-                           av_q2d(metadata->display_primaries[0][0]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_RY,
-                           av_q2d(metadata->display_primaries[0][1]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_GX,
-                           av_q2d(metadata->display_primaries[1][0]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_GY,
-                           av_q2d(metadata->display_primaries[1][1]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_BX,
-                           av_q2d(metadata->display_primaries[2][0]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_BY,
-                           av_q2d(metadata->display_primaries[2][1]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_WHITEX,
-                           av_q2d(metadata->white_point[0]));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_WHITEY,
-                           av_q2d(metadata->white_point[1]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_RX,
+                                  av_q2d(metadata->display_primaries[0][0]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_RY,
+                                  av_q2d(metadata->display_primaries[0][1]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_GX,
+                                  av_q2d(metadata->display_primaries[1][0]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_GY,
+                                  av_q2d(metadata->display_primaries[1][1]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_BX,
+                                  av_q2d(metadata->display_primaries[2][0]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_BY,
+                                  av_q2d(metadata->display_primaries[2][1]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_WHITEX,
+                                  av_q2d(metadata->white_point[0]));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_WHITEY,
+                                  av_q2d(metadata->white_point[1]));
         }
         if (metadata->has_luminance) {
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX,
-                           av_q2d(metadata->max_luminance));
-            put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN,
-                           av_q2d(metadata->min_luminance));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX,
+                                  av_q2d(metadata->max_luminance));
+            ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN,
+                                  av_q2d(metadata->min_luminance));
         }
-        end_ebml_master(dyn_cp, meta_element);
+        ebml_writer_close_or_discard_master(writer);
     }
 
-    colorinfo_size = avio_tell(dyn_cp);
-    if (colorinfo_size)
-        put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLOR, colour, colorinfo_size);
+    ebml_writer_close_or_discard_master(writer);
 }
 
-static void mkv_write_video_projection(AVFormatContext *s, AVIOContext *pb,
-                                       const AVStream *st)
+#define MAX_VIDEO_PROJECTION_ELEMS 6
+static void mkv_write_video_projection(AVFormatContext *s, EbmlWriter *writer,
+                                       const AVStream *st, uint8_t private[])
 {
-    ebml_master projection;
-    uint8_t private[20];
-
     const AVSphericalMapping *spherical =
         (const AVSphericalMapping *)av_stream_get_side_data(st, AV_PKT_DATA_SPHERICAL,
                                                             NULL);
@@ -1209,91 +1226,92 @@ static void mkv_write_video_projection(AVFormatContext *s, AVIOContext *pb,
 
     // Maximally 4 8-byte elements with id-length 2 + 1 byte length field
     // and the private data of the AV_SPHERICAL_EQUIRECTANGULAR_TILE case
-    projection = start_ebml_master(pb, MATROSKA_ID_VIDEOPROJECTION,
-                                   4 * (2 + 1 + 8) + (2 + 1 + 20));
+    ebml_writer_open_master(writer, MATROSKA_ID_VIDEOPROJECTION);
 
     switch (spherical->projection) {
     case AV_SPHERICAL_EQUIRECTANGULAR:
-        put_ebml_uint(pb, MATROSKA_ID_VIDEOPROJECTIONTYPE,
-                      MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOPROJECTIONTYPE,
+                             MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
         break;
     case AV_SPHERICAL_EQUIRECTANGULAR_TILE:
-        put_ebml_uint(pb, MATROSKA_ID_VIDEOPROJECTIONTYPE,
-                      MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOPROJECTIONTYPE,
+                             MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
         AV_WB32(private,      0); // version + flags
         AV_WB32(private +  4, spherical->bound_top);
         AV_WB32(private +  8, spherical->bound_bottom);
         AV_WB32(private + 12, spherical->bound_left);
         AV_WB32(private + 16, spherical->bound_right);
-        put_ebml_binary(pb, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
-                        private, 20);
+        ebml_writer_add_bin(writer, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
+                            private, 20);
         break;
     case AV_SPHERICAL_CUBEMAP:
-        put_ebml_uint(pb, MATROSKA_ID_VIDEOPROJECTIONTYPE,
-                      MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOPROJECTIONTYPE,
+                             MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP);
         AV_WB32(private,     0); // version + flags
         AV_WB32(private + 4, 0); // layout
         AV_WB32(private + 8, spherical->padding);
-        put_ebml_binary(pb, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
-                        private, 12);
+        ebml_writer_add_bin(writer, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
+                            private, 12);
         break;
     default:
         av_assert0(0);
     }
 
     if (spherical->yaw)
-        put_ebml_float(pb, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW,
-                       (double) spherical->yaw   / (1 << 16));
+        ebml_writer_add_float(writer, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW,
+                              (double) spherical->yaw   / (1 << 16));
     if (spherical->pitch)
-        put_ebml_float(pb, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH,
+        ebml_writer_add_float(writer, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH,
                        (double) spherical->pitch / (1 << 16));
     if (spherical->roll)
-        put_ebml_float(pb, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL,
+        ebml_writer_add_float(writer, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL,
                        (double) spherical->roll  / (1 << 16));
 
-    end_ebml_master(pb, projection);
+    ebml_writer_close_master(writer);
 }
 
-static void mkv_write_field_order(AVIOContext *pb, int is_webm,
+#define MAX_FIELD_ORDER_ELEMS 2
+static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
                                   enum AVFieldOrder field_order)
 {
     switch (field_order) {
     case AV_FIELD_UNKNOWN:
         break;
     case AV_FIELD_PROGRESSIVE:
-        put_ebml_uint(pb, MATROSKA_ID_VIDEOFLAGINTERLACED,
-                      MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFLAGINTERLACED,
+                             MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE);
         break;
     case AV_FIELD_TT:
     case AV_FIELD_BB:
     case AV_FIELD_TB:
     case AV_FIELD_BT:
-        put_ebml_uint(pb, MATROSKA_ID_VIDEOFLAGINTERLACED,
-                      MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED);
+        ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFLAGINTERLACED,
+                             MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED);
         if (!is_webm) {
             switch (field_order) {
             case AV_FIELD_TT:
-                put_ebml_uint(pb, MATROSKA_ID_VIDEOFIELDORDER,
-                              MATROSKA_VIDEO_FIELDORDER_TT);
+                ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
+                                     MATROSKA_VIDEO_FIELDORDER_TT);
                 break;
             case AV_FIELD_BB:
-                put_ebml_uint(pb, MATROSKA_ID_VIDEOFIELDORDER,
-                              MATROSKA_VIDEO_FIELDORDER_BB);
+                ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
+                                     MATROSKA_VIDEO_FIELDORDER_BB);
                 break;
             case AV_FIELD_TB:
-                put_ebml_uint(pb, MATROSKA_ID_VIDEOFIELDORDER,
-                              MATROSKA_VIDEO_FIELDORDER_TB);
+                ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
+                                     MATROSKA_VIDEO_FIELDORDER_TB);
                 break;
             case AV_FIELD_BT:
-                put_ebml_uint(pb, MATROSKA_ID_VIDEOFIELDORDER,
-                              MATROSKA_VIDEO_FIELDORDER_BT);
+                ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
+                                     MATROSKA_VIDEO_FIELDORDER_BT);
                 break;
             }
         }
     }
 }
 
-static int mkv_write_stereo_mode(AVFormatContext *s, AVIOContext *pb,
+#define MAX_STEREO_MODE_ELEMS 1
+static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer,
                                  AVStream *st, int is_webm,
                                  int *h_width, int *h_height)
 {
@@ -1380,7 +1398,7 @@ static int mkv_write_stereo_mode(AVFormatContext *s, AVIOContext *pb,
     }
 
     // write StereoMode if format is valid
-    put_ebml_uint(pb, MATROSKA_ID_VIDEOSTEREOMODE, format);
+    ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOSTEREOMODE, format);
 
     return 0;
 }
@@ -1424,19 +1442,21 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
 {
     const AVDictionaryEntry *tag;
     int display_width_div = 1, display_height_div = 1;
-    ebml_master subinfo;
+    uint8_t color_space[4], projection_private[20];
+    EBML_WRITER(MAX_FIELD_ORDER_ELEMS + MAX_STEREO_MODE_ELEMS      +
+                MAX_VIDEO_COLOR_ELEMS + MAX_VIDEO_PROJECTION_ELEMS + 8);
     int ret;
 
-    subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
+    ebml_writer_open_master(&writer, MATROSKA_ID_TRACKVIDEO);
 
-    put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , par->width);
-    put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height);
+    ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELWIDTH , par->width);
+    ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height);
 
-    mkv_write_field_order(pb, IS_WEBM(mkv), par->field_order);
+    mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
 
     // check both side data and metadata for stereo information,
     // write the result to the bitstream if any is found
-    ret = mkv_write_stereo_mode(s, pb, st, IS_WEBM(mkv),
+    ret = mkv_write_stereo_mode(s, &writer, st, IS_WEBM(mkv),
                                 &display_width_div,
                                 &display_height_div);
     if (ret < 0)
@@ -1445,7 +1465,7 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
     if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
         ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
         (par->format == AV_PIX_FMT_YUVA420P)) {
-        put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1);
+        ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOALPHAMODE, 1);
     }
 
     // write DisplayWidth and DisplayHeight, they contain the size of
@@ -1458,35 +1478,42 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
         }
         if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
             if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
-                put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
-                put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div);
+                ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH,
+                                     d_width / display_width_div);
+                ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT,
+                                     par->height / display_height_div);
             } else {
                 AVRational display_aspect_ratio;
                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                             par->width  * (int64_t)st->sample_aspect_ratio.num,
                             par->height * (int64_t)st->sample_aspect_ratio.den,
                             1024 * 1024);
-                put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH,  display_aspect_ratio.num);
-                put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, display_aspect_ratio.den);
-                put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_DAR);
+                ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH,
+                                     display_aspect_ratio.num);
+                ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT,
+                                     display_aspect_ratio.den);
+                ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYUNIT,
+                                     MATROSKA_VIDEO_DISPLAYUNIT_DAR);
             }
         }
     } else if (display_width_div != 1 || display_height_div != 1) {
-        put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , par->width / display_width_div);
-        put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div);
+        ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH,
+                             par->width / display_width_div);
+        ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT,
+                             par->height / display_height_div);
     } else if (!IS_WEBM(mkv))
-        put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN);
+        ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYUNIT,
+                             MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN);
 
     if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
-        uint32_t color_space = av_le2ne32(par->codec_tag);
-        put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
+        AV_WL32(color_space, par->codec_tag);
+        ebml_writer_add_bin(&writer, MATROSKA_ID_VIDEOCOLORSPACE,
+                            color_space, sizeof(color_space));
     }
-    mkv_write_video_color(pb, st, par);
-    mkv_write_video_projection(s, pb, st);
+    mkv_write_video_color(&writer, st, par);
+    mkv_write_video_projection(s, &writer, st, projection_private);
 
-    end_ebml_master(pb, subinfo);
-
-    return 0;
+    return ebml_writer_write(&writer, pb);
 }
 
 static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak
index 295489d2cc..2fcac34d62 100644
--- a/tests/fate/matroska.mak
+++ b/tests/fate/matroska.mak
@@ -12,7 +12,7 @@ fate-matroska-prores-header-insertion-bz2: CMD = framecrc -i $(TARGET_SAMPLES)/m
 FATE_MATROSKA-$(call DEMMUX, MATROSKA, MATROSKA) += fate-matroska-remux
 fate-matroska-remux: CMD = md5pipe -i $(TARGET_SAMPLES)/vp9-test-vectors/vp90-2-2pass-akiyo.webm -color_trc 4 -c:v copy -fflags +bitexact -strict -2 -f matroska
 fate-matroska-remux: CMP = oneline
-fate-matroska-remux: REF = 26fabd90326e3de4bb6afe1b216ce232
+fate-matroska-remux: REF = b9c7b650349972c9dce42ab79b472917
 
 FATE_MATROSKA-$(call ALLYES, MATROSKA_DEMUXER VORBIS_PARSER) += fate-matroska-xiph-lacing
 fate-matroska-xiph-lacing: CMD = framecrc -i $(TARGET_SAMPLES)/mkv/xiph_lacing.mka -c:a copy
diff --git a/tests/ref/fate/matroska-dovi-write-config7 b/tests/ref/fate/matroska-dovi-write-config7
index b652fe411d..65c397b807 100644
--- a/tests/ref/fate/matroska-dovi-write-config7
+++ b/tests/ref/fate/matroska-dovi-write-config7
@@ -1,5 +1,5 @@
-058e95f1effefe3842b6174bba2693fc *tests/data/fate/matroska-dovi-write-config7.matroska
-72707 tests/data/fate/matroska-dovi-write-config7.matroska
+7981e419c77ebd4180caafc0b45ade4d *tests/data/fate/matroska-dovi-write-config7.matroska
+72693 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-h264-remux b/tests/ref/fate/matroska-h264-remux
index e55708ce37..4004afef9a 100644
--- a/tests/ref/fate/matroska-h264-remux
+++ b/tests/ref/fate/matroska-h264-remux
@@ -1,5 +1,5 @@
-ae0ccd32cc0d3bfc1eaa15c7fc985dfb *tests/data/fate/matroska-h264-remux.matroska
-2036075 tests/data/fate/matroska-h264-remux.matroska
+e4b0303e7bfbc8394bf47e11ab991d48 *tests/data/fate/matroska-h264-remux.matroska
+2036061 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 f13d60ff2f..a3055c7bc7 100644
--- a/tests/ref/fate/matroska-mastering-display-metadata
+++ b/tests/ref/fate/matroska-mastering-display-metadata
@@ -1,5 +1,5 @@
-64403411e52d7ba9081ab5692e422737 *tests/data/fate/matroska-mastering-display-metadata.matroska
-1669597 tests/data/fate/matroska-mastering-display-metadata.matroska
+62866a79d93bbe699d6abb6592ceb50e *tests/data/fate/matroska-mastering-display-metadata.matroska
+1669585 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-ms-mode b/tests/ref/fate/matroska-ms-mode
index 5fe052c39b..a38c0aa91c 100644
--- a/tests/ref/fate/matroska-ms-mode
+++ b/tests/ref/fate/matroska-ms-mode
@@ -1,5 +1,5 @@
-b3d928e92bc8b323793a237ce82f9437 *tests/data/fate/matroska-ms-mode.matroska
-413108 tests/data/fate/matroska-ms-mode.matroska
+20e86febdfa8a2d5aefd83f89d5d99a7 *tests/data/fate/matroska-ms-mode.matroska
+413101 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-qt-mode b/tests/ref/fate/matroska-qt-mode
index 770d9e7e91..f322df24c1 100644
--- a/tests/ref/fate/matroska-qt-mode
+++ b/tests/ref/fate/matroska-qt-mode
@@ -1,5 +1,5 @@
-1e525172caec2bf4e191a68c97bbbc51 *tests/data/fate/matroska-qt-mode.matroska
-1884243 tests/data/fate/matroska-qt-mode.matroska
+7f3a5c46ccb2b95074bdabc034ee71d1 *tests/data/fate/matroska-qt-mode.matroska
+1884236 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 24d4413a87..6b7d64f6d5 100644
--- a/tests/ref/fate/matroska-spherical-mono-remux
+++ b/tests/ref/fate/matroska-spherical-mono-remux
@@ -1,5 +1,5 @@
-9176856edc1ff2b401e323f422fd8e78 *tests/data/fate/matroska-spherical-mono-remux.matroska
-161583 tests/data/fate/matroska-spherical-mono-remux.matroska
+28d9277e1992fa362e7eac7a16261fde *tests/data/fate/matroska-spherical-mono-remux.matroska
+161569 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 165c2404e7..413961672f 100644
--- a/tests/ref/fate/matroska-vp8-alpha-remux
+++ b/tests/ref/fate/matroska-vp8-alpha-remux
@@ -1,5 +1,5 @@
-e3912762161bb6e7f9607c085b371676 *tests/data/fate/matroska-vp8-alpha-remux.matroska
-237185 tests/data/fate/matroska-vp8-alpha-remux.matroska
+89c4f6136f151f45c217ec363fe9db2b *tests/data/fate/matroska-vp8-alpha-remux.matroska
+237178 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/rgb24-mkv b/tests/ref/fate/rgb24-mkv
index 2630265ebc..a9461dfc1b 100644
--- a/tests/ref/fate/rgb24-mkv
+++ b/tests/ref/fate/rgb24-mkv
@@ -1,5 +1,5 @@
-2c7192d8b62c6a11e311a09ab3bef3b8 *tests/data/fate/rgb24-mkv.matroska
-58224 tests/data/fate/rgb24-mkv.matroska
+afc7b59819a8d69773b04757278fccef *tests/data/fate/rgb24-mkv.matroska
+58217 tests/data/fate/rgb24-mkv.matroska
 #tb 0: 1/10
 #media_type 0: video
 #codec_id 0: rawvideo
diff --git a/tests/ref/lavf-fate/av1.mkv b/tests/ref/lavf-fate/av1.mkv
index 454aef53fb..7d7acae135 100644
--- a/tests/ref/lavf-fate/av1.mkv
+++ b/tests/ref/lavf-fate/av1.mkv
@@ -1,3 +1,3 @@
-61e66b67c0c38532f6c6edc8c9d32e48 *tests/data/lavf-fate/lavf.av1.mkv
-55656 tests/data/lavf-fate/lavf.av1.mkv
+33e58aee4132d60e7ae21ecf3ae9e35f *tests/data/lavf-fate/lavf.av1.mkv
+55649 tests/data/lavf-fate/lavf.av1.mkv
 tests/data/lavf-fate/lavf.av1.mkv CRC=0x7c27cc15
diff --git a/tests/ref/lavf/mkv b/tests/ref/lavf/mkv
index e737cdf6ac..e089bff61c 100644
--- a/tests/ref/lavf/mkv
+++ b/tests/ref/lavf/mkv
@@ -1,3 +1,3 @@
-3e37e53471f20f2b7169afd75cba70d2 *tests/data/lavf/lavf.mkv
-320424 tests/data/lavf/lavf.mkv
+c5e6db66eb81415c2a5128f4298850a8 *tests/data/lavf/lavf.mkv
+320417 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 4a2f9eb7f1..18dc902ca7 100644
--- a/tests/ref/lavf/mkv_attachment
+++ b/tests/ref/lavf/mkv_attachment
@@ -1,3 +1,3 @@
-32b24b97956f1fbcfbc1a94aac8f6202 *tests/data/lavf/lavf.mkv_attachment
-472574 tests/data/lavf/lavf.mkv_attachment
+a24aaf26dde6461f89acb7dd7c492015 *tests/data/lavf/lavf.mkv_attachment
+472567 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 37945246ab..cbdbe9bfd1 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:    672 size:   208
+ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    665 size:   208
 ret: 0         st:-1 flags:0  ts:-1.000000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292304 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
 ret: 0         st: 0 flags:0  ts: 0.788000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292304 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
 ret: 0         st: 0 flags:1  ts:-0.317000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 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: 320145 size:   209
+ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320138 size:   209
 ret: 0         st:-1 flags:0  ts: 0.365002
-ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146856 size: 27925
+ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146849 size: 27925
 ret: 0         st:-1 flags:1  ts:-0.740831
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 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: 292304 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
 ret: 0         st: 1 flags:0  ts:-0.058000
-ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    672 size:   208
+ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    665 size:   208
 ret: 0         st: 1 flags:1  ts: 2.836000
-ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320145 size:   209
+ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320138 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: 146856 size: 27925
+ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146849 size: 27925
 ret: 0         st: 0 flags:0  ts:-0.482000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
 ret: 0         st: 0 flags:1  ts: 2.413000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292304 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 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:    672 size:   208
+ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:    665 size:   208
 ret: 0         st:-1 flags:0  ts:-0.904994
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
 ret: 0         st:-1 flags:1  ts: 1.989173
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292304 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
 ret: 0         st: 0 flags:0  ts: 0.883000
-ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292304 size: 27834
+ret: 0         st: 0 flags:1 dts: 0.971000 pts: 0.971000 pos: 292297 size: 27834
 ret: 0         st: 0 flags:1  ts:-0.222000
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 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: 320145 size:   209
+ret: 0         st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 320138 size:   209
 ret: 0         st:-1 flags:0  ts: 0.460008
-ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146856 size: 27925
+ret: 0         st: 0 flags:1 dts: 0.491000 pts: 0.491000 pos: 146849 size: 27925
 ret: 0         st:-1 flags:1  ts:-0.645825
-ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    888 size: 27837
+ret: 0         st: 0 flags:1 dts: 0.011000 pts: 0.011000 pos:    881 size: 27837
