]> git.sesse.net Git - vlc/blobdiff - modules/demux/avi/avi.c
demux: avi: use CLOCK_FREQ
[vlc] / modules / demux / avi / avi.c
index 7096d8d2efbafa7dc3de64ff6060756f355661dc..4c4b4666c886f5fdcb94191052c916f2db3773d4 100644 (file)
@@ -1,23 +1,24 @@
 /*****************************************************************************
  * avi.c : AVI file Stream input module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2009 the VideoLAN team
+ * Copyright (C) 2001-2009 VLC authors and VideoLAN
  * $Id$
+ *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -28,6 +29,7 @@
 # include "config.h"
 #endif
 #include <assert.h>
+#include <ctype.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_memory.h>
 
 #include "libavi.h"
+#include "../rawdv.h"
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 
 #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)." )
 
+#define BI_RAWRGB 0x00
+#define BI_RGBBITFIELDS 0x03
+
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-static const int pi_index[] = {0,1,2};
+static const int pi_index[] = {0,1,2,3};
 
 static const char *const ppsz_indexes[] = { N_("Ask for action"),
                                             N_("Always fix"),
-                                            N_("Never fix") };
+                                            N_("Never fix"),
+                                            N_("Fix when necessary")};
 
 vlc_module_begin ()
     set_shortname( "AVI" )
@@ -72,7 +78,7 @@ vlc_module_begin ()
     set_subcategory( SUBCAT_INPUT_DEMUX )
 
     add_bool( "avi-interleaved", false,
-              INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
+              INTERLEAVE_TEXT, INTERLEAVE_TEXT, true )
     add_integer( "avi-index", 0,
               INDEX_TEXT, INDEX_LONGTEXT, false )
         change_integer_list( pi_index, ppsz_indexes )
@@ -88,8 +94,6 @@ static int Seek            ( demux_t *, mtime_t, int );
 static int Demux_Seekable  ( demux_t * );
 static int Demux_UnSeekable( demux_t * );
 
-#define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
-
 static char *FromACP( const char *str )
 {
     return FromCharset(vlc_pgettext("GetACP", "CP1252"), str, strlen(str));
@@ -144,8 +148,14 @@ typedef struct
     int             i_scale;
     unsigned int    i_samplesize;
 
+    unsigned int    i_width_bytes;
+    bool            b_flipped;
+
     es_out_id_t     *p_es;
 
+    int             i_dv_audio_rate;
+    es_out_id_t     *p_es_dv_audio;
+
     /* Avi Index */
     avi_index_t     idx;
 
@@ -156,8 +166,6 @@ typedef struct
     unsigned int    i_blockno;
     unsigned int    i_blocksize;
 
-    /* For muxed streams */
-    stream_t        *p_out_muxed;
 } avi_track_t;
 
 struct demux_sys_t
@@ -166,7 +174,9 @@ struct demux_sys_t
     mtime_t i_length;
 
     bool  b_seekable;
-    bool  b_muxed;
+    bool  b_fastseekable;
+    bool  b_indexloaded; /* if we read indexes from end of file before starting */
+    uint32_t i_avih_flags;
     avi_chunk_t ck_root;
 
     bool  b_odml;
@@ -215,10 +225,15 @@ static void AVI_IndexCreate  ( demux_t * );
 
 static void AVI_ExtractSubtitle( demux_t *, unsigned int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
 
+static void AVI_DvHandleAudio( demux_t *, avi_track_t *, block_t * );
+
 static mtime_t  AVI_MovieGetLength( demux_t * );
 
 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
 
+block_t * ReadFrame( demux_t *p_demux, const avi_track_t *tk,
+                     const int i_header, const int i_size );
+
 /*****************************************************************************
  * Stream management
  *****************************************************************************/
@@ -242,7 +257,7 @@ static int Open( vlc_object_t * p_this )
     demux_t  *p_demux = (demux_t *)p_this;
     demux_sys_t     *p_sys;
 
-    bool       b_index = false;
+    bool       b_index = false, b_aborted = false;
     int              i_do_index;
 
     avi_chunk_list_t    *p_riff;
@@ -270,20 +285,15 @@ static int Open( vlc_object_t * p_this )
         return VLC_EGENERIC;
     }
 
-    /* Initialize input  structures. */
-    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_movi_lastchunk_pos = 0;
+    /* Initialize input structures. */
+    p_sys = p_demux->p_sys = calloc( 1, sizeof(demux_sys_t) );
     p_sys->b_odml   = false;
-    p_sys->b_muxed  = false;
-    p_sys->i_track  = 0;
     p_sys->track    = NULL;
     p_sys->meta     = NULL;
     TAB_INIT(p_sys->i_attachment, p_sys->attachment);
 
-    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
+    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
+    stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
 
     p_demux->pf_control = Control;
     p_demux->pf_demux = Demux_Seekable;
@@ -330,16 +340,18 @@ static int Open( vlc_object_t * p_this )
     p_riff  = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
     p_hdrl  = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
     p_movi  = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
