1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
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. Use this if your AVI file is damaged "\
46 "or incomplete (not seekable)" )
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_shortname( "AVI" );
53 set_description( _("AVI demuxer") );
54 set_capability( "demux2", 212 );
55 set_category( CAT_INPUT );
56 set_subcategory( SUBCAT_INPUT_DEMUX );
58 add_bool( "avi-interleaved", 0, NULL,
59 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
60 add_bool( "avi-index", 0, NULL,
61 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
63 set_callbacks( Open, Close );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Control ( demux_t *, int, va_list );
70 static int Seek ( demux_t *, mtime_t, int );
71 static int Demux_Seekable ( demux_t * );
72 static int Demux_UnSeekable( demux_t * );
74 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
75 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
79 vlc_fourcc_t i_fourcc;
82 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
84 uint8_t i_peek[8]; /* first 8 bytes */
86 unsigned int i_stream;
97 uint32_t i_lengthtotal;
103 vlc_bool_t b_activated;
105 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
106 vlc_fourcc_t i_codec;
114 avi_entry_t *p_index;
115 unsigned int i_idxnb;
116 unsigned int i_idxmax;
118 unsigned int i_idxposc; /* numero of chunk */
119 unsigned int i_idxposb; /* byte in the current chunk */
121 /* For VBR audio only */
122 unsigned int i_blockno;
123 unsigned int i_blocksize;
131 vlc_bool_t b_seekable;
137 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
139 /* number of streams and information */
140 unsigned int i_track;
147 static inline off_t __EVEN( off_t i )
149 return (i & 1) ? i + 1 : i;
152 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
153 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
154 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
155 static mtime_t AVI_GetPTS ( avi_track_t * );
158 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
159 static int AVI_StreamChunkSet ( demux_t *,
160 unsigned int i_stream, unsigned int i_ck );
161 static int AVI_StreamBytesSet ( demux_t *,
162 unsigned int i_stream, off_t i_byte );
164 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
165 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
167 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
168 static int AVI_PacketNext ( demux_t * );
169 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
170 static int AVI_PacketSearch ( demux_t * );
172 static void AVI_IndexLoad ( demux_t * );
173 static void AVI_IndexCreate ( demux_t * );
174 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
176 static mtime_t AVI_MovieGetLength( demux_t * );
178 /*****************************************************************************
180 *****************************************************************************/
181 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
182 static int AVI_TrackStopFinishedStreams( demux_t *);
185 - For VBR mp3 stream:
186 count blocks by rounded-up chunksizes instead of chunks
187 we need full emulation of dshow avi demuxer bugs :(
188 fixes silly nandub-style a-v delaying in avi with vbr mp3...
189 (from mplayer 2002/08/02)
193 /*****************************************************************************
194 * Open: check file and initializes AVI structures
195 *****************************************************************************/
196 static int Open( vlc_object_t * p_this )
198 demux_t *p_demux = (demux_t *)p_this;
202 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
203 avi_chunk_list_t *p_hdrl, *p_movi;
204 avi_chunk_avih_t *p_avih;
206 unsigned int i_track;
207 unsigned int i, i_peeker;
211 /* Is it an avi file ? */
212 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
214 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
216 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) &&
217 !strncmp( (char *)&p_peek[8], "AVI ", 4 ) ) break;
220 if( i_peeker == 188 )
225 /* Initialize input structures. */
226 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
227 memset( p_sys, 0, sizeof( demux_sys_t ) );
230 p_sys->i_movi_lastchunk_pos = 0;
231 p_sys->b_odml = VLC_FALSE;
236 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
238 p_demux->pf_control = Control;
239 p_demux->pf_demux = Demux_Seekable;
241 /* For unseekable stream, automaticaly use Demux_UnSeekable */
242 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
244 p_demux->pf_demux = Demux_UnSeekable;
249 stream_Read( p_demux->s, NULL, i_peeker );
252 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
254 msg_Err( p_demux, "avi module discarded (invalid file)" );
258 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
260 unsigned int i_count =
261 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
263 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
264 for( i = 1; i < i_count; i++ )
266 avi_chunk_list_t *p_sysx;
268 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
269 if( p_sysx->i_type == AVIFOURCC_AVIX )
271 msg_Warn( p_demux, "detected OpenDML file" );
272 p_sys->b_odml = VLC_TRUE;
278 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
279 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
280 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
282 if( !p_hdrl || !p_movi )
284 msg_Err( p_demux, "avi module discarded (invalid file)" );
288 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
290 msg_Err( p_demux, "cannot find avih chunk" );
293 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
294 if( p_avih->i_streams != i_track )
297 "found %d stream but %d are declared",
298 i_track, p_avih->i_streams );
302 msg_Err( p_demux, "no stream defined!" );
306 /* print information on streams */
307 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
309 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
310 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
311 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
312 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
313 if( ( p_sys->meta = vlc_meta_New() ) )
316 sprintf( buffer, "%s%s%s%s",
317 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
318 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
319 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
320 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
321 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
324 /* now read info on each stream and create ES */
325 for( i = 0 ; i < i_track; i++ )
327 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
328 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
329 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
330 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
331 avi_chunk_strf_auds_t *p_auds;
332 avi_chunk_strf_vids_t *p_vids;
335 tk->b_activated = VLC_FALSE;
345 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
346 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
348 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
350 msg_Warn( p_demux, "stream[%d] incomplete", i );
354 tk->i_rate = p_strh->i_rate;
355 tk->i_scale = p_strh->i_scale;
356 tk->i_samplesize = p_strh->i_samplesize;
357 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
358 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
360 switch( p_strh->i_type )
362 case( AVIFOURCC_auds ):
363 tk->i_cat = AUDIO_ES;
364 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
365 p_auds->p_wf->wFormatTag );
366 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
368 if( p_auds->p_wf->wFormatTag == 1 )
370 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
377 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
379 fmt.audio.i_channels = p_auds->p_wf->nChannels;
380 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
381 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
382 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
383 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
384 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
385 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
386 fmt.p_extra = &p_auds->p_wf[1];
387 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
388 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
389 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
392 case( AVIFOURCC_vids ):
393 tk->i_cat = VIDEO_ES;
394 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
395 p_vids->p_bih->biCompression );
396 if( p_vids->p_bih->biCompression == 0x00 )
398 switch( p_vids->p_bih->biBitCount )
401 tk->i_codec = VLC_FOURCC('R','V','3','2');
404 tk->i_codec = VLC_FOURCC('R','V','2','4');
407 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
410 tk->i_codec = VLC_FOURCC('R','V','1','5');
413 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
416 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
418 if( p_vids->p_bih->biBitCount == 24 )
420 /* This is in BGR format */
421 fmt.video.i_bmask = 0x00ff0000;
422 fmt.video.i_gmask = 0x0000ff00;
423 fmt.video.i_rmask = 0x000000ff;
428 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
429 if( tk->i_codec == FOURCC_mp4v &&
430 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
432 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
435 tk->i_samplesize = 0;
436 fmt.video.i_width = p_vids->p_bih->biWidth;
437 fmt.video.i_height = p_vids->p_bih->biHeight;
438 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
439 fmt.video.i_frame_rate = tk->i_rate;
440 fmt.video.i_frame_rate_base = tk->i_scale;
442 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
443 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
444 fmt.p_extra = &p_vids->p_bih[1];
445 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
446 i, (char*)&p_vids->p_bih->biCompression,
447 p_vids->p_bih->biWidth,
448 p_vids->p_bih->biHeight,
449 p_vids->p_bih->biBitCount,
450 (float)tk->i_rate/(float)tk->i_scale );
452 if( p_vids->p_bih->biCompression == 0x00 )
454 /* RGB DIB are coded from bottom to top */
456 (unsigned int)(-(int)p_vids->p_bih->biHeight);
459 /* Extract palette from extradata if bpp <= 8
460 * (assumes that extradata contains only palette but appears
461 * to be true for all palettized codecs we support) */
462 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
463 fmt.video.i_bits_per_pixel > 0 )
467 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
468 fmt.video.p_palette->i_entries = 1;
470 /* Apparently this is necessary. But why ? */
472 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
473 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
475 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
476 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 );
492 msg_Warn( p_demux, "stream[%d] unknown type", i );
498 fmt.psz_description = strdup( p_strn->p_str );
500 tk->p_es = es_out_Add( p_demux->out, &fmt );
501 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
504 if( p_sys->i_track <= 0 )
506 msg_Err( p_demux, "no valid track" );
510 if( config_GetInt( p_demux, "avi-index" ) )
512 if( p_sys->b_seekable )
514 AVI_IndexCreate( p_demux );
518 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
519 AVI_IndexLoad( p_demux );
524 AVI_IndexLoad( p_demux );
527 /* *** movie length in sec *** */
528 p_sys->i_length = AVI_MovieGetLength( p_demux );
529 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
530 (mtime_t)p_avih->i_microsecperframe /
533 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
535 /* fix some BeOS MediaKit generated file */
536 for( i = 0 ; i < p_sys->i_track; i++ )
538 avi_track_t *tk = p_sys->track[i];
539 avi_chunk_list_t *p_strl;
540 avi_chunk_strh_t *p_strh;
541 avi_chunk_strf_auds_t *p_auds;
543 if( tk->i_cat != AUDIO_ES )
547 if( tk->i_idxnb < 1 ||
549 tk->i_samplesize != 0 )
553 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
554 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
555 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
557 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
558 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
560 int64_t i_track_length =
561 tk->p_index[tk->i_idxnb-1].i_length +
562 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
563 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
564 (mtime_t)p_avih->i_microsecperframe;
568 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
571 tk->i_samplesize = 1;
572 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
573 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
577 if( p_sys->b_seekable )
579 /* we have read all chunk so go back to movi */
580 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
582 /* Skip movi header */
583 stream_Read( p_demux->s, NULL, 12 );
585 p_sys->i_movi_begin = p_movi->i_chunk_pos;
591 vlc_meta_Delete( p_sys->meta );
593 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
598 /*****************************************************************************
599 * Close: frees unused data
600 *****************************************************************************/
601 static void Close ( vlc_object_t * p_this )
603 demux_t * p_demux = (demux_t *)p_this;
605 demux_sys_t *p_sys = p_demux->p_sys ;
607 for( i = 0; i < p_sys->i_track; i++ )
609 if( p_sys->track[i] )
611 FREE( p_sys->track[i]->p_index );
612 free( p_sys->track[i] );
615 FREE( p_sys->track );
616 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
617 vlc_meta_Delete( p_sys->meta );
622 /*****************************************************************************
623 * Demux_Seekable: reads and demuxes data packets for stream seekable
624 *****************************************************************************
625 * AVIDemux: reads and demuxes data packets
626 *****************************************************************************
627 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
628 *****************************************************************************/
635 off_t i_posf; /* where we will read :
636 if i_idxposb == 0 : begining of chunk (+8 to acces data)
637 else : point on data directly */
638 } avi_track_toread_t;
640 static int Demux_Seekable( demux_t *p_demux )
642 demux_sys_t *p_sys = p_demux->p_sys;
644 unsigned int i_track_count = 0;
645 unsigned int i_track;
647 /* cannot be more than 100 stream (dcXX or wbXX) */
648 avi_track_toread_t toread[100];
651 /* detect new selected/unselected streams */
652 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
654 avi_track_t *tk = p_sys->track[i_track];
657 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
658 if( b && !tk->b_activated )
660 if( p_sys->b_seekable)
662 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
664 tk->b_activated = VLC_TRUE;
666 else if( !b && tk->b_activated )
668 tk->b_activated = VLC_FALSE;
676 if( i_track_count <= 0 )
678 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
680 p_sys->i_time += 25*1000; /* read 25ms */
683 if( p_sys->i_time >= i_length )
687 msg_Warn( p_demux, "no track selected, exiting..." );
691 /* wait for the good time */
692 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
693 p_sys->i_time += 25*1000; /* read 25ms */
696 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
698 avi_track_t *tk = p_sys->track[i_track];
701 toread[i_track].b_ok = tk->b_activated;
702 if( tk->i_idxposc < tk->i_idxnb )
704 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
705 if( tk->i_idxposb > 0 )
707 toread[i_track].i_posf += 8 + tk->i_idxposb;
712 toread[i_track].i_posf = -1;
715 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
717 if( tk->i_samplesize )
719 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
723 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
728 toread[i_track].i_toread *= -1;
732 b_stream = VLC_FALSE;
743 /* search for first chunk to be read */
744 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
746 if( !toread[i].b_ok ||
747 AVI_GetDPTS( p_sys->track[i],
748 toread[i].i_toread ) <= -25 * 1000 )
753 if( toread[i].i_toread > 0 )
755 b_done = VLC_FALSE; /* not yet finished */
757 if( toread[i].i_posf > 0 )
759 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
762 i_pos = toread[i].i_posf;
774 int i_loop_count = 0;
776 /* no valid index, we will parse directly the stream
777 * in case we fail we will disable all finished stream */
778 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
780 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
781 if( AVI_PacketNext( p_demux ) )
783 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
788 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
795 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
798 "cannot get packet header, track disabled" );
799 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
801 if( avi_pk.i_stream >= p_sys->i_track ||
802 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
804 if( AVI_PacketNext( p_demux ) )
807 "cannot skip packet, track disabled" );
808 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
811 /* Prevents from eating all the CPU with broken files.
812 * This value should be low enough so that it doesn't
813 * affect the reading speed too much. */
814 if( !(++i_loop_count % 1024) )
816 if( p_demux->b_die ) return -1;
819 if( !(i_loop_count % (1024 * 10)) )
821 "don't seem to find any data..." );
827 /* add this chunk to the index */
830 index.i_id = avi_pk.i_fourcc;
832 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
834 index.i_pos = avi_pk.i_pos;
835 index.i_length = avi_pk.i_size;
836 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
838 i_track = avi_pk.i_stream;
839 tk = p_sys->track[i_track];
840 /* do we will read this data ? */
841 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
847 if( AVI_PacketNext( p_demux ) )
850 "cannot skip packet, track disabled" );
851 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
860 stream_Seek( p_demux->s, i_pos );
863 /* Set the track to use */
864 tk = p_sys->track[i_track];
866 /* read thoses data */
867 if( tk->i_samplesize )
869 unsigned int i_toread;
871 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
873 if( tk->i_samplesize > 1 )
875 i_toread = tk->i_samplesize;
879 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
880 i_toread = __MAX( i_toread, 100 );
883 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
889 i_size = tk->p_index[tk->i_idxposc].i_length;
892 if( tk->i_idxposb == 0 )
894 i_size += 8; /* need to read and skip header */
897 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
899 msg_Warn( p_demux, "failed reading data" );
900 tk->b_activated = VLC_FALSE;
901 toread[i_track].b_ok = VLC_FALSE;
904 if( i_size % 2 ) /* read was padded on word boundary */
909 if( tk->i_idxposb == 0 )
911 p_frame->p_buffer += 8;
912 p_frame->i_buffer -= 8;
914 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
915 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
917 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
921 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
925 if( tk->i_samplesize )
927 if( tk->i_idxposb == 0 )
931 toread[i_track].i_toread -= i_size;
932 tk->i_idxposb += i_size;
934 tk->p_index[tk->i_idxposc].i_length )
942 int i_length = tk->p_index[tk->i_idxposc].i_length;
945 if( tk->i_cat == AUDIO_ES )
947 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
949 toread[i_track].i_toread--;
952 if( tk->i_idxposc < tk->i_idxnb)
954 toread[i_track].i_posf =
955 tk->p_index[tk->i_idxposc].i_pos;
956 if( tk->i_idxposb > 0 )
958 toread[i_track].i_posf += 8 + tk->i_idxposb;
964 toread[i_track].i_posf = -1;
967 b_stream = VLC_TRUE; /* at least one read succeed */
969 if( tk->i_cat != VIDEO_ES )
970 p_frame->i_dts = p_frame->i_pts;
973 p_frame->i_dts = p_frame->i_pts;
977 //p_pes->i_rate = p_demux->stream.control.i_rate;
978 es_out_Send( p_demux->out, tk->p_es, p_frame );
983 /*****************************************************************************
984 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
985 *****************************************************************************
986 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
987 *****************************************************************************/
988 static int Demux_UnSeekable( demux_t *p_demux )
990 demux_sys_t *p_sys = p_demux->p_sys;
991 avi_track_t *p_stream_master = NULL;
992 unsigned int i_stream;
993 unsigned int i_packet;
995 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
997 /* *** find master stream for data packet skipping algo *** */
998 /* *** -> first video, if any, or first audio ES *** */
999 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1001 avi_track_t *tk = p_sys->track[i_stream];
1004 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1006 if( b && tk->i_cat == VIDEO_ES )
1008 p_stream_master = tk;
1012 p_stream_master = tk;
1016 if( !p_stream_master )
1018 msg_Warn( p_demux, "no more stream selected" );
1022 p_sys->i_time = AVI_GetPTS( p_stream_master );
1024 for( i_packet = 0; i_packet < 10; i_packet++)
1026 #define p_stream p_sys->track[avi_pk.i_stream]
1028 avi_packet_t avi_pk;
1030 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1035 if( avi_pk.i_stream >= p_sys->i_track ||
1036 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1038 /* we haven't found an audio or video packet:
1039 * - we have seek, found first next packet
1040 * - others packets could be found, skip them
1042 switch( avi_pk.i_fourcc )
1044 case AVIFOURCC_JUNK:
1045 case AVIFOURCC_LIST:
1046 case AVIFOURCC_RIFF:
1047 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1048 case AVIFOURCC_idx1:
1051 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1053 return( 0 ); /* eof */
1056 "seems to have lost position, resync" );
1057 if( AVI_PacketSearch( p_demux ) )
1059 msg_Err( p_demux, "resync failed" );
1066 /* check for time */
1067 if( __ABS( AVI_GetPTS( p_stream ) -
1068 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1070 /* load it and send to decoder */
1072 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1076 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1078 if( avi_pk.i_cat != VIDEO_ES )
1079 p_frame->i_dts = p_frame->i_pts;
1082 p_frame->i_dts = p_frame->i_pts;
1086 //p_pes->i_rate = p_demux->stream.control.i_rate;
1087 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1091 if( AVI_PacketNext( p_demux ) )
1097 /* *** update stream time position *** */
1098 if( p_stream->i_samplesize )
1100 p_stream->i_idxposb += avi_pk.i_size;
1104 if( p_stream->i_cat == AUDIO_ES )
1106 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1108 p_stream->i_idxposc++;
1118 /*****************************************************************************
1119 * Seek: goto to i_date or i_percent
1120 *****************************************************************************/
1121 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1124 demux_sys_t *p_sys = p_demux->p_sys;
1125 unsigned int i_stream;
1126 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1127 i_date / 1000000, i_percent );
1129 if( p_sys->b_seekable )
1131 if( !p_sys->i_length )
1133 avi_track_t *p_stream;
1136 /* use i_percent to create a true i_date */
1137 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1138 " work only for interleaved file", i_percent );
1139 if( i_percent >= 100 )
1141 msg_Warn( p_demux, "cannot seek so far !" );
1142 return VLC_EGENERIC;
1144 i_percent = __MAX( i_percent, 0 );
1146 /* try to find chunk that is at i_percent or the file */
1147 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1148 p_sys->i_movi_begin );
1149 /* search first selected stream */
1150 for( i_stream = 0, p_stream = NULL;
1151 i_stream < p_sys->i_track; i_stream++ )
1153 p_stream = p_sys->track[i_stream];
1154 if( p_stream->b_activated )
1159 if( !p_stream || !p_stream->b_activated )
1161 msg_Warn( p_demux, "cannot find any selected stream" );
1162 return VLC_EGENERIC;
1165 /* be sure that the index exist */
1166 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1168 msg_Warn( p_demux, "cannot seek" );
1169 return VLC_EGENERIC;
1172 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1173 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1175 /* search after i_idxposc */
1176 if( AVI_StreamChunkSet( p_demux,
1177 i_stream, p_stream->i_idxposc + 1 ) )
1179 msg_Warn( p_demux, "cannot seek" );
1180 return VLC_EGENERIC;
1184 i_date = AVI_GetPTS( p_stream );
1185 /* TODO better support for i_samplesize != 0 */
1186 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1189 #define p_stream p_sys->track[i_stream]
1191 /* seek for chunk based streams */
1192 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1194 if( p_stream->b_activated && !p_stream->i_samplesize )
1195 /* if( p_stream->b_activated ) */
1197 AVI_TrackSeek( p_demux, i_stream, i_date );
1198 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1205 i_date = p_sys->i_time;
1207 /* seek for bytes based streams */
1208 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1210 if( p_stream->b_activated && p_stream->i_samplesize )
1212 AVI_TrackSeek( p_demux, i_stream, i_date );
1213 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1216 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1217 /* set true movie time */
1219 if( !p_sys->i_time )
1221 p_sys->i_time = i_date;
1228 msg_Err( p_demux, "shouldn't yet be executed" );
1229 return VLC_EGENERIC;
1233 /*****************************************************************************
1235 *****************************************************************************
1237 *****************************************************************************/
1238 static double ControlGetPosition( demux_t *p_demux )
1240 demux_sys_t *p_sys = p_demux->p_sys;
1242 if( p_sys->i_length > 0 )
1244 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1246 else if( stream_Size( p_demux->s ) > 0 )
1252 /* search the more advanced selected es */
1253 for( i = 0; i < p_sys->i_track; i++ )
1255 avi_track_t *tk = p_sys->track[i];
1256 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1258 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1259 tk->p_index[tk->i_idxposc].i_length + 8;
1266 return (double)i64 / (double)stream_Size( p_demux->s );
1271 static int Control( demux_t *p_demux, int i_query, va_list args )
1273 demux_sys_t *p_sys = p_demux->p_sys;
1277 vlc_meta_t **pp_meta;
1281 case DEMUX_GET_POSITION:
1282 pf = (double*)va_arg( args, double * );
1283 *pf = ControlGetPosition( p_demux );
1285 case DEMUX_SET_POSITION:
1286 f = (double)va_arg( args, double );
1287 if( p_sys->b_seekable )
1289 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1290 return Seek( p_demux, i64, (int)(f * 100) );
1294 int64_t i_pos = stream_Size( p_demux->s ) * f;
1295 return stream_Seek( p_demux->s, i_pos );
1298 case DEMUX_GET_TIME:
1299 pi64 = (int64_t*)va_arg( args, int64_t * );
1300 *pi64 = p_sys->i_time;
1303 case DEMUX_SET_TIME:
1307 i64 = (int64_t)va_arg( args, int64_t );
1308 if( p_sys->i_length > 0 )
1310 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1312 else if( p_sys->i_time > 0 )
1314 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1315 (double)i64 / (double)p_sys->i_time );
1317 return Seek( p_demux, i64, i_percent );
1319 case DEMUX_GET_LENGTH:
1320 pi64 = (int64_t*)va_arg( args, int64_t * );
1321 *pi64 = p_sys->i_length * (mtime_t)1000000;
1325 pf = (double*)va_arg( args, double * );
1327 for( i = 0; i < (int)p_sys->i_track; i++ )
1329 avi_track_t *tk = p_sys->track[i];
1330 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1332 *pf = (float)tk->i_rate / (float)tk->i_scale;
1337 case DEMUX_GET_META:
1338 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1339 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1343 return VLC_EGENERIC;
1347 /*****************************************************************************
1348 * Function to convert pts to chunk or byte
1349 *****************************************************************************/
1351 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1356 return (mtime_t)((int64_t)i_pts *
1357 (int64_t)tk->i_rate /
1358 (int64_t)tk->i_scale /
1361 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1363 if( !tk->i_scale || !tk->i_samplesize )
1366 return (mtime_t)((int64_t)i_pts *
1367 (int64_t)tk->i_rate /
1368 (int64_t)tk->i_scale /
1370 (int64_t)tk->i_samplesize );
1373 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1380 i_dpts = (mtime_t)( (int64_t)1000000 *
1382 (int64_t)tk->i_scale /
1383 (int64_t)tk->i_rate );
1385 if( tk->i_samplesize )
1387 return i_dpts / tk->i_samplesize;
1392 static mtime_t AVI_GetPTS( avi_track_t *tk )
1394 if( tk->i_samplesize )
1396 int64_t i_count = 0;
1398 /* we need a valid entry we will emulate one */
1399 if( tk->i_idxposc == tk->i_idxnb )
1403 /* use the last entry */
1404 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1405 + tk->p_index[tk->i_idxnb - 1].i_length;
1410 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1412 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1416 if( tk->i_cat == AUDIO_ES )
1418 return AVI_GetDPTS( tk, tk->i_blockno );
1422 return AVI_GetDPTS( tk, tk->i_idxposc );
1427 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1429 demux_sys_t *p_sys = p_demux->p_sys;
1430 avi_packet_t avi_pk;
1431 int i_loop_count = 0;
1433 /* find first chunk of i_stream that isn't in index */
1435 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1437 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1438 if( AVI_PacketNext( p_demux ) )
1440 return VLC_EGENERIC;
1445 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1450 if( p_demux->b_die ) return VLC_EGENERIC;
1452 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1454 msg_Warn( p_demux, "cannot get packet header" );
1455 return VLC_EGENERIC;
1457 if( avi_pk.i_stream >= p_sys->i_track ||
1458 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1460 if( AVI_PacketNext( p_demux ) )
1462 return VLC_EGENERIC;
1465 /* Prevents from eating all the CPU with broken files.
1466 * This value should be low enough so that it doesn't
1467 * affect the reading speed too much. */
1468 if( !(++i_loop_count % 1024) )
1470 if( p_demux->b_die ) return VLC_EGENERIC;
1473 if( !(i_loop_count % (1024 * 10)) )
1474 msg_Warn( p_demux, "don't seem to find any data..." );
1479 /* add this chunk to the index */
1482 index.i_id = avi_pk.i_fourcc;
1484 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1486 index.i_pos = avi_pk.i_pos;
1487 index.i_length = avi_pk.i_size;
1488 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1490 if( avi_pk.i_stream == i_stream )
1495 if( AVI_PacketNext( p_demux ) )
1497 return VLC_EGENERIC;
1503 /* be sure that i_ck will be a valid index entry */
1504 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1507 demux_sys_t *p_sys = p_demux->p_sys;
1508 avi_track_t *p_stream = p_sys->track[i_stream];
1510 p_stream->i_idxposc = i_ck;
1511 p_stream->i_idxposb = 0;
1513 if( i_ck >= p_stream->i_idxnb )
1515 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1518 p_stream->i_idxposc++;
1519 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1521 return VLC_EGENERIC;
1524 } while( p_stream->i_idxposc < i_ck );
1530 /* XXX FIXME up to now, we assume that all chunk are one after one */
1531 static int AVI_StreamBytesSet( demux_t *p_demux,
1532 unsigned int i_stream,
1535 demux_sys_t *p_sys = p_demux->p_sys;
1536 avi_track_t *p_stream = p_sys->track[i_stream];
1538 if( ( p_stream->i_idxnb > 0 )
1539 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1540 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1542 /* index is valid to find the ck */
1543 /* uses dichototmie to be fast enougth */
1544 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1545 int i_idxmax = p_stream->i_idxnb;
1549 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1551 i_idxmax = i_idxposc ;
1552 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1556 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1557 p_stream->p_index[i_idxposc].i_length <= i_byte)
1559 i_idxmin = i_idxposc ;
1560 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1564 p_stream->i_idxposc = i_idxposc;
1565 p_stream->i_idxposb = i_byte -
1566 p_stream->p_index[i_idxposc].i_lengthtotal;
1575 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1576 p_stream->i_idxposb = 0;
1579 p_stream->i_idxposc++;
1580 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1582 return VLC_EGENERIC;
1585 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1586 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1588 p_stream->i_idxposb = i_byte -
1589 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1594 static int AVI_TrackSeek( demux_t *p_demux,
1598 demux_sys_t *p_sys = p_demux->p_sys;
1599 avi_track_t *tk = p_sys->track[i_stream];
1601 #define p_stream p_sys->track[i_stream]
1604 i_oldpts = AVI_GetPTS( p_stream );
1606 if( !p_stream->i_samplesize )
1608 if( AVI_StreamChunkSet( p_demux,
1610 AVI_PTSToChunk( p_stream, i_date ) ) )
1612 return VLC_EGENERIC;
1615 if( p_stream->i_cat == AUDIO_ES )
1619 for( i = 0; i < tk->i_idxposc; i++ )
1621 if( tk->i_blocksize > 0 )
1623 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1633 "old:"I64Fd" %s new "I64Fd,
1635 i_oldpts > i_date ? ">" : "<",
1638 if( p_stream->i_cat == VIDEO_ES )
1640 /* search key frame */
1641 if( i_date < i_oldpts )
1643 while( p_stream->i_idxposc > 0 &&
1644 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1647 if( AVI_StreamChunkSet( p_demux,
1649 p_stream->i_idxposc - 1 ) )
1651 return VLC_EGENERIC;
1657 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1658 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1661 if( AVI_StreamChunkSet( p_demux,
1663 p_stream->i_idxposc + 1 ) )
1665 return VLC_EGENERIC;
1673 if( AVI_StreamBytesSet( p_demux,
1675 AVI_PTSToByte( p_stream, i_date ) ) )
1677 return VLC_EGENERIC;
1684 /****************************************************************************
1685 * Return VLC_TRUE if it's a key frame
1686 ****************************************************************************/
1687 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1693 * startcode: 0x00000100 32bits
1694 * framenumber ? 5bits
1695 * piture type 0(I),1(P) 2bits
1697 if( GetDWBE( p_byte ) != 0x00000100 )
1699 /* it's not an msmpegv1 stream, strange...*/
1700 return AVIIF_KEYFRAME;
1702 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1705 case FOURCC_DIV3: /* wmv1 also */
1707 * picture type 0(I),1(P) 2bits
1709 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1711 /* we should find first occurrence of 0x000001b6 (32bits)
1712 * startcode: 0x000001b6 32bits
1713 * piture type 0(I),1(P) 2bits
1715 if( GetDWBE( p_byte ) != 0x000001b6 )
1717 /* not true , need to find the first VOP header */
1718 return AVIIF_KEYFRAME;
1720 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1723 /* I can't do it, so say yes */
1724 return AVIIF_KEYFRAME;
1728 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1733 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1737 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1741 return VLC_FOURCC('m','r','l','e');
1790 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1794 /****************************************************************************
1796 ****************************************************************************/
1797 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1798 int *pi_number, int *pi_type )
1800 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1803 c1 = ((uint8_t *)&i_id)[0];
1804 c2 = ((uint8_t *)&i_id)[1];
1806 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1808 SET_PTR( pi_number, 100 ); /* > max stream number */
1809 SET_PTR( pi_type, UNKNOWN_ES );
1813 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1814 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1817 SET_PTR( pi_type, AUDIO_ES );
1821 SET_PTR( pi_type, VIDEO_ES );
1824 SET_PTR( pi_type, UNKNOWN_ES );
1831 /****************************************************************************
1833 ****************************************************************************/
1834 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1838 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1840 return VLC_EGENERIC;
1842 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1843 p_pk->i_size = GetDWLE( p_peek + 4 );
1844 p_pk->i_pos = stream_Tell( p_demux->s );
1845 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1847 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1848 p_peek[10], p_peek[11] );
1855 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1857 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1861 static int AVI_PacketNext( demux_t *p_demux )
1863 avi_packet_t avi_ck;
1866 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1868 return VLC_EGENERIC;
1871 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1872 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1876 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1877 avi_ck.i_type == AVIFOURCC_AVIX )
1883 i_skip = __EVEN( avi_ck.i_size ) + 8;
1886 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1888 return VLC_EGENERIC;
1893 static int AVI_PacketRead( demux_t *p_demux,
1895 block_t **pp_frame )
1899 i_size = __EVEN( p_pk->i_size + 8 );
1901 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1903 return VLC_EGENERIC;
1905 (*pp_frame)->p_buffer += 8;
1906 (*pp_frame)->i_buffer -= 8;
1908 if( i_size != p_pk->i_size + 8 )
1910 (*pp_frame)->i_buffer--;
1916 static int AVI_PacketSearch( demux_t *p_demux )
1918 demux_sys_t *p_sys = p_demux->p_sys;
1919 avi_packet_t avi_pk;
1924 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1926 return VLC_EGENERIC;
1928 AVI_PacketGetHeader( p_demux, &avi_pk );
1929 if( avi_pk.i_stream < p_sys->i_track &&
1930 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1934 switch( avi_pk.i_fourcc )
1936 case AVIFOURCC_JUNK:
1937 case AVIFOURCC_LIST:
1938 case AVIFOURCC_RIFF:
1939 case AVIFOURCC_idx1:
1943 /* Prevents from eating all the CPU with broken files.
1944 * This value should be low enough so that it doesn't affect the
1945 * reading speed too much (not that we care much anyway because
1946 * this code is called only on broken files). */
1947 if( !(++i_count % 1024) )
1949 if( p_demux->b_die ) return VLC_EGENERIC;
1952 if( !(i_count % (1024 * 10)) )
1953 msg_Warn( p_demux, "trying to resync..." );
1958 /****************************************************************************
1960 ****************************************************************************/
1961 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1963 avi_entry_t *p_index)
1965 avi_track_t *tk = p_sys->track[i_stream];
1967 /* Update i_movi_lastchunk_pos */
1968 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1970 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1974 if( tk->i_idxnb >= tk->i_idxmax )
1976 tk->i_idxmax += 16384;
1977 tk->p_index = realloc( tk->p_index,
1978 tk->i_idxmax * sizeof( avi_entry_t ) );
1979 if( tk->p_index == NULL )
1984 /* calculate cumulate length */
1985 if( tk->i_idxnb > 0 )
1987 p_index->i_lengthtotal =
1988 tk->p_index[tk->i_idxnb - 1].i_length +
1989 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1993 p_index->i_lengthtotal = 0;
1996 tk->p_index[tk->i_idxnb++] = *p_index;
1999 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2001 demux_sys_t *p_sys = p_demux->p_sys;
2003 avi_chunk_list_t *p_riff;
2004 avi_chunk_list_t *p_movi;
2005 avi_chunk_idx1_t *p_idx1;
2007 unsigned int i_stream;
2008 unsigned int i_index;
2012 vlc_bool_t b_keyset[100];
2014 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2015 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2016 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2020 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2021 return VLC_EGENERIC;
2024 /* *** calculate offset *** */
2025 /* Well, avi is __SHIT__ so test more than one entry
2026 * (needed for some avi files) */
2028 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2030 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2032 i_offset = p_movi->i_chunk_pos + 8;
2037 /* Reset b_keyset */
2038 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2039 b_keyset[i_stream] = VLC_FALSE;
2041 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2045 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2048 if( i_stream < p_sys->i_track &&
2049 i_cat == p_sys->track[i_stream]->i_cat )
2052 index.i_id = p_idx1->entry[i_index].i_fourcc;
2054 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2055 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2056 index.i_length = p_idx1->entry[i_index].i_length;
2057 AVI_IndexAddEntry( p_sys, i_stream, &index );
2059 if( index.i_flags&AVIIF_KEYFRAME )
2060 b_keyset[i_stream] = VLC_TRUE;
2064 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2066 if( !b_keyset[i_stream] )
2068 avi_track_t *tk = p_sys->track[i_stream];
2070 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2071 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2072 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2078 static void __Parse_indx( demux_t *p_demux,
2080 avi_chunk_indx_t *p_indx )
2082 demux_sys_t *p_sys = p_demux->p_sys;
2086 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2087 if( p_indx->i_indexsubtype == 0 )
2089 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2091 index.i_id = p_indx->i_id;
2092 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2093 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2094 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2096 AVI_IndexAddEntry( p_sys, i_stream, &index );
2099 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2101 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2103 index.i_id = p_indx->i_id;
2104 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2105 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2106 index.i_length = p_indx->idx.field[i].i_size;
2108 AVI_IndexAddEntry( p_sys, i_stream, &index );
2113 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2117 static void AVI_IndexLoad_indx( demux_t *p_demux )
2119 demux_sys_t *p_sys = p_demux->p_sys;
2120 unsigned int i_stream;
2123 avi_chunk_list_t *p_riff;
2124 avi_chunk_list_t *p_hdrl;
2126 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2127 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2129 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2131 avi_chunk_list_t *p_strl;
2132 avi_chunk_indx_t *p_indx;
2134 #define p_stream p_sys->track[i_stream]
2135 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2136 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2140 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2144 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2146 __Parse_indx( p_demux, i_stream, p_indx );
2148 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2151 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2153 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2154 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2158 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2163 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2169 static void AVI_IndexLoad( demux_t *p_demux )
2171 demux_sys_t *p_sys = p_demux->p_sys;
2172 unsigned int i_stream;
2174 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2176 p_sys->track[i_stream]->i_idxnb = 0;
2177 p_sys->track[i_stream]->i_idxmax = 0;
2178 p_sys->track[i_stream]->p_index = NULL;
2183 AVI_IndexLoad_indx( p_demux );
2185 else if( AVI_IndexLoad_idx1( p_demux ) )
2187 /* try indx if idx1 failed as some "normal" file have indx too */
2188 AVI_IndexLoad_indx( p_demux );
2191 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2193 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2194 i_stream, p_sys->track[i_stream]->i_idxnb );
2198 static void AVI_IndexCreate( demux_t *p_demux )
2200 demux_sys_t *p_sys = p_demux->p_sys;
2202 avi_chunk_list_t *p_riff;
2203 avi_chunk_list_t *p_movi;
2205 unsigned int i_stream;
2208 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2209 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2213 msg_Err( p_demux, "cannot find p_movi" );
2217 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2219 p_sys->track[i_stream]->i_idxnb = 0;
2220 p_sys->track[i_stream]->i_idxmax = 0;
2221 p_sys->track[i_stream]->p_index = NULL;
2223 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2224 stream_Size( p_demux->s ) );
2226 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2227 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2232 if( p_demux->b_die )
2237 if( AVI_PacketGetHeader( p_demux, &pk ) )
2241 if( pk.i_stream < p_sys->i_track &&
2242 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2245 index.i_id = pk.i_fourcc;
2247 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2248 index.i_pos = pk.i_pos;
2249 index.i_length = pk.i_size;
2250 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2254 switch( pk.i_fourcc )
2256 case AVIFOURCC_idx1:
2259 avi_chunk_list_t *p_sysx;
2260 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2261 AVIFOURCC_RIFF, 1 );
2263 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2264 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2271 case AVIFOURCC_RIFF:
2272 msg_Dbg( p_demux, "new RIFF chunk found" );
2274 case AVIFOURCC_JUNK:
2277 msg_Warn( p_demux, "need resync, probably broken avi" );
2278 if( AVI_PacketSearch( p_demux ) )
2280 msg_Warn( p_demux, "lost sync, abord index creation" );
2286 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2287 AVI_PacketNext( p_demux ) )
2294 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2297 "stream[%d] creating %d index entries",
2299 p_sys->track[i_stream]->i_idxnb );
2303 /*****************************************************************************
2305 *****************************************************************************/
2306 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2308 demux_sys_t *p_sys = p_demux->p_sys;
2310 int b_end = VLC_TRUE;
2312 for( i = 0; i < p_sys->i_track; i++ )
2314 avi_track_t *tk = p_sys->track[i];
2315 if( tk->i_idxposc >= tk->i_idxnb )
2317 tk->b_activated = VLC_FALSE;
2318 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2328 /****************************************************************************
2329 * AVI_MovieGetLength give max streams length in second
2330 ****************************************************************************/
2331 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2333 demux_sys_t *p_sys = p_demux->p_sys;
2334 mtime_t i_maxlength = 0;
2337 for( i = 0; i < p_sys->i_track; i++ )
2339 avi_track_t *tk = p_sys->track[i];
2342 /* fix length for each stream */
2343 if( tk->i_idxnb < 1 || !tk->p_index )
2348 if( tk->i_samplesize )
2350 i_length = AVI_GetDPTS( tk,
2351 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2352 tk->p_index[tk->i_idxnb-1].i_length );
2356 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2358 i_length /= (mtime_t)1000000; /* in seconds */
2361 "stream[%d] length:"I64Fd" (based on index)",
2364 i_maxlength = __MAX( i_maxlength, i_length );