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 );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
56 add_bool( "avi-interleaved", 0, NULL,
57 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
58 add_bool( "avi-index", 0, NULL,
59 INDEX_TEXT, INDEX_LONGTEXT, VLC_TRUE );
61 set_callbacks( Open, Close );
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Control ( demux_t *, int, va_list );
68 static int Seek ( demux_t *, mtime_t, int );
69 static int Demux_Seekable ( demux_t * );
70 static int Demux_UnSeekable( demux_t * );
72 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
73 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
77 vlc_fourcc_t i_fourcc;
80 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
82 uint8_t i_peek[8]; /* first 8 bytes */
84 unsigned int i_stream;
95 uint32_t i_lengthtotal;
101 vlc_bool_t b_activated;
103 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
104 vlc_fourcc_t i_codec;
112 avi_entry_t *p_index;
113 unsigned int i_idxnb;
114 unsigned int i_idxmax;
116 unsigned int i_idxposc; /* numero of chunk */
117 unsigned int i_idxposb; /* byte in the current chunk */
119 /* For VBR audio only */
120 unsigned int i_blockno;
121 unsigned int i_blocksize;
129 vlc_bool_t b_seekable;
135 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
137 /* number of streams and information */
138 unsigned int i_track;
145 static inline off_t __EVEN( off_t i )
147 return (i & 1) ? i + 1 : i;
150 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
151 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
152 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
153 static mtime_t AVI_GetPTS ( avi_track_t * );
156 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
157 static int AVI_StreamChunkSet ( demux_t *,
158 unsigned int i_stream, unsigned int i_ck );
159 static int AVI_StreamBytesSet ( demux_t *,
160 unsigned int i_stream, off_t i_byte );
162 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
163 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
165 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
166 static int AVI_PacketNext ( demux_t * );
167 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
168 static int AVI_PacketSearch ( demux_t * );
170 static void AVI_IndexLoad ( demux_t * );
171 static void AVI_IndexCreate ( demux_t * );
172 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
174 static mtime_t AVI_MovieGetLength( demux_t * );
176 /*****************************************************************************
178 *****************************************************************************/
179 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
180 static int AVI_TrackStopFinishedStreams( demux_t *);
183 - For VBR mp3 stream:
184 count blocks by rounded-up chunksizes instead of chunks
185 we need full emulation of dshow avi demuxer bugs :(
186 fixes silly nandub-style a-v delaying in avi with vbr mp3...
187 (from mplayer 2002/08/02)
191 /*****************************************************************************
192 * Open: check file and initializes AVI structures
193 *****************************************************************************/
194 static int Open( vlc_object_t * p_this )
196 demux_t *p_demux = (demux_t *)p_this;
200 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
201 avi_chunk_list_t *p_hdrl, *p_movi;
202 avi_chunk_avih_t *p_avih;
204 unsigned int i_track;
205 unsigned int i, i_peeker;
209 /* Is it an avi file ? */
210 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
212 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
214 if( !strncmp( &p_peek[0], "RIFF", 4 ) &&
215 !strncmp( &p_peek[8], "AVI ", 4 ) ) break;
218 if( i_peeker == 188 )
223 /* Initialize input structures. */
224 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
225 memset( p_sys, 0, sizeof( demux_sys_t ) );
228 p_sys->i_movi_lastchunk_pos = 0;
229 p_sys->b_odml = VLC_FALSE;
234 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
236 p_demux->pf_control = Control;
237 p_demux->pf_demux = Demux_Seekable;
238 /* For unseekable stream, automaticaly use Demux_UnSeekable */
239 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
241 p_demux->pf_demux = Demux_UnSeekable;
246 stream_Read( p_demux->s, NULL, i_peeker );
249 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
251 msg_Err( p_demux, "avi module discarded (invalid file)" );
255 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
257 unsigned int i_count =
258 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
260 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
261 for( i = 1; i < i_count; i++ )
263 avi_chunk_list_t *p_sysx;
265 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
266 if( p_sysx->i_type == AVIFOURCC_AVIX )
268 msg_Warn( p_demux, "detected OpenDML file" );
269 p_sys->b_odml = VLC_TRUE;
275 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
276 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
277 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
279 if( !p_hdrl || !p_movi )
281 msg_Err( p_demux, "avi module discarded (invalid file)" );
285 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
287 msg_Err( p_demux, "cannot find avih chunk" );
290 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
291 if( p_avih->i_streams != i_track )
294 "found %d stream but %d are declared",
295 i_track, p_avih->i_streams );
299 msg_Err( p_demux, "no stream defined!" );
303 /* print information on streams */
304 msg_Dbg( p_demux, "AVIH: %d stream, flags %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 if( ( p_sys->meta = vlc_meta_New() ) )
313 sprintf( buffer, "%s%s%s%s",
314 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
315 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
316 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
317 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
318 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
321 /* now read info on each stream and create ES */
322 for( i = 0 ; i < i_track; i++ )
324 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
325 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
326 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
327 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
328 avi_chunk_strf_auds_t *p_auds;
329 avi_chunk_strf_vids_t *p_vids;
332 tk->b_activated = VLC_FALSE;
342 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
343 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
345 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
347 msg_Warn( p_demux, "stream[%d] incomplete", i );
351 tk->i_rate = p_strh->i_rate;
352 tk->i_scale = p_strh->i_scale;
353 tk->i_samplesize = p_strh->i_samplesize;
354 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
355 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
357 switch( p_strh->i_type )
359 case( AVIFOURCC_auds ):
360 tk->i_cat = AUDIO_ES;
361 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
362 p_auds->p_wf->wFormatTag );
363 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
365 if( p_auds->p_wf->wFormatTag == 1 )
367 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
374 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
376 fmt.audio.i_channels = p_auds->p_wf->nChannels;
377 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
378 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
379 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
380 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
381 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
382 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
383 fmt.p_extra = &p_auds->p_wf[1];
384 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
385 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
386 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
389 case( AVIFOURCC_vids ):
390 tk->i_cat = VIDEO_ES;
391 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
392 p_vids->p_bih->biCompression );
393 if( p_vids->p_bih->biCompression == 0x00 )
395 switch( p_vids->p_bih->biBitCount )
398 tk->i_codec = VLC_FOURCC('R','V','3','2');
401 tk->i_codec = VLC_FOURCC('R','V','2','4');
404 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
407 tk->i_codec = VLC_FOURCC('R','V','1','5');
410 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
413 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
415 if( p_vids->p_bih->biBitCount == 24 )
417 /* This is in BGR format */
418 fmt.video.i_bmask = 0x00ff0000;
419 fmt.video.i_gmask = 0x0000ff00;
420 fmt.video.i_rmask = 0x000000ff;
425 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
426 if( tk->i_codec == FOURCC_mp4v &&
427 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
429 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
432 tk->i_samplesize = 0;
433 fmt.video.i_width = p_vids->p_bih->biWidth;
434 fmt.video.i_height = p_vids->p_bih->biHeight;
435 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
437 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
438 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
439 fmt.p_extra = &p_vids->p_bih[1];
440 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
442 (char*)&p_vids->p_bih->biCompression,
443 p_vids->p_bih->biWidth,
444 p_vids->p_bih->biHeight,
445 p_vids->p_bih->biBitCount,
446 (float)tk->i_rate/(float)tk->i_scale );
448 if( p_vids->p_bih->biCompression == 0x00 )
450 /* RGB DIB are coded from bottom to top */
452 (unsigned int)(-(int)p_vids->p_bih->biHeight);
456 case( AVIFOURCC_txts):
458 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
459 msg_Dbg( p_demux, "stream[%d] subtitles", i );
460 es_format_Init( &fmt, SPU_ES, tk->i_codec );
463 case( AVIFOURCC_mids):
464 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
466 msg_Warn( p_demux, "stream[%d] unknown type", i );
472 fmt.psz_description = strdup( p_strn->p_str );
474 tk->p_es = es_out_Add( p_demux->out, &fmt );
475 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
478 if( p_sys->i_track <= 0 )
480 msg_Err( p_demux, "no valid track" );
484 if( config_GetInt( p_demux, "avi-index" ) )
486 if( p_sys->b_seekable )
488 AVI_IndexCreate( p_demux );
492 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
493 AVI_IndexLoad( p_demux );
498 AVI_IndexLoad( p_demux );
501 /* *** movie length in sec *** */
502 p_sys->i_length = AVI_MovieGetLength( p_demux );
503 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
504 (mtime_t)p_avih->i_microsecperframe /
507 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
509 /* fix some BeOS MediaKit generated file */
510 for( i = 0 ; i < p_sys->i_track; i++ )
512 avi_track_t *tk = p_sys->track[i];
513 avi_chunk_list_t *p_strl;
514 avi_chunk_strh_t *p_strh;
515 avi_chunk_strf_auds_t *p_auds;
517 if( tk->i_cat != AUDIO_ES )
521 if( tk->i_idxnb < 1 ||
523 tk->i_samplesize != 0 )
527 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
528 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
529 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
531 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
532 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
534 int64_t i_track_length =
535 tk->p_index[tk->i_idxnb-1].i_length +
536 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
537 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
538 (mtime_t)p_avih->i_microsecperframe;
542 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
545 tk->i_samplesize = 1;
546 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
547 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
551 if( p_sys->b_seekable )
553 /* we have read all chunk so go back to movi */
554 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
556 /* Skip movi header */
557 stream_Read( p_demux->s, NULL, 12 );
559 p_sys->i_movi_begin = p_movi->i_chunk_pos;
565 vlc_meta_Delete( p_sys->meta );
567 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
572 /*****************************************************************************
573 * Close: frees unused data
574 *****************************************************************************/
575 static void Close ( vlc_object_t * p_this )
577 demux_t * p_demux = (demux_t *)p_this;
579 demux_sys_t *p_sys = p_demux->p_sys ;
581 for( i = 0; i < p_sys->i_track; i++ )
583 if( p_sys->track[i] )
585 FREE( p_sys->track[i]->p_index );
586 free( p_sys->track[i] );
589 FREE( p_sys->track );
590 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
591 vlc_meta_Delete( p_sys->meta );
596 /*****************************************************************************
597 * Demux_Seekable: reads and demuxes data packets for stream seekable
598 *****************************************************************************
599 * AVIDemux: reads and demuxes data packets
600 *****************************************************************************
601 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
602 *****************************************************************************/
609 off_t i_posf; /* where we will read :
610 if i_idxposb == 0 : begining of chunk (+8 to acces data)
611 else : point on data directly */
612 } avi_track_toread_t;
614 static int Demux_Seekable( demux_t *p_demux )
616 demux_sys_t *p_sys = p_demux->p_sys;
618 unsigned int i_track_count = 0;
619 unsigned int i_track;
621 /* cannot be more than 100 stream (dcXX or wbXX) */
622 avi_track_toread_t toread[100];
625 /* detect new selected/unselected streams */
626 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
628 avi_track_t *tk = p_sys->track[i_track];
631 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
632 if( b && !tk->b_activated )
634 if( p_sys->b_seekable)
636 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
638 tk->b_activated = VLC_TRUE;
640 else if( !b && tk->b_activated )
642 tk->b_activated = VLC_FALSE;
650 if( i_track_count <= 0 )
652 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
654 p_sys->i_time += 25*1000; /* read 25ms */
657 if( p_sys->i_time >= i_length )
661 msg_Warn( p_demux, "no track selected, exiting..." );
665 /* wait for the good time */
666 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
667 p_sys->i_time += 25*1000; /* read 25ms */
670 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
672 avi_track_t *tk = p_sys->track[i_track];
675 toread[i_track].b_ok = tk->b_activated;
676 if( tk->i_idxposc < tk->i_idxnb )
678 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
679 if( tk->i_idxposb > 0 )
681 toread[i_track].i_posf += 8 + tk->i_idxposb;
686 toread[i_track].i_posf = -1;
689 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
691 if( tk->i_samplesize )
693 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
697 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
702 toread[i_track].i_toread *= -1;
706 b_stream = VLC_FALSE;
717 /* search for first chunk to be read */
718 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
720 if( !toread[i].b_ok ||
721 AVI_GetDPTS( p_sys->track[i],
722 toread[i].i_toread ) <= -25 * 1000 )
727 if( toread[i].i_toread > 0 )
729 b_done = VLC_FALSE; /* not yet finished */
731 if( toread[i].i_posf > 0 )
733 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
736 i_pos = toread[i].i_posf;
748 int i_loop_count = 0;
750 /* no valid index, we will parse directly the stream
751 * in case we fail we will disable all finished stream */
752 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
754 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
755 if( AVI_PacketNext( p_demux ) )
757 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
762 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
769 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
772 "cannot get packet header, track disabled" );
773 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
775 if( avi_pk.i_stream >= p_sys->i_track ||
776 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
778 if( AVI_PacketNext( p_demux ) )
781 "cannot skip packet, track disabled" );
782 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
785 /* Prevents from eating all the CPU with broken files.
786 * This value should be low enough so that it doesn't
787 * affect the reading speed too much. */
788 if( !(++i_loop_count % 1024) )
790 if( p_demux->b_die ) return -1;
793 if( !(i_loop_count % (1024 * 10)) )
795 "don't seem to find any data..." );
801 /* add this chunk to the index */
804 index.i_id = avi_pk.i_fourcc;
806 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
808 index.i_pos = avi_pk.i_pos;
809 index.i_length = avi_pk.i_size;
810 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
812 i_track = avi_pk.i_stream;
813 tk = p_sys->track[i_track];
814 /* do we will read this data ? */
815 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
821 if( AVI_PacketNext( p_demux ) )
824 "cannot skip packet, track disabled" );
825 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
834 stream_Seek( p_demux->s, i_pos );
837 /* Set the track to use */
838 tk = p_sys->track[i_track];
840 /* read thoses data */
841 if( tk->i_samplesize )
843 unsigned int i_toread;
845 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
847 if( tk->i_samplesize > 1 )
849 i_toread = tk->i_samplesize;
853 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
854 i_toread = __MAX( i_toread, 100 );
857 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
863 i_size = tk->p_index[tk->i_idxposc].i_length;
866 if( tk->i_idxposb == 0 )
868 i_size += 8; /* need to read and skip header */
871 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
873 msg_Warn( p_demux, "failled reading data" );
874 tk->b_activated = VLC_FALSE;
875 toread[i_track].b_ok = VLC_FALSE;
878 if( i_size % 2 ) /* read was padded on word boundary */
883 if( tk->i_idxposb == 0 )
885 p_frame->p_buffer += 8;
886 p_frame->i_buffer -= 8;
888 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
889 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
891 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
895 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
899 if( tk->i_samplesize )
901 if( tk->i_idxposb == 0 )
905 toread[i_track].i_toread -= i_size;
906 tk->i_idxposb += i_size;
908 tk->p_index[tk->i_idxposc].i_length )
916 int i_length = tk->p_index[tk->i_idxposc].i_length;
919 if( tk->i_cat == AUDIO_ES )
921 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
923 toread[i_track].i_toread--;
926 if( tk->i_idxposc < tk->i_idxnb)
928 toread[i_track].i_posf =
929 tk->p_index[tk->i_idxposc].i_pos;
930 if( tk->i_idxposb > 0 )
932 toread[i_track].i_posf += 8 + tk->i_idxposb;
938 toread[i_track].i_posf = -1;
941 b_stream = VLC_TRUE; /* at least one read succeed */
943 if( tk->i_cat != VIDEO_ES )
944 p_frame->i_dts = p_frame->i_pts;
947 p_frame->i_dts = p_frame->i_pts;
951 //p_pes->i_rate = p_demux->stream.control.i_rate;
952 es_out_Send( p_demux->out, tk->p_es, p_frame );
957 /*****************************************************************************
958 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
959 *****************************************************************************
960 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
961 *****************************************************************************/
962 static int Demux_UnSeekable( demux_t *p_demux )
964 demux_sys_t *p_sys = p_demux->p_sys;
965 avi_track_t *p_stream_master = NULL;
966 unsigned int i_stream;
967 unsigned int i_packet;
969 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
971 /* *** find master stream for data packet skipping algo *** */
972 /* *** -> first video, if any, or first audio ES *** */
973 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
975 avi_track_t *tk = p_sys->track[i_stream];
978 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
980 if( b && tk->i_cat == VIDEO_ES )
982 p_stream_master = tk;
986 p_stream_master = tk;
990 if( !p_stream_master )
992 msg_Warn( p_demux, "no more stream selected" );
996 p_sys->i_time = AVI_GetPTS( p_stream_master );
998 for( i_packet = 0; i_packet < 10; i_packet++)
1000 #define p_stream p_sys->track[avi_pk.i_stream]
1002 avi_packet_t avi_pk;
1004 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1009 if( avi_pk.i_stream >= p_sys->i_track ||
1010 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1012 /* we haven't found an audio or video packet:
1013 * - we have seek, found first next packet
1014 * - others packets could be found, skip them
1016 switch( avi_pk.i_fourcc )
1018 case AVIFOURCC_JUNK:
1019 case AVIFOURCC_LIST:
1020 case AVIFOURCC_RIFF:
1021 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1022 case AVIFOURCC_idx1:
1025 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1027 return( 0 ); /* eof */
1030 "seems to have lost position, resync" );
1031 if( AVI_PacketSearch( p_demux ) )
1033 msg_Err( p_demux, "resync failed" );
1040 /* check for time */
1041 if( __ABS( AVI_GetPTS( p_stream ) -
1042 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1044 /* load it and send to decoder */
1046 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1050 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1052 if( avi_pk.i_cat != VIDEO_ES )
1053 p_frame->i_dts = p_frame->i_pts;
1056 p_frame->i_dts = p_frame->i_pts;
1060 //p_pes->i_rate = p_demux->stream.control.i_rate;
1061 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1065 if( AVI_PacketNext( p_demux ) )
1071 /* *** update stream time position *** */
1072 if( p_stream->i_samplesize )
1074 p_stream->i_idxposb += avi_pk.i_size;
1078 if( p_stream->i_cat == AUDIO_ES )
1080 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1082 p_stream->i_idxposc++;
1092 /*****************************************************************************
1093 * Seek: goto to i_date or i_percent
1094 *****************************************************************************/
1095 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1098 demux_sys_t *p_sys = p_demux->p_sys;
1099 unsigned int i_stream;
1100 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1101 i_date / 1000000, i_percent );
1103 if( p_sys->b_seekable )
1105 if( !p_sys->i_length )
1107 avi_track_t *p_stream;
1110 /* use i_percent to create a true i_date */
1111 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1112 " work only for interleaved file", i_percent );
1113 if( i_percent >= 100 )
1115 msg_Warn( p_demux, "cannot seek so far !" );
1116 return VLC_EGENERIC;
1118 i_percent = __MAX( i_percent, 0 );
1120 /* try to find chunk that is at i_percent or the file */
1121 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1122 p_sys->i_movi_begin );
1123 /* search first selected stream */
1124 for( i_stream = 0, p_stream = NULL;
1125 i_stream < p_sys->i_track; i_stream++ )
1127 p_stream = p_sys->track[i_stream];
1128 if( p_stream->b_activated )
1133 if( !p_stream || !p_stream->b_activated )
1135 msg_Warn( p_demux, "cannot find any selected stream" );
1136 return VLC_EGENERIC;
1139 /* be sure that the index exist */
1140 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1142 msg_Warn( p_demux, "cannot seek" );
1143 return VLC_EGENERIC;
1146 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1147 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1149 /* search after i_idxposc */
1150 if( AVI_StreamChunkSet( p_demux,
1151 i_stream, p_stream->i_idxposc + 1 ) )
1153 msg_Warn( p_demux, "cannot seek" );
1154 return VLC_EGENERIC;
1158 i_date = AVI_GetPTS( p_stream );
1159 /* TODO better support for i_samplesize != 0 */
1160 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1163 #define p_stream p_sys->track[i_stream]
1165 /* seek for chunk based streams */
1166 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1168 if( p_stream->b_activated && !p_stream->i_samplesize )
1169 /* if( p_stream->b_activated ) */
1171 AVI_TrackSeek( p_demux, i_stream, i_date );
1172 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1179 i_date = p_sys->i_time;
1181 /* seek for bytes based streams */
1182 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1184 if( p_stream->b_activated && p_stream->i_samplesize )
1186 AVI_TrackSeek( p_demux, i_stream, i_date );
1187 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1190 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1191 /* set true movie time */
1193 if( !p_sys->i_time )
1195 p_sys->i_time = i_date;
1202 msg_Err( p_demux, "shouldn't yet be executed" );
1203 return VLC_EGENERIC;
1207 /*****************************************************************************
1209 *****************************************************************************
1211 *****************************************************************************/
1212 static double ControlGetPosition( demux_t *p_demux )
1214 demux_sys_t *p_sys = p_demux->p_sys;
1216 if( p_sys->i_length > 0 )
1218 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1220 else if( stream_Size( p_demux->s ) > 0 )
1226 /* search the more advanced selected es */
1227 for( i = 0; i < p_sys->i_track; i++ )
1229 avi_track_t *tk = p_sys->track[i];
1230 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1232 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1233 tk->p_index[tk->i_idxposc].i_length + 8;
1240 return (double)i64 / (double)stream_Size( p_demux->s );
1245 static int Control( demux_t *p_demux, int i_query, va_list args )
1247 demux_sys_t *p_sys = p_demux->p_sys;
1251 vlc_meta_t **pp_meta;
1255 case DEMUX_GET_POSITION:
1256 pf = (double*)va_arg( args, double * );
1257 *pf = ControlGetPosition( p_demux );
1259 case DEMUX_SET_POSITION:
1260 f = (double)va_arg( args, double );
1261 if( p_sys->b_seekable )
1263 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1264 return Seek( p_demux, i64, (int)(f * 100) );
1268 int64_t i_pos = stream_Size( p_demux->s ) * f;
1269 return stream_Seek( p_demux->s, i_pos );
1272 case DEMUX_GET_TIME:
1273 pi64 = (int64_t*)va_arg( args, int64_t * );
1274 *pi64 = p_sys->i_time;
1277 case DEMUX_SET_TIME:
1281 i64 = (int64_t)va_arg( args, int64_t );
1282 if( p_sys->i_length > 0 )
1284 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1286 else if( p_sys->i_time > 0 )
1288 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1289 (double)i64 / (double)p_sys->i_time );
1291 return Seek( p_demux, i64, i_percent );
1293 case DEMUX_GET_LENGTH:
1294 pi64 = (int64_t*)va_arg( args, int64_t * );
1295 *pi64 = p_sys->i_length * (mtime_t)1000000;
1299 pf = (double*)va_arg( args, double * );
1301 for( i = 0; i < (int)p_sys->i_track; i++ )
1303 avi_track_t *tk = p_sys->track[i];
1304 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1306 *pf = (float)tk->i_rate / (float)tk->i_scale;
1311 case DEMUX_GET_META:
1312 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1313 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1317 return VLC_EGENERIC;
1321 /*****************************************************************************
1322 * Function to convert pts to chunk or byte
1323 *****************************************************************************/
1325 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1327 return (mtime_t)((int64_t)i_pts *
1328 (int64_t)tk->i_rate /
1329 (int64_t)tk->i_scale /
1332 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1334 return (mtime_t)((int64_t)i_pts *
1335 (int64_t)tk->i_rate /
1336 (int64_t)tk->i_scale /
1338 (int64_t)tk->i_samplesize );
1341 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1345 i_dpts = (mtime_t)( (int64_t)1000000 *
1347 (int64_t)tk->i_scale /
1348 (int64_t)tk->i_rate );
1350 if( tk->i_samplesize )
1352 return i_dpts / tk->i_samplesize;
1357 static mtime_t AVI_GetPTS( avi_track_t *tk )
1359 if( tk->i_samplesize )
1361 int64_t i_count = 0;
1363 /* we need a valid entry we will emulate one */
1364 if( tk->i_idxposc == tk->i_idxnb )
1368 /* use the last entry */
1369 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1370 + tk->p_index[tk->i_idxnb - 1].i_length;
1375 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1377 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1381 if( tk->i_cat == AUDIO_ES )
1383 return AVI_GetDPTS( tk, tk->i_blockno );
1387 return AVI_GetDPTS( tk, tk->i_idxposc );
1392 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1394 demux_sys_t *p_sys = p_demux->p_sys;
1395 avi_packet_t avi_pk;
1396 int i_loop_count = 0;
1398 /* find first chunk of i_stream that isn't in index */
1400 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1402 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1403 if( AVI_PacketNext( p_demux ) )
1405 return VLC_EGENERIC;
1410 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1415 if( p_demux->b_die ) return VLC_EGENERIC;
1417 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1419 msg_Warn( p_demux, "cannot get packet header" );
1420 return VLC_EGENERIC;
1422 if( avi_pk.i_stream >= p_sys->i_track ||
1423 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1425 if( AVI_PacketNext( p_demux ) )
1427 return VLC_EGENERIC;
1430 /* Prevents from eating all the CPU with broken files.
1431 * This value should be low enough so that it doesn't
1432 * affect the reading speed too much. */
1433 if( !(++i_loop_count % 1024) )
1435 if( p_demux->b_die ) return VLC_EGENERIC;
1438 if( !(i_loop_count % (1024 * 10)) )
1439 msg_Warn( p_demux, "don't seem to find any data..." );
1444 /* add this chunk to the index */
1447 index.i_id = avi_pk.i_fourcc;
1449 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1451 index.i_pos = avi_pk.i_pos;
1452 index.i_length = avi_pk.i_size;
1453 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1455 if( avi_pk.i_stream == i_stream )
1460 if( AVI_PacketNext( p_demux ) )
1462 return VLC_EGENERIC;
1469 /* be sure that i_ck will be a valid index entry */
1470 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1473 demux_sys_t *p_sys = p_demux->p_sys;
1474 avi_track_t *p_stream = p_sys->track[i_stream];
1476 p_stream->i_idxposc = i_ck;
1477 p_stream->i_idxposb = 0;
1479 if( i_ck >= p_stream->i_idxnb )
1481 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1484 p_stream->i_idxposc++;
1485 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1487 return VLC_EGENERIC;
1490 } while( p_stream->i_idxposc < i_ck );
1497 /* XXX FIXME up to now, we assume that all chunk are one after one */
1498 static int AVI_StreamBytesSet( demux_t *p_demux,
1499 unsigned int i_stream,
1502 demux_sys_t *p_sys = p_demux->p_sys;
1503 avi_track_t *p_stream = p_sys->track[i_stream];
1505 if( ( p_stream->i_idxnb > 0 )
1506 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1507 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1509 /* index is valid to find the ck */
1510 /* uses dichototmie to be fast enougth */
1511 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1512 int i_idxmax = p_stream->i_idxnb;
1516 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1518 i_idxmax = i_idxposc ;
1519 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1523 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1524 p_stream->p_index[i_idxposc].i_length <= i_byte)
1526 i_idxmin = i_idxposc ;
1527 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1531 p_stream->i_idxposc = i_idxposc;
1532 p_stream->i_idxposb = i_byte -
1533 p_stream->p_index[i_idxposc].i_lengthtotal;
1542 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1543 p_stream->i_idxposb = 0;
1546 p_stream->i_idxposc++;
1547 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1549 return VLC_EGENERIC;
1552 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1553 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1555 p_stream->i_idxposb = i_byte -
1556 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1561 static int AVI_TrackSeek( demux_t *p_demux,
1565 demux_sys_t *p_sys = p_demux->p_sys;
1566 avi_track_t *tk = p_sys->track[i_stream];
1568 #define p_stream p_sys->track[i_stream]
1571 i_oldpts = AVI_GetPTS( p_stream );
1573 if( !p_stream->i_samplesize )
1575 if( AVI_StreamChunkSet( p_demux,
1577 AVI_PTSToChunk( p_stream, i_date ) ) )
1579 return VLC_EGENERIC;
1582 if( p_stream->i_cat == AUDIO_ES )
1586 for( i = 0; i < tk->i_idxposc; i++ )
1588 if( tk->i_blocksize > 0 )
1590 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1600 "old:"I64Fd" %s new "I64Fd,
1602 i_oldpts > i_date ? ">" : "<",
1605 if( p_stream->i_cat == VIDEO_ES )
1607 /* search key frame */
1608 if( i_date < i_oldpts )
1610 while( p_stream->i_idxposc > 0 &&
1611 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1614 if( AVI_StreamChunkSet( p_demux,
1616 p_stream->i_idxposc - 1 ) )
1618 return VLC_EGENERIC;
1624 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1625 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1628 if( AVI_StreamChunkSet( p_demux,
1630 p_stream->i_idxposc + 1 ) )
1632 return VLC_EGENERIC;
1640 if( AVI_StreamBytesSet( p_demux,
1642 AVI_PTSToByte( p_stream, i_date ) ) )
1644 return VLC_EGENERIC;
1651 /****************************************************************************
1652 * Return VLC_TRUE if it's a key frame
1653 ****************************************************************************/
1654 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1660 * startcode: 0x00000100 32bits
1661 * framenumber ? 5bits
1662 * piture type 0(I),1(P) 2bits
1664 if( GetDWBE( p_byte ) != 0x00000100 )
1666 /* it's not an msmpegv1 stream, strange...*/
1667 return AVIIF_KEYFRAME;
1669 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1672 case FOURCC_DIV3: /* wmv1 also */
1674 * picture type 0(I),1(P) 2bits
1676 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1678 /* we should find first occurrence of 0x000001b6 (32bits)
1679 * startcode: 0x000001b6 32bits
1680 * piture type 0(I),1(P) 2bits
1682 if( GetDWBE( p_byte ) != 0x000001b6 )
1684 /* not true , need to find the first VOP header */
1685 return AVIIF_KEYFRAME;
1687 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1690 /* I can't do it, so say yes */
1691 return AVIIF_KEYFRAME;
1695 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1700 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1704 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1708 return VLC_FOURCC('m','r','l','e');
1757 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1761 /****************************************************************************
1763 ****************************************************************************/
1764 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1765 int *pi_number, int *pi_type )
1767 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1770 c1 = ((uint8_t *)&i_id)[0];
1771 c2 = ((uint8_t *)&i_id)[1];
1773 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1775 SET_PTR( pi_number, 100 ); /* > max stream number */
1776 SET_PTR( pi_type, UNKNOWN_ES );
1780 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1781 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1784 SET_PTR( pi_type, AUDIO_ES );
1788 SET_PTR( pi_type, VIDEO_ES );
1791 SET_PTR( pi_type, UNKNOWN_ES );
1798 /****************************************************************************
1800 ****************************************************************************/
1801 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1805 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1807 return VLC_EGENERIC;
1809 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1810 p_pk->i_size = GetDWLE( p_peek + 4 );
1811 p_pk->i_pos = stream_Tell( p_demux->s );
1812 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1814 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1815 p_peek[10], p_peek[11] );
1822 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1824 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1828 static int AVI_PacketNext( demux_t *p_demux )
1830 avi_packet_t avi_ck;
1833 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1835 return VLC_EGENERIC;
1838 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1839 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1843 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1844 avi_ck.i_type == AVIFOURCC_AVIX )
1850 i_skip = __EVEN( avi_ck.i_size ) + 8;
1853 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1855 return VLC_EGENERIC;
1859 static int AVI_PacketRead( demux_t *p_demux,
1861 block_t **pp_frame )
1865 i_size = __EVEN( p_pk->i_size + 8 );
1867 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1869 return VLC_EGENERIC;
1871 (*pp_frame)->p_buffer += 8;
1872 (*pp_frame)->i_buffer -= 8;
1874 if( i_size != p_pk->i_size + 8 )
1876 (*pp_frame)->i_buffer--;
1882 static int AVI_PacketSearch( demux_t *p_demux )
1884 demux_sys_t *p_sys = p_demux->p_sys;
1885 avi_packet_t avi_pk;
1890 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1892 return VLC_EGENERIC;
1894 AVI_PacketGetHeader( p_demux, &avi_pk );
1895 if( avi_pk.i_stream < p_sys->i_track &&
1896 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1900 switch( avi_pk.i_fourcc )
1902 case AVIFOURCC_JUNK:
1903 case AVIFOURCC_LIST:
1904 case AVIFOURCC_RIFF:
1905 case AVIFOURCC_idx1:
1909 /* Prevents from eating all the CPU with broken files.
1910 * This value should be low enough so that it doesn't affect the
1911 * reading speed too much (not that we care much anyway because
1912 * this code is called only on broken files). */
1913 if( !(++i_count % 1024) )
1915 if( p_demux->b_die ) return VLC_EGENERIC;
1918 if( !(i_count % (1024 * 10)) )
1919 msg_Warn( p_demux, "trying to resync..." );
1924 /****************************************************************************
1926 ****************************************************************************/
1927 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1929 avi_entry_t *p_index)
1931 avi_track_t *tk = p_sys->track[i_stream];
1933 /* Update i_movi_lastchunk_pos */
1934 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1936 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1940 if( tk->i_idxnb >= tk->i_idxmax )
1942 tk->i_idxmax += 16384;
1943 tk->p_index = realloc( tk->p_index,
1944 tk->i_idxmax * sizeof( avi_entry_t ) );
1945 if( tk->p_index == NULL )
1950 /* calculate cumulate length */
1951 if( tk->i_idxnb > 0 )
1953 p_index->i_lengthtotal =
1954 tk->p_index[tk->i_idxnb - 1].i_length +
1955 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1959 p_index->i_lengthtotal = 0;
1962 tk->p_index[tk->i_idxnb++] = *p_index;
1965 static int AVI_IndexLoad_idx1( demux_t *p_demux )
1967 demux_sys_t *p_sys = p_demux->p_sys;
1969 avi_chunk_list_t *p_riff;
1970 avi_chunk_list_t *p_movi;
1971 avi_chunk_idx1_t *p_idx1;
1973 unsigned int i_stream;
1974 unsigned int i_index;
1978 vlc_bool_t b_keyset[100];
1980 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1981 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1982 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1986 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
1987 return VLC_EGENERIC;
1990 /* *** calculate offset *** */
1991 /* Well, avi is __SHIT__ so test more than one entry
1992 * (needed for some avi files) */
1994 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1996 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1998 i_offset = p_movi->i_chunk_pos + 8;
2003 /* Reset b_keyset */
2004 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2005 b_keyset[i_stream] = VLC_FALSE;
2007 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2011 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2014 if( i_stream < p_sys->i_track &&
2015 i_cat == p_sys->track[i_stream]->i_cat )
2018 index.i_id = p_idx1->entry[i_index].i_fourcc;
2020 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2021 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2022 index.i_length = p_idx1->entry[i_index].i_length;
2023 AVI_IndexAddEntry( p_sys, i_stream, &index );
2025 if( index.i_flags&AVIIF_KEYFRAME )
2026 b_keyset[i_stream] = VLC_TRUE;
2030 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2032 if( !b_keyset[i_stream] )
2034 avi_track_t *tk = p_sys->track[i_stream];
2036 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2037 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2038 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2044 static void __Parse_indx( demux_t *p_demux,
2046 avi_chunk_indx_t *p_indx )
2048 demux_sys_t *p_sys = p_demux->p_sys;
2052 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2053 if( p_indx->i_indexsubtype == 0 )
2055 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2057 index.i_id = p_indx->i_id;
2058 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2059 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2060 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2062 AVI_IndexAddEntry( p_sys, i_stream, &index );
2065 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2067 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2069 index.i_id = p_indx->i_id;
2070 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2071 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2072 index.i_length = p_indx->idx.field[i].i_size;
2074 AVI_IndexAddEntry( p_sys, i_stream, &index );
2079 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2083 static void AVI_IndexLoad_indx( demux_t *p_demux )
2085 demux_sys_t *p_sys = p_demux->p_sys;
2086 unsigned int i_stream;
2089 avi_chunk_list_t *p_riff;
2090 avi_chunk_list_t *p_hdrl;
2092 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2093 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2095 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2097 avi_chunk_list_t *p_strl;
2098 avi_chunk_indx_t *p_indx;
2100 #define p_stream p_sys->track[i_stream]
2101 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2102 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2106 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2110 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2112 __Parse_indx( p_demux, i_stream, p_indx );
2114 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2117 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2119 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2120 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2124 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2129 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2135 static void AVI_IndexLoad( demux_t *p_demux )
2137 demux_sys_t *p_sys = p_demux->p_sys;
2138 unsigned int i_stream;
2140 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2142 p_sys->track[i_stream]->i_idxnb = 0;
2143 p_sys->track[i_stream]->i_idxmax = 0;
2144 p_sys->track[i_stream]->p_index = NULL;
2149 AVI_IndexLoad_indx( p_demux );
2151 else if( AVI_IndexLoad_idx1( p_demux ) )
2153 /* try indx if idx1 failed as some "normal" file have indx too */
2154 AVI_IndexLoad_indx( p_demux );
2157 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2159 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2160 i_stream, p_sys->track[i_stream]->i_idxnb );
2164 static void AVI_IndexCreate( demux_t *p_demux )
2166 demux_sys_t *p_sys = p_demux->p_sys;
2168 avi_chunk_list_t *p_riff;
2169 avi_chunk_list_t *p_movi;
2171 unsigned int i_stream;
2174 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2175 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2179 msg_Err( p_demux, "cannot find p_movi" );
2183 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2185 p_sys->track[i_stream]->i_idxnb = 0;
2186 p_sys->track[i_stream]->i_idxmax = 0;
2187 p_sys->track[i_stream]->p_index = NULL;
2189 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2190 stream_Size( p_demux->s ) );
2192 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2193 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2198 if( p_demux->b_die )
2203 if( AVI_PacketGetHeader( p_demux, &pk ) )
2207 if( pk.i_stream < p_sys->i_track &&
2208 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2211 index.i_id = pk.i_fourcc;
2213 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2214 index.i_pos = pk.i_pos;
2215 index.i_length = pk.i_size;
2216 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2220 switch( pk.i_fourcc )
2222 case AVIFOURCC_idx1:
2225 avi_chunk_list_t *p_sysx;
2226 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2227 AVIFOURCC_RIFF, 1 );
2229 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2230 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2237 case AVIFOURCC_RIFF:
2238 msg_Dbg( p_demux, "new RIFF chunk found" );
2240 case AVIFOURCC_JUNK:
2243 msg_Warn( p_demux, "need resync, probably broken avi" );
2244 if( AVI_PacketSearch( p_demux ) )
2246 msg_Warn( p_demux, "lost sync, abord index creation" );
2252 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2253 AVI_PacketNext( p_demux ) )
2260 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2263 "stream[%d] creating %d index entries",
2265 p_sys->track[i_stream]->i_idxnb );
2269 /*****************************************************************************
2271 *****************************************************************************/
2272 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2274 demux_sys_t *p_sys = p_demux->p_sys;
2276 int b_end = VLC_TRUE;
2278 for( i = 0; i < p_sys->i_track; i++ )
2280 avi_track_t *tk = p_sys->track[i];
2281 if( tk->i_idxposc >= tk->i_idxnb )
2283 tk->b_activated = VLC_FALSE;
2284 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2294 /****************************************************************************
2295 * AVI_MovieGetLength give max streams length in second
2296 ****************************************************************************/
2297 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2299 demux_sys_t *p_sys = p_demux->p_sys;
2300 mtime_t i_maxlength = 0;
2303 for( i = 0; i < p_sys->i_track; i++ )
2305 avi_track_t *tk = p_sys->track[i];
2308 /* fix length for each stream */
2309 if( tk->i_idxnb < 1 || !tk->p_index )
2314 if( tk->i_samplesize )
2316 i_length = AVI_GetDPTS( tk,
2317 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2318 tk->p_index[tk->i_idxnb-1].i_length );
2322 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2324 i_length /= (mtime_t)1000000; /* in seconds */
2327 "stream[%d] length:"I64Fd" (based on index)",
2330 i_maxlength = __MAX( i_maxlength, i_length );