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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
37 #include <vlc_dialog.h>
40 #include <vlc_codecs.h>
41 #include <vlc_charset.h>
42 #include <vlc_memory.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 #define INTERLEAVE_TEXT N_("Force interleaved method" )
51 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
53 #define INDEX_TEXT N_("Force index creation")
54 #define INDEX_LONGTEXT N_( \
55 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
56 "or incomplete (not seekable)." )
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 static const int pi_index[] = {0,1,2};
63 static const char *const ppsz_indexes[] = { N_("Ask for action"),
68 set_shortname( "AVI" )
69 set_description( N_("AVI demuxer") )
70 set_capability( "demux", 212 )
71 set_category( CAT_INPUT )
72 set_subcategory( SUBCAT_INPUT_DEMUX )
74 add_bool( "avi-interleaved", false, NULL,
75 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
76 add_integer( "avi-index", 0, NULL,
77 INDEX_TEXT, INDEX_LONGTEXT, false )
78 change_integer_list( pi_index, ppsz_indexes, NULL )
80 set_callbacks( Open, Close )
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Control ( demux_t *, int, va_list );
87 static int Seek ( demux_t *, mtime_t, int );
88 static int Demux_Seekable ( demux_t * );
89 static int Demux_UnSeekable( demux_t * );
91 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
95 vlc_fourcc_t i_fourcc;
98 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
100 uint8_t i_peek[8]; /* first 8 bytes */
102 unsigned int i_stream;
113 int64_t i_lengthtotal;
121 avi_entry_t *p_entry;
124 static void avi_index_Init( avi_index_t * );
125 static void avi_index_Clean( avi_index_t * );
126 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
133 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
134 vlc_fourcc_t i_codec;
138 unsigned int i_samplesize;
145 unsigned int i_idxposc; /* numero of chunk */
146 unsigned int i_idxposb; /* byte in the current chunk */
148 /* For VBR audio only */
149 unsigned int i_blockno;
150 unsigned int i_blocksize;
152 /* For muxed streams */
153 stream_t *p_out_muxed;
168 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
170 /* number of streams and information */
171 unsigned int i_track;
177 unsigned int i_attachment;
178 input_attachment_t **attachment;
181 static inline off_t __EVEN( off_t i )
183 return (i & 1) ? i + 1 : i;
186 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
187 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
188 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
189 static mtime_t AVI_GetPTS ( avi_track_t * );
192 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
193 static int AVI_StreamChunkSet ( demux_t *,
194 unsigned int i_stream, unsigned int i_ck );
195 static int AVI_StreamBytesSet ( demux_t *,
196 unsigned int i_stream, off_t i_byte );
198 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
199 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
201 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
202 static int AVI_PacketNext ( demux_t * );
203 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
204 static int AVI_PacketSearch ( demux_t * );
206 static void AVI_IndexLoad ( demux_t * );
207 static void AVI_IndexCreate ( demux_t * );
209 static void AVI_ExtractSubtitle( demux_t *, int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
211 static mtime_t AVI_MovieGetLength( demux_t * );
213 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
215 /*****************************************************************************
217 *****************************************************************************/
218 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
219 static int AVI_TrackStopFinishedStreams( demux_t *);
222 - For VBR mp3 stream:
223 count blocks by rounded-up chunksizes instead of chunks
224 we need full emulation of dshow avi demuxer bugs :(
225 fixes silly nandub-style a-v delaying in avi with vbr mp3...
226 (from mplayer 2002/08/02)
230 /*****************************************************************************
231 * Open: check file and initializes AVI structures
232 *****************************************************************************/
233 static int Open( vlc_object_t * p_this )
235 demux_t *p_demux = (demux_t *)p_this;
238 bool b_index = false;
241 avi_chunk_list_t *p_riff;
242 avi_chunk_list_t *p_hdrl, *p_movi;
243 avi_chunk_avih_t *p_avih;
245 unsigned int i_track;
246 unsigned int i, i_peeker;
248 const uint8_t *p_peek;
250 /* Is it an avi file ? */
251 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
253 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
255 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
257 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
261 if( i_peeker == 188 )
266 /* Initialize input structures. */
267 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
268 memset( p_sys, 0, sizeof( demux_sys_t ) );
271 p_sys->i_movi_lastchunk_pos = 0;
272 p_sys->b_odml = false;
273 p_sys->b_muxed = false;
277 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
279 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
281 p_demux->pf_control = Control;
282 p_demux->pf_demux = Demux_Seekable;
284 /* For unseekable stream, automatically use Demux_UnSeekable */
285 if( !p_sys->b_seekable
286 || var_InheritBool( p_demux, "avi-interleaved" ) )
288 p_demux->pf_demux = Demux_UnSeekable;
293 stream_Read( p_demux->s, NULL, i_peeker );
296 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
298 msg_Err( p_demux, "avi module discarded (invalid file)" );
303 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
305 unsigned int i_count =
306 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
308 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
309 for( i = 1; i < i_count; i++ )
311 avi_chunk_list_t *p_sysx;
313 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
314 if( p_sysx->i_type == AVIFOURCC_AVIX )
316 msg_Warn( p_demux, "detected OpenDML file" );
317 p_sys->b_odml = true;
323 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
324 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
325 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
327 if( !p_hdrl || !p_movi )
329 msg_Err( p_demux, "avi module discarded (invalid file)" );
333 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
335 msg_Err( p_demux, "cannot find avih chunk" );
338 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
339 if( p_avih->i_streams != i_track )
342 "found %d stream but %d are declared",
343 i_track, p_avih->i_streams );
347 msg_Err( p_demux, "no stream defined!" );
351 /* print information on streams */
352 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
354 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
355 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
356 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
357 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
359 AVI_MetaLoad( p_demux, p_riff, p_avih );
361 /* now read info on each stream and create ES */
362 for( i = 0 ; i < i_track; i++ )
364 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
368 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
369 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
370 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
371 avi_chunk_strf_auds_t *p_auds = NULL;
372 avi_chunk_strf_vids_t *p_vids = NULL;
375 memset( tk, 0, sizeof(*tk) );
377 tk->b_activated = true;
379 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
380 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
382 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
384 msg_Warn( p_demux, "stream[%d] incomplete", i );
389 tk->i_rate = p_strh->i_rate;
390 tk->i_scale = p_strh->i_scale;
391 tk->i_samplesize = p_strh->i_samplesize;
392 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
393 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
395 switch( p_strh->i_type )
397 case( AVIFOURCC_auds ):
398 tk->i_cat = AUDIO_ES;
399 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
400 p_auds->p_wf->wFormatTag );
402 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
403 if( tk->i_blocksize == 0 )
405 if( p_auds->p_wf->wFormatTag == 1 )
406 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
410 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
412 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
413 "Using blocksize as a workaround.",
414 i, tk->i_samplesize, tk->i_blocksize );
415 tk->i_samplesize = tk->i_blocksize;
418 if( tk->i_codec == VLC_CODEC_VORBIS )
420 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
423 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
425 fmt.audio.i_channels = p_auds->p_wf->nChannels;
426 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
427 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
428 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
429 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
430 fmt.b_packetized = !tk->i_blocksize;
433 "stream[%d] audio(0x%x) %d channels %dHz %dbits",
434 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
435 p_auds->p_wf->nSamplesPerSec,
436 p_auds->p_wf->wBitsPerSample );
438 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
439 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
440 if( fmt.i_extra > 0 )
442 fmt.p_extra = malloc( fmt.i_extra );
443 if( !fmt.p_extra ) goto error;
444 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
448 case( AVIFOURCC_vids ):
449 tk->i_cat = VIDEO_ES;
450 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
451 p_vids->p_bih->biCompression );
452 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
454 msg_Dbg( p_demux, "stream[%d] subtitles", i );
455 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
459 else if( p_vids->p_bih->biCompression == 0x00 )
461 switch( p_vids->p_bih->biBitCount )
464 tk->i_codec = VLC_CODEC_RGB32;
467 tk->i_codec = VLC_CODEC_RGB24;
469 case 16: /* Yes it is RV15 */
471 tk->i_codec = VLC_CODEC_RGB15;
473 case 9: /* <- TODO check that */
474 tk->i_codec = VLC_CODEC_I410;
476 case 8: /* <- TODO check that */
477 tk->i_codec = VLC_CODEC_GREY;
480 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
482 switch( tk->i_codec )
484 case VLC_CODEC_RGB24:
485 case VLC_CODEC_RGB32:
486 fmt.video.i_rmask = 0x00ff0000;
487 fmt.video.i_gmask = 0x0000ff00;
488 fmt.video.i_bmask = 0x000000ff;
490 case VLC_CODEC_RGB15:
491 fmt.video.i_rmask = 0x7c00;
492 fmt.video.i_gmask = 0x03e0;
493 fmt.video.i_bmask = 0x001f;
501 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
502 if( tk->i_codec == VLC_CODEC_MP4V &&
503 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
506 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
509 tk->i_samplesize = 0;
510 fmt.video.i_width = p_vids->p_bih->biWidth;
511 fmt.video.i_height = p_vids->p_bih->biHeight;
512 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
513 fmt.video.i_frame_rate = tk->i_rate;
514 fmt.video.i_frame_rate_base = tk->i_scale;
516 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
517 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
518 if( fmt.i_extra > 0 )
520 fmt.p_extra = malloc( fmt.i_extra );
521 if( !fmt.p_extra ) goto error;
522 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
525 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
526 i, (char*)&p_vids->p_bih->biCompression,
527 (uint32_t)p_vids->p_bih->biWidth,
528 (uint32_t)p_vids->p_bih->biHeight,
529 p_vids->p_bih->biBitCount,
530 (float)tk->i_rate/(float)tk->i_scale );
532 if( p_vids->p_bih->biCompression == 0x00 )
534 /* RGB DIB are coded from bottom to top */
536 (unsigned int)(-(int)p_vids->p_bih->biHeight);
539 /* Extract palette from extradata if bpp <= 8
540 * (assumes that extradata contains only palette but appears
541 * to be true for all palettized codecs we support) */
542 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
544 /* The palette is not always included in biSize */
545 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
546 if( fmt.i_extra > 0 )
548 const uint8_t *p_pal = fmt.p_extra;
550 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
551 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
553 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
555 for( int j = 0; j < 4; j++ )
556 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
562 case( AVIFOURCC_txts):
563 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
564 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
568 case( AVIFOURCC_iavs):
569 case( AVIFOURCC_ivas):
570 p_sys->b_muxed = true;
571 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
572 if( p_strh->i_handler == FOURCC_dvsd ||
573 p_strh->i_handler == FOURCC_dvhd ||
574 p_strh->i_handler == FOURCC_dvsl ||
575 p_strh->i_handler == FOURCC_dv25 ||
576 p_strh->i_handler == FOURCC_dv50 )
578 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
579 if( !tk->p_out_muxed )
580 msg_Err( p_demux, "could not load the DV parser" );
586 case( AVIFOURCC_mids):
587 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
590 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
595 fmt.psz_description = FromLatin1( p_strn->p_str );
596 if( tk->p_out_muxed == NULL )
597 tk->p_es = es_out_Add( p_demux->out, &fmt );
598 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
601 es_format_Clean( &fmt );
605 if( p_sys->i_track <= 0 )
607 msg_Err( p_demux, "no valid track" );
611 i_do_index = var_InheritInteger( p_demux, "avi-index" );
612 if( i_do_index == 1 ) /* Always fix */
615 if( p_sys->b_seekable )
617 AVI_IndexCreate( p_demux );
621 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
622 AVI_IndexLoad( p_demux );
627 AVI_IndexLoad( p_demux );
630 /* *** movie length in sec *** */
631 p_sys->i_length = AVI_MovieGetLength( p_demux );
633 /* Check the index completeness */
634 unsigned int i_idx_totalframes = 0;
635 for( unsigned int i = 0; i < p_sys->i_track; i++ )
637 const avi_track_t *tk = p_sys->track[i];
638 if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
639 i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
642 if( i_idx_totalframes != p_avih->i_totalframes &&
643 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
644 (mtime_t)p_avih->i_microsecperframe /
647 if( !vlc_object_alive( p_demux) )
650 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
651 "approximative or will exhibit strange behavior" );
652 if( i_do_index == 0 && !b_index )
654 if( !p_sys->b_seekable ) {
658 switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
659 _( "Because this AVI file index is broken or missing, "
660 "seeking will not work correctly.\n"
661 "VLC won't repair your file but can temporary fix this "
662 "problem by building an index in memory.\n"
663 "This step might take a long time on a large file.\n"
664 "What do you want to do ?" ),
665 _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
669 msg_Dbg( p_demux, "Fixing AVI index" );
673 vlc_object_kill( p_demux->p_parent );
679 /* fix some BeOS MediaKit generated file */
680 for( i = 0 ; i < p_sys->i_track; i++ )
682 avi_track_t *tk = p_sys->track[i];
683 avi_chunk_list_t *p_strl;
684 avi_chunk_strh_t *p_strh;
685 avi_chunk_strf_auds_t *p_auds;
687 if( tk->i_cat != AUDIO_ES )
691 if( tk->idx.i_size < 1 ||
693 tk->i_samplesize != 0 )
697 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
698 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
699 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
701 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
702 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
704 int64_t i_track_length =
705 tk->idx.p_entry[tk->idx.i_size-1].i_length +
706 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
707 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
708 (mtime_t)p_avih->i_microsecperframe;
712 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
715 tk->i_samplesize = 1;
716 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
717 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
721 if( p_sys->b_seekable )
723 /* we have read all chunk so go back to movi */
724 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
726 /* Skip movi header */
727 stream_Read( p_demux->s, NULL, 12 );
729 p_sys->i_movi_begin = p_movi->i_chunk_pos;
733 for( unsigned i = 0; i < p_sys->i_attachment; i++)
734 vlc_input_attachment_Delete(p_sys->attachment[i]);
735 free(p_sys->attachment);
738 vlc_meta_Delete( p_sys->meta );
740 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
742 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
745 /*****************************************************************************
746 * Close: frees unused data
747 *****************************************************************************/
748 static void Close ( vlc_object_t * p_this )
750 demux_t * p_demux = (demux_t *)p_this;
752 demux_sys_t *p_sys = p_demux->p_sys ;
754 for( i = 0; i < p_sys->i_track; i++ )
756 if( p_sys->track[i] )
758 if( p_sys->track[i]->p_out_muxed )
759 stream_Delete( p_sys->track[i]->p_out_muxed );
760 avi_index_Clean( &p_sys->track[i]->idx );
761 free( p_sys->track[i] );
764 free( p_sys->track );
765 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
766 vlc_meta_Delete( p_sys->meta );
767 for( unsigned i = 0; i < p_sys->i_attachment; i++)
768 vlc_input_attachment_Delete(p_sys->attachment[i]);
769 free(p_sys->attachment);
774 /*****************************************************************************
775 * Demux_Seekable: reads and demuxes data packets for stream seekable
776 *****************************************************************************
777 * AVIDemux: reads and demuxes data packets
778 *****************************************************************************
779 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
780 *****************************************************************************/
787 off_t i_posf; /* where we will read :
788 if i_idxposb == 0 : begining of chunk (+8 to acces data)
789 else : point on data directly */
790 } avi_track_toread_t;
792 static int Demux_Seekable( demux_t *p_demux )
794 demux_sys_t *p_sys = p_demux->p_sys;
796 unsigned int i_track_count = 0;
797 unsigned int i_track;
798 /* cannot be more than 100 stream (dcXX or wbXX) */
799 avi_track_toread_t toread[100];
802 /* detect new selected/unselected streams */
803 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
805 avi_track_t *tk = p_sys->track[i_track];
808 if( p_sys->b_muxed && tk->p_out_muxed )
811 tk->b_activated = true;
815 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
816 if( b && !tk->b_activated )
818 if( p_sys->b_seekable)
820 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
822 tk->b_activated = true;
824 else if( !b && tk->b_activated )
826 tk->b_activated = false;
834 if( i_track_count <= 0 )
836 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
838 p_sys->i_time += 25*1000; /* read 25ms */
841 if( p_sys->i_time >= i_length )
845 msg_Warn( p_demux, "no track selected, exiting..." );
849 /* wait for the good time */
850 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
851 p_sys->i_time += 25*1000; /* read 25ms */
854 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
856 avi_track_t *tk = p_sys->track[i_track];
859 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
860 if( tk->i_idxposc < tk->idx.i_size )
862 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
863 if( tk->i_idxposb > 0 )
865 toread[i_track].i_posf += 8 + tk->i_idxposb;
870 toread[i_track].i_posf = -1;
873 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
875 if( tk->i_samplesize )
877 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
881 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
886 toread[i_track].i_toread *= -1;
899 /* search for first chunk to be read */
900 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
902 if( !toread[i].b_ok ||
903 AVI_GetDPTS( p_sys->track[i],
904 toread[i].i_toread ) <= -25 * 1000 )
909 if( toread[i].i_toread > 0 )
911 b_done = false; /* not yet finished */
913 if( toread[i].i_posf > 0 )
915 if( i_pos == -1 || i_pos > toread[i].i_posf )
918 i_pos = toread[i].i_posf;
925 for( i = 0; i < p_sys->i_track; i++ )
930 msg_Warn( p_demux, "all tracks have failed, exiting..." );
936 int i_loop_count = 0;
938 /* no valid index, we will parse directly the stream
939 * in case we fail we will disable all finished stream */
940 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
942 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
943 if( AVI_PacketNext( p_demux ) )
945 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
950 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
957 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
960 "cannot get packet header, track disabled" );
961 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
963 if( avi_pk.i_stream >= p_sys->i_track ||
964 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
966 if( AVI_PacketNext( p_demux ) )
969 "cannot skip packet, track disabled" );
970 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
973 /* Prevents from eating all the CPU with broken files.
974 * This value should be low enough so that it doesn't
975 * affect the reading speed too much. */
976 if( !(++i_loop_count % 1024) )
978 if( !vlc_object_alive (p_demux) ) return -1;
981 if( !(i_loop_count % (1024 * 10)) )
983 "don't seem to find any data..." );
989 i_track = avi_pk.i_stream;
990 tk = p_sys->track[i_track];
992 /* add this chunk to the index */
994 index.i_id = avi_pk.i_fourcc;
995 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
996 index.i_pos = avi_pk.i_pos;
997 index.i_length = avi_pk.i_size;
998 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1000 /* do we will read this data ? */
1001 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1007 if( AVI_PacketNext( p_demux ) )
1010 "cannot skip packet, track disabled" );
1011 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1020 stream_Seek( p_demux->s, i_pos );
1023 /* Set the track to use */
1024 tk = p_sys->track[i_track];
1026 /* read thoses data */
1027 if( tk->i_samplesize )
1029 unsigned int i_toread;
1031 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1033 if( tk->i_samplesize > 1 )
1035 i_toread = tk->i_samplesize;
1039 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1040 i_toread = __MAX( i_toread, 100 );
1043 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1049 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1052 if( tk->i_idxposb == 0 )
1054 i_size += 8; /* need to read and skip header */
1057 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1059 msg_Warn( p_demux, "failed reading data" );
1061 toread[i_track].b_ok = false;
1064 if( i_size % 2 ) /* read was padded on word boundary */
1066 p_frame->i_buffer--;
1069 if( tk->i_idxposb == 0 )
1071 p_frame->p_buffer += 8;
1072 p_frame->i_buffer -= 8;
1074 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1075 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1077 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1081 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1085 if( tk->i_samplesize )
1087 if( tk->i_idxposb == 0 )
1091 toread[i_track].i_toread -= i_size;
1092 tk->i_idxposb += i_size;
1093 if( tk->i_idxposb >=
1094 tk->idx.p_entry[tk->i_idxposc].i_length )
1102 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1105 if( tk->i_cat == AUDIO_ES )
1107 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1109 toread[i_track].i_toread--;
1112 if( tk->i_idxposc < tk->idx.i_size)
1114 toread[i_track].i_posf =
1115 tk->idx.p_entry[tk->i_idxposc].i_pos;
1116 if( tk->i_idxposb > 0 )
1118 toread[i_track].i_posf += 8 + tk->i_idxposb;
1124 toread[i_track].i_posf = -1;
1127 if( tk->i_cat != VIDEO_ES )
1128 p_frame->i_dts = p_frame->i_pts;
1131 p_frame->i_dts = p_frame->i_pts;
1132 p_frame->i_pts = VLC_TS_INVALID;
1135 //p_pes->i_rate = p_demux->stream.control.i_rate;
1136 if( tk->p_out_muxed )
1137 stream_DemuxSend( tk->p_out_muxed, p_frame );
1139 es_out_Send( p_demux->out, tk->p_es, p_frame );
1144 /*****************************************************************************
1145 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1146 *****************************************************************************
1147 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1148 *****************************************************************************/
1149 static int Demux_UnSeekable( demux_t *p_demux )
1151 demux_sys_t *p_sys = p_demux->p_sys;
1152 avi_track_t *p_stream_master = NULL;
1153 unsigned int i_stream;
1154 unsigned int i_packet;
1156 if( p_sys->b_muxed )
1158 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1159 return VLC_EGENERIC;
1162 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1164 /* *** find master stream for data packet skipping algo *** */
1165 /* *** -> first video, if any, or first audio ES *** */
1166 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1168 avi_track_t *tk = p_sys->track[i_stream];
1171 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1173 if( b && tk->i_cat == VIDEO_ES )
1175 p_stream_master = tk;
1179 p_stream_master = tk;
1183 if( !p_stream_master )
1185 msg_Warn( p_demux, "no more stream selected" );
1189 p_sys->i_time = AVI_GetPTS( p_stream_master );
1191 for( i_packet = 0; i_packet < 10; i_packet++)
1193 #define p_stream p_sys->track[avi_pk.i_stream]
1195 avi_packet_t avi_pk;
1197 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1202 if( avi_pk.i_stream >= p_sys->i_track ||
1203 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1205 /* we haven't found an audio or video packet:
1206 * - we have seek, found first next packet
1207 * - others packets could be found, skip them
1209 switch( avi_pk.i_fourcc )
1211 case AVIFOURCC_JUNK:
1212 case AVIFOURCC_LIST:
1213 case AVIFOURCC_RIFF:
1214 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1215 case AVIFOURCC_idx1:
1218 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1220 return( 0 ); /* eof */
1223 "seems to have lost position, resync" );
1224 if( AVI_PacketSearch( p_demux ) )
1226 msg_Err( p_demux, "resync failed" );
1233 /* check for time */
1234 if( __ABS( AVI_GetPTS( p_stream ) -
1235 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1237 /* load it and send to decoder */
1239 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1243 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1245 if( avi_pk.i_cat != VIDEO_ES )
1246 p_frame->i_dts = p_frame->i_pts;
1249 p_frame->i_dts = p_frame->i_pts;
1250 p_frame->i_pts = VLC_TS_INVALID;
1253 //p_pes->i_rate = p_demux->stream.control.i_rate;
1254 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1258 if( AVI_PacketNext( p_demux ) )
1264 /* *** update stream time position *** */
1265 if( p_stream->i_samplesize )
1267 p_stream->i_idxposb += avi_pk.i_size;
1271 if( p_stream->i_cat == AUDIO_ES )
1273 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1275 p_stream->i_idxposc++;
1285 /*****************************************************************************
1286 * Seek: goto to i_date or i_percent
1287 *****************************************************************************/
1288 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1291 demux_sys_t *p_sys = p_demux->p_sys;
1292 unsigned int i_stream;
1293 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1294 i_date / 1000000, i_percent );
1296 if( p_sys->b_seekable )
1298 if( !p_sys->i_length )
1300 avi_track_t *p_stream;
1303 /* use i_percent to create a true i_date */
1304 msg_Warn( p_demux, "seeking without index at %d%%"
1305 " only works for interleaved files", i_percent );
1306 if( i_percent >= 100 )
1308 msg_Warn( p_demux, "cannot seek so far !" );
1309 return VLC_EGENERIC;
1311 i_percent = __MAX( i_percent, 0 );
1313 /* try to find chunk that is at i_percent or the file */
1314 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1315 p_sys->i_movi_begin );
1316 /* search first selected stream (and prefer non eof ones) */
1317 for( i_stream = 0, p_stream = NULL;
1318 i_stream < p_sys->i_track; i_stream++ )
1320 if( !p_stream || p_stream->b_eof )
1321 p_stream = p_sys->track[i_stream];
1323 if( p_stream->b_activated && !p_stream->b_eof )
1326 if( !p_stream || !p_stream->b_activated )
1328 msg_Warn( p_demux, "cannot find any selected stream" );
1329 return VLC_EGENERIC;
1332 /* be sure that the index exist */
1333 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1335 msg_Warn( p_demux, "cannot seek" );
1336 return VLC_EGENERIC;
1339 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1340 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1342 /* search after i_idxposc */
1343 if( AVI_StreamChunkSet( p_demux,
1344 i_stream, p_stream->i_idxposc + 1 ) )
1346 msg_Warn( p_demux, "cannot seek" );
1347 return VLC_EGENERIC;
1351 i_date = AVI_GetPTS( p_stream );
1352 /* TODO better support for i_samplesize != 0 */
1353 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1357 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1359 avi_track_t *p_stream = p_sys->track[i_stream];
1361 if( !p_stream->b_activated )
1364 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1366 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1367 p_sys->i_time = i_date;
1368 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1373 msg_Err( p_demux, "shouldn't yet be executed" );
1374 return VLC_EGENERIC;
1378 /*****************************************************************************
1380 *****************************************************************************/
1381 static double ControlGetPosition( demux_t *p_demux )
1383 demux_sys_t *p_sys = p_demux->p_sys;
1385 if( p_sys->i_length > 0 )
1387 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1389 else if( stream_Size( p_demux->s ) > 0 )
1395 /* search the more advanced selected es */
1396 for( i = 0; i < p_sys->i_track; i++ )
1398 avi_track_t *tk = p_sys->track[i];
1399 if( tk->b_activated && tk->i_idxposc < tk->idx.i_size )
1401 i_tmp = tk->idx.p_entry[tk->i_idxposc].i_pos +
1402 tk->idx.p_entry[tk->i_idxposc].i_length + 8;
1409 return (double)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, UNKNOWN_ES );
1945 /****************************************************************************
1947 ****************************************************************************/
1948 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1950 const uint8_t *p_peek;
1952 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1954 return VLC_EGENERIC;
1956 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1957 p_pk->i_size = GetDWLE( p_peek + 4 );
1958 p_pk->i_pos = stream_Tell( p_demux->s );
1959 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1961 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1962 p_peek[10], p_peek[11] );
1969 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1971 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1975 static int AVI_PacketNext( demux_t *p_demux )
1977 avi_packet_t avi_ck;
1980 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1982 return VLC_EGENERIC;
1985 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1986 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1990 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1991 avi_ck.i_type == AVIFOURCC_AVIX )
1997 i_skip = __EVEN( avi_ck.i_size ) + 8;
2000 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2002 return VLC_EGENERIC;
2007 static int AVI_PacketRead( demux_t *p_demux,
2009 block_t **pp_frame )
2013 i_size = __EVEN( p_pk->i_size + 8 );
2015 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2017 return VLC_EGENERIC;
2019 (*pp_frame)->p_buffer += 8;
2020 (*pp_frame)->i_buffer -= 8;
2022 if( i_size != p_pk->i_size + 8 )
2024 (*pp_frame)->i_buffer--;
2030 static int AVI_PacketSearch( demux_t *p_demux )
2032 demux_sys_t *p_sys = p_demux->p_sys;
2033 avi_packet_t avi_pk;
2038 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2040 return VLC_EGENERIC;
2042 AVI_PacketGetHeader( p_demux, &avi_pk );
2043 if( avi_pk.i_stream < p_sys->i_track &&
2044 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2048 switch( avi_pk.i_fourcc )
2050 case AVIFOURCC_JUNK:
2051 case AVIFOURCC_LIST:
2052 case AVIFOURCC_RIFF:
2053 case AVIFOURCC_idx1:
2057 /* Prevents from eating all the CPU with broken files.
2058 * This value should be low enough so that it doesn't affect the
2059 * reading speed too much (not that we care much anyway because
2060 * this code is called only on broken files). */
2061 if( !(++i_count % 1024) )
2063 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2066 if( !(i_count % (1024 * 10)) )
2067 msg_Warn( p_demux, "trying to resync..." );
2072 /****************************************************************************
2074 ****************************************************************************/
2075 static void avi_index_Init( avi_index_t *p_index )
2077 p_index->i_size = 0;
2079 p_index->p_entry = NULL;
2081 static void avi_index_Clean( avi_index_t *p_index )
2083 free( p_index->p_entry );
2085 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2086 avi_entry_t *p_entry )
2088 /* Update last chunk position */
2089 if( *pi_last_pos < p_entry->i_pos )
2090 *pi_last_pos = p_entry->i_pos;
2093 if( p_index->i_size >= p_index->i_max )
2095 p_index->i_max += 16384;
2096 p_index->p_entry = realloc_or_free( p_index->p_entry,
2097 p_index->i_max * sizeof( *p_index->p_entry ) );
2098 if( !p_index->p_entry )
2101 /* calculate cumulate length */
2102 if( p_index->i_size > 0 )
2104 p_entry->i_lengthtotal =
2105 p_index->p_entry[p_index->i_size - 1].i_length +
2106 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2110 p_entry->i_lengthtotal = 0;
2113 p_index->p_entry[p_index->i_size++] = *p_entry;
2116 static int AVI_IndexFind_idx1( demux_t *p_demux,
2117 avi_chunk_idx1_t **pp_idx1,
2118 uint64_t *pi_offset )
2120 demux_sys_t *p_sys = p_demux->p_sys;
2122 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2123 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2127 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2128 return VLC_EGENERIC;
2132 /* *** calculate offset *** */
2133 /* Well, avi is __SHIT__ so test more than one entry
2134 * (needed for some avi files) */
2135 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2137 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2139 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2141 *pi_offset = p_movi->i_chunk_pos + 8;
2148 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2149 avi_index_t p_index[], off_t *pi_last_offset )
2151 demux_sys_t *p_sys = p_demux->p_sys;
2153 avi_chunk_idx1_t *p_idx1;
2155 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2156 return VLC_EGENERIC;
2158 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2163 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2166 if( i_stream < p_sys->i_track &&
2167 i_cat == p_sys->track[i_stream]->i_cat )
2170 index.i_id = p_idx1->entry[i_index].i_fourcc;
2171 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2172 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2173 index.i_length = p_idx1->entry[i_index].i_length;
2175 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2181 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2182 avi_chunk_indx_t *p_indx )
2186 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2187 if( p_indx->i_indexsubtype == 0 )
2189 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2191 index.i_id = p_indx->i_id;
2192 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2193 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2194 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2196 avi_index_Append( p_index, pi_max_offset, &index );
2199 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2201 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2203 index.i_id = p_indx->i_id;
2204 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2205 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2206 index.i_length = p_indx->idx.field[i].i_size;
2208 avi_index_Append( p_index, pi_max_offset, &index );
2213 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2217 static void AVI_IndexLoad_indx( demux_t *p_demux,
2218 avi_index_t p_index[], off_t *pi_last_offset )
2220 demux_sys_t *p_sys = p_demux->p_sys;
2222 avi_chunk_list_t *p_riff;
2223 avi_chunk_list_t *p_hdrl;
2225 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2226 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2228 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2230 avi_chunk_list_t *p_strl;
2231 avi_chunk_indx_t *p_indx;
2233 #define p_stream p_sys->track[i_stream]
2234 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2235 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2240 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2245 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2247 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2249 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2252 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2254 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2255 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2259 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2260 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2261 AVI_ChunkFree( p_demux->s, &ck_sub );
2266 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2272 static void AVI_IndexLoad( demux_t *p_demux )
2274 demux_sys_t *p_sys = p_demux->p_sys;
2277 assert( p_sys->i_track <= 100 );
2278 avi_index_t p_idx_indx[p_sys->i_track];
2279 avi_index_t p_idx_idx1[p_sys->i_track];
2280 for( unsigned i = 0; i < p_sys->i_track; i++ )
2282 avi_index_Init( &p_idx_indx[i] );
2283 avi_index_Init( &p_idx_idx1[i] );
2285 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2286 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2288 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2289 if( !p_sys->b_odml )
2290 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2292 /* Select the longest index */
2293 for( unsigned i = 0; i < p_sys->i_track; i++ )
2295 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2297 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2298 p_sys->track[i]->idx = p_idx_indx[i];
2299 avi_index_Clean( &p_idx_idx1[i] );
2303 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2304 p_sys->track[i]->idx = p_idx_idx1[i];
2305 avi_index_Clean( &p_idx_indx[i] );
2308 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2310 for( unsigned i = 0; i < p_sys->i_track; i++ )
2312 avi_index_t *p_index = &p_sys->track[i]->idx;
2316 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2317 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2320 msg_Err( p_demux, "no key frame set for track %u", i );
2321 for( unsigned j = 0; j < p_index->i_size; j++ )
2322 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2326 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2327 i, p_index->i_size );
2331 static void AVI_IndexCreate( demux_t *p_demux )
2333 demux_sys_t *p_sys = p_demux->p_sys;
2335 avi_chunk_list_t *p_riff;
2336 avi_chunk_list_t *p_movi;
2338 unsigned int i_stream;
2341 mtime_t i_dialog_update;
2342 dialog_progress_bar_t *p_dialog = NULL;
2344 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2345 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2349 msg_Err( p_demux, "cannot find p_movi" );
2353 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2354 avi_index_Init( &p_sys->track[i_stream]->idx );
2356 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2357 stream_Size( p_demux->s ) );
2359 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2360 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2363 /* Only show dialog if AVI is > 10MB */
2364 i_dialog_update = mdate();
2365 if( stream_Size( p_demux->s ) > 10000000 )
2366 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2367 NULL, _("Cancel") );
2373 if( !vlc_object_alive (p_demux) )
2376 /* Don't update/check dialog too often */
2377 if( p_dialog && mdate() - i_dialog_update > 100000 )
2379 if( dialog_ProgressCancelled( p_dialog ) )
2382 double f_current = stream_Tell( p_demux->s );
2383 double f_size = stream_Size( p_demux->s );
2384 double f_pos = f_current / f_size;
2385 dialog_ProgressSet( p_dialog, NULL, f_pos );
2387 i_dialog_update = mdate();
2390 if( AVI_PacketGetHeader( p_demux, &pk ) )
2393 if( pk.i_stream < p_sys->i_track &&
2394 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2396 avi_track_t *tk = p_sys->track[pk.i_stream];
2399 index.i_id = pk.i_fourcc;
2400 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2401 index.i_pos = pk.i_pos;
2402 index.i_length = pk.i_size;
2403 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2407 switch( pk.i_fourcc )
2409 case AVIFOURCC_idx1:
2412 avi_chunk_list_t *p_sysx;
2413 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2414 AVIFOURCC_RIFF, 1 );
2416 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2417 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2423 case AVIFOURCC_RIFF:
2424 msg_Dbg( p_demux, "new RIFF chunk found" );
2428 case AVIFOURCC_JUNK:
2432 msg_Warn( p_demux, "need resync, probably broken avi" );
2433 if( AVI_PacketSearch( p_demux ) )
2435 msg_Warn( p_demux, "lost sync, abord index creation" );
2441 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2442 AVI_PacketNext( p_demux ) )
2449 if( p_dialog != NULL )
2450 dialog_ProgressDestroy( p_dialog );
2452 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2454 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2455 i_stream, p_sys->track[i_stream]->idx.i_size );
2460 static void AVI_MetaLoad( demux_t *p_demux,
2461 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2463 demux_sys_t *p_sys = p_demux->p_sys;
2465 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2470 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2471 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2472 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2473 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2474 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2475 vlc_meta_SetSetting( p_meta, buffer );
2477 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2481 static const struct {
2485 { AVIFOURCC_IART, vlc_meta_Artist },
2486 { AVIFOURCC_ICMT, vlc_meta_Description },
2487 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2488 { AVIFOURCC_IGNR, vlc_meta_Genre },
2489 { AVIFOURCC_INAM, vlc_meta_Title },
2492 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2494 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2497 char *psz_value = FromLatin1( p_strz->p_str );
2502 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2507 /*****************************************************************************
2509 *****************************************************************************/
2510 static void AVI_ExtractSubtitle( demux_t *p_demux,
2512 avi_chunk_list_t *p_strl,
2513 avi_chunk_STRING_t *p_strn )
2515 demux_sys_t *p_sys = p_demux->p_sys;
2516 block_t *p_block = NULL;
2517 input_attachment_t *p_attachment = NULL;
2518 char *psz_description = NULL;
2519 avi_chunk_indx_t *p_indx = NULL;
2521 if( !p_sys->b_seekable )
2524 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2530 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2531 p_indx->i_entriesinuse > 0 )
2533 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2534 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2539 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2540 p_indx->i_entriesinuse != 1 ||
2541 p_indx->i_indexsubtype != 0 )
2544 i_position = p_indx->i_baseoffset +
2545 p_indx->idx.std[0].i_offset - 8;
2546 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2550 avi_chunk_idx1_t *p_idx1;
2553 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2557 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2559 const idx1_entry_t *e = &p_idx1->entry[i];
2561 unsigned i_stream_idx;
2563 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2564 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2566 i_position = e->i_pos + i_offset;
2567 i_size = e->i_length + 8;
2576 if( i_size > 1000000 )
2579 if( stream_Seek( p_demux->s, i_position ) )
2581 p_block = stream_Block( p_demux->s, i_size );
2585 /* Parse packet header */
2586 const uint8_t *p = p_block->p_buffer;
2587 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2592 /* Parse subtitle chunk header */
2593 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2594 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2596 const unsigned i_name = GetDWLE( &p[7] );
2597 if( 11 + i_size <= i_name )
2600 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2602 i_size -= 11 + i_name;
2603 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2605 const unsigned i_payload = GetDWLE( &p[2] );
2606 if( i_size < 6 + i_payload || i_payload <= 0 )
2611 if( !psz_description )
2612 psz_description = p_strn ? FromLatin1( p_strn->p_str ) : NULL;
2614 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2616 p_attachment = vlc_input_attachment_New( psz_name,
2617 "application/x-srt",
2621 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2625 free( psz_description );
2628 block_Release( p_block );
2631 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2633 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2635 if( p_indx == &ck.indx )
2636 AVI_ChunkFree( p_demux->s, &ck );
2638 /*****************************************************************************
2640 *****************************************************************************/
2641 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2643 demux_sys_t *p_sys = p_demux->p_sys;
2647 for( i = 0; i < p_sys->i_track; i++ )
2649 avi_track_t *tk = p_sys->track[i];
2650 if( tk->i_idxposc >= tk->idx.i_size )
2662 /****************************************************************************
2663 * AVI_MovieGetLength give max streams length in second
2664 ****************************************************************************/
2665 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2667 demux_sys_t *p_sys = p_demux->p_sys;
2668 mtime_t i_maxlength = 0;
2671 for( i = 0; i < p_sys->i_track; i++ )
2673 avi_track_t *tk = p_sys->track[i];
2676 /* fix length for each stream */
2677 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2682 if( tk->i_samplesize )
2684 i_length = AVI_GetDPTS( tk,
2685 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2686 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2690 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2692 i_length /= (mtime_t)1000000; /* in seconds */
2695 "stream[%d] length:%"PRId64" (based on index)",
2698 i_maxlength = __MAX( i_maxlength, i_length );