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 * Event handler. Basic:
119 * - EOS: end-of-file, stop processing, forward EOS.
120 * - Interrupt: stop processing.
121 * - Discont: shouldn't be handled here but in the seek handler. Error.
122 * - Flush: ignore, since we check for flush flags manually. Don't forward.
123 * - Others: warn, ignore.
124 * Return value indicates whether to continue processing.
128 gst_ebml_read_use_event (GstEbmlRead * ebml, GstEvent * event)
131 GST_ELEMENT_ERROR (ebml, RESOURCE, READ, (NULL), (NULL));
135 switch (GST_EVENT_TYPE (event)) {
137 gst_pad_event_default (ebml->sinkpad, event);
140 case GST_EVENT_INTERRUPT:
141 gst_event_unref (event);
144 case GST_EVENT_DISCONTINUOUS:
145 GST_WARNING_OBJECT (ebml, "Unexpected discont - might lose sync");
146 gst_pad_event_default (ebml->sinkpad, event);
149 case GST_EVENT_FLUSH:
150 gst_event_unref (event);
154 GST_WARNING ("don't know how to handle event %d", GST_EVENT_TYPE (event));
155 gst_pad_event_default (ebml->sinkpad, event);
160 g_assert_not_reached ();
165 gst_ebml_read_handle_event (GstEbmlRead * ebml)
167 GstEvent *event = NULL;
170 gst_bytestream_get_status (ebml->bs, &remaining, &event);
172 return gst_ebml_read_use_event (ebml, event);
176 * Return: the amount of levels in the hierarchy that the
177 * current element lies higher than the previous one.
178 * The opposite isn't done - that's auto-done using master
183 gst_ebml_read_element_level_up (GstEbmlRead * ebml)
186 guint64 pos = gst_bytestream_tell (ebml->bs);
188 while (ebml->level != NULL) {
189 GList *last = g_list_last (ebml->level);
190 GstEbmlLevel *level = last->data;
192 if (pos >= level->start + level->length) {
193 ebml->level = g_list_remove (ebml->level, level);
204 * Read: the element content data ID.
205 * Return: the number of bytes read or -1 on error.
209 gst_ebml_read_element_id (GstEbmlRead * ebml, guint32 * id, guint * level_up)
212 gint len_mask = 0x80, read = 1, n = 1;
215 while (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
216 if (!gst_ebml_read_handle_event (ebml))
220 while (read <= 4 && !(total & len_mask)) {
225 guint64 pos = gst_bytestream_tell (ebml->bs);
227 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
228 ("Invalid EBML ID size tag (0x%x) at position %llu (0x%llx)",
233 while (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
234 if (!gst_ebml_read_handle_event (ebml))
238 total = (total << 8) | data[n++];
244 *level_up = gst_ebml_read_element_level_up (ebml);
250 * Read: element content length.
251 * Return: the number of bytes read or -1 on error.
255 gst_ebml_read_element_length (GstEbmlRead * ebml, guint64 * length)
258 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
261 while (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
262 if (!gst_ebml_read_handle_event (ebml))
266 while (read <= 8 && !(total & len_mask)) {
271 guint64 pos = gst_bytestream_tell (ebml->bs);
273 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
274 ("Invalid EBML length size tag (0x%x) at position %llu (0x%llx)",
279 if ((total &= (len_mask - 1)) == len_mask - 1)
281 while (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
282 if (!gst_ebml_read_handle_event (ebml))
288 total = (total << 8) | data[n];
293 *length = G_MAXUINT64;
301 * Read: the actual data.
302 * Return: the data, as a GstBuffer.
306 gst_ebml_read_element_data (GstEbmlRead * ebml, guint64 length)
308 GstBuffer *buf = NULL;
310 if (gst_bytestream_peek (ebml->bs, &buf, length) != length) {
311 if (!gst_ebml_read_handle_event (ebml))
315 gst_bytestream_flush_fast (ebml->bs, length);
321 * Return: the ID of the next element.
322 * Level_up contains the amount of levels that this
323 * next element lies higher than the previous one.
327 gst_ebml_peek_id (GstEbmlRead * ebml, guint * level_up)
333 if (gst_ebml_read_element_id (ebml, &id, level_up) <= 0)
340 * Seek to a given offset.
344 gst_ebml_read_seek (GstEbmlRead * ebml, guint64 offset)
347 GstEvent *event = NULL;
350 /* first, flush remaining buffers */
351 gst_bytestream_get_status (ebml->bs, &remaining, &event);
353 GST_WARNING ("Unexpected event before seek");
354 if (!gst_ebml_read_use_event (ebml, event))
359 gst_bytestream_flush_fast (ebml->bs, remaining);
362 if (!gst_bytestream_seek (ebml->bs, offset, GST_SEEK_METHOD_SET)) {
363 GST_ELEMENT_ERROR (ebml, RESOURCE, SEEK, (NULL),
364 ("Seek to position %llu (0x%llx) failed", offset, offset));
369 /* and now, peek a new byte. This will fail because there's a
370 * pending event. Then, take the event and return it. */
371 if (gst_bytestream_peek_bytes (ebml->bs, &data, 1)) {
372 GST_WARNING ("Unexpected data after seek - this means seek failed");
376 /* get the discont event and return */
377 gst_bytestream_get_status (ebml->bs, &remaining, &event);
379 GST_WARNING ("No discontinuity event after seek - seek failed");
381 } else if (GST_EVENT_TYPE (event) != GST_EVENT_DISCONTINUOUS) {
382 if (!gst_ebml_read_use_event (ebml, event))
392 * Skip the next element.
396 gst_ebml_read_skip (GstEbmlRead * ebml)
399 guint32 id, remaining;
403 if ((bytes = gst_ebml_read_element_id (ebml, &id, NULL)) < 0)
405 gst_bytestream_flush_fast (ebml->bs, bytes);
407 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
409 gst_bytestream_flush_fast (ebml->bs, bytes);
411 /* do we have enough bytes left to skip? */
412 gst_bytestream_get_status (ebml->bs, &remaining, &event);
414 g_warning ("Unexpected event before skip");
415 if (!gst_ebml_read_use_event (ebml, event))
419 if (remaining >= length)
420 return gst_bytestream_flush (ebml->bs, length);
422 if (!(event = gst_ebml_read_seek (ebml,
423 gst_bytestream_tell (ebml->bs) + length))) {
427 gst_event_unref (event);
433 * Read the next element as a GstBuffer (binary).
437 gst_ebml_read_buffer (GstEbmlRead * ebml, guint32 * id, GstBuffer ** buf)
442 if ((bytes = gst_ebml_read_element_id (ebml, id, NULL)) < 0)
444 gst_bytestream_flush_fast (ebml->bs, bytes);
446 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
448 gst_bytestream_flush_fast (ebml->bs, bytes);
450 return ((*buf = gst_ebml_read_element_data (ebml, length)) != NULL);
454 * Read the next element as an unsigned int.
458 gst_ebml_read_uint (GstEbmlRead * ebml, guint32 * id, guint64 * num)
464 if (!gst_ebml_read_buffer (ebml, id, &buf))
467 data = GST_BUFFER_DATA (buf);
468 size = GST_BUFFER_SIZE (buf);
469 if (size < 1 || size > 8) {
470 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
471 ("Invalid integer element size %d at position %llu (0x%llu)",
472 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf)));
473 gst_buffer_unref (buf);
478 *num = (*num << 8) | data[GST_BUFFER_SIZE (buf) - size];
482 gst_buffer_unref (buf);
488 * Read the next element as a signed int.
492 gst_ebml_read_sint (GstEbmlRead * ebml, guint32 * id, gint64 * num)
496 guint size, negative = 0, n = 0;
498 if (!gst_ebml_read_buffer (ebml, id, &buf))
501 data = GST_BUFFER_DATA (buf);
502 size = GST_BUFFER_SIZE (buf);
503 if (size < 1 || size > 8) {
504 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
505 ("Invalid integer element size %d at position %llu (0x%llx)",
506 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf)));
507 gst_buffer_unref (buf);
510 if (data[0] & 0x80) {
516 *num = (*num << 8) | data[n++];
521 *num = *num - (1LL << ((8 * size) - 1));
524 gst_buffer_unref (buf);
530 * Read the next element as a float.
534 gst_ebml_read_float (GstEbmlRead * ebml, guint32 * id, gdouble * num)
540 if (!gst_ebml_read_buffer (ebml, id, &buf))
543 data = GST_BUFFER_DATA (buf);
544 size = GST_BUFFER_SIZE (buf);
546 if (size != 4 && size != 8 && size != 10) {
547 GST_ELEMENT_ERROR (ebml, STREAM, DEMUX, (NULL),
548 ("Invalid float element size %d at position %llu (0x%llx)",
549 size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf)));
550 gst_buffer_unref (buf);
555 GST_ELEMENT_ERROR (ebml, CORE, NOT_IMPLEMENTED, (NULL),
556 ("FIXME! 10-byte floats unimplemented"));
557 gst_buffer_unref (buf);
564 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
565 f = *(gfloat *) data;
568 ((guint8 *) & f)[size - 1] = data[4 - size];
577 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
578 d = *(gdouble *) data;
581 ((guint8 *) & d)[size - 1] = data[8 - size];
589 gst_buffer_unref (buf);
595 * Read the next element as an ASCII string.
599 gst_ebml_read_ascii (GstEbmlRead * ebml, guint32 * id, gchar ** str)
603 if (!gst_ebml_read_buffer (ebml, id, &buf))
606 *str = g_malloc (GST_BUFFER_SIZE (buf) + 1);
607 memcpy (*str, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
608 (*str)[GST_BUFFER_SIZE (buf)] = '\0';
610 gst_buffer_unref (buf);
616 * Read the next element as a UTF-8 string.
620 gst_ebml_read_utf8 (GstEbmlRead * ebml, guint32 * id, gchar ** str)
622 return gst_ebml_read_ascii (ebml, id, str);
626 * Read the next element as a date.
627 * Returns the seconds since the unix epoch.
631 gst_ebml_read_date (GstEbmlRead * ebml, guint32 * id, gint64 * date)
634 gboolean res = gst_ebml_read_sint (ebml, id, &ebml_date);
636 *date = (ebml_date / GST_SECOND) + GST_EBML_DATE_OFFSET;
641 * Read the next element, but only the header. The contents
642 * are supposed to be sub-elements which can be read separately.
646 gst_ebml_read_master (GstEbmlRead * ebml, guint32 * id)
652 if ((bytes = gst_ebml_read_element_id (ebml, id, NULL)) < 0)
654 gst_bytestream_flush_fast (ebml->bs, bytes);
656 if ((bytes = gst_ebml_read_element_length (ebml, &length)) < 0)
658 gst_bytestream_flush_fast (ebml->bs, bytes);
661 level = g_new (GstEbmlLevel, 1);
662 level->start = gst_bytestream_tell (ebml->bs);
663 level->length = length;
664 ebml->level = g_list_append (ebml->level, level);
670 * Read the next element as binary data.
674 gst_ebml_read_binary (GstEbmlRead * ebml,
675 guint32 * id, guint8 ** binary, guint64 * length)
679 if (!gst_ebml_read_buffer (ebml, id, &buf))
682 *length = GST_BUFFER_SIZE (buf);
683 *binary = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
685 gst_buffer_unref (buf);
691 * Read an EBML header.
695 gst_ebml_read_header (GstEbmlRead * ebml, gchar ** doctype, guint * version)
697 /* this function is the first to be called */
707 if (!(id = gst_ebml_peek_id (ebml, &level_up)))
709 if (level_up != 0 || id != GST_EBML_ID_HEADER) {
710 GST_ELEMENT_ERROR (ebml, STREAM, WRONG_TYPE, (NULL), (NULL));
713 if (!gst_ebml_read_master (ebml, &id))
715 g_assert (id == GST_EBML_ID_HEADER);
718 if (!(id = gst_ebml_peek_id (ebml, &level_up)))
726 /* is our read version uptodate? */
727 case GST_EBML_ID_EBMLREADVERSION:{
730 if (!gst_ebml_read_uint (ebml, &id, &num))
732 g_assert (id == GST_EBML_ID_EBMLREADVERSION);
733 if (num != GST_EBML_VERSION)
738 /* we only handle 8 byte lengths at max */
739 case GST_EBML_ID_EBMLMAXSIZELENGTH:{
742 if (!gst_ebml_read_uint (ebml, &id, &num))
744 g_assert (id == GST_EBML_ID_EBMLMAXSIZELENGTH);
745 if (num != sizeof (guint64))
750 /* we handle 4 byte IDs at max */
751 case GST_EBML_ID_EBMLMAXIDLENGTH:{
754 if (!gst_ebml_read_uint (ebml, &id, &num))
756 g_assert (id == GST_EBML_ID_EBMLMAXIDLENGTH);
757 if (num != sizeof (guint32))
762 case GST_EBML_ID_DOCTYPE:{
765 if (!gst_ebml_read_ascii (ebml, &id, &text))
767 g_assert (id == GST_EBML_ID_DOCTYPE);
777 case GST_EBML_ID_DOCTYPEREADVERSION:{
780 if (!gst_ebml_read_uint (ebml, &id, &num))
782 g_assert (id == GST_EBML_ID_DOCTYPEREADVERSION);
789 GST_WARNING ("Unknown data type 0x%x in EBML header (ignored)", id);
792 /* we ignore these two, as they don't tell us anything we care about */
793 case GST_EBML_ID_VOID:
794 case GST_EBML_ID_EBMLVERSION:
795 case GST_EBML_ID_DOCTYPEVERSION:
796 if (!gst_ebml_read_skip (ebml))