1 /* GStreamer bz2 encoder
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 "gstbz2enc.h"
23
24 #include <bzlib.h>
25 #include <string.h>
26
27 GST_DEBUG_CATEGORY_STATIC (bz2enc_debug);
28 #define GST_CAT_DEFAULT bz2enc_debug
29
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"));
36
37 #define DEFAULT_BLOCK_SIZE 6
38 #define DEFAULT_BUFFER_SIZE 1024
39
40 enum
41 {
42 PROP_0,
43 PROP_BLOCK_SIZE,
44 PROP_BUFFER_SIZE
45 };
46
47 struct _GstBz2enc
48 {
49 GstElement parent;
50
51 GstPad *sink;
52 GstPad *src;
53
54 /* Properties */
55 guint block_size;
56 guint buffer_size;
57
58 gboolean ready;
59 bz_stream stream;
60 guint64 offset;
61 };
62
63 struct _GstBz2encClass
64 {
65 GstElementClass parent_class;
66 };
67
68 #define gst_bz2enc_parent_class parent_class
69 G_DEFINE_TYPE (GstBz2enc, gst_bz2enc, GST_TYPE_ELEMENT);
70
71 static void
gst_bz2enc_compress_end(GstBz2enc * b)72 gst_bz2enc_compress_end (GstBz2enc * b)
73 {
74 g_return_if_fail (GST_IS_BZ2ENC (b));
75
76 if (b->ready) {
77 BZ2_bzCompressEnd (&b->stream);
78 memset (&b->stream, 0, sizeof (b->stream));
79 b->ready = FALSE;
80 }
81 }
82
83 static void
gst_bz2enc_compress_init(GstBz2enc * b)84 gst_bz2enc_compress_init (GstBz2enc * b)
85 {
86 g_return_if_fail (GST_IS_BZ2ENC (b));
87
88 gst_bz2enc_compress_end (b);
89 b->offset = 0;
90 switch (BZ2_bzCompressInit (&b->stream, b->block_size, 0, 0)) {
91 case BZ_OK:
92 b->ready = TRUE;
93 return;
94 default:
95 b->ready = FALSE;
96 GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL),
97 ("Failed to start compression."));
98 return;
99 }
100 }
101
102 static gboolean
gst_bz2enc_event(GstPad * pad,GstObject * parent,GstEvent * e)103 gst_bz2enc_event (GstPad * pad, GstObject * parent, GstEvent * e)
104 {
105 GstBz2enc *b;
106 gboolean ret;
107
108 b = GST_BZ2ENC (parent);
109 switch (GST_EVENT_TYPE (e)) {
110 case GST_EVENT_EOS:{
111 GstFlowReturn flow = GST_FLOW_OK;
112 int r = BZ_FINISH_OK;
113
114 do {
115 GstBuffer *out;
116 GstMapInfo omap;
117 guint n;
118
119 out = gst_buffer_new_and_alloc (b->buffer_size);
120
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);
130 break;
131 }
132
133 n = gst_buffer_get_size (out);
134 if (b->stream.avail_out >= n) {
135 gst_buffer_unref (out);
136 break;
137 }
138
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;
142
143 flow = gst_pad_push (b->src, out);
144
145 if (flow != GST_FLOW_OK) {
146 GST_DEBUG_OBJECT (b, "push on EOS failed: %s",
147 gst_flow_get_name (flow));
148 break;
149 }
150 } while (r != BZ_STREAM_END);
151
152 ret = gst_pad_event_default (pad, parent, e);
153
154 if (r != BZ_STREAM_END || flow != GST_FLOW_OK)
155 ret = FALSE;
156
157 gst_bz2enc_compress_init (b);
158 break;
159 }
160 default:
161 ret = gst_pad_event_default (pad, parent, e);
162 break;
163 }
164
165 return ret;
166 }
167
168 static GstFlowReturn
gst_bz2enc_chain(GstPad * pad,GstObject * parent,GstBuffer * in)169 gst_bz2enc_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
170 {
171 GstFlowReturn flow = GST_FLOW_OK;
172 GstBuffer *out;
173 GstBz2enc *b;
174 guint n;
175 int bz2_ret;
176 GstMapInfo map = GST_MAP_INFO_INIT, omap;
177
178 b = GST_BZ2ENC (parent);
179
180 if (!b->ready)
181 goto not_ready;
182
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);
188
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)
195 goto compress_error;
196
197 n = gst_buffer_get_size (out);
198 if (b->stream.avail_out >= n) {
199 gst_buffer_unref (out);
200 break;
201 }
202
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;
206
207 flow = gst_pad_push (b->src, out);
208
209 if (flow != GST_FLOW_OK)
210 break;
211
212 b->offset += n;
213 }
214
215 done:
216
217 gst_buffer_unmap (in, &map);
218 gst_buffer_unref (in);
219 return flow;
220
221 /* ERRORS */
222 not_ready:
223 {
224 GST_ELEMENT_ERROR (b, LIBRARY, FAILED, (NULL), ("Compressor not ready."));
225 flow = GST_FLOW_FLUSHING;
226 goto done;
227 }
228 compress_error:
229 {
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;
235 goto done;
236 }
237 }
238
239 static void
gst_bz2enc_init(GstBz2enc * b)240 gst_bz2enc_init (GstBz2enc * b)
241 {
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);
246
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);
251
252 b->block_size = DEFAULT_BLOCK_SIZE;
253 b->buffer_size = DEFAULT_BUFFER_SIZE;
254 gst_bz2enc_compress_init (b);
255 }
256
257 static void
gst_bz2enc_finalize(GObject * object)258 gst_bz2enc_finalize (GObject * object)
259 {
260 GstBz2enc *b = GST_BZ2ENC (object);
261
262 gst_bz2enc_compress_end (b);
263
264 G_OBJECT_CLASS (parent_class)->finalize (object);
265 }
266
267 static void
gst_bz2enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)268 gst_bz2enc_get_property (GObject * object, guint prop_id,
269 GValue * value, GParamSpec * pspec)
270 {
271 GstBz2enc *b = GST_BZ2ENC (object);
272
273 switch (prop_id) {
274 case PROP_BLOCK_SIZE:
275 g_value_set_uint (value, b->block_size);
276 break;
277 case PROP_BUFFER_SIZE:
278 g_value_set_uint (value, b->buffer_size);
279 break;
280 default:
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 }
283 }
284
285 static void
gst_bz2enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)286 gst_bz2enc_set_property (GObject * object, guint prop_id,
287 const GValue * value, GParamSpec * pspec)
288 {
289 GstBz2enc *b = GST_BZ2ENC (object);
290
291 switch (prop_id) {
292 case PROP_BLOCK_SIZE:
293 b->block_size = g_value_get_uint (value);
294 gst_bz2enc_compress_init (b);
295 break;
296 case PROP_BUFFER_SIZE:
297 b->buffer_size = g_value_get_uint (value);
298 break;
299 default:
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 }
302 }
303
304 static void
gst_bz2enc_class_init(GstBz2encClass * klass)305 gst_bz2enc_class_init (GstBz2encClass * klass)
306 {
307 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
308 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
309
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;
313
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));
322
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>");
328
329 GST_DEBUG_CATEGORY_INIT (bz2enc_debug, "bz2enc", 0, "BZ2 compressor");
330 }
331