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