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"
47 typedef struct Track {
48 MatroskaTrackType type;
50 /* Unique track number and track ID. stream_index is the index that
51 * the calling app uses for this track. */
61 unsigned char *codec_priv;
65 uint64_t default_duration;
66 MatroskaTrackFlags flags;
69 MatroskaTrackEncodingCompAlgo encoding_algo;
70 uint8_t *encoding_settings;
71 int encoding_settings_len;
74 typedef struct MatroskaVideoTrack {
87 typedef struct MatroskaAudioTrack {
92 int internal_samplerate;
96 /* real audio header */
105 } MatroskaAudioTrack;
107 typedef struct MatroskaSubtitleTrack {
110 } MatroskaSubtitleTrack;
112 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
113 sizeof(MatroskaAudioTrack), \
114 sizeof(MatroskaSubtitleTrack)))
116 typedef struct MatroskaLevel {
121 typedef struct MatroskaDemuxIndex {
122 uint64_t pos; /* of the corresponding *cluster*! */
123 uint16_t track; /* reference to 'num' */
124 uint64_t time; /* in nanoseconds */
125 } MatroskaDemuxIndex;
127 typedef struct MatroskaDemuxContext {
128 AVFormatContext *ctx;
132 MatroskaLevel levels[EBML_MAX_DEPTH];
135 /* timescale in the file */
138 /* num_streams is the number of streams that av_new_stream() was called
139 * for ( = that are available to the calling program). */
142 MatroskaTrack *tracks[MAX_STREAMS];
144 /* cache for ID peeking */
147 /* byte position of the segment inside the stream */
148 offset_t segment_start;
150 /* The packet queue. */
154 /* have we already parse metadata/cues/clusters? */
159 /* The index for seeking. */
161 MatroskaDemuxIndex *index;
163 /* What to skip before effectively reading a packet. */
164 int skip_to_keyframe;
165 AVStream *skip_to_stream;
166 } MatroskaDemuxContext;
168 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
171 * The first few functions handle EBML file parsing. The rest
172 * is the document interpretation. Matroska really just is a
177 * Return: the amount of levels in the hierarchy that the
178 * current element lies higher than the previous one.
179 * The opposite isn't done - that's auto-done using master
184 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
186 ByteIOContext *pb = matroska->ctx->pb;
187 offset_t pos = url_ftell(pb);
190 while (matroska->num_levels > 0) {
191 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
193 if (pos >= level->start + level->length) {
194 matroska->num_levels--;
205 * Read: an "EBML number", which is defined as a variable-length
206 * array of bytes. The first byte indicates the length by giving a
207 * number of 0-bits followed by a one. The position of the first
208 * "one" bit inside the first byte indicates the length of this
210 * Returns: num. of bytes read. < 0 on error.
214 ebml_read_num (MatroskaDemuxContext *matroska,
218 ByteIOContext *pb = matroska->ctx->pb;
219 int len_mask = 0x80, read = 1, n = 1;
222 /* the first byte tells us the length in bytes - get_byte() can normally
223 * return 0, but since that's not a valid first ebmlID byte, we can
224 * use it safely here to catch EOS. */
225 if (!(total = get_byte(pb))) {
226 /* we might encounter EOS here */
228 offset_t pos = url_ftell(pb);
229 av_log(matroska->ctx, AV_LOG_ERROR,
230 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
233 return AVERROR(EIO); /* EOS or actual I/O error */
236 /* get the length of the EBML number */
237 while (read <= max_size && !(total & len_mask)) {
241 if (read > max_size) {
242 offset_t pos = url_ftell(pb) - 1;
243 av_log(matroska->ctx, AV_LOG_ERROR,
244 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
245 (uint8_t) total, pos, pos);
246 return AVERROR_INVALIDDATA;
249 /* read out length */
252 total = (total << 8) | get_byte(pb);
260 * Read: the element content data ID.
261 * Return: the number of bytes read or < 0 on error.
265 ebml_read_element_id (MatroskaDemuxContext *matroska,
272 /* if we re-call this, use our cached ID */
273 if (matroska->peek_id != 0) {
276 *id = matroska->peek_id;
280 /* read out the "EBML number", include tag in ID */
281 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
283 *id = matroska->peek_id = total | (1 << (read * 7));
287 *level_up = ebml_read_element_level_up(matroska);
293 * Read: element content length.
294 * Return: the number of bytes read or < 0 on error.
298 ebml_read_element_length (MatroskaDemuxContext *matroska,
301 /* clear cache since we're now beyond that data point */
302 matroska->peek_id = 0;
304 /* read out the "EBML number", include tag in ID */
305 return ebml_read_num(matroska, 8, length);
309 * Return: the ID of the next element, or 0 on error.
310 * Level_up contains the amount of levels that this
311 * next element lies higher than the previous one.
315 ebml_peek_id (MatroskaDemuxContext *matroska,
320 if (ebml_read_element_id(matroska, &id, level_up) < 0)
327 * Seek to a given offset.
328 * 0 is success, -1 is failure.
332 ebml_read_seek (MatroskaDemuxContext *matroska,
335 ByteIOContext *pb = matroska->ctx->pb;
337 /* clear ID cache, if any */
338 matroska->peek_id = 0;
340 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
344 * Skip the next element.
345 * 0 is success, -1 is failure.
349 ebml_read_skip (MatroskaDemuxContext *matroska)
351 ByteIOContext *pb = matroska->ctx->pb;
356 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
357 (res = ebml_read_element_length(matroska, &length)) < 0)
360 url_fskip(pb, length);
366 * Read the next element as an unsigned int.
367 * 0 is success, < 0 is failure.
371 ebml_read_uint (MatroskaDemuxContext *matroska,
375 ByteIOContext *pb = matroska->ctx->pb;
376 int n = 0, size, res;
379 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
380 (res = ebml_read_element_length(matroska, &rlength)) < 0)
383 if (size < 1 || size > 8) {
384 offset_t pos = url_ftell(pb);
385 av_log(matroska->ctx, AV_LOG_ERROR,
386 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
388 return AVERROR_INVALIDDATA;
391 /* big-endian ordening; build up number */
394 *num = (*num << 8) | get_byte(pb);
400 * Read the next element as a signed int.
401 * 0 is success, < 0 is failure.
405 ebml_read_sint (MatroskaDemuxContext *matroska,
409 ByteIOContext *pb = matroska->ctx->pb;
410 int size, n = 1, negative = 0, res;
413 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
414 (res = ebml_read_element_length(matroska, &rlength)) < 0)
417 if (size < 1 || size > 8) {
418 offset_t pos = url_ftell(pb);
419 av_log(matroska->ctx, AV_LOG_ERROR,
420 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
422 return AVERROR_INVALIDDATA;
424 if ((*num = get_byte(pb)) & 0x80) {
429 *num = (*num << 8) | get_byte(pb);
433 *num = *num - (1LL << ((8 * size) - 1));
439 * Read the next element as a float.
440 * 0 is success, < 0 is failure.
444 ebml_read_float (MatroskaDemuxContext *matroska,
448 ByteIOContext *pb = matroska->ctx->pb;
452 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
453 (res = ebml_read_element_length(matroska, &rlength)) < 0)
458 *num= av_int2flt(get_be32(pb));
460 *num= av_int2dbl(get_be64(pb));
462 offset_t pos = url_ftell(pb);
463 av_log(matroska->ctx, AV_LOG_ERROR,
464 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
466 return AVERROR_INVALIDDATA;
473 * Read the next element as an ASCII string.
474 * 0 is success, < 0 is failure.
478 ebml_read_ascii (MatroskaDemuxContext *matroska,
482 ByteIOContext *pb = matroska->ctx->pb;
486 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
487 (res = ebml_read_element_length(matroska, &rlength)) < 0)
491 /* ebml strings are usually not 0-terminated, so we allocate one
492 * byte more, read the string and NULL-terminate it ourselves. */
493 if (size < 0 || !(*str = av_malloc(size + 1))) {
494 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
495 return AVERROR(ENOMEM);
497 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
498 offset_t pos = url_ftell(pb);
499 av_log(matroska->ctx, AV_LOG_ERROR,
500 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
510 * Read the next element as a UTF-8 string.
511 * 0 is success, < 0 is failure.
515 ebml_read_utf8 (MatroskaDemuxContext *matroska,
519 return ebml_read_ascii(matroska, id, str);
523 * Read the next element, but only the header. The contents
524 * are supposed to be sub-elements which can be read separately.
525 * 0 is success, < 0 is failure.
529 ebml_read_master (MatroskaDemuxContext *matroska,
532 ByteIOContext *pb = matroska->ctx->pb;
534 MatroskaLevel *level;
537 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
538 (res = ebml_read_element_length(matroska, &length)) < 0)
541 /* protect... (Heaven forbids that the '>' is true) */
542 if (matroska->num_levels >= EBML_MAX_DEPTH) {
543 av_log(matroska->ctx, AV_LOG_ERROR,
544 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
545 return AVERROR(ENOSYS);
549 level = &matroska->levels[matroska->num_levels++];
550 level->start = url_ftell(pb);
551 level->length = length;
557 * Read the next element as binary data.
558 * 0 is success, < 0 is failure.
562 ebml_read_binary (MatroskaDemuxContext *matroska,
567 ByteIOContext *pb = matroska->ctx->pb;
571 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
572 (res = ebml_read_element_length(matroska, &rlength)) < 0)
576 if (!(*binary = av_malloc(*size))) {
577 av_log(matroska->ctx, AV_LOG_ERROR,
578 "Memory allocation error\n");
579 return AVERROR(ENOMEM);
582 if (get_buffer(pb, *binary, *size) != *size) {
583 offset_t pos = url_ftell(pb);
584 av_log(matroska->ctx, AV_LOG_ERROR,
585 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
593 * Read signed/unsigned "EBML" numbers.
594 * Return: number of bytes processed, < 0 on error.
595 * XXX: use ebml_read_num().
599 matroska_ebmlnum_uint (uint8_t *data,
603 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
607 return AVERROR_INVALIDDATA;
610 while (read <= 8 && !(total & len_mask)) {
615 return AVERROR_INVALIDDATA;
617 if ((total &= (len_mask - 1)) == len_mask - 1)
620 return AVERROR_INVALIDDATA;
624 total = (total << 8) | data[n];
637 * Same as above, but signed.
641 matroska_ebmlnum_sint (uint8_t *data,
648 /* read as unsigned number first */
649 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
652 /* make signed (weird way) */
653 if (unum == (uint64_t)-1)
656 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
662 * Read an EBML header.
663 * 0 is success, < 0 is failure.
667 ebml_read_header (MatroskaDemuxContext *matroska,
672 int level_up, res = 0;
680 if (!(id = ebml_peek_id(matroska, &level_up)) ||
681 level_up != 0 || id != EBML_ID_HEADER) {
682 av_log(matroska->ctx, AV_LOG_ERROR,
683 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
684 return AVERROR_INVALIDDATA;
686 if ((res = ebml_read_master(matroska, &id)) < 0)
690 if (!(id = ebml_peek_id(matroska, &level_up)))
698 /* is our read version uptodate? */
699 case EBML_ID_EBMLREADVERSION: {
702 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
704 if (num > EBML_VERSION) {
705 av_log(matroska->ctx, AV_LOG_ERROR,
706 "EBML version %"PRIu64" (> %d) is not supported\n",
708 return AVERROR_INVALIDDATA;
713 /* we only handle 8 byte lengths at max */
714 case EBML_ID_EBMLMAXSIZELENGTH: {
717 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
719 if (num > sizeof(uint64_t)) {
720 av_log(matroska->ctx, AV_LOG_ERROR,
721 "Integers of size %"PRIu64" (> %zd) not supported\n",
722 num, sizeof(uint64_t));
723 return AVERROR_INVALIDDATA;
728 /* we handle 4 byte IDs at max */
729 case EBML_ID_EBMLMAXIDLENGTH: {
732 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
734 if (num > sizeof(uint32_t)) {
735 av_log(matroska->ctx, AV_LOG_ERROR,
736 "IDs of size %"PRIu64" (> %zu) not supported\n",
737 num, sizeof(uint32_t));
738 return AVERROR_INVALIDDATA;
743 case EBML_ID_DOCTYPE: {
746 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
757 case EBML_ID_DOCTYPEREADVERSION: {
760 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
768 av_log(matroska->ctx, AV_LOG_INFO,
769 "Unknown data type 0x%x in EBML header", id);
773 /* we ignore these two, as they don't tell us anything we
775 case EBML_ID_EBMLVERSION:
776 case EBML_ID_DOCTYPEVERSION:
777 res = ebml_read_skip (matroska);
787 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
792 for (i = 0; i < matroska->num_tracks; i++)
793 if (matroska->tracks[i]->num == num)
801 * Put one packet in an application-supplied AVPacket struct.
802 * Returns 0 on success or -1 on failure.
806 matroska_deliver_packet (MatroskaDemuxContext *matroska,
809 if (matroska->num_packets > 0) {
810 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
811 av_free(matroska->packets[0]);
812 if (matroska->num_packets > 1) {
813 memmove(&matroska->packets[0], &matroska->packets[1],
814 (matroska->num_packets - 1) * sizeof(AVPacket *));
816 av_realloc(matroska->packets, (matroska->num_packets - 1) *
819 av_freep(&matroska->packets);
821 matroska->num_packets--;
829 * Put a packet into our internal queue. Will be delivered to the
830 * user/application during the next get_packet() call.
834 matroska_queue_packet (MatroskaDemuxContext *matroska,
838 av_realloc(matroska->packets, (matroska->num_packets + 1) *
840 matroska->packets[matroska->num_packets] = pkt;
841 matroska->num_packets++;
845 * Free all packets in our internal queue.
848 matroska_clear_queue (MatroskaDemuxContext *matroska)
850 if (matroska->packets) {
852 for (n = 0; n < matroska->num_packets; n++) {
853 av_free_packet(matroska->packets[n]);
854 av_free(matroska->packets[n]);
856 av_free(matroska->packets);
857 matroska->packets = NULL;
858 matroska->num_packets = 0;
868 matroska_probe (AVProbeData *p)
871 int len_mask = 0x80, size = 1, n = 1;
872 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
875 if (AV_RB32(p->buf) != EBML_ID_HEADER)
878 /* length of header */
880 while (size <= 8 && !(total & len_mask)) {
886 total &= (len_mask - 1);
888 total = (total << 8) | p->buf[4 + n++];
890 /* does the probe data contain the whole header? */
891 if (p->buf_size < 4 + size + total)
894 /* the header must contain the document type 'matroska'. For now,
895 * we don't parse the whole header but simply check for the
896 * availability of that array of characters inside the header.
897 * Not fully fool-proof, but good enough. */
898 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
899 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
900 return AVPROBE_SCORE_MAX;
906 * From here on, it's all XML-style DTD stuff... Needs no comments.
910 matroska_parse_info (MatroskaDemuxContext *matroska)
915 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
918 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
921 } else if (matroska->level_up) {
922 matroska->level_up--;
927 /* cluster timecode */
928 case MATROSKA_ID_TIMECODESCALE: {
930 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
932 matroska->time_scale = num;
936 case MATROSKA_ID_DURATION: {
938 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
940 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
944 case MATROSKA_ID_TITLE: {
946 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
948 strncpy(matroska->ctx->title, text,
949 sizeof(matroska->ctx->title)-1);
955 av_log(matroska->ctx, AV_LOG_INFO,
956 "Unknown entry 0x%x in info header\n", id);
959 case MATROSKA_ID_WRITINGAPP:
960 case MATROSKA_ID_MUXINGAPP:
961 case MATROSKA_ID_DATEUTC:
962 case MATROSKA_ID_SEGMENTUID:
964 res = ebml_read_skip(matroska);
968 if (matroska->level_up) {
969 matroska->level_up--;
978 matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
980 uint8_t* data = *buf;
981 int isize = *buf_size;
982 uint8_t* pkt_data = NULL;
983 int pkt_size = isize;
987 switch (track->encoding_algo) {
988 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
989 return track->encoding_settings_len;
990 case MATROSKA_TRACK_ENCODING_COMP_LZO:
992 olen = pkt_size *= 3;
993 pkt_data = av_realloc(pkt_data,
994 pkt_size+LZO_OUTPUT_PADDING);
995 result = lzo1x_decode(pkt_data, &olen, data, &isize);
996 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
1002 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1003 z_stream zstream = {0};
1004 if (inflateInit(&zstream) != Z_OK)
1006 zstream.next_in = data;
1007 zstream.avail_in = isize;
1010 pkt_data = av_realloc(pkt_data, pkt_size);
1011 zstream.avail_out = pkt_size - zstream.total_out;
1012 zstream.next_out = pkt_data + zstream.total_out;
1013 result = inflate(&zstream, Z_NO_FLUSH);
1014 } while (result==Z_OK && pkt_size<10000000);
1015 pkt_size = zstream.total_out;
1016 inflateEnd(&zstream);
1017 if (result != Z_STREAM_END)
1023 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1024 bz_stream bzstream = {0};
1025 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1027 bzstream.next_in = data;
1028 bzstream.avail_in = isize;
1031 pkt_data = av_realloc(pkt_data, pkt_size);
1032 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1033 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1034 result = BZ2_bzDecompress(&bzstream);
1035 } while (result==BZ_OK && pkt_size<10000000);
1036 pkt_size = bzstream.total_out_lo32;
1037 BZ2_bzDecompressEnd(&bzstream);
1038 if (result != BZ_STREAM_END)
1046 *buf_size = pkt_size;
1054 matroska_add_stream (MatroskaDemuxContext *matroska)
1058 MatroskaTrack *track;
1060 /* start with the master */
1061 if ((res = ebml_read_master(matroska, &id)) < 0)
1064 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1066 /* Allocate a generic track. */
1067 track = av_mallocz(MAX_TRACK_SIZE);
1068 track->time_scale = 1.0;
1069 strcpy(track->language, "eng");
1071 /* try reading the trackentry headers */
1073 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1076 } else if (matroska->level_up > 0) {
1077 matroska->level_up--;
1082 /* track number (unique stream ID) */
1083 case MATROSKA_ID_TRACKNUMBER: {
1085 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1091 /* track UID (unique identifier) */
1092 case MATROSKA_ID_TRACKUID: {
1094 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1100 /* track type (video, audio, combined, subtitle, etc.) */
1101 case MATROSKA_ID_TRACKTYPE: {
1103 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1105 if (track->type && track->type != num) {
1106 av_log(matroska->ctx, AV_LOG_INFO,
1107 "More than one tracktype in an entry - skip\n");
1112 switch (track->type) {
1113 case MATROSKA_TRACK_TYPE_VIDEO:
1114 case MATROSKA_TRACK_TYPE_AUDIO:
1115 case MATROSKA_TRACK_TYPE_SUBTITLE:
1117 case MATROSKA_TRACK_TYPE_COMPLEX:
1118 case MATROSKA_TRACK_TYPE_LOGO:
1119 case MATROSKA_TRACK_TYPE_CONTROL:
1121 av_log(matroska->ctx, AV_LOG_INFO,
1122 "Unknown or unsupported track type 0x%x\n",
1124 track->type = MATROSKA_TRACK_TYPE_NONE;
1130 /* tracktype specific stuff for video */
1131 case MATROSKA_ID_TRACKVIDEO: {
1132 MatroskaVideoTrack *videotrack;
1134 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1135 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1136 av_log(matroska->ctx, AV_LOG_INFO,
1137 "video data in non-video track - ignoring\n");
1138 res = AVERROR_INVALIDDATA;
1140 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1142 videotrack = (MatroskaVideoTrack *)track;
1145 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1148 } else if (matroska->level_up > 0) {
1149 matroska->level_up--;
1154 /* fixme, this should be one-up, but I get it here */
1155 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1157 if ((res = ebml_read_uint (matroska, &id,
1160 track->default_duration = num;
1164 /* video framerate */
1165 case MATROSKA_ID_VIDEOFRAMERATE: {
1167 if ((res = ebml_read_float(matroska, &id,
1170 if (!track->default_duration)
1171 track->default_duration = 1000000000/num;
1175 /* width of the size to display the video at */
1176 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1178 if ((res = ebml_read_uint(matroska, &id,
1181 videotrack->display_width = num;
1185 /* height of the size to display the video at */
1186 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1188 if ((res = ebml_read_uint(matroska, &id,
1191 videotrack->display_height = num;
1195 /* width of the video in the file */
1196 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1198 if ((res = ebml_read_uint(matroska, &id,
1201 videotrack->pixel_width = num;
1205 /* height of the video in the file */
1206 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1208 if ((res = ebml_read_uint(matroska, &id,
1211 videotrack->pixel_height = num;
1215 /* whether the video is interlaced */
1216 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1218 if ((res = ebml_read_uint(matroska, &id,
1223 MATROSKA_VIDEOTRACK_INTERLACED;
1226 ~MATROSKA_VIDEOTRACK_INTERLACED;
1230 /* colorspace (only matters for raw video)
1232 case MATROSKA_ID_VIDEOCOLORSPACE: {
1234 if ((res = ebml_read_uint(matroska, &id,
1237 videotrack->fourcc = num;
1242 av_log(matroska->ctx, AV_LOG_INFO,
1243 "Unknown video track header entry "
1244 "0x%x - ignoring\n", id);
1247 case MATROSKA_ID_VIDEOSTEREOMODE:
1248 case MATROSKA_ID_VIDEOASPECTRATIO:
1250 res = ebml_read_skip(matroska);
1254 if (matroska->level_up) {
1255 matroska->level_up--;
1262 /* tracktype specific stuff for audio */
1263 case MATROSKA_ID_TRACKAUDIO: {
1264 MatroskaAudioTrack *audiotrack;
1266 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1267 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1268 av_log(matroska->ctx, AV_LOG_INFO,
1269 "audio data in non-audio track - ignoring\n");
1270 res = AVERROR_INVALIDDATA;
1272 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1274 audiotrack = (MatroskaAudioTrack *)track;
1275 audiotrack->channels = 1;
1276 audiotrack->samplerate = 8000;
1279 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1282 } else if (matroska->level_up > 0) {
1283 matroska->level_up--;
1289 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1291 if ((res = ebml_read_float(matroska, &id,
1294 audiotrack->internal_samplerate =
1295 audiotrack->samplerate = num;
1299 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1301 if ((res = ebml_read_float(matroska, &id,
1304 audiotrack->samplerate = num;
1309 case MATROSKA_ID_AUDIOBITDEPTH: {
1311 if ((res = ebml_read_uint(matroska, &id,
1314 audiotrack->bitdepth = num;
1319 case MATROSKA_ID_AUDIOCHANNELS: {
1321 if ((res = ebml_read_uint(matroska, &id,
1324 audiotrack->channels = num;
1329 av_log(matroska->ctx, AV_LOG_INFO,
1330 "Unknown audio track header entry "
1331 "0x%x - ignoring\n", id);
1335 res = ebml_read_skip(matroska);
1339 if (matroska->level_up) {
1340 matroska->level_up--;
1347 /* codec identifier */
1348 case MATROSKA_ID_CODECID: {
1350 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1352 track->codec_id = text;
1356 /* codec private data */
1357 case MATROSKA_ID_CODECPRIVATE: {
1360 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1362 track->codec_priv = data;
1363 track->codec_priv_size = size;
1367 /* name of this track */
1368 case MATROSKA_ID_TRACKNAME: {
1370 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1376 /* language (matters for audio/subtitles, mostly) */
1377 case MATROSKA_ID_TRACKLANGUAGE: {
1379 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1381 if ((end = strchr(text, '-')))
1383 if (strlen(text) == 3)
1384 strcpy(track->language, text);
1389 /* whether this is actually used */
1390 case MATROSKA_ID_TRACKFLAGENABLED: {
1392 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1395 track->flags |= MATROSKA_TRACK_ENABLED;
1397 track->flags &= ~MATROSKA_TRACK_ENABLED;
1401 /* whether it's the default for this track type */
1402 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1404 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1407 track->flags |= MATROSKA_TRACK_DEFAULT;
1409 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1413 /* lacing (like MPEG, where blocks don't end/start on frame
1415 case MATROSKA_ID_TRACKFLAGLACING: {
1417 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1420 track->flags |= MATROSKA_TRACK_LACING;
1422 track->flags &= ~MATROSKA_TRACK_LACING;
1426 /* default length (in time) of one data block in this track */
1427 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1429 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1431 track->default_duration = num;
1435 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1436 if ((res = ebml_read_master(matroska, &id)) < 0)
1440 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1443 } else if (matroska->level_up > 0) {
1444 matroska->level_up--;
1449 case MATROSKA_ID_TRACKCONTENTENCODING: {
1450 int encoding_scope = 1;
1451 if ((res = ebml_read_master(matroska, &id)) < 0)
1455 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1458 } else if (matroska->level_up > 0) {
1459 matroska->level_up--;
1464 case MATROSKA_ID_ENCODINGSCOPE: {
1466 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1468 encoding_scope = num;
1472 case MATROSKA_ID_ENCODINGTYPE: {
1474 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1477 av_log(matroska->ctx, AV_LOG_ERROR,
1478 "Unsupported encoding type");
1482 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1483 if ((res = ebml_read_master(matroska, &id)) < 0)
1487 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1490 } else if (matroska->level_up > 0) {
1491 matroska->level_up--;
1496 case MATROSKA_ID_ENCODINGCOMPALGO: {
1498 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1500 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1502 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1505 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1507 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1508 av_log(matroska->ctx, AV_LOG_ERROR,
1509 "Unsupported compression algo\n");
1510 track->encoding_algo = num;
1514 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1517 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1519 track->encoding_settings = data;
1520 track->encoding_settings_len = size;
1525 av_log(matroska->ctx, AV_LOG_INFO,
1526 "Unknown compression header entry "
1527 "0x%x - ignoring\n", id);
1531 res = ebml_read_skip(matroska);
1535 if (matroska->level_up) {
1536 matroska->level_up--;
1544 av_log(matroska->ctx, AV_LOG_INFO,
1545 "Unknown content encoding header entry "
1546 "0x%x - ignoring\n", id);
1550 res = ebml_read_skip(matroska);
1554 if (matroska->level_up) {
1555 matroska->level_up--;
1560 track->encoding_scope = encoding_scope;
1565 av_log(matroska->ctx, AV_LOG_INFO,
1566 "Unknown content encodings header entry "
1567 "0x%x - ignoring\n", id);
1571 res = ebml_read_skip(matroska);
1575 if (matroska->level_up) {
1576 matroska->level_up--;
1583 case MATROSKA_ID_TRACKTIMECODESCALE: {
1585 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1587 track->time_scale = num;
1592 av_log(matroska->ctx, AV_LOG_INFO,
1593 "Unknown track header entry 0x%x - ignoring\n", id);
1597 /* we ignore these because they're nothing useful. */
1598 case MATROSKA_ID_TRACKFLAGFORCED:
1599 case MATROSKA_ID_CODECNAME:
1600 case MATROSKA_ID_CODECDECODEALL:
1601 case MATROSKA_ID_CODECINFOURL:
1602 case MATROSKA_ID_CODECDOWNLOADURL:
1603 case MATROSKA_ID_TRACKMINCACHE:
1604 case MATROSKA_ID_TRACKMAXCACHE:
1605 res = ebml_read_skip(matroska);
1609 if (matroska->level_up) {
1610 matroska->level_up--;
1615 if (track->codec_priv_size && track->encoding_scope & 2) {
1616 uint8_t *orig_priv = track->codec_priv;
1617 int offset = matroska_decode_buffer(&track->codec_priv,
1618 &track->codec_priv_size, track);
1620 track->codec_priv = av_malloc(track->codec_priv_size + offset);
1621 memcpy(track->codec_priv, track->encoding_settings, offset);
1622 memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1623 track->codec_priv_size += offset;
1625 } else if (!offset) {
1628 av_log(matroska->ctx, AV_LOG_ERROR,
1629 "Failed to decode codec private data\n");
1632 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1633 matroska->tracks[matroska->num_tracks++] = track;
1641 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1646 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1649 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1652 } else if (matroska->level_up) {
1653 matroska->level_up--;
1658 /* one track within the "all-tracks" header */
1659 case MATROSKA_ID_TRACKENTRY:
1660 res = matroska_add_stream(matroska);
1664 av_log(matroska->ctx, AV_LOG_INFO,
1665 "Unknown entry 0x%x in track header\n", id);
1669 res = ebml_read_skip(matroska);
1673 if (matroska->level_up) {
1674 matroska->level_up--;
1683 matroska_parse_index (MatroskaDemuxContext *matroska)
1687 MatroskaDemuxIndex idx;
1689 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1692 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1695 } else if (matroska->level_up) {
1696 matroska->level_up--;
1701 /* one single index entry ('point') */
1702 case MATROSKA_ID_POINTENTRY:
1703 if ((res = ebml_read_master(matroska, &id)) < 0)
1706 /* in the end, we hope to fill one entry with a
1707 * timestamp, a file position and a tracknum */
1708 idx.pos = (uint64_t) -1;
1709 idx.time = (uint64_t) -1;
1710 idx.track = (uint16_t) -1;
1713 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1716 } else if (matroska->level_up) {
1717 matroska->level_up--;
1722 /* one single index entry ('point') */
1723 case MATROSKA_ID_CUETIME: {
1725 if ((res = ebml_read_uint(matroska, &id,
1728 idx.time = time * matroska->time_scale;
1732 /* position in the file + track to which it
1734 case MATROSKA_ID_CUETRACKPOSITION:
1735 if ((res = ebml_read_master(matroska, &id)) < 0)
1739 if (!(id = ebml_peek_id (matroska,
1740 &matroska->level_up))) {
1743 } else if (matroska->level_up) {
1744 matroska->level_up--;
1750 case MATROSKA_ID_CUETRACK: {
1752 if ((res = ebml_read_uint(matroska,
1759 /* position in file */
1760 case MATROSKA_ID_CUECLUSTERPOSITION: {
1762 if ((res = ebml_read_uint(matroska,
1765 idx.pos = num+matroska->segment_start;
1770 av_log(matroska->ctx, AV_LOG_INFO,
1771 "Unknown entry 0x%x in "
1772 "CuesTrackPositions\n", id);
1776 res = ebml_read_skip(matroska);
1780 if (matroska->level_up) {
1781 matroska->level_up--;
1789 av_log(matroska->ctx, AV_LOG_INFO,
1790 "Unknown entry 0x%x in cuespoint "
1795 res = ebml_read_skip(matroska);
1799 if (matroska->level_up) {
1800 matroska->level_up--;
1805 /* so let's see if we got what we wanted */
1806 if (idx.pos != (uint64_t) -1 &&
1807 idx.time != (uint64_t) -1 &&
1808 idx.track != (uint16_t) -1) {
1809 if (matroska->num_indexes % 32 == 0) {
1810 /* re-allocate bigger index */
1812 av_realloc(matroska->index,
1813 (matroska->num_indexes + 32) *
1814 sizeof(MatroskaDemuxIndex));
1816 matroska->index[matroska->num_indexes] = idx;
1817 matroska->num_indexes++;
1822 av_log(matroska->ctx, AV_LOG_INFO,
1823 "Unknown entry 0x%x in cues header\n", id);
1827 res = ebml_read_skip(matroska);
1831 if (matroska->level_up) {
1832 matroska->level_up--;
1841 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1847 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1850 } else if (matroska->level_up) {
1851 matroska->level_up--;
1856 /* Hm, this is unsupported... */
1858 av_log(matroska->ctx, AV_LOG_INFO,
1859 "Unknown entry 0x%x in metadata header\n", id);
1863 res = ebml_read_skip(matroska);
1867 if (matroska->level_up) {
1868 matroska->level_up--;
1877 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1882 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1885 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1888 } else if (matroska->level_up) {
1889 matroska->level_up--;
1894 case MATROSKA_ID_SEEKENTRY: {
1895 uint32_t seek_id = 0, peek_id_cache = 0;
1896 uint64_t seek_pos = (uint64_t) -1, t;
1897 int dummy_level = 0;
1899 if ((res = ebml_read_master(matroska, &id)) < 0)
1903 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1906 } else if (matroska->level_up) {
1907 matroska->level_up--;
1912 case MATROSKA_ID_SEEKID:
1913 res = ebml_read_uint(matroska, &id, &t);
1917 case MATROSKA_ID_SEEKPOSITION:
1918 res = ebml_read_uint(matroska, &id, &seek_pos);
1922 av_log(matroska->ctx, AV_LOG_INFO,
1923 "Unknown seekhead ID 0x%x\n", id);
1927 res = ebml_read_skip(matroska);
1931 if (matroska->level_up) {
1932 matroska->level_up--;
1937 if (!seek_id || seek_pos == (uint64_t) -1) {
1938 av_log(matroska->ctx, AV_LOG_INFO,
1939 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1945 case MATROSKA_ID_CUES:
1946 case MATROSKA_ID_TAGS: {
1947 uint32_t level_up = matroska->level_up;
1948 offset_t before_pos;
1950 MatroskaLevel level;
1952 /* remember the peeked ID and the current position */
1953 peek_id_cache = matroska->peek_id;
1954 before_pos = url_ftell(matroska->ctx->pb);
1957 if ((res = ebml_read_seek(matroska, seek_pos +
1958 matroska->segment_start)) < 0)
1961 /* we don't want to lose our seekhead level, so we add
1962 * a dummy. This is a crude hack. */
1963 if (matroska->num_levels == EBML_MAX_DEPTH) {
1964 av_log(matroska->ctx, AV_LOG_INFO,
1965 "Max EBML element depth (%d) reached, "
1966 "cannot parse further.\n", EBML_MAX_DEPTH);
1967 return AVERROR_UNKNOWN;
1971 level.length = (uint64_t)-1;
1972 matroska->levels[matroska->num_levels] = level;
1973 matroska->num_levels++;
1977 if (!(id = ebml_peek_id (matroska,
1978 &matroska->level_up)))
1980 if (id != seek_id) {
1981 av_log(matroska->ctx, AV_LOG_INFO,
1982 "We looked for ID=0x%x but got "
1983 "ID=0x%x (pos=%"PRIu64")",
1984 seek_id, id, seek_pos +
1985 matroska->segment_start);
1989 /* read master + parse */
1990 if ((res = ebml_read_master(matroska, &id)) < 0)
1993 case MATROSKA_ID_CUES:
1994 if (!(res = matroska_parse_index(matroska)) ||
1995 url_feof(matroska->ctx->pb)) {
1996 matroska->index_parsed = 1;
2000 case MATROSKA_ID_TAGS:
2001 if (!(res = matroska_parse_metadata(matroska)) ||
2002 url_feof(matroska->ctx->pb)) {
2003 matroska->metadata_parsed = 1;
2010 /* remove dummy level */
2012 while (matroska->num_levels) {
2013 matroska->num_levels--;
2015 matroska->levels[matroska->num_levels].length;
2016 if (length == (uint64_t)-1)
2021 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2023 matroska->peek_id = peek_id_cache;
2024 matroska->level_up = level_up;
2029 av_log(matroska->ctx, AV_LOG_INFO,
2030 "Ignoring seekhead entry for ID=0x%x\n",
2039 av_log(matroska->ctx, AV_LOG_INFO,
2040 "Unknown seekhead ID 0x%x\n", id);
2044 res = ebml_read_skip(matroska);
2048 if (matroska->level_up) {
2049 matroska->level_up--;
2058 matroska_parse_attachments(AVFormatContext *s)
2060 MatroskaDemuxContext *matroska = s->priv_data;
2064 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2067 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2070 } else if (matroska->level_up) {
2071 matroska->level_up--;
2076 case MATROSKA_ID_ATTACHEDFILE: {
2079 uint8_t* data = NULL;
2080 int i, data_size = 0;
2083 if ((res = ebml_read_master(matroska, &id)) < 0)
2087 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2090 } else if (matroska->level_up) {
2091 matroska->level_up--;
2096 case MATROSKA_ID_FILENAME:
2097 res = ebml_read_utf8 (matroska, &id, &name);
2100 case MATROSKA_ID_FILEMIMETYPE:
2101 res = ebml_read_ascii (matroska, &id, &mime);
2104 case MATROSKA_ID_FILEDATA:
2105 res = ebml_read_binary(matroska, &id, &data, &data_size);
2109 av_log(matroska->ctx, AV_LOG_INFO,
2110 "Unknown attachedfile ID 0x%x\n", id);
2111 case MATROSKA_ID_FILEUID:
2113 res = ebml_read_skip(matroska);
2117 if (matroska->level_up) {
2118 matroska->level_up--;
2123 if (!(name && mime && data && data_size > 0)) {
2124 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2128 st = av_new_stream(s, matroska->num_streams++);
2130 return AVERROR(ENOMEM);
2131 st->filename = av_strdup(name);
2132 st->codec->codec_id = CODEC_ID_NONE;
2133 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2134 st->codec->extradata = av_malloc(data_size);
2135 if(st->codec->extradata == NULL)
2136 return AVERROR(ENOMEM);
2137 st->codec->extradata_size = data_size;
2138 memcpy(st->codec->extradata, data, data_size);
2140 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2141 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2142 strlen(ff_mkv_mime_tags[i].str))) {
2143 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2148 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2153 av_log(matroska->ctx, AV_LOG_INFO,
2154 "Unknown attachments ID 0x%x\n", id);
2158 res = ebml_read_skip(matroska);
2162 if (matroska->level_up) {
2163 matroska->level_up--;
2172 matroska_parse_chapters(AVFormatContext *s)
2174 MatroskaDemuxContext *matroska = s->priv_data;
2178 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2181 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2184 } else if (matroska->level_up) {
2185 matroska->level_up--;
2190 case MATROSKA_ID_EDITIONENTRY: {
2191 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2194 /* if there is more than one chapter edition
2195 we take only the first one */
2197 ebml_read_skip(matroska);
2201 if ((res = ebml_read_master(matroska, &id)) < 0)
2205 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2208 } else if (matroska->level_up) {
2209 matroska->level_up--;
2214 case MATROSKA_ID_CHAPTERATOM:
2215 if ((res = ebml_read_master(matroska, &id)) < 0)
2219 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2222 } else if (matroska->level_up) {
2223 matroska->level_up--;
2228 case MATROSKA_ID_CHAPTERTIMEEND:
2229 res = ebml_read_uint(matroska, &id, &end);
2232 case MATROSKA_ID_CHAPTERTIMESTART:
2233 res = ebml_read_uint(matroska, &id, &start);
2236 case MATROSKA_ID_CHAPTERDISPLAY:
2237 if ((res = ebml_read_master(matroska, &id)) < 0)
2241 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2244 } else if (matroska->level_up) {
2245 matroska->level_up--;
2250 case MATROSKA_ID_CHAPSTRING:
2251 res = ebml_read_utf8(matroska, &id, &title);
2255 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2257 res = ebml_read_skip(matroska);
2261 if (matroska->level_up) {
2262 matroska->level_up--;
2268 case MATROSKA_ID_CHAPTERUID:
2269 res = ebml_read_uint(matroska, &id, &uid);
2272 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2273 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2275 res = ebml_read_skip(matroska);
2279 if (matroska->level_up) {
2280 matroska->level_up--;
2285 if (start != AV_NOPTS_VALUE && uid != -1) {
2286 if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2287 res= AVERROR(ENOMEM);
2293 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2294 case MATROSKA_ID_EDITIONUID:
2295 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2296 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2298 res = ebml_read_skip(matroska);
2303 if (matroska->level_up) {
2304 matroska->level_up--;
2312 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2314 res = ebml_read_skip(matroska);
2318 if (matroska->level_up) {
2319 matroska->level_up--;
2328 matroska_aac_profile (char *codec_id)
2330 static const char *aac_profiles[] = {
2335 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2336 if (strstr(codec_id, aac_profiles[profile]))
2342 matroska_aac_sri (int samplerate)
2346 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2347 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2353 matroska_read_header (AVFormatContext *s,
2354 AVFormatParameters *ap)
2356 MatroskaDemuxContext *matroska = s->priv_data;
2358 int version, last_level, res = 0;
2363 /* First read the EBML header. */
2365 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2367 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2368 av_log(matroska->ctx, AV_LOG_ERROR,
2369 "Wrong EBML doctype ('%s' != 'matroska').\n",
2370 doctype ? doctype : "(none)");
2373 return AVERROR_NOFMT;
2377 av_log(matroska->ctx, AV_LOG_ERROR,
2378 "Matroska demuxer version 2 too old for file version %d\n",
2380 return AVERROR_NOFMT;
2383 /* The next thing is a segment. */
2385 if (!(id = ebml_peek_id(matroska, &last_level)))
2386 return AVERROR(EIO);
2387 if (id == MATROSKA_ID_SEGMENT)
2391 av_log(matroska->ctx, AV_LOG_INFO,
2392 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2393 MATROSKA_ID_SEGMENT, id);
2394 if ((res = ebml_read_skip(matroska)) < 0)
2398 /* We now have a Matroska segment.
2399 * Seeks are from the beginning of the segment,
2400 * after the segment ID/length. */
2401 if ((res = ebml_read_master(matroska, &id)) < 0)
2403 matroska->segment_start = url_ftell(s->pb);
2405 matroska->time_scale = 1000000;
2406 /* we've found our segment, start reading the different contents in here */
2408 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2411 } else if (matroska->level_up) {
2412 matroska->level_up--;
2418 case MATROSKA_ID_INFO: {
2419 if ((res = ebml_read_master(matroska, &id)) < 0)
2421 res = matroska_parse_info(matroska);
2425 /* track info headers */
2426 case MATROSKA_ID_TRACKS: {
2427 if ((res = ebml_read_master(matroska, &id)) < 0)
2429 res = matroska_parse_tracks(matroska);
2434 case MATROSKA_ID_CUES: {
2435 if (!matroska->index_parsed) {
2436 if ((res = ebml_read_master(matroska, &id)) < 0)
2438 res = matroska_parse_index(matroska);
2440 res = ebml_read_skip(matroska);
2445 case MATROSKA_ID_TAGS: {
2446 if (!matroska->metadata_parsed) {
2447 if ((res = ebml_read_master(matroska, &id)) < 0)
2449 res = matroska_parse_metadata(matroska);
2451 res = ebml_read_skip(matroska);
2455 /* file index (if seekable, seek to Cues/Tags to parse it) */
2456 case MATROSKA_ID_SEEKHEAD: {
2457 if ((res = ebml_read_master(matroska, &id)) < 0)
2459 res = matroska_parse_seekhead(matroska);
2463 case MATROSKA_ID_ATTACHMENTS: {
2464 if ((res = ebml_read_master(matroska, &id)) < 0)
2466 res = matroska_parse_attachments(s);
2470 case MATROSKA_ID_CLUSTER: {
2471 /* Do not read the master - this will be done in the next
2472 * call to matroska_read_packet. */
2477 case MATROSKA_ID_CHAPTERS: {
2478 if ((res = ebml_read_master(matroska, &id)) < 0)
2480 res = matroska_parse_chapters(s);
2485 av_log(matroska->ctx, AV_LOG_INFO,
2486 "Unknown matroska file header ID 0x%x\n", id);
2490 res = ebml_read_skip(matroska);
2494 if (matroska->level_up) {
2495 matroska->level_up--;
2500 /* Have we found a cluster? */
2501 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2503 MatroskaTrack *track;
2506 for (i = 0; i < matroska->num_tracks; i++) {
2507 enum CodecID codec_id = CODEC_ID_NONE;
2508 uint8_t *extradata = NULL;
2509 int extradata_size = 0;
2510 int extradata_offset = 0;
2511 track = matroska->tracks[i];
2512 track->stream_index = -1;
2514 /* Apply some sanity checks. */
2515 if (track->codec_id == NULL)
2518 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2519 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2520 strlen(ff_mkv_codec_tags[j].str))){
2521 codec_id= ff_mkv_codec_tags[j].id;
2526 /* Set the FourCC from the CodecID. */
2527 /* This is the MS compatibility mode which stores a
2528 * BITMAPINFOHEADER in the CodecPrivate. */
2529 if (!strcmp(track->codec_id,
2530 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2531 (track->codec_priv_size >= 40) &&
2532 (track->codec_priv != NULL)) {
2533 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2535 /* Offset of biCompression. Stored in LE. */
2536 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2537 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2541 /* This is the MS compatibility mode which stores a
2542 * WAVEFORMATEX in the CodecPrivate. */
2543 else if (!strcmp(track->codec_id,
2544 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2545 (track->codec_priv_size >= 18) &&
2546 (track->codec_priv != NULL)) {
2549 /* Offset of wFormatTag. Stored in LE. */
2550 tag = AV_RL16(track->codec_priv);
2551 codec_id = codec_get_id(codec_wav_tags, tag);
2555 if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2556 (track->codec_priv_size >= 86) &&
2557 (track->codec_priv != NULL)) {
2558 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2560 vtrack->fourcc = AV_RL32(track->codec_priv);
2561 codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2564 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2565 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2566 int profile = matroska_aac_profile(track->codec_id);
2567 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2568 extradata = av_malloc(5);
2569 if (extradata == NULL)
2570 return AVERROR(ENOMEM);
2571 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2572 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2573 if (strstr(track->codec_id, "SBR")) {
2574 sri = matroska_aac_sri(audiotrack->samplerate);
2575 extradata[2] = 0x56;
2576 extradata[3] = 0xE5;
2577 extradata[4] = 0x80 | (sri<<3);
2584 else if (codec_id == CODEC_ID_TTA) {
2585 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2587 extradata_size = 30;
2588 extradata = av_mallocz(extradata_size);
2589 if (extradata == NULL)
2590 return AVERROR(ENOMEM);
2591 init_put_byte(&b, extradata, extradata_size, 1,
2592 NULL, NULL, NULL, NULL);
2593 put_buffer(&b, "TTA1", 4);
2595 put_le16(&b, audiotrack->channels);
2596 put_le16(&b, audiotrack->bitdepth);
2597 put_le32(&b, audiotrack->samplerate);
2598 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2601 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2602 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2603 extradata_offset = 26;
2604 track->codec_priv_size -= extradata_offset;
2607 else if (codec_id == CODEC_ID_RA_144) {
2608 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2609 audiotrack->samplerate = 8000;
2610 audiotrack->channels = 1;
2613 else if (codec_id == CODEC_ID_RA_288 ||
2614 codec_id == CODEC_ID_COOK ||
2615 codec_id == CODEC_ID_ATRAC3) {
2616 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2619 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2620 NULL, NULL, NULL, NULL);
2622 audiotrack->coded_framesize = get_be32(&b);
2624 audiotrack->sub_packet_h = get_be16(&b);
2625 audiotrack->frame_size = get_be16(&b);
2626 audiotrack->sub_packet_size = get_be16(&b);
2627 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2628 if (codec_id == CODEC_ID_RA_288) {
2629 audiotrack->block_align = audiotrack->coded_framesize;
2630 track->codec_priv_size = 0;
2632 audiotrack->block_align = audiotrack->sub_packet_size;
2633 extradata_offset = 78;
2634 track->codec_priv_size -= extradata_offset;
2638 if (codec_id == CODEC_ID_NONE) {
2639 av_log(matroska->ctx, AV_LOG_INFO,
2640 "Unknown/unsupported CodecID %s.\n",
2644 track->stream_index = matroska->num_streams;
2646 matroska->num_streams++;
2647 st = av_new_stream(s, track->stream_index);
2649 return AVERROR(ENOMEM);
2650 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2652 st->codec->codec_id = codec_id;
2654 if (strcmp(track->language, "und"))
2655 av_strlcpy(st->language, track->language, 4);
2657 if (track->flags & MATROSKA_TRACK_DEFAULT)
2658 st->disposition |= AV_DISPOSITION_DEFAULT;
2660 if (track->default_duration)
2661 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2662 track->default_duration, 1000000000, 30000);
2665 st->codec->extradata = extradata;
2666 st->codec->extradata_size = extradata_size;
2667 } else if(track->codec_priv && track->codec_priv_size > 0){
2668 st->codec->extradata = av_malloc(track->codec_priv_size);
2669 if(st->codec->extradata == NULL)
2670 return AVERROR(ENOMEM);
2671 st->codec->extradata_size = track->codec_priv_size;
2672 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2673 track->codec_priv_size);
2676 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2677 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2679 st->codec->codec_type = CODEC_TYPE_VIDEO;
2680 st->codec->codec_tag = videotrack->fourcc;
2681 st->codec->width = videotrack->pixel_width;
2682 st->codec->height = videotrack->pixel_height;
2683 if (videotrack->display_width == 0)
2684 videotrack->display_width= videotrack->pixel_width;
2685 if (videotrack->display_height == 0)
2686 videotrack->display_height= videotrack->pixel_height;
2687 av_reduce(&st->codec->sample_aspect_ratio.num,
2688 &st->codec->sample_aspect_ratio.den,
2689 st->codec->height * videotrack->display_width,
2690 st->codec-> width * videotrack->display_height,
2692 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2693 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2694 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2696 st->codec->codec_type = CODEC_TYPE_AUDIO;
2697 st->codec->sample_rate = audiotrack->samplerate;
2698 st->codec->channels = audiotrack->channels;
2699 st->codec->block_align = audiotrack->block_align;
2700 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2701 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2704 /* What do we do with private data? E.g. for Vorbis. */
2709 if (matroska->index_parsed) {
2710 int i, track, stream;
2711 for (i=0; i<matroska->num_indexes; i++) {
2712 MatroskaDemuxIndex *idx = &matroska->index[i];
2713 track = matroska_find_track_by_num(matroska, idx->track);
2714 if (track < 0) continue;
2715 stream = matroska->tracks[track]->stream_index;
2716 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2717 av_add_index_entry(matroska->ctx->streams[stream],
2718 idx->pos, idx->time/AV_TIME_BASE,
2719 0, 0, AVINDEX_KEYFRAME);
2727 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2728 int64_t pos, uint64_t cluster_time, uint64_t duration,
2729 int is_keyframe, int is_bframe)
2735 uint8_t *origdata = data;
2737 uint32_t *lace_size = NULL;
2738 int n, flags, laces = 0;
2742 /* first byte(s): tracknum */
2743 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2744 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2751 /* fetch track from num */
2752 track = matroska_find_track_by_num(matroska, num);
2753 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2754 av_log(matroska->ctx, AV_LOG_INFO,
2755 "Invalid stream %d or size %u\n", track, size);
2759 stream_index = matroska->tracks[track]->stream_index;
2760 if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2764 st = matroska->ctx->streams[stream_index];
2765 if (st->discard >= AVDISCARD_ALL) {
2769 if (duration == AV_NOPTS_VALUE)
2770 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2772 /* block_time (relative to cluster time) */
2773 block_time = AV_RB16(data);
2777 if (is_keyframe == -1)
2778 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2780 if (matroska->skip_to_keyframe) {
2781 if (!is_keyframe || st != matroska->skip_to_stream) {
2785 matroska->skip_to_keyframe = 0;
2788 switch ((flags & 0x06) >> 1) {
2789 case 0x0: /* no lacing */
2791 lace_size = av_mallocz(sizeof(int));
2792 lace_size[0] = size;
2795 case 0x1: /* xiph lacing */
2796 case 0x2: /* fixed-size lacing */
2797 case 0x3: /* EBML lacing */
2798 assert(size>0); // size <=3 is checked before size-=3 above
2799 laces = (*data) + 1;
2802 lace_size = av_mallocz(laces * sizeof(int));
2804 switch ((flags & 0x06) >> 1) {
2805 case 0x1: /* xiph lacing */ {
2808 for (n = 0; res == 0 && n < laces - 1; n++) {
2815 lace_size[n] += temp;
2821 total += lace_size[n];
2823 lace_size[n] = size - total;
2827 case 0x2: /* fixed-size lacing */
2828 for (n = 0; n < laces; n++)
2829 lace_size[n] = size / laces;
2832 case 0x3: /* EBML lacing */ {
2834 n = matroska_ebmlnum_uint(data, size, &num);
2836 av_log(matroska->ctx, AV_LOG_INFO,
2837 "EBML block data error\n");
2842 total = lace_size[0] = num;
2843 for (n = 1; res == 0 && n < laces - 1; n++) {
2846 r = matroska_ebmlnum_sint (data, size, &snum);
2848 av_log(matroska->ctx, AV_LOG_INFO,
2849 "EBML block data error\n");
2854 lace_size[n] = lace_size[n - 1] + snum;
2855 total += lace_size[n];
2857 lace_size[n] = size - total;
2865 uint64_t timecode = AV_NOPTS_VALUE;
2867 if (cluster_time != (uint64_t)-1
2868 && (block_time >= 0 || cluster_time >= -block_time))
2869 timecode = cluster_time + block_time;
2871 for (n = 0; n < laces; n++) {
2872 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2873 st->codec->codec_id == CODEC_ID_COOK ||
2874 st->codec->codec_id == CODEC_ID_ATRAC3) {
2875 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2876 int a = st->codec->block_align;
2877 int sps = audiotrack->sub_packet_size;
2878 int cfs = audiotrack->coded_framesize;
2879 int h = audiotrack->sub_packet_h;
2880 int y = audiotrack->sub_packet_cnt;
2881 int w = audiotrack->frame_size;
2884 if (!audiotrack->pkt_cnt) {
2885 if (st->codec->codec_id == CODEC_ID_RA_288)
2886 for (x=0; x<h/2; x++)
2887 memcpy(audiotrack->buf+x*2*w+y*cfs,
2890 for (x=0; x<w/sps; x++)
2891 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2893 if (++audiotrack->sub_packet_cnt >= h) {
2894 audiotrack->sub_packet_cnt = 0;
2895 audiotrack->pkt_cnt = h*w / a;
2898 while (audiotrack->pkt_cnt) {
2899 pkt = av_mallocz(sizeof(AVPacket));
2900 av_new_packet(pkt, a);
2901 memcpy(pkt->data, audiotrack->buf
2902 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2904 pkt->stream_index = stream_index;
2905 matroska_queue_packet(matroska, pkt);
2908 int offset = 0, pkt_size = lace_size[n];
2909 uint8_t *pkt_data = data;
2911 if (matroska->tracks[track]->encoding_scope & 1) {
2912 offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2913 matroska->tracks[track]);
2918 pkt = av_mallocz(sizeof(AVPacket));
2919 /* XXX: prevent data copy... */
2920 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2922 res = AVERROR(ENOMEM);
2927 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2928 memcpy (pkt->data+offset, pkt_data, pkt_size);
2930 if (pkt_data != data)
2934 pkt->flags = is_keyframe;
2935 pkt->stream_index = stream_index;
2937 pkt->pts = timecode;
2939 pkt->duration = duration;
2941 matroska_queue_packet(matroska, pkt);
2944 if (timecode != AV_NOPTS_VALUE)
2945 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2946 data += lace_size[n];
2956 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2957 uint64_t cluster_time)
2962 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2963 uint64_t duration = AV_NOPTS_VALUE;
2968 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2971 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2974 } else if (matroska->level_up) {
2975 matroska->level_up--;
2980 /* one block inside the group. Note, block parsing is one
2981 * of the harder things, so this code is a bit complicated.
2982 * See http://www.matroska.org/ for documentation. */
2983 case MATROSKA_ID_BLOCK: {
2984 pos = url_ftell(matroska->ctx->pb);
2985 res = ebml_read_binary(matroska, &id, &data, &size);
2989 case MATROSKA_ID_BLOCKDURATION: {
2990 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2995 case MATROSKA_ID_BLOCKREFERENCE: {
2997 /* We've found a reference, so not even the first frame in
2998 * the lace is a key frame. */
3000 if (last_num_packets != matroska->num_packets)
3001 matroska->packets[last_num_packets]->flags = 0;
3002 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3010 av_log(matroska->ctx, AV_LOG_INFO,
3011 "Unknown entry 0x%x in blockgroup data\n", id);
3015 res = ebml_read_skip(matroska);
3019 if (matroska->level_up) {
3020 matroska->level_up--;
3029 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3030 duration, is_keyframe, is_bframe);
3036 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3040 uint64_t cluster_time = 0;
3045 av_log(matroska->ctx, AV_LOG_DEBUG,
3046 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3049 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3052 } else if (matroska->level_up) {
3053 matroska->level_up--;
3058 /* cluster timecode */
3059 case MATROSKA_ID_CLUSTERTIMECODE: {
3061 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3067 /* a group of blocks inside a cluster */
3068 case MATROSKA_ID_BLOCKGROUP:
3069 if ((res = ebml_read_master(matroska, &id)) < 0)
3071 res = matroska_parse_blockgroup(matroska, cluster_time);
3074 case MATROSKA_ID_SIMPLEBLOCK:
3075 pos = url_ftell(matroska->ctx->pb);
3076 res = ebml_read_binary(matroska, &id, &data, &size);
3078 res = matroska_parse_block(matroska, data, size, pos,
3079 cluster_time, AV_NOPTS_VALUE,
3084 av_log(matroska->ctx, AV_LOG_INFO,
3085 "Unknown entry 0x%x in cluster data\n", id);
3089 res = ebml_read_skip(matroska);
3093 if (matroska->level_up) {
3094 matroska->level_up--;
3103 matroska_read_packet (AVFormatContext *s,
3106 MatroskaDemuxContext *matroska = s->priv_data;
3110 /* Read stream until we have a packet queued. */
3111 while (matroska_deliver_packet(matroska, pkt)) {
3113 /* Have we already reached the end? */
3115 return AVERROR(EIO);
3119 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3120 return AVERROR(EIO);
3121 } else if (matroska->level_up) {
3122 matroska->level_up--;
3127 case MATROSKA_ID_CLUSTER:
3128 if ((res = ebml_read_master(matroska, &id)) < 0)
3130 if ((res = matroska_parse_cluster(matroska)) == 0)
3131 res = 1; /* Parsed one cluster, let's get out. */
3136 res = ebml_read_skip(matroska);
3140 if (matroska->level_up) {
3141 matroska->level_up--;
3154 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3157 MatroskaDemuxContext *matroska = s->priv_data;
3158 AVStream *st = s->streams[stream_index];
3161 /* find index entry */
3162 index = av_index_search_timestamp(st, timestamp, flags);
3166 matroska_clear_queue(matroska);
3169 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3170 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3171 matroska->skip_to_stream = st;
3172 matroska->peek_id = 0;
3173 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3178 matroska_read_close (AVFormatContext *s)
3180 MatroskaDemuxContext *matroska = s->priv_data;
3183 av_free(matroska->index);
3185 matroska_clear_queue(matroska);
3187 for (n = 0; n < matroska->num_tracks; n++) {
3188 MatroskaTrack *track = matroska->tracks[n];
3189 av_free(track->codec_id);
3190 av_free(track->codec_priv);
3191 av_free(track->name);
3193 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3194 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3195 av_free(audiotrack->buf);
3204 AVInputFormat matroska_demuxer = {
3206 NULL_IF_CONFIG_SMALL("Matroska file format"),
3207 sizeof(MatroskaDemuxContext),
3209 matroska_read_header,
3210 matroska_read_packet,
3211 matroska_read_close,