fix filename self-references
[vlc.git] / modules / demux / mkv / matroska_segment_parse.cpp
index 3ff89ab..1a5ec21 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * mkv.cpp : matroska demuxer
+ * matroska_segment_parse.cpp : matroska demuxer
  *****************************************************************************
- * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright (C) 2003-2010 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -28,6 +28,8 @@
 
 #include "demux.hpp"
 
+#include "Ebml_parser.hpp"
+
 /*****************************************************************************
  * Some functions to manipulate memory
  *****************************************************************************/
@@ -57,10 +59,12 @@ void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
     {
         if( MKV_IS_ID( l, KaxSeek ) )
         {
-            EbmlId id = EbmlVoid::ClassInfos.GlobalId;
+            EbmlId id = EBML_ID(EbmlVoid);
             int64_t i_pos = -1;
 
+#ifdef MKV_DEBUG
             msg_Dbg( &sys.demuxer, "|   |   + Seek" );
+#endif
             ep->Down();
             while( ( l = ep->Get() ) != NULL )
             {
@@ -86,43 +90,45 @@ void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
 
             if( i_pos >= 0 )
             {
-                if( id == KaxCues::ClassInfos.GlobalId )
+                if( id == EBML_ID(KaxCues) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - cues at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxCues::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
                 }
-                else if( id == KaxInfo::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxInfo) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - info at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxInfo::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
                 }
-                else if( id == KaxChapters::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxChapters) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - chapters at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxChapters::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
                 }
-                else if( id == KaxTags::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxTags) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - tags at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxTags::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
                 }
-                else if( id == KaxSeekHead::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxSeekHead) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - chained seekhead at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxSeekHead::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
                 }
-                else if( id == KaxTracks::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxTracks) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - tracks at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxTracks::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
                 }
-                else if( id == KaxAttachments::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxAttachments) )
                 {
                     msg_Dbg( &sys.demuxer, "|   - attachments at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxAttachments::ClassInfos, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
                 }
+#ifdef MKV_DEBUG
                 else
                     msg_Dbg( &sys.demuxer, "|   - unknown seekhead reference at %"PRId64, i_pos );
+#endif
             }
         }
         else
@@ -140,7 +146,7 @@ static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ...
     va_list args;
     if( i_level > 9 )
     {
-        msg_Err( &demuxer, "too deep tree" );
+        msg_Err( &demuxer, "MKV tree is too deep" );
         return;
     }
     va_start( args, psz_format );
@@ -150,7 +156,7 @@ static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ...
     psz_foo2[ 4 * i_level ] = '+';
     psz_foo2[ 4 * i_level + 1 ] = ' ';
     strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
-    __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
+    msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
     free( psz_foo2 );
     va_end( args );
 }
@@ -161,46 +167,42 @@ static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ...
  *****************************************************************************/
 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 {
-    size_t i, j, k, n;
     bool bSupported = true;
 
-    mkv_track_t *tk;
-
-    msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
-
-    tk = new mkv_track_t();
-
     /* Init the track */
+    mkv_track_t *tk = new mkv_track_t();
     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 = true;
-    tk->b_enabled = true;
-    tk->b_silent = false;
-    tk->i_number = tracks.size() - 1;
-    tk->i_extra_data = 0;
-    tk->p_extra_data = NULL;
-    tk->psz_codec = NULL;
-    tk->b_dts_only = false;
-    tk->i_default_duration = 0;
-    tk->f_timecodescale = 1.0;
-
-    tk->b_inited = 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->fmt.psz_language       = strdup("English");
+    tk->fmt.psz_description    = NULL;
+
+    tk->b_default              = true;
+    tk->b_enabled              = true;
+    tk->b_silent               = false;
+    tk->i_number               = tracks.size() - 1;
+    tk->i_extra_data           = 0;
+    tk->p_extra_data           = NULL;
+    tk->psz_codec              = NULL;
+    tk->b_dts_only             = false;
+    tk->i_default_duration     = 0;
+    tk->f_timecodescale        = 1.0;
+
+    tk->b_inited               = 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;
-    tk->p_compression_data = NULL;
 
-    for( i = 0; i < m->ListSize(); i++ )
+    tk->i_compression_type     = MATROSKA_COMPRESSION_NONE;
+    tk->p_compression_data     = NULL;
+
+    msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
+
+    for( size_t i = 0; i < m->ListSize(); i++ )
     {
         EbmlElement *l = (*m)[i];
 
@@ -248,34 +250,39 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Type=%s", psz_type );
         }
-//        else  if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
-//        {
-//            KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
+        else  if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
+        {
+            KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
 
-//            tk->b_enabled = uint32( fenb );
-//            msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u",
-//                     uint32( fenb )  );
-//        }
+            // tk->b_enabled = uint32( fenb );
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u", uint32( fenb ) );
+        }
         else  if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
         {
             KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
 
             tk->b_default = uint32( fdef );
-            msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef )  );
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef ) );
+        }
+        else  if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
+        {
+            KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Forced=%u", uint32( ffor ) );
         }
