]> git.sesse.net Git - vlc/blobdiff - modules/demux/avi/avi.c
Qt: Simple prefs Input/codec ui fixes
[vlc] / modules / demux / avi / avi.c
index 225b01019f2b55c348bfc2234c90c11f27be053a..2d5de5c1defe3f479ef61edf5e7d276ccc4bda54 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * avi.c : AVI file Stream input module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2004 the VideoLAN team
+ * Copyright (C) 2001-2009 the VideoLAN team
  * $Id$
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_demux.h>
 
-#include <vlc_interface.h>
+#include <vlc_dialog.h>
 
 #include <vlc_meta.h>
 #include <vlc_codecs.h>
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-static int pi_index[] = {0,1,2};
+static const int pi_index[] = {0,1,2};
 
-static const char *ppsz_indexes[] = { N_("Ask"), N_("Always fix"),
-                                N_("Never fix") };
+static const char *const ppsz_indexes[] = { N_("Ask for action"),
+                                            N_("Always fix"),
+                                            N_("Never fix") };
 
-vlc_module_begin();
-    set_shortname( "AVI" );
-    set_description( _("AVI demuxer") );
-    set_capability( "demux", 212 );
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_DEMUX );
+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_bool( "avi-interleaved", false, 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 );
+              INDEX_TEXT, INDEX_LONGTEXT, false )
+        change_integer_list( pi_index, ppsz_indexes, NULL )
 
-    set_callbacks( Open, Close );
-vlc_module_end();
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
@@ -105,20 +107,21 @@ typedef struct
     uint32_t     i_flags;
     off_t        i_pos;
     uint32_t     i_length;
-    uint32_t     i_lengthtotal;
+    int64_t      i_lengthtotal;
 
 } avi_entry_t;
 
 typedef struct
 {
-    bool      b_activated;
+    bool            b_activated;
+    bool            b_eof;
 
     unsigned int    i_cat; /* AUDIO_ES, VIDEO_ES */
     vlc_fourcc_t    i_codec;
 
     int             i_rate;
     int             i_scale;
-    int             i_samplesize;
+    unsigned int    i_samplesize;
 
     es_out_id_t     *p_es;
 
@@ -161,10 +164,6 @@ struct demux_sys_t
 
     /* meta */
     vlc_meta_t  *meta;
-
-    /* Progress box */
-    mtime_t    last_update;
-    int        i_dialog_id;
 };
 
 static inline off_t __EVEN( off_t i )
@@ -224,8 +223,7 @@ static int Open( vlc_object_t * p_this )
     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_riff;
     avi_chunk_list_t    *p_hdrl, *p_movi;
     avi_chunk_avih_t    *p_avih;
 
@@ -342,11 +340,11 @@ static int Open( vlc_object_t * p_this )
     if( ( p_sys->meta = vlc_meta_New() ) )
     {
         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":"" );
+        snprintf( buffer, sizeof(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 );
     }
 
@@ -355,10 +353,7 @@ static int Open( vlc_object_t * p_this )
     {
         avi_track_t           *tk     = malloc( sizeof( avi_track_t ) );
         if( !tk )
-        {
-            msg_Err( p_demux, "Out of memory" );
             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 );
@@ -367,7 +362,9 @@ static int Open( vlc_object_t * p_this )
         avi_chunk_strf_vids_t *p_vids = NULL;
         es_format_t fmt;
 
-        memset( tk, 0, sizeof( avi_track_t ) );
+        memset( tk, 0, sizeof(*tk) );
+        tk->b_eof = false;
+        tk->b_activated = true;
 
         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 );
@@ -375,6 +372,7 @@ static int Open( vlc_object_t * p_this )
         if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
         {
             msg_Warn( p_demux, "stream[%d] incomplete", i );
+            free( tk );
             continue;
         }
 
@@ -390,19 +388,28 @@ 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 )
+
+                tk->i_blocksize = p_auds->p_wf->nBlockAlign;
+                if( tk->i_blocksize == 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;
-                    }
                 }
