1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2009 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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
35 #include <vlc_dialog.h>
38 #include <vlc_codecs.h>
39 #include <vlc_charset.h>
43 /*****************************************************************************
45 *****************************************************************************/
47 #define INTERLEAVE_TEXT N_("Force interleaved method" )
48 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
50 #define INDEX_TEXT N_("Force index creation")
51 #define INDEX_LONGTEXT N_( \
52 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
53 "or incomplete (not seekable)." )
55 static int Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
58 static const int pi_index[] = {0,1,2};
60 static const char *const ppsz_indexes[] = { N_("Ask for action"),
65 set_shortname( "AVI" )
66 set_description( N_("AVI demuxer") )
67 set_capability( "demux", 212 )
68 set_category( CAT_INPUT )
69 set_subcategory( SUBCAT_INPUT_DEMUX )
71 add_bool( "avi-interleaved", false, NULL,
72 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
73 add_integer( "avi-index", 0, NULL,
74 INDEX_TEXT, INDEX_LONGTEXT, false )
75 change_integer_list( pi_index, ppsz_indexes, NULL )
77 set_callbacks( Open, Close )
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Control ( demux_t *, int, va_list );
84 static int Seek ( demux_t *, mtime_t, int );
85 static int Demux_Seekable ( demux_t * );
86 static int Demux_UnSeekable( demux_t * );
88 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
92 vlc_fourcc_t i_fourcc;
95 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
97 uint8_t i_peek[8]; /* first 8 bytes */
99 unsigned int i_stream;
110 int64_t i_lengthtotal;
119 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
120 vlc_fourcc_t i_codec;
124 unsigned int i_samplesize;
129 avi_entry_t *p_index;
130 unsigned int i_idxnb;
131 unsigned int i_idxmax;
133 unsigned int i_idxposc; /* numero of chunk */
134 unsigned int i_idxposb; /* byte in the current chunk */
136 /* extra information given to the decoder */
139 /* For VBR audio only */
140 unsigned int i_blockno;
141 unsigned int i_blocksize;
143 /* For muxed streams */
144 stream_t *p_out_muxed;
159 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
161 /* number of streams and information */
162 unsigned int i_track;
169 static inline off_t __EVEN( off_t i )
171 return (i & 1) ? i + 1 : i;
174 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
175 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
176 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
177 static mtime_t AVI_GetPTS ( avi_track_t * );
180 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
181 static int AVI_StreamChunkSet ( demux_t *,
182 unsigned int i_stream, unsigned int i_ck );
183 static int AVI_StreamBytesSet ( demux_t *,
184 unsigned int i_stream, off_t i_byte );
186 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
187 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
189 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
190 static int AVI_PacketNext ( demux_t * );
191 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
192 static int AVI_PacketSearch ( demux_t * );
194 static void AVI_IndexLoad ( demux_t * );
195 static void AVI_IndexCreate ( demux_t * );
196 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
198 static mtime_t AVI_MovieGetLength( demux_t * );
200 /*****************************************************************************
202 *****************************************************************************/
203 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
204 static int AVI_TrackStopFinishedStreams( demux_t *);
207 - For VBR mp3 stream:
208 count blocks by rounded-up chunksizes instead of chunks
209 we need full emulation of dshow avi demuxer bugs :(
210 fixes silly nandub-style a-v delaying in avi with vbr mp3...
211 (from mplayer 2002/08/02)
215 /*****************************************************************************
216 * Open: check file and initializes AVI structures
217 *****************************************************************************/
218 static int Open( vlc_object_t * p_this )
220 demux_t *p_demux = (demux_t *)p_this;
223 bool b_index = false;
226 avi_chunk_list_t *p_riff;
227 avi_chunk_list_t *p_hdrl, *p_movi;
228 avi_chunk_avih_t *p_avih;
230 unsigned int i_track;
231 unsigned int i, i_peeker;
233 const uint8_t *p_peek;
235 /* Is it an avi file ? */
236 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
238 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
240 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
242 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
246 if( i_peeker == 188 )
251 /* Initialize input structures. */
252 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
253 memset( p_sys, 0, sizeof( demux_sys_t ) );
256 p_sys->i_movi_lastchunk_pos = 0;
257 p_sys->b_odml = false;
258 p_sys->b_muxed = false;
263 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
265 p_demux->pf_control = Control;
266 p_demux->pf_demux = Demux_Seekable;
268 /* For unseekable stream, automaticaly use Demux_UnSeekable */
269 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
271 p_demux->pf_demux = Demux_UnSeekable;
276 stream_Read( p_demux->s, NULL, i_peeker );
279 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
281 msg_Err( p_demux, "avi module discarded (invalid file)" );
285 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
287 unsigned int i_count =
288 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
290 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
291 for( i = 1; i < i_count; i++ )
293 avi_chunk_list_t *p_sysx;
295 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
296 if( p_sysx->i_type == AVIFOURCC_AVIX )
298 msg_Warn( p_demux, "detected OpenDML file" );
299 p_sys->b_odml = true;
305 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
306 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
307 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
309 if( !p_hdrl || !p_movi )
311 msg_Err( p_demux, "avi module discarded (invalid file)" );
315 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
317 msg_Err( p_demux, "cannot find avih chunk" );
320 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
321 if( p_avih->i_streams != i_track )
324 "found %d stream but %d are declared",
325 i_track, p_avih->i_streams );
329 msg_Err( p_demux, "no stream defined!" );
333 /* print information on streams */
334 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
336 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
337 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
338 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
339 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
340 if( ( p_sys->meta = vlc_meta_New() ) )
343 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
344 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
345 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
346 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
347 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
348 vlc_meta_SetSetting( p_sys->meta, buffer );
351 /* now read info on each stream and create ES */
352 for( i = 0 ; i < i_track; i++ )
354 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
358 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
359 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
360 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
361 avi_chunk_strf_auds_t *p_auds = NULL;
362 avi_chunk_strf_vids_t *p_vids = NULL;
365 memset( tk, 0, sizeof(*tk) );
367 tk->b_activated = true;
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 );
379 tk->i_rate = p_strh->i_rate;
380 tk->i_scale = p_strh->i_scale;
381 tk->i_samplesize = p_strh->i_samplesize;
382 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
383 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
385 switch( p_strh->i_type )
387 case( AVIFOURCC_auds ):
388 tk->i_cat = AUDIO_ES;
389 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
390 p_auds->p_wf->wFormatTag );
392 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
393 if( tk->i_blocksize == 0 )
395 if( p_auds->p_wf->wFormatTag == 1 )
396 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
400 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
402 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
403 "Using blocksize as a workaround.",
404 i, tk->i_samplesize, tk->i_blocksize );
405 tk->i_samplesize = tk->i_blocksize;
408 if( tk->i_codec == VLC_CODEC_VORBIS )
410 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
413 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
415 fmt.audio.i_channels = p_auds->p_wf->nChannels;
416 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
417 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
418 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
419 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
420 fmt.b_packetized = !tk->i_blocksize;
423 "stream[%d] audio(0x%x) %d channels %dHz %dbits",
424 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
425 p_auds->p_wf->nSamplesPerSec,
426 p_auds->p_wf->wBitsPerSample );
428 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
429 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
430 fmt.p_extra = tk->p_extra = malloc( fmt.i_extra );
431 if( !fmt.p_extra ) goto error;
432 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
434 /* Rewrite the vorbis headers from Xiph-like format
435 * to VLC internal format
438 * - 1st byte == N, is the number of packets - 1
439 * - Following bytes are the size of the N first packets:
440 * while( *p == 0xFF ) { size += 0xFF; p++ } size += *p;
441 * (the size of the last packet is the size of remaining
442 * data in the buffer)
443 * - Finally, all the packets concatenated
446 * - Size of the packet on 16 bits (big endian) FIXME: should be 32 bits to be safe
447 * - The packet itself
448 * - Size of the next packet, and so on ...
451 if( tk->i_codec == VLC_CODEC_VORBIS )
453 uint8_t *p_extra = fmt.p_extra;
454 size_t i_extra = fmt.i_extra;
456 if( i_extra <= 1 ) break;
457 if( *p_extra++ != 2 ) break; /* 3 packets - 1 = 2 */
460 size_t i_identifier_len = 0;
461 while( *p_extra == 0xFF )
463 i_identifier_len += 0xFF;
465 if( --i_extra <= 1 ) break;
467 i_identifier_len += *p_extra++;
468 if( i_identifier_len > --i_extra ) break;
470 size_t i_comment_len = 0;
471 while( *p_extra == 0xFF )
473 i_comment_len += 0xFF;
475 if( --i_extra <= 1 ) break;
477 i_comment_len += *p_extra++;
478 if( i_comment_len > --i_extra ) break;
479 size_t i_cookbook_len = i_extra;
481 size_t i_headers_size = 3 * 2 + i_identifier_len +
482 i_comment_len + i_cookbook_len;
483 uint8_t *p_out = malloc( i_headers_size );
484 if( !p_out ) goto error;
486 fmt.p_extra = tk->p_extra = p_out;
487 fmt.i_extra = i_headers_size;
488 #define copy_packet( len ) \
489 *p_out++ = len >> 8; \
490 *p_out++ = len & 0xFF; \
491 memcpy( p_out, p_extra, len ); \
494 copy_packet( i_identifier_len );
495 copy_packet( i_comment_len );
496 copy_packet( i_cookbook_len );
502 case( AVIFOURCC_vids ):
503 tk->i_cat = VIDEO_ES;
504 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
505 p_vids->p_bih->biCompression );
506 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
508 msg_Dbg( p_demux, "stream[%d] subtitles", i );
509 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
512 else if( p_vids->p_bih->biCompression == 0x00 )
514 switch( p_vids->p_bih->biBitCount )
517 tk->i_codec = VLC_CODEC_RGB32;
520 tk->i_codec = VLC_CODEC_RGB24;
522 case 16: /* Yes it is RV15 */
524 tk->i_codec = VLC_CODEC_RGB15;
526 case 9: /* <- TODO check that */
527 tk->i_codec = VLC_CODEC_I410;
529 case 8: /* <- TODO check that */
530 tk->i_codec = VLC_CODEC_GREY;
533 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
535 switch( tk->i_codec )
537 case VLC_CODEC_RGB24:
538 case VLC_CODEC_RGB32:
539 fmt.video.i_rmask = 0x00ff0000;
540 fmt.video.i_gmask = 0x0000ff00;
541 fmt.video.i_bmask = 0x000000ff;
543 case VLC_CODEC_RGB15:
544 fmt.video.i_rmask = 0x7c00;
545 fmt.video.i_gmask = 0x03e0;
546 fmt.video.i_bmask = 0x001f;
554 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
555 if( tk->i_codec == VLC_CODEC_MP4V &&
556 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
559 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
562 tk->i_samplesize = 0;
563 fmt.video.i_width = p_vids->p_bih->biWidth;
564 fmt.video.i_height = p_vids->p_bih->biHeight;
565 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
566 fmt.video.i_frame_rate = tk->i_rate;
567 fmt.video.i_frame_rate_base = tk->i_scale;
569 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
570 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
571 fmt.p_extra = &p_vids->p_bih[1];
572 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
573 i, (char*)&p_vids->p_bih->biCompression,
574 (uint32_t)p_vids->p_bih->biWidth,
575 (uint32_t)p_vids->p_bih->biHeight,
576 p_vids->p_bih->biBitCount,
577 (float)tk->i_rate/(float)tk->i_scale );
579 if( p_vids->p_bih->biCompression == 0x00 )
581 /* RGB DIB are coded from bottom to top */
583 (unsigned int)(-(int)p_vids->p_bih->biHeight);
586 /* Extract palette from extradata if bpp <= 8
587 * (assumes that extradata contains only palette but appears
588 * to be true for all palettized codecs we support) */
589 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
591 /* The palette is not always included in biSize */
592 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
593 if( fmt.i_extra > 0 )
595 const uint8_t *p_pal = fmt.p_extra;
597 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
598 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
600 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
602 for( int j = 0; j < 4; j++ )
603 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
609 case( AVIFOURCC_txts):
611 tk->i_codec = VLC_CODEC_SUBT;
612 msg_Dbg( p_demux, "stream[%d] subtitles", i );
613 es_format_Init( &fmt, SPU_ES, tk->i_codec );
616 case( AVIFOURCC_iavs):
617 case( AVIFOURCC_ivas):
618 p_sys->b_muxed = true;
619 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
620 if( p_strh->i_handler == FOURCC_dvsd ||
621 p_strh->i_handler == FOURCC_dvhd ||
622 p_strh->i_handler == FOURCC_dvsl ||
623 p_strh->i_handler == FOURCC_dv25 ||
624 p_strh->i_handler == FOURCC_dv50 )
626 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
627 if( !tk->p_out_muxed )
628 msg_Err( p_demux, "could not load the DV parser" );
634 case( AVIFOURCC_mids):
635 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
638 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
643 fmt.psz_description = FromLatin1( p_strn->p_str );
644 if( tk->p_out_muxed == NULL )
645 tk->p_es = es_out_Add( p_demux->out, &fmt );
646 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
649 if( p_sys->i_track <= 0 )
651 msg_Err( p_demux, "no valid track" );
655 i_do_index = config_GetInt( p_demux, "avi-index" );
656 if( i_do_index == 1 ) /* Always fix */
659 if( p_sys->b_seekable )
661 AVI_IndexCreate( p_demux );
665 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
666 AVI_IndexLoad( p_demux );
671 AVI_IndexLoad( p_demux );
674 /* *** movie length in sec *** */
675 p_sys->i_length = AVI_MovieGetLength( p_demux );
676 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
677 (mtime_t)p_avih->i_microsecperframe /
680 if( !vlc_object_alive( p_demux) )
683 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
684 "approximative or will exhibit strange behavior" );
685 if( i_do_index == 0 && !b_index )
687 if( !p_sys->b_seekable ) {
691 switch( dialog_Question( p_demux, _("AVI Index") ,
692 _( "This AVI file is broken. Seeking will not work correctly.\n"
693 "Do you want to try to fix it?\n\n"
694 "This might take a long time." ),
695 _( "Repair" ), _( "Don't repair" ), _( "Cancel") ) )
699 msg_Dbg( p_demux, "Fixing AVI index" );
703 vlc_object_kill( p_demux->p_parent );
709 /* fix some BeOS MediaKit generated file */
710 for( i = 0 ; i < p_sys->i_track; i++ )
712 avi_track_t *tk = p_sys->track[i];
713 avi_chunk_list_t *p_strl;
714 avi_chunk_strh_t *p_strh;
715 avi_chunk_strf_auds_t *p_auds;
717 if( tk->i_cat != AUDIO_ES )
721 if( tk->i_idxnb < 1 ||
723 tk->i_samplesize != 0 )
727 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
728 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
729 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
731 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
732 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
734 int64_t i_track_length =
735 tk->p_index[tk->i_idxnb-1].i_length +
736 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
737 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
738 (mtime_t)p_avih->i_microsecperframe;
742 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
745 tk->i_samplesize = 1;
746 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
747 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
751 if( p_sys->b_seekable )
753 /* we have read all chunk so go back to movi */
754 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
756 /* Skip movi header */
757 stream_Read( p_demux->s, NULL, 12 );
759 p_sys->i_movi_begin = p_movi->i_chunk_pos;
765 vlc_meta_Delete( p_sys->meta );
767 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
769 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
772 /*****************************************************************************
773 * Close: frees unused data
774 *****************************************************************************/
775 static void Close ( vlc_object_t * p_this )
777 demux_t * p_demux = (demux_t *)p_this;
779 demux_sys_t *p_sys = p_demux->p_sys ;
781 for( i = 0; i < p_sys->i_track; i++ )
783 if( p_sys->track[i] )
785 if( p_sys->track[i]->p_out_muxed )
786 stream_Delete( p_sys->track[i]->p_out_muxed );
787 free( p_sys->track[i]->p_index );
788 free( p_sys->track[i]->p_extra );
789 free( p_sys->track[i] );
792 free( p_sys->track );
793 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
794 vlc_meta_Delete( p_sys->meta );
799 /*****************************************************************************
800 * Demux_Seekable: reads and demuxes data packets for stream seekable
801 *****************************************************************************
802 * AVIDemux: reads and demuxes data packets
803 *****************************************************************************
804 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
805 *****************************************************************************/
812 off_t i_posf; /* where we will read :
813 if i_idxposb == 0 : begining of chunk (+8 to acces data)
814 else : point on data directly */
815 } avi_track_toread_t;
817 static int Demux_Seekable( demux_t *p_demux )
819 demux_sys_t *p_sys = p_demux->p_sys;
821 unsigned int i_track_count = 0;
822 unsigned int i_track;
823 /* cannot be more than 100 stream (dcXX or wbXX) */
824 avi_track_toread_t toread[100];
827 /* detect new selected/unselected streams */
828 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
830 avi_track_t *tk = p_sys->track[i_track];
833 if( p_sys->b_muxed && tk->p_out_muxed )
836 tk->b_activated = true;
840 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
841 if( b && !tk->b_activated )
843 if( p_sys->b_seekable)
845 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
847 tk->b_activated = true;
849 else if( !b && tk->b_activated )
851 tk->b_activated = false;
859 if( i_track_count <= 0 )
861 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
863 p_sys->i_time += 25*1000; /* read 25ms */
866 if( p_sys->i_time >= i_length )
870 msg_Warn( p_demux, "no track selected, exiting..." );
874 /* wait for the good time */
875 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
876 p_sys->i_time += 25*1000; /* read 25ms */
879 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
881 avi_track_t *tk = p_sys->track[i_track];
884 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
885 if( tk->i_idxposc < tk->i_idxnb )
887 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
888 if( tk->i_idxposb > 0 )
890 toread[i_track].i_posf += 8 + tk->i_idxposb;
895 toread[i_track].i_posf = -1;
898 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
900 if( tk->i_samplesize )
902 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
906 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
911 toread[i_track].i_toread *= -1;
924 /* search for first chunk to be read */
925 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
927 if( !toread[i].b_ok ||
928 AVI_GetDPTS( p_sys->track[i],
929 toread[i].i_toread ) <= -25 * 1000 )
934 if( toread[i].i_toread > 0 )
936 b_done = false; /* not yet finished */
938 if( toread[i].i_posf > 0 )
940 if( i_pos == -1 || i_pos > toread[i].i_posf )
943 i_pos = toread[i].i_posf;
950 for( i = 0; i < p_sys->i_track; i++ )
955 msg_Warn( p_demux, "all tracks have failed, exiting..." );
961 int i_loop_count = 0;
963 /* no valid index, we will parse directly the stream
964 * in case we fail we will disable all finished stream */
965 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
967 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
968 if( AVI_PacketNext( p_demux ) )
970 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
975 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
982 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
985 "cannot get packet header, track disabled" );
986 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
988 if( avi_pk.i_stream >= p_sys->i_track ||
989 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
991 if( AVI_PacketNext( p_demux ) )
994 "cannot skip packet, track disabled" );
995 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
998 /* Prevents from eating all the CPU with broken files.
999 * This value should be low enough so that it doesn't
1000 * affect the reading speed too much. */
1001 if( !(++i_loop_count % 1024) )
1003 if( !vlc_object_alive (p_demux) ) return -1;
1006 if( !(i_loop_count % (1024 * 10)) )
1008 "don't seem to find any data..." );
1014 /* add this chunk to the index */
1017 index.i_id = avi_pk.i_fourcc;
1019 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1021 index.i_pos = avi_pk.i_pos;
1022 index.i_length = avi_pk.i_size;
1023 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1025 i_track = avi_pk.i_stream;
1026 tk = p_sys->track[i_track];
1027 /* do we will read this data ? */
1028 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1034 if( AVI_PacketNext( p_demux ) )
1037 "cannot skip packet, track disabled" );
1038 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1047 stream_Seek( p_demux->s, i_pos );
1050 /* Set the track to use */
1051 tk = p_sys->track[i_track];
1053 /* read thoses data */
1054 if( tk->i_samplesize )
1056 unsigned int i_toread;
1058 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1060 if( tk->i_samplesize > 1 )
1062 i_toread = tk->i_samplesize;
1066 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1067 i_toread = __MAX( i_toread, 100 );
1070 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
1076 i_size = tk->p_index[tk->i_idxposc].i_length;
1079 if( tk->i_idxposb == 0 )
1081 i_size += 8; /* need to read and skip header */
1084 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1086 msg_Warn( p_demux, "failed reading data" );
1088 toread[i_track].b_ok = false;
1091 if( i_size % 2 ) /* read was padded on word boundary */
1093 p_frame->i_buffer--;
1096 if( tk->i_idxposb == 0 )
1098 p_frame->p_buffer += 8;
1099 p_frame->i_buffer -= 8;
1101 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1102 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1104 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1108 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1112 if( tk->i_samplesize )
1114 if( tk->i_idxposb == 0 )
1118 toread[i_track].i_toread -= i_size;
1119 tk->i_idxposb += i_size;
1120 if( tk->i_idxposb >=
1121 tk->p_index[tk->i_idxposc].i_length )
1129 int i_length = tk->p_index[tk->i_idxposc].i_length;
1132 if( tk->i_cat == AUDIO_ES )
1134 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1136 toread[i_track].i_toread--;
1139 if( tk->i_idxposc < tk->i_idxnb)
1141 toread[i_track].i_posf =
1142 tk->p_index[tk->i_idxposc].i_pos;
1143 if( tk->i_idxposb > 0 )
1145 toread[i_track].i_posf += 8 + tk->i_idxposb;
1151 toread[i_track].i_posf = -1;
1154 if( tk->i_cat != VIDEO_ES )
1155 p_frame->i_dts = p_frame->i_pts;
1158 p_frame->i_dts = p_frame->i_pts;
1162 //p_pes->i_rate = p_demux->stream.control.i_rate;
1163 if( tk->p_out_muxed )
1164 stream_DemuxSend( tk->p_out_muxed, p_frame );
1166 es_out_Send( p_demux->out, tk->p_es, p_frame );
1171 /*****************************************************************************
1172 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1173 *****************************************************************************
1174 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1175 *****************************************************************************/
1176 static int Demux_UnSeekable( demux_t *p_demux )
1178 demux_sys_t *p_sys = p_demux->p_sys;
1179 avi_track_t *p_stream_master = NULL;
1180 unsigned int i_stream;
1181 unsigned int i_packet;
1183 if( p_sys->b_muxed )
1185 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1186 return VLC_EGENERIC;
1189 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1191 /* *** find master stream for data packet skipping algo *** */
1192 /* *** -> first video, if any, or first audio ES *** */
1193 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1195 avi_track_t *tk = p_sys->track[i_stream];
1198 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1200 if( b && tk->i_cat == VIDEO_ES )
1202 p_stream_master = tk;
1206 p_stream_master = tk;
1210 if( !p_stream_master )
1212 msg_Warn( p_demux, "no more stream selected" );
1216 p_sys->i_time = AVI_GetPTS( p_stream_master );
1218 for( i_packet = 0; i_packet < 10; i_packet++)
1220 #define p_stream p_sys->track[avi_pk.i_stream]
1222 avi_packet_t avi_pk;
1224 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1229 if( avi_pk.i_stream >= p_sys->i_track ||
1230 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1232 /* we haven't found an audio or video packet:
1233 * - we have seek, found first next packet
1234 * - others packets could be found, skip them
1236 switch( avi_pk.i_fourcc )
1238 case AVIFOURCC_JUNK:
1239 case AVIFOURCC_LIST:
1240 case AVIFOURCC_RIFF:
1241 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1242 case AVIFOURCC_idx1:
1245 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1247 return( 0 ); /* eof */
1250 "seems to have lost position, resync" );
1251 if( AVI_PacketSearch( p_demux ) )
1253 msg_Err( p_demux, "resync failed" );
1260 /* check for time */
1261 if( __ABS( AVI_GetPTS( p_stream ) -
1262 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1264 /* load it and send to decoder */
1266 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1270 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1272 if( avi_pk.i_cat != VIDEO_ES )
1273 p_frame->i_dts = p_frame->i_pts;
1276 p_frame->i_dts = p_frame->i_pts;
1280 //p_pes->i_rate = p_demux->stream.control.i_rate;
1281 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1285 if( AVI_PacketNext( p_demux ) )
1291 /* *** update stream time position *** */
1292 if( p_stream->i_samplesize )
1294 p_stream->i_idxposb += avi_pk.i_size;
1298 if( p_stream->i_cat == AUDIO_ES )
1300 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1302 p_stream->i_idxposc++;
1312 /*****************************************************************************
1313 * Seek: goto to i_date or i_percent
1314 *****************************************************************************/
1315 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1318 demux_sys_t *p_sys = p_demux->p_sys;
1319 unsigned int i_stream;
1320 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1321 i_date / 1000000, i_percent );
1323 if( p_sys->b_seekable )
1325 if( !p_sys->i_length )
1327 avi_track_t *p_stream;
1330 /* use i_percent to create a true i_date */
1331 msg_Warn( p_demux, "seeking without index at %d%%"
1332 " only works for interleaved files", i_percent );
1333 if( i_percent >= 100 )
1335 msg_Warn( p_demux, "cannot seek so far !" );
1336 return VLC_EGENERIC;
1338 i_percent = __MAX( i_percent, 0 );
1340 /* try to find chunk that is at i_percent or the file */
1341 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1342 p_sys->i_movi_begin );
1343 /* search first selected stream (and prefer non eof ones) */
1344 for( i_stream = 0, p_stream = NULL;
1345 i_stream < p_sys->i_track; i_stream++ )
1347 if( !p_stream || p_stream->b_eof )
1348 p_stream = p_sys->track[i_stream];
1350 if( p_stream->b_activated && !p_stream->b_eof )
1353 if( !p_stream || !p_stream->b_activated )
1355 msg_Warn( p_demux, "cannot find any selected stream" );
1356 return VLC_EGENERIC;
1359 /* be sure that the index exist */
1360 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1362 msg_Warn( p_demux, "cannot seek" );
1363 return VLC_EGENERIC;
1366 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1367 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1369 /* search after i_idxposc */
1370 if( AVI_StreamChunkSet( p_demux,
1371 i_stream, p_stream->i_idxposc + 1 ) )
1373 msg_Warn( p_demux, "cannot seek" );
1374 return VLC_EGENERIC;
1378 i_date = AVI_GetPTS( p_stream );
1379 /* TODO better support for i_samplesize != 0 */
1380 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1384 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1386 avi_track_t *p_stream = p_sys->track[i_stream];
1388 if( !p_stream->b_activated )
1391 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1393 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1394 p_sys->i_time = i_date;
1395 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1400 msg_Err( p_demux, "shouldn't yet be executed" );
1401 return VLC_EGENERIC;
1405 /*****************************************************************************
1407 *****************************************************************************/
1408 static double ControlGetPosition( demux_t *p_demux )
1410 demux_sys_t *p_sys = p_demux->p_sys;
1412 if( p_sys->i_length > 0 )
1414 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1416 else if( stream_Size( p_demux->s ) > 0 )
1422 /* search the more advanced selected es */
1423 for( i = 0; i < p_sys->i_track; i++ )
1425 avi_track_t *tk = p_sys->track[i];
1426 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1428 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1429 tk->p_index[tk->i_idxposc].i_length + 8;
1436 return (double)i64 / stream_Size( p_demux->s );
1441 static int Control( demux_t *p_demux, int i_query, va_list args )
1443 demux_sys_t *p_sys = p_demux->p_sys;
1451 case DEMUX_GET_POSITION:
1452 pf = (double*)va_arg( args, double * );
1453 *pf = ControlGetPosition( p_demux );
1455 case DEMUX_SET_POSITION:
1456 f = (double)va_arg( args, double );
1457 if( p_sys->b_seekable )
1459 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1460 return Seek( p_demux, i64, (int)(f * 100) );
1464 int64_t i_pos = stream_Size( p_demux->s ) * f;
1465 return stream_Seek( p_demux->s, i_pos );
1468 case DEMUX_GET_TIME:
1469 pi64 = (int64_t*)va_arg( args, int64_t * );
1470 *pi64 = p_sys->i_time;
1473 case DEMUX_SET_TIME:
1477 i64 = (int64_t)va_arg( args, int64_t );
1478 if( p_sys->i_length > 0 )
1480 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1482 else if( p_sys->i_time > 0 )
1484 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1485 (double)i64 / (double)p_sys->i_time );
1487 return Seek( p_demux, i64, i_percent );
1489 case DEMUX_GET_LENGTH:
1490 pi64 = (int64_t*)va_arg( args, int64_t * );
1491 *pi64 = p_sys->i_length * (mtime_t)1000000;
1495 pf = (double*)va_arg( args, double * );
1497 for( i = 0; i < (int)p_sys->i_track; i++ )
1499 avi_track_t *tk = p_sys->track[i];
1500 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1502 *pf = (float)tk->i_rate / (float)tk->i_scale;
1507 case DEMUX_GET_META:
1508 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1509 vlc_meta_Merge( p_meta, p_sys->meta );
1513 return VLC_EGENERIC;
1517 /*****************************************************************************
1518 * Function to convert pts to chunk or byte
1519 *****************************************************************************/
1521 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1526 return (mtime_t)((int64_t)i_pts *
1527 (int64_t)tk->i_rate /
1528 (int64_t)tk->i_scale /
1531 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1533 if( !tk->i_scale || !tk->i_samplesize )
1536 return (mtime_t)((int64_t)i_pts *
1537 (int64_t)tk->i_rate /
1538 (int64_t)tk->i_scale /
1540 (int64_t)tk->i_samplesize );
1543 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1550 i_dpts = (mtime_t)( (int64_t)1000000 *
1552 (int64_t)tk->i_scale /
1553 (int64_t)tk->i_rate );
1555 if( tk->i_samplesize )
1557 return i_dpts / tk->i_samplesize;
1562 static mtime_t AVI_GetPTS( avi_track_t *tk )
1564 if( tk->i_samplesize )
1566 int64_t i_count = 0;
1568 /* we need a valid entry we will emulate one */
1569 if( tk->i_idxposc == tk->i_idxnb )
1573 /* use the last entry */
1574 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1575 + tk->p_index[tk->i_idxnb - 1].i_length;
1580 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1582 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1586 if( tk->i_cat == AUDIO_ES )
1588 return AVI_GetDPTS( tk, tk->i_blockno );
1592 return AVI_GetDPTS( tk, tk->i_idxposc );
1597 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1599 demux_sys_t *p_sys = p_demux->p_sys;
1600 avi_packet_t avi_pk;
1601 int i_loop_count = 0;
1603 /* find first chunk of i_stream that isn't in index */
1605 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1607 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1608 if( AVI_PacketNext( p_demux ) )
1610 return VLC_EGENERIC;
1615 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1620 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1622 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1624 msg_Warn( p_demux, "cannot get packet header" );
1625 return VLC_EGENERIC;
1627 if( avi_pk.i_stream >= p_sys->i_track ||
1628 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1630 if( AVI_PacketNext( p_demux ) )
1632 return VLC_EGENERIC;
1635 /* Prevents from eating all the CPU with broken files.
1636 * This value should be low enough so that it doesn't
1637 * affect the reading speed too much. */
1638 if( !(++i_loop_count % 1024) )
1640 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1643 if( !(i_loop_count % (1024 * 10)) )
1644 msg_Warn( p_demux, "don't seem to find any data..." );
1649 /* add this chunk to the index */
1652 index.i_id = avi_pk.i_fourcc;
1654 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1656 index.i_pos = avi_pk.i_pos;
1657 index.i_length = avi_pk.i_size;
1658 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1660 if( avi_pk.i_stream == i_stream )
1665 if( AVI_PacketNext( p_demux ) )
1667 return VLC_EGENERIC;
1673 /* be sure that i_ck will be a valid index entry */
1674 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1677 demux_sys_t *p_sys = p_demux->p_sys;
1678 avi_track_t *p_stream = p_sys->track[i_stream];
1680 p_stream->i_idxposc = i_ck;
1681 p_stream->i_idxposb = 0;
1683 if( i_ck >= p_stream->i_idxnb )
1685 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1688 p_stream->i_idxposc++;
1689 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1691 return VLC_EGENERIC;
1694 } while( p_stream->i_idxposc < i_ck );
1700 /* XXX FIXME up to now, we assume that all chunk are one after one */
1701 static int AVI_StreamBytesSet( demux_t *p_demux,
1702 unsigned int i_stream,
1705 demux_sys_t *p_sys = p_demux->p_sys;
1706 avi_track_t *p_stream = p_sys->track[i_stream];
1708 if( ( p_stream->i_idxnb > 0 )
1709 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1710 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1712 /* index is valid to find the ck */
1713 /* uses dichototmie to be fast enougth */
1714 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1715 int i_idxmax = p_stream->i_idxnb;
1719 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1721 i_idxmax = i_idxposc ;
1722 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1726 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1727 p_stream->p_index[i_idxposc].i_length <= i_byte)
1729 i_idxmin = i_idxposc ;
1730 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1734 p_stream->i_idxposc = i_idxposc;
1735 p_stream->i_idxposb = i_byte -
1736 p_stream->p_index[i_idxposc].i_lengthtotal;
1745 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1746 p_stream->i_idxposb = 0;
1749 p_stream->i_idxposc++;
1750 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1752 return VLC_EGENERIC;
1755 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1756 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1758 p_stream->i_idxposb = i_byte -
1759 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1764 static int AVI_TrackSeek( demux_t *p_demux,
1768 demux_sys_t *p_sys = p_demux->p_sys;
1769 avi_track_t *tk = p_sys->track[i_stream];
1771 #define p_stream p_sys->track[i_stream]
1774 i_oldpts = AVI_GetPTS( p_stream );
1776 if( !p_stream->i_samplesize )
1778 if( AVI_StreamChunkSet( p_demux,
1780 AVI_PTSToChunk( p_stream, i_date ) ) )
1782 return VLC_EGENERIC;
1785 if( p_stream->i_cat == AUDIO_ES )
1789 for( i = 0; i < tk->i_idxposc; i++ )
1791 if( tk->i_blocksize > 0 )
1793 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1803 "old:%"PRId64" %s new %"PRId64,
1805 i_oldpts > i_date ? ">" : "<",
1808 if( p_stream->i_cat == VIDEO_ES )
1810 /* search key frame */
1811 //if( i_date < i_oldpts || 1 )
1813 while( p_stream->i_idxposc > 0 &&
1814 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1817 if( AVI_StreamChunkSet( p_demux,
1819 p_stream->i_idxposc - 1 ) )
1821 return VLC_EGENERIC;
1828 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1829 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1832 if( AVI_StreamChunkSet( p_demux,
1834 p_stream->i_idxposc + 1 ) )
1836 return VLC_EGENERIC;
1845 if( AVI_StreamBytesSet( p_demux,
1847 AVI_PTSToByte( p_stream, i_date ) ) )
1849 return VLC_EGENERIC;
1856 /****************************************************************************
1857 * Return true if it's a key frame
1858 ****************************************************************************/
1859 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1863 case VLC_CODEC_DIV1:
1865 * startcode: 0x00000100 32bits
1866 * framenumber ? 5bits
1867 * piture type 0(I),1(P) 2bits
1869 if( GetDWBE( p_byte ) != 0x00000100 )
1871 /* it's not an msmpegv1 stream, strange...*/
1872 return AVIIF_KEYFRAME;
1874 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1876 case VLC_CODEC_DIV2:
1877 case VLC_CODEC_DIV3:
1878 case VLC_CODEC_WMV1:
1880 * picture type 0(I),1(P) 2bits
1882 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1883 case VLC_CODEC_MP4V:
1884 /* we should find first occurrence of 0x000001b6 (32bits)
1885 * startcode: 0x000001b6 32bits
1886 * piture type 0(I),1(P) 2bits
1888 if( GetDWBE( p_byte ) != 0x000001b6 )
1890 /* not true , need to find the first VOP header */
1891 return AVIIF_KEYFRAME;
1893 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1896 /* I can't do it, so say yes */
1897 return AVIIF_KEYFRAME;
1901 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1906 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1909 return vlc_fourcc_GetCodec( i_cat, i_codec );
1911 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1915 /****************************************************************************
1917 ****************************************************************************/
1918 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1919 unsigned int *pi_number, unsigned int *pi_type )
1921 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1924 c1 = ((uint8_t *)&i_id)[0];
1925 c2 = ((uint8_t *)&i_id)[1];
1927 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1929 SET_PTR( pi_number, 100 ); /* > max stream number */
1930 SET_PTR( pi_type, UNKNOWN_ES );
1934 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1935 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1938 SET_PTR( pi_type, AUDIO_ES );
1943 SET_PTR( pi_type, VIDEO_ES );
1946 SET_PTR( pi_type, UNKNOWN_ES );
1953 /****************************************************************************
1955 ****************************************************************************/
1956 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1958 const uint8_t *p_peek;
1960 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1962 return VLC_EGENERIC;
1964 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1965 p_pk->i_size = GetDWLE( p_peek + 4 );
1966 p_pk->i_pos = stream_Tell( p_demux->s );
1967 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1969 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1970 p_peek[10], p_peek[11] );
1977 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1979 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1983 static int AVI_PacketNext( demux_t *p_demux )
1985 avi_packet_t avi_ck;
1988 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1990 return VLC_EGENERIC;
1993 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1994 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1998 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1999 avi_ck.i_type == AVIFOURCC_AVIX )
2005 i_skip = __EVEN( avi_ck.i_size ) + 8;
2008 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2010 return VLC_EGENERIC;
2015 static int AVI_PacketRead( demux_t *p_demux,
2017 block_t **pp_frame )
2021 i_size = __EVEN( p_pk->i_size + 8 );
2023 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2025 return VLC_EGENERIC;
2027 (*pp_frame)->p_buffer += 8;
2028 (*pp_frame)->i_buffer -= 8;
2030 if( i_size != p_pk->i_size + 8 )
2032 (*pp_frame)->i_buffer--;
2038 static int AVI_PacketSearch( demux_t *p_demux )
2040 demux_sys_t *p_sys = p_demux->p_sys;
2041 avi_packet_t avi_pk;
2046 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2048 return VLC_EGENERIC;
2050 AVI_PacketGetHeader( p_demux, &avi_pk );
2051 if( avi_pk.i_stream < p_sys->i_track &&
2052 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2056 switch( avi_pk.i_fourcc )
2058 case AVIFOURCC_JUNK:
2059 case AVIFOURCC_LIST:
2060 case AVIFOURCC_RIFF:
2061 case AVIFOURCC_idx1:
2065 /* Prevents from eating all the CPU with broken files.
2066 * This value should be low enough so that it doesn't affect the
2067 * reading speed too much (not that we care much anyway because
2068 * this code is called only on broken files). */
2069 if( !(++i_count % 1024) )
2071 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2074 if( !(i_count % (1024 * 10)) )
2075 msg_Warn( p_demux, "trying to resync..." );
2080 /****************************************************************************
2082 ****************************************************************************/
2083 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2085 avi_entry_t *p_index)
2087 avi_track_t *tk = p_sys->track[i_stream];
2089 /* Update i_movi_lastchunk_pos */
2090 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2092 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2096 if( tk->i_idxnb >= tk->i_idxmax )
2098 tk->i_idxmax += 16384;
2099 tk->p_index = realloc( tk->p_index,
2100 tk->i_idxmax * sizeof( avi_entry_t ) );
2101 if( tk->p_index == NULL )
2106 /* calculate cumulate length */
2107 if( tk->i_idxnb > 0 )
2109 p_index->i_lengthtotal =
2110 tk->p_index[tk->i_idxnb - 1].i_length +
2111 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2115 p_index->i_lengthtotal = 0;
2118 tk->p_index[tk->i_idxnb++] = *p_index;
2121 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2123 demux_sys_t *p_sys = p_demux->p_sys;
2125 avi_chunk_list_t *p_riff;
2126 avi_chunk_list_t *p_movi;
2127 avi_chunk_idx1_t *p_idx1;
2129 unsigned int i_stream;
2130 unsigned int i_index;
2136 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2137 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2138 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2142 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2143 return VLC_EGENERIC;
2146 /* *** calculate offset *** */
2147 /* Well, avi is __SHIT__ so test more than one entry
2148 * (needed for some avi files) */
2150 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2152 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2154 i_offset = p_movi->i_chunk_pos + 8;
2159 /* Reset b_keyset */
2160 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2161 b_keyset[i_stream] = false;
2163 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2167 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2170 if( i_stream < p_sys->i_track &&
2171 i_cat == p_sys->track[i_stream]->i_cat )
2174 index.i_id = p_idx1->entry[i_index].i_fourcc;
2176 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2177 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2178 index.i_length = p_idx1->entry[i_index].i_length;
2179 AVI_IndexAddEntry( p_sys, i_stream, &index );
2181 if( index.i_flags&AVIIF_KEYFRAME )
2182 b_keyset[i_stream] = true;
2186 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2188 if( !b_keyset[i_stream] )
2190 avi_track_t *tk = p_sys->track[i_stream];
2192 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2193 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2194 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2200 static void __Parse_indx( demux_t *p_demux,
2202 avi_chunk_indx_t *p_indx )
2204 demux_sys_t *p_sys = p_demux->p_sys;
2208 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2209 if( p_indx->i_indexsubtype == 0 )
2211 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2213 index.i_id = p_indx->i_id;
2214 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2215 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2216 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2218 AVI_IndexAddEntry( p_sys, i_stream, &index );
2221 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2223 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2225 index.i_id = p_indx->i_id;
2226 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2227 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2228 index.i_length = p_indx->idx.field[i].i_size;
2230 AVI_IndexAddEntry( p_sys, i_stream, &index );
2235 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2239 static void AVI_IndexLoad_indx( demux_t *p_demux )
2241 demux_sys_t *p_sys = p_demux->p_sys;
2242 unsigned int i_stream;
2245 avi_chunk_list_t *p_riff;
2246 avi_chunk_list_t *p_hdrl;
2248 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2249 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2251 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2253 avi_chunk_list_t *p_strl;
2254 avi_chunk_indx_t *p_indx;
2256 #define p_stream p_sys->track[i_stream]
2257 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2258 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2262 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2267 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2269 __Parse_indx( p_demux, i_stream, p_indx );
2271 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2274 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2276 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2277 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2281 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2286 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2292 static void AVI_IndexLoad( demux_t *p_demux )
2294 demux_sys_t *p_sys = p_demux->p_sys;
2295 unsigned int i_stream;
2297 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2299 p_sys->track[i_stream]->i_idxnb = 0;
2300 p_sys->track[i_stream]->i_idxmax = 0;
2301 p_sys->track[i_stream]->p_index = NULL;
2306 AVI_IndexLoad_indx( p_demux );
2308 else if( AVI_IndexLoad_idx1( p_demux ) )
2310 /* try indx if idx1 failed as some "normal" file have indx too */
2311 AVI_IndexLoad_indx( p_demux );
2314 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2316 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2317 i_stream, p_sys->track[i_stream]->i_idxnb );
2321 static void AVI_IndexCreate( demux_t *p_demux )
2323 demux_sys_t *p_sys = p_demux->p_sys;
2325 avi_chunk_list_t *p_riff;
2326 avi_chunk_list_t *p_movi;
2328 unsigned int i_stream;
2331 mtime_t i_dialog_update;
2332 dialog_progress_bar_t *p_dialog = NULL;
2334 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2335 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2339 msg_Err( p_demux, "cannot find p_movi" );
2343 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2345 p_sys->track[i_stream]->i_idxnb = 0;
2346 p_sys->track[i_stream]->i_idxmax = 0;
2347 p_sys->track[i_stream]->p_index = NULL;
2349 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2350 stream_Size( p_demux->s ) );
2352 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2353 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2356 /* Only show dialog if AVI is > 10MB */
2357 i_dialog_update = mdate();
2358 if( stream_Size( p_demux->s ) > 10000000 )
2359 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2360 NULL, _("Cancel") );
2366 if( !vlc_object_alive (p_demux) )
2369 /* Don't update/check dialog too often */
2370 if( p_dialog && mdate() - i_dialog_update > 100000 )
2372 if( dialog_ProgressCancelled( p_dialog ) )
2375 double f_current = stream_Tell( p_demux->s );
2376 double f_size = stream_Size( p_demux->s );
2377 double f_pos = f_current / f_size;
2378 dialog_ProgressSet( p_dialog, NULL, f_pos );
2380 i_dialog_update = mdate();
2383 if( AVI_PacketGetHeader( p_demux, &pk ) )
2386 if( pk.i_stream < p_sys->i_track &&
2387 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2390 index.i_id = pk.i_fourcc;
2392 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2393 index.i_pos = pk.i_pos;
2394 index.i_length = pk.i_size;
2395 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2399 switch( pk.i_fourcc )
2401 case AVIFOURCC_idx1:
2404 avi_chunk_list_t *p_sysx;
2405 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2406 AVIFOURCC_RIFF, 1 );
2408 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2409 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2415 case AVIFOURCC_RIFF:
2416 msg_Dbg( p_demux, "new RIFF chunk found" );
2420 case AVIFOURCC_JUNK:
2424 msg_Warn( p_demux, "need resync, probably broken avi" );
2425 if( AVI_PacketSearch( p_demux ) )
2427 msg_Warn( p_demux, "lost sync, abord index creation" );
2433 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2434 AVI_PacketNext( p_demux ) )
2441 if( p_dialog != NULL )
2442 dialog_ProgressDestroy( p_dialog );
2444 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2446 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2447 i_stream, p_sys->track[i_stream]->i_idxnb );
2451 /*****************************************************************************
2453 *****************************************************************************/
2454 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2456 demux_sys_t *p_sys = p_demux->p_sys;
2460 for( i = 0; i < p_sys->i_track; i++ )
2462 avi_track_t *tk = p_sys->track[i];
2463 if( tk->i_idxposc >= tk->i_idxnb )
2475 /****************************************************************************
2476 * AVI_MovieGetLength give max streams length in second
2477 ****************************************************************************/
2478 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2480 demux_sys_t *p_sys = p_demux->p_sys;
2481 mtime_t i_maxlength = 0;
2484 for( i = 0; i < p_sys->i_track; i++ )
2486 avi_track_t *tk = p_sys->track[i];
2489 /* fix length for each stream */
2490 if( tk->i_idxnb < 1 || !tk->p_index )
2495 if( tk->i_samplesize )
2497 i_length = AVI_GetDPTS( tk,
2498 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2499 tk->p_index[tk->i_idxnb-1].i_length );
2503 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2505 i_length /= (mtime_t)1000000; /* in seconds */
2508 "stream[%d] length:%"PRId64" (based on index)",
2511 i_maxlength = __MAX( i_maxlength, i_length );