]> git.sesse.net Git - vlc/blobdiff - modules/demux/avi/avi.c
demux: avi: improve broken index offset heuristic (fix #14120)
[vlc] / modules / demux / avi / avi.c
index 690d38c75d4c9511e022ef7d3b1035b618c323bc..34a33621814b7c32b7006873d9488ca31b7ec827 100644 (file)
@@ -57,6 +57,9 @@
     "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 * );
 
@@ -97,6 +100,7 @@ static char *FromACP( const char *str )
 }
 
 #define IGNORE_ES NAV_ES
+#define READ_LENGTH (25 * 1000) // 25ms
 
 typedef struct
 {
@@ -145,6 +149,9 @@ 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;
@@ -167,7 +174,11 @@ struct demux_sys_t
     mtime_t i_time;
     mtime_t i_length;
 
+    bool  b_interleaved;
     bool  b_seekable;
+    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;
@@ -222,6 +233,9 @@ 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
  *****************************************************************************/
@@ -280,17 +294,13 @@ static int Open( vlc_object_t * p_this )
     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;
+    p_demux->pf_demux = (p_sys->b_seekable) ? Demux_Seekable : Demux_UnSeekable;
 
-    /* For unseekable stream, automatically use Demux_UnSeekable */
-    if( !p_sys->b_seekable
-     || var_InheritBool( p_demux, "avi-interleaved" ) )
-    {
-        p_demux->pf_demux = Demux_UnSeekable;
-    }
+    p_sys->b_interleaved = var_InheritBool( p_demux, "avi-interleaved" );
 
     if( i_peeker > 0 )
     {
@@ -362,7 +372,10 @@ static int Open( vlc_object_t * p_this )
              p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
              p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
 
+    p_sys->b_interleaved |= (p_avih->i_flags & AVIF_ISINTERLEAVED);
+
     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++ )
@@ -382,9 +395,9 @@ static int Open( vlc_object_t * p_this )
         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 );
+        p_auds = (avi_chunk_strf_auds_t*)p_vids;
 
-        if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
+        if( p_strl == NULL || p_strh == NULL || p_vids == NULL )
         {
             msg_Warn( p_demux, "stream[%d] incomplete", i );
             free( tk );
@@ -395,7 +408,7 @@ static int Open( vlc_object_t * p_this )
         tk->i_scale = p_strh->i_scale;
         tk->i_samplesize = p_strh->i_samplesize;
         msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
-                 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
+                i, tk->i_rate, tk->i_scale, tk->i_samplesize );
 
         switch( p_strh->i_type )
         {
@@ -433,6 +446,11 @@ static int Open( vlc_object_t * p_this )
                     tk->i_blocksize = 0; /* fix vorbis VBR decoding */
                 }
 
+                if ( tk->i_codec == VLC_CODEC_MP4A )
+                {
+                    tk->i_samplesize = 0; /* ADTS/AAC VBR */
+                }
+
                 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
 
                 fmt.audio.i_channels        = p_auds->p_wf->nChannels;
@@ -442,6 +460,15 @@ static int Open( vlc_object_t * p_this )
                 fmt.audio.i_bitspersample   = p_auds->p_wf->wBitsPerSample;
                 fmt.b_packetized            = !tk->i_blocksize;
 
+                avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
+                if( p_info )
+                {
+                    int i_chunk = AVIFOURCC_IAS1 + ((i - 1) << 24);
+                    avi_chunk_STRING_t *p_lang = AVI_ChunkFind( p_info, i_chunk, 0 );
+                    if( p_lang != NULL )
+                        fmt.psz_language = FromACP( p_lang->p_str );
+                }
+
                 msg_Dbg( p_demux,
                     "stream[%d] audio(0x%x - %s) %d channels %dHz %dbits",
                     i, p_auds->p_wf->wFormatTag,vlc_fourcc_GetDescription(AUDIO_ES,tk->i_codec),
@@ -454,7 +481,11 @@ 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;
@@ -471,7 +502,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 )
                     {
@@ -488,8 +519,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 );
@@ -507,9 +541,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
                 {
@@ -522,11 +579,18 @@ 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;
+
+                 /* 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 )
                 {
@@ -542,7 +606,11 @@ 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_vids->p_bih[1], fmt.i_extra );
                 }
 
@@ -553,13 +621,6 @@ 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 */
                 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
                 {
@@ -626,7 +687,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 );
         }
@@ -655,16 +716,13 @@ aviindex:
     if( i_idx_totalframes != p_avih->i_totalframes &&
         p_sys->i_length < (mtime_t)p_avih->i_totalframes *
                           (mtime_t)p_avih->i_microsecperframe /
-                          (mtime_t)1000000 )
+                          CLOCK_FREQ )
     {
-        if( !vlc_object_alive( p_demux) )
-            goto error;
-
         msg_Warn( p_demux, "broken or missing index, 'seek' will be "
                            "approximative or will exhibit strange behavior" );
         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;
             }
