Merge branch 'move_subdir_bad' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / bz2 / gstbz2enc.c
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 GST_ELEMENT_REGISTER_DEFINE (bz2enc, "bz2enc", GST_RANK_NONE, GST_TYPE_BZ2ENC);
71
72 static void
73 gst_bz2enc_compress_end (GstBz2enc * b)
74 {
75   g_return_if_fail (GST_IS_BZ2ENC (b));
76
77   if (b->ready) {
78     BZ2_bzCompressEnd (&b->stream);
79     memset (&b->stream, 0, sizeof (b->stream));
80     b->ready = FALSE;
81   }
82 }
83
84 static void
85 gst_bz2enc_compress_init (GstBz2enc * b)
86 {
87   g_return_if_fail (GST_IS_BZ2ENC (b));
88
89   gst_bz2enc_compress_end (b);
90   b->offset = 0;
91   switch (BZ2_bzCompressInit (&b->stream, b->block_size, 0, 0)) {
92     case BZ_OK:
93       b->ready = TRUE;
94       return;
95     default:
96       b->ready = FALSE;
97       GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL),
98           ("Failed to start compression."));
99       return;
100   }
101 }
102
103 static gboolean
104 gst_bz2enc_event (GstPad * pad, GstObject * parent, GstEvent * e)
105 {
106   GstBz2enc *b;
107   gboolean ret;
108
109   b = GST_BZ2ENC (parent);
110   switch (GST_EVENT_TYPE (e)) {
111     case GST_EVENT_EOS:{
112       GstFlowReturn flow = GST_FLOW_OK;
113       int r = BZ_FINISH_OK;
114
115       do {
116         GstBuffer *out;
117         GstMapInfo omap;
118         guint n;
119
120         out = gst_buffer_new_and_alloc (b->buffer_size);
121
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);
131           break;
132         }
133
134         n = gst_buffer_get_size (out);
135         if (b->stream.avail_out >= n) {
136           gst_buffer_unref (out);
137           break;
138         }
139
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;
143
144         flow = gst_pad_push (b->src, out);
145
146         if (flow != GST_FLOW_OK) {
147           GST_DEBUG_OBJECT (b, "push on EOS failed: %s",
148               gst_flow_get_name (flow));
149           break;
150         }
151       } while (r != BZ_STREAM_END);
152
153       ret = gst_pad_event_default (pad, parent, e);
154
155       if (r != BZ_STREAM_END || flow != GST_FLOW_OK)
156         ret = FALSE;
157
158       gst_bz2enc_compress_init (b);
159       break;
160     }
161     default:
162       ret = gst_pad_event_default (pad, parent, e);
163       break;
164   }
165
166   return ret;
167 }
168
169 static GstFlowReturn
170 gst_bz2enc_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
171 {
172   GstFlowReturn flow = GST_FLOW_OK;
173   GstBuffer *out;
174   GstBz2enc *b;
175   guint n;
176   int bz2_ret;
177   GstMapInfo map = GST_MAP_INFO_INIT, omap;
178
179   b = GST_BZ2ENC (parent);
180
181   if (!b->ready)
182     goto not_ready;
183
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);
189
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)
196       goto compress_error;
197
198     n = gst_buffer_get_size (out);
199     if (b->stream.avail_out >= n) {
200       gst_buffer_unref (out);
201       break;
202     }
203
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;
207
208     flow = gst_pad_push (b->src, out);
209
210     if (flow != GST_FLOW_OK)
211       break;
212
213     b->offset += n;
214   }
215
216 done:
217
218   gst_buffer_unmap (in, &map);
219   gst_buffer_unref (in);
220   return flow;
221
222 /* ERRORS */
223 not_ready:
224   {
225     GST_ELEMENT_ERROR (b, LIBRARY, FAILED, (NULL), ("Compressor not ready."));
226     flow = GST_FLOW_FLUSHING;
227     goto done;
228   }
229 compress_error:
230   {
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;
236     goto done;
237   }
238 }
239
240 static void
241 gst_bz2enc_init (GstBz2enc * b)
242 {
243   GstCaps *tmp = NULL;
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);
248
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);
253
254   gst_caps_unref(tmp);
255   b->block_size = DEFAULT_BLOCK_SIZE;
256   b->buffer_size = DEFAULT_BUFFER_SIZE;
257   gst_bz2enc_compress_init (b);
258 }
259
260 static void
261 gst_bz2enc_finalize (GObject * object)
262 {
263   GstBz2enc *b = GST_BZ2ENC (object);
264
265   gst_bz2enc_compress_end (b);
266
267   G_OBJECT_CLASS (parent_class)->finalize (object);
268 }
269
270 static void
271 gst_bz2enc_get_property (GObject * object, guint prop_id,
272     GValue * value, GParamSpec * pspec)
273 {
274   GstBz2enc *b = GST_BZ2ENC (object);
275
276   switch (prop_id) {
277     case PROP_BLOCK_SIZE:
278       g_value_set_uint (value, b->block_size);
279       break;
280     case PROP_BUFFER_SIZE:
281       g_value_set_uint (value, b->buffer_size);
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285   }
286 }
287
288 static void
289 gst_bz2enc_set_property (GObject * object, guint prop_id,
290     const GValue * value, GParamSpec * pspec)
291 {
292   GstBz2enc *b = GST_BZ2ENC (object);
293
294   switch (prop_id) {
295     case PROP_BLOCK_SIZE:
296       b->block_size = g_value_get_uint (value);
297       gst_bz2enc_compress_init (b);
298       break;
299     case PROP_BUFFER_SIZE:
300       b->buffer_size = g_value_get_uint (value);
301       break;
302     default:
303       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304   }
305 }
306
307 static void
308 gst_bz2enc_class_init (GstBz2encClass * klass)
309 {
310   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
311   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
312
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;
316
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));
325
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>");
331
332   GST_DEBUG_CATEGORY_INIT (bz2enc_debug, "bz2enc", 0, "BZ2 compressor");
333 }