2 * native ebml reader for the Matroska demuxer
3 * Written by Aurelien Jacobs <aurel@gnuage.org>
4 * Based on the one written by Ronald Bultje for gstreamer
12 #include "stream/stream.h"
14 #include "libavutil/common.h"
16 #include "libavutil/intfloat_readwrite.h"
20 #define SIZE_MAX ((size_t)-1)
24 * Read: the element content data ID.
28 ebml_read_id (stream_t
*s
, int *length
)
30 int i
, len_mask
= 0x80;
33 for (i
=0, id
=stream_read_char (s
); i
<4 && !(id
& len_mask
); i
++)
36 return EBML_ID_INVALID
;
40 id
= (id
<< 8) | stream_read_char (s
);
45 * Read a variable length unsigned int.
48 ebml_read_vlen_uint (uint8_t *buffer
, int *length
)
50 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
53 for (i
=0, num
=*buffer
++; i
<8 && !(num
& len_mask
); i
++)
56 return EBML_UINT_INVALID
;
60 if ((int)(num
&= (len_mask
- 1)) == len_mask
- 1)
64 num
= (num
<< 8) | *buffer
++;
65 if ((num
& 0xFF) == 0xFF)
69 return EBML_UINT_INVALID
;
74 * Read a variable length signed int.
77 ebml_read_vlen_int (uint8_t *buffer
, int *length
)
82 /* read as unsigned number first */
83 unum
= ebml_read_vlen_uint (buffer
, &l
);
84 if (unum
== EBML_UINT_INVALID
)
85 return EBML_INT_INVALID
;
89 return unum
- ((1 << ((7 * l
) - 1)) - 1);
93 * Read: element content length.
96 ebml_read_length (stream_t
*s
, int *length
)
98 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
101 for (i
=0, len
=stream_read_char (s
); i
<8 && !(len
& len_mask
); i
++)
104 return EBML_UINT_INVALID
;
108 if ((int)(len
&= (len_mask
- 1)) == len_mask
- 1)
112 len
= (len
<< 8) | stream_read_char (s
);
113 if ((len
& 0xFF) == 0xFF)
117 return EBML_UINT_INVALID
;
122 * Read the next element as an unsigned int.
125 ebml_read_uint (stream_t
*s
, uint64_t *length
)
127 uint64_t len
, value
= 0;
130 len
= ebml_read_length (s
, &l
);
131 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
132 return EBML_UINT_INVALID
;
137 value
= (value
<< 8) | stream_read_char (s
);
143 * Read the next element as a signed int.
146 ebml_read_int (stream_t
*s
, uint64_t *length
)
152 len
= ebml_read_length (s
, &l
);
153 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
154 return EBML_INT_INVALID
;
159 l
= stream_read_char (s
);
162 value
= (value
<< 8) | l
;
164 value
= (value
<< 8) | stream_read_char (s
);
170 * Read the next element as a float.
173 ebml_read_float (stream_t
*s
, uint64_t *length
)
179 len
= ebml_read_length (s
, &l
);
183 value
= av_int2flt(stream_read_dword(s
));
187 value
= av_int2dbl(stream_read_qword(s
));
191 return EBML_FLOAT_INVALID
;
201 * Read the next element as an ASCII string.
204 ebml_read_ascii (stream_t
*s
, uint64_t *length
)
210 len
= ebml_read_length (s
, &l
);
211 if (len
== EBML_UINT_INVALID
)
213 if (len
> SIZE_MAX
- 1)
218 str
= (char *) malloc (len
+1);
219 if (stream_read(s
, str
, len
) != (int) len
)
230 * Read the next element as a UTF-8 string.
233 ebml_read_utf8 (stream_t
*s
, uint64_t *length
)
235 return ebml_read_ascii (s
, length
);
239 * Skip the next element.
242 ebml_read_skip (stream_t
*s
, uint64_t *length
)
247 len
= ebml_read_length (s
, &l
);
248 if (len
== EBML_UINT_INVALID
)
259 * Read the next element, but only the header. The contents
260 * are supposed to be sub-elements which can be read separately.
263 ebml_read_master (stream_t
*s
, uint64_t *length
)
268 id
= ebml_read_id (s
, NULL
);
269 if (id
== EBML_ID_INVALID
)
272 len
= ebml_read_length (s
, NULL
);
273 if (len
== EBML_UINT_INVALID
)
274 return EBML_ID_INVALID
;
283 * Read an EBML header.
286 ebml_read_header (stream_t
*s
, int *version
)
288 uint64_t length
, l
, num
;
292 if (ebml_read_master (s
, &length
) != EBML_ID_HEADER
)
300 id
= ebml_read_id (s
, NULL
);
301 if (id
== EBML_ID_INVALID
)
307 /* is our read version uptodate? */
308 case EBML_ID_EBMLREADVERSION
:
309 num
= ebml_read_uint (s
, &l
);
310 if (num
!= EBML_VERSION
)
314 /* we only handle 8 byte lengths at max */
315 case EBML_ID_EBMLMAXSIZELENGTH
:
316 num
= ebml_read_uint (s
, &l
);
317 if (num
!= sizeof (uint64_t))
321 /* we handle 4 byte IDs at max */
322 case EBML_ID_EBMLMAXIDLENGTH
:
323 num
= ebml_read_uint (s
, &l
);
324 if (num
!= sizeof (uint32_t))
328 case EBML_ID_DOCTYPE
:
329 str
= ebml_read_ascii (s
, &l
);
334 case EBML_ID_DOCTYPEREADVERSION
:
335 num
= ebml_read_uint (s
, &l
);
336 if (num
== EBML_UINT_INVALID
)
342 /* we ignore these two, they don't tell us anything we care about */
344 case EBML_ID_EBMLVERSION
:
345 case EBML_ID_DOCTYPEVERSION
:
347 if (ebml_read_skip (s
, &l
))