1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.16 2002/12/04 15:47:31 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() */
27 #include <string.h> /* strdup() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
38 #define __AVI_SUBTITLE__ 1
40 #ifdef __AVI_SUBTITLE__
41 # include "../util/sub.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int AVIInit ( vlc_object_t * );
49 static void __AVIEnd ( vlc_object_t * );
50 static int AVISeek ( input_thread_t *, mtime_t, int );
51 static int AVIDemux_Seekable ( input_thread_t * );
52 static int AVIDemux_UnSeekable( input_thread_t *p_input );
54 #define AVIEnd(a) __AVIEnd(VLC_OBJECT(a))
56 /*****************************************************************************
58 *****************************************************************************/
60 add_category_hint( "demuxer", NULL );
61 add_bool( "avi-interleaved", 0, NULL,
62 "force interleaved method",
63 "force interleaved method" );
64 add_bool( "avi-index", 0, NULL,
65 "force index creation",
66 "force index creation" );
68 set_description( "avi demuxer" );
69 set_capability( "demux", 212 );
70 set_callbacks( AVIInit, __AVIEnd );
73 /*****************************************************************************
74 * Some useful functions to manipulate memory
75 *****************************************************************************/
77 static uint16_t GetWLE( uint8_t *p_buff )
79 return (uint16_t)p_buff[0] | ( ((uint16_t)p_buff[1]) << 8 );
82 static uint32_t GetDWLE( uint8_t *p_buff )
84 return (uint32_t)p_buff[0] | ( ((uint32_t)p_buff[1]) << 8 ) |
85 ( ((uint32_t)p_buff[2]) << 16 ) | ( ((uint32_t)p_buff[3]) << 24 );
88 static uint32_t GetDWBE( uint8_t *p_buff )
90 return (uint32_t)p_buff[3] | ( ((uint32_t)p_buff[2]) << 8 ) |
91 ( ((uint32_t)p_buff[1]) << 16 ) | ( ((uint32_t)p_buff[0]) << 24 );
93 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
95 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
98 static inline off_t __EVEN( off_t i )
100 return (i & 1) ? i + 1 : i;
103 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
105 /* read data in a pes */
106 static int input_ReadInPES( input_thread_t *p_input,
107 pes_packet_t **pp_pes,
111 data_packet_t *p_data;
114 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
126 input_NewPacket( p_input->p_method_data, 0 );
127 p_pes->i_nb_data = 1;
128 p_pes->i_pes_size = 0;
132 p_pes->i_nb_data = 0;
133 p_pes->i_pes_size = 0;
135 while( p_pes->i_pes_size < i_size )
139 i_read = input_SplitBuffer(p_input,
142 p_pes->i_pes_size, 1024 ) );
145 return p_pes->i_pes_size;
148 if( !p_pes->p_first )
150 p_pes->p_first = p_data;
154 p_pes->p_last->p_next = p_data;
156 p_pes->p_last = p_data;
158 p_pes->i_pes_size += i_read;
162 return p_pes->i_pes_size;
165 /* Test if it seems that it's a key frame */
166 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
172 * startcode: 0x00000100 32bits
173 * framenumber ? 5bits
174 * piture type 0(I),1(P) 2bits
176 if( GetDWBE( p_byte ) != 0x00000100 )
178 /* it's not an msmpegv1 stream, strange...*/
179 return AVIIF_KEYFRAME;
183 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
186 case FOURCC_DIV3: // wmv1 also
188 * picture type 0(I),1(P) 2bits
190 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
192 /* we should find first occurence of 0x000001b6 (32bits)
193 * startcode: 0x000001b6 32bits
194 * piture type 0(I),1(P) 2bits
196 if( GetDWBE( p_byte ) != 0x000001b6 )
198 /* not true , need to find the first VOP header */
199 return AVIIF_KEYFRAME;
203 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
206 /* I can't do it, so say yes */
207 return AVIIF_KEYFRAME;
211 vlc_fourcc_t AVI_FourccGetCodec( int i_cat, vlc_fourcc_t i_codec )
218 case WAVE_FORMAT_PCM:
219 return VLC_FOURCC( 'a', 'r', 'a', 'w' );
220 case WAVE_FORMAT_MPEG:
221 case WAVE_FORMAT_MPEGLAYER3:
222 return VLC_FOURCC( 'm', 'p', 'g', 'a' );
223 case WAVE_FORMAT_A52:
224 return VLC_FOURCC( 'a', '5', '2', ' ' );
225 case WAVE_FORMAT_WMA1:
226 return VLC_FOURCC( 'w', 'm', 'a', '1' );
227 case WAVE_FORMAT_WMA2:
228 return VLC_FOURCC( 'w', 'm', 'a', '2' );
230 return VLC_FOURCC( 'm', 's',
231 ( i_codec >> 8 )&0xff, i_codec&0xff );
234 // XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4
277 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
281 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
282 int *pi_number, int *pi_type )
284 #define SET_PTR( p, v ) if( p ) *(p) = (v);
287 c1 = ((uint8_t *)&i_id)[0];
288 c2 = ((uint8_t *)&i_id)[1];
290 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
292 SET_PTR( pi_number, 100 ); /* > max stream number */
293 SET_PTR( pi_type, UNKNOWN_ES );
297 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
298 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
301 SET_PTR( pi_type, AUDIO_ES );
305 SET_PTR( pi_type, VIDEO_ES );
308 SET_PTR( pi_type, UNKNOWN_ES );
315 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
319 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
323 p_pk->i_fourcc = GetFOURCC( p_peek );
324 p_pk->i_size = GetDWLE( p_peek + 4 );
325 p_pk->i_pos = AVI_TellAbsolute( p_input );
326 if( p_pk->i_fourcc == AVIFOURCC_LIST )
328 p_pk->i_type = GetFOURCC( p_peek + 8 );
335 memcpy( p_pk->i_peek, p_peek + 8, 8 );
337 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
341 static int AVI_PacketNext( input_thread_t *p_input )
345 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
349 if( avi_ck.i_fourcc == AVIFOURCC_LIST && avi_ck.i_type == AVIFOURCC_rec )
351 return AVI_SkipBytes( p_input, 12 );
355 return AVI_SkipBytes( p_input, __EVEN( avi_ck.i_size ) + 8 );
358 static int AVI_PacketRead( input_thread_t *p_input,
360 pes_packet_t **pp_pes )
365 i_size = __EVEN( p_pk->i_size + 8 );
366 b_pad = ( i_size != p_pk->i_size + 8 );
368 if( input_ReadInPES( p_input, pp_pes, i_size ) != i_size )
372 (*pp_pes)->p_first->p_payload_start += 8;
373 (*pp_pes)->i_pes_size -= 8;
377 (*pp_pes)->p_last->p_payload_end--;
378 (*pp_pes)->i_pes_size--;
384 static int AVI_PacketSearch( input_thread_t *p_input )
386 demux_sys_t *p_avi = p_input->p_demux_data;
391 if( AVI_SkipBytes( p_input, 1 ) )
395 AVI_PacketGetHeader( p_input, &avi_pk );
396 if( avi_pk.i_stream < p_avi->i_streams &&
397 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
401 switch( avi_pk.i_fourcc )
412 static void __AVI_AddEntryIndex( avi_stream_t *p_info,
413 AVIIndexEntry_t *p_index)
415 if( p_info->p_index == NULL )
417 p_info->i_idxmax = 16384;
419 if( !( p_info->p_index = calloc( p_info->i_idxmax,
420 sizeof( AVIIndexEntry_t ) ) ) )
425 if( p_info->i_idxnb >= p_info->i_idxmax )
427 p_info->i_idxmax += 16384;
428 if( !( p_info->p_index = realloc( (void*)p_info->p_index,
430 sizeof( AVIIndexEntry_t ) ) ) )
435 /* calculate cumulate length */
436 if( p_info->i_idxnb > 0 )
438 p_index->i_lengthtotal =
439 p_info->p_index[p_info->i_idxnb - 1].i_length +
440 p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal;
444 p_index->i_lengthtotal = 0;
447 p_info->p_index[p_info->i_idxnb] = *p_index;
452 static void AVI_IndexAddEntry( demux_sys_t *p_avi,
454 AVIIndexEntry_t *p_index)
456 __AVI_AddEntryIndex( p_avi->pp_info[i_stream],
458 if( p_avi->i_movi_lastchunk_pos < p_index->i_pos )
460 p_avi->i_movi_lastchunk_pos = p_index->i_pos;
464 static void AVI_IndexLoad( input_thread_t *p_input )
466 demux_sys_t *p_avi = p_input->p_demux_data;
468 avi_chunk_list_t *p_riff;
469 avi_chunk_list_t *p_movi;
470 avi_chunk_idx1_t *p_idx1;
476 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
479 p_idx1 = (avi_chunk_idx1_t*)AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
480 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
484 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
487 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
489 p_avi->pp_info[i_stream]->i_idxnb = 0;
490 p_avi->pp_info[i_stream]->i_idxmax = 0;
491 p_avi->pp_info[i_stream]->p_index = NULL;
493 /* *** calculate offset *** */
494 if( p_idx1->i_entry_count > 0 &&
495 p_idx1->entry[0].i_pos < p_movi->i_chunk_pos )
497 i_offset = p_movi->i_chunk_pos + 8;
504 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
508 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
511 if( i_stream < p_avi->i_streams &&
512 i_cat == p_avi->pp_info[i_stream]->i_cat )
514 AVIIndexEntry_t index;
515 index.i_id = p_idx1->entry[i_index].i_fourcc;
517 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
518 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
519 index.i_length = p_idx1->entry[i_index].i_length;
520 AVI_IndexAddEntry( p_avi, i_stream, &index );
523 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
526 "stream[%d] creating %d index entries",
528 p_avi->pp_info[i_stream]->i_idxnb );
533 static void AVI_IndexCreate( input_thread_t *p_input )
535 demux_sys_t *p_avi = p_input->p_demux_data;
537 avi_chunk_list_t *p_riff;
538 avi_chunk_list_t *p_movi;
543 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
545 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
549 msg_Err( p_input, "cannot find p_movi" );
553 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
555 p_avi->pp_info[i_stream]->i_idxnb = 0;
556 p_avi->pp_info[i_stream]->i_idxmax = 0;
557 p_avi->pp_info[i_stream]->p_index = NULL;
559 i_movi_end = __MIN( p_movi->i_chunk_pos + p_movi->i_chunk_size,
560 p_input->stream.p_selected_area->i_size );
562 AVI_SeekAbsolute( p_input, p_movi->i_chunk_pos + 12);
563 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
568 if( AVI_PacketGetHeader( p_input, &pk ) )
572 if( pk.i_stream < p_avi->i_streams &&
573 pk.i_cat == p_avi->pp_info[pk.i_stream]->i_cat )
575 AVIIndexEntry_t index;
576 index.i_id = pk.i_fourcc;
578 AVI_GetKeyFlag(p_avi->pp_info[pk.i_stream]->i_codec, pk.i_peek);
579 index.i_pos = pk.i_pos;
580 index.i_length = pk.i_size;
581 AVI_IndexAddEntry( p_avi, pk.i_stream, &index );
585 switch( pk.i_fourcc )
593 msg_Warn( p_input, "need resync, probably broken avi" );
594 if( AVI_PacketSearch( p_input ) )
596 msg_Warn( p_input, "lost sync, abord index creation" );
601 if( pk.i_pos + pk.i_size >= i_movi_end ||
602 AVI_PacketNext( p_input ) )
609 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
612 "stream[%d] creating %d index entries",
614 p_avi->pp_info[i_stream]->i_idxnb );
619 /*****************************************************************************
621 *****************************************************************************/
622 static vlc_bool_t AVI_StreamStart ( input_thread_t *, demux_sys_t *, int );
623 static int AVI_StreamSeek ( input_thread_t *, demux_sys_t *, int, mtime_t );
624 static void AVI_StreamStop ( input_thread_t *, demux_sys_t *, int );
626 static vlc_bool_t AVI_StreamStart( input_thread_t *p_input,
627 demux_sys_t *p_avi, int i_stream )
629 #define p_stream p_avi->pp_info[i_stream]
630 if( !p_stream->p_es )
632 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
635 if( p_stream->b_activated )
637 msg_Warn( p_input, "stream[%d] already selected", i_stream );
641 if( !p_stream->p_es->p_decoder_fifo )
643 vlc_mutex_lock( &p_input->stream.stream_lock );
644 input_SelectES( p_input, p_stream->p_es );
645 vlc_mutex_unlock( &p_input->stream.stream_lock );
647 p_stream->b_activated = p_stream->p_es->p_decoder_fifo ? VLC_TRUE
649 if( p_stream->b_activated && p_avi->b_seekable)
651 AVI_StreamSeek( p_input, p_avi, i_stream, p_avi->i_time );
654 return p_stream->b_activated;
658 static void AVI_StreamStop( input_thread_t *p_input,
659 demux_sys_t *p_avi, int i_stream )
661 #define p_stream p_avi->pp_info[i_stream]
663 if( !p_stream->b_activated )
665 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
669 if( p_stream->p_es->p_decoder_fifo )
671 vlc_mutex_lock( &p_input->stream.stream_lock );
672 input_UnselectES( p_input, p_stream->p_es );
673 vlc_mutex_unlock( &p_input->stream.stream_lock );
677 p_stream->b_activated = VLC_FALSE;
682 /****************************************************************************
683 * AVI_MovieGetLength give max streams length in second
684 ****************************************************************************/
685 static mtime_t AVI_MovieGetLength( input_thread_t *p_input, demux_sys_t *p_avi )
691 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
693 #define p_stream p_avi->pp_info[i_stream]
695 /* fix length for each stream */
696 if( p_stream->i_idxnb < 1 || !p_stream->p_index )
701 if( p_stream->i_samplesize )
704 (mtime_t)( p_stream->p_index[p_stream->i_idxnb-1].i_lengthtotal +
705 p_stream->p_index[p_stream->i_idxnb-1].i_length ) /
706 (mtime_t)p_stream->i_scale /
707 (mtime_t)p_stream->i_rate /
708 (mtime_t)p_stream->i_samplesize;
712 i_length = (mtime_t)p_stream->i_idxnb *
713 (mtime_t)p_stream->i_scale /
714 (mtime_t)p_stream->i_rate;
718 "stream[%d] length:"I64Fd" (based on index)",
721 i_maxlength = __MAX( i_maxlength, i_length );
728 /*****************************************************************************
729 * AVIEnd: frees unused data
730 *****************************************************************************/
731 static void __AVIEnd ( vlc_object_t * p_this )
733 input_thread_t * p_input = (input_thread_t *)p_this;
735 demux_sys_t *p_avi = p_input->p_demux_data ;
739 for( i = 0; i < p_avi->i_streams; i++ )
741 if( p_avi->pp_info[i] )
743 if( p_avi->pp_info[i]->p_index )
745 free( p_avi->pp_info[i]->p_index );
747 free( p_avi->pp_info[i] );
750 free( p_avi->pp_info );
752 #ifdef __AVI_SUBTITLE__
755 subtitle_Close( p_avi->p_sub );
759 AVI_ChunkFreeRoot( p_input, &p_avi->ck_root );
762 /*****************************************************************************
763 * AVIInit: check file and initializes AVI structures
764 *****************************************************************************/
765 static int AVIInit( vlc_object_t * p_this )
767 input_thread_t * p_input = (input_thread_t *)p_this;
769 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
770 avi_chunk_list_t *p_hdrl, *p_movi;
772 avi_chunk_list_t *p_INFO;
773 avi_chunk_strz_t *p_name;
775 avi_chunk_avih_t *p_avih;
777 es_descriptor_t *p_es = NULL; /* avoid warning */
779 #ifdef __AVI_SUBTITLE__
780 mtime_t i_microsecperframe = 0; // for some subtitle format
783 vlc_bool_t b_stream_audio, b_stream_video;
785 p_input->pf_demux = AVIDemux_Seekable;
786 if( AVI_TestFile( p_input ) )
788 msg_Warn( p_input, "avi module discarded (invalid headr)" );
792 /* Initialize access plug-in structures. */
793 if( p_input->i_mtu == 0 )
796 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
799 if( !( p_input->p_demux_data =
800 p_avi = malloc( sizeof(demux_sys_t) ) ) )
802 msg_Err( p_input, "out of memory" );
805 memset( p_avi, 0, sizeof( demux_sys_t ) );
808 p_avi->b_seekable = ( ( p_input->stream.b_seekable )
809 &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
810 p_avi->i_movi_lastchunk_pos = 0;
812 /* *** for unseekable stream, automaticaly use AVIDemux_interleaved *** */
813 if( !p_avi->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
815 p_input->pf_demux = AVIDemux_UnSeekable;
818 if( AVI_ChunkReadRoot( p_input, &p_avi->ck_root, p_avi->b_seekable ) )
820 msg_Err( p_input, "avi module discarded (invalid file)" );
823 AVI_ChunkDumpDebug( p_input, &p_avi->ck_root );
826 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
828 p_hdrl = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
830 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
833 p_INFO = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
835 p_name = (avi_chunk_strz_t*)AVI_ChunkFind( p_INFO,
843 if( !p_hdrl || !p_movi )
845 msg_Err( p_input, "avi module discarded (invalid file)" );
849 if( !( p_avih = (avi_chunk_avih_t*)AVI_ChunkFind( p_hdrl,
850 AVIFOURCC_avih, 0 ) ) )
852 msg_Err( p_input, "cannot find avih chunk" );
855 p_avi->i_streams = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
856 if( p_avih->i_streams != p_avi->i_streams )
859 "found %d stream but %d are declared",
863 if( p_avi->i_streams == 0 )
866 msg_Err( p_input, "no stream defined!" );
870 /* create one program */
871 vlc_mutex_lock( &p_input->stream.stream_lock );
872 if( input_InitStream( p_input, 0 ) == -1)
874 vlc_mutex_unlock( &p_input->stream.stream_lock );
876 msg_Err( p_input, "cannot init stream" );
879 if( input_AddProgram( p_input, 0, 0) == NULL )
881 vlc_mutex_unlock( &p_input->stream.stream_lock );
883 msg_Err( p_input, "cannot add program" );
886 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
887 vlc_mutex_unlock( &p_input->stream.stream_lock );
889 /* print informations on streams */
890 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
892 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
893 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
894 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
895 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
897 /* now read info on each stream and create ES */
898 p_avi->pp_info = calloc( p_avi->i_streams,
899 sizeof( avi_stream_t* ) );
900 memset( p_avi->pp_info,
902 sizeof( avi_stream_t* ) * p_avi->i_streams );
904 for( i = 0 ; i < p_avi->i_streams; i++ )
906 avi_chunk_list_t *p_avi_strl;
907 avi_chunk_strh_t *p_avi_strh;
908 avi_chunk_strf_auds_t *p_avi_strf_auds;
909 avi_chunk_strf_vids_t *p_avi_strf_vids;
912 #define p_info p_avi->pp_info[i]
913 p_info = malloc( sizeof(avi_stream_t ) );
914 memset( p_info, 0, sizeof( avi_stream_t ) );
916 p_avi_strl = (avi_chunk_list_t*)AVI_ChunkFind( p_hdrl,
918 p_avi_strh = (avi_chunk_strh_t*)AVI_ChunkFind( p_avi_strl,
920 p_avi_strf_auds = (avi_chunk_strf_auds_t*)
921 p_avi_strf_vids = (avi_chunk_strf_vids_t*)
922 AVI_ChunkFind( p_avi_strl, AVIFOURCC_strf, 0 );
924 if( !p_avi_strl || !p_avi_strh ||
925 ( !p_avi_strf_auds && !p_avi_strf_vids ) )
927 msg_Warn( p_input, "stream[%d] incomlete", i );
931 /* *** Init p_info *** */
932 p_info->i_rate = p_avi_strh->i_rate;
933 p_info->i_scale = p_avi_strh->i_scale;
934 p_info->i_samplesize = p_avi_strh->i_samplesize;
936 switch( p_avi_strh->i_type )
938 case( AVIFOURCC_auds ):
939 p_info->i_cat = AUDIO_ES;
941 AVI_FourccGetCodec( AUDIO_ES,
942 p_avi_strf_auds->p_wf->wFormatTag );
943 p_info->i_codec = p_info->i_fourcc;
944 i_init_size = p_avi_strf_auds->i_chunk_size;
945 p_init_data = p_avi_strf_auds->p_wf;
946 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
948 p_avi_strf_auds->p_wf->wFormatTag,
949 p_avi_strf_auds->p_wf->nChannels,
950 p_avi_strf_auds->p_wf->nSamplesPerSec,
951 p_avi_strf_auds->p_wf->wBitsPerSample );
954 case( AVIFOURCC_vids ):
955 p_info->i_cat = VIDEO_ES;
956 /* XXX quick hack for playing ffmpeg video, I don't know
957 who is doing something wrong */
958 p_info->i_samplesize = 0;
959 p_info->i_fourcc = p_avi_strf_vids->p_bih->biCompression;
961 AVI_FourccGetCodec( VIDEO_ES, p_info->i_fourcc );
962 i_init_size = p_avi_strf_vids->i_chunk_size;
963 p_init_data = p_avi_strf_vids->p_bih;
964 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
966 (char*)&p_avi_strf_vids->p_bih->biCompression,
967 p_avi_strf_vids->p_bih->biWidth,
968 p_avi_strf_vids->p_bih->biHeight,
969 p_avi_strf_vids->p_bih->biBitCount,
970 (float)p_info->i_rate /
971 (float)p_info->i_scale );
972 #ifdef __AVI_SUBTITLE__
973 if( i_microsecperframe == 0 )
975 i_microsecperframe = (mtime_t)1000000 *
976 (mtime_t)p_info->i_scale /
977 (mtime_t)p_info->i_rate;
982 msg_Err( p_input, "stream[%d] unknown type", i );
983 p_info->i_cat = UNKNOWN_ES;
988 p_info->b_activated = VLC_FALSE;
990 vlc_mutex_lock( &p_input->stream.stream_lock );
992 p_es = input_AddES( p_input,
993 p_input->stream.p_selected_program, 1+i,
995 vlc_mutex_unlock( &p_input->stream.stream_lock );
996 p_es->i_stream_id =i; /* XXX: i don't use it */
997 p_es->i_fourcc = p_info->i_fourcc;
998 p_es->i_cat = p_info->i_cat;
1000 /* We copy strf for decoder in p_es->p_demux_data */
1003 memcpy( p_es->p_demux_data,
1010 #ifdef __AVI_SUBTITLE__
1011 if( ( p_avi->p_sub = subtitle_New( p_input, NULL, i_microsecperframe ) ) )
1013 subtitle_Select( p_avi->p_sub );
1017 if( config_GetInt( p_input, "avi-index" ) )
1019 if( p_avi->b_seekable )
1021 AVI_IndexCreate( p_input );
1025 msg_Warn( p_input, "cannot create index (unseekable stream)" );
1026 AVI_IndexLoad( p_input );
1031 AVI_IndexLoad( p_input );
1034 /* *** movie length in sec *** */
1036 p_avi->i_length = (mtime_t)p_avih->i_totalframes *
1037 (mtime_t)p_avih->i_microsecperframe /
1041 p_avi->i_length = AVI_MovieGetLength( p_input, p_avi );
1042 if( p_avi->i_length < (mtime_t)p_avih->i_totalframes *
1043 (mtime_t)p_avih->i_microsecperframe /
1046 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
1049 vlc_mutex_lock( &p_input->stream.stream_lock );
1050 if( p_avi->i_length )
1052 p_input->stream.i_mux_rate =
1053 p_input->stream.p_selected_area->i_size / 50 / p_avi->i_length;
1057 p_input->stream.i_mux_rate = 0;
1059 vlc_mutex_unlock( &p_input->stream.stream_lock );
1061 b_stream_audio = VLC_FALSE;
1062 b_stream_video = VLC_FALSE;
1064 for( i = 0; i < p_avi->i_streams; i++ )
1066 #define p_info p_avi->pp_info[i]
1067 switch( p_info->p_es->i_cat )
1071 if( !b_stream_video )
1073 b_stream_video = AVI_StreamStart( p_input, p_avi, i );
1078 if( !b_stream_audio )
1080 b_stream_audio = AVI_StreamStart( p_input, p_avi, i );
1089 if( !b_stream_video )
1091 msg_Warn( p_input, "no video stream found" );
1093 if( !b_stream_audio )
1095 msg_Warn( p_input, "no audio stream found!" );
1098 vlc_mutex_lock( &p_input->stream.stream_lock );
1099 p_input->stream.p_selected_program->b_is_ok = 1;
1100 vlc_mutex_unlock( &p_input->stream.stream_lock );
1102 if( p_avi->b_seekable )
1104 AVI_ChunkGoto( p_input, p_movi );
1108 // already at begining of p_movi
1110 AVI_SkipBytes( p_input, 12 ); // enter in p_movi
1112 p_avi->i_movi_begin = p_movi->i_chunk_pos;
1119 /*****************************************************************************
1120 * Function to convert pts to chunk or byte
1121 *****************************************************************************/
1123 static inline mtime_t AVI_PTSToChunk( avi_stream_t *p_info,
1126 return (mtime_t)((int64_t)i_pts *
1127 (int64_t)p_info->i_rate /
1128 (int64_t)p_info->i_scale /
1131 static inline mtime_t AVI_PTSToByte( avi_stream_t *p_info,
1134 return (mtime_t)((int64_t)i_pts *
1135 (int64_t)p_info->i_samplesize *
1136 (int64_t)p_info->i_rate /
1137 (int64_t)p_info->i_scale /
1142 static mtime_t AVI_GetDPTS( avi_stream_t *p_stream, int i_count )
1144 if( p_stream->i_samplesize )
1146 return (mtime_t)( (int64_t)1000000 *
1148 (int64_t)p_stream->i_scale /
1149 (int64_t)p_stream->i_rate /
1150 (int64_t)p_stream->i_samplesize );
1154 return (mtime_t)( (int64_t)1000000 *
1156 (int64_t)p_stream->i_scale /
1157 (int64_t)p_stream->i_rate);
1162 static mtime_t AVI_GetPTS( avi_stream_t *p_info )
1165 if( p_info->i_samplesize )
1167 /* we need a valid entry we will emulate one */
1169 if( p_info->i_idxposc == p_info->i_idxnb )
1171 if( p_info->i_idxposc )
1173 /* use the last entry */
1174 i_len = p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal
1175 + p_info->p_index[p_info->i_idxnb - 1].i_length
1176 + p_info->i_idxposb; /* should be 0 */
1180 i_len = p_info->i_idxposb;
1181 /* no valid entry use only offset*/
1186 i_len = p_info->p_index[p_info->i_idxposc].i_lengthtotal
1187 + p_info->i_idxposb;
1189 return (mtime_t)( (int64_t)1000000 *
1191 (int64_t)p_info->i_scale /
1192 (int64_t)p_info->i_rate /
1193 (int64_t)p_info->i_samplesize );
1197 /* even if p_info->i_idxposc isn't valid, there isn't any problem */
1198 return (mtime_t)( (int64_t)1000000 *
1199 (int64_t)(p_info->i_idxposc ) *
1200 (int64_t)p_info->i_scale /
1201 (int64_t)p_info->i_rate);
1205 static int AVI_StreamChunkFind( input_thread_t *p_input,
1208 demux_sys_t *p_avi = p_input->p_demux_data;
1209 avi_packet_t avi_pk;
1211 /* find first chunk of i_stream that isn't in index */
1213 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin )
1215 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
1216 if( AVI_PacketNext( p_input ) )
1218 return VLC_EGENERIC;
1223 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin );
1229 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1231 msg_Err( p_input, "cannot get packet header" );
1232 return VLC_EGENERIC;
1234 if( avi_pk.i_stream >= p_avi->i_streams ||
1235 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1237 switch( avi_pk.i_fourcc )
1239 case AVIFOURCC_LIST:
1240 AVI_SkipBytes( p_input, 12 );
1243 if( AVI_PacketNext( p_input ) )
1245 return VLC_EGENERIC;
1252 /* add this chunk to the index */
1253 AVIIndexEntry_t index;
1255 index.i_id = avi_pk.i_fourcc;
1257 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
1259 index.i_pos = avi_pk.i_pos;
1260 index.i_length = avi_pk.i_size;
1261 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
1263 if( avi_pk.i_stream == i_stream )
1268 if( AVI_PacketNext( p_input ) )
1270 return VLC_EGENERIC;
1277 /* be sure that i_ck will be a valid index entry */
1278 static int AVI_SetStreamChunk( input_thread_t *p_input,
1282 demux_sys_t *p_avi = p_input->p_demux_data;
1283 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1285 p_stream->i_idxposc = i_ck;
1286 p_stream->i_idxposb = 0;
1288 if( i_ck >= p_stream->i_idxnb )
1290 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1293 p_stream->i_idxposc++;
1294 if( AVI_StreamChunkFind( p_input, i_stream ) )
1296 return VLC_EGENERIC;
1299 } while( p_stream->i_idxposc < i_ck );
1306 /* XXX FIXME up to now, we assume that all chunk are one after one */
1307 static int AVI_SetStreamBytes( input_thread_t *p_input,
1311 demux_sys_t *p_avi = p_input->p_demux_data;
1312 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1314 if( ( p_stream->i_idxnb > 0 )
1315 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1316 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1318 /* index is valid to find the ck */
1319 /* uses dichototmie to be fast enougth */
1320 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1321 int i_idxmax = p_stream->i_idxnb;
1325 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1327 i_idxmax = i_idxposc ;
1328 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1332 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1333 p_stream->p_index[i_idxposc].i_length <= i_byte)
1335 i_idxmin = i_idxposc ;
1336 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1340 p_stream->i_idxposc = i_idxposc;
1341 p_stream->i_idxposb = i_byte -
1342 p_stream->p_index[i_idxposc].i_lengthtotal;
1351 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1352 p_stream->i_idxposb = 0;
1355 p_stream->i_idxposc++;
1356 if( AVI_StreamChunkFind( p_input, i_stream ) )
1358 return VLC_EGENERIC;
1361 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1362 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1364 p_stream->i_idxposb = i_byte -
1365 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1370 static int AVI_StreamSeek( input_thread_t *p_input,
1375 #define p_stream p_avi->pp_info[i_stream]
1378 i_oldpts = AVI_GetPTS( p_stream );
1380 if( !p_stream->i_samplesize )
1382 if( AVI_SetStreamChunk( p_input,
1384 AVI_PTSToChunk( p_stream, i_date ) ) )
1386 return VLC_EGENERIC;
1389 /* search key frame */
1391 "old:"I64Fd" %s new "I64Fd,
1393 i_oldpts > i_date ? ">" : "<",
1396 if( i_date < i_oldpts )
1398 while( p_stream->i_idxposc > 0 &&
1399 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1402 if( AVI_SetStreamChunk( p_input,
1404 p_stream->i_idxposc - 1 ) )
1406 return VLC_EGENERIC;
1412 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1413 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1416 if( AVI_SetStreamChunk( p_input,
1418 p_stream->i_idxposc + 1 ) )
1420 return VLC_EGENERIC;
1427 if( AVI_SetStreamBytes( p_input,
1429 AVI_PTSToByte( p_stream, i_date ) ) )
1431 return VLC_EGENERIC;
1438 /*****************************************************************************
1439 * AVISeek: goto to i_date or i_percent
1440 *****************************************************************************
1441 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1442 *****************************************************************************/
1443 static int AVISeek ( input_thread_t *p_input,
1444 mtime_t i_date, int i_percent )
1447 demux_sys_t *p_avi = p_input->p_demux_data;
1450 "seek requested: "I64Fd" secondes %d%%",
1454 if( p_avi->b_seekable )
1456 if( !p_avi->i_length )
1458 avi_stream_t *p_stream;
1461 /* use i_percent to create a true i_date */
1463 "mmh, seeking without index at %d%%"
1464 " work only for interleaved file", i_percent );
1466 if( i_percent >= 100 )
1468 msg_Err( p_input, "cannot seek so far !" );
1471 i_percent = __MAX( i_percent, 0 );
1473 /* try to find chunk that is at i_percent or the file */
1474 i_pos = __MAX( i_percent *
1475 p_input->stream.p_selected_area->i_size / 100,
1476 p_avi->i_movi_begin );
1477 /* search first selected stream */
1478 for( i_stream = 0, p_stream = NULL;
1479 i_stream < p_avi->i_streams; i_stream++ )
1481 p_stream = p_avi->pp_info[i_stream];
1482 if( p_stream->b_activated )
1487 if( !p_stream || !p_stream->b_activated )
1489 msg_Err( p_input, "cannot find any selected stream" );
1493 /* be sure that the index exit */
1494 if( AVI_SetStreamChunk( p_input,
1498 msg_Err( p_input, "cannot seek" );
1502 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1503 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1505 /* search after i_idxposc */
1506 if( AVI_SetStreamChunk( p_input,
1507 i_stream, p_stream->i_idxposc + 1 ) )
1509 msg_Err( p_input, "cannot seek" );
1513 i_date = AVI_GetPTS( p_stream );
1514 /* TODO better support for i_samplesize != 0 */
1515 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1518 #define p_stream p_avi->pp_info[i_stream]
1520 /* seek for chunk based streams */
1521 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1523 if( p_stream->b_activated && !p_stream->i_samplesize )
1524 // if( p_stream->b_activated )
1526 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1527 p_avi->i_time = __MAX( AVI_GetPTS( p_stream ),
1534 i_date = p_avi->i_time;
1536 /* seek for bytes based streams */
1537 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1539 if( p_stream->b_activated && p_stream->i_samplesize )
1541 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1542 // p_avi->i_time = __MAX( AVI_GetPTS( p_stream ), p_avi->i_time );
1545 msg_Dbg( p_input, "seek: "I64Fd" secondes", p_avi->i_time /1000000 );
1546 /* set true movie time */
1548 if( !p_avi->i_time )
1550 p_avi->i_time = i_date;
1557 msg_Err( p_input, "shouldn't yet be executed" );
1562 /*****************************************************************************
1563 * AVIDemux_Seekable: reads and demuxes data packets for stream seekable
1564 *****************************************************************************
1565 * AVIDemux: reads and demuxes data packets
1566 *****************************************************************************
1567 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1568 *****************************************************************************/
1569 typedef struct avi_stream_toread_s
1575 off_t i_posf; // where we will read :
1576 // if i_idxposb == 0 : begining of chunk (+8 to acces data)
1577 // else : point on data directly
1578 } avi_stream_toread_t;
1580 static int AVIDemux_Seekable( input_thread_t *p_input )
1583 vlc_bool_t b_stream;
1585 // cannot be more than 100 stream (dcXX or wbXX)
1586 avi_stream_toread_t toread[100];
1588 demux_sys_t *p_avi = p_input->p_demux_data;
1590 /* detect new selected/unselected streams */
1591 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1593 #define p_stream p_avi->pp_info[i_stream]
1594 if( p_stream->p_es )
1596 if( p_stream->p_es->p_decoder_fifo &&
1597 !p_stream->b_activated )
1599 AVI_StreamStart( p_input, p_avi, i_stream );
1602 if( !p_stream->p_es->p_decoder_fifo &&
1603 p_stream->b_activated )
1605 AVI_StreamStop( p_input, p_avi, i_stream );
1611 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1615 /* first wait for empty buffer, arbitrary time FIXME */
1616 // msleep( DEFAULT_PTS_DELAY );
1618 i_date = (mtime_t)1000000 *
1619 (mtime_t)p_avi->i_length *
1620 (mtime_t)AVI_TellAbsolute( p_input ) /
1621 (mtime_t)p_input->stream.p_selected_area->i_size;
1622 i_percent = 100 * AVI_TellAbsolute( p_input ) /
1623 p_input->stream.p_selected_area->i_size;
1625 // input_ClockInit( p_input->stream.p_selected_program );
1626 AVISeek( p_input, i_date, i_percent);
1628 #ifdef __AVI_SUBTITLE__
1631 subtitle_Seek( p_avi->p_sub, p_avi->i_time );
1637 /* wait for the good time */
1639 p_avi->i_pcr = p_avi->i_time * 9 / 100;
1641 input_ClockManageRef( p_input,
1642 p_input->stream.p_selected_program,
1646 p_avi->i_time += 100*1000; /* read 100ms */
1648 #ifdef __AVI_SUBTITLE__
1651 subtitle_Demux( p_avi->p_sub, p_avi->i_time );
1655 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1657 #define p_stream p_avi->pp_info[i_stream]
1660 toread[i_stream].b_ok = p_stream->b_activated;
1662 if( p_stream->i_idxposc < p_stream->i_idxnb )
1664 toread[i_stream].i_posf =
1665 p_stream->p_index[p_stream->i_idxposc].i_pos;
1666 if( p_stream->i_idxposb > 0 )
1668 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
1673 toread[i_stream].i_posf = -1;
1676 i_dpts = p_avi->i_time - AVI_GetPTS( p_stream );
1678 if( p_stream->i_samplesize )
1680 toread[i_stream].i_toread = AVI_PTSToByte( p_stream,
1685 toread[i_stream].i_toread = AVI_PTSToChunk( p_stream,
1691 toread[i_stream].i_toread *= -1;
1696 b_stream = VLC_FALSE;
1700 #define p_stream p_avi->pp_info[i_stream]
1702 pes_packet_t *p_pes;
1707 /* search for first chunk to be read */
1708 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_avi->i_streams; i++ )
1710 if( !toread[i].b_ok ||
1711 AVI_GetDPTS( p_avi->pp_info[i],
1712 toread[i].i_toread ) <= -25 * 1000 )
1717 if( toread[i].i_toread > 0 )
1719 b_done = VLC_FALSE; // not yet finished
1722 if( toread[i].i_posf > 0 )
1724 if( i_pos == -1 || i_pos > toread[i_stream].i_posf )
1727 i_pos = toread[i].i_posf;
1734 // return( b_stream ? 1 : 0 );
1740 /* no valid index, we will parse directly the stream */
1741 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin )
1743 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
1744 if( AVI_PacketNext( p_input ) )
1751 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin );
1756 avi_packet_t avi_pk;
1758 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1760 msg_Err( p_input, "cannot get packet header" );
1763 if( avi_pk.i_stream >= p_avi->i_streams ||
1764 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1766 switch( avi_pk.i_fourcc )
1768 case AVIFOURCC_LIST:
1769 AVI_SkipBytes( p_input, 12 );
1772 if( AVI_PacketNext( p_input ) )
1774 msg_Err( p_input, "cannot skip packet" );
1783 /* add this chunk to the index */
1784 AVIIndexEntry_t index;
1786 index.i_id = avi_pk.i_fourcc;
1788 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
1790 index.i_pos = avi_pk.i_pos;
1791 index.i_length = avi_pk.i_size;
1792 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
1794 i_stream = avi_pk.i_stream;
1795 /* do we will read this data ? */
1796 if( AVI_GetDPTS( p_stream,
1797 toread[i_stream].i_toread ) > -25 * 1000 )
1803 if( AVI_PacketNext( p_input ) )
1805 msg_Err( p_input, "cannot skip packet" );
1815 AVI_SeekAbsolute( p_input, i_pos );
1818 /* read thoses data */
1819 if( p_stream->i_samplesize )
1821 i_size = __MIN( p_stream->p_index[p_stream->i_idxposc].i_length -
1822 p_stream->i_idxposb,
1823 100 * 1024 ); // 10Ko max
1824 // toread[i_stream].i_toread );
1828 i_size = p_stream->p_index[p_stream->i_idxposc].i_length;
1831 if( p_stream->i_idxposb == 0 )
1833 i_size += 8; // need to read and skip header
1836 if( input_ReadInPES( p_input, &p_pes, __EVEN( i_size ) ) < 0 )
1838 msg_Err( p_input, "failled reading data" );
1839 toread[i_stream].b_ok = VLC_FALSE;
1843 if( i_size % 2 ) // read was padded on word boundary
1845 p_pes->p_last->p_payload_end--;
1846 p_pes->i_pes_size--;
1849 if( p_stream->i_idxposb == 0 )
1851 p_pes->p_first->p_payload_start += 8;
1852 p_pes->i_pes_size -= 8;
1855 p_pes->i_pts = AVI_GetPTS( p_stream );
1858 if( p_stream->i_samplesize )
1860 if( p_stream->i_idxposb == 0 )
1864 toread[i_stream].i_toread -= i_size;
1865 p_stream->i_idxposb += i_size;
1866 if( p_stream->i_idxposb >=
1867 p_stream->p_index[p_stream->i_idxposc].i_length )
1869 p_stream->i_idxposb = 0;
1870 p_stream->i_idxposc++;
1875 toread[i_stream].i_toread--;
1876 p_stream->i_idxposc++;
1879 if( p_stream->i_idxposc < p_stream->i_idxnb)
1881 toread[i_stream].i_posf =
1882 p_stream->p_index[p_stream->i_idxposc].i_pos;
1883 if( p_stream->i_idxposb > 0 )
1885 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
1891 toread[i_stream].i_posf = -1;
1894 b_stream = VLC_TRUE; // at least one read succeed
1896 if( p_stream->p_es && p_stream->p_es->p_decoder_fifo )
1900 input_ClockGetTS( p_input,
1901 p_input->stream.p_selected_program,
1902 p_pes->i_pts * 9/100);
1904 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
1908 input_DeletePES( p_input->p_method_data, p_pes );
1914 /*****************************************************************************
1915 * AVIDemux_UnSeekable: reads and demuxes data packets for unseekable file
1916 *****************************************************************************
1917 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1918 *****************************************************************************/
1919 static int AVIDemux_UnSeekable( input_thread_t *p_input )
1921 demux_sys_t *p_avi = p_input->p_demux_data;
1922 avi_stream_t *p_stream_master;
1927 /* *** send audio data to decoder only if rate == DEFAULT_RATE *** */
1928 vlc_mutex_lock( &p_input->stream.stream_lock );
1929 b_audio = p_input->stream.control.i_rate == DEFAULT_RATE;
1930 vlc_mutex_unlock( &p_input->stream.stream_lock );
1932 input_ClockManageRef( p_input,
1933 p_input->stream.p_selected_program,
1935 /* *** find master stream for data packet skipping algo *** */
1936 /* *** -> first video, if any, or first audio ES *** */
1937 for( i_stream = 0, p_stream_master = NULL;
1938 i_stream < p_avi->i_streams; i_stream++ )
1940 #define p_stream p_avi->pp_info[i_stream]
1941 if( p_stream->p_es &&
1942 p_stream->p_es->p_decoder_fifo )
1944 if( p_stream->i_cat == VIDEO_ES )
1946 p_stream_master = p_stream;
1949 if( p_stream->i_cat == AUDIO_ES && !p_stream_master )
1951 p_stream_master = p_stream;
1956 if( !p_stream_master )
1958 msg_Err( p_input, "no more stream selected" );
1962 p_avi->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
1964 for( i_packet = 0; i_packet < 10; i_packet++)
1966 #define p_stream p_avi->pp_info[avi_pk.i_stream]
1968 avi_packet_t avi_pk;
1970 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1974 // AVI_ParseStreamHeader( avi_pk.i_fourcc, &i_stream, &i_cat );
1976 if( avi_pk.i_stream >= p_avi->i_streams ||
1977 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1979 /* we haven't found an audio or video packet:
1980 * - we have seek, found first next packet
1981 * - others packets could be found, skip them
1983 switch( avi_pk.i_fourcc )
1985 case AVIFOURCC_JUNK:
1986 case AVIFOURCC_LIST:
1987 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
1988 case AVIFOURCC_idx1:
1992 "seems to have lost position, resync" );
1993 if( AVI_PacketSearch( p_input ) )
1995 msg_Err( p_input, "resync failed" );
2002 /* do will send this packet to decoder ? */
2003 if( ( !b_audio && avi_pk.i_cat == AUDIO_ES )||
2005 !p_stream->p_es->p_decoder_fifo )
2007 if( AVI_PacketNext( p_input ) )
2014 /* it's a selected stream, check for time */
2015 if( __ABS( AVI_GetPTS( p_stream ) -
2016 AVI_GetPTS( p_stream_master ) )< 600*1000 )
2018 /* load it and send to decoder */
2019 pes_packet_t *p_pes;
2020 if( AVI_PacketRead( p_input, &avi_pk, &p_pes ) || !p_pes)
2025 input_ClockGetTS( p_input,
2026 p_input->stream.p_selected_program,
2027 AVI_GetPTS( p_stream ) * 9/100);
2028 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
2032 if( AVI_PacketNext( p_input ) )
2039 /* *** update stream time position *** */
2040 if( p_stream->i_samplesize )
2042 p_stream->i_idxposb += avi_pk.i_size;
2046 p_stream->i_idxposc++;