+                else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
+                {
+                    msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
+                                       "Using blocksize as a workaround.",
+                                       i, tk->i_samplesize, tk->i_blocksize );
+                    tk->i_samplesize = tk->i_blocksize;
+                }
+
+                if( tk->i_codec == VLC_CODEC_VORBIS )
+                {
+                    tk->i_blocksize = 0; /* fix vorbis VBR decoding */
+                }
+
                 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
 
                 fmt.audio.i_channels        = p_auds->p_wf->nChannels;
@@ -441,7 +448,7 @@ static int Open( vlc_object_t * p_this )
                  *  - Size of the next packet, and so on ...
                  */
 
-                if( tk->i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
+                if( tk->i_codec == VLC_CODEC_VORBIS )
                 {
                     uint8_t *p_extra = fmt.p_extra; 
                     size_t i_extra = fmt.i_extra;
@@ -496,46 +503,60 @@ static int Open( vlc_object_t * p_this )
                 tk->i_cat   = VIDEO_ES;
                 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
                                                   p_vids->p_bih->biCompression );
-                if( p_vids->p_bih->biCompression == 0x00 )
+                if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
+                {
+                   msg_Dbg( p_demux, "stream[%d] subtitles", i );
+                   es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
+                   break;
+                }
+                else if( p_vids->p_bih->biCompression == 0x00 )
                 {
                     switch( p_vids->p_bih->biBitCount )
                     {
                         case 32:
-                            tk->i_codec = VLC_FOURCC('R','V','3','2');
+                            tk->i_codec = VLC_CODEC_RGB32;
                             break;
                         case 24:
-                            tk->i_codec = VLC_FOURCC('R','V','2','4');
+                            tk->i_codec = VLC_CODEC_RGB24;
                             break;
-                        case 16:
-                            /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
-                            /* break;*/
+                        case 16: /* Yes it is RV15 */
                         case 15:
-                            tk->i_codec = VLC_FOURCC('R','V','1','5');
+                            tk->i_codec = VLC_CODEC_RGB15;
                             break;
-                        case 9:
-                            tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
+                        case 9: /* <- TODO check that */
+                            tk->i_codec = VLC_CODEC_I410;
                             break;
-                        case 8:
-                            tk->i_codec = VLC_FOURCC('Y','8','0','0');
+                        case 8: /* <- TODO check that */
+                            tk->i_codec = VLC_CODEC_GREY;
                             break;
                     }
                     es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
 
-                    if( p_vids->p_bih->biBitCount == 24 )
+                    switch( tk->i_codec )
                     {
-                        /* This is in BGR format */
-                        fmt.video.i_bmask = 0x00ff0000;
+                    case VLC_CODEC_RGB24:
+                    case VLC_CODEC_RGB32:
+                        fmt.video.i_rmask = 0x00ff0000;
                         fmt.video.i_gmask = 0x0000ff00;
-                        fmt.video.i_rmask = 0x000000ff;
+                        fmt.video.i_bmask = 0x000000ff;
+                        break;
+                    case VLC_CODEC_RGB15:
+                        fmt.video.i_rmask = 0x7c00;
+                        fmt.video.i_gmask = 0x03e0;
+                        fmt.video.i_bmask = 0x001f;
+                        break;
+                    default:
+                        break;
                     }
                 }
                 else
                 {
                     es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
-                    if( tk->i_codec == FOURCC_mp4v &&
+                    if( tk->i_codec == VLC_CODEC_MP4V &&
                         !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
                     {
-                        fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
+                        fmt.i_codec           =
+                        fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
                     }
                 }
                 tk->i_samplesize = 0;
@@ -548,10 +569,10 @@ static int Open( vlc_object_t * p_this )
                     __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_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
+                msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
                          i, (char*)&p_vids->p_bih->biCompression,
-                         p_vids->p_bih->biWidth,
-                         p_vids->p_bih->biHeight,
+                         (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 );
 
@@ -565,28 +586,29 @@ static int Open( vlc_object_t * p_this )
                 /* 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 )
+                if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
                 {
-                    int i;
+                    /* The palette is not always included in biSize */
+                    fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
+                    if( fmt.i_extra > 0 )
+                    {
+                        const uint8_t *p_pal = fmt.p_extra;
 
-                    fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
-                    fmt.video.p_palette->i_entries = 1;
+                        fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
+                        fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
 
-                    /* 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);
+                        for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
+                        {
+                            for( int j = 0; j < 4; j++ )
+                                fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
+                        }
                     }
                 }
                 break;
 
             case( AVIFOURCC_txts):
                 tk->i_cat   = SPU_ES;
-                tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
+                tk->i_codec = VLC_CODEC_SUBT;
                 msg_Dbg( p_demux, "stream[%d] subtitles", i );
                 es_format_Init( &fmt, SPU_ES, tk->i_codec );
                 break;
@@ -618,11 +640,7 @@ static int Open( vlc_object_t * p_this )
                 continue;
         }
         if( p_strn )
-        {
-            /* The charset of p_strn is undefined */
-            EnsureUTF8( p_strn->p_str );
-            fmt.psz_description = strdup( p_strn->p_str );
-        }
+            fmt.psz_description = FromLatin1( 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 );
@@ -659,27 +677,31 @@ aviindex:
                           (mtime_t)p_avih->i_microsecperframe /
                           (mtime_t)1000000 )
     {
+        if( !vlc_object_alive( p_demux) )
+            goto error;
+
         msg_Warn( p_demux, "broken or missing index, 'seek' will be "
-                           "axproximative or will have strange behaviour" );
+                           "approximative or will exhibit strange behavior" );
         if( i_do_index == 0 && !b_index )
         {
-            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 )
-            {
+            if( !p_sys->b_seekable ) {
                 b_index = true;
-                msg_Dbg( p_demux, "Fixing AVI index" );
                 goto aviindex;
             }
-            else if( i_create == DIALOG_CANCELLED )
+            switch( dialog_Question( p_demux, _("AVI Index") ,
+               _( "This AVI file is broken. Seeking will not work correctly.\n"
+                  "Do you want to try to fix it?\n\n"
+                  "This might take a long time." ),
+                  _( "Repair" ), _( "Don't repair" ), _( "Cancel") ) )
             {
-                /* Kill input */
-                vlc_object_kill( p_demux->p_parent );
-                goto error;
+                case 1:
+                    b_index = true;
+                    msg_Dbg( p_demux, "Fixing AVI index" );
+                    goto aviindex;
+                case 3:
+                    /* Kill input */
+                    vlc_object_kill( p_demux->p_parent );
+                    goto error;
             }
         }
     }
@@ -744,7 +766,7 @@ error:
     }
     AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
     free( p_sys );
