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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. */
32 #include "intfloat_readwrite.h"
34 /* EBML version supported */
35 #define EBML_VERSION 1
37 /* top-level master-IDs */
38 #define EBML_ID_HEADER 0x1A45DFA3
40 /* IDs in the HEADER master */
41 #define EBML_ID_EBMLVERSION 0x4286
42 #define EBML_ID_EBMLREADVERSION 0x42F7
43 #define EBML_ID_EBMLMAXIDLENGTH 0x42F2
44 #define EBML_ID_EBMLMAXSIZELENGTH 0x42F3
45 #define EBML_ID_DOCTYPE 0x4282
46 #define EBML_ID_DOCTYPEVERSION 0x4287
47 #define EBML_ID_DOCTYPEREADVERSION 0x4285
49 /* general EBML types */
50 #define EBML_ID_VOID 0xEC
53 * Matroska element IDs. max. 32-bit.
56 /* toplevel segment */
57 #define MATROSKA_ID_SEGMENT 0x18538067
59 /* matroska top-level master IDs */
60 #define MATROSKA_ID_INFO 0x1549A966
61 #define MATROSKA_ID_TRACKS 0x1654AE6B
62 #define MATROSKA_ID_CUES 0x1C53BB6B
63 #define MATROSKA_ID_TAGS 0x1254C367
64 #define MATROSKA_ID_SEEKHEAD 0x114D9B74
65 #define MATROSKA_ID_CLUSTER 0x1F43B675
67 /* IDs in the info master */
68 #define MATROSKA_ID_TIMECODESCALE 0x2AD7B1
69 #define MATROSKA_ID_DURATION 0x4489
70 #define MATROSKA_ID_WRITINGAPP 0x5741
71 #define MATROSKA_ID_MUXINGAPP 0x4D80
72 #define MATROSKA_ID_DATEUTC 0x4461
74 /* ID in the tracks master */
75 #define MATROSKA_ID_TRACKENTRY 0xAE
77 /* IDs in the trackentry master */
78 #define MATROSKA_ID_TRACKNUMBER 0xD7
79 #define MATROSKA_ID_TRACKUID 0x73C5
80 #define MATROSKA_ID_TRACKTYPE 0x83
81 #define MATROSKA_ID_TRACKAUDIO 0xE1
82 #define MATROSKA_ID_TRACKVIDEO 0xE0
83 #define MATROSKA_ID_CODECID 0x86
84 #define MATROSKA_ID_CODECPRIVATE 0x63A2
85 #define MATROSKA_ID_CODECNAME 0x258688
86 #define MATROSKA_ID_CODECINFOURL 0x3B4040
87 #define MATROSKA_ID_CODECDOWNLOADURL 0x26B240
88 #define MATROSKA_ID_TRACKNAME 0x536E
89 #define MATROSKA_ID_TRACKLANGUAGE 0x22B59C
90 #define MATROSKA_ID_TRACKFLAGENABLED 0xB9
91 #define MATROSKA_ID_TRACKFLAGDEFAULT 0x88
92 #define MATROSKA_ID_TRACKFLAGLACING 0x9C
93 #define MATROSKA_ID_TRACKMINCACHE 0x6DE7
94 #define MATROSKA_ID_TRACKMAXCACHE 0x6DF8
95 #define MATROSKA_ID_TRACKDEFAULTDURATION 0x23E383
97 /* IDs in the trackvideo master */
98 #define MATROSKA_ID_VIDEOFRAMERATE 0x2383E3
99 #define MATROSKA_ID_VIDEODISPLAYWIDTH 0x54B0
100 #define MATROSKA_ID_VIDEODISPLAYHEIGHT 0x54BA
101 #define MATROSKA_ID_VIDEOPIXELWIDTH 0xB0
102 #define MATROSKA_ID_VIDEOPIXELHEIGHT 0xBA
103 #define MATROSKA_ID_VIDEOFLAGINTERLACED 0x9A
104 #define MATROSKA_ID_VIDEOSTEREOMODE 0x53B9
105 #define MATROSKA_ID_VIDEOASPECTRATIO 0x54B3
106 #define MATROSKA_ID_VIDEOCOLOURSPACE 0x2EB524
108 /* IDs in the trackaudio master */
109 #define MATROSKA_ID_AUDIOSAMPLINGFREQ 0xB5
110 #define MATROSKA_ID_AUDIOBITDEPTH 0x6264
111 #define MATROSKA_ID_AUDIOCHANNELS 0x9F
113 /* ID in the cues master */
114 #define MATROSKA_ID_POINTENTRY 0xBB
116 /* IDs in the pointentry master */
117 #define MATROSKA_ID_CUETIME 0xB3
118 #define MATROSKA_ID_CUETRACKPOSITION 0xB7
120 /* IDs in the cuetrackposition master */
121 #define MATROSKA_ID_CUETRACK 0xF7
122 #define MATROSKA_ID_CUECLUSTERPOSITION 0xF1
124 /* IDs in the tags master */
127 /* IDs in the seekhead master */
128 #define MATROSKA_ID_SEEKENTRY 0x4DBB
130 /* IDs in the seekpoint master */
131 #define MATROSKA_ID_SEEKID 0x53AB
132 #define MATROSKA_ID_SEEKPOSITION 0x53AC
134 /* IDs in the cluster master */
135 #define MATROSKA_ID_CLUSTERTIMECODE 0xE7
136 #define MATROSKA_ID_BLOCKGROUP 0xA0
138 /* IDs in the blockgroup master */
139 #define MATROSKA_ID_BLOCK 0xA1
140 #define MATROSKA_ID_BLOCKDURATION 0x9B
141 #define MATROSKA_ID_BLOCKREFERENCE 0xFB
144 MATROSKA_TRACK_TYPE_VIDEO = 0x1,
145 MATROSKA_TRACK_TYPE_AUDIO = 0x2,
146 MATROSKA_TRACK_TYPE_COMPLEX = 0x3,
147 MATROSKA_TRACK_TYPE_LOGO = 0x10,
148 MATROSKA_TRACK_TYPE_SUBTITLE = 0x11,
149 MATROSKA_TRACK_TYPE_CONTROL = 0x20,
153 MATROSKA_EYE_MODE_MONO = 0x0,
154 MATROSKA_EYE_MODE_RIGHT = 0x1,
155 MATROSKA_EYE_MODE_LEFT = 0x2,
156 MATROSKA_EYE_MODE_BOTH = 0x3,
160 MATROSKA_ASPECT_RATIO_MODE_FREE = 0x0,
161 MATROSKA_ASPECT_RATIO_MODE_KEEP = 0x1,
162 MATROSKA_ASPECT_RATIO_MODE_FIXED = 0x2,
163 } MatroskaAspectRatioMode;
166 * These aren't in any way "matroska-form" things,
167 * it's just something I use in the muxer/demuxer.
171 MATROSKA_TRACK_ENABLED = (1<<0),
172 MATROSKA_TRACK_DEFAULT = (1<<1),
173 MATROSKA_TRACK_LACING = (1<<2),
174 MATROSKA_TRACK_SHIFT = (1<<16)
175 } MatroskaTrackFlags;
178 MATROSKA_VIDEOTRACK_INTERLACED = (MATROSKA_TRACK_SHIFT<<0)
179 } MatroskaVideoTrackFlags;
182 * Matroska Codec IDs. Strings.
185 typedef struct CodecTags{
190 #define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC "V_MS/VFW/FOURCC"
191 #define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
193 static CodecTags codec_tags[]={
194 // {"V_MS/VFW/FOURCC" , CODEC_ID_NONE},
195 {"V_UNCOMPRESSED" , CODEC_ID_RAWVIDEO},
196 {"V_MPEG4/ISO/SP" , CODEC_ID_MPEG4},
197 {"V_MPEG4/ISO/ASP" , CODEC_ID_MPEG4},
198 {"V_MPEG4/ISO/AP" , CODEC_ID_MPEG4},
199 {"V_MPEG4/ISO/AVC" , CODEC_ID_H264},
200 {"V_MPEG4/MS/V3" , CODEC_ID_MSMPEG4V3},
201 {"V_MPEG1" , CODEC_ID_MPEG1VIDEO},
202 {"V_MPEG2" , CODEC_ID_MPEG2VIDEO},
203 {"V_MJPEG" , CODEC_ID_MJPEG},
204 {"V_REAL/RV10" , CODEC_ID_RV10},
205 {"V_REAL/RV20" , CODEC_ID_RV20},
206 {"V_REAL/RV30" , CODEC_ID_RV30},
207 {"V_REAL/RV40" , CODEC_ID_RV40},
208 /* TODO: Real/Quicktime */
210 // {"A_MS/ACM" , CODEC_ID_NONE},
211 {"A_MPEG/L1" , CODEC_ID_MP3},
212 {"A_MPEG/L2" , CODEC_ID_MP3},
213 {"A_MPEG/L3" , CODEC_ID_MP3},
214 {"A_PCM/INT/BIG" , CODEC_ID_PCM_U16BE},
215 {"A_PCM/INT/LIT" , CODEC_ID_PCM_U16LE},
216 // {"A_PCM/FLOAT/IEEE" , CODEC_ID_NONE},
217 {"A_AC3" , CODEC_ID_AC3},
218 {"A_DTS" , CODEC_ID_DTS},
219 {"A_VORBIS" , CODEC_ID_VORBIS},
220 {"A_AAC/MPEG2/" , CODEC_ID_AAC},
221 {"A_AAC/MPEG4/" , CODEC_ID_AAC},
222 {NULL , CODEC_ID_NONE}
223 /* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
226 /* max. depth in the EBML tree structure */
227 #define EBML_MAX_DEPTH 16
229 typedef struct Track {
230 MatroskaTrackType type;
232 /* Unique track number and track ID. stream_index is the index that
233 * the calling app uses for this track. */
244 unsigned char *codec_priv;
247 int64_t default_duration;
248 MatroskaTrackFlags flags;
251 typedef struct MatroskaVideoTrack {
261 MatroskaAspectRatioMode ar_mode;
262 MatroskaEyeMode eye_mode;
265 } MatroskaVideoTrack;
267 typedef struct MatroskaAudioTrack {
274 } MatroskaAudioTrack;
276 typedef struct MatroskaSubtitleTrack {
280 } MatroskaSubtitleTrack;
282 typedef struct MatroskaLevel {
283 uint64_t start, length;
286 typedef struct MatroskaDemuxIndex {
287 uint64_t pos; /* of the corresponding *cluster*! */
288 uint16_t track; /* reference to 'num' */
289 uint64_t time; /* in nanoseconds */
290 } MatroskaDemuxIndex;
292 typedef struct MatroskaDemuxContext {
293 AVFormatContext *ctx;
297 MatroskaLevel levels[EBML_MAX_DEPTH];
305 /* timescale in the file */
308 /* position (time, ns) */
311 /* num_streams is the number of streams that av_new_stream() was called
312 * for ( = that are available to the calling program). */
313 int num_tracks, num_streams;
314 MatroskaTrack *tracks[MAX_STREAMS];
316 /* cache for ID peeking */
319 /* byte position of the segment inside the stream */
320 offset_t segment_start;
322 /* The packet queue. */
326 /* have we already parse metadata/cues/clusters? */
331 /* The index for seeking. */
333 MatroskaDemuxIndex *index;
334 } MatroskaDemuxContext;
337 * The first few functions handle EBML file parsing. The rest
338 * is the document interpretation. Matroska really just is a
343 * Return: the amount of levels in the hierarchy that the
344 * current element lies higher than the previous one.
345 * The opposite isn't done - that's auto-done using master
350 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
352 ByteIOContext *pb = &matroska->ctx->pb;
353 offset_t pos = url_ftell(pb);
356 while (matroska->num_levels > 0) {
357 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
359 if (pos >= level->start + level->length) {
360 matroska->num_levels--;
371 * Read: an "EBML number", which is defined as a variable-length
372 * array of bytes. The first byte indicates the length by giving a
373 * number of 0-bits followed by a one. The position of the first
374 * "one" bit inside the first byte indicates the length of this
376 * Returns: num. of bytes read. < 0 on error.
380 ebml_read_num (MatroskaDemuxContext *matroska,
384 ByteIOContext *pb = &matroska->ctx->pb;
385 int len_mask = 0x80, read = 1, n = 1;
388 /* the first byte tells us the length in bytes - get_byte() can normally
389 * return 0, but since that's not a valid first ebmlID byte, we can
390 * use it safely here to catch EOS. */
391 if (!(total = get_byte(pb))) {
392 /* we might encounter EOS here */
394 offset_t pos = url_ftell(pb);
395 av_log(matroska->ctx, AV_LOG_ERROR,
396 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
399 return AVERROR_IO; /* EOS or actual I/O error */
402 /* get the length of the EBML number */
403 while (read <= max_size && !(total & len_mask)) {
407 if (read > max_size) {
408 offset_t pos = url_ftell(pb) - 1;
409 av_log(matroska->ctx, AV_LOG_ERROR,
410 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
411 (uint8_t) total, pos, pos);
412 return AVERROR_INVALIDDATA;
415 /* read out length */
418 total = (total << 8) | get_byte(pb);
426 * Read: the element content data ID.
427 * Return: the number of bytes read or < 0 on error.
431 ebml_read_element_id (MatroskaDemuxContext *matroska,
438 /* if we re-call this, use our cached ID */
439 if (matroska->peek_id != 0) {
442 *id = matroska->peek_id;
446 /* read out the "EBML number", include tag in ID */
447 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
449 *id = matroska->peek_id = total | (1 << (read * 7));
453 *level_up = ebml_read_element_level_up(matroska);
459 * Read: element content length.
460 * Return: the number of bytes read or < 0 on error.
464 ebml_read_element_length (MatroskaDemuxContext *matroska,
467 /* clear cache since we're now beyond that data point */
468 matroska->peek_id = 0;
470 /* read out the "EBML number", include tag in ID */
471 return ebml_read_num(matroska, 8, length);
475 * Return: the ID of the next element, or 0 on error.
476 * Level_up contains the amount of levels that this
477 * next element lies higher than the previous one.
481 ebml_peek_id (MatroskaDemuxContext *matroska,
486 assert(level_up != NULL);
488 if (ebml_read_element_id(matroska, &id, level_up) < 0)
495 * Seek to a given offset.
496 * 0 is success, -1 is failure.
500 ebml_read_seek (MatroskaDemuxContext *matroska,
503 ByteIOContext *pb = &matroska->ctx->pb;
505 /* clear ID cache, if any */
506 matroska->peek_id = 0;
508 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
512 * Skip the next element.
513 * 0 is success, -1 is failure.
517 ebml_read_skip (MatroskaDemuxContext *matroska)
519 ByteIOContext *pb = &matroska->ctx->pb;
524 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
525 (res = ebml_read_element_length(matroska, &length)) < 0)
528 url_fskip(pb, length);
534 * Read the next element as an unsigned int.
535 * 0 is success, < 0 is failure.
539 ebml_read_uint (MatroskaDemuxContext *matroska,
543 ByteIOContext *pb = &matroska->ctx->pb;
544 int n = 0, size, res;
547 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
548 (res = ebml_read_element_length(matroska, &rlength)) < 0)
551 if (size < 1 || size > 8) {
552 offset_t pos = url_ftell(pb);
553 av_log(matroska->ctx, AV_LOG_ERROR,
554 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
556 return AVERROR_INVALIDDATA;
559 /* big-endian ordening; build up number */
562 *num = (*num << 8) | get_byte(pb);
568 * Read the next element as a signed int.
569 * 0 is success, < 0 is failure.
573 ebml_read_sint (MatroskaDemuxContext *matroska,
577 ByteIOContext *pb = &matroska->ctx->pb;
578 int size, n = 1, negative = 0, res;
581 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
582 (res = ebml_read_element_length(matroska, &rlength)) < 0)
585 if (size < 1 || size > 8) {
586 offset_t pos = url_ftell(pb);
587 av_log(matroska->ctx, AV_LOG_ERROR,
588 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
590 return AVERROR_INVALIDDATA;
592 if ((*num = get_byte(pb)) & 0x80) {
598 *num = (*num << 8) | get_byte(pb);
602 *num = *num - (1LL << ((8 * size) - 1));
608 * Read the next element as a float.
609 * 0 is success, < 0 is failure.
613 ebml_read_float (MatroskaDemuxContext *matroska,
617 ByteIOContext *pb = &matroska->ctx->pb;
621 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
622 (res = ebml_read_element_length(matroska, &rlength)) < 0)
627 *num= av_int2flt(get_be32(pb));
629 *num= av_int2dbl(get_be64(pb));
631 av_log(matroska->ctx, AV_LOG_ERROR,
632 "FIXME! 10-byte floats unimplemented\n");
633 return AVERROR_UNKNOWN;
635 offset_t pos = url_ftell(pb);
636 av_log(matroska->ctx, AV_LOG_ERROR,
637 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
639 return AVERROR_INVALIDDATA;
646 * Read the next element as an ASCII string.
647 * 0 is success, < 0 is failure.
651 ebml_read_ascii (MatroskaDemuxContext *matroska,
655 ByteIOContext *pb = &matroska->ctx->pb;
659 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
660 (res = ebml_read_element_length(matroska, &rlength)) < 0)
664 /* ebml strings are usually not 0-terminated, so we allocate one
665 * byte more, read the string and NULL-terminate it ourselves. */
666 if (size < 0 || !(*str = av_malloc(size + 1))) {
667 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
668 return AVERROR_NOMEM;
670 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
671 offset_t pos = url_ftell(pb);
672 av_log(matroska->ctx, AV_LOG_ERROR,
673 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
682 * Read the next element as a UTF-8 string.
683 * 0 is success, < 0 is failure.
687 ebml_read_utf8 (MatroskaDemuxContext *matroska,
691 return ebml_read_ascii(matroska, id, str);
695 * Read the next element as a date (nanoseconds since 1/1/2000).
696 * 0 is success, < 0 is failure.
700 ebml_read_date (MatroskaDemuxContext *matroska,
704 return ebml_read_sint(matroska, id, date);
708 * Read the next element, but only the header. The contents
709 * are supposed to be sub-elements which can be read separately.
710 * 0 is success, < 0 is failure.
714 ebml_read_master (MatroskaDemuxContext *matroska,
717 ByteIOContext *pb = &matroska->ctx->pb;
719 MatroskaLevel *level;
722 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
723 (res = ebml_read_element_length(matroska, &length)) < 0)
726 /* protect... (Heaven forbids that the '>' is true) */
727 if (matroska->num_levels >= EBML_MAX_DEPTH) {
728 av_log(matroska->ctx, AV_LOG_ERROR,
729 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
730 return AVERROR_NOTSUPP;
734 level = &matroska->levels[matroska->num_levels++];
735 level->start = url_ftell(pb);
736 level->length = length;
742 * Read the next element as binary data.
743 * 0 is success, < 0 is failure.
747 ebml_read_binary (MatroskaDemuxContext *matroska,
752 ByteIOContext *pb = &matroska->ctx->pb;
756 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
757 (res = ebml_read_element_length(matroska, &rlength)) < 0)
761 if (!(*binary = av_malloc(*size))) {
762 av_log(matroska->ctx, AV_LOG_ERROR,
763 "Memory allocation error\n");
764 return AVERROR_NOMEM;
767 if (get_buffer(pb, *binary, *size) != *size) {
768 offset_t pos = url_ftell(pb);
769 av_log(matroska->ctx, AV_LOG_ERROR,
770 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
778 * Read signed/unsigned "EBML" numbers.
779 * Return: number of bytes processed, < 0 on error.
780 * XXX: use ebml_read_num().
784 matroska_ebmlnum_uint (uint8_t *data,
788 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
792 return AVERROR_INVALIDDATA;
795 while (read <= 8 && !(total & len_mask)) {
800 return AVERROR_INVALIDDATA;
802 if ((total &= (len_mask - 1)) == len_mask - 1)
805 return AVERROR_INVALIDDATA;
809 total = (total << 8) | data[n];
822 * Same as above, but signed.
826 matroska_ebmlnum_sint (uint8_t *data,
833 /* read as unsigned number first */
834 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
837 /* make signed (weird way) */
838 if (unum == (uint64_t)-1)
841 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
847 * Read an EBML header.
848 * 0 is success, < 0 is failure.
852 ebml_read_header (MatroskaDemuxContext *matroska,
857 int level_up, res = 0;
865 if (!(id = ebml_peek_id(matroska, &level_up)) ||
866 level_up != 0 || id != EBML_ID_HEADER) {
867 av_log(matroska->ctx, AV_LOG_ERROR,
868 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
869 return AVERROR_INVALIDDATA;
871 if ((res = ebml_read_master(matroska, &id)) < 0)
875 if (!(id = ebml_peek_id(matroska, &level_up)))
883 /* is our read version uptodate? */
884 case EBML_ID_EBMLREADVERSION: {
887 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
889 if (num > EBML_VERSION) {
890 av_log(matroska->ctx, AV_LOG_ERROR,
891 "EBML version %"PRIu64" (> %d) is not supported\n",
893 return AVERROR_INVALIDDATA;
898 /* we only handle 8 byte lengths at max */
899 case EBML_ID_EBMLMAXSIZELENGTH: {
902 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
904 if (num > sizeof(uint64_t)) {
905 av_log(matroska->ctx, AV_LOG_ERROR,
906 "Integers of size %"PRIu64" (> %zd) not supported\n",
907 num, sizeof(uint64_t));
908 return AVERROR_INVALIDDATA;
913 /* we handle 4 byte IDs at max */
914 case EBML_ID_EBMLMAXIDLENGTH: {
917 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
919 if (num > sizeof(uint32_t)) {
920 av_log(matroska->ctx, AV_LOG_ERROR,
921 "IDs of size %"PRIu64" (> %zu) not supported\n",
922 num, sizeof(uint32_t));
923 return AVERROR_INVALIDDATA;
928 case EBML_ID_DOCTYPE: {
931 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
942 case EBML_ID_DOCTYPEREADVERSION: {
945 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
953 av_log(matroska->ctx, AV_LOG_INFO,
954 "Unknown data type 0x%x in EBML header", id);
958 /* we ignore these two, as they don't tell us anything we
960 case EBML_ID_EBMLVERSION:
961 case EBML_ID_DOCTYPEVERSION:
962 res = ebml_read_skip (matroska);
971 * Put one packet in an application-supplied AVPacket struct.
972 * Returns 0 on success or -1 on failure.
976 matroska_deliver_packet (MatroskaDemuxContext *matroska,
979 if (matroska->num_packets > 0) {
980 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
981 av_free(matroska->packets[0]);
982 if (matroska->num_packets > 1) {
983 memmove(&matroska->packets[0], &matroska->packets[1],
984 (matroska->num_packets - 1) * sizeof(AVPacket *));
986 av_realloc(matroska->packets, (matroska->num_packets - 1) *
989 av_free(matroska->packets);
990 matroska->packets = NULL;
992 matroska->num_packets--;
1000 * Put a packet into our internal queue. Will be delivered to the
1001 * user/application during the next get_packet() call.
1005 matroska_queue_packet (MatroskaDemuxContext *matroska,
1009 av_realloc(matroska->packets, (matroska->num_packets + 1) *
1010 sizeof(AVPacket *));
1011 matroska->packets[matroska->num_packets] = pkt;
1012 matroska->num_packets++;
1020 matroska_probe (AVProbeData *p)
1023 int len_mask = 0x80, size = 1, n = 1;
1024 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
1026 if (p->buf_size < 5)
1030 if ((p->buf[0] << 24 | p->buf[1] << 16 |
1031 p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
1034 /* length of header */
1036 while (size <= 8 && !(total & len_mask)) {
1042 total &= (len_mask - 1);
1044 total = (total << 8) | p->buf[4 + n++];
1046 /* does the probe data contain the whole header? */
1047 if (p->buf_size < 4 + size + total)
1050 /* the header must contain the document type 'matroska'. For now,
1051 * we don't parse the whole header but simply check for the
1052 * availability of that array of characters inside the header.
1053 * Not fully fool-proof, but good enough. */
1054 for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
1055 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
1056 return AVPROBE_SCORE_MAX;
1062 * From here on, it's all XML-style DTD stuff... Needs no comments.
1066 matroska_parse_info (MatroskaDemuxContext *matroska)
1071 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
1074 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1077 } else if (matroska->level_up) {
1078 matroska->level_up--;
1083 /* cluster timecode */
1084 case MATROSKA_ID_TIMECODESCALE: {
1086 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1088 matroska->time_scale = num;
1092 case MATROSKA_ID_DURATION: {
1094 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1096 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1100 case MATROSKA_ID_WRITINGAPP: {
1102 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1104 matroska->writing_app = text;
1108 case MATROSKA_ID_MUXINGAPP: {
1110 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1112 matroska->muxing_app = text;
1116 case MATROSKA_ID_DATEUTC: {
1118 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
1120 matroska->created = time;
1125 av_log(matroska->ctx, AV_LOG_INFO,
1126 "Unknown entry 0x%x in info header\n", id);
1130 res = ebml_read_skip(matroska);
1134 if (matroska->level_up) {
1135 matroska->level_up--;
1144 matroska_add_stream (MatroskaDemuxContext *matroska)
1148 MatroskaTrack *track;
1150 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1152 /* Allocate a generic track. As soon as we know its type we'll realloc. */
1153 track = av_mallocz(sizeof(MatroskaTrack));
1154 matroska->num_tracks++;
1156 /* start with the master */
1157 if ((res = ebml_read_master(matroska, &id)) < 0)
1160 /* try reading the trackentry headers */
1162 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1165 } else if (matroska->level_up > 0) {
1166 matroska->level_up--;
1171 /* track number (unique stream ID) */
1172 case MATROSKA_ID_TRACKNUMBER: {
1174 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1180 /* track UID (unique identifier) */
1181 case MATROSKA_ID_TRACKUID: {
1183 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1189 /* track type (video, audio, combined, subtitle, etc.) */
1190 case MATROSKA_ID_TRACKTYPE: {
1192 if (track->type != 0) {
1193 av_log(matroska->ctx, AV_LOG_INFO,
1194 "More than one tracktype in an entry - skip\n");
1197 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1201 /* ok, so we're actually going to reallocate this thing */
1202 switch (track->type) {
1203 case MATROSKA_TRACK_TYPE_VIDEO:
1204 track = (MatroskaTrack *)
1205 av_realloc(track, sizeof(MatroskaVideoTrack));
1207 case MATROSKA_TRACK_TYPE_AUDIO:
1208 track = (MatroskaTrack *)
1209 av_realloc(track, sizeof(MatroskaAudioTrack));
1210 ((MatroskaAudioTrack *)track)->channels = 1;
1211 ((MatroskaAudioTrack *)track)->samplerate = 8000;
1213 case MATROSKA_TRACK_TYPE_SUBTITLE:
1214 track = (MatroskaTrack *)
1215 av_realloc(track, sizeof(MatroskaSubtitleTrack));
1217 case MATROSKA_TRACK_TYPE_COMPLEX:
1218 case MATROSKA_TRACK_TYPE_LOGO:
1219 case MATROSKA_TRACK_TYPE_CONTROL:
1221 av_log(matroska->ctx, AV_LOG_INFO,
1222 "Unknown or unsupported track type 0x%x\n",
1227 matroska->tracks[matroska->num_tracks - 1] = track;
1231 /* tracktype specific stuff for video */
1232 case MATROSKA_ID_TRACKVIDEO: {
1233 MatroskaVideoTrack *videotrack;
1234 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1235 av_log(matroska->ctx, AV_LOG_INFO,
1236 "video data in non-video track - ignoring\n");
1237 res = AVERROR_INVALIDDATA;
1239 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1241 videotrack = (MatroskaVideoTrack *)track;
1244 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1247 } else if (matroska->level_up > 0) {
1248 matroska->level_up--;
1253 /* fixme, this should be one-up, but I get it here */
1254 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1256 if ((res = ebml_read_uint (matroska, &id,
1259 track->default_duration = num;
1263 /* video framerate */
1264 case MATROSKA_ID_VIDEOFRAMERATE: {
1266 if ((res = ebml_read_float(matroska, &id,
1269 track->default_duration = 1000000000 * (1. / num);
1273 /* width of the size to display the video at */
1274 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1276 if ((res = ebml_read_uint(matroska, &id,
1279 videotrack->display_width = num;
1283 /* height of the size to display the video at */
1284 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1286 if ((res = ebml_read_uint(matroska, &id,
1289 videotrack->display_height = num;
1293 /* width of the video in the file */
1294 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1296 if ((res = ebml_read_uint(matroska, &id,
1299 videotrack->pixel_width = num;
1303 /* height of the video in the file */
1304 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1306 if ((res = ebml_read_uint(matroska, &id,
1309 videotrack->pixel_height = num;
1313 /* whether the video is interlaced */
1314 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1316 if ((res = ebml_read_uint(matroska, &id,
1321 MATROSKA_VIDEOTRACK_INTERLACED;
1324 ~MATROSKA_VIDEOTRACK_INTERLACED;
1328 /* stereo mode (whether the video has two streams,
1329 * where one is for the left eye and the other for
1330 * the right eye, which creates a 3D-like
1332 case MATROSKA_ID_VIDEOSTEREOMODE: {
1334 if ((res = ebml_read_uint(matroska, &id,
1337 if (num != MATROSKA_EYE_MODE_MONO &&
1338 num != MATROSKA_EYE_MODE_LEFT &&
1339 num != MATROSKA_EYE_MODE_RIGHT &&
1340 num != MATROSKA_EYE_MODE_BOTH) {
1341 av_log(matroska->ctx, AV_LOG_INFO,
1342 "Ignoring unknown eye mode 0x%x\n",
1346 videotrack->eye_mode = num;
1350 /* aspect ratio behaviour */
1351 case MATROSKA_ID_VIDEOASPECTRATIO: {
1353 if ((res = ebml_read_uint(matroska, &id,
1356 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1357 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1358 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1359 av_log(matroska->ctx, AV_LOG_INFO,
1360 "Ignoring unknown aspect ratio 0x%x\n",
1364 videotrack->ar_mode = num;
1368 /* colourspace (only matters for raw video)
1370 case MATROSKA_ID_VIDEOCOLOURSPACE: {
1372 if ((res = ebml_read_uint(matroska, &id,
1375 videotrack->fourcc = num;
1380 av_log(matroska->ctx, AV_LOG_INFO,
1381 "Unknown video track header entry "
1382 "0x%x - ignoring\n", id);
1386 res = ebml_read_skip(matroska);
1390 if (matroska->level_up) {
1391 matroska->level_up--;
1398 /* tracktype specific stuff for audio */
1399 case MATROSKA_ID_TRACKAUDIO: {
1400 MatroskaAudioTrack *audiotrack;
1401 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1402 av_log(matroska->ctx, AV_LOG_INFO,
1403 "audio data in non-audio track - ignoring\n");
1404 res = AVERROR_INVALIDDATA;
1406 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1408 audiotrack = (MatroskaAudioTrack *)track;
1411 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1414 } else if (matroska->level_up > 0) {
1415 matroska->level_up--;
1421 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1423 if ((res = ebml_read_float(matroska, &id,
1426 audiotrack->samplerate = num;
1431 case MATROSKA_ID_AUDIOBITDEPTH: {
1433 if ((res = ebml_read_uint(matroska, &id,
1436 audiotrack->bitdepth = num;
1441 case MATROSKA_ID_AUDIOCHANNELS: {
1443 if ((res = ebml_read_uint(matroska, &id,
1446 audiotrack->channels = num;
1451 av_log(matroska->ctx, AV_LOG_INFO,
1452 "Unknown audio track header entry "
1453 "0x%x - ignoring\n", id);
1457 res = ebml_read_skip(matroska);
1461 if (matroska->level_up) {
1462 matroska->level_up--;
1469 /* codec identifier */
1470 case MATROSKA_ID_CODECID: {
1472 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1474 track->codec_id = text;
1478 /* codec private data */
1479 case MATROSKA_ID_CODECPRIVATE: {
1482 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1484 track->codec_priv = data;
1485 track->codec_priv_size = size;
1489 /* name of the codec */
1490 case MATROSKA_ID_CODECNAME: {
1492 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1494 track->codec_name = text;
1498 /* name of this track */
1499 case MATROSKA_ID_TRACKNAME: {
1501 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1507 /* language (matters for audio/subtitles, mostly) */
1508 case MATROSKA_ID_TRACKLANGUAGE: {
1510 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1512 track->language = text;
1516 /* whether this is actually used */
1517 case MATROSKA_ID_TRACKFLAGENABLED: {
1519 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1522 track->flags |= MATROSKA_TRACK_ENABLED;
1524 track->flags &= ~MATROSKA_TRACK_ENABLED;
1528 /* whether it's the default for this track type */
1529 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1531 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1534 track->flags |= MATROSKA_TRACK_DEFAULT;
1536 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1540 /* lacing (like MPEG, where blocks don't end/start on frame
1542 case MATROSKA_ID_TRACKFLAGLACING: {
1544 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1547 track->flags |= MATROSKA_TRACK_LACING;
1549 track->flags &= ~MATROSKA_TRACK_LACING;
1553 /* default length (in time) of one data block in this track */
1554 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1556 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1558 track->default_duration = num;
1563 av_log(matroska->ctx, AV_LOG_INFO,
1564 "Unknown track header entry 0x%x - ignoring\n", id);
1568 /* we ignore these because they're nothing useful. */
1569 case MATROSKA_ID_CODECINFOURL:
1570 case MATROSKA_ID_CODECDOWNLOADURL:
1571 case MATROSKA_ID_TRACKMINCACHE:
1572 case MATROSKA_ID_TRACKMAXCACHE:
1573 res = ebml_read_skip(matroska);
1577 if (matroska->level_up) {
1578 matroska->level_up--;
1587 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1592 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1595 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1598 } else if (matroska->level_up) {
1599 matroska->level_up--;
1604 /* one track within the "all-tracks" header */
1605 case MATROSKA_ID_TRACKENTRY:
1606 res = matroska_add_stream(matroska);
1610 av_log(matroska->ctx, AV_LOG_INFO,
1611 "Unknown entry 0x%x in track header\n", id);
1615 res = ebml_read_skip(matroska);
1619 if (matroska->level_up) {
1620 matroska->level_up--;
1629 matroska_parse_index (MatroskaDemuxContext *matroska)
1633 MatroskaDemuxIndex idx;
1635 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1638 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1641 } else if (matroska->level_up) {
1642 matroska->level_up--;
1647 /* one single index entry ('point') */
1648 case MATROSKA_ID_POINTENTRY:
1649 if ((res = ebml_read_master(matroska, &id)) < 0)
1652 /* in the end, we hope to fill one entry with a
1653 * timestamp, a file position and a tracknum */
1654 idx.pos = (uint64_t) -1;
1655 idx.time = (uint64_t) -1;
1656 idx.track = (uint16_t) -1;
1659 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1662 } else if (matroska->level_up) {
1663 matroska->level_up--;
1668 /* one single index entry ('point') */
1669 case MATROSKA_ID_CUETIME: {
1671 if ((res = ebml_read_uint(matroska, &id,
1674 idx.time = time * matroska->time_scale;
1678 /* position in the file + track to which it
1680 case MATROSKA_ID_CUETRACKPOSITION:
1681 if ((res = ebml_read_master(matroska, &id)) < 0)
1685 if (!(id = ebml_peek_id (matroska,
1686 &matroska->level_up))) {
1689 } else if (matroska->level_up) {
1690 matroska->level_up--;
1696 case MATROSKA_ID_CUETRACK: {
1698 if ((res = ebml_read_uint(matroska,
1705 /* position in file */
1706 case MATROSKA_ID_CUECLUSTERPOSITION: {
1708 if ((res = ebml_read_uint(matroska,
1716 av_log(matroska->ctx, AV_LOG_INFO,
1717 "Unknown entry 0x%x in "
1718 "CuesTrackPositions\n", id);
1722 res = ebml_read_skip(matroska);
1726 if (matroska->level_up) {
1727 matroska->level_up--;
1735 av_log(matroska->ctx, AV_LOG_INFO,
1736 "Unknown entry 0x%x in cuespoint "
1741 res = ebml_read_skip(matroska);
1745 if (matroska->level_up) {
1746 matroska->level_up--;
1751 /* so let's see if we got what we wanted */
1752 if (idx.pos != (uint64_t) -1 &&
1753 idx.time != (uint64_t) -1 &&
1754 idx.track != (uint16_t) -1) {
1755 if (matroska->num_indexes % 32 == 0) {
1756 /* re-allocate bigger index */
1758 av_realloc(matroska->index,
1759 (matroska->num_indexes + 32) *
1760 sizeof(MatroskaDemuxIndex));
1762 matroska->index[matroska->num_indexes] = idx;
1763 matroska->num_indexes++;
1768 av_log(matroska->ctx, AV_LOG_INFO,
1769 "Unknown entry 0x%x in cues header\n", id);
1773 res = ebml_read_skip(matroska);
1777 if (matroska->level_up) {
1778 matroska->level_up--;
1787 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1793 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1796 } else if (matroska->level_up) {
1797 matroska->level_up--;
1802 /* Hm, this is unsupported... */
1804 av_log(matroska->ctx, AV_LOG_INFO,
1805 "Unknown entry 0x%x in metadata header\n", id);
1809 res = ebml_read_skip(matroska);
1813 if (matroska->level_up) {
1814 matroska->level_up--;
1823 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1828 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1831 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1834 } else if (matroska->level_up) {
1835 matroska->level_up--;
1840 case MATROSKA_ID_SEEKENTRY: {
1841 uint32_t seek_id = 0, peek_id_cache = 0;
1842 uint64_t seek_pos = (uint64_t) -1, t;
1844 if ((res = ebml_read_master(matroska, &id)) < 0)
1848 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1851 } else if (matroska->level_up) {
1852 matroska->level_up--;
1857 case MATROSKA_ID_SEEKID:
1858 res = ebml_read_uint(matroska, &id, &t);
1862 case MATROSKA_ID_SEEKPOSITION:
1863 res = ebml_read_uint(matroska, &id, &seek_pos);
1867 av_log(matroska->ctx, AV_LOG_INFO,
1868 "Unknown seekhead ID 0x%x\n", id);
1872 res = ebml_read_skip(matroska);
1876 if (matroska->level_up) {
1877 matroska->level_up--;
1882 if (!seek_id || seek_pos == (uint64_t) -1) {
1883 av_log(matroska->ctx, AV_LOG_INFO,
1884 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1890 case MATROSKA_ID_CUES:
1891 case MATROSKA_ID_TAGS: {
1892 uint32_t level_up = matroska->level_up;
1893 offset_t before_pos;
1895 MatroskaLevel level;
1897 /* remember the peeked ID and the current position */
1898 peek_id_cache = matroska->peek_id;
1899 before_pos = url_ftell(&matroska->ctx->pb);
1902 if ((res = ebml_read_seek(matroska, seek_pos +
1903 matroska->segment_start)) < 0)
1906 /* we don't want to lose our seekhead level, so we add
1907 * a dummy. This is a crude hack. */
1908 if (matroska->num_levels == EBML_MAX_DEPTH) {
1909 av_log(matroska->ctx, AV_LOG_INFO,
1910 "Max EBML element depth (%d) reached, "
1911 "cannot parse further.\n", EBML_MAX_DEPTH);
1912 return AVERROR_UNKNOWN;
1916 level.length = (uint64_t)-1;
1917 matroska->levels[matroska->num_levels] = level;
1918 matroska->num_levels++;
1921 if (!(id = ebml_peek_id (matroska,
1922 &matroska->level_up)))
1924 if (id != seek_id) {
1925 av_log(matroska->ctx, AV_LOG_INFO,
1926 "We looked for ID=0x%x but got "
1927 "ID=0x%x (pos=%"PRIu64")",
1928 seek_id, id, seek_pos +
1929 matroska->segment_start);
1933 /* read master + parse */
1934 if ((res = ebml_read_master(matroska, &id)) < 0)
1937 case MATROSKA_ID_CUES:
1938 if (!(res = matroska_parse_index(matroska)) ||
1939 url_feof(&matroska->ctx->pb)) {
1940 matroska->index_parsed = 1;
1944 case MATROSKA_ID_TAGS:
1945 if (!(res = matroska_parse_metadata(matroska)) ||
1946 url_feof(&matroska->ctx->pb)) {
1947 matroska->metadata_parsed = 1;
1956 /* remove dummy level */
1957 while (matroska->num_levels) {
1958 matroska->num_levels--;
1960 matroska->levels[matroska->num_levels].length;
1961 if (length == (uint64_t)-1)
1966 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1968 matroska->peek_id = peek_id_cache;
1969 matroska->level_up = level_up;
1974 av_log(matroska->ctx, AV_LOG_INFO,
1975 "Ignoring seekhead entry for ID=0x%x\n",
1984 av_log(matroska->ctx, AV_LOG_INFO,
1985 "Unknown seekhead ID 0x%x\n", id);
1989 res = ebml_read_skip(matroska);
1993 if (matroska->level_up) {
1994 matroska->level_up--;
2003 matroska_read_header (AVFormatContext *s,
2004 AVFormatParameters *ap)
2006 MatroskaDemuxContext *matroska = s->priv_data;
2008 int version, last_level, res = 0;
2013 /* First read the EBML header. */
2015 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2017 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2018 av_log(matroska->ctx, AV_LOG_ERROR,
2019 "Wrong EBML doctype ('%s' != 'matroska').\n",
2020 doctype ? doctype : "(none)");
2023 return AVERROR_NOFMT;
2027 av_log(matroska->ctx, AV_LOG_ERROR,
2028 "Matroska demuxer version 1 too old for file version %d\n",
2030 return AVERROR_NOFMT;
2033 /* The next thing is a segment. */
2035 if (!(id = ebml_peek_id(matroska, &last_level)))
2037 if (id == MATROSKA_ID_SEGMENT)
2041 av_log(matroska->ctx, AV_LOG_INFO,
2042 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2043 MATROSKA_ID_SEGMENT, id);
2044 if ((res = ebml_read_skip(matroska)) < 0)
2048 /* We now have a Matroska segment.
2049 * Seeks are from the beginning of the segment,
2050 * after the segment ID/length. */
2051 if ((res = ebml_read_master(matroska, &id)) < 0)
2053 matroska->segment_start = url_ftell(&s->pb);
2055 matroska->time_scale = 1000000;
2056 /* we've found our segment, start reading the different contents in here */
2058 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2061 } else if (matroska->level_up) {
2062 matroska->level_up--;
2068 case MATROSKA_ID_INFO: {
2069 if ((res = ebml_read_master(matroska, &id)) < 0)
2071 res = matroska_parse_info(matroska);
2075 /* track info headers */
2076 case MATROSKA_ID_TRACKS: {
2077 if ((res = ebml_read_master(matroska, &id)) < 0)
2079 res = matroska_parse_tracks(matroska);
2084 case MATROSKA_ID_CUES: {
2085 if (!matroska->index_parsed) {
2086 if ((res = ebml_read_master(matroska, &id)) < 0)
2088 res = matroska_parse_index(matroska);
2090 res = ebml_read_skip(matroska);
2095 case MATROSKA_ID_TAGS: {
2096 if (!matroska->metadata_parsed) {
2097 if ((res = ebml_read_master(matroska, &id)) < 0)
2099 res = matroska_parse_metadata(matroska);
2101 res = ebml_read_skip(matroska);
2105 /* file index (if seekable, seek to Cues/Tags to parse it) */
2106 case MATROSKA_ID_SEEKHEAD: {
2107 if ((res = ebml_read_master(matroska, &id)) < 0)
2109 res = matroska_parse_seekhead(matroska);
2113 case MATROSKA_ID_CLUSTER: {
2114 /* Do not read the master - this will be done in the next
2115 * call to matroska_read_packet. */
2121 av_log(matroska->ctx, AV_LOG_INFO,
2122 "Unknown matroska file header ID 0x%x\n", id);
2126 res = ebml_read_skip(matroska);
2130 if (matroska->level_up) {
2131 matroska->level_up--;
2139 /* Have we found a cluster? */
2142 enum CodecID codec_id= CODEC_ID_NONE;
2143 MatroskaTrack *track;
2146 for (i = 0; i < matroska->num_tracks; i++) {
2147 void *extradata = NULL;
2148 int extradata_size = 0;
2149 track = matroska->tracks[i];
2151 /* libavformat does not really support subtitles.
2152 * Also apply some sanity checks. */
2153 if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
2154 (track->codec_id == NULL))
2157 for(j=0; codec_tags[j].str; j++){
2158 if(!strcmp(codec_tags[j].str, track->codec_id)){
2159 codec_id= codec_tags[j].id;
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);
2181 /* This is the MS compatibility mode which stores a
2182 * WAVEFORMATEX in the CodecPrivate. */
2183 else if (!strcmp(track->codec_id,
2184 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2185 (track->codec_priv_size >= 18) &&
2186 (track->codec_priv != NULL)) {
2190 /* Offset of wFormatTag. Stored in LE. */
2191 p = (unsigned char *)track->codec_priv;
2192 tag = (p[1] << 8) | p[0];
2193 codec_id = codec_get_wav_id(tag);
2197 if (codec_id == CODEC_ID_NONE) {
2198 av_log(matroska->ctx, AV_LOG_INFO,
2199 "Unknown/unsupported CodecID %s.\n",
2203 track->stream_index = matroska->num_streams;
2205 matroska->num_streams++;
2206 st = av_new_stream(s, track->stream_index);
2208 return AVERROR_NOMEM;
2209 av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2211 st->codec->codec_id = codec_id;
2214 st->codec->extradata = extradata;
2215 st->codec->extradata_size = extradata_size;
2216 } else if(track->codec_priv && track->codec_priv_size > 0){
2217 st->codec->extradata = av_malloc(track->codec_priv_size);
2218 if(st->codec->extradata == NULL)
2219 return AVERROR_NOMEM;
2220 st->codec->extradata_size = track->codec_priv_size;
2221 memcpy(st->codec->extradata, track->codec_priv,
2222 track->codec_priv_size);
2225 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2226 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2228 st->codec->codec_type = CODEC_TYPE_VIDEO;
2229 st->codec->codec_tag = videotrack->fourcc;
2230 st->codec->width = videotrack->pixel_width;
2231 st->codec->height = videotrack->pixel_height;
2232 if (videotrack->display_width == 0)
2233 videotrack->display_width= videotrack->pixel_width;
2234 if (videotrack->display_height == 0)
2235 videotrack->display_height= videotrack->pixel_height;
2236 av_reduce(&st->codec->sample_aspect_ratio.num,
2237 &st->codec->sample_aspect_ratio.den,
2238 st->codec->height * videotrack->display_width,
2239 st->codec-> width * videotrack->display_height,
2241 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2242 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2244 st->codec->codec_type = CODEC_TYPE_AUDIO;
2245 st->codec->sample_rate = audiotrack->samplerate;
2246 st->codec->channels = audiotrack->channels;
2247 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2248 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2251 /* What do we do with private data? E.g. for Vorbis. */
2259 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
2264 for (i = 0; i < matroska->num_tracks; i++)
2265 if (matroska->tracks[i]->num == num)
2272 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2273 uint64_t cluster_time)
2278 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2280 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2283 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2286 } else if (matroska->level_up) {
2287 matroska->level_up--;
2292 /* one block inside the group. Note, block parsing is one
2293 * of the harder things, so this code is a bit complicated.
2294 * See http://www.matroska.org/ for documentation. */
2295 case MATROSKA_ID_BLOCK: {
2296 uint8_t *data, *origdata;
2299 uint32_t *lace_size = NULL;
2300 int n, track, flags, laces = 0;
2302 int64_t pos= url_ftell(&matroska->ctx->pb);
2304 if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
2308 /* first byte(s): blocknum */
2309 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2310 av_log(matroska->ctx, AV_LOG_ERROR,
2311 "EBML block data error\n");
2318 /* fetch track from num */
2319 track = matroska_find_track_by_num(matroska, num);
2320 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2321 av_log(matroska->ctx, AV_LOG_INFO,
2322 "Invalid stream %d or size %u\n", track, size);
2326 if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
2331 /* block_time (relative to cluster time) */
2332 block_time = (data[0] << 8) | data[1];
2338 switch ((flags & 0x06) >> 1) {
2339 case 0x0: /* no lacing */
2341 lace_size = av_mallocz(sizeof(int));
2342 lace_size[0] = size;
2345 case 0x1: /* xiph lacing */
2346 case 0x2: /* fixed-size lacing */
2347 case 0x3: /* EBML lacing */
2352 laces = (*data) + 1;
2355 lace_size = av_mallocz(laces * sizeof(int));
2357 switch ((flags & 0x06) >> 1) {
2358 case 0x1: /* xiph lacing */ {
2361 for (n = 0; res == 0 && n < laces - 1; n++) {
2368 lace_size[n] += temp;
2374 total += lace_size[n];
2376 lace_size[n] = size - total;
2380 case 0x2: /* fixed-size lacing */
2381 for (n = 0; n < laces; n++)
2382 lace_size[n] = size / laces;
2385 case 0x3: /* EBML lacing */ {
2387 n = matroska_ebmlnum_uint(data, size, &num);
2389 av_log(matroska->ctx, AV_LOG_INFO,
2390 "EBML block data error\n");
2395 total = lace_size[0] = num;
2396 for (n = 1; res == 0 && n < laces - 1; n++) {
2399 r = matroska_ebmlnum_sint (data, size,
2402 av_log(matroska->ctx, AV_LOG_INFO,
2403 "EBML block data error\n");
2408 lace_size[n] = lace_size[n - 1] + snum;
2409 total += lace_size[n];
2411 lace_size[n] = size - total;
2419 for (n = 0; n < laces; n++) {
2420 uint64_t timecode = AV_NOPTS_VALUE;
2422 pkt = av_mallocz(sizeof(AVPacket));
2423 /* XXX: prevent data copy... */
2424 if (av_new_packet(pkt,lace_size[n]) < 0) {
2425 res = AVERROR_NOMEM;
2428 if (cluster_time != (uint64_t)-1 && n == 0) {
2429 if (cluster_time + block_time >= 0)
2430 timecode = cluster_time + block_time;
2432 /* FIXME: duration */
2434 memcpy(pkt->data, data, lace_size[n]);
2435 data += lace_size[n];
2437 pkt->flags = is_keyframe;
2439 matroska->tracks[track]->stream_index;
2441 pkt->pts = timecode;
2444 matroska_queue_packet(matroska, pkt);
2453 case MATROSKA_ID_BLOCKDURATION: {
2455 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2457 av_log(matroska->ctx, AV_LOG_INFO,
2458 "FIXME: implement support for BlockDuration\n");
2462 case MATROSKA_ID_BLOCKREFERENCE:
2463 /* We've found a reference, so not even the first frame in
2464 * the lace is a key frame. */
2466 if (last_num_packets != matroska->num_packets)
2467 matroska->packets[last_num_packets]->flags = 0;
2468 res = ebml_read_skip(matroska);
2472 av_log(matroska->ctx, AV_LOG_INFO,
2473 "Unknown entry 0x%x in blockgroup data\n", id);
2477 res = ebml_read_skip(matroska);
2481 if (matroska->level_up) {
2482 matroska->level_up--;
2491 matroska_parse_cluster (MatroskaDemuxContext *matroska)
2495 uint64_t cluster_time = 0;
2497 av_log(matroska->ctx, AV_LOG_DEBUG,
2498 "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
2501 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2504 } else if (matroska->level_up) {
2505 matroska->level_up--;
2510 /* cluster timecode */
2511 case MATROSKA_ID_CLUSTERTIMECODE: {
2513 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2519 /* a group of blocks inside a cluster */
2520 case MATROSKA_ID_BLOCKGROUP:
2521 if ((res = ebml_read_master(matroska, &id)) < 0)
2523 res = matroska_parse_blockgroup(matroska, cluster_time);
2527 av_log(matroska->ctx, AV_LOG_INFO,
2528 "Unknown entry 0x%x in cluster data\n", id);
2532 res = ebml_read_skip(matroska);
2536 if (matroska->level_up) {
2537 matroska->level_up--;
2546 matroska_read_packet (AVFormatContext *s,
2549 MatroskaDemuxContext *matroska = s->priv_data;
2553 /* Do we still have a packet queued? */
2554 if (matroska_deliver_packet(matroska, pkt) == 0)
2557 /* Have we already reached the end? */
2562 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2565 } else if (matroska->level_up) {
2566 matroska->level_up--;
2571 case MATROSKA_ID_CLUSTER:
2572 if ((res = ebml_read_master(matroska, &id)) < 0)
2574 if ((res = matroska_parse_cluster(matroska)) == 0)
2575 res = 1; /* Parsed one cluster, let's get out. */
2580 res = ebml_read_skip(matroska);
2584 if (matroska->level_up) {
2585 matroska->level_up--;
2593 return matroska_deliver_packet(matroska, pkt);
2597 matroska_read_close (AVFormatContext *s)
2599 MatroskaDemuxContext *matroska = s->priv_data;
2602 if (matroska->writing_app)
2603 av_free(matroska->writing_app);
2604 if (matroska->muxing_app)
2605 av_free(matroska->muxing_app);
2606 if (matroska->index)
2607 av_free(matroska->index);
2609 if (matroska->packets != NULL) {
2610 for (n = 0; n < matroska->num_packets; n++) {
2611 av_free_packet(matroska->packets[n]);
2612 av_free(matroska->packets[n]);
2614 av_free(matroska->packets);
2617 for (n = 0; n < matroska->num_tracks; n++) {
2618 MatroskaTrack *track = matroska->tracks[n];
2619 if (track->codec_id)
2620 av_free(track->codec_id);
2621 if (track->codec_name)
2622 av_free(track->codec_name);
2623 if (track->codec_priv)
2624 av_free(track->codec_priv);
2626 av_free(track->name);
2627 if (track->language)
2628 av_free(track->language);
2633 memset(matroska, 0, sizeof(MatroskaDemuxContext));
2638 AVInputFormat matroska_demuxer = {
2640 "Matroska file format",
2641 sizeof(MatroskaDemuxContext),
2643 matroska_read_header,
2644 matroska_read_packet,
2645 matroska_read_close,