]> git.sesse.net Git - vlc/blobdiff - modules/demux/avi/libavi.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / demux / avi / libavi.c
index 1fe59e30129c6242ba50fb0aebe05364a677c9c9..678e060277f6e734bbc49217cecdfa6d8579ce20 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************
- * libavi.c : 
+ * libavi.c : LibAVI
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: libavi.c,v 1.1 2002/10/15 00:56:43 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
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program 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
  *
  * 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 <string.h>                                              /* strdup() */
-#include <errno.h>
-#include <sys/types.h>
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
+#include <vlc_demux.h>
+#include <vlc_codecs.h>                                /* BITMAPINFOHEADER */
 
 #include "libavi.h"
 
 #define AVI_DEBUG 1
-#define AVIFOURCC_PRINT( x ) \
-    (x)&0xff,           \
-    ( (x) >>  8 )&0xff, \
-    ( (x) >> 16 )&0xff, \
-    ( (x) >> 24 )&0xff
-    
-#define FREE( p ) \
-    if( p ) {free( p ); p = NULL; }
 
 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
-  
-/* Some functions to manipulate memory */
-static u16 GetWLE( u8 *p_buff )
-{
-    return( (p_buff[0]) + ( p_buff[1] <<8 ) );
-}
 
-static u32 GetDWLE( u8 *p_buff )
-{
-    return( p_buff[0] + ( p_buff[1] <<8 ) +
-            ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
-}
 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
 {
-    return( VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] ) );
-}
-/*****************************************************************************
- * Some basic functions to manipulate stream more easily in vlc
- *
- * AVI_TellAbsolute get file position
- * 
- * AVI_SeekAbsolute seek in the file
- *
- * AVI_ReadData read data from the file in a buffer
- *
- * AVI_SkipBytes skip bytes
- *
- *****************************************************************************/
-off_t AVI_TellAbsolute( input_thread_t *p_input )
-{
-    off_t i_pos;
-    
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    
-    i_pos= p_input->stream.p_selected_area->i_tell -
-            ( p_input->p_last_data - p_input->p_current_data  );
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    return( i_pos );
-}
-int AVI_SeekAbsolute( input_thread_t *p_input,
-                      off_t i_pos)
-{
-    off_t i_filepos;
-
-    if( i_pos >= p_input->stream.p_selected_area->i_size )
-    {
-        return( 0 );
-    }
-    
-    i_filepos = AVI_TellAbsolute( p_input );
-
-    if( i_filepos == i_pos )
-    {
-        return( 1 );
-    }
-
-    if( p_input->stream.b_seekable &&
-        p_input->stream.i_method == INPUT_METHOD_FILE )
-    {
-        p_input->pf_seek( p_input, i_pos );
-        input_AccessReinit( p_input );
-        return( 1 );
-    }
-    else
-    {
-        int i_peek;
-        int i_skip = i_pos - i_filepos;
-        u8  *p_peek;
-        msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
-        if( i_skip < 0 )
-        {
-            return( 0 ); // failed
-        }
-        while( i_skip > 0 )
-        {
-            i_peek = input_Peek( p_input, &p_peek, i_skip+1 );
-            i_peek--;
-            i_skip -= i_peek;
-            vlc_mutex_lock( &p_input->stream.stream_lock );
-            p_input->p_current_data += i_peek;  // skip them
-            vlc_mutex_unlock( &p_input->stream.stream_lock );
-            if( i_peek <= 0 )
-            {
-                return( 0);
-            }
-        }
-        return( 1 );
-    }
-}
-
-/* return 1 if success, 0 if fail */
-int AVI_ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
-{
-    data_packet_t *p_data;
-
-    int i_count;
-    int i_read = 0;
-
-    
-    if( !i_size )
-    {
-        return( 0 );
-    }
-
-    do
-    {
-        i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
-        if( i_count <= 0 )
-        {
-            return( i_read );
-        }
-        memcpy( p_buff, p_data->p_payload_start, i_count );
-        input_DeletePacket( p_input->p_method_data, p_data );
-        
-        p_buff += i_count;
-        i_size -= i_count;
-        i_read += i_count;
-                
-    } while( i_size );
-    
-    return( i_read );
-}
-
-int  AVI_SkipBytes( input_thread_t *p_input, int i_count )
-{
-    int i_buff_size;
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    i_buff_size = p_input->p_last_data - p_input->p_current_data;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-            
-    if( i_count > 0 && i_count + 1 < i_buff_size )
-    {
-        u8 *p_peek;
-        
-        input_Peek( p_input, &p_peek, i_count + 1 );
-
-        vlc_mutex_lock( &p_input->stream.stream_lock );
-        p_input->p_current_data += i_count;  // skip them
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        return( 1 );
-    }
-    else
-    {
-        return( AVI_SeekAbsolute( p_input, 
-                              AVI_TellAbsolute( p_input ) + i_count ) );
-    }
+    return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
 }
 
