Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / ext / bz2 / gstbz2dec.c
1 /* GStreamer bz2 decoder
2  * Copyright (C) 2006 Lutz Müller <lutz topfrose de>
3
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #include "gstbz2dec.h"
23
24 #include <gst/base/gsttypefindhelper.h>
25
26 #include <bzlib.h>
27 #include <string.h>
28
29 GST_DEBUG_CATEGORY_STATIC (bz2dec_debug);
30 #define GST_CAT_DEFAULT bz2dec_debug
31
32 static GstStaticPadTemplate sink_template =
33 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("application/x-bzip"));
35 static GstStaticPadTemplate src_template =
36 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
37     GST_STATIC_CAPS_ANY);
38
39 struct _GstBz2dec
40 {
41   GstElement parent;
42
43   GstPad *sink;
44   GstPad *src;
45
46   /* Properties */
47   guint first_buffer_size;
48   guint buffer_size;
49
50   gboolean ready;
51   bz_stream stream;
52   guint64 offset;
53 };
54
55 struct _GstBz2decClass
56 {
57   GstElementClass parent_class;
58 };
59
60 #define gst_bz2dec_parent_class parent_class
61 G_DEFINE_TYPE (GstBz2dec, gst_bz2dec, GST_TYPE_ELEMENT);
62 GST_ELEMENT_REGISTER_DEFINE (bz2dec, "bz2dec", GST_RANK_NONE, GST_TYPE_BZ2DEC);
63
64 #define DEFAULT_FIRST_BUFFER_SIZE 1024
65 #define DEFAULT_BUFFER_SIZE 1024
66
67 enum
68 {
69   PROP_0,
70   PROP_FIRST_BUFFER_SIZE,
71   PROP_BUFFER_SIZE
72 };
73
74 static void
75 gst_bz2dec_decompress_end (GstBz2dec * b)
76 {
77   g_return_if_fail (GST_IS_BZ2DEC (b));
78
79   if (b->ready) {
80     BZ2_bzDecompressEnd (&b->stream);
81     memset (&b->stream, 0, sizeof (b->stream));
82     b->ready = FALSE;
83   }
84 }
85
86 static void
87 gst_bz2dec_decompress_init (GstBz2dec * b)
88 {
89   g_return_if_fail (GST_IS_BZ2DEC (b));
90
91   gst_bz2dec_decompress_end (b);
92   b->offset = 0;
93   switch (BZ2_bzDecompressInit (&b->stream, 0, 0)) {
94     case BZ_OK:
95       b->ready = TRUE;
96       return;
97     default:
98       b->ready = FALSE;
99       GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL),
100           ("Failed to start decompression."));
101       return;
102   }
103 }
104
105 static GstFlowReturn
106 gst_bz2dec_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
107 {
108   GstFlowReturn flow = GST_FLOW_OK;
109   GstBuffer *out;
110   GstBz2dec *b;
111   int r = BZ_OK;
112   GstMapInfo map = GST_MAP_INFO_INIT, omap;
113
114   b = GST_BZ2DEC (parent);
115
116   if (!b->ready)
117     goto not_ready;
118
119   gst_buffer_map (in, &map, GST_MAP_READ);
120   b->stream.next_in = (char *) map.data;
121   b->stream.avail_in = map.size;
122
123   do {
124     guint n;
125
126     /* Create the output buffer */
127     out =
128         gst_buffer_new_and_alloc (b->offset ? b->buffer_size : b->
129         first_buffer_size);
130
131     /* Decode */
132     gst_buffer_map (out, &omap, GST_MAP_WRITE);
133     b->stream.next_out = (char *) omap.data;
134     b->stream.avail_out = omap.size;
135     r = BZ2_bzDecompress (&b->stream);
136     gst_buffer_unmap (out, &omap);
137     if ((r != BZ_OK) && (r != BZ_STREAM_END))
138       goto decode_failed;
139
140     if (b->stream.avail_out >= gst_buffer_get_size (out)) {
141       gst_buffer_unref (out);
142       break;
143     }
144     gst_buffer_resize (out, 0, gst_buffer_get_size (out) - b->stream.avail_out);
145     GST_BUFFER_OFFSET (out) =
146         b->stream.total_out_lo32 - gst_buffer_get_size (out);
147
148     /* Configure source pad (if necessary) */
149     if (!b->offset) {
150       GstCaps *caps = NULL;
151
152       caps = gst_type_find_helper_for_buffer (GST_OBJECT (b), out, NULL);
153       if (caps) {
154         gst_pad_set_caps (b->src, caps);
155         gst_pad_use_fixed_caps (b->src);
156         gst_caps_unref (caps);
157       } else {
158         /* FIXME: shouldn't we queue output buffers until we have a type? */
159       }
160     }
161
162     /* Push data */
163     n = gst_buffer_get_size (out);
164     flow = gst_pad_push (b->src, out);
165     if (flow != GST_FLOW_OK)
166       break;
167     b->offset += n;
168   } while (r != BZ_STREAM_END);
169
170 done:
171
172   gst_buffer_unmap (in, &map);
173   gst_buffer_unref (in);
174   return flow;
175
176 /* ERRORS */
177 decode_failed:
178   {
179     GST_ELEMENT_ERROR (b, STREAM, DECODE, (NULL),
180         ("Failed to decompress data (error code %i).", r));
181     gst_bz2dec_decompress_init (b);
182     gst_buffer_unref (out);
183     flow = GST_FLOW_ERROR;
184     goto done;
185   }
186 not_ready:
187   {
188     GST_ELEMENT_ERROR (b, LIBRARY, FAILED, (NULL), ("Decompressor not ready."));
189     flow = GST_FLOW_FLUSHING;
190     goto done;
191   }
192 }
193
194 static void
195 gst_bz2dec_init (GstBz2dec * b)
196 {
197   b->first_buffer_size = DEFAULT_FIRST_BUFFER_SIZE;
198   b->buffer_size = DEFAULT_BUFFER_SIZE;
199
200   b->sink = gst_pad_new_from_static_template (&sink_template, "sink");
201   gst_pad_set_chain_function (b->sink, GST_DEBUG_FUNCPTR (gst_bz2dec_chain));
202   gst_element_add_pad (GST_ELEMENT (b), b->sink);
203
204   b->src = gst_pad_new_from_static_template (&src_template, "src");
205   gst_element_add_pad (GST_ELEMENT (b), b->src);
206   gst_pad_use_fixed_caps (b->src);
207
208   gst_bz2dec_decompress_init (b);
209 }
210
211 static void
212 gst_bz2dec_finalize (GObject * object)
213 {
214   GstBz2dec *b = GST_BZ2DEC (object);
215
216   gst_bz2dec_decompress_end (b);
217
218   G_OBJECT_CLASS (parent_class)->finalize (object);
219 }
220
221 static void
222 gst_bz2dec_get_property (GObject * object, guint prop_id,
223     GValue * value, GParamSpec * pspec)
224 {
225   GstBz2dec *b = GST_BZ2DEC (object);
226
227   switch (prop_id) {
228     case PROP_BUFFER_SIZE:
229       g_value_set_uint (value, b->buffer_size);
230       break;
231     case PROP_FIRST_BUFFER_SIZE:
232       g_value_set_uint (value, b->first_buffer_size);
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236   }
237 }
238
239 static void
240 gst_bz2dec_set_property (GObject * object, guint prop_id,
241     const GValue * value, GParamSpec * pspec)
242 {
243   GstBz2dec *b = GST_BZ2DEC (object);
244
245   switch (prop_id) {
246     case PROP_BUFFER_SIZE:
247       b->buffer_size = g_value_get_uint (value);
248       break;
249     case PROP_FIRST_BUFFER_SIZE:
250       b->first_buffer_size = g_value_get_uint (value);
251       break;
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254   }
255 }
256
257 static GstStateChangeReturn
258 gst_bz2dec_change_state (GstElement * element, GstStateChange transition)
259 {
260   GstBz2dec *b = GST_BZ2DEC (element);
261   GstStateChangeReturn ret;
262
263   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
264   if (ret != GST_STATE_CHANGE_SUCCESS)
265     return ret;
266
267   switch (transition) {
268     case GST_STATE_CHANGE_PAUSED_TO_READY:
269       gst_bz2dec_decompress_init (b);
270       break;
271     default:
272       break;
273   }
274   return ret;
275 }
276
277 static void
278 gst_bz2dec_class_init (GstBz2decClass * klass)
279 {
280   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
281   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
282
283   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bz2dec_change_state);
284
285   gobject_class->finalize = gst_bz2dec_finalize;
286   gobject_class->get_property = gst_bz2dec_get_property;
287   gobject_class->set_property = gst_bz2dec_set_property;
288
289   g_object_class_install_property (G_OBJECT_CLASS (klass),
290       PROP_FIRST_BUFFER_SIZE, g_param_spec_uint ("first-buffer-size",
291           "Size of first buffer", "Size of first buffer (used to determine the "
292           "mime type of the uncompressed data)", 1, G_MAXUINT,
293           DEFAULT_FIRST_BUFFER_SIZE,
294           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
296       g_param_spec_uint ("buffer-size", "Buffer size", "Buffer size",
297           1, G_MAXUINT, DEFAULT_BUFFER_SIZE,
298           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299
300   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
301   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
302   gst_element_class_set_static_metadata (gstelement_class, "BZ2 decoder",
303       "Codec/Decoder", "Decodes compressed streams",
304       "Lutz Mueller <lutz@users.sourceforge.net>");
305
306   GST_DEBUG_CATEGORY_INIT (bz2dec_debug, "bz2dec", 0, "BZ2 decompressor");
307 }