upload tizen1.0 source
[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   GstPad *sinkpad;
56   GstBuffer *buf;
57   guint64 offset;
58
59   GArray *readers;
60 } GstEbmlRead;
61
62 typedef const guint8 * (*GstPeekData) (gpointer * context, guint peek);
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 GstFlowReturn gst_ebml_read_seek         (GstEbmlRead *ebml,
79                                           guint64      offset);
80
81 gint64        gst_ebml_read_get_length   (GstEbmlRead *ebml);
82
83 /* return _PARSE if not enough data to read what is needed, _ERROR or _OK */
84 GstFlowReturn gst_ebml_read_skip         (GstEbmlRead *ebml);
85
86 GstFlowReturn gst_ebml_read_buffer       (GstEbmlRead *ebml,
87                                           guint32     *id,
88                                           GstBuffer  **buf);
89
90 GstFlowReturn gst_ebml_read_uint         (GstEbmlRead *ebml,
91                                           guint32     *id,
92                                           guint64     *num);
93
94 GstFlowReturn gst_ebml_read_sint         (GstEbmlRead *ebml,
95                                           guint32     *id,
96                                           gint64      *num);
97
98 GstFlowReturn gst_ebml_read_float        (GstEbmlRead *ebml,
99                                           guint32     *id,
100                                           gdouble     *num);
101
102 GstFlowReturn gst_ebml_read_ascii        (GstEbmlRead *ebml,
103                                           guint32     *id,
104                                           gchar      **str);
105
106 GstFlowReturn gst_ebml_read_utf8         (GstEbmlRead *ebml,
107                                           guint32     *id,
108                                           gchar      **str);
109
110 GstFlowReturn gst_ebml_read_date         (GstEbmlRead *ebml,
111                                           guint32     *id,
112                                           gint64      *date);
113
114 GstFlowReturn gst_ebml_read_master       (GstEbmlRead *ebml,
115                                           guint32     *id);
116
117 GstFlowReturn gst_ebml_read_pop_master   (GstEbmlRead *ebml);
118
119 GstFlowReturn gst_ebml_read_binary       (GstEbmlRead *ebml,
120                                           guint32     *id,
121                                           guint8     **binary,
122                                           guint64     *length);
123
124 GstFlowReturn gst_ebml_read_header       (GstEbmlRead *read,
125                                           gchar      **doctype,
126                                           guint       *version);
127
128 /* Returns current (absolute) position of Ebml parser,
129  * i.e. taking into account offset provided at init */
130 static inline guint64
131 gst_ebml_read_get_pos (GstEbmlRead * ebml)
132 {
133   GstEbmlMaster *m;
134
135   g_return_val_if_fail (ebml->readers, 0);
136   g_return_val_if_fail (ebml->readers->len, 0);
137
138   m = &(g_array_index (ebml->readers, GstEbmlMaster, ebml->readers->len - 1));
139   return m->offset + gst_byte_reader_get_pos (&m->br);
140 }
141
142 /* Returns starting offset of Ebml parser */
143 static inline guint64
144 gst_ebml_read_get_offset (GstEbmlRead * ebml)
145 {
146   return ebml->offset;
147 }
148
149 static inline GstByteReader *
150 gst_ebml_read_br (GstEbmlRead * ebml)
151 {
152   g_return_val_if_fail (ebml->readers, NULL);
153   g_return_val_if_fail (ebml->readers->len, NULL);
154
155   return &(g_array_index (ebml->readers,
156           GstEbmlMaster, ebml->readers->len - 1).br);
157 }
158
159 static inline gboolean
160 gst_ebml_read_has_remaining (GstEbmlRead * ebml, guint64 bytes_needed,
161     gboolean auto_pop)
162 {
163   gboolean res;
164
165   res = (gst_byte_reader_get_remaining (gst_ebml_read_br (ebml)) >= bytes_needed);
166   if (G_LIKELY (!res && auto_pop)) {
167     gst_ebml_read_pop_master (ebml);
168   }
169
170   return G_LIKELY (res);
171 }
172
173 G_END_DECLS
174
175 #endif /* __GST_EBML_READ_H__ */