-/*****************************************************************************
- *
- * AVI_TestFile: look at first bytes to see if it's a valid avi file
- * 
- * unseekable: ok
- *
- *****************************************************************************/
-int AVI_TestFile( input_thread_t *p_input )
-{
-    u8  *p_peek;
-    
-    if( input_Peek( p_input, &p_peek, 8 ) < 8 )
-    {
-        msg_Err( p_input, "cannot peek()" );
-        return( 0 );
-    }
+#define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
+void    _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
 
-    if( GetDWLE( p_peek ) == AVIFOURCC_RIFF && 
-        GetDWLE( p_peek + 8 ) == AVIFOURCC_AVI )
-    {
-        return( 1 );
-    }
-    else
-    {
-        return( 0 );
-    }
-}
 /****************************************************************************
  *
  * Basics functions to manipulates chunks
  *
  ****************************************************************************/
-static int AVI_ChunkReadCommon( input_thread_t *p_input,
-                                avi_chunk_t *p_chk )
+static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
 {
-    u8  *p_peek;
+    uint8_t  *p_peek;
     int i_peek;
 
     memset( p_chk, 0, sizeof( avi_chunk_t ) );
 
-    if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
+    if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
     {
-        return( 0 );
+        return VLC_EGENERIC;
     }
-    
-    p_chk->common.i_chunk_fourcc = GetDWLE( p_peek );
+
+    p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
     p_chk->common.i_chunk_size   = GetDWLE( p_peek + 4 );
-    p_chk->common.i_chunk_pos    = AVI_TellAbsolute( p_input );
+    p_chk->common.i_chunk_pos    = stream_Tell( s );
 
     p_chk->common.p_father = NULL;
     p_chk->common.p_next = NULL;
     p_chk->common.p_first = NULL;
     p_chk->common.p_next = NULL;
+
 #ifdef AVI_DEBUG
-    msg_Dbg( p_input, 
-             "Found Chunk fourcc:%c%c%c%c size:%lld pos:%lld",
-             AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
+    msg_Dbg( (vlc_object_t*)s,
+             "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,
              p_chk->common.i_chunk_pos );
 #endif
-    return( 1 );
+    return VLC_SUCCESS;
 }
 
-static int AVI_NextChunk( input_thread_t *p_input,
-                          avi_chunk_t *p_chk )
+static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
 {
     avi_chunk_t chk;
-    
+
     if( !p_chk )
     {
-        if( !AVI_ChunkReadCommon( p_input, &chk ) )
+        if( AVI_ChunkReadCommon( s, &chk ) )
         {
-            return( 0 );
+            return VLC_EGENERIC;
         }
         p_chk = &chk;
     }
 
     if( p_chk->common.p_father )
     {
-        if( p_chk->common.p_father->common.i_chunk_pos + 
+        if( p_chk->common.p_father->common.i_chunk_pos +
                 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
-            p_chk->common.i_chunk_pos + 
+            p_chk->common.i_chunk_pos +
                 __EVEN( p_chk->common.i_chunk_size ) + 8 )
         {
-            return( 0 );
+            return VLC_EGENERIC;
         }
     }
-    return( AVI_SeekAbsolute( p_input,
-                              p_chk->common.i_chunk_pos + 
-                                  __EVEN( p_chk->common.i_chunk_size ) + 8 ) );
-}
-
-int _AVI_ChunkGoto( input_thread_t *p_input,
-                   avi_chunk_t *p_chk )
-{
-    if( !p_chk )
-    {
-        return( 0 );
-    }
-    return( AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos ) );
+    return stream_Seek( s, p_chk->common.i_chunk_pos +
+                                 __EVEN( p_chk->common.i_chunk_size ) + 8 );
 }
 
 /****************************************************************************
  *
- * Functions to read chunks 
+ * Functions to read chunks
  *
  ****************************************************************************/
-static int AVI_ChunkRead_list( input_thread_t *p_input,
-                               avi_chunk_t *p_container,
-                               int b_seekable )
+static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
 {
     avi_chunk_t *p_chk;
-    u8 *p_peek;
-    
-    if( p_container->common.i_chunk_size < 8 )
+    uint8_t *p_peek;
+    vlc_bool_t b_seekable;
+
+    if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
     {
         /* empty box */
-        msg_Warn( p_input, "empty list chunk" );
-        return( 0 );
+        msg_Warn( (vlc_object_t*)s, "empty list chunk" );
+        return VLC_EGENERIC;
     }
-    if( input_Peek( p_input, &p_peek, 12 ) < 12 )
+    if( stream_Peek( s, &p_peek, 12 ) < 12 )
     {
-        msg_Warn( p_input, "cannot peek while reading list chunk" );
-        return( 0 );
+        msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
+        return VLC_EGENERIC;
     }
-    p_container->list.i_type = GetDWLE( p_peek + 8 );
+
+    stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
+
+    p_container->list.i_type = GetFOURCC( p_peek + 8 );
 
     if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
         p_container->list.i_type == AVIFOURCC_movi )
     {
-        msg_Dbg( p_input, "Skipping movi chunk" );
+        msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
         if( b_seekable )
         {
-            return( AVI_NextChunk( p_input, p_container ) );
-        }
-        else
-        {
-            return( 1 ); // point at begining of LIST-movi 
+            return AVI_NextChunk( s, p_container );
         }
+        return VLC_SUCCESS; /* point at begining of LIST-movi */
+    }
+
+    if( stream_Read( s, NULL, 12 ) != 12 )
+    {
+        msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
+        return VLC_EGENERIC;
     }
 
-    AVI_SkipBytes( p_input, 12 );
 #ifdef AVI_DEBUG
-    msg_Dbg( p_input, 
-             "found LIST chunk: \'%c%c%c%c\'",
-             AVIFOURCC_PRINT( p_container->list.i_type ) );
+    msg_Dbg( (vlc_object_t*)s,
+             "found LIST chunk: \'%4.4s\'",
+             (char*)&p_container->list.i_type );
 #endif
+    msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
     for( ; ; )
     {
         p_chk = malloc( sizeof( avi_chunk_t ) );
@@ -353,61 +167,82 @@ static int AVI_ChunkRead_list( input_thread_t *p_input,
         }
         p_container->common.p_last = p_chk;
 
-        if( !AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
-           ( AVI_TellAbsolute( p_input ) >=
-                p_chk->common.p_father->common.i_chunk_pos + 
-                    __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
+        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;
         }
 
-    } 
-    
-    return( 1 );
+    }
+    msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
+
+    return VLC_SUCCESS;
 }
 
 #define AVI_READCHUNK_ENTER \