+    if( !p_movi )
+        p_movi  = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_movi, 0 );
 
     if( !p_hdrl || !p_movi )
     {
-        msg_Err( p_demux, "avi module discarded (invalid file)" );
+        msg_Err( p_demux, "invalid file: cannot find hdrl or movi chunks" );
         goto error;
     }
 
     if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
     {
-        msg_Err( p_demux, "cannot find avih chunk" );
+        msg_Err( p_demux, "invalid file: cannot find avih chunk" );
         goto error;
     }
     i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
@@ -364,12 +376,13 @@ static int Open( vlc_object_t * p_this )
              p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
 
     AVI_MetaLoad( p_demux, p_riff, p_avih );
+    p_sys->i_avih_flags = p_avih->i_flags;
 
     /* 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 ) );
-        if( !tk )
+        avi_track_t           *tk     = calloc( 1, sizeof( avi_track_t ) );
+        if( unlikely( !tk ) )
             goto error;
 
         avi_chunk_list_t      *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
@@ -379,7 +392,6 @@ static int Open( vlc_object_t * p_this )
         avi_chunk_strf_vids_t *p_vids = NULL;
         es_format_t fmt;
 
-        memset( tk, 0, sizeof(*tk) );
         tk->b_eof = false;
         tk->b_activated = true;
 
@@ -456,12 +468,17 @@ static int Open( vlc_object_t * p_this )
                 if( fmt.i_extra > 0 )
                 {
                     fmt.p_extra = malloc( fmt.i_extra );
-                    if( !fmt.p_extra ) goto error;
+                    if( unlikely(fmt.p_extra == NULL) )
+                    {
+                        free( tk );
+                        goto error;
+                    }
                     memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
                 }
                 break;
 
             case( AVIFOURCC_vids ):
+            {
                 tk->i_cat   = VIDEO_ES;
                 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
                                                   p_vids->p_bih->biCompression );
@@ -472,7 +489,7 @@ static int Open( vlc_object_t * p_this )
                    tk->i_cat = SPU_ES;
                    break;
                 }
-                else if( p_vids->p_bih->biCompression == 0x00 )
+                else if( p_vids->p_bih->biCompression == BI_RAWRGB )
                 {
                     switch( p_vids->p_bih->biBitCount )
                     {
@@ -489,8 +506,11 @@ static int Open( vlc_object_t * p_this )
                         case 9: /* <- TODO check that */
                             tk->i_codec = VLC_CODEC_I410;
                             break;
-                        case 8: /* <- TODO check that */
-                            tk->i_codec = VLC_CODEC_GREY;
+                        case 8:
+                            if ( p_vids->p_bih->biClrUsed )
+                                tk->i_codec = VLC_CODEC_RGBP;
+                            else
+                                tk->i_codec = VLC_CODEC_GREY;
                             break;
                     }
                     es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
@@ -508,9 +528,32 @@ static int Open( vlc_object_t * p_this )
                         fmt.video.i_gmask = 0x03e0;
                         fmt.video.i_bmask = 0x001f;
                         break;
+                    case VLC_CODEC_RGBP:
+                    {
+                        const VLC_BITMAPINFO *p_bi = (const VLC_BITMAPINFO *) p_vids->p_bih;
+                        fmt.video.p_palette = malloc( sizeof(video_palette_t) );
+                        if ( fmt.video.p_palette )
+                        {
+                            uint32_t entry;
+                            for ( uint32_t i=0; i<p_vids->p_bih->biClrUsed; i++ )
+                            {
+                                 entry = GetDWBE( &p_bi->bmiColors[i] );
+                                 fmt.video.p_palette->palette[i][0] = entry >> 24;
+                                 fmt.video.p_palette->palette[i][1] = (entry >> 16) & 0xFF;
+                                 fmt.video.p_palette->palette[i][2] = (entry >> 8) & 0xFF;
+                                 fmt.video.p_palette->palette[i][3] = entry & 0xFF;
+                            }
+                            fmt.video.p_palette->i_entries = p_vids->p_bih->biClrUsed;
+                        }
+                    }
+                        break;
                     default:
                         break;
                     }
+
+                    tk->i_width_bytes = p_vids->p_bih->biWidth * (p_vids->p_bih->biBitCount >> 3);
+                    /* RGB DIB are coded from bottom to top */
+                    if ( p_vids->p_bih->biHeight < INT32_MAX ) tk->b_flipped = true;
                 }
                 else
                 {
@@ -523,18 +566,38 @@ static int Open( vlc_object_t * p_this )
                     }
                 }
                 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) );
