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( "demux", 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 ( input_thread_t *, int, va_list );
66 static int Seek ( input_thread_t *, mtime_t, int );
67 static int Demux_Seekable ( input_thread_t * );
68 static int Demux_UnSeekable( input_thread_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;
128 vlc_bool_t b_seekable;
134 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
136 /* number of streams and information */
137 unsigned int i_track;
144 static inline off_t __EVEN( off_t i )
146 return (i & 1) ? i + 1 : i;
149 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
150 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
151 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
152 static mtime_t AVI_GetPTS ( avi_track_t * );
155 static int AVI_StreamChunkFind( input_thread_t *, unsigned int i_stream );
156 static int AVI_StreamChunkSet ( input_thread_t *,
157 unsigned int i_stream, unsigned int i_ck );
158 static int AVI_StreamBytesSet ( input_thread_t *,
159 unsigned int i_stream, off_t i_byte );
161 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
162 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
164 static int AVI_PacketGetHeader( input_thread_t *, avi_packet_t *p_pk );
165 static int AVI_PacketNext ( input_thread_t * );
166 static int AVI_PacketRead ( input_thread_t *, avi_packet_t *, block_t **);
167 static int AVI_PacketSearch ( input_thread_t * );
169 static void AVI_IndexLoad ( input_thread_t * );
170 static void AVI_IndexCreate ( input_thread_t * );
171 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
173 static mtime_t AVI_MovieGetLength( input_thread_t * );
175 /*****************************************************************************
177 *****************************************************************************/
178 static int AVI_TrackSeek ( input_thread_t *, int, mtime_t );
179 static int AVI_TrackStopFinishedStreams( input_thread_t *);
182 - For VBR mp3 stream:
183 count blocks by rounded-up chunksizes instead of chunks
184 we need full emulation of dshow avi demuxer bugs :(
185 fixes silly nandub-style a-v delaying in avi with vbr mp3...
186 (from mplayer 2002/08/02)
190 /*****************************************************************************
191 * Open: check file and initializes AVI structures
192 *****************************************************************************/
193 static int Open( vlc_object_t * p_this )
195 input_thread_t *p_input = (input_thread_t *)p_this;
199 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
200 avi_chunk_list_t *p_hdrl, *p_movi;
201 avi_chunk_avih_t *p_avih;
203 unsigned int i_track;
209 /* Is it an avi file ? */
210 if( stream_Peek( p_input->s, &p_peek, 12 ) < 12 )
212 msg_Err( p_input, "cannot peek()" );
215 if( strncmp( &p_peek[0], "RIFF", 4 ) || strncmp( &p_peek[8], "AVI ", 4 ) )
217 msg_Warn( p_input, "avi module discarded (invalid header)" );
221 /* Initialize input structures. */
222 p_sys = p_input->p_demux_data = malloc( sizeof(demux_sys_t) );
223 memset( p_sys, 0, sizeof( demux_sys_t ) );
227 p_sys->i_movi_lastchunk_pos = 0;
228 p_sys->b_odml = VLC_FALSE;
233 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
235 p_input->pf_demux_control = Control;
236 p_input->pf_demux = Demux_Seekable;
237 /* For unseekable stream, automaticaly use Demux_UnSeekable */
238 if( !p_sys->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
240 p_input->pf_demux = Demux_UnSeekable;
243 if( AVI_ChunkReadRoot( p_input->s, &p_sys->ck_root ) )
245 msg_Err( p_input, "avi module discarded (invalid file)" );
249 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
251 unsigned int i_count =
252 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
254 msg_Warn( p_input, "multiple riff -> OpenDML ?" );
255 for( i = 1; i < i_count; i++ )
257 avi_chunk_list_t *p_sysx;
259 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
260 if( p_sysx->i_type == AVIFOURCC_AVIX )
262 msg_Warn( p_input, "detected OpenDML file" );
263 p_sys->b_odml = VLC_TRUE;
269 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
270 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
271 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
273 if( !p_hdrl || !p_movi )
275 msg_Err( p_input, "avi module discarded (invalid file)" );
279 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
281 msg_Err( p_input, "cannot find avih chunk" );
284 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
285 if( p_avih->i_streams != i_track )
288 "found %d stream but %d are declared",
289 i_track, p_avih->i_streams );
293 msg_Err( p_input, "no stream defined!" );
297 /* create one program */
298 vlc_mutex_lock( &p_input->stream.stream_lock );
299 if( input_InitStream( p_input, 0 ) == -1)
301 vlc_mutex_unlock( &p_input->stream.stream_lock );
302 msg_Err( p_input, "cannot init stream" );
305 p_input->stream.i_mux_rate = 0; /* Fixed later */
306 vlc_mutex_unlock( &p_input->stream.stream_lock );
308 /* print informations on streams */
309 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
311 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
312 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
313 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
314 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
315 if( ( p_sys->meta = vlc_meta_New() ) )
318 sprintf( buffer, "%s%s%s%s",
319 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
320 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
321 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
322 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
323 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
326 /* now read info on each stream and create ES */
327 for( i = 0 ; i < i_track; i++ )
329 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
330 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
331 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
332 avi_chunk_strf_auds_t *p_auds;
333 avi_chunk_strf_vids_t *p_vids;
336 tk->b_activated = VLC_FALSE;
346 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
347 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
349 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
351 msg_Warn( p_input, "stream[%d] incomplete", i );
355 tk->i_rate = p_strh->i_rate;
356 tk->i_scale = p_strh->i_scale;
357 tk->i_samplesize = p_strh->i_samplesize;
358 msg_Dbg( p_input, "stream[%d] rate:%d scale:%d samplesize:%d",
359 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
361 switch( p_strh->i_type )
363 case( AVIFOURCC_auds ):
364 tk->i_cat = AUDIO_ES;
365 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
366 p_auds->p_wf->wFormatTag );
367 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
369 if( p_auds->p_wf->wFormatTag == 1 )
371 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
378 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
380 fmt.audio.i_channels = p_auds->p_wf->nChannels;
381 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
382 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
383 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
384 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
385 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
386 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
387 fmt.p_extra = &p_auds->p_wf[1];
388 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
389 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
390 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
393 case( AVIFOURCC_vids ):
394 tk->i_cat = VIDEO_ES;
395 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
396 p_vids->p_bih->biCompression );
397 if( p_vids->p_bih->biCompression == 0x00 )
399 switch( p_vids->p_bih->biBitCount )
402 tk->i_codec = VLC_FOURCC('R','V','3','2');
405 tk->i_codec = VLC_FOURCC('R','V','2','4');
408 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
411 tk->i_codec = VLC_FOURCC('R','V','1','5');
414 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
417 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
421 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
423 tk->i_samplesize = 0;
424 fmt.video.i_width = p_vids->p_bih->biWidth;
425 fmt.video.i_height = p_vids->p_bih->biHeight;
426 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
428 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
429 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
430 fmt.p_extra = &p_vids->p_bih[1];
431 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
433 (char*)&p_vids->p_bih->biCompression,
434 p_vids->p_bih->biWidth,
435 p_vids->p_bih->biHeight,
436 p_vids->p_bih->biBitCount,
437 (float)tk->i_rate/(float)tk->i_scale );
440 msg_Warn( p_input, "stream[%d] unknown type", i );
444 tk->p_es = es_out_Add( p_input->p_es_out, &fmt );
445 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
448 if( p_sys->i_track <= 0 )
450 msg_Err( p_input, "no valid track" );
454 if( config_GetInt( p_input, "avi-index" ) )
456 if( p_sys->b_seekable )
458 AVI_IndexCreate( p_input );
462 msg_Warn( p_input, "cannot create index (unseekable stream)" );
463 AVI_IndexLoad( p_input );
468 AVI_IndexLoad( p_input );
471 /* *** movie length in sec *** */
472 p_sys->i_length = AVI_MovieGetLength( p_input );
473 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
474 (mtime_t)p_avih->i_microsecperframe /
477 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
479 /* fix some BeOS MediaKit generated file */
480 for( i = 0 ; i < p_sys->i_track; i++ )
482 avi_track_t *tk = p_sys->track[i];
483 avi_chunk_list_t *p_strl;
484 avi_chunk_strh_t *p_strh;
485 avi_chunk_strf_auds_t *p_auds;
487 if( tk->i_cat != AUDIO_ES )
491 if( tk->i_idxnb < 1 ||
493 tk->i_samplesize != 0 )
497 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
498 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
499 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
501 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
502 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
504 int64_t i_track_length =
505 tk->p_index[tk->i_idxnb-1].i_length +
506 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
507 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
508 (mtime_t)p_avih->i_microsecperframe;
512 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
515 tk->i_samplesize = 1;
516 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
517 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
521 if( p_sys->i_length )
523 p_input->stream.i_mux_rate =
524 stream_Size( p_input->s ) / 50 / p_sys->i_length;
527 if( p_sys->b_seekable )
529 /* we have read all chunk so go back to movi */
530 stream_Seek( p_input->s, p_movi->i_chunk_pos );
532 /* Skip movi header */
533 stream_Read( p_input->s, NULL, 12 );
535 p_sys->i_movi_begin = p_movi->i_chunk_pos;
541 vlc_meta_Delete( p_sys->meta );
543 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
548 /*****************************************************************************
549 * Close: frees unused data
550 *****************************************************************************/
551 static void Close ( vlc_object_t * p_this )
553 input_thread_t * p_input = (input_thread_t *)p_this;
555 demux_sys_t *p_sys = p_input->p_demux_data ;
557 for( i = 0; i < p_sys->i_track; i++ )
559 if( p_sys->track[i] )
561 FREE( p_sys->track[i]->p_index );
562 free( p_sys->track[i] );
565 FREE( p_sys->track );
566 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
567 vlc_meta_Delete( p_sys->meta );
572 /*****************************************************************************
573 * Demux_Seekable: reads and demuxes data packets for stream seekable
574 *****************************************************************************
575 * AVIDemux: reads and demuxes data packets
576 *****************************************************************************
577 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
578 *****************************************************************************/
585 off_t i_posf; /* where we will read :
586 if i_idxposb == 0 : begining of chunk (+8 to acces data)
587 else : point on data directly */
588 } avi_track_toread_t;
590 static int Demux_Seekable( input_thread_t *p_input )
592 demux_sys_t *p_sys = p_input->p_demux_data;
594 unsigned int i_track_count = 0;
595 unsigned int i_track;
597 /* cannot be more than 100 stream (dcXX or wbXX) */
598 avi_track_toread_t toread[100];
601 /* detect new selected/unselected streams */
602 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
604 avi_track_t *tk = p_sys->track[i_track];
607 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
608 if( b && !tk->b_activated )
610 if( p_sys->b_seekable)
612 AVI_TrackSeek( p_input, i_track, p_sys->i_time );
614 tk->b_activated = VLC_TRUE;
616 else if( !b && tk->b_activated )
618 tk->b_activated = VLC_FALSE;
626 if( i_track_count <= 0 )
628 msg_Warn( p_input, "no track selected, exiting..." );
632 /* wait for the good time */
633 p_sys->i_pcr = p_sys->i_time * 9 / 100;
635 input_ClockManageRef( p_input,
636 p_input->stream.p_selected_program,
640 p_sys->i_time += 25*1000; /* read 25ms */
643 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
645 avi_track_t *tk = p_sys->track[i_track];
648 toread[i_track].b_ok = tk->b_activated;
649 if( tk->i_idxposc < tk->i_idxnb )
651 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
652 if( tk->i_idxposb > 0 )
654 toread[i_track].i_posf += 8 + tk->i_idxposb;
659 toread[i_track].i_posf = -1;
662 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
664 if( tk->i_samplesize )
666 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
670 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
675 toread[i_track].i_toread *= -1;
679 b_stream = VLC_FALSE;
690 /* search for first chunk to be read */
691 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
693 if( !toread[i].b_ok ||
694 AVI_GetDPTS( p_sys->track[i],
695 toread[i].i_toread ) <= -25 * 1000 )
700 if( toread[i].i_toread > 0 )
702 b_done = VLC_FALSE; /* not yet finished */
704 if( toread[i].i_posf > 0 )
706 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
709 i_pos = toread[i].i_posf;
721 /* no valid index, we will parse directly the stream
722 * in case we fail we will disable all finished stream */
723 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
725 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
726 if( AVI_PacketNext( p_input ) )
728 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
733 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
740 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
743 "cannot get packet header, track disabled" );
744 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
746 if( avi_pk.i_stream >= p_sys->i_track ||
747 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
749 if( AVI_PacketNext( p_input ) )
752 "cannot skip packet, track disabled" );
753 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
759 /* add this chunk to the index */
762 index.i_id = avi_pk.i_fourcc;
764 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
766 index.i_pos = avi_pk.i_pos;
767 index.i_length = avi_pk.i_size;
768 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
770 i_track = avi_pk.i_stream;
771 tk = p_sys->track[i_track];
772 /* do we will read this data ? */
773 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
779 if( AVI_PacketNext( p_input ) )
782 "cannot skip packet, track disabled" );
783 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
792 stream_Seek( p_input->s, i_pos );
795 /* Set the track to use */
796 tk = p_sys->track[i_track];
798 /* read thoses data */
799 if( tk->i_samplesize )
801 unsigned int i_toread;
803 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
805 if( tk->i_samplesize > 1 )
807 i_toread = tk->i_samplesize;
811 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
814 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
820 i_size = tk->p_index[tk->i_idxposc].i_length;
823 if( tk->i_idxposb == 0 )
825 i_size += 8; /* need to read and skip header */
828 if( ( p_frame = stream_Block( p_input->s, __EVEN( i_size ) ) )==NULL )
830 msg_Warn( p_input, "failled reading data" );
831 tk->b_activated = VLC_FALSE;
832 toread[i_track].b_ok = VLC_FALSE;
835 if( i_size % 2 ) /* read was padded on word boundary */
840 if( tk->i_idxposb == 0 )
842 p_frame->p_buffer += 8;
843 p_frame->i_buffer -= 8;
845 p_frame->i_pts = AVI_GetPTS( tk );
846 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
848 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
852 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
856 if( tk->i_samplesize )
858 if( tk->i_idxposb == 0 )
862 toread[i_track].i_toread -= i_size;
863 tk->i_idxposb += i_size;
865 tk->p_index[tk->i_idxposc].i_length )
873 int i_length = tk->p_index[tk->i_idxposc].i_length;
876 if( tk->i_cat == AUDIO_ES )
878 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
880 toread[i_track].i_toread--;
883 if( tk->i_idxposc < tk->i_idxnb)
885 toread[i_track].i_posf =
886 tk->p_index[tk->i_idxposc].i_pos;
887 if( tk->i_idxposb > 0 )
889 toread[i_track].i_posf += 8 + tk->i_idxposb;
895 toread[i_track].i_posf = -1;
898 b_stream = VLC_TRUE; /* at least one read succeed */
901 input_ClockGetTS( p_input,
902 p_input->stream.p_selected_program,
903 p_frame->i_pts * 9/100);
905 if( tk->i_cat != VIDEO_ES )
906 p_frame->i_dts = p_frame->i_pts;
909 p_frame->i_dts = p_frame->i_pts;
913 //p_pes->i_rate = p_input->stream.control.i_rate;
914 es_out_Send( p_input->p_es_out, tk->p_es, p_frame );
919 /*****************************************************************************
920 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
921 *****************************************************************************
922 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
923 *****************************************************************************/
924 static int Demux_UnSeekable( input_thread_t *p_input )
926 demux_sys_t *p_sys = p_input->p_demux_data;
927 avi_track_t *p_stream_master = NULL;
929 unsigned int i_stream;
930 unsigned int i_packet;
932 /* Check if we need to send the audio data to decoder */
933 b_audio = !p_input->stream.control.b_mute;
935 input_ClockManageRef( p_input,
936 p_input->stream.p_selected_program,
939 /* *** find master stream for data packet skipping algo *** */
940 /* *** -> first video, if any, or first audio ES *** */
941 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
943 avi_track_t *tk = p_sys->track[i_stream];
946 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
948 if( b && tk->i_cat == VIDEO_ES )
950 p_stream_master = tk;
954 p_stream_master = tk;
958 if( !p_stream_master )
960 msg_Warn( p_input, "no more stream selected" );
964 p_sys->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
966 for( i_packet = 0; i_packet < 10; i_packet++)
968 #define p_stream p_sys->track[avi_pk.i_stream]
972 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
977 if( avi_pk.i_stream >= p_sys->i_track ||
978 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
980 /* we haven't found an audio or video packet:
981 * - we have seek, found first next packet
982 * - others packets could be found, skip them
984 switch( avi_pk.i_fourcc )
989 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
993 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
995 return( 0 ); /* eof */
998 "seems to have lost position, resync" );
999 if( AVI_PacketSearch( p_input ) )
1001 msg_Err( p_input, "resync failed" );
1008 /* do will send this packet to decoder ? */
1009 if( !b_audio && avi_pk.i_cat == AUDIO_ES )
1011 if( AVI_PacketNext( p_input ) )
1018 /* it's a selected stream, check for time */
1019 if( __ABS( AVI_GetPTS( p_stream ) -
1020 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1022 /* load it and send to decoder */
1024 if( AVI_PacketRead( p_input, &avi_pk, &p_frame ) || p_frame == NULL )
1029 input_ClockGetTS( p_input,
1030 p_input->stream.p_selected_program,
1031 AVI_GetPTS( p_stream ) * 9/100);
1033 if( avi_pk.i_cat != VIDEO_ES )
1034 p_frame->i_dts = p_frame->i_pts;
1037 p_frame->i_dts = p_frame->i_pts;
1041 //p_pes->i_rate = p_input->stream.control.i_rate;
1042 es_out_Send( p_input->p_es_out, p_stream->p_es, p_frame );
1046 if( AVI_PacketNext( p_input ) )
1053 /* *** update stream time position *** */
1054 if( p_stream->i_samplesize )
1056 p_stream->i_idxposb += avi_pk.i_size;
1060 if( p_stream->i_cat == AUDIO_ES )
1062 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1064 p_stream->i_idxposc++;
1074 /*****************************************************************************
1075 * Seek: goto to i_date or i_percent
1076 *****************************************************************************
1077 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1078 *****************************************************************************/
1079 static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
1082 demux_sys_t *p_sys = p_input->p_demux_data;
1083 unsigned int i_stream;
1085 "seek requested: "I64Fd" secondes %d%%",
1089 if( p_sys->b_seekable )
1091 if( !p_sys->i_length )
1093 avi_track_t *p_stream;
1096 /* use i_percent to create a true i_date */
1098 "mmh, seeking without index at %d%%"
1099 " work only for interleaved file", i_percent );
1100 if( i_percent >= 100 )
1102 msg_Warn( p_input, "cannot seek so far !" );
1105 i_percent = __MAX( i_percent, 0 );
1107 /* try to find chunk that is at i_percent or the file */
1108 i_pos = __MAX( i_percent *
1109 stream_Size( p_input->s ) / 100,
1110 p_sys->i_movi_begin );
1111 /* search first selected stream */
1112 for( i_stream = 0, p_stream = NULL;
1113 i_stream < p_sys->i_track; i_stream++ )
1115 p_stream = p_sys->track[i_stream];
1116 if( p_stream->b_activated )
1121 if( !p_stream || !p_stream->b_activated )
1123 msg_Warn( p_input, "cannot find any selected stream" );
1127 /* be sure that the index exist */
1128 if( AVI_StreamChunkSet( p_input,
1132 msg_Warn( p_input, "cannot seek" );
1136 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1137 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1139 /* search after i_idxposc */
1140 if( AVI_StreamChunkSet( p_input,
1141 i_stream, p_stream->i_idxposc + 1 ) )
1143 msg_Warn( p_input, "cannot seek" );
1147 i_date = AVI_GetPTS( p_stream );
1148 /* TODO better support for i_samplesize != 0 */
1149 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1152 #define p_stream p_sys->track[i_stream]
1154 /* seek for chunk based streams */
1155 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1157 if( p_stream->b_activated && !p_stream->i_samplesize )
1158 /* if( p_stream->b_activated ) */
1160 AVI_TrackSeek( p_input, i_stream, i_date );
1161 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1168 i_date = p_sys->i_time;
1170 /* seek for bytes based streams */
1171 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1173 if( p_stream->b_activated && p_stream->i_samplesize )
1175 AVI_TrackSeek( p_input, i_stream, i_date );
1176 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1179 msg_Dbg( p_input, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1180 /* set true movie time */
1182 if( !p_sys->i_time )
1184 p_sys->i_time = i_date;
1191 msg_Err( p_input, "shouldn't yet be executed" );
1196 /*****************************************************************************
1198 *****************************************************************************
1200 *****************************************************************************/
1201 static double ControlGetPosition( input_thread_t *p_input )
1203 demux_sys_t *p_sys = p_input->p_demux_data;
1205 if( p_sys->i_length > 0 )
1207 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1209 else if( stream_Size( p_input->s ) > 0 )
1215 /* search the more advanced selected es */
1216 for( i = 0; i < p_sys->i_track; i++ )
1218 avi_track_t *tk = p_sys->track[i];
1219 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1221 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1222 tk->p_index[tk->i_idxposc].i_length + 8;
1229 return (double)i64 / (double)stream_Size( p_input->s );
1234 static int Control( input_thread_t *p_input, int i_query, va_list args )
1236 demux_sys_t *p_sys = p_input->p_demux_data;
1240 vlc_meta_t **pp_meta;
1244 case DEMUX_GET_POSITION:
1245 pf = (double*)va_arg( args, double * );
1246 *pf = ControlGetPosition( p_input );
1248 case DEMUX_SET_POSITION:
1249 if( p_sys->b_seekable )
1251 f = (double)va_arg( args, double );
1252 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1253 return Seek( p_input, i64, (int)(f * 100) ) < 0 ? VLC_EGENERIC : VLC_SUCCESS;
1255 return demux_vaControlDefault( p_input, i_query, args );
1257 case DEMUX_GET_TIME:
1258 pi64 = (int64_t*)va_arg( args, int64_t * );
1259 *pi64 = p_sys->i_time;
1262 case DEMUX_SET_TIME:
1266 i64 = (int64_t)va_arg( args, int64_t );
1267 if( p_sys->i_length > 0 )
1269 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1271 else if( p_sys->i_time > 0 )
1273 i_percent = (int)( 100.0 * ControlGetPosition( p_input ) *
1274 (double)i64 / (double)p_sys->i_time );
1276 return Seek( p_input, i64, i_percent );
1278 case DEMUX_GET_LENGTH:
1279 pi64 = (int64_t*)va_arg( args, int64_t * );
1280 *pi64 = p_sys->i_length * (mtime_t)1000000;
1284 pf = (double*)va_arg( args, double * );
1286 for( i = 0; i < (int)p_sys->i_track; i++ )
1288 avi_track_t *tk = p_sys->track[i];
1289 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1291 *pf = (float)tk->i_rate / (float)tk->i_scale;
1296 case DEMUX_GET_META:
1297 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1298 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1302 return demux_vaControlDefault( p_input, i_query, args );
1304 return VLC_EGENERIC;
1307 /*****************************************************************************
1308 * Function to convert pts to chunk or byte
1309 *****************************************************************************/
1311 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1313 return (mtime_t)((int64_t)i_pts *
1314 (int64_t)tk->i_rate /
1315 (int64_t)tk->i_scale /
1318 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1320 return (mtime_t)((int64_t)i_pts *
1321 (int64_t)tk->i_rate /
1322 (int64_t)tk->i_scale /
1324 (int64_t)tk->i_samplesize );
1327 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1331 i_dpts = (mtime_t)( (int64_t)1000000 *
1333 (int64_t)tk->i_scale /
1334 (int64_t)tk->i_rate );
1336 if( tk->i_samplesize )
1338 return i_dpts / tk->i_samplesize;
1343 static mtime_t AVI_GetPTS( avi_track_t *tk )
1345 if( tk->i_samplesize )
1347 int64_t i_count = 0;
1349 /* we need a valid entry we will emulate one */
1350 if( tk->i_idxposc == tk->i_idxnb )
1354 /* use the last entry */
1355 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1356 + tk->p_index[tk->i_idxnb - 1].i_length;
1361 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1363 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1367 if( tk->i_cat == AUDIO_ES )
1369 return AVI_GetDPTS( tk, tk->i_blockno );
1373 return AVI_GetDPTS( tk, tk->i_idxposc );
1378 static int AVI_StreamChunkFind( input_thread_t *p_input,
1379 unsigned int i_stream )
1381 demux_sys_t *p_sys = p_input->p_demux_data;
1382 avi_packet_t avi_pk;
1384 /* find first chunk of i_stream that isn't in index */
1386 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1388 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
1389 if( AVI_PacketNext( p_input ) )
1391 return VLC_EGENERIC;
1396 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
1401 if( p_input->b_die )
1403 return VLC_EGENERIC;
1406 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1408 msg_Warn( p_input, "cannot get packet header" );
1409 return VLC_EGENERIC;
1411 if( avi_pk.i_stream >= p_sys->i_track ||
1412 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1414 if( AVI_PacketNext( p_input ) )
1416 return VLC_EGENERIC;
1421 /* add this chunk to the index */
1424 index.i_id = avi_pk.i_fourcc;
1426 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1428 index.i_pos = avi_pk.i_pos;
1429 index.i_length = avi_pk.i_size;
1430 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1432 if( avi_pk.i_stream == i_stream )
1437 if( AVI_PacketNext( p_input ) )
1439 return VLC_EGENERIC;
1446 /* be sure that i_ck will be a valid index entry */
1447 static int AVI_StreamChunkSet( input_thread_t *p_input,
1448 unsigned int i_stream,
1451 demux_sys_t *p_sys = p_input->p_demux_data;
1452 avi_track_t *p_stream = p_sys->track[i_stream];
1454 p_stream->i_idxposc = i_ck;
1455 p_stream->i_idxposb = 0;
1457 if( i_ck >= p_stream->i_idxnb )
1459 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1462 p_stream->i_idxposc++;
1463 if( AVI_StreamChunkFind( p_input, i_stream ) )
1465 return VLC_EGENERIC;
1468 } while( p_stream->i_idxposc < i_ck );
1475 /* XXX FIXME up to now, we assume that all chunk are one after one */
1476 static int AVI_StreamBytesSet( input_thread_t *p_input,
1477 unsigned int i_stream,
1480 demux_sys_t *p_sys = p_input->p_demux_data;
1481 avi_track_t *p_stream = p_sys->track[i_stream];
1483 if( ( p_stream->i_idxnb > 0 )
1484 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1485 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1487 /* index is valid to find the ck */
1488 /* uses dichototmie to be fast enougth */
1489 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1490 int i_idxmax = p_stream->i_idxnb;
1494 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1496 i_idxmax = i_idxposc ;
1497 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1501 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1502 p_stream->p_index[i_idxposc].i_length <= i_byte)
1504 i_idxmin = i_idxposc ;
1505 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1509 p_stream->i_idxposc = i_idxposc;
1510 p_stream->i_idxposb = i_byte -
1511 p_stream->p_index[i_idxposc].i_lengthtotal;
1520 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1521 p_stream->i_idxposb = 0;
1524 p_stream->i_idxposc++;
1525 if( AVI_StreamChunkFind( p_input, i_stream ) )
1527 return VLC_EGENERIC;
1530 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1531 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1533 p_stream->i_idxposb = i_byte -
1534 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1539 static int AVI_TrackSeek( input_thread_t *p_input,
1543 demux_sys_t *p_sys = p_input->p_demux_data;
1544 avi_track_t *tk = p_sys->track[i_stream];
1546 #define p_stream p_sys->track[i_stream]
1549 i_oldpts = AVI_GetPTS( p_stream );
1551 if( !p_stream->i_samplesize )
1553 if( AVI_StreamChunkSet( p_input,
1555 AVI_PTSToChunk( p_stream, i_date ) ) )
1557 return VLC_EGENERIC;
1560 if( p_stream->i_cat == AUDIO_ES )
1564 for( i = 0; i < tk->i_idxposc; i++ )
1566 if( tk->i_blocksize > 0 )
1568 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1578 "old:"I64Fd" %s new "I64Fd,
1580 i_oldpts > i_date ? ">" : "<",
1583 if( p_stream->i_cat == VIDEO_ES )
1585 /* search key frame */
1586 if( i_date < i_oldpts )
1588 while( p_stream->i_idxposc > 0 &&
1589 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1592 if( AVI_StreamChunkSet( p_input,
1594 p_stream->i_idxposc - 1 ) )
1596 return VLC_EGENERIC;
1602 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1603 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1606 if( AVI_StreamChunkSet( p_input,
1608 p_stream->i_idxposc + 1 ) )
1610 return VLC_EGENERIC;
1618 if( AVI_StreamBytesSet( p_input,
1620 AVI_PTSToByte( p_stream, i_date ) ) )
1622 return VLC_EGENERIC;
1629 /****************************************************************************
1630 * Return VLC_TRUE if it's a key frame
1631 ****************************************************************************/
1632 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1638 * startcode: 0x00000100 32bits
1639 * framenumber ? 5bits
1640 * piture type 0(I),1(P) 2bits
1642 if( GetDWBE( p_byte ) != 0x00000100 )
1644 /* it's not an msmpegv1 stream, strange...*/
1645 return AVIIF_KEYFRAME;
1647 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1650 case FOURCC_DIV3: /* wmv1 also */
1652 * picture type 0(I),1(P) 2bits
1654 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1656 /* we should find first occurence of 0x000001b6 (32bits)
1657 * startcode: 0x000001b6 32bits
1658 * piture type 0(I),1(P) 2bits
1660 if( GetDWBE( p_byte ) != 0x000001b6 )
1662 /* not true , need to find the first VOP header */
1663 return AVIIF_KEYFRAME;
1665 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1668 /* I can't do it, so say yes */
1669 return AVIIF_KEYFRAME;
1673 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1678 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1682 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1686 return VLC_FOURCC('m','r','l','e');
1735 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1739 /****************************************************************************
1741 ****************************************************************************/
1742 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1743 int *pi_number, int *pi_type )
1745 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1748 c1 = ((uint8_t *)&i_id)[0];
1749 c2 = ((uint8_t *)&i_id)[1];
1751 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1753 SET_PTR( pi_number, 100 ); /* > max stream number */
1754 SET_PTR( pi_type, UNKNOWN_ES );
1758 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1759 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1762 SET_PTR( pi_type, AUDIO_ES );
1766 SET_PTR( pi_type, VIDEO_ES );
1769 SET_PTR( pi_type, UNKNOWN_ES );
1776 /****************************************************************************
1778 ****************************************************************************/
1779 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
1783 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1785 return VLC_EGENERIC;
1787 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1788 p_pk->i_size = GetDWLE( p_peek + 4 );
1789 p_pk->i_pos = stream_Tell( p_input->s );
1790 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1792 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1793 p_peek[10], p_peek[11] );
1800 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1802 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1806 static int AVI_PacketNext( input_thread_t *p_input )
1808 avi_packet_t avi_ck;
1811 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
1813 return VLC_EGENERIC;
1816 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1817 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1821 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1822 avi_ck.i_type == AVIFOURCC_AVIX )
1828 i_skip = __EVEN( avi_ck.i_size ) + 8;
1831 if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
1833 return VLC_EGENERIC;
1837 static int AVI_PacketRead( input_thread_t *p_input,
1839 block_t **pp_frame )
1843 i_size = __EVEN( p_pk->i_size + 8 );
1845 if( ( *pp_frame = stream_Block( p_input->s, i_size ) ) == NULL )
1847 return VLC_EGENERIC;
1849 (*pp_frame)->p_buffer += 8;
1850 (*pp_frame)->i_buffer -= 8;
1852 if( i_size != p_pk->i_size + 8 )
1854 (*pp_frame)->i_buffer--;
1860 static int AVI_PacketSearch( input_thread_t *p_input )
1862 demux_sys_t *p_sys = p_input->p_demux_data;
1863 avi_packet_t avi_pk;
1868 if( stream_Read( p_input->s, NULL, 1 ) != 1 )
1870 return VLC_EGENERIC;
1872 AVI_PacketGetHeader( p_input, &avi_pk );
1873 if( avi_pk.i_stream < p_sys->i_track &&
1874 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1878 switch( avi_pk.i_fourcc )
1880 case AVIFOURCC_JUNK:
1881 case AVIFOURCC_LIST:
1882 case AVIFOURCC_RIFF:
1883 case AVIFOURCC_idx1:
1887 /* Prevents from eating all the CPU with broken files.
1888 * This value should be low enough so that it doesn't affect the
1889 * reading speed too much (not that we care much anyway because
1890 * this code is called only on broken files). */
1891 if( !(++i_count % 1024) )
1893 if( p_input->b_die ) return VLC_EGENERIC;
1896 if( !(i_count % (1024 * 10)) )
1897 msg_Warn( p_input, "trying to resync..." );
1902 /****************************************************************************
1904 ****************************************************************************/
1905 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1907 avi_entry_t *p_index)
1909 avi_track_t *tk = p_sys->track[i_stream];
1911 /* Update i_movi_lastchunk_pos */
1912 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1914 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1918 if( tk->i_idxnb >= tk->i_idxmax )
1920 tk->i_idxmax += 16384;
1921 tk->p_index = realloc( tk->p_index,
1922 tk->i_idxmax * sizeof( avi_entry_t ) );
1923 if( tk->p_index == NULL )
1928 /* calculate cumulate length */
1929 if( tk->i_idxnb > 0 )
1931 p_index->i_lengthtotal =
1932 tk->p_index[tk->i_idxnb - 1].i_length +
1933 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1937 p_index->i_lengthtotal = 0;
1940 tk->p_index[tk->i_idxnb++] = *p_index;
1943 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
1945 demux_sys_t *p_sys = p_input->p_demux_data;
1947 avi_chunk_list_t *p_riff;
1948 avi_chunk_list_t *p_movi;
1949 avi_chunk_idx1_t *p_idx1;
1951 unsigned int i_stream;
1952 unsigned int i_index;
1956 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1957 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1958 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1962 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
1963 return VLC_EGENERIC;
1966 /* *** calculate offset *** */
1967 /* Well, avi is __SHIT__ so test more than one entry
1968 * (needed for some avi files) */
1970 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1972 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1974 i_offset = p_movi->i_chunk_pos + 8;
1979 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
1983 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
1986 if( i_stream < p_sys->i_track &&
1987 i_cat == p_sys->track[i_stream]->i_cat )
1990 index.i_id = p_idx1->entry[i_index].i_fourcc;
1992 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
1993 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
1994 index.i_length = p_idx1->entry[i_index].i_length;
1995 AVI_IndexAddEntry( p_sys, i_stream, &index );
2001 static void __Parse_indx( input_thread_t *p_input,
2003 avi_chunk_indx_t *p_indx )
2005 demux_sys_t *p_sys = p_input->p_demux_data;
2009 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2010 if( p_indx->i_indexsubtype == 0 )
2012 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2014 index.i_id = p_indx->i_id;
2015 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2016 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2017 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2019 AVI_IndexAddEntry( p_sys, i_stream, &index );
2022 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2024 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2026 index.i_id = p_indx->i_id;
2027 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2028 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2029 index.i_length = p_indx->idx.field[i].i_size;
2031 AVI_IndexAddEntry( p_sys, i_stream, &index );
2036 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
2040 static void AVI_IndexLoad_indx( input_thread_t *p_input )
2042 demux_sys_t *p_sys = p_input->p_demux_data;
2043 unsigned int i_stream;
2046 avi_chunk_list_t *p_riff;
2047 avi_chunk_list_t *p_hdrl;
2049 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2050 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2052 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2054 avi_chunk_list_t *p_strl;
2055 avi_chunk_indx_t *p_indx;
2057 #define p_stream p_sys->track[i_stream]
2058 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2059 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2063 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
2067 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2069 __Parse_indx( p_input, i_stream, p_indx );
2071 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2073 avi_chunk_indx_t ck_sub;
2074 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2076 if( stream_Seek( p_input->s, p_indx->idx.super[i].i_offset )||
2077 AVI_ChunkRead( p_input->s, &ck_sub, NULL ) )
2081 __Parse_indx( p_input, i_stream, &ck_sub );
2086 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
2092 static void AVI_IndexLoad( input_thread_t *p_input )
2094 demux_sys_t *p_sys = p_input->p_demux_data;
2095 unsigned int i_stream;
2097 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2099 p_sys->track[i_stream]->i_idxnb = 0;
2100 p_sys->track[i_stream]->i_idxmax = 0;
2101 p_sys->track[i_stream]->p_index = NULL;
2106 AVI_IndexLoad_indx( p_input );
2108 else if( AVI_IndexLoad_idx1( p_input ) )
2110 /* try indx if idx1 failed as some "normal" file have indx too */
2111 AVI_IndexLoad_indx( p_input );
2114 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2116 msg_Dbg( p_input, "stream[%d] created %d index entries",
2117 i_stream, p_sys->track[i_stream]->i_idxnb );
2121 static void AVI_IndexCreate( input_thread_t *p_input )
2123 demux_sys_t *p_sys = p_input->p_demux_data;
2125 avi_chunk_list_t *p_riff;
2126 avi_chunk_list_t *p_movi;
2128 unsigned int i_stream;
2131 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2132 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2136 msg_Err( p_input, "cannot find p_movi" );
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;
2146 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2147 stream_Size( p_input->s ) );
2149 stream_Seek( p_input->s, p_movi->i_chunk_pos + 12 );
2150 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
2155 if( p_input->b_die )
2160 if( AVI_PacketGetHeader( p_input, &pk ) )
2164 if( pk.i_stream < p_sys->i_track &&
2165 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2168 index.i_id = pk.i_fourcc;
2170 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2171 index.i_pos = pk.i_pos;
2172 index.i_length = pk.i_size;
2173 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2177 switch( pk.i_fourcc )
2179 case AVIFOURCC_idx1:
2182 avi_chunk_list_t *p_sysx;
2183 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2184 AVIFOURCC_RIFF, 1 );
2186 msg_Dbg( p_input, "looking for new RIFF chunk" );
2187 if( stream_Seek( p_input->s, p_sysx->i_chunk_pos + 24))
2194 case AVIFOURCC_RIFF:
2195 msg_Dbg( p_input, "new RIFF chunk found" );
2197 case AVIFOURCC_JUNK:
2200 msg_Warn( p_input, "need resync, probably broken avi" );
2201 if( AVI_PacketSearch( p_input ) )
2203 msg_Warn( p_input, "lost sync, abord index creation" );
2209 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2210 AVI_PacketNext( p_input ) )
2217 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2220 "stream[%d] creating %d index entries",
2222 p_sys->track[i_stream]->i_idxnb );
2226 /*****************************************************************************
2228 *****************************************************************************/
2229 static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
2231 demux_sys_t *p_sys = p_input->p_demux_data;
2233 int b_end = VLC_TRUE;
2235 for( i = 0; i < p_sys->i_track; i++ )
2237 avi_track_t *tk = p_sys->track[i];
2238 if( tk->i_idxposc >= tk->i_idxnb )
2240 tk->b_activated = VLC_FALSE;
2241 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2251 /****************************************************************************
2252 * AVI_MovieGetLength give max streams length in second
2253 ****************************************************************************/
2254 static mtime_t AVI_MovieGetLength( input_thread_t *p_input )
2256 demux_sys_t *p_sys = p_input->p_demux_data;
2257 mtime_t i_maxlength = 0;
2260 for( i = 0; i < p_sys->i_track; i++ )
2262 avi_track_t *tk = p_sys->track[i];
2265 /* fix length for each stream */
2266 if( tk->i_idxnb < 1 || !tk->p_index )
2271 if( tk->i_samplesize )
2273 i_length = AVI_GetDPTS( tk,
2274 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2275 tk->p_index[tk->i_idxnb-1].i_length );
2279 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2281 i_length /= (mtime_t)1000000; /* in seconds */
2284 "stream[%d] length:"I64Fd" (based on index)",
2287 i_maxlength = __MAX( i_maxlength, i_length );