2 * Matroska file demuxer (no muxer yet)
3 * Copyright (c) 2003-2004 The ffmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Matroska file demuxer
25 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
26 * with a little help from Moritz Bunkus <moritz@bunkus.org>
27 * Specs available on the matroska project page:
28 * http://www.matroska.org/.
32 /* For codec_get_id(). */
36 #include "libavcodec/mpeg4audio.h"
37 #include "libavutil/intfloat_readwrite.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/lzo.h"
59 typedef const struct EbmlSyntax {
68 const struct EbmlSyntax *n;
88 uint64_t doctype_version;
91 typedef struct Track {
92 MatroskaTrackType type;
94 /* Unique track number and track ID. stream_index is the index that
95 * the calling app uses for this track. */
104 unsigned char *codec_priv;
108 uint64_t default_duration;
109 uint64_t flag_default;
112 MatroskaTrackEncodingCompAlgo encoding_algo;
113 uint8_t *encoding_settings;
114 int encoding_settings_len;
119 typedef struct MatroskaVideoTrack {
130 } MatroskaVideoTrack;
132 typedef struct MatroskaAudioTrack {
137 int internal_samplerate;
141 /* real audio header */
150 } MatroskaAudioTrack;
152 typedef struct MatroskaSubtitleTrack {
155 } MatroskaSubtitleTrack;
157 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
158 sizeof(MatroskaAudioTrack), \
159 sizeof(MatroskaSubtitleTrack)))
165 } MatroskaAttachement;
184 typedef struct MatroskaLevel {
189 typedef struct MatroskaDemuxContext {
190 AVFormatContext *ctx;
194 MatroskaLevel levels[EBML_MAX_DEPTH];
197 /* timescale in the file */
201 EbmlList attachments;
205 /* num_streams is the number of streams that av_new_stream() was called
206 * for ( = that are available to the calling program). */
209 MatroskaTrack *tracks[MAX_STREAMS];
211 /* cache for ID peeking */
214 /* byte position of the segment inside the stream */
215 offset_t segment_start;
217 /* The packet queue. */
221 /* have we already parse metadata/cues/clusters? */
226 /* What to skip before effectively reading a packet. */
227 int skip_to_keyframe;
228 AVStream *skip_to_stream;
229 } MatroskaDemuxContext;
231 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
233 static EbmlSyntax ebml_header[] = {
234 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
235 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
236 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
237 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
238 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
239 { EBML_ID_EBMLVERSION, EBML_NONE },
240 { EBML_ID_DOCTYPEVERSION, EBML_NONE },
241 { EBML_ID_VOID, EBML_NONE },
245 static EbmlSyntax ebml_syntax[] = {
246 { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
250 static EbmlSyntax matroska_info[] = {
251 { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
252 { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
253 { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
254 { MATROSKA_ID_WRITINGAPP, EBML_NONE },
255 { MATROSKA_ID_MUXINGAPP, EBML_NONE },
256 { MATROSKA_ID_DATEUTC, EBML_NONE },
257 { MATROSKA_ID_SEGMENTUID, EBML_NONE },
258 { EBML_ID_VOID, EBML_NONE },
262 static EbmlSyntax matroska_attachment[] = {
263 { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
264 { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
265 { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
266 { MATROSKA_ID_FILEUID, EBML_NONE },
267 { EBML_ID_VOID, EBML_NONE },
271 static EbmlSyntax matroska_attachments[] = {
272 { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
273 { EBML_ID_VOID, EBML_NONE },
277 static EbmlSyntax matroska_chapter_display[] = {
278 { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
279 { EBML_ID_VOID, EBML_NONE },
283 static EbmlSyntax matroska_chapter_entry[] = {
284 { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
285 { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
286 { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
287 { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
288 { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
289 { EBML_ID_VOID, EBML_NONE },
293 static EbmlSyntax matroska_chapter[] = {
294 { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
295 { MATROSKA_ID_EDITIONUID, EBML_NONE },
296 { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
297 { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
298 { EBML_ID_VOID, EBML_NONE },
302 static EbmlSyntax matroska_chapters[] = {
303 { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
304 { EBML_ID_VOID, EBML_NONE },
308 static EbmlSyntax matroska_index_pos[] = {
309 { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
310 { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
311 { EBML_ID_VOID, EBML_NONE },
315 static EbmlSyntax matroska_index_entry[] = {
316 { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
317 { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
318 { EBML_ID_VOID, EBML_NONE },
322 static EbmlSyntax matroska_index[] = {
323 { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
324 { EBML_ID_VOID, EBML_NONE },
328 static EbmlSyntax matroska_tags[] = {
329 { EBML_ID_VOID, EBML_NONE },
334 * The first few functions handle EBML file parsing. The rest
335 * is the document interpretation. Matroska really just is a
340 * Return: the amount of levels in the hierarchy that the
341 * current element lies higher than the previous one.
342 * The opposite isn't done - that's auto-done using master
347 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
349 ByteIOContext *pb = matroska->ctx->pb;
350 offset_t pos = url_ftell(pb);
353 while (matroska->num_levels > 0) {
354 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
356 if (pos >= level->start + level->length) {
357 matroska->num_levels--;
368 * Read: an "EBML number", which is defined as a variable-length
369 * array of bytes. The first byte indicates the length by giving a
370 * number of 0-bits followed by a one. The position of the first
371 * "one" bit inside the first byte indicates the length of this
373 * Returns: num. of bytes read. < 0 on error.
377 ebml_read_num (MatroskaDemuxContext *matroska,
381 ByteIOContext *pb = matroska->ctx->pb;
382 int len_mask = 0x80, read = 1, n = 1;
385 /* the first byte tells us the length in bytes - get_byte() can normally
386 * return 0, but since that's not a valid first ebmlID byte, we can
387 * use it safely here to catch EOS. */
388 if (!(total = get_byte(pb))) {
389 /* we might encounter EOS here */
391 offset_t pos = url_ftell(pb);
392 av_log(matroska->ctx, AV_LOG_ERROR,
393 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
396 return AVERROR(EIO); /* EOS or actual I/O error */
399 /* get the length of the EBML number */
400 while (read <= max_size && !(total & len_mask)) {
404 if (read > max_size) {
405 offset_t pos = url_ftell(pb) - 1;
406 av_log(matroska->ctx, AV_LOG_ERROR,
407 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
408 (uint8_t) total, pos, pos);
409 return AVERROR_INVALIDDATA;
412 /* read out length */
415 total = (total << 8) | get_byte(pb);
423 * Read: the element content data ID.
424 * Return: the number of bytes read or < 0 on error.
428 ebml_read_element_id (MatroskaDemuxContext *matroska,
435 /* if we re-call this, use our cached ID */
436 if (matroska->peek_id != 0) {
439 *id = matroska->peek_id;
443 /* read out the "EBML number", include tag in ID */
444 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
446 *id = matroska->peek_id = total | (1 << (read * 7));
450 *level_up = ebml_read_element_level_up(matroska);
456 * Read: element content length.
457 * Return: the number of bytes read or < 0 on error.
461 ebml_read_element_length (MatroskaDemuxContext *matroska,
464 /* clear cache since we're now beyond that data point */
465 matroska->peek_id = 0;
467 /* read out the "EBML number", include tag in ID */
468 return ebml_read_num(matroska, 8, length);
472 * Return: the ID of the next element, or 0 on error.
473 * Level_up contains the amount of levels that this
474 * next element lies higher than the previous one.
478 ebml_peek_id (MatroskaDemuxContext *matroska,
483 if (ebml_read_element_id(matroska, &id, level_up) < 0)
490 * Seek to a given offset.
491 * 0 is success, -1 is failure.
495 ebml_read_seek (MatroskaDemuxContext *matroska,
498 ByteIOContext *pb = matroska->ctx->pb;
500 /* clear ID cache, if any */
501 matroska->peek_id = 0;
503 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
507 * Skip the next element.
508 * 0 is success, -1 is failure.
512 ebml_read_skip (MatroskaDemuxContext *matroska)
514 ByteIOContext *pb = matroska->ctx->pb;
519 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
520 (res = ebml_read_element_length(matroska, &length)) < 0)
523 url_fskip(pb, length);
529 * Read the next element as an unsigned int.
530 * 0 is success, < 0 is failure.
534 ebml_read_uint (MatroskaDemuxContext *matroska,
538 ByteIOContext *pb = matroska->ctx->pb;
539 int n = 0, size, res;
542 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
543 (res = ebml_read_element_length(matroska, &rlength)) < 0)
546 if (size < 1 || size > 8) {
547 offset_t pos = url_ftell(pb);
548 av_log(matroska->ctx, AV_LOG_ERROR,
549 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
551 return AVERROR_INVALIDDATA;
554 /* big-endian ordening; build up number */
557 *num = (*num << 8) | get_byte(pb);
563 * Read the next element as a signed int.
564 * 0 is success, < 0 is failure.
568 ebml_read_sint (MatroskaDemuxContext *matroska,
572 ByteIOContext *pb = matroska->ctx->pb;
573 int size, n = 1, negative = 0, res;
576 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
577 (res = ebml_read_element_length(matroska, &rlength)) < 0)
580 if (size < 1 || size > 8) {
581 offset_t pos = url_ftell(pb);
582 av_log(matroska->ctx, AV_LOG_ERROR,
583 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
585 return AVERROR_INVALIDDATA;
587 if ((*num = get_byte(pb)) & 0x80) {
592 *num = (*num << 8) | get_byte(pb);
596 *num = *num - (1LL << ((8 * size) - 1));
602 * Read the next element as a float.
603 * 0 is success, < 0 is failure.
607 ebml_read_float (MatroskaDemuxContext *matroska,
611 ByteIOContext *pb = matroska->ctx->pb;
615 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
616 (res = ebml_read_element_length(matroska, &rlength)) < 0)
621 *num= av_int2flt(get_be32(pb));
623 *num= av_int2dbl(get_be64(pb));
625 offset_t pos = url_ftell(pb);
626 av_log(matroska->ctx, AV_LOG_ERROR,
627 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
629 return AVERROR_INVALIDDATA;
636 * Read the next element as an ASCII string.
637 * 0 is success, < 0 is failure.
641 ebml_read_ascii (MatroskaDemuxContext *matroska,
645 ByteIOContext *pb = matroska->ctx->pb;
649 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
650 (res = ebml_read_element_length(matroska, &rlength)) < 0)
654 /* ebml strings are usually not 0-terminated, so we allocate one
655 * byte more, read the string and NULL-terminate it ourselves. */
656 if (size < 0 || !(*str = av_malloc(size + 1))) {
657 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
658 return AVERROR(ENOMEM);
660 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
661 offset_t pos = url_ftell(pb);
662 av_log(matroska->ctx, AV_LOG_ERROR,
663 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
673 * Read the next element as a UTF-8 string.
674 * 0 is success, < 0 is failure.
678 ebml_read_utf8 (MatroskaDemuxContext *matroska,
682 return ebml_read_ascii(matroska, id, str);
686 * Read the next element, but only the header. The contents
687 * are supposed to be sub-elements which can be read separately.
688 * 0 is success, < 0 is failure.
692 ebml_read_master (MatroskaDemuxContext *matroska,
695 ByteIOContext *pb = matroska->ctx->pb;
697 MatroskaLevel *level;
700 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
701 (res = ebml_read_element_length(matroska, &length)) < 0)
704 /* protect... (Heaven forbids that the '>' is true) */
705 if (matroska->num_levels >= EBML_MAX_DEPTH) {
706 av_log(matroska->ctx, AV_LOG_ERROR,
707 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
708 return AVERROR(ENOSYS);
712 level = &matroska->levels[matroska->num_levels++];
713 level->start = url_ftell(pb);
714 level->length = length;
720 * Read the next element as binary data.
721 * 0 is success, < 0 is failure.
725 ebml_read_binary (MatroskaDemuxContext *matroska,
730 ByteIOContext *pb = matroska->ctx->pb;
734 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
735 (res = ebml_read_element_length(matroska, &rlength)) < 0)
739 if (!(*binary = av_malloc(*size))) {
740 av_log(matroska->ctx, AV_LOG_ERROR,
741 "Memory allocation error\n");
742 return AVERROR(ENOMEM);
745 if (get_buffer(pb, *binary, *size) != *size) {
746 offset_t pos = url_ftell(pb);
747 av_log(matroska->ctx, AV_LOG_ERROR,
748 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
756 * Read signed/unsigned "EBML" numbers.
757 * Return: number of bytes processed, < 0 on error.
758 * XXX: use ebml_read_num().
762 matroska_ebmlnum_uint (uint8_t *data,
766 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
770 return AVERROR_INVALIDDATA;
773 while (read <= 8 && !(total & len_mask)) {
778 return AVERROR_INVALIDDATA;
780 if ((total &= (len_mask - 1)) == len_mask - 1)
783 return AVERROR_INVALIDDATA;
787 total = (total << 8) | data[n];
800 * Same as above, but signed.
804 matroska_ebmlnum_sint (uint8_t *data,
811 /* read as unsigned number first */
812 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
815 /* make signed (weird way) */
816 if (unum == (uint64_t)-1)
819 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
825 static MatroskaTrack *
826 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
831 for (i = 0; i < matroska->num_tracks; i++)
832 if (matroska->tracks[i]->num == num)
833 return matroska->tracks[i];
835 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
841 * Put one packet in an application-supplied AVPacket struct.
842 * Returns 0 on success or -1 on failure.
846 matroska_deliver_packet (MatroskaDemuxContext *matroska,
849 if (matroska->num_packets > 0) {
850 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
851 av_free(matroska->packets[0]);
852 if (matroska->num_packets > 1) {
853 memmove(&matroska->packets[0], &matroska->packets[1],
854 (matroska->num_packets - 1) * sizeof(AVPacket *));
856 av_realloc(matroska->packets, (matroska->num_packets - 1) *
859 av_freep(&matroska->packets);
861 matroska->num_packets--;
869 * Put a packet into our internal queue. Will be delivered to the
870 * user/application during the next get_packet() call.
874 matroska_queue_packet (MatroskaDemuxContext *matroska,
878 av_realloc(matroska->packets, (matroska->num_packets + 1) *
880 matroska->packets[matroska->num_packets] = pkt;
881 matroska->num_packets++;
885 * Free all packets in our internal queue.
888 matroska_clear_queue (MatroskaDemuxContext *matroska)
890 if (matroska->packets) {
892 for (n = 0; n < matroska->num_packets; n++) {
893 av_free_packet(matroska->packets[n]);
894 av_free(matroska->packets[n]);
896 av_free(matroska->packets);
897 matroska->packets = NULL;
898 matroska->num_packets = 0;
908 matroska_probe (AVProbeData *p)
911 int len_mask = 0x80, size = 1, n = 1;
912 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
915 if (AV_RB32(p->buf) != EBML_ID_HEADER)
918 /* length of header */
920 while (size <= 8 && !(total & len_mask)) {
926 total &= (len_mask - 1);
928 total = (total << 8) | p->buf[4 + n++];
930 /* does the probe data contain the whole header? */
931 if (p->buf_size < 4 + size + total)
934 /* the header must contain the document type 'matroska'. For now,
935 * we don't parse the whole header but simply check for the
936 * availability of that array of characters inside the header.
937 * Not fully fool-proof, but good enough. */
938 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
939 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
940 return AVPROBE_SCORE_MAX;
946 * From here on, it's all XML-style DTD stuff... Needs no comments.
949 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
950 void *data, uint32_t expected_id, int once);
952 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
953 EbmlSyntax *syntax, void *data)
955 uint32_t id = syntax->id;
959 data = (char *)data + syntax->data_offset;
960 if (syntax->list_elem_size) {
961 EbmlList *list = data;
962 list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
963 data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
964 memset(data, 0, syntax->list_elem_size);
969 switch (syntax->type) {
970 case EBML_UINT: return ebml_read_uint (matroska, &id, data);
971 case EBML_FLOAT: return ebml_read_float(matroska, &id, data);
973 case EBML_UTF8: av_free(*(char **)data);
974 return ebml_read_ascii(matroska, &id, data);
975 case EBML_BIN: av_free(bin->data);
976 bin->pos = url_ftell(matroska->ctx->pb);
977 return ebml_read_binary(matroska, &id, &bin->data,
979 case EBML_NEST: if ((res=ebml_read_master(matroska, &id)) < 0)
981 if (id == MATROSKA_ID_SEGMENT)
982 matroska->segment_start = url_ftell(matroska->ctx->pb);
983 return ebml_parse(matroska, syntax->def.n, data, 0, 0);
984 case EBML_PASS: return ebml_parse(matroska, syntax->def.n, data, 0, 1);
985 case EBML_STOP: *(int *)data = 1; return 1;
986 default: return ebml_read_skip(matroska);
990 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
991 uint32_t id, void *data)
994 for (i=0; syntax[i].id; i++)
995 if (id == syntax[i].id)
998 av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
999 return ebml_parse_elem(matroska, &syntax[i], data);
1002 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
1003 void *data, uint32_t expected_id, int once)
1008 for (i=0; syntax[i].id; i++)
1009 switch (syntax[i].type) {
1011 *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
1014 *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
1018 *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
1023 res = ebml_read_master(matroska, &id);
1024 if (id != expected_id)
1025 return AVERROR_INVALIDDATA;
1026 if (id == MATROSKA_ID_SEGMENT)
1027 matroska->segment_start = url_ftell(matroska->ctx->pb);
1031 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1034 } else if (matroska->level_up) {
1035 matroska->level_up--;
1039 res = ebml_parse_id(matroska, syntax, id, data);
1044 if (matroska->level_up) {
1045 matroska->level_up--;
1053 static void ebml_free(EbmlSyntax *syntax, void *data)
1056 for (i=0; syntax[i].id; i++) {
1057 void *data_off = (char *)data + syntax[i].data_offset;
1058 switch (syntax[i].type) {
1060 case EBML_UTF8: av_freep(data_off); break;
1061 case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
1063 if (syntax[i].list_elem_size) {
1064 EbmlList *list = data_off;
1065 char *ptr = list->elem;
1066 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
1067 ebml_free(syntax[i].def.n, ptr);
1068 av_free(list->elem);
1070 ebml_free(syntax[i].def.n, data_off);
1077 matroska_parse_info (MatroskaDemuxContext *matroska)
1079 int res = ebml_parse(matroska, matroska_info, matroska, MATROSKA_ID_INFO, 0);
1081 if (matroska->duration)
1082 matroska->ctx->duration = matroska->duration * matroska->time_scale
1083 * 1000 / AV_TIME_BASE;
1084 if (matroska->title)
1085 strncpy(matroska->ctx->title, matroska->title,
1086 sizeof(matroska->ctx->title)-1);
1092 matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
1094 uint8_t* data = *buf;
1095 int isize = *buf_size;
1096 uint8_t* pkt_data = NULL;
1097 int pkt_size = isize;
1101 switch (track->encoding_algo) {
1102 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
1103 return track->encoding_settings_len;
1104 case MATROSKA_TRACK_ENCODING_COMP_LZO:
1106 olen = pkt_size *= 3;
1107 pkt_data = av_realloc(pkt_data,
1108 pkt_size+LZO_OUTPUT_PADDING);
1109 result = lzo1x_decode(pkt_data, &olen, data, &isize);
1110 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
1116 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1117 z_stream zstream = {0};
1118 if (inflateInit(&zstream) != Z_OK)
1120 zstream.next_in = data;
1121 zstream.avail_in = isize;
1124 pkt_data = av_realloc(pkt_data, pkt_size);
1125 zstream.avail_out = pkt_size - zstream.total_out;
1126 zstream.next_out = pkt_data + zstream.total_out;
1127 result = inflate(&zstream, Z_NO_FLUSH);
1128 } while (result==Z_OK && pkt_size<10000000);
1129 pkt_size = zstream.total_out;
1130 inflateEnd(&zstream);
1131 if (result != Z_STREAM_END)
1137 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1138 bz_stream bzstream = {0};
1139 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1141 bzstream.next_in = data;
1142 bzstream.avail_in = isize;
1145 pkt_data = av_realloc(pkt_data, pkt_size);
1146 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1147 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1148 result = BZ2_bzDecompress(&bzstream);
1149 } while (result==BZ_OK && pkt_size<10000000);
1150 pkt_size = bzstream.total_out_lo32;
1151 BZ2_bzDecompressEnd(&bzstream);
1152 if (result != BZ_STREAM_END)
1160 *buf_size = pkt_size;
1168 matroska_add_stream (MatroskaDemuxContext *matroska)
1172 MatroskaTrack *track;
1174 /* start with the master */
1175 if ((res = ebml_read_master(matroska, &id)) < 0)
1178 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1180 /* Allocate a generic track. */
1181 track = av_mallocz(MAX_TRACK_SIZE);
1182 track->time_scale = 1.0;
1183 strcpy(track->language, "eng");
1185 /* try reading the trackentry headers */
1187 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1190 } else if (matroska->level_up > 0) {
1191 matroska->level_up--;
1196 /* track number (unique stream ID) */
1197 case MATROSKA_ID_TRACKNUMBER: {
1199 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1205 /* track UID (unique identifier) */
1206 case MATROSKA_ID_TRACKUID: {
1208 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1214 /* track type (video, audio, combined, subtitle, etc.) */
1215 case MATROSKA_ID_TRACKTYPE: {
1217 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1219 if (track->type && track->type != num) {
1220 av_log(matroska->ctx, AV_LOG_INFO,
1221 "More than one tracktype in an entry - skip\n");
1226 switch (track->type) {
1227 case MATROSKA_TRACK_TYPE_VIDEO:
1228 case MATROSKA_TRACK_TYPE_AUDIO:
1229 case MATROSKA_TRACK_TYPE_SUBTITLE:
1231 case MATROSKA_TRACK_TYPE_COMPLEX:
1232 case MATROSKA_TRACK_TYPE_LOGO:
1233 case MATROSKA_TRACK_TYPE_CONTROL:
1235 av_log(matroska->ctx, AV_LOG_INFO,
1236 "Unknown or unsupported track type 0x%x\n",
1238 track->type = MATROSKA_TRACK_TYPE_NONE;
1244 /* tracktype specific stuff for video */
1245 case MATROSKA_ID_TRACKVIDEO: {
1246 MatroskaVideoTrack *videotrack;
1248 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1249 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1250 av_log(matroska->ctx, AV_LOG_INFO,
1251 "video data in non-video track - ignoring\n");
1252 res = AVERROR_INVALIDDATA;
1254 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1256 videotrack = (MatroskaVideoTrack *)track;
1259 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1262 } else if (matroska->level_up > 0) {
1263 matroska->level_up--;
1268 /* fixme, this should be one-up, but I get it here */
1269 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1271 if ((res = ebml_read_uint (matroska, &id,
1274 track->default_duration = num;
1278 /* video framerate */
1279 case MATROSKA_ID_VIDEOFRAMERATE: {
1281 if ((res = ebml_read_float(matroska, &id,
1284 if (!track->default_duration)
1285 track->default_duration = 1000000000/num;
1289 /* width of the size to display the video at */
1290 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1292 if ((res = ebml_read_uint(matroska, &id,
1295 videotrack->display_width = num;
1299 /* height of the size to display the video at */
1300 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1302 if ((res = ebml_read_uint(matroska, &id,
1305 videotrack->display_height = num;
1309 /* width of the video in the file */
1310 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1312 if ((res = ebml_read_uint(matroska, &id,
1315 videotrack->pixel_width = num;
1319 /* height of the video in the file */
1320 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1322 if ((res = ebml_read_uint(matroska, &id,
1325 videotrack->pixel_height = num;
1329 /* whether the video is interlaced */
1330 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1332 if ((res = ebml_read_uint(matroska, &id,
1338 /* colorspace (only matters for raw video)
1340 case MATROSKA_ID_VIDEOCOLORSPACE: {
1342 if ((res = ebml_read_uint(matroska, &id,
1345 videotrack->fourcc = num;
1350 av_log(matroska->ctx, AV_LOG_INFO,
1351 "Unknown video track header entry "
1352 "0x%x - ignoring\n", id);
1355 case MATROSKA_ID_VIDEOSTEREOMODE:
1356 case MATROSKA_ID_VIDEOASPECTRATIO:
1358 res = ebml_read_skip(matroska);
1362 if (matroska->level_up) {
1363 matroska->level_up--;
1370 /* tracktype specific stuff for audio */
1371 case MATROSKA_ID_TRACKAUDIO: {
1372 MatroskaAudioTrack *audiotrack;
1374 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1375 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1376 av_log(matroska->ctx, AV_LOG_INFO,
1377 "audio data in non-audio track - ignoring\n");
1378 res = AVERROR_INVALIDDATA;
1380 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1382 audiotrack = (MatroskaAudioTrack *)track;
1383 audiotrack->channels = 1;
1384 audiotrack->samplerate = 8000;
1387 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1390 } else if (matroska->level_up > 0) {
1391 matroska->level_up--;
1397 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1399 if ((res = ebml_read_float(matroska, &id,
1402 audiotrack->internal_samplerate =
1403 audiotrack->samplerate = num;
1407 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1409 if ((res = ebml_read_float(matroska, &id,
1412 audiotrack->samplerate = num;
1417 case MATROSKA_ID_AUDIOBITDEPTH: {
1419 if ((res = ebml_read_uint(matroska, &id,
1422 audiotrack->bitdepth = num;
1427 case MATROSKA_ID_AUDIOCHANNELS: {
1429 if ((res = ebml_read_uint(matroska, &id,
1432 audiotrack->channels = num;
1437 av_log(matroska->ctx, AV_LOG_INFO,
1438 "Unknown audio track header entry "
1439 "0x%x - ignoring\n", id);
1443 res = ebml_read_skip(matroska);
1447 if (matroska->level_up) {
1448 matroska->level_up--;
1455 /* codec identifier */
1456 case MATROSKA_ID_CODECID: {
1458 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1460 track->codec_id = text;
1464 /* codec private data */
1465 case MATROSKA_ID_CODECPRIVATE: {
1468 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1470 track->codec_priv = data;
1471 track->codec_priv_size = size;
1475 /* name of this track */
1476 case MATROSKA_ID_TRACKNAME: {
1478 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1484 /* language (matters for audio/subtitles, mostly) */
1485 case MATROSKA_ID_TRACKLANGUAGE: {
1487 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1489 if ((end = strchr(text, '-')))
1491 if (strlen(text) == 3)
1492 strcpy(track->language, text);
1497 /* whether this is actually used */
1498 case MATROSKA_ID_TRACKFLAGENABLED: {
1500 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1505 /* whether it's the default for this track type */
1506 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1508 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1510 track->flag_default = num;
1514 /* lacing (like MPEG, where blocks don't end/start on frame
1516 case MATROSKA_ID_TRACKFLAGLACING: {
1518 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1523 /* default length (in time) of one data block in this track */
1524 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1526 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1528 track->default_duration = num;
1532 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1533 if ((res = ebml_read_master(matroska, &id)) < 0)
1537 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1540 } else if (matroska->level_up > 0) {
1541 matroska->level_up--;
1546 case MATROSKA_ID_TRACKCONTENTENCODING: {
1547 int encoding_scope = 1;
1548 if ((res = ebml_read_master(matroska, &id)) < 0)
1552 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1555 } else if (matroska->level_up > 0) {
1556 matroska->level_up--;
1561 case MATROSKA_ID_ENCODINGSCOPE: {
1563 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1565 encoding_scope = num;
1569 case MATROSKA_ID_ENCODINGTYPE: {
1571 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1574 av_log(matroska->ctx, AV_LOG_ERROR,
1575 "Unsupported encoding type");
1579 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1580 if ((res = ebml_read_master(matroska, &id)) < 0)
1584 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1587 } else if (matroska->level_up > 0) {
1588 matroska->level_up--;
1593 case MATROSKA_ID_ENCODINGCOMPALGO: {
1595 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1597 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1599 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1602 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1604 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1605 av_log(matroska->ctx, AV_LOG_ERROR,
1606 "Unsupported compression algo\n");
1607 track->encoding_algo = num;
1611 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1614 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1616 track->encoding_settings = data;
1617 track->encoding_settings_len = size;
1622 av_log(matroska->ctx, AV_LOG_INFO,
1623 "Unknown compression header entry "
1624 "0x%x - ignoring\n", id);
1628 res = ebml_read_skip(matroska);
1632 if (matroska->level_up) {
1633 matroska->level_up--;
1641 av_log(matroska->ctx, AV_LOG_INFO,
1642 "Unknown content encoding header entry "
1643 "0x%x - ignoring\n", id);
1647 res = ebml_read_skip(matroska);
1651 if (matroska->level_up) {
1652 matroska->level_up--;
1657 track->encoding_scope = encoding_scope;
1662 av_log(matroska->ctx, AV_LOG_INFO,
1663 "Unknown content encodings header entry "
1664 "0x%x - ignoring\n", id);
1668 res = ebml_read_skip(matroska);
1672 if (matroska->level_up) {
1673 matroska->level_up--;
1680 case MATROSKA_ID_TRACKTIMECODESCALE: {
1682 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1684 track->time_scale = num;
1689 av_log(matroska->ctx, AV_LOG_INFO,
1690 "Unknown track header entry 0x%x - ignoring\n", id);
1694 /* we ignore these because they're nothing useful. */
1695 case MATROSKA_ID_TRACKFLAGFORCED:
1696 case MATROSKA_ID_CODECNAME:
1697 case MATROSKA_ID_CODECDECODEALL:
1698 case MATROSKA_ID_CODECINFOURL:
1699 case MATROSKA_ID_CODECDOWNLOADURL:
1700 case MATROSKA_ID_TRACKMINCACHE:
1701 case MATROSKA_ID_TRACKMAXCACHE:
1702 res = ebml_read_skip(matroska);
1706 if (matroska->level_up) {
1707 matroska->level_up--;
1712 if (track->codec_priv_size && track->encoding_scope & 2) {
1713 uint8_t *orig_priv = track->codec_priv;
1714 int offset = matroska_decode_buffer(&track->codec_priv,
1715 &track->codec_priv_size, track);
1717 track->codec_priv = av_malloc(track->codec_priv_size + offset);
1718 memcpy(track->codec_priv, track->encoding_settings, offset);
1719 memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1720 track->codec_priv_size += offset;
1722 } else if (!offset) {
1725 av_log(matroska->ctx, AV_LOG_ERROR,
1726 "Failed to decode codec private data\n");
1729 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1730 matroska->tracks[matroska->num_tracks++] = track;
1738 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1743 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1746 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1749 } else if (matroska->level_up) {
1750 matroska->level_up--;
1755 /* one track within the "all-tracks" header */
1756 case MATROSKA_ID_TRACKENTRY:
1757 res = matroska_add_stream(matroska);
1761 av_log(matroska->ctx, AV_LOG_INFO,
1762 "Unknown entry 0x%x in track header\n", id);
1766 res = ebml_read_skip(matroska);
1770 if (matroska->level_up) {
1771 matroska->level_up--;
1780 matroska_parse_index (MatroskaDemuxContext *matroska)
1782 return ebml_parse(matroska, matroska_index, matroska, MATROSKA_ID_CUES, 0);
1786 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1788 return ebml_parse(matroska, matroska_tags, matroska, MATROSKA_ID_TAGS, 0);
1792 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1797 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1800 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1803 } else if (matroska->level_up) {
1804 matroska->level_up--;
1809 case MATROSKA_ID_SEEKENTRY: {
1810 uint32_t seek_id = 0, peek_id_cache = 0;
1811 uint64_t seek_pos = (uint64_t) -1, t;
1812 int dummy_level = 0;
1814 if ((res = ebml_read_master(matroska, &id)) < 0)
1818 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1821 } else if (matroska->level_up) {
1822 matroska->level_up--;
1827 case MATROSKA_ID_SEEKID:
1828 res = ebml_read_uint(matroska, &id, &t);
1832 case MATROSKA_ID_SEEKPOSITION:
1833 res = ebml_read_uint(matroska, &id, &seek_pos);
1837 av_log(matroska->ctx, AV_LOG_INFO,
1838 "Unknown seekhead ID 0x%x\n", id);
1842 res = ebml_read_skip(matroska);
1846 if (matroska->level_up) {
1847 matroska->level_up--;
1852 if (!seek_id || seek_pos == (uint64_t) -1) {
1853 av_log(matroska->ctx, AV_LOG_INFO,
1854 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1860 case MATROSKA_ID_CUES:
1861 case MATROSKA_ID_TAGS: {
1862 uint32_t level_up = matroska->level_up;
1863 offset_t before_pos;
1865 MatroskaLevel level;
1867 /* remember the peeked ID and the current position */
1868 peek_id_cache = matroska->peek_id;
1869 before_pos = url_ftell(matroska->ctx->pb);
1872 if ((res = ebml_read_seek(matroska, seek_pos +
1873 matroska->segment_start)) < 0)
1876 /* we don't want to lose our seekhead level, so we add
1877 * a dummy. This is a crude hack. */
1878 if (matroska->num_levels == EBML_MAX_DEPTH) {
1879 av_log(matroska->ctx, AV_LOG_INFO,
1880 "Max EBML element depth (%d) reached, "
1881 "cannot parse further.\n", EBML_MAX_DEPTH);
1882 return AVERROR_UNKNOWN;
1886 level.length = (uint64_t)-1;
1887 matroska->levels[matroska->num_levels] = level;
1888 matroska->num_levels++;
1892 if (!(id = ebml_peek_id (matroska,
1893 &matroska->level_up)))
1895 if (id != seek_id) {
1896 av_log(matroska->ctx, AV_LOG_INFO,
1897 "We looked for ID=0x%x but got "
1898 "ID=0x%x (pos=%"PRIu64")",
1899 seek_id, id, seek_pos +
1900 matroska->segment_start);
1904 /* read master + parse */
1906 case MATROSKA_ID_CUES:
1907 if (!(res = matroska_parse_index(matroska)) ||
1908 url_feof(matroska->ctx->pb)) {
1909 matroska->index_parsed = 1;
1913 case MATROSKA_ID_TAGS:
1914 if (!(res = matroska_parse_metadata(matroska)) ||
1915 url_feof(matroska->ctx->pb)) {
1916 matroska->metadata_parsed = 1;
1923 /* remove dummy level */
1925 while (matroska->num_levels) {
1926 matroska->num_levels--;
1928 matroska->levels[matroska->num_levels].length;
1929 if (length == (uint64_t)-1)
1934 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1936 matroska->peek_id = peek_id_cache;
1937 matroska->level_up = level_up;
1942 av_log(matroska->ctx, AV_LOG_INFO,
1943 "Ignoring seekhead entry for ID=0x%x\n",
1952 av_log(matroska->ctx, AV_LOG_INFO,
1953 "Unknown seekhead ID 0x%x\n", id);
1957 res = ebml_read_skip(matroska);
1961 if (matroska->level_up) {
1962 matroska->level_up--;
1971 matroska_parse_attachments(AVFormatContext *s)
1973 MatroskaDemuxContext *matroska = s->priv_data;
1974 EbmlList *attachements_list = &matroska->attachments;
1975 MatroskaAttachement *attachements;
1978 res = ebml_parse(matroska, matroska_attachments, matroska, MATROSKA_ID_ATTACHMENTS, 0);
1980 attachements = attachements_list->elem;
1981 for (j=0; j<attachements_list->nb_elem; j++) {
1982 if (!(attachements[j].filename && attachements[j].mime &&
1983 attachements[j].bin.data && attachements[j].bin.size > 0)) {
1984 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
1986 AVStream *st = av_new_stream(s, matroska->num_streams++);
1989 st->filename = av_strdup(attachements[j].filename);
1990 st->codec->codec_id = CODEC_ID_NONE;
1991 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
1992 st->codec->extradata = av_malloc(attachements[j].bin.size);
1993 if(st->codec->extradata == NULL)
1995 st->codec->extradata_size = attachements[j].bin.size;
1996 memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
1998 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
1999 if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
2000 strlen(ff_mkv_mime_tags[i].str))) {
2001 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2012 matroska_parse_chapters(AVFormatContext *s)
2014 MatroskaDemuxContext *matroska = s->priv_data;
2015 EbmlList *chapters_list = &matroska->chapters;
2016 MatroskaChapter *chapters;
2019 res = ebml_parse(matroska, matroska_chapters, matroska, MATROSKA_ID_CHAPTERS, 0);
2021 chapters = chapters_list->elem;
2022 for (i=0; i<chapters_list->nb_elem; i++)
2023 if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid)
2024 ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
2025 chapters[i].start, chapters[i].end,
2032 matroska_aac_profile (char *codec_id)
2034 static const char *aac_profiles[] = {
2039 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2040 if (strstr(codec_id, aac_profiles[profile]))
2046 matroska_aac_sri (int samplerate)
2050 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2051 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2057 matroska_read_header (AVFormatContext *s,
2058 AVFormatParameters *ap)
2060 MatroskaDemuxContext *matroska = s->priv_data;
2061 EbmlList *index_list;
2062 MatroskaIndex *index;
2063 int i, j, last_level, res = 0;
2069 /* First read the EBML header. */
2070 if (ebml_parse(matroska, ebml_syntax, &ebml, 0, 1)
2071 || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
2072 || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
2073 || ebml.doctype_version > 2) {
2074 av_log(matroska->ctx, AV_LOG_ERROR,
2075 "EBML header using unsupported features\n"
2076 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
2077 ebml.version, ebml.doctype, ebml.doctype_version);
2078 return AVERROR_NOFMT;
2080 ebml_free(ebml_syntax, &ebml);
2082 /* The next thing is a segment. */
2084 if (!(id = ebml_peek_id(matroska, &last_level)))
2085 return AVERROR(EIO);
2086 if (id == MATROSKA_ID_SEGMENT)
2090 av_log(matroska->ctx, AV_LOG_INFO,
2091 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2092 MATROSKA_ID_SEGMENT, id);
2093 if ((res = ebml_read_skip(matroska)) < 0)
2097 /* We now have a Matroska segment.
2098 * Seeks are from the beginning of the segment,
2099 * after the segment ID/length. */
2100 if ((res = ebml_read_master(matroska, &id)) < 0)
2102 matroska->segment_start = url_ftell(s->pb);
2104 matroska->time_scale = 1000000;
2105 /* we've found our segment, start reading the different contents in here */
2107 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2110 } else if (matroska->level_up) {
2111 matroska->level_up--;
2117 case MATROSKA_ID_INFO: {
2118 res = matroska_parse_info(matroska);
2122 /* track info headers */
2123 case MATROSKA_ID_TRACKS: {
2124 if ((res = ebml_read_master(matroska, &id)) < 0)
2126 res = matroska_parse_tracks(matroska);
2131 case MATROSKA_ID_CUES: {
2132 if (!matroska->index_parsed) {
2133 res = matroska_parse_index(matroska);
2135 res = ebml_read_skip(matroska);
2140 case MATROSKA_ID_TAGS: {
2141 if (!matroska->metadata_parsed) {
2142 res = matroska_parse_metadata(matroska);
2144 res = ebml_read_skip(matroska);
2148 /* file index (if seekable, seek to Cues/Tags to parse it) */
2149 case MATROSKA_ID_SEEKHEAD: {
2150 if ((res = ebml_read_master(matroska, &id)) < 0)
2152 res = matroska_parse_seekhead(matroska);
2156 case MATROSKA_ID_ATTACHMENTS: {
2157 res = matroska_parse_attachments(s);
2161 case MATROSKA_ID_CLUSTER: {
2162 /* Do not read the master - this will be done in the next
2163 * call to matroska_read_packet. */
2168 case MATROSKA_ID_CHAPTERS: {
2169 res = matroska_parse_chapters(s);
2174 av_log(matroska->ctx, AV_LOG_INFO,
2175 "Unknown matroska file header ID 0x%x\n", id);
2179 res = ebml_read_skip(matroska);
2183 if (matroska->level_up) {
2184 matroska->level_up--;
2189 /* Have we found a cluster? */
2190 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2192 MatroskaTrack *track;
2195 for (i = 0; i < matroska->num_tracks; i++) {
2196 enum CodecID codec_id = CODEC_ID_NONE;
2197 uint8_t *extradata = NULL;
2198 int extradata_size = 0;
2199 int extradata_offset = 0;
2200 track = matroska->tracks[i];
2202 /* Apply some sanity checks. */
2203 if (track->codec_id == NULL)
2206 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2207 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2208 strlen(ff_mkv_codec_tags[j].str))){
2209 codec_id= ff_mkv_codec_tags[j].id;
2214 st = track->stream = av_new_stream(s, matroska->num_streams++);
2216 return AVERROR(ENOMEM);
2218 /* Set the FourCC from the CodecID. */
2219 /* This is the MS compatibility mode which stores a
2220 * BITMAPINFOHEADER in the CodecPrivate. */
2221 if (!strcmp(track->codec_id,
2222 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2223 (track->codec_priv_size >= 40) &&
2224 (track->codec_priv != NULL)) {
2225 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2227 /* Offset of biCompression. Stored in LE. */
2228 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2229 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2233 /* This is the MS compatibility mode which stores a
2234 * WAVEFORMATEX in the CodecPrivate. */
2235 else if (!strcmp(track->codec_id,
2236 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2237 (track->codec_priv_size >= 18) &&
2238 (track->codec_priv != NULL)) {
2241 /* Offset of wFormatTag. Stored in LE. */
2242 tag = AV_RL16(track->codec_priv);
2243 codec_id = codec_get_id(codec_wav_tags, tag);
2247 if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2248 (track->codec_priv_size >= 86) &&
2249 (track->codec_priv != NULL)) {
2250 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2252 vtrack->fourcc = AV_RL32(track->codec_priv);
2253 codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2256 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2257 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2258 int profile = matroska_aac_profile(track->codec_id);
2259 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2260 extradata = av_malloc(5);
2261 if (extradata == NULL)
2262 return AVERROR(ENOMEM);
2263 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2264 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2265 if (strstr(track->codec_id, "SBR")) {
2266 sri = matroska_aac_sri(audiotrack->samplerate);
2267 extradata[2] = 0x56;
2268 extradata[3] = 0xE5;
2269 extradata[4] = 0x80 | (sri<<3);
2276 else if (codec_id == CODEC_ID_TTA) {
2277 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2279 extradata_size = 30;
2280 extradata = av_mallocz(extradata_size);
2281 if (extradata == NULL)
2282 return AVERROR(ENOMEM);
2283 init_put_byte(&b, extradata, extradata_size, 1,
2284 NULL, NULL, NULL, NULL);
2285 put_buffer(&b, "TTA1", 4);
2287 put_le16(&b, audiotrack->channels);
2288 put_le16(&b, audiotrack->bitdepth);
2289 put_le32(&b, audiotrack->samplerate);
2290 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2293 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2294 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2295 extradata_offset = 26;
2296 track->codec_priv_size -= extradata_offset;
2299 else if (codec_id == CODEC_ID_RA_144) {
2300 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2301 audiotrack->samplerate = 8000;
2302 audiotrack->channels = 1;
2305 else if (codec_id == CODEC_ID_RA_288 ||
2306 codec_id == CODEC_ID_COOK ||
2307 codec_id == CODEC_ID_ATRAC3) {
2308 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2311 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2312 NULL, NULL, NULL, NULL);
2314 audiotrack->coded_framesize = get_be32(&b);
2316 audiotrack->sub_packet_h = get_be16(&b);
2317 audiotrack->frame_size = get_be16(&b);
2318 audiotrack->sub_packet_size = get_be16(&b);
2319 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2320 if (codec_id == CODEC_ID_RA_288) {
2321 audiotrack->block_align = audiotrack->coded_framesize;
2322 track->codec_priv_size = 0;
2324 audiotrack->block_align = audiotrack->sub_packet_size;
2325 extradata_offset = 78;
2326 track->codec_priv_size -= extradata_offset;
2330 if (codec_id == CODEC_ID_NONE) {
2331 av_log(matroska->ctx, AV_LOG_INFO,
2332 "Unknown/unsupported CodecID %s.\n",
2336 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2338 st->codec->codec_id = codec_id;
2340 if (strcmp(track->language, "und"))
2341 av_strlcpy(st->language, track->language, 4);
2343 if (track->flag_default)
2344 st->disposition |= AV_DISPOSITION_DEFAULT;
2346 if (track->default_duration)
2347 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2348 track->default_duration, 1000000000, 30000);
2351 st->codec->extradata = extradata;
2352 st->codec->extradata_size = extradata_size;
2353 } else if(track->codec_priv && track->codec_priv_size > 0){
2354 st->codec->extradata = av_malloc(track->codec_priv_size);
2355 if(st->codec->extradata == NULL)
2356 return AVERROR(ENOMEM);
2357 st->codec->extradata_size = track->codec_priv_size;
2358 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2359 track->codec_priv_size);
2362 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2363 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2365 st->codec->codec_type = CODEC_TYPE_VIDEO;
2366 st->codec->codec_tag = videotrack->fourcc;
2367 st->codec->width = videotrack->pixel_width;
2368 st->codec->height = videotrack->pixel_height;
2369 if (videotrack->display_width == 0)
2370 videotrack->display_width= videotrack->pixel_width;
2371 if (videotrack->display_height == 0)
2372 videotrack->display_height= videotrack->pixel_height;
2373 av_reduce(&st->codec->sample_aspect_ratio.num,
2374 &st->codec->sample_aspect_ratio.den,
2375 st->codec->height * videotrack->display_width,
2376 st->codec-> width * videotrack->display_height,
2378 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2379 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2380 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2382 st->codec->codec_type = CODEC_TYPE_AUDIO;
2383 st->codec->sample_rate = audiotrack->samplerate;
2384 st->codec->channels = audiotrack->channels;
2385 st->codec->block_align = audiotrack->block_align;
2386 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2387 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2390 /* What do we do with private data? E.g. for Vorbis. */
2395 index_list = &matroska->index;
2396 index = index_list->elem;
2397 for (i=0; i<index_list->nb_elem; i++) {
2398 EbmlList *pos_list = &index[i].pos;
2399 MatroskaIndexPos *pos = pos_list->elem;
2400 for (j=0; j<pos_list->nb_elem; j++) {
2401 MatroskaTrack *track = matroska_find_track_by_num(matroska,
2403 if (track && track->stream)
2404 av_add_index_entry(track->stream,
2405 pos[j].pos + matroska->segment_start,
2406 index[i].time*matroska->time_scale/AV_TIME_BASE,
2407 0, 0, AVINDEX_KEYFRAME);
2415 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2416 int64_t pos, uint64_t cluster_time, uint64_t duration,
2419 MatroskaTrack *track;
2423 uint8_t *origdata = data;
2425 uint32_t *lace_size = NULL;
2426 int n, flags, laces = 0;
2429 /* first byte(s): tracknum */
2430 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2431 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2438 /* fetch track from num */
2439 track = matroska_find_track_by_num(matroska, num);
2440 if (size <= 3 || !track || !track->stream) {
2441 av_log(matroska->ctx, AV_LOG_INFO,
2442 "Invalid stream %"PRIu64" or size %u\n", num, size);
2447 if (st->discard >= AVDISCARD_ALL) {
2451 if (duration == AV_NOPTS_VALUE)
2452 duration = track->default_duration / matroska->time_scale;
2454 /* block_time (relative to cluster time) */
2455 block_time = AV_RB16(data);
2459 if (is_keyframe == -1)
2460 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2462 if (matroska->skip_to_keyframe) {
2463 if (!is_keyframe || st != matroska->skip_to_stream) {
2467 matroska->skip_to_keyframe = 0;
2470 switch ((flags & 0x06) >> 1) {
2471 case 0x0: /* no lacing */
2473 lace_size = av_mallocz(sizeof(int));
2474 lace_size[0] = size;
2477 case 0x1: /* xiph lacing */
2478 case 0x2: /* fixed-size lacing */
2479 case 0x3: /* EBML lacing */
2480 assert(size>0); // size <=3 is checked before size-=3 above
2481 laces = (*data) + 1;
2484 lace_size = av_mallocz(laces * sizeof(int));
2486 switch ((flags & 0x06) >> 1) {
2487 case 0x1: /* xiph lacing */ {
2490 for (n = 0; res == 0 && n < laces - 1; n++) {
2497 lace_size[n] += temp;
2503 total += lace_size[n];
2505 lace_size[n] = size - total;
2509 case 0x2: /* fixed-size lacing */
2510 for (n = 0; n < laces; n++)
2511 lace_size[n] = size / laces;
2514 case 0x3: /* EBML lacing */ {
2516 n = matroska_ebmlnum_uint(data, size, &num);
2518 av_log(matroska->ctx, AV_LOG_INFO,
2519 "EBML block data error\n");
2524 total = lace_size[0] = num;
2525 for (n = 1; res == 0 && n < laces - 1; n++) {
2528 r = matroska_ebmlnum_sint (data, size, &snum);
2530 av_log(matroska->ctx, AV_LOG_INFO,
2531 "EBML block data error\n");
2536 lace_size[n] = lace_size[n - 1] + snum;
2537 total += lace_size[n];
2539 lace_size[n] = size - total;
2547 uint64_t timecode = AV_NOPTS_VALUE;
2549 if (cluster_time != (uint64_t)-1
2550 && (block_time >= 0 || cluster_time >= -block_time))
2551 timecode = cluster_time + block_time;
2553 for (n = 0; n < laces; n++) {
2554 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2555 st->codec->codec_id == CODEC_ID_COOK ||
2556 st->codec->codec_id == CODEC_ID_ATRAC3) {
2557 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2558 int a = st->codec->block_align;
2559 int sps = audiotrack->sub_packet_size;
2560 int cfs = audiotrack->coded_framesize;
2561 int h = audiotrack->sub_packet_h;
2562 int y = audiotrack->sub_packet_cnt;
2563 int w = audiotrack->frame_size;
2566 if (!audiotrack->pkt_cnt) {
2567 if (st->codec->codec_id == CODEC_ID_RA_288)
2568 for (x=0; x<h/2; x++)
2569 memcpy(audiotrack->buf+x*2*w+y*cfs,
2572 for (x=0; x<w/sps; x++)
2573 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2575 if (++audiotrack->sub_packet_cnt >= h) {
2576 audiotrack->sub_packet_cnt = 0;
2577 audiotrack->pkt_cnt = h*w / a;
2580 while (audiotrack->pkt_cnt) {
2581 pkt = av_mallocz(sizeof(AVPacket));
2582 av_new_packet(pkt, a);
2583 memcpy(pkt->data, audiotrack->buf
2584 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2586 pkt->stream_index = st->index;
2587 matroska_queue_packet(matroska, pkt);
2590 int offset = 0, pkt_size = lace_size[n];
2591 uint8_t *pkt_data = data;
2593 if (track->encoding_scope & 1) {
2594 offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2600 pkt = av_mallocz(sizeof(AVPacket));
2601 /* XXX: prevent data copy... */
2602 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2604 res = AVERROR(ENOMEM);
2609 memcpy (pkt->data, track->encoding_settings, offset);
2610 memcpy (pkt->data+offset, pkt_data, pkt_size);
2612 if (pkt_data != data)
2616 pkt->flags = is_keyframe;
2617 pkt->stream_index = st->index;
2619 pkt->pts = timecode;
2621 pkt->duration = duration;
2623 matroska_queue_packet(matroska, pkt);
2626 if (timecode != AV_NOPTS_VALUE)
2627 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2628 data += lace_size[n];
2638 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2639 uint64_t cluster_time)
2643 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2644 uint64_t duration = AV_NOPTS_VALUE;
2649 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2652 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2655 } else if (matroska->level_up) {
2656 matroska->level_up--;
2661 /* one block inside the group. Note, block parsing is one
2662 * of the harder things, so this code is a bit complicated.
2663 * See http://www.matroska.org/ for documentation. */
2664 case MATROSKA_ID_BLOCK: {
2665 pos = url_ftell(matroska->ctx->pb);
2666 res = ebml_read_binary(matroska, &id, &data, &size);
2670 case MATROSKA_ID_BLOCKDURATION: {
2671 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2676 case MATROSKA_ID_BLOCKREFERENCE: {
2678 /* We've found a reference, so not even the first frame in
2679 * the lace is a key frame. */
2681 if (last_num_packets != matroska->num_packets)
2682 matroska->packets[last_num_packets]->flags = 0;
2683 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2689 av_log(matroska->ctx, AV_LOG_INFO,
2690 "Unknown entry 0x%x in blockgroup data\n", id);
2694 res = ebml_read_skip(matroska);
2698 if (matroska->level_up) {
2699 matroska->level_up--;
2708 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
2709 duration, is_keyframe);
2715 matroska_parse_cluster (MatroskaDemuxContext *matroska)
2719 uint64_t cluster_time = 0;
2724 av_log(matroska->ctx, AV_LOG_DEBUG,
2725 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
2728 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2731 } else if (matroska->level_up) {
2732 matroska->level_up--;
2737 /* cluster timecode */
2738 case MATROSKA_ID_CLUSTERTIMECODE: {
2740 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2746 /* a group of blocks inside a cluster */
2747 case MATROSKA_ID_BLOCKGROUP:
2748 if ((res = ebml_read_master(matroska, &id)) < 0)
2750 res = matroska_parse_blockgroup(matroska, cluster_time);
2753 case MATROSKA_ID_SIMPLEBLOCK:
2754 pos = url_ftell(matroska->ctx->pb);
2755 res = ebml_read_binary(matroska, &id, &data, &size);
2757 res = matroska_parse_block(matroska, data, size, pos,
2758 cluster_time, AV_NOPTS_VALUE,
2763 av_log(matroska->ctx, AV_LOG_INFO,
2764 "Unknown entry 0x%x in cluster data\n", id);
2768 res = ebml_read_skip(matroska);
2772 if (matroska->level_up) {
2773 matroska->level_up--;
2782 matroska_read_packet (AVFormatContext *s,
2785 MatroskaDemuxContext *matroska = s->priv_data;
2789 /* Read stream until we have a packet queued. */
2790 while (matroska_deliver_packet(matroska, pkt)) {
2792 /* Have we already reached the end? */
2794 return AVERROR(EIO);
2798 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2799 return AVERROR(EIO);
2800 } else if (matroska->level_up) {
2801 matroska->level_up--;
2806 case MATROSKA_ID_CLUSTER:
2807 if ((res = ebml_read_master(matroska, &id)) < 0)
2809 if ((res = matroska_parse_cluster(matroska)) == 0)
2810 res = 1; /* Parsed one cluster, let's get out. */
2815 res = ebml_read_skip(matroska);
2819 if (matroska->level_up) {
2820 matroska->level_up--;
2833 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
2836 MatroskaDemuxContext *matroska = s->priv_data;
2837 AVStream *st = s->streams[stream_index];
2840 /* find index entry */
2841 index = av_index_search_timestamp(st, timestamp, flags);
2845 matroska_clear_queue(matroska);
2848 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
2849 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
2850 matroska->skip_to_stream = st;
2851 matroska->peek_id = 0;
2852 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
2857 matroska_read_close (AVFormatContext *s)
2859 MatroskaDemuxContext *matroska = s->priv_data;
2862 matroska_clear_queue(matroska);
2864 for (n = 0; n < matroska->num_tracks; n++) {
2865 MatroskaTrack *track = matroska->tracks[n];
2866 av_free(track->codec_id);
2867 av_free(track->codec_priv);
2868 av_free(track->name);
2870 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2871 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2872 av_free(audiotrack->buf);
2877 ebml_free(matroska_index, matroska);
2882 AVInputFormat matroska_demuxer = {
2884 NULL_IF_CONFIG_SMALL("Matroska file format"),
2885 sizeof(MatroskaDemuxContext),
2887 matroska_read_header,
2888 matroska_read_packet,
2889 matroska_read_close,