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_demux.h>
31 #include <vlc_interface.h>
34 #include <vlc_codecs.h>
35 #include <vlc_charset.h>
39 /*****************************************************************************
41 *****************************************************************************/
43 #define INTERLEAVE_TEXT N_("Force interleaved method" )
44 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
46 #define INDEX_TEXT N_("Force index creation")
47 #define INDEX_LONGTEXT N_( \
48 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
49 "or incomplete (not seekable)." )
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
54 static int pi_index[] = {0,1,2};
56 static const char *ppsz_indexes[] = { N_("Ask"), N_("Always fix"),
60 set_shortname( "AVI" );
61 set_description( _("AVI demuxer") );
62 set_capability( "demux2", 212 );
63 set_category( CAT_INPUT );
64 set_subcategory( SUBCAT_INPUT_DEMUX );
66 add_bool( "avi-interleaved", 0, NULL,
67 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
68 add_integer( "avi-index", 0, NULL,
69 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
70 change_integer_list( pi_index, ppsz_indexes, 0 );
72 set_callbacks( Open, Close );
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Control ( demux_t *, int, va_list );
79 static int Seek ( demux_t *, mtime_t, int );
80 static int Demux_Seekable ( demux_t * );
81 static int Demux_UnSeekable( demux_t * );
83 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
87 vlc_fourcc_t i_fourcc;
90 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
92 uint8_t i_peek[8]; /* first 8 bytes */
94 unsigned int i_stream;
105 uint32_t i_lengthtotal;
111 vlc_bool_t b_activated;
113 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
114 vlc_fourcc_t i_codec;
123 avi_entry_t *p_index;
124 unsigned int i_idxnb;
125 unsigned int i_idxmax;
127 unsigned int i_idxposc; /* numero of chunk */
128 unsigned int i_idxposb; /* byte in the current chunk */
130 /* For VBR audio only */
131 unsigned int i_blockno;
132 unsigned int i_blocksize;
134 /* For muxed streams */
135 stream_t *p_out_muxed;
143 vlc_bool_t b_seekable;
150 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
152 /* number of streams and information */
153 unsigned int i_track;
164 static inline off_t __EVEN( off_t i )
166 return (i & 1) ? i + 1 : i;
169 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
170 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
171 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
172 static mtime_t AVI_GetPTS ( avi_track_t * );
175 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
176 static int AVI_StreamChunkSet ( demux_t *,
177 unsigned int i_stream, unsigned int i_ck );
178 static int AVI_StreamBytesSet ( demux_t *,
179 unsigned int i_stream, off_t i_byte );
181 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
182 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
184 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
185 static int AVI_PacketNext ( demux_t * );
186 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
187 static int AVI_PacketSearch ( demux_t * );
189 static void AVI_IndexLoad ( demux_t * );
190 static void AVI_IndexCreate ( demux_t * );
191 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
193 static mtime_t AVI_MovieGetLength( demux_t * );
195 /*****************************************************************************
197 *****************************************************************************/
198 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
199 static int AVI_TrackStopFinishedStreams( demux_t *);
202 - For VBR mp3 stream:
203 count blocks by rounded-up chunksizes instead of chunks
204 we need full emulation of dshow avi demuxer bugs :(
205 fixes silly nandub-style a-v delaying in avi with vbr mp3...
206 (from mplayer 2002/08/02)
210 /*****************************************************************************
211 * Open: check file and initializes AVI structures
212 *****************************************************************************/
213 static int Open( vlc_object_t * p_this )
215 demux_t *p_demux = (demux_t *)p_this;
218 vlc_bool_t b_index = VLC_FALSE;
222 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
223 avi_chunk_list_t *p_hdrl, *p_movi;
224 avi_chunk_avih_t *p_avih;
226 unsigned int i_track;
227 unsigned int i, i_peeker;
231 /* Is it an avi file ? */
232 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
234 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
236 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) &&
237 !strncmp( (char *)&p_peek[8], "AVI ", 4 ) ) break;
240 if( i_peeker == 188 )
245 /* Initialize input structures. */
246 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
247 memset( p_sys, 0, sizeof( demux_sys_t ) );
250 p_sys->i_movi_lastchunk_pos = 0;
251 p_sys->b_odml = VLC_FALSE;
252 p_sys->b_muxed = VLC_FALSE;
257 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
259 p_demux->pf_control = Control;
260 p_demux->pf_demux = Demux_Seekable;
262 /* For unseekable stream, automaticaly use Demux_UnSeekable */
263 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
265 p_demux->pf_demux = Demux_UnSeekable;
270 stream_Read( p_demux->s, NULL, i_peeker );
273 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
275 msg_Err( p_demux, "avi module discarded (invalid file)" );
279 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
281 unsigned int i_count =
282 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
284 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
285 for( i = 1; i < i_count; i++ )
287 avi_chunk_list_t *p_sysx;
289 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
290 if( p_sysx->i_type == AVIFOURCC_AVIX )
292 msg_Warn( p_demux, "detected OpenDML file" );
293 p_sys->b_odml = VLC_TRUE;
299 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
300 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
301 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
303 if( !p_hdrl || !p_movi )
305 msg_Err( p_demux, "avi module discarded (invalid file)" );
309 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
311 msg_Err( p_demux, "cannot find avih chunk" );
314 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
315 if( p_avih->i_streams != i_track )
318 "found %d stream but %d are declared",
319 i_track, p_avih->i_streams );
323 msg_Err( p_demux, "no stream defined!" );
327 /* print information on streams */
328 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
330 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
331 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
332 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
333 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
334 if( ( p_sys->meta = vlc_meta_New() ) )
337 sprintf( buffer, "%s%s%s%s",
338 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
339 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
340 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
341 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
342 vlc_meta_SetSetting( p_sys->meta, buffer );
345 /* now read info on each stream and create ES */
346 for( i = 0 ; i < i_track; i++ )
348 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
349 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
350 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
351 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
352 avi_chunk_strf_auds_t *p_auds = NULL;
353 avi_chunk_strf_vids_t *p_vids = NULL;
356 tk->b_activated = VLC_FALSE;
367 tk->p_out_muxed = NULL;
369 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
370 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
372 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
374 msg_Warn( p_demux, "stream[%d] incomplete", i );
378 tk->i_rate = p_strh->i_rate;
379 tk->i_scale = p_strh->i_scale;
380 tk->i_samplesize = p_strh->i_samplesize;
381 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
382 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
384 switch( p_strh->i_type )
386 case( AVIFOURCC_auds ):
387 tk->i_cat = AUDIO_ES;
388 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
389 p_auds->p_wf->wFormatTag );
390 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
392 if( p_auds->p_wf->wFormatTag == 1 )
394 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
401 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
403 fmt.audio.i_channels = p_auds->p_wf->nChannels;
404 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
405 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
406 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
407 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
408 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
409 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
410 fmt.p_extra = &p_auds->p_wf[1];
411 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
412 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
413 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
416 case( AVIFOURCC_vids ):
417 tk->i_cat = VIDEO_ES;
418 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
419 p_vids->p_bih->biCompression );
420 if( p_vids->p_bih->biCompression == 0x00 )
422 switch( p_vids->p_bih->biBitCount )
425 tk->i_codec = VLC_FOURCC('R','V','3','2');
428 tk->i_codec = VLC_FOURCC('R','V','2','4');
431 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
434 tk->i_codec = VLC_FOURCC('R','V','1','5');
437 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
440 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
442 if( p_vids->p_bih->biBitCount == 24 )
444 /* This is in BGR format */
445 fmt.video.i_bmask = 0x00ff0000;
446 fmt.video.i_gmask = 0x0000ff00;
447 fmt.video.i_rmask = 0x000000ff;
452 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
453 if( tk->i_codec == FOURCC_mp4v &&
454 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
456 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
459 tk->i_samplesize = 0;
460 fmt.video.i_width = p_vids->p_bih->biWidth;
461 fmt.video.i_height = p_vids->p_bih->biHeight;
462 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
463 fmt.video.i_frame_rate = tk->i_rate;
464 fmt.video.i_frame_rate_base = tk->i_scale;
466 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
467 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
468 fmt.p_extra = &p_vids->p_bih[1];
469 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
470 i, (char*)&p_vids->p_bih->biCompression,
471 p_vids->p_bih->biWidth,
472 p_vids->p_bih->biHeight,
473 p_vids->p_bih->biBitCount,
474 (float)tk->i_rate/(float)tk->i_scale );
476 if( p_vids->p_bih->biCompression == 0x00 )
478 /* RGB DIB are coded from bottom to top */
480 (unsigned int)(-(int)p_vids->p_bih->biHeight);
483 /* Extract palette from extradata if bpp <= 8
484 * (assumes that extradata contains only palette but appears
485 * to be true for all palettized codecs we support) */
486 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
487 fmt.video.i_bits_per_pixel > 0 )
491 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
492 fmt.video.p_palette->i_entries = 1;
494 /* Apparently this is necessary. But why ? */
496 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
497 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
499 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
500 GetDWLE((uint32_t*)fmt.p_extra + i);
505 case( AVIFOURCC_txts):
507 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
508 msg_Dbg( p_demux, "stream[%d] subtitles", i );
509 es_format_Init( &fmt, SPU_ES, tk->i_codec );
512 case( AVIFOURCC_iavs):
513 case( AVIFOURCC_ivas):
514 p_sys->b_muxed = VLC_TRUE;
515 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
516 if( p_strh->i_handler == FOURCC_dvsd ||
517 p_strh->i_handler == FOURCC_dvhd ||
518 p_strh->i_handler == FOURCC_dvsl )
520 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
521 if( !tk->p_out_muxed )
522 msg_Err( p_demux, "could not load the DV parser" );
528 case( AVIFOURCC_mids):
529 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
532 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
538 /* The charset of p_strn is undefined */
539 EnsureUTF8( p_strn->p_str );
540 fmt.psz_description = strdup( p_strn->p_str );
542 if( tk->p_out_muxed == NULL )
543 tk->p_es = es_out_Add( p_demux->out, &fmt );
544 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
547 if( p_sys->i_track <= 0 )
549 msg_Err( p_demux, "no valid track" );
553 i_do_index = config_GetInt( p_demux, "avi-index" );
554 if( i_do_index == 1 ) /* Always fix */
557 if( p_sys->b_seekable )
559 AVI_IndexCreate( p_demux );
563 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
564 AVI_IndexLoad( p_demux );
569 AVI_IndexLoad( p_demux );
572 /* *** movie length in sec *** */
573 p_sys->i_length = AVI_MovieGetLength( p_demux );
574 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
575 (mtime_t)p_avih->i_microsecperframe /
578 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
579 "axproximative or will have strange behaviour" );
580 if( i_do_index == 0 && !b_index )
583 i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
584 _( "This AVI file is broken. Seeking will not "
585 "work correctly.\nDo you want to "
586 "try to repair it (this might take a long time) ?" ),
587 _( "Repair" ), _( "Don't repair" ), _( "Cancel") );
588 if( i_create == DIALOG_OK_YES )
591 msg_Dbg( p_demux, "Fixing AVI index" );
594 else if( i_create == DIALOG_CANCELLED )
597 vlc_object_kill( p_demux->p_parent );
603 /* fix some BeOS MediaKit generated file */
604 for( i = 0 ; i < p_sys->i_track; i++ )
606 avi_track_t *tk = p_sys->track[i];
607 avi_chunk_list_t *p_strl;
608 avi_chunk_strh_t *p_strh;
609 avi_chunk_strf_auds_t *p_auds;
611 if( tk->i_cat != AUDIO_ES )
615 if( tk->i_idxnb < 1 ||
617 tk->i_samplesize != 0 )
621 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
622 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
623 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
625 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
626 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
628 int64_t i_track_length =
629 tk->p_index[tk->i_idxnb-1].i_length +
630 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
631 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
632 (mtime_t)p_avih->i_microsecperframe;
636 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
639 tk->i_samplesize = 1;
640 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
641 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
645 if( p_sys->b_seekable )
647 /* we have read all chunk so go back to movi */
648 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
650 /* Skip movi header */
651 stream_Read( p_demux->s, NULL, 12 );
653 p_sys->i_movi_begin = p_movi->i_chunk_pos;
659 vlc_meta_Delete( p_sys->meta );
661 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
666 /*****************************************************************************
667 * Close: frees unused data
668 *****************************************************************************/
669 static void Close ( vlc_object_t * p_this )
671 demux_t * p_demux = (demux_t *)p_this;
673 demux_sys_t *p_sys = p_demux->p_sys ;
675 for( i = 0; i < p_sys->i_track; i++ )
677 if( p_sys->track[i] )
679 if( p_sys->track[i]->p_out_muxed )
680 stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
681 FREENULL( p_sys->track[i]->p_index );
682 free( p_sys->track[i] );
685 FREENULL( p_sys->track );
686 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
687 vlc_meta_Delete( p_sys->meta );
692 /*****************************************************************************
693 * Demux_Seekable: reads and demuxes data packets for stream seekable
694 *****************************************************************************
695 * AVIDemux: reads and demuxes data packets
696 *****************************************************************************
697 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
698 *****************************************************************************/
705 off_t i_posf; /* where we will read :
706 if i_idxposb == 0 : begining of chunk (+8 to acces data)
707 else : point on data directly */
708 } avi_track_toread_t;
710 static int Demux_Seekable( demux_t *p_demux )
712 demux_sys_t *p_sys = p_demux->p_sys;
714 unsigned int i_track_count = 0;
715 unsigned int i_track;
717 /* cannot be more than 100 stream (dcXX or wbXX) */
718 avi_track_toread_t toread[100];
721 /* detect new selected/unselected streams */
722 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
724 avi_track_t *tk = p_sys->track[i_track];
727 if( p_sys->b_muxed && tk->p_out_muxed )
730 tk->b_activated = VLC_TRUE;
734 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
735 if( b && !tk->b_activated )
737 if( p_sys->b_seekable)
739 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
741 tk->b_activated = VLC_TRUE;
743 else if( !b && tk->b_activated )
745 tk->b_activated = VLC_FALSE;
753 if( i_track_count <= 0 )
755 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
757 p_sys->i_time += 25*1000; /* read 25ms */
760 if( p_sys->i_time >= i_length )
764 msg_Warn( p_demux, "no track selected, exiting..." );
768 /* wait for the good time */
769 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
770 p_sys->i_time += 25*1000; /* read 25ms */
773 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
775 avi_track_t *tk = p_sys->track[i_track];
778 toread[i_track].b_ok = tk->b_activated;
779 if( tk->i_idxposc < tk->i_idxnb )
781 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
782 if( tk->i_idxposb > 0 )
784 toread[i_track].i_posf += 8 + tk->i_idxposb;
789 toread[i_track].i_posf = -1;
792 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
794 if( tk->i_samplesize )
796 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
800 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
805 toread[i_track].i_toread *= -1;
809 b_stream = VLC_FALSE;
820 /* search for first chunk to be read */
821 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
823 if( !toread[i].b_ok ||
824 AVI_GetDPTS( p_sys->track[i],
825 toread[i].i_toread ) <= -25 * 1000 )
830 if( toread[i].i_toread > 0 )
832 b_done = VLC_FALSE; /* not yet finished */
834 if( toread[i].i_posf > 0 )
836 if( i_pos == -1 || i_pos > toread[i].i_posf )
839 i_pos = toread[i].i_posf;
851 int i_loop_count = 0;
853 /* no valid index, we will parse directly the stream
854 * in case we fail we will disable all finished stream */
855 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
857 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
858 if( AVI_PacketNext( p_demux ) )
860 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
865 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
872 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
875 "cannot get packet header, track disabled" );
876 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
878 if( avi_pk.i_stream >= p_sys->i_track ||
879 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
881 if( AVI_PacketNext( p_demux ) )
884 "cannot skip packet, track disabled" );
885 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
888 /* Prevents from eating all the CPU with broken files.
889 * This value should be low enough so that it doesn't
890 * affect the reading speed too much. */
891 if( !(++i_loop_count % 1024) )
893 if( p_demux->b_die ) return -1;
896 if( !(i_loop_count % (1024 * 10)) )
898 "don't seem to find any data..." );
904 /* add this chunk to the index */
907 index.i_id = avi_pk.i_fourcc;
909 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
911 index.i_pos = avi_pk.i_pos;
912 index.i_length = avi_pk.i_size;
913 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
915 i_track = avi_pk.i_stream;
916 tk = p_sys->track[i_track];
917 /* do we will read this data ? */
918 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
924 if( AVI_PacketNext( p_demux ) )
927 "cannot skip packet, track disabled" );
928 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
937 stream_Seek( p_demux->s, i_pos );
940 /* Set the track to use */
941 tk = p_sys->track[i_track];
943 /* read thoses data */
944 if( tk->i_samplesize )
946 unsigned int i_toread;
948 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
950 if( tk->i_samplesize > 1 )
952 i_toread = tk->i_samplesize;
956 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
957 i_toread = __MAX( i_toread, 100 );
960 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
966 i_size = tk->p_index[tk->i_idxposc].i_length;
969 if( tk->i_idxposb == 0 )
971 i_size += 8; /* need to read and skip header */
974 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
976 msg_Warn( p_demux, "failed reading data" );
977 tk->b_activated = VLC_FALSE;
978 toread[i_track].b_ok = VLC_FALSE;
981 if( i_size % 2 ) /* read was padded on word boundary */
986 if( tk->i_idxposb == 0 )
988 p_frame->p_buffer += 8;
989 p_frame->i_buffer -= 8;
991 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
992 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
994 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
998 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1002 if( tk->i_samplesize )
1004 if( tk->i_idxposb == 0 )
1008 toread[i_track].i_toread -= i_size;
1009 tk->i_idxposb += i_size;
1010 if( tk->i_idxposb >=
1011 tk->p_index[tk->i_idxposc].i_length )
1019 int i_length = tk->p_index[tk->i_idxposc].i_length;
1022 if( tk->i_cat == AUDIO_ES )
1024 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1026 toread[i_track].i_toread--;
1029 if( tk->i_idxposc < tk->i_idxnb)
1031 toread[i_track].i_posf =
1032 tk->p_index[tk->i_idxposc].i_pos;
1033 if( tk->i_idxposb > 0 )
1035 toread[i_track].i_posf += 8 + tk->i_idxposb;
1041 toread[i_track].i_posf = -1;
1044 b_stream = VLC_TRUE; /* at least one read succeed */
1046 if( tk->i_cat != VIDEO_ES )
1047 p_frame->i_dts = p_frame->i_pts;
1050 p_frame->i_dts = p_frame->i_pts;
1054 //p_pes->i_rate = p_demux->stream.control.i_rate;
1055 if( tk->p_out_muxed )
1056 stream_DemuxSend( tk->p_out_muxed, p_frame );
1058 es_out_Send( p_demux->out, tk->p_es, p_frame );
1063 /*****************************************************************************
1064 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1065 *****************************************************************************
1066 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1067 *****************************************************************************/
1068 static int Demux_UnSeekable( demux_t *p_demux )
1070 demux_sys_t *p_sys = p_demux->p_sys;
1071 avi_track_t *p_stream_master = NULL;
1072 unsigned int i_stream;
1073 unsigned int i_packet;
1075 if( p_sys->b_muxed )
1077 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1078 return VLC_EGENERIC;
1081 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1083 /* *** find master stream for data packet skipping algo *** */
1084 /* *** -> first video, if any, or first audio ES *** */
1085 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1087 avi_track_t *tk = p_sys->track[i_stream];
1090 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1092 if( b && tk->i_cat == VIDEO_ES )
1094 p_stream_master = tk;
1098 p_stream_master = tk;
1102 if( !p_stream_master )
1104 msg_Warn( p_demux, "no more stream selected" );
1108 p_sys->i_time = AVI_GetPTS( p_stream_master );
1110 for( i_packet = 0; i_packet < 10; i_packet++)
1112 #define p_stream p_sys->track[avi_pk.i_stream]
1114 avi_packet_t avi_pk;
1116 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1121 if( avi_pk.i_stream >= p_sys->i_track ||
1122 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1124 /* we haven't found an audio or video packet:
1125 * - we have seek, found first next packet
1126 * - others packets could be found, skip them
1128 switch( avi_pk.i_fourcc )
1130 case AVIFOURCC_JUNK:
1131 case AVIFOURCC_LIST:
1132 case AVIFOURCC_RIFF:
1133 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1134 case AVIFOURCC_idx1:
1137 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1139 return( 0 ); /* eof */
1142 "seems to have lost position, resync" );
1143 if( AVI_PacketSearch( p_demux ) )
1145 msg_Err( p_demux, "resync failed" );
1152 /* check for time */
1153 if( __ABS( AVI_GetPTS( p_stream ) -
1154 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1156 /* load it and send to decoder */
1158 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1162 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1164 if( avi_pk.i_cat != VIDEO_ES )
1165 p_frame->i_dts = p_frame->i_pts;
1168 p_frame->i_dts = p_frame->i_pts;
1172 //p_pes->i_rate = p_demux->stream.control.i_rate;
1173 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1177 if( AVI_PacketNext( p_demux ) )
1183 /* *** update stream time position *** */
1184 if( p_stream->i_samplesize )
1186 p_stream->i_idxposb += avi_pk.i_size;
1190 if( p_stream->i_cat == AUDIO_ES )
1192 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1194 p_stream->i_idxposc++;
1204 /*****************************************************************************
1205 * Seek: goto to i_date or i_percent
1206 *****************************************************************************/
1207 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1210 demux_sys_t *p_sys = p_demux->p_sys;
1211 unsigned int i_stream;
1212 msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%",
1213 i_date / 1000000, i_percent );
1215 if( p_sys->b_seekable )
1217 if( !p_sys->i_length )
1219 avi_track_t *p_stream;
1222 /* use i_percent to create a true i_date */
1223 msg_Warn( p_demux, "seeking without index at %d%%"
1224 " only works for interleaved files", i_percent );
1225 if( i_percent >= 100 )
1227 msg_Warn( p_demux, "cannot seek so far !" );
1228 return VLC_EGENERIC;
1230 i_percent = __MAX( i_percent, 0 );
1232 /* try to find chunk that is at i_percent or the file */
1233 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1234 p_sys->i_movi_begin );
1235 /* search first selected stream */
1236 for( i_stream = 0, p_stream = NULL;
1237 i_stream < p_sys->i_track; i_stream++ )
1239 p_stream = p_sys->track[i_stream];
1240 if( p_stream->b_activated )
1245 if( !p_stream || !p_stream->b_activated )
1247 msg_Warn( p_demux, "cannot find any selected stream" );
1248 return VLC_EGENERIC;
1251 /* be sure that the index exist */
1252 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1254 msg_Warn( p_demux, "cannot seek" );
1255 return VLC_EGENERIC;
1258 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1259 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1261 /* search after i_idxposc */
1262 if( AVI_StreamChunkSet( p_demux,
1263 i_stream, p_stream->i_idxposc + 1 ) )
1265 msg_Warn( p_demux, "cannot seek" );
1266 return VLC_EGENERIC;
1270 i_date = AVI_GetPTS( p_stream );
1271 /* TODO better support for i_samplesize != 0 */
1272 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1275 #define p_stream p_sys->track[i_stream]
1277 /* seek for chunk based streams */
1278 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1280 if( p_stream->b_activated && !p_stream->i_samplesize )
1281 /* if( p_stream->b_activated ) */
1283 AVI_TrackSeek( p_demux, i_stream, i_date );
1284 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1291 i_date = p_sys->i_time;
1293 /* seek for bytes based streams */
1294 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1296 if( p_stream->b_activated && p_stream->i_samplesize )
1298 AVI_TrackSeek( p_demux, i_stream, i_date );
1299 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1302 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1303 /* set true movie time */
1305 if( !p_sys->i_time )
1307 p_sys->i_time = i_date;
1314 msg_Err( p_demux, "shouldn't yet be executed" );
1315 return VLC_EGENERIC;
1319 /*****************************************************************************
1321 *****************************************************************************/
1322 static double ControlGetPosition( demux_t *p_demux )
1324 demux_sys_t *p_sys = p_demux->p_sys;
1326 if( p_sys->i_length > 0 )
1328 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1330 else if( stream_Size( p_demux->s ) > 0 )
1336 /* search the more advanced selected es */
1337 for( i = 0; i < p_sys->i_track; i++ )
1339 avi_track_t *tk = p_sys->track[i];
1340 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1342 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1343 tk->p_index[tk->i_idxposc].i_length + 8;
1350 return (double)i64 / stream_Size( p_demux->s );
1355 static int Control( demux_t *p_demux, int i_query, va_list args )
1357 demux_sys_t *p_sys = p_demux->p_sys;
1365 case DEMUX_GET_POSITION:
1366 pf = (double*)va_arg( args, double * );
1367 *pf = ControlGetPosition( p_demux );
1369 case DEMUX_SET_POSITION:
1370 f = (double)va_arg( args, double );
1371 if( p_sys->b_seekable )
1373 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1374 return Seek( p_demux, i64, (int)(f * 100) );
1378 int64_t i_pos = stream_Size( p_demux->s ) * f;
1379 return stream_Seek( p_demux->s, i_pos );
1382 case DEMUX_GET_TIME:
1383 pi64 = (int64_t*)va_arg( args, int64_t * );
1384 *pi64 = p_sys->i_time;
1387 case DEMUX_SET_TIME:
1391 i64 = (int64_t)va_arg( args, int64_t );
1392 if( p_sys->i_length > 0 )
1394 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1396 else if( p_sys->i_time > 0 )
1398 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1399 (double)i64 / (double)p_sys->i_time );
1401 return Seek( p_demux, i64, i_percent );
1403 case DEMUX_GET_LENGTH:
1404 pi64 = (int64_t*)va_arg( args, int64_t * );
1405 *pi64 = p_sys->i_length * (mtime_t)1000000;
1409 pf = (double*)va_arg( args, double * );
1411 for( i = 0; i < (int)p_sys->i_track; i++ )
1413 avi_track_t *tk = p_sys->track[i];
1414 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1416 *pf = (float)tk->i_rate / (float)tk->i_scale;
1421 case DEMUX_GET_META:
1422 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1423 vlc_meta_Merge( p_meta, p_sys->meta );
1427 return VLC_EGENERIC;
1431 /*****************************************************************************
1432 * Function to convert pts to chunk or byte
1433 *****************************************************************************/
1435 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1440 return (mtime_t)((int64_t)i_pts *
1441 (int64_t)tk->i_rate /
1442 (int64_t)tk->i_scale /
1445 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1447 if( !tk->i_scale || !tk->i_samplesize )
1450 return (mtime_t)((int64_t)i_pts *
1451 (int64_t)tk->i_rate /
1452 (int64_t)tk->i_scale /
1454 (int64_t)tk->i_samplesize );
1457 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1464 i_dpts = (mtime_t)( (int64_t)1000000 *
1466 (int64_t)tk->i_scale /
1467 (int64_t)tk->i_rate );
1469 if( tk->i_samplesize )
1471 return i_dpts / tk->i_samplesize;
1476 static mtime_t AVI_GetPTS( avi_track_t *tk )
1478 if( tk->i_samplesize )
1480 int64_t i_count = 0;
1482 /* we need a valid entry we will emulate one */
1483 if( tk->i_idxposc == tk->i_idxnb )
1487 /* use the last entry */
1488 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1489 + tk->p_index[tk->i_idxnb - 1].i_length;
1494 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1496 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1500 if( tk->i_cat == AUDIO_ES )
1502 return AVI_GetDPTS( tk, tk->i_blockno );
1506 return AVI_GetDPTS( tk, tk->i_idxposc );
1511 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1513 demux_sys_t *p_sys = p_demux->p_sys;
1514 avi_packet_t avi_pk;
1515 int i_loop_count = 0;
1517 /* find first chunk of i_stream that isn't in index */
1519 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1521 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1522 if( AVI_PacketNext( p_demux ) )
1524 return VLC_EGENERIC;
1529 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1534 if( p_demux->b_die ) return VLC_EGENERIC;
1536 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1538 msg_Warn( p_demux, "cannot get packet header" );
1539 return VLC_EGENERIC;
1541 if( avi_pk.i_stream >= p_sys->i_track ||
1542 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1544 if( AVI_PacketNext( p_demux ) )
1546 return VLC_EGENERIC;
1549 /* Prevents from eating all the CPU with broken files.
1550 * This value should be low enough so that it doesn't
1551 * affect the reading speed too much. */
1552 if( !(++i_loop_count % 1024) )
1554 if( p_demux->b_die ) return VLC_EGENERIC;
1557 if( !(i_loop_count % (1024 * 10)) )
1558 msg_Warn( p_demux, "don't seem to find any data..." );
1563 /* add this chunk to the index */
1566 index.i_id = avi_pk.i_fourcc;
1568 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1570 index.i_pos = avi_pk.i_pos;
1571 index.i_length = avi_pk.i_size;
1572 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1574 if( avi_pk.i_stream == i_stream )
1579 if( AVI_PacketNext( p_demux ) )
1581 return VLC_EGENERIC;
1587 /* be sure that i_ck will be a valid index entry */
1588 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1591 demux_sys_t *p_sys = p_demux->p_sys;
1592 avi_track_t *p_stream = p_sys->track[i_stream];
1594 p_stream->i_idxposc = i_ck;
1595 p_stream->i_idxposb = 0;
1597 if( i_ck >= p_stream->i_idxnb )
1599 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1602 p_stream->i_idxposc++;
1603 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1605 return VLC_EGENERIC;
1608 } while( p_stream->i_idxposc < i_ck );
1614 /* XXX FIXME up to now, we assume that all chunk are one after one */
1615 static int AVI_StreamBytesSet( demux_t *p_demux,
1616 unsigned int i_stream,
1619 demux_sys_t *p_sys = p_demux->p_sys;
1620 avi_track_t *p_stream = p_sys->track[i_stream];
1622 if( ( p_stream->i_idxnb > 0 )
1623 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1624 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1626 /* index is valid to find the ck */
1627 /* uses dichototmie to be fast enougth */
1628 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1629 int i_idxmax = p_stream->i_idxnb;
1633 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1635 i_idxmax = i_idxposc ;
1636 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1640 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1641 p_stream->p_index[i_idxposc].i_length <= i_byte)
1643 i_idxmin = i_idxposc ;
1644 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1648 p_stream->i_idxposc = i_idxposc;
1649 p_stream->i_idxposb = i_byte -
1650 p_stream->p_index[i_idxposc].i_lengthtotal;
1659 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1660 p_stream->i_idxposb = 0;
1663 p_stream->i_idxposc++;
1664 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1666 return VLC_EGENERIC;
1669 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1670 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1672 p_stream->i_idxposb = i_byte -
1673 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1678 static int AVI_TrackSeek( demux_t *p_demux,
1682 demux_sys_t *p_sys = p_demux->p_sys;
1683 avi_track_t *tk = p_sys->track[i_stream];
1685 #define p_stream p_sys->track[i_stream]
1688 i_oldpts = AVI_GetPTS( p_stream );
1690 if( !p_stream->i_samplesize )
1692 if( AVI_StreamChunkSet( p_demux,
1694 AVI_PTSToChunk( p_stream, i_date ) ) )
1696 return VLC_EGENERIC;
1699 if( p_stream->i_cat == AUDIO_ES )
1703 for( i = 0; i < tk->i_idxposc; i++ )
1705 if( tk->i_blocksize > 0 )
1707 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1717 "old:"I64Fd" %s new "I64Fd,
1719 i_oldpts > i_date ? ">" : "<",
1722 if( p_stream->i_cat == VIDEO_ES )
1724 /* search key frame */
1725 if( i_date < i_oldpts )
1727 while( p_stream->i_idxposc > 0 &&
1728 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1731 if( AVI_StreamChunkSet( p_demux,
1733 p_stream->i_idxposc - 1 ) )
1735 return VLC_EGENERIC;
1741 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1742 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1745 if( AVI_StreamChunkSet( p_demux,
1747 p_stream->i_idxposc + 1 ) )
1749 return VLC_EGENERIC;
1757 if( AVI_StreamBytesSet( p_demux,
1759 AVI_PTSToByte( p_stream, i_date ) ) )
1761 return VLC_EGENERIC;
1768 /****************************************************************************
1769 * Return VLC_TRUE if it's a key frame
1770 ****************************************************************************/
1771 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1777 * startcode: 0x00000100 32bits
1778 * framenumber ? 5bits
1779 * piture type 0(I),1(P) 2bits
1781 if( GetDWBE( p_byte ) != 0x00000100 )
1783 /* it's not an msmpegv1 stream, strange...*/
1784 return AVIIF_KEYFRAME;
1786 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1789 case FOURCC_DIV3: /* wmv1 also */
1791 * picture type 0(I),1(P) 2bits
1793 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1795 /* we should find first occurrence of 0x000001b6 (32bits)
1796 * startcode: 0x000001b6 32bits
1797 * piture type 0(I),1(P) 2bits
1799 if( GetDWBE( p_byte ) != 0x000001b6 )
1801 /* not true , need to find the first VOP header */
1802 return AVIIF_KEYFRAME;
1804 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1807 /* I can't do it, so say yes */
1808 return AVIIF_KEYFRAME;
1812 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1817 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1821 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1825 return VLC_FOURCC('m','r','l','e');
1874 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1878 /****************************************************************************
1880 ****************************************************************************/
1881 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1882 unsigned int *pi_number, unsigned int *pi_type )
1884 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1887 c1 = ((uint8_t *)&i_id)[0];
1888 c2 = ((uint8_t *)&i_id)[1];
1890 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1892 SET_PTR( pi_number, 100 ); /* > max stream number */
1893 SET_PTR( pi_type, UNKNOWN_ES );
1897 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1898 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1901 SET_PTR( pi_type, AUDIO_ES );
1905 SET_PTR( pi_type, VIDEO_ES );
1908 SET_PTR( pi_type, UNKNOWN_ES );
1915 /****************************************************************************
1917 ****************************************************************************/
1918 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1922 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1924 return VLC_EGENERIC;
1926 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1927 p_pk->i_size = GetDWLE( p_peek + 4 );
1928 p_pk->i_pos = stream_Tell( p_demux->s );
1929 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1931 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1932 p_peek[10], p_peek[11] );
1939 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1941 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1945 static int AVI_PacketNext( demux_t *p_demux )
1947 avi_packet_t avi_ck;
1950 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1952 return VLC_EGENERIC;
1955 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1956 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1960 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1961 avi_ck.i_type == AVIFOURCC_AVIX )
1967 i_skip = __EVEN( avi_ck.i_size ) + 8;
1970 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1972 return VLC_EGENERIC;
1977 static int AVI_PacketRead( demux_t *p_demux,
1979 block_t **pp_frame )
1983 i_size = __EVEN( p_pk->i_size + 8 );
1985 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1987 return VLC_EGENERIC;
1989 (*pp_frame)->p_buffer += 8;
1990 (*pp_frame)->i_buffer -= 8;
1992 if( i_size != p_pk->i_size + 8 )
1994 (*pp_frame)->i_buffer--;
2000 static int AVI_PacketSearch( demux_t *p_demux )
2002 demux_sys_t *p_sys = p_demux->p_sys;
2003 avi_packet_t avi_pk;
2008 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2010 return VLC_EGENERIC;
2012 AVI_PacketGetHeader( p_demux, &avi_pk );
2013 if( avi_pk.i_stream < p_sys->i_track &&
2014 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2018 switch( avi_pk.i_fourcc )
2020 case AVIFOURCC_JUNK:
2021 case AVIFOURCC_LIST:
2022 case AVIFOURCC_RIFF:
2023 case AVIFOURCC_idx1:
2027 /* Prevents from eating all the CPU with broken files.
2028 * This value should be low enough so that it doesn't affect the
2029 * reading speed too much (not that we care much anyway because
2030 * this code is called only on broken files). */
2031 if( !(++i_count % 1024) )
2033 if( p_demux->b_die ) return VLC_EGENERIC;
2036 if( !(i_count % (1024 * 10)) )
2037 msg_Warn( p_demux, "trying to resync..." );
2042 /****************************************************************************
2044 ****************************************************************************/
2045 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2047 avi_entry_t *p_index)
2049 avi_track_t *tk = p_sys->track[i_stream];
2051 /* Update i_movi_lastchunk_pos */
2052 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2054 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2058 if( tk->i_idxnb >= tk->i_idxmax )
2060 tk->i_idxmax += 16384;
2061 tk->p_index = realloc( tk->p_index,
2062 tk->i_idxmax * sizeof( avi_entry_t ) );
2063 if( tk->p_index == NULL )
2068 /* calculate cumulate length */
2069 if( tk->i_idxnb > 0 )
2071 p_index->i_lengthtotal =
2072 tk->p_index[tk->i_idxnb - 1].i_length +
2073 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2077 p_index->i_lengthtotal = 0;
2080 tk->p_index[tk->i_idxnb++] = *p_index;
2083 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2085 demux_sys_t *p_sys = p_demux->p_sys;
2087 avi_chunk_list_t *p_riff;
2088 avi_chunk_list_t *p_movi;
2089 avi_chunk_idx1_t *p_idx1;
2091 unsigned int i_stream;
2092 unsigned int i_index;
2096 vlc_bool_t b_keyset[100];
2098 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2099 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2100 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2104 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2105 return VLC_EGENERIC;
2108 /* *** calculate offset *** */
2109 /* Well, avi is __SHIT__ so test more than one entry
2110 * (needed for some avi files) */
2112 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2114 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2116 i_offset = p_movi->i_chunk_pos + 8;
2121 /* Reset b_keyset */
2122 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2123 b_keyset[i_stream] = VLC_FALSE;
2125 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2129 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2132 if( i_stream < p_sys->i_track &&
2133 i_cat == p_sys->track[i_stream]->i_cat )
2136 index.i_id = p_idx1->entry[i_index].i_fourcc;
2138 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2139 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2140 index.i_length = p_idx1->entry[i_index].i_length;
2141 AVI_IndexAddEntry( p_sys, i_stream, &index );
2143 if( index.i_flags&AVIIF_KEYFRAME )
2144 b_keyset[i_stream] = VLC_TRUE;
2148 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2150 if( !b_keyset[i_stream] )
2152 avi_track_t *tk = p_sys->track[i_stream];
2154 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2155 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2156 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2162 static void __Parse_indx( demux_t *p_demux,
2164 avi_chunk_indx_t *p_indx )
2166 demux_sys_t *p_sys = p_demux->p_sys;
2170 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2171 if( p_indx->i_indexsubtype == 0 )
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.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2177 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2178 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2180 AVI_IndexAddEntry( p_sys, i_stream, &index );
2183 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2185 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2187 index.i_id = p_indx->i_id;
2188 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2189 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2190 index.i_length = p_indx->idx.field[i].i_size;
2192 AVI_IndexAddEntry( p_sys, i_stream, &index );
2197 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2201 static void AVI_IndexLoad_indx( demux_t *p_demux )
2203 demux_sys_t *p_sys = p_demux->p_sys;
2204 unsigned int i_stream;
2207 avi_chunk_list_t *p_riff;
2208 avi_chunk_list_t *p_hdrl;
2210 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2211 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2213 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2215 avi_chunk_list_t *p_strl;
2216 avi_chunk_indx_t *p_indx;
2218 #define p_stream p_sys->track[i_stream]
2219 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2220 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2224 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2229 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2231 __Parse_indx( p_demux, i_stream, p_indx );
2233 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2236 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2238 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2239 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2243 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2248 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2254 static void AVI_IndexLoad( demux_t *p_demux )
2256 demux_sys_t *p_sys = p_demux->p_sys;
2257 unsigned int i_stream;
2259 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2261 p_sys->track[i_stream]->i_idxnb = 0;
2262 p_sys->track[i_stream]->i_idxmax = 0;
2263 p_sys->track[i_stream]->p_index = NULL;
2268 AVI_IndexLoad_indx( p_demux );
2270 else if( AVI_IndexLoad_idx1( p_demux ) )
2272 /* try indx if idx1 failed as some "normal" file have indx too */
2273 AVI_IndexLoad_indx( p_demux );
2276 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2278 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2279 i_stream, p_sys->track[i_stream]->i_idxnb );
2283 static void AVI_IndexCreate( demux_t *p_demux )
2285 demux_sys_t *p_sys = p_demux->p_sys;
2287 avi_chunk_list_t *p_riff;
2288 avi_chunk_list_t *p_movi;
2290 unsigned int i_stream;
2293 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2294 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2298 msg_Err( p_demux, "cannot find p_movi" );
2302 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2304 p_sys->track[i_stream]->i_idxnb = 0;
2305 p_sys->track[i_stream]->i_idxmax = 0;
2306 p_sys->track[i_stream]->p_index = NULL;
2308 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2309 stream_Size( p_demux->s ) );
2311 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2312 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2315 /* Only show dialog if AVI is > 10MB */
2316 p_demux->p_sys->i_dialog_id = -1;
2317 if( stream_Size( p_demux->s ) > 10000000 )
2319 p_demux->p_sys->i_dialog_id = intf_IntfProgress( p_demux,
2320 _( "Fixing AVI Index..." ),
2322 p_demux->p_sys->last_update = mdate();
2329 if( p_demux->b_die )
2334 /* Don't update dialog too often */
2335 if( p_demux->p_sys->i_dialog_id > 0 &&
2336 mdate() - p_demux->p_sys->last_update > 100000 )
2338 int64_t i_pos = stream_Tell( p_demux->s )* 100 /
2339 stream_Size( p_demux->s );
2340 float f_pos = (float)i_pos;
2341 p_demux->p_sys->last_update = mdate();
2342 intf_ProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
2343 _( "Fixing AVI Index..." ), f_pos, -1 );
2346 if( AVI_PacketGetHeader( p_demux, &pk ) )
2350 if( pk.i_stream < p_sys->i_track &&
2351 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2354 index.i_id = pk.i_fourcc;
2356 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2357 index.i_pos = pk.i_pos;
2358 index.i_length = pk.i_size;
2359 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2363 switch( pk.i_fourcc )
2365 case AVIFOURCC_idx1:
2368 avi_chunk_list_t *p_sysx;
2369 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2370 AVIFOURCC_RIFF, 1 );
2372 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2373 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2380 case AVIFOURCC_RIFF:
2381 msg_Dbg( p_demux, "new RIFF chunk found" );
2383 case AVIFOURCC_JUNK:
2386 msg_Warn( p_demux, "need resync, probably broken avi" );
2387 if( AVI_PacketSearch( p_demux ) )
2389 msg_Warn( p_demux, "lost sync, abord index creation" );
2395 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2396 AVI_PacketNext( p_demux ) )
2403 if( p_demux->p_sys->i_dialog_id > 0 )
2405 intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
2408 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2411 "stream[%d] creating %d index entries",
2413 p_sys->track[i_stream]->i_idxnb );
2417 /*****************************************************************************
2419 *****************************************************************************/
2420 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2422 demux_sys_t *p_sys = p_demux->p_sys;
2424 int b_end = VLC_TRUE;
2426 for( i = 0; i < p_sys->i_track; i++ )
2428 avi_track_t *tk = p_sys->track[i];
2429 if( tk->i_idxposc >= tk->i_idxnb )
2431 tk->b_activated = VLC_FALSE;
2432 if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2442 /****************************************************************************
2443 * AVI_MovieGetLength give max streams length in second
2444 ****************************************************************************/
2445 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2447 demux_sys_t *p_sys = p_demux->p_sys;
2448 mtime_t i_maxlength = 0;
2451 for( i = 0; i < p_sys->i_track; i++ )
2453 avi_track_t *tk = p_sys->track[i];
2456 /* fix length for each stream */
2457 if( tk->i_idxnb < 1 || !tk->p_index )
2462 if( tk->i_samplesize )
2464 i_length = AVI_GetDPTS( tk,
2465 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2466 tk->p_index[tk->i_idxnb-1].i_length );
2470 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2472 i_length /= (mtime_t)1000000; /* in seconds */
2475 "stream[%d] length:"I64Fd" (based on index)",
2478 i_maxlength = __MAX( i_maxlength, i_length );