diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index f119a1ab45b51fcf642de3ddafbc487631c37d46..db0ed3c890a8f939ed1b370679c41a2f2e6b4d9a 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -157,7 +157,7 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
     AVIStream *ast;
     int i;
     int64_t last_pos= -1;
-    int64_t filesize= avio_size(s->pb);
+    int64_t filesize= avi->fsize;
 
     av_dlog(s, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
             longs_pre_entry,index_type, entries_in_use, chunk_id, base);
@@ -372,7 +372,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
     av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
 
     avi->fsize = avio_size(pb);
-    if(avi->fsize<=0)
+    if(avi->fsize<=0 || avi->fsize < avi->riff_end)
         avi->fsize= avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
 
     /* first list tag */
@@ -398,7 +398,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
             if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
                 avi->movi_list = avio_tell(pb) - 4;
                 if(size) avi->movi_end = avi->movi_list + size + (size & 1);
-                else     avi->movi_end = avio_size(pb);
+                else     avi->movi_end = avi->fsize;
                 av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
                 goto end_of_header;
             }
@@ -731,7 +731,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
                                         "I will ignore it and try to continue anyway.\n");
                 if (s->error_recognition >= FF_ER_EXPLODE) goto fail;
                 avi->movi_list = avio_tell(pb) - 4;
-                avi->movi_end  = avio_size(pb);
+                avi->movi_end  = avi->fsize;
                 goto end_of_header;
             }
             /* skip tag */
diff --git a/tests/ref/fate/aasc b/tests/ref/fate/aasc
index 7ec02ba57542367050f1b95ff55b163da4f34a5b..07b326983d5ed66e86a07d5ca0987c3a1511017c 100644
--- a/tests/ref/fate/aasc
+++ b/tests/ref/fate/aasc
@@ -21,3 +21,4 @@
 0, 72000, 168000, 0x646fa087
 0, 75600, 168000, 0x404450a2
 0, 79200, 168000, 0x5214c456
+0, 82800, 168000, 0xe573025c
diff --git a/tests/ref/fate/cvid b/tests/ref/fate/cvid
index d7ebe1193d532bb4846305b0fef4495a89628b3d..9a85275c690ae4f57d6e77b2c6043c6f6f8062c4 100644
--- a/tests/ref/fate/cvid
+++ b/tests/ref/fate/cvid
@@ -76,3 +76,4 @@
 0, 562500, 112400, 0xe4394f1f
 0, 570000, 112400, 0x8ca8649f
 0, 577500, 112400, 0x804d44eb
+0, 585000, 112400, 0x3864488b
diff --git a/tests/ref/fate/fraps-v5 b/tests/ref/fate/fraps-v5
index 3fe1c49329b9e1bf8a0e4d1a2cbe38ddfddc9972..05fa9da37bcad102338a85ed85fa34f30233b47a 100644
--- a/tests/ref/fate/fraps-v5
+++ b/tests/ref/fate/fraps-v5
@@ -56,3 +56,4 @@
 0, 165000, 145152, 0x80448031
 0, 168000, 145152, 0xe3b1fbf7
 0, 171000, 145152, 0xa00395a4
+0, 174000, 145152, 0xdf3b4fce
diff --git a/tests/ref/fate/truemotion1-24 b/tests/ref/fate/truemotion1-24
index b0b1c4e44e9a3361961e58c6aca9c5c889cb2af5..edca66fe4384a834e96d9d2b64538c6ba0d1c2a7 100644
--- a/tests/ref/fate/truemotion1-24
+++ b/tests/ref/fate/truemotion1-24
@@ -30,6 +30,7 @@
 1, 82898, 10832, 0x3c746fbe
 0, 84000, 69120, 0x5876d758
 1, 88424, 10832, 0x05f3b08a
+0, 90000, 69120, 0x45e7dd5c
 1, 93951, 10832, 0xa6560483
 1, 99478, 10832, 0xd98a8e19
 1, 105004, 10832, 0xf98a0b2e
diff --git a/tests/ref/fate/vp5 b/tests/ref/fate/vp5
index bf67801a47d68bc6fb7c12edc6314d9ca9cbbaeb..556d1112d7b8976f8a3b2d670ee564524cb78372 100644
--- a/tests/ref/fate/vp5
+++ b/tests/ref/fate/vp5
@@ -244,3 +244,4 @@
 0, 912161, 233472, 0x6f530ac6
 0, 915915, 233472, 0x94f7466c
 0, 919669, 233472, 0xa8c1d365
+0, 923423, 233472, 0xedcff050
diff --git a/tests/ref/fate/zmbv-8bit b/tests/ref/fate/zmbv-8bit
index 258147f91400321200c6fe17f7abd225113ce191..75a212b3c93853d1b1b40a1bb4a1ded01eed9e89 100644
--- a/tests/ref/fate/zmbv-8bit
+++ b/tests/ref/fate/zmbv-8bit
@@ -273,3 +273,4 @@
 0, 349284, 192000, 0xd08e49d1
 0, 350568, 192000, 0xd08e49d1
 0, 351852, 192000, 0xd08e49d1
+0, 353136, 192000, 0x1f34135f