+
+                 /* Uncompresse Bitmap or YUV, YUV being always topdown */
+                if ( fmt.video.i_height > INT32_MAX )
+                    fmt.video.i_height =
+                        (unsigned int)(-(int)p_vids->p_bih->biHeight);
+
+                avi_chunk_vprp_t *p_vprp = AVI_ChunkFind( p_strl, AVIFOURCC_vprp, 0 );
+                if( p_vprp )
+                {
+                    uint32_t i_frame_aspect_ratio = p_vprp->i_frame_aspect_ratio;
+                    if( p_vprp->i_video_format_token >= 1 &&
+                        p_vprp->i_video_format_token <= 4 )
+                        i_frame_aspect_ratio = 0x00040003;
+                    fmt.video.i_sar_num = ((i_frame_aspect_ratio >> 16) & 0xffff) * fmt.video.i_height;
+                    fmt.video.i_sar_den = ((i_frame_aspect_ratio >>  0) & 0xffff) * fmt.video.i_width;
+                }
+                /* Extradata is the remainder of the chunk less the BIH */
+                fmt.i_extra = p_vids->i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
                 if( fmt.i_extra > 0 )
                 {
                     fmt.p_extra = malloc( fmt.i_extra );
-                    if( !fmt.p_extra ) goto error;
+                    if( unlikely(fmt.p_extra == NULL) )
+                    {
+                        free( tk );
+                        goto error;
+                    }
                     memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
                 }
 
@@ -545,20 +608,11 @@ static int Open( vlc_object_t * p_this )
                          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) */
+                /* Extract palette from extradata if bpp <= 8 */
                 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
                 {
-                    /* The palette is not always included in biSize */
-                    fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
+                    /* The palette should not be included in biSize, but come
+                     * directly after BITMAPINFORHEADER in the BITMAPINFO structure */
                     if( fmt.i_extra > 0 && fmt.p_extra )
                     {
                         const uint8_t *p_pal = fmt.p_extra;
@@ -566,14 +620,15 @@ static int Open( vlc_object_t * p_this )
                         fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
                         fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
 
-                        for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
+                        for( int k = 0; k < fmt.video.p_palette->i_entries; k++ )
                         {
                             for( int j = 0; j < 4; j++ )
-                                fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
+                                fmt.video.p_palette->palette[k][j] = p_pal[4*k+j];
                         }
                     }
                 }
                 break;
+            }
 
             case( AVIFOURCC_txts):
                 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
@@ -583,21 +638,16 @@ static int Open( vlc_object_t * p_this )
 
             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;
+                tk->i_cat   = VIDEO_ES;
+                tk->i_codec = AVI_FourccGetCodec( VIDEO_ES, p_strh->i_handler );
+                tk->i_samplesize = 0;
+                tk->i_dv_audio_rate = tk->i_codec == VLC_CODEC_DV ? -1 : 0;
+
+                es_format_Init( &fmt, VIDEO_ES, p_strh->i_handler );
+                fmt.video.i_width  = p_avih->i_width;
+                fmt.video.i_height = p_avih->i_height;
+                break;
 
             case( AVIFOURCC_mids):
                 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
@@ -609,13 +659,9 @@ static int Open( vlc_object_t * p_this )
         }
         if( p_strn )
             fmt.psz_description = FromACP( p_strn->p_str );
-        if( tk->p_out_muxed == NULL )
-            tk->p_es = es_out_Add( p_demux->out, &fmt );
+        tk->p_es = es_out_Add( p_demux->out, &fmt );
         TAB_APPEND( p_sys->i_track, p_sys->track, tk );
-        if(!p_sys->b_muxed )
-        {
-            es_format_Clean( &fmt );
-        }
+        es_format_Clean( &fmt );
     }
 
     if( p_sys->i_track <= 0 )
