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)))
161 typedef struct MatroskaLevel {
166 typedef struct MatroskaDemuxIndex {
167 uint64_t pos; /* of the corresponding *cluster*! */
168 uint16_t track; /* reference to 'num' */
169 uint64_t time; /* in nanoseconds */
170 } MatroskaDemuxIndex;
172 typedef struct MatroskaDemuxContext {
173 AVFormatContext *ctx;
177 MatroskaLevel levels[EBML_MAX_DEPTH];
180 /* timescale in the file */
183 /* num_streams is the number of streams that av_new_stream() was called
184 * for ( = that are available to the calling program). */
187 MatroskaTrack *tracks[MAX_STREAMS];
189 /* cache for ID peeking */
192 /* byte position of the segment inside the stream */
193 offset_t segment_start;
195 /* The packet queue. */
199 /* have we already parse metadata/cues/clusters? */
204 /* The index for seeking. */
206 MatroskaDemuxIndex *index;
208 /* What to skip before effectively reading a packet. */
209 int skip_to_keyframe;
210 AVStream *skip_to_stream;
211 } MatroskaDemuxContext;
213 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
215 static EbmlSyntax ebml_header[] = {
216 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
217 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
218 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
219 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
220 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
221 { EBML_ID_EBMLVERSION, EBML_NONE },
222 { EBML_ID_DOCTYPEVERSION, EBML_NONE },
223 { EBML_ID_VOID, EBML_NONE },
227 static EbmlSyntax ebml_syntax[] = {
228 { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
233 * The first few functions handle EBML file parsing. The rest
234 * is the document interpretation. Matroska really just is a
239 * Return: the amount of levels in the hierarchy that the
240 * current element lies higher than the previous one.
241 * The opposite isn't done - that's auto-done using master
246 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
248 ByteIOContext *pb = matroska->ctx->pb;
249 offset_t pos = url_ftell(pb);
252 while (matroska->num_levels > 0) {
253 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
255 if (pos >= level->start + level->length) {
256 matroska->num_levels--;
267 * Read: an "EBML number", which is defined as a variable-length
268 * array of bytes. The first byte indicates the length by giving a
269 * number of 0-bits followed by a one. The position of the first
270 * "one" bit inside the first byte indicates the length of this
272 * Returns: num. of bytes read. < 0 on error.
276 ebml_read_num (MatroskaDemuxContext *matroska,
280 ByteIOContext *pb = matroska->ctx->pb;
281 int len_mask = 0x80, read = 1, n = 1;
284 /* the first byte tells us the length in bytes - get_byte() can normally
285 * return 0, but since that's not a valid first ebmlID byte, we can
286 * use it safely here to catch EOS. */
287 if (!(total = get_byte(pb))) {
288 /* we might encounter EOS here */
290 offset_t pos = url_ftell(pb);
291 av_log(matroska->ctx, AV_LOG_ERROR,
292 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
295 return AVERROR(EIO); /* EOS or actual I/O error */
298 /* get the length of the EBML number */
299 while (read <= max_size && !(total & len_mask)) {
303 if (read > max_size) {
304 offset_t pos = url_ftell(pb) - 1;
305 av_log(matroska->ctx, AV_LOG_ERROR,
306 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
307 (uint8_t) total, pos, pos);
308 return AVERROR_INVALIDDATA;
311 /* read out length */
314 total = (total << 8) | get_byte(pb);
322 * Read: the element content data ID.
323 * Return: the number of bytes read or < 0 on error.
327 ebml_read_element_id (MatroskaDemuxContext *matroska,
334 /* if we re-call this, use our cached ID */
335 if (matroska->peek_id != 0) {
338 *id = matroska->peek_id;
342 /* read out the "EBML number", include tag in ID */
343 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
345 *id = matroska->peek_id = total | (1 << (read * 7));
349 *level_up = ebml_read_element_level_up(matroska);
355 * Read: element content length.
356 * Return: the number of bytes read or < 0 on error.
360 ebml_read_element_length (MatroskaDemuxContext *matroska,
363 /* clear cache since we're now beyond that data point */
364 matroska->peek_id = 0;
366 /* read out the "EBML number", include tag in ID */
367 return ebml_read_num(matroska, 8, length);
371 * Return: the ID of the next element, or 0 on error.
372 * Level_up contains the amount of levels that this
373 * next element lies higher than the previous one.
377 ebml_peek_id (MatroskaDemuxContext *matroska,
382 if (ebml_read_element_id(matroska, &id, level_up) < 0)
389 * Seek to a given offset.
390 * 0 is success, -1 is failure.
394 ebml_read_seek (MatroskaDemuxContext *matroska,
397 ByteIOContext *pb = matroska->ctx->pb;
399 /* clear ID cache, if any */
400 matroska->peek_id = 0;
402 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
406 * Skip the next element.
407 * 0 is success, -1 is failure.
411 ebml_read_skip (MatroskaDemuxContext *matroska)
413 ByteIOContext *pb = matroska->ctx->pb;
418 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
419 (res = ebml_read_element_length(matroska, &length)) < 0)
422 url_fskip(pb, length);
428 * Read the next element as an unsigned int.
429 * 0 is success, < 0 is failure.
433 ebml_read_uint (MatroskaDemuxContext *matroska,
437 ByteIOContext *pb = matroska->ctx->pb;
438 int n = 0, size, res;
441 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
442 (res = ebml_read_element_length(matroska, &rlength)) < 0)
445 if (size < 1 || size > 8) {
446 offset_t pos = url_ftell(pb);
447 av_log(matroska->ctx, AV_LOG_ERROR,
448 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
450 return AVERROR_INVALIDDATA;
453 /* big-endian ordening; build up number */
456 *num = (*num << 8) | get_byte(pb);
462 * Read the next element as a signed int.
463 * 0 is success, < 0 is failure.
467 ebml_read_sint (MatroskaDemuxContext *matroska,
471 ByteIOContext *pb = matroska->ctx->pb;
472 int size, n = 1, negative = 0, res;
475 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
476 (res = ebml_read_element_length(matroska, &rlength)) < 0)
479 if (size < 1 || size > 8) {
480 offset_t pos = url_ftell(pb);
481 av_log(matroska->ctx, AV_LOG_ERROR,
482 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
484 return AVERROR_INVALIDDATA;
486 if ((*num = get_byte(pb)) & 0x80) {
491 *num = (*num << 8) | get_byte(pb);
495 *num = *num - (1LL << ((8 * size) - 1));
501 * Read the next element as a float.
502 * 0 is success, < 0 is failure.
506 ebml_read_float (MatroskaDemuxContext *matroska,
510 ByteIOContext *pb = matroska->ctx->pb;
514 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
515 (res = ebml_read_element_length(matroska, &rlength)) < 0)
520 *num= av_int2flt(get_be32(pb));
522 *num= av_int2dbl(get_be64(pb));
524 offset_t pos = url_ftell(pb);
525 av_log(matroska->ctx, AV_LOG_ERROR,
526 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
528 return AVERROR_INVALIDDATA;
535 * Read the next element as an ASCII string.
536 * 0 is success, < 0 is failure.
540 ebml_read_ascii (MatroskaDemuxContext *matroska,
544 ByteIOContext *pb = matroska->ctx->pb;
548 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
549 (res = ebml_read_element_length(matroska, &rlength)) < 0)
553 /* ebml strings are usually not 0-terminated, so we allocate one
554 * byte more, read the string and NULL-terminate it ourselves. */
555 if (size < 0 || !(*str = av_malloc(size + 1))) {
556 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
557 return AVERROR(ENOMEM);
559 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
560 offset_t pos = url_ftell(pb);
561 av_log(matroska->ctx, AV_LOG_ERROR,
562 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
572 * Read the next element as a UTF-8 string.
573 * 0 is success, < 0 is failure.
577 ebml_read_utf8 (MatroskaDemuxContext *matroska,
581 return ebml_read_ascii(matroska, id, str);
585 * Read the next element, but only the header. The contents
586 * are supposed to be sub-elements which can be read separately.
587 * 0 is success, < 0 is failure.
591 ebml_read_master (MatroskaDemuxContext *matroska,
594 ByteIOContext *pb = matroska->ctx->pb;
596 MatroskaLevel *level;
599 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
600 (res = ebml_read_element_length(matroska, &length)) < 0)
603 /* protect... (Heaven forbids that the '>' is true) */
604 if (matroska->num_levels >= EBML_MAX_DEPTH) {
605 av_log(matroska->ctx, AV_LOG_ERROR,
606 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
607 return AVERROR(ENOSYS);
611 level = &matroska->levels[matroska->num_levels++];
612 level->start = url_ftell(pb);
613 level->length = length;
619 * Read the next element as binary data.
620 * 0 is success, < 0 is failure.
624 ebml_read_binary (MatroskaDemuxContext *matroska,
629 ByteIOContext *pb = matroska->ctx->pb;
633 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
634 (res = ebml_read_element_length(matroska, &rlength)) < 0)
638 if (!(*binary = av_malloc(*size))) {
639 av_log(matroska->ctx, AV_LOG_ERROR,
640 "Memory allocation error\n");
641 return AVERROR(ENOMEM);
644 if (get_buffer(pb, *binary, *size) != *size) {
645 offset_t pos = url_ftell(pb);
646 av_log(matroska->ctx, AV_LOG_ERROR,
647 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
655 * Read signed/unsigned "EBML" numbers.
656 * Return: number of bytes processed, < 0 on error.
657 * XXX: use ebml_read_num().
661 matroska_ebmlnum_uint (uint8_t *data,
665 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
669 return AVERROR_INVALIDDATA;
672 while (read <= 8 && !(total & len_mask)) {
677 return AVERROR_INVALIDDATA;
679 if ((total &= (len_mask - 1)) == len_mask - 1)
682 return AVERROR_INVALIDDATA;
686 total = (total << 8) | data[n];
699 * Same as above, but signed.
703 matroska_ebmlnum_sint (uint8_t *data,
710 /* read as unsigned number first */
711 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
714 /* make signed (weird way) */
715 if (unum == (uint64_t)-1)
718 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
725 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
730 for (i = 0; i < matroska->num_tracks; i++)
731 if (matroska->tracks[i]->num == num)
739 * Put one packet in an application-supplied AVPacket struct.
740 * Returns 0 on success or -1 on failure.
744 matroska_deliver_packet (MatroskaDemuxContext *matroska,
747 if (matroska->num_packets > 0) {
748 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
749 av_free(matroska->packets[0]);
750 if (matroska->num_packets > 1) {
751 memmove(&matroska->packets[0], &matroska->packets[1],
752 (matroska->num_packets - 1) * sizeof(AVPacket *));
754 av_realloc(matroska->packets, (matroska->num_packets - 1) *
757 av_freep(&matroska->packets);
759 matroska->num_packets--;
767 * Put a packet into our internal queue. Will be delivered to the
768 * user/application during the next get_packet() call.
772 matroska_queue_packet (MatroskaDemuxContext *matroska,
776 av_realloc(matroska->packets, (matroska->num_packets + 1) *
778 matroska->packets[matroska->num_packets] = pkt;
779 matroska->num_packets++;
783 * Free all packets in our internal queue.
786 matroska_clear_queue (MatroskaDemuxContext *matroska)
788 if (matroska->packets) {
790 for (n = 0; n < matroska->num_packets; n++) {
791 av_free_packet(matroska->packets[n]);
792 av_free(matroska->packets[n]);
794 av_free(matroska->packets);
795 matroska->packets = NULL;
796 matroska->num_packets = 0;
806 matroska_probe (AVProbeData *p)
809 int len_mask = 0x80, size = 1, n = 1;
810 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
813 if (AV_RB32(p->buf) != EBML_ID_HEADER)
816 /* length of header */
818 while (size <= 8 && !(total & len_mask)) {
824 total &= (len_mask - 1);
826 total = (total << 8) | p->buf[4 + n++];
828 /* does the probe data contain the whole header? */
829 if (p->buf_size < 4 + size + total)
832 /* the header must contain the document type 'matroska'. For now,
833 * we don't parse the whole header but simply check for the
834 * availability of that array of characters inside the header.
835 * Not fully fool-proof, but good enough. */
836 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
837 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
838 return AVPROBE_SCORE_MAX;
844 * From here on, it's all XML-style DTD stuff... Needs no comments.
847 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
848 void *data, uint32_t expected_id, int once);
850 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
851 EbmlSyntax *syntax, void *data)
853 uint32_t id = syntax->id;
857 data = (char *)data + syntax->data_offset;
858 if (syntax->list_elem_size) {
859 EbmlList *list = data;
860 list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
861 data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
862 memset(data, 0, syntax->list_elem_size);
867 switch (syntax->type) {
868 case EBML_UINT: return ebml_read_uint (matroska, &id, data);
869 case EBML_FLOAT: return ebml_read_float(matroska, &id, data);
871 case EBML_UTF8: av_free(*(char **)data);
872 return ebml_read_ascii(matroska, &id, data);
873 case EBML_BIN: av_free(bin->data);
874 bin->pos = url_ftell(matroska->ctx->pb);
875 return ebml_read_binary(matroska, &id, &bin->data,
877 case EBML_NEST: if ((res=ebml_read_master(matroska, &id)) < 0)
879 if (id == MATROSKA_ID_SEGMENT)
880 matroska->segment_start = url_ftell(matroska->ctx->pb);
881 return ebml_parse(matroska, syntax->def.n, data, 0, 0);
882 case EBML_PASS: return ebml_parse(matroska, syntax->def.n, data, 0, 1);
883 case EBML_STOP: *(int *)data = 1; return 1;
884 default: return ebml_read_skip(matroska);
888 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
889 uint32_t id, void *data)
892 for (i=0; syntax[i].id; i++)
893 if (id == syntax[i].id)
896 av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
897 return ebml_parse_elem(matroska, &syntax[i], data);
900 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
901 void *data, uint32_t expected_id, int once)
906 for (i=0; syntax[i].id; i++)
907 switch (syntax[i].type) {
909 *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
912 *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
916 *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
921 res = ebml_read_master(matroska, &id);
922 if (id != expected_id)
923 return AVERROR_INVALIDDATA;
924 if (id == MATROSKA_ID_SEGMENT)
925 matroska->segment_start = url_ftell(matroska->ctx->pb);
929 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
932 } else if (matroska->level_up) {
933 matroska->level_up--;
937 res = ebml_parse_id(matroska, syntax, id, data);
942 if (matroska->level_up) {
943 matroska->level_up--;
951 static void ebml_free(EbmlSyntax *syntax, void *data)
954 for (i=0; syntax[i].id; i++) {
955 void *data_off = (char *)data + syntax[i].data_offset;
956 switch (syntax[i].type) {
958 case EBML_UTF8: av_freep(data_off); break;
959 case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
961 if (syntax[i].list_elem_size) {
962 EbmlList *list = data_off;
963 char *ptr = list->elem;
964 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
965 ebml_free(syntax[i].def.n, ptr);
968 ebml_free(syntax[i].def.n, data_off);
975 matroska_parse_info (MatroskaDemuxContext *matroska)
980 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
983 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
986 } else if (matroska->level_up) {
987 matroska->level_up--;
992 /* cluster timecode */
993 case MATROSKA_ID_TIMECODESCALE: {
995 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
997 matroska->time_scale = num;
1001 case MATROSKA_ID_DURATION: {
1003 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1005 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1009 case MATROSKA_ID_TITLE: {
1011 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1013 strncpy(matroska->ctx->title, text,
1014 sizeof(matroska->ctx->title)-1);
1020 av_log(matroska->ctx, AV_LOG_INFO,
1021 "Unknown entry 0x%x in info header\n", id);
1024 case MATROSKA_ID_WRITINGAPP:
1025 case MATROSKA_ID_MUXINGAPP:
1026 case MATROSKA_ID_DATEUTC:
1027 case MATROSKA_ID_SEGMENTUID:
1029 res = ebml_read_skip(matroska);
1033 if (matroska->level_up) {
1034 matroska->level_up--;
1043 matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
1045 uint8_t* data = *buf;
1046 int isize = *buf_size;
1047 uint8_t* pkt_data = NULL;
1048 int pkt_size = isize;
1052 switch (track->encoding_algo) {
1053 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
1054 return track->encoding_settings_len;
1055 case MATROSKA_TRACK_ENCODING_COMP_LZO:
1057 olen = pkt_size *= 3;
1058 pkt_data = av_realloc(pkt_data,
1059 pkt_size+LZO_OUTPUT_PADDING);
1060 result = lzo1x_decode(pkt_data, &olen, data, &isize);
1061 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
1067 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1068 z_stream zstream = {0};
1069 if (inflateInit(&zstream) != Z_OK)
1071 zstream.next_in = data;
1072 zstream.avail_in = isize;
1075 pkt_data = av_realloc(pkt_data, pkt_size);
1076 zstream.avail_out = pkt_size - zstream.total_out;
1077 zstream.next_out = pkt_data + zstream.total_out;
1078 result = inflate(&zstream, Z_NO_FLUSH);
1079 } while (result==Z_OK && pkt_size<10000000);
1080 pkt_size = zstream.total_out;
1081 inflateEnd(&zstream);
1082 if (result != Z_STREAM_END)
1088 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1089 bz_stream bzstream = {0};
1090 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1092 bzstream.next_in = data;
1093 bzstream.avail_in = isize;
1096 pkt_data = av_realloc(pkt_data, pkt_size);
1097 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1098 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1099 result = BZ2_bzDecompress(&bzstream);
1100 } while (result==BZ_OK && pkt_size<10000000);
1101 pkt_size = bzstream.total_out_lo32;
1102 BZ2_bzDecompressEnd(&bzstream);
1103 if (result != BZ_STREAM_END)
1111 *buf_size = pkt_size;
1119 matroska_add_stream (MatroskaDemuxContext *matroska)
1123 MatroskaTrack *track;
1125 /* start with the master */
1126 if ((res = ebml_read_master(matroska, &id)) < 0)
1129 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1131 /* Allocate a generic track. */
1132 track = av_mallocz(MAX_TRACK_SIZE);
1133 track->time_scale = 1.0;
1134 strcpy(track->language, "eng");
1136 /* try reading the trackentry headers */
1138 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1141 } else if (matroska->level_up > 0) {
1142 matroska->level_up--;
1147 /* track number (unique stream ID) */
1148 case MATROSKA_ID_TRACKNUMBER: {
1150 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1156 /* track UID (unique identifier) */
1157 case MATROSKA_ID_TRACKUID: {
1159 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1165 /* track type (video, audio, combined, subtitle, etc.) */
1166 case MATROSKA_ID_TRACKTYPE: {
1168 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1170 if (track->type && track->type != num) {
1171 av_log(matroska->ctx, AV_LOG_INFO,
1172 "More than one tracktype in an entry - skip\n");
1177 switch (track->type) {
1178 case MATROSKA_TRACK_TYPE_VIDEO:
1179 case MATROSKA_TRACK_TYPE_AUDIO:
1180 case MATROSKA_TRACK_TYPE_SUBTITLE:
1182 case MATROSKA_TRACK_TYPE_COMPLEX:
1183 case MATROSKA_TRACK_TYPE_LOGO:
1184 case MATROSKA_TRACK_TYPE_CONTROL:
1186 av_log(matroska->ctx, AV_LOG_INFO,
1187 "Unknown or unsupported track type 0x%x\n",
1189 track->type = MATROSKA_TRACK_TYPE_NONE;
1195 /* tracktype specific stuff for video */
1196 case MATROSKA_ID_TRACKVIDEO: {
1197 MatroskaVideoTrack *videotrack;
1199 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1200 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1201 av_log(matroska->ctx, AV_LOG_INFO,
1202 "video data in non-video track - ignoring\n");
1203 res = AVERROR_INVALIDDATA;
1205 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1207 videotrack = (MatroskaVideoTrack *)track;
1210 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1213 } else if (matroska->level_up > 0) {
1214 matroska->level_up--;
1219 /* fixme, this should be one-up, but I get it here */
1220 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1222 if ((res = ebml_read_uint (matroska, &id,
1225 track->default_duration = num;
1229 /* video framerate */
1230 case MATROSKA_ID_VIDEOFRAMERATE: {
1232 if ((res = ebml_read_float(matroska, &id,
1235 if (!track->default_duration)
1236 track->default_duration = 1000000000/num;
1240 /* width of the size to display the video at */
1241 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1243 if ((res = ebml_read_uint(matroska, &id,
1246 videotrack->display_width = num;
1250 /* height of the size to display the video at */
1251 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1253 if ((res = ebml_read_uint(matroska, &id,
1256 videotrack->display_height = num;
1260 /* width of the video in the file */
1261 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1263 if ((res = ebml_read_uint(matroska, &id,
1266 videotrack->pixel_width = num;
1270 /* height of the video in the file */
1271 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1273 if ((res = ebml_read_uint(matroska, &id,
1276 videotrack->pixel_height = num;
1280 /* whether the video is interlaced */
1281 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1283 if ((res = ebml_read_uint(matroska, &id,
1289 /* colorspace (only matters for raw video)
1291 case MATROSKA_ID_VIDEOCOLORSPACE: {
1293 if ((res = ebml_read_uint(matroska, &id,
1296 videotrack->fourcc = num;
1301 av_log(matroska->ctx, AV_LOG_INFO,
1302 "Unknown video track header entry "
1303 "0x%x - ignoring\n", id);
1306 case MATROSKA_ID_VIDEOSTEREOMODE:
1307 case MATROSKA_ID_VIDEOASPECTRATIO:
1309 res = ebml_read_skip(matroska);
1313 if (matroska->level_up) {
1314 matroska->level_up--;
1321 /* tracktype specific stuff for audio */
1322 case MATROSKA_ID_TRACKAUDIO: {
1323 MatroskaAudioTrack *audiotrack;
1325 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1326 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1327 av_log(matroska->ctx, AV_LOG_INFO,
1328 "audio data in non-audio track - ignoring\n");
1329 res = AVERROR_INVALIDDATA;
1331 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1333 audiotrack = (MatroskaAudioTrack *)track;
1334 audiotrack->channels = 1;
1335 audiotrack->samplerate = 8000;
1338 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1341 } else if (matroska->level_up > 0) {
1342 matroska->level_up--;
1348 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1350 if ((res = ebml_read_float(matroska, &id,
1353 audiotrack->internal_samplerate =
1354 audiotrack->samplerate = num;
1358 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1360 if ((res = ebml_read_float(matroska, &id,
1363 audiotrack->samplerate = num;
1368 case MATROSKA_ID_AUDIOBITDEPTH: {
1370 if ((res = ebml_read_uint(matroska, &id,
1373 audiotrack->bitdepth = num;
1378 case MATROSKA_ID_AUDIOCHANNELS: {
1380 if ((res = ebml_read_uint(matroska, &id,
1383 audiotrack->channels = num;
1388 av_log(matroska->ctx, AV_LOG_INFO,
1389 "Unknown audio track header entry "
1390 "0x%x - ignoring\n", id);
1394 res = ebml_read_skip(matroska);
1398 if (matroska->level_up) {
1399 matroska->level_up--;
1406 /* codec identifier */
1407 case MATROSKA_ID_CODECID: {
1409 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1411 track->codec_id = text;
1415 /* codec private data */
1416 case MATROSKA_ID_CODECPRIVATE: {
1419 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1421 track->codec_priv = data;
1422 track->codec_priv_size = size;
1426 /* name of this track */
1427 case MATROSKA_ID_TRACKNAME: {
1429 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1435 /* language (matters for audio/subtitles, mostly) */
1436 case MATROSKA_ID_TRACKLANGUAGE: {
1438 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1440 if ((end = strchr(text, '-')))
1442 if (strlen(text) == 3)
1443 strcpy(track->language, text);
1448 /* whether this is actually used */
1449 case MATROSKA_ID_TRACKFLAGENABLED: {
1451 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1456 /* whether it's the default for this track type */
1457 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1459 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1461 track->flag_default = num;
1465 /* lacing (like MPEG, where blocks don't end/start on frame
1467 case MATROSKA_ID_TRACKFLAGLACING: {
1469 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1474 /* default length (in time) of one data block in this track */
1475 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1477 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1479 track->default_duration = num;
1483 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1484 if ((res = ebml_read_master(matroska, &id)) < 0)
1488 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1491 } else if (matroska->level_up > 0) {
1492 matroska->level_up--;
1497 case MATROSKA_ID_TRACKCONTENTENCODING: {
1498 int encoding_scope = 1;
1499 if ((res = ebml_read_master(matroska, &id)) < 0)
1503 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1506 } else if (matroska->level_up > 0) {
1507 matroska->level_up--;
1512 case MATROSKA_ID_ENCODINGSCOPE: {
1514 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1516 encoding_scope = num;
1520 case MATROSKA_ID_ENCODINGTYPE: {
1522 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1525 av_log(matroska->ctx, AV_LOG_ERROR,
1526 "Unsupported encoding type");
1530 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1531 if ((res = ebml_read_master(matroska, &id)) < 0)
1535 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1538 } else if (matroska->level_up > 0) {
1539 matroska->level_up--;
1544 case MATROSKA_ID_ENCODINGCOMPALGO: {
1546 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1548 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1550 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1553 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1555 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1556 av_log(matroska->ctx, AV_LOG_ERROR,
1557 "Unsupported compression algo\n");
1558 track->encoding_algo = num;
1562 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1565 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1567 track->encoding_settings = data;
1568 track->encoding_settings_len = size;
1573 av_log(matroska->ctx, AV_LOG_INFO,
1574 "Unknown compression header entry "
1575 "0x%x - ignoring\n", id);
1579 res = ebml_read_skip(matroska);
1583 if (matroska->level_up) {
1584 matroska->level_up--;
1592 av_log(matroska->ctx, AV_LOG_INFO,
1593 "Unknown content encoding header entry "
1594 "0x%x - ignoring\n", id);
1598 res = ebml_read_skip(matroska);
1602 if (matroska->level_up) {
1603 matroska->level_up--;
1608 track->encoding_scope = encoding_scope;
1613 av_log(matroska->ctx, AV_LOG_INFO,
1614 "Unknown content encodings header entry "
1615 "0x%x - ignoring\n", id);
1619 res = ebml_read_skip(matroska);
1623 if (matroska->level_up) {
1624 matroska->level_up--;
1631 case MATROSKA_ID_TRACKTIMECODESCALE: {
1633 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1635 track->time_scale = num;
1640 av_log(matroska->ctx, AV_LOG_INFO,
1641 "Unknown track header entry 0x%x - ignoring\n", id);
1645 /* we ignore these because they're nothing useful. */
1646 case MATROSKA_ID_TRACKFLAGFORCED:
1647 case MATROSKA_ID_CODECNAME:
1648 case MATROSKA_ID_CODECDECODEALL:
1649 case MATROSKA_ID_CODECINFOURL:
1650 case MATROSKA_ID_CODECDOWNLOADURL:
1651 case MATROSKA_ID_TRACKMINCACHE:
1652 case MATROSKA_ID_TRACKMAXCACHE:
1653 res = ebml_read_skip(matroska);
1657 if (matroska->level_up) {
1658 matroska->level_up--;
1663 if (track->codec_priv_size && track->encoding_scope & 2) {
1664 uint8_t *orig_priv = track->codec_priv;
1665 int offset = matroska_decode_buffer(&track->codec_priv,
1666 &track->codec_priv_size, track);
1668 track->codec_priv = av_malloc(track->codec_priv_size + offset);
1669 memcpy(track->codec_priv, track->encoding_settings, offset);
1670 memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1671 track->codec_priv_size += offset;
1673 } else if (!offset) {
1676 av_log(matroska->ctx, AV_LOG_ERROR,
1677 "Failed to decode codec private data\n");
1680 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1681 matroska->tracks[matroska->num_tracks++] = track;
1689 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1694 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1697 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1700 } else if (matroska->level_up) {
1701 matroska->level_up--;
1706 /* one track within the "all-tracks" header */
1707 case MATROSKA_ID_TRACKENTRY:
1708 res = matroska_add_stream(matroska);
1712 av_log(matroska->ctx, AV_LOG_INFO,
1713 "Unknown entry 0x%x in track header\n", id);
1717 res = ebml_read_skip(matroska);
1721 if (matroska->level_up) {
1722 matroska->level_up--;
1731 matroska_parse_index (MatroskaDemuxContext *matroska)
1735 MatroskaDemuxIndex idx;
1737 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1740 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1743 } else if (matroska->level_up) {
1744 matroska->level_up--;
1749 /* one single index entry ('point') */
1750 case MATROSKA_ID_POINTENTRY:
1751 if ((res = ebml_read_master(matroska, &id)) < 0)
1754 /* in the end, we hope to fill one entry with a
1755 * timestamp, a file position and a tracknum */
1756 idx.pos = (uint64_t) -1;
1757 idx.time = (uint64_t) -1;
1758 idx.track = (uint16_t) -1;
1761 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1764 } else if (matroska->level_up) {
1765 matroska->level_up--;
1770 /* one single index entry ('point') */
1771 case MATROSKA_ID_CUETIME: {
1773 if ((res = ebml_read_uint(matroska, &id,
1776 idx.time = time * matroska->time_scale;
1780 /* position in the file + track to which it
1782 case MATROSKA_ID_CUETRACKPOSITION:
1783 if ((res = ebml_read_master(matroska, &id)) < 0)
1787 if (!(id = ebml_peek_id (matroska,
1788 &matroska->level_up))) {
1791 } else if (matroska->level_up) {
1792 matroska->level_up--;
1798 case MATROSKA_ID_CUETRACK: {
1800 if ((res = ebml_read_uint(matroska,
1807 /* position in file */
1808 case MATROSKA_ID_CUECLUSTERPOSITION: {
1810 if ((res = ebml_read_uint(matroska,
1813 idx.pos = num+matroska->segment_start;
1818 av_log(matroska->ctx, AV_LOG_INFO,
1819 "Unknown entry 0x%x in "
1820 "CuesTrackPositions\n", id);
1824 res = ebml_read_skip(matroska);
1828 if (matroska->level_up) {
1829 matroska->level_up--;
1837 av_log(matroska->ctx, AV_LOG_INFO,
1838 "Unknown entry 0x%x in cuespoint "
1843 res = ebml_read_skip(matroska);
1847 if (matroska->level_up) {
1848 matroska->level_up--;
1853 /* so let's see if we got what we wanted */
1854 if (idx.pos != (uint64_t) -1 &&
1855 idx.time != (uint64_t) -1 &&
1856 idx.track != (uint16_t) -1) {
1857 if (matroska->num_indexes % 32 == 0) {
1858 /* re-allocate bigger index */
1860 av_realloc(matroska->index,
1861 (matroska->num_indexes + 32) *
1862 sizeof(MatroskaDemuxIndex));
1864 matroska->index[matroska->num_indexes] = idx;
1865 matroska->num_indexes++;
1870 av_log(matroska->ctx, AV_LOG_INFO,
1871 "Unknown entry 0x%x in cues header\n", id);
1875 res = ebml_read_skip(matroska);
1879 if (matroska->level_up) {
1880 matroska->level_up--;
1889 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1895 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1898 } else if (matroska->level_up) {
1899 matroska->level_up--;
1904 /* Hm, this is unsupported... */
1906 av_log(matroska->ctx, AV_LOG_INFO,
1907 "Unknown entry 0x%x in metadata header\n", id);
1911 res = ebml_read_skip(matroska);
1915 if (matroska->level_up) {
1916 matroska->level_up--;
1925 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1930 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1933 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1936 } else if (matroska->level_up) {
1937 matroska->level_up--;
1942 case MATROSKA_ID_SEEKENTRY: {
1943 uint32_t seek_id = 0, peek_id_cache = 0;
1944 uint64_t seek_pos = (uint64_t) -1, t;
1945 int dummy_level = 0;
1947 if ((res = ebml_read_master(matroska, &id)) < 0)
1951 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1954 } else if (matroska->level_up) {
1955 matroska->level_up--;
1960 case MATROSKA_ID_SEEKID:
1961 res = ebml_read_uint(matroska, &id, &t);
1965 case MATROSKA_ID_SEEKPOSITION:
1966 res = ebml_read_uint(matroska, &id, &seek_pos);
1970 av_log(matroska->ctx, AV_LOG_INFO,
1971 "Unknown seekhead ID 0x%x\n", id);
1975 res = ebml_read_skip(matroska);
1979 if (matroska->level_up) {
1980 matroska->level_up--;
1985 if (!seek_id || seek_pos == (uint64_t) -1) {
1986 av_log(matroska->ctx, AV_LOG_INFO,
1987 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1993 case MATROSKA_ID_CUES:
1994 case MATROSKA_ID_TAGS: {
1995 uint32_t level_up = matroska->level_up;
1996 offset_t before_pos;
1998 MatroskaLevel level;
2000 /* remember the peeked ID and the current position */
2001 peek_id_cache = matroska->peek_id;
2002 before_pos = url_ftell(matroska->ctx->pb);
2005 if ((res = ebml_read_seek(matroska, seek_pos +
2006 matroska->segment_start)) < 0)
2009 /* we don't want to lose our seekhead level, so we add
2010 * a dummy. This is a crude hack. */
2011 if (matroska->num_levels == EBML_MAX_DEPTH) {
2012 av_log(matroska->ctx, AV_LOG_INFO,
2013 "Max EBML element depth (%d) reached, "
2014 "cannot parse further.\n", EBML_MAX_DEPTH);
2015 return AVERROR_UNKNOWN;
2019 level.length = (uint64_t)-1;
2020 matroska->levels[matroska->num_levels] = level;
2021 matroska->num_levels++;
2025 if (!(id = ebml_peek_id (matroska,
2026 &matroska->level_up)))
2028 if (id != seek_id) {
2029 av_log(matroska->ctx, AV_LOG_INFO,
2030 "We looked for ID=0x%x but got "
2031 "ID=0x%x (pos=%"PRIu64")",
2032 seek_id, id, seek_pos +
2033 matroska->segment_start);
2037 /* read master + parse */
2038 if ((res = ebml_read_master(matroska, &id)) < 0)
2041 case MATROSKA_ID_CUES:
2042 if (!(res = matroska_parse_index(matroska)) ||
2043 url_feof(matroska->ctx->pb)) {
2044 matroska->index_parsed = 1;
2048 case MATROSKA_ID_TAGS:
2049 if (!(res = matroska_parse_metadata(matroska)) ||
2050 url_feof(matroska->ctx->pb)) {
2051 matroska->metadata_parsed = 1;
2058 /* remove dummy level */
2060 while (matroska->num_levels) {
2061 matroska->num_levels--;
2063 matroska->levels[matroska->num_levels].length;
2064 if (length == (uint64_t)-1)
2069 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2071 matroska->peek_id = peek_id_cache;
2072 matroska->level_up = level_up;
2077 av_log(matroska->ctx, AV_LOG_INFO,
2078 "Ignoring seekhead entry for ID=0x%x\n",
2087 av_log(matroska->ctx, AV_LOG_INFO,
2088 "Unknown seekhead ID 0x%x\n", id);
2092 res = ebml_read_skip(matroska);
2096 if (matroska->level_up) {
2097 matroska->level_up--;
2106 matroska_parse_attachments(AVFormatContext *s)
2108 MatroskaDemuxContext *matroska = s->priv_data;
2112 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2115 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2118 } else if (matroska->level_up) {
2119 matroska->level_up--;
2124 case MATROSKA_ID_ATTACHEDFILE: {
2127 uint8_t* data = NULL;
2128 int i, data_size = 0;
2131 if ((res = ebml_read_master(matroska, &id)) < 0)
2135 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2138 } else if (matroska->level_up) {
2139 matroska->level_up--;
2144 case MATROSKA_ID_FILENAME:
2145 res = ebml_read_utf8 (matroska, &id, &name);
2148 case MATROSKA_ID_FILEMIMETYPE:
2149 res = ebml_read_ascii (matroska, &id, &mime);
2152 case MATROSKA_ID_FILEDATA:
2153 res = ebml_read_binary(matroska, &id, &data, &data_size);
2157 av_log(matroska->ctx, AV_LOG_INFO,
2158 "Unknown attachedfile ID 0x%x\n", id);
2159 case MATROSKA_ID_FILEUID:
2161 res = ebml_read_skip(matroska);
2165 if (matroska->level_up) {
2166 matroska->level_up--;
2171 if (!(name && mime && data && data_size > 0)) {
2172 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2176 st = av_new_stream(s, matroska->num_streams++);
2178 return AVERROR(ENOMEM);
2179 st->filename = av_strdup(name);
2180 st->codec->codec_id = CODEC_ID_NONE;
2181 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2182 st->codec->extradata = av_malloc(data_size);
2183 if(st->codec->extradata == NULL)
2184 return AVERROR(ENOMEM);
2185 st->codec->extradata_size = data_size;
2186 memcpy(st->codec->extradata, data, data_size);
2188 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2189 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2190 strlen(ff_mkv_mime_tags[i].str))) {
2191 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2196 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2201 av_log(matroska->ctx, AV_LOG_INFO,
2202 "Unknown attachments ID 0x%x\n", id);
2206 res = ebml_read_skip(matroska);
2210 if (matroska->level_up) {
2211 matroska->level_up--;
2220 matroska_parse_chapters(AVFormatContext *s)
2222 MatroskaDemuxContext *matroska = s->priv_data;
2226 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2229 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2232 } else if (matroska->level_up) {
2233 matroska->level_up--;
2238 case MATROSKA_ID_EDITIONENTRY: {
2239 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2242 /* if there is more than one chapter edition
2243 we take only the first one */
2245 ebml_read_skip(matroska);
2249 if ((res = ebml_read_master(matroska, &id)) < 0)
2253 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2256 } else if (matroska->level_up) {
2257 matroska->level_up--;
2262 case MATROSKA_ID_CHAPTERATOM:
2263 if ((res = ebml_read_master(matroska, &id)) < 0)
2267 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2270 } else if (matroska->level_up) {
2271 matroska->level_up--;
2276 case MATROSKA_ID_CHAPTERTIMEEND:
2277 res = ebml_read_uint(matroska, &id, &end);
2280 case MATROSKA_ID_CHAPTERTIMESTART:
2281 res = ebml_read_uint(matroska, &id, &start);
2284 case MATROSKA_ID_CHAPTERDISPLAY:
2285 if ((res = ebml_read_master(matroska, &id)) < 0)
2289 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2292 } else if (matroska->level_up) {
2293 matroska->level_up--;
2298 case MATROSKA_ID_CHAPSTRING:
2299 res = ebml_read_utf8(matroska, &id, &title);
2303 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2305 res = ebml_read_skip(matroska);
2309 if (matroska->level_up) {
2310 matroska->level_up--;
2316 case MATROSKA_ID_CHAPTERUID:
2317 res = ebml_read_uint(matroska, &id, &uid);
2320 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2321 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2323 res = ebml_read_skip(matroska);
2327 if (matroska->level_up) {
2328 matroska->level_up--;
2333 if (start != AV_NOPTS_VALUE && uid != -1) {
2334 if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2335 res= AVERROR(ENOMEM);
2341 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2342 case MATROSKA_ID_EDITIONUID:
2343 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2344 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2346 res = ebml_read_skip(matroska);
2351 if (matroska->level_up) {
2352 matroska->level_up--;
2360 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2362 res = ebml_read_skip(matroska);
2366 if (matroska->level_up) {
2367 matroska->level_up--;
2376 matroska_aac_profile (char *codec_id)
2378 static const char *aac_profiles[] = {
2383 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2384 if (strstr(codec_id, aac_profiles[profile]))
2390 matroska_aac_sri (int samplerate)
2394 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2395 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2401 matroska_read_header (AVFormatContext *s,
2402 AVFormatParameters *ap)
2404 MatroskaDemuxContext *matroska = s->priv_data;
2405 int last_level, res = 0;
2411 /* First read the EBML header. */
2412 if (ebml_parse(matroska, ebml_syntax, &ebml, 0, 1)
2413 || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
2414 || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
2415 || ebml.doctype_version > 2) {
2416 av_log(matroska->ctx, AV_LOG_ERROR,
2417 "EBML header using unsupported features\n"
2418 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
2419 ebml.version, ebml.doctype, ebml.doctype_version);
2420 return AVERROR_NOFMT;
2422 ebml_free(ebml_syntax, &ebml);
2424 /* The next thing is a segment. */
2426 if (!(id = ebml_peek_id(matroska, &last_level)))
2427 return AVERROR(EIO);
2428 if (id == MATROSKA_ID_SEGMENT)
2432 av_log(matroska->ctx, AV_LOG_INFO,
2433 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2434 MATROSKA_ID_SEGMENT, id);
2435 if ((res = ebml_read_skip(matroska)) < 0)
2439 /* We now have a Matroska segment.
2440 * Seeks are from the beginning of the segment,
2441 * after the segment ID/length. */
2442 if ((res = ebml_read_master(matroska, &id)) < 0)
2444 matroska->segment_start = url_ftell(s->pb);
2446 matroska->time_scale = 1000000;
2447 /* we've found our segment, start reading the different contents in here */
2449 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2452 } else if (matroska->level_up) {
2453 matroska->level_up--;
2459 case MATROSKA_ID_INFO: {
2460 if ((res = ebml_read_master(matroska, &id)) < 0)
2462 res = matroska_parse_info(matroska);
2466 /* track info headers */
2467 case MATROSKA_ID_TRACKS: {
2468 if ((res = ebml_read_master(matroska, &id)) < 0)
2470 res = matroska_parse_tracks(matroska);
2475 case MATROSKA_ID_CUES: {
2476 if (!matroska->index_parsed) {
2477 if ((res = ebml_read_master(matroska, &id)) < 0)
2479 res = matroska_parse_index(matroska);
2481 res = ebml_read_skip(matroska);
2486 case MATROSKA_ID_TAGS: {
2487 if (!matroska->metadata_parsed) {
2488 if ((res = ebml_read_master(matroska, &id)) < 0)
2490 res = matroska_parse_metadata(matroska);
2492 res = ebml_read_skip(matroska);
2496 /* file index (if seekable, seek to Cues/Tags to parse it) */
2497 case MATROSKA_ID_SEEKHEAD: {
2498 if ((res = ebml_read_master(matroska, &id)) < 0)
2500 res = matroska_parse_seekhead(matroska);
2504 case MATROSKA_ID_ATTACHMENTS: {
2505 if ((res = ebml_read_master(matroska, &id)) < 0)
2507 res = matroska_parse_attachments(s);
2511 case MATROSKA_ID_CLUSTER: {
2512 /* Do not read the master - this will be done in the next
2513 * call to matroska_read_packet. */
2518 case MATROSKA_ID_CHAPTERS: {
2519 if ((res = ebml_read_master(matroska, &id)) < 0)
2521 res = matroska_parse_chapters(s);
2526 av_log(matroska->ctx, AV_LOG_INFO,
2527 "Unknown matroska file header ID 0x%x\n", id);
2531 res = ebml_read_skip(matroska);
2535 if (matroska->level_up) {
2536 matroska->level_up--;
2541 /* Have we found a cluster? */
2542 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2544 MatroskaTrack *track;
2547 for (i = 0; i < matroska->num_tracks; i++) {
2548 enum CodecID codec_id = CODEC_ID_NONE;
2549 uint8_t *extradata = NULL;
2550 int extradata_size = 0;
2551 int extradata_offset = 0;
2552 track = matroska->tracks[i];
2554 /* Apply some sanity checks. */
2555 if (track->codec_id == NULL)
2558 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2559 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2560 strlen(ff_mkv_codec_tags[j].str))){
2561 codec_id= ff_mkv_codec_tags[j].id;
2566 st = track->stream = av_new_stream(s, matroska->num_streams++);
2568 return AVERROR(ENOMEM);
2570 /* Set the FourCC from the CodecID. */
2571 /* This is the MS compatibility mode which stores a
2572 * BITMAPINFOHEADER in the CodecPrivate. */
2573 if (!strcmp(track->codec_id,
2574 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2575 (track->codec_priv_size >= 40) &&
2576 (track->codec_priv != NULL)) {
2577 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2579 /* Offset of biCompression. Stored in LE. */
2580 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2581 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2585 /* This is the MS compatibility mode which stores a
2586 * WAVEFORMATEX in the CodecPrivate. */
2587 else if (!strcmp(track->codec_id,
2588 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2589 (track->codec_priv_size >= 18) &&
2590 (track->codec_priv != NULL)) {
2593 /* Offset of wFormatTag. Stored in LE. */
2594 tag = AV_RL16(track->codec_priv);
2595 codec_id = codec_get_id(codec_wav_tags, tag);
2599 if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2600 (track->codec_priv_size >= 86) &&
2601 (track->codec_priv != NULL)) {
2602 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2604 vtrack->fourcc = AV_RL32(track->codec_priv);
2605 codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2608 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2609 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2610 int profile = matroska_aac_profile(track->codec_id);
2611 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2612 extradata = av_malloc(5);
2613 if (extradata == NULL)
2614 return AVERROR(ENOMEM);
2615 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2616 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2617 if (strstr(track->codec_id, "SBR")) {
2618 sri = matroska_aac_sri(audiotrack->samplerate);
2619 extradata[2] = 0x56;
2620 extradata[3] = 0xE5;
2621 extradata[4] = 0x80 | (sri<<3);
2628 else if (codec_id == CODEC_ID_TTA) {
2629 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2631 extradata_size = 30;
2632 extradata = av_mallocz(extradata_size);
2633 if (extradata == NULL)
2634 return AVERROR(ENOMEM);
2635 init_put_byte(&b, extradata, extradata_size, 1,
2636 NULL, NULL, NULL, NULL);
2637 put_buffer(&b, "TTA1", 4);
2639 put_le16(&b, audiotrack->channels);
2640 put_le16(&b, audiotrack->bitdepth);
2641 put_le32(&b, audiotrack->samplerate);
2642 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2645 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2646 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2647 extradata_offset = 26;
2648 track->codec_priv_size -= extradata_offset;
2651 else if (codec_id == CODEC_ID_RA_144) {
2652 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2653 audiotrack->samplerate = 8000;
2654 audiotrack->channels = 1;
2657 else if (codec_id == CODEC_ID_RA_288 ||
2658 codec_id == CODEC_ID_COOK ||
2659 codec_id == CODEC_ID_ATRAC3) {
2660 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2663 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2664 NULL, NULL, NULL, NULL);
2666 audiotrack->coded_framesize = get_be32(&b);
2668 audiotrack->sub_packet_h = get_be16(&b);
2669 audiotrack->frame_size = get_be16(&b);
2670 audiotrack->sub_packet_size = get_be16(&b);
2671 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2672 if (codec_id == CODEC_ID_RA_288) {
2673 audiotrack->block_align = audiotrack->coded_framesize;
2674 track->codec_priv_size = 0;
2676 audiotrack->block_align = audiotrack->sub_packet_size;
2677 extradata_offset = 78;
2678 track->codec_priv_size -= extradata_offset;
2682 if (codec_id == CODEC_ID_NONE) {
2683 av_log(matroska->ctx, AV_LOG_INFO,
2684 "Unknown/unsupported CodecID %s.\n",
2688 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2690 st->codec->codec_id = codec_id;
2692 if (strcmp(track->language, "und"))
2693 av_strlcpy(st->language, track->language, 4);
2695 if (track->flag_default)
2696 st->disposition |= AV_DISPOSITION_DEFAULT;
2698 if (track->default_duration)
2699 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2700 track->default_duration, 1000000000, 30000);
2703 st->codec->extradata = extradata;
2704 st->codec->extradata_size = extradata_size;
2705 } else if(track->codec_priv && track->codec_priv_size > 0){
2706 st->codec->extradata = av_malloc(track->codec_priv_size);
2707 if(st->codec->extradata == NULL)
2708 return AVERROR(ENOMEM);
2709 st->codec->extradata_size = track->codec_priv_size;
2710 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2711 track->codec_priv_size);
2714 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2715 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2717 st->codec->codec_type = CODEC_TYPE_VIDEO;
2718 st->codec->codec_tag = videotrack->fourcc;
2719 st->codec->width = videotrack->pixel_width;
2720 st->codec->height = videotrack->pixel_height;
2721 if (videotrack->display_width == 0)
2722 videotrack->display_width= videotrack->pixel_width;
2723 if (videotrack->display_height == 0)
2724 videotrack->display_height= videotrack->pixel_height;
2725 av_reduce(&st->codec->sample_aspect_ratio.num,
2726 &st->codec->sample_aspect_ratio.den,
2727 st->codec->height * videotrack->display_width,
2728 st->codec-> width * videotrack->display_height,
2730 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2731 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2732 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2734 st->codec->codec_type = CODEC_TYPE_AUDIO;
2735 st->codec->sample_rate = audiotrack->samplerate;
2736 st->codec->channels = audiotrack->channels;
2737 st->codec->block_align = audiotrack->block_align;
2738 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2739 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2742 /* What do we do with private data? E.g. for Vorbis. */
2747 if (matroska->index_parsed) {
2748 int i, track, stream;
2749 for (i=0; i<matroska->num_indexes; i++) {
2750 MatroskaDemuxIndex *idx = &matroska->index[i];
2751 track = matroska_find_track_by_num(matroska, idx->track);
2752 if (track < 0) continue;
2753 stream = matroska->tracks[track]->stream->index;
2754 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2755 av_add_index_entry(matroska->ctx->streams[stream],
2756 idx->pos, idx->time/AV_TIME_BASE,
2757 0, 0, AVINDEX_KEYFRAME);
2765 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2766 int64_t pos, uint64_t cluster_time, uint64_t duration,
2773 uint8_t *origdata = data;
2775 uint32_t *lace_size = NULL;
2776 int n, flags, laces = 0;
2779 /* first byte(s): tracknum */
2780 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2781 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2788 /* fetch track from num */
2789 track = matroska_find_track_by_num(matroska, num);
2790 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2791 av_log(matroska->ctx, AV_LOG_INFO,
2792 "Invalid stream %d or size %u\n", track, size);
2796 st = matroska->tracks[track]->stream;
2797 if (st->discard >= AVDISCARD_ALL) {
2801 if (duration == AV_NOPTS_VALUE)
2802 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2804 /* block_time (relative to cluster time) */
2805 block_time = AV_RB16(data);
2809 if (is_keyframe == -1)
2810 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2812 if (matroska->skip_to_keyframe) {
2813 if (!is_keyframe || st != matroska->skip_to_stream) {
2817 matroska->skip_to_keyframe = 0;
2820 switch ((flags & 0x06) >> 1) {
2821 case 0x0: /* no lacing */
2823 lace_size = av_mallocz(sizeof(int));
2824 lace_size[0] = size;
2827 case 0x1: /* xiph lacing */
2828 case 0x2: /* fixed-size lacing */
2829 case 0x3: /* EBML lacing */
2830 assert(size>0); // size <=3 is checked before size-=3 above
2831 laces = (*data) + 1;
2834 lace_size = av_mallocz(laces * sizeof(int));
2836 switch ((flags & 0x06) >> 1) {
2837 case 0x1: /* xiph lacing */ {
2840 for (n = 0; res == 0 && n < laces - 1; n++) {
2847 lace_size[n] += temp;
2853 total += lace_size[n];
2855 lace_size[n] = size - total;
2859 case 0x2: /* fixed-size lacing */
2860 for (n = 0; n < laces; n++)
2861 lace_size[n] = size / laces;
2864 case 0x3: /* EBML lacing */ {
2866 n = matroska_ebmlnum_uint(data, size, &num);
2868 av_log(matroska->ctx, AV_LOG_INFO,
2869 "EBML block data error\n");
2874 total = lace_size[0] = num;
2875 for (n = 1; res == 0 && n < laces - 1; n++) {
2878 r = matroska_ebmlnum_sint (data, size, &snum);
2880 av_log(matroska->ctx, AV_LOG_INFO,
2881 "EBML block data error\n");
2886 lace_size[n] = lace_size[n - 1] + snum;
2887 total += lace_size[n];
2889 lace_size[n] = size - total;
2897 uint64_t timecode = AV_NOPTS_VALUE;
2899 if (cluster_time != (uint64_t)-1
2900 && (block_time >= 0 || cluster_time >= -block_time))
2901 timecode = cluster_time + block_time;
2903 for (n = 0; n < laces; n++) {
2904 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2905 st->codec->codec_id == CODEC_ID_COOK ||
2906 st->codec->codec_id == CODEC_ID_ATRAC3) {
2907 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2908 int a = st->codec->block_align;
2909 int sps = audiotrack->sub_packet_size;
2910 int cfs = audiotrack->coded_framesize;
2911 int h = audiotrack->sub_packet_h;
2912 int y = audiotrack->sub_packet_cnt;
2913 int w = audiotrack->frame_size;
2916 if (!audiotrack->pkt_cnt) {
2917 if (st->codec->codec_id == CODEC_ID_RA_288)
2918 for (x=0; x<h/2; x++)
2919 memcpy(audiotrack->buf+x*2*w+y*cfs,
2922 for (x=0; x<w/sps; x++)
2923 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2925 if (++audiotrack->sub_packet_cnt >= h) {
2926 audiotrack->sub_packet_cnt = 0;
2927 audiotrack->pkt_cnt = h*w / a;
2930 while (audiotrack->pkt_cnt) {
2931 pkt = av_mallocz(sizeof(AVPacket));
2932 av_new_packet(pkt, a);
2933 memcpy(pkt->data, audiotrack->buf
2934 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2936 pkt->stream_index = st->index;
2937 matroska_queue_packet(matroska, pkt);
2940 int offset = 0, pkt_size = lace_size[n];
2941 uint8_t *pkt_data = data;
2943 if (matroska->tracks[track]->encoding_scope & 1) {
2944 offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2945 matroska->tracks[track]);
2950 pkt = av_mallocz(sizeof(AVPacket));
2951 /* XXX: prevent data copy... */
2952 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2954 res = AVERROR(ENOMEM);
2959 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2960 memcpy (pkt->data+offset, pkt_data, pkt_size);
2962 if (pkt_data != data)
2966 pkt->flags = is_keyframe;
2967 pkt->stream_index = st->index;
2969 pkt->pts = timecode;
2971 pkt->duration = duration;
2973 matroska_queue_packet(matroska, pkt);
2976 if (timecode != AV_NOPTS_VALUE)
2977 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2978 data += lace_size[n];
2988 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2989 uint64_t cluster_time)
2993 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2994 uint64_t duration = AV_NOPTS_VALUE;
2999 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
3002 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3005 } else if (matroska->level_up) {
3006 matroska->level_up--;
3011 /* one block inside the group. Note, block parsing is one
3012 * of the harder things, so this code is a bit complicated.
3013 * See http://www.matroska.org/ for documentation. */
3014 case MATROSKA_ID_BLOCK: {
3015 pos = url_ftell(matroska->ctx->pb);
3016 res = ebml_read_binary(matroska, &id, &data, &size);
3020 case MATROSKA_ID_BLOCKDURATION: {
3021 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
3026 case MATROSKA_ID_BLOCKREFERENCE: {
3028 /* We've found a reference, so not even the first frame in
3029 * the lace is a key frame. */
3031 if (last_num_packets != matroska->num_packets)
3032 matroska->packets[last_num_packets]->flags = 0;
3033 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3039 av_log(matroska->ctx, AV_LOG_INFO,
3040 "Unknown entry 0x%x in blockgroup data\n", id);
3044 res = ebml_read_skip(matroska);
3048 if (matroska->level_up) {
3049 matroska->level_up--;
3058 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3059 duration, is_keyframe);
3065 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3069 uint64_t cluster_time = 0;
3074 av_log(matroska->ctx, AV_LOG_DEBUG,
3075 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3078 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3081 } else if (matroska->level_up) {
3082 matroska->level_up--;
3087 /* cluster timecode */
3088 case MATROSKA_ID_CLUSTERTIMECODE: {
3090 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3096 /* a group of blocks inside a cluster */
3097 case MATROSKA_ID_BLOCKGROUP:
3098 if ((res = ebml_read_master(matroska, &id)) < 0)
3100 res = matroska_parse_blockgroup(matroska, cluster_time);
3103 case MATROSKA_ID_SIMPLEBLOCK:
3104 pos = url_ftell(matroska->ctx->pb);
3105 res = ebml_read_binary(matroska, &id, &data, &size);
3107 res = matroska_parse_block(matroska, data, size, pos,
3108 cluster_time, AV_NOPTS_VALUE,
3113 av_log(matroska->ctx, AV_LOG_INFO,
3114 "Unknown entry 0x%x in cluster data\n", id);
3118 res = ebml_read_skip(matroska);
3122 if (matroska->level_up) {
3123 matroska->level_up--;
3132 matroska_read_packet (AVFormatContext *s,
3135 MatroskaDemuxContext *matroska = s->priv_data;
3139 /* Read stream until we have a packet queued. */
3140 while (matroska_deliver_packet(matroska, pkt)) {
3142 /* Have we already reached the end? */
3144 return AVERROR(EIO);
3148 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3149 return AVERROR(EIO);
3150 } else if (matroska->level_up) {
3151 matroska->level_up--;
3156 case MATROSKA_ID_CLUSTER:
3157 if ((res = ebml_read_master(matroska, &id)) < 0)
3159 if ((res = matroska_parse_cluster(matroska)) == 0)
3160 res = 1; /* Parsed one cluster, let's get out. */
3165 res = ebml_read_skip(matroska);
3169 if (matroska->level_up) {
3170 matroska->level_up--;
3183 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3186 MatroskaDemuxContext *matroska = s->priv_data;
3187 AVStream *st = s->streams[stream_index];
3190 /* find index entry */
3191 index = av_index_search_timestamp(st, timestamp, flags);
3195 matroska_clear_queue(matroska);
3198 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3199 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3200 matroska->skip_to_stream = st;
3201 matroska->peek_id = 0;
3202 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3207 matroska_read_close (AVFormatContext *s)
3209 MatroskaDemuxContext *matroska = s->priv_data;
3212 av_free(matroska->index);
3214 matroska_clear_queue(matroska);
3216 for (n = 0; n < matroska->num_tracks; n++) {
3217 MatroskaTrack *track = matroska->tracks[n];
3218 av_free(track->codec_id);
3219 av_free(track->codec_priv);
3220 av_free(track->name);
3222 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3223 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3224 av_free(audiotrack->buf);
3233 AVInputFormat matroska_demuxer = {
3235 NULL_IF_CONFIG_SMALL("Matroska file format"),
3236 sizeof(MatroskaDemuxContext),
3238 matroska_read_header,
3239 matroska_read_packet,
3240 matroska_read_close,