1 /*
2  * Copyright (C) 2000-2018 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Matroska EBML stream handling
21  */
22 #ifndef MATROSKA_H
23 #define MATROSKA_H
24 
25 #include <xine/xine_internal.h>
26 #include <xine/xineutils.h>
27 #include <xine/demux.h>
28 
29 #include "ebml.h"
30 
31 /*
32  * Matroska Element IDs
33  */
34 
35 /* Segment */
36 #define MATROSKA_ID_SEGMENT                       0x18538067
37 
38 /* Meta Seek Information */
39 #define MATROSKA_ID_SEEKHEAD                      0x114D9B74
40 #define MATROSKA_ID_S_ENTRY                       0x4DBB
41 #define MATROSKA_ID_S_ID                          0x53AB
42 #define MATROSKA_ID_S_POSITION                    0x53AC
43 
44 /* Segment Information */
45 #define MATROSKA_ID_INFO                          0x1549A966
46 #define MATROSKA_ID_I_SEGMENTUID                  0x73A4
47 #define MATROSKA_ID_I_FILENAME                    0x7384
48 #define MATROSKA_ID_I_PREVUID                     0x3CB923
49 #define MATROSKA_ID_I_PREVFILENAME                0x3C83AB
50 #define MATROSKA_ID_I_NEXTUID                     0x3EB923
51 #define MATROSKA_ID_I_NEXTFILENAME                0x3E83BB
52 #define MATROSKA_ID_I_TIMECODESCALE               0x2AD7B1
53 #define MATROSKA_ID_I_DURATION                    0x4489
54 #define MATROSKA_ID_I_DATEUTC                     0x4461
55 #define MATROSKA_ID_I_TITLE                       0x7BA9
56 #define MATROSKA_ID_I_MUXINGAPP                   0x4D80
57 #define MATROSKA_ID_I_WRITINGAPP                  0x5741
58 
59 /* Cluster */
60 #define MATROSKA_ID_CLUSTER                       0x1F43B675
61 #define MATROSKA_ID_CL_TIMECODE                   0xE7
62 #define MATROSKA_ID_CL_POSITION                   0xA7
63 #define MATROSKA_ID_CL_PREVSIZE                   0xAB
64 #define MATROSKA_ID_CL_BLOCKGROUP                 0xA0
65 #define MATROSKA_ID_CL_BLOCK                      0xA1
66 #define MATROSKA_ID_CL_BLOCKVIRTUAL               0xA2
67 #define MATROSKA_ID_CL_SIMPLEBLOCK                0xA3
68 #define MATROSKA_ID_CL_BLOCKADDITIONS             0x75A1
69 #define MATROSKA_ID_CL_BLOCKMORE                  0xA6
70 #define MATROSKA_ID_CL_BLOCKADDID                 0xEE
71 #define MATROSKA_ID_CL_BLOCKADDITIONAL            0xA5
72 #define MATROSKA_ID_CL_BLOCKDURATION              0x9B
73 #define MATROSKA_ID_CL_REFERENCEPRIORITY          0xFA
74 #define MATROSKA_ID_CL_REFERENCEBLOCK             0xFB
75 #define MATROSKA_ID_CL_REFERENCEVIRTUAL           0xFD
76 #define MATROSKA_ID_CL_CODECSTATE                 0xA4
77 #define MATROSKA_ID_CL_SLICES                     0x8E
78 #define MATROSKA_ID_CL_TIMESLICE                  0xE8
79 #define MATROSKA_ID_CL_LACENUMBER                 0xCC
80 #define MATROSKA_ID_CL_FRAMENUMBER                0xCD
81 #define MATROSKA_ID_CL_BLOCKADDITIONID            0xCB
82 #define MATROSKA_ID_CL_DELAY                      0xCE
83 #define MATROSKA_ID_CL_DURATION                   0xCF
84 
85 /* Track */
86 #define MATROSKA_ID_TRACKS                        0x1654AE6B
87 #define MATROSKA_ID_TR_ENTRY                      0xAE
88 #define MATROSKA_ID_TR_NUMBER                     0xD7
89 #define MATROSKA_ID_TR_UID                        0x73C5
90 #define MATROSKA_ID_TR_TYPE                       0x83
91 #define MATROSKA_ID_TR_FLAGENABLED                0xB9
92 #define MATROSKA_ID_TR_FLAGDEFAULT                0x88
93 #define MATROSKA_ID_TR_FLAGLACING                 0x9C
94 #define MATROSKA_ID_TR_MINCACHE                   0x6DE7
95 #define MATROSKA_ID_TR_MAXCACHE                   0x6DF8
96 #define MATROSKA_ID_TR_DEFAULTDURATION            0x23E383
97 #define MATROSKA_ID_TR_TIMECODESCALE              0x23314F
98 #define MATROSKA_ID_TR_NAME                       0x536E
99 #define MATROSKA_ID_TR_LANGUAGE                   0x22B59C
100 #define MATROSKA_ID_TR_CODECID                    0x86
101 #define MATROSKA_ID_TR_CODECPRIVATE               0x63A2
102 #define MATROSKA_ID_TR_CODECNAME                  0x258688
103 #define MATROSKA_ID_TR_CODECSETTINGS              0x3A9697
104 #define MATROSKA_ID_TR_CODECINFOURL               0x3B4040
105 #define MATROSKA_ID_TR_CODECDOWNLOADURL           0x26B240
106 #define MATROSKA_ID_TR_CODECDECODEALL             0xAA
107 #define MATROSKA_ID_TR_OVERLAY                    0x6FAB
108 
109 /* Video */
110 #define MATROSKA_ID_TV                            0xE0
111 #define MATROSKA_ID_TV_FLAGINTERLACED             0x9A
112 #define MATROSKA_ID_TV_STEREOMODE                 0x53B9
113 #define MATROSKA_ID_TV_PIXELWIDTH                 0xB0
114 #define MATROSKA_ID_TV_PIXELHEIGHT                0xBA
115 #define MATROSKA_ID_TV_VIDEODISPLAYWIDTH          0x54B0
116 #define MATROSKA_ID_TV_VIDEODISPLAYHEIGHT         0x54BA
117 #define MATROSKA_ID_TV_VIDEOUNIT                  0x54B2
118 #define MATROSKA_ID_TV_ASPECTRATIOTYPE            0x54B3
119 #define MATROSKA_ID_TV_COLOURSPACE                0x2EB524
120 #define MATROSKA_ID_TV_GAMMAVALUE                 0x2FB523
121 
122 /* Audio */
123 #define MATROSKA_ID_TA                            0xE1
124 #define MATROSKA_ID_TA_SAMPLINGFREQUENCY          0xB5
125 #define MATROSKA_ID_TA_OUTPUTSAMPLINGFREQUENCY    0x78B5
126 #define MATROSKA_ID_TA_CHANNELS                   0x9F
127 #define MATROSKA_ID_TA_CHANNELPOSITIONS           0x9F
128 #define MATROSKA_ID_TA_BITDEPTH                   0x6264
129 
130 /* Content Encoding */
131 #define MATROSKA_ID_CONTENTENCODINGS              0x6D80
132 #define MATROSKA_ID_CONTENTENCODING               0x6240
133 #define MATROSKA_ID_CE_ORDER                      0x5031
134 #define MATROSKA_ID_CE_SCOPE                      0x5032
135 #define MATROSKA_ID_CE_TYPE                       0x5033
136 #define MATROSKA_ID_CE_COMPRESSION                0x5034
137 #define MATROSKA_ID_CE_COMPALGO                   0x4254
138 #define MATROSKA_ID_CE_COMPSETTINGS               0x4255
139 #define MATROSKA_ID_CE_ENCRYPTION                 0x5035
140 #define MATROSKA_ID_CE_ENCALGO                    0x47E1
141 #define MATROSKA_ID_CE_ENCKEYID                   0x47E2
142 #define MATROSKA_ID_CE_SIGNATURE                  0x47E3
143 #define MATROSKA_ID_CE_SIGKEYID                   0x47E4
144 #define MATROSKA_ID_CE_SIGALGO                    0x47E5
145 #define MATROSKA_ID_CE_SIGHASHALGO                0x47E6
146 
147 /* Cueing Data */
148 #define MATROSKA_ID_CUES                          0x1C53BB6B
149 #define MATROSKA_ID_CU_POINT                      0xBB
150 #define MATROSKA_ID_CU_TIME                       0xB3
151 #define MATROSKA_ID_CU_TRACKPOSITION              0xB7
152 #define MATROSKA_ID_CU_TRACK                      0xF7
153 #define MATROSKA_ID_CU_CLUSTERPOSITION            0xF1
154 #define MATROSKA_ID_CU_BLOCKNUMBER                0x5387
155 #define MATROSKA_ID_CU_CODECSTATE                 0xEA
156 #define MATROSKA_ID_CU_REFERENCE                  0xDB
157 #define MATROSKA_ID_CU_REFTIME                    0x96
158 #define MATROSKA_ID_CU_REFCLUSTER                 0x97
159 #define MATROSKA_ID_CU_REFNUMBER                  0x535F
160 #define MATROSKA_ID_CU_REFCODECSTATE              0xEB
161 
162 /* Attachements */
163 #define MATROSKA_ID_ATTACHMENTS                   0x1941A469
164 #define MATROSKA_ID_AT_FILE                       0x61A7
165 #define MATROSKA_ID_AT_FILEDESCRIPTION            0x467E
166 #define MATROSKA_ID_AT_FILENAME                   0x466E
167 #define MATROSKA_ID_AT_FILEMIMETYPE               0x4660
168 #define MATROSKA_ID_AT_FILEDATA                   0x465C
169 #define MATROSKA_ID_AT_FILEUID                    0x46AE
170 
171 /* Chapters */
172 #define MATROSKA_ID_CHAPTERS                      0x1043A770
173 #define MATROSKA_ID_CH_EDITIONENTRY               0x45B9
174 #define MATROSKA_ID_CH_ED_UID                     0x45BC
175 #define MATROSKA_ID_CH_ED_HIDDEN                  0x45BD
176 #define MATROSKA_ID_CH_ED_DEFAULT                 0x45DB
177 #define MATROSKA_ID_CH_ED_ORDERED                 0x45DD
178 #define MATROSKA_ID_CH_ATOM                       0xB6
179 #define MATROSKA_ID_CH_UID                        0x73C4
180 #define MATROSKA_ID_CH_TIMESTART                  0x91
181 #define MATROSKA_ID_CH_TIMEEND                    0x92
182 #define MATROSKA_ID_CH_HIDDEN                     0x98
183 #define MATROSKA_ID_CH_ENABLED                    0x4598
184 #define MATROSKA_ID_CH_TRACK                      0x8F
185 #define MATROSKA_ID_CH_TRACKNUMBER                0x89
186 #define MATROSKA_ID_CH_DISPLAY                    0x80
187 #define MATROSKA_ID_CH_STRING                     0x85
188 #define MATROSKA_ID_CH_LANGUAGE                   0x437C
189 #define MATROSKA_ID_CH_COUNTRY                    0x437E
190 
191 /* Tags */
192 #define MATROSKA_ID_TAGS                          0x1254C367
193 
194 /* Chapter (used in tracks) */
195 typedef struct {
196   uint64_t uid;
197   uint64_t time_start;
198   uint64_t time_end;
199   /* if not 0, the chapter could e.g. be used for skipping, but not
200    * be shown in the chapter list */
201   int hidden;
202   /* disabled chapters should be skipped during playback (using this
203    * would require parsing control blocks) */
204   int enabled;
205   /* Tracks this chapter belongs to.
206    * Remember that elements can occur in any order, so in theory the
207    * chapters could become available before the tracks do.
208    * TODO: currently unused
209    */
210   /* uint64_t* tracks; */
211   /* Chapter titles and locale information
212    * TODO: chapters can have multiple sets of those, i.e. several tuples
213    *  (title, language, country). The current implementation picks from
214    *  those by the following rules:
215    *   1) remember the first element
216    *   2) overwrite with an element where language=="eng"
217    */
218   char* title;
219   char* language;
220   char* country;
221 } matroska_chapter_t;
222 
223 /* Edition */
224 typedef struct {
225   uint64_t uid;
226   unsigned int hidden;
227   unsigned int is_default;
228   unsigned int ordered;
229 
230   int num_chapters, cap_chapters;
231   matroska_chapter_t** chapters;
232 } matroska_edition_t;
233 
234 /* Matroska Track */
235 typedef struct {
236   int                      flag_interlaced;
237   int                      pixel_width;
238   int                      pixel_height;
239   int                      display_width;
240   int                      display_height;
241 } matroska_video_track_t;
242 
243 typedef struct {
244   int                      sampling_freq;
245   int                      output_sampling_freq;
246   int                      channels;
247   int                      bits_per_sample;
248 } matroska_audio_track_t;
249 
250 typedef struct {
251   char                     type;
252 
253   /* The rest is used for VobSubs (type = 'v'). */
254   int                      width;
255   int                      height;
256   uint32_t                 palette[16];
257   int                      custom_colors;
258   uint32_t                 colors[4];
259   int                      forced_subs_only;
260 } matroska_sub_track_t;
261 
262 typedef struct matroska_track_s matroska_track_t;
263 struct matroska_track_s {
264   int                      track_num;
265   uint64_t uid;
266 
267   uint32_t                 track_type;
268   uint64_t                 default_duration;
269   char                    *language;
270   char                    *codec_id;
271   uint8_t                 *codec_private;
272   uint32_t                 codec_private_len;
273   int                      default_flag;
274   uint32_t                 compress_algo;
275   uint32_t                 compress_len;
276   char                    *compress_settings;
277 
278   uint32_t                 buf_type;
279   fifo_buffer_t           *fifo;
280 
281   matroska_video_track_t  *video_track;
282   matroska_audio_track_t  *audio_track;
283   matroska_sub_track_t    *sub_track;
284 
285   int64_t                  last_pts;
286   int64_t                  delayed_pts;
287 
288   void                   (*handle_content) (demux_plugin_t *this_gen,
289                                             matroska_track_t *track,
290 		                            int decoder_flags,
291                                             uint8_t *data, size_t data_len,
292                                             int64_t data_pts, int data_duration,
293                                             int input_normpos, int input_time);
294 };
295 
296 /* IDs in the tags master */
297 
298 /*
299  * Matroska Codec IDs. Strings.
300  */
301 
302 #define MATROSKA_CODEC_ID_V_VFW_FOURCC   "V_MS/VFW/FOURCC"
303 #define MATROSKA_CODEC_ID_V_UNCOMPRESSED "V_UNCOMPRESSED"
304 #define MATROSKA_CODEC_ID_V_MPEG4_SP     "V_MPEG4/ISO/SP"
305 #define MATROSKA_CODEC_ID_V_MPEG4_ASP    "V_MPEG4/ISO/ASP"
306 #define MATROSKA_CODEC_ID_V_MPEG4_AP     "V_MPEG4/ISO/AP"
307 #define MATROSKA_CODEC_ID_V_MPEG4_AVC    "V_MPEG4/ISO/AVC"
308 #define MATROSKA_CODEC_ID_V_MSMPEG4V3    "V_MPEG4/MS/V3"
309 #define MATROSKA_CODEC_ID_V_MPEG1        "V_MPEG1"
310 #define MATROSKA_CODEC_ID_V_MPEG2        "V_MPEG2"
311 #define MATROSKA_CODEC_ID_V_MPEG2        "V_MPEG2"
312 #define MATROSKA_CODEC_ID_V_REAL_RV10    "V_REAL/RV10"
313 #define MATROSKA_CODEC_ID_V_REAL_RV20    "V_REAL/RV20"
314 #define MATROSKA_CODEC_ID_V_REAL_RV30    "V_REAL/RV30"
315 #define MATROSKA_CODEC_ID_V_REAL_RV40    "V_REAL/RV40"
316 #define MATROSKA_CODEC_ID_V_MJPEG        "V_MJPEG"
317 #define MATROSKA_CODEC_ID_V_THEORA       "V_THEORA"
318 #define MATROSKA_CODEC_ID_V_VP8          "V_VP8"
319 #define MATROSKA_CODEC_ID_V_VP9          "V_VP9"
320 #define MATROSKA_CODEC_ID_V_HEVC         "V_MPEGH/ISO/HEVC"
321 #define MATROSKA_CODEC_ID_V_AV1          "V_AV1"
322 
323 #define MATROSKA_CODEC_ID_A_MPEG1_L1     "A_MPEG/L1"
324 #define MATROSKA_CODEC_ID_A_MPEG1_L2     "A_MPEG/L2"
325 #define MATROSKA_CODEC_ID_A_MPEG1_L3     "A_MPEG/L3"
326 #define MATROSKA_CODEC_ID_A_PCM_INT_BE   "A_PCM/INT/BIG"
327 #define MATROSKA_CODEC_ID_A_PCM_INT_LE   "A_PCM/INT/LIT"
328 #define MATROSKA_CODEC_ID_A_PCM_FLOAT    "A_PCM/FLOAT/IEEE"
329 #define MATROSKA_CODEC_ID_A_AC3          "A_AC3"
330 #define MATROSKA_CODEC_ID_A_EAC3         "A_EAC3"
331 #define MATROSKA_CODEC_ID_A_DTS          "A_DTS"
332 #define MATROSKA_CODEC_ID_A_VORBIS       "A_VORBIS"
333 #define MATROSKA_CODEC_ID_A_ACM          "A_MS/ACM"
334 #define MATROSKA_CODEC_ID_A_AAC          "A_AAC"
335 #define MATROSKA_CODEC_ID_A_REAL_14_4    "A_REAL/14_4"
336 #define MATROSKA_CODEC_ID_A_REAL_28_8    "A_REAL/28_8"
337 #define MATROSKA_CODEC_ID_A_REAL_COOK    "A_REAL/COOK"
338 #define MATROSKA_CODEC_ID_A_REAL_SIPR    "A_REAL/SIPR"
339 #define MATROSKA_CODEC_ID_A_REAL_RALF    "A_REAL/RALF"
340 #define MATROSKA_CODEC_ID_A_REAL_ATRC    "A_REAL/ATRC"
341 #define MATROSKA_CODEC_ID_A_FLAC         "A_FLAC"
342 #define MATROSKA_CODEC_ID_A_OPUS         "A_OPUS"
343 #define MATROSKA_CODEC_ID_A_TRUEHD       "A_TRUEHD"
344 
345 #define MATROSKA_CODEC_ID_S_TEXT_UTF8    "S_TEXT/UTF8"
346 #define MATROSKA_CODEC_ID_S_TEXT_SSA     "S_TEXT/SSA"
347 #define MATROSKA_CODEC_ID_S_TEXT_ASS     "S_TEXT/ASS"
348 #define MATROSKA_CODEC_ID_S_TEXT_USF     "S_TEXT/USF"
349 #define MATROSKA_CODEC_ID_S_UTF8         "S_UTF8"        /* deprecated */
350 #define MATROSKA_CODEC_ID_S_SSA          "S_SSA"         /* deprecated */
351 #define MATROSKA_CODEC_ID_S_ASS          "S_ASS"         /* deprecated */
352 #define MATROSKA_CODEC_ID_S_DVBSUB       "S_DVBSUB"
353 #define MATROSKA_CODEC_ID_S_VOBSUB       "S_VOBSUB"
354 #define MATROSKA_CODEC_ID_S_HDMV_PGS     "S_HDMV/PGS"
355 #define MATROSKA_CODEC_ID_S_HDMV_TEXTST  "S_HDMV/TEXTST"
356 
357 /* block lacing */
358 #define MATROSKA_NO_LACING               0x0
359 #define MATROSKA_XIPH_LACING             0x1
360 #define MATROSKA_FIXED_SIZE_LACING       0x2
361 #define MATROSKA_EBML_LACING             0x3
362 
363 /* track types */
364 #define MATROSKA_TRACK_VIDEO             0x01
365 #define MATROSKA_TRACK_AUDIO             0x02
366 #define MATROSKA_TRACK_COMPLEX           0x03
367 #define MATROSKA_TRACK_LOGO              0x10
368 #define MATROSKA_TRACK_SUBTITLE          0x11
369 #define MATROSKA_TRACK_CONTROL           0x20
370 
371 /* compression algorithms */
372 #define MATROSKA_COMPRESS_ZLIB           0x00
373 #define MATROSKA_COMPRESS_BZLIB          0x01
374 #define MATROSKA_COMPRESS_LZO1X          0x02
375 #define MATROSKA_COMPRESS_HEADER_STRIP   0x03
376 #define MATROSKA_COMPRESS_UNKNOWN        0xFFFFFFFE  /* Xine internal type */
377 #define MATROSKA_COMPRESS_NONE           0xFFFFFFFF  /* Xine internal type */
378 
379 #endif /* MATROSKA_H */
380