]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv/matroska_segment.cpp
MKV: Set default value for audio tracks when parsing one
[vlc] / modules / demux / mkv / matroska_segment.cpp
index 361e4137e75143e0f53e4114fa0e54ba6896f42d..c19e1d5d2e5b5133b3e996aa312444a464c81f0a 100644 (file)
@@ -1,33 +1,31 @@
 /*****************************************************************************
  * matroska_segment.cpp : matroska demuxer
  *****************************************************************************
- * Copyright (C) 2003-2010 the VideoLAN team
+ * Copyright (C) 2003-2010 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Steve Lhomme <steve.lhomme@free.fr>
  *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include "matroska_segment.hpp"
-
 #include "chapters.hpp"
-
 #include "demux.hpp"
-
+#include "util.hpp"
 #include "Ebml_parser.hpp"
 
 extern "C" {
@@ -87,6 +85,7 @@ matroska_segment_c::~matroska_segment_c()
     {
         delete tracks[i_track]->p_compression_data;
         es_format_Clean( &tracks[i_track]->fmt );
+        delete tracks[i_track]->p_sys;
         free( tracks[i_track]->p_extra_data );
         free( tracks[i_track]->psz_codec );
         delete tracks[i_track];
@@ -219,36 +218,41 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
 }
 
 
-#define PARSE_TAG( type ) \
-    do { \
-    msg_Dbg( &sys.demuxer, "|   + " type ); \
-    ep->Down();                             \
-    while( ( el = ep->Get() ) != NULL )     \
-    {                                       \
-        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() ); \
-    }                                      \
-    ep->Up(); } while( 0 )
-
 static const struct {
     vlc_meta_type_t type;
     const char *key;
-} metadata_map[] = { {vlc_meta_Title,       "TITLE"},
-                     {vlc_meta_Artist,      "ARTIST"},
-                     {vlc_meta_Genre,       "GENRE"},
-                     {vlc_meta_Copyright,   "COPYRIGHT"},
-                     {vlc_meta_Description, "DESCRIPTION"},
-                     {vlc_meta_Publisher,   "PUBLISHER"},
-                     {vlc_meta_URL,         "URL"},
-                     {vlc_meta_TrackNumber, "PART_NUMBER"},
-                     {vlc_meta_Date,        "DATE_RELEASE"},
-                     {vlc_meta_Title,       NULL},
+    int target_type; /* 0 is valid for all target_type */
+} metadata_map[] = {
+                     {vlc_meta_Album,       "TITLE",         50},
+                     {vlc_meta_Title,       "TITLE",         0},
+                     {vlc_meta_Artist,      "ARTIST",        0},
+                     {vlc_meta_Genre,       "GENRE",         0},
+                     {vlc_meta_Copyright,   "COPYRIGHT",     0},
+                     {vlc_meta_TrackNumber, "PART_NUMBER",   0},
+                     {vlc_meta_Description, "DESCRIPTION",   0},
+                     {vlc_meta_Description, "COMMENT",       0},
+                     {vlc_meta_Rating,      "RATING",        0},
+                     {vlc_meta_Date,        "DATE_RELEASED", 0},
+                     {vlc_meta_Date,        "DATE_RELEASE",  0},
+                     {vlc_meta_Date,        "DATE_RECORDED", 0},
+                     {vlc_meta_URL,         "URL",           0},
+                     {vlc_meta_Publisher,   "PUBLISHER",     0},
+                     {vlc_meta_EncodedBy,   "ENCODED_BY",    0},
+                     {vlc_meta_TrackTotal,  "TOTAL_PARTS",   0},
+                     {vlc_meta_Title,       NULL,            0},
 };
 
