1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.70 2003/11/16 22:54:12 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 *, pes_packet_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.i_extra_type = ES_EXTRA_TYPE_WAVEFORMATEX;
285 fmt.p_extra = malloc( fmt.i_extra );
286 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
288 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
290 p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
291 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
294 case( AVIFOURCC_vids ):
295 tk->i_cat = VIDEO_ES;
296 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
297 p_vids->p_bih->biCompression );
298 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
299 tk->i_samplesize = 0;
300 fmt.video.i_width = p_vids->p_bih->biWidth;
301 fmt.video.i_height = p_vids->p_bih->biHeight;
302 if( ( fmt.i_extra = __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
303 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) ) ) > 0 )
305 fmt.i_extra_type = ES_EXTRA_TYPE_BITMAPINFOHEADER;
306 fmt.p_extra = malloc( fmt.i_extra );
307 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
309 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
311 (char*)&p_vids->p_bih->biCompression,
312 p_vids->p_bih->biWidth,
313 p_vids->p_bih->biHeight,
314 p_vids->p_bih->biBitCount,
315 (float)tk->i_rate/(float)tk->i_scale );
318 msg_Warn( p_input, "stream[%d] unknown type", i );
322 tk->p_es = es_out_Add( p_input->p_es_out, &fmt );
323 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
326 if( p_sys->i_track <= 0 )
328 msg_Err( p_input, "No valid track" );
332 if( config_GetInt( p_input, "avi-index" ) )
334 if( p_sys->b_seekable )
336 AVI_IndexCreate( p_input );
340 msg_Warn( p_input, "cannot create index (unseekable stream)" );
341 AVI_IndexLoad( p_input );
346 AVI_IndexLoad( p_input );
349 /* *** movie length in sec *** */
350 p_sys->i_length = AVI_MovieGetLength( p_input );
351 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
352 (mtime_t)p_avih->i_microsecperframe /
355 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
357 /* fix some BeOS MediaKit generated file */
358 for( i = 0 ; i < p_sys->i_track; i++ )
360 avi_track_t *tk = p_sys->track[i];
361 avi_chunk_list_t *p_strl;
362 avi_chunk_strh_t *p_strh;
363 avi_chunk_strf_auds_t *p_auds;
365 if( tk->i_cat != AUDIO_ES )
369 if( tk->i_idxnb < 1 ||
371 tk->i_samplesize != 0 )
375 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
376 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
377 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
379 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
380 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
382 int64_t i_track_length =
383 tk->p_index[tk->i_idxnb-1].i_length +
384 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
385 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
386 (mtime_t)p_avih->i_microsecperframe;
390 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
393 tk->i_samplesize = 1;
394 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
395 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
399 if( p_sys->i_length )
401 p_input->stream.i_mux_rate =
402 stream_Size( p_input->s ) / 50 / p_sys->i_length;
405 if( p_sys->b_seekable )
407 /* we have read all chunk so go back to movi */
408 stream_Seek( p_input->s, p_movi->i_chunk_pos );
410 /* Skip movi header */
411 stream_Read( p_input->s, NULL, 12 );
413 p_sys->i_movi_begin = p_movi->i_chunk_pos;
417 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
422 /*****************************************************************************
423 * Close: frees unused data
424 *****************************************************************************/
425 static void Close ( vlc_object_t * p_this )
427 input_thread_t * p_input = (input_thread_t *)p_this;
429 demux_sys_t *p_sys = p_input->p_demux_data ;
431 for( i = 0; i < p_sys->i_track; i++ )
433 if( p_sys->track[i] )
435 FREE( p_sys->track[i]->p_index );
436 free( p_sys->track[i] );
439 FREE( p_sys->track );
440 AVI_ChunkFreeRoot( p_input->s, &p_sys->ck_root );
445 /*****************************************************************************
446 * Demux_Seekable: reads and demuxes data packets for stream seekable
447 *****************************************************************************
448 * AVIDemux: reads and demuxes data packets
449 *****************************************************************************
450 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
451 *****************************************************************************/
458 off_t i_posf; /* where we will read :
459 if i_idxposb == 0 : begining of chunk (+8 to acces data)
460 else : point on data directly */
461 } avi_track_toread_t;
463 static int Demux_Seekable( input_thread_t *p_input )
465 demux_sys_t *p_sys = p_input->p_demux_data;
467 unsigned int i_track_count = 0;
468 unsigned int i_track;
470 vlc_bool_t b_play_audio;
471 /* cannot be more than 100 stream (dcXX or wbXX) */
472 avi_track_toread_t toread[100];
475 /* detect new selected/unselected streams */
476 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
478 avi_track_t *tk = p_sys->track[i_track];
481 es_out_Control( p_input->p_es_out, ES_OUT_GET_SELECT, tk->p_es, &b );
482 if( b && !tk->b_activated )
484 if( p_sys->b_seekable)
486 AVI_TrackSeek( p_input, i_track, p_sys->i_time );
488 tk->b_activated = VLC_TRUE;
490 else if( !b && tk->b_activated )
492 tk->b_activated = VLC_FALSE;
500 if( i_track_count <= 0 )
502 msg_Warn( p_input, "no track selected, exiting..." );
506 /* wait for the good time */
507 p_sys->i_pcr = p_sys->i_time * 9 / 100;
509 input_ClockManageRef( p_input,
510 p_input->stream.p_selected_program,
514 p_sys->i_time += 25*1000; /* read 25ms */
516 /* Check if we need to send the audio data to decoder */
517 b_play_audio = !p_input->stream.control.b_mute;
520 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
522 avi_track_t *tk = p_sys->track[i_track];
525 toread[i_track].b_ok = tk->b_activated;
526 if( tk->i_idxposc < tk->i_idxnb )
528 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
529 if( tk->i_idxposb > 0 )
531 toread[i_track].i_posf += 8 + tk->i_idxposb;
536 toread[i_track].i_posf = -1;
539 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
541 if( tk->i_samplesize )
543 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
547 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
552 toread[i_track].i_toread *= -1;
556 b_stream = VLC_FALSE;
567 /* search for first chunk to be read */
568 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
570 if( !toread[i].b_ok ||
571 AVI_GetDPTS( p_sys->track[i],
572 toread[i].i_toread ) <= -25 * 1000 )
577 if( toread[i].i_toread > 0 )
579 b_done = VLC_FALSE; /* not yet finished */
581 if( toread[i].i_posf > 0 )
583 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
586 i_pos = toread[i].i_posf;
598 /* no valid index, we will parse directly the stream
599 * in case we fail we will disable all finished stream */
600 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
602 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
603 if( AVI_PacketNext( p_input ) )
605 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
610 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
617 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
620 "cannot get packet header, track disabled" );
621 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
623 if( avi_pk.i_stream >= p_sys->i_track ||
624 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
626 if( AVI_PacketNext( p_input ) )
629 "cannot skip packet, track disabled" );
630 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
636 /* add this chunk to the index */
637 AVIIndexEntry_t index;
639 index.i_id = avi_pk.i_fourcc;
641 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
643 index.i_pos = avi_pk.i_pos;
644 index.i_length = avi_pk.i_size;
645 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
647 i_track = avi_pk.i_stream;
648 tk = p_sys->track[i_track];
649 /* do we will read this data ? */
650 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
656 if( AVI_PacketNext( p_input ) )
659 "cannot skip packet, track disabled" );
660 return( AVI_TrackStopFinishedStreams( p_input ) ? 0 : 1 );
669 stream_Seek( p_input->s, i_pos );
672 /* Set the track to use */
673 tk = p_sys->track[i_track];
675 /* read thoses data */
676 if( tk->i_samplesize )
678 unsigned int i_toread;
680 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
682 if( tk->i_samplesize > 1 )
684 i_toread = tk->i_samplesize;
688 i_toread = __MAX( AVI_PTSToByte( tk, 20 * 1000 ), 100 );
691 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
697 i_size = tk->p_index[tk->i_idxposc].i_length;
700 if( tk->i_idxposb == 0 )
702 i_size += 8; /* need to read and skip header */
705 if( ( p_pes = stream_PesPacket( p_input->s, __EVEN( i_size ) ) )==NULL )
707 msg_Warn( p_input, "failled reading data" );
708 tk->b_activated = VLC_FALSE;
709 toread[i_track].b_ok = VLC_FALSE;
712 if( i_size % 2 ) /* read was padded on word boundary */
714 p_pes->p_last->p_payload_end--;
718 if( tk->i_idxposb == 0 )
720 p_pes->p_first->p_payload_start += 8;
721 p_pes->i_pes_size -= 8;
724 p_pes->i_pts = AVI_GetPTS( tk );
727 /* fix pts for audio: ie pts sould be for the first byte of the first frame */
728 if( tk->i_samplesize == 1 )
730 AVI_FixPTS( p_stream, p_pes );
735 if( tk->i_samplesize )
737 if( tk->i_idxposb == 0 )
741 toread[i_track].i_toread -= i_size;
742 tk->i_idxposb += i_size;
744 tk->p_index[tk->i_idxposc].i_length )
752 toread[i_track].i_toread--;
756 if( tk->i_idxposc < tk->i_idxnb)
758 toread[i_track].i_posf =
759 tk->p_index[tk->i_idxposc].i_pos;
760 if( tk->i_idxposb > 0 )
762 toread[i_track].i_posf += 8 + tk->i_idxposb;
768 toread[i_track].i_posf = -1;
771 b_stream = VLC_TRUE; /* at least one read succeed */
775 input_ClockGetTS( p_input,
776 p_input->stream.p_selected_program,
777 p_pes->i_pts * 9/100);
778 p_pes->i_rate = p_input->stream.control.i_rate;
779 if( b_play_audio || tk->i_cat != AUDIO_ES )
781 es_out_Send( p_input->p_es_out, tk->p_es, p_pes );
785 input_DeletePES( p_input->p_method_data, p_pes );
791 /*****************************************************************************
792 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
793 *****************************************************************************
794 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
795 *****************************************************************************/
796 static int Demux_UnSeekable( input_thread_t *p_input )
798 demux_sys_t *p_sys = p_input->p_demux_data;
799 avi_track_t *p_stream_master = NULL;
801 unsigned int i_stream;
802 unsigned int i_packet;
804 /* Check if we need to send the audio data to decoder */
805 b_audio = !p_input->stream.control.b_mute;
807 input_ClockManageRef( p_input,
808 p_input->stream.p_selected_program,
811 /* *** find master stream for data packet skipping algo *** */
812 /* *** -> first video, if any, or first audio ES *** */
813 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
815 avi_track_t *tk = p_sys->track[i_stream];
818 es_out_Control( p_input->p_es_out, ES_OUT_GET_SELECT, tk->p_es, &b );
820 if( b && tk->i_cat == VIDEO_ES )
822 p_stream_master = tk;
826 p_stream_master = tk;
830 if( !p_stream_master )
832 msg_Warn( p_input, "no more stream selected" );
836 p_sys->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
838 for( i_packet = 0; i_packet < 10; i_packet++)
840 #define p_stream p_sys->track[avi_pk.i_stream]
844 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
849 if( avi_pk.i_stream >= p_sys->i_track ||
850 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
852 /* we haven't found an audio or video packet:
853 * - we have seek, found first next packet
854 * - others packets could be found, skip them
856 switch( avi_pk.i_fourcc )
861 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
865 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
867 return( 0 ); /* eof */
870 "seems to have lost position, resync" );
871 if( AVI_PacketSearch( p_input ) )
873 msg_Err( p_input, "resync failed" );
880 /* do will send this packet to decoder ? */
881 if( !b_audio && avi_pk.i_cat == AUDIO_ES )
883 if( AVI_PacketNext( p_input ) )
890 /* it's a selected stream, check for time */
891 if( __ABS( AVI_GetPTS( p_stream ) -
892 AVI_GetPTS( p_stream_master ) )< 600*1000 )
894 /* load it and send to decoder */
896 if( AVI_PacketRead( p_input, &avi_pk, &p_pes ) || !p_pes)
902 input_ClockGetTS( p_input,
903 p_input->stream.p_selected_program,
904 AVI_GetPTS( p_stream ) * 9/100);
906 p_pes->i_rate = p_input->stream.control.i_rate;
907 es_out_Send( p_input->p_es_out, p_stream->p_es, p_pes );
911 if( AVI_PacketNext( p_input ) )
918 /* *** update stream time position *** */
919 if( p_stream->i_samplesize )
921 p_stream->i_idxposb += avi_pk.i_size;
925 p_stream->i_idxposc++;
935 /*****************************************************************************
936 * Seek: goto to i_date or i_percent
937 *****************************************************************************
938 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
939 *****************************************************************************/
940 static int Seek( input_thread_t *p_input, mtime_t i_date, int i_percent )
943 demux_sys_t *p_sys = p_input->p_demux_data;
944 unsigned int i_stream;
946 "seek requested: "I64Fd" secondes %d%%",
950 if( p_sys->b_seekable )
952 if( !p_sys->i_length )
954 avi_track_t *p_stream;
957 /* use i_percent to create a true i_date */
959 "mmh, seeking without index at %d%%"
960 " work only for interleaved file", i_percent );
961 if( i_percent >= 100 )
963 msg_Warn( p_input, "cannot seek so far !" );
966 i_percent = __MAX( i_percent, 0 );
968 /* try to find chunk that is at i_percent or the file */
969 i_pos = __MAX( i_percent *
970 stream_Size( p_input->s ) / 100,
971 p_sys->i_movi_begin );
972 /* search first selected stream */
973 for( i_stream = 0, p_stream = NULL;
974 i_stream < p_sys->i_track; i_stream++ )
976 p_stream = p_sys->track[i_stream];
977 if( p_stream->b_activated )
982 if( !p_stream || !p_stream->b_activated )
984 msg_Warn( p_input, "cannot find any selected stream" );
988 /* be sure that the index exist */
989 if( AVI_StreamChunkSet( p_input,
993 msg_Warn( p_input, "cannot seek" );
997 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
998 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1000 /* search after i_idxposc */
1001 if( AVI_StreamChunkSet( p_input,
1002 i_stream, p_stream->i_idxposc + 1 ) )
1004 msg_Warn( p_input, "cannot seek" );
1008 i_date = AVI_GetPTS( p_stream );
1009 /* TODO better support for i_samplesize != 0 */
1010 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1013 #define p_stream p_sys->track[i_stream]
1015 /* seek for chunk based streams */
1016 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1018 if( p_stream->b_activated && !p_stream->i_samplesize )
1019 /* if( p_stream->b_activated ) */
1021 AVI_TrackSeek( p_input, i_stream, i_date );
1022 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1029 i_date = p_sys->i_time;
1031 /* seek for bytes based streams */
1032 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1034 if( p_stream->b_activated && p_stream->i_samplesize )
1036 AVI_TrackSeek( p_input, i_stream, i_date );
1037 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1040 msg_Dbg( p_input, "seek: "I64Fd" secondes", p_sys->i_time /1000000 );
1041 /* set true movie time */
1043 if( !p_sys->i_time )
1045 p_sys->i_time = i_date;
1052 msg_Err( p_input, "shouldn't yet be executed" );
1057 /*****************************************************************************
1059 *****************************************************************************
1061 *****************************************************************************/
1062 static double ControlGetPosition( input_thread_t *p_input )
1064 demux_sys_t *p_sys = p_input->p_demux_data;
1066 if( p_sys->i_length > 0 )
1068 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1070 else if( stream_Size( p_input->s ) > 0 )
1076 /* search the more advanced selected es */
1077 for( i = 0; i < p_sys->i_track; i++ )
1079 avi_track_t *tk = p_sys->track[i];
1080 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1082 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1083 tk->p_index[tk->i_idxposc].i_length + 8;
1090 return (double)i64 / (double)stream_Size( p_input->s );
1095 static int Control( input_thread_t *p_input, int i_query, va_list args )
1097 demux_sys_t *p_sys = p_input->p_demux_data;
1104 case DEMUX_GET_POSITION:
1105 pf = (double*)va_arg( args, double * );
1106 *pf = ControlGetPosition( p_input );
1108 case DEMUX_SET_POSITION:
1109 if( p_sys->b_seekable )
1111 f = (double)va_arg( args, double );
1112 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1113 return Seek( p_input, i64, (int)(f * 100) );
1115 return demux_vaControlDefault( p_input, i_query, args );
1117 case DEMUX_GET_TIME:
1118 pi64 = (int64_t*)va_arg( args, int64_t * );
1119 *pi64 = p_sys->i_time;
1122 case DEMUX_SET_TIME:
1126 i64 = (int64_t)va_arg( args, int64_t );
1127 if( p_sys->i_length > 0 )
1129 i_percent = 100 * i64 / (p_sys->i_length*1000000ULL);
1131 else if( p_sys->i_time > 0 )
1133 i_percent = (int)( 100.0 * ControlGetPosition( p_input ) *
1134 (double)i64 / (double)p_sys->i_time );
1136 return Seek( p_input, i64, i_percent );
1138 case DEMUX_GET_LENGTH:
1139 pi64 = (int64_t*)va_arg( args, int64_t * );
1140 *pi64 = p_sys->i_length * (mtime_t)1000000;
1144 pf = (double*)va_arg( args, double * );
1146 for( i = 0; i < (int)p_sys->i_track; i++ )
1148 avi_track_t *tk = p_sys->track[i];
1149 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1151 *pf = (float)tk->i_rate / (float)tk->i_scale;
1158 return demux_vaControlDefault( p_input, i_query, args );
1160 return VLC_EGENERIC;
1163 /*****************************************************************************
1164 * Function to convert pts to chunk or byte
1165 *****************************************************************************/
1167 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1169 return (mtime_t)((int64_t)i_pts *
1170 (int64_t)tk->i_rate /
1171 (int64_t)tk->i_scale /
1174 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1176 return (mtime_t)((int64_t)i_pts *
1177 (int64_t)tk->i_rate /
1178 (int64_t)tk->i_scale /
1180 (int64_t)tk->i_samplesize );
1183 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1187 i_dpts = (mtime_t)( (int64_t)1000000 *
1189 (int64_t)tk->i_scale /
1190 (int64_t)tk->i_rate );
1192 if( tk->i_samplesize )
1194 return i_dpts / tk->i_samplesize;
1199 static mtime_t AVI_GetPTS( avi_track_t *tk )
1201 if( tk->i_samplesize )
1203 int64_t i_count = 0;
1205 /* we need a valid entry we will emulate one */
1206 if( tk->i_idxposc == tk->i_idxnb )
1210 /* use the last entry */
1211 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1212 + tk->p_index[tk->i_idxnb - 1].i_length;
1217 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1219 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1223 return AVI_GetDPTS( tk, tk->i_idxposc );
1228 static void AVI_FixPTS( avi_track_t *p_stream, pes_packet_t *p_pes )
1230 data_packet_t *p_data;
1234 switch( p_stream->i_fourcc )
1236 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1237 p_data = p_pes->p_first;
1240 p = p_data->p_payload_start;
1241 while( p < p_data->p_payload_end - 2 )
1243 if( p[0] == 0xff && ( p[1]&0xe0) == 0xe0 )
1245 mtime_t i_diff = AVI_GetDPTS( p_stream, i_pos );
1246 p_pes->i_dts += i_diff;
1247 p_pes->i_pts += i_diff;
1252 p_data = p_data->p_next;
1255 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1256 p_data = p_pes->p_first;
1259 p = p_data->p_payload_start;
1260 while( p < p_data->p_payload_end - 2 )
1262 if( p[0] == 0x0b && p[1] == 0x77 )
1264 mtime_t i_diff = AVI_GetDPTS( p_stream, i_pos );
1265 p_pes->i_dts += i_diff;
1266 p_pes->i_pts += i_diff;
1270 p_data = p_data->p_next;
1274 /* we can't fix :( */
1280 static int AVI_StreamChunkFind( input_thread_t *p_input,
1281 unsigned int i_stream )
1283 demux_sys_t *p_sys = p_input->p_demux_data;
1284 avi_packet_t avi_pk;
1286 /* find first chunk of i_stream that isn't in index */
1288 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1290 stream_Seek( p_input->s, p_sys->i_movi_lastchunk_pos );
1291 if( AVI_PacketNext( p_input ) )
1293 return VLC_EGENERIC;
1298 stream_Seek( p_input->s, p_sys->i_movi_begin + 12 );
1304 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1306 msg_Warn( p_input, "cannot get packet header" );
1307 return VLC_EGENERIC;
1309 if( avi_pk.i_stream >= p_sys->i_track ||
1310 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1312 if( AVI_PacketNext( p_input ) )
1314 return VLC_EGENERIC;
1319 /* add this chunk to the index */
1320 AVIIndexEntry_t index;
1322 index.i_id = avi_pk.i_fourcc;
1324 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1326 index.i_pos = avi_pk.i_pos;
1327 index.i_length = avi_pk.i_size;
1328 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1330 if( avi_pk.i_stream == i_stream )
1335 if( AVI_PacketNext( p_input ) )
1337 return VLC_EGENERIC;
1344 /* be sure that i_ck will be a valid index entry */
1345 static int AVI_StreamChunkSet( input_thread_t *p_input,
1346 unsigned int i_stream,
1349 demux_sys_t *p_sys = p_input->p_demux_data;
1350 avi_track_t *p_stream = p_sys->track[i_stream];
1352 p_stream->i_idxposc = i_ck;
1353 p_stream->i_idxposb = 0;
1355 if( i_ck >= p_stream->i_idxnb )
1357 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1360 p_stream->i_idxposc++;
1361 if( AVI_StreamChunkFind( p_input, i_stream ) )
1363 return VLC_EGENERIC;
1366 } while( p_stream->i_idxposc < i_ck );
1373 /* XXX FIXME up to now, we assume that all chunk are one after one */
1374 static int AVI_StreamBytesSet( input_thread_t *p_input,
1375 unsigned int i_stream,
1378 demux_sys_t *p_sys = p_input->p_demux_data;
1379 avi_track_t *p_stream = p_sys->track[i_stream];
1381 if( ( p_stream->i_idxnb > 0 )
1382 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1383 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1385 /* index is valid to find the ck */
1386 /* uses dichototmie to be fast enougth */
1387 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1388 int i_idxmax = p_stream->i_idxnb;
1392 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1394 i_idxmax = i_idxposc ;
1395 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1399 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1400 p_stream->p_index[i_idxposc].i_length <= i_byte)
1402 i_idxmin = i_idxposc ;
1403 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1407 p_stream->i_idxposc = i_idxposc;
1408 p_stream->i_idxposb = i_byte -
1409 p_stream->p_index[i_idxposc].i_lengthtotal;
1418 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1419 p_stream->i_idxposb = 0;
1422 p_stream->i_idxposc++;
1423 if( AVI_StreamChunkFind( p_input, i_stream ) )
1425 return VLC_EGENERIC;
1428 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1429 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1431 p_stream->i_idxposb = i_byte -
1432 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1437 static int AVI_TrackSeek( input_thread_t *p_input,
1441 demux_sys_t *p_sys = p_input->p_demux_data;
1442 #define p_stream p_sys->track[i_stream]
1445 i_oldpts = AVI_GetPTS( p_stream );
1447 if( !p_stream->i_samplesize )
1449 if( AVI_StreamChunkSet( p_input,
1451 AVI_PTSToChunk( p_stream, i_date ) ) )
1453 return VLC_EGENERIC;
1457 "old:"I64Fd" %s new "I64Fd,
1459 i_oldpts > i_date ? ">" : "<",
1462 if( p_stream->i_cat == VIDEO_ES )
1464 /* search key frame */
1465 if( i_date < i_oldpts )
1467 while( p_stream->i_idxposc > 0 &&
1468 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1471 if( AVI_StreamChunkSet( p_input,
1473 p_stream->i_idxposc - 1 ) )
1475 return VLC_EGENERIC;
1481 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1482 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1485 if( AVI_StreamChunkSet( p_input,
1487 p_stream->i_idxposc + 1 ) )
1489 return VLC_EGENERIC;
1497 if( AVI_StreamBytesSet( p_input,
1499 AVI_PTSToByte( p_stream, i_date ) ) )
1501 return VLC_EGENERIC;
1508 /****************************************************************************
1509 * Return VLC_TRUE if it's a key frame
1510 ****************************************************************************/
1511 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1517 * startcode: 0x00000100 32bits
1518 * framenumber ? 5bits
1519 * piture type 0(I),1(P) 2bits
1521 if( GetDWBE( p_byte ) != 0x00000100 )
1523 /* it's not an msmpegv1 stream, strange...*/
1524 return AVIIF_KEYFRAME;
1528 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1531 case FOURCC_DIV3: /* wmv1 also */
1533 * picture type 0(I),1(P) 2bits
1535 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1537 /* we should find first occurence of 0x000001b6 (32bits)
1538 * startcode: 0x000001b6 32bits
1539 * piture type 0(I),1(P) 2bits
1541 if( GetDWBE( p_byte ) != 0x000001b6 )
1543 /* not true , need to find the first VOP header */
1544 return AVIIF_KEYFRAME;
1548 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1551 /* I can't do it, so say yes */
1552 return AVIIF_KEYFRAME;
1556 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1561 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1565 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1615 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1619 /****************************************************************************
1621 ****************************************************************************/
1622 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1623 int *pi_number, int *pi_type )
1625 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1628 c1 = ((uint8_t *)&i_id)[0];
1629 c2 = ((uint8_t *)&i_id)[1];
1631 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1633 SET_PTR( pi_number, 100 ); /* > max stream number */
1634 SET_PTR( pi_type, UNKNOWN_ES );
1638 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1639 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1642 SET_PTR( pi_type, AUDIO_ES );
1646 SET_PTR( pi_type, VIDEO_ES );
1649 SET_PTR( pi_type, UNKNOWN_ES );
1656 /****************************************************************************
1658 ****************************************************************************/
1659 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
1663 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1665 return VLC_EGENERIC;
1667 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1668 p_pk->i_size = GetDWLE( p_peek + 4 );
1669 p_pk->i_pos = stream_Tell( p_input->s );
1670 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1672 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1673 p_peek[10], p_peek[11] );
1680 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1682 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1686 static int AVI_PacketNext( input_thread_t *p_input )
1688 avi_packet_t avi_ck;
1691 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
1693 return VLC_EGENERIC;
1696 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1697 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1701 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1702 avi_ck.i_type == AVIFOURCC_AVIX )
1708 i_skip = __EVEN( avi_ck.i_size ) + 8;
1711 if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
1713 return VLC_EGENERIC;
1717 static int AVI_PacketRead( input_thread_t *p_input,
1719 pes_packet_t **pp_pes )
1723 i_size = __EVEN( p_pk->i_size + 8 );
1725 if( ( *pp_pes = stream_PesPacket( p_input->s, i_size ) ) == NULL )
1727 return VLC_EGENERIC;
1729 (*pp_pes)->p_first->p_payload_start += 8;
1730 (*pp_pes)->i_pes_size -= 8;
1732 if( i_size != p_pk->i_size + 8 )
1734 (*pp_pes)->p_last->p_payload_end--;
1735 (*pp_pes)->i_pes_size--;
1741 static int AVI_PacketSearch( input_thread_t *p_input )
1743 demux_sys_t *p_sys = p_input->p_demux_data;
1745 avi_packet_t avi_pk;
1748 if( stream_Read( p_input->s, NULL, 1 ) != 1 )
1750 return VLC_EGENERIC;
1752 AVI_PacketGetHeader( p_input, &avi_pk );
1753 if( avi_pk.i_stream < p_sys->i_track &&
1754 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1758 switch( avi_pk.i_fourcc )
1760 case AVIFOURCC_JUNK:
1761 case AVIFOURCC_LIST:
1762 case AVIFOURCC_RIFF:
1763 case AVIFOURCC_idx1:
1769 /****************************************************************************
1771 ****************************************************************************/
1772 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1774 AVIIndexEntry_t *p_index)
1776 avi_track_t *tk = p_sys->track[i_stream];
1778 /* Update i_movi_lastchunk_pos */
1779 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1781 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1785 if( tk->i_idxnb >= tk->i_idxmax )
1787 tk->i_idxmax += 16384;
1788 tk->p_index = realloc( tk->p_index,
1789 tk->i_idxmax * sizeof( AVIIndexEntry_t ) );
1790 if( tk->p_index == NULL )
1795 /* calculate cumulate length */
1796 if( tk->i_idxnb > 0 )
1798 p_index->i_lengthtotal =
1799 tk->p_index[tk->i_idxnb - 1].i_length +
1800 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1804 p_index->i_lengthtotal = 0;
1807 tk->p_index[tk->i_idxnb++] = *p_index;
1810 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
1812 demux_sys_t *p_sys = p_input->p_demux_data;
1814 avi_chunk_list_t *p_riff;
1815 avi_chunk_list_t *p_movi;
1816 avi_chunk_idx1_t *p_idx1;
1818 unsigned int i_stream;
1819 unsigned int i_index;
1823 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1824 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1825 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1829 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
1830 return VLC_EGENERIC;
1833 /* *** calculate offset *** */
1834 /* Well, avi is __SHIT__ so test more than one entry
1835 * (needed for some avi files) */
1837 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
1839 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
1841 i_offset = p_movi->i_chunk_pos + 8;
1846 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
1850 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
1853 if( i_stream < p_sys->i_track &&
1854 i_cat == p_sys->track[i_stream]->i_cat )
1856 AVIIndexEntry_t index;
1857 index.i_id = p_idx1->entry[i_index].i_fourcc;
1859 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
1860 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
1861 index.i_length = p_idx1->entry[i_index].i_length;
1862 AVI_IndexAddEntry( p_sys, i_stream, &index );
1868 static void __Parse_indx( input_thread_t *p_input,
1870 avi_chunk_indx_t *p_indx )
1872 demux_sys_t *p_sys = p_input->p_demux_data;
1873 AVIIndexEntry_t index;
1876 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
1877 if( p_indx->i_indexsubtype == 0 )
1879 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1881 index.i_id = p_indx->i_id;
1882 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1883 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
1884 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
1886 AVI_IndexAddEntry( p_sys, i_stream, &index );
1889 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
1891 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1893 index.i_id = p_indx->i_id;
1894 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
1895 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
1896 index.i_length = p_indx->idx.field[i].i_size;
1898 AVI_IndexAddEntry( p_sys, i_stream, &index );
1903 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
1907 static void AVI_IndexLoad_indx( input_thread_t *p_input )
1909 demux_sys_t *p_sys = p_input->p_demux_data;
1910 unsigned int i_stream;
1913 avi_chunk_list_t *p_riff;
1914 avi_chunk_list_t *p_hdrl;
1916 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1917 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
1919 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1921 avi_chunk_list_t *p_strl;
1922 avi_chunk_indx_t *p_indx;
1924 #define p_stream p_sys->track[i_stream]
1925 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
1926 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
1930 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
1934 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
1936 __Parse_indx( p_input, i_stream, p_indx );
1938 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
1940 avi_chunk_indx_t ck_sub;
1941 for( i = 0; i < p_indx->i_entriesinuse; i++ )
1943 if( stream_Seek( p_input->s, p_indx->idx.super[i].i_offset )||
1944 AVI_ChunkRead( p_input->s, &ck_sub, NULL ) )
1948 __Parse_indx( p_input, i_stream, &ck_sub );
1953 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
1959 static void AVI_IndexLoad( input_thread_t *p_input )
1961 demux_sys_t *p_sys = p_input->p_demux_data;
1962 unsigned int i_stream;
1964 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1966 p_sys->track[i_stream]->i_idxnb = 0;
1967 p_sys->track[i_stream]->i_idxmax = 0;
1968 p_sys->track[i_stream]->p_index = NULL;
1973 AVI_IndexLoad_indx( p_input );
1975 else if( AVI_IndexLoad_idx1( p_input ) )
1977 /* try indx if idx1 failed as some "normal" file have indx too */
1978 AVI_IndexLoad_indx( p_input );
1981 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1983 msg_Dbg( p_input, "stream[%d] created %d index entries",
1984 i_stream, p_sys->track[i_stream]->i_idxnb );
1988 static void AVI_IndexCreate( input_thread_t *p_input )
1990 demux_sys_t *p_sys = p_input->p_demux_data;
1992 avi_chunk_list_t *p_riff;
1993 avi_chunk_list_t *p_movi;
1995 unsigned int i_stream;
1998 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1999 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2003 msg_Err( p_input, "cannot find p_movi" );
2007 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2009 p_sys->track[i_stream]->i_idxnb = 0;
2010 p_sys->track[i_stream]->i_idxmax = 0;
2011 p_sys->track[i_stream]->p_index = NULL;
2013 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2014 stream_Size( p_input->s ) );
2016 stream_Seek( p_input->s, p_movi->i_chunk_pos + 12 );
2017 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
2022 if( AVI_PacketGetHeader( p_input, &pk ) )
2026 if( pk.i_stream < p_sys->i_track &&
2027 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2029 AVIIndexEntry_t index;
2030 index.i_id = pk.i_fourcc;
2032 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2033 index.i_pos = pk.i_pos;
2034 index.i_length = pk.i_size;
2035 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2039 switch( pk.i_fourcc )
2041 case AVIFOURCC_idx1:
2044 avi_chunk_list_t *p_sysx;
2045 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2046 AVIFOURCC_RIFF, 1 );
2048 msg_Dbg( p_input, "looking for new RIFF chunk" );
2049 if( stream_Seek( p_input->s, p_sysx->i_chunk_pos + 24))
2056 case AVIFOURCC_RIFF:
2057 msg_Dbg( p_input, "new RIFF chunk found" );
2059 case AVIFOURCC_JUNK:
2062 msg_Warn( p_input, "need resync, probably broken avi" );
2063 if( AVI_PacketSearch( p_input ) )
2065 msg_Warn( p_input, "lost sync, abord index creation" );
2071 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2072 AVI_PacketNext( p_input ) )
2079 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2082 "stream[%d] creating %d index entries",
2084 p_sys->track[i_stream]->i_idxnb );
2088 /*****************************************************************************
2090 *****************************************************************************/
2091 static int AVI_TrackStopFinishedStreams( input_thread_t *p_input )
2093 demux_sys_t *p_sys = p_input->p_demux_data;
2095 int b_end = VLC_TRUE;
2097 for( i = 0; i < p_sys->i_track; i++ )
2099 avi_track_t *tk = p_sys->track[i];
2100 if( tk->i_idxposc >= tk->i_idxnb )
2102 tk->b_activated = VLC_FALSE;
2112 /****************************************************************************
2113 * AVI_MovieGetLength give max streams length in second
2114 ****************************************************************************/
2115 static mtime_t AVI_MovieGetLength( input_thread_t *p_input )
2117 demux_sys_t *p_sys = p_input->p_demux_data;
2118 mtime_t i_maxlength = 0;
2121 for( i = 0; i < p_sys->i_track; i++ )
2123 avi_track_t *tk = p_sys->track[i];
2126 /* fix length for each stream */
2127 if( tk->i_idxnb < 1 || !tk->p_index )
2132 if( tk->i_samplesize )
2134 i_length = AVI_GetDPTS( tk,
2135 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2136 tk->p_index[tk->i_idxnb-1].i_length );
2140 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2142 i_length /= (mtime_t)1000000; /* in seconds */
2145 "stream[%d] length:"I64Fd" (based on index)",
2148 i_maxlength = __MAX( i_maxlength, i_length );