typo fix
[mplayer/greg.git] / libmpdemux / demux_mkv.c
blob867dfd2f8a396700c0e625acc9bae11cb4fc44a6
1 /*
2 * native Matroska demuxer
3 * Written by Aurelien Jacobs <aurel@gnuage.org>
4 * Based on the one written by Ronald Bultje for gstreamer
5 * and on demux_mkv.cpp from Moritz Bunkus.
6 * Licence: GPL
7 */
9 #include "config.h"
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <inttypes.h>
16 #include "stream/stream.h"
17 #include "demuxer.h"
18 #include "stheader.h"
19 #include "ebml.h"
20 #include "matroska.h"
22 #include "mp_msg.h"
23 #include "help_mp.h"
25 #include "vobsub.h"
26 #include "subreader.h"
27 #include "libvo/sub.h"
29 #include "libass/ass.h"
30 #include "libass/ass_mp.h"
32 #include "libavutil/common.h"
34 #ifdef USE_QTX_CODECS
35 #include "loader/qtx/qtxsdk/components.h"
36 #endif
38 #ifdef HAVE_ZLIB
39 #include <zlib.h>
40 #endif
42 #ifdef USE_LIBAVUTIL_SO
43 #include <ffmpeg/lzo.h>
44 #include <ffmpeg/intreadwrite.h>
45 #else
46 #include "libavutil/lzo.h"
47 #include "libavutil/intreadwrite.h"
48 #endif
49 #include "libavutil/avstring.h"
51 static unsigned char sipr_swaps[38][2]={
52 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
53 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
54 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
55 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
56 {77,80} };
58 // Map flavour to bytes per second
59 #define SIPR_FLAVORS 4
60 #define ATRC_FLAVORS 8
61 #define COOK_FLAVORS 34
62 static int sipr_fl2bps[SIPR_FLAVORS] = {813, 1062, 625, 2000};
63 static int atrc_fl2bps[ATRC_FLAVORS] = {8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100};
64 static int cook_fl2bps[COOK_FLAVORS] = {1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
65 4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
66 4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
67 12016, 16408, 22911, 33506};
69 typedef struct
71 uint32_t order, type, scope;
72 uint32_t comp_algo;
73 uint8_t *comp_settings;
74 int comp_settings_len;
75 } mkv_content_encoding_t;
77 typedef struct mkv_track
79 int tnum;
80 char *name;
82 char *codec_id;
83 int ms_compat;
84 char *language;
86 int type;
88 uint32_t v_width, v_height, v_dwidth, v_dheight;
89 float v_frate;
91 uint32_t a_formattag;
92 uint32_t a_channels, a_bps;
93 float a_sfreq;
95 float default_duration;
97 int default_track;
99 void *private_data;
100 unsigned int private_size;
102 /* stuff for realmedia */
103 int realmedia;
104 int rv_kf_base, rv_kf_pts;
105 float rv_pts; /* previous video timestamp */
106 float ra_pts; /* previous audio timestamp */
108 /** realaudio descrambling */
109 int sub_packet_size; ///< sub packet size, per stream
110 int sub_packet_h; ///< number of coded frames per block
111 int coded_framesize; ///< coded frame size, per stream
112 int audiopk_size; ///< audio packet size
113 unsigned char *audio_buf; ///< place to store reordered audio data
114 float *audio_timestamp; ///< timestamp for each audio packet
115 int sub_packet_cnt; ///< number of subpacket already received
116 int audio_filepos; ///< file position of first audio packet in block
118 /* stuff for quicktime */
119 int fix_i_bps;
120 float qt_last_a_pts;
122 int subtitle_type;
124 /* The timecodes of video frames might have to be reordered if they're
125 in display order (the timecodes, not the frames themselves!). In this
126 case demux packets have to be cached with the help of these variables. */
127 int reorder_timecodes;
128 demux_packet_t **cached_dps;
129 int num_cached_dps, num_allocated_dps;
130 float max_pts;
132 /* generic content encoding support */
133 mkv_content_encoding_t *encodings;
134 int num_encodings;
136 /* For VobSubs and SSA/ASS */
137 sh_sub_t *sh_sub;
138 } mkv_track_t;
140 typedef struct mkv_index
142 int tnum;
143 uint64_t timecode, filepos;
144 } mkv_index_t;
146 typedef struct mkv_demuxer
148 off_t segment_start;
150 float duration, last_pts;
151 uint64_t last_filepos;
153 mkv_track_t **tracks;
154 int num_tracks;
156 uint64_t tc_scale, cluster_tc, first_tc;
157 int has_first_tc;
159 uint64_t cluster_size;
160 uint64_t blockgroup_size;
162 mkv_index_t *indexes;
163 int num_indexes;
165 off_t *parsed_cues;
166 int parsed_cues_num;
167 off_t *parsed_seekhead;
168 int parsed_seekhead_num;
170 uint64_t *cluster_positions;
171 int num_cluster_pos;
173 int64_t skip_to_timecode;
174 int v_skip_to_keyframe, a_skip_to_keyframe;
176 int64_t stop_timecode;
178 int last_aid;
179 int audio_tracks[MAX_A_STREAMS];
180 } mkv_demuxer_t;
182 #define REALHEADER_SIZE 16
183 #define RVPROPERTIES_SIZE 34
184 #define RAPROPERTIES4_SIZE 56
185 #define RAPROPERTIES5_SIZE 70
187 /* for e.g. "-slang ger" */
188 extern char *dvdsub_lang;
189 extern char *audio_lang;
190 extern int dvdsub_id;
193 * \brief ensures there is space for at least one additional element
194 * \param array array to grow
195 * \param nelem current number of elements in array
196 * \param elsize size of one array element
198 static void grow_array(void **array, int nelem, size_t elsize) {
199 if (!(nelem & 31))
200 *array = realloc(*array, (nelem + 32) * elsize);
203 static mkv_track_t *
204 demux_mkv_find_track_by_num (mkv_demuxer_t *d, int n, int type)
206 int i, id;
208 for (i=0, id=0; i < d->num_tracks; i++)
209 if (d->tracks[i] != NULL && d->tracks[i]->type == type)
210 if (id++ == n)
211 return d->tracks[i];
213 return NULL;
216 static mkv_track_t *
217 demux_mkv_find_track_by_language (mkv_demuxer_t *d, char *language, int type)
219 int i, len;
221 language += strspn(language,",");
222 while((len = strcspn(language,",")) > 0)
224 for (i=0; i < d->num_tracks; i++)
225 if (d->tracks[i] != NULL && d->tracks[i]->language != NULL &&
226 d->tracks[i]->type == type &&
227 !strncmp(d->tracks[i]->language, language, len))
228 return d->tracks[i];
229 language += len;
230 language += strspn(language,",");
233 return NULL;
236 static void
237 add_cluster_position (mkv_demuxer_t *mkv_d, uint64_t position)
239 int i = mkv_d->num_cluster_pos;
241 while (i--)
242 if (mkv_d->cluster_positions[i] == position)
243 return;
245 grow_array(&mkv_d->cluster_positions, mkv_d->num_cluster_pos,
246 sizeof(uint64_t));
247 mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position;
251 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
252 static int
253 aac_get_sample_rate_index (uint32_t sample_rate)
255 if (92017 <= sample_rate)
256 return 0;
257 else if (75132 <= sample_rate)
258 return 1;
259 else if (55426 <= sample_rate)
260 return 2;
261 else if (46009 <= sample_rate)
262 return 3;
263 else if (37566 <= sample_rate)
264 return 4;
265 else if (27713 <= sample_rate)
266 return 5;
267 else if (23004 <= sample_rate)
268 return 6;
269 else if (18783 <= sample_rate)
270 return 7;
271 else if (13856 <= sample_rate)
272 return 8;
273 else if (11502 <= sample_rate)
274 return 9;
275 else if (9391 <= sample_rate)
276 return 10;
277 else
278 return 11;
282 static int
283 vobsub_parse_size (sh_sub_t *sh, const char *start)
285 if (sscanf(&start[6], "%dx%d", &sh->width, &sh->height) == 2)
287 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] VobSub size: %ux%u\n",
288 sh->width, sh->height);
289 return 1;
291 return 0;
294 static int
295 vobsub_parse_palette (sh_sub_t *sh, const char *start)
297 int i;
299 start += 8;
300 while (isspace(*start))
301 start++;
302 for (i = 0; i < 16; i++)
304 unsigned int tmp;
305 if (sscanf(start, "%06x", &tmp) != 1)
306 break;
307 sh->palette[i] = vobsub_palette_to_yuv(tmp);
308 start += 6;
309 while ((*start == ',') || isspace(*start))
310 start++;
312 if (i == 16)
314 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] VobSub palette: %06x,%06x,"
315 "%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,"
316 "%06x,%06x,%06x\n", sh->palette[0],
317 sh->palette[1], sh->palette[2],
318 sh->palette[3], sh->palette[4],
319 sh->palette[5], sh->palette[6],
320 sh->palette[7], sh->palette[8],
321 sh->palette[9], sh->palette[10],
322 sh->palette[11], sh->palette[12],
323 sh->palette[13], sh->palette[14],
324 sh->palette[15]);
325 sh->has_palette = 1;
326 return 2;
328 return 0;
331 static int
332 vobsub_parse_custom_colors (sh_sub_t *sh, const char *start)
334 int use_custom_colors, i;
335 const char *p;
336 unsigned int tridx = 0;
338 use_custom_colors = 0;
339 start += 14;
340 while (isspace(*start))
341 start++;
342 if (!strncasecmp(start, "ON", 2) || (*start == '1'))
343 use_custom_colors = 1;
344 else if (!strncasecmp(start, "OFF", 3) || (*start == '0'))
345 use_custom_colors = 0;
346 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] VobSub custom colors: %s\n",
347 use_custom_colors ? "ON" : "OFF");
348 if ((p = strstr(start, "tridx:")) != NULL)
349 tridx = strtoul(p + 6, NULL, 2);
350 if ((start = strstr(start, "colors:")) != NULL)
352 start += 7;
353 while (isspace(*start))
354 start++;
355 for (i = 0; i < 4; i++)
357 unsigned int tmp;
358 if (sscanf(start, "%06x", &tmp) != 1)
359 break;
360 sh->colors[i] = vobsub_rgb_to_yuv(tmp);
361 if ((tridx << i) & 0x08)
362 sh->colors[i] |= 1 << 31;
363 start += 6;
364 while ((*start == ',') || isspace(*start))
365 start++;
367 if (i == 4)
369 sh->custom_colors = 4;
370 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] VobSub colors: %08x,"
371 "%08x,%08x,%08x\n", sh->colors[0],
372 sh->colors[1], sh->colors[2],
373 sh->colors[3]);
376 if (!use_custom_colors)
377 sh->custom_colors = 0;
378 return 4;
381 static int
382 vobsub_parse_forced_subs (sh_sub_t *sh, const char *start)
384 start += 12;
385 while (isspace(*start))
386 start++;
387 if (!strncasecmp(start, "on", 2) || (*start == '1'))
388 sh->forced_subs_only = 1;
389 else if (!strncasecmp(start, "off", 3) || (*start == '0'))
390 sh->forced_subs_only = 0;
391 else
392 return 0;
393 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] VobSub forced subs: %d\n",
394 sh->forced_subs_only);
395 return 8;
398 /** \brief Free cached demux packets
400 * Reordering the timecodes requires caching of demux packets. This function
401 * frees all these cached packets and the memory for the cached pointers
402 * itself.
404 * \param demuxer The demuxer for which the cache is to be freed.
406 static void
407 free_cached_dps (demuxer_t *demuxer)
409 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
410 mkv_track_t *track;
411 int i, k;
413 for (k = 0; k < mkv_d->num_tracks; k++)
415 track = mkv_d->tracks[k];
416 for (i = 0; i < track->num_cached_dps; i++)
417 free_demux_packet (track->cached_dps[i]);
418 free(track->cached_dps);
419 track->cached_dps = NULL;
420 track->num_cached_dps = 0;
421 track->num_allocated_dps = 0;
422 track->max_pts = 0;
426 static int
427 demux_mkv_parse_idx (mkv_track_t *t)
429 int things_found, last;
430 char *buf, *pos, *start;
432 if ((t->private_data == NULL) || (t->private_size == 0))
433 return 0;
435 things_found = 0;
436 buf = malloc(t->private_size + 1);
437 if (buf == NULL)
438 return 0;
439 memcpy(buf, t->private_data, t->private_size);
440 buf[t->private_size] = 0;
441 t->sh_sub->has_palette = 0;
443 pos = buf;
444 start = buf;
445 last = 0;
448 if ((*pos == 0) || (*pos == '\r') || (*pos == '\n'))
450 if (*pos == 0)
451 last = 1;
452 *pos = 0;
454 if (!strncasecmp(start, "size: ", 6))
455 things_found |= vobsub_parse_size(t->sh_sub, start);
456 else if (!strncasecmp(start, "palette:", 8))
457 things_found |= vobsub_parse_palette(t->sh_sub, start);
458 else if (!strncasecmp(start, "custom colors:", 14))
459 things_found |= vobsub_parse_custom_colors(t->sh_sub, start);
460 else if (!strncasecmp(start, "forced subs:", 12))
461 things_found |= vobsub_parse_forced_subs(t->sh_sub, start);
463 if (last)
464 break;
467 pos++;
469 while ((*pos == '\r') || (*pos == '\n'));
470 start = pos;
472 else
473 pos++;
475 while (!last && (*start != 0));
477 free(buf);
479 return (things_found & 3) == 3;
483 static int
484 demux_mkv_decode (mkv_track_t *track, uint8_t *src, uint8_t **dest,
485 uint32_t *size, uint32_t type)
487 int i, result;
488 int modified = 0;
490 *dest = src;
491 if (track->num_encodings <= 0)
492 return 0;
494 for (i=0; i<track->num_encodings; i++)
496 if (!(track->encodings[i].scope & type))
497 continue;
499 #ifdef HAVE_ZLIB
500 if (track->encodings[i].comp_algo == 0)
502 /* zlib encoded track */
503 z_stream zstream;
505 zstream.zalloc = (alloc_func) 0;
506 zstream.zfree = (free_func) 0;
507 zstream.opaque = (voidpf) 0;
508 if (inflateInit (&zstream) != Z_OK)
510 mp_msg (MSGT_DEMUX, MSGL_WARN,
511 MSGTR_MPDEMUX_MKV_ZlibInitializationFailed);
512 return modified;
514 zstream.next_in = (Bytef *) src;
515 zstream.avail_in = *size;
517 modified = 1;
518 *dest = NULL;
519 zstream.avail_out = *size;
520 do {
521 *size += 4000;
522 *dest = realloc (*dest, *size);
523 zstream.next_out = (Bytef *) (*dest + zstream.total_out);
524 result = inflate (&zstream, Z_NO_FLUSH);
525 if (result != Z_OK && result != Z_STREAM_END)
527 mp_msg (MSGT_DEMUX, MSGL_WARN,
528 MSGTR_MPDEMUX_MKV_ZlibDecompressionFailed);
529 free(*dest);
530 *dest = NULL;
531 inflateEnd (&zstream);
532 return modified;
534 zstream.avail_out += 4000;
535 } while (zstream.avail_out == 4000 &&
536 zstream.avail_in != 0 && result != Z_STREAM_END);
538 *size = zstream.total_out;
539 inflateEnd (&zstream);
541 #endif
542 if (track->encodings[i].comp_algo == 2)
544 /* lzo encoded track */
545 int dstlen = *size * 3;
547 *dest = NULL;
548 while (1)
550 int srclen = *size;
551 if (dstlen > SIZE_MAX - LZO_OUTPUT_PADDING) goto lzo_fail;
552 *dest = realloc (*dest, dstlen + LZO_OUTPUT_PADDING);
553 result = lzo1x_decode (*dest, &dstlen, src, &srclen);
554 if (result == 0)
555 break;
556 if (!(result & LZO_OUTPUT_FULL))
558 lzo_fail:
559 mp_msg (MSGT_DEMUX, MSGL_WARN,
560 MSGTR_MPDEMUX_MKV_LzoDecompressionFailed);
561 free(*dest);
562 *dest = NULL;
563 return modified;
565 mp_msg (MSGT_DEMUX, MSGL_DBG2,
566 "[mkv] lzo decompression buffer too small.\n");
567 dstlen *= 2;
569 *size = dstlen;
573 return modified;
577 static int
578 demux_mkv_read_info (demuxer_t *demuxer)
580 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
581 stream_t *s = demuxer->stream;
582 uint64_t length, l;
583 int il;
584 uint64_t tc_scale = 1000000;
585 long double duration = 0.;
587 length = ebml_read_length (s, NULL);
588 while (length > 0)
590 switch (ebml_read_id (s, &il))
592 case MATROSKA_ID_TIMECODESCALE:
594 uint64_t num = ebml_read_uint (s, &l);
595 if (num == EBML_UINT_INVALID)
596 return 1;
597 tc_scale = num;
598 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + timecode scale: %"PRIu64"\n",
599 tc_scale);
600 break;
603 case MATROSKA_ID_DURATION:
605 long double num = ebml_read_float (s, &l);
606 if (num == EBML_FLOAT_INVALID)
607 return 1;
608 duration = num;
609 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + duration: %.3Lfs\n",
610 duration * tc_scale / 1000000000.0);
611 break;
614 default:
615 ebml_read_skip (s, &l);
616 break;
618 length -= l + il;
620 mkv_d->tc_scale = tc_scale;
621 mkv_d->duration = duration * tc_scale / 1000000000.0;
622 return 0;
626 * \brief free array of kv_content_encoding_t
627 * \param encodings pointer to array
628 * \param numencodings number of encodings in array
630 static void
631 demux_mkv_free_encodings(mkv_content_encoding_t *encodings, int numencodings)
633 while (numencodings-- > 0)
634 free(encodings[numencodings].comp_settings);
635 free(encodings);
638 static int
639 demux_mkv_read_trackencodings (demuxer_t *demuxer, mkv_track_t *track)
641 stream_t *s = demuxer->stream;
642 mkv_content_encoding_t *ce, e;
643 uint64_t len, length, l;
644 int il, n;
646 ce = malloc (sizeof (*ce));
647 n = 0;
649 len = length = ebml_read_length (s, &il);
650 len += il;
651 while (length > 0)
653 switch (ebml_read_id (s, &il))
655 case MATROSKA_ID_CONTENTENCODING:
657 uint64_t len;
658 int i;
660 memset (&e, 0, sizeof (e));
661 e.scope = 1;
663 len = ebml_read_length (s, &i);
664 l = len + i;
666 while (len > 0)
668 uint64_t num, l;
669 int il;
671 switch (ebml_read_id (s, &il))
673 case MATROSKA_ID_CONTENTENCODINGORDER:
674 num = ebml_read_uint (s, &l);
675 if (num == EBML_UINT_INVALID)
676 goto err_out;
677 e.order = num;
678 break;
680 case MATROSKA_ID_CONTENTENCODINGSCOPE:
681 num = ebml_read_uint (s, &l);
682 if (num == EBML_UINT_INVALID)
683 goto err_out;
684 e.scope = num;
685 break;
687 case MATROSKA_ID_CONTENTENCODINGTYPE:
688 num = ebml_read_uint (s, &l);
689 if (num == EBML_UINT_INVALID)
690 goto err_out;
691 e.type = num;
692 break;
694 case MATROSKA_ID_CONTENTCOMPRESSION:
696 uint64_t le;
698 le = ebml_read_length (s, &i);
699 l = le + i;
701 while (le > 0)
703 uint64_t l;
704 int il;
706 switch (ebml_read_id (s, &il))
708 case MATROSKA_ID_CONTENTCOMPALGO:
709 num = ebml_read_uint (s, &l);
710 if (num == EBML_UINT_INVALID)
711 goto err_out;
712 e.comp_algo = num;
713 break;
715 case MATROSKA_ID_CONTENTCOMPSETTINGS:
716 l = ebml_read_length (s, &i);
717 e.comp_settings = malloc (l);
718 stream_read (s, e.comp_settings, l);
719 e.comp_settings_len = l;
720 l += i;
721 break;
723 default:
724 ebml_read_skip (s, &l);
725 break;
727 le -= l + il;
730 if (e.type == 1)
732 mp_msg(MSGT_DEMUX, MSGL_WARN,
733 MSGTR_MPDEMUX_MKV_TrackEncrypted, track->tnum);
735 else if (e.type != 0)
737 mp_msg(MSGT_DEMUX, MSGL_WARN,
738 MSGTR_MPDEMUX_MKV_UnknownContentEncoding, track->tnum);
741 if (e.comp_algo != 0 && e.comp_algo != 2)
743 mp_msg (MSGT_DEMUX, MSGL_WARN,
744 MSGTR_MPDEMUX_MKV_UnknownCompression,
745 track->tnum, e.comp_algo);
747 #ifndef HAVE_ZLIB
748 else if (e.comp_algo == 0)
750 mp_msg (MSGT_DEMUX, MSGL_WARN,
751 MSGTR_MPDEMUX_MKV_ZlibCompressionUnsupported,
752 track->tnum);
754 #endif
756 break;
759 default:
760 ebml_read_skip (s, &l);
761 break;
763 len -= l + il;
765 for (i=0; i<n; i++)
766 if (e.order <= ce[i].order)
767 break;
768 ce = realloc (ce, (n+1) *sizeof (*ce));
769 memmove (ce+i+1, ce+i, (n-i) * sizeof (*ce));
770 memcpy (ce+i, &e, sizeof (e));
771 n++;
772 break;
775 default:
776 ebml_read_skip (s, &l);
777 break;
780 length -= l + il;
783 track->encodings = ce;
784 track->num_encodings = n;
785 return len;
787 err_out:
788 demux_mkv_free_encodings(ce, n);
789 return 0;
792 static int
793 demux_mkv_read_trackaudio (demuxer_t *demuxer, mkv_track_t *track)
795 stream_t *s = demuxer->stream;
796 uint64_t len, length, l;
797 int il;
799 track->a_sfreq = 8000.0;
800 track->a_channels = 1;
802 len = length = ebml_read_length (s, &il);
803 len += il;
804 while (length > 0)
806 switch (ebml_read_id (s, &il))
808 case MATROSKA_ID_AUDIOSAMPLINGFREQ:
810 long double num = ebml_read_float (s, &l);
811 if (num == EBML_FLOAT_INVALID)
812 return 0;
813 track->a_sfreq = num;
814 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Sampling frequency: %f\n",
815 track->a_sfreq);
816 break;
819 case MATROSKA_ID_AUDIOBITDEPTH:
821 uint64_t num = ebml_read_uint (s, &l);
822 if (num == EBML_UINT_INVALID)
823 return 0;
824 track->a_bps = num;
825 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Bit depth: %u\n",
826 track->a_bps);
827 break;
830 case MATROSKA_ID_AUDIOCHANNELS:
832 uint64_t num = ebml_read_uint (s, &l);
833 if (num == EBML_UINT_INVALID)
834 return 0;
835 track->a_channels = num;
836 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Channels: %u\n",
837 track->a_channels);
838 break;
841 default:
842 ebml_read_skip (s, &l);
843 break;
845 length -= l + il;
847 return len;
850 static int
851 demux_mkv_read_trackvideo (demuxer_t *demuxer, mkv_track_t *track)
853 stream_t *s = demuxer->stream;
854 uint64_t len, length, l;
855 int il;
857 len = length = ebml_read_length (s, &il);
858 len += il;
859 while (length > 0)
861 switch (ebml_read_id (s, &il))
863 case MATROSKA_ID_VIDEOFRAMERATE:
865 long double num = ebml_read_float (s, &l);
866 if (num == EBML_FLOAT_INVALID)
867 return 0;
868 track->v_frate = num;
869 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Frame rate: %f\n",
870 track->v_frate);
871 if (track->v_frate > 0)
872 track->default_duration = 1 / track->v_frate;
873 break;
876 case MATROSKA_ID_VIDEODISPLAYWIDTH:
878 uint64_t num = ebml_read_uint (s, &l);
879 if (num == EBML_UINT_INVALID)
880 return 0;
881 track->v_dwidth = num;
882 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Display width: %u\n",
883 track->v_dwidth);
884 break;
887 case MATROSKA_ID_VIDEODISPLAYHEIGHT:
889 uint64_t num = ebml_read_uint (s, &l);
890 if (num == EBML_UINT_INVALID)
891 return 0;
892 track->v_dheight = num;
893 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Display height: %u\n",
894 track->v_dheight);
895 break;
898 case MATROSKA_ID_VIDEOPIXELWIDTH:
900 uint64_t num = ebml_read_uint (s, &l);
901 if (num == EBML_UINT_INVALID)
902 return 0;
903 track->v_width = num;
904 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel width: %u\n",
905 track->v_width);
906 break;
909 case MATROSKA_ID_VIDEOPIXELHEIGHT:
911 uint64_t num = ebml_read_uint (s, &l);
912 if (num == EBML_UINT_INVALID)
913 return 0;
914 track->v_height = num;
915 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel height: %u\n",
916 track->v_height);
917 break;
920 default:
921 ebml_read_skip (s, &l);
922 break;
924 length -= l + il;
926 return len;
930 * \brief free any data associated with given track
931 * \param track track of which to free data
933 static void
934 demux_mkv_free_trackentry(mkv_track_t *track) {
935 if (track->name)
936 free (track->name);
937 if (track->codec_id)
938 free (track->codec_id);
939 if (track->language)
940 free (track->language);
941 if (track->private_data)
942 free (track->private_data);
943 if (track->audio_buf)
944 free (track->audio_buf);
945 if (track->audio_timestamp)
946 free (track->audio_timestamp);
947 demux_mkv_free_encodings(track->encodings, track->num_encodings);
948 free(track);
951 static int
952 demux_mkv_read_trackentry (demuxer_t *demuxer)
954 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
955 stream_t *s = demuxer->stream;
956 mkv_track_t *track;
957 uint64_t len, length, l;
958 int il;
960 track = calloc (1, sizeof (*track));
961 /* set default values */
962 track->default_track = 1;
963 track->name = 0;
964 track->language = strdup("eng");
966 len = length = ebml_read_length (s, &il);
967 len += il;
968 while (length > 0)
970 switch (ebml_read_id (s, &il))
972 case MATROSKA_ID_TRACKNUMBER:
974 uint64_t num = ebml_read_uint (s, &l);
975 if (num == EBML_UINT_INVALID)
976 goto err_out;
977 track->tnum = num;
978 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Track number: %u\n",
979 track->tnum);
980 break;
983 case MATROSKA_ID_TRACKNAME:
985 track->name = ebml_read_utf8 (s, &l);
986 if (track->name == NULL)
987 goto err_out;
988 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Name: %s\n",
989 track->name);
990 break;
993 case MATROSKA_ID_TRACKTYPE:
995 uint64_t num = ebml_read_uint (s, &l);
996 if (num == EBML_UINT_INVALID)
997 return 0;
998 track->type = num;
999 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Track type: ");
1000 switch (track->type)
1002 case MATROSKA_TRACK_AUDIO:
1003 mp_msg (MSGT_DEMUX, MSGL_V, "Audio\n");
1004 break;
1005 case MATROSKA_TRACK_VIDEO:
1006 mp_msg (MSGT_DEMUX, MSGL_V, "Video\n");
1007 break;
1008 case MATROSKA_TRACK_SUBTITLE:
1009 mp_msg (MSGT_DEMUX, MSGL_V, "Subtitle\n");
1010 break;
1011 default:
1012 mp_msg (MSGT_DEMUX, MSGL_V, "unknown\n");
1013 break;
1015 break;
1018 case MATROSKA_ID_TRACKAUDIO:
1019 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Audio track\n");
1020 l = demux_mkv_read_trackaudio (demuxer, track);
1021 if (l == 0)
1022 goto err_out;
1023 break;
1025 case MATROSKA_ID_TRACKVIDEO:
1026 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Video track\n");
1027 l = demux_mkv_read_trackvideo (demuxer, track);
1028 if (l == 0)
1029 goto err_out;
1030 break;
1032 case MATROSKA_ID_CODECID:
1033 track->codec_id = ebml_read_ascii (s, &l);
1034 if (track->codec_id == NULL)
1035 goto err_out;
1036 if (!strcmp (track->codec_id, MKV_V_MSCOMP) ||
1037 !strcmp (track->codec_id, MKV_A_ACM))
1038 track->ms_compat = 1;
1039 else if (!strcmp (track->codec_id, MKV_S_VOBSUB))
1040 track->subtitle_type = MATROSKA_SUBTYPE_VOBSUB;
1041 else if (!strcmp (track->codec_id, MKV_S_TEXTSSA)
1042 || !strcmp (track->codec_id, MKV_S_TEXTASS)
1043 || !strcmp (track->codec_id, MKV_S_SSA)
1044 || !strcmp (track->codec_id, MKV_S_ASS))
1046 track->subtitle_type = MATROSKA_SUBTYPE_SSA;
1048 else if (!strcmp (track->codec_id, MKV_S_TEXTASCII))
1049 track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
1050 if (!strcmp (track->codec_id, MKV_S_TEXTUTF8))
1052 track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
1054 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Codec ID: %s\n",
1055 track->codec_id);
1056 break;
1058 case MATROSKA_ID_CODECPRIVATE:
1060 int x;
1061 uint64_t num = ebml_read_length (s, &x);
1062 // audit: cheap guard against overflows later..
1063 if (num > SIZE_MAX - 1000) return 0;
1064 l = x + num;
1065 track->private_data = malloc (num + LZO_INPUT_PADDING);
1066 if (stream_read(s, track->private_data, num) != (int) num)
1067 goto err_out;
1068 track->private_size = num;
1069 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + CodecPrivate, length "
1070 "%u\n", track->private_size);
1071 break;
1074 case MATROSKA_ID_TRACKLANGUAGE:
1075 free(track->language);
1076 track->language = ebml_read_utf8 (s, &l);
1077 if (track->language == NULL)
1078 goto err_out;
1079 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Language: %s\n",
1080 track->language);
1081 break;
1083 case MATROSKA_ID_TRACKFLAGDEFAULT:
1085 uint64_t num = ebml_read_uint (s, &l);
1086 if (num == EBML_UINT_INVALID)
1087 goto err_out;
1088 track->default_track = num;
1089 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Default flag: %u\n",
1090 track->default_track);
1091 break;
1094 case MATROSKA_ID_TRACKDEFAULTDURATION:
1096 uint64_t num = ebml_read_uint (s, &l);
1097 if (num == EBML_UINT_INVALID)
1098 goto err_out;
1099 if (num == 0)
1100 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Default duration: 0");
1101 else
1103 track->v_frate = 1000000000.0 / num;
1104 track->default_duration = num / 1000000000.0;
1105 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Default duration: "
1106 "%.3fms ( = %.3f fps)\n",num/1000000.0,track->v_frate);
1108 break;
1111 case MATROSKA_ID_TRACKENCODINGS:
1112 l = demux_mkv_read_trackencodings (demuxer, track);
1113 if (l == 0)
1114 goto err_out;
1115 break;
1117 default:
1118 ebml_read_skip (s, &l);
1119 break;
1121 length -= l + il;
1124 mkv_d->tracks[mkv_d->num_tracks++] = track;
1125 return len;
1127 err_out:
1128 demux_mkv_free_trackentry(track);
1129 return 0;
1132 static int
1133 demux_mkv_read_tracks (demuxer_t *demuxer)
1135 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1136 stream_t *s = demuxer->stream;
1137 uint64_t length, l;
1138 int il;
1140 mkv_d->tracks = malloc (sizeof (*mkv_d->tracks));
1141 mkv_d->num_tracks = 0;
1143 length = ebml_read_length (s, NULL);
1144 while (length > 0)
1146 switch (ebml_read_id (s, &il))
1148 case MATROSKA_ID_TRACKENTRY:
1149 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + a track...\n");
1150 mkv_d->tracks = realloc (mkv_d->tracks,
1151 (mkv_d->num_tracks+1)
1152 *sizeof (*mkv_d->tracks));
1153 l = demux_mkv_read_trackentry (demuxer);
1154 if (l == 0)
1155 return 1;
1156 break;
1158 default:
1159 ebml_read_skip (s, &l);
1160 break;
1162 length -= l + il;
1164 return 0;
1167 static int
1168 demux_mkv_read_cues (demuxer_t *demuxer)
1170 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1171 stream_t *s = demuxer->stream;
1172 uint64_t length, l, time, track, pos;
1173 off_t off;
1174 int i, il;
1176 if (index_mode == 0) {
1177 ebml_read_skip (s, NULL);
1178 return 0;
1180 off = stream_tell (s);
1181 for (i=0; i<mkv_d->parsed_cues_num; i++)
1182 if (mkv_d->parsed_cues[i] == off)
1184 ebml_read_skip (s, NULL);
1185 return 0;
1187 mkv_d->parsed_cues = realloc (mkv_d->parsed_cues,
1188 (mkv_d->parsed_cues_num+1)
1189 * sizeof (off_t));
1190 mkv_d->parsed_cues[mkv_d->parsed_cues_num++] = off;
1192 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing cues ] -----------\n");
1193 length = ebml_read_length (s, NULL);
1195 while (length > 0)
1197 time = track = pos = EBML_UINT_INVALID;
1199 switch (ebml_read_id (s, &il))
1201 case MATROSKA_ID_POINTENTRY:
1203 uint64_t len;
1205 len = ebml_read_length (s, &i);
1206 l = len + i;
1208 while (len > 0)
1210 uint64_t l;
1211 int il;
1213 switch (ebml_read_id (s, &il))
1215 case MATROSKA_ID_CUETIME:
1216 time = ebml_read_uint (s, &l);
1217 break;
1219 case MATROSKA_ID_CUETRACKPOSITION:
1221 uint64_t le;
1223 le = ebml_read_length (s, &i);
1224 l = le + i;
1226 while (le > 0)
1228 uint64_t l;
1229 int il;
1231 switch (ebml_read_id (s, &il))
1233 case MATROSKA_ID_CUETRACK:
1234 track = ebml_read_uint (s, &l);
1235 break;
1237 case MATROSKA_ID_CUECLUSTERPOSITION:
1238 pos = ebml_read_uint (s, &l);
1239 break;
1241 default:
1242 ebml_read_skip (s, &l);
1243 break;
1245 le -= l + il;
1247 break;
1250 default:
1251 ebml_read_skip (s, &l);
1252 break;
1254 len -= l + il;
1256 break;
1259 default:
1260 ebml_read_skip (s, &l);
1261 break;
1264 length -= l + il;
1266 if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID
1267 && pos != EBML_UINT_INVALID)
1269 grow_array(&mkv_d->indexes, mkv_d->num_indexes, sizeof(mkv_index_t));
1270 mkv_d->indexes[mkv_d->num_indexes].tnum = track;
1271 mkv_d->indexes[mkv_d->num_indexes].timecode = time;
1272 mkv_d->indexes[mkv_d->num_indexes].filepos =mkv_d->segment_start+pos;
1273 mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] |+ found cue point "
1274 "for track %"PRIu64": timecode %"PRIu64", filepos: %"PRIu64"\n",
1275 track, time, mkv_d->segment_start + pos);
1276 mkv_d->num_indexes++;
1280 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing cues ] -----------\n");
1281 return 0;
1284 static int
1285 demux_mkv_read_chapters (demuxer_t *demuxer)
1287 stream_t *s = demuxer->stream;
1288 uint64_t length, l;
1289 int il;
1291 if (demuxer->chapters)
1293 ebml_read_skip (s, NULL);
1294 return 0;
1297 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing chapters ] ---------\n");
1298 length = ebml_read_length (s, NULL);
1300 while (length > 0)
1302 switch (ebml_read_id (s, &il))
1304 case MATROSKA_ID_EDITIONENTRY:
1306 uint64_t len;
1307 int i;
1309 len = ebml_read_length (s, &i);
1310 l = len + i;
1312 while (len > 0)
1314 uint64_t l;
1315 int il;
1317 switch (ebml_read_id (s, &il))
1319 case MATROSKA_ID_CHAPTERATOM:
1321 uint64_t len, start=0, end=0;
1322 char* name = 0;
1323 int i;
1324 int cid;
1326 len = ebml_read_length (s, &i);
1327 l = len + i;
1329 while (len > 0)
1331 uint64_t l;
1332 int il;
1334 switch (ebml_read_id (s, &il))
1336 case MATROSKA_ID_CHAPTERTIMESTART:
1337 start = ebml_read_uint (s, &l) / 1000000;
1338 break;
1340 case MATROSKA_ID_CHAPTERTIMEEND:
1341 end = ebml_read_uint (s, &l) / 1000000;
1342 break;
1344 case MATROSKA_ID_CHAPTERDISPLAY:
1346 uint64_t len;
1347 int i;
1349 len = ebml_read_length (s, &i);
1350 l = len + i;
1351 while (len > 0)
1353 uint64_t l;
1354 int il;
1356 switch (ebml_read_id (s, &il))
1358 case MATROSKA_ID_CHAPSTRING:
1359 name = ebml_read_utf8 (s, &l);
1360 break;
1361 default:
1362 ebml_read_skip (s, &l);
1363 break;
1365 len -= l + il;
1368 break;
1370 default:
1371 ebml_read_skip (s, &l);
1372 break;
1374 len -= l + il;
1377 if (!name)
1378 name = strdup("(unnamed)");
1380 cid = demuxer_add_chapter(demuxer, name, start, end);
1382 mp_msg(MSGT_DEMUX, MSGL_V,
1383 "[mkv] Chapter %u from %02d:%02d:%02d."
1384 "%03d to %02d:%02d:%02d.%03d, %s\n",
1385 cid,
1386 (int) (start / 60 / 60 / 1000),
1387 (int) ((start / 60 / 1000) % 60),
1388 (int) ((start / 1000) % 60),
1389 (int) (start % 1000),
1390 (int) (end / 60 / 60 / 1000),
1391 (int) ((end / 60 / 1000) % 60),
1392 (int) ((end / 1000) % 60),
1393 (int) (end % 1000), name);
1395 free(name);
1396 break;
1399 default:
1400 ebml_read_skip (s, &l);
1401 break;
1403 len -= l + il;
1405 break;
1408 default:
1409 ebml_read_skip (s, &l);
1410 break;
1413 length -= l + il;
1416 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing chapters ] ---------\n");
1417 return 0;
1420 static int
1421 demux_mkv_read_tags (demuxer_t *demuxer)
1423 ebml_read_skip (demuxer->stream, NULL);
1424 return 0;
1427 static int
1428 demux_mkv_read_attachments (demuxer_t *demuxer)
1430 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1431 stream_t *s = demuxer->stream;
1432 uint64_t length, l;
1433 int il;
1435 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing attachments ] ---------\n");
1436 length = ebml_read_length (s, NULL);
1438 while (length > 0)
1440 switch (ebml_read_id (s, &il))
1442 case MATROSKA_ID_ATTACHEDFILE:
1444 uint64_t len;
1445 int i;
1446 char* name = NULL;
1447 char* mime = NULL;
1448 char* data = NULL;
1449 int data_size = 0;
1451 len = ebml_read_length (s, &i);
1452 l = len + i;
1454 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + an attachment...\n");
1456 while (len > 0)
1458 uint64_t l;
1459 int il;
1461 switch (ebml_read_id (s, &il))
1463 case MATROSKA_ID_FILENAME:
1464 name = ebml_read_utf8 (s, &l);
1465 if (name == NULL)
1466 return 0;
1467 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + FileName: %s\n",
1468 name);
1469 break;
1471 case MATROSKA_ID_FILEMIMETYPE:
1472 mime = ebml_read_ascii (s, &l);
1473 if (mime == NULL)
1474 return 0;
1475 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + FileMimeType: %s\n",
1476 mime);
1477 break;
1479 case MATROSKA_ID_FILEDATA:
1481 int x;
1482 uint64_t num = ebml_read_length (s, &x);
1483 l = x + num;
1484 free(data);
1485 data = malloc (num);
1486 if (stream_read(s, data, num) != (int) num)
1488 free(data);
1489 return 0;
1491 data_size = num;
1492 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + FileData, length "
1493 "%u\n", data_size);
1494 break;
1497 default:
1498 ebml_read_skip (s, &l);
1499 break;
1501 len -= l + il;
1504 demuxer_add_attachment(demuxer, name, mime, data, data_size);
1505 mp_msg(MSGT_DEMUX, MSGL_V,
1506 "[mkv] Attachment: %s, %s, %u bytes\n",
1507 name, mime, data_size);
1508 break;
1511 default:
1512 ebml_read_skip (s, &l);
1513 break;
1515 length -= l + il;
1518 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing attachments ] ---------\n");
1519 return 0;
1522 static int
1523 demux_mkv_read_seekhead (demuxer_t *demuxer)
1525 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1526 stream_t *s = demuxer->stream;
1527 uint64_t length, l, seek_pos, saved_pos, num;
1528 uint32_t seek_id;
1529 int i, il, res = 0;
1530 off_t off;
1532 off = stream_tell (s);
1533 for (i=0; i<mkv_d->parsed_seekhead_num; i++)
1534 if (mkv_d->parsed_seekhead[i] == off)
1536 ebml_read_skip (s, NULL);
1537 return 0;
1539 mkv_d->parsed_seekhead = realloc (mkv_d->parsed_seekhead,
1540 (mkv_d->parsed_seekhead_num+1)
1541 * sizeof (off_t));
1542 mkv_d->parsed_seekhead[mkv_d->parsed_seekhead_num++] = off;
1544 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing seek head ] ---------\n");
1545 length = ebml_read_length (s, NULL);
1546 /* off now holds the position of the next element after the seek head. */
1547 off = stream_tell (s) + length;
1548 while (length > 0 && !res)
1551 seek_id = 0;
1552 seek_pos = EBML_UINT_INVALID;
1554 switch (ebml_read_id (s, &il))
1556 case MATROSKA_ID_SEEKENTRY:
1558 uint64_t len;
1560 len = ebml_read_length (s, &i);
1561 l = len + i;
1563 while (len > 0)
1565 uint64_t l;
1566 int il;
1568 switch (ebml_read_id (s, &il))
1570 case MATROSKA_ID_SEEKID:
1571 num = ebml_read_uint (s, &l);
1572 if (num != EBML_UINT_INVALID)
1573 seek_id = num;
1574 break;
1576 case MATROSKA_ID_SEEKPOSITION:
1577 seek_pos = ebml_read_uint (s, &l);
1578 break;
1580 default:
1581 ebml_read_skip (s, &l);
1582 break;
1584 len -= l + il;
1587 break;
1590 default:
1591 ebml_read_skip (s, &l);
1592 break;
1594 length -= l + il;
1596 if (seek_id == 0 || seek_id == MATROSKA_ID_CLUSTER
1597 || seek_pos == EBML_UINT_INVALID ||
1598 ((mkv_d->segment_start + seek_pos) >= (uint64_t)demuxer->movi_end))
1599 continue;
1601 saved_pos = stream_tell (s);
1602 if (!stream_seek (s, mkv_d->segment_start + seek_pos))
1603 res = 1;
1604 else
1606 if (ebml_read_id (s, &il) != seek_id)
1607 res = 1;
1608 else
1609 switch (seek_id)
1611 case MATROSKA_ID_CUES:
1612 if (demux_mkv_read_cues (demuxer))
1613 res = 1;
1614 break;
1616 case MATROSKA_ID_TAGS:
1617 if (demux_mkv_read_tags (demuxer))
1618 res = 1;
1619 break;
1621 case MATROSKA_ID_SEEKHEAD:
1622 if (demux_mkv_read_seekhead (demuxer))
1623 res = 1;
1624 break;
1626 case MATROSKA_ID_CHAPTERS:
1627 if (demux_mkv_read_chapters (demuxer))
1628 res = 1;
1629 break;
1633 stream_seek (s, saved_pos);
1635 if (res)
1637 /* If there was an error then try to skip this seek head. */
1638 if (stream_seek (s, off))
1639 res = 0;
1641 else
1642 if (length > 0)
1643 stream_seek (s, stream_tell (s) + length);
1644 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing seek head ] ---------\n");
1645 return res;
1648 static int
1649 demux_mkv_open_video (demuxer_t *demuxer, mkv_track_t *track, int vid);
1650 static int
1651 demux_mkv_open_audio (demuxer_t *demuxer, mkv_track_t *track, int aid);
1652 static int
1653 demux_mkv_open_sub (demuxer_t *demuxer, mkv_track_t *track, int sid);
1655 static void
1656 display_create_tracks (demuxer_t *demuxer)
1658 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)demuxer->priv;
1659 int i, vid=0, aid=0, sid=0;
1661 for (i=0; i<mkv_d->num_tracks; i++)
1663 char *type = "unknown", str[32];
1664 *str = '\0';
1665 switch (mkv_d->tracks[i]->type)
1667 case MATROSKA_TRACK_VIDEO:
1668 type = "video";
1669 demux_mkv_open_video(demuxer, mkv_d->tracks[i], vid);
1670 if (mkv_d->tracks[i]->name)
1671 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", vid, mkv_d->tracks[i]->name);
1672 sprintf (str, "-vid %u", vid++);
1673 break;
1674 case MATROSKA_TRACK_AUDIO:
1675 type = "audio";
1676 demux_mkv_open_audio(demuxer, mkv_d->tracks[i], aid);
1677 if (mkv_d->tracks[i]->name)
1678 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", aid, mkv_d->tracks[i]->name);
1679 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", aid, mkv_d->tracks[i]->language);
1680 sprintf (str, "-aid %u, -alang %.5s",aid++,mkv_d->tracks[i]->language);
1681 break;
1682 case MATROSKA_TRACK_SUBTITLE:
1683 type = "subtitles";
1684 demux_mkv_open_sub(demuxer, mkv_d->tracks[i], sid);
1685 if (mkv_d->tracks[i]->name)
1686 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", sid, mkv_d->tracks[i]->name);
1687 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, mkv_d->tracks[i]->language);
1688 sprintf (str, "-sid %u, -slang %.5s",sid++,mkv_d->tracks[i]->language);
1689 break;
1691 if (mkv_d->tracks[i]->name)
1692 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackIDName,
1693 mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, mkv_d->tracks[i]->name, str);
1694 else
1695 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackID,
1696 mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, str);
1700 typedef struct {
1701 char *id;
1702 int fourcc;
1703 int extradata;
1704 } videocodec_info_t;
1706 static const videocodec_info_t vinfo[] = {
1707 { MKV_V_MPEG1, mmioFOURCC('m', 'p', 'g', '1'), 0 },
1708 { MKV_V_MPEG2, mmioFOURCC('m', 'p', 'g', '2'), 0 },
1709 { MKV_V_MPEG4_SP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1710 { MKV_V_MPEG4_ASP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1711 { MKV_V_MPEG4_AP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1712 { MKV_V_MPEG4_AVC, mmioFOURCC('a', 'v', 'c', '1'), 1 },
1713 { MKV_V_THEORA, mmioFOURCC('t', 'h', 'e', 'o'), 1 },
1714 { NULL, 0, 0 }
1717 static int
1718 demux_mkv_open_video (demuxer_t *demuxer, mkv_track_t *track, int vid)
1720 BITMAPINFOHEADER *bih;
1721 void *ImageDesc = NULL;
1722 sh_video_t *sh_v;
1724 if (track->ms_compat) /* MS compatibility mode */
1726 BITMAPINFOHEADER *src;
1728 if (track->private_data == NULL
1729 || track->private_size < sizeof (BITMAPINFOHEADER))
1730 return 1;
1732 src = (BITMAPINFOHEADER *) track->private_data;
1733 bih = calloc (1, track->private_size);
1734 bih->biSize = le2me_32 (src->biSize);
1735 bih->biWidth = le2me_32 (src->biWidth);
1736 bih->biHeight = le2me_32 (src->biHeight);
1737 bih->biPlanes = le2me_16 (src->biPlanes);
1738 bih->biBitCount = le2me_16 (src->biBitCount);
1739 bih->biCompression = le2me_32 (src->biCompression);
1740 bih->biSizeImage = le2me_32 (src->biSizeImage);
1741 bih->biXPelsPerMeter = le2me_32 (src->biXPelsPerMeter);
1742 bih->biYPelsPerMeter = le2me_32 (src->biYPelsPerMeter);
1743 bih->biClrUsed = le2me_32 (src->biClrUsed);
1744 bih->biClrImportant = le2me_32 (src->biClrImportant);
1745 memcpy((char *) bih + sizeof (BITMAPINFOHEADER),
1746 (char *) src + sizeof (BITMAPINFOHEADER),
1747 track->private_size - sizeof (BITMAPINFOHEADER));
1749 if (track->v_width == 0)
1750 track->v_width = bih->biWidth;
1751 if (track->v_height == 0)
1752 track->v_height = bih->biHeight;
1754 else
1756 bih = calloc (1, sizeof (BITMAPINFOHEADER));
1757 bih->biSize = sizeof (BITMAPINFOHEADER);
1758 bih->biWidth = track->v_width;
1759 bih->biHeight = track->v_height;
1760 bih->biBitCount = 24;
1761 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
1763 if (track->private_size >= RVPROPERTIES_SIZE
1764 && (!strcmp (track->codec_id, MKV_V_REALV10)
1765 || !strcmp (track->codec_id, MKV_V_REALV20)
1766 || !strcmp (track->codec_id, MKV_V_REALV30)
1767 || !strcmp (track->codec_id, MKV_V_REALV40)))
1769 unsigned char *dst, *src;
1770 uint32_t type2;
1771 unsigned int cnt;
1773 src = track->private_data + RVPROPERTIES_SIZE;
1775 cnt = track->private_size - RVPROPERTIES_SIZE;
1776 bih = realloc(bih, sizeof (BITMAPINFOHEADER)+8+cnt);
1777 bih->biSize = 48+cnt;
1778 bih->biPlanes = 1;
1779 type2 = AV_RB32(src - 4);
1780 if (type2 == 0x10003000 || type2 == 0x10003001)
1781 bih->biCompression=mmioFOURCC('R','V','1','3');
1782 else
1783 bih->biCompression=mmioFOURCC('R','V',track->codec_id[9],'0');
1784 dst = (unsigned char *) (bih + 1);
1785 // copy type1 and type2 info from rv properties
1786 memcpy(dst, src - 8, 8);
1787 stream_read(demuxer->stream, dst+8, cnt);
1788 track->realmedia = 1;
1790 #ifdef USE_QTX_CODECS
1792 else if (track->private_size >= sizeof (ImageDescription)
1793 && !strcmp(track->codec_id, MKV_V_QUICKTIME))
1795 ImageDescriptionPtr idesc;
1797 idesc = (ImageDescriptionPtr) track->private_data;
1798 idesc->idSize = be2me_32 (idesc->idSize);
1799 idesc->cType = be2me_32 (idesc->cType);
1800 idesc->version = be2me_16 (idesc->version);
1801 idesc->revisionLevel = be2me_16 (idesc->revisionLevel);
1802 idesc->vendor = be2me_32 (idesc->vendor);
1803 idesc->temporalQuality = be2me_32 (idesc->temporalQuality);
1804 idesc->spatialQuality = be2me_32 (idesc->spatialQuality);
1805 idesc->width = be2me_16 (idesc->width);
1806 idesc->height = be2me_16 (idesc->height);
1807 idesc->hRes = be2me_32 (idesc->hRes);
1808 idesc->vRes = be2me_32 (idesc->vRes);
1809 idesc->dataSize = be2me_32 (idesc->dataSize);
1810 idesc->frameCount = be2me_16 (idesc->frameCount);
1811 idesc->depth = be2me_16 (idesc->depth);
1812 idesc->clutID = be2me_16 (idesc->clutID);
1813 bih->biPlanes = 1;
1814 bih->biCompression = idesc->cType;
1815 ImageDesc = idesc;
1816 #endif /* USE_QTX_CODECS */
1819 else
1821 const videocodec_info_t *vi = vinfo;
1822 while (vi->id && strcmp(vi->id, track->codec_id)) vi++;
1823 bih->biCompression = vi->fourcc;
1824 if (vi->extradata && track->private_data && (track->private_size > 0))
1826 bih->biSize += track->private_size;
1827 bih = realloc (bih, bih->biSize);
1828 memcpy (bih + 1, track->private_data, track->private_size);
1830 track->reorder_timecodes = !correct_pts;
1831 if (!vi->id) {
1832 mp_msg (MSGT_DEMUX,MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownCodecID,
1833 track->codec_id, track->tnum);
1834 free(bih);
1835 return 1;
1840 sh_v = new_sh_video_vid (demuxer, track->tnum, vid);
1841 sh_v->bih = bih;
1842 sh_v->format = sh_v->bih->biCompression;
1843 if (track->v_frate == 0.0)
1844 track->v_frate = 25.0;
1845 sh_v->fps = track->v_frate;
1846 sh_v->frametime = 1 / track->v_frate;
1847 sh_v->aspect = 0;
1848 if (!track->realmedia)
1850 sh_v->disp_w = track->v_width;
1851 sh_v->disp_h = track->v_height;
1852 if (track->v_dheight)
1853 sh_v->aspect = (float)track->v_dwidth / (float)track->v_dheight;
1855 else
1857 // vd_realvid.c will set aspect to disp_w/disp_h and rederive
1858 // disp_w and disp_h from the RealVideo stream contents returned
1859 // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
1860 // the Matroska file then it has already been set to PixelWidth/Height
1861 // by check_track_information.
1862 sh_v->disp_w = track->v_dwidth;
1863 sh_v->disp_h = track->v_dheight;
1865 sh_v->ImageDesc = ImageDesc;
1866 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] Aspect: %f\n", sh_v->aspect);
1868 sh_v->ds = demuxer->video;
1869 return 0;
1872 static int
1873 demux_mkv_open_audio (demuxer_t *demuxer, mkv_track_t *track, int aid)
1875 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1876 sh_audio_t *sh_a = new_sh_audio_aid(demuxer, track->tnum, aid);
1877 demux_packet_t *dp;
1878 if(!sh_a) return 1;
1879 mkv_d->audio_tracks[mkv_d->last_aid] = track->tnum;
1881 sh_a->ds = demuxer->audio;
1882 sh_a->wf = malloc (sizeof (WAVEFORMATEX));
1883 if (track->ms_compat && (track->private_size >= sizeof(WAVEFORMATEX)))
1885 WAVEFORMATEX *wf = (WAVEFORMATEX *)track->private_data;
1886 sh_a->wf = realloc(sh_a->wf, track->private_size);
1887 sh_a->wf->wFormatTag = le2me_16 (wf->wFormatTag);
1888 sh_a->wf->nChannels = le2me_16 (wf->nChannels);
1889 sh_a->wf->nSamplesPerSec = le2me_32 (wf->nSamplesPerSec);
1890 sh_a->wf->nAvgBytesPerSec = le2me_32 (wf->nAvgBytesPerSec);
1891 sh_a->wf->nBlockAlign = le2me_16 (wf->nBlockAlign);
1892 sh_a->wf->wBitsPerSample = le2me_16 (wf->wBitsPerSample);
1893 sh_a->wf->cbSize = track->private_size - sizeof(WAVEFORMATEX);
1894 memcpy(sh_a->wf + 1, wf + 1, track->private_size - sizeof(WAVEFORMATEX));
1895 if (track->a_sfreq == 0.0)
1896 track->a_sfreq = sh_a->wf->nSamplesPerSec;
1897 if (track->a_channels == 0)
1898 track->a_channels = sh_a->wf->nChannels;
1899 if (track->a_bps == 0)
1900 track->a_bps = sh_a->wf->wBitsPerSample;
1901 track->a_formattag = sh_a->wf->wFormatTag;
1903 else
1905 memset(sh_a->wf, 0, sizeof (WAVEFORMATEX));
1906 if (!strcmp(track->codec_id, MKV_A_MP3) ||
1907 !strcmp(track->codec_id, MKV_A_MP2))
1908 track->a_formattag = 0x0055;
1909 else if (!strncmp(track->codec_id, MKV_A_AC3, strlen(MKV_A_AC3)))
1910 track->a_formattag = 0x2000;
1911 else if (!strcmp(track->codec_id, MKV_A_DTS))
1912 track->a_formattag = 0x2001;
1913 else if (!strcmp(track->codec_id, MKV_A_PCM) ||
1914 !strcmp(track->codec_id, MKV_A_PCM_BE))
1915 track->a_formattag = 0x0001;
1916 else if (!strcmp(track->codec_id, MKV_A_AAC_2MAIN) ||
1917 !strncmp(track->codec_id, MKV_A_AAC_2LC,
1918 strlen(MKV_A_AAC_2LC)) ||
1919 !strcmp(track->codec_id, MKV_A_AAC_2SSR) ||
1920 !strcmp(track->codec_id, MKV_A_AAC_4MAIN) ||
1921 !strncmp(track->codec_id, MKV_A_AAC_4LC,
1922 strlen(MKV_A_AAC_4LC)) ||
1923 !strcmp(track->codec_id, MKV_A_AAC_4SSR) ||
1924 !strcmp(track->codec_id, MKV_A_AAC_4LTP) ||
1925 !strcmp(track->codec_id, MKV_A_AAC))
1926 track->a_formattag = mmioFOURCC('M', 'P', '4', 'A');
1927 else if (!strcmp(track->codec_id, MKV_A_VORBIS))
1929 if (track->private_data == NULL)
1930 return 1;
1931 track->a_formattag = mmioFOURCC('v', 'r', 'b', 's');
1933 else if (!strcmp(track->codec_id, MKV_A_QDMC))
1934 track->a_formattag = mmioFOURCC('Q', 'D', 'M', 'C');
1935 else if (!strcmp(track->codec_id, MKV_A_QDMC2))
1936 track->a_formattag = mmioFOURCC('Q', 'D', 'M', '2');
1937 else if (!strcmp(track->codec_id, MKV_A_WAVPACK))
1938 track->a_formattag = mmioFOURCC('W', 'V', 'P', 'K');
1939 else if (!strcmp(track->codec_id, MKV_A_FLAC))
1941 if (track->private_data == NULL || track->private_size == 0)
1943 mp_msg (MSGT_DEMUX, MSGL_WARN,
1944 MSGTR_MPDEMUX_MKV_FlacTrackDoesNotContainValidHeaders);
1945 return 1;
1947 track->a_formattag = mmioFOURCC ('f', 'L', 'a', 'C');
1949 else if (track->private_size >= RAPROPERTIES4_SIZE)
1951 if (!strcmp(track->codec_id, MKV_A_REAL28))
1952 track->a_formattag = mmioFOURCC('2', '8', '_', '8');
1953 else if (!strcmp(track->codec_id, MKV_A_REALATRC))
1954 track->a_formattag = mmioFOURCC('a', 't', 'r', 'c');
1955 else if (!strcmp(track->codec_id, MKV_A_REALCOOK))
1956 track->a_formattag = mmioFOURCC('c', 'o', 'o', 'k');
1957 else if (!strcmp(track->codec_id, MKV_A_REALDNET))
1958 track->a_formattag = mmioFOURCC('d', 'n', 'e', 't');
1959 else if (!strcmp(track->codec_id, MKV_A_REALSIPR))
1960 track->a_formattag = mmioFOURCC('s', 'i', 'p', 'r');
1962 else
1964 mp_msg (MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownAudioCodec,
1965 track->codec_id, track->tnum);
1966 free_sh_audio(demuxer, track->tnum);
1967 return 1;
1971 sh_a->format = track->a_formattag;
1972 sh_a->wf->wFormatTag = track->a_formattag;
1973 sh_a->channels = track->a_channels;
1974 sh_a->wf->nChannels = track->a_channels;
1975 sh_a->samplerate = (uint32_t) track->a_sfreq;
1976 sh_a->wf->nSamplesPerSec = (uint32_t) track->a_sfreq;
1977 if (track->a_bps == 0)
1979 sh_a->samplesize = 2;
1980 sh_a->wf->wBitsPerSample = 16;
1982 else
1984 sh_a->samplesize = track->a_bps / 8;
1985 sh_a->wf->wBitsPerSample = track->a_bps;
1987 if (track->a_formattag == 0x0055) /* MP3 || MP2 */
1989 sh_a->wf->nAvgBytesPerSec = 16000;
1990 sh_a->wf->nBlockAlign = 1152;
1992 else if ((track->a_formattag == 0x2000) || /* AC3 */
1993 (track->a_formattag == 0x2001)) /* DTS */
1995 free(sh_a->wf);
1996 sh_a->wf = NULL;
1998 else if (track->a_formattag == 0x0001) /* PCM || PCM_BE */
2000 sh_a->wf->nAvgBytesPerSec = sh_a->channels * sh_a->samplerate*2;
2001 sh_a->wf->nBlockAlign = sh_a->wf->nAvgBytesPerSec;
2002 if (!strcmp(track->codec_id, MKV_A_PCM_BE))
2003 sh_a->format = mmioFOURCC('t', 'w', 'o', 's');
2005 else if (!strcmp(track->codec_id, MKV_A_QDMC) ||
2006 !strcmp(track->codec_id, MKV_A_QDMC2))
2008 sh_a->wf->nAvgBytesPerSec = 16000;
2009 sh_a->wf->nBlockAlign = 1486;
2010 track->fix_i_bps = 1;
2011 track->qt_last_a_pts = 0.0;
2012 if (track->private_data != NULL)
2014 sh_a->codecdata=malloc(track->private_size);
2015 memcpy (sh_a->codecdata, track->private_data,
2016 track->private_size);
2017 sh_a->codecdata_len = track->private_size;
2020 else if (track->a_formattag == mmioFOURCC('M', 'P', '4', 'A'))
2022 int profile, srate_idx;
2024 sh_a->wf->nAvgBytesPerSec = 16000;
2025 sh_a->wf->nBlockAlign = 1024;
2027 if (!strcmp (track->codec_id, MKV_A_AAC) &&
2028 (NULL != track->private_data))
2030 sh_a->codecdata=malloc(track->private_size);
2031 memcpy (sh_a->codecdata, track->private_data,
2032 track->private_size);
2033 sh_a->codecdata_len = track->private_size;
2034 return 0;
2037 /* Recreate the 'private data' */
2038 /* which faad2 uses in its initialization */
2039 srate_idx = aac_get_sample_rate_index (sh_a->samplerate);
2040 if (!strncmp (&track->codec_id[12], "MAIN", 4))
2041 profile = 0;
2042 else if (!strncmp (&track->codec_id[12], "LC", 2))
2043 profile = 1;
2044 else if (!strncmp (&track->codec_id[12], "SSR", 3))
2045 profile = 2;
2046 else
2047 profile = 3;
2048 sh_a->codecdata = malloc (5);
2049 sh_a->codecdata[0] = ((profile+1) << 3) | ((srate_idx&0xE) >> 1);
2050 sh_a->codecdata[1] = ((srate_idx&0x1)<<7)|(track->a_channels<<3);
2052 if (strstr(track->codec_id, "SBR") != NULL)
2054 /* HE-AAC (aka SBR AAC) */
2055 sh_a->codecdata_len = 5;
2057 sh_a->samplerate *= 2;
2058 sh_a->wf->nSamplesPerSec *= 2;
2059 srate_idx = aac_get_sample_rate_index(sh_a->samplerate);
2060 sh_a->codecdata[2] = AAC_SYNC_EXTENSION_TYPE >> 3;
2061 sh_a->codecdata[3] = ((AAC_SYNC_EXTENSION_TYPE&0x07)<<5) | 5;
2062 sh_a->codecdata[4] = (1 << 7) | (srate_idx << 3);
2063 track->default_duration = 1024.0 / (sh_a->samplerate / 2);
2065 else
2067 sh_a->codecdata_len = 2;
2068 track->default_duration = 1024.0 / (float)sh_a->samplerate;
2071 else if (track->a_formattag == mmioFOURCC('v', 'r', 'b', 's')) /* VORBIS */
2073 sh_a->wf->cbSize = track->private_size;
2074 sh_a->wf = realloc(sh_a->wf, sizeof(WAVEFORMATEX) + sh_a->wf->cbSize);
2075 memcpy((unsigned char *) (sh_a->wf+1), track->private_data, sh_a->wf->cbSize);
2077 else if (track->private_size >= RAPROPERTIES4_SIZE
2078 && !strncmp (track->codec_id, MKV_A_REALATRC, 7))
2080 /* Common initialization for all RealAudio codecs */
2081 unsigned char *src = track->private_data;
2082 int codecdata_length, version;
2083 int flavor;
2085 sh_a->wf->nAvgBytesPerSec = 0; /* FIXME !? */
2087 version = AV_RB16(src + 4);
2088 flavor = AV_RB16(src + 22);
2089 track->coded_framesize = AV_RB32(src + 24);
2090 track->sub_packet_h = AV_RB16(src + 40);
2091 sh_a->wf->nBlockAlign =
2092 track->audiopk_size = AV_RB16(src + 42);
2093 track->sub_packet_size = AV_RB16(src + 44);
2094 if (version == 4)
2096 src += RAPROPERTIES4_SIZE;
2097 src += src[0] + 1;
2098 src += src[0] + 1;
2100 else
2101 src += RAPROPERTIES5_SIZE;
2103 src += 3;
2104 if (version == 5)
2105 src++;
2106 codecdata_length = AV_RB32(src);
2107 src += 4;
2108 sh_a->wf->cbSize = codecdata_length;
2109 sh_a->wf = realloc (sh_a->wf,
2110 sizeof (WAVEFORMATEX) +
2111 sh_a->wf->cbSize);
2112 memcpy(((char *)(sh_a->wf + 1)), src, codecdata_length);
2114 switch (track->a_formattag) {
2115 case mmioFOURCC('a', 't', 'r', 'c'):
2116 sh_a->wf->nAvgBytesPerSec = atrc_fl2bps[flavor];
2117 sh_a->wf->nBlockAlign = track->sub_packet_size;
2118 track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
2119 track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
2120 break;
2121 case mmioFOURCC('c', 'o', 'o', 'k'):
2122 sh_a->wf->nAvgBytesPerSec = cook_fl2bps[flavor];
2123 sh_a->wf->nBlockAlign = track->sub_packet_size;
2124 track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
2125 track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
2126 break;
2127 case mmioFOURCC('s', 'i', 'p', 'r'):
2128 sh_a->wf->nAvgBytesPerSec = sipr_fl2bps[flavor];
2129 sh_a->wf->nBlockAlign = track->coded_framesize;
2130 track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
2131 track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
2132 break;
2133 case mmioFOURCC('2', '8', '_', '8'):
2134 sh_a->wf->nAvgBytesPerSec = 3600;
2135 sh_a->wf->nBlockAlign = track->coded_framesize;
2136 track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
2137 track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
2138 break;
2141 track->realmedia = 1;
2143 else if (!strcmp(track->codec_id, MKV_A_FLAC) ||
2144 (track->a_formattag == 0xf1ac))
2146 unsigned char *ptr;
2147 int size;
2148 free(sh_a->wf);
2149 sh_a->wf = NULL;
2151 if (track->a_formattag == mmioFOURCC('f', 'L', 'a', 'C'))
2153 ptr = (unsigned char *)track->private_data;
2154 size = track->private_size;
2156 else
2158 sh_a->format = mmioFOURCC('f', 'L', 'a', 'C');
2159 ptr = (unsigned char *) track->private_data
2160 + sizeof (WAVEFORMATEX);
2161 size = track->private_size - sizeof (WAVEFORMATEX);
2163 if (size < 4 || ptr[0] != 'f' || ptr[1] != 'L' ||
2164 ptr[2] != 'a' || ptr[3] != 'C')
2166 dp = new_demux_packet (4);
2167 memcpy (dp->buffer, "fLaC", 4);
2169 else
2171 dp = new_demux_packet (size);
2172 memcpy (dp->buffer, ptr, size);
2174 dp->pts = 0;
2175 dp->flags = 0;
2176 ds_add_packet (demuxer->audio, dp);
2178 else if (track->a_formattag == mmioFOURCC('W', 'V', 'P', 'K'))
2179 { /* do nothing, still works */ }
2180 else if (!track->ms_compat || (track->private_size < sizeof(WAVEFORMATEX)))
2182 free_sh_audio(demuxer, track->tnum);
2183 return 1;
2186 return 0;
2189 /** \brief Parse the private data for VobSub subtitle tracks.
2191 This function tries to parse the private data for all VobSub tracks.
2192 The private data contains the normal text from the original .idx file.
2193 Things like the palette, subtitle dimensions and custom colors are
2194 stored here.
2196 \param demuxer The generic demuxer.
2198 static void
2199 demux_mkv_parse_vobsub_data (demuxer_t *demuxer)
2201 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2202 mkv_track_t *track;
2203 int i;
2205 for (i = 0; i < mkv_d->num_tracks; i++)
2207 track = mkv_d->tracks[i];
2208 if ((track->type != MATROSKA_TRACK_SUBTITLE) ||
2209 (track->subtitle_type != MATROSKA_SUBTYPE_VOBSUB))
2210 continue;
2212 if (!demux_mkv_parse_idx (track))
2214 free (track->private_data);
2215 track->private_data = NULL;
2216 track->private_size = 0;
2221 static int
2222 demux_mkv_open_sub (demuxer_t *demuxer, mkv_track_t *track, int sid)
2224 if (track->subtitle_type != MATROSKA_SUBTYPE_UNKNOWN)
2226 int size, m;
2227 uint8_t *buffer;
2228 sh_sub_t *sh = new_sh_sub_sid(demuxer, track->tnum, sid);
2229 track->sh_sub = sh;
2230 sh->type = 't';
2231 if (track->subtitle_type == MATROSKA_SUBTYPE_VOBSUB)
2232 sh->type = 'v';
2233 if (track->subtitle_type == MATROSKA_SUBTYPE_SSA)
2234 sh->type = 'a';
2235 size = track->private_size;
2236 m = demux_mkv_decode (track,track->private_data,&buffer,&size,2);
2237 if (buffer && m)
2239 free (track->private_data);
2240 track->private_data = buffer;
2241 track->private_size = size;
2243 sh->extradata=malloc(track->private_size);
2244 memcpy (sh->extradata, track->private_data,
2245 track->private_size);
2246 sh->extradata_len = track->private_size;
2248 else
2250 mp_msg (MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_MKV_SubtitleTypeNotSupported,
2251 track->codec_id);
2252 return 1;
2255 return 0;
2258 static void demux_mkv_seek (demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags);
2260 /** \brief Given a matroska track number and type, find the id that mplayer would ask for.
2261 * \param d The demuxer for which the subtitle id should be returned.
2262 * \param num The matroska track number we are looking up.
2263 * \param type The track type.
2265 static int demux_mkv_reverse_id(mkv_demuxer_t *d, int num, int type)
2267 int i, id;
2269 for (i=0, id=0; i < d->num_tracks; i++)
2270 if (d->tracks[i] != NULL && d->tracks[i]->type == type) {
2271 if (d->tracks[i]->tnum == num)
2272 return id;
2273 id++;
2276 return -1;
2279 static int
2280 demux_mkv_open (demuxer_t *demuxer)
2282 stream_t *s = demuxer->stream;
2283 mkv_demuxer_t *mkv_d;
2284 mkv_track_t *track;
2285 int i, version, cont = 0;
2286 char *str;
2288 stream_seek(s, s->start_pos);
2289 str = ebml_read_header (s, &version);
2290 if (str == NULL || strcmp (str, "matroska") || version > 2)
2292 mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] no head found\n");
2293 return 0;
2295 free (str);
2297 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n");
2299 if (ebml_read_id (s, NULL) != MATROSKA_ID_SEGMENT)
2301 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n");
2302 return 0;
2304 ebml_read_length (s, NULL); /* return bytes number until EOF */
2306 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n");
2308 mkv_d = calloc (1, sizeof (mkv_demuxer_t));
2309 demuxer->priv = mkv_d;
2310 mkv_d->tc_scale = 1000000;
2311 mkv_d->segment_start = stream_tell (s);
2312 mkv_d->parsed_cues = malloc (sizeof (off_t));
2313 mkv_d->parsed_seekhead = malloc (sizeof (off_t));
2315 while (!cont)
2317 switch (ebml_read_id (s, NULL))
2319 case MATROSKA_ID_INFO:
2320 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ segment information...\n");
2321 cont = demux_mkv_read_info (demuxer);
2322 break;
2324 case MATROSKA_ID_TRACKS:
2325 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ segment tracks...\n");
2326 cont = demux_mkv_read_tracks (demuxer);
2327 break;
2329 case MATROSKA_ID_CUES:
2330 cont = demux_mkv_read_cues (demuxer);
2331 break;
2333 case MATROSKA_ID_TAGS:
2334 cont = demux_mkv_read_tags (demuxer);
2335 break;
2337 case MATROSKA_ID_SEEKHEAD:
2338 cont = demux_mkv_read_seekhead (demuxer);
2339 break;
2341 case MATROSKA_ID_CHAPTERS:
2342 cont = demux_mkv_read_chapters (demuxer);
2343 break;
2345 case MATROSKA_ID_ATTACHMENTS:
2346 cont = demux_mkv_read_attachments (demuxer);
2347 break;
2349 case MATROSKA_ID_CLUSTER:
2351 int p, l;
2352 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ found cluster, headers are "
2353 "parsed completely :)\n");
2354 /* get the first cluster timecode */
2355 p = stream_tell(s);
2356 l = ebml_read_length (s, NULL);
2357 while (ebml_read_id (s, NULL) != MATROSKA_ID_CLUSTERTIMECODE)
2359 ebml_read_skip (s, NULL);
2360 if (stream_tell (s) >= p + l)
2361 break;
2363 if (stream_tell (s) < p + l)
2365 uint64_t num = ebml_read_uint (s, NULL);
2366 if (num == EBML_UINT_INVALID)
2367 return 0;
2368 mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0;
2369 mkv_d->has_first_tc = 1;
2371 stream_seek (s, p - 4);
2372 cont = 1;
2373 break;
2376 default:
2377 cont = 1;
2378 case EBML_ID_VOID:
2379 ebml_read_skip (s, NULL);
2380 break;
2384 display_create_tracks (demuxer);
2386 /* select video track */
2387 track = NULL;
2388 if (demuxer->video->id == -1) /* automatically select a video track */
2390 /* search for a video track that has the 'default' flag set */
2391 for (i=0; i<mkv_d->num_tracks; i++)
2392 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
2393 && mkv_d->tracks[i]->default_track)
2395 track = mkv_d->tracks[i];
2396 break;
2399 if (track == NULL)
2400 /* no track has the 'default' flag set */
2401 /* let's take the first video track */
2402 for (i=0; i<mkv_d->num_tracks; i++)
2403 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO)
2405 track = mkv_d->tracks[i];
2406 break;
2409 else if (demuxer->video->id != -2) /* -2 = no video at all */
2410 track = demux_mkv_find_track_by_num (mkv_d, demuxer->video->id,
2411 MATROSKA_TRACK_VIDEO);
2413 if (track && demuxer->v_streams[track->tnum])
2415 mp_msg (MSGT_DEMUX, MSGL_INFO,
2416 MSGTR_MPDEMUX_MKV_WillPlayVideoTrack, track->tnum);
2417 demuxer->video->id = track->tnum;
2418 demuxer->video->sh = demuxer->v_streams[track->tnum];
2420 else
2422 mp_msg (MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoVideoTrackFound);
2423 demuxer->video->id = -2;
2426 /* select audio track */
2427 track = NULL;
2428 if (demuxer->audio->id == -1) /* automatically select an audio track */
2430 /* check if the user specified an audio language */
2431 if (audio_lang != NULL)
2432 track = demux_mkv_find_track_by_language(mkv_d, audio_lang,
2433 MATROSKA_TRACK_AUDIO);
2434 if (track == NULL)
2435 /* no audio language specified, or language not found */
2436 /* search for an audio track that has the 'default' flag set */
2437 for (i=0; i < mkv_d->num_tracks; i++)
2438 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
2439 && mkv_d->tracks[i]->default_track)
2441 track = mkv_d->tracks[i];
2442 break;
2445 if (track == NULL)
2446 /* no track has the 'default' flag set */
2447 /* let's take the first audio track */
2448 for (i=0; i < mkv_d->num_tracks; i++)
2449 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO)
2451 track = mkv_d->tracks[i];
2452 break;
2455 else if (demuxer->audio->id != -2) /* -2 = no audio at all */
2456 track = demux_mkv_find_track_by_num (mkv_d, demuxer->audio->id,
2457 MATROSKA_TRACK_AUDIO);
2458 if (track && demuxer->a_streams[track->tnum])
2460 demuxer->audio->id = track->tnum;
2461 demuxer->audio->sh = demuxer->a_streams[track->tnum];
2463 else
2465 mp_msg (MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoAudioTrackFound);
2466 demuxer->audio->id = -2;
2470 if(demuxer->audio->id != -2)
2471 for (i=0; i < mkv_d->num_tracks; i++)
2473 if(mkv_d->tracks[i]->type != MATROSKA_TRACK_AUDIO)
2474 continue;
2475 if(demuxer->a_streams[track->tnum])
2477 mkv_d->last_aid++;
2478 if(mkv_d->last_aid == MAX_A_STREAMS)
2479 break;
2483 demux_mkv_parse_vobsub_data (demuxer);
2485 /* DO NOT automatically select a subtitle track and behave like DVD */
2486 /* playback: only show subtitles if the user explicitely wants them. */
2487 track = NULL;
2488 if (demuxer->sub->id >= 0)
2489 track = demux_mkv_find_track_by_num (mkv_d, demuxer->sub->id,
2490 MATROSKA_TRACK_SUBTITLE);
2491 else if (dvdsub_lang != NULL)
2492 track = demux_mkv_find_track_by_language (mkv_d, dvdsub_lang,
2493 MATROSKA_TRACK_SUBTITLE);
2495 if (track)
2497 mp_msg (MSGT_DEMUX, MSGL_INFO,
2498 MSGTR_MPDEMUX_MKV_WillDisplaySubtitleTrack, track->tnum);
2499 dvdsub_id = demux_mkv_reverse_id(mkv_d, track->tnum, MATROSKA_TRACK_SUBTITLE);
2500 demuxer->sub->id = track->tnum;
2501 demuxer->sub->sh = demuxer->s_streams[track->tnum];
2503 else
2504 demuxer->sub->id = -2;
2506 if (demuxer->chapters)
2508 for (i=0; i < (int)demuxer->num_chapters; i++)
2510 demuxer->chapters[i].start -= mkv_d->first_tc;
2511 demuxer->chapters[i].end -= mkv_d->first_tc;
2513 if (dvd_last_chapter > 0 && dvd_last_chapter <= demuxer->num_chapters)
2515 if (demuxer->chapters[dvd_last_chapter-1].end != 0)
2516 mkv_d->stop_timecode = demuxer->chapters[dvd_last_chapter-1].end;
2517 else if (dvd_last_chapter + 1 <= demuxer->num_chapters)
2518 mkv_d->stop_timecode = demuxer->chapters[dvd_last_chapter].start;
2522 if (s->end_pos == 0 || (mkv_d->indexes == NULL && index_mode < 0))
2523 demuxer->seekable = 0;
2524 else
2526 demuxer->movi_start = s->start_pos;
2527 demuxer->movi_end = s->end_pos;
2528 demuxer->seekable = 1;
2529 if (demuxer->chapters && dvd_chapter>1 && dvd_chapter<=demuxer->num_chapters)
2531 if (!mkv_d->has_first_tc)
2533 mkv_d->first_tc = 0;
2534 mkv_d->has_first_tc = 1;
2536 demux_mkv_seek (demuxer,
2537 demuxer->chapters[dvd_chapter-1].start/1000.0, 0.0, 1);
2541 return DEMUXER_TYPE_MATROSKA;
2544 static void
2545 demux_close_mkv (demuxer_t *demuxer)
2547 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2549 if (mkv_d)
2551 int i;
2552 free_cached_dps (demuxer);
2553 if (mkv_d->tracks)
2555 for (i=0; i<mkv_d->num_tracks; i++)
2556 demux_mkv_free_trackentry(mkv_d->tracks[i]);
2557 free (mkv_d->tracks);
2559 if (mkv_d->indexes)
2560 free (mkv_d->indexes);
2561 if (mkv_d->cluster_positions)
2562 free (mkv_d->cluster_positions);
2563 if (mkv_d->parsed_cues)
2564 free (mkv_d->parsed_cues);
2565 if (mkv_d->parsed_seekhead)
2566 free (mkv_d->parsed_seekhead);
2567 free (mkv_d);
2571 static int
2572 demux_mkv_read_block_lacing (uint8_t *buffer, uint64_t *size,
2573 uint8_t *laces, uint32_t **all_lace_sizes)
2575 uint32_t total = 0, *lace_size;
2576 uint8_t flags;
2577 int i;
2579 *all_lace_sizes = NULL;
2580 lace_size = NULL;
2581 /* lacing flags */
2582 flags = *buffer++;
2583 (*size)--;
2585 switch ((flags & 0x06) >> 1)
2587 case 0: /* no lacing */
2588 *laces = 1;
2589 lace_size = calloc(*laces, sizeof(uint32_t));
2590 lace_size[0] = *size;
2591 break;
2593 case 1: /* xiph lacing */
2594 case 2: /* fixed-size lacing */
2595 case 3: /* EBML lacing */
2596 *laces = *buffer++;
2597 (*size)--;
2598 (*laces)++;
2599 lace_size = calloc(*laces, sizeof(uint32_t));
2601 switch ((flags & 0x06) >> 1)
2603 case 1: /* xiph lacing */
2604 for (i=0; i < *laces-1; i++)
2606 lace_size[i] = 0;
2609 lace_size[i] += *buffer;
2610 (*size)--;
2611 } while (*buffer++ == 0xFF);
2612 total += lace_size[i];
2614 lace_size[i] = *size - total;
2615 break;
2617 case 2: /* fixed-size lacing */
2618 for (i=0; i < *laces; i++)
2619 lace_size[i] = *size / *laces;
2620 break;
2622 case 3: /* EBML lacing */
2624 int l;
2625 uint64_t num = ebml_read_vlen_uint (buffer, &l);
2626 if (num == EBML_UINT_INVALID) {
2627 free(lace_size);
2628 return 1;
2630 buffer += l;
2631 *size -= l;
2633 total = lace_size[0] = num;
2634 for (i=1; i < *laces-1; i++)
2636 int64_t snum;
2637 snum = ebml_read_vlen_int (buffer, &l);
2638 if (snum == EBML_INT_INVALID) {
2639 free(lace_size);
2640 return 1;
2642 buffer += l;
2643 *size -= l;
2644 lace_size[i] = lace_size[i-1] + snum;
2645 total += lace_size[i];
2647 lace_size[i] = *size - total;
2648 break;
2651 break;
2653 *all_lace_sizes = lace_size;
2654 return 0;
2657 static void
2658 handle_subtitles(demuxer_t *demuxer, mkv_track_t *track, char *block,
2659 int64_t size, uint64_t block_duration, uint64_t timecode)
2661 demux_packet_t *dp;
2663 if (block_duration == 0)
2665 mp_msg (MSGT_DEMUX, MSGL_WARN,
2666 MSGTR_MPDEMUX_MKV_NoBlockDurationForSubtitleTrackFound);
2667 return;
2670 sub_utf8 = 1;
2671 dp = new_demux_packet(size);
2672 memcpy(dp->buffer, block, size);
2673 dp->pts = timecode / 1000.0f;
2674 dp->endpts = (timecode + block_duration) / 1000.0f;
2675 ds_add_packet(demuxer->sub, dp);
2678 // Taken from demux_real.c. Thanks to the original developpers :)
2679 #define SKIP_BITS(n) buffer <<= n
2680 #define SHOW_BITS(n) ((buffer) >> (32 - (n)))
2682 static float real_fix_timestamp(mkv_track_t *track, unsigned char *s,
2683 int timestamp) {
2684 float v_pts;
2685 uint32_t buffer = (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3];
2686 int kf = timestamp;
2687 int pict_type;
2688 int orig_kf;
2690 if (!strcmp(track->codec_id, MKV_V_REALV30) ||
2691 !strcmp(track->codec_id, MKV_V_REALV40)) {
2693 if (!strcmp(track->codec_id, MKV_V_REALV30)) {
2694 SKIP_BITS(3);
2695 pict_type = SHOW_BITS(2);
2696 SKIP_BITS(2 + 7);
2697 }else{
2698 SKIP_BITS(1);
2699 pict_type = SHOW_BITS(2);
2700 SKIP_BITS(2 + 7 + 3);
2702 kf = SHOW_BITS(13); // kf= 2*SHOW_BITS(12);
2703 orig_kf = kf;
2704 if (pict_type <= 1) {
2705 // I frame, sync timestamps:
2706 track->rv_kf_base = timestamp - kf;
2707 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\nTS: base=%08X\n", track->rv_kf_base);
2708 kf = timestamp;
2709 } else {
2710 // P/B frame, merge timestamps:
2711 int tmp = timestamp - track->rv_kf_base;
2712 kf |= tmp & (~0x1fff); // combine with packet timestamp
2713 if (kf < (tmp - 4096)) // workaround wrap-around problems
2714 kf += 8192;
2715 else if (kf > (tmp + 4096))
2716 kf -= 8192;
2717 kf += track->rv_kf_base;
2719 if (pict_type != 3) { // P || I frame -> swap timestamps
2720 int tmp = kf;
2721 kf = track->rv_kf_pts;
2722 track->rv_kf_pts = tmp;
2724 mp_msg(MSGT_DEMUX, MSGL_DBG2, "\nTS: %08X -> %08X (%04X) %d %02X %02X %02X "
2725 "%02X %5d\n", timestamp, kf, orig_kf, pict_type, s[0], s[1], s[2],
2726 s[3], kf - (int)(1000.0 * track->rv_pts));
2728 v_pts = kf * 0.001f;
2729 track->rv_pts = v_pts;
2731 return v_pts;
2734 static void
2735 handle_realvideo (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
2736 uint32_t size, int block_bref)
2738 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2739 demux_packet_t *dp;
2740 uint32_t timestamp = mkv_d->last_pts * 1000;
2741 uint32_t *hdr;
2742 uint8_t chunks;
2743 int isize;
2744 #ifdef WORDS_BIGENDIAN
2745 uint8_t *p;
2746 int i;
2747 #endif
2749 chunks = *buffer++;
2750 isize = --size - (chunks+1)*8;
2751 dp = new_demux_packet (REALHEADER_SIZE + size);
2752 memcpy (dp->buffer + REALHEADER_SIZE, buffer + (chunks+1)*8, isize);
2753 #ifdef WORDS_BIGENDIAN
2754 p = (uint8_t *)(dp->buffer + REALHEADER_SIZE + isize);
2755 for (i = 0; i<(chunks+1)*8; i+=4) {
2756 p[i] = *((uint8_t *)buffer+i+3);
2757 p[i+1] = *((uint8_t *)buffer+i+2);
2758 p[i+2] = *((uint8_t *)buffer+i+1);
2759 p[i+3] = *((uint8_t *)buffer+i);
2761 #else
2762 memcpy (dp->buffer + REALHEADER_SIZE + isize, buffer, (chunks+1)*8);
2763 #endif
2765 hdr = dp->buffer;
2766 *hdr++ = chunks; // number of chunks
2767 *hdr++ = timestamp; // timestamp from packet header
2768 *hdr++ = isize; // length of actual data
2769 *hdr++ = REALHEADER_SIZE + isize; // offset to chunk offset array
2771 if (mkv_d->v_skip_to_keyframe)
2773 dp->pts = mkv_d->last_pts;
2774 track->rv_kf_base = 0;
2775 track->rv_kf_pts = timestamp;
2777 else
2778 dp->pts = real_fix_timestamp (track, dp->buffer + REALHEADER_SIZE,
2779 timestamp);
2780 dp->pos = demuxer->filepos;
2781 dp->flags = block_bref ? 0 : 0x10;
2783 ds_add_packet(demuxer->video, dp);
2786 static void
2787 handle_realaudio (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
2788 uint32_t size, int block_bref)
2790 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2791 int sps = track->sub_packet_size;
2792 int sph = track->sub_packet_h;
2793 int cfs = track->coded_framesize;
2794 int w = track->audiopk_size;
2795 int spc = track->sub_packet_cnt;
2796 demux_packet_t *dp;
2797 int x;
2799 if ((track->a_formattag == mmioFOURCC('2', '8', '_', '8')) ||
2800 (track->a_formattag == mmioFOURCC('c', 'o', 'o', 'k')) ||
2801 (track->a_formattag == mmioFOURCC('a', 't', 'r', 'c')) ||
2802 (track->a_formattag == mmioFOURCC('s', 'i', 'p', 'r')))
2804 // if(!block_bref)
2805 // spc = track->sub_packet_cnt = 0;
2806 switch (track->a_formattag) {
2807 case mmioFOURCC('2', '8', '_', '8'):
2808 for (x = 0; x < sph / 2; x++)
2809 memcpy(track->audio_buf + x * 2 * w + spc * cfs, buffer + cfs * x, cfs);
2810 break;
2811 case mmioFOURCC('c', 'o', 'o', 'k'):
2812 case mmioFOURCC('a', 't', 'r', 'c'):
2813 for (x = 0; x < w / sps; x++)
2814 memcpy(track->audio_buf + sps * (sph * x + ((sph + 1) / 2) * (spc & 1) + (spc >> 1)), buffer + sps * x, sps);
2815 break;
2816 case mmioFOURCC('s', 'i', 'p', 'r'):
2817 memcpy(track->audio_buf + spc * w, buffer, w);
2818 if (spc == sph - 1)
2820 int n;
2821 int bs = sph * w * 2 / 96; // nibbles per subpacket
2822 // Perform reordering
2823 for(n=0; n < 38; n++)
2825 int j;
2826 int i = bs * sipr_swaps[n][0];
2827 int o = bs * sipr_swaps[n][1];
2828 // swap nibbles of block 'i' with 'o' TODO: optimize
2829 for(j = 0;j < bs; j++)
2831 int x = (i & 1) ? (track->audio_buf[i >> 1] >> 4) : (track->audio_buf[i >> 1] & 0x0F);
2832 int y = (o & 1) ? (track->audio_buf[o >> 1] >> 4) : (track->audio_buf[o >> 1] & 0x0F);
2833 if(o & 1)
2834 track->audio_buf[o >> 1] = (track->audio_buf[o >> 1] & 0x0F) | (x << 4);
2835 else
2836 track->audio_buf[o >> 1] = (track->audio_buf[o >> 1] & 0xF0) | x;
2837 if(i & 1)
2838 track->audio_buf[i >> 1] = (track->audio_buf[i >> 1] & 0x0F) | (y << 4);
2839 else
2840 track->audio_buf[i >> 1] = (track->audio_buf[i >> 1] & 0xF0) | y;
2841 ++i; ++o;
2845 break;
2847 track->audio_timestamp[track->sub_packet_cnt] = (track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts);
2848 track->ra_pts = mkv_d->last_pts;
2849 if (track->sub_packet_cnt == 0)
2850 track->audio_filepos = demuxer->filepos;
2851 if (++(track->sub_packet_cnt) == sph)
2853 int apk_usize = ((WAVEFORMATEX*)((sh_audio_t*)demuxer->audio->sh)->wf)->nBlockAlign;
2854 track->sub_packet_cnt = 0;
2855 // Release all the audio packets
2856 for (x = 0; x < sph*w/apk_usize; x++)
2858 dp = new_demux_packet(apk_usize);
2859 memcpy(dp->buffer, track->audio_buf + x * apk_usize, apk_usize);
2860 /* Put timestamp only on packets that correspond to original audio packets in file */
2861 dp->pts = (x * apk_usize % w) ? 0 : track->audio_timestamp[x * apk_usize / w];
2862 dp->pos = track->audio_filepos; // all equal
2863 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
2864 ds_add_packet(demuxer->audio, dp);
2867 } else { // Not a codec that require reordering
2868 dp = new_demux_packet (size);
2869 memcpy(dp->buffer, buffer, size);
2870 if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe)
2871 dp->pts = 0;
2872 else
2873 dp->pts = mkv_d->last_pts;
2874 track->ra_pts = mkv_d->last_pts;
2876 dp->pos = demuxer->filepos;
2877 dp->flags = block_bref ? 0 : 0x10;
2878 ds_add_packet (demuxer->audio, dp);
2882 /** Reorder timecodes and add cached demux packets to the queues.
2884 * Timecode reordering is needed if a video track contains B frames that
2885 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2886 * MPlayer doesn't like timestamps in display order. This function adjusts
2887 * the timestamp of cached frames (which are exactly one I/P frame followed
2888 * by one or more B frames) so that they are in coding order again.
2890 * Example: The track with 25 FPS contains four frames with the timecodes
2891 * I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I
2892 * or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms,
2893 * B at 80ms and B at 120ms.
2895 * This works for simple H.264 B-frame pyramids, but not for arbitrary orders.
2897 * \param demuxer The Matroska demuxer struct for this instance.
2898 * \param track The track structure whose cache should be handled.
2900 static void
2901 flush_cached_dps (demuxer_t *demuxer, mkv_track_t *track)
2903 int i, ok;
2905 if (track->num_cached_dps == 0)
2906 return;
2908 do {
2909 ok = 1;
2910 for (i = 1; i < track->num_cached_dps; i++)
2911 if (track->cached_dps[i - 1]->pts > track->cached_dps[i]->pts) {
2912 float tmp_pts = track->cached_dps[i - 1]->pts;
2913 track->cached_dps[i - 1]->pts = track->cached_dps[i]->pts;
2914 track->cached_dps[i]->pts = tmp_pts;
2915 ok = 0;
2917 } while (!ok);
2919 for (i = 0; i < track->num_cached_dps; i++)
2920 ds_add_packet (demuxer->video, track->cached_dps[i]);
2921 track->num_cached_dps = 0;
2924 /** Cache video frames if timecodes have to be reordered.
2926 * Timecode reordering is needed if a video track contains B frames that
2927 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2928 * This function takes in a Matroska block read from the file, allocates a
2929 * demux packet for it, fills in its values, allocates space for storing
2930 * pointers to the cached demux packets and adds the packet to it. If
2931 * the packet contains an I or a P frame then ::flush_cached_dps is called
2932 * in order to send the old cached frames downstream.
2934 * \param demuxer The Matroska demuxer struct for this instance.
2935 * \param track The packet is meant for this track.
2936 * \param buffer The actual frame contents.
2937 * \param size The frame size in bytes.
2938 * \param block_bref A relative timecode (backward reference). If it is \c 0
2939 * then the frame is an I frame.
2940 * \param block_fref A relative timecode (forward reference). If it is \c 0
2941 * then the frame is either an I frame or a P frame depending on the value
2942 * of \a block_bref. Otherwise it's a B frame.
2944 static void
2945 handle_video_bframes (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
2946 uint32_t size, int block_bref, int block_fref)
2948 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2949 demux_packet_t *dp;
2951 dp = new_demux_packet (size);
2952 memcpy(dp->buffer, buffer, size);
2953 dp->pos = demuxer->filepos;
2954 dp->pts = mkv_d->last_pts;
2955 if ((track->num_cached_dps > 0) && (dp->pts < track->max_pts))
2956 block_fref = 1;
2957 if (block_fref == 0) /* I or P frame */
2958 flush_cached_dps (demuxer, track);
2959 if (block_bref != 0) /* I frame, don't cache it */
2960 dp->flags = 0x10;
2961 if ((track->num_cached_dps + 1) > track->num_allocated_dps)
2963 track->cached_dps = (demux_packet_t **)
2964 realloc(track->cached_dps, (track->num_cached_dps + 10) *
2965 sizeof(demux_packet_t *));
2966 track->num_allocated_dps += 10;
2968 track->cached_dps[track->num_cached_dps] = dp;
2969 track->num_cached_dps++;
2970 if (dp->pts > track->max_pts)
2971 track->max_pts = dp->pts;
2974 static int
2975 handle_block (demuxer_t *demuxer, uint8_t *block, uint64_t length,
2976 uint64_t block_duration, int64_t block_bref, int64_t block_fref, uint8_t simpleblock)
2978 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2979 mkv_track_t *track = NULL;
2980 demux_stream_t *ds = NULL;
2981 uint64_t old_length;
2982 int64_t tc;
2983 uint32_t *lace_size;
2984 uint8_t laces, flags;
2985 int i, num, tmp, use_this_block = 1;
2986 float current_pts;
2987 int16_t time;
2989 /* first byte(s): track num */
2990 num = ebml_read_vlen_uint (block, &tmp);
2991 block += tmp;
2992 /* time (relative to cluster time) */
2993 time = block[0] << 8 | block[1];
2994 block += 2;
2995 length -= tmp + 2;
2996 old_length = length;
2997 flags = block[0];
2998 if (demux_mkv_read_block_lacing (block, &length, &laces, &lace_size))
2999 return 0;
3000 block += old_length - length;
3002 tc = ((time*mkv_d->tc_scale+mkv_d->cluster_tc) /1000000.0 - mkv_d->first_tc);
3003 if (tc < 0)
3004 tc = 0;
3005 if (mkv_d->stop_timecode > 0 && tc > mkv_d->stop_timecode) {
3006 free(lace_size);
3007 return -1;
3009 current_pts = tc / 1000.0;
3011 for (i=0; i<mkv_d->num_tracks; i++)
3012 if (mkv_d->tracks[i]->tnum == num) {
3013 track = mkv_d->tracks[i];
3014 break;
3016 if (track == NULL)
3018 free(lace_size);
3019 return 1;
3021 if (num == demuxer->audio->id)
3023 ds = demuxer->audio;
3025 if (mkv_d->a_skip_to_keyframe)
3027 if (simpleblock)
3029 if (!(flags&0x80)) /*current frame isn't a keyframe*/
3030 use_this_block = 0;
3032 else if (block_bref != 0)
3033 use_this_block = 0;
3035 else if (mkv_d->v_skip_to_keyframe)
3036 use_this_block = 0;
3038 if (track->fix_i_bps && use_this_block)
3040 sh_audio_t *sh = (sh_audio_t *) ds->sh;
3042 if (block_duration != 0)
3044 sh->i_bps = length * 1000 / block_duration;
3045 track->fix_i_bps = 0;
3047 else if (track->qt_last_a_pts == 0.0)
3048 track->qt_last_a_pts = current_pts;
3049 else if(track->qt_last_a_pts != current_pts)
3051 sh->i_bps = length / (current_pts - track->qt_last_a_pts);
3052 track->fix_i_bps = 0;
3056 else if (tc < mkv_d->skip_to_timecode)
3057 use_this_block = 0;
3058 else if (num == demuxer->video->id)
3060 ds = demuxer->video;
3061 if (mkv_d->v_skip_to_keyframe)
3063 if (simpleblock)
3065 if (!(flags&0x80)) /*current frame isn't a keyframe*/
3066 use_this_block = 0;
3068 else if (block_bref != 0 || block_fref != 0)
3069 use_this_block = 0;
3072 else if (num == demuxer->sub->id)
3074 ds = demuxer->sub;
3075 if (track->subtitle_type != MATROSKA_SUBTYPE_VOBSUB)
3077 if (!mkv_d->v_skip_to_keyframe)
3078 handle_subtitles (demuxer, track, block, length,
3079 block_duration, tc);
3080 use_this_block = 0;
3083 else
3084 use_this_block = 0;
3086 if (use_this_block)
3088 mkv_d->last_pts = current_pts;
3089 mkv_d->last_filepos = demuxer->filepos;
3091 for (i=0; i < laces; i++)
3093 if (ds == demuxer->video && track->realmedia)
3094 handle_realvideo (demuxer, track, block, lace_size[i], block_bref);
3095 else if (ds == demuxer->audio && track->realmedia)
3096 handle_realaudio (demuxer, track, block, lace_size[i], block_bref);
3097 else if (ds == demuxer->video && track->reorder_timecodes)
3098 handle_video_bframes (demuxer, track, block, lace_size[i],
3099 block_bref, block_fref);
3100 else
3102 int modified, size = lace_size[i];
3103 demux_packet_t *dp;
3104 uint8_t *buffer;
3105 modified = demux_mkv_decode (track, block, &buffer, &size, 1);
3106 if (buffer)
3108 dp = new_demux_packet (size);
3109 memcpy (dp->buffer, buffer, size);
3110 if (modified)
3111 free (buffer);
3112 dp->flags = (block_bref == 0 && block_fref == 0) ? 0x10 : 0;
3113 /* If default_duration is 0, assume no pts value is known
3114 * for packets after the first one (rather than all pts
3115 * values being the same) */
3116 if (i == 0 || track->default_duration)
3117 dp->pts = mkv_d->last_pts + i * track->default_duration;
3118 ds_add_packet (ds, dp);
3121 block += lace_size[i];
3124 if (ds == demuxer->video)
3126 mkv_d->v_skip_to_keyframe = 0;
3127 mkv_d->skip_to_timecode = 0;
3129 else if (ds == demuxer->audio)
3130 mkv_d->a_skip_to_keyframe = 0;
3132 free(lace_size);
3133 return 1;
3136 free(lace_size);
3137 return 0;
3140 static int
3141 demux_mkv_fill_buffer (demuxer_t *demuxer, demux_stream_t *ds)
3143 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3144 stream_t *s = demuxer->stream;
3145 uint64_t l;
3146 int il, tmp;
3148 while (1)
3150 while (mkv_d->cluster_size > 0)
3152 uint64_t block_duration = 0, block_length = 0;
3153 int64_t block_bref = 0, block_fref = 0;
3154 uint8_t *block = NULL;
3156 while (mkv_d->blockgroup_size > 0)
3158 switch (ebml_read_id (s, &il))
3160 case MATROSKA_ID_BLOCKDURATION:
3162 block_duration = ebml_read_uint (s, &l);
3163 if (block_duration == EBML_UINT_INVALID) {
3164 free(block);
3165 return 0;
3167 block_duration *= mkv_d->tc_scale / 1000000.0;
3168 break;
3171 case MATROSKA_ID_BLOCK:
3172 block_length = ebml_read_length (s, &tmp);
3173 free(block);
3174 if (block_length > SIZE_MAX - LZO_INPUT_PADDING) return 0;
3175 block = malloc (block_length + LZO_INPUT_PADDING);
3176 demuxer->filepos = stream_tell (s);
3177 if (stream_read (s,block,block_length) != (int) block_length)
3179 free(block);
3180 return 0;
3182 l = tmp + block_length;
3183 break;
3185 case MATROSKA_ID_REFERENCEBLOCK:
3187 int64_t num = ebml_read_int (s, &l);
3188 if (num == EBML_INT_INVALID) {
3189 free(block);
3190 return 0;
3192 if (num <= 0)
3193 block_bref = num;
3194 else
3195 block_fref = num;
3196 break;
3199 case EBML_ID_INVALID:
3200 free(block);
3201 return 0;
3203 default:
3204 ebml_read_skip (s, &l);
3205 break;
3207 mkv_d->blockgroup_size -= l + il;
3208 mkv_d->cluster_size -= l + il;
3211 if (block)
3213 int res = handle_block (demuxer, block, block_length,
3214 block_duration, block_bref, block_fref, 0);
3215 free (block);
3216 if (res < 0)
3217 return 0;
3218 if (res)
3219 return 1;
3222 if (mkv_d->cluster_size > 0)
3224 switch (ebml_read_id (s, &il))
3226 case MATROSKA_ID_CLUSTERTIMECODE:
3228 uint64_t num = ebml_read_uint (s, &l);
3229 if (num == EBML_UINT_INVALID)
3230 return 0;
3231 if (!mkv_d->has_first_tc)
3233 mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0;
3234 mkv_d->has_first_tc = 1;
3236 mkv_d->cluster_tc = num * mkv_d->tc_scale;
3237 break;
3240 case MATROSKA_ID_BLOCKGROUP:
3241 mkv_d->blockgroup_size = ebml_read_length (s, &tmp);
3242 l = tmp;
3243 break;
3245 case MATROSKA_ID_SIMPLEBLOCK:
3247 int res;
3248 block_length = ebml_read_length (s, &tmp);
3249 block = malloc (block_length);
3250 demuxer->filepos = stream_tell (s);
3251 if (stream_read (s,block,block_length) != (int) block_length)
3253 free(block);
3254 return 0;
3256 l = tmp + block_length;
3257 res = handle_block (demuxer, block, block_length,
3258 block_duration, block_bref, block_fref, 1);
3259 free (block);
3260 mkv_d->cluster_size -= l + il;
3261 if (res < 0)
3262 return 0;
3263 else if (res)
3264 return 1;
3265 else mkv_d->cluster_size += l + il;
3266 break;
3268 case EBML_ID_INVALID:
3269 return 0;
3271 default:
3272 ebml_read_skip (s, &l);
3273 break;
3275 mkv_d->cluster_size -= l + il;
3279 if (ebml_read_id (s, &il) != MATROSKA_ID_CLUSTER)
3280 return 0;
3281 add_cluster_position(mkv_d, stream_tell(s)-il);
3282 mkv_d->cluster_size = ebml_read_length (s, NULL);
3285 return 0;
3288 static void
3289 demux_mkv_seek (demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
3291 free_cached_dps (demuxer);
3292 if (!(flags & 2)) /* time in secs */
3294 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3295 stream_t *s = demuxer->stream;
3296 int64_t target_timecode = 0, diff, min_diff=0xFFFFFFFFFFFFFFFLL;
3297 int i;
3299 if (!(flags & 1)) /* relative seek */
3300 target_timecode = (int64_t) (mkv_d->last_pts * 1000.0);
3301 target_timecode += (int64_t)(rel_seek_secs * 1000.0);
3302 if (target_timecode < 0)
3303 target_timecode = 0;
3305 if (mkv_d->indexes == NULL) /* no index was found */
3307 uint64_t target_filepos, cluster_pos, max_pos;
3309 target_filepos = (uint64_t) (target_timecode * mkv_d->last_filepos
3310 / (mkv_d->last_pts * 1000.0));
3312 max_pos = mkv_d->cluster_positions[mkv_d->num_cluster_pos-1];
3313 if (target_filepos > max_pos)
3315 if ((off_t) max_pos > stream_tell (s))
3316 stream_seek (s, max_pos);
3317 else
3318 stream_seek (s, stream_tell (s) + mkv_d->cluster_size);
3319 /* parse all the clusters upto target_filepos */
3320 while (!s->eof && stream_tell(s) < (off_t) target_filepos)
3322 switch (ebml_read_id (s, &i))
3324 case MATROSKA_ID_CLUSTER:
3325 add_cluster_position(mkv_d, (uint64_t) stream_tell(s)-i);
3326 break;
3328 case MATROSKA_ID_CUES:
3329 demux_mkv_read_cues (demuxer);
3330 break;
3332 ebml_read_skip (s, NULL);
3334 if (s->eof)
3335 stream_reset(s);
3338 if (mkv_d->indexes == NULL)
3340 cluster_pos = mkv_d->cluster_positions[0];
3341 /* Let's find the nearest cluster */
3342 for (i=0; i < mkv_d->num_cluster_pos; i++)
3344 diff = mkv_d->cluster_positions[i] - target_filepos;
3345 if (rel_seek_secs < 0 && diff < 0 && -diff < min_diff)
3347 cluster_pos = mkv_d->cluster_positions[i];
3348 min_diff = -diff;
3350 else if (rel_seek_secs > 0
3351 && (diff < 0 ? -1 * diff : diff) < min_diff)
3353 cluster_pos = mkv_d->cluster_positions[i];
3354 min_diff = diff < 0 ? -1 * diff : diff;
3357 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
3358 stream_seek (s, cluster_pos);
3361 else
3363 mkv_index_t *index = NULL;
3364 int seek_id = (demuxer->video->id < 0) ? demuxer->audio->id : demuxer->video->id;
3366 /* let's find the entry in the indexes with the smallest */
3367 /* difference to the wanted timecode. */
3368 for (i=0; i < mkv_d->num_indexes; i++)
3369 if (mkv_d->indexes[i].tnum == seek_id)
3371 diff = target_timecode + mkv_d->first_tc -
3372 (int64_t) mkv_d->indexes[i].timecode * mkv_d->tc_scale / 1000000.0;
3374 if ((flags & 1 || target_timecode <= mkv_d->last_pts*1000)) {
3375 // Absolute seek or seek backward: find the last index
3376 // position before target time
3377 if (diff < 0 || diff >= min_diff)
3378 continue;
3380 else {
3381 // Relative seek forward: find the first index position
3382 // after target time. If no such index exists, find last
3383 // position between current position and target time.
3384 if (diff <= 0) {
3385 if (min_diff <= 0 && diff <= min_diff)
3386 continue;
3388 else if (diff >= FFMIN(target_timecode - mkv_d->last_pts,
3389 min_diff))
3390 continue;
3392 min_diff = diff;
3393 index = mkv_d->indexes + i;
3396 if (index) /* We've found an entry. */
3398 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
3399 stream_seek (s, index->filepos);
3403 if (demuxer->video->id >= 0)
3404 mkv_d->v_skip_to_keyframe = 1;
3405 if (rel_seek_secs > 0.0)
3406 mkv_d->skip_to_timecode = target_timecode;
3407 mkv_d->a_skip_to_keyframe = 1;
3409 demux_mkv_fill_buffer(demuxer, NULL);
3411 else if ((demuxer->movi_end <= 0) || !(flags & 1))
3412 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
3413 else
3415 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3416 stream_t *s = demuxer->stream;
3417 uint64_t target_filepos;
3418 mkv_index_t *index = NULL;
3419 int i;
3421 if (mkv_d->indexes == NULL) /* no index was found */
3422 { /* I'm lazy... */
3423 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
3424 return;
3427 target_filepos = (uint64_t)(demuxer->movi_end * rel_seek_secs);
3428 for (i=0; i < mkv_d->num_indexes; i++)
3429 if (mkv_d->indexes[i].tnum == demuxer->video->id)
3430 if ((index == NULL) ||
3431 ((mkv_d->indexes[i].filepos >= target_filepos) &&
3432 ((index->filepos < target_filepos) ||
3433 (mkv_d->indexes[i].filepos < index->filepos))))
3434 index = &mkv_d->indexes[i];
3436 if (!index)
3437 return;
3439 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
3440 stream_seek (s, index->filepos);
3442 if (demuxer->video->id >= 0)
3443 mkv_d->v_skip_to_keyframe = 1;
3444 mkv_d->skip_to_timecode = index->timecode;
3445 mkv_d->a_skip_to_keyframe = 1;
3447 demux_mkv_fill_buffer(demuxer, NULL);
3451 static int
3452 demux_mkv_control (demuxer_t *demuxer, int cmd, void *arg)
3454 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3456 switch (cmd)
3458 case DEMUXER_CTRL_GET_TIME_LENGTH:
3459 if (mkv_d->duration == 0)
3460 return DEMUXER_CTRL_DONTKNOW;
3462 *((double *)arg) = (double)mkv_d->duration;
3463 return DEMUXER_CTRL_OK;
3465 case DEMUXER_CTRL_GET_PERCENT_POS:
3466 if (mkv_d->duration == 0)
3468 return DEMUXER_CTRL_DONTKNOW;
3471 *((int *) arg) = (int) (100 * mkv_d->last_pts / mkv_d->duration);
3472 return DEMUXER_CTRL_OK;
3474 case DEMUXER_CTRL_SWITCH_AUDIO:
3475 if (demuxer->audio && demuxer->audio->sh) {
3476 sh_audio_t *sh = demuxer->a_streams[demuxer->audio->id];
3477 int aid = *(int*)arg;
3478 if (aid < 0)
3479 aid = (sh->aid + 1) % mkv_d->last_aid;
3480 if (aid != sh->aid) {
3481 mkv_track_t *track = demux_mkv_find_track_by_num (mkv_d, aid, MATROSKA_TRACK_AUDIO);
3482 if (track) {
3483 demuxer->audio->id = track->tnum;
3484 sh = demuxer->a_streams[demuxer->audio->id];
3485 ds_free_packs(demuxer->audio);
3488 *(int*)arg = sh->aid;
3489 } else
3490 *(int*)arg = -2;
3491 return DEMUXER_CTRL_OK;
3493 default:
3494 return DEMUXER_CTRL_NOTIMPL;
3498 /** \brief Get the language code for a subtitle track.
3500 Retrieves the language code for a subtitle track if it is known.
3501 If the language code is "und" then do not copy it ("und" = "undefined").
3503 \param demuxer The demuxer to work on
3504 \param track_num The subtitle track number to get the language from
3505 \param lang Store the language here
3506 \param maxlen The maximum number of characters to copy into lang
3508 void
3509 demux_mkv_get_sub_lang(demuxer_t *demuxer, int track_num, char *lang,
3510 int maxlen)
3512 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3513 mkv_track_t *track = demux_mkv_find_track_by_num (mkv_d, track_num, MATROSKA_TRACK_SUBTITLE);
3514 if (track && track->language && strcmp(track->language, "und"))
3515 av_strlcpy(lang, track->language, maxlen);
3518 /** \brief Get the language code for an audio track.
3520 Retrieves the language code for an audio track if it is known.
3521 If the language code is "und" then do not copy it ("und" = "undefined").
3523 \param demuxer The demuxer to work on
3524 \param track_num The n'th audio track to get the language from
3525 \param lang Store the language here
3526 \param maxlen The maximum number of characters to copy into lang
3528 void
3529 demux_mkv_get_audio_lang(demuxer_t *demuxer, int track_num, char *lang,
3530 int maxlen)
3532 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3533 mkv_track_t *track = demux_mkv_find_track_by_num (mkv_d, track_num, MATROSKA_TRACK_AUDIO);
3534 if (track && track->language && strcmp(track->language, "und"))
3535 av_strlcpy(lang, track->language, maxlen);
3539 const demuxer_desc_t demuxer_desc_matroska = {
3540 "Matroska demuxer",
3541 "mkv",
3542 "Matroska",
3543 "Aurelien Jacobs",
3545 DEMUXER_TYPE_MATROSKA,
3546 1, // safe autodetect
3547 demux_mkv_open,
3548 demux_mkv_fill_buffer,
3549 NULL,
3550 demux_close_mkv,
3551 demux_mkv_seek,
3552 demux_mkv_control