]> 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 5522c418fcc20dfbe421c19856bbedd614f8b69c..c19e1d5d2e5b5133b3e996aa312444a464c81f0a 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * 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"
@@ -218,40 +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_TrackNumber, "PART_NUMBER"},
-                     {vlc_meta_Description, "DESCRIPTION"},
-                     {vlc_meta_Description, "COMMENT"},
-                     {vlc_meta_Rating,      "RATING"},
-                     {vlc_meta_Date,        "DATE_RELEASED"},
-                     {vlc_meta_URL,         "URL"},
-                     {vlc_meta_Publisher,   "PUBLISHER"},
-                     {vlc_meta_EncodedBy,   "ENCODED_BY"},
-                     {vlc_meta_TrackTotal,  "TOTAL_PARTS"},
-                     {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();
@@ -263,39 +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;
         }
     }
-    msg_Dbg( &sys.demuxer, "|   |   + %s: %s", k, v);
-    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 */
@@ -306,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" );
@@ -350,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
         {
@@ -735,11 +830,11 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
         }
     }
 
-#if !defined(WIN32) && !defined(__ANDROID__)
     /* 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 );
 
@@ -749,9 +844,8 @@ 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 )
@@ -797,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;
                 }
@@ -818,11 +912,11 @@ 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;
 
     for(;;)
     {
-        while( i_pts < i_date )
+        do
         {
             bool b_key_picture;
             bool b_discardable_picture;
@@ -865,7 +959,7 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
             }
 
             delete block;
-        }
+        } while( i_pts < i_date );
         if( b_has_key || !i_idx )
             break;
 
@@ -1487,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( ;; )
     {
@@ -1499,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;
@@ -1511,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]
@@ -1587,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 &&
@@ -1661,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];
+}