-void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
+SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_type )
 {
     EbmlElement *el;
     EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
-    char *k = NULL, *v = NULL;
+    SimpleTag * p_simple = new SimpleTag;
+
+    if( !p_simple )
+    {
+        msg_Err( &sys.demuxer, "Couldn't allocate memory for Simple Tag... ignoring it");
+        return NULL;
+    }
 
     if( !sys.meta )
         sys.meta = vlc_meta_New();
@@ -260,38 +264,71 @@ void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
         {
             KaxTagName &key = *(KaxTagName*)el;
             key.ReadData( es.I_O(), SCOPE_ALL_DATA );
-            k = strdup( UTFstring( key ).GetUTF8().c_str() );
+            p_simple->psz_tag_name = strdup( UTFstring( key ).GetUTF8().c_str() );
         }
-        if( MKV_IS_ID( el, KaxTagString ) )
+        else if( MKV_IS_ID( el, KaxTagString ) )
         {
             KaxTagString &value = *(KaxTagString*)el;
             value.ReadData( es.I_O(), SCOPE_ALL_DATA );
-            v = strdup( UTFstring( value ).GetUTF8().c_str() );
+            p_simple->p_value = strdup( UTFstring( value ).GetUTF8().c_str() );
         }
+        else if(  MKV_IS_ID( el, KaxTagLangue ) )
+        {
+            KaxTagLangue &language = *(KaxTagLangue*) el;
+            language.ReadData( es.I_O(), SCOPE_ALL_DATA );
+            p_simple->psz_lang = strdup( string( language ).c_str());
+        }
+        else if(  MKV_IS_ID( el, KaxTagDefault ) )
+        {
+            KaxTagDefault & dft = *(KaxTagDefault*) el;
+            dft.ReadData( es.I_O(), SCOPE_ALL_DATA );
+            p_simple->b_default = (bool) uint8( dft );
+        }
+        /*Tags can be nested*/
+        else if( MKV_IS_ID( el, KaxTagSimple) )
+        {
+            SimpleTag * p_st = ParseSimpleTags( (KaxTagSimple*)el, target_type );
+            if( p_st )
+                p_simple->sub_tags.push_back( p_st );
+        }
+        /*TODO Handle binary tags*/
     }
     delete ep;
 
