]> git.sesse.net Git - vlc/blob - modules/demux/avi/avi.c
demux: avi: handle paletized bitmaps
[vlc] / modules / demux / avi / avi.c
1 /*****************************************************************************
2  * avi.c : AVI file Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
32 #include <ctype.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_input.h>
38
39 #include <vlc_dialog.h>
40
41 #include <vlc_meta.h>
42 #include <vlc_codecs.h>
43 #include <vlc_charset.h>
44 #include <vlc_memory.h>
45
46 #include "libavi.h"
47 #include "../rawdv.h"
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52
53 #define INTERLEAVE_TEXT N_("Force interleaved method" )
54
55 #define INDEX_TEXT N_("Force index creation")
56 #define INDEX_LONGTEXT N_( \
57     "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
58     "or incomplete (not seekable)." )
59
60 #define BI_RAWRGB 0x00
61 #define BI_RGBBITFIELDS 0x03
62
63 static int  Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
65
66 static const int pi_index[] = {0,1,2,3};
67
68 static const char *const ppsz_indexes[] = { N_("Ask for action"),
69                                             N_("Always fix"),
70                                             N_("Never fix"),
71                                             N_("Fix when necessary")};
72
73 vlc_module_begin ()
74     set_shortname( "AVI" )
75     set_description( N_("AVI demuxer") )
76     set_capability( "demux", 212 )
77     set_category( CAT_INPUT )
78     set_subcategory( SUBCAT_INPUT_DEMUX )
79
80     add_bool( "avi-interleaved", false,
81               INTERLEAVE_TEXT, INTERLEAVE_TEXT, true )
82     add_integer( "avi-index", 0,
83               INDEX_TEXT, INDEX_LONGTEXT, false )
84         change_integer_list( pi_index, ppsz_indexes )
85
86     set_callbacks( Open, Close )
87 vlc_module_end ()
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static int Control         ( demux_t *, int, va_list );
93 static int Seek            ( demux_t *, mtime_t, int );
94 static int Demux_Seekable  ( demux_t * );
95 static int Demux_UnSeekable( demux_t * );
96
97 static char *FromACP( const char *str )
98 {
99     return FromCharset(vlc_pgettext("GetACP", "CP1252"), str, strlen(str));
100 }
101
102 #define IGNORE_ES NAV_ES
103
104 typedef struct
105 {
106     vlc_fourcc_t i_fourcc;
107     off_t        i_pos;
108     uint32_t     i_size;
109     vlc_fourcc_t i_type;     /* only for AVIFOURCC_LIST */
110
111     uint8_t      i_peek[8];  /* first 8 bytes */
112
113     unsigned int i_stream;
114     unsigned int i_cat;
115 } avi_packet_t;
116
117
118 typedef struct
119 {
120     vlc_fourcc_t i_id;
121     uint32_t     i_flags;
122     off_t        i_pos;
123     uint32_t     i_length;
124     int64_t      i_lengthtotal;
125
126 } avi_entry_t;
127
128 typedef struct
129 {
130     unsigned int    i_size;
131     unsigned int    i_max;
132     avi_entry_t     *p_entry;
133
134 } avi_index_t;
135 static void avi_index_Init( avi_index_t * );
136 static void avi_index_Clean( avi_index_t * );
137 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
138
139 typedef struct
140 {
141     bool            b_activated;
142     bool            b_eof;
143
144     unsigned int    i_cat; /* AUDIO_ES, VIDEO_ES */
145     vlc_fourcc_t    i_codec;
146
147     int             i_rate;
148     int             i_scale;
149     unsigned int    i_samplesize;
150
151     unsigned int    i_width_bytes;
152     bool            b_flipped;
153
154     es_out_id_t     *p_es;
155
156     int             i_dv_audio_rate;
157     es_out_id_t     *p_es_dv_audio;
158
159     /* Avi Index */
160     avi_index_t     idx;
161
162     unsigned int    i_idxposc;  /* numero of chunk */
163     unsigned int    i_idxposb;  /* byte in the current chunk */
164
165     /* For VBR audio only */
166     unsigned int    i_blockno;
167     unsigned int    i_blocksize;
168
169 } avi_track_t;
170
171 struct demux_sys_t
172 {
173     mtime_t i_time;
174     mtime_t i_length;
175
176     bool  b_seekable;
177     avi_chunk_t ck_root;
178
179     bool  b_odml;
180
181     off_t   i_movi_begin;
182     off_t   i_movi_lastchunk_pos;   /* XXX position of last valid chunk */
183
184     /* number of streams and information */
185     unsigned int i_track;
186     avi_track_t  **track;
187
188     /* meta */
189     vlc_meta_t  *meta;
190
191     unsigned int       i_attachment;
192     input_attachment_t **attachment;
193 };
194
195 static inline off_t __EVEN( off_t i )
196 {
197     return (i & 1) ? i + 1 : i;
198 }
199
200 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
201 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
202 static mtime_t AVI_GetDPTS   ( avi_track_t *, int64_t i_count );
203 static mtime_t AVI_GetPTS    ( avi_track_t * );
204
205
206 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
207 static int AVI_StreamChunkSet ( demux_t *,
208                                 unsigned int i_stream, unsigned int i_ck );
209 static int AVI_StreamBytesSet ( demux_t *,
210                                 unsigned int i_stream, off_t   i_byte );
211
212 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
213 static int   AVI_GetKeyFlag    ( vlc_fourcc_t , uint8_t * );
214
215 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
216 static int AVI_PacketNext     ( demux_t * );
217 static int AVI_PacketRead     ( demux_t *, avi_packet_t *, block_t **);
218 static int AVI_PacketSearch   ( demux_t * );
219
220 static void AVI_IndexLoad    ( demux_t * );
221 static void AVI_IndexCreate  ( demux_t * );
222
223 static void AVI_ExtractSubtitle( demux_t *, unsigned int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
224
225 static void AVI_DvHandleAudio( demux_t *, avi_track_t *, block_t * );
226
227 static mtime_t  AVI_MovieGetLength( demux_t * );
228
229 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
230
231 block_t * ReadFrame( demux_t *p_demux, const avi_track_t *tk, const int i_size );
232
233 /*****************************************************************************
234  * Stream management
235  *****************************************************************************/
236 static int        AVI_TrackSeek  ( demux_t *, int, mtime_t );
237 static int        AVI_TrackStopFinishedStreams( demux_t *);
238
239 /* Remarks:
240  - For VBR mp3 stream:
241     count blocks by rounded-up chunksizes instead of chunks
242     we need full emulation of dshow avi demuxer bugs :(
243     fixes silly nandub-style a-v delaying in avi with vbr mp3...
244     (from mplayer 2002/08/02)
245  - to complete....
246  */
247
248 /*****************************************************************************
249  * Open: check file and initializes AVI structures
250  *****************************************************************************/
251 static int Open( vlc_object_t * p_this )
252 {
253     demux_t  *p_demux = (demux_t *)p_this;
254     demux_sys_t     *p_sys;
255
256     bool       b_index = false, b_aborted = false;
257     int              i_do_index;
258
259     avi_chunk_list_t    *p_riff;
260     avi_chunk_list_t    *p_hdrl, *p_movi;
261     avi_chunk_avih_t    *p_avih;
262
263     unsigned int i_track;
264     unsigned int i, i_peeker;
265
266     const uint8_t *p_peek;
267
268     /* Is it an avi file ? */
269     if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
270
271     for( i_peeker = 0; i_peeker < 188; i_peeker++ )
272     {
273         if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
274             break;
275         if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
276             break;
277         p_peek++;
278     }
279     if( i_peeker == 188 )
280     {
281         return VLC_EGENERIC;
282     }
283
284     /* Initialize input structures. */
285     p_sys = p_demux->p_sys = calloc( 1, sizeof(demux_sys_t) );
286     p_sys->b_odml   = false;
287     p_sys->track    = NULL;
288     p_sys->meta     = NULL;
289     TAB_INIT(p_sys->i_attachment, p_sys->attachment);
290
291     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
292
293     p_demux->pf_control = Control;
294     p_demux->pf_demux = Demux_Seekable;
295
296     /* For unseekable stream, automatically use Demux_UnSeekable */
297     if( !p_sys->b_seekable
298      || var_InheritBool( p_demux, "avi-interleaved" ) )
299     {
300         p_demux->pf_demux = Demux_UnSeekable;
301     }
302
303     if( i_peeker > 0 )
304     {
305         stream_Read( p_demux->s, NULL, i_peeker );
306     }
307
308     if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
309     {
310         msg_Err( p_demux, "avi module discarded (invalid file)" );
311         free(p_sys);
312         return VLC_EGENERIC;
313     }
314
315     if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
316     {
317         unsigned int i_count =
318             AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
319
320         msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
321         for( i = 1; i < i_count; i++ )
322         {
323             avi_chunk_list_t *p_sysx;
324
325             p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
326             if( p_sysx->i_type == AVIFOURCC_AVIX )
327             {
328                 msg_Warn( p_demux, "detected OpenDML file" );
329                 p_sys->b_odml = true;
330                 break;
331             }
332         }
333     }
334
335     p_riff  = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
336     p_hdrl  = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
337     p_movi  = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
338     if( !p_movi )
339         p_movi  = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_movi, 0 );
340
341     if( !p_hdrl || !p_movi )
342     {
343         msg_Err( p_demux, "invalid file: cannot find hdrl or movi chunks" );
344         goto error;
345     }
346
347     if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
348     {
349         msg_Err( p_demux, "invalid file: cannot find avih chunk" );
350         goto error;
351     }
352     i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
353     if( p_avih->i_streams != i_track )
354     {
355         msg_Warn( p_demux,
356                   "found %d stream but %d are declared",
357                   i_track, p_avih->i_streams );
358     }
359     if( i_track == 0 )
360     {
361         msg_Err( p_demux, "no stream defined!" );
362         goto error;
363     }
364
365     /* print information on streams */
366     msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
367              i_track,
368              p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
369              p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
370              p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
371              p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
372
373     AVI_MetaLoad( p_demux, p_riff, p_avih );
374
375     /* now read info on each stream and create ES */
376     for( i = 0 ; i < i_track; i++ )
377     {
378         avi_track_t           *tk     = calloc( 1, sizeof( avi_track_t ) );
379         if( unlikely( !tk ) )
380             goto error;
381
382         avi_chunk_list_t      *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
383         avi_chunk_strh_t      *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
384         avi_chunk_STRING_t    *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
385         avi_chunk_strf_auds_t *p_auds = NULL;
386         avi_chunk_strf_vids_t *p_vids = NULL;
387         es_format_t fmt;
388
389         tk->b_eof = false;
390         tk->b_activated = true;
391
392         p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
393         p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
394
395         if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
396         {
397             msg_Warn( p_demux, "stream[%d] incomplete", i );
398             free( tk );
399             continue;
400         }
401
402         tk->i_rate  = p_strh->i_rate;
403         tk->i_scale = p_strh->i_scale;
404         tk->i_samplesize = p_strh->i_samplesize;
405         msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
406                  i, tk->i_rate, tk->i_scale, tk->i_samplesize );
407
408         switch( p_strh->i_type )
409         {
410             case( AVIFOURCC_auds ):
411                 tk->i_cat   = AUDIO_ES;
412                 if( p_auds->p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
413                     p_auds->p_wf->cbSize >= sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) )
414                 {
415                     WAVEFORMATEXTENSIBLE *p_wfe = (WAVEFORMATEXTENSIBLE *)p_auds->p_wf;
416                     tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
417                                                       p_wfe->SubFormat.Data1 );
418                 }
419                 else
420                     tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
421                                                       p_auds->p_wf->wFormatTag );
422
423                 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
424                 if( tk->i_blocksize == 0 )
425                 {
426                     if( p_auds->p_wf->wFormatTag == 1 )
427                         tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
428                     else
429                         tk->i_blocksize = 1;
430                 }
431                 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
432                 {
433                     msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
434                                        "Using blocksize as a workaround.",
435                                        i, tk->i_samplesize, tk->i_blocksize );
436                     tk->i_samplesize = tk->i_blocksize;
437                 }
438
439                 if( tk->i_codec == VLC_CODEC_VORBIS )
440                 {
441                     tk->i_blocksize = 0; /* fix vorbis VBR decoding */
442                 }
443
444                 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
445
446                 fmt.audio.i_channels        = p_auds->p_wf->nChannels;
447                 fmt.audio.i_rate            = p_auds->p_wf->nSamplesPerSec;
448                 fmt.i_bitrate               = p_auds->p_wf->nAvgBytesPerSec*8;
449                 fmt.audio.i_blockalign      = p_auds->p_wf->nBlockAlign;
450                 fmt.audio.i_bitspersample   = p_auds->p_wf->wBitsPerSample;
451                 fmt.b_packetized            = !tk->i_blocksize;
452
453                 msg_Dbg( p_demux,
454                     "stream[%d] audio(0x%x - %s) %d channels %dHz %dbits",
455                     i, p_auds->p_wf->wFormatTag,vlc_fourcc_GetDescription(AUDIO_ES,tk->i_codec),
456                     p_auds->p_wf->nChannels,
457                     p_auds->p_wf->nSamplesPerSec,
458                     p_auds->p_wf->wBitsPerSample );
459
460                 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
461                     p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
462                 if( fmt.i_extra > 0 )
463                 {
464                     fmt.p_extra = malloc( fmt.i_extra );
465                     if( !fmt.p_extra ) goto error;
466                     memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
467                 }
468                 break;
469
470             case( AVIFOURCC_vids ):
471             {
472                 tk->i_cat   = VIDEO_ES;
473                 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
474                                                   p_vids->p_bih->biCompression );
475                 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
476                 {
477                    msg_Dbg( p_demux, "stream[%d] subtitles", i );
478                    es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
479                    tk->i_cat = SPU_ES;
480                    break;
481                 }
482                 else if( p_vids->p_bih->biCompression == BI_RAWRGB )
483                 {
484                     switch( p_vids->p_bih->biBitCount )
485                     {
486                         case 32:
487                             tk->i_codec = VLC_CODEC_RGB32;
488                             break;
489                         case 24:
490                             tk->i_codec = VLC_CODEC_RGB24;
491                             break;
492                         case 16: /* Yes it is RV15 */
493                         case 15:
494                             tk->i_codec = VLC_CODEC_RGB15;
495                             break;
496                         case 9: /* <- TODO check that */
497                             tk->i_codec = VLC_CODEC_I410;
498                             break;
499                         case 8:
500                             if ( p_vids->p_bih->biClrUsed )
501                                 tk->i_codec = VLC_CODEC_RGBP;
502                             else
503                                 tk->i_codec = VLC_CODEC_GREY;
504                             break;
505                     }
506                     es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
507
508                     switch( tk->i_codec )
509                     {
510                     case VLC_CODEC_RGB24:
511                     case VLC_CODEC_RGB32:
512                         fmt.video.i_rmask = 0x00ff0000;
513                         fmt.video.i_gmask = 0x0000ff00;
514                         fmt.video.i_bmask = 0x000000ff;
515                         break;
516                     case VLC_CODEC_RGB15:
517                         fmt.video.i_rmask = 0x7c00;
518                         fmt.video.i_gmask = 0x03e0;
519                         fmt.video.i_bmask = 0x001f;
520                         break;
521                     case VLC_CODEC_RGBP:
522                     {
523                         const VLC_BITMAPINFO *p_bi = (const VLC_BITMAPINFO *) p_vids->p_bih;
524                         fmt.video.p_palette = malloc( sizeof(video_palette_t) );
525                         if ( fmt.video.p_palette )
526                         {
527                             uint32_t entry;
528                             for ( uint32_t i=0; i<p_vids->p_bih->biClrUsed; i++ )
529                             {
530                                  entry = GetDWBE( &p_bi->bmiColors[i] );
531                                  fmt.video.p_palette->palette[i][0] = entry >> 24;
532                                  fmt.video.p_palette->palette[i][1] = (entry >> 16) & 0xFF;
533                                  fmt.video.p_palette->palette[i][2] = (entry >> 8) & 0xFF;
534                                  fmt.video.p_palette->palette[i][3] = entry & 0xFF;
535                             }
536                             fmt.video.p_palette->i_entries = p_vids->p_bih->biClrUsed;
537                         }
538                     }
539                         break;
540                     default:
541                         break;
542                     }
543
544                     tk->i_width_bytes = p_vids->p_bih->biWidth * (p_vids->p_bih->biBitCount >> 3);
545                     /* RGB DIB are coded from bottom to top */
546                     if ( p_vids->p_bih->biHeight < INT32_MAX ) tk->b_flipped = true;
547                 }
548                 else
549                 {
550                     es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
551                     if( tk->i_codec == VLC_CODEC_MP4V &&
552                         !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
553                     {
554                         fmt.i_codec           =
555                         fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
556                     }
557                 }
558                 tk->i_samplesize = 0;
559
560                 fmt.video.i_width  = p_vids->p_bih->biWidth;
561                 fmt.video.i_height = p_vids->p_bih->biHeight;
562                 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
563                 fmt.video.i_frame_rate = tk->i_rate;
564                 fmt.video.i_frame_rate_base = tk->i_scale;
565
566                  /* Uncompresse Bitmap or YUV, YUV being always topdown */
567                 if ( fmt.video.i_height > INT32_MAX )
568                     fmt.video.i_height =
569                         (unsigned int)(-(int)p_vids->p_bih->biHeight);
570
571                 avi_chunk_vprp_t *p_vprp = AVI_ChunkFind( p_strl, AVIFOURCC_vprp, 0 );
572                 if( p_vprp )
573                 {
574                     uint32_t i_frame_aspect_ratio = p_vprp->i_frame_aspect_ratio;
575                     if( p_vprp->i_video_format_token >= 1 &&
576                         p_vprp->i_video_format_token <= 4 )
577                         i_frame_aspect_ratio = 0x00040003;
578                     fmt.video.i_sar_num = ((i_frame_aspect_ratio >> 16) & 0xffff) * fmt.video.i_height;
579                     fmt.video.i_sar_den = ((i_frame_aspect_ratio >>  0) & 0xffff) * fmt.video.i_width;
580                 }
581                 /* Extradata is the remainder of the chunk less the BIH */
582                 fmt.i_extra = p_vids->i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
583                 if( fmt.i_extra > 0 )
584                 {
585                     fmt.p_extra = malloc( fmt.i_extra );
586                     if( !fmt.p_extra ) goto error;
587                     memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
588                 }
589
590                 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
591                          i, (char*)&p_vids->p_bih->biCompression,
592                          (uint32_t)p_vids->p_bih->biWidth,
593                          (uint32_t)p_vids->p_bih->biHeight,
594                          p_vids->p_bih->biBitCount,
595                          (float)tk->i_rate/(float)tk->i_scale );
596
597                 /* Extract palette from extradata if bpp <= 8 */
598                 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
599                 {
600                     /* The palette should not be included in biSize, but come
601                      * directly after BITMAPINFORHEADER in the BITMAPINFO structure */
602                     if( fmt.i_extra > 0 && fmt.p_extra )
603                     {
604                         const uint8_t *p_pal = fmt.p_extra;
605
606                         fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
607                         fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
608
609                         for( int k = 0; k < fmt.video.p_palette->i_entries; k++ )
610                         {
611                             for( int j = 0; j < 4; j++ )
612                                 fmt.video.p_palette->palette[k][j] = p_pal[4*k+j];
613                         }
614                     }
615                 }
616                 break;
617             }
618
619             case( AVIFOURCC_txts):
620                 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
621                 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
622                 free( tk );
623                 continue;
624
625             case( AVIFOURCC_iavs):
626             case( AVIFOURCC_ivas):
627                 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
628                 tk->i_cat   = VIDEO_ES;
629                 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES, p_strh->i_handler );
630                 tk->i_samplesize = 0;
631                 tk->i_dv_audio_rate = tk->i_codec == VLC_CODEC_DV ? -1 : 0;
632
633                 es_format_Init( &fmt, VIDEO_ES, p_strh->i_handler );
634                 fmt.video.i_width  = p_avih->i_width;
635                 fmt.video.i_height = p_avih->i_height;
636                 break;
637
638             case( AVIFOURCC_mids):
639                 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
640
641             default:
642                 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
643                 free( tk );
644                 continue;
645         }
646         if( p_strn )
647             fmt.psz_description = FromACP( p_strn->p_str );
648         tk->p_es = es_out_Add( p_demux->out, &fmt );
649         TAB_APPEND( p_sys->i_track, p_sys->track, tk );
650         es_format_Clean( &fmt );
651     }
652
653     if( p_sys->i_track <= 0 )
654     {
655         msg_Err( p_demux, "no valid track" );
656         goto error;
657     }
658
659     i_do_index = var_InheritInteger( p_demux, "avi-index" );
660     if( i_do_index == 1 ) /* Always fix */
661     {
662 aviindex:
663         if( p_sys->b_seekable )
664         {
665             AVI_IndexCreate( p_demux );
666         }
667         else
668         {
669             msg_Warn( p_demux, "cannot create index (unseekable stream)" );
670             AVI_IndexLoad( p_demux );
671         }
672     }
673     else
674     {
675         AVI_IndexLoad( p_demux );
676     }
677
678     /* *** movie length in sec *** */
679     p_sys->i_length = AVI_MovieGetLength( p_demux );
680
681     /* Check the index completeness */
682     unsigned int i_idx_totalframes = 0;
683     for( unsigned int i = 0; i < p_sys->i_track; i++ )
684     {
685         const avi_track_t *tk = p_sys->track[i];
686         if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
687             i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
688     }
689     if( i_idx_totalframes != p_avih->i_totalframes &&
690         p_sys->i_length < (mtime_t)p_avih->i_totalframes *
691                           (mtime_t)p_avih->i_microsecperframe /
692                           (mtime_t)1000000 )
693     {
694         if( !vlc_object_alive( p_demux) )
695             goto error;
696
697         msg_Warn( p_demux, "broken or missing index, 'seek' will be "
698                            "approximative or will exhibit strange behavior" );
699         if( (i_do_index == 0 || i_do_index == 3) && !b_index )
700         {
701             if( !p_sys->b_seekable ) {
702                 b_index = true;
703                 goto aviindex;
704             }
705             if( i_do_index == 0 )
706             {
707                 switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
708                    _( "Because this AVI file index is broken or missing, "
709                       "seeking will not work correctly.\n"
710                       "VLC won't repair your file but can temporary fix this "
711                       "problem by building an index in memory.\n"
712                       "This step might take a long time on a large file.\n"
713                       "What do you want to do?" ),
714                       _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
715                 {
716                     case 1:
717                         b_index = true;
718                         msg_Dbg( p_demux, "Fixing AVI index" );
719                         goto aviindex;
720                     case 3:
721                         b_aborted = true;
722                         goto error;
723                 }
724             }
725             else
726             {
727                 b_index = true;
728                 msg_Dbg( p_demux, "Fixing AVI index" );
729                 goto aviindex;
730             }
731         }
732     }
733
734     /* fix some BeOS MediaKit generated file */
735     for( i = 0 ; i < p_sys->i_track; i++ )
736     {
737         avi_track_t         *tk = p_sys->track[i];
738         avi_chunk_list_t    *p_strl;
739         avi_chunk_strf_auds_t    *p_auds;
740
741         if( tk->i_cat != AUDIO_ES )
742         {
743             continue;
744         }
745         if( tk->idx.i_size < 1 ||
746             tk->i_scale != 1 ||
747             tk->i_samplesize != 0 )
748         {
749             continue;
750         }
751         p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
752         p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
753
754         if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
755             (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
756         {
757             int64_t i_track_length =
758                 tk->idx.p_entry[tk->idx.i_size-1].i_length +
759                 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
760             mtime_t i_length = (mtime_t)p_avih->i_totalframes *
761                                (mtime_t)p_avih->i_microsecperframe;
762
763             if( i_length == 0 )
764             {
765                 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
766                 continue;
767             }
768             tk->i_samplesize = 1;
769             tk->i_rate       = i_track_length  * (int64_t)1000000/ i_length;
770             msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
771         }
772     }
773
774     if( p_sys->b_seekable )
775     {
776         /* we have read all chunk so go back to movi */
777         stream_Seek( p_demux->s, p_movi->i_chunk_pos );
778     }
779     /* Skip movi header */
780     stream_Read( p_demux->s, NULL, 12 );
781
782     p_sys->i_movi_begin = p_movi->i_chunk_pos;
783     return VLC_SUCCESS;
784
785 error:
786     for( unsigned i = 0; i < p_sys->i_attachment; i++)
787         vlc_input_attachment_Delete(p_sys->attachment[i]);
788     free(p_sys->attachment);
789
790     if( p_sys->meta )
791         vlc_meta_Delete( p_sys->meta );
792
793     AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
794     free( p_sys );
795     return b_aborted ? VLC_ETIMEOUT : VLC_EGENERIC;
796 }
797
798 /*****************************************************************************
799  * Close: frees unused data
800  *****************************************************************************/
801 static void Close ( vlc_object_t * p_this )
802 {
803     demux_t *    p_demux = (demux_t *)p_this;
804     demux_sys_t *p_sys = p_demux->p_sys  ;
805
806     for( unsigned int i = 0; i < p_sys->i_track; i++ )
807     {
808         if( p_sys->track[i] )
809         {
810             avi_index_Clean( &p_sys->track[i]->idx );
811             free( p_sys->track[i] );
812         }
813     }
814     free( p_sys->track );
815
816     AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
817     vlc_meta_Delete( p_sys->meta );
818
819     for( unsigned i = 0; i < p_sys->i_attachment; i++)
820         vlc_input_attachment_Delete(p_sys->attachment[i]);
821     free(p_sys->attachment);
822
823     free( p_sys );
824 }
825
826 /*****************************************************************************
827  * ReadFrame: Reads frame, using stride if necessary
828  *****************************************************************************/
829
830 block_t * ReadFrame( demux_t *p_demux, const avi_track_t *tk, const int i_size )
831 {
832     block_t *p_frame = stream_Block( p_demux->s, __EVEN( i_size ) );
833     if ( !p_frame ) return p_frame;
834
835     const uint8_t i_header = ( tk->i_idxposb == 0 ) ? 8 : 0;
836
837     if( i_size % 2 )    /* read was padded on word boundary */
838     {
839         p_frame->i_buffer--;
840     }
841
842     /* skip header */
843     if( tk->i_idxposb == 0 )
844     {
845         p_frame->p_buffer += i_header;
846         p_frame->i_buffer -= i_header;
847     }
848
849     if ( !tk->i_width_bytes )
850         return p_frame;
851
852     const unsigned int i_stride_bytes = ((( (tk->i_width_bytes << 3) + 31) & ~31) >> 3);
853
854     if ( p_frame->i_buffer < i_stride_bytes )
855     {
856         p_frame->i_buffer = 0;
857         return p_frame;
858     }
859
860     if( !tk->b_flipped )
861     {
862         const uint8_t *p_src = p_frame->p_buffer + i_stride_bytes;
863         const uint8_t *p_end = p_frame->p_buffer + p_frame->i_buffer;
864         uint8_t *p_dst = p_frame->p_buffer + tk->i_width_bytes;
865
866         p_frame->i_buffer = tk->i_width_bytes;
867
868         while ( p_src + i_stride_bytes <= p_end )
869         {
870             memmove( p_dst, p_src, tk->i_width_bytes );
871             p_src += i_stride_bytes;
872             p_dst += tk->i_width_bytes;
873             p_frame->i_buffer += tk->i_width_bytes;
874         }
875     }
876     else
877     {
878         block_t *p_flippedframe = block_Alloc( p_frame->i_buffer );
879         if ( !p_flippedframe )
880         {
881             block_Release( p_frame );
882             return NULL;
883         }
884
885         unsigned int i_lines = p_frame->i_buffer / i_stride_bytes;
886         const uint8_t *p_src = p_frame->p_buffer + i_lines * i_stride_bytes;
887         uint8_t *p_dst = p_flippedframe->p_buffer;
888
889         p_flippedframe->i_buffer = 0;
890
891         while ( i_lines-- > 0 )
892         {
893             p_src -= i_stride_bytes;
894             memcpy( p_dst, p_src, tk->i_width_bytes );
895             p_dst += tk->i_width_bytes;
896             p_flippedframe->i_buffer += tk->i_width_bytes;
897         }
898
899         block_Release( p_frame );
900         p_frame = p_flippedframe;
901     }
902
903     return p_frame;
904 }
905
906 /*****************************************************************************
907  * Demux_Seekable: reads and demuxes data packets for stream seekable
908  *****************************************************************************
909  * AVIDemux: reads and demuxes data packets
910  *****************************************************************************
911  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
912  *****************************************************************************/
913 typedef struct
914 {
915     bool b_ok;
916
917     int i_toread;
918
919     off_t i_posf; /* where we will read :
920                    if i_idxposb == 0 : begining of chunk (+8 to acces data)
921                    else : point on data directly */
922 } avi_track_toread_t;
923
924 static int Demux_Seekable( demux_t *p_demux )
925 {
926     demux_sys_t *p_sys = p_demux->p_sys;
927
928     unsigned int i_track_count = 0;
929     unsigned int i_track;
930     /* cannot be more than 100 stream (dcXX or wbXX) */
931     avi_track_toread_t toread[100];
932
933
934     /* detect new selected/unselected streams */
935     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
936     {
937         avi_track_t *tk = p_sys->track[i_track];
938         bool  b;
939
940         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
941         if( tk->p_es_dv_audio )
942         {
943             bool b_extra;
944             es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
945             b |= b_extra;
946         }
947         if( b && !tk->b_activated )
948         {
949             if( p_sys->b_seekable)
950             {
951                 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
952             }
953             tk->b_activated = true;
954         }
955         else if( !b && tk->b_activated )
956         {
957             tk->b_activated = false;
958         }
959         if( b )
960         {
961             i_track_count++;
962         }
963     }
964
965     if( i_track_count <= 0 )
966     {
967         int64_t i_length = p_sys->i_length * (mtime_t)1000000;
968
969         p_sys->i_time += 25*1000;  /* read 25ms */
970         if( i_length > 0 )
971         {
972             if( p_sys->i_time >= i_length )
973                 return 0;
974             return 1;
975         }
976         msg_Warn( p_demux, "no track selected, exiting..." );
977         return 0;
978     }
979
980     /* wait for the good time */
981     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
982     p_sys->i_time += 25*1000;  /* read 25ms */
983
984     /* init toread */
985     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
986     {
987         avi_track_t *tk = p_sys->track[i_track];
988         mtime_t i_dpts;
989
990         toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
991         if( tk->i_idxposc < tk->idx.i_size )
992         {
993             toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
994            if( tk->i_idxposb > 0 )
995            {
996                 toread[i_track].i_posf += 8 + tk->i_idxposb;
997            }
998         }
999         else
1000         {
1001             toread[i_track].i_posf = -1;
1002         }
1003
1004         i_dpts = p_sys->i_time - AVI_GetPTS( tk  );
1005
1006         if( tk->i_samplesize )
1007         {
1008             toread[i_track].i_toread = AVI_PTSToByte( tk, llabs( i_dpts ) );
1009         }
1010         else
1011         {
1012             toread[i_track].i_toread = AVI_PTSToChunk( tk, llabs( i_dpts ) );
1013         }
1014
1015         if( i_dpts < 0 )
1016         {
1017             toread[i_track].i_toread *= -1;
1018         }
1019     }
1020
1021     for( ;; )
1022     {
1023         avi_track_t     *tk;
1024         bool       b_done;
1025         block_t         *p_frame;
1026         off_t i_pos;
1027         unsigned int i;
1028         size_t i_size;
1029
1030         /* search for first chunk to be read */
1031         for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
1032         {
1033             if( !toread[i].b_ok ||
1034                 AVI_GetDPTS( p_sys->track[i],
1035                              toread[i].i_toread ) <= -25 * 1000 )
1036             {
1037                 continue;
1038             }
1039
1040             if( toread[i].i_toread > 0 )
1041             {
1042                 b_done = false; /* not yet finished */
1043             }
1044             if( toread[i].i_posf > 0 )
1045             {
1046                 if( i_pos == -1 || i_pos > toread[i].i_posf )
1047                 {
1048                     i_track = i;
1049                     i_pos = toread[i].i_posf;
1050                 }
1051             }
1052         }
1053
1054         if( b_done )
1055         {
1056             for( i = 0; i < p_sys->i_track; i++ )
1057             {
1058                 if( toread[i].b_ok )
1059                     return 1;
1060             }
1061             msg_Warn( p_demux, "all tracks have failed, exiting..." );
1062             return 0;
1063         }
1064
1065         if( i_pos == -1 )
1066         {
1067             int i_loop_count = 0;
1068
1069             /* no valid index, we will parse directly the stream
1070              * in case we fail we will disable all finished stream */
1071             if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1072             {
1073                 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1074                 if( AVI_PacketNext( p_demux ) )
1075                 {
1076                     return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1077                 }
1078             }
1079             else
1080             {
1081                 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1082             }
1083
1084             for( ;; )
1085             {
1086                 avi_packet_t avi_pk;
1087
1088                 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1089                 {
1090                     msg_Warn( p_demux,
1091                              "cannot get packet header, track disabled" );
1092                     return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1093                 }
1094                 if( avi_pk.i_stream >= p_sys->i_track ||
1095                     ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1096                 {
1097                     if( AVI_PacketNext( p_demux ) )
1098                     {
1099                         msg_Warn( p_demux,
1100                                   "cannot skip packet, track disabled" );
1101                         return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1102                     }
1103
1104                     /* Prevents from eating all the CPU with broken files.
1105                      * This value should be low enough so that it doesn't
1106                      * affect the reading speed too much. */
1107                     if( !(++i_loop_count % 1024) )
1108                     {
1109                         if( !vlc_object_alive (p_demux) ) return -1;
1110                         msleep( 10000 );
1111
1112                         if( !(i_loop_count % (1024 * 10)) )
1113                             msg_Warn( p_demux,
1114                                       "don't seem to find any data..." );
1115                     }
1116                     continue;
1117                 }
1118                 else
1119                 {
1120                     i_track = avi_pk.i_stream;
1121                     tk = p_sys->track[i_track];
1122
1123                     /* add this chunk to the index */
1124                     avi_entry_t index;
1125                     index.i_id     = avi_pk.i_fourcc;
1126                     index.i_flags  = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
1127                     index.i_pos    = avi_pk.i_pos;
1128                     index.i_length = avi_pk.i_size;
1129                     index.i_lengthtotal = index.i_length;
1130                     avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1131
1132                     /* do we will read this data ? */
1133                     if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1134                     {
1135                         break;
1136                     }
1137                     else
1138                     {
1139                         if( AVI_PacketNext( p_demux ) )
1140                         {
1141                             msg_Warn( p_demux,
1142                                       "cannot skip packet, track disabled" );
1143                             return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1144                         }
1145                     }
1146                 }
1147             }
1148
1149         }
1150         else
1151         {
1152             stream_Seek( p_demux->s, i_pos );
1153         }
1154
1155         /* Set the track to use */
1156         tk = p_sys->track[i_track];
1157
1158         /* read thoses data */
1159         if( tk->i_samplesize )
1160         {
1161             unsigned int i_toread;
1162
1163             if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1164             {
1165                 if( tk->i_samplesize > 1 )
1166                 {
1167                     i_toread = tk->i_samplesize;
1168                 }
1169                 else
1170                 {
1171                     i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1172                     i_toread = __MAX( i_toread, 100 );
1173                 }
1174             }
1175             i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1176                                 tk->i_idxposb,
1177                             i_toread );
1178         }
1179         else
1180         {
1181             i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1182         }
1183
1184         if( tk->i_idxposb == 0 )
1185         {
1186             i_size += 8; /* need to read and skip header */
1187         }
1188
1189         if( ( p_frame = ReadFrame( p_demux, tk, i_size ) )==NULL )
1190         {
1191             msg_Warn( p_demux, "failed reading data" );
1192             tk->b_eof = false;
1193             toread[i_track].b_ok = false;
1194             continue;
1195         }
1196
1197         p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1198         if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1199         {
1200             p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1201         }
1202         else
1203         {
1204             p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1205         }
1206
1207         /* read data */
1208         if( tk->i_samplesize )
1209         {
1210             if( tk->i_idxposb == 0 )
1211             {
1212                 i_size -= 8;
1213             }
1214             toread[i_track].i_toread -= i_size;
1215             tk->i_idxposb += i_size;
1216             if( tk->i_idxposb >=
1217                     tk->idx.p_entry[tk->i_idxposc].i_length )
1218             {
1219                 tk->i_idxposb = 0;
1220                 tk->i_idxposc++;
1221             }
1222         }
1223         else
1224         {
1225             int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1226
1227             tk->i_idxposc++;
1228             if( tk->i_cat == AUDIO_ES )
1229             {
1230                 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1231             }
1232             toread[i_track].i_toread--;
1233         }
1234
1235         if( tk->i_idxposc < tk->idx.i_size)
1236         {
1237             toread[i_track].i_posf =
1238                 tk->idx.p_entry[tk->i_idxposc].i_pos;
1239             if( tk->i_idxposb > 0 )
1240             {
1241                 toread[i_track].i_posf += 8 + tk->i_idxposb;
1242             }
1243
1244         }
1245         else
1246         {
1247             toread[i_track].i_posf = -1;
1248         }
1249
1250         if( tk->i_cat != VIDEO_ES )
1251             p_frame->i_dts = p_frame->i_pts;
1252         else
1253         {
1254             p_frame->i_dts = p_frame->i_pts;
1255             p_frame->i_pts = VLC_TS_INVALID;
1256         }
1257
1258         if( tk->i_dv_audio_rate )
1259             AVI_DvHandleAudio( p_demux, tk, p_frame );
1260         es_out_Send( p_demux->out, tk->p_es, p_frame );
1261     }
1262 }
1263
1264
1265 /*****************************************************************************
1266  * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1267  *****************************************************************************
1268  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1269  *****************************************************************************/
1270 static int Demux_UnSeekable( demux_t *p_demux )
1271 {
1272     demux_sys_t     *p_sys = p_demux->p_sys;
1273     avi_track_t *p_stream_master = NULL;
1274     unsigned int i_stream;
1275     unsigned int i_packet;
1276
1277     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1278
1279     /* *** find master stream for data packet skipping algo *** */
1280     /* *** -> first video, if any, or first audio ES *** */
1281     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1282     {
1283         avi_track_t *tk = p_sys->track[i_stream];
1284         bool  b;
1285
1286         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1287         if( tk->p_es_dv_audio )
1288         {
1289             bool b_extra;
1290             es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
1291             b |= b_extra;
1292         }
1293
1294         if( b && tk->i_cat == VIDEO_ES )
1295         {
1296             p_stream_master = tk;
1297         }
1298         else if( b )
1299         {
1300             p_stream_master = tk;
1301         }
1302     }
1303
1304     if( !p_stream_master )
1305     {
1306         msg_Warn( p_demux, "no more stream selected" );
1307         return( 0 );
1308     }
1309
1310     p_sys->i_time = AVI_GetPTS( p_stream_master );
1311
1312     for( i_packet = 0; i_packet < 10; i_packet++)
1313     {
1314 #define p_stream    p_sys->track[avi_pk.i_stream]
1315
1316         avi_packet_t    avi_pk;
1317
1318         if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1319         {
1320             return( 0 );
1321         }
1322
1323         if( avi_pk.i_stream >= p_sys->i_track ||
1324             ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1325         {
1326             /* we haven't found an audio or video packet:
1327              *  - we have seek, found first next packet
1328              *  - others packets could be found, skip them
1329              */
1330             switch( avi_pk.i_fourcc )
1331             {
1332                 case AVIFOURCC_JUNK:
1333                 case AVIFOURCC_LIST:
1334                 case AVIFOURCC_RIFF:
1335                     return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1336                 case AVIFOURCC_idx1:
1337                     if( p_sys->b_odml )
1338                     {
1339                         return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1340                     }
1341                     return( 0 );    /* eof */
1342                 default:
1343                     msg_Warn( p_demux,
1344                               "seems to have lost position, resync" );
1345                     if( AVI_PacketSearch( p_demux ) )
1346                     {
1347                         msg_Err( p_demux, "resync failed" );
1348                         return( -1 );
1349                     }
1350             }
1351         }
1352         else
1353         {
1354             /* check for time */
1355             if( llabs( AVI_GetPTS( p_stream ) -
1356                         AVI_GetPTS( p_stream_master ) )< 600*1000 )
1357             {
1358                 /* load it and send to decoder */
1359                 block_t *p_frame;
1360                 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1361                 {
1362                     return( -1 );
1363                 }
1364                 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1365
1366                 if( avi_pk.i_cat != VIDEO_ES )
1367                     p_frame->i_dts = p_frame->i_pts;
1368                 else
1369                 {
1370                     p_frame->i_dts = p_frame->i_pts;
1371                     p_frame->i_pts = VLC_TS_INVALID;
1372                 }
1373
1374                 if( p_stream->i_dv_audio_rate )
1375                     AVI_DvHandleAudio( p_demux, p_stream, p_frame );
1376                 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1377             }
1378             else
1379             {
1380                 if( AVI_PacketNext( p_demux ) )
1381                 {
1382                     return( 0 );
1383                 }
1384             }
1385
1386             /* *** update stream time position *** */
1387             if( p_stream->i_samplesize )
1388             {
1389                 p_stream->i_idxposb += avi_pk.i_size;
1390             }
1391             else
1392             {
1393                 if( p_stream->i_cat == AUDIO_ES )
1394                 {
1395                     p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1396                 }
1397                 p_stream->i_idxposc++;
1398             }
1399
1400         }
1401 #undef p_stream
1402     }
1403
1404     return( 1 );
1405 }
1406
1407 /*****************************************************************************
1408  * Seek: goto to i_date or i_percent
1409  *****************************************************************************/
1410 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1411 {
1412     demux_sys_t *p_sys = p_demux->p_sys;
1413     msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1414              i_date / 1000000, i_percent );
1415
1416     if( p_sys->b_seekable )
1417     {
1418         if( !p_sys->i_length )
1419         {
1420             avi_track_t *p_stream = NULL;
1421             unsigned i_stream = 0;
1422             int64_t i_pos;
1423
1424             /* use i_percent to create a true i_date */
1425             msg_Warn( p_demux, "seeking without index at %d%%"
1426                       " only works for interleaved files", i_percent );
1427             if( i_percent >= 100 )
1428             {
1429                 msg_Warn( p_demux, "cannot seek so far !" );
1430                 return VLC_EGENERIC;
1431             }
1432             i_percent = __MAX( i_percent, 0 );
1433
1434             /* try to find chunk that is at i_percent or the file */
1435             i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1436                            p_sys->i_movi_begin );
1437             /* search first selected stream (and prefer non-EOF ones) */
1438             for( unsigned i = 0; i < p_sys->i_track; i++ )
1439             {
1440                 avi_track_t *p_track = p_sys->track[i];
1441                 if( !p_track->b_activated )
1442                     continue;
1443
1444                 p_stream = p_track;
1445                 i_stream = i;
1446                 if( !p_track->b_eof )
1447                     break;
1448             }
1449             if( p_stream == NULL )
1450             {
1451                 msg_Warn( p_demux, "cannot find any selected stream" );
1452                 return VLC_EGENERIC;
1453             }
1454
1455             /* be sure that the index exist */
1456             if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1457             {
1458                 msg_Warn( p_demux, "cannot seek" );
1459                 return VLC_EGENERIC;
1460             }
1461
1462             while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1463                p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1464             {
1465                 /* search after i_idxposc */
1466                 if( AVI_StreamChunkSet( p_demux,
1467                                         i_stream, p_stream->i_idxposc + 1 ) )
1468                 {
1469                     msg_Warn( p_demux, "cannot seek" );
1470                     return VLC_EGENERIC;
1471                 }
1472             }
1473
1474             i_date = AVI_GetPTS( p_stream );
1475             /* TODO better support for i_samplesize != 0 */
1476             msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1477         }
1478
1479         /* */
1480         for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1481         {
1482             avi_track_t *p_stream = p_sys->track[i_stream];
1483
1484             if( !p_stream->b_activated )
1485                 continue;
1486
1487             p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1488         }
1489         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1490         p_sys->i_time = i_date;
1491         msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1492         return VLC_SUCCESS;
1493     }
1494     else
1495     {
1496         msg_Err( p_demux, "shouldn't yet be executed" );
1497         return VLC_EGENERIC;
1498     }
1499 }
1500
1501 /*****************************************************************************
1502  * Control:
1503  *****************************************************************************/
1504 static double ControlGetPosition( demux_t *p_demux )
1505 {
1506     demux_sys_t *p_sys = p_demux->p_sys;
1507
1508     if( p_sys->i_length > 0 )
1509     {
1510         return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1511     }
1512     else if( stream_Size( p_demux->s ) > 0 )
1513     {
1514         double i64 = (uint64_t)stream_Tell( p_demux->s );
1515         return i64 / stream_Size( p_demux->s );
1516     }
1517     return 0.0;
1518 }
1519
1520 static int Control( demux_t *p_demux, int i_query, va_list args )
1521 {
1522     demux_sys_t *p_sys = p_demux->p_sys;
1523     int i;
1524     double   f, *pf;
1525     int64_t i64, *pi64;
1526     vlc_meta_t *p_meta;
1527
1528     switch( i_query )
1529     {
1530         case DEMUX_GET_POSITION:
1531             pf = (double*)va_arg( args, double * );
1532             *pf = ControlGetPosition( p_demux );
1533             return VLC_SUCCESS;
1534         case DEMUX_SET_POSITION:
1535             f = (double)va_arg( args, double );
1536             if( p_sys->b_seekable )
1537             {
1538                 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1539                 return Seek( p_demux, i64, (int)(f * 100) );
1540             }
1541             else
1542             {
1543                 int64_t i_pos = stream_Size( p_demux->s ) * f;
1544                 return stream_Seek( p_demux->s, i_pos );
1545             }
1546
1547         case DEMUX_GET_TIME:
1548             pi64 = (int64_t*)va_arg( args, int64_t * );
1549             *pi64 = p_sys->i_time;
1550             return VLC_SUCCESS;
1551
1552         case DEMUX_SET_TIME:
1553         {
1554             int i_percent = 0;
1555
1556             i64 = (int64_t)va_arg( args, int64_t );
1557             if( p_sys->i_length > 0 )
1558             {
1559                 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1560             }
1561             else if( p_sys->i_time > 0 )
1562             {
1563                 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1564                                    (double)i64 / (double)p_sys->i_time );
1565             }
1566             return Seek( p_demux, i64, i_percent );
1567         }
1568         case DEMUX_GET_LENGTH:
1569             pi64 = (int64_t*)va_arg( args, int64_t * );
1570             *pi64 = p_sys->i_length * (mtime_t)1000000;
1571             return VLC_SUCCESS;
1572
1573         case DEMUX_GET_FPS:
1574             pf = (double*)va_arg( args, double * );
1575             *pf = 0.0;
1576             for( i = 0; i < (int)p_sys->i_track; i++ )
1577             {
1578                 avi_track_t *tk = p_sys->track[i];
1579                 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1580                 {
1581                     *pf = (float)tk->i_rate / (float)tk->i_scale;
1582                     break;
1583                 }
1584             }
1585             return VLC_SUCCESS;
1586
1587         case DEMUX_GET_META:
1588             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1589             vlc_meta_Merge( p_meta,  p_sys->meta );
1590             return VLC_SUCCESS;
1591
1592         case DEMUX_GET_ATTACHMENTS:
1593         {
1594             if( p_sys->i_attachment <= 0 )
1595                 return VLC_EGENERIC;
1596
1597             input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1598             int *pi_int = va_arg( args, int * );
1599
1600             *pi_int     = p_sys->i_attachment;
1601             *ppp_attach = calloc( p_sys->i_attachment, sizeof(**ppp_attach));
1602             for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1603                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1604             return VLC_SUCCESS;
1605         }
1606
1607         default:
1608             return VLC_EGENERIC;
1609     }
1610 }
1611
1612 /*****************************************************************************
1613  * Function to convert pts to chunk or byte
1614  *****************************************************************************/
1615
1616 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1617 {
1618     if( !tk->i_scale )
1619         return (mtime_t)0;
1620
1621     return (mtime_t)((int64_t)i_pts *
1622                      (int64_t)tk->i_rate /
1623                      (int64_t)tk->i_scale /
1624                      (int64_t)1000000 );
1625 }
1626 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1627 {
1628     if( !tk->i_scale || !tk->i_samplesize )
1629         return (mtime_t)0;
1630
1631     return (mtime_t)((int64_t)i_pts *
1632                      (int64_t)tk->i_rate /
1633                      (int64_t)tk->i_scale /
1634                      (int64_t)1000000 *
1635                      (int64_t)tk->i_samplesize );
1636 }
1637
1638 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1639 {
1640     mtime_t i_dpts = 0;
1641
1642     if( !tk->i_rate )
1643         return i_dpts;
1644
1645     i_dpts = (mtime_t)( (int64_t)1000000 *
1646                         (int64_t)i_count *
1647                         (int64_t)tk->i_scale /
1648                         (int64_t)tk->i_rate );
1649
1650     if( tk->i_samplesize )
1651     {
1652         return i_dpts / tk->i_samplesize;
1653     }
1654     return i_dpts;
1655 }
1656
1657 static mtime_t AVI_GetPTS( avi_track_t *tk )
1658 {
1659     if( tk->i_samplesize )
1660     {
1661         int64_t i_count = 0;
1662
1663         /* we need a valid entry we will emulate one */
1664         if( tk->i_idxposc == tk->idx.i_size )
1665         {
1666             if( tk->i_idxposc )
1667             {
1668                 /* use the last entry */
1669                 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1670                             + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1671             }
1672         }
1673         else
1674         {
1675             i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1676         }
1677         return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1678     }
1679     else
1680     {
1681         if( tk->i_cat == AUDIO_ES )
1682         {
1683             return AVI_GetDPTS( tk, tk->i_blockno );
1684         }
1685         else
1686         {
1687             return AVI_GetDPTS( tk, tk->i_idxposc );
1688         }
1689     }
1690 }
1691
1692 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1693 {
1694     demux_sys_t *p_sys = p_demux->p_sys;
1695     avi_packet_t avi_pk;
1696     int i_loop_count = 0;
1697
1698     /* find first chunk of i_stream that isn't in index */
1699
1700     if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1701     {
1702         stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1703         if( AVI_PacketNext( p_demux ) )
1704         {
1705             return VLC_EGENERIC;
1706         }
1707     }
1708     else
1709     {
1710         stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1711     }
1712
1713     for( ;; )
1714     {
1715         if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1716
1717         if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1718         {
1719             msg_Warn( p_demux, "cannot get packet header" );
1720             return VLC_EGENERIC;
1721         }
1722         if( avi_pk.i_stream >= p_sys->i_track ||
1723             ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1724         {
1725             if( AVI_PacketNext( p_demux ) )
1726             {
1727                 return VLC_EGENERIC;
1728             }
1729
1730             /* Prevents from eating all the CPU with broken files.
1731              * This value should be low enough so that it doesn't
1732              * affect the reading speed too much. */
1733             if( !(++i_loop_count % 1024) )
1734             {
1735                 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1736                 msleep( 10000 );
1737
1738                 if( !(i_loop_count % (1024 * 10)) )
1739                     msg_Warn( p_demux, "don't seem to find any data..." );
1740             }
1741         }
1742         else
1743         {
1744             avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1745
1746             /* add this chunk to the index */
1747             avi_entry_t index;
1748             index.i_id     = avi_pk.i_fourcc;
1749             index.i_flags  = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1750             index.i_pos    = avi_pk.i_pos;
1751             index.i_length = avi_pk.i_size;
1752             index.i_lengthtotal = index.i_length;
1753             avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1754
1755             if( avi_pk.i_stream == i_stream  )
1756             {
1757                 return VLC_SUCCESS;
1758             }
1759
1760             if( AVI_PacketNext( p_demux ) )
1761             {
1762                 return VLC_EGENERIC;
1763             }
1764         }
1765     }
1766 }
1767
1768 /* be sure that i_ck will be a valid index entry */
1769 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1770                                unsigned int i_ck )
1771 {
1772     demux_sys_t *p_sys = p_demux->p_sys;
1773     avi_track_t *p_stream = p_sys->track[i_stream];
1774
1775     p_stream->i_idxposc = i_ck;
1776     p_stream->i_idxposb = 0;
1777
1778     if(  i_ck >= p_stream->idx.i_size )
1779     {
1780         p_stream->i_idxposc = p_stream->idx.i_size - 1;
1781         do
1782         {
1783             p_stream->i_idxposc++;
1784             if( AVI_StreamChunkFind( p_demux, i_stream ) )
1785             {
1786                 return VLC_EGENERIC;
1787             }
1788
1789         } while( p_stream->i_idxposc < i_ck );
1790     }
1791
1792     return VLC_SUCCESS;
1793 }
1794
1795 /* XXX FIXME up to now, we assume that all chunk are one after one */
1796 static int AVI_StreamBytesSet( demux_t    *p_demux,
1797                                unsigned int i_stream,
1798                                off_t   i_byte )
1799 {
1800     demux_sys_t *p_sys = p_demux->p_sys;
1801     avi_track_t *p_stream = p_sys->track[i_stream];
1802
1803     if( ( p_stream->idx.i_size > 0 )
1804         &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1805                 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1806     {
1807         /* index is valid to find the ck */
1808         /* uses dichototmie to be fast enougth */
1809         int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1810         int i_idxmax  = p_stream->idx.i_size;
1811         int i_idxmin  = 0;
1812         for( ;; )
1813         {
1814             if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1815             {
1816                 i_idxmax  = i_idxposc ;
1817                 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1818             }
1819             else
1820             {
1821                 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1822                         p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1823                 {
1824                     i_idxmin  = i_idxposc ;
1825                     i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1826                 }
1827                 else
1828                 {
1829                     p_stream->i_idxposc = i_idxposc;
1830                     p_stream->i_idxposb = i_byte -
1831                             p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1832                     return VLC_SUCCESS;
1833                 }
1834             }
1835         }
1836
1837     }
1838     else
1839     {
1840         p_stream->i_idxposc = p_stream->idx.i_size - 1;
1841         p_stream->i_idxposb = 0;
1842         do
1843         {
1844             p_stream->i_idxposc++;
1845             if( AVI_StreamChunkFind( p_demux, i_stream ) )
1846             {
1847                 return VLC_EGENERIC;
1848             }
1849
1850         } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1851                     p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1852
1853         p_stream->i_idxposb = i_byte -
1854                        p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1855         return VLC_SUCCESS;
1856     }
1857 }
1858
1859 static int AVI_TrackSeek( demux_t *p_demux,
1860                            int i_stream,
1861                            mtime_t i_date )
1862 {
1863     demux_sys_t  *p_sys = p_demux->p_sys;
1864     avi_track_t  *tk = p_sys->track[i_stream];
1865
1866 #define p_stream    p_sys->track[i_stream]
1867     mtime_t i_oldpts;
1868
1869     i_oldpts = AVI_GetPTS( p_stream );
1870
1871     if( !p_stream->i_samplesize )
1872     {
1873         if( AVI_StreamChunkSet( p_demux,
1874                                 i_stream,
1875                                 AVI_PTSToChunk( p_stream, i_date ) ) )
1876         {
1877             return VLC_EGENERIC;
1878         }
1879
1880         if( p_stream->i_cat == AUDIO_ES )
1881         {
1882             unsigned int i;
1883             tk->i_blockno = 0;
1884             for( i = 0; i < tk->i_idxposc; i++ )
1885             {
1886                 if( tk->i_blocksize > 0 )
1887                 {
1888                     tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1889                 }
1890                 else
1891                 {
1892                     tk->i_blockno++;
1893                 }
1894             }
1895         }
1896
1897         msg_Dbg( p_demux,
1898                  "old:%"PRId64" %s new %"PRId64,
1899                  i_oldpts,
1900                  i_oldpts > i_date ? ">" : "<",
1901                  i_date );
1902
1903         if( p_stream->i_cat == VIDEO_ES )
1904         {
1905             /* search key frame */
1906             //if( i_date < i_oldpts || 1 )
1907             {
1908                 while( p_stream->i_idxposc > 0 &&
1909                    !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1910                                                                 AVIIF_KEYFRAME ) )
1911                 {
1912                     if( AVI_StreamChunkSet( p_demux,
1913                                             i_stream,
1914                                             p_stream->i_idxposc - 1 ) )
1915                     {
1916                         return VLC_EGENERIC;
1917                     }
1918                 }
1919             }
1920 #if 0
1921             else
1922             {
1923                 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1924                         !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1925                                                                 AVIIF_KEYFRAME ) )
1926                 {
1927                     if( AVI_StreamChunkSet( p_demux,
1928                                             i_stream,
1929                                             p_stream->i_idxposc + 1 ) )
1930                     {
1931                         return VLC_EGENERIC;
1932                     }
1933                 }
1934             }
1935 #endif
1936         }
1937     }
1938     else
1939     {
1940         if( AVI_StreamBytesSet( p_demux,
1941                                 i_stream,
1942                                 AVI_PTSToByte( p_stream, i_date ) ) )
1943         {
1944             return VLC_EGENERIC;
1945         }
1946     }
1947     return VLC_SUCCESS;
1948 #undef p_stream
1949 }
1950
1951 /****************************************************************************
1952  * Return true if it's a key frame
1953  ****************************************************************************/
1954 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1955 {
1956     switch( i_fourcc )
1957     {
1958         case VLC_CODEC_DIV1:
1959             /* we have:
1960              *  startcode:      0x00000100   32bits
1961              *  framenumber     ?             5bits
1962              *  piture type     0(I),1(P)     2bits
1963              */
1964             if( GetDWBE( p_byte ) != 0x00000100 )
1965             {
1966                 /* it's not an msmpegv1 stream, strange...*/
1967                 return AVIIF_KEYFRAME;
1968             }
1969             return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1970
1971         case VLC_CODEC_DIV2:
1972         case VLC_CODEC_DIV3:
1973         case VLC_CODEC_WMV1:
1974             /* we have
1975              *  picture type    0(I),1(P)     2bits
1976              */
1977             return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1978         case VLC_CODEC_MP4V:
1979             /* we should find first occurrence of 0x000001b6 (32bits)
1980              *  startcode:      0x000001b6   32bits
1981              *  piture type     0(I),1(P)     2bits
1982              */
1983             if( GetDWBE( p_byte ) != 0x000001b6 )
1984             {
1985                 /* not true , need to find the first VOP header */
1986                 return AVIIF_KEYFRAME;
1987             }
1988             return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1989
1990         default:
1991             /* I can't do it, so say yes */
1992             return AVIIF_KEYFRAME;
1993     }
1994 }
1995
1996 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1997 {
1998     switch( i_cat )
1999     {
2000         case AUDIO_ES:
2001             wf_tag_to_fourcc( i_codec, &i_codec, NULL );
2002             return i_codec;
2003         case VIDEO_ES:
2004             return vlc_fourcc_GetCodec( i_cat, i_codec );
2005         default:
2006             return VLC_FOURCC( 'u', 'n', 'd', 'f' );
2007     }
2008 }
2009
2010 /****************************************************************************
2011  *
2012  ****************************************************************************/
2013 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
2014                                    unsigned int *pi_number, unsigned int *pi_type )
2015 {
2016 #define SET_PTR( p, v ) if( p ) *(p) = (v);
2017     int c1, c2;
2018
2019     c1 = ((uint8_t *)&i_id)[0];
2020     c2 = ((uint8_t *)&i_id)[1];
2021
2022     if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
2023     {
2024         SET_PTR( pi_number, 100 ); /* > max stream number */
2025         SET_PTR( pi_type, UNKNOWN_ES );
2026     }
2027     else
2028     {
2029         SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
2030         switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
2031         {
2032             case AVITWOCC_wb:
2033                 SET_PTR( pi_type, AUDIO_ES );
2034                 break;
2035             case AVITWOCC_dc:
2036             case AVITWOCC_db:
2037             case AVITWOCC_AC:
2038                 SET_PTR( pi_type, VIDEO_ES );
2039                 break;
2040             case AVITWOCC_tx:
2041             case AVITWOCC_sb:
2042                 SET_PTR( pi_type, SPU_ES );
2043                 break;
2044             case AVITWOCC_pc:
2045                 SET_PTR( pi_type, IGNORE_ES );
2046                 break;
2047             default:
2048                 SET_PTR( pi_type, UNKNOWN_ES );
2049                 break;
2050         }
2051     }
2052 #undef SET_PTR
2053 }
2054
2055 /****************************************************************************
2056  *
2057  ****************************************************************************/
2058 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
2059 {
2060     const uint8_t *p_peek;
2061
2062     if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
2063     {
2064         return VLC_EGENERIC;
2065     }
2066     p_pk->i_fourcc  = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
2067     p_pk->i_size    = GetDWLE( p_peek + 4 );
2068     p_pk->i_pos     = stream_Tell( p_demux->s );
2069     if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
2070     {
2071         p_pk->i_type = VLC_FOURCC( p_peek[8],  p_peek[9],
2072                                    p_peek[10], p_peek[11] );
2073     }
2074     else
2075     {
2076         p_pk->i_type = 0;
2077     }
2078
2079     memcpy( p_pk->i_peek, p_peek + 8, 8 );
2080
2081     AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
2082     return VLC_SUCCESS;
2083 }
2084
2085 static int AVI_PacketNext( demux_t *p_demux )
2086 {
2087     avi_packet_t    avi_ck;
2088     int             i_skip = 0;
2089
2090     if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
2091     {
2092         return VLC_EGENERIC;
2093     }
2094
2095     if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
2096         ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
2097     {
2098         i_skip = 12;
2099     }
2100     else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
2101              avi_ck.i_type == AVIFOURCC_AVIX )
2102     {
2103         i_skip = 24;
2104     }
2105     else
2106     {
2107         i_skip = __EVEN( avi_ck.i_size ) + 8;
2108     }
2109
2110     if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2111     {
2112         return VLC_EGENERIC;
2113     }
2114     return VLC_SUCCESS;
2115 }
2116
2117 static int AVI_PacketRead( demux_t   *p_demux,
2118                            avi_packet_t     *p_pk,
2119                            block_t          **pp_frame )
2120 {
2121     size_t i_size;
2122
2123     i_size = __EVEN( p_pk->i_size + 8 );
2124
2125     if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2126     {
2127         return VLC_EGENERIC;
2128     }
2129     (*pp_frame)->p_buffer += 8;
2130     (*pp_frame)->i_buffer -= 8;
2131
2132     if( i_size != p_pk->i_size + 8 )
2133     {
2134         (*pp_frame)->i_buffer--;
2135     }
2136
2137     return VLC_SUCCESS;
2138 }
2139
2140 static int AVI_PacketSearch( demux_t *p_demux )
2141 {
2142     demux_sys_t     *p_sys = p_demux->p_sys;
2143     avi_packet_t    avi_pk;
2144     int             i_count = 0;
2145
2146     for( ;; )
2147     {
2148         if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2149         {
2150             return VLC_EGENERIC;
2151         }
2152         AVI_PacketGetHeader( p_demux, &avi_pk );
2153         if( avi_pk.i_stream < p_sys->i_track &&
2154             ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2155         {
2156             return VLC_SUCCESS;
2157         }
2158         switch( avi_pk.i_fourcc )
2159         {
2160             case AVIFOURCC_JUNK:
2161             case AVIFOURCC_LIST:
2162             case AVIFOURCC_RIFF:
2163             case AVIFOURCC_idx1:
2164                 return VLC_SUCCESS;
2165         }
2166
2167         /* Prevents from eating all the CPU with broken files.
2168          * This value should be low enough so that it doesn't affect the
2169          * reading speed too much (not that we care much anyway because
2170          * this code is called only on broken files). */
2171         if( !(++i_count % 1024) )
2172         {
2173             if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2174
2175             msleep( 10000 );
2176             if( !(i_count % (1024 * 10)) )
2177                 msg_Warn( p_demux, "trying to resync..." );
2178         }
2179     }
2180 }
2181
2182 /****************************************************************************
2183  * Index stuff.
2184  ****************************************************************************/
2185 static void avi_index_Init( avi_index_t *p_index )
2186 {
2187     p_index->i_size  = 0;
2188     p_index->i_max   = 0;
2189     p_index->p_entry = NULL;
2190 }
2191 static void avi_index_Clean( avi_index_t *p_index )
2192 {
2193     free( p_index->p_entry );
2194 }
2195 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2196                               avi_entry_t *p_entry )
2197 {
2198     /* Update last chunk position */
2199     if( *pi_last_pos < p_entry->i_pos )
2200          *pi_last_pos = p_entry->i_pos;
2201
2202     /* add the entry */
2203     if( p_index->i_size >= p_index->i_max )
2204     {
2205         p_index->i_max += 16384;
2206         p_index->p_entry = realloc_or_free( p_index->p_entry,
2207                                             p_index->i_max * sizeof( *p_index->p_entry ) );
2208         if( !p_index->p_entry )
2209             return;
2210     }
2211     /* calculate cumulate length */
2212     if( p_index->i_size > 0 )
2213     {
2214         p_entry->i_lengthtotal =
2215             p_index->p_entry[p_index->i_size - 1].i_length +
2216                 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2217     }
2218     else
2219     {
2220         p_entry->i_lengthtotal = 0;
2221     }
2222
2223     p_index->p_entry[p_index->i_size++] = *p_entry;
2224 }
2225
2226 static int AVI_IndexFind_idx1( demux_t *p_demux,
2227                                avi_chunk_idx1_t **pp_idx1,
2228                                uint64_t *pi_offset )
2229 {
2230     demux_sys_t *p_sys = p_demux->p_sys;
2231
2232     avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2233     avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2234
2235     if( !p_idx1 )
2236     {
2237         msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2238         return VLC_EGENERIC;
2239     }
2240     *pp_idx1 = p_idx1;
2241
2242     /* The offset in the index should be from the start of the movi content,
2243      * but some broken files use offset from the start of the file. Just
2244      * checking the offset of the first packet is not enough as some files
2245      * has unused chunk at the beginning of the movi content.
2246      */
2247     avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2248     uint64_t i_first_pos = UINT64_MAX;
2249     for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 100 ); i++ )
2250     {
2251         if ( p_idx1->entry[i].i_length > 0 )
2252             i_first_pos = __MIN( i_first_pos, p_idx1->entry[i].i_pos );
2253     }
2254
2255     const uint64_t i_movi_content = p_movi->i_chunk_pos + 8;
2256     if( i_first_pos < i_movi_content )
2257     {
2258         *pi_offset = i_movi_content;
2259     }
2260     else if( p_sys->b_seekable && i_first_pos < UINT64_MAX )
2261     {
2262         const uint8_t *p_peek;
2263         if( !stream_Seek( p_demux->s, i_movi_content + i_first_pos ) &&
2264             stream_Peek( p_demux->s, &p_peek, 4 ) >= 4 &&
2265             ( !isdigit( p_peek[0] ) || !isdigit( p_peek[1] ) ||
2266               !isalpha( p_peek[2] ) || !isalpha( p_peek[3] ) ) )
2267             *pi_offset = 0;
2268         else
2269             *pi_offset = i_movi_content;
2270     }
2271     else
2272     {
2273         *pi_offset = 0;
2274     }
2275     return VLC_SUCCESS;
2276 }
2277
2278 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2279                                avi_index_t p_index[], off_t *pi_last_offset )
2280 {
2281     demux_sys_t *p_sys = p_demux->p_sys;
2282
2283     avi_chunk_idx1_t *p_idx1;
2284     uint64_t         i_offset;
2285     if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2286         return VLC_EGENERIC;
2287
2288     for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2289     {
2290         unsigned i_cat;
2291         unsigned i_stream;
2292
2293         AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2294                                &i_stream,
2295                                &i_cat );
2296         if( i_stream < p_sys->i_track &&
2297             (i_cat == p_sys->track[i_stream]->i_cat || i_cat == UNKNOWN_ES ) )
2298         {
2299             avi_entry_t index;
2300             index.i_id     = p_idx1->entry[i_index].i_fourcc;
2301             index.i_flags  = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2302             index.i_pos    = p_idx1->entry[i_index].i_pos + i_offset;
2303             index.i_length = p_idx1->entry[i_index].i_length;
2304             index.i_lengthtotal = index.i_length;
2305
2306             avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2307         }
2308     }
2309     return VLC_SUCCESS;
2310 }
2311
2312 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2313                           avi_chunk_indx_t *p_indx )
2314 {
2315     avi_entry_t index;
2316
2317     msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2318     if( p_indx->i_indexsubtype == 0 )
2319     {
2320         for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2321         {
2322             index.i_id     = p_indx->i_id;
2323             index.i_flags  = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2324             index.i_pos    = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2325             index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2326             index.i_lengthtotal = index.i_length;
2327
2328             avi_index_Append( p_index, pi_max_offset, &index );
2329         }
2330     }
2331     else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2332     {
2333         for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2334         {
2335             index.i_id     = p_indx->i_id;
2336             index.i_flags  = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2337             index.i_pos    = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2338             index.i_length = p_indx->idx.field[i].i_size;
2339             index.i_lengthtotal = index.i_length;
2340
2341             avi_index_Append( p_index, pi_max_offset, &index );
2342         }
2343     }
2344     else
2345     {
2346         msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2347     }
2348 }
2349
2350 static void AVI_IndexLoad_indx( demux_t *p_demux,
2351                                 avi_index_t p_index[], off_t *pi_last_offset )
2352 {
2353     demux_sys_t         *p_sys = p_demux->p_sys;
2354
2355     avi_chunk_list_t    *p_riff;
2356     avi_chunk_list_t    *p_hdrl;
2357
2358     p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2359     p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2360
2361     for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2362     {
2363         avi_chunk_list_t    *p_strl;
2364         avi_chunk_indx_t    *p_indx;
2365
2366 #define p_stream  p_sys->track[i_stream]
2367         p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2368         p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2369
2370         if( !p_indx )
2371         {
2372             if( p_sys->b_odml )
2373                 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2374                                    "file?)" );
2375             continue;
2376         }
2377
2378         if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2379         {
2380             __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2381         }
2382         else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2383         {
2384             avi_chunk_t    ck_sub;
2385             for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2386             {
2387                 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2388                     AVI_ChunkRead( p_demux->s, &ck_sub, NULL  ) )
2389                 {
2390                     break;
2391                 }
2392                 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2393                     __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2394                 AVI_ChunkFree( p_demux->s, &ck_sub );
2395             }
2396         }
2397         else
2398         {
2399             msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2400         }
2401 #undef p_stream
2402     }
2403 }
2404
2405 static void AVI_IndexLoad( demux_t *p_demux )
2406 {
2407     demux_sys_t *p_sys = p_demux->p_sys;
2408
2409     /* Load indexes */
2410     assert( p_sys->i_track <= 100 );
2411     avi_index_t p_idx_indx[p_sys->i_track];
2412     avi_index_t p_idx_idx1[p_sys->i_track];
2413     for( unsigned i = 0; i < p_sys->i_track; i++ )
2414     {
2415         avi_index_Init( &p_idx_indx[i] );
2416         avi_index_Init( &p_idx_idx1[i] );
2417     }
2418     off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2419     off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2420
2421     AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2422     if( !p_sys->b_odml )
2423         AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2424
2425     /* Select the longest index */
2426     for( unsigned i = 0; i < p_sys->i_track; i++ )
2427     {
2428         if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2429         {
2430             msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2431             p_sys->track[i]->idx = p_idx_indx[i];
2432             avi_index_Clean( &p_idx_idx1[i] );
2433         }
2434         else
2435         {
2436             msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2437             p_sys->track[i]->idx = p_idx_idx1[i];
2438             avi_index_Clean( &p_idx_indx[i] );
2439         }
2440     }
2441     p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2442
2443     for( unsigned i = 0; i < p_sys->i_track; i++ )
2444     {
2445         avi_index_t *p_index = &p_sys->track[i]->idx;
2446
2447         /* Fix key flag */
2448         bool b_key = false;
2449         for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2450             b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2451         if( !b_key )
2452         {
2453             msg_Err( p_demux, "no key frame set for track %u", i );
2454             for( unsigned j = 0; j < p_index->i_size; j++ )
2455                 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2456         }
2457
2458         /* */
2459         msg_Dbg( p_demux, "stream[%d] created %d index entries",
2460                  i, p_index->i_size );
2461     }
2462 }
2463
2464 static void AVI_IndexCreate( demux_t *p_demux )
2465 {
2466     demux_sys_t *p_sys = p_demux->p_sys;
2467
2468     avi_chunk_list_t *p_riff;
2469     avi_chunk_list_t *p_movi;
2470
2471     unsigned int i_stream;
2472     off_t i_movi_end;
2473
2474     mtime_t i_dialog_update;
2475     dialog_progress_bar_t *p_dialog = NULL;
2476
2477     p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2478     p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2479
2480     if( !p_movi )
2481     {
2482         msg_Err( p_demux, "cannot find p_movi" );
2483         return;
2484     }
2485
2486     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2487         avi_index_Init( &p_sys->track[i_stream]->idx );
2488
2489     i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2490                         stream_Size( p_demux->s ) );
2491
2492     stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2493     msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2494
2495
2496     /* Only show dialog if AVI is > 10MB */
2497     i_dialog_update = mdate();
2498     if( stream_Size( p_demux->s ) > 10000000 )
2499         p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2500                                        NULL, _("Cancel") );
2501
2502     for( ;; )
2503     {
2504         avi_packet_t pk;
2505
2506         if( !vlc_object_alive (p_demux) )
2507             break;
2508
2509         /* Don't update/check dialog too often */
2510         if( p_dialog && mdate() - i_dialog_update > 100000 )
2511         {
2512             if( dialog_ProgressCancelled( p_dialog ) )
2513                 break;
2514
2515             double f_current = stream_Tell( p_demux->s );
2516             double f_size    = stream_Size( p_demux->s );
2517             double f_pos     = f_current / f_size;
2518             dialog_ProgressSet( p_dialog, NULL, f_pos );
2519
2520             i_dialog_update = mdate();
2521         }
2522
2523         if( AVI_PacketGetHeader( p_demux, &pk ) )
2524             break;
2525
2526         if( pk.i_stream < p_sys->i_track &&
2527             pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2528         {
2529             avi_track_t *tk = p_sys->track[pk.i_stream];
2530
2531             avi_entry_t index;
2532             index.i_id      = pk.i_fourcc;
2533             index.i_flags   = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2534             index.i_pos     = pk.i_pos;
2535             index.i_length  = pk.i_size;
2536             index.i_lengthtotal = pk.i_size;
2537             avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2538         }
2539         else
2540         {
2541             switch( pk.i_fourcc )
2542             {
2543             case AVIFOURCC_idx1:
2544                 if( p_sys->b_odml )
2545                 {
2546                     avi_chunk_list_t *p_sysx;
2547                     p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2548                                             AVIFOURCC_RIFF, 1 );
2549
2550                     msg_Dbg( p_demux, "looking for new RIFF chunk" );
2551                     if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2552                         goto print_stat;
2553                     break;
2554                 }
2555                 goto print_stat;
2556
2557             case AVIFOURCC_RIFF:
2558                     msg_Dbg( p_demux, "new RIFF chunk found" );
2559                     break;
2560
2561             case AVIFOURCC_rec:
2562             case AVIFOURCC_JUNK:
2563                 break;
2564
2565             default:
2566                 msg_Warn( p_demux, "need resync, probably broken avi" );
2567                 if( AVI_PacketSearch( p_demux ) )
2568                 {
2569                     msg_Warn( p_demux, "lost sync, abord index creation" );
2570                     goto print_stat;
2571                 }
2572             }
2573         }
2574
2575         if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2576             AVI_PacketNext( p_demux ) )
2577         {
2578             break;
2579         }
2580     }
2581
2582 print_stat:
2583     if( p_dialog != NULL )
2584         dialog_ProgressDestroy( p_dialog );
2585
2586     for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2587     {
2588         msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2589                 i_stream, p_sys->track[i_stream]->idx.i_size );
2590     }
2591 }
2592
2593 /* */
2594 static void AVI_MetaLoad( demux_t *p_demux,
2595                           avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2596 {
2597     demux_sys_t *p_sys = p_demux->p_sys;
2598
2599     vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2600     if( !p_meta )
2601         return;
2602
2603     char buffer[200];
2604     snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2605               p_avih->i_flags&AVIF_HASINDEX      ? " HAS_INDEX"      : "",
2606               p_avih->i_flags&AVIF_MUSTUSEINDEX  ? " MUST_USE_INDEX" : "",
2607               p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2608               p_avih->i_flags&AVIF_TRUSTCKTYPE   ? " TRUST_CKTYPE"   : "" );
2609     vlc_meta_SetSetting( p_meta, buffer );
2610
2611     avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2612     if( !p_info )
2613         return;
2614
2615     static const struct {
2616         vlc_fourcc_t i_id;
2617         int          i_type;
2618     } p_dsc[] = {
2619         { AVIFOURCC_IART, vlc_meta_Artist },
2620         { AVIFOURCC_ICMT, vlc_meta_Description },
2621         { AVIFOURCC_ICOP, vlc_meta_Copyright },
2622         { AVIFOURCC_IGNR, vlc_meta_Genre },
2623         { AVIFOURCC_INAM, vlc_meta_Title },
2624         { AVIFOURCC_ICRD, vlc_meta_Date },
2625         { AVIFOURCC_ILNG, vlc_meta_Language },
2626         { AVIFOURCC_IRTD, vlc_meta_Rating },
2627         { AVIFOURCC_IWEB, vlc_meta_URL },
2628         { AVIFOURCC_IPRT, vlc_meta_TrackNumber },
2629         { AVIFOURCC_IFRM, vlc_meta_TrackTotal },
2630         { 0, -1 }
2631     };
2632     for( int i = 0; p_dsc[i].i_id != 0; i++ )
2633     {
2634         avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2635         if( !p_strz )
2636             continue;
2637         char *psz_value = FromACP( p_strz->p_str );
2638         if( !psz_value )
2639             continue;
2640
2641         if( *psz_value )
2642             vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2643         free( psz_value );
2644     }
2645
2646     static const vlc_fourcc_t p_extra[] = {
2647         AVIFOURCC_IARL, AVIFOURCC_ICMS, AVIFOURCC_ICRP, AVIFOURCC_IDIM, AVIFOURCC_IDPI,
2648         AVIFOURCC_IENG, AVIFOURCC_IKEY, AVIFOURCC_ILGT, AVIFOURCC_IMED, AVIFOURCC_IPLT,
2649         AVIFOURCC_IPRD, AVIFOURCC_ISBJ, AVIFOURCC_ISFT, AVIFOURCC_ISHP, AVIFOURCC_ISRC,
2650         AVIFOURCC_ISRF, AVIFOURCC_ITCH, AVIFOURCC_ISMP, AVIFOURCC_IDIT, AVIFOURCC_ISGN,
2651         AVIFOURCC_IWRI, AVIFOURCC_IPRO, AVIFOURCC_ICNM, AVIFOURCC_IPDS, AVIFOURCC_IEDT,
2652         AVIFOURCC_ICDS, AVIFOURCC_IMUS, AVIFOURCC_ISTD, AVIFOURCC_IDST, AVIFOURCC_ICNT,
2653         AVIFOURCC_ISTR, 0,
2654     };
2655
2656     for( int i = 0; p_extra[i] != 0; i++ )
2657     {
2658         avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_extra[i], 0 );
2659         if( !p_strz )
2660             continue;
2661         char *psz_value = FromACP( p_strz->p_str );
2662         if( !psz_value )
2663             continue;
2664
2665         if( *psz_value )
2666             vlc_meta_AddExtra( p_meta, p_strz->p_type, psz_value );
2667         free( psz_value );
2668     }
2669 }
2670
2671 static void AVI_DvHandleAudio( demux_t *p_demux, avi_track_t *tk, block_t *p_frame )
2672 {
2673     size_t i_offset = 80 * 6 + 80 * 16 * 3 + 3;
2674     if( p_frame->i_buffer < i_offset + 5 )
2675         return;
2676
2677     if( p_frame->p_buffer[i_offset] != 0x50 )
2678         return;
2679
2680     es_format_t fmt;
2681     dv_get_audio_format( &fmt, &p_frame->p_buffer[i_offset + 1] );
2682
2683     if( tk->p_es_dv_audio && tk->i_dv_audio_rate != (int)fmt.audio.i_rate )
2684     {
2685         es_out_Del( p_demux->out, tk->p_es_dv_audio );
2686         tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
2687     }
2688     else if( !tk->p_es_dv_audio )
2689     {
2690         tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
2691     }
2692     tk->i_dv_audio_rate = fmt.audio.i_rate;
2693
2694     block_t *p_frame_audio = dv_extract_audio( p_frame );
2695     if( p_frame_audio )
2696         es_out_Send( p_demux->out, tk->p_es_dv_audio, p_frame_audio );
2697 }
2698
2699 /*****************************************************************************
2700  * Subtitles
2701  *****************************************************************************/
2702 static void AVI_ExtractSubtitle( demux_t *p_demux,
2703                                  unsigned int i_stream,
2704                                  avi_chunk_list_t *p_strl,
2705                                  avi_chunk_STRING_t *p_strn )
2706 {
2707     demux_sys_t *p_sys = p_demux->p_sys;
2708     block_t *p_block = NULL;
2709     input_attachment_t *p_attachment = NULL;
2710     char *psz_description = NULL;
2711     avi_chunk_indx_t *p_indx = NULL;
2712
2713     if( !p_sys->b_seekable )
2714         goto exit;
2715
2716     p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2717     avi_chunk_t ck;
2718     int64_t  i_position;
2719     unsigned i_size;
2720     if( p_indx )
2721     {
2722         if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2723             p_indx->i_entriesinuse > 0 )
2724         {
2725             if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2726                 AVI_ChunkRead( p_demux->s, &ck, NULL  ) )
2727                 goto exit;
2728             p_indx = &ck.indx;
2729         }
2730
2731         if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2732             p_indx->i_entriesinuse != 1 ||
2733             p_indx->i_indexsubtype != 0 )
2734             goto exit;
2735
2736         i_position  = p_indx->i_baseoffset +
2737                       p_indx->idx.std[0].i_offset - 8;
2738         i_size      = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2739     }
2740     else
2741     {
2742         avi_chunk_idx1_t *p_idx1;
2743         uint64_t         i_offset;
2744
2745         if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2746             goto exit;
2747
2748         i_size = 0;
2749         for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2750         {
2751             const idx1_entry_t *e = &p_idx1->entry[i];
2752             unsigned i_cat;
2753             unsigned i_stream_idx;
2754
2755             AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2756             if( i_cat == SPU_ES && i_stream_idx == i_stream )
2757             {
2758                 i_position = e->i_pos + i_offset;
2759                 i_size     = e->i_length + 8;
2760                 break;
2761             }
2762         }
2763         if( i_size <= 0 )
2764             goto exit;
2765     }
2766
2767     /* */
2768     if( i_size > 10000000 )
2769     {
2770         msg_Dbg( p_demux, "Attached subtitle too big: %u", i_size );
2771         goto exit;
2772     }
2773
2774     if( stream_Seek( p_demux->s, i_position ) )
2775         goto exit;
2776     p_block = stream_Block( p_demux->s, i_size );
2777     if( !p_block )
2778         goto exit;
2779
2780     /* Parse packet header */
2781     const uint8_t *p = p_block->p_buffer;
2782     if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2783         goto exit;
2784     p += 8;
2785     i_size -= 8;
2786
2787     /* Parse subtitle chunk header */
2788     if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2789         p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2790         goto exit;
2791     const unsigned i_name = GetDWLE( &p[7] );
2792     if( 11 + i_size <= i_name )
2793         goto exit;
2794     if( i_name > 0 )
2795         psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2796     p += 11 + i_name;
2797     i_size -= 11 + i_name;
2798     if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2799         goto exit;
2800     const unsigned i_payload = GetDWLE( &p[2] );
2801     if( i_size < 6 + i_payload || i_payload <= 0 )
2802         goto exit;
2803     p += 6;
2804     i_size -= 6;
2805
2806     if( !psz_description )
2807         psz_description = p_strn ? FromACP( p_strn->p_str ) : NULL;
2808     char *psz_name;
2809     if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2810         psz_name = NULL;
2811     p_attachment = vlc_input_attachment_New( psz_name,
2812                                              "application/x-srt",
2813                                              psz_description,
2814                                              p, i_payload );
2815     if( p_attachment )
2816         TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2817     free( psz_name );
2818
2819 exit:
2820     free( psz_description );
2821
2822     if( p_block )
2823         block_Release( p_block );
2824
2825     if( p_attachment )
2826         msg_Dbg( p_demux, "Loaded an embedded subtitle" );
2827     else
2828         msg_Warn( p_demux, "Failed to load an embedded subtitle" );
2829
2830     if( p_indx == &ck.indx )
2831         AVI_ChunkFree( p_demux->s, &ck );
2832 }
2833 /*****************************************************************************
2834  * Stream management
2835  *****************************************************************************/
2836 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2837 {
2838     demux_sys_t *p_sys = p_demux->p_sys;
2839     unsigned int i;
2840     int b_end = true;
2841
2842     for( i = 0; i < p_sys->i_track; i++ )
2843     {
2844         avi_track_t *tk = p_sys->track[i];
2845         if( tk->i_idxposc >= tk->idx.i_size )
2846         {
2847             tk->b_eof = true;
2848         }
2849         else
2850         {
2851             b_end = false;
2852         }
2853     }
2854     return( b_end );
2855 }
2856
2857 /****************************************************************************
2858  * AVI_MovieGetLength give max streams length in second
2859  ****************************************************************************/
2860 static mtime_t  AVI_MovieGetLength( demux_t *p_demux )
2861 {
2862     demux_sys_t  *p_sys = p_demux->p_sys;
2863     mtime_t      i_maxlength = 0;
2864     unsigned int i;
2865
2866     for( i = 0; i < p_sys->i_track; i++ )
2867     {
2868         avi_track_t *tk = p_sys->track[i];
2869         mtime_t i_length;
2870
2871         /* fix length for each stream */
2872         if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2873         {
2874             continue;
2875         }
2876
2877         if( tk->i_samplesize )
2878         {
2879             i_length = AVI_GetDPTS( tk,
2880                                     tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2881                                         tk->idx.p_entry[tk->idx.i_size-1].i_length );
2882         }
2883         else
2884         {
2885             i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2886         }
2887         i_length /= (mtime_t)1000000;    /* in seconds */
2888
2889         msg_Dbg( p_demux,
2890                  "stream[%d] length:%"PRId64" (based on index)",
2891                  i,
2892                  i_length );
2893         i_maxlength = __MAX( i_maxlength, i_length );
2894     }
2895
2896     return i_maxlength;
2897 }