@@ -628,7 +674,7 @@ static int Open( vlc_object_t * p_this )
     if( i_do_index == 1 ) /* Always fix */
     {
 aviindex:
-        if( p_sys->b_seekable )
+        if( p_sys->b_fastseekable )
         {
             AVI_IndexCreate( p_demux );
         }
@@ -653,7 +699,6 @@ aviindex:
         const avi_track_t *tk = p_sys->track[i];
         if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
             i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
-            continue;
     }
     if( i_idx_totalframes != p_avih->i_totalframes &&
         p_sys->i_length < (mtime_t)p_avih->i_totalframes *
@@ -665,29 +710,37 @@ aviindex:
 
         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( (i_do_index == 0 || i_do_index == 3) && !b_index )
         {
-            if( !p_sys->b_seekable ) {
+            if( !p_sys->b_fastseekable ) {
                 b_index = true;
                 goto aviindex;
             }
-            switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
-               _( "Because this AVI file index is broken or missing, "
-                  "seeking will not work correctly.\n"
-                  "VLC won't repair your file but can temporary fix this "
-                  "problem by building an index in memory.\n"
-                  "This step might take a long time on a large file.\n"
-                  "What do you want to do ?" ),
-                  _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
+            if( i_do_index == 0 )
+            {
+                switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
+                   _( "Because this AVI file index is broken or missing, "
+                      "seeking will not work correctly.\n"
+                      "VLC won't repair your file but can temporary fix this "
+                      "problem by building an index in memory.\n"
+                      "This step might take a long time on a large file.\n"
+                      "What do you want to do?" ),
+                      _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
+                {
+                    case 1:
+                        b_index = true;
+                        msg_Dbg( p_demux, "Fixing AVI index" );
+                        goto aviindex;
+                    case 3:
+                        b_aborted = true;
+                        goto error;
+                }
+            }
+            else
             {
-                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;
+                b_index = true;
+                msg_Dbg( p_demux, "Fixing AVI index" );
+                goto aviindex;
             }
         }
     }
@@ -697,7 +750,6 @@ aviindex:
     {
         avi_track_t         *tk = p_sys->track[i];
         avi_chunk_list_t    *p_strl;
-        avi_chunk_strh_t    *p_strh;
         avi_chunk_strf_auds_t    *p_auds;
 
         if( tk->i_cat != AUDIO_ES )
@@ -711,7 +763,6 @@ aviindex:
             continue;
         }
         p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
-        p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
         p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
 
         if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
@@ -755,7 +806,7 @@ error:
 
     AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
     free( p_sys );
-    return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
+    return b_aborted ? VLC_ETIMEOUT : VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -764,22 +815,21 @@ error:
 static void Close ( vlc_object_t * p_this )
 {
     demux_t *    p_demux = (demux_t *)p_this;
-    unsigned int i;
     demux_sys_t *p_sys = p_demux->p_sys  ;
 
-    for( i = 0; i < p_sys->i_track; i++ )
+    for( unsigned int i = 0; i < p_sys->i_track; i++ )
     {
         if( p_sys->track[i] )
         {
-            if( p_sys->track[i]->p_out_muxed )
-                stream_Delete( p_sys->track[i]->p_out_muxed );
             avi_index_Clean( &p_sys->track[i]->idx );
             free( p_sys->track[i] );
         }
     }
     free( p_sys->track );
+
     AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
     vlc_meta_Delete( p_sys->meta );
+
     for( unsigned i = 0; i < p_sys->i_attachment; i++)
         vlc_input_attachment_Delete(p_sys->attachment[i]);
     free(p_sys->attachment);
@@ -787,6 +837,85 @@ static void Close ( vlc_object_t * p_this )
     free( p_sys );
 }
 
+/*****************************************************************************
+ * ReadFrame: Reads frame, using stride if necessary
+ *****************************************************************************/
+
+block_t * ReadFrame( demux_t *p_demux, const avi_track_t *tk,
+                     const int i_header, const int i_size )
+{
+    block_t *p_frame = stream_Block( p_demux->s, __EVEN( i_size ) );
+    if ( !p_frame ) return p_frame;
+
+    if( i_size % 2 )    /* read was padded on word boundary */
+    {
+        p_frame->i_buffer--;
+    }
+
+    /* skip header */
+    if( tk->i_idxposb == 0 )
+    {
+        p_frame->p_buffer += i_header;
+        p_frame->i_buffer -= i_header;
+    }
+
+    if ( !tk->i_width_bytes )
+        return p_frame;
+
+    const unsigned int i_stride_bytes = ((( (tk->i_width_bytes << 3) + 31) & ~31) >> 3);
+
+    if ( p_frame->i_buffer < i_stride_bytes )
+    {
+        p_frame->i_buffer = 0;
+        return p_frame;
+    }
+
+    if( !tk->b_flipped )
+    {
+        const uint8_t *p_src = p_frame->p_buffer + i_stride_bytes;
+        const uint8_t *p_end = p_frame->p_buffer + p_frame->i_buffer;
+        uint8_t *p_dst = p_frame->p_buffer + tk->i_width_bytes;
+
+        p_frame->i_buffer = tk->i_width_bytes;
+
+        while ( p_src + i_stride_bytes <= p_end )
+        {
+            memmove( p_dst, p_src, tk->i_width_bytes );
+            p_src += i_stride_bytes;
+            p_dst += tk->i_width_bytes;
+            p_frame->i_buffer += tk->i_width_bytes;
+        }
+    }
+    else
+    {
+        block_t *p_flippedframe = block_Alloc( p_frame->i_buffer );
+        if ( !p_flippedframe )
+        {
+            block_Release( p_frame );
+            return NULL;
+        }
+
+        unsigned int i_lines = p_frame->i_buffer / i_stride_bytes;
+        const uint8_t *p_src = p_frame->p_buffer + i_lines * i_stride_bytes;
+        uint8_t *p_dst = p_flippedframe->p_buffer;
+
+        p_flippedframe->i_buffer = 0;
+
+        while ( i_lines-- > 0 )
+        {
+            p_src -= i_stride_bytes;
+            memcpy( p_dst, p_src, tk->i_width_bytes );
+            p_dst += tk->i_width_bytes;
+            p_flippedframe->i_buffer += tk->i_width_bytes;
+        }
+
+        block_Release( p_frame );
+        p_frame = p_flippedframe;
+    }
+
+    return p_frame;
+}
+
 /*****************************************************************************
  * Demux_Seekable: reads and demuxes data packets for stream seekable
  *****************************************************************************
@@ -821,14 +950,13 @@ static int Demux_Seekable( demux_t *p_demux )
         avi_track_t *tk = p_sys->track[i_track];
         bool  b;
 
-        if( p_sys->b_muxed && tk->p_out_muxed )
+        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
+        if( tk->p_es_dv_audio )
         {
-            i_track_count++;
-            tk->b_activated = true;
-            continue;
+            bool b_extra;
+            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
+            b |= b_extra;
         }
-
-        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)
@@ -890,11 +1018,11 @@ static int Demux_Seekable( demux_t *p_demux )
 
         if( tk->i_samplesize )
         {
-            toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
+            toread[i_track].i_toread = AVI_PTSToByte( tk, llabs( i_dpts ) );
         }
         else
         {
-            toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
+            toread[i_track].i_toread = AVI_PTSToChunk( tk, llabs( i_dpts ) );
         }
 
         if( i_dpts < 0 )
@@ -1011,6 +1139,7 @@ static int Demux_Seekable( demux_t *p_demux )
                     index.i_flags  = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
                     index.i_pos    = avi_pk.i_pos;
                     index.i_length = avi_pk.i_size;
+                    index.i_lengthtotal = index.i_length;
                     avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
 
                     /* do we will read this data ? */
@@ -1070,23 +1199,15 @@ static int Demux_Seekable( demux_t *p_demux )
             i_size += 8; /* need to read and skip header */
         }
 
