2 * native ebml reader for the Matroska demuxer
3 * copyright (c) 2004 Aurelien Jacobs <aurel@gnuage.org>
4 * based on the one written by Ronald Bultje for gstreamer
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "stream/stream.h"
29 #include "libavutil/common.h"
31 #include "libavutil/intfloat_readwrite.h"
35 #define SIZE_MAX ((size_t)-1)
39 * Read: the element content data ID.
43 ebml_read_id (stream_t
*s
, int *length
)
45 int i
, len_mask
= 0x80;
48 for (i
=0, id
=stream_read_char (s
); i
<4 && !(id
& len_mask
); i
++)
51 return EBML_ID_INVALID
;
55 id
= (id
<< 8) | stream_read_char (s
);
60 * Read a variable length unsigned int.
63 ebml_read_vlen_uint (uint8_t *buffer
, int *length
)
65 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
68 for (i
=0, num
=*buffer
++; i
<8 && !(num
& len_mask
); i
++)
71 return EBML_UINT_INVALID
;
75 if ((int)(num
&= (len_mask
- 1)) == len_mask
- 1)
79 num
= (num
<< 8) | *buffer
++;
80 if ((num
& 0xFF) == 0xFF)
84 return EBML_UINT_INVALID
;
89 * Read a variable length signed int.
92 ebml_read_vlen_int (uint8_t *buffer
, int *length
)
97 /* read as unsigned number first */
98 unum
= ebml_read_vlen_uint (buffer
, &l
);
99 if (unum
== EBML_UINT_INVALID
)
100 return EBML_INT_INVALID
;
104 return unum
- ((1 << ((7 * l
) - 1)) - 1);
108 * Read: element content length.
111 ebml_read_length (stream_t
*s
, int *length
)
113 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
116 for (i
=0, len
=stream_read_char (s
); i
<8 && !(len
& len_mask
); i
++)
119 return EBML_UINT_INVALID
;
123 if ((int)(len
&= (len_mask
- 1)) == len_mask
- 1)
127 len
= (len
<< 8) | stream_read_char (s
);
128 if ((len
& 0xFF) == 0xFF)
132 return EBML_UINT_INVALID
;
137 * Read the next element as an unsigned int.
140 ebml_read_uint (stream_t
*s
, uint64_t *length
)
142 uint64_t len
, value
= 0;
145 len
= ebml_read_length (s
, &l
);
146 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
147 return EBML_UINT_INVALID
;
152 value
= (value
<< 8) | stream_read_char (s
);
158 * Read the next element as a signed int.
161 ebml_read_int (stream_t
*s
, uint64_t *length
)
167 len
= ebml_read_length (s
, &l
);
168 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
169 return EBML_INT_INVALID
;
174 l
= stream_read_char (s
);
177 value
= (value
<< 8) | l
;
179 value
= (value
<< 8) | stream_read_char (s
);
185 * Read the next element as a float.
188 ebml_read_float (stream_t
*s
, uint64_t *length
)
194 len
= ebml_read_length (s
, &l
);
198 value
= av_int2flt(stream_read_dword(s
));
202 value
= av_int2dbl(stream_read_qword(s
));
206 return EBML_FLOAT_INVALID
;
216 * Read the next element as an ASCII string.
219 ebml_read_ascii (stream_t
*s
, uint64_t *length
)
225 len
= ebml_read_length (s
, &l
);
226 if (len
== EBML_UINT_INVALID
)
228 if (len
> SIZE_MAX
- 1)
233 str
= (char *) malloc (len
+1);
234 if (stream_read(s
, str
, len
) != (int) len
)
245 * Read the next element as a UTF-8 string.
248 ebml_read_utf8 (stream_t
*s
, uint64_t *length
)
250 return ebml_read_ascii (s
, length
);
254 * Skip the next element.
257 ebml_read_skip (stream_t
*s
, uint64_t *length
)
262 len
= ebml_read_length (s
, &l
);
263 if (len
== EBML_UINT_INVALID
)
274 * Read the next element, but only the header. The contents
275 * are supposed to be sub-elements which can be read separately.
278 ebml_read_master (stream_t
*s
, uint64_t *length
)
283 id
= ebml_read_id (s
, NULL
);
284 if (id
== EBML_ID_INVALID
)
287 len
= ebml_read_length (s
, NULL
);
288 if (len
== EBML_UINT_INVALID
)
289 return EBML_ID_INVALID
;
298 * Read an EBML header.
301 ebml_read_header (stream_t
*s
, int *version
)
303 uint64_t length
, l
, num
;
307 if (ebml_read_master (s
, &length
) != EBML_ID_HEADER
)
315 id
= ebml_read_id (s
, NULL
);
316 if (id
== EBML_ID_INVALID
)
322 /* is our read version uptodate? */
323 case EBML_ID_EBMLREADVERSION
:
324 num
= ebml_read_uint (s
, &l
);
325 if (num
!= EBML_VERSION
)
329 /* we only handle 8 byte lengths at max */
330 case EBML_ID_EBMLMAXSIZELENGTH
:
331 num
= ebml_read_uint (s
, &l
);
332 if (num
!= sizeof (uint64_t))
336 /* we handle 4 byte IDs at max */
337 case EBML_ID_EBMLMAXIDLENGTH
:
338 num
= ebml_read_uint (s
, &l
);
339 if (num
!= sizeof (uint32_t))
343 case EBML_ID_DOCTYPE
:
344 str
= ebml_read_ascii (s
, &l
);
349 case EBML_ID_DOCTYPEREADVERSION
:
350 num
= ebml_read_uint (s
, &l
);
351 if (num
== EBML_UINT_INVALID
)
357 /* we ignore these two, they don't tell us anything we care about */
359 case EBML_ID_EBMLVERSION
:
360 case EBML_ID_DOCTYPEVERSION
:
362 if (ebml_read_skip (s
, &l
))