@@ -732,7 +790,7 @@ aviindex:
                 continue;
             }
             tk->i_samplesize = 1;
-            tk->i_rate       = i_track_length  * (int64_t)1000000/ i_length;
+            tk->i_rate       = i_track_length  * CLOCK_FREQ / i_length;
             msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
         }
     }
@@ -789,6 +847,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
  *****************************************************************************
@@ -816,17 +953,18 @@ static int Demux_Seekable( demux_t *p_demux )
     /* cannot be more than 100 stream (dcXX or wbXX) */
     avi_track_toread_t toread[100];
 
+
     /* detect new selected/unselected streams */
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
     {
         avi_track_t *tk = p_sys->track[i_track];
-        bool  b, b_error;
+        bool  b;
 
-        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b, &b_error );
+        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_error );
+            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
             b |= b_extra;
         }
         if( b && !tk->b_activated )
@@ -849,9 +987,9 @@ static int Demux_Seekable( demux_t *p_demux )
 
     if( i_track_count <= 0 )
     {
-        int64_t i_length = p_sys->i_length * (mtime_t)1000000;
+        int64_t i_length = p_sys->i_length * CLOCK_FREQ;
 
-        p_sys->i_time += 25*1000;  /* read 25ms */
+        p_sys->i_time += READ_LENGTH;
         if( i_length > 0 )
         {
             if( p_sys->i_time >= i_length )
@@ -863,14 +1001,13 @@ static int Demux_Seekable( demux_t *p_demux )
     }
 
     /* wait for the good time */
-    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
-    p_sys->i_time += 25*1000;  /* read 25ms */
+    es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
+    p_sys->i_time += READ_LENGTH;
 
     /* init toread */
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
     {
         avi_track_t *tk = p_sys->track[i_track];
-        mtime_t i_dpts;
 
         toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
         if( tk->i_idxposc < tk->idx.i_size )
@@ -886,21 +1023,18 @@ static int Demux_Seekable( demux_t *p_demux )
             toread[i_track].i_posf = -1;
         }
 
-        i_dpts = p_sys->i_time - AVI_GetPTS( tk  );
+        mtime_t i_dpts = p_sys->i_time - AVI_GetPTS( tk );
 
         if( tk->i_samplesize )
         {
-            toread[i_track].i_toread = AVI_PTSToByte( tk, llabs( i_dpts ) );
+            toread[i_track].i_toread = AVI_PTSToByte( tk, i_dpts );
         }
-        else
+        else if ( i_dpts > -2 * CLOCK_FREQ ) /* don't send a too early dts (low fps video) */
         {
-            toread[i_track].i_toread = AVI_PTSToChunk( tk, llabs( i_dpts ) );
-        }
-
-        if( i_dpts < 0 )
-        {
-            toread[i_track].i_toread *= -1;
+            toread[i_track].i_toread = AVI_PTSToChunk( tk, i_dpts );
         }
+        else
+            toread[i_track].i_toread = -1;
     }
 
     for( ;; )
@@ -917,12 +1051,12 @@ static int Demux_Seekable( demux_t *p_demux )
         {
             if( !toread[i].b_ok ||
                 AVI_GetDPTS( p_sys->track[i],
-                             toread[i].i_toread ) <= -25 * 1000 )
+                             toread[i].i_toread ) <= -READ_LENGTH )
             {
                 continue;
             }
 
-            if( toread[i].i_toread > 0 )
+            if( toread[i].i_toread >= 0 )
             {
                 b_done = false; /* not yet finished */
             }
@@ -953,7 +1087,7 @@ static int Demux_Seekable( demux_t *p_demux )
 
             /* 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 )
+            if( p_sys->b_seekable && p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
             {
                 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
                 if( AVI_PacketNext( p_demux ) )
@@ -991,7 +1125,6 @@ static int Demux_Seekable( demux_t *p_demux )
                      * affect the reading speed too much. */
                     if( !(++i_loop_count % 1024) )
                     {
-                        if( !vlc_object_alive (p_demux) ) return -1;
                         msleep( 10000 );
 
                         if( !(i_loop_count % (1024 * 10)) )
@@ -1011,10 +1144,11 @@ 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 ? */
-                    if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
+                    if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -READ_LENGTH )
                     {
                         break;
                     }