-    s64 i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
-    u8  *p_read, *p_buff;    \
+    int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
+    uint8_t  *p_read, *p_buff;    \
     if( !( p_read = p_buff = malloc(i_read ) ) ) \
     { \
-        return( 0 ); \
+        return VLC_EGENERIC; \
     } \
-    i_read = AVI_ReadData( p_input, p_read, i_read ); \
+    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( p_input, "not enougth data" ); \
-    } \
-    return( code )
+    return code
+
+static inline uint8_t GetB( uint8_t *ptr )
+{
+    return *ptr;
+}
+
+#define AVI_READ1BYTE( i_byte ) \
+    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_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( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
 {
     AVI_READCHUNK_ENTER;
 
@@ -426,21 +261,19 @@ static int AVI_ChunkRead_avih( input_thread_t *p_input,
     AVI_READ4BYTES( p_chk->avih.i_start );
     AVI_READ4BYTES( p_chk->avih.i_length );
 #ifdef AVI_DEBUG
-    msg_Dbg( p_input, 
-             "avih: streams:%d flags:%s%s%s%s %dx%d", 
+    msg_Dbg( (vlc_object_t*)s,
+             "avih: streams:%d flags:%s%s%s%s %dx%d",
              p_chk->avih.i_streams,
              p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
              p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
              p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
              p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
              p_chk->avih.i_width, p_chk->avih.i_height );
-#endif 
-    AVI_READCHUNK_EXIT( 1 );
+#endif
+    AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
 
-static int AVI_ChunkRead_strh( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
 {
     AVI_READCHUNK_ENTER;
 
@@ -457,123 +290,158 @@ static int AVI_ChunkRead_strh( input_thread_t *p_input,
     AVI_READ4BYTES( p_chk->strh.i_quality );
     AVI_READ4BYTES( p_chk->strh.i_samplesize );
 #ifdef AVI_DEBUG
-    msg_Dbg( p_input, 
-             "strh: type:%c%c%c%c handler:0x%8.8x samplesize:%d %.2ffps",
-             AVIFOURCC_PRINT( p_chk->strh.i_type ),
+    msg_Dbg( (vlc_object_t*)s,
+             "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
+             (char*)&p_chk->strh.i_type,
              p_chk->strh.i_handler,
              p_chk->strh.i_samplesize,
-             ( p_chk->strh.i_scale ? 
+             ( p_chk->strh.i_scale ?
                 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
 #endif
-    
-    AVI_READCHUNK_EXIT( 1 );
+
+    AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
 
-static int AVI_ChunkRead_strf( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
 {
     avi_chunk_t *p_strh;
 
     AVI_READCHUNK_ENTER;
     if( p_chk->common.p_father == NULL )
     {
-        msg_Err( p_input, "malformed avi file" );
-        AVI_READCHUNK_EXIT( 0 );
+        msg_Err( (vlc_object_t*)s, "malformed avi file" );
+        AVI_READCHUNK_EXIT( VLC_EGENERIC );
     }
     if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
     {
-        msg_Err( p_input, "malformed avi file" );
-        AVI_READCHUNK_EXIT( 0 );
+        msg_Err( (vlc_object_t*)s, "malformed avi file" );
+        AVI_READCHUNK_EXIT( VLC_EGENERIC );
     }
-    
+
     switch( p_strh->strh.i_type )
     {
         case( AVIFOURCC_auds ):
-            AVI_READ2BYTES( p_chk->strf.auds.i_formattag );
-            AVI_READ2BYTES( p_chk->strf.auds.i_channels );
-            AVI_READ4BYTES( p_chk->strf.auds.i_samplespersec );
-            AVI_READ4BYTES( p_chk->strf.auds.i_avgbytespersec );
-            AVI_READ2BYTES( p_chk->strf.auds.i_blockalign );
-            AVI_READ2BYTES( p_chk->strf.auds.i_bitspersample );
-            if( p_chk->strf.auds.i_formattag != WAVE_FORMAT_PCM )
+            p_chk->strf.auds.i_cat = AUDIO_ES;
+            p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
+            AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
+            AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
+            AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
+            AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
+            AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
+            AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
+            if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
+                 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
+            {
+                AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
+                /* prevent segfault */
+                if( p_chk->strf.auds.p_wf->cbSize >
+                        p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
+                {
+                    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
+            {
+                p_chk->strf.auds.p_wf->cbSize = 0;
+            }
+            if( p_chk->strf.auds.p_wf->cbSize > 0 )
             {
-                AVI_READ2BYTES( p_chk->strf.auds.i_size );
+                memcpy( &p_chk->strf.auds.p_wf[1] ,
+                        p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourrc+size */
+                        p_chk->strf.auds.p_wf->cbSize );
             }
-            p_chk->strf.auds.p_wfx = malloc( p_chk->common.i_chunk_size );
-            memcpy( p_chk->strf.auds.p_wfx, 
-                    p_buff + 8, 
-                    p_chk->common.i_chunk_size );
 #ifdef AVI_DEBUG
-            msg_Dbg( p_input, 
+            msg_Dbg( (vlc_object_t*)s,
                      "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
-                     p_chk->strf.auds.i_formattag,
-                     p_chk->strf.auds.i_channels,
-                     p_chk->strf.auds.i_samplespersec,
-                     p_chk->strf.auds.i_bitspersample,
-                     p_chk->strf.auds.i_avgbytespersec * 8 / 1024 );
+                     p_chk->strf.auds.p_wf->wFormatTag,
+                     p_chk->strf.auds.p_wf->nChannels,
+                     p_chk->strf.auds.p_wf->nSamplesPerSec,
+                     p_chk->strf.auds.p_wf->wBitsPerSample,
+                     p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
 #endif
             break;
         case( AVIFOURCC_vids ):
-            p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
-            AVI_READ4BYTES( p_chk->strf.vids.i_size );
-            AVI_READ4BYTES( p_chk->strf.vids.i_width );
-            AVI_READ4BYTES( p_chk->strf.vids.i_height );
-            AVI_READ2BYTES( p_chk->strf.vids.i_planes );
-            AVI_READ2BYTES( p_chk->strf.vids.i_bitcount );
-            AVI_READFOURCC( p_chk->strf.vids.i_compression );
-            AVI_READ4BYTES( p_chk->strf.vids.i_sizeimage );
-            AVI_READ4BYTES( p_chk->strf.vids.i_xpelspermeter );
-            AVI_READ4BYTES( p_chk->strf.vids.i_ypelspermeter );
-            AVI_READ4BYTES( p_chk->strf.vids.i_clrused );
-            AVI_READ4BYTES( p_chk->strf.vids.i_clrimportant );
+            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 );
-            memcpy( p_chk->strf.vids.p_bih, 
-                    p_buff + 8, 
-                    p_chk->common.i_chunk_size );
+            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
+            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
+            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
+            AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
+            AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
+            AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
+            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
+            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
+            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 )
+            {
+                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->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
+            }
 #ifdef AVI_DEBUG
-            msg_Dbg( p_input,
-                     "strf: video:%c%c%c%c %dx%d planes:%d %dbpp",
-                     AVIFOURCC_PRINT( p_chk->strf.vids.i_compression ),
-                     p_chk->strf.vids.i_width,
-                     p_chk->strf.vids.i_height,
-                     p_chk->strf.vids.i_planes,
-                     p_chk->strf.vids.i_bitcount );
+            msg_Dbg( (vlc_object_t*)s,
+                     "strf: video:%4.4s %dx%d planes:%d %dbpp",
+                     (char*)&p_chk->strf.vids.p_bih->biCompression,
+                     p_chk->strf.vids.p_bih->biWidth,
+                     p_chk->strf.vids.p_bih->biHeight,
+                     p_chk->strf.vids.p_bih->biPlanes,
+                     p_chk->strf.vids.p_bih->biBitCount );
 #endif
             break;
         default:
-            msg_Warn( p_input, "unknown stream type" );
+            msg_Warn( (vlc_object_t*)s, "unknown stream type" );
+            p_chk->strf.common.i_cat = UNKNOWN_ES;
             break;
     }
-    AVI_READCHUNK_EXIT( 1 );
+    AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
-static void AVI_ChunkFree_strf( input_thread_t *p_input,
-                               avi_chunk_t *p_chk )
+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 )
+    {
+        FREENULL( p_strf->auds.p_wf );
+    }
+    else if( p_strf->common.i_cat == VIDEO_ES )
+    {
+        FREENULL( p_strf->vids.p_bih );
+    }
 }
 
-static int AVI_ChunkRead_strd( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+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,
-            p_chk->common.i_chunk_size );
-    AVI_READCHUNK_EXIT( 1 );
+    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( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
 {
-    int i_count, i_index;
+    unsigned int i_count, i_index;
 
     AVI_READCHUNK_ENTER;
 
-    i_count = __MIN( p_chk->common.i_chunk_size, i_read ) / 16;
+    i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
 
     p_chk->idx1.i_entry_count = i_count;
     p_chk->idx1.i_entry_max   = i_count;
@@ -583,7 +451,7 @@ static int AVI_ChunkRead_idx1( input_thread_t *p_input,
 
         for( i_index = 0; i_index < i_count ; i_index++ )
         {
-            AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_fourcc );
+            AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
@@ -594,23 +462,111 @@ static int AVI_ChunkRead_idx1( input_thread_t *p_input,
         p_chk->idx1.entry = NULL;
     }
 #ifdef AVI_DEBUG
-    msg_Dbg( p_input, "idx1: index entry:%d", i_count );
+    msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
 #endif
-    AVI_READCHUNK_EXIT( 1 );
+    AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
 
-static void AVI_ChunkFree_idx1( input_thread_t *p_input,
-                               avi_chunk_t *p_chk )
+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 )
+}
+
+
+
+static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
+{
+    unsigned int i_count, i;
+    int32_t      i_dummy;
+    avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
+
+    AVI_READCHUNK_ENTER;
+
+    AVI_READ2BYTES( p_indx->i_longsperentry );
+    AVI_READ1BYTE ( p_indx->i_indexsubtype );
+    AVI_READ1BYTE ( p_indx->i_indextype );
+    AVI_READ4BYTES( p_indx->i_entriesinuse );
+
+    AVI_READ4BYTES( p_indx->i_id );
+    p_indx->idx.std     = NULL;
+    p_indx->idx.field   = NULL;
+    p_indx->idx.super   = NULL;
+
+    if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
+    {
+        AVI_READ8BYTES( p_indx->i_baseoffset );
+        AVI_READ4BYTES( i_dummy );
+
+        i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
+        p_indx->i_entriesinuse = i_count;
+        p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
+
+        for( i = 0; i < i_count; i++ )
+        {
+            AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
+            AVI_READ4BYTES( p_indx->idx.std[i].i_size );
+        }
+    }
+    else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
+    {
+        AVI_READ8BYTES( p_indx->i_baseoffset );
+        AVI_READ4BYTES( i_dummy );
+
+        i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
+        p_indx->i_entriesinuse = i_count;
+        p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
+        for( i = 0; i < i_count; i++ )
+        {
+            AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
+            AVI_READ4BYTES( p_indx->idx.field[i].i_size );
+            AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
+        }
+    }
+    else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
+    {
+        p_indx->i_baseoffset = 0;
+        AVI_READ4BYTES( i_dummy );
+        AVI_READ4BYTES( i_dummy );
+        AVI_READ4BYTES( i_dummy );
+
+        i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
+        p_indx->i_entriesinuse = i_count;
+        p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
+
+        for( i = 0; i < i_count; i++ )
+        {
+            AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
+            AVI_READ4BYTES( p_indx->idx.super[i].i_size );
+            AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
+        }
+    }
+    else
+    {
+        msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
+    }
+
+#ifdef AVI_DEBUG
+    msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
+#endif
+    AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
+static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
+{
+    avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
 
-static struct 
+    FREENULL( p_indx->idx.std );
+    FREENULL( p_indx->idx.field );
+    FREENULL( p_indx->idx.super );
+}
+
+
+
+static struct
 {
-    u32 i_fourcc;
-    char *psz_type;
+    vlc_fourcc_t i_fourcc;
+    const char *psz_type;
 } AVI_strz_type[] =
 {
     { AVIFOURCC_IARL, "archive location" },
@@ -622,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" },
@@ -638,11 +594,10 @@ static struct
     { AVIFOURCC_ITCH, "technician" },
     { AVIFOURCC_ISMP, "time code" },
     { AVIFOURCC_IDIT, "digitalization time" },
+    { AVIFOURCC_strn, "stream name" },
     { 0,              "???" }
 };
-static int AVI_ChunkRead_strz( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
 {
     int i_index;
     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
@@ -664,41 +619,34 @@ static int AVI_ChunkRead_strz( input_thread_t *p_input,
         memcpy( p_strz->p_str, p_read, i_read );
     }
     p_strz->p_str[i_read] = 0;
-    
+
 #ifdef AVI_DEBUG
-    msg_Dbg( p_input, "%c%c%c%c: %s : %s", 
-             AVIFOURCC_PRINT( p_strz->i_chunk_fourcc), p_strz->p_type, p_strz->p_str);
+    msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
+             (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
 #endif
-    AVI_READCHUNK_EXIT( 1 );
+    AVI_READCHUNK_EXIT( VLC_SUCCESS );
 }
-static void AVI_ChunkFree_strz( input_thread_t *p_input,
-                                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( input_thread_t *p_input,
-                               avi_chunk_t *p_chk,
-                               int b_seekable )
+static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
 {
-    return( AVI_NextChunk( p_input, p_chk ) );
+    return AVI_NextChunk( s, p_chk );
 }
-static void AVI_ChunkFree_nothing( input_thread_t *p_input,
-                               avi_chunk_t *p_chk )
+static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
 {
 
 }
 
 static struct
 {
-    u32   i_fourcc;
-    int   (*AVI_ChunkRead_function)( input_thread_t *p_input, 
-                                     avi_chunk_t *p_chk,
-                                     int b_seekable );
-    void  (*AVI_ChunkFree_function)( input_thread_t *p_input,
-                                     avi_chunk_t *p_chk );
+    vlc_fourcc_t i_fourcc;
+    int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
+    void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
 } AVI_Chunk_Function [] =
 {
     { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
@@ -706,8 +654,9 @@ 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 },
 
     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
@@ -736,61 +685,62 @@ static struct
     { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
     { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
     { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+    { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
     { 0,           NULL,               NULL }
 };
 
-static int AVI_ChunkFunctionFind( int i_fourcc )
+static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
 {
-    int i_index;
+    unsigned int i_index;
     for( i_index = 0; ; i_index++ )
     {
         if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
             ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
         {
-            return( i_index );
+            return i_index;
         }
     }
 }
 
-int  _AVI_ChunkRead( input_thread_t *p_input,
-                     avi_chunk_t *p_chk,
-                     avi_chunk_t *p_father,
-                     int b_seekable )
+int  _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
 {
     int i_index;
-    int i_result;
-
 
     if( !p_chk )
     {
-        return( 0 );
+        return VLC_EGENERIC;
     }
 
-    if( !AVI_ChunkReadCommon( p_input, p_chk ) )
+    if( AVI_ChunkReadCommon( s, p_chk ) )
+    {
+        msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
+        return VLC_EGENERIC;
+    }
+    if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
     {
-        msg_Warn( p_input, "cannot read one chunk" );
-        return( 0 );
+        msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
+        return VLC_EGENERIC;
     }
     p_chk->common.p_father = p_father;
 
     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
     if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
     {
-        i_result = 
-            AVI_Chunk_Function[i_index].AVI_ChunkRead_function( p_input,
-                                                                p_chk,
-                                                                b_seekable );
+        return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
     }
-    else
+    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' ) )
     {
-        msg_Warn( p_input, "unknown chunk (not loaded)" );
-        i_result = AVI_NextChunk( p_input, p_chk );
+        p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
+        return AVI_ChunkRead_indx( s, p_chk );
     }
-
-    return( i_result );
+    msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
+    return AVI_NextChunk( s, p_chk );
 }
 
-void _AVI_ChunkFree( input_thread_t *p_input,
+void _AVI_ChunkFree( stream_t *s,
                      avi_chunk_t *p_chk )
 {
     int i_index;
@@ -800,13 +750,13 @@ void _AVI_ChunkFree( input_thread_t *p_input,
     {
         return;
     }
-    
+
     /* Free all child chunk */
     p_child = p_chk->common.p_first;
     while( p_child )
     {
         p_next = p_child->common.p_next;
-        AVI_ChunkFree( p_input, p_child );
+        AVI_ChunkFree( s, p_child );
         free( p_child );
         p_child = p_next;
     }
@@ -815,38 +765,80 @@ void _AVI_ChunkFree( input_thread_t *p_input,
     if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
     {
 #ifdef AVI_DEBUG
-        msg_Dbg( p_input, "free chunk %c%c%c%c", 
-                 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ) );
+        msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
+                 (char*)&p_chk->common.i_chunk_fourcc );
 #endif
-        AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
+        AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
     }
     else
     {
-        msg_Warn( p_input, "unknown chunk (not unloaded)" );
+        msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
     }
     p_chk->common.p_first = NULL;
     p_chk->common.p_last  = NULL;
-    
+
     return;
 }
 
-int AVI_ChunkReadRoot( input_thread_t *p_input,
-                       avi_chunk_t *p_root,
-                       int b_seekable )
+static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
+                                      avi_chunk_t  *p_chk, int i_level )
+{
+    char str[1024];
+    int i;
+    avi_chunk_t *p_child;
+
+    memset( str, ' ', sizeof( str ) );
+    for( i = 1; i < i_level; i++ )
+    {
+        str[i * 5] = '|';
+    }
+    if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
+        p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
+    {
+        sprintf( str + i_level * 5,
+                 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
+                 i_level ? '+' : '*',
+                 (char*)&p_chk->common.i_chunk_fourcc,
+                 (char*)&p_chk->list.i_type,
+                 p_chk->common.i_chunk_size,
+                 p_chk->common.i_chunk_pos );
+    }
+    else
+    {
+        sprintf( str + i_level * 5,
+                 "+ %4.4s size:"I64Fu" pos:"I64Fu,
+                 (char*)&p_chk->common.i_chunk_fourcc,
+                 p_chk->common.i_chunk_size,
+                 p_chk->common.i_chunk_pos );
+    }
+    msg_Dbg( p_obj, "%s", str );
+
+    p_child = p_chk->common.p_first;
+    while( p_child )
+    {
+        AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
+        p_child = p_child->common.p_next;
+    }
+}
+
+int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
 {
     avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
     avi_chunk_t      *p_chk;
-    
+    vlc_bool_t b_seekable;
+
+    stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
+
     p_list->i_chunk_pos  = 0;
-    p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
+    p_list->i_chunk_size = stream_Size( s );
     p_list->i_chunk_fourcc = AVIFOURCC_LIST;
     p_list->p_father = NULL;
     p_list->p_next  = NULL;
     p_list->p_first = NULL;
     p_list->p_last  = NULL;
 
-    p_list->i_type = MKFOURCC( 'r', 'o', 'o', 't' );
-    
+    p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
+
     for( ; ; )
     {
         p_chk = malloc( sizeof( avi_chunk_t ) );
@@ -861,10 +853,10 @@ int AVI_ChunkReadRoot( input_thread_t *p_input,
         }
         p_root->common.p_last = p_chk;
 
-        if( !AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
-           ( AVI_TellAbsolute( p_input ) >=
-                p_chk->common.p_father->common.i_chunk_pos + 
-                    __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
+        if( AVI_ChunkRead( s, p_chk, p_root ) ||
+           ( 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;
         }
@@ -874,26 +866,27 @@ int AVI_ChunkReadRoot( input_thread_t *p_input,
         {
             break;
         }
-    } 
-    
-    return( 1 );
+    }
+
+    AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
+    return VLC_SUCCESS;
 }
 
-void AVI_ChunkFreeRoot( input_thread_t *p_input,
+void AVI_ChunkFreeRoot( stream_t *s,
                         avi_chunk_t  *p_chk )
 {
-    AVI_ChunkFree( p_input, p_chk );
+    AVI_ChunkFree( s, p_chk );
 }
 
 
-int  _AVI_ChunkCount( avi_chunk_t *p_chk, u32 i_fourcc )
+int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
 {
     int i_count;
     avi_chunk_t *p_child;
 
     if( !p_chk )
     {
-        return( 0 );
+        return 0;
     }
 
     i_count = 0;
@@ -901,87 +894,43 @@ int  _AVI_ChunkCount( avi_chunk_t *p_chk, u32 i_fourcc )
     while( p_child )
     {
         if( p_child->common.i_chunk_fourcc == i_fourcc ||
-            ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST && 
+            ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
               p_child->list.i_type == i_fourcc ) )
         {
             i_count++;
         }
         p_child = p_child->common.p_next;
     }
-    return( i_count );
+    return i_count;
 }
 
-avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk, u32 i_fourcc, int i_number )
+void *_AVI_ChunkFind( avi_chunk_t *p_chk,
+                      vlc_fourcc_t i_fourcc, int i_number )
 {
     avi_chunk_t *p_child;
     if( !p_chk )
     {
-        return( NULL );
+        return NULL;
     }
     p_child = p_chk->common.p_first;
 
     while( p_child )
     {
         if( p_child->common.i_chunk_fourcc == i_fourcc ||
-            ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST && 
+            ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
               p_child->list.i_type == i_fourcc ) )
         {
             if( i_number == 0 )
             {
                 /* We found it */
-                return( p_child );
+                return p_child;
             }
 
             i_number--;
         }
         p_child = p_child->common.p_next;
     }
-    return( NULL );
+    return NULL;
 }
 
-static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
-                                      avi_chunk_t  *p_chk, int i_level )
-{
-    char str[1024];
-    int i;
-    avi_chunk_t *p_child;
-    
-    memset( str, ' ', sizeof( str ) );
-    for( i = 1; i < i_level; i++ )
-    {
-        str[i * 5] = '|';
-    }
-    if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
-        p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
-    {
-        sprintf( str + i_level * 5, 
-                 "%c %c%c%c%c-%c%c%c%c size:%lld pos:%lld",
-                 i_level ? '+' : '*',
-                 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
-                 AVIFOURCC_PRINT( p_chk->list.i_type ),
-                 p_chk->common.i_chunk_size,
-                 p_chk->common.i_chunk_pos );
-    }
-    else
-    {
-        sprintf( str + i_level * 5, 
-                 "+ %c%c%c%c size:%lld pos:%lld",
-                 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
-                 p_chk->common.i_chunk_size,
-                 p_chk->common.i_chunk_pos );
-    }
-    msg_Dbg( p_input, "%s", str );
-
-    p_child = p_chk->common.p_first;
-    while( p_child )
-    {
-        AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
-        p_child = p_child->common.p_next;
-    }
-}
-void _AVI_ChunkDumpDebug( input_thread_t *p_input,
-                         avi_chunk_t  *p_chk )
-{
-    AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );
-}