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_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
333 avi_chunk_strf_auds_t *p_auds;
334 avi_chunk_strf_vids_t *p_vids;
337 tk->b_activated = VLC_FALSE;
347 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
348 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
350 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
352 msg_Warn( p_demux, "stream[%d] incomplete", i );
356 tk->i_rate = p_strh->i_rate;
357 tk->i_scale = p_strh->i_scale;
358 tk->i_samplesize = p_strh->i_samplesize;
359 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
360 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
362 switch( p_strh->i_type )
364 case( AVIFOURCC_auds ):
365 tk->i_cat = AUDIO_ES;
366 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
367 p_auds->p_wf->wFormatTag );
368 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
370 if( p_auds->p_wf->wFormatTag == 1 )
372 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
379 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
381 fmt.audio.i_channels = p_auds->p_wf->nChannels;
382 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
383 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
384 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
385 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
386 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
387 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
388 fmt.p_extra = &p_auds->p_wf[1];
389 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
390 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
391 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
394 case( AVIFOURCC_vids ):
395 tk->i_cat = VIDEO_ES;
396 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
397 p_vids->p_bih->biCompression );
398 if( p_vids->p_bih->biCompression == 0x00 )
400 switch( p_vids->p_bih->biBitCount )
403 tk->i_codec = VLC_FOURCC('R','V','3','2');
406 tk->i_codec = VLC_FOURCC('R','V','2','4');
409 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
412 tk->i_codec = VLC_FOURCC('R','V','1','5');
415 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
418 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
420 if( p_vids->p_bih->biBitCount == 24 )
422 /* This is in BGR format */
423 fmt.video.i_bmask = 0x00ff0000;
424 fmt.video.i_gmask = 0x0000ff00;
425 fmt.video.i_rmask = 0x000000ff;
430 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
431 if( tk->i_codec == FOURCC_mp4v &&
432 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
434 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
437 tk->i_samplesize = 0;
438 fmt.video.i_width = p_vids->p_bih->biWidth;
439 fmt.video.i_height = p_vids->p_bih->biHeight;
440 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
442 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
443 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
444 fmt.p_extra = &p_vids->p_bih[1];
445 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
447 (char*)&p_vids->p_bih->biCompression,
448 p_vids->p_bih->biWidth,
449 p_vids->p_bih->biHeight,
450 p_vids->p_bih->biBitCount,
451 (float)tk->i_rate/(float)tk->i_scale );
453 if( p_vids->p_bih->biCompression == 0x00 )
455 /* RGB DIB are coded from bottom to top */
457 (unsigned int)(-(int)p_vids->p_bih->biHeight);
461 case( AVIFOURCC_txts):
463 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
464 msg_Dbg( p_demux, "stream[%d] subtitles", i );
465 es_format_Init( &fmt, SPU_ES, tk->i_codec );
468 case( AVIFOURCC_mids):
469 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
471 msg_Warn( p_demux, "stream[%d] unknown type", i );
477 fmt.psz_description = strdup( p_strn->p_str );
479 tk->p_es = es_out_Add( p_demux->out, &fmt );
480 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
483 if( p_sys->i_track <= 0 )
485 msg_Err( p_demux, "no valid track" );
489 if( config_GetInt( p_demux, "avi-index" ) )
491 if( p_sys->b_seekable )
493 AVI_IndexCreate( p_demux );
497 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
498 AVI_IndexLoad( p_demux );
503 AVI_IndexLoad( p_demux );
506 /* *** movie length in sec *** */
507 p_sys->i_length = AVI_MovieGetLength( p_demux );
508 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
509 (mtime_t)p_avih->i_microsecperframe /
512 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
514 /* fix some BeOS MediaKit generated file */
515 for( i = 0 ; i < p_sys->i_track; i++ )
517 avi_track_t *tk = p_sys->track[i];
518 avi_chunk_list_t *p_strl;
519 avi_chunk_strh_t *p_strh;
520 avi_chunk_strf_auds_t *p_auds;
522 if( tk->i_cat != AUDIO_ES )
526 if( tk->i_idxnb < 1 ||
528 tk->i_samplesize != 0 )
532 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
533 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
534 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
536 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
537 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
539 int64_t i_track_length =
540 tk->p_index[tk->i_idxnb-1].i_length +
541 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
542 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
543 (mtime_t)p_avih->i_microsecperframe;
547 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
550 tk->i_samplesize = 1;
551 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
552 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
556 if( p_sys->b_seekable )
558 /* we have read all chunk so go back to movi */
559 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
561 /* Skip movi header */
562 stream_Read( p_demux->s, NULL, 12 );
564 p_sys->i_movi_begin = p_movi->i_chunk_pos;
570 vlc_meta_Delete( p_sys->meta );
572 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
577 /*****************************************************************************
578 * Close: frees unused data
579 *****************************************************************************/
580 static void Close ( vlc_object_t * p_this )
582 demux_t * p_demux = (demux_t *)p_this;
584 demux_sys_t *p_sys = p_demux->p_sys ;
586 for( i = 0; i < p_sys->i_track; i++ )
588 if( p_sys->track[i] )
590 FREE( p_sys->track[i]->p_index );
591 free( p_sys->track[i] );
594 FREE( p_sys->track );
595 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
596 vlc_meta_Delete( p_sys->meta );
601 /*****************************************************************************
602 * Demux_Seekable: reads and demuxes data packets for stream seekable
603 *****************************************************************************
604 * AVIDemux: reads and demuxes data packets
605 *****************************************************************************
606 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
607 *****************************************************************************/
614 off_t i_posf; /* where we will read :
615 if i_idxposb == 0 : begining of chunk (+8 to acces data)
616 else : point on data directly */
617 } avi_track_toread_t;
619 static int Demux_Seekable( demux_t *p_demux )
621 demux_sys_t *p_sys = p_demux->p_sys;
623 unsigned int i_track_count = 0;
624 unsigned int i_track;
626 /* cannot be more than 100 stream (dcXX or wbXX) */
627 avi_track_toread_t toread[100];
630 /* detect new selected/unselected streams */
631 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
633 avi_track_t *tk = p_sys->track[i_track];
636 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
637 if( b && !tk->b_activated )
639 if( p_sys->b_seekable)
641 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
643 tk->b_activated = VLC_TRUE;
645 else if( !b && tk->b_activated )
647 tk->b_activated = VLC_FALSE;
655 if( i_track_count <= 0 )
657 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
659 p_sys->i_time += 25*1000; /* read 25ms */
662 if( p_sys->i_time >= i_length )
666 msg_Warn( p_demux, "no track selected, exiting..." );
670 /* wait for the good time */
671 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
672 p_sys->i_time += 25*1000; /* read 25ms */
675 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
677 avi_track_t *tk = p_sys->track[i_track];
680 toread[i_track].b_ok = tk->b_activated;
681 if( tk->i_idxposc < tk->i_idxnb )
683 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
684 if( tk->i_idxposb > 0 )
686 toread[i_track].i_posf += 8 + tk->i_idxposb;
691 toread[i_track].i_posf = -1;
694 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
696 if( tk->i_samplesize )
698 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
702 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
707 toread[i_track].i_toread *= -1;
711 b_stream = VLC_FALSE;
722 /* search for first chunk to be read */
723 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
725 if( !toread[i].b_ok ||
726 AVI_GetDPTS( p_sys->track[i],
727 toread[i].i_toread ) <= -25 * 1000 )
732 if( toread[i].i_toread > 0 )
734 b_done = VLC_FALSE; /* not yet finished */
736 if( toread[i].i_posf > 0 )
738 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
741 i_pos = toread[i].i_posf;
753 int i_loop_count = 0;
755 /* no valid index, we will parse directly the stream
756 * in case we fail we will disable all finished stream */
757 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
759 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
760 if( AVI_PacketNext( p_demux ) )
762 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
767 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
774 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
777 "cannot get packet header, track disabled" );
778 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
780 if( avi_pk.i_stream >= p_sys->i_track ||
781 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
783 if( AVI_PacketNext( p_demux ) )
786 "cannot skip packet, track disabled" );
787 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
790 /* Prevents from eating all the CPU with broken files.
791 * This value should be low enough so that it doesn't
792 * affect the reading speed too much. */
793 if( !(++i_loop_count % 1024) )
795 if( p_demux->b_die ) return -1;
798 if( !(i_loop_count % (1024 * 10)) )
800 "don't seem to find any data..." );
806 /* add this chunk to the index */
809 index.i_id = avi_pk.i_fourcc;
811 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
813 index.i_pos = avi_pk.i_pos;
814 index.i_length = avi_pk.i_size;
815 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
817 i_track = avi_pk.i_stream;
818 tk = p_sys->track[i_track];
819 /* do we will read this data ? */
820 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
826 if( AVI_PacketNext( p_demux ) )
829 "cannot skip packet, track disabled" );
830 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
839 stream_Seek( p_demux->s, i_pos );
842 /* Set the track to use */
843 tk = p_sys->track[i_track];
845 /* read thoses data */
846 if( tk->i_samplesize )
848 unsigned int i_toread;
850 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
852 if( tk->i_samplesize > 1 )
854 i_toread = tk->i_samplesize;
858 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
859 i_toread = __MAX( i_toread, 100 );
862 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
868 i_size = tk->p_index[tk->i_idxposc].i_length;
871 if( tk->i_idxposb == 0 )
873 i_size += 8; /* need to read and skip header */
876 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
878 msg_Warn( p_demux, "failled reading data" );
879 tk->b_activated = VLC_FALSE;
880 toread[i_track].b_ok = VLC_FALSE;
883 if( i_size % 2 ) /* read was padded on word boundary */
888 if( tk->i_idxposb == 0 )
890 p_frame->p_buffer += 8;
891 p_frame->i_buffer -= 8;
893 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
894 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
896 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
900 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
904 if( tk->i_samplesize )
906 if( tk->i_idxposb == 0 )
910 toread[i_track].i_toread -= i_size;
911 tk->i_idxposb += i_size;
913 tk->p_index[tk->i_idxposc].i_length )
921 int i_length = tk->p_index[tk->i_idxposc].i_length;
924 if( tk->i_cat == AUDIO_ES )
926 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
928 toread[i_track].i_toread--;
931 if( tk->i_idxposc < tk->i_idxnb)
933 toread[i_track].i_posf =
934 tk->p_index[tk->i_idxposc].i_pos;
935 if( tk->i_idxposb > 0 )
937 toread[i_track].i_posf += 8 + tk->i_idxposb;
943 toread[i_track].i_posf = -1;
946 b_stream = VLC_TRUE; /* at least one read succeed */
948 if( tk->i_cat != VIDEO_ES )
949 p_frame->i_dts = p_frame->i_pts;
952 p_frame->i_dts = p_frame->i_pts;
956 //p_pes->i_rate = p_demux->stream.control.i_rate;
957 es_out_Send( p_demux->out, tk->p_es, p_frame );
962 /*****************************************************************************
963 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
964 *****************************************************************************
965 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
966 *****************************************************************************/
967 static int Demux_UnSeekable( demux_t *p_demux )
969 demux_sys_t *p_sys = p_demux->p_sys;
970 avi_track_t *p_stream_master = NULL;
971 unsigned int i_stream;
972 unsigned int i_packet;
974 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
976 /* *** find master stream for data packet skipping algo *** */
977 /* *** -> first video, if any, or first audio ES *** */
978 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
980 avi_track_t *tk = p_sys->track[i_stream];
983 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
985 if( b && tk->i_cat == VIDEO_ES )
987 p_stream_master = tk;
991 p_stream_master = tk;
995 if( !p_stream_master )
997 msg_Warn( p_demux, "no more stream selected" );
1001 p_sys->i_time = AVI_GetPTS( p_stream_master );
1003 for( i_packet = 0; i_packet < 10; i_packet++)
1005 #define p_stream p_sys->track[avi_pk.i_stream]
1007 avi_packet_t avi_pk;
1009 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1014 if( avi_pk.i_stream >= p_sys->i_track ||
1015 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1017 /* we haven't found an audio or video packet:
1018 * - we have seek, found first next packet
1019 * - others packets could be found, skip them
1021 switch( avi_pk.i_fourcc )
1023 case AVIFOURCC_JUNK:
1024 case AVIFOURCC_LIST:
1025 case AVIFOURCC_RIFF:
1026 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1027 case AVIFOURCC_idx1:
1030 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1032 return( 0 ); /* eof */
1035 "seems to have lost position, resync" );
1036 if( AVI_PacketSearch( p_demux ) )
1038 msg_Err( p_demux, "resync failed" );
1045 /* check for time */
1046 if( __ABS( AVI_GetPTS( p_stream ) -
1047 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1049 /* load it and send to decoder */
1051 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1055 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1057 if( avi_pk.i_cat != VIDEO_ES )
1058 p_frame->i_dts = p_frame->i_pts;
1061 p_frame->i_dts = p_frame->i_pts;
1065 //p_pes->i_rate = p_demux->stream.control.i_rate;
1066 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1070 if( AVI_PacketNext( p_demux ) )
1076 /* *** update stream time position *** */
1077 if( p_stream->i_samplesize )
1079 p_stream->i_idxposb += avi_pk.i_size;
1083 if( p_stream->i_cat == AUDIO_ES )
1085 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1087 p_stream->i_idxposc++;
1097 /*****************************************************************************
1098 * Seek: goto to i_date or i_percent
1099 *****************************************************************************/
1100 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1103 demux_sys_t *p_sys = p_demux->p_sys;
1104 unsigned int i_stream;
1105 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1106 i_date / 1000000, i_percent );
1108 if( p_sys->b_seekable )
1110 if( !p_sys->i_length )
1112 avi_track_t *p_stream;
1115 /* use i_percent to create a true i_date */
1116 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1117 " work only for interleaved file", i_percent );
1118 if( i_percent >= 100 )
1120 msg_Warn( p_demux, "cannot seek so far !" );
1121 return VLC_EGENERIC;
1123 i_percent = __MAX( i_percent, 0 );
1125 /* try to find chunk that is at i_percent or the file */
1126 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1127 p_sys->i_movi_begin );
1128 /* search first selected stream */
1129 for( i_stream = 0, p_stream = NULL;
1130 i_stream < p_sys->i_track; i_stream++ )
1132 p_stream = p_sys->track[i_stream];
1133 if( p_stream->b_activated )
1138 if( !p_stream || !p_stream->b_activated )
1140 msg_Warn( p_demux, "cannot find any selected stream" );
1141 return VLC_EGENERIC;
1144 /* be sure that the index exist */
1145 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1147 msg_Warn( p_demux, "cannot seek" );
1148 return VLC_EGENERIC;
1151 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1152 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1154 /* search after i_idxposc */
1155 if( AVI_StreamChunkSet( p_demux,
1156 i_stream, p_stream->i_idxposc + 1 ) )
1158 msg_Warn( p_demux, "cannot seek" );
1159 return VLC_EGENERIC;
1163 i_date = AVI_GetPTS( p_stream );
1164 /* TODO better support for i_samplesize != 0 */
1165 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1168 #define p_stream p_sys->track[i_stream]
1170 /* seek for chunk 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 )
1174 /* if( p_stream->b_activated ) */
1176 AVI_TrackSeek( p_demux, i_stream, i_date );
1177 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1184 i_date = p_sys->i_time;
1186 /* seek for bytes based streams */
1187 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1189 if( p_stream->b_activated && p_stream->i_samplesize )
1191 AVI_TrackSeek( p_demux, i_stream, i_date );
1192 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1195 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1196 /* set true movie time */
1198 if( !p_sys->i_time )
1200 p_sys->i_time = i_date;
1207 msg_Err( p_demux, "shouldn't yet be executed" );
1208 return VLC_EGENERIC;
1212 /*****************************************************************************
1214 *****************************************************************************
1216 *****************************************************************************/
1217 static double ControlGetPosition( demux_t *p_demux )
1219 demux_sys_t *p_sys = p_demux->p_sys;
1221 if( p_sys->i_length > 0 )
1223 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1225 else if( stream_Size( p_demux->s ) > 0 )
1231 /* search the more advanced selected es */
1232 for( i = 0; i < p_sys->i_track; i++ )
1234 avi_track_t *tk = p_sys->track[i];
1235 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1237 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1238 tk->p_index[tk->i_idxposc].i_length + 8;
1245 return (double)i64 / (double)stream_Size( p_demux->s );
1250 static int Control( demux_t *p_demux, int i_query, va_list args )
1252 demux_sys_t *p_sys = p_demux->p_sys;
1256 vlc_meta_t **pp_meta;
1260 case DEMUX_GET_POSITION:
1261 pf = (double*)va_arg( args, double * );
1262 *pf = ControlGetPosition( p_demux );
1264 case DEMUX_SET_POSITION:
1265 f = (double)va_arg( args, double );
1266 if( p_sys->b_seekable )
1268 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1269 return Seek( p_demux, i64, (int)(f * 100) );
1273 int64_t i_pos = stream_Size( p_demux->s ) * f;
1274 return stream_Seek( p_demux->s, i_pos );
1277 case DEMUX_GET_TIME:
1278 pi64 = (int64_t*)va_arg( args, int64_t * );
1279 *pi64 = p_sys->i_time;
1282 case DEMUX_SET_TIME:
1286 i64 = (int64_t)va_arg( args, int64_t );
1287 if( p_sys->i_length > 0 )
1289 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1291 else if( p_sys->i_time > 0 )
1293 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1294 (double)i64 / (double)p_sys->i_time );
1296 return Seek( p_demux, i64, i_percent );
1298 case DEMUX_GET_LENGTH:
1299 pi64 = (int64_t*)va_arg( args, int64_t * );
1300 *pi64 = p_sys->i_length * (mtime_t)1000000;
1304 pf = (double*)va_arg( args, double * );
1306 for( i = 0; i < (int)p_sys->i_track; i++ )
1308 avi_track_t *tk = p_sys->track[i];
1309 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1311 *pf = (float)tk->i_rate / (float)tk->i_scale;
1316 case DEMUX_GET_META:
1317 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1318 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1322 return VLC_EGENERIC;
1326 /*****************************************************************************
1327 * Function to convert pts to chunk or byte
1328 *****************************************************************************/
1330 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1332 return (mtime_t)((int64_t)i_pts *
1333 (int64_t)tk->i_rate /
1334 (int64_t)tk->i_scale /
1337 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1339 return (mtime_t)((int64_t)i_pts *
1340 (int64_t)tk->i_rate /
1341 (int64_t)tk->i_scale /
1343 (int64_t)tk->i_samplesize );
1346 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1350 i_dpts = (mtime_t)( (int64_t)1000000 *
1352 (int64_t)tk->i_scale /
1353 (int64_t)tk->i_rate );
1355 if( tk->i_samplesize )
1357 return i_dpts / tk->i_samplesize;
1362 static mtime_t AVI_GetPTS( avi_track_t *tk )
1364 if( tk->i_samplesize )
1366 int64_t i_count = 0;
1368 /* we need a valid entry we will emulate one */
1369 if( tk->i_idxposc == tk->i_idxnb )
1373 /* use the last entry */
1374 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1375 + tk->p_index[tk->i_idxnb - 1].i_length;
1380 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1382 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1386 if( tk->i_cat == AUDIO_ES )
1388 return AVI_GetDPTS( tk, tk->i_blockno );
1392 return AVI_GetDPTS( tk, tk->i_idxposc );
1397 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1399 demux_sys_t *p_sys = p_demux->p_sys;
1400 avi_packet_t avi_pk;
1401 int i_loop_count = 0;
1403 /* find first chunk of i_stream that isn't in index */
1405 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1407 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1408 if( AVI_PacketNext( p_demux ) )
1410 return VLC_EGENERIC;
1415 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1420 if( p_demux->b_die ) return VLC_EGENERIC;
1422 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1424 msg_Warn( p_demux, "cannot get packet header" );
1425 return VLC_EGENERIC;
1427 if( avi_pk.i_stream >= p_sys->i_track ||
1428 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1430 if( AVI_PacketNext( p_demux ) )
1432 return VLC_EGENERIC;
1435 /* Prevents from eating all the CPU with broken files.
1436 * This value should be low enough so that it doesn't
1437 * affect the reading speed too much. */
1438 if( !(++i_loop_count % 1024) )
1440 if( p_demux->b_die ) return VLC_EGENERIC;
1443 if( !(i_loop_count % (1024 * 10)) )
1444 msg_Warn( p_demux, "don't seem to find any data..." );
1449 /* add this chunk to the index */
1452 index.i_id = avi_pk.i_fourcc;
1454 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1456 index.i_pos = avi_pk.i_pos;
1457 index.i_length = avi_pk.i_size;
1458 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1460 if( avi_pk.i_stream == i_stream )
1465 if( AVI_PacketNext( p_demux ) )
1467 return VLC_EGENERIC;
1474 /* be sure that i_ck will be a valid index entry */
1475 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1478 demux_sys_t *p_sys = p_demux->p_sys;
1479 avi_track_t *p_stream = p_sys->track[i_stream];
1481 p_stream->i_idxposc = i_ck;
1482 p_stream->i_idxposb = 0;
1484 if( i_ck >= p_stream->i_idxnb )
1486 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1489 p_stream->i_idxposc++;
1490 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1492 return VLC_EGENERIC;
1495 } while( p_stream->i_idxposc < i_ck );
1502 /* XXX FIXME up to now, we assume that all chunk are one after one */
1503 static int AVI_StreamBytesSet( demux_t *p_demux,
1504 unsigned int i_stream,
1507 demux_sys_t *p_sys = p_demux->p_sys;
1508 avi_track_t *p_stream = p_sys->track[i_stream];
1510 if( ( p_stream->i_idxnb > 0 )
1511 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1512 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1514 /* index is valid to find the ck */
1515 /* uses dichototmie to be fast enougth */
1516 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1517 int i_idxmax = p_stream->i_idxnb;
1521 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1523 i_idxmax = i_idxposc ;
1524 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1528 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1529 p_stream->p_index[i_idxposc].i_length <= i_byte)
1531 i_idxmin = i_idxposc ;
1532 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1536 p_stream->i_idxposc = i_idxposc;
1537 p_stream->i_idxposb = i_byte -
1538 p_stream->p_index[i_idxposc].i_lengthtotal;
1547 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1548 p_stream->i_idxposb = 0;
1551 p_stream->i_idxposc++;
1552 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1554 return VLC_EGENERIC;
1557 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1558 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1560 p_stream->i_idxposb = i_byte -
1561 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1566 static int AVI_TrackSeek( demux_t *p_demux,
1570 demux_sys_t *p_sys = p_demux->p_sys;
1571 avi_track_t *tk = p_sys->track[i_stream];
1573 #define p_stream p_sys->track[i_stream]
1576 i_oldpts = AVI_GetPTS( p_stream );
1578 if( !p_stream->i_samplesize )
1580 if( AVI_StreamChunkSet( p_demux,
1582 AVI_PTSToChunk( p_stream, i_date ) ) )
1584 return VLC_EGENERIC;
1587 if( p_stream->i_cat == AUDIO_ES )
1591 for( i = 0; i < tk->i_idxposc; i++ )
1593 if( tk->i_blocksize > 0 )
1595 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1605 "old:"I64Fd" %s new "I64Fd,
1607 i_oldpts > i_date ? ">" : "<",
1610 if( p_stream->i_cat == VIDEO_ES )
1612 /* search key frame */
1613 if( i_date < i_oldpts )
1615 while( p_stream->i_idxposc > 0 &&
1616 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1619 if( AVI_StreamChunkSet( p_demux,
1621 p_stream->i_idxposc - 1 ) )
1623 return VLC_EGENERIC;
1629 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1630 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1633 if( AVI_StreamChunkSet( p_demux,
1635 p_stream->i_idxposc + 1 ) )
1637 return VLC_EGENERIC;
1645 if( AVI_StreamBytesSet( p_demux,
1647 AVI_PTSToByte( p_stream, i_date ) ) )
1649 return VLC_EGENERIC;
1656 /****************************************************************************
1657 * Return VLC_TRUE if it's a key frame
1658 ****************************************************************************/
1659 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1665 * startcode: 0x00000100 32bits
1666 * framenumber ? 5bits
1667 * piture type 0(I),1(P) 2bits
1669 if( GetDWBE( p_byte ) != 0x00000100 )
1671 /* it's not an msmpegv1 stream, strange...*/
1672 return AVIIF_KEYFRAME;
1674 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1677 case FOURCC_DIV3: /* wmv1 also */
1679 * picture type 0(I),1(P) 2bits
1681 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1683 /* we should find first occurrence of 0x000001b6 (32bits)
1684 * startcode: 0x000001b6 32bits
1685 * piture type 0(I),1(P) 2bits
1687 if( GetDWBE( p_byte ) != 0x000001b6 )
1689 /* not true , need to find the first VOP header */
1690 return AVIIF_KEYFRAME;
1692 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1695 /* I can't do it, so say yes */
1696 return AVIIF_KEYFRAME;
1700 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1705 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1709 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1713 return VLC_FOURCC('m','r','l','e');
1762 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1766 /****************************************************************************
1768 ****************************************************************************/
1769 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1770 int *pi_number, int *pi_type )
1772 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1775 c1 = ((uint8_t *)&i_id)[0];
1776 c2 = ((uint8_t *)&i_id)[1];
1778 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1780 SET_PTR( pi_number, 100 ); /* > max stream number */
1781 SET_PTR( pi_type, UNKNOWN_ES );
1785 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1786 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1789 SET_PTR( pi_type, AUDIO_ES );
1793 SET_PTR( pi_type, VIDEO_ES );
1796 SET_PTR( pi_type, UNKNOWN_ES );
1803 /****************************************************************************
1805 ****************************************************************************/
1806 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1810 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1812 return VLC_EGENERIC;
1814 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1815 p_pk->i_size = GetDWLE( p_peek + 4 );
1816 p_pk->i_pos = stream_Tell( p_demux->s );
1817 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1819 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1820 p_peek[10], p_peek[11] );
1827 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1829 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1833 static int AVI_PacketNext( demux_t *p_demux )
1835 avi_packet_t avi_ck;
1838 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1840 return VLC_EGENERIC;
1843 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1844 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1848 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1849 avi_ck.i_type == AVIFOURCC_AVIX )
1855 i_skip = __EVEN( avi_ck.i_size ) + 8;
1858 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1860 return VLC_EGENERIC;
1864 static int AVI_PacketRead( demux_t *p_demux,
1866 block_t **pp_frame )
1870 i_size = __EVEN( p_pk->i_size + 8 );
1872 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1874 return VLC_EGENERIC;
1876 (*pp_frame)->p_buffer += 8;
1877 (*pp_frame)->i_buffer -= 8;
1879 if( i_size != p_pk->i_size + 8 )
1881 (*pp_frame)->i_buffer--;
1887 static int AVI_PacketSearch( demux_t *p_demux )
1889 demux_sys_t *p_sys = p_demux->p_sys;
1890 avi_packet_t avi_pk;
1895 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1897 return VLC_EGENERIC;
1899 AVI_PacketGetHeader( p_demux, &avi_pk );
1900 if( avi_pk.i_stream < p_sys->i_track &&
1901 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1905 switch( avi_pk.i_fourcc )
1907 case AVIFOURCC_JUNK:
1908 case AVIFOURCC_LIST:
1909 case AVIFOURCC_RIFF:
1910 case AVIFOURCC_idx1:
1914 /* Prevents from eating all the CPU with broken files.
1915 * This value should be low enough so that it doesn't affect the
1916 * reading speed too much (not that we care much anyway because
1917 * this code is called only on broken files). */
1918 if( !(++i_count % 1024) )
1920 if( p_demux->b_die ) return VLC_EGENERIC;
1923 if( !(i_count % (1024 * 10)) )
1924 msg_Warn( p_demux, "trying to resync..." );
1929 /****************************************************************************
1931 ****************************************************************************/
1932 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1934 avi_entry_t *p_index)
1936 avi_track_t *tk = p_sys->track[i_stream];
1938 /* Update i_movi_lastchunk_pos */
1939 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1941 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1945 if( tk->i_idxnb >= tk->i_idxmax )
1947 tk->i_idxmax += 16384;
1948 tk->p_index = realloc( tk->p_index,
1949 tk->i_idxmax * sizeof( avi_entry_t ) );
1950 if( tk->p_index == NULL )
1955 /* calculate cumulate length */
1956 if( tk->i_idxnb > 0 )
1958 p_index->i_lengthtotal =
1959 tk->p_index[tk->i_idxnb - 1].i_length +
1960 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1964 p_index->i_lengthtotal = 0;
1967 tk->p_index[tk->i_idxnb++] = *p_index;
1970 static int AVI_IndexLoad_idx1( demux_t *p_demux )
1972 demux_sys_t *p_sys = p_demux->p_sys;
1974 avi_chunk_list_t *p_riff;
1975 avi_chunk_list_t *p_movi;
1976 avi_chunk_idx1_t *p_idx1;
1978 unsigned int i_stream;
1979 unsigned int i_index;
1983 vlc_bool_t b_keyset[100];
1985 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1986 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1987 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1991 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
1992 return VLC_EGENERIC;
1995 /* *** calculate offset *** */
1996 /* Well, avi is __SHIT__ so test more than one entry
1997 * (needed for some avi files) */
1999 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2001 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2003 i_offset = p_movi->i_chunk_pos + 8;
2008 /* Reset b_keyset */
2009 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2010 b_keyset[i_stream] = VLC_FALSE;
2012 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2016 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2019 if( i_stream < p_sys->i_track &&
2020 i_cat == p_sys->track[i_stream]->i_cat )
2023 index.i_id = p_idx1->entry[i_index].i_fourcc;
2025 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2026 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2027 index.i_length = p_idx1->entry[i_index].i_length;
2028 AVI_IndexAddEntry( p_sys, i_stream, &index );
2030 if( index.i_flags&AVIIF_KEYFRAME )
2031 b_keyset[i_stream] = VLC_TRUE;
2035 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2037 if( !b_keyset[i_stream] )
2039 avi_track_t *tk = p_sys->track[i_stream];
2041 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2042 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2043 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2049 static void __Parse_indx( demux_t *p_demux,
2051 avi_chunk_indx_t *p_indx )
2053 demux_sys_t *p_sys = p_demux->p_sys;
2057 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2058 if( p_indx->i_indexsubtype == 0 )
2060 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2062 index.i_id = p_indx->i_id;
2063 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2064 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2065 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2067 AVI_IndexAddEntry( p_sys, i_stream, &index );
2070 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2072 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2074 index.i_id = p_indx->i_id;
2075 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2076 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2077 index.i_length = p_indx->idx.field[i].i_size;
2079 AVI_IndexAddEntry( p_sys, i_stream, &index );
2084 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2088 static void AVI_IndexLoad_indx( demux_t *p_demux )
2090 demux_sys_t *p_sys = p_demux->p_sys;
2091 unsigned int i_stream;
2094 avi_chunk_list_t *p_riff;
2095 avi_chunk_list_t *p_hdrl;
2097 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2098 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2100 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2102 avi_chunk_list_t *p_strl;
2103 avi_chunk_indx_t *p_indx;
2105 #define p_stream p_sys->track[i_stream]
2106 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2107 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2111 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2115 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2117 __Parse_indx( p_demux, i_stream, p_indx );
2119 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2122 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2124 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2125 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2129 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2134 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2140 static void AVI_IndexLoad( demux_t *p_demux )
2142 demux_sys_t *p_sys = p_demux->p_sys;
2143 unsigned int i_stream;
2145 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2147 p_sys->track[i_stream]->i_idxnb = 0;
2148 p_sys->track[i_stream]->i_idxmax = 0;
2149 p_sys->track[i_stream]->p_index = NULL;
2154 AVI_IndexLoad_indx( p_demux );
2156 else if( AVI_IndexLoad_idx1( p_demux ) )
2158 /* try indx if idx1 failed as some "normal" file have indx too */
2159 AVI_IndexLoad_indx( p_demux );
2162 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2164 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2165 i_stream, p_sys->track[i_stream]->i_idxnb );
2169 static void AVI_IndexCreate( demux_t *p_demux )
2171 demux_sys_t *p_sys = p_demux->p_sys;
2173 avi_chunk_list_t *p_riff;
2174 avi_chunk_list_t *p_movi;
2176 unsigned int i_stream;
2179 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2180 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2184 msg_Err( p_demux, "cannot find p_movi" );
2188 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2190 p_sys->track[i_stream]->i_idxnb = 0;
2191 p_sys->track[i_stream]->i_idxmax = 0;
2192 p_sys->track[i_stream]->p_index = NULL;
2194 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2195 stream_Size( p_demux->s ) );
2197 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2198 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2203 if( p_demux->b_die )
2208 if( AVI_PacketGetHeader( p_demux, &pk ) )
2212 if( pk.i_stream < p_sys->i_track &&
2213 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2216 index.i_id = pk.i_fourcc;
2218 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2219 index.i_pos = pk.i_pos;
2220 index.i_length = pk.i_size;
2221 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2225 switch( pk.i_fourcc )
2227 case AVIFOURCC_idx1:
2230 avi_chunk_list_t *p_sysx;
2231 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2232 AVIFOURCC_RIFF, 1 );
2234 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2235 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2242 case AVIFOURCC_RIFF:
2243 msg_Dbg( p_demux, "new RIFF chunk found" );
2245 case AVIFOURCC_JUNK:
2248 msg_Warn( p_demux, "need resync, probably broken avi" );
2249 if( AVI_PacketSearch( p_demux ) )
2251 msg_Warn( p_demux, "lost sync, abord index creation" );
2257 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2258 AVI_PacketNext( p_demux ) )
2265 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2268 "stream[%d] creating %d index entries",
2270 p_sys->track[i_stream]->i_idxnb );
2274 /*****************************************************************************
2276 *****************************************************************************/
2277 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2279 demux_sys_t *p_sys = p_demux->p_sys;
2281 int b_end = VLC_TRUE;
2283 for( i = 0; i < p_sys->i_track; i++ )
2285 avi_track_t *tk = p_sys->track[i];
2286 if( tk->i_idxposc >= tk->i_idxnb )
2288 tk->b_activated = VLC_FALSE;
2289 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2299 /****************************************************************************
2300 * AVI_MovieGetLength give max streams length in second
2301 ****************************************************************************/
2302 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2304 demux_sys_t *p_sys = p_demux->p_sys;
2305 mtime_t i_maxlength = 0;
2308 for( i = 0; i < p_sys->i_track; i++ )
2310 avi_track_t *tk = p_sys->track[i];
2313 /* fix length for each stream */
2314 if( tk->i_idxnb < 1 || !tk->p_index )
2319 if( tk->i_samplesize )
2321 i_length = AVI_GetDPTS( tk,
2322 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2323 tk->p_index[tk->i_idxnb-1].i_length );
2327 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2329 i_length /= (mtime_t)1000000; /* in seconds */
2332 "stream[%d] length:"I64Fd" (based on index)",
2335 i_maxlength = __MAX( i_maxlength, i_length );