]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/avidec.c
10l CODEC vs. FORMAT
[ffmpeg] / libavformat / avidec.c
index b3c96211ca3d33988c6c96e0c13fcd118be6592e..21544c7731b2fe25d437803fa7e09dbeb2915809 100644 (file)
@@ -2,23 +2,26 @@
  * AVI decoder.
  * Copyright (c) 2001 Fabrice Bellard.
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
 #include "avi.h"
 #include "dv.h"
+#include "riff.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -35,7 +38,6 @@ typedef struct AVIStream {
     int scale;
     int rate;
     int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */
-    int start;
 
     int64_t cum_len; /* temporary storage (used during seek) */
 
@@ -100,9 +102,12 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
     AVIStream *ast;
     int i;
     int64_t last_pos= -1;
+    int64_t filesize= url_fsize(&s->pb);
 
-//    av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%Ld\n",
-//        longs_pre_entry,index_type, entries_in_use, chunk_id, base);
+#ifdef DEBUG_SEEK
+    av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16LX\n",
+        longs_pre_entry,index_type, entries_in_use, chunk_id, base);
+#endif
 
     if(stream_id > s->nb_streams || stream_id < 0)
         return -1;
@@ -119,6 +124,14 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
     if(index_type>1)
         return -1;
 
+    if(filesize > 0 && base >= filesize){
+        av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
+        if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
+            base &= 0xFFFFFFFF;
+        else
+            return -1;
+    }
+
     for(i=0; i<entries_in_use; i++){
         if(index_type){
             int64_t pos= get_le32(pb) + base - 8;
@@ -126,7 +139,9 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
             int key= len >= 0;
             len &= 0x7FFFFFFF;
 
-//av_log(s, AV_LOG_ERROR, "pos:%Ld, len:%X\n", pos, len);
+#ifdef DEBUG_SEEK
+            av_log(s, AV_LOG_ERROR, "pos:%Ld, len:%X\n", pos, len);
+#endif
             if(last_pos == pos || pos == base - 8)
                 avi->non_interleaved= 1;
             else
@@ -138,10 +153,12 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
                 ast->cum_len ++;
             last_pos= pos;
         }else{
-            int64_t offset= get_le64(pb);
-            int size      = get_le32(pb);
-            int duration  = get_le32(pb);
-            int64_t pos= url_ftell(pb);
+            int64_t offset, pos;
+            int duration;
+            offset = get_le64(pb);
+            get_le32(pb);       /* size */
+            duration = get_le32(pb);
+            pos = url_ftell(pb);
 
             url_fseek(pb, offset+8, SEEK_SET);
             read_braindead_odml_indx(s, frame_num);
@@ -150,11 +167,13 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
             url_fseek(pb, pos, SEEK_SET);
         }
     }
+    avi->index_loaded=1;
     return 0;
 }
 
 static void clean_index(AVFormatContext *s){
-    int i, j;
+    int i;
+    int64_t j;
 
     for(i=0; i<s->nb_streams; i++){
         AVStream *st = s->streams[i];
@@ -178,6 +197,15 @@ static void clean_index(AVFormatContext *s){
     }
 }
 
+static int avi_read_tag(ByteIOContext *pb, char *buf, int maxlen,  unsigned int size)
+{
+    offset_t i = url_ftell(pb);
+    size += (size & 1);
+    get_strz(pb, buf, maxlen);
+    url_fseek(pb, i+size, SEEK_SET);
+    return 0;
+}
+
 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
 {
     AVIContext *avi = s->priv_data;
@@ -187,8 +215,9 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
     unsigned int size, nb_frames;
     int i, n;
     AVStream *st;
-    AVIStream *ast;
+    AVIStream *ast = NULL;
     int xan_video = 0;  /* hack to support Xan A/V */
+    char str_track[4];
 
     avi->stream_index= -1;
 
@@ -217,7 +246,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
 #endif
             if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
                 avi->movi_list = url_ftell(pb) - 4;
-                if(size) avi->movi_end = avi->movi_list + size;
+                if(size) avi->movi_end = avi->movi_list + size + (size & 1);
                 else     avi->movi_end = url_fsize(pb);
 #ifdef DEBUG
                 printf("movi end=%Lx\n", avi->movi_end);
@@ -291,6 +320,9 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
             if (stream_index >= s->nb_streams) {
                 url_fskip(pb, size - 8);
+                /* ignore padding stream */
+                if (tag1 == MKTAG('p', 'a', 'd', 's'))
+                    stream_index--;
                 break;
             }
             st = s->streams[stream_index];
@@ -313,7 +345,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
             }
             av_set_pts_info(st, 64, ast->scale, ast->rate);
 
-            ast->start= get_le32(pb); /* start */
+            ast->cum_len=get_le32(pb); /* start */
             nb_frames = get_le32(pb);
 
             st->start_time = 0;
@@ -344,6 +376,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
                 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
                 goto fail;
             }
