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(). */
35 #include "libavcodec/mpeg4audio.h"
36 #include "libavutil/intfloat_readwrite.h"
37 #include "libavutil/lzo.h"
45 typedef struct Track {
46 MatroskaTrackType type;
48 /* Unique track number and track ID. stream_index is the index that
49 * the calling app uses for this track. */
59 unsigned char *codec_priv;
63 uint64_t default_duration;
64 MatroskaTrackFlags flags;
67 MatroskaTrackEncodingCompAlgo encoding_algo;
68 uint8_t *encoding_settings;
69 int encoding_settings_len;
72 typedef struct MatroskaVideoTrack {
85 typedef struct MatroskaAudioTrack {
90 int internal_samplerate;
94 /* real audio header */
103 } MatroskaAudioTrack;
105 typedef struct MatroskaSubtitleTrack {
108 } MatroskaSubtitleTrack;
110 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
111 sizeof(MatroskaAudioTrack), \
112 sizeof(MatroskaSubtitleTrack)))
114 typedef struct MatroskaLevel {
119 typedef struct MatroskaDemuxIndex {
120 uint64_t pos; /* of the corresponding *cluster*! */
121 uint16_t track; /* reference to 'num' */
122 uint64_t time; /* in nanoseconds */
123 } MatroskaDemuxIndex;
125 typedef struct MatroskaDemuxContext {
126 AVFormatContext *ctx;
130 MatroskaLevel levels[EBML_MAX_DEPTH];
133 /* timescale in the file */
136 /* num_streams is the number of streams that av_new_stream() was called
137 * for ( = that are available to the calling program). */
140 MatroskaTrack *tracks[MAX_STREAMS];
142 /* cache for ID peeking */
145 /* byte position of the segment inside the stream */
146 offset_t segment_start;
148 /* The packet queue. */
152 /* have we already parse metadata/cues/clusters? */
157 /* The index for seeking. */
159 MatroskaDemuxIndex *index;
161 /* What to skip before effectively reading a packet. */
162 int skip_to_keyframe;
163 AVStream *skip_to_stream;
164 } MatroskaDemuxContext;
166 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
169 * The first few functions handle EBML file parsing. The rest
170 * is the document interpretation. Matroska really just is a
175 * Return: the amount of levels in the hierarchy that the
176 * current element lies higher than the previous one.
177 * The opposite isn't done - that's auto-done using master
182 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
184 ByteIOContext *pb = matroska->ctx->pb;
185 offset_t pos = url_ftell(pb);
188 while (matroska->num_levels > 0) {
189 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
191 if (pos >= level->start + level->length) {
192 matroska->num_levels--;
203 * Read: an "EBML number", which is defined as a variable-length
204 * array of bytes. The first byte indicates the length by giving a
205 * number of 0-bits followed by a one. The position of the first
206 * "one" bit inside the first byte indicates the length of this
208 * Returns: num. of bytes read. < 0 on error.
212 ebml_read_num (MatroskaDemuxContext *matroska,
216 ByteIOContext *pb = matroska->ctx->pb;
217 int len_mask = 0x80, read = 1, n = 1;
220 /* the first byte tells us the length in bytes - get_byte() can normally
221 * return 0, but since that's not a valid first ebmlID byte, we can
222 * use it safely here to catch EOS. */
223 if (!(total = get_byte(pb))) {
224 /* we might encounter EOS here */
226 offset_t pos = url_ftell(pb);
227 av_log(matroska->ctx, AV_LOG_ERROR,
228 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
231 return AVERROR(EIO); /* EOS or actual I/O error */
234 /* get the length of the EBML number */
235 while (read <= max_size && !(total & len_mask)) {
239 if (read > max_size) {
240 offset_t pos = url_ftell(pb) - 1;
241 av_log(matroska->ctx, AV_LOG_ERROR,
242 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
243 (uint8_t) total, pos, pos);
244 return AVERROR_INVALIDDATA;
247 /* read out length */
250 total = (total << 8) | get_byte(pb);
258 * Read: the element content data ID.
259 * Return: the number of bytes read or < 0 on error.
263 ebml_read_element_id (MatroskaDemuxContext *matroska,
270 /* if we re-call this, use our cached ID */
271 if (matroska->peek_id != 0) {
274 *id = matroska->peek_id;
278 /* read out the "EBML number", include tag in ID */
279 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
281 *id = matroska->peek_id = total | (1 << (read * 7));
285 *level_up = ebml_read_element_level_up(matroska);
291 * Read: element content length.
292 * Return: the number of bytes read or < 0 on error.
296 ebml_read_element_length (MatroskaDemuxContext *matroska,
299 /* clear cache since we're now beyond that data point */
300 matroska->peek_id = 0;
302 /* read out the "EBML number", include tag in ID */
303 return ebml_read_num(matroska, 8, length);
307 * Return: the ID of the next element, or 0 on error.
308 * Level_up contains the amount of levels that this
309 * next element lies higher than the previous one.
313 ebml_peek_id (MatroskaDemuxContext *matroska,
318 if (ebml_read_element_id(matroska, &id, level_up) < 0)
325 * Seek to a given offset.
326 * 0 is success, -1 is failure.
330 ebml_read_seek (MatroskaDemuxContext *matroska,
333 ByteIOContext *pb = matroska->ctx->pb;
335 /* clear ID cache, if any */
336 matroska->peek_id = 0;
338 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
342 * Skip the next element.
343 * 0 is success, -1 is failure.
347 ebml_read_skip (MatroskaDemuxContext *matroska)
349 ByteIOContext *pb = matroska->ctx->pb;
354 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
355 (res = ebml_read_element_length(matroska, &length)) < 0)
358 url_fskip(pb, length);
364 * Read the next element as an unsigned int.
365 * 0 is success, < 0 is failure.
369 ebml_read_uint (MatroskaDemuxContext *matroska,
373 ByteIOContext *pb = matroska->ctx->pb;
374 int n = 0, size, res;
377 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
378 (res = ebml_read_element_length(matroska, &rlength)) < 0)
381 if (size < 1 || size > 8) {
382 offset_t pos = url_ftell(pb);
383 av_log(matroska->ctx, AV_LOG_ERROR,
384 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
386 return AVERROR_INVALIDDATA;
389 /* big-endian ordening; build up number */
392 *num = (*num << 8) | get_byte(pb);
398 * Read the next element as a signed int.
399 * 0 is success, < 0 is failure.
403 ebml_read_sint (MatroskaDemuxContext *matroska,
407 ByteIOContext *pb = matroska->ctx->pb;
408 int size, n = 1, negative = 0, res;
411 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
412 (res = ebml_read_element_length(matroska, &rlength)) < 0)
415 if (size < 1 || size > 8) {
416 offset_t pos = url_ftell(pb);
417 av_log(matroska->ctx, AV_LOG_ERROR,
418 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
420 return AVERROR_INVALIDDATA;
422 if ((*num = get_byte(pb)) & 0x80) {
427 *num = (*num << 8) | get_byte(pb);
431 *num = *num - (1LL << ((8 * size) - 1));
437 * Read the next element as a float.
438 * 0 is success, < 0 is failure.
442 ebml_read_float (MatroskaDemuxContext *matroska,
446 ByteIOContext *pb = matroska->ctx->pb;
450 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
451 (res = ebml_read_element_length(matroska, &rlength)) < 0)
456 *num= av_int2flt(get_be32(pb));
458 *num= av_int2dbl(get_be64(pb));
460 offset_t pos = url_ftell(pb);
461 av_log(matroska->ctx, AV_LOG_ERROR,
462 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
464 return AVERROR_INVALIDDATA;
471 * Read the next element as an ASCII string.
472 * 0 is success, < 0 is failure.
476 ebml_read_ascii (MatroskaDemuxContext *matroska,
480 ByteIOContext *pb = matroska->ctx->pb;
484 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
485 (res = ebml_read_element_length(matroska, &rlength)) < 0)
489 /* ebml strings are usually not 0-terminated, so we allocate one
490 * byte more, read the string and NULL-terminate it ourselves. */
491 if (size < 0 || !(*str = av_malloc(size + 1))) {
492 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
493 return AVERROR(ENOMEM);
495 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
496 offset_t pos = url_ftell(pb);
497 av_log(matroska->ctx, AV_LOG_ERROR,
498 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
508 * Read the next element as a UTF-8 string.
509 * 0 is success, < 0 is failure.
513 ebml_read_utf8 (MatroskaDemuxContext *matroska,
517 return ebml_read_ascii(matroska, id, str);
521 * Read the next element, but only the header. The contents
522 * are supposed to be sub-elements which can be read separately.
523 * 0 is success, < 0 is failure.
527 ebml_read_master (MatroskaDemuxContext *matroska,
530 ByteIOContext *pb = matroska->ctx->pb;
532 MatroskaLevel *level;
535 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
536 (res = ebml_read_element_length(matroska, &length)) < 0)
539 /* protect... (Heaven forbids that the '>' is true) */
540 if (matroska->num_levels >= EBML_MAX_DEPTH) {
541 av_log(matroska->ctx, AV_LOG_ERROR,
542 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
543 return AVERROR(ENOSYS);
547 level = &matroska->levels[matroska->num_levels++];
548 level->start = url_ftell(pb);
549 level->length = length;
555 * Read the next element as binary data.
556 * 0 is success, < 0 is failure.
560 ebml_read_binary (MatroskaDemuxContext *matroska,
565 ByteIOContext *pb = matroska->ctx->pb;
569 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
570 (res = ebml_read_element_length(matroska, &rlength)) < 0)
574 if (!(*binary = av_malloc(*size))) {
575 av_log(matroska->ctx, AV_LOG_ERROR,
576 "Memory allocation error\n");
577 return AVERROR(ENOMEM);
580 if (get_buffer(pb, *binary, *size) != *size) {
581 offset_t pos = url_ftell(pb);
582 av_log(matroska->ctx, AV_LOG_ERROR,
583 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
591 * Read signed/unsigned "EBML" numbers.
592 * Return: number of bytes processed, < 0 on error.
593 * XXX: use ebml_read_num().
597 matroska_ebmlnum_uint (uint8_t *data,
601 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
605 return AVERROR_INVALIDDATA;
608 while (read <= 8 && !(total & len_mask)) {
613 return AVERROR_INVALIDDATA;
615 if ((total &= (len_mask - 1)) == len_mask - 1)
618 return AVERROR_INVALIDDATA;
622 total = (total << 8) | data[n];
635 * Same as above, but signed.
639 matroska_ebmlnum_sint (uint8_t *data,
646 /* read as unsigned number first */
647 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
650 /* make signed (weird way) */
651 if (unum == (uint64_t)-1)
654 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
660 * Read an EBML header.
661 * 0 is success, < 0 is failure.
665 ebml_read_header (MatroskaDemuxContext *matroska,
670 int level_up, res = 0;
678 if (!(id = ebml_peek_id(matroska, &level_up)) ||
679 level_up != 0 || id != EBML_ID_HEADER) {
680 av_log(matroska->ctx, AV_LOG_ERROR,
681 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
682 return AVERROR_INVALIDDATA;
684 if ((res = ebml_read_master(matroska, &id)) < 0)
688 if (!(id = ebml_peek_id(matroska, &level_up)))
696 /* is our read version uptodate? */
697 case EBML_ID_EBMLREADVERSION: {
700 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
702 if (num > EBML_VERSION) {
703 av_log(matroska->ctx, AV_LOG_ERROR,
704 "EBML version %"PRIu64" (> %d) is not supported\n",
706 return AVERROR_INVALIDDATA;
711 /* we only handle 8 byte lengths at max */
712 case EBML_ID_EBMLMAXSIZELENGTH: {
715 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
717 if (num > sizeof(uint64_t)) {
718 av_log(matroska->ctx, AV_LOG_ERROR,
719 "Integers of size %"PRIu64" (> %zd) not supported\n",
720 num, sizeof(uint64_t));
721 return AVERROR_INVALIDDATA;
726 /* we handle 4 byte IDs at max */
727 case EBML_ID_EBMLMAXIDLENGTH: {
730 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
732 if (num > sizeof(uint32_t)) {
733 av_log(matroska->ctx, AV_LOG_ERROR,
734 "IDs of size %"PRIu64" (> %zu) not supported\n",
735 num, sizeof(uint32_t));
736 return AVERROR_INVALIDDATA;
741 case EBML_ID_DOCTYPE: {
744 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
755 case EBML_ID_DOCTYPEREADVERSION: {
758 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
766 av_log(matroska->ctx, AV_LOG_INFO,
767 "Unknown data type 0x%x in EBML header", id);
771 /* we ignore these two, as they don't tell us anything we
773 case EBML_ID_EBMLVERSION:
774 case EBML_ID_DOCTYPEVERSION:
775 res = ebml_read_skip (matroska);
785 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
790 for (i = 0; i < matroska->num_tracks; i++)
791 if (matroska->tracks[i]->num == num)
799 * Put one packet in an application-supplied AVPacket struct.
800 * Returns 0 on success or -1 on failure.
804 matroska_deliver_packet (MatroskaDemuxContext *matroska,
807 if (matroska->num_packets > 0) {
808 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
809 av_free(matroska->packets[0]);
810 if (matroska->num_packets > 1) {
811 memmove(&matroska->packets[0], &matroska->packets[1],
812 (matroska->num_packets - 1) * sizeof(AVPacket *));
814 av_realloc(matroska->packets, (matroska->num_packets - 1) *
817 av_freep(&matroska->packets);
819 matroska->num_packets--;
827 * Put a packet into our internal queue. Will be delivered to the
828 * user/application during the next get_packet() call.
832 matroska_queue_packet (MatroskaDemuxContext *matroska,
836 av_realloc(matroska->packets, (matroska->num_packets + 1) *
838 matroska->packets[matroska->num_packets] = pkt;
839 matroska->num_packets++;
843 * Free all packets in our internal queue.
846 matroska_clear_queue (MatroskaDemuxContext *matroska)
848 if (matroska->packets) {
850 for (n = 0; n < matroska->num_packets; n++) {
851 av_free_packet(matroska->packets[n]);
852 av_free(matroska->packets[n]);
854 av_free(matroska->packets);
855 matroska->packets = NULL;
856 matroska->num_packets = 0;
866 matroska_probe (AVProbeData *p)
869 int len_mask = 0x80, size = 1, n = 1;
870 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
873 if (AV_RB32(p->buf) != EBML_ID_HEADER)
876 /* length of header */
878 while (size <= 8 && !(total & len_mask)) {
884 total &= (len_mask - 1);
886 total = (total << 8) | p->buf[4 + n++];
888 /* does the probe data contain the whole header? */
889 if (p->buf_size < 4 + size + total)
892 /* the header must contain the document type 'matroska'. For now,
893 * we don't parse the whole header but simply check for the
894 * availability of that array of characters inside the header.
895 * Not fully fool-proof, but good enough. */
896 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
897 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
898 return AVPROBE_SCORE_MAX;
904 * From here on, it's all XML-style DTD stuff... Needs no comments.
908 matroska_parse_info (MatroskaDemuxContext *matroska)
913 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
916 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
919 } else if (matroska->level_up) {
920 matroska->level_up--;
925 /* cluster timecode */
926 case MATROSKA_ID_TIMECODESCALE: {
928 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
930 matroska->time_scale = num;
934 case MATROSKA_ID_DURATION: {
936 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
938 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
942 case MATROSKA_ID_TITLE: {
944 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
946 strncpy(matroska->ctx->title, text,
947 sizeof(matroska->ctx->title)-1);
953 av_log(matroska->ctx, AV_LOG_INFO,
954 "Unknown entry 0x%x in info header\n", id);
957 case MATROSKA_ID_WRITINGAPP:
958 case MATROSKA_ID_MUXINGAPP:
959 case MATROSKA_ID_DATEUTC:
960 case MATROSKA_ID_SEGMENTUID:
962 res = ebml_read_skip(matroska);
966 if (matroska->level_up) {
967 matroska->level_up--;
976 matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
978 uint8_t* data = *buf;
979 int isize = *buf_size;
980 uint8_t* pkt_data = NULL;
981 int pkt_size = isize;
985 switch (track->encoding_algo) {
986 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
987 return track->encoding_settings_len;
988 case MATROSKA_TRACK_ENCODING_COMP_LZO:
990 olen = pkt_size *= 3;
991 pkt_data = av_realloc(pkt_data,
992 pkt_size+LZO_OUTPUT_PADDING);
993 result = lzo1x_decode(pkt_data, &olen, data, &isize);
994 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
1000 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1001 z_stream zstream = {0};
1002 if (inflateInit(&zstream) != Z_OK)
1004 zstream.next_in = data;
1005 zstream.avail_in = isize;
1008 pkt_data = av_realloc(pkt_data, pkt_size);
1009 zstream.avail_out = pkt_size - zstream.total_out;
1010 zstream.next_out = pkt_data + zstream.total_out;
1011 result = inflate(&zstream, Z_NO_FLUSH);
1012 } while (result==Z_OK && pkt_size<10000000);
1013 pkt_size = zstream.total_out;
1014 inflateEnd(&zstream);
1015 if (result != Z_STREAM_END)
1021 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1022 bz_stream bzstream = {0};
1023 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1025 bzstream.next_in = data;
1026 bzstream.avail_in = isize;
1029 pkt_data = av_realloc(pkt_data, pkt_size);
1030 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1031 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1032 result = BZ2_bzDecompress(&bzstream);
1033 } while (result==BZ_OK && pkt_size<10000000);
1034 pkt_size = bzstream.total_out_lo32;
1035 BZ2_bzDecompressEnd(&bzstream);
1036 if (result != BZ_STREAM_END)
1044 *buf_size = pkt_size;
1052 matroska_add_stream (MatroskaDemuxContext *matroska)
1056 MatroskaTrack *track;
1058 /* start with the master */
1059 if ((res = ebml_read_master(matroska, &id)) < 0)
1062 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1064 /* Allocate a generic track. */
1065 track = av_mallocz(MAX_TRACK_SIZE);
1066 track->time_scale = 1.0;
1067 strcpy(track->language, "eng");
1069 /* try reading the trackentry headers */
1071 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1074 } else if (matroska->level_up > 0) {
1075 matroska->level_up--;
1080 /* track number (unique stream ID) */
1081 case MATROSKA_ID_TRACKNUMBER: {
1083 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1089 /* track UID (unique identifier) */
1090 case MATROSKA_ID_TRACKUID: {
1092 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1098 /* track type (video, audio, combined, subtitle, etc.) */
1099 case MATROSKA_ID_TRACKTYPE: {
1101 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1103 if (track->type && track->type != num) {
1104 av_log(matroska->ctx, AV_LOG_INFO,
1105 "More than one tracktype in an entry - skip\n");
1110 switch (track->type) {
1111 case MATROSKA_TRACK_TYPE_VIDEO:
1112 case MATROSKA_TRACK_TYPE_AUDIO:
1113 case MATROSKA_TRACK_TYPE_SUBTITLE:
1115 case MATROSKA_TRACK_TYPE_COMPLEX:
1116 case MATROSKA_TRACK_TYPE_LOGO:
1117 case MATROSKA_TRACK_TYPE_CONTROL:
1119 av_log(matroska->ctx, AV_LOG_INFO,
1120 "Unknown or unsupported track type 0x%x\n",
1122 track->type = MATROSKA_TRACK_TYPE_NONE;
1128 /* tracktype specific stuff for video */
1129 case MATROSKA_ID_TRACKVIDEO: {
1130 MatroskaVideoTrack *videotrack;
1132 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1133 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1134 av_log(matroska->ctx, AV_LOG_INFO,
1135 "video data in non-video track - ignoring\n");
1136 res = AVERROR_INVALIDDATA;
1138 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1140 videotrack = (MatroskaVideoTrack *)track;
1143 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1146 } else if (matroska->level_up > 0) {
1147 matroska->level_up--;
1152 /* fixme, this should be one-up, but I get it here */
1153 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1155 if ((res = ebml_read_uint (matroska, &id,
1158 track->default_duration = num;
1162 /* video framerate */
1163 case MATROSKA_ID_VIDEOFRAMERATE: {
1165 if ((res = ebml_read_float(matroska, &id,
1168 if (!track->default_duration)
1169 track->default_duration = 1000000000/num;
1173 /* width of the size to display the video at */
1174 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1176 if ((res = ebml_read_uint(matroska, &id,
1179 videotrack->display_width = num;
1183 /* height of the size to display the video at */
1184 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1186 if ((res = ebml_read_uint(matroska, &id,
1189 videotrack->display_height = num;
1193 /* width of the video in the file */
1194 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1196 if ((res = ebml_read_uint(matroska, &id,
1199 videotrack->pixel_width = num;
1203 /* height of the video in the file */
1204 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1206 if ((res = ebml_read_uint(matroska, &id,
1209 videotrack->pixel_height = num;
1213 /* whether the video is interlaced */
1214 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1216 if ((res = ebml_read_uint(matroska, &id,
1221 MATROSKA_VIDEOTRACK_INTERLACED;
1224 ~MATROSKA_VIDEOTRACK_INTERLACED;
1228 /* colorspace (only matters for raw video)
1230 case MATROSKA_ID_VIDEOCOLORSPACE: {
1232 if ((res = ebml_read_uint(matroska, &id,
1235 videotrack->fourcc = num;
1240 av_log(matroska->ctx, AV_LOG_INFO,
1241 "Unknown video track header entry "
1242 "0x%x - ignoring\n", id);
1245 case MATROSKA_ID_VIDEOSTEREOMODE:
1246 case MATROSKA_ID_VIDEOASPECTRATIO:
1248 res = ebml_read_skip(matroska);
1252 if (matroska->level_up) {
1253 matroska->level_up--;
1260 /* tracktype specific stuff for audio */
1261 case MATROSKA_ID_TRACKAUDIO: {
1262 MatroskaAudioTrack *audiotrack;
1264 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1265 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1266 av_log(matroska->ctx, AV_LOG_INFO,
1267 "audio data in non-audio track - ignoring\n");
1268 res = AVERROR_INVALIDDATA;
1270 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1272 audiotrack = (MatroskaAudioTrack *)track;
1273 audiotrack->channels = 1;
1274 audiotrack->samplerate = 8000;
1277 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1280 } else if (matroska->level_up > 0) {
1281 matroska->level_up--;
1287 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1289 if ((res = ebml_read_float(matroska, &id,
1292 audiotrack->internal_samplerate =
1293 audiotrack->samplerate = num;
1297 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1299 if ((res = ebml_read_float(matroska, &id,
1302 audiotrack->samplerate = num;
1307 case MATROSKA_ID_AUDIOBITDEPTH: {
1309 if ((res = ebml_read_uint(matroska, &id,
1312 audiotrack->bitdepth = num;
1317 case MATROSKA_ID_AUDIOCHANNELS: {
1319 if ((res = ebml_read_uint(matroska, &id,
1322 audiotrack->channels = num;
1327 av_log(matroska->ctx, AV_LOG_INFO,
1328 "Unknown audio track header entry "
1329 "0x%x - ignoring\n", id);
1333 res = ebml_read_skip(matroska);
1337 if (matroska->level_up) {
1338 matroska->level_up--;
1345 /* codec identifier */
1346 case MATROSKA_ID_CODECID: {
1348 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1350 track->codec_id = text;
1354 /* codec private data */
1355 case MATROSKA_ID_CODECPRIVATE: {
1358 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1360 track->codec_priv = data;
1361 track->codec_priv_size = size;
1365 /* name of this track */
1366 case MATROSKA_ID_TRACKNAME: {
1368 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1374 /* language (matters for audio/subtitles, mostly) */
1375 case MATROSKA_ID_TRACKLANGUAGE: {
1377 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1379 if ((end = strchr(text, '-')))
1381 if (strlen(text) == 3)
1382 strcpy(track->language, text);
1387 /* whether this is actually used */
1388 case MATROSKA_ID_TRACKFLAGENABLED: {
1390 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1393 track->flags |= MATROSKA_TRACK_ENABLED;
1395 track->flags &= ~MATROSKA_TRACK_ENABLED;
1399 /* whether it's the default for this track type */
1400 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1402 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1405 track->flags |= MATROSKA_TRACK_DEFAULT;
1407 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1411 /* lacing (like MPEG, where blocks don't end/start on frame
1413 case MATROSKA_ID_TRACKFLAGLACING: {
1415 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1418 track->flags |= MATROSKA_TRACK_LACING;
1420 track->flags &= ~MATROSKA_TRACK_LACING;
1424 /* default length (in time) of one data block in this track */
1425 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1427 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1429 track->default_duration = num;
1433 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1434 if ((res = ebml_read_master(matroska, &id)) < 0)
1438 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1441 } else if (matroska->level_up > 0) {
1442 matroska->level_up--;
1447 case MATROSKA_ID_TRACKCONTENTENCODING: {
1448 int encoding_scope = 1;
1449 if ((res = ebml_read_master(matroska, &id)) < 0)
1453 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1456 } else if (matroska->level_up > 0) {
1457 matroska->level_up--;
1462 case MATROSKA_ID_ENCODINGSCOPE: {
1464 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1466 encoding_scope = num;
1470 case MATROSKA_ID_ENCODINGTYPE: {
1472 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1475 av_log(matroska->ctx, AV_LOG_ERROR,
1476 "Unsupported encoding type");
1480 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1481 if ((res = ebml_read_master(matroska, &id)) < 0)
1485 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1488 } else if (matroska->level_up > 0) {
1489 matroska->level_up--;
1494 case MATROSKA_ID_ENCODINGCOMPALGO: {
1496 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1498 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1500 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1503 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1505 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1506 av_log(matroska->ctx, AV_LOG_ERROR,
1507 "Unsupported compression algo\n");
1508 track->encoding_algo = num;
1512 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1515 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1517 track->encoding_settings = data;
1518 track->encoding_settings_len = size;
1523 av_log(matroska->ctx, AV_LOG_INFO,
1524 "Unknown compression header entry "
1525 "0x%x - ignoring\n", id);
1529 res = ebml_read_skip(matroska);
1533 if (matroska->level_up) {
1534 matroska->level_up--;
1542 av_log(matroska->ctx, AV_LOG_INFO,
1543 "Unknown content encoding header entry "
1544 "0x%x - ignoring\n", id);
1548 res = ebml_read_skip(matroska);
1552 if (matroska->level_up) {
1553 matroska->level_up--;
1558 track->encoding_scope = encoding_scope;
1563 av_log(matroska->ctx, AV_LOG_INFO,
1564 "Unknown content encodings header entry "
1565 "0x%x - ignoring\n", id);
1569 res = ebml_read_skip(matroska);
1573 if (matroska->level_up) {
1574 matroska->level_up--;
1581 case MATROSKA_ID_TRACKTIMECODESCALE: {
1583 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1585 track->time_scale = num;
1590 av_log(matroska->ctx, AV_LOG_INFO,
1591 "Unknown track header entry 0x%x - ignoring\n", id);
1595 /* we ignore these because they're nothing useful. */
1596 case MATROSKA_ID_TRACKFLAGFORCED:
1597 case MATROSKA_ID_CODECNAME:
1598 case MATROSKA_ID_CODECDECODEALL:
1599 case MATROSKA_ID_CODECINFOURL:
1600 case MATROSKA_ID_CODECDOWNLOADURL:
1601 case MATROSKA_ID_TRACKMINCACHE:
1602 case MATROSKA_ID_TRACKMAXCACHE:
1603 res = ebml_read_skip(matroska);
1607 if (matroska->level_up) {
1608 matroska->level_up--;
1613 if (track->codec_priv_size && track->encoding_scope & 2) {
1614 uint8_t *orig_priv = track->codec_priv;
1615 int offset = matroska_decode_buffer(&track->codec_priv,
1616 &track->codec_priv_size, track);
1618 track->codec_priv = av_malloc(track->codec_priv_size + offset);
1619 memcpy(track->codec_priv, track->encoding_settings, offset);
1620 memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1621 track->codec_priv_size += offset;
1623 } else if (!offset) {
1626 av_log(matroska->ctx, AV_LOG_ERROR,
1627 "Failed to decode codec private data\n");
1630 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1631 matroska->tracks[matroska->num_tracks++] = track;
1639 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1644 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1647 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1650 } else if (matroska->level_up) {
1651 matroska->level_up--;
1656 /* one track within the "all-tracks" header */
1657 case MATROSKA_ID_TRACKENTRY:
1658 res = matroska_add_stream(matroska);
1662 av_log(matroska->ctx, AV_LOG_INFO,
1663 "Unknown entry 0x%x in track header\n", id);
1667 res = ebml_read_skip(matroska);
1671 if (matroska->level_up) {
1672 matroska->level_up--;
1681 matroska_parse_index (MatroskaDemuxContext *matroska)
1685 MatroskaDemuxIndex idx;
1687 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1690 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1693 } else if (matroska->level_up) {
1694 matroska->level_up--;
1699 /* one single index entry ('point') */
1700 case MATROSKA_ID_POINTENTRY:
1701 if ((res = ebml_read_master(matroska, &id)) < 0)
1704 /* in the end, we hope to fill one entry with a
1705 * timestamp, a file position and a tracknum */
1706 idx.pos = (uint64_t) -1;
1707 idx.time = (uint64_t) -1;
1708 idx.track = (uint16_t) -1;
1711 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1714 } else if (matroska->level_up) {
1715 matroska->level_up--;
1720 /* one single index entry ('point') */
1721 case MATROSKA_ID_CUETIME: {
1723 if ((res = ebml_read_uint(matroska, &id,
1726 idx.time = time * matroska->time_scale;
1730 /* position in the file + track to which it
1732 case MATROSKA_ID_CUETRACKPOSITION:
1733 if ((res = ebml_read_master(matroska, &id)) < 0)
1737 if (!(id = ebml_peek_id (matroska,
1738 &matroska->level_up))) {
1741 } else if (matroska->level_up) {
1742 matroska->level_up--;
1748 case MATROSKA_ID_CUETRACK: {
1750 if ((res = ebml_read_uint(matroska,
1757 /* position in file */
1758 case MATROSKA_ID_CUECLUSTERPOSITION: {
1760 if ((res = ebml_read_uint(matroska,
1763 idx.pos = num+matroska->segment_start;
1768 av_log(matroska->ctx, AV_LOG_INFO,
1769 "Unknown entry 0x%x in "
1770 "CuesTrackPositions\n", id);
1774 res = ebml_read_skip(matroska);
1778 if (matroska->level_up) {
1779 matroska->level_up--;
1787 av_log(matroska->ctx, AV_LOG_INFO,
1788 "Unknown entry 0x%x in cuespoint "
1793 res = ebml_read_skip(matroska);
1797 if (matroska->level_up) {
1798 matroska->level_up--;
1803 /* so let's see if we got what we wanted */
1804 if (idx.pos != (uint64_t) -1 &&
1805 idx.time != (uint64_t) -1 &&
1806 idx.track != (uint16_t) -1) {
1807 if (matroska->num_indexes % 32 == 0) {
1808 /* re-allocate bigger index */
1810 av_realloc(matroska->index,
1811 (matroska->num_indexes + 32) *
1812 sizeof(MatroskaDemuxIndex));
1814 matroska->index[matroska->num_indexes] = idx;
1815 matroska->num_indexes++;
1820 av_log(matroska->ctx, AV_LOG_INFO,
1821 "Unknown entry 0x%x in cues header\n", id);
1825 res = ebml_read_skip(matroska);
1829 if (matroska->level_up) {
1830 matroska->level_up--;
1839 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1845 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1848 } else if (matroska->level_up) {
1849 matroska->level_up--;
1854 /* Hm, this is unsupported... */
1856 av_log(matroska->ctx, AV_LOG_INFO,
1857 "Unknown entry 0x%x in metadata header\n", id);
1861 res = ebml_read_skip(matroska);
1865 if (matroska->level_up) {
1866 matroska->level_up--;
1875 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1880 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1883 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1886 } else if (matroska->level_up) {
1887 matroska->level_up--;
1892 case MATROSKA_ID_SEEKENTRY: {
1893 uint32_t seek_id = 0, peek_id_cache = 0;
1894 uint64_t seek_pos = (uint64_t) -1, t;
1895 int dummy_level = 0;
1897 if ((res = ebml_read_master(matroska, &id)) < 0)
1901 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1904 } else if (matroska->level_up) {
1905 matroska->level_up--;
1910 case MATROSKA_ID_SEEKID:
1911 res = ebml_read_uint(matroska, &id, &t);
1915 case MATROSKA_ID_SEEKPOSITION:
1916 res = ebml_read_uint(matroska, &id, &seek_pos);
1920 av_log(matroska->ctx, AV_LOG_INFO,
1921 "Unknown seekhead ID 0x%x\n", id);
1925 res = ebml_read_skip(matroska);
1929 if (matroska->level_up) {
1930 matroska->level_up--;
1935 if (!seek_id || seek_pos == (uint64_t) -1) {
1936 av_log(matroska->ctx, AV_LOG_INFO,
1937 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1943 case MATROSKA_ID_CUES:
1944 case MATROSKA_ID_TAGS: {
1945 uint32_t level_up = matroska->level_up;
1946 offset_t before_pos;
1948 MatroskaLevel level;
1950 /* remember the peeked ID and the current position */
1951 peek_id_cache = matroska->peek_id;
1952 before_pos = url_ftell(matroska->ctx->pb);
1955 if ((res = ebml_read_seek(matroska, seek_pos +
1956 matroska->segment_start)) < 0)
1959 /* we don't want to lose our seekhead level, so we add
1960 * a dummy. This is a crude hack. */
1961 if (matroska->num_levels == EBML_MAX_DEPTH) {
1962 av_log(matroska->ctx, AV_LOG_INFO,
1963 "Max EBML element depth (%d) reached, "
1964 "cannot parse further.\n", EBML_MAX_DEPTH);
1965 return AVERROR_UNKNOWN;
1969 level.length = (uint64_t)-1;
1970 matroska->levels[matroska->num_levels] = level;
1971 matroska->num_levels++;
1975 if (!(id = ebml_peek_id (matroska,
1976 &matroska->level_up)))
1978 if (id != seek_id) {
1979 av_log(matroska->ctx, AV_LOG_INFO,
1980 "We looked for ID=0x%x but got "
1981 "ID=0x%x (pos=%"PRIu64")",
1982 seek_id, id, seek_pos +
1983 matroska->segment_start);
1987 /* read master + parse */
1988 if ((res = ebml_read_master(matroska, &id)) < 0)
1991 case MATROSKA_ID_CUES:
1992 if (!(res = matroska_parse_index(matroska)) ||
1993 url_feof(matroska->ctx->pb)) {
1994 matroska->index_parsed = 1;
1998 case MATROSKA_ID_TAGS:
1999 if (!(res = matroska_parse_metadata(matroska)) ||
2000 url_feof(matroska->ctx->pb)) {
2001 matroska->metadata_parsed = 1;
2008 /* remove dummy level */
2010 while (matroska->num_levels) {
2011 matroska->num_levels--;
2013 matroska->levels[matroska->num_levels].length;
2014 if (length == (uint64_t)-1)
2019 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2021 matroska->peek_id = peek_id_cache;
2022 matroska->level_up = level_up;
2027 av_log(matroska->ctx, AV_LOG_INFO,
2028 "Ignoring seekhead entry for ID=0x%x\n",
2037 av_log(matroska->ctx, AV_LOG_INFO,
2038 "Unknown seekhead ID 0x%x\n", id);
2042 res = ebml_read_skip(matroska);
2046 if (matroska->level_up) {
2047 matroska->level_up--;
2056 matroska_parse_attachments(AVFormatContext *s)
2058 MatroskaDemuxContext *matroska = s->priv_data;
2062 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2065 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2068 } else if (matroska->level_up) {
2069 matroska->level_up--;
2074 case MATROSKA_ID_ATTACHEDFILE: {
2077 uint8_t* data = NULL;
2078 int i, data_size = 0;
2081 if ((res = ebml_read_master(matroska, &id)) < 0)
2085 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2088 } else if (matroska->level_up) {
2089 matroska->level_up--;
2094 case MATROSKA_ID_FILENAME:
2095 res = ebml_read_utf8 (matroska, &id, &name);
2098 case MATROSKA_ID_FILEMIMETYPE:
2099 res = ebml_read_ascii (matroska, &id, &mime);
2102 case MATROSKA_ID_FILEDATA:
2103 res = ebml_read_binary(matroska, &id, &data, &data_size);
2107 av_log(matroska->ctx, AV_LOG_INFO,
2108 "Unknown attachedfile ID 0x%x\n", id);
2109 case MATROSKA_ID_FILEUID:
2111 res = ebml_read_skip(matroska);
2115 if (matroska->level_up) {
2116 matroska->level_up--;
2121 if (!(name && mime && data && data_size > 0)) {
2122 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2126 st = av_new_stream(s, matroska->num_streams++);
2128 return AVERROR(ENOMEM);
2129 st->filename = av_strdup(name);
2130 st->codec->codec_id = CODEC_ID_NONE;
2131 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2132 st->codec->extradata = av_malloc(data_size);
2133 if(st->codec->extradata == NULL)
2134 return AVERROR(ENOMEM);
2135 st->codec->extradata_size = data_size;
2136 memcpy(st->codec->extradata, data, data_size);
2138 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2139 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2140 strlen(ff_mkv_mime_tags[i].str))) {
2141 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2146 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2151 av_log(matroska->ctx, AV_LOG_INFO,
2152 "Unknown attachments ID 0x%x\n", id);
2156 res = ebml_read_skip(matroska);
2160 if (matroska->level_up) {
2161 matroska->level_up--;
2170 matroska_parse_chapters(AVFormatContext *s)
2172 MatroskaDemuxContext *matroska = s->priv_data;
2176 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2179 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2182 } else if (matroska->level_up) {
2183 matroska->level_up--;
2188 case MATROSKA_ID_EDITIONENTRY: {
2189 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2192 /* if there is more than one chapter edition
2193 we take only the first one */
2195 ebml_read_skip(matroska);
2199 if ((res = ebml_read_master(matroska, &id)) < 0)
2203 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2206 } else if (matroska->level_up) {
2207 matroska->level_up--;
2212 case MATROSKA_ID_CHAPTERATOM:
2213 if ((res = ebml_read_master(matroska, &id)) < 0)
2217 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2220 } else if (matroska->level_up) {
2221 matroska->level_up--;
2226 case MATROSKA_ID_CHAPTERTIMEEND:
2227 res = ebml_read_uint(matroska, &id, &end);
2230 case MATROSKA_ID_CHAPTERTIMESTART:
2231 res = ebml_read_uint(matroska, &id, &start);
2234 case MATROSKA_ID_CHAPTERDISPLAY:
2235 if ((res = ebml_read_master(matroska, &id)) < 0)
2239 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2242 } else if (matroska->level_up) {
2243 matroska->level_up--;
2248 case MATROSKA_ID_CHAPSTRING:
2249 res = ebml_read_utf8(matroska, &id, &title);
2253 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2255 res = ebml_read_skip(matroska);
2259 if (matroska->level_up) {
2260 matroska->level_up--;
2266 case MATROSKA_ID_CHAPTERUID:
2267 res = ebml_read_uint(matroska, &id, &uid);
2270 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2271 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2273 res = ebml_read_skip(matroska);
2277 if (matroska->level_up) {
2278 matroska->level_up--;
2283 if (start != AV_NOPTS_VALUE && uid != -1) {
2284 if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2285 res= AVERROR(ENOMEM);
2291 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2292 case MATROSKA_ID_EDITIONUID:
2293 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2294 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2296 res = ebml_read_skip(matroska);
2301 if (matroska->level_up) {
2302 matroska->level_up--;
2310 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2312 res = ebml_read_skip(matroska);
2316 if (matroska->level_up) {
2317 matroska->level_up--;
2326 matroska_aac_profile (char *codec_id)
2328 static const char *aac_profiles[] = {
2333 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2334 if (strstr(codec_id, aac_profiles[profile]))
2340 matroska_aac_sri (int samplerate)
2344 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2345 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2351 matroska_read_header (AVFormatContext *s,
2352 AVFormatParameters *ap)
2354 MatroskaDemuxContext *matroska = s->priv_data;
2356 int version, last_level, res = 0;
2361 /* First read the EBML header. */
2363 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2365 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2366 av_log(matroska->ctx, AV_LOG_ERROR,
2367 "Wrong EBML doctype ('%s' != 'matroska').\n",
2368 doctype ? doctype : "(none)");
2371 return AVERROR_NOFMT;
2375 av_log(matroska->ctx, AV_LOG_ERROR,
2376 "Matroska demuxer version 2 too old for file version %d\n",
2378 return AVERROR_NOFMT;
2381 /* The next thing is a segment. */
2383 if (!(id = ebml_peek_id(matroska, &last_level)))
2384 return AVERROR(EIO);
2385 if (id == MATROSKA_ID_SEGMENT)
2389 av_log(matroska->ctx, AV_LOG_INFO,
2390 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2391 MATROSKA_ID_SEGMENT, id);
2392 if ((res = ebml_read_skip(matroska)) < 0)
2396 /* We now have a Matroska segment.
2397 * Seeks are from the beginning of the segment,
2398 * after the segment ID/length. */
2399 if ((res = ebml_read_master(matroska, &id)) < 0)
2401 matroska->segment_start = url_ftell(s->pb);
2403 matroska->time_scale = 1000000;
2404 /* we've found our segment, start reading the different contents in here */
2406 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2409 } else if (matroska->level_up) {
2410 matroska->level_up--;
2416 case MATROSKA_ID_INFO: {
2417 if ((res = ebml_read_master(matroska, &id)) < 0)
2419 res = matroska_parse_info(matroska);
2423 /* track info headers */
2424 case MATROSKA_ID_TRACKS: {
2425 if ((res = ebml_read_master(matroska, &id)) < 0)
2427 res = matroska_parse_tracks(matroska);
2432 case MATROSKA_ID_CUES: {
2433 if (!matroska->index_parsed) {
2434 if ((res = ebml_read_master(matroska, &id)) < 0)
2436 res = matroska_parse_index(matroska);
2438 res = ebml_read_skip(matroska);
2443 case MATROSKA_ID_TAGS: {
2444 if (!matroska->metadata_parsed) {
2445 if ((res = ebml_read_master(matroska, &id)) < 0)
2447 res = matroska_parse_metadata(matroska);
2449 res = ebml_read_skip(matroska);
2453 /* file index (if seekable, seek to Cues/Tags to parse it) */
2454 case MATROSKA_ID_SEEKHEAD: {
2455 if ((res = ebml_read_master(matroska, &id)) < 0)
2457 res = matroska_parse_seekhead(matroska);
2461 case MATROSKA_ID_ATTACHMENTS: {
2462 if ((res = ebml_read_master(matroska, &id)) < 0)
2464 res = matroska_parse_attachments(s);
2468 case MATROSKA_ID_CLUSTER: {
2469 /* Do not read the master - this will be done in the next
2470 * call to matroska_read_packet. */
2475 case MATROSKA_ID_CHAPTERS: {
2476 if ((res = ebml_read_master(matroska, &id)) < 0)
2478 res = matroska_parse_chapters(s);
2483 av_log(matroska->ctx, AV_LOG_INFO,
2484 "Unknown matroska file header ID 0x%x\n", id);
2488 res = ebml_read_skip(matroska);
2492 if (matroska->level_up) {
2493 matroska->level_up--;
2498 /* Have we found a cluster? */
2499 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2501 MatroskaTrack *track;
2504 for (i = 0; i < matroska->num_tracks; i++) {
2505 enum CodecID codec_id = CODEC_ID_NONE;
2506 uint8_t *extradata = NULL;
2507 int extradata_size = 0;
2508 int extradata_offset = 0;
2509 track = matroska->tracks[i];
2510 track->stream_index = -1;
2512 /* Apply some sanity checks. */
2513 if (track->codec_id == NULL)
2516 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2517 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2518 strlen(ff_mkv_codec_tags[j].str))){
2519 codec_id= ff_mkv_codec_tags[j].id;
2524 /* Set the FourCC from the CodecID. */
2525 /* This is the MS compatibility mode which stores a
2526 * BITMAPINFOHEADER in the CodecPrivate. */
2527 if (!strcmp(track->codec_id,
2528 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2529 (track->codec_priv_size >= 40) &&
2530 (track->codec_priv != NULL)) {
2531 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2533 /* Offset of biCompression. Stored in LE. */
2534 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2535 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2539 /* This is the MS compatibility mode which stores a
2540 * WAVEFORMATEX in the CodecPrivate. */
2541 else if (!strcmp(track->codec_id,
2542 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2543 (track->codec_priv_size >= 18) &&
2544 (track->codec_priv != NULL)) {
2547 /* Offset of wFormatTag. Stored in LE. */
2548 tag = AV_RL16(track->codec_priv);
2549 codec_id = codec_get_id(codec_wav_tags, tag);
2553 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2554 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2555 int profile = matroska_aac_profile(track->codec_id);
2556 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2557 extradata = av_malloc(5);
2558 if (extradata == NULL)
2559 return AVERROR(ENOMEM);
2560 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2561 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2562 if (strstr(track->codec_id, "SBR")) {
2563 sri = matroska_aac_sri(audiotrack->samplerate);
2564 extradata[2] = 0x56;
2565 extradata[3] = 0xE5;
2566 extradata[4] = 0x80 | (sri<<3);
2573 else if (codec_id == CODEC_ID_TTA) {
2574 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2576 extradata_size = 30;
2577 extradata = av_mallocz(extradata_size);
2578 if (extradata == NULL)
2579 return AVERROR(ENOMEM);
2580 init_put_byte(&b, extradata, extradata_size, 1,
2581 NULL, NULL, NULL, NULL);
2582 put_buffer(&b, "TTA1", 4);
2584 put_le16(&b, audiotrack->channels);
2585 put_le16(&b, audiotrack->bitdepth);
2586 put_le32(&b, audiotrack->samplerate);
2587 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2590 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2591 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2592 extradata_offset = 26;
2593 track->codec_priv_size -= extradata_offset;
2596 else if (codec_id == CODEC_ID_RA_144) {
2597 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2598 audiotrack->samplerate = 8000;
2599 audiotrack->channels = 1;
2602 else if (codec_id == CODEC_ID_RA_288 ||
2603 codec_id == CODEC_ID_COOK ||
2604 codec_id == CODEC_ID_ATRAC3) {
2605 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2608 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2609 NULL, NULL, NULL, NULL);
2611 audiotrack->coded_framesize = get_be32(&b);
2613 audiotrack->sub_packet_h = get_be16(&b);
2614 audiotrack->frame_size = get_be16(&b);
2615 audiotrack->sub_packet_size = get_be16(&b);
2616 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2617 if (codec_id == CODEC_ID_RA_288) {
2618 audiotrack->block_align = audiotrack->coded_framesize;
2619 track->codec_priv_size = 0;
2621 audiotrack->block_align = audiotrack->sub_packet_size;
2622 extradata_offset = 78;
2623 track->codec_priv_size -= extradata_offset;
2627 if (codec_id == CODEC_ID_NONE) {
2628 av_log(matroska->ctx, AV_LOG_INFO,
2629 "Unknown/unsupported CodecID %s.\n",
2633 track->stream_index = matroska->num_streams;
2635 matroska->num_streams++;
2636 st = av_new_stream(s, track->stream_index);
2638 return AVERROR(ENOMEM);
2639 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2641 st->codec->codec_id = codec_id;
2643 if (strcmp(track->language, "und"))
2644 strcpy(st->language, track->language);
2646 if (track->flags & MATROSKA_TRACK_DEFAULT)
2647 st->disposition |= AV_DISPOSITION_DEFAULT;
2649 if (track->default_duration)
2650 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2651 track->default_duration, 1000000000, 30000);
2654 st->codec->extradata = extradata;
2655 st->codec->extradata_size = extradata_size;
2656 } else if(track->codec_priv && track->codec_priv_size > 0){
2657 st->codec->extradata = av_malloc(track->codec_priv_size);
2658 if(st->codec->extradata == NULL)
2659 return AVERROR(ENOMEM);
2660 st->codec->extradata_size = track->codec_priv_size;
2661 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2662 track->codec_priv_size);
2665 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2666 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2668 st->codec->codec_type = CODEC_TYPE_VIDEO;
2669 st->codec->codec_tag = videotrack->fourcc;
2670 st->codec->width = videotrack->pixel_width;
2671 st->codec->height = videotrack->pixel_height;
2672 if (videotrack->display_width == 0)
2673 videotrack->display_width= videotrack->pixel_width;
2674 if (videotrack->display_height == 0)
2675 videotrack->display_height= videotrack->pixel_height;
2676 av_reduce(&st->codec->sample_aspect_ratio.num,
2677 &st->codec->sample_aspect_ratio.den,
2678 st->codec->height * videotrack->display_width,
2679 st->codec-> width * videotrack->display_height,
2681 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2682 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2683 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2685 st->codec->codec_type = CODEC_TYPE_AUDIO;
2686 st->codec->sample_rate = audiotrack->samplerate;
2687 st->codec->channels = audiotrack->channels;
2688 st->codec->block_align = audiotrack->block_align;
2689 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2690 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2693 /* What do we do with private data? E.g. for Vorbis. */
2698 if (matroska->index_parsed) {
2699 int i, track, stream;
2700 for (i=0; i<matroska->num_indexes; i++) {
2701 MatroskaDemuxIndex *idx = &matroska->index[i];
2702 track = matroska_find_track_by_num(matroska, idx->track);
2703 if (track < 0) continue;
2704 stream = matroska->tracks[track]->stream_index;
2705 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2706 av_add_index_entry(matroska->ctx->streams[stream],
2707 idx->pos, idx->time/AV_TIME_BASE,
2708 0, 0, AVINDEX_KEYFRAME);
2716 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2717 int64_t pos, uint64_t cluster_time, uint64_t duration,
2718 int is_keyframe, int is_bframe)
2724 uint8_t *origdata = data;
2726 uint32_t *lace_size = NULL;
2727 int n, flags, laces = 0;
2731 /* first byte(s): tracknum */
2732 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2733 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2740 /* fetch track from num */
2741 track = matroska_find_track_by_num(matroska, num);
2742 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2743 av_log(matroska->ctx, AV_LOG_INFO,
2744 "Invalid stream %d or size %u\n", track, size);
2748 stream_index = matroska->tracks[track]->stream_index;
2749 if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2753 st = matroska->ctx->streams[stream_index];
2754 if (st->discard >= AVDISCARD_ALL) {
2758 if (duration == AV_NOPTS_VALUE)
2759 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2761 /* block_time (relative to cluster time) */
2762 block_time = AV_RB16(data);
2766 if (is_keyframe == -1)
2767 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2769 if (matroska->skip_to_keyframe) {
2770 if (!is_keyframe || st != matroska->skip_to_stream) {
2774 matroska->skip_to_keyframe = 0;
2777 switch ((flags & 0x06) >> 1) {
2778 case 0x0: /* no lacing */
2780 lace_size = av_mallocz(sizeof(int));
2781 lace_size[0] = size;
2784 case 0x1: /* xiph lacing */
2785 case 0x2: /* fixed-size lacing */
2786 case 0x3: /* EBML lacing */
2787 assert(size>0); // size <=3 is checked before size-=3 above
2788 laces = (*data) + 1;
2791 lace_size = av_mallocz(laces * sizeof(int));
2793 switch ((flags & 0x06) >> 1) {
2794 case 0x1: /* xiph lacing */ {
2797 for (n = 0; res == 0 && n < laces - 1; n++) {
2804 lace_size[n] += temp;
2810 total += lace_size[n];
2812 lace_size[n] = size - total;
2816 case 0x2: /* fixed-size lacing */
2817 for (n = 0; n < laces; n++)
2818 lace_size[n] = size / laces;
2821 case 0x3: /* EBML lacing */ {
2823 n = matroska_ebmlnum_uint(data, size, &num);
2825 av_log(matroska->ctx, AV_LOG_INFO,
2826 "EBML block data error\n");
2831 total = lace_size[0] = num;
2832 for (n = 1; res == 0 && n < laces - 1; n++) {
2835 r = matroska_ebmlnum_sint (data, size, &snum);
2837 av_log(matroska->ctx, AV_LOG_INFO,
2838 "EBML block data error\n");
2843 lace_size[n] = lace_size[n - 1] + snum;
2844 total += lace_size[n];
2846 lace_size[n] = size - total;
2854 uint64_t timecode = AV_NOPTS_VALUE;
2856 if (cluster_time != (uint64_t)-1
2857 && (block_time >= 0 || cluster_time >= -block_time))
2858 timecode = cluster_time + block_time;
2860 for (n = 0; n < laces; n++) {
2861 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2862 st->codec->codec_id == CODEC_ID_COOK ||
2863 st->codec->codec_id == CODEC_ID_ATRAC3) {
2864 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2865 int a = st->codec->block_align;
2866 int sps = audiotrack->sub_packet_size;
2867 int cfs = audiotrack->coded_framesize;
2868 int h = audiotrack->sub_packet_h;
2869 int y = audiotrack->sub_packet_cnt;
2870 int w = audiotrack->frame_size;
2873 if (!audiotrack->pkt_cnt) {
2874 if (st->codec->codec_id == CODEC_ID_RA_288)
2875 for (x=0; x<h/2; x++)
2876 memcpy(audiotrack->buf+x*2*w+y*cfs,
2879 for (x=0; x<w/sps; x++)
2880 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2882 if (++audiotrack->sub_packet_cnt >= h) {
2883 audiotrack->sub_packet_cnt = 0;
2884 audiotrack->pkt_cnt = h*w / a;
2887 while (audiotrack->pkt_cnt) {
2888 pkt = av_mallocz(sizeof(AVPacket));
2889 av_new_packet(pkt, a);
2890 memcpy(pkt->data, audiotrack->buf
2891 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2893 pkt->stream_index = stream_index;
2894 matroska_queue_packet(matroska, pkt);
2897 int offset = 0, pkt_size = lace_size[n];
2898 uint8_t *pkt_data = data;
2900 if (matroska->tracks[track]->encoding_scope & 1) {
2901 offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2902 matroska->tracks[track]);
2907 pkt = av_mallocz(sizeof(AVPacket));
2908 /* XXX: prevent data copy... */
2909 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2911 res = AVERROR(ENOMEM);
2916 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2917 memcpy (pkt->data+offset, pkt_data, pkt_size);
2919 if (pkt_data != data)
2923 pkt->flags = is_keyframe;
2924 pkt->stream_index = stream_index;
2926 pkt->pts = timecode;
2928 pkt->duration = duration;
2930 matroska_queue_packet(matroska, pkt);
2933 if (timecode != AV_NOPTS_VALUE)
2934 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2935 data += lace_size[n];
2945 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2946 uint64_t cluster_time)
2951 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2952 uint64_t duration = AV_NOPTS_VALUE;
2957 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2960 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2963 } else if (matroska->level_up) {
2964 matroska->level_up--;
2969 /* one block inside the group. Note, block parsing is one
2970 * of the harder things, so this code is a bit complicated.
2971 * See http://www.matroska.org/ for documentation. */
2972 case MATROSKA_ID_BLOCK: {
2973 pos = url_ftell(matroska->ctx->pb);
2974 res = ebml_read_binary(matroska, &id, &data, &size);
2978 case MATROSKA_ID_BLOCKDURATION: {
2979 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2984 case MATROSKA_ID_BLOCKREFERENCE: {
2986 /* We've found a reference, so not even the first frame in
2987 * the lace is a key frame. */
2989 if (last_num_packets != matroska->num_packets)
2990 matroska->packets[last_num_packets]->flags = 0;
2991 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2999 av_log(matroska->ctx, AV_LOG_INFO,
3000 "Unknown entry 0x%x in blockgroup data\n", id);
3004 res = ebml_read_skip(matroska);
3008 if (matroska->level_up) {
3009 matroska->level_up--;
3018 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3019 duration, is_keyframe, is_bframe);
3025 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3029 uint64_t cluster_time = 0;
3034 av_log(matroska->ctx, AV_LOG_DEBUG,
3035 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3038 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3041 } else if (matroska->level_up) {
3042 matroska->level_up--;
3047 /* cluster timecode */
3048 case MATROSKA_ID_CLUSTERTIMECODE: {
3050 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3056 /* a group of blocks inside a cluster */
3057 case MATROSKA_ID_BLOCKGROUP:
3058 if ((res = ebml_read_master(matroska, &id)) < 0)
3060 res = matroska_parse_blockgroup(matroska, cluster_time);
3063 case MATROSKA_ID_SIMPLEBLOCK:
3064 pos = url_ftell(matroska->ctx->pb);
3065 res = ebml_read_binary(matroska, &id, &data, &size);
3067 res = matroska_parse_block(matroska, data, size, pos,
3068 cluster_time, AV_NOPTS_VALUE,
3073 av_log(matroska->ctx, AV_LOG_INFO,
3074 "Unknown entry 0x%x in cluster data\n", id);
3078 res = ebml_read_skip(matroska);
3082 if (matroska->level_up) {
3083 matroska->level_up--;
3092 matroska_read_packet (AVFormatContext *s,
3095 MatroskaDemuxContext *matroska = s->priv_data;
3099 /* Read stream until we have a packet queued. */
3100 while (matroska_deliver_packet(matroska, pkt)) {
3102 /* Have we already reached the end? */
3104 return AVERROR(EIO);
3108 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3109 return AVERROR(EIO);
3110 } else if (matroska->level_up) {
3111 matroska->level_up--;
3116 case MATROSKA_ID_CLUSTER:
3117 if ((res = ebml_read_master(matroska, &id)) < 0)
3119 if ((res = matroska_parse_cluster(matroska)) == 0)
3120 res = 1; /* Parsed one cluster, let's get out. */
3125 res = ebml_read_skip(matroska);
3129 if (matroska->level_up) {
3130 matroska->level_up--;
3143 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3146 MatroskaDemuxContext *matroska = s->priv_data;
3147 AVStream *st = s->streams[stream_index];
3150 /* find index entry */
3151 index = av_index_search_timestamp(st, timestamp, flags);
3155 matroska_clear_queue(matroska);
3158 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3159 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3160 matroska->skip_to_stream = st;
3161 matroska->peek_id = 0;
3162 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3167 matroska_read_close (AVFormatContext *s)
3169 MatroskaDemuxContext *matroska = s->priv_data;
3172 av_free(matroska->index);
3174 matroska_clear_queue(matroska);
3176 for (n = 0; n < matroska->num_tracks; n++) {
3177 MatroskaTrack *track = matroska->tracks[n];
3178 av_free(track->codec_id);
3179 av_free(track->codec_priv);
3180 av_free(track->name);
3182 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3183 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3184 av_free(audiotrack->buf);
3193 AVInputFormat matroska_demuxer = {
3195 NULL_IF_CONFIG_SMALL("Matroska file format"),
3196 sizeof(MatroskaDemuxContext),
3198 matroska_read_header,
3199 matroska_read_packet,
3200 matroska_read_close,