-        if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
+        if( ( p_frame = ReadFrame( p_demux, tk,
+                        ( tk->i_idxposb == 0 ) ? 8 : 0, i_size ) )==NULL )
         {
             msg_Warn( p_demux, "failed reading data" );
             tk->b_eof = false;
             toread[i_track].b_ok = false;
             continue;
         }
-        if( i_size % 2 )    /* read was padded on word boundary */
-        {
-            p_frame->i_buffer--;
-        }
-        /* skip header */
-        if( tk->i_idxposb == 0 )
-        {
-            p_frame->p_buffer += 8;
-            p_frame->i_buffer -= 8;
-        }
+
         p_frame->i_pts = AVI_GetPTS( tk ) + 1;
         if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
         {
@@ -1148,11 +1269,9 @@ static int Demux_Seekable( demux_t *p_demux )
             p_frame->i_pts = VLC_TS_INVALID;
         }
 
-        //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 );
+        if( tk->i_dv_audio_rate )
+            AVI_DvHandleAudio( p_demux, tk, p_frame );
+        es_out_Send( p_demux->out, tk->p_es, p_frame );
     }
 }
 
@@ -1169,12 +1288,6 @@ static int Demux_UnSeekable( demux_t *p_demux )
     unsigned int i_stream;
     unsigned int i_packet;
 
-    if( p_sys->b_muxed )
-    {
-        msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
-        return VLC_EGENERIC;
-    }
-
     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
 
     /* *** find master stream for data packet skipping algo *** */
@@ -1185,6 +1298,12 @@ static int Demux_UnSeekable( demux_t *p_demux )
         bool  b;
 
         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
