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;
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 {
82 MatroskaAspectRatioMode ar_mode;
83 MatroskaEyeMode eye_mode;
88 typedef struct MatroskaAudioTrack {
93 int internal_samplerate;
97 /* real audio header */
106 } MatroskaAudioTrack;
108 typedef struct MatroskaSubtitleTrack {
111 } MatroskaSubtitleTrack;
113 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
114 sizeof(MatroskaAudioTrack), \
115 sizeof(MatroskaSubtitleTrack)))
117 typedef struct MatroskaLevel {
122 typedef struct MatroskaDemuxIndex {
123 uint64_t pos; /* of the corresponding *cluster*! */
124 uint16_t track; /* reference to 'num' */
125 uint64_t time; /* in nanoseconds */
126 } MatroskaDemuxIndex;
128 typedef struct MatroskaDemuxContext {
129 AVFormatContext *ctx;
133 MatroskaLevel levels[EBML_MAX_DEPTH];
141 /* timescale in the file */
144 /* num_streams is the number of streams that av_new_stream() was called
145 * for ( = that are available to the calling program). */
148 MatroskaTrack *tracks[MAX_STREAMS];
150 /* cache for ID peeking */
153 /* byte position of the segment inside the stream */
154 offset_t segment_start;
156 /* The packet queue. */
160 /* have we already parse metadata/cues/clusters? */
165 /* The index for seeking. */
167 MatroskaDemuxIndex *index;
169 /* What to skip before effectively reading a packet. */
170 int skip_to_keyframe;
171 AVStream *skip_to_stream;
172 } MatroskaDemuxContext;
175 * The first few functions handle EBML file parsing. The rest
176 * is the document interpretation. Matroska really just is a
181 * Return: the amount of levels in the hierarchy that the
182 * current element lies higher than the previous one.
183 * The opposite isn't done - that's auto-done using master
188 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
190 ByteIOContext *pb = matroska->ctx->pb;
191 offset_t pos = url_ftell(pb);
194 while (matroska->num_levels > 0) {
195 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
197 if (pos >= level->start + level->length) {
198 matroska->num_levels--;
209 * Read: an "EBML number", which is defined as a variable-length
210 * array of bytes. The first byte indicates the length by giving a
211 * number of 0-bits followed by a one. The position of the first
212 * "one" bit inside the first byte indicates the length of this
214 * Returns: num. of bytes read. < 0 on error.
218 ebml_read_num (MatroskaDemuxContext *matroska,
222 ByteIOContext *pb = matroska->ctx->pb;
223 int len_mask = 0x80, read = 1, n = 1;
226 /* the first byte tells us the length in bytes - get_byte() can normally
227 * return 0, but since that's not a valid first ebmlID byte, we can
228 * use it safely here to catch EOS. */
229 if (!(total = get_byte(pb))) {
230 /* we might encounter EOS here */
232 offset_t pos = url_ftell(pb);
233 av_log(matroska->ctx, AV_LOG_ERROR,
234 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
237 return AVERROR(EIO); /* EOS or actual I/O error */
240 /* get the length of the EBML number */
241 while (read <= max_size && !(total & len_mask)) {
245 if (read > max_size) {
246 offset_t pos = url_ftell(pb) - 1;
247 av_log(matroska->ctx, AV_LOG_ERROR,
248 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
249 (uint8_t) total, pos, pos);
250 return AVERROR_INVALIDDATA;
253 /* read out length */
256 total = (total << 8) | get_byte(pb);
264 * Read: the element content data ID.
265 * Return: the number of bytes read or < 0 on error.
269 ebml_read_element_id (MatroskaDemuxContext *matroska,
276 /* if we re-call this, use our cached ID */
277 if (matroska->peek_id != 0) {
280 *id = matroska->peek_id;
284 /* read out the "EBML number", include tag in ID */
285 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
287 *id = matroska->peek_id = total | (1 << (read * 7));
291 *level_up = ebml_read_element_level_up(matroska);
297 * Read: element content length.
298 * Return: the number of bytes read or < 0 on error.
302 ebml_read_element_length (MatroskaDemuxContext *matroska,
305 /* clear cache since we're now beyond that data point */
306 matroska->peek_id = 0;
308 /* read out the "EBML number", include tag in ID */
309 return ebml_read_num(matroska, 8, length);
313 * Return: the ID of the next element, or 0 on error.
314 * Level_up contains the amount of levels that this
315 * next element lies higher than the previous one.
319 ebml_peek_id (MatroskaDemuxContext *matroska,
324 if (ebml_read_element_id(matroska, &id, level_up) < 0)
331 * Seek to a given offset.
332 * 0 is success, -1 is failure.
336 ebml_read_seek (MatroskaDemuxContext *matroska,
339 ByteIOContext *pb = matroska->ctx->pb;
341 /* clear ID cache, if any */
342 matroska->peek_id = 0;
344 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
348 * Skip the next element.
349 * 0 is success, -1 is failure.
353 ebml_read_skip (MatroskaDemuxContext *matroska)
355 ByteIOContext *pb = matroska->ctx->pb;
360 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
361 (res = ebml_read_element_length(matroska, &length)) < 0)
364 url_fskip(pb, length);
370 * Read the next element as an unsigned int.
371 * 0 is success, < 0 is failure.
375 ebml_read_uint (MatroskaDemuxContext *matroska,
379 ByteIOContext *pb = matroska->ctx->pb;
380 int n = 0, size, res;
383 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
384 (res = ebml_read_element_length(matroska, &rlength)) < 0)
387 if (size < 1 || size > 8) {
388 offset_t pos = url_ftell(pb);
389 av_log(matroska->ctx, AV_LOG_ERROR,
390 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
392 return AVERROR_INVALIDDATA;
395 /* big-endian ordening; build up number */
398 *num = (*num << 8) | get_byte(pb);
404 * Read the next element as a signed int.
405 * 0 is success, < 0 is failure.
409 ebml_read_sint (MatroskaDemuxContext *matroska,
413 ByteIOContext *pb = matroska->ctx->pb;
414 int size, n = 1, negative = 0, res;
417 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
418 (res = ebml_read_element_length(matroska, &rlength)) < 0)
421 if (size < 1 || size > 8) {
422 offset_t pos = url_ftell(pb);
423 av_log(matroska->ctx, AV_LOG_ERROR,
424 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
426 return AVERROR_INVALIDDATA;
428 if ((*num = get_byte(pb)) & 0x80) {
433 *num = (*num << 8) | get_byte(pb);
437 *num = *num - (1LL << ((8 * size) - 1));
443 * Read the next element as a float.
444 * 0 is success, < 0 is failure.
448 ebml_read_float (MatroskaDemuxContext *matroska,
452 ByteIOContext *pb = matroska->ctx->pb;
456 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
457 (res = ebml_read_element_length(matroska, &rlength)) < 0)
462 *num= av_int2flt(get_be32(pb));
464 *num= av_int2dbl(get_be64(pb));
466 offset_t pos = url_ftell(pb);
467 av_log(matroska->ctx, AV_LOG_ERROR,
468 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
470 return AVERROR_INVALIDDATA;
477 * Read the next element as an ASCII string.
478 * 0 is success, < 0 is failure.
482 ebml_read_ascii (MatroskaDemuxContext *matroska,
486 ByteIOContext *pb = matroska->ctx->pb;
490 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
491 (res = ebml_read_element_length(matroska, &rlength)) < 0)
495 /* ebml strings are usually not 0-terminated, so we allocate one
496 * byte more, read the string and NULL-terminate it ourselves. */
497 if (size < 0 || !(*str = av_malloc(size + 1))) {
498 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
499 return AVERROR(ENOMEM);
501 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
502 offset_t pos = url_ftell(pb);
503 av_log(matroska->ctx, AV_LOG_ERROR,
504 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
513 * Read the next element as a UTF-8 string.
514 * 0 is success, < 0 is failure.
518 ebml_read_utf8 (MatroskaDemuxContext *matroska,
522 return ebml_read_ascii(matroska, id, str);
526 * Read the next element as a date (nanoseconds since 1/1/2000).
527 * 0 is success, < 0 is failure.
531 ebml_read_date (MatroskaDemuxContext *matroska,
535 return ebml_read_sint(matroska, id, date);
539 * Read the next element, but only the header. The contents
540 * are supposed to be sub-elements which can be read separately.
541 * 0 is success, < 0 is failure.
545 ebml_read_master (MatroskaDemuxContext *matroska,
548 ByteIOContext *pb = matroska->ctx->pb;
550 MatroskaLevel *level;
553 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
554 (res = ebml_read_element_length(matroska, &length)) < 0)
557 /* protect... (Heaven forbids that the '>' is true) */
558 if (matroska->num_levels >= EBML_MAX_DEPTH) {
559 av_log(matroska->ctx, AV_LOG_ERROR,
560 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
561 return AVERROR(ENOSYS);
565 level = &matroska->levels[matroska->num_levels++];
566 level->start = url_ftell(pb);
567 level->length = length;
573 * Read the next element as binary data.
574 * 0 is success, < 0 is failure.
578 ebml_read_binary (MatroskaDemuxContext *matroska,
583 ByteIOContext *pb = matroska->ctx->pb;
587 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
588 (res = ebml_read_element_length(matroska, &rlength)) < 0)
592 if (!(*binary = av_malloc(*size))) {
593 av_log(matroska->ctx, AV_LOG_ERROR,
594 "Memory allocation error\n");
595 return AVERROR(ENOMEM);
598 if (get_buffer(pb, *binary, *size) != *size) {
599 offset_t pos = url_ftell(pb);
600 av_log(matroska->ctx, AV_LOG_ERROR,
601 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
609 * Read signed/unsigned "EBML" numbers.
610 * Return: number of bytes processed, < 0 on error.
611 * XXX: use ebml_read_num().
615 matroska_ebmlnum_uint (uint8_t *data,
619 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
623 return AVERROR_INVALIDDATA;
626 while (read <= 8 && !(total & len_mask)) {
631 return AVERROR_INVALIDDATA;
633 if ((total &= (len_mask - 1)) == len_mask - 1)
636 return AVERROR_INVALIDDATA;
640 total = (total << 8) | data[n];
653 * Same as above, but signed.
657 matroska_ebmlnum_sint (uint8_t *data,
664 /* read as unsigned number first */
665 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
668 /* make signed (weird way) */
669 if (unum == (uint64_t)-1)
672 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
678 * Read an EBML header.
679 * 0 is success, < 0 is failure.
683 ebml_read_header (MatroskaDemuxContext *matroska,
688 int level_up, res = 0;
696 if (!(id = ebml_peek_id(matroska, &level_up)) ||
697 level_up != 0 || id != EBML_ID_HEADER) {
698 av_log(matroska->ctx, AV_LOG_ERROR,
699 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
700 return AVERROR_INVALIDDATA;
702 if ((res = ebml_read_master(matroska, &id)) < 0)
706 if (!(id = ebml_peek_id(matroska, &level_up)))
714 /* is our read version uptodate? */
715 case EBML_ID_EBMLREADVERSION: {
718 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
720 if (num > EBML_VERSION) {
721 av_log(matroska->ctx, AV_LOG_ERROR,
722 "EBML version %"PRIu64" (> %d) is not supported\n",
724 return AVERROR_INVALIDDATA;
729 /* we only handle 8 byte lengths at max */
730 case EBML_ID_EBMLMAXSIZELENGTH: {
733 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
735 if (num > sizeof(uint64_t)) {
736 av_log(matroska->ctx, AV_LOG_ERROR,
737 "Integers of size %"PRIu64" (> %zd) not supported\n",
738 num, sizeof(uint64_t));
739 return AVERROR_INVALIDDATA;
744 /* we handle 4 byte IDs at max */
745 case EBML_ID_EBMLMAXIDLENGTH: {
748 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
750 if (num > sizeof(uint32_t)) {
751 av_log(matroska->ctx, AV_LOG_ERROR,
752 "IDs of size %"PRIu64" (> %zu) not supported\n",
753 num, sizeof(uint32_t));
754 return AVERROR_INVALIDDATA;
759 case EBML_ID_DOCTYPE: {
762 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
773 case EBML_ID_DOCTYPEREADVERSION: {
776 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
784 av_log(matroska->ctx, AV_LOG_INFO,
785 "Unknown data type 0x%x in EBML header", id);
789 /* we ignore these two, as they don't tell us anything we
791 case EBML_ID_EBMLVERSION:
792 case EBML_ID_DOCTYPEVERSION:
793 res = ebml_read_skip (matroska);
803 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
808 for (i = 0; i < matroska->num_tracks; i++)
809 if (matroska->tracks[i]->num == num)
817 * Put one packet in an application-supplied AVPacket struct.
818 * Returns 0 on success or -1 on failure.
822 matroska_deliver_packet (MatroskaDemuxContext *matroska,
825 if (matroska->num_packets > 0) {
826 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
827 av_free(matroska->packets[0]);
828 if (matroska->num_packets > 1) {
829 memmove(&matroska->packets[0], &matroska->packets[1],
830 (matroska->num_packets - 1) * sizeof(AVPacket *));
832 av_realloc(matroska->packets, (matroska->num_packets - 1) *
835 av_freep(&matroska->packets);
837 matroska->num_packets--;
845 * Put a packet into our internal queue. Will be delivered to the
846 * user/application during the next get_packet() call.
850 matroska_queue_packet (MatroskaDemuxContext *matroska,
854 av_realloc(matroska->packets, (matroska->num_packets + 1) *
856 matroska->packets[matroska->num_packets] = pkt;
857 matroska->num_packets++;
861 * Free all packets in our internal queue.
864 matroska_clear_queue (MatroskaDemuxContext *matroska)
866 if (matroska->packets) {
868 for (n = 0; n < matroska->num_packets; n++) {
869 av_free_packet(matroska->packets[n]);
870 av_free(matroska->packets[n]);
872 av_free(matroska->packets);
873 matroska->packets = NULL;
874 matroska->num_packets = 0;
884 matroska_probe (AVProbeData *p)
887 int len_mask = 0x80, size = 1, n = 1;
888 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
891 if (AV_RB32(p->buf) != EBML_ID_HEADER)
894 /* length of header */
896 while (size <= 8 && !(total & len_mask)) {
902 total &= (len_mask - 1);
904 total = (total << 8) | p->buf[4 + n++];
906 /* does the probe data contain the whole header? */
907 if (p->buf_size < 4 + size + total)
910 /* the header must contain the document type 'matroska'. For now,
911 * we don't parse the whole header but simply check for the
912 * availability of that array of characters inside the header.
913 * Not fully fool-proof, but good enough. */
914 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
915 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
916 return AVPROBE_SCORE_MAX;
922 * From here on, it's all XML-style DTD stuff... Needs no comments.
926 matroska_parse_info (MatroskaDemuxContext *matroska)
931 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
934 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
937 } else if (matroska->level_up) {
938 matroska->level_up--;
943 /* cluster timecode */
944 case MATROSKA_ID_TIMECODESCALE: {
946 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
948 matroska->time_scale = num;
952 case MATROSKA_ID_DURATION: {
954 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
956 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
960 case MATROSKA_ID_TITLE: {
962 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
964 strncpy(matroska->ctx->title, text,
965 sizeof(matroska->ctx->title)-1);
970 case MATROSKA_ID_WRITINGAPP: {
972 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
974 matroska->writing_app = text;
978 case MATROSKA_ID_MUXINGAPP: {
980 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
982 matroska->muxing_app = text;
986 case MATROSKA_ID_DATEUTC: {
988 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
990 matroska->created = time;
995 av_log(matroska->ctx, AV_LOG_INFO,
996 "Unknown entry 0x%x in info header\n", id);
1000 res = ebml_read_skip(matroska);
1004 if (matroska->level_up) {
1005 matroska->level_up--;
1014 matroska_add_stream (MatroskaDemuxContext *matroska)
1018 MatroskaTrack *track;
1020 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1022 /* Allocate a generic track. As soon as we know its type we'll realloc. */
1023 track = av_mallocz(MAX_TRACK_SIZE);
1024 matroska->num_tracks++;
1025 strcpy(track->language, "eng");
1027 /* start with the master */
1028 if ((res = ebml_read_master(matroska, &id)) < 0)
1031 /* try reading the trackentry headers */
1033 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1036 } else if (matroska->level_up > 0) {
1037 matroska->level_up--;
1042 /* track number (unique stream ID) */
1043 case MATROSKA_ID_TRACKNUMBER: {
1045 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1051 /* track UID (unique identifier) */
1052 case MATROSKA_ID_TRACKUID: {
1054 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1060 /* track type (video, audio, combined, subtitle, etc.) */
1061 case MATROSKA_ID_TRACKTYPE: {
1063 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1065 if (track->type && track->type != num) {
1066 av_log(matroska->ctx, AV_LOG_INFO,
1067 "More than one tracktype in an entry - skip\n");
1072 switch (track->type) {
1073 case MATROSKA_TRACK_TYPE_VIDEO:
1074 case MATROSKA_TRACK_TYPE_AUDIO:
1075 case MATROSKA_TRACK_TYPE_SUBTITLE:
1077 case MATROSKA_TRACK_TYPE_COMPLEX:
1078 case MATROSKA_TRACK_TYPE_LOGO:
1079 case MATROSKA_TRACK_TYPE_CONTROL:
1081 av_log(matroska->ctx, AV_LOG_INFO,
1082 "Unknown or unsupported track type 0x%x\n",
1084 track->type = MATROSKA_TRACK_TYPE_NONE;
1087 matroska->tracks[matroska->num_tracks - 1] = track;
1091 /* tracktype specific stuff for video */
1092 case MATROSKA_ID_TRACKVIDEO: {
1093 MatroskaVideoTrack *videotrack;
1095 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1096 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1097 av_log(matroska->ctx, AV_LOG_INFO,
1098 "video data in non-video track - ignoring\n");
1099 res = AVERROR_INVALIDDATA;
1101 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1103 videotrack = (MatroskaVideoTrack *)track;
1106 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1109 } else if (matroska->level_up > 0) {
1110 matroska->level_up--;
1115 /* fixme, this should be one-up, but I get it here */
1116 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1118 if ((res = ebml_read_uint (matroska, &id,
1121 track->default_duration = num;
1125 /* video framerate */
1126 case MATROSKA_ID_VIDEOFRAMERATE: {
1128 if ((res = ebml_read_float(matroska, &id,
1131 if (!track->default_duration)
1132 track->default_duration = 1000000000/num;
1136 /* width of the size to display the video at */
1137 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1139 if ((res = ebml_read_uint(matroska, &id,
1142 videotrack->display_width = num;
1146 /* height of the size to display the video at */
1147 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1149 if ((res = ebml_read_uint(matroska, &id,
1152 videotrack->display_height = num;
1156 /* width of the video in the file */
1157 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1159 if ((res = ebml_read_uint(matroska, &id,
1162 videotrack->pixel_width = num;
1166 /* height of the video in the file */
1167 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1169 if ((res = ebml_read_uint(matroska, &id,
1172 videotrack->pixel_height = num;
1176 /* whether the video is interlaced */
1177 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1179 if ((res = ebml_read_uint(matroska, &id,
1184 MATROSKA_VIDEOTRACK_INTERLACED;
1187 ~MATROSKA_VIDEOTRACK_INTERLACED;
1191 /* stereo mode (whether the video has two streams,
1192 * where one is for the left eye and the other for
1193 * the right eye, which creates a 3D-like
1195 case MATROSKA_ID_VIDEOSTEREOMODE: {
1197 if ((res = ebml_read_uint(matroska, &id,
1200 if (num != MATROSKA_EYE_MODE_MONO &&
1201 num != MATROSKA_EYE_MODE_LEFT &&
1202 num != MATROSKA_EYE_MODE_RIGHT &&
1203 num != MATROSKA_EYE_MODE_BOTH) {
1204 av_log(matroska->ctx, AV_LOG_INFO,
1205 "Ignoring unknown eye mode 0x%x\n",
1209 videotrack->eye_mode = num;
1213 /* aspect ratio behaviour */
1214 case MATROSKA_ID_VIDEOASPECTRATIO: {
1216 if ((res = ebml_read_uint(matroska, &id,
1219 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1220 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1221 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1222 av_log(matroska->ctx, AV_LOG_INFO,
1223 "Ignoring unknown aspect ratio 0x%x\n",
1227 videotrack->ar_mode = num;
1231 /* colorspace (only matters for raw video)
1233 case MATROSKA_ID_VIDEOCOLORSPACE: {
1235 if ((res = ebml_read_uint(matroska, &id,
1238 videotrack->fourcc = num;
1243 av_log(matroska->ctx, AV_LOG_INFO,
1244 "Unknown video track header entry "
1245 "0x%x - ignoring\n", id);
1249 res = ebml_read_skip(matroska);
1253 if (matroska->level_up) {
1254 matroska->level_up--;
1261 /* tracktype specific stuff for audio */
1262 case MATROSKA_ID_TRACKAUDIO: {
1263 MatroskaAudioTrack *audiotrack;
1265 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1266 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1267 av_log(matroska->ctx, AV_LOG_INFO,
1268 "audio data in non-audio track - ignoring\n");
1269 res = AVERROR_INVALIDDATA;
1271 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1273 audiotrack = (MatroskaAudioTrack *)track;
1274 audiotrack->channels = 1;
1275 audiotrack->samplerate = 8000;
1278 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1281 } else if (matroska->level_up > 0) {
1282 matroska->level_up--;
1288 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1290 if ((res = ebml_read_float(matroska, &id,
1293 audiotrack->internal_samplerate =
1294 audiotrack->samplerate = num;
1298 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1300 if ((res = ebml_read_float(matroska, &id,
1303 audiotrack->samplerate = num;
1308 case MATROSKA_ID_AUDIOBITDEPTH: {
1310 if ((res = ebml_read_uint(matroska, &id,
1313 audiotrack->bitdepth = num;
1318 case MATROSKA_ID_AUDIOCHANNELS: {
1320 if ((res = ebml_read_uint(matroska, &id,
1323 audiotrack->channels = num;
1328 av_log(matroska->ctx, AV_LOG_INFO,
1329 "Unknown audio track header entry "
1330 "0x%x - ignoring\n", id);
1334 res = ebml_read_skip(matroska);
1338 if (matroska->level_up) {
1339 matroska->level_up--;
1346 /* codec identifier */
1347 case MATROSKA_ID_CODECID: {
1349 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1351 track->codec_id = text;
1355 /* codec private data */
1356 case MATROSKA_ID_CODECPRIVATE: {
1359 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1361 track->codec_priv = data;
1362 track->codec_priv_size = size;
1366 /* name of the codec */
1367 case MATROSKA_ID_CODECNAME: {
1369 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1371 track->codec_name = text;
1375 /* name of this track */
1376 case MATROSKA_ID_TRACKNAME: {
1378 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1384 /* language (matters for audio/subtitles, mostly) */
1385 case MATROSKA_ID_TRACKLANGUAGE: {
1387 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1389 if ((end = strchr(text, '-')))
1391 if (strlen(text) == 3)
1392 strcpy(track->language, text);
1397 /* whether this is actually used */
1398 case MATROSKA_ID_TRACKFLAGENABLED: {
1400 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1403 track->flags |= MATROSKA_TRACK_ENABLED;
1405 track->flags &= ~MATROSKA_TRACK_ENABLED;
1409 /* whether it's the default for this track type */
1410 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1412 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1415 track->flags |= MATROSKA_TRACK_DEFAULT;
1417 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1421 /* lacing (like MPEG, where blocks don't end/start on frame
1423 case MATROSKA_ID_TRACKFLAGLACING: {
1425 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1428 track->flags |= MATROSKA_TRACK_LACING;
1430 track->flags &= ~MATROSKA_TRACK_LACING;
1434 /* default length (in time) of one data block in this track */
1435 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1437 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1439 track->default_duration = num;
1443 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1444 if ((res = ebml_read_master(matroska, &id)) < 0)
1448 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1451 } else if (matroska->level_up > 0) {
1452 matroska->level_up--;
1457 case MATROSKA_ID_TRACKCONTENTENCODING: {
1458 int encoding_scope = 1;
1459 if ((res = ebml_read_master(matroska, &id)) < 0)
1463 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1466 } else if (matroska->level_up > 0) {
1467 matroska->level_up--;
1472 case MATROSKA_ID_ENCODINGSCOPE: {
1474 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1476 encoding_scope = num;
1480 case MATROSKA_ID_ENCODINGTYPE: {
1482 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1485 av_log(matroska->ctx, AV_LOG_ERROR,
1486 "Unsupported encoding type");
1490 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1491 if ((res = ebml_read_master(matroska, &id)) < 0)
1495 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1498 } else if (matroska->level_up > 0) {
1499 matroska->level_up--;
1504 case MATROSKA_ID_ENCODINGCOMPALGO: {
1506 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1508 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1510 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1513 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1515 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1516 av_log(matroska->ctx, AV_LOG_ERROR,
1517 "Unsupported compression algo\n");
1518 track->encoding_algo = num;
1522 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1525 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1527 track->encoding_settings = data;
1528 track->encoding_settings_len = size;
1533 av_log(matroska->ctx, AV_LOG_INFO,
1534 "Unknown compression header entry "
1535 "0x%x - ignoring\n", id);
1539 res = ebml_read_skip(matroska);
1543 if (matroska->level_up) {
1544 matroska->level_up--;
1552 av_log(matroska->ctx, AV_LOG_INFO,
1553 "Unknown content encoding header entry "
1554 "0x%x - ignoring\n", id);
1558 res = ebml_read_skip(matroska);
1562 if (matroska->level_up) {
1563 matroska->level_up--;
1568 track->encoding_scope = encoding_scope;
1573 av_log(matroska->ctx, AV_LOG_INFO,
1574 "Unknown content encodings header entry "
1575 "0x%x - ignoring\n", id);
1579 res = ebml_read_skip(matroska);
1583 if (matroska->level_up) {
1584 matroska->level_up--;
1592 av_log(matroska->ctx, AV_LOG_INFO,
1593 "Unknown track header entry 0x%x - ignoring\n", id);
1597 /* we ignore these because they're nothing useful. */
1598 case MATROSKA_ID_CODECINFOURL:
1599 case MATROSKA_ID_CODECDOWNLOADURL:
1600 case MATROSKA_ID_TRACKMINCACHE:
1601 case MATROSKA_ID_TRACKMAXCACHE:
1602 res = ebml_read_skip(matroska);
1606 if (matroska->level_up) {
1607 matroska->level_up--;
1616 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1621 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1624 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1627 } else if (matroska->level_up) {
1628 matroska->level_up--;
1633 /* one track within the "all-tracks" header */
1634 case MATROSKA_ID_TRACKENTRY:
1635 res = matroska_add_stream(matroska);
1639 av_log(matroska->ctx, AV_LOG_INFO,
1640 "Unknown entry 0x%x in track header\n", id);
1644 res = ebml_read_skip(matroska);
1648 if (matroska->level_up) {
1649 matroska->level_up--;
1658 matroska_parse_index (MatroskaDemuxContext *matroska)
1662 MatroskaDemuxIndex idx;
1664 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1667 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1670 } else if (matroska->level_up) {
1671 matroska->level_up--;
1676 /* one single index entry ('point') */
1677 case MATROSKA_ID_POINTENTRY:
1678 if ((res = ebml_read_master(matroska, &id)) < 0)
1681 /* in the end, we hope to fill one entry with a
1682 * timestamp, a file position and a tracknum */
1683 idx.pos = (uint64_t) -1;
1684 idx.time = (uint64_t) -1;
1685 idx.track = (uint16_t) -1;
1688 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1691 } else if (matroska->level_up) {
1692 matroska->level_up--;
1697 /* one single index entry ('point') */
1698 case MATROSKA_ID_CUETIME: {
1700 if ((res = ebml_read_uint(matroska, &id,
1703 idx.time = time * matroska->time_scale;
1707 /* position in the file + track to which it
1709 case MATROSKA_ID_CUETRACKPOSITION:
1710 if ((res = ebml_read_master(matroska, &id)) < 0)
1714 if (!(id = ebml_peek_id (matroska,
1715 &matroska->level_up))) {
1718 } else if (matroska->level_up) {
1719 matroska->level_up--;
1725 case MATROSKA_ID_CUETRACK: {
1727 if ((res = ebml_read_uint(matroska,
1734 /* position in file */
1735 case MATROSKA_ID_CUECLUSTERPOSITION: {
1737 if ((res = ebml_read_uint(matroska,
1740 idx.pos = num+matroska->segment_start;
1745 av_log(matroska->ctx, AV_LOG_INFO,
1746 "Unknown entry 0x%x in "
1747 "CuesTrackPositions\n", id);
1751 res = ebml_read_skip(matroska);
1755 if (matroska->level_up) {
1756 matroska->level_up--;
1764 av_log(matroska->ctx, AV_LOG_INFO,
1765 "Unknown entry 0x%x in cuespoint "
1770 res = ebml_read_skip(matroska);
1774 if (matroska->level_up) {
1775 matroska->level_up--;
1780 /* so let's see if we got what we wanted */
1781 if (idx.pos != (uint64_t) -1 &&
1782 idx.time != (uint64_t) -1 &&
1783 idx.track != (uint16_t) -1) {
1784 if (matroska->num_indexes % 32 == 0) {
1785 /* re-allocate bigger index */
1787 av_realloc(matroska->index,
1788 (matroska->num_indexes + 32) *
1789 sizeof(MatroskaDemuxIndex));
1791 matroska->index[matroska->num_indexes] = idx;
1792 matroska->num_indexes++;
1797 av_log(matroska->ctx, AV_LOG_INFO,
1798 "Unknown entry 0x%x in cues header\n", id);
1802 res = ebml_read_skip(matroska);
1806 if (matroska->level_up) {
1807 matroska->level_up--;
1816 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1822 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1825 } else if (matroska->level_up) {
1826 matroska->level_up--;
1831 /* Hm, this is unsupported... */
1833 av_log(matroska->ctx, AV_LOG_INFO,
1834 "Unknown entry 0x%x in metadata header\n", id);
1838 res = ebml_read_skip(matroska);
1842 if (matroska->level_up) {
1843 matroska->level_up--;
1852 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1857 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1860 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1863 } else if (matroska->level_up) {
1864 matroska->level_up--;
1869 case MATROSKA_ID_SEEKENTRY: {
1870 uint32_t seek_id = 0, peek_id_cache = 0;
1871 uint64_t seek_pos = (uint64_t) -1, t;
1873 if ((res = ebml_read_master(matroska, &id)) < 0)
1877 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1880 } else if (matroska->level_up) {
1881 matroska->level_up--;
1886 case MATROSKA_ID_SEEKID:
1887 res = ebml_read_uint(matroska, &id, &t);
1891 case MATROSKA_ID_SEEKPOSITION:
1892 res = ebml_read_uint(matroska, &id, &seek_pos);
1896 av_log(matroska->ctx, AV_LOG_INFO,
1897 "Unknown seekhead ID 0x%x\n", id);
1901 res = ebml_read_skip(matroska);
1905 if (matroska->level_up) {
1906 matroska->level_up--;
1911 if (!seek_id || seek_pos == (uint64_t) -1) {
1912 av_log(matroska->ctx, AV_LOG_INFO,
1913 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1919 case MATROSKA_ID_CUES:
1920 case MATROSKA_ID_TAGS: {
1921 uint32_t level_up = matroska->level_up;
1922 offset_t before_pos;
1924 MatroskaLevel level;
1926 /* remember the peeked ID and the current position */
1927 peek_id_cache = matroska->peek_id;
1928 before_pos = url_ftell(matroska->ctx->pb);
1931 if ((res = ebml_read_seek(matroska, seek_pos +
1932 matroska->segment_start)) < 0)
1935 /* we don't want to lose our seekhead level, so we add
1936 * a dummy. This is a crude hack. */
1937 if (matroska->num_levels == EBML_MAX_DEPTH) {
1938 av_log(matroska->ctx, AV_LOG_INFO,
1939 "Max EBML element depth (%d) reached, "
1940 "cannot parse further.\n", EBML_MAX_DEPTH);
1941 return AVERROR_UNKNOWN;
1945 level.length = (uint64_t)-1;
1946 matroska->levels[matroska->num_levels] = level;
1947 matroska->num_levels++;
1950 if (!(id = ebml_peek_id (matroska,
1951 &matroska->level_up)))
1953 if (id != seek_id) {
1954 av_log(matroska->ctx, AV_LOG_INFO,
1955 "We looked for ID=0x%x but got "
1956 "ID=0x%x (pos=%"PRIu64")",
1957 seek_id, id, seek_pos +
1958 matroska->segment_start);
1962 /* read master + parse */
1963 if ((res = ebml_read_master(matroska, &id)) < 0)
1966 case MATROSKA_ID_CUES:
1967 if (!(res = matroska_parse_index(matroska)) ||
1968 url_feof(matroska->ctx->pb)) {
1969 matroska->index_parsed = 1;
1973 case MATROSKA_ID_TAGS:
1974 if (!(res = matroska_parse_metadata(matroska)) ||
1975 url_feof(matroska->ctx->pb)) {
1976 matroska->metadata_parsed = 1;
1983 /* remove dummy level */
1984 while (matroska->num_levels) {
1985 matroska->num_levels--;
1987 matroska->levels[matroska->num_levels].length;
1988 if (length == (uint64_t)-1)
1993 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1995 matroska->peek_id = peek_id_cache;
1996 matroska->level_up = level_up;
2001 av_log(matroska->ctx, AV_LOG_INFO,
2002 "Ignoring seekhead entry for ID=0x%x\n",
2011 av_log(matroska->ctx, AV_LOG_INFO,
2012 "Unknown seekhead ID 0x%x\n", id);
2016 res = ebml_read_skip(matroska);
2020 if (matroska->level_up) {
2021 matroska->level_up--;
2030 matroska_parse_attachments(AVFormatContext *s)
2032 MatroskaDemuxContext *matroska = s->priv_data;
2036 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2039 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2042 } else if (matroska->level_up) {
2043 matroska->level_up--;
2048 case MATROSKA_ID_ATTACHEDFILE: {
2051 uint8_t* data = NULL;
2052 int i, data_size = 0;
2055 if ((res = ebml_read_master(matroska, &id)) < 0)
2059 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2062 } else if (matroska->level_up) {
2063 matroska->level_up--;
2068 case MATROSKA_ID_FILENAME:
2069 res = ebml_read_utf8 (matroska, &id, &name);
2072 case MATROSKA_ID_FILEMIMETYPE:
2073 res = ebml_read_ascii (matroska, &id, &mime);
2076 case MATROSKA_ID_FILEDATA:
2077 res = ebml_read_binary(matroska, &id, &data, &data_size);
2081 av_log(matroska->ctx, AV_LOG_INFO,
2082 "Unknown attachedfile ID 0x%x\n", id);
2084 res = ebml_read_skip(matroska);
2088 if (matroska->level_up) {
2089 matroska->level_up--;
2094 if (!(name && mime && data && data_size > 0)) {
2095 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2099 st = av_new_stream(s, matroska->num_streams++);
2101 return AVERROR(ENOMEM);
2102 st->filename = av_strdup(name);
2103 st->codec->codec_id = CODEC_ID_NONE;
2104 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2105 st->codec->extradata = av_malloc(data_size);
2106 if(st->codec->extradata == NULL)
2107 return AVERROR(ENOMEM);
2108 st->codec->extradata_size = data_size;
2109 memcpy(st->codec->extradata, data, data_size);
2111 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2112 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2113 strlen(ff_mkv_mime_tags[i].str))) {
2114 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2119 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2124 av_log(matroska->ctx, AV_LOG_INFO,
2125 "Unknown attachments ID 0x%x\n", id);
2129 res = ebml_read_skip(matroska);
2133 if (matroska->level_up) {
2134 matroska->level_up--;
2143 matroska_parse_chapters(AVFormatContext *s)
2145 MatroskaDemuxContext *matroska = s->priv_data;
2149 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2152 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2155 } else if (matroska->level_up) {
2156 matroska->level_up--;
2161 case MATROSKA_ID_EDITIONENTRY: {
2162 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2165 /* if there is more than one chapter edition
2166 we take only the first one */
2168 ebml_read_skip(matroska);
2172 if ((res = ebml_read_master(matroska, &id)) < 0)
2176 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2179 } else if (matroska->level_up) {
2180 matroska->level_up--;
2185 case MATROSKA_ID_CHAPTERATOM:
2186 if ((res = ebml_read_master(matroska, &id)) < 0)
2190 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2193 } else if (matroska->level_up) {
2194 matroska->level_up--;
2199 case MATROSKA_ID_CHAPTERTIMEEND:
2200 res = ebml_read_uint(matroska, &id, &end);
2203 case MATROSKA_ID_CHAPTERTIMESTART:
2204 res = ebml_read_uint(matroska, &id, &start);
2207 case MATROSKA_ID_CHAPTERDISPLAY:
2208 if ((res = ebml_read_master(matroska, &id)) < 0)
2212 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2215 } else if (matroska->level_up) {
2216 matroska->level_up--;
2221 case MATROSKA_ID_CHAPSTRING:
2222 res = ebml_read_utf8(matroska, &id, &title);
2226 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2228 res = ebml_read_skip(matroska);
2232 if (matroska->level_up) {
2233 matroska->level_up--;
2239 case MATROSKA_ID_CHAPTERUID:
2240 res = ebml_read_uint(matroska, &id, &uid);
2243 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2244 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2246 res = ebml_read_skip(matroska);
2250 if (matroska->level_up) {
2251 matroska->level_up--;
2256 if (start != AV_NOPTS_VALUE && uid != -1) {
2257 start = start * AV_TIME_BASE / 1000000000;
2258 if (end != AV_NOPTS_VALUE)
2259 end = end * AV_TIME_BASE / 1000000000;
2260 res = ff_new_chapter(s, uid, start, end, title);
2266 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2267 case MATROSKA_ID_EDITIONUID:
2268 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2270 res = ebml_read_skip(matroska);
2275 if (matroska->level_up) {
2276 matroska->level_up--;
2284 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2286 res = ebml_read_skip(matroska);
2290 if (matroska->level_up) {
2291 matroska->level_up--;
2299 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
2302 matroska_aac_profile (char *codec_id)
2304 static const char *aac_profiles[] = {
2309 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2310 if (strstr(codec_id, aac_profiles[profile]))
2316 matroska_aac_sri (int samplerate)
2320 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2321 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2327 matroska_read_header (AVFormatContext *s,
2328 AVFormatParameters *ap)
2330 MatroskaDemuxContext *matroska = s->priv_data;
2332 int version, last_level, res = 0;
2337 /* First read the EBML header. */
2339 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2341 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2342 av_log(matroska->ctx, AV_LOG_ERROR,
2343 "Wrong EBML doctype ('%s' != 'matroska').\n",
2344 doctype ? doctype : "(none)");
2347 return AVERROR_NOFMT;
2351 av_log(matroska->ctx, AV_LOG_ERROR,
2352 "Matroska demuxer version 2 too old for file version %d\n",
2354 return AVERROR_NOFMT;
2357 /* The next thing is a segment. */
2359 if (!(id = ebml_peek_id(matroska, &last_level)))
2360 return AVERROR(EIO);
2361 if (id == MATROSKA_ID_SEGMENT)
2365 av_log(matroska->ctx, AV_LOG_INFO,
2366 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2367 MATROSKA_ID_SEGMENT, id);
2368 if ((res = ebml_read_skip(matroska)) < 0)
2372 /* We now have a Matroska segment.
2373 * Seeks are from the beginning of the segment,
2374 * after the segment ID/length. */
2375 if ((res = ebml_read_master(matroska, &id)) < 0)
2377 matroska->segment_start = url_ftell(s->pb);
2379 matroska->time_scale = 1000000;
2380 /* we've found our segment, start reading the different contents in here */
2382 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2385 } else if (matroska->level_up) {
2386 matroska->level_up--;
2392 case MATROSKA_ID_INFO: {
2393 if ((res = ebml_read_master(matroska, &id)) < 0)
2395 res = matroska_parse_info(matroska);
2399 /* track info headers */
2400 case MATROSKA_ID_TRACKS: {
2401 if ((res = ebml_read_master(matroska, &id)) < 0)
2403 res = matroska_parse_tracks(matroska);
2408 case MATROSKA_ID_CUES: {
2409 if (!matroska->index_parsed) {
2410 if ((res = ebml_read_master(matroska, &id)) < 0)
2412 res = matroska_parse_index(matroska);
2414 res = ebml_read_skip(matroska);
2419 case MATROSKA_ID_TAGS: {
2420 if (!matroska->metadata_parsed) {
2421 if ((res = ebml_read_master(matroska, &id)) < 0)
2423 res = matroska_parse_metadata(matroska);
2425 res = ebml_read_skip(matroska);
2429 /* file index (if seekable, seek to Cues/Tags to parse it) */
2430 case MATROSKA_ID_SEEKHEAD: {
2431 if ((res = ebml_read_master(matroska, &id)) < 0)
2433 res = matroska_parse_seekhead(matroska);
2437 case MATROSKA_ID_ATTACHMENTS: {
2438 if ((res = ebml_read_master(matroska, &id)) < 0)
2440 res = matroska_parse_attachments(s);
2444 case MATROSKA_ID_CLUSTER: {
2445 /* Do not read the master - this will be done in the next
2446 * call to matroska_read_packet. */
2451 case MATROSKA_ID_CHAPTERS: {
2452 if ((res = ebml_read_master(matroska, &id)) < 0)
2454 res = matroska_parse_chapters(s);
2459 av_log(matroska->ctx, AV_LOG_INFO,
2460 "Unknown matroska file header ID 0x%x\n", id);
2464 res = ebml_read_skip(matroska);
2468 if (matroska->level_up) {
2469 matroska->level_up--;
2474 /* Have we found a cluster? */
2475 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2477 MatroskaTrack *track;
2480 for (i = 0; i < matroska->num_tracks; i++) {
2481 enum CodecID codec_id = CODEC_ID_NONE;
2482 uint8_t *extradata = NULL;
2483 int extradata_size = 0;
2484 int extradata_offset = 0;
2485 track = matroska->tracks[i];
2486 track->stream_index = -1;
2488 /* Apply some sanity checks. */
2489 if (track->codec_id == NULL)
2492 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2493 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2494 strlen(ff_mkv_codec_tags[j].str))){
2495 codec_id= ff_mkv_codec_tags[j].id;
2500 /* Set the FourCC from the CodecID. */
2501 /* This is the MS compatibility mode which stores a
2502 * BITMAPINFOHEADER in the CodecPrivate. */
2503 if (!strcmp(track->codec_id,
2504 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2505 (track->codec_priv_size >= 40) &&
2506 (track->codec_priv != NULL)) {
2507 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2509 /* Offset of biCompression. Stored in LE. */
2510 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2511 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2515 /* This is the MS compatibility mode which stores a
2516 * WAVEFORMATEX in the CodecPrivate. */
2517 else if (!strcmp(track->codec_id,
2518 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2519 (track->codec_priv_size >= 18) &&
2520 (track->codec_priv != NULL)) {
2523 /* Offset of wFormatTag. Stored in LE. */
2524 tag = AV_RL16(track->codec_priv);
2525 codec_id = codec_get_id(codec_wav_tags, tag);
2529 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2530 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2531 int profile = matroska_aac_profile(track->codec_id);
2532 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2533 extradata = av_malloc(5);
2534 if (extradata == NULL)
2535 return AVERROR(ENOMEM);
2536 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2537 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2538 if (strstr(track->codec_id, "SBR")) {
2539 sri = matroska_aac_sri(audiotrack->samplerate);
2540 extradata[2] = 0x56;
2541 extradata[3] = 0xE5;
2542 extradata[4] = 0x80 | (sri<<3);
2549 else if (codec_id == CODEC_ID_TTA) {
2550 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2552 extradata_size = 30;
2553 extradata = av_mallocz(extradata_size);
2554 if (extradata == NULL)
2555 return AVERROR(ENOMEM);
2556 init_put_byte(&b, extradata, extradata_size, 1,
2557 NULL, NULL, NULL, NULL);
2558 put_buffer(&b, "TTA1", 4);
2560 put_le16(&b, audiotrack->channels);
2561 put_le16(&b, audiotrack->bitdepth);
2562 put_le32(&b, audiotrack->samplerate);
2563 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2566 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2567 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2568 extradata_offset = 26;
2569 track->codec_priv_size -= extradata_offset;
2572 else if (codec_id == CODEC_ID_RA_144) {
2573 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2574 audiotrack->samplerate = 8000;
2575 audiotrack->channels = 1;
2578 else if (codec_id == CODEC_ID_RA_288 ||
2579 codec_id == CODEC_ID_COOK ||
2580 codec_id == CODEC_ID_ATRAC3) {
2581 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2584 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2585 NULL, NULL, NULL, NULL);
2587 audiotrack->coded_framesize = get_be32(&b);
2589 audiotrack->sub_packet_h = get_be16(&b);
2590 audiotrack->frame_size = get_be16(&b);
2591 audiotrack->sub_packet_size = get_be16(&b);
2592 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2593 if (codec_id == CODEC_ID_RA_288) {
2594 audiotrack->block_align = audiotrack->coded_framesize;
2595 track->codec_priv_size = 0;
2597 audiotrack->block_align = audiotrack->sub_packet_size;
2598 extradata_offset = 78;
2599 track->codec_priv_size -= extradata_offset;
2603 if (codec_id == CODEC_ID_NONE) {
2604 av_log(matroska->ctx, AV_LOG_INFO,
2605 "Unknown/unsupported CodecID %s.\n",
2609 track->stream_index = matroska->num_streams;
2611 matroska->num_streams++;
2612 st = av_new_stream(s, track->stream_index);
2614 return AVERROR(ENOMEM);
2615 av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2617 st->codec->codec_id = codec_id;
2619 if (strcmp(track->language, "und"))
2620 strcpy(st->language, track->language);
2622 if (track->flags & MATROSKA_TRACK_DEFAULT)
2623 st->disposition |= AV_DISPOSITION_DEFAULT;
2625 if (track->default_duration)
2626 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2627 track->default_duration, 1000000000, 30000);
2630 st->codec->extradata = extradata;
2631 st->codec->extradata_size = extradata_size;
2632 } else if(track->codec_priv && track->codec_priv_size > 0){
2633 st->codec->extradata = av_malloc(track->codec_priv_size);
2634 if(st->codec->extradata == NULL)
2635 return AVERROR(ENOMEM);
2636 st->codec->extradata_size = track->codec_priv_size;
2637 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2638 track->codec_priv_size);
2641 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2642 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2644 st->codec->codec_type = CODEC_TYPE_VIDEO;
2645 st->codec->codec_tag = videotrack->fourcc;
2646 st->codec->width = videotrack->pixel_width;
2647 st->codec->height = videotrack->pixel_height;
2648 if (videotrack->display_width == 0)
2649 videotrack->display_width= videotrack->pixel_width;
2650 if (videotrack->display_height == 0)
2651 videotrack->display_height= videotrack->pixel_height;
2652 av_reduce(&st->codec->sample_aspect_ratio.num,
2653 &st->codec->sample_aspect_ratio.den,
2654 st->codec->height * videotrack->display_width,
2655 st->codec-> width * videotrack->display_height,
2657 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2658 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2659 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2661 st->codec->codec_type = CODEC_TYPE_AUDIO;
2662 st->codec->sample_rate = audiotrack->samplerate;
2663 st->codec->channels = audiotrack->channels;
2664 st->codec->block_align = audiotrack->block_align;
2665 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2666 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2669 /* What do we do with private data? E.g. for Vorbis. */
2674 if (matroska->index_parsed) {
2675 int i, track, stream;
2676 for (i=0; i<matroska->num_indexes; i++) {
2677 MatroskaDemuxIndex *idx = &matroska->index[i];
2678 track = matroska_find_track_by_num(matroska, idx->track);
2679 if (track < 0) continue;
2680 stream = matroska->tracks[track]->stream_index;
2681 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2682 av_add_index_entry(matroska->ctx->streams[stream],
2683 idx->pos, idx->time/matroska->time_scale,
2684 0, 0, AVINDEX_KEYFRAME);
2692 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2693 int64_t pos, uint64_t cluster_time, uint64_t duration,
2694 int is_keyframe, int is_bframe)
2700 uint8_t *origdata = data;
2702 uint32_t *lace_size = NULL;
2703 int n, flags, laces = 0;
2707 /* first byte(s): tracknum */
2708 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2709 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2716 /* fetch track from num */
2717 track = matroska_find_track_by_num(matroska, num);
2718 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2719 av_log(matroska->ctx, AV_LOG_INFO,
2720 "Invalid stream %d or size %u\n", track, size);
2724 stream_index = matroska->tracks[track]->stream_index;
2725 if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2729 st = matroska->ctx->streams[stream_index];
2730 if (st->discard >= AVDISCARD_ALL) {
2734 if (duration == AV_NOPTS_VALUE)
2735 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2737 /* block_time (relative to cluster time) */
2738 block_time = AV_RB16(data);
2742 if (is_keyframe == -1)
2743 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2745 if (matroska->skip_to_keyframe) {
2746 if (!is_keyframe || st != matroska->skip_to_stream) {
2750 matroska->skip_to_keyframe = 0;
2753 switch ((flags & 0x06) >> 1) {
2754 case 0x0: /* no lacing */
2756 lace_size = av_mallocz(sizeof(int));
2757 lace_size[0] = size;
2760 case 0x1: /* xiph lacing */
2761 case 0x2: /* fixed-size lacing */
2762 case 0x3: /* EBML lacing */
2767 laces = (*data) + 1;
2770 lace_size = av_mallocz(laces * sizeof(int));
2772 switch ((flags & 0x06) >> 1) {
2773 case 0x1: /* xiph lacing */ {
2776 for (n = 0; res == 0 && n < laces - 1; n++) {
2783 lace_size[n] += temp;
2789 total += lace_size[n];
2791 lace_size[n] = size - total;
2795 case 0x2: /* fixed-size lacing */
2796 for (n = 0; n < laces; n++)
2797 lace_size[n] = size / laces;
2800 case 0x3: /* EBML lacing */ {
2802 n = matroska_ebmlnum_uint(data, size, &num);
2804 av_log(matroska->ctx, AV_LOG_INFO,
2805 "EBML block data error\n");
2810 total = lace_size[0] = num;
2811 for (n = 1; res == 0 && n < laces - 1; n++) {
2814 r = matroska_ebmlnum_sint (data, size, &snum);
2816 av_log(matroska->ctx, AV_LOG_INFO,
2817 "EBML block data error\n");
2822 lace_size[n] = lace_size[n - 1] + snum;
2823 total += lace_size[n];
2825 lace_size[n] = size - total;
2833 uint64_t timecode = AV_NOPTS_VALUE;
2835 if (cluster_time != (uint64_t)-1
2836 && (block_time >= 0 || cluster_time >= -block_time))
2837 timecode = cluster_time + block_time;
2839 for (n = 0; n < laces; n++) {
2840 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2841 st->codec->codec_id == CODEC_ID_COOK ||
2842 st->codec->codec_id == CODEC_ID_ATRAC3) {
2843 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2844 int a = st->codec->block_align;
2845 int sps = audiotrack->sub_packet_size;
2846 int cfs = audiotrack->coded_framesize;
2847 int h = audiotrack->sub_packet_h;
2848 int y = audiotrack->sub_packet_cnt;
2849 int w = audiotrack->frame_size;
2852 if (!audiotrack->pkt_cnt) {
2853 if (st->codec->codec_id == CODEC_ID_RA_288)
2854 for (x=0; x<h/2; x++)
2855 memcpy(audiotrack->buf+x*2*w+y*cfs,
2858 for (x=0; x<w/sps; x++)
2859 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2861 if (++audiotrack->sub_packet_cnt >= h) {
2862 audiotrack->sub_packet_cnt = 0;
2863 audiotrack->pkt_cnt = h*w / a;
2866 while (audiotrack->pkt_cnt) {
2867 pkt = av_mallocz(sizeof(AVPacket));
2868 av_new_packet(pkt, a);
2869 memcpy(pkt->data, audiotrack->buf
2870 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2872 pkt->stream_index = stream_index;
2873 matroska_queue_packet(matroska, pkt);
2876 int result, offset = 0, ilen, olen, pkt_size = lace_size[n];
2877 uint8_t *pkt_data = data;
2879 if (matroska->tracks[track]->encoding_scope & 1) {
2880 switch (matroska->tracks[track]->encoding_algo) {
2881 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
2882 offset = matroska->tracks[track]->encoding_settings_len;
2884 case MATROSKA_TRACK_ENCODING_COMP_LZO:
2887 ilen = lace_size[n];
2888 olen = pkt_size *= 3;
2889 pkt_data = av_realloc(pkt_data,
2890 pkt_size+LZO_OUTPUT_PADDING);
2891 result = lzo1x_decode(pkt_data, &olen, data, &ilen);
2892 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
2900 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
2901 z_stream zstream = {0};
2903 if (inflateInit(&zstream) != Z_OK)
2905 zstream.next_in = data;
2906 zstream.avail_in = lace_size[n];
2909 pkt_data = av_realloc(pkt_data, pkt_size);
2910 zstream.avail_out = pkt_size - zstream.total_out;
2911 zstream.next_out = pkt_data + zstream.total_out;
2912 result = inflate(&zstream, Z_NO_FLUSH);
2913 } while (result==Z_OK && pkt_size<10000000);
2914 pkt_size = zstream.total_out;
2915 inflateEnd(&zstream);
2916 if (result != Z_STREAM_END) {
2924 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
2925 bz_stream bzstream = {0};
2927 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
2929 bzstream.next_in = data;
2930 bzstream.avail_in = lace_size[n];
2933 pkt_data = av_realloc(pkt_data, pkt_size);
2934 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
2935 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
2936 result = BZ2_bzDecompress(&bzstream);
2937 } while (result==BZ_OK && pkt_size<10000000);
2938 pkt_size = bzstream.total_out_lo32;
2939 BZ2_bzDecompressEnd(&bzstream);
2940 if (result != BZ_STREAM_END) {
2950 pkt = av_mallocz(sizeof(AVPacket));
2951 /* XXX: prevent data copy... */
2952 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2953 res = AVERROR(ENOMEM);
2958 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2959 memcpy (pkt->data+offset, pkt_data, pkt_size);
2962 pkt->flags = is_keyframe;
2963 pkt->stream_index = stream_index;
2965 pkt->pts = timecode;
2967 pkt->duration = duration;
2969 matroska_queue_packet(matroska, pkt);
2972 if (timecode != AV_NOPTS_VALUE)
2973 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2974 data += lace_size[n];
2984 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2985 uint64_t cluster_time)
2990 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2991 uint64_t duration = AV_NOPTS_VALUE;
2996 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2999 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3002 } else if (matroska->level_up) {
3003 matroska->level_up--;
3008 /* one block inside the group. Note, block parsing is one
3009 * of the harder things, so this code is a bit complicated.
3010 * See http://www.matroska.org/ for documentation. */
3011 case MATROSKA_ID_BLOCK: {
3012 pos = url_ftell(matroska->ctx->pb);
3013 res = ebml_read_binary(matroska, &id, &data, &size);
3017 case MATROSKA_ID_BLOCKDURATION: {
3018 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
3023 case MATROSKA_ID_BLOCKREFERENCE: {
3025 /* We've found a reference, so not even the first frame in
3026 * the lace is a key frame. */
3028 if (last_num_packets != matroska->num_packets)
3029 matroska->packets[last_num_packets]->flags = 0;
3030 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3038 av_log(matroska->ctx, AV_LOG_INFO,
3039 "Unknown entry 0x%x in blockgroup data\n", id);
3043 res = ebml_read_skip(matroska);
3047 if (matroska->level_up) {
3048 matroska->level_up--;
3057 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3058 duration, is_keyframe, is_bframe);
3064 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3068 uint64_t cluster_time = 0;
3073 av_log(matroska->ctx, AV_LOG_DEBUG,
3074 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3077 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3080 } else if (matroska->level_up) {
3081 matroska->level_up--;
3086 /* cluster timecode */
3087 case MATROSKA_ID_CLUSTERTIMECODE: {
3089 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3095 /* a group of blocks inside a cluster */
3096 case MATROSKA_ID_BLOCKGROUP:
3097 if ((res = ebml_read_master(matroska, &id)) < 0)
3099 res = matroska_parse_blockgroup(matroska, cluster_time);
3102 case MATROSKA_ID_SIMPLEBLOCK:
3103 pos = url_ftell(matroska->ctx->pb);
3104 res = ebml_read_binary(matroska, &id, &data, &size);
3106 res = matroska_parse_block(matroska, data, size, pos,
3107 cluster_time, AV_NOPTS_VALUE,
3112 av_log(matroska->ctx, AV_LOG_INFO,
3113 "Unknown entry 0x%x in cluster data\n", id);
3117 res = ebml_read_skip(matroska);
3121 if (matroska->level_up) {
3122 matroska->level_up--;
3131 matroska_read_packet (AVFormatContext *s,
3134 MatroskaDemuxContext *matroska = s->priv_data;
3138 /* Read stream until we have a packet queued. */
3139 while (matroska_deliver_packet(matroska, pkt)) {
3141 /* Have we already reached the end? */
3143 return AVERROR(EIO);
3147 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3148 return AVERROR(EIO);
3149 } else if (matroska->level_up) {
3150 matroska->level_up--;
3155 case MATROSKA_ID_CLUSTER:
3156 if ((res = ebml_read_master(matroska, &id)) < 0)
3158 if ((res = matroska_parse_cluster(matroska)) == 0)
3159 res = 1; /* Parsed one cluster, let's get out. */
3164 res = ebml_read_skip(matroska);
3168 if (matroska->level_up) {
3169 matroska->level_up--;
3182 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3185 MatroskaDemuxContext *matroska = s->priv_data;
3186 AVStream *st = s->streams[stream_index];
3189 /* find index entry */
3190 index = av_index_search_timestamp(st, timestamp, flags);
3194 matroska_clear_queue(matroska);
3197 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3198 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3199 matroska->skip_to_stream = st;
3200 matroska->peek_id = 0;
3205 matroska_read_close (AVFormatContext *s)
3207 MatroskaDemuxContext *matroska = s->priv_data;
3210 av_free(matroska->writing_app);
3211 av_free(matroska->muxing_app);
3212 av_free(matroska->index);
3214 matroska_clear_queue(matroska);
3216 for (n = 0; n < matroska->num_tracks; n++) {
3217 MatroskaTrack *track = matroska->tracks[n];
3218 av_free(track->codec_id);
3219 av_free(track->codec_name);
3220 av_free(track->codec_priv);
3221 av_free(track->name);
3223 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3224 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3225 av_free(audiotrack->buf);
3234 AVInputFormat matroska_demuxer = {
3236 "Matroska file format",
3237 sizeof(MatroskaDemuxContext),
3239 matroska_read_header,
3240 matroska_read_packet,
3241 matroska_read_close,