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"
36 static void gst_ebml_read_class_init (GstEbmlReadClass *klass);
37 static void gst_ebml_read_init (GstEbmlRead *ebml);
38 static GstElementStateReturn
39 gst_ebml_read_change_state (GstElement *element);
41 static GstElementClass *parent_class = NULL;
44 gst_ebml_read_get_type (void)
46 static GType gst_ebml_read_type = 0;
48 if (!gst_ebml_read_type) {
49 static const GTypeInfo gst_ebml_read_info = {
50 sizeof (GstEbmlReadClass),
53 (GClassInitFunc) gst_ebml_read_class_init,
58 (GInstanceInitFunc) gst_ebml_read_init,
62 g_type_register_static (GST_TYPE_ELEMENT, "GstEbmlRead",
63 &gst_ebml_read_info, 0);
66 return gst_ebml_read_type;
70 gst_ebml_read_class_init (GstEbmlReadClass *klass)
72 GstElementClass *gstelement_class = (GstElementClass *) klass;
74 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
76 gstelement_class->change_state = gst_ebml_read_change_state;
80 gst_ebml_read_init (GstEbmlRead *ebml)
87 static GstElementStateReturn
88 gst_ebml_read_change_state (GstElement *element)
90 GstEbmlRead *ebml = GST_EBML_READ (element);
92 switch (GST_STATE_TRANSITION (element)) {
93 case GST_STATE_READY_TO_PAUSED:
95 return GST_STATE_FAILURE;
96 ebml->bs = gst_bytestream_new (ebml->sinkpad);
98 case GST_STATE_PAUSED_TO_READY:
99 gst_bytestream_destroy (ebml->bs);
100 while (ebml->level) {
101 GstEbmlLevel *level = ebml->level->data;
103 ebml->level = g_list_remove (ebml->level, level);
111 if (GST_ELEMENT_CLASS (parent_class)->change_state)
112 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
114 return GST_STATE_SUCCESS;
118 * Return: the amount of levels in the hierarchy that the
119 * current element lies higher than the previous one.
120 * The opposite isn't done - that's auto-done using master
125 gst_ebml_read_element_level_up (GstEbmlRead *ebml)
128 guint64 pos = gst_bytestream_tell (ebml->bs);
130 while (ebml->level != NULL) {
131 GList *last = g_list_last (ebml->level);
132 GstEbmlLevel *level = last->data;
134 if (pos >= level->start + level->length) {
135 ebml->level = g_list_remove (ebml->level, level);
146 * Read: the element content data ID.
147 * Return: the number of bytes read or -1 on error.
151 gst_ebml_read_element_id (GstEbmlRead *ebml,
156 gint len_mask = 0x80, read = 1, n = 1;
159 if (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
160 GstEvent *event = NULL;
163 /* Here, we might encounter EOS */
164 gst_bytestream_get_status (ebml->bs, &remaining, &event);
165 if (event && GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
166 gst_pad_event_default (ebml->sinkpad, event);
168 guint64 pos = gst_bytestream_tell (ebml->bs);
169 gst_event_unref (event);
170 gst_element_error (GST_ELEMENT (ebml),
171 "Read error at position %llu (0x%llx)",
177 while (read <= 4 && !(total & len_mask)) {
182 guint64 pos = gst_bytestream_tell (ebml->bs);
183 gst_element_error (GST_ELEMENT (ebml),
184 "Invalid EBML ID size tag (0x%x) at position %llu (0x%llx)",
189 if (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
190 guint64 pos = gst_bytestream_tell (ebml->bs);
191 gst_element_error (GST_ELEMENT (ebml),
192 "Read error at position %llu (0x%llx)",
197 total = (total << 8) | data[n++];
203 *level_up = gst_ebml_read_element_level_up (ebml);
209 * Read: element content length.
210 * Return: the number of bytes read or -1 on error.
214 gst_ebml_read_element_length (GstEbmlRead *ebml,
218 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
221 if (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
222 guint64 pos = gst_bytestream_tell (ebml->bs);
223 gst_element_error (GST_ELEMENT (ebml),
224 "Read error at position %llu (0x%llx)",
229 while (read <= 8 && !(total & len_mask)) {
234 guint64 pos = gst_bytestream_tell (ebml->bs);
235 gst_element_error (GST_ELEMENT (ebml),
236 "Invalid EBML length size tag (0x%x) at position %llu (0x%llx)",
241 if ((total &= (len_mask - 1)) == len_mask - 1)
243 if (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
244 guint64 pos = gst_bytestream_tell (ebml->bs);
245 gst_element_error (GST_ELEMENT (ebml),
246 "Read error at position %llu (0x%llx)",
253 total = (total << 8) | data[n];
258 *length = G_MAXUINT64;
266 * Read: the actual data.
267 * Return: the data, as a GstBuffer.
271 gst_ebml_read_element_data (GstEbmlRead *ebml,
274 GstBuffer *buf = NULL;
276 if (gst_bytestream_peek (ebml->bs, &buf, length) != length) {
277 guint64 pos = gst_bytestream_tell (ebml->bs);
278 gst_element_error (GST_ELEMENT (ebml),
279 "Read error at position %llu (0x%llx)",
282 gst_buffer_unref (buf);
286 gst_bytestream_flush_fast (ebml->bs, length);
292 * Return: the ID of the next element.
293 * Level_up contains the amount of levels that this
294 * next element lies higher than the previous one.
298 gst_ebml_peek_id (GstEbmlRead *ebml,
305 if (gst_ebml_read_element_id (ebml, &id, level_up) < 0)
312 * Seek to a given offset.
316 gst_ebml_read_seek (GstEbmlRead *ebml,
323 /* first, flush remaining buffers */
324 gst_bytestream_get_status (ebml->bs, &remaining, &event);
326 g_warning ("Unexpected event before seek");
327 gst_event_unref (event);
330 gst_bytestream_flush_fast (ebml->bs, remaining);
333 if (!gst_bytestream_seek (ebml->bs, offset, GST_SEEK_METHOD_SET)) {
334 gst_element_error (GST_ELEMENT (ebml),
335 "Seek to position %llu (0x%llx) failed",
340 /* and now, peek a new byte. This will fail because there's a
341 * pending event. Then, take the event and return it. */
342 if (gst_bytestream_peek_bytes (ebml->bs, &data, 1))
343 g_warning ("Unexpected data after seek");
345 /* get the discont event and return */
346 gst_bytestream_get_status (ebml->bs, &remaining, &event);
347 if (!event || GST_EVENT_TYPE (event) != GST_EVENT_DISCONTINUOUS) {
348 gst_element_error (GST_ELEMENT (ebml),
349 "No discontinuity event after seek");
351 gst_event_unref (event);
359 * Skip the next element.
363 gst_ebml_read_skip (GstEbmlRead *ebml)
366 guint32 id, remaining;
370 if ((bytes = gst_ebml_read_element_id (ebml, &id, NULL)) < 0)
372 gst_bytestream_flush_fast (ebml->bs, bytes);
374 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
376 gst_bytestream_flush_fast (ebml->bs, bytes);
378 /* do we have enough bytes left to skip? */
379 gst_bytestream_get_status (ebml->bs, &remaining, &event);
381 g_warning ("Unexpected event before skip");
382 gst_event_unref (event);
385 if (remaining >= length)
386 return gst_bytestream_flush (ebml->bs, length);
388 if (!(event = gst_ebml_read_seek (ebml,
389 gst_bytestream_tell (ebml->bs) + length)))
392 gst_event_unref (event);
398 * Read the next element as a GstBuffer (binary).
402 gst_ebml_read_buffer (GstEbmlRead *ebml,
409 if ((bytes = gst_ebml_read_element_id (ebml, id, NULL)) < 0)
411 gst_bytestream_flush_fast (ebml->bs, bytes);
413 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
415 gst_bytestream_flush_fast (ebml->bs, bytes);
417 return ((*buf = gst_ebml_read_element_data (ebml, length)) != NULL);
421 * Read the next element as an unsigned int.
425 gst_ebml_read_uint (GstEbmlRead *ebml,
433 if (!gst_ebml_read_buffer (ebml, id, &buf))
436 data = GST_BUFFER_DATA (buf);
437 size = GST_BUFFER_SIZE (buf);
438 if (size < 1 || size > 8) {
439 gst_element_error (GST_ELEMENT (ebml),
440 "Invalid integer element size %d at position %llu (0x%llu)",
441 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf));
442 gst_buffer_unref (buf);
447 *num = (*num << 8) | data[GST_BUFFER_SIZE (buf) - size];
451 gst_buffer_unref (buf);
457 * Read the next element as a signed int.
461 gst_ebml_read_sint (GstEbmlRead *ebml,
467 guint size, negative = 0, n = 0;
469 if (!gst_ebml_read_buffer (ebml, id, &buf))
472 data = GST_BUFFER_DATA (buf);
473 size = GST_BUFFER_SIZE (buf);
474 if (size < 1 || size > 8) {
475 gst_element_error (GST_ELEMENT (ebml),
476 "Invalid integer element size %d at position %llu (0x%llx)",
477 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf));
478 gst_buffer_unref (buf);
481 if (data[0] & 0x80) {
487 *num = (*num << 8) | data[n++];
492 *num = *num - (1LL << ((8 * size) - 1));
495 gst_buffer_unref (buf);
501 * Read the next element as a float.
505 gst_ebml_read_float (GstEbmlRead *ebml,
513 if (!gst_ebml_read_buffer (ebml, id, &buf))
516 data = GST_BUFFER_DATA (buf);
517 size = GST_BUFFER_SIZE (buf);
519 if (size != 4 && size != 8 && size != 10) {
520 gst_element_error (GST_ELEMENT (ebml),
521 "Invalid float element size %d at position %llu (0x%llx)",
522 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf));
523 gst_buffer_unref (buf);
528 gst_element_error (GST_ELEMENT (ebml),
529 "FIXME! 10-byte floats unimplemented");
530 gst_buffer_unref (buf);
537 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
538 f = * (gfloat *) data;
541 ((guint8 *) &f)[size - 1] = data[4 - size];
550 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
551 d = * (gdouble *) data;
554 ((guint8 *) &d)[size - 1] = data[8 - size];
562 gst_buffer_unref (buf);
568 * Read the next element as an ASCII string.
572 gst_ebml_read_ascii (GstEbmlRead *ebml,
578 if (!gst_ebml_read_buffer (ebml, id, &buf))
581 *str = g_malloc (GST_BUFFER_SIZE (buf) + 1);
582 memcpy (*str, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
583 (*str)[GST_BUFFER_SIZE (buf)] = '\0';
585 gst_buffer_unref (buf);
591 * Read the next element as a UTF-8 string.
595 gst_ebml_read_utf8 (GstEbmlRead *ebml,
599 return gst_ebml_read_ascii (ebml, id, str);
603 * Read the next element as a date (nanoseconds since 1/1/2000).
607 gst_ebml_read_date (GstEbmlRead *ebml,
611 return gst_ebml_read_sint (ebml, id, date);
615 * Read the next element, but only the header. The contents
616 * are supposed to be sub-elements which can be read separately.
620 gst_ebml_read_master (GstEbmlRead *ebml,
627 if ((bytes = gst_ebml_read_element_id (ebml, id, NULL)) < 0)
629 gst_bytestream_flush_fast (ebml->bs, bytes);
631 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
633 gst_bytestream_flush_fast (ebml->bs, bytes);
636 level = g_new (GstEbmlLevel, 1);
637 level->start = gst_bytestream_tell (ebml->bs);
638 level->length = length;
639 ebml->level = g_list_append (ebml->level, level);
645 * Read the next element as binary data.
649 gst_ebml_read_binary (GstEbmlRead *ebml,
656 if (!gst_ebml_read_buffer (ebml, id, &buf))
659 *length = GST_BUFFER_SIZE (buf);
660 *binary = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
662 gst_buffer_unref (buf);
668 * Read an EBML header.
672 gst_ebml_read_header (GstEbmlRead *ebml,
676 /* this function is the first to be called */
686 if (!(id = gst_ebml_peek_id (ebml, &level_up)))
688 if (level_up != 0 || id != GST_EBML_ID_HEADER) {
689 gst_element_error (GST_ELEMENT (ebml), "Not a EBML file");
692 if (!gst_ebml_read_master (ebml, &id))
694 g_assert (id == GST_EBML_ID_HEADER);
697 if (!(id = gst_ebml_peek_id (ebml, &level_up)))
705 /* is our read version uptodate? */
706 case GST_EBML_ID_EBMLREADVERSION: {
709 if (!gst_ebml_read_uint (ebml, &id, &num))
711 g_assert (id == GST_EBML_ID_EBMLREADVERSION);
712 if (num != GST_EBML_VERSION)
717 /* we only handle 8 byte lengths at max */
718 case GST_EBML_ID_EBMLMAXSIZELENGTH: {
721 if (!gst_ebml_read_uint (ebml, &id, &num))
723 g_assert (id == GST_EBML_ID_EBMLMAXSIZELENGTH);
724 if (num != sizeof (guint64))
729 /* we handle 4 byte IDs at max */
730 case GST_EBML_ID_EBMLMAXIDLENGTH: {
733 if (!gst_ebml_read_uint (ebml, &id, &num))
735 g_assert (id == GST_EBML_ID_EBMLMAXIDLENGTH);
736 if (num != sizeof (guint32))
741 case GST_EBML_ID_DOCTYPE: {
744 if (!gst_ebml_read_ascii (ebml, &id, &text))
746 g_assert (id == GST_EBML_ID_DOCTYPE);
756 case GST_EBML_ID_DOCTYPEREADVERSION: {
759 if (!gst_ebml_read_uint (ebml, &id, &num))
761 g_assert (id == GST_EBML_ID_DOCTYPEREADVERSION);
768 GST_WARNING ("Unknown data type 0x%x in EBML header (ignored)", id);
771 /* we ignore these two, as they don't tell us anything we care about */
772 case GST_EBML_ID_VOID:
773 case GST_EBML_ID_EBMLVERSION:
774 case GST_EBML_ID_DOCTYPEVERSION:
775 if (!gst_ebml_read_skip (ebml))