+        if( tk->p_es_dv_audio )
+        {
+            bool b_extra;
+            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
+            b |= b_extra;
+        }
 
         if( b && tk->i_cat == VIDEO_ES )
         {
@@ -1247,12 +1366,12 @@ static int Demux_UnSeekable( demux_t *p_demux )
         else
         {
             /* check for time */
-            if( __ABS( AVI_GetPTS( p_stream ) -
+            if( llabs( AVI_GetPTS( p_stream ) -
                         AVI_GetPTS( p_stream_master ) )< 600*1000 )
             {
                 /* load it and send to decoder */
-                block_t *p_frame;
-                if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
+                block_t *p_frame = ReadFrame( p_demux, p_stream, 8, avi_pk.i_size + 8 ) ;
+                if( p_frame == NULL )
                 {
                     return( -1 );
                 }
@@ -1266,7 +1385,8 @@ static int Demux_UnSeekable( demux_t *p_demux )
                     p_frame->i_pts = VLC_TS_INVALID;
                 }
 
-                //p_pes->i_rate = p_demux->stream.control.i_rate;
+                if( p_stream->i_dv_audio_rate )
+                    AVI_DvHandleAudio( p_demux, p_stream, p_frame );
                 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
             }
             else
@@ -1303,27 +1423,54 @@ static int Demux_UnSeekable( demux_t *p_demux )
  *****************************************************************************/
 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
 {
-
     demux_sys_t *p_sys = p_demux->p_sys;
     msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
-             i_date / 1000000, i_percent );
+             i_date / CLOCK_FREQ, i_percent );
 
     if( p_sys->b_seekable )
     {
-        unsigned i_stream;
+        int64_t i_pos_backup = stream_Tell( p_demux->s );
+
+        /* Check and lazy load indexes if it was not done (not fastseekable) */
+        if ( !p_sys->b_indexloaded && ( p_sys->i_avih_flags & AVIF_HASINDEX ) )
+        {
+            avi_chunk_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
+            if (unlikely( !p_riff ))
+                return VLC_EGENERIC;
+
+            int i_ret = AVI_ChunkFetchIndexes( p_demux->s, p_riff );
+            if ( i_ret )
+            {
+                /* Go back to position before index failure */
+                if ( stream_Tell( p_demux->s ) - i_pos_backup )
+                    stream_Seek( p_demux->s, i_pos_backup );
+
+                if ( p_sys->i_avih_flags & AVIF_MUSTUSEINDEX )
+                    return VLC_EGENERIC;
+            }
+            else AVI_IndexLoad( p_demux );
+
+            p_sys->b_indexloaded = true; /* we don't want to try each time */
+        }
 
         if( !p_sys->i_length )
         {
             avi_track_t *p_stream = NULL;
+            unsigned i_stream = 0;
             int64_t i_pos;
 
+            if ( !p_sys->i_movi_lastchunk_pos && /* set when index is successfully loaded */
+                 ! ( p_sys->i_avih_flags & AVIF_ISINTERLEAVED ) )
+            {
+                msg_Err( p_demux, "seeking without index at %d%%"
+                         " only works for interleaved files", i_percent );
+                goto failandresetpos;
+            }
             /* use i_percent to create a true i_date */
-            msg_Warn( p_demux, "seeking without index at %d%%"
-                      " only works for interleaved files", i_percent );
             if( i_percent >= 100 )
             {
                 msg_Warn( p_demux, "cannot seek so far !" );
-                return VLC_EGENERIC;
+                goto failandresetpos;
             }
             i_percent = __MAX( i_percent, 0 );
 
@@ -1345,14 +1492,14 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
             if( p_stream == NULL )
             {
                 msg_Warn( p_demux, "cannot find any selected stream" );
-                return VLC_EGENERIC;
+                goto failandresetpos;
             }
 
             /* be sure that the index exist */
             if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
             {
                 msg_Warn( p_demux, "cannot seek" );
-                return VLC_EGENERIC;
+                goto failandresetpos;
             }
 
             while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
@@ -1363,7 +1510,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
                                         i_stream, p_stream->i_idxposc + 1 ) )
                 {
                     msg_Warn( p_demux, "cannot seek" );
-                    return VLC_EGENERIC;
+                    goto failandresetpos;
                 }
             }
 
@@ -1373,7 +1520,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
         }
 
         /* */
-        for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
+        for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
         {
             avi_track_t *p_stream = p_sys->track[i_stream];
 
@@ -1384,8 +1531,15 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
         }
         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: %"PRId64" seconds", p_sys->i_time /1000000 );
+        msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /CLOCK_FREQ );
         return VLC_SUCCESS;
+
+failandresetpos:
+        /* Go back to position before index failure */
+        if ( stream_Tell( p_demux->s ) - i_pos_backup )
+            stream_Seek( p_demux->s, i_pos_backup );
+
+        return VLC_EGENERIC;
     }
     else
     {
@@ -1403,29 +1557,12 @@ static double ControlGetPosition( demux_t *p_demux )
 
     if( p_sys->i_length > 0 )
     {
-        return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
+        return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)CLOCK_FREQ );
     }
     else if( stream_Size( p_demux->s ) > 0 )
     {
-        unsigned int i;
-        int64_t i_tmp;
-        int64_t i64 = 0;
-
-        /* search the more advanced selected es */
-        for( i = 0; i < p_sys->i_track; i++ )
-        {
-            avi_track_t *tk = p_sys->track[i];
-            if( tk->b_activated && tk->i_idxposc < tk->idx.i_size )
-            {
-                i_tmp = tk->idx.p_entry[tk->i_idxposc].i_pos +
-                        tk->idx.p_entry[tk->i_idxposc].i_length + 8;
-                if( i_tmp > i64 )
-                {
-                    i64 = i_tmp;
-                }
-            }
-        }
-        return (double)i64 / stream_Size( p_demux->s );
+        double i64 = (uint64_t)stream_Tell( p_demux->s );
+        return i64 / stream_Size( p_demux->s );
     }
     return 0.0;
 }
@@ -1448,7 +1585,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             f = (double)va_arg( args, double );
             if( p_sys->b_seekable )
             {
-                i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
+                i64 = (mtime_t)(f * CLOCK_FREQ * p_sys->i_length);
                 return Seek( p_demux, i64, (int)(f * 100) );
             }
             else
@@ -1467,9 +1604,13 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             int i_percent = 0;
 
             i64 = (int64_t)va_arg( args, int64_t );
