]> git.sesse.net Git - vlc/blob - modules/demux/mkv/mkv.hpp
9cf6ccd10177ea4c8f59a8dd0504f62299545713
[vlc] / modules / demux / mkv / mkv.hpp
1 /*****************************************************************************
2  * mkv.hpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2005, 2008 VLC authors and VideoLAN
5  * $Id$
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 _MKV_H_
26 #define _MKV_H_
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31
32
33 /* config.h may include inttypes.h, so make sure we define that option
34  * early enough. */
35 #define __STDC_FORMAT_MACROS 1
36 #define __STDC_CONSTANT_MACROS 1
37 #define __STDC_LIMIT_MACROS 1
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #include <inttypes.h>
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47
48 #include <time.h>
49
50 #include <vlc_meta.h>
51 #include <vlc_charset.h>
52 #include <vlc_input.h>
53 #include <vlc_demux.h>
54 #include <vlc_aout.h> /* For reordering */
55
56 #include <iostream>
57 #include <cassert>
58 #include <typeinfo>
59 #include <string>
60 #include <vector>
61 #include <algorithm>
62
63 /* libebml and matroska */
64 #include "ebml/EbmlHead.h"
65 #include "ebml/EbmlSubHead.h"
66 #include "ebml/EbmlStream.h"
67 #include "ebml/EbmlContexts.h"
68 #include "ebml/EbmlVoid.h"
69 #include "ebml/EbmlVersion.h"
70 #include "ebml/StdIOCallback.h"
71
72 #include "matroska/KaxAttachments.h"
73 #include "matroska/KaxAttached.h"
74 #include "matroska/KaxBlock.h"
75 #include "matroska/KaxBlockData.h"
76 #include "matroska/KaxChapters.h"
77 #include "matroska/KaxCluster.h"
78 #include "matroska/KaxClusterData.h"
79 #include "matroska/KaxContexts.h"
80 #include "matroska/KaxCues.h"
81 #include "matroska/KaxCuesData.h"
82 #include "matroska/KaxInfo.h"
83 #include "matroska/KaxInfoData.h"
84 #include "matroska/KaxSeekHead.h"
85 #include "matroska/KaxSegment.h"
86 #include "matroska/KaxTag.h"
87 #include "matroska/KaxTags.h"
88 //#include "matroska/KaxTagMulti.h"
89 #include "matroska/KaxTracks.h"
90 #include "matroska/KaxTrackAudio.h"
91 #include "matroska/KaxTrackVideo.h"
92 #include "matroska/KaxTrackEntryData.h"
93 #include "matroska/KaxContentEncoding.h"
94 #include "matroska/KaxVersion.h"
95
96 #include "ebml/StdIOCallback.h"
97
98 #ifdef HAVE_ZLIB_H
99 #   include <zlib.h>
100 #endif
101
102 #define MKV_DEBUG 0
103
104 #define MATROSKA_COMPRESSION_NONE  -1
105 #define MATROSKA_COMPRESSION_ZLIB   0
106 #define MATROSKA_COMPRESSION_BLIB   1
107 #define MATROSKA_COMPRESSION_LZOX   2
108 #define MATROSKA_COMPRESSION_HEADER 3
109
110 enum
111 {
112     MATROSKA_ENCODING_SCOPE_ALL_FRAMES = 1,
113     MATROSKA_ENCODING_SCOPE_PRIVATE = 2,
114     MATROSKA_ENCODING_SCOPE_NEXT = 4 /* unsupported */
115 };
116
117 #define MKVD_TIMECODESCALE 1000000
118
119 #define MKV_IS_ID( el, C ) ( el != NULL && typeid( *el ) == typeid( C ) )
120
121
122 using namespace LIBMATROSKA_NAMESPACE;
123 using namespace std;
124
125 void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
126                   mtime_t i_pts, mtime_t i_duration, bool b_key_picture,
127                   bool b_discardable_picture );
128
129 class attachment_c
130 {
131 public:
132     attachment_c( const std::string& _psz_file_name, const std::string& _psz_mime_type, int _i_size )
133         :i_size(_i_size)
134         ,psz_file_name( _psz_file_name)
135         ,psz_mime_type( _psz_mime_type)
136     {
137         p_data = NULL;
138     }
139     ~attachment_c() { free( p_data ); }
140
141     /* Allocs the data space. Returns true if allocation went ok */
142     bool init()
143     {
144         p_data = malloc( i_size );
145         return (p_data != NULL);
146     }
147
148     const char* fileName() const { return psz_file_name.c_str(); }
149     const char* mimeType() const { return psz_mime_type.c_str(); }
150     int         size() const    { return i_size; }
151
152     void          *p_data;
153 private:
154     int            i_size;
155     std::string    psz_file_name;
156     std::string    psz_mime_type;
157 };
158
159 class matroska_segment_c;
160 struct matroska_stream_c
161 {
162     matroska_stream_c() :p_io_callback(NULL) ,p_estream(NULL) {}
163     ~matroska_stream_c()
164     {
165         delete p_io_callback;
166         delete p_estream;
167     }
168
169     IOCallback         *p_io_callback;
170     EbmlStream         *p_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:
182     virtual ~PrivateTrackData() {}
183     virtual int32_t Init() { return 0; }
184 };
185
186 struct mkv_track_t
187 {
188     bool         b_default;
189     bool         b_enabled;
190     bool         b_forced;
191     unsigned int i_number;
192
193     unsigned int i_extra_data;
194     uint8_t      *p_extra_data;
195
196     char         *psz_codec;
197     bool         b_dts_only;
198     bool         b_pts_only;
199
200     bool         b_no_duration;
201     uint64_t     i_default_duration;
202     float        f_timecodescale;
203     mtime_t      i_last_dts;
204
205     /* video */
206     es_format_t fmt;
207     float       f_fps;
208     es_out_id_t *p_es;
209
210     /* audio */
211     unsigned int i_original_rate;
212     uint8_t i_chans_to_reorder;            /* do we need channel reordering */
213     uint8_t pi_chan_table[AOUT_CHAN_MAX];
214
215
216     /* Private track paramters */
217     PrivateTrackData *p_sys;
218
219     bool            b_inited;
220     /* data to be send first */
221     int             i_data_init;
222     uint8_t         *p_data_init;
223
224     /* hack : it's for seek */
225     bool            b_search_keyframe;
226     bool            b_silent;
227
228     /* informative */
229     const char   *psz_codec_name;
230     const char   *psz_codec_settings;
231     const char   *psz_codec_info_url;
232     const char   *psz_codec_download_url;
233
234     /* encryption/compression */
235     int                    i_compression_type;
236     uint32_t               i_encoding_scope;
237     KaxContentCompSettings *p_compression_data;
238
239     /* Matroska 4 new elements used by Opus */
240     mtime_t i_seek_preroll;
241     mtime_t i_codec_delay;
242
243 };
244
245 struct mkv_index_t
246 {
247     int     i_track;
248     int     i_block_number;
249
250     int64_t i_position;
251     int64_t i_time;
252
253     bool       b_key;
254 };
255
256
257 #endif /* _MKV_HPP_ */