-    return VLC_EGENERIC;
+    return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
 }
 
 /*****************************************************************************
@@ -761,7 +783,7 @@ static void Close ( vlc_object_t * p_this )
         if( p_sys->track[i] )
         {
             if( p_sys->track[i]->p_out_muxed )
-                stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
+                stream_Delete( 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] );
@@ -798,7 +820,6 @@ static int Demux_Seekable( demux_t *p_demux )
 
     unsigned int i_track_count = 0;
     unsigned int i_track;
-    bool b_stream;
     /* cannot be more than 100 stream (dcXX or wbXX) */
     avi_track_toread_t toread[100];
 
@@ -860,7 +881,7 @@ static int Demux_Seekable( demux_t *p_demux )
         avi_track_t *tk = p_sys->track[i_track];
         mtime_t i_dpts;
 
-        toread[i_track].b_ok = tk->b_activated;
+        toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
         if( tk->i_idxposc < tk->i_idxnb )
         {
             toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
@@ -891,8 +912,6 @@ static int Demux_Seekable( demux_t *p_demux )
         }
     }
 
-    b_stream = false;
-
     for( ;; )
     {
         avi_track_t     *tk;
@@ -928,7 +947,13 @@ static int Demux_Seekable( demux_t *p_demux )
 
         if( b_done )
         {
-            return( 1 );
+            for( i = 0; i < p_sys->i_track; i++ )
+            {
+                if( toread[i].b_ok )
+                    return 1;
+            }
+            msg_Warn( p_demux, "all tracks have failed, exiting..." );
+            return 0;
         }
 
         if( i_pos == -1 )
@@ -975,7 +1000,7 @@ static int Demux_Seekable( demux_t *p_demux )
                      * affect the reading speed too much. */
                     if( !(++i_loop_count % 1024) )
                     {
-                        if( p_demux->b_die ) return -1;
+                        if( !vlc_object_alive (p_demux) ) return -1;
                         msleep( 10000 );
 
                         if( !(i_loop_count % (1024 * 10)) )
@@ -1059,7 +1084,7 @@ static int Demux_Seekable( demux_t *p_demux )
         if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
         {
             msg_Warn( p_demux, "failed reading data" );
-            tk->b_activated = false;
+            tk->b_eof = false;
             toread[i_track].b_ok = false;
             continue;
         }
@@ -1126,8 +1151,6 @@ static int Demux_Seekable( demux_t *p_demux )
             toread[i_track].i_posf = -1;
         }
 
-        b_stream = true; /* at least one read succeed */
-
         if( tk->i_cat != VIDEO_ES )
             p_frame->i_dts = p_frame->i_pts;
         else
@@ -1294,7 +1317,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
 
     demux_sys_t *p_sys = p_demux->p_sys;
     unsigned int i_stream;
-    msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%",
+    msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
              i_date / 1000000, i_percent );
 
     if( p_sys->b_seekable )
