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 *****************************************************************************/
28 #include <vlc_demux.h>
30 #include <vlc_interface.h>
33 #include <vlc_codecs.h>
34 #include <vlc_charset.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 * );
53 static int pi_index[] = {0,1,2};
55 static const char *ppsz_indexes[] = { N_("Ask"), N_("Always fix"),
59 set_shortname( "AVI" );
60 set_description( _("AVI demuxer") );
61 set_capability( "demux2", 212 );
62 set_category( CAT_INPUT );
63 set_subcategory( SUBCAT_INPUT_DEMUX );
65 add_bool( "avi-interleaved", 0, NULL,
66 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
68 add_integer( "avi-index", 0, NULL,
69 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
71 change_integer_list( pi_index, ppsz_indexes, 0 );
73 set_callbacks( Open, Close );
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Control ( demux_t *, int, va_list );
80 static int Seek ( demux_t *, mtime_t, int );
81 static int Demux_Seekable ( demux_t * );
82 static int Demux_UnSeekable( demux_t * );
84 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
88 vlc_fourcc_t i_fourcc;
91 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
93 uint8_t i_peek[8]; /* first 8 bytes */
95 unsigned int i_stream;
106 uint32_t i_lengthtotal;
112 vlc_bool_t b_activated;
114 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
115 vlc_fourcc_t i_codec;
124 avi_entry_t *p_index;
125 unsigned int i_idxnb;
126 unsigned int i_idxmax;
128 unsigned int i_idxposc; /* numero of chunk */
129 unsigned int i_idxposb; /* byte in the current chunk */
131 /* For VBR audio only */
132 unsigned int i_blockno;
133 unsigned int i_blocksize;
135 /* For muxed streams */
136 stream_t *p_out_muxed;
144 vlc_bool_t b_seekable;
151 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
153 /* number of streams and information */
154 unsigned int i_track;
165 static inline off_t __EVEN( off_t i )
167 return (i & 1) ? i + 1 : i;
170 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
171 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
172 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
173 static mtime_t AVI_GetPTS ( avi_track_t * );
176 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
177 static int AVI_StreamChunkSet ( demux_t *,
178 unsigned int i_stream, unsigned int i_ck );
179 static int AVI_StreamBytesSet ( demux_t *,
180 unsigned int i_stream, off_t i_byte );
182 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
183 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
185 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
186 static int AVI_PacketNext ( demux_t * );
187 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
188 static int AVI_PacketSearch ( demux_t * );
190 static void AVI_IndexLoad ( demux_t * );
191 static void AVI_IndexCreate ( demux_t * );
192 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
194 static mtime_t AVI_MovieGetLength( demux_t * );
196 /*****************************************************************************
198 *****************************************************************************/
199 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
200 static int AVI_TrackStopFinishedStreams( demux_t *);
203 - For VBR mp3 stream:
204 count blocks by rounded-up chunksizes instead of chunks
205 we need full emulation of dshow avi demuxer bugs :(
206 fixes silly nandub-style a-v delaying in avi with vbr mp3...
207 (from mplayer 2002/08/02)
211 /*****************************************************************************
212 * Open: check file and initializes AVI structures
213 *****************************************************************************/
214 static int Open( vlc_object_t * p_this )
216 demux_t *p_demux = (demux_t *)p_this;
219 vlc_bool_t b_index = VLC_FALSE;
223 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
224 avi_chunk_list_t *p_hdrl, *p_movi;
225 avi_chunk_avih_t *p_avih;
227 unsigned int i_track;
228 unsigned int i, i_peeker;
230 const uint8_t *p_peek;
232 /* Is it an avi file ? */
233 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
235 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
237 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
239 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
243 if( i_peeker == 188 )
248 /* Initialize input structures. */
249 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
250 memset( p_sys, 0, sizeof( demux_sys_t ) );
253 p_sys->i_movi_lastchunk_pos = 0;
254 p_sys->b_odml = VLC_FALSE;
255 p_sys->b_muxed = VLC_FALSE;
260 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
262 p_demux->pf_control = Control;
263 p_demux->pf_demux = Demux_Seekable;
265 /* For unseekable stream, automaticaly use Demux_UnSeekable */
266 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
268 p_demux->pf_demux = Demux_UnSeekable;
273 stream_Read( p_demux->s, NULL, i_peeker );
276 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
278 msg_Err( p_demux, "avi module discarded (invalid file)" );
282 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
284 unsigned int i_count =
285 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
287 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
288 for( i = 1; i < i_count; i++ )
290 avi_chunk_list_t *p_sysx;
292 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
293 if( p_sysx->i_type == AVIFOURCC_AVIX )
295 msg_Warn( p_demux, "detected OpenDML file" );
296 p_sys->b_odml = VLC_TRUE;
302 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
303 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
304 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
306 if( !p_hdrl || !p_movi )
308 msg_Err( p_demux, "avi module discarded (invalid file)" );
312 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
314 msg_Err( p_demux, "cannot find avih chunk" );
317 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
318 if( p_avih->i_streams != i_track )
321 "found %d stream but %d are declared",
322 i_track, p_avih->i_streams );
326 msg_Err( p_demux, "no stream defined!" );
330 /* print information on streams */
331 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
333 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
334 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
335 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
336 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
337 if( ( p_sys->meta = vlc_meta_New() ) )
340 sprintf( buffer, "%s%s%s%s",
341 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
342 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
343 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
344 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
345 vlc_meta_SetSetting( p_sys->meta, buffer );
348 /* now read info on each stream and create ES */
349 for( i = 0 ; i < i_track; i++ )
351 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
352 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
353 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
354 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
355 avi_chunk_strf_auds_t *p_auds = NULL;
356 avi_chunk_strf_vids_t *p_vids = NULL;
359 tk->b_activated = VLC_FALSE;
370 tk->p_out_muxed = NULL;
372 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
373 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
375 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
377 msg_Warn( p_demux, "stream[%d] incomplete", i );
381 tk->i_rate = p_strh->i_rate;
382 tk->i_scale = p_strh->i_scale;
383 tk->i_samplesize = p_strh->i_samplesize;
384 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
385 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
387 switch( p_strh->i_type )
389 case( AVIFOURCC_auds ):
390 tk->i_cat = AUDIO_ES;
391 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
392 p_auds->p_wf->wFormatTag );
393 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
395 if( p_auds->p_wf->wFormatTag == 1 )
397 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
404 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
406 fmt.audio.i_channels = p_auds->p_wf->nChannels;
407 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
408 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
409 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
410 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
411 fmt.b_packetized = !tk->i_blocksize;
412 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
413 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
414 fmt.p_extra = &p_auds->p_wf[1];
415 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
416 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
417 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
420 case( AVIFOURCC_vids ):
421 tk->i_cat = VIDEO_ES;
422 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
423 p_vids->p_bih->biCompression );
424 if( p_vids->p_bih->biCompression == 0x00 )
426 switch( p_vids->p_bih->biBitCount )
429 tk->i_codec = VLC_FOURCC('R','V','3','2');
432 tk->i_codec = VLC_FOURCC('R','V','2','4');
435 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
438 tk->i_codec = VLC_FOURCC('R','V','1','5');
441 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
444 tk->i_codec = VLC_FOURCC('Y','8','0','0');
447 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
449 if( p_vids->p_bih->biBitCount == 24 )
451 /* This is in BGR format */
452 fmt.video.i_bmask = 0x00ff0000;
453 fmt.video.i_gmask = 0x0000ff00;
454 fmt.video.i_rmask = 0x000000ff;
459 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
460 if( tk->i_codec == FOURCC_mp4v &&
461 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
463 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
466 tk->i_samplesize = 0;
467 fmt.video.i_width = p_vids->p_bih->biWidth;
468 fmt.video.i_height = p_vids->p_bih->biHeight;
469 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
470 fmt.video.i_frame_rate = tk->i_rate;
471 fmt.video.i_frame_rate_base = tk->i_scale;
473 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
474 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
475 fmt.p_extra = &p_vids->p_bih[1];
476 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
477 i, (char*)&p_vids->p_bih->biCompression,
478 p_vids->p_bih->biWidth,
479 p_vids->p_bih->biHeight,
480 p_vids->p_bih->biBitCount,
481 (float)tk->i_rate/(float)tk->i_scale );
483 if( p_vids->p_bih->biCompression == 0x00 )
485 /* RGB DIB are coded from bottom to top */
487 (unsigned int)(-(int)p_vids->p_bih->biHeight);
490 /* Extract palette from extradata if bpp <= 8
491 * (assumes that extradata contains only palette but appears
492 * to be true for all palettized codecs we support) */
493 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
494 fmt.video.i_bits_per_pixel > 0 )
498 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
499 fmt.video.p_palette->i_entries = 1;
501 /* Apparently this is necessary. But why ? */
503 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
504 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
506 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
507 GetDWLE((uint32_t*)fmt.p_extra + i);
512 case( AVIFOURCC_txts):
514 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
515 msg_Dbg( p_demux, "stream[%d] subtitles", i );
516 es_format_Init( &fmt, SPU_ES, tk->i_codec );
519 case( AVIFOURCC_iavs):
520 case( AVIFOURCC_ivas):
521 p_sys->b_muxed = VLC_TRUE;
522 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
523 if( p_strh->i_handler == FOURCC_dvsd ||
524 p_strh->i_handler == FOURCC_dvhd ||
525 p_strh->i_handler == FOURCC_dvsl )
527 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
528 if( !tk->p_out_muxed )
529 msg_Err( p_demux, "could not load the DV parser" );
535 case( AVIFOURCC_mids):
536 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
539 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
545 /* The charset of p_strn is undefined */
546 EnsureUTF8( p_strn->p_str );
547 fmt.psz_description = strdup( p_strn->p_str );
549 if( tk->p_out_muxed == NULL )
550 tk->p_es = es_out_Add( p_demux->out, &fmt );
551 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
554 if( p_sys->i_track <= 0 )
556 msg_Err( p_demux, "no valid track" );
560 i_do_index = config_GetInt( p_demux, "avi-index" );
561 if( i_do_index == 1 ) /* Always fix */
564 if( p_sys->b_seekable )
566 AVI_IndexCreate( p_demux );
570 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
571 AVI_IndexLoad( p_demux );
576 AVI_IndexLoad( p_demux );
579 /* *** movie length in sec *** */
580 p_sys->i_length = AVI_MovieGetLength( p_demux );
581 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
582 (mtime_t)p_avih->i_microsecperframe /
585 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
586 "axproximative or will have strange behaviour" );
587 if( i_do_index == 0 && !b_index )
590 i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
591 _( "This AVI file is broken. Seeking will not "
592 "work correctly.\nDo you want to "
593 "try to repair it?\n\nThis might take a long time." ),
594 _( "Repair" ), _( "Don't repair" ), _( "Cancel") );
595 if( i_create == DIALOG_OK_YES )
598 msg_Dbg( p_demux, "Fixing AVI index" );
601 else if( i_create == DIALOG_CANCELLED )
604 vlc_object_kill( p_demux->p_parent );
610 /* fix some BeOS MediaKit generated file */
611 for( i = 0 ; i < p_sys->i_track; i++ )
613 avi_track_t *tk = p_sys->track[i];
614 avi_chunk_list_t *p_strl;
615 avi_chunk_strh_t *p_strh;
616 avi_chunk_strf_auds_t *p_auds;
618 if( tk->i_cat != AUDIO_ES )
622 if( tk->i_idxnb < 1 ||
624 tk->i_samplesize != 0 )
628 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
629 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
630 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
632 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
633 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
635 int64_t i_track_length =
636 tk->p_index[tk->i_idxnb-1].i_length +
637 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
638 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
639 (mtime_t)p_avih->i_microsecperframe;
643 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
646 tk->i_samplesize = 1;
647 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
648 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
652 if( p_sys->b_seekable )
654 /* we have read all chunk so go back to movi */
655 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
657 /* Skip movi header */
658 stream_Read( p_demux->s, NULL, 12 );
660 p_sys->i_movi_begin = p_movi->i_chunk_pos;
666 vlc_meta_Delete( p_sys->meta );
668 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
673 /*****************************************************************************
674 * Close: frees unused data
675 *****************************************************************************/
676 static void Close ( vlc_object_t * p_this )
678 demux_t * p_demux = (demux_t *)p_this;
680 demux_sys_t *p_sys = p_demux->p_sys ;
682 for( i = 0; i < p_sys->i_track; i++ )
684 if( p_sys->track[i] )
686 if( p_sys->track[i]->p_out_muxed )
687 stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
688 FREENULL( p_sys->track[i]->p_index );
689 free( p_sys->track[i] );
692 FREENULL( p_sys->track );
693 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
694 vlc_meta_Delete( p_sys->meta );
699 /*****************************************************************************
700 * Demux_Seekable: reads and demuxes data packets for stream seekable
701 *****************************************************************************
702 * AVIDemux: reads and demuxes data packets
703 *****************************************************************************
704 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
705 *****************************************************************************/
712 off_t i_posf; /* where we will read :
713 if i_idxposb == 0 : begining of chunk (+8 to acces data)
714 else : point on data directly */
715 } avi_track_toread_t;
717 static int Demux_Seekable( demux_t *p_demux )
719 demux_sys_t *p_sys = p_demux->p_sys;
721 unsigned int i_track_count = 0;
722 unsigned int i_track;
724 /* cannot be more than 100 stream (dcXX or wbXX) */
725 avi_track_toread_t toread[100];
728 /* detect new selected/unselected streams */
729 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
731 avi_track_t *tk = p_sys->track[i_track];
734 if( p_sys->b_muxed && tk->p_out_muxed )
737 tk->b_activated = VLC_TRUE;
741 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
742 if( b && !tk->b_activated )
744 if( p_sys->b_seekable)
746 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
748 tk->b_activated = VLC_TRUE;
750 else if( !b && tk->b_activated )
752 tk->b_activated = VLC_FALSE;
760 if( i_track_count <= 0 )
762 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
764 p_sys->i_time += 25*1000; /* read 25ms */
767 if( p_sys->i_time >= i_length )
771 msg_Warn( p_demux, "no track selected, exiting..." );
775 /* wait for the good time */
776 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
777 p_sys->i_time += 25*1000; /* read 25ms */
780 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
782 avi_track_t *tk = p_sys->track[i_track];
785 toread[i_track].b_ok = tk->b_activated;
786 if( tk->i_idxposc < tk->i_idxnb )
788 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
789 if( tk->i_idxposb > 0 )
791 toread[i_track].i_posf += 8 + tk->i_idxposb;
796 toread[i_track].i_posf = -1;
799 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
801 if( tk->i_samplesize )
803 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
807 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
812 toread[i_track].i_toread *= -1;
816 b_stream = VLC_FALSE;
827 /* search for first chunk to be read */
828 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
830 if( !toread[i].b_ok ||
831 AVI_GetDPTS( p_sys->track[i],
832 toread[i].i_toread ) <= -25 * 1000 )
837 if( toread[i].i_toread > 0 )
839 b_done = VLC_FALSE; /* not yet finished */
841 if( toread[i].i_posf > 0 )
843 if( i_pos == -1 || i_pos > toread[i].i_posf )
846 i_pos = toread[i].i_posf;
858 int i_loop_count = 0;
860 /* no valid index, we will parse directly the stream
861 * in case we fail we will disable all finished stream */
862 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
864 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
865 if( AVI_PacketNext( p_demux ) )
867 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
872 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
879 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
882 "cannot get packet header, track disabled" );
883 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
885 if( avi_pk.i_stream >= p_sys->i_track ||
886 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
888 if( AVI_PacketNext( p_demux ) )
891 "cannot skip packet, track disabled" );
892 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
895 /* Prevents from eating all the CPU with broken files.
896 * This value should be low enough so that it doesn't
897 * affect the reading speed too much. */
898 if( !(++i_loop_count % 1024) )
900 if( p_demux->b_die ) return -1;
903 if( !(i_loop_count % (1024 * 10)) )
905 "don't seem to find any data..." );
911 /* add this chunk to the index */
914 index.i_id = avi_pk.i_fourcc;
916 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
918 index.i_pos = avi_pk.i_pos;
919 index.i_length = avi_pk.i_size;
920 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
922 i_track = avi_pk.i_stream;
923 tk = p_sys->track[i_track];
924 /* do we will read this data ? */
925 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
931 if( AVI_PacketNext( p_demux ) )
934 "cannot skip packet, track disabled" );
935 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
944 stream_Seek( p_demux->s, i_pos );
947 /* Set the track to use */
948 tk = p_sys->track[i_track];
950 /* read thoses data */
951 if( tk->i_samplesize )
953 unsigned int i_toread;
955 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
957 if( tk->i_samplesize > 1 )
959 i_toread = tk->i_samplesize;
963 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
964 i_toread = __MAX( i_toread, 100 );
967 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
973 i_size = tk->p_index[tk->i_idxposc].i_length;
976 if( tk->i_idxposb == 0 )
978 i_size += 8; /* need to read and skip header */
981 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
983 msg_Warn( p_demux, "failed reading data" );
984 tk->b_activated = VLC_FALSE;
985 toread[i_track].b_ok = VLC_FALSE;
988 if( i_size % 2 ) /* read was padded on word boundary */
993 if( tk->i_idxposb == 0 )
995 p_frame->p_buffer += 8;
996 p_frame->i_buffer -= 8;
998 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
999 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1001 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1005 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1009 if( tk->i_samplesize )
1011 if( tk->i_idxposb == 0 )
1015 toread[i_track].i_toread -= i_size;
1016 tk->i_idxposb += i_size;
1017 if( tk->i_idxposb >=
1018 tk->p_index[tk->i_idxposc].i_length )
1026 int i_length = tk->p_index[tk->i_idxposc].i_length;
1029 if( tk->i_cat == AUDIO_ES )
1031 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1033 toread[i_track].i_toread--;
1036 if( tk->i_idxposc < tk->i_idxnb)
1038 toread[i_track].i_posf =
1039 tk->p_index[tk->i_idxposc].i_pos;
1040 if( tk->i_idxposb > 0 )
1042 toread[i_track].i_posf += 8 + tk->i_idxposb;
1048 toread[i_track].i_posf = -1;
1051 b_stream = VLC_TRUE; /* at least one read succeed */
1053 if( tk->i_cat != VIDEO_ES )
1054 p_frame->i_dts = p_frame->i_pts;
1057 p_frame->i_dts = p_frame->i_pts;
1061 //p_pes->i_rate = p_demux->stream.control.i_rate;
1062 if( tk->p_out_muxed )
1063 stream_DemuxSend( tk->p_out_muxed, p_frame );
1065 es_out_Send( p_demux->out, tk->p_es, p_frame );
1070 /*****************************************************************************
1071 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1072 *****************************************************************************
1073 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1074 *****************************************************************************/
1075 static int Demux_UnSeekable( demux_t *p_demux )
1077 demux_sys_t *p_sys = p_demux->p_sys;
1078 avi_track_t *p_stream_master = NULL;
1079 unsigned int i_stream;
1080 unsigned int i_packet;
1082 if( p_sys->b_muxed )
1084 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1085 return VLC_EGENERIC;
1088 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1090 /* *** find master stream for data packet skipping algo *** */
1091 /* *** -> first video, if any, or first audio ES *** */
1092 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1094 avi_track_t *tk = p_sys->track[i_stream];
1097 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1099 if( b && tk->i_cat == VIDEO_ES )
1101 p_stream_master = tk;
1105 p_stream_master = tk;
1109 if( !p_stream_master )
1111 msg_Warn( p_demux, "no more stream selected" );
1115 p_sys->i_time = AVI_GetPTS( p_stream_master );
1117 for( i_packet = 0; i_packet < 10; i_packet++)
1119 #define p_stream p_sys->track[avi_pk.i_stream]
1121 avi_packet_t avi_pk;
1123 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1128 if( avi_pk.i_stream >= p_sys->i_track ||
1129 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1131 /* we haven't found an audio or video packet:
1132 * - we have seek, found first next packet
1133 * - others packets could be found, skip them
1135 switch( avi_pk.i_fourcc )
1137 case AVIFOURCC_JUNK:
1138 case AVIFOURCC_LIST:
1139 case AVIFOURCC_RIFF:
1140 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1141 case AVIFOURCC_idx1:
1144 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1146 return( 0 ); /* eof */
1149 "seems to have lost position, resync" );
1150 if( AVI_PacketSearch( p_demux ) )
1152 msg_Err( p_demux, "resync failed" );
1159 /* check for time */
1160 if( __ABS( AVI_GetPTS( p_stream ) -
1161 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1163 /* load it and send to decoder */
1165 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1169 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1171 if( avi_pk.i_cat != VIDEO_ES )
1172 p_frame->i_dts = p_frame->i_pts;
1175 p_frame->i_dts = p_frame->i_pts;
1179 //p_pes->i_rate = p_demux->stream.control.i_rate;
1180 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1184 if( AVI_PacketNext( p_demux ) )
1190 /* *** update stream time position *** */
1191 if( p_stream->i_samplesize )
1193 p_stream->i_idxposb += avi_pk.i_size;
1197 if( p_stream->i_cat == AUDIO_ES )
1199 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1201 p_stream->i_idxposc++;
1211 /*****************************************************************************
1212 * Seek: goto to i_date or i_percent
1213 *****************************************************************************/
1214 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1217 demux_sys_t *p_sys = p_demux->p_sys;
1218 unsigned int i_stream;
1219 msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%",
1220 i_date / 1000000, i_percent );
1222 if( p_sys->b_seekable )
1224 if( !p_sys->i_length )
1226 avi_track_t *p_stream;
1229 /* use i_percent to create a true i_date */
1230 msg_Warn( p_demux, "seeking without index at %d%%"
1231 " only works for interleaved files", i_percent );
1232 if( i_percent >= 100 )
1234 msg_Warn( p_demux, "cannot seek so far !" );
1235 return VLC_EGENERIC;
1237 i_percent = __MAX( i_percent, 0 );
1239 /* try to find chunk that is at i_percent or the file */
1240 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1241 p_sys->i_movi_begin );
1242 /* search first selected stream */
1243 for( i_stream = 0, p_stream = NULL;
1244 i_stream < p_sys->i_track; i_stream++ )
1246 p_stream = p_sys->track[i_stream];
1247 if( p_stream->b_activated )
1252 if( !p_stream || !p_stream->b_activated )
1254 msg_Warn( p_demux, "cannot find any selected stream" );
1255 return VLC_EGENERIC;
1258 /* be sure that the index exist */
1259 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1261 msg_Warn( p_demux, "cannot seek" );
1262 return VLC_EGENERIC;
1265 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1266 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1268 /* search after i_idxposc */
1269 if( AVI_StreamChunkSet( p_demux,
1270 i_stream, p_stream->i_idxposc + 1 ) )
1272 msg_Warn( p_demux, "cannot seek" );
1273 return VLC_EGENERIC;
1277 i_date = AVI_GetPTS( p_stream );
1278 /* TODO better support for i_samplesize != 0 */
1279 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1283 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1285 avi_track_t *p_stream = p_sys->track[i_stream];
1287 if( !p_stream->b_activated )
1290 AVI_TrackSeek( p_demux, i_stream, i_date );
1292 p_sys->i_time = i_date;
1293 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1298 msg_Err( p_demux, "shouldn't yet be executed" );
1299 return VLC_EGENERIC;
1303 /*****************************************************************************
1305 *****************************************************************************/
1306 static double ControlGetPosition( demux_t *p_demux )
1308 demux_sys_t *p_sys = p_demux->p_sys;
1310 if( p_sys->i_length > 0 )
1312 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1314 else if( stream_Size( p_demux->s ) > 0 )
1320 /* search the more advanced selected es */
1321 for( i = 0; i < p_sys->i_track; i++ )
1323 avi_track_t *tk = p_sys->track[i];
1324 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1326 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1327 tk->p_index[tk->i_idxposc].i_length + 8;
1334 return (double)i64 / stream_Size( p_demux->s );
1339 static int Control( demux_t *p_demux, int i_query, va_list args )
1341 demux_sys_t *p_sys = p_demux->p_sys;
1349 case DEMUX_GET_POSITION:
1350 pf = (double*)va_arg( args, double * );
1351 *pf = ControlGetPosition( p_demux );
1353 case DEMUX_SET_POSITION:
1354 f = (double)va_arg( args, double );
1355 if( p_sys->b_seekable )
1357 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1358 return Seek( p_demux, i64, (int)(f * 100) );
1362 int64_t i_pos = stream_Size( p_demux->s ) * f;
1363 return stream_Seek( p_demux->s, i_pos );
1366 case DEMUX_GET_TIME:
1367 pi64 = (int64_t*)va_arg( args, int64_t * );
1368 *pi64 = p_sys->i_time;
1371 case DEMUX_SET_TIME:
1375 i64 = (int64_t)va_arg( args, int64_t );
1376 if( p_sys->i_length > 0 )
1378 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1380 else if( p_sys->i_time > 0 )
1382 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1383 (double)i64 / (double)p_sys->i_time );
1385 return Seek( p_demux, i64, i_percent );
1387 case DEMUX_GET_LENGTH:
1388 pi64 = (int64_t*)va_arg( args, int64_t * );
1389 *pi64 = p_sys->i_length * (mtime_t)1000000;
1393 pf = (double*)va_arg( args, double * );
1395 for( i = 0; i < (int)p_sys->i_track; i++ )
1397 avi_track_t *tk = p_sys->track[i];
1398 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1400 *pf = (float)tk->i_rate / (float)tk->i_scale;
1405 case DEMUX_GET_META:
1406 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1407 vlc_meta_Merge( p_meta, p_sys->meta );
1411 return VLC_EGENERIC;
1415 /*****************************************************************************
1416 * Function to convert pts to chunk or byte
1417 *****************************************************************************/
1419 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1424 return (mtime_t)((int64_t)i_pts *
1425 (int64_t)tk->i_rate /
1426 (int64_t)tk->i_scale /
1429 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1431 if( !tk->i_scale || !tk->i_samplesize )
1434 return (mtime_t)((int64_t)i_pts *
1435 (int64_t)tk->i_rate /
1436 (int64_t)tk->i_scale /
1438 (int64_t)tk->i_samplesize );
1441 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1448 i_dpts = (mtime_t)( (int64_t)1000000 *
1450 (int64_t)tk->i_scale /
1451 (int64_t)tk->i_rate );
1453 if( tk->i_samplesize )
1455 return i_dpts / tk->i_samplesize;
1460 static mtime_t AVI_GetPTS( avi_track_t *tk )
1462 if( tk->i_samplesize )
1464 int64_t i_count = 0;
1466 /* we need a valid entry we will emulate one */
1467 if( tk->i_idxposc == tk->i_idxnb )
1471 /* use the last entry */
1472 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1473 + tk->p_index[tk->i_idxnb - 1].i_length;
1478 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1480 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1484 if( tk->i_cat == AUDIO_ES )
1486 return AVI_GetDPTS( tk, tk->i_blockno );
1490 return AVI_GetDPTS( tk, tk->i_idxposc );
1495 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1497 demux_sys_t *p_sys = p_demux->p_sys;
1498 avi_packet_t avi_pk;
1499 int i_loop_count = 0;
1501 /* find first chunk of i_stream that isn't in index */
1503 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1505 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1506 if( AVI_PacketNext( p_demux ) )
1508 return VLC_EGENERIC;
1513 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1518 if( p_demux->b_die ) return VLC_EGENERIC;
1520 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1522 msg_Warn( p_demux, "cannot get packet header" );
1523 return VLC_EGENERIC;
1525 if( avi_pk.i_stream >= p_sys->i_track ||
1526 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1528 if( AVI_PacketNext( p_demux ) )
1530 return VLC_EGENERIC;
1533 /* Prevents from eating all the CPU with broken files.
1534 * This value should be low enough so that it doesn't
1535 * affect the reading speed too much. */
1536 if( !(++i_loop_count % 1024) )
1538 if( p_demux->b_die ) return VLC_EGENERIC;
1541 if( !(i_loop_count % (1024 * 10)) )
1542 msg_Warn( p_demux, "don't seem to find any data..." );
1547 /* add this chunk to the index */
1550 index.i_id = avi_pk.i_fourcc;
1552 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1554 index.i_pos = avi_pk.i_pos;
1555 index.i_length = avi_pk.i_size;
1556 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1558 if( avi_pk.i_stream == i_stream )
1563 if( AVI_PacketNext( p_demux ) )
1565 return VLC_EGENERIC;
1571 /* be sure that i_ck will be a valid index entry */
1572 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1575 demux_sys_t *p_sys = p_demux->p_sys;
1576 avi_track_t *p_stream = p_sys->track[i_stream];
1578 p_stream->i_idxposc = i_ck;
1579 p_stream->i_idxposb = 0;
1581 if( i_ck >= p_stream->i_idxnb )
1583 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1586 p_stream->i_idxposc++;
1587 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1589 return VLC_EGENERIC;
1592 } while( p_stream->i_idxposc < i_ck );
1598 /* XXX FIXME up to now, we assume that all chunk are one after one */
1599 static int AVI_StreamBytesSet( demux_t *p_demux,
1600 unsigned int i_stream,
1603 demux_sys_t *p_sys = p_demux->p_sys;
1604 avi_track_t *p_stream = p_sys->track[i_stream];
1606 if( ( p_stream->i_idxnb > 0 )
1607 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1608 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1610 /* index is valid to find the ck */
1611 /* uses dichototmie to be fast enougth */
1612 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1613 int i_idxmax = p_stream->i_idxnb;
1617 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1619 i_idxmax = i_idxposc ;
1620 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1624 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1625 p_stream->p_index[i_idxposc].i_length <= i_byte)
1627 i_idxmin = i_idxposc ;
1628 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1632 p_stream->i_idxposc = i_idxposc;
1633 p_stream->i_idxposb = i_byte -
1634 p_stream->p_index[i_idxposc].i_lengthtotal;
1643 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1644 p_stream->i_idxposb = 0;
1647 p_stream->i_idxposc++;
1648 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1650 return VLC_EGENERIC;
1653 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1654 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1656 p_stream->i_idxposb = i_byte -
1657 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1662 static int AVI_TrackSeek( demux_t *p_demux,
1666 demux_sys_t *p_sys = p_demux->p_sys;
1667 avi_track_t *tk = p_sys->track[i_stream];
1669 #define p_stream p_sys->track[i_stream]
1672 i_oldpts = AVI_GetPTS( p_stream );
1674 if( !p_stream->i_samplesize )
1676 if( AVI_StreamChunkSet( p_demux,
1678 AVI_PTSToChunk( p_stream, i_date ) ) )
1680 return VLC_EGENERIC;
1683 if( p_stream->i_cat == AUDIO_ES )
1687 for( i = 0; i < tk->i_idxposc; i++ )
1689 if( tk->i_blocksize > 0 )
1691 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1701 "old:"I64Fd" %s new "I64Fd,
1703 i_oldpts > i_date ? ">" : "<",
1706 if( p_stream->i_cat == VIDEO_ES )
1708 /* search key frame */
1709 //if( i_date < i_oldpts || 1 )
1711 while( p_stream->i_idxposc > 0 &&
1712 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1715 if( AVI_StreamChunkSet( p_demux,
1717 p_stream->i_idxposc - 1 ) )
1719 return VLC_EGENERIC;
1722 if( p_stream->p_es )
1723 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_stream->p_es, i_date );
1728 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1729 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1732 if( AVI_StreamChunkSet( p_demux,
1734 p_stream->i_idxposc + 1 ) )
1736 return VLC_EGENERIC;
1745 if( AVI_StreamBytesSet( p_demux,
1747 AVI_PTSToByte( p_stream, i_date ) ) )
1749 return VLC_EGENERIC;
1756 /****************************************************************************
1757 * Return VLC_TRUE if it's a key frame
1758 ****************************************************************************/
1759 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1765 * startcode: 0x00000100 32bits
1766 * framenumber ? 5bits
1767 * piture type 0(I),1(P) 2bits
1769 if( GetDWBE( p_byte ) != 0x00000100 )
1771 /* it's not an msmpegv1 stream, strange...*/
1772 return AVIIF_KEYFRAME;
1774 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1777 case FOURCC_DIV3: /* wmv1 also */
1779 * picture type 0(I),1(P) 2bits
1781 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1783 /* we should find first occurrence of 0x000001b6 (32bits)
1784 * startcode: 0x000001b6 32bits
1785 * piture type 0(I),1(P) 2bits
1787 if( GetDWBE( p_byte ) != 0x000001b6 )
1789 /* not true , need to find the first VOP header */
1790 return AVIIF_KEYFRAME;
1792 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1795 /* I can't do it, so say yes */
1796 return AVIIF_KEYFRAME;
1800 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1805 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1809 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1813 return VLC_FOURCC('m','r','l','e');
1862 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1866 /****************************************************************************
1868 ****************************************************************************/
1869 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1870 unsigned int *pi_number, unsigned int *pi_type )
1872 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1875 c1 = ((uint8_t *)&i_id)[0];
1876 c2 = ((uint8_t *)&i_id)[1];
1878 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1880 SET_PTR( pi_number, 100 ); /* > max stream number */
1881 SET_PTR( pi_type, UNKNOWN_ES );
1885 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1886 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1889 SET_PTR( pi_type, AUDIO_ES );
1893 SET_PTR( pi_type, VIDEO_ES );
1896 SET_PTR( pi_type, UNKNOWN_ES );
1903 /****************************************************************************
1905 ****************************************************************************/
1906 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1908 const uint8_t *p_peek;
1910 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1912 return VLC_EGENERIC;
1914 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1915 p_pk->i_size = GetDWLE( p_peek + 4 );
1916 p_pk->i_pos = stream_Tell( p_demux->s );
1917 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1919 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1920 p_peek[10], p_peek[11] );
1927 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1929 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1933 static int AVI_PacketNext( demux_t *p_demux )
1935 avi_packet_t avi_ck;
1938 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1940 return VLC_EGENERIC;
1943 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1944 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1948 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1949 avi_ck.i_type == AVIFOURCC_AVIX )
1955 i_skip = __EVEN( avi_ck.i_size ) + 8;
1958 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1960 return VLC_EGENERIC;
1965 static int AVI_PacketRead( demux_t *p_demux,
1967 block_t **pp_frame )
1971 i_size = __EVEN( p_pk->i_size + 8 );
1973 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1975 return VLC_EGENERIC;
1977 (*pp_frame)->p_buffer += 8;
1978 (*pp_frame)->i_buffer -= 8;
1980 if( i_size != p_pk->i_size + 8 )
1982 (*pp_frame)->i_buffer--;
1988 static int AVI_PacketSearch( demux_t *p_demux )
1990 demux_sys_t *p_sys = p_demux->p_sys;
1991 avi_packet_t avi_pk;
1996 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1998 return VLC_EGENERIC;
2000 AVI_PacketGetHeader( p_demux, &avi_pk );
2001 if( avi_pk.i_stream < p_sys->i_track &&
2002 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2006 switch( avi_pk.i_fourcc )
2008 case AVIFOURCC_JUNK:
2009 case AVIFOURCC_LIST:
2010 case AVIFOURCC_RIFF:
2011 case AVIFOURCC_idx1:
2015 /* Prevents from eating all the CPU with broken files.
2016 * This value should be low enough so that it doesn't affect the
2017 * reading speed too much (not that we care much anyway because
2018 * this code is called only on broken files). */
2019 if( !(++i_count % 1024) )
2021 if( p_demux->b_die ) return VLC_EGENERIC;
2024 if( !(i_count % (1024 * 10)) )
2025 msg_Warn( p_demux, "trying to resync..." );
2030 /****************************************************************************
2032 ****************************************************************************/
2033 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2035 avi_entry_t *p_index)
2037 avi_track_t *tk = p_sys->track[i_stream];
2039 /* Update i_movi_lastchunk_pos */
2040 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2042 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2046 if( tk->i_idxnb >= tk->i_idxmax )
2048 tk->i_idxmax += 16384;
2049 tk->p_index = realloc( tk->p_index,
2050 tk->i_idxmax * sizeof( avi_entry_t ) );
2051 if( tk->p_index == NULL )
2056 /* calculate cumulate length */
2057 if( tk->i_idxnb > 0 )
2059 p_index->i_lengthtotal =
2060 tk->p_index[tk->i_idxnb - 1].i_length +
2061 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2065 p_index->i_lengthtotal = 0;
2068 tk->p_index[tk->i_idxnb++] = *p_index;
2071 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2073 demux_sys_t *p_sys = p_demux->p_sys;
2075 avi_chunk_list_t *p_riff;
2076 avi_chunk_list_t *p_movi;
2077 avi_chunk_idx1_t *p_idx1;
2079 unsigned int i_stream;
2080 unsigned int i_index;
2084 vlc_bool_t b_keyset[100];
2086 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2087 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2088 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2092 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2093 return VLC_EGENERIC;
2096 /* *** calculate offset *** */
2097 /* Well, avi is __SHIT__ so test more than one entry
2098 * (needed for some avi files) */
2100 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2102 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2104 i_offset = p_movi->i_chunk_pos + 8;
2109 /* Reset b_keyset */
2110 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2111 b_keyset[i_stream] = VLC_FALSE;
2113 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2117 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2120 if( i_stream < p_sys->i_track &&
2121 i_cat == p_sys->track[i_stream]->i_cat )
2124 index.i_id = p_idx1->entry[i_index].i_fourcc;
2126 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2127 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2128 index.i_length = p_idx1->entry[i_index].i_length;
2129 AVI_IndexAddEntry( p_sys, i_stream, &index );
2131 if( index.i_flags&AVIIF_KEYFRAME )
2132 b_keyset[i_stream] = VLC_TRUE;
2136 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2138 if( !b_keyset[i_stream] )
2140 avi_track_t *tk = p_sys->track[i_stream];
2142 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2143 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2144 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2150 static void __Parse_indx( demux_t *p_demux,
2152 avi_chunk_indx_t *p_indx )
2154 demux_sys_t *p_sys = p_demux->p_sys;
2158 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2159 if( p_indx->i_indexsubtype == 0 )
2161 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2163 index.i_id = p_indx->i_id;
2164 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2165 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2166 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2168 AVI_IndexAddEntry( p_sys, i_stream, &index );
2171 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2173 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2175 index.i_id = p_indx->i_id;
2176 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2177 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2178 index.i_length = p_indx->idx.field[i].i_size;
2180 AVI_IndexAddEntry( p_sys, i_stream, &index );
2185 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2189 static void AVI_IndexLoad_indx( demux_t *p_demux )
2191 demux_sys_t *p_sys = p_demux->p_sys;
2192 unsigned int i_stream;
2195 avi_chunk_list_t *p_riff;
2196 avi_chunk_list_t *p_hdrl;
2198 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2199 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2201 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2203 avi_chunk_list_t *p_strl;
2204 avi_chunk_indx_t *p_indx;
2206 #define p_stream p_sys->track[i_stream]
2207 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2208 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2212 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2217 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2219 __Parse_indx( p_demux, i_stream, p_indx );
2221 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2224 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2226 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2227 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2231 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2236 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2242 static void AVI_IndexLoad( demux_t *p_demux )
2244 demux_sys_t *p_sys = p_demux->p_sys;
2245 unsigned int i_stream;
2247 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2249 p_sys->track[i_stream]->i_idxnb = 0;
2250 p_sys->track[i_stream]->i_idxmax = 0;
2251 p_sys->track[i_stream]->p_index = NULL;
2256 AVI_IndexLoad_indx( p_demux );
2258 else if( AVI_IndexLoad_idx1( p_demux ) )
2260 /* try indx if idx1 failed as some "normal" file have indx too */
2261 AVI_IndexLoad_indx( p_demux );
2264 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2266 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2267 i_stream, p_sys->track[i_stream]->i_idxnb );
2271 static void AVI_IndexCreate( demux_t *p_demux )
2273 demux_sys_t *p_sys = p_demux->p_sys;
2275 avi_chunk_list_t *p_riff;
2276 avi_chunk_list_t *p_movi;
2278 unsigned int i_stream;
2281 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2282 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2286 msg_Err( p_demux, "cannot find p_movi" );
2290 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2292 p_sys->track[i_stream]->i_idxnb = 0;
2293 p_sys->track[i_stream]->i_idxmax = 0;
2294 p_sys->track[i_stream]->p_index = NULL;
2296 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2297 stream_Size( p_demux->s ) );
2299 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2300 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2303 /* Only show dialog if AVI is > 10MB */
2304 p_demux->p_sys->i_dialog_id = -1;
2305 if( stream_Size( p_demux->s ) > 10000000 )
2307 p_demux->p_sys->i_dialog_id = intf_IntfProgress( p_demux,
2308 _( "Fixing AVI Index..." ),
2310 p_demux->p_sys->last_update = mdate();
2317 if( p_demux->b_die )
2322 /* Don't update dialog too often */
2323 if( p_demux->p_sys->i_dialog_id > 0 &&
2324 mdate() - p_demux->p_sys->last_update > 100000 )
2326 int64_t i_pos = stream_Tell( p_demux->s )* 100 /
2327 stream_Size( p_demux->s );
2328 float f_pos = (float)i_pos;
2329 p_demux->p_sys->last_update = mdate();
2330 intf_ProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
2331 _( "Fixing AVI Index..." ), f_pos, -1 );
2334 if( AVI_PacketGetHeader( p_demux, &pk ) )
2338 if( pk.i_stream < p_sys->i_track &&
2339 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2342 index.i_id = pk.i_fourcc;
2344 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2345 index.i_pos = pk.i_pos;
2346 index.i_length = pk.i_size;
2347 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2351 switch( pk.i_fourcc )
2353 case AVIFOURCC_idx1:
2356 avi_chunk_list_t *p_sysx;
2357 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2358 AVIFOURCC_RIFF, 1 );
2360 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2361 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2368 case AVIFOURCC_RIFF:
2369 msg_Dbg( p_demux, "new RIFF chunk found" );
2371 case AVIFOURCC_JUNK:
2374 msg_Warn( p_demux, "need resync, probably broken avi" );
2375 if( AVI_PacketSearch( p_demux ) )
2377 msg_Warn( p_demux, "lost sync, abord index creation" );
2383 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2384 AVI_PacketNext( p_demux ) )
2391 if( p_demux->p_sys->i_dialog_id > 0 )
2393 intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
2396 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2399 "stream[%d] creating %d index entries",
2401 p_sys->track[i_stream]->i_idxnb );
2405 /*****************************************************************************
2407 *****************************************************************************/
2408 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2410 demux_sys_t *p_sys = p_demux->p_sys;
2412 int b_end = VLC_TRUE;
2414 for( i = 0; i < p_sys->i_track; i++ )
2416 avi_track_t *tk = p_sys->track[i];
2417 if( tk->i_idxposc >= tk->i_idxnb )
2419 tk->b_activated = VLC_FALSE;
2420 if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2430 /****************************************************************************
2431 * AVI_MovieGetLength give max streams length in second
2432 ****************************************************************************/
2433 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2435 demux_sys_t *p_sys = p_demux->p_sys;
2436 mtime_t i_maxlength = 0;
2439 for( i = 0; i < p_sys->i_track; i++ )
2441 avi_track_t *tk = p_sys->track[i];
2444 /* fix length for each stream */
2445 if( tk->i_idxnb < 1 || !tk->p_index )
2450 if( tk->i_samplesize )
2452 i_length = AVI_GetDPTS( tk,
2453 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2454 tk->p_index[tk->i_idxnb-1].i_length );
2458 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2460 i_length /= (mtime_t)1000000; /* in seconds */
2463 "stream[%d] length:"I64Fd" (based on index)",
2466 i_maxlength = __MAX( i_maxlength, i_length );