1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
40 #define INTERLEAVE_TEXT N_("Force interleaved method" )
41 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method" )
43 #define INDEX_TEXT N_("Force index creation")
44 #define INDEX_LONGTEXT N_( \
45 "Recreate a index for the AVI file so we can seek trough it more reliably." )
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
51 set_description( _("AVI demuxer") );
52 set_capability( "demux2", 212 );
54 add_bool( "avi-interleaved", 0, NULL,
55 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
56 add_bool( "avi-index", 0, NULL,
57 INDEX_TEXT, INDEX_LONGTEXT, VLC_TRUE );
59 set_callbacks( Open, Close );
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Control ( demux_t *, int, va_list );
66 static int Seek ( demux_t *, mtime_t, int );
67 static int Demux_Seekable ( demux_t * );
68 static int Demux_UnSeekable( demux_t * );
70 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
71 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
75 vlc_fourcc_t i_fourcc;
78 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
80 uint8_t i_peek[8]; /* first 8 bytes */
82 unsigned int i_stream;
93 uint32_t i_lengthtotal;
99 vlc_bool_t b_activated;
101 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
102 vlc_fourcc_t i_codec;
110 avi_entry_t *p_index;
111 unsigned int i_idxnb;
112 unsigned int i_idxmax;
114 unsigned int i_idxposc; /* numero of chunk */
115 unsigned int i_idxposb; /* byte in the current chunk */
117 /* For VBR audio only */
118 unsigned int i_blockno;
119 unsigned int i_blocksize;
127 vlc_bool_t b_seekable;
133 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
135 /* number of streams and information */
136 unsigned int i_track;
143 static inline off_t __EVEN( off_t i )
145 return (i & 1) ? i + 1 : i;
148 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
149 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
150 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
151 static mtime_t AVI_GetPTS ( avi_track_t * );
154 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
155 static int AVI_StreamChunkSet ( demux_t *,
156 unsigned int i_stream, unsigned int i_ck );
157 static int AVI_StreamBytesSet ( demux_t *,
158 unsigned int i_stream, off_t i_byte );
160 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
161 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
163 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
164 static int AVI_PacketNext ( demux_t * );
165 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
166 static int AVI_PacketSearch ( demux_t * );
168 static void AVI_IndexLoad ( demux_t * );
169 static void AVI_IndexCreate ( demux_t * );
170 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
172 static mtime_t AVI_MovieGetLength( demux_t * );
174 /*****************************************************************************
176 *****************************************************************************/
177 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
178 static int AVI_TrackStopFinishedStreams( demux_t *);
181 - For VBR mp3 stream:
182 count blocks by rounded-up chunksizes instead of chunks
183 we need full emulation of dshow avi demuxer bugs :(
184 fixes silly nandub-style a-v delaying in avi with vbr mp3...
185 (from mplayer 2002/08/02)
189 /*****************************************************************************
190 * Open: check file and initializes AVI structures
191 *****************************************************************************/
192 static int Open( vlc_object_t * p_this )
194 demux_t *p_demux = (demux_t *)p_this;
198 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
199 avi_chunk_list_t *p_hdrl, *p_movi;
200 avi_chunk_avih_t *p_avih;
202 unsigned int i_track;
203 unsigned int i, i_peeker;
208 /* Is it an avi file ? */
209 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 )
211 msg_Dbg( p_demux, "cannot peek()" );
214 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
216 if( !strncmp( &p_peek[0], "RIFF", 4 ) && !strncmp( &p_peek[8], "AVI ", 4 ) )
222 if( i_peeker == 188 )
224 msg_Warn( p_demux, "avi module discarded (invalid header)" );
228 /* Initialize input structures. */
229 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
230 memset( p_sys, 0, sizeof( demux_sys_t ) );
233 p_sys->i_movi_lastchunk_pos = 0;
234 p_sys->b_odml = VLC_FALSE;
239 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
241 p_demux->pf_control = Control;
242 p_demux->pf_demux = Demux_Seekable;
243 /* For unseekable stream, automaticaly use Demux_UnSeekable */
244 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
246 p_demux->pf_demux = Demux_UnSeekable;
251 stream_Read( p_demux->s, NULL, i_peeker );
254 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
256 msg_Err( p_demux, "avi module discarded (invalid file)" );
260 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
262 unsigned int i_count =
263 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
265 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
266 for( i = 1; i < i_count; i++ )
268 avi_chunk_list_t *p_sysx;
270 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
271 if( p_sysx->i_type == AVIFOURCC_AVIX )
273 msg_Warn( p_demux, "detected OpenDML file" );
274 p_sys->b_odml = VLC_TRUE;
280 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
281 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
282 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
284 if( !p_hdrl || !p_movi )
286 msg_Err( p_demux, "avi module discarded (invalid file)" );
290 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
292 msg_Err( p_demux, "cannot find avih chunk" );
295 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
296 if( p_avih->i_streams != i_track )
299 "found %d stream but %d are declared",
300 i_track, p_avih->i_streams );
304 msg_Err( p_demux, "no stream defined!" );
308 /* print information on streams */
309 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
311 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
312 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
313 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
314 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
315 if( ( p_sys->meta = vlc_meta_New() ) )
318 sprintf( buffer, "%s%s%s%s",
319 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
320 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
321 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
322 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
323 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
326 /* now read info on each stream and create ES */
327 for( i = 0 ; i < i_track; i++ )
329 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
330 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
331 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
332 avi_chunk_strf_auds_t *p_auds;
333 avi_chunk_strf_vids_t *p_vids;
336 tk->b_activated = VLC_FALSE;
346 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
347 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
349 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
351 msg_Warn( p_demux, "stream[%d] incomplete", i );
355 tk->i_rate = p_strh->i_rate;
356 tk->i_scale = p_strh->i_scale;
357 tk->i_samplesize = p_strh->i_samplesize;
358 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
359 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
361 switch( p_strh->i_type )
363 case( AVIFOURCC_auds ):
364 tk->i_cat = AUDIO_ES;
365 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
366 p_auds->p_wf->wFormatTag );
367 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
369 if( p_auds->p_wf->wFormatTag == 1 )
371 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
378 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
380 fmt.audio.i_channels = p_auds->p_wf->nChannels;
381 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
382 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
383 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
384 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
385 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
386 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
387 fmt.p_extra = &p_auds->p_wf[1];
388 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
389 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
390 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
393 case( AVIFOURCC_vids ):
394 tk->i_cat = VIDEO_ES;
395 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
396 p_vids->p_bih->biCompression );
397 if( p_vids->p_bih->biCompression == 0x00 )
399 switch( p_vids->p_bih->biBitCount )
402 tk->i_codec = VLC_FOURCC('R','V','3','2');
405 tk->i_codec = VLC_FOURCC('R','V','2','4');
408 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
411 tk->i_codec = VLC_FOURCC('R','V','1','5');
414 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
417 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
419 if( p_vids->p_bih->biBitCount == 24 )
421 /* This is in BGR format */
422 fmt.video.i_bmask = 0x00ff0000;
423 fmt.video.i_gmask = 0x0000ff00;
424 fmt.video.i_rmask = 0x000000ff;
429 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
430 if( tk->i_codec == FOURCC_mp4v &&
431 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
433 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
436 tk->i_samplesize = 0;
437 fmt.video.i_width = p_vids->p_bih->biWidth;
438 fmt.video.i_height = p_vids->p_bih->biHeight;
439 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
441 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
442 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
443 fmt.p_extra = &p_vids->p_bih[1];
444 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
446 (char*)&p_vids->p_bih->biCompression,
447 p_vids->p_bih->biWidth,
448 p_vids->p_bih->biHeight,
449 p_vids->p_bih->biBitCount,
450 (float)tk->i_rate/(float)tk->i_scale );
452 if( p_vids->p_bih->biCompression == 0x00 )
454 /* RGB DIB are coded from bottom to top */
456 (unsigned int)(-(int)p_vids->p_bih->biHeight);
460 msg_Warn( p_demux, "stream[%d] unknown type", i );
464 tk->p_es = es_out_Add( p_demux->out, &fmt );
465 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
468 if( p_sys->i_track <= 0 )
470 msg_Err( p_demux, "no valid track" );
474 if( config_GetInt( p_demux, "avi-index" ) )
476 if( p_sys->b_seekable )
478 AVI_IndexCreate( p_demux );
482 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
483 AVI_IndexLoad( p_demux );
488 AVI_IndexLoad( p_demux );
491 /* *** movie length in sec *** */
492 p_sys->i_length = AVI_MovieGetLength( p_demux );
493 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
494 (mtime_t)p_avih->i_microsecperframe /
497 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
499 /* fix some BeOS MediaKit generated file */
500 for( i = 0 ; i < p_sys->i_track; i++ )
502 avi_track_t *tk = p_sys->track[i];
503 avi_chunk_list_t *p_strl;
504 avi_chunk_strh_t *p_strh;
505 avi_chunk_strf_auds_t *p_auds;
507 if( tk->i_cat != AUDIO_ES )
511 if( tk->i_idxnb < 1 ||
513 tk->i_samplesize != 0 )
517 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
518 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
519 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
521 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
522 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
524 int64_t i_track_length =
525 tk->p_index[tk->i_idxnb-1].i_length +
526 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
527 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
528 (mtime_t)p_avih->i_microsecperframe;
532 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
535 tk->i_samplesize = 1;
536 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
537 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
541 if( p_sys->b_seekable )
543 /* we have read all chunk so go back to movi */
544 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
546 /* Skip movi header */
547 stream_Read( p_demux->s, NULL, 12 );
549 p_sys->i_movi_begin = p_movi->i_chunk_pos;
555 vlc_meta_Delete( p_sys->meta );
557 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
562 /*****************************************************************************
563 * Close: frees unused data
564 *****************************************************************************/
565 static void Close ( vlc_object_t * p_this )
567 demux_t * p_demux = (demux_t *)p_this;
569 demux_sys_t *p_sys = p_demux->p_sys ;
571 for( i = 0; i < p_sys->i_track; i++ )
573 if( p_sys->track[i] )
575 FREE( p_sys->track[i]->p_index );
576 free( p_sys->track[i] );
579 FREE( p_sys->track );
580 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
581 vlc_meta_Delete( p_sys->meta );
586 /*****************************************************************************
587 * Demux_Seekable: reads and demuxes data packets for stream seekable
588 *****************************************************************************
589 * AVIDemux: reads and demuxes data packets
590 *****************************************************************************
591 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
592 *****************************************************************************/
599 off_t i_posf; /* where we will read :
600 if i_idxposb == 0 : begining of chunk (+8 to acces data)
601 else : point on data directly */
602 } avi_track_toread_t;
604 static int Demux_Seekable( demux_t *p_demux )
606 demux_sys_t *p_sys = p_demux->p_sys;
608 unsigned int i_track_count = 0;
609 unsigned int i_track;
611 /* cannot be more than 100 stream (dcXX or wbXX) */
612 avi_track_toread_t toread[100];
615 /* detect new selected/unselected streams */
616 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
618 avi_track_t *tk = p_sys->track[i_track];
621 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
622 if( b && !tk->b_activated )
624 if( p_sys->b_seekable)
626 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
628 tk->b_activated = VLC_TRUE;
630 else if( !b && tk->b_activated )
632 tk->b_activated = VLC_FALSE;
640 if( i_track_count <= 0 )
642 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
644 p_sys->i_time += 25*1000; /* read 25ms */
647 if( p_sys->i_time >= i_length )
651 msg_Warn( p_demux, "no track selected, exiting..." );
655 /* wait for the good time */
656 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
657 p_sys->i_time += 25*1000; /* read 25ms */
660 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
662 avi_track_t *tk = p_sys->track[i_track];
665 toread[i_track].b_ok = tk->b_activated;
666 if( tk->i_idxposc < tk->i_idxnb )
668 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
669 if( tk->i_idxposb > 0 )
671 toread[i_track].i_posf += 8 + tk->i_idxposb;
676 toread[i_track].i_posf = -1;
679 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
681 if( tk->i_samplesize )
683 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
687 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
692 toread[i_track].i_toread *= -1;
696 b_stream = VLC_FALSE;
707 /* search for first chunk to be read */
708 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
710 if( !toread[i].b_ok ||
711 AVI_GetDPTS( p_sys->track[i],
712 toread[i].i_toread ) <= -25 * 1000 )
717 if( toread[i].i_toread > 0 )
719 b_done = VLC_FALSE; /* not yet finished */
721 if( toread[i].i_posf > 0 )
723 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
726 i_pos = toread[i].i_posf;
738 int i_loop_count = 0;
740 /* no valid index, we will parse directly the stream
741 * in case we fail we will disable all finished stream */
742 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
744 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
745 if( AVI_PacketNext( p_demux ) )
747 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
752 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
759 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
762 "cannot get packet header, track disabled" );
763 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
765 if( avi_pk.i_stream >= p_sys->i_track ||
766 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
768 if( AVI_PacketNext( p_demux ) )
771 "cannot skip packet, track disabled" );
772 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
775 /* Prevents from eating all the CPU with broken files.
776 * This value should be low enough so that it doesn't
777 * affect the reading speed too much. */
778 if( !(++i_loop_count % 1024) )
780 if( p_demux->b_die ) return -1;
783 if( !(i_loop_count % (1024 * 10)) )
785 "don't seem to find any data..." );
791 /* add this chunk to the index */
794 index.i_id = avi_pk.i_fourcc;
796 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
798 index.i_pos = avi_pk.i_pos;
799 index.i_length = avi_pk.i_size;
800 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
802 i_track = avi_pk.i_stream;
803 tk = p_sys->track[i_track];
804 /* do we will read this data ? */
805 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
811 if( AVI_PacketNext( p_demux ) )
814 "cannot skip packet, track disabled" );
815 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
824 stream_Seek( p_demux->s, i_pos );
827 /* Set the track to use */
828 tk = p_sys->track[i_track];
830 /* read thoses data */
831 if( tk->i_samplesize )
833 unsigned int i_toread;
835 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
837 if( tk->i_samplesize > 1 )
839 i_toread = tk->i_samplesize;
843 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
846 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
852 i_size = tk->p_index[tk->i_idxposc].i_length;
855 if( tk->i_idxposb == 0 )
857 i_size += 8; /* need to read and skip header */
860 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
862 msg_Warn( p_demux, "failled reading data" );
863 tk->b_activated = VLC_FALSE;
864 toread[i_track].b_ok = VLC_FALSE;
867 if( i_size % 2 ) /* read was padded on word boundary */
872 if( tk->i_idxposb == 0 )
874 p_frame->p_buffer += 8;
875 p_frame->i_buffer -= 8;
877 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
878 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
880 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
884 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
888 if( tk->i_samplesize )
890 if( tk->i_idxposb == 0 )
894 toread[i_track].i_toread -= i_size;
895 tk->i_idxposb += i_size;
897 tk->p_index[tk->i_idxposc].i_length )
905 int i_length = tk->p_index[tk->i_idxposc].i_length;
908 if( tk->i_cat == AUDIO_ES )
910 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
912 toread[i_track].i_toread--;
915 if( tk->i_idxposc < tk->i_idxnb)
917 toread[i_track].i_posf =
918 tk->p_index[tk->i_idxposc].i_pos;
919 if( tk->i_idxposb > 0 )
921 toread[i_track].i_posf += 8 + tk->i_idxposb;
927 toread[i_track].i_posf = -1;
930 b_stream = VLC_TRUE; /* at least one read succeed */
932 if( tk->i_cat != VIDEO_ES )
933 p_frame->i_dts = p_frame->i_pts;
936 p_frame->i_dts = p_frame->i_pts;
940 //p_pes->i_rate = p_demux->stream.control.i_rate;
941 es_out_Send( p_demux->out, tk->p_es, p_frame );
946 /*****************************************************************************
947 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
948 *****************************************************************************
949 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
950 *****************************************************************************/
951 static int Demux_UnSeekable( demux_t *p_demux )
953 demux_sys_t *p_sys = p_demux->p_sys;
954 avi_track_t *p_stream_master = NULL;
955 unsigned int i_stream;
956 unsigned int i_packet;
958 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
960 /* *** find master stream for data packet skipping algo *** */
961 /* *** -> first video, if any, or first audio ES *** */
962 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
964 avi_track_t *tk = p_sys->track[i_stream];
967 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
969 if( b && tk->i_cat == VIDEO_ES )
971 p_stream_master = tk;
975 p_stream_master = tk;
979 if( !p_stream_master )
981 msg_Warn( p_demux, "no more stream selected" );
985 p_sys->i_time = AVI_GetPTS( p_stream_master );
987 for( i_packet = 0; i_packet < 10; i_packet++)
989 #define p_stream p_sys->track[avi_pk.i_stream]
993 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
998 if( avi_pk.i_stream >= p_sys->i_track ||
999 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1001 /* we haven't found an audio or video packet:
1002 * - we have seek, found first next packet
1003 * - others packets could be found, skip them
1005 switch( avi_pk.i_fourcc )
1007 case AVIFOURCC_JUNK:
1008 case AVIFOURCC_LIST:
1009 case AVIFOURCC_RIFF:
1010 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1011 case AVIFOURCC_idx1:
1014 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1016 return( 0 ); /* eof */
1019 "seems to have lost position, resync" );
1020 if( AVI_PacketSearch( p_demux ) )
1022 msg_Err( p_demux, "resync failed" );
1029 /* check for time */
1030 if( __ABS( AVI_GetPTS( p_stream ) -
1031 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1033 /* load it and send to decoder */
1035 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1039 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1041 if( avi_pk.i_cat != VIDEO_ES )
1042 p_frame->i_dts = p_frame->i_pts;
1045 p_frame->i_dts = p_frame->i_pts;
1049 //p_pes->i_rate = p_demux->stream.control.i_rate;
1050 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1054 if( AVI_PacketNext( p_demux ) )
1060 /* *** update stream time position *** */
1061 if( p_stream->i_samplesize )
1063 p_stream->i_idxposb += avi_pk.i_size;
1067 if( p_stream->i_cat == AUDIO_ES )
1069 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1071 p_stream->i_idxposc++;
1081 /*****************************************************************************
1082 * Seek: goto to i_date or i_percent
1083 *****************************************************************************/
1084 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1087 demux_sys_t *p_sys = p_demux->p_sys;
1088 unsigned int i_stream;
1089 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1090 i_date / 1000000, i_percent );
1092 if( p_sys->b_seekable )
1094 if( !p_sys->i_length )
1096 avi_track_t *p_stream;
1099 /* use i_percent to create a true i_date */
1100 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1101 " work only for interleaved file", i_percent );
1102 if( i_percent >= 100 )
1104 msg_Warn( p_demux, "cannot seek so far !" );
1105 return VLC_EGENERIC;
1107 i_percent = __MAX( i_percent, 0 );
1109 /* try to find chunk that is at i_percent or the file */
1110 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1111 p_sys->i_movi_begin );
1112 /* search first selected stream */
1113 for( i_stream = 0, p_stream = NULL;
1114 i_stream < p_sys->i_track; i_stream++ )
1116 p_stream = p_sys->track[i_stream];
1117 if( p_stream->b_activated )
1122 if( !p_stream || !p_stream->b_activated )
1124 msg_Warn( p_demux, "cannot find any selected stream" );
1125 return VLC_EGENERIC;
1128 /* be sure that the index exist */
1129 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1131 msg_Warn( p_demux, "cannot seek" );
1132 return VLC_EGENERIC;
1135 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1136 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1138 /* search after i_idxposc */
1139 if( AVI_StreamChunkSet( p_demux,
1140 i_stream, p_stream->i_idxposc + 1 ) )
1142 msg_Warn( p_demux, "cannot seek" );
1143 return VLC_EGENERIC;
1147 i_date = AVI_GetPTS( p_stream );
1148 /* TODO better support for i_samplesize != 0 */
1149 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1152 #define p_stream p_sys->track[i_stream]
1154 /* seek for chunk based streams */
1155 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1157 if( p_stream->b_activated && !p_stream->i_samplesize )
1158 /* if( p_stream->b_activated ) */
1160 AVI_TrackSeek( p_demux, i_stream, i_date );
1161 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1168 i_date = p_sys->i_time;
1170 /* seek for bytes based streams */
1171 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1173 if( p_stream->b_activated && p_stream->i_samplesize )
1175 AVI_TrackSeek( p_demux, i_stream, i_date );
1176 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1179 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1180 /* set true movie time */
1182 if( !p_sys->i_time )
1184 p_sys->i_time = i_date;
1191 msg_Err( p_demux, "shouldn't yet be executed" );
1192 return VLC_EGENERIC;
1196 /*****************************************************************************
1198 *****************************************************************************
1200 *****************************************************************************/
1201 static double ControlGetPosition( demux_t *p_demux )
1203 demux_sys_t *p_sys = p_demux->p_sys;
1205 if( p_sys->i_length > 0 )
1207 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1209 else if( stream_Size( p_demux->s ) > 0 )
1215 /* search the more advanced selected es */
1216 for( i = 0; i < p_sys->i_track; i++ )
1218 avi_track_t *tk = p_sys->track[i];
1219 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1221 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1222 tk->p_index[tk->i_idxposc].i_length + 8;
1229 return (double)i64 / (double)stream_Size( p_demux->s );
1234 static int Control( demux_t *p_demux, int i_query, va_list args )
1236 demux_sys_t *p_sys = p_demux->p_sys;
1240 vlc_meta_t **pp_meta;
1244 case DEMUX_GET_POSITION:
1245 pf = (double*)va_arg( args, double * );
1246 *pf = ControlGetPosition( p_demux );
1248 case DEMUX_SET_POSITION:
1249 f = (double)va_arg( args, double );
1250 if( p_sys->b_seekable )
1252 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1253 return Seek( p_demux, i64, (int)(f * 100) );
1257 int64_t i_pos = stream_Size( p_demux->s ) * f;
1258 return stream_Seek( p_demux->s, i_pos );
1261 case DEMUX_GET_TIME:
1262 pi64 = (int64_t*)va_arg( args, int64_t * );
1263 *pi64 = p_sys->i_time;
1266 case DEMUX_SET_TIME:
1270 i64 = (int64_t)va_arg( args, int64_t );
1271 if( p_sys->i_length > 0 )
1273 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1275 else if( p_sys->i_time > 0 )
1277 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1278 (double)i64 / (double)p_sys->i_time );
1280 return Seek( p_demux, i64, i_percent );
1282 case DEMUX_GET_LENGTH:
1283 pi64 = (int64_t*)va_arg( args, int64_t * );
1284 *pi64 = p_sys->i_length * (mtime_t)1000000;
1288 pf = (double*)va_arg( args, double * );
1290 for( i = 0; i < (int)p_sys->i_track; i++ )
1292 avi_track_t *tk = p_sys->track[i];
1293 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1295 *pf = (float)tk->i_rate / (float)tk->i_scale;
1300 case DEMUX_GET_META:
1301 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1302 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1306 return VLC_EGENERIC;
1310 /*****************************************************************************
1311 * Function to convert pts to chunk or byte
1312 *****************************************************************************/
1314 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1316 return (mtime_t)((int64_t)i_pts *
1317 (int64_t)tk->i_rate /
1318 (int64_t)tk->i_scale /
1321 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1323 return (mtime_t)((int64_t)i_pts *
1324 (int64_t)tk->i_rate /
1325 (int64_t)tk->i_scale /
1327 (int64_t)tk->i_samplesize );
1330 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1334 i_dpts = (mtime_t)( (int64_t)1000000 *
1336 (int64_t)tk->i_scale /
1337 (int64_t)tk->i_rate );
1339 if( tk->i_samplesize )
1341 return i_dpts / tk->i_samplesize;
1346 static mtime_t AVI_GetPTS( avi_track_t *tk )
1348 if( tk->i_samplesize )
1350 int64_t i_count = 0;
1352 /* we need a valid entry we will emulate one */
1353 if( tk->i_idxposc == tk->i_idxnb )
1357 /* use the last entry */
1358 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1359 + tk->p_index[tk->i_idxnb - 1].i_length;
1364 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1366 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1370 if( tk->i_cat == AUDIO_ES )
1372 return AVI_GetDPTS( tk, tk->i_blockno );
1376 return AVI_GetDPTS( tk, tk->i_idxposc );
1381 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1383 demux_sys_t *p_sys = p_demux->p_sys;
1384 avi_packet_t avi_pk;
1385 int i_loop_count = 0;
1387 /* find first chunk of i_stream that isn't in index */
1389 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1391 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1392 if( AVI_PacketNext( p_demux ) )
1394 return VLC_EGENERIC;
1399 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1404 if( p_demux->b_die ) return VLC_EGENERIC;
1406 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1408 msg_Warn( p_demux, "cannot get packet header" );
1409 return VLC_EGENERIC;
1411 if( avi_pk.i_stream >= p_sys->i_track ||
1412 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1414 if( AVI_PacketNext( p_demux ) )
1416 return VLC_EGENERIC;
1419 /* Prevents from eating all the CPU with broken files.
1420 * This value should be low enough so that it doesn't
1421 * affect the reading speed too much. */
1422 if( !(++i_loop_count % 1024) )
1424 if( p_demux->b_die ) return VLC_EGENERIC;
1427 if( !(i_loop_count % (1024 * 10)) )
1428 msg_Warn( p_demux, "don't seem to find any data..." );
1433 /* add this chunk to the index */
1436 index.i_id = avi_pk.i_fourcc;
1438 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1440 index.i_pos = avi_pk.i_pos;
1441 index.i_length = avi_pk.i_size;
1442 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1444 if( avi_pk.i_stream == i_stream )
1449 if( AVI_PacketNext( p_demux ) )
1451 return VLC_EGENERIC;
1458 /* be sure that i_ck will be a valid index entry */
1459 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1462 demux_sys_t *p_sys = p_demux->p_sys;
1463 avi_track_t *p_stream = p_sys->track[i_stream];
1465 p_stream->i_idxposc = i_ck;
1466 p_stream->i_idxposb = 0;
1468 if( i_ck >= p_stream->i_idxnb )
1470 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1473 p_stream->i_idxposc++;
1474 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1476 return VLC_EGENERIC;
1479 } while( p_stream->i_idxposc < i_ck );
1486 /* XXX FIXME up to now, we assume that all chunk are one after one */
1487 static int AVI_StreamBytesSet( demux_t *p_demux,
1488 unsigned int i_stream,
1491 demux_sys_t *p_sys = p_demux->p_sys;
1492 avi_track_t *p_stream = p_sys->track[i_stream];
1494 if( ( p_stream->i_idxnb > 0 )
1495 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1496 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1498 /* index is valid to find the ck */
1499 /* uses dichototmie to be fast enougth */
1500 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1501 int i_idxmax = p_stream->i_idxnb;
1505 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1507 i_idxmax = i_idxposc ;
1508 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1512 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1513 p_stream->p_index[i_idxposc].i_length <= i_byte)
1515 i_idxmin = i_idxposc ;
1516 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1520 p_stream->i_idxposc = i_idxposc;
1521 p_stream->i_idxposb = i_byte -
1522 p_stream->p_index[i_idxposc].i_lengthtotal;
1531 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1532 p_stream->i_idxposb = 0;
1535 p_stream->i_idxposc++;
1536 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1538 return VLC_EGENERIC;
1541 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1542 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1544 p_stream->i_idxposb = i_byte -
1545 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1550 static int AVI_TrackSeek( demux_t *p_demux,
1554 demux_sys_t *p_sys = p_demux->p_sys;
1555 avi_track_t *tk = p_sys->track[i_stream];
1557 #define p_stream p_sys->track[i_stream]
1560 i_oldpts = AVI_GetPTS( p_stream );
1562 if( !p_stream->i_samplesize )
1564 if( AVI_StreamChunkSet( p_demux,
1566 AVI_PTSToChunk( p_stream, i_date ) ) )
1568 return VLC_EGENERIC;
1571 if( p_stream->i_cat == AUDIO_ES )
1575 for( i = 0; i < tk->i_idxposc; i++ )
1577 if( tk->i_blocksize > 0 )
1579 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1589 "old:"I64Fd" %s new "I64Fd,
1591 i_oldpts > i_date ? ">" : "<",
1594 if( p_stream->i_cat == VIDEO_ES )
1596 /* search key frame */
1597 if( i_date < i_oldpts )
1599 while( p_stream->i_idxposc > 0 &&
1600 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1603 if( AVI_StreamChunkSet( p_demux,
1605 p_stream->i_idxposc - 1 ) )
1607 return VLC_EGENERIC;
1613 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1614 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1617 if( AVI_StreamChunkSet( p_demux,
1619 p_stream->i_idxposc + 1 ) )
1621 return VLC_EGENERIC;
1629 if( AVI_StreamBytesSet( p_demux,
1631 AVI_PTSToByte( p_stream, i_date ) ) )
1633 return VLC_EGENERIC;
1640 /****************************************************************************
1641 * Return VLC_TRUE if it's a key frame
1642 ****************************************************************************/
1643 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1649 * startcode: 0x00000100 32bits
1650 * framenumber ? 5bits
1651 * piture type 0(I),1(P) 2bits
1653 if( GetDWBE( p_byte ) != 0x00000100 )
1655 /* it's not an msmpegv1 stream, strange...*/
1656 return AVIIF_KEYFRAME;
1658 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1661 case FOURCC_DIV3: /* wmv1 also */
1663 * picture type 0(I),1(P) 2bits
1665 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1667 /* we should find first occurrence of 0x000001b6 (32bits)
1668 * startcode: 0x000001b6 32bits
1669 * piture type 0(I),1(P) 2bits
1671 if( GetDWBE( p_byte ) != 0x000001b6 )
1673 /* not true , need to find the first VOP header */
1674 return AVIIF_KEYFRAME;
1676 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1679 /* I can't do it, so say yes */
1680 return AVIIF_KEYFRAME;
1684 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1689 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1693 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1697 return VLC_FOURCC('m','r','l','e');
1746 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1750 /****************************************************************************
1752 ****************************************************************************/
1753 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1754 int *pi_number, int *pi_type )
1756 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1759 c1 = ((uint8_t *)&i_id)[0];
1760 c2 = ((uint8_t *)&i_id)[1];
1762 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1764 SET_PTR( pi_number, 100 ); /* > max stream number */
1765 SET_PTR( pi_type, UNKNOWN_ES );
1769 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1770 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1773 SET_PTR( pi_type, AUDIO_ES );
1777 SET_PTR( pi_type, VIDEO_ES );
1780 SET_PTR( pi_type, UNKNOWN_ES );
1787 /****************************************************************************
1789 ****************************************************************************/
1790 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1794 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1796 return VLC_EGENERIC;
1798 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1799 p_pk->i_size = GetDWLE( p_peek + 4 );
1800 p_pk->i_pos = stream_Tell( p_demux->s );
1801 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1803 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1804 p_peek[10], p_peek[11] );
1811 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1813 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1817 static int AVI_PacketNext( demux_t *p_demux )
1819 avi_packet_t avi_ck;
1822 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1824 return VLC_EGENERIC;
1827 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1828 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1832 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1833 avi_ck.i_type == AVIFOURCC_AVIX )
1839 i_skip = __EVEN( avi_ck.i_size ) + 8;
1842 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1844 return VLC_EGENERIC;
1848 static int AVI_PacketRead( demux_t *p_demux,
1850 block_t **pp_frame )
1854 i_size = __EVEN( p_pk->i_size + 8 );
1856 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1858 return VLC_EGENERIC;
1860 (*pp_frame)->p_buffer += 8;
1861 (*pp_frame)->i_buffer -= 8;
1863 if( i_size != p_pk->i_size + 8 )
1865 (*pp_frame)->i_buffer--;
1871 static int AVI_PacketSearch( demux_t *p_demux )
1873 demux_sys_t *p_sys = p_demux->p_sys;
1874 avi_packet_t avi_pk;
1879 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1881 return VLC_EGENERIC;
1883 AVI_PacketGetHeader( p_demux, &avi_pk );
1884 if( avi_pk.i_stream < p_sys->i_track &&
1885 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1889 switch( avi_pk.i_fourcc )
1891 case AVIFOURCC_JUNK:
1892 case AVIFOURCC_LIST:
1893 case AVIFOURCC_RIFF:
1894 case AVIFOURCC_idx1:
1898 /* Prevents from eating all the CPU with broken files.
1899 * This value should be low enough so that it doesn't affect the
1900 * reading speed too much (not that we care much anyway because
1901 * this code is called only on broken files). */
1902 if( !(++i_count % 1024) )
1904 if( p_demux->b_die ) return VLC_EGENERIC;
1907 if( !(i_count % (1024 * 10)) )
1908 msg_Warn( p_demux, "trying to resync..." );
1913 /****************************************************************************
1915 ****************************************************************************/
1916 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1918 avi_entry_t *p_index)
1920 avi_track_t *tk = p_sys->track[i_stream];
1922 /* Update i_movi_lastchunk_pos */
1923 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1925 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1929 if( tk->i_idxnb >= tk->i_idxmax )
1931 tk->i_idxmax += 16384;
1932 tk->p_index = realloc( tk->p_index,
1933 tk->i_idxmax * sizeof( avi_entry_t ) );
1934 if( tk->p_index == NULL )
1939 /* calculate cumulate length */
1940 if( tk->i_idxnb > 0 )
1942 p_index->i_lengthtotal =
1943 tk->p_index[tk->i_idxnb - 1].i_length +
1944 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1948 p_index->i_lengthtotal = 0;
1951 tk->p_index[tk->i_idxnb++] = *p_index;
1954 static int AVI_IndexLoad_idx1( demux_t *p_demux )
1956 demux_sys_t *p_sys = p_demux->p_sys;
1958 avi_chunk_list_t *p_riff;
1959 avi_chunk_list_t *p_movi;
1960 avi_chunk_idx1_t *p_idx1;
1962 unsigned int i_stream;
1963 unsigned int i_index;
1967 vlc_bool_t b_keyset[100];
1969 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1970 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1971 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1975 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
1976 return VLC_EGENERIC;
1979 /* *** calculate offset *** */
1980 /* Well, avi is __SHIT__ so test more than one entry
1981 * (needed for some avi files) */
1983 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1985 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1987 i_offset = p_movi->i_chunk_pos + 8;
1992 /* Reset b_keyset */
1993 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1994 b_keyset[i_stream] = VLC_FALSE;
1996 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2000 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2003 if( i_stream < p_sys->i_track &&
2004 i_cat == p_sys->track[i_stream]->i_cat )
2007 index.i_id = p_idx1->entry[i_index].i_fourcc;
2009 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2010 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2011 index.i_length = p_idx1->entry[i_index].i_length;
2012 AVI_IndexAddEntry( p_sys, i_stream, &index );
2014 if( index.i_flags&AVIIF_KEYFRAME )
2015 b_keyset[i_stream] = VLC_TRUE;
2019 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2021 if( !b_keyset[i_stream] )
2023 avi_track_t *tk = p_sys->track[i_stream];
2025 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2026 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2027 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2033 static void __Parse_indx( demux_t *p_demux,
2035 avi_chunk_indx_t *p_indx )
2037 demux_sys_t *p_sys = p_demux->p_sys;
2041 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2042 if( p_indx->i_indexsubtype == 0 )
2044 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2046 index.i_id = p_indx->i_id;
2047 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2048 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2049 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2051 AVI_IndexAddEntry( p_sys, i_stream, &index );
2054 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2056 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2058 index.i_id = p_indx->i_id;
2059 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2060 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2061 index.i_length = p_indx->idx.field[i].i_size;
2063 AVI_IndexAddEntry( p_sys, i_stream, &index );
2068 msg_Warn( p_demux, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
2072 static void AVI_IndexLoad_indx( demux_t *p_demux )
2074 demux_sys_t *p_sys = p_demux->p_sys;
2075 unsigned int i_stream;
2078 avi_chunk_list_t *p_riff;
2079 avi_chunk_list_t *p_hdrl;
2081 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2082 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2084 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2086 avi_chunk_list_t *p_strl;
2087 avi_chunk_indx_t *p_indx;
2089 #define p_stream p_sys->track[i_stream]
2090 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2091 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2095 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2099 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2101 __Parse_indx( p_demux, i_stream, p_indx );
2103 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2105 avi_chunk_indx_t ck_sub;
2106 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2108 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2109 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2113 __Parse_indx( p_demux, i_stream, &ck_sub );
2118 msg_Warn( p_demux, "unknow type index(0x%x)", p_indx->i_indextype );
2124 static void AVI_IndexLoad( demux_t *p_demux )
2126 demux_sys_t *p_sys = p_demux->p_sys;
2127 unsigned int i_stream;
2129 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2131 p_sys->track[i_stream]->i_idxnb = 0;
2132 p_sys->track[i_stream]->i_idxmax = 0;
2133 p_sys->track[i_stream]->p_index = NULL;
2138 AVI_IndexLoad_indx( p_demux );
2140 else if( AVI_IndexLoad_idx1( p_demux ) )
2142 /* try indx if idx1 failed as some "normal" file have indx too */
2143 AVI_IndexLoad_indx( p_demux );
2146 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2148 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2149 i_stream, p_sys->track[i_stream]->i_idxnb );
2153 static void AVI_IndexCreate( demux_t *p_demux )
2155 demux_sys_t *p_sys = p_demux->p_sys;
2157 avi_chunk_list_t *p_riff;
2158 avi_chunk_list_t *p_movi;
2160 unsigned int i_stream;
2163 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2164 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2168 msg_Err( p_demux, "cannot find p_movi" );
2172 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2174 p_sys->track[i_stream]->i_idxnb = 0;
2175 p_sys->track[i_stream]->i_idxmax = 0;
2176 p_sys->track[i_stream]->p_index = NULL;
2178 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2179 stream_Size( p_demux->s ) );
2181 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2182 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2187 if( p_demux->b_die )
2192 if( AVI_PacketGetHeader( p_demux, &pk ) )
2196 if( pk.i_stream < p_sys->i_track &&
2197 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2200 index.i_id = pk.i_fourcc;
2202 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2203 index.i_pos = pk.i_pos;
2204 index.i_length = pk.i_size;
2205 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2209 switch( pk.i_fourcc )
2211 case AVIFOURCC_idx1:
2214 avi_chunk_list_t *p_sysx;
2215 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2216 AVIFOURCC_RIFF, 1 );
2218 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2219 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2226 case AVIFOURCC_RIFF:
2227 msg_Dbg( p_demux, "new RIFF chunk found" );
2229 case AVIFOURCC_JUNK:
2232 msg_Warn( p_demux, "need resync, probably broken avi" );
2233 if( AVI_PacketSearch( p_demux ) )
2235 msg_Warn( p_demux, "lost sync, abord index creation" );
2241 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2242 AVI_PacketNext( p_demux ) )
2249 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2252 "stream[%d] creating %d index entries",
2254 p_sys->track[i_stream]->i_idxnb );
2258 /*****************************************************************************
2260 *****************************************************************************/
2261 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2263 demux_sys_t *p_sys = p_demux->p_sys;
2265 int b_end = VLC_TRUE;
2267 for( i = 0; i < p_sys->i_track; i++ )
2269 avi_track_t *tk = p_sys->track[i];
2270 if( tk->i_idxposc >= tk->i_idxnb )
2272 tk->b_activated = VLC_FALSE;
2273 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2283 /****************************************************************************
2284 * AVI_MovieGetLength give max streams length in second
2285 ****************************************************************************/
2286 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2288 demux_sys_t *p_sys = p_demux->p_sys;
2289 mtime_t i_maxlength = 0;
2292 for( i = 0; i < p_sys->i_track; i++ )
2294 avi_track_t *tk = p_sys->track[i];
2297 /* fix length for each stream */
2298 if( tk->i_idxnb < 1 || !tk->p_index )
2303 if( tk->i_samplesize )
2305 i_length = AVI_GetDPTS( tk,
2306 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2307 tk->p_index[tk->i_idxnb-1].i_length );
2311 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2313 i_length /= (mtime_t)1000000; /* in seconds */
2316 "stream[%d] length:"I64Fd" (based on index)",
2319 i_maxlength = __MAX( i_maxlength, i_length );