-            if( p_sys->i_length > 0 )
+            if( !p_sys->b_seekable )
+            {
+                return VLC_EGENERIC;
+            }
+            else if( p_sys->i_length > 0 )
             {
-                i_percent = 100 * i64 / (p_sys->i_length*1000000);
+                i_percent = 100 * i64 / (p_sys->i_length*CLOCK_FREQ);
             }
             else if( p_sys->i_time > 0 )
             {
@@ -1480,7 +1621,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         }
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = p_sys->i_length * (mtime_t)1000000;
+            *pi64 = p_sys->i_length * (mtime_t)CLOCK_FREQ;
             return VLC_SUCCESS;
 
         case DEMUX_GET_FPS:
@@ -1511,7 +1652,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             int *pi_int = va_arg( args, int * );
 
             *pi_int     = p_sys->i_attachment;
-            *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
+            *ppp_attach = calloc( p_sys->i_attachment, sizeof(**ppp_attach));
             for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
             return VLC_SUCCESS;
@@ -1534,7 +1675,7 @@ static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
     return (mtime_t)((int64_t)i_pts *
                      (int64_t)tk->i_rate /
                      (int64_t)tk->i_scale /
-                     (int64_t)1000000 );
+                     (int64_t)CLOCK_FREQ );
 }
 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
 {
@@ -1662,6 +1803,7 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
             index.i_flags  = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
             index.i_pos    = avi_pk.i_pos;
             index.i_length = avi_pk.i_size;
+            index.i_lengthtotal = index.i_length;
             avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
 
             if( avi_pk.i_stream == i_stream  )
@@ -2151,18 +2293,38 @@ static int AVI_IndexFind_idx1( demux_t *p_demux,
     }
     *pp_idx1 = p_idx1;
 
-    /* *** calculate offset *** */
-    /* Well, avi is __SHIT__ so test more than one entry
-     * (needed for some avi files) */
+    /* The offset in the index should be from the start of the movi content,
+     * but some broken files use offset from the start of the file. Just
+     * checking the offset of the first packet is not enough as some files
+     * has unused chunk at the beginning of the movi content.
+     */
     avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
-    *pi_offset = 0;
-    for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
+    uint64_t i_first_pos = UINT64_MAX;
+    for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 100 ); i++ )
     {
-        if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
-        {
-            *pi_offset = p_movi->i_chunk_pos + 8;
-            break;
-        }
+        if ( p_idx1->entry[i].i_length > 0 )
+            i_first_pos = __MIN( i_first_pos, p_idx1->entry[i].i_pos );
+    }
+
+    const uint64_t i_movi_content = p_movi->i_chunk_pos + 8;
+    if( i_first_pos < i_movi_content )
+    {
+        *pi_offset = i_movi_content;
+    }
+    else if( p_sys->b_seekable && i_first_pos < UINT64_MAX )
+    {
+        const uint8_t *p_peek;
+        if( !stream_Seek( p_demux->s, i_movi_content + i_first_pos ) &&
+            stream_Peek( p_demux->s, &p_peek, 4 ) >= 4 &&
+            ( !isdigit( p_peek[0] ) || !isdigit( p_peek[1] ) ||
+              !isalpha( p_peek[2] ) || !isalpha( p_peek[3] ) ) )
+            *pi_offset = 0;
+        else
+            *pi_offset = i_movi_content;
+    }
+    else
+    {
+        *pi_offset = 0;
     }
     return VLC_SUCCESS;
 }
