]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv.cpp
* modules/demux/mkv.cpp: fix for chapters seeking + support for more chapter elements...
[vlc] / modules / demux / mkv.cpp
index 1477151cea8805b8f5c097e7800f53ad0b2d666e..24386dde47abd5100e9424b52fa3bcd93f353e84 100644 (file)
@@ -5,6 +5,7 @@
  * $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
 #include <iostream>
 #include <cassert>
 #include <typeinfo>
+#include <string>
+#include <vector>
+
+#ifdef HAVE_DIRENT_H
+#   include <dirent.h>
+#else
+#   include "../../src/extras/dirent.h"
+#endif
 
 /* libebml and matroska */
 #include "ebml/EbmlHead.h"
 #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/KaxAttachments.h"
 #include "matroska/KaxBlock.h"
 #include "matroska/KaxBlockData.h"
@@ -85,6 +93,15 @@ extern "C" {
 #define MATROSKA_COMPRESSION_NONE 0
 #define MATROSKA_COMPRESSION_ZLIB 1
 
+/**
+ * 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;
 
@@ -298,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;
@@ -322,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;
@@ -340,6 +388,12 @@ struct demux_sys_t
     vlc_meta_t              *meta;
 
     input_title_t           *title;
+
+    std::vector<KaxSegmentFamily> families;
+    std::vector<KaxSegment*> family_members;
+
+    int64_t                  edition_uid;
+    bool                     edition_ordered;
 };
 
 #define MKVD_TIMECODESCALE 1000000
@@ -364,6 +418,9 @@ static int Open( vlc_object_t * p_this )
     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;
 
     int          i_track;
 
@@ -389,9 +446,8 @@ static int Open( vlc_object_t * p_this )
     /* Set the demux function */
     p_demux->pf_demux   = Demux;
     p_demux->pf_control = Control;
-    p_demux->p_sys      = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
+    p_demux->p_sys      = p_sys = new demux_sys_t;
 
-    memset( p_sys, 0, sizeof( demux_sys_t ) );
     p_sys->in = new vlc_stream_io_callback( p_demux->s );
     p_sys->es = new EbmlStream( *p_sys->in );
     p_sys->f_duration   = -1;
@@ -421,7 +477,7 @@ static int Open( vlc_object_t * p_this )
     {
         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 */
@@ -496,6 +552,139 @@ static int Open( vlc_object_t * p_this )
         }
     }
 
+    /* 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 (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0) 
+            {
+                s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
+            }
+        }
+
+        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 (strlen(p_file_item->d_name) > 4)
+                {
+                    s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
+
+                    if (!s_filename.compare(p_demux->psz_path))
+                        continue;
+
+                    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)
+                        {
+                            delete p_stream;
+                            delete p_file_io;
+                            continue;
+                        }
+
+                        p_l0->SkipData(*p_stream, EbmlHead_Context);
+                        delete p_l0;
+
+                        // find all segments in this file
+                        p_l0 = p_stream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
+                        if (p_l0 == NULL)
+                        {
+                            delete p_stream;
+                            delete p_file_io;
+                            continue;
+                        }
+
+                        i_upper_lvl = 0;
+
+                        while (p_l0 != 0)
+                        {
+                            if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
+                            {
+                                EbmlParser  *ep;
+                                KaxSegmentUID *p_uid = NULL;
+
+                                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<KaxInfo*>(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<KaxSegmentUID*>(l);
+                                                if (p_sys->segment_uid == *p_uid)
+                                                    break;
+                                            }
+                                            else if( MKV_IS_ID( l, KaxSegmentFamily ) )
+                                            {
+                                                KaxSegmentFamily *p_fam = static_cast<KaxSegmentFamily*>(l);
+                                                for (j=0; j<p_sys->families.size(); j++)
+                                                {
+                                                    if (p_sys->families.at(j) == *p_fam)
+                                                    {
+                                                        b_this_segment_matches = true;
+                                                        break;
+                                                    }
+                                                }
+                                            }
+                                        }
+                                        break;
+                                    }
+                                }
+
+                                if (b_this_segment_matches)
+                                {
+                                    b_keep_file_opened = true;
+                                }
+                            }
+
+                            p_l0->SkipData(*p_stream, EbmlHead_Context);
+                            delete p_l0;
+                            p_l0 = p_stream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
+                        }
+
+                        if (!b_keep_file_opened)
+                        {
+                            delete p_stream;
+                            delete p_file_io;
+                        }
+                    }
+                }
+            }
+            closedir( p_src_dir );
+        }
+    }
+
+
     if( p_sys->cluster == NULL )
     {
         msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
@@ -569,6 +758,14 @@ static int Open( vlc_object_t * p_this )
             {
                 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' );
@@ -735,6 +932,18 @@ static int Open( vlc_object_t * p_this )
             }
             tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
         }
+        else if( !strcmp( tk.psz_codec, "A_TTA1" ) )
+        {
+            /* 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' );
+        }
+        else if( !strcmp( tk.psz_codec, "A_WAVPACK4" ) )
+        {
+            /* 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' );
+        }
         else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
         {
             tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
@@ -771,6 +980,12 @@ static int Open( vlc_object_t * p_this )
                 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 );
@@ -793,7 +1008,7 @@ static int Open( vlc_object_t * p_this )
 error:
     delete p_sys->es;
     delete p_sys->in;
-    free( p_sys );
+    delete p_sys;
     return VLC_EGENERIC;
 }
 
@@ -839,8 +1054,7 @@ static void Close( vlc_object_t *p_this )
     delete p_sys->ep;
     delete p_sys->es;
     delete p_sys->in;
-
-    free( p_sys );
+    delete p_sys;
 }
 
 /*****************************************************************************
@@ -851,6 +1065,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
     demux_sys_t *p_sys = p_demux->p_sys;
     int64_t     *pi64;
     double      *pf, f;
+    int         i_skp;
 
     vlc_meta_t **pp_meta;
 
@@ -919,11 +1134,13 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 
         case DEMUX_SET_SEEKPOINT:
             /* FIXME do a better implementation */
