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"
37 static void gst_ebml_read_class_init (GstEbmlReadClass * klass);
38 static void gst_ebml_read_init (GstEbmlRead * ebml);
39 static GstElementStateReturn 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, guint32 * id, guint * level_up)
154 gint len_mask = 0x80, read = 1, n = 1;
157 while (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
158 GstEvent *event = NULL;
161 /* Here, we might encounter EOS */
162 gst_bytestream_get_status (ebml->bs, &remaining, &event);
163 if (event && GST_IS_EVENT (event)) {
164 gboolean eos = (GST_EVENT_TYPE (event) == GST_EVENT_EOS);
166 gst_pad_event_default (ebml->sinkpad, event);
170 guint64 pos = gst_bytestream_tell (ebml->bs);
172 gst_event_unref (event);
173 GST_ELEMENT_ERROR (ebml, RESOURCE, READ, (NULL),
174 ("Read error at position %llu (0x%llx)", pos, pos));
179 while (read <= 4 && !(total & len_mask)) {
184 guint64 pos = gst_bytestream_tell (ebml->bs);
186 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
187 ("Invalid EBML ID size tag (0x%x) at position %llu (0x%llx)",
192 if (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
193 guint64 pos = gst_bytestream_tell (ebml->bs);
195 GST_ELEMENT_ERROR (ebml, RESOURCE, READ, (NULL),
196 ("Read error at position %llu (0x%llx)", pos, pos));
200 total = (total << 8) | data[n++];
206 *level_up = gst_ebml_read_element_level_up (ebml);
212 * Read: element content length.
213 * Return: the number of bytes read or -1 on error.
217 gst_ebml_read_element_length (GstEbmlRead * ebml, guint64 * length)
220 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
223 if (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
224 guint64 pos = gst_bytestream_tell (ebml->bs);
226 GST_ELEMENT_ERROR (ebml, RESOURCE, READ, (NULL),
227 ("Read error at position %llu (0x%llx)", pos, pos));
231 while (read <= 8 && !(total & len_mask)) {
236 guint64 pos = gst_bytestream_tell (ebml->bs);
238 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
239 ("Invalid EBML length size tag (0x%x) at position %llu (0x%llx)",
244 if ((total &= (len_mask - 1)) == len_mask - 1)
246 if (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
247 guint64 pos = gst_bytestream_tell (ebml->bs);
249 GST_ELEMENT_ERROR (ebml, RESOURCE, READ, (NULL),
250 ("Read error at position %llu (0x%llx)", pos, pos));
256 total = (total << 8) | data[n];
261 *length = G_MAXUINT64;
269 * Read: the actual data.
270 * Return: the data, as a GstBuffer.
274 gst_ebml_read_element_data (GstEbmlRead * ebml, guint64 length)
276 GstBuffer *buf = NULL;
278 if (gst_bytestream_peek (ebml->bs, &buf, length) != length) {
279 guint64 pos = gst_bytestream_tell (ebml->bs);
281 GST_ELEMENT_ERROR (ebml, RESOURCE, READ, (NULL),
282 ("Read error at position %llu (0x%llx)", pos, pos));
284 gst_buffer_unref (buf);
288 gst_bytestream_flush_fast (ebml->bs, length);
294 * Return: the ID of the next element.
295 * Level_up contains the amount of levels that this
296 * next element lies higher than the previous one.
300 gst_ebml_peek_id (GstEbmlRead * ebml, guint * level_up)
306 if (gst_ebml_read_element_id (ebml, &id, level_up) <= 0)
313 * Seek to a given offset.
317 gst_ebml_read_seek (GstEbmlRead * ebml, guint64 offset)
320 GstEvent *event = NULL;
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 (ebml, RESOURCE, SEEK, (NULL),
335 ("Seek to position %llu (0x%llx) failed", offset, offset));
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 GST_WARNING ("Unexpected data after seek - this means seek failed");
347 /* get the discont event and return */
348 gst_bytestream_get_status (ebml->bs, &remaining, &event);
350 GST_WARNING ("No discontinuity event after seek - seek failed");
352 } else if (GST_EVENT_TYPE (event) != GST_EVENT_DISCONTINUOUS) {
353 gst_pad_event_default (ebml->sinkpad, event);
362 * Skip the next element.
366 gst_ebml_read_skip (GstEbmlRead * ebml)
369 guint32 id, remaining;
373 if ((bytes = gst_ebml_read_element_id (ebml, &id, NULL)) < 0)
375 gst_bytestream_flush_fast (ebml->bs, bytes);
377 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
379 gst_bytestream_flush_fast (ebml->bs, bytes);
381 /* do we have enough bytes left to skip? */
382 gst_bytestream_get_status (ebml->bs, &remaining, &event);
384 g_warning ("Unexpected event before skip");
385 gst_event_unref (event);
388 if (remaining >= length)
389 return gst_bytestream_flush (ebml->bs, length);
391 if (!(event = gst_ebml_read_seek (ebml,
392 gst_bytestream_tell (ebml->bs) + length)))
395 gst_event_unref (event);
401 * Read the next element as a GstBuffer (binary).
405 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
410 if ((bytes = gst_ebml_read_element_id (ebml, id, NULL)) < 0)
412 gst_bytestream_flush_fast (ebml->bs, bytes);
414 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
416 gst_bytestream_flush_fast (ebml->bs, bytes);
418 return ((*buf = gst_ebml_read_element_data (ebml, length)) != NULL);
422 * Read the next element as an unsigned int.
426 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
432 if (!gst_ebml_read_buffer (ebml, id, &buf))
435 data = GST_BUFFER_DATA (buf);
436 size = GST_BUFFER_SIZE (buf);
437 if (size < 1 || size > 8) {
438 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
439 ("Invalid integer element size %d at position %llu (0x%llu)",
440 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf)));
441 gst_buffer_unref (buf);
446 *num = (*num << 8) | data[GST_BUFFER_SIZE (buf) - size];
450 gst_buffer_unref (buf);
456 * Read the next element as a signed int.
460 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
464 guint size, negative = 0, n = 0;
466 if (!gst_ebml_read_buffer (ebml, id, &buf))
469 data = GST_BUFFER_DATA (buf);
470 size = GST_BUFFER_SIZE (buf);
471 if (size < 1 || size > 8) {
472 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
473 ("Invalid integer element size %d at position %llu (0x%llx)",
474 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf)));
475 gst_buffer_unref (buf);
478 if (data[0] & 0x80) {
484 *num = (*num << 8) | data[n++];
489 *num = *num - (1LL << ((8 * size) - 1));
492 gst_buffer_unref (buf);
498 * Read the next element as a float.
502 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
508 if (!gst_ebml_read_buffer (ebml, id, &buf))
511 data = GST_BUFFER_DATA (buf);
512 size = GST_BUFFER_SIZE (buf);
514 if (size != 4 && size != 8 && size != 10) {
515 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
516 ("Invalid float element size %d at position %llu (0x%llx)",
517 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf)));
518 gst_buffer_unref (buf);
523 GST_ELEMENT_ERROR (ebml, CORE, NOT_IMPLEMENTED, (NULL),
524 ("FIXME! 10-byte floats unimplemented"));
525 gst_buffer_unref (buf);
532 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
533 f = *(gfloat *) data;
536 ((guint8 *) & f)[size - 1] = data[4 - size];
545 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
546 d = *(gdouble *) data;
549 ((guint8 *) & d)[size - 1] = data[8 - size];
557 gst_buffer_unref (buf);
563 * Read the next element as an ASCII string.
567 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str)
571 if (!gst_ebml_read_buffer (ebml, id, &buf))
574 *str = g_malloc (GST_BUFFER_SIZE (buf) + 1);
575 memcpy (*str, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
576 (*str)[GST_BUFFER_SIZE (buf)] = '\0';
578 gst_buffer_unref (buf);
584 * Read the next element as a UTF-8 string.
588 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
590 return gst_ebml_read_ascii (ebml, id, str);
594 * Read the next element as a date (nanoseconds since 1/1/2000).
598 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
600 return gst_ebml_read_sint (ebml, id, date);
604 * Read the next element, but only the header. The contents
605 * are supposed to be sub-elements which can be read separately.
609 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
615 if ((bytes = gst_ebml_read_element_id (ebml, id, NULL)) < 0)
617 gst_bytestream_flush_fast (ebml->bs, bytes);
619 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
621 gst_bytestream_flush_fast (ebml->bs, bytes);
624 level = g_new (GstEbmlLevel, 1);
625 level->start = gst_bytestream_tell (ebml->bs);
626 level->length = length;
627 ebml->level = g_list_append (ebml->level, level);
633 * Read the next element as binary data.
637 gst_ebml_read_binary (GstEbmlRead * ebml,
638 guint32 * id, guint8 ** binary, guint64 * length)
642 if (!gst_ebml_read_buffer (ebml, id, &buf))
645 *length = GST_BUFFER_SIZE (buf);
646 *binary = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
648 gst_buffer_unref (buf);
654 * Read an EBML header.
658 gst_ebml_read_header (GstEbmlRead * ebml, gchar ** doctype, guint * version)
660 /* this function is the first to be called */
670 if (!(id = gst_ebml_peek_id (ebml, &level_up)))
672 if (level_up != 0 || id != GST_EBML_ID_HEADER) {
673 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
676 if (!gst_ebml_read_master (ebml, &id))
678 g_assert (id == GST_EBML_ID_HEADER);
681 if (!(id = gst_ebml_peek_id (ebml, &level_up)))
689 /* is our read version uptodate? */
690 case GST_EBML_ID_EBMLREADVERSION:{
693 if (!gst_ebml_read_uint (ebml, &id, &num))
695 g_assert (id == GST_EBML_ID_EBMLREADVERSION);
696 if (num != GST_EBML_VERSION)
701 /* we only handle 8 byte lengths at max */
702 case GST_EBML_ID_EBMLMAXSIZELENGTH:{
705 if (!gst_ebml_read_uint (ebml, &id, &num))
707 g_assert (id == GST_EBML_ID_EBMLMAXSIZELENGTH);
708 if (num != sizeof (guint64))
713 /* we handle 4 byte IDs at max */
714 case GST_EBML_ID_EBMLMAXIDLENGTH:{
717 if (!gst_ebml_read_uint (ebml, &id, &num))
719 g_assert (id == GST_EBML_ID_EBMLMAXIDLENGTH);
720 if (num != sizeof (guint32))
725 case GST_EBML_ID_DOCTYPE:{
728 if (!gst_ebml_read_ascii (ebml, &id, &text))
730 g_assert (id == GST_EBML_ID_DOCTYPE);
740 case GST_EBML_ID_DOCTYPEREADVERSION:{
743 if (!gst_ebml_read_uint (ebml, &id, &num))
745 g_assert (id == GST_EBML_ID_DOCTYPEREADVERSION);
752 GST_WARNING ("Unknown data type 0x%x in EBML header (ignored)", id);
755 /* we ignore these two, as they don't tell us anything we care about */
756 case GST_EBML_ID_VOID:
757 case GST_EBML_ID_EBMLVERSION:
758 case GST_EBML_ID_DOCTYPEVERSION:
759 if (!gst_ebml_read_skip (ebml))