@@ -1317,15 +1340,15 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
             /* try to find chunk that is at i_percent or the file */
             i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
                            p_sys->i_movi_begin );
-            /* search first selected stream */
+            /* search first selected stream (and prefer non eof ones) */
             for( i_stream = 0, p_stream = NULL;
                         i_stream < p_sys->i_track; i_stream++ )
             {
-                p_stream = p_sys->track[i_stream];
-                if( p_stream->b_activated )
-                {
+                if( !p_stream || p_stream->b_eof )
+                    p_stream = p_sys->track[i_stream];
+
+                if( p_stream->b_activated && !p_stream->b_eof )
                     break;
-                }
             }
             if( !p_stream || !p_stream->b_activated )
             {
@@ -1354,7 +1377,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
 
             i_date = AVI_GetPTS( p_stream );
             /* TODO better support for i_samplesize != 0 */
-            msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
+            msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
         }
 
         /* */
@@ -1365,10 +1388,11 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
             if( !p_stream->b_activated )
                 continue;
 
-            AVI_TrackSeek( p_demux, i_stream, i_date );
+            p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
         }
+        es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
         p_sys->i_time = i_date;
-        msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
+        msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
         return VLC_SUCCESS;
     }
     else
@@ -1593,7 +1617,7 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
 
     for( ;; )
     {
-        if( p_demux->b_die ) return VLC_EGENERIC;
+        if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
 
         if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
         {
@@ -1613,7 +1637,7 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
              * affect the reading speed too much. */
             if( !(++i_loop_count % 1024) )
             {
-                if( p_demux->b_die ) return VLC_EGENERIC;
+                if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
                 msleep( 10000 );
 
                 if( !(i_loop_count % (1024 * 10)) )
@@ -1776,7 +1800,7 @@ static int AVI_TrackSeek( demux_t *p_demux,
         }
 
         msg_Dbg( p_demux,
-                 "old:"I64Fd" %s new "I64Fd,
+                 "old:%"PRId64" %s new %"PRId64,
                  i_oldpts,
                  i_oldpts > i_date ? ">" : "<",
                  i_date );
@@ -1797,8 +1821,6 @@ static int AVI_TrackSeek( demux_t *p_demux,
                         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
@@ -1838,7 +1860,7 @@ static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
 {
     switch( i_fourcc )
     {
-        case FOURCC_DIV1:
+        case VLC_CODEC_DIV1:
             /* we have:
              *  startcode:      0x00000100   32bits
              *  framenumber     ?             5bits
@@ -1851,13 +1873,14 @@ static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
             }
             return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
 
-        case FOURCC_DIV2:
-        case FOURCC_DIV3:   /* wmv1 also */
+        case VLC_CODEC_DIV2:
+        case VLC_CODEC_DIV3:
+        case VLC_CODEC_WMV1:
             /* we have
              *  picture type    0(I),1(P)     2bits
              */
             return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
-        case FOURCC_mp4v:
+        case VLC_CODEC_MP4V:
             /* we should find first occurrence of 0x000001b6 (32bits)
              *  startcode:      0x000001b6   32bits
              *  piture type     0(I),1(P)     2bits
@@ -1882,60 +1905,8 @@ vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
         case AUDIO_ES:
             wf_tag_to_fourcc( i_codec, &i_codec, NULL );
             return i_codec;
-
         case VIDEO_ES:
-            /* 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:
-                case FOURCC_mpg4:
-                    return FOURCC_DIV1;
-                case FOURCC_DIV2:
-                case FOURCC_div2:
-                case FOURCC_MP42:
-                case FOURCC_mp42:
-                case FOURCC_MPG3:
-                case FOURCC_mpg3:
-                    return FOURCC_DIV2;
-                case FOURCC_div3:
-                case FOURCC_MP43:
-                case FOURCC_mp43:
-                case FOURCC_DIV3:
-                case FOURCC_DIV4:
-                case FOURCC_div4:
-                case FOURCC_DIV5:
-                case FOURCC_div5:
-                case FOURCC_DIV6:
-                case FOURCC_div6:
-                case FOURCC_AP41:
-                case FOURCC_3IV1:
-                case FOURCC_3iv1:
-                case FOURCC_3IVD:
-                case FOURCC_3ivd:
-                case FOURCC_3VID:
-                case FOURCC_3vid:
-                    return FOURCC_DIV3;
-                case FOURCC_DIVX:
-                case FOURCC_divx:
-                case FOURCC_MP4S:
-                case FOURCC_mp4s:
-                case FOURCC_M4S2:
-                case FOURCC_m4s2:
-                case FOURCC_xvid:
-                case FOURCC_XVID:
-                case FOURCC_XviD:
-                case FOURCC_DX50:
-                case FOURCC_dx50:
-                case FOURCC_mp4v:
-                case FOURCC_4:
-                case FOURCC_3IV2:
-                case FOURCC_3iv2:
-                    return FOURCC_mp4v;
-            }
+            return vlc_fourcc_GetCodec( i_cat, i_codec );
         default:
             return VLC_FOURCC( 'u', 'n', 'd', 'f' );
     }
@@ -1968,6 +1939,7 @@ static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
                 break;
             case AVITWOCC_dc:
             case AVITWOCC_db:
+            case AVITWOCC_AC:
                 SET_PTR( pi_type, VIDEO_ES );
                 break;
             default:
@@ -2096,7 +2068,7 @@ static int AVI_PacketSearch( demux_t *p_demux )
          * this code is called only on broken files). */
         if( !(++i_count % 1024) )
         {
-            if( p_demux->b_die ) return VLC_EGENERIC;
+            if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
 
             msleep( 10000 );
             if( !(i_count % (1024 * 10)) )
@@ -2350,12 +2322,15 @@ static void AVI_IndexCreate( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
 
-    avi_chunk_list_t    *p_riff;
-    avi_chunk_list_t    *p_movi;
+    avi_chunk_list_t *p_riff;
+    avi_chunk_list_t *p_movi;
 
     unsigned int i_stream;
     off_t i_movi_end;
 
+    mtime_t i_dialog_update;
+    dialog_progress_bar_t *p_dialog = NULL;
+
     p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
     p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
 
@@ -2379,40 +2354,35 @@ static void AVI_IndexCreate( demux_t *p_demux )
 
 
     /* Only show dialog if AVI is > 10MB */
-    p_demux->p_sys->i_dialog_id = -1;
+    i_dialog_update = mdate();
     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();
-    }
+        p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
+                                       NULL, _("Cancel") );
 
     for( ;; )
     {
         avi_packet_t pk;
 
-        if( p_demux->b_die )
-        {
-            return;
-        }
+        if( !vlc_object_alive (p_demux) )
+            break;
 
-        /* Don't update dialog too often */
-        if( p_demux->p_sys->i_dialog_id > 0 &&
-            mdate() - p_demux->p_sys->last_update > 100000 )
+        /* Don't update/check dialog too often */
+        if( p_dialog && mdate() - i_dialog_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( dialog_ProgressCancelled( p_dialog ) )
+                break;
+
+            double f_current = stream_Tell( p_demux->s );
+            double f_size    = stream_Size( p_demux->s );
+            double f_pos     = f_current / f_size;
+            dialog_ProgressSet( p_dialog, NULL, f_pos );
+
+            i_dialog_update = mdate();
         }
 
         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 )
         {
@@ -2428,33 +2398,35 @@ static void AVI_IndexCreate( demux_t *p_demux )
         {
             switch( pk.i_fourcc )
             {
-                case AVIFOURCC_idx1:
-                    if( p_sys->b_odml )
-                    {
-                        avi_chunk_list_t *p_sysx;
-                        p_sysx = AVI_ChunkFind( &p_sys->ck_root,
-                                                AVIFOURCC_RIFF, 1 );
+            case AVIFOURCC_idx1:
+                if( p_sys->b_odml )
+                {
+                    avi_chunk_list_t *p_sysx;
+                    p_sysx = AVI_ChunkFind( &p_sys->ck_root,
+                                            AVIFOURCC_RIFF, 1 );
 
-                        msg_Dbg( p_demux, "looking for new RIFF chunk" );
-                        if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
-                        {
-                            goto print_stat;
-                        }
-                        break;
-                    }
-                    goto print_stat;
-                case AVIFOURCC_RIFF:
-                        msg_Dbg( p_demux, "new RIFF chunk found" );
-                case AVIFOURCC_rec:
-                case AVIFOURCC_JUNK:
-                    break;
-                default:
-                    msg_Warn( p_demux, "need resync, probably broken avi" );
-                    if( AVI_PacketSearch( p_demux ) )
-                    {
-                        msg_Warn( p_demux, "lost sync, abord index creation" );
+                    msg_Dbg( p_demux, "looking for new RIFF chunk" );
+                    if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
                         goto print_stat;
-                    }
+                    break;
+                }
+                goto print_stat;
+
+            case AVIFOURCC_RIFF:
+                    msg_Dbg( p_demux, "new RIFF chunk found" );
+                    break;
+
+            case AVIFOURCC_rec:
+            case AVIFOURCC_JUNK:
+                break;
+
+            default:
+                msg_Warn( p_demux, "need resync, probably broken avi" );
+                if( AVI_PacketSearch( p_demux ) )
+                {
+                    msg_Warn( p_demux, "lost sync, abord index creation" );
+                    goto print_stat;
+                }
             }
         }
 
@@ -2466,17 +2438,13 @@ static void AVI_IndexCreate( demux_t *p_demux )
     }
 
 print_stat:
-    if( p_demux->p_sys->i_dialog_id > 0 )
-    {
-        intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
-    }
+    if( p_dialog != NULL )
+        dialog_ProgressDestroy( p_dialog );
 
     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
     {
-        msg_Dbg( p_demux,
-                "stream[%d] creating %d index entries",
-                i_stream,
-                p_sys->track[i_stream]->i_idxnb );
+        msg_Dbg( p_demux, "stream[%d] creating %d index entries",
+                i_stream, p_sys->track[i_stream]->i_idxnb );
     }
 }
 
@@ -2494,8 +2462,7 @@ static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
         avi_track_t *tk = p_sys->track[i];
         if( tk->i_idxposc >= tk->i_idxnb )
         {
-            tk->b_activated = false;
-            if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, false );
+            tk->b_eof = true;
         }
         else
         {
@@ -2538,7 +2505,7 @@ static mtime_t  AVI_MovieGetLength( demux_t *p_demux )
         i_length /= (mtime_t)1000000;    /* in seconds */
 
         msg_Dbg( p_demux,
-                 "stream[%d] length:"I64Fd" (based on index)",
+                 "stream[%d] length:%"PRId64" (based on index)",
                  i,
                  i_length );
         i_maxlength = __MAX( i_maxlength, i_length );