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"), N_("Always fix"),
64 set_shortname( "AVI" )
65 set_description( N_("AVI demuxer") )
66 set_capability( "demux", 212 )
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_DEMUX )
70 add_bool( "avi-interleaved", false, NULL,
71 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
72 add_integer( "avi-index", 0, NULL,
73 INDEX_TEXT, INDEX_LONGTEXT, false )
74 change_integer_list( pi_index, ppsz_indexes, NULL )
76 set_callbacks( Open, Close )
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Control ( demux_t *, int, va_list );
83 static int Seek ( demux_t *, mtime_t, int );
84 static int Demux_Seekable ( demux_t * );
85 static int Demux_UnSeekable( demux_t * );
87 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
91 vlc_fourcc_t i_fourcc;
94 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
96 uint8_t i_peek[8]; /* first 8 bytes */
98 unsigned int i_stream;
109 int64_t i_lengthtotal;
118 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
119 vlc_fourcc_t i_codec;
123 unsigned int i_samplesize;
128 avi_entry_t *p_index;
129 unsigned int i_idxnb;
130 unsigned int i_idxmax;
132 unsigned int i_idxposc; /* numero of chunk */
133 unsigned int i_idxposb; /* byte in the current chunk */
135 /* extra information given to the decoder */
138 /* For VBR audio only */
139 unsigned int i_blockno;
140 unsigned int i_blocksize;
142 /* For muxed streams */
143 stream_t *p_out_muxed;
158 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
160 /* number of streams and information */
161 unsigned int i_track;
168 static inline off_t __EVEN( off_t i )
170 return (i & 1) ? i + 1 : i;
173 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
174 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
175 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
176 static mtime_t AVI_GetPTS ( avi_track_t * );
179 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
180 static int AVI_StreamChunkSet ( demux_t *,
181 unsigned int i_stream, unsigned int i_ck );
182 static int AVI_StreamBytesSet ( demux_t *,
183 unsigned int i_stream, off_t i_byte );
185 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
186 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
188 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
189 static int AVI_PacketNext ( demux_t * );
190 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
191 static int AVI_PacketSearch ( demux_t * );
193 static void AVI_IndexLoad ( demux_t * );
194 static void AVI_IndexCreate ( demux_t * );
195 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
197 static mtime_t AVI_MovieGetLength( demux_t * );
199 /*****************************************************************************
201 *****************************************************************************/
202 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
203 static int AVI_TrackStopFinishedStreams( demux_t *);
206 - For VBR mp3 stream:
207 count blocks by rounded-up chunksizes instead of chunks
208 we need full emulation of dshow avi demuxer bugs :(
209 fixes silly nandub-style a-v delaying in avi with vbr mp3...
210 (from mplayer 2002/08/02)
214 /*****************************************************************************
215 * Open: check file and initializes AVI structures
216 *****************************************************************************/
217 static int Open( vlc_object_t * p_this )
219 demux_t *p_demux = (demux_t *)p_this;
222 bool b_index = false;
225 avi_chunk_list_t *p_riff;
226 avi_chunk_list_t *p_hdrl, *p_movi;
227 avi_chunk_avih_t *p_avih;
229 unsigned int i_track;
230 unsigned int i, i_peeker;
232 const uint8_t *p_peek;
234 /* Is it an avi file ? */
235 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
237 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
239 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
241 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
245 if( i_peeker == 188 )
250 /* Initialize input structures. */
251 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
252 memset( p_sys, 0, sizeof( demux_sys_t ) );
255 p_sys->i_movi_lastchunk_pos = 0;
256 p_sys->b_odml = false;
257 p_sys->b_muxed = false;
262 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
264 p_demux->pf_control = Control;
265 p_demux->pf_demux = Demux_Seekable;
267 /* For unseekable stream, automaticaly use Demux_UnSeekable */
268 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
270 p_demux->pf_demux = Demux_UnSeekable;
275 stream_Read( p_demux->s, NULL, i_peeker );
278 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
280 msg_Err( p_demux, "avi module discarded (invalid file)" );
284 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
286 unsigned int i_count =
287 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
289 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
290 for( i = 1; i < i_count; i++ )
292 avi_chunk_list_t *p_sysx;
294 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
295 if( p_sysx->i_type == AVIFOURCC_AVIX )
297 msg_Warn( p_demux, "detected OpenDML file" );
298 p_sys->b_odml = true;
304 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
305 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
306 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
308 if( !p_hdrl || !p_movi )
310 msg_Err( p_demux, "avi module discarded (invalid file)" );
314 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
316 msg_Err( p_demux, "cannot find avih chunk" );
319 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
320 if( p_avih->i_streams != i_track )
323 "found %d stream but %d are declared",
324 i_track, p_avih->i_streams );
328 msg_Err( p_demux, "no stream defined!" );
332 /* print information on streams */
333 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
335 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
336 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
337 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
338 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
339 if( ( p_sys->meta = vlc_meta_New() ) )
342 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
343 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
344 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
345 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
346 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
347 vlc_meta_SetSetting( p_sys->meta, buffer );
350 /* now read info on each stream and create ES */
351 for( i = 0 ; i < i_track; i++ )
353 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
357 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
358 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
359 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
360 avi_chunk_strf_auds_t *p_auds = NULL;
361 avi_chunk_strf_vids_t *p_vids = NULL;
364 memset( tk, 0, sizeof(*tk) );
366 tk->b_activated = true;
368 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
369 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
371 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
373 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 );
391 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
392 if( tk->i_blocksize == 0 )
394 if( p_auds->p_wf->wFormatTag == 1 )
395 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
399 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
401 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
402 "Using blocksize as a workaround.",
403 i, tk->i_samplesize, tk->i_blocksize );
404 tk->i_samplesize = tk->i_blocksize;
407 if( tk->i_codec == VLC_CODEC_VORBIS )
409 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
412 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
414 fmt.audio.i_channels = p_auds->p_wf->nChannels;
415 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
416 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
417 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
418 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
419 fmt.b_packetized = !tk->i_blocksize;
422 "stream[%d] audio(0x%x) %d channels %dHz %dbits",
423 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
424 p_auds->p_wf->nSamplesPerSec,
425 p_auds->p_wf->wBitsPerSample );
427 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
428 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
429 fmt.p_extra = tk->p_extra = malloc( fmt.i_extra );
430 if( !fmt.p_extra ) goto error;
431 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
433 /* Rewrite the vorbis headers from Xiph-like format
434 * to VLC internal format
437 * - 1st byte == N, is the number of packets - 1
438 * - Following bytes are the size of the N first packets:
439 * while( *p == 0xFF ) { size += 0xFF; p++ } size += *p;
440 * (the size of the last packet is the size of remaining
441 * data in the buffer)
442 * - Finally, all the packets concatenated
445 * - Size of the packet on 16 bits (big endian) FIXME: should be 32 bits to be safe
446 * - The packet itself
447 * - Size of the next packet, and so on ...
450 if( tk->i_codec == VLC_CODEC_VORBIS )
452 uint8_t *p_extra = fmt.p_extra;
453 size_t i_extra = fmt.i_extra;
455 if( i_extra <= 1 ) break;
456 if( *p_extra++ != 2 ) break; /* 3 packets - 1 = 2 */
459 size_t i_identifier_len = 0;
460 while( *p_extra == 0xFF )
462 i_identifier_len += 0xFF;
464 if( --i_extra <= 1 ) break;
466 i_identifier_len += *p_extra++;
467 if( i_identifier_len > --i_extra ) break;
469 size_t i_comment_len = 0;
470 while( *p_extra == 0xFF )
472 i_comment_len += 0xFF;
474 if( --i_extra <= 1 ) break;
476 i_comment_len += *p_extra++;
477 if( i_comment_len > --i_extra ) break;
478 size_t i_cookbook_len = i_extra;
480 size_t i_headers_size = 3 * 2 + i_identifier_len +
481 i_comment_len + i_cookbook_len;
482 uint8_t *p_out = malloc( i_headers_size );
483 if( !p_out ) goto error;
485 fmt.p_extra = tk->p_extra = p_out;
486 fmt.i_extra = i_headers_size;
487 #define copy_packet( len ) \
488 *p_out++ = len >> 8; \
489 *p_out++ = len & 0xFF; \
490 memcpy( p_out, p_extra, len ); \
493 copy_packet( i_identifier_len );
494 copy_packet( i_comment_len );
495 copy_packet( i_cookbook_len );
501 case( AVIFOURCC_vids ):
502 tk->i_cat = VIDEO_ES;
503 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
504 p_vids->p_bih->biCompression );
505 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
507 msg_Dbg( p_demux, "stream[%d] subtitles", i );
508 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
511 else if( p_vids->p_bih->biCompression == 0x00 )
513 switch( p_vids->p_bih->biBitCount )
516 tk->i_codec = VLC_CODEC_RGB32;
519 tk->i_codec = VLC_CODEC_RGB24;
521 case 16: /* Yes it is RV15 */
523 tk->i_codec = VLC_CODEC_RGB15;
525 case 9: /* <- TODO check that */
526 tk->i_codec = VLC_CODEC_I410;
528 case 8: /* <- TODO check that */
529 tk->i_codec = VLC_CODEC_GREY;
532 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
534 switch( tk->i_codec )
536 case VLC_CODEC_RGB24:
537 case VLC_CODEC_RGB32:
538 fmt.video.i_rmask = 0x00ff0000;
539 fmt.video.i_gmask = 0x0000ff00;
540 fmt.video.i_bmask = 0x000000ff;
542 case VLC_CODEC_RGB15:
543 fmt.video.i_rmask = 0x7c00;
544 fmt.video.i_gmask = 0x03e0;
545 fmt.video.i_bmask = 0x001f;
553 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
554 if( tk->i_codec == VLC_CODEC_MP4V &&
555 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
558 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
561 tk->i_samplesize = 0;
562 fmt.video.i_width = p_vids->p_bih->biWidth;
563 fmt.video.i_height = p_vids->p_bih->biHeight;
564 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
565 fmt.video.i_frame_rate = tk->i_rate;
566 fmt.video.i_frame_rate_base = tk->i_scale;
568 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
569 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
570 fmt.p_extra = &p_vids->p_bih[1];
571 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
572 i, (char*)&p_vids->p_bih->biCompression,
573 (uint32_t)p_vids->p_bih->biWidth,
574 (uint32_t)p_vids->p_bih->biHeight,
575 p_vids->p_bih->biBitCount,
576 (float)tk->i_rate/(float)tk->i_scale );
578 if( p_vids->p_bih->biCompression == 0x00 )
580 /* RGB DIB are coded from bottom to top */
582 (unsigned int)(-(int)p_vids->p_bih->biHeight);
585 /* Extract palette from extradata if bpp <= 8
586 * (assumes that extradata contains only palette but appears
587 * to be true for all palettized codecs we support) */
588 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
590 /* The palette is not always included in biSize */
591 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
592 if( fmt.i_extra > 0 )
594 const uint8_t *p_pal = fmt.p_extra;
596 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
597 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
599 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
601 for( int j = 0; j < 4; j++ )
602 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
608 case( AVIFOURCC_txts):
610 tk->i_codec = VLC_CODEC_SUBT;
611 msg_Dbg( p_demux, "stream[%d] subtitles", i );
612 es_format_Init( &fmt, SPU_ES, tk->i_codec );
615 case( AVIFOURCC_iavs):
616 case( AVIFOURCC_ivas):
617 p_sys->b_muxed = true;
618 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
619 if( p_strh->i_handler == FOURCC_dvsd ||
620 p_strh->i_handler == FOURCC_dvhd ||
621 p_strh->i_handler == FOURCC_dvsl ||
622 p_strh->i_handler == FOURCC_dv25 ||
623 p_strh->i_handler == FOURCC_dv50 )
625 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
626 if( !tk->p_out_muxed )
627 msg_Err( p_demux, "could not load the DV parser" );
633 case( AVIFOURCC_mids):
634 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
637 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
642 fmt.psz_description = FromLatin1( p_strn->p_str );
643 if( tk->p_out_muxed == NULL )
644 tk->p_es = es_out_Add( p_demux->out, &fmt );
645 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
648 if( p_sys->i_track <= 0 )
650 msg_Err( p_demux, "no valid track" );
654 i_do_index = config_GetInt( p_demux, "avi-index" );
655 if( i_do_index == 1 ) /* Always fix */
658 if( p_sys->b_seekable )
660 AVI_IndexCreate( p_demux );
664 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
665 AVI_IndexLoad( p_demux );
670 AVI_IndexLoad( p_demux );
673 /* *** movie length in sec *** */
674 p_sys->i_length = AVI_MovieGetLength( p_demux );
675 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
676 (mtime_t)p_avih->i_microsecperframe /
679 if( !vlc_object_alive( p_demux) )
682 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
683 "approximative or will exhibit strange behavior" );
684 if( i_do_index == 0 && !b_index )
686 if( !p_sys->b_seekable ) {
690 switch( dialog_Question( p_demux, _("AVI Index") ,
691 _( "This AVI file is broken. Seeking will not work correctly.\n"
692 "Do you want to try to fix it?\n\n"
693 "This might take a long time." ),
694 _( "Repair" ), _( "Don't repair" ), _( "Cancel") ) )
698 msg_Dbg( p_demux, "Fixing AVI index" );
702 vlc_object_kill( p_demux->p_parent );
708 /* fix some BeOS MediaKit generated file */
709 for( i = 0 ; i < p_sys->i_track; i++ )
711 avi_track_t *tk = p_sys->track[i];
712 avi_chunk_list_t *p_strl;
713 avi_chunk_strh_t *p_strh;
714 avi_chunk_strf_auds_t *p_auds;
716 if( tk->i_cat != AUDIO_ES )
720 if( tk->i_idxnb < 1 ||
722 tk->i_samplesize != 0 )
726 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
727 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
728 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
730 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
731 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
733 int64_t i_track_length =
734 tk->p_index[tk->i_idxnb-1].i_length +
735 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
736 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
737 (mtime_t)p_avih->i_microsecperframe;
741 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
744 tk->i_samplesize = 1;
745 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
746 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
750 if( p_sys->b_seekable )
752 /* we have read all chunk so go back to movi */
753 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
755 /* Skip movi header */
756 stream_Read( p_demux->s, NULL, 12 );
758 p_sys->i_movi_begin = p_movi->i_chunk_pos;
764 vlc_meta_Delete( p_sys->meta );
766 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
768 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
771 /*****************************************************************************
772 * Close: frees unused data
773 *****************************************************************************/
774 static void Close ( vlc_object_t * p_this )
776 demux_t * p_demux = (demux_t *)p_this;
778 demux_sys_t *p_sys = p_demux->p_sys ;
780 for( i = 0; i < p_sys->i_track; i++ )
782 if( p_sys->track[i] )
784 if( p_sys->track[i]->p_out_muxed )
785 stream_Delete( p_sys->track[i]->p_out_muxed );
786 free( p_sys->track[i]->p_index );
787 free( p_sys->track[i]->p_extra );
788 free( p_sys->track[i] );
791 free( p_sys->track );
792 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
793 vlc_meta_Delete( p_sys->meta );
798 /*****************************************************************************
799 * Demux_Seekable: reads and demuxes data packets for stream seekable
800 *****************************************************************************
801 * AVIDemux: reads and demuxes data packets
802 *****************************************************************************
803 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
804 *****************************************************************************/
811 off_t i_posf; /* where we will read :
812 if i_idxposb == 0 : begining of chunk (+8 to acces data)
813 else : point on data directly */
814 } avi_track_toread_t;
816 static int Demux_Seekable( demux_t *p_demux )
818 demux_sys_t *p_sys = p_demux->p_sys;
820 unsigned int i_track_count = 0;
821 unsigned int i_track;
822 /* cannot be more than 100 stream (dcXX or wbXX) */
823 avi_track_toread_t toread[100];
826 /* detect new selected/unselected streams */
827 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
829 avi_track_t *tk = p_sys->track[i_track];
832 if( p_sys->b_muxed && tk->p_out_muxed )
835 tk->b_activated = true;
839 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
840 if( b && !tk->b_activated )
842 if( p_sys->b_seekable)
844 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
846 tk->b_activated = true;
848 else if( !b && tk->b_activated )
850 tk->b_activated = false;
858 if( i_track_count <= 0 )
860 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
862 p_sys->i_time += 25*1000; /* read 25ms */
865 if( p_sys->i_time >= i_length )
869 msg_Warn( p_demux, "no track selected, exiting..." );
873 /* wait for the good time */
874 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
875 p_sys->i_time += 25*1000; /* read 25ms */
878 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
880 avi_track_t *tk = p_sys->track[i_track];
883 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
884 if( tk->i_idxposc < tk->i_idxnb )
886 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
887 if( tk->i_idxposb > 0 )
889 toread[i_track].i_posf += 8 + tk->i_idxposb;
894 toread[i_track].i_posf = -1;
897 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
899 if( tk->i_samplesize )
901 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
905 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
910 toread[i_track].i_toread *= -1;
923 /* search for first chunk to be read */
924 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
926 if( !toread[i].b_ok ||
927 AVI_GetDPTS( p_sys->track[i],
928 toread[i].i_toread ) <= -25 * 1000 )
933 if( toread[i].i_toread > 0 )
935 b_done = false; /* not yet finished */
937 if( toread[i].i_posf > 0 )
939 if( i_pos == -1 || i_pos > toread[i].i_posf )
942 i_pos = toread[i].i_posf;
949 for( i = 0; i < p_sys->i_track; i++ )
954 msg_Warn( p_demux, "all tracks have failed, exiting..." );
960 int i_loop_count = 0;
962 /* no valid index, we will parse directly the stream
963 * in case we fail we will disable all finished stream */
964 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
966 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
967 if( AVI_PacketNext( p_demux ) )
969 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
974 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
981 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
984 "cannot get packet header, track disabled" );
985 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
987 if( avi_pk.i_stream >= p_sys->i_track ||
988 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
990 if( AVI_PacketNext( p_demux ) )
993 "cannot skip packet, track disabled" );
994 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
997 /* Prevents from eating all the CPU with broken files.
998 * This value should be low enough so that it doesn't
999 * affect the reading speed too much. */
1000 if( !(++i_loop_count % 1024) )
1002 if( !vlc_object_alive (p_demux) ) return -1;
1005 if( !(i_loop_count % (1024 * 10)) )
1007 "don't seem to find any data..." );
1013 /* add this chunk to the index */
1016 index.i_id = avi_pk.i_fourcc;
1018 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1020 index.i_pos = avi_pk.i_pos;
1021 index.i_length = avi_pk.i_size;
1022 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1024 i_track = avi_pk.i_stream;
1025 tk = p_sys->track[i_track];
1026 /* do we will read this data ? */
1027 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1033 if( AVI_PacketNext( p_demux ) )
1036 "cannot skip packet, track disabled" );
1037 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1046 stream_Seek( p_demux->s, i_pos );
1049 /* Set the track to use */
1050 tk = p_sys->track[i_track];
1052 /* read thoses data */
1053 if( tk->i_samplesize )
1055 unsigned int i_toread;
1057 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1059 if( tk->i_samplesize > 1 )
1061 i_toread = tk->i_samplesize;
1065 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1066 i_toread = __MAX( i_toread, 100 );
1069 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
1075 i_size = tk->p_index[tk->i_idxposc].i_length;
1078 if( tk->i_idxposb == 0 )
1080 i_size += 8; /* need to read and skip header */
1083 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1085 msg_Warn( p_demux, "failed reading data" );
1087 toread[i_track].b_ok = false;
1090 if( i_size % 2 ) /* read was padded on word boundary */
1092 p_frame->i_buffer--;
1095 if( tk->i_idxposb == 0 )
1097 p_frame->p_buffer += 8;
1098 p_frame->i_buffer -= 8;
1100 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1101 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1103 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1107 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1111 if( tk->i_samplesize )
1113 if( tk->i_idxposb == 0 )
1117 toread[i_track].i_toread -= i_size;
1118 tk->i_idxposb += i_size;
1119 if( tk->i_idxposb >=
1120 tk->p_index[tk->i_idxposc].i_length )
1128 int i_length = tk->p_index[tk->i_idxposc].i_length;
1131 if( tk->i_cat == AUDIO_ES )
1133 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1135 toread[i_track].i_toread--;
1138 if( tk->i_idxposc < tk->i_idxnb)
1140 toread[i_track].i_posf =
1141 tk->p_index[tk->i_idxposc].i_pos;
1142 if( tk->i_idxposb > 0 )
1144 toread[i_track].i_posf += 8 + tk->i_idxposb;
1150 toread[i_track].i_posf = -1;
1153 if( tk->i_cat != VIDEO_ES )
1154 p_frame->i_dts = p_frame->i_pts;
1157 p_frame->i_dts = p_frame->i_pts;
1161 //p_pes->i_rate = p_demux->stream.control.i_rate;
1162 if( tk->p_out_muxed )
1163 stream_DemuxSend( tk->p_out_muxed, p_frame );
1165 es_out_Send( p_demux->out, tk->p_es, p_frame );
1170 /*****************************************************************************
1171 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1172 *****************************************************************************
1173 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1174 *****************************************************************************/
1175 static int Demux_UnSeekable( demux_t *p_demux )
1177 demux_sys_t *p_sys = p_demux->p_sys;
1178 avi_track_t *p_stream_master = NULL;
1179 unsigned int i_stream;
1180 unsigned int i_packet;
1182 if( p_sys->b_muxed )
1184 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1185 return VLC_EGENERIC;
1188 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1190 /* *** find master stream for data packet skipping algo *** */
1191 /* *** -> first video, if any, or first audio ES *** */
1192 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1194 avi_track_t *tk = p_sys->track[i_stream];
1197 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1199 if( b && tk->i_cat == VIDEO_ES )
1201 p_stream_master = tk;
1205 p_stream_master = tk;
1209 if( !p_stream_master )
1211 msg_Warn( p_demux, "no more stream selected" );
1215 p_sys->i_time = AVI_GetPTS( p_stream_master );
1217 for( i_packet = 0; i_packet < 10; i_packet++)
1219 #define p_stream p_sys->track[avi_pk.i_stream]
1221 avi_packet_t avi_pk;
1223 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1228 if( avi_pk.i_stream >= p_sys->i_track ||
1229 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1231 /* we haven't found an audio or video packet:
1232 * - we have seek, found first next packet
1233 * - others packets could be found, skip them
1235 switch( avi_pk.i_fourcc )
1237 case AVIFOURCC_JUNK:
1238 case AVIFOURCC_LIST:
1239 case AVIFOURCC_RIFF:
1240 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1241 case AVIFOURCC_idx1:
1244 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1246 return( 0 ); /* eof */
1249 "seems to have lost position, resync" );
1250 if( AVI_PacketSearch( p_demux ) )
1252 msg_Err( p_demux, "resync failed" );
1259 /* check for time */
1260 if( __ABS( AVI_GetPTS( p_stream ) -
1261 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1263 /* load it and send to decoder */
1265 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1269 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1271 if( avi_pk.i_cat != VIDEO_ES )
1272 p_frame->i_dts = p_frame->i_pts;
1275 p_frame->i_dts = p_frame->i_pts;
1279 //p_pes->i_rate = p_demux->stream.control.i_rate;
1280 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1284 if( AVI_PacketNext( p_demux ) )
1290 /* *** update stream time position *** */
1291 if( p_stream->i_samplesize )
1293 p_stream->i_idxposb += avi_pk.i_size;
1297 if( p_stream->i_cat == AUDIO_ES )
1299 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1301 p_stream->i_idxposc++;
1311 /*****************************************************************************
1312 * Seek: goto to i_date or i_percent
1313 *****************************************************************************/
1314 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1317 demux_sys_t *p_sys = p_demux->p_sys;
1318 unsigned int i_stream;
1319 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1320 i_date / 1000000, i_percent );
1322 if( p_sys->b_seekable )
1324 if( !p_sys->i_length )
1326 avi_track_t *p_stream;
1329 /* use i_percent to create a true i_date */
1330 msg_Warn( p_demux, "seeking without index at %d%%"
1331 " only works for interleaved files", i_percent );
1332 if( i_percent >= 100 )
1334 msg_Warn( p_demux, "cannot seek so far !" );
1335 return VLC_EGENERIC;
1337 i_percent = __MAX( i_percent, 0 );
1339 /* try to find chunk that is at i_percent or the file */
1340 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1341 p_sys->i_movi_begin );
1342 /* search first selected stream (and prefer non eof ones) */
1343 for( i_stream = 0, p_stream = NULL;
1344 i_stream < p_sys->i_track; i_stream++ )
1346 if( !p_stream || p_stream->b_eof )
1347 p_stream = p_sys->track[i_stream];
1349 if( p_stream->b_activated && !p_stream->b_eof )
1352 if( !p_stream || !p_stream->b_activated )
1354 msg_Warn( p_demux, "cannot find any selected stream" );
1355 return VLC_EGENERIC;
1358 /* be sure that the index exist */
1359 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1361 msg_Warn( p_demux, "cannot seek" );
1362 return VLC_EGENERIC;
1365 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1366 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1368 /* search after i_idxposc */
1369 if( AVI_StreamChunkSet( p_demux,
1370 i_stream, p_stream->i_idxposc + 1 ) )
1372 msg_Warn( p_demux, "cannot seek" );
1373 return VLC_EGENERIC;
1377 i_date = AVI_GetPTS( p_stream );
1378 /* TODO better support for i_samplesize != 0 */
1379 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1383 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1385 avi_track_t *p_stream = p_sys->track[i_stream];
1387 if( !p_stream->b_activated )
1390 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1392 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1393 p_sys->i_time = i_date;
1394 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1399 msg_Err( p_demux, "shouldn't yet be executed" );
1400 return VLC_EGENERIC;
1404 /*****************************************************************************
1406 *****************************************************************************/
1407 static double ControlGetPosition( demux_t *p_demux )
1409 demux_sys_t *p_sys = p_demux->p_sys;
1411 if( p_sys->i_length > 0 )
1413 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1415 else if( stream_Size( p_demux->s ) > 0 )
1421 /* search the more advanced selected es */
1422 for( i = 0; i < p_sys->i_track; i++ )
1424 avi_track_t *tk = p_sys->track[i];
1425 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1427 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1428 tk->p_index[tk->i_idxposc].i_length + 8;
1435 return (double)i64 / stream_Size( p_demux->s );
1440 static int Control( demux_t *p_demux, int i_query, va_list args )
1442 demux_sys_t *p_sys = p_demux->p_sys;
1450 case DEMUX_GET_POSITION:
1451 pf = (double*)va_arg( args, double * );
1452 *pf = ControlGetPosition( p_demux );
1454 case DEMUX_SET_POSITION:
1455 f = (double)va_arg( args, double );
1456 if( p_sys->b_seekable )
1458 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1459 return Seek( p_demux, i64, (int)(f * 100) );
1463 int64_t i_pos = stream_Size( p_demux->s ) * f;
1464 return stream_Seek( p_demux->s, i_pos );
1467 case DEMUX_GET_TIME:
1468 pi64 = (int64_t*)va_arg( args, int64_t * );
1469 *pi64 = p_sys->i_time;
1472 case DEMUX_SET_TIME:
1476 i64 = (int64_t)va_arg( args, int64_t );
1477 if( p_sys->i_length > 0 )
1479 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1481 else if( p_sys->i_time > 0 )
1483 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1484 (double)i64 / (double)p_sys->i_time );
1486 return Seek( p_demux, i64, i_percent );
1488 case DEMUX_GET_LENGTH:
1489 pi64 = (int64_t*)va_arg( args, int64_t * );
1490 *pi64 = p_sys->i_length * (mtime_t)1000000;
1494 pf = (double*)va_arg( args, double * );
1496 for( i = 0; i < (int)p_sys->i_track; i++ )
1498 avi_track_t *tk = p_sys->track[i];
1499 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1501 *pf = (float)tk->i_rate / (float)tk->i_scale;
1506 case DEMUX_GET_META:
1507 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1508 vlc_meta_Merge( p_meta, p_sys->meta );
1512 return VLC_EGENERIC;
1516 /*****************************************************************************
1517 * Function to convert pts to chunk or byte
1518 *****************************************************************************/
1520 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1525 return (mtime_t)((int64_t)i_pts *
1526 (int64_t)tk->i_rate /
1527 (int64_t)tk->i_scale /
1530 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1532 if( !tk->i_scale || !tk->i_samplesize )
1535 return (mtime_t)((int64_t)i_pts *
1536 (int64_t)tk->i_rate /
1537 (int64_t)tk->i_scale /
1539 (int64_t)tk->i_samplesize );
1542 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1549 i_dpts = (mtime_t)( (int64_t)1000000 *
1551 (int64_t)tk->i_scale /
1552 (int64_t)tk->i_rate );
1554 if( tk->i_samplesize )
1556 return i_dpts / tk->i_samplesize;
1561 static mtime_t AVI_GetPTS( avi_track_t *tk )
1563 if( tk->i_samplesize )
1565 int64_t i_count = 0;
1567 /* we need a valid entry we will emulate one */
1568 if( tk->i_idxposc == tk->i_idxnb )
1572 /* use the last entry */
1573 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1574 + tk->p_index[tk->i_idxnb - 1].i_length;
1579 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1581 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1585 if( tk->i_cat == AUDIO_ES )
1587 return AVI_GetDPTS( tk, tk->i_blockno );
1591 return AVI_GetDPTS( tk, tk->i_idxposc );
1596 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1598 demux_sys_t *p_sys = p_demux->p_sys;
1599 avi_packet_t avi_pk;
1600 int i_loop_count = 0;
1602 /* find first chunk of i_stream that isn't in index */
1604 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1606 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1607 if( AVI_PacketNext( p_demux ) )
1609 return VLC_EGENERIC;
1614 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1619 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1621 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1623 msg_Warn( p_demux, "cannot get packet header" );
1624 return VLC_EGENERIC;
1626 if( avi_pk.i_stream >= p_sys->i_track ||
1627 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1629 if( AVI_PacketNext( p_demux ) )
1631 return VLC_EGENERIC;
1634 /* Prevents from eating all the CPU with broken files.
1635 * This value should be low enough so that it doesn't
1636 * affect the reading speed too much. */
1637 if( !(++i_loop_count % 1024) )
1639 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1642 if( !(i_loop_count % (1024 * 10)) )
1643 msg_Warn( p_demux, "don't seem to find any data..." );
1648 /* add this chunk to the index */
1651 index.i_id = avi_pk.i_fourcc;
1653 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1655 index.i_pos = avi_pk.i_pos;
1656 index.i_length = avi_pk.i_size;
1657 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1659 if( avi_pk.i_stream == i_stream )
1664 if( AVI_PacketNext( p_demux ) )
1666 return VLC_EGENERIC;
1672 /* be sure that i_ck will be a valid index entry */
1673 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1676 demux_sys_t *p_sys = p_demux->p_sys;
1677 avi_track_t *p_stream = p_sys->track[i_stream];
1679 p_stream->i_idxposc = i_ck;
1680 p_stream->i_idxposb = 0;
1682 if( i_ck >= p_stream->i_idxnb )
1684 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1687 p_stream->i_idxposc++;
1688 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1690 return VLC_EGENERIC;
1693 } while( p_stream->i_idxposc < i_ck );
1699 /* XXX FIXME up to now, we assume that all chunk are one after one */
1700 static int AVI_StreamBytesSet( demux_t *p_demux,
1701 unsigned int i_stream,
1704 demux_sys_t *p_sys = p_demux->p_sys;
1705 avi_track_t *p_stream = p_sys->track[i_stream];
1707 if( ( p_stream->i_idxnb > 0 )
1708 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1709 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1711 /* index is valid to find the ck */
1712 /* uses dichototmie to be fast enougth */
1713 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1714 int i_idxmax = p_stream->i_idxnb;
1718 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1720 i_idxmax = i_idxposc ;
1721 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1725 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1726 p_stream->p_index[i_idxposc].i_length <= i_byte)
1728 i_idxmin = i_idxposc ;
1729 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1733 p_stream->i_idxposc = i_idxposc;
1734 p_stream->i_idxposb = i_byte -
1735 p_stream->p_index[i_idxposc].i_lengthtotal;
1744 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1745 p_stream->i_idxposb = 0;
1748 p_stream->i_idxposc++;
1749 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1751 return VLC_EGENERIC;
1754 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1755 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1757 p_stream->i_idxposb = i_byte -
1758 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1763 static int AVI_TrackSeek( demux_t *p_demux,
1767 demux_sys_t *p_sys = p_demux->p_sys;
1768 avi_track_t *tk = p_sys->track[i_stream];
1770 #define p_stream p_sys->track[i_stream]
1773 i_oldpts = AVI_GetPTS( p_stream );
1775 if( !p_stream->i_samplesize )
1777 if( AVI_StreamChunkSet( p_demux,
1779 AVI_PTSToChunk( p_stream, i_date ) ) )
1781 return VLC_EGENERIC;
1784 if( p_stream->i_cat == AUDIO_ES )
1788 for( i = 0; i < tk->i_idxposc; i++ )
1790 if( tk->i_blocksize > 0 )
1792 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1802 "old:%"PRId64" %s new %"PRId64,
1804 i_oldpts > i_date ? ">" : "<",
1807 if( p_stream->i_cat == VIDEO_ES )
1809 /* search key frame */
1810 //if( i_date < i_oldpts || 1 )
1812 while( p_stream->i_idxposc > 0 &&
1813 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1816 if( AVI_StreamChunkSet( p_demux,
1818 p_stream->i_idxposc - 1 ) )
1820 return VLC_EGENERIC;
1827 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1828 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1831 if( AVI_StreamChunkSet( p_demux,
1833 p_stream->i_idxposc + 1 ) )
1835 return VLC_EGENERIC;
1844 if( AVI_StreamBytesSet( p_demux,
1846 AVI_PTSToByte( p_stream, i_date ) ) )
1848 return VLC_EGENERIC;
1855 /****************************************************************************
1856 * Return true if it's a key frame
1857 ****************************************************************************/
1858 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1862 case VLC_CODEC_DIV1:
1864 * startcode: 0x00000100 32bits
1865 * framenumber ? 5bits
1866 * piture type 0(I),1(P) 2bits
1868 if( GetDWBE( p_byte ) != 0x00000100 )
1870 /* it's not an msmpegv1 stream, strange...*/
1871 return AVIIF_KEYFRAME;
1873 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1875 case VLC_CODEC_DIV2:
1876 case VLC_CODEC_DIV3:
1877 case VLC_CODEC_WMV1:
1879 * picture type 0(I),1(P) 2bits
1881 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1882 case VLC_CODEC_MP4V:
1883 /* we should find first occurrence of 0x000001b6 (32bits)
1884 * startcode: 0x000001b6 32bits
1885 * piture type 0(I),1(P) 2bits
1887 if( GetDWBE( p_byte ) != 0x000001b6 )
1889 /* not true , need to find the first VOP header */
1890 return AVIIF_KEYFRAME;
1892 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1895 /* I can't do it, so say yes */
1896 return AVIIF_KEYFRAME;
1900 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1905 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1908 return vlc_fourcc_GetCodec( i_cat, i_codec );
1910 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1914 /****************************************************************************
1916 ****************************************************************************/
1917 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1918 unsigned int *pi_number, unsigned int *pi_type )
1920 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1923 c1 = ((uint8_t *)&i_id)[0];
1924 c2 = ((uint8_t *)&i_id)[1];
1926 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1928 SET_PTR( pi_number, 100 ); /* > max stream number */
1929 SET_PTR( pi_type, UNKNOWN_ES );
1933 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1934 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1937 SET_PTR( pi_type, AUDIO_ES );
1942 SET_PTR( pi_type, VIDEO_ES );
1945 SET_PTR( pi_type, UNKNOWN_ES );
1952 /****************************************************************************
1954 ****************************************************************************/
1955 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1957 const uint8_t *p_peek;
1959 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1961 return VLC_EGENERIC;
1963 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1964 p_pk->i_size = GetDWLE( p_peek + 4 );
1965 p_pk->i_pos = stream_Tell( p_demux->s );
1966 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1968 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1969 p_peek[10], p_peek[11] );
1976 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1978 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1982 static int AVI_PacketNext( demux_t *p_demux )
1984 avi_packet_t avi_ck;
1987 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1989 return VLC_EGENERIC;
1992 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1993 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1997 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1998 avi_ck.i_type == AVIFOURCC_AVIX )
2004 i_skip = __EVEN( avi_ck.i_size ) + 8;
2007 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2009 return VLC_EGENERIC;
2014 static int AVI_PacketRead( demux_t *p_demux,
2016 block_t **pp_frame )
2020 i_size = __EVEN( p_pk->i_size + 8 );
2022 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2024 return VLC_EGENERIC;
2026 (*pp_frame)->p_buffer += 8;
2027 (*pp_frame)->i_buffer -= 8;
2029 if( i_size != p_pk->i_size + 8 )
2031 (*pp_frame)->i_buffer--;
2037 static int AVI_PacketSearch( demux_t *p_demux )
2039 demux_sys_t *p_sys = p_demux->p_sys;
2040 avi_packet_t avi_pk;
2045 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2047 return VLC_EGENERIC;
2049 AVI_PacketGetHeader( p_demux, &avi_pk );
2050 if( avi_pk.i_stream < p_sys->i_track &&
2051 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2055 switch( avi_pk.i_fourcc )
2057 case AVIFOURCC_JUNK:
2058 case AVIFOURCC_LIST:
2059 case AVIFOURCC_RIFF:
2060 case AVIFOURCC_idx1:
2064 /* Prevents from eating all the CPU with broken files.
2065 * This value should be low enough so that it doesn't affect the
2066 * reading speed too much (not that we care much anyway because
2067 * this code is called only on broken files). */
2068 if( !(++i_count % 1024) )
2070 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2073 if( !(i_count % (1024 * 10)) )
2074 msg_Warn( p_demux, "trying to resync..." );
2079 /****************************************************************************
2081 ****************************************************************************/
2082 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2084 avi_entry_t *p_index)
2086 avi_track_t *tk = p_sys->track[i_stream];
2088 /* Update i_movi_lastchunk_pos */
2089 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2091 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2095 if( tk->i_idxnb >= tk->i_idxmax )
2097 tk->i_idxmax += 16384;
2098 tk->p_index = realloc( tk->p_index,
2099 tk->i_idxmax * sizeof( avi_entry_t ) );
2100 if( tk->p_index == NULL )
2105 /* calculate cumulate length */
2106 if( tk->i_idxnb > 0 )
2108 p_index->i_lengthtotal =
2109 tk->p_index[tk->i_idxnb - 1].i_length +
2110 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2114 p_index->i_lengthtotal = 0;
2117 tk->p_index[tk->i_idxnb++] = *p_index;
2120 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2122 demux_sys_t *p_sys = p_demux->p_sys;
2124 avi_chunk_list_t *p_riff;
2125 avi_chunk_list_t *p_movi;
2126 avi_chunk_idx1_t *p_idx1;
2128 unsigned int i_stream;
2129 unsigned int i_index;
2135 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2136 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2137 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2141 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2142 return VLC_EGENERIC;
2145 /* *** calculate offset *** */
2146 /* Well, avi is __SHIT__ so test more than one entry
2147 * (needed for some avi files) */
2149 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2151 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2153 i_offset = p_movi->i_chunk_pos + 8;
2158 /* Reset b_keyset */
2159 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2160 b_keyset[i_stream] = false;
2162 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2166 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2169 if( i_stream < p_sys->i_track &&
2170 i_cat == p_sys->track[i_stream]->i_cat )
2173 index.i_id = p_idx1->entry[i_index].i_fourcc;
2175 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2176 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2177 index.i_length = p_idx1->entry[i_index].i_length;
2178 AVI_IndexAddEntry( p_sys, i_stream, &index );
2180 if( index.i_flags&AVIIF_KEYFRAME )
2181 b_keyset[i_stream] = true;
2185 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2187 if( !b_keyset[i_stream] )
2189 avi_track_t *tk = p_sys->track[i_stream];
2191 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2192 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2193 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2199 static void __Parse_indx( demux_t *p_demux,
2201 avi_chunk_indx_t *p_indx )
2203 demux_sys_t *p_sys = p_demux->p_sys;
2207 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2208 if( p_indx->i_indexsubtype == 0 )
2210 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2212 index.i_id = p_indx->i_id;
2213 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2214 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2215 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2217 AVI_IndexAddEntry( p_sys, i_stream, &index );
2220 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2222 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2224 index.i_id = p_indx->i_id;
2225 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2226 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2227 index.i_length = p_indx->idx.field[i].i_size;
2229 AVI_IndexAddEntry( p_sys, i_stream, &index );
2234 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2238 static void AVI_IndexLoad_indx( demux_t *p_demux )
2240 demux_sys_t *p_sys = p_demux->p_sys;
2241 unsigned int i_stream;
2244 avi_chunk_list_t *p_riff;
2245 avi_chunk_list_t *p_hdrl;
2247 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2248 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2250 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2252 avi_chunk_list_t *p_strl;
2253 avi_chunk_indx_t *p_indx;
2255 #define p_stream p_sys->track[i_stream]
2256 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2257 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2261 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2266 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2268 __Parse_indx( p_demux, i_stream, p_indx );
2270 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2273 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2275 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2276 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2280 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2285 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2291 static void AVI_IndexLoad( demux_t *p_demux )
2293 demux_sys_t *p_sys = p_demux->p_sys;
2294 unsigned int i_stream;
2296 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2298 p_sys->track[i_stream]->i_idxnb = 0;
2299 p_sys->track[i_stream]->i_idxmax = 0;
2300 p_sys->track[i_stream]->p_index = NULL;
2305 AVI_IndexLoad_indx( p_demux );
2307 else if( AVI_IndexLoad_idx1( p_demux ) )
2309 /* try indx if idx1 failed as some "normal" file have indx too */
2310 AVI_IndexLoad_indx( p_demux );
2313 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2315 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2316 i_stream, p_sys->track[i_stream]->i_idxnb );
2320 static void AVI_IndexCreate( demux_t *p_demux )
2322 demux_sys_t *p_sys = p_demux->p_sys;
2324 avi_chunk_list_t *p_riff;
2325 avi_chunk_list_t *p_movi;
2327 unsigned int i_stream;
2330 mtime_t i_dialog_update;
2331 dialog_progress_bar_t *p_dialog = NULL;
2333 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2334 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2338 msg_Err( p_demux, "cannot find p_movi" );
2342 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2344 p_sys->track[i_stream]->i_idxnb = 0;
2345 p_sys->track[i_stream]->i_idxmax = 0;
2346 p_sys->track[i_stream]->p_index = NULL;
2348 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2349 stream_Size( p_demux->s ) );
2351 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2352 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2355 /* Only show dialog if AVI is > 10MB */
2356 i_dialog_update = mdate();
2357 if( stream_Size( p_demux->s ) > 10000000 )
2358 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2359 NULL, _("Cancel") );
2365 if( !vlc_object_alive (p_demux) )
2368 /* Don't update/check dialog too often */
2369 if( p_dialog && mdate() - i_dialog_update > 100000 )
2371 if( dialog_ProgressCancelled( p_dialog ) )
2374 double f_current = stream_Tell( p_demux->s );
2375 double f_size = stream_Size( p_demux->s );
2376 double f_pos = f_current / f_size;
2377 dialog_ProgressSet( p_dialog, NULL, f_pos );
2379 i_dialog_update = mdate();
2382 if( AVI_PacketGetHeader( p_demux, &pk ) )
2385 if( pk.i_stream < p_sys->i_track &&
2386 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2389 index.i_id = pk.i_fourcc;
2391 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2392 index.i_pos = pk.i_pos;
2393 index.i_length = pk.i_size;
2394 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2398 switch( pk.i_fourcc )
2400 case AVIFOURCC_idx1:
2403 avi_chunk_list_t *p_sysx;
2404 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2405 AVIFOURCC_RIFF, 1 );
2407 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2408 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2414 case AVIFOURCC_RIFF:
2415 msg_Dbg( p_demux, "new RIFF chunk found" );
2419 case AVIFOURCC_JUNK:
2423 msg_Warn( p_demux, "need resync, probably broken avi" );
2424 if( AVI_PacketSearch( p_demux ) )
2426 msg_Warn( p_demux, "lost sync, abord index creation" );
2432 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2433 AVI_PacketNext( p_demux ) )
2440 if( p_dialog != NULL )
2441 dialog_ProgressDestroy( p_dialog );
2443 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2445 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2446 i_stream, p_sys->track[i_stream]->i_idxnb );
2450 /*****************************************************************************
2452 *****************************************************************************/
2453 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2455 demux_sys_t *p_sys = p_demux->p_sys;
2459 for( i = 0; i < p_sys->i_track; i++ )
2461 avi_track_t *tk = p_sys->track[i];
2462 if( tk->i_idxposc >= tk->i_idxnb )
2474 /****************************************************************************
2475 * AVI_MovieGetLength give max streams length in second
2476 ****************************************************************************/
2477 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2479 demux_sys_t *p_sys = p_demux->p_sys;
2480 mtime_t i_maxlength = 0;
2483 for( i = 0; i < p_sys->i_track; i++ )
2485 avi_track_t *tk = p_sys->track[i];
2488 /* fix length for each stream */
2489 if( tk->i_idxnb < 1 || !tk->p_index )
2494 if( tk->i_samplesize )
2496 i_length = AVI_GetDPTS( tk,
2497 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2498 tk->p_index[tk->i_idxnb-1].i_length );
2502 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2504 i_length /= (mtime_t)1000000; /* in seconds */
2507 "stream[%d] length:%"PRId64" (based on index)",
2510 i_maxlength = __MAX( i_maxlength, i_length );