1 /* GStreamer Wavpack plugin
2  * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
3  * Copyright (c) 2006 Edward Hervey <bilboed@gmail.com>
4  * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
5  *
6  * gstwavpackdec.c: raw Wavpack bitstream decoder
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /**
25  * SECTION:element-wavpackdec
26  *
27  * WavpackDec decodes framed (for example by the WavpackParse element)
28  * Wavpack streams and decodes them to raw audio.
29  * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
30  * audio codec that features both lossless and lossy encoding.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch-1.0 filesrc location=test.wv ! wavpackparse ! wavpackdec ! audioconvert ! audioresample ! autoaudiosink
36  * ]| This pipeline decodes the Wavpack file test.wv into raw audio buffers and
37  * tries to play it back using an automatically found audio sink.
38  * </refsect2>
39  */
40 
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
45 #include <gst/gst.h>
46 #include <gst/audio/audio.h>
47 
48 #include <math.h>
49 #include <string.h>
50 
51 #include <wavpack/wavpack.h>
52 #include "gstwavpackdec.h"
53 #include "gstwavpackcommon.h"
54 #include "gstwavpackstreamreader.h"
55 
56 
57 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_dec_debug);
58 #define GST_CAT_DEFAULT gst_wavpack_dec_debug
59 
60 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("audio/x-wavpack, "
64         "depth = (int) [ 1, 32 ], "
65         "channels = (int) [ 1, 8 ], "
66         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
67     );
68 
69 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS ("audio/x-raw, "
73         "format = (string) S8, "
74         "layout = (string) interleaved, "
75         "channels = (int) [ 1, 8 ], "
76         "rate = (int) [ 6000, 192000 ]; "
77         "audio/x-raw, "
78         "format = (string) " GST_AUDIO_NE (S16) ", "
79         "layout = (string) interleaved, "
80         "channels = (int) [ 1, 8 ], "
81         "rate = (int) [ 6000, 192000 ]; "
82         "audio/x-raw, "
83         "format = (string) " GST_AUDIO_NE (S32) ", "
84         "layout = (string) interleaved, "
85         "channels = (int) [ 1, 8 ], " "rate = (int) [ 6000, 192000 ]")
86     );
87 
88 static gboolean gst_wavpack_dec_start (GstAudioDecoder * dec);
89 static gboolean gst_wavpack_dec_stop (GstAudioDecoder * dec);
90 static gboolean gst_wavpack_dec_set_format (GstAudioDecoder * dec,
91     GstCaps * caps);
92 static GstFlowReturn gst_wavpack_dec_handle_frame (GstAudioDecoder * dec,
93     GstBuffer * buffer);
94 
95 static void gst_wavpack_dec_finalize (GObject * object);
96 static void gst_wavpack_dec_post_tags (GstWavpackDec * dec);
97 
98 #define gst_wavpack_dec_parent_class parent_class
99 G_DEFINE_TYPE (GstWavpackDec, gst_wavpack_dec, GST_TYPE_AUDIO_DECODER);
100 
101 static void
gst_wavpack_dec_class_init(GstWavpackDecClass * klass)102 gst_wavpack_dec_class_init (GstWavpackDecClass * klass)
103 {
104   GObjectClass *gobject_class = (GObjectClass *) klass;
105   GstElementClass *element_class = (GstElementClass *) (klass);
106   GstAudioDecoderClass *base_class = (GstAudioDecoderClass *) (klass);
107 
108   gst_element_class_add_static_pad_template (element_class, &src_factory);
109   gst_element_class_add_static_pad_template (element_class, &sink_factory);
110   gst_element_class_set_static_metadata (element_class, "Wavpack audio decoder",
111       "Codec/Decoder/Audio",
112       "Decodes Wavpack audio data",
113       "Arwed v. Merkatz <v.merkatz@gmx.net>, "
114       "Sebastian Dröge <slomo@circular-chaos.org>");
115 
116   gobject_class->finalize = gst_wavpack_dec_finalize;
117 
118   base_class->start = GST_DEBUG_FUNCPTR (gst_wavpack_dec_start);
119   base_class->stop = GST_DEBUG_FUNCPTR (gst_wavpack_dec_stop);
120   base_class->set_format = GST_DEBUG_FUNCPTR (gst_wavpack_dec_set_format);
121   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_wavpack_dec_handle_frame);
122 }
123 
124 static void
gst_wavpack_dec_reset(GstWavpackDec * dec)125 gst_wavpack_dec_reset (GstWavpackDec * dec)
126 {
127   dec->wv_id.buffer = NULL;
128   dec->wv_id.position = dec->wv_id.length = 0;
129 
130   dec->channels = 0;
131   dec->channel_mask = 0;
132   dec->sample_rate = 0;
133   dec->depth = 0;
134 }
135 
136 static void
gst_wavpack_dec_init(GstWavpackDec * dec)137 gst_wavpack_dec_init (GstWavpackDec * dec)
138 {
139   dec->context = NULL;
140   dec->stream_reader = gst_wavpack_stream_reader_new ();
141 
142   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
143   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
144       (dec), TRUE);
145   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
146 
147   gst_wavpack_dec_reset (dec);
148 }
149 
150 static void
gst_wavpack_dec_finalize(GObject * object)151 gst_wavpack_dec_finalize (GObject * object)
152 {
153   GstWavpackDec *dec = GST_WAVPACK_DEC (object);
154 
155   g_free (dec->stream_reader);
156   dec->stream_reader = NULL;
157 
158   G_OBJECT_CLASS (parent_class)->finalize (object);
159 }
160 
161 static gboolean
gst_wavpack_dec_start(GstAudioDecoder * dec)162 gst_wavpack_dec_start (GstAudioDecoder * dec)
163 {
164   GST_DEBUG_OBJECT (dec, "start");
165 
166   /* never mind a few errors */
167   gst_audio_decoder_set_max_errors (dec, 16);
168   /* don't bother us with flushing */
169   gst_audio_decoder_set_drainable (dec, FALSE);
170   /* aim for some perfect timestamping */
171   gst_audio_decoder_set_tolerance (dec, 10 * GST_MSECOND);
172 
173   return TRUE;
174 }
175 
176 static gboolean
gst_wavpack_dec_stop(GstAudioDecoder * dec)177 gst_wavpack_dec_stop (GstAudioDecoder * dec)
178 {
179   GstWavpackDec *wpdec = GST_WAVPACK_DEC (dec);
180 
181   GST_DEBUG_OBJECT (dec, "stop");
182 
183   if (wpdec->context) {
184     WavpackCloseFile (wpdec->context);
185     wpdec->context = NULL;
186   }
187 
188   gst_wavpack_dec_reset (wpdec);
189 
190   return TRUE;
191 }
192 
193 static void
gst_wavpack_dec_negotiate(GstWavpackDec * dec)194 gst_wavpack_dec_negotiate (GstWavpackDec * dec)
195 {
196   GstAudioInfo info;
197   GstAudioFormat fmt;
198   GstAudioChannelPosition pos[64] = { GST_AUDIO_CHANNEL_POSITION_INVALID, };
199 
200   /* arrange for 1, 2 or 4-byte width == depth output */
201   dec->width = dec->depth;
202   switch (dec->depth) {
203     case 8:
204       fmt = GST_AUDIO_FORMAT_S8;
205       break;
206     case 16:
207       fmt = _GST_AUDIO_FORMAT_NE (S16);
208       break;
209     case 24:
210     case 32:
211       fmt = _GST_AUDIO_FORMAT_NE (S32);
212       dec->width = 32;
213       break;
214     default:
215       fmt = GST_AUDIO_FORMAT_UNKNOWN;
216       g_assert_not_reached ();
217       break;
218   }
219 
220   g_assert (dec->channel_mask != 0);
221 
222   if (!gst_wavpack_get_channel_positions (dec->channels,
223           dec->channel_mask, pos))
224     GST_WARNING_OBJECT (dec, "Failed to set channel layout");
225 
226   gst_audio_info_init (&info);
227   gst_audio_info_set_format (&info, fmt, dec->sample_rate, dec->channels, pos);
228 
229   gst_audio_channel_positions_to_valid_order (info.position, info.channels);
230   gst_audio_get_channel_reorder_map (info.channels,
231       info.position, pos, dec->channel_reorder_map);
232 
233   /* should always succeed */
234   gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info);
235 }
236 
237 static gboolean
gst_wavpack_dec_set_format(GstAudioDecoder * bdec,GstCaps * caps)238 gst_wavpack_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
239 {
240   /* pretty much nothing to do here,
241    * we'll parse it all from the stream and setup then */
242 
243   return TRUE;
244 }
245 
246 static void
gst_wavpack_dec_post_tags(GstWavpackDec * dec)247 gst_wavpack_dec_post_tags (GstWavpackDec * dec)
248 {
249   GstTagList *list;
250   GstFormat format_time = GST_FORMAT_TIME, format_bytes = GST_FORMAT_BYTES;
251   gint64 duration, size;
252 
253   /* try to estimate the average bitrate */
254   if (gst_pad_peer_query_duration (GST_AUDIO_DECODER_SINK_PAD (dec),
255           format_bytes, &size) &&
256       gst_pad_peer_query_duration (GST_AUDIO_DECODER_SINK_PAD (dec),
257           format_time, &duration) && size > 0 && duration > 0) {
258     guint64 bitrate;
259 
260     list = gst_tag_list_new_empty ();
261 
262     bitrate = gst_util_uint64_scale (size, 8 * GST_SECOND, duration);
263     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
264         (guint) bitrate, NULL);
265     gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list,
266         GST_TAG_MERGE_REPLACE);
267     gst_tag_list_unref (list);
268   }
269 }
270 
271 static GstFlowReturn
gst_wavpack_dec_handle_frame(GstAudioDecoder * bdec,GstBuffer * buf)272 gst_wavpack_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
273 {
274   GstWavpackDec *dec;
275   GstBuffer *outbuf = NULL;
276   GstFlowReturn ret = GST_FLOW_OK;
277   WavpackHeader wph;
278   int32_t decoded, unpacked_size;
279   gboolean format_changed;
280   gint width, depth, i, j, max;
281   gint32 *dec_data = NULL;
282   guint8 *out_data;
283   GstMapInfo map, omap;
284 
285   dec = GST_WAVPACK_DEC (bdec);
286 
287   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
288 
289   gst_buffer_map (buf, &map, GST_MAP_READ);
290 
291   /* check input, we only accept framed input with complete chunks */
292   if (map.size < sizeof (WavpackHeader))
293     goto input_not_framed;
294 
295   if (!gst_wavpack_read_header (&wph, map.data))
296     goto invalid_header;
297 
298   if (map.size < wph.ckSize + 4 * 1 + 4)
299     goto input_not_framed;
300 
301   if (!(wph.flags & INITIAL_BLOCK))
302     goto input_not_framed;
303 
304   dec->wv_id.buffer = map.data;
305   dec->wv_id.length = map.size;
306   dec->wv_id.position = 0;
307 
308   /* create a new wavpack context if there is none yet but if there
309    * was already one (i.e. caps were set on the srcpad) check whether
310    * the new one has the same caps */
311   if (!dec->context) {
312     gchar error_msg[80];
313 
314     dec->context = WavpackOpenFileInputEx (dec->stream_reader,
315         &dec->wv_id, NULL, error_msg, OPEN_STREAMING, 0);
316 
317     /* expect this to work */
318     if (!dec->context) {
319       GST_WARNING_OBJECT (dec, "Couldn't decode buffer: %s", error_msg);
320       goto context_failed;
321     }
322   }
323 
324   g_assert (dec->context != NULL);
325 
326   format_changed =
327       (dec->sample_rate != WavpackGetSampleRate (dec->context)) ||
328       (dec->channels != WavpackGetNumChannels (dec->context)) ||
329       (dec->depth != WavpackGetBytesPerSample (dec->context) * 8) ||
330       (dec->channel_mask != WavpackGetChannelMask (dec->context));
331 
332   if (!gst_pad_has_current_caps (GST_AUDIO_DECODER_SRC_PAD (dec)) ||
333       format_changed) {
334     gint channel_mask;
335 
336     dec->sample_rate = WavpackGetSampleRate (dec->context);
337     dec->channels = WavpackGetNumChannels (dec->context);
338     dec->depth = WavpackGetBytesPerSample (dec->context) * 8;
339 
340     channel_mask = WavpackGetChannelMask (dec->context);
341     if (channel_mask == 0)
342       channel_mask = gst_wavpack_get_default_channel_mask (dec->channels);
343 
344     dec->channel_mask = channel_mask;
345 
346     gst_wavpack_dec_negotiate (dec);
347 
348     /* send GST_TAG_AUDIO_CODEC and GST_TAG_BITRATE tags before something
349      * is decoded or after the format has changed */
350     gst_wavpack_dec_post_tags (dec);
351   }
352 
353   /* alloc output buffer */
354   dec_data = g_malloc (4 * wph.block_samples * dec->channels);
355 
356   /* decode */
357   decoded = WavpackUnpackSamples (dec->context, dec_data, wph.block_samples);
358   if (decoded != wph.block_samples)
359     goto decode_error;
360 
361   unpacked_size = (dec->width / 8) * wph.block_samples * dec->channels;
362   outbuf = gst_buffer_new_and_alloc (unpacked_size);
363 
364   /* legacy; pass along offset, whatever that might entail */
365   GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
366 
367   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
368   out_data = omap.data;
369 
370   width = dec->width;
371   depth = dec->depth;
372   max = dec->channels * wph.block_samples;
373   if (width == 8) {
374     gint8 *outbuffer = (gint8 *) out_data;
375     gint *reorder_map = dec->channel_reorder_map;
376 
377     for (i = 0; i < max; i += dec->channels) {
378       for (j = 0; j < dec->channels; j++)
379         *outbuffer++ = (gint8) (dec_data[i + reorder_map[j]]);
380     }
381   } else if (width == 16) {
382     gint16 *outbuffer = (gint16 *) out_data;
383     gint *reorder_map = dec->channel_reorder_map;
384 
385     for (i = 0; i < max; i += dec->channels) {
386       for (j = 0; j < dec->channels; j++)
387         *outbuffer++ = (gint16) (dec_data[i + reorder_map[j]]);
388     }
389   } else if (dec->width == 32) {
390     gint32 *outbuffer = (gint32 *) out_data;
391     gint *reorder_map = dec->channel_reorder_map;
392 
393     if (width != depth) {
394       for (i = 0; i < max; i += dec->channels) {
395         for (j = 0; j < dec->channels; j++)
396           *outbuffer++ =
397               (gint32) (dec_data[i + reorder_map[j]] << (width - depth));
398       }
399     } else {
400       for (i = 0; i < max; i += dec->channels) {
401         for (j = 0; j < dec->channels; j++)
402           *outbuffer++ = (gint32) (dec_data[i + reorder_map[j]]);
403       }
404     }
405   } else {
406     g_assert_not_reached ();
407   }
408 
409   gst_buffer_unmap (outbuf, &omap);
410   gst_buffer_unmap (buf, &map);
411   buf = NULL;
412 
413   g_free (dec_data);
414 
415   ret = gst_audio_decoder_finish_frame (bdec, outbuf, 1);
416 
417 out:
418   if (buf)
419     gst_buffer_unmap (buf, &map);
420 
421   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
422     GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (ret));
423   }
424 
425   return ret;
426 
427 /* ERRORS */
428 input_not_framed:
429   {
430     GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Expected framed input"));
431     ret = GST_FLOW_ERROR;
432     goto out;
433   }
434 invalid_header:
435   {
436     GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Invalid wavpack header"));
437     ret = GST_FLOW_ERROR;
438     goto out;
439   }
440 context_failed:
441   {
442     GST_AUDIO_DECODER_ERROR (bdec, 1, LIBRARY, INIT, (NULL),
443         ("error creating Wavpack context"), ret);
444     goto out;
445   }
446 decode_error:
447   {
448     const gchar *reason = "unknown";
449 
450     if (dec->context) {
451       reason = WavpackGetErrorMessage (dec->context);
452     } else {
453       reason = "couldn't create decoder context";
454     }
455     GST_AUDIO_DECODER_ERROR (bdec, 1, STREAM, DECODE, (NULL),
456         ("decoding error: %s", reason), ret);
457     g_free (dec_data);
458     if (ret == GST_FLOW_OK)
459       gst_audio_decoder_finish_frame (bdec, NULL, 1);
460     goto out;
461   }
462 }
463 
464 gboolean
gst_wavpack_dec_plugin_init(GstPlugin * plugin)465 gst_wavpack_dec_plugin_init (GstPlugin * plugin)
466 {
467   if (!gst_element_register (plugin, "wavpackdec",
468           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_DEC))
469     return FALSE;
470   GST_DEBUG_CATEGORY_INIT (gst_wavpack_dec_debug, "wavpackdec", 0,
471       "Wavpack decoder");
472   return TRUE;
473 }
474