-        else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
+        else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
         {
             KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Lacing=%d", uint32( lac ) );
         }
-        else  if( MKV_IS_ID( l, KaxTrackMinCache ) )
+        else  if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
         {
             KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
         }
-        else  if( MKV_IS_ID( l, KaxTrackMaxCache ) )
+        else  if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
         {
             KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
 
@@ -295,6 +302,12 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
             tk->f_timecodescale = float( ttcs );
             msg_Dbg( &sys.demuxer, "|   |   |   + Track TimeCodeScale=%f", tk->f_timecodescale );
         }
+        else  if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
+        {
+            KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Max BlockAdditionID=%d", uint32( mbl ) );
+        }
         else if( MKV_IS_ID( l, KaxTrackName ) )
         {
             KaxTrackName &tname = *(KaxTrackName*)l;
@@ -337,6 +350,19 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
             tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Name=%s", tk->psz_codec_name );
         }
+        //AttachmentLink
+        else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
+        {
+            KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u", uint8( cdall ) );
+        }
+        else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
+        {
+            KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u", uint32( tovr ) );
+        }
         else if( MKV_IS_ID( l, KaxContentEncodings ) )
         {
             EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
@@ -346,14 +372,14 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
                 bSupported = false;
             }
-            for( j = 0; j < cencs->ListSize(); j++ )
+            for( size_t j = 0; j < cencs->ListSize(); j++ )
             {
                 EbmlElement *l2 = (*cencs)[j];
                 if( MKV_IS_ID( l2, KaxContentEncoding ) )
                 {
                     MkvTree( sys.demuxer, 4, "Content Encoding" );
                     EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
-                    for( k = 0; k < cenc->ListSize(); k++ )
+                    for( size_t k = 0; k < cenc->ListSize(); k++ )
                     {
                         EbmlElement *l3 = (*cenc)[k];
                         if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
@@ -375,7 +401,9 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                         {
                             EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
                             MkvTree( sys.demuxer, 5, "Content Compression" );
-                            for( n = 0; n < compr->ListSize(); n++ )
+                            //Default compression type is 0 (Zlib)
+                            tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
+                            for( size_t n = 0; n < compr->ListSize(); n++ )
                             {
                                 EbmlElement *l4 = (*compr)[n];
                                 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
@@ -400,6 +428,7 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                                 }
                             }
                         }
+                        // ContentEncryption Unsupported
                         else
                         {
                             MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
@@ -412,43 +441,30 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 }
             }
         }
-//        else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
+//        else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
 //        {
 //            KaxCodecSettings &cset = *(KaxCodecSettings*)l;
 
 //            tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Settings=%s", tk->psz_codec_settings );
 //        }
-//        else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
+//        else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
 //        {
 //            KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
 
 //            tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_info_url );
 //        }
-//        else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
+//        else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
 //        {
 //            KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
 
 //            tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_download_url );
