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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #ifndef __GST_EBML_READ_H__
23 #define __GST_EBML_READ_H__
26 #include <gst/base/gstbytereader.h>
30 #define GST_TYPE_EBML_READ \
31 (gst_ebml_read_get_type ())
32 #define GST_EBML_READ(obj) \
33 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_EBML_READ, GstEbmlRead))
34 #define GST_EBML_READ_CLASS(klass) \
35 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_EBML_READ, GstEbmlReadClass))
36 #define GST_IS_EBML_READ(obj) \
37 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_EBML_READ))
38 #define GST_IS_EBML_READ_CLASS(klass) \
39 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_EBML_READ))
40 #define GST_EBML_READ_GET_CLASS(obj) \
41 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_EBML_READ, GstEbmlReadClass))
43 GST_DEBUG_CATEGORY_EXTERN (ebmlread_debug);
45 /* custom flow return code */
46 #define GST_FLOW_PARSE GST_FLOW_CUSTOM_ERROR
48 typedef struct _GstEbmlMaster {
53 typedef struct _GstEbmlRead {
63 typedef GstFlowReturn (*GstPeekData) (gpointer * context, guint peek, const guint8 ** data);
65 /* returns UNEXPECTED if not enough data */
66 GstFlowReturn gst_ebml_peek_id_length (guint32 * _id, guint64 * _length,
68 GstPeekData peek, gpointer * ctx,
69 GstElement * el, guint64 offset);
71 void gst_ebml_read_init (GstEbmlRead * ebml,
72 GstElement * el, GstBuffer * buf,
75 void gst_ebml_read_clear (GstEbmlRead * ebml);
77 GstFlowReturn gst_ebml_peek_id (GstEbmlRead * ebml, guint32 * id);
79 /* return _PARSE if not enough data to read what is needed, _ERROR or _OK */
80 GstFlowReturn gst_ebml_read_skip (GstEbmlRead *ebml);
82 GstFlowReturn gst_ebml_read_buffer (GstEbmlRead *ebml,
86 GstFlowReturn gst_ebml_read_uint (GstEbmlRead *ebml,
90 GstFlowReturn gst_ebml_read_sint (GstEbmlRead *ebml,
94 GstFlowReturn gst_ebml_read_float (GstEbmlRead *ebml,
98 GstFlowReturn gst_ebml_read_ascii (GstEbmlRead *ebml,
102 GstFlowReturn gst_ebml_read_utf8 (GstEbmlRead *ebml,
106 GstFlowReturn gst_ebml_read_date (GstEbmlRead *ebml,
110 GstFlowReturn gst_ebml_read_master (GstEbmlRead *ebml,
113 GstFlowReturn gst_ebml_read_pop_master (GstEbmlRead *ebml);
115 GstFlowReturn gst_ebml_read_binary (GstEbmlRead *ebml,
120 GstFlowReturn gst_ebml_read_header (GstEbmlRead *read,
124 /* Returns current (absolute) position of Ebml parser,
125 * i.e. taking into account offset provided at init */
126 static inline guint64
127 gst_ebml_read_get_pos (GstEbmlRead * ebml)
131 g_return_val_if_fail (ebml->readers, 0);
132 g_return_val_if_fail (ebml->readers->len, 0);
134 m = &(g_array_index (ebml->readers, GstEbmlMaster, ebml->readers->len - 1));
135 return m->offset + gst_byte_reader_get_pos (&m->br);
138 /* Returns starting offset of Ebml parser */
139 static inline guint64
140 gst_ebml_read_get_offset (GstEbmlRead * ebml)
145 static inline GstByteReader *
146 gst_ebml_read_br (GstEbmlRead * ebml)
148 g_return_val_if_fail (ebml->readers, NULL);
149 g_return_val_if_fail (ebml->readers->len, NULL);
151 return &(g_array_index (ebml->readers,
152 GstEbmlMaster, ebml->readers->len - 1).br);
155 static inline gboolean
156 gst_ebml_read_has_remaining (GstEbmlRead * ebml, guint64 bytes_needed,
161 res = (gst_byte_reader_get_remaining (gst_ebml_read_br (ebml)) >= bytes_needed);
162 if (G_LIKELY (!res && auto_pop)) {
163 gst_ebml_read_pop_master (ebml);
166 return G_LIKELY (res);
171 #endif /* __GST_EBML_READ_H__ */