diff --git a/doc/muxers.texi b/doc/muxers.texi
index 1ea98a69a3..c49ae3a17b 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1567,6 +1567,15 @@ A safe size for most use cases should be about 50kB per hour of video.
 
 Note that cues are only written if the output is seekable and this option will
 have no effect if it is not.
+
+@item cues_to_front
+If set, the muxer will write the index at the beginning of the file
+by shifting the main data if necessary. This can be combined with
+reserve_index_space in which case the data is only shifted if
+the initially reserved space turns out to be insufficient.
+
+This option is ignored if the output is unseekable.
+
 @item default_mode
 This option controls how the FlagDefault of the output tracks will be set.
 It influences which tracks players should play by default. The default mode
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 152312102a..41b2df7dbf 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -161,6 +161,7 @@ typedef struct MatroskaMuxContext {
     int                 allow_raw_vfw;
     int                 flipped_raw_rgb;
     int                 default_mode;
+    int                 move_cues_to_front;
 
     uint32_t            segment_uid[4];
 } MatroskaMuxContext;
@@ -566,7 +567,8 @@ static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts,
 }
 
 static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp,
-                             mkv_cues *cues, mkv_track *tracks, int num_tracks)
+                             const mkv_cues *cues, mkv_track *tracks, int num_tracks,
+                             uint64_t offset)
 {
     AVIOContext *cuepoint;
     int ret;
@@ -597,7 +599,7 @@ static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp,
             tracks[idx].has_cue = 1;
             track_positions = start_ebml_master(cuepoint, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE);
             put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK           , tracks[idx].track_num);
-            put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos);
+            put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos + offset);
             put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos);
             if (entry->duration > 0)
                 put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION    , entry->duration);
@@ -1984,12 +1986,14 @@ static int mkv_write_header(AVFormatContext *s)
         put_ebml_void(pb, s->metadata_header_padding);
     }
 
-    if (mkv->reserve_cues_space) {
+    if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
         if (IS_SEEKABLE(pb, mkv)) {
             mkv->cues_pos = avio_tell(pb);
-            if (mkv->reserve_cues_space == 1)
-                mkv->reserve_cues_space++;
-            put_ebml_void(pb, mkv->reserve_cues_space);
+            if (mkv->reserve_cues_space >= 1) {
+                if (mkv->reserve_cues_space == 1)
+                    mkv->reserve_cues_space++;
+                put_ebml_void(pb, mkv->reserve_cues_space);
+            }
         } else
             mkv->reserve_cues_space = -1;
     }
@@ -2575,25 +2579,31 @@ static int mkv_write_trailer(AVFormatContext *s)
 
     if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) {
         AVIOContext *cues = NULL;
-        uint64_t size;
+        uint64_t size, offset = 0;
         int length_size = 0;
 
+redo_cues:
         ret = start_ebml_master_crc32(&cues, mkv);
         if (ret < 0)
             return ret;
 
         ret = mkv_assemble_cues(s->streams, cues, &mkv->cues,
-                                mkv->tracks, s->nb_streams);
+                                mkv->tracks, s->nb_streams, offset);
         if (ret < 0) {
             ffio_free_dyn_buf(&cues);
             return ret;
         }
 
