1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: avi.c,v 1.89 2004/02/27 14:22:18 fenrir Exp $
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>
37 /*****************************************************************************
39 *****************************************************************************/
41 #define INTERLEAVE_TEXT N_("Force interleaved method" )
42 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method" )
44 #define INDEX_TEXT N_("Force index creation")
45 #define INDEX_LONGTEXT N_( \
46 "Recreate a index for the AVI file so we can seek trough it more reliably." )
48 static int Open ( vlc_object_t * );
49 static void Close ( vlc_object_t * );
52 set_description( _("AVI demuxer") );
53 set_capability( "demux", 212 );
55 add_bool( "avi-interleaved", 0, NULL,
56 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
57 add_bool( "avi-index", 0, NULL,
58 INDEX_TEXT, INDEX_LONGTEXT, VLC_TRUE );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Control ( input_thread_t *, int, va_list );
67 static int Seek ( input_thread_t *, mtime_t, int );
68 static int Demux_Seekable ( input_thread_t * );
69 static int Demux_UnSeekable( input_thread_t *p_input );
71 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
72 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
74 static inline off_t __EVEN( off_t i )
76 return (i & 1) ? i + 1 : i;
79 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
80 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
81 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
82 static mtime_t AVI_GetPTS ( avi_track_t * );
85 static int AVI_StreamChunkFind( input_thread_t *, unsigned int i_stream );
86 static int AVI_StreamChunkSet ( input_thread_t *,
87 unsigned int i_stream, unsigned int i_ck );
88 static int AVI_StreamBytesSet ( input_thread_t *,
89 unsigned int i_stream, off_t i_byte );
91 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
92 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
94 static int AVI_PacketGetHeader( input_thread_t *, avi_packet_t *p_pk );
95 static int AVI_PacketNext ( input_thread_t * );
96 static int AVI_PacketRead ( input_thread_t *, avi_packet_t *, block_t **);
97 static int AVI_PacketSearch ( input_thread_t * );
99 static void AVI_IndexLoad ( input_thread_t * );
100 static void AVI_IndexCreate ( input_thread_t * );
101 static void AVI_IndexAddEntry( demux_sys_t *, int, AVIIndexEntry_t * );
103 static mtime_t AVI_MovieGetLength( input_thread_t * );
105 /*****************************************************************************
107 *****************************************************************************/
108 static int AVI_TrackSeek ( input_thread_t *, int, mtime_t );
109 static int AVI_TrackStopFinishedStreams( input_thread_t *);
112 - For VBR mp3 stream:
113 count blocks by rounded-up chunksizes instead of chunks
114 we need full emulation of dshow avi demuxer bugs :(
115 fixes silly nandub-style a-v delaying in avi with vbr mp3...
116 (from mplayer 2002/08/02)
120 /*****************************************************************************
121 * Open: check file and initializes AVI structures
122 *****************************************************************************/
123 static int Open( vlc_object_t * p_this )
125 input_thread_t *p_input = (input_thread_t *)p_this;
129 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
130 avi_chunk_list_t *p_hdrl, *p_movi;
131 avi_chunk_avih_t *p_avih;
133 unsigned int i_track;
139 /* Is it an avi file ? */
140 if( stream_Peek( p_input->s, &p_peek, 12 ) < 12 )
142 msg_Err( p_input, "cannot peek()" );
145 if( strncmp( &p_peek[0], "RIFF", 4 ) || strncmp( &p_peek[8], "AVI ", 4 ) )
147 msg_Warn( p_input, "avi module discarded (invalid header)" );
151 /* Initialize input structures. */
152 p_sys = p_input->p_demux_data = malloc( sizeof(demux_sys_t) );
153 memset( p_sys, 0, sizeof( demux_sys_t ) );
157 p_sys->i_movi_lastchunk_pos = 0;
158 p_sys->b_odml = VLC_FALSE;
163 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
165 p_input->pf_demux_control = Control;
166 p_input->pf_demux = Demux_Seekable;
167 /* For unseekable stream, automaticaly use Demux_UnSeekable */
168 if( !p_sys->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
170 p_input->pf_demux = Demux_UnSeekable;
173 if( AVI_ChunkReadRoot( p_input->s, &p_sys->ck_root ) )
175 msg_Err( p_input, "avi module discarded (invalid file)" );
179 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
181 unsigned int i_count =
182 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
184 msg_Warn( p_input, "multiple riff -> OpenDML ?" );
185 for( i = 1; i < i_count; i++ )
187 avi_chunk_list_t *p_sysx;
189 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
190 if( p_sysx->i_type == AVIFOURCC_AVIX )
192 msg_Warn( p_input, "detected OpenDML file" );
193 p_sys->b_odml = VLC_TRUE;
199 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
200 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
201 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
203 if( !p_hdrl || !p_movi )
205 msg_Err( p_input, "avi module discarded (invalid file)" );
209 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
211 msg_Err( p_input, "cannot find avih chunk" );
214 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
215 if( p_avih->i_streams != i_track )
218 "found %d stream but %d are declared",
219 i_track, p_avih->i_streams );
223 msg_Err( p_input, "no stream defined!" );
227 /* create one program */
228 vlc_mutex_lock( &p_input->stream.stream_lock );
229 if( input_InitStream( p_input, 0 ) == -1)
231 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 msg_Err( p_input, "cannot init stream" );
235 p_input->stream.i_mux_rate = 0; /* Fixed later */
236 vlc_mutex_unlock( &p_input->stream.stream_lock );
238 /* print informations on streams */
239 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
241 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
242 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
243 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
244 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
245 if( ( p_sys->meta = vlc_meta_New() ) )
248 sprintf( buffer, "%s%s%s%s",
249 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
250 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
251 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
252 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
253 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
256 /* now read info on each stream and create ES */
257 for( i = 0 ; i < i_track; i++ )
259 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
260 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
261 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
262 avi_chunk_strf_auds_t *p_auds;
263 avi_chunk_strf_vids_t *p_vids;
266 tk->b_activated = VLC_FALSE;
276 p_auds = (void*)p_vids = (void*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
278 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
280 msg_Warn( p_input, "stream[%d] incomplete", i );
284 tk->i_rate = p_strh->i_rate;
285 tk->i_scale = p_strh->i_scale;
286 tk->i_samplesize = p_strh->i_samplesize;
287 msg_Dbg( p_input, "stream[%d] rate:%d scale:%d samplesize:%d",
288 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
290 switch( p_strh->i_type )
292 case( AVIFOURCC_auds ):
293 tk->i_cat = AUDIO_ES;
294 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
295 p_auds->p_wf->wFormatTag );
296 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
298 if( p_auds->p_wf->wFormatTag == 1 )
300 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
307 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
309 fmt.audio.i_channels = p_auds->p_wf->nChannels;
310 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
311 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
312 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
313 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
314 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
315 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
316 fmt.p_extra = &p_auds->p_wf[1];
317 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
318 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
319 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
322 case( AVIFOURCC_vids ):
323 tk->i_cat = VIDEO_ES;
324 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
325 p_vids->p_bih->biCompression );
326 if( p_vids->p_bih->biCompression == 0x00 )
328 switch( p_vids->p_bih->biBitCount )
331 tk->i_codec = VLC_FOURCC('R','V','3','2');
334 tk->i_codec = VLC_FOURCC('R','V','2','4');
337 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
340 tk->i_codec = VLC_FOURCC('R','V','1','5');
343 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
346 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
350 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
352 tk->i_samplesize = 0;
353 fmt.video.i_width = p_vids->p_bih->biWidth;
354 fmt.video.i_height = p_vids->p_bih->biHeight;
355 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
357 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
358 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
359 fmt.p_extra = &p_vids->p_bih[1];
360 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
362 (char*)&p_vids->p_bih->biCompression,
363 p_vids->p_bih->biWidth,
364 p_vids->p_bih->biHeight,
365 p_vids->p_bih->biBitCount,
366 (float)tk->i_rate/(float)tk->i_scale );
369 msg_Warn( p_input, "stream[%d] unknown type", i );
373 tk->p_es = es_out_Add( p_input->p_es_out, &fmt );
374 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
377 if( p_sys->i_track <= 0 )
379 msg_Err( p_input, "no valid track" );
383 if( config_GetInt( p_input, "avi-index" ) )
385 if( p_sys->b_seekable )
387 AVI_IndexCreate( p_input );
391 msg_Warn( p_input, "cannot create index (unseekable stream)" );
392 AVI_IndexLoad( p_input );
397 AVI_IndexLoad( p_input );
400 /* *** movie length in sec *** */
401 p_sys->i_length = AVI_MovieGetLength( p_input );
402 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
403 (mtime_t)p_avih->i_microsecperframe /
406 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
408 /* fix some BeOS MediaKit generated file */
409 for( i = 0 ; i < p_sys->i_track; i++ )
411 avi_track_t *tk = p_sys->track[i];
412 avi_chunk_list_t *p_strl;
413 avi_chunk_strh_t *p_strh;
414 avi_chunk_strf_auds_t *p_auds;
416 if( tk->i_cat != AUDIO_ES )
420 if( tk->i_idxnb < 1 ||
422 tk->i_samplesize != 0 )
426 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
427 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
428 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
430 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
431 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
433 int64_t i_track_length =
434 tk->p_index[tk->i_idxnb-1].i_length +
435 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
436 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
437 (mtime_t)p_avih->i_microsecperframe;
441 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
444 tk->i_samplesize = 1;
445 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
446 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
450 if( p_sys->i_length )
452 p_input->stream.i_mux_rate =
453 stream_Size( p_input->s ) / 50 / p_sys->i_length;
456 if( p_sys->b_seekable )
458 /* we have read all chunk so go back to movi */
459 stream_Seek( p_input->s, p_movi->i_chunk_pos );
461 /* Skip movi header */
462 stream_Read( p_input->s, NULL, 12 );
464 p_sys->i_movi_begin = p_movi->i_chunk_pos;
470 vlc_meta_Delete( p_sys->meta );
472 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
477 /*****************************************************************************
478 * Close: frees unused data
479 *****************************************************************************/
480 static void Close ( vlc_object_t * p_this )
482 input_thread_t * p_input = (input_thread_t *)p_this;
484 demux_sys_t *p_sys = p_input->p_demux_data ;
486 for( i = 0; i < p_sys->i_track; i++ )
488 if( p_sys->track[i] )
490 FREE( p_sys->track[i]->p_index );
491 free( p_sys->track[i] );
494 FREE( p_sys->track );
495 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
496 vlc_meta_Delete( p_sys->meta );
501 /*****************************************************************************
502 * Demux_Seekable: reads and demuxes data packets for stream seekable
503 *****************************************************************************
504 * AVIDemux: reads and demuxes data packets
505 *****************************************************************************
506 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
507 *****************************************************************************/
514 off_t i_posf; /* where we will read :
515 if i_idxposb == 0 : begining of chunk (+8 to acces data)
516 else : point on data directly */
517 } avi_track_toread_t;
519 static int Demux_Seekable( input_thread_t *p_input )
521 demux_sys_t *p_sys = p_input->p_demux_data;
523 unsigned int i_track_count = 0;
524 unsigned int i_track;
526 /* cannot be more than 100 stream (dcXX or wbXX) */
527 avi_track_toread_t toread[100];
530 /* detect new selected/unselected streams */
531 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
533 avi_track_t *tk = p_sys->track[i_track];
536 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
537 if( b && !tk->b_activated )
539 if( p_sys->b_seekable)
541 AVI_TrackSeek( p_input, i_track, p_sys->i_time );
543 tk->b_activated = VLC_TRUE;
545 else if( !b && tk->b_activated )
547 tk->b_activated = VLC_FALSE;
555 if( i_track_count <= 0 )
557 msg_Warn( p_input, "no track selected, exiting..." );
561 /* wait for the good time */
562 p_sys->i_pcr = p_sys->i_time * 9 / 100;
564 input_ClockManageRef( p_input,
565 p_input->stream.p_selected_program,
569 p_sys->i_time += 25*1000; /* read 25ms */
572 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
574 avi_track_t *tk = p_sys->track[i_track];
577 toread[i_track].b_ok = tk->b_activated;
578 if( tk->i_idxposc < tk->i_idxnb )
580 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
581 if( tk->i_idxposb > 0 )
583 toread[i_track].i_posf += 8 + tk->i_idxposb;
588 toread[i_track].i_posf = -1;
591 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
593 if( tk->i_samplesize )
595 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
599 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
604 toread[i_track].i_toread *= -1;
608 b_stream = VLC_FALSE;
619 /* search for first chunk to be read */
620 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
622 if( !toread[i].b_ok ||
623 AVI_GetDPTS( p_sys->track[i],
624 toread[i].i_toread ) <= -25 * 1000 )
629 if( toread[i].i_toread > 0 )
631 b_done = VLC_FALSE; /* not yet finished */
633 if( toread[i].i_posf > 0 )
635 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
638 i_pos = toread[i].i_posf;
650 /* no valid index, we will parse directly the stream
651 * in case we fail we will disable all finished stream */
652 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
654 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
655 if( AVI_PacketNext( p_input ) )
657 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
662 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
669 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
672 "cannot get packet header, track disabled" );
673 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
675 if( avi_pk.i_stream >= p_sys->i_track ||
676 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
678 if( AVI_PacketNext( p_input ) )
681 "cannot skip packet, track disabled" );
682 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
688 /* add this chunk to the index */
689 AVIIndexEntry_t index;
691 index.i_id = avi_pk.i_fourcc;
693 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
695 index.i_pos = avi_pk.i_pos;
696 index.i_length = avi_pk.i_size;
697 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
699 i_track = avi_pk.i_stream;
700 tk = p_sys->track[i_track];
701 /* do we will read this data ? */
702 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
708 if( AVI_PacketNext( p_input ) )
711 "cannot skip packet, track disabled" );
712 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
721 stream_Seek( p_input->s, i_pos );
724 /* Set the track to use */
725 tk = p_sys->track[i_track];
727 /* read thoses data */
728 if( tk->i_samplesize )
730 unsigned int i_toread;
732 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
734 if( tk->i_samplesize > 1 )
736 i_toread = tk->i_samplesize;
740 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
743 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
749 i_size = tk->p_index[tk->i_idxposc].i_length;
752 if( tk->i_idxposb == 0 )
754 i_size += 8; /* need to read and skip header */
757 if( ( p_frame = stream_Block( p_input->s, __EVEN( i_size ) ) )==NULL )
759 msg_Warn( p_input, "failled reading data" );
760 tk->b_activated = VLC_FALSE;
761 toread[i_track].b_ok = VLC_FALSE;
764 if( i_size % 2 ) /* read was padded on word boundary */
769 if( tk->i_idxposb == 0 )
771 p_frame->p_buffer += 8;
772 p_frame->i_buffer -= 8;
774 p_frame->i_pts = AVI_GetPTS( tk );
777 if( tk->i_samplesize )
779 if( tk->i_idxposb == 0 )
783 toread[i_track].i_toread -= i_size;
784 tk->i_idxposb += i_size;
786 tk->p_index[tk->i_idxposc].i_length )
794 int i_length = tk->p_index[tk->i_idxposc].i_length;
797 if( tk->i_cat == AUDIO_ES )
799 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
801 toread[i_track].i_toread--;
804 if( tk->i_idxposc < tk->i_idxnb)
806 toread[i_track].i_posf =
807 tk->p_index[tk->i_idxposc].i_pos;
808 if( tk->i_idxposb > 0 )
810 toread[i_track].i_posf += 8 + tk->i_idxposb;
816 toread[i_track].i_posf = -1;
819 b_stream = VLC_TRUE; /* at least one read succeed */
822 input_ClockGetTS( p_input,
823 p_input->stream.p_selected_program,
824 p_frame->i_pts * 9/100);
826 if( tk->i_cat != VIDEO_ES )
827 p_frame->i_dts = p_frame->i_pts;
830 p_frame->i_dts = p_frame->i_pts;
834 //p_pes->i_rate = p_input->stream.control.i_rate;
835 es_out_Send( p_input->p_es_out, tk->p_es, p_frame );
840 /*****************************************************************************
841 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
842 *****************************************************************************
843 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
844 *****************************************************************************/
845 static int Demux_UnSeekable( input_thread_t *p_input )
847 demux_sys_t *p_sys = p_input->p_demux_data;
848 avi_track_t *p_stream_master = NULL;
850 unsigned int i_stream;
851 unsigned int i_packet;
853 /* Check if we need to send the audio data to decoder */
854 b_audio = !p_input->stream.control.b_mute;
856 input_ClockManageRef( p_input,
857 p_input->stream.p_selected_program,
860 /* *** find master stream for data packet skipping algo *** */
861 /* *** -> first video, if any, or first audio ES *** */
862 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
864 avi_track_t *tk = p_sys->track[i_stream];
867 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
869 if( b && tk->i_cat == VIDEO_ES )
871 p_stream_master = tk;
875 p_stream_master = tk;
879 if( !p_stream_master )
881 msg_Warn( p_input, "no more stream selected" );
885 p_sys->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
887 for( i_packet = 0; i_packet < 10; i_packet++)
889 #define p_stream p_sys->track[avi_pk.i_stream]
893 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
898 if( avi_pk.i_stream >= p_sys->i_track ||
899 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
901 /* we haven't found an audio or video packet:
902 * - we have seek, found first next packet
903 * - others packets could be found, skip them
905 switch( avi_pk.i_fourcc )
910 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
914 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
916 return( 0 ); /* eof */
919 "seems to have lost position, resync" );
920 if( AVI_PacketSearch( p_input ) )
922 msg_Err( p_input, "resync failed" );
929 /* do will send this packet to decoder ? */
930 if( !b_audio && avi_pk.i_cat == AUDIO_ES )
932 if( AVI_PacketNext( p_input ) )
939 /* it's a selected stream, check for time */
940 if( __ABS( AVI_GetPTS( p_stream ) -
941 AVI_GetPTS( p_stream_master ) )< 600*1000 )
943 /* load it and send to decoder */
945 if( AVI_PacketRead( p_input, &avi_pk, &p_frame ) || p_frame == NULL )
950 input_ClockGetTS( p_input,
951 p_input->stream.p_selected_program,
952 AVI_GetPTS( p_stream ) * 9/100);
954 if( avi_pk.i_cat != VIDEO_ES )
955 p_frame->i_dts = p_frame->i_pts;
958 p_frame->i_dts = p_frame->i_pts;
962 //p_pes->i_rate = p_input->stream.control.i_rate;
963 es_out_Send( p_input->p_es_out, p_stream->p_es, p_frame );
967 if( AVI_PacketNext( p_input ) )
974 /* *** update stream time position *** */
975 if( p_stream->i_samplesize )
977 p_stream->i_idxposb += avi_pk.i_size;
981 if( p_stream->i_cat == AUDIO_ES )
983 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
985 p_stream->i_idxposc++;
995 /*****************************************************************************
996 * Seek: goto to i_date or i_percent
997 *****************************************************************************
998 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
999 *****************************************************************************/
1000 static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
1003 demux_sys_t *p_sys = p_input->p_demux_data;
1004 unsigned int i_stream;
1006 "seek requested: "I64Fd" secondes %d%%",
1010 if( p_sys->b_seekable )
1012 if( !p_sys->i_length )
1014 avi_track_t *p_stream;
1017 /* use i_percent to create a true i_date */
1019 "mmh, seeking without index at %d%%"
1020 " work only for interleaved file", i_percent );
1021 if( i_percent >= 100 )
1023 msg_Warn( p_input, "cannot seek so far !" );
1026 i_percent = __MAX( i_percent, 0 );
1028 /* try to find chunk that is at i_percent or the file */
1029 i_pos = __MAX( i_percent *
1030 stream_Size( p_input->s ) / 100,
1031 p_sys->i_movi_begin );
1032 /* search first selected stream */
1033 for( i_stream = 0, p_stream = NULL;
1034 i_stream < p_sys->i_track; i_stream++ )
1036 p_stream = p_sys->track[i_stream];
1037 if( p_stream->b_activated )
1042 if( !p_stream || !p_stream->b_activated )
1044 msg_Warn( p_input, "cannot find any selected stream" );
1048 /* be sure that the index exist */
1049 if( AVI_StreamChunkSet( p_input,
1053 msg_Warn( p_input, "cannot seek" );
1057 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1058 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1060 /* search after i_idxposc */
1061 if( AVI_StreamChunkSet( p_input,
1062 i_stream, p_stream->i_idxposc + 1 ) )
1064 msg_Warn( p_input, "cannot seek" );
1068 i_date = AVI_GetPTS( p_stream );
1069 /* TODO better support for i_samplesize != 0 */
1070 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1073 #define p_stream p_sys->track[i_stream]
1075 /* seek for chunk based streams */
1076 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1078 if( p_stream->b_activated && !p_stream->i_samplesize )
1079 /* if( p_stream->b_activated ) */
1081 AVI_TrackSeek( p_input, i_stream, i_date );
1082 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1089 i_date = p_sys->i_time;
1091 /* seek for bytes based streams */
1092 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1094 if( p_stream->b_activated && p_stream->i_samplesize )
1096 AVI_TrackSeek( p_input, i_stream, i_date );
1097 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1100 msg_Dbg( p_input, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1101 /* set true movie time */
1103 if( !p_sys->i_time )
1105 p_sys->i_time = i_date;
1112 msg_Err( p_input, "shouldn't yet be executed" );
1117 /*****************************************************************************
1119 *****************************************************************************
1121 *****************************************************************************/
1122 static double ControlGetPosition( input_thread_t *p_input )
1124 demux_sys_t *p_sys = p_input->p_demux_data;
1126 if( p_sys->i_length > 0 )
1128 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1130 else if( stream_Size( p_input->s ) > 0 )
1136 /* search the more advanced selected es */
1137 for( i = 0; i < p_sys->i_track; i++ )
1139 avi_track_t *tk = p_sys->track[i];
1140 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1142 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1143 tk->p_index[tk->i_idxposc].i_length + 8;
1150 return (double)i64 / (double)stream_Size( p_input->s );
1155 static int Control( input_thread_t *p_input, int i_query, va_list args )
1157 demux_sys_t *p_sys = p_input->p_demux_data;
1161 vlc_meta_t **pp_meta;
1165 case DEMUX_GET_POSITION:
1166 pf = (double*)va_arg( args, double * );
1167 *pf = ControlGetPosition( p_input );
1169 case DEMUX_SET_POSITION:
1170 if( p_sys->b_seekable )
1172 f = (double)va_arg( args, double );
1173 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1174 return Seek( p_input, i64, (int)(f * 100) ) < 0 ? VLC_EGENERIC : VLC_SUCCESS;
1176 return demux_vaControlDefault( p_input, i_query, args );
1178 case DEMUX_GET_TIME:
1179 pi64 = (int64_t*)va_arg( args, int64_t * );
1180 *pi64 = p_sys->i_time;
1183 case DEMUX_SET_TIME:
1187 i64 = (int64_t)va_arg( args, int64_t );
1188 if( p_sys->i_length > 0 )
1190 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1192 else if( p_sys->i_time > 0 )
1194 i_percent = (int)( 100.0 * ControlGetPosition( p_input ) *
1195 (double)i64 / (double)p_sys->i_time );
1197 return Seek( p_input, i64, i_percent );
1199 case DEMUX_GET_LENGTH:
1200 pi64 = (int64_t*)va_arg( args, int64_t * );
1201 *pi64 = p_sys->i_length * (mtime_t)1000000;
1205 pf = (double*)va_arg( args, double * );
1207 for( i = 0; i < (int)p_sys->i_track; i++ )
1209 avi_track_t *tk = p_sys->track[i];
1210 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1212 *pf = (float)tk->i_rate / (float)tk->i_scale;
1217 case DEMUX_GET_META:
1218 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1219 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1223 return demux_vaControlDefault( p_input, i_query, args );
1225 return VLC_EGENERIC;
1228 /*****************************************************************************
1229 * Function to convert pts to chunk or byte
1230 *****************************************************************************/
1232 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1234 return (mtime_t)((int64_t)i_pts *
1235 (int64_t)tk->i_rate /
1236 (int64_t)tk->i_scale /
1239 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1241 return (mtime_t)((int64_t)i_pts *
1242 (int64_t)tk->i_rate /
1243 (int64_t)tk->i_scale /
1245 (int64_t)tk->i_samplesize );
1248 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1252 i_dpts = (mtime_t)( (int64_t)1000000 *
1254 (int64_t)tk->i_scale /
1255 (int64_t)tk->i_rate );
1257 if( tk->i_samplesize )
1259 return i_dpts / tk->i_samplesize;
1264 static mtime_t AVI_GetPTS( avi_track_t *tk )
1266 if( tk->i_samplesize )
1268 int64_t i_count = 0;
1270 /* we need a valid entry we will emulate one */
1271 if( tk->i_idxposc == tk->i_idxnb )
1275 /* use the last entry */
1276 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1277 + tk->p_index[tk->i_idxnb - 1].i_length;
1282 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1284 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1288 if( tk->i_cat == AUDIO_ES )
1290 return AVI_GetDPTS( tk, tk->i_blockno );
1294 return AVI_GetDPTS( tk, tk->i_idxposc );
1299 static int AVI_StreamChunkFind( input_thread_t *p_input,
1300 unsigned int i_stream )
1302 demux_sys_t *p_sys = p_input->p_demux_data;
1303 avi_packet_t avi_pk;
1305 /* find first chunk of i_stream that isn't in index */
1307 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1309 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
1310 if( AVI_PacketNext( p_input ) )
1312 return VLC_EGENERIC;
1317 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
1322 if( p_input->b_die )
1324 return VLC_EGENERIC;
1327 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1329 msg_Warn( p_input, "cannot get packet header" );
1330 return VLC_EGENERIC;
1332 if( avi_pk.i_stream >= p_sys->i_track ||
1333 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1335 if( AVI_PacketNext( p_input ) )
1337 return VLC_EGENERIC;
1342 /* add this chunk to the index */
1343 AVIIndexEntry_t index;
1345 index.i_id = avi_pk.i_fourcc;
1347 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1349 index.i_pos = avi_pk.i_pos;
1350 index.i_length = avi_pk.i_size;
1351 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1353 if( avi_pk.i_stream == i_stream )
1358 if( AVI_PacketNext( p_input ) )
1360 return VLC_EGENERIC;
1367 /* be sure that i_ck will be a valid index entry */
1368 static int AVI_StreamChunkSet( input_thread_t *p_input,
1369 unsigned int i_stream,
1372 demux_sys_t *p_sys = p_input->p_demux_data;
1373 avi_track_t *p_stream = p_sys->track[i_stream];
1375 p_stream->i_idxposc = i_ck;
1376 p_stream->i_idxposb = 0;
1378 if( i_ck >= p_stream->i_idxnb )
1380 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1383 p_stream->i_idxposc++;
1384 if( AVI_StreamChunkFind( p_input, i_stream ) )
1386 return VLC_EGENERIC;
1389 } while( p_stream->i_idxposc < i_ck );
1396 /* XXX FIXME up to now, we assume that all chunk are one after one */
1397 static int AVI_StreamBytesSet( input_thread_t *p_input,
1398 unsigned int i_stream,
1401 demux_sys_t *p_sys = p_input->p_demux_data;
1402 avi_track_t *p_stream = p_sys->track[i_stream];
1404 if( ( p_stream->i_idxnb > 0 )
1405 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1406 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1408 /* index is valid to find the ck */
1409 /* uses dichototmie to be fast enougth */
1410 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1411 int i_idxmax = p_stream->i_idxnb;
1415 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1417 i_idxmax = i_idxposc ;
1418 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1422 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1423 p_stream->p_index[i_idxposc].i_length <= i_byte)
1425 i_idxmin = i_idxposc ;
1426 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1430 p_stream->i_idxposc = i_idxposc;
1431 p_stream->i_idxposb = i_byte -
1432 p_stream->p_index[i_idxposc].i_lengthtotal;
1441 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1442 p_stream->i_idxposb = 0;
1445 p_stream->i_idxposc++;
1446 if( AVI_StreamChunkFind( p_input, i_stream ) )
1448 return VLC_EGENERIC;
1451 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1452 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1454 p_stream->i_idxposb = i_byte -
1455 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1460 static int AVI_TrackSeek( input_thread_t *p_input,
1464 demux_sys_t *p_sys = p_input->p_demux_data;
1465 avi_track_t *tk = p_sys->track[i_stream];
1467 #define p_stream p_sys->track[i_stream]
1470 i_oldpts = AVI_GetPTS( p_stream );
1472 if( !p_stream->i_samplesize )
1474 if( AVI_StreamChunkSet( p_input,
1476 AVI_PTSToChunk( p_stream, i_date ) ) )
1478 return VLC_EGENERIC;
1481 if( p_stream->i_cat == AUDIO_ES )
1485 for( i = 0; i < tk->i_idxposc; i++ )
1487 if( tk->i_blocksize > 0 )
1489 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1499 "old:"I64Fd" %s new "I64Fd,
1501 i_oldpts > i_date ? ">" : "<",
1504 if( p_stream->i_cat == VIDEO_ES )
1506 /* search key frame */
1507 if( i_date < i_oldpts )
1509 while( p_stream->i_idxposc > 0 &&
1510 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1513 if( AVI_StreamChunkSet( p_input,
1515 p_stream->i_idxposc - 1 ) )
1517 return VLC_EGENERIC;
1523 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1524 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1527 if( AVI_StreamChunkSet( p_input,
1529 p_stream->i_idxposc + 1 ) )
1531 return VLC_EGENERIC;
1539 if( AVI_StreamBytesSet( p_input,
1541 AVI_PTSToByte( p_stream, i_date ) ) )
1543 return VLC_EGENERIC;
1550 /****************************************************************************
1551 * Return VLC_TRUE if it's a key frame
1552 ****************************************************************************/
1553 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1559 * startcode: 0x00000100 32bits
1560 * framenumber ? 5bits
1561 * piture type 0(I),1(P) 2bits
1563 if( GetDWBE( p_byte ) != 0x00000100 )
1565 /* it's not an msmpegv1 stream, strange...*/
1566 return AVIIF_KEYFRAME;
1570 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1573 case FOURCC_DIV3: /* wmv1 also */
1575 * picture type 0(I),1(P) 2bits
1577 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1579 /* we should find first occurence of 0x000001b6 (32bits)
1580 * startcode: 0x000001b6 32bits
1581 * piture type 0(I),1(P) 2bits
1583 if( GetDWBE( p_byte ) != 0x000001b6 )
1585 /* not true , need to find the first VOP header */
1586 return AVIIF_KEYFRAME;
1590 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1593 /* I can't do it, so say yes */
1594 return AVIIF_KEYFRAME;
1598 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1603 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1607 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1611 return VLC_FOURCC('m','r','l','e');
1660 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1664 /****************************************************************************
1666 ****************************************************************************/
1667 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1668 int *pi_number, int *pi_type )
1670 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1673 c1 = ((uint8_t *)&i_id)[0];
1674 c2 = ((uint8_t *)&i_id)[1];
1676 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1678 SET_PTR( pi_number, 100 ); /* > max stream number */
1679 SET_PTR( pi_type, UNKNOWN_ES );
1683 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1684 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1687 SET_PTR( pi_type, AUDIO_ES );
1691 SET_PTR( pi_type, VIDEO_ES );
1694 SET_PTR( pi_type, UNKNOWN_ES );
1701 /****************************************************************************
1703 ****************************************************************************/
1704 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
1708 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1710 return VLC_EGENERIC;
1712 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1713 p_pk->i_size = GetDWLE( p_peek + 4 );
1714 p_pk->i_pos = stream_Tell( p_input->s );
1715 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1717 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1718 p_peek[10], p_peek[11] );
1725 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1727 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1731 static int AVI_PacketNext( input_thread_t *p_input )
1733 avi_packet_t avi_ck;
1736 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
1738 return VLC_EGENERIC;
1741 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1742 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1746 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1747 avi_ck.i_type == AVIFOURCC_AVIX )
1753 i_skip = __EVEN( avi_ck.i_size ) + 8;
1756 if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
1758 return VLC_EGENERIC;
1762 static int AVI_PacketRead( input_thread_t *p_input,
1764 block_t **pp_frame )
1768 i_size = __EVEN( p_pk->i_size + 8 );
1770 if( ( *pp_frame = stream_Block( p_input->s, i_size ) ) == NULL )
1772 return VLC_EGENERIC;
1774 (*pp_frame)->p_buffer += 8;
1775 (*pp_frame)->i_buffer -= 8;
1777 if( i_size != p_pk->i_size + 8 )
1779 (*pp_frame)->i_buffer--;
1785 static int AVI_PacketSearch( input_thread_t *p_input )
1787 demux_sys_t *p_sys = p_input->p_demux_data;
1788 avi_packet_t avi_pk;
1793 if( stream_Read( p_input->s, NULL, 1 ) != 1 )
1795 return VLC_EGENERIC;
1797 AVI_PacketGetHeader( p_input, &avi_pk );
1798 if( avi_pk.i_stream < p_sys->i_track &&
1799 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1803 switch( avi_pk.i_fourcc )
1805 case AVIFOURCC_JUNK:
1806 case AVIFOURCC_LIST:
1807 case AVIFOURCC_RIFF:
1808 case AVIFOURCC_idx1:
1812 /* Prevents from eating all the CPU with broken files.
1813 * This value should be low enough so that it doesn't affect the
1814 * reading speed too much (not that we care much anyway because
1815 * this code is called only on broken files). */
1816 if( !(++i_count % 1024) )
1818 if( p_input->b_die ) return VLC_EGENERIC;
1821 if( !(i_count % (1024 * 10)) )
1822 msg_Warn( p_input, "trying to resync..." );
1827 /****************************************************************************
1829 ****************************************************************************/
1830 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1832 AVIIndexEntry_t *p_index)
1834 avi_track_t *tk = p_sys->track[i_stream];
1836 /* Update i_movi_lastchunk_pos */
1837 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1839 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1843 if( tk->i_idxnb >= tk->i_idxmax )
1845 tk->i_idxmax += 16384;
1846 tk->p_index = realloc( tk->p_index,
1847 tk->i_idxmax * sizeof( AVIIndexEntry_t ) );
1848 if( tk->p_index == NULL )
1853 /* calculate cumulate length */
1854 if( tk->i_idxnb > 0 )
1856 p_index->i_lengthtotal =
1857 tk->p_index[tk->i_idxnb - 1].i_length +
1858 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1862 p_index->i_lengthtotal = 0;
1865 tk->p_index[tk->i_idxnb++] = *p_index;
1868 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
1870 demux_sys_t *p_sys = p_input->p_demux_data;
1872 avi_chunk_list_t *p_riff;
1873 avi_chunk_list_t *p_movi;
1874 avi_chunk_idx1_t *p_idx1;
1876 unsigned int i_stream;
1877 unsigned int i_index;
1881 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1882 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1883 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1887 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
1888 return VLC_EGENERIC;
1891 /* *** calculate offset *** */
1892 /* Well, avi is __SHIT__ so test more than one entry
1893 * (needed for some avi files) */
1895 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1897 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1899 i_offset = p_movi->i_chunk_pos + 8;
1904 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
1908 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
1911 if( i_stream < p_sys->i_track &&
1912 i_cat == p_sys->track[i_stream]->i_cat )
1914 AVIIndexEntry_t index;
1915 index.i_id = p_idx1->entry[i_index].i_fourcc;
1917 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
1918 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
1919 index.i_length = p_idx1->entry[i_index].i_length;
1920 AVI_IndexAddEntry( p_sys, i_stream, &index );
1926 static void __Parse_indx( input_thread_t *p_input,
1928 avi_chunk_indx_t *p_indx )
1930 demux_sys_t *p_sys = p_input->p_demux_data;
1931 AVIIndexEntry_t index;
1934 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
1935 if( p_indx->i_indexsubtype == 0 )
1937 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1939 index.i_id = p_indx->i_id;
1940 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1941 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
1942 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
1944 AVI_IndexAddEntry( p_sys, i_stream, &index );
1947 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
1949 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1951 index.i_id = p_indx->i_id;
1952 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1953 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
1954 index.i_length = p_indx->idx.field[i].i_size;
1956 AVI_IndexAddEntry( p_sys, i_stream, &index );
1961 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
1965 static void AVI_IndexLoad_indx( input_thread_t *p_input )
1967 demux_sys_t *p_sys = p_input->p_demux_data;
1968 unsigned int i_stream;
1971 avi_chunk_list_t *p_riff;
1972 avi_chunk_list_t *p_hdrl;
1974 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1975 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
1977 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1979 avi_chunk_list_t *p_strl;
1980 avi_chunk_indx_t *p_indx;
1982 #define p_stream p_sys->track[i_stream]
1983 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
1984 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
1988 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
1992 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
1994 __Parse_indx( p_input, i_stream, p_indx );
1996 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
1998 avi_chunk_indx_t ck_sub;
1999 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2001 if( stream_Seek( p_input->s, p_indx->idx.super[i].i_offset )||
2002 AVI_ChunkRead( p_input->s, &ck_sub, NULL ) )
2006 __Parse_indx( p_input, i_stream, &ck_sub );
2011 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
2017 static void AVI_IndexLoad( input_thread_t *p_input )
2019 demux_sys_t *p_sys = p_input->p_demux_data;
2020 unsigned int i_stream;
2022 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2024 p_sys->track[i_stream]->i_idxnb = 0;
2025 p_sys->track[i_stream]->i_idxmax = 0;
2026 p_sys->track[i_stream]->p_index = NULL;
2031 AVI_IndexLoad_indx( p_input );
2033 else if( AVI_IndexLoad_idx1( p_input ) )
2035 /* try indx if idx1 failed as some "normal" file have indx too */
2036 AVI_IndexLoad_indx( p_input );
2039 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2041 msg_Dbg( p_input, "stream[%d] created %d index entries",
2042 i_stream, p_sys->track[i_stream]->i_idxnb );
2046 static void AVI_IndexCreate( input_thread_t *p_input )
2048 demux_sys_t *p_sys = p_input->p_demux_data;
2050 avi_chunk_list_t *p_riff;
2051 avi_chunk_list_t *p_movi;
2053 unsigned int i_stream;
2056 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2057 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2061 msg_Err( p_input, "cannot find p_movi" );
2065 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2067 p_sys->track[i_stream]->i_idxnb = 0;
2068 p_sys->track[i_stream]->i_idxmax = 0;
2069 p_sys->track[i_stream]->p_index = NULL;
2071 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2072 stream_Size( p_input->s ) );
2074 stream_Seek( p_input->s, p_movi->i_chunk_pos + 12 );
2075 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
2080 if( p_input->b_die )
2085 if( AVI_PacketGetHeader( p_input, &pk ) )
2089 if( pk.i_stream < p_sys->i_track &&
2090 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2092 AVIIndexEntry_t index;
2093 index.i_id = pk.i_fourcc;
2095 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2096 index.i_pos = pk.i_pos;
2097 index.i_length = pk.i_size;
2098 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2102 switch( pk.i_fourcc )
2104 case AVIFOURCC_idx1:
2107 avi_chunk_list_t *p_sysx;
2108 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2109 AVIFOURCC_RIFF, 1 );
2111 msg_Dbg( p_input, "looking for new RIFF chunk" );
2112 if( stream_Seek( p_input->s, p_sysx->i_chunk_pos + 24))
2119 case AVIFOURCC_RIFF:
2120 msg_Dbg( p_input, "new RIFF chunk found" );
2122 case AVIFOURCC_JUNK:
2125 msg_Warn( p_input, "need resync, probably broken avi" );
2126 if( AVI_PacketSearch( p_input ) )
2128 msg_Warn( p_input, "lost sync, abord index creation" );
2134 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2135 AVI_PacketNext( p_input ) )
2142 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2145 "stream[%d] creating %d index entries",
2147 p_sys->track[i_stream]->i_idxnb );
2151 /*****************************************************************************
2153 *****************************************************************************/
2154 static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
2156 demux_sys_t *p_sys = p_input->p_demux_data;
2158 int b_end = VLC_TRUE;
2160 for( i = 0; i < p_sys->i_track; i++ )
2162 avi_track_t *tk = p_sys->track[i];
2163 if( tk->i_idxposc >= tk->i_idxnb )
2165 tk->b_activated = VLC_FALSE;
2166 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2176 /****************************************************************************
2177 * AVI_MovieGetLength give max streams length in second
2178 ****************************************************************************/
2179 static mtime_t AVI_MovieGetLength( input_thread_t *p_input )
2181 demux_sys_t *p_sys = p_input->p_demux_data;
2182 mtime_t i_maxlength = 0;
2185 for( i = 0; i < p_sys->i_track; i++ )
2187 avi_track_t *tk = p_sys->track[i];
2190 /* fix length for each stream */
2191 if( tk->i_idxnb < 1 || !tk->p_index )
2196 if( tk->i_samplesize )
2198 i_length = AVI_GetDPTS( tk,
2199 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2200 tk->p_index[tk->i_idxnb-1].i_length );
2204 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2206 i_length /= (mtime_t)1000000; /* in seconds */
2209 "stream[%d] length:"I64Fd" (based on index)",
2212 i_maxlength = __MAX( i_maxlength, i_length );