@@ -2177,6 +2339,8 @@ static int AVI_IndexLoad_idx1( demux_t *p_demux,
     if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
         return VLC_EGENERIC;
 
+    p_sys->b_indexloaded = true;
+
     for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
     {
         unsigned i_cat;
@@ -2193,6 +2357,7 @@ static int AVI_IndexLoad_idx1( demux_t *p_demux,
             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;
+            index.i_lengthtotal = index.i_length;
 
             avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
         }
@@ -2205,6 +2370,8 @@ static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_
 {
     avi_entry_t index;
 
+    p_demux->p_sys->b_indexloaded = true;
+
     msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
     if( p_indx->i_indexsubtype == 0 )
     {
@@ -2214,6 +2381,7 @@ static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_
             index.i_flags  = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
             index.i_pos    = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
             index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
+            index.i_lengthtotal = index.i_length;
 
             avi_index_Append( p_index, pi_max_offset, &index );
         }
@@ -2226,6 +2394,7 @@ static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_
             index.i_flags  = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
             index.i_pos    = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
             index.i_length = p_indx->idx.field[i].i_size;
+            index.i_lengthtotal = index.i_length;
 
             avi_index_Append( p_index, pi_max_offset, &index );
         }
@@ -2270,6 +2439,8 @@ static void AVI_IndexLoad_indx( demux_t *p_demux,
         }
         else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
         {
+            if ( !p_sys->b_seekable )
+                return;
             avi_chunk_t    ck_sub;
             for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
             {
@@ -2422,6 +2593,7 @@ static void AVI_IndexCreate( demux_t *p_demux )
             index.i_flags   = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
             index.i_pos     = pk.i_pos;
             index.i_length  = pk.i_size;
+            index.i_lengthtotal = pk.i_size;
             avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
         }
         else
@@ -2509,6 +2681,12 @@ static void AVI_MetaLoad( demux_t *p_demux,
         { AVIFOURCC_ICOP, vlc_meta_Copyright },
         { AVIFOURCC_IGNR, vlc_meta_Genre },
         { AVIFOURCC_INAM, vlc_meta_Title },
+        { AVIFOURCC_ICRD, vlc_meta_Date },
+        { AVIFOURCC_ILNG, vlc_meta_Language },
+        { AVIFOURCC_IRTD, vlc_meta_Rating },
+        { AVIFOURCC_IWEB, vlc_meta_URL },
+        { AVIFOURCC_IPRT, vlc_meta_TrackNumber },
+        { AVIFOURCC_IFRM, vlc_meta_TrackTotal },
         { 0, -1 }
     };
     for( int i = 0; p_dsc[i].i_id != 0; i++ )
@@ -2524,6 +2702,58 @@ static void AVI_MetaLoad( demux_t *p_demux,
             vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
         free( psz_value );
     }
+
+    static const vlc_fourcc_t p_extra[] = {
+        AVIFOURCC_IARL, AVIFOURCC_ICMS, AVIFOURCC_ICRP, AVIFOURCC_IDIM, AVIFOURCC_IDPI,
+        AVIFOURCC_IENG, AVIFOURCC_IKEY, AVIFOURCC_ILGT, AVIFOURCC_IMED, AVIFOURCC_IPLT,
+        AVIFOURCC_IPRD, AVIFOURCC_ISBJ, AVIFOURCC_ISFT, AVIFOURCC_ISHP, AVIFOURCC_ISRC,
+        AVIFOURCC_ISRF, AVIFOURCC_ITCH, AVIFOURCC_ISMP, AVIFOURCC_IDIT, AVIFOURCC_ISGN,
+        AVIFOURCC_IWRI, AVIFOURCC_IPRO, AVIFOURCC_ICNM, AVIFOURCC_IPDS, AVIFOURCC_IEDT,
+        AVIFOURCC_ICDS, AVIFOURCC_IMUS, AVIFOURCC_ISTD, AVIFOURCC_IDST, AVIFOURCC_ICNT,
+        AVIFOURCC_ISTR, 0,
+    };
+
+    for( int i = 0; p_extra[i] != 0; i++ )
+    {
+        avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_extra[i], 0 );
+        if( !p_strz )
+            continue;
+        char *psz_value = FromACP( p_strz->p_str );
+        if( !psz_value )
+            continue;
+
+        if( *psz_value )
+            vlc_meta_AddExtra( p_meta, p_strz->p_type, psz_value );
+        free( psz_value );
+    }
+}
+
+static void AVI_DvHandleAudio( demux_t *p_demux, avi_track_t *tk, block_t *p_frame )
+{
+    size_t i_offset = 80 * 6 + 80 * 16 * 3 + 3;
+    if( p_frame->i_buffer < i_offset + 5 )
+        return;
+
+    if( p_frame->p_buffer[i_offset] != 0x50 )
+        return;
+
+    es_format_t fmt;
+    dv_get_audio_format( &fmt, &p_frame->p_buffer[i_offset + 1] );
+
+    if( tk->p_es_dv_audio && tk->i_dv_audio_rate != (int)fmt.audio.i_rate )
+    {
+        es_out_Del( p_demux->out, tk->p_es_dv_audio );
+        tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
+    }
+    else if( !tk->p_es_dv_audio )
+    {
+        tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
+    }
+    tk->i_dv_audio_rate = fmt.audio.i_rate;
+
+    block_t *p_frame_audio = dv_extract_audio( p_frame );
+    if( p_frame_audio )
+        es_out_Send( p_demux->out, tk->p_es_dv_audio, p_frame_audio );
 }
 
 /*****************************************************************************
@@ -2595,8 +2825,11 @@ static void AVI_ExtractSubtitle( demux_t *p_demux,
     }
 
     /* */
-    if( i_size > 1000000 )
+    if( i_size > 10000000 )
+    {
+        msg_Dbg( p_demux, "Attached subtitle too big: %u", i_size );
         goto exit;
+    }
 
     if( stream_Seek( p_demux->s, i_position ) )
         goto exit;
@@ -2650,9 +2883,9 @@ exit:
         block_Release( p_block );
 
     if( p_attachment )
-        msg_Dbg( p_demux, "Loaded an embed subtitle" );
+        msg_Dbg( p_demux, "Loaded an embedded subtitle" );
     else
-        msg_Warn( p_demux, "Failed to load an embed subtitle" );
+        msg_Warn( p_demux, "Failed to load an embedded subtitle" );
 
     if( p_indx == &ck.indx )
         AVI_ChunkFree( p_demux->s, &ck );