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