Unify the long descriptions in the plugin details (#337263).
[platform/upstream/gst-plugins-good.git] / ext / wavpack / gstwavpackdec.c
1 /* GStreamer Wavpack plugin
2  * (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
3  *
4  * gstwavpackdec.c: raw Wavpack bitstream decoder
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/gst.h>
23
24 #include <math.h>
25 #include <string.h>
26
27 #include <wavpack/wavpack.h>
28 #include "gstwavpackdec.h"
29 #include "gstwavpackcommon.h"
30
31 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_dec_debug);
32 #define GST_CAT_DEFAULT gst_wavpack_dec_debug
33
34 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("audio/x-wavpack, "
38         "width = (int) { 8, 16, 24, 32 }, "
39         "channels = (int) { 1, 2 }, "
40         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
41     );
42
43 static GstStaticPadTemplate wvc_sink_factory =
44 GST_STATIC_PAD_TEMPLATE ("wvcsink",
45     GST_PAD_SINK,
46     GST_PAD_REQUEST,
47     GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
48     );
49
50 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/x-raw-int, "
54         "width = (int) { 8, 16, 24, 32 }, "
55         "depth = (int) { 8, 16, 24, 32 }, "
56         "channels = (int) { 1, 2 }, "
57         "rate = (int) [ 6000, 192000 ], "
58         "endianness = (int) LITTLE_ENDIAN, " "signed = (boolean) true")
59 /*
60         "audio/x-raw-float, "
61         "width = (int) 32, "
62         "channels = (int) { 1, 2 }, "
63         "rate = (int) [ 6000, 192000 ], " "endianness = (int) LITTLE_ENDIAN"
64 */
65     );
66
67 static GstFlowReturn gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buffer);
68 static gboolean gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event);
69
70 GST_BOILERPLATE (GstWavpackDec, gst_wavpack_dec, GstElement, GST_TYPE_ELEMENT)
71
72      static gboolean gst_wavpack_dec_setcaps (GstPad * pad, GstCaps * caps)
73 {
74   GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
75   GstStructure *structure;
76   GstCaps *srccaps;
77   gint bits, rate, channels;
78
79   structure = gst_caps_get_structure (caps, 0);
80   if (!gst_structure_get_int (structure, "rate", &rate) ||
81       !gst_structure_get_int (structure, "channels", &channels) ||
82       !gst_structure_get_int (structure, "width", &bits)) {
83     return FALSE;
84   }
85
86   wavpackdec->samplerate = rate;
87   wavpackdec->channels = channels;
88   wavpackdec->width = bits;
89
90 /* 32-bit output seems to be in fact 32 bit int (e.g. Prod_Girls.wv) */
91 /*  if (bits != 32) { */
92   srccaps = gst_caps_new_simple ("audio/x-raw-int",
93       "rate", G_TYPE_INT, wavpackdec->samplerate,
94       "channels", G_TYPE_INT, wavpackdec->channels,
95       "depth", G_TYPE_INT, bits,
96       "width", G_TYPE_INT, bits,
97       "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
98       "signed", G_TYPE_BOOLEAN, TRUE, NULL);
99 /*
100   } else {
101     srccaps = gst_caps_new_simple ("audio/x-raw-float",
102         "rate", G_TYPE_INT, wavpackdec->samplerate,
103         "channels", G_TYPE_INT, wavpackdec->channels,
104         "width", G_TYPE_INT, 32,
105         "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL);
106   }
107 */
108 /*  gst_pad_set_caps (wavpackdec->sinkpad, caps); */
109
110   gst_pad_set_caps (wavpackdec->srcpad, srccaps);
111   gst_pad_use_fixed_caps (wavpackdec->srcpad);
112
113   return TRUE;
114 }
115
116 #if 0
117 static GstPadLinkReturn
118 gst_wavpack_dec_wvclink (GstPad * pad, GstPad * peer)
119 {
120   if (!gst_caps_is_fixed (GST_PAD_CAPS (peer)))
121     return GST_PAD_LINK_REFUSED;
122
123   return GST_PAD_LINK_OK;
124 }
125 #endif
126
127 static void
128 gst_wavpack_dec_base_init (gpointer klass)
129 {
130   static GstElementDetails plugin_details =
131       GST_ELEMENT_DETAILS ("WavePack audio decoder",
132       "Codec/Decoder/Audio",
133       "Decode Wavpack audio data",
134       "Arwed v. Merkatz <v.merkatz@gmx.net>");
135   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
136
137   gst_element_class_add_pad_template (element_class,
138       gst_static_pad_template_get (&src_factory));
139   gst_element_class_add_pad_template (element_class,
140       gst_static_pad_template_get (&sink_factory));
141   gst_element_class_add_pad_template (element_class,
142       gst_static_pad_template_get (&wvc_sink_factory));
143   gst_element_class_set_details (element_class, &plugin_details);
144 }
145
146 static void
147 gst_wavpack_dec_dispose (GObject * object)
148 {
149   GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (object);
150
151   g_free (wavpackdec->decodebuf);
152   wavpackdec->decodebuf = NULL;
153
154   /* FIXME: what about wavpackdec->stream and wavpackdec->context? (tpm) */
155
156   G_OBJECT_CLASS (parent_class)->dispose (object);
157 }
158
159 static void
160 gst_wavpack_dec_class_init (GstWavpackDecClass * klass)
161 {
162   GObjectClass *gobject_class;
163   GstElementClass *gstelement_class;
164
165   gobject_class = (GObjectClass *) klass;
166   gstelement_class = (GstElementClass *) klass;
167
168   gobject_class->dispose = gst_wavpack_dec_dispose;
169 }
170
171 static gboolean
172 gst_wavpack_dec_src_query (GstPad * pad, GstQuery * query)
173 {
174   return gst_pad_query_default (pad, query);
175 }
176
177 static gboolean
178 gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event)
179 {
180   GstWavpackDec *dec;
181
182   dec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
183
184   GST_LOG_OBJECT (dec, "Received %s event", GST_EVENT_TYPE_NAME (event));
185   switch (GST_EVENT_TYPE (event)) {
186     case GST_EVENT_NEWSEGMENT:{
187       /* TODO: save current segment so we can do clipping, for now
188        * we'll just leave the clipping to the audio sink */
189       break;
190     }
191     default:
192       break;
193   }
194
195   gst_object_unref (dec);
196
197   return gst_pad_event_default (pad, event);
198 }
199
200 static void
201 gst_wavpack_dec_init (GstWavpackDec * wavpackdec, GstWavpackDecClass * gklass)
202 {
203   GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavpackdec);
204
205   wavpackdec->sinkpad =
206       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
207           "sink"), "sink");
208   gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->sinkpad);
209
210   gst_pad_set_chain_function (wavpackdec->sinkpad,
211       GST_DEBUG_FUNCPTR (gst_wavpack_dec_chain));
212   gst_pad_set_setcaps_function (wavpackdec->sinkpad,
213       GST_DEBUG_FUNCPTR (gst_wavpack_dec_setcaps));
214   gst_pad_set_event_function (wavpackdec->sinkpad,
215       GST_DEBUG_FUNCPTR (gst_wavpack_dec_sink_event));
216
217 #if 0
218   wavpackdec->wvcsinkpad =
219       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
220           "wvcsink"), "wvcsink");
221   gst_pad_set_link_function (wavpackdec->wvcsinkpad, gst_wavpack_dec_wvclink);
222   gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->wvcsinkpad);
223 #endif
224
225
226   wavpackdec->srcpad =
227       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
228           "src"), "src");
229   gst_pad_use_fixed_caps (wavpackdec->srcpad);
230
231   gst_pad_set_query_function (wavpackdec->srcpad,
232       GST_DEBUG_FUNCPTR (gst_wavpack_dec_src_query));
233
234   gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->srcpad);
235
236   wavpackdec->decodebuf = NULL;
237   wavpackdec->decodebuf_size = 0;
238   wavpackdec->stream = (WavpackStream *) g_malloc0 (sizeof (WavpackStream));
239   wavpackdec->context = (WavpackContext *) g_malloc0 (sizeof (WavpackContext));
240 }
241
242 static void
243 gst_wavpack_dec_setup_context (GstWavpackDec * wavpackdec, guchar * data,
244     guchar * cdata)
245 {
246   WavpackContext *context = wavpackdec->context;
247   WavpackStream *stream = wavpackdec->stream;
248   guint buffer_size;
249
250   memset (context, 0, sizeof (context));
251
252   context->open_flags = 0;
253   context->current_stream = 0;
254   context->num_streams = 1;
255
256   memset (stream, 0, sizeof (stream));
257   context->streams[0] = stream;
258
259   gst_wavpack_read_header (&stream->wphdr, data);
260   stream->blockbuff = data;
261
262   if (cdata) {
263     context->wvc_flag = TRUE;
264     gst_wavpack_read_header (&stream->wphdr, cdata);
265     stream->block2buff = cdata;
266   } else {
267     context->wvc_flag = FALSE;
268   }
269
270   buffer_size =
271       stream->wphdr.block_samples * wavpackdec->channels * sizeof (int32_t);
272   if (wavpackdec->decodebuf_size < buffer_size) {
273     wavpackdec->decodebuf =
274         (int32_t *) g_realloc (wavpackdec->decodebuf, buffer_size);
275     wavpackdec->decodebuf_size = buffer_size;
276   }
277
278   unpack_init (context);
279 }
280
281 static GstBuffer *
282 gst_wavpack_dec_format_samples (GstWavpackDec * wavpackdec, int32_t * samples,
283     guint num_samples)
284 {
285   GstBuffer *buf;
286   gint i;
287   guint8 *dst;
288   int32_t temp;
289
290   buf =
291       gst_buffer_new_and_alloc (num_samples * wavpackdec->width / 8 *
292       wavpackdec->channels);
293
294   dst = (guint8 *) GST_BUFFER_DATA (buf);
295
296   switch (wavpackdec->width) {
297     case 8:
298       for (i = 0; i < num_samples * wavpackdec->channels; ++i)
299         *dst++ = *samples++ + 128;
300       break;
301     case 16:
302       for (i = 0; i < num_samples * wavpackdec->channels; ++i) {
303         *dst++ = (guint8) (temp = *samples++);
304         *dst++ = (guint8) (temp >> 8);
305       }
306       break;
307     case 24:
308       for (i = 0; i < num_samples * wavpackdec->channels; ++i) {
309         *dst++ = (guint8) (temp = *samples++);
310         *dst++ = (guint8) (temp >> 8);
311         *dst++ = (guint8) (temp >> 16);
312       }
313       break;
314     case 32:
315       for (i = 0; i < num_samples * wavpackdec->channels; ++i) {
316         *dst++ = (guint8) (temp = *samples++);
317         *dst++ = (guint8) (temp >> 8);
318         *dst++ = (guint8) (temp >> 16);
319         *dst++ = (guint8) (temp >> 24);
320       }
321       break;
322     default:
323       break;
324   }
325
326   return buf;
327 }
328
329 static GstFlowReturn
330 gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buf)
331 {
332
333   GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
334   GstBuffer *outbuf, *cbuf = NULL;
335   GstFlowReturn ret = GST_FLOW_OK;
336
337 #if 0
338   if (gst_pad_is_linked (wavpackdec->wvcsinkpad)) {
339     if (GST_FLOW_OK != gst_pad_pull_range (wavpackdec->wvcsinkpad,
340             wavpackdec->wvcflushed_bytes, -1, &cbuf)) {
341       cbuf = NULL;
342     } else {
343       wavpackdec->wvcflushed_bytes += GST_BUFFER_SIZE (cbuf);
344     }
345
346   }
347 #endif
348
349   gst_wavpack_dec_setup_context (wavpackdec, GST_BUFFER_DATA (buf),
350       cbuf ? GST_BUFFER_DATA (cbuf) : NULL);
351   unpack_samples (wavpackdec->context, wavpackdec->decodebuf,
352       wavpackdec->context->streams[0]->wphdr.block_samples);
353   outbuf =
354       gst_wavpack_dec_format_samples (wavpackdec, wavpackdec->decodebuf,
355       wavpackdec->context->streams[0]->wphdr.block_samples);
356
357   gst_buffer_stamp (outbuf, buf);
358
359   gst_buffer_unref (buf);
360   if (cbuf) {
361     gst_buffer_unref (cbuf);
362   }
363
364   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (wavpackdec->srcpad));
365
366   GST_LOG_OBJECT (wavpackdec, "pushing buffer with time %" GST_TIME_FORMAT,
367       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
368
369   ret = gst_pad_push (wavpackdec->srcpad, outbuf);
370   if (ret != GST_FLOW_OK) {
371     GST_DEBUG_OBJECT (wavpackdec, "pad_push: %s", gst_flow_get_name (ret));
372   }
373
374   return ret;
375 }
376
377 gboolean
378 gst_wavpack_dec_plugin_init (GstPlugin * plugin)
379 {
380   if (!gst_element_register (plugin, "wavpackdec",
381           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_DEC))
382     return FALSE;
383
384   GST_DEBUG_CATEGORY_INIT (gst_wavpack_dec_debug, "wavpackdec", 0,
385       "wavpack decoder");
386
387   return TRUE;
388 }