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"
31 GST_DEBUG_CATEGORY_STATIC (ebmlread_debug);
32 #define GST_CAT_DEFAULT ebmlread_debug
34 static void gst_ebml_read_class_init (GstEbmlReadClass * klass);
35 static void gst_ebml_read_init (GstEbmlRead * ebml);
36 static GstStateChangeReturn gst_ebml_read_change_state (GstElement * element,
37 GstStateChange transition);
39 /* convenience functions */
40 static gboolean gst_ebml_read_peek_bytes (GstEbmlRead * ebml, guint size,
41 GstBuffer ** p_buf, guint8 ** bytes);
42 static gboolean gst_ebml_read_pull_bytes (GstEbmlRead * ebml, guint size,
43 GstBuffer ** p_buf, guint8 ** bytes);
46 static GstElementClass *parent_class; /* NULL */
49 gst_ebml_read_get_type (void)
51 static GType gst_ebml_read_type; /* 0 */
53 if (!gst_ebml_read_type) {
54 static const GTypeInfo gst_ebml_read_info = {
55 sizeof (GstEbmlReadClass),
58 (GClassInitFunc) gst_ebml_read_class_init,
63 (GInstanceInitFunc) gst_ebml_read_init,
67 g_type_register_static (GST_TYPE_ELEMENT, "GstEbmlRead",
68 &gst_ebml_read_info, 0);
71 return gst_ebml_read_type;
75 gst_ebml_read_class_init (GstEbmlReadClass * klass)
77 GstElementClass *gstelement_class = (GstElementClass *) klass;
79 parent_class = g_type_class_peek_parent (klass);
81 GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
82 0, "EBML stream helper class");
84 gstelement_class->change_state =
85 GST_DEBUG_FUNCPTR (gst_ebml_read_change_state);
89 gst_ebml_read_init (GstEbmlRead * ebml)
95 static GstStateChangeReturn
96 gst_ebml_read_change_state (GstElement * element, GstStateChange transition)
98 GstStateChangeReturn ret;
99 GstEbmlRead *ebml = GST_EBML_READ (element);
101 switch (transition) {
102 case GST_STATE_CHANGE_READY_TO_PAUSED:
103 if (!ebml->sinkpad) {
104 g_return_val_if_reached (GST_STATE_CHANGE_FAILURE);
111 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
113 switch (transition) {
114 case GST_STATE_CHANGE_PAUSED_TO_READY:
116 g_list_foreach (ebml->level, (GFunc) g_free, NULL);
117 g_list_free (ebml->level);
119 if (ebml->cached_buffer) {
120 gst_buffer_unref (ebml->cached_buffer);
121 ebml->cached_buffer = NULL;
134 * Return: the amount of levels in the hierarchy that the
135 * current element lies higher than the previous one.
136 * The opposite isn't done - that's auto-done using master
141 gst_ebml_read_element_level_up (GstEbmlRead * ebml)
144 guint64 pos = ebml->offset;
146 while (ebml->level != NULL) {
147 GList *last = g_list_last (ebml->level);
148 GstEbmlLevel *level = last->data;
150 if (pos >= level->start + level->length) {
151 ebml->level = g_list_remove (ebml->level, level);
163 * Calls pull_range for (offset,size) without advancing our offset
166 gst_ebml_read_peek_bytes (GstEbmlRead * ebml, guint size, GstBuffer ** p_buf,
171 /* Caching here actually makes much less difference than one would expect.
172 * We do it mainly to avoid pulling buffers of 1 byte all the time */
173 if (ebml->cached_buffer) {
174 guint64 cache_offset = GST_BUFFER_OFFSET (ebml->cached_buffer);
175 guint cache_size = GST_BUFFER_SIZE (ebml->cached_buffer);
177 if (cache_offset <= ebml->offset &&
178 (ebml->offset + size) < (cache_offset + cache_size)) {
180 *p_buf = gst_buffer_create_sub (ebml->cached_buffer,
181 ebml->offset - cache_offset, size);
184 GST_BUFFER_DATA (ebml->cached_buffer) + ebml->offset - cache_offset;
187 gst_buffer_unref (ebml->cached_buffer);
188 ebml->cached_buffer = NULL;
191 if (gst_pad_pull_range (ebml->sinkpad, ebml->offset, MAX (size, 64 * 1024),
192 &ebml->cached_buffer) == GST_FLOW_OK &&
193 GST_BUFFER_SIZE (ebml->cached_buffer) >= size) {
195 *p_buf = gst_buffer_create_sub (ebml->cached_buffer, 0, size);
197 *bytes = GST_BUFFER_DATA (ebml->cached_buffer);
204 ret = gst_pad_pull_range (ebml->sinkpad, ebml->offset, size, p_buf);
205 if (ret != GST_FLOW_OK) {
206 GST_DEBUG ("pull_range returned %d", ret);
210 if (GST_BUFFER_SIZE (*p_buf) < size) {
211 GST_WARNING_OBJECT (ebml, "Dropping short buffer at offset %"
212 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", ebml->offset,
213 size, GST_BUFFER_SIZE (*p_buf));
214 gst_buffer_unref (*p_buf);
222 *bytes = GST_BUFFER_DATA (*p_buf);
228 * Calls pull_range for (offset,size) and advances our offset by size
231 gst_ebml_read_pull_bytes (GstEbmlRead * ebml, guint size, GstBuffer ** p_buf,
234 if (!gst_ebml_read_peek_bytes (ebml, size, p_buf, bytes))
237 ebml->offset += size;
242 * Read: the element content data ID.
243 * Return: FALSE on error.
247 gst_ebml_read_element_id (GstEbmlRead * ebml, guint32 * id, guint * level_up)
250 gint len_mask = 0x80, read = 1, n = 1;
254 if (!gst_ebml_read_peek_bytes (ebml, 1, NULL, &buf))
257 b = GST_READ_UINT8 (buf);
261 while (read <= 4 && !(total & len_mask)) {
266 guint64 pos = ebml->offset;
268 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
269 ("Invalid EBML ID size tag (0x%x) at position %llu (0x%llx)",
270 (guint) b, pos, pos));
274 if (!gst_ebml_read_peek_bytes (ebml, read, NULL, &buf))
278 b = GST_READ_UINT8 (buf + n);
279 total = (total << 8) | b;
287 *level_up = gst_ebml_read_element_level_up (ebml);
289 ebml->offset += read;
294 * Read: element content length.
295 * Return: the number of bytes read or -1 on error.
299 gst_ebml_read_element_length (GstEbmlRead * ebml, guint64 * length)
302 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
306 if (!gst_ebml_read_peek_bytes (ebml, 1, NULL, &buf))
309 b = GST_READ_UINT8 (buf);
313 while (read <= 8 && !(total & len_mask)) {
318 guint64 pos = ebml->offset;
320 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
321 ("Invalid EBML length size tag (0x%x) at position %llu (0x%llx)",
322 (guint) b, pos, pos));
326 if ((total &= (len_mask - 1)) == len_mask - 1)
329 if (!gst_ebml_read_peek_bytes (ebml, read, NULL, &buf))
333 guint8 b = GST_READ_UINT8 (buf + n);
337 total = (total << 8) | b;
342 *length = G_MAXUINT64;
346 ebml->offset += read;
352 * Return: the ID of the next element.
353 * Level_up contains the amount of levels that this
354 * next element lies higher than the previous one.
358 gst_ebml_peek_id (GstEbmlRead * ebml, guint * level_up, guint32 * id)
364 off = ebml->offset; /* save offset */
366 if (!gst_ebml_read_element_id (ebml, id, level_up))
369 ebml->offset = off; /* restore offset */
374 * Return the length of the stream in bytes
378 gst_ebml_read_get_length (GstEbmlRead * ebml)
380 GstFormat fmt = GST_FORMAT_BYTES;
383 if (!gst_pad_query_duration (GST_PAD_PEER (ebml->sinkpad), &fmt, &end))
384 g_return_val_if_reached (0); ///// FIXME /////////
386 if (fmt != GST_FORMAT_BYTES || end < 0)
387 g_return_val_if_reached (0); ///// FIXME /////////
393 * Seek to a given offset.
397 gst_ebml_read_seek (GstEbmlRead * ebml, guint64 offset)
399 if (offset >= gst_ebml_read_get_length (ebml))
402 ebml->offset = offset;
408 * Skip the next element.
412 gst_ebml_read_skip (GstEbmlRead * ebml)
417 if (!gst_ebml_read_element_id (ebml, &id, NULL))
420 if (gst_ebml_read_element_length (ebml, &length) < 0)
423 ebml->offset += length;
428 * Read the next element as a GstBuffer (binary).
432 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
436 if (!gst_ebml_read_element_id (ebml, id, NULL))
439 if (gst_ebml_read_element_length (ebml, &length) < 0)
443 *buf = gst_buffer_new ();
448 if (!gst_ebml_read_pull_bytes (ebml, (guint) length, buf, NULL))
455 * Read the next element, return a pointer to it and its size.
459 gst_ebml_read_bytes (GstEbmlRead * ebml, guint32 * id, guint8 ** data,
466 if (!gst_ebml_read_element_id (ebml, id, NULL))
469 if (gst_ebml_read_element_length (ebml, &length) < 0)
478 if (!gst_ebml_read_pull_bytes (ebml, (guint) length, NULL, data))
481 *size = (guint) length;
487 * Read the next element as an unsigned int.
491 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
496 if (!gst_ebml_read_bytes (ebml, id, &data, &size))
499 if (size < 1 || size > 8) {
500 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
501 ("Invalid integer element size %d at position %llu (0x%llu)",
502 size, ebml->offset - size, ebml->offset - size));
507 *num = (*num << 8) | *data;
516 * Read the next element as a signed int.
520 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
524 gboolean negative = 0;
526 if (!gst_ebml_read_bytes (ebml, id, &data, &size))
529 if (size < 1 || size > 8) {
530 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
531 ("Invalid integer element size %d at position %llu (0x%llx)",
532 size, ebml->offset - size, ebml->offset - size));
539 *num = *data & ~0x80;
545 *num = (*num << 8) | *data;
559 * Read the next element as a float.
563 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
568 if (!gst_ebml_read_bytes (ebml, id, &data, &size))
571 if (size != 4 && size != 8 && size != 10) {
572 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
573 ("Invalid float element size %d at position %llu (0x%llx)",
574 size, ebml->offset - size, ebml->offset - size));
579 GST_ELEMENT_ERROR (ebml, CORE, NOT_IMPLEMENTED, (NULL),
580 ("FIXME! 10-byte floats unimplemented"));
587 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
588 f = *(gfloat *) data;
591 ((guint8 *) & f)[size - 1] = data[4 - size];
600 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
601 d = *(gdouble *) data;
604 ((guint8 *) & d)[size - 1] = data[8 - size];
616 * Read the next element as an ASCII string.
620 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str)
625 if (!gst_ebml_read_bytes (ebml, id, &data, &size))
628 *str = g_malloc (size + 1);
629 memcpy (*str, data, size);
636 * Read the next element as a UTF-8 string.
640 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
644 #ifndef GST_DISABLE_GST_DEBUG
645 guint64 oldoff = ebml->offset;
648 ret = gst_ebml_read_ascii (ebml, id, str);
650 if (str != NULL && *str != NULL && **str != '\0' &&
651 !g_utf8_validate (*str, -1, NULL)) {
652 GST_WARNING ("Invalid UTF-8 string at offset %" G_GUINT64_FORMAT, oldoff);
659 * Read the next element as a date.
660 * Returns the seconds since the unix epoch.
664 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
667 gboolean res = gst_ebml_read_sint (ebml, id, &ebml_date);
669 *date = (ebml_date / GST_SECOND) + GST_EBML_DATE_OFFSET;
674 * Read the next element, but only the header. The contents
675 * are supposed to be sub-elements which can be read separately.
679 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
684 if (!gst_ebml_read_element_id (ebml, id, NULL))
687 if (gst_ebml_read_element_length (ebml, &length) < 0)
691 level = g_new (GstEbmlLevel, 1);
692 level->start = ebml->offset;
693 level->length = length;
694 ebml->level = g_list_append (ebml->level, level);
700 * Read the next element as binary data.
704 gst_ebml_read_binary (GstEbmlRead * ebml,
705 guint32 * id, guint8 ** binary, guint64 * length)
710 if (!gst_ebml_read_bytes (ebml, id, &data, &size))
714 *binary = g_memdup (data, size);
720 * Read an EBML header.
724 gst_ebml_read_header (GstEbmlRead * ebml, gchar ** doctype, guint * version)
726 /* this function is the first to be called */
736 if (!gst_ebml_peek_id (ebml, &level_up, &id))
739 GST_DEBUG_OBJECT (ebml, "id: %08x", GST_READ_UINT32_BE (&id));
741 if (level_up != 0 || id != GST_EBML_ID_HEADER) {
742 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
745 if (!gst_ebml_read_master (ebml, &id))
749 if (!gst_ebml_peek_id (ebml, &level_up, &id))
757 /* is our read version uptodate? */
758 case GST_EBML_ID_EBMLREADVERSION:{
761 if (!gst_ebml_read_uint (ebml, &id, &num))
763 g_assert (id == GST_EBML_ID_EBMLREADVERSION);
764 if (num != GST_EBML_VERSION)
769 /* we only handle 8 byte lengths at max */
770 case GST_EBML_ID_EBMLMAXSIZELENGTH:{
773 if (!gst_ebml_read_uint (ebml, &id, &num))
775 g_assert (id == GST_EBML_ID_EBMLMAXSIZELENGTH);
776 if (num != sizeof (guint64))
781 /* we handle 4 byte IDs at max */
782 case GST_EBML_ID_EBMLMAXIDLENGTH:{
785 if (!gst_ebml_read_uint (ebml, &id, &num))
787 g_assert (id == GST_EBML_ID_EBMLMAXIDLENGTH);
788 if (num != sizeof (guint32))
793 case GST_EBML_ID_DOCTYPE:{
796 if (!gst_ebml_read_ascii (ebml, &id, &text))
798 g_assert (id == GST_EBML_ID_DOCTYPE);
808 case GST_EBML_ID_DOCTYPEREADVERSION:{
811 if (!gst_ebml_read_uint (ebml, &id, &num))
813 g_assert (id == GST_EBML_ID_DOCTYPEREADVERSION);
820 GST_WARNING ("Unknown data type 0x%x in EBML header (ignored)", id);
823 /* we ignore these two, as they don't tell us anything we care about */
824 case GST_EBML_ID_VOID:
825 case GST_EBML_ID_EBMLVERSION:
826 case GST_EBML_ID_DOCTYPEVERSION:
827 if (!gst_ebml_read_skip (ebml))