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 AVI Index") ,
659 _( "Because this AVI file is broken, seeking will not work correctly.\n"
660 "It can be fixed, but this step might take a long time.\n"
661 "What do you want to do ?" ),
662 _( "Repair and play" ), _( "Play as is" ), _( "Do not play") ) )
666 msg_Dbg( p_demux, "Fixing AVI index" );
670 vlc_object_kill( p_demux->p_parent );
676 /* fix some BeOS MediaKit generated file */
677 for( i = 0 ; i < p_sys->i_track; i++ )
679 avi_track_t *tk = p_sys->track[i];
680 avi_chunk_list_t *p_strl;
681 avi_chunk_strh_t *p_strh;
682 avi_chunk_strf_auds_t *p_auds;
684 if( tk->i_cat != AUDIO_ES )
688 if( tk->idx.i_size < 1 ||
690 tk->i_samplesize != 0 )
694 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
695 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
696 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
698 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
699 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
701 int64_t i_track_length =
702 tk->idx.p_entry[tk->idx.i_size-1].i_length +
703 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
704 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
705 (mtime_t)p_avih->i_microsecperframe;
709 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
712 tk->i_samplesize = 1;
713 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
714 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
718 if( p_sys->b_seekable )
720 /* we have read all chunk so go back to movi */
721 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
723 /* Skip movi header */
724 stream_Read( p_demux->s, NULL, 12 );
726 p_sys->i_movi_begin = p_movi->i_chunk_pos;
730 for( unsigned i = 0; i < p_sys->i_attachment; i++)
731 vlc_input_attachment_Delete(p_sys->attachment[i]);
732 free(p_sys->attachment);
735 vlc_meta_Delete( p_sys->meta );
737 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
739 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
742 /*****************************************************************************
743 * Close: frees unused data
744 *****************************************************************************/
745 static void Close ( vlc_object_t * p_this )
747 demux_t * p_demux = (demux_t *)p_this;
749 demux_sys_t *p_sys = p_demux->p_sys ;
751 for( i = 0; i < p_sys->i_track; i++ )
753 if( p_sys->track[i] )
755 if( p_sys->track[i]->p_out_muxed )
756 stream_Delete( p_sys->track[i]->p_out_muxed );
757 avi_index_Clean( &p_sys->track[i]->idx );
758 free( p_sys->track[i] );
761 free( p_sys->track );
762 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
763 vlc_meta_Delete( p_sys->meta );
764 for( unsigned i = 0; i < p_sys->i_attachment; i++)
765 vlc_input_attachment_Delete(p_sys->attachment[i]);
766 free(p_sys->attachment);
771 /*****************************************************************************
772 * Demux_Seekable: reads and demuxes data packets for stream seekable
773 *****************************************************************************
774 * AVIDemux: reads and demuxes data packets
775 *****************************************************************************
776 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
777 *****************************************************************************/
784 off_t i_posf; /* where we will read :
785 if i_idxposb == 0 : begining of chunk (+8 to acces data)
786 else : point on data directly */
787 } avi_track_toread_t;
789 static int Demux_Seekable( demux_t *p_demux )
791 demux_sys_t *p_sys = p_demux->p_sys;
793 unsigned int i_track_count = 0;
794 unsigned int i_track;
795 /* cannot be more than 100 stream (dcXX or wbXX) */
796 avi_track_toread_t toread[100];
799 /* detect new selected/unselected streams */
800 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
802 avi_track_t *tk = p_sys->track[i_track];
805 if( p_sys->b_muxed && tk->p_out_muxed )
808 tk->b_activated = true;
812 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
813 if( b && !tk->b_activated )
815 if( p_sys->b_seekable)
817 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
819 tk->b_activated = true;
821 else if( !b && tk->b_activated )
823 tk->b_activated = false;
831 if( i_track_count <= 0 )
833 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
835 p_sys->i_time += 25*1000; /* read 25ms */
838 if( p_sys->i_time >= i_length )
842 msg_Warn( p_demux, "no track selected, exiting..." );
846 /* wait for the good time */
847 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
848 p_sys->i_time += 25*1000; /* read 25ms */
851 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
853 avi_track_t *tk = p_sys->track[i_track];
856 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
857 if( tk->i_idxposc < tk->idx.i_size )
859 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
860 if( tk->i_idxposb > 0 )
862 toread[i_track].i_posf += 8 + tk->i_idxposb;
867 toread[i_track].i_posf = -1;
870 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
872 if( tk->i_samplesize )
874 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
878 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
883 toread[i_track].i_toread *= -1;
896 /* search for first chunk to be read */
897 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
899 if( !toread[i].b_ok ||
900 AVI_GetDPTS( p_sys->track[i],
901 toread[i].i_toread ) <= -25 * 1000 )
906 if( toread[i].i_toread > 0 )
908 b_done = false; /* not yet finished */
910 if( toread[i].i_posf > 0 )
912 if( i_pos == -1 || i_pos > toread[i].i_posf )
915 i_pos = toread[i].i_posf;
922 for( i = 0; i < p_sys->i_track; i++ )
927 msg_Warn( p_demux, "all tracks have failed, exiting..." );
933 int i_loop_count = 0;
935 /* no valid index, we will parse directly the stream
936 * in case we fail we will disable all finished stream */
937 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
939 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
940 if( AVI_PacketNext( p_demux ) )
942 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
947 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
954 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
957 "cannot get packet header, track disabled" );
958 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
960 if( avi_pk.i_stream >= p_sys->i_track ||
961 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
963 if( AVI_PacketNext( p_demux ) )
966 "cannot skip packet, track disabled" );
967 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
970 /* Prevents from eating all the CPU with broken files.
971 * This value should be low enough so that it doesn't
972 * affect the reading speed too much. */
973 if( !(++i_loop_count % 1024) )
975 if( !vlc_object_alive (p_demux) ) return -1;
978 if( !(i_loop_count % (1024 * 10)) )
980 "don't seem to find any data..." );
986 i_track = avi_pk.i_stream;
987 tk = p_sys->track[i_track];
989 /* add this chunk to the index */
991 index.i_id = avi_pk.i_fourcc;
992 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
993 index.i_pos = avi_pk.i_pos;
994 index.i_length = avi_pk.i_size;
995 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
997 /* do we will read this data ? */
998 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1004 if( AVI_PacketNext( p_demux ) )
1007 "cannot skip packet, track disabled" );
1008 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1017 stream_Seek( p_demux->s, i_pos );
1020 /* Set the track to use */
1021 tk = p_sys->track[i_track];
1023 /* read thoses data */
1024 if( tk->i_samplesize )
1026 unsigned int i_toread;
1028 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1030 if( tk->i_samplesize > 1 )
1032 i_toread = tk->i_samplesize;
1036 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1037 i_toread = __MAX( i_toread, 100 );
1040 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1046 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1049 if( tk->i_idxposb == 0 )
1051 i_size += 8; /* need to read and skip header */
1054 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1056 msg_Warn( p_demux, "failed reading data" );
1058 toread[i_track].b_ok = false;
1061 if( i_size % 2 ) /* read was padded on word boundary */
1063 p_frame->i_buffer--;
1066 if( tk->i_idxposb == 0 )
1068 p_frame->p_buffer += 8;
1069 p_frame->i_buffer -= 8;
1071 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1072 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1074 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1078 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1082 if( tk->i_samplesize )
1084 if( tk->i_idxposb == 0 )
1088 toread[i_track].i_toread -= i_size;
1089 tk->i_idxposb += i_size;
1090 if( tk->i_idxposb >=
1091 tk->idx.p_entry[tk->i_idxposc].i_length )
1099 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1102 if( tk->i_cat == AUDIO_ES )
1104 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1106 toread[i_track].i_toread--;
1109 if( tk->i_idxposc < tk->idx.i_size)
1111 toread[i_track].i_posf =
1112 tk->idx.p_entry[tk->i_idxposc].i_pos;
1113 if( tk->i_idxposb > 0 )
1115 toread[i_track].i_posf += 8 + tk->i_idxposb;
1121 toread[i_track].i_posf = -1;
1124 if( tk->i_cat != VIDEO_ES )
1125 p_frame->i_dts = p_frame->i_pts;
1128 p_frame->i_dts = p_frame->i_pts;
1129 p_frame->i_pts = VLC_TS_INVALID;
1132 //p_pes->i_rate = p_demux->stream.control.i_rate;
1133 if( tk->p_out_muxed )
1134 stream_DemuxSend( tk->p_out_muxed, p_frame );
1136 es_out_Send( p_demux->out, tk->p_es, p_frame );
1141 /*****************************************************************************
1142 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1143 *****************************************************************************
1144 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1145 *****************************************************************************/
1146 static int Demux_UnSeekable( demux_t *p_demux )
1148 demux_sys_t *p_sys = p_demux->p_sys;
1149 avi_track_t *p_stream_master = NULL;
1150 unsigned int i_stream;
1151 unsigned int i_packet;
1153 if( p_sys->b_muxed )
1155 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1156 return VLC_EGENERIC;
1159 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1161 /* *** find master stream for data packet skipping algo *** */
1162 /* *** -> first video, if any, or first audio ES *** */
1163 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1165 avi_track_t *tk = p_sys->track[i_stream];
1168 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1170 if( b && tk->i_cat == VIDEO_ES )
1172 p_stream_master = tk;
1176 p_stream_master = tk;
1180 if( !p_stream_master )
1182 msg_Warn( p_demux, "no more stream selected" );
1186 p_sys->i_time = AVI_GetPTS( p_stream_master );
1188 for( i_packet = 0; i_packet < 10; i_packet++)
1190 #define p_stream p_sys->track[avi_pk.i_stream]
1192 avi_packet_t avi_pk;
1194 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1199 if( avi_pk.i_stream >= p_sys->i_track ||
1200 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1202 /* we haven't found an audio or video packet:
1203 * - we have seek, found first next packet
1204 * - others packets could be found, skip them
1206 switch( avi_pk.i_fourcc )
1208 case AVIFOURCC_JUNK:
1209 case AVIFOURCC_LIST:
1210 case AVIFOURCC_RIFF:
1211 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1212 case AVIFOURCC_idx1:
1215 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1217 return( 0 ); /* eof */
1220 "seems to have lost position, resync" );
1221 if( AVI_PacketSearch( p_demux ) )
1223 msg_Err( p_demux, "resync failed" );
1230 /* check for time */
1231 if( __ABS( AVI_GetPTS( p_stream ) -
1232 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1234 /* load it and send to decoder */
1236 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1240 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1242 if( avi_pk.i_cat != VIDEO_ES )
1243 p_frame->i_dts = p_frame->i_pts;
1246 p_frame->i_dts = p_frame->i_pts;
1247 p_frame->i_pts = VLC_TS_INVALID;
1250 //p_pes->i_rate = p_demux->stream.control.i_rate;
1251 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1255 if( AVI_PacketNext( p_demux ) )
1261 /* *** update stream time position *** */
1262 if( p_stream->i_samplesize )
1264 p_stream->i_idxposb += avi_pk.i_size;
1268 if( p_stream->i_cat == AUDIO_ES )
1270 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1272 p_stream->i_idxposc++;
1282 /*****************************************************************************
1283 * Seek: goto to i_date or i_percent
1284 *****************************************************************************/
1285 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1288 demux_sys_t *p_sys = p_demux->p_sys;
1289 unsigned int i_stream;
1290 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1291 i_date / 1000000, i_percent );
1293 if( p_sys->b_seekable )
1295 if( !p_sys->i_length )
1297 avi_track_t *p_stream;
1300 /* use i_percent to create a true i_date */
1301 msg_Warn( p_demux, "seeking without index at %d%%"
1302 " only works for interleaved files", i_percent );
1303 if( i_percent >= 100 )
1305 msg_Warn( p_demux, "cannot seek so far !" );
1306 return VLC_EGENERIC;
1308 i_percent = __MAX( i_percent, 0 );
1310 /* try to find chunk that is at i_percent or the file */
1311 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1312 p_sys->i_movi_begin );
1313 /* search first selected stream (and prefer non eof ones) */
1314 for( i_stream = 0, p_stream = NULL;
1315 i_stream < p_sys->i_track; i_stream++ )
1317 if( !p_stream || p_stream->b_eof )
1318 p_stream = p_sys->track[i_stream];
1320 if( p_stream->b_activated && !p_stream->b_eof )
1323 if( !p_stream || !p_stream->b_activated )
1325 msg_Warn( p_demux, "cannot find any selected stream" );
1326 return VLC_EGENERIC;
1329 /* be sure that the index exist */
1330 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1332 msg_Warn( p_demux, "cannot seek" );
1333 return VLC_EGENERIC;
1336 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1337 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1339 /* search after i_idxposc */
1340 if( AVI_StreamChunkSet( p_demux,
1341 i_stream, p_stream->i_idxposc + 1 ) )
1343 msg_Warn( p_demux, "cannot seek" );
1344 return VLC_EGENERIC;
1348 i_date = AVI_GetPTS( p_stream );
1349 /* TODO better support for i_samplesize != 0 */
1350 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1354 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1356 avi_track_t *p_stream = p_sys->track[i_stream];
1358 if( !p_stream->b_activated )
1361 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1363 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1364 p_sys->i_time = i_date;
1365 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1370 msg_Err( p_demux, "shouldn't yet be executed" );
1371 return VLC_EGENERIC;
1375 /*****************************************************************************
1377 *****************************************************************************/
1378 static double ControlGetPosition( demux_t *p_demux )
1380 demux_sys_t *p_sys = p_demux->p_sys;
1382 if( p_sys->i_length > 0 )
1384 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1386 else if( stream_Size( p_demux->s ) > 0 )
1392 /* search the more advanced selected es */
1393 for( i = 0; i < p_sys->i_track; i++ )
1395 avi_track_t *tk = p_sys->track[i];
1396 if( tk->b_activated && tk->i_idxposc < tk->idx.i_size )
1398 i_tmp = tk->idx.p_entry[tk->i_idxposc].i_pos +
1399 tk->idx.p_entry[tk->i_idxposc].i_length + 8;
1406 return (double)i64 / stream_Size( p_demux->s );
1411 static int Control( demux_t *p_demux, int i_query, va_list args )
1413 demux_sys_t *p_sys = p_demux->p_sys;
1421 case DEMUX_GET_POSITION:
1422 pf = (double*)va_arg( args, double * );
1423 *pf = ControlGetPosition( p_demux );
1425 case DEMUX_SET_POSITION:
1426 f = (double)va_arg( args, double );
1427 if( p_sys->b_seekable )
1429 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1430 return Seek( p_demux, i64, (int)(f * 100) );
1434 int64_t i_pos = stream_Size( p_demux->s ) * f;
1435 return stream_Seek( p_demux->s, i_pos );
1438 case DEMUX_GET_TIME:
1439 pi64 = (int64_t*)va_arg( args, int64_t * );
1440 *pi64 = p_sys->i_time;
1443 case DEMUX_SET_TIME:
1447 i64 = (int64_t)va_arg( args, int64_t );
1448 if( p_sys->i_length > 0 )
1450 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1452 else if( p_sys->i_time > 0 )
1454 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1455 (double)i64 / (double)p_sys->i_time );
1457 return Seek( p_demux, i64, i_percent );
1459 case DEMUX_GET_LENGTH:
1460 pi64 = (int64_t*)va_arg( args, int64_t * );
1461 *pi64 = p_sys->i_length * (mtime_t)1000000;
1465 pf = (double*)va_arg( args, double * );
1467 for( i = 0; i < (int)p_sys->i_track; i++ )
1469 avi_track_t *tk = p_sys->track[i];
1470 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1472 *pf = (float)tk->i_rate / (float)tk->i_scale;
1478 case DEMUX_GET_META:
1479 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1480 vlc_meta_Merge( p_meta, p_sys->meta );
1483 case DEMUX_GET_ATTACHMENTS:
1485 if( p_sys->i_attachment <= 0 )
1486 return VLC_EGENERIC;
1488 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1489 int *pi_int = va_arg( args, int * );
1491 *pi_int = p_sys->i_attachment;
1492 *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
1493 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1494 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1499 return VLC_EGENERIC;
1503 /*****************************************************************************
1504 * Function to convert pts to chunk or byte
1505 *****************************************************************************/
1507 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1512 return (mtime_t)((int64_t)i_pts *
1513 (int64_t)tk->i_rate /
1514 (int64_t)tk->i_scale /
1517 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1519 if( !tk->i_scale || !tk->i_samplesize )
1522 return (mtime_t)((int64_t)i_pts *
1523 (int64_t)tk->i_rate /
1524 (int64_t)tk->i_scale /
1526 (int64_t)tk->i_samplesize );
1529 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1536 i_dpts = (mtime_t)( (int64_t)1000000 *
1538 (int64_t)tk->i_scale /
1539 (int64_t)tk->i_rate );
1541 if( tk->i_samplesize )
1543 return i_dpts / tk->i_samplesize;
1548 static mtime_t AVI_GetPTS( avi_track_t *tk )
1550 if( tk->i_samplesize )
1552 int64_t i_count = 0;
1554 /* we need a valid entry we will emulate one */
1555 if( tk->i_idxposc == tk->idx.i_size )
1559 /* use the last entry */
1560 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1561 + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1566 i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1568 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1572 if( tk->i_cat == AUDIO_ES )
1574 return AVI_GetDPTS( tk, tk->i_blockno );
1578 return AVI_GetDPTS( tk, tk->i_idxposc );
1583 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1585 demux_sys_t *p_sys = p_demux->p_sys;
1586 avi_packet_t avi_pk;
1587 int i_loop_count = 0;
1589 /* find first chunk of i_stream that isn't in index */
1591 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1593 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1594 if( AVI_PacketNext( p_demux ) )
1596 return VLC_EGENERIC;
1601 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1606 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1608 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1610 msg_Warn( p_demux, "cannot get packet header" );
1611 return VLC_EGENERIC;
1613 if( avi_pk.i_stream >= p_sys->i_track ||
1614 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1616 if( AVI_PacketNext( p_demux ) )
1618 return VLC_EGENERIC;
1621 /* Prevents from eating all the CPU with broken files.
1622 * This value should be low enough so that it doesn't
1623 * affect the reading speed too much. */
1624 if( !(++i_loop_count % 1024) )
1626 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1629 if( !(i_loop_count % (1024 * 10)) )
1630 msg_Warn( p_demux, "don't seem to find any data..." );
1635 avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1637 /* add this chunk to the index */
1639 index.i_id = avi_pk.i_fourcc;
1640 index.i_flags = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1641 index.i_pos = avi_pk.i_pos;
1642 index.i_length = avi_pk.i_size;
1643 avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1645 if( avi_pk.i_stream == i_stream )
1650 if( AVI_PacketNext( p_demux ) )
1652 return VLC_EGENERIC;
1658 /* be sure that i_ck will be a valid index entry */
1659 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1662 demux_sys_t *p_sys = p_demux->p_sys;
1663 avi_track_t *p_stream = p_sys->track[i_stream];
1665 p_stream->i_idxposc = i_ck;
1666 p_stream->i_idxposb = 0;
1668 if( i_ck >= p_stream->idx.i_size )
1670 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1673 p_stream->i_idxposc++;
1674 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1676 return VLC_EGENERIC;
1679 } while( p_stream->i_idxposc < i_ck );
1685 /* XXX FIXME up to now, we assume that all chunk are one after one */
1686 static int AVI_StreamBytesSet( demux_t *p_demux,
1687 unsigned int i_stream,
1690 demux_sys_t *p_sys = p_demux->p_sys;
1691 avi_track_t *p_stream = p_sys->track[i_stream];
1693 if( ( p_stream->idx.i_size > 0 )
1694 &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1695 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1697 /* index is valid to find the ck */
1698 /* uses dichototmie to be fast enougth */
1699 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1700 int i_idxmax = p_stream->idx.i_size;
1704 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1706 i_idxmax = i_idxposc ;
1707 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1711 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1712 p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1714 i_idxmin = i_idxposc ;
1715 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1719 p_stream->i_idxposc = i_idxposc;
1720 p_stream->i_idxposb = i_byte -
1721 p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1730 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1731 p_stream->i_idxposb = 0;
1734 p_stream->i_idxposc++;
1735 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1737 return VLC_EGENERIC;
1740 } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1741 p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1743 p_stream->i_idxposb = i_byte -
1744 p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1749 static int AVI_TrackSeek( demux_t *p_demux,
1753 demux_sys_t *p_sys = p_demux->p_sys;
1754 avi_track_t *tk = p_sys->track[i_stream];
1756 #define p_stream p_sys->track[i_stream]
1759 i_oldpts = AVI_GetPTS( p_stream );
1761 if( !p_stream->i_samplesize )
1763 if( AVI_StreamChunkSet( p_demux,
1765 AVI_PTSToChunk( p_stream, i_date ) ) )
1767 return VLC_EGENERIC;
1770 if( p_stream->i_cat == AUDIO_ES )
1774 for( i = 0; i < tk->i_idxposc; i++ )
1776 if( tk->i_blocksize > 0 )
1778 tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1788 "old:%"PRId64" %s new %"PRId64,
1790 i_oldpts > i_date ? ">" : "<",
1793 if( p_stream->i_cat == VIDEO_ES )
1795 /* search key frame */
1796 //if( i_date < i_oldpts || 1 )
1798 while( p_stream->i_idxposc > 0 &&
1799 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1802 if( AVI_StreamChunkSet( p_demux,
1804 p_stream->i_idxposc - 1 ) )
1806 return VLC_EGENERIC;
1813 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1814 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1817 if( AVI_StreamChunkSet( p_demux,
1819 p_stream->i_idxposc + 1 ) )
1821 return VLC_EGENERIC;
1830 if( AVI_StreamBytesSet( p_demux,
1832 AVI_PTSToByte( p_stream, i_date ) ) )
1834 return VLC_EGENERIC;
1841 /****************************************************************************
1842 * Return true if it's a key frame
1843 ****************************************************************************/
1844 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1848 case VLC_CODEC_DIV1:
1850 * startcode: 0x00000100 32bits
1851 * framenumber ? 5bits
1852 * piture type 0(I),1(P) 2bits
1854 if( GetDWBE( p_byte ) != 0x00000100 )
1856 /* it's not an msmpegv1 stream, strange...*/
1857 return AVIIF_KEYFRAME;
1859 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1861 case VLC_CODEC_DIV2:
1862 case VLC_CODEC_DIV3:
1863 case VLC_CODEC_WMV1:
1865 * picture type 0(I),1(P) 2bits
1867 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1868 case VLC_CODEC_MP4V:
1869 /* we should find first occurrence of 0x000001b6 (32bits)
1870 * startcode: 0x000001b6 32bits
1871 * piture type 0(I),1(P) 2bits
1873 if( GetDWBE( p_byte ) != 0x000001b6 )
1875 /* not true , need to find the first VOP header */
1876 return AVIIF_KEYFRAME;
1878 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1881 /* I can't do it, so say yes */
1882 return AVIIF_KEYFRAME;
1886 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1891 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1894 return vlc_fourcc_GetCodec( i_cat, i_codec );
1896 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1900 /****************************************************************************
1902 ****************************************************************************/
1903 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1904 unsigned int *pi_number, unsigned int *pi_type )
1906 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1909 c1 = ((uint8_t *)&i_id)[0];
1910 c2 = ((uint8_t *)&i_id)[1];
1912 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1914 SET_PTR( pi_number, 100 ); /* > max stream number */
1915 SET_PTR( pi_type, UNKNOWN_ES );
1919 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1920 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1923 SET_PTR( pi_type, AUDIO_ES );
1928 SET_PTR( pi_type, VIDEO_ES );
1932 SET_PTR( pi_type, SPU_ES );
1935 SET_PTR( pi_type, UNKNOWN_ES );
1942 /****************************************************************************
1944 ****************************************************************************/
1945 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1947 const uint8_t *p_peek;
1949 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1951 return VLC_EGENERIC;
1953 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1954 p_pk->i_size = GetDWLE( p_peek + 4 );
1955 p_pk->i_pos = stream_Tell( p_demux->s );
1956 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1958 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1959 p_peek[10], p_peek[11] );
1966 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1968 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1972 static int AVI_PacketNext( demux_t *p_demux )
1974 avi_packet_t avi_ck;
1977 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1979 return VLC_EGENERIC;
1982 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1983 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1987 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1988 avi_ck.i_type == AVIFOURCC_AVIX )
1994 i_skip = __EVEN( avi_ck.i_size ) + 8;
1997 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1999 return VLC_EGENERIC;
2004 static int AVI_PacketRead( demux_t *p_demux,
2006 block_t **pp_frame )
2010 i_size = __EVEN( p_pk->i_size + 8 );
2012 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2014 return VLC_EGENERIC;
2016 (*pp_frame)->p_buffer += 8;
2017 (*pp_frame)->i_buffer -= 8;
2019 if( i_size != p_pk->i_size + 8 )
2021 (*pp_frame)->i_buffer--;
2027 static int AVI_PacketSearch( demux_t *p_demux )
2029 demux_sys_t *p_sys = p_demux->p_sys;
2030 avi_packet_t avi_pk;
2035 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2037 return VLC_EGENERIC;
2039 AVI_PacketGetHeader( p_demux, &avi_pk );
2040 if( avi_pk.i_stream < p_sys->i_track &&
2041 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2045 switch( avi_pk.i_fourcc )
2047 case AVIFOURCC_JUNK:
2048 case AVIFOURCC_LIST:
2049 case AVIFOURCC_RIFF:
2050 case AVIFOURCC_idx1:
2054 /* Prevents from eating all the CPU with broken files.
2055 * This value should be low enough so that it doesn't affect the
2056 * reading speed too much (not that we care much anyway because
2057 * this code is called only on broken files). */
2058 if( !(++i_count % 1024) )
2060 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2063 if( !(i_count % (1024 * 10)) )
2064 msg_Warn( p_demux, "trying to resync..." );
2069 /****************************************************************************
2071 ****************************************************************************/
2072 static void avi_index_Init( avi_index_t *p_index )
2074 p_index->i_size = 0;
2076 p_index->p_entry = NULL;
2078 static void avi_index_Clean( avi_index_t *p_index )
2080 free( p_index->p_entry );
2082 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2083 avi_entry_t *p_entry )
2085 /* Update last chunk position */
2086 if( *pi_last_pos < p_entry->i_pos )
2087 *pi_last_pos = p_entry->i_pos;
2090 if( p_index->i_size >= p_index->i_max )
2092 p_index->i_max += 16384;
2093 p_index->p_entry = realloc_or_free( p_index->p_entry,
2094 p_index->i_max * sizeof( *p_index->p_entry ) );
2095 if( !p_index->p_entry )
2098 /* calculate cumulate length */
2099 if( p_index->i_size > 0 )
2101 p_entry->i_lengthtotal =
2102 p_index->p_entry[p_index->i_size - 1].i_length +
2103 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2107 p_entry->i_lengthtotal = 0;
2110 p_index->p_entry[p_index->i_size++] = *p_entry;
2113 static int AVI_IndexFind_idx1( demux_t *p_demux,
2114 avi_chunk_idx1_t **pp_idx1,
2115 uint64_t *pi_offset )
2117 demux_sys_t *p_sys = p_demux->p_sys;
2119 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2120 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2124 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2125 return VLC_EGENERIC;
2129 /* *** calculate offset *** */
2130 /* Well, avi is __SHIT__ so test more than one entry
2131 * (needed for some avi files) */
2132 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2134 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2136 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2138 *pi_offset = p_movi->i_chunk_pos + 8;
2145 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2146 avi_index_t p_index[], off_t *pi_last_offset )
2148 demux_sys_t *p_sys = p_demux->p_sys;
2150 avi_chunk_idx1_t *p_idx1;
2152 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2153 return VLC_EGENERIC;
2155 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2160 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2163 if( i_stream < p_sys->i_track &&
2164 i_cat == p_sys->track[i_stream]->i_cat )
2167 index.i_id = p_idx1->entry[i_index].i_fourcc;
2168 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2169 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2170 index.i_length = p_idx1->entry[i_index].i_length;
2172 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2178 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2179 avi_chunk_indx_t *p_indx )
2183 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2184 if( p_indx->i_indexsubtype == 0 )
2186 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2188 index.i_id = p_indx->i_id;
2189 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2190 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2191 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2193 avi_index_Append( p_index, pi_max_offset, &index );
2196 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2198 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2200 index.i_id = p_indx->i_id;
2201 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2202 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2203 index.i_length = p_indx->idx.field[i].i_size;
2205 avi_index_Append( p_index, pi_max_offset, &index );
2210 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2214 static void AVI_IndexLoad_indx( demux_t *p_demux,
2215 avi_index_t p_index[], off_t *pi_last_offset )
2217 demux_sys_t *p_sys = p_demux->p_sys;
2219 avi_chunk_list_t *p_riff;
2220 avi_chunk_list_t *p_hdrl;
2222 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2223 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2225 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2227 avi_chunk_list_t *p_strl;
2228 avi_chunk_indx_t *p_indx;
2230 #define p_stream p_sys->track[i_stream]
2231 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2232 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2237 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2242 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2244 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2246 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2249 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2251 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2252 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2256 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2257 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2258 AVI_ChunkFree( p_demux->s, &ck_sub );
2263 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2269 static void AVI_IndexLoad( demux_t *p_demux )
2271 demux_sys_t *p_sys = p_demux->p_sys;
2274 assert( p_sys->i_track <= 100 );
2275 avi_index_t p_idx_indx[p_sys->i_track];
2276 avi_index_t p_idx_idx1[p_sys->i_track];
2277 for( unsigned i = 0; i < p_sys->i_track; i++ )
2279 avi_index_Init( &p_idx_indx[i] );
2280 avi_index_Init( &p_idx_idx1[i] );
2282 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2283 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2285 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2286 if( !p_sys->b_odml )
2287 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2289 /* Select the longest index */
2290 for( unsigned i = 0; i < p_sys->i_track; i++ )
2292 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2294 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2295 p_sys->track[i]->idx = p_idx_indx[i];
2296 avi_index_Clean( &p_idx_idx1[i] );
2300 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2301 p_sys->track[i]->idx = p_idx_idx1[i];
2302 avi_index_Clean( &p_idx_indx[i] );
2305 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2307 for( unsigned i = 0; i < p_sys->i_track; i++ )
2309 avi_index_t *p_index = &p_sys->track[i]->idx;
2313 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2314 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2317 msg_Err( p_demux, "no key frame set for track %u", i );
2318 for( unsigned j = 0; j < p_index->i_size; j++ )
2319 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2323 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2324 i, p_index->i_size );
2328 static void AVI_IndexCreate( demux_t *p_demux )
2330 demux_sys_t *p_sys = p_demux->p_sys;
2332 avi_chunk_list_t *p_riff;
2333 avi_chunk_list_t *p_movi;
2335 unsigned int i_stream;
2338 mtime_t i_dialog_update;
2339 dialog_progress_bar_t *p_dialog = NULL;
2341 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2342 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2346 msg_Err( p_demux, "cannot find p_movi" );
2350 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2351 avi_index_Init( &p_sys->track[i_stream]->idx );
2353 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2354 stream_Size( p_demux->s ) );
2356 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2357 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2360 /* Only show dialog if AVI is > 10MB */
2361 i_dialog_update = mdate();
2362 if( stream_Size( p_demux->s ) > 10000000 )
2363 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2364 NULL, _("Cancel") );
2370 if( !vlc_object_alive (p_demux) )
2373 /* Don't update/check dialog too often */
2374 if( p_dialog && mdate() - i_dialog_update > 100000 )
2376 if( dialog_ProgressCancelled( p_dialog ) )
2379 double f_current = stream_Tell( p_demux->s );
2380 double f_size = stream_Size( p_demux->s );
2381 double f_pos = f_current / f_size;
2382 dialog_ProgressSet( p_dialog, NULL, f_pos );
2384 i_dialog_update = mdate();
2387 if( AVI_PacketGetHeader( p_demux, &pk ) )
2390 if( pk.i_stream < p_sys->i_track &&
2391 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2393 avi_track_t *tk = p_sys->track[pk.i_stream];
2396 index.i_id = pk.i_fourcc;
2397 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2398 index.i_pos = pk.i_pos;
2399 index.i_length = pk.i_size;
2400 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2404 switch( pk.i_fourcc )
2406 case AVIFOURCC_idx1:
2409 avi_chunk_list_t *p_sysx;
2410 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2411 AVIFOURCC_RIFF, 1 );
2413 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2414 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2420 case AVIFOURCC_RIFF:
2421 msg_Dbg( p_demux, "new RIFF chunk found" );
2425 case AVIFOURCC_JUNK:
2429 msg_Warn( p_demux, "need resync, probably broken avi" );
2430 if( AVI_PacketSearch( p_demux ) )
2432 msg_Warn( p_demux, "lost sync, abord index creation" );
2438 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2439 AVI_PacketNext( p_demux ) )
2446 if( p_dialog != NULL )
2447 dialog_ProgressDestroy( p_dialog );
2449 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2451 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2452 i_stream, p_sys->track[i_stream]->idx.i_size );
2457 static void AVI_MetaLoad( demux_t *p_demux,
2458 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2460 demux_sys_t *p_sys = p_demux->p_sys;
2462 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2467 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2468 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2469 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2470 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2471 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2472 vlc_meta_SetSetting( p_meta, buffer );
2474 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2478 static const struct {
2482 { AVIFOURCC_IART, vlc_meta_Artist },
2483 { AVIFOURCC_ICMT, vlc_meta_Description },
2484 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2485 { AVIFOURCC_IGNR, vlc_meta_Genre },
2486 { AVIFOURCC_INAM, vlc_meta_Title },
2489 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2491 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2494 char *psz_value = FromLatin1( p_strz->p_str );
2499 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2504 /*****************************************************************************
2506 *****************************************************************************/
2507 static void AVI_ExtractSubtitle( demux_t *p_demux,
2509 avi_chunk_list_t *p_strl,
2510 avi_chunk_STRING_t *p_strn )
2512 demux_sys_t *p_sys = p_demux->p_sys;
2513 block_t *p_block = NULL;
2514 input_attachment_t *p_attachment = NULL;
2515 char *psz_description = NULL;
2516 avi_chunk_indx_t *p_indx = NULL;
2518 if( !p_sys->b_seekable )
2521 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2527 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2528 p_indx->i_entriesinuse > 0 )
2530 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2531 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2536 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2537 p_indx->i_entriesinuse != 1 ||
2538 p_indx->i_indexsubtype != 0 )
2541 i_position = p_indx->i_baseoffset +
2542 p_indx->idx.std[0].i_offset - 8;
2543 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2547 avi_chunk_idx1_t *p_idx1;
2550 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2554 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2556 const idx1_entry_t *e = &p_idx1->entry[i];
2558 unsigned i_stream_idx;
2560 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2561 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2563 i_position = e->i_pos + i_offset;
2564 i_size = e->i_length + 8;
2573 if( i_size > 1000000 )
2576 if( stream_Seek( p_demux->s, i_position ) )
2578 p_block = stream_Block( p_demux->s, i_size );
2582 /* Parse packet header */
2583 const uint8_t *p = p_block->p_buffer;
2584 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2589 /* Parse subtitle chunk header */
2590 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2591 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2593 const unsigned i_name = GetDWLE( &p[7] );
2594 if( 11 + i_size <= i_name )
2597 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2599 i_size -= 11 + i_name;
2600 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2602 const unsigned i_payload = GetDWLE( &p[2] );
2603 if( i_size < 6 + i_payload || i_payload <= 0 )
2608 if( !psz_description )
2609 psz_description = p_strn ? FromLatin1( p_strn->p_str ) : NULL;
2611 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2613 p_attachment = vlc_input_attachment_New( psz_name,
2614 "application/x-srt",
2618 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2622 free( psz_description );
2625 block_Release( p_block );
2628 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2630 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2632 if( p_indx == &ck.indx )
2633 AVI_ChunkFree( p_demux->s, &ck );
2635 /*****************************************************************************
2637 *****************************************************************************/
2638 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2640 demux_sys_t *p_sys = p_demux->p_sys;
2644 for( i = 0; i < p_sys->i_track; i++ )
2646 avi_track_t *tk = p_sys->track[i];
2647 if( tk->i_idxposc >= tk->idx.i_size )
2659 /****************************************************************************
2660 * AVI_MovieGetLength give max streams length in second
2661 ****************************************************************************/
2662 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2664 demux_sys_t *p_sys = p_demux->p_sys;
2665 mtime_t i_maxlength = 0;
2668 for( i = 0; i < p_sys->i_track; i++ )
2670 avi_track_t *tk = p_sys->track[i];
2673 /* fix length for each stream */
2674 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2679 if( tk->i_samplesize )
2681 i_length = AVI_GetDPTS( tk,
2682 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2683 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2687 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2689 i_length /= (mtime_t)1000000; /* in seconds */
2692 "stream[%d] length:%"PRId64" (based on index)",
2695 i_maxlength = __MAX( i_maxlength, i_length );