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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
31 #include <vlc_interaction.h>
38 /*****************************************************************************
40 *****************************************************************************/
42 #define INTERLEAVE_TEXT N_("Force interleaved method" )
43 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
45 #define INDEX_TEXT N_("Force index creation")
46 #define INDEX_LONGTEXT N_( \
47 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
48 "or incomplete (not seekable)." )
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_shortname( "AVI" );
55 set_description( _("AVI demuxer") );
56 set_capability( "demux2", 212 );
57 set_category( CAT_INPUT );
58 set_subcategory( SUBCAT_INPUT_DEMUX );
60 add_bool( "avi-interleaved", 0, NULL,
61 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
62 add_bool( "avi-index", 0, NULL,
63 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
65 set_callbacks( Open, Close );
68 /*****************************************************************************
70 *****************************************************************************/
71 static int Control ( demux_t *, int, va_list );
72 static int Seek ( demux_t *, mtime_t, int );
73 static int Demux_Seekable ( demux_t * );
74 static int Demux_UnSeekable( demux_t * );
76 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
77 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
81 vlc_fourcc_t i_fourcc;
84 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
86 uint8_t i_peek[8]; /* first 8 bytes */
88 unsigned int i_stream;
99 uint32_t i_lengthtotal;
105 vlc_bool_t b_activated;
107 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
108 vlc_fourcc_t i_codec;
116 avi_entry_t *p_index;
117 unsigned int i_idxnb;
118 unsigned int i_idxmax;
120 unsigned int i_idxposc; /* numero of chunk */
121 unsigned int i_idxposb; /* byte in the current chunk */
123 /* For VBR audio only */
124 unsigned int i_blockno;
125 unsigned int i_blocksize;
133 vlc_bool_t b_seekable;
139 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
141 /* number of streams and information */
142 unsigned int i_track;
153 static inline off_t __EVEN( off_t i )
155 return (i & 1) ? i + 1 : i;
158 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
159 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
160 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
161 static mtime_t AVI_GetPTS ( avi_track_t * );
164 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
165 static int AVI_StreamChunkSet ( demux_t *,
166 unsigned int i_stream, unsigned int i_ck );
167 static int AVI_StreamBytesSet ( demux_t *,
168 unsigned int i_stream, off_t i_byte );
170 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
171 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
173 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
174 static int AVI_PacketNext ( demux_t * );
175 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
176 static int AVI_PacketSearch ( demux_t * );
178 static void AVI_IndexLoad ( demux_t * );
179 static void AVI_IndexCreate ( demux_t * );
180 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
182 static mtime_t AVI_MovieGetLength( demux_t * );
184 /*****************************************************************************
186 *****************************************************************************/
187 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
188 static int AVI_TrackStopFinishedStreams( demux_t *);
191 - For VBR mp3 stream:
192 count blocks by rounded-up chunksizes instead of chunks
193 we need full emulation of dshow avi demuxer bugs :(
194 fixes silly nandub-style a-v delaying in avi with vbr mp3...
195 (from mplayer 2002/08/02)
199 /*****************************************************************************
200 * Open: check file and initializes AVI structures
201 *****************************************************************************/
202 static int Open( vlc_object_t * p_this )
204 demux_t *p_demux = (demux_t *)p_this;
207 vlc_bool_t b_index = VLC_FALSE;
210 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
211 avi_chunk_list_t *p_hdrl, *p_movi;
212 avi_chunk_avih_t *p_avih;
214 unsigned int i_track;
215 unsigned int i, i_peeker;
219 /* Is it an avi file ? */
220 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
222 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
224 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) &&
225 !strncmp( (char *)&p_peek[8], "AVI ", 4 ) ) break;
228 if( i_peeker == 188 )
233 /* Initialize input structures. */
234 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
235 memset( p_sys, 0, sizeof( demux_sys_t ) );
238 p_sys->i_movi_lastchunk_pos = 0;
239 p_sys->b_odml = VLC_FALSE;
244 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
246 p_demux->pf_control = Control;
247 p_demux->pf_demux = Demux_Seekable;
249 /* For unseekable stream, automaticaly use Demux_UnSeekable */
250 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
252 p_demux->pf_demux = Demux_UnSeekable;
257 stream_Read( p_demux->s, NULL, i_peeker );
260 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
262 msg_Err( p_demux, "avi module discarded (invalid file)" );
266 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
268 unsigned int i_count =
269 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
271 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
272 for( i = 1; i < i_count; i++ )
274 avi_chunk_list_t *p_sysx;
276 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
277 if( p_sysx->i_type == AVIFOURCC_AVIX )
279 msg_Warn( p_demux, "detected OpenDML file" );
280 p_sys->b_odml = VLC_TRUE;
286 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
287 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
288 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
290 if( !p_hdrl || !p_movi )
292 msg_Err( p_demux, "avi module discarded (invalid file)" );
296 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
298 msg_Err( p_demux, "cannot find avih chunk" );
301 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
302 if( p_avih->i_streams != i_track )
305 "found %d stream but %d are declared",
306 i_track, p_avih->i_streams );
310 msg_Err( p_demux, "no stream defined!" );
314 /* print information on streams */
315 msg_Dbg( p_demux, "AVIH: %d stream, flags %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 if( ( p_sys->meta = vlc_meta_New() ) )
324 sprintf( buffer, "%s%s%s%s",
325 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
326 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
327 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
328 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
329 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
332 /* now read info on each stream and create ES */
333 for( i = 0 ; i < i_track; i++ )
335 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
336 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
337 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
338 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
339 avi_chunk_strf_auds_t *p_auds;
340 avi_chunk_strf_vids_t *p_vids;
343 tk->b_activated = VLC_FALSE;
353 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
354 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
356 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
358 msg_Warn( p_demux, "stream[%d] incomplete", i );
362 tk->i_rate = p_strh->i_rate;
363 tk->i_scale = p_strh->i_scale;
364 tk->i_samplesize = p_strh->i_samplesize;
365 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
366 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
368 switch( p_strh->i_type )
370 case( AVIFOURCC_auds ):
371 tk->i_cat = AUDIO_ES;
372 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
373 p_auds->p_wf->wFormatTag );
374 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
376 if( p_auds->p_wf->wFormatTag == 1 )
378 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
385 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
387 fmt.audio.i_channels = p_auds->p_wf->nChannels;
388 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
389 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
390 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
391 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
392 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
393 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
394 fmt.p_extra = &p_auds->p_wf[1];
395 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
396 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
397 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
400 case( AVIFOURCC_vids ):
401 tk->i_cat = VIDEO_ES;
402 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
403 p_vids->p_bih->biCompression );
404 if( p_vids->p_bih->biCompression == 0x00 )
406 switch( p_vids->p_bih->biBitCount )
409 tk->i_codec = VLC_FOURCC('R','V','3','2');
412 tk->i_codec = VLC_FOURCC('R','V','2','4');
415 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
418 tk->i_codec = VLC_FOURCC('R','V','1','5');
421 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
424 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
426 if( p_vids->p_bih->biBitCount == 24 )
428 /* This is in BGR format */
429 fmt.video.i_bmask = 0x00ff0000;
430 fmt.video.i_gmask = 0x0000ff00;
431 fmt.video.i_rmask = 0x000000ff;
436 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
437 if( tk->i_codec == FOURCC_mp4v &&
438 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
440 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
443 tk->i_samplesize = 0;
444 fmt.video.i_width = p_vids->p_bih->biWidth;
445 fmt.video.i_height = p_vids->p_bih->biHeight;
446 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
447 fmt.video.i_frame_rate = tk->i_rate;
448 fmt.video.i_frame_rate_base = tk->i_scale;
450 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
451 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
452 fmt.p_extra = &p_vids->p_bih[1];
453 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
454 i, (char*)&p_vids->p_bih->biCompression,
455 p_vids->p_bih->biWidth,
456 p_vids->p_bih->biHeight,
457 p_vids->p_bih->biBitCount,
458 (float)tk->i_rate/(float)tk->i_scale );
460 if( p_vids->p_bih->biCompression == 0x00 )
462 /* RGB DIB are coded from bottom to top */
464 (unsigned int)(-(int)p_vids->p_bih->biHeight);
467 /* Extract palette from extradata if bpp <= 8
468 * (assumes that extradata contains only palette but appears
469 * to be true for all palettized codecs we support) */
470 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
471 fmt.video.i_bits_per_pixel > 0 )
475 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
476 fmt.video.p_palette->i_entries = 1;
478 /* Apparently this is necessary. But why ? */
480 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
481 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
483 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
484 GetDWLE((uint32_t*)fmt.p_extra + i);
489 case( AVIFOURCC_txts):
491 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
492 msg_Dbg( p_demux, "stream[%d] subtitles", i );
493 es_format_Init( &fmt, SPU_ES, tk->i_codec );
496 case( AVIFOURCC_mids):
497 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
500 msg_Warn( p_demux, "stream[%d] unknown type", i );
506 /* The charset of p_strn is undefined */
507 EnsureUTF8( p_strn->p_str );
508 fmt.psz_description = strdup( p_strn->p_str );
510 tk->p_es = es_out_Add( p_demux->out, &fmt );
511 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
514 if( p_sys->i_track <= 0 )
516 msg_Err( p_demux, "no valid track" );
520 if( config_GetInt( p_demux, "avi-index" ) )
523 if( p_sys->b_seekable )
525 AVI_IndexCreate( p_demux );
529 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
530 AVI_IndexLoad( p_demux );
535 AVI_IndexLoad( p_demux );
538 /* *** movie length in sec *** */
539 p_sys->i_length = AVI_MovieGetLength( p_demux );
540 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
541 (mtime_t)p_avih->i_microsecperframe /
544 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
545 "axproximative or will have strange behaviour" );
549 i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
550 _( "This AVI file is broken. Seeking will not "
551 "work correctly.\nDo you want to "
552 "try to repair it (this might take a long time) ?" ) );
553 if( i_create == DIALOG_OK_YES )
556 msg_Dbg( p_demux, "Fixing AVI index" );
559 else if( i_create == DIALOG_CANCELLED )
562 p_demux->p_parent->b_die = VLC_TRUE;
568 /* fix some BeOS MediaKit generated file */
569 for( i = 0 ; i < p_sys->i_track; i++ )
571 avi_track_t *tk = p_sys->track[i];
572 avi_chunk_list_t *p_strl;
573 avi_chunk_strh_t *p_strh;
574 avi_chunk_strf_auds_t *p_auds;
576 if( tk->i_cat != AUDIO_ES )
580 if( tk->i_idxnb < 1 ||
582 tk->i_samplesize != 0 )
586 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
587 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
588 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
590 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
591 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
593 int64_t i_track_length =
594 tk->p_index[tk->i_idxnb-1].i_length +
595 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
596 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
597 (mtime_t)p_avih->i_microsecperframe;
601 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
604 tk->i_samplesize = 1;
605 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
606 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
610 if( p_sys->b_seekable )
612 /* we have read all chunk so go back to movi */
613 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
615 /* Skip movi header */
616 stream_Read( p_demux->s, NULL, 12 );
618 p_sys->i_movi_begin = p_movi->i_chunk_pos;
624 vlc_meta_Delete( p_sys->meta );
626 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
631 /*****************************************************************************
632 * Close: frees unused data
633 *****************************************************************************/
634 static void Close ( vlc_object_t * p_this )
636 demux_t * p_demux = (demux_t *)p_this;
638 demux_sys_t *p_sys = p_demux->p_sys ;
640 for( i = 0; i < p_sys->i_track; i++ )
642 if( p_sys->track[i] )
644 FREE( p_sys->track[i]->p_index );
645 free( p_sys->track[i] );
648 FREE( p_sys->track );
649 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
650 vlc_meta_Delete( p_sys->meta );
655 /*****************************************************************************
656 * Demux_Seekable: reads and demuxes data packets for stream seekable
657 *****************************************************************************
658 * AVIDemux: reads and demuxes data packets
659 *****************************************************************************
660 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
661 *****************************************************************************/
668 off_t i_posf; /* where we will read :
669 if i_idxposb == 0 : begining of chunk (+8 to acces data)
670 else : point on data directly */
671 } avi_track_toread_t;
673 static int Demux_Seekable( demux_t *p_demux )
675 demux_sys_t *p_sys = p_demux->p_sys;
677 unsigned int i_track_count = 0;
678 unsigned int i_track;
680 /* cannot be more than 100 stream (dcXX or wbXX) */
681 avi_track_toread_t toread[100];
684 /* detect new selected/unselected streams */
685 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
687 avi_track_t *tk = p_sys->track[i_track];
690 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
691 if( b && !tk->b_activated )
693 if( p_sys->b_seekable)
695 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
697 tk->b_activated = VLC_TRUE;
699 else if( !b && tk->b_activated )
701 tk->b_activated = VLC_FALSE;
709 if( i_track_count <= 0 )
711 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
713 p_sys->i_time += 25*1000; /* read 25ms */
716 if( p_sys->i_time >= i_length )
720 msg_Warn( p_demux, "no track selected, exiting..." );
724 /* wait for the good time */
725 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
726 p_sys->i_time += 25*1000; /* read 25ms */
729 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
731 avi_track_t *tk = p_sys->track[i_track];
734 toread[i_track].b_ok = tk->b_activated;
735 if( tk->i_idxposc < tk->i_idxnb )
737 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
738 if( tk->i_idxposb > 0 )
740 toread[i_track].i_posf += 8 + tk->i_idxposb;
745 toread[i_track].i_posf = -1;
748 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
750 if( tk->i_samplesize )
752 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
756 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
761 toread[i_track].i_toread *= -1;
765 b_stream = VLC_FALSE;
776 /* search for first chunk to be read */
777 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
779 if( !toread[i].b_ok ||
780 AVI_GetDPTS( p_sys->track[i],
781 toread[i].i_toread ) <= -25 * 1000 )
786 if( toread[i].i_toread > 0 )
788 b_done = VLC_FALSE; /* not yet finished */
790 if( toread[i].i_posf > 0 )
792 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
795 i_pos = toread[i].i_posf;
807 int i_loop_count = 0;
809 /* no valid index, we will parse directly the stream
810 * in case we fail we will disable all finished stream */
811 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
813 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
814 if( AVI_PacketNext( p_demux ) )
816 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
821 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
828 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
831 "cannot get packet header, track disabled" );
832 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
834 if( avi_pk.i_stream >= p_sys->i_track ||
835 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
837 if( AVI_PacketNext( p_demux ) )
840 "cannot skip packet, track disabled" );
841 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
844 /* Prevents from eating all the CPU with broken files.
845 * This value should be low enough so that it doesn't
846 * affect the reading speed too much. */
847 if( !(++i_loop_count % 1024) )
849 if( p_demux->b_die ) return -1;
852 if( !(i_loop_count % (1024 * 10)) )
854 "don't seem to find any data..." );
860 /* add this chunk to the index */
863 index.i_id = avi_pk.i_fourcc;
865 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
867 index.i_pos = avi_pk.i_pos;
868 index.i_length = avi_pk.i_size;
869 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
871 i_track = avi_pk.i_stream;
872 tk = p_sys->track[i_track];
873 /* do we will read this data ? */
874 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
880 if( AVI_PacketNext( p_demux ) )
883 "cannot skip packet, track disabled" );
884 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
893 stream_Seek( p_demux->s, i_pos );
896 /* Set the track to use */
897 tk = p_sys->track[i_track];
899 /* read thoses data */
900 if( tk->i_samplesize )
902 unsigned int i_toread;
904 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
906 if( tk->i_samplesize > 1 )
908 i_toread = tk->i_samplesize;
912 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
913 i_toread = __MAX( i_toread, 100 );
916 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
922 i_size = tk->p_index[tk->i_idxposc].i_length;
925 if( tk->i_idxposb == 0 )
927 i_size += 8; /* need to read and skip header */
930 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
932 msg_Warn( p_demux, "failed reading data" );
933 tk->b_activated = VLC_FALSE;
934 toread[i_track].b_ok = VLC_FALSE;
937 if( i_size % 2 ) /* read was padded on word boundary */
942 if( tk->i_idxposb == 0 )
944 p_frame->p_buffer += 8;
945 p_frame->i_buffer -= 8;
947 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
948 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
950 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
954 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
958 if( tk->i_samplesize )
960 if( tk->i_idxposb == 0 )
964 toread[i_track].i_toread -= i_size;
965 tk->i_idxposb += i_size;
967 tk->p_index[tk->i_idxposc].i_length )
975 int i_length = tk->p_index[tk->i_idxposc].i_length;
978 if( tk->i_cat == AUDIO_ES )
980 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
982 toread[i_track].i_toread--;
985 if( tk->i_idxposc < tk->i_idxnb)
987 toread[i_track].i_posf =
988 tk->p_index[tk->i_idxposc].i_pos;
989 if( tk->i_idxposb > 0 )
991 toread[i_track].i_posf += 8 + tk->i_idxposb;
997 toread[i_track].i_posf = -1;
1000 b_stream = VLC_TRUE; /* at least one read succeed */
1002 if( tk->i_cat != VIDEO_ES )
1003 p_frame->i_dts = p_frame->i_pts;
1006 p_frame->i_dts = p_frame->i_pts;
1010 //p_pes->i_rate = p_demux->stream.control.i_rate;
1011 es_out_Send( p_demux->out, tk->p_es, p_frame );
1016 /*****************************************************************************
1017 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1018 *****************************************************************************
1019 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1020 *****************************************************************************/
1021 static int Demux_UnSeekable( demux_t *p_demux )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1024 avi_track_t *p_stream_master = NULL;
1025 unsigned int i_stream;
1026 unsigned int i_packet;
1028 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1030 /* *** find master stream for data packet skipping algo *** */
1031 /* *** -> first video, if any, or first audio ES *** */
1032 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1034 avi_track_t *tk = p_sys->track[i_stream];
1037 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1039 if( b && tk->i_cat == VIDEO_ES )
1041 p_stream_master = tk;
1045 p_stream_master = tk;
1049 if( !p_stream_master )
1051 msg_Warn( p_demux, "no more stream selected" );
1055 p_sys->i_time = AVI_GetPTS( p_stream_master );
1057 for( i_packet = 0; i_packet < 10; i_packet++)
1059 #define p_stream p_sys->track[avi_pk.i_stream]
1061 avi_packet_t avi_pk;
1063 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1068 if( avi_pk.i_stream >= p_sys->i_track ||
1069 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1071 /* we haven't found an audio or video packet:
1072 * - we have seek, found first next packet
1073 * - others packets could be found, skip them
1075 switch( avi_pk.i_fourcc )
1077 case AVIFOURCC_JUNK:
1078 case AVIFOURCC_LIST:
1079 case AVIFOURCC_RIFF:
1080 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1081 case AVIFOURCC_idx1:
1084 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1086 return( 0 ); /* eof */
1089 "seems to have lost position, resync" );
1090 if( AVI_PacketSearch( p_demux ) )
1092 msg_Err( p_demux, "resync failed" );
1099 /* check for time */
1100 if( __ABS( AVI_GetPTS( p_stream ) -
1101 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1103 /* load it and send to decoder */
1105 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1109 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1111 if( avi_pk.i_cat != VIDEO_ES )
1112 p_frame->i_dts = p_frame->i_pts;
1115 p_frame->i_dts = p_frame->i_pts;
1119 //p_pes->i_rate = p_demux->stream.control.i_rate;
1120 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1124 if( AVI_PacketNext( p_demux ) )
1130 /* *** update stream time position *** */
1131 if( p_stream->i_samplesize )
1133 p_stream->i_idxposb += avi_pk.i_size;
1137 if( p_stream->i_cat == AUDIO_ES )
1139 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1141 p_stream->i_idxposc++;
1151 /*****************************************************************************
1152 * Seek: goto to i_date or i_percent
1153 *****************************************************************************/
1154 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1157 demux_sys_t *p_sys = p_demux->p_sys;
1158 unsigned int i_stream;
1159 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1160 i_date / 1000000, i_percent );
1162 if( p_sys->b_seekable )
1164 if( !p_sys->i_length )
1166 avi_track_t *p_stream;
1169 /* use i_percent to create a true i_date */
1170 msg_Warn( p_demux, "seeking without index at %d%%"
1171 " only works for interleaved files", i_percent );
1172 if( i_percent >= 100 )
1174 msg_Warn( p_demux, "cannot seek so far !" );
1175 return VLC_EGENERIC;
1177 i_percent = __MAX( i_percent, 0 );
1179 /* try to find chunk that is at i_percent or the file */
1180 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1181 p_sys->i_movi_begin );
1182 /* search first selected stream */
1183 for( i_stream = 0, p_stream = NULL;
1184 i_stream < p_sys->i_track; i_stream++ )
1186 p_stream = p_sys->track[i_stream];
1187 if( p_stream->b_activated )
1192 if( !p_stream || !p_stream->b_activated )
1194 msg_Warn( p_demux, "cannot find any selected stream" );
1195 return VLC_EGENERIC;
1198 /* be sure that the index exist */
1199 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1201 msg_Warn( p_demux, "cannot seek" );
1202 return VLC_EGENERIC;
1205 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1206 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1208 /* search after i_idxposc */
1209 if( AVI_StreamChunkSet( p_demux,
1210 i_stream, p_stream->i_idxposc + 1 ) )
1212 msg_Warn( p_demux, "cannot seek" );
1213 return VLC_EGENERIC;
1217 i_date = AVI_GetPTS( p_stream );
1218 /* TODO better support for i_samplesize != 0 */
1219 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1222 #define p_stream p_sys->track[i_stream]
1224 /* seek for chunk based streams */
1225 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1227 if( p_stream->b_activated && !p_stream->i_samplesize )
1228 /* if( p_stream->b_activated ) */
1230 AVI_TrackSeek( p_demux, i_stream, i_date );
1231 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1238 i_date = p_sys->i_time;
1240 /* seek for bytes based streams */
1241 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1243 if( p_stream->b_activated && p_stream->i_samplesize )
1245 AVI_TrackSeek( p_demux, i_stream, i_date );
1246 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1249 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1250 /* set true movie time */
1252 if( !p_sys->i_time )
1254 p_sys->i_time = i_date;
1261 msg_Err( p_demux, "shouldn't yet be executed" );
1262 return VLC_EGENERIC;
1266 /*****************************************************************************
1268 *****************************************************************************
1270 *****************************************************************************/
1271 static double ControlGetPosition( demux_t *p_demux )
1273 demux_sys_t *p_sys = p_demux->p_sys;
1275 if( p_sys->i_length > 0 )
1277 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1279 else if( stream_Size( p_demux->s ) > 0 )
1285 /* search the more advanced selected es */
1286 for( i = 0; i < p_sys->i_track; i++ )
1288 avi_track_t *tk = p_sys->track[i];
1289 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1291 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1292 tk->p_index[tk->i_idxposc].i_length + 8;
1299 return (double)i64 / (double)stream_Size( p_demux->s );
1304 static int Control( demux_t *p_demux, int i_query, va_list args )
1306 demux_sys_t *p_sys = p_demux->p_sys;
1310 vlc_meta_t **pp_meta;
1314 case DEMUX_GET_POSITION:
1315 pf = (double*)va_arg( args, double * );
1316 *pf = ControlGetPosition( p_demux );
1318 case DEMUX_SET_POSITION:
1319 f = (double)va_arg( args, double );
1320 if( p_sys->b_seekable )
1322 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1323 return Seek( p_demux, i64, (int)(f * 100) );
1327 int64_t i_pos = stream_Size( p_demux->s ) * f;
1328 return stream_Seek( p_demux->s, i_pos );
1331 case DEMUX_GET_TIME:
1332 pi64 = (int64_t*)va_arg( args, int64_t * );
1333 *pi64 = p_sys->i_time;
1336 case DEMUX_SET_TIME:
1340 i64 = (int64_t)va_arg( args, int64_t );
1341 if( p_sys->i_length > 0 )
1343 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1345 else if( p_sys->i_time > 0 )
1347 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1348 (double)i64 / (double)p_sys->i_time );
1350 return Seek( p_demux, i64, i_percent );
1352 case DEMUX_GET_LENGTH:
1353 pi64 = (int64_t*)va_arg( args, int64_t * );
1354 *pi64 = p_sys->i_length * (mtime_t)1000000;
1358 pf = (double*)va_arg( args, double * );
1360 for( i = 0; i < (int)p_sys->i_track; i++ )
1362 avi_track_t *tk = p_sys->track[i];
1363 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1365 *pf = (float)tk->i_rate / (float)tk->i_scale;
1370 case DEMUX_GET_META:
1371 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1372 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1376 return VLC_EGENERIC;
1380 /*****************************************************************************
1381 * Function to convert pts to chunk or byte
1382 *****************************************************************************/
1384 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1389 return (mtime_t)((int64_t)i_pts *
1390 (int64_t)tk->i_rate /
1391 (int64_t)tk->i_scale /
1394 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1396 if( !tk->i_scale || !tk->i_samplesize )
1399 return (mtime_t)((int64_t)i_pts *
1400 (int64_t)tk->i_rate /
1401 (int64_t)tk->i_scale /
1403 (int64_t)tk->i_samplesize );
1406 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1413 i_dpts = (mtime_t)( (int64_t)1000000 *
1415 (int64_t)tk->i_scale /
1416 (int64_t)tk->i_rate );
1418 if( tk->i_samplesize )
1420 return i_dpts / tk->i_samplesize;
1425 static mtime_t AVI_GetPTS( avi_track_t *tk )
1427 if( tk->i_samplesize )
1429 int64_t i_count = 0;
1431 /* we need a valid entry we will emulate one */
1432 if( tk->i_idxposc == tk->i_idxnb )
1436 /* use the last entry */
1437 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1438 + tk->p_index[tk->i_idxnb - 1].i_length;
1443 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1445 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1449 if( tk->i_cat == AUDIO_ES )
1451 return AVI_GetDPTS( tk, tk->i_blockno );
1455 return AVI_GetDPTS( tk, tk->i_idxposc );
1460 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1462 demux_sys_t *p_sys = p_demux->p_sys;
1463 avi_packet_t avi_pk;
1464 int i_loop_count = 0;
1466 /* find first chunk of i_stream that isn't in index */
1468 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1470 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1471 if( AVI_PacketNext( p_demux ) )
1473 return VLC_EGENERIC;
1478 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1483 if( p_demux->b_die ) return VLC_EGENERIC;
1485 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1487 msg_Warn( p_demux, "cannot get packet header" );
1488 return VLC_EGENERIC;
1490 if( avi_pk.i_stream >= p_sys->i_track ||
1491 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1493 if( AVI_PacketNext( p_demux ) )
1495 return VLC_EGENERIC;
1498 /* Prevents from eating all the CPU with broken files.
1499 * This value should be low enough so that it doesn't
1500 * affect the reading speed too much. */
1501 if( !(++i_loop_count % 1024) )
1503 if( p_demux->b_die ) return VLC_EGENERIC;
1506 if( !(i_loop_count % (1024 * 10)) )
1507 msg_Warn( p_demux, "don't seem to find any data..." );
1512 /* add this chunk to the index */
1515 index.i_id = avi_pk.i_fourcc;
1517 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1519 index.i_pos = avi_pk.i_pos;
1520 index.i_length = avi_pk.i_size;
1521 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1523 if( avi_pk.i_stream == i_stream )
1528 if( AVI_PacketNext( p_demux ) )
1530 return VLC_EGENERIC;
1536 /* be sure that i_ck will be a valid index entry */
1537 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1540 demux_sys_t *p_sys = p_demux->p_sys;
1541 avi_track_t *p_stream = p_sys->track[i_stream];
1543 p_stream->i_idxposc = i_ck;
1544 p_stream->i_idxposb = 0;
1546 if( i_ck >= p_stream->i_idxnb )
1548 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1551 p_stream->i_idxposc++;
1552 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1554 return VLC_EGENERIC;
1557 } while( p_stream->i_idxposc < i_ck );
1563 /* XXX FIXME up to now, we assume that all chunk are one after one */
1564 static int AVI_StreamBytesSet( demux_t *p_demux,
1565 unsigned int i_stream,
1568 demux_sys_t *p_sys = p_demux->p_sys;
1569 avi_track_t *p_stream = p_sys->track[i_stream];
1571 if( ( p_stream->i_idxnb > 0 )
1572 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1573 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1575 /* index is valid to find the ck */
1576 /* uses dichototmie to be fast enougth */
1577 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1578 int i_idxmax = p_stream->i_idxnb;
1582 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1584 i_idxmax = i_idxposc ;
1585 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1589 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1590 p_stream->p_index[i_idxposc].i_length <= i_byte)
1592 i_idxmin = i_idxposc ;
1593 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1597 p_stream->i_idxposc = i_idxposc;
1598 p_stream->i_idxposb = i_byte -
1599 p_stream->p_index[i_idxposc].i_lengthtotal;
1608 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1609 p_stream->i_idxposb = 0;
1612 p_stream->i_idxposc++;
1613 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1615 return VLC_EGENERIC;
1618 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1619 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1621 p_stream->i_idxposb = i_byte -
1622 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1627 static int AVI_TrackSeek( demux_t *p_demux,
1631 demux_sys_t *p_sys = p_demux->p_sys;
1632 avi_track_t *tk = p_sys->track[i_stream];
1634 #define p_stream p_sys->track[i_stream]
1637 i_oldpts = AVI_GetPTS( p_stream );
1639 if( !p_stream->i_samplesize )
1641 if( AVI_StreamChunkSet( p_demux,
1643 AVI_PTSToChunk( p_stream, i_date ) ) )
1645 return VLC_EGENERIC;
1648 if( p_stream->i_cat == AUDIO_ES )
1652 for( i = 0; i < tk->i_idxposc; i++ )
1654 if( tk->i_blocksize > 0 )
1656 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1666 "old:"I64Fd" %s new "I64Fd,
1668 i_oldpts > i_date ? ">" : "<",
1671 if( p_stream->i_cat == VIDEO_ES )
1673 /* search key frame */
1674 if( i_date < i_oldpts )
1676 while( p_stream->i_idxposc > 0 &&
1677 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1680 if( AVI_StreamChunkSet( p_demux,
1682 p_stream->i_idxposc - 1 ) )
1684 return VLC_EGENERIC;
1690 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1691 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1694 if( AVI_StreamChunkSet( p_demux,
1696 p_stream->i_idxposc + 1 ) )
1698 return VLC_EGENERIC;
1706 if( AVI_StreamBytesSet( p_demux,
1708 AVI_PTSToByte( p_stream, i_date ) ) )
1710 return VLC_EGENERIC;
1717 /****************************************************************************
1718 * Return VLC_TRUE if it's a key frame
1719 ****************************************************************************/
1720 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1726 * startcode: 0x00000100 32bits
1727 * framenumber ? 5bits
1728 * piture type 0(I),1(P) 2bits
1730 if( GetDWBE( p_byte ) != 0x00000100 )
1732 /* it's not an msmpegv1 stream, strange...*/
1733 return AVIIF_KEYFRAME;
1735 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1738 case FOURCC_DIV3: /* wmv1 also */
1740 * picture type 0(I),1(P) 2bits
1742 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1744 /* we should find first occurrence of 0x000001b6 (32bits)
1745 * startcode: 0x000001b6 32bits
1746 * piture type 0(I),1(P) 2bits
1748 if( GetDWBE( p_byte ) != 0x000001b6 )
1750 /* not true , need to find the first VOP header */
1751 return AVIIF_KEYFRAME;
1753 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1756 /* I can't do it, so say yes */
1757 return AVIIF_KEYFRAME;
1761 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1766 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1770 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1774 return VLC_FOURCC('m','r','l','e');
1823 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1827 /****************************************************************************
1829 ****************************************************************************/
1830 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1831 int *pi_number, int *pi_type )
1833 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1836 c1 = ((uint8_t *)&i_id)[0];
1837 c2 = ((uint8_t *)&i_id)[1];
1839 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1841 SET_PTR( pi_number, 100 ); /* > max stream number */
1842 SET_PTR( pi_type, UNKNOWN_ES );
1846 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1847 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1850 SET_PTR( pi_type, AUDIO_ES );
1854 SET_PTR( pi_type, VIDEO_ES );
1857 SET_PTR( pi_type, UNKNOWN_ES );
1864 /****************************************************************************
1866 ****************************************************************************/
1867 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1871 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1873 return VLC_EGENERIC;
1875 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1876 p_pk->i_size = GetDWLE( p_peek + 4 );
1877 p_pk->i_pos = stream_Tell( p_demux->s );
1878 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1880 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1881 p_peek[10], p_peek[11] );
1888 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1890 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1894 static int AVI_PacketNext( demux_t *p_demux )
1896 avi_packet_t avi_ck;
1899 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1901 return VLC_EGENERIC;
1904 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1905 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1909 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1910 avi_ck.i_type == AVIFOURCC_AVIX )
1916 i_skip = __EVEN( avi_ck.i_size ) + 8;
1919 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1921 return VLC_EGENERIC;
1926 static int AVI_PacketRead( demux_t *p_demux,
1928 block_t **pp_frame )
1932 i_size = __EVEN( p_pk->i_size + 8 );
1934 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1936 return VLC_EGENERIC;
1938 (*pp_frame)->p_buffer += 8;
1939 (*pp_frame)->i_buffer -= 8;
1941 if( i_size != p_pk->i_size + 8 )
1943 (*pp_frame)->i_buffer--;
1949 static int AVI_PacketSearch( demux_t *p_demux )
1951 demux_sys_t *p_sys = p_demux->p_sys;
1952 avi_packet_t avi_pk;
1957 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1959 return VLC_EGENERIC;
1961 AVI_PacketGetHeader( p_demux, &avi_pk );
1962 if( avi_pk.i_stream < p_sys->i_track &&
1963 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1967 switch( avi_pk.i_fourcc )
1969 case AVIFOURCC_JUNK:
1970 case AVIFOURCC_LIST:
1971 case AVIFOURCC_RIFF:
1972 case AVIFOURCC_idx1:
1976 /* Prevents from eating all the CPU with broken files.
1977 * This value should be low enough so that it doesn't affect the
1978 * reading speed too much (not that we care much anyway because
1979 * this code is called only on broken files). */
1980 if( !(++i_count % 1024) )
1982 if( p_demux->b_die ) return VLC_EGENERIC;
1985 if( !(i_count % (1024 * 10)) )
1986 msg_Warn( p_demux, "trying to resync..." );
1991 /****************************************************************************
1993 ****************************************************************************/
1994 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1996 avi_entry_t *p_index)
1998 avi_track_t *tk = p_sys->track[i_stream];
2000 /* Update i_movi_lastchunk_pos */
2001 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2003 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2007 if( tk->i_idxnb >= tk->i_idxmax )
2009 tk->i_idxmax += 16384;
2010 tk->p_index = realloc( tk->p_index,
2011 tk->i_idxmax * sizeof( avi_entry_t ) );
2012 if( tk->p_index == NULL )
2017 /* calculate cumulate length */
2018 if( tk->i_idxnb > 0 )
2020 p_index->i_lengthtotal =
2021 tk->p_index[tk->i_idxnb - 1].i_length +
2022 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2026 p_index->i_lengthtotal = 0;
2029 tk->p_index[tk->i_idxnb++] = *p_index;
2032 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2034 demux_sys_t *p_sys = p_demux->p_sys;
2036 avi_chunk_list_t *p_riff;
2037 avi_chunk_list_t *p_movi;
2038 avi_chunk_idx1_t *p_idx1;
2040 unsigned int i_stream;
2041 unsigned int i_index;
2045 vlc_bool_t b_keyset[100];
2047 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2048 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2049 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2053 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2054 return VLC_EGENERIC;
2057 /* *** calculate offset *** */
2058 /* Well, avi is __SHIT__ so test more than one entry
2059 * (needed for some avi files) */
2061 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2063 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2065 i_offset = p_movi->i_chunk_pos + 8;
2070 /* Reset b_keyset */
2071 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2072 b_keyset[i_stream] = VLC_FALSE;
2074 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2078 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2081 if( i_stream < p_sys->i_track &&
2082 i_cat == p_sys->track[i_stream]->i_cat )
2085 index.i_id = p_idx1->entry[i_index].i_fourcc;
2087 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2088 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2089 index.i_length = p_idx1->entry[i_index].i_length;
2090 AVI_IndexAddEntry( p_sys, i_stream, &index );
2092 if( index.i_flags&AVIIF_KEYFRAME )
2093 b_keyset[i_stream] = VLC_TRUE;
2097 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2099 if( !b_keyset[i_stream] )
2101 avi_track_t *tk = p_sys->track[i_stream];
2103 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2104 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2105 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2111 static void __Parse_indx( demux_t *p_demux,
2113 avi_chunk_indx_t *p_indx )
2115 demux_sys_t *p_sys = p_demux->p_sys;
2119 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2120 if( p_indx->i_indexsubtype == 0 )
2122 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2124 index.i_id = p_indx->i_id;
2125 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2126 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2127 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2129 AVI_IndexAddEntry( p_sys, i_stream, &index );
2132 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2134 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2136 index.i_id = p_indx->i_id;
2137 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2138 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2139 index.i_length = p_indx->idx.field[i].i_size;
2141 AVI_IndexAddEntry( p_sys, i_stream, &index );
2146 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2150 static void AVI_IndexLoad_indx( demux_t *p_demux )
2152 demux_sys_t *p_sys = p_demux->p_sys;
2153 unsigned int i_stream;
2156 avi_chunk_list_t *p_riff;
2157 avi_chunk_list_t *p_hdrl;
2159 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2160 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2162 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2164 avi_chunk_list_t *p_strl;
2165 avi_chunk_indx_t *p_indx;
2167 #define p_stream p_sys->track[i_stream]
2168 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2169 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2173 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2178 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2180 __Parse_indx( p_demux, i_stream, p_indx );
2182 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2185 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2187 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2188 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2192 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2197 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2203 static void AVI_IndexLoad( demux_t *p_demux )
2205 demux_sys_t *p_sys = p_demux->p_sys;
2206 unsigned int i_stream;
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;
2217 AVI_IndexLoad_indx( p_demux );
2219 else if( AVI_IndexLoad_idx1( p_demux ) )
2221 /* try indx if idx1 failed as some "normal" file have indx too */
2222 AVI_IndexLoad_indx( p_demux );
2225 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2227 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2228 i_stream, p_sys->track[i_stream]->i_idxnb );
2232 static void AVI_IndexCreate( demux_t *p_demux )
2234 demux_sys_t *p_sys = p_demux->p_sys;
2236 avi_chunk_list_t *p_riff;
2237 avi_chunk_list_t *p_movi;
2239 unsigned int i_stream;
2242 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2243 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2247 msg_Err( p_demux, "cannot find p_movi" );
2251 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2253 p_sys->track[i_stream]->i_idxnb = 0;
2254 p_sys->track[i_stream]->i_idxmax = 0;
2255 p_sys->track[i_stream]->p_index = NULL;
2257 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2258 stream_Size( p_demux->s ) );
2260 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2261 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2264 /* Only show dialog if AVI is > 10MB */
2265 p_demux->p_sys->i_dialog_id = -1;
2266 if( stream_Size( p_demux->s ) > 10000000 )
2268 p_demux->p_sys->i_dialog_id = intf_UserProgress( p_demux,
2269 _( "Fixing AVI Index" ),
2270 _( "Creating AVI Index ..." ),
2272 p_demux->p_sys->last_update = mdate();
2279 if( p_demux->b_die )
2284 /* Don't update dialog too often */
2285 if( p_demux->p_sys->i_dialog_id > 0 &&
2286 mdate() - p_demux->p_sys->last_update > 100000 )
2288 int64_t i_pos = stream_Tell( p_demux->s )* 100 /
2289 stream_Size( p_demux->s );
2290 float f_pos = (float)i_pos;
2291 p_demux->p_sys->last_update = mdate();
2292 intf_UserProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
2293 _( "Creating AVI Index ..." ), f_pos );
2296 if( AVI_PacketGetHeader( p_demux, &pk ) )
2300 if( pk.i_stream < p_sys->i_track &&
2301 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2304 index.i_id = pk.i_fourcc;
2306 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2307 index.i_pos = pk.i_pos;
2308 index.i_length = pk.i_size;
2309 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2313 switch( pk.i_fourcc )
2315 case AVIFOURCC_idx1:
2318 avi_chunk_list_t *p_sysx;
2319 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2320 AVIFOURCC_RIFF, 1 );
2322 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2323 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2330 case AVIFOURCC_RIFF:
2331 msg_Dbg( p_demux, "new RIFF chunk found" );
2333 case AVIFOURCC_JUNK:
2336 msg_Warn( p_demux, "need resync, probably broken avi" );
2337 if( AVI_PacketSearch( p_demux ) )
2339 msg_Warn( p_demux, "lost sync, abord index creation" );
2345 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2346 AVI_PacketNext( p_demux ) )
2353 if( p_demux->p_sys->i_dialog_id > 0 )
2355 intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
2358 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2361 "stream[%d] creating %d index entries",
2363 p_sys->track[i_stream]->i_idxnb );
2367 /*****************************************************************************
2369 *****************************************************************************/
2370 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2372 demux_sys_t *p_sys = p_demux->p_sys;
2374 int b_end = VLC_TRUE;
2376 for( i = 0; i < p_sys->i_track; i++ )
2378 avi_track_t *tk = p_sys->track[i];
2379 if( tk->i_idxposc >= tk->i_idxnb )
2381 tk->b_activated = VLC_FALSE;
2382 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2392 /****************************************************************************
2393 * AVI_MovieGetLength give max streams length in second
2394 ****************************************************************************/
2395 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2397 demux_sys_t *p_sys = p_demux->p_sys;
2398 mtime_t i_maxlength = 0;
2401 for( i = 0; i < p_sys->i_track; i++ )
2403 avi_track_t *tk = p_sys->track[i];
2406 /* fix length for each stream */
2407 if( tk->i_idxnb < 1 || !tk->p_index )
2412 if( tk->i_samplesize )
2414 i_length = AVI_GetDPTS( tk,
2415 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2416 tk->p_index[tk->i_idxnb-1].i_length );
2420 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2422 i_length /= (mtime_t)1000000; /* in seconds */
2425 "stream[%d] length:"I64Fd" (based on index)",
2428 i_maxlength = __MAX( i_maxlength, i_length );