]> git.sesse.net Git - vlc/blobdiff - modules/demux/avi/libavi.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / demux / avi / libavi.c
index 51c9c224c686050e0db9e9be159040cc04367d9b..678e060277f6e734bbc49217cecdfa6d8579ce20 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * libavi.c :
+ * libavi.c : LibAVI
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: libavi.c,v 1.25 2003/08/30 02:03:44 fenrir Exp $
+ * Copyright (C) 2001 the VideoLAN team
+ * $Id$
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
-#include "ninput.h"
-#include "codecs.h"                                      /* BITMAPINFOHEADER */
+#include <vlc_demux.h>
+#include <vlc_codecs.h>                                /* BITMAPINFOHEADER */
 
 #include "libavi.h"
 
 #define AVI_DEBUG 1
 
-#define FREE( p ) \
-    if( p ) {free( p ); p = NULL; }
-
 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
 
 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
@@ -72,7 +67,7 @@ static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
 
 #ifdef AVI_DEBUG
     msg_Dbg( (vlc_object_t*)s,
-             "Found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
+             "found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
              p_chk->common.i_chunk_fourcc,
              (char*)&p_chk->common.i_chunk_fourcc,
              p_chk->common.i_chunk_size,
@@ -119,7 +114,7 @@ static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
     uint8_t *p_peek;
     vlc_bool_t b_seekable;
 
-    if( p_container->common.i_chunk_size < 8 )
+    if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
     {
         /* empty box */
         msg_Warn( (vlc_object_t*)s, "empty list chunk" );
@@ -138,12 +133,12 @@ static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
     if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
         p_container->list.i_type == AVIFOURCC_movi )
     {
-        msg_Dbg( (vlc_object_t*)s, "Skipping movi chunk" );
+        msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
         if( b_seekable )
         {
             return AVI_NextChunk( s, p_container );
         }
-        return VLC_SUCCESS; // point at begining of LIST-movi
+        return VLC_SUCCESS; /* point at begining of LIST-movi */
     }
 
     if( stream_Read( s, NULL, 12 ) != 12 )
@@ -172,16 +167,22 @@ static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
         }
         p_container->common.p_last = p_chk;
 
