2 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * ebml-read.c: read EBML data from file/stream
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
28 #include "ebml-read.h"
33 /* NAN is supposed to be in math.h, Microsoft defines it in xmath.h */
38 /* If everything goes wrong try 0.0/0.0 which should be NAN */
40 #define NAN (0.0 / 0.0)
43 GST_DEBUG_CATEGORY (ebmlread_debug);
44 #define GST_CAT_DEFAULT ebmlread_debug
46 /* Peeks following element id and element length in datastream provided
47 * by @peek with @ctx as user data.
48 * Returns GST_FLOW_UNEXPECTED if not enough data to read id and length.
49 * Otherwise, @needed provides the prefix length (id + length), and
50 * @length provides element length.
52 * @object and @offset are provided for informative messaging/debug purposes.
55 gst_ebml_peek_id_length (guint32 * _id, guint64 * _length, guint * _needed,
56 GstPeekData peek, gpointer * ctx, GstElement * el, guint64 offset)
60 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
64 g_return_val_if_fail (_id != NULL, GST_FLOW_ERROR);
65 g_return_val_if_fail (_length != NULL, GST_FLOW_ERROR);
66 g_return_val_if_fail (_needed != NULL, GST_FLOW_ERROR);
69 *_id = (guint32) GST_EBML_SIZE_UNKNOWN;
70 *_length = GST_EBML_SIZE_UNKNOWN;
74 buf = peek (ctx, needed);
78 b = GST_READ_UINT8 (buf);
80 while (read <= 4 && !(total & len_mask)) {
84 if (G_UNLIKELY (read > 4))
87 /* need id and at least something for subsequent length */
89 buf = peek (ctx, needed);
94 b = GST_READ_UINT8 (buf + n);
95 total = (total << 8) | b;
98 *_id = (guint32) total;
100 /* read element length */
101 b = GST_READ_UINT8 (buf + n);
105 while (read <= 8 && !(total & len_mask)) {
109 if (G_UNLIKELY (read > 8))
111 if ((total &= (len_mask - 1)) == len_mask - 1)
115 buf = peek (ctx, needed);
117 goto not_enough_data;
119 buf += (needed - read);
122 guint8 b = GST_READ_UINT8 (buf + n);
124 if (G_UNLIKELY (b == 0xff))
126 total = (total << 8) | b;
130 if (G_UNLIKELY (read == num_ffs))
131 *_length = G_MAXUINT64;
143 return GST_FLOW_UNEXPECTED;
147 GST_ERROR_OBJECT (el,
148 "Invalid EBML ID size tag (0x%x) at position %" G_GUINT64_FORMAT " (0x%"
149 G_GINT64_MODIFIER "x)", (guint) b, offset, offset);
150 return GST_FLOW_ERROR;
154 GST_ERROR_OBJECT (el,
155 "Invalid EBML length size tag (0x%x) at position %" G_GUINT64_FORMAT
156 " (0x%" G_GINT64_MODIFIER "x)", (guint) b, offset, offset);
157 return GST_FLOW_ERROR;
161 /* setup for parsing @buf at position @offset on behalf of @el.
162 * Takes ownership of @buf. */
164 gst_ebml_read_init (GstEbmlRead * ebml, GstElement * el, GstBuffer * buf,
169 g_return_if_fail (el);
170 g_return_if_fail (buf);
173 ebml->offset = offset;
175 ebml->readers = g_array_sized_new (FALSE, FALSE, sizeof (GstEbmlMaster), 10);
176 m.offset = ebml->offset;
177 gst_byte_reader_init (&m.br, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
178 g_array_append_val (ebml->readers, m);
182 gst_ebml_read_clear (GstEbmlRead * ebml)
185 g_array_free (ebml->readers, TRUE);
186 ebml->readers = NULL;
188 gst_buffer_unref (ebml->buf);
193 static const guint8 *
194 gst_ebml_read_peek (GstByteReader * br, guint peek)
196 const guint8 *data = NULL;
198 if (G_LIKELY (gst_byte_reader_peek_data (br, peek, &data)))
205 gst_ebml_peek_id_full (GstEbmlRead * ebml, guint32 * id, guint64 * length,
210 ret = gst_ebml_peek_id_length (id, length, prefix,
211 (GstPeekData) gst_ebml_read_peek, (gpointer) gst_ebml_read_br (ebml),
212 ebml->el, gst_ebml_read_get_pos (ebml));
213 if (ret != GST_FLOW_OK)
216 GST_LOG_OBJECT (ebml->el, "id 0x%x at offset 0x%" G_GINT64_MODIFIER "x"
217 " of length %" G_GUINT64_FORMAT ", prefix %d", *id,
218 gst_ebml_read_get_pos (ebml), *length, *prefix);
220 #ifndef GST_DISABLE_GST_DEBUG
222 const guint8 *data = NULL;
223 GstByteReader *br = gst_ebml_read_br (ebml);
224 guint size = gst_byte_reader_get_remaining (br);
226 gst_byte_reader_peek_data (br, size, &data);
228 GST_LOG_OBJECT (ebml->el, "current br %p; remaining %d", br, size);
230 GST_MEMDUMP_OBJECT (ebml->el, "element", data, MIN (size, *length));
238 gst_ebml_peek_id (GstEbmlRead * ebml, guint32 * id)
243 return gst_ebml_peek_id_full (ebml, id, &length, &needed);
247 * Read the next element, the contents are supposed to be sub-elements which
248 * can be read separately. A new bytereader is setup for doing so.
251 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
255 const guint8 *data = NULL;
259 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
260 if (ret != GST_FLOW_OK)
263 /* we just at least peeked the id */
264 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
265 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
267 m.offset = gst_ebml_read_get_pos (ebml);
268 if (!gst_byte_reader_get_data (gst_ebml_read_br (ebml), length, &data))
269 return GST_FLOW_PARSE;
271 GST_LOG_OBJECT (ebml->el, "pushing level %d at offset %" G_GUINT64_FORMAT,
272 ebml->readers->len, m.offset);
273 gst_byte_reader_init (&m.br, data, length);
274 g_array_append_val (ebml->readers, m);
279 /* explicitly pop a bytereader from stack. Usually invoked automagically. */
281 gst_ebml_read_pop_master (GstEbmlRead * ebml)
283 g_return_val_if_fail (ebml->readers, GST_FLOW_ERROR);
285 /* never remove initial bytereader */
286 if (ebml->readers->len > 1) {
287 GST_LOG_OBJECT (ebml->el, "popping level %d", ebml->readers->len - 1);
288 g_array_remove_index (ebml->readers, ebml->readers->len - 1);
295 * Skip the next element.
299 gst_ebml_read_skip (GstEbmlRead * ebml)
306 ret = gst_ebml_peek_id_full (ebml, &id, &length, &prefix);
307 if (ret != GST_FLOW_OK)
310 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), length + prefix))
311 return GST_FLOW_PARSE;
317 * Read the next element as a GstBuffer (binary).
321 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
327 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
328 if (ret != GST_FLOW_OK)
331 /* we just at least peeked the id */
332 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
333 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
335 if (G_LIKELY (length > 0)) {
338 offset = gst_ebml_read_get_pos (ebml) - ebml->offset;
339 if (G_LIKELY (gst_byte_reader_skip (gst_ebml_read_br (ebml), length))) {
340 *buf = gst_buffer_create_sub (ebml->buf, offset, length);
343 return GST_FLOW_PARSE;
346 *buf = gst_buffer_new ();
353 * Read the next element, return a pointer to it and its size.
357 gst_ebml_read_bytes (GstEbmlRead * ebml, guint32 * id, const guint8 ** data,
366 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
367 if (ret != GST_FLOW_OK)
370 /* we just at least peeked the id */
371 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
372 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
375 if (G_LIKELY (length >= 0)) {
376 if (!gst_byte_reader_get_data (gst_ebml_read_br (ebml), length, data))
377 return GST_FLOW_PARSE;
386 * Read the next element as an unsigned int.
390 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
396 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
397 if (ret != GST_FLOW_OK)
401 GST_ERROR_OBJECT (ebml->el,
402 "Invalid integer element size %d at position %" G_GUINT64_FORMAT " (0x%"
403 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
404 gst_ebml_read_get_pos (ebml) - size);
405 return GST_FLOW_ERROR;
415 *num = (*num << 8) | *data;
424 * Read the next element as a signed int.
428 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
432 gboolean negative = 0;
435 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
436 if (ret != GST_FLOW_OK)
440 GST_ERROR_OBJECT (ebml->el,
441 "Invalid integer element size %d at position %" G_GUINT64_FORMAT " (0x%"
442 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
443 gst_ebml_read_get_pos (ebml) - size);
444 return GST_FLOW_ERROR;
455 *num = *data & ~0x80;
461 *num = (*num << 8) | *data;
474 /* Convert 80 bit extended precision float in big endian format to double.
475 * Code taken from libavutil/intfloat_readwrite.c from ffmpeg,
476 * licensed under LGPL */
485 _ext2dbl (const guint8 * data)
487 struct _ext_float ext;
491 memcpy (&ext.exponent, data, 2);
492 memcpy (&ext.mantissa, data + 2, 8);
494 for (i = 0; i < 8; i++)
495 m = (m << 8) + ext.mantissa[i];
496 e = (((gint) ext.exponent[0] & 0x7f) << 8) | ext.exponent[1];
497 if (e == 0x7fff && m)
499 e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
500 * mantissa bit is written as opposed to the
501 * single and double precision formats */
502 if (ext.exponent[0] & 0x80)
508 * Read the next element as a float.
512 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
518 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
519 if (ret != GST_FLOW_OK)
522 if (size != 0 && size != 4 && size != 8 && size != 10) {
523 GST_ERROR_OBJECT (ebml->el,
524 "Invalid float element size %d at position %" G_GUINT64_FORMAT " (0x%"
525 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
526 gst_ebml_read_get_pos (ebml) - size);
527 return GST_FLOW_ERROR;
533 memcpy (&f, data, 4);
534 f = GFLOAT_FROM_BE (f);
537 } else if (size == 8) {
540 memcpy (&d, data, 8);
541 d = GDOUBLE_FROM_BE (d);
544 } else if (size == 10) {
545 *num = _ext2dbl (data);
547 /* size == 0 means a value of 0.0 */
555 * Read the next element as a C string.
559 gst_ebml_read_string (GstEbmlRead * ebml, guint32 * id, gchar ** str)
565 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
566 if (ret != GST_FLOW_OK)
569 *str = g_malloc (size + 1);
570 memcpy (*str, data, size);
577 * Read the next element as an ASCII string.
581 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str_out)
587 #ifndef GST_DISABLE_GST_DEBUG
588 guint64 oldoff = ebml->offset;
591 ret = gst_ebml_read_string (ebml, id, &str);
592 if (ret != GST_FLOW_OK)
595 for (iter = str; *iter != '\0'; iter++) {
596 if (G_UNLIKELY (*iter & 0x80)) {
597 GST_ERROR_OBJECT (ebml,
598 "Invalid ASCII string at offset %" G_GUINT64_FORMAT, oldoff);
600 return GST_FLOW_ERROR;
609 * Read the next element as a UTF-8 string.
613 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
617 #ifndef GST_DISABLE_GST_DEBUG
618 guint64 oldoff = gst_ebml_read_get_pos (ebml);
621 ret = gst_ebml_read_string (ebml, id, str);
622 if (ret != GST_FLOW_OK)
625 if (str != NULL && *str != NULL && **str != '\0' &&
626 !g_utf8_validate (*str, -1, NULL)) {
627 GST_WARNING_OBJECT (ebml->el,
628 "Invalid UTF-8 string at offset %" G_GUINT64_FORMAT, oldoff);
635 * Read the next element as a date.
636 * Returns the seconds since the unix epoch.
640 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
645 ret = gst_ebml_read_sint (ebml, id, &ebml_date);
646 if (ret != GST_FLOW_OK)
649 *date = (ebml_date / GST_SECOND) + GST_EBML_DATE_OFFSET;
655 * Read the next element as binary data.
659 gst_ebml_read_binary (GstEbmlRead * ebml,
660 guint32 * id, guint8 ** binary, guint64 * length)
666 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
667 if (ret != GST_FLOW_OK)
671 *binary = g_memdup (data, size);