]> git.sesse.net Git - vlc/blobdiff - modules/demux/avi/avi.c
Remove unneeded msg_Err.
[vlc] / modules / demux / avi / avi.c
index 4b63a76dc2242394f445fb541af963ec85551191..723aa0efdab38de562cf8f01d41d77a0f426cea4 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * avi.c : AVI file Stream input module for vlc
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: avi.c,v 1.81 2003/12/02 13:31:23 gbazin Exp $
+ * Copyright (C) 2001-2004 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.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
-#include "codecs.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_demux.h>
+
+#include <vlc_interface.h>
+
+#include <vlc_meta.h>
+#include <vlc_codecs.h>
+#include <vlc_charset.h>
 
 #include "libavi.h"
-#include "avi.h"
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-static int  Open   ( vlc_object_t * );
-static void Close  ( vlc_object_t * );
 
-vlc_module_begin();
-    add_category_hint( N_("avi-demuxer"), NULL, VLC_TRUE );
-        add_bool( "avi-interleaved", 0, NULL,
-                  N_("force interleaved method"),
-                  N_("force interleaved method"), VLC_TRUE );
-        add_bool( "avi-index", 0, NULL,
-                  N_("force index creation"),
-                  N_("force index creation"), VLC_TRUE );
+#define INTERLEAVE_TEXT N_("Force interleaved method" )
+#define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
+
+#define INDEX_TEXT N_("Force index creation")
+#define INDEX_LONGTEXT N_( \
+    "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
+    "or incomplete (not seekable)." )
+
+static int  Open ( vlc_object_t * );
+static void Close( vlc_object_t * );
+
+static const int pi_index[] = {0,1,2};
+
+static const char *const ppsz_indexes[] = { N_("Ask"), N_("Always fix"),
+                                N_("Never fix") };
 
+vlc_module_begin();
+    set_shortname( "AVI" );
     set_description( N_("AVI demuxer") );
     set_capability( "demux", 212 );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_DEMUX );
+
+    add_bool( "avi-interleaved", 0, NULL,
+              INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true );
+    add_integer( "avi-index", 0, NULL,
+              INDEX_TEXT, INDEX_LONGTEXT, false );
+        change_integer_list( pi_index, ppsz_indexes, 0 );
+
     set_callbacks( Open, Close );
 vlc_module_end();
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int    Control         ( input_thread_t *, int, va_list );
-static int    Seek            ( input_thread_t *, mtime_t, int );
-static int    Demux_Seekable  ( input_thread_t * );
-static int    Demux_UnSeekable( input_thread_t *p_input );
+static int Control         ( demux_t *, int, va_list );
+static int Seek            ( demux_t *, mtime_t, int );
+static int Demux_Seekable  ( demux_t * );
+static int Demux_UnSeekable( demux_t * );
 
-#define FREE( p ) if( p ) { free( p ); (p) = NULL; }
 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
 
