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_EOS 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 gst_buffer_map (buf, &ebml->map, GST_MAP_READ);
175 ebml->readers = g_array_sized_new (FALSE, FALSE, sizeof (GstEbmlMaster), 10);
176 m.offset = ebml->offset;
177 gst_byte_reader_init (&m.br, ebml->map.data, ebml->map.size);
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_unmap (ebml->buf, &ebml->map);
189 gst_buffer_unref (ebml->buf);
196 gst_ebml_read_peek (GstByteReader * br, guint peek, const guint8 ** data)
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 if (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));
239 gst_ebml_peek_id (GstEbmlRead * ebml, guint32 * id)
244 return gst_ebml_peek_id_full (ebml, id, &length, &needed);
248 * Read the next element, the contents are supposed to be sub-elements which
249 * can be read separately. A new bytereader is setup for doing so.
252 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
256 const guint8 *data = NULL;
260 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
261 if (ret != GST_FLOW_OK)
264 /* we just at least peeked the id */
265 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
266 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
268 m.offset = gst_ebml_read_get_pos (ebml);
269 if (!gst_byte_reader_get_data (gst_ebml_read_br (ebml), length, &data))
270 return GST_FLOW_PARSE;
272 GST_LOG_OBJECT (ebml->el, "pushing level %d at offset %" G_GUINT64_FORMAT,
273 ebml->readers->len, m.offset);
274 gst_byte_reader_init (&m.br, data, length);
275 g_array_append_val (ebml->readers, m);
280 /* explicitly pop a bytereader from stack. Usually invoked automagically. */
282 gst_ebml_read_pop_master (GstEbmlRead * ebml)
284 g_return_val_if_fail (ebml->readers, GST_FLOW_ERROR);
286 /* never remove initial bytereader */
287 if (ebml->readers->len > 1) {
288 GST_LOG_OBJECT (ebml->el, "popping level %d", ebml->readers->len - 1);
289 g_array_remove_index (ebml->readers, ebml->readers->len - 1);
296 * Skip the next element.
300 gst_ebml_read_skip (GstEbmlRead * ebml)
307 ret = gst_ebml_peek_id_full (ebml, &id, &length, &prefix);
308 if (ret != GST_FLOW_OK)
311 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), length + prefix))
312 return GST_FLOW_PARSE;
318 * Read the next element as a GstBuffer (binary).
322 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
328 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
329 if (ret != GST_FLOW_OK)
332 /* we just at least peeked the id */
333 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
334 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
336 if (G_LIKELY (length > 0)) {
339 offset = gst_ebml_read_get_pos (ebml) - ebml->offset;
340 if (G_LIKELY (gst_byte_reader_skip (gst_ebml_read_br (ebml), length))) {
341 *buf = gst_buffer_copy_region (ebml->buf, GST_BUFFER_COPY_ALL,
345 return GST_FLOW_PARSE;
348 *buf = gst_buffer_new ();
355 * Read the next element, return a pointer to it and its size.
359 gst_ebml_read_bytes (GstEbmlRead * ebml, guint32 * id, const guint8 ** data,
368 ret = gst_ebml_peek_id_full (ebml, id, &length, &prefix);
369 if (ret != GST_FLOW_OK)
372 /* we just at least peeked the id */
373 if (!gst_byte_reader_skip (gst_ebml_read_br (ebml), prefix))
374 return GST_FLOW_ERROR; /* FIXME: do proper error handling */
377 if (G_LIKELY (length >= 0)) {
378 if (!gst_byte_reader_get_data (gst_ebml_read_br (ebml), length, data))
379 return GST_FLOW_PARSE;
388 * Read the next element as an unsigned int.
392 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
398 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
399 if (ret != GST_FLOW_OK)
403 GST_ERROR_OBJECT (ebml->el,
404 "Invalid integer element size %d at position %" G_GUINT64_FORMAT " (0x%"
405 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
406 gst_ebml_read_get_pos (ebml) - size);
407 return GST_FLOW_ERROR;
417 *num = (*num << 8) | *data;
426 * Read the next element as a signed int.
430 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
434 gboolean negative = 0;
437 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
438 if (ret != GST_FLOW_OK)
442 GST_ERROR_OBJECT (ebml->el,
443 "Invalid integer element size %d at position %" G_GUINT64_FORMAT " (0x%"
444 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
445 gst_ebml_read_get_pos (ebml) - size);
446 return GST_FLOW_ERROR;
457 *num = *data & ~0x80;
463 *num = (*num << 8) | *data;
476 /* Convert 80 bit extended precision float in big endian format to double.
477 * Code taken from libavutil/intfloat_readwrite.c from ffmpeg,
478 * licensed under LGPL */
487 _ext2dbl (const guint8 * data)
489 struct _ext_float ext;
493 memcpy (&ext.exponent, data, 2);
494 memcpy (&ext.mantissa, data + 2, 8);
496 for (i = 0; i < 8; i++)
497 m = (m << 8) + ext.mantissa[i];
498 e = (((gint) ext.exponent[0] & 0x7f) << 8) | ext.exponent[1];
499 if (e == 0x7fff && m)
501 e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
502 * mantissa bit is written as opposed to the
503 * single and double precision formats */
504 if (ext.exponent[0] & 0x80)
510 * Read the next element as a float.
514 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
520 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
521 if (ret != GST_FLOW_OK)
524 if (size != 0 && size != 4 && size != 8 && size != 10) {
525 GST_ERROR_OBJECT (ebml->el,
526 "Invalid float element size %d at position %" G_GUINT64_FORMAT " (0x%"
527 G_GINT64_MODIFIER "x)", size, gst_ebml_read_get_pos (ebml) - size,
528 gst_ebml_read_get_pos (ebml) - size);
529 return GST_FLOW_ERROR;
535 memcpy (&f, data, 4);
536 f = GFLOAT_FROM_BE (f);
539 } else if (size == 8) {
542 memcpy (&d, data, 8);
543 d = GDOUBLE_FROM_BE (d);
546 } else if (size == 10) {
547 *num = _ext2dbl (data);
549 /* size == 0 means a value of 0.0 */
557 * Read the next element as a C string.
561 gst_ebml_read_string (GstEbmlRead * ebml, guint32 * id, gchar ** str)
567 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
568 if (ret != GST_FLOW_OK)
571 *str = g_malloc (size + 1);
572 memcpy (*str, data, size);
579 * Read the next element as an ASCII string.
583 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str_out)
589 #ifndef GST_DISABLE_GST_DEBUG
590 guint64 oldoff = ebml->offset;
593 ret = gst_ebml_read_string (ebml, id, &str);
594 if (ret != GST_FLOW_OK)
597 for (iter = str; *iter != '\0'; iter++) {
598 if (G_UNLIKELY (*iter & 0x80)) {
599 GST_ERROR_OBJECT (ebml,
600 "Invalid ASCII string at offset %" G_GUINT64_FORMAT, oldoff);
602 return GST_FLOW_ERROR;
611 * Read the next element as a UTF-8 string.
615 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
619 #ifndef GST_DISABLE_GST_DEBUG
620 guint64 oldoff = gst_ebml_read_get_pos (ebml);
623 ret = gst_ebml_read_string (ebml, id, str);
624 if (ret != GST_FLOW_OK)
627 if (str != NULL && *str != NULL && **str != '\0' &&
628 !g_utf8_validate (*str, -1, NULL)) {
629 GST_WARNING_OBJECT (ebml->el,
630 "Invalid UTF-8 string at offset %" G_GUINT64_FORMAT, oldoff);
637 * Read the next element as a date.
638 * Returns the seconds since the unix epoch.
642 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
647 ret = gst_ebml_read_sint (ebml, id, &ebml_date);
648 if (ret != GST_FLOW_OK)
651 *date = (ebml_date / GST_SECOND) + GST_EBML_DATE_OFFSET;
657 * Read the next element as binary data.
661 gst_ebml_read_binary (GstEbmlRead * ebml,
662 guint32 * id, guint8 ** binary, guint64 * length)
668 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
669 if (ret != GST_FLOW_OK)
673 *binary = g_memdup (data, size);