1 /*****************************************************************************
2  * mkv.hpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2005, 2008 VLC authors and VideoLAN
5  * $Id: fa9a1e81a5994a487ec846fed6d8c9a2cec3139e $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #ifndef VLC_MKV_MKV_HPP_
26 #define VLC_MKV_MKV_HPP_
27 
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <inttypes.h>
37 
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 
41 #include <time.h>
42 
43 #include <vlc_meta.h>
44 #include <vlc_charset.h>
45 #include <vlc_input.h>
46 #include <vlc_demux.h>
47 #include <vlc_aout.h> /* For reordering */
48 
49 #include <iostream>
50 #include <cassert>
51 #include <typeinfo>
52 #include <string>
53 #include <vector>
54 #include <algorithm>
55 #include <map>
56 #include <stdexcept>
57 
58 /* libebml and matroska */
59 #include "ebml/EbmlHead.h"
60 #include "ebml/EbmlSubHead.h"
61 #include "ebml/EbmlStream.h"
62 #include "ebml/EbmlContexts.h"
63 #include "ebml/EbmlVoid.h"
64 #include "ebml/EbmlVersion.h"
65 #include "ebml/StdIOCallback.h"
66 
67 #include "matroska/KaxAttachments.h"
68 #include "matroska/KaxAttached.h"
69 #include "matroska/KaxBlock.h"
70 #include "matroska/KaxBlockData.h"
71 #include "matroska/KaxChapters.h"
72 #include "matroska/KaxCluster.h"
73 #include "matroska/KaxClusterData.h"
74 #include "matroska/KaxContexts.h"
75 #include "matroska/KaxCues.h"
76 #include "matroska/KaxCuesData.h"
77 #include "matroska/KaxInfo.h"
78 #include "matroska/KaxInfoData.h"
79 #include "matroska/KaxSeekHead.h"
80 #include "matroska/KaxSegment.h"
81 #include "matroska/KaxTag.h"
82 #include "matroska/KaxTags.h"
83 //#include "matroska/KaxTagMulti.h"
84 #include "matroska/KaxTracks.h"
85 #include "matroska/KaxTrackAudio.h"
86 #include "matroska/KaxTrackVideo.h"
87 #include "matroska/KaxTrackEntryData.h"
88 #include "matroska/KaxContentEncoding.h"
89 #include "matroska/KaxVersion.h"
90 
91 #include "ebml/StdIOCallback.h"
92 
93 #ifdef HAVE_ZLIB_H
94 #   include <zlib.h>
95 #endif
96 
97 #ifndef NDEBUG
98 //# define MKV_DEBUG 0
99 #endif
100 
101 #define MATROSKA_COMPRESSION_NONE  -1
102 #define MATROSKA_COMPRESSION_ZLIB   0
103 #define MATROSKA_COMPRESSION_BLIB   1
104 #define MATROSKA_COMPRESSION_LZOX   2
105 #define MATROSKA_COMPRESSION_HEADER 3
106 
107 enum
108 {
109     MATROSKA_ENCODING_SCOPE_ALL_FRAMES = 1,
110     MATROSKA_ENCODING_SCOPE_PRIVATE = 2,
111     MATROSKA_ENCODING_SCOPE_NEXT = 4 /* unsupported */
112 };
113 
114 #define MKVD_TIMECODESCALE 1000000
115 
116 #define MKV_IS_ID( el, C ) ( el != NULL && (el->operator const EbmlId&()) == (C::ClassInfos.ClassId()) && !el->IsDummy() )
117 #define MKV_CHECKED_PTR_DECL( name, type, src ) type * name = MKV_IS_ID(src, type) ? static_cast<type*>(src) : NULL
118 #define MKV_CHECKED_PTR_DECL_CONST( name, type, src ) const type * name = MKV_IS_ID(src, type) ? static_cast<const type*>(src) : NULL
119 
120 
121 using namespace LIBMATROSKA_NAMESPACE;
122 
123 void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
124                   KaxBlockAdditions *additions,
125                   mtime_t i_pts, mtime_t i_duration, bool b_key_picture,
126                   bool b_discardable_picture );
127 
128 class attachment_c
129 {
130 public:
attachment_c(const std::string & _psz_file_name,const std::string & _psz_mime_type,int _i_size)131     attachment_c( const std::string& _psz_file_name, const std::string& _psz_mime_type, int _i_size )
132         :i_size(_i_size)
133         ,psz_file_name( _psz_file_name)
134         ,psz_mime_type( _psz_mime_type)
135     {
136         p_data = NULL;
137     }
~attachment_c()138     ~attachment_c() { free( p_data ); }
139 
140     /* Allocs the data space. Returns true if allocation went ok */
init()141     bool init()
142     {
143         p_data = malloc( i_size );
144         return (p_data != NULL);
145     }
146 
fileName() const147     const char* fileName() const { return psz_file_name.c_str(); }
mimeType() const148     const char* mimeType() const { return psz_mime_type.c_str(); }
size() const149     int         size() const    { return i_size; }
150 
151     void          *p_data;
152 private:
153     int            i_size;
154     std::string    psz_file_name;
155     std::string    psz_mime_type;
156 };
157 
158 class matroska_segment_c;
159 struct matroska_stream_c
160 {
161     matroska_stream_c(stream_t *s, bool owner);
~matroska_stream_cmatroska_stream_c162     ~matroska_stream_c()
163     {
164         delete io_callback;
165     }
166 
167     bool isUsed() const;
168 
169     IOCallback         * io_callback;
170     EbmlStream         estream;
171 
172     std::vector<matroska_segment_c*> segments;
173 };
174 
175 
176 /*****************************************************************************
177  * definitions of structures and functions used by this plugins
178  *****************************************************************************/
179 class PrivateTrackData
180 {
181 public:
~PrivateTrackData()182     virtual ~PrivateTrackData() {}
Init()183     virtual int32_t Init() { return 0; }
184 };
185 
186 class mkv_track_t
187 {
188     public:
189         mkv_track_t(enum es_format_category_e es_cat);
190         ~mkv_track_t();
191 
192         typedef unsigned int track_id_t;
193 
194         bool         b_default;
195         bool         b_enabled;
196         bool         b_forced;
197         track_id_t   i_number;
198 
199         unsigned int i_extra_data;
200         uint8_t      *p_extra_data;
201 
202         std::string  codec;
203         bool         b_dts_only;
204         bool         b_pts_only;
205 
206         bool         b_no_duration;
207         uint64_t     i_default_duration;
208         float        f_timecodescale;
209         mtime_t      i_last_dts;
210         uint64_t     i_skip_until_fpos; /*< any block before this fpos should be ignored */
211 
212         /* video */
213         es_format_t fmt;
214         float       f_fps;
215         es_out_id_t *p_es;
216 
217         /* audio */
218         unsigned int i_original_rate;
219         uint8_t i_chans_to_reorder;            /* do we need channel reordering */
220         uint8_t pi_chan_table[AOUT_CHAN_MAX];
221 
222 
223         /* Private track paramters */
224         PrivateTrackData *p_sys;
225 
226         bool            b_discontinuity;
227 
228         /* informative */
229         std::string str_codec_name;
230 
231         /* encryption/compression */
232         int                    i_compression_type;
233         uint32_t               i_encoding_scope;
234         KaxContentCompSettings *p_compression_data;
235 
236         /* Matroska 4 new elements used by Opus */
237         mtime_t i_seek_preroll;
238         mtime_t i_codec_delay;
239 };
240 
241 
242 #endif /* _MKV_HPP_ */
243