-            if( p_sys->title && p_sys->title->i_seekpoint > 0 )
-            {
-                int i_skp = (int)va_arg( args, int );
+            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;
@@ -1094,6 +1311,11 @@ static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
         msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
         return;
     }
+    if( tk.p_es == NULL )
+    {
+        msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
+        return;
+    }
 
     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
     if( !b )
@@ -1133,8 +1355,16 @@ static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
         }
 #endif
 
-        if( tk.fmt.i_cat != VIDEO_ES )
+        if (i_pts < p_sys->i_start_pts)
+        {
+            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;
@@ -1179,7 +1409,7 @@ static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
     p_sys->cluster = NULL;
 
     /* seek without index or without date */
-    if( config_GetInt( p_demux, "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 * stream_Size( p_demux->s ) / 100;
 
@@ -1261,6 +1491,8 @@ static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
         }
     }
 
+    p_sys->i_start_pts = i_date;
+
     while( i_track_skipping > 0 )
     {
         if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
@@ -1270,7 +1502,7 @@ static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
             return;
         }
 
-        p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
+        p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000;
 
         for( i_track = 0; i_track < p_sys->i_track; i_track++ )
         {
@@ -1325,7 +1557,7 @@ static int Demux( demux_t *p_demux)
             return 0;
         }
 
-        p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
+        p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000;
 
         if( p_sys->i_pts > 0 )
         {
@@ -2303,9 +2535,9 @@ static void ParseInfo( demux_t *p_demux, EbmlElement *info )
 
         if( MKV_IS_ID( l, KaxSegmentUID ) )
         {
-            KaxSegmentUID &uid = *(KaxSegmentUID*)l;
+            p_sys->segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
 
-            msg_Dbg( p_demux, "|   |   + UID=%d", uint32(uid) );
+            msg_Dbg( p_demux, "|   |   + UID=%d", *(uint32*)p_sys->segment_uid.GetBuffer() );
         }
         else if( MKV_IS_ID( l, KaxTimecodeScale ) )
         {
@@ -2360,6 +2592,14 @@ static void ParseInfo( demux_t *p_demux, EbmlElement *info )
 
             msg_Dbg( p_demux, "|   |   + Title=%s", p_sys->psz_title );
         }
+        if( MKV_IS_ID( l, KaxSegmentFamily ) )
+        {
+            KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(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 ) )
         {
@@ -2397,6 +2637,7 @@ 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 )
     {
@@ -2404,6 +2645,8 @@ static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
     }
     sk = vlc_seekpoint_New();
 
+    sk->i_level = i_level;
+
     msg_Dbg( p_demux, "|   |   |   + ChapterAtom (level=%d)", i_level );
     for( i = 0; i < ca->ListSize(); i++ )
     {
@@ -2415,6 +2658,13 @@ static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
             uint32_t i_uid = uint32( uid );
             msg_Dbg( p_demux, "|   |   |   |   + ChapterUID: 0x%x", i_uid );
         }
+        else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
+        {
+            KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
+            b_display_seekpoint = uint8( flag ) == 0;
+
+            msg_Dbg( p_demux, "|   |   |   |   + ChapterFlagHidden: %s", b_display_seekpoint ? "no":"yes" );
+        }
         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
         {
             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
@@ -2441,10 +2691,16 @@ static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
 
                 if( MKV_IS_ID( l, KaxChapterString ) )
                 {
+                    std::string psz;
+                    int k;
+
                     KaxChapterString &name =*(KaxChapterString*)l;
-                    char *psz = UTF8ToStr( UTFstring( name ) );
-                    sk->psz_name = strdup( psz );
-                    msg_Dbg( p_demux, "|   |   |   |   |    + ChapterString '%s'", psz );
+                    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)) );
                 }
                 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
                 {
@@ -2467,10 +2723,18 @@ static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
             ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
         }
     }
-    // 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;
+
+    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);
+    }
 }
 
 /*****************************************************************************
@@ -2498,6 +2762,7 @@ static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
             EbmlMaster *E = static_cast<EbmlMaster *>(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];
@@ -2506,6 +2771,14 @@ static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
                 {
                     ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
                 }
+                else if( MKV_IS_ID( l, KaxEditionUID ) )
+                {
+                    p_sys->edition_uid = uint64(*static_cast<KaxEditionUID *>(l));
+                }
+                else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
+                {
+                    p_sys->edition_ordered = uint8(*static_cast<KaxEditionFlagOrdered *>(l)) != 0;
+                }
                 else
                 {
                     msg_Dbg( p_demux, "|   |   |   + Unknown (%s)", typeid(*l).name() );