Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / matroska / ebml-read.h
1 /* GStreamer EBML I/O
2  * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * ebml-read.c: read EBML data from file/stream
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifndef __GST_EBML_READ_H__
23 #define __GST_EBML_READ_H__
24
25 #include <gst/gst.h>
26 #include <gst/base/gstbytereader.h>
27
28 G_BEGIN_DECLS
29
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))
42
43 GST_DEBUG_CATEGORY_EXTERN (ebmlread_debug);
44
45 /* custom flow return code */
46 #define  GST_FLOW_PARSE  GST_FLOW_CUSTOM_ERROR
47
48 typedef struct _GstEbmlMaster {
49   guint64       offset;
50   GstByteReader br;
51 } GstEbmlMaster;
52
53 typedef struct _GstEbmlRead {
54   GstElement *el;
55
56   GstBuffer *buf;
57   guint64 offset;
58
59   GArray *readers;
60 } GstEbmlRead;
61
62 typedef GstFlowReturn (*GstPeekData) (gpointer * context, guint peek, const guint8 ** data);
63
64 /* returns UNEXPECTED if not enough data */
65 GstFlowReturn gst_ebml_peek_id_length    (guint32 * _id, guint64 * _length,
66                                           guint * _needed,
67                                           GstPeekData peek, gpointer * ctx,
68                                           GstElement * el, guint64 offset);
69
70 void          gst_ebml_read_init         (GstEbmlRead * ebml,
71                                           GstElement * el, GstBuffer * buf,
72                                           guint64 offset);
73
74 void          gst_ebml_read_clear        (GstEbmlRead * ebml);
75
76 GstFlowReturn gst_ebml_peek_id           (GstEbmlRead * ebml, guint32 * id);
77
78 /* return _PARSE if not enough data to read what is needed, _ERROR or _OK */
79 GstFlowReturn gst_ebml_read_skip         (GstEbmlRead *ebml);
80
81 GstFlowReturn gst_ebml_read_buffer       (GstEbmlRead *ebml,
82                                           guint32     *id,
83                                           GstBuffer  **buf);
84
85 GstFlowReturn gst_ebml_read_uint         (GstEbmlRead *ebml,
86                                           guint32     *id,
87                                           guint64     *num);
88
89 GstFlowReturn gst_ebml_read_sint         (GstEbmlRead *ebml,
90                                           guint32     *id,
91                                           gint64      *num);
92
93 GstFlowReturn gst_ebml_read_float        (GstEbmlRead *ebml,
94                                           guint32     *id,
95                                           gdouble     *num);
96
97 GstFlowReturn gst_ebml_read_ascii        (GstEbmlRead *ebml,
98                                           guint32     *id,
99                                           gchar      **str);
100
101 GstFlowReturn gst_ebml_read_utf8         (GstEbmlRead *ebml,
102                                           guint32     *id,
103                                           gchar      **str);
104
105 GstFlowReturn gst_ebml_read_date         (GstEbmlRead *ebml,
106                                           guint32     *id,
107                                           gint64      *date);
108
109 GstFlowReturn gst_ebml_read_master       (GstEbmlRead *ebml,
110                                           guint32     *id);
111
112 GstFlowReturn gst_ebml_read_pop_master   (GstEbmlRead *ebml);
113
114 GstFlowReturn gst_ebml_read_binary       (GstEbmlRead *ebml,
115                                           guint32     *id,
116                                           guint8     **binary,
117                                           guint64     *length);
118
119 GstFlowReturn gst_ebml_read_header       (GstEbmlRead *read,
120                                           gchar      **doctype,
121                                           guint       *version);
122
123 /* Returns current (absolute) position of Ebml parser,
124  * i.e. taking into account offset provided at init */
125 static inline guint64
126 gst_ebml_read_get_pos (GstEbmlRead * ebml)
127 {
128   GstEbmlMaster *m;
129
130   g_return_val_if_fail (ebml->readers, 0);
131   g_return_val_if_fail (ebml->readers->len, 0);
132
133   m = &(g_array_index (ebml->readers, GstEbmlMaster, ebml->readers->len - 1));
134   return m->offset + gst_byte_reader_get_pos (&m->br);
135 }
136
137 /* Returns starting offset of Ebml parser */
138 static inline guint64
139 gst_ebml_read_get_offset (GstEbmlRead * ebml)
140 {
141   return ebml->offset;
142 }
143
144 static inline GstByteReader *
145 gst_ebml_read_br (GstEbmlRead * ebml)
146 {
147   g_return_val_if_fail (ebml->readers, NULL);
148   g_return_val_if_fail (ebml->readers->len, NULL);
149
150   return &(g_array_index (ebml->readers,
151           GstEbmlMaster, ebml->readers->len - 1).br);
152 }
153
154 static inline gboolean
155 gst_ebml_read_has_remaining (GstEbmlRead * ebml, guint64 bytes_needed,
156     gboolean auto_pop)
157 {
158   gboolean res;
159
160   res = (gst_byte_reader_get_remaining (gst_ebml_read_br (ebml)) >= bytes_needed);
161   if (G_LIKELY (!res && auto_pop)) {
162     gst_ebml_read_pop_master (ebml);
163   }
164
165   return G_LIKELY (res);
166 }
167
168 G_END_DECLS
169
170 #endif /* __GST_EBML_READ_H__ */