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,
1224 /* colorspace (only matters for raw video)
1226 case MATROSKA_ID_VIDEOCOLORSPACE: {
1228 if ((res = ebml_read_uint(matroska, &id,
1231 videotrack->fourcc = num;
1236 av_log(matroska->ctx, AV_LOG_INFO,
1237 "Unknown video track header entry "
1238 "0x%x - ignoring\n", id);
1241 case MATROSKA_ID_VIDEOSTEREOMODE:
1242 case MATROSKA_ID_VIDEOASPECTRATIO:
1244 res = ebml_read_skip(matroska);
1248 if (matroska->level_up) {
1249 matroska->level_up--;
1256 /* tracktype specific stuff for audio */
1257 case MATROSKA_ID_TRACKAUDIO: {
1258 MatroskaAudioTrack *audiotrack;
1260 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1261 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1262 av_log(matroska->ctx, AV_LOG_INFO,
1263 "audio data in non-audio track - ignoring\n");
1264 res = AVERROR_INVALIDDATA;
1266 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1268 audiotrack = (MatroskaAudioTrack *)track;
1269 audiotrack->channels = 1;
1270 audiotrack->samplerate = 8000;
1273 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1276 } else if (matroska->level_up > 0) {
1277 matroska->level_up--;
1283 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1285 if ((res = ebml_read_float(matroska, &id,
1288 audiotrack->internal_samplerate =
1289 audiotrack->samplerate = num;
1293 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1295 if ((res = ebml_read_float(matroska, &id,
1298 audiotrack->samplerate = num;
1303 case MATROSKA_ID_AUDIOBITDEPTH: {
1305 if ((res = ebml_read_uint(matroska, &id,
1308 audiotrack->bitdepth = num;
1313 case MATROSKA_ID_AUDIOCHANNELS: {
1315 if ((res = ebml_read_uint(matroska, &id,
1318 audiotrack->channels = num;
1323 av_log(matroska->ctx, AV_LOG_INFO,
1324 "Unknown audio track header entry "
1325 "0x%x - ignoring\n", id);
1329 res = ebml_read_skip(matroska);
1333 if (matroska->level_up) {
1334 matroska->level_up--;
1341 /* codec identifier */
1342 case MATROSKA_ID_CODECID: {
1344 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1346 track->codec_id = text;
1350 /* codec private data */
1351 case MATROSKA_ID_CODECPRIVATE: {
1354 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1356 track->codec_priv = data;
1357 track->codec_priv_size = size;
1361 /* name of this track */
1362 case MATROSKA_ID_TRACKNAME: {
1364 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1370 /* language (matters for audio/subtitles, mostly) */
1371 case MATROSKA_ID_TRACKLANGUAGE: {
1373 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1375 if ((end = strchr(text, '-')))
1377 if (strlen(text) == 3)
1378 strcpy(track->language, text);
1383 /* whether this is actually used */
1384 case MATROSKA_ID_TRACKFLAGENABLED: {
1386 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1391 /* whether it's the default for this track type */
1392 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1394 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1397 track->flags |= MATROSKA_TRACK_DEFAULT;
1399 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1403 /* lacing (like MPEG, where blocks don't end/start on frame
1405 case MATROSKA_ID_TRACKFLAGLACING: {
1407 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1412 /* default length (in time) of one data block in this track */
1413 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1415 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1417 track->default_duration = num;
1421 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1422 if ((res = ebml_read_master(matroska, &id)) < 0)
1426 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1429 } else if (matroska->level_up > 0) {
1430 matroska->level_up--;
1435 case MATROSKA_ID_TRACKCONTENTENCODING: {
1436 int encoding_scope = 1;
1437 if ((res = ebml_read_master(matroska, &id)) < 0)
1441 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1444 } else if (matroska->level_up > 0) {
1445 matroska->level_up--;
1450 case MATROSKA_ID_ENCODINGSCOPE: {
1452 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1454 encoding_scope = num;
1458 case MATROSKA_ID_ENCODINGTYPE: {
1460 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1463 av_log(matroska->ctx, AV_LOG_ERROR,
1464 "Unsupported encoding type");
1468 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1469 if ((res = ebml_read_master(matroska, &id)) < 0)
1473 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1476 } else if (matroska->level_up > 0) {
1477 matroska->level_up--;
1482 case MATROSKA_ID_ENCODINGCOMPALGO: {
1484 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1486 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1488 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1491 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1493 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1494 av_log(matroska->ctx, AV_LOG_ERROR,
1495 "Unsupported compression algo\n");
1496 track->encoding_algo = num;
1500 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1503 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1505 track->encoding_settings = data;
1506 track->encoding_settings_len = size;
1511 av_log(matroska->ctx, AV_LOG_INFO,
1512 "Unknown compression header entry "
1513 "0x%x - ignoring\n", id);
1517 res = ebml_read_skip(matroska);
1521 if (matroska->level_up) {
1522 matroska->level_up--;
1530 av_log(matroska->ctx, AV_LOG_INFO,
1531 "Unknown content encoding header entry "
1532 "0x%x - ignoring\n", id);
1536 res = ebml_read_skip(matroska);
1540 if (matroska->level_up) {
1541 matroska->level_up--;
1546 track->encoding_scope = encoding_scope;
1551 av_log(matroska->ctx, AV_LOG_INFO,
1552 "Unknown content encodings header entry "
1553 "0x%x - ignoring\n", id);
1557 res = ebml_read_skip(matroska);
1561 if (matroska->level_up) {
1562 matroska->level_up--;
1569 case MATROSKA_ID_TRACKTIMECODESCALE: {
1571 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1573 track->time_scale = num;
1578 av_log(matroska->ctx, AV_LOG_INFO,
1579 "Unknown track header entry 0x%x - ignoring\n", id);
1583 /* we ignore these because they're nothing useful. */
1584 case MATROSKA_ID_TRACKFLAGFORCED:
1585 case MATROSKA_ID_CODECNAME:
1586 case MATROSKA_ID_CODECDECODEALL:
1587 case MATROSKA_ID_CODECINFOURL:
1588 case MATROSKA_ID_CODECDOWNLOADURL:
1589 case MATROSKA_ID_TRACKMINCACHE:
1590 case MATROSKA_ID_TRACKMAXCACHE:
1591 res = ebml_read_skip(matroska);
1595 if (matroska->level_up) {
1596 matroska->level_up--;
1601 if (track->codec_priv_size && track->encoding_scope & 2) {
1602 uint8_t *orig_priv = track->codec_priv;
1603 int offset = matroska_decode_buffer(&track->codec_priv,
1604 &track->codec_priv_size, track);
1606 track->codec_priv = av_malloc(track->codec_priv_size + offset);
1607 memcpy(track->codec_priv, track->encoding_settings, offset);
1608 memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1609 track->codec_priv_size += offset;
1611 } else if (!offset) {
1614 av_log(matroska->ctx, AV_LOG_ERROR,
1615 "Failed to decode codec private data\n");
1618 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1619 matroska->tracks[matroska->num_tracks++] = track;
1627 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1632 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1635 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1638 } else if (matroska->level_up) {
1639 matroska->level_up--;
1644 /* one track within the "all-tracks" header */
1645 case MATROSKA_ID_TRACKENTRY:
1646 res = matroska_add_stream(matroska);
1650 av_log(matroska->ctx, AV_LOG_INFO,
1651 "Unknown entry 0x%x in track header\n", id);
1655 res = ebml_read_skip(matroska);
1659 if (matroska->level_up) {
1660 matroska->level_up--;
1669 matroska_parse_index (MatroskaDemuxContext *matroska)
1673 MatroskaDemuxIndex idx;
1675 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1678 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1681 } else if (matroska->level_up) {
1682 matroska->level_up--;
1687 /* one single index entry ('point') */
1688 case MATROSKA_ID_POINTENTRY:
1689 if ((res = ebml_read_master(matroska, &id)) < 0)
1692 /* in the end, we hope to fill one entry with a
1693 * timestamp, a file position and a tracknum */
1694 idx.pos = (uint64_t) -1;
1695 idx.time = (uint64_t) -1;
1696 idx.track = (uint16_t) -1;
1699 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1702 } else if (matroska->level_up) {
1703 matroska->level_up--;
1708 /* one single index entry ('point') */
1709 case MATROSKA_ID_CUETIME: {
1711 if ((res = ebml_read_uint(matroska, &id,
1714 idx.time = time * matroska->time_scale;
1718 /* position in the file + track to which it
1720 case MATROSKA_ID_CUETRACKPOSITION:
1721 if ((res = ebml_read_master(matroska, &id)) < 0)
1725 if (!(id = ebml_peek_id (matroska,
1726 &matroska->level_up))) {
1729 } else if (matroska->level_up) {
1730 matroska->level_up--;
1736 case MATROSKA_ID_CUETRACK: {
1738 if ((res = ebml_read_uint(matroska,
1745 /* position in file */
1746 case MATROSKA_ID_CUECLUSTERPOSITION: {
1748 if ((res = ebml_read_uint(matroska,
1751 idx.pos = num+matroska->segment_start;
1756 av_log(matroska->ctx, AV_LOG_INFO,
1757 "Unknown entry 0x%x in "
1758 "CuesTrackPositions\n", id);
1762 res = ebml_read_skip(matroska);
1766 if (matroska->level_up) {
1767 matroska->level_up--;
1775 av_log(matroska->ctx, AV_LOG_INFO,
1776 "Unknown entry 0x%x in cuespoint "
1781 res = ebml_read_skip(matroska);
1785 if (matroska->level_up) {
1786 matroska->level_up--;
1791 /* so let's see if we got what we wanted */
1792 if (idx.pos != (uint64_t) -1 &&
1793 idx.time != (uint64_t) -1 &&
1794 idx.track != (uint16_t) -1) {
1795 if (matroska->num_indexes % 32 == 0) {
1796 /* re-allocate bigger index */
1798 av_realloc(matroska->index,
1799 (matroska->num_indexes + 32) *
1800 sizeof(MatroskaDemuxIndex));
1802 matroska->index[matroska->num_indexes] = idx;
1803 matroska->num_indexes++;
1808 av_log(matroska->ctx, AV_LOG_INFO,
1809 "Unknown entry 0x%x in cues header\n", id);
1813 res = ebml_read_skip(matroska);
1817 if (matroska->level_up) {
1818 matroska->level_up--;
1827 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1833 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1836 } else if (matroska->level_up) {
1837 matroska->level_up--;
1842 /* Hm, this is unsupported... */
1844 av_log(matroska->ctx, AV_LOG_INFO,
1845 "Unknown entry 0x%x in metadata header\n", id);
1849 res = ebml_read_skip(matroska);
1853 if (matroska->level_up) {
1854 matroska->level_up--;
1863 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1868 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1871 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1874 } else if (matroska->level_up) {
1875 matroska->level_up--;
1880 case MATROSKA_ID_SEEKENTRY: {
1881 uint32_t seek_id = 0, peek_id_cache = 0;
1882 uint64_t seek_pos = (uint64_t) -1, t;
1883 int dummy_level = 0;
1885 if ((res = ebml_read_master(matroska, &id)) < 0)
1889 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1892 } else if (matroska->level_up) {
1893 matroska->level_up--;
1898 case MATROSKA_ID_SEEKID:
1899 res = ebml_read_uint(matroska, &id, &t);
1903 case MATROSKA_ID_SEEKPOSITION:
1904 res = ebml_read_uint(matroska, &id, &seek_pos);
1908 av_log(matroska->ctx, AV_LOG_INFO,
1909 "Unknown seekhead ID 0x%x\n", id);
1913 res = ebml_read_skip(matroska);
1917 if (matroska->level_up) {
1918 matroska->level_up--;
1923 if (!seek_id || seek_pos == (uint64_t) -1) {
1924 av_log(matroska->ctx, AV_LOG_INFO,
1925 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1931 case MATROSKA_ID_CUES:
1932 case MATROSKA_ID_TAGS: {
1933 uint32_t level_up = matroska->level_up;
1934 offset_t before_pos;
1936 MatroskaLevel level;
1938 /* remember the peeked ID and the current position */
1939 peek_id_cache = matroska->peek_id;
1940 before_pos = url_ftell(matroska->ctx->pb);
1943 if ((res = ebml_read_seek(matroska, seek_pos +
1944 matroska->segment_start)) < 0)
1947 /* we don't want to lose our seekhead level, so we add
1948 * a dummy. This is a crude hack. */
1949 if (matroska->num_levels == EBML_MAX_DEPTH) {
1950 av_log(matroska->ctx, AV_LOG_INFO,
1951 "Max EBML element depth (%d) reached, "
1952 "cannot parse further.\n", EBML_MAX_DEPTH);
1953 return AVERROR_UNKNOWN;
1957 level.length = (uint64_t)-1;
1958 matroska->levels[matroska->num_levels] = level;
1959 matroska->num_levels++;
1963 if (!(id = ebml_peek_id (matroska,
1964 &matroska->level_up)))
1966 if (id != seek_id) {
1967 av_log(matroska->ctx, AV_LOG_INFO,
1968 "We looked for ID=0x%x but got "
1969 "ID=0x%x (pos=%"PRIu64")",
1970 seek_id, id, seek_pos +
1971 matroska->segment_start);
1975 /* read master + parse */
1976 if ((res = ebml_read_master(matroska, &id)) < 0)
1979 case MATROSKA_ID_CUES:
1980 if (!(res = matroska_parse_index(matroska)) ||
1981 url_feof(matroska->ctx->pb)) {
1982 matroska->index_parsed = 1;
1986 case MATROSKA_ID_TAGS:
1987 if (!(res = matroska_parse_metadata(matroska)) ||
1988 url_feof(matroska->ctx->pb)) {
1989 matroska->metadata_parsed = 1;
1996 /* remove dummy level */
1998 while (matroska->num_levels) {
1999 matroska->num_levels--;
2001 matroska->levels[matroska->num_levels].length;
2002 if (length == (uint64_t)-1)
2007 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2009 matroska->peek_id = peek_id_cache;
2010 matroska->level_up = level_up;
2015 av_log(matroska->ctx, AV_LOG_INFO,
2016 "Ignoring seekhead entry for ID=0x%x\n",
2025 av_log(matroska->ctx, AV_LOG_INFO,
2026 "Unknown seekhead ID 0x%x\n", id);
2030 res = ebml_read_skip(matroska);
2034 if (matroska->level_up) {
2035 matroska->level_up--;
2044 matroska_parse_attachments(AVFormatContext *s)
2046 MatroskaDemuxContext *matroska = s->priv_data;
2050 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2053 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2056 } else if (matroska->level_up) {
2057 matroska->level_up--;
2062 case MATROSKA_ID_ATTACHEDFILE: {
2065 uint8_t* data = NULL;
2066 int i, data_size = 0;
2069 if ((res = ebml_read_master(matroska, &id)) < 0)
2073 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2076 } else if (matroska->level_up) {
2077 matroska->level_up--;
2082 case MATROSKA_ID_FILENAME:
2083 res = ebml_read_utf8 (matroska, &id, &name);
2086 case MATROSKA_ID_FILEMIMETYPE:
2087 res = ebml_read_ascii (matroska, &id, &mime);
2090 case MATROSKA_ID_FILEDATA:
2091 res = ebml_read_binary(matroska, &id, &data, &data_size);
2095 av_log(matroska->ctx, AV_LOG_INFO,
2096 "Unknown attachedfile ID 0x%x\n", id);
2097 case MATROSKA_ID_FILEUID:
2099 res = ebml_read_skip(matroska);
2103 if (matroska->level_up) {
2104 matroska->level_up--;
2109 if (!(name && mime && data && data_size > 0)) {
2110 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2114 st = av_new_stream(s, matroska->num_streams++);
2116 return AVERROR(ENOMEM);
2117 st->filename = av_strdup(name);
2118 st->codec->codec_id = CODEC_ID_NONE;
2119 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2120 st->codec->extradata = av_malloc(data_size);
2121 if(st->codec->extradata == NULL)
2122 return AVERROR(ENOMEM);
2123 st->codec->extradata_size = data_size;
2124 memcpy(st->codec->extradata, data, data_size);
2126 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2127 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2128 strlen(ff_mkv_mime_tags[i].str))) {
2129 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2134 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2139 av_log(matroska->ctx, AV_LOG_INFO,
2140 "Unknown attachments ID 0x%x\n", id);
2144 res = ebml_read_skip(matroska);
2148 if (matroska->level_up) {
2149 matroska->level_up--;
2158 matroska_parse_chapters(AVFormatContext *s)
2160 MatroskaDemuxContext *matroska = s->priv_data;
2164 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2167 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2170 } else if (matroska->level_up) {
2171 matroska->level_up--;
2176 case MATROSKA_ID_EDITIONENTRY: {
2177 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2180 /* if there is more than one chapter edition
2181 we take only the first one */
2183 ebml_read_skip(matroska);
2187 if ((res = ebml_read_master(matroska, &id)) < 0)
2191 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2194 } else if (matroska->level_up) {
2195 matroska->level_up--;
2200 case MATROSKA_ID_CHAPTERATOM:
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_CHAPTERTIMEEND:
2215 res = ebml_read_uint(matroska, &id, &end);
2218 case MATROSKA_ID_CHAPTERTIMESTART:
2219 res = ebml_read_uint(matroska, &id, &start);
2222 case MATROSKA_ID_CHAPTERDISPLAY:
2223 if ((res = ebml_read_master(matroska, &id)) < 0)
2227 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2230 } else if (matroska->level_up) {
2231 matroska->level_up--;
2236 case MATROSKA_ID_CHAPSTRING:
2237 res = ebml_read_utf8(matroska, &id, &title);
2241 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2243 res = ebml_read_skip(matroska);
2247 if (matroska->level_up) {
2248 matroska->level_up--;
2254 case MATROSKA_ID_CHAPTERUID:
2255 res = ebml_read_uint(matroska, &id, &uid);
2258 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2259 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2261 res = ebml_read_skip(matroska);
2265 if (matroska->level_up) {
2266 matroska->level_up--;
2271 if (start != AV_NOPTS_VALUE && uid != -1) {
2272 if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2273 res= AVERROR(ENOMEM);
2279 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2280 case MATROSKA_ID_EDITIONUID:
2281 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2282 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2284 res = ebml_read_skip(matroska);
2289 if (matroska->level_up) {
2290 matroska->level_up--;
2298 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2300 res = ebml_read_skip(matroska);
2304 if (matroska->level_up) {
2305 matroska->level_up--;
2314 matroska_aac_profile (char *codec_id)
2316 static const char *aac_profiles[] = {
2321 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2322 if (strstr(codec_id, aac_profiles[profile]))
2328 matroska_aac_sri (int samplerate)
2332 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2333 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2339 matroska_read_header (AVFormatContext *s,
2340 AVFormatParameters *ap)
2342 MatroskaDemuxContext *matroska = s->priv_data;
2344 int version, last_level, res = 0;
2349 /* First read the EBML header. */
2351 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2353 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2354 av_log(matroska->ctx, AV_LOG_ERROR,
2355 "Wrong EBML doctype ('%s' != 'matroska').\n",
2356 doctype ? doctype : "(none)");
2359 return AVERROR_NOFMT;
2363 av_log(matroska->ctx, AV_LOG_ERROR,
2364 "Matroska demuxer version 2 too old for file version %d\n",
2366 return AVERROR_NOFMT;
2369 /* The next thing is a segment. */
2371 if (!(id = ebml_peek_id(matroska, &last_level)))
2372 return AVERROR(EIO);
2373 if (id == MATROSKA_ID_SEGMENT)
2377 av_log(matroska->ctx, AV_LOG_INFO,
2378 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2379 MATROSKA_ID_SEGMENT, id);
2380 if ((res = ebml_read_skip(matroska)) < 0)
2384 /* We now have a Matroska segment.
2385 * Seeks are from the beginning of the segment,
2386 * after the segment ID/length. */
2387 if ((res = ebml_read_master(matroska, &id)) < 0)
2389 matroska->segment_start = url_ftell(s->pb);
2391 matroska->time_scale = 1000000;
2392 /* we've found our segment, start reading the different contents in here */
2394 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2397 } else if (matroska->level_up) {
2398 matroska->level_up--;
2404 case MATROSKA_ID_INFO: {
2405 if ((res = ebml_read_master(matroska, &id)) < 0)
2407 res = matroska_parse_info(matroska);
2411 /* track info headers */
2412 case MATROSKA_ID_TRACKS: {
2413 if ((res = ebml_read_master(matroska, &id)) < 0)
2415 res = matroska_parse_tracks(matroska);
2420 case MATROSKA_ID_CUES: {
2421 if (!matroska->index_parsed) {
2422 if ((res = ebml_read_master(matroska, &id)) < 0)
2424 res = matroska_parse_index(matroska);
2426 res = ebml_read_skip(matroska);
2431 case MATROSKA_ID_TAGS: {
2432 if (!matroska->metadata_parsed) {
2433 if ((res = ebml_read_master(matroska, &id)) < 0)
2435 res = matroska_parse_metadata(matroska);
2437 res = ebml_read_skip(matroska);
2441 /* file index (if seekable, seek to Cues/Tags to parse it) */
2442 case MATROSKA_ID_SEEKHEAD: {
2443 if ((res = ebml_read_master(matroska, &id)) < 0)
2445 res = matroska_parse_seekhead(matroska);
2449 case MATROSKA_ID_ATTACHMENTS: {
2450 if ((res = ebml_read_master(matroska, &id)) < 0)
2452 res = matroska_parse_attachments(s);
2456 case MATROSKA_ID_CLUSTER: {
2457 /* Do not read the master - this will be done in the next
2458 * call to matroska_read_packet. */
2463 case MATROSKA_ID_CHAPTERS: {
2464 if ((res = ebml_read_master(matroska, &id)) < 0)
2466 res = matroska_parse_chapters(s);
2471 av_log(matroska->ctx, AV_LOG_INFO,
2472 "Unknown matroska file header ID 0x%x\n", id);
2476 res = ebml_read_skip(matroska);
2480 if (matroska->level_up) {
2481 matroska->level_up--;
2486 /* Have we found a cluster? */
2487 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2489 MatroskaTrack *track;
2492 for (i = 0; i < matroska->num_tracks; i++) {
2493 enum CodecID codec_id = CODEC_ID_NONE;
2494 uint8_t *extradata = NULL;
2495 int extradata_size = 0;
2496 int extradata_offset = 0;
2497 track = matroska->tracks[i];
2498 track->stream_index = -1;
2500 /* Apply some sanity checks. */
2501 if (track->codec_id == NULL)
2504 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2505 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2506 strlen(ff_mkv_codec_tags[j].str))){
2507 codec_id= ff_mkv_codec_tags[j].id;
2512 /* Set the FourCC from the CodecID. */
2513 /* This is the MS compatibility mode which stores a
2514 * BITMAPINFOHEADER in the CodecPrivate. */
2515 if (!strcmp(track->codec_id,
2516 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2517 (track->codec_priv_size >= 40) &&
2518 (track->codec_priv != NULL)) {
2519 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2521 /* Offset of biCompression. Stored in LE. */
2522 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2523 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2527 /* This is the MS compatibility mode which stores a
2528 * WAVEFORMATEX in the CodecPrivate. */
2529 else if (!strcmp(track->codec_id,
2530 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2531 (track->codec_priv_size >= 18) &&
2532 (track->codec_priv != NULL)) {
2535 /* Offset of wFormatTag. Stored in LE. */
2536 tag = AV_RL16(track->codec_priv);
2537 codec_id = codec_get_id(codec_wav_tags, tag);
2541 if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2542 (track->codec_priv_size >= 86) &&
2543 (track->codec_priv != NULL)) {
2544 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2546 vtrack->fourcc = AV_RL32(track->codec_priv);
2547 codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2550 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2551 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2552 int profile = matroska_aac_profile(track->codec_id);
2553 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2554 extradata = av_malloc(5);
2555 if (extradata == NULL)
2556 return AVERROR(ENOMEM);
2557 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2558 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2559 if (strstr(track->codec_id, "SBR")) {
2560 sri = matroska_aac_sri(audiotrack->samplerate);
2561 extradata[2] = 0x56;
2562 extradata[3] = 0xE5;
2563 extradata[4] = 0x80 | (sri<<3);
2570 else if (codec_id == CODEC_ID_TTA) {
2571 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2573 extradata_size = 30;
2574 extradata = av_mallocz(extradata_size);
2575 if (extradata == NULL)
2576 return AVERROR(ENOMEM);
2577 init_put_byte(&b, extradata, extradata_size, 1,
2578 NULL, NULL, NULL, NULL);
2579 put_buffer(&b, "TTA1", 4);
2581 put_le16(&b, audiotrack->channels);
2582 put_le16(&b, audiotrack->bitdepth);
2583 put_le32(&b, audiotrack->samplerate);
2584 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2587 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2588 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2589 extradata_offset = 26;
2590 track->codec_priv_size -= extradata_offset;
2593 else if (codec_id == CODEC_ID_RA_144) {
2594 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2595 audiotrack->samplerate = 8000;
2596 audiotrack->channels = 1;
2599 else if (codec_id == CODEC_ID_RA_288 ||
2600 codec_id == CODEC_ID_COOK ||
2601 codec_id == CODEC_ID_ATRAC3) {
2602 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2605 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2606 NULL, NULL, NULL, NULL);
2608 audiotrack->coded_framesize = get_be32(&b);
2610 audiotrack->sub_packet_h = get_be16(&b);
2611 audiotrack->frame_size = get_be16(&b);
2612 audiotrack->sub_packet_size = get_be16(&b);
2613 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2614 if (codec_id == CODEC_ID_RA_288) {
2615 audiotrack->block_align = audiotrack->coded_framesize;
2616 track->codec_priv_size = 0;
2618 audiotrack->block_align = audiotrack->sub_packet_size;
2619 extradata_offset = 78;
2620 track->codec_priv_size -= extradata_offset;
2624 if (codec_id == CODEC_ID_NONE) {
2625 av_log(matroska->ctx, AV_LOG_INFO,
2626 "Unknown/unsupported CodecID %s.\n",
2630 track->stream_index = matroska->num_streams;
2632 matroska->num_streams++;
2633 st = av_new_stream(s, track->stream_index);
2635 return AVERROR(ENOMEM);
2636 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2638 st->codec->codec_id = codec_id;
2640 if (strcmp(track->language, "und"))
2641 av_strlcpy(st->language, track->language, 4);
2643 if (track->flags & MATROSKA_TRACK_DEFAULT)
2644 st->disposition |= AV_DISPOSITION_DEFAULT;
2646 if (track->default_duration)
2647 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2648 track->default_duration, 1000000000, 30000);
2651 st->codec->extradata = extradata;
2652 st->codec->extradata_size = extradata_size;
2653 } else if(track->codec_priv && track->codec_priv_size > 0){
2654 st->codec->extradata = av_malloc(track->codec_priv_size);
2655 if(st->codec->extradata == NULL)
2656 return AVERROR(ENOMEM);
2657 st->codec->extradata_size = track->codec_priv_size;
2658 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2659 track->codec_priv_size);
2662 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2663 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2665 st->codec->codec_type = CODEC_TYPE_VIDEO;
2666 st->codec->codec_tag = videotrack->fourcc;
2667 st->codec->width = videotrack->pixel_width;
2668 st->codec->height = videotrack->pixel_height;
2669 if (videotrack->display_width == 0)
2670 videotrack->display_width= videotrack->pixel_width;
2671 if (videotrack->display_height == 0)
2672 videotrack->display_height= videotrack->pixel_height;
2673 av_reduce(&st->codec->sample_aspect_ratio.num,
2674 &st->codec->sample_aspect_ratio.den,
2675 st->codec->height * videotrack->display_width,
2676 st->codec-> width * videotrack->display_height,
2678 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2679 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2680 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2682 st->codec->codec_type = CODEC_TYPE_AUDIO;
2683 st->codec->sample_rate = audiotrack->samplerate;
2684 st->codec->channels = audiotrack->channels;
2685 st->codec->block_align = audiotrack->block_align;
2686 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2687 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2690 /* What do we do with private data? E.g. for Vorbis. */
2695 if (matroska->index_parsed) {
2696 int i, track, stream;
2697 for (i=0; i<matroska->num_indexes; i++) {
2698 MatroskaDemuxIndex *idx = &matroska->index[i];
2699 track = matroska_find_track_by_num(matroska, idx->track);
2700 if (track < 0) continue;
2701 stream = matroska->tracks[track]->stream_index;
2702 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2703 av_add_index_entry(matroska->ctx->streams[stream],
2704 idx->pos, idx->time/AV_TIME_BASE,
2705 0, 0, AVINDEX_KEYFRAME);
2713 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2714 int64_t pos, uint64_t cluster_time, uint64_t duration,
2715 int is_keyframe, int is_bframe)
2721 uint8_t *origdata = data;
2723 uint32_t *lace_size = NULL;
2724 int n, flags, laces = 0;
2728 /* first byte(s): tracknum */
2729 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2730 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2737 /* fetch track from num */
2738 track = matroska_find_track_by_num(matroska, num);
2739 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2740 av_log(matroska->ctx, AV_LOG_INFO,
2741 "Invalid stream %d or size %u\n", track, size);
2745 stream_index = matroska->tracks[track]->stream_index;
2746 if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2750 st = matroska->ctx->streams[stream_index];
2751 if (st->discard >= AVDISCARD_ALL) {
2755 if (duration == AV_NOPTS_VALUE)
2756 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2758 /* block_time (relative to cluster time) */
2759 block_time = AV_RB16(data);
2763 if (is_keyframe == -1)
2764 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2766 if (matroska->skip_to_keyframe) {
2767 if (!is_keyframe || st != matroska->skip_to_stream) {
2771 matroska->skip_to_keyframe = 0;
2774 switch ((flags & 0x06) >> 1) {
2775 case 0x0: /* no lacing */
2777 lace_size = av_mallocz(sizeof(int));
2778 lace_size[0] = size;
2781 case 0x1: /* xiph lacing */
2782 case 0x2: /* fixed-size lacing */
2783 case 0x3: /* EBML lacing */
2784 assert(size>0); // size <=3 is checked before size-=3 above
2785 laces = (*data) + 1;
2788 lace_size = av_mallocz(laces * sizeof(int));
2790 switch ((flags & 0x06) >> 1) {
2791 case 0x1: /* xiph lacing */ {
2794 for (n = 0; res == 0 && n < laces - 1; n++) {
2801 lace_size[n] += temp;
2807 total += lace_size[n];
2809 lace_size[n] = size - total;
2813 case 0x2: /* fixed-size lacing */
2814 for (n = 0; n < laces; n++)
2815 lace_size[n] = size / laces;
2818 case 0x3: /* EBML lacing */ {
2820 n = matroska_ebmlnum_uint(data, size, &num);
2822 av_log(matroska->ctx, AV_LOG_INFO,
2823 "EBML block data error\n");
2828 total = lace_size[0] = num;
2829 for (n = 1; res == 0 && n < laces - 1; n++) {
2832 r = matroska_ebmlnum_sint (data, size, &snum);
2834 av_log(matroska->ctx, AV_LOG_INFO,
2835 "EBML block data error\n");
2840 lace_size[n] = lace_size[n - 1] + snum;
2841 total += lace_size[n];
2843 lace_size[n] = size - total;
2851 uint64_t timecode = AV_NOPTS_VALUE;
2853 if (cluster_time != (uint64_t)-1
2854 && (block_time >= 0 || cluster_time >= -block_time))
2855 timecode = cluster_time + block_time;
2857 for (n = 0; n < laces; n++) {
2858 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2859 st->codec->codec_id == CODEC_ID_COOK ||
2860 st->codec->codec_id == CODEC_ID_ATRAC3) {
2861 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2862 int a = st->codec->block_align;
2863 int sps = audiotrack->sub_packet_size;
2864 int cfs = audiotrack->coded_framesize;
2865 int h = audiotrack->sub_packet_h;
2866 int y = audiotrack->sub_packet_cnt;
2867 int w = audiotrack->frame_size;
2870 if (!audiotrack->pkt_cnt) {
2871 if (st->codec->codec_id == CODEC_ID_RA_288)
2872 for (x=0; x<h/2; x++)
2873 memcpy(audiotrack->buf+x*2*w+y*cfs,
2876 for (x=0; x<w/sps; x++)
2877 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2879 if (++audiotrack->sub_packet_cnt >= h) {
2880 audiotrack->sub_packet_cnt = 0;
2881 audiotrack->pkt_cnt = h*w / a;
2884 while (audiotrack->pkt_cnt) {
2885 pkt = av_mallocz(sizeof(AVPacket));
2886 av_new_packet(pkt, a);
2887 memcpy(pkt->data, audiotrack->buf
2888 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2890 pkt->stream_index = stream_index;
2891 matroska_queue_packet(matroska, pkt);
2894 int offset = 0, pkt_size = lace_size[n];
2895 uint8_t *pkt_data = data;
2897 if (matroska->tracks[track]->encoding_scope & 1) {
2898 offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2899 matroska->tracks[track]);
2904 pkt = av_mallocz(sizeof(AVPacket));
2905 /* XXX: prevent data copy... */
2906 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2908 res = AVERROR(ENOMEM);
2913 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2914 memcpy (pkt->data+offset, pkt_data, pkt_size);
2916 if (pkt_data != data)
2920 pkt->flags = is_keyframe;
2921 pkt->stream_index = stream_index;
2923 pkt->pts = timecode;
2925 pkt->duration = duration;
2927 matroska_queue_packet(matroska, pkt);
2930 if (timecode != AV_NOPTS_VALUE)
2931 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2932 data += lace_size[n];
2942 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2943 uint64_t cluster_time)
2948 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2949 uint64_t duration = AV_NOPTS_VALUE;
2954 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2957 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2960 } else if (matroska->level_up) {
2961 matroska->level_up--;
2966 /* one block inside the group. Note, block parsing is one
2967 * of the harder things, so this code is a bit complicated.
2968 * See http://www.matroska.org/ for documentation. */
2969 case MATROSKA_ID_BLOCK: {
2970 pos = url_ftell(matroska->ctx->pb);
2971 res = ebml_read_binary(matroska, &id, &data, &size);
2975 case MATROSKA_ID_BLOCKDURATION: {
2976 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2981 case MATROSKA_ID_BLOCKREFERENCE: {
2983 /* We've found a reference, so not even the first frame in
2984 * the lace is a key frame. */
2986 if (last_num_packets != matroska->num_packets)
2987 matroska->packets[last_num_packets]->flags = 0;
2988 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2996 av_log(matroska->ctx, AV_LOG_INFO,
2997 "Unknown entry 0x%x in blockgroup data\n", id);
3001 res = ebml_read_skip(matroska);
3005 if (matroska->level_up) {
3006 matroska->level_up--;
3015 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3016 duration, is_keyframe, is_bframe);
3022 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3026 uint64_t cluster_time = 0;
3031 av_log(matroska->ctx, AV_LOG_DEBUG,
3032 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3035 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3038 } else if (matroska->level_up) {
3039 matroska->level_up--;
3044 /* cluster timecode */
3045 case MATROSKA_ID_CLUSTERTIMECODE: {
3047 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3053 /* a group of blocks inside a cluster */
3054 case MATROSKA_ID_BLOCKGROUP:
3055 if ((res = ebml_read_master(matroska, &id)) < 0)
3057 res = matroska_parse_blockgroup(matroska, cluster_time);
3060 case MATROSKA_ID_SIMPLEBLOCK:
3061 pos = url_ftell(matroska->ctx->pb);
3062 res = ebml_read_binary(matroska, &id, &data, &size);
3064 res = matroska_parse_block(matroska, data, size, pos,
3065 cluster_time, AV_NOPTS_VALUE,
3070 av_log(matroska->ctx, AV_LOG_INFO,
3071 "Unknown entry 0x%x in cluster data\n", id);
3075 res = ebml_read_skip(matroska);
3079 if (matroska->level_up) {
3080 matroska->level_up--;
3089 matroska_read_packet (AVFormatContext *s,
3092 MatroskaDemuxContext *matroska = s->priv_data;
3096 /* Read stream until we have a packet queued. */
3097 while (matroska_deliver_packet(matroska, pkt)) {
3099 /* Have we already reached the end? */
3101 return AVERROR(EIO);
3105 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3106 return AVERROR(EIO);
3107 } else if (matroska->level_up) {
3108 matroska->level_up--;
3113 case MATROSKA_ID_CLUSTER:
3114 if ((res = ebml_read_master(matroska, &id)) < 0)
3116 if ((res = matroska_parse_cluster(matroska)) == 0)
3117 res = 1; /* Parsed one cluster, let's get out. */
3122 res = ebml_read_skip(matroska);
3126 if (matroska->level_up) {
3127 matroska->level_up--;
3140 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3143 MatroskaDemuxContext *matroska = s->priv_data;
3144 AVStream *st = s->streams[stream_index];
3147 /* find index entry */
3148 index = av_index_search_timestamp(st, timestamp, flags);
3152 matroska_clear_queue(matroska);
3155 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3156 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3157 matroska->skip_to_stream = st;
3158 matroska->peek_id = 0;
3159 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3164 matroska_read_close (AVFormatContext *s)
3166 MatroskaDemuxContext *matroska = s->priv_data;
3169 av_free(matroska->index);
3171 matroska_clear_queue(matroska);
3173 for (n = 0; n < matroska->num_tracks; n++) {
3174 MatroskaTrack *track = matroska->tracks[n];
3175 av_free(track->codec_id);
3176 av_free(track->codec_priv);
3177 av_free(track->name);
3179 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3180 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3181 av_free(audiotrack->buf);
3190 AVInputFormat matroska_demuxer = {
3192 NULL_IF_CONFIG_SMALL("Matroska file format"),
3193 sizeof(MatroskaDemuxContext),
3195 matroska_read_header,
3196 matroska_read_packet,
3197 matroska_read_close,