X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdemux%2Fmkv.cpp;h=24386dde47abd5100e9424b52fa3bcd93f353e84;hb=11d6a0719f60e769c3f5f72f879b70607aa5e82e;hp=8d79b005601f1b7a251fd0260828c67572f3aef0;hpb=bf73ff64ecf2159aabe69d061fedde8e4e268f7b;p=vlc diff --git a/modules/demux/mkv.cpp b/modules/demux/mkv.cpp index 8d79b00560..24386dde47 100644 --- a/modules/demux/mkv.cpp +++ b/modules/demux/mkv.cpp @@ -1,10 +1,11 @@ /***************************************************************************** * mkv.cpp : matroska demuxer ***************************************************************************** - * Copyright (C) 2001 VideoLAN - * $Id: mkv.cpp,v 1.18 2003/08/01 00:05:07 gbazin Exp $ + * Copyright (C) 2003-2004 VideoLAN + * $Id$ * * Authors: Laurent Aimar + * Steve Lhomme * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,13 +36,19 @@ #include #include /* BITMAPINFOHEADER, WAVEFORMATEX */ +#include "iso_lang.h" +#include "vlc_meta.h" #include #include #include +#include +#include -#ifdef HAVE_WCHAR_H -# include +#ifdef HAVE_DIRENT_H +# include +#else +# include "../../src/extras/dirent.h" #endif /* libebml and matroska */ @@ -49,11 +56,10 @@ #include "ebml/EbmlSubHead.h" #include "ebml/EbmlStream.h" #include "ebml/EbmlContexts.h" -#include "ebml/EbmlVersion.h" #include "ebml/EbmlVoid.h" +#include "ebml/StdIOCallback.h" -#include "matroska/FileKax.h" -#include "matroska/KaxAttachements.h" +#include "matroska/KaxAttachments.h" #include "matroska/KaxBlock.h" #include "matroska/KaxBlockData.h" #include "matroska/KaxChapters.h" @@ -73,53 +79,150 @@ #include "matroska/KaxTrackAudio.h" #include "matroska/KaxTrackVideo.h" #include "matroska/KaxTrackEntryData.h" +#include "matroska/KaxContentEncoding.h" #include "ebml/StdIOCallback.h" -using namespace LIBMATROSKA_NAMESPACE; -using namespace std; +extern "C" { + #include "mp4/libmp4.h" +} +#ifdef HAVE_ZLIB_H +# include +#endif +#define MATROSKA_COMPRESSION_NONE 0 +#define MATROSKA_COMPRESSION_ZLIB 1 -/***************************************************************************** - * Local prototypes - *****************************************************************************/ -static int Activate ( vlc_object_t * ); -static void Deactivate( vlc_object_t * ); -static int Demux ( input_thread_t * ); +/** + * What's between a directory and a filename? + */ +#if defined( WIN32 ) + #define DIRECTORY_SEPARATOR '\\' +#else + #define DIRECTORY_SEPARATOR '/' +#endif + +using namespace LIBMATROSKA_NAMESPACE; +using namespace std; /***************************************************************************** * Module descriptor *****************************************************************************/ +static int Open ( vlc_object_t * ); +static void Close( vlc_object_t * ); + vlc_module_begin(); - add_category_hint( N_("mkv-demuxer"), NULL, VLC_TRUE ); - add_bool( "mkv-seek-percent", 1, NULL, - N_("Seek based on percent not time"), - N_("Seek based on percent not time"), VLC_TRUE ); - - set_description( _("mka/mkv stream demuxer" ) ); - set_capability( "demux", 50 ); - set_callbacks( Activate, Deactivate ); + set_shortname( _("Matroska") ); + set_description( _("Matroska stream demuxer" ) ); + set_capability( "demux2", 50 ); + set_callbacks( Open, Close ); + set_category( CAT_INPUT ); + set_subcategory( SUBCAT_INPUT_DEMUX ); + + add_bool( "mkv-seek-percent", 1, NULL, + N_("Seek based on percent not time"), + N_("Seek based on percent not time"), VLC_TRUE ); + add_shortcut( "mka" ); add_shortcut( "mkv" ); vlc_module_end(); +/***************************************************************************** + * Local prototypes + *****************************************************************************/ +static int Demux ( demux_t * ); +static int Control( demux_t *, int, va_list ); +static void Seek ( demux_t *, mtime_t i_date, int i_percent ); + +#ifdef HAVE_ZLIB_H +block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) { + int result, dstsize, n; + unsigned char *dst; + block_t *p_block; + z_stream d_stream; + + d_stream.zalloc = (alloc_func)0; + d_stream.zfree = (free_func)0; + d_stream.opaque = (voidpf)0; + result = inflateInit(&d_stream); + if( result != Z_OK ) + { + msg_Dbg( p_this, "inflateInit() failed. Result: %d", result ); + return NULL; + } + + d_stream.next_in = (Bytef *)p_in_block->p_buffer; + d_stream.avail_in = p_in_block->i_buffer; + n = 0; + p_block = block_New( p_this, 0 ); + dst = NULL; + do + { + n++; + p_block = block_Realloc( p_block, 0, n * 1000 ); + dst = (unsigned char *)p_block->p_buffer; + d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000]; + d_stream.avail_out = 1000; + result = inflate(&d_stream, Z_NO_FLUSH); + if( ( result != Z_OK ) && ( result != Z_STREAM_END ) ) + { + msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result ); + return NULL; + } + } + while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) && + ( result != Z_STREAM_END ) ); + + dstsize = d_stream.total_out; + inflateEnd( &d_stream ); + + p_block = block_Realloc( p_block, 0, dstsize ); + p_block->i_buffer = dstsize; + block_Release( p_in_block ); + return p_block; +} +#endif + +/** + * Helper function to print the mkv parse tree + */ +static void MkvTree( demux_t *p_this, int i_level, char *psz_format, ... ) +{ + va_list args; + if( i_level > 9 ) + { + msg_Err( p_this, "too deep tree" ); + return; + } + va_start( args, psz_format ); + static char *psz_foo = "| | | | | | | | | |"; + char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) ); + strncpy( psz_foo2, psz_foo, 4 * i_level ); + psz_foo2[ 4 * i_level ] = '+'; + psz_foo2[ 4 * i_level + 1 ] = ' '; + strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format ); + __msg_GenericVa( VLC_OBJECT(p_this), VLC_MSG_DBG, "mkv", psz_foo2, args ); + free( psz_foo2 ); + va_end( args ); +} + /***************************************************************************** * Stream managment *****************************************************************************/ class vlc_stream_io_callback: public IOCallback { private: - input_thread_t *p_input; + stream_t *s; vlc_bool_t mb_eof; public: - vlc_stream_io_callback( input_thread_t * ); + vlc_stream_io_callback( stream_t * ); - virtual uint32_t read ( void *p_buffer, size_t i_size); + virtual uint32 read ( void *p_buffer, size_t i_size); virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning ); virtual size_t write ( const void *p_buffer, size_t i_size); - virtual uint64_t getFilePointer ( void ); + virtual uint64 getFilePointer ( void ); virtual void close ( void ); }; @@ -142,7 +245,7 @@ class EbmlParser private: EbmlStream *m_es; int mi_level; - EbmlElement *m_el[6]; + EbmlElement *m_el[10]; EbmlElement *m_got; @@ -154,18 +257,7 @@ class EbmlParser /***************************************************************************** * Some functions to manipulate memory *****************************************************************************/ -#define GetWLE( p ) __GetWLE( (uint8_t*)p ) -#define GetDWLE( p ) __GetDWLE( (uint8_t*)p ) #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p ) -static uint16_t __GetWLE( uint8_t *p ) -{ - return (uint16_t)p[0] | ( ((uint16_t)p[1]) << 8 ); -} -static uint32_t __GetDWLE( uint8_t *p ) -{ - return (uint32_t)p[0] | ( ((uint32_t)p[1]) << 8 ) | - ( ((uint32_t)p[2]) << 16 ) | ( ((uint32_t)p[3]) << 24 ); -} static vlc_fourcc_t __GetFOURCC( uint8_t *p ) { return VLC_FOURCC( p[0], p[1], p[2], p[3] ); @@ -176,7 +268,6 @@ static vlc_fourcc_t __GetFOURCC( uint8_t *p ) *****************************************************************************/ typedef struct { - int i_cat; vlc_bool_t b_default; vlc_bool_t b_enabled; int i_number; @@ -184,29 +275,15 @@ typedef struct int i_extra_data; uint8_t *p_extra_data; - char *psz_language; - char *psz_codec; - vlc_fourcc_t i_codec; uint64_t i_default_duration; float f_timecodescale; + /* video */ - int i_width; - int i_height; - int i_display_width; - int i_display_height; + es_format_t fmt; float f_fps; - - - /* audio */ - int i_channels; - int i_samplerate; - int i_bitspersample; - - es_descriptor_t *p_es; - - + es_out_id_t *p_es; vlc_bool_t b_inited; /* data to be send first */ @@ -217,11 +294,13 @@ typedef struct vlc_bool_t b_search_keyframe; /* informative */ - char *psz_name; char *psz_codec_name; char *psz_codec_settings; char *psz_codec_info_url; char *psz_codec_download_url; + + /* encryption/compression */ + int i_compression_type; } mkv_track_t; @@ -236,8 +315,37 @@ typedef struct vlc_bool_t b_key; } mkv_index_t; -struct demux_sys_t +class demux_sys_t { +public: + demux_sys_t() + :in(NULL) + ,es(NULL) + ,ep(NULL) + ,i_timescale(0) + ,f_duration(0.0) + ,i_track(0) + ,track(NULL) + ,i_cues_position(0) + ,i_chapters_position(0) + ,i_tags_position(0) + ,segment(NULL) + ,cluster(NULL) + ,i_pts(0) + ,i_start_pts(0) + ,b_cues(false) + ,i_index(0) + ,i_index_max(0) + ,index(NULL) + ,psz_muxing_application(NULL) + ,psz_writing_application(NULL) + ,psz_segment_filename(NULL) + ,psz_title(NULL) + ,psz_date_utc(NULL) + ,meta(NULL) + ,title(NULL) + {} + vlc_stream_io_callback *in; EbmlStream *es; EbmlParser *ep; @@ -260,8 +368,10 @@ struct demux_sys_t /* current data */ KaxSegment *segment; KaxCluster *cluster; + KaxSegmentUID segment_uid; mtime_t i_pts; + mtime_t i_start_pts; vlc_bool_t b_cues; int i_index; @@ -274,61 +384,71 @@ struct demux_sys_t char *psz_segment_filename; char *psz_title; char *psz_date_utc; + + vlc_meta_t *meta; + + input_title_t *title; + + std::vector families; + std::vector family_members; + + int64_t edition_uid; + bool edition_ordered; }; #define MKVD_TIMECODESCALE 1000000 -static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster ); +#define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId ) + +static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster ); static char *UTF8ToStr ( const UTFstring &u ); -static void LoadCues ( input_thread_t *); -static void InformationsCreate ( input_thread_t *p_input ); +static void LoadCues ( demux_t * ); +static void InformationCreate ( demux_t * ); + +static void ParseInfo( demux_t *, EbmlElement *info ); +static void ParseTracks( demux_t *, EbmlElement *tracks ); +static void ParseSeekHead( demux_t *, EbmlElement *seekhead ); +static void ParseChapters( demux_t *, EbmlElement *chapters ); /***************************************************************************** - * Activate: initializes matroska demux structures + * Open: initializes matroska demux structures *****************************************************************************/ -static int Activate( vlc_object_t * p_this ) +static int Open( vlc_object_t * p_this ) { - input_thread_t *p_input = (input_thread_t *)p_this; - demux_sys_t *p_sys; - uint8_t *p_peek; - - int i_track; - vlc_bool_t b_audio_selected; - int i_spu_channel, i_audio_channel; + demux_t *p_demux = (demux_t*)p_this; + demux_sys_t *p_sys; + uint8_t *p_peek; + std::string s_path, s_filename; + int i_upper_lvl; + size_t i, j; - EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL; + int i_track; - - /* Initialize access plug-in structures. */ - if( p_input->i_mtu == 0 ) - { - /* Improve speed. */ - p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE; - } - - /* Set the demux function */ - p_input->pf_demux = Demux; + EbmlElement *el = NULL, *el1 = NULL; /* peek the begining */ - if( input_Peek( p_input, &p_peek, 4 ) < 4 ) + if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) { - msg_Warn( p_input, "cannot peek" ); + msg_Warn( p_demux, "cannot peek" ); return VLC_EGENERIC; } /* is a valid file */ - if( p_peek[0] != 0x1a || p_peek[1] != 0x45 || p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) + if( p_peek[0] != 0x1a || p_peek[1] != 0x45 || + p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) { - msg_Warn( p_input, "matroska module discarded " + msg_Warn( p_demux, "matroska module discarded " "(invalid header 0x%.2x%.2x%.2x%.2x)", p_peek[0], p_peek[1], p_peek[2], p_peek[3] ); return VLC_EGENERIC; } - p_input->p_demux_data = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) ); - memset( p_sys, 0, sizeof( demux_sys_t ) ); + /* Set the demux function */ + p_demux->pf_demux = Demux; + p_demux->pf_control = Control; + p_demux->p_sys = p_sys = new demux_sys_t; - p_sys->in = new vlc_stream_io_callback( p_input ); + p_sys->in = new vlc_stream_io_callback( p_demux->s ); p_sys->es = new EbmlStream( *p_sys->in ); p_sys->f_duration = -1; p_sys->i_timescale = MKVD_TIMECODESCALE; @@ -342,29 +462,32 @@ static int Activate( vlc_object_t * p_this ) p_sys->b_cues = VLC_FALSE; p_sys->i_index = 0; p_sys->i_index_max = 1024; - p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * p_sys->i_index_max ); + p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * + p_sys->i_index_max ); p_sys->psz_muxing_application = NULL; p_sys->psz_writing_application = NULL; p_sys->psz_segment_filename = NULL; p_sys->psz_title = NULL; p_sys->psz_date_utc = NULL;; + p_sys->meta = NULL; + p_sys->title = NULL; if( p_sys->es == NULL ) { - msg_Err( p_input, "failed to create EbmlStream" ); + msg_Err( p_demux, "failed to create EbmlStream" ); delete p_sys->in; - free( p_sys ); + delete p_sys; return VLC_EGENERIC; } /* Find the EbmlHead element */ el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL); if( el == NULL ) { - msg_Err( p_input, "cannot find EbmlHead" ); + msg_Err( p_demux, "cannot find EbmlHead" ); goto error; } - msg_Dbg( p_input, "EbmlHead" ); + msg_Dbg( p_demux, "EbmlHead" ); /* skip it */ el->SkipData( *p_sys->es, el->Generic().Context ); delete el; @@ -373,10 +496,10 @@ static int Activate( vlc_object_t * p_this ) el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL); if( el == NULL ) { - msg_Err( p_input, "cannot find KaxSegment" ); + msg_Err( p_demux, "cannot find KaxSegment" ); goto error; } - msg_Dbg( p_input, "+ Segment" ); + MkvTree( p_demux, 0, "Segment" ); p_sys->segment = (KaxSegment*)el; p_sys->cluster = NULL; @@ -384,784 +507,384 @@ static int Activate( vlc_object_t * p_this ) while( ( el1 = p_sys->ep->Get() ) != NULL ) { - if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId ) + if( MKV_IS_ID( el1, KaxInfo ) ) { - msg_Dbg( p_input, "| + Informations" ); - - p_sys->ep->Down(); - while( ( el2 = p_sys->ep->Get() ) != NULL ) - { - if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId ) - { - KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2; - - tcs.ReadData( p_sys->es->I_O() ); - p_sys->i_timescale = uint64(tcs); - - msg_Dbg( p_input, "| | + TimecodeScale=%lld", p_sys->i_timescale ); - } - else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId ) - { - KaxDuration &dur = *(KaxDuration*)el2; - - dur.ReadData( p_sys->es->I_O() ); - p_sys->f_duration = float(dur); - - msg_Dbg( p_input, "| | + Duration=%f", p_sys->f_duration ); - } - else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId ) - { - KaxMuxingApp &mapp = *(KaxMuxingApp*)el2; - - mapp.ReadData( p_sys->es->I_O() ); - - p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) ); - - msg_Dbg( p_input, "| | + Muxing Application=%s", p_sys->psz_muxing_application ); - } - else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId ) - { - KaxWritingApp &wapp = *(KaxWritingApp*)el2; - - wapp.ReadData( p_sys->es->I_O() ); - - p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) ); - - msg_Dbg( p_input, "| | + Wrinting Application=%s", p_sys->psz_writing_application ); - } - else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId ) - { - KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2; - - sfn.ReadData( p_sys->es->I_O() ); - - p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) ); - - msg_Dbg( p_input, "| | + Segment Filename=%s", p_sys->psz_segment_filename ); - } - else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId ) - { - KaxTitle &title = *(KaxTitle*)el2; - - title.ReadData( p_sys->es->I_O() ); - - p_sys->psz_title = UTF8ToStr( UTFstring( title ) ); + ParseInfo( p_demux, el1 ); + } + else if( MKV_IS_ID( el1, KaxTracks ) ) + { + ParseTracks( p_demux, el1 ); + } + else if( MKV_IS_ID( el1, KaxSeekHead ) ) + { + ParseSeekHead( p_demux, el1 ); + } + else if( MKV_IS_ID( el1, KaxCues ) ) + { + msg_Dbg( p_demux, "| + Cues" ); + } + else if( MKV_IS_ID( el1, KaxCluster ) ) + { + msg_Dbg( p_demux, "| + Cluster" ); - msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title ); - } -#ifdef HAVE_GMTIME_R - else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId ) - { - KaxDateUTC &date = *(KaxDateUTC*)el2; - time_t i_date; - struct tm tmres; - char buffer[256]; + p_sys->cluster = (KaxCluster*)el1; - date.ReadData( p_sys->es->I_O() ); + p_sys->ep->Down(); + /* stop parsing the stream */ + break; + } + else if( MKV_IS_ID( el1, KaxAttachments ) ) + { + msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" ); + } + else if( MKV_IS_ID( el1, KaxChapters ) ) + { + msg_Dbg( p_demux, "| + Chapters" ); + ParseChapters( p_demux, el1 ); + } + else if( MKV_IS_ID( el1, KaxTag ) ) + { + msg_Dbg( p_demux, "| + Tags FIXME TODO" ); + } + else + { + msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el1).name() ); + } + } - i_date = date.GetEpochDate(); - memset( buffer, 0, 256 ); - if( gmtime_r( &i_date, &tmres ) && - asctime_r( &tmres, buffer ) ) - { - buffer[strlen( buffer)-1]= '\0'; - p_sys->psz_date_utc = strdup( buffer ); - msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc ); - } - } -#endif - else - { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() ); - } - } - p_sys->ep->Up(); + /* get the files from the same dir from the same family (based on p_demux->psz_path) */ + /* _todo_ handle multi-segment files */ + if (p_demux->psz_path[0] != '\0' && (!strcmp(p_demux->psz_access, "") || !strcmp(p_demux->psz_access, ""))) + { + // assume it's a regular file + // get the directory path + s_path = p_demux->psz_path; + if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR) + { + s_path = s_path.substr(0,s_path.length()-1); } - else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId ) + else { - msg_Dbg( p_input, "| + Tracks" ); + if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0) + { + s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR)); + } + } - p_sys->ep->Down(); - while( ( el2 = p_sys->ep->Get() ) != NULL ) + struct dirent *p_file_item; + DIR *p_src_dir = opendir(s_path.c_str()); + + if (p_src_dir != NULL) + { + while ((p_file_item = readdir(p_src_dir))) { - if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId ) + if (strlen(p_file_item->d_name) > 4) { - msg_Dbg( p_input, "| | + Track Entry" ); - - p_sys->i_track++; - p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) ); -#define tk p_sys->track[p_sys->i_track - 1] - memset( &tk, 0, sizeof( mkv_track_t ) ); - tk.i_cat = UNKNOWN_ES; - tk.b_default = VLC_TRUE; - tk.b_enabled = VLC_TRUE; - tk.i_number = p_sys->i_track - 1; - tk.i_extra_data = 0; - tk.p_extra_data = NULL; - tk.i_codec = 0; - tk.psz_codec = NULL; - tk.psz_language = NULL; - tk.i_default_duration = 0; - tk.f_timecodescale = 1.0; - - tk.b_inited = VLC_FALSE; - tk.i_data_init = 0; - tk.p_data_init = NULL; - - tk.psz_name = NULL; - tk.psz_codec_name = NULL; - tk.psz_codec_settings = NULL; - tk.psz_codec_info_url = NULL; - tk.psz_codec_download_url = NULL; - - p_sys->ep->Down(); - - while( ( el3 = p_sys->ep->Get() ) != NULL ) - { - if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId ) - { - KaxTrackNumber &tnum = *(KaxTrackNumber*)el3; - tnum.ReadData( p_sys->es->I_O() ); + s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name; - tk.i_number = uint32( tnum ); - msg_Dbg( p_input, "| | | + Track Number=%u", uint32( tnum ) ); - } - else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId ) - { - KaxTrackUID &tuid = *(KaxTrackUID*)el3; - tuid.ReadData( p_sys->es->I_O() ); + if (!s_filename.compare(p_demux->psz_path)) + continue; - msg_Dbg( p_input, "| | | + Track UID=%u", uint32( tuid ) ); - } - else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId ) + if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") || + !s_filename.compare(s_filename.length() - 3, 3, "mka")) + { + // test wether this file belongs to the our family + bool b_keep_file_opened = false; + StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ); + EbmlStream *p_stream = new EbmlStream(*p_file_io); + EbmlElement *p_l0, *p_l1, *p_l2; + + // verify the EBML Header + p_l0 = p_stream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL); + if (p_l0 == NULL) { - char *psz_type; - KaxTrackType &ttype = *(KaxTrackType*)el3; - ttype.ReadData( p_sys->es->I_O() ); - switch( uint8(ttype) ) - { - case track_audio: - psz_type = "audio"; - tk.i_cat = AUDIO_ES; - break; - case track_video: - psz_type = "video"; - tk.i_cat = VIDEO_ES; - break; - case track_subtitle: - psz_type = "subtitle"; - tk.i_cat = SPU_ES; - break; - default: - psz_type = "unknown"; - tk.i_cat = UNKNOWN_ES; - break; - } - - msg_Dbg( p_input, "| | | + Track Type=%s", psz_type ); + delete p_stream; + delete p_file_io; + continue; } - else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId ) - { - KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3; - fenb.ReadData( p_sys->es->I_O() ); - tk.b_enabled = uint32( fenb ); - msg_Dbg( p_input, "| | | + Track Enabled=%u", uint32( fenb ) ); - } - else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId ) - { - KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3; - fdef.ReadData( p_sys->es->I_O() ); + p_l0->SkipData(*p_stream, EbmlHead_Context); + delete p_l0; - tk.b_default = uint32( fdef ); - msg_Dbg( p_input, "| | | + Track Default=%u", uint32( fdef ) ); - } - else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId ) + // find all segments in this file + p_l0 = p_stream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL); + if (p_l0 == NULL) { - KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3; - lac.ReadData( p_sys->es->I_O() ); - - msg_Dbg( p_input, "| | | + Track Lacing=%d", uint32( lac ) ); + delete p_stream; + delete p_file_io; + continue; } - else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId ) - { - KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3; - cmin.ReadData( p_sys->es->I_O() ); - msg_Dbg( p_input, "| | | + Track MinCache=%d", uint32( cmin ) ); - } - else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId ) - { - KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3; - cmax.ReadData( p_sys->es->I_O() ); + i_upper_lvl = 0; - msg_Dbg( p_input, "| | | + Track MaxCache=%d", uint32( cmax ) ); - } - else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId ) + while (p_l0 != 0) { - KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3; - defd.ReadData( p_sys->es->I_O() ); + if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId) + { + EbmlParser *ep; + KaxSegmentUID *p_uid = NULL; - tk.i_default_duration = uint64(defd); - msg_Dbg( p_input, "| | | + Track Default Duration=%lld", uint64(defd) ); - } - else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId ) - { - KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3; - ttcs.ReadData( p_sys->es->I_O() ); + ep = new EbmlParser(p_stream, p_l0); + bool b_this_segment_matches = false; + while ((p_l1 = ep->Get())) + { + if (MKV_IS_ID(p_l1, KaxInfo)) + { + // find the families of this segment + KaxInfo *p_info = static_cast(p_l1); + + p_info->Read(*p_stream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true); + for( i = 0; i < p_info->ListSize() && !b_this_segment_matches; i++ ) + { + EbmlElement *l = (*p_info)[i]; + + if( MKV_IS_ID( l, KaxSegmentUID ) ) + { + p_uid = static_cast(l); + if (p_sys->segment_uid == *p_uid) + break; + } + else if( MKV_IS_ID( l, KaxSegmentFamily ) ) + { + KaxSegmentFamily *p_fam = static_cast(l); + for (j=0; jfamilies.size(); j++) + { + if (p_sys->families.at(j) == *p_fam) + { + b_this_segment_matches = true; + break; + } + } + } + } + break; + } + } - tk.f_timecodescale = float( ttcs ); - msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale ); - } - else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId ) - { - KaxTrackName &tname = *(KaxTrackName*)el3; - tname.ReadData( p_sys->es->I_O() ); + if (b_this_segment_matches) + { + b_keep_file_opened = true; + } + } - tk.psz_name = UTF8ToStr( UTFstring( tname ) ); - msg_Dbg( p_input, "| | | + Track Name=%s", tk.psz_name ); + p_l0->SkipData(*p_stream, EbmlHead_Context); + delete p_l0; + p_l0 = p_stream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL); } - else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId ) - { - KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3; - lang.ReadData( p_sys->es->I_O() ); - - tk.psz_language = strdup( string( lang ).c_str() ); - msg_Dbg( p_input, "| | | + Track Language=`%s'", string( lang ).c_str() ); - } - else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId ) + if (!b_keep_file_opened) { - KaxCodecID &codecid = *(KaxCodecID*)el3; - codecid.ReadData( p_sys->es->I_O() ); - - tk.psz_codec = strdup( string( codecid ).c_str() ); - msg_Dbg( p_input, "| | | + Track CodecId=%s", string( codecid ).c_str() ); + delete p_stream; + delete p_file_io; } - else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId ) - { - KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3; - cpriv.ReadData( p_sys->es->I_O() ); + } + } + } + closedir( p_src_dir ); + } + } - tk.i_extra_data = cpriv.GetSize(); - if( tk.i_extra_data > 0 ) - { - tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data ); - memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data ); - } - msg_Dbg( p_input, "| | | + Track CodecPrivate size=%lld", cpriv.GetSize() ); - } - else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId ) - { - KaxCodecName &cname = *(KaxCodecName*)el3; - cname.ReadData( p_sys->es->I_O() ); - tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) ); - msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name ); - } - else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId ) - { - KaxCodecSettings &cset = *(KaxCodecSettings*)el3; - cset.ReadData( p_sys->es->I_O() ); + if( p_sys->cluster == NULL ) + { + msg_Err( p_demux, "cannot find any cluster, damaged file ?" ); + goto error; + } - tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) ); - msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings ); - } - else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId ) - { - KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3; - ciurl.ReadData( p_sys->es->I_O() ); + /* *** Load the cue if found *** */ + if( p_sys->i_cues_position >= 0 ) + { + vlc_bool_t b_seekable; - tk.psz_codec_info_url = strdup( string( ciurl ).c_str() ); - msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url ); - } - else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId ) - { - KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3; - cdurl.ReadData( p_sys->es->I_O() ); + stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable ); + if( b_seekable ) + { + LoadCues( p_demux ); + } + } - tk.psz_codec_download_url = strdup( string( cdurl ).c_str() ); - msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url ); - } - else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId ) - { - KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3; - cdall.ReadData( p_sys->es->I_O() ); + if( !p_sys->b_cues || p_sys->i_index <= 0 ) + { + msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" ); - msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) ); - } - else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId ) - { - KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3; - tovr.ReadData( p_sys->es->I_O() ); + IndexAppendCluster( p_demux, p_sys->cluster ); - msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) ); - } - else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| | | + Track Video" ); - tk.i_width = 0; - tk.i_height = 0; - tk.i_display_width = 0; - tk.i_display_height = 0; - tk.f_fps = 0.0; + p_sys->b_cues = VLC_FALSE; + } - p_sys->ep->Down(); + /* add all es */ + msg_Dbg( p_demux, "found %d es", p_sys->i_track ); + for( i_track = 0; i_track < p_sys->i_track; i_track++ ) + { +#define tk p_sys->track[i_track] + if( tk.fmt.i_cat == UNKNOWN_ES ) + { + msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number ); + tk.p_es = NULL; + continue; + } - while( ( el4 = p_sys->ep->Get() ) != NULL ) - { - if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId ) - { - KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4; - fint.ReadData( p_sys->es->I_O() ); + if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) ) + { + if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) ) + { + msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" ); + tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); + } + else + { + BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data; - msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) ); - } - else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId ) - { - KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4; - stereo.ReadData( p_sys->es->I_O() ); + tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth ); + tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight ); + tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression ); - msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) ); - } - else if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId ) - { - KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4; - vwidth.ReadData( p_sys->es->I_O() ); + tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER ); + if( tk.fmt.i_extra > 0 ) + { + tk.fmt.p_extra = malloc( tk.fmt.i_extra ); + memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra ); + } + } + } + else if( !strcmp( tk.psz_codec, "V_MPEG1" ) || + !strcmp( tk.psz_codec, "V_MPEG2" ) ) + { + tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' ); + } + else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) ) + { + if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) ) + { + tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' ); + } + else if( !strcmp( tk.psz_codec, "V_MPEG4/ISO/AVC" ) ) + { + tk.fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' ); + tk.fmt.b_packetized = VLC_FALSE; + tk.fmt.i_extra = tk.i_extra_data; + tk.fmt.p_extra = malloc( tk.i_extra_data ); + memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data ); + } + else + { + tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' ); + } + } + else if( !strcmp( tk.psz_codec, "V_QUICKTIME" ) ) + { + MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) ); + stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux), + tk.p_extra_data, + tk.i_extra_data ); + MP4_ReadBoxCommon( p_mp4_stream, p_box ); + MP4_ReadBox_sample_vide( p_mp4_stream, p_box ); + tk.fmt.i_codec = p_box->i_type; + tk.fmt.video.i_width = p_box->data.p_sample_vide->i_width; + tk.fmt.video.i_height = p_box->data.p_sample_vide->i_height; + tk.fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description; + tk.fmt.p_extra = malloc( tk.fmt.i_extra ); + memcpy( tk.fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk.fmt.i_extra ); + MP4_FreeBox_sample_vide( p_box ); + stream_MemoryDelete( p_mp4_stream, VLC_TRUE ); + } + else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) ) + { + if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) ) + { + msg_Err( p_demux, "missing/invalid WAVEFORMATEX" ); + tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); + } + else + { + WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data; - tk.i_width = uint16( vwidth ); - msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) ); - } - else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId ) - { - KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4; - vheight.ReadData( p_sys->es->I_O() ); + wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL ); - tk.i_height = uint16( vheight ); - msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) ); - } - else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId ) - { - KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4; - vwidth.ReadData( p_sys->es->I_O() ); + tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels ); + tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec ); + tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8; + tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );; + tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample ); - tk.i_display_width = uint16( vwidth ); - msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) ); - } - else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId ) - { - KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4; - vheight.ReadData( p_sys->es->I_O() ); - - tk.i_display_height = uint16( vheight ); - msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) ); - } - else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId ) - { - KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4; - vfps.ReadData( p_sys->es->I_O() ); - - tk.f_fps = float( vfps ); - msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) ); - } - else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId ) - { - KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4; - vdmode.ReadData( p_sys->es->I_O() ); - - msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s", - uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) ); - } - else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId ) - { - KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4; - ratio.ReadData( p_sys->es->I_O() ); - - msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) ); - } - else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId ) - { - KaxVideoGamma &gamma = *(KaxVideoGamma*)el4; - gamma.ReadData( p_sys->es->I_O() ); - - msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) ); - } - else - { - msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() ); - } - } - p_sys->ep->Up(); - } - else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| | | + Track Audio" ); - tk.i_channels = 0; - tk.i_samplerate = 0; - tk.i_bitspersample = 0; - - p_sys->ep->Down(); - - while( ( el4 = p_sys->ep->Get() ) != NULL ) - { - if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId ) - { - KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4; - afreq.ReadData( p_sys->es->I_O() ); - - tk.i_samplerate = (int)float( afreq ); - msg_Dbg( p_input, "| | | | + afreq=%d", tk.i_samplerate ); - } - else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId ) - { - KaxAudioChannels &achan = *(KaxAudioChannels*)el4; - achan.ReadData( p_sys->es->I_O() ); - - tk.i_channels = uint8( achan ); - msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) ); - } - else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId ) - { - KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4; - abits.ReadData( p_sys->es->I_O() ); - - tk.i_bitspersample = uint8( abits ); - msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) ); - } - else - { - msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() ); - } - } - p_sys->ep->Up(); - } - else - { - msg_Dbg( p_input, "| | | + Unknown (%s)", typeid(*el3).name() ); - } - } - p_sys->ep->Up(); - } - else - { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() ); - } -#undef tk - } - p_sys->ep->Up(); - } - else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| + Seek head" ); - p_sys->ep->Down(); - while( ( el = p_sys->ep->Get() ) != NULL ) - { - if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId ) - { - EbmlId id = EbmlVoid::ClassInfos.GlobalId; - int64_t i_pos = -1; - - //msg_Dbg( p_input, "| | + Seek" ); - p_sys->ep->Down(); - while( ( el = p_sys->ep->Get() ) != NULL ) - { - if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId ) - { - KaxSeekID &sid = *(KaxSeekID*)el; - - sid.ReadData( p_sys->es->I_O() ); - - id = EbmlId( sid.GetBuffer(), sid.GetSize() ); - } - else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId ) - { - KaxSeekPosition &spos = *(KaxSeekPosition*)el; - - spos.ReadData( p_sys->es->I_O() ); - - i_pos = uint64( spos ); - } - else - { - msg_Dbg( p_input, "| | | + Unknown (%s)", typeid(*el).name() ); - } - } - p_sys->ep->Up(); - - if( i_pos >= 0 ) - { - if( id == KaxCues::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| | | = cues at %lld", i_pos ); - p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos ); - } - else if( id == KaxChapters::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| | | = chapters at %lld", i_pos ); - p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos ); - } - else if( id == KaxTags::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| | | = tags at %lld", i_pos ); - p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos ); - } - - } - } - else + tk.fmt.i_extra = GetWLE( &p_wf->cbSize ); + if( tk.fmt.i_extra > 0 ) { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el).name() ); + tk.fmt.p_extra = malloc( tk.fmt.i_extra ); + memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra ); } } - p_sys->ep->Up(); - } - else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId ) - { - msg_Dbg( p_input, "| + Cues" ); } - else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId ) + else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) || + !strcmp( tk.psz_codec, "A_MPEG/L2" ) || + !strcmp( tk.psz_codec, "A_MPEG/L1" ) ) { - msg_Dbg( p_input, "| + Cluster" ); - - p_sys->cluster = (KaxCluster*)el1; - - p_sys->ep->Down(); - /* stop parsing the stream */ - break; + tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' ); } - else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId ) + else if( !strcmp( tk.psz_codec, "A_AC3" ) ) { - msg_Dbg( p_input, "| + Attachements FIXME TODO (but probably never supported)" ); + tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' ); } - else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId ) + else if( !strcmp( tk.psz_codec, "A_DTS" ) ) { - msg_Dbg( p_input, "| + Chapters FIXME TODO" ); + tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' ); } - else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId ) + else if( !strcmp( tk.psz_codec, "A_FLAC" ) ) { - msg_Dbg( p_input, "| + Tags FIXME TODO" ); + tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' ); + tk.fmt.i_extra = tk.i_extra_data; + tk.fmt.p_extra = malloc( tk.i_extra_data ); + memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data ); } - else + else if( !strcmp( tk.psz_codec, "A_VORBIS" ) ) { - msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() ); - } - } + int i, i_offset = 1, i_size[3], i_extra; + uint8_t *p_extra; - if( p_sys->cluster == NULL ) - { - msg_Err( p_input, "cannot find any cluster, damaged file ?" ); - goto error; - } - - if( p_sys->i_chapters_position >= 0 ) - { - msg_Warn( p_input, "chapters unsupported" ); - } - - /* *** Load the cue if found *** */ - if( p_sys->i_cues_position >= 0 && p_input->stream.b_seekable ) - { - LoadCues( p_input ); - } - - if( !p_sys->b_cues || p_sys->i_index <= 0 ) - { - msg_Warn( p_input, "no cues/empty cues found -> seek won't be precise" ); - - IndexAppendCluster( p_input, p_sys->cluster ); - - p_sys->b_cues = VLC_FALSE; - } + tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' ); - /* Create one program */ - vlc_mutex_lock( &p_input->stream.stream_lock ); - if( input_InitStream( p_input, 0 ) == -1) - { - vlc_mutex_unlock( &p_input->stream.stream_lock ); - msg_Err( p_input, "cannot init stream" ); - goto error; - } - if( input_AddProgram( p_input, 0, 0) == NULL ) - { - vlc_mutex_unlock( &p_input->stream.stream_lock ); - msg_Err( p_input, "cannot add program" ); - goto error; - } - p_input->stream.p_selected_program = p_input->stream.pp_programs[0]; - if( p_sys->f_duration > 1001.0 ) - { - mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 ); - p_input->stream.i_mux_rate = p_input->stream.p_selected_area->i_size / 50 / i_duration; - } - else - { - p_input->stream.i_mux_rate = 0; - } - vlc_mutex_unlock( &p_input->stream.stream_lock ); + /* Split the 3 headers */ + if( tk.p_extra_data[0] != 0x02 ) + msg_Err( p_demux, "invalid vorbis header" ); - /* add all es */ - msg_Dbg( p_input, "found %d es", p_sys->i_track ); - for( i_track = 0; i_track < p_sys->i_track; i_track++ ) - { -#define tk p_sys->track[i_track] - if( tk.i_cat == UNKNOWN_ES ) - { - msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number ); - tk.p_es = NULL; - continue; - } - tk.p_es = input_AddES( p_input, - p_input->stream.p_selected_program, - i_track + 1, - tk.i_cat, - tk.psz_language, 0 ); - if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) ) - { - if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) ) - { - msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" ); - tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); - } - else + for( i = 0; i < 2; i++ ) { - BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data; - - p_bih->biSize = GetDWLE( &p_bih->biSize ); - p_bih->biWidth = GetDWLE( &p_bih->biWidth ); - p_bih->biHeight = GetDWLE( &p_bih->biHeight ); - p_bih->biPlanes = GetWLE( &p_bih->biPlanes ); - p_bih->biBitCount = GetWLE( &p_bih->biBitCount ); - p_bih->biCompression = GetFOURCC( &p_bih->biCompression ); - p_bih->biSizeImage = GetDWLE( &p_bih->biSizeImage ); - p_bih->biXPelsPerMeter = GetDWLE( &p_bih->biXPelsPerMeter ); - p_bih->biYPelsPerMeter = GetDWLE( &p_bih->biYPelsPerMeter ); - p_bih->biClrUsed = GetDWLE( &p_bih->biClrUsed ); - p_bih->biClrImportant = GetDWLE( &p_bih->biClrImportant ); - - - tk.i_codec = p_bih->biCompression; - tk.p_es->p_bitmapinfoheader = p_bih; + i_size[i] = 0; + while( i_offset < tk.i_extra_data ) + { + i_size[i] += tk.p_extra_data[i_offset]; + if( tk.p_extra_data[i_offset++] != 0xff ) break; + } } - } - else if( !strcmp( tk.psz_codec, "V_MPEG1" ) || - !strcmp( tk.psz_codec, "V_MPEG2" ) ) - { - tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' ); - } - else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) ) - { - BITMAPINFOHEADER *p_bih; - tk.i_extra_data = sizeof( BITMAPINFOHEADER ); - tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data ); + i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset); + i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]); + i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1]; - p_bih = (BITMAPINFOHEADER*)tk.p_extra_data; - memset( p_bih, 0, sizeof( BITMAPINFOHEADER ) ); - p_bih->biSize = sizeof( BITMAPINFOHEADER ); - p_bih->biWidth = tk.i_width; - p_bih->biHeight= tk.i_height; - - if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) ) - { - tk.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' ); - } - else - { - tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' ); - } - } - else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) ) - { - if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) ) - { - msg_Err( p_input, "missing/invalid WAVEFORMATEX" ); - tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); - } - else + tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2]; + tk.fmt.p_extra = malloc( tk.fmt.i_extra ); + p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0; + for( i = 0; i < 3; i++ ) { - WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data; - - p_wf->wFormatTag = GetWLE( &p_wf->wFormatTag ); - p_wf->nChannels = GetWLE( &p_wf->nChannels ); - p_wf->nSamplesPerSec = GetDWLE( &p_wf->nSamplesPerSec ); - p_wf->nAvgBytesPerSec = GetDWLE( &p_wf->nAvgBytesPerSec ); - p_wf->nBlockAlign = GetWLE( &p_wf->nBlockAlign ); - p_wf->wBitsPerSample = GetWLE( &p_wf->wBitsPerSample ); - p_wf->cbSize = GetWLE( &p_wf->cbSize ); - - switch( p_wf->wFormatTag ) - { - case WAVE_FORMAT_PCM: - tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' ); - break; - case WAVE_FORMAT_ADPCM: - tk.i_codec = VLC_FOURCC( 'm', 's', 0x00, 0x02 ); - break; - case WAVE_FORMAT_ALAW: - tk.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' ); - break; - case WAVE_FORMAT_MULAW: - tk.i_codec = VLC_FOURCC( 'm', 'l', 'a', 'w' ); - break; - case WAVE_FORMAT_IMA_ADPCM: - tk.i_codec = VLC_FOURCC( 'm', 's', 0x00, 0x11 ); - break; - case WAVE_FORMAT_MPEG: - case WAVE_FORMAT_MPEGLAYER3: - tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' ); - break; - case WAVE_FORMAT_A52: - tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' ); - break; - case WAVE_FORMAT_WMA1: - tk.i_codec = VLC_FOURCC( 'w', 'm', 'a', '1' ); - break; - case WAVE_FORMAT_WMA2: - tk.i_codec = VLC_FOURCC( 'w', 'm', 'a', '2' ); - break; - case WAVE_FORMAT_WMA3: - tk.i_codec = VLC_FOURCC( 'w', 'm', 'a', '3' ); - break; - default: - msg_Err( p_input, "unknown wFormatTag=0x%x", p_wf->wFormatTag ); - tk.i_codec = VLC_FOURCC( 'm', 's', p_wf->wFormatTag >> 8, p_wf->wFormatTag&0xff ); - break; - } - tk.p_es->p_waveformatex = p_wf; + *(p_extra++) = i_size[i] >> 8; + *(p_extra++) = i_size[i] & 0xFF; + memcpy( p_extra, tk.p_extra_data + i_offset + i_extra, + i_size[i] ); + p_extra += i_size[i]; + i_extra += i_size[i]; } } - else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) || - !strcmp( tk.psz_codec, "A_MPEG/L2" ) || - !strcmp( tk.psz_codec, "A_MPEG/L1" ) ) - { - tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' ); - } - else if( !strcmp( tk.psz_codec, "A_AC3" ) ) - { - tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' ); - } - else if( !strcmp( tk.psz_codec, "A_DTS" ) ) - { - tk.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' ); - } - else if( !strcmp( tk.psz_codec, "A_VORBIS" ) ) - { - tk.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' ); - tk.i_data_init = tk.i_extra_data; - tk.p_data_init = tk.p_extra_data; - } else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) || !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) ) { int i_profile, i_srate; - static int i_sample_rates[] = + static unsigned int i_sample_rates[] = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0 }; - WAVEFORMATEX *p_wf; - tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' ); + tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' ); /* create data for faad (MP4DecSpecificDescrTag)*/ if( !strcmp( &tk.psz_codec[12], "MAIN" ) ) @@ -1183,164 +906,135 @@ static int Activate( vlc_object_t * p_this ) for( i_srate = 0; i_srate < 13; i_srate++ ) { - if( i_sample_rates[i_srate] == tk.i_samplerate ) + if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate ) { break; } } - msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate ); + msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate ); - tk.i_extra_data = sizeof( WAVEFORMATEX ) + 2; - tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data ); - p_wf = (WAVEFORMATEX*)tk.p_extra_data; - - p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN; - p_wf->nChannels = tk.i_channels; - p_wf->nSamplesPerSec = tk.i_samplerate; - p_wf->nAvgBytesPerSec = 0; - p_wf->nBlockAlign = 0; - p_wf->wBitsPerSample = 0; - p_wf->cbSize = 2; - - tk.p_extra_data[sizeof( WAVEFORMATEX )+ 0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1); - tk.p_extra_data[sizeof( WAVEFORMATEX )+ 1] = ((i_srate & 0x1) << 7) | (tk.i_channels << 3); - - tk.p_es->p_waveformatex = p_wf; + tk.fmt.i_extra = 2; + tk.fmt.p_extra = malloc( tk.fmt.i_extra ); + ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1); + ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3); } else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) || !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) || !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) ) { - WAVEFORMATEX *p_wf; - - tk.i_extra_data = sizeof( WAVEFORMATEX ); - tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data ); - - p_wf = (WAVEFORMATEX*)tk.p_extra_data; - - if( !strncmp( &tk.psz_codec[6], "INT", 3 ) ) - { - p_wf->wFormatTag = WAVE_FORMAT_PCM; - } - else - { - p_wf->wFormatTag = WAVE_FORMAT_IEEE_FLOAT; - } - p_wf->nChannels = tk.i_channels; - p_wf->nSamplesPerSec = tk.i_samplerate; - p_wf->nAvgBytesPerSec = 0; - p_wf->nBlockAlign = ( tk.i_bitspersample + 7 ) / 8 * tk.i_channels; - p_wf->wBitsPerSample = tk.i_bitspersample; - p_wf->cbSize = 0; - - tk.p_es->p_waveformatex = p_wf; - if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ) { - tk.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' ); + tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' ); } else { - tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' ); + tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' ); } + tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels; } - else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) ) + else if( !strcmp( tk.psz_codec, "A_TTA1" ) ) { - tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' ); + /* FIXME: support this codec */ + msg_Err( p_demux, "TTA not supported yet[%d, n=%d]", i_track, tk.i_number ); + tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); } -#if 0 - else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ) + else if( !strcmp( tk.psz_codec, "A_WAVPACK4" ) ) { - tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' ); + /* FIXME: support this codec */ + msg_Err( p_demux, "Wavpack not supported yet[%d, n=%d]", i_track, tk.i_number ); + tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); } -#endif - else + else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) ) { - msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec ); - tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); + tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' ); + tk.fmt.subs.psz_encoding = strdup( "UTF-8" ); } - - tk.p_es->i_fourcc = tk.i_codec; -#undef tk - } - - /* select track all video, one audio, no spu TODO : improve */ - b_audio_selected = VLC_FALSE; - i_audio_channel = 0; - i_spu_channel = 0; - for( i_track = 0; i_track < p_sys->i_track; i_track++ ) - { -#define tk p_sys->track[i_track] - switch( tk.i_cat ) + else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) || + !strcmp( tk.psz_codec, "S_TEXT/ASS" ) || + !strcmp( tk.psz_codec, "S_SSA" ) || + !strcmp( tk.psz_codec, "S_ASS" )) { - case VIDEO_ES: - vlc_mutex_lock( &p_input->stream.stream_lock ); - input_SelectES( p_input, tk.p_es ); - vlc_mutex_unlock( &p_input->stream.stream_lock ); - break; - - case AUDIO_ES: - if( ( !b_audio_selected && config_GetInt( p_input, "audio-channel" ) < 0 ) || - i_audio_channel == config_GetInt( p_input, "audio-channel" ) ) + tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' ); + tk.fmt.subs.psz_encoding = strdup( "UTF-8" ); + } + else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) ) + { + tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' ); + if( tk.i_extra_data ) + { + char *p_start; + char *p_buf = (char *)malloc( tk.i_extra_data + 1); + memcpy( p_buf, tk.p_extra_data , tk.i_extra_data ); + p_buf[tk.i_extra_data] = '\0'; + + p_start = strstr( p_buf, "size:" ); + if( sscanf( p_start, "size: %dx%d", + &tk.fmt.subs.spu.i_original_frame_width, &tk.fmt.subs.spu.i_original_frame_height ) == 2 ) { - vlc_mutex_lock( &p_input->stream.stream_lock ); - input_SelectES( p_input, tk.p_es ); - vlc_mutex_unlock( &p_input->stream.stream_lock ); - - b_audio_selected = tk.p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE; + msg_Dbg( p_demux, "original frame size vobsubs: %dx%d", tk.fmt.subs.spu.i_original_frame_width, tk.fmt.subs.spu.i_original_frame_height ); } - i_audio_channel++; - break; - case SPU_ES: - if( i_spu_channel == config_GetInt( p_input, "spu-channel" ) ) + else { - vlc_mutex_lock( &p_input->stream.stream_lock ); - input_SelectES( p_input, tk.p_es ); - vlc_mutex_unlock( &p_input->stream.stream_lock ); + msg_Warn( p_demux, "reading original frame size for vobsub failed" ); } - i_spu_channel++; - break; + free( p_buf ); + } + } + else if( !strcmp( tk.psz_codec, "B_VOBBTN" ) ) + { + /* FIXME: support this codec */ + msg_Err( p_demux, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk.i_number ); + tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); + } + else + { + msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec ); + tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' ); + } + if( tk.b_default ) + { + tk.fmt.i_priority = 1000; } -#undef tk - } - if( !b_audio_selected ) - { - msg_Warn( p_input, "cannot find/select audio track" ); + tk.p_es = es_out_Add( p_demux->out, &tk.fmt ); +#undef tk } - /* add informations */ - InformationsCreate( p_input ); + /* add information */ + InformationCreate( p_demux ); return VLC_SUCCESS; error: delete p_sys->es; delete p_sys->in; - free( p_sys ); + delete p_sys; return VLC_EGENERIC; } /***************************************************************************** - * Deactivate: frees unused data + * Close: frees unused data *****************************************************************************/ -static void Deactivate( vlc_object_t *p_this ) +static void Close( vlc_object_t *p_this ) { - input_thread_t *p_input = (input_thread_t *)p_this; - demux_sys_t *p_sys = p_input->p_demux_data; - - int i_track; + demux_t *p_demux = (demux_t*)p_this; + demux_sys_t *p_sys = p_demux->p_sys; + int i_track; for( i_track = 0; i_track < p_sys->i_track; i_track++ ) { #define tk p_sys->track[i_track] + if( tk.fmt.psz_description ) + { + free( tk.fmt.psz_description ); + } if( tk.psz_codec ) { free( tk.psz_codec ); } - if( tk.psz_language ) + if( tk.fmt.psz_language ) { - free( tk.psz_language ); + free( tk.fmt.psz_language ); } #undef tk } @@ -1360,24 +1054,119 @@ static void Deactivate( vlc_object_t *p_this ) delete p_sys->ep; delete p_sys->es; delete p_sys->in; - - free( p_sys ); + delete p_sys; } -static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration ) +/***************************************************************************** + * Control: + *****************************************************************************/ +static int Control( demux_t *p_demux, int i_query, va_list args ) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; + int64_t *pi64; + double *pf, f; + int i_skp; - *pp_block = NULL; - *pi_ref1 = -1; - *pi_ref2 = -1; + vlc_meta_t **pp_meta; - for( ;; ) + switch( i_query ) { - EbmlElement *el; - int i_level; + case DEMUX_GET_META: + pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** ); + *pp_meta = vlc_meta_Duplicate( p_sys->meta ); + return VLC_SUCCESS; + + case DEMUX_GET_LENGTH: + pi64 = (int64_t*)va_arg( args, int64_t * ); + if( p_sys->f_duration > 0.0 ) + { + *pi64 = (int64_t)(p_sys->f_duration * 1000); + return VLC_SUCCESS; + } + return VLC_EGENERIC; + + case DEMUX_GET_POSITION: + pf = (double*)va_arg( args, double * ); + *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s ); + return VLC_SUCCESS; + + case DEMUX_SET_POSITION: + f = (double)va_arg( args, double ); + Seek( p_demux, -1, (int)(100.0 * f) ); + return VLC_SUCCESS; + + case DEMUX_GET_TIME: + pi64 = (int64_t*)va_arg( args, int64_t * ); + if( p_sys->f_duration > 0.0 ) + { + mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 ); + + /* FIXME */ + *pi64 = (mtime_t)1000000 * + (mtime_t)i_duration* + (mtime_t)p_sys->in->getFilePointer() / + (mtime_t)stream_Size( p_demux->s ); + return VLC_SUCCESS; + } + return VLC_EGENERIC; + + case DEMUX_GET_TITLE_INFO: + if( p_sys->title && p_sys->title->i_seekpoint > 0 ) + { + input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** ); + int *pi_int = (int*)va_arg( args, int* ); + + *pi_int = 1; + *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) ); + + (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title ); + + return VLC_SUCCESS; + } + return VLC_EGENERIC; + + case DEMUX_SET_TITLE: + if( p_sys->title && p_sys->title->i_seekpoint > 0 ) + { + return VLC_SUCCESS; + } + return VLC_EGENERIC; + + case DEMUX_SET_SEEKPOINT: + /* FIXME do a better implementation */ + i_skp = (int)va_arg( args, int ); + + if( p_sys->title && i_skp < p_sys->title->i_seekpoint) + { + Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1); + p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT; + p_demux->info.i_seekpoint = i_skp; + return VLC_SUCCESS; + } + return VLC_EGENERIC; + + + case DEMUX_SET_TIME: + case DEMUX_GET_FPS: + default: + return VLC_EGENERIC; + } +} + +static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + + *pp_block = NULL; + *pi_ref1 = -1; + *pi_ref2 = -1; + + for( ;; ) + { + EbmlElement *el; + int i_level; - if( p_input->b_die ) + if( p_demux->b_die ) { return VLC_EGENERIC; } @@ -1391,7 +1180,7 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r #define idx p_sys->index[p_sys->i_index - 1] if( p_sys->i_index > 0 && idx.i_time == -1 ) { - idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale; + idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000; idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE; } #undef idx @@ -1405,14 +1194,14 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r p_sys->ep->Up(); continue; } - msg_Warn( p_input, "EOF" ); + msg_Warn( p_demux, "EOF" ); return VLC_EGENERIC; } /* do parsing */ if( i_level == 1 ) { - if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxCluster ) ) { p_sys->cluster = (KaxCluster*)el; @@ -1420,38 +1209,38 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r if( p_sys->i_index == 0 || ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) ) { - IndexAppendCluster( p_input, p_sys->cluster ); + IndexAppendCluster( p_demux, p_sys->cluster ); } p_sys->ep->Down(); } - else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxCues ) ) { - msg_Warn( p_input, "find KaxCues FIXME" ); + msg_Warn( p_demux, "find KaxCues FIXME" ); return VLC_EGENERIC; } else { - msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() ); + msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() ); } } else if( i_level == 2 ) { - if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxClusterTimecode ) ) { KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el; - ctc.ReadData( p_sys->es->I_O() ); + ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA ); p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale ); } - else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxBlockGroup ) ) { p_sys->ep->Down(); } } else if( i_level == 3 ) { - if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxBlock ) ) { *pp_block = (KaxBlock*)el; @@ -1460,14 +1249,14 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r p_sys->ep->Keep(); } - else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxBlockDuration ) ) { KaxBlockDuration &dur = *(KaxBlockDuration*)el; dur.ReadData( p_sys->es->I_O() ); *pi_duration = uint64( dur ); } - else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxReferenceBlock ) ) { KaxReferenceBlock &ref = *(KaxReferenceBlock*)el; @@ -1484,40 +1273,29 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r } else { - msg_Err( p_input, "invalid level = %d", i_level ); + msg_Err( p_demux, "invalid level = %d", i_level ); return VLC_EGENERIC; } } } -static pes_packet_t *MemToPES( input_thread_t *p_input, uint8_t *p_mem, int i_mem ) +static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem) { - pes_packet_t *p_pes; - data_packet_t *p_data; - - if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL ) - { - return NULL; - } - - p_data = input_NewPacket( p_input->p_method_data, i_mem); - - memcpy( p_data->p_payload_start, p_mem, i_mem ); - p_data->p_payload_end = p_data->p_payload_start + i_mem; - - p_pes->p_first = p_pes->p_last = p_data; - p_pes->i_nb_data = 1; - p_pes->i_pes_size = i_mem; - - return p_pes; + block_t *p_block; + if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL; + memcpy( p_block->p_buffer, p_mem, i_mem ); + //p_block->i_rate = p_input->stream.control.i_rate; + return p_block; } -static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration ) +static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts, + mtime_t i_duration ) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; int i_track; unsigned int i; + vlc_bool_t b; #define tk p_sys->track[i_track] for( i_track = 0; i_track < p_sys->i_track; i_track++ ) @@ -1530,116 +1308,74 @@ static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts if( i_track >= p_sys->i_track ) { - msg_Err( p_input, "invalid track number=%d", block->TrackNum() ); + msg_Err( p_demux, "invalid track number=%d", block->TrackNum() ); return; } - - if( tk.p_es->p_decoder_fifo == NULL ) + if( tk.p_es == NULL ) { - tk.b_inited = VLC_FALSE; + msg_Err( p_demux, "unknown track number=%d", block->TrackNum() ); return; } - if( tk.i_cat == AUDIO_ES && p_input->stream.control.b_mute ) + es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b ); + if( !b ) { + tk.b_inited = VLC_FALSE; return; } /* First send init data */ if( !tk.b_inited && tk.i_data_init > 0 ) { - pes_packet_t *p_init; + block_t *p_init; - msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init ); - - if( tk.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ) - { - int i; - int i_offset = 1; - int i_size[3]; - - /* XXX hack split the 3 headers */ - if( tk.p_data_init[0] != 0x02 ) - { - msg_Err( p_input, "invalid vorbis header" ); - } - - for( i = 0; i < 2; i++ ) - { - i_size[i] = 0; - while( i_offset < tk.i_data_init ) - { - i_size[i] += tk.p_data_init[i_offset]; - if( tk.p_data_init[i_offset++] != 0xff ) - { - break; - } - } - } - i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset ); - i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] ); - i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1]; - - p_init = MemToPES( p_input, &tk.p_data_init[i_offset], i_size[0] ); - if( p_init ) - { - input_DecodePES( tk.p_es->p_decoder_fifo, p_init ); - } - p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] ); - if( p_init ) - { - input_DecodePES( tk.p_es->p_decoder_fifo, p_init ); - } - p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] ); - if( p_init ) - { - input_DecodePES( tk.p_es->p_decoder_fifo, p_init ); - } - } - else - { - p_init = MemToPES( p_input, tk.p_data_init, tk.i_data_init ); - if( p_init ) - { - input_DecodePES( tk.p_es->p_decoder_fifo, p_init ); - } - } + msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init ); + p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init ); + if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init ); } tk.b_inited = VLC_TRUE; for( i = 0; i < block->NumberFrames(); i++ ) { - pes_packet_t *p_pes; + block_t *p_block; DataBuffer &data = block->GetBuffer(i); - p_pes = MemToPES( p_input, data.Buffer(), data.Size() ); - if( p_pes == NULL ) + p_block = MemToBlock( p_demux, data.Buffer(), data.Size() ); + + if( p_block == NULL ) { break; } - p_pes->i_pts = i_pts; - p_pes->i_dts = i_pts; +#if defined(HAVE_ZLIB_H) + if( tk.i_compression_type ) + { + p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block ); + } +#endif - if( tk.i_cat == SPU_ES ) + if (i_pts < p_sys->i_start_pts) { - if( i_duration > 0 ) - { - /* FIXME not sure about that */ - p_pes->i_dts += i_duration * 1000;// * (mtime_t) 1000 / p_sys->i_timescale; - } - else - { - p_pes->i_dts = 0; - } - if( p_pes->p_first && p_pes->i_pes_size > 0 ) - { - p_pes->p_first->p_payload_end[-1] = '\0'; - } + p_block->i_pts = -1; + p_block->i_dts = -1; +/* p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;*/ + } + else if( tk.fmt.i_cat != VIDEO_ES ) + { + p_block->i_dts = p_block->i_pts = i_pts; + } + else + { + p_block->i_dts = i_pts; + p_block->i_pts = 0; } - input_DecodePES( tk.p_es->p_decoder_fifo, p_pes ); + if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) ) + { + p_block->i_length = i_duration * 1000; + } + es_out_Send( p_demux->out, tk.p_es, p_block ); /* use time stamp only for first block */ i_pts = 0; @@ -1648,9 +1384,9 @@ static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts #undef tk } -static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) +static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; KaxBlock *block; int64_t i_block_duration; @@ -1661,22 +1397,23 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) int i_track_skipping; int i_track; - msg_Dbg( p_input, "seek request to %lld (%d%%)", i_date, i_percent ); + msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent ); if( i_date < 0 && i_percent < 0 ) { return; } + if( i_percent > 100 ) i_percent = 100; delete p_sys->ep; p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment ); p_sys->cluster = NULL; /* seek without index or without date */ - if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 ) + if( i_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )) { - int64_t i_pos = i_percent * p_input->stream.p_selected_area->i_size / 100; + int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100; - msg_Dbg( p_input, "imprecise way of seeking" ); + msg_Dbg( p_demux, "inacurate way of seeking" ); for( i_index = 0; i_index < p_sys->i_index; i_index++ ) { if( p_sys->index[i_index].i_position >= i_pos) @@ -1689,23 +1426,24 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) i_index--; } - p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning ); + p_sys->in->setFilePointer( p_sys->index[i_index].i_position, + seek_beginning ); if( p_sys->index[i_index].i_position < i_pos ) { EbmlElement *el; - msg_Warn( p_input, "searching for cluster, could take some time" ); + msg_Warn( p_demux, "searching for cluster, could take some time" ); /* search a cluster */ while( ( el = p_sys->ep->Get() ) != NULL ) { - if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxCluster ) ) { KaxCluster *cluster = (KaxCluster*)el; /* add it to the index */ - IndexAppendCluster( p_input, cluster ); + IndexAppendCluster( p_demux, cluster ); if( (int64_t)cluster->GetElementPosition() >= i_pos ) { @@ -1732,10 +1470,13 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) i_index--; } - msg_Dbg( p_input, "seek got %lld (%d%%)", - p_sys->index[i_index].i_time, (int)(100 * p_sys->index[i_index].i_position /p_input->stream.p_selected_area->i_size ) ); + msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)", + p_sys->index[i_index].i_time, + (int)( 100 * p_sys->index[i_index].i_position / + stream_Size( p_demux->s ) ) ); - p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning ); + p_sys->in->setFilePointer( p_sys->index[i_index].i_position, + seek_beginning ); } /* now parse until key frame */ @@ -1743,23 +1484,25 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) i_track_skipping = 0; for( i_track = 0; i_track < p_sys->i_track; i_track++ ) { - if( tk.i_cat == VIDEO_ES ) + if( tk.fmt.i_cat == VIDEO_ES ) { tk.b_search_keyframe = VLC_TRUE; i_track_skipping++; } } + p_sys->i_start_pts = i_date; + while( i_track_skipping > 0 ) { - if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) ) + if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) ) { - msg_Warn( p_input, "cannot get block EOF?" ); + msg_Warn( p_demux, "cannot get block EOF?" ); return; } - p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale; + p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000; for( i_track = 0; i_track < p_sys->i_track; i_track++ ) { @@ -1771,14 +1514,14 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) if( i_track < p_sys->i_track ) { - if( tk.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe ) + if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe ) { tk.b_search_keyframe = VLC_FALSE; i_track_skipping--; } - if( tk.i_cat == VIDEO_ES && !tk.b_search_keyframe ) + if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe ) { - BlockDecode( p_input, block, 0, 0 ); + BlockDecode( p_demux, block, 0, 0 ); } } @@ -1792,9 +1535,9 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent) ***************************************************************************** * Returns -1 in case of error, 0 in case of EOF, 1 otherwise *****************************************************************************/ -static int Demux( input_thread_t * p_input ) +static int Demux( demux_t *p_demux) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; mtime_t i_start_pts; int i_block_count = 0; @@ -1803,57 +1546,25 @@ static int Demux( input_thread_t * p_input ) int64_t i_block_ref1; int64_t i_block_ref2; - if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT ) - { - mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 ); - mtime_t i_date = -1; - int i_percent = -1; - - if( i_duration > 0 ) - { - i_date = (mtime_t)1000000 * - (mtime_t)i_duration* - (mtime_t)p_sys->in->getFilePointer() / - (mtime_t)p_input->stream.p_selected_area->i_size; - } - if( p_input->stream.p_selected_area->i_size > 0 ) - { - i_percent = 100 * p_sys->in->getFilePointer() / - p_input->stream.p_selected_area->i_size; - } - - Seek( p_input, i_date, i_percent); - } - i_start_pts = -1; for( ;; ) { - mtime_t i_pts; - - if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) ) + if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) ) { - msg_Warn( p_input, "cannot get block EOF?" ); + msg_Warn( p_demux, "cannot get block EOF?" ); return 0; } - p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale; + p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000; if( p_sys->i_pts > 0 ) { - input_ClockManageRef( p_input, - p_input->stream.p_selected_program, - p_sys->i_pts * 9 / 100 ); + es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts ); } - i_pts = input_ClockGetTS( p_input, - p_input->stream.p_selected_program, - p_sys->i_pts * 9 / 100 ); - - - - BlockDecode( p_input, block, i_pts, i_block_duration ); + BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration ); delete block; i_block_count++; @@ -1874,141 +1585,58 @@ static int Demux( input_thread_t * p_input ) /***************************************************************************** * Stream managment *****************************************************************************/ -vlc_stream_io_callback::vlc_stream_io_callback( input_thread_t *p_input_ ) +vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ ) { - p_input = p_input_; + s = s_; mb_eof = VLC_FALSE; } -uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size ) -{ - data_packet_t *p_data; - - int i_count; - int i_read = 0; - - if( !i_size || mb_eof ) +uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size ) +{ + if( i_size <= 0 || mb_eof ) { return 0; } - do - { - i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 10240 ) ); - if( i_count <= 0 ) - { - return i_read; - } - memcpy( p_buffer, p_data->p_payload_start, i_count ); - input_DeletePacket( p_input->p_method_data, p_data ); - - (uint8_t*)p_buffer += i_count; - i_size -= i_count; - i_read += i_count; - - } while( i_size ); - - return i_read; + return stream_Read( s, p_buffer, i_size ); } void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode ) { int64_t i_pos; - int64_t i_last; - - i_last = getFilePointer(); - vlc_mutex_lock( &p_input->stream.stream_lock ); switch( mode ) { case seek_beginning: i_pos = i_offset; break; case seek_end: - i_pos = p_input->stream.p_selected_area->i_size - i_offset; + i_pos = stream_Size( s ) - i_offset; break; default: - i_pos= i_last + i_offset; + i_pos= stream_Tell( s ) + i_offset; break; } - if( i_pos < 0 || - ( i_pos > p_input->stream.p_selected_area->i_size && p_input->stream.p_selected_area->i_size != 0 ) ) + if( i_pos < 0 || i_pos >= stream_Size( s ) ) { - msg_Err( p_input, "seeking to wrong place (i_pos=%lld)", i_pos ); - vlc_mutex_unlock( &p_input->stream.stream_lock ); - mb_eof = VLC_TRUE; return; } - vlc_mutex_unlock( &p_input->stream.stream_lock ); mb_eof = VLC_FALSE; - - if( i_pos == i_last ) + if( stream_Seek( s, i_pos ) ) { - return; - } - - msg_Dbg( p_input, "####################seek new=%lld old=%lld", i_pos, getFilePointer() ); - - if( p_input->stream.b_seekable && - ( /*p_input->stream.i_method == INPUT_METHOD_FILE ||*/ i_pos < i_last || i_pos - i_last > p_input->i_bufsize / 4 ) ) - { - input_AccessReinit( p_input ); - p_input->pf_seek( p_input, i_pos ); - } - else if( i_pos > i_last ) - { - data_packet_t *p_data; - int i_skip = i_pos - i_last; - - if( i_skip > 1024 ) - { - msg_Warn( p_input, "will skip %d bytes, slow", i_skip ); - } - - while (i_skip > 0 ) - { - int i_read; - - i_read = input_SplitBuffer( p_input, &p_data, - __MIN( 4096, i_skip ) ); - if( i_read <= 0 ) - { - msg_Err( p_input, "seek failed" ); - mb_eof = VLC_TRUE; - return; - } - i_skip -= i_read; - - input_DeletePacket( p_input->p_method_data, p_data ); - if( i_read == 0 && i_skip > 0 ) - { - msg_Err( p_input, "seek failed" ); - mb_eof = VLC_TRUE; - return; - } - } - } - else - { - msg_Err( p_input, "cannot seek or emulate seek to %lld from %lld", i_pos, i_last ); mb_eof = VLC_TRUE; } + return; } size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size ) { return 0; } -uint64_t vlc_stream_io_callback::getFilePointer( void ) +uint64 vlc_stream_io_callback::getFilePointer( void ) { - uint64_t i_pos; - - vlc_mutex_lock( &p_input->stream.stream_lock ); - i_pos= p_input->stream.p_selected_area->i_tell; - vlc_mutex_unlock( &p_input->stream.stream_lock ); - - return i_pos; + return stream_Tell( s ); } void vlc_stream_io_callback::close( void ) { @@ -2137,70 +1765,71 @@ EbmlElement *EbmlParser::Get( void ) * * * LoadTags : load ... the tags element * - * * InformationsCreate : create all informations, load tags if present + * * InformationCreate : create all information, load tags if present * *****************************************************************************/ -static void LoadCues( input_thread_t *p_input ) +static void LoadCues( demux_t *p_demux ) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; int64_t i_sav_position = p_sys->in->getFilePointer(); EbmlParser *ep; EbmlElement *el, *cues; - msg_Dbg( p_input, "loading cues" ); + msg_Dbg( p_demux, "loading cues" ); p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning ); cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL); if( cues == NULL ) { - msg_Err( p_input, "cannot load cues (broken seekhead or file)" ); + msg_Err( p_demux, "cannot load cues (broken seekhead or file)" ); + p_sys->in->setFilePointer( i_sav_position, seek_beginning ); return; } ep = new EbmlParser( p_sys->es, cues ); while( ( el = ep->Get() ) != NULL ) { - if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxCuePoint ) ) { #define idx p_sys->index[p_sys->i_index] idx.i_track = -1; idx.i_block_number= -1; idx.i_position = -1; - idx.i_time = -1; + idx.i_time = 0; idx.b_key = VLC_TRUE; ep->Down(); while( ( el = ep->Get() ) != NULL ) { - if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxCueTime ) ) { KaxCueTime &ctime = *(KaxCueTime*)el; ctime.ReadData( p_sys->es->I_O() ); - idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale; + idx.i_time = uint64( ctime ) * p_sys->i_timescale / (mtime_t)1000; } - else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxCueTrackPositions ) ) { ep->Down(); while( ( el = ep->Get() ) != NULL ) { - if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxCueTrack ) ) { KaxCueTrack &ctrack = *(KaxCueTrack*)el; ctrack.ReadData( p_sys->es->I_O() ); idx.i_track = uint16( ctrack ); } - else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxCueClusterPosition ) ) { KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el; ccpos.ReadData( p_sys->es->I_O() ); idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) ); } - else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxCueBlockNumber ) ) { KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el; @@ -2209,20 +1838,23 @@ static void LoadCues( input_thread_t *p_input ) } else { - msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() ); + msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() ); } } ep->Up(); } else { - msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() ); + msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() ); } } ep->Up(); - msg_Dbg( p_input, " * added time=%lld pos=%lld track=%d bnum=%d", - idx.i_time, idx.i_position, idx.i_track, idx.i_block_number ); +#if 0 + msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd + " track=%d bnum=%d", idx.i_time, idx.i_position, + idx.i_track, idx.i_block_number ); +#endif p_sys->i_index++; if( p_sys->i_index >= p_sys->i_index_max ) @@ -2234,7 +1866,7 @@ static void LoadCues( input_thread_t *p_input ) } else { - msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() ); + msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() ); } } delete ep; @@ -2242,248 +1874,995 @@ static void LoadCues( input_thread_t *p_input ) p_sys->b_cues = VLC_TRUE; - msg_Dbg( p_input, "loading cues done." ); + msg_Dbg( p_demux, "loading cues done." ); p_sys->in->setFilePointer( i_sav_position, seek_beginning ); } -static void LoadTags( input_thread_t *p_input ) +static void LoadTags( demux_t *p_demux ) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; int64_t i_sav_position = p_sys->in->getFilePointer(); EbmlParser *ep; EbmlElement *el, *tags; - msg_Dbg( p_input, "loading tags" ); + msg_Dbg( p_demux, "loading tags" ); p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning ); tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL); if( tags == NULL ) { - msg_Err( p_input, "cannot load tags (broken seekhead or file)" ); + msg_Err( p_demux, "cannot load tags (broken seekhead or file)" ); + p_sys->in->setFilePointer( i_sav_position, seek_beginning ); return; } - msg_Dbg( p_input, "Tags" ); + msg_Dbg( p_demux, "Tags" ); ep = new EbmlParser( p_sys->es, tags ); while( ( el = ep->Get() ) != NULL ) { - if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxTag ) ) { - msg_Dbg( p_input, "+ Tag" ); + msg_Dbg( p_demux, "+ Tag" ); ep->Down(); while( ( el = ep->Get() ) != NULL ) { - if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId ) + if( MKV_IS_ID( el, KaxTagTargets ) ) { - msg_Dbg( p_input, "| + Targets" ); + msg_Dbg( p_demux, "| + Targets" ); ep->Down(); while( ( el = ep->Get() ) != NULL ) { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() ); } ep->Up(); } - else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagGeneral ) ) { - msg_Dbg( p_input, "| + General" ); + msg_Dbg( p_demux, "| + General" ); ep->Down(); while( ( el = ep->Get() ) != NULL ) { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() ); } ep->Up(); } - else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagGenres ) ) { - msg_Dbg( p_input, "| + Genres" ); + msg_Dbg( p_demux, "| + Genres" ); ep->Down(); while( ( el = ep->Get() ) != NULL ) { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() ); } ep->Up(); } - else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagAudioSpecific ) ) { - msg_Dbg( p_input, "| + Audio Specific" ); + msg_Dbg( p_demux, "| + Audio Specific" ); ep->Down(); while( ( el = ep->Get() ) != NULL ) { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() ); } ep->Up(); } - else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagImageSpecific ) ) { - msg_Dbg( p_input, "| + Images Specific" ); + msg_Dbg( p_demux, "| + Images Specific" ); ep->Down(); while( ( el = ep->Get() ) != NULL ) { - msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() ); } ep->Up(); } - else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiComment ) ) { - msg_Dbg( p_input, "| + Multi Comment" ); + msg_Dbg( p_demux, "| + Multi Comment" ); } - else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiCommercial ) ) { - msg_Dbg( p_input, "| + Multi Commercial" ); + msg_Dbg( p_demux, "| + Multi Commercial" ); } - else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiDate ) ) { - msg_Dbg( p_input, "| + Multi Date" ); + msg_Dbg( p_demux, "| + Multi Date" ); } - else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiEntity ) ) { - msg_Dbg( p_input, "| + Multi Entity" ); + msg_Dbg( p_demux, "| + Multi Entity" ); } - else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) ) { - msg_Dbg( p_input, "| + Multi Identifier" ); + msg_Dbg( p_demux, "| + Multi Identifier" ); } - else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiLegal ) ) { - msg_Dbg( p_input, "| + Multi Legal" ); + msg_Dbg( p_demux, "| + Multi Legal" ); } - else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId ) + else if( MKV_IS_ID( el, KaxTagMultiTitle ) ) { - msg_Dbg( p_input, "| + Multi Title" ); + msg_Dbg( p_demux, "| + Multi Title" ); } else { - msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() ); } } ep->Up(); } else { - msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() ); + msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() ); } } delete ep; delete tags; - msg_Dbg( p_input, "loading tags done." ); + msg_Dbg( p_demux, "loading tags done." ); p_sys->in->setFilePointer( i_sav_position, seek_beginning ); } -static void InformationsCreate( input_thread_t *p_input ) +/***************************************************************************** + * ParseInfo: + *****************************************************************************/ +static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead ) { - demux_sys_t *p_sys = p_input->p_demux_data; - input_info_category_t *p_cat; - int i_track; + demux_sys_t *p_sys = p_demux->p_sys; + EbmlElement *el; + EbmlMaster *m; + unsigned int i; + int i_upper_level = 0; + + msg_Dbg( p_demux, "| + Seek head" ); + + /* Master elements */ + m = static_cast(seekhead); + m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true ); - p_cat = input_InfoCategory( p_input, "Matroska" ); - if( p_sys->f_duration > 1000.1 ) + for( i = 0; i < m->ListSize(); i++ ) { - int64_t i_sec = (int64_t)p_sys->f_duration / 1000; - int h,m,s; + EbmlElement *l = (*m)[i]; - h = i_sec / 3600; - m = ( i_sec / 60 ) % 60; - s = i_sec % 60; + if( MKV_IS_ID( l, KaxSeek ) ) + { + EbmlMaster *sk = static_cast(l); + EbmlId id = EbmlVoid::ClassInfos.GlobalId; + int64_t i_pos = -1; - input_AddInfo( p_cat, _("Duration"), "%d:%2.2d:%2.2d" , h, m, s ); - } + unsigned int j; - if( p_sys->psz_title ) - { - input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title ); + for( j = 0; j < sk->ListSize(); j++ ) + { + EbmlElement *l = (*sk)[j]; + + if( MKV_IS_ID( l, KaxSeekID ) ) + { + KaxSeekID &sid = *(KaxSeekID*)l; + id = EbmlId( sid.GetBuffer(), sid.GetSize() ); + } + else if( MKV_IS_ID( l, KaxSeekPosition ) ) + { + KaxSeekPosition &spos = *(KaxSeekPosition*)l; + i_pos = uint64( spos ); + } + else + { + msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() ); + } + } + + if( i_pos >= 0 ) + { + if( id == KaxCues::ClassInfos.GlobalId ) + { + msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos ); + p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos ); + } + else if( id == KaxChapters::ClassInfos.GlobalId ) + { + msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos ); + p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos ); + } + else if( id == KaxTags::ClassInfos.GlobalId ) + { + msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos ); + p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos ); + } + } + } + else + { + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() ); + } } - if( p_sys->psz_date_utc ) +} + +/***************************************************************************** + * ParseTracks: + *****************************************************************************/ +static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + unsigned int i; + + mkv_track_t *tk; + + msg_Dbg( p_demux, "| | + Track Entry" ); + + p_sys->i_track++; + p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) ); + + /* Init the track */ + tk = &p_sys->track[p_sys->i_track - 1]; + + memset( tk, 0, sizeof( mkv_track_t ) ); + + es_format_Init( &tk->fmt, UNKNOWN_ES, 0 ); + tk->fmt.psz_language = strdup("English"); + tk->fmt.psz_description = NULL; + + tk->b_default = VLC_TRUE; + tk->b_enabled = VLC_TRUE; + tk->i_number = p_sys->i_track - 1; + tk->i_extra_data = 0; + tk->p_extra_data = NULL; + tk->psz_codec = NULL; + tk->i_default_duration = 0; + tk->f_timecodescale = 1.0; + + tk->b_inited = VLC_FALSE; + tk->i_data_init = 0; + tk->p_data_init = NULL; + + tk->psz_codec_name = NULL; + tk->psz_codec_settings = NULL; + tk->psz_codec_info_url = NULL; + tk->psz_codec_download_url = NULL; + + tk->i_compression_type = MATROSKA_COMPRESSION_NONE; + + for( i = 0; i < m->ListSize(); i++ ) { - input_AddInfo( p_cat, _("Date UTC"), "%s" ,p_sys->psz_date_utc ); + EbmlElement *l = (*m)[i]; + + if( MKV_IS_ID( l, KaxTrackNumber ) ) + { + KaxTrackNumber &tnum = *(KaxTrackNumber*)l; + + tk->i_number = uint32( tnum ); + msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) ); + } + else if( MKV_IS_ID( l, KaxTrackUID ) ) + { + KaxTrackUID &tuid = *(KaxTrackUID*)l; + + msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) ); + } + else if( MKV_IS_ID( l, KaxTrackType ) ) + { + char *psz_type; + KaxTrackType &ttype = *(KaxTrackType*)l; + + switch( uint8(ttype) ) + { + case track_audio: + psz_type = "audio"; + tk->fmt.i_cat = AUDIO_ES; + break; + case track_video: + psz_type = "video"; + tk->fmt.i_cat = VIDEO_ES; + break; + case track_subtitle: + psz_type = "subtitle"; + tk->fmt.i_cat = SPU_ES; + break; + default: + psz_type = "unknown"; + tk->fmt.i_cat = UNKNOWN_ES; + break; + } + + msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type ); + } +// else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId ) +// { +// KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l; + +// tk->b_enabled = uint32( fenb ); +// msg_Dbg( p_demux, "| | | + Track Enabled=%u", +// uint32( fenb ) ); +// } + else if( MKV_IS_ID( l, KaxTrackFlagDefault ) ) + { + KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l; + + tk->b_default = uint32( fdef ); + msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) ); + } + else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) + { + KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l; + + msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) ); + } + else if( MKV_IS_ID( l, KaxTrackMinCache ) ) + { + KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l; + + msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) ); + } + else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) + { + KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l; + + msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) ); + } + else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) ) + { + KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l; + + tk->i_default_duration = uint64(defd); + msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) ); + } + else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) ) + { + KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l; + + tk->f_timecodescale = float( ttcs ); + msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale ); + } + else if( MKV_IS_ID( l, KaxTrackName ) ) + { + KaxTrackName &tname = *(KaxTrackName*)l; + + tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) ); + msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description ); + } + else if( MKV_IS_ID( l, KaxTrackLanguage ) ) + { + KaxTrackLanguage &lang = *(KaxTrackLanguage*)l; + + tk->fmt.psz_language = strdup( string( lang ).c_str() ); + msg_Dbg( p_demux, + "| | | + Track Language=`%s'", tk->fmt.psz_language ); + } + else if( MKV_IS_ID( l, KaxCodecID ) ) + { + KaxCodecID &codecid = *(KaxCodecID*)l; + + tk->psz_codec = strdup( string( codecid ).c_str() ); + msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() ); + } + else if( MKV_IS_ID( l, KaxCodecPrivate ) ) + { + KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l; + + tk->i_extra_data = cpriv.GetSize(); + if( tk->i_extra_data > 0 ) + { + tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data ); + memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data ); + } + msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() ); + } + else if( MKV_IS_ID( l, KaxCodecName ) ) + { + KaxCodecName &cname = *(KaxCodecName*)l; + + tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) ); + msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name ); + } + else if( MKV_IS_ID( l, KaxContentEncodings ) ) + { + EbmlMaster *cencs = static_cast(l); + MkvTree( p_demux, 3, "Content Encodings" ); + for( unsigned int i = 0; i < cencs->ListSize(); i++ ) + { + EbmlElement *l2 = (*cencs)[i]; + if( MKV_IS_ID( l2, KaxContentEncoding ) ) + { + MkvTree( p_demux, 4, "Content Encoding" ); + EbmlMaster *cenc = static_cast(l2); + for( unsigned int i = 0; i < cenc->ListSize(); i++ ) + { + EbmlElement *l3 = (*cenc)[i]; + if( MKV_IS_ID( l3, KaxContentEncodingOrder ) ) + { + KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3; + MkvTree( p_demux, 5, "Order: %i", uint32( encord ) ); + } + else if( MKV_IS_ID( l3, KaxContentEncodingScope ) ) + { + KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3; + MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) ); + } + else if( MKV_IS_ID( l3, KaxContentEncodingType ) ) + { + KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3; + MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) ); + } + else if( MKV_IS_ID( l3, KaxContentCompression ) ) + { + EbmlMaster *compr = static_cast(l3); + MkvTree( p_demux, 5, "Content Compression" ); + for( unsigned int i = 0; i < compr->ListSize(); i++ ) + { + EbmlElement *l4 = (*compr)[i]; + if( MKV_IS_ID( l4, KaxContentCompAlgo ) ) + { + KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4; + MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) ); + if( uint32( compalg ) == 0 ) + { + tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB; + } + } + else + { + MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() ); + } + } + } + + else + { + MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() ); + } + } + + } + else + { + MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() ); + } + } + + } +// else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId ) +// { +// KaxCodecSettings &cset = *(KaxCodecSettings*)l; + +// tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) ); +// msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings ); +// } +// else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId ) +// { +// KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l; + +// tk->psz_codec_info_url = strdup( string( ciurl ).c_str() ); +// msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url ); +// } +// else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId ) +// { +// KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l; + +// tk->psz_codec_download_url = strdup( string( cdurl ).c_str() ); +// msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url ); +// } +// else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId ) +// { +// KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l; + +// msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) ); +// } +// else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId ) +// { +// KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l; + +// msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) ); +// } + else if( MKV_IS_ID( l, KaxTrackVideo ) ) + { + EbmlMaster *tkv = static_cast(l); + unsigned int j; + + msg_Dbg( p_demux, "| | | + Track Video" ); + tk->f_fps = 0.0; + + for( j = 0; j < tkv->ListSize(); j++ ) + { + EbmlElement *l = (*tkv)[j]; +// if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId ) +// { +// KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4; + +// msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) ); +// } +// else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId ) +// { +// KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4; + +// msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) ); +// } +// else + if( MKV_IS_ID( l, KaxVideoPixelWidth ) ) + { + KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l; + + tk->fmt.video.i_width = uint16( vwidth ); + msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) ); + } + else if( MKV_IS_ID( l, KaxVideoPixelHeight ) ) + { + KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l; + + tk->fmt.video.i_height = uint16( vheight ); + msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) ); + } + else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) ) + { + KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l; + + tk->fmt.video.i_visible_width = uint16( vwidth ); + msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) ); + } + else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) ) + { + KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l; + + tk->fmt.video.i_visible_height = uint16( vheight ); + msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) ); + } + else if( MKV_IS_ID( l, KaxVideoFrameRate ) ) + { + KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l; + + tk->f_fps = float( vfps ); + msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) ); + } +// else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId ) +// { +// KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l; + +// msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s", +// uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) ); +// } +// else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId ) +// { +// KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l; + +// msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) ); +// } +// else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId ) +// { +// KaxVideoGamma &gamma = *(KaxVideoGamma*)l; + +// msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) ); +// } + else + { + msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() ); + } + } + } + else if( MKV_IS_ID( l, KaxTrackAudio ) ) + { + EbmlMaster *tka = static_cast(l); + unsigned int j; + + msg_Dbg( p_demux, "| | | + Track Audio" ); + + for( j = 0; j < tka->ListSize(); j++ ) + { + EbmlElement *l = (*tka)[j]; + + if( MKV_IS_ID( l, KaxAudioSamplingFreq ) ) + { + KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l; + + tk->fmt.audio.i_rate = (int)float( afreq ); + msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate ); + } + else if( MKV_IS_ID( l, KaxAudioChannels ) ) + { + KaxAudioChannels &achan = *(KaxAudioChannels*)l; + + tk->fmt.audio.i_channels = uint8( achan ); + msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) ); + } + else if( MKV_IS_ID( l, KaxAudioBitDepth ) ) + { + KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l; + + tk->fmt.audio.i_bitspersample = uint8( abits ); + msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) ); + } + else + { + msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() ); + } + } + } + else + { + msg_Dbg( p_demux, "| | | + Unknown (%s)", + typeid(*l).name() ); + } } - if( p_sys->psz_segment_filename ) +} + +static void ParseTracks( demux_t *p_demux, EbmlElement *tracks ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + EbmlElement *el; + EbmlMaster *m; + unsigned int i; + int i_upper_level = 0; + + msg_Dbg( p_demux, "| + Tracks" ); + + /* Master elements */ + m = static_cast(tracks); + m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true ); + + for( i = 0; i < m->ListSize(); i++ ) { - input_AddInfo( p_cat, _("Segment Filename"), "%s" ,p_sys->psz_segment_filename ); + EbmlElement *l = (*m)[i]; + + if( MKV_IS_ID( l, KaxTrackEntry ) ) + { + ParseTrackEntry( p_demux, static_cast(l) ); + } + else + { + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() ); + } } - if( p_sys->psz_muxing_application ) +} + +/***************************************************************************** + * ParseInfo: + *****************************************************************************/ +static void ParseInfo( demux_t *p_demux, EbmlElement *info ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + EbmlElement *el; + EbmlMaster *m; + unsigned int i; + int i_upper_level = 0; + + msg_Dbg( p_demux, "| + Information" ); + + /* Master elements */ + m = static_cast(info); + m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true ); + + for( i = 0; i < m->ListSize(); i++ ) { - input_AddInfo( p_cat, _("Muxing Application"), "%s" ,p_sys->psz_muxing_application ); + EbmlElement *l = (*m)[i]; + + if( MKV_IS_ID( l, KaxSegmentUID ) ) + { + p_sys->segment_uid = *(new KaxSegmentUID(*static_cast(l))); + + msg_Dbg( p_demux, "| | + UID=%d", *(uint32*)p_sys->segment_uid.GetBuffer() ); + } + else if( MKV_IS_ID( l, KaxTimecodeScale ) ) + { + KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l; + + p_sys->i_timescale = uint64(tcs); + + msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd, + p_sys->i_timescale ); + } + else if( MKV_IS_ID( l, KaxDuration ) ) + { + KaxDuration &dur = *(KaxDuration*)l; + + p_sys->f_duration = float(dur); + + msg_Dbg( p_demux, "| | + Duration=%f", + p_sys->f_duration ); + } + else if( MKV_IS_ID( l, KaxMuxingApp ) ) + { + KaxMuxingApp &mapp = *(KaxMuxingApp*)l; + + p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) ); + + msg_Dbg( p_demux, "| | + Muxing Application=%s", + p_sys->psz_muxing_application ); + } + else if( MKV_IS_ID( l, KaxWritingApp ) ) + { + KaxWritingApp &wapp = *(KaxWritingApp*)l; + + p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) ); + + msg_Dbg( p_demux, "| | + Writing Application=%s", + p_sys->psz_writing_application ); + } + else if( MKV_IS_ID( l, KaxSegmentFilename ) ) + { + KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l; + + p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) ); + + msg_Dbg( p_demux, "| | + Segment Filename=%s", + p_sys->psz_segment_filename ); + } + else if( MKV_IS_ID( l, KaxTitle ) ) + { + KaxTitle &title = *(KaxTitle*)l; + + p_sys->psz_title = UTF8ToStr( UTFstring( title ) ); + + msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title ); + } + if( MKV_IS_ID( l, KaxSegmentFamily ) ) + { + KaxSegmentFamily *uid = static_cast(l); + + p_sys->families.push_back(*uid); + + msg_Dbg( p_demux, "| | + family=%d", *(uint32*)uid->GetBuffer() ); + } +#if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN ) + else if( MKV_IS_ID( l, KaxDateUTC ) ) + { + KaxDateUTC &date = *(KaxDateUTC*)l; + time_t i_date; + struct tm tmres; + char buffer[256]; + + i_date = date.GetEpochDate(); + memset( buffer, 0, 256 ); + if( gmtime_r( &i_date, &tmres ) && + asctime_r( &tmres, buffer ) ) + { + buffer[strlen( buffer)-1]= '\0'; + p_sys->psz_date_utc = strdup( buffer ); + msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc ); + } + } +#endif + else + { + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() ); + } } - if( p_sys->psz_writing_application ) + + p_sys->f_duration = p_sys->f_duration * p_sys->i_timescale / 1000000.0; +} + + +/***************************************************************************** + * ParseChapterAtom + *****************************************************************************/ +static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + unsigned int i; + seekpoint_t *sk; + bool b_display_seekpoint = true; + + if( p_sys->title == NULL ) { - input_AddInfo( p_cat, _("Writing Application"), "%s" ,p_sys->psz_writing_application ); + p_sys->title = vlc_input_title_New(); } - input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track ); + sk = vlc_seekpoint_New(); - for( i_track = 0; i_track < p_sys->i_track; i_track++ ) + sk->i_level = i_level; + + msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level ); + for( i = 0; i < ca->ListSize(); i++ ) { - char psz_cat[strlen( "Stream " ) + 10]; -#define tk p_sys->track[i_track] + EbmlElement *l = (*ca)[i]; - sprintf( psz_cat, "Stream %d", i_track ); - p_cat = input_InfoCategory( p_input, psz_cat); - if( tk.psz_name ) + if( MKV_IS_ID( l, KaxChapterUID ) ) { - input_AddInfo( p_cat, _("Name"), "%s", tk.psz_name ); + KaxChapterUID &uid = *(KaxChapterUID*)l; + uint32_t i_uid = uint32( uid ); + msg_Dbg( p_demux, "| | | | + ChapterUID: 0x%x", i_uid ); } - if( tk.psz_codec_name ) + else if( MKV_IS_ID( l, KaxChapterFlagHidden ) ) { - input_AddInfo( p_cat, _("Codec Name"), "%s", tk.psz_codec_name ); + KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l; + b_display_seekpoint = uint8( flag ) == 0; + + msg_Dbg( p_demux, "| | | | + ChapterFlagHidden: %s", b_display_seekpoint ? "no":"yes" ); } - if( tk.psz_codec_settings ) + else if( MKV_IS_ID( l, KaxChapterTimeStart ) ) { - input_AddInfo( p_cat, _("Codec Setting"), "%s", tk.psz_codec_settings ); + KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l; + sk->i_time_offset = uint64( start ) / I64C(1000); + + msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", sk->i_time_offset ); } - if( tk.psz_codec_info_url ) + else if( MKV_IS_ID( l, KaxChapterTimeEnd ) ) { - input_AddInfo( p_cat, _("Codec Info"), "%s", tk.psz_codec_info_url ); + KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l; + int64_t i_end = uint64( end ); + + msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", i_end ); } - if( tk.psz_codec_download_url ) + else if( MKV_IS_ID( l, KaxChapterDisplay ) ) { - input_AddInfo( p_cat, _("Codec Download"), "%s", tk.psz_codec_download_url ); - } + EbmlMaster *cd = static_cast(l); + unsigned int j; - switch( tk.i_cat ) - { - case AUDIO_ES: - input_AddInfo( p_cat, _("Type"), _("Audio") ); - input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec ); - if( tk.i_channels > 0 ) + msg_Dbg( p_demux, "| | | | + ChapterDisplay" ); + for( j = 0; j < cd->ListSize(); j++ ) + { + EbmlElement *l= (*cd)[j]; + + if( MKV_IS_ID( l, KaxChapterString ) ) { - input_AddInfo( p_cat, _("Channels"), "%d", tk.i_channels ); + std::string psz; + int k; + + KaxChapterString &name =*(KaxChapterString*)l; + for (k = 0; k < i_level; k++) + psz += '+'; + psz += ' '; + psz += UTF8ToStr( UTFstring( name ) ); + sk->psz_name = strdup( psz.c_str() ); + msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) ); } - if( tk.i_samplerate > 0 ) + else if( MKV_IS_ID( l, KaxChapterLanguage ) ) { - input_AddInfo( p_cat, _("Sample Rate"), "%d", tk.i_samplerate ); + KaxChapterLanguage &lang =*(KaxChapterLanguage*)l; + const char *psz = string( lang ).c_str(); + + msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz ); } - if( tk.i_bitspersample ) + else if( MKV_IS_ID( l, KaxChapterCountry ) ) { - input_AddInfo( p_cat, _("Bits Per Sample"), "%d", tk.i_bitspersample ); + KaxChapterCountry &ct =*(KaxChapterCountry*)l; + const char *psz = string( ct ).c_str(); + + msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz ); } - break; - case VIDEO_ES: - input_AddInfo( p_cat, _("Type"), _("Video") ); - input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec ); - if( tk.i_width > 0 && tk.i_height ) + } + } + else if( MKV_IS_ID( l, KaxChapterAtom ) ) + { + ParseChapterAtom( p_demux, i_level+1, static_cast(l) ); + } + } + + if (b_display_seekpoint) + { + // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value. + p_sys->title->i_seekpoint++; + p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) ); + p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk; + } + else + { + vlc_seekpoint_Delete(sk); + } +} + +/***************************************************************************** + * ParseChapters: + *****************************************************************************/ +static void ParseChapters( demux_t *p_demux, EbmlElement *chapters ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + EbmlElement *el; + EbmlMaster *m; + unsigned int i; + int i_upper_level = 0; + + + /* Master elements */ + m = static_cast(chapters); + m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true ); + + for( i = 0; i < m->ListSize(); i++ ) + { + EbmlElement *l = (*m)[i]; + + if( MKV_IS_ID( l, KaxEditionEntry ) ) + { + EbmlMaster *E = static_cast(l ); + unsigned int j; + msg_Dbg( p_demux, "| | + EditionEntry" ); + p_sys->edition_ordered = false; + for( j = 0; j < E->ListSize(); j++ ) + { + EbmlElement *l = (*E)[j]; + + if( MKV_IS_ID( l, KaxChapterAtom ) ) { - input_AddInfo( p_cat, _("Resolution"), "%dx%d", tk.i_width, tk.i_height ); + ParseChapterAtom( p_demux, 0, static_cast(l) ); } - if( tk.i_display_width > 0 && tk.i_display_height ) + else if( MKV_IS_ID( l, KaxEditionUID ) ) { - input_AddInfo( p_cat, _("Display Resolution"), "%dx%d", tk.i_display_width, tk.i_display_height ); + p_sys->edition_uid = uint64(*static_cast(l)); } - if( tk.f_fps > 0.1 ) + else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) ) { - input_AddInfo( p_cat, _("Frame Per Second"), "%.3f", tk.f_fps ); + p_sys->edition_ordered = uint8(*static_cast(l)) != 0; } - break; - case SPU_ES: - input_AddInfo( p_cat, _("Type"), _("Subtitle") ); - input_AddInfo( p_cat, _("Codec"), "%s", tk.psz_codec ); - break; + else + { + msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() ); + } + } } + else + { + msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() ); + } + } +} -#undef tk +/***************************************************************************** + * InformationCreate: + *****************************************************************************/ +static void InformationCreate( demux_t *p_demux ) +{ + demux_sys_t *p_sys = p_demux->p_sys; + int i_track; + + p_sys->meta = vlc_meta_New(); + + if( p_sys->psz_title ) + { + vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title ); } - if( p_sys->i_tags_position >= 0 && p_input->stream.b_seekable ) + if( p_sys->psz_date_utc ) { - LoadTags( p_input ); + vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc ); + } + if( p_sys->psz_segment_filename ) + { + vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename ); + } + if( p_sys->psz_muxing_application ) + { + vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application ); + } + if( p_sys->psz_writing_application ) + { + vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application ); + } + + for( i_track = 0; i_track < p_sys->i_track; i_track++ ) + { + mkv_track_t *tk = &p_sys->track[i_track]; + vlc_meta_t *mtk = vlc_meta_New(); + + p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track, + sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) ); + p_sys->meta->track[p_sys->meta->i_track++] = mtk; + + if( tk->fmt.psz_description ) + { + vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description ); + } + if( tk->psz_codec_name ) + { + vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name ); + } + if( tk->psz_codec_settings ) + { + vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings ); + } + if( tk->psz_codec_info_url ) + { + vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url ); + } + if( tk->psz_codec_download_url ) + { + vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url ); + } + } + + if( p_sys->i_tags_position >= 0 ) + { + vlc_bool_t b_seekable; + + stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable ); + if( b_seekable ) + { + LoadTags( p_demux ); + } } } @@ -2492,9 +2871,9 @@ static void InformationsCreate( input_thread_t *p_input ) * Divers *****************************************************************************/ -static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster ) +static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster ) { - demux_sys_t *p_sys = p_input->p_demux_data; + demux_sys_t *p_sys = p_demux->p_sys; #define idx p_sys->index[p_sys->i_index] idx.i_track = -1;