1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.80 2003/11/30 22:14:39 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>
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 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
282 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
283 fmt.p_extra = &p_auds->p_wf[1];
284 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
285 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
286 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
289 case( AVIFOURCC_vids ):
290 tk->i_cat = VIDEO_ES;
291 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
292 p_vids->p_bih->biCompression );
293 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
294 tk->i_samplesize = 0;
295 fmt.video.i_width = p_vids->p_bih->biWidth;
296 fmt.video.i_height = p_vids->p_bih->biHeight;
298 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
299 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
300 fmt.p_extra = &p_vids->p_bih[1];
301 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
303 (char*)&p_vids->p_bih->biCompression,
304 p_vids->p_bih->biWidth,
305 p_vids->p_bih->biHeight,
306 p_vids->p_bih->biBitCount,
307 (float)tk->i_rate/(float)tk->i_scale );
310 msg_Warn( p_input, "stream[%d] unknown type", i );
314 tk->p_es = es_out_Add( p_input->p_es_out, &fmt );
315 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
318 if( p_sys->i_track <= 0 )
320 msg_Err( p_input, "No valid track" );
324 if( config_GetInt( p_input, "avi-index" ) )
326 if( p_sys->b_seekable )
328 AVI_IndexCreate( p_input );
332 msg_Warn( p_input, "cannot create index (unseekable stream)" );
333 AVI_IndexLoad( p_input );
338 AVI_IndexLoad( p_input );
341 /* *** movie length in sec *** */
342 p_sys->i_length = AVI_MovieGetLength( p_input );
343 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
344 (mtime_t)p_avih->i_microsecperframe /
347 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
349 /* fix some BeOS MediaKit generated file */
350 for( i = 0 ; i < p_sys->i_track; i++ )
352 avi_track_t *tk = p_sys->track[i];
353 avi_chunk_list_t *p_strl;
354 avi_chunk_strh_t *p_strh;
355 avi_chunk_strf_auds_t *p_auds;
357 if( tk->i_cat != AUDIO_ES )
361 if( tk->i_idxnb < 1 ||
363 tk->i_samplesize != 0 )
367 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
368 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
369 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
371 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
372 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
374 int64_t i_track_length =
375 tk->p_index[tk->i_idxnb-1].i_length +
376 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
377 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
378 (mtime_t)p_avih->i_microsecperframe;
382 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
385 tk->i_samplesize = 1;
386 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
387 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
391 if( p_sys->i_length )
393 p_input->stream.i_mux_rate =
394 stream_Size( p_input->s ) / 50 / p_sys->i_length;
397 if( p_sys->b_seekable )
399 /* we have read all chunk so go back to movi */
400 stream_Seek( p_input->s, p_movi->i_chunk_pos );
402 /* Skip movi header */
403 stream_Read( p_input->s, NULL, 12 );
405 p_sys->i_movi_begin = p_movi->i_chunk_pos;
409 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
414 /*****************************************************************************
415 * Close: frees unused data
416 *****************************************************************************/
417 static void Close ( vlc_object_t * p_this )
419 input_thread_t * p_input = (input_thread_t *)p_this;
421 demux_sys_t *p_sys = p_input->p_demux_data ;
423 for( i = 0; i < p_sys->i_track; i++ )
425 if( p_sys->track[i] )
427 FREE( p_sys->track[i]->p_index );
428 free( p_sys->track[i] );
431 FREE( p_sys->track );
432 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
437 /*****************************************************************************
438 * Demux_Seekable: reads and demuxes data packets for stream seekable
439 *****************************************************************************
440 * AVIDemux: reads and demuxes data packets
441 *****************************************************************************
442 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
443 *****************************************************************************/
450 off_t i_posf; /* where we will read :
451 if i_idxposb == 0 : begining of chunk (+8 to acces data)
452 else : point on data directly */
453 } avi_track_toread_t;
455 static int Demux_Seekable( input_thread_t *p_input )
457 demux_sys_t *p_sys = p_input->p_demux_data;
459 unsigned int i_track_count = 0;
460 unsigned int i_track;
462 /* cannot be more than 100 stream (dcXX or wbXX) */
463 avi_track_toread_t toread[100];
466 /* detect new selected/unselected streams */
467 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
469 avi_track_t *tk = p_sys->track[i_track];
472 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
473 if( b && !tk->b_activated )
475 if( p_sys->b_seekable)
477 AVI_TrackSeek( p_input, i_track, p_sys->i_time );
479 tk->b_activated = VLC_TRUE;
481 else if( !b && tk->b_activated )
483 tk->b_activated = VLC_FALSE;
491 if( i_track_count <= 0 )
493 msg_Warn( p_input, "no track selected, exiting..." );
497 /* wait for the good time */
498 p_sys->i_pcr = p_sys->i_time * 9 / 100;
500 input_ClockManageRef( p_input,
501 p_input->stream.p_selected_program,
505 p_sys->i_time += 25*1000; /* read 25ms */
508 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
510 avi_track_t *tk = p_sys->track[i_track];
513 toread[i_track].b_ok = tk->b_activated;
514 if( tk->i_idxposc < tk->i_idxnb )
516 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
517 if( tk->i_idxposb > 0 )
519 toread[i_track].i_posf += 8 + tk->i_idxposb;
524 toread[i_track].i_posf = -1;
527 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
529 if( tk->i_samplesize )
531 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
535 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
540 toread[i_track].i_toread *= -1;
544 b_stream = VLC_FALSE;
555 /* search for first chunk to be read */
556 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
558 if( !toread[i].b_ok ||
559 AVI_GetDPTS( p_sys->track[i],
560 toread[i].i_toread ) <= -25 * 1000 )
565 if( toread[i].i_toread > 0 )
567 b_done = VLC_FALSE; /* not yet finished */
569 if( toread[i].i_posf > 0 )
571 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
574 i_pos = toread[i].i_posf;
586 /* no valid index, we will parse directly the stream
587 * in case we fail we will disable all finished stream */
588 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
590 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
591 if( AVI_PacketNext( p_input ) )
593 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
598 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
605 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
608 "cannot get packet header, track disabled" );
609 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
611 if( avi_pk.i_stream >= p_sys->i_track ||
612 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
614 if( AVI_PacketNext( p_input ) )
617 "cannot skip packet, track disabled" );
618 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
624 /* add this chunk to the index */
625 AVIIndexEntry_t index;
627 index.i_id = avi_pk.i_fourcc;
629 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
631 index.i_pos = avi_pk.i_pos;
632 index.i_length = avi_pk.i_size;
633 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
635 i_track = avi_pk.i_stream;
636 tk = p_sys->track[i_track];
637 /* do we will read this data ? */
638 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
644 if( AVI_PacketNext( p_input ) )
647 "cannot skip packet, track disabled" );
648 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
657 stream_Seek( p_input->s, i_pos );
660 /* Set the track to use */
661 tk = p_sys->track[i_track];
663 /* read thoses data */
664 if( tk->i_samplesize )
666 unsigned int i_toread;
668 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
670 if( tk->i_samplesize > 1 )
672 i_toread = tk->i_samplesize;
676 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
679 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
685 i_size = tk->p_index[tk->i_idxposc].i_length;
688 if( tk->i_idxposb == 0 )
690 i_size += 8; /* need to read and skip header */
693 if( ( p_frame = stream_Block( p_input->s, __EVEN( i_size ) ) )==NULL )
695 msg_Warn( p_input, "failled reading data" );
696 tk->b_activated = VLC_FALSE;
697 toread[i_track].b_ok = VLC_FALSE;
700 if( i_size % 2 ) /* read was padded on word boundary */
705 if( tk->i_idxposb == 0 )
707 p_frame->p_buffer += 8;
708 p_frame->i_buffer -= 8;
710 p_frame->i_pts = AVI_GetPTS( tk );
713 if( tk->i_samplesize )
715 if( tk->i_idxposb == 0 )
719 toread[i_track].i_toread -= i_size;
720 tk->i_idxposb += i_size;
722 tk->p_index[tk->i_idxposc].i_length )
730 toread[i_track].i_toread--;
734 if( tk->i_idxposc < tk->i_idxnb)
736 toread[i_track].i_posf =
737 tk->p_index[tk->i_idxposc].i_pos;
738 if( tk->i_idxposb > 0 )
740 toread[i_track].i_posf += 8 + tk->i_idxposb;
746 toread[i_track].i_posf = -1;
749 b_stream = VLC_TRUE; /* at least one read succeed */
752 input_ClockGetTS( p_input,
753 p_input->stream.p_selected_program,
754 p_frame->i_pts * 9/100);
756 if( tk->i_cat != VIDEO_ES )
757 p_frame->i_dts = p_frame->i_pts;
760 p_frame->i_dts = p_frame->i_pts;
764 //p_pes->i_rate = p_input->stream.control.i_rate;
765 es_out_Send( p_input->p_es_out, tk->p_es, p_frame );
770 /*****************************************************************************
771 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
772 *****************************************************************************
773 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
774 *****************************************************************************/
775 static int Demux_UnSeekable( input_thread_t *p_input )
777 demux_sys_t *p_sys = p_input->p_demux_data;
778 avi_track_t *p_stream_master = NULL;
780 unsigned int i_stream;
781 unsigned int i_packet;
783 /* Check if we need to send the audio data to decoder */
784 b_audio = !p_input->stream.control.b_mute;
786 input_ClockManageRef( p_input,
787 p_input->stream.p_selected_program,
790 /* *** find master stream for data packet skipping algo *** */
791 /* *** -> first video, if any, or first audio ES *** */
792 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
794 avi_track_t *tk = p_sys->track[i_stream];
797 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
799 if( b && tk->i_cat == VIDEO_ES )
801 p_stream_master = tk;
805 p_stream_master = tk;
809 if( !p_stream_master )
811 msg_Warn( p_input, "no more stream selected" );
815 p_sys->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
817 for( i_packet = 0; i_packet < 10; i_packet++)
819 #define p_stream p_sys->track[avi_pk.i_stream]
823 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
828 if( avi_pk.i_stream >= p_sys->i_track ||
829 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
831 /* we haven't found an audio or video packet:
832 * - we have seek, found first next packet
833 * - others packets could be found, skip them
835 switch( avi_pk.i_fourcc )
840 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
844 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
846 return( 0 ); /* eof */
849 "seems to have lost position, resync" );
850 if( AVI_PacketSearch( p_input ) )
852 msg_Err( p_input, "resync failed" );
859 /* do will send this packet to decoder ? */
860 if( !b_audio && avi_pk.i_cat == AUDIO_ES )
862 if( AVI_PacketNext( p_input ) )
869 /* it's a selected stream, check for time */
870 if( __ABS( AVI_GetPTS( p_stream ) -
871 AVI_GetPTS( p_stream_master ) )< 600*1000 )
873 /* load it and send to decoder */
875 if( AVI_PacketRead( p_input, &avi_pk, &p_frame ) || p_frame == NULL )
880 input_ClockGetTS( p_input,
881 p_input->stream.p_selected_program,
882 AVI_GetPTS( p_stream ) * 9/100);
884 if( avi_pk.i_cat != VIDEO_ES )
885 p_frame->i_dts = p_frame->i_pts;
888 p_frame->i_dts = p_frame->i_pts;
892 //p_pes->i_rate = p_input->stream.control.i_rate;
893 es_out_Send( p_input->p_es_out, p_stream->p_es, p_frame );
897 if( AVI_PacketNext( p_input ) )
904 /* *** update stream time position *** */
905 if( p_stream->i_samplesize )
907 p_stream->i_idxposb += avi_pk.i_size;
911 p_stream->i_idxposc++;
921 /*****************************************************************************
922 * Seek: goto to i_date or i_percent
923 *****************************************************************************
924 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
925 *****************************************************************************/
926 static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
929 demux_sys_t *p_sys = p_input->p_demux_data;
930 unsigned int i_stream;
932 "seek requested: "I64Fd" secondes %d%%",
936 if( p_sys->b_seekable )
938 if( !p_sys->i_length )
940 avi_track_t *p_stream;
943 /* use i_percent to create a true i_date */
945 "mmh, seeking without index at %d%%"
946 " work only for interleaved file", i_percent );
947 if( i_percent >= 100 )
949 msg_Warn( p_input, "cannot seek so far !" );
952 i_percent = __MAX( i_percent, 0 );
954 /* try to find chunk that is at i_percent or the file */
955 i_pos = __MAX( i_percent *
956 stream_Size( p_input->s ) / 100,
957 p_sys->i_movi_begin );
958 /* search first selected stream */
959 for( i_stream = 0, p_stream = NULL;
960 i_stream < p_sys->i_track; i_stream++ )
962 p_stream = p_sys->track[i_stream];
963 if( p_stream->b_activated )
968 if( !p_stream || !p_stream->b_activated )
970 msg_Warn( p_input, "cannot find any selected stream" );
974 /* be sure that the index exist */
975 if( AVI_StreamChunkSet( p_input,
979 msg_Warn( p_input, "cannot seek" );
983 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
984 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
986 /* search after i_idxposc */
987 if( AVI_StreamChunkSet( p_input,
988 i_stream, p_stream->i_idxposc + 1 ) )
990 msg_Warn( p_input, "cannot seek" );
994 i_date = AVI_GetPTS( p_stream );
995 /* TODO better support for i_samplesize != 0 */
996 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
999 #define p_stream p_sys->track[i_stream]
1001 /* seek for chunk based streams */
1002 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1004 if( p_stream->b_activated && !p_stream->i_samplesize )
1005 /* if( p_stream->b_activated ) */
1007 AVI_TrackSeek( p_input, i_stream, i_date );
1008 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1015 i_date = p_sys->i_time;
1017 /* seek for bytes based streams */
1018 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1020 if( p_stream->b_activated && p_stream->i_samplesize )
1022 AVI_TrackSeek( p_input, i_stream, i_date );
1023 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1026 msg_Dbg( p_input, "seek: "I64Fd" secondes", p_sys->i_time /1000000 );
1027 /* set true movie time */
1029 if( !p_sys->i_time )
1031 p_sys->i_time = i_date;
1038 msg_Err( p_input, "shouldn't yet be executed" );
1043 /*****************************************************************************
1045 *****************************************************************************
1047 *****************************************************************************/
1048 static double ControlGetPosition( input_thread_t *p_input )
1050 demux_sys_t *p_sys = p_input->p_demux_data;
1052 if( p_sys->i_length > 0 )
1054 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1056 else if( stream_Size( p_input->s ) > 0 )
1062 /* search the more advanced selected es */
1063 for( i = 0; i < p_sys->i_track; i++ )
1065 avi_track_t *tk = p_sys->track[i];
1066 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1068 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1069 tk->p_index[tk->i_idxposc].i_length + 8;
1076 return (double)i64 / (double)stream_Size( p_input->s );
1081 static int Control( input_thread_t *p_input, int i_query, va_list args )
1083 demux_sys_t *p_sys = p_input->p_demux_data;
1090 case DEMUX_GET_POSITION:
1091 pf = (double*)va_arg( args, double * );
1092 *pf = ControlGetPosition( p_input );
1094 case DEMUX_SET_POSITION:
1095 if( p_sys->b_seekable )
1097 f = (double)va_arg( args, double );
1098 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1099 return Seek( p_input, i64, (int)(f * 100) ) < 0 ? VLC_EGENERIC : VLC_SUCCESS;
1101 return demux_vaControlDefault( p_input, i_query, args );
1103 case DEMUX_GET_TIME:
1104 pi64 = (int64_t*)va_arg( args, int64_t * );
1105 *pi64 = p_sys->i_time;
1108 case DEMUX_SET_TIME:
1112 i64 = (int64_t)va_arg( args, int64_t );
1113 if( p_sys->i_length > 0 )
1115 i_percent = 100 * i64 / (p_sys->i_length*1000000ULL);
1117 else if( p_sys->i_time > 0 )
1119 i_percent = (int)( 100.0 * ControlGetPosition( p_input ) *
1120 (double)i64 / (double)p_sys->i_time );
1122 return Seek( p_input, i64, i_percent );
1124 case DEMUX_GET_LENGTH:
1125 pi64 = (int64_t*)va_arg( args, int64_t * );
1126 *pi64 = p_sys->i_length * (mtime_t)1000000;
1130 pf = (double*)va_arg( args, double * );
1132 for( i = 0; i < (int)p_sys->i_track; i++ )
1134 avi_track_t *tk = p_sys->track[i];
1135 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1137 *pf = (float)tk->i_rate / (float)tk->i_scale;
1144 return demux_vaControlDefault( p_input, i_query, args );
1146 return VLC_EGENERIC;
1149 /*****************************************************************************
1150 * Function to convert pts to chunk or byte
1151 *****************************************************************************/
1153 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1155 return (mtime_t)((int64_t)i_pts *
1156 (int64_t)tk->i_rate /
1157 (int64_t)tk->i_scale /
1160 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1162 return (mtime_t)((int64_t)i_pts *
1163 (int64_t)tk->i_rate /
1164 (int64_t)tk->i_scale /
1166 (int64_t)tk->i_samplesize );
1169 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1173 i_dpts = (mtime_t)( (int64_t)1000000 *
1175 (int64_t)tk->i_scale /
1176 (int64_t)tk->i_rate );
1178 if( tk->i_samplesize )
1180 return i_dpts / tk->i_samplesize;
1185 static mtime_t AVI_GetPTS( avi_track_t *tk )
1187 if( tk->i_samplesize )
1189 int64_t i_count = 0;
1191 /* we need a valid entry we will emulate one */
1192 if( tk->i_idxposc == tk->i_idxnb )
1196 /* use the last entry */
1197 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1198 + tk->p_index[tk->i_idxnb - 1].i_length;
1203 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1205 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1209 return AVI_GetDPTS( tk, tk->i_idxposc );
1213 static int AVI_StreamChunkFind( input_thread_t *p_input,
1214 unsigned int i_stream )
1216 demux_sys_t *p_sys = p_input->p_demux_data;
1217 avi_packet_t avi_pk;
1219 /* find first chunk of i_stream that isn't in index */
1221 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1223 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
1224 if( AVI_PacketNext( p_input ) )
1226 return VLC_EGENERIC;
1231 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
1236 if( p_input->b_die )
1238 return VLC_EGENERIC;
1241 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1243 msg_Warn( p_input, "cannot get packet header" );
1244 return VLC_EGENERIC;
1246 if( avi_pk.i_stream >= p_sys->i_track ||
1247 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1249 if( AVI_PacketNext( p_input ) )
1251 return VLC_EGENERIC;
1256 /* add this chunk to the index */
1257 AVIIndexEntry_t index;
1259 index.i_id = avi_pk.i_fourcc;
1261 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1263 index.i_pos = avi_pk.i_pos;
1264 index.i_length = avi_pk.i_size;
1265 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1267 if( avi_pk.i_stream == i_stream )
1272 if( AVI_PacketNext( p_input ) )
1274 return VLC_EGENERIC;
1281 /* be sure that i_ck will be a valid index entry */
1282 static int AVI_StreamChunkSet( input_thread_t *p_input,
1283 unsigned int i_stream,
1286 demux_sys_t *p_sys = p_input->p_demux_data;
1287 avi_track_t *p_stream = p_sys->track[i_stream];
1289 p_stream->i_idxposc = i_ck;
1290 p_stream->i_idxposb = 0;
1292 if( i_ck >= p_stream->i_idxnb )
1294 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1297 p_stream->i_idxposc++;
1298 if( AVI_StreamChunkFind( p_input, i_stream ) )
1300 return VLC_EGENERIC;
1303 } while( p_stream->i_idxposc < i_ck );
1310 /* XXX FIXME up to now, we assume that all chunk are one after one */
1311 static int AVI_StreamBytesSet( input_thread_t *p_input,
1312 unsigned int i_stream,
1315 demux_sys_t *p_sys = p_input->p_demux_data;
1316 avi_track_t *p_stream = p_sys->track[i_stream];
1318 if( ( p_stream->i_idxnb > 0 )
1319 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1320 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1322 /* index is valid to find the ck */
1323 /* uses dichototmie to be fast enougth */
1324 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1325 int i_idxmax = p_stream->i_idxnb;
1329 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1331 i_idxmax = i_idxposc ;
1332 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1336 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1337 p_stream->p_index[i_idxposc].i_length <= i_byte)
1339 i_idxmin = i_idxposc ;
1340 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1344 p_stream->i_idxposc = i_idxposc;
1345 p_stream->i_idxposb = i_byte -
1346 p_stream->p_index[i_idxposc].i_lengthtotal;
1355 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1356 p_stream->i_idxposb = 0;
1359 p_stream->i_idxposc++;
1360 if( AVI_StreamChunkFind( p_input, i_stream ) )
1362 return VLC_EGENERIC;
1365 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1366 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1368 p_stream->i_idxposb = i_byte -
1369 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1374 static int AVI_TrackSeek( input_thread_t *p_input,
1378 demux_sys_t *p_sys = p_input->p_demux_data;
1379 #define p_stream p_sys->track[i_stream]
1382 i_oldpts = AVI_GetPTS( p_stream );
1384 if( !p_stream->i_samplesize )
1386 if( AVI_StreamChunkSet( p_input,
1388 AVI_PTSToChunk( p_stream, i_date ) ) )
1390 return VLC_EGENERIC;
1394 "old:"I64Fd" %s new "I64Fd,
1396 i_oldpts > i_date ? ">" : "<",
1399 if( p_stream->i_cat == VIDEO_ES )
1401 /* search key frame */
1402 if( i_date < i_oldpts )
1404 while( p_stream->i_idxposc > 0 &&
1405 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1408 if( AVI_StreamChunkSet( p_input,
1410 p_stream->i_idxposc - 1 ) )
1412 return VLC_EGENERIC;
1418 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1419 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1422 if( AVI_StreamChunkSet( p_input,
1424 p_stream->i_idxposc + 1 ) )
1426 return VLC_EGENERIC;
1434 if( AVI_StreamBytesSet( p_input,
1436 AVI_PTSToByte( p_stream, i_date ) ) )
1438 return VLC_EGENERIC;
1445 /****************************************************************************
1446 * Return VLC_TRUE if it's a key frame
1447 ****************************************************************************/
1448 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1454 * startcode: 0x00000100 32bits
1455 * framenumber ? 5bits
1456 * piture type 0(I),1(P) 2bits
1458 if( GetDWBE( p_byte ) != 0x00000100 )
1460 /* it's not an msmpegv1 stream, strange...*/
1461 return AVIIF_KEYFRAME;
1465 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1468 case FOURCC_DIV3: /* wmv1 also */
1470 * picture type 0(I),1(P) 2bits
1472 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1474 /* we should find first occurence of 0x000001b6 (32bits)
1475 * startcode: 0x000001b6 32bits
1476 * piture type 0(I),1(P) 2bits
1478 if( GetDWBE( p_byte ) != 0x000001b6 )
1480 /* not true , need to find the first VOP header */
1481 return AVIIF_KEYFRAME;
1485 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1488 /* I can't do it, so say yes */
1489 return AVIIF_KEYFRAME;
1493 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1498 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1502 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1553 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1557 /****************************************************************************
1559 ****************************************************************************/
1560 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1561 int *pi_number, int *pi_type )
1563 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1566 c1 = ((uint8_t *)&i_id)[0];
1567 c2 = ((uint8_t *)&i_id)[1];
1569 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1571 SET_PTR( pi_number, 100 ); /* > max stream number */
1572 SET_PTR( pi_type, UNKNOWN_ES );
1576 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1577 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1580 SET_PTR( pi_type, AUDIO_ES );
1584 SET_PTR( pi_type, VIDEO_ES );
1587 SET_PTR( pi_type, UNKNOWN_ES );
1594 /****************************************************************************
1596 ****************************************************************************/
1597 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
1601 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1603 return VLC_EGENERIC;
1605 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1606 p_pk->i_size = GetDWLE( p_peek + 4 );
1607 p_pk->i_pos = stream_Tell( p_input->s );
1608 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1610 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1611 p_peek[10], p_peek[11] );
1618 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1620 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1624 static int AVI_PacketNext( input_thread_t *p_input )
1626 avi_packet_t avi_ck;
1629 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
1631 return VLC_EGENERIC;
1634 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1635 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1639 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1640 avi_ck.i_type == AVIFOURCC_AVIX )
1646 i_skip = __EVEN( avi_ck.i_size ) + 8;
1649 if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
1651 return VLC_EGENERIC;
1655 static int AVI_PacketRead( input_thread_t *p_input,
1657 block_t **pp_frame )
1661 i_size = __EVEN( p_pk->i_size + 8 );
1663 if( ( *pp_frame = stream_Block( p_input->s, i_size ) ) == NULL )
1665 return VLC_EGENERIC;
1667 (*pp_frame)->p_buffer += 8;
1668 (*pp_frame)->i_buffer -= 8;
1670 if( i_size != p_pk->i_size + 8 )
1672 (*pp_frame)->i_buffer--;
1678 static int AVI_PacketSearch( input_thread_t *p_input )
1680 demux_sys_t *p_sys = p_input->p_demux_data;
1681 avi_packet_t avi_pk;
1686 if( stream_Read( p_input->s, NULL, 1 ) != 1 )
1688 return VLC_EGENERIC;
1690 AVI_PacketGetHeader( p_input, &avi_pk );
1691 if( avi_pk.i_stream < p_sys->i_track &&
1692 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1696 switch( avi_pk.i_fourcc )
1698 case AVIFOURCC_JUNK:
1699 case AVIFOURCC_LIST:
1700 case AVIFOURCC_RIFF:
1701 case AVIFOURCC_idx1:
1705 /* Prevents from eating all the CPU with broken files.
1706 * This value should be low enough so that it doesn't affect the
1707 * reading speed too much (not that we care much anyway because
1708 * this code is called only on broken files). */
1709 if( !(++i_count % 1024) )
1711 if( p_input->b_die ) return VLC_EGENERIC;
1714 if( !(i_count % (1024 * 10)) )
1715 msg_Warn( p_input, "trying to resync..." );
1720 /****************************************************************************
1722 ****************************************************************************/
1723 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1725 AVIIndexEntry_t *p_index)
1727 avi_track_t *tk = p_sys->track[i_stream];
1729 /* Update i_movi_lastchunk_pos */
1730 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1732 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1736 if( tk->i_idxnb >= tk->i_idxmax )
1738 tk->i_idxmax += 16384;
1739 tk->p_index = realloc( tk->p_index,
1740 tk->i_idxmax * sizeof( AVIIndexEntry_t ) );
1741 if( tk->p_index == NULL )
1746 /* calculate cumulate length */
1747 if( tk->i_idxnb > 0 )
1749 p_index->i_lengthtotal =
1750 tk->p_index[tk->i_idxnb - 1].i_length +
1751 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1755 p_index->i_lengthtotal = 0;
1758 tk->p_index[tk->i_idxnb++] = *p_index;
1761 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
1763 demux_sys_t *p_sys = p_input->p_demux_data;
1765 avi_chunk_list_t *p_riff;
1766 avi_chunk_list_t *p_movi;
1767 avi_chunk_idx1_t *p_idx1;
1769 unsigned int i_stream;
1770 unsigned int i_index;
1774 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1775 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1776 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1780 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
1781 return VLC_EGENERIC;
1784 /* *** calculate offset *** */
1785 /* Well, avi is __SHIT__ so test more than one entry
1786 * (needed for some avi files) */
1788 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1790 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1792 i_offset = p_movi->i_chunk_pos + 8;
1797 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
1801 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
1804 if( i_stream < p_sys->i_track &&
1805 i_cat == p_sys->track[i_stream]->i_cat )
1807 AVIIndexEntry_t index;
1808 index.i_id = p_idx1->entry[i_index].i_fourcc;
1810 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
1811 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
1812 index.i_length = p_idx1->entry[i_index].i_length;
1813 AVI_IndexAddEntry( p_sys, i_stream, &index );
1819 static void __Parse_indx( input_thread_t *p_input,
1821 avi_chunk_indx_t *p_indx )
1823 demux_sys_t *p_sys = p_input->p_demux_data;
1824 AVIIndexEntry_t index;
1827 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
1828 if( p_indx->i_indexsubtype == 0 )
1830 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1832 index.i_id = p_indx->i_id;
1833 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1834 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
1835 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
1837 AVI_IndexAddEntry( p_sys, i_stream, &index );
1840 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
1842 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1844 index.i_id = p_indx->i_id;
1845 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1846 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
1847 index.i_length = p_indx->idx.field[i].i_size;
1849 AVI_IndexAddEntry( p_sys, i_stream, &index );
1854 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
1858 static void AVI_IndexLoad_indx( input_thread_t *p_input )
1860 demux_sys_t *p_sys = p_input->p_demux_data;
1861 unsigned int i_stream;
1864 avi_chunk_list_t *p_riff;
1865 avi_chunk_list_t *p_hdrl;
1867 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1868 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
1870 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1872 avi_chunk_list_t *p_strl;
1873 avi_chunk_indx_t *p_indx;
1875 #define p_stream p_sys->track[i_stream]
1876 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
1877 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
1881 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
1885 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
1887 __Parse_indx( p_input, i_stream, p_indx );
1889 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
1891 avi_chunk_indx_t ck_sub;
1892 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1894 if( stream_Seek( p_input->s, p_indx->idx.super[i].i_offset )||
1895 AVI_ChunkRead( p_input->s, &ck_sub, NULL ) )
1899 __Parse_indx( p_input, i_stream, &ck_sub );
1904 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
1910 static void AVI_IndexLoad( input_thread_t *p_input )
1912 demux_sys_t *p_sys = p_input->p_demux_data;
1913 unsigned int i_stream;
1915 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1917 p_sys->track[i_stream]->i_idxnb = 0;
1918 p_sys->track[i_stream]->i_idxmax = 0;
1919 p_sys->track[i_stream]->p_index = NULL;
1924 AVI_IndexLoad_indx( p_input );
1926 else if( AVI_IndexLoad_idx1( p_input ) )
1928 /* try indx if idx1 failed as some "normal" file have indx too */
1929 AVI_IndexLoad_indx( p_input );
1932 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1934 msg_Dbg( p_input, "stream[%d] created %d index entries",
1935 i_stream, p_sys->track[i_stream]->i_idxnb );
1939 static void AVI_IndexCreate( input_thread_t *p_input )
1941 demux_sys_t *p_sys = p_input->p_demux_data;
1943 avi_chunk_list_t *p_riff;
1944 avi_chunk_list_t *p_movi;
1946 unsigned int i_stream;
1949 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1950 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1954 msg_Err( p_input, "cannot find p_movi" );
1958 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1960 p_sys->track[i_stream]->i_idxnb = 0;
1961 p_sys->track[i_stream]->i_idxmax = 0;
1962 p_sys->track[i_stream]->p_index = NULL;
1964 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
1965 stream_Size( p_input->s ) );
1967 stream_Seek( p_input->s, p_movi->i_chunk_pos + 12 );
1968 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
1973 if( p_input->b_die )
1978 if( AVI_PacketGetHeader( p_input, &pk ) )
1982 if( pk.i_stream < p_sys->i_track &&
1983 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
1985 AVIIndexEntry_t index;
1986 index.i_id = pk.i_fourcc;
1988 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
1989 index.i_pos = pk.i_pos;
1990 index.i_length = pk.i_size;
1991 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
1995 switch( pk.i_fourcc )
1997 case AVIFOURCC_idx1:
2000 avi_chunk_list_t *p_sysx;
2001 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2002 AVIFOURCC_RIFF, 1 );
2004 msg_Dbg( p_input, "looking for new RIFF chunk" );
2005 if( stream_Seek( p_input->s, p_sysx->i_chunk_pos + 24))
2012 case AVIFOURCC_RIFF:
2013 msg_Dbg( p_input, "new RIFF chunk found" );
2015 case AVIFOURCC_JUNK:
2018 msg_Warn( p_input, "need resync, probably broken avi" );
2019 if( AVI_PacketSearch( p_input ) )
2021 msg_Warn( p_input, "lost sync, abord index creation" );
2027 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2028 AVI_PacketNext( p_input ) )
2035 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2038 "stream[%d] creating %d index entries",
2040 p_sys->track[i_stream]->i_idxnb );
2044 /*****************************************************************************
2046 *****************************************************************************/
2047 static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
2049 demux_sys_t *p_sys = p_input->p_demux_data;
2051 int b_end = VLC_TRUE;
2053 for( i = 0; i < p_sys->i_track; i++ )
2055 avi_track_t *tk = p_sys->track[i];
2056 if( tk->i_idxposc >= tk->i_idxnb )
2058 tk->b_activated = VLC_FALSE;
2059 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2069 /****************************************************************************
2070 * AVI_MovieGetLength give max streams length in second
2071 ****************************************************************************/
2072 static mtime_t AVI_MovieGetLength( input_thread_t *p_input )
2074 demux_sys_t *p_sys = p_input->p_demux_data;
2075 mtime_t i_maxlength = 0;
2078 for( i = 0; i < p_sys->i_track; i++ )
2080 avi_track_t *tk = p_sys->track[i];
2083 /* fix length for each stream */
2084 if( tk->i_idxnb < 1 || !tk->p_index )
2089 if( tk->i_samplesize )
2091 i_length = AVI_GetDPTS( tk,
2092 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2093 tk->p_index[tk->i_idxnb-1].i_length );
2097 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2099 i_length /= (mtime_t)1000000; /* in seconds */
2102 "stream[%d] length:"I64Fd" (based on index)",
2105 i_maxlength = __MAX( i_maxlength, i_length );