Mercurial > mplayer.hg
annotate libmpdemux/demux_mkv.cpp @ 10125:252428ac7978
Added support for AAC.
author | mosu |
---|---|
date | Sat, 17 May 2003 20:53:12 +0000 |
parents | dc4f1b768062 |
children | d9c9422887ca |
rev | line source |
---|---|
10024 | 1 extern "C" { |
2 #include "config.h" | |
3 } | |
4 | |
5 #ifdef HAVE_MATROSKA | |
6 | |
7 extern "C" { | |
8 #include <stdlib.h> | |
9 #include <stdio.h> | |
10 #include <string.h> | |
11 | |
12 #include "../mp_msg.h" | |
13 #include "../help_mp.h" | |
14 #include "stream.h" | |
15 #include "demuxer.h" | |
16 #include "stheader.h" | |
17 | |
18 #include "../subreader.h" | |
19 #include "../libvo/sub.h" | |
20 } | |
21 | |
22 #include <iostream> | |
23 #include <cassert> | |
24 #include <typeinfo> | |
25 | |
26 #include "EbmlHead.h" | |
27 #include "EbmlSubHead.h" | |
28 #include "EbmlStream.h" | |
29 #include "EbmlContexts.h" | |
30 #include "FileKax.h" | |
31 | |
32 #include "KaxAttachements.h" | |
33 #include "KaxBlock.h" | |
34 #include "KaxBlockData.h" | |
35 #include "KaxChapters.h" | |
36 #include "KaxCluster.h" | |
37 #include "KaxClusterData.h" | |
38 #include "KaxContexts.h" | |
39 #include "KaxCues.h" | |
40 #include "KaxCuesData.h" | |
41 #include "KaxInfo.h" | |
42 #include "KaxInfoData.h" | |
43 #include "KaxSeekHead.h" | |
44 #include "KaxSegment.h" | |
45 #include "KaxTracks.h" | |
46 #include "KaxTrackAudio.h" | |
47 #include "KaxTrackVideo.h" | |
48 | |
49 #include "StdIOCallback.h" | |
50 | |
51 #include "matroska.h" | |
52 | |
53 using namespace LIBMATROSKA_NAMESPACE; | |
54 using namespace std; | |
55 | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
56 // for e.g. "-slang ger" |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
57 extern char *dvdsub_lang; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
58 |
10024 | 59 // default values for Matroska elements |
60 #define MKVD_TIMECODESCALE 1000000 // 1000000 = 1ms | |
61 | |
62 class mpstream_io_callback: public IOCallback { | |
63 private: | |
64 stream_t *s; | |
65 public: | |
66 mpstream_io_callback(stream_t *stream); | |
67 | |
68 virtual uint32 read(void *buffer, size_t size); | |
69 virtual void setFilePointer(int64 offset, seek_mode mode = seek_beginning); | |
70 virtual size_t write(const void *buffer, size_t size); | |
71 virtual uint64 getFilePointer(); | |
72 virtual void close(); | |
73 }; | |
74 | |
75 mpstream_io_callback::mpstream_io_callback(stream_t *stream) { | |
76 s = stream; | |
77 } | |
78 | |
79 uint32 mpstream_io_callback::read(void *buffer, size_t size) { | |
80 uint32_t result; | |
81 | |
82 result = stream_read(s, (char *)buffer, size); | |
83 | |
84 return result; | |
85 } | |
86 | |
87 void mpstream_io_callback::setFilePointer(int64 offset, seek_mode mode) { | |
88 int64 new_pos; | |
89 | |
90 if (mode == seek_beginning) | |
91 new_pos = offset + s->start_pos; | |
92 else if (mode == seek_end) | |
93 new_pos = s->end_pos - offset; | |
94 else | |
95 new_pos = s->pos + offset; | |
96 | |
97 if (new_pos > s->end_pos) { | |
98 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek warning: new_pos %lld > end_pos " | |
99 "%lld\n", new_pos, s->end_pos); | |
100 return; | |
101 } | |
102 | |
103 stream_seek(s, new_pos); | |
104 } | |
105 | |
106 size_t mpstream_io_callback::write(const void */*buffer*/, size_t /*size*/) { | |
107 return 0; | |
108 } | |
109 | |
110 uint64 mpstream_io_callback::getFilePointer() { | |
111 return s->pos - s->buf_len + s->buf_pos; | |
112 } | |
113 | |
114 void mpstream_io_callback::close() { | |
115 } | |
116 | |
117 typedef struct mkv_index_entry { | |
118 uint64_t timecode, filepos; | |
119 int is_key; | |
120 } mkv_index_entry_t; | |
121 | |
122 typedef struct mkv_track_index { | |
123 uint32_t tnum; | |
124 int num_entries; | |
125 mkv_index_entry_t *entries; | |
126 } mkv_track_index_t; | |
127 | |
128 typedef struct mkv_track { | |
129 uint32_t tnum; | |
130 | |
131 char *codec_id; | |
132 int ms_compat; | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
133 char *language; |
10024 | 134 |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
135 char type; // 'v' = video, 'a' = audio, 's' = subs |
10024 | 136 |
137 char v_fourcc[5]; | |
10081
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
138 uint32_t v_width, v_height, v_dwidth, v_dheight; |
10024 | 139 float v_frate; |
140 | |
10125 | 141 uint32_t a_formattag; |
10024 | 142 uint32_t a_channels, a_bps; |
143 float a_sfreq; | |
144 | |
145 int default_track; | |
146 | |
147 void *private_data; | |
148 unsigned int private_size; | |
149 | |
150 unsigned char *headers[3]; | |
151 uint32_t header_sizes[3]; | |
152 | |
153 int ok; | |
154 } mkv_track_t; | |
155 | |
156 typedef struct mkv_demuxer { | |
157 float duration, last_pts; | |
158 uint64_t last_filepos; | |
159 | |
160 mkv_track_t **tracks; | |
161 int num_tracks; | |
162 mkv_track_t *video, *audio, *subs_track; | |
163 | |
164 uint64_t tc_scale, cluster_tc; | |
165 | |
166 mpstream_io_callback *in; | |
167 | |
168 uint64_t clear_subs_at; | |
169 | |
170 subtitle subs; | |
171 | |
172 EbmlStream *es; | |
173 EbmlElement *saved_l1, *saved_l2; | |
174 KaxSegment *segment; | |
175 KaxCluster *cluster; | |
176 | |
177 mkv_track_index_t *index; | |
178 int num_indexes, cues_found, cues_searched; | |
179 int64_t *cluster_positions; | |
180 int num_cluster_pos; | |
181 | |
182 int skip_to_keyframe; | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
183 int64_t skip_to_timecode; |
10024 | 184 } mkv_demuxer_t; |
185 | |
186 static uint16_t get_uint16(const void *buf) { | |
187 uint16_t ret; | |
188 unsigned char *tmp; | |
189 | |
190 tmp = (unsigned char *) buf; | |
191 | |
192 ret = tmp[1] & 0xff; | |
193 ret = (ret << 8) + (tmp[0] & 0xff); | |
194 | |
195 return ret; | |
196 } | |
197 | |
198 static uint32_t get_uint32(const void *buf) { | |
199 uint32_t ret; | |
200 unsigned char *tmp; | |
201 | |
202 tmp = (unsigned char *) buf; | |
203 | |
204 ret = tmp[3] & 0xff; | |
205 ret = (ret << 8) + (tmp[2] & 0xff); | |
206 ret = (ret << 8) + (tmp[1] & 0xff); | |
207 ret = (ret << 8) + (tmp[0] & 0xff); | |
208 | |
209 return ret; | |
210 } | |
211 | |
212 static void handle_subtitles(demuxer_t *d, KaxBlock *block, int64_t duration) { | |
213 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)d->priv; | |
214 int len, line, state; | |
215 char *s1, *s2, *buffer; | |
216 | |
217 if (duration == -1) { | |
218 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Warning: No KaxBlockDuration " | |
219 "element for subtitle track found.\n"); | |
220 return; | |
221 } | |
222 | |
223 DataBuffer &data = block->GetBuffer(0); | |
224 len = data.Size(); | |
225 | |
226 buffer = (char *)data.Buffer(); | |
227 s1 = buffer; | |
228 | |
229 while (((*s1 == '\n') || (*s1 == '\r')) && | |
230 ((unsigned int)(s1 - buffer) <= data.Size())) | |
231 s1++; | |
232 | |
233 line = 0; | |
234 s2 = mkv_d->subs.text[0]; | |
235 mkv_d->subs.lines = 1; | |
236 state = 0; | |
237 while ((unsigned int)(s1 - buffer) != data.Size()) { | |
238 if ((*s1 == '\n') || (*s1 == '\r')) { | |
239 if (state == 0) { // normal char --> newline | |
240 if (mkv_d->subs.lines == SUB_MAX_TEXT) | |
241 break; | |
242 *s2 = 0; | |
243 s2 = mkv_d->subs.text[mkv_d->subs.lines]; | |
244 mkv_d->subs.lines++; | |
245 state = 1; | |
246 } | |
247 } else if (*s1 == '<') // skip HTML tags | |
248 state = 2; | |
249 else if (*s1 == '>') | |
250 state = 0; | |
251 else if (state != 2) { // normal character | |
252 state = 0; | |
253 if ((s2 - mkv_d->subs.text[mkv_d->subs.lines - 1]) < 255) { | |
254 *s2 = *s1; | |
255 s2++; | |
256 } | |
257 } | |
258 s1++; | |
259 } | |
260 | |
261 *s2 = 0; | |
262 | |
263 #ifdef USE_ICONV | |
264 subcp_recode1(&mkv_d->subs); | |
265 #endif | |
266 | |
267 vo_sub = &mkv_d->subs; | |
268 vo_osd_changed(OSDTYPE_SUBTITLE); | |
269 | |
270 mkv_d->clear_subs_at = block->GlobalTimecode() / 1000000 + duration; | |
271 } | |
272 | |
273 static mkv_track_t *new_mkv_track(mkv_demuxer_t *d) { | |
274 mkv_track_t *t; | |
275 | |
276 t = (mkv_track_t *)malloc(sizeof(mkv_track_t)); | |
277 if (t != NULL) { | |
278 memset(t, 0, sizeof(mkv_track_t)); | |
279 d->tracks = (mkv_track_t **)realloc(d->tracks, (d->num_tracks + 1) * | |
280 sizeof(mkv_track_t *)); | |
281 if (d->tracks == NULL) | |
282 return NULL; | |
283 d->tracks[d->num_tracks] = t; | |
284 d->num_tracks++; | |
10036
1dfe4dab4a42
Implemented some default values. The corresponding Kax elements are not stored if they are set to their default value.
mosu
parents:
10029
diff
changeset
|
285 |
1dfe4dab4a42
Implemented some default values. The corresponding Kax elements are not stored if they are set to their default value.
mosu
parents:
10029
diff
changeset
|
286 // Set default values. |
1dfe4dab4a42
Implemented some default values. The corresponding Kax elements are not stored if they are set to their default value.
mosu
parents:
10029
diff
changeset
|
287 t->default_track = 1; |
1dfe4dab4a42
Implemented some default values. The corresponding Kax elements are not stored if they are set to their default value.
mosu
parents:
10029
diff
changeset
|
288 t->a_sfreq = 8000.0; |
1dfe4dab4a42
Implemented some default values. The corresponding Kax elements are not stored if they are set to their default value.
mosu
parents:
10029
diff
changeset
|
289 t->a_channels = 1; |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
290 t->language = strdup("eng"); |
10024 | 291 } |
292 | |
293 return t; | |
294 } | |
295 | |
296 static mkv_track_t *find_track_by_num(mkv_demuxer_t *d, uint32_t n, | |
297 mkv_track_t *c) { | |
298 int i; | |
299 | |
300 for (i = 0; i < d->num_tracks; i++) | |
301 if ((d->tracks[i] != NULL) && (d->tracks[i]->tnum == n) && | |
302 (d->tracks[i] != c)) | |
303 return d->tracks[i]; | |
304 | |
305 return NULL; | |
306 } | |
307 | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
308 static mkv_track_t *find_track_by_language(mkv_demuxer_t *d, char *language, |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
309 mkv_track_t *c, char type = 's') { |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
310 int i; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
311 |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
312 for (i = 0; i < d->num_tracks; i++) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
313 if ((d->tracks[i] != NULL) && (d->tracks[i] != c) && |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
314 (d->tracks[i]->language != NULL) && |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
315 !strcmp(d->tracks[i]->language, language) && |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
316 (d->tracks[i]->type == type)) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
317 return d->tracks[i]; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
318 |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
319 return NULL; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
320 } |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
321 |
10024 | 322 static int check_track_information(mkv_demuxer_t *d) { |
10028
7ca8b302032f
Loop counter was used inside for another loop as well.
mosu
parents:
10024
diff
changeset
|
323 int i, track_num; |
10024 | 324 unsigned char *c; |
325 uint32_t u, offset, length; | |
326 mkv_track_t *t; | |
327 BITMAPINFOHEADER *bih; | |
328 WAVEFORMATEX *wfe; | |
329 | |
10028
7ca8b302032f
Loop counter was used inside for another loop as well.
mosu
parents:
10024
diff
changeset
|
330 for (track_num = 0; track_num < d->num_tracks; track_num++) { |
7ca8b302032f
Loop counter was used inside for another loop as well.
mosu
parents:
10024
diff
changeset
|
331 |
7ca8b302032f
Loop counter was used inside for another loop as well.
mosu
parents:
10024
diff
changeset
|
332 t = d->tracks[track_num]; |
10024 | 333 switch (t->type) { |
334 case 'v': // video track | |
335 if (t->codec_id == NULL) | |
336 continue; | |
337 if (!strcmp(t->codec_id, MKV_V_MSCOMP)) { | |
338 if ((t->private_data == NULL) || | |
339 (t->private_size < sizeof(BITMAPINFOHEADER))) { | |
340 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: CodecID for track " | |
341 "%u is '" MKV_V_MSCOMP "', but there was no " | |
342 "BITMAPINFOHEADER struct present. Therefore we don't have " | |
343 "a FourCC to identify the video codec used.\n", t->tnum); | |
344 continue; | |
345 } else { | |
346 t->ms_compat = 1; | |
347 | |
348 bih = (BITMAPINFOHEADER *)t->private_data; | |
349 | |
350 u = get_uint32(&bih->biWidth); | |
351 if (t->v_width != u) { | |
352 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: (MS " | |
353 "compatibility mode, track %u) " | |
354 "Matrosa says video width is %u, but the " | |
355 "BITMAPINFOHEADER says %u.\n", t->tnum, t->v_width, u); | |
356 if (t->v_width == 0) | |
357 t->v_width = u; | |
358 } | |
359 | |
360 u = get_uint32(&bih->biHeight); | |
361 if (t->v_height != u) { | |
362 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: (MS compatibility " | |
363 "mode, track %u) " | |
364 "Matrosa video height is %u, but the BITMAPINFOHEADER " | |
365 "says %u.\n", t->tnum, t->v_height, u); | |
366 if (t->v_height == 0) | |
367 t->v_height = u; | |
368 } | |
369 | |
370 memcpy(t->v_fourcc, &bih->biCompression, 4); | |
371 | |
372 if (t->v_frate == 0.0) { | |
373 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] ERROR: (MS compatibility " | |
374 "mode, track %u) " | |
375 "No VideoFrameRate element was found.\n", t->tnum); | |
376 continue; | |
377 } | |
378 } | |
379 } else { | |
380 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Native CodecIDs for video " | |
381 "tracks are not supported yet (track %u).\n", t->tnum); | |
382 continue; | |
383 } | |
384 | |
385 if (t->v_width == 0) { | |
386 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] The width for track %u was " | |
387 "not set.\n", t->tnum); | |
388 continue; | |
389 } | |
390 if (t->v_height == 0) { | |
391 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] The height for track %u was " | |
392 "not set.\n", t->tnum); | |
393 continue; | |
394 } | |
395 | |
10081
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
396 if (t->v_dwidth == 0) |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
397 t->v_dwidth = t->v_width; |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
398 if (t->v_dheight == 0) |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
399 t->v_dheight = t->v_height; |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
400 |
10024 | 401 // This track seems to be ok. |
402 t->ok = 1; | |
403 | |
404 break; | |
405 | |
406 case 'a': // audio track | |
407 if (t->codec_id == NULL) | |
408 continue; | |
409 if (!strcmp(t->codec_id, MKV_A_ACM)) { | |
410 if ((t->private_data == NULL) || | |
411 (t->private_size < sizeof(WAVEFORMATEX))) { | |
412 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: CodecID for track " | |
413 "%u is '" MKV_A_ACM "', " | |
414 "but there was no WAVEFORMATEX struct present. " | |
415 "Therefore we don't have a format ID to identify the audio " | |
416 "codec used.\n", t->tnum); | |
417 continue; | |
418 } else { | |
419 t->ms_compat = 1; | |
420 | |
421 wfe = (WAVEFORMATEX *)t->private_data; | |
422 u = get_uint32(&wfe->nSamplesPerSec); | |
423 if (((uint32_t)t->a_sfreq) != u) { | |
424 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: (MS compatibility " | |
425 "mode for track %u) " | |
426 "Matroska says that there are %u samples per second, " | |
427 "but WAVEFORMATEX says that there are %u.\n", t->tnum, | |
428 (uint32_t)t->a_sfreq, u); | |
429 if (t->a_sfreq == 0.0) | |
430 t->a_sfreq = (float)u; | |
431 } | |
432 | |
433 u = get_uint16(&wfe->nChannels); | |
434 if (t->a_channels != u) { | |
435 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: (MS " | |
436 "compatibility mode for track %u) " | |
437 "Matroska says that there are %u channels, but the " | |
438 "WAVEFORMATEX says that there are %u.\n", t->tnum, | |
439 t->a_channels, u); | |
440 if (t->a_channels == 0) | |
441 t->a_channels = u; | |
442 } | |
443 | |
444 u = get_uint16(&wfe->wBitsPerSample); | |
10098 | 445 if (t->a_bps != u) { |
10024 | 446 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: (MS " |
447 "compatibility mode for track %u) " | |
448 "Matroska says that there are %u bits per sample, " | |
449 "but the WAVEFORMATEX says that there are %u.\n", t->tnum, | |
450 t->a_bps, u); | |
451 if (t->a_bps == 0) | |
452 t->a_bps = u; | |
453 } | |
454 | |
455 t->a_formattag = get_uint16(&wfe->wFormatTag); | |
456 } | |
457 } else { | |
458 if (!strcmp(t->codec_id, MKV_A_MP3)) | |
459 t->a_formattag = 0x0055; | |
460 else if (!strcmp(t->codec_id, MKV_A_AC3)) | |
461 t->a_formattag = 0x2000; | |
10105
dc4f1b768062
Support for DTS tracks (only with -hwac3 of course).
mosu
parents:
10099
diff
changeset
|
462 else if (!strcmp(t->codec_id, MKV_A_DTS)) |
dc4f1b768062
Support for DTS tracks (only with -hwac3 of course).
mosu
parents:
10099
diff
changeset
|
463 // uses same format tag as AC3, only supported with -hwac3 |
dc4f1b768062
Support for DTS tracks (only with -hwac3 of course).
mosu
parents:
10099
diff
changeset
|
464 t->a_formattag = 0x2000; |
10024 | 465 else if (!strcmp(t->codec_id, MKV_A_PCM)) |
466 t->a_formattag = 0x0001; | |
10125 | 467 else if (!strcmp(t->codec_id, MKV_A_AAC_2LC) || |
468 !strcmp(t->codec_id, MKV_A_AAC_4LC) || | |
469 !strcmp(t->codec_id, MKV_A_AAC_4SBR)) | |
470 t->a_formattag = mmioFOURCC('M', 'P', '4', 'A'); | |
10024 | 471 else if (!strcmp(t->codec_id, MKV_A_VORBIS)) { |
472 if (t->private_data == NULL) { | |
473 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] WARNING: CodecID for " | |
474 "track %u is '" MKV_A_VORBIS | |
475 "', but there are no header packets present.", t->tnum); | |
476 continue; | |
477 } | |
478 | |
479 c = (unsigned char *)t->private_data; | |
480 if (c[0] != 2) { | |
481 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Vorbis track does not " | |
482 "contain valid headers.\n"); | |
483 continue; | |
484 } | |
485 | |
486 offset = 1; | |
487 for (i = 0; i < 2; i++) { | |
488 length = 0; | |
489 while ((c[offset] == (unsigned char )255) && | |
490 (length < t->private_size)) { | |
491 length += 255; | |
492 offset++; | |
493 } | |
494 if (offset >= (t->private_size - 1)) { | |
495 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Vorbis track does not " | |
496 "contain valid headers.\n"); | |
497 continue; | |
498 } | |
499 length += c[offset]; | |
500 offset++; | |
501 t->header_sizes[i] = length; | |
502 } | |
503 | |
504 t->headers[0] = &c[offset]; | |
505 t->headers[1] = &c[offset + t->header_sizes[0]]; | |
506 t->headers[2] = &c[offset + t->header_sizes[0] + | |
507 t->header_sizes[1]]; | |
10079
7c13793dbfa0
Vorbis header packet lengths were calculated incorrectly.
mosu
parents:
10073
diff
changeset
|
508 t->header_sizes[2] = t->private_size - offset - |
7c13793dbfa0
Vorbis header packet lengths were calculated incorrectly.
mosu
parents:
10073
diff
changeset
|
509 t->header_sizes[0] - t->header_sizes[1]; |
10024 | 510 |
511 t->a_formattag = 0xFFFE; | |
512 } else { | |
513 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Unknown/unsupported audio " | |
514 "codec ID '%s' for track %u.\n", t->codec_id, t->tnum); | |
515 continue; | |
516 } | |
517 } | |
518 | |
519 if (t->a_sfreq == 0.0) { | |
520 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] The sampling frequency was not " | |
521 "set for track %u.\n", t->tnum); | |
522 continue; | |
523 } | |
524 | |
525 if (t->a_channels == 0) { | |
526 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] The number of channels was not " | |
527 "set for track %u.\n", t->tnum); | |
528 continue; | |
529 } | |
530 | |
531 if (t->a_formattag == 0) { | |
532 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] The audio format tag was not " | |
533 "set for track %u.\n", t->tnum); | |
534 continue; | |
535 } | |
536 | |
537 // This track seems to be ok. | |
538 t->ok = 1; | |
539 | |
540 break; | |
541 | |
542 case 's': // Text subtitles do not need any data | |
543 t->ok = 1; // except the CodecID. | |
544 break; | |
545 | |
546 default: // unknown track type!? error in demuxer... | |
547 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Error in demux_mkv.cpp: unknown " | |
548 "demuxer type for track %u: '%c'\n", t->tnum, t->type); | |
549 continue; | |
550 } | |
551 | |
552 if (t->ok) | |
553 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Track %u seems to be ok.\n", t->tnum); | |
554 } | |
555 | |
556 return 1; | |
557 } | |
558 | |
559 static void free_mkv_demuxer(mkv_demuxer_t *d) { | |
560 int i; | |
561 | |
562 if (d == NULL) | |
563 return; | |
564 | |
565 for (i = 0; i < d->num_tracks; i++) | |
566 if (d->tracks[i] != NULL) { | |
567 if (d->tracks[i]->private_data != NULL) | |
568 free(d->tracks[i]->private_data); | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
569 if (d->tracks[i]->language != NULL) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
570 free(d->tracks[i]->language); |
10024 | 571 free(d->tracks[i]); |
572 } | |
573 | |
574 for (i = 0; i < d->num_indexes; i++) | |
575 free(d->index[i].entries); | |
576 free(d->index); | |
577 | |
578 if (d->es != NULL) | |
579 delete d->es; | |
580 if (d->saved_l1 != NULL) | |
581 delete d->saved_l1; | |
582 if (d->in != NULL) | |
583 delete d->in; | |
584 if (d->segment != NULL) | |
585 delete d->segment; | |
586 | |
587 free(d); | |
588 } | |
589 | |
590 static void add_index_entry(mkv_demuxer_t *d, uint32_t tnum, uint64_t filepos, | |
591 uint64_t timecode, int is_key) { | |
592 int i, found; | |
593 mkv_index_entry_t *entry; | |
594 | |
595 for (i = 0, found = 0; i < d->num_indexes; i++) | |
596 if (d->index[i].tnum == tnum) { | |
597 found = 1; | |
598 break; | |
599 } | |
600 | |
601 if (!found) { | |
602 d->index = (mkv_track_index_t *)realloc(d->index, (d->num_indexes + 1) * | |
603 sizeof(mkv_track_index_t)); | |
604 if (d->index == NULL) | |
605 return; | |
606 i = d->num_indexes; | |
607 memset(&d->index[i], 0, sizeof(mkv_track_index_t)); | |
608 d->index[i].tnum = tnum; | |
609 d->num_indexes++; | |
610 } | |
611 | |
612 d->index[i].entries = | |
613 (mkv_index_entry_t *)realloc(d->index[i].entries, | |
614 (d->index[i].num_entries + 1) * | |
615 sizeof(mkv_index_entry_t)); | |
616 if (d->index[i].entries == NULL) | |
617 return; | |
618 entry = &d->index[i].entries[d->index[i].num_entries]; | |
619 entry->filepos = filepos; | |
620 entry->timecode = timecode; | |
621 entry->is_key = is_key; | |
622 d->index[i].num_entries++; | |
623 } | |
624 | |
625 static void add_cluster_position(mkv_demuxer_t *mkv_d, int64_t position) { | |
626 mkv_d->cluster_positions = (int64_t *)realloc(mkv_d->cluster_positions, | |
627 (mkv_d->num_cluster_pos + 1) * | |
628 sizeof(int64_t)); | |
629 if (mkv_d->cluster_positions != NULL) { | |
630 mkv_d->cluster_positions[mkv_d->num_cluster_pos] = position; | |
631 mkv_d->num_cluster_pos++; | |
632 } else | |
633 mkv_d->num_cluster_pos = 0; | |
634 } | |
635 | |
636 static int parse_cues(mkv_demuxer_t *mkv_d) { | |
637 EbmlElement *l1 = NULL, *l2 = NULL, *l3 = NULL, *l4 = NULL, *l5 = NULL; | |
638 EbmlStream *es; | |
639 int upper_lvl_el, elements_found, i, k; | |
640 uint64_t tc_scale, filepos = 0, timecode = 0; | |
641 uint32_t tnum = 0; | |
642 mkv_index_entry_t *entry; | |
643 | |
644 es = mkv_d->es; | |
645 tc_scale = mkv_d->tc_scale; | |
646 upper_lvl_el = 0; | |
647 | |
648 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing cues ] -----------\n"); | |
649 | |
650 l1 = es->FindNextElement(mkv_d->segment->Generic().Context, upper_lvl_el, | |
651 0xFFFFFFFFL, true, 1); | |
652 if (l1 == NULL) | |
653 return 0; | |
654 | |
655 if (!(EbmlId(*l1) == KaxCues::ClassInfos.GlobalId)) { | |
656 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] No KaxCues element found but but %s.\n" | |
657 "[mkv] \\---- [ parsing cues ] -----------\n", typeid(*l1).name()); | |
658 | |
659 return 0; | |
660 } | |
661 | |
662 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ found cues\n"); | |
663 | |
664 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, 0xFFFFFFFFL, | |
665 true, 1); | |
666 while (l2 != NULL) { | |
667 if (upper_lvl_el != 0) | |
668 break; | |
669 | |
670 if (EbmlId(*l2) == KaxCuePoint::ClassInfos.GlobalId) { | |
671 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue point\n"); | |
672 | |
673 elements_found = 0; | |
674 | |
675 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
676 0xFFFFFFFFL, true, 1); | |
677 while (l3 != NULL) { | |
678 if (upper_lvl_el != 0) | |
679 break; | |
680 | |
681 if (EbmlId(*l3) == KaxCueTime::ClassInfos.GlobalId) { | |
682 KaxCueTime &cue_time = *static_cast<KaxCueTime *>(l3); | |
683 cue_time.ReadData(es->I_O()); | |
684 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue time: %.3fs\n", | |
685 ((float)uint64(cue_time)) * tc_scale / 1000000000.0); | |
686 | |
687 timecode = uint64(cue_time) * tc_scale / 1000000; | |
688 elements_found |= 1; | |
689 | |
690 } else if (EbmlId(*l3) == | |
691 KaxCueTrackPositions::ClassInfos.GlobalId) { | |
692 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue track " | |
693 "positions\n"); | |
694 | |
695 l4 = es->FindNextElement(l3->Generic().Context, upper_lvl_el, | |
696 0xFFFFFFFFL, true, 1); | |
697 while (l4 != NULL) { | |
698 if (upper_lvl_el != 0) | |
699 break; | |
700 | |
701 if (EbmlId(*l4) == KaxCueTrack::ClassInfos.GlobalId) { | |
702 KaxCueTrack &cue_track = *static_cast<KaxCueTrack *>(l4); | |
703 cue_track.ReadData(es->I_O()); | |
704 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue track: " | |
705 "%u\n", uint32(cue_track)); | |
706 | |
707 tnum = uint32(cue_track); | |
708 elements_found |= 2; | |
709 | |
710 } else if (EbmlId(*l4) == | |
711 KaxCueClusterPosition::ClassInfos.GlobalId) { | |
712 KaxCueClusterPosition &cue_cp = | |
713 *static_cast<KaxCueClusterPosition *>(l4); | |
714 cue_cp.ReadData(es->I_O()); | |
715 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue cluster " | |
716 "position: %llu\n", uint64(cue_cp)); | |
717 | |
718 filepos = mkv_d->segment->GetGlobalPosition(uint64_t(cue_cp)); | |
719 elements_found |= 4; | |
720 | |
721 } else if (EbmlId(*l4) == | |
722 KaxCueBlockNumber::ClassInfos.GlobalId) { | |
723 KaxCueBlockNumber &cue_bn = | |
724 *static_cast<KaxCueBlockNumber *>(l4); | |
725 cue_bn.ReadData(es->I_O()); | |
726 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue block " | |
727 "number: %llu\n", uint64(cue_bn)); | |
728 | |
729 } else if (EbmlId(*l4) == | |
730 KaxCueCodecState::ClassInfos.GlobalId) { | |
731 KaxCueCodecState &cue_cs = | |
732 *static_cast<KaxCueCodecState *>(l4); | |
733 cue_cs.ReadData(es->I_O()); | |
734 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue codec " | |
735 "state: %llu\n", uint64(cue_cs)); | |
736 | |
737 } else if (EbmlId(*l4) == | |
738 KaxCueReference::ClassInfos.GlobalId) { | |
739 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue " | |
740 "reference\n"); | |
741 | |
742 elements_found |= 8; | |
743 | |
744 l5 = es->FindNextElement(l4->Generic().Context, upper_lvl_el, | |
745 0xFFFFFFFFL, true, 1); | |
746 while (l5 != NULL) { | |
747 if (upper_lvl_el != 0) | |
748 break; | |
749 | |
750 if (EbmlId(*l5) == KaxCueRefTime::ClassInfos.GlobalId) { | |
751 KaxCueRefTime &cue_rt = | |
752 *static_cast<KaxCueRefTime *>(l5); | |
753 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue ref " | |
754 "time: %.3fs\n", ((float)uint64(cue_rt)) * tc_scale / | |
755 1000000000.0); | |
756 | |
757 } else if (EbmlId(*l5) == | |
758 KaxCueRefCluster::ClassInfos.GlobalId) { | |
759 KaxCueRefCluster &cue_rc = | |
760 *static_cast<KaxCueRefCluster *>(l5); | |
761 cue_rc.ReadData(es->I_O()); | |
762 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue ref " | |
763 "cluster: %llu\n", uint64(cue_rc)); | |
764 | |
765 } else if (EbmlId(*l5) == | |
766 KaxCueRefNumber::ClassInfos.GlobalId) { | |
767 KaxCueRefNumber &cue_rn = | |
768 *static_cast<KaxCueRefNumber *>(l5); | |
769 cue_rn.ReadData(es->I_O()); | |
770 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue ref " | |
771 "number: %llu\n", uint64(cue_rn)); | |
772 | |
773 } else if (EbmlId(*l5) == | |
774 KaxCueRefCodecState::ClassInfos.GlobalId) { | |
775 KaxCueRefCodecState &cue_rcs = | |
776 *static_cast<KaxCueRefCodecState *>(l5); | |
777 cue_rcs.ReadData(es->I_O()); | |
778 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + found cue ref " | |
779 "codec state: %llu\n", uint64(cue_rcs)); | |
780 | |
781 } else { | |
782 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + unknown " | |
783 "element, level 5: %s\n", typeid(*l5).name()); | |
784 } | |
785 | |
786 l5->SkipData(static_cast<EbmlStream &>(*es), | |
787 l5->Generic().Context); | |
788 delete l5; | |
789 l5 = es->FindNextElement(l4->Generic().Context, | |
790 upper_lvl_el, 0xFFFFFFFFL, true, 1); | |
791 } // while (l5 != NULL) | |
792 | |
793 } else | |
794 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + unknown element, " | |
795 "level 4: %s\n", typeid(*l4).name()); | |
796 | |
797 if (upper_lvl_el > 0) { // we're coming from l5 | |
798 upper_lvl_el--; | |
799 delete l4; | |
800 l4 = l5; | |
801 if (upper_lvl_el > 0) | |
802 break; | |
803 | |
804 } else { | |
805 l4->SkipData(static_cast<EbmlStream &>(*es), | |
806 l4->Generic().Context); | |
807 delete l4; | |
808 l4 = es->FindNextElement(l3->Generic().Context, upper_lvl_el, | |
809 0xFFFFFFFFL, true, 1); | |
810 } | |
811 } // while (l4 != NULL) | |
812 | |
813 } else | |
814 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + unknown element, level 3: " | |
815 "%s\n", typeid(*l3).name()); | |
816 | |
817 if (upper_lvl_el > 0) { // we're coming from l4 | |
818 upper_lvl_el--; | |
819 delete l3; | |
820 l3 = l4; | |
821 if (upper_lvl_el > 0) | |
822 break; | |
823 | |
824 } else { | |
825 l3->SkipData(static_cast<EbmlStream &>(*es), | |
826 l3->Generic().Context); | |
827 delete l3; | |
828 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
829 0xFFFFFFFFL, true, 1); | |
830 } | |
831 } // while (l3 != NULL) | |
832 | |
833 // Three elements must have been found in order for this to be a | |
834 // correct entry: | |
835 // 1: cue time (timecode) | |
836 // 2: cue track (tnum) | |
837 // 4: cue cluster position (filepos) | |
838 // If 8 is also set, then there was a reference element, and the | |
839 // current block is not an I frame. If 8 is not set then this is | |
840 // an I frame. | |
841 if ((elements_found & 7) == 7) | |
842 add_index_entry(mkv_d, tnum, filepos, timecode, | |
843 (elements_found & 8) ? 0 : 1); | |
844 | |
845 } else | |
846 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] | + unknown element, level 2: " | |
847 "%s\n", typeid(*l2).name()); | |
848 | |
849 if (upper_lvl_el > 0) { // we're coming from l3 | |
850 upper_lvl_el--; | |
851 delete l2; | |
852 l2 = l3; | |
853 if (upper_lvl_el > 0) | |
854 break; | |
855 | |
856 } else { | |
857 l2->SkipData(static_cast<EbmlStream &>(*es), | |
858 l2->Generic().Context); | |
859 delete l2; | |
860 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
861 0xFFFFFFFFL, true, 1); | |
862 } | |
863 } // while (l2 != NULL) | |
864 | |
865 // Debug: dump the index | |
866 for (i = 0; i < mkv_d->num_indexes; i++) { | |
867 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] Index for track %u contains %u " | |
868 "entries.\n", mkv_d->index[i].tnum, mkv_d->index[i].num_entries); | |
869 for (k = 0; k < mkv_d->index[i].num_entries; k++) { | |
870 entry = &mkv_d->index[i].entries[k]; | |
871 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] %d: timecode %llu, filepos %llu, " | |
872 "is key: %s\n", k, entry->timecode, entry->filepos, | |
873 entry->is_key ? "yes" : "no"); | |
874 } | |
875 } | |
876 | |
877 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing cues ] -----------\n"); | |
878 | |
879 return 1; | |
880 } | |
881 | |
882 extern "C" int demux_mkv_open(demuxer_t *demuxer) { | |
883 unsigned char signature[4]; | |
884 stream_t *s; | |
885 demux_packet_t *dp; | |
886 mkv_demuxer_t *mkv_d; | |
887 int upper_lvl_el, exit_loop, i; | |
888 // Elements for different levels | |
889 EbmlElement *l0 = NULL, *l1 = NULL, *l2 = NULL, *l3 = NULL, *l4 = NULL; | |
890 EbmlStream *es; | |
891 mkv_track_t *track; | |
892 sh_audio_t *sh_a; | |
893 sh_video_t *sh_v; | |
894 uint64_t seek_pos, current_pos; | |
895 int seek_element_is_cue; | |
896 | |
897 #ifdef USE_ICONV | |
898 subcp_open(); | |
899 #endif | |
900 | |
901 s = demuxer->stream; | |
902 stream_seek(s, s->start_pos); | |
903 memset(signature, 0, 4); | |
904 stream_read(s, (char *)signature, 4); | |
905 if ((signature[0] != 0x1A) || (signature[1] != 0x45) || | |
906 (signature[2] != 0xDF) || (signature[3] != 0xA3)) | |
907 return 0; | |
908 stream_seek(s, s->start_pos); | |
909 | |
910 try { | |
911 // structure for storing the demuxer's private data | |
912 mkv_d = (mkv_demuxer_t *)malloc(sizeof(mkv_demuxer_t)); | |
913 if (mkv_d == NULL) | |
914 return 0; | |
915 memset(mkv_d, 0, sizeof(mkv_demuxer_t)); | |
916 mkv_d->duration = -1.0; | |
917 | |
918 // Create the interface between MPlayer's IO system and | |
919 // libmatroska's IO system. | |
920 mkv_d->in = new mpstream_io_callback(demuxer->stream); | |
921 if (mkv_d->in == NULL) { | |
922 free_mkv_demuxer(mkv_d); | |
923 return 0; | |
924 } | |
925 mpstream_io_callback &io = *static_cast<mpstream_io_callback *>(mkv_d->in); | |
926 mkv_d->es = new EbmlStream(io); | |
927 if (mkv_d->es == NULL) { | |
928 free_mkv_demuxer(mkv_d); | |
929 return 0; | |
930 } | |
931 es = mkv_d->es; | |
932 | |
933 // Find the EbmlHead element. Must be the first one. | |
934 l0 = es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL); | |
935 if (l0 == NULL) { | |
936 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] no head found\n"); | |
937 free_mkv_demuxer(mkv_d); | |
938 return 0; | |
939 } | |
940 // Don't verify its data for now. | |
941 l0->SkipData(static_cast<EbmlStream &>(*es), l0->Generic().Context); | |
942 delete l0; | |
943 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n"); | |
944 | |
945 // Next element must be a segment | |
946 l0 = es->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL); | |
947 if (l0 == NULL) { | |
948 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n"); | |
949 free_mkv_demuxer(mkv_d); | |
950 return 0; | |
951 } | |
952 if (!(EbmlId(*l0) == KaxSegment::ClassInfos.GlobalId)) { | |
953 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n"); | |
954 free_mkv_demuxer(mkv_d); | |
955 return 0; | |
956 } | |
957 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n"); | |
958 | |
959 mkv_d->segment = (KaxSegment *)l0; | |
960 | |
961 upper_lvl_el = 0; | |
962 exit_loop = 0; | |
963 // We've got our segment, so let's find the tracks | |
964 l1 = es->FindNextElement(l0->Generic().Context, upper_lvl_el, 0xFFFFFFFFL, | |
965 true, 1); | |
966 while (l1 != NULL) { | |
967 if ((upper_lvl_el != 0) || exit_loop) | |
968 break; | |
969 | |
970 if (EbmlId(*l1) == KaxInfo::ClassInfos.GlobalId) { | |
971 // General info about this Matroska file | |
972 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ segment information...\n"); | |
973 | |
10084
04dacb55d55d
timecode scale value must be initialized before the duration is found. API change of libmatroska 0.4.2. Will not work with 0.4.1!
mosu
parents:
10081
diff
changeset
|
974 mkv_d->tc_scale = MKVD_TIMECODESCALE; |
04dacb55d55d
timecode scale value must be initialized before the duration is found. API change of libmatroska 0.4.2. Will not work with 0.4.1!
mosu
parents:
10081
diff
changeset
|
975 |
10024 | 976 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, |
977 0xFFFFFFFFL, true, 1); | |
978 while (l2 != NULL) { | |
979 if ((upper_lvl_el != 0) || exit_loop) | |
980 break; | |
981 | |
982 if (EbmlId(*l2) == KaxTimecodeScale::ClassInfos.GlobalId) { | |
983 KaxTimecodeScale &tc_scale = *static_cast<KaxTimecodeScale *>(l2); | |
984 tc_scale.ReadData(es->I_O()); | |
985 mkv_d->tc_scale = uint64(tc_scale); | |
986 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + timecode scale: %llu\n", | |
987 mkv_d->tc_scale); | |
988 | |
989 } else if (EbmlId(*l2) == KaxDuration::ClassInfos.GlobalId) { | |
990 KaxDuration &duration = *static_cast<KaxDuration *>(l2); | |
991 duration.ReadData(es->I_O()); | |
992 mkv_d->duration = float(duration) * mkv_d->tc_scale / 1000000000.0; | |
993 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + duration: %.3fs\n", | |
994 mkv_d->duration); | |
995 | |
996 } else | |
997 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown element@2: %s\n", | |
998 typeid(*l2).name()); | |
999 | |
1000 l2->SkipData(static_cast<EbmlStream &>(*es), | |
1001 l2->Generic().Context); | |
1002 delete l2; | |
1003 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1004 0xFFFFFFFFL, true, 1); | |
1005 } | |
1006 | |
1007 } else if (EbmlId(*l1) == KaxTracks::ClassInfos.GlobalId) { | |
1008 // Yep, we've found our KaxTracks element. Now find all tracks | |
1009 // contained in this segment. | |
1010 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ segment tracks...\n"); | |
1011 | |
1012 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1013 0xFFFFFFFFL, true, 1); | |
1014 while (l2 != NULL) { | |
1015 if ((upper_lvl_el != 0) || exit_loop) | |
1016 break; | |
1017 | |
1018 if (EbmlId(*l2) == KaxTrackEntry::ClassInfos.GlobalId) { | |
1019 // We actually found a track entry :) We're happy now. | |
1020 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + a track...\n"); | |
1021 | |
1022 track = new_mkv_track(mkv_d); | |
1023 if (track == NULL) | |
1024 return 0; | |
1025 | |
1026 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
1027 0xFFFFFFFFL, true, 1); | |
1028 while (l3 != NULL) { | |
1029 if (upper_lvl_el != 0) | |
1030 break; | |
1031 | |
1032 // Now evaluate the data belonging to this track | |
1033 if (EbmlId(*l3) == KaxTrackNumber::ClassInfos.GlobalId) { | |
1034 KaxTrackNumber &tnum = *static_cast<KaxTrackNumber *>(l3); | |
1035 tnum.ReadData(es->I_O()); | |
1036 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track number: %u\n", | |
1037 uint32(tnum)); | |
1038 track->tnum = uint32(tnum); | |
1039 if (find_track_by_num(mkv_d, track->tnum, track) != NULL) | |
1040 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] | + WARNING: There's " | |
1041 "more than one track with the number %u.\n", | |
1042 track->tnum); | |
1043 | |
1044 } else if (EbmlId(*l3) == KaxTrackUID::ClassInfos.GlobalId) { | |
1045 KaxTrackUID &tuid = *static_cast<KaxTrackUID *>(l3); | |
1046 tuid.ReadData(es->I_O()); | |
1047 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track UID: %u\n", | |
1048 uint32(tuid)); | |
1049 | |
1050 } else if (EbmlId(*l3) == KaxTrackType::ClassInfos.GlobalId) { | |
1051 KaxTrackType &ttype = *static_cast<KaxTrackType *>(l3); | |
1052 ttype.ReadData(es->I_O()); | |
1053 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track type: "); | |
1054 | |
1055 switch (uint8(ttype)) { | |
1056 case track_audio: | |
1057 mp_msg(MSGT_DEMUX, MSGL_V, "Audio\n"); | |
1058 track->type = 'a'; | |
1059 break; | |
1060 case track_video: | |
1061 mp_msg(MSGT_DEMUX, MSGL_V, "Video\n"); | |
1062 track->type = 'v'; | |
1063 break; | |
1064 case track_subtitle: | |
1065 mp_msg(MSGT_DEMUX, MSGL_V, "Subtitle\n"); | |
1066 track->type = 's'; | |
1067 break; | |
1068 default: | |
1069 mp_msg(MSGT_DEMUX, MSGL_V, "unknown\n"); | |
1070 track->type = '?'; | |
1071 break; | |
1072 } | |
1073 | |
1074 } else if (EbmlId(*l3) == KaxTrackAudio::ClassInfos.GlobalId) { | |
1075 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Audio track\n"); | |
1076 l4 = es->FindNextElement(l3->Generic().Context, upper_lvl_el, | |
1077 0xFFFFFFFFL, true, 1); | |
1078 while (l4 != NULL) { | |
1079 if (upper_lvl_el != 0) | |
1080 break; | |
1081 | |
1082 if (EbmlId(*l4) == | |
1083 KaxAudioSamplingFreq::ClassInfos.GlobalId) { | |
1084 KaxAudioSamplingFreq &freq = | |
1085 *static_cast<KaxAudioSamplingFreq*>(l4); | |
1086 freq.ReadData(es->I_O()); | |
1087 track->a_sfreq = float(freq); | |
1088 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Sampling " | |
1089 "frequency: %f\n", track->a_sfreq); | |
1090 | |
1091 } else if (EbmlId(*l4) == | |
1092 KaxAudioChannels::ClassInfos.GlobalId) { | |
1093 KaxAudioChannels &channels = | |
1094 *static_cast<KaxAudioChannels*>(l4); | |
1095 channels.ReadData(es->I_O()); | |
1096 track->a_channels = uint8(channels); | |
1097 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Channels: %u\n", | |
1098 track->a_channels); | |
1099 | |
1100 } else if (EbmlId(*l4) == | |
1101 KaxAudioBitDepth::ClassInfos.GlobalId) { | |
1102 KaxAudioBitDepth &bps = | |
1103 *static_cast<KaxAudioBitDepth*>(l4); | |
1104 bps.ReadData(es->I_O()); | |
1105 track->a_bps = uint8(bps); | |
1106 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Bit depth: %u\n", | |
1107 track->a_bps); | |
1108 | |
1109 } else | |
1110 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown " | |
1111 "element@4: %s\n", typeid(*l4).name()); | |
1112 | |
1113 l4->SkipData(static_cast<EbmlStream &>(*es), | |
1114 l4->Generic().Context); | |
1115 delete l4; | |
1116 l4 = es->FindNextElement(l3->Generic().Context, upper_lvl_el, | |
1117 0xFFFFFFFFL, true, 1); | |
1118 } // while (l4 != NULL) | |
1119 | |
1120 } else if (EbmlId(*l3) == KaxTrackVideo::ClassInfos.GlobalId) { | |
1121 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Video track\n"); | |
1122 l4 = es->FindNextElement(l3->Generic().Context, upper_lvl_el, | |
1123 0xFFFFFFFFL, true, 1); | |
1124 while (l4 != NULL) { | |
1125 if (upper_lvl_el != 0) | |
1126 break; | |
1127 | |
1128 if (EbmlId(*l4) == KaxVideoPixelWidth::ClassInfos.GlobalId) { | |
1129 KaxVideoPixelWidth &width = | |
1130 *static_cast<KaxVideoPixelWidth *>(l4); | |
1131 width.ReadData(es->I_O()); | |
1132 track->v_width = uint16(width); | |
1133 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel width: %u\n", | |
1134 track->v_width); | |
1135 | |
1136 } else if (EbmlId(*l4) == | |
1137 KaxVideoPixelHeight::ClassInfos.GlobalId) { | |
1138 KaxVideoPixelHeight &height = | |
1139 *static_cast<KaxVideoPixelHeight *>(l4); | |
1140 height.ReadData(es->I_O()); | |
1141 track->v_height = uint16(height); | |
1142 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel height: " | |
1143 "%u\n", track->v_height); | |
1144 | |
1145 } else if (EbmlId(*l4) == | |
10081
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1146 KaxVideoDisplayWidth::ClassInfos.GlobalId) { |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1147 KaxVideoDisplayWidth &width = |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1148 *static_cast<KaxVideoDisplayWidth *>(l4); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1149 width.ReadData(es->I_O()); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1150 track->v_dwidth = uint16(width); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1151 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Display width: " |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1152 "%u\n", track->v_dwidth); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1153 |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1154 } else if (EbmlId(*l4) == |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1155 KaxVideoDisplayHeight::ClassInfos.GlobalId) { |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1156 KaxVideoDisplayHeight &height = |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1157 *static_cast<KaxVideoDisplayHeight *>(l4); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1158 height.ReadData(es->I_O()); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1159 track->v_dheight = uint16(height); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1160 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Display height: " |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1161 "%u\n", track->v_dheight); |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1162 |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1163 } else if (EbmlId(*l4) == |
10024 | 1164 KaxVideoFrameRate::ClassInfos.GlobalId) { |
1165 KaxVideoFrameRate &framerate = | |
1166 *static_cast<KaxVideoFrameRate *>(l4); | |
1167 framerate.ReadData(es->I_O()); | |
1168 track->v_frate = float(framerate); | |
1169 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Frame rate: %f\n", | |
1170 float(framerate)); | |
1171 | |
1172 } else | |
1173 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown " | |
1174 "element@4: %s\n", typeid(*l4).name()); | |
1175 | |
1176 l4->SkipData(static_cast<EbmlStream &>(*es), | |
1177 l4->Generic().Context); | |
1178 delete l4; | |
1179 l4 = es->FindNextElement(l3->Generic().Context, upper_lvl_el, | |
1180 0xFFFFFFFFL, true, 1); | |
1181 } // while (l4 != NULL) | |
1182 | |
1183 } else if (EbmlId(*l3) == KaxCodecID::ClassInfos.GlobalId) { | |
1184 KaxCodecID &codec_id = *static_cast<KaxCodecID*>(l3); | |
1185 codec_id.ReadData(es->I_O()); | |
1186 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Codec ID: %s\n", | |
1187 &binary(codec_id)); | |
1188 track->codec_id = strdup((char *)&binary(codec_id)); | |
1189 | |
1190 } else if (EbmlId(*l3) == KaxCodecPrivate::ClassInfos.GlobalId) { | |
1191 KaxCodecPrivate &c_priv = *static_cast<KaxCodecPrivate*>(l3); | |
1192 c_priv.ReadData(es->I_O()); | |
1193 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + CodecPrivate, length " | |
1194 "%llu\n", c_priv.GetSize()); | |
1195 track->private_size = c_priv.GetSize(); | |
1196 if (track->private_size > 0) { | |
1197 track->private_data = malloc(track->private_size); | |
1198 if (track->private_data == NULL) | |
1199 return 0; | |
1200 memcpy(track->private_data, c_priv.GetBuffer(), | |
1201 track->private_size); | |
1202 } | |
1203 | |
1204 } else if (EbmlId(*l3) == | |
1205 KaxTrackFlagDefault::ClassInfos.GlobalId) { | |
1206 KaxTrackFlagDefault &f_default = | |
1207 *static_cast<KaxTrackFlagDefault *>(l3); | |
1208 f_default.ReadData(es->I_O()); | |
1209 track->default_track = uint32(f_default); | |
1210 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Default flag: %u\n", | |
1211 track->default_track); | |
1212 | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1213 } else if (EbmlId(*l3) == |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1214 KaxTrackLanguage::ClassInfos.GlobalId) { |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1215 KaxTrackLanguage &language = |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1216 *static_cast<KaxTrackLanguage *>(l3); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1217 language.ReadData(es->I_O()); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1218 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Language: %s\n", |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1219 string(language).c_str()); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1220 if (track->language != NULL) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1221 free(track->language); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1222 track->language = strdup(string(language).c_str()); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1223 |
10029 | 1224 } else if ((!(EbmlId(*l3) == |
1225 KaxTrackFlagLacing::ClassInfos.GlobalId)) && | |
1226 (!(EbmlId(*l3) == | |
1227 KaxTrackMinCache::ClassInfos.GlobalId)) && | |
1228 (!(EbmlId(*l3) == | |
1229 KaxTrackMaxCache::ClassInfos.GlobalId))) | |
10024 | 1230 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown element@3: " |
1231 "%s\n", typeid(*l3).name()); | |
1232 | |
1233 if (upper_lvl_el > 0) { // we're coming from l4 | |
1234 upper_lvl_el--; | |
1235 delete l3; | |
1236 l3 = l4; | |
1237 if (upper_lvl_el > 0) | |
1238 break; | |
1239 } else { | |
1240 l3->SkipData(static_cast<EbmlStream &>(*es), | |
1241 l3->Generic().Context); | |
1242 delete l3; | |
1243 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
1244 0xFFFFFFFFL, true, 1); | |
1245 } | |
1246 } // while (l3 != NULL) | |
1247 | |
1248 } else | |
1249 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown element@2: %s\n", | |
1250 typeid(*l2).name()); | |
1251 if (upper_lvl_el > 0) { // we're coming from l3 | |
1252 upper_lvl_el--; | |
1253 delete l2; | |
1254 l2 = l3; | |
1255 if (upper_lvl_el > 0) | |
1256 break; | |
1257 } else { | |
1258 l2->SkipData(static_cast<EbmlStream &>(*es), | |
1259 l2->Generic().Context); | |
1260 delete l2; | |
1261 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1262 0xFFFFFFFFL, true, 1); | |
1263 } | |
1264 } // while (l2 != NULL) | |
1265 | |
1266 } else if (EbmlId(*l1) == KaxSeekHead::ClassInfos.GlobalId) { | |
1267 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ found seek head\n"); | |
1268 | |
1269 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1270 0xFFFFFFFFL, true, 1); | |
1271 while (l2 != NULL) { | |
1272 if (upper_lvl_el != 0) | |
1273 break; | |
1274 | |
1275 if (EbmlId(*l2) == KaxSeek::ClassInfos.GlobalId) { | |
1276 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + seek entry\n"); | |
1277 | |
1278 seek_pos = 0; | |
1279 seek_element_is_cue = 0; | |
1280 | |
1281 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
1282 0xFFFFFFFFL, true, 1); | |
1283 while (l3 != NULL) { | |
1284 if (upper_lvl_el != 0) | |
1285 break; | |
1286 | |
1287 if (EbmlId(*l3) == KaxSeekID::ClassInfos.GlobalId) { | |
1288 binary *b; | |
1289 int s; | |
1290 KaxSeekID &seek_id = static_cast<KaxSeekID &>(*l3); | |
1291 seek_id.ReadData(es->I_O()); | |
1292 b = seek_id.GetBuffer(); | |
1293 s = seek_id.GetSize(); | |
1294 EbmlId id(b, s); | |
1295 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + seek ID: "); | |
1296 for (i = 0; i < s; i++) | |
1297 mp_msg(MSGT_DEMUX, MSGL_V, "0x%02x ", | |
1298 ((unsigned char *)b)[i]); | |
1299 mp_msg(MSGT_DEMUX, MSGL_V, "(%s)\n", | |
1300 (id == KaxInfo::ClassInfos.GlobalId) ? | |
1301 "KaxInfo" : | |
1302 (id == KaxCluster::ClassInfos.GlobalId) ? | |
1303 "KaxCluster" : | |
1304 (id == KaxTracks::ClassInfos.GlobalId) ? | |
1305 "KaxTracks" : | |
1306 (id == KaxCues::ClassInfos.GlobalId) ? | |
1307 "KaxCues" : | |
1308 (id == KaxAttachements::ClassInfos.GlobalId) ? | |
1309 "KaxAttachements" : | |
1310 (id == KaxChapters::ClassInfos.GlobalId) ? | |
1311 "KaxChapters" : | |
1312 "unknown"); | |
1313 | |
1314 if (id == KaxCues::ClassInfos.GlobalId) | |
1315 seek_element_is_cue = 1; | |
1316 | |
1317 } else if (EbmlId(*l3) == KaxSeekPosition::ClassInfos.GlobalId) { | |
1318 KaxSeekPosition &kax_seek_pos = | |
1319 static_cast<KaxSeekPosition &>(*l3); | |
1320 kax_seek_pos.ReadData(es->I_O()); | |
1321 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + seek position: %llu\n", | |
1322 uint64(kax_seek_pos)); | |
1323 | |
1324 seek_pos = uint64(kax_seek_pos); | |
1325 | |
1326 } else | |
1327 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown element, " | |
1328 "level 3: %s\n", typeid(*l3).name()); | |
1329 | |
1330 l3->SkipData(static_cast<EbmlStream &>(*es), | |
1331 l3->Generic().Context); | |
1332 delete l3; | |
1333 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
1334 0xFFFFFFFFL, true, 1); | |
1335 } // while (l3 != NULL) | |
1336 | |
1337 if (!mkv_d->cues_found && (seek_pos > 0) && | |
1338 seek_element_is_cue && (s->end_pos != 0)) { | |
1339 current_pos = io.getFilePointer(); | |
1340 io.setFilePointer(mkv_d->segment->GetGlobalPosition(seek_pos)); | |
1341 mkv_d->cues_found = parse_cues(mkv_d); | |
1342 if (s->eof) | |
1343 stream_reset(s); | |
1344 io.setFilePointer(current_pos); | |
1345 } | |
1346 | |
1347 } else | |
1348 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + unknown element, level 2: " | |
1349 "%s\n", typeid(*l2).name()); | |
1350 | |
1351 if (upper_lvl_el > 0) { // we're coming from l3 | |
1352 upper_lvl_el--; | |
1353 delete l2; | |
1354 l2 = l3; | |
1355 if (upper_lvl_el > 0) | |
1356 break; | |
1357 | |
1358 } else { | |
1359 l2->SkipData(static_cast<EbmlStream &>(*es), | |
1360 l2->Generic().Context); | |
1361 delete l2; | |
1362 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1363 0xFFFFFFFFL, true, 1); | |
1364 } | |
1365 } // while (l2 != NULL) | |
1366 | |
1367 } else if ((EbmlId(*l1) == KaxCues::ClassInfos.GlobalId) && | |
1368 !mkv_d->cues_found) { | |
1369 // If the cues are up front then by all means read them now! | |
1370 current_pos = io.getFilePointer(); | |
1371 io.setFilePointer(l1->GetElementPosition()); | |
1372 mkv_d->cues_found = parse_cues(mkv_d); | |
1373 stream_reset(s); | |
1374 io.setFilePointer(current_pos); | |
1375 | |
1376 } else if (EbmlId(*l1) == KaxCluster::ClassInfos.GlobalId) { | |
1377 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ found cluster, headers are " | |
1378 "parsed completely :)\n"); | |
1379 add_cluster_position(mkv_d, l1->GetElementPosition()); | |
1380 mkv_d->saved_l1 = l1; | |
1381 exit_loop = 1; | |
1382 | |
1383 } else | |
1384 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ unknown element@1: %s\n", | |
1385 typeid(*l1).name()); | |
1386 | |
1387 if (exit_loop) // we've found the first cluster, so get out | |
1388 break; | |
1389 | |
1390 if (upper_lvl_el > 0) { // we're coming from l2 | |
1391 upper_lvl_el--; | |
1392 delete l1; | |
1393 l1 = l2; | |
1394 if (upper_lvl_el > 0) | |
1395 break; | |
1396 } else { | |
1397 l1->SkipData(static_cast<EbmlStream &>(*es), l1->Generic().Context); | |
1398 delete l1; | |
1399 l1 = es->FindNextElement(l0->Generic().Context, upper_lvl_el, | |
1400 0xFFFFFFFFL, true, 1); | |
1401 } | |
1402 } // while (l1 != NULL) | |
1403 | |
1404 if (!exit_loop) { | |
1405 free_mkv_demuxer(mkv_d); | |
1406 return 0; | |
1407 } | |
1408 | |
1409 } catch (exception &ex) { | |
1410 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] caught exception\n"); | |
1411 return 0; | |
1412 } | |
1413 | |
1414 if (!check_track_information(mkv_d)) { | |
1415 free_mkv_demuxer(mkv_d); | |
1416 return 0; | |
1417 } | |
1418 | |
1419 track = NULL; | |
1420 if (demuxer->video->id == -1) { // Automatically select a video track. | |
1421 // Search for a video track that has the 'default' flag set. | |
1422 for (i = 0; i < mkv_d->num_tracks; i++) | |
1423 if ((mkv_d->tracks[i]->type == 'v') && mkv_d->tracks[i]->ok && | |
1424 mkv_d->tracks[i]->default_track) { | |
1425 track = mkv_d->tracks[i]; | |
1426 break; | |
1427 } | |
1428 | |
1429 if (track == NULL) | |
1430 // No track has the 'default' flag set - let's take the first video | |
1431 // track. | |
1432 for (i = 0; i < mkv_d->num_tracks; i++) | |
1433 if ((mkv_d->tracks[i]->type == 'v') && mkv_d->tracks[i]->ok) { | |
1434 track = mkv_d->tracks[i]; | |
1435 break; | |
1436 } | |
1437 } else if (demuxer->video->id != -2) // -2 = no video at all | |
1438 track = find_track_by_num(mkv_d, demuxer->video->id, NULL); | |
1439 | |
1440 if (track) { | |
1441 if (track->ms_compat) { // MS compatibility mode | |
1442 mp_msg(MSGT_DEMUX, MSGL_INFO, "[mkv] Will play video track %u\n", | |
1443 track->tnum); | |
1444 sh_v = new_sh_video(demuxer, track->tnum); | |
1445 sh_v->bih = (BITMAPINFOHEADER *)calloc(1, sizeof(BITMAPINFOHEADER)); | |
1446 if (sh_v->bih == NULL) { | |
1447 free_mkv_demuxer(mkv_d); | |
1448 return 0; | |
1449 } | |
1450 | |
1451 memcpy(sh_v->bih, track->private_data, sizeof(BITMAPINFOHEADER)); | |
1452 sh_v->format = sh_v->bih->biCompression; | |
1453 sh_v->fps = track->v_frate; | |
1454 sh_v->frametime = 1 / track->v_frate; | |
10081
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1455 sh_v->disp_w = track->v_width; |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1456 sh_v->disp_h = track->v_height; |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1457 sh_v->aspect = (float)track->v_dwidth / (float)track->v_dheight; |
0181d5fc8474
Support for aspect ratio set via DisplayWidth/DisplayHeight.
mosu
parents:
10079
diff
changeset
|
1458 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Aspect: %f\n", sh_v->aspect); |
10024 | 1459 |
1460 demuxer->video->id = track->tnum; | |
1461 demuxer->video->sh = sh_v; | |
1462 sh_v->ds = demuxer->video; | |
1463 | |
1464 mkv_d->video = track; | |
1465 } else { | |
1466 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Native CodecIDs not supported at " | |
1467 "the moment (track %u).\n", track->tnum); | |
1468 demuxer->video->id = -2; | |
1469 } | |
1470 } else { | |
1471 mp_msg(MSGT_DEMUX, MSGL_INFO, "[mkv] No video track found/wanted.\n"); | |
1472 demuxer->video->id = -2; | |
1473 } | |
1474 | |
1475 track = NULL; | |
1476 if (demuxer->audio->id == -1) { // Automatically select an audio track. | |
1477 // Search for an audio track that has the 'default' flag set. | |
1478 for (i = 0; i < mkv_d->num_tracks; i++) | |
1479 if ((mkv_d->tracks[i]->type == 'a') && mkv_d->tracks[i]->ok && | |
1480 mkv_d->tracks[i]->default_track) { | |
1481 track = mkv_d->tracks[i]; | |
1482 break; | |
1483 } | |
1484 | |
1485 if (track == NULL) | |
1486 // No track has the 'default' flag set - let's take the first audio | |
1487 // track. | |
1488 for (i = 0; i < mkv_d->num_tracks; i++) | |
1489 if ((mkv_d->tracks[i]->type == 'a') && mkv_d->tracks[i]->ok) { | |
1490 track = mkv_d->tracks[i]; | |
1491 break; | |
1492 } | |
1493 } else if (demuxer->audio->id != -2) // -2 = no audio at all | |
1494 track = find_track_by_num(mkv_d, demuxer->audio->id, NULL); | |
1495 | |
1496 if (track) { | |
1497 mp_msg(MSGT_DEMUX, MSGL_INFO, "[mkv] Will play audio track %u\n", | |
1498 track->tnum); | |
1499 sh_a = new_sh_audio(demuxer, track->tnum); | |
1500 | |
1501 demuxer->audio->id = track->tnum; | |
1502 demuxer->audio->sh = sh_a; | |
1503 sh_a->ds = demuxer->audio; | |
1504 | |
1505 mkv_d->audio = track; | |
1506 | |
1507 if (track->ms_compat) { | |
1508 sh_a->wf = (WAVEFORMATEX *)calloc(1, track->private_size); | |
1509 if (sh_a->wf == NULL) { | |
1510 free_mkv_demuxer(mkv_d); | |
1511 return 0; | |
1512 } | |
1513 memcpy(sh_a->wf, track->private_data, track->private_size); | |
1514 } else { | |
1515 sh_a->wf = (WAVEFORMATEX *)calloc(1, sizeof(WAVEFORMATEX)); | |
1516 if (sh_a->wf == NULL) { | |
1517 free_mkv_demuxer(mkv_d); | |
1518 return 0; | |
1519 } | |
1520 } | |
10125 | 1521 sh_a->format = track->a_formattag; |
1522 sh_a->wf->wFormatTag = track->a_formattag; | |
10024 | 1523 sh_a->channels = sh_a->wf->nChannels = track->a_channels; |
1524 sh_a->samplerate = sh_a->wf->nSamplesPerSec = (uint32_t)track->a_sfreq; | |
1525 if (!strcmp(track->codec_id, MKV_A_MP3)) { | |
1526 sh_a->wf->nAvgBytesPerSec = 16000; | |
1527 sh_a->wf->nBlockAlign = 1152; | |
1528 sh_a->wf->wBitsPerSample = 0; | |
1529 sh_a->samplesize = 0; | |
1530 } else if (!strcmp(track->codec_id, MKV_A_AC3)) { | |
1531 sh_a->wf->nAvgBytesPerSec = 16000; | |
1532 sh_a->wf->nBlockAlign = 1536; | |
1533 sh_a->wf->wBitsPerSample = 0; | |
1534 sh_a->samplesize = 0; | |
1535 } else if (!strcmp(track->codec_id, MKV_A_PCM)) { | |
1536 sh_a->wf->nAvgBytesPerSec = sh_a->channels * sh_a->samplerate * 2; | |
1537 sh_a->wf->nBlockAlign = sh_a->wf->nAvgBytesPerSec; | |
1538 sh_a->wf->wBitsPerSample = track->a_bps; | |
1539 sh_a->samplesize = track->a_bps / 8; | |
10125 | 1540 } else if (!strcmp(track->codec_id, MKV_A_AAC_2LC) || |
1541 !strcmp(track->codec_id, MKV_A_AAC_4LC) || | |
1542 !strcmp(track->codec_id, MKV_A_AAC_4SBR)) { | |
1543 sh_a->wf->nAvgBytesPerSec = 16000; | |
1544 sh_a->wf->nBlockAlign = 1024; | |
1545 sh_a->wf->wBitsPerSample = 0; | |
1546 sh_a->samplesize = 0; | |
10024 | 1547 } else if (!strcmp(track->codec_id, MKV_A_VORBIS)) { |
1548 for (i = 0; i < 3; i++) { | |
1549 dp = new_demux_packet(track->header_sizes[i]); | |
1550 memcpy(dp->buffer, track->headers[i], track->header_sizes[i]); | |
1551 dp->pts = 0; | |
1552 dp->flags = 0; | |
1553 ds_add_packet(demuxer->audio, dp); | |
1554 } | |
1555 } | |
1556 } else { | |
1557 mp_msg(MSGT_DEMUX, MSGL_INFO, "[mkv] No audio track found/wanted.\n"); | |
1558 demuxer->audio->id = -2; | |
1559 } | |
1560 | |
1561 // DO NOT automatically select a subtitle track and behave like DVD | |
1562 // playback: only show subtitles if the user explicitely wants them. | |
10073
4c4c2761643c
Proper key frame reporting. Proper handling of "no subtitles wanted".
mosu
parents:
10060
diff
changeset
|
1563 track = NULL; |
4c4c2761643c
Proper key frame reporting. Proper handling of "no subtitles wanted".
mosu
parents:
10060
diff
changeset
|
1564 if (demuxer->sub->id >= 0) |
4c4c2761643c
Proper key frame reporting. Proper handling of "no subtitles wanted".
mosu
parents:
10060
diff
changeset
|
1565 track = find_track_by_num(mkv_d, demuxer->sub->id, NULL); |
4c4c2761643c
Proper key frame reporting. Proper handling of "no subtitles wanted".
mosu
parents:
10060
diff
changeset
|
1566 else if (dvdsub_lang != NULL) |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1567 track = find_track_by_language(mkv_d, dvdsub_lang, NULL); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1568 if (track) { |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1569 if (strcmp(track->codec_id, MKV_S_TEXTASCII) && |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1570 strcmp(track->codec_id, MKV_S_TEXTUTF8)) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1571 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Subtitle type '%s' is not " |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1572 "supported. Track will not be displayed.\n", track->codec_id); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1573 else { |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1574 mp_msg(MSGT_DEMUX, MSGL_INFO, "[mkv] Will display subtitle track %u\n", |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1575 track->tnum); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1576 mkv_d->subs_track = track; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1577 if (!mkv_d->subs.text[0]) { |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1578 for (i = 0; i < SUB_MAX_TEXT; i++) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1579 mkv_d->subs.text[i] = (char *)malloc(256); |
10024 | 1580 |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1581 if (!strcmp(track->codec_id, MKV_S_TEXTUTF8)) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1582 sub_utf8 = 1; // Force UTF-8 conversion. |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1583 } else |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1584 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] File does not contain a " |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1585 "subtitle track with the id %u.\n", demuxer->sub->id); |
10024 | 1586 } |
1587 } | |
1588 | |
1589 if (s->end_pos == 0) | |
1590 demuxer->seekable = 0; | |
1591 else { | |
1592 demuxer->movi_start = s->start_pos; | |
1593 demuxer->movi_end = s->end_pos; | |
1594 demuxer->seekable = 1; | |
1595 } | |
1596 | |
1597 demuxer->priv = mkv_d; | |
1598 | |
1599 return 1; | |
1600 } | |
1601 | |
1602 extern "C" int demux_mkv_fill_buffer(demuxer_t *d) { | |
1603 demux_packet_t *dp; | |
1604 demux_stream_t *ds; | |
1605 mkv_demuxer_t *mkv_d; | |
1606 int upper_lvl_el, exit_loop, found_data, i, delete_element, elements_found; | |
1607 // Elements for different levels | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1608 EbmlElement *l0 = NULL, *l1 = NULL, *l2 = NULL, *l3 = NULL; |
10024 | 1609 EbmlStream *es; |
1610 KaxBlock *block; | |
1611 int64_t block_duration, block_ref1, block_ref2; | |
1612 | |
1613 mkv_d = (mkv_demuxer_t *)d->priv; | |
1614 es = mkv_d->es; | |
1615 l0 = mkv_d->segment; | |
1616 | |
1617 // End of stream | |
1618 if (mkv_d->saved_l1 == NULL) | |
1619 return 0; | |
1620 | |
1621 exit_loop = 0; | |
1622 upper_lvl_el = 0; | |
1623 l1 = mkv_d->saved_l1; | |
1624 mkv_d->saved_l1 = NULL; | |
1625 found_data = 0; | |
1626 try { | |
1627 // The idea is not to handle a complete KaxCluster with each call to | |
1628 // demux_mkv_fill_buffer because those might be rather big. | |
1629 while (l1 != NULL) { | |
1630 if ((upper_lvl_el != 0) || exit_loop) | |
1631 break; | |
1632 | |
1633 if (EbmlId(*l1) == KaxCluster::ClassInfos.GlobalId) { | |
1634 mkv_d->cluster = (KaxCluster *)l1; | |
1635 if (found_data) { | |
1636 mkv_d->saved_l1 = l1; | |
1637 break; | |
1638 } | |
1639 | |
1640 if (mkv_d->saved_l2 != NULL) { | |
1641 l2 = mkv_d->saved_l2; | |
1642 mkv_d->saved_l2 = NULL; | |
1643 } else | |
1644 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1645 0xFFFFFFFFL, true, 1); | |
1646 while (l2 != NULL) { | |
1647 if (upper_lvl_el != 0) | |
1648 break; | |
1649 | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1650 // Handle at least one data packets in one call to |
10024 | 1651 // demux_mkv_fill_buffer - but abort if we have found that. |
1652 if (found_data >= 1) { | |
1653 mkv_d->saved_l2 = l2; | |
1654 mkv_d->saved_l1 = l1; | |
1655 exit_loop = 1; | |
1656 break; | |
1657 } | |
1658 | |
1659 if (EbmlId(*l2) == KaxClusterTimecode::ClassInfos.GlobalId) { | |
1660 KaxClusterTimecode &ctc = *static_cast<KaxClusterTimecode *>(l2); | |
1661 ctc.ReadData(es->I_O()); | |
1662 mkv_d->cluster_tc = uint64(ctc); | |
1663 mkv_d->cluster->InitTimecode(mkv_d->cluster_tc); | |
1664 | |
1665 } else if (EbmlId(*l2) == KaxBlockGroup::ClassInfos.GlobalId) { | |
1666 | |
1667 block = NULL; | |
1668 block_duration = -1; | |
1669 block_ref1 = 0; | |
1670 block_ref2 = 0; | |
1671 elements_found = 0; | |
1672 | |
1673 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, | |
1674 0xFFFFFFFFL, true, 1); | |
1675 while (l3 != NULL) { | |
1676 delete_element = 1; | |
1677 if (upper_lvl_el > 0) | |
1678 break; | |
1679 | |
1680 if (EbmlId(*l3) == KaxBlock::ClassInfos.GlobalId) { | |
1681 block = static_cast<KaxBlock *>(l3); | |
10084
04dacb55d55d
timecode scale value must be initialized before the duration is found. API change of libmatroska 0.4.2. Will not work with 0.4.1!
mosu
parents:
10081
diff
changeset
|
1682 block->ReadData(es->I_O()); |
10024 | 1683 block->SetParent(*mkv_d->cluster); |
1684 delete_element = 0; | |
1685 elements_found |= 1; | |
1686 | |
1687 } else if (EbmlId(*l3) == | |
1688 KaxBlockDuration::ClassInfos.GlobalId) { | |
1689 KaxBlockDuration &duration = | |
1690 *static_cast<KaxBlockDuration *>(l3); | |
1691 duration.ReadData(es->I_O()); | |
1692 block_duration = (int64_t)uint64(duration); | |
1693 elements_found |= 2; | |
1694 | |
1695 } else if (EbmlId(*l3) == | |
1696 KaxReferenceBlock::ClassInfos.GlobalId) { | |
1697 KaxReferenceBlock &ref = | |
1698 *static_cast<KaxReferenceBlock *>(l3); | |
1699 ref.ReadData(es->I_O()); | |
1700 if (block_ref1 == 0) { | |
1701 block_ref1 = int64(ref); | |
1702 elements_found |= 4; | |
1703 } else { | |
1704 block_ref2 = int64(ref); | |
1705 elements_found |= 8; | |
1706 } | |
1707 | |
1708 } | |
1709 | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1710 l3->SkipData(static_cast<EbmlStream &>(*es), |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1711 l3->Generic().Context); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1712 if (delete_element) |
10024 | 1713 delete l3; |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1714 l3 = es->FindNextElement(l2->Generic().Context, upper_lvl_el, |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1715 0xFFFFFFFFL, true, 1); |
10024 | 1716 } // while (l3 != NULL) |
1717 | |
1718 if (block != NULL) { | |
1719 // Clear the subtitles if they're obsolete now. | |
1720 if ((mkv_d->clear_subs_at > 0) && | |
1721 (mkv_d->clear_subs_at <= | |
1722 (block->GlobalTimecode() / 1000000))) { | |
1723 mkv_d->subs.lines = 0; | |
1724 vo_sub = &mkv_d->subs; | |
1725 vo_osd_changed(OSDTYPE_SUBTITLE); | |
1726 mkv_d->clear_subs_at = 0; | |
1727 } | |
1728 | |
1729 ds = NULL; | |
1730 if ((mkv_d->video != NULL) && | |
1731 (mkv_d->video->tnum == block->TrackNum())) | |
1732 ds = d->video; | |
1733 else if ((mkv_d->audio != NULL) && | |
1734 (mkv_d->audio->tnum == block->TrackNum())) | |
1735 ds = d->audio; | |
1736 | |
1737 if (!mkv_d->skip_to_keyframe || // Not skipping is ok. | |
1738 (((elements_found & 4) == 0) && // It's a key frame. | |
1739 (ds != NULL) && // Corresponding track found | |
1740 (ds == d->video))) { // track is our video track | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1741 if ((ds != NULL) && ((block->GlobalTimecode() / 1000000) >= |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1742 (uint64_t)mkv_d->skip_to_timecode)) { |
10024 | 1743 for (i = 0; i < (int)block->NumberFrames(); i++) { |
1744 DataBuffer &data = block->GetBuffer(i); | |
1745 dp = new_demux_packet(data.Size()); | |
1746 memcpy(dp->buffer, data.Buffer(), data.Size()); | |
1747 dp->pts = mkv_d->last_pts; | |
10073
4c4c2761643c
Proper key frame reporting. Proper handling of "no subtitles wanted".
mosu
parents:
10060
diff
changeset
|
1748 dp->flags = (elements_found & 4) == 0 ? 1 : 0; // keyframe |
10024 | 1749 ds_add_packet(ds, dp); |
1750 found_data++; | |
1751 } | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1752 mkv_d->skip_to_keyframe = 0; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1753 mkv_d->skip_to_timecode = 0; |
10024 | 1754 } else if ((mkv_d->subs_track != NULL) && |
1755 (mkv_d->subs_track->tnum == block->TrackNum())) | |
1756 handle_subtitles(d, block, block_duration); | |
1757 | |
1758 d->filepos = mkv_d->in->getFilePointer(); | |
1759 mkv_d->last_pts = (float)block->GlobalTimecode() / | |
1760 1000000000.0; | |
1761 mkv_d->last_filepos = d->filepos; | |
1762 } | |
1763 | |
1764 delete block; | |
1765 } // block != NULL | |
1766 | |
1767 } | |
1768 | |
1769 if (upper_lvl_el > 0) { // we're coming from l3 | |
1770 upper_lvl_el--; | |
1771 delete l2; | |
1772 l2 = l3; | |
1773 if (upper_lvl_el > 0) | |
1774 break; | |
1775 } else { | |
1776 l2->SkipData(static_cast<EbmlStream &>(*es), | |
1777 l2->Generic().Context); | |
1778 delete l2; | |
1779 l2 = es->FindNextElement(l1->Generic().Context, upper_lvl_el, | |
1780 0xFFFFFFFFL, true, 1); | |
1781 } | |
1782 } // while (l2 != NULL) | |
1783 } else if (EbmlId(*l1) == KaxCues::ClassInfos.GlobalId) | |
1784 return 0; | |
1785 else | |
1786 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Unknown element@1: %s (stream " | |
1787 "position %llu)\n", typeid(*l1).name(), | |
1788 l1->GetElementPosition()); | |
1789 | |
1790 if (exit_loop) | |
1791 break; | |
1792 | |
1793 if (upper_lvl_el > 0) { // we're coming from l2 | |
1794 upper_lvl_el--; | |
1795 delete l1; | |
1796 l1 = l2; | |
1797 if (upper_lvl_el > 0) | |
1798 break; | |
1799 } else { | |
1800 l1->SkipData(static_cast<EbmlStream &>(*es), l1->Generic().Context); | |
1801 delete l1; | |
1802 l1 = es->FindNextElement(l0->Generic().Context, upper_lvl_el, | |
1803 0xFFFFFFFFL, true, 1); | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1804 if ((l1 != NULL) && (EbmlId(*l1) == KaxCluster::ClassInfos.GlobalId)) |
10024 | 1805 add_cluster_position(mkv_d, l1->GetElementPosition()); |
1806 } | |
1807 } // while (l1 != NULL) | |
1808 } catch (exception ex) { | |
1809 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] exception caught\n"); | |
1810 return 0; | |
1811 } | |
1812 | |
1813 if (found_data) | |
1814 return 1; | |
1815 | |
1816 return 0; | |
1817 } | |
1818 | |
1819 extern "C" void resync_audio_stream(sh_audio_t *sh_audio); | |
1820 | |
1821 extern "C" void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs, | |
1822 int flags) { | |
1823 int i, k, upper_lvl_el; | |
1824 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)demuxer->priv; | |
1825 int64_t target_timecode, target_filepos = 0, min_diff, diff, current_pos; | |
1826 int64_t cluster_pos; | |
1827 mkv_track_index_t *index; | |
1828 mkv_index_entry_t *entry; | |
1829 EbmlElement *l1; | |
1830 | |
1831 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] SEEK, relss: %.3f, flags: %d\n", | |
1832 rel_seek_secs, flags); | |
1833 | |
1834 if (!mkv_d->cues_found && !mkv_d->cues_searched) { | |
1835 // We've not found an index so far. So let's skip over all level 1 | |
1836 // elements until we either hit another segment, the end of the file | |
1837 // or - suprise - some cues. | |
1838 current_pos = mkv_d->in->getFilePointer(); | |
1839 | |
1840 // Skip the data but do not delete the element! This is our current | |
1841 // cluster, and we need it later on in demux_mkv_fill_buffer. | |
1842 l1 = mkv_d->saved_l1; | |
1843 l1->SkipData(static_cast<EbmlStream &>(*mkv_d->es), l1->Generic().Context); | |
1844 l1 = mkv_d->es->FindNextElement(mkv_d->segment->Generic().Context, | |
1845 upper_lvl_el, 0xFFFFFFFFL, true, 1); | |
1846 while (l1 != NULL) { | |
1847 if (upper_lvl_el) | |
1848 break; | |
1849 | |
1850 if (EbmlId(*l1) == KaxCues::ClassInfos.GlobalId) { | |
1851 mkv_d->in->setFilePointer(l1->GetElementPosition()); | |
1852 delete l1; | |
1853 mkv_d->cues_found = parse_cues(mkv_d); | |
1854 break; | |
1855 } else { | |
1856 if (EbmlId(*l1) == KaxCluster::ClassInfos.GlobalId) | |
1857 add_cluster_position(mkv_d, l1->GetElementPosition()); | |
1858 l1->SkipData(static_cast<EbmlStream &>(*mkv_d->es), | |
1859 l1->Generic().Context); | |
1860 delete l1; | |
1861 l1 = mkv_d->es->FindNextElement(mkv_d->segment->Generic().Context, | |
1862 upper_lvl_el, 0xFFFFFFFFL, true, 1); | |
1863 } | |
1864 } | |
1865 | |
1866 if (demuxer->stream->eof) | |
1867 stream_reset(demuxer->stream); | |
1868 mkv_d->in->setFilePointer(current_pos); | |
1869 | |
1870 mkv_d->cues_searched = 1; | |
1871 } | |
1872 | |
1873 if (!(flags & 2)) { // Time in secs | |
1874 if (flags & 1) // Absolute seek | |
1875 target_timecode = 0; | |
1876 else // Relative seek | |
1877 target_timecode = (int64_t)(mkv_d->last_pts * 1000.0); | |
1878 target_timecode += (int64_t)(rel_seek_secs * 1000.0); | |
1879 if (target_timecode < 0) | |
1880 target_timecode = 0; | |
1881 | |
1882 min_diff = 0xFFFFFFFL; | |
1883 | |
1884 // Let's find the entry in the index with the smallest difference | |
1885 // to the wanted timecode. | |
1886 entry = NULL; | |
1887 for (i = 0; i < mkv_d->num_indexes; i++) | |
1888 if (mkv_d->index[i].tnum == mkv_d->video->tnum) { | |
1889 index = &mkv_d->index[i]; | |
1890 for (k = 0; k < index->num_entries; k++) { | |
1891 if (!index->entries[k].is_key) | |
1892 continue; | |
1893 diff = target_timecode - (int64_t)index->entries[k].timecode; | |
1894 if (diff < 0) | |
1895 diff *= -1; | |
1896 if (diff < min_diff) { | |
1897 min_diff = diff; | |
1898 entry = & index->entries[k]; | |
1899 } | |
1900 } | |
1901 break; | |
1902 } | |
1903 | |
1904 if (mkv_d->saved_l1 != NULL) | |
1905 delete mkv_d->saved_l1; | |
1906 | |
1907 if (mkv_d->saved_l2 != NULL) { | |
1908 delete mkv_d->saved_l2; | |
1909 mkv_d->saved_l2 = NULL; | |
1910 } | |
1911 | |
1912 if (entry != NULL) { // We've found an entry. | |
1913 mkv_d->in->setFilePointer(entry->filepos); | |
1914 upper_lvl_el = 0; | |
1915 mkv_d->saved_l1 = | |
1916 mkv_d->es->FindNextElement(mkv_d->segment->Generic().Context, | |
1917 upper_lvl_el, 0xFFFFFFFFL, true, 1); | |
1918 } else { // We've not found an entry --> no index? | |
1919 target_filepos = (int64_t)(target_timecode * mkv_d->last_filepos / | |
1920 (mkv_d->last_pts * 1000.0)); | |
1921 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] No index entry found. Calculated " | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1922 "filepos %lld. Old timecode %lld.\n", target_filepos, |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1923 (int64_t)(mkv_d->last_pts * 1000.0)); |
10024 | 1924 // Let's find the nearest cluster so that libebml does not have to |
1925 // do so much work. | |
1926 cluster_pos = 0; | |
1927 min_diff = 0x0FFFFFFFL; | |
1928 for (i = 0; i < mkv_d->num_cluster_pos; i++) { | |
1929 diff = mkv_d->cluster_positions[i] - target_filepos; | |
1930 if (rel_seek_secs < 0) { | |
1931 if ((diff > 0) && (diff < min_diff)) { | |
1932 cluster_pos = mkv_d->cluster_positions[i]; | |
1933 min_diff = diff; | |
1934 } | |
10099
71d703e35ff3
Removed the abs function as someone reported compilation problems under MacOS.
mosu
parents:
10098
diff
changeset
|
1935 } else if ((diff < 0 ? -1 * diff : diff) < min_diff) { |
10024 | 1936 cluster_pos = mkv_d->cluster_positions[i]; |
10099
71d703e35ff3
Removed the abs function as someone reported compilation problems under MacOS.
mosu
parents:
10098
diff
changeset
|
1937 min_diff = diff < 0 ? -1 * diff : diff; |
10024 | 1938 } |
1939 } | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1940 if (min_diff != 0x0FFFFFFFL) { |
10024 | 1941 target_filepos = cluster_pos; |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1942 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] New target_filepos because of " |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1943 "cluster: %lld.\n", target_filepos); |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1944 } |
10024 | 1945 if (target_filepos >= demuxer->movi_end) |
1946 return; | |
1947 mkv_d->in->setFilePointer(target_filepos); | |
1948 upper_lvl_el = 0; | |
1949 mkv_d->saved_l1 = | |
1950 mkv_d->es->FindNextElement(mkv_d->segment->Generic().Context, | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1951 upper_lvl_el, 0xFFFFFFFFL, true, 1); |
10024 | 1952 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek result: target_timecode %lld, " |
1953 "did not find an entry. Calculated target_filspos: %lld\n", | |
1954 target_timecode, target_filepos); | |
1955 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek found %p (%s).\n", | |
1956 mkv_d->saved_l1, mkv_d->saved_l1 == NULL ? "null" : | |
1957 typeid(*mkv_d->saved_l1).name()); | |
1958 } | |
1959 | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1960 if (mkv_d->video != NULL) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1961 mkv_d->skip_to_keyframe = 1; |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1962 if (rel_seek_secs > 0.0) |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1963 mkv_d->skip_to_timecode = target_timecode; |
10024 | 1964 |
1965 demux_mkv_fill_buffer(demuxer); | |
10060
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1966 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] New timecode: %lld\n", |
f4d6cf8c6c93
Fixed seeking in audio-only files. Added support for subtitle track selection via "-slang".
mosu
parents:
10036
diff
changeset
|
1967 (int64_t)(mkv_d->last_pts * 1000.0)); |
10024 | 1968 |
1969 mkv_d->subs.lines = 0; | |
1970 vo_sub = &mkv_d->subs; | |
1971 vo_osd_changed(OSDTYPE_SUBTITLE); | |
1972 mkv_d->clear_subs_at = 0; | |
1973 | |
1974 if(demuxer->audio->sh != NULL) | |
1975 resync_audio_stream((sh_audio_t *)demuxer->audio->sh); | |
1976 | |
1977 } else | |
1978 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n"); | |
1979 | |
1980 } | |
1981 | |
1982 extern "C" void demux_close_mkv(demuxer_t *demuxer) { | |
1983 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)demuxer->priv; | |
1984 | |
1985 free_mkv_demuxer(mkv_d); | |
1986 | |
1987 #ifdef USE_ICONV | |
1988 subcp_close(); | |
1989 #endif | |
1990 } | |
1991 | |
1992 extern "C" int demux_mkv_control(demuxer_t *demuxer, int cmd, void *arg) { | |
1993 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)demuxer->priv; | |
1994 | |
1995 switch (cmd) { | |
1996 case DEMUXER_CTRL_GET_TIME_LENGTH: | |
1997 if (mkv_d->duration == -1.0) | |
1998 return DEMUXER_CTRL_DONTKNOW; | |
1999 | |
2000 *((unsigned long *)arg) = (unsigned long)mkv_d->duration; | |
2001 return DEMUXER_CTRL_OK; | |
2002 | |
2003 case DEMUXER_CTRL_GET_PERCENT_POS: | |
2004 if (mkv_d->duration == -1.0) { | |
2005 if (demuxer->movi_start == demuxer->movi_end) | |
2006 return DEMUXER_CTRL_DONTKNOW; | |
2007 | |
2008 *((int *)arg) = | |
2009 (int)((demuxer->filepos - demuxer->movi_start) / | |
2010 ((demuxer->movi_end - demuxer->movi_start) / 100)); | |
2011 return DEMUXER_CTRL_OK; | |
2012 } | |
2013 | |
2014 *((int *)arg) = (int)(100 * mkv_d->last_pts / mkv_d->duration); | |
2015 return DEMUXER_CTRL_OK; | |
2016 | |
2017 default: | |
2018 return DEMUXER_CTRL_NOTIMPL; | |
2019 } | |
2020 } | |
2021 | |
2022 #endif /* HAVE_MATROSKA */ |