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