+typedef struct
+{
+    vlc_fourcc_t i_fourcc;
+    off_t        i_pos;
+    uint32_t     i_size;
+    vlc_fourcc_t i_type;     /* only for AVIFOURCC_LIST */
+
+    uint8_t      i_peek[8];  /* first 8 bytes */
+
+    unsigned int i_stream;
+    unsigned int i_cat;
+} avi_packet_t;
+
+
+typedef struct
+{
+    vlc_fourcc_t i_id;
+    uint32_t     i_flags;
+    off_t        i_pos;
+    uint32_t     i_length;
+    uint32_t     i_lengthtotal;
+
+} avi_entry_t;
+
+typedef struct
+{
+    bool      b_activated;
+
+    unsigned int    i_cat; /* AUDIO_ES, VIDEO_ES */
+    vlc_fourcc_t    i_codec;
+
+    int             i_rate;
+    int             i_scale;
+    int             i_samplesize;
+
+    es_out_id_t     *p_es;
+
+    /* Avi Index */
+    avi_entry_t     *p_index;
+    unsigned int    i_idxnb;
+    unsigned int    i_idxmax;
+
+    unsigned int    i_idxposc;  /* numero of chunk */
+    unsigned int    i_idxposb;  /* byte in the current chunk */
+
+    /* extra information given to the decoder */
+    void            *p_extra;
+
+    /* For VBR audio only */
+    unsigned int    i_blockno;
+    unsigned int    i_blocksize;
+
+    /* For muxed streams */
+    stream_t        *p_out_muxed;
+} avi_track_t;
+
+struct demux_sys_t
+{
+    mtime_t i_time;
+    mtime_t i_length;
+
+    bool  b_seekable;
+    bool  b_muxed;
+    avi_chunk_t ck_root;
+
+    bool  b_odml;
+
+    off_t   i_movi_begin;
+    off_t   i_movi_lastchunk_pos;   /* XXX position of last valid chunk */
+
+    /* number of streams and information */
+    unsigned int i_track;
+    avi_track_t  **track;
+
+    /* meta */
+    vlc_meta_t  *meta;
+
+    /* Progress box */
+    mtime_t    last_update;
+    int        i_dialog_id;
+};
+
 static inline off_t __EVEN( off_t i )
 {
     return (i & 1) ? i + 1 : i;
@@ -74,87 +179,109 @@ static mtime_t AVI_GetDPTS   ( avi_track_t *, int64_t i_count );
 static mtime_t AVI_GetPTS    ( avi_track_t * );
 
 
-static int AVI_StreamChunkFind( input_thread_t *, unsigned int i_stream );
-static int AVI_StreamChunkSet ( input_thread_t *,
+static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
+static int AVI_StreamChunkSet ( demux_t *,
                                 unsigned int i_stream, unsigned int i_ck );
-static int AVI_StreamBytesSet ( input_thread_t *,
+static int AVI_StreamBytesSet ( demux_t *,
                                 unsigned int i_stream, off_t   i_byte );
 
 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
 static int   AVI_GetKeyFlag    ( vlc_fourcc_t , uint8_t * );
 
-static int AVI_PacketGetHeader( input_thread_t *, avi_packet_t *p_pk );
-static int AVI_PacketNext     ( input_thread_t * );
-static int AVI_PacketRead     ( input_thread_t *, avi_packet_t *, block_t **);
-static int AVI_PacketSearch   ( input_thread_t * );
+static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
+static int AVI_PacketNext     ( demux_t * );
+static int AVI_PacketRead     ( demux_t *, avi_packet_t *, block_t **);
+static int AVI_PacketSearch   ( demux_t * );
 
-static void AVI_IndexLoad    ( input_thread_t * );
-static void AVI_IndexCreate  ( input_thread_t * );
-static void AVI_IndexAddEntry( demux_sys_t *, int, AVIIndexEntry_t * );
+static void AVI_IndexLoad    ( demux_t * );
+static void AVI_IndexCreate  ( demux_t * );
+static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
 
-static mtime_t  AVI_MovieGetLength( input_thread_t * );
+static mtime_t  AVI_MovieGetLength( demux_t * );
 
 /*****************************************************************************
  * Stream management
  *****************************************************************************/
-static int        AVI_TrackSeek  ( input_thread_t *, int, mtime_t );
-static int        AVI_TrackStopFinishedStreams( input_thread_t *);
+static int        AVI_TrackSeek  ( demux_t *, int, mtime_t );
+static int        AVI_TrackStopFinishedStreams( demux_t *);
+
+/* Remarks:
+ - For VBR mp3 stream:
+    count blocks by rounded-up chunksizes instead of chunks
+    we need full emulation of dshow avi demuxer bugs :(
+    fixes silly nandub-style a-v delaying in avi with vbr mp3...
+    (from mplayer 2002/08/02)
+ - to complete....
+ */
 
 /*****************************************************************************
  * Open: check file and initializes AVI structures
  *****************************************************************************/
 static int Open( vlc_object_t * p_this )
 {
-    input_thread_t  *p_input = (input_thread_t *)p_this;
+    demux_t  *p_demux = (demux_t *)p_this;
     demux_sys_t     *p_sys;
 
+    bool       b_index = false;
+    int              i_do_index;
+
     avi_chunk_t         ck_riff;
     avi_chunk_list_t    *p_riff = (avi_chunk_list_t*)&ck_riff;
     avi_chunk_list_t    *p_hdrl, *p_movi;
     avi_chunk_avih_t    *p_avih;
 
     unsigned int i_track;
-    unsigned int i;
-
-    uint8_t  *p_peek;
+    unsigned int i, i_peeker;
 
+    const uint8_t *p_peek;
 
     /* Is it an avi file ? */
-    if( stream_Peek( p_input->s, &p_peek, 12 ) < 12 )
+    if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
+
+    for( i_peeker = 0; i_peeker < 188; i_peeker++ )
     {
-        msg_Err( p_input, "cannot peek()" );
-        return VLC_EGENERIC;
+        if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
+            break;
+        if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
+            break;
+        p_peek++;
     }
-    if( strncmp( &p_peek[0], "RIFF", 4 ) || strncmp( &p_peek[8], "AVI ", 4 ) )
+    if( i_peeker == 188 )
     {
-        msg_Warn( p_input, "avi module discarded (invalid header)" );
         return VLC_EGENERIC;
     }
 
     /* Initialize input  structures. */
-    p_sys = p_input->p_demux_data = malloc( sizeof(demux_sys_t) );
+    p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
     memset( p_sys, 0, sizeof( demux_sys_t ) );
     p_sys->i_time   = 0;
     p_sys->i_length = 0;
-    p_sys->i_pcr    = 0;
     p_sys->i_movi_lastchunk_pos = 0;
-    p_sys->b_odml   = VLC_FALSE;
+    p_sys->b_odml   = false;
+    p_sys->b_muxed  = false;
     p_sys->i_track  = 0;
     p_sys->track    = NULL;
+    p_sys->meta     = NULL;
 
-    stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
+    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
+
+    p_demux->pf_control = Control;
+    p_demux->pf_demux = Demux_Seekable;
 
-    p_input->pf_demux_control = Control;
-    p_input->pf_demux = Demux_Seekable;
     /* For unseekable stream, automaticaly use Demux_UnSeekable */
-    if( !p_sys->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
+    if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
+    {
+        p_demux->pf_demux = Demux_UnSeekable;
+    }
+
+    if( i_peeker > 0 )
     {
-        p_input->pf_demux = Demux_UnSeekable;
+        stream_Read( p_demux->s, NULL, i_peeker );
     }
 
-    if( AVI_ChunkReadRoot( p_input->s, &p_sys->ck_root ) )
+    if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
     {
-        msg_Err( p_input, "avi module discarded (invalid file)" );
+        msg_Err( p_demux, "avi module discarded (invalid file)" );
         return VLC_EGENERIC;
     }
 
@@ -163,7 +290,7 @@ static int Open( vlc_object_t * p_this )
         unsigned int i_count =
             AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
 
-        msg_Warn( p_input, "multiple riff -> OpenDML ?" );
+        msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
         for( i = 1; i < i_count; i++ )
         {
             avi_chunk_list_t *p_sysx;
@@ -171,8 +298,8 @@ static int Open( vlc_object_t * p_this )
             p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
             if( p_sysx->i_type == AVIFOURCC_AVIX )
             {
-                msg_Warn( p_input, "detected OpenDML file" );
-                p_sys->b_odml = VLC_TRUE;
+                msg_Warn( p_demux, "detected OpenDML file" );
+                p_sys->b_odml = true;
                 break;
             }
         }
@@ -184,85 +311,75 @@ static int Open( vlc_object_t * p_this )
 
     if( !p_hdrl || !p_movi )
     {
-        msg_Err( p_input, "avi module discarded (invalid file)" );
+        msg_Err( p_demux, "avi module discarded (invalid file)" );
         goto error;
     }
 
     if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
     {
-        msg_Err( p_input, "cannot find avih chunk" );
+        msg_Err( p_demux, "cannot find avih chunk" );
         goto error;
     }
     i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
     if( p_avih->i_streams != i_track )
     {
-        msg_Warn( p_input,
+        msg_Warn( p_demux,
                   "found %d stream but %d are declared",
                   i_track, p_avih->i_streams );
     }
     if( i_track == 0 )
     {
-        msg_Err( p_input, "no stream defined!" );
+        msg_Err( p_demux, "no stream defined!" );
         goto error;
     }
 
-    /*  create one program */
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    if( input_InitStream( p_input, 0 ) == -1)
-    {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        msg_Err( p_input, "cannot init stream" );
-        goto error;
-    }
-    p_input->stream.i_mux_rate = 0; /* Fixed later */
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    /* print informations on streams */
-    msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
+    /* print information on streams */
+    msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
              i_track,
              p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
              p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
              p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
              p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
+    if( ( p_sys->meta = vlc_meta_New() ) )
     {
-        input_info_category_t *p_cat = input_InfoCategory( p_input, _("Avi") );
-        input_AddInfo( p_cat, _("Number of Streams"), "%d", i_track );
-        input_AddInfo( p_cat, _("Flags"), "%s%s%s%s",
-                       p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
-                       p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
-                       p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
-                       p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
+        char buffer[200];
+        sprintf( buffer, "%s%s%s%s",
+                 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
+                 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
+                 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
+                 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
+        vlc_meta_SetSetting( p_sys->meta, buffer );
     }
 
     /* now read info on each stream and create ES */
     for( i = 0 ; i < i_track; i++ )
     {
-        avi_track_t      *tk = malloc( sizeof( avi_track_t ) );
-        avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
-        avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
-        avi_chunk_strf_auds_t *p_auds;
-        avi_chunk_strf_vids_t *p_vids;
+        avi_track_t           *tk     = malloc( sizeof( avi_track_t ) );
+        if( !tk )
+            goto error;
+
+        avi_chunk_list_t      *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
+        avi_chunk_strh_t      *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
+        avi_chunk_STRING_t    *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
+        avi_chunk_strf_auds_t *p_auds = NULL;
+        avi_chunk_strf_vids_t *p_vids = NULL;
         es_format_t fmt;
 
-        tk->b_activated = VLC_FALSE;
-        tk->p_index     = 0;
-        tk->i_idxnb     = 0;
-        tk->i_idxmax    = 0;
-        tk->i_idxposc   = 0;
-        tk->i_idxposb   = 0;
+        memset( tk, 0, sizeof( avi_track_t ) );
 
-        p_auds = (void*)p_vids = (void*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
+        p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
+        p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
 
         if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
         {
-            msg_Warn( p_input, "stream[%d] incomplete", i );
+            msg_Warn( p_demux, "stream[%d] incomplete", i );
             continue;
         }
 
         tk->i_rate  = p_strh->i_rate;
         tk->i_scale = p_strh->i_scale;
         tk->i_samplesize = p_strh->i_samplesize;
-        msg_Dbg( p_input, "stream[%d] rate:%d scale:%d samplesize:%d",
+        msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
                  i, tk->i_rate, tk->i_scale, tk->i_samplesize );
 
         switch( p_strh->i_type )
@@ -271,6 +388,19 @@ static int Open( vlc_object_t * p_this )
                 tk->i_cat   = AUDIO_ES;
                 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
                                                   p_auds->p_wf->wFormatTag );
+                if( tk->i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
+                    tk->i_blocksize = 0; /* fix vorbis VBR decoding */
+                else if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
+                {
+                    if( p_auds->p_wf->wFormatTag == 1 )
+                    {
+                        tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
+                    }
+                    else
+                    {
+                        tk->i_blocksize = 1;
+                    }
+                }
                 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
 
                 fmt.audio.i_channels        = p_auds->p_wf->nChannels;
@@ -278,74 +408,284 @@ static int Open( vlc_object_t * p_this )
                 fmt.i_bitrate               = p_auds->p_wf->nAvgBytesPerSec*8;
                 fmt.audio.i_blockalign      = p_auds->p_wf->nBlockAlign;
                 fmt.audio.i_bitspersample   = p_auds->p_wf->wBitsPerSample;
+                fmt.b_packetized            = !tk->i_blocksize;
+
+                msg_Dbg( p_demux,
+                    "stream[%d] audio(0x%x) %d channels %dHz %dbits",
+                    i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
+                    p_auds->p_wf->nSamplesPerSec, 
+                    p_auds->p_wf->wBitsPerSample );
+
                 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
                     p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
-                fmt.p_extra = &p_auds->p_wf[1];
-                msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
-                         i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
-                         p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
+                fmt.p_extra = tk->p_extra = malloc( fmt.i_extra );
+                if( !fmt.p_extra ) goto error;
+                memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
+
+                /* Rewrite the vorbis headers from Xiph-like format
+                 * to VLC internal format
+                 *
+                 * Xiph format:
+                 *  - 1st byte == N, is the number of packets - 1
+                 *  - Following bytes are the size of the N first packets:
+                 *      while( *p == 0xFF ) { size += 0xFF; p++ } size += *p;
+                 *      (the size of the last packet is the size of remaining
+                 *      data in the buffer)
+                 *  - Finally, all the packets concatenated
+                 *
+                 * VLC format:
+                 *  - Size of the packet on 16 bits (big endian) FIXME: should be 32 bits to be safe
+                 *  - The packet itself
+                 *  - Size of the next packet, and so on ...
+                 */
+
+                if( tk->i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
+                {
+                    uint8_t *p_extra = fmt.p_extra; 
+                    size_t i_extra = fmt.i_extra;
+
+                    if( i_extra <= 1 ) break;
+                    if( *p_extra++ != 2 ) break; /* 3 packets - 1 = 2 */
+                    i_extra--;
+
+                    size_t i_identifier_len = 0;
+                    while( *p_extra == 0xFF )
+                    {
+                        i_identifier_len += 0xFF;
+                        p_extra++;
+                        if( --i_extra <= 1 ) break;
+                    }
+                    i_identifier_len += *p_extra++;
+                    if( i_identifier_len > --i_extra ) break;
+
+                    size_t i_comment_len = 0;
+                    while( *p_extra == 0xFF )
+                    {
+                        i_comment_len += 0xFF;
+                        p_extra++;
+                        if( --i_extra <= 1 ) break;
+                    }
+                    i_comment_len += *p_extra++;
+                    if( i_comment_len > --i_extra ) break;
+                    size_t i_cookbook_len = i_extra;
+
+                    size_t i_headers_size = 3  * 2 + i_identifier_len +
+                                            i_comment_len + i_cookbook_len;
+                    uint8_t *p_out = malloc( i_headers_size );
+                    if( !p_out ) goto error;
+                    free( fmt.p_extra );
+                    fmt.p_extra = tk->p_extra = p_out;
+                    fmt.i_extra = i_headers_size;
+                    #define copy_packet( len ) \
+                        *p_out++ = len >> 8; \
+                        *p_out++ = len & 0xFF; \
+                        memcpy( p_out, p_extra, len ); \
+                        p_out += len; \
+                        p_extra += len;
+                    copy_packet( i_identifier_len );
+                    copy_packet( i_comment_len );
+                    copy_packet( i_cookbook_len );
+                    #undef copy_packet
+                    break;
+                }
                 break;
 
             case( AVIFOURCC_vids ):
                 tk->i_cat   = VIDEO_ES;
                 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
                                                   p_vids->p_bih->biCompression );
-                es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
+                if( p_vids->p_bih->biCompression == 0x00 )
+                {
+                    switch( p_vids->p_bih->biBitCount )
+                    {
+                        case 32:
+                            tk->i_codec = VLC_FOURCC('R','V','3','2');
+                            break;
+                        case 24:
+                            tk->i_codec = VLC_FOURCC('R','V','2','4');
+                            break;
+                        case 16:
+                            /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
+                            /* break;*/
+                        case 15:
+                            tk->i_codec = VLC_FOURCC('R','V','1','5');
+                            break;
+                        case 9:
+                            tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
+                            break;
+                        case 8:
+                            tk->i_codec = VLC_FOURCC('Y','8','0','0');
+                            break;
+                    }
+                    es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
+
+                    if( p_vids->p_bih->biBitCount == 24 )
+                    {
+                        /* This is in BGR format */
+                        fmt.video.i_bmask = 0x00ff0000;
+                        fmt.video.i_gmask = 0x0000ff00;
+                        fmt.video.i_rmask = 0x000000ff;
+                    }
+                }
+                else
+                {
+                    es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
+                    if( tk->i_codec == FOURCC_mp4v &&
+                        !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
+                    {
+                        fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
+                    }
+                }
                 tk->i_samplesize = 0;
                 fmt.video.i_width  = p_vids->p_bih->biWidth;
                 fmt.video.i_height = p_vids->p_bih->biHeight;
+                fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
+                fmt.video.i_frame_rate = tk->i_rate;
+                fmt.video.i_frame_rate_base = tk->i_scale;
                 fmt.i_extra =
                     __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
                            p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
                 fmt.p_extra = &p_vids->p_bih[1];
-                msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
-                        i,
-                         (char*)&p_vids->p_bih->biCompression,
-                         p_vids->p_bih->biWidth,
-                         p_vids->p_bih->biHeight,
+                msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
+                         i, (char*)&p_vids->p_bih->biCompression,
+                         (uint32_t)p_vids->p_bih->biWidth,
+                         (uint32_t)p_vids->p_bih->biHeight,
                          p_vids->p_bih->biBitCount,
                          (float)tk->i_rate/(float)tk->i_scale );
+
+                if( p_vids->p_bih->biCompression == 0x00 )
+                {
+                    /* RGB DIB are coded from bottom to top */
+                    fmt.video.i_height =
+                        (unsigned int)(-(int)p_vids->p_bih->biHeight);
+                }
+
+                /* Extract palette from extradata if bpp <= 8
+                 * (assumes that extradata contains only palette but appears
+                 *  to be true for all palettized codecs we support) */
+                if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
+                    fmt.video.i_bits_per_pixel > 0 )
+                {
+                    int i;
+
+                    fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
+                    fmt.video.p_palette->i_entries = 1;
+
+                    /* Apparently this is necessary. But why ? */
+                    fmt.i_extra =
+                        p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
+                    for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
+                    {
+                        ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
+                            GetDWLE((uint32_t*)fmt.p_extra + i);
+                    }
+                }
                 break;
+
+            case( AVIFOURCC_txts):
+                tk->i_cat   = SPU_ES;
+                tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
+                msg_Dbg( p_demux, "stream[%d] subtitles", i );
+                es_format_Init( &fmt, SPU_ES, tk->i_codec );
+                break;
+
+            case( AVIFOURCC_iavs):
+            case( AVIFOURCC_ivas):
+                p_sys->b_muxed = true;
+                msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
+                if( p_strh->i_handler == FOURCC_dvsd ||
+                    p_strh->i_handler == FOURCC_dvhd ||
+                    p_strh->i_handler == FOURCC_dvsl ||
+                    p_strh->i_handler == FOURCC_dv25 ||
+                    p_strh->i_handler == FOURCC_dv50 )
+                {
+                    tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
+                    if( !tk->p_out_muxed )
+                        msg_Err( p_demux, "could not load the DV parser" );
+                    else break;
+                }
+                free( tk );
+                continue;
+
+            case( AVIFOURCC_mids):
+                msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
+
             default:
-                msg_Warn( p_input, "stream[%d] unknown type", i );
+                msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
                 free( tk );
                 continue;
         }
-        tk->p_es = es_out_Add( p_input->p_es_out, &fmt );
+        if( p_strn )
+        {
+            /* The charset of p_strn is undefined */
+            EnsureUTF8( p_strn->p_str );
+            fmt.psz_description = strdup( p_strn->p_str );
+        }
+        if( tk->p_out_muxed == NULL )
+            tk->p_es = es_out_Add( p_demux->out, &fmt );
         TAB_APPEND( p_sys->i_track, p_sys->track, tk );
     }
 
     if( p_sys->i_track <= 0 )
     {
-        msg_Err( p_input, "No valid track" );
+        msg_Err( p_demux, "no valid track" );
         goto error;
     }
 
-    if( config_GetInt( p_input, "avi-index" ) )
+    i_do_index =  config_GetInt( p_demux, "avi-index" );
+    if( i_do_index == 1 ) /* Always fix */
     {
+aviindex:
         if( p_sys->b_seekable )
         {
-            AVI_IndexCreate( p_input );
+            AVI_IndexCreate( p_demux );
         }
         else
         {
-            msg_Warn( p_input, "cannot create index (unseekable stream)" );
-            AVI_IndexLoad( p_input );
+            msg_Warn( p_demux, "cannot create index (unseekable stream)" );
+            AVI_IndexLoad( p_demux );
         }
     }
     else
     {
-        AVI_IndexLoad( p_input );
+        AVI_IndexLoad( p_demux );
     }
 
     /* *** movie length in sec *** */
-    p_sys->i_length = AVI_MovieGetLength( p_input );
+    p_sys->i_length = AVI_MovieGetLength( p_demux );
     if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
                           (mtime_t)p_avih->i_microsecperframe /
                           (mtime_t)1000000 )
     {
-        msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
+        msg_Warn( p_demux, "broken or missing index, 'seek' will be "
+                           "approximative or will exhibit strange behavior" );
+        if( i_do_index == 0 && !b_index )
+        {
+            if( !p_sys->b_seekable ) {
+                b_index = true;
+                goto aviindex;
+            }
+            int i_create;
+            i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
+                        _( "This AVI file is broken. Seeking will not "
+                        "work correctly.\nDo you want to "
+                        "try to repair it?\n\nThis might take a long time." ),
+                        _( "Repair" ), _( "Don't repair" ), _( "Cancel") );
+            if( i_create == DIALOG_OK_YES )
+            {
+                b_index = true;
+                msg_Dbg( p_demux, "Fixing AVI index" );
+                goto aviindex;
+            }
+            else if( i_create == DIALOG_CANCELLED )
+            {
+                /* Kill input */
+                vlc_object_kill( p_demux->p_parent );
+                goto error;
+            }
+        }
     }
+
     /* fix some BeOS MediaKit generated file */
     for( i = 0 ; i < p_sys->i_track; i++ )
     {
@@ -379,34 +719,32 @@ static int Open( vlc_object_t * p_this )
 
             if( i_length == 0 )
             {
-                msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
+                msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
                 continue;
             }
             tk->i_samplesize = 1;
             tk->i_rate       = i_track_length  * (int64_t)1000000/ i_length;
-            msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
+            msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
         }
     }
 
-    if( p_sys->i_length )
-    {
-        p_input->stream.i_mux_rate =
-            stream_Size( p_input->s ) / 50 / p_sys->i_length;
-    }
-
     if( p_sys->b_seekable )
     {
         /* we have read all chunk so go back to movi */
-        stream_Seek( p_input->s, p_movi->i_chunk_pos );
+        stream_Seek( p_demux->s, p_movi->i_chunk_pos );
     }
     /* Skip movi header */
-    stream_Read( p_input->s, NULL, 12 );
+    stream_Read( p_demux->s, NULL, 12 );
 
     p_sys->i_movi_begin = p_movi->i_chunk_pos;
     return VLC_SUCCESS;
 
 error:
-    AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
+    if( p_sys->meta )
+    {
+        vlc_meta_Delete( p_sys->meta );
+    }
+    AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
     free( p_sys );
     return VLC_EGENERIC;
 }
@@ -416,20 +754,24 @@ error:
  *****************************************************************************/
 static void Close ( vlc_object_t * p_this )
 {
-    input_thread_t *    p_input = (input_thread_t *)p_this;
+    demux_t *    p_demux = (demux_t *)p_this;
     unsigned int i;
-    demux_sys_t *p_sys = p_input->p_demux_data  ;
+    demux_sys_t *p_sys = p_demux->p_sys  ;
 
     for( i = 0; i < p_sys->i_track; i++ )
     {
         if( p_sys->track[i] )
         {
-            FREE( p_sys->track[i]->p_index );
+            if( p_sys->track[i]->p_out_muxed )
+                stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
+            free( p_sys->track[i]->p_index );
+            free( p_sys->track[i]->p_extra );
             free( p_sys->track[i] );
         }
     }
-    FREE( p_sys->track );
-    AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
+    free( p_sys->track );
+    AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
+    vlc_meta_Delete( p_sys->meta );
 
     free( p_sys );
 }
@@ -443,7 +785,7 @@ static void Close ( vlc_object_t * p_this )
  *****************************************************************************/
 typedef struct
 {
-    vlc_bool_t b_ok;
+    bool b_ok;
 
     int i_toread;
 
@@ -452,13 +794,13 @@ typedef struct
                    else : point on data directly */
 } avi_track_toread_t;
 
-static int Demux_Seekable( input_thread_t *p_input )
+static int Demux_Seekable( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
 
     unsigned int i_track_count = 0;
     unsigned int i_track;
-    vlc_bool_t b_stream;
+    bool b_stream;
     /* cannot be more than 100 stream (dcXX or wbXX) */
     avi_track_toread_t toread[100];
 
@@ -467,20 +809,27 @@ static int Demux_Seekable( input_thread_t *p_input )
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
     {
         avi_track_t *tk = p_sys->track[i_track];
-        vlc_bool_t  b;
+        bool  b;
 
-        es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
+        if( p_sys->b_muxed && tk->p_out_muxed )
+        {
+            i_track_count++;
+            tk->b_activated = true;
+            continue;
+        }
+
+        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
         if( b && !tk->b_activated )
         {
             if( p_sys->b_seekable)
             {
-                AVI_TrackSeek( p_input, i_track, p_sys->i_time );
+                AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
             }
-            tk->b_activated = VLC_TRUE;
+            tk->b_activated = true;
         }
         else if( !b && tk->b_activated )
         {
-            tk->b_activated = VLC_FALSE;
+            tk->b_activated = false;
         }
         if( b )
         {
@@ -490,18 +839,21 @@ static int Demux_Seekable( input_thread_t *p_input )
 
     if( i_track_count <= 0 )
     {
-        msg_Warn( p_input, "no track selected, exiting..." );
-        return( 0 );
+        int64_t i_length = p_sys->i_length * (mtime_t)1000000;
+
+        p_sys->i_time += 25*1000;  /* read 25ms */
+        if( i_length > 0 )
+        {
+            if( p_sys->i_time >= i_length )
+                return 0;
+            return 1;
+        }
+        msg_Warn( p_demux, "no track selected, exiting..." );
+        return 0;
     }
 
     /* wait for the good time */
-    p_sys->i_pcr = p_sys->i_time * 9 / 100;
-
-    input_ClockManageRef( p_input,
-                          p_input->stream.p_selected_program,
-                          p_sys->i_pcr );
-
-
+    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
     p_sys->i_time += 25*1000;  /* read 25ms */
 
     /* init toread */
@@ -541,19 +893,19 @@ static int Demux_Seekable( input_thread_t *p_input )
         }
     }
 
-    b_stream = VLC_FALSE;
+    b_stream = false;
 
     for( ;; )
     {
         avi_track_t     *tk;
-        vlc_bool_t       b_done;
+        bool       b_done;
         block_t         *p_frame;
         off_t i_pos;
         unsigned int i;
         size_t i_size;
 
         /* search for first chunk to be read */
-        for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
+        for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
         {
             if( !toread[i].b_ok ||
                 AVI_GetDPTS( p_sys->track[i],
@@ -564,11 +916,11 @@ static int Demux_Seekable( input_thread_t *p_input )
 
             if( toread[i].i_toread > 0 )
             {
-                b_done = VLC_FALSE; /* not yet finished */
+                b_done = false; /* not yet finished */
             }
             if( toread[i].i_posf > 0 )
             {
-                if( i_pos == -1 || i_pos > toread[i_track].i_posf )
+                if( i_pos == -1 || i_pos > toread[i].i_posf )
                 {
                     i_track = i;
                     i_pos = toread[i].i_posf;
@@ -583,46 +935,61 @@ static int Demux_Seekable( input_thread_t *p_input )
 
         if( i_pos == -1 )
         {
+            int i_loop_count = 0;
+
             /* no valid index, we will parse directly the stream
              * in case we fail we will disable all finished stream */
             if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
             {
-                stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
-                if( AVI_PacketNext( p_input ) )
+                stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
+                if( AVI_PacketNext( p_demux ) )
                 {
-                    return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
+                    return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
                 }
             }
             else
             {
-                stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
+                stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
             }
 
             for( ;; )
             {
                 avi_packet_t avi_pk;
 
-                if( AVI_PacketGetHeader( p_input, &avi_pk ) )
+                if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
                 {
-                    msg_Warn( p_input,
+                    msg_Warn( p_demux,
                              "cannot get packet header, track disabled" );
-                    return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
+                    return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
                 }
                 if( avi_pk.i_stream >= p_sys->i_track ||
                     ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
                 {
-                    if( AVI_PacketNext( p_input ) )
+                    if( AVI_PacketNext( p_demux ) )
                     {
-                        msg_Warn( p_input,
+                        msg_Warn( p_demux,
                                   "cannot skip packet, track disabled" );
-                        return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
+                        return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
+                    }
+
+                    /* Prevents from eating all the CPU with broken files.
+                     * This value should be low enough so that it doesn't
+                     * affect the reading speed too much. */
+                    if( !(++i_loop_count % 1024) )
+                    {
+                        if( p_demux->b_die ) return -1;
+                        msleep( 10000 );
+
+                        if( !(i_loop_count % (1024 * 10)) )
+                            msg_Warn( p_demux,
+                                      "don't seem to find any data..." );
                     }
                     continue;
                 }
                 else
                 {
                     /* add this chunk to the index */
-                    AVIIndexEntry_t index;
+                    avi_entry_t index;
 
                     index.i_id = avi_pk.i_fourcc;
                     index.i_flags =
@@ -641,11 +1008,11 @@ static int Demux_Seekable( input_thread_t *p_input )
                     }
                     else
                     {
-                        if( AVI_PacketNext( p_input ) )
+                        if( AVI_PacketNext( p_demux ) )
                         {
-                            msg_Warn( p_input,
+                            msg_Warn( p_demux,
                                       "cannot skip packet, track disabled" );
-                            return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
+                            return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
                         }
                     }
                 }
@@ -654,7 +1021,7 @@ static int Demux_Seekable( input_thread_t *p_input )
         }
         else
         {
-            stream_Seek( p_input->s, i_pos );
+            stream_Seek( p_demux->s, i_pos );
         }
 
         /* Set the track to use */
@@ -673,7 +1040,8 @@ static int Demux_Seekable( input_thread_t *p_input )
                 }
                 else
                 {
-                    i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
+                    i_toread = AVI_PTSToByte( tk, 20 * 1000 );
+                    i_toread = __MAX( i_toread, 100 );
                 }
             }
             i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
@@ -690,11 +1058,11 @@ static int Demux_Seekable( input_thread_t *p_input )
             i_size += 8; /* need to read and skip header */
         }
 
-        if( ( p_frame = stream_Block( p_input->s, __EVEN( i_size ) ) )==NULL )
+        if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
         {
-            msg_Warn( p_input, "failled reading data" );
-            tk->b_activated = VLC_FALSE;
-            toread[i_track].b_ok = VLC_FALSE;
+            msg_Warn( p_demux, "failed reading data" );
+            tk->b_activated = false;
+            toread[i_track].b_ok = false;
             continue;
         }
         if( i_size % 2 )    /* read was padded on word boundary */
@@ -707,7 +1075,15 @@ static int Demux_Seekable( input_thread_t *p_input )
             p_frame->p_buffer += 8;
             p_frame->i_buffer -= 8;
         }
-        p_frame->i_pts = AVI_GetPTS( tk );
+        p_frame->i_pts = AVI_GetPTS( tk ) + 1;
+        if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
+        {
+            p_frame->i_flags = BLOCK_FLAG_TYPE_I;
+        }
+        else
+        {
+            p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
+        }
 
         /* read data */
         if( tk->i_samplesize )
@@ -727,8 +1103,14 @@ static int Demux_Seekable( input_thread_t *p_input )
         }
         else
         {
-            toread[i_track].i_toread--;
+            int i_length = tk->p_index[tk->i_idxposc].i_length;
+
             tk->i_idxposc++;
+            if( tk->i_cat == AUDIO_ES )
+            {
+                tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
+            }
+            toread[i_track].i_toread--;
         }
 
         if( tk->i_idxposc < tk->i_idxnb)
@@ -746,12 +1128,7 @@ static int Demux_Seekable( input_thread_t *p_input )
             toread[i_track].i_posf = -1;
         }
 
-        b_stream = VLC_TRUE; /* at least one read succeed */
-
-        p_frame->i_pts =
-            input_ClockGetTS( p_input,
-                              p_input->stream.p_selected_program,
-                              p_frame->i_pts * 9/100);
+        b_stream = true; /* at least one read succeed */
 
         if( tk->i_cat != VIDEO_ES )
             p_frame->i_dts = p_frame->i_pts;
@@ -761,8 +1138,11 @@ static int Demux_Seekable( input_thread_t *p_input )
             p_frame->i_pts = 0;
         }
 
-        //p_pes->i_rate = p_input->stream.control.i_rate;
-        es_out_Send( p_input->p_es_out, tk->p_es, p_frame );
+        //p_pes->i_rate = p_demux->stream.control.i_rate;
+        if( tk->p_out_muxed )
+            stream_DemuxSend( tk->p_out_muxed, p_frame );
+        else
+            es_out_Send( p_demux->out, tk->p_es, p_frame );
     }
 }
 
@@ -772,29 +1152,29 @@ static int Demux_Seekable( input_thread_t *p_input )
  *****************************************************************************
  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
  *****************************************************************************/
-static int Demux_UnSeekable( input_thread_t *p_input )
+static int Demux_UnSeekable( demux_t *p_demux )
 {
-    demux_sys_t     *p_sys = p_input->p_demux_data;
+    demux_sys_t     *p_sys = p_demux->p_sys;
     avi_track_t *p_stream_master = NULL;
-    vlc_bool_t b_audio;
     unsigned int i_stream;
     unsigned int i_packet;
 
-    /* Check if we need to send the audio data to decoder */
-    b_audio = !p_input->stream.control.b_mute;
+    if( p_sys->b_muxed )
+    {
+        msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
+        return VLC_EGENERIC;
+    }
 
-    input_ClockManageRef( p_input,
-                          p_input->stream.p_selected_program,
-                          p_sys->i_pcr );
+    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
 
     /* *** find master stream for data packet skipping algo *** */
     /* *** -> first video, if any, or first audio ES *** */
     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
     {
         avi_track_t *tk = p_sys->track[i_stream];
-        vlc_bool_t  b;
+        bool  b;
 
-        es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
+        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
 
         if( b && tk->i_cat == VIDEO_ES )
         {
@@ -808,11 +1188,11 @@ static int Demux_UnSeekable( input_thread_t *p_input )
 
     if( !p_stream_master )
     {
-        msg_Warn( p_input, "no more stream selected" );
+        msg_Warn( p_demux, "no more stream selected" );
         return( 0 );
     }
 
-    p_sys->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
+    p_sys->i_time = AVI_GetPTS( p_stream_master );
 
     for( i_packet = 0; i_packet < 10; i_packet++)
     {
@@ -820,7 +1200,7 @@ static int Demux_UnSeekable( input_thread_t *p_input )
 
         avi_packet_t    avi_pk;
 
-        if( AVI_PacketGetHeader( p_input, &avi_pk ) )
+        if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
         {
             return( 0 );
         }
@@ -837,67 +1217,53 @@ static int Demux_UnSeekable( input_thread_t *p_input )
                 case AVIFOURCC_JUNK:
                 case AVIFOURCC_LIST:
                 case AVIFOURCC_RIFF:
-                    return( !AVI_PacketNext( p_input ) ? 1 : 0 );
+                    return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
                 case AVIFOURCC_idx1:
                     if( p_sys->b_odml )
                     {
-                        return( !AVI_PacketNext( p_input ) ? 1 : 0 );
+                        return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
                     }
                     return( 0 );    /* eof */
                 default:
-                    msg_Warn( p_input,
+                    msg_Warn( p_demux,
                               "seems to have lost position, resync" );
-                    if( AVI_PacketSearch( p_input ) )
+                    if( AVI_PacketSearch( p_demux ) )
                     {
-                        msg_Err( p_input, "resync failed" );
+                        msg_Err( p_demux, "resync failed" );
                         return( -1 );
                     }
             }
         }
         else
         {
-            /* do will send this packet to decoder ? */
-            if( !b_audio && avi_pk.i_cat == AUDIO_ES )
+            /* check for time */
+            if( __ABS( AVI_GetPTS( p_stream ) -
+                        AVI_GetPTS( p_stream_master ) )< 600*1000 )
             {
-                if( AVI_PacketNext( p_input ) )
+                /* load it and send to decoder */
+                block_t *p_frame;
+                if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
                 {
-                    return( 0 );
+                    return( -1 );
                 }
+                p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
+
+                if( avi_pk.i_cat != VIDEO_ES )
+                    p_frame->i_dts = p_frame->i_pts;
+                else
+                {
+                    p_frame->i_dts = p_frame->i_pts;
+                    p_frame->i_pts = 0;
+                }
+
+                //p_pes->i_rate = p_demux->stream.control.i_rate;
+                es_out_Send( p_demux->out, p_stream->p_es, p_frame );
             }
             else
             {
-                /* it's a selected stream, check for time */
-                if( __ABS( AVI_GetPTS( p_stream ) -
-                            AVI_GetPTS( p_stream_master ) )< 600*1000 )
+                if( AVI_PacketNext( p_demux ) )
                 {
-                    /* load it and send to decoder */
-                    block_t *p_frame;
-                    if( AVI_PacketRead( p_input, &avi_pk, &p_frame ) || p_frame == NULL )
-                    {
-                        return( -1 );
-                    }
-                    p_frame->i_pts =
-                        input_ClockGetTS( p_input,
-                                          p_input->stream.p_selected_program,
-                                          AVI_GetPTS( p_stream ) * 9/100);
-
-                    if( avi_pk.i_cat != VIDEO_ES )
-                        p_frame->i_dts = p_frame->i_pts;
-                    else
-                    {
-                        p_frame->i_dts = p_frame->i_pts;
-                        p_frame->i_pts = 0;
-                    }
-
-                    //p_pes->i_rate = p_input->stream.control.i_rate;
-                    es_out_Send( p_input->p_es_out, p_stream->p_es, p_frame );
-                }
-                else
-                {
-                    if( AVI_PacketNext( p_input ) )
-                    {
-                        return( 0 );
-                    }
+                    return( 0 );
                 }
             }
 
@@ -908,6 +1274,10 @@ static int Demux_UnSeekable( input_thread_t *p_input )
             }
             else
             {
+                if( p_stream->i_cat == AUDIO_ES )
+                {
+                    p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
+                }
                 p_stream->i_idxposc++;
             }
 
@@ -920,18 +1290,14 @@ static int Demux_UnSeekable( input_thread_t *p_input )
 
 /*****************************************************************************
  * Seek: goto to i_date or i_percent
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
  *****************************************************************************/
-static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
+static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
 {
 
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     unsigned int i_stream;
-    msg_Dbg( p_input,
-             "seek requested: "I64Fd" secondes %d%%",
-             i_date / 1000000,
-             i_percent );
+    msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
+             i_date / 1000000, i_percent );
 
     if( p_sys->b_seekable )
     {
@@ -941,19 +1307,17 @@ static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
             int64_t i_pos;
 
             /* use i_percent to create a true i_date */
-            msg_Warn( p_input,
-                      "mmh, seeking without index at %d%%"
-                      " work only for interleaved file", i_percent );
+            msg_Warn( p_demux, "seeking without index at %d%%"
+                      " only works for interleaved files", i_percent );
             if( i_percent >= 100 )
             {
-                msg_Warn( p_input, "cannot seek so far !" );
-                return( -1 );
+                msg_Warn( p_demux, "cannot seek so far !" );
+                return VLC_EGENERIC;
             }
             i_percent = __MAX( i_percent, 0 );
 
             /* try to find chunk that is at i_percent or the file */
-            i_pos = __MAX( i_percent *
-                           stream_Size( p_input->s ) / 100,
+            i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
                            p_sys->i_movi_begin );
             /* search first selected stream */
             for( i_stream = 0, p_stream = NULL;
@@ -967,93 +1331,67 @@ static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
             }
             if( !p_stream || !p_stream->b_activated )
             {
-                msg_Warn( p_input, "cannot find any selected stream" );
-                return( -1 );
+                msg_Warn( p_demux, "cannot find any selected stream" );
+                return VLC_EGENERIC;
             }
 
             /* be sure that the index exist */
-            if( AVI_StreamChunkSet( p_input,
-                                    i_stream,
-                                    0 ) )
+            if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
             {
-                msg_Warn( p_input, "cannot seek" );
-                return( -1 );
+                msg_Warn( p_demux, "cannot seek" );
+                return VLC_EGENERIC;
             }
 
             while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
                p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
             {
                 /* search after i_idxposc */
-                if( AVI_StreamChunkSet( p_input,
+                if( AVI_StreamChunkSet( p_demux,
                                         i_stream, p_stream->i_idxposc + 1 ) )
                 {
-                    msg_Warn( p_input, "cannot seek" );
-                    return( -1 );
+                    msg_Warn( p_demux, "cannot seek" );
+                    return VLC_EGENERIC;
                 }
             }
+
             i_date = AVI_GetPTS( p_stream );
             /* TODO better support for i_samplesize != 0 */
-            msg_Dbg( p_input, "estimate date "I64Fd, i_date );
+            msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
         }
 
-#define p_stream    p_sys->track[i_stream]
-        p_sys->i_time = 0;
-        /* seek for chunk based streams */
-        for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
-        {
-            if( p_stream->b_activated && !p_stream->i_samplesize )
-/*            if( p_stream->b_activated ) */
-            {
-                AVI_TrackSeek( p_input, i_stream, i_date );
-                p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
-                                        p_sys->i_time );
-            }
-        }
-#if 1
-        if( p_sys->i_time )
-        {
-            i_date = p_sys->i_time;
-        }
-        /* seek for bytes based streams */
+        /* */
         for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
         {
-            if( p_stream->b_activated && p_stream->i_samplesize )
-            {
-                AVI_TrackSeek( p_input, i_stream, i_date );
-/*                p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
-            }
-        }
-        msg_Dbg( p_input, "seek: "I64Fd" secondes", p_sys->i_time /1000000 );
-        /* set true movie time */
-#endif
-        if( !p_sys->i_time )
-        {
-            p_sys->i_time = i_date;
+            avi_track_t *p_stream = p_sys->track[i_stream];
+
+            if( !p_stream->b_activated )
+                continue;
+
+            AVI_TrackSeek( p_demux, i_stream, i_date );
         }
-#undef p_stream
-        return( 1 );
+        p_sys->i_time = i_date;
+        msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
+        return VLC_SUCCESS;
     }
     else
     {
-        msg_Err( p_input, "shouldn't yet be executed" );
-        return( -1 );
+        msg_Err( p_demux, "shouldn't yet be executed" );
+        return VLC_EGENERIC;
     }
 }
 
 /*****************************************************************************
  * Control:
- *****************************************************************************
- *
  *****************************************************************************/
-static double ControlGetPosition( input_thread_t *p_input )
+static double ControlGetPosition( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
 
     if( p_sys->i_length > 0 )
     {
         return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
     }
-    else if( stream_Size( p_input->s ) > 0 )
+    else if( stream_Size( p_demux->s ) > 0 )
     {
         unsigned int i;
         int64_t i_tmp;
@@ -1073,32 +1411,37 @@ static double ControlGetPosition( input_thread_t *p_input )
                 }
             }
         }
-        return (double)i64 / (double)stream_Size( p_input->s );
+        return (double)i64 / stream_Size( p_demux->s );
     }
     return 0.0;
 }
 
-static int    Control( input_thread_t *p_input, int i_query, va_list args )
+static int Control( demux_t *p_demux, int i_query, va_list args )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     int i;
     double   f, *pf;
     int64_t i64, *pi64;
+    vlc_meta_t *p_meta;
 
     switch( i_query )
     {
         case DEMUX_GET_POSITION:
             pf = (double*)va_arg( args, double * );
-            *pf = ControlGetPosition( p_input );
+            *pf = ControlGetPosition( p_demux );
             return VLC_SUCCESS;
         case DEMUX_SET_POSITION:
+            f = (double)va_arg( args, double );
             if( p_sys->b_seekable )
             {
-                f = (double)va_arg( args, double );
                 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
-                return Seek( p_input, i64, (int)(f * 100) ) < 0 ? VLC_EGENERIC : VLC_SUCCESS;
+                return Seek( p_demux, i64, (int)(f * 100) );
+            }
+            else
+            {
+                int64_t i_pos = stream_Size( p_demux->s ) * f;
+                return stream_Seek( p_demux->s, i_pos );
             }
-            return demux_vaControlDefault( p_input, i_query, args );
 
         case DEMUX_GET_TIME:
             pi64 = (int64_t*)va_arg( args, int64_t * );
@@ -1116,10 +1459,10 @@ static int    Control( input_thread_t *p_input, int i_query, va_list args )
             }
             else if( p_sys->i_time > 0 )
             {
-                i_percent = (int)( 100.0 * ControlGetPosition( p_input ) *
+                i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
                                    (double)i64 / (double)p_sys->i_time );
             }
-            return Seek( p_input, i64, i_percent );
+            return Seek( p_demux, i64, i_percent );
         }
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
@@ -1139,11 +1482,14 @@ static int    Control( input_thread_t *p_input, int i_query, va_list args )
                 }
             }
             return VLC_SUCCESS;
+        case DEMUX_GET_META:
+            p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
+            vlc_meta_Merge( p_meta,  p_sys->meta );
+            return VLC_SUCCESS;
 
         default:
-            return demux_vaControlDefault( p_input, i_query, args );
+            return VLC_EGENERIC;
     }
-    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -1152,6 +1498,9 @@ static int    Control( input_thread_t *p_input, int i_query, va_list args )
 
 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
 {
+    if( !tk->i_scale )
+        return (mtime_t)0;
+
     return (mtime_t)((int64_t)i_pts *
                      (int64_t)tk->i_rate /
                      (int64_t)tk->i_scale /
@@ -1159,6 +1508,9 @@ static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
 }
 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
 {
+    if( !tk->i_scale || !tk->i_samplesize )
+        return (mtime_t)0;
+
     return (mtime_t)((int64_t)i_pts *
                      (int64_t)tk->i_rate /
                      (int64_t)tk->i_scale /
@@ -1168,7 +1520,10 @@ static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
 
 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
 {
-    mtime_t i_dpts;
+    mtime_t i_dpts = 0;
+
+    if( !tk->i_rate )
+        return i_dpts;
 
     i_dpts = (mtime_t)( (int64_t)1000000 *
                         (int64_t)i_count *
@@ -1206,55 +1561,71 @@ static mtime_t AVI_GetPTS( avi_track_t *tk )
     }
     else
     {
-        return AVI_GetDPTS( tk, tk->i_idxposc );
+        if( tk->i_cat == AUDIO_ES )
+        {
+            return AVI_GetDPTS( tk, tk->i_blockno );
+        }
+        else
+        {
+            return AVI_GetDPTS( tk, tk->i_idxposc );
+        }
     }
 }
 
-static int AVI_StreamChunkFind( input_thread_t *p_input,
-                                unsigned int i_stream )
+static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     avi_packet_t avi_pk;
+    int i_loop_count = 0;
 
     /* find first chunk of i_stream that isn't in index */
 
     if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
     {
-        stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
-        if( AVI_PacketNext( p_input ) )
+        stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
+        if( AVI_PacketNext( p_demux ) )
         {
             return VLC_EGENERIC;
         }
     }
     else
     {
-        stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
+        stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
     }
 
     for( ;; )
     {
-        if( p_input->b_die )
-        {
-            return VLC_EGENERIC;
-        }
+        if( p_demux->b_die ) return VLC_EGENERIC;
 
-        if( AVI_PacketGetHeader( p_input, &avi_pk ) )
+        if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
         {
-            msg_Warn( p_input, "cannot get packet header" );
+            msg_Warn( p_demux, "cannot get packet header" );
             return VLC_EGENERIC;
         }
         if( avi_pk.i_stream >= p_sys->i_track ||
             ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
         {
-            if( AVI_PacketNext( p_input ) )
+            if( AVI_PacketNext( p_demux ) )
             {
                 return VLC_EGENERIC;
             }
+
+            /* Prevents from eating all the CPU with broken files.
+             * This value should be low enough so that it doesn't
+             * affect the reading speed too much. */
+            if( !(++i_loop_count % 1024) )
+            {
+                if( p_demux->b_die ) return VLC_EGENERIC;
+                msleep( 10000 );
+
+                if( !(i_loop_count % (1024 * 10)) )
+                    msg_Warn( p_demux, "don't seem to find any data..." );
+            }
         }
         else
         {
             /* add this chunk to the index */
-            AVIIndexEntry_t index;
+            avi_entry_t index;
 
             index.i_id = avi_pk.i_fourcc;
             index.i_flags =
@@ -1269,7 +1640,7 @@ static int AVI_StreamChunkFind( input_thread_t *p_input,
                 return VLC_SUCCESS;
             }
 
-            if( AVI_PacketNext( p_input ) )
+            if( AVI_PacketNext( p_demux ) )
             {
                 return VLC_EGENERIC;
             }
@@ -1277,13 +1648,11 @@ static int AVI_StreamChunkFind( input_thread_t *p_input,
     }
 }
 
-
 /* be sure that i_ck will be a valid index entry */
-static int AVI_StreamChunkSet( input_thread_t    *p_input,
-                               unsigned int i_stream,
+static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
                                unsigned int i_ck )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     avi_track_t *p_stream = p_sys->track[i_stream];
 
     p_stream->i_idxposc = i_ck;
@@ -1295,7 +1664,7 @@ static int AVI_StreamChunkSet( input_thread_t    *p_input,
         do
         {
             p_stream->i_idxposc++;
-            if( AVI_StreamChunkFind( p_input, i_stream ) )
+            if( AVI_StreamChunkFind( p_demux, i_stream ) )
             {
                 return VLC_EGENERIC;
             }
@@ -1306,13 +1675,12 @@ static int AVI_StreamChunkSet( input_thread_t    *p_input,
     return VLC_SUCCESS;
 }
 
-
 /* XXX FIXME up to now, we assume that all chunk are one after one */
-static int AVI_StreamBytesSet( input_thread_t    *p_input,
+static int AVI_StreamBytesSet( demux_t    *p_demux,
                                unsigned int i_stream,
                                off_t   i_byte )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     avi_track_t *p_stream = p_sys->track[i_stream];
 
     if( ( p_stream->i_idxnb > 0 )
@@ -1357,7 +1725,7 @@ static int AVI_StreamBytesSet( input_thread_t    *p_input,
         do
         {
             p_stream->i_idxposc++;
-            if( AVI_StreamChunkFind( p_input, i_stream ) )
+            if( AVI_StreamChunkFind( p_demux, i_stream ) )
             {
                 return VLC_EGENERIC;
             }
@@ -1371,11 +1739,13 @@ static int AVI_StreamBytesSet( input_thread_t    *p_input,
     }
 }
 
-static int AVI_TrackSeek( input_thread_t *p_input,
+static int AVI_TrackSeek( demux_t *p_demux,
                            int i_stream,
                            mtime_t i_date )
 {
-    demux_sys_t  *p_sys = p_input->p_demux_data;
+    demux_sys_t  *p_sys = p_demux->p_sys;
+    avi_track_t  *tk = p_sys->track[i_stream];
+
 #define p_stream    p_sys->track[i_stream]
     mtime_t i_oldpts;
 
@@ -1383,15 +1753,32 @@ static int AVI_TrackSeek( input_thread_t *p_input,
 
     if( !p_stream->i_samplesize )
     {
-        if( AVI_StreamChunkSet( p_input,
+        if( AVI_StreamChunkSet( p_demux,
                                 i_stream,
                                 AVI_PTSToChunk( p_stream, i_date ) ) )
         {
             return VLC_EGENERIC;
         }
 
-        msg_Dbg( p_input,
-                 "old:"I64Fd" %s new "I64Fd,
+        if( p_stream->i_cat == AUDIO_ES )
+        {
+            unsigned int i;
+            tk->i_blockno = 0;
+            for( i = 0; i < tk->i_idxposc; i++ )
+            {
+                if( tk->i_blocksize > 0 )
+                {
+                    tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
+                }
+                else
+                {
+                    tk->i_blockno++;
+                }
+            }
+        }
+
+        msg_Dbg( p_demux,
+                 "old:%"PRId64" %s new %"PRId64,
                  i_oldpts,
                  i_oldpts > i_date ? ">" : "<",
                  i_date );
@@ -1399,27 +1786,30 @@ static int AVI_TrackSeek( input_thread_t *p_input,
         if( p_stream->i_cat == VIDEO_ES )
         {
             /* search key frame */
-            if( i_date < i_oldpts )
+            //if( i_date < i_oldpts || 1 )
             {
                 while( p_stream->i_idxposc > 0 &&
                    !( p_stream->p_index[p_stream->i_idxposc].i_flags &
                                                                 AVIIF_KEYFRAME ) )
                 {
-                    if( AVI_StreamChunkSet( p_input,
+                    if( AVI_StreamChunkSet( p_demux,
                                             i_stream,
                                             p_stream->i_idxposc - 1 ) )
                     {
                         return VLC_EGENERIC;
                     }
                 }
+                if( p_stream->p_es )
+                    es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_stream->p_es, i_date );
             }
+#if 0
             else
             {
                 while( p_stream->i_idxposc < p_stream->i_idxnb &&
                         !( p_stream->p_index[p_stream->i_idxposc].i_flags &
                                                                 AVIIF_KEYFRAME ) )
                 {
-                    if( AVI_StreamChunkSet( p_input,
+                    if( AVI_StreamChunkSet( p_demux,
                                             i_stream,
                                             p_stream->i_idxposc + 1 ) )
                     {
@@ -1427,11 +1817,12 @@ static int AVI_TrackSeek( input_thread_t *p_input,
                     }
                 }
             }
+#endif
         }
     }
     else
     {
-        if( AVI_StreamBytesSet( p_input,
+        if( AVI_StreamBytesSet( p_demux,
                                 i_stream,
                                 AVI_PTSToByte( p_stream, i_date ) ) )
         {
@@ -1443,7 +1834,7 @@ static int AVI_TrackSeek( input_thread_t *p_input,
 }
 
 /****************************************************************************
- * Return VLC_TRUE if it's a key frame
+ * Return true if it's a key frame
  ****************************************************************************/
 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
 {
@@ -1460,10 +1851,8 @@ static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
                 /* it's not an msmpegv1 stream, strange...*/
                 return AVIIF_KEYFRAME;
             }
-            else
-            {
-                return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
-            }
+            return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
+
         case FOURCC_DIV2:
         case FOURCC_DIV3:   /* wmv1 also */
             /* we have
@@ -1471,7 +1860,7 @@ static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
              */
             return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
         case FOURCC_mp4v:
-            /* we should find first occurence of 0x000001b6 (32bits)
+            /* we should find first occurrence of 0x000001b6 (32bits)
              *  startcode:      0x000001b6   32bits
              *  piture type     0(I),1(P)     2bits
              */
@@ -1480,10 +1869,8 @@ static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
                 /* not true , need to find the first VOP header */
                 return AVIIF_KEYFRAME;
             }
-            else
-            {
-                return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
-            }
+            return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
+
         default:
             /* I can't do it, so say yes */
             return AVIIF_KEYFRAME;
@@ -1502,6 +1889,8 @@ vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
             /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
             switch( i_codec )
             {
+                case FOURCC_1:
+                    return VLC_FOURCC('m','r','l','e');
                 case FOURCC_DIV1:
                 case FOURCC_div1:
                 case FOURCC_MPG4:
@@ -1558,7 +1947,7 @@ vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
  *
  ****************************************************************************/
 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
-                                   int *pi_number, int *pi_type )
+                                   unsigned int *pi_number, unsigned int *pi_type )
 {
 #define SET_PTR( p, v ) if( p ) *(p) = (v);
     int c1, c2;
@@ -1594,17 +1983,17 @@ static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
 /****************************************************************************
  *
  ****************************************************************************/
-static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
+static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
 {
-    uint8_t  *p_peek;
+    const uint8_t *p_peek;
 
-    if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
+    if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
     {
         return VLC_EGENERIC;
     }
     p_pk->i_fourcc  = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
     p_pk->i_size    = GetDWLE( p_peek + 4 );
-    p_pk->i_pos     = stream_Tell( p_input->s );
+    p_pk->i_pos     = stream_Tell( p_demux->s );
     if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
     {
         p_pk->i_type = VLC_FOURCC( p_peek[8],  p_peek[9],
@@ -1621,12 +2010,12 @@ static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
     return VLC_SUCCESS;
 }
 
-static int AVI_PacketNext( input_thread_t *p_input )
+static int AVI_PacketNext( demux_t *p_demux )
 {
     avi_packet_t    avi_ck;
     int             i_skip = 0;
 
-    if( AVI_PacketGetHeader( p_input, &avi_ck ) )
+    if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
     {
         return VLC_EGENERIC;
     }
@@ -1646,13 +2035,14 @@ static int AVI_PacketNext( input_thread_t *p_input )
         i_skip = __EVEN( avi_ck.i_size ) + 8;
     }
 
-    if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
+    if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
     {
         return VLC_EGENERIC;
     }
     return VLC_SUCCESS;
 }
-static int AVI_PacketRead( input_thread_t   *p_input,
+
+static int AVI_PacketRead( demux_t   *p_demux,
                            avi_packet_t     *p_pk,
                            block_t          **pp_frame )
 {
@@ -1660,7 +2050,7 @@ static int AVI_PacketRead( input_thread_t   *p_input,
 
     i_size = __EVEN( p_pk->i_size + 8 );
 
-    if( ( *pp_frame = stream_Block( p_input->s, i_size ) ) == NULL )
+    if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
     {
         return VLC_EGENERIC;
     }
@@ -1675,19 +2065,19 @@ static int AVI_PacketRead( input_thread_t   *p_input,
     return VLC_SUCCESS;
 }
 
-static int AVI_PacketSearch( input_thread_t *p_input )
+static int AVI_PacketSearch( demux_t *p_demux )
 {
-    demux_sys_t     *p_sys = p_input->p_demux_data;
+    demux_sys_t     *p_sys = p_demux->p_sys;
     avi_packet_t    avi_pk;
     int             i_count = 0;
 
     for( ;; )
     {
-        if( stream_Read( p_input->s, NULL, 1 ) != 1 )
+        if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
         {
             return VLC_EGENERIC;
         }
-        AVI_PacketGetHeader( p_input, &avi_pk );
+        AVI_PacketGetHeader( p_demux, &avi_pk );
         if( avi_pk.i_stream < p_sys->i_track &&
             ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
         {
@@ -1708,11 +2098,11 @@ static int AVI_PacketSearch( input_thread_t *p_input )
          * this code is called only on broken files). */
         if( !(++i_count % 1024) )
         {
-            if( p_input->b_die ) return VLC_EGENERIC;
+            if( p_demux->b_die ) return VLC_EGENERIC;
 
             msleep( 10000 );
             if( !(i_count % (1024 * 10)) )
-                msg_Warn( p_input, "trying to resync..." );
+                msg_Warn( p_demux, "trying to resync..." );
         }
     }
 }
@@ -1722,7 +2112,7 @@ static int AVI_PacketSearch( input_thread_t *p_input )
  ****************************************************************************/
 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
                                int i_stream,
-                               AVIIndexEntry_t *p_index)
+                               avi_entry_t *p_index)
 {
     avi_track_t *tk = p_sys->track[i_stream];
 
@@ -1737,7 +2127,7 @@ static void AVI_IndexAddEntry( demux_sys_t *p_sys,
     {
         tk->i_idxmax += 16384;
         tk->p_index = realloc( tk->p_index,
-                               tk->i_idxmax * sizeof( AVIIndexEntry_t ) );
+                               tk->i_idxmax * sizeof( avi_entry_t ) );
         if( tk->p_index == NULL )
         {
             return;
@@ -1758,9 +2148,9 @@ static void AVI_IndexAddEntry( demux_sys_t *p_sys,
     tk->p_index[tk->i_idxnb++] = *p_index;
 }
 
-static int AVI_IndexLoad_idx1( input_thread_t *p_input )
+static int AVI_IndexLoad_idx1( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
 
     avi_chunk_list_t    *p_riff;
     avi_chunk_list_t    *p_movi;
@@ -1771,18 +2161,20 @@ static int AVI_IndexLoad_idx1( input_thread_t *p_input )
     off_t        i_offset;
     unsigned int i;
 
+    bool b_keyset[100];
+
     p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
     p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
     p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
 
     if( !p_idx1 )
     {
-        msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
+        msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
         return VLC_EGENERIC;
     }
 
     /* *** calculate offset *** */
-    /* Well, avi is __SHIT__ so test more than one entry 
+    /* Well, avi is __SHIT__ so test more than one entry
      * (needed for some avi files) */
     i_offset = 0;
     for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
@@ -1794,6 +2186,10 @@ static int AVI_IndexLoad_idx1( input_thread_t *p_input )
         }
     }
 
+    /* Reset b_keyset */
+    for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
+        b_keyset[i_stream] = false;
+
     for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
     {
         unsigned int i_cat;
@@ -1804,27 +2200,42 @@ static int AVI_IndexLoad_idx1( input_thread_t *p_input )
         if( i_stream < p_sys->i_track &&
             i_cat == p_sys->track[i_stream]->i_cat )
         {
-            AVIIndexEntry_t index;
+            avi_entry_t index;
             index.i_id      = p_idx1->entry[i_index].i_fourcc;
             index.i_flags   =
                 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
             index.i_pos     = p_idx1->entry[i_index].i_pos + i_offset;
             index.i_length  = p_idx1->entry[i_index].i_length;
             AVI_IndexAddEntry( p_sys, i_stream, &index );
+
+            if( index.i_flags&AVIIF_KEYFRAME )
+                b_keyset[i_stream] = true;
+        }
+    }
+
+    for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
+    {
+        if( !b_keyset[i_stream] )
+        {
+            avi_track_t *tk = p_sys->track[i_stream];
+
+            msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
+            for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
+                tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
         }
     }
     return VLC_SUCCESS;
 }
 
-static void __Parse_indx( input_thread_t    *p_input,
+static void __Parse_indx( demux_t    *p_demux,
                           int               i_stream,
                           avi_chunk_indx_t  *p_indx )
 {
-    demux_sys_t         *p_sys    = p_input->p_demux_data;
-    AVIIndexEntry_t     index;
+    demux_sys_t         *p_sys    = p_demux->p_sys;
+    avi_entry_t     index;
     int32_t             i;
 
-    msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
+    msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
     if( p_indx->i_indexsubtype == 0 )
     {
         for( i = 0; i < p_indx->i_entriesinuse; i++ )
@@ -1851,13 +2262,13 @@ static void __Parse_indx( input_thread_t    *p_input,
     }
     else
     {
-        msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
+        msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
     }
 }
 
-static void AVI_IndexLoad_indx( input_thread_t *p_input )
+static void AVI_IndexLoad_indx( demux_t *p_demux )
 {
-    demux_sys_t         *p_sys = p_input->p_demux_data;
+    demux_sys_t         *p_sys = p_demux->p_sys;
     unsigned int        i_stream;
     int32_t             i;
 
@@ -1878,38 +2289,39 @@ static void AVI_IndexLoad_indx( input_thread_t *p_input )
 
         if( !p_indx )
         {
-            msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
+            msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
+                               "file?)" );
             continue;
         }
 
         if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
         {
-            __Parse_indx( p_input, i_stream, p_indx );
+            __Parse_indx( p_demux, i_stream, p_indx );
         }
         else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
         {
-            avi_chunk_indx_t    ck_sub;
+            avi_chunk_t    ck_sub;
             for( i = 0; i < p_indx->i_entriesinuse; i++ )
             {
-                if( stream_Seek( p_input->s, p_indx->idx.super[i].i_offset )||
-                    AVI_ChunkRead( p_input->s, &ck_sub, NULL  ) )
+                if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
+                    AVI_ChunkRead( p_demux->s, &ck_sub, NULL  ) )
                 {
                     break;
                 }
-                __Parse_indx( p_input, i_stream, &ck_sub );
+                __Parse_indx( p_demux, i_stream, &ck_sub.indx );
             }
         }
         else
         {
-            msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
+            msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
         }
 #undef p_stream
     }
 }
 
-static void AVI_IndexLoad( input_thread_t *p_input )
+static void AVI_IndexLoad( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     unsigned int i_stream;
 
     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
@@ -1921,24 +2333,24 @@ static void AVI_IndexLoad( input_thread_t *p_input )
 
     if( p_sys->b_odml )
     {
-        AVI_IndexLoad_indx( p_input );
+        AVI_IndexLoad_indx( p_demux );
     }
-    else  if( AVI_IndexLoad_idx1( p_input ) )
+    else  if( AVI_IndexLoad_idx1( p_demux ) )
     {
         /* try indx if idx1 failed as some "normal" file have indx too */
-        AVI_IndexLoad_indx( p_input );
+        AVI_IndexLoad_indx( p_demux );
     }
 
     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
     {
-        msg_Dbg( p_input, "stream[%d] created %d index entries",
+        msg_Dbg( p_demux, "stream[%d] created %d index entries",
                 i_stream, p_sys->track[i_stream]->i_idxnb );
     }
 }
 
-static void AVI_IndexCreate( input_thread_t *p_input )
+static void AVI_IndexCreate( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
 
     avi_chunk_list_t    *p_riff;
     avi_chunk_list_t    *p_movi;
@@ -1951,7 +2363,7 @@ static void AVI_IndexCreate( input_thread_t *p_input )
 
     if( !p_movi )
     {
-        msg_Err( p_input, "cannot find p_movi" );
+        msg_Err( p_demux, "cannot find p_movi" );
         return;
     }
 
@@ -1962,27 +2374,51 @@ static void AVI_IndexCreate( input_thread_t *p_input )
         p_sys->track[i_stream]->p_index  = NULL;
     }
     i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
-                        stream_Size( p_input->s ) );
+                        stream_Size( p_demux->s ) );
+
+    stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
+    msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
+
+
+    /* Only show dialog if AVI is > 10MB */
+    p_demux->p_sys->i_dialog_id = -1;
+    if( stream_Size( p_demux->s ) > 10000000 )
+    {
+        p_demux->p_sys->i_dialog_id = intf_IntfProgress( p_demux,
+                                        _( "Fixing AVI Index..." ),
+                                        0.0 );
+        p_demux->p_sys->last_update = mdate();
+    }
 
-    stream_Seek( p_input->s, p_movi->i_chunk_pos + 12 );
-    msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
     for( ;; )
     {
         avi_packet_t pk;
 
-        if( p_input->b_die )
+        if( p_demux->b_die )
         {
             return;
         }
 
-        if( AVI_PacketGetHeader( p_input, &pk ) )
+        /* Don't update dialog too often */
+        if( p_demux->p_sys->i_dialog_id > 0 &&
+            mdate() - p_demux->p_sys->last_update > 100000 )
+        {
+            int64_t i_pos = stream_Tell( p_demux->s )* 100 /
+                            stream_Size( p_demux->s );
+            float f_pos = (float)i_pos;
+            p_demux->p_sys->last_update = mdate();
+            intf_ProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
+                                 _( "Fixing AVI Index..." ), f_pos, -1 );
+        }
+
+        if( AVI_PacketGetHeader( p_demux, &pk ) )
         {
             break;
         }
         if( pk.i_stream < p_sys->i_track &&
             pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
         {
-            AVIIndexEntry_t index;
+            avi_entry_t index;
             index.i_id      = pk.i_fourcc;
             index.i_flags   =
                AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
@@ -2001,8 +2437,8 @@ static void AVI_IndexCreate( input_thread_t *p_input )
                         p_sysx = AVI_ChunkFind( &p_sys->ck_root,
                                                 AVIFOURCC_RIFF, 1 );
 
-                        msg_Dbg( p_input, "looking for new RIFF chunk" );
-                        if( stream_Seek( p_input->s, p_sysx->i_chunk_pos + 24))
+                        msg_Dbg( p_demux, "looking for new RIFF chunk" );
+                        if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
                         {
                             goto print_stat;
                         }
@@ -2010,31 +2446,36 @@ static void AVI_IndexCreate( input_thread_t *p_input )
                     }
                     goto print_stat;
                 case AVIFOURCC_RIFF:
-                        msg_Dbg( p_input, "new RIFF chunk found" );
+                        msg_Dbg( p_demux, "new RIFF chunk found" );
                 case AVIFOURCC_rec:
                 case AVIFOURCC_JUNK:
                     break;
                 default:
-                    msg_Warn( p_input, "need resync, probably broken avi" );
-                    if( AVI_PacketSearch( p_input ) )
+                    msg_Warn( p_demux, "need resync, probably broken avi" );
+                    if( AVI_PacketSearch( p_demux ) )
                     {
-                        msg_Warn( p_input, "lost sync, abord index creation" );
+                        msg_Warn( p_demux, "lost sync, abord index creation" );
                         goto print_stat;
                     }
             }
         }
 
         if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
-            AVI_PacketNext( p_input ) )
+            AVI_PacketNext( p_demux ) )
         {
             break;
         }
     }
 
 print_stat:
+    if( p_demux->p_sys->i_dialog_id > 0 )
+    {
+        intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
+    }
+
     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
     {
-        msg_Dbg( p_input,
+        msg_Dbg( p_demux,
                 "stream[%d] creating %d index entries",
                 i_stream,
                 p_sys->track[i_stream]->i_idxnb );
@@ -2044,23 +2485,23 @@ print_stat:
 /*****************************************************************************
  * Stream management
  *****************************************************************************/
-static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
+static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
     unsigned int i;
-    int b_end = VLC_TRUE;
+    int b_end = true;
 
     for( i = 0; i < p_sys->i_track; i++ )
     {
         avi_track_t *tk = p_sys->track[i];
         if( tk->i_idxposc >= tk->i_idxnb )
         {
-            tk->b_activated = VLC_FALSE;
-            es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
+            tk->b_activated = false;
+            if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, false );
         }
         else
         {
-            b_end = VLC_FALSE;
+            b_end = false;
         }
     }
     return( b_end );
@@ -2069,9 +2510,9 @@ static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
 /****************************************************************************
  * AVI_MovieGetLength give max streams length in second
  ****************************************************************************/
-static mtime_t  AVI_MovieGetLength( input_thread_t *p_input )
+static mtime_t  AVI_MovieGetLength( demux_t *p_demux )
 {
-    demux_sys_t  *p_sys = p_input->p_demux_data;
+    demux_sys_t  *p_sys = p_demux->p_sys;
     mtime_t      i_maxlength = 0;
     unsigned int i;
 
@@ -2098,8 +2539,8 @@ static mtime_t  AVI_MovieGetLength( input_thread_t *p_input )
         }
         i_length /= (mtime_t)1000000;    /* in seconds */
 
-        msg_Dbg( p_input,
-                 "stream[%d] length:"I64Fd" (based on index)",
+        msg_Dbg( p_demux,
+                 "stream[%d] length:%"PRId64" (based on index)",
                  i,
                  i_length );
         i_maxlength = __MAX( i_maxlength, i_length );
@@ -2107,5 +2548,3 @@ static mtime_t  AVI_MovieGetLength( input_thread_t *p_input )
 
     return i_maxlength;
 }
-
-