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_bmp_id and codec_get_wav_id. */
34 #include "intfloat_readwrite.h"
36 /* EBML version supported */
37 #define EBML_VERSION 1
39 /* top-level master-IDs */
40 #define EBML_ID_HEADER 0x1A45DFA3
42 /* IDs in the HEADER master */
43 #define EBML_ID_EBMLVERSION 0x4286
44 #define EBML_ID_EBMLREADVERSION 0x42F7
45 #define EBML_ID_EBMLMAXIDLENGTH 0x42F2
46 #define EBML_ID_EBMLMAXSIZELENGTH 0x42F3
47 #define EBML_ID_DOCTYPE 0x4282
48 #define EBML_ID_DOCTYPEVERSION 0x4287
49 #define EBML_ID_DOCTYPEREADVERSION 0x4285
51 /* general EBML types */
52 #define EBML_ID_VOID 0xEC
55 * Matroska element IDs. max. 32-bit.
58 /* toplevel segment */
59 #define MATROSKA_ID_SEGMENT 0x18538067
61 /* matroska top-level master IDs */
62 #define MATROSKA_ID_INFO 0x1549A966
63 #define MATROSKA_ID_TRACKS 0x1654AE6B
64 #define MATROSKA_ID_CUES 0x1C53BB6B
65 #define MATROSKA_ID_TAGS 0x1254C367
66 #define MATROSKA_ID_SEEKHEAD 0x114D9B74
67 #define MATROSKA_ID_CLUSTER 0x1F43B675
69 /* IDs in the info master */
70 #define MATROSKA_ID_TIMECODESCALE 0x2AD7B1
71 #define MATROSKA_ID_DURATION 0x4489
72 #define MATROSKA_ID_WRITINGAPP 0x5741
73 #define MATROSKA_ID_MUXINGAPP 0x4D80
74 #define MATROSKA_ID_DATEUTC 0x4461
76 /* ID in the tracks master */
77 #define MATROSKA_ID_TRACKENTRY 0xAE
79 /* IDs in the trackentry master */
80 #define MATROSKA_ID_TRACKNUMBER 0xD7
81 #define MATROSKA_ID_TRACKUID 0x73C5
82 #define MATROSKA_ID_TRACKTYPE 0x83
83 #define MATROSKA_ID_TRACKAUDIO 0xE1
84 #define MATROSKA_ID_TRACKVIDEO 0xE0
85 #define MATROSKA_ID_CODECID 0x86
86 #define MATROSKA_ID_CODECPRIVATE 0x63A2
87 #define MATROSKA_ID_CODECNAME 0x258688
88 #define MATROSKA_ID_CODECINFOURL 0x3B4040
89 #define MATROSKA_ID_CODECDOWNLOADURL 0x26B240
90 #define MATROSKA_ID_TRACKNAME 0x536E
91 #define MATROSKA_ID_TRACKLANGUAGE 0x22B59C
92 #define MATROSKA_ID_TRACKFLAGENABLED 0xB9
93 #define MATROSKA_ID_TRACKFLAGDEFAULT 0x88
94 #define MATROSKA_ID_TRACKFLAGLACING 0x9C
95 #define MATROSKA_ID_TRACKMINCACHE 0x6DE7
96 #define MATROSKA_ID_TRACKMAXCACHE 0x6DF8
97 #define MATROSKA_ID_TRACKDEFAULTDURATION 0x23E383
99 /* IDs in the trackvideo master */
100 #define MATROSKA_ID_VIDEOFRAMERATE 0x2383E3
101 #define MATROSKA_ID_VIDEODISPLAYWIDTH 0x54B0
102 #define MATROSKA_ID_VIDEODISPLAYHEIGHT 0x54BA
103 #define MATROSKA_ID_VIDEOPIXELWIDTH 0xB0
104 #define MATROSKA_ID_VIDEOPIXELHEIGHT 0xBA
105 #define MATROSKA_ID_VIDEOFLAGINTERLACED 0x9A
106 #define MATROSKA_ID_VIDEOSTEREOMODE 0x53B9
107 #define MATROSKA_ID_VIDEOASPECTRATIO 0x54B3
108 #define MATROSKA_ID_VIDEOCOLOURSPACE 0x2EB524
110 /* IDs in the trackaudio master */
111 #define MATROSKA_ID_AUDIOSAMPLINGFREQ 0xB5
112 #define MATROSKA_ID_AUDIOBITDEPTH 0x6264
113 #define MATROSKA_ID_AUDIOCHANNELS 0x9F
115 /* ID in the cues master */
116 #define MATROSKA_ID_POINTENTRY 0xBB
118 /* IDs in the pointentry master */
119 #define MATROSKA_ID_CUETIME 0xB3
120 #define MATROSKA_ID_CUETRACKPOSITION 0xB7
122 /* IDs in the cuetrackposition master */
123 #define MATROSKA_ID_CUETRACK 0xF7
124 #define MATROSKA_ID_CUECLUSTERPOSITION 0xF1
126 /* IDs in the tags master */
129 /* IDs in the seekhead master */
130 #define MATROSKA_ID_SEEKENTRY 0x4DBB
132 /* IDs in the seekpoint master */
133 #define MATROSKA_ID_SEEKID 0x53AB
134 #define MATROSKA_ID_SEEKPOSITION 0x53AC
136 /* IDs in the cluster master */
137 #define MATROSKA_ID_CLUSTERTIMECODE 0xE7
138 #define MATROSKA_ID_BLOCKGROUP 0xA0
140 /* IDs in the blockgroup master */
141 #define MATROSKA_ID_BLOCK 0xA1
142 #define MATROSKA_ID_BLOCKDURATION 0x9B
143 #define MATROSKA_ID_BLOCKREFERENCE 0xFB
146 MATROSKA_TRACK_TYPE_VIDEO = 0x1,
147 MATROSKA_TRACK_TYPE_AUDIO = 0x2,
148 MATROSKA_TRACK_TYPE_COMPLEX = 0x3,
149 MATROSKA_TRACK_TYPE_LOGO = 0x10,
150 MATROSKA_TRACK_TYPE_SUBTITLE = 0x11,
151 MATROSKA_TRACK_TYPE_CONTROL = 0x20,
155 MATROSKA_EYE_MODE_MONO = 0x0,
156 MATROSKA_EYE_MODE_RIGHT = 0x1,
157 MATROSKA_EYE_MODE_LEFT = 0x2,
158 MATROSKA_EYE_MODE_BOTH = 0x3,
162 MATROSKA_ASPECT_RATIO_MODE_FREE = 0x0,
163 MATROSKA_ASPECT_RATIO_MODE_KEEP = 0x1,
164 MATROSKA_ASPECT_RATIO_MODE_FIXED = 0x2,
165 } MatroskaAspectRatioMode;
168 * These aren't in any way "matroska-form" things,
169 * it's just something I use in the muxer/demuxer.
173 MATROSKA_TRACK_ENABLED = (1<<0),
174 MATROSKA_TRACK_DEFAULT = (1<<1),
175 MATROSKA_TRACK_LACING = (1<<2),
176 MATROSKA_TRACK_SHIFT = (1<<16)
177 } MatroskaTrackFlags;
180 MATROSKA_VIDEOTRACK_INTERLACED = (MATROSKA_TRACK_SHIFT<<0)
181 } MatroskaVideoTrackFlags;
184 * Matroska Codec IDs. Strings.
187 typedef struct CodecTags{
192 #define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC "V_MS/VFW/FOURCC"
193 #define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
195 static CodecTags codec_tags[]={
196 // {"V_MS/VFW/FOURCC" , CODEC_ID_NONE},
197 {"V_UNCOMPRESSED" , CODEC_ID_RAWVIDEO},
198 {"V_MPEG4/ISO/SP" , CODEC_ID_MPEG4},
199 {"V_MPEG4/ISO/ASP" , CODEC_ID_MPEG4},
200 {"V_MPEG4/ISO/AP" , CODEC_ID_MPEG4},
201 {"V_MPEG4/ISO/AVC" , CODEC_ID_H264},
202 {"V_MPEG4/MS/V3" , CODEC_ID_MSMPEG4V3},
203 {"V_MPEG1" , CODEC_ID_MPEG1VIDEO},
204 {"V_MPEG2" , CODEC_ID_MPEG2VIDEO},
205 {"V_MJPEG" , CODEC_ID_MJPEG},
206 {"V_REAL/RV10" , CODEC_ID_RV10},
207 {"V_REAL/RV20" , CODEC_ID_RV20},
208 {"V_REAL/RV30" , CODEC_ID_RV30},
209 {"V_REAL/RV40" , CODEC_ID_RV40},
210 /* TODO: Real/Quicktime */
212 // {"A_MS/ACM" , CODEC_ID_NONE},
213 {"A_MPEG/L1" , CODEC_ID_MP3},
214 {"A_MPEG/L2" , CODEC_ID_MP3},
215 {"A_MPEG/L3" , CODEC_ID_MP3},
216 {"A_PCM/INT/BIG" , CODEC_ID_PCM_U16BE},
217 {"A_PCM/INT/LIT" , CODEC_ID_PCM_U16LE},
218 // {"A_PCM/FLOAT/IEEE" , CODEC_ID_NONE},
219 {"A_AC3" , CODEC_ID_AC3},
220 {"A_DTS" , CODEC_ID_DTS},
221 {"A_VORBIS" , CODEC_ID_VORBIS},
222 {"A_AAC/MPEG2/" , CODEC_ID_AAC},
223 {"A_AAC/MPEG4/" , CODEC_ID_AAC},
224 {NULL , CODEC_ID_NONE}
225 /* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
228 /* max. depth in the EBML tree structure */
229 #define EBML_MAX_DEPTH 16
231 typedef struct Track {
232 MatroskaTrackType type;
234 /* Unique track number and track ID. stream_index is the index that
235 * the calling app uses for this track. */
246 unsigned char *codec_priv;
249 int64_t default_duration;
250 MatroskaTrackFlags flags;
253 typedef struct MatroskaVideoTrack {
263 MatroskaAspectRatioMode ar_mode;
264 MatroskaEyeMode eye_mode;
267 } MatroskaVideoTrack;
269 typedef struct MatroskaAudioTrack {
276 } MatroskaAudioTrack;
278 typedef struct MatroskaSubtitleTrack {
282 } MatroskaSubtitleTrack;
284 typedef struct MatroskaLevel {
285 uint64_t start, length;
288 typedef struct MatroskaDemuxIndex {
289 uint64_t pos; /* of the corresponding *cluster*! */
290 uint16_t track; /* reference to 'num' */
291 uint64_t time; /* in nanoseconds */
292 } MatroskaDemuxIndex;
294 typedef struct MatroskaDemuxContext {
295 AVFormatContext *ctx;
299 MatroskaLevel levels[EBML_MAX_DEPTH];
307 /* timescale in the file */
310 /* position (time, ns) */
313 /* num_streams is the number of streams that av_new_stream() was called
314 * for ( = that are available to the calling program). */
315 int num_tracks, num_streams;
316 MatroskaTrack *tracks[MAX_STREAMS];
318 /* cache for ID peeking */
321 /* byte position of the segment inside the stream */
322 offset_t segment_start;
324 /* The packet queue. */
328 /* have we already parse metadata/cues/clusters? */
333 /* The index for seeking. */
335 MatroskaDemuxIndex *index;
336 } MatroskaDemuxContext;
339 * The first few functions handle EBML file parsing. The rest
340 * is the document interpretation. Matroska really just is a
345 * Return: the amount of levels in the hierarchy that the
346 * current element lies higher than the previous one.
347 * The opposite isn't done - that's auto-done using master
352 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
354 ByteIOContext *pb = &matroska->ctx->pb;
355 offset_t pos = url_ftell(pb);
358 while (matroska->num_levels > 0) {
359 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
361 if (pos >= level->start + level->length) {
362 matroska->num_levels--;
373 * Read: an "EBML number", which is defined as a variable-length
374 * array of bytes. The first byte indicates the length by giving a
375 * number of 0-bits followed by a one. The position of the first
376 * "one" bit inside the first byte indicates the length of this
378 * Returns: num. of bytes read. < 0 on error.
382 ebml_read_num (MatroskaDemuxContext *matroska,
386 ByteIOContext *pb = &matroska->ctx->pb;
387 int len_mask = 0x80, read = 1, n = 1;
390 /* the first byte tells us the length in bytes - get_byte() can normally
391 * return 0, but since that's not a valid first ebmlID byte, we can
392 * use it safely here to catch EOS. */
393 if (!(total = get_byte(pb))) {
394 /* we might encounter EOS here */
396 offset_t pos = url_ftell(pb);
397 av_log(matroska->ctx, AV_LOG_ERROR,
398 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
401 return AVERROR_IO; /* EOS or actual I/O error */
404 /* get the length of the EBML number */
405 while (read <= max_size && !(total & len_mask)) {
409 if (read > max_size) {
410 offset_t pos = url_ftell(pb) - 1;
411 av_log(matroska->ctx, AV_LOG_ERROR,
412 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
413 (uint8_t) total, pos, pos);
414 return AVERROR_INVALIDDATA;
417 /* read out length */
420 total = (total << 8) | get_byte(pb);
428 * Read: the element content data ID.
429 * Return: the number of bytes read or < 0 on error.
433 ebml_read_element_id (MatroskaDemuxContext *matroska,
440 /* if we re-call this, use our cached ID */
441 if (matroska->peek_id != 0) {
444 *id = matroska->peek_id;
448 /* read out the "EBML number", include tag in ID */
449 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
451 *id = matroska->peek_id = total | (1 << (read * 7));
455 *level_up = ebml_read_element_level_up(matroska);
461 * Read: element content length.
462 * Return: the number of bytes read or < 0 on error.
466 ebml_read_element_length (MatroskaDemuxContext *matroska,
469 /* clear cache since we're now beyond that data point */
470 matroska->peek_id = 0;
472 /* read out the "EBML number", include tag in ID */
473 return ebml_read_num(matroska, 8, length);
477 * Return: the ID of the next element, or 0 on error.
478 * Level_up contains the amount of levels that this
479 * next element lies higher than the previous one.
483 ebml_peek_id (MatroskaDemuxContext *matroska,
488 assert(level_up != NULL);
490 if (ebml_read_element_id(matroska, &id, level_up) < 0)
497 * Seek to a given offset.
498 * 0 is success, -1 is failure.
502 ebml_read_seek (MatroskaDemuxContext *matroska,
505 ByteIOContext *pb = &matroska->ctx->pb;
507 /* clear ID cache, if any */
508 matroska->peek_id = 0;
510 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
514 * Skip the next element.
515 * 0 is success, -1 is failure.
519 ebml_read_skip (MatroskaDemuxContext *matroska)
521 ByteIOContext *pb = &matroska->ctx->pb;
526 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
527 (res = ebml_read_element_length(matroska, &length)) < 0)
530 url_fskip(pb, length);
536 * Read the next element as an unsigned int.
537 * 0 is success, < 0 is failure.
541 ebml_read_uint (MatroskaDemuxContext *matroska,
545 ByteIOContext *pb = &matroska->ctx->pb;
546 int n = 0, size, res;
549 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
550 (res = ebml_read_element_length(matroska, &rlength)) < 0)
553 if (size < 1 || size > 8) {
554 offset_t pos = url_ftell(pb);
555 av_log(matroska->ctx, AV_LOG_ERROR,
556 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
558 return AVERROR_INVALIDDATA;
561 /* big-endian ordening; build up number */
564 *num = (*num << 8) | get_byte(pb);
570 * Read the next element as a signed int.
571 * 0 is success, < 0 is failure.
575 ebml_read_sint (MatroskaDemuxContext *matroska,
579 ByteIOContext *pb = &matroska->ctx->pb;
580 int size, n = 1, negative = 0, res;
583 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
584 (res = ebml_read_element_length(matroska, &rlength)) < 0)
587 if (size < 1 || size > 8) {
588 offset_t pos = url_ftell(pb);
589 av_log(matroska->ctx, AV_LOG_ERROR,
590 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
592 return AVERROR_INVALIDDATA;
594 if ((*num = get_byte(pb)) & 0x80) {
600 *num = (*num << 8) | get_byte(pb);
604 *num = *num - (1LL << ((8 * size) - 1));
610 * Read the next element as a float.
611 * 0 is success, < 0 is failure.
615 ebml_read_float (MatroskaDemuxContext *matroska,
619 ByteIOContext *pb = &matroska->ctx->pb;
623 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
624 (res = ebml_read_element_length(matroska, &rlength)) < 0)
629 *num= av_int2flt(get_be32(pb));
631 *num= av_int2dbl(get_be64(pb));
633 av_log(matroska->ctx, AV_LOG_ERROR,
634 "FIXME! 10-byte floats unimplemented\n");
635 return AVERROR_UNKNOWN;
637 offset_t pos = url_ftell(pb);
638 av_log(matroska->ctx, AV_LOG_ERROR,
639 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
641 return AVERROR_INVALIDDATA;
648 * Read the next element as an ASCII string.
649 * 0 is success, < 0 is failure.
653 ebml_read_ascii (MatroskaDemuxContext *matroska,
657 ByteIOContext *pb = &matroska->ctx->pb;
661 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
662 (res = ebml_read_element_length(matroska, &rlength)) < 0)
666 /* ebml strings are usually not 0-terminated, so we allocate one
667 * byte more, read the string and NULL-terminate it ourselves. */
668 if (size < 0 || !(*str = av_malloc(size + 1))) {
669 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
670 return AVERROR_NOMEM;
672 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
673 offset_t pos = url_ftell(pb);
674 av_log(matroska->ctx, AV_LOG_ERROR,
675 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
684 * Read the next element as a UTF-8 string.
685 * 0 is success, < 0 is failure.
689 ebml_read_utf8 (MatroskaDemuxContext *matroska,
693 return ebml_read_ascii(matroska, id, str);
697 * Read the next element as a date (nanoseconds since 1/1/2000).
698 * 0 is success, < 0 is failure.
702 ebml_read_date (MatroskaDemuxContext *matroska,
706 return ebml_read_sint(matroska, id, date);
710 * Read the next element, but only the header. The contents
711 * are supposed to be sub-elements which can be read separately.
712 * 0 is success, < 0 is failure.
716 ebml_read_master (MatroskaDemuxContext *matroska,
719 ByteIOContext *pb = &matroska->ctx->pb;
721 MatroskaLevel *level;
724 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
725 (res = ebml_read_element_length(matroska, &length)) < 0)
728 /* protect... (Heaven forbids that the '>' is true) */
729 if (matroska->num_levels >= EBML_MAX_DEPTH) {
730 av_log(matroska->ctx, AV_LOG_ERROR,
731 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
732 return AVERROR_NOTSUPP;
736 level = &matroska->levels[matroska->num_levels++];
737 level->start = url_ftell(pb);
738 level->length = length;
744 * Read the next element as binary data.
745 * 0 is success, < 0 is failure.
749 ebml_read_binary (MatroskaDemuxContext *matroska,
754 ByteIOContext *pb = &matroska->ctx->pb;
758 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
759 (res = ebml_read_element_length(matroska, &rlength)) < 0)
763 if (!(*binary = av_malloc(*size))) {
764 av_log(matroska->ctx, AV_LOG_ERROR,
765 "Memory allocation error\n");
766 return AVERROR_NOMEM;
769 if (get_buffer(pb, *binary, *size) != *size) {
770 offset_t pos = url_ftell(pb);
771 av_log(matroska->ctx, AV_LOG_ERROR,
772 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
780 * Read signed/unsigned "EBML" numbers.
781 * Return: number of bytes processed, < 0 on error.
782 * XXX: use ebml_read_num().
786 matroska_ebmlnum_uint (uint8_t *data,
790 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
794 return AVERROR_INVALIDDATA;
797 while (read <= 8 && !(total & len_mask)) {
802 return AVERROR_INVALIDDATA;
804 if ((total &= (len_mask - 1)) == len_mask - 1)
807 return AVERROR_INVALIDDATA;
811 total = (total << 8) | data[n];
824 * Same as above, but signed.
828 matroska_ebmlnum_sint (uint8_t *data,
835 /* read as unsigned number first */
836 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
839 /* make signed (weird way) */
840 if (unum == (uint64_t)-1)
843 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
849 * Read an EBML header.
850 * 0 is success, < 0 is failure.
854 ebml_read_header (MatroskaDemuxContext *matroska,
859 int level_up, res = 0;
867 if (!(id = ebml_peek_id(matroska, &level_up)) ||
868 level_up != 0 || id != EBML_ID_HEADER) {
869 av_log(matroska->ctx, AV_LOG_ERROR,
870 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
871 return AVERROR_INVALIDDATA;
873 if ((res = ebml_read_master(matroska, &id)) < 0)
877 if (!(id = ebml_peek_id(matroska, &level_up)))
885 /* is our read version uptodate? */
886 case EBML_ID_EBMLREADVERSION: {
889 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
891 if (num > EBML_VERSION) {
892 av_log(matroska->ctx, AV_LOG_ERROR,
893 "EBML version %"PRIu64" (> %d) is not supported\n",
895 return AVERROR_INVALIDDATA;
900 /* we only handle 8 byte lengths at max */
901 case EBML_ID_EBMLMAXSIZELENGTH: {
904 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
906 if (num > sizeof(uint64_t)) {
907 av_log(matroska->ctx, AV_LOG_ERROR,
908 "Integers of size %"PRIu64" (> %zd) not supported\n",
909 num, sizeof(uint64_t));
910 return AVERROR_INVALIDDATA;
915 /* we handle 4 byte IDs at max */
916 case EBML_ID_EBMLMAXIDLENGTH: {
919 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
921 if (num > sizeof(uint32_t)) {
922 av_log(matroska->ctx, AV_LOG_ERROR,
923 "IDs of size %"PRIu64" (> %zu) not supported\n",
924 num, sizeof(uint32_t));
925 return AVERROR_INVALIDDATA;
930 case EBML_ID_DOCTYPE: {
933 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
944 case EBML_ID_DOCTYPEREADVERSION: {
947 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
955 av_log(matroska->ctx, AV_LOG_INFO,
956 "Unknown data type 0x%x in EBML header", id);
960 /* we ignore these two, as they don't tell us anything we
962 case EBML_ID_EBMLVERSION:
963 case EBML_ID_DOCTYPEVERSION:
964 res = ebml_read_skip (matroska);
973 * Put one packet in an application-supplied AVPacket struct.
974 * Returns 0 on success or -1 on failure.
978 matroska_deliver_packet (MatroskaDemuxContext *matroska,
981 if (matroska->num_packets > 0) {
982 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
983 av_free(matroska->packets[0]);
984 if (matroska->num_packets > 1) {
985 memmove(&matroska->packets[0], &matroska->packets[1],
986 (matroska->num_packets - 1) * sizeof(AVPacket *));
988 av_realloc(matroska->packets, (matroska->num_packets - 1) *
991 av_free(matroska->packets);
992 matroska->packets = NULL;
994 matroska->num_packets--;
1002 * Put a packet into our internal queue. Will be delivered to the
1003 * user/application during the next get_packet() call.
1007 matroska_queue_packet (MatroskaDemuxContext *matroska,
1011 av_realloc(matroska->packets, (matroska->num_packets + 1) *
1012 sizeof(AVPacket *));
1013 matroska->packets[matroska->num_packets] = pkt;
1014 matroska->num_packets++;
1022 matroska_probe (AVProbeData *p)
1025 int len_mask = 0x80, size = 1, n = 1;
1026 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
1028 if (p->buf_size < 5)
1032 if ((p->buf[0] << 24 | p->buf[1] << 16 |
1033 p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
1036 /* length of header */
1038 while (size <= 8 && !(total & len_mask)) {
1044 total &= (len_mask - 1);
1046 total = (total << 8) | p->buf[4 + n++];
1048 /* does the probe data contain the whole header? */
1049 if (p->buf_size < 4 + size + total)
1052 /* the header must contain the document type 'matroska'. For now,
1053 * we don't parse the whole header but simply check for the
1054 * availability of that array of characters inside the header.
1055 * Not fully fool-proof, but good enough. */
1056 for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
1057 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
1058 return AVPROBE_SCORE_MAX;
1064 * From here on, it's all XML-style DTD stuff... Needs no comments.
1068 matroska_parse_info (MatroskaDemuxContext *matroska)
1073 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
1076 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1079 } else if (matroska->level_up) {
1080 matroska->level_up--;
1085 /* cluster timecode */
1086 case MATROSKA_ID_TIMECODESCALE: {
1088 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1090 matroska->time_scale = num;
1094 case MATROSKA_ID_DURATION: {
1096 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1098 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1102 case MATROSKA_ID_WRITINGAPP: {
1104 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1106 matroska->writing_app = text;
1110 case MATROSKA_ID_MUXINGAPP: {
1112 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1114 matroska->muxing_app = text;
1118 case MATROSKA_ID_DATEUTC: {
1120 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
1122 matroska->created = time;
1127 av_log(matroska->ctx, AV_LOG_INFO,
1128 "Unknown entry 0x%x in info header\n", id);
1132 res = ebml_read_skip(matroska);
1136 if (matroska->level_up) {
1137 matroska->level_up--;
1146 matroska_add_stream (MatroskaDemuxContext *matroska)
1150 MatroskaTrack *track;
1152 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1154 /* Allocate a generic track. As soon as we know its type we'll realloc. */
1155 track = av_mallocz(sizeof(MatroskaTrack));
1156 matroska->num_tracks++;
1158 /* start with the master */
1159 if ((res = ebml_read_master(matroska, &id)) < 0)
1162 /* try reading the trackentry headers */
1164 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1167 } else if (matroska->level_up > 0) {
1168 matroska->level_up--;
1173 /* track number (unique stream ID) */
1174 case MATROSKA_ID_TRACKNUMBER: {
1176 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1182 /* track UID (unique identifier) */
1183 case MATROSKA_ID_TRACKUID: {
1185 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1191 /* track type (video, audio, combined, subtitle, etc.) */
1192 case MATROSKA_ID_TRACKTYPE: {
1194 if (track->type != 0) {
1195 av_log(matroska->ctx, AV_LOG_INFO,
1196 "More than one tracktype in an entry - skip\n");
1199 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1203 /* ok, so we're actually going to reallocate this thing */
1204 switch (track->type) {
1205 case MATROSKA_TRACK_TYPE_VIDEO:
1206 track = (MatroskaTrack *)
1207 av_realloc(track, sizeof(MatroskaVideoTrack));
1209 case MATROSKA_TRACK_TYPE_AUDIO:
1210 track = (MatroskaTrack *)
1211 av_realloc(track, sizeof(MatroskaAudioTrack));
1212 ((MatroskaAudioTrack *)track)->channels = 1;
1213 ((MatroskaAudioTrack *)track)->samplerate = 8000;
1215 case MATROSKA_TRACK_TYPE_SUBTITLE:
1216 track = (MatroskaTrack *)
1217 av_realloc(track, sizeof(MatroskaSubtitleTrack));
1219 case MATROSKA_TRACK_TYPE_COMPLEX:
1220 case MATROSKA_TRACK_TYPE_LOGO:
1221 case MATROSKA_TRACK_TYPE_CONTROL:
1223 av_log(matroska->ctx, AV_LOG_INFO,
1224 "Unknown or unsupported track type 0x%x\n",
1229 matroska->tracks[matroska->num_tracks - 1] = track;
1233 /* tracktype specific stuff for video */
1234 case MATROSKA_ID_TRACKVIDEO: {
1235 MatroskaVideoTrack *videotrack;
1236 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1237 av_log(matroska->ctx, AV_LOG_INFO,
1238 "video data in non-video track - ignoring\n");
1239 res = AVERROR_INVALIDDATA;
1241 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1243 videotrack = (MatroskaVideoTrack *)track;
1246 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1249 } else if (matroska->level_up > 0) {
1250 matroska->level_up--;
1255 /* fixme, this should be one-up, but I get it here */
1256 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1258 if ((res = ebml_read_uint (matroska, &id,
1261 track->default_duration = num;
1265 /* video framerate */
1266 case MATROSKA_ID_VIDEOFRAMERATE: {
1268 if ((res = ebml_read_float(matroska, &id,
1271 track->default_duration = 1000000000 * (1. / num);
1275 /* width of the size to display the video at */
1276 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1278 if ((res = ebml_read_uint(matroska, &id,
1281 videotrack->display_width = num;
1285 /* height of the size to display the video at */
1286 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1288 if ((res = ebml_read_uint(matroska, &id,
1291 videotrack->display_height = num;
1295 /* width of the video in the file */
1296 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1298 if ((res = ebml_read_uint(matroska, &id,
1301 videotrack->pixel_width = num;
1305 /* height of the video in the file */
1306 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1308 if ((res = ebml_read_uint(matroska, &id,
1311 videotrack->pixel_height = num;
1315 /* whether the video is interlaced */
1316 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1318 if ((res = ebml_read_uint(matroska, &id,
1323 MATROSKA_VIDEOTRACK_INTERLACED;
1326 ~MATROSKA_VIDEOTRACK_INTERLACED;
1330 /* stereo mode (whether the video has two streams,
1331 * where one is for the left eye and the other for
1332 * the right eye, which creates a 3D-like
1334 case MATROSKA_ID_VIDEOSTEREOMODE: {
1336 if ((res = ebml_read_uint(matroska, &id,
1339 if (num != MATROSKA_EYE_MODE_MONO &&
1340 num != MATROSKA_EYE_MODE_LEFT &&
1341 num != MATROSKA_EYE_MODE_RIGHT &&
1342 num != MATROSKA_EYE_MODE_BOTH) {
1343 av_log(matroska->ctx, AV_LOG_INFO,
1344 "Ignoring unknown eye mode 0x%x\n",
1348 videotrack->eye_mode = num;
1352 /* aspect ratio behaviour */
1353 case MATROSKA_ID_VIDEOASPECTRATIO: {
1355 if ((res = ebml_read_uint(matroska, &id,
1358 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1359 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1360 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1361 av_log(matroska->ctx, AV_LOG_INFO,
1362 "Ignoring unknown aspect ratio 0x%x\n",
1366 videotrack->ar_mode = num;
1370 /* colourspace (only matters for raw video)
1372 case MATROSKA_ID_VIDEOCOLOURSPACE: {
1374 if ((res = ebml_read_uint(matroska, &id,
1377 videotrack->fourcc = num;
1382 av_log(matroska->ctx, AV_LOG_INFO,
1383 "Unknown video track header entry "
1384 "0x%x - ignoring\n", id);
1388 res = ebml_read_skip(matroska);
1392 if (matroska->level_up) {
1393 matroska->level_up--;
1400 /* tracktype specific stuff for audio */
1401 case MATROSKA_ID_TRACKAUDIO: {
1402 MatroskaAudioTrack *audiotrack;
1403 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1404 av_log(matroska->ctx, AV_LOG_INFO,
1405 "audio data in non-audio track - ignoring\n");
1406 res = AVERROR_INVALIDDATA;
1408 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1410 audiotrack = (MatroskaAudioTrack *)track;
1413 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1416 } else if (matroska->level_up > 0) {
1417 matroska->level_up--;
1423 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1425 if ((res = ebml_read_float(matroska, &id,
1428 audiotrack->samplerate = num;
1433 case MATROSKA_ID_AUDIOBITDEPTH: {
1435 if ((res = ebml_read_uint(matroska, &id,
1438 audiotrack->bitdepth = num;
1443 case MATROSKA_ID_AUDIOCHANNELS: {
1445 if ((res = ebml_read_uint(matroska, &id,
1448 audiotrack->channels = num;
1453 av_log(matroska->ctx, AV_LOG_INFO,
1454 "Unknown audio track header entry "
1455 "0x%x - ignoring\n", id);
1459 res = ebml_read_skip(matroska);
1463 if (matroska->level_up) {
1464 matroska->level_up--;
1471 /* codec identifier */
1472 case MATROSKA_ID_CODECID: {
1474 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1476 track->codec_id = text;
1480 /* codec private data */
1481 case MATROSKA_ID_CODECPRIVATE: {
1484 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1486 track->codec_priv = data;
1487 track->codec_priv_size = size;
1491 /* name of the codec */
1492 case MATROSKA_ID_CODECNAME: {
1494 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1496 track->codec_name = text;
1500 /* name of this track */
1501 case MATROSKA_ID_TRACKNAME: {
1503 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1509 /* language (matters for audio/subtitles, mostly) */
1510 case MATROSKA_ID_TRACKLANGUAGE: {
1512 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1514 track->language = text;
1518 /* whether this is actually used */
1519 case MATROSKA_ID_TRACKFLAGENABLED: {
1521 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1524 track->flags |= MATROSKA_TRACK_ENABLED;
1526 track->flags &= ~MATROSKA_TRACK_ENABLED;
1530 /* whether it's the default for this track type */
1531 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1533 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1536 track->flags |= MATROSKA_TRACK_DEFAULT;
1538 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1542 /* lacing (like MPEG, where blocks don't end/start on frame
1544 case MATROSKA_ID_TRACKFLAGLACING: {
1546 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1549 track->flags |= MATROSKA_TRACK_LACING;
1551 track->flags &= ~MATROSKA_TRACK_LACING;
1555 /* default length (in time) of one data block in this track */
1556 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1558 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1560 track->default_duration = num;
1565 av_log(matroska->ctx, AV_LOG_INFO,
1566 "Unknown track header entry 0x%x - ignoring\n", id);
1570 /* we ignore these because they're nothing useful. */
1571 case MATROSKA_ID_CODECINFOURL:
1572 case MATROSKA_ID_CODECDOWNLOADURL:
1573 case MATROSKA_ID_TRACKMINCACHE:
1574 case MATROSKA_ID_TRACKMAXCACHE:
1575 res = ebml_read_skip(matroska);
1579 if (matroska->level_up) {
1580 matroska->level_up--;
1589 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1594 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1597 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1600 } else if (matroska->level_up) {
1601 matroska->level_up--;
1606 /* one track within the "all-tracks" header */
1607 case MATROSKA_ID_TRACKENTRY:
1608 res = matroska_add_stream(matroska);
1612 av_log(matroska->ctx, AV_LOG_INFO,
1613 "Unknown entry 0x%x in track header\n", id);
1617 res = ebml_read_skip(matroska);
1621 if (matroska->level_up) {
1622 matroska->level_up--;
1631 matroska_parse_index (MatroskaDemuxContext *matroska)
1635 MatroskaDemuxIndex idx;
1637 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1640 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1643 } else if (matroska->level_up) {
1644 matroska->level_up--;
1649 /* one single index entry ('point') */
1650 case MATROSKA_ID_POINTENTRY:
1651 if ((res = ebml_read_master(matroska, &id)) < 0)
1654 /* in the end, we hope to fill one entry with a
1655 * timestamp, a file position and a tracknum */
1656 idx.pos = (uint64_t) -1;
1657 idx.time = (uint64_t) -1;
1658 idx.track = (uint16_t) -1;
1661 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1664 } else if (matroska->level_up) {
1665 matroska->level_up--;
1670 /* one single index entry ('point') */
1671 case MATROSKA_ID_CUETIME: {
1673 if ((res = ebml_read_uint(matroska, &id,
1676 idx.time = time * matroska->time_scale;
1680 /* position in the file + track to which it
1682 case MATROSKA_ID_CUETRACKPOSITION:
1683 if ((res = ebml_read_master(matroska, &id)) < 0)
1687 if (!(id = ebml_peek_id (matroska,
1688 &matroska->level_up))) {
1691 } else if (matroska->level_up) {
1692 matroska->level_up--;
1698 case MATROSKA_ID_CUETRACK: {
1700 if ((res = ebml_read_uint(matroska,
1707 /* position in file */
1708 case MATROSKA_ID_CUECLUSTERPOSITION: {
1710 if ((res = ebml_read_uint(matroska,
1718 av_log(matroska->ctx, AV_LOG_INFO,
1719 "Unknown entry 0x%x in "
1720 "CuesTrackPositions\n", id);
1724 res = ebml_read_skip(matroska);
1728 if (matroska->level_up) {
1729 matroska->level_up--;
1737 av_log(matroska->ctx, AV_LOG_INFO,
1738 "Unknown entry 0x%x in cuespoint "
1743 res = ebml_read_skip(matroska);
1747 if (matroska->level_up) {
1748 matroska->level_up--;
1753 /* so let's see if we got what we wanted */
1754 if (idx.pos != (uint64_t) -1 &&
1755 idx.time != (uint64_t) -1 &&
1756 idx.track != (uint16_t) -1) {
1757 if (matroska->num_indexes % 32 == 0) {
1758 /* re-allocate bigger index */
1760 av_realloc(matroska->index,
1761 (matroska->num_indexes + 32) *
1762 sizeof(MatroskaDemuxIndex));
1764 matroska->index[matroska->num_indexes] = idx;
1765 matroska->num_indexes++;
1770 av_log(matroska->ctx, AV_LOG_INFO,
1771 "Unknown entry 0x%x in cues header\n", id);
1775 res = ebml_read_skip(matroska);
1779 if (matroska->level_up) {
1780 matroska->level_up--;
1789 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1795 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1798 } else if (matroska->level_up) {
1799 matroska->level_up--;
1804 /* Hm, this is unsupported... */
1806 av_log(matroska->ctx, AV_LOG_INFO,
1807 "Unknown entry 0x%x in metadata header\n", id);
1811 res = ebml_read_skip(matroska);
1815 if (matroska->level_up) {
1816 matroska->level_up--;
1825 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1830 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1833 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1836 } else if (matroska->level_up) {
1837 matroska->level_up--;
1842 case MATROSKA_ID_SEEKENTRY: {
1843 uint32_t seek_id = 0, peek_id_cache = 0;
1844 uint64_t seek_pos = (uint64_t) -1, t;
1846 if ((res = ebml_read_master(matroska, &id)) < 0)
1850 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1853 } else if (matroska->level_up) {
1854 matroska->level_up--;
1859 case MATROSKA_ID_SEEKID:
1860 res = ebml_read_uint(matroska, &id, &t);
1864 case MATROSKA_ID_SEEKPOSITION:
1865 res = ebml_read_uint(matroska, &id, &seek_pos);
1869 av_log(matroska->ctx, AV_LOG_INFO,
1870 "Unknown seekhead ID 0x%x\n", id);
1874 res = ebml_read_skip(matroska);
1878 if (matroska->level_up) {
1879 matroska->level_up--;
1884 if (!seek_id || seek_pos == (uint64_t) -1) {
1885 av_log(matroska->ctx, AV_LOG_INFO,
1886 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1892 case MATROSKA_ID_CUES:
1893 case MATROSKA_ID_TAGS: {
1894 uint32_t level_up = matroska->level_up;
1895 offset_t before_pos;
1897 MatroskaLevel level;
1899 /* remember the peeked ID and the current position */
1900 peek_id_cache = matroska->peek_id;
1901 before_pos = url_ftell(&matroska->ctx->pb);
1904 if ((res = ebml_read_seek(matroska, seek_pos +
1905 matroska->segment_start)) < 0)
1908 /* we don't want to lose our seekhead level, so we add
1909 * a dummy. This is a crude hack. */
1910 if (matroska->num_levels == EBML_MAX_DEPTH) {
1911 av_log(matroska->ctx, AV_LOG_INFO,
1912 "Max EBML element depth (%d) reached, "
1913 "cannot parse further.\n", EBML_MAX_DEPTH);
1914 return AVERROR_UNKNOWN;
1918 level.length = (uint64_t)-1;
1919 matroska->levels[matroska->num_levels] = level;
1920 matroska->num_levels++;
1923 if (!(id = ebml_peek_id (matroska,
1924 &matroska->level_up)))
1926 if (id != seek_id) {
1927 av_log(matroska->ctx, AV_LOG_INFO,
1928 "We looked for ID=0x%x but got "
1929 "ID=0x%x (pos=%"PRIu64")",
1930 seek_id, id, seek_pos +
1931 matroska->segment_start);
1935 /* read master + parse */
1936 if ((res = ebml_read_master(matroska, &id)) < 0)
1939 case MATROSKA_ID_CUES:
1940 if (!(res = matroska_parse_index(matroska)) ||
1941 url_feof(&matroska->ctx->pb)) {
1942 matroska->index_parsed = 1;
1946 case MATROSKA_ID_TAGS:
1947 if (!(res = matroska_parse_metadata(matroska)) ||
1948 url_feof(&matroska->ctx->pb)) {
1949 matroska->metadata_parsed = 1;
1958 /* remove dummy level */
1959 while (matroska->num_levels) {
1960 matroska->num_levels--;
1962 matroska->levels[matroska->num_levels].length;
1963 if (length == (uint64_t)-1)
1968 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1970 matroska->peek_id = peek_id_cache;
1971 matroska->level_up = level_up;
1976 av_log(matroska->ctx, AV_LOG_INFO,
1977 "Ignoring seekhead entry for ID=0x%x\n",
1986 av_log(matroska->ctx, AV_LOG_INFO,
1987 "Unknown seekhead ID 0x%x\n", id);
1991 res = ebml_read_skip(matroska);
1995 if (matroska->level_up) {
1996 matroska->level_up--;
2005 matroska_read_header (AVFormatContext *s,
2006 AVFormatParameters *ap)
2008 MatroskaDemuxContext *matroska = s->priv_data;
2010 int version, last_level, res = 0;
2015 /* First read the EBML header. */
2017 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2019 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2020 av_log(matroska->ctx, AV_LOG_ERROR,
2021 "Wrong EBML doctype ('%s' != 'matroska').\n",
2022 doctype ? doctype : "(none)");
2025 return AVERROR_NOFMT;
2029 av_log(matroska->ctx, AV_LOG_ERROR,
2030 "Matroska demuxer version 1 too old for file version %d\n",
2032 return AVERROR_NOFMT;
2035 /* The next thing is a segment. */
2037 if (!(id = ebml_peek_id(matroska, &last_level)))
2039 if (id == MATROSKA_ID_SEGMENT)
2043 av_log(matroska->ctx, AV_LOG_INFO,
2044 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2045 MATROSKA_ID_SEGMENT, id);
2046 if ((res = ebml_read_skip(matroska)) < 0)
2050 /* We now have a Matroska segment.
2051 * Seeks are from the beginning of the segment,
2052 * after the segment ID/length. */
2053 if ((res = ebml_read_master(matroska, &id)) < 0)
2055 matroska->segment_start = url_ftell(&s->pb);
2057 matroska->time_scale = 1000000;
2058 /* we've found our segment, start reading the different contents in here */
2060 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2063 } else if (matroska->level_up) {
2064 matroska->level_up--;
2070 case MATROSKA_ID_INFO: {
2071 if ((res = ebml_read_master(matroska, &id)) < 0)
2073 res = matroska_parse_info(matroska);
2077 /* track info headers */
2078 case MATROSKA_ID_TRACKS: {
2079 if ((res = ebml_read_master(matroska, &id)) < 0)
2081 res = matroska_parse_tracks(matroska);
2086 case MATROSKA_ID_CUES: {
2087 if (!matroska->index_parsed) {
2088 if ((res = ebml_read_master(matroska, &id)) < 0)
2090 res = matroska_parse_index(matroska);
2092 res = ebml_read_skip(matroska);
2097 case MATROSKA_ID_TAGS: {
2098 if (!matroska->metadata_parsed) {
2099 if ((res = ebml_read_master(matroska, &id)) < 0)
2101 res = matroska_parse_metadata(matroska);
2103 res = ebml_read_skip(matroska);
2107 /* file index (if seekable, seek to Cues/Tags to parse it) */
2108 case MATROSKA_ID_SEEKHEAD: {
2109 if ((res = ebml_read_master(matroska, &id)) < 0)
2111 res = matroska_parse_seekhead(matroska);
2115 case MATROSKA_ID_CLUSTER: {
2116 /* Do not read the master - this will be done in the next
2117 * call to matroska_read_packet. */
2123 av_log(matroska->ctx, AV_LOG_INFO,
2124 "Unknown matroska file header ID 0x%x\n", id);
2128 res = ebml_read_skip(matroska);
2132 if (matroska->level_up) {
2133 matroska->level_up--;
2141 /* Have we found a cluster? */
2144 enum CodecID codec_id= CODEC_ID_NONE;
2145 MatroskaTrack *track;
2148 for (i = 0; i < matroska->num_tracks; i++) {
2149 void *extradata = NULL;
2150 int extradata_size = 0;
2151 track = matroska->tracks[i];
2153 /* libavformat does not really support subtitles.
2154 * Also apply some sanity checks. */
2155 if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
2156 (track->codec_id == NULL))
2159 for(j=0; codec_tags[j].str; j++){
2160 if(!strcmp(codec_tags[j].str, track->codec_id)){
2161 codec_id= codec_tags[j].id;
2166 /* Set the FourCC from the CodecID. */
2167 /* This is the MS compatibility mode which stores a
2168 * BITMAPINFOHEADER in the CodecPrivate. */
2169 if (!strcmp(track->codec_id,
2170 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2171 (track->codec_priv_size >= 40) &&
2172 (track->codec_priv != NULL)) {
2175 /* Offset of biCompression. Stored in LE. */
2176 p = (unsigned char *)track->codec_priv + 16;
2177 ((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
2178 (p[2] << 16) | (p[1] << 8) | p[0];
2179 codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
2183 /* This is the MS compatibility mode which stores a
2184 * WAVEFORMATEX in the CodecPrivate. */
2185 else if (!strcmp(track->codec_id,
2186 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2187 (track->codec_priv_size >= 18) &&
2188 (track->codec_priv != NULL)) {
2192 /* Offset of wFormatTag. Stored in LE. */
2193 p = (unsigned char *)track->codec_priv;
2194 tag = (p[1] << 8) | p[0];
2195 codec_id = codec_get_wav_id(tag);
2199 if (codec_id == CODEC_ID_NONE) {
2200 av_log(matroska->ctx, AV_LOG_INFO,
2201 "Unknown/unsupported CodecID %s.\n",
2205 track->stream_index = matroska->num_streams;
2207 matroska->num_streams++;
2208 st = av_new_stream(s, track->stream_index);
2210 return AVERROR_NOMEM;
2211 av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2213 st->codec->codec_id = codec_id;
2216 st->codec->extradata = extradata;
2217 st->codec->extradata_size = extradata_size;
2218 } else if(track->codec_priv && track->codec_priv_size > 0){
2219 st->codec->extradata = av_malloc(track->codec_priv_size);
2220 if(st->codec->extradata == NULL)
2221 return AVERROR_NOMEM;
2222 st->codec->extradata_size = track->codec_priv_size;
2223 memcpy(st->codec->extradata, track->codec_priv,
2224 track->codec_priv_size);
2227 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2228 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2230 st->codec->codec_type = CODEC_TYPE_VIDEO;
2231 st->codec->codec_tag = videotrack->fourcc;
2232 st->codec->width = videotrack->pixel_width;
2233 st->codec->height = videotrack->pixel_height;
2234 if (videotrack->display_width == 0)
2235 videotrack->display_width= videotrack->pixel_width;
2236 if (videotrack->display_height == 0)
2237 videotrack->display_height= videotrack->pixel_height;
2238 av_reduce(&st->codec->sample_aspect_ratio.num,
2239 &st->codec->sample_aspect_ratio.den,
2240 st->codec->height * videotrack->display_width,
2241 st->codec-> width * videotrack->display_height,
2243 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2244 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2246 st->codec->codec_type = CODEC_TYPE_AUDIO;
2247 st->codec->sample_rate = audiotrack->samplerate;
2248 st->codec->channels = audiotrack->channels;
2249 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2250 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2253 /* What do we do with private data? E.g. for Vorbis. */
2261 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
2266 for (i = 0; i < matroska->num_tracks; i++)
2267 if (matroska->tracks[i]->num == num)
2274 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2275 uint64_t cluster_time)
2280 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2282 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2285 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2288 } else if (matroska->level_up) {
2289 matroska->level_up--;
2294 /* one block inside the group. Note, block parsing is one
2295 * of the harder things, so this code is a bit complicated.
2296 * See http://www.matroska.org/ for documentation. */
2297 case MATROSKA_ID_BLOCK: {
2298 uint8_t *data, *origdata;
2301 uint32_t *lace_size = NULL;
2302 int n, track, flags, laces = 0;
2304 int64_t pos= url_ftell(&matroska->ctx->pb);
2306 if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
2310 /* first byte(s): blocknum */
2311 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2312 av_log(matroska->ctx, AV_LOG_ERROR,
2313 "EBML block data error\n");
2320 /* fetch track from num */
2321 track = matroska_find_track_by_num(matroska, num);
2322 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2323 av_log(matroska->ctx, AV_LOG_INFO,
2324 "Invalid stream %d or size %u\n", track, size);
2328 if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
2333 /* block_time (relative to cluster time) */
2334 block_time = (data[0] << 8) | data[1];
2340 switch ((flags & 0x06) >> 1) {
2341 case 0x0: /* no lacing */
2343 lace_size = av_mallocz(sizeof(int));
2344 lace_size[0] = size;
2347 case 0x1: /* xiph lacing */
2348 case 0x2: /* fixed-size lacing */
2349 case 0x3: /* EBML lacing */
2354 laces = (*data) + 1;
2357 lace_size = av_mallocz(laces * sizeof(int));
2359 switch ((flags & 0x06) >> 1) {
2360 case 0x1: /* xiph lacing */ {
2363 for (n = 0; res == 0 && n < laces - 1; n++) {
2370 lace_size[n] += temp;
2376 total += lace_size[n];
2378 lace_size[n] = size - total;
2382 case 0x2: /* fixed-size lacing */
2383 for (n = 0; n < laces; n++)
2384 lace_size[n] = size / laces;
2387 case 0x3: /* EBML lacing */ {
2389 n = matroska_ebmlnum_uint(data, size, &num);
2391 av_log(matroska->ctx, AV_LOG_INFO,
2392 "EBML block data error\n");
2397 total = lace_size[0] = num;
2398 for (n = 1; res == 0 && n < laces - 1; n++) {
2401 r = matroska_ebmlnum_sint (data, size,
2404 av_log(matroska->ctx, AV_LOG_INFO,
2405 "EBML block data error\n");
2410 lace_size[n] = lace_size[n - 1] + snum;
2411 total += lace_size[n];
2413 lace_size[n] = size - total;
2421 for (n = 0; n < laces; n++) {
2422 uint64_t timecode = AV_NOPTS_VALUE;
2424 pkt = av_mallocz(sizeof(AVPacket));
2425 /* XXX: prevent data copy... */
2426 if (av_new_packet(pkt,lace_size[n]) < 0) {
2427 res = AVERROR_NOMEM;
2430 if (cluster_time != (uint64_t)-1 && n == 0) {
2431 if (cluster_time + block_time >= 0)
2432 timecode = cluster_time + block_time;
2434 /* FIXME: duration */
2436 memcpy(pkt->data, data, lace_size[n]);
2437 data += lace_size[n];
2439 pkt->flags = is_keyframe;
2441 matroska->tracks[track]->stream_index;
2443 pkt->pts = timecode;
2446 matroska_queue_packet(matroska, pkt);
2455 case MATROSKA_ID_BLOCKDURATION: {
2457 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2459 av_log(matroska->ctx, AV_LOG_INFO,
2460 "FIXME: implement support for BlockDuration\n");
2464 case MATROSKA_ID_BLOCKREFERENCE:
2465 /* We've found a reference, so not even the first frame in
2466 * the lace is a key frame. */
2468 if (last_num_packets != matroska->num_packets)
2469 matroska->packets[last_num_packets]->flags = 0;
2470 res = ebml_read_skip(matroska);
2474 av_log(matroska->ctx, AV_LOG_INFO,
2475 "Unknown entry 0x%x in blockgroup data\n", id);
2479 res = ebml_read_skip(matroska);
2483 if (matroska->level_up) {
2484 matroska->level_up--;
2493 matroska_parse_cluster (MatroskaDemuxContext *matroska)
2497 uint64_t cluster_time = 0;
2499 av_log(matroska->ctx, AV_LOG_DEBUG,
2500 "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
2503 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2506 } else if (matroska->level_up) {
2507 matroska->level_up--;
2512 /* cluster timecode */
2513 case MATROSKA_ID_CLUSTERTIMECODE: {
2515 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2521 /* a group of blocks inside a cluster */
2522 case MATROSKA_ID_BLOCKGROUP:
2523 if ((res = ebml_read_master(matroska, &id)) < 0)
2525 res = matroska_parse_blockgroup(matroska, cluster_time);
2529 av_log(matroska->ctx, AV_LOG_INFO,
2530 "Unknown entry 0x%x in cluster data\n", id);
2534 res = ebml_read_skip(matroska);
2538 if (matroska->level_up) {
2539 matroska->level_up--;
2548 matroska_read_packet (AVFormatContext *s,
2551 MatroskaDemuxContext *matroska = s->priv_data;
2555 /* Do we still have a packet queued? */
2556 if (matroska_deliver_packet(matroska, pkt) == 0)
2559 /* Have we already reached the end? */
2564 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2567 } else if (matroska->level_up) {
2568 matroska->level_up--;
2573 case MATROSKA_ID_CLUSTER:
2574 if ((res = ebml_read_master(matroska, &id)) < 0)
2576 if ((res = matroska_parse_cluster(matroska)) == 0)
2577 res = 1; /* Parsed one cluster, let's get out. */
2582 res = ebml_read_skip(matroska);
2586 if (matroska->level_up) {
2587 matroska->level_up--;
2595 return matroska_deliver_packet(matroska, pkt);
2599 matroska_read_close (AVFormatContext *s)
2601 MatroskaDemuxContext *matroska = s->priv_data;
2604 if (matroska->writing_app)
2605 av_free(matroska->writing_app);
2606 if (matroska->muxing_app)
2607 av_free(matroska->muxing_app);
2608 if (matroska->index)
2609 av_free(matroska->index);
2611 if (matroska->packets != NULL) {
2612 for (n = 0; n < matroska->num_packets; n++) {
2613 av_free_packet(matroska->packets[n]);
2614 av_free(matroska->packets[n]);
2616 av_free(matroska->packets);
2619 for (n = 0; n < matroska->num_tracks; n++) {
2620 MatroskaTrack *track = matroska->tracks[n];
2621 if (track->codec_id)
2622 av_free(track->codec_id);
2623 if (track->codec_name)
2624 av_free(track->codec_name);
2625 if (track->codec_priv)
2626 av_free(track->codec_priv);
2628 av_free(track->name);
2629 if (track->language)
2630 av_free(track->language);
2635 memset(matroska, 0, sizeof(MatroskaDemuxContext));
2640 AVInputFormat matroska_demuxer = {
2642 "Matroska file format",
2643 sizeof(MatroskaDemuxContext),
2645 matroska_read_header,
2646 matroska_read_packet,
2647 matroska_read_close,