2 * Matroska file demuxer (no muxer yet)
3 * Copyright (c) 2003-2004 The ffmpeg Project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Matroska file demuxer
23 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
24 * with a little help from Moritz Bunkus <moritz@bunkus.org>
25 * Specs available on the matroska project page:
26 * http://www.matroska.org/.
30 /* For codec_get_bmp_id and codec_get_wav_id. */
33 /* EBML version supported */
34 #define EBML_VERSION 1
36 /* top-level master-IDs */
37 #define EBML_ID_HEADER 0x1A45DFA3
39 /* IDs in the HEADER master */
40 #define EBML_ID_EBMLVERSION 0x4286
41 #define EBML_ID_EBMLREADVERSION 0x42F7
42 #define EBML_ID_EBMLMAXIDLENGTH 0x42F2
43 #define EBML_ID_EBMLMAXSIZELENGTH 0x42F3
44 #define EBML_ID_DOCTYPE 0x4282
45 #define EBML_ID_DOCTYPEVERSION 0x4287
46 #define EBML_ID_DOCTYPEREADVERSION 0x4285
48 /* general EBML types */
49 #define EBML_ID_VOID 0xEC
52 * Matroska element IDs. max. 32-bit.
55 /* toplevel segment */
56 #define MATROSKA_ID_SEGMENT 0x18538067
58 /* matroska top-level master IDs */
59 #define MATROSKA_ID_INFO 0x1549A966
60 #define MATROSKA_ID_TRACKS 0x1654AE6B
61 #define MATROSKA_ID_CUES 0x1C53BB6B
62 #define MATROSKA_ID_TAGS 0x1254C367
63 #define MATROSKA_ID_SEEKHEAD 0x114D9B74
64 #define MATROSKA_ID_CLUSTER 0x1F43B675
66 /* IDs in the info master */
67 #define MATROSKA_ID_TIMECODESCALE 0x2AD7B1
68 #define MATROSKA_ID_DURATION 0x4489
69 #define MATROSKA_ID_WRITINGAPP 0x5741
70 #define MATROSKA_ID_MUXINGAPP 0x4D80
71 #define MATROSKA_ID_DATEUTC 0x4461
73 /* ID in the tracks master */
74 #define MATROSKA_ID_TRACKENTRY 0xAE
76 /* IDs in the trackentry master */
77 #define MATROSKA_ID_TRACKNUMBER 0xD7
78 #define MATROSKA_ID_TRACKUID 0x73C5
79 #define MATROSKA_ID_TRACKTYPE 0x83
80 #define MATROSKA_ID_TRACKAUDIO 0xE1
81 #define MATROSKA_ID_TRACKVIDEO 0xE0
82 #define MATROSKA_ID_CODECID 0x86
83 #define MATROSKA_ID_CODECPRIVATE 0x63A2
84 #define MATROSKA_ID_CODECNAME 0x258688
85 #define MATROSKA_ID_CODECINFOURL 0x3B4040
86 #define MATROSKA_ID_CODECDOWNLOADURL 0x26B240
87 #define MATROSKA_ID_TRACKNAME 0x536E
88 #define MATROSKA_ID_TRACKLANGUAGE 0x22B59C
89 #define MATROSKA_ID_TRACKFLAGENABLED 0xB9
90 #define MATROSKA_ID_TRACKFLAGDEFAULT 0x88
91 #define MATROSKA_ID_TRACKFLAGLACING 0x9C
92 #define MATROSKA_ID_TRACKMINCACHE 0x6DE7
93 #define MATROSKA_ID_TRACKMAXCACHE 0x6DF8
94 #define MATROSKA_ID_TRACKDEFAULTDURATION 0x23E383
96 /* IDs in the trackvideo master */
97 #define MATROSKA_ID_VIDEOFRAMERATE 0x2383E3
98 #define MATROSKA_ID_VIDEODISPLAYWIDTH 0x54B0
99 #define MATROSKA_ID_VIDEODISPLAYHEIGHT 0x54BA
100 #define MATROSKA_ID_VIDEOPIXELWIDTH 0xB0
101 #define MATROSKA_ID_VIDEOPIXELHEIGHT 0xBA
102 #define MATROSKA_ID_VIDEOFLAGINTERLACED 0x9A
103 #define MATROSKA_ID_VIDEOSTEREOMODE 0x53B9
104 #define MATROSKA_ID_VIDEOASPECTRATIO 0x54B3
105 #define MATROSKA_ID_VIDEOCOLOURSPACE 0x2EB524
107 /* IDs in the trackaudio master */
108 #define MATROSKA_ID_AUDIOSAMPLINGFREQ 0xB5
109 #define MATROSKA_ID_AUDIOBITDEPTH 0x6264
110 #define MATROSKA_ID_AUDIOCHANNELS 0x9F
112 /* ID in the cues master */
113 #define MATROSKA_ID_POINTENTRY 0xBB
115 /* IDs in the pointentry master */
116 #define MATROSKA_ID_CUETIME 0xB3
117 #define MATROSKA_ID_CUETRACKPOSITION 0xB7
119 /* IDs in the cuetrackposition master */
120 #define MATROSKA_ID_CUETRACK 0xF7
121 #define MATROSKA_ID_CUECLUSTERPOSITION 0xF1
123 /* IDs in the tags master */
126 /* IDs in the seekhead master */
127 #define MATROSKA_ID_SEEKENTRY 0x4DBB
129 /* IDs in the seekpoint master */
130 #define MATROSKA_ID_SEEKID 0x53AB
131 #define MATROSKA_ID_SEEKPOSITION 0x53AC
133 /* IDs in the cluster master */
134 #define MATROSKA_ID_CLUSTERTIMECODE 0xE7
135 #define MATROSKA_ID_BLOCKGROUP 0xA0
137 /* IDs in the blockgroup master */
138 #define MATROSKA_ID_BLOCK 0xA1
139 #define MATROSKA_ID_BLOCKDURATION 0x9B
140 #define MATROSKA_ID_BLOCKREFERENCE 0xFB
143 MATROSKA_TRACK_TYPE_VIDEO = 0x1,
144 MATROSKA_TRACK_TYPE_AUDIO = 0x2,
145 MATROSKA_TRACK_TYPE_COMPLEX = 0x3,
146 MATROSKA_TRACK_TYPE_LOGO = 0x10,
147 MATROSKA_TRACK_TYPE_SUBTITLE = 0x11,
148 MATROSKA_TRACK_TYPE_CONTROL = 0x20,
152 MATROSKA_EYE_MODE_MONO = 0x0,
153 MATROSKA_EYE_MODE_RIGHT = 0x1,
154 MATROSKA_EYE_MODE_LEFT = 0x2,
155 MATROSKA_EYE_MODE_BOTH = 0x3,
159 MATROSKA_ASPECT_RATIO_MODE_FREE = 0x0,
160 MATROSKA_ASPECT_RATIO_MODE_KEEP = 0x1,
161 MATROSKA_ASPECT_RATIO_MODE_FIXED = 0x2,
162 } MatroskaAspectRatioMode;
165 * These aren't in any way "matroska-form" things,
166 * it's just something I use in the muxer/demuxer.
170 MATROSKA_TRACK_ENABLED = (1<<0),
171 MATROSKA_TRACK_DEFAULT = (1<<1),
172 MATROSKA_TRACK_LACING = (1<<2),
173 MATROSKA_TRACK_SHIFT = (1<<16)
174 } MatroskaTrackFlags;
177 MATROSKA_VIDEOTRACK_INTERLACED = (MATROSKA_TRACK_SHIFT<<0)
178 } MatroskaVideoTrackFlags;
181 * Matroska Codec IDs. Strings.
184 #define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC "V_MS/VFW/FOURCC"
185 #define MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED "V_UNCOMPRESSED"
186 #define MATROSKA_CODEC_ID_VIDEO_MPEG4_SP "V_MPEG4/ISO/SP"
187 #define MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP "V_MPEG4/ISO/ASP"
188 #define MATROSKA_CODEC_ID_VIDEO_MPEG4_AP "V_MPEG4/ISO/AP"
189 #define MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3 "V_MPEG4/MS/V3"
190 #define MATROSKA_CODEC_ID_VIDEO_MPEG1 "V_MPEG1"
191 #define MATROSKA_CODEC_ID_VIDEO_MPEG2 "V_MPEG2"
192 #define MATROSKA_CODEC_ID_VIDEO_MJPEG "V_MJPEG"
193 /* TODO: Real/Quicktime */
195 #define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
196 #define MATROSKA_CODEC_ID_AUDIO_MPEG1_L1 "A_MPEG/L1"
197 #define MATROSKA_CODEC_ID_AUDIO_MPEG1_L2 "A_MPEG/L2"
198 #define MATROSKA_CODEC_ID_AUDIO_MPEG1_L3 "A_MPEG/L3"
199 #define MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE "A_PCM/INT/BIG"
200 #define MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE "A_PCM/INT/LIT"
201 #define MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT "A_PCM/FLOAT/IEEE"
202 #define MATROSKA_CODEC_ID_AUDIO_AC3 "A_AC3"
203 #define MATROSKA_CODEC_ID_AUDIO_DTS "A_DTS"
204 #define MATROSKA_CODEC_ID_AUDIO_VORBIS "A_VORBIS"
205 #define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
206 #define MATROSKA_CODEC_ID_AUDIO_MPEG2 "A_AAC/MPEG2/"
207 #define MATROSKA_CODEC_ID_AUDIO_MPEG4 "A_AAC/MPEG4/"
208 /* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
210 /* max. depth in the EBML tree structure */
211 #define EBML_MAX_DEPTH 16
213 typedef struct Track {
214 MatroskaTrackType type;
216 /* Unique track number and track ID. stream_index is the index that
217 * the calling app uses for this track. */
228 unsigned char *codec_priv;
231 int64_t default_duration;
232 MatroskaTrackFlags flags;
235 typedef struct MatroskaVideoTrack {
245 MatroskaAspectRatioMode ar_mode;
246 MatroskaEyeMode eye_mode;
249 } MatroskaVideoTrack;
251 typedef struct MatroskaAudioTrack {
258 } MatroskaAudioTrack;
260 typedef struct MatroskaSubtitleTrack {
264 } MatroskaSubtitleTrack;
266 typedef struct MatroskaLevel {
267 uint64_t start, length;
270 typedef struct MatroskaDemuxIndex {
271 uint64_t pos; /* of the corresponding *cluster*! */
272 uint16_t track; /* reference to 'num' */
273 uint64_t time; /* in nanoseconds */
274 } MatroskaDemuxIndex;
276 typedef struct MatroskaDemuxContext {
277 AVFormatContext *ctx;
281 MatroskaLevel levels[EBML_MAX_DEPTH];
289 /* timescale in the file */
292 /* length, position (time, ns) */
296 /* num_streams is the number of streams that av_new_stream() was called
297 * for ( = that are available to the calling program). */
298 int num_tracks, num_streams;
299 MatroskaTrack *tracks[MAX_STREAMS];
301 /* cache for ID peeking */
304 /* byte position of the segment inside the stream */
305 offset_t segment_start;
307 /* The packet queue. */
311 /* have we already parse metadata/cues/clusters? */
316 /* The index for seeking. */
318 MatroskaDemuxIndex *index;
319 } MatroskaDemuxContext;
322 * The first few functions handle EBML file parsing. The rest
323 * is the document interpretation. Matroska really just is a
328 * Return: the amount of levels in the hierarchy that the
329 * current element lies higher than the previous one.
330 * The opposite isn't done - that's auto-done using master
335 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
337 ByteIOContext *pb = &matroska->ctx->pb;
338 offset_t pos = url_ftell(pb);
341 while (matroska->num_levels > 0) {
342 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
344 if (pos >= level->start + level->length) {
345 matroska->num_levels--;
356 * Read: an "EBML number", which is defined as a variable-length
357 * array of bytes. The first byte indicates the length by giving a
358 * number of 0-bits followed by a one. The position of the first
359 * "one" bit inside the first byte indicates the length of this
361 * Returns: num. of bytes read. < 0 on error.
365 ebml_read_num (MatroskaDemuxContext *matroska,
369 ByteIOContext *pb = &matroska->ctx->pb;
370 int len_mask = 0x80, read = 1, n = 1;
373 /* the first byte tells us the length in bytes - get_byte() can normally
374 * return 0, but since that's not a valid first ebmlID byte, we can
375 * use it safely here to catch EOS. */
376 if (!(total = get_byte(pb))) {
377 /* we might encounter EOS here */
379 offset_t pos = url_ftell(pb);
380 av_log(matroska->ctx, AV_LOG_ERROR,
381 "Read error at pos. %llu (0x%llx)\n",
384 return AVERROR_IO; /* EOS or actual I/O error */
387 /* get the length of the EBML number */
388 while (read <= max_size && !(total & len_mask)) {
392 if (read > max_size) {
393 offset_t pos = url_ftell(pb) - 1;
394 av_log(matroska->ctx, AV_LOG_ERROR,
395 "Invalid EBML number size tag 0x%02x at pos %llu (0x%llx)\n",
396 (uint8_t) total, pos, pos);
397 return AVERROR_INVALIDDATA;
400 /* read out length */
403 total = (total << 8) | get_byte(pb);
411 * Read: the element content data ID.
412 * Return: the number of bytes read or < 0 on error.
416 ebml_read_element_id (MatroskaDemuxContext *matroska,
423 /* if we re-call this, use our cached ID */
424 if (matroska->peek_id != 0) {
427 *id = matroska->peek_id;
431 /* read out the "EBML number", include tag in ID */
432 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
434 *id = matroska->peek_id = total | (1 << (read * 7));
438 *level_up = ebml_read_element_level_up(matroska);
444 * Read: element content length.
445 * Return: the number of bytes read or < 0 on error.
449 ebml_read_element_length (MatroskaDemuxContext *matroska,
452 /* clear cache since we're now beyond that data point */
453 matroska->peek_id = 0;
455 /* read out the "EBML number", include tag in ID */
456 return ebml_read_num(matroska, 8, length);
460 * Return: the ID of the next element, or 0 on error.
461 * Level_up contains the amount of levels that this
462 * next element lies higher than the previous one.
466 ebml_peek_id (MatroskaDemuxContext *matroska,
471 assert(level_up != NULL);
473 if (ebml_read_element_id(matroska, &id, level_up) < 0)
480 * Seek to a given offset.
481 * 0 is success, -1 is failure.
485 ebml_read_seek (MatroskaDemuxContext *matroska,
488 ByteIOContext *pb = &matroska->ctx->pb;
490 /* clear ID cache, if any */
491 matroska->peek_id = 0;
493 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
497 * Skip the next element.
498 * 0 is success, -1 is failure.
502 ebml_read_skip (MatroskaDemuxContext *matroska)
504 ByteIOContext *pb = &matroska->ctx->pb;
509 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
510 (res = ebml_read_element_length(matroska, &length)) < 0)
513 url_fskip(pb, length);
519 * Read the next element as an unsigned int.
520 * 0 is success, < 0 is failure.
524 ebml_read_uint (MatroskaDemuxContext *matroska,
528 ByteIOContext *pb = &matroska->ctx->pb;
529 int n = 0, size, res;
532 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
533 (res = ebml_read_element_length(matroska, &rlength)) < 0)
536 if (size < 1 || size > 8) {
537 offset_t pos = url_ftell(pb);
538 av_log(matroska->ctx, AV_LOG_ERROR,
539 "Invalid uint element size %d at position %lld (0x%llx)\n",
541 return AVERROR_INVALIDDATA;
544 /* big-endian ordening; build up number */
547 *num = (*num << 8) | get_byte(pb);
553 * Read the next element as a signed int.
554 * 0 is success, < 0 is failure.
558 ebml_read_sint (MatroskaDemuxContext *matroska,
562 ByteIOContext *pb = &matroska->ctx->pb;
563 int size, n = 1, negative = 0, res;
566 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
567 (res = ebml_read_element_length(matroska, &rlength)) < 0)
570 if (size < 1 || size > 8) {
571 offset_t pos = url_ftell(pb);
572 av_log(matroska->ctx, AV_LOG_ERROR,
573 "Invalid sint element size %d at position %lld (0x%llx)\n",
575 return AVERROR_INVALIDDATA;
577 if ((*num = get_byte(pb)) & 0x80) {
583 *num = (*num << 8) | get_byte(pb);
587 *num = *num - (1LL << ((8 * size) - 1));
593 * Read the next element as a float.
594 * 0 is success, < 0 is failure.
598 ebml_read_float (MatroskaDemuxContext *matroska,
602 ByteIOContext *pb = &matroska->ctx->pb;
606 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
607 (res = ebml_read_element_length(matroska, &rlength)) < 0)
611 if (size != 4 && size != 8 && size != 10) {
612 offset_t pos = url_ftell(pb);
613 av_log(matroska->ctx, AV_LOG_ERROR,
614 "Invalid float element size %d at position %llu (0x%llx)\n",
616 return AVERROR_INVALIDDATA;
619 av_log(matroska->ctx, AV_LOG_ERROR,
620 "FIXME! 10-byte floats unimplemented\n");
621 return AVERROR_UNKNOWN;
628 #ifdef WORDS_BIGENDIAN
629 ((uint8_t *) &f)[3 - size] = get_byte(pb);
631 ((uint8_t *) &f)[size] = get_byte(pb);
639 #ifdef WORDS_BIGENDIAN
640 ((uint8_t *) &d)[7 - size] = get_byte(pb);
642 ((uint8_t *) &d)[size] = get_byte(pb);
652 * Read the next element as an ASCII string.
653 * 0 is success, < 0 is failure.
657 ebml_read_ascii (MatroskaDemuxContext *matroska,
661 ByteIOContext *pb = &matroska->ctx->pb;
665 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
666 (res = ebml_read_element_length(matroska, &rlength)) < 0)
670 /* ebml strings are usually not 0-terminated, so we allocate one
671 * byte more, read the string and NULL-terminate it ourselves. */
672 if (!(*str = av_malloc(size + 1))) {
673 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
674 return AVERROR_NOMEM;
676 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
677 offset_t pos = url_ftell(pb);
678 av_log(matroska->ctx, AV_LOG_ERROR,
679 "Read error at pos. %llu (0x%llx)\n", pos, pos);
688 * Read the next element as a UTF-8 string.
689 * 0 is success, < 0 is failure.
693 ebml_read_utf8 (MatroskaDemuxContext *matroska,
697 return ebml_read_ascii(matroska, id, str);
701 * Read the next element as a date (nanoseconds since 1/1/2000).
702 * 0 is success, < 0 is failure.
706 ebml_read_date (MatroskaDemuxContext *matroska,
710 return ebml_read_sint(matroska, id, date);
714 * Read the next element, but only the header. The contents
715 * are supposed to be sub-elements which can be read separately.
716 * 0 is success, < 0 is failure.
720 ebml_read_master (MatroskaDemuxContext *matroska,
723 ByteIOContext *pb = &matroska->ctx->pb;
725 MatroskaLevel *level;
728 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
729 (res = ebml_read_element_length(matroska, &length)) < 0)
732 /* protect... (Heaven forbids that the '>' is true) */
733 if (matroska->num_levels >= EBML_MAX_DEPTH) {
734 av_log(matroska->ctx, AV_LOG_ERROR,
735 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
736 return AVERROR_NOTSUPP;
740 level = &matroska->levels[matroska->num_levels++];
741 level->start = url_ftell(pb);
742 level->length = length;
748 * Read the next element as binary data.
749 * 0 is success, < 0 is failure.
753 ebml_read_binary (MatroskaDemuxContext *matroska,
758 ByteIOContext *pb = &matroska->ctx->pb;
762 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
763 (res = ebml_read_element_length(matroska, &rlength)) < 0)
767 if (!(*binary = av_malloc(*size))) {
768 av_log(matroska->ctx, AV_LOG_ERROR,
769 "Memory allocation error\n");
770 return AVERROR_NOMEM;
773 if (get_buffer(pb, *binary, *size) != *size) {
774 offset_t pos = url_ftell(pb);
775 av_log(matroska->ctx, AV_LOG_ERROR,
776 "Read error at pos. %llu (0x%llx)\n", pos, pos);
784 * Read signed/unsigned "EBML" numbers.
785 * Return: number of bytes processed, < 0 on error.
786 * XXX: use ebml_read_num().
790 matroska_ebmlnum_uint (uint8_t *data,
794 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
798 return AVERROR_INVALIDDATA;
801 while (read <= 8 && !(total & len_mask)) {
806 return AVERROR_INVALIDDATA;
808 if ((total &= (len_mask - 1)) == len_mask - 1)
811 return AVERROR_INVALIDDATA;
815 total = (total << 8) | data[n];
820 return AVERROR_INVALIDDATA;
831 * Same as above, but signed.
835 matroska_ebmlnum_sint (uint8_t *data,
842 /* read as unsigned number first */
843 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
846 /* make signed (weird way) */
847 if (unum == (uint64_t)-1)
850 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
856 * Read an EBML header.
857 * 0 is success, < 0 is failure.
861 ebml_read_header (MatroskaDemuxContext *matroska,
866 int level_up, res = 0;
874 if (!(id = ebml_peek_id(matroska, &level_up)) ||
875 level_up != 0 || id != EBML_ID_HEADER) {
876 av_log(matroska->ctx, AV_LOG_ERROR,
877 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
878 return AVERROR_INVALIDDATA;
880 if ((res = ebml_read_master(matroska, &id)) < 0)
884 if (!(id = ebml_peek_id(matroska, &level_up)))
892 /* is our read version uptodate? */
893 case EBML_ID_EBMLREADVERSION: {
896 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
898 if (num > EBML_VERSION) {
899 av_log(matroska->ctx, AV_LOG_ERROR,
900 "EBML version %llu (> %d) is not supported\n",
902 return AVERROR_INVALIDDATA;
907 /* we only handle 8 byte lengths at max */
908 case EBML_ID_EBMLMAXSIZELENGTH: {
911 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
913 if (num > sizeof(uint64_t)) {
914 av_log(matroska->ctx, AV_LOG_ERROR,
915 "Integers of size %llu (> %d) not supported\n",
916 num, sizeof(uint64_t));
917 return AVERROR_INVALIDDATA;
922 /* we handle 4 byte IDs at max */
923 case EBML_ID_EBMLMAXIDLENGTH: {
926 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
928 if (num > sizeof(uint32_t)) {
929 av_log(matroska->ctx, AV_LOG_ERROR,
930 "IDs of size %llu (> %u) not supported\n",
931 num, sizeof(uint32_t));
932 return AVERROR_INVALIDDATA;
937 case EBML_ID_DOCTYPE: {
940 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
951 case EBML_ID_DOCTYPEREADVERSION: {
954 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
962 av_log(matroska->ctx, AV_LOG_INFO,
963 "Unknown data type 0x%x in EBML header", id);
967 /* we ignore these two, as they don't tell us anything we
969 case EBML_ID_EBMLVERSION:
970 case EBML_ID_DOCTYPEVERSION:
971 res = ebml_read_skip (matroska);
980 * Put one packet in an application-supplied AVPacket struct.
981 * Returns 0 on success or -1 on failure.
985 matroska_deliver_packet (MatroskaDemuxContext *matroska,
988 if (matroska->num_packets > 0) {
989 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
990 av_free(matroska->packets[0]);
991 if (matroska->num_packets > 1) {
992 memmove(&matroska->packets[0], &matroska->packets[1],
993 (matroska->num_packets - 1) * sizeof(AVPacket *));
995 av_realloc(matroska->packets, (matroska->num_packets - 1) *
998 av_free(matroska->packets);
999 matroska->packets = NULL;
1001 matroska->num_packets--;
1009 * Put a packet into our internal queue. Will be delivered to the
1010 * user/application during the next get_packet() call.
1014 matroska_queue_packet (MatroskaDemuxContext *matroska,
1018 av_realloc(matroska->packets, (matroska->num_packets + 1) *
1019 sizeof(AVPacket *));
1020 matroska->packets[matroska->num_packets] = pkt;
1021 matroska->num_packets++;
1029 matroska_probe (AVProbeData *p)
1032 int len_mask = 0x80, size = 1, n = 1;
1033 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
1035 if (p->buf_size < 5)
1039 if ((p->buf[0] << 24 | p->buf[1] << 16 |
1040 p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
1043 /* length of header */
1045 while (size <= 8 && !(total & len_mask)) {
1051 total &= (len_mask - 1);
1053 total = (total << 8) | p->buf[4 + n++];
1055 /* does the probe data contain the whole header? */
1056 if (p->buf_size < 4 + size + total)
1059 /* the header must contain the document type 'matroska'. For now,
1060 * we don't parse the whole header but simply check for the
1061 * availability of that array of characters inside the header.
1062 * Not fully fool-proof, but good enough. */
1063 for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
1064 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
1065 return AVPROBE_SCORE_MAX;
1071 * From here on, it's all XML-style DTD stuff... Needs no comments.
1075 matroska_parse_info (MatroskaDemuxContext *matroska)
1080 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
1083 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1086 } else if (matroska->level_up) {
1087 matroska->level_up--;
1092 /* cluster timecode */
1093 case MATROSKA_ID_TIMECODESCALE: {
1095 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1097 matroska->time_scale = num;
1101 case MATROSKA_ID_DURATION: {
1103 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1105 matroska->duration = num * matroska->time_scale;
1109 case MATROSKA_ID_WRITINGAPP: {
1111 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1113 matroska->writing_app = text;
1117 case MATROSKA_ID_MUXINGAPP: {
1119 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1121 matroska->muxing_app = text;
1125 case MATROSKA_ID_DATEUTC: {
1127 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
1129 matroska->created = time;
1134 av_log(matroska->ctx, AV_LOG_INFO,
1135 "Unknown entry 0x%x in info header\n", id);
1139 res = ebml_read_skip(matroska);
1143 if (matroska->level_up) {
1144 matroska->level_up--;
1153 matroska_add_stream (MatroskaDemuxContext *matroska)
1157 MatroskaTrack *track;
1159 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1161 /* Allocate a generic track. As soon as we know its type we'll realloc. */
1162 track = av_mallocz(sizeof(MatroskaTrack));
1163 matroska->num_tracks++;
1165 /* start with the master */
1166 if ((res = ebml_read_master(matroska, &id)) < 0)
1169 /* try reading the trackentry headers */
1171 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1174 } else if (matroska->level_up > 0) {
1175 matroska->level_up--;
1180 /* track number (unique stream ID) */
1181 case MATROSKA_ID_TRACKNUMBER: {
1183 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1189 /* track UID (unique identifier) */
1190 case MATROSKA_ID_TRACKUID: {
1192 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1198 /* track type (video, audio, combined, subtitle, etc.) */
1199 case MATROSKA_ID_TRACKTYPE: {
1201 if (track->type != 0) {
1202 av_log(matroska->ctx, AV_LOG_INFO,
1203 "More than one tracktype in an entry - skip\n");
1206 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1210 /* ok, so we're actually going to reallocate this thing */
1211 switch (track->type) {
1212 case MATROSKA_TRACK_TYPE_VIDEO:
1213 track = (MatroskaTrack *)
1214 av_realloc(track, sizeof(MatroskaVideoTrack));
1216 case MATROSKA_TRACK_TYPE_AUDIO:
1217 track = (MatroskaTrack *)
1218 av_realloc(track, sizeof(MatroskaAudioTrack));
1219 ((MatroskaAudioTrack *)track)->channels = 1;
1220 ((MatroskaAudioTrack *)track)->samplerate = 8000;
1222 case MATROSKA_TRACK_TYPE_SUBTITLE:
1223 track = (MatroskaTrack *)
1224 av_realloc(track, sizeof(MatroskaSubtitleTrack));
1226 case MATROSKA_TRACK_TYPE_COMPLEX:
1227 case MATROSKA_TRACK_TYPE_LOGO:
1228 case MATROSKA_TRACK_TYPE_CONTROL:
1230 av_log(matroska->ctx, AV_LOG_INFO,
1231 "Unknown or unsupported track type 0x%x\n",
1236 matroska->tracks[matroska->num_tracks - 1] = track;
1240 /* tracktype specific stuff for video */
1241 case MATROSKA_ID_TRACKVIDEO: {
1242 MatroskaVideoTrack *videotrack;
1243 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1244 av_log(matroska->ctx, AV_LOG_INFO,
1245 "video data in non-video track - ignoring\n");
1246 res = AVERROR_INVALIDDATA;
1248 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1250 videotrack = (MatroskaVideoTrack *)track;
1253 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1256 } else if (matroska->level_up > 0) {
1257 matroska->level_up--;
1262 /* fixme, this should be one-up, but I get it here */
1263 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1265 if ((res = ebml_read_uint (matroska, &id,
1268 track->default_duration = num;
1272 /* video framerate */
1273 case MATROSKA_ID_VIDEOFRAMERATE: {
1275 if ((res = ebml_read_float(matroska, &id,
1278 track->default_duration = 1000000000 * (1. / num);
1282 /* width of the size to display the video at */
1283 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1285 if ((res = ebml_read_uint(matroska, &id,
1288 videotrack->display_width = num;
1292 /* height of the size to display the video at */
1293 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1295 if ((res = ebml_read_uint(matroska, &id,
1298 videotrack->display_height = num;
1302 /* width of the video in the file */
1303 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1305 if ((res = ebml_read_uint(matroska, &id,
1308 videotrack->pixel_width = num;
1312 /* height of the video in the file */
1313 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1315 if ((res = ebml_read_uint(matroska, &id,
1318 videotrack->pixel_height = num;
1322 /* whether the video is interlaced */
1323 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1325 if ((res = ebml_read_uint(matroska, &id,
1330 MATROSKA_VIDEOTRACK_INTERLACED;
1333 ~MATROSKA_VIDEOTRACK_INTERLACED;
1337 /* stereo mode (whether the video has two streams,
1338 * where one is for the left eye and the other for
1339 * the right eye, which creates a 3D-like
1341 case MATROSKA_ID_VIDEOSTEREOMODE: {
1343 if ((res = ebml_read_uint(matroska, &id,
1346 if (num != MATROSKA_EYE_MODE_MONO &&
1347 num != MATROSKA_EYE_MODE_LEFT &&
1348 num != MATROSKA_EYE_MODE_RIGHT &&
1349 num != MATROSKA_EYE_MODE_BOTH) {
1350 av_log(matroska->ctx, AV_LOG_INFO,
1351 "Ignoring unknown eye mode 0x%x\n",
1355 videotrack->eye_mode = num;
1359 /* aspect ratio behaviour */
1360 case MATROSKA_ID_VIDEOASPECTRATIO: {
1362 if ((res = ebml_read_uint(matroska, &id,
1365 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1366 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1367 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1368 av_log(matroska->ctx, AV_LOG_INFO,
1369 "Ignoring unknown aspect ratio 0x%x\n",
1373 videotrack->ar_mode = num;
1377 /* colourspace (only matters for raw video)
1379 case MATROSKA_ID_VIDEOCOLOURSPACE: {
1381 if ((res = ebml_read_uint(matroska, &id,
1384 videotrack->fourcc = num;
1389 av_log(matroska->ctx, AV_LOG_INFO,
1390 "Unknown video track header entry "
1391 "0x%x - ignoring\n", id);
1395 res = ebml_read_skip(matroska);
1399 if (matroska->level_up) {
1400 matroska->level_up--;
1407 /* tracktype specific stuff for audio */
1408 case MATROSKA_ID_TRACKAUDIO: {
1409 MatroskaAudioTrack *audiotrack;
1410 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1411 av_log(matroska->ctx, AV_LOG_INFO,
1412 "audio data in non-audio track - ignoring\n");
1413 res = AVERROR_INVALIDDATA;
1415 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1417 audiotrack = (MatroskaAudioTrack *)track;
1420 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1423 } else if (matroska->level_up > 0) {
1424 matroska->level_up--;
1430 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1432 if ((res = ebml_read_float(matroska, &id,
1435 audiotrack->samplerate = num;
1440 case MATROSKA_ID_AUDIOBITDEPTH: {
1442 if ((res = ebml_read_uint(matroska, &id,
1445 audiotrack->bitdepth = num;
1450 case MATROSKA_ID_AUDIOCHANNELS: {
1452 if ((res = ebml_read_uint(matroska, &id,
1455 audiotrack->channels = num;
1460 av_log(matroska->ctx, AV_LOG_INFO,
1461 "Unknown audio track header entry "
1462 "0x%x - ignoring\n", id);
1466 res = ebml_read_skip(matroska);
1470 if (matroska->level_up) {
1471 matroska->level_up--;
1478 /* codec identifier */
1479 case MATROSKA_ID_CODECID: {
1481 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1483 track->codec_id = text;
1487 /* codec private data */
1488 case MATROSKA_ID_CODECPRIVATE: {
1491 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1493 track->codec_priv = data;
1494 track->codec_priv_size = size;
1498 /* name of the codec */
1499 case MATROSKA_ID_CODECNAME: {
1501 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1503 track->codec_name = text;
1507 /* name of this track */
1508 case MATROSKA_ID_TRACKNAME: {
1510 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1516 /* language (matters for audio/subtitles, mostly) */
1517 case MATROSKA_ID_TRACKLANGUAGE: {
1519 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1521 track->language = text;
1525 /* whether this is actually used */
1526 case MATROSKA_ID_TRACKFLAGENABLED: {
1528 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1531 track->flags |= MATROSKA_TRACK_ENABLED;
1533 track->flags &= ~MATROSKA_TRACK_ENABLED;
1537 /* whether it's the default for this track type */
1538 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1540 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1543 track->flags |= MATROSKA_TRACK_DEFAULT;
1545 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1549 /* lacing (like MPEG, where blocks don't end/start on frame
1551 case MATROSKA_ID_TRACKFLAGLACING: {
1553 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1556 track->flags |= MATROSKA_TRACK_LACING;
1558 track->flags &= ~MATROSKA_TRACK_LACING;
1562 /* default length (in time) of one data block in this track */
1563 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1565 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1567 track->default_duration = num;
1572 av_log(matroska->ctx, AV_LOG_INFO,
1573 "Unknown track header entry 0x%x - ignoring\n", id);
1577 /* we ignore these because they're nothing useful. */
1578 case MATROSKA_ID_CODECINFOURL:
1579 case MATROSKA_ID_CODECDOWNLOADURL:
1580 case MATROSKA_ID_TRACKMINCACHE:
1581 case MATROSKA_ID_TRACKMAXCACHE:
1582 res = ebml_read_skip(matroska);
1586 if (matroska->level_up) {
1587 matroska->level_up--;
1596 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1601 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1604 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1607 } else if (matroska->level_up) {
1608 matroska->level_up--;
1613 /* one track within the "all-tracks" header */
1614 case MATROSKA_ID_TRACKENTRY:
1615 res = matroska_add_stream(matroska);
1619 av_log(matroska->ctx, AV_LOG_INFO,
1620 "Unknown entry 0x%x in track header\n", id);
1624 res = ebml_read_skip(matroska);
1628 if (matroska->level_up) {
1629 matroska->level_up--;
1638 matroska_parse_index (MatroskaDemuxContext *matroska)
1642 MatroskaDemuxIndex idx;
1644 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1647 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1650 } else if (matroska->level_up) {
1651 matroska->level_up--;
1656 /* one single index entry ('point') */
1657 case MATROSKA_ID_POINTENTRY:
1658 if ((res = ebml_read_master(matroska, &id)) < 0)
1661 /* in the end, we hope to fill one entry with a
1662 * timestamp, a file position and a tracknum */
1663 idx.pos = (uint64_t) -1;
1664 idx.time = (uint64_t) -1;
1665 idx.track = (uint16_t) -1;
1668 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1671 } else if (matroska->level_up) {
1672 matroska->level_up--;
1677 /* one single index entry ('point') */
1678 case MATROSKA_ID_CUETIME: {
1680 if ((res = ebml_read_uint(matroska, &id,
1683 idx.time = time * matroska->time_scale;
1687 /* position in the file + track to which it
1689 case MATROSKA_ID_CUETRACKPOSITION:
1690 if ((res = ebml_read_master(matroska, &id)) < 0)
1694 if (!(id = ebml_peek_id (matroska,
1695 &matroska->level_up))) {
1698 } else if (matroska->level_up) {
1699 matroska->level_up--;
1705 case MATROSKA_ID_CUETRACK: {
1707 if ((res = ebml_read_uint(matroska,
1714 /* position in file */
1715 case MATROSKA_ID_CUECLUSTERPOSITION: {
1717 if ((res = ebml_read_uint(matroska,
1725 av_log(matroska->ctx, AV_LOG_INFO,
1726 "Unknown entry 0x%x in "
1727 "CuesTrackPositions\n", id);
1731 res = ebml_read_skip(matroska);
1735 if (matroska->level_up) {
1736 matroska->level_up--;
1744 av_log(matroska->ctx, AV_LOG_INFO,
1745 "Unknown entry 0x%x in cuespoint "
1750 res = ebml_read_skip(matroska);
1754 if (matroska->level_up) {
1755 matroska->level_up--;
1760 /* so let's see if we got what we wanted */
1761 if (idx.pos != (uint64_t) -1 &&
1762 idx.time != (uint64_t) -1 &&
1763 idx.track != (uint16_t) -1) {
1764 if (matroska->num_indexes % 32 == 0) {
1765 /* re-allocate bigger index */
1767 av_realloc(matroska->index,
1768 (matroska->num_indexes + 32) *
1769 sizeof(MatroskaDemuxIndex));
1771 matroska->index[matroska->num_indexes] = idx;
1772 matroska->num_indexes++;
1777 av_log(matroska->ctx, AV_LOG_INFO,
1778 "Unknown entry 0x%x in cues header\n", id);
1782 res = ebml_read_skip(matroska);
1786 if (matroska->level_up) {
1787 matroska->level_up--;
1796 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1802 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1805 } else if (matroska->level_up) {
1806 matroska->level_up--;
1811 /* Hm, this is unsupported... */
1813 av_log(matroska->ctx, AV_LOG_INFO,
1814 "Unknown entry 0x%x in metadata header\n", id);
1818 res = ebml_read_skip(matroska);
1822 if (matroska->level_up) {
1823 matroska->level_up--;
1832 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1837 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1840 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1843 } else if (matroska->level_up) {
1844 matroska->level_up--;
1849 case MATROSKA_ID_SEEKENTRY: {
1850 uint32_t seek_id = 0, peek_id_cache = 0;
1851 uint64_t seek_pos = (uint64_t) -1, t;
1853 if ((res = ebml_read_master(matroska, &id)) < 0)
1857 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1860 } else if (matroska->level_up) {
1861 matroska->level_up--;
1866 case MATROSKA_ID_SEEKID:
1867 res = ebml_read_uint(matroska, &id, &t);
1871 case MATROSKA_ID_SEEKPOSITION:
1872 res = ebml_read_uint(matroska, &id, &seek_pos);
1876 av_log(matroska->ctx, AV_LOG_INFO,
1877 "Unknown seekhead ID 0x%x\n", id);
1881 res = ebml_read_skip(matroska);
1885 if (matroska->level_up) {
1886 matroska->level_up--;
1891 if (!seek_id || seek_pos == (uint64_t) -1) {
1892 av_log(matroska->ctx, AV_LOG_INFO,
1893 "Incomplete seekhead entry (0x%x/%llu)\n",
1899 case MATROSKA_ID_CUES:
1900 case MATROSKA_ID_TAGS: {
1901 uint32_t level_up = matroska->level_up;
1902 offset_t before_pos;
1904 MatroskaLevel level;
1906 /* remember the peeked ID and the current position */
1907 peek_id_cache = matroska->peek_id;
1908 before_pos = url_ftell(&matroska->ctx->pb);
1911 if ((res = ebml_read_seek(matroska, seek_pos +
1912 matroska->segment_start)) < 0)
1915 /* we don't want to lose our seekhead level, so we add
1916 * a dummy. This is a crude hack. */
1917 if (matroska->num_levels == EBML_MAX_DEPTH) {
1918 av_log(matroska->ctx, AV_LOG_INFO,
1919 "Max EBML element depth (%d) reached, "
1920 "cannot parse further.\n", EBML_MAX_DEPTH);
1921 return AVERROR_UNKNOWN;
1925 level.length = (uint64_t)-1;
1926 matroska->levels[matroska->num_levels] = level;
1927 matroska->num_levels++;
1930 if (!(id = ebml_peek_id (matroska,
1931 &matroska->level_up)))
1933 if (id != seek_id) {
1934 av_log(matroska->ctx, AV_LOG_INFO,
1935 "We looked for ID=0x%x but got "
1936 "ID=0x%x (pos=%llu)",
1937 seek_id, id, seek_pos +
1938 matroska->segment_start);
1942 /* read master + parse */
1943 if ((res = ebml_read_master(matroska, &id)) < 0)
1946 case MATROSKA_ID_CUES:
1947 if (!(res = matroska_parse_index(matroska)) ||
1948 url_feof(&matroska->ctx->pb)) {
1949 matroska->index_parsed = 1;
1953 case MATROSKA_ID_TAGS:
1954 if (!(res = matroska_parse_metadata(matroska)) ||
1955 url_feof(&matroska->ctx->pb)) {
1956 matroska->metadata_parsed = 1;
1965 /* remove dummy level */
1966 while (matroska->num_levels) {
1967 matroska->num_levels--;
1969 matroska->levels[matroska->num_levels].length;
1970 if (length == (uint64_t)-1)
1975 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1977 matroska->peek_id = peek_id_cache;
1978 matroska->level_up = level_up;
1983 av_log(matroska->ctx, AV_LOG_INFO,
1984 "Ignoring seekhead entry for ID=0x%x\n",
1993 av_log(matroska->ctx, AV_LOG_INFO,
1994 "Unknown seekhead ID 0x%x\n", id);
1998 res = ebml_read_skip(matroska);
2002 if (matroska->level_up) {
2003 matroska->level_up--;
2012 matroska_read_header (AVFormatContext *s,
2013 AVFormatParameters *ap)
2015 MatroskaDemuxContext *matroska = s->priv_data;
2017 int version, last_level, res = 0;
2022 /* First read the EBML header. */
2024 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2026 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2027 av_log(matroska->ctx, AV_LOG_ERROR,
2028 "Wrong EBML doctype ('%s' != 'matroska').\n",
2029 doctype ? doctype : "(none)");
2032 return AVERROR_NOFMT;
2036 av_log(matroska->ctx, AV_LOG_ERROR,
2037 "Matroska demuxer version 1 too old for file version %d\n",
2039 return AVERROR_NOFMT;
2042 /* The next thing is a segment. */
2044 if (!(id = ebml_peek_id(matroska, &last_level)))
2046 if (id == MATROSKA_ID_SEGMENT)
2050 av_log(matroska->ctx, AV_LOG_INFO,
2051 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2052 MATROSKA_ID_SEGMENT, id);
2053 if ((res = ebml_read_skip(matroska)) < 0)
2057 /* We now have a Matroska segment.
2058 * Seeks are from the beginning of the segment,
2059 * after the segment ID/length. */
2060 if ((res = ebml_read_master(matroska, &id)) < 0)
2062 matroska->segment_start = url_ftell(&s->pb);
2064 matroska->time_scale = 1000000;
2065 /* we've found our segment, start reading the different contents in here */
2067 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2070 } else if (matroska->level_up) {
2071 matroska->level_up--;
2077 case MATROSKA_ID_INFO: {
2078 if ((res = ebml_read_master(matroska, &id)) < 0)
2080 res = matroska_parse_info(matroska);
2084 /* track info headers */
2085 case MATROSKA_ID_TRACKS: {
2086 if ((res = ebml_read_master(matroska, &id)) < 0)
2088 res = matroska_parse_tracks(matroska);
2093 case MATROSKA_ID_CUES: {
2094 if (!matroska->index_parsed) {
2095 if ((res = ebml_read_master(matroska, &id)) < 0)
2097 res = matroska_parse_index(matroska);
2099 res = ebml_read_skip(matroska);
2104 case MATROSKA_ID_TAGS: {
2105 if (!matroska->metadata_parsed) {
2106 if ((res = ebml_read_master(matroska, &id)) < 0)
2108 res = matroska_parse_metadata(matroska);
2110 res = ebml_read_skip(matroska);
2114 /* file index (if seekable, seek to Cues/Tags to parse it) */
2115 case MATROSKA_ID_SEEKHEAD: {
2116 if ((res = ebml_read_master(matroska, &id)) < 0)
2118 res = matroska_parse_seekhead(matroska);
2122 case MATROSKA_ID_CLUSTER: {
2123 /* Do not read the master - this will be done in the next
2124 * call to matroska_read_packet. */
2130 av_log(matroska->ctx, AV_LOG_INFO,
2131 "Unknown matroska file header ID 0x%x\n", id);
2135 res = ebml_read_skip(matroska);
2139 if (matroska->level_up) {
2140 matroska->level_up--;
2148 /* Have we found a cluster? */
2151 enum CodecID codec_id;
2152 MatroskaTrack *track;
2155 for (i = 0; i < matroska->num_tracks; i++) {
2156 track = matroska->tracks[i];
2158 /* libavformat does not really support subtitles.
2159 * Also apply some sanity checks. */
2160 if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
2161 (track->codec_id == NULL))
2164 /* Set the FourCC from the CodecID. */
2165 /* This is the MS compatibility mode which stores a
2166 * BITMAPINFOHEADER in the CodecPrivate. */
2167 if (!strcmp(track->codec_id,
2168 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2169 (track->codec_priv_size >= 40) &&
2170 (track->codec_priv != NULL)) {
2173 /* Offset of biCompression. Stored in LE. */
2174 p = (unsigned char *)track->codec_priv + 16;
2175 ((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
2176 (p[2] << 16) | (p[1] << 8) | p[0];
2177 codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
2179 } else if (!strcmp(track->codec_id,
2180 MATROSKA_CODEC_ID_VIDEO_MPEG4_SP) ||
2181 !strcmp(track->codec_id,
2182 MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP) ||
2183 !strcmp(track->codec_id,
2184 MATROSKA_CODEC_ID_VIDEO_MPEG4_AP))
2185 codec_id = CODEC_ID_MPEG4;
2186 /* else if (!strcmp(track->codec_id, */
2187 /* MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED)) */
2188 /* codec_id = CODEC_ID_???; */
2189 else if (!strcmp(track->codec_id,
2190 MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3))
2191 codec_id = CODEC_ID_MSMPEG4V3;
2192 else if (!strcmp(track->codec_id,
2193 MATROSKA_CODEC_ID_VIDEO_MPEG1) ||
2194 !strcmp(track->codec_id,
2195 MATROSKA_CODEC_ID_VIDEO_MPEG2))
2196 codec_id = CODEC_ID_MPEG2VIDEO;
2198 /* This is the MS compatibility mode which stores a
2199 * WAVEFORMATEX in the CodecPrivate. */
2200 else if (!strcmp(track->codec_id,
2201 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2202 (track->codec_priv_size >= 18) &&
2203 (track->codec_priv != NULL)) {
2207 /* Offset of wFormatTag. Stored in LE. */
2208 p = (unsigned char *)track->codec_priv;
2209 tag = (p[1] << 8) | p[0];
2210 codec_id = codec_get_wav_id(tag);
2212 } else if (!strcmp(track->codec_id,
2213 MATROSKA_CODEC_ID_AUDIO_MPEG1_L1) ||
2214 !strcmp(track->codec_id,
2215 MATROSKA_CODEC_ID_AUDIO_MPEG1_L2) ||
2216 !strcmp(track->codec_id,
2217 MATROSKA_CODEC_ID_AUDIO_MPEG1_L3))
2218 codec_id = CODEC_ID_MP3;
2219 else if (!strcmp(track->codec_id,
2220 MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE))
2221 codec_id = CODEC_ID_PCM_U16BE;
2222 else if (!strcmp(track->codec_id,
2223 MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE))
2224 codec_id = CODEC_ID_PCM_U16LE;
2225 /* else if (!strcmp(track->codec_id, */
2226 /* MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT)) */
2227 /* codec_id = CODEC_ID_PCM_???; */
2228 else if (!strcmp(track->codec_id,
2229 MATROSKA_CODEC_ID_AUDIO_AC3))
2230 codec_id = CODEC_ID_AC3;
2231 else if (!strcmp(track->codec_id,
2232 MATROSKA_CODEC_ID_AUDIO_DTS))
2233 codec_id = CODEC_ID_DTS;
2234 /* No such codec id so far. */
2235 /* else if (!strcmp(track->codec_id, */
2236 /* MATROSKA_CODEC_ID_AUDIO_DTS)) */
2237 /* codec_id = CODEC_ID_DTS; */
2238 else if (!strcmp(track->codec_id,
2239 MATROSKA_CODEC_ID_AUDIO_VORBIS))
2240 codec_id = CODEC_ID_VORBIS;
2241 else if (!strcmp(track->codec_id,
2242 MATROSKA_CODEC_ID_AUDIO_MPEG2) ||
2243 !strcmp(track->codec_id,
2244 MATROSKA_CODEC_ID_AUDIO_MPEG4))
2245 codec_id = CODEC_ID_AAC;
2247 codec_id = CODEC_ID_NONE;
2249 if (codec_id == CODEC_ID_NONE) {
2250 av_log(matroska->ctx, AV_LOG_INFO,
2251 "Unknown/unsupported CodecID %s.\n",
2255 track->stream_index = matroska->num_streams;
2257 matroska->num_streams++;
2258 st = av_new_stream(s, track->stream_index);
2260 return AVERROR_NOMEM;
2261 av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
2263 st->codec.codec_id = codec_id;
2265 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2266 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2268 st->codec.codec_type = CODEC_TYPE_VIDEO;
2269 st->codec.codec_tag = videotrack->fourcc;
2270 st->codec.width = videotrack->pixel_width;
2271 st->codec.height = videotrack->pixel_height;
2272 if (videotrack->display_width == 0)
2273 st->codec.sample_aspect_ratio.num =
2274 videotrack->pixel_width;
2276 st->codec.sample_aspect_ratio.num =
2277 videotrack->display_width;
2278 if (videotrack->display_height == 0)
2279 st->codec.sample_aspect_ratio.num =
2280 videotrack->pixel_height;
2282 st->codec.sample_aspect_ratio.num =
2283 videotrack->display_height;
2285 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2286 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2288 st->codec.codec_type = CODEC_TYPE_AUDIO;
2289 st->codec.sample_rate = audiotrack->samplerate;
2290 st->codec.channels = audiotrack->channels;
2293 /* What do we do with private data? E.g. for Vorbis. */
2301 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
2306 for (i = 0; i < matroska->num_tracks; i++)
2307 if (matroska->tracks[i]->num == num)
2314 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2315 uint64_t cluster_time)
2320 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2322 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2325 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2328 } else if (matroska->level_up) {
2329 matroska->level_up--;
2334 /* one block inside the group. Note, block parsing is one
2335 * of the harder things, so this code is a bit complicated.
2336 * See http://www.matroska.org/ for documentation. */
2337 case MATROSKA_ID_BLOCK: {
2338 uint8_t *data, *origdata;
2341 uint32_t *lace_size = NULL;
2342 int n, track, flags, laces = 0;
2345 if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
2349 /* first byte(s): blocknum */
2350 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2351 av_log(matroska->ctx, AV_LOG_ERROR,
2352 "EBML block data error\n");
2359 /* fetch track from num */
2360 track = matroska_find_track_by_num(matroska, num);
2361 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2362 av_log(matroska->ctx, AV_LOG_INFO,
2363 "Invalid stream %d or size %u\n", track, size);
2368 /* time (relative to cluster time) */
2369 time = ((data[0] << 8) | data[1]) * matroska->time_scale;
2375 switch ((flags & 0x06) >> 1) {
2376 case 0x0: /* no lacing */
2378 lace_size = av_mallocz(sizeof(int));
2379 lace_size[0] = size;
2382 case 0x1: /* xiph lacing */
2383 case 0x2: /* fixed-size lacing */
2384 case 0x3: /* EBML lacing */
2389 laces = (*data) + 1;
2392 lace_size = av_mallocz(laces * sizeof(int));
2394 switch ((flags & 0x06) >> 1) {
2395 case 0x1: /* xiph lacing */ {
2398 for (n = 0; res == 0 && n < laces - 1; n++) {
2405 lace_size[n] += temp;
2411 total += lace_size[n];
2413 lace_size[n] = size - total;
2417 case 0x2: /* fixed-size lacing */
2418 for (n = 0; n < laces; n++)
2419 lace_size[n] = size / laces;
2422 case 0x3: /* EBML lacing */ {
2424 n = matroska_ebmlnum_uint(data, size, &num);
2426 av_log(matroska->ctx, AV_LOG_INFO,
2427 "EBML block data error\n");
2432 total = lace_size[0] = num;
2433 for (n = 1; res == 0 && n < laces - 1; n++) {
2436 r = matroska_ebmlnum_sint (data, size,
2439 av_log(matroska->ctx, AV_LOG_INFO,
2440 "EBML block data error\n");
2445 lace_size[n] = lace_size[n - 1] + snum;
2446 total += lace_size[n];
2448 lace_size[n] = size - total;
2456 for (n = 0; n < laces; n++) {
2457 uint64_t timecode = 0;
2459 pkt = av_mallocz(sizeof(AVPacket));
2460 /* XXX: prevent data copy... */
2461 if (av_new_packet(pkt,lace_size[n]) < 0) {
2462 res = AVERROR_NOMEM;
2465 if (cluster_time != (uint64_t)-1) {
2466 if (time < 0 && (-time) > cluster_time)
2467 timecode = cluster_time;
2469 timecode = cluster_time + time;
2471 /* FIXME: duration */
2473 memcpy(pkt->data, data, lace_size[n]);
2474 data += lace_size[n];
2476 pkt->flags = is_keyframe;
2478 matroska->tracks[track]->stream_index;
2480 pkt->pts = timecode / 1000000; /* ns to ms */
2482 matroska_queue_packet(matroska, pkt);
2491 case MATROSKA_ID_BLOCKDURATION: {
2493 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2495 av_log(matroska->ctx, AV_LOG_INFO,
2496 "FIXME: implement support for BlockDuration\n");
2500 case MATROSKA_ID_BLOCKREFERENCE:
2501 /* We've found a reference, so not even the first frame in
2502 * the lace is a key frame. */
2504 if (last_num_packets != matroska->num_packets)
2505 matroska->packets[last_num_packets]->flags = 0;
2506 res = ebml_read_skip(matroska);
2510 av_log(matroska->ctx, AV_LOG_INFO,
2511 "Unknown entry 0x%x in blockgroup data\n", id);
2515 res = ebml_read_skip(matroska);
2519 if (matroska->level_up) {
2520 matroska->level_up--;
2529 matroska_parse_cluster (MatroskaDemuxContext *matroska)
2533 uint64_t cluster_time = 0;
2535 av_log(matroska->ctx, AV_LOG_DEBUG,
2536 "parsing cluster at %lld\n", url_ftell(&matroska->ctx->pb));
2539 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2542 } else if (matroska->level_up) {
2543 matroska->level_up--;
2548 /* cluster timecode */
2549 case MATROSKA_ID_CLUSTERTIMECODE: {
2551 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2553 cluster_time = num * matroska->time_scale;
2557 /* a group of blocks inside a cluster */
2558 case MATROSKA_ID_BLOCKGROUP:
2559 if ((res = ebml_read_master(matroska, &id)) < 0)
2561 res = matroska_parse_blockgroup(matroska, cluster_time);
2565 av_log(matroska->ctx, AV_LOG_INFO,
2566 "Unknown entry 0x%x in cluster data\n", id);
2570 res = ebml_read_skip(matroska);
2574 if (matroska->level_up) {
2575 matroska->level_up--;
2584 matroska_read_packet (AVFormatContext *s,
2587 MatroskaDemuxContext *matroska = s->priv_data;
2591 /* Do we still have a packet queued? */
2592 if (matroska_deliver_packet(matroska, pkt) == 0)
2595 /* Have we already reached the end? */
2600 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2603 } else if (matroska->level_up) {
2604 matroska->level_up--;
2609 case MATROSKA_ID_CLUSTER:
2610 if ((res = ebml_read_master(matroska, &id)) < 0)
2612 if ((res = matroska_parse_cluster(matroska)) == 0)
2613 res = 1; /* Parsed one cluster, let's get out. */
2618 res = ebml_read_skip(matroska);
2622 if (matroska->level_up) {
2623 matroska->level_up--;
2631 return matroska_deliver_packet(matroska, pkt);
2635 matroska_read_close (AVFormatContext *s)
2637 MatroskaDemuxContext *matroska = s->priv_data;
2640 if (matroska->writing_app)
2641 av_free(matroska->writing_app);
2642 if (matroska->muxing_app)
2643 av_free(matroska->muxing_app);
2644 if (matroska->index)
2645 av_free(matroska->index);
2647 if (matroska->packets != NULL) {
2648 for (n = 0; n < matroska->num_packets; n++) {
2649 av_free_packet(matroska->packets[n]);
2650 av_free(matroska->packets[n]);
2652 av_free(matroska->packets);
2655 for (n = 0; n < matroska->num_tracks; n++) {
2656 MatroskaTrack *track = matroska->tracks[n];
2657 if (track->codec_id)
2658 av_free(track->codec_id);
2659 if (track->codec_name)
2660 av_free(track->codec_name);
2661 if (track->codec_priv)
2662 av_free(track->codec_priv);
2664 av_free(track->name);
2665 if (track->language)
2666 av_free(track->language);
2671 memset(matroska, 0, sizeof(MatroskaDemuxContext));
2676 static AVInputFormat matroska_iformat = {
2678 "Matroska file format",
2679 sizeof(MatroskaDemuxContext),
2681 matroska_read_header,
2682 matroska_read_packet,
2683 matroska_read_close,
2689 av_register_input_format(&matroska_iformat);