Index: libavformat/avi.h
===================================================================
--- libavformat/avi.h	(revision 26402)
+++ libavformat/avi.h	(working copy)
@@ -32,6 +32,7 @@
 
 #define AVI_MAX_RIFF_SIZE       0x40000000LL
 #define AVI_MASTER_INDEX_SIZE   256
+#define AVI_MAX_STREAM_COUNT  99		//AVI format is able to assign stream ID for at most 99 streams
 
 /* index flags */
 #define AVIIF_INDEX             0x10
Index: libavformat/avienc.c
===================================================================
--- libavformat/avienc.c	(revision 26402)
+++ libavformat/avienc.c	(working copy)
@@ -85,8 +85,8 @@
 
 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
 {
-    tag[0] = '0';
-    tag[1] = '0' + index;
+    tag[0] = '0' + (index/10);
+    tag[1] = '0' + (index%10);
     if (type == AVMEDIA_TYPE_VIDEO) {
         tag[2] = 'd';
         tag[3] = 'c';
@@ -158,6 +158,12 @@
     int64_t list1, list2, strh, strf;
     AVMetadataTag *t = NULL;
 
+    /* check the total stream number */
+    if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
+        av_log(s, AV_LOG_ERROR, "Too many streams for avi format file. Please limit the number to be less than 100.\n");
+        return -1;
+    }
+
     for(n=0;n<s->nb_streams;n++) {
         s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
         if(!s->streams[n]->priv_data)