-        if( AVI_ChunkRead( s, p_chk, p_container ) ||
-           ( stream_Tell( s ) >=
+        if( AVI_ChunkRead( s, p_chk, p_container ) )
+        {
+            break;
+        }
+        if( p_chk->common.p_father->common.i_chunk_size > 0 &&
+           ( stream_Tell( s ) >
               (off_t)p_chk->common.p_father->common.i_chunk_pos +
                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
         {
             break;
         }
+
         /* If we can't seek then stop when we 've found LIST-movi */
         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
-            p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
+            p_chk->list.i_type == AVIFOURCC_movi &&
+            ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
         {
             break;
         }
@@ -202,43 +203,44 @@ static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
     i_read = stream_Read( s, p_read, i_read ); \
     if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
     { \
+        free( p_buff ); \
         return VLC_EGENERIC; \
     }\
     p_read += 8; \
     i_read -= 8
 
+#define AVI_READ( res, func, size ) \
+    if( i_read < size ) { \
+        free( p_buff); \
+        return VLC_EGENERIC; \
+    } \
+    i_read -= size; \
+    res = func( p_read ); \
+    p_read += size \
+
 #define AVI_READCHUNK_EXIT( code ) \
     free( p_buff ); \
-    if( i_read < 0 ) \
-    { \
-        msg_Warn( (vlc_object_t*)s, "not enough data" ); \
-    } \
     return code
 
+static inline uint8_t GetB( uint8_t *ptr )
+{
+    return *ptr;
+}
+
 #define AVI_READ1BYTE( i_byte ) \
-    i_byte = *p_read; \
-    p_read++; \
-    i_read--
+    AVI_READ( i_byte, GetB, 1 )
 
 #define AVI_READ2BYTES( i_word ) \
-    i_word = GetWLE( p_read ); \
-    p_read += 2; \
-    i_read -= 2
+    AVI_READ( i_word, GetWLE, 2 )
 
 #define AVI_READ4BYTES( i_dword ) \
-    i_dword = GetDWLE( p_read ); \
-    p_read += 4; \
-    i_read -= 4
+    AVI_READ( i_dword, GetDWLE, 4 )
 
-#define AVI_READ8BYTES( i_dword ) \
-    i_dword = GetQWLE( p_read ); \
-    p_read += 8; \
-    i_read -= 8
+#define AVI_READ8BYTES( i_qword ) \
+    AVI_READ( i_qword, GetQWLE, 8 )
 
 #define AVI_READFOURCC( i_dword ) \
-    i_dword = GetFOURCC( p_read ); \
-    p_read += 4; \
-    i_read -= 4
+    AVI_READ( i_dword, GetFOURCC, 4 )
 
 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
 {
@@ -338,6 +340,12 @@ static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
                     p_chk->strf.auds.p_wf->cbSize =
                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
                 }
+                if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
+                {
+                    /* Found an extensible header atm almost nothing uses that. */
+                    msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
+                              "vorbis audio dectected: not supported" );
+                }
             }
             else
             {
@@ -346,7 +354,7 @@ static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
             if( p_chk->strf.auds.p_wf->cbSize > 0 )
             {
                 memcpy( &p_chk->strf.auds.p_wf[1] ,
-                        p_buff + 8 + sizeof( WAVEFORMATEX ),    // 8=fourrc+size
+                        p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourrc+size */
                         p_chk->strf.auds.p_wf->cbSize );
             }
 #ifdef AVI_DEBUG
@@ -360,7 +368,7 @@ static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
 #endif
             break;
         case( AVIFOURCC_vids ):
-            p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
+            p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
             p_chk->strf.vids.i_cat = VIDEO_ES;
             p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
@@ -374,17 +382,15 @@ static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
-            if( p_chk->strf.vids.p_bih->biSize >
-                        p_chk->common.i_chunk_size )
+            if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
             {
                 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
             }
             if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
             {
                 memcpy( &p_chk->strf.vids.p_bih[1],
-                        p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
-                        p_chk->strf.vids.p_bih->biSize -
-                                                    sizeof(BITMAPINFOHEADER) );
+                        p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
+                        p_chk->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
             }
 #ifdef AVI_DEBUG
             msg_Dbg( (vlc_object_t*)s,
@@ -408,11 +414,11 @@ static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
     avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
     if( p_strf->common.i_cat == AUDIO_ES )
     {
-        FREE( p_strf->auds.p_wf );
+        FREENULL( p_strf->auds.p_wf );
     }
     else if( p_strf->common.i_cat == VIDEO_ES )
     {
-        FREE( p_strf->vids.p_bih );
+        FREENULL( p_strf->vids.p_bih );
     }
 }
 
@@ -420,12 +426,15 @@ static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
 {
     AVI_READCHUNK_ENTER;
     p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
-    memcpy( p_chk->strd.p_data,
-            p_buff + 8,
-            p_chk->common.i_chunk_size );
+    memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
     AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
 
+static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
+{
+    if( p_chk->strd.p_data ) free( p_chk->strd.p_data );
+}
+
 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
 {
     unsigned int i_count, i_index;
@@ -462,7 +471,7 @@ static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
 {
     p_chk->idx1.i_entry_count = 0;
     p_chk->idx1.i_entry_max   = 0;
-    FREE( p_chk->idx1.entry )
+    FREENULL( p_chk->idx1.entry )
 }
 
 
@@ -547,9 +556,9 @@ static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
 {
     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
 
-    FREE( p_indx->idx.std );
-    FREE( p_indx->idx.field );
-    FREE( p_indx->idx.super );
+    FREENULL( p_indx->idx.std );
+    FREENULL( p_indx->idx.field );
+    FREENULL( p_indx->idx.super );
 }
 
 
@@ -557,7 +566,7 @@ static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
 static struct
 {
     vlc_fourcc_t i_fourcc;
-    char *psz_type;
+    const char *psz_type;
 } AVI_strz_type[] =
 {
     { AVIFOURCC_IARL, "archive location" },
@@ -569,7 +578,7 @@ static struct
     { AVIFOURCC_ICRP, "cropped" },
     { AVIFOURCC_IDIM, "dimensions" },
     { AVIFOURCC_IDPI, "dots per inch" },
-    { AVIFOURCC_IENG, "enginner" },
+    { AVIFOURCC_IENG, "engineer" },
     { AVIFOURCC_IGNR, "genre" },
     { AVIFOURCC_IKEY, "keywords" },
     { AVIFOURCC_ILGT, "lightness" },
@@ -620,8 +629,8 @@ static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
 {
     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
-    FREE( p_strz->p_type );
-    FREE( p_strz->p_str );
+    FREENULL( p_strz->p_type );
+    FREENULL( p_strz->p_str );
 }
 
 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
@@ -645,7 +654,7 @@ static struct
     { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
     { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
     { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
-    { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
+    { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
     { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
     { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
     { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
@@ -719,8 +728,10 @@ int  _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
     {
         return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
     }
-    else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
-             ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
+    else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
+               ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
+             ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
+               ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
     {
         p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
         return AVI_ChunkRead_indx( s, p_chk );