-    if( !k || !v )
+    if( !p_simple->psz_tag_name || !p_simple->p_value )
     {
         msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
-        return;
+        delete p_simple;
+        return NULL;
     }
 
     for( int i = 0; metadata_map[i].key; i++ )
     {
-        if( !strcmp( k, metadata_map[i].key ) )
+        if( !strcmp( p_simple->psz_tag_name, metadata_map[i].key ) &&
+            (metadata_map[i].target_type == 0 || target_type == metadata_map[i].target_type ) )
         {
-            vlc_meta_Set( sys.meta, metadata_map[i].type, v );
+            vlc_meta_Set( sys.meta, metadata_map[i].type, p_simple->p_value );
+            msg_Dbg( &sys.demuxer, "|   |   + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
             goto done;
         }
     }
-    vlc_meta_AddExtra( sys.meta, k, v );
+    msg_Dbg( &sys.demuxer, "|   |   + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
+    vlc_meta_AddExtra( sys.meta, p_simple->psz_tag_name, p_simple->p_value);
 done:
-    free( k );
-    free( v );
-    return;
+    return p_simple;
 }
 
+#define PARSE_TAG( type ) \
+    do { \
+    msg_Dbg( &sys.demuxer, "|   + " type ); \
+    ep->Down();                             \
+    while( ( el = ep->Get() ) != NULL )     \
+    {                                       \
+        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() ); \
+    }                                      \
+    ep->Up(); } while( 0 )
+
+
 void matroska_segment_c::LoadTags( KaxTags *tags )
 {
     /* Master elements */
@@ -302,12 +339,75 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
     {
         if( MKV_IS_ID( el, KaxTag ) )
         {
+            Tag * p_tag = new Tag;
+            if(!p_tag)
+            {
+                msg_Err( &sys.demuxer,"Couldn't allocate memory for tag... ignoring it");
+                continue;
+            }
             msg_Dbg( &sys.demuxer, "+ Tag" );
             ep->Down();
+            int target_type = 50;
             while( ( el = ep->Get() ) != NULL )
             {
                 if( MKV_IS_ID( el, KaxTagTargets ) )
-                    PARSE_TAG( "Targets" );
+                {
+                    msg_Dbg( &sys.demuxer, "|   + Targets" );
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        if( MKV_IS_ID( el, KaxTagTargetTypeValue ) )
+                        {
+                            KaxTagTargetTypeValue &value = *(KaxTagTargetTypeValue*)el;
+                            value.ReadData( es.I_O() );
+
+                            msg_Dbg( &sys.demuxer, "|   |   + TargetTypeValue: %u", uint32(value));
+                            target_type = uint32(value);
+                        }
+                        if( MKV_IS_ID( el, KaxTagTrackUID ) )
+                        {
+                            p_tag->i_tag_type = TRACK_UID;
+                            KaxTagTrackUID &uid = *(KaxTagTrackUID*) el;
+                            uid.ReadData( es.I_O() );
+                            p_tag->i_uid = uint64( uid );
+                            msg_Dbg( &sys.demuxer, "|   |   + TrackUID: %"PRIu64, p_tag->i_uid);
+
+                        }
+                        if( MKV_IS_ID( el, KaxTagEditionUID ) )
+                        {
+                            p_tag->i_tag_type = EDITION_UID;
+                            KaxTagEditionUID &uid = *(KaxTagEditionUID*) el;
+                            uid.ReadData( es.I_O() );
+                            p_tag->i_uid = uint64( uid );
+                            msg_Dbg( &sys.demuxer, "|   |   + EditionUID: %"PRIu64, p_tag->i_uid);
+                        }
+                        if( MKV_IS_ID( el, KaxTagChapterUID ) )
+                        {
+                            p_tag->i_tag_type = CHAPTER_UID;
+                            KaxTagChapterUID &uid = *(KaxTagChapterUID*) el;
+                            uid.ReadData( es.I_O() );
+                            p_tag->i_uid = uint64( uid );
+                            msg_Dbg( &sys.demuxer, "|   |   + ChapterUID: %"PRIu64, p_tag->i_uid);
+                        }
+                        if( MKV_IS_ID( el, KaxTagAttachmentUID ) )
+                        {
+                            p_tag->i_tag_type = ATTACHMENT_UID;
+                            KaxTagAttachmentUID &uid = *(KaxTagAttachmentUID*) el;
+                            uid.ReadData( es.I_O() );
+                            p_tag->i_uid = uint64( uid );
+                            msg_Dbg( &sys.demuxer, "|   |   + AttachmentUID: %"PRIu64, p_tag->i_uid);
+                        }
+                    }
+                    ep->Up();
+                }
+                else if( MKV_IS_ID( el, KaxTagSimple ) )
+                {
+                    SimpleTag * p_simple =
+                        ParseSimpleTags( static_cast<KaxTagSimple*>( el ),
+                                         target_type );
+                    if( p_simple )
+                        p_tag->simple_tags.push_back( p_simple );
+                }
 #if 0 // not valid anymore
                 else if( MKV_IS_ID( el, KaxTagGeneral ) )
                     PARSE_TAG( "General" );
@@ -346,14 +446,13 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
                     msg_Dbg( &sys.demuxer, "|   + Multi Title" );
                 }
 #endif
-                else if( MKV_IS_ID( el, KaxTagSimple ) )
-                    ParseSimpleTags( static_cast<KaxTagSimple*>( el ) );
                 else
                 {
                     msg_Dbg( &sys.demuxer, "|   + LoadTag Unknown (%s)", typeid( *el ).name() );
                 }
             }
             ep->Up();
+            this->tags.push_back(p_tag);
         }
         else
         {
@@ -371,17 +470,18 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
  *****************************************************************************/
 void matroska_segment_c::InformationCreate( )
 {
-    sys.meta = vlc_meta_New();
+    if( !sys.meta )
+        sys.meta = vlc_meta_New();
 
     if( psz_title )
     {
         vlc_meta_SetTitle( sys.meta, psz_title );
     }
+#if 0
     if( psz_date_utc )
     {
         vlc_meta_SetDate( sys.meta, psz_date_utc );
     }
-#if 0
 
     if( psz_segment_filename )
     {
@@ -691,6 +791,10 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
     spoint *p_first = NULL;
     spoint *p_last = NULL;
     int i_cat;
+    bool b_has_key = false;
+
+    for( size_t i = 0; i < tracks.size(); i++)
+        tracks[i]->i_last_dts = VLC_TS_INVALID;
 
     if( i_global_position >= 0 )
     {
@@ -717,7 +821,7 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
                     ( i_index > 0 &&
                       p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
                 {
-                    ParseCluster();
+                    ParseCluster(false);
                     IndexAppendCluster( cluster );
                 }
                 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
@@ -726,11 +830,11 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
         }
     }
 
-#ifndef WIN32
     /* Don't try complex seek if we seek to 0 */
-    if( i_date == 0 )
+    if( i_date == 0 && i_time_offset == 0 )
     {
-        es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, 0 );
+        es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME,
+                        INT64_C(0) );
         es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
         es.I_O().setFilePointer( i_start_pos );
 
@@ -740,13 +844,12 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
         sys.i_start_pts = 0;
         sys.i_pts = 0;
         sys.i_pcr = 0;
-        return;       
+        return;
     }
-#endif
 
+    int i_idx = 0;
     if ( i_index > 0 )
     {
-        int i_idx = 0;
 
         for( ; i_idx < i_index; i_idx++ )
             if( p_indexes[i_idx].i_time + i_time_offset > i_date )
@@ -773,9 +876,9 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
     es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
 
     /* now parse until key frame */
-    const int es[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
-    i_cat = es[0];
-    for( int i = 0; i < 2; i_cat = es[++i] )
+    const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
+    i_cat = es_types[0];
+    for( int i = 0; i < 2; i_cat = es_types[++i] )
     {
         for( i_track = 0; i_track < tracks.size(); i_track++ )
         {
@@ -788,7 +891,7 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
                     {
                         spoint * tmp = sp;
                         sp = sp->p_next;
-                        delete tmp;                    
+                        delete tmp;
                     }
                     return;
                 }
@@ -809,51 +912,65 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
     }
     /*Neither video nor audio track... no seek further*/
     if( unlikely( !p_first ) )
-        return; 
+        return;
 
-    while( i_pts < i_date )
+    for(;;)
     {
-        bool b_key_picture;
-        bool b_discardable_picture;
-        if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
-        {
-            msg_Warn( &sys.demuxer, "cannot get block EOF?" );
-
-            return;
-        }
-
-        /* check if block's track is in our list */
-        for( i_track = 0; i_track < tracks.size(); i_track++ )
+        do
         {
-            if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
-                (block && tracks[i_track]->i_number == block->TrackNum()) )
-                break;
-        }
+            bool b_key_picture;
+            bool b_discardable_picture;
+            if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
+            {
+                msg_Warn( &sys.demuxer, "cannot get block EOF?" );
+                return;
+            }
 
-        if( simpleblock )
-            i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
-        else
-            i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
-        if( i_track < tracks.size() )
-        {
-            if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
+            /* check if block's track is in our list */
+            for( i_track = 0; i_track < tracks.size(); i_track++ )
             {
-                /* get the seekpoint */
-                spoint * sp;
-                for( sp =  p_first; sp; sp = sp->p_next )
-                    if( sp->i_track == i_track )
-                        break;
+                if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
+                    (block && tracks[i_track]->i_number == block->TrackNum()) )
+                    break;
+            }
 
-                sp->i_date = i_pts;
-                if( simpleblock )
-                    sp->i_seek_pos = simpleblock->GetElementPosition();
-                else
-                    sp->i_seek_pos = i_block_pos;
-                sp->i_cluster_pos = i_cluster_pos;
+            if( simpleblock )
+                i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
+            else
+                i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
+            if( i_track < tracks.size() )
+            {
+                if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
+                {
+                    /* get the seekpoint */
+                    spoint * sp;
+                    for( sp =  p_first; sp; sp = sp->p_next )
+                        if( sp->i_track == i_track )
+                            break;
+
+                    sp->i_date = i_pts;
+                    if( simpleblock )
+                        sp->i_seek_pos = simpleblock->GetElementPosition();
+                    else
+                        sp->i_seek_pos = i_block_pos;
+                    sp->i_cluster_pos = i_cluster_pos;
+                    b_has_key = true;
+                }
             }
-        }
 
-        delete block;
+            delete block;
+        } while( i_pts < i_date );
+        if( b_has_key || !i_idx )
+            break;
+
+        /* No key picture was found in the cluster seek to previous seekpoint */
+        i_date = i_time_offset + p_indexes[i_idx].i_time;
+        i_idx--;
+        i_pts = 0;
+        es.I_O().setFilePointer( p_indexes[i_idx].i_position );
+        delete ep;
+        ep = new EbmlParser( &es, segment, &sys.demuxer );
+        cluster = NULL;
     }
 
     /* rewind to the last I img */
