1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_input.h>
36 #include <vlc_dialog.h>
39 #include <vlc_codecs.h>
40 #include <vlc_charset.h>
41 #include <vlc_memory.h>
45 /*****************************************************************************
47 *****************************************************************************/
49 #define INTERLEAVE_TEXT N_("Force interleaved method" )
50 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
52 #define INDEX_TEXT N_("Force index creation")
53 #define INDEX_LONGTEXT N_( \
54 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
55 "or incomplete (not seekable)." )
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 static const int pi_index[] = {0,1,2};
62 static const char *const ppsz_indexes[] = { N_("Ask for action"),
67 set_shortname( "AVI" )
68 set_description( N_("AVI demuxer") )
69 set_capability( "demux", 212 )
70 set_category( CAT_INPUT )
71 set_subcategory( SUBCAT_INPUT_DEMUX )
73 add_bool( "avi-interleaved", false, NULL,
74 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
75 add_integer( "avi-index", 0, NULL,
76 INDEX_TEXT, INDEX_LONGTEXT, false )
77 change_integer_list( pi_index, ppsz_indexes, NULL )
79 set_callbacks( Open, Close )
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Control ( demux_t *, int, va_list );
86 static int Seek ( demux_t *, mtime_t, int );
87 static int Demux_Seekable ( demux_t * );
88 static int Demux_UnSeekable( demux_t * );
90 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
94 vlc_fourcc_t i_fourcc;
97 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
99 uint8_t i_peek[8]; /* first 8 bytes */
101 unsigned int i_stream;
112 int64_t i_lengthtotal;
121 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
122 vlc_fourcc_t i_codec;
126 unsigned int i_samplesize;
131 avi_entry_t *p_index;
132 unsigned int i_idxnb;
133 unsigned int i_idxmax;
135 unsigned int i_idxposc; /* numero of chunk */
136 unsigned int i_idxposb; /* byte in the current chunk */
138 /* For VBR audio only */
139 unsigned int i_blockno;
140 unsigned int i_blocksize;
142 /* For muxed streams */
143 stream_t *p_out_muxed;
158 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
160 /* number of streams and information */
161 unsigned int i_track;
167 unsigned int i_attachment;
168 input_attachment_t **attachment;
171 static inline off_t __EVEN( off_t i )
173 return (i & 1) ? i + 1 : i;
176 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
177 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
178 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
179 static mtime_t AVI_GetPTS ( avi_track_t * );
182 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
183 static int AVI_StreamChunkSet ( demux_t *,
184 unsigned int i_stream, unsigned int i_ck );
185 static int AVI_StreamBytesSet ( demux_t *,
186 unsigned int i_stream, off_t i_byte );
188 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
189 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
191 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
192 static int AVI_PacketNext ( demux_t * );
193 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
194 static int AVI_PacketSearch ( demux_t * );
196 static void AVI_IndexLoad ( demux_t * );
197 static void AVI_IndexCreate ( demux_t * );
198 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
200 static void AVI_ExtractSubtitle( demux_t *, int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
202 static mtime_t AVI_MovieGetLength( demux_t * );
204 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
206 /*****************************************************************************
208 *****************************************************************************/
209 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
210 static int AVI_TrackStopFinishedStreams( demux_t *);
213 - For VBR mp3 stream:
214 count blocks by rounded-up chunksizes instead of chunks
215 we need full emulation of dshow avi demuxer bugs :(
216 fixes silly nandub-style a-v delaying in avi with vbr mp3...
217 (from mplayer 2002/08/02)
221 /*****************************************************************************
222 * Open: check file and initializes AVI structures
223 *****************************************************************************/
224 static int Open( vlc_object_t * p_this )
226 demux_t *p_demux = (demux_t *)p_this;
229 bool b_index = false;
232 avi_chunk_list_t *p_riff;
233 avi_chunk_list_t *p_hdrl, *p_movi;
234 avi_chunk_avih_t *p_avih;
236 unsigned int i_track;
237 unsigned int i, i_peeker;
239 const uint8_t *p_peek;
241 /* Is it an avi file ? */
242 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
244 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
246 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
248 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
252 if( i_peeker == 188 )
257 /* Initialize input structures. */
258 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
259 memset( p_sys, 0, sizeof( demux_sys_t ) );
262 p_sys->i_movi_lastchunk_pos = 0;
263 p_sys->b_odml = false;
264 p_sys->b_muxed = false;
268 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
270 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
272 p_demux->pf_control = Control;
273 p_demux->pf_demux = Demux_Seekable;
275 /* For unseekable stream, automaticaly use Demux_UnSeekable */
276 if( !p_sys->b_seekable
277 || var_InheritInteger( p_demux, "avi-interleaved" ) )
279 p_demux->pf_demux = Demux_UnSeekable;
284 stream_Read( p_demux->s, NULL, i_peeker );
287 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
289 msg_Err( p_demux, "avi module discarded (invalid file)" );
294 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
296 unsigned int i_count =
297 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
299 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
300 for( i = 1; i < i_count; i++ )
302 avi_chunk_list_t *p_sysx;
304 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
305 if( p_sysx->i_type == AVIFOURCC_AVIX )
307 msg_Warn( p_demux, "detected OpenDML file" );
308 p_sys->b_odml = true;
314 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
315 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
316 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
318 if( !p_hdrl || !p_movi )
320 msg_Err( p_demux, "avi module discarded (invalid file)" );
324 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
326 msg_Err( p_demux, "cannot find avih chunk" );
329 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
330 if( p_avih->i_streams != i_track )
333 "found %d stream but %d are declared",
334 i_track, p_avih->i_streams );
338 msg_Err( p_demux, "no stream defined!" );
342 /* print information on streams */
343 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
345 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
346 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
347 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
348 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
350 AVI_MetaLoad( p_demux, p_riff, p_avih );
352 /* now read info on each stream and create ES */
353 for( i = 0 ; i < i_track; i++ )
355 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
359 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
360 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
361 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
362 avi_chunk_strf_auds_t *p_auds = NULL;
363 avi_chunk_strf_vids_t *p_vids = NULL;
366 memset( tk, 0, sizeof(*tk) );
368 tk->b_activated = true;
370 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
371 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
373 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
375 msg_Warn( p_demux, "stream[%d] incomplete", i );
380 tk->i_rate = p_strh->i_rate;
381 tk->i_scale = p_strh->i_scale;
382 tk->i_samplesize = p_strh->i_samplesize;
383 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
384 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
386 switch( p_strh->i_type )
388 case( AVIFOURCC_auds ):
389 tk->i_cat = AUDIO_ES;
390 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
391 p_auds->p_wf->wFormatTag );
393 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
394 if( tk->i_blocksize == 0 )
396 if( p_auds->p_wf->wFormatTag == 1 )
397 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
401 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
403 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
404 "Using blocksize as a workaround.",
405 i, tk->i_samplesize, tk->i_blocksize );
406 tk->i_samplesize = tk->i_blocksize;
409 if( tk->i_codec == VLC_CODEC_VORBIS )
411 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
414 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
416 fmt.audio.i_channels = p_auds->p_wf->nChannels;
417 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
418 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
419 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
420 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
421 fmt.b_packetized = !tk->i_blocksize;
424 "stream[%d] audio(0x%x) %d channels %dHz %dbits",
425 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
426 p_auds->p_wf->nSamplesPerSec,
427 p_auds->p_wf->wBitsPerSample );
429 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
430 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
431 fmt.p_extra = malloc( fmt.i_extra );
432 if( !fmt.p_extra ) goto error;
433 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
435 /* Rewrite the vorbis headers from Xiph-like format
436 * to VLC internal format
439 * - 1st byte == N, is the number of packets - 1
440 * - Following bytes are the size of the N first packets:
441 * while( *p == 0xFF ) { size += 0xFF; p++ } size += *p;
442 * (the size of the last packet is the size of remaining
443 * data in the buffer)
444 * - Finally, all the packets concatenated
447 * - Size of the packet on 16 bits (big endian) FIXME: should be 32 bits to be safe
448 * - The packet itself
449 * - Size of the next packet, and so on ...
452 if( tk->i_codec == VLC_CODEC_VORBIS )
454 uint8_t *p_extra = fmt.p_extra;
455 size_t i_extra = fmt.i_extra;
457 if( i_extra <= 1 ) break;
458 if( *p_extra++ != 2 ) break; /* 3 packets - 1 = 2 */
461 size_t i_identifier_len = 0;
462 while( *p_extra == 0xFF )
464 i_identifier_len += 0xFF;
466 if( --i_extra <= 1 ) break;
468 i_identifier_len += *p_extra++;
469 if( i_identifier_len > --i_extra ) break;
471 size_t i_comment_len = 0;
472 while( *p_extra == 0xFF )
474 i_comment_len += 0xFF;
476 if( --i_extra <= 1 ) break;
478 i_comment_len += *p_extra++;
479 if( i_comment_len > --i_extra ) break;
480 size_t i_cookbook_len = i_extra;
482 size_t i_headers_size = 3 * 2 + i_identifier_len +
483 i_comment_len + i_cookbook_len;
484 uint8_t *p_out = malloc( i_headers_size );
485 if( !p_out ) goto error;
488 fmt.i_extra = i_headers_size;
489 #define copy_packet( len ) \
490 *p_out++ = len >> 8; \
491 *p_out++ = len & 0xFF; \
492 memcpy( p_out, p_extra, len ); \
495 copy_packet( i_identifier_len );
496 copy_packet( i_comment_len );
497 copy_packet( i_cookbook_len );
503 case( AVIFOURCC_vids ):
504 tk->i_cat = VIDEO_ES;
505 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
506 p_vids->p_bih->biCompression );
507 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
509 msg_Dbg( p_demux, "stream[%d] subtitles", i );
510 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
513 else if( p_vids->p_bih->biCompression == 0x00 )
515 switch( p_vids->p_bih->biBitCount )
518 tk->i_codec = VLC_CODEC_RGB32;
521 tk->i_codec = VLC_CODEC_RGB24;
523 case 16: /* Yes it is RV15 */
525 tk->i_codec = VLC_CODEC_RGB15;
527 case 9: /* <- TODO check that */
528 tk->i_codec = VLC_CODEC_I410;
530 case 8: /* <- TODO check that */
531 tk->i_codec = VLC_CODEC_GREY;
534 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
536 switch( tk->i_codec )
538 case VLC_CODEC_RGB24:
539 case VLC_CODEC_RGB32:
540 fmt.video.i_rmask = 0x00ff0000;
541 fmt.video.i_gmask = 0x0000ff00;
542 fmt.video.i_bmask = 0x000000ff;
544 case VLC_CODEC_RGB15:
545 fmt.video.i_rmask = 0x7c00;
546 fmt.video.i_gmask = 0x03e0;
547 fmt.video.i_bmask = 0x001f;
555 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
556 if( tk->i_codec == VLC_CODEC_MP4V &&
557 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
560 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
563 tk->i_samplesize = 0;
564 fmt.video.i_width = p_vids->p_bih->biWidth;
565 fmt.video.i_height = p_vids->p_bih->biHeight;
566 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
567 fmt.video.i_frame_rate = tk->i_rate;
568 fmt.video.i_frame_rate_base = tk->i_scale;
570 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
571 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
572 fmt.p_extra = &p_vids->p_bih[1];
573 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
574 i, (char*)&p_vids->p_bih->biCompression,
575 (uint32_t)p_vids->p_bih->biWidth,
576 (uint32_t)p_vids->p_bih->biHeight,
577 p_vids->p_bih->biBitCount,
578 (float)tk->i_rate/(float)tk->i_scale );
580 if( p_vids->p_bih->biCompression == 0x00 )
582 /* RGB DIB are coded from bottom to top */
584 (unsigned int)(-(int)p_vids->p_bih->biHeight);
587 /* Extract palette from extradata if bpp <= 8
588 * (assumes that extradata contains only palette but appears
589 * to be true for all palettized codecs we support) */
590 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
592 /* The palette is not always included in biSize */
593 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
594 if( fmt.i_extra > 0 )
596 const uint8_t *p_pal = fmt.p_extra;
598 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
599 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
601 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
603 for( int j = 0; j < 4; j++ )
604 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
610 case( AVIFOURCC_txts):
611 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
612 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
616 case( AVIFOURCC_iavs):
617 case( AVIFOURCC_ivas):
618 p_sys->b_muxed = true;
619 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
620 if( p_strh->i_handler == FOURCC_dvsd ||
621 p_strh->i_handler == FOURCC_dvhd ||
622 p_strh->i_handler == FOURCC_dvsl ||
623 p_strh->i_handler == FOURCC_dv25 ||
624 p_strh->i_handler == FOURCC_dv50 )
626 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
627 if( !tk->p_out_muxed )
628 msg_Err( p_demux, "could not load the DV parser" );
634 case( AVIFOURCC_mids):
635 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
638 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
643 fmt.psz_description = FromLatin1( p_strn->p_str );
644 if( tk->p_out_muxed == NULL )
645 tk->p_es = es_out_Add( p_demux->out, &fmt );
646 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
647 es_format_Clean( &fmt );
650 if( p_sys->i_track <= 0 )
652 msg_Err( p_demux, "no valid track" );
656 i_do_index = var_InheritInteger( p_demux, "avi-index" );
657 if( i_do_index == 1 ) /* Always fix */
660 if( p_sys->b_seekable )
662 AVI_IndexCreate( p_demux );
666 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
667 AVI_IndexLoad( p_demux );
672 AVI_IndexLoad( p_demux );
675 /* *** movie length in sec *** */
676 p_sys->i_length = AVI_MovieGetLength( p_demux );
678 /* Check the index completeness */
679 unsigned int i_idx_totalframes = 0;
680 for( unsigned int i = 0; i < p_sys->i_track; i++ )
682 const avi_track_t *tk = p_sys->track[i];
683 if( tk->i_cat == VIDEO_ES && tk->p_index )
684 i_idx_totalframes = __MAX(i_idx_totalframes, tk->i_idxnb);
687 if( i_idx_totalframes != p_avih->i_totalframes &&
688 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
689 (mtime_t)p_avih->i_microsecperframe /
692 if( !vlc_object_alive( p_demux) )
695 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
696 "approximative or will exhibit strange behavior" );
697 if( i_do_index == 0 && !b_index )
699 if( !p_sys->b_seekable ) {
703 switch( dialog_Question( p_demux, _("AVI Index") ,
704 _( "This AVI file is broken. Seeking will not work correctly.\n"
705 "Do you want to try to fix it?\n\n"
706 "This might take a long time." ),
707 _( "Repair" ), _( "Don't repair" ), _( "Cancel") ) )
711 msg_Dbg( p_demux, "Fixing AVI index" );
715 vlc_object_kill( p_demux->p_parent );
721 /* fix some BeOS MediaKit generated file */
722 for( i = 0 ; i < p_sys->i_track; i++ )
724 avi_track_t *tk = p_sys->track[i];
725 avi_chunk_list_t *p_strl;
726 avi_chunk_strh_t *p_strh;
727 avi_chunk_strf_auds_t *p_auds;
729 if( tk->i_cat != AUDIO_ES )
733 if( tk->i_idxnb < 1 ||
735 tk->i_samplesize != 0 )
739 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
740 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
741 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
743 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
744 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
746 int64_t i_track_length =
747 tk->p_index[tk->i_idxnb-1].i_length +
748 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
749 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
750 (mtime_t)p_avih->i_microsecperframe;
754 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
757 tk->i_samplesize = 1;
758 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
759 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
763 if( p_sys->b_seekable )
765 /* we have read all chunk so go back to movi */
766 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
768 /* Skip movi header */
769 stream_Read( p_demux->s, NULL, 12 );
771 p_sys->i_movi_begin = p_movi->i_chunk_pos;
775 for( unsigned i = 0; i < p_sys->i_attachment; i++)
776 vlc_input_attachment_Delete(p_sys->attachment[i]);
777 free(p_sys->attachment);
780 vlc_meta_Delete( p_sys->meta );
782 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
784 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
787 /*****************************************************************************
788 * Close: frees unused data
789 *****************************************************************************/
790 static void Close ( vlc_object_t * p_this )
792 demux_t * p_demux = (demux_t *)p_this;
794 demux_sys_t *p_sys = p_demux->p_sys ;
796 for( i = 0; i < p_sys->i_track; i++ )
798 if( p_sys->track[i] )
800 if( p_sys->track[i]->p_out_muxed )
801 stream_Delete( p_sys->track[i]->p_out_muxed );
802 free( p_sys->track[i]->p_index );
803 free( p_sys->track[i] );
806 free( p_sys->track );
807 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
808 vlc_meta_Delete( p_sys->meta );
809 for( unsigned i = 0; i < p_sys->i_attachment; i++)
810 vlc_input_attachment_Delete(p_sys->attachment[i]);
811 free(p_sys->attachment);
816 /*****************************************************************************
817 * Demux_Seekable: reads and demuxes data packets for stream seekable
818 *****************************************************************************
819 * AVIDemux: reads and demuxes data packets
820 *****************************************************************************
821 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
822 *****************************************************************************/
829 off_t i_posf; /* where we will read :
830 if i_idxposb == 0 : begining of chunk (+8 to acces data)
831 else : point on data directly */
832 } avi_track_toread_t;
834 static int Demux_Seekable( demux_t *p_demux )
836 demux_sys_t *p_sys = p_demux->p_sys;
838 unsigned int i_track_count = 0;
839 unsigned int i_track;
840 /* cannot be more than 100 stream (dcXX or wbXX) */
841 avi_track_toread_t toread[100];
844 /* detect new selected/unselected streams */
845 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
847 avi_track_t *tk = p_sys->track[i_track];
850 if( p_sys->b_muxed && tk->p_out_muxed )
853 tk->b_activated = true;
857 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
858 if( b && !tk->b_activated )
860 if( p_sys->b_seekable)
862 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
864 tk->b_activated = true;
866 else if( !b && tk->b_activated )
868 tk->b_activated = false;
876 if( i_track_count <= 0 )
878 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
880 p_sys->i_time += 25*1000; /* read 25ms */
883 if( p_sys->i_time >= i_length )
887 msg_Warn( p_demux, "no track selected, exiting..." );
891 /* wait for the good time */
892 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
893 p_sys->i_time += 25*1000; /* read 25ms */
896 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
898 avi_track_t *tk = p_sys->track[i_track];
901 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
902 if( tk->i_idxposc < tk->i_idxnb )
904 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
905 if( tk->i_idxposb > 0 )
907 toread[i_track].i_posf += 8 + tk->i_idxposb;
912 toread[i_track].i_posf = -1;
915 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
917 if( tk->i_samplesize )
919 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
923 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
928 toread[i_track].i_toread *= -1;
941 /* search for first chunk to be read */
942 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
944 if( !toread[i].b_ok ||
945 AVI_GetDPTS( p_sys->track[i],
946 toread[i].i_toread ) <= -25 * 1000 )
951 if( toread[i].i_toread > 0 )
953 b_done = false; /* not yet finished */
955 if( toread[i].i_posf > 0 )
957 if( i_pos == -1 || i_pos > toread[i].i_posf )
960 i_pos = toread[i].i_posf;
967 for( i = 0; i < p_sys->i_track; i++ )
972 msg_Warn( p_demux, "all tracks have failed, exiting..." );
978 int i_loop_count = 0;
980 /* no valid index, we will parse directly the stream
981 * in case we fail we will disable all finished stream */
982 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
984 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
985 if( AVI_PacketNext( p_demux ) )
987 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
992 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
999 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1002 "cannot get packet header, track disabled" );
1003 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1005 if( avi_pk.i_stream >= p_sys->i_track ||
1006 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1008 if( AVI_PacketNext( p_demux ) )
1011 "cannot skip packet, track disabled" );
1012 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1015 /* Prevents from eating all the CPU with broken files.
1016 * This value should be low enough so that it doesn't
1017 * affect the reading speed too much. */
1018 if( !(++i_loop_count % 1024) )
1020 if( !vlc_object_alive (p_demux) ) return -1;
1023 if( !(i_loop_count % (1024 * 10)) )
1025 "don't seem to find any data..." );
1031 /* add this chunk to the index */
1034 index.i_id = avi_pk.i_fourcc;
1036 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1038 index.i_pos = avi_pk.i_pos;
1039 index.i_length = avi_pk.i_size;
1040 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1042 i_track = avi_pk.i_stream;
1043 tk = p_sys->track[i_track];
1044 /* do we will read this data ? */
1045 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1051 if( AVI_PacketNext( p_demux ) )
1054 "cannot skip packet, track disabled" );
1055 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1064 stream_Seek( p_demux->s, i_pos );
1067 /* Set the track to use */
1068 tk = p_sys->track[i_track];
1070 /* read thoses data */
1071 if( tk->i_samplesize )
1073 unsigned int i_toread;
1075 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1077 if( tk->i_samplesize > 1 )
1079 i_toread = tk->i_samplesize;
1083 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1084 i_toread = __MAX( i_toread, 100 );
1087 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
1093 i_size = tk->p_index[tk->i_idxposc].i_length;
1096 if( tk->i_idxposb == 0 )
1098 i_size += 8; /* need to read and skip header */
1101 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1103 msg_Warn( p_demux, "failed reading data" );
1105 toread[i_track].b_ok = false;
1108 if( i_size % 2 ) /* read was padded on word boundary */
1110 p_frame->i_buffer--;
1113 if( tk->i_idxposb == 0 )
1115 p_frame->p_buffer += 8;
1116 p_frame->i_buffer -= 8;
1118 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1119 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1121 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1125 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1129 if( tk->i_samplesize )
1131 if( tk->i_idxposb == 0 )
1135 toread[i_track].i_toread -= i_size;
1136 tk->i_idxposb += i_size;
1137 if( tk->i_idxposb >=
1138 tk->p_index[tk->i_idxposc].i_length )
1146 int i_length = tk->p_index[tk->i_idxposc].i_length;
1149 if( tk->i_cat == AUDIO_ES )
1151 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1153 toread[i_track].i_toread--;
1156 if( tk->i_idxposc < tk->i_idxnb)
1158 toread[i_track].i_posf =
1159 tk->p_index[tk->i_idxposc].i_pos;
1160 if( tk->i_idxposb > 0 )
1162 toread[i_track].i_posf += 8 + tk->i_idxposb;
1168 toread[i_track].i_posf = -1;
1171 if( tk->i_cat != VIDEO_ES )
1172 p_frame->i_dts = p_frame->i_pts;
1175 p_frame->i_dts = p_frame->i_pts;
1176 p_frame->i_pts = VLC_TS_INVALID;
1179 //p_pes->i_rate = p_demux->stream.control.i_rate;
1180 if( tk->p_out_muxed )
1181 stream_DemuxSend( tk->p_out_muxed, p_frame );
1183 es_out_Send( p_demux->out, tk->p_es, p_frame );
1188 /*****************************************************************************
1189 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1190 *****************************************************************************
1191 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1192 *****************************************************************************/
1193 static int Demux_UnSeekable( demux_t *p_demux )
1195 demux_sys_t *p_sys = p_demux->p_sys;
1196 avi_track_t *p_stream_master = NULL;
1197 unsigned int i_stream;
1198 unsigned int i_packet;
1200 if( p_sys->b_muxed )
1202 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1203 return VLC_EGENERIC;
1206 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1208 /* *** find master stream for data packet skipping algo *** */
1209 /* *** -> first video, if any, or first audio ES *** */
1210 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1212 avi_track_t *tk = p_sys->track[i_stream];
1215 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1217 if( b && tk->i_cat == VIDEO_ES )
1219 p_stream_master = tk;
1223 p_stream_master = tk;
1227 if( !p_stream_master )
1229 msg_Warn( p_demux, "no more stream selected" );
1233 p_sys->i_time = AVI_GetPTS( p_stream_master );
1235 for( i_packet = 0; i_packet < 10; i_packet++)
1237 #define p_stream p_sys->track[avi_pk.i_stream]
1239 avi_packet_t avi_pk;
1241 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1246 if( avi_pk.i_stream >= p_sys->i_track ||
1247 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1249 /* we haven't found an audio or video packet:
1250 * - we have seek, found first next packet
1251 * - others packets could be found, skip them
1253 switch( avi_pk.i_fourcc )
1255 case AVIFOURCC_JUNK:
1256 case AVIFOURCC_LIST:
1257 case AVIFOURCC_RIFF:
1258 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1259 case AVIFOURCC_idx1:
1262 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1264 return( 0 ); /* eof */
1267 "seems to have lost position, resync" );
1268 if( AVI_PacketSearch( p_demux ) )
1270 msg_Err( p_demux, "resync failed" );
1277 /* check for time */
1278 if( __ABS( AVI_GetPTS( p_stream ) -
1279 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1281 /* load it and send to decoder */
1283 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1287 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1289 if( avi_pk.i_cat != VIDEO_ES )
1290 p_frame->i_dts = p_frame->i_pts;
1293 p_frame->i_dts = p_frame->i_pts;
1294 p_frame->i_pts = VLC_TS_INVALID;
1297 //p_pes->i_rate = p_demux->stream.control.i_rate;
1298 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1302 if( AVI_PacketNext( p_demux ) )
1308 /* *** update stream time position *** */
1309 if( p_stream->i_samplesize )
1311 p_stream->i_idxposb += avi_pk.i_size;
1315 if( p_stream->i_cat == AUDIO_ES )
1317 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1319 p_stream->i_idxposc++;
1329 /*****************************************************************************
1330 * Seek: goto to i_date or i_percent
1331 *****************************************************************************/
1332 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1335 demux_sys_t *p_sys = p_demux->p_sys;
1336 unsigned int i_stream;
1337 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1338 i_date / 1000000, i_percent );
1340 if( p_sys->b_seekable )
1342 if( !p_sys->i_length )
1344 avi_track_t *p_stream;
1347 /* use i_percent to create a true i_date */
1348 msg_Warn( p_demux, "seeking without index at %d%%"
1349 " only works for interleaved files", i_percent );
1350 if( i_percent >= 100 )
1352 msg_Warn( p_demux, "cannot seek so far !" );
1353 return VLC_EGENERIC;
1355 i_percent = __MAX( i_percent, 0 );
1357 /* try to find chunk that is at i_percent or the file */
1358 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1359 p_sys->i_movi_begin );
1360 /* search first selected stream (and prefer non eof ones) */
1361 for( i_stream = 0, p_stream = NULL;
1362 i_stream < p_sys->i_track; i_stream++ )
1364 if( !p_stream || p_stream->b_eof )
1365 p_stream = p_sys->track[i_stream];
1367 if( p_stream->b_activated && !p_stream->b_eof )
1370 if( !p_stream || !p_stream->b_activated )
1372 msg_Warn( p_demux, "cannot find any selected stream" );
1373 return VLC_EGENERIC;
1376 /* be sure that the index exist */
1377 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1379 msg_Warn( p_demux, "cannot seek" );
1380 return VLC_EGENERIC;
1383 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1384 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1386 /* search after i_idxposc */
1387 if( AVI_StreamChunkSet( p_demux,
1388 i_stream, p_stream->i_idxposc + 1 ) )
1390 msg_Warn( p_demux, "cannot seek" );
1391 return VLC_EGENERIC;
1395 i_date = AVI_GetPTS( p_stream );
1396 /* TODO better support for i_samplesize != 0 */
1397 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1401 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1403 avi_track_t *p_stream = p_sys->track[i_stream];
1405 if( !p_stream->b_activated )
1408 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1410 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1411 p_sys->i_time = i_date;
1412 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1417 msg_Err( p_demux, "shouldn't yet be executed" );
1418 return VLC_EGENERIC;
1422 /*****************************************************************************
1424 *****************************************************************************/
1425 static double ControlGetPosition( demux_t *p_demux )
1427 demux_sys_t *p_sys = p_demux->p_sys;
1429 if( p_sys->i_length > 0 )
1431 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1433 else if( stream_Size( p_demux->s ) > 0 )
1439 /* search the more advanced selected es */
1440 for( i = 0; i < p_sys->i_track; i++ )
1442 avi_track_t *tk = p_sys->track[i];
1443 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1445 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1446 tk->p_index[tk->i_idxposc].i_length + 8;
1453 return (double)i64 / stream_Size( p_demux->s );
1458 static int Control( demux_t *p_demux, int i_query, va_list args )
1460 demux_sys_t *p_sys = p_demux->p_sys;
1468 case DEMUX_GET_POSITION:
1469 pf = (double*)va_arg( args, double * );
1470 *pf = ControlGetPosition( p_demux );
1472 case DEMUX_SET_POSITION:
1473 f = (double)va_arg( args, double );
1474 if( p_sys->b_seekable )
1476 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1477 return Seek( p_demux, i64, (int)(f * 100) );
1481 int64_t i_pos = stream_Size( p_demux->s ) * f;
1482 return stream_Seek( p_demux->s, i_pos );
1485 case DEMUX_GET_TIME:
1486 pi64 = (int64_t*)va_arg( args, int64_t * );
1487 *pi64 = p_sys->i_time;
1490 case DEMUX_SET_TIME:
1494 i64 = (int64_t)va_arg( args, int64_t );
1495 if( p_sys->i_length > 0 )
1497 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1499 else if( p_sys->i_time > 0 )
1501 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1502 (double)i64 / (double)p_sys->i_time );
1504 return Seek( p_demux, i64, i_percent );
1506 case DEMUX_GET_LENGTH:
1507 pi64 = (int64_t*)va_arg( args, int64_t * );
1508 *pi64 = p_sys->i_length * (mtime_t)1000000;
1512 pf = (double*)va_arg( args, double * );
1514 for( i = 0; i < (int)p_sys->i_track; i++ )
1516 avi_track_t *tk = p_sys->track[i];
1517 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1519 *pf = (float)tk->i_rate / (float)tk->i_scale;
1525 case DEMUX_GET_META:
1526 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1527 vlc_meta_Merge( p_meta, p_sys->meta );
1530 case DEMUX_GET_ATTACHMENTS:
1532 if( p_sys->i_attachment <= 0 )
1533 return VLC_EGENERIC;
1535 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1536 int *pi_int = va_arg( args, int * );
1538 *pi_int = p_sys->i_attachment;
1539 *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
1540 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1541 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1546 return VLC_EGENERIC;
1550 /*****************************************************************************
1551 * Function to convert pts to chunk or byte
1552 *****************************************************************************/
1554 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1559 return (mtime_t)((int64_t)i_pts *
1560 (int64_t)tk->i_rate /
1561 (int64_t)tk->i_scale /
1564 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1566 if( !tk->i_scale || !tk->i_samplesize )
1569 return (mtime_t)((int64_t)i_pts *
1570 (int64_t)tk->i_rate /
1571 (int64_t)tk->i_scale /
1573 (int64_t)tk->i_samplesize );
1576 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1583 i_dpts = (mtime_t)( (int64_t)1000000 *
1585 (int64_t)tk->i_scale /
1586 (int64_t)tk->i_rate );
1588 if( tk->i_samplesize )
1590 return i_dpts / tk->i_samplesize;
1595 static mtime_t AVI_GetPTS( avi_track_t *tk )
1597 if( tk->i_samplesize )
1599 int64_t i_count = 0;
1601 /* we need a valid entry we will emulate one */
1602 if( tk->i_idxposc == tk->i_idxnb )
1606 /* use the last entry */
1607 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1608 + tk->p_index[tk->i_idxnb - 1].i_length;
1613 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1615 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1619 if( tk->i_cat == AUDIO_ES )
1621 return AVI_GetDPTS( tk, tk->i_blockno );
1625 return AVI_GetDPTS( tk, tk->i_idxposc );
1630 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1632 demux_sys_t *p_sys = p_demux->p_sys;
1633 avi_packet_t avi_pk;
1634 int i_loop_count = 0;
1636 /* find first chunk of i_stream that isn't in index */
1638 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1640 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1641 if( AVI_PacketNext( p_demux ) )
1643 return VLC_EGENERIC;
1648 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1653 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1655 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1657 msg_Warn( p_demux, "cannot get packet header" );
1658 return VLC_EGENERIC;
1660 if( avi_pk.i_stream >= p_sys->i_track ||
1661 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1663 if( AVI_PacketNext( p_demux ) )
1665 return VLC_EGENERIC;
1668 /* Prevents from eating all the CPU with broken files.
1669 * This value should be low enough so that it doesn't
1670 * affect the reading speed too much. */
1671 if( !(++i_loop_count % 1024) )
1673 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1676 if( !(i_loop_count % (1024 * 10)) )
1677 msg_Warn( p_demux, "don't seem to find any data..." );
1682 /* add this chunk to the index */
1685 index.i_id = avi_pk.i_fourcc;
1687 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1689 index.i_pos = avi_pk.i_pos;
1690 index.i_length = avi_pk.i_size;
1691 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1693 if( avi_pk.i_stream == i_stream )
1698 if( AVI_PacketNext( p_demux ) )
1700 return VLC_EGENERIC;
1706 /* be sure that i_ck will be a valid index entry */
1707 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1710 demux_sys_t *p_sys = p_demux->p_sys;
1711 avi_track_t *p_stream = p_sys->track[i_stream];
1713 p_stream->i_idxposc = i_ck;
1714 p_stream->i_idxposb = 0;
1716 if( i_ck >= p_stream->i_idxnb )
1718 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1721 p_stream->i_idxposc++;
1722 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1724 return VLC_EGENERIC;
1727 } while( p_stream->i_idxposc < i_ck );
1733 /* XXX FIXME up to now, we assume that all chunk are one after one */
1734 static int AVI_StreamBytesSet( demux_t *p_demux,
1735 unsigned int i_stream,
1738 demux_sys_t *p_sys = p_demux->p_sys;
1739 avi_track_t *p_stream = p_sys->track[i_stream];
1741 if( ( p_stream->i_idxnb > 0 )
1742 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1743 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1745 /* index is valid to find the ck */
1746 /* uses dichototmie to be fast enougth */
1747 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1748 int i_idxmax = p_stream->i_idxnb;
1752 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1754 i_idxmax = i_idxposc ;
1755 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1759 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1760 p_stream->p_index[i_idxposc].i_length <= i_byte)
1762 i_idxmin = i_idxposc ;
1763 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1767 p_stream->i_idxposc = i_idxposc;
1768 p_stream->i_idxposb = i_byte -
1769 p_stream->p_index[i_idxposc].i_lengthtotal;
1778 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1779 p_stream->i_idxposb = 0;
1782 p_stream->i_idxposc++;
1783 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1785 return VLC_EGENERIC;
1788 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1789 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1791 p_stream->i_idxposb = i_byte -
1792 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1797 static int AVI_TrackSeek( demux_t *p_demux,
1801 demux_sys_t *p_sys = p_demux->p_sys;
1802 avi_track_t *tk = p_sys->track[i_stream];
1804 #define p_stream p_sys->track[i_stream]
1807 i_oldpts = AVI_GetPTS( p_stream );
1809 if( !p_stream->i_samplesize )
1811 if( AVI_StreamChunkSet( p_demux,
1813 AVI_PTSToChunk( p_stream, i_date ) ) )
1815 return VLC_EGENERIC;
1818 if( p_stream->i_cat == AUDIO_ES )
1822 for( i = 0; i < tk->i_idxposc; i++ )
1824 if( tk->i_blocksize > 0 )
1826 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1836 "old:%"PRId64" %s new %"PRId64,
1838 i_oldpts > i_date ? ">" : "<",
1841 if( p_stream->i_cat == VIDEO_ES )
1843 /* search key frame */
1844 //if( i_date < i_oldpts || 1 )
1846 while( p_stream->i_idxposc > 0 &&
1847 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1850 if( AVI_StreamChunkSet( p_demux,
1852 p_stream->i_idxposc - 1 ) )
1854 return VLC_EGENERIC;
1861 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1862 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1865 if( AVI_StreamChunkSet( p_demux,
1867 p_stream->i_idxposc + 1 ) )
1869 return VLC_EGENERIC;
1878 if( AVI_StreamBytesSet( p_demux,
1880 AVI_PTSToByte( p_stream, i_date ) ) )
1882 return VLC_EGENERIC;
1889 /****************************************************************************
1890 * Return true if it's a key frame
1891 ****************************************************************************/
1892 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1896 case VLC_CODEC_DIV1:
1898 * startcode: 0x00000100 32bits
1899 * framenumber ? 5bits
1900 * piture type 0(I),1(P) 2bits
1902 if( GetDWBE( p_byte ) != 0x00000100 )
1904 /* it's not an msmpegv1 stream, strange...*/
1905 return AVIIF_KEYFRAME;
1907 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1909 case VLC_CODEC_DIV2:
1910 case VLC_CODEC_DIV3:
1911 case VLC_CODEC_WMV1:
1913 * picture type 0(I),1(P) 2bits
1915 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1916 case VLC_CODEC_MP4V:
1917 /* we should find first occurrence of 0x000001b6 (32bits)
1918 * startcode: 0x000001b6 32bits
1919 * piture type 0(I),1(P) 2bits
1921 if( GetDWBE( p_byte ) != 0x000001b6 )
1923 /* not true , need to find the first VOP header */
1924 return AVIIF_KEYFRAME;
1926 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1929 /* I can't do it, so say yes */
1930 return AVIIF_KEYFRAME;
1934 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1939 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1942 return vlc_fourcc_GetCodec( i_cat, i_codec );
1944 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1948 /****************************************************************************
1950 ****************************************************************************/
1951 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1952 unsigned int *pi_number, unsigned int *pi_type )
1954 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1957 c1 = ((uint8_t *)&i_id)[0];
1958 c2 = ((uint8_t *)&i_id)[1];
1960 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1962 SET_PTR( pi_number, 100 ); /* > max stream number */
1963 SET_PTR( pi_type, UNKNOWN_ES );
1967 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1968 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1971 SET_PTR( pi_type, AUDIO_ES );
1976 SET_PTR( pi_type, VIDEO_ES );
1979 SET_PTR( pi_type, SPU_ES );
1982 SET_PTR( pi_type, UNKNOWN_ES );
1989 /****************************************************************************
1991 ****************************************************************************/
1992 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1994 const uint8_t *p_peek;
1996 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1998 return VLC_EGENERIC;
2000 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
2001 p_pk->i_size = GetDWLE( p_peek + 4 );
2002 p_pk->i_pos = stream_Tell( p_demux->s );
2003 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
2005 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
2006 p_peek[10], p_peek[11] );
2013 memcpy( p_pk->i_peek, p_peek + 8, 8 );
2015 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
2019 static int AVI_PacketNext( demux_t *p_demux )
2021 avi_packet_t avi_ck;
2024 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
2026 return VLC_EGENERIC;
2029 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
2030 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
2034 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
2035 avi_ck.i_type == AVIFOURCC_AVIX )
2041 i_skip = __EVEN( avi_ck.i_size ) + 8;
2044 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2046 return VLC_EGENERIC;
2051 static int AVI_PacketRead( demux_t *p_demux,
2053 block_t **pp_frame )
2057 i_size = __EVEN( p_pk->i_size + 8 );
2059 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2061 return VLC_EGENERIC;
2063 (*pp_frame)->p_buffer += 8;
2064 (*pp_frame)->i_buffer -= 8;
2066 if( i_size != p_pk->i_size + 8 )
2068 (*pp_frame)->i_buffer--;
2074 static int AVI_PacketSearch( demux_t *p_demux )
2076 demux_sys_t *p_sys = p_demux->p_sys;
2077 avi_packet_t avi_pk;
2082 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2084 return VLC_EGENERIC;
2086 AVI_PacketGetHeader( p_demux, &avi_pk );
2087 if( avi_pk.i_stream < p_sys->i_track &&
2088 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2092 switch( avi_pk.i_fourcc )
2094 case AVIFOURCC_JUNK:
2095 case AVIFOURCC_LIST:
2096 case AVIFOURCC_RIFF:
2097 case AVIFOURCC_idx1:
2101 /* Prevents from eating all the CPU with broken files.
2102 * This value should be low enough so that it doesn't affect the
2103 * reading speed too much (not that we care much anyway because
2104 * this code is called only on broken files). */
2105 if( !(++i_count % 1024) )
2107 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2110 if( !(i_count % (1024 * 10)) )
2111 msg_Warn( p_demux, "trying to resync..." );
2116 /****************************************************************************
2118 ****************************************************************************/
2119 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2121 avi_entry_t *p_index)
2123 avi_track_t *tk = p_sys->track[i_stream];
2125 /* Update i_movi_lastchunk_pos */
2126 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2128 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2132 if( tk->i_idxnb >= tk->i_idxmax )
2134 tk->i_idxmax += 16384;
2135 tk->p_index = realloc_or_free( tk->p_index,
2136 tk->i_idxmax * sizeof( avi_entry_t ) );
2137 if( tk->p_index == NULL )
2142 /* calculate cumulate length */
2143 if( tk->i_idxnb > 0 )
2145 p_index->i_lengthtotal =
2146 tk->p_index[tk->i_idxnb - 1].i_length +
2147 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2151 p_index->i_lengthtotal = 0;
2154 tk->p_index[tk->i_idxnb++] = *p_index;
2157 static int AVI_IndexFind_idx1( demux_t *p_demux,
2158 avi_chunk_idx1_t **pp_idx1,
2159 uint64_t *pi_offset )
2161 demux_sys_t *p_sys = p_demux->p_sys;
2163 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2164 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2168 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2169 return VLC_EGENERIC;
2173 /* *** calculate offset *** */
2174 /* Well, avi is __SHIT__ so test more than one entry
2175 * (needed for some avi files) */
2176 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2178 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2180 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2182 *pi_offset = p_movi->i_chunk_pos + 8;
2189 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2191 demux_sys_t *p_sys = p_demux->p_sys;
2193 avi_chunk_idx1_t *p_idx1;
2195 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2196 return VLC_EGENERIC;
2200 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2201 b_keyset[i_stream] = false;
2203 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2208 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2211 if( i_stream < p_sys->i_track &&
2212 i_cat == p_sys->track[i_stream]->i_cat )
2215 index.i_id = p_idx1->entry[i_index].i_fourcc;
2217 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2218 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2219 index.i_length = p_idx1->entry[i_index].i_length;
2220 AVI_IndexAddEntry( p_sys, i_stream, &index );
2222 if( index.i_flags&AVIIF_KEYFRAME )
2223 b_keyset[i_stream] = true;
2227 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2229 if( !b_keyset[i_stream] )
2231 avi_track_t *tk = p_sys->track[i_stream];
2233 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2234 for( unsigned i_index = 0; i_index < tk->i_idxnb; i_index++ )
2235 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2241 static void __Parse_indx( demux_t *p_demux,
2243 avi_chunk_indx_t *p_indx )
2245 demux_sys_t *p_sys = p_demux->p_sys;
2249 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2250 if( p_indx->i_indexsubtype == 0 )
2252 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2254 index.i_id = p_indx->i_id;
2255 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2256 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2257 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2259 AVI_IndexAddEntry( p_sys, i_stream, &index );
2262 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2264 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2266 index.i_id = p_indx->i_id;
2267 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2268 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2269 index.i_length = p_indx->idx.field[i].i_size;
2271 AVI_IndexAddEntry( p_sys, i_stream, &index );
2276 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2280 static void AVI_IndexLoad_indx( demux_t *p_demux )
2282 demux_sys_t *p_sys = p_demux->p_sys;
2283 unsigned int i_stream;
2286 avi_chunk_list_t *p_riff;
2287 avi_chunk_list_t *p_hdrl;
2289 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2290 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2292 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2294 avi_chunk_list_t *p_strl;
2295 avi_chunk_indx_t *p_indx;
2297 #define p_stream p_sys->track[i_stream]
2298 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2299 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2303 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2308 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2310 __Parse_indx( p_demux, i_stream, p_indx );
2312 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2315 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2317 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2318 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2322 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2323 AVI_ChunkFree( p_demux->s, &ck_sub );
2328 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2334 static void AVI_IndexLoad( demux_t *p_demux )
2336 demux_sys_t *p_sys = p_demux->p_sys;
2337 unsigned int i_stream;
2339 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2341 p_sys->track[i_stream]->i_idxnb = 0;
2342 p_sys->track[i_stream]->i_idxmax = 0;
2343 p_sys->track[i_stream]->p_index = NULL;
2348 AVI_IndexLoad_indx( p_demux );
2350 else if( AVI_IndexLoad_idx1( p_demux ) )
2352 /* try indx if idx1 failed as some "normal" file have indx too */
2353 AVI_IndexLoad_indx( p_demux );
2356 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2358 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2359 i_stream, p_sys->track[i_stream]->i_idxnb );
2363 static void AVI_IndexCreate( demux_t *p_demux )
2365 demux_sys_t *p_sys = p_demux->p_sys;
2367 avi_chunk_list_t *p_riff;
2368 avi_chunk_list_t *p_movi;
2370 unsigned int i_stream;
2373 mtime_t i_dialog_update;
2374 dialog_progress_bar_t *p_dialog = NULL;
2376 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2377 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2381 msg_Err( p_demux, "cannot find p_movi" );
2385 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2387 p_sys->track[i_stream]->i_idxnb = 0;
2388 p_sys->track[i_stream]->i_idxmax = 0;
2389 p_sys->track[i_stream]->p_index = NULL;
2391 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2392 stream_Size( p_demux->s ) );
2394 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2395 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2398 /* Only show dialog if AVI is > 10MB */
2399 i_dialog_update = mdate();
2400 if( stream_Size( p_demux->s ) > 10000000 )
2401 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2402 NULL, _("Cancel") );
2408 if( !vlc_object_alive (p_demux) )
2411 /* Don't update/check dialog too often */
2412 if( p_dialog && mdate() - i_dialog_update > 100000 )
2414 if( dialog_ProgressCancelled( p_dialog ) )
2417 double f_current = stream_Tell( p_demux->s );
2418 double f_size = stream_Size( p_demux->s );
2419 double f_pos = f_current / f_size;
2420 dialog_ProgressSet( p_dialog, NULL, f_pos );
2422 i_dialog_update = mdate();
2425 if( AVI_PacketGetHeader( p_demux, &pk ) )
2428 if( pk.i_stream < p_sys->i_track &&
2429 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2432 index.i_id = pk.i_fourcc;
2434 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2435 index.i_pos = pk.i_pos;
2436 index.i_length = pk.i_size;
2437 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2441 switch( pk.i_fourcc )
2443 case AVIFOURCC_idx1:
2446 avi_chunk_list_t *p_sysx;
2447 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2448 AVIFOURCC_RIFF, 1 );
2450 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2451 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2457 case AVIFOURCC_RIFF:
2458 msg_Dbg( p_demux, "new RIFF chunk found" );
2462 case AVIFOURCC_JUNK:
2466 msg_Warn( p_demux, "need resync, probably broken avi" );
2467 if( AVI_PacketSearch( p_demux ) )
2469 msg_Warn( p_demux, "lost sync, abord index creation" );
2475 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2476 AVI_PacketNext( p_demux ) )
2483 if( p_dialog != NULL )
2484 dialog_ProgressDestroy( p_dialog );
2486 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2488 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2489 i_stream, p_sys->track[i_stream]->i_idxnb );
2494 static void AVI_MetaLoad( demux_t *p_demux,
2495 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2497 demux_sys_t *p_sys = p_demux->p_sys;
2499 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2504 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2505 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2506 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2507 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2508 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2509 vlc_meta_SetSetting( p_meta, buffer );
2511 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2515 static const struct {
2519 { AVIFOURCC_IART, vlc_meta_Artist },
2520 { AVIFOURCC_ICMT, vlc_meta_Description },
2521 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2522 { AVIFOURCC_IGNR, vlc_meta_Genre },
2523 { AVIFOURCC_INAM, vlc_meta_Title },
2526 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2528 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2531 char *psz_value = FromLatin1( p_strz->p_str );
2536 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2541 /*****************************************************************************
2543 *****************************************************************************/
2544 static void AVI_ExtractSubtitle( demux_t *p_demux,
2546 avi_chunk_list_t *p_strl,
2547 avi_chunk_STRING_t *p_strn )
2549 demux_sys_t *p_sys = p_demux->p_sys;
2550 block_t *p_block = NULL;
2551 input_attachment_t *p_attachment = NULL;
2552 char *psz_description = NULL;
2553 avi_chunk_indx_t *p_indx = NULL;
2555 if( !p_sys->b_seekable )
2558 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2564 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2565 p_indx->i_entriesinuse > 0 )
2567 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2568 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2573 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2574 p_indx->i_entriesinuse != 1 ||
2575 p_indx->i_indexsubtype != 0 )
2578 i_position = p_indx->i_baseoffset +
2579 p_indx->idx.std[0].i_offset - 8;
2580 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2584 avi_chunk_idx1_t *p_idx1;
2587 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2591 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2593 const idx1_entry_t *e = &p_idx1->entry[i];
2595 unsigned i_stream_idx;
2597 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2598 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2600 i_position = e->i_pos + i_offset;
2601 i_size = e->i_length + 8;
2610 if( i_size > 1000000 )
2613 if( stream_Seek( p_demux->s, i_position ) )
2615 p_block = stream_Block( p_demux->s, i_size );
2619 /* Parse packet header */
2620 const uint8_t *p = p_block->p_buffer;
2621 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2626 /* Parse subtitle chunk header */
2627 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2628 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2630 const unsigned i_name = GetDWLE( &p[7] );
2631 if( 11 + i_size <= i_name )
2634 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2636 i_size -= 11 + i_name;
2637 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2639 const unsigned i_payload = GetDWLE( &p[2] );
2640 if( i_size < 6 + i_payload || i_payload <= 0 )
2645 if( !psz_description )
2646 psz_description = p_strn ? FromLatin1( p_strn->p_str ) : NULL;
2648 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2650 p_attachment = vlc_input_attachment_New( psz_name,
2651 "application/x-srt",
2655 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2659 free( psz_description );
2662 block_Release( p_block );
2665 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2667 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2669 if( p_indx == &ck.indx )
2670 AVI_ChunkFree( p_demux->s, &ck );
2672 /*****************************************************************************
2674 *****************************************************************************/
2675 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2677 demux_sys_t *p_sys = p_demux->p_sys;
2681 for( i = 0; i < p_sys->i_track; i++ )
2683 avi_track_t *tk = p_sys->track[i];
2684 if( tk->i_idxposc >= tk->i_idxnb )
2696 /****************************************************************************
2697 * AVI_MovieGetLength give max streams length in second
2698 ****************************************************************************/
2699 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2701 demux_sys_t *p_sys = p_demux->p_sys;
2702 mtime_t i_maxlength = 0;
2705 for( i = 0; i < p_sys->i_track; i++ )
2707 avi_track_t *tk = p_sys->track[i];
2710 /* fix length for each stream */
2711 if( tk->i_idxnb < 1 || !tk->p_index )
2716 if( tk->i_samplesize )
2718 i_length = AVI_GetDPTS( tk,
2719 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2720 tk->p_index[tk->i_idxnb-1].i_length );
2724 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2726 i_length /= (mtime_t)1000000; /* in seconds */
2729 "stream[%d] length:%"PRId64" (based on index)",
2732 i_maxlength = __MAX( i_maxlength, i_length );