1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_input.h>
39 #include <vlc_dialog.h>
42 #include <vlc_codecs.h>
43 #include <vlc_charset.h>
44 #include <vlc_memory.h>
49 /*****************************************************************************
51 *****************************************************************************/
53 #define INTERLEAVE_TEXT N_("Force interleaved method" )
55 #define INDEX_TEXT N_("Force index creation")
56 #define INDEX_LONGTEXT N_( \
57 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
58 "or incomplete (not seekable)." )
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 static const int pi_index[] = {0,1,2,3};
65 static const char *const ppsz_indexes[] = { N_("Ask for action"),
68 N_("Fix when necessary")};
71 set_shortname( "AVI" )
72 set_description( N_("AVI demuxer") )
73 set_capability( "demux", 212 )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_DEMUX )
77 add_bool( "avi-interleaved", false,
78 INTERLEAVE_TEXT, INTERLEAVE_TEXT, true )
79 add_integer( "avi-index", 0,
80 INDEX_TEXT, INDEX_LONGTEXT, false )
81 change_integer_list( pi_index, ppsz_indexes )
83 set_callbacks( Open, Close )
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Control ( demux_t *, int, va_list );
90 static int Seek ( demux_t *, mtime_t, int );
91 static int Demux_Seekable ( demux_t * );
92 static int Demux_UnSeekable( demux_t * );
94 static char *FromACP( const char *str )
96 return FromCharset(vlc_pgettext("GetACP", "CP1252"), str, strlen(str));
99 #define IGNORE_ES NAV_ES
103 vlc_fourcc_t i_fourcc;
106 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
108 uint8_t i_peek[8]; /* first 8 bytes */
110 unsigned int i_stream;
121 int64_t i_lengthtotal;
129 avi_entry_t *p_entry;
132 static void avi_index_Init( avi_index_t * );
133 static void avi_index_Clean( avi_index_t * );
134 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
141 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
142 vlc_fourcc_t i_codec;
146 unsigned int i_samplesize;
151 es_out_id_t *p_es_dv_audio;
156 unsigned int i_idxposc; /* numero of chunk */
157 unsigned int i_idxposb; /* byte in the current chunk */
159 /* For VBR audio only */
160 unsigned int i_blockno;
161 unsigned int i_blocksize;
176 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
178 /* number of streams and information */
179 unsigned int i_track;
185 unsigned int i_attachment;
186 input_attachment_t **attachment;
189 static inline off_t __EVEN( off_t i )
191 return (i & 1) ? i + 1 : i;
194 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
195 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
196 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
197 static mtime_t AVI_GetPTS ( avi_track_t * );
200 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
201 static int AVI_StreamChunkSet ( demux_t *,
202 unsigned int i_stream, unsigned int i_ck );
203 static int AVI_StreamBytesSet ( demux_t *,
204 unsigned int i_stream, off_t i_byte );
206 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
207 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
209 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
210 static int AVI_PacketNext ( demux_t * );
211 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
212 static int AVI_PacketSearch ( demux_t * );
214 static void AVI_IndexLoad ( demux_t * );
215 static void AVI_IndexCreate ( demux_t * );
217 static void AVI_ExtractSubtitle( demux_t *, unsigned int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
219 static void AVI_DvHandleAudio( demux_t *, avi_track_t *, block_t * );
221 static mtime_t AVI_MovieGetLength( demux_t * );
223 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
225 /*****************************************************************************
227 *****************************************************************************/
228 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
229 static int AVI_TrackStopFinishedStreams( demux_t *);
232 - For VBR mp3 stream:
233 count blocks by rounded-up chunksizes instead of chunks
234 we need full emulation of dshow avi demuxer bugs :(
235 fixes silly nandub-style a-v delaying in avi with vbr mp3...
236 (from mplayer 2002/08/02)
240 /*****************************************************************************
241 * Open: check file and initializes AVI structures
242 *****************************************************************************/
243 static int Open( vlc_object_t * p_this )
245 demux_t *p_demux = (demux_t *)p_this;
248 bool b_index = false, b_aborted = false;
251 avi_chunk_list_t *p_riff;
252 avi_chunk_list_t *p_hdrl, *p_movi;
253 avi_chunk_avih_t *p_avih;
255 unsigned int i_track;
256 unsigned int i, i_peeker;
258 const uint8_t *p_peek;
260 /* Is it an avi file ? */
261 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
263 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
265 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
267 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
271 if( i_peeker == 188 )
276 /* Initialize input structures. */
277 p_sys = p_demux->p_sys = calloc( 1, sizeof(demux_sys_t) );
278 p_sys->b_odml = false;
281 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
283 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
285 p_demux->pf_control = Control;
286 p_demux->pf_demux = Demux_Seekable;
288 /* For unseekable stream, automatically use Demux_UnSeekable */
289 if( !p_sys->b_seekable
290 || var_InheritBool( p_demux, "avi-interleaved" ) )
292 p_demux->pf_demux = Demux_UnSeekable;
297 stream_Read( p_demux->s, NULL, i_peeker );
300 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
302 msg_Err( p_demux, "avi module discarded (invalid file)" );
307 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
309 unsigned int i_count =
310 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
312 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
313 for( i = 1; i < i_count; i++ )
315 avi_chunk_list_t *p_sysx;
317 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
318 if( p_sysx->i_type == AVIFOURCC_AVIX )
320 msg_Warn( p_demux, "detected OpenDML file" );
321 p_sys->b_odml = true;
327 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
328 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
329 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
331 p_movi = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_movi, 0 );
333 if( !p_hdrl || !p_movi )
335 msg_Err( p_demux, "invalid file: cannot find hdrl or movi chunks" );
339 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
341 msg_Err( p_demux, "invalid file: cannot find avih chunk" );
344 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
345 if( p_avih->i_streams != i_track )
348 "found %d stream but %d are declared",
349 i_track, p_avih->i_streams );
353 msg_Err( p_demux, "no stream defined!" );
357 /* print information on streams */
358 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
360 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
361 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
362 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
363 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
365 AVI_MetaLoad( p_demux, p_riff, p_avih );
367 /* now read info on each stream and create ES */
368 for( i = 0 ; i < i_track; i++ )
370 avi_track_t *tk = calloc( 1, sizeof( avi_track_t ) );
371 if( unlikely( !tk ) )
374 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
375 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
376 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
377 avi_chunk_strf_auds_t *p_auds = NULL;
378 avi_chunk_strf_vids_t *p_vids = NULL;
382 tk->b_activated = true;
384 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
385 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
387 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
389 msg_Warn( p_demux, "stream[%d] incomplete", i );
394 tk->i_rate = p_strh->i_rate;
395 tk->i_scale = p_strh->i_scale;
396 tk->i_samplesize = p_strh->i_samplesize;
397 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
398 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
400 switch( p_strh->i_type )
402 case( AVIFOURCC_auds ):
403 tk->i_cat = AUDIO_ES;
404 if( p_auds->p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
405 p_auds->p_wf->cbSize >= sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) )
407 WAVEFORMATEXTENSIBLE *p_wfe = (WAVEFORMATEXTENSIBLE *)p_auds->p_wf;
408 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
409 p_wfe->SubFormat.Data1 );
412 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
413 p_auds->p_wf->wFormatTag );
415 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
416 if( tk->i_blocksize == 0 )
418 if( p_auds->p_wf->wFormatTag == 1 )
419 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
423 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
425 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
426 "Using blocksize as a workaround.",
427 i, tk->i_samplesize, tk->i_blocksize );
428 tk->i_samplesize = tk->i_blocksize;
431 if( tk->i_codec == VLC_CODEC_VORBIS )
433 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
436 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
438 fmt.audio.i_channels = p_auds->p_wf->nChannels;
439 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
440 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
441 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
442 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
443 fmt.b_packetized = !tk->i_blocksize;
446 "stream[%d] audio(0x%x - %s) %d channels %dHz %dbits",
447 i, p_auds->p_wf->wFormatTag,vlc_fourcc_GetDescription(AUDIO_ES,tk->i_codec),
448 p_auds->p_wf->nChannels,
449 p_auds->p_wf->nSamplesPerSec,
450 p_auds->p_wf->wBitsPerSample );
452 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
453 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
454 if( fmt.i_extra > 0 )
456 fmt.p_extra = malloc( fmt.i_extra );
457 if( !fmt.p_extra ) goto error;
458 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
462 case( AVIFOURCC_vids ):
464 tk->i_cat = VIDEO_ES;
465 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
466 p_vids->p_bih->biCompression );
467 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
469 msg_Dbg( p_demux, "stream[%d] subtitles", i );
470 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
474 else if( p_vids->p_bih->biCompression == 0x00 )
476 switch( p_vids->p_bih->biBitCount )
479 tk->i_codec = VLC_CODEC_RGB32;
482 tk->i_codec = VLC_CODEC_RGB24;
484 case 16: /* Yes it is RV15 */
486 tk->i_codec = VLC_CODEC_RGB15;
488 case 9: /* <- TODO check that */
489 tk->i_codec = VLC_CODEC_I410;
491 case 8: /* <- TODO check that */
492 tk->i_codec = VLC_CODEC_GREY;
495 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
497 switch( tk->i_codec )
499 case VLC_CODEC_RGB24:
500 case VLC_CODEC_RGB32:
501 fmt.video.i_rmask = 0x00ff0000;
502 fmt.video.i_gmask = 0x0000ff00;
503 fmt.video.i_bmask = 0x000000ff;
505 case VLC_CODEC_RGB15:
506 fmt.video.i_rmask = 0x7c00;
507 fmt.video.i_gmask = 0x03e0;
508 fmt.video.i_bmask = 0x001f;
516 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
517 if( tk->i_codec == VLC_CODEC_MP4V &&
518 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
521 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
524 tk->i_samplesize = 0;
525 fmt.video.i_width = p_vids->p_bih->biWidth;
526 fmt.video.i_height = p_vids->p_bih->biHeight;
527 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
528 fmt.video.i_frame_rate = tk->i_rate;
529 fmt.video.i_frame_rate_base = tk->i_scale;
530 avi_chunk_vprp_t *p_vprp = AVI_ChunkFind( p_strl, AVIFOURCC_vprp, 0 );
533 uint32_t i_frame_aspect_ratio = p_vprp->i_frame_aspect_ratio;
534 if( p_vprp->i_video_format_token >= 1 &&
535 p_vprp->i_video_format_token <= 4 )
536 i_frame_aspect_ratio = 0x00040003;
537 fmt.video.i_sar_num = ((i_frame_aspect_ratio >> 16) & 0xffff) * fmt.video.i_height;
538 fmt.video.i_sar_den = ((i_frame_aspect_ratio >> 0) & 0xffff) * fmt.video.i_width;
540 /* Extradata is the remainder of the chunk less the BIH */
541 fmt.i_extra = p_vids->i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
542 if( fmt.i_extra > 0 )
544 fmt.p_extra = malloc( fmt.i_extra );
545 if( !fmt.p_extra ) goto error;
546 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
549 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
550 i, (char*)&p_vids->p_bih->biCompression,
551 (uint32_t)p_vids->p_bih->biWidth,
552 (uint32_t)p_vids->p_bih->biHeight,
553 p_vids->p_bih->biBitCount,
554 (float)tk->i_rate/(float)tk->i_scale );
556 if( p_vids->p_bih->biCompression == 0x00 )
558 /* RGB DIB are coded from bottom to top */
560 (unsigned int)(-(int)p_vids->p_bih->biHeight);
563 /* Extract palette from extradata if bpp <= 8 */
564 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
566 /* The palette should not be included in biSize, but come
567 * directly after BITMAPINFORHEADER in the BITMAPINFO structure */
568 if( fmt.i_extra > 0 && fmt.p_extra )
570 const uint8_t *p_pal = fmt.p_extra;
572 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
573 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
575 for( int k = 0; k < fmt.video.p_palette->i_entries; k++ )
577 for( int j = 0; j < 4; j++ )
578 fmt.video.p_palette->palette[k][j] = p_pal[4*k+j];
585 case( AVIFOURCC_txts):
586 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
587 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
591 case( AVIFOURCC_iavs):
592 case( AVIFOURCC_ivas):
593 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
594 tk->i_cat = VIDEO_ES;
595 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES, p_strh->i_handler );
596 tk->i_samplesize = 0;
597 tk->i_dv_audio_rate = tk->i_codec == VLC_CODEC_DV ? -1 : 0;
599 es_format_Init( &fmt, VIDEO_ES, p_strh->i_handler );
600 fmt.video.i_width = p_avih->i_width;
601 fmt.video.i_height = p_avih->i_height;
604 case( AVIFOURCC_mids):
605 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
608 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
613 fmt.psz_description = FromACP( p_strn->p_str );
614 tk->p_es = es_out_Add( p_demux->out, &fmt );
615 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
616 es_format_Clean( &fmt );
619 if( p_sys->i_track <= 0 )
621 msg_Err( p_demux, "no valid track" );
625 i_do_index = var_InheritInteger( p_demux, "avi-index" );
626 if( i_do_index == 1 ) /* Always fix */
629 if( p_sys->b_seekable )
631 AVI_IndexCreate( p_demux );
635 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
636 AVI_IndexLoad( p_demux );
641 AVI_IndexLoad( p_demux );
644 /* *** movie length in sec *** */
645 p_sys->i_length = AVI_MovieGetLength( p_demux );
647 /* Check the index completeness */
648 unsigned int i_idx_totalframes = 0;
649 for( unsigned int i = 0; i < p_sys->i_track; i++ )
651 const avi_track_t *tk = p_sys->track[i];
652 if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
653 i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
655 if( i_idx_totalframes != p_avih->i_totalframes &&
656 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
657 (mtime_t)p_avih->i_microsecperframe /
660 if( !vlc_object_alive( p_demux) )
663 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
664 "approximative or will exhibit strange behavior" );
665 if( (i_do_index == 0 || i_do_index == 3) && !b_index )
667 if( !p_sys->b_seekable ) {
671 if( i_do_index == 0 )
673 switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
674 _( "Because this AVI file index is broken or missing, "
675 "seeking will not work correctly.\n"
676 "VLC won't repair your file but can temporary fix this "
677 "problem by building an index in memory.\n"
678 "This step might take a long time on a large file.\n"
679 "What do you want to do?" ),
680 _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
684 msg_Dbg( p_demux, "Fixing AVI index" );
694 msg_Dbg( p_demux, "Fixing AVI index" );
700 /* fix some BeOS MediaKit generated file */
701 for( i = 0 ; i < p_sys->i_track; i++ )
703 avi_track_t *tk = p_sys->track[i];
704 avi_chunk_list_t *p_strl;
705 avi_chunk_strf_auds_t *p_auds;
707 if( tk->i_cat != AUDIO_ES )
711 if( tk->idx.i_size < 1 ||
713 tk->i_samplesize != 0 )
717 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
718 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
720 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
721 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
723 int64_t i_track_length =
724 tk->idx.p_entry[tk->idx.i_size-1].i_length +
725 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
726 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
727 (mtime_t)p_avih->i_microsecperframe;
731 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
734 tk->i_samplesize = 1;
735 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
736 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
740 if( p_sys->b_seekable )
742 /* we have read all chunk so go back to movi */
743 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
745 /* Skip movi header */
746 stream_Read( p_demux->s, NULL, 12 );
748 p_sys->i_movi_begin = p_movi->i_chunk_pos;
752 for( unsigned i = 0; i < p_sys->i_attachment; i++)
753 vlc_input_attachment_Delete(p_sys->attachment[i]);
754 free(p_sys->attachment);
757 vlc_meta_Delete( p_sys->meta );
759 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
761 return b_aborted ? VLC_ETIMEOUT : VLC_EGENERIC;
764 /*****************************************************************************
765 * Close: frees unused data
766 *****************************************************************************/
767 static void Close ( vlc_object_t * p_this )
769 demux_t * p_demux = (demux_t *)p_this;
770 demux_sys_t *p_sys = p_demux->p_sys ;
772 for( unsigned int i = 0; i < p_sys->i_track; i++ )
774 if( p_sys->track[i] )
776 avi_index_Clean( &p_sys->track[i]->idx );
777 free( p_sys->track[i] );
780 free( p_sys->track );
782 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
783 vlc_meta_Delete( p_sys->meta );
785 for( unsigned i = 0; i < p_sys->i_attachment; i++)
786 vlc_input_attachment_Delete(p_sys->attachment[i]);
787 free(p_sys->attachment);
792 /*****************************************************************************
793 * Demux_Seekable: reads and demuxes data packets for stream seekable
794 *****************************************************************************
795 * AVIDemux: reads and demuxes data packets
796 *****************************************************************************
797 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
798 *****************************************************************************/
805 off_t i_posf; /* where we will read :
806 if i_idxposb == 0 : begining of chunk (+8 to acces data)
807 else : point on data directly */
808 } avi_track_toread_t;
810 static int Demux_Seekable( demux_t *p_demux )
812 demux_sys_t *p_sys = p_demux->p_sys;
814 unsigned int i_track_count = 0;
815 unsigned int i_track;
816 /* cannot be more than 100 stream (dcXX or wbXX) */
817 avi_track_toread_t toread[100];
820 /* detect new selected/unselected streams */
821 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
823 avi_track_t *tk = p_sys->track[i_track];
826 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
827 if( tk->p_es_dv_audio )
830 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
833 if( b && !tk->b_activated )
835 if( p_sys->b_seekable)
837 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
839 tk->b_activated = true;
841 else if( !b && tk->b_activated )
843 tk->b_activated = false;
851 if( i_track_count <= 0 )
853 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
855 p_sys->i_time += 25*1000; /* read 25ms */
858 if( p_sys->i_time >= i_length )
862 msg_Warn( p_demux, "no track selected, exiting..." );
866 /* wait for the good time */
867 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
868 p_sys->i_time += 25*1000; /* read 25ms */
871 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
873 avi_track_t *tk = p_sys->track[i_track];
876 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
877 if( tk->i_idxposc < tk->idx.i_size )
879 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
880 if( tk->i_idxposb > 0 )
882 toread[i_track].i_posf += 8 + tk->i_idxposb;
887 toread[i_track].i_posf = -1;
890 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
892 if( tk->i_samplesize )
894 toread[i_track].i_toread = AVI_PTSToByte( tk, llabs( i_dpts ) );
898 toread[i_track].i_toread = AVI_PTSToChunk( tk, llabs( i_dpts ) );
903 toread[i_track].i_toread *= -1;
916 /* search for first chunk to be read */
917 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
919 if( !toread[i].b_ok ||
920 AVI_GetDPTS( p_sys->track[i],
921 toread[i].i_toread ) <= -25 * 1000 )
926 if( toread[i].i_toread > 0 )
928 b_done = false; /* not yet finished */
930 if( toread[i].i_posf > 0 )
932 if( i_pos == -1 || i_pos > toread[i].i_posf )
935 i_pos = toread[i].i_posf;
942 for( i = 0; i < p_sys->i_track; i++ )
947 msg_Warn( p_demux, "all tracks have failed, exiting..." );
953 int i_loop_count = 0;
955 /* no valid index, we will parse directly the stream
956 * in case we fail we will disable all finished stream */
957 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
959 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
960 if( AVI_PacketNext( p_demux ) )
962 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
967 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
974 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
977 "cannot get packet header, track disabled" );
978 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
980 if( avi_pk.i_stream >= p_sys->i_track ||
981 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
983 if( AVI_PacketNext( p_demux ) )
986 "cannot skip packet, track disabled" );
987 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
990 /* Prevents from eating all the CPU with broken files.
991 * This value should be low enough so that it doesn't
992 * affect the reading speed too much. */
993 if( !(++i_loop_count % 1024) )
995 if( !vlc_object_alive (p_demux) ) return -1;
998 if( !(i_loop_count % (1024 * 10)) )
1000 "don't seem to find any data..." );
1006 i_track = avi_pk.i_stream;
1007 tk = p_sys->track[i_track];
1009 /* add this chunk to the index */
1011 index.i_id = avi_pk.i_fourcc;
1012 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
1013 index.i_pos = avi_pk.i_pos;
1014 index.i_length = avi_pk.i_size;
1015 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1017 /* do we will read this data ? */
1018 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1024 if( AVI_PacketNext( p_demux ) )
1027 "cannot skip packet, track disabled" );
1028 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1037 stream_Seek( p_demux->s, i_pos );
1040 /* Set the track to use */
1041 tk = p_sys->track[i_track];
1043 /* read thoses data */
1044 if( tk->i_samplesize )
1046 unsigned int i_toread;
1048 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1050 if( tk->i_samplesize > 1 )
1052 i_toread = tk->i_samplesize;
1056 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1057 i_toread = __MAX( i_toread, 100 );
1060 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1066 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1069 if( tk->i_idxposb == 0 )
1071 i_size += 8; /* need to read and skip header */
1074 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1076 msg_Warn( p_demux, "failed reading data" );
1078 toread[i_track].b_ok = false;
1081 if( i_size % 2 ) /* read was padded on word boundary */
1083 p_frame->i_buffer--;
1086 if( tk->i_idxposb == 0 )
1088 p_frame->p_buffer += 8;
1089 p_frame->i_buffer -= 8;
1091 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1092 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1094 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1098 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1102 if( tk->i_samplesize )
1104 if( tk->i_idxposb == 0 )
1108 toread[i_track].i_toread -= i_size;
1109 tk->i_idxposb += i_size;
1110 if( tk->i_idxposb >=
1111 tk->idx.p_entry[tk->i_idxposc].i_length )
1119 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1122 if( tk->i_cat == AUDIO_ES )
1124 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1126 toread[i_track].i_toread--;
1129 if( tk->i_idxposc < tk->idx.i_size)
1131 toread[i_track].i_posf =
1132 tk->idx.p_entry[tk->i_idxposc].i_pos;
1133 if( tk->i_idxposb > 0 )
1135 toread[i_track].i_posf += 8 + tk->i_idxposb;
1141 toread[i_track].i_posf = -1;
1144 if( tk->i_cat != VIDEO_ES )
1145 p_frame->i_dts = p_frame->i_pts;
1148 p_frame->i_dts = p_frame->i_pts;
1149 p_frame->i_pts = VLC_TS_INVALID;
1152 if( tk->i_dv_audio_rate )
1153 AVI_DvHandleAudio( p_demux, tk, p_frame );
1154 es_out_Send( p_demux->out, tk->p_es, p_frame );
1159 /*****************************************************************************
1160 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1161 *****************************************************************************
1162 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1163 *****************************************************************************/
1164 static int Demux_UnSeekable( demux_t *p_demux )
1166 demux_sys_t *p_sys = p_demux->p_sys;
1167 avi_track_t *p_stream_master = NULL;
1168 unsigned int i_stream;
1169 unsigned int i_packet;
1171 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1173 /* *** find master stream for data packet skipping algo *** */
1174 /* *** -> first video, if any, or first audio ES *** */
1175 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1177 avi_track_t *tk = p_sys->track[i_stream];
1180 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1181 if( tk->p_es_dv_audio )
1184 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
1188 if( b && tk->i_cat == VIDEO_ES )
1190 p_stream_master = tk;
1194 p_stream_master = tk;
1198 if( !p_stream_master )
1200 msg_Warn( p_demux, "no more stream selected" );
1204 p_sys->i_time = AVI_GetPTS( p_stream_master );
1206 for( i_packet = 0; i_packet < 10; i_packet++)
1208 #define p_stream p_sys->track[avi_pk.i_stream]
1210 avi_packet_t avi_pk;
1212 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1217 if( avi_pk.i_stream >= p_sys->i_track ||
1218 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1220 /* we haven't found an audio or video packet:
1221 * - we have seek, found first next packet
1222 * - others packets could be found, skip them
1224 switch( avi_pk.i_fourcc )
1226 case AVIFOURCC_JUNK:
1227 case AVIFOURCC_LIST:
1228 case AVIFOURCC_RIFF:
1229 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1230 case AVIFOURCC_idx1:
1233 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1235 return( 0 ); /* eof */
1238 "seems to have lost position, resync" );
1239 if( AVI_PacketSearch( p_demux ) )
1241 msg_Err( p_demux, "resync failed" );
1248 /* check for time */
1249 if( llabs( AVI_GetPTS( p_stream ) -
1250 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1252 /* load it and send to decoder */
1254 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1258 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1260 if( avi_pk.i_cat != VIDEO_ES )
1261 p_frame->i_dts = p_frame->i_pts;
1264 p_frame->i_dts = p_frame->i_pts;
1265 p_frame->i_pts = VLC_TS_INVALID;
1268 if( p_stream->i_dv_audio_rate )
1269 AVI_DvHandleAudio( p_demux, p_stream, p_frame );
1270 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1274 if( AVI_PacketNext( p_demux ) )
1280 /* *** update stream time position *** */
1281 if( p_stream->i_samplesize )
1283 p_stream->i_idxposb += avi_pk.i_size;
1287 if( p_stream->i_cat == AUDIO_ES )
1289 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1291 p_stream->i_idxposc++;
1301 /*****************************************************************************
1302 * Seek: goto to i_date or i_percent
1303 *****************************************************************************/
1304 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1306 demux_sys_t *p_sys = p_demux->p_sys;
1307 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1308 i_date / 1000000, i_percent );
1310 if( p_sys->b_seekable )
1312 if( !p_sys->i_length )
1314 avi_track_t *p_stream = NULL;
1315 unsigned i_stream = 0;
1318 /* use i_percent to create a true i_date */
1319 msg_Warn( p_demux, "seeking without index at %d%%"
1320 " only works for interleaved files", i_percent );
1321 if( i_percent >= 100 )
1323 msg_Warn( p_demux, "cannot seek so far !" );
1324 return VLC_EGENERIC;
1326 i_percent = __MAX( i_percent, 0 );
1328 /* try to find chunk that is at i_percent or the file */
1329 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1330 p_sys->i_movi_begin );
1331 /* search first selected stream (and prefer non-EOF ones) */
1332 for( unsigned i = 0; i < p_sys->i_track; i++ )
1334 avi_track_t *p_track = p_sys->track[i];
1335 if( !p_track->b_activated )
1340 if( !p_track->b_eof )
1343 if( p_stream == NULL )
1345 msg_Warn( p_demux, "cannot find any selected stream" );
1346 return VLC_EGENERIC;
1349 /* be sure that the index exist */
1350 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1352 msg_Warn( p_demux, "cannot seek" );
1353 return VLC_EGENERIC;
1356 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1357 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1359 /* search after i_idxposc */
1360 if( AVI_StreamChunkSet( p_demux,
1361 i_stream, p_stream->i_idxposc + 1 ) )
1363 msg_Warn( p_demux, "cannot seek" );
1364 return VLC_EGENERIC;
1368 i_date = AVI_GetPTS( p_stream );
1369 /* TODO better support for i_samplesize != 0 */
1370 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1374 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1376 avi_track_t *p_stream = p_sys->track[i_stream];
1378 if( !p_stream->b_activated )
1381 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1383 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1384 p_sys->i_time = i_date;
1385 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1390 msg_Err( p_demux, "shouldn't yet be executed" );
1391 return VLC_EGENERIC;
1395 /*****************************************************************************
1397 *****************************************************************************/
1398 static double ControlGetPosition( demux_t *p_demux )
1400 demux_sys_t *p_sys = p_demux->p_sys;
1402 if( p_sys->i_length > 0 )
1404 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1406 else if( stream_Size( p_demux->s ) > 0 )
1408 double i64 = (uint64_t)stream_Tell( p_demux->s );
1409 return i64 / stream_Size( p_demux->s );
1414 static int Control( demux_t *p_demux, int i_query, va_list args )
1416 demux_sys_t *p_sys = p_demux->p_sys;
1424 case DEMUX_GET_POSITION:
1425 pf = (double*)va_arg( args, double * );
1426 *pf = ControlGetPosition( p_demux );
1428 case DEMUX_SET_POSITION:
1429 f = (double)va_arg( args, double );
1430 if( p_sys->b_seekable )
1432 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1433 return Seek( p_demux, i64, (int)(f * 100) );
1437 int64_t i_pos = stream_Size( p_demux->s ) * f;
1438 return stream_Seek( p_demux->s, i_pos );
1441 case DEMUX_GET_TIME:
1442 pi64 = (int64_t*)va_arg( args, int64_t * );
1443 *pi64 = p_sys->i_time;
1446 case DEMUX_SET_TIME:
1450 i64 = (int64_t)va_arg( args, int64_t );
1451 if( p_sys->i_length > 0 )
1453 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1455 else if( p_sys->i_time > 0 )
1457 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1458 (double)i64 / (double)p_sys->i_time );
1460 return Seek( p_demux, i64, i_percent );
1462 case DEMUX_GET_LENGTH:
1463 pi64 = (int64_t*)va_arg( args, int64_t * );
1464 *pi64 = p_sys->i_length * (mtime_t)1000000;
1468 pf = (double*)va_arg( args, double * );
1470 for( i = 0; i < (int)p_sys->i_track; i++ )
1472 avi_track_t *tk = p_sys->track[i];
1473 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1475 *pf = (float)tk->i_rate / (float)tk->i_scale;
1481 case DEMUX_GET_META:
1482 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1483 vlc_meta_Merge( p_meta, p_sys->meta );
1486 case DEMUX_GET_ATTACHMENTS:
1488 if( p_sys->i_attachment <= 0 )
1489 return VLC_EGENERIC;
1491 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1492 int *pi_int = va_arg( args, int * );
1494 *pi_int = p_sys->i_attachment;
1495 *ppp_attach = calloc( p_sys->i_attachment, sizeof(**ppp_attach));
1496 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1497 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1502 return VLC_EGENERIC;
1506 /*****************************************************************************
1507 * Function to convert pts to chunk or byte
1508 *****************************************************************************/
1510 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1515 return (mtime_t)((int64_t)i_pts *
1516 (int64_t)tk->i_rate /
1517 (int64_t)tk->i_scale /
1520 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1522 if( !tk->i_scale || !tk->i_samplesize )
1525 return (mtime_t)((int64_t)i_pts *
1526 (int64_t)tk->i_rate /
1527 (int64_t)tk->i_scale /
1529 (int64_t)tk->i_samplesize );
1532 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1539 i_dpts = (mtime_t)( (int64_t)1000000 *
1541 (int64_t)tk->i_scale /
1542 (int64_t)tk->i_rate );
1544 if( tk->i_samplesize )
1546 return i_dpts / tk->i_samplesize;
1551 static mtime_t AVI_GetPTS( avi_track_t *tk )
1553 if( tk->i_samplesize )
1555 int64_t i_count = 0;
1557 /* we need a valid entry we will emulate one */
1558 if( tk->i_idxposc == tk->idx.i_size )
1562 /* use the last entry */
1563 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1564 + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1569 i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1571 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1575 if( tk->i_cat == AUDIO_ES )
1577 return AVI_GetDPTS( tk, tk->i_blockno );
1581 return AVI_GetDPTS( tk, tk->i_idxposc );
1586 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1588 demux_sys_t *p_sys = p_demux->p_sys;
1589 avi_packet_t avi_pk;
1590 int i_loop_count = 0;
1592 /* find first chunk of i_stream that isn't in index */
1594 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1596 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1597 if( AVI_PacketNext( p_demux ) )
1599 return VLC_EGENERIC;
1604 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1609 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1611 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1613 msg_Warn( p_demux, "cannot get packet header" );
1614 return VLC_EGENERIC;
1616 if( avi_pk.i_stream >= p_sys->i_track ||
1617 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1619 if( AVI_PacketNext( p_demux ) )
1621 return VLC_EGENERIC;
1624 /* Prevents from eating all the CPU with broken files.
1625 * This value should be low enough so that it doesn't
1626 * affect the reading speed too much. */
1627 if( !(++i_loop_count % 1024) )
1629 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1632 if( !(i_loop_count % (1024 * 10)) )
1633 msg_Warn( p_demux, "don't seem to find any data..." );
1638 avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1640 /* add this chunk to the index */
1642 index.i_id = avi_pk.i_fourcc;
1643 index.i_flags = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1644 index.i_pos = avi_pk.i_pos;
1645 index.i_length = avi_pk.i_size;
1646 avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1648 if( avi_pk.i_stream == i_stream )
1653 if( AVI_PacketNext( p_demux ) )
1655 return VLC_EGENERIC;
1661 /* be sure that i_ck will be a valid index entry */
1662 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1665 demux_sys_t *p_sys = p_demux->p_sys;
1666 avi_track_t *p_stream = p_sys->track[i_stream];
1668 p_stream->i_idxposc = i_ck;
1669 p_stream->i_idxposb = 0;
1671 if( i_ck >= p_stream->idx.i_size )
1673 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1676 p_stream->i_idxposc++;
1677 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1679 return VLC_EGENERIC;
1682 } while( p_stream->i_idxposc < i_ck );
1688 /* XXX FIXME up to now, we assume that all chunk are one after one */
1689 static int AVI_StreamBytesSet( demux_t *p_demux,
1690 unsigned int i_stream,
1693 demux_sys_t *p_sys = p_demux->p_sys;
1694 avi_track_t *p_stream = p_sys->track[i_stream];
1696 if( ( p_stream->idx.i_size > 0 )
1697 &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1698 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1700 /* index is valid to find the ck */
1701 /* uses dichototmie to be fast enougth */
1702 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1703 int i_idxmax = p_stream->idx.i_size;
1707 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1709 i_idxmax = i_idxposc ;
1710 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1714 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1715 p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1717 i_idxmin = i_idxposc ;
1718 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1722 p_stream->i_idxposc = i_idxposc;
1723 p_stream->i_idxposb = i_byte -
1724 p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1733 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1734 p_stream->i_idxposb = 0;
1737 p_stream->i_idxposc++;
1738 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1740 return VLC_EGENERIC;
1743 } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1744 p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1746 p_stream->i_idxposb = i_byte -
1747 p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1752 static int AVI_TrackSeek( demux_t *p_demux,
1756 demux_sys_t *p_sys = p_demux->p_sys;
1757 avi_track_t *tk = p_sys->track[i_stream];
1759 #define p_stream p_sys->track[i_stream]
1762 i_oldpts = AVI_GetPTS( p_stream );
1764 if( !p_stream->i_samplesize )
1766 if( AVI_StreamChunkSet( p_demux,
1768 AVI_PTSToChunk( p_stream, i_date ) ) )
1770 return VLC_EGENERIC;
1773 if( p_stream->i_cat == AUDIO_ES )
1777 for( i = 0; i < tk->i_idxposc; i++ )
1779 if( tk->i_blocksize > 0 )
1781 tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1791 "old:%"PRId64" %s new %"PRId64,
1793 i_oldpts > i_date ? ">" : "<",
1796 if( p_stream->i_cat == VIDEO_ES )
1798 /* search key frame */
1799 //if( i_date < i_oldpts || 1 )
1801 while( p_stream->i_idxposc > 0 &&
1802 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1805 if( AVI_StreamChunkSet( p_demux,
1807 p_stream->i_idxposc - 1 ) )
1809 return VLC_EGENERIC;
1816 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1817 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1820 if( AVI_StreamChunkSet( p_demux,
1822 p_stream->i_idxposc + 1 ) )
1824 return VLC_EGENERIC;
1833 if( AVI_StreamBytesSet( p_demux,
1835 AVI_PTSToByte( p_stream, i_date ) ) )
1837 return VLC_EGENERIC;
1844 /****************************************************************************
1845 * Return true if it's a key frame
1846 ****************************************************************************/
1847 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1851 case VLC_CODEC_DIV1:
1853 * startcode: 0x00000100 32bits
1854 * framenumber ? 5bits
1855 * piture type 0(I),1(P) 2bits
1857 if( GetDWBE( p_byte ) != 0x00000100 )
1859 /* it's not an msmpegv1 stream, strange...*/
1860 return AVIIF_KEYFRAME;
1862 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1864 case VLC_CODEC_DIV2:
1865 case VLC_CODEC_DIV3:
1866 case VLC_CODEC_WMV1:
1868 * picture type 0(I),1(P) 2bits
1870 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1871 case VLC_CODEC_MP4V:
1872 /* we should find first occurrence of 0x000001b6 (32bits)
1873 * startcode: 0x000001b6 32bits
1874 * piture type 0(I),1(P) 2bits
1876 if( GetDWBE( p_byte ) != 0x000001b6 )
1878 /* not true , need to find the first VOP header */
1879 return AVIIF_KEYFRAME;
1881 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1884 /* I can't do it, so say yes */
1885 return AVIIF_KEYFRAME;
1889 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1894 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1897 return vlc_fourcc_GetCodec( i_cat, i_codec );
1899 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1903 /****************************************************************************
1905 ****************************************************************************/
1906 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1907 unsigned int *pi_number, unsigned int *pi_type )
1909 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1912 c1 = ((uint8_t *)&i_id)[0];
1913 c2 = ((uint8_t *)&i_id)[1];
1915 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1917 SET_PTR( pi_number, 100 ); /* > max stream number */
1918 SET_PTR( pi_type, UNKNOWN_ES );
1922 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1923 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1926 SET_PTR( pi_type, AUDIO_ES );
1931 SET_PTR( pi_type, VIDEO_ES );
1935 SET_PTR( pi_type, SPU_ES );
1938 SET_PTR( pi_type, IGNORE_ES );
1941 SET_PTR( pi_type, UNKNOWN_ES );
1948 /****************************************************************************
1950 ****************************************************************************/
1951 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1953 const uint8_t *p_peek;
1955 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1957 return VLC_EGENERIC;
1959 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1960 p_pk->i_size = GetDWLE( p_peek + 4 );
1961 p_pk->i_pos = stream_Tell( p_demux->s );
1962 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1964 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1965 p_peek[10], p_peek[11] );
1972 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1974 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1978 static int AVI_PacketNext( demux_t *p_demux )
1980 avi_packet_t avi_ck;
1983 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1985 return VLC_EGENERIC;
1988 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1989 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1993 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1994 avi_ck.i_type == AVIFOURCC_AVIX )
2000 i_skip = __EVEN( avi_ck.i_size ) + 8;
2003 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2005 return VLC_EGENERIC;
2010 static int AVI_PacketRead( demux_t *p_demux,
2012 block_t **pp_frame )
2016 i_size = __EVEN( p_pk->i_size + 8 );
2018 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2020 return VLC_EGENERIC;
2022 (*pp_frame)->p_buffer += 8;
2023 (*pp_frame)->i_buffer -= 8;
2025 if( i_size != p_pk->i_size + 8 )
2027 (*pp_frame)->i_buffer--;
2033 static int AVI_PacketSearch( demux_t *p_demux )
2035 demux_sys_t *p_sys = p_demux->p_sys;
2036 avi_packet_t avi_pk;
2041 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2043 return VLC_EGENERIC;
2045 AVI_PacketGetHeader( p_demux, &avi_pk );
2046 if( avi_pk.i_stream < p_sys->i_track &&
2047 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2051 switch( avi_pk.i_fourcc )
2053 case AVIFOURCC_JUNK:
2054 case AVIFOURCC_LIST:
2055 case AVIFOURCC_RIFF:
2056 case AVIFOURCC_idx1:
2060 /* Prevents from eating all the CPU with broken files.
2061 * This value should be low enough so that it doesn't affect the
2062 * reading speed too much (not that we care much anyway because
2063 * this code is called only on broken files). */
2064 if( !(++i_count % 1024) )
2066 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2069 if( !(i_count % (1024 * 10)) )
2070 msg_Warn( p_demux, "trying to resync..." );
2075 /****************************************************************************
2077 ****************************************************************************/
2078 static void avi_index_Init( avi_index_t *p_index )
2080 p_index->i_size = 0;
2082 p_index->p_entry = NULL;
2084 static void avi_index_Clean( avi_index_t *p_index )
2086 free( p_index->p_entry );
2088 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2089 avi_entry_t *p_entry )
2091 /* Update last chunk position */
2092 if( *pi_last_pos < p_entry->i_pos )
2093 *pi_last_pos = p_entry->i_pos;
2096 if( p_index->i_size >= p_index->i_max )
2098 p_index->i_max += 16384;
2099 p_index->p_entry = realloc_or_free( p_index->p_entry,
2100 p_index->i_max * sizeof( *p_index->p_entry ) );
2101 if( !p_index->p_entry )
2104 /* calculate cumulate length */
2105 if( p_index->i_size > 0 )
2107 p_entry->i_lengthtotal =
2108 p_index->p_entry[p_index->i_size - 1].i_length +
2109 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2113 p_entry->i_lengthtotal = 0;
2116 p_index->p_entry[p_index->i_size++] = *p_entry;
2119 static int AVI_IndexFind_idx1( demux_t *p_demux,
2120 avi_chunk_idx1_t **pp_idx1,
2121 uint64_t *pi_offset )
2123 demux_sys_t *p_sys = p_demux->p_sys;
2125 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2126 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2130 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2131 return VLC_EGENERIC;
2135 /* The offset in the index should be from the start of the movi content,
2136 * but some broken files use offset from the start of the file. Just
2137 * checking the offset of the first packet is not enough as some files
2138 * has unused chunk at the beginning of the movi content.
2140 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2141 uint64_t i_first_pos = UINT64_MAX;
2142 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 100 ); i++ )
2144 if ( p_idx1->entry[i].i_length > 0 )
2145 i_first_pos = __MIN( i_first_pos, p_idx1->entry[i].i_pos );
2148 const uint64_t i_movi_content = p_movi->i_chunk_pos + 8;
2149 if( i_first_pos < i_movi_content )
2151 *pi_offset = i_movi_content;
2153 else if( p_sys->b_seekable && i_first_pos < UINT64_MAX )
2155 const uint8_t *p_peek;
2156 if( !stream_Seek( p_demux->s, i_movi_content + i_first_pos ) &&
2157 stream_Peek( p_demux->s, &p_peek, 4 ) >= 4 &&
2158 ( !isdigit( p_peek[0] ) || !isdigit( p_peek[1] ) ||
2159 !isalpha( p_peek[2] ) || !isalpha( p_peek[3] ) ) )
2162 *pi_offset = i_movi_content;
2171 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2172 avi_index_t p_index[], off_t *pi_last_offset )
2174 demux_sys_t *p_sys = p_demux->p_sys;
2176 avi_chunk_idx1_t *p_idx1;
2178 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2179 return VLC_EGENERIC;
2181 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2186 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2189 if( i_stream < p_sys->i_track &&
2190 (i_cat == p_sys->track[i_stream]->i_cat || i_cat == UNKNOWN_ES ) )
2193 index.i_id = p_idx1->entry[i_index].i_fourcc;
2194 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2195 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2196 index.i_length = p_idx1->entry[i_index].i_length;
2198 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2204 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2205 avi_chunk_indx_t *p_indx )
2209 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2210 if( p_indx->i_indexsubtype == 0 )
2212 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2214 index.i_id = p_indx->i_id;
2215 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2216 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2217 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2219 avi_index_Append( p_index, pi_max_offset, &index );
2222 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2224 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2226 index.i_id = p_indx->i_id;
2227 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2228 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2229 index.i_length = p_indx->idx.field[i].i_size;
2231 avi_index_Append( p_index, pi_max_offset, &index );
2236 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2240 static void AVI_IndexLoad_indx( demux_t *p_demux,
2241 avi_index_t p_index[], off_t *pi_last_offset )
2243 demux_sys_t *p_sys = p_demux->p_sys;
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( unsigned 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 );
2263 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2268 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2270 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2272 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2275 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2277 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2278 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2282 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2283 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2284 AVI_ChunkFree( p_demux->s, &ck_sub );
2289 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2295 static void AVI_IndexLoad( demux_t *p_demux )
2297 demux_sys_t *p_sys = p_demux->p_sys;
2300 assert( p_sys->i_track <= 100 );
2301 avi_index_t p_idx_indx[p_sys->i_track];
2302 avi_index_t p_idx_idx1[p_sys->i_track];
2303 for( unsigned i = 0; i < p_sys->i_track; i++ )
2305 avi_index_Init( &p_idx_indx[i] );
2306 avi_index_Init( &p_idx_idx1[i] );
2308 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2309 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2311 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2312 if( !p_sys->b_odml )
2313 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2315 /* Select the longest index */
2316 for( unsigned i = 0; i < p_sys->i_track; i++ )
2318 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2320 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2321 p_sys->track[i]->idx = p_idx_indx[i];
2322 avi_index_Clean( &p_idx_idx1[i] );
2326 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2327 p_sys->track[i]->idx = p_idx_idx1[i];
2328 avi_index_Clean( &p_idx_indx[i] );
2331 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2333 for( unsigned i = 0; i < p_sys->i_track; i++ )
2335 avi_index_t *p_index = &p_sys->track[i]->idx;
2339 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2340 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2343 msg_Err( p_demux, "no key frame set for track %u", i );
2344 for( unsigned j = 0; j < p_index->i_size; j++ )
2345 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2349 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2350 i, p_index->i_size );
2354 static void AVI_IndexCreate( demux_t *p_demux )
2356 demux_sys_t *p_sys = p_demux->p_sys;
2358 avi_chunk_list_t *p_riff;
2359 avi_chunk_list_t *p_movi;
2361 unsigned int i_stream;
2364 mtime_t i_dialog_update;
2365 dialog_progress_bar_t *p_dialog = NULL;
2367 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2368 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2372 msg_Err( p_demux, "cannot find p_movi" );
2376 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2377 avi_index_Init( &p_sys->track[i_stream]->idx );
2379 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2380 stream_Size( p_demux->s ) );
2382 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2383 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2386 /* Only show dialog if AVI is > 10MB */
2387 i_dialog_update = mdate();
2388 if( stream_Size( p_demux->s ) > 10000000 )
2389 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2390 NULL, _("Cancel") );
2396 if( !vlc_object_alive (p_demux) )
2399 /* Don't update/check dialog too often */
2400 if( p_dialog && mdate() - i_dialog_update > 100000 )
2402 if( dialog_ProgressCancelled( p_dialog ) )
2405 double f_current = stream_Tell( p_demux->s );
2406 double f_size = stream_Size( p_demux->s );
2407 double f_pos = f_current / f_size;
2408 dialog_ProgressSet( p_dialog, NULL, f_pos );
2410 i_dialog_update = mdate();
2413 if( AVI_PacketGetHeader( p_demux, &pk ) )
2416 if( pk.i_stream < p_sys->i_track &&
2417 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2419 avi_track_t *tk = p_sys->track[pk.i_stream];
2422 index.i_id = pk.i_fourcc;
2423 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2424 index.i_pos = pk.i_pos;
2425 index.i_length = pk.i_size;
2426 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2430 switch( pk.i_fourcc )
2432 case AVIFOURCC_idx1:
2435 avi_chunk_list_t *p_sysx;
2436 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2437 AVIFOURCC_RIFF, 1 );
2439 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2440 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2446 case AVIFOURCC_RIFF:
2447 msg_Dbg( p_demux, "new RIFF chunk found" );
2451 case AVIFOURCC_JUNK:
2455 msg_Warn( p_demux, "need resync, probably broken avi" );
2456 if( AVI_PacketSearch( p_demux ) )
2458 msg_Warn( p_demux, "lost sync, abord index creation" );
2464 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2465 AVI_PacketNext( p_demux ) )
2472 if( p_dialog != NULL )
2473 dialog_ProgressDestroy( p_dialog );
2475 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2477 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2478 i_stream, p_sys->track[i_stream]->idx.i_size );
2483 static void AVI_MetaLoad( demux_t *p_demux,
2484 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2486 demux_sys_t *p_sys = p_demux->p_sys;
2488 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2493 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2494 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2495 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2496 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2497 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2498 vlc_meta_SetSetting( p_meta, buffer );
2500 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2504 static const struct {
2508 { AVIFOURCC_IART, vlc_meta_Artist },
2509 { AVIFOURCC_ICMT, vlc_meta_Description },
2510 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2511 { AVIFOURCC_IGNR, vlc_meta_Genre },
2512 { AVIFOURCC_INAM, vlc_meta_Title },
2513 { AVIFOURCC_ICRD, vlc_meta_Date },
2514 { AVIFOURCC_ILNG, vlc_meta_Language },
2515 { AVIFOURCC_IRTD, vlc_meta_Rating },
2516 { AVIFOURCC_IWEB, vlc_meta_URL },
2517 { AVIFOURCC_IPRT, vlc_meta_TrackNumber },
2518 { AVIFOURCC_IFRM, vlc_meta_TrackTotal },
2521 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2523 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2526 char *psz_value = FromACP( p_strz->p_str );
2531 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2535 static const vlc_fourcc_t p_extra[] = {
2536 AVIFOURCC_IARL, AVIFOURCC_ICMS, AVIFOURCC_ICRP, AVIFOURCC_IDIM, AVIFOURCC_IDPI,
2537 AVIFOURCC_IENG, AVIFOURCC_IKEY, AVIFOURCC_ILGT, AVIFOURCC_IMED, AVIFOURCC_IPLT,
2538 AVIFOURCC_IPRD, AVIFOURCC_ISBJ, AVIFOURCC_ISFT, AVIFOURCC_ISHP, AVIFOURCC_ISRC,
2539 AVIFOURCC_ISRF, AVIFOURCC_ITCH, AVIFOURCC_ISMP, AVIFOURCC_IDIT, AVIFOURCC_ISGN,
2540 AVIFOURCC_IWRI, AVIFOURCC_IPRO, AVIFOURCC_ICNM, AVIFOURCC_IPDS, AVIFOURCC_IEDT,
2541 AVIFOURCC_ICDS, AVIFOURCC_IMUS, AVIFOURCC_ISTD, AVIFOURCC_IDST, AVIFOURCC_ICNT,
2545 for( int i = 0; p_extra[i] != 0; i++ )
2547 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_extra[i], 0 );
2550 char *psz_value = FromACP( p_strz->p_str );
2555 vlc_meta_AddExtra( p_meta, p_strz->p_type, psz_value );
2560 static void AVI_DvHandleAudio( demux_t *p_demux, avi_track_t *tk, block_t *p_frame )
2562 size_t i_offset = 80*6+80*16*3 + 3;
2563 if( p_frame->i_buffer < i_offset + 5 )
2566 if( p_frame->p_buffer[i_offset] != 0x50 )
2570 dv_get_audio_format( &fmt, &p_frame->p_buffer[i_offset + 1] );
2572 if( tk->p_es_dv_audio && tk->i_dv_audio_rate != fmt.audio.i_rate )
2574 es_out_Del( p_demux->out, tk->p_es_dv_audio );
2575 tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
2577 else if( !tk->p_es_dv_audio )
2579 tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
2581 tk->i_dv_audio_rate = fmt.audio.i_rate;
2583 block_t *p_frame_audio = dv_extract_audio( p_frame );
2585 es_out_Send( p_demux->out, tk->p_es_dv_audio, p_frame_audio );
2588 /*****************************************************************************
2590 *****************************************************************************/
2591 static void AVI_ExtractSubtitle( demux_t *p_demux,
2592 unsigned int i_stream,
2593 avi_chunk_list_t *p_strl,
2594 avi_chunk_STRING_t *p_strn )
2596 demux_sys_t *p_sys = p_demux->p_sys;
2597 block_t *p_block = NULL;
2598 input_attachment_t *p_attachment = NULL;
2599 char *psz_description = NULL;
2600 avi_chunk_indx_t *p_indx = NULL;
2602 if( !p_sys->b_seekable )
2605 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2611 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2612 p_indx->i_entriesinuse > 0 )
2614 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2615 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2620 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2621 p_indx->i_entriesinuse != 1 ||
2622 p_indx->i_indexsubtype != 0 )
2625 i_position = p_indx->i_baseoffset +
2626 p_indx->idx.std[0].i_offset - 8;
2627 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2631 avi_chunk_idx1_t *p_idx1;
2634 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2638 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2640 const idx1_entry_t *e = &p_idx1->entry[i];
2642 unsigned i_stream_idx;
2644 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2645 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2647 i_position = e->i_pos + i_offset;
2648 i_size = e->i_length + 8;
2657 if( i_size > 10000000 )
2659 msg_Dbg( p_demux, "Attached subtitle too big: %u", i_size );
2663 if( stream_Seek( p_demux->s, i_position ) )
2665 p_block = stream_Block( p_demux->s, i_size );
2669 /* Parse packet header */
2670 const uint8_t *p = p_block->p_buffer;
2671 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2676 /* Parse subtitle chunk header */
2677 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2678 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2680 const unsigned i_name = GetDWLE( &p[7] );
2681 if( 11 + i_size <= i_name )
2684 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2686 i_size -= 11 + i_name;
2687 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2689 const unsigned i_payload = GetDWLE( &p[2] );
2690 if( i_size < 6 + i_payload || i_payload <= 0 )
2695 if( !psz_description )
2696 psz_description = p_strn ? FromACP( p_strn->p_str ) : NULL;
2698 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2700 p_attachment = vlc_input_attachment_New( psz_name,
2701 "application/x-srt",
2705 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2709 free( psz_description );
2712 block_Release( p_block );
2715 msg_Dbg( p_demux, "Loaded an embedded subtitle" );
2717 msg_Warn( p_demux, "Failed to load an embedded subtitle" );
2719 if( p_indx == &ck.indx )
2720 AVI_ChunkFree( p_demux->s, &ck );
2722 /*****************************************************************************
2724 *****************************************************************************/
2725 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2727 demux_sys_t *p_sys = p_demux->p_sys;
2731 for( i = 0; i < p_sys->i_track; i++ )
2733 avi_track_t *tk = p_sys->track[i];
2734 if( tk->i_idxposc >= tk->idx.i_size )
2746 /****************************************************************************
2747 * AVI_MovieGetLength give max streams length in second
2748 ****************************************************************************/
2749 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2751 demux_sys_t *p_sys = p_demux->p_sys;
2752 mtime_t i_maxlength = 0;
2755 for( i = 0; i < p_sys->i_track; i++ )
2757 avi_track_t *tk = p_sys->track[i];
2760 /* fix length for each stream */
2761 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2766 if( tk->i_samplesize )
2768 i_length = AVI_GetDPTS( tk,
2769 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2770 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2774 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2776 i_length /= (mtime_t)1000000; /* in seconds */
2779 "stream[%d] length:%"PRId64" (based on index)",
2782 i_maxlength = __MAX( i_maxlength, i_length );