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. */
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_MPEG4_AVC "V_MPEG4/ISO/AVC"
190
#define MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3 "V_MPEG4/MS/V3"
191
#define MATROSKA_CODEC_ID_VIDEO_MPEG1 "V_MPEG1"
192
#define MATROSKA_CODEC_ID_VIDEO_MPEG2 "V_MPEG2"
193
#define MATROSKA_CODEC_ID_VIDEO_MJPEG "V_MJPEG"
194
/* TODO: Real/Quicktime */
196
#define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
197
#define MATROSKA_CODEC_ID_AUDIO_MPEG1_L1 "A_MPEG/L1"
198
#define MATROSKA_CODEC_ID_AUDIO_MPEG1_L2 "A_MPEG/L2"
199
#define MATROSKA_CODEC_ID_AUDIO_MPEG1_L3 "A_MPEG/L3"
200
#define MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE "A_PCM/INT/BIG"
201
#define MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE "A_PCM/INT/LIT"
202
#define MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT "A_PCM/FLOAT/IEEE"
203
#define MATROSKA_CODEC_ID_AUDIO_AC3 "A_AC3"
204
#define MATROSKA_CODEC_ID_AUDIO_DTS "A_DTS"
205
#define MATROSKA_CODEC_ID_AUDIO_VORBIS "A_VORBIS"
206
#define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
207
#define MATROSKA_CODEC_ID_AUDIO_MPEG2 "A_AAC/MPEG2/"
208
#define MATROSKA_CODEC_ID_AUDIO_MPEG4 "A_AAC/MPEG4/"
209
/* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
211
/* max. depth in the EBML tree structure */
212
#define EBML_MAX_DEPTH 16
214
typedef struct Track {
215
MatroskaTrackType type;
217
/* Unique track number and track ID. stream_index is the index that
218
* the calling app uses for this track. */
229
unsigned char *codec_priv;
232
int64_t default_duration;
233
MatroskaTrackFlags flags;
236
typedef struct MatroskaVideoTrack {
246
MatroskaAspectRatioMode ar_mode;
247
MatroskaEyeMode eye_mode;
250
} MatroskaVideoTrack;
252
typedef struct MatroskaAudioTrack {
259
} MatroskaAudioTrack;
261
typedef struct MatroskaSubtitleTrack {
265
} MatroskaSubtitleTrack;
267
typedef struct MatroskaLevel {
268
uint64_t start, length;
271
typedef struct MatroskaDemuxIndex {
272
uint64_t pos; /* of the corresponding *cluster*! */
273
uint16_t track; /* reference to 'num' */
274
uint64_t time; /* in nanoseconds */
275
} MatroskaDemuxIndex;
277
typedef struct MatroskaDemuxContext {
278
AVFormatContext *ctx;
282
MatroskaLevel levels[EBML_MAX_DEPTH];
290
/* timescale in the file */
293
/* length, position (time, ns) */
297
/* num_streams is the number of streams that av_new_stream() was called
298
* for ( = that are available to the calling program). */
299
int num_tracks, num_streams;
300
MatroskaTrack *tracks[MAX_STREAMS];
302
/* cache for ID peeking */
305
/* byte position of the segment inside the stream */
306
offset_t segment_start;
308
/* The packet queue. */
312
/* have we already parse metadata/cues/clusters? */
317
/* The index for seeking. */
319
MatroskaDemuxIndex *index;
320
} MatroskaDemuxContext;
323
* The first few functions handle EBML file parsing. The rest
324
* is the document interpretation. Matroska really just is a
329
* Return: the amount of levels in the hierarchy that the
330
* current element lies higher than the previous one.
331
* The opposite isn't done - that's auto-done using master
336
ebml_read_element_level_up (MatroskaDemuxContext *matroska)
338
ByteIOContext *pb = &matroska->ctx->pb;
339
offset_t pos = url_ftell(pb);
342
while (matroska->num_levels > 0) {
343
MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
345
if (pos >= level->start + level->length) {
346
matroska->num_levels--;
357
* Read: an "EBML number", which is defined as a variable-length
358
* array of bytes. The first byte indicates the length by giving a
359
* number of 0-bits followed by a one. The position of the first
360
* "one" bit inside the first byte indicates the length of this
362
* Returns: num. of bytes read. < 0 on error.
366
ebml_read_num (MatroskaDemuxContext *matroska,
370
ByteIOContext *pb = &matroska->ctx->pb;
371
int len_mask = 0x80, read = 1, n = 1;
374
/* the first byte tells us the length in bytes - get_byte() can normally
375
* return 0, but since that's not a valid first ebmlID byte, we can
376
* use it safely here to catch EOS. */
377
if (!(total = get_byte(pb))) {
378
/* we might encounter EOS here */
380
offset_t pos = url_ftell(pb);
381
av_log(matroska->ctx, AV_LOG_ERROR,
382
"Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
385
return AVERROR_IO; /* EOS or actual I/O error */
388
/* get the length of the EBML number */
389
while (read <= max_size && !(total & len_mask)) {
393
if (read > max_size) {
394
offset_t pos = url_ftell(pb) - 1;
395
av_log(matroska->ctx, AV_LOG_ERROR,
396
"Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
397
(uint8_t) total, pos, pos);
398
return AVERROR_INVALIDDATA;
401
/* read out length */
404
total = (total << 8) | get_byte(pb);
412
* Read: the element content data ID.
413
* Return: the number of bytes read or < 0 on error.
417
ebml_read_element_id (MatroskaDemuxContext *matroska,
424
/* if we re-call this, use our cached ID */
425
if (matroska->peek_id != 0) {
428
*id = matroska->peek_id;
432
/* read out the "EBML number", include tag in ID */
433
if ((read = ebml_read_num(matroska, 4, &total)) < 0)
435
*id = matroska->peek_id = total | (1 << (read * 7));
439
*level_up = ebml_read_element_level_up(matroska);
445
* Read: element content length.
446
* Return: the number of bytes read or < 0 on error.
450
ebml_read_element_length (MatroskaDemuxContext *matroska,
453
/* clear cache since we're now beyond that data point */
454
matroska->peek_id = 0;
456
/* read out the "EBML number", include tag in ID */
457
return ebml_read_num(matroska, 8, length);
461
* Return: the ID of the next element, or 0 on error.
462
* Level_up contains the amount of levels that this
463
* next element lies higher than the previous one.
467
ebml_peek_id (MatroskaDemuxContext *matroska,
472
assert(level_up != NULL);
474
if (ebml_read_element_id(matroska, &id, level_up) < 0)
481
* Seek to a given offset.
482
* 0 is success, -1 is failure.
486
ebml_read_seek (MatroskaDemuxContext *matroska,
489
ByteIOContext *pb = &matroska->ctx->pb;
491
/* clear ID cache, if any */
492
matroska->peek_id = 0;
494
return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
498
* Skip the next element.
499
* 0 is success, -1 is failure.
503
ebml_read_skip (MatroskaDemuxContext *matroska)
505
ByteIOContext *pb = &matroska->ctx->pb;
510
if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
511
(res = ebml_read_element_length(matroska, &length)) < 0)
514
url_fskip(pb, length);
520
* Read the next element as an unsigned int.
521
* 0 is success, < 0 is failure.
525
ebml_read_uint (MatroskaDemuxContext *matroska,
529
ByteIOContext *pb = &matroska->ctx->pb;
530
int n = 0, size, res;
533
if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
534
(res = ebml_read_element_length(matroska, &rlength)) < 0)
537
if (size < 1 || size > 8) {
538
offset_t pos = url_ftell(pb);
539
av_log(matroska->ctx, AV_LOG_ERROR,
540
"Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
542
return AVERROR_INVALIDDATA;
545
/* big-endian ordening; build up number */
548
*num = (*num << 8) | get_byte(pb);
554
* Read the next element as a signed int.
555
* 0 is success, < 0 is failure.
559
ebml_read_sint (MatroskaDemuxContext *matroska,
563
ByteIOContext *pb = &matroska->ctx->pb;
564
int size, n = 1, negative = 0, res;
567
if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
568
(res = ebml_read_element_length(matroska, &rlength)) < 0)
571
if (size < 1 || size > 8) {
572
offset_t pos = url_ftell(pb);
573
av_log(matroska->ctx, AV_LOG_ERROR,
574
"Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
576
return AVERROR_INVALIDDATA;
578
if ((*num = get_byte(pb)) & 0x80) {
584
*num = (*num << 8) | get_byte(pb);
588
*num = *num - (1LL << ((8 * size) - 1));
594
* Read the next element as a float.
595
* 0 is success, < 0 is failure.
599
ebml_read_float (MatroskaDemuxContext *matroska,
603
ByteIOContext *pb = &matroska->ctx->pb;
607
if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
608
(res = ebml_read_element_length(matroska, &rlength)) < 0)
613
*num= av_int2flt(get_be32(pb));
615
*num= av_int2dbl(get_be64(pb));
617
av_log(matroska->ctx, AV_LOG_ERROR,
618
"FIXME! 10-byte floats unimplemented\n");
619
return AVERROR_UNKNOWN;
621
offset_t pos = url_ftell(pb);
622
av_log(matroska->ctx, AV_LOG_ERROR,
623
"Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
625
return AVERROR_INVALIDDATA;
632
* Read the next element as an ASCII string.
633
* 0 is success, < 0 is failure.
637
ebml_read_ascii (MatroskaDemuxContext *matroska,
641
ByteIOContext *pb = &matroska->ctx->pb;
645
if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
646
(res = ebml_read_element_length(matroska, &rlength)) < 0)
650
/* ebml strings are usually not 0-terminated, so we allocate one
651
* byte more, read the string and NULL-terminate it ourselves. */
652
if (size < 0 || !(*str = av_malloc(size + 1))) {
653
av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
654
return AVERROR_NOMEM;
656
if (get_buffer(pb, (uint8_t *) *str, size) != size) {
657
offset_t pos = url_ftell(pb);
658
av_log(matroska->ctx, AV_LOG_ERROR,
659
"Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
668
* Read the next element as a UTF-8 string.
669
* 0 is success, < 0 is failure.
673
ebml_read_utf8 (MatroskaDemuxContext *matroska,
677
return ebml_read_ascii(matroska, id, str);
681
* Read the next element as a date (nanoseconds since 1/1/2000).
682
* 0 is success, < 0 is failure.
686
ebml_read_date (MatroskaDemuxContext *matroska,
690
return ebml_read_sint(matroska, id, date);
694
* Read the next element, but only the header. The contents
695
* are supposed to be sub-elements which can be read separately.
696
* 0 is success, < 0 is failure.
700
ebml_read_master (MatroskaDemuxContext *matroska,
703
ByteIOContext *pb = &matroska->ctx->pb;
705
MatroskaLevel *level;
708
if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
709
(res = ebml_read_element_length(matroska, &length)) < 0)
712
/* protect... (Heaven forbids that the '>' is true) */
713
if (matroska->num_levels >= EBML_MAX_DEPTH) {
714
av_log(matroska->ctx, AV_LOG_ERROR,
715
"File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
716
return AVERROR_NOTSUPP;
720
level = &matroska->levels[matroska->num_levels++];
721
level->start = url_ftell(pb);
722
level->length = length;
728
* Read the next element as binary data.
729
* 0 is success, < 0 is failure.
733
ebml_read_binary (MatroskaDemuxContext *matroska,
738
ByteIOContext *pb = &matroska->ctx->pb;
742
if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
743
(res = ebml_read_element_length(matroska, &rlength)) < 0)
747
if (!(*binary = av_malloc(*size))) {
748
av_log(matroska->ctx, AV_LOG_ERROR,
749
"Memory allocation error\n");
750
return AVERROR_NOMEM;
753
if (get_buffer(pb, *binary, *size) != *size) {
754
offset_t pos = url_ftell(pb);
755
av_log(matroska->ctx, AV_LOG_ERROR,
756
"Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
764
* Read signed/unsigned "EBML" numbers.
765
* Return: number of bytes processed, < 0 on error.
766
* XXX: use ebml_read_num().
770
matroska_ebmlnum_uint (uint8_t *data,
774
int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
778
return AVERROR_INVALIDDATA;
781
while (read <= 8 && !(total & len_mask)) {
786
return AVERROR_INVALIDDATA;
788
if ((total &= (len_mask - 1)) == len_mask - 1)
791
return AVERROR_INVALIDDATA;
795
total = (total << 8) | data[n];
808
* Same as above, but signed.
812
matroska_ebmlnum_sint (uint8_t *data,
819
/* read as unsigned number first */
820
if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
823
/* make signed (weird way) */
824
if (unum == (uint64_t)-1)
827
*num = unum - ((1LL << ((7 * res) - 1)) - 1);
833
* Read an EBML header.
834
* 0 is success, < 0 is failure.
838
ebml_read_header (MatroskaDemuxContext *matroska,
843
int level_up, res = 0;
851
if (!(id = ebml_peek_id(matroska, &level_up)) ||
852
level_up != 0 || id != EBML_ID_HEADER) {
853
av_log(matroska->ctx, AV_LOG_ERROR,
854
"This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
855
return AVERROR_INVALIDDATA;
857
if ((res = ebml_read_master(matroska, &id)) < 0)
861
if (!(id = ebml_peek_id(matroska, &level_up)))
869
/* is our read version uptodate? */
870
case EBML_ID_EBMLREADVERSION: {
873
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
875
if (num > EBML_VERSION) {
876
av_log(matroska->ctx, AV_LOG_ERROR,
877
"EBML version %"PRIu64" (> %d) is not supported\n",
879
return AVERROR_INVALIDDATA;
884
/* we only handle 8 byte lengths at max */
885
case EBML_ID_EBMLMAXSIZELENGTH: {
888
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
890
if (num > sizeof(uint64_t)) {
891
av_log(matroska->ctx, AV_LOG_ERROR,
892
"Integers of size %"PRIu64" (> %zd) not supported\n",
893
num, sizeof(uint64_t));
894
return AVERROR_INVALIDDATA;
899
/* we handle 4 byte IDs at max */
900
case EBML_ID_EBMLMAXIDLENGTH: {
903
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
905
if (num > sizeof(uint32_t)) {
906
av_log(matroska->ctx, AV_LOG_ERROR,
907
"IDs of size %"PRIu64" (> %zu) not supported\n",
908
num, sizeof(uint32_t));
909
return AVERROR_INVALIDDATA;
914
case EBML_ID_DOCTYPE: {
917
if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
928
case EBML_ID_DOCTYPEREADVERSION: {
931
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
939
av_log(matroska->ctx, AV_LOG_INFO,
940
"Unknown data type 0x%x in EBML header", id);
944
/* we ignore these two, as they don't tell us anything we
946
case EBML_ID_EBMLVERSION:
947
case EBML_ID_DOCTYPEVERSION:
948
res = ebml_read_skip (matroska);
957
* Put one packet in an application-supplied AVPacket struct.
958
* Returns 0 on success or -1 on failure.
962
matroska_deliver_packet (MatroskaDemuxContext *matroska,
965
if (matroska->num_packets > 0) {
966
memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
967
av_free(matroska->packets[0]);
968
if (matroska->num_packets > 1) {
969
memmove(&matroska->packets[0], &matroska->packets[1],
970
(matroska->num_packets - 1) * sizeof(AVPacket *));
972
av_realloc(matroska->packets, (matroska->num_packets - 1) *
975
av_free(matroska->packets);
976
matroska->packets = NULL;
978
matroska->num_packets--;
986
* Put a packet into our internal queue. Will be delivered to the
987
* user/application during the next get_packet() call.
991
matroska_queue_packet (MatroskaDemuxContext *matroska,
995
av_realloc(matroska->packets, (matroska->num_packets + 1) *
997
matroska->packets[matroska->num_packets] = pkt;
998
matroska->num_packets++;
1006
matroska_probe (AVProbeData *p)
1009
int len_mask = 0x80, size = 1, n = 1;
1010
uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
1012
if (p->buf_size < 5)
1016
if ((p->buf[0] << 24 | p->buf[1] << 16 |
1017
p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
1020
/* length of header */
1022
while (size <= 8 && !(total & len_mask)) {
1028
total &= (len_mask - 1);
1030
total = (total << 8) | p->buf[4 + n++];
1032
/* does the probe data contain the whole header? */
1033
if (p->buf_size < 4 + size + total)
1036
/* the header must contain the document type 'matroska'. For now,
1037
* we don't parse the whole header but simply check for the
1038
* availability of that array of characters inside the header.
1039
* Not fully fool-proof, but good enough. */
1040
for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
1041
if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
1042
return AVPROBE_SCORE_MAX;
1048
* From here on, it's all XML-style DTD stuff... Needs no comments.
1052
matroska_parse_info (MatroskaDemuxContext *matroska)
1057
av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
1060
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1063
} else if (matroska->level_up) {
1064
matroska->level_up--;
1069
/* cluster timecode */
1070
case MATROSKA_ID_TIMECODESCALE: {
1072
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1074
matroska->time_scale = num;
1078
case MATROSKA_ID_DURATION: {
1080
if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1082
matroska->duration = num * matroska->time_scale;
1086
case MATROSKA_ID_WRITINGAPP: {
1088
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1090
matroska->writing_app = text;
1094
case MATROSKA_ID_MUXINGAPP: {
1096
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1098
matroska->muxing_app = text;
1102
case MATROSKA_ID_DATEUTC: {
1104
if ((res = ebml_read_date(matroska, &id, &time)) < 0)
1106
matroska->created = time;
1111
av_log(matroska->ctx, AV_LOG_INFO,
1112
"Unknown entry 0x%x in info header\n", id);
1116
res = ebml_read_skip(matroska);
1120
if (matroska->level_up) {
1121
matroska->level_up--;
1130
matroska_add_stream (MatroskaDemuxContext *matroska)
1134
MatroskaTrack *track;
1136
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1138
/* Allocate a generic track. As soon as we know its type we'll realloc. */
1139
track = av_mallocz(sizeof(MatroskaTrack));
1140
matroska->num_tracks++;
1142
/* start with the master */
1143
if ((res = ebml_read_master(matroska, &id)) < 0)
1146
/* try reading the trackentry headers */
1148
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1151
} else if (matroska->level_up > 0) {
1152
matroska->level_up--;
1157
/* track number (unique stream ID) */
1158
case MATROSKA_ID_TRACKNUMBER: {
1160
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1166
/* track UID (unique identifier) */
1167
case MATROSKA_ID_TRACKUID: {
1169
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1175
/* track type (video, audio, combined, subtitle, etc.) */
1176
case MATROSKA_ID_TRACKTYPE: {
1178
if (track->type != 0) {
1179
av_log(matroska->ctx, AV_LOG_INFO,
1180
"More than one tracktype in an entry - skip\n");
1183
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1187
/* ok, so we're actually going to reallocate this thing */
1188
switch (track->type) {
1189
case MATROSKA_TRACK_TYPE_VIDEO:
1190
track = (MatroskaTrack *)
1191
av_realloc(track, sizeof(MatroskaVideoTrack));
1193
case MATROSKA_TRACK_TYPE_AUDIO:
1194
track = (MatroskaTrack *)
1195
av_realloc(track, sizeof(MatroskaAudioTrack));
1196
((MatroskaAudioTrack *)track)->channels = 1;
1197
((MatroskaAudioTrack *)track)->samplerate = 8000;
1199
case MATROSKA_TRACK_TYPE_SUBTITLE:
1200
track = (MatroskaTrack *)
1201
av_realloc(track, sizeof(MatroskaSubtitleTrack));
1203
case MATROSKA_TRACK_TYPE_COMPLEX:
1204
case MATROSKA_TRACK_TYPE_LOGO:
1205
case MATROSKA_TRACK_TYPE_CONTROL:
1207
av_log(matroska->ctx, AV_LOG_INFO,
1208
"Unknown or unsupported track type 0x%x\n",
1213
matroska->tracks[matroska->num_tracks - 1] = track;
1217
/* tracktype specific stuff for video */
1218
case MATROSKA_ID_TRACKVIDEO: {
1219
MatroskaVideoTrack *videotrack;
1220
if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1221
av_log(matroska->ctx, AV_LOG_INFO,
1222
"video data in non-video track - ignoring\n");
1223
res = AVERROR_INVALIDDATA;
1225
} else if ((res = ebml_read_master(matroska, &id)) < 0)
1227
videotrack = (MatroskaVideoTrack *)track;
1230
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1233
} else if (matroska->level_up > 0) {
1234
matroska->level_up--;
1239
/* fixme, this should be one-up, but I get it here */
1240
case MATROSKA_ID_TRACKDEFAULTDURATION: {
1242
if ((res = ebml_read_uint (matroska, &id,
1245
track->default_duration = num;
1249
/* video framerate */
1250
case MATROSKA_ID_VIDEOFRAMERATE: {
1252
if ((res = ebml_read_float(matroska, &id,
1255
track->default_duration = 1000000000 * (1. / num);
1259
/* width of the size to display the video at */
1260
case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1262
if ((res = ebml_read_uint(matroska, &id,
1265
videotrack->display_width = num;
1269
/* height of the size to display the video at */
1270
case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1272
if ((res = ebml_read_uint(matroska, &id,
1275
videotrack->display_height = num;
1279
/* width of the video in the file */
1280
case MATROSKA_ID_VIDEOPIXELWIDTH: {
1282
if ((res = ebml_read_uint(matroska, &id,
1285
videotrack->pixel_width = num;
1289
/* height of the video in the file */
1290
case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1292
if ((res = ebml_read_uint(matroska, &id,
1295
videotrack->pixel_height = num;
1299
/* whether the video is interlaced */
1300
case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1302
if ((res = ebml_read_uint(matroska, &id,
1307
MATROSKA_VIDEOTRACK_INTERLACED;
1310
~MATROSKA_VIDEOTRACK_INTERLACED;
1314
/* stereo mode (whether the video has two streams,
1315
* where one is for the left eye and the other for
1316
* the right eye, which creates a 3D-like
1318
case MATROSKA_ID_VIDEOSTEREOMODE: {
1320
if ((res = ebml_read_uint(matroska, &id,
1323
if (num != MATROSKA_EYE_MODE_MONO &&
1324
num != MATROSKA_EYE_MODE_LEFT &&
1325
num != MATROSKA_EYE_MODE_RIGHT &&
1326
num != MATROSKA_EYE_MODE_BOTH) {
1327
av_log(matroska->ctx, AV_LOG_INFO,
1328
"Ignoring unknown eye mode 0x%x\n",
1332
videotrack->eye_mode = num;
1336
/* aspect ratio behaviour */
1337
case MATROSKA_ID_VIDEOASPECTRATIO: {
1339
if ((res = ebml_read_uint(matroska, &id,
1342
if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1343
num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1344
num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1345
av_log(matroska->ctx, AV_LOG_INFO,
1346
"Ignoring unknown aspect ratio 0x%x\n",
1350
videotrack->ar_mode = num;
1354
/* colourspace (only matters for raw video)
1356
case MATROSKA_ID_VIDEOCOLOURSPACE: {
1358
if ((res = ebml_read_uint(matroska, &id,
1361
videotrack->fourcc = num;
1366
av_log(matroska->ctx, AV_LOG_INFO,
1367
"Unknown video track header entry "
1368
"0x%x - ignoring\n", id);
1372
res = ebml_read_skip(matroska);
1376
if (matroska->level_up) {
1377
matroska->level_up--;
1384
/* tracktype specific stuff for audio */
1385
case MATROSKA_ID_TRACKAUDIO: {
1386
MatroskaAudioTrack *audiotrack;
1387
if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1388
av_log(matroska->ctx, AV_LOG_INFO,
1389
"audio data in non-audio track - ignoring\n");
1390
res = AVERROR_INVALIDDATA;
1392
} else if ((res = ebml_read_master(matroska, &id)) < 0)
1394
audiotrack = (MatroskaAudioTrack *)track;
1397
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1400
} else if (matroska->level_up > 0) {
1401
matroska->level_up--;
1407
case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1409
if ((res = ebml_read_float(matroska, &id,
1412
audiotrack->samplerate = num;
1417
case MATROSKA_ID_AUDIOBITDEPTH: {
1419
if ((res = ebml_read_uint(matroska, &id,
1422
audiotrack->bitdepth = num;
1427
case MATROSKA_ID_AUDIOCHANNELS: {
1429
if ((res = ebml_read_uint(matroska, &id,
1432
audiotrack->channels = num;
1437
av_log(matroska->ctx, AV_LOG_INFO,
1438
"Unknown audio track header entry "
1439
"0x%x - ignoring\n", id);
1443
res = ebml_read_skip(matroska);
1447
if (matroska->level_up) {
1448
matroska->level_up--;
1455
/* codec identifier */
1456
case MATROSKA_ID_CODECID: {
1458
if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1460
track->codec_id = text;
1464
/* codec private data */
1465
case MATROSKA_ID_CODECPRIVATE: {
1468
if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1470
track->codec_priv = data;
1471
track->codec_priv_size = size;
1475
/* name of the codec */
1476
case MATROSKA_ID_CODECNAME: {
1478
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1480
track->codec_name = text;
1484
/* name of this track */
1485
case MATROSKA_ID_TRACKNAME: {
1487
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1493
/* language (matters for audio/subtitles, mostly) */
1494
case MATROSKA_ID_TRACKLANGUAGE: {
1496
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1498
track->language = text;
1502
/* whether this is actually used */
1503
case MATROSKA_ID_TRACKFLAGENABLED: {
1505
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1508
track->flags |= MATROSKA_TRACK_ENABLED;
1510
track->flags &= ~MATROSKA_TRACK_ENABLED;
1514
/* whether it's the default for this track type */
1515
case MATROSKA_ID_TRACKFLAGDEFAULT: {
1517
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1520
track->flags |= MATROSKA_TRACK_DEFAULT;
1522
track->flags &= ~MATROSKA_TRACK_DEFAULT;
1526
/* lacing (like MPEG, where blocks don't end/start on frame
1528
case MATROSKA_ID_TRACKFLAGLACING: {
1530
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1533
track->flags |= MATROSKA_TRACK_LACING;
1535
track->flags &= ~MATROSKA_TRACK_LACING;
1539
/* default length (in time) of one data block in this track */
1540
case MATROSKA_ID_TRACKDEFAULTDURATION: {
1542
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1544
track->default_duration = num;
1549
av_log(matroska->ctx, AV_LOG_INFO,
1550
"Unknown track header entry 0x%x - ignoring\n", id);
1554
/* we ignore these because they're nothing useful. */
1555
case MATROSKA_ID_CODECINFOURL:
1556
case MATROSKA_ID_CODECDOWNLOADURL:
1557
case MATROSKA_ID_TRACKMINCACHE:
1558
case MATROSKA_ID_TRACKMAXCACHE:
1559
res = ebml_read_skip(matroska);
1563
if (matroska->level_up) {
1564
matroska->level_up--;
1573
matroska_parse_tracks (MatroskaDemuxContext *matroska)
1578
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1581
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1584
} else if (matroska->level_up) {
1585
matroska->level_up--;
1590
/* one track within the "all-tracks" header */
1591
case MATROSKA_ID_TRACKENTRY:
1592
res = matroska_add_stream(matroska);
1596
av_log(matroska->ctx, AV_LOG_INFO,
1597
"Unknown entry 0x%x in track header\n", id);
1601
res = ebml_read_skip(matroska);
1605
if (matroska->level_up) {
1606
matroska->level_up--;
1615
matroska_parse_index (MatroskaDemuxContext *matroska)
1619
MatroskaDemuxIndex idx;
1621
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1624
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1627
} else if (matroska->level_up) {
1628
matroska->level_up--;
1633
/* one single index entry ('point') */
1634
case MATROSKA_ID_POINTENTRY:
1635
if ((res = ebml_read_master(matroska, &id)) < 0)
1638
/* in the end, we hope to fill one entry with a
1639
* timestamp, a file position and a tracknum */
1640
idx.pos = (uint64_t) -1;
1641
idx.time = (uint64_t) -1;
1642
idx.track = (uint16_t) -1;
1645
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1648
} else if (matroska->level_up) {
1649
matroska->level_up--;
1654
/* one single index entry ('point') */
1655
case MATROSKA_ID_CUETIME: {
1657
if ((res = ebml_read_uint(matroska, &id,
1660
idx.time = time * matroska->time_scale;
1664
/* position in the file + track to which it
1666
case MATROSKA_ID_CUETRACKPOSITION:
1667
if ((res = ebml_read_master(matroska, &id)) < 0)
1671
if (!(id = ebml_peek_id (matroska,
1672
&matroska->level_up))) {
1675
} else if (matroska->level_up) {
1676
matroska->level_up--;
1682
case MATROSKA_ID_CUETRACK: {
1684
if ((res = ebml_read_uint(matroska,
1691
/* position in file */
1692
case MATROSKA_ID_CUECLUSTERPOSITION: {
1694
if ((res = ebml_read_uint(matroska,
1702
av_log(matroska->ctx, AV_LOG_INFO,
1703
"Unknown entry 0x%x in "
1704
"CuesTrackPositions\n", id);
1708
res = ebml_read_skip(matroska);
1712
if (matroska->level_up) {
1713
matroska->level_up--;
1721
av_log(matroska->ctx, AV_LOG_INFO,
1722
"Unknown entry 0x%x in cuespoint "
1727
res = ebml_read_skip(matroska);
1731
if (matroska->level_up) {
1732
matroska->level_up--;
1737
/* so let's see if we got what we wanted */
1738
if (idx.pos != (uint64_t) -1 &&
1739
idx.time != (uint64_t) -1 &&
1740
idx.track != (uint16_t) -1) {
1741
if (matroska->num_indexes % 32 == 0) {
1742
/* re-allocate bigger index */
1744
av_realloc(matroska->index,
1745
(matroska->num_indexes + 32) *
1746
sizeof(MatroskaDemuxIndex));
1748
matroska->index[matroska->num_indexes] = idx;
1749
matroska->num_indexes++;
1754
av_log(matroska->ctx, AV_LOG_INFO,
1755
"Unknown entry 0x%x in cues header\n", id);
1759
res = ebml_read_skip(matroska);
1763
if (matroska->level_up) {
1764
matroska->level_up--;
1773
matroska_parse_metadata (MatroskaDemuxContext *matroska)
1779
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1782
} else if (matroska->level_up) {
1783
matroska->level_up--;
1788
/* Hm, this is unsupported... */
1790
av_log(matroska->ctx, AV_LOG_INFO,
1791
"Unknown entry 0x%x in metadata header\n", id);
1795
res = ebml_read_skip(matroska);
1799
if (matroska->level_up) {
1800
matroska->level_up--;
1809
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1814
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1817
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1820
} else if (matroska->level_up) {
1821
matroska->level_up--;
1826
case MATROSKA_ID_SEEKENTRY: {
1827
uint32_t seek_id = 0, peek_id_cache = 0;
1828
uint64_t seek_pos = (uint64_t) -1, t;
1830
if ((res = ebml_read_master(matroska, &id)) < 0)
1834
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1837
} else if (matroska->level_up) {
1838
matroska->level_up--;
1843
case MATROSKA_ID_SEEKID:
1844
res = ebml_read_uint(matroska, &id, &t);
1848
case MATROSKA_ID_SEEKPOSITION:
1849
res = ebml_read_uint(matroska, &id, &seek_pos);
1853
av_log(matroska->ctx, AV_LOG_INFO,
1854
"Unknown seekhead ID 0x%x\n", id);
1858
res = ebml_read_skip(matroska);
1862
if (matroska->level_up) {
1863
matroska->level_up--;
1868
if (!seek_id || seek_pos == (uint64_t) -1) {
1869
av_log(matroska->ctx, AV_LOG_INFO,
1870
"Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1876
case MATROSKA_ID_CUES:
1877
case MATROSKA_ID_TAGS: {
1878
uint32_t level_up = matroska->level_up;
1879
offset_t before_pos;
1881
MatroskaLevel level;
1883
/* remember the peeked ID and the current position */
1884
peek_id_cache = matroska->peek_id;
1885
before_pos = url_ftell(&matroska->ctx->pb);
1888
if ((res = ebml_read_seek(matroska, seek_pos +
1889
matroska->segment_start)) < 0)
1892
/* we don't want to lose our seekhead level, so we add
1893
* a dummy. This is a crude hack. */
1894
if (matroska->num_levels == EBML_MAX_DEPTH) {
1895
av_log(matroska->ctx, AV_LOG_INFO,
1896
"Max EBML element depth (%d) reached, "
1897
"cannot parse further.\n", EBML_MAX_DEPTH);
1898
return AVERROR_UNKNOWN;
1902
level.length = (uint64_t)-1;
1903
matroska->levels[matroska->num_levels] = level;
1904
matroska->num_levels++;
1907
if (!(id = ebml_peek_id (matroska,
1908
&matroska->level_up)))
1910
if (id != seek_id) {
1911
av_log(matroska->ctx, AV_LOG_INFO,
1912
"We looked for ID=0x%x but got "
1913
"ID=0x%x (pos=%"PRIu64")",
1914
seek_id, id, seek_pos +
1915
matroska->segment_start);
1919
/* read master + parse */
1920
if ((res = ebml_read_master(matroska, &id)) < 0)
1923
case MATROSKA_ID_CUES:
1924
if (!(res = matroska_parse_index(matroska)) ||
1925
url_feof(&matroska->ctx->pb)) {
1926
matroska->index_parsed = 1;
1930
case MATROSKA_ID_TAGS:
1931
if (!(res = matroska_parse_metadata(matroska)) ||
1932
url_feof(&matroska->ctx->pb)) {
1933
matroska->metadata_parsed = 1;
1942
/* remove dummy level */
1943
while (matroska->num_levels) {
1944
matroska->num_levels--;
1946
matroska->levels[matroska->num_levels].length;
1947
if (length == (uint64_t)-1)
1952
if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1954
matroska->peek_id = peek_id_cache;
1955
matroska->level_up = level_up;
1960
av_log(matroska->ctx, AV_LOG_INFO,
1961
"Ignoring seekhead entry for ID=0x%x\n",
1970
av_log(matroska->ctx, AV_LOG_INFO,
1971
"Unknown seekhead ID 0x%x\n", id);
1975
res = ebml_read_skip(matroska);
1979
if (matroska->level_up) {
1980
matroska->level_up--;
1989
matroska_read_header (AVFormatContext *s,
1990
AVFormatParameters *ap)
1992
MatroskaDemuxContext *matroska = s->priv_data;
1994
int version, last_level, res = 0;
1999
/* First read the EBML header. */
2001
if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2003
if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2004
av_log(matroska->ctx, AV_LOG_ERROR,
2005
"Wrong EBML doctype ('%s' != 'matroska').\n",
2006
doctype ? doctype : "(none)");
2009
return AVERROR_NOFMT;
2013
av_log(matroska->ctx, AV_LOG_ERROR,
2014
"Matroska demuxer version 1 too old for file version %d\n",
2016
return AVERROR_NOFMT;
2019
/* The next thing is a segment. */
2021
if (!(id = ebml_peek_id(matroska, &last_level)))
2023
if (id == MATROSKA_ID_SEGMENT)
2027
av_log(matroska->ctx, AV_LOG_INFO,
2028
"Expected a Segment ID (0x%x), but received 0x%x!\n",
2029
MATROSKA_ID_SEGMENT, id);
2030
if ((res = ebml_read_skip(matroska)) < 0)
2034
/* We now have a Matroska segment.
2035
* Seeks are from the beginning of the segment,
2036
* after the segment ID/length. */
2037
if ((res = ebml_read_master(matroska, &id)) < 0)
2039
matroska->segment_start = url_ftell(&s->pb);
2041
matroska->time_scale = 1000000;
2042
/* we've found our segment, start reading the different contents in here */
2044
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2047
} else if (matroska->level_up) {
2048
matroska->level_up--;
2054
case MATROSKA_ID_INFO: {
2055
if ((res = ebml_read_master(matroska, &id)) < 0)
2057
res = matroska_parse_info(matroska);
2061
/* track info headers */
2062
case MATROSKA_ID_TRACKS: {
2063
if ((res = ebml_read_master(matroska, &id)) < 0)
2065
res = matroska_parse_tracks(matroska);
2070
case MATROSKA_ID_CUES: {
2071
if (!matroska->index_parsed) {
2072
if ((res = ebml_read_master(matroska, &id)) < 0)
2074
res = matroska_parse_index(matroska);
2076
res = ebml_read_skip(matroska);
2081
case MATROSKA_ID_TAGS: {
2082
if (!matroska->metadata_parsed) {
2083
if ((res = ebml_read_master(matroska, &id)) < 0)
2085
res = matroska_parse_metadata(matroska);
2087
res = ebml_read_skip(matroska);
2091
/* file index (if seekable, seek to Cues/Tags to parse it) */
2092
case MATROSKA_ID_SEEKHEAD: {
2093
if ((res = ebml_read_master(matroska, &id)) < 0)
2095
res = matroska_parse_seekhead(matroska);
2099
case MATROSKA_ID_CLUSTER: {
2100
/* Do not read the master - this will be done in the next
2101
* call to matroska_read_packet. */
2107
av_log(matroska->ctx, AV_LOG_INFO,
2108
"Unknown matroska file header ID 0x%x\n", id);
2112
res = ebml_read_skip(matroska);
2116
if (matroska->level_up) {
2117
matroska->level_up--;
2125
/* Have we found a cluster? */
2128
enum CodecID codec_id;
2129
MatroskaTrack *track;
2131
void *extradata = NULL;
2132
int extradata_size = 0;
2134
for (i = 0; i < matroska->num_tracks; i++) {
2135
track = matroska->tracks[i];
2137
/* libavformat does not really support subtitles.
2138
* Also apply some sanity checks. */
2139
if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
2140
(track->codec_id == NULL))
2143
/* Set the FourCC from the CodecID. */
2144
/* This is the MS compatibility mode which stores a
2145
* BITMAPINFOHEADER in the CodecPrivate. */
2146
if (!strcmp(track->codec_id,
2147
MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2148
(track->codec_priv_size >= 40) &&
2149
(track->codec_priv != NULL)) {
2152
/* Offset of biCompression. Stored in LE. */
2153
p = (unsigned char *)track->codec_priv + 16;
2154
((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
2155
(p[2] << 16) | (p[1] << 8) | p[0];
2156
codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
2158
} else if (!strcmp(track->codec_id,
2159
MATROSKA_CODEC_ID_VIDEO_MPEG4_SP) ||
2160
!strcmp(track->codec_id,
2161
MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP) ||
2162
!strcmp(track->codec_id,
2163
MATROSKA_CODEC_ID_VIDEO_MPEG4_AP))
2164
codec_id = CODEC_ID_MPEG4;
2165
else if (!strcmp(track->codec_id,
2166
MATROSKA_CODEC_ID_VIDEO_MPEG4_AVC))
2167
codec_id = CODEC_ID_H264;
2168
/* else if (!strcmp(track->codec_id, */
2169
/* MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED)) */
2170
/* codec_id = CODEC_ID_???; */
2171
else if (!strcmp(track->codec_id,
2172
MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3))
2173
codec_id = CODEC_ID_MSMPEG4V3;
2174
else if (!strcmp(track->codec_id,
2175
MATROSKA_CODEC_ID_VIDEO_MPEG1) ||
2176
!strcmp(track->codec_id,
2177
MATROSKA_CODEC_ID_VIDEO_MPEG2))
2178
codec_id = CODEC_ID_MPEG2VIDEO;
2180
/* This is the MS compatibility mode which stores a
2181
* WAVEFORMATEX in the CodecPrivate. */
2182
else if (!strcmp(track->codec_id,
2183
MATROSKA_CODEC_ID_AUDIO_ACM) &&
2184
(track->codec_priv_size >= 18) &&
2185
(track->codec_priv != NULL)) {
2189
/* Offset of wFormatTag. Stored in LE. */
2190
p = (unsigned char *)track->codec_priv;
2191
tag = (p[1] << 8) | p[0];
2192
codec_id = codec_get_wav_id(tag);
2194
} else if (!strcmp(track->codec_id,
2195
MATROSKA_CODEC_ID_AUDIO_MPEG1_L1) ||
2196
!strcmp(track->codec_id,
2197
MATROSKA_CODEC_ID_AUDIO_MPEG1_L2) ||
2198
!strcmp(track->codec_id,
2199
MATROSKA_CODEC_ID_AUDIO_MPEG1_L3))
2200
codec_id = CODEC_ID_MP3;
2201
else if (!strcmp(track->codec_id,
2202
MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE))
2203
codec_id = CODEC_ID_PCM_U16BE;
2204
else if (!strcmp(track->codec_id,
2205
MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE))
2206
codec_id = CODEC_ID_PCM_U16LE;
2207
/* else if (!strcmp(track->codec_id, */
2208
/* MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT)) */
2209
/* codec_id = CODEC_ID_PCM_???; */
2210
else if (!strcmp(track->codec_id,
2211
MATROSKA_CODEC_ID_AUDIO_AC3))
2212
codec_id = CODEC_ID_AC3;
2213
else if (!strcmp(track->codec_id,
2214
MATROSKA_CODEC_ID_AUDIO_DTS))
2215
codec_id = CODEC_ID_DTS;
2216
/* No such codec id so far. */
2217
/* else if (!strcmp(track->codec_id, */
2218
/* MATROSKA_CODEC_ID_AUDIO_DTS)) */
2219
/* codec_id = CODEC_ID_DTS; */
2220
else if (!strcmp(track->codec_id,
2221
MATROSKA_CODEC_ID_AUDIO_VORBIS)) {
2222
extradata_size = track->codec_priv_size;
2223
if(extradata_size) {
2224
extradata = av_malloc(extradata_size);
2225
if(extradata == NULL)
2226
return AVERROR_NOMEM;
2227
memcpy(extradata, track->codec_priv, extradata_size);
2229
codec_id = CODEC_ID_VORBIS;
2230
} else if (!strcmp(track->codec_id,
2231
MATROSKA_CODEC_ID_AUDIO_MPEG2) ||
2232
!strcmp(track->codec_id,
2233
MATROSKA_CODEC_ID_AUDIO_MPEG4))
2234
codec_id = CODEC_ID_AAC;
2236
codec_id = CODEC_ID_NONE;
2238
if (codec_id == CODEC_ID_NONE) {
2239
av_log(matroska->ctx, AV_LOG_INFO,
2240
"Unknown/unsupported CodecID %s.\n",
2244
track->stream_index = matroska->num_streams;
2246
matroska->num_streams++;
2247
st = av_new_stream(s, track->stream_index);
2249
return AVERROR_NOMEM;
2250
av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
2252
st->codec->codec_id = codec_id;
2255
st->codec->extradata = extradata;
2256
st->codec->extradata_size = extradata_size;
2257
} else if(track->codec_priv && track->codec_priv_size > 0){
2258
st->codec->extradata = av_malloc(track->codec_priv_size);
2259
if(st->codec->extradata == NULL)
2260
return AVERROR_NOMEM;
2261
st->codec->extradata_size = track->codec_priv_size;
2262
memcpy(st->codec->extradata, track->codec_priv,
2263
track->codec_priv_size);
2266
if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2267
MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2269
st->codec->codec_type = CODEC_TYPE_VIDEO;
2270
st->codec->codec_tag = videotrack->fourcc;
2271
st->codec->width = videotrack->pixel_width;
2272
st->codec->height = videotrack->pixel_height;
2273
if (videotrack->display_width == 0)
2274
st->codec->sample_aspect_ratio.num =
2275
videotrack->pixel_width;
2277
st->codec->sample_aspect_ratio.num =
2278
videotrack->display_width;
2279
if (videotrack->display_height == 0)
2280
st->codec->sample_aspect_ratio.num =
2281
videotrack->pixel_height;
2283
st->codec->sample_aspect_ratio.num =
2284
videotrack->display_height;
2286
} else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2287
MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2289
st->codec->codec_type = CODEC_TYPE_AUDIO;
2290
st->codec->sample_rate = audiotrack->samplerate;
2291
st->codec->channels = audiotrack->channels;
2294
/* What do we do with private data? E.g. for Vorbis. */
2302
matroska_find_track_by_num (MatroskaDemuxContext *matroska,
2307
for (i = 0; i < matroska->num_tracks; i++)
2308
if (matroska->tracks[i]->num == num)
2315
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2316
uint64_t cluster_time)
2321
int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2323
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2326
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2329
} else if (matroska->level_up) {
2330
matroska->level_up--;
2335
/* one block inside the group. Note, block parsing is one
2336
* of the harder things, so this code is a bit complicated.
2337
* See http://www.matroska.org/ for documentation. */
2338
case MATROSKA_ID_BLOCK: {
2339
uint8_t *data, *origdata;
2342
uint32_t *lace_size = NULL;
2343
int n, track, flags, laces = 0;
2345
int64_t pos= url_ftell(&matroska->ctx->pb);
2347
if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
2351
/* first byte(s): blocknum */
2352
if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2353
av_log(matroska->ctx, AV_LOG_ERROR,
2354
"EBML block data error\n");
2361
/* fetch track from num */
2362
track = matroska_find_track_by_num(matroska, num);
2363
if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2364
av_log(matroska->ctx, AV_LOG_INFO,
2365
"Invalid stream %d or size %u\n", track, size);
2369
if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
2374
/* time (relative to cluster time) */
2375
time = ((data[0] << 8) | data[1]) * matroska->time_scale;
2381
switch ((flags & 0x06) >> 1) {
2382
case 0x0: /* no lacing */
2384
lace_size = av_mallocz(sizeof(int));
2385
lace_size[0] = size;
2388
case 0x1: /* xiph lacing */
2389
case 0x2: /* fixed-size lacing */
2390
case 0x3: /* EBML lacing */
2395
laces = (*data) + 1;
2398
lace_size = av_mallocz(laces * sizeof(int));
2400
switch ((flags & 0x06) >> 1) {
2401
case 0x1: /* xiph lacing */ {
2404
for (n = 0; res == 0 && n < laces - 1; n++) {
2411
lace_size[n] += temp;
2417
total += lace_size[n];
2419
lace_size[n] = size - total;
2423
case 0x2: /* fixed-size lacing */
2424
for (n = 0; n < laces; n++)
2425
lace_size[n] = size / laces;
2428
case 0x3: /* EBML lacing */ {
2430
n = matroska_ebmlnum_uint(data, size, &num);
2432
av_log(matroska->ctx, AV_LOG_INFO,
2433
"EBML block data error\n");
2438
total = lace_size[0] = num;
2439
for (n = 1; res == 0 && n < laces - 1; n++) {
2442
r = matroska_ebmlnum_sint (data, size,
2445
av_log(matroska->ctx, AV_LOG_INFO,
2446
"EBML block data error\n");
2451
lace_size[n] = lace_size[n - 1] + snum;
2452
total += lace_size[n];
2454
lace_size[n] = size - total;
2462
for (n = 0; n < laces; n++) {
2463
uint64_t timecode = 0;
2465
pkt = av_mallocz(sizeof(AVPacket));
2466
/* XXX: prevent data copy... */
2467
if (av_new_packet(pkt,lace_size[n]) < 0) {
2468
res = AVERROR_NOMEM;
2471
if (cluster_time != (uint64_t)-1) {
2472
if (time < 0 && (-time) > cluster_time)
2473
timecode = cluster_time;
2475
timecode = cluster_time + time;
2477
/* FIXME: duration */
2479
memcpy(pkt->data, data, lace_size[n]);
2480
data += lace_size[n];
2482
pkt->flags = is_keyframe;
2484
matroska->tracks[track]->stream_index;
2486
pkt->pts = timecode / 1000000; /* ns to ms */
2489
matroska_queue_packet(matroska, pkt);
2498
case MATROSKA_ID_BLOCKDURATION: {
2500
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2502
av_log(matroska->ctx, AV_LOG_INFO,
2503
"FIXME: implement support for BlockDuration\n");
2507
case MATROSKA_ID_BLOCKREFERENCE:
2508
/* We've found a reference, so not even the first frame in
2509
* the lace is a key frame. */
2511
if (last_num_packets != matroska->num_packets)
2512
matroska->packets[last_num_packets]->flags = 0;
2513
res = ebml_read_skip(matroska);
2517
av_log(matroska->ctx, AV_LOG_INFO,
2518
"Unknown entry 0x%x in blockgroup data\n", id);
2522
res = ebml_read_skip(matroska);
2526
if (matroska->level_up) {
2527
matroska->level_up--;
2536
matroska_parse_cluster (MatroskaDemuxContext *matroska)
2540
uint64_t cluster_time = 0;
2542
av_log(matroska->ctx, AV_LOG_DEBUG,
2543
"parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
2546
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2549
} else if (matroska->level_up) {
2550
matroska->level_up--;
2555
/* cluster timecode */
2556
case MATROSKA_ID_CLUSTERTIMECODE: {
2558
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2560
cluster_time = num * matroska->time_scale;
2564
/* a group of blocks inside a cluster */
2565
case MATROSKA_ID_BLOCKGROUP:
2566
if ((res = ebml_read_master(matroska, &id)) < 0)
2568
res = matroska_parse_blockgroup(matroska, cluster_time);
2572
av_log(matroska->ctx, AV_LOG_INFO,
2573
"Unknown entry 0x%x in cluster data\n", id);
2577
res = ebml_read_skip(matroska);
2581
if (matroska->level_up) {
2582
matroska->level_up--;
2591
matroska_read_packet (AVFormatContext *s,
2594
MatroskaDemuxContext *matroska = s->priv_data;
2598
/* Do we still have a packet queued? */
2599
if (matroska_deliver_packet(matroska, pkt) == 0)
2602
/* Have we already reached the end? */
2607
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2610
} else if (matroska->level_up) {
2611
matroska->level_up--;
2616
case MATROSKA_ID_CLUSTER:
2617
if ((res = ebml_read_master(matroska, &id)) < 0)
2619
if ((res = matroska_parse_cluster(matroska)) == 0)
2620
res = 1; /* Parsed one cluster, let's get out. */
2625
res = ebml_read_skip(matroska);
2629
if (matroska->level_up) {
2630
matroska->level_up--;
2638
return matroska_deliver_packet(matroska, pkt);
2642
matroska_read_close (AVFormatContext *s)
2644
MatroskaDemuxContext *matroska = s->priv_data;
2647
if (matroska->writing_app)
2648
av_free(matroska->writing_app);
2649
if (matroska->muxing_app)
2650
av_free(matroska->muxing_app);
2651
if (matroska->index)
2652
av_free(matroska->index);
2654
if (matroska->packets != NULL) {
2655
for (n = 0; n < matroska->num_packets; n++) {
2656
av_free_packet(matroska->packets[n]);
2657
av_free(matroska->packets[n]);
2659
av_free(matroska->packets);
2662
for (n = 0; n < matroska->num_tracks; n++) {
2663
MatroskaTrack *track = matroska->tracks[n];
2664
if (track->codec_id)
2665
av_free(track->codec_id);
2666
if (track->codec_name)
2667
av_free(track->codec_name);
2668
if (track->codec_priv)
2669
av_free(track->codec_priv);
2671
av_free(track->name);
2672
if (track->language)
2673
av_free(track->language);
2678
for (n = 0; n < s->nb_streams; n++) {
2679
av_free(s->streams[n]->codec->extradata);
2682
memset(matroska, 0, sizeof(MatroskaDemuxContext));
2687
static AVInputFormat matroska_iformat = {
2689
"Matroska file format",
2690
sizeof(MatroskaDemuxContext),
2692
matroska_read_header,
2693
matroska_read_packet,
2694
matroska_read_close,
2700
av_register_input_format(&matroska_iformat);