1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.9 2002/11/05 10:07:56 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() */
27 #include <string.h> /* strdup() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int AVIInit ( vlc_object_t * );
43 static void __AVIEnd ( vlc_object_t * );
44 static int AVISeek ( input_thread_t *, mtime_t, int );
45 static int AVIDemux_Seekable ( input_thread_t * );
46 static int AVIDemux_UnSeekable( input_thread_t *p_input );
48 #define AVIEnd(a) __AVIEnd(VLC_OBJECT(a))
50 /*****************************************************************************
52 *****************************************************************************/
54 add_category_hint( "demuxer", NULL );
55 add_bool( "avi-interleaved", 0, NULL,
56 "force interleaved method",
57 "force interleaved method" );
58 add_bool( "avi-index", 0, NULL,
59 "force index creation",
60 "force index creation" );
62 set_description( "avi demuxer" );
63 set_capability( "demux", 212 );
64 set_callbacks( AVIInit, __AVIEnd );
67 /*****************************************************************************
68 * Some usefull functions to manipulate memory
69 *****************************************************************************/
71 static u16 GetWLE( byte_t *p_buff )
73 return( p_buff[0] + ( p_buff[1] << 8 ) );
75 static u32 GetDWLE( byte_t *p_buff )
77 return( p_buff[0] + ( p_buff[1] << 8 ) +
78 ( p_buff[2] << 16 ) + ( p_buff[3] << 24 ) );
80 static u32 GetDWBE( byte_t *p_buff )
82 return( p_buff[3] + ( p_buff[2] << 8 ) +
83 ( p_buff[1] << 16 ) + ( p_buff[0] << 24 ) );
85 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
87 return( VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] ) );
90 static inline off_t __EVEN( off_t i )
92 return( (i & 1) ? i+1 : i );
95 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
97 /* read data in a pes */
98 static int input_ReadInPES( input_thread_t *p_input,
99 pes_packet_t **pp_pes,
103 data_packet_t *p_data;
106 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
118 input_NewPacket( p_input->p_method_data, 0 );
119 p_pes->i_nb_data = 1;
120 p_pes->i_pes_size = 0;
124 p_pes->i_nb_data = 0;
125 p_pes->i_pes_size = 0;
127 while( p_pes->i_pes_size < i_size )
131 i_read = input_SplitBuffer(p_input,
134 p_pes->i_pes_size, 1024 ) );
137 return( p_pes->i_pes_size );
140 if( !p_pes->p_first )
142 p_pes->p_first = p_data;
146 p_pes->p_last->p_next = p_data;
148 p_pes->p_last = p_data;
150 p_pes->i_pes_size += i_read;
154 return( p_pes->i_pes_size );
157 /* Test if it seems that it's a key frame */
158 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, u8 *p_byte )
164 startcode: 0x00000100 32bits
166 piture type 0(I),1(P) 2bits
168 if( GetDWBE( p_byte ) != 0x00000100 )
170 /* it's not an msmpegv1 stream, strange...*/
171 return( AVIIF_KEYFRAME );
175 return( p_byte[4]&0x06 ? 0 : AVIIF_KEYFRAME);
178 case FOURCC_DIV3: // wmv1 also
180 picture type 0(I),1(P) 2bits
182 return( p_byte[0]&0xC0 ? 0 : AVIIF_KEYFRAME );
184 /* we should find first occurence of 0x000001b6 (32bits)
185 startcode: 0x000001b6 32bits
186 piture type 0(I),1(P) 2bits
188 if( GetDWBE( p_byte ) != 0x000001b6 )
190 /* not true , need to find the first VOP header */
191 return( AVIIF_KEYFRAME );
195 return( p_byte[4]&0xC0 ? 0 : AVIIF_KEYFRAME );
198 /* I can't do it, so said yes */
199 return( AVIIF_KEYFRAME );
203 vlc_fourcc_t AVI_FourccGetCodec( int i_cat, vlc_fourcc_t i_codec )
210 case( WAVE_FORMAT_PCM ):
211 return( VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
212 case( WAVE_FORMAT_MPEG ):
213 case( WAVE_FORMAT_MPEGLAYER3 ):
214 return( VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
215 case( WAVE_FORMAT_A52 ):
216 return( VLC_FOURCC( 'a', '5', '2', ' ' ) );
217 case( WAVE_FORMAT_WMA1 ):
218 return( VLC_FOURCC( 'w', 'm', 'a', '1' ) );
219 case( WAVE_FORMAT_WMA2 ):
220 return( VLC_FOURCC( 'w', 'm', 'a', '2' ) );
222 return( VLC_FOURCC( 'm', 's',
223 ( i_codec >> 8 )&0xff,
227 // XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4
234 return( FOURCC_DIV1 );
241 return( FOURCC_DIV2 );
254 return( FOURCC_DIV3 );
267 return( FOURCC_mp4v );
270 return( VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
274 static void AVI_ParseStreamHeader( u32 i_id, int *pi_number, int *pi_type )
276 #define SET_PTR( p, v ) if( p ) *(p) = (v);
278 /* XXX i_id have to be read using MKFOURCC and NOT VLC_FOURCC */
279 c1 = ( i_id ) & 0xFF;
280 c2 = ( i_id >> 8 ) & 0xFF;
282 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
284 SET_PTR( pi_number, 100); /* > max stream number */
285 SET_PTR( pi_type, UNKNOWN_ES);
289 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
290 switch( ( i_id >> 16 ) & 0xFFFF )
293 SET_PTR( pi_type, AUDIO_ES );
297 SET_PTR( pi_type, VIDEO_ES);
300 SET_PTR( pi_type, UNKNOWN_ES );
307 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
311 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
315 p_pk->i_fourcc = GetDWLE( p_peek );
316 p_pk->i_size = GetDWLE( p_peek + 4 );
317 p_pk->i_pos = AVI_TellAbsolute( p_input );
318 if( p_pk->i_fourcc == AVIFOURCC_LIST )
320 p_pk->i_type = GetDWLE( p_peek + 8 );
327 memcpy( p_pk->i_peek, p_peek + 8, 8 );
329 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
333 static int AVI_PacketNext( input_thread_t *p_input )
337 if( !AVI_PacketGetHeader( p_input, &avi_ck ) )
341 if( avi_ck.i_fourcc == AVIFOURCC_LIST && avi_ck.i_type == AVIFOURCC_rec )
343 return( AVI_SkipBytes( p_input, 12 ) );
347 return( AVI_SkipBytes( p_input, __EVEN( avi_ck.i_size ) + 8 ));
350 static int AVI_PacketRead( input_thread_t *p_input,
352 pes_packet_t **pp_pes )
357 i_size = __EVEN( p_pk->i_size + 8 );
358 b_pad = ( i_size != p_pk->i_size + 8 );
360 if( input_ReadInPES( p_input, pp_pes, i_size ) != i_size )
364 (*pp_pes)->p_first->p_payload_start += 8;
365 (*pp_pes)->i_pes_size -= 8;
369 (*pp_pes)->p_last->p_payload_end--;
370 (*pp_pes)->i_pes_size--;
376 static int AVI_PacketSearch( input_thread_t *p_input )
378 demux_sys_t *p_avi = p_input->p_demux_data;
383 if( !AVI_SkipBytes( p_input, 1 ) )
387 AVI_PacketGetHeader( p_input, &avi_pk );
388 if( avi_pk.i_stream < p_avi->i_streams &&
389 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
393 switch( avi_pk.i_fourcc )
404 static void __AVI_AddEntryIndex( avi_stream_t *p_info,
405 AVIIndexEntry_t *p_index)
407 if( p_info->p_index == NULL )
409 p_info->i_idxmax = 16384;
411 if( !( p_info->p_index = calloc( p_info->i_idxmax,
412 sizeof( AVIIndexEntry_t ) ) ) )
417 if( p_info->i_idxnb >= p_info->i_idxmax )
419 p_info->i_idxmax += 16384;
420 if( !( p_info->p_index = realloc( (void*)p_info->p_index,
422 sizeof( AVIIndexEntry_t ) ) ) )
427 /* calculate cumulate length */
428 if( p_info->i_idxnb > 0 )
430 p_index->i_lengthtotal =
431 p_info->p_index[p_info->i_idxnb - 1].i_length +
432 p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal;
436 p_index->i_lengthtotal = 0;
439 p_info->p_index[p_info->i_idxnb] = *p_index;
444 static void AVI_IndexAddEntry( demux_sys_t *p_avi,
446 AVIIndexEntry_t *p_index)
448 __AVI_AddEntryIndex( p_avi->pp_info[i_stream],
450 if( p_avi->i_movi_lastchunk_pos < p_index->i_pos )
452 p_avi->i_movi_lastchunk_pos = p_index->i_pos;
456 static void AVI_IndexLoad( input_thread_t *p_input )
458 demux_sys_t *p_avi = p_input->p_demux_data;
460 avi_chunk_list_t *p_riff;
461 avi_chunk_list_t *p_movi;
462 avi_chunk_idx1_t *p_idx1;
468 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
471 p_idx1 = (avi_chunk_idx1_t*)AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
472 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
476 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
479 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
481 p_avi->pp_info[i_stream]->i_idxnb = 0;
482 p_avi->pp_info[i_stream]->i_idxmax = 0;
483 p_avi->pp_info[i_stream]->p_index = NULL;
485 /* *** calculate offset *** */
486 if( p_idx1->i_entry_count > 0 &&
487 p_idx1->entry[0].i_pos < p_movi->i_chunk_pos )
489 i_offset = p_movi->i_chunk_pos + 8;
496 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
500 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
503 if( i_stream < p_avi->i_streams &&
504 i_cat == p_avi->pp_info[i_stream]->i_cat )
506 AVIIndexEntry_t index;
507 index.i_id = p_idx1->entry[i_index].i_fourcc;
509 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
510 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
511 index.i_length = p_idx1->entry[i_index].i_length;
512 AVI_IndexAddEntry( p_avi, i_stream, &index );
515 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
518 "stream[%d] creating %d index entries",
520 p_avi->pp_info[i_stream]->i_idxnb );
525 static void AVI_IndexCreate( input_thread_t *p_input )
527 demux_sys_t *p_avi = p_input->p_demux_data;
529 avi_chunk_list_t *p_riff;
530 avi_chunk_list_t *p_movi;
535 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
537 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
541 msg_Err( p_input, "cannot find p_movi" );
545 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
547 p_avi->pp_info[i_stream]->i_idxnb = 0;
548 p_avi->pp_info[i_stream]->i_idxmax = 0;
549 p_avi->pp_info[i_stream]->p_index = NULL;
551 i_movi_end = __MIN( p_movi->i_chunk_pos + p_movi->i_chunk_size,
552 p_input->stream.p_selected_area->i_size );
554 AVI_SeekAbsolute( p_input, p_movi->i_chunk_pos + 12);
555 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
560 if( !AVI_PacketGetHeader( p_input, &pk ) )
564 if( pk.i_stream < p_avi->i_streams &&
565 pk.i_cat == p_avi->pp_info[pk.i_stream]->i_cat )
567 AVIIndexEntry_t index;
568 index.i_id = pk.i_fourcc;
570 AVI_GetKeyFlag(p_avi->pp_info[pk.i_stream]->i_codec, pk.i_peek);
571 index.i_pos = pk.i_pos;
572 index.i_length = pk.i_size;
573 AVI_IndexAddEntry( p_avi, pk.i_stream, &index );
577 switch( pk.i_fourcc )
585 msg_Warn( p_input, "need resync, probably broken avi" );
586 if( !AVI_PacketSearch( p_input ) )
588 msg_Warn( p_input, "lost sync, abord index creation" );
593 if( pk.i_pos + pk.i_size >= i_movi_end ||
594 !AVI_PacketNext( p_input ) )
601 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
604 "stream[%d] creating %d index entries",
606 p_avi->pp_info[i_stream]->i_idxnb );
611 /*****************************************************************************
613 *****************************************************************************/
614 static int AVI_StreamStart ( input_thread_t *, demux_sys_t *, int );
615 static int AVI_StreamSeek ( input_thread_t *, demux_sys_t *, int, mtime_t );
616 static void AVI_StreamStop ( input_thread_t *, demux_sys_t *, int );
618 static int AVI_StreamStart( input_thread_t *p_input,
619 demux_sys_t *p_avi, int i_stream )
621 #define p_stream p_avi->pp_info[i_stream]
622 if( !p_stream->p_es )
624 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
627 if( p_stream->i_activated )
629 msg_Warn( p_input, "stream[%d] already selected", i_stream );
633 if( !p_stream->p_es->p_decoder_fifo )
635 vlc_mutex_lock( &p_input->stream.stream_lock );
636 input_SelectES( p_input, p_stream->p_es );
637 vlc_mutex_unlock( &p_input->stream.stream_lock );
639 p_stream->i_activated = p_stream->p_es->p_decoder_fifo ? 1 : 0;
640 if( p_stream->i_activated )
642 AVI_StreamSeek( p_input, p_avi, i_stream, p_avi->i_time );
645 return( p_stream->i_activated );
649 static void AVI_StreamStop( input_thread_t *p_input,
650 demux_sys_t *p_avi, int i_stream )
652 #define p_stream p_avi->pp_info[i_stream]
654 if( !p_stream->i_activated )
656 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
660 if( p_stream->p_es->p_decoder_fifo )
662 vlc_mutex_lock( &p_input->stream.stream_lock );
663 input_UnselectES( p_input, p_stream->p_es );
664 vlc_mutex_unlock( &p_input->stream.stream_lock );
668 p_stream->i_activated = 0;
673 /****************************************************************************
674 * AVI_MovieGetLength give max streams length in second
675 ****************************************************************************/
676 static mtime_t AVI_MovieGetLength( input_thread_t *p_input, demux_sys_t *p_avi )
682 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
684 #define p_stream p_avi->pp_info[i_stream]
686 /* fix length for each stream */
687 if( p_stream->i_idxnb < 1 || !p_stream->p_index )
692 if( p_stream->i_samplesize )
695 (mtime_t)( p_stream->p_index[p_stream->i_idxnb-1].i_lengthtotal +
696 p_stream->p_index[p_stream->i_idxnb-1].i_length ) /
697 (mtime_t)p_stream->i_scale /
698 (mtime_t)p_stream->i_rate /
699 (mtime_t)p_stream->i_samplesize;
703 i_length = (mtime_t)p_stream->i_idxnb *
704 (mtime_t)p_stream->i_scale /
705 (mtime_t)p_stream->i_rate;
709 "stream[%d] length:%lld (based on index)",
712 i_maxlength = __MAX( i_maxlength, i_length );
716 return( i_maxlength );
719 /*****************************************************************************
720 * AVIEnd: frees unused data
721 *****************************************************************************/
722 static void __AVIEnd ( vlc_object_t * p_this )
724 input_thread_t * p_input = (input_thread_t *)p_this;
726 demux_sys_t *p_avi = p_input->p_demux_data ;
730 for( i = 0; i < p_avi->i_streams; i++ )
732 if( p_avi->pp_info[i] )
734 if( p_avi->pp_info[i]->p_index )
736 free( p_avi->pp_info[i]->p_index );
738 free( p_avi->pp_info[i] );
741 free( p_avi->pp_info );
743 AVI_ChunkFreeRoot( p_input, &p_avi->ck_root );
746 /*****************************************************************************
747 * AVIInit: check file and initializes AVI structures
748 *****************************************************************************/
749 static int AVIInit( vlc_object_t * p_this )
751 input_thread_t * p_input = (input_thread_t *)p_this;
753 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
754 avi_chunk_list_t *p_hdrl, *p_movi;
756 avi_chunk_list_t *p_INFO;
757 avi_chunk_strz_t *p_name;
759 avi_chunk_avih_t *p_avih;
761 es_descriptor_t *p_es = NULL; /* avoid warning */
764 int b_stream_audio, b_stream_video;
766 p_input->pf_demux = AVIDemux_Seekable;
767 if( !AVI_TestFile( p_input ) )
769 msg_Warn( p_input, "avi module discarded (invalid headr)" );
773 /* Initialize access plug-in structures. */
774 if( p_input->i_mtu == 0 )
777 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
780 if( !( p_input->p_demux_data =
781 p_avi = malloc( sizeof(demux_sys_t) ) ) )
783 msg_Err( p_input, "out of memory" );
786 memset( p_avi, 0, sizeof( demux_sys_t ) );
789 p_avi->b_seekable = ( ( p_input->stream.b_seekable )
790 &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
791 p_avi->i_movi_lastchunk_pos = 0;
793 /* *** for unseekable stream, automaticaly use AVIDemux_interleaved *** */
794 if( !p_avi->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
796 p_input->pf_demux = AVIDemux_UnSeekable;
799 if( !AVI_ChunkReadRoot( p_input, &p_avi->ck_root, p_avi->b_seekable ) )
801 msg_Err( p_input, "avi module discarded (invalid file)" );
804 AVI_ChunkDumpDebug( p_input, &p_avi->ck_root );
807 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
809 p_hdrl = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
811 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
814 p_INFO = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
816 p_name = (avi_chunk_strz_t*)AVI_ChunkFind( p_INFO,
824 if( !p_hdrl || !p_movi )
826 msg_Err( p_input, "avi module discarded (invalid file)" );
830 if( !( p_avih = (avi_chunk_avih_t*)AVI_ChunkFind( p_hdrl,
831 AVIFOURCC_avih, 0 ) ) )
833 msg_Err( p_input, "cannot find avih chunk" );
836 p_avi->i_streams = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
837 if( p_avih->i_streams != p_avi->i_streams )
840 "found %d stream but %d are declared",
844 if( p_avi->i_streams == 0 )
847 msg_Err( p_input, "no stream defined!" );
851 /* create one program */
852 vlc_mutex_lock( &p_input->stream.stream_lock );
853 if( input_InitStream( p_input, 0 ) == -1)
855 vlc_mutex_unlock( &p_input->stream.stream_lock );
857 msg_Err( p_input, "cannot init stream" );
860 if( input_AddProgram( p_input, 0, 0) == NULL )
862 vlc_mutex_unlock( &p_input->stream.stream_lock );
864 msg_Err( p_input, "cannot add program" );
867 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
868 vlc_mutex_unlock( &p_input->stream.stream_lock );
870 /* print informations on streams */
871 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
873 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
874 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
875 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
876 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
878 /* now read info on each stream and create ES */
879 p_avi->pp_info = calloc( p_avi->i_streams,
880 sizeof( avi_stream_t* ) );
881 memset( p_avi->pp_info,
883 sizeof( avi_stream_t* ) * p_avi->i_streams );
885 for( i = 0 ; i < p_avi->i_streams; i++ )
887 avi_chunk_list_t *p_avi_strl;
888 avi_chunk_strh_t *p_avi_strh;
889 avi_chunk_strf_auds_t *p_avi_strf_auds;
890 avi_chunk_strf_vids_t *p_avi_strf_vids;
893 #define p_info p_avi->pp_info[i]
894 p_info = malloc( sizeof(avi_stream_t ) );
895 memset( p_info, 0, sizeof( avi_stream_t ) );
897 p_avi_strl = (avi_chunk_list_t*)AVI_ChunkFind( p_hdrl,
899 p_avi_strh = (avi_chunk_strh_t*)AVI_ChunkFind( p_avi_strl,
901 p_avi_strf_auds = (avi_chunk_strf_auds_t*)
902 p_avi_strf_vids = (avi_chunk_strf_vids_t*)
903 AVI_ChunkFind( p_avi_strl, AVIFOURCC_strf, 0 );
905 if( !p_avi_strl || !p_avi_strh ||
906 ( !p_avi_strf_auds && !p_avi_strf_vids ) )
908 msg_Warn( p_input, "stream[%d] incomlete", i );
912 /* *** Init p_info *** */
913 p_info->i_rate = p_avi_strh->i_rate;
914 p_info->i_scale = p_avi_strh->i_scale;
915 p_info->i_samplesize = p_avi_strh->i_samplesize;
917 switch( p_avi_strh->i_type )
919 case( AVIFOURCC_auds ):
920 p_info->i_cat = AUDIO_ES;
922 AVI_FourccGetCodec( AUDIO_ES,
923 p_avi_strf_auds->i_formattag );
924 p_info->i_codec = p_info->i_fourcc;
925 i_init_size = p_avi_strf_auds->i_chunk_size;
926 p_init_data = p_avi_strf_auds->p_wfx;
927 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
929 p_avi_strf_auds->i_formattag,
930 p_avi_strf_auds->i_channels,
931 p_avi_strf_auds->i_samplespersec,
932 p_avi_strf_auds->i_bitspersample );
935 case( AVIFOURCC_vids ):
936 p_info->i_cat = VIDEO_ES;
937 /* XXX quick hack for playing ffmpeg video, I don't know
938 who is doing something wrong */
939 p_info->i_samplesize = 0;
940 p_info->i_fourcc = p_avi_strf_vids->p_bih->biCompression;
942 AVI_FourccGetCodec( VIDEO_ES, p_info->i_fourcc );
943 i_init_size = p_avi_strf_vids->i_chunk_size;
944 p_init_data = p_avi_strf_vids->p_bih;
945 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
947 (char*)&p_avi_strf_vids->p_bih->biCompression,
948 p_avi_strf_vids->p_bih->biWidth,
949 p_avi_strf_vids->p_bih->biHeight,
950 p_avi_strf_vids->p_bih->biBitCount,
951 (float)p_info->i_rate /
952 (float)p_info->i_scale );
955 msg_Err( p_input, "stream[%d] unknown type", i );
956 p_info->i_cat = UNKNOWN_ES;
961 p_info->i_activated = 0;
963 vlc_mutex_lock( &p_input->stream.stream_lock );
965 p_es = input_AddES( p_input,
966 p_input->stream.p_selected_program, 1+i,
968 vlc_mutex_unlock( &p_input->stream.stream_lock );
969 p_es->i_stream_id =i; /* XXX: i don't use it */
970 p_es->i_fourcc = p_info->i_fourcc;
971 p_es->i_cat = p_info->i_cat;
973 /* We copy strf for decoder in p_es->p_demux_data */
976 memcpy( p_es->p_demux_data,
982 if( config_GetInt( p_input, "avi-index" ) )
984 if( p_avi->b_seekable )
986 AVI_IndexCreate( p_input );
990 msg_Warn( p_input, "cannot create index (unseekable stream)" );
991 AVI_IndexLoad( p_input );
996 AVI_IndexLoad( p_input );
999 /* *** movie length in sec *** */
1001 p_avi->i_length = (mtime_t)p_avih->i_totalframes *
1002 (mtime_t)p_avih->i_microsecperframe /
1006 p_avi->i_length = AVI_MovieGetLength( p_input, p_avi );
1007 if( p_avi->i_length < (mtime_t)p_avih->i_totalframes *
1008 (mtime_t)p_avih->i_microsecperframe /
1011 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
1014 vlc_mutex_lock( &p_input->stream.stream_lock );
1015 if( p_avi->i_length )
1017 p_input->stream.i_mux_rate =
1018 p_input->stream.p_selected_area->i_size / 50 / p_avi->i_length;
1022 p_input->stream.i_mux_rate = 0;
1024 vlc_mutex_unlock( &p_input->stream.stream_lock );
1029 for( i = 0; i < p_avi->i_streams; i++ )
1031 #define p_info p_avi->pp_info[i]
1032 switch( p_info->p_es->i_cat )
1036 if( !b_stream_video )
1038 b_stream_video = AVI_StreamStart( p_input, p_avi, i );
1043 if( !b_stream_audio )
1045 b_stream_audio = AVI_StreamStart( p_input, p_avi, i );
1054 if( !b_stream_video )
1056 msg_Warn( p_input, "no video stream found" );
1058 if( !b_stream_audio )
1060 msg_Warn( p_input, "no audio stream found!" );
1063 vlc_mutex_lock( &p_input->stream.stream_lock );
1064 p_input->stream.p_selected_program->b_is_ok = 1;
1065 vlc_mutex_unlock( &p_input->stream.stream_lock );
1067 if( p_avi->b_seekable )
1069 AVI_ChunkGoto( p_input, p_movi );
1073 // already at begining of p_movi
1075 AVI_SkipBytes( p_input, 12 ); // enter in p_movi
1077 p_avi->i_movi_begin = p_movi->i_chunk_pos;
1084 /*****************************************************************************
1085 * Function to convert pts to chunk or byte
1086 *****************************************************************************/
1088 static inline mtime_t AVI_PTSToChunk( avi_stream_t *p_info,
1091 return( (mtime_t)((s64)i_pts *
1092 (s64)p_info->i_rate /
1093 (s64)p_info->i_scale /
1096 static inline mtime_t AVI_PTSToByte( avi_stream_t *p_info,
1099 return( (mtime_t)((s64)i_pts *
1100 (s64)p_info->i_samplesize *
1101 (s64)p_info->i_rate /
1102 (s64)p_info->i_scale /
1107 static mtime_t AVI_GetDPTS( avi_stream_t *p_stream, int i_count )
1109 if( p_stream->i_samplesize )
1111 return( (mtime_t)( (s64)1000000 *
1113 (s64)p_stream->i_scale /
1114 (s64)p_stream->i_rate /
1115 (s64)p_stream->i_samplesize ) );
1119 return( (mtime_t)( (s64)1000000 *
1121 (s64)p_stream->i_scale /
1122 (s64)p_stream->i_rate) );
1127 static mtime_t AVI_GetPTS( avi_stream_t *p_info )
1130 if( p_info->i_samplesize )
1132 /* we need a valid entry we will emulate one */
1134 if( p_info->i_idxposc == p_info->i_idxnb )
1136 if( p_info->i_idxposc )
1138 /* use the last entry */
1139 i_len = p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal
1140 + p_info->p_index[p_info->i_idxnb - 1].i_length
1141 + p_info->i_idxposb; /* should be 0 */
1145 i_len = p_info->i_idxposb;
1146 /* no valid entry use only offset*/
1151 i_len = p_info->p_index[p_info->i_idxposc].i_lengthtotal
1152 + p_info->i_idxposb;
1154 return( (mtime_t)( (s64)1000000 *
1156 (s64)p_info->i_scale /
1157 (s64)p_info->i_rate /
1158 (s64)p_info->i_samplesize ) );
1162 /* even if p_info->i_idxposc isn't valid, there isn't any problem */
1163 return( (mtime_t)( (s64)1000000 *
1164 (s64)(p_info->i_idxposc ) *
1165 (s64)p_info->i_scale /
1166 (s64)p_info->i_rate) );
1170 static int AVI_StreamChunkFind( input_thread_t *p_input,
1173 demux_sys_t *p_avi = p_input->p_demux_data;
1174 avi_packet_t avi_pk;
1176 /* find first chunk of i_stream that isn't in index */
1178 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin )
1180 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
1181 if( !AVI_PacketNext( p_input ) )
1188 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin );
1194 if( !AVI_PacketGetHeader( p_input, &avi_pk ) )
1196 msg_Err( p_input, "cannot get packet header" );
1199 if( avi_pk.i_stream >= p_avi->i_streams ||
1200 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1202 switch( avi_pk.i_fourcc )
1204 case AVIFOURCC_LIST:
1205 AVI_SkipBytes( p_input, 12 );
1208 if( !AVI_PacketNext( p_input ) )
1217 /* add this chunk to the index */
1218 AVIIndexEntry_t index;
1220 index.i_id = avi_pk.i_fourcc;
1222 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
1224 index.i_pos = avi_pk.i_pos;
1225 index.i_length = avi_pk.i_size;
1226 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
1228 if( avi_pk.i_stream == i_stream )
1233 if( !AVI_PacketNext( p_input ) )
1242 /* be sure that i_ck will be a valid index entry */
1243 static int AVI_SetStreamChunk( input_thread_t *p_input,
1247 demux_sys_t *p_avi = p_input->p_demux_data;
1248 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1250 p_stream->i_idxposc = i_ck;
1251 p_stream->i_idxposb = 0;
1253 if( i_ck >= p_stream->i_idxnb )
1255 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1258 p_stream->i_idxposc++;
1259 if( !AVI_StreamChunkFind( p_input, i_stream ) )
1264 } while( p_stream->i_idxposc < i_ck );
1271 /* XXX FIXME up to now, we assume that all chunk are one after one */
1272 static int AVI_SetStreamBytes( input_thread_t *p_input,
1276 demux_sys_t *p_avi = p_input->p_demux_data;
1277 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1279 if( ( p_stream->i_idxnb > 0 )
1280 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1281 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1283 /* index is valid to find the ck */
1284 /* uses dichototmie to be fast enougth */
1285 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1286 int i_idxmax = p_stream->i_idxnb;
1290 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1292 i_idxmax = i_idxposc ;
1293 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1297 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1298 p_stream->p_index[i_idxposc].i_length <= i_byte)
1300 i_idxmin = i_idxposc ;
1301 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1305 p_stream->i_idxposc = i_idxposc;
1306 p_stream->i_idxposb = i_byte -
1307 p_stream->p_index[i_idxposc].i_lengthtotal;
1316 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1317 p_stream->i_idxposb = 0;
1320 p_stream->i_idxposc++;
1321 if( !AVI_StreamChunkFind( p_input, i_stream ) )
1326 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1327 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1329 p_stream->i_idxposb = i_byte -
1330 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1335 static int AVI_StreamSeek( input_thread_t *p_input,
1340 #define p_stream p_avi->pp_info[i_stream]
1343 i_oldpts = AVI_GetPTS( p_stream );
1345 if( !p_stream->i_samplesize )
1347 if( !AVI_SetStreamChunk( p_input,
1349 AVI_PTSToChunk( p_stream, i_date ) ) )
1354 /* search key frame */
1356 "old:%lld %s new %lld",
1358 i_oldpts > i_date ? ">" : "<",
1361 if( i_date < i_oldpts )
1363 while( p_stream->i_idxposc > 0 &&
1364 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1367 if( !AVI_SetStreamChunk( p_input,
1369 p_stream->i_idxposc - 1 ) )
1377 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1378 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1381 if( !AVI_SetStreamChunk( p_input,
1383 p_stream->i_idxposc + 1 ) )
1392 if( !AVI_SetStreamBytes( p_input,
1394 AVI_PTSToByte( p_stream, i_date ) ) )
1403 /*****************************************************************************
1404 * AVISeek: goto to i_date or i_percent
1405 *****************************************************************************
1406 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1407 *****************************************************************************/
1408 static int AVISeek ( input_thread_t *p_input,
1409 mtime_t i_date, int i_percent )
1412 demux_sys_t *p_avi = p_input->p_demux_data;
1415 "seek requested: %lld secondes %d%%",
1419 if( p_avi->b_seekable )
1421 if( !p_avi->i_length )
1423 avi_stream_t *p_stream;
1426 /* use i_percent to create a true i_date */
1428 "mmh, seeking without index at %d%%"
1429 " work only for interleaved file", i_percent );
1431 if( i_percent >= 100 )
1433 msg_Err( p_input, "cannot seek so far !" );
1436 i_percent = __MAX( i_percent, 0 );
1438 /* try to find chunk that is at i_percent or the file */
1439 i_pos = __MAX( i_percent *
1440 p_input->stream.p_selected_area->i_size / 100,
1441 p_avi->i_movi_begin );
1442 /* search first selected stream */
1443 for( i_stream = 0, p_stream = NULL;
1444 i_stream < p_avi->i_streams; i_stream++ )
1446 p_stream = p_avi->pp_info[i_stream];
1447 if( p_stream->i_activated )
1452 if( !p_stream || !p_stream->i_activated )
1454 msg_Err( p_input, "cannot find any selected stream" );
1458 /* be sure that the index exit */
1459 if( !AVI_SetStreamChunk( p_input,
1463 msg_Err( p_input, "cannot seek" );
1467 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1468 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1470 /* search after i_idxposc */
1471 if( !AVI_SetStreamChunk( p_input,
1472 i_stream, p_stream->i_idxposc + 1 ) )
1474 msg_Err( p_input, "cannot seek" );
1478 i_date = AVI_GetPTS( p_stream );
1479 /* TODO better support for i_samplesize != 0 */
1480 msg_Dbg( p_input, "estimate date %lld", i_date );
1483 #define p_stream p_avi->pp_info[i_stream]
1485 /* seek for chunk based streams */
1486 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1488 if( p_stream->i_activated && !p_stream->i_samplesize )
1489 // if( p_stream->i_activated )
1491 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1492 p_avi->i_time = __MAX( AVI_GetPTS( p_stream ),
1499 i_date = p_avi->i_time;
1501 /* seek for bytes based streams */
1502 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1504 if( p_stream->i_activated && p_stream->i_samplesize )
1506 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1507 // p_avi->i_time = __MAX( AVI_GetPTS( p_stream ), p_avi->i_time );
1510 msg_Dbg( p_input, "seek: %lld secondes", p_avi->i_time /1000000 );
1511 /* set true movie time */
1513 if( !p_avi->i_time )
1515 p_avi->i_time = i_date;
1522 msg_Err( p_input, "shouldn't yet be executed" );
1527 /*****************************************************************************
1528 * AVIDemux_Seekable: reads and demuxes data packets for stream seekable
1529 *****************************************************************************
1530 * AVIDemux: reads and demuxes data packets
1531 *****************************************************************************
1532 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1533 *****************************************************************************/
1534 typedef struct avi_stream_toread_s
1540 off_t i_posf; // where we will read :
1541 // if i_idxposb == 0 : begining of chunk (+8 to acces data)
1542 // else : point on data directly
1543 } avi_stream_toread_t;
1545 static int AVIDemux_Seekable( input_thread_t *p_input )
1550 // cannot be more than 100 stream (dcXX or wbXX)
1551 avi_stream_toread_t toread[100];
1553 demux_sys_t *p_avi = p_input->p_demux_data;
1555 /* detect new selected/unselected streams */
1556 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1558 #define p_stream p_avi->pp_info[i_stream]
1559 if( p_stream->p_es )
1561 if( p_stream->p_es->p_decoder_fifo &&
1562 !p_stream->i_activated )
1564 AVI_StreamStart( p_input, p_avi, i_stream );
1567 if( !p_stream->p_es->p_decoder_fifo &&
1568 p_stream->i_activated )
1570 AVI_StreamStop( p_input, p_avi, i_stream );
1576 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1580 /* first wait for empty buffer, arbitrary time FIXME */
1581 // msleep( DEFAULT_PTS_DELAY );
1583 i_date = (mtime_t)1000000 *
1584 (mtime_t)p_avi->i_length *
1585 (mtime_t)AVI_TellAbsolute( p_input ) /
1586 (mtime_t)p_input->stream.p_selected_area->i_size;
1587 i_percent = 100 * AVI_TellAbsolute( p_input ) /
1588 p_input->stream.p_selected_area->i_size;
1590 // input_ClockInit( p_input->stream.p_selected_program );
1591 AVISeek( p_input, i_date, i_percent);
1595 /* wait for the good time */
1597 p_avi->i_pcr = p_avi->i_time * 9 / 100;
1599 input_ClockManageRef( p_input,
1600 p_input->stream.p_selected_program,
1604 p_avi->i_time += 100*1000; /* read 100ms */
1607 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1609 #define p_stream p_avi->pp_info[i_stream]
1612 toread[i_stream].i_ok = p_stream->i_activated;
1614 if( p_stream->i_idxposc < p_stream->i_idxnb )
1616 toread[i_stream].i_posf =
1617 p_stream->p_index[p_stream->i_idxposc].i_pos;
1618 if( p_stream->i_idxposb > 0 )
1620 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
1625 toread[i_stream].i_posf = -1;
1628 i_dpts = p_avi->i_time - AVI_GetPTS( p_stream );
1630 if( p_stream->i_samplesize )
1632 toread[i_stream].i_toread = AVI_PTSToByte( p_stream,
1637 toread[i_stream].i_toread = AVI_PTSToChunk( p_stream,
1643 toread[i_stream].i_toread *= -1;
1652 #define p_stream p_avi->pp_info[i_stream]
1654 pes_packet_t *p_pes;
1659 /* search for first chunk to be read */
1660 for( i = 0, b_done = 1, i_pos = -1; i < p_avi->i_streams; i++ )
1662 if( !toread[i].i_ok ||
1663 AVI_GetDPTS( p_avi->pp_info[i],
1664 toread[i].i_toread ) <= -25 * 1000 )
1669 if( toread[i].i_toread > 0 )
1671 b_done = 0; // not yet finished
1674 if( toread[i].i_posf > 0 )
1676 if( i_pos == -1 || i_pos > toread[i_stream].i_posf )
1679 i_pos = toread[i].i_posf;
1686 // return( b_stream ? 1 : 0 );
1692 /* no valid index, we will parse directly the stream */
1693 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin )
1695 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
1696 if( !AVI_PacketNext( p_input ) )
1703 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin );
1708 avi_packet_t avi_pk;
1710 if( !AVI_PacketGetHeader( p_input, &avi_pk ) )
1712 msg_Err( p_input, "cannot get packet header" );
1715 if( avi_pk.i_stream >= p_avi->i_streams ||
1716 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1718 switch( avi_pk.i_fourcc )
1720 case AVIFOURCC_LIST:
1721 AVI_SkipBytes( p_input, 12 );
1724 if( !AVI_PacketNext( p_input ) )
1726 msg_Err( p_input, "cannot skip packet" );
1735 /* add this chunk to the index */
1736 AVIIndexEntry_t index;
1738 index.i_id = avi_pk.i_fourcc;
1740 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
1742 index.i_pos = avi_pk.i_pos;
1743 index.i_length = avi_pk.i_size;
1744 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
1746 i_stream = avi_pk.i_stream;
1747 /* do we will read this data ? */
1748 if( AVI_GetDPTS( p_stream,
1749 toread[i_stream].i_toread ) > -25 * 1000 )
1755 if( !AVI_PacketNext( p_input ) )
1757 msg_Err( p_input, "cannot skip packet" );
1767 AVI_SeekAbsolute( p_input, i_pos );
1770 /* read thoses data */
1771 if( p_stream->i_samplesize )
1773 i_size = __MIN( p_stream->p_index[p_stream->i_idxposc].i_length -
1774 p_stream->i_idxposb,
1775 100 * 1024 ); // 10Ko max
1776 // toread[i_stream].i_toread );
1780 i_size = p_stream->p_index[p_stream->i_idxposc].i_length;
1783 if( p_stream->i_idxposb == 0 )
1785 i_size += 8; // need to read and skip header
1788 if( input_ReadInPES( p_input, &p_pes, __EVEN( i_size ) ) < 0 )
1790 msg_Err( p_input, "failled reading data" );
1791 toread[i_stream].i_ok = 0;
1795 if( i_size % 2 ) // read was padded on word boundary
1797 p_pes->p_last->p_payload_end--;
1798 p_pes->i_pes_size--;
1801 if( p_stream->i_idxposb == 0 )
1803 p_pes->p_first->p_payload_start += 8;
1804 p_pes->i_pes_size -= 8;
1807 p_pes->i_pts = AVI_GetPTS( p_stream );
1810 if( p_stream->i_samplesize )
1812 if( p_stream->i_idxposb == 0 )
1816 toread[i_stream].i_toread -= i_size;
1817 p_stream->i_idxposb += i_size;
1818 if( p_stream->i_idxposb >=
1819 p_stream->p_index[p_stream->i_idxposc].i_length )
1821 p_stream->i_idxposb = 0;
1822 p_stream->i_idxposc++;
1827 toread[i_stream].i_toread--;
1828 p_stream->i_idxposc++;
1831 if( p_stream->i_idxposc < p_stream->i_idxnb)
1833 toread[i_stream].i_posf =
1834 p_stream->p_index[p_stream->i_idxposc].i_pos;
1835 if( p_stream->i_idxposb > 0 )
1837 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
1843 toread[i_stream].i_posf = -1;
1846 b_stream = 1; // at least one read succeed
1848 if( p_stream->p_es && p_stream->p_es->p_decoder_fifo )
1852 input_ClockGetTS( p_input,
1853 p_input->stream.p_selected_program,
1854 p_pes->i_pts * 9/100);
1856 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
1860 input_DeletePES( p_input->p_method_data, p_pes );
1867 /*****************************************************************************
1868 * AVIDemux_UnSeekable: reads and demuxes data packets for unseekable
1870 *****************************************************************************
1871 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1872 *****************************************************************************/
1873 static int AVIDemux_UnSeekable( input_thread_t *p_input )
1875 demux_sys_t *p_avi = p_input->p_demux_data;
1876 avi_stream_t *p_stream_master;
1881 /* *** send audio data to decoder only if rate == DEFAULT_RATE *** */
1882 vlc_mutex_lock( &p_input->stream.stream_lock );
1883 b_audio = p_input->stream.control.i_rate == DEFAULT_RATE;
1884 vlc_mutex_unlock( &p_input->stream.stream_lock );
1886 input_ClockManageRef( p_input,
1887 p_input->stream.p_selected_program,
1889 /* *** find master stream for data packet skipping algo *** */
1890 /* *** -> first video, if any, or first audio ES *** */
1891 for( i_stream = 0, p_stream_master = NULL;
1892 i_stream < p_avi->i_streams; i_stream++ )
1894 #define p_stream p_avi->pp_info[i_stream]
1895 if( p_stream->p_es &&
1896 p_stream->p_es->p_decoder_fifo )
1898 if( p_stream->i_cat == VIDEO_ES )
1900 p_stream_master = p_stream;
1903 if( p_stream->i_cat == AUDIO_ES && !p_stream_master )
1905 p_stream_master = p_stream;
1910 if( !p_stream_master )
1912 msg_Err( p_input, "no more stream selected" );
1916 p_avi->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
1918 for( i_packet = 0; i_packet < 10; i_packet++)
1920 #define p_stream p_avi->pp_info[avi_pk.i_stream]
1922 avi_packet_t avi_pk;
1924 if( !AVI_PacketGetHeader( p_input, &avi_pk ) )
1928 // AVI_ParseStreamHeader( avi_pk.i_fourcc, &i_stream, &i_cat );
1930 if( avi_pk.i_stream >= p_avi->i_streams ||
1931 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1933 /* we haven't found an audio or video packet:
1934 - we have seek, found first next packet
1935 - others packets could be found, skip them
1937 switch( avi_pk.i_fourcc )
1939 case AVIFOURCC_JUNK:
1940 case AVIFOURCC_LIST:
1941 return( AVI_PacketNext( p_input ) ? 1 : 0 );
1942 case AVIFOURCC_idx1:
1946 "seems to have lost position, resync" );
1947 if( !AVI_PacketSearch( p_input ) )
1949 msg_Err( p_input, "resync failed" );
1956 /* do will send this packet to decoder ? */
1957 if( ( !b_audio && avi_pk.i_cat == AUDIO_ES )||
1959 !p_stream->p_es->p_decoder_fifo )
1961 if( !AVI_PacketNext( p_input ) )
1968 /* it's a selected stream, check for time */
1969 if( __ABS( AVI_GetPTS( p_stream ) -
1970 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1972 /* load it and send to decoder */
1973 pes_packet_t *p_pes;
1974 if( !AVI_PacketRead( p_input, &avi_pk, &p_pes ) || !p_pes)
1979 input_ClockGetTS( p_input,
1980 p_input->stream.p_selected_program,
1981 AVI_GetPTS( p_stream ) * 9/100);
1982 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
1986 if( !AVI_PacketNext( p_input ) )
1993 /* *** update stream time position *** */
1994 if( p_stream->i_samplesize )
1996 p_stream->i_idxposb += avi_pk.i_size;
2000 p_stream->i_idxposc++;