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;
208 /* Is it an avi file ? */
209 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
211 msg_Err( p_demux, "cannot peek()" );
214 if( strncmp( &p_peek[0], "RIFF", 4 ) || strncmp( &p_peek[8], "AVI ", 4 ) )
216 msg_Warn( p_demux, "avi module discarded (invalid header)" );
220 /* Initialize input structures. */
221 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
222 memset( p_sys, 0, sizeof( demux_sys_t ) );
225 p_sys->i_movi_lastchunk_pos = 0;
226 p_sys->b_odml = VLC_FALSE;
231 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
233 p_demux->pf_control = Control;
234 p_demux->pf_demux = Demux_Seekable;
235 /* For unseekable stream, automaticaly use Demux_UnSeekable */
236 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
238 p_demux->pf_demux = Demux_UnSeekable;
241 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
243 msg_Err( p_demux, "avi module discarded (invalid file)" );
247 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
249 unsigned int i_count =
250 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
252 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
253 for( i = 1; i < i_count; i++ )
255 avi_chunk_list_t *p_sysx;
257 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
258 if( p_sysx->i_type == AVIFOURCC_AVIX )
260 msg_Warn( p_demux, "detected OpenDML file" );
261 p_sys->b_odml = VLC_TRUE;
267 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
268 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
269 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
271 if( !p_hdrl || !p_movi )
273 msg_Err( p_demux, "avi module discarded (invalid file)" );
277 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
279 msg_Err( p_demux, "cannot find avih chunk" );
282 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
283 if( p_avih->i_streams != i_track )
286 "found %d stream but %d are declared",
287 i_track, p_avih->i_streams );
291 msg_Err( p_demux, "no stream defined!" );
295 /* print informations on streams */
296 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
298 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
299 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
300 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
301 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
302 if( ( p_sys->meta = vlc_meta_New() ) )
305 sprintf( buffer, "%s%s%s%s",
306 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
307 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
308 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
309 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
310 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
313 /* now read info on each stream and create ES */
314 for( i = 0 ; i < i_track; i++ )
316 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
317 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
318 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
319 avi_chunk_strf_auds_t *p_auds;
320 avi_chunk_strf_vids_t *p_vids;
323 tk->b_activated = VLC_FALSE;
333 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
334 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
336 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
338 msg_Warn( p_demux, "stream[%d] incomplete", i );
342 tk->i_rate = p_strh->i_rate;
343 tk->i_scale = p_strh->i_scale;
344 tk->i_samplesize = p_strh->i_samplesize;
345 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
346 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
348 switch( p_strh->i_type )
350 case( AVIFOURCC_auds ):
351 tk->i_cat = AUDIO_ES;
352 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
353 p_auds->p_wf->wFormatTag );
354 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
356 if( p_auds->p_wf->wFormatTag == 1 )
358 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
365 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
367 fmt.audio.i_channels = p_auds->p_wf->nChannels;
368 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
369 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
370 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
371 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
372 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
373 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
374 fmt.p_extra = &p_auds->p_wf[1];
375 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
376 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
377 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
380 case( AVIFOURCC_vids ):
381 tk->i_cat = VIDEO_ES;
382 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
383 p_vids->p_bih->biCompression );
384 if( p_vids->p_bih->biCompression == 0x00 )
386 switch( p_vids->p_bih->biBitCount )
389 tk->i_codec = VLC_FOURCC('R','V','3','2');
392 tk->i_codec = VLC_FOURCC('R','V','2','4');
395 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
398 tk->i_codec = VLC_FOURCC('R','V','1','5');
401 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
404 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
408 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
409 if( tk->i_codec == FOURCC_mp4v && !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
411 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
414 tk->i_samplesize = 0;
415 fmt.video.i_width = p_vids->p_bih->biWidth;
416 fmt.video.i_height = p_vids->p_bih->biHeight;
417 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
419 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
420 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
421 fmt.p_extra = &p_vids->p_bih[1];
422 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
424 (char*)&p_vids->p_bih->biCompression,
425 p_vids->p_bih->biWidth,
426 p_vids->p_bih->biHeight,
427 p_vids->p_bih->biBitCount,
428 (float)tk->i_rate/(float)tk->i_scale );
431 msg_Warn( p_demux, "stream[%d] unknown type", i );
435 tk->p_es = es_out_Add( p_demux->out, &fmt );
436 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
439 if( p_sys->i_track <= 0 )
441 msg_Err( p_demux, "no valid track" );
445 if( config_GetInt( p_demux, "avi-index" ) )
447 if( p_sys->b_seekable )
449 AVI_IndexCreate( p_demux );
453 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
454 AVI_IndexLoad( p_demux );
459 AVI_IndexLoad( p_demux );
462 /* *** movie length in sec *** */
463 p_sys->i_length = AVI_MovieGetLength( p_demux );
464 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
465 (mtime_t)p_avih->i_microsecperframe /
468 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
470 /* fix some BeOS MediaKit generated file */
471 for( i = 0 ; i < p_sys->i_track; i++ )
473 avi_track_t *tk = p_sys->track[i];
474 avi_chunk_list_t *p_strl;
475 avi_chunk_strh_t *p_strh;
476 avi_chunk_strf_auds_t *p_auds;
478 if( tk->i_cat != AUDIO_ES )
482 if( tk->i_idxnb < 1 ||
484 tk->i_samplesize != 0 )
488 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
489 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
490 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
492 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
493 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
495 int64_t i_track_length =
496 tk->p_index[tk->i_idxnb-1].i_length +
497 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
498 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
499 (mtime_t)p_avih->i_microsecperframe;
503 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
506 tk->i_samplesize = 1;
507 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
508 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
512 if( p_sys->b_seekable )
514 /* we have read all chunk so go back to movi */
515 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
517 /* Skip movi header */
518 stream_Read( p_demux->s, NULL, 12 );
520 p_sys->i_movi_begin = p_movi->i_chunk_pos;
526 vlc_meta_Delete( p_sys->meta );
528 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
533 /*****************************************************************************
534 * Close: frees unused data
535 *****************************************************************************/
536 static void Close ( vlc_object_t * p_this )
538 demux_t * p_demux = (demux_t *)p_this;
540 demux_sys_t *p_sys = p_demux->p_sys ;
542 for( i = 0; i < p_sys->i_track; i++ )
544 if( p_sys->track[i] )
546 FREE( p_sys->track[i]->p_index );
547 free( p_sys->track[i] );
550 FREE( p_sys->track );
551 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
552 vlc_meta_Delete( p_sys->meta );
557 /*****************************************************************************
558 * Demux_Seekable: reads and demuxes data packets for stream seekable
559 *****************************************************************************
560 * AVIDemux: reads and demuxes data packets
561 *****************************************************************************
562 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
563 *****************************************************************************/
570 off_t i_posf; /* where we will read :
571 if i_idxposb == 0 : begining of chunk (+8 to acces data)
572 else : point on data directly */
573 } avi_track_toread_t;
575 static int Demux_Seekable( demux_t *p_demux )
577 demux_sys_t *p_sys = p_demux->p_sys;
579 unsigned int i_track_count = 0;
580 unsigned int i_track;
582 /* cannot be more than 100 stream (dcXX or wbXX) */
583 avi_track_toread_t toread[100];
586 /* detect new selected/unselected streams */
587 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
589 avi_track_t *tk = p_sys->track[i_track];
592 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
593 if( b && !tk->b_activated )
595 if( p_sys->b_seekable)
597 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
599 tk->b_activated = VLC_TRUE;
601 else if( !b && tk->b_activated )
603 tk->b_activated = VLC_FALSE;
611 if( i_track_count <= 0 )
613 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
615 p_sys->i_time += 25*1000; /* read 25ms */
618 if( p_sys->i_time >= i_length )
622 msg_Warn( p_demux, "no track selected, exiting..." );
626 /* wait for the good time */
627 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
628 p_sys->i_time += 25*1000; /* read 25ms */
631 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
633 avi_track_t *tk = p_sys->track[i_track];
636 toread[i_track].b_ok = tk->b_activated;
637 if( tk->i_idxposc < tk->i_idxnb )
639 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
640 if( tk->i_idxposb > 0 )
642 toread[i_track].i_posf += 8 + tk->i_idxposb;
647 toread[i_track].i_posf = -1;
650 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
652 if( tk->i_samplesize )
654 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
658 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
663 toread[i_track].i_toread *= -1;
667 b_stream = VLC_FALSE;
678 /* search for first chunk to be read */
679 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
681 if( !toread[i].b_ok ||
682 AVI_GetDPTS( p_sys->track[i],
683 toread[i].i_toread ) <= -25 * 1000 )
688 if( toread[i].i_toread > 0 )
690 b_done = VLC_FALSE; /* not yet finished */
692 if( toread[i].i_posf > 0 )
694 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
697 i_pos = toread[i].i_posf;
709 int i_loop_count = 0;
711 /* no valid index, we will parse directly the stream
712 * in case we fail we will disable all finished stream */
713 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
715 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
716 if( AVI_PacketNext( p_demux ) )
718 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
723 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
730 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
733 "cannot get packet header, track disabled" );
734 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
736 if( avi_pk.i_stream >= p_sys->i_track ||
737 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
739 if( AVI_PacketNext( p_demux ) )
742 "cannot skip packet, track disabled" );
743 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
746 /* Prevents from eating all the CPU with broken files.
747 * This value should be low enough so that it doesn't
748 * affect the reading speed too much. */
749 if( !(++i_loop_count % 1024) )
751 if( p_demux->b_die ) return -1;
754 if( !(i_loop_count % (1024 * 10)) )
756 "don't seem to find any data..." );
762 /* add this chunk to the index */
765 index.i_id = avi_pk.i_fourcc;
767 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
769 index.i_pos = avi_pk.i_pos;
770 index.i_length = avi_pk.i_size;
771 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
773 i_track = avi_pk.i_stream;
774 tk = p_sys->track[i_track];
775 /* do we will read this data ? */
776 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
782 if( AVI_PacketNext( p_demux ) )
785 "cannot skip packet, track disabled" );
786 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
795 stream_Seek( p_demux->s, i_pos );
798 /* Set the track to use */
799 tk = p_sys->track[i_track];
801 /* read thoses data */
802 if( tk->i_samplesize )
804 unsigned int i_toread;
806 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
808 if( tk->i_samplesize > 1 )
810 i_toread = tk->i_samplesize;
814 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
817 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
823 i_size = tk->p_index[tk->i_idxposc].i_length;
826 if( tk->i_idxposb == 0 )
828 i_size += 8; /* need to read and skip header */
831 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
833 msg_Warn( p_demux, "failled reading data" );
834 tk->b_activated = VLC_FALSE;
835 toread[i_track].b_ok = VLC_FALSE;
838 if( i_size % 2 ) /* read was padded on word boundary */
843 if( tk->i_idxposb == 0 )
845 p_frame->p_buffer += 8;
846 p_frame->i_buffer -= 8;
848 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
849 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
851 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
855 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
859 if( tk->i_samplesize )
861 if( tk->i_idxposb == 0 )
865 toread[i_track].i_toread -= i_size;
866 tk->i_idxposb += i_size;
868 tk->p_index[tk->i_idxposc].i_length )
876 int i_length = tk->p_index[tk->i_idxposc].i_length;
879 if( tk->i_cat == AUDIO_ES )
881 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
883 toread[i_track].i_toread--;
886 if( tk->i_idxposc < tk->i_idxnb)
888 toread[i_track].i_posf =
889 tk->p_index[tk->i_idxposc].i_pos;
890 if( tk->i_idxposb > 0 )
892 toread[i_track].i_posf += 8 + tk->i_idxposb;
898 toread[i_track].i_posf = -1;
901 b_stream = VLC_TRUE; /* at least one read succeed */
903 if( tk->i_cat != VIDEO_ES )
904 p_frame->i_dts = p_frame->i_pts;
907 p_frame->i_dts = p_frame->i_pts;
911 //p_pes->i_rate = p_demux->stream.control.i_rate;
912 es_out_Send( p_demux->out, tk->p_es, p_frame );
917 /*****************************************************************************
918 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
919 *****************************************************************************
920 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
921 *****************************************************************************/
922 static int Demux_UnSeekable( demux_t *p_demux )
924 demux_sys_t *p_sys = p_demux->p_sys;
925 avi_track_t *p_stream_master = NULL;
926 unsigned int i_stream;
927 unsigned int i_packet;
929 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
931 /* *** find master stream for data packet skipping algo *** */
932 /* *** -> first video, if any, or first audio ES *** */
933 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
935 avi_track_t *tk = p_sys->track[i_stream];
938 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
940 if( b && tk->i_cat == VIDEO_ES )
942 p_stream_master = tk;
946 p_stream_master = tk;
950 if( !p_stream_master )
952 msg_Warn( p_demux, "no more stream selected" );
956 p_sys->i_time = AVI_GetPTS( p_stream_master );
958 for( i_packet = 0; i_packet < 10; i_packet++)
960 #define p_stream p_sys->track[avi_pk.i_stream]
964 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
969 if( avi_pk.i_stream >= p_sys->i_track ||
970 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
972 /* we haven't found an audio or video packet:
973 * - we have seek, found first next packet
974 * - others packets could be found, skip them
976 switch( avi_pk.i_fourcc )
981 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
985 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
987 return( 0 ); /* eof */
990 "seems to have lost position, resync" );
991 if( AVI_PacketSearch( p_demux ) )
993 msg_Err( p_demux, "resync failed" );
1000 /* check for time */
1001 if( __ABS( AVI_GetPTS( p_stream ) -
1002 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1004 /* load it and send to decoder */
1006 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1010 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1012 if( avi_pk.i_cat != VIDEO_ES )
1013 p_frame->i_dts = p_frame->i_pts;
1016 p_frame->i_dts = p_frame->i_pts;
1020 //p_pes->i_rate = p_demux->stream.control.i_rate;
1021 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1025 if( AVI_PacketNext( p_demux ) )
1031 /* *** update stream time position *** */
1032 if( p_stream->i_samplesize )
1034 p_stream->i_idxposb += avi_pk.i_size;
1038 if( p_stream->i_cat == AUDIO_ES )
1040 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1042 p_stream->i_idxposc++;
1052 /*****************************************************************************
1053 * Seek: goto to i_date or i_percent
1054 *****************************************************************************/
1055 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1058 demux_sys_t *p_sys = p_demux->p_sys;
1059 unsigned int i_stream;
1060 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1061 i_date / 1000000, i_percent );
1063 if( p_sys->b_seekable )
1065 if( !p_sys->i_length )
1067 avi_track_t *p_stream;
1070 /* use i_percent to create a true i_date */
1071 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1072 " work only for interleaved file", i_percent );
1073 if( i_percent >= 100 )
1075 msg_Warn( p_demux, "cannot seek so far !" );
1076 return VLC_EGENERIC;
1078 i_percent = __MAX( i_percent, 0 );
1080 /* try to find chunk that is at i_percent or the file */
1081 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1082 p_sys->i_movi_begin );
1083 /* search first selected stream */
1084 for( i_stream = 0, p_stream = NULL;
1085 i_stream < p_sys->i_track; i_stream++ )
1087 p_stream = p_sys->track[i_stream];
1088 if( p_stream->b_activated )
1093 if( !p_stream || !p_stream->b_activated )
1095 msg_Warn( p_demux, "cannot find any selected stream" );
1096 return VLC_EGENERIC;
1099 /* be sure that the index exist */
1100 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1102 msg_Warn( p_demux, "cannot seek" );
1103 return VLC_EGENERIC;
1106 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1107 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1109 /* search after i_idxposc */
1110 if( AVI_StreamChunkSet( p_demux,
1111 i_stream, p_stream->i_idxposc + 1 ) )
1113 msg_Warn( p_demux, "cannot seek" );
1114 return VLC_EGENERIC;
1118 i_date = AVI_GetPTS( p_stream );
1119 /* TODO better support for i_samplesize != 0 */
1120 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1123 #define p_stream p_sys->track[i_stream]
1125 /* seek for chunk based streams */
1126 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1128 if( p_stream->b_activated && !p_stream->i_samplesize )
1129 /* if( p_stream->b_activated ) */
1131 AVI_TrackSeek( p_demux, i_stream, i_date );
1132 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1139 i_date = p_sys->i_time;
1141 /* seek for bytes based streams */
1142 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1144 if( p_stream->b_activated && p_stream->i_samplesize )
1146 AVI_TrackSeek( p_demux, i_stream, i_date );
1147 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1150 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1151 /* set true movie time */
1153 if( !p_sys->i_time )
1155 p_sys->i_time = i_date;
1162 msg_Err( p_demux, "shouldn't yet be executed" );
1163 return VLC_EGENERIC;
1167 /*****************************************************************************
1169 *****************************************************************************
1171 *****************************************************************************/
1172 static double ControlGetPosition( demux_t *p_demux )
1174 demux_sys_t *p_sys = p_demux->p_sys;
1176 if( p_sys->i_length > 0 )
1178 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1180 else if( stream_Size( p_demux->s ) > 0 )
1186 /* search the more advanced selected es */
1187 for( i = 0; i < p_sys->i_track; i++ )
1189 avi_track_t *tk = p_sys->track[i];
1190 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1192 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1193 tk->p_index[tk->i_idxposc].i_length + 8;
1200 return (double)i64 / (double)stream_Size( p_demux->s );
1205 static int Control( demux_t *p_demux, int i_query, va_list args )
1207 demux_sys_t *p_sys = p_demux->p_sys;
1211 vlc_meta_t **pp_meta;
1215 case DEMUX_GET_POSITION:
1216 pf = (double*)va_arg( args, double * );
1217 *pf = ControlGetPosition( p_demux );
1219 case DEMUX_SET_POSITION:
1220 f = (double)va_arg( args, double );
1221 if( p_sys->b_seekable )
1223 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1224 return Seek( p_demux, i64, (int)(f * 100) );
1228 int64_t i_pos = stream_Size( p_demux->s ) * f;
1229 return stream_Seek( p_demux->s, i_pos );
1232 case DEMUX_GET_TIME:
1233 pi64 = (int64_t*)va_arg( args, int64_t * );
1234 *pi64 = p_sys->i_time;
1237 case DEMUX_SET_TIME:
1241 i64 = (int64_t)va_arg( args, int64_t );
1242 if( p_sys->i_length > 0 )
1244 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1246 else if( p_sys->i_time > 0 )
1248 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1249 (double)i64 / (double)p_sys->i_time );
1251 return Seek( p_demux, i64, i_percent );
1253 case DEMUX_GET_LENGTH:
1254 pi64 = (int64_t*)va_arg( args, int64_t * );
1255 *pi64 = p_sys->i_length * (mtime_t)1000000;
1259 pf = (double*)va_arg( args, double * );
1261 for( i = 0; i < (int)p_sys->i_track; i++ )
1263 avi_track_t *tk = p_sys->track[i];
1264 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1266 *pf = (float)tk->i_rate / (float)tk->i_scale;
1271 case DEMUX_GET_META:
1272 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1273 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1277 return VLC_EGENERIC;
1281 /*****************************************************************************
1282 * Function to convert pts to chunk or byte
1283 *****************************************************************************/
1285 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1287 return (mtime_t)((int64_t)i_pts *
1288 (int64_t)tk->i_rate /
1289 (int64_t)tk->i_scale /
1292 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1294 return (mtime_t)((int64_t)i_pts *
1295 (int64_t)tk->i_rate /
1296 (int64_t)tk->i_scale /
1298 (int64_t)tk->i_samplesize );
1301 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1305 i_dpts = (mtime_t)( (int64_t)1000000 *
1307 (int64_t)tk->i_scale /
1308 (int64_t)tk->i_rate );
1310 if( tk->i_samplesize )
1312 return i_dpts / tk->i_samplesize;
1317 static mtime_t AVI_GetPTS( avi_track_t *tk )
1319 if( tk->i_samplesize )
1321 int64_t i_count = 0;
1323 /* we need a valid entry we will emulate one */
1324 if( tk->i_idxposc == tk->i_idxnb )
1328 /* use the last entry */
1329 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1330 + tk->p_index[tk->i_idxnb - 1].i_length;
1335 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1337 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1341 if( tk->i_cat == AUDIO_ES )
1343 return AVI_GetDPTS( tk, tk->i_blockno );
1347 return AVI_GetDPTS( tk, tk->i_idxposc );
1352 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1354 demux_sys_t *p_sys = p_demux->p_sys;
1355 avi_packet_t avi_pk;
1356 int i_loop_count = 0;
1358 /* find first chunk of i_stream that isn't in index */
1360 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1362 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1363 if( AVI_PacketNext( p_demux ) )
1365 return VLC_EGENERIC;
1370 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1375 if( p_demux->b_die ) return VLC_EGENERIC;
1377 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1379 msg_Warn( p_demux, "cannot get packet header" );
1380 return VLC_EGENERIC;
1382 if( avi_pk.i_stream >= p_sys->i_track ||
1383 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1385 if( AVI_PacketNext( p_demux ) )
1387 return VLC_EGENERIC;
1390 /* Prevents from eating all the CPU with broken files.
1391 * This value should be low enough so that it doesn't
1392 * affect the reading speed too much. */
1393 if( !(++i_loop_count % 1024) )
1395 if( p_demux->b_die ) return VLC_EGENERIC;
1398 if( !(i_loop_count % (1024 * 10)) )
1399 msg_Warn( p_demux, "don't seem to find any data..." );
1404 /* add this chunk to the index */
1407 index.i_id = avi_pk.i_fourcc;
1409 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1411 index.i_pos = avi_pk.i_pos;
1412 index.i_length = avi_pk.i_size;
1413 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1415 if( avi_pk.i_stream == i_stream )
1420 if( AVI_PacketNext( p_demux ) )
1422 return VLC_EGENERIC;
1429 /* be sure that i_ck will be a valid index entry */
1430 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1433 demux_sys_t *p_sys = p_demux->p_sys;
1434 avi_track_t *p_stream = p_sys->track[i_stream];
1436 p_stream->i_idxposc = i_ck;
1437 p_stream->i_idxposb = 0;
1439 if( i_ck >= p_stream->i_idxnb )
1441 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1444 p_stream->i_idxposc++;
1445 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1447 return VLC_EGENERIC;
1450 } while( p_stream->i_idxposc < i_ck );
1457 /* XXX FIXME up to now, we assume that all chunk are one after one */
1458 static int AVI_StreamBytesSet( demux_t *p_demux,
1459 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 if( ( p_stream->i_idxnb > 0 )
1466 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1467 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1469 /* index is valid to find the ck */
1470 /* uses dichototmie to be fast enougth */
1471 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1472 int i_idxmax = p_stream->i_idxnb;
1476 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1478 i_idxmax = i_idxposc ;
1479 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1483 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1484 p_stream->p_index[i_idxposc].i_length <= i_byte)
1486 i_idxmin = i_idxposc ;
1487 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1491 p_stream->i_idxposc = i_idxposc;
1492 p_stream->i_idxposb = i_byte -
1493 p_stream->p_index[i_idxposc].i_lengthtotal;
1502 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1503 p_stream->i_idxposb = 0;
1506 p_stream->i_idxposc++;
1507 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1509 return VLC_EGENERIC;
1512 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1513 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1515 p_stream->i_idxposb = i_byte -
1516 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1521 static int AVI_TrackSeek( demux_t *p_demux,
1525 demux_sys_t *p_sys = p_demux->p_sys;
1526 avi_track_t *tk = p_sys->track[i_stream];
1528 #define p_stream p_sys->track[i_stream]
1531 i_oldpts = AVI_GetPTS( p_stream );
1533 if( !p_stream->i_samplesize )
1535 if( AVI_StreamChunkSet( p_demux,
1537 AVI_PTSToChunk( p_stream, i_date ) ) )
1539 return VLC_EGENERIC;
1542 if( p_stream->i_cat == AUDIO_ES )
1546 for( i = 0; i < tk->i_idxposc; i++ )
1548 if( tk->i_blocksize > 0 )
1550 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1560 "old:"I64Fd" %s new "I64Fd,
1562 i_oldpts > i_date ? ">" : "<",
1565 if( p_stream->i_cat == VIDEO_ES )
1567 /* search key frame */
1568 if( i_date < i_oldpts )
1570 while( p_stream->i_idxposc > 0 &&
1571 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1574 if( AVI_StreamChunkSet( p_demux,
1576 p_stream->i_idxposc - 1 ) )
1578 return VLC_EGENERIC;
1584 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1585 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1588 if( AVI_StreamChunkSet( p_demux,
1590 p_stream->i_idxposc + 1 ) )
1592 return VLC_EGENERIC;
1600 if( AVI_StreamBytesSet( p_demux,
1602 AVI_PTSToByte( p_stream, i_date ) ) )
1604 return VLC_EGENERIC;
1611 /****************************************************************************
1612 * Return VLC_TRUE if it's a key frame
1613 ****************************************************************************/
1614 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1620 * startcode: 0x00000100 32bits
1621 * framenumber ? 5bits
1622 * piture type 0(I),1(P) 2bits
1624 if( GetDWBE( p_byte ) != 0x00000100 )
1626 /* it's not an msmpegv1 stream, strange...*/
1627 return AVIIF_KEYFRAME;
1629 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1632 case FOURCC_DIV3: /* wmv1 also */
1634 * picture type 0(I),1(P) 2bits
1636 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1638 /* we should find first occurence of 0x000001b6 (32bits)
1639 * startcode: 0x000001b6 32bits
1640 * piture type 0(I),1(P) 2bits
1642 if( GetDWBE( p_byte ) != 0x000001b6 )
1644 /* not true , need to find the first VOP header */
1645 return AVIIF_KEYFRAME;
1647 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1650 /* I can't do it, so say yes */
1651 return AVIIF_KEYFRAME;
1655 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1660 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1664 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1668 return VLC_FOURCC('m','r','l','e');
1717 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1721 /****************************************************************************
1723 ****************************************************************************/
1724 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1725 int *pi_number, int *pi_type )
1727 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1730 c1 = ((uint8_t *)&i_id)[0];
1731 c2 = ((uint8_t *)&i_id)[1];
1733 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1735 SET_PTR( pi_number, 100 ); /* > max stream number */
1736 SET_PTR( pi_type, UNKNOWN_ES );
1740 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1741 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1744 SET_PTR( pi_type, AUDIO_ES );
1748 SET_PTR( pi_type, VIDEO_ES );
1751 SET_PTR( pi_type, UNKNOWN_ES );
1758 /****************************************************************************
1760 ****************************************************************************/
1761 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1765 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1767 return VLC_EGENERIC;
1769 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1770 p_pk->i_size = GetDWLE( p_peek + 4 );
1771 p_pk->i_pos = stream_Tell( p_demux->s );
1772 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1774 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1775 p_peek[10], p_peek[11] );
1782 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1784 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1788 static int AVI_PacketNext( demux_t *p_demux )
1790 avi_packet_t avi_ck;
1793 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1795 return VLC_EGENERIC;
1798 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1799 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1803 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1804 avi_ck.i_type == AVIFOURCC_AVIX )
1810 i_skip = __EVEN( avi_ck.i_size ) + 8;
1813 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1815 return VLC_EGENERIC;
1819 static int AVI_PacketRead( demux_t *p_demux,
1821 block_t **pp_frame )
1825 i_size = __EVEN( p_pk->i_size + 8 );
1827 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1829 return VLC_EGENERIC;
1831 (*pp_frame)->p_buffer += 8;
1832 (*pp_frame)->i_buffer -= 8;
1834 if( i_size != p_pk->i_size + 8 )
1836 (*pp_frame)->i_buffer--;
1842 static int AVI_PacketSearch( demux_t *p_demux )
1844 demux_sys_t *p_sys = p_demux->p_sys;
1845 avi_packet_t avi_pk;
1850 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1852 return VLC_EGENERIC;
1854 AVI_PacketGetHeader( p_demux, &avi_pk );
1855 if( avi_pk.i_stream < p_sys->i_track &&
1856 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1860 switch( avi_pk.i_fourcc )
1862 case AVIFOURCC_JUNK:
1863 case AVIFOURCC_LIST:
1864 case AVIFOURCC_RIFF:
1865 case AVIFOURCC_idx1:
1869 /* Prevents from eating all the CPU with broken files.
1870 * This value should be low enough so that it doesn't affect the
1871 * reading speed too much (not that we care much anyway because
1872 * this code is called only on broken files). */
1873 if( !(++i_count % 1024) )
1875 if( p_demux->b_die ) return VLC_EGENERIC;
1878 if( !(i_count % (1024 * 10)) )
1879 msg_Warn( p_demux, "trying to resync..." );
1884 /****************************************************************************
1886 ****************************************************************************/
1887 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1889 avi_entry_t *p_index)
1891 avi_track_t *tk = p_sys->track[i_stream];
1893 /* Update i_movi_lastchunk_pos */
1894 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1896 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1900 if( tk->i_idxnb >= tk->i_idxmax )
1902 tk->i_idxmax += 16384;
1903 tk->p_index = realloc( tk->p_index,
1904 tk->i_idxmax * sizeof( avi_entry_t ) );
1905 if( tk->p_index == NULL )
1910 /* calculate cumulate length */
1911 if( tk->i_idxnb > 0 )
1913 p_index->i_lengthtotal =
1914 tk->p_index[tk->i_idxnb - 1].i_length +
1915 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1919 p_index->i_lengthtotal = 0;
1922 tk->p_index[tk->i_idxnb++] = *p_index;
1925 static int AVI_IndexLoad_idx1( demux_t *p_demux )
1927 demux_sys_t *p_sys = p_demux->p_sys;
1929 avi_chunk_list_t *p_riff;
1930 avi_chunk_list_t *p_movi;
1931 avi_chunk_idx1_t *p_idx1;
1933 unsigned int i_stream;
1934 unsigned int i_index;
1938 vlc_bool_t b_keyset[100];
1940 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1941 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1942 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1946 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
1947 return VLC_EGENERIC;
1950 /* *** calculate offset *** */
1951 /* Well, avi is __SHIT__ so test more than one entry
1952 * (needed for some avi files) */
1954 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1956 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1958 i_offset = p_movi->i_chunk_pos + 8;
1963 /* Reset b_keyset */
1964 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1965 b_keyset[i_stream] = VLC_FALSE;
1967 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
1971 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
1974 if( i_stream < p_sys->i_track &&
1975 i_cat == p_sys->track[i_stream]->i_cat )
1978 index.i_id = p_idx1->entry[i_index].i_fourcc;
1980 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
1981 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
1982 index.i_length = p_idx1->entry[i_index].i_length;
1983 AVI_IndexAddEntry( p_sys, i_stream, &index );
1985 if( index.i_flags&AVIIF_KEYFRAME )
1986 b_keyset[i_stream] = VLC_TRUE;
1990 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1992 if( !b_keyset[i_stream] )
1994 avi_track_t *tk = p_sys->track[i_stream];
1996 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
1997 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
1998 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2004 static void __Parse_indx( demux_t *p_demux,
2006 avi_chunk_indx_t *p_indx )
2008 demux_sys_t *p_sys = p_demux->p_sys;
2012 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2013 if( p_indx->i_indexsubtype == 0 )
2015 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2017 index.i_id = p_indx->i_id;
2018 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2019 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2020 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2022 AVI_IndexAddEntry( p_sys, i_stream, &index );
2025 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2027 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2029 index.i_id = p_indx->i_id;
2030 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2031 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2032 index.i_length = p_indx->idx.field[i].i_size;
2034 AVI_IndexAddEntry( p_sys, i_stream, &index );
2039 msg_Warn( p_demux, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
2043 static void AVI_IndexLoad_indx( demux_t *p_demux )
2045 demux_sys_t *p_sys = p_demux->p_sys;
2046 unsigned int i_stream;
2049 avi_chunk_list_t *p_riff;
2050 avi_chunk_list_t *p_hdrl;
2052 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2053 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2055 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2057 avi_chunk_list_t *p_strl;
2058 avi_chunk_indx_t *p_indx;
2060 #define p_stream p_sys->track[i_stream]
2061 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2062 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2066 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2070 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2072 __Parse_indx( p_demux, i_stream, p_indx );
2074 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2076 avi_chunk_indx_t ck_sub;
2077 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2079 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2080 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2084 __Parse_indx( p_demux, i_stream, &ck_sub );
2089 msg_Warn( p_demux, "unknow type index(0x%x)", p_indx->i_indextype );
2095 static void AVI_IndexLoad( demux_t *p_demux )
2097 demux_sys_t *p_sys = p_demux->p_sys;
2098 unsigned int i_stream;
2100 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2102 p_sys->track[i_stream]->i_idxnb = 0;
2103 p_sys->track[i_stream]->i_idxmax = 0;
2104 p_sys->track[i_stream]->p_index = NULL;
2109 AVI_IndexLoad_indx( p_demux );
2111 else if( AVI_IndexLoad_idx1( p_demux ) )
2113 /* try indx if idx1 failed as some "normal" file have indx too */
2114 AVI_IndexLoad_indx( p_demux );
2117 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2119 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2120 i_stream, p_sys->track[i_stream]->i_idxnb );
2124 static void AVI_IndexCreate( demux_t *p_demux )
2126 demux_sys_t *p_sys = p_demux->p_sys;
2128 avi_chunk_list_t *p_riff;
2129 avi_chunk_list_t *p_movi;
2131 unsigned int i_stream;
2134 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2135 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2139 msg_Err( p_demux, "cannot find p_movi" );
2143 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2145 p_sys->track[i_stream]->i_idxnb = 0;
2146 p_sys->track[i_stream]->i_idxmax = 0;
2147 p_sys->track[i_stream]->p_index = NULL;
2149 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2150 stream_Size( p_demux->s ) );
2152 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2153 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2158 if( p_demux->b_die )
2163 if( AVI_PacketGetHeader( p_demux, &pk ) )
2167 if( pk.i_stream < p_sys->i_track &&
2168 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2171 index.i_id = pk.i_fourcc;
2173 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2174 index.i_pos = pk.i_pos;
2175 index.i_length = pk.i_size;
2176 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2180 switch( pk.i_fourcc )
2182 case AVIFOURCC_idx1:
2185 avi_chunk_list_t *p_sysx;
2186 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2187 AVIFOURCC_RIFF, 1 );
2189 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2190 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2197 case AVIFOURCC_RIFF:
2198 msg_Dbg( p_demux, "new RIFF chunk found" );
2200 case AVIFOURCC_JUNK:
2203 msg_Warn( p_demux, "need resync, probably broken avi" );
2204 if( AVI_PacketSearch( p_demux ) )
2206 msg_Warn( p_demux, "lost sync, abord index creation" );
2212 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2213 AVI_PacketNext( p_demux ) )
2220 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2223 "stream[%d] creating %d index entries",
2225 p_sys->track[i_stream]->i_idxnb );
2229 /*****************************************************************************
2231 *****************************************************************************/
2232 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2234 demux_sys_t *p_sys = p_demux->p_sys;
2236 int b_end = VLC_TRUE;
2238 for( i = 0; i < p_sys->i_track; i++ )
2240 avi_track_t *tk = p_sys->track[i];
2241 if( tk->i_idxposc >= tk->i_idxnb )
2243 tk->b_activated = VLC_FALSE;
2244 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2254 /****************************************************************************
2255 * AVI_MovieGetLength give max streams length in second
2256 ****************************************************************************/
2257 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2259 demux_sys_t *p_sys = p_demux->p_sys;
2260 mtime_t i_maxlength = 0;
2263 for( i = 0; i < p_sys->i_track; i++ )
2265 avi_track_t *tk = p_sys->track[i];
2268 /* fix length for each stream */
2269 if( tk->i_idxnb < 1 || !tk->p_index )
2274 if( tk->i_samplesize )
2276 i_length = AVI_GetDPTS( tk,
2277 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2278 tk->p_index[tk->i_idxnb-1].i_length );
2282 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2284 i_length /= (mtime_t)1000000; /* in seconds */
2287 "stream[%d] length:"I64Fd" (based on index)",
2290 i_maxlength = __MAX( i_maxlength, i_length );