-//        }
-//        else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
-//        {
-//            KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
-
-//            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
-//        }
-//        else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
-//        {
-//            KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
-
-//            msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u <== UNUSED", uint32( tovr ) );
 //        }
         else  if( MKV_IS_ID( l, KaxTrackVideo ) )
         {
             EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
-            unsigned int j;
             unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
             unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
 
@@ -457,24 +473,23 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 
             tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
             tk->fmt.video.i_frame_rate = 1000000;
-            for( j = 0; j < tkv->ListSize(); j++ )
+
+            for( unsigned int j = 0; j < tkv->ListSize(); j++ )
             {
                 EbmlElement *l = (*tkv)[j];
-//                if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
-//                {
-//                    KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
+                if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
+                {
+                    KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
 
-//                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
-//                }
-//                else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
-//                {
-//                    KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
+                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
+                }
+                else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
+                {
+                    KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
 
-//                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
-//                }
-//                else
-                if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
+                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
+                }
+                else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
                 {
                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
 
@@ -530,28 +545,29 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                     i_crop_left = uint16( cropval );
                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel left=%d", uint16( cropval ) );
                 }
-                else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
-                {
-                    KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
-
-                    tk->f_fps = float( vfps );
-                    msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
-                }
-                else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
+                else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
                 {
                     KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
 
                     i_display_unit = uint8( vdmode );
                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Display Unit=%s",
-                             uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
+                             i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
                 }
-//                else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
-//                {
-//                    KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
+                else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
+                {
+                    KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
 
-//                    msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
-//                }
-//                else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
+                    msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
+                }
+                // ColourSpace UNUSED
+                else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
+                {
+                    KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
+
+                    tk->f_fps = float( vfps );
+                    msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
+                }
+//                else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
 //                {
 //                    KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
 
@@ -569,11 +585,11 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
             }
             if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
             {
-                tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
-                tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
-                tk->fmt.video.i_x_offset = i_crop_left;
-                tk->fmt.video.i_y_offset = i_crop_top;
-                tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
+                tk->fmt.video.i_visible_width   = tk->fmt.video.i_width;
+                tk->fmt.video.i_visible_height  = tk->fmt.video.i_height;
+                tk->fmt.video.i_x_offset        = i_crop_left;
+                tk->fmt.video.i_y_offset        = i_crop_top;
+                tk->fmt.video.i_visible_width  -= i_crop_left + i_crop_right;
                 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
             }
             /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
@@ -582,11 +598,10 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
         else  if( MKV_IS_ID( l, KaxTrackAudio ) )
         {
             EbmlMaster *tka = static_cast<EbmlMaster*>(l);
-            unsigned int j;
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Audio" );
 
-            for( j = 0; j < tka->ListSize(); j++ )
+            for( unsigned int j = 0; j < tka->ListSize(); j++ )
             {
                 EbmlElement *l = (*tka)[j];
 
@@ -648,13 +663,12 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
 {
     EbmlElement *el;
-    unsigned int i;
     int i_upper_level = 0;
 
     /* Master elements */
-    tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
+    tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
 
-    for( i = 0; i < tracks->ListSize(); i++ )
+    for( size_t i = 0; i < tracks->ListSize(); i++ )
     {
         EbmlElement *l = (*tracks)[i];
 
@@ -676,14 +690,13 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
 {
     EbmlElement *el;
     EbmlMaster  *m;
-    size_t i, j;
     int i_upper_level = 0;
 
     /* Master elements */
     m = static_cast<EbmlMaster *>(info);
-    m->Read( es, info->Generic().Context, i_upper_level, el, true );
+    m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
 
-    for( i = 0; i < m->ListSize(); i++ )
+    for( size_t i = 0; i < m->ListSize(); i++ )
     {
         EbmlElement *l = (*m)[i];
 
@@ -790,8 +803,8 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
             KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
             chapter_translation_c *p_translate = new chapter_translation_c();
 
-            p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
-            for( j = 0; j < p_trans->ListSize(); j++ )
+            p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
+            for( size_t j = 0; j < p_trans->ListSize(); j++ )
             {
                 EbmlElement *l = (*p_trans)[j];
 
@@ -827,17 +840,15 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
  *****************************************************************************/
 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
 {
-    size_t i, j;
-
     msg_Dbg( &sys.demuxer, "|   |   |   + ChapterAtom (level=%d)", i_level );
-    for( i = 0; i < ca->ListSize(); i++ )
+    for( size_t i = 0; i < ca->ListSize(); i++ )
     {
         EbmlElement *l = (*ca)[i];
 
         if( MKV_IS_ID( l, KaxChapterUID ) )
         {
             chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
-            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %"PRId64"", chapters.i_uid );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %"PRIu64"", chapters.i_uid );
         }
         else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
         {
@@ -846,6 +857,22 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
 
             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
         }
+        else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
+        {
+            chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
+        }
+        else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
+        {
+            chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterSegmentEditionUID= %u",
+#if LIBMATROSKA_VERSION < 0x010300
+            *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
+#else
+            *(uint32*)chapters.p_segment_edition_uid
+#endif
+            );
+        }
         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
         {
             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
@@ -865,16 +892,14 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
             EbmlMaster *cd = static_cast<EbmlMaster *>(l);
 
             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterDisplay" );
-            for( j = 0; j < cd->ListSize(); j++ )
+            for( size_t j = 0; j < cd->ListSize(); j++ )
             {
                 EbmlElement *l= (*cd)[j];
 
                 if( MKV_IS_ID( l, KaxChapterString ) )
                 {
-                    int k;
-
                     KaxChapterString &name =*(KaxChapterString*)l;
-                    for (k = 0; k < i_level; k++)
+                    for ( int k = 0; k < i_level; k++)
                         chapters.psz_name += '+';
                     chapters.psz_name += ' ';
                     char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
@@ -907,7 +932,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
             KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
             chapter_codec_cmds_c *p_ccodec = NULL;
 
-            for( j = 0; j < cp->ListSize(); j++ )
+            for( size_t j = 0; j < cp->ListSize(); j++ )
             {
                 EbmlElement *k= (*cp)[j];
 
@@ -924,7 +949,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
 
             if ( p_ccodec != NULL )
             {
-                for( j = 0; j < cp->ListSize(); j++ )
+                for( size_t j = 0; j < cp->ListSize(); j++ )
                 {
                     EbmlElement *k= (*cp)[j];
 
@@ -945,7 +970,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
         {
             chapter_item_c *new_sub_chapter = new chapter_item_c();
             ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
-            new_sub_chapter->psz_parent = &chapters;
+            new_sub_chapter->p_parent = &chapters;
             chapters.sub_chapters.push_back( new_sub_chapter );
         }
     }
@@ -959,34 +984,27 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
     EbmlElement *el;
     int i_upper_level = 0;
 
-    attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
+    attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
 
     KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
 
     while( attachedFile && ( attachedFile->GetSize() > 0 ) )
     {
-        std::string psz_mime_type  = GetChild<KaxMimeType>( *attachedFile );
-        KaxFileName  &file_name    = GetChild<KaxFileName>( *attachedFile );
         KaxFileData  &img_data     = GetChild<KaxFileData>( *attachedFile );
+        attachment_c *new_attachment = new attachment_c( ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) ),
+                                                        GetChild<KaxMimeType>( *attachedFile ),
+                                                        img_data.GetSize() );
 
-        attachment_c *new_attachment = new attachment_c();
+        msg_Dbg( &sys.demuxer, "|   |   - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
 
-        if( new_attachment )
+        if( new_attachment->init() )
         {
-            new_attachment->psz_file_name  = ToUTF8( UTFstring( file_name ) );
-            new_attachment->psz_mime_type  = psz_mime_type;
-            new_attachment->i_size         = img_data.GetSize();
-            new_attachment->p_data         = malloc( img_data.GetSize() );
-
-            if( new_attachment->p_data )
-            {
-                memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
-                sys.stored_attachments.push_back( new_attachment );
-            }
-            else
-            {
-                delete new_attachment;
-            }
+             memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
+             sys.stored_attachments.push_back( new_attachment );
+        }
+        else
+        {
+            delete new_attachment;
         }
 
         attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
@@ -999,25 +1017,22 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
 {
     EbmlElement *el;
-    size_t i;
     int i_upper_level = 0;
-    mtime_t i_dur;
 
     /* Master elements */
-    chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
+    chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
 
-    for( i = 0; i < chapters->ListSize(); i++ )
+    for( size_t i = 0; i < chapters->ListSize(); i++ )
     {
         EbmlElement *l = (*chapters)[i];
 
         if( MKV_IS_ID( l, KaxEditionEntry ) )
         {
             chapter_edition_c *p_edition = new chapter_edition_c();
+
             EbmlMaster *E = static_cast<EbmlMaster *>(l );
-            size_t j;
             msg_Dbg( &sys.demuxer, "|   |   + EditionEntry" );
-            for( j = 0; j < E->ListSize(); j++ )
+            for( size_t j = 0; j < E->ListSize(); j++ )
             {
                 EbmlElement *l = (*E)[j];
 
@@ -1033,13 +1048,17 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
                 }
                 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
                 {
-                    p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
+                    p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
                 }
                 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
                 {
                     if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
                         i_default_edition = stored_editions.size();
                 }
+                else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
+                {
+                    // FIXME to implement
+                }
                 else
                 {
                     msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)", typeid(*l).name() );
@@ -1052,33 +1071,19 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
         }
     }
-
-    for( i = 0; i < stored_editions.size(); i++ )
-    {
-        stored_editions[i]->RefreshChapters( );
-    }
-    if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
-    {
-        /* update the duration of the segment according to the sum of all sub chapters */
-        i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
-        if (i_dur > 0)
-            i_duration = i_dur;
-    }
 }
 
 void matroska_segment_c::ParseCluster( )
 {
     EbmlElement *el;
     EbmlMaster  *m;
-    unsigned int i;
     int i_upper_level = 0;
 
     /* Master elements */
     m = static_cast<EbmlMaster *>( cluster );
-    m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
+    m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
 
-    for( i = 0; i < m->ListSize(); i++ )
+    for( unsigned int i = 0; i < m->ListSize(); i++ )
     {
         EbmlElement *l = (*m)[i];
 
@@ -1094,4 +1099,3 @@ void matroska_segment_c::ParseCluster( )
     i_start_time = cluster->GlobalTimecode() / 1000;
 }
 
-