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