1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 # include <time.h> /* time() */
36 #include <vlc/input.h>
38 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
53 /* libebml and matroska */
54 #include "ebml/EbmlHead.h"
55 #include "ebml/EbmlSubHead.h"
56 #include "ebml/EbmlStream.h"
57 #include "ebml/EbmlContexts.h"
58 #include "ebml/EbmlVoid.h"
59 #include "ebml/EbmlVersion.h"
60 #include "ebml/StdIOCallback.h"
62 #include "matroska/KaxAttachments.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
82 #include "matroska/KaxContentEncoding.h"
84 #include "ebml/StdIOCallback.h"
87 #include "mp4/libmp4.h"
93 #define MATROSKA_COMPRESSION_NONE 0
94 #define MATROSKA_COMPRESSION_ZLIB 1
96 #define MKVD_TIMECODESCALE 1000000
99 * What's between a directory and a filename?
102 #define DIRECTORY_SEPARATOR '\\'
104 #define DIRECTORY_SEPARATOR '/'
107 using namespace LIBMATROSKA_NAMESPACE;
110 /*****************************************************************************
112 *****************************************************************************/
113 static int Open ( vlc_object_t * );
114 static void Close( vlc_object_t * );
117 set_shortname( _("Matroska") );
118 set_description( _("Matroska stream demuxer" ) );
119 set_capability( "demux2", 50 );
120 set_callbacks( Open, Close );
121 set_category( CAT_INPUT );
122 set_subcategory( SUBCAT_INPUT_DEMUX );
124 add_bool( "mkv-seek-percent", 1, NULL,
125 N_("Seek based on percent not time"),
126 N_("Seek based on percent not time"), VLC_TRUE );
128 add_shortcut( "mka" );
129 add_shortcut( "mkv" );
132 /*****************************************************************************
134 *****************************************************************************/
136 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
137 int result, dstsize, n;
142 d_stream.zalloc = (alloc_func)0;
143 d_stream.zfree = (free_func)0;
144 d_stream.opaque = (voidpf)0;
145 result = inflateInit(&d_stream);
148 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
152 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
153 d_stream.avail_in = p_in_block->i_buffer;
155 p_block = block_New( p_this, 0 );
160 p_block = block_Realloc( p_block, 0, n * 1000 );
161 dst = (unsigned char *)p_block->p_buffer;
162 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
163 d_stream.avail_out = 1000;
164 result = inflate(&d_stream, Z_NO_FLUSH);
165 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
167 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
171 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
172 ( result != Z_STREAM_END ) );
174 dstsize = d_stream.total_out;
175 inflateEnd( &d_stream );
177 p_block = block_Realloc( p_block, 0, dstsize );
178 p_block->i_buffer = dstsize;
179 block_Release( p_in_block );
186 * Helper function to print the mkv parse tree
188 static void MkvTree( demux_t & demuxer, int i_level, char *psz_format, ... )
193 msg_Err( &demuxer, "too deep tree" );
196 va_start( args, psz_format );
197 static char *psz_foo = "| | | | | | | | | |";
198 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
199 strncpy( psz_foo2, psz_foo, 4 * i_level );
200 psz_foo2[ 4 * i_level ] = '+';
201 psz_foo2[ 4 * i_level + 1 ] = ' ';
202 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
203 __msg_GenericVa( VLC_OBJECT(&demuxer), VLC_MSG_DBG, "mkv", psz_foo2, args );
208 /*****************************************************************************
210 *****************************************************************************/
211 class vlc_stream_io_callback: public IOCallback
218 vlc_stream_io_callback( stream_t * );
220 virtual uint32 read ( void *p_buffer, size_t i_size);
221 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
222 virtual size_t write ( const void *p_buffer, size_t i_size);
223 virtual uint64 getFilePointer ( void );
224 virtual void close ( void );
227 /*****************************************************************************
229 *****************************************************************************/
233 EbmlParser( EbmlStream *es, EbmlElement *el_start );
239 EbmlElement *Get( void );
242 int GetLevel( void );
247 EbmlElement *m_el[10];
256 /*****************************************************************************
257 * Some functions to manipulate memory
258 *****************************************************************************/
259 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
260 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
262 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
265 /*****************************************************************************
266 * definitions of structures and functions used by this plugins
267 *****************************************************************************/
270 vlc_bool_t b_default;
271 vlc_bool_t b_enabled;
275 uint8_t *p_extra_data;
279 uint64_t i_default_duration;
280 float f_timecodescale;
288 /* data to be send first */
290 uint8_t *p_data_init;
292 /* hack : it's for seek */
293 vlc_bool_t b_search_keyframe;
297 char *psz_codec_name;
298 char *psz_codec_settings;
299 char *psz_codec_info_url;
300 char *psz_codec_download_url;
302 /* encryption/compression */
303 int i_compression_type;
324 ,i_user_start_time(-1)
327 ,b_display_seekpoint(true)
331 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title );
332 const chapter_item_t * FindTimecode( mtime_t i_timecode ) const;
334 int64_t i_start_time, i_end_time;
335 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
336 std::vector<chapter_item_t> sub_chapters;
339 bool b_display_seekpoint;
340 std::string psz_name;
341 chapter_item_t *psz_parent;
343 bool operator<( const chapter_item_t & item ) const
345 return ( i_user_start_time < item.i_user_start_time || (i_user_start_time == item.i_user_start_time && i_user_end_time < item.i_user_end_time) );
353 class chapter_edition_t
361 void RefreshChapters( input_title_t & title );
362 double Duration() const;
363 const chapter_item_t * FindTimecode( mtime_t i_timecode ) const;
365 std::vector<chapter_item_t> chapters;
372 class matroska_segment_t
375 matroska_segment_t( demux_sys_t & demuxer, EbmlStream & estream )
378 ,i_timescale(MKVD_TIMECODESCALE)
381 ,i_chapters_position(-1)
388 ,psz_muxing_application(NULL)
389 ,psz_writing_application(NULL)
390 ,psz_segment_filename(NULL)
393 ,i_current_edition(-1)
394 ,psz_current_chapter(NULL)
399 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
402 ~matroska_segment_t()
404 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
406 #define tk tracks[i_track]
407 if( tk->fmt.psz_description )
409 free( tk->fmt.psz_description );
413 free( tk->psz_codec );
415 if( tk->fmt.psz_language )
417 free( tk->fmt.psz_language );
423 if( psz_writing_application )
425 free( psz_writing_application );
427 if( psz_muxing_application )
429 free( psz_muxing_application );
431 if( psz_segment_filename )
433 free( psz_segment_filename );
441 free( psz_date_utc );
453 uint64_t i_timescale;
455 /* duration of the segment */
459 std::vector<mkv_track_t*> tracks;
462 int64_t i_cues_position;
463 int64_t i_chapters_position;
464 int64_t i_tags_position;
468 KaxSegmentUID segment_uid;
469 KaxPrevUID prev_segment_uid;
470 KaxNextUID next_segment_uid;
478 char *psz_muxing_application;
479 char *psz_writing_application;
480 char *psz_segment_filename;
484 std::vector<chapter_edition_t> editions;
485 int i_current_edition;
486 const chapter_item_t *psz_current_chapter;
488 std::vector<KaxSegmentFamily> families;
494 inline chapter_edition_t *Edition()
496 if ( i_current_edition >= 0 && size_t(i_current_edition) < editions.size() )
497 return &editions[i_current_edition];
502 bool PreloadFamily( const matroska_segment_t & segment );
503 size_t PreloadLinked( const demux_sys_t & of_sys );
504 void ParseInfo( EbmlElement *info );
505 void ParseChapters( EbmlElement *chapters );
506 void ParseSeekHead( EbmlElement *seekhead );
507 void ParseTracks( EbmlElement *tracks );
508 void ParseChapterAtom( int i_level, EbmlMaster *ca, chapter_item_t & chapters );
509 void ParseTrackEntry( EbmlMaster *m );
510 void IndexAppendCluster( KaxCluster *cluster );
511 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
512 bool Select( mtime_t i_start_time );
516 class matroska_stream_t
519 matroska_stream_t( demux_sys_t & demuxer )
522 ,i_current_segment(-1)
528 for ( size_t i=0; i<segments.size(); i++ )
537 std::vector<matroska_segment_t*> segments;
538 int i_current_segment;
542 inline matroska_segment_t *Segment()
544 if ( i_current_segment >= 0 && size_t(i_current_segment) < segments.size() )
545 return segments[i_current_segment];
549 matroska_segment_t *FindSegment( const EbmlBinary & uid ) const;
551 void PreloadFamily( const matroska_segment_t & segment );
552 size_t PreloadLinked( const demux_sys_t & of_sys );
558 demux_sys_t( demux_t & demux )
565 ,i_current_stream(-1)
570 for (size_t i=0; i<streams.size(); i++)
579 mtime_t i_chapter_time;
583 input_title_t *title;
585 std::vector<matroska_stream_t*> streams;
586 int i_current_stream;
588 inline matroska_stream_t *Stream()
590 if ( i_current_stream >= 0 && size_t(i_current_stream) < streams.size() )
591 return streams[i_current_stream];
595 matroska_segment_t *FindSegment( const EbmlBinary & uid ) const;
596 void PreloadFamily( );
597 void PreloadLinked( );
598 matroska_stream_t *AnalyseAllSegmentsFound( EbmlStream *p_estream );
601 static int Demux ( demux_t * );
602 static int Control( demux_t *, int, va_list );
603 static void Seek ( demux_t *, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter );
605 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
607 static char *UTF8ToStr ( const UTFstring &u );
608 static void LoadCues ( demux_t * );
609 static void InformationCreate ( demux_t * );
611 /*****************************************************************************
612 * Open: initializes matroska demux structures
613 *****************************************************************************/
614 static int Open( vlc_object_t * p_this )
616 demux_t *p_demux = (demux_t*)p_this;
618 matroska_stream_t *p_stream;
619 matroska_segment_t *p_segment;
621 std::string s_path, s_filename;
622 vlc_stream_io_callback *p_io_callback;
623 EbmlStream *p_io_stream;
625 /* peek the begining */
626 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
628 /* is a valid file */
629 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
630 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
632 /* Set the demux function */
633 p_demux->pf_demux = Demux;
634 p_demux->pf_control = Control;
635 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
637 p_io_callback = new vlc_stream_io_callback( p_demux->s );
638 p_io_stream = new EbmlStream( *p_io_callback );
640 if( p_io_stream == NULL )
642 msg_Err( p_demux, "failed to create EbmlStream" );
643 delete p_io_callback;
648 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
649 if( p_stream == NULL )
651 msg_Err( p_demux, "cannot find KaxSegment" );
654 p_sys->streams.push_back( p_stream );
655 p_sys->i_current_stream = 0;
657 p_stream->p_in = p_io_callback;
658 p_stream->p_es = p_io_stream;
660 for (size_t i=0; i<p_stream->segments.size(); i++)
662 p_stream->segments[i]->Preload();
664 p_stream->i_current_segment = 0;
666 p_segment = p_stream->Segment();
667 if( p_segment->cluster == NULL )
669 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
672 // reset the stream reading to the first cluster of the segment used
673 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
675 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
676 /* TODO handle multi-segment files */
677 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
679 // assume it's a regular file
680 // get the directory path
681 s_path = p_demux->psz_path;
682 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
684 s_path = s_path.substr(0,s_path.length()-1);
688 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
690 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
694 struct dirent *p_file_item;
695 DIR *p_src_dir = opendir(s_path.c_str());
697 if (p_src_dir != NULL)
699 while ((p_file_item = (dirent *) readdir(p_src_dir)))
701 if (strlen(p_file_item->d_name) > 4)
703 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
705 if (!s_filename.compare(p_demux->psz_path))
706 continue; // don't reuse the original opened file
708 #if defined(__GNUC__) && (__GNUC__ < 3)
709 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
710 !s_filename.compare("mka", s_filename.length() - 3, 3))
712 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
713 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
716 // test wether this file belongs to the our family
717 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
718 EbmlStream *p_estream = new EbmlStream(*p_file_io);
720 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
721 if ( p_stream == NULL )
723 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
729 p_stream->p_in = p_file_io;
730 p_stream->p_es = p_estream;
731 p_sys->streams.push_back( p_stream );
736 closedir( p_src_dir );
740 p_sys->PreloadFamily( );
741 p_sys->PreloadLinked( );
743 /* *** Load the cue if found *** */
744 if( p_segment->i_cues_position >= 0 )
746 vlc_bool_t b_seekable;
748 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
755 if( !p_segment->b_cues || p_segment->i_index <= 0 )
757 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
759 p_segment->IndexAppendCluster( p_segment->cluster );
761 p_segment->b_cues = VLC_FALSE;
764 /* add information */
765 InformationCreate( p_demux );
767 if ( !p_segment->Select( 0 ) )
769 msg_Err( p_demux, "cannot use the segment" );
780 /*****************************************************************************
781 * Close: frees unused data
782 *****************************************************************************/
783 static void Close( vlc_object_t *p_this )
785 demux_t *p_demux = (demux_t*)p_this;
786 demux_sys_t *p_sys = p_demux->p_sys;
787 matroska_stream_t *p_stream = p_sys->Stream();
788 matroska_segment_t *p_segment = p_stream->Segment();
790 /* TODO close everything ? */
792 delete p_segment->segment;
797 /*****************************************************************************
799 *****************************************************************************/
800 static int Control( demux_t *p_demux, int i_query, va_list args )
802 demux_sys_t *p_sys = p_demux->p_sys;
803 matroska_stream_t *p_stream = p_sys->Stream();
804 matroska_segment_t *p_segment = p_stream->Segment();
809 vlc_meta_t **pp_meta;
814 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
815 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
818 case DEMUX_GET_LENGTH:
819 pi64 = (int64_t*)va_arg( args, int64_t * );
820 if( p_segment && p_segment->f_duration > 0.0 )
822 *pi64 = (int64_t)(p_segment->f_duration * 1000);
827 case DEMUX_GET_POSITION:
828 pf = (double*)va_arg( args, double * );
829 if ( p_segment && p_segment->f_duration > 0.0 )
830 *pf = (double)p_sys->i_pts / (1000.0 * p_segment->f_duration);
833 case DEMUX_SET_POSITION:
834 f = (double)va_arg( args, double );
835 Seek( p_demux, -1, f, NULL );
839 pi64 = (int64_t*)va_arg( args, int64_t * );
840 *pi64 = p_sys->i_pts;
843 case DEMUX_GET_TITLE_INFO:
844 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
846 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
847 int *pi_int = (int*)va_arg( args, int* );
850 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
852 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
858 case DEMUX_SET_TITLE:
859 /* TODO handle editions as titles & DVD titles as well */
860 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
866 case DEMUX_SET_SEEKPOINT:
867 /* FIXME do a better implementation */
868 i_skp = (int)va_arg( args, int );
870 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
872 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
873 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
874 p_demux->info.i_seekpoint = i_skp;
886 int matroska_segment_t::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
897 if( sys.demuxer.b_die )
903 i_level = ep->GetLevel();
905 if( el == NULL && *pp_block != NULL )
907 /* update the index */
908 #define idx index[i_index - 1]
909 if( i_index > 0 && idx.i_time == -1 )
911 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
912 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
920 if( ep->GetLevel() > 1 )
925 msg_Warn( &sys.demuxer, "EOF" );
932 if( MKV_IS_ID( el, KaxCluster ) )
934 cluster = (KaxCluster*)el;
936 /* add it to the index */
938 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
940 IndexAppendCluster( cluster );
943 // reset silent tracks
944 for (size_t i=0; i<tracks.size(); i++)
946 tracks[i]->b_silent = VLC_FALSE;
951 else if( MKV_IS_ID( el, KaxCues ) )
953 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
958 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
961 else if( i_level == 2 )
963 if( MKV_IS_ID( el, KaxClusterTimecode ) )
965 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
967 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
968 cluster->InitTimecode( uint64( ctc ), i_timescale );
970 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
974 else if( MKV_IS_ID( el, KaxBlockGroup ) )
979 else if( i_level == 3 )
981 if( MKV_IS_ID( el, KaxBlock ) )
983 *pp_block = (KaxBlock*)el;
985 (*pp_block)->ReadData( es.I_O() );
986 (*pp_block)->SetParent( *cluster );
990 else if( MKV_IS_ID( el, KaxBlockDuration ) )
992 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
994 dur.ReadData( es.I_O() );
995 *pi_duration = uint64( dur );
997 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
999 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1001 ref.ReadData( es.I_O() );
1002 if( *pi_ref1 == -1 )
1004 *pi_ref1 = int64( ref );
1008 *pi_ref2 = int64( ref );
1011 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1013 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1014 track_num.ReadData( es.I_O() );
1016 for (size_t i=0; i<tracks.size(); i++)
1018 if ( tracks[i]->i_number == uint32(track_num))
1020 tracks[i]->b_silent = VLC_TRUE;
1028 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1029 return VLC_EGENERIC;
1034 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1037 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1038 memcpy( p_block->p_buffer, p_mem, i_mem );
1039 //p_block->i_rate = p_input->stream.control.i_rate;
1043 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1044 mtime_t i_duration )
1046 demux_sys_t *p_sys = p_demux->p_sys;
1047 matroska_stream_t *p_stream = p_sys->Stream();
1048 matroska_segment_t *p_segment = p_stream->Segment();
1054 #define tk p_segment->tracks[i_track]
1055 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1057 if( tk->i_number == block->TrackNum() )
1063 if( i_track >= p_segment->tracks.size() )
1065 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1068 if( tk->p_es == NULL )
1070 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1073 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1075 return; /* discard audio packets that shouldn't be rendered */
1078 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1081 tk->b_inited = VLC_FALSE;
1085 /* First send init data */
1086 if( !tk->b_inited && tk->i_data_init > 0 )
1090 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1091 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1092 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1094 tk->b_inited = VLC_TRUE;
1097 for( i = 0; i < block->NumberFrames(); i++ )
1100 DataBuffer &data = block->GetBuffer(i);
1102 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1104 if( p_block == NULL )
1109 #if defined(HAVE_ZLIB_H)
1110 if( tk->i_compression_type )
1112 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1116 // TODO implement correct timestamping when B frames are used
1117 if( tk->fmt.i_cat != VIDEO_ES )
1119 p_block->i_dts = p_block->i_pts = i_pts;
1123 p_block->i_dts = i_pts;
1127 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1129 p_block->i_length = i_duration * 1000;
1131 msg_Warn( p_demux, "Sending block %d", p_block );
1132 es_out_Send( p_demux->out, tk->p_es, p_block );
1134 /* use time stamp only for first block */
1141 matroska_stream_t *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1143 int i_upper_lvl = 0;
1145 EbmlElement *p_l0, *p_l1, *p_l2;
1146 bool b_keep_stream = false, b_keep_segment;
1148 // verify the EBML Header
1149 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1154 p_l0->SkipData(*p_estream, EbmlHead_Context);
1157 // find all segments in this file
1158 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1164 matroska_stream_t *p_stream1 = new matroska_stream_t( *this );
1168 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1171 matroska_segment_t *p_segment1 = new matroska_segment_t( *this, *p_estream );
1172 b_keep_segment = false;
1174 ep = new EbmlParser(p_estream, p_l0);
1175 p_segment1->ep = ep;
1176 p_segment1->segment = (KaxSegment*)p_l0;
1178 while ((p_l1 = ep->Get()))
1180 if (MKV_IS_ID(p_l1, KaxInfo))
1182 // find the families of this segment
1183 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1185 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1186 for( i = 0; i < p_info->ListSize(); i++ )
1188 EbmlElement *l = (*p_info)[i];
1190 if( MKV_IS_ID( l, KaxSegmentUID ) )
1192 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1193 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1194 if ( !b_keep_segment )
1195 break; // this segment is already known
1196 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1198 else if( MKV_IS_ID( l, KaxPrevUID ) )
1200 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1202 else if( MKV_IS_ID( l, KaxNextUID ) )
1204 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1206 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1208 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1209 std::vector<KaxSegmentFamily>::iterator iter;
1210 p_segment1->families.push_back( *p_fam );
1216 if ( b_keep_segment )
1218 b_keep_stream = true;
1219 p_stream1->segments.push_back( p_segment1 );
1225 p_l0->SkipData(*p_estream, EbmlHead_Context);
1226 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1229 if ( !b_keep_stream )
1238 bool matroska_segment_t::Select( mtime_t i_start_time )
1243 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1244 for( i_track = 0; i_track < tracks.size(); i_track++ )
1246 #define tk tracks[i_track]
1247 if( tk->fmt.i_cat == UNKNOWN_ES )
1249 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tk->i_number );
1254 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1256 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1258 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1259 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1263 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
1265 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1266 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1267 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1269 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1270 if( tk->fmt.i_extra > 0 )
1272 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1273 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
1277 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
1278 !strcmp( tk->psz_codec, "V_MPEG2" ) )
1280 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1282 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
1284 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
1286 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1288 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1290 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1291 tk->fmt.b_packetized = VLC_FALSE;
1292 tk->fmt.i_extra = tk->i_extra_data;
1293 tk->fmt.p_extra = malloc( tk->i_extra_data );
1294 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1298 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1301 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
1303 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1304 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1307 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1308 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1309 tk->fmt.i_codec = p_box->i_type;
1310 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1311 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1312 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1313 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1314 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
1315 MP4_FreeBox_sample_vide( p_box );
1316 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
1318 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
1320 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1322 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1323 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1327 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
1329 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
1331 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1332 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1333 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1334 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1335 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1337 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1338 if( tk->fmt.i_extra > 0 )
1340 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1341 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
1345 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
1346 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
1347 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
1349 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1351 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
1353 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1355 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
1357 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1359 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
1361 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1362 tk->fmt.i_extra = tk->i_extra_data;
1363 tk->fmt.p_extra = malloc( tk->i_extra_data );
1364 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1366 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
1368 int i, i_offset = 1, i_size[3], i_extra;
1371 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1373 /* Split the 3 headers */
1374 if( tk->p_extra_data[0] != 0x02 )
1375 msg_Err( &sys.demuxer, "invalid vorbis header" );
1377 for( i = 0; i < 2; i++ )
1380 while( i_offset < tk->i_extra_data )
1382 i_size[i] += tk->p_extra_data[i_offset];
1383 if( tk->p_extra_data[i_offset++] != 0xff ) break;
1387 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
1388 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
1389 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
1391 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
1392 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1393 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
1394 for( i = 0; i < 3; i++ )
1396 *(p_extra++) = i_size[i] >> 8;
1397 *(p_extra++) = i_size[i] & 0xFF;
1398 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
1400 p_extra += i_size[i];
1401 i_extra += i_size[i];
1404 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1405 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1407 int i_profile, i_srate;
1408 static unsigned int i_sample_rates[] =
1410 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1411 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1414 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1415 /* create data for faad (MP4DecSpecificDescrTag)*/
1417 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
1421 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
1425 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
1434 for( i_srate = 0; i_srate < 13; i_srate++ )
1436 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
1441 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1443 tk->fmt.i_extra = 2;
1444 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1445 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1446 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
1448 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
1449 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
1450 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1452 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
1454 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1458 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1460 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
1462 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
1464 /* FIXME: support this codec */
1465 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
1466 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1468 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
1470 /* FIXME: support this codec */
1471 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
1472 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1474 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1476 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1477 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1479 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1480 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1481 !strcmp( tk->psz_codec, "S_SSA" ) ||
1482 !strcmp( tk->psz_codec, "S_ASS" ))
1484 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1485 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1487 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1489 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1490 if( tk->i_extra_data )
1493 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1494 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1495 p_buf[tk->i_extra_data] = '\0';
1497 p_start = strstr( p_buf, "size:" );
1498 if( sscanf( p_start, "size: %dx%d",
1499 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1501 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tk->fmt.subs.spu.i_original_frame_width, tk->fmt.subs.spu.i_original_frame_height );
1505 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1510 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1512 /* FIXME: support this codec */
1513 msg_Err( &sys.demuxer, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1514 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1518 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tk->psz_codec );
1519 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1523 tk->fmt.i_priority = 1000;
1526 tk->p_es = es_out_Add( sys.demuxer.out, &tk->fmt );
1528 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_start_time );
1532 sys.i_start_pts = i_start_time;
1535 // reset the stream reading to the first cluster of the segment used
1536 es.I_O().setFilePointer( i_start_pos );
1541 void matroska_segment_t::UnSelect( )
1545 for( i_track = 0; i_track < tracks.size(); i_track++ )
1547 #define tk tracks[i_track]
1548 if ( tk->p_es != NULL )
1550 es_out_Del( sys.demuxer.out, tk->p_es );
1557 static void UpdateCurrentToChapter( demux_t & demux )
1559 demux_sys_t & sys = *demux.p_sys;
1560 matroska_stream_t *p_stream = sys.Stream();
1561 matroska_segment_t *p_segment = p_stream->Segment();
1562 const chapter_item_t *psz_curr_chapter;
1564 /* update current chapter/seekpoint */
1565 if ( p_segment->editions.size())
1567 /* 1st, we need to know in which chapter we are */
1568 psz_curr_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( sys.i_pts );
1570 /* we have moved to a new chapter */
1571 if (p_segment->psz_current_chapter != NULL && psz_curr_chapter != NULL && p_segment->psz_current_chapter != psz_curr_chapter)
1573 if (p_segment->psz_current_chapter->i_seekpoint_num != psz_curr_chapter->i_seekpoint_num && psz_curr_chapter->i_seekpoint_num > 0)
1575 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1576 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1579 if (p_segment->editions[p_segment->i_current_edition].b_ordered )
1581 /* TODO check if we need to silently seek to a new location in the stream (switch to another chapter) */
1582 if (p_segment->psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time)
1583 Seek(&demux, sys.i_pts, -1, psz_curr_chapter);
1584 /* count the last duration time found for each track in a table (-1 not found, -2 silent) */
1585 /* only seek after each duration >= end timecode of the current chapter */
1588 // p_segment->i_user_time = psz_curr_chapter->i_user_start_time - psz_curr_chapter->i_start_time;
1589 // p_segment->i_start_pts = psz_curr_chapter->i_user_start_time;
1591 p_segment->psz_current_chapter = psz_curr_chapter;
1595 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter)
1597 demux_sys_t *p_sys = p_demux->p_sys;
1598 matroska_stream_t *p_stream = p_sys->Stream();
1599 matroska_segment_t *p_segment = p_stream->Segment();
1600 mtime_t i_time_offset = 0;
1603 int64_t i_block_duration;
1604 int64_t i_block_ref1;
1605 int64_t i_block_ref2;
1608 int i_track_skipping;
1611 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
1612 if( i_date < 0 && f_percent < 0 )
1614 msg_Warn( p_demux, "cannot seek nowhere !" );
1617 if( f_percent > 1.0 )
1619 msg_Warn( p_demux, "cannot seek so far !" );
1623 delete p_segment->ep;
1624 p_segment->ep = new EbmlParser( p_stream->p_es, p_segment->segment );
1625 p_segment->cluster = NULL;
1627 /* seek without index or without date */
1628 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
1630 if (p_segment->f_duration >= 0)
1632 i_date = int64_t( f_percent * p_segment->f_duration * 1000.0 );
1636 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
1638 msg_Dbg( p_demux, "inacurate way of seeking" );
1639 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
1641 if( p_segment->index[i_index].i_position >= i_pos)
1646 if( i_index == p_segment->i_index )
1651 i_date = p_segment->index[i_index].i_time;
1654 if( p_segment->index[i_index].i_position < i_pos )
1658 msg_Warn( p_demux, "searching for cluster, could take some time" );
1660 /* search a cluster */
1661 while( ( el = p_sys->ep->Get() ) != NULL )
1663 if( MKV_IS_ID( el, KaxCluster ) )
1665 KaxCluster *cluster = (KaxCluster*)el;
1667 /* add it to the index */
1668 p_segment->IndexAppendCluster( cluster );
1670 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1672 p_sys->cluster = cluster;
1683 // find the actual time for an ordered edition
1684 if ( psz_chapter == NULL )
1686 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1688 /* 1st, we need to know in which chapter we are */
1689 psz_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( i_date );
1693 if ( psz_chapter != NULL )
1695 p_segment->psz_current_chapter = psz_chapter;
1696 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
1697 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1698 p_demux->info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
1701 for( ; i_index < p_segment->i_index; i_index++ )
1703 if( p_segment->index[i_index].i_time + i_time_offset > i_date )
1714 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1715 p_segment->index[i_index].i_time,
1716 (int)( 100 * p_segment->index[i_index].i_position /
1717 stream_Size( p_demux->s ) ) );
1719 p_stream->p_in->setFilePointer( p_segment->index[i_index].i_position,
1722 p_sys->i_start_pts = i_date;
1724 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1726 /* now parse until key frame */
1727 #define tk p_segment->tracks[i_track]
1728 i_track_skipping = 0;
1729 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1731 if( tk->fmt.i_cat == VIDEO_ES )
1733 tk->b_search_keyframe = VLC_TRUE;
1736 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
1740 while( i_track_skipping > 0 )
1742 if( p_segment->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1744 msg_Warn( p_demux, "cannot get block EOF?" );
1749 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1751 if( tk->i_number == block->TrackNum() )
1757 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1759 if( i_track < p_segment->tracks.size() )
1761 if( p_sys->i_pts >= p_sys->i_start_pts )
1763 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1764 i_track_skipping = 0;
1766 else if( tk->fmt.i_cat == VIDEO_ES )
1768 if( i_block_ref1 == -1 && tk->b_search_keyframe )
1770 tk->b_search_keyframe = VLC_FALSE;
1773 if( !tk->b_search_keyframe )
1775 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1785 /*****************************************************************************
1786 * Demux: reads and demuxes data packets
1787 *****************************************************************************
1788 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1789 *****************************************************************************/
1790 static int Demux( demux_t *p_demux)
1792 demux_sys_t *p_sys = p_demux->p_sys;
1793 matroska_stream_t *p_stream = p_sys->Stream();
1794 matroska_segment_t *p_segment = p_stream->Segment();
1795 int i_block_count = 0;
1798 int64_t i_block_duration;
1799 int64_t i_block_ref1;
1800 int64_t i_block_ref2;
1804 if( p_sys->i_pts >= p_sys->i_start_pts )
1805 UpdateCurrentToChapter( *p_demux );
1807 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered && p_segment->psz_current_chapter == NULL )
1809 /* nothing left to read in this ordered edition */
1810 if ( p_stream->i_current_segment == p_stream->segments.size() - 1)
1812 p_segment->UnSelect( );
1814 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1816 /* switch to the next segment (TODO update the duration) */
1817 p_stream->i_current_segment++;
1818 p_segment = p_stream->Segment();
1819 if ( !p_segment || !p_segment->Select( 0 ) )
1824 if( p_segment->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1826 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1828 // check if there are more chapters to read
1829 if ( p_segment->psz_current_chapter != NULL )
1831 p_sys->i_pts = p_segment->psz_current_chapter->i_user_end_time;
1837 msg_Warn( p_demux, "cannot get block EOF?" );
1838 p_segment->UnSelect( );
1840 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1842 /* switch to the next segment (TODO update the duration) */
1843 p_stream->i_current_segment++;
1844 p_segment = p_stream->Segment();
1845 if ( !p_segment || !p_segment->Select( 0 ) )
1851 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1853 if( p_sys->i_pts >= p_sys->i_start_pts )
1855 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1858 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1863 // TODO optimize when there is need to leave or when seeking has been called
1864 if( i_block_count > 5 )
1873 /*****************************************************************************
1875 *****************************************************************************/
1876 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1882 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1884 if( i_size <= 0 || mb_eof )
1889 return stream_Read( s, p_buffer, i_size );
1891 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1897 case seek_beginning:
1901 i_pos = stream_Size( s ) - i_offset;
1904 i_pos= stream_Tell( s ) + i_offset;
1908 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1915 if( stream_Seek( s, i_pos ) )
1921 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1925 uint64 vlc_stream_io_callback::getFilePointer( void )
1927 return stream_Tell( s );
1929 void vlc_stream_io_callback::close( void )
1935 /*****************************************************************************
1936 * Ebml Stream parser
1937 *****************************************************************************/
1938 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1946 for( i = 1; i < 6; i++ )
1952 mb_keep = VLC_FALSE;
1955 EbmlParser::~EbmlParser( void )
1959 for( i = 1; i < mi_level; i++ )
1965 mb_keep = VLC_FALSE;
1969 void EbmlParser::Up( void )
1971 if( mi_user_level == mi_level )
1973 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1979 void EbmlParser::Down( void )
1985 void EbmlParser::Keep( void )
1990 int EbmlParser::GetLevel( void )
1992 return mi_user_level;
1995 void EbmlParser::Reset( void )
1997 while ( mi_level > 0)
1999 delete m_el[mi_level];
2000 m_el[mi_level] = NULL;
2003 mi_user_level = mi_level = 1;
2004 #if LIBEBML_VERSION >= 0x000704
2005 // a little faster and cleaner
2006 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2008 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2012 EbmlElement *EbmlParser::Get( void )
2016 if( mi_user_level != mi_level )
2022 EbmlElement *ret = m_got;
2028 if( m_el[mi_level] )
2030 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2033 delete m_el[mi_level];
2035 mb_keep = VLC_FALSE;
2038 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
2049 delete m_el[mi_level - 1];
2050 m_got = m_el[mi_level -1] = m_el[mi_level];
2051 m_el[mi_level] = NULL;
2058 else if( m_el[mi_level] == NULL )
2060 fprintf( stderr," m_el[mi_level] == NULL\n" );
2063 return m_el[mi_level];
2067 /*****************************************************************************
2069 * * LoadCues : load the cues element and update index
2071 * * LoadTags : load ... the tags element
2073 * * InformationCreate : create all information, load tags if present
2075 *****************************************************************************/
2076 static void LoadCues( demux_t *p_demux )
2078 demux_sys_t *p_sys = p_demux->p_sys;
2079 matroska_stream_t *p_stream = p_sys->Stream();
2080 matroska_segment_t *p_segment = p_stream->Segment();
2081 int64_t i_sav_position = p_stream->p_in->getFilePointer();
2083 EbmlElement *el, *cues;
2085 msg_Dbg( p_demux, "loading cues" );
2086 p_stream->p_in->setFilePointer( p_segment->i_cues_position, seek_beginning );
2087 cues = p_stream->p_es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2091 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
2092 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2096 ep = new EbmlParser( p_stream->p_es, cues );
2097 while( ( el = ep->Get() ) != NULL )
2099 if( MKV_IS_ID( el, KaxCuePoint ) )
2101 #define idx p_segment->index[p_segment->i_index]
2104 idx.i_block_number= -1;
2105 idx.i_position = -1;
2107 idx.b_key = VLC_TRUE;
2110 while( ( el = ep->Get() ) != NULL )
2112 if( MKV_IS_ID( el, KaxCueTime ) )
2114 KaxCueTime &ctime = *(KaxCueTime*)el;
2116 ctime.ReadData( p_stream->p_es->I_O() );
2118 idx.i_time = uint64( ctime ) * p_segment->i_timescale / (mtime_t)1000;
2120 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2123 while( ( el = ep->Get() ) != NULL )
2125 if( MKV_IS_ID( el, KaxCueTrack ) )
2127 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2129 ctrack.ReadData( p_stream->p_es->I_O() );
2130 idx.i_track = uint16( ctrack );
2132 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2134 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2136 ccpos.ReadData( p_stream->p_es->I_O() );
2137 idx.i_position = p_segment->segment->GetGlobalPosition( uint64( ccpos ) );
2139 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2141 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2143 cbnum.ReadData( p_stream->p_es->I_O() );
2144 idx.i_block_number = uint32( cbnum );
2148 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2155 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2161 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
2162 " track=%d bnum=%d", idx.i_time, idx.i_position,
2163 idx.i_track, idx.i_block_number );
2166 p_segment->i_index++;
2167 if( p_segment->i_index >= p_segment->i_index_max )
2169 p_segment->i_index_max += 1024;
2170 p_segment->index = (mkv_index_t*)realloc( p_segment->index, sizeof( mkv_index_t ) * p_segment->i_index_max );
2176 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2182 p_segment->b_cues = VLC_TRUE;
2184 msg_Dbg( p_demux, "loading cues done." );
2185 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2188 static void LoadTags( demux_t *p_demux )
2190 demux_sys_t *p_sys = p_demux->p_sys;
2191 matroska_stream_t *p_stream = p_sys->Stream();
2192 matroska_segment_t *p_segment = p_stream->Segment();
2193 int64_t i_sav_position = p_stream->p_in->getFilePointer();
2195 EbmlElement *el, *tags;
2197 msg_Dbg( p_demux, "loading tags" );
2198 p_stream->p_in->setFilePointer( p_segment->i_tags_position, seek_beginning );
2199 tags = p_stream->p_es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2203 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
2204 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2208 msg_Dbg( p_demux, "Tags" );
2209 ep = new EbmlParser( p_stream->p_es, tags );
2210 while( ( el = ep->Get() ) != NULL )
2212 if( MKV_IS_ID( el, KaxTag ) )
2214 msg_Dbg( p_demux, "+ Tag" );
2216 while( ( el = ep->Get() ) != NULL )
2218 if( MKV_IS_ID( el, KaxTagTargets ) )
2220 msg_Dbg( p_demux, "| + Targets" );
2222 while( ( el = ep->Get() ) != NULL )
2224 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2228 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2230 msg_Dbg( p_demux, "| + General" );
2232 while( ( el = ep->Get() ) != NULL )
2234 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2238 else if( MKV_IS_ID( el, KaxTagGenres ) )
2240 msg_Dbg( p_demux, "| + Genres" );
2242 while( ( el = ep->Get() ) != NULL )
2244 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2248 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2250 msg_Dbg( p_demux, "| + Audio Specific" );
2252 while( ( el = ep->Get() ) != NULL )
2254 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2258 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2260 msg_Dbg( p_demux, "| + Images Specific" );
2262 while( ( el = ep->Get() ) != NULL )
2264 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2268 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2270 msg_Dbg( p_demux, "| + Multi Comment" );
2272 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2274 msg_Dbg( p_demux, "| + Multi Commercial" );
2276 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2278 msg_Dbg( p_demux, "| + Multi Date" );
2280 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2282 msg_Dbg( p_demux, "| + Multi Entity" );
2284 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2286 msg_Dbg( p_demux, "| + Multi Identifier" );
2288 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2290 msg_Dbg( p_demux, "| + Multi Legal" );
2292 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2294 msg_Dbg( p_demux, "| + Multi Title" );
2298 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
2305 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
2311 msg_Dbg( p_demux, "loading tags done." );
2312 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2315 /*****************************************************************************
2317 *****************************************************************************/
2318 void matroska_segment_t::ParseSeekHead( EbmlElement *seekhead )
2323 int i_upper_level = 0;
2325 msg_Dbg( &sys.demuxer, "| + Seek head" );
2327 /* Master elements */
2328 m = static_cast<EbmlMaster *>(seekhead);
2329 m->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2331 for( i = 0; i < m->ListSize(); i++ )
2333 EbmlElement *l = (*m)[i];
2335 if( MKV_IS_ID( l, KaxSeek ) )
2337 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2338 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2343 for( j = 0; j < sk->ListSize(); j++ )
2345 EbmlElement *l = (*sk)[j];
2347 if( MKV_IS_ID( l, KaxSeekID ) )
2349 KaxSeekID &sid = *(KaxSeekID*)l;
2350 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2352 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2354 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2355 i_pos = uint64( spos );
2359 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2365 if( id == KaxCues::ClassInfos.GlobalId )
2367 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2368 i_cues_position = segment->GetGlobalPosition( i_pos );
2370 else if( id == KaxChapters::ClassInfos.GlobalId )
2372 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2373 i_chapters_position = segment->GetGlobalPosition( i_pos );
2375 else if( id == KaxTags::ClassInfos.GlobalId )
2377 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2378 i_tags_position = segment->GetGlobalPosition( i_pos );
2384 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2389 /*****************************************************************************
2391 *****************************************************************************/
2392 void matroska_segment_t::ParseTrackEntry( EbmlMaster *m )
2398 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2400 tk = new mkv_track_t();
2401 tracks.push_back( tk );
2403 /* Init the track */
2404 memset( tk, 0, sizeof( mkv_track_t ) );
2406 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2407 tk->fmt.psz_language = strdup("English");
2408 tk->fmt.psz_description = NULL;
2410 tk->b_default = VLC_TRUE;
2411 tk->b_enabled = VLC_TRUE;
2412 tk->b_silent = VLC_FALSE;
2413 tk->i_number = tracks.size() - 1;
2414 tk->i_extra_data = 0;
2415 tk->p_extra_data = NULL;
2416 tk->psz_codec = NULL;
2417 tk->i_default_duration = 0;
2418 tk->f_timecodescale = 1.0;
2420 tk->b_inited = VLC_FALSE;
2421 tk->i_data_init = 0;
2422 tk->p_data_init = NULL;
2424 tk->psz_codec_name = NULL;
2425 tk->psz_codec_settings = NULL;
2426 tk->psz_codec_info_url = NULL;
2427 tk->psz_codec_download_url = NULL;
2429 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2431 for( i = 0; i < m->ListSize(); i++ )
2433 EbmlElement *l = (*m)[i];
2435 if( MKV_IS_ID( l, KaxTrackNumber ) )
2437 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2439 tk->i_number = uint32( tnum );
2440 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2442 else if( MKV_IS_ID( l, KaxTrackUID ) )
2444 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2446 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2448 else if( MKV_IS_ID( l, KaxTrackType ) )
2451 KaxTrackType &ttype = *(KaxTrackType*)l;
2453 switch( uint8(ttype) )
2457 tk->fmt.i_cat = AUDIO_ES;
2461 tk->fmt.i_cat = VIDEO_ES;
2463 case track_subtitle:
2464 psz_type = "subtitle";
2465 tk->fmt.i_cat = SPU_ES;
2468 psz_type = "unknown";
2469 tk->fmt.i_cat = UNKNOWN_ES;
2473 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2475 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2477 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2479 // tk->b_enabled = uint32( fenb );
2480 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2481 // uint32( fenb ) );
2483 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2485 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2487 tk->b_default = uint32( fdef );
2488 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2490 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2492 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2494 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2496 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2498 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2500 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2502 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2504 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2506 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2508 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2510 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2512 tk->i_default_duration = uint64(defd);
2513 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2515 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2517 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2519 tk->f_timecodescale = float( ttcs );
2520 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2522 else if( MKV_IS_ID( l, KaxTrackName ) )
2524 KaxTrackName &tname = *(KaxTrackName*)l;
2526 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2527 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2529 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2531 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2533 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2534 msg_Dbg( &sys.demuxer,
2535 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2537 else if( MKV_IS_ID( l, KaxCodecID ) )
2539 KaxCodecID &codecid = *(KaxCodecID*)l;
2541 tk->psz_codec = strdup( string( codecid ).c_str() );
2542 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2544 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2546 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2548 tk->i_extra_data = cpriv.GetSize();
2549 if( tk->i_extra_data > 0 )
2551 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2552 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2554 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2556 else if( MKV_IS_ID( l, KaxCodecName ) )
2558 KaxCodecName &cname = *(KaxCodecName*)l;
2560 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2561 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2563 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2565 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2566 MkvTree( sys.demuxer, 3, "Content Encodings" );
2567 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2569 EbmlElement *l2 = (*cencs)[i];
2570 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2572 MkvTree( sys.demuxer, 4, "Content Encoding" );
2573 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2574 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2576 EbmlElement *l3 = (*cenc)[i];
2577 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2579 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2580 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2582 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2584 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2585 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2587 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2589 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2590 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2592 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2594 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2595 MkvTree( sys.demuxer, 5, "Content Compression" );
2596 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2598 EbmlElement *l4 = (*compr)[i];
2599 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2601 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2602 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2603 if( uint32( compalg ) == 0 )
2605 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2610 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2617 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2624 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2629 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2631 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2633 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2634 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2636 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2638 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2640 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2641 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2643 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2645 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2647 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2648 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2650 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2652 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2654 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2656 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2658 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2660 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2662 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2664 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2667 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
2670 for( j = 0; j < tkv->ListSize(); j++ )
2672 EbmlElement *l = (*tkv)[j];
2673 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2675 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2677 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2679 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2681 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2683 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2686 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2688 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2690 tk->fmt.video.i_width = uint16( vwidth );
2691 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
2693 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2695 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2697 tk->fmt.video.i_height = uint16( vheight );
2698 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
2700 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2702 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2704 tk->fmt.video.i_visible_width = uint16( vwidth );
2705 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
2707 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2709 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2711 tk->fmt.video.i_visible_height = uint16( vheight );
2712 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
2714 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2716 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2718 tk->f_fps = float( vfps );
2719 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
2721 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2723 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2725 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
2726 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2728 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2730 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2732 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2734 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2736 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2738 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
2742 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2745 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
2746 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
2748 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2750 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2753 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
2755 for( j = 0; j < tka->ListSize(); j++ )
2757 EbmlElement *l = (*tka)[j];
2759 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2761 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2763 tk->fmt.audio.i_rate = (int)float( afreq );
2764 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2766 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2768 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2770 tk->fmt.audio.i_channels = uint8( achan );
2771 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
2773 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2775 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2777 tk->fmt.audio.i_bitspersample = uint8( abits );
2778 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
2782 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2788 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
2789 typeid(*l).name() );
2794 /*****************************************************************************
2796 *****************************************************************************/
2797 void matroska_segment_t::ParseTracks( EbmlElement *tracks )
2802 int i_upper_level = 0;
2804 msg_Dbg( &sys.demuxer, "| + Tracks" );
2806 /* Master elements */
2807 m = static_cast<EbmlMaster *>(tracks);
2808 m->Read( es, tracks->Generic().Context, i_upper_level, el, true );
2810 for( i = 0; i < m->ListSize(); i++ )
2812 EbmlElement *l = (*m)[i];
2814 if( MKV_IS_ID( l, KaxTrackEntry ) )
2816 ParseTrackEntry( static_cast<EbmlMaster *>(l) );
2820 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2825 /*****************************************************************************
2827 *****************************************************************************/
2828 void matroska_segment_t::ParseInfo( EbmlElement *info )
2833 int i_upper_level = 0;
2835 msg_Dbg( &sys.demuxer, "| + Information" );
2837 /* Master elements */
2838 m = static_cast<EbmlMaster *>(info);
2839 m->Read( es, info->Generic().Context, i_upper_level, el, true );
2841 for( i = 0; i < m->ListSize(); i++ )
2843 EbmlElement *l = (*m)[i];
2845 if( MKV_IS_ID( l, KaxSegmentUID ) )
2847 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
2849 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
2851 else if( MKV_IS_ID( l, KaxPrevUID ) )
2853 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
2855 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
2857 else if( MKV_IS_ID( l, KaxNextUID ) )
2859 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
2861 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
2863 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2865 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2867 i_timescale = uint64(tcs);
2869 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
2872 else if( MKV_IS_ID( l, KaxDuration ) )
2874 KaxDuration &dur = *(KaxDuration*)l;
2876 f_duration = float(dur);
2878 msg_Dbg( &sys.demuxer, "| | + Duration=%f",
2881 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2883 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2885 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2887 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
2888 psz_muxing_application );
2890 else if( MKV_IS_ID( l, KaxWritingApp ) )
2892 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2894 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2896 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
2897 psz_writing_application );
2899 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2901 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2903 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2905 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
2906 psz_segment_filename );
2908 else if( MKV_IS_ID( l, KaxTitle ) )
2910 KaxTitle &title = *(KaxTitle*)l;
2912 psz_title = UTF8ToStr( UTFstring( title ) );
2914 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
2916 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2918 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
2920 families.push_back(*uid);
2922 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
2924 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2925 else if( MKV_IS_ID( l, KaxDateUTC ) )
2927 KaxDateUTC &date = *(KaxDateUTC*)l;
2932 i_date = date.GetEpochDate();
2933 memset( buffer, 0, 256 );
2934 if( gmtime_r( &i_date, &tmres ) &&
2935 asctime_r( &tmres, buffer ) )
2937 buffer[strlen( buffer)-1]= '\0';
2938 psz_date_utc = strdup( buffer );
2939 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
2945 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2949 f_duration *= i_timescale / 1000000.0;
2953 /*****************************************************************************
2955 *****************************************************************************/
2956 void matroska_segment_t::ParseChapterAtom( int i_level, EbmlMaster *ca, chapter_item_t & chapters )
2960 if( sys.title == NULL )
2962 sys.title = vlc_input_title_New();
2965 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
2966 for( i = 0; i < ca->ListSize(); i++ )
2968 EbmlElement *l = (*ca)[i];
2970 if( MKV_IS_ID( l, KaxChapterUID ) )
2972 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
2973 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
2975 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
2977 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
2978 chapters.b_display_seekpoint = uint8( flag ) == 0;
2980 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
2982 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2984 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2985 chapters.i_start_time = uint64( start ) / I64C(1000);
2987 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
2989 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2991 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2992 chapters.i_end_time = uint64( end ) / I64C(1000);
2994 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
2996 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2998 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3001 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3002 for( j = 0; j < cd->ListSize(); j++ )
3004 EbmlElement *l= (*cd)[j];
3006 if( MKV_IS_ID( l, KaxChapterString ) )
3010 KaxChapterString &name =*(KaxChapterString*)l;
3011 for (k = 0; k < i_level; k++)
3012 chapters.psz_name += '+';
3013 chapters.psz_name += ' ';
3014 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3016 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3018 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3020 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3021 const char *psz = string( lang ).c_str();
3023 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3025 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3027 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3028 const char *psz = string( ct ).c_str();
3030 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3034 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3036 chapter_item_t new_sub_chapter;
3037 ParseChapterAtom( i_level+1, static_cast<EbmlMaster *>(l), new_sub_chapter );
3038 new_sub_chapter.psz_parent = &chapters;
3039 chapters.sub_chapters.push_back( new_sub_chapter );
3044 /*****************************************************************************
3046 *****************************************************************************/
3047 void matroska_segment_t::ParseChapters( EbmlElement *chapters )
3052 int i_upper_level = 0;
3053 int i_default_edition = 0;
3056 /* Master elements */
3057 m = static_cast<EbmlMaster *>(chapters);
3058 m->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3060 for( i = 0; i < m->ListSize(); i++ )
3062 EbmlElement *l = (*m)[i];
3064 if( MKV_IS_ID( l, KaxEditionEntry ) )
3066 chapter_edition_t edition;
3068 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3070 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3071 for( j = 0; j < E->ListSize(); j++ )
3073 EbmlElement *l = (*E)[j];
3075 if( MKV_IS_ID( l, KaxChapterAtom ) )
3077 chapter_item_t new_sub_chapter;
3078 ParseChapterAtom( 0, static_cast<EbmlMaster *>(l), new_sub_chapter );
3079 edition.chapters.push_back( new_sub_chapter );
3081 else if( MKV_IS_ID( l, KaxEditionUID ) )
3083 edition.i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3085 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3087 edition.b_ordered = uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0;
3089 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3091 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3092 i_default_edition = editions.size();
3096 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3099 editions.push_back( edition );
3103 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3107 for( i = 0; i < editions.size(); i++ )
3109 editions[i].RefreshChapters( *sys.title );
3112 i_current_edition = i_default_edition;
3114 if ( editions[i_default_edition].b_ordered )
3116 /* update the duration of the segment according to the sum of all sub chapters */
3117 f_dur = editions[i_default_edition].Duration() / I64C(1000);
3123 /*****************************************************************************
3124 * InformationCreate:
3125 *****************************************************************************/
3126 static void InformationCreate( demux_t *p_demux )
3128 demux_sys_t *p_sys = p_demux->p_sys;
3129 matroska_stream_t *p_stream = p_sys->Stream();
3130 matroska_segment_t *p_segment = p_stream->Segment();
3133 p_sys->meta = vlc_meta_New();
3135 if( p_segment->psz_title )
3137 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_segment->psz_title );
3139 if( p_segment->psz_date_utc )
3141 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_segment->psz_date_utc );
3143 if( p_segment->psz_segment_filename )
3145 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_segment->psz_segment_filename );
3147 if( p_segment->psz_muxing_application )
3149 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_segment->psz_muxing_application );
3151 if( p_segment->psz_writing_application )
3153 vlc_meta_Add( p_sys->meta, _("Writing application"), p_segment->psz_writing_application );
3156 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
3158 mkv_track_t *tk = p_segment->tracks[i_track];
3159 vlc_meta_t *mtk = vlc_meta_New();
3161 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
3162 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
3163 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
3165 if( tk->fmt.psz_description )
3167 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3169 if( tk->psz_codec_name )
3171 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3173 if( tk->psz_codec_settings )
3175 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
3177 if( tk->psz_codec_info_url )
3179 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3181 if( tk->psz_codec_download_url )
3183 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
3187 if( p_segment->i_tags_position >= 0 )
3189 vlc_bool_t b_seekable;
3191 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
3194 LoadTags( p_demux );
3200 /*****************************************************************************
3202 *****************************************************************************/
3204 void matroska_segment_t::IndexAppendCluster( KaxCluster *cluster )
3206 #define idx index[i_index]
3208 idx.i_block_number= -1;
3209 idx.i_position = cluster->GetElementPosition();
3211 idx.b_key = VLC_TRUE;
3214 if( i_index >= i_index_max )
3216 i_index_max += 1024;
3217 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3222 static char * UTF8ToStr( const UTFstring &u )
3231 p = dst = (char*)malloc( i_src + 1);
3250 void chapter_edition_t::RefreshChapters( input_title_t & title )
3252 int64_t i_prev_user_time = 0;
3253 std::vector<chapter_item_t>::iterator index = chapters.begin();
3255 while ( index != chapters.end() )
3257 i_prev_user_time = (*index).RefreshChapters( b_ordered, i_prev_user_time, title );
3262 int64_t chapter_item_t::RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title )
3264 int64_t i_user_time = i_prev_user_time;
3266 // first the sub-chapters, and then ourself
3267 std::vector<chapter_item_t>::iterator index = sub_chapters.begin();
3268 while ( index != sub_chapters.end() )
3270 i_user_time = (*index).RefreshChapters( b_ordered, i_user_time, title );
3276 i_user_start_time = i_prev_user_time;
3277 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3279 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3283 i_user_end_time = i_user_time;
3288 std::sort( sub_chapters.begin(), sub_chapters.end() );
3289 i_user_start_time = i_start_time;
3290 i_user_end_time = i_end_time;
3293 if (b_display_seekpoint)
3295 seekpoint_t *sk = vlc_seekpoint_New();
3297 // sk->i_level = i_level;
3298 sk->i_time_offset = i_start_time;
3299 sk->psz_name = strdup( psz_name.c_str() );
3301 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3302 title.i_seekpoint++;
3303 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3304 title.seekpoint[title.i_seekpoint-1] = sk;
3307 i_seekpoint_num = title.i_seekpoint;
3309 return i_user_end_time;
3312 double chapter_edition_t::Duration() const
3314 double f_result = 0.0;
3316 if ( chapters.size() )
3318 std::vector<chapter_item_t>::const_iterator index = chapters.end();
3320 f_result = (*index).i_user_end_time;
3326 const chapter_item_t *chapter_item_t::FindTimecode( mtime_t i_user_timecode ) const
3328 const chapter_item_t *psz_result = NULL;
3330 if (i_user_timecode >= i_user_start_time && i_user_timecode < i_user_end_time)
3332 std::vector<chapter_item_t>::const_iterator index = sub_chapters.begin();
3333 while ( index != sub_chapters.end() && psz_result == NULL )
3335 psz_result = (*index).FindTimecode( i_user_timecode );
3339 if ( psz_result == NULL )
3346 const chapter_item_t *chapter_edition_t::FindTimecode( mtime_t i_user_timecode ) const
3348 const chapter_item_t *psz_result = NULL;
3350 std::vector<chapter_item_t>::const_iterator index = chapters.begin();
3351 while ( index != chapters.end() && psz_result == NULL )
3353 psz_result = (*index).FindTimecode( i_user_timecode );
3360 void demux_sys_t::PreloadFamily( )
3362 matroska_stream_t *p_stream = Stream();
3365 matroska_segment_t *p_segment = p_stream->Segment();
3368 for (size_t i=0; i<streams.size(); i++)
3370 streams[i]->PreloadFamily( *p_segment );
3376 void matroska_stream_t::PreloadFamily( const matroska_segment_t & of_segment )
3378 for (size_t i=0; i<segments.size(); i++)
3380 segments[i]->PreloadFamily( of_segment );
3384 bool matroska_segment_t::PreloadFamily( const matroska_segment_t & of_segment )
3389 for (size_t i=0; i<families.size(); i++)
3391 for (size_t j=0; j<of_segment.families.size(); j++)
3393 if ( families[i] == of_segment.families[j] )
3401 // preload all the linked segments for all preloaded segments
3402 void demux_sys_t::PreloadLinked( )
3404 size_t i_prealoaded;
3407 for (size_t i=0; i<streams.size(); i++)
3409 i_prealoaded += streams[i]->PreloadLinked( *this );
3411 } while ( i_prealoaded ); // worst case: will stop when all segments are preloaded
3414 size_t matroska_stream_t::PreloadLinked( const demux_sys_t & of_sys )
3416 size_t i_result = 0;
3417 for (size_t i=0; i<segments.size(); i++)
3419 i_result += segments[i]->PreloadLinked( of_sys );
3424 size_t matroska_segment_t::PreloadLinked( const demux_sys_t & of_sys )
3426 size_t i_result = 0;
3427 if ( prev_segment_uid.GetBuffer() )
3429 matroska_segment_t *p_segment = of_sys.FindSegment( prev_segment_uid );
3432 i_result += p_segment->Preload( ) ? 1 : 0;
3435 if ( next_segment_uid.GetBuffer() )
3437 matroska_segment_t *p_segment = of_sys.FindSegment( next_segment_uid );
3440 i_result += p_segment->Preload( ) ? 1 : 0;
3446 bool matroska_segment_t::Preload( )
3451 EbmlElement *el = NULL;
3455 while( ( el = ep->Get() ) != NULL )
3457 if( MKV_IS_ID( el, KaxInfo ) )
3461 else if( MKV_IS_ID( el, KaxTracks ) )
3465 else if( MKV_IS_ID( el, KaxSeekHead ) )
3467 ParseSeekHead( el );
3469 else if( MKV_IS_ID( el, KaxCues ) )
3471 msg_Dbg( &sys.demuxer, "| + Cues" );
3473 else if( MKV_IS_ID( el, KaxCluster ) )
3475 msg_Dbg( &sys.demuxer, "| + Cluster" );
3477 cluster = (KaxCluster*)el;
3479 i_start_pos = cluster->GetElementPosition();
3482 /* stop parsing the stream */
3485 else if( MKV_IS_ID( el, KaxAttachments ) )
3487 msg_Dbg( &sys.demuxer, "| + Attachments FIXME TODO (but probably never supported)" );
3489 else if( MKV_IS_ID( el, KaxChapters ) )
3491 msg_Dbg( &sys.demuxer, "| + Chapters" );
3492 ParseChapters( el );
3494 else if( MKV_IS_ID( el, KaxTag ) )
3496 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3500 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3509 matroska_segment_t *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3511 matroska_segment_t *p_segment = NULL;
3512 for (size_t i=0; i<streams.size() && p_segment == NULL; i++)
3514 p_segment = streams[i]->FindSegment( uid );
3519 matroska_segment_t *matroska_stream_t::FindSegment( const EbmlBinary & uid ) const
3521 for (size_t i=0; i<segments.size(); i++)
3523 if ( segments[i]->segment_uid == uid )