1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.76 2003/11/24 13:40:03 gbazin 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>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 add_category_hint( N_("avi-demuxer"), NULL, VLC_TRUE );
43 add_bool( "avi-interleaved", 0, NULL,
44 N_("force interleaved method"),
45 N_("force interleaved method"), VLC_TRUE );
46 add_bool( "avi-index", 0, NULL,
47 N_("force index creation"),
48 N_("force index creation"), VLC_TRUE );
50 set_description( N_("AVI demuxer") );
51 set_capability( "demux", 212 );
52 set_callbacks( Open, Close );
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Control ( input_thread_t *, int, va_list );
59 static int Seek ( input_thread_t *, mtime_t, int );
60 static int Demux_Seekable ( input_thread_t * );
61 static int Demux_UnSeekable( input_thread_t *p_input );
63 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
64 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
66 static inline off_t __EVEN( off_t i )
68 return (i & 1) ? i + 1 : i;
71 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
72 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
73 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
74 static mtime_t AVI_GetPTS ( avi_track_t * );
77 static int AVI_StreamChunkFind( input_thread_t *, unsigned int i_stream );
78 static int AVI_StreamChunkSet ( input_thread_t *,
79 unsigned int i_stream, unsigned int i_ck );
80 static int AVI_StreamBytesSet ( input_thread_t *,
81 unsigned int i_stream, off_t i_byte );
83 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
84 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
86 static int AVI_PacketGetHeader( input_thread_t *, avi_packet_t *p_pk );
87 static int AVI_PacketNext ( input_thread_t * );
88 static int AVI_PacketRead ( input_thread_t *, avi_packet_t *, block_t **);
89 static int AVI_PacketSearch ( input_thread_t * );
91 static void AVI_IndexLoad ( input_thread_t * );
92 static void AVI_IndexCreate ( input_thread_t * );
93 static void AVI_IndexAddEntry( demux_sys_t *, int, AVIIndexEntry_t * );
95 static mtime_t AVI_MovieGetLength( input_thread_t * );
97 /*****************************************************************************
99 *****************************************************************************/
100 static int AVI_TrackSeek ( input_thread_t *, int, mtime_t );
101 static int AVI_TrackStopFinishedStreams( input_thread_t *);
103 /*****************************************************************************
104 * Open: check file and initializes AVI structures
105 *****************************************************************************/
106 static int Open( vlc_object_t * p_this )
108 input_thread_t *p_input = (input_thread_t *)p_this;
112 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
113 avi_chunk_list_t *p_hdrl, *p_movi;
114 avi_chunk_avih_t *p_avih;
116 unsigned int i_track;
122 /* Is it an avi file ? */
123 if( stream_Peek( p_input->s, &p_peek, 12 ) < 12 )
125 msg_Err( p_input, "cannot peek()" );
128 if( strncmp( &p_peek[0], "RIFF", 4 ) || strncmp( &p_peek[8], "AVI ", 4 ) )
130 msg_Warn( p_input, "avi module discarded (invalid header)" );
134 /* Initialize input structures. */
135 p_sys = p_input->p_demux_data = malloc( sizeof(demux_sys_t) );
136 memset( p_sys, 0, sizeof( demux_sys_t ) );
140 p_sys->i_movi_lastchunk_pos = 0;
141 p_sys->b_odml = VLC_FALSE;
145 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
147 p_input->pf_demux_control = Control;
148 p_input->pf_demux = Demux_Seekable;
149 /* For unseekable stream, automaticaly use Demux_UnSeekable */
150 if( !p_sys->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
152 p_input->pf_demux = Demux_UnSeekable;
155 if( AVI_ChunkReadRoot( p_input->s, &p_sys->ck_root ) )
157 msg_Err( p_input, "avi module discarded (invalid file)" );
161 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
163 unsigned int i_count =
164 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
166 msg_Warn( p_input, "multiple riff -> OpenDML ?" );
167 for( i = 1; i < i_count; i++ )
169 avi_chunk_list_t *p_sysx;
171 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
172 if( p_sysx->i_type == AVIFOURCC_AVIX )
174 msg_Warn( p_input, "detected OpenDML file" );
175 p_sys->b_odml = VLC_TRUE;
181 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
182 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
183 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
185 if( !p_hdrl || !p_movi )
187 msg_Err( p_input, "avi module discarded (invalid file)" );
191 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
193 msg_Err( p_input, "cannot find avih chunk" );
196 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
197 if( p_avih->i_streams != i_track )
200 "found %d stream but %d are declared",
201 i_track, p_avih->i_streams );
205 msg_Err( p_input, "no stream defined!" );
209 /* create one program */
210 vlc_mutex_lock( &p_input->stream.stream_lock );
211 if( input_InitStream( p_input, 0 ) == -1)
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
214 msg_Err( p_input, "cannot init stream" );
217 p_input->stream.i_mux_rate = 0; /* Fixed later */
218 vlc_mutex_unlock( &p_input->stream.stream_lock );
220 /* print informations on streams */
221 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
223 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
224 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
225 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
226 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
228 input_info_category_t *p_cat = input_InfoCategory( p_input, _("Avi") );
229 input_AddInfo( p_cat, _("Number of Streams"), "%d", i_track );
230 input_AddInfo( p_cat, _("Flags"), "%s%s%s%s",
231 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
232 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
233 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
234 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
237 /* now read info on each stream and create ES */
238 for( i = 0 ; i < i_track; i++ )
240 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
241 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
242 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
243 avi_chunk_strf_auds_t *p_auds;
244 avi_chunk_strf_vids_t *p_vids;
247 tk->b_activated = VLC_FALSE;
254 p_auds = (void*)p_vids = (void*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
256 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
258 msg_Warn( p_input, "stream[%d] incomplete", i );
262 tk->i_rate = p_strh->i_rate;
263 tk->i_scale = p_strh->i_scale;
264 tk->i_samplesize = p_strh->i_samplesize;
265 msg_Dbg( p_input, "stream[%d] rate:%d scale:%d samplesize:%d",
266 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
268 switch( p_strh->i_type )
270 case( AVIFOURCC_auds ):
271 tk->i_cat = AUDIO_ES;
272 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
273 p_auds->p_wf->wFormatTag );
274 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
276 fmt.audio.i_channels = p_auds->p_wf->nChannels;
277 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
278 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
279 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
280 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
281 if( ( fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
282 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) ) ) > 0 )
284 fmt.p_extra = malloc( fmt.i_extra );
285 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
287 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
289 p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
290 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
293 case( AVIFOURCC_vids ):
294 tk->i_cat = VIDEO_ES;
295 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
296 p_vids->p_bih->biCompression );
297 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
298 tk->i_samplesize = 0;
299 fmt.video.i_width = p_vids->p_bih->biWidth;
300 fmt.video.i_height = p_vids->p_bih->biHeight;
301 if( ( fmt.i_extra = __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
302 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) ) ) > 0 )
304 fmt.p_extra = malloc( fmt.i_extra );
305 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
307 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
309 (char*)&p_vids->p_bih->biCompression,
310 p_vids->p_bih->biWidth,
311 p_vids->p_bih->biHeight,
312 p_vids->p_bih->biBitCount,
313 (float)tk->i_rate/(float)tk->i_scale );
316 msg_Warn( p_input, "stream[%d] unknown type", i );
320 tk->p_es = es_out_Add( p_input->p_es_out, &fmt );
321 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
324 if( p_sys->i_track <= 0 )
326 msg_Err( p_input, "No valid track" );
330 if( config_GetInt( p_input, "avi-index" ) )
332 if( p_sys->b_seekable )
334 AVI_IndexCreate( p_input );
338 msg_Warn( p_input, "cannot create index (unseekable stream)" );
339 AVI_IndexLoad( p_input );
344 AVI_IndexLoad( p_input );
347 /* *** movie length in sec *** */
348 p_sys->i_length = AVI_MovieGetLength( p_input );
349 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
350 (mtime_t)p_avih->i_microsecperframe /
353 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
355 /* fix some BeOS MediaKit generated file */
356 for( i = 0 ; i < p_sys->i_track; i++ )
358 avi_track_t *tk = p_sys->track[i];
359 avi_chunk_list_t *p_strl;
360 avi_chunk_strh_t *p_strh;
361 avi_chunk_strf_auds_t *p_auds;
363 if( tk->i_cat != AUDIO_ES )
367 if( tk->i_idxnb < 1 ||
369 tk->i_samplesize != 0 )
373 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
374 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
375 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
377 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
378 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
380 int64_t i_track_length =
381 tk->p_index[tk->i_idxnb-1].i_length +
382 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
383 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
384 (mtime_t)p_avih->i_microsecperframe;
388 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
391 tk->i_samplesize = 1;
392 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
393 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
397 if( p_sys->i_length )
399 p_input->stream.i_mux_rate =
400 stream_Size( p_input->s ) / 50 / p_sys->i_length;
403 if( p_sys->b_seekable )
405 /* we have read all chunk so go back to movi */
406 stream_Seek( p_input->s, p_movi->i_chunk_pos );
408 /* Skip movi header */
409 stream_Read( p_input->s, NULL, 12 );
411 p_sys->i_movi_begin = p_movi->i_chunk_pos;
415 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
420 /*****************************************************************************
421 * Close: frees unused data
422 *****************************************************************************/
423 static void Close ( vlc_object_t * p_this )
425 input_thread_t * p_input = (input_thread_t *)p_this;
427 demux_sys_t *p_sys = p_input->p_demux_data ;
429 for( i = 0; i < p_sys->i_track; i++ )
431 if( p_sys->track[i] )
433 FREE( p_sys->track[i]->p_index );
434 free( p_sys->track[i] );
437 FREE( p_sys->track );
438 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
443 /*****************************************************************************
444 * Demux_Seekable: reads and demuxes data packets for stream seekable
445 *****************************************************************************
446 * AVIDemux: reads and demuxes data packets
447 *****************************************************************************
448 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
449 *****************************************************************************/
456 off_t i_posf; /* where we will read :
457 if i_idxposb == 0 : begining of chunk (+8 to acces data)
458 else : point on data directly */
459 } avi_track_toread_t;
461 static int Demux_Seekable( input_thread_t *p_input )
463 demux_sys_t *p_sys = p_input->p_demux_data;
465 unsigned int i_track_count = 0;
466 unsigned int i_track;
468 /* cannot be more than 100 stream (dcXX or wbXX) */
469 avi_track_toread_t toread[100];
472 /* detect new selected/unselected streams */
473 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
475 avi_track_t *tk = p_sys->track[i_track];
478 es_out_Control( p_input->p_es_out, ES_OUT_GET_SELECT, tk->p_es, &b );
479 if( b && !tk->b_activated )
481 if( p_sys->b_seekable)
483 AVI_TrackSeek( p_input, i_track, p_sys->i_time );
485 tk->b_activated = VLC_TRUE;
487 else if( !b && tk->b_activated )
489 tk->b_activated = VLC_FALSE;
497 if( i_track_count <= 0 )
499 msg_Warn( p_input, "no track selected, exiting..." );
503 /* wait for the good time */
504 p_sys->i_pcr = p_sys->i_time * 9 / 100;
506 input_ClockManageRef( p_input,
507 p_input->stream.p_selected_program,
511 p_sys->i_time += 25*1000; /* read 25ms */
514 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
516 avi_track_t *tk = p_sys->track[i_track];
519 toread[i_track].b_ok = tk->b_activated;
520 if( tk->i_idxposc < tk->i_idxnb )
522 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
523 if( tk->i_idxposb > 0 )
525 toread[i_track].i_posf += 8 + tk->i_idxposb;
530 toread[i_track].i_posf = -1;
533 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
535 if( tk->i_samplesize )
537 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
541 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
546 toread[i_track].i_toread *= -1;
550 b_stream = VLC_FALSE;
561 /* search for first chunk to be read */
562 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
564 if( !toread[i].b_ok ||
565 AVI_GetDPTS( p_sys->track[i],
566 toread[i].i_toread ) <= -25 * 1000 )
571 if( toread[i].i_toread > 0 )
573 b_done = VLC_FALSE; /* not yet finished */
575 if( toread[i].i_posf > 0 )
577 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
580 i_pos = toread[i].i_posf;
592 /* no valid index, we will parse directly the stream
593 * in case we fail we will disable all finished stream */
594 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
596 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
597 if( AVI_PacketNext( p_input ) )
599 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
604 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
611 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
614 "cannot get packet header, track disabled" );
615 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
617 if( avi_pk.i_stream >= p_sys->i_track ||
618 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
620 if( AVI_PacketNext( p_input ) )
623 "cannot skip packet, track disabled" );
624 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
630 /* add this chunk to the index */
631 AVIIndexEntry_t index;
633 index.i_id = avi_pk.i_fourcc;
635 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
637 index.i_pos = avi_pk.i_pos;
638 index.i_length = avi_pk.i_size;
639 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
641 i_track = avi_pk.i_stream;
642 tk = p_sys->track[i_track];
643 /* do we will read this data ? */
644 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
650 if( AVI_PacketNext( p_input ) )
653 "cannot skip packet, track disabled" );
654 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
663 stream_Seek( p_input->s, i_pos );
666 /* Set the track to use */
667 tk = p_sys->track[i_track];
669 /* read thoses data */
670 if( tk->i_samplesize )
672 unsigned int i_toread;
674 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
676 if( tk->i_samplesize > 1 )
678 i_toread = tk->i_samplesize;
682 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
685 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
691 i_size = tk->p_index[tk->i_idxposc].i_length;
694 if( tk->i_idxposb == 0 )
696 i_size += 8; /* need to read and skip header */
699 if( ( p_frame = stream_Block( p_input->s, __EVEN( i_size ) ) )==NULL )
701 msg_Warn( p_input, "failled reading data" );
702 tk->b_activated = VLC_FALSE;
703 toread[i_track].b_ok = VLC_FALSE;
706 if( i_size % 2 ) /* read was padded on word boundary */
711 if( tk->i_idxposb == 0 )
713 p_frame->p_buffer += 8;
714 p_frame->i_buffer -= 8;
716 p_frame->i_pts = AVI_GetPTS( tk );
719 if( tk->i_samplesize )
721 if( tk->i_idxposb == 0 )
725 toread[i_track].i_toread -= i_size;
726 tk->i_idxposb += i_size;
728 tk->p_index[tk->i_idxposc].i_length )
736 toread[i_track].i_toread--;
740 if( tk->i_idxposc < tk->i_idxnb)
742 toread[i_track].i_posf =
743 tk->p_index[tk->i_idxposc].i_pos;
744 if( tk->i_idxposb > 0 )
746 toread[i_track].i_posf += 8 + tk->i_idxposb;
752 toread[i_track].i_posf = -1;
755 b_stream = VLC_TRUE; /* at least one read succeed */
758 input_ClockGetTS( p_input,
759 p_input->stream.p_selected_program,
760 p_frame->i_pts * 9/100);
762 if( tk->i_cat != VIDEO_ES )
763 p_frame->i_dts = p_frame->i_pts;
766 p_frame->i_dts = p_frame->i_pts;
770 //p_pes->i_rate = p_input->stream.control.i_rate;
771 es_out_Send( p_input->p_es_out, tk->p_es, p_frame );
776 /*****************************************************************************
777 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
778 *****************************************************************************
779 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
780 *****************************************************************************/
781 static int Demux_UnSeekable( input_thread_t *p_input )
783 demux_sys_t *p_sys = p_input->p_demux_data;
784 avi_track_t *p_stream_master = NULL;
786 unsigned int i_stream;
787 unsigned int i_packet;
789 /* Check if we need to send the audio data to decoder */
790 b_audio = !p_input->stream.control.b_mute;
792 input_ClockManageRef( p_input,
793 p_input->stream.p_selected_program,
796 /* *** find master stream for data packet skipping algo *** */
797 /* *** -> first video, if any, or first audio ES *** */
798 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
800 avi_track_t *tk = p_sys->track[i_stream];
803 es_out_Control( p_input->p_es_out, ES_OUT_GET_SELECT, tk->p_es, &b );
805 if( b && tk->i_cat == VIDEO_ES )
807 p_stream_master = tk;
811 p_stream_master = tk;
815 if( !p_stream_master )
817 msg_Warn( p_input, "no more stream selected" );
821 p_sys->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
823 for( i_packet = 0; i_packet < 10; i_packet++)
825 #define p_stream p_sys->track[avi_pk.i_stream]
829 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
834 if( avi_pk.i_stream >= p_sys->i_track ||
835 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
837 /* we haven't found an audio or video packet:
838 * - we have seek, found first next packet
839 * - others packets could be found, skip them
841 switch( avi_pk.i_fourcc )
846 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
850 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
852 return( 0 ); /* eof */
855 "seems to have lost position, resync" );
856 if( AVI_PacketSearch( p_input ) )
858 msg_Err( p_input, "resync failed" );
865 /* do will send this packet to decoder ? */
866 if( !b_audio && avi_pk.i_cat == AUDIO_ES )
868 if( AVI_PacketNext( p_input ) )
875 /* it's a selected stream, check for time */
876 if( __ABS( AVI_GetPTS( p_stream ) -
877 AVI_GetPTS( p_stream_master ) )< 600*1000 )
879 /* load it and send to decoder */
881 if( AVI_PacketRead( p_input, &avi_pk, &p_frame ) || p_frame == NULL )
886 input_ClockGetTS( p_input,
887 p_input->stream.p_selected_program,
888 AVI_GetPTS( p_stream ) * 9/100);
890 if( avi_pk.i_cat != VIDEO_ES )
891 p_frame->i_dts = p_frame->i_pts;
894 p_frame->i_dts = p_frame->i_pts;
898 //p_pes->i_rate = p_input->stream.control.i_rate;
899 es_out_Send( p_input->p_es_out, p_stream->p_es, p_frame );
903 if( AVI_PacketNext( p_input ) )
910 /* *** update stream time position *** */
911 if( p_stream->i_samplesize )
913 p_stream->i_idxposb += avi_pk.i_size;
917 p_stream->i_idxposc++;
927 /*****************************************************************************
928 * Seek: goto to i_date or i_percent
929 *****************************************************************************
930 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
931 *****************************************************************************/
932 static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
935 demux_sys_t *p_sys = p_input->p_demux_data;
936 unsigned int i_stream;
938 "seek requested: "I64Fd" secondes %d%%",
942 if( p_sys->b_seekable )
944 if( !p_sys->i_length )
946 avi_track_t *p_stream;
949 /* use i_percent to create a true i_date */
951 "mmh, seeking without index at %d%%"
952 " work only for interleaved file", i_percent );
953 if( i_percent >= 100 )
955 msg_Warn( p_input, "cannot seek so far !" );
958 i_percent = __MAX( i_percent, 0 );
960 /* try to find chunk that is at i_percent or the file */
961 i_pos = __MAX( i_percent *
962 stream_Size( p_input->s ) / 100,
963 p_sys->i_movi_begin );
964 /* search first selected stream */
965 for( i_stream = 0, p_stream = NULL;
966 i_stream < p_sys->i_track; i_stream++ )
968 p_stream = p_sys->track[i_stream];
969 if( p_stream->b_activated )
974 if( !p_stream || !p_stream->b_activated )
976 msg_Warn( p_input, "cannot find any selected stream" );
980 /* be sure that the index exist */
981 if( AVI_StreamChunkSet( p_input,
985 msg_Warn( p_input, "cannot seek" );
989 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
990 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
992 /* search after i_idxposc */
993 if( AVI_StreamChunkSet( p_input,
994 i_stream, p_stream->i_idxposc + 1 ) )
996 msg_Warn( p_input, "cannot seek" );
1000 i_date = AVI_GetPTS( p_stream );
1001 /* TODO better support for i_samplesize != 0 */
1002 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1005 #define p_stream p_sys->track[i_stream]
1007 /* seek for chunk based streams */
1008 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1010 if( p_stream->b_activated && !p_stream->i_samplesize )
1011 /* if( p_stream->b_activated ) */
1013 AVI_TrackSeek( p_input, i_stream, i_date );
1014 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1021 i_date = p_sys->i_time;
1023 /* seek for bytes based streams */
1024 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1026 if( p_stream->b_activated && p_stream->i_samplesize )
1028 AVI_TrackSeek( p_input, i_stream, i_date );
1029 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1032 msg_Dbg( p_input, "seek: "I64Fd" secondes", p_sys->i_time /1000000 );
1033 /* set true movie time */
1035 if( !p_sys->i_time )
1037 p_sys->i_time = i_date;
1044 msg_Err( p_input, "shouldn't yet be executed" );
1049 /*****************************************************************************
1051 *****************************************************************************
1053 *****************************************************************************/
1054 static double ControlGetPosition( input_thread_t *p_input )
1056 demux_sys_t *p_sys = p_input->p_demux_data;
1058 if( p_sys->i_length > 0 )
1060 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1062 else if( stream_Size( p_input->s ) > 0 )
1068 /* search the more advanced selected es */
1069 for( i = 0; i < p_sys->i_track; i++ )
1071 avi_track_t *tk = p_sys->track[i];
1072 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1074 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1075 tk->p_index[tk->i_idxposc].i_length + 8;
1082 return (double)i64 / (double)stream_Size( p_input->s );
1087 static int Control( input_thread_t *p_input, int i_query, va_list args )
1089 demux_sys_t *p_sys = p_input->p_demux_data;
1096 case DEMUX_GET_POSITION:
1097 pf = (double*)va_arg( args, double * );
1098 *pf = ControlGetPosition( p_input );
1100 case DEMUX_SET_POSITION:
1101 if( p_sys->b_seekable )
1103 f = (double)va_arg( args, double );
1104 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1105 return Seek( p_input, i64, (int)(f * 100) );
1107 return demux_vaControlDefault( p_input, i_query, args );
1109 case DEMUX_GET_TIME:
1110 pi64 = (int64_t*)va_arg( args, int64_t * );
1111 *pi64 = p_sys->i_time;
1114 case DEMUX_SET_TIME:
1118 i64 = (int64_t)va_arg( args, int64_t );
1119 if( p_sys->i_length > 0 )
1121 i_percent = 100 * i64 / (p_sys->i_length*1000000ULL);
1123 else if( p_sys->i_time > 0 )
1125 i_percent = (int)( 100.0 * ControlGetPosition( p_input ) *
1126 (double)i64 / (double)p_sys->i_time );
1128 return Seek( p_input, i64, i_percent );
1130 case DEMUX_GET_LENGTH:
1131 pi64 = (int64_t*)va_arg( args, int64_t * );
1132 *pi64 = p_sys->i_length * (mtime_t)1000000;
1136 pf = (double*)va_arg( args, double * );
1138 for( i = 0; i < (int)p_sys->i_track; i++ )
1140 avi_track_t *tk = p_sys->track[i];
1141 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1143 *pf = (float)tk->i_rate / (float)tk->i_scale;
1150 return demux_vaControlDefault( p_input, i_query, args );
1152 return VLC_EGENERIC;
1155 /*****************************************************************************
1156 * Function to convert pts to chunk or byte
1157 *****************************************************************************/
1159 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1161 return (mtime_t)((int64_t)i_pts *
1162 (int64_t)tk->i_rate /
1163 (int64_t)tk->i_scale /
1166 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1168 return (mtime_t)((int64_t)i_pts *
1169 (int64_t)tk->i_rate /
1170 (int64_t)tk->i_scale /
1172 (int64_t)tk->i_samplesize );
1175 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1179 i_dpts = (mtime_t)( (int64_t)1000000 *
1181 (int64_t)tk->i_scale /
1182 (int64_t)tk->i_rate );
1184 if( tk->i_samplesize )
1186 return i_dpts / tk->i_samplesize;
1191 static mtime_t AVI_GetPTS( avi_track_t *tk )
1193 if( tk->i_samplesize )
1195 int64_t i_count = 0;
1197 /* we need a valid entry we will emulate one */
1198 if( tk->i_idxposc == tk->i_idxnb )
1202 /* use the last entry */
1203 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1204 + tk->p_index[tk->i_idxnb - 1].i_length;
1209 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1211 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1215 return AVI_GetDPTS( tk, tk->i_idxposc );
1219 static int AVI_StreamChunkFind( input_thread_t *p_input,
1220 unsigned int i_stream )
1222 demux_sys_t *p_sys = p_input->p_demux_data;
1223 avi_packet_t avi_pk;
1225 /* find first chunk of i_stream that isn't in index */
1227 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1229 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
1230 if( AVI_PacketNext( p_input ) )
1232 return VLC_EGENERIC;
1237 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
1242 if( p_input->b_die )
1244 return VLC_EGENERIC;
1247 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1249 msg_Warn( p_input, "cannot get packet header" );
1250 return VLC_EGENERIC;
1252 if( avi_pk.i_stream >= p_sys->i_track ||
1253 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1255 if( AVI_PacketNext( p_input ) )
1257 return VLC_EGENERIC;
1262 /* add this chunk to the index */
1263 AVIIndexEntry_t index;
1265 index.i_id = avi_pk.i_fourcc;
1267 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1269 index.i_pos = avi_pk.i_pos;
1270 index.i_length = avi_pk.i_size;
1271 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1273 if( avi_pk.i_stream == i_stream )
1278 if( AVI_PacketNext( p_input ) )
1280 return VLC_EGENERIC;
1287 /* be sure that i_ck will be a valid index entry */
1288 static int AVI_StreamChunkSet( input_thread_t *p_input,
1289 unsigned int i_stream,
1292 demux_sys_t *p_sys = p_input->p_demux_data;
1293 avi_track_t *p_stream = p_sys->track[i_stream];
1295 p_stream->i_idxposc = i_ck;
1296 p_stream->i_idxposb = 0;
1298 if( i_ck >= p_stream->i_idxnb )
1300 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1303 p_stream->i_idxposc++;
1304 if( AVI_StreamChunkFind( p_input, i_stream ) )
1306 return VLC_EGENERIC;
1309 } while( p_stream->i_idxposc < i_ck );
1316 /* XXX FIXME up to now, we assume that all chunk are one after one */
1317 static int AVI_StreamBytesSet( input_thread_t *p_input,
1318 unsigned int i_stream,
1321 demux_sys_t *p_sys = p_input->p_demux_data;
1322 avi_track_t *p_stream = p_sys->track[i_stream];
1324 if( ( p_stream->i_idxnb > 0 )
1325 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1326 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1328 /* index is valid to find the ck */
1329 /* uses dichototmie to be fast enougth */
1330 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1331 int i_idxmax = p_stream->i_idxnb;
1335 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1337 i_idxmax = i_idxposc ;
1338 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1342 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1343 p_stream->p_index[i_idxposc].i_length <= i_byte)
1345 i_idxmin = i_idxposc ;
1346 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1350 p_stream->i_idxposc = i_idxposc;
1351 p_stream->i_idxposb = i_byte -
1352 p_stream->p_index[i_idxposc].i_lengthtotal;
1361 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1362 p_stream->i_idxposb = 0;
1365 p_stream->i_idxposc++;
1366 if( AVI_StreamChunkFind( p_input, i_stream ) )
1368 return VLC_EGENERIC;
1371 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1372 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1374 p_stream->i_idxposb = i_byte -
1375 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1380 static int AVI_TrackSeek( input_thread_t *p_input,
1384 demux_sys_t *p_sys = p_input->p_demux_data;
1385 #define p_stream p_sys->track[i_stream]
1388 i_oldpts = AVI_GetPTS( p_stream );
1390 if( !p_stream->i_samplesize )
1392 if( AVI_StreamChunkSet( p_input,
1394 AVI_PTSToChunk( p_stream, i_date ) ) )
1396 return VLC_EGENERIC;
1400 "old:"I64Fd" %s new "I64Fd,
1402 i_oldpts > i_date ? ">" : "<",
1405 if( p_stream->i_cat == VIDEO_ES )
1407 /* search key frame */
1408 if( i_date < i_oldpts )
1410 while( p_stream->i_idxposc > 0 &&
1411 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1414 if( AVI_StreamChunkSet( p_input,
1416 p_stream->i_idxposc - 1 ) )
1418 return VLC_EGENERIC;
1424 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1425 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1428 if( AVI_StreamChunkSet( p_input,
1430 p_stream->i_idxposc + 1 ) )
1432 return VLC_EGENERIC;
1440 if( AVI_StreamBytesSet( p_input,
1442 AVI_PTSToByte( p_stream, i_date ) ) )
1444 return VLC_EGENERIC;
1451 /****************************************************************************
1452 * Return VLC_TRUE if it's a key frame
1453 ****************************************************************************/
1454 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1460 * startcode: 0x00000100 32bits
1461 * framenumber ? 5bits
1462 * piture type 0(I),1(P) 2bits
1464 if( GetDWBE( p_byte ) != 0x00000100 )
1466 /* it's not an msmpegv1 stream, strange...*/
1467 return AVIIF_KEYFRAME;
1471 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1474 case FOURCC_DIV3: /* wmv1 also */
1476 * picture type 0(I),1(P) 2bits
1478 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1480 /* we should find first occurence of 0x000001b6 (32bits)
1481 * startcode: 0x000001b6 32bits
1482 * piture type 0(I),1(P) 2bits
1484 if( GetDWBE( p_byte ) != 0x000001b6 )
1486 /* not true , need to find the first VOP header */
1487 return AVIIF_KEYFRAME;
1491 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1494 /* I can't do it, so say yes */
1495 return AVIIF_KEYFRAME;
1499 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1504 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1508 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1558 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1562 /****************************************************************************
1564 ****************************************************************************/
1565 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1566 int *pi_number, int *pi_type )
1568 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1571 c1 = ((uint8_t *)&i_id)[0];
1572 c2 = ((uint8_t *)&i_id)[1];
1574 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1576 SET_PTR( pi_number, 100 ); /* > max stream number */
1577 SET_PTR( pi_type, UNKNOWN_ES );
1581 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1582 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1585 SET_PTR( pi_type, AUDIO_ES );
1589 SET_PTR( pi_type, VIDEO_ES );
1592 SET_PTR( pi_type, UNKNOWN_ES );
1599 /****************************************************************************
1601 ****************************************************************************/
1602 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
1606 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1608 return VLC_EGENERIC;
1610 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1611 p_pk->i_size = GetDWLE( p_peek + 4 );
1612 p_pk->i_pos = stream_Tell( p_input->s );
1613 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1615 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1616 p_peek[10], p_peek[11] );
1623 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1625 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1629 static int AVI_PacketNext( input_thread_t *p_input )
1631 avi_packet_t avi_ck;
1634 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
1636 return VLC_EGENERIC;
1639 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1640 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1644 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1645 avi_ck.i_type == AVIFOURCC_AVIX )
1651 i_skip = __EVEN( avi_ck.i_size ) + 8;
1654 if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
1656 return VLC_EGENERIC;
1660 static int AVI_PacketRead( input_thread_t *p_input,
1662 block_t **pp_frame )
1666 i_size = __EVEN( p_pk->i_size + 8 );
1668 if( ( *pp_frame = stream_Block( p_input->s, i_size ) ) == NULL )
1670 return VLC_EGENERIC;
1672 (*pp_frame)->p_buffer += 8;
1673 (*pp_frame)->i_buffer -= 8;
1675 if( i_size != p_pk->i_size + 8 )
1677 (*pp_frame)->i_buffer--;
1683 static int AVI_PacketSearch( input_thread_t *p_input )
1685 demux_sys_t *p_sys = p_input->p_demux_data;
1686 avi_packet_t avi_pk;
1691 if( stream_Read( p_input->s, NULL, 1 ) != 1 )
1693 return VLC_EGENERIC;
1695 AVI_PacketGetHeader( p_input, &avi_pk );
1696 if( avi_pk.i_stream < p_sys->i_track &&
1697 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1701 switch( avi_pk.i_fourcc )
1703 case AVIFOURCC_JUNK:
1704 case AVIFOURCC_LIST:
1705 case AVIFOURCC_RIFF:
1706 case AVIFOURCC_idx1:
1710 /* Prevents from eating all the CPU with broken files.
1711 * This value should be low enough so that it doesn't affect the
1712 * reading speed too much (not that we care much anyway because
1713 * this code is called only on broken files). */
1714 if( !(++i_count % 1024) )
1716 if( p_input->b_die ) return VLC_EGENERIC;
1719 if( !(i_count % (1024 * 10)) )
1720 msg_Warn( p_input, "trying to resync..." );
1725 /****************************************************************************
1727 ****************************************************************************/
1728 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1730 AVIIndexEntry_t *p_index)
1732 avi_track_t *tk = p_sys->track[i_stream];
1734 /* Update i_movi_lastchunk_pos */
1735 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1737 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1741 if( tk->i_idxnb >= tk->i_idxmax )
1743 tk->i_idxmax += 16384;
1744 tk->p_index = realloc( tk->p_index,
1745 tk->i_idxmax * sizeof( AVIIndexEntry_t ) );
1746 if( tk->p_index == NULL )
1751 /* calculate cumulate length */
1752 if( tk->i_idxnb > 0 )
1754 p_index->i_lengthtotal =
1755 tk->p_index[tk->i_idxnb - 1].i_length +
1756 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1760 p_index->i_lengthtotal = 0;
1763 tk->p_index[tk->i_idxnb++] = *p_index;
1766 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
1768 demux_sys_t *p_sys = p_input->p_demux_data;
1770 avi_chunk_list_t *p_riff;
1771 avi_chunk_list_t *p_movi;
1772 avi_chunk_idx1_t *p_idx1;
1774 unsigned int i_stream;
1775 unsigned int i_index;
1779 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1780 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1781 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1785 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
1786 return VLC_EGENERIC;
1789 /* *** calculate offset *** */
1790 /* Well, avi is __SHIT__ so test more than one entry
1791 * (needed for some avi files) */
1793 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1795 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1797 i_offset = p_movi->i_chunk_pos + 8;
1802 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
1806 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
1809 if( i_stream < p_sys->i_track &&
1810 i_cat == p_sys->track[i_stream]->i_cat )
1812 AVIIndexEntry_t index;
1813 index.i_id = p_idx1->entry[i_index].i_fourcc;
1815 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
1816 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
1817 index.i_length = p_idx1->entry[i_index].i_length;
1818 AVI_IndexAddEntry( p_sys, i_stream, &index );
1824 static void __Parse_indx( input_thread_t *p_input,
1826 avi_chunk_indx_t *p_indx )
1828 demux_sys_t *p_sys = p_input->p_demux_data;
1829 AVIIndexEntry_t index;
1832 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
1833 if( p_indx->i_indexsubtype == 0 )
1835 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1837 index.i_id = p_indx->i_id;
1838 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1839 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
1840 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
1842 AVI_IndexAddEntry( p_sys, i_stream, &index );
1845 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
1847 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1849 index.i_id = p_indx->i_id;
1850 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1851 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
1852 index.i_length = p_indx->idx.field[i].i_size;
1854 AVI_IndexAddEntry( p_sys, i_stream, &index );
1859 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
1863 static void AVI_IndexLoad_indx( input_thread_t *p_input )
1865 demux_sys_t *p_sys = p_input->p_demux_data;
1866 unsigned int i_stream;
1869 avi_chunk_list_t *p_riff;
1870 avi_chunk_list_t *p_hdrl;
1872 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1873 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
1875 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1877 avi_chunk_list_t *p_strl;
1878 avi_chunk_indx_t *p_indx;
1880 #define p_stream p_sys->track[i_stream]
1881 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
1882 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
1886 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
1890 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
1892 __Parse_indx( p_input, i_stream, p_indx );
1894 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
1896 avi_chunk_indx_t ck_sub;
1897 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1899 if( stream_Seek( p_input->s, p_indx->idx.super[i].i_offset )||
1900 AVI_ChunkRead( p_input->s, &ck_sub, NULL ) )
1904 __Parse_indx( p_input, i_stream, &ck_sub );
1909 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
1915 static void AVI_IndexLoad( input_thread_t *p_input )
1917 demux_sys_t *p_sys = p_input->p_demux_data;
1918 unsigned int i_stream;
1920 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1922 p_sys->track[i_stream]->i_idxnb = 0;
1923 p_sys->track[i_stream]->i_idxmax = 0;
1924 p_sys->track[i_stream]->p_index = NULL;
1929 AVI_IndexLoad_indx( p_input );
1931 else if( AVI_IndexLoad_idx1( p_input ) )
1933 /* try indx if idx1 failed as some "normal" file have indx too */
1934 AVI_IndexLoad_indx( p_input );
1937 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1939 msg_Dbg( p_input, "stream[%d] created %d index entries",
1940 i_stream, p_sys->track[i_stream]->i_idxnb );
1944 static void AVI_IndexCreate( input_thread_t *p_input )
1946 demux_sys_t *p_sys = p_input->p_demux_data;
1948 avi_chunk_list_t *p_riff;
1949 avi_chunk_list_t *p_movi;
1951 unsigned int i_stream;
1954 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1955 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1959 msg_Err( p_input, "cannot find p_movi" );
1963 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1965 p_sys->track[i_stream]->i_idxnb = 0;
1966 p_sys->track[i_stream]->i_idxmax = 0;
1967 p_sys->track[i_stream]->p_index = NULL;
1969 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
1970 stream_Size( p_input->s ) );
1972 stream_Seek( p_input->s, p_movi->i_chunk_pos + 12 );
1973 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
1978 if( p_input->b_die )
1983 if( AVI_PacketGetHeader( p_input, &pk ) )
1987 if( pk.i_stream < p_sys->i_track &&
1988 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
1990 AVIIndexEntry_t index;
1991 index.i_id = pk.i_fourcc;
1993 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
1994 index.i_pos = pk.i_pos;
1995 index.i_length = pk.i_size;
1996 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2000 switch( pk.i_fourcc )
2002 case AVIFOURCC_idx1:
2005 avi_chunk_list_t *p_sysx;
2006 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2007 AVIFOURCC_RIFF, 1 );
2009 msg_Dbg( p_input, "looking for new RIFF chunk" );
2010 if( stream_Seek( p_input->s, p_sysx->i_chunk_pos + 24))
2017 case AVIFOURCC_RIFF:
2018 msg_Dbg( p_input, "new RIFF chunk found" );
2020 case AVIFOURCC_JUNK:
2023 msg_Warn( p_input, "need resync, probably broken avi" );
2024 if( AVI_PacketSearch( p_input ) )
2026 msg_Warn( p_input, "lost sync, abord index creation" );
2032 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2033 AVI_PacketNext( p_input ) )
2040 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2043 "stream[%d] creating %d index entries",
2045 p_sys->track[i_stream]->i_idxnb );
2049 /*****************************************************************************
2051 *****************************************************************************/
2052 static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
2054 demux_sys_t *p_sys = p_input->p_demux_data;
2056 int b_end = VLC_TRUE;
2058 for( i = 0; i < p_sys->i_track; i++ )
2060 avi_track_t *tk = p_sys->track[i];
2061 if( tk->i_idxposc >= tk->i_idxnb )
2063 tk->b_activated = VLC_FALSE;
2073 /****************************************************************************
2074 * AVI_MovieGetLength give max streams length in second
2075 ****************************************************************************/
2076 static mtime_t AVI_MovieGetLength( input_thread_t *p_input )
2078 demux_sys_t *p_sys = p_input->p_demux_data;
2079 mtime_t i_maxlength = 0;
2082 for( i = 0; i < p_sys->i_track; i++ )
2084 avi_track_t *tk = p_sys->track[i];
2087 /* fix length for each stream */
2088 if( tk->i_idxnb < 1 || !tk->p_index )
2093 if( tk->i_samplesize )
2095 i_length = AVI_GetDPTS( tk,
2096 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2097 tk->p_index[tk->i_idxnb-1].i_length );
2101 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2103 i_length /= (mtime_t)1000000; /* in seconds */
2106 "stream[%d] length:"I64Fd" (based on index)",
2109 i_maxlength = __MAX( i_maxlength, i_length );