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 GST_DEBUG_CATEGORY_STATIC (ebmlread_debug);
34 #define GST_CAT_DEFAULT ebmlread_debug
36 static void gst_ebml_read_class_init (GstEbmlReadClass * klass);
38 static void gst_ebml_read_init (GstEbmlRead * ebml);
40 static GstStateChangeReturn gst_ebml_read_change_state (GstElement * element,
41 GstStateChange transition);
43 /* convenience functions */
44 static GstFlowReturn gst_ebml_read_peek_bytes (GstEbmlRead * ebml, guint size,
45 GstBuffer ** p_buf, guint8 ** bytes);
46 static GstFlowReturn gst_ebml_read_pull_bytes (GstEbmlRead * ebml, guint size,
47 GstBuffer ** p_buf, guint8 ** bytes);
50 static GstElementClass *parent_class; /* NULL */
53 gst_ebml_read_get_type (void)
55 static GType gst_ebml_read_type; /* 0 */
57 if (!gst_ebml_read_type) {
58 static const GTypeInfo gst_ebml_read_info = {
59 sizeof (GstEbmlReadClass),
62 (GClassInitFunc) gst_ebml_read_class_init,
67 (GInstanceInitFunc) gst_ebml_read_init,
71 g_type_register_static (GST_TYPE_ELEMENT, "GstEbmlRead",
72 &gst_ebml_read_info, 0);
75 return gst_ebml_read_type;
79 gst_ebml_level_free (GstEbmlLevel * level)
81 g_slice_free (GstEbmlLevel, level);
85 gst_ebml_finalize (GObject * obj)
87 GstEbmlRead *ebml = GST_EBML_READ (obj);
89 g_list_foreach (ebml->level, (GFunc) gst_ebml_level_free, NULL);
90 g_list_free (ebml->level);
92 if (ebml->cached_buffer) {
93 gst_buffer_unref (ebml->cached_buffer);
94 ebml->cached_buffer = NULL;
97 G_OBJECT_CLASS (parent_class)->finalize (obj);
101 gst_ebml_read_class_init (GstEbmlReadClass * klass)
103 GstElementClass *gstelement_class = (GstElementClass *) klass;
105 GObjectClass *gobject_class = (GObjectClass *) klass;
107 parent_class = g_type_class_peek_parent (klass);
109 GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
110 0, "EBML stream helper class");
112 gobject_class->finalize = gst_ebml_finalize;
114 gstelement_class->change_state =
115 GST_DEBUG_FUNCPTR (gst_ebml_read_change_state);
119 gst_ebml_read_init (GstEbmlRead * ebml)
121 ebml->sinkpad = NULL;
125 static GstStateChangeReturn
126 gst_ebml_read_change_state (GstElement * element, GstStateChange transition)
128 GstStateChangeReturn ret;
130 GstEbmlRead *ebml = GST_EBML_READ (element);
132 switch (transition) {
133 case GST_STATE_CHANGE_READY_TO_PAUSED:
134 if (!ebml->sinkpad) {
135 g_return_val_if_reached (GST_STATE_CHANGE_FAILURE);
142 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
144 switch (transition) {
145 case GST_STATE_CHANGE_PAUSED_TO_READY:
147 g_list_foreach (ebml->level, (GFunc) gst_ebml_level_free, NULL);
148 g_list_free (ebml->level);
150 if (ebml->cached_buffer) {
151 gst_buffer_unref (ebml->cached_buffer);
152 ebml->cached_buffer = NULL;
165 * Return: the amount of levels in the hierarchy that the
166 * current element lies higher than the previous one.
167 * The opposite isn't done - that's auto-done using master
172 gst_ebml_read_element_level_up (GstEbmlRead * ebml)
176 guint64 pos = ebml->offset;
178 while (ebml->level != NULL) {
179 GstEbmlLevel *level = ebml->level->data;
181 if (pos >= level->start + level->length) {
182 ebml->level = g_list_delete_link (ebml->level, ebml->level);
183 gst_ebml_level_free (level);
194 * Calls pull_range for (offset,size) without advancing our offset
197 gst_ebml_read_peek_bytes (GstEbmlRead * ebml, guint size, GstBuffer ** p_buf,
202 /* Caching here actually makes much less difference than one would expect.
203 * We do it mainly to avoid pulling buffers of 1 byte all the time */
204 if (ebml->cached_buffer) {
205 guint64 cache_offset = GST_BUFFER_OFFSET (ebml->cached_buffer);
207 guint cache_size = GST_BUFFER_SIZE (ebml->cached_buffer);
209 if (cache_offset <= ebml->offset &&
210 (ebml->offset + size) < (cache_offset + cache_size)) {
212 *p_buf = gst_buffer_create_sub (ebml->cached_buffer,
213 ebml->offset - cache_offset, size);
216 GST_BUFFER_DATA (ebml->cached_buffer) + ebml->offset - cache_offset;
219 /* not enough data in the cache, free cache and get a new one */
220 gst_buffer_unref (ebml->cached_buffer);
221 ebml->cached_buffer = NULL;
224 /* refill the cache */
225 ret = gst_pad_pull_range (ebml->sinkpad, ebml->offset, MAX (size, 64 * 1024),
226 &ebml->cached_buffer);
227 if (ret != GST_FLOW_OK) {
228 ebml->cached_buffer = NULL;
232 if (GST_BUFFER_SIZE (ebml->cached_buffer) >= size) {
234 *p_buf = gst_buffer_create_sub (ebml->cached_buffer, 0, size);
236 *bytes = GST_BUFFER_DATA (ebml->cached_buffer);
240 /* Not possible to get enough data, try a last time with
241 * requesting exactly the size we need */
242 gst_buffer_unref (ebml->cached_buffer);
243 ebml->cached_buffer = NULL;
246 gst_pad_pull_range (ebml->sinkpad, ebml->offset, size,
247 &ebml->cached_buffer);
248 if (ret != GST_FLOW_OK) {
249 GST_DEBUG ("pull_range returned %d", ret);
257 if (GST_BUFFER_SIZE (ebml->cached_buffer) < size) {
258 GST_WARNING_OBJECT (ebml, "Dropping short buffer at offset %"
259 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", ebml->offset,
260 size, GST_BUFFER_SIZE (ebml->cached_buffer));
262 gst_buffer_unref (ebml->cached_buffer);
263 ebml->cached_buffer = NULL;
268 return GST_FLOW_ERROR;
272 *p_buf = gst_buffer_create_sub (ebml->cached_buffer, 0, size);
274 *bytes = GST_BUFFER_DATA (*p_buf);
280 * Calls pull_range for (offset,size) and advances our offset by size
283 gst_ebml_read_pull_bytes (GstEbmlRead * ebml, guint size, GstBuffer ** p_buf,
288 ret = gst_ebml_read_peek_bytes (ebml, size, p_buf, bytes);
289 if (ret != GST_FLOW_OK)
292 ebml->offset += size;
297 * Read: the element content data ID.
298 * Return: FALSE on error.
302 gst_ebml_read_element_id (GstEbmlRead * ebml, guint32 * id, guint * level_up)
306 gint len_mask = 0x80, read = 1, n = 1;
314 ret = gst_ebml_read_peek_bytes (ebml, 1, NULL, &buf);
315 if (ret != GST_FLOW_OK)
318 b = GST_READ_UINT8 (buf);
322 while (read <= 4 && !(total & len_mask)) {
327 guint64 pos = ebml->offset;
329 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
330 ("Invalid EBML ID size tag (0x%x) at position %" G_GUINT64_FORMAT
331 " (0x%" G_GINT64_MODIFIER "x)", (guint) b, pos, pos));
332 return GST_FLOW_ERROR;
335 ret = gst_ebml_read_peek_bytes (ebml, read, NULL, &buf);
336 if (ret != GST_FLOW_OK)
340 b = GST_READ_UINT8 (buf + n);
341 total = (total << 8) | b;
349 *level_up = gst_ebml_read_element_level_up (ebml);
351 ebml->offset += read;
356 * Read: element content length.
357 * Return: the number of bytes read or -1 on error.
361 gst_ebml_read_element_length (GstEbmlRead * ebml, guint64 * length,
368 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
374 ret = gst_ebml_read_peek_bytes (ebml, 1, NULL, &buf);
375 if (ret != GST_FLOW_OK)
378 b = GST_READ_UINT8 (buf);
382 while (read <= 8 && !(total & len_mask)) {
387 guint64 pos = ebml->offset;
389 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
390 ("Invalid EBML length size tag (0x%x) at position %" G_GUINT64_FORMAT
391 " (0x%" G_GINT64_MODIFIER "x)", (guint) b, pos, pos));
392 return GST_FLOW_ERROR;
395 if ((total &= (len_mask - 1)) == len_mask - 1)
398 ret = gst_ebml_read_peek_bytes (ebml, read, NULL, &buf);
399 if (ret != GST_FLOW_OK)
403 guint8 b = GST_READ_UINT8 (buf + n);
407 total = (total << 8) | b;
412 *length = G_MAXUINT64;
419 ebml->offset += read;
425 * Return: the ID of the next element.
426 * Level_up contains the amount of levels that this
427 * next element lies higher than the previous one.
431 gst_ebml_peek_id (GstEbmlRead * ebml, guint * level_up, guint32 * id)
439 off = ebml->offset; /* save offset */
441 if ((ret = gst_ebml_read_element_id (ebml, id, level_up)) != GST_FLOW_OK)
444 ebml->offset = off; /* restore offset */
449 * Return the length of the stream in bytes
453 gst_ebml_read_get_length (GstEbmlRead * ebml)
455 GstFormat fmt = GST_FORMAT_BYTES;
459 /* FIXME: what to do if we don't get the upstream length */
460 if (!gst_pad_query_peer_duration (ebml->sinkpad, &fmt, &end) ||
461 fmt != GST_FORMAT_BYTES || end < 0)
462 g_return_val_if_reached (0);
468 * Seek to a given offset.
472 gst_ebml_read_seek (GstEbmlRead * ebml, guint64 offset)
474 if (offset >= gst_ebml_read_get_length (ebml))
475 return GST_FLOW_UNEXPECTED;
477 ebml->offset = offset;
483 * Skip the next element.
487 gst_ebml_read_skip (GstEbmlRead * ebml)
495 ret = gst_ebml_read_element_id (ebml, &id, NULL);
496 if (ret != GST_FLOW_OK)
499 ret = gst_ebml_read_element_length (ebml, &length, NULL);
500 if (ret != GST_FLOW_OK)
503 ebml->offset += length;
508 * Read the next element as a GstBuffer (binary).
512 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
518 ret = gst_ebml_read_element_id (ebml, id, NULL);
519 if (ret != GST_FLOW_OK)
522 ret = gst_ebml_read_element_length (ebml, &length, NULL);
523 if (ret != GST_FLOW_OK)
527 *buf = gst_buffer_new ();
532 ret = gst_ebml_read_pull_bytes (ebml, (guint) length, buf, NULL);
538 * Read the next element, return a pointer to it and its size.
542 gst_ebml_read_bytes (GstEbmlRead * ebml, guint32 * id, guint8 ** data,
551 ret = gst_ebml_read_element_id (ebml, id, NULL);
552 if (ret != GST_FLOW_OK)
555 ret = gst_ebml_read_element_length (ebml, &length, NULL);
556 if (ret != GST_FLOW_OK)
565 ret = gst_ebml_read_pull_bytes (ebml, (guint) length, NULL, data);
566 if (ret != GST_FLOW_OK)
569 *size = (guint) length;
575 * Read the next element as an unsigned int.
579 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
587 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
588 if (ret != GST_FLOW_OK)
591 if (size < 1 || size > 8) {
592 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
593 ("Invalid integer element size %d at position %" G_GUINT64_FORMAT
594 " (0x%" G_GINT64_MODIFIER "x)",
595 size, ebml->offset - size, ebml->offset - size));
596 return GST_FLOW_ERROR;
600 *num = (*num << 8) | *data;
609 * Read the next element as a signed int.
613 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
619 gboolean negative = 0;
623 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
624 if (ret != GST_FLOW_OK)
627 if (size < 1 || size > 8) {
628 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
629 ("Invalid integer element size %d at position %" G_GUINT64_FORMAT
630 " (0x%" G_GINT64_MODIFIER "x)", size, ebml->offset - size,
631 ebml->offset - size));
632 return GST_FLOW_ERROR;
638 *num = *data & ~0x80;
644 *num = (*num << 8) | *data;
657 /* Convert 80 bit extended precision float in big endian format to double.
658 * Code taken from libavutil/intfloat_readwrite.c from ffmpeg,
659 * licensed under LGPL */
668 _ext2dbl (guint8 * data)
670 struct _ext_float *ext = (struct _ext_float *) data;
676 for (i = 0; i < 8; i++)
677 m = (m << 8) + ext->mantissa[i];
678 e = (((gint) ext->exponent[0] & 0x7f) << 8) | ext->exponent[1];
679 if (e == 0x7fff && m)
681 e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
682 * mantissa bit is written as opposed to the
683 * single and double precision formats */
684 if (ext->exponent[0] & 0x80)
690 * Read the next element as a float.
694 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
702 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
703 if (ret != GST_FLOW_OK)
706 if (size != 4 && size != 8 && size != 10) {
707 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
708 ("Invalid float element size %d at position %" G_GUINT64_FORMAT
709 " (0x%" G_GINT64_MODIFIER "x)", size, ebml->offset - size,
710 ebml->offset - size));
711 return GST_FLOW_ERROR;
717 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
718 f = *(gfloat *) data;
721 ((guint8 *) & f)[size - 1] = data[4 - size];
727 } else if (size == 8) {
730 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
731 d = *(gdouble *) data;
734 ((guint8 *) & d)[size - 1] = data[8 - size];
741 *num = _ext2dbl (data);
748 * Read the next element as an ASCII string.
752 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str)
760 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
761 if (ret != GST_FLOW_OK)
764 *str = g_malloc (size + 1);
765 memcpy (*str, data, size);
772 * Read the next element as a UTF-8 string.
776 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
780 #ifndef GST_DISABLE_GST_DEBUG
781 guint64 oldoff = ebml->offset;
784 ret = gst_ebml_read_ascii (ebml, id, str);
785 if (ret != GST_FLOW_OK)
788 if (str != NULL && *str != NULL && **str != '\0' &&
789 !g_utf8_validate (*str, -1, NULL)) {
790 GST_WARNING ("Invalid UTF-8 string at offset %" G_GUINT64_FORMAT, oldoff);
797 * Read the next element as a date.
798 * Returns the seconds since the unix epoch.
802 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
808 ret = gst_ebml_read_sint (ebml, id, &ebml_date);
809 if (ret != GST_FLOW_OK)
812 *date = (ebml_date / GST_SECOND) + GST_EBML_DATE_OFFSET;
818 * Read the next element, but only the header. The contents
819 * are supposed to be sub-elements which can be read separately.
823 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
831 ret = gst_ebml_read_element_id (ebml, id, NULL);
832 if (ret != GST_FLOW_OK)
835 ret = gst_ebml_read_element_length (ebml, &length, NULL);
836 if (ret != GST_FLOW_OK)
840 level = g_slice_new (GstEbmlLevel);
841 level->start = ebml->offset;
842 level->length = length;
843 ebml->level = g_list_prepend (ebml->level, level);
849 * Read the next element as binary data.
853 gst_ebml_read_binary (GstEbmlRead * ebml,
854 guint32 * id, guint8 ** binary, guint64 * length)
862 ret = gst_ebml_read_bytes (ebml, id, &data, &size);
863 if (ret != GST_FLOW_OK)
867 *binary = g_memdup (data, size);
873 * Read an EBML header.
877 gst_ebml_read_header (GstEbmlRead * ebml, gchar ** doctype, guint * version)
879 /* this function is the first to be called */
892 ret = gst_ebml_peek_id (ebml, &level_up, &id);
893 if (ret != GST_FLOW_OK)
896 GST_DEBUG_OBJECT (ebml, "id: %08x", GST_READ_UINT32_BE (&id));
898 if (level_up != 0 || id != GST_EBML_ID_HEADER) {
899 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
900 return GST_FLOW_ERROR;
902 ret = gst_ebml_read_master (ebml, &id);
903 if (ret != GST_FLOW_OK)
907 ret = gst_ebml_peek_id (ebml, &level_up, &id);
908 if (ret != GST_FLOW_OK)
916 /* is our read version uptodate? */
917 case GST_EBML_ID_EBMLREADVERSION:{
920 ret = gst_ebml_read_uint (ebml, &id, &num);
921 if (ret != GST_FLOW_OK)
923 g_assert (id == GST_EBML_ID_EBMLREADVERSION);
924 if (num != GST_EBML_VERSION) {
925 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
926 return GST_FLOW_ERROR;
931 /* we only handle 8 byte lengths at max */
932 case GST_EBML_ID_EBMLMAXSIZELENGTH:{
935 ret = gst_ebml_read_uint (ebml, &id, &num);
936 if (ret != GST_FLOW_OK)
938 g_assert (id == GST_EBML_ID_EBMLMAXSIZELENGTH);
939 if (num > sizeof (guint64)) {
940 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
941 return GST_FLOW_ERROR;
946 /* we handle 4 byte IDs at max */
947 case GST_EBML_ID_EBMLMAXIDLENGTH:{
950 ret = gst_ebml_read_uint (ebml, &id, &num);
951 if (ret != GST_FLOW_OK)
953 g_assert (id == GST_EBML_ID_EBMLMAXIDLENGTH);
954 if (num > sizeof (guint32)) {
955 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
956 return GST_FLOW_ERROR;
961 case GST_EBML_ID_DOCTYPE:{
964 ret = gst_ebml_read_ascii (ebml, &id, &text);
965 if (ret != GST_FLOW_OK)
967 g_assert (id == GST_EBML_ID_DOCTYPE);
976 case GST_EBML_ID_DOCTYPEREADVERSION:{
979 ret = gst_ebml_read_uint (ebml, &id, &num);
980 if (ret != GST_FLOW_OK)
982 g_assert (id == GST_EBML_ID_DOCTYPEREADVERSION);
989 GST_WARNING ("Unknown data type 0x%x in EBML header (ignored)", id);
992 /* we ignore these two, as they don't tell us anything we care about */
993 case GST_EBML_ID_VOID:
994 case GST_EBML_ID_EBMLVERSION:
995 case GST_EBML_ID_DOCTYPEVERSION:
996 ret = gst_ebml_read_skip (ebml);
997 if (ret != GST_FLOW_OK)