1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN (Centrale Réseaux) and its contributors
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>
36 /*****************************************************************************
38 *****************************************************************************/
40 #define INTERLEAVE_TEXT N_("Force interleaved method" )
41 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method" )
43 #define INDEX_TEXT N_("Force index creation")
44 #define INDEX_LONGTEXT N_( \
45 "Recreate a index for the AVI file so we can seek trough it more reliably." )
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
51 set_shortname( "AVI" );
52 set_description( _("AVI demuxer") );
53 set_capability( "demux2", 212 );
54 set_category( CAT_INPUT );
55 set_subcategory( SUBCAT_INPUT_DEMUX );
57 add_bool( "avi-interleaved", 0, NULL,
58 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
59 add_bool( "avi-index", 0, NULL,
60 INDEX_TEXT, INDEX_LONGTEXT, VLC_TRUE );
62 set_callbacks( Open, Close );
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Control ( demux_t *, int, va_list );
69 static int Seek ( demux_t *, mtime_t, int );
70 static int Demux_Seekable ( demux_t * );
71 static int Demux_UnSeekable( demux_t * );
73 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
74 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
78 vlc_fourcc_t i_fourcc;
81 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
83 uint8_t i_peek[8]; /* first 8 bytes */
85 unsigned int i_stream;
96 uint32_t i_lengthtotal;
102 vlc_bool_t b_activated;
104 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
105 vlc_fourcc_t i_codec;
113 avi_entry_t *p_index;
114 unsigned int i_idxnb;
115 unsigned int i_idxmax;
117 unsigned int i_idxposc; /* numero of chunk */
118 unsigned int i_idxposb; /* byte in the current chunk */
120 /* For VBR audio only */
121 unsigned int i_blockno;
122 unsigned int i_blocksize;
130 vlc_bool_t b_seekable;
136 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
138 /* number of streams and information */
139 unsigned int i_track;
146 static inline off_t __EVEN( off_t i )
148 return (i & 1) ? i + 1 : i;
151 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
152 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
153 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
154 static mtime_t AVI_GetPTS ( avi_track_t * );
157 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
158 static int AVI_StreamChunkSet ( demux_t *,
159 unsigned int i_stream, unsigned int i_ck );
160 static int AVI_StreamBytesSet ( demux_t *,
161 unsigned int i_stream, off_t i_byte );
163 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
164 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
166 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
167 static int AVI_PacketNext ( demux_t * );
168 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
169 static int AVI_PacketSearch ( demux_t * );
171 static void AVI_IndexLoad ( demux_t * );
172 static void AVI_IndexCreate ( demux_t * );
173 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
175 static mtime_t AVI_MovieGetLength( demux_t * );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
181 static int AVI_TrackStopFinishedStreams( demux_t *);
184 - For VBR mp3 stream:
185 count blocks by rounded-up chunksizes instead of chunks
186 we need full emulation of dshow avi demuxer bugs :(
187 fixes silly nandub-style a-v delaying in avi with vbr mp3...
188 (from mplayer 2002/08/02)
192 /*****************************************************************************
193 * Open: check file and initializes AVI structures
194 *****************************************************************************/
195 static int Open( vlc_object_t * p_this )
197 demux_t *p_demux = (demux_t *)p_this;
201 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
202 avi_chunk_list_t *p_hdrl, *p_movi;
203 avi_chunk_avih_t *p_avih;
205 unsigned int i_track;
206 unsigned int i, i_peeker;
210 /* Is it an avi file ? */
211 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
213 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
215 if( !strncmp( &p_peek[0], "RIFF", 4 ) &&
216 !strncmp( &p_peek[8], "AVI ", 4 ) ) break;
219 if( i_peeker == 188 )
224 /* Initialize input structures. */
225 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
226 memset( p_sys, 0, sizeof( demux_sys_t ) );
229 p_sys->i_movi_lastchunk_pos = 0;
230 p_sys->b_odml = VLC_FALSE;
235 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
237 p_demux->pf_control = Control;
238 p_demux->pf_demux = Demux_Seekable;
239 /* For unseekable stream, automaticaly use Demux_UnSeekable */
240 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
242 p_demux->pf_demux = Demux_UnSeekable;
247 stream_Read( p_demux->s, NULL, i_peeker );
250 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
252 msg_Err( p_demux, "avi module discarded (invalid file)" );
256 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
258 unsigned int i_count =
259 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
261 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
262 for( i = 1; i < i_count; i++ )
264 avi_chunk_list_t *p_sysx;
266 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
267 if( p_sysx->i_type == AVIFOURCC_AVIX )
269 msg_Warn( p_demux, "detected OpenDML file" );
270 p_sys->b_odml = VLC_TRUE;
276 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
277 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
278 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
280 if( !p_hdrl || !p_movi )
282 msg_Err( p_demux, "avi module discarded (invalid file)" );
286 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
288 msg_Err( p_demux, "cannot find avih chunk" );
291 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
292 if( p_avih->i_streams != i_track )
295 "found %d stream but %d are declared",
296 i_track, p_avih->i_streams );
300 msg_Err( p_demux, "no stream defined!" );
304 /* print information on streams */
305 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
307 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
308 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
309 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
310 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
311 if( ( p_sys->meta = vlc_meta_New() ) )
314 sprintf( buffer, "%s%s%s%s",
315 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
316 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
317 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
318 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
319 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
322 /* now read info on each stream and create ES */
323 for( i = 0 ; i < i_track; i++ )
325 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
326 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
327 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
328 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
329 avi_chunk_strf_auds_t *p_auds;
330 avi_chunk_strf_vids_t *p_vids;
333 tk->b_activated = VLC_FALSE;
343 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
344 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
346 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
348 msg_Warn( p_demux, "stream[%d] incomplete", i );
352 tk->i_rate = p_strh->i_rate;
353 tk->i_scale = p_strh->i_scale;
354 tk->i_samplesize = p_strh->i_samplesize;
355 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
356 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
358 switch( p_strh->i_type )
360 case( AVIFOURCC_auds ):
361 tk->i_cat = AUDIO_ES;
362 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
363 p_auds->p_wf->wFormatTag );
364 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
366 if( p_auds->p_wf->wFormatTag == 1 )
368 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
375 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
377 fmt.audio.i_channels = p_auds->p_wf->nChannels;
378 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
379 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
380 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
381 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
382 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
383 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
384 fmt.p_extra = &p_auds->p_wf[1];
385 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
386 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
387 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
390 case( AVIFOURCC_vids ):
391 tk->i_cat = VIDEO_ES;
392 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
393 p_vids->p_bih->biCompression );
394 if( p_vids->p_bih->biCompression == 0x00 )
396 switch( p_vids->p_bih->biBitCount )
399 tk->i_codec = VLC_FOURCC('R','V','3','2');
402 tk->i_codec = VLC_FOURCC('R','V','2','4');
405 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
408 tk->i_codec = VLC_FOURCC('R','V','1','5');
411 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
414 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
416 if( p_vids->p_bih->biBitCount == 24 )
418 /* This is in BGR format */
419 fmt.video.i_bmask = 0x00ff0000;
420 fmt.video.i_gmask = 0x0000ff00;
421 fmt.video.i_rmask = 0x000000ff;
426 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
427 if( tk->i_codec == FOURCC_mp4v &&
428 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
430 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
433 tk->i_samplesize = 0;
434 fmt.video.i_width = p_vids->p_bih->biWidth;
435 fmt.video.i_height = p_vids->p_bih->biHeight;
436 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
437 fmt.video.i_frame_rate = tk->i_rate;
438 fmt.video.i_frame_rate_base = tk->i_scale;
440 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
441 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
442 fmt.p_extra = &p_vids->p_bih[1];
443 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
444 i, (char*)&p_vids->p_bih->biCompression,
445 p_vids->p_bih->biWidth,
446 p_vids->p_bih->biHeight,
447 p_vids->p_bih->biBitCount,
448 (float)tk->i_rate/(float)tk->i_scale );
450 if( p_vids->p_bih->biCompression == 0x00 )
452 /* RGB DIB are coded from bottom to top */
454 (unsigned int)(-(int)p_vids->p_bih->biHeight);
457 /* Extract palette from extradata if bpp <= 8
458 * (assumes that extradata contains only palette but appears
459 * to be true for all palettized codecs we support) */
460 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
461 fmt.video.i_bits_per_pixel > 0 )
465 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
466 fmt.video.p_palette->i_entries = 1;
468 /* Apparently this is necessary. But why ? */
470 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
472 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
474 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
475 GetDWLE((uint32_t*)fmt.p_extra + i);
481 case( AVIFOURCC_txts):
483 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
484 msg_Dbg( p_demux, "stream[%d] subtitles", i );
485 es_format_Init( &fmt, SPU_ES, tk->i_codec );
488 case( AVIFOURCC_mids):
489 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
491 msg_Warn( p_demux, "stream[%d] unknown type", i );
497 fmt.psz_description = strdup( p_strn->p_str );
499 tk->p_es = es_out_Add( p_demux->out, &fmt );
500 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
503 if( p_sys->i_track <= 0 )
505 msg_Err( p_demux, "no valid track" );
509 if( config_GetInt( p_demux, "avi-index" ) )
511 if( p_sys->b_seekable )
513 AVI_IndexCreate( p_demux );
517 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
518 AVI_IndexLoad( p_demux );
523 AVI_IndexLoad( p_demux );
526 /* *** movie length in sec *** */
527 p_sys->i_length = AVI_MovieGetLength( p_demux );
528 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
529 (mtime_t)p_avih->i_microsecperframe /
532 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
534 /* fix some BeOS MediaKit generated file */
535 for( i = 0 ; i < p_sys->i_track; i++ )
537 avi_track_t *tk = p_sys->track[i];
538 avi_chunk_list_t *p_strl;
539 avi_chunk_strh_t *p_strh;
540 avi_chunk_strf_auds_t *p_auds;
542 if( tk->i_cat != AUDIO_ES )
546 if( tk->i_idxnb < 1 ||
548 tk->i_samplesize != 0 )
552 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
553 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
554 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
556 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
557 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
559 int64_t i_track_length =
560 tk->p_index[tk->i_idxnb-1].i_length +
561 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
562 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
563 (mtime_t)p_avih->i_microsecperframe;
567 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
570 tk->i_samplesize = 1;
571 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
572 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
576 if( p_sys->b_seekable )
578 /* we have read all chunk so go back to movi */
579 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
581 /* Skip movi header */
582 stream_Read( p_demux->s, NULL, 12 );
584 p_sys->i_movi_begin = p_movi->i_chunk_pos;
590 vlc_meta_Delete( p_sys->meta );
592 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
597 /*****************************************************************************
598 * Close: frees unused data
599 *****************************************************************************/
600 static void Close ( vlc_object_t * p_this )
602 demux_t * p_demux = (demux_t *)p_this;
604 demux_sys_t *p_sys = p_demux->p_sys ;
606 for( i = 0; i < p_sys->i_track; i++ )
608 if( p_sys->track[i] )
610 FREE( p_sys->track[i]->p_index );
611 free( p_sys->track[i] );
614 FREE( p_sys->track );
615 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
616 vlc_meta_Delete( p_sys->meta );
621 /*****************************************************************************
622 * Demux_Seekable: reads and demuxes data packets for stream seekable
623 *****************************************************************************
624 * AVIDemux: reads and demuxes data packets
625 *****************************************************************************
626 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
627 *****************************************************************************/
634 off_t i_posf; /* where we will read :
635 if i_idxposb == 0 : begining of chunk (+8 to acces data)
636 else : point on data directly */
637 } avi_track_toread_t;
639 static int Demux_Seekable( demux_t *p_demux )
641 demux_sys_t *p_sys = p_demux->p_sys;
643 unsigned int i_track_count = 0;
644 unsigned int i_track;
646 /* cannot be more than 100 stream (dcXX or wbXX) */
647 avi_track_toread_t toread[100];
650 /* detect new selected/unselected streams */
651 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
653 avi_track_t *tk = p_sys->track[i_track];
656 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
657 if( b && !tk->b_activated )
659 if( p_sys->b_seekable)
661 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
663 tk->b_activated = VLC_TRUE;
665 else if( !b && tk->b_activated )
667 tk->b_activated = VLC_FALSE;
675 if( i_track_count <= 0 )
677 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
679 p_sys->i_time += 25*1000; /* read 25ms */
682 if( p_sys->i_time >= i_length )
686 msg_Warn( p_demux, "no track selected, exiting..." );
690 /* wait for the good time */
691 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
692 p_sys->i_time += 25*1000; /* read 25ms */
695 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
697 avi_track_t *tk = p_sys->track[i_track];
700 toread[i_track].b_ok = tk->b_activated;
701 if( tk->i_idxposc < tk->i_idxnb )
703 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
704 if( tk->i_idxposb > 0 )
706 toread[i_track].i_posf += 8 + tk->i_idxposb;
711 toread[i_track].i_posf = -1;
714 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
716 if( tk->i_samplesize )
718 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
722 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
727 toread[i_track].i_toread *= -1;
731 b_stream = VLC_FALSE;
742 /* search for first chunk to be read */
743 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
745 if( !toread[i].b_ok ||
746 AVI_GetDPTS( p_sys->track[i],
747 toread[i].i_toread ) <= -25 * 1000 )
752 if( toread[i].i_toread > 0 )
754 b_done = VLC_FALSE; /* not yet finished */
756 if( toread[i].i_posf > 0 )
758 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
761 i_pos = toread[i].i_posf;
773 int i_loop_count = 0;
775 /* no valid index, we will parse directly the stream
776 * in case we fail we will disable all finished stream */
777 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
779 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
780 if( AVI_PacketNext( p_demux ) )
782 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
787 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
794 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
797 "cannot get packet header, track disabled" );
798 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
800 if( avi_pk.i_stream >= p_sys->i_track ||
801 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
803 if( AVI_PacketNext( p_demux ) )
806 "cannot skip packet, track disabled" );
807 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
810 /* Prevents from eating all the CPU with broken files.
811 * This value should be low enough so that it doesn't
812 * affect the reading speed too much. */
813 if( !(++i_loop_count % 1024) )
815 if( p_demux->b_die ) return -1;
818 if( !(i_loop_count % (1024 * 10)) )
820 "don't seem to find any data..." );
826 /* add this chunk to the index */
829 index.i_id = avi_pk.i_fourcc;
831 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
833 index.i_pos = avi_pk.i_pos;
834 index.i_length = avi_pk.i_size;
835 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
837 i_track = avi_pk.i_stream;
838 tk = p_sys->track[i_track];
839 /* do we will read this data ? */
840 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
846 if( AVI_PacketNext( p_demux ) )
849 "cannot skip packet, track disabled" );
850 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
859 stream_Seek( p_demux->s, i_pos );
862 /* Set the track to use */
863 tk = p_sys->track[i_track];
865 /* read thoses data */
866 if( tk->i_samplesize )
868 unsigned int i_toread;
870 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
872 if( tk->i_samplesize > 1 )
874 i_toread = tk->i_samplesize;
878 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
879 i_toread = __MAX( i_toread, 100 );
882 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
888 i_size = tk->p_index[tk->i_idxposc].i_length;
891 if( tk->i_idxposb == 0 )
893 i_size += 8; /* need to read and skip header */
896 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
898 msg_Warn( p_demux, "failled reading data" );
899 tk->b_activated = VLC_FALSE;
900 toread[i_track].b_ok = VLC_FALSE;
903 if( i_size % 2 ) /* read was padded on word boundary */
908 if( tk->i_idxposb == 0 )
910 p_frame->p_buffer += 8;
911 p_frame->i_buffer -= 8;
913 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
914 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
916 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
920 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
924 if( tk->i_samplesize )
926 if( tk->i_idxposb == 0 )
930 toread[i_track].i_toread -= i_size;
931 tk->i_idxposb += i_size;
933 tk->p_index[tk->i_idxposc].i_length )
941 int i_length = tk->p_index[tk->i_idxposc].i_length;
944 if( tk->i_cat == AUDIO_ES )
946 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
948 toread[i_track].i_toread--;
951 if( tk->i_idxposc < tk->i_idxnb)
953 toread[i_track].i_posf =
954 tk->p_index[tk->i_idxposc].i_pos;
955 if( tk->i_idxposb > 0 )
957 toread[i_track].i_posf += 8 + tk->i_idxposb;
963 toread[i_track].i_posf = -1;
966 b_stream = VLC_TRUE; /* at least one read succeed */
968 if( tk->i_cat != VIDEO_ES )
969 p_frame->i_dts = p_frame->i_pts;
972 p_frame->i_dts = p_frame->i_pts;
976 //p_pes->i_rate = p_demux->stream.control.i_rate;
977 es_out_Send( p_demux->out, tk->p_es, p_frame );
982 /*****************************************************************************
983 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
984 *****************************************************************************
985 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
986 *****************************************************************************/
987 static int Demux_UnSeekable( demux_t *p_demux )
989 demux_sys_t *p_sys = p_demux->p_sys;
990 avi_track_t *p_stream_master = NULL;
991 unsigned int i_stream;
992 unsigned int i_packet;
994 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
996 /* *** find master stream for data packet skipping algo *** */
997 /* *** -> first video, if any, or first audio ES *** */
998 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1000 avi_track_t *tk = p_sys->track[i_stream];
1003 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1005 if( b && tk->i_cat == VIDEO_ES )
1007 p_stream_master = tk;
1011 p_stream_master = tk;
1015 if( !p_stream_master )
1017 msg_Warn( p_demux, "no more stream selected" );
1021 p_sys->i_time = AVI_GetPTS( p_stream_master );
1023 for( i_packet = 0; i_packet < 10; i_packet++)
1025 #define p_stream p_sys->track[avi_pk.i_stream]
1027 avi_packet_t avi_pk;
1029 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1034 if( avi_pk.i_stream >= p_sys->i_track ||
1035 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1037 /* we haven't found an audio or video packet:
1038 * - we have seek, found first next packet
1039 * - others packets could be found, skip them
1041 switch( avi_pk.i_fourcc )
1043 case AVIFOURCC_JUNK:
1044 case AVIFOURCC_LIST:
1045 case AVIFOURCC_RIFF:
1046 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1047 case AVIFOURCC_idx1:
1050 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1052 return( 0 ); /* eof */
1055 "seems to have lost position, resync" );
1056 if( AVI_PacketSearch( p_demux ) )
1058 msg_Err( p_demux, "resync failed" );
1065 /* check for time */
1066 if( __ABS( AVI_GetPTS( p_stream ) -
1067 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1069 /* load it and send to decoder */
1071 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1075 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1077 if( avi_pk.i_cat != VIDEO_ES )
1078 p_frame->i_dts = p_frame->i_pts;
1081 p_frame->i_dts = p_frame->i_pts;
1085 //p_pes->i_rate = p_demux->stream.control.i_rate;
1086 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1090 if( AVI_PacketNext( p_demux ) )
1096 /* *** update stream time position *** */
1097 if( p_stream->i_samplesize )
1099 p_stream->i_idxposb += avi_pk.i_size;
1103 if( p_stream->i_cat == AUDIO_ES )
1105 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1107 p_stream->i_idxposc++;
1117 /*****************************************************************************
1118 * Seek: goto to i_date or i_percent
1119 *****************************************************************************/
1120 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1123 demux_sys_t *p_sys = p_demux->p_sys;
1124 unsigned int i_stream;
1125 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1126 i_date / 1000000, i_percent );
1128 if( p_sys->b_seekable )
1130 if( !p_sys->i_length )
1132 avi_track_t *p_stream;
1135 /* use i_percent to create a true i_date */
1136 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1137 " work only for interleaved file", i_percent );
1138 if( i_percent >= 100 )
1140 msg_Warn( p_demux, "cannot seek so far !" );
1141 return VLC_EGENERIC;
1143 i_percent = __MAX( i_percent, 0 );
1145 /* try to find chunk that is at i_percent or the file */
1146 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1147 p_sys->i_movi_begin );
1148 /* search first selected stream */
1149 for( i_stream = 0, p_stream = NULL;
1150 i_stream < p_sys->i_track; i_stream++ )
1152 p_stream = p_sys->track[i_stream];
1153 if( p_stream->b_activated )
1158 if( !p_stream || !p_stream->b_activated )
1160 msg_Warn( p_demux, "cannot find any selected stream" );
1161 return VLC_EGENERIC;
1164 /* be sure that the index exist */
1165 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1167 msg_Warn( p_demux, "cannot seek" );
1168 return VLC_EGENERIC;
1171 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1172 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1174 /* search after i_idxposc */
1175 if( AVI_StreamChunkSet( p_demux,
1176 i_stream, p_stream->i_idxposc + 1 ) )
1178 msg_Warn( p_demux, "cannot seek" );
1179 return VLC_EGENERIC;
1183 i_date = AVI_GetPTS( p_stream );
1184 /* TODO better support for i_samplesize != 0 */
1185 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1188 #define p_stream p_sys->track[i_stream]
1190 /* seek for chunk based streams */
1191 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1193 if( p_stream->b_activated && !p_stream->i_samplesize )
1194 /* if( p_stream->b_activated ) */
1196 AVI_TrackSeek( p_demux, i_stream, i_date );
1197 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1204 i_date = p_sys->i_time;
1206 /* seek for bytes based streams */
1207 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1209 if( p_stream->b_activated && p_stream->i_samplesize )
1211 AVI_TrackSeek( p_demux, i_stream, i_date );
1212 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1215 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1216 /* set true movie time */
1218 if( !p_sys->i_time )
1220 p_sys->i_time = i_date;
1227 msg_Err( p_demux, "shouldn't yet be executed" );
1228 return VLC_EGENERIC;
1232 /*****************************************************************************
1234 *****************************************************************************
1236 *****************************************************************************/
1237 static double ControlGetPosition( demux_t *p_demux )
1239 demux_sys_t *p_sys = p_demux->p_sys;
1241 if( p_sys->i_length > 0 )
1243 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1245 else if( stream_Size( p_demux->s ) > 0 )
1251 /* search the more advanced selected es */
1252 for( i = 0; i < p_sys->i_track; i++ )
1254 avi_track_t *tk = p_sys->track[i];
1255 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1257 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1258 tk->p_index[tk->i_idxposc].i_length + 8;
1265 return (double)i64 / (double)stream_Size( p_demux->s );
1270 static int Control( demux_t *p_demux, int i_query, va_list args )
1272 demux_sys_t *p_sys = p_demux->p_sys;
1276 vlc_meta_t **pp_meta;
1280 case DEMUX_GET_POSITION:
1281 pf = (double*)va_arg( args, double * );
1282 *pf = ControlGetPosition( p_demux );
1284 case DEMUX_SET_POSITION:
1285 f = (double)va_arg( args, double );
1286 if( p_sys->b_seekable )
1288 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1289 return Seek( p_demux, i64, (int)(f * 100) );
1293 int64_t i_pos = stream_Size( p_demux->s ) * f;
1294 return stream_Seek( p_demux->s, i_pos );
1297 case DEMUX_GET_TIME:
1298 pi64 = (int64_t*)va_arg( args, int64_t * );
1299 *pi64 = p_sys->i_time;
1302 case DEMUX_SET_TIME:
1306 i64 = (int64_t)va_arg( args, int64_t );
1307 if( p_sys->i_length > 0 )
1309 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1311 else if( p_sys->i_time > 0 )
1313 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1314 (double)i64 / (double)p_sys->i_time );
1316 return Seek( p_demux, i64, i_percent );
1318 case DEMUX_GET_LENGTH:
1319 pi64 = (int64_t*)va_arg( args, int64_t * );
1320 *pi64 = p_sys->i_length * (mtime_t)1000000;
1324 pf = (double*)va_arg( args, double * );
1326 for( i = 0; i < (int)p_sys->i_track; i++ )
1328 avi_track_t *tk = p_sys->track[i];
1329 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1331 *pf = (float)tk->i_rate / (float)tk->i_scale;
1336 case DEMUX_GET_META:
1337 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1338 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1342 return VLC_EGENERIC;
1346 /*****************************************************************************
1347 * Function to convert pts to chunk or byte
1348 *****************************************************************************/
1350 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1352 return (mtime_t)((int64_t)i_pts *
1353 (int64_t)tk->i_rate /
1354 (int64_t)tk->i_scale /
1357 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1359 return (mtime_t)((int64_t)i_pts *
1360 (int64_t)tk->i_rate /
1361 (int64_t)tk->i_scale /
1363 (int64_t)tk->i_samplesize );
1366 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1370 i_dpts = (mtime_t)( (int64_t)1000000 *
1372 (int64_t)tk->i_scale /
1373 (int64_t)tk->i_rate );
1375 if( tk->i_samplesize )
1377 return i_dpts / tk->i_samplesize;
1382 static mtime_t AVI_GetPTS( avi_track_t *tk )
1384 if( tk->i_samplesize )
1386 int64_t i_count = 0;
1388 /* we need a valid entry we will emulate one */
1389 if( tk->i_idxposc == tk->i_idxnb )
1393 /* use the last entry */
1394 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1395 + tk->p_index[tk->i_idxnb - 1].i_length;
1400 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1402 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1406 if( tk->i_cat == AUDIO_ES )
1408 return AVI_GetDPTS( tk, tk->i_blockno );
1412 return AVI_GetDPTS( tk, tk->i_idxposc );
1417 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1419 demux_sys_t *p_sys = p_demux->p_sys;
1420 avi_packet_t avi_pk;
1421 int i_loop_count = 0;
1423 /* find first chunk of i_stream that isn't in index */
1425 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1427 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1428 if( AVI_PacketNext( p_demux ) )
1430 return VLC_EGENERIC;
1435 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1440 if( p_demux->b_die ) return VLC_EGENERIC;
1442 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1444 msg_Warn( p_demux, "cannot get packet header" );
1445 return VLC_EGENERIC;
1447 if( avi_pk.i_stream >= p_sys->i_track ||
1448 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1450 if( AVI_PacketNext( p_demux ) )
1452 return VLC_EGENERIC;
1455 /* Prevents from eating all the CPU with broken files.
1456 * This value should be low enough so that it doesn't
1457 * affect the reading speed too much. */
1458 if( !(++i_loop_count % 1024) )
1460 if( p_demux->b_die ) return VLC_EGENERIC;
1463 if( !(i_loop_count % (1024 * 10)) )
1464 msg_Warn( p_demux, "don't seem to find any data..." );
1469 /* add this chunk to the index */
1472 index.i_id = avi_pk.i_fourcc;
1474 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1476 index.i_pos = avi_pk.i_pos;
1477 index.i_length = avi_pk.i_size;
1478 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1480 if( avi_pk.i_stream == i_stream )
1485 if( AVI_PacketNext( p_demux ) )
1487 return VLC_EGENERIC;
1494 /* be sure that i_ck will be a valid index entry */
1495 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1498 demux_sys_t *p_sys = p_demux->p_sys;
1499 avi_track_t *p_stream = p_sys->track[i_stream];
1501 p_stream->i_idxposc = i_ck;
1502 p_stream->i_idxposb = 0;
1504 if( i_ck >= p_stream->i_idxnb )
1506 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1509 p_stream->i_idxposc++;
1510 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1512 return VLC_EGENERIC;
1515 } while( p_stream->i_idxposc < i_ck );
1522 /* XXX FIXME up to now, we assume that all chunk are one after one */
1523 static int AVI_StreamBytesSet( demux_t *p_demux,
1524 unsigned int i_stream,
1527 demux_sys_t *p_sys = p_demux->p_sys;
1528 avi_track_t *p_stream = p_sys->track[i_stream];
1530 if( ( p_stream->i_idxnb > 0 )
1531 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1532 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1534 /* index is valid to find the ck */
1535 /* uses dichototmie to be fast enougth */
1536 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1537 int i_idxmax = p_stream->i_idxnb;
1541 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1543 i_idxmax = i_idxposc ;
1544 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1548 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1549 p_stream->p_index[i_idxposc].i_length <= i_byte)
1551 i_idxmin = i_idxposc ;
1552 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1556 p_stream->i_idxposc = i_idxposc;
1557 p_stream->i_idxposb = i_byte -
1558 p_stream->p_index[i_idxposc].i_lengthtotal;
1567 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1568 p_stream->i_idxposb = 0;
1571 p_stream->i_idxposc++;
1572 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1574 return VLC_EGENERIC;
1577 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1578 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1580 p_stream->i_idxposb = i_byte -
1581 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1586 static int AVI_TrackSeek( demux_t *p_demux,
1590 demux_sys_t *p_sys = p_demux->p_sys;
1591 avi_track_t *tk = p_sys->track[i_stream];
1593 #define p_stream p_sys->track[i_stream]
1596 i_oldpts = AVI_GetPTS( p_stream );
1598 if( !p_stream->i_samplesize )
1600 if( AVI_StreamChunkSet( p_demux,
1602 AVI_PTSToChunk( p_stream, i_date ) ) )
1604 return VLC_EGENERIC;
1607 if( p_stream->i_cat == AUDIO_ES )
1611 for( i = 0; i < tk->i_idxposc; i++ )
1613 if( tk->i_blocksize > 0 )
1615 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1625 "old:"I64Fd" %s new "I64Fd,
1627 i_oldpts > i_date ? ">" : "<",
1630 if( p_stream->i_cat == VIDEO_ES )
1632 /* search key frame */
1633 if( i_date < i_oldpts )
1635 while( p_stream->i_idxposc > 0 &&
1636 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1639 if( AVI_StreamChunkSet( p_demux,
1641 p_stream->i_idxposc - 1 ) )
1643 return VLC_EGENERIC;
1649 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1650 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1653 if( AVI_StreamChunkSet( p_demux,
1655 p_stream->i_idxposc + 1 ) )
1657 return VLC_EGENERIC;
1665 if( AVI_StreamBytesSet( p_demux,
1667 AVI_PTSToByte( p_stream, i_date ) ) )
1669 return VLC_EGENERIC;
1676 /****************************************************************************
1677 * Return VLC_TRUE if it's a key frame
1678 ****************************************************************************/
1679 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1685 * startcode: 0x00000100 32bits
1686 * framenumber ? 5bits
1687 * piture type 0(I),1(P) 2bits
1689 if( GetDWBE( p_byte ) != 0x00000100 )
1691 /* it's not an msmpegv1 stream, strange...*/
1692 return AVIIF_KEYFRAME;
1694 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1697 case FOURCC_DIV3: /* wmv1 also */
1699 * picture type 0(I),1(P) 2bits
1701 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1703 /* we should find first occurrence of 0x000001b6 (32bits)
1704 * startcode: 0x000001b6 32bits
1705 * piture type 0(I),1(P) 2bits
1707 if( GetDWBE( p_byte ) != 0x000001b6 )
1709 /* not true , need to find the first VOP header */
1710 return AVIIF_KEYFRAME;
1712 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1715 /* I can't do it, so say yes */
1716 return AVIIF_KEYFRAME;
1720 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1725 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1729 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1733 return VLC_FOURCC('m','r','l','e');
1782 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1786 /****************************************************************************
1788 ****************************************************************************/
1789 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1790 int *pi_number, int *pi_type )
1792 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1795 c1 = ((uint8_t *)&i_id)[0];
1796 c2 = ((uint8_t *)&i_id)[1];
1798 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1800 SET_PTR( pi_number, 100 ); /* > max stream number */
1801 SET_PTR( pi_type, UNKNOWN_ES );
1805 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1806 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1809 SET_PTR( pi_type, AUDIO_ES );
1813 SET_PTR( pi_type, VIDEO_ES );
1816 SET_PTR( pi_type, UNKNOWN_ES );
1823 /****************************************************************************
1825 ****************************************************************************/
1826 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1830 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1832 return VLC_EGENERIC;
1834 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1835 p_pk->i_size = GetDWLE( p_peek + 4 );
1836 p_pk->i_pos = stream_Tell( p_demux->s );
1837 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1839 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1840 p_peek[10], p_peek[11] );
1847 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1849 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1853 static int AVI_PacketNext( demux_t *p_demux )
1855 avi_packet_t avi_ck;
1858 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1860 return VLC_EGENERIC;
1863 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1864 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1868 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1869 avi_ck.i_type == AVIFOURCC_AVIX )
1875 i_skip = __EVEN( avi_ck.i_size ) + 8;
1878 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1880 return VLC_EGENERIC;
1884 static int AVI_PacketRead( demux_t *p_demux,
1886 block_t **pp_frame )
1890 i_size = __EVEN( p_pk->i_size + 8 );
1892 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1894 return VLC_EGENERIC;
1896 (*pp_frame)->p_buffer += 8;
1897 (*pp_frame)->i_buffer -= 8;
1899 if( i_size != p_pk->i_size + 8 )
1901 (*pp_frame)->i_buffer--;
1907 static int AVI_PacketSearch( demux_t *p_demux )
1909 demux_sys_t *p_sys = p_demux->p_sys;
1910 avi_packet_t avi_pk;
1915 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1917 return VLC_EGENERIC;
1919 AVI_PacketGetHeader( p_demux, &avi_pk );
1920 if( avi_pk.i_stream < p_sys->i_track &&
1921 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1925 switch( avi_pk.i_fourcc )
1927 case AVIFOURCC_JUNK:
1928 case AVIFOURCC_LIST:
1929 case AVIFOURCC_RIFF:
1930 case AVIFOURCC_idx1:
1934 /* Prevents from eating all the CPU with broken files.
1935 * This value should be low enough so that it doesn't affect the
1936 * reading speed too much (not that we care much anyway because
1937 * this code is called only on broken files). */
1938 if( !(++i_count % 1024) )
1940 if( p_demux->b_die ) return VLC_EGENERIC;
1943 if( !(i_count % (1024 * 10)) )
1944 msg_Warn( p_demux, "trying to resync..." );
1949 /****************************************************************************
1951 ****************************************************************************/
1952 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1954 avi_entry_t *p_index)
1956 avi_track_t *tk = p_sys->track[i_stream];
1958 /* Update i_movi_lastchunk_pos */
1959 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1961 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1965 if( tk->i_idxnb >= tk->i_idxmax )
1967 tk->i_idxmax += 16384;
1968 tk->p_index = realloc( tk->p_index,
1969 tk->i_idxmax * sizeof( avi_entry_t ) );
1970 if( tk->p_index == NULL )
1975 /* calculate cumulate length */
1976 if( tk->i_idxnb > 0 )
1978 p_index->i_lengthtotal =
1979 tk->p_index[tk->i_idxnb - 1].i_length +
1980 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1984 p_index->i_lengthtotal = 0;
1987 tk->p_index[tk->i_idxnb++] = *p_index;
1990 static int AVI_IndexLoad_idx1( demux_t *p_demux )
1992 demux_sys_t *p_sys = p_demux->p_sys;
1994 avi_chunk_list_t *p_riff;
1995 avi_chunk_list_t *p_movi;
1996 avi_chunk_idx1_t *p_idx1;
1998 unsigned int i_stream;
1999 unsigned int i_index;
2003 vlc_bool_t b_keyset[100];
2005 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2006 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2007 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2011 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2012 return VLC_EGENERIC;
2015 /* *** calculate offset *** */
2016 /* Well, avi is __SHIT__ so test more than one entry
2017 * (needed for some avi files) */
2019 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2021 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2023 i_offset = p_movi->i_chunk_pos + 8;
2028 /* Reset b_keyset */
2029 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2030 b_keyset[i_stream] = VLC_FALSE;
2032 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2036 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2039 if( i_stream < p_sys->i_track &&
2040 i_cat == p_sys->track[i_stream]->i_cat )
2043 index.i_id = p_idx1->entry[i_index].i_fourcc;
2045 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2046 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2047 index.i_length = p_idx1->entry[i_index].i_length;
2048 AVI_IndexAddEntry( p_sys, i_stream, &index );
2050 if( index.i_flags&AVIIF_KEYFRAME )
2051 b_keyset[i_stream] = VLC_TRUE;
2055 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2057 if( !b_keyset[i_stream] )
2059 avi_track_t *tk = p_sys->track[i_stream];
2061 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2062 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2063 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2069 static void __Parse_indx( demux_t *p_demux,
2071 avi_chunk_indx_t *p_indx )
2073 demux_sys_t *p_sys = p_demux->p_sys;
2077 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2078 if( p_indx->i_indexsubtype == 0 )
2080 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2082 index.i_id = p_indx->i_id;
2083 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2084 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2085 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2087 AVI_IndexAddEntry( p_sys, i_stream, &index );
2090 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2092 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2094 index.i_id = p_indx->i_id;
2095 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2096 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2097 index.i_length = p_indx->idx.field[i].i_size;
2099 AVI_IndexAddEntry( p_sys, i_stream, &index );
2104 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2108 static void AVI_IndexLoad_indx( demux_t *p_demux )
2110 demux_sys_t *p_sys = p_demux->p_sys;
2111 unsigned int i_stream;
2114 avi_chunk_list_t *p_riff;
2115 avi_chunk_list_t *p_hdrl;
2117 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2118 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2120 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2122 avi_chunk_list_t *p_strl;
2123 avi_chunk_indx_t *p_indx;
2125 #define p_stream p_sys->track[i_stream]
2126 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2127 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2131 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2135 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2137 __Parse_indx( p_demux, i_stream, p_indx );
2139 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2142 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2144 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2145 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2149 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2154 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2160 static void AVI_IndexLoad( demux_t *p_demux )
2162 demux_sys_t *p_sys = p_demux->p_sys;
2163 unsigned int i_stream;
2165 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2167 p_sys->track[i_stream]->i_idxnb = 0;
2168 p_sys->track[i_stream]->i_idxmax = 0;
2169 p_sys->track[i_stream]->p_index = NULL;
2174 AVI_IndexLoad_indx( p_demux );
2176 else if( AVI_IndexLoad_idx1( p_demux ) )
2178 /* try indx if idx1 failed as some "normal" file have indx too */
2179 AVI_IndexLoad_indx( p_demux );
2182 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2184 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2185 i_stream, p_sys->track[i_stream]->i_idxnb );
2189 static void AVI_IndexCreate( demux_t *p_demux )
2191 demux_sys_t *p_sys = p_demux->p_sys;
2193 avi_chunk_list_t *p_riff;
2194 avi_chunk_list_t *p_movi;
2196 unsigned int i_stream;
2199 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2200 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2204 msg_Err( p_demux, "cannot find p_movi" );
2208 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2210 p_sys->track[i_stream]->i_idxnb = 0;
2211 p_sys->track[i_stream]->i_idxmax = 0;
2212 p_sys->track[i_stream]->p_index = NULL;
2214 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2215 stream_Size( p_demux->s ) );
2217 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2218 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2223 if( p_demux->b_die )
2228 if( AVI_PacketGetHeader( p_demux, &pk ) )
2232 if( pk.i_stream < p_sys->i_track &&
2233 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2236 index.i_id = pk.i_fourcc;
2238 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2239 index.i_pos = pk.i_pos;
2240 index.i_length = pk.i_size;
2241 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2245 switch( pk.i_fourcc )
2247 case AVIFOURCC_idx1:
2250 avi_chunk_list_t *p_sysx;
2251 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2252 AVIFOURCC_RIFF, 1 );
2254 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2255 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2262 case AVIFOURCC_RIFF:
2263 msg_Dbg( p_demux, "new RIFF chunk found" );
2265 case AVIFOURCC_JUNK:
2268 msg_Warn( p_demux, "need resync, probably broken avi" );
2269 if( AVI_PacketSearch( p_demux ) )
2271 msg_Warn( p_demux, "lost sync, abord index creation" );
2277 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2278 AVI_PacketNext( p_demux ) )
2285 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2288 "stream[%d] creating %d index entries",
2290 p_sys->track[i_stream]->i_idxnb );
2294 /*****************************************************************************
2296 *****************************************************************************/
2297 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2299 demux_sys_t *p_sys = p_demux->p_sys;
2301 int b_end = VLC_TRUE;
2303 for( i = 0; i < p_sys->i_track; i++ )
2305 avi_track_t *tk = p_sys->track[i];
2306 if( tk->i_idxposc >= tk->i_idxnb )
2308 tk->b_activated = VLC_FALSE;
2309 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2319 /****************************************************************************
2320 * AVI_MovieGetLength give max streams length in second
2321 ****************************************************************************/
2322 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2324 demux_sys_t *p_sys = p_demux->p_sys;
2325 mtime_t i_maxlength = 0;
2328 for( i = 0; i < p_sys->i_track; i++ )
2330 avi_track_t *tk = p_sys->track[i];
2333 /* fix length for each stream */
2334 if( tk->i_idxnb < 1 || !tk->p_index )
2339 if( tk->i_samplesize )
2341 i_length = AVI_GetDPTS( tk,
2342 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2343 tk->p_index[tk->i_idxnb-1].i_length );
2347 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2349 i_length /= (mtime_t)1000000; /* in seconds */
2352 "stream[%d] length:"I64Fd" (based on index)",
2355 i_maxlength = __MAX( i_maxlength, i_length );