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);
72 gst_bz2enc_compress_end (GstBz2enc * b)
74 g_return_if_fail (GST_IS_BZ2ENC (b));
77 BZ2_bzCompressEnd (&b->stream);
78 memset (&b->stream, 0, sizeof (b->stream));
84 gst_bz2enc_compress_init (GstBz2enc * b)
86 g_return_if_fail (GST_IS_BZ2ENC (b));
88 gst_bz2enc_compress_end (b);
90 switch (BZ2_bzCompressInit (&b->stream, b->block_size, 0, 0)) {
96 GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL),
97 ("Failed to start compression."));
103 gst_bz2enc_event (GstPad * pad, GstObject * parent, GstEvent * e)
108 b = GST_BZ2ENC (parent);
109 switch (GST_EVENT_TYPE (e)) {
111 GstFlowReturn flow = GST_FLOW_OK;
112 int r = BZ_FINISH_OK;
119 out = gst_buffer_new_and_alloc (b->buffer_size);
121 gst_buffer_map (out, &omap, GST_MAP_WRITE);
122 b->stream.next_out = (char *) omap.data;
123 b->stream.avail_out = omap.size;
124 r = BZ2_bzCompress (&b->stream, BZ_FINISH);
125 gst_buffer_unmap (out, &omap);
126 if ((r != BZ_FINISH_OK) && (r != BZ_STREAM_END)) {
127 GST_ELEMENT_ERROR (b, STREAM, ENCODE, (NULL),
128 ("Failed to finish to compress (error code %i).", r));
129 gst_buffer_unref (out);
133 n = gst_buffer_get_size (out);
134 if (b->stream.avail_out >= n) {
135 gst_buffer_unref (out);
139 gst_buffer_resize (out, 0, n - b->stream.avail_out);
140 n = gst_buffer_get_size (out);
141 GST_BUFFER_OFFSET (out) = b->stream.total_out_lo32 - n;
143 flow = gst_pad_push (b->src, out);
145 if (flow != GST_FLOW_OK) {
146 GST_DEBUG_OBJECT (b, "push on EOS failed: %s",
147 gst_flow_get_name (flow));
150 } while (r != BZ_STREAM_END);
152 ret = gst_pad_event_default (pad, parent, e);
154 if (r != BZ_STREAM_END || flow != GST_FLOW_OK)
157 gst_bz2enc_compress_init (b);
161 ret = gst_pad_event_default (pad, parent, e);
169 gst_bz2enc_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
171 GstFlowReturn flow = GST_FLOW_OK;
176 GstMapInfo map = GST_MAP_INFO_INIT, omap;
178 b = GST_BZ2ENC (parent);
183 gst_buffer_map (in, &map, GST_MAP_READ);
184 b->stream.next_in = (char *) map.data;
185 b->stream.avail_in = map.size;
186 while (b->stream.avail_in) {
187 out = gst_buffer_new_and_alloc (b->buffer_size);
189 gst_buffer_map (out, &omap, GST_MAP_WRITE);
190 b->stream.next_out = (char *) omap.data;
191 b->stream.avail_out = omap.size;
192 bz2_ret = BZ2_bzCompress (&b->stream, BZ_RUN);
193 gst_buffer_unmap (out, &omap);
194 if (bz2_ret != BZ_RUN_OK)
197 n = gst_buffer_get_size (out);
198 if (b->stream.avail_out >= n) {
199 gst_buffer_unref (out);
203 gst_buffer_resize (out, 0, n - b->stream.avail_out);
204 n = gst_buffer_get_size (out);
205 GST_BUFFER_OFFSET (out) = b->stream.total_out_lo32 - n;
207 flow = gst_pad_push (b->src, out);
209 if (flow != GST_FLOW_OK)
217 gst_buffer_unmap (in, &map);
218 gst_buffer_unref (in);
224 GST_ELEMENT_ERROR (b, LIBRARY, FAILED, (NULL), ("Compressor not ready."));
225 flow = GST_FLOW_FLUSHING;
230 GST_ELEMENT_ERROR (b, STREAM, ENCODE, (NULL),
231 ("Failed to compress data (error code %i)", bz2_ret));
232 gst_bz2enc_compress_init (b);
233 gst_buffer_unref (out);
234 flow = GST_FLOW_ERROR;
240 gst_bz2enc_init (GstBz2enc * b)
242 b->sink = gst_pad_new_from_static_template (&sink_template, "sink");
243 gst_pad_set_chain_function (b->sink, GST_DEBUG_FUNCPTR (gst_bz2enc_chain));
244 gst_pad_set_event_function (b->sink, GST_DEBUG_FUNCPTR (gst_bz2enc_event));
245 gst_element_add_pad (GST_ELEMENT (b), b->sink);
247 b->src = gst_pad_new_from_static_template (&src_template, "src");
248 gst_pad_set_caps (b->src, gst_static_pad_template_get_caps (&src_template));
249 gst_pad_use_fixed_caps (b->src);
250 gst_element_add_pad (GST_ELEMENT (b), b->src);
252 b->block_size = DEFAULT_BLOCK_SIZE;
253 b->buffer_size = DEFAULT_BUFFER_SIZE;
254 gst_bz2enc_compress_init (b);
258 gst_bz2enc_finalize (GObject * object)
260 GstBz2enc *b = GST_BZ2ENC (object);
262 gst_bz2enc_compress_end (b);
264 G_OBJECT_CLASS (parent_class)->finalize (object);
268 gst_bz2enc_get_property (GObject * object, guint prop_id,
269 GValue * value, GParamSpec * pspec)
271 GstBz2enc *b = GST_BZ2ENC (object);
274 case PROP_BLOCK_SIZE:
275 g_value_set_uint (value, b->block_size);
277 case PROP_BUFFER_SIZE:
278 g_value_set_uint (value, b->buffer_size);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 gst_bz2enc_set_property (GObject * object, guint prop_id,
287 const GValue * value, GParamSpec * pspec)
289 GstBz2enc *b = GST_BZ2ENC (object);
292 case PROP_BLOCK_SIZE:
293 b->block_size = g_value_get_uint (value);
294 gst_bz2enc_compress_init (b);
296 case PROP_BUFFER_SIZE:
297 b->buffer_size = g_value_get_uint (value);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 gst_bz2enc_class_init (GstBz2encClass * klass)
307 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
308 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
310 gobject_class->finalize = gst_bz2enc_finalize;
311 gobject_class->set_property = gst_bz2enc_set_property;
312 gobject_class->get_property = gst_bz2enc_get_property;
314 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCK_SIZE,
315 g_param_spec_uint ("block-size", "Block size", "Block size",
316 1, 9, DEFAULT_BLOCK_SIZE,
317 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_SIZE,
319 g_param_spec_uint ("buffer-size", "Buffer size", "Buffer size",
320 1, G_MAXUINT, DEFAULT_BUFFER_SIZE,
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
324 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
325 gst_element_class_set_static_metadata (gstelement_class, "BZ2 encoder",
326 "Codec/Encoder", "Compresses streams",
327 "Lutz Mueller <lutz@users.sourceforge.net>");
329 GST_DEBUG_CATEGORY_INIT (bz2enc_debug, "bz2enc", 0, "BZ2 compressor");