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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 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>
47 #include <gst/audio/multichannel.h>
52 #include <wavpack/wavpack.h>
53 #include "gstwavpackdec.h"
54 #include "gstwavpackcommon.h"
55 #include "gstwavpackstreamreader.h"
58 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_dec_debug);
59 #define GST_CAT_DEFAULT gst_wavpack_dec_debug
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("audio/x-wavpack, "
65 "width = (int) [ 1, 32 ], "
66 "channels = (int) [ 1, 8 ], "
67 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
70 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
73 GST_STATIC_CAPS ("audio/x-raw-int, "
74 "width = (int) 8, depth = (int) 8, "
75 "channels = (int) [ 1, 8 ], "
76 "rate = (int) [ 6000, 192000 ], "
77 "endianness = (int) BYTE_ORDER, " "signed = (boolean) true; "
79 "width = (int) 16, depth = (int) 16, "
80 "channels = (int) [ 1, 8 ], "
81 "rate = (int) [ 6000, 192000 ], "
82 "endianness = (int) BYTE_ORDER, " "signed = (boolean) true; "
84 "width = (int) 32, depth = (int) 32, "
85 "channels = (int) [ 1, 8 ], "
86 "rate = (int) [ 6000, 192000 ], "
87 "endianness = (int) BYTE_ORDER, " "signed = (boolean) true; ")
90 static gboolean gst_wavpack_dec_start (GstAudioDecoder * dec);
91 static gboolean gst_wavpack_dec_stop (GstAudioDecoder * dec);
92 static gboolean gst_wavpack_dec_set_format (GstAudioDecoder * dec,
94 static GstFlowReturn gst_wavpack_dec_handle_frame (GstAudioDecoder * dec,
97 static void gst_wavpack_dec_finalize (GObject * object);
98 static void gst_wavpack_dec_post_tags (GstWavpackDec * dec);
100 GST_BOILERPLATE (GstWavpackDec, gst_wavpack_dec, GstAudioDecoder,
101 GST_TYPE_AUDIO_DECODER);
104 gst_wavpack_dec_base_init (gpointer klass)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108 gst_element_class_add_pad_template (element_class,
109 gst_static_pad_template_get (&src_factory));
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&sink_factory));
112 gst_element_class_set_details_simple (element_class, "Wavpack audio decoder",
113 "Codec/Decoder/Audio",
114 "Decodes Wavpack audio data",
115 "Arwed v. Merkatz <v.merkatz@gmx.net>, "
116 "Sebastian Dröge <slomo@circular-chaos.org>");
120 gst_wavpack_dec_class_init (GstWavpackDecClass * klass)
122 GObjectClass *gobject_class = (GObjectClass *) klass;
123 GstAudioDecoderClass *base_class = (GstAudioDecoderClass *) (klass);
125 gobject_class->finalize = gst_wavpack_dec_finalize;
127 base_class->start = GST_DEBUG_FUNCPTR (gst_wavpack_dec_start);
128 base_class->stop = GST_DEBUG_FUNCPTR (gst_wavpack_dec_stop);
129 base_class->set_format = GST_DEBUG_FUNCPTR (gst_wavpack_dec_set_format);
130 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_wavpack_dec_handle_frame);
134 gst_wavpack_dec_reset (GstWavpackDec * dec)
136 dec->wv_id.buffer = NULL;
137 dec->wv_id.position = dec->wv_id.length = 0;
140 dec->channel_mask = 0;
141 dec->sample_rate = 0;
146 gst_wavpack_dec_init (GstWavpackDec * dec, GstWavpackDecClass * gklass)
149 dec->stream_reader = gst_wavpack_stream_reader_new ();
151 gst_wavpack_dec_reset (dec);
155 gst_wavpack_dec_finalize (GObject * object)
157 GstWavpackDec *dec = GST_WAVPACK_DEC (object);
159 g_free (dec->stream_reader);
160 dec->stream_reader = NULL;
162 G_OBJECT_CLASS (parent_class)->finalize (object);
166 gst_wavpack_dec_start (GstAudioDecoder * dec)
168 GST_DEBUG_OBJECT (dec, "start");
170 /* never mind a few errors */
171 gst_audio_decoder_set_max_errors (dec, 16);
172 /* don't bother us with flushing */
173 gst_audio_decoder_set_drainable (dec, FALSE);
179 gst_wavpack_dec_stop (GstAudioDecoder * dec)
181 GstWavpackDec *wpdec = GST_WAVPACK_DEC (dec);
183 GST_DEBUG_OBJECT (dec, "stop");
185 if (wpdec->context) {
186 WavpackCloseFile (wpdec->context);
187 wpdec->context = NULL;
190 gst_wavpack_dec_reset (wpdec);
196 gst_wavpack_dec_negotiate (GstWavpackDec * dec)
200 /* arrange for 1, 2 or 4-byte width == depth output */
201 if (dec->depth == 24)
204 dec->width = dec->depth;
206 caps = gst_caps_new_simple ("audio/x-raw-int",
207 "rate", G_TYPE_INT, dec->sample_rate,
208 "channels", G_TYPE_INT, dec->channels,
209 "depth", G_TYPE_INT, dec->width,
210 "width", G_TYPE_INT, dec->width,
211 "endianness", G_TYPE_INT, G_BYTE_ORDER,
212 "signed", G_TYPE_BOOLEAN, TRUE, NULL);
214 /* Only set the channel layout for more than two channels
215 * otherwise things break unfortunately */
216 if (dec->channel_mask != 0 && dec->channels > 2)
217 if (!gst_wavpack_set_channel_layout (caps, dec->channel_mask))
218 GST_WARNING_OBJECT (dec, "Failed to set channel layout");
220 GST_DEBUG_OBJECT (dec, "setting caps %" GST_PTR_FORMAT, caps);
222 /* should always succeed */
223 gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps);
224 gst_caps_unref (caps);
228 gst_wavpack_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
230 GstWavpackDec *dec = GST_WAVPACK_DEC (bdec);
231 GstStructure *structure = gst_caps_get_structure (caps, 0);
233 /* Check if we can set the caps here already */
234 if (gst_structure_get_int (structure, "channels", &dec->channels) &&
235 gst_structure_get_int (structure, "rate", &dec->sample_rate) &&
236 gst_structure_get_int (structure, "width", &dec->depth)) {
237 GstAudioChannelPosition *pos;
239 /* If we already have the channel layout set from upstream
241 if (gst_structure_has_field (structure, "channel-positions")) {
242 pos = gst_audio_get_channel_positions (structure);
243 if (pos != NULL && dec->channels > 2) {
244 GstStructure *new_str = gst_caps_get_structure (caps, 0);
246 gst_audio_set_channel_positions (new_str, pos);
248 gst_wavpack_get_channel_mask_from_positions (pos, dec->channels);
255 gst_wavpack_dec_negotiate (dec);
257 /* send GST_TAG_AUDIO_CODEC and GST_TAG_BITRATE tags before something
258 * is decoded or after the format has changed */
259 gst_wavpack_dec_post_tags (dec);
266 gst_wavpack_dec_post_tags (GstWavpackDec * dec)
269 GstFormat format_time = GST_FORMAT_TIME, format_bytes = GST_FORMAT_BYTES;
270 gint64 duration, size;
272 /* try to estimate the average bitrate */
273 if (gst_pad_query_peer_duration (GST_AUDIO_DECODER_SINK_PAD (dec),
274 &format_bytes, &size) &&
275 gst_pad_query_peer_duration (GST_AUDIO_DECODER_SINK_PAD (dec),
276 &format_time, &duration) && size > 0 && duration > 0) {
279 list = gst_tag_list_new ();
281 bitrate = gst_util_uint64_scale (size, 8 * GST_SECOND, duration);
282 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
283 (guint) bitrate, NULL);
285 gst_element_post_message (GST_ELEMENT (dec),
286 gst_message_new_tag (GST_OBJECT (dec), list));
291 gst_wavpack_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
294 GstBuffer *outbuf = NULL;
295 GstFlowReturn ret = GST_FLOW_OK;
297 int32_t decoded, unpacked_size;
298 gboolean format_changed;
299 gint width, depth, i, max;
300 gint32 *dec_data = NULL;
303 dec = GST_WAVPACK_DEC (bdec);
305 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
307 /* check input, we only accept framed input with complete chunks */
308 if (GST_BUFFER_SIZE (buf) < sizeof (WavpackHeader))
309 goto input_not_framed;
311 if (!gst_wavpack_read_header (&wph, GST_BUFFER_DATA (buf)))
314 if (GST_BUFFER_SIZE (buf) < wph.ckSize + 4 * 1 + 4)
315 goto input_not_framed;
317 if (!(wph.flags & INITIAL_BLOCK))
318 goto input_not_framed;
320 dec->wv_id.buffer = GST_BUFFER_DATA (buf);
321 dec->wv_id.length = GST_BUFFER_SIZE (buf);
322 dec->wv_id.position = 0;
324 /* create a new wavpack context if there is none yet but if there
325 * was already one (i.e. caps were set on the srcpad) check whether
326 * the new one has the same caps */
330 dec->context = WavpackOpenFileInputEx (dec->stream_reader,
331 &dec->wv_id, NULL, error_msg, OPEN_STREAMING, 0);
333 /* expect this to work */
335 GST_WARNING_OBJECT (dec, "Couldn't decode buffer: %s", error_msg);
340 g_assert (dec->context != NULL);
343 (dec->sample_rate != WavpackGetSampleRate (dec->context)) ||
344 (dec->channels != WavpackGetNumChannels (dec->context)) ||
345 (dec->depth != WavpackGetBytesPerSample (dec->context) * 8) ||
346 #ifdef WAVPACK_OLD_API
347 (dec->channel_mask != dec->context->config.channel_mask);
349 (dec->channel_mask != WavpackGetChannelMask (dec->context));
352 if (!GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)) || format_changed) {
355 dec->sample_rate = WavpackGetSampleRate (dec->context);
356 dec->channels = WavpackGetNumChannels (dec->context);
357 dec->depth = WavpackGetBytesPerSample (dec->context) * 8;
359 #ifdef WAVPACK_OLD_API
360 channel_mask = dec->context->config.channel_mask;
362 channel_mask = WavpackGetChannelMask (dec->context);
364 if (channel_mask == 0)
365 channel_mask = gst_wavpack_get_default_channel_mask (dec->channels);
367 dec->channel_mask = channel_mask;
369 gst_wavpack_dec_negotiate (dec);
371 /* send GST_TAG_AUDIO_CODEC and GST_TAG_BITRATE tags before something
372 * is decoded or after the format has changed */
373 gst_wavpack_dec_post_tags (dec);
376 /* alloc output buffer */
377 unpacked_size = (dec->width / 8) * wph.block_samples * dec->channels;
378 ret = gst_pad_alloc_buffer (GST_AUDIO_DECODER_SRC_PAD (dec),
379 GST_BUFFER_OFFSET (buf), unpacked_size,
380 GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
382 if (ret != GST_FLOW_OK)
385 dec_data = g_malloc (4 * wph.block_samples * dec->channels);
386 out_data = GST_BUFFER_DATA (outbuf);
389 decoded = WavpackUnpackSamples (dec->context, dec_data, wph.block_samples);
390 if (decoded != wph.block_samples)
395 max = dec->channels * wph.block_samples;
397 gint8 *outbuffer = (gint8 *) out_data;
399 for (i = 0; i < max; i--) {
400 *outbuffer++ = (gint8) (dec_data[i]);
402 } else if (width == 16) {
403 gint16 *outbuffer = (gint16 *) out_data;
405 for (i = 0; i < max; i++) {
406 *outbuffer++ = (gint8) (dec_data[i]);
408 } else if (dec->width == 32) {
409 gint32 *outbuffer = (gint32 *) out_data;
411 if (width != depth) {
412 for (i = 0; i < max; i++) {
413 *outbuffer++ = (gint32) (dec_data[i] << (width - depth));
416 for (i = 0; i < max; i++) {
417 *outbuffer++ = (gint32) dec_data[i];
421 g_assert_not_reached ();
426 ret = gst_audio_decoder_finish_frame (bdec, outbuf, 1);
430 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
431 GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (ret));
439 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Expected framed input"));
440 return GST_FLOW_ERROR;
444 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Invalid wavpack header"));
445 return GST_FLOW_ERROR;
449 GST_AUDIO_DECODER_ERROR (bdec, 1, LIBRARY, INIT, (NULL),
450 ("error creating Wavpack context"), ret);
455 const gchar *reason = "unknown";
458 #ifdef WAVPACK_OLD_API
459 reason = dec->context->error_message;
461 reason = WavpackGetErrorMessage (dec->context);
464 reason = "couldn't create decoder context";
466 GST_AUDIO_DECODER_ERROR (bdec, 1, STREAM, DECODE, (NULL),
467 ("decoding error: %s", reason), ret);
470 gst_buffer_unref (outbuf);
471 if (ret == GST_FLOW_OK)
472 gst_audio_decoder_finish_frame (bdec, NULL, 1);
478 gst_wavpack_dec_plugin_init (GstPlugin * plugin)
480 if (!gst_element_register (plugin, "wavpackdec",
481 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_DEC))
483 GST_DEBUG_CATEGORY_INIT (gst_wavpack_dec_debug, "wavpack_dec", 0,