@@ -1070,24 +1204,16 @@ 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;
+
+        p_frame->i_pts = VLC_TS_0 + AVI_GetPTS( tk );
         if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
         {
             p_frame->i_flags = BLOCK_FLAG_TYPE_I;
@@ -1167,20 +1293,20 @@ static int Demux_UnSeekable( demux_t *p_demux )
     unsigned int i_stream;
     unsigned int i_packet;
 
-    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
+    es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
 
     /* *** 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];
-        bool  b, b_error;
+        bool  b;
 
-        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b, &b_error );
+        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_error );
+            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
             b |= b_extra;
         }
 
@@ -1249,12 +1375,12 @@ static int Demux_UnSeekable( demux_t *p_demux )
                         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 );
                 }
-                p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
+                p_frame->i_pts = VLC_TS_0 + AVI_GetPTS( p_stream );
 
                 if( avi_pk.i_cat != VIDEO_ES )
                     p_frame->i_dts = p_frame->i_pts;
@@ -1304,23 +1430,52 @@ 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 )
     {
+        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 );
 
@@ -1342,14 +1497,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 +
@@ -1360,7 +1515,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;
                 }
             }
 
@@ -1379,10 +1534,18 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
 
             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: %"PRId64" seconds", p_sys->i_time /1000000 );
+        es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
+        es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + p_sys->i_time );
+        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
     {
@@ -1400,7 +1563,7 @@ 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 )
     {
@@ -1426,15 +1589,14 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             return VLC_SUCCESS;
         case DEMUX_SET_POSITION:
             f = (double)va_arg( args, double );
-            ifp_sys->b_seekable )
+            if ( !p_sys->b_seekable )
             {
-                i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
-                return Seek( p_demux, i64, (int)(f * 100) );
+                return VLC_EGENERIC;
             }
             else
             {
-                int64_t i_pos = stream_Size( p_demux->s ) * f;
-                return stream_Seek( p_demux->s, i_pos );
+                i64 = (mtime_t)(f * CLOCK_FREQ * p_sys->i_length);
+                return Seek( p_demux, i64, (int)(f * 100) );
             }
 
         case DEMUX_GET_TIME:
@@ -1447,9 +1609,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 )
             {
@@ -1460,7 +1626,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:
@@ -1514,7 +1680,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 )
 {
@@ -1605,8 +1771,6 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
 
     for( ;; )
     {
-        if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
-
         if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
         {
             msg_Warn( p_demux, "cannot get packet header" );
@@ -1625,7 +1789,6 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
              * affect the reading speed too much. */
             if( !(++i_loop_count % 1024) )
             {
-                if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
                 msleep( 10000 );
 
                 if( !(i_loop_count % (1024 * 10)) )
@@ -1642,6 +1805,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  )
@@ -2062,8 +2226,6 @@ static int AVI_PacketSearch( demux_t *p_demux )
          * this code is called only on broken files). */
         if( !(++i_count % 1024) )
         {
-            if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
-
             msleep( 10000 );
             if( !(i_count % (1024 * 10)) )
                 msg_Warn( p_demux, "trying to resync..." );
@@ -2159,11 +2321,22 @@ static int AVI_IndexFind_idx1( demux_t *p_demux,
             *pi_offset = 0;
         else
             *pi_offset = i_movi_content;
+
+        if( p_idx1->i_entry_count )
+        {
+            /* Invalidate offset if index refers past the data section to avoid false
+               positives when the offset equals sample size */
+            size_t i_dataend = *pi_offset + p_idx1->entry[p_idx1->i_entry_count - 1].i_pos +
+                                            p_idx1->entry[p_idx1->i_entry_count - 1].i_length;
+            if( i_dataend > p_movi->i_chunk_pos + p_movi->i_chunk_size )
+                *pi_offset = 0;
+        }
     }
     else
     {
         *pi_offset = 0;
     }
+
     return VLC_SUCCESS;
 }
 
@@ -2177,6 +2350,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 +2368,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 +2381,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 +2392,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 +2405,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 +2450,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++ )
             {
@@ -2392,9 +2574,6 @@ static void AVI_IndexCreate( demux_t *p_demux )
     {
         avi_packet_t pk;
 
-        if( !vlc_object_alive (p_demux) )
-            break;
-
         /* Don't update/check dialog too often */
         if( p_dialog && mdate() - i_dialog_update > 100000 )
         {
@@ -2422,6 +2601,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
@@ -2772,7 +2952,7 @@ static mtime_t  AVI_MovieGetLength( demux_t *p_demux )
         {
             i_length = AVI_GetDPTS( tk, tk->idx.i_size );
         }
-        i_length /= (mtime_t)1000000;    /* in seconds */
+        i_length /= CLOCK_FREQ;    /* in seconds */
 
         msg_Dbg( p_demux,
                  "stream[%d] length:%"PRId64" (based on index)",