]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv/matroska_segment.cpp
MKV: map metadata ENCODED-BY
[vlc] / modules / demux / mkv / matroska_segment.cpp
index fe22f6d6585b3c750d81bdbcb287409ff39f4ccb..c3000551e3b53955685b9232f02c18c1e3d64571 100644 (file)
  *****************************************************************************/
 
 #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];
@@ -240,7 +239,9 @@ static const struct {
                      {vlc_meta_Publisher,   "PUBLISHER"},
                      {vlc_meta_URL,         "URL"},
                      {vlc_meta_TrackNumber, "PART_NUMBER"},
+                     {vlc_meta_TrackTotal,  "TOTAL_PARTS"},
                      {vlc_meta_Date,        "DATE_RELEASE"},
+                     {vlc_meta_EncodedBy,   "ENCODED_BY"},
                      {vlc_meta_Title,       NULL},
 };
 
@@ -285,6 +286,7 @@ void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
             goto done;
         }
     }
+    msg_Dbg( &sys.demuxer, "|   |   + %s: %s", k, v);
     vlc_meta_AddExtra( sys.meta, k, v );
 done:
     free( k );
@@ -371,7 +373,8 @@ 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 )
     {
@@ -691,6 +694,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 +724,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,7 +733,7 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
         }
     }
 
-#ifndef WIN32
+#if !defined(WIN32) && !defined(__ANDROID__)
     /* Don't try complex seek if we seek to 0 */
     if( i_date == 0 )
     {
@@ -744,9 +751,9 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
     }
 #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 +780,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++ )
         {
@@ -811,49 +818,63 @@ void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_
     if( unlikely( !p_first ) )
         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++ )
+        while( i_pts < i_date )
         {
-            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;
         }
+        if( b_has_key || !i_idx )
+            break;
 
-        delete block;
+        /* 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 +887,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 )
     {
@@ -1255,21 +1275,63 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
         }
         else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
         {
-            if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
-                p_tk->fmt.i_codec = VLC_CODEC_COOK;
-            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 */
-            fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
-        }
-        else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
-        {
-            p_fmt->i_codec = VLC_CODEC_RA_144;
-            p_fmt->audio.i_channels = 1;
-            p_fmt->audio.i_rate = 8000;
-            p_fmt->audio.i_blockalign = 0x14;
+            if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
+            {
+                p_fmt->i_codec = VLC_CODEC_RA_144;
+                p_fmt->audio.i_channels = 1;
+                p_fmt->audio.i_rate = 8000;
+                p_fmt->audio.i_blockalign = 0x14;
+            }
+            else if( p_tk->i_extra_data > 28 )
+            {
+                uint8_t *p = p_tk->p_extra_data;
+                if( memcmp( p, ".ra", 3 ) ) {
+                    msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
+                    p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+                }
+                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);
+                }
+            }
         }
         else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
         {