@@ -866,7 +983,6 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
     es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
     cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
 
-
     /* hack use BlockGet to get the cluster then goto the wanted block */
     if ( !cluster )
     {
@@ -1269,14 +1385,45 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
                     msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
                     p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
                 }
-                else {
+                else
+                {
+                    real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
                     if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
+                    {
                         p_tk->fmt.i_codec = VLC_CODEC_COOK;
+                        p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
+                    }
                     else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
                         p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
                     else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
                         p_tk->fmt.i_codec = VLC_CODEC_RA_288;
                     /* FIXME RALF and SIPR */
+                    uint16_t version = (uint16_t) hton16(priv->version);
+                    p_tk->p_sys =
+                        new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
+                                                   hton16(priv->frame_size),
+                                                   hton16(priv->sub_packet_size));
+                    if( unlikely( !p_tk->p_sys ) )
+                        continue;
+
+                    if( unlikely( p_tk->p_sys->Init() ) )
+                        continue;
+
+                    if( version == 4 )
+                    {
+                        real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
+                        p_tk->fmt.audio.i_channels = hton16(v4->channels);
+                        p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
+                        p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
+                    }
+                    else if( version == 5 )
+                    {
+                        real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
+                        p_tk->fmt.audio.i_channels = hton16(v5->channels);
+                        p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
+                        p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
+                    }
+                    msg_Dbg(&sys.demuxer, "%d channels %d bits %d Hz",p_tk->fmt.audio.i_channels, p_tk->fmt.audio.i_bitspersample, p_tk->fmt.audio.i_rate);
 
                     fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
                 }
