Index: libavformat/avidec.c
===================================================================
--- libavformat/avidec.c	(revision 19327)
+++ libavformat/avidec.c	(working copy)
@@ -60,6 +60,7 @@
     int is_odml;
     int non_interleaved;
     int stream_index;
+    int nb_avi_streams;           ///< same as nb_streams, unless dv, then 1
     DVDemuxContext* dv_demux;
 } AVIContext;
 
@@ -132,7 +133,7 @@
         longs_pre_entry,index_type, entries_in_use, chunk_id, base);
 #endif
 
-    if(stream_id > s->nb_streams || stream_id < 0)
+    if(stream_id > avi->nb_avi_streams || stream_id < 0)
         return -1;
     st= s->streams[stream_id];
     ast = st->priv_data;
@@ -202,10 +203,11 @@
 }
 
 static void clean_index(AVFormatContext *s){
+    AVIContext *avi = s->priv_data;
     int i;
     int64_t j;
 
-    for(i=0; i<s->nb_streams; i++){
+    for(i=0; i<avi->nb_avi_streams; i++){
         AVStream *st = s->streams[i];
         AVIStream *ast = st->priv_data;
         int n= st->nb_index_entries;
@@ -320,7 +322,7 @@
             tag1 = get_le32(pb);
             handler = get_le32(pb); /* codec tag */
 
-            if(tag1 == MKTAG('p', 'a', 'd', 's')){
+            if(avi->dv_demux || tag1 == MKTAG('p', 'a', 'd', 's')){
                 url_fskip(pb, size - 8);
                 break;
             }else{
@@ -333,6 +335,7 @@
                 if (!ast)
                     goto fail;
                 st->priv_data = ast;
+                avi->nb_avi_streams++;
             }
             if(amv_file_format)
                 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
@@ -364,6 +367,7 @@
                     if (!avi->dv_demux)
                         goto fail;
                 }
+                avi->nb_avi_streams = 1;
                 s->streams[0]->priv_data = ast;
                 url_fskip(pb, 3 * 4);
                 ast->scale = get_le32(pb);
@@ -661,14 +665,14 @@
             return size;
     }
 
-    if(avi->non_interleaved){
+    if(avi->nb_avi_streams > 1 && avi->non_interleaved){
         int best_stream_index = 0;
         AVStream *best_st= NULL;
         AVIStream *best_ast;
         int64_t best_ts= INT64_MAX;
         int i;
 
-        for(i=0; i<s->nb_streams; i++){
+        for(i=0; i<avi->nb_avi_streams; i++){
             AVStream *st = s->streams[i];
             AVIStream *ast = st->priv_data;
             int64_t ts= ast->frame_offset;
@@ -805,7 +809,7 @@
             continue;
 
         //parse ix##
-        if(  (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
+        if(  (d[0] == 'i' && d[1] == 'x' && n < avi->nb_avi_streams)
         //parse JUNK
            ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
            ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
@@ -816,17 +820,17 @@
 
         n= get_stream_idx(d);
 
-        if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
+        if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < avi->nb_avi_streams)
             continue;
 
         //parse ##dc/##wb
-        if(n < s->nb_streams){
+        if(n < avi->nb_avi_streams){
             AVStream *st;
             AVIStream *ast;
             st = s->streams[n];
             ast = st->priv_data;
 
-            if(s->nb_streams>=2){
+            if(avi->nb_avi_streams>=2){
                 AVStream *st1  = s->streams[1];
                 AVIStream *ast1= st1->priv_data;
                 //workaround for broken small-file-bug402.avi
@@ -927,7 +931,7 @@
 
         index = ((tag & 0xff) - '0') * 10;
         index += ((tag >> 8) & 0xff) - '0';
-        if (index >= s->nb_streams)
+        if (index >= avi->nb_avi_streams)
             continue;
         st = s->streams[index];
         ast = st->priv_data;
@@ -952,12 +956,13 @@
 }
 
 static int guess_ni_flag(AVFormatContext *s){
+    AVIContext *avi = s->priv_data;
     int i;
     int64_t last_start=0;
     int64_t first_end= INT64_MAX;
     int64_t oldpos= url_ftell(s->pb);
 
-    for(i=0; i<s->nb_streams; i++){
+    for(i=0; i<avi->nb_avi_streams; i++){
         AVStream *st = s->streams[i];
         int n= st->nb_index_entries;
         unsigned int size;
@@ -1065,7 +1070,7 @@
         return 0;
     }
 
-    for(i = 0; i < s->nb_streams; i++) {
+    for(i = 0; i < avi->nb_avi_streams; i++) {
         AVStream *st2 = s->streams[i];
         AVIStream *ast2 = st2->priv_data;
 