+            ast->frame_offset= ast->cum_len * FFMAX(ast->sample_size, 1);
             url_fskip(pb, size - 12 * 4);
             break;
         case MKTAG('s', 't', 'r', 'f'):
@@ -403,6 +436,8 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
                     break;
                 case CODEC_TYPE_AUDIO:
                     get_wav_header(pb, st->codec, size);
+                    if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
+                        av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
                     if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
                         url_fskip(pb, 1);
                     /* special case time: To support Xan DPCM, hardcode
@@ -424,10 +459,33 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
             break;
         case MKTAG('i', 'n', 'd', 'x'):
             i= url_ftell(pb);
-            read_braindead_odml_indx(s, 0);
-            avi->index_loaded=1;
+            if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
+                read_braindead_odml_indx(s, 0);
+            }
             url_fseek(pb, i+size, SEEK_SET);
             break;
+        case MKTAG('I', 'N', 'A', 'M'):
+            avi_read_tag(pb, s->title, sizeof(s->title), size);
+            break;
+        case MKTAG('I', 'A', 'R', 'T'):
+            avi_read_tag(pb, s->author, sizeof(s->author), size);
+            break;
+        case MKTAG('I', 'C', 'O', 'P'):
+            avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
+            break;
+        case MKTAG('I', 'C', 'M', 'T'):
+            avi_read_tag(pb, s->comment, sizeof(s->comment), size);
+            break;
+        case MKTAG('I', 'G', 'N', 'R'):
+            avi_read_tag(pb, s->genre, sizeof(s->genre), size);
+            break;
+        case MKTAG('I', 'P', 'R', 'D'):
+            avi_read_tag(pb, s->album, sizeof(s->album), size);
+            break;
+        case MKTAG('I', 'P', 'R', 'T'):
+            avi_read_tag(pb, str_track, sizeof(str_track), size);
+            sscanf(str_track, "%d", &s->track);
+            break;
         default:
             /* skip tag */
             size += (size & 1);
@@ -446,7 +504,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
         return -1;
     }
 
-    if(!avi->index_loaded)
+    if(!avi->index_loaded && !url_is_streamed(pb))
         avi_load_index(s);
     avi->index_loaded = 1;
     avi->non_interleaved |= guess_ni_flag(s);
@@ -522,7 +580,7 @@ resync:
         AVIStream *ast= st->priv_data;
         int size;
 
-        if(ast->sample_size == 0)
+        if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
             size= INT_MAX;
         else if(ast->sample_size < 32)
             size= 64*ast->sample_size;
@@ -737,7 +795,7 @@ static int avi_read_idx1(AVFormatContext *s, int size)
         ast = st->priv_data;
 
 #if defined(DEBUG_SEEK)
-        av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%d\n", len, ast->cum_len);
+        av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%Ld\n", len, ast->cum_len);
 #endif
         if(last_pos == pos)
             avi->non_interleaved= 1;
@@ -850,7 +908,7 @@ static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
         if (st2->nb_index_entries <= 0)
             continue;
 
-//        assert(st2->codec.block_align);
+//        assert(st2->codec->block_align);
         assert(st2->time_base.den == ast2->rate);
         assert(st2->time_base.num == ast2->scale);
         index = av_index_search_timestamp(
@@ -891,7 +949,6 @@ static int avi_read_close(AVFormatContext *s)
         AVStream *st = s->streams[i];
         AVIStream *ast = st->priv_data;
         av_free(ast);
-        av_free(st->codec->extradata);
         av_free(st->codec->palctrl);
     }
 
@@ -915,7 +972,7 @@ static int avi_probe(AVProbeData *p)
         return 0;
 }
 
-static AVInputFormat avi_iformat = {
+AVInputFormat avi_demuxer = {
     "avi",
     "avi format",
     sizeof(AVIContext),
@@ -925,9 +982,3 @@ static AVInputFormat avi_iformat = {
     avi_read_close,
     avi_read_seek,
 };
-
-int avidec_init(void)
-{
-    av_register_input_format(&avi_iformat);
-    return 0;
-}