@@ -1434,6 +1581,7 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
 
     *pb_key_picture         = true;
     *pb_discardable_picture = false;
+    size_t i_tk;
 
     for( ;; )
     {
@@ -1446,7 +1594,7 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
         if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
         {
             /* Check blocks validity to protect againts broken files */
-            if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
+            if( BlockFindTrackIndex( &i_tk, pp_block , pp_simpleblock ) )
             {
                 delete pp_block;
                 pp_simpleblock = NULL;
@@ -1458,6 +1606,29 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
                 *pb_key_picture         = pp_simpleblock->IsKeyframe();
                 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
             }
+            /* We have block group let's check if the picture is a keyframe */
+            else if( *pb_key_picture )
+            {
+                switch(tracks[i_tk]->fmt.i_codec)
+                {
+                    case VLC_CODEC_THEORA:
+                    {
+                        DataBuffer *p_data = &pp_block->GetBuffer(0);
+                        size_t sz = p_data->Size();
+                        const uint8_t * p_buff = p_data->Buffer();
+                        /* if the second bit of a Theora frame is 1 
+                           it's not a keyframe */
+                        if( sz && p_buff )
+                        {
+                            if( p_buff[0] & 0x40 )
+                                *pb_key_picture = false;
+                        }
+                        else
+                            *pb_key_picture = false;
+                        break;
+                    }
+                }
+            }
 
             /* update the index */
 #define idx p_indexes[i_index - 1]
@@ -1534,7 +1705,7 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
 
                 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
                 cluster->InitTimecode( uint64( ctc ), i_timescale );
+
                 /* add it to the index */
                 if( i_index == 0 ||
                     ( i_index > 0 &&
@@ -1608,3 +1779,17 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
     }
 }
 
+SimpleTag::~SimpleTag()
+{
+    free(psz_tag_name);
+    free(psz_lang);
+    free(p_value);
+    for(size_t i = 0; i < sub_tags.size(); i++)
+        delete sub_tags[i];
+}
+
+Tag::~Tag()
+{
+    for(size_t i = 0; i < simple_tags.size(); i++)
+        delete simple_tags[i];
+}