diff --git a/libavformat/riffenc.c b/libavformat/riffenc.c
index 195a58ea7f..33879ea1ab 100644
--- a/libavformat/riffenc.c
+++ b/libavformat/riffenc.c
@@ -219,11 +219,8 @@ void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
                pix_fmt == AV_PIX_FMT_MONOWHITE ||
                pix_fmt == AV_PIX_FMT_MONOBLACK);
 
-    if (!enc->extradata_size && pal_avi)
-        extradata_size = 4 * (1 << enc->bits_per_coded_sample);
-
-    /* size */
-    avio_wl32(pb, 40 + (ignore_extradata ? 0 :extradata_size));
+    /* Size (not including the size of the color table or color masks) */
+    avio_wl32(pb, 40 + (ignore_extradata || pal_avi ? 0 : extradata_size));
     avio_wl32(pb, enc->width);
     //We always store RGB TopDown
     avio_wl32(pb, enc->codec_tag || keep_height ? enc->height : -enc->height);
@@ -236,7 +233,10 @@ void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
     avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
     avio_wl32(pb, 0);
     avio_wl32(pb, 0);
-    avio_wl32(pb, 0);
+    /* Number of color indices in the color table that are used.
+     * A value of 0 means 2^biBitCount indices, but this doesn't work
+     * with Windows Media Player and files containing xxpc chunks. */
+    avio_wl32(pb, pal_avi ? 1 << enc->bits_per_coded_sample : 0);
     avio_wl32(pb, 0);
 
     if (!ignore_extradata) {
diff --git a/tests/ref/lavf-fate/avi_cram b/tests/ref/lavf-fate/avi_cram
index 7864ab92a7..7b4e69cddf 100644
--- a/tests/ref/lavf-fate/avi_cram
+++ b/tests/ref/lavf-fate/avi_cram
@@ -1,3 +1,3 @@
-e202447ccd6660149c17070204d258a4 *./tests/data/lavf-fate/lavf.avi
+ba77c5c8bd2b0d1e0478d143346cc3b3 *./tests/data/lavf-fate/lavf.avi
 928228 ./tests/data/lavf-fate/lavf.avi
 ./tests/data/lavf-fate/lavf.avi CRC=0xa4770de2
diff --git a/tests/ref/vsynth/vsynth1-bpp1 b/tests/ref/vsynth/vsynth1-bpp1
index 92d5987797..af1fb0ecd4 100644
--- a/tests/ref/vsynth/vsynth1-bpp1
+++ b/tests/ref/vsynth/vsynth1-bpp1
@@ -1,4 +1,4 @@
-0fcba876d3e499c0bebf7bb32a7f83f2 *tests/data/fate/vsynth1-bpp1.avi
+a0b35707a9aa7144e3e1c70c1d01f4ce *tests/data/fate/vsynth1-bpp1.avi
 640460 tests/data/fate/vsynth1-bpp1.avi
 cd1e1448d9895561347ceb66d0add34d *tests/data/fate/vsynth1-bpp1.out.rawvideo
 stddev:   84.48 PSNR:  9.60 MAXDIFF:  218 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth2-bpp1 b/tests/ref/vsynth/vsynth2-bpp1
index 2a2a63e299..4005755662 100644
--- a/tests/ref/vsynth/vsynth2-bpp1
+++ b/tests/ref/vsynth/vsynth2-bpp1
@@ -1,4 +1,4 @@
-af7eae5293b820493d1f4e6d258d8da0 *tests/data/fate/vsynth2-bpp1.avi
+a0330430d7dbd76cbd6d099b778397e8 *tests/data/fate/vsynth2-bpp1.avi
 640460 tests/data/fate/vsynth2-bpp1.avi
 f0dfc0e87e5d96bce29a5944b1bd7471 *tests/data/fate/vsynth2-bpp1.out.rawvideo
 stddev:   68.98 PSNR: 11.36 MAXDIFF:  218 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth3-bpp1 b/tests/ref/vsynth/vsynth3-bpp1
index b3b757c79c..3c8c47ac86 100644
--- a/tests/ref/vsynth/vsynth3-bpp1
+++ b/tests/ref/vsynth/vsynth3-bpp1
@@ -1,4 +1,4 @@
-01b0fbf35305b50b1c148c0a23f2cff4 *tests/data/fate/vsynth3-bpp1.avi
+4c8777a88a9e52b99d5a345acffcbf06 *tests/data/fate/vsynth3-bpp1.avi
 20460 tests/data/fate/vsynth3-bpp1.avi
 52ae74ef7910e5b603c12288d425b9ae *tests/data/fate/vsynth3-bpp1.out.rawvideo
 stddev:   84.76 PSNR:  9.57 MAXDIFF:  232 bytes:    86700/    86700
diff --git a/tests/ref/vsynth/vsynth_lena-bpp1 b/tests/ref/vsynth/vsynth_lena-bpp1
index 57219cc73c..3489989268 100644
--- a/tests/ref/vsynth/vsynth_lena-bpp1
+++ b/tests/ref/vsynth/vsynth_lena-bpp1
@@ -1,4 +1,4 @@
-930d019cbf8e06c67bdec58a3639c045 *tests/data/fate/vsynth_lena-bpp1.avi
+32673399a8442e397a608839eb3e95cb *tests/data/fate/vsynth_lena-bpp1.avi
 640460 tests/data/fate/vsynth_lena-bpp1.avi
 6183ba861d4e48d4aaefc514fde270e5 *tests/data/fate/vsynth_lena-bpp1.out.rawvideo
 stddev:   83.28 PSNR:  9.72 MAXDIFF:  215 bytes:  7603200/  7603200