1 /* GStreamer bz2 encoder
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 "gstbz2enc.h"
27 GST_DEBUG_CATEGORY_STATIC (bz2enc_debug);
28 #define GST_CAT_DEFAULT bz2enc_debug
30 static GstStaticPadTemplate sink_template =
31 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
32 GST_STATIC_CAPS ("ANY"));
33 static GstStaticPadTemplate src_template =
34 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
35 GST_STATIC_CAPS ("application/x-bzip"));
37 #define DEFAULT_BLOCK_SIZE 6
38 #define DEFAULT_BUFFER_SIZE 1024
63 struct _GstBz2encClass
65 GstElementClass parent_class;
68 #define gst_bz2enc_parent_class parent_class
69 G_DEFINE_TYPE (GstBz2enc, gst_bz2enc, GST_TYPE_ELEMENT);
70 GST_ELEMENT_REGISTER_DEFINE (bz2enc, "bz2enc", GST_RANK_NONE, GST_TYPE_BZ2ENC);
73 gst_bz2enc_compress_end (GstBz2enc * b)
75 g_return_if_fail (GST_IS_BZ2ENC (b));
78 BZ2_bzCompressEnd (&b->stream);
79 memset (&b->stream, 0, sizeof (b->stream));
85 gst_bz2enc_compress_init (GstBz2enc * b)
87 g_return_if_fail (GST_IS_BZ2ENC (b));
89 gst_bz2enc_compress_end (b);
91 switch (BZ2_bzCompressInit (&b->stream, b->block_size, 0, 0)) {
97 GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL),
98 ("Failed to start compression."));
104 gst_bz2enc_event (GstPad * pad, GstObject * parent, GstEvent * e)
109 b = GST_BZ2ENC (parent);
110 switch (GST_EVENT_TYPE (e)) {
112 GstFlowReturn flow = GST_FLOW_OK;
113 int r = BZ_FINISH_OK;
120 out = gst_buffer_new_and_alloc (b->buffer_size);
122 gst_buffer_map (out, &omap, GST_MAP_WRITE);
123 b->stream.next_out = (char *) omap.data;
124 b->stream.avail_out = omap.size;
125 r = BZ2_bzCompress (&b->stream, BZ_FINISH);
126 gst_buffer_unmap (out, &omap);
127 if ((r != BZ_FINISH_OK) && (r != BZ_STREAM_END)) {
128 GST_ELEMENT_ERROR (b, STREAM, ENCODE, (NULL),
129 ("Failed to finish to compress (error code %i).", r));
130 gst_buffer_unref (out);
134 n = gst_buffer_get_size (out);
135 if (b->stream.avail_out >= n) {
136 gst_buffer_unref (out);
140 gst_buffer_resize (out, 0, n - b->stream.avail_out);
141 n = gst_buffer_get_size (out);
142 GST_BUFFER_OFFSET (out) = b->stream.total_out_lo32 - n;
144 flow = gst_pad_push (b->src, out);
146 if (flow != GST_FLOW_OK) {
147 GST_DEBUG_OBJECT (b, "push on EOS failed: %s",
148 gst_flow_get_name (flow));
151 } while (r != BZ_STREAM_END);
153 ret = gst_pad_event_default (pad, parent, e);
155 if (r != BZ_STREAM_END || flow != GST_FLOW_OK)
158 gst_bz2enc_compress_init (b);
162 ret = gst_pad_event_default (pad, parent, e);
170 gst_bz2enc_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
172 GstFlowReturn flow = GST_FLOW_OK;
177 GstMapInfo map = GST_MAP_INFO_INIT, omap;
179 b = GST_BZ2ENC (parent);
184 gst_buffer_map (in, &map, GST_MAP_READ);
185 b->stream.next_in = (char *) map.data;
186 b->stream.avail_in = map.size;
187 while (b->stream.avail_in) {
188 out = gst_buffer_new_and_alloc (b->buffer_size);
190 gst_buffer_map (out, &omap, GST_MAP_WRITE);
191 b->stream.next_out = (char *) omap.data;
192 b->stream.avail_out = omap.size;
193 bz2_ret = BZ2_bzCompress (&b->stream, BZ_RUN);
194 gst_buffer_unmap (out, &omap);
195 if (bz2_ret != BZ_RUN_OK)
198 n = gst_buffer_get_size (out);
199 if (b->stream.avail_out >= n) {
200 gst_buffer_unref (out);
204 gst_buffer_resize (out, 0, n - b->stream.avail_out);
205 n = gst_buffer_get_size (out);
206 GST_BUFFER_OFFSET (out) = b->stream.total_out_lo32 - n;
208 flow = gst_pad_push (b->src, out);
210 if (flow != GST_FLOW_OK)
218 gst_buffer_unmap (in, &map);
219 gst_buffer_unref (in);
225 GST_ELEMENT_ERROR (b, LIBRARY, FAILED, (NULL), ("Compressor not ready."));
226 flow = GST_FLOW_FLUSHING;
231 GST_ELEMENT_ERROR (b, STREAM, ENCODE, (NULL),
232 ("Failed to compress data (error code %i)", bz2_ret));
233 gst_bz2enc_compress_init (b);
234 gst_buffer_unref (out);
235 flow = GST_FLOW_ERROR;
241 gst_bz2enc_init (GstBz2enc * b)
244 b->sink = gst_pad_new_from_static_template (&sink_template, "sink");
245 gst_pad_set_chain_function (b->sink, GST_DEBUG_FUNCPTR (gst_bz2enc_chain));
246 gst_pad_set_event_function (b->sink, GST_DEBUG_FUNCPTR (gst_bz2enc_event));
247 gst_element_add_pad (GST_ELEMENT (b), b->sink);
249 b->src = gst_pad_new_from_static_template (&src_template, "src");
250 gst_pad_set_caps (b->src, tmp = gst_static_pad_template_get_caps (&src_template));
251 gst_pad_use_fixed_caps (b->src);
252 gst_element_add_pad (GST_ELEMENT (b), b->src);
255 b->block_size = DEFAULT_BLOCK_SIZE;
256 b->buffer_size = DEFAULT_BUFFER_SIZE;
257 gst_bz2enc_compress_init (b);
261 gst_bz2enc_finalize (GObject * object)
263 GstBz2enc *b = GST_BZ2ENC (object);
265 gst_bz2enc_compress_end (b);
267 G_OBJECT_CLASS (parent_class)->finalize (object);
271 gst_bz2enc_get_property (GObject * object, guint prop_id,
272 GValue * value, GParamSpec * pspec)
274 GstBz2enc *b = GST_BZ2ENC (object);
277 case PROP_BLOCK_SIZE:
278 g_value_set_uint (value, b->block_size);
280 case PROP_BUFFER_SIZE:
281 g_value_set_uint (value, b->buffer_size);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 gst_bz2enc_set_property (GObject * object, guint prop_id,
290 const GValue * value, GParamSpec * pspec)
292 GstBz2enc *b = GST_BZ2ENC (object);
295 case PROP_BLOCK_SIZE:
296 b->block_size = g_value_get_uint (value);
297 gst_bz2enc_compress_init (b);
299 case PROP_BUFFER_SIZE:
300 b->buffer_size = g_value_get_uint (value);
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 gst_bz2enc_class_init (GstBz2encClass * klass)
310 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
311 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
313 gobject_class->finalize = gst_bz2enc_finalize;
314 gobject_class->set_property = gst_bz2enc_set_property;
315 gobject_class->get_property = gst_bz2enc_get_property;
317 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCK_SIZE,
318 g_param_spec_uint ("block-size", "Block size", "Block size",
319 1, 9, DEFAULT_BLOCK_SIZE,
320 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
322 g_param_spec_uint ("buffer-size", "Buffer size", "Buffer size",
323 1, G_MAXUINT, DEFAULT_BUFFER_SIZE,
324 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
327 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
328 gst_element_class_set_static_metadata (gstelement_class, "BZ2 encoder",
329 "Codec/Encoder", "Compresses streams",
330 "Lutz Mueller <lutz@users.sourceforge.net>");
332 GST_DEBUG_CATEGORY_INIT (bz2enc_debug, "bz2enc", 0, "BZ2 compressor");