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>
6 * gstwavpackdec.c: raw Wavpack bitstream decoder
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.
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.
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.
25 * SECTION:element-wavpackdec
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.
33 * <title>Example launch line</title>
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.
46 #include <gst/audio/audio.h>
51 #include <wavpack/wavpack.h>
52 #include "gstwavpackdec.h"
53 #include "gstwavpackcommon.h"
54 #include "gstwavpackstreamreader.h"
57 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_dec_debug);
58 #define GST_CAT_DEFAULT gst_wavpack_dec_debug
60 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
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")
69 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
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 ]; "
78 "format = (string) " GST_AUDIO_NE (S16) ", "
79 "layout = (string) interleaved, "
80 "channels = (int) [ 1, 8 ], "
81 "rate = (int) [ 6000, 192000 ]; "
83 "format = (string) " GST_AUDIO_NE (S32) ", "
84 "layout = (string) interleaved, "
85 "channels = (int) [ 1, 8 ], " "rate = (int) [ 6000, 192000 ]")
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,
92 static GstFlowReturn gst_wavpack_dec_handle_frame (GstAudioDecoder * dec,
95 static void gst_wavpack_dec_finalize (GObject * object);
96 static void gst_wavpack_dec_post_tags (GstWavpackDec * dec);
98 #define gst_wavpack_dec_parent_class parent_class
99 G_DEFINE_TYPE (GstWavpackDec, gst_wavpack_dec, GST_TYPE_AUDIO_DECODER);
102 gst_wavpack_dec_class_init (GstWavpackDecClass * klass)
104 GObjectClass *gobject_class = (GObjectClass *) klass;
105 GstElementClass *element_class = (GstElementClass *) (klass);
106 GstAudioDecoderClass *base_class = (GstAudioDecoderClass *) (klass);
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>");
116 gobject_class->finalize = gst_wavpack_dec_finalize;
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);
125 gst_wavpack_dec_reset (GstWavpackDec * dec)
127 dec->wv_id.buffer = NULL;
128 dec->wv_id.position = dec->wv_id.length = 0;
131 dec->channel_mask = 0;
132 dec->sample_rate = 0;
137 gst_wavpack_dec_init (GstWavpackDec * dec)
140 dec->stream_reader = gst_wavpack_stream_reader_new ();
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
145 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
147 gst_wavpack_dec_reset (dec);
151 gst_wavpack_dec_finalize (GObject * object)
153 GstWavpackDec *dec = GST_WAVPACK_DEC (object);
155 g_free (dec->stream_reader);
156 dec->stream_reader = NULL;
158 G_OBJECT_CLASS (parent_class)->finalize (object);
162 gst_wavpack_dec_start (GstAudioDecoder * dec)
164 GST_DEBUG_OBJECT (dec, "start");
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);
177 gst_wavpack_dec_stop (GstAudioDecoder * dec)
179 GstWavpackDec *wpdec = GST_WAVPACK_DEC (dec);
181 GST_DEBUG_OBJECT (dec, "stop");
183 if (wpdec->context) {
184 WavpackCloseFile (wpdec->context);
185 wpdec->context = NULL;
188 gst_wavpack_dec_reset (wpdec);
194 gst_wavpack_dec_negotiate (GstWavpackDec * dec)
198 GstAudioChannelPosition pos[64] = { GST_AUDIO_CHANNEL_POSITION_INVALID, };
200 /* arrange for 1, 2 or 4-byte width == depth output */
201 dec->width = dec->depth;
202 switch (dec->depth) {
204 fmt = GST_AUDIO_FORMAT_S8;
207 fmt = _GST_AUDIO_FORMAT_NE (S16);
211 fmt = _GST_AUDIO_FORMAT_NE (S32);
215 fmt = GST_AUDIO_FORMAT_UNKNOWN;
216 g_assert_not_reached ();
220 g_assert (dec->channel_mask != 0);
222 if (!gst_wavpack_get_channel_positions (dec->channels,
223 dec->channel_mask, pos))
224 GST_WARNING_OBJECT (dec, "Failed to set channel layout");
226 gst_audio_info_init (&info);
227 gst_audio_info_set_format (&info, fmt, dec->sample_rate, dec->channels, pos);
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);
233 /* should always succeed */
234 gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info);
238 gst_wavpack_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
240 /* pretty much nothing to do here,
241 * we'll parse it all from the stream and setup then */
247 gst_wavpack_dec_post_tags (GstWavpackDec * dec)
250 GstFormat format_time = GST_FORMAT_TIME, format_bytes = GST_FORMAT_BYTES;
251 gint64 duration, size;
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) {
260 list = gst_tag_list_new_empty ();
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);
272 gst_wavpack_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
275 GstBuffer *outbuf = NULL;
276 GstFlowReturn ret = GST_FLOW_OK;
278 int32_t decoded, unpacked_size;
279 gboolean format_changed;
280 gint width, depth, i, j, max;
281 gint32 *dec_data = NULL;
283 GstMapInfo map, omap;
285 dec = GST_WAVPACK_DEC (bdec);
287 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
289 gst_buffer_map (buf, &map, GST_MAP_READ);
291 /* check input, we only accept framed input with complete chunks */
292 if (map.size < sizeof (WavpackHeader))
293 goto input_not_framed;
295 if (!gst_wavpack_read_header (&wph, map.data))
298 if (map.size < wph.ckSize + 4 * 1 + 4)
299 goto input_not_framed;
301 if (!(wph.flags & INITIAL_BLOCK))
302 goto input_not_framed;
304 dec->wv_id.buffer = map.data;
305 dec->wv_id.length = map.size;
306 dec->wv_id.position = 0;
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 */
314 dec->context = WavpackOpenFileInputEx (dec->stream_reader,
315 &dec->wv_id, NULL, error_msg, OPEN_STREAMING, 0);
317 /* expect this to work */
319 GST_WARNING_OBJECT (dec, "Couldn't decode buffer: %s", error_msg);
324 g_assert (dec->context != NULL);
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));
332 if (!gst_pad_has_current_caps (GST_AUDIO_DECODER_SRC_PAD (dec)) ||
336 dec->sample_rate = WavpackGetSampleRate (dec->context);
337 dec->channels = WavpackGetNumChannels (dec->context);
338 dec->depth = WavpackGetBytesPerSample (dec->context) * 8;
340 channel_mask = WavpackGetChannelMask (dec->context);
341 if (channel_mask == 0)
342 channel_mask = gst_wavpack_get_default_channel_mask (dec->channels);
344 dec->channel_mask = channel_mask;
346 gst_wavpack_dec_negotiate (dec);
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);
353 /* alloc output buffer */
354 dec_data = g_malloc (4 * wph.block_samples * dec->channels);
357 decoded = WavpackUnpackSamples (dec->context, dec_data, wph.block_samples);
358 if (decoded != wph.block_samples)
361 unpacked_size = (dec->width / 8) * wph.block_samples * dec->channels;
362 outbuf = gst_buffer_new_and_alloc (unpacked_size);
364 /* legacy; pass along offset, whatever that might entail */
365 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
367 gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
368 out_data = omap.data;
372 max = dec->channels * wph.block_samples;
374 gint8 *outbuffer = (gint8 *) out_data;
375 gint *reorder_map = dec->channel_reorder_map;
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]]);
381 } else if (width == 16) {
382 gint16 *outbuffer = (gint16 *) out_data;
383 gint *reorder_map = dec->channel_reorder_map;
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]]);
389 } else if (dec->width == 32) {
390 gint32 *outbuffer = (gint32 *) out_data;
391 gint *reorder_map = dec->channel_reorder_map;
393 if (width != depth) {
394 for (i = 0; i < max; i += dec->channels) {
395 for (j = 0; j < dec->channels; j++)
397 (gint32) (dec_data[i + reorder_map[j]] << (width - depth));
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]]);
406 g_assert_not_reached ();
409 gst_buffer_unmap (outbuf, &omap);
410 gst_buffer_unmap (buf, &map);
415 ret = gst_audio_decoder_finish_frame (bdec, outbuf, 1);
419 gst_buffer_unmap (buf, &map);
421 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
422 GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (ret));
430 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Expected framed input"));
431 ret = GST_FLOW_ERROR;
436 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Invalid wavpack header"));
437 ret = GST_FLOW_ERROR;
442 GST_AUDIO_DECODER_ERROR (bdec, 1, LIBRARY, INIT, (NULL),
443 ("error creating Wavpack context"), ret);
448 const gchar *reason = "unknown";
451 reason = WavpackGetErrorMessage (dec->context);
453 reason = "couldn't create decoder context";
455 GST_AUDIO_DECODER_ERROR (bdec, 1, STREAM, DECODE, (NULL),
456 ("decoding error: %s", reason), ret);
458 if (ret == GST_FLOW_OK)
459 gst_audio_decoder_finish_frame (bdec, NULL, 1);
465 gst_wavpack_dec_plugin_init (GstPlugin * plugin)
467 if (!gst_element_register (plugin, "wavpackdec",
468 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_DEC))
470 GST_DEBUG_CATEGORY_INIT (gst_wavpack_dec_debug, "wavpackdec", 0,