1 /* GStreamer bz2 decoder
2 * Copyright (C) 2006 Lutz Müller <lutz topfrose de>
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.
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.
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.
22 #include "gstbz2dec.h"
24 #include <gst/base/gsttypefindhelper.h>
29 GST_DEBUG_CATEGORY_STATIC (bz2dec_debug);
30 #define GST_CAT_DEFAULT bz2dec_debug
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,
47 guint first_buffer_size;
55 struct _GstBz2decClass
57 GstElementClass parent_class;
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);
64 #define DEFAULT_FIRST_BUFFER_SIZE 1024
65 #define DEFAULT_BUFFER_SIZE 1024
70 PROP_FIRST_BUFFER_SIZE,
75 gst_bz2dec_decompress_end (GstBz2dec * b)
77 g_return_if_fail (GST_IS_BZ2DEC (b));
80 BZ2_bzDecompressEnd (&b->stream);
81 memset (&b->stream, 0, sizeof (b->stream));
87 gst_bz2dec_decompress_init (GstBz2dec * b)
89 g_return_if_fail (GST_IS_BZ2DEC (b));
91 gst_bz2dec_decompress_end (b);
93 switch (BZ2_bzDecompressInit (&b->stream, 0, 0)) {
99 GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL),
100 ("Failed to start decompression."));
106 gst_bz2dec_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
108 GstFlowReturn flow = GST_FLOW_OK;
112 GstMapInfo map = GST_MAP_INFO_INIT, omap;
114 b = GST_BZ2DEC (parent);
119 gst_buffer_map (in, &map, GST_MAP_READ);
120 b->stream.next_in = (char *) map.data;
121 b->stream.avail_in = map.size;
126 /* Create the output buffer */
128 gst_buffer_new_and_alloc (b->offset ? b->buffer_size : b->
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))
140 if (b->stream.avail_out >= gst_buffer_get_size (out)) {
141 gst_buffer_unref (out);
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);
148 /* Configure source pad (if necessary) */
150 GstCaps *caps = NULL;
152 caps = gst_type_find_helper_for_buffer (GST_OBJECT (b), out, NULL);
154 gst_pad_set_caps (b->src, caps);
155 gst_pad_use_fixed_caps (b->src);
156 gst_caps_unref (caps);
158 /* FIXME: shouldn't we queue output buffers until we have a type? */
163 n = gst_buffer_get_size (out);
164 flow = gst_pad_push (b->src, out);
165 if (flow != GST_FLOW_OK)
168 } while (r != BZ_STREAM_END);
172 gst_buffer_unmap (in, &map);
173 gst_buffer_unref (in);
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;
188 GST_ELEMENT_ERROR (b, LIBRARY, FAILED, (NULL), ("Decompressor not ready."));
189 flow = GST_FLOW_FLUSHING;
195 gst_bz2dec_init (GstBz2dec * b)
197 b->first_buffer_size = DEFAULT_FIRST_BUFFER_SIZE;
198 b->buffer_size = DEFAULT_BUFFER_SIZE;
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);
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);
208 gst_bz2dec_decompress_init (b);
212 gst_bz2dec_finalize (GObject * object)
214 GstBz2dec *b = GST_BZ2DEC (object);
216 gst_bz2dec_decompress_end (b);
218 G_OBJECT_CLASS (parent_class)->finalize (object);
222 gst_bz2dec_get_property (GObject * object, guint prop_id,
223 GValue * value, GParamSpec * pspec)
225 GstBz2dec *b = GST_BZ2DEC (object);
228 case PROP_BUFFER_SIZE:
229 g_value_set_uint (value, b->buffer_size);
231 case PROP_FIRST_BUFFER_SIZE:
232 g_value_set_uint (value, b->first_buffer_size);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 gst_bz2dec_set_property (GObject * object, guint prop_id,
241 const GValue * value, GParamSpec * pspec)
243 GstBz2dec *b = GST_BZ2DEC (object);
246 case PROP_BUFFER_SIZE:
247 b->buffer_size = g_value_get_uint (value);
249 case PROP_FIRST_BUFFER_SIZE:
250 b->first_buffer_size = g_value_get_uint (value);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 static GstStateChangeReturn
258 gst_bz2dec_change_state (GstElement * element, GstStateChange transition)
260 GstBz2dec *b = GST_BZ2DEC (element);
261 GstStateChangeReturn ret;
263 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
264 if (ret != GST_STATE_CHANGE_SUCCESS)
267 switch (transition) {
268 case GST_STATE_CHANGE_PAUSED_TO_READY:
269 gst_bz2dec_decompress_init (b);
278 gst_bz2dec_class_init (GstBz2decClass * klass)
280 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
281 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
283 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bz2dec_change_state);
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;
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));
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>");
306 GST_DEBUG_CATEGORY_INIT (bz2dec_debug, "bz2dec", 0, "BZ2 decompressor");