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. */
60 unsigned char *codec_priv;
64 uint64_t default_duration;
65 MatroskaTrackFlags flags;
68 MatroskaTrackEncodingCompAlgo encoding_algo;
69 uint8_t *encoding_settings;
70 int encoding_settings_len;
73 typedef struct MatroskaVideoTrack {
83 MatroskaAspectRatioMode ar_mode;
84 MatroskaEyeMode eye_mode;
89 typedef struct MatroskaAudioTrack {
94 int internal_samplerate;
98 /* real audio header */
107 } MatroskaAudioTrack;
109 typedef struct MatroskaSubtitleTrack {
112 } MatroskaSubtitleTrack;
114 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
115 sizeof(MatroskaAudioTrack), \
116 sizeof(MatroskaSubtitleTrack)))
118 typedef struct MatroskaLevel {
123 typedef struct MatroskaDemuxIndex {
124 uint64_t pos; /* of the corresponding *cluster*! */
125 uint16_t track; /* reference to 'num' */
126 uint64_t time; /* in nanoseconds */
127 } MatroskaDemuxIndex;
129 typedef struct MatroskaDemuxContext {
130 AVFormatContext *ctx;
134 MatroskaLevel levels[EBML_MAX_DEPTH];
142 /* timescale in the file */
145 /* num_streams is the number of streams that av_new_stream() was called
146 * for ( = that are available to the calling program). */
149 MatroskaTrack *tracks[MAX_STREAMS];
151 /* cache for ID peeking */
154 /* byte position of the segment inside the stream */
155 offset_t segment_start;
157 /* The packet queue. */
161 /* have we already parse metadata/cues/clusters? */
166 /* The index for seeking. */
168 MatroskaDemuxIndex *index;
170 /* What to skip before effectively reading a packet. */
171 int skip_to_keyframe;
172 AVStream *skip_to_stream;
173 } MatroskaDemuxContext;
175 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
178 * The first few functions handle EBML file parsing. The rest
179 * is the document interpretation. Matroska really just is a
184 * Return: the amount of levels in the hierarchy that the
185 * current element lies higher than the previous one.
186 * The opposite isn't done - that's auto-done using master
191 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
193 ByteIOContext *pb = matroska->ctx->pb;
194 offset_t pos = url_ftell(pb);
197 while (matroska->num_levels > 0) {
198 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
200 if (pos >= level->start + level->length) {
201 matroska->num_levels--;
212 * Read: an "EBML number", which is defined as a variable-length
213 * array of bytes. The first byte indicates the length by giving a
214 * number of 0-bits followed by a one. The position of the first
215 * "one" bit inside the first byte indicates the length of this
217 * Returns: num. of bytes read. < 0 on error.
221 ebml_read_num (MatroskaDemuxContext *matroska,
225 ByteIOContext *pb = matroska->ctx->pb;
226 int len_mask = 0x80, read = 1, n = 1;
229 /* the first byte tells us the length in bytes - get_byte() can normally
230 * return 0, but since that's not a valid first ebmlID byte, we can
231 * use it safely here to catch EOS. */
232 if (!(total = get_byte(pb))) {
233 /* we might encounter EOS here */
235 offset_t pos = url_ftell(pb);
236 av_log(matroska->ctx, AV_LOG_ERROR,
237 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
240 return AVERROR(EIO); /* EOS or actual I/O error */
243 /* get the length of the EBML number */
244 while (read <= max_size && !(total & len_mask)) {
248 if (read > max_size) {
249 offset_t pos = url_ftell(pb) - 1;
250 av_log(matroska->ctx, AV_LOG_ERROR,
251 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
252 (uint8_t) total, pos, pos);
253 return AVERROR_INVALIDDATA;
256 /* read out length */
259 total = (total << 8) | get_byte(pb);
267 * Read: the element content data ID.
268 * Return: the number of bytes read or < 0 on error.
272 ebml_read_element_id (MatroskaDemuxContext *matroska,
279 /* if we re-call this, use our cached ID */
280 if (matroska->peek_id != 0) {
283 *id = matroska->peek_id;
287 /* read out the "EBML number", include tag in ID */
288 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
290 *id = matroska->peek_id = total | (1 << (read * 7));
294 *level_up = ebml_read_element_level_up(matroska);
300 * Read: element content length.
301 * Return: the number of bytes read or < 0 on error.
305 ebml_read_element_length (MatroskaDemuxContext *matroska,
308 /* clear cache since we're now beyond that data point */
309 matroska->peek_id = 0;
311 /* read out the "EBML number", include tag in ID */
312 return ebml_read_num(matroska, 8, length);
316 * Return: the ID of the next element, or 0 on error.
317 * Level_up contains the amount of levels that this
318 * next element lies higher than the previous one.
322 ebml_peek_id (MatroskaDemuxContext *matroska,
327 if (ebml_read_element_id(matroska, &id, level_up) < 0)
334 * Seek to a given offset.
335 * 0 is success, -1 is failure.
339 ebml_read_seek (MatroskaDemuxContext *matroska,
342 ByteIOContext *pb = matroska->ctx->pb;
344 /* clear ID cache, if any */
345 matroska->peek_id = 0;
347 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
351 * Skip the next element.
352 * 0 is success, -1 is failure.
356 ebml_read_skip (MatroskaDemuxContext *matroska)
358 ByteIOContext *pb = matroska->ctx->pb;
363 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
364 (res = ebml_read_element_length(matroska, &length)) < 0)
367 url_fskip(pb, length);
373 * Read the next element as an unsigned int.
374 * 0 is success, < 0 is failure.
378 ebml_read_uint (MatroskaDemuxContext *matroska,
382 ByteIOContext *pb = matroska->ctx->pb;
383 int n = 0, size, res;
386 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
387 (res = ebml_read_element_length(matroska, &rlength)) < 0)
390 if (size < 1 || size > 8) {
391 offset_t pos = url_ftell(pb);
392 av_log(matroska->ctx, AV_LOG_ERROR,
393 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
395 return AVERROR_INVALIDDATA;
398 /* big-endian ordening; build up number */
401 *num = (*num << 8) | get_byte(pb);
407 * Read the next element as a signed int.
408 * 0 is success, < 0 is failure.
412 ebml_read_sint (MatroskaDemuxContext *matroska,
416 ByteIOContext *pb = matroska->ctx->pb;
417 int size, n = 1, negative = 0, res;
420 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
421 (res = ebml_read_element_length(matroska, &rlength)) < 0)
424 if (size < 1 || size > 8) {
425 offset_t pos = url_ftell(pb);
426 av_log(matroska->ctx, AV_LOG_ERROR,
427 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
429 return AVERROR_INVALIDDATA;
431 if ((*num = get_byte(pb)) & 0x80) {
436 *num = (*num << 8) | get_byte(pb);
440 *num = *num - (1LL << ((8 * size) - 1));
446 * Read the next element as a float.
447 * 0 is success, < 0 is failure.
451 ebml_read_float (MatroskaDemuxContext *matroska,
455 ByteIOContext *pb = matroska->ctx->pb;
459 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
460 (res = ebml_read_element_length(matroska, &rlength)) < 0)
465 *num= av_int2flt(get_be32(pb));
467 *num= av_int2dbl(get_be64(pb));
469 offset_t pos = url_ftell(pb);
470 av_log(matroska->ctx, AV_LOG_ERROR,
471 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
473 return AVERROR_INVALIDDATA;
480 * Read the next element as an ASCII string.
481 * 0 is success, < 0 is failure.
485 ebml_read_ascii (MatroskaDemuxContext *matroska,
489 ByteIOContext *pb = matroska->ctx->pb;
493 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
494 (res = ebml_read_element_length(matroska, &rlength)) < 0)
498 /* ebml strings are usually not 0-terminated, so we allocate one
499 * byte more, read the string and NULL-terminate it ourselves. */
500 if (size < 0 || !(*str = av_malloc(size + 1))) {
501 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
502 return AVERROR(ENOMEM);
504 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
505 offset_t pos = url_ftell(pb);
506 av_log(matroska->ctx, AV_LOG_ERROR,
507 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
517 * Read the next element as a UTF-8 string.
518 * 0 is success, < 0 is failure.
522 ebml_read_utf8 (MatroskaDemuxContext *matroska,
526 return ebml_read_ascii(matroska, id, str);
530 * Read the next element as a date (nanoseconds since 1/1/2000).
531 * 0 is success, < 0 is failure.
535 ebml_read_date (MatroskaDemuxContext *matroska,
539 return ebml_read_sint(matroska, id, date);
543 * Read the next element, but only the header. The contents
544 * are supposed to be sub-elements which can be read separately.
545 * 0 is success, < 0 is failure.
549 ebml_read_master (MatroskaDemuxContext *matroska,
552 ByteIOContext *pb = matroska->ctx->pb;
554 MatroskaLevel *level;
557 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
558 (res = ebml_read_element_length(matroska, &length)) < 0)
561 /* protect... (Heaven forbids that the '>' is true) */
562 if (matroska->num_levels >= EBML_MAX_DEPTH) {
563 av_log(matroska->ctx, AV_LOG_ERROR,
564 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
565 return AVERROR(ENOSYS);
569 level = &matroska->levels[matroska->num_levels++];
570 level->start = url_ftell(pb);
571 level->length = length;
577 * Read the next element as binary data.
578 * 0 is success, < 0 is failure.
582 ebml_read_binary (MatroskaDemuxContext *matroska,
587 ByteIOContext *pb = matroska->ctx->pb;
591 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
592 (res = ebml_read_element_length(matroska, &rlength)) < 0)
596 if (!(*binary = av_malloc(*size))) {
597 av_log(matroska->ctx, AV_LOG_ERROR,
598 "Memory allocation error\n");
599 return AVERROR(ENOMEM);
602 if (get_buffer(pb, *binary, *size) != *size) {
603 offset_t pos = url_ftell(pb);
604 av_log(matroska->ctx, AV_LOG_ERROR,
605 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
613 * Read signed/unsigned "EBML" numbers.
614 * Return: number of bytes processed, < 0 on error.
615 * XXX: use ebml_read_num().
619 matroska_ebmlnum_uint (uint8_t *data,
623 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
627 return AVERROR_INVALIDDATA;
630 while (read <= 8 && !(total & len_mask)) {
635 return AVERROR_INVALIDDATA;
637 if ((total &= (len_mask - 1)) == len_mask - 1)
640 return AVERROR_INVALIDDATA;
644 total = (total << 8) | data[n];
657 * Same as above, but signed.
661 matroska_ebmlnum_sint (uint8_t *data,
668 /* read as unsigned number first */
669 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
672 /* make signed (weird way) */
673 if (unum == (uint64_t)-1)
676 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
682 * Read an EBML header.
683 * 0 is success, < 0 is failure.
687 ebml_read_header (MatroskaDemuxContext *matroska,
692 int level_up, res = 0;
700 if (!(id = ebml_peek_id(matroska, &level_up)) ||
701 level_up != 0 || id != EBML_ID_HEADER) {
702 av_log(matroska->ctx, AV_LOG_ERROR,
703 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
704 return AVERROR_INVALIDDATA;
706 if ((res = ebml_read_master(matroska, &id)) < 0)
710 if (!(id = ebml_peek_id(matroska, &level_up)))
718 /* is our read version uptodate? */
719 case EBML_ID_EBMLREADVERSION: {
722 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
724 if (num > EBML_VERSION) {
725 av_log(matroska->ctx, AV_LOG_ERROR,
726 "EBML version %"PRIu64" (> %d) is not supported\n",
728 return AVERROR_INVALIDDATA;
733 /* we only handle 8 byte lengths at max */
734 case EBML_ID_EBMLMAXSIZELENGTH: {
737 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
739 if (num > sizeof(uint64_t)) {
740 av_log(matroska->ctx, AV_LOG_ERROR,
741 "Integers of size %"PRIu64" (> %zd) not supported\n",
742 num, sizeof(uint64_t));
743 return AVERROR_INVALIDDATA;
748 /* we handle 4 byte IDs at max */
749 case EBML_ID_EBMLMAXIDLENGTH: {
752 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
754 if (num > sizeof(uint32_t)) {
755 av_log(matroska->ctx, AV_LOG_ERROR,
756 "IDs of size %"PRIu64" (> %zu) not supported\n",
757 num, sizeof(uint32_t));
758 return AVERROR_INVALIDDATA;
763 case EBML_ID_DOCTYPE: {
766 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
777 case EBML_ID_DOCTYPEREADVERSION: {
780 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
788 av_log(matroska->ctx, AV_LOG_INFO,
789 "Unknown data type 0x%x in EBML header", id);
793 /* we ignore these two, as they don't tell us anything we
795 case EBML_ID_EBMLVERSION:
796 case EBML_ID_DOCTYPEVERSION:
797 res = ebml_read_skip (matroska);
807 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
812 for (i = 0; i < matroska->num_tracks; i++)
813 if (matroska->tracks[i]->num == num)
821 * Put one packet in an application-supplied AVPacket struct.
822 * Returns 0 on success or -1 on failure.
826 matroska_deliver_packet (MatroskaDemuxContext *matroska,
829 if (matroska->num_packets > 0) {
830 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
831 av_free(matroska->packets[0]);
832 if (matroska->num_packets > 1) {
833 memmove(&matroska->packets[0], &matroska->packets[1],
834 (matroska->num_packets - 1) * sizeof(AVPacket *));
836 av_realloc(matroska->packets, (matroska->num_packets - 1) *
839 av_freep(&matroska->packets);
841 matroska->num_packets--;
849 * Put a packet into our internal queue. Will be delivered to the
850 * user/application during the next get_packet() call.
854 matroska_queue_packet (MatroskaDemuxContext *matroska,
858 av_realloc(matroska->packets, (matroska->num_packets + 1) *
860 matroska->packets[matroska->num_packets] = pkt;
861 matroska->num_packets++;
865 * Free all packets in our internal queue.
868 matroska_clear_queue (MatroskaDemuxContext *matroska)
870 if (matroska->packets) {
872 for (n = 0; n < matroska->num_packets; n++) {
873 av_free_packet(matroska->packets[n]);
874 av_free(matroska->packets[n]);
876 av_free(matroska->packets);
877 matroska->packets = NULL;
878 matroska->num_packets = 0;
888 matroska_probe (AVProbeData *p)
891 int len_mask = 0x80, size = 1, n = 1;
892 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
895 if (AV_RB32(p->buf) != EBML_ID_HEADER)
898 /* length of header */
900 while (size <= 8 && !(total & len_mask)) {
906 total &= (len_mask - 1);
908 total = (total << 8) | p->buf[4 + n++];
910 /* does the probe data contain the whole header? */
911 if (p->buf_size < 4 + size + total)
914 /* the header must contain the document type 'matroska'. For now,
915 * we don't parse the whole header but simply check for the
916 * availability of that array of characters inside the header.
917 * Not fully fool-proof, but good enough. */
918 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
919 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
920 return AVPROBE_SCORE_MAX;
926 * From here on, it's all XML-style DTD stuff... Needs no comments.
930 matroska_parse_info (MatroskaDemuxContext *matroska)
935 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
938 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
941 } else if (matroska->level_up) {
942 matroska->level_up--;
947 /* cluster timecode */
948 case MATROSKA_ID_TIMECODESCALE: {
950 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
952 matroska->time_scale = num;
956 case MATROSKA_ID_DURATION: {
958 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
960 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
964 case MATROSKA_ID_TITLE: {
966 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
968 strncpy(matroska->ctx->title, text,
969 sizeof(matroska->ctx->title)-1);
974 case MATROSKA_ID_WRITINGAPP: {
976 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
978 matroska->writing_app = text;
982 case MATROSKA_ID_MUXINGAPP: {
984 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
986 matroska->muxing_app = text;
990 case MATROSKA_ID_DATEUTC: {
992 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
994 matroska->created = time;
999 av_log(matroska->ctx, AV_LOG_INFO,
1000 "Unknown entry 0x%x in info header\n", id);
1003 case MATROSKA_ID_SEGMENTUID:
1005 res = ebml_read_skip(matroska);
1009 if (matroska->level_up) {
1010 matroska->level_up--;
1019 matroska_add_stream (MatroskaDemuxContext *matroska)
1023 MatroskaTrack *track;
1025 /* start with the master */
1026 if ((res = ebml_read_master(matroska, &id)) < 0)
1029 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1031 /* Allocate a generic track. As soon as we know its type we'll realloc. */
1032 track = av_mallocz(MAX_TRACK_SIZE);
1033 strcpy(track->language, "eng");
1035 /* try reading the trackentry headers */
1037 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1040 } else if (matroska->level_up > 0) {
1041 matroska->level_up--;
1046 /* track number (unique stream ID) */
1047 case MATROSKA_ID_TRACKNUMBER: {
1049 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1055 /* track UID (unique identifier) */
1056 case MATROSKA_ID_TRACKUID: {
1058 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1064 /* track type (video, audio, combined, subtitle, etc.) */
1065 case MATROSKA_ID_TRACKTYPE: {
1067 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1069 if (track->type && track->type != num) {
1070 av_log(matroska->ctx, AV_LOG_INFO,
1071 "More than one tracktype in an entry - skip\n");
1076 switch (track->type) {
1077 case MATROSKA_TRACK_TYPE_VIDEO:
1078 case MATROSKA_TRACK_TYPE_AUDIO:
1079 case MATROSKA_TRACK_TYPE_SUBTITLE:
1081 case MATROSKA_TRACK_TYPE_COMPLEX:
1082 case MATROSKA_TRACK_TYPE_LOGO:
1083 case MATROSKA_TRACK_TYPE_CONTROL:
1085 av_log(matroska->ctx, AV_LOG_INFO,
1086 "Unknown or unsupported track type 0x%x\n",
1088 track->type = MATROSKA_TRACK_TYPE_NONE;
1094 /* tracktype specific stuff for video */
1095 case MATROSKA_ID_TRACKVIDEO: {
1096 MatroskaVideoTrack *videotrack;
1098 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1099 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1100 av_log(matroska->ctx, AV_LOG_INFO,
1101 "video data in non-video track - ignoring\n");
1102 res = AVERROR_INVALIDDATA;
1104 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1106 videotrack = (MatroskaVideoTrack *)track;
1109 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1112 } else if (matroska->level_up > 0) {
1113 matroska->level_up--;
1118 /* fixme, this should be one-up, but I get it here */
1119 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1121 if ((res = ebml_read_uint (matroska, &id,
1124 track->default_duration = num;
1128 /* video framerate */
1129 case MATROSKA_ID_VIDEOFRAMERATE: {
1131 if ((res = ebml_read_float(matroska, &id,
1134 if (!track->default_duration)
1135 track->default_duration = 1000000000/num;
1139 /* width of the size to display the video at */
1140 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1142 if ((res = ebml_read_uint(matroska, &id,
1145 videotrack->display_width = num;
1149 /* height of the size to display the video at */
1150 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1152 if ((res = ebml_read_uint(matroska, &id,
1155 videotrack->display_height = num;
1159 /* width of the video in the file */
1160 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1162 if ((res = ebml_read_uint(matroska, &id,
1165 videotrack->pixel_width = num;
1169 /* height of the video in the file */
1170 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1172 if ((res = ebml_read_uint(matroska, &id,
1175 videotrack->pixel_height = num;
1179 /* whether the video is interlaced */
1180 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1182 if ((res = ebml_read_uint(matroska, &id,
1187 MATROSKA_VIDEOTRACK_INTERLACED;
1190 ~MATROSKA_VIDEOTRACK_INTERLACED;
1194 /* stereo mode (whether the video has two streams,
1195 * where one is for the left eye and the other for
1196 * the right eye, which creates a 3D-like
1198 case MATROSKA_ID_VIDEOSTEREOMODE: {
1200 if ((res = ebml_read_uint(matroska, &id,
1203 if (num != MATROSKA_EYE_MODE_MONO &&
1204 num != MATROSKA_EYE_MODE_LEFT &&
1205 num != MATROSKA_EYE_MODE_RIGHT &&
1206 num != MATROSKA_EYE_MODE_BOTH) {
1207 av_log(matroska->ctx, AV_LOG_INFO,
1208 "Ignoring unknown eye mode 0x%x\n",
1212 videotrack->eye_mode = num;
1216 /* aspect ratio behaviour */
1217 case MATROSKA_ID_VIDEOASPECTRATIO: {
1219 if ((res = ebml_read_uint(matroska, &id,
1222 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1223 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1224 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1225 av_log(matroska->ctx, AV_LOG_INFO,
1226 "Ignoring unknown aspect ratio 0x%x\n",
1230 videotrack->ar_mode = num;
1234 /* colorspace (only matters for raw video)
1236 case MATROSKA_ID_VIDEOCOLORSPACE: {
1238 if ((res = ebml_read_uint(matroska, &id,
1241 videotrack->fourcc = num;
1246 av_log(matroska->ctx, AV_LOG_INFO,
1247 "Unknown video track header entry "
1248 "0x%x - ignoring\n", id);
1252 res = ebml_read_skip(matroska);
1256 if (matroska->level_up) {
1257 matroska->level_up--;
1264 /* tracktype specific stuff for audio */
1265 case MATROSKA_ID_TRACKAUDIO: {
1266 MatroskaAudioTrack *audiotrack;
1268 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1269 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1270 av_log(matroska->ctx, AV_LOG_INFO,
1271 "audio data in non-audio track - ignoring\n");
1272 res = AVERROR_INVALIDDATA;
1274 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1276 audiotrack = (MatroskaAudioTrack *)track;
1277 audiotrack->channels = 1;
1278 audiotrack->samplerate = 8000;
1281 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1284 } else if (matroska->level_up > 0) {
1285 matroska->level_up--;
1291 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1293 if ((res = ebml_read_float(matroska, &id,
1296 audiotrack->internal_samplerate =
1297 audiotrack->samplerate = num;
1301 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1303 if ((res = ebml_read_float(matroska, &id,
1306 audiotrack->samplerate = num;
1311 case MATROSKA_ID_AUDIOBITDEPTH: {
1313 if ((res = ebml_read_uint(matroska, &id,
1316 audiotrack->bitdepth = num;
1321 case MATROSKA_ID_AUDIOCHANNELS: {
1323 if ((res = ebml_read_uint(matroska, &id,
1326 audiotrack->channels = num;
1331 av_log(matroska->ctx, AV_LOG_INFO,
1332 "Unknown audio track header entry "
1333 "0x%x - ignoring\n", id);
1337 res = ebml_read_skip(matroska);
1341 if (matroska->level_up) {
1342 matroska->level_up--;
1349 /* codec identifier */
1350 case MATROSKA_ID_CODECID: {
1352 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1354 track->codec_id = text;
1358 /* codec private data */
1359 case MATROSKA_ID_CODECPRIVATE: {
1362 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1364 track->codec_priv = data;
1365 track->codec_priv_size = size;
1369 /* name of the codec */
1370 case MATROSKA_ID_CODECNAME: {
1372 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1374 track->codec_name = text;
1378 /* name of this track */
1379 case MATROSKA_ID_TRACKNAME: {
1381 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1387 /* language (matters for audio/subtitles, mostly) */
1388 case MATROSKA_ID_TRACKLANGUAGE: {
1390 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1392 if ((end = strchr(text, '-')))
1394 if (strlen(text) == 3)
1395 strcpy(track->language, text);
1400 /* whether this is actually used */
1401 case MATROSKA_ID_TRACKFLAGENABLED: {
1403 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1406 track->flags |= MATROSKA_TRACK_ENABLED;
1408 track->flags &= ~MATROSKA_TRACK_ENABLED;
1412 /* whether it's the default for this track type */
1413 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1415 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1418 track->flags |= MATROSKA_TRACK_DEFAULT;
1420 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1424 /* lacing (like MPEG, where blocks don't end/start on frame
1426 case MATROSKA_ID_TRACKFLAGLACING: {
1428 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1431 track->flags |= MATROSKA_TRACK_LACING;
1433 track->flags &= ~MATROSKA_TRACK_LACING;
1437 /* default length (in time) of one data block in this track */
1438 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1440 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1442 track->default_duration = num;
1446 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1447 if ((res = ebml_read_master(matroska, &id)) < 0)
1451 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1454 } else if (matroska->level_up > 0) {
1455 matroska->level_up--;
1460 case MATROSKA_ID_TRACKCONTENTENCODING: {
1461 int encoding_scope = 1;
1462 if ((res = ebml_read_master(matroska, &id)) < 0)
1466 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1469 } else if (matroska->level_up > 0) {
1470 matroska->level_up--;
1475 case MATROSKA_ID_ENCODINGSCOPE: {
1477 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1479 encoding_scope = num;
1483 case MATROSKA_ID_ENCODINGTYPE: {
1485 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1488 av_log(matroska->ctx, AV_LOG_ERROR,
1489 "Unsupported encoding type");
1493 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1494 if ((res = ebml_read_master(matroska, &id)) < 0)
1498 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1501 } else if (matroska->level_up > 0) {
1502 matroska->level_up--;
1507 case MATROSKA_ID_ENCODINGCOMPALGO: {
1509 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1511 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1513 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1516 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1518 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1519 av_log(matroska->ctx, AV_LOG_ERROR,
1520 "Unsupported compression algo\n");
1521 track->encoding_algo = num;
1525 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1528 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1530 track->encoding_settings = data;
1531 track->encoding_settings_len = size;
1536 av_log(matroska->ctx, AV_LOG_INFO,
1537 "Unknown compression header entry "
1538 "0x%x - ignoring\n", id);
1542 res = ebml_read_skip(matroska);
1546 if (matroska->level_up) {
1547 matroska->level_up--;
1555 av_log(matroska->ctx, AV_LOG_INFO,
1556 "Unknown content encoding header entry "
1557 "0x%x - ignoring\n", id);
1561 res = ebml_read_skip(matroska);
1565 if (matroska->level_up) {
1566 matroska->level_up--;
1571 track->encoding_scope = encoding_scope;
1576 av_log(matroska->ctx, AV_LOG_INFO,
1577 "Unknown content encodings header entry "
1578 "0x%x - ignoring\n", id);
1582 res = ebml_read_skip(matroska);
1586 if (matroska->level_up) {
1587 matroska->level_up--;
1594 case MATROSKA_ID_TRACKTIMECODESCALE: {
1596 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1598 track->time_scale = num;
1603 av_log(matroska->ctx, AV_LOG_INFO,
1604 "Unknown track header entry 0x%x - ignoring\n", id);
1608 /* we ignore these because they're nothing useful. */
1609 case MATROSKA_ID_TRACKFLAGFORCED:
1610 case MATROSKA_ID_CODECDECODEALL:
1611 case MATROSKA_ID_CODECINFOURL:
1612 case MATROSKA_ID_CODECDOWNLOADURL:
1613 case MATROSKA_ID_TRACKMINCACHE:
1614 case MATROSKA_ID_TRACKMAXCACHE:
1615 res = ebml_read_skip(matroska);
1619 if (matroska->level_up) {
1620 matroska->level_up--;
1625 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1626 matroska->tracks[matroska->num_tracks++] = track;
1634 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1639 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1642 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1645 } else if (matroska->level_up) {
1646 matroska->level_up--;
1651 /* one track within the "all-tracks" header */
1652 case MATROSKA_ID_TRACKENTRY:
1653 res = matroska_add_stream(matroska);
1657 av_log(matroska->ctx, AV_LOG_INFO,
1658 "Unknown entry 0x%x in track header\n", id);
1662 res = ebml_read_skip(matroska);
1666 if (matroska->level_up) {
1667 matroska->level_up--;
1676 matroska_parse_index (MatroskaDemuxContext *matroska)
1680 MatroskaDemuxIndex idx;
1682 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1685 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1688 } else if (matroska->level_up) {
1689 matroska->level_up--;
1694 /* one single index entry ('point') */
1695 case MATROSKA_ID_POINTENTRY:
1696 if ((res = ebml_read_master(matroska, &id)) < 0)
1699 /* in the end, we hope to fill one entry with a
1700 * timestamp, a file position and a tracknum */
1701 idx.pos = (uint64_t) -1;
1702 idx.time = (uint64_t) -1;
1703 idx.track = (uint16_t) -1;
1706 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1709 } else if (matroska->level_up) {
1710 matroska->level_up--;
1715 /* one single index entry ('point') */
1716 case MATROSKA_ID_CUETIME: {
1718 if ((res = ebml_read_uint(matroska, &id,
1721 idx.time = time * matroska->time_scale;
1725 /* position in the file + track to which it
1727 case MATROSKA_ID_CUETRACKPOSITION:
1728 if ((res = ebml_read_master(matroska, &id)) < 0)
1732 if (!(id = ebml_peek_id (matroska,
1733 &matroska->level_up))) {
1736 } else if (matroska->level_up) {
1737 matroska->level_up--;
1743 case MATROSKA_ID_CUETRACK: {
1745 if ((res = ebml_read_uint(matroska,
1752 /* position in file */
1753 case MATROSKA_ID_CUECLUSTERPOSITION: {
1755 if ((res = ebml_read_uint(matroska,
1758 idx.pos = num+matroska->segment_start;
1763 av_log(matroska->ctx, AV_LOG_INFO,
1764 "Unknown entry 0x%x in "
1765 "CuesTrackPositions\n", id);
1769 res = ebml_read_skip(matroska);
1773 if (matroska->level_up) {
1774 matroska->level_up--;
1782 av_log(matroska->ctx, AV_LOG_INFO,
1783 "Unknown entry 0x%x in cuespoint "
1788 res = ebml_read_skip(matroska);
1792 if (matroska->level_up) {
1793 matroska->level_up--;
1798 /* so let's see if we got what we wanted */
1799 if (idx.pos != (uint64_t) -1 &&
1800 idx.time != (uint64_t) -1 &&
1801 idx.track != (uint16_t) -1) {
1802 if (matroska->num_indexes % 32 == 0) {
1803 /* re-allocate bigger index */
1805 av_realloc(matroska->index,
1806 (matroska->num_indexes + 32) *
1807 sizeof(MatroskaDemuxIndex));
1809 matroska->index[matroska->num_indexes] = idx;
1810 matroska->num_indexes++;
1815 av_log(matroska->ctx, AV_LOG_INFO,
1816 "Unknown entry 0x%x in cues header\n", id);
1820 res = ebml_read_skip(matroska);
1824 if (matroska->level_up) {
1825 matroska->level_up--;
1834 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1840 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1843 } else if (matroska->level_up) {
1844 matroska->level_up--;
1849 /* Hm, this is unsupported... */
1851 av_log(matroska->ctx, AV_LOG_INFO,
1852 "Unknown entry 0x%x in metadata header\n", id);
1856 res = ebml_read_skip(matroska);
1860 if (matroska->level_up) {
1861 matroska->level_up--;
1870 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1875 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1878 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1881 } else if (matroska->level_up) {
1882 matroska->level_up--;
1887 case MATROSKA_ID_SEEKENTRY: {
1888 uint32_t seek_id = 0, peek_id_cache = 0;
1889 uint64_t seek_pos = (uint64_t) -1, t;
1891 if ((res = ebml_read_master(matroska, &id)) < 0)
1895 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1898 } else if (matroska->level_up) {
1899 matroska->level_up--;
1904 case MATROSKA_ID_SEEKID:
1905 res = ebml_read_uint(matroska, &id, &t);
1909 case MATROSKA_ID_SEEKPOSITION:
1910 res = ebml_read_uint(matroska, &id, &seek_pos);
1914 av_log(matroska->ctx, AV_LOG_INFO,
1915 "Unknown seekhead ID 0x%x\n", id);
1919 res = ebml_read_skip(matroska);
1923 if (matroska->level_up) {
1924 matroska->level_up--;
1929 if (!seek_id || seek_pos == (uint64_t) -1) {
1930 av_log(matroska->ctx, AV_LOG_INFO,
1931 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1937 case MATROSKA_ID_CUES:
1938 case MATROSKA_ID_TAGS: {
1939 uint32_t level_up = matroska->level_up;
1940 offset_t before_pos;
1942 MatroskaLevel level;
1944 /* remember the peeked ID and the current position */
1945 peek_id_cache = matroska->peek_id;
1946 before_pos = url_ftell(matroska->ctx->pb);
1949 if ((res = ebml_read_seek(matroska, seek_pos +
1950 matroska->segment_start)) < 0)
1953 /* we don't want to lose our seekhead level, so we add
1954 * a dummy. This is a crude hack. */
1955 if (matroska->num_levels == EBML_MAX_DEPTH) {
1956 av_log(matroska->ctx, AV_LOG_INFO,
1957 "Max EBML element depth (%d) reached, "
1958 "cannot parse further.\n", EBML_MAX_DEPTH);
1959 return AVERROR_UNKNOWN;
1963 level.length = (uint64_t)-1;
1964 matroska->levels[matroska->num_levels] = level;
1965 matroska->num_levels++;
1968 if (!(id = ebml_peek_id (matroska,
1969 &matroska->level_up)))
1971 if (id != seek_id) {
1972 av_log(matroska->ctx, AV_LOG_INFO,
1973 "We looked for ID=0x%x but got "
1974 "ID=0x%x (pos=%"PRIu64")",
1975 seek_id, id, seek_pos +
1976 matroska->segment_start);
1980 /* read master + parse */
1981 if ((res = ebml_read_master(matroska, &id)) < 0)
1984 case MATROSKA_ID_CUES:
1985 if (!(res = matroska_parse_index(matroska)) ||
1986 url_feof(matroska->ctx->pb)) {
1987 matroska->index_parsed = 1;
1991 case MATROSKA_ID_TAGS:
1992 if (!(res = matroska_parse_metadata(matroska)) ||
1993 url_feof(matroska->ctx->pb)) {
1994 matroska->metadata_parsed = 1;
2001 /* remove dummy level */
2002 while (matroska->num_levels) {
2003 matroska->num_levels--;
2005 matroska->levels[matroska->num_levels].length;
2006 if (length == (uint64_t)-1)
2011 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2013 matroska->peek_id = peek_id_cache;
2014 matroska->level_up = level_up;
2019 av_log(matroska->ctx, AV_LOG_INFO,
2020 "Ignoring seekhead entry for ID=0x%x\n",
2029 av_log(matroska->ctx, AV_LOG_INFO,
2030 "Unknown seekhead ID 0x%x\n", id);
2034 res = ebml_read_skip(matroska);
2038 if (matroska->level_up) {
2039 matroska->level_up--;
2048 matroska_parse_attachments(AVFormatContext *s)
2050 MatroskaDemuxContext *matroska = s->priv_data;
2054 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2057 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2060 } else if (matroska->level_up) {
2061 matroska->level_up--;
2066 case MATROSKA_ID_ATTACHEDFILE: {
2069 uint8_t* data = NULL;
2070 int i, data_size = 0;
2073 if ((res = ebml_read_master(matroska, &id)) < 0)
2077 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2080 } else if (matroska->level_up) {
2081 matroska->level_up--;
2086 case MATROSKA_ID_FILENAME:
2087 res = ebml_read_utf8 (matroska, &id, &name);
2090 case MATROSKA_ID_FILEMIMETYPE:
2091 res = ebml_read_ascii (matroska, &id, &mime);
2094 case MATROSKA_ID_FILEDATA:
2095 res = ebml_read_binary(matroska, &id, &data, &data_size);
2099 av_log(matroska->ctx, AV_LOG_INFO,
2100 "Unknown attachedfile ID 0x%x\n", id);
2101 case MATROSKA_ID_FILEUID:
2103 res = ebml_read_skip(matroska);
2107 if (matroska->level_up) {
2108 matroska->level_up--;
2113 if (!(name && mime && data && data_size > 0)) {
2114 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2118 st = av_new_stream(s, matroska->num_streams++);
2120 return AVERROR(ENOMEM);
2121 st->filename = av_strdup(name);
2122 st->codec->codec_id = CODEC_ID_NONE;
2123 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2124 st->codec->extradata = av_malloc(data_size);
2125 if(st->codec->extradata == NULL)
2126 return AVERROR(ENOMEM);
2127 st->codec->extradata_size = data_size;
2128 memcpy(st->codec->extradata, data, data_size);
2130 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2131 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2132 strlen(ff_mkv_mime_tags[i].str))) {
2133 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2138 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2143 av_log(matroska->ctx, AV_LOG_INFO,
2144 "Unknown attachments ID 0x%x\n", id);
2148 res = ebml_read_skip(matroska);
2152 if (matroska->level_up) {
2153 matroska->level_up--;
2162 matroska_parse_chapters(AVFormatContext *s)
2164 MatroskaDemuxContext *matroska = s->priv_data;
2168 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2171 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2174 } else if (matroska->level_up) {
2175 matroska->level_up--;
2180 case MATROSKA_ID_EDITIONENTRY: {
2181 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2184 /* if there is more than one chapter edition
2185 we take only the first one */
2187 ebml_read_skip(matroska);
2191 if ((res = ebml_read_master(matroska, &id)) < 0)
2195 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2198 } else if (matroska->level_up) {
2199 matroska->level_up--;
2204 case MATROSKA_ID_CHAPTERATOM:
2205 if ((res = ebml_read_master(matroska, &id)) < 0)
2209 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2212 } else if (matroska->level_up) {
2213 matroska->level_up--;
2218 case MATROSKA_ID_CHAPTERTIMEEND:
2219 res = ebml_read_uint(matroska, &id, &end);
2222 case MATROSKA_ID_CHAPTERTIMESTART:
2223 res = ebml_read_uint(matroska, &id, &start);
2226 case MATROSKA_ID_CHAPTERDISPLAY:
2227 if ((res = ebml_read_master(matroska, &id)) < 0)
2231 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2234 } else if (matroska->level_up) {
2235 matroska->level_up--;
2240 case MATROSKA_ID_CHAPSTRING:
2241 res = ebml_read_utf8(matroska, &id, &title);
2245 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2247 res = ebml_read_skip(matroska);
2251 if (matroska->level_up) {
2252 matroska->level_up--;
2258 case MATROSKA_ID_CHAPTERUID:
2259 res = ebml_read_uint(matroska, &id, &uid);
2262 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2263 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2265 res = ebml_read_skip(matroska);
2269 if (matroska->level_up) {
2270 matroska->level_up--;
2275 if (start != AV_NOPTS_VALUE && uid != -1) {
2276 if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2277 res= AVERROR(ENOMEM);
2283 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2284 case MATROSKA_ID_EDITIONUID:
2285 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2286 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2288 res = ebml_read_skip(matroska);
2293 if (matroska->level_up) {
2294 matroska->level_up--;
2302 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2304 res = ebml_read_skip(matroska);
2308 if (matroska->level_up) {
2309 matroska->level_up--;
2318 matroska_aac_profile (char *codec_id)
2320 static const char *aac_profiles[] = {
2325 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2326 if (strstr(codec_id, aac_profiles[profile]))
2332 matroska_aac_sri (int samplerate)
2336 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2337 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2343 matroska_read_header (AVFormatContext *s,
2344 AVFormatParameters *ap)
2346 MatroskaDemuxContext *matroska = s->priv_data;
2348 int version, last_level, res = 0;
2353 /* First read the EBML header. */
2355 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2357 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2358 av_log(matroska->ctx, AV_LOG_ERROR,
2359 "Wrong EBML doctype ('%s' != 'matroska').\n",
2360 doctype ? doctype : "(none)");
2363 return AVERROR_NOFMT;
2367 av_log(matroska->ctx, AV_LOG_ERROR,
2368 "Matroska demuxer version 2 too old for file version %d\n",
2370 return AVERROR_NOFMT;
2373 /* The next thing is a segment. */
2375 if (!(id = ebml_peek_id(matroska, &last_level)))
2376 return AVERROR(EIO);
2377 if (id == MATROSKA_ID_SEGMENT)
2381 av_log(matroska->ctx, AV_LOG_INFO,
2382 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2383 MATROSKA_ID_SEGMENT, id);
2384 if ((res = ebml_read_skip(matroska)) < 0)
2388 /* We now have a Matroska segment.
2389 * Seeks are from the beginning of the segment,
2390 * after the segment ID/length. */
2391 if ((res = ebml_read_master(matroska, &id)) < 0)
2393 matroska->segment_start = url_ftell(s->pb);
2395 matroska->time_scale = 1000000;
2396 /* we've found our segment, start reading the different contents in here */
2398 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2401 } else if (matroska->level_up) {
2402 matroska->level_up--;
2408 case MATROSKA_ID_INFO: {
2409 if ((res = ebml_read_master(matroska, &id)) < 0)
2411 res = matroska_parse_info(matroska);
2415 /* track info headers */
2416 case MATROSKA_ID_TRACKS: {
2417 if ((res = ebml_read_master(matroska, &id)) < 0)
2419 res = matroska_parse_tracks(matroska);
2424 case MATROSKA_ID_CUES: {
2425 if (!matroska->index_parsed) {
2426 if ((res = ebml_read_master(matroska, &id)) < 0)
2428 res = matroska_parse_index(matroska);
2430 res = ebml_read_skip(matroska);
2435 case MATROSKA_ID_TAGS: {
2436 if (!matroska->metadata_parsed) {
2437 if ((res = ebml_read_master(matroska, &id)) < 0)
2439 res = matroska_parse_metadata(matroska);
2441 res = ebml_read_skip(matroska);
2445 /* file index (if seekable, seek to Cues/Tags to parse it) */
2446 case MATROSKA_ID_SEEKHEAD: {
2447 if ((res = ebml_read_master(matroska, &id)) < 0)
2449 res = matroska_parse_seekhead(matroska);
2453 case MATROSKA_ID_ATTACHMENTS: {
2454 if ((res = ebml_read_master(matroska, &id)) < 0)
2456 res = matroska_parse_attachments(s);
2460 case MATROSKA_ID_CLUSTER: {
2461 /* Do not read the master - this will be done in the next
2462 * call to matroska_read_packet. */
2467 case MATROSKA_ID_CHAPTERS: {
2468 if ((res = ebml_read_master(matroska, &id)) < 0)
2470 res = matroska_parse_chapters(s);
2475 av_log(matroska->ctx, AV_LOG_INFO,
2476 "Unknown matroska file header ID 0x%x\n", id);
2480 res = ebml_read_skip(matroska);
2484 if (matroska->level_up) {
2485 matroska->level_up--;
2490 /* Have we found a cluster? */
2491 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2493 MatroskaTrack *track;
2496 for (i = 0; i < matroska->num_tracks; i++) {
2497 enum CodecID codec_id = CODEC_ID_NONE;
2498 uint8_t *extradata = NULL;
2499 int extradata_size = 0;
2500 int extradata_offset = 0;
2501 track = matroska->tracks[i];
2502 track->stream_index = -1;
2504 /* Apply some sanity checks. */
2505 if (track->codec_id == NULL)
2508 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2509 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2510 strlen(ff_mkv_codec_tags[j].str))){
2511 codec_id= ff_mkv_codec_tags[j].id;
2516 /* Set the FourCC from the CodecID. */
2517 /* This is the MS compatibility mode which stores a
2518 * BITMAPINFOHEADER in the CodecPrivate. */
2519 if (!strcmp(track->codec_id,
2520 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2521 (track->codec_priv_size >= 40) &&
2522 (track->codec_priv != NULL)) {
2523 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2525 /* Offset of biCompression. Stored in LE. */
2526 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2527 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2531 /* This is the MS compatibility mode which stores a
2532 * WAVEFORMATEX in the CodecPrivate. */
2533 else if (!strcmp(track->codec_id,
2534 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2535 (track->codec_priv_size >= 18) &&
2536 (track->codec_priv != NULL)) {
2539 /* Offset of wFormatTag. Stored in LE. */
2540 tag = AV_RL16(track->codec_priv);
2541 codec_id = codec_get_id(codec_wav_tags, tag);
2545 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2546 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2547 int profile = matroska_aac_profile(track->codec_id);
2548 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2549 extradata = av_malloc(5);
2550 if (extradata == NULL)
2551 return AVERROR(ENOMEM);
2552 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2553 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2554 if (strstr(track->codec_id, "SBR")) {
2555 sri = matroska_aac_sri(audiotrack->samplerate);
2556 extradata[2] = 0x56;
2557 extradata[3] = 0xE5;
2558 extradata[4] = 0x80 | (sri<<3);
2565 else if (codec_id == CODEC_ID_TTA) {
2566 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2568 extradata_size = 30;
2569 extradata = av_mallocz(extradata_size);
2570 if (extradata == NULL)
2571 return AVERROR(ENOMEM);
2572 init_put_byte(&b, extradata, extradata_size, 1,
2573 NULL, NULL, NULL, NULL);
2574 put_buffer(&b, "TTA1", 4);
2576 put_le16(&b, audiotrack->channels);
2577 put_le16(&b, audiotrack->bitdepth);
2578 put_le32(&b, audiotrack->samplerate);
2579 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2582 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2583 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2584 extradata_offset = 26;
2585 track->codec_priv_size -= extradata_offset;
2588 else if (codec_id == CODEC_ID_RA_144) {
2589 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2590 audiotrack->samplerate = 8000;
2591 audiotrack->channels = 1;
2594 else if (codec_id == CODEC_ID_RA_288 ||
2595 codec_id == CODEC_ID_COOK ||
2596 codec_id == CODEC_ID_ATRAC3) {
2597 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2600 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2601 NULL, NULL, NULL, NULL);
2603 audiotrack->coded_framesize = get_be32(&b);
2605 audiotrack->sub_packet_h = get_be16(&b);
2606 audiotrack->frame_size = get_be16(&b);
2607 audiotrack->sub_packet_size = get_be16(&b);
2608 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2609 if (codec_id == CODEC_ID_RA_288) {
2610 audiotrack->block_align = audiotrack->coded_framesize;
2611 track->codec_priv_size = 0;
2613 audiotrack->block_align = audiotrack->sub_packet_size;
2614 extradata_offset = 78;
2615 track->codec_priv_size -= extradata_offset;
2619 if (codec_id == CODEC_ID_NONE) {
2620 av_log(matroska->ctx, AV_LOG_INFO,
2621 "Unknown/unsupported CodecID %s.\n",
2625 track->stream_index = matroska->num_streams;
2627 matroska->num_streams++;
2628 st = av_new_stream(s, track->stream_index);
2630 return AVERROR(ENOMEM);
2631 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2633 st->codec->codec_id = codec_id;
2635 if (strcmp(track->language, "und"))
2636 strcpy(st->language, track->language);
2638 if (track->flags & MATROSKA_TRACK_DEFAULT)
2639 st->disposition |= AV_DISPOSITION_DEFAULT;
2641 if (track->default_duration)
2642 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2643 track->default_duration, 1000000000, 30000);
2646 st->codec->extradata = extradata;
2647 st->codec->extradata_size = extradata_size;
2648 } else if(track->codec_priv && track->codec_priv_size > 0){
2649 st->codec->extradata = av_malloc(track->codec_priv_size);
2650 if(st->codec->extradata == NULL)
2651 return AVERROR(ENOMEM);
2652 st->codec->extradata_size = track->codec_priv_size;
2653 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2654 track->codec_priv_size);
2657 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2658 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2660 st->codec->codec_type = CODEC_TYPE_VIDEO;
2661 st->codec->codec_tag = videotrack->fourcc;
2662 st->codec->width = videotrack->pixel_width;
2663 st->codec->height = videotrack->pixel_height;
2664 if (videotrack->display_width == 0)
2665 videotrack->display_width= videotrack->pixel_width;
2666 if (videotrack->display_height == 0)
2667 videotrack->display_height= videotrack->pixel_height;
2668 av_reduce(&st->codec->sample_aspect_ratio.num,
2669 &st->codec->sample_aspect_ratio.den,
2670 st->codec->height * videotrack->display_width,
2671 st->codec-> width * videotrack->display_height,
2673 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2674 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2675 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2677 st->codec->codec_type = CODEC_TYPE_AUDIO;
2678 st->codec->sample_rate = audiotrack->samplerate;
2679 st->codec->channels = audiotrack->channels;
2680 st->codec->block_align = audiotrack->block_align;
2681 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2682 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2685 /* What do we do with private data? E.g. for Vorbis. */
2690 if (matroska->index_parsed) {
2691 int i, track, stream;
2692 for (i=0; i<matroska->num_indexes; i++) {
2693 MatroskaDemuxIndex *idx = &matroska->index[i];
2694 track = matroska_find_track_by_num(matroska, idx->track);
2695 if (track < 0) continue;
2696 stream = matroska->tracks[track]->stream_index;
2697 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2698 av_add_index_entry(matroska->ctx->streams[stream],
2699 idx->pos, idx->time/AV_TIME_BASE,
2700 0, 0, AVINDEX_KEYFRAME);
2708 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2709 int64_t pos, uint64_t cluster_time, uint64_t duration,
2710 int is_keyframe, int is_bframe)
2716 uint8_t *origdata = data;
2718 uint32_t *lace_size = NULL;
2719 int n, flags, laces = 0;
2723 /* first byte(s): tracknum */
2724 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2725 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2732 /* fetch track from num */
2733 track = matroska_find_track_by_num(matroska, num);
2734 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2735 av_log(matroska->ctx, AV_LOG_INFO,
2736 "Invalid stream %d or size %u\n", track, size);
2740 stream_index = matroska->tracks[track]->stream_index;
2741 if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2745 st = matroska->ctx->streams[stream_index];
2746 if (st->discard >= AVDISCARD_ALL) {
2750 if (duration == AV_NOPTS_VALUE)
2751 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2753 /* block_time (relative to cluster time) */
2754 block_time = AV_RB16(data);
2758 if (is_keyframe == -1)
2759 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2761 if (matroska->skip_to_keyframe) {
2762 if (!is_keyframe || st != matroska->skip_to_stream) {
2766 matroska->skip_to_keyframe = 0;
2769 switch ((flags & 0x06) >> 1) {
2770 case 0x0: /* no lacing */
2772 lace_size = av_mallocz(sizeof(int));
2773 lace_size[0] = size;
2776 case 0x1: /* xiph lacing */
2777 case 0x2: /* fixed-size lacing */
2778 case 0x3: /* EBML lacing */
2779 assert(size>0); // size <=3 is checked before size-=3 above
2780 laces = (*data) + 1;
2783 lace_size = av_mallocz(laces * sizeof(int));
2785 switch ((flags & 0x06) >> 1) {
2786 case 0x1: /* xiph lacing */ {
2789 for (n = 0; res == 0 && n < laces - 1; n++) {
2796 lace_size[n] += temp;
2802 total += lace_size[n];
2804 lace_size[n] = size - total;
2808 case 0x2: /* fixed-size lacing */
2809 for (n = 0; n < laces; n++)
2810 lace_size[n] = size / laces;
2813 case 0x3: /* EBML lacing */ {
2815 n = matroska_ebmlnum_uint(data, size, &num);
2817 av_log(matroska->ctx, AV_LOG_INFO,
2818 "EBML block data error\n");
2823 total = lace_size[0] = num;
2824 for (n = 1; res == 0 && n < laces - 1; n++) {
2827 r = matroska_ebmlnum_sint (data, size, &snum);
2829 av_log(matroska->ctx, AV_LOG_INFO,
2830 "EBML block data error\n");
2835 lace_size[n] = lace_size[n - 1] + snum;
2836 total += lace_size[n];
2838 lace_size[n] = size - total;
2846 uint64_t timecode = AV_NOPTS_VALUE;
2848 if (cluster_time != (uint64_t)-1
2849 && (block_time >= 0 || cluster_time >= -block_time))
2850 timecode = cluster_time + block_time;
2852 for (n = 0; n < laces; n++) {
2853 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2854 st->codec->codec_id == CODEC_ID_COOK ||
2855 st->codec->codec_id == CODEC_ID_ATRAC3) {
2856 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2857 int a = st->codec->block_align;
2858 int sps = audiotrack->sub_packet_size;
2859 int cfs = audiotrack->coded_framesize;
2860 int h = audiotrack->sub_packet_h;
2861 int y = audiotrack->sub_packet_cnt;
2862 int w = audiotrack->frame_size;
2865 if (!audiotrack->pkt_cnt) {
2866 if (st->codec->codec_id == CODEC_ID_RA_288)
2867 for (x=0; x<h/2; x++)
2868 memcpy(audiotrack->buf+x*2*w+y*cfs,
2871 for (x=0; x<w/sps; x++)
2872 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2874 if (++audiotrack->sub_packet_cnt >= h) {
2875 audiotrack->sub_packet_cnt = 0;
2876 audiotrack->pkt_cnt = h*w / a;
2879 while (audiotrack->pkt_cnt) {
2880 pkt = av_mallocz(sizeof(AVPacket));
2881 av_new_packet(pkt, a);
2882 memcpy(pkt->data, audiotrack->buf
2883 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2885 pkt->stream_index = stream_index;
2886 matroska_queue_packet(matroska, pkt);
2889 int result, offset = 0, ilen, olen, pkt_size = lace_size[n];
2890 uint8_t *pkt_data = data;
2892 if (matroska->tracks[track]->encoding_scope & 1) {
2893 switch (matroska->tracks[track]->encoding_algo) {
2894 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
2895 offset = matroska->tracks[track]->encoding_settings_len;
2897 case MATROSKA_TRACK_ENCODING_COMP_LZO:
2900 ilen = lace_size[n];
2901 olen = pkt_size *= 3;
2902 pkt_data = av_realloc(pkt_data,
2903 pkt_size+LZO_OUTPUT_PADDING);
2904 result = lzo1x_decode(pkt_data, &olen, data, &ilen);
2905 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
2913 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
2914 z_stream zstream = {0};
2916 if (inflateInit(&zstream) != Z_OK)
2918 zstream.next_in = data;
2919 zstream.avail_in = lace_size[n];
2922 pkt_data = av_realloc(pkt_data, pkt_size);
2923 zstream.avail_out = pkt_size - zstream.total_out;
2924 zstream.next_out = pkt_data + zstream.total_out;
2925 result = inflate(&zstream, Z_NO_FLUSH);
2926 } while (result==Z_OK && pkt_size<10000000);
2927 pkt_size = zstream.total_out;
2928 inflateEnd(&zstream);
2929 if (result != Z_STREAM_END) {
2937 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
2938 bz_stream bzstream = {0};
2940 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
2942 bzstream.next_in = data;
2943 bzstream.avail_in = lace_size[n];
2946 pkt_data = av_realloc(pkt_data, pkt_size);
2947 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
2948 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
2949 result = BZ2_bzDecompress(&bzstream);
2950 } while (result==BZ_OK && pkt_size<10000000);
2951 pkt_size = bzstream.total_out_lo32;
2952 BZ2_bzDecompressEnd(&bzstream);
2953 if (result != BZ_STREAM_END) {
2963 pkt = av_mallocz(sizeof(AVPacket));
2964 /* XXX: prevent data copy... */
2965 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2967 res = AVERROR(ENOMEM);
2972 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2973 memcpy (pkt->data+offset, pkt_data, pkt_size);
2976 pkt->flags = is_keyframe;
2977 pkt->stream_index = stream_index;
2979 pkt->pts = timecode;
2981 pkt->duration = duration;
2983 matroska_queue_packet(matroska, pkt);
2986 if (timecode != AV_NOPTS_VALUE)
2987 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2988 data += lace_size[n];
2998 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2999 uint64_t cluster_time)
3004 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
3005 uint64_t duration = AV_NOPTS_VALUE;
3010 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
3013 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3016 } else if (matroska->level_up) {
3017 matroska->level_up--;
3022 /* one block inside the group. Note, block parsing is one
3023 * of the harder things, so this code is a bit complicated.
3024 * See http://www.matroska.org/ for documentation. */
3025 case MATROSKA_ID_BLOCK: {
3026 pos = url_ftell(matroska->ctx->pb);
3027 res = ebml_read_binary(matroska, &id, &data, &size);
3031 case MATROSKA_ID_BLOCKDURATION: {
3032 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
3037 case MATROSKA_ID_BLOCKREFERENCE: {
3039 /* We've found a reference, so not even the first frame in
3040 * the lace is a key frame. */
3042 if (last_num_packets != matroska->num_packets)
3043 matroska->packets[last_num_packets]->flags = 0;
3044 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3052 av_log(matroska->ctx, AV_LOG_INFO,
3053 "Unknown entry 0x%x in blockgroup data\n", id);
3057 res = ebml_read_skip(matroska);
3061 if (matroska->level_up) {
3062 matroska->level_up--;
3071 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3072 duration, is_keyframe, is_bframe);
3078 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3082 uint64_t cluster_time = 0;
3087 av_log(matroska->ctx, AV_LOG_DEBUG,
3088 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3091 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3094 } else if (matroska->level_up) {
3095 matroska->level_up--;
3100 /* cluster timecode */
3101 case MATROSKA_ID_CLUSTERTIMECODE: {
3103 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3109 /* a group of blocks inside a cluster */
3110 case MATROSKA_ID_BLOCKGROUP:
3111 if ((res = ebml_read_master(matroska, &id)) < 0)
3113 res = matroska_parse_blockgroup(matroska, cluster_time);
3116 case MATROSKA_ID_SIMPLEBLOCK:
3117 pos = url_ftell(matroska->ctx->pb);
3118 res = ebml_read_binary(matroska, &id, &data, &size);
3120 res = matroska_parse_block(matroska, data, size, pos,
3121 cluster_time, AV_NOPTS_VALUE,
3126 av_log(matroska->ctx, AV_LOG_INFO,
3127 "Unknown entry 0x%x in cluster data\n", id);
3131 res = ebml_read_skip(matroska);
3135 if (matroska->level_up) {
3136 matroska->level_up--;
3145 matroska_read_packet (AVFormatContext *s,
3148 MatroskaDemuxContext *matroska = s->priv_data;
3152 /* Read stream until we have a packet queued. */
3153 while (matroska_deliver_packet(matroska, pkt)) {
3155 /* Have we already reached the end? */
3157 return AVERROR(EIO);
3161 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3162 return AVERROR(EIO);
3163 } else if (matroska->level_up) {
3164 matroska->level_up--;
3169 case MATROSKA_ID_CLUSTER:
3170 if ((res = ebml_read_master(matroska, &id)) < 0)
3172 if ((res = matroska_parse_cluster(matroska)) == 0)
3173 res = 1; /* Parsed one cluster, let's get out. */
3178 res = ebml_read_skip(matroska);
3182 if (matroska->level_up) {
3183 matroska->level_up--;
3196 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3199 MatroskaDemuxContext *matroska = s->priv_data;
3200 AVStream *st = s->streams[stream_index];
3203 /* find index entry */
3204 index = av_index_search_timestamp(st, timestamp, flags);
3208 matroska_clear_queue(matroska);
3211 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3212 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3213 matroska->skip_to_stream = st;
3214 matroska->peek_id = 0;
3219 matroska_read_close (AVFormatContext *s)
3221 MatroskaDemuxContext *matroska = s->priv_data;
3224 av_free(matroska->writing_app);
3225 av_free(matroska->muxing_app);
3226 av_free(matroska->index);
3228 matroska_clear_queue(matroska);
3230 for (n = 0; n < matroska->num_tracks; n++) {
3231 MatroskaTrack *track = matroska->tracks[n];
3232 av_free(track->codec_id);
3233 av_free(track->codec_name);
3234 av_free(track->codec_priv);
3235 av_free(track->name);
3237 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3238 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3239 av_free(audiotrack->buf);
3248 AVInputFormat matroska_demuxer = {
3250 "Matroska file format",
3251 sizeof(MatroskaDemuxContext),
3253 matroska_read_header,
3254 matroska_read_packet,
3255 matroska_read_close,