-        if (mkv->reserve_cues_space) {
+        if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
             size  = avio_tell(cues);
             length_size = ebml_length_size(size);
             size += 4 + length_size;
-            if (mkv->reserve_cues_space < size) {
+            if (offset + mkv->reserve_cues_space < size) {
+                if (mkv->move_cues_to_front) {
+                    offset = size - mkv->reserve_cues_space;
+                    ffio_reset_dyn_buf(cues);
+                    goto redo_cues;
+                }
                 av_log(s, AV_LOG_WARNING,
                        "Insufficient space reserved for Cues: "
                        "%d < %"PRIu64". No Cues will be output.\n",
@@ -2601,6 +2611,15 @@ static int mkv_write_trailer(AVFormatContext *s)
                 ret2 = AVERROR(EINVAL);
                 goto after_cues;
             } else {
+                if (offset) {
+                    ret = ff_format_shift_data(s, mkv->cues_pos + mkv->reserve_cues_space,
+                                               offset);
+                    if (ret < 0) {
+                        ffio_free_dyn_buf(&cues);
+                        return ret;
+                    }
+                    endpos += offset;
+                }
                 if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
                     ffio_free_dyn_buf(&cues);
                     return ret64;
@@ -2623,7 +2642,7 @@ static int mkv_write_trailer(AVFormatContext *s)
         if (mkv->reserve_cues_space) {
             if (size < mkv->reserve_cues_space)
                 put_ebml_void(pb, mkv->reserve_cues_space - size);
-        } else
+        } else if (!mkv->move_cues_to_front)
             endpos = avio_tell(pb);
     }
 
@@ -2848,6 +2867,7 @@ static const AVCodecTag additional_subtitle_tags[] = {
 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "reserve_index_space", "Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).", OFFSET(reserve_cues_space), AV_OPT_TYPE_INT,   { .i64 = 0 },   0, INT_MAX,   FLAGS },
+    { "cues_to_front", "Move Cues (the index) to the front by shifting data if necessary", OFFSET(move_cues_to_front), AV_OPT_TYPE_BOOL, { .i64 = 0}, 0, 1, FLAGS },
     { "cluster_size_limit",  "Store at most the provided amount of bytes in a cluster. ",                                     OFFSET(cluster_size_limit), AV_OPT_TYPE_INT  , { .i64 = -1 }, -1, INT_MAX,   FLAGS },
     { "cluster_time_limit",  "Store at most the provided number of milliseconds in a cluster.",                               OFFSET(cluster_time_limit), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
     { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak
index e117a0f6a6..fca84ecf12 100644
--- a/tests/fate/matroska.mak
+++ b/tests/fate/matroska.mak
@@ -67,6 +67,38 @@ FATE_MATROSKA_FFMPEG_FFPROBE-$(call DEMMUX, MATROSKA, MATROSKA) \
                                += fate-matroska-zero-length-block
 fate-matroska-zero-length-block: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/zero_length_block.mks matroska "-c:s copy -dash 1 -dash_track_number 2000000000 -reserve_index_space 62 -metadata_header_padding 1 -default_mode infer_no_subs" "-c:s copy" "" "-show_entries stream_tags=description"
 
+# This mainly tests the Matroska muxer's ability to shift the data
+# to create enough free space to write the Cues at the front.
+# The metadata_header_padding has been chosen so that three attempts
+# to write the Cues are necessary.
+# It also tests writing PCM audio in both endiannesses and putting
+# Cues with the same timestamp in the same CuePoint as well as
+# omitting CRC-32 elements when writing Matroska.
+FATE_MATROSKA-$(call ALLYES, FILE_PROTOCOL WAV_DEMUXER PCM_S24LE_DECODER    \
+                             PCM_S24BE_ENCODER MATROSKA_MUXER               \
+                             MATROSKA_DEMUXER FRAMECRC_MUXER PIPE_PROTOCOL) \
+                += fate-matroska-move-cues-to-front
+fate-matroska-move-cues-to-front: CMD = transcode wav $(TARGET_SAMPLES)/audio-reference/divertimenti_2ch_96kHz_s24.wav matroska "-map 0 -map 0 -c:a:0 pcm_s24be -c:a:1 copy -cluster_time_limit 5 -cues_to_front yes -metadata_header_padding 7840 -write_crc32 0" "-map 0 -c copy -t 0.1"
+
+# This tests DOVI (reading from MP4 and Matroska and writing to Matroska)
+# as well as writing the Cues at the front (by shifting data) if
+# the initially reserved amount of space turns out to be insufficient.
+FATE_MATROSKA_FFMPEG_FFPROBE-$(call ALLYES, FILE_PROTOCOL MOV_DEMUXER       \
+                                            HEVC_DECODER MATROSKA_MUXER     \
+                                            MATROSKA_DEMUXER FRAMECRC_MUXER \
+                                            PIPE_PROTOCOL)                  \
+                               += fate-matroska-dovi-write-config7
+fate-matroska-dovi-write-config7: CMD = transcode mov $(TARGET_SAMPLES)/mov/dovi-p7.mp4 matroska "-map 0 -c copy -cues_to_front yes -reserve_index_space 40  -metadata_header_padding 64339" "-map 0 -c copy" "" "-show_entries stream_side_data_list"
+
+# This tests writing the MS-compatibility modes V_MS/VFW/FOURCC and A_MS/ACM.
+# It furthermore tests writing the Cues at the front if the cues_to_front
+# option is set and more than enough space has been reserved in advance.
+# (Btw: The keyframe flags of the input video stream seem wrong.)
+FATE_MATROSKA-$(call ALLYES, FILE_PROTOCOL AVI_DEMUXER MATROSKA_MUXER \
+                             MATROSKA_DEMUXER FRAMECRC_MUXER          \
+                             PIPE_PROTOCOL) += fate-matroska-ms-mode
+fate-matroska-ms-mode: CMD = transcode avi $(TARGET_SAMPLES)/vp5/potter512-400-partial.avi matroska "-map 0 -c copy -cues_to_front yes -reserve_index_space 5000" "-map 0 -c copy -t 1"
+
 # This test the following features of the Matroska muxer: Writing projection
 # stream side-data; not setting any track to default if the user requested it;
 # and modifying and writing colorspace properties.
diff --git a/tests/ref/fate/matroska-dovi-write-config7 b/tests/ref/fate/matroska-dovi-write-config7
new file mode 100644
index 0000000000..1c1422c0e4
--- /dev/null
+++ b/tests/ref/fate/matroska-dovi-write-config7
@@ -0,0 +1,49 @@
+3fa1f47c5c3d22b5c33156ff14928d6c *tests/data/fate/matroska-dovi-write-config7.matroska
+72758 tests/data/fate/matroska-dovi-write-config7.matroska
+#extradata 0:      116, 0x2b8d1669
+#extradata 1:      116, 0x2b8d1669
+#tb 0: 1/1000
+#media_type 0: video
+#codec_id 0: hevc
+#dimensions 0: 1920x1080
+#sar 0: 0/1
+#tb 1: 1/1000
+#media_type 1: video
+#codec_id 1: hevc
+#dimensions 1: 1920x1080
+#sar 1: 0/1
+0,        -83,          0,       41,      699, 0x728548f1
+1,        -83,          0,       41,     1085, 0xfb2dba82, S=1,        8
+0,        -42,        167,       41,       95, 0xc0312044, F=0x0
+1,        -42,        167,       41,      481, 0xf23f91d5, F=0x0
+0,          0,         83,       41,       99, 0x5e0a2221, F=0x0
+1,          0,         83,       41,      485, 0x5f7b93b2, F=0x0
+0,         42,         42,       41,       99, 0xe60e208b, F=0x0
+1,         42,         42,       41,      485, 0x8335921c, F=0x0
+0,         83,        125,       41,       99, 0xa1e422e1, F=0x0
+1,         83,        125,       41,      485, 0xc4e49472, F=0x0
+0,        125,        333,       41,       96, 0xdc762089, F=0x0
+1,        125,        333,       41,      482, 0x769c921a, F=0x0
+0,        167,        250,       41,      100, 0x89cd22a0, F=0x0
+1,        167,        250,       41,      486, 0x4aca9431, F=0x0
+0,        208,        208,       41,      100, 0x6d4521ff, F=0x0
+1,        208,        208,       41,      486, 0x3b719390, F=0x0
+0,        250,        292,       41,       99, 0x92ab22c0, F=0x0
+1,        250,        292,       41,      485, 0x83e99451, F=0x0
+0,        292,        292,       41,       95, 0xcd9020bd, F=0x0
+1,        292,        292,       41,      481, 0x44ec924e, F=0x0
+[STREAM]
+[/STREAM]
+[STREAM]
+[SIDE_DATA]
+side_data_type=DOVI configuration record
+dv_version_major=1
+dv_version_minor=0
+dv_profile=7
+dv_level=4
+rpu_present_flag=1
+el_present_flag=1
+bl_present_flag=0
+dv_bl_signal_compatibility_id=6
+[/SIDE_DATA]
+[/STREAM]
diff --git a/tests/ref/fate/matroska-move-cues-to-front b/tests/ref/fate/matroska-move-cues-to-front
new file mode 100644
index 0000000000..46effff53e
--- /dev/null
+++ b/tests/ref/fate/matroska-move-cues-to-front
@@ -0,0 +1,72 @@
+ce15d8b7577933a057c413af505500df *tests/data/fate/matroska-move-cues-to-front.matroska
+23210310 tests/data/fate/matroska-move-cues-to-front.matroska
+#tb 0: 1/1000
+#media_type 0: audio
+#codec_id 0: pcm_s24be
+#sample_rate 0: 192000
+#channel_layout 0: 3
+#channel_layout_name 0: stereo
+#tb 1: 1/1000
+#media_type 1: audio
+#codec_id 1: pcm_s24le
+#sample_rate 1: 192000
+#channel_layout 1: 3
+#channel_layout_name 1: stereo
+0,          0,          0,        3,     4092, 0x71f10ea0
+1,          0,          0,        3,     4092, 0xa6320ea0
+0,          4,          4,        3,     4092, 0x51852317
+1,          4,          4,        3,     4092, 0x27732317
+0,          7,          7,        3,     4092, 0xc8e2693d
+1,          7,          7,        3,     4092, 0x5899693d
+0,         11,         11,        3,     4092, 0x8df13008
+1,         11,         11,        3,     4092, 0x6fa63008
+0,         14,         14,        3,     4092, 0xc56bdf7f
+1,         14,         14,        3,     4092, 0x22b0df7f
+0,         18,         18,        3,     4092, 0x4ac2c0f9
+1,         18,         18,        3,     4092, 0x5512c0f9
+0,         21,         21,        3,     4092, 0x11a50650
+1,         21,         21,        3,     4092, 0x11b90650
+0,         25,         25,        3,     4092, 0x0a3837f4
+1,         25,         25,        3,     4092, 0x9cb537f4
+0,         28,         28,        3,     4092, 0xff0a3ce7
+1,         28,         28,        3,     4092, 0x7d1a3ce7
+0,         32,         32,        3,     4092, 0x42d2c983
+1,         32,         32,        3,     4092, 0x0f56c983
+0,         36,         36,        3,     4092, 0x2adbf4ea
+1,         36,         36,        3,     4092, 0x386bf4ea
+0,         39,         39,        3,     4092, 0x86d4f0a5
+1,         39,         39,        3,     4092, 0x5924f0a5
+0,         43,         43,        3,     4092, 0x5f35d5f7
+1,         43,         43,        3,     4092, 0x565fd5f7
+0,         46,         46,        3,     4092, 0xd3f27234
+1,         46,         46,        3,     4092, 0x4d197234
+0,         50,         50,        3,     4092, 0xb3a97ff5
+1,         50,         50,        3,     4092, 0x61e67ff5
+0,         53,         53,        3,     4092, 0xce30e2ba
+1,         53,         53,        3,     4092, 0xe65de2ba
+0,         57,         57,        3,     4092, 0x3d482d44
+1,         57,         57,        3,     4092, 0xf85b2d44
+0,         60,         60,        3,     4092, 0x691d161c
+1,         60,         60,        3,     4092, 0x3b01161c
+0,         64,         64,        3,     4092, 0xe6b93525
+1,         64,         64,        3,     4092, 0xdd4e3525
+0,         67,         67,        3,     4092, 0x9ce3f785
+1,         67,         67,        3,     4092, 0x8a28f785
+0,         71,         71,        3,     4092, 0x688fc452
+1,         71,         71,        3,     4092, 0x8c5ec452
+0,         75,         75,        3,     4092, 0x400cf87e
+1,         75,         75,        3,     4092, 0x1e64f87e
+0,         78,         78,        3,     4092, 0x49baa923
+1,         78,         78,        3,     4092, 0x68d9a923
+0,         82,         82,        3,     4092, 0x4df27658
+1,         82,         82,        3,     4092, 0x38d77658
+0,         85,         85,        3,     4092, 0xdfebf0e7
+1,         85,         85,        3,     4092, 0xab2cf0e7
+0,         89,         89,        3,     4092, 0x69d2f76c
+1,         89,         89,        3,     4092, 0x35b9f76c
+0,         92,         92,        3,     4092, 0x877b89d3
+1,         92,         92,        3,     4092, 0xcc4889d3
+0,         96,         96,        3,     4092, 0x70035443
+1,         96,         96,        3,     4092, 0x04825443
+0,         99,         99,        3,     4092, 0x30135036
+1,         99,         99,        3,     4092, 0x4fba5036
diff --git a/tests/ref/fate/matroska-ms-mode b/tests/ref/fate/matroska-ms-mode
new file mode 100644
index 0000000000..5fe052c39b
--- /dev/null
+++ b/tests/ref/fate/matroska-ms-mode
@@ -0,0 +1,89 @@
+b3d928e92bc8b323793a237ce82f9437 *tests/data/fate/matroska-ms-mode.matroska
+413108 tests/data/fate/matroska-ms-mode.matroska
+#extradata 0:       40, 0x54290c93
+#extradata 1:      114, 0xb6c80771
+#tb 0: 1/1000
+#media_type 0: video
+#codec_id 0: vp5
+#dimensions 0: 512x304
+#sar 0: 0/1
+#tb 1: 1/1000
+#media_type 1: audio
+#codec_id 1: speex
+#sample_rate 1: 32000
+#channel_layout 1: 4
+#channel_layout_name 1: mono
+0,          0,          0,       41,    12972, 0x6588cf8e
+1,          0,          0,        0,       74, 0xd4eb274d
+1,         20,         20,        0,       74, 0xef822181
+1,         40,         40,        0,       74, 0x61e3239c
+0,         42,         42,       41,      478, 0xeca1eeb9
+1,         60,         60,        0,       74, 0x474623d5
+1,         80,         80,        0,       74, 0x79a21f22
+0,         83,         83,       41,      260, 0x335f8133
+1,        100,        100,        0,       74, 0xb3022058
+1,        120,        120,        0,       74, 0x57a32240
+0,        125,        125,       41,      199, 0xf6f86142
+1,        140,        140,        0,       74, 0x34892453
+1,        160,        160,        0,       74, 0x55621efb
+0,        167,        167,       41,      188, 0x0eeb5f55
+1,        180,        180,        0,       74, 0xb92f206a
+1,        200,        200,        0,       74, 0x1988222e
+0,        209,        209,       41,      183, 0x921a5b3c
+1,        220,        220,        0,       74, 0x033b20dc
+1,        240,        240,        0,       74, 0xf8f41da4
+0,        250,        250,       41,      181, 0xae765703
+1,        260,        260,        0,       74, 0xfc89201f
+1,        280,        280,        0,       74, 0x2b102428
+0,        292,        292,       41,      181, 0xdc975d93
+1,        300,        300,        0,       74, 0x2df42380
+1,        320,        320,        0,       74, 0xebcf20fd
+0,        334,        334,       41,      181, 0x30355b73
+1,        340,        340,        0,       74, 0x3eb524f8
+1,        360,        360,        0,       74, 0x1f802308
+0,        375,        375,       41,      179, 0xef275e89
+1,        380,        380,        0,       74, 0x218d23bd
+1,        400,        400,        0,       74, 0x77f82421
+0,        417,        417,       41,      181, 0xbdb35a1b
+1,        420,        420,        0,       74, 0xf20023a3
+1,        440,        440,        0,       74, 0x82cc1f9a
+0,        459,        459,       41,      179, 0x1b245f55
+1,        460,        460,        0,       74, 0x8d3222e4
+1,        480,        480,        0,       74, 0x939d1e4c
+1,        500,        500,        0,       74, 0x55c3232c
+0,        501,        501,       41,      181, 0x30355b73
+1,        520,        520,        0,       74, 0x85e02092
+1,        540,        540,        0,       74, 0xb9d02059
+0,        542,        542,       41,      179, 0xef275e89
+1,        560,        560,        0,       74, 0xbbd8211f
+1,        580,        580,        0,       74, 0xe0ca20e1
+0,        584,        584,       41,      181, 0xbdb35a1b
+1,        600,        600,        0,       74, 0xd4f9216b
+1,        620,        620,        0,       74, 0xdea723f9
+0,        626,        626,       41,      179, 0x1b245f55
+1,        640,        640,        0,       74, 0xc2611fe9
+1,        660,        660,        0,       74, 0x9f941f2d
+0,        667,        667,       41,      181, 0x30355b73
+1,        680,        680,        0,       74, 0xaf991eb9
+1,        700,        700,        0,       74, 0x7e79250e
+0,        709,        709,       41,      179, 0xef275e89
+1,        720,        720,        0,       74, 0x5a421faa
+1,        740,        740,        0,       74, 0x3b211ce0
+0,        751,        751,       41,      181, 0xbdb35a1b
+1,        760,        760,        0,       74, 0x4a812478
+1,        780,        780,        0,       74, 0xfc1b234f
+0,        792,        792,       41,      179, 0x1b245f55
+1,        800,        800,        0,       74, 0x3d561db1
+1,        820,        820,        0,       74, 0x6bbb2475
+0,        834,        834,       41,      181, 0x30355b73
+1,        840,        840,        0,       74, 0x76fe1f63
+1,        860,        860,        0,       74, 0x15861cf1
+0,        876,        876,       41,      179, 0xef275e89
+1,        880,        880,        0,       74, 0x7dca1c6a
+1,        900,        900,        0,       74, 0xad8b20aa
+0,        918,        918,       41,      181, 0xbdb35a1b
+1,        920,        920,        0,       74, 0x6ba01e89
+1,        940,        940,        0,       74, 0x621421eb
+0,        959,        959,       41,      179, 0x1b245f55
+1,        960,        960,        0,       74, 0x26672424
+1,        980,        980,        0,       74, 0xcb6120f4
