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;
65 g_return_val_if_fail (_id != NULL, GST_FLOW_ERROR);
66 g_return_val_if_fail (_length != NULL, GST_FLOW_ERROR);
67 g_return_val_if_fail (_needed != NULL, GST_FLOW_ERROR);
70 *_id = (guint32) GST_EBML_SIZE_UNKNOWN;
71 *_length = GST_EBML_SIZE_UNKNOWN;
75 ret = peek (ctx, needed, &buf);
76 if (ret != GST_FLOW_OK)
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 ret = peek (ctx, needed, &buf);
90 if (ret != GST_FLOW_OK)
93 b = GST_READ_UINT8 (buf + n);
94 total = (total << 8) | b;
97 *_id = (guint32) total;
99 /* read element length */
100 b = GST_READ_UINT8 (buf + n);
104 while (read <= 8 && !(total & len_mask)) {
108 if (G_UNLIKELY (read > 8))
110 if ((total &= (len_mask - 1)) == len_mask - 1)
114 ret = peek (ctx, needed, &buf);
115 if (ret != GST_FLOW_OK)
117 buf += (needed - read);
120 guint8 b = GST_READ_UINT8 (buf + n);
122 if (G_UNLIKELY (b == 0xff))
124 total = (total << 8) | b;
128 if (G_UNLIKELY (read == num_ffs))
129 *_length = G_MAXUINT64;
140 GST_WARNING_OBJECT (el, "peek failed, ret = %d", ret);
146 GST_ERROR_OBJECT (el,
147 "Invalid EBML ID size tag (0x%x) at position %" G_GUINT64_FORMAT " (0x%"
148 G_GINT64_MODIFIER "x)", (guint) b, offset, offset);
149 return GST_FLOW_ERROR;
153 GST_ERROR_OBJECT (el,
154 "Invalid EBML length size tag (0x%x) at position %" G_GUINT64_FORMAT
155 " (0x%" G_GINT64_MODIFIER "x)", (guint) b, offset, offset);
156 return GST_FLOW_ERROR;
160 /* setup for parsing @buf at position @offset on behalf of @el.
161 * Takes ownership of @buf. */
163 gst_ebml_read_init (GstEbmlRead * ebml, GstElement * el, GstBuffer * buf,
168 g_return_if_fail (el);
169 g_return_if_fail (buf);
172 ebml->offset = offset;
174 ebml->readers = g_array_sized_new (FALSE, FALSE, sizeof (GstEbmlMaster), 10);
175 m.offset = ebml->offset;
176 gst_byte_reader_init (&m.br, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
177 g_array_append_val (ebml->readers, m);
181 gst_ebml_read_clear (GstEbmlRead * ebml)
184 g_array_free (ebml->readers, TRUE);
185 ebml->readers = NULL;
187 gst_buffer_unref (ebml->buf);
193 gst_ebml_read_peek (GstByteReader * br, guint peek, const guint8 ** data)
195 if (G_LIKELY (gst_byte_reader_peek_data (br, peek, data)))
198 return GST_FLOW_UNEXPECTED;
202 gst_ebml_peek_id_full (GstEbmlRead * ebml, guint32 * id, guint64 * length,
207 ret = gst_ebml_peek_id_length (id, length, prefix,
208 (GstPeekData) gst_ebml_read_peek, (gpointer) gst_ebml_read_br (ebml),
209 ebml->el, gst_ebml_read_get_pos (ebml));
210 if (ret != GST_FLOW_OK)
213 GST_LOG_OBJECT (ebml->el, "id 0x%x at offset 0x%" G_GINT64_MODIFIER "x"
214 " of length %" G_GUINT64_FORMAT ", prefix %d", *id,
215 gst_ebml_read_get_pos (ebml), *length, *prefix);
217 #ifndef GST_DISABLE_GST_DEBUG
219 const guint8 *data = NULL;
220 GstByteReader *br = gst_ebml_read_br (ebml);
221 guint size = gst_byte_reader_get_remaining (br);
223 gst_byte_reader_peek_data (br, size, &data);
225 GST_LOG_OBJECT (ebml->el, "current br %p; remaining %d", br, size);
227 GST_MEMDUMP_OBJECT (ebml->el, "element", data, MIN (size, *length));
235 gst_ebml_peek_id (GstEbmlRead * ebml, guint32 * id)
240 return gst_ebml_peek_id_full (ebml, id, &length, &needed);
244 * Read the next element, the contents are supposed to be sub-elements which
245 * can be read separately. A new bytereader is setup for doing so.
248 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
252 const guint8 *data = NULL;
256 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
257 if (ret != GST_FLOW_OK)
260 /* we just at least peeked the id */
261 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
262 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
264 m.offset = gst_ebml_read_get_pos (ebml);
265 if (!gst_byte_reader_get_data (gst_ebml_read_br (ebml), length, &data))
266 return GST_FLOW_PARSE;
268 GST_LOG_OBJECT (ebml->el, "pushing level %d at offset %" G_GUINT64_FORMAT,
269 ebml->readers->len, m.offset);
270 gst_byte_reader_init (&m.br, data, length);
271 g_array_append_val (ebml->readers, m);
276 /* explicitly pop a bytereader from stack. Usually invoked automagically. */
278 gst_ebml_read_pop_master (GstEbmlRead * ebml)
280 g_return_val_if_fail (ebml->readers, GST_FLOW_ERROR);
282 /* never remove initial bytereader */
283 if (ebml->readers->len > 1) {
284 GST_LOG_OBJECT (ebml->el, "popping level %d", ebml->readers->len - 1);
285 g_array_remove_index (ebml->readers, ebml->readers->len - 1);
292 * Skip the next element.
296 gst_ebml_read_skip (GstEbmlRead * ebml)
303 ret = gst_ebml_peek_id_full (ebml, &id, &length, &prefix);
304 if (ret != GST_FLOW_OK)
307 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), length + prefix))
308 return GST_FLOW_PARSE;
314 * Read the next element as a GstBuffer (binary).
318 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
324 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
325 if (ret != GST_FLOW_OK)
328 /* we just at least peeked the id */
329 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
330 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
332 if (G_LIKELY (length > 0)) {
335 offset = gst_ebml_read_get_pos (ebml) - ebml->offset;
336 if (G_LIKELY (gst_byte_reader_skip (gst_ebml_read_br (ebml), length))) {
337 *buf = gst_buffer_create_sub (ebml->buf, offset, length);
340 return GST_FLOW_PARSE;
343 *buf = gst_buffer_new ();
350 * Read the next element, return a pointer to it and its size.
354 gst_ebml_read_bytes (GstEbmlRead * ebml, guint32 * id, const guint8 ** data,
363 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
364 if (ret != GST_FLOW_OK)
367 /* we just at least peeked the id */
368 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
369 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
372 if (G_LIKELY (length >= 0)) {
373 if (!gst_byte_reader_get_data (gst_ebml_read_br (ebml), length, data))
374 return GST_FLOW_PARSE;
383 * Read the next element as an unsigned int.
387 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
393 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
394 if (ret != GST_FLOW_OK)
398 GST_ERROR_OBJECT (ebml->el,
399 "Invalid integer element size %d at position %" G_GUINT64_FORMAT " (0x%"
400 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
401 gst_ebml_read_get_pos (ebml) - size);
402 return GST_FLOW_ERROR;
412 *num = (*num << 8) | *data;
421 * Read the next element as a signed int.
425 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
429 gboolean negative = 0;
432 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
433 if (ret != GST_FLOW_OK)
437 GST_ERROR_OBJECT (ebml->el,
438 "Invalid integer element size %d at position %" G_GUINT64_FORMAT " (0x%"
439 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
440 gst_ebml_read_get_pos (ebml) - size);
441 return GST_FLOW_ERROR;
452 *num = *data & ~0x80;
458 *num = (*num << 8) | *data;
471 /* Convert 80 bit extended precision float in big endian format to double.
472 * Code taken from libavutil/intfloat_readwrite.c from ffmpeg,
473 * licensed under LGPL */
482 _ext2dbl (const guint8 * data)
484 struct _ext_float ext;
488 memcpy (&ext.exponent, data, 2);
489 memcpy (&ext.mantissa, data + 2, 8);
491 for (i = 0; i < 8; i++)
492 m = (m << 8) + ext.mantissa[i];
493 e = (((gint) ext.exponent[0] & 0x7f) << 8) | ext.exponent[1];
494 if (e == 0x7fff && m)
496 e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
497 * mantissa bit is written as opposed to the
498 * single and double precision formats */
499 if (ext.exponent[0] & 0x80)
505 * Read the next element as a float.
509 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
515 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
516 if (ret != GST_FLOW_OK)
519 if (size != 0 && size != 4 && size != 8 && size != 10) {
520 GST_ERROR_OBJECT (ebml->el,
521 "Invalid float element size %d at position %" G_GUINT64_FORMAT " (0x%"
522 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
523 gst_ebml_read_get_pos (ebml) - size);
524 return GST_FLOW_ERROR;
530 memcpy (&f, data, 4);
531 f = GFLOAT_FROM_BE (f);
534 } else if (size == 8) {
537 memcpy (&d, data, 8);
538 d = GDOUBLE_FROM_BE (d);
541 } else if (size == 10) {
542 *num = _ext2dbl (data);
544 /* size == 0 means a value of 0.0 */
552 * Read the next element as a C string.
556 gst_ebml_read_string (GstEbmlRead * ebml, guint32 * id, gchar ** str)
562 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
563 if (ret != GST_FLOW_OK)
566 *str = g_malloc (size + 1);
567 memcpy (*str, data, size);
574 * Read the next element as an ASCII string.
578 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str_out)
584 #ifndef GST_DISABLE_GST_DEBUG
585 guint64 oldoff = ebml->offset;
588 ret = gst_ebml_read_string (ebml, id, &str);
589 if (ret != GST_FLOW_OK)
592 for (iter = str; *iter != '\0'; iter++) {
593 if (G_UNLIKELY (*iter & 0x80)) {
594 GST_ERROR_OBJECT (ebml,
595 "Invalid ASCII string at offset %" G_GUINT64_FORMAT, oldoff);
597 return GST_FLOW_ERROR;
606 * Read the next element as a UTF-8 string.
610 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
614 #ifndef GST_DISABLE_GST_DEBUG
615 guint64 oldoff = gst_ebml_read_get_pos (ebml);
618 ret = gst_ebml_read_string (ebml, id, str);
619 if (ret != GST_FLOW_OK)
622 if (str != NULL && *str != NULL && **str != '\0' &&
623 !g_utf8_validate (*str, -1, NULL)) {
624 GST_WARNING_OBJECT (ebml->el,
625 "Invalid UTF-8 string at offset %" G_GUINT64_FORMAT, oldoff);
632 * Read the next element as a date.
633 * Returns the seconds since the unix epoch.
637 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
642 ret = gst_ebml_read_sint (ebml, id, &ebml_date);
643 if (ret != GST_FLOW_OK)
646 *date = (ebml_date / GST_SECOND) + GST_EBML_DATE_OFFSET;
652 * Read the next element as binary data.
656 gst_ebml_read_binary (GstEbmlRead * ebml,
657 guint32 * id, guint8 ** binary, guint64 * length)
663 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
664 if (ret != GST_FLOW_OK)
668 *binary = g_memdup (data, size);