Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / ext / wavpack / gstwavpackdec.c
index 69328a5..8f0778d 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+/**
+ * SECTION:element-wavpackdec
+ *
+ * WavpackDec decodes framed (for example by the WavpackParse element)
+ * Wavpack streams and decodes them to raw audio.
+ * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
+ * audio codec that features both lossless and lossy encoding.
+ *
+ * <refsect2>
+ * <title>Example launch line</title>
+ * |[
+ * gst-launch filesrc location=test.wv ! wavpackparse ! wavpackdec ! audioconvert ! audioresample ! autoaudiosink
+ * ]| This pipeline decodes the Wavpack file test.wv into raw audio buffers and
+ * tries to play it back using an automatically found audio sink.
+ * </refsect2>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <gst/gst.h>
 #include <gst/audio/audio.h>
+#include <gst/audio/multichannel.h>
 
 #include <math.h>
 #include <string.h>
@@ -33,8 +55,6 @@
 #include "gstwavpackstreamreader.h"
 
 
-#define WAVPACK_DEC_MAX_ERRORS 16
-
 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_dec_debug);
 #define GST_CAT_DEFAULT gst_wavpack_dec_debug
 
@@ -42,8 +62,8 @@ static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS ("audio/x-wavpack, "
-        "width = (int) { 8, 16, 24, 32 }, "
-        "channels = (int) [ 1, 2 ], "
+        "width = (int) [ 1, 32 ], "
+        "channels = (int) [ 1, 8 ], "
         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
     );
 
@@ -51,78 +71,84 @@ static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_SRC,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS ("audio/x-raw-int, "
-        "width = (int) { 8, 16, 24, 32 }, "
-        "depth = (int) { 8, 16, 24, 32 }, "
-        "channels = (int) [ 1, 2 ], "
+        "width = (int) 8, depth = (int) 8, "
+        "channels = (int) [ 1, 8 ], "
+        "rate = (int) [ 6000, 192000 ], "
+        "endianness = (int) BYTE_ORDER, " "signed = (boolean) true; "
+        "audio/x-raw-int, "
+        "width = (int) 16, depth = (int) 16, "
+        "channels = (int) [ 1, 8 ], "
         "rate = (int) [ 6000, 192000 ], "
-        "endianness = (int) LITTLE_ENDIAN, " "signed = (boolean) true")
+        "endianness = (int) BYTE_ORDER, " "signed = (boolean) true; "
+        "audio/x-raw-int, "
+        "width = (int) 32, depth = (int) 32, "
+        "channels = (int) [ 1, 8 ], "
+        "rate = (int) [ 6000, 192000 ], "
+        "endianness = (int) BYTE_ORDER, " "signed = (boolean) true; ")
     );
 
-static GstFlowReturn gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buffer);
-static gboolean gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event);
+static gboolean gst_wavpack_dec_start (GstAudioDecoder * dec);
+static gboolean gst_wavpack_dec_stop (GstAudioDecoder * dec);
+static gboolean gst_wavpack_dec_set_format (GstAudioDecoder * dec,
+    GstCaps * caps);
+static GstFlowReturn gst_wavpack_dec_handle_frame (GstAudioDecoder * dec,
+    GstBuffer * buffer);
+
 static void gst_wavpack_dec_finalize (GObject * object);
-static GstStateChangeReturn gst_wavpack_dec_change_state (GstElement * element,
-    GstStateChange transition);
-static gboolean gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event);
+static void gst_wavpack_dec_post_tags (GstWavpackDec * dec);
 
-GST_BOILERPLATE (GstWavpackDec, gst_wavpack_dec, GstElement, GST_TYPE_ELEMENT);
+GST_BOILERPLATE (GstWavpackDec, gst_wavpack_dec, GstAudioDecoder,
+    GST_TYPE_AUDIO_DECODER);
 
 static void
 gst_wavpack_dec_base_init (gpointer klass)
 {
-  static const GstElementDetails plugin_details =
-      GST_ELEMENT_DETAILS ("WavePack audio decoder",
-      "Codec/Decoder/Audio",
-      "Decode Wavpack audio data",
-      "Arwed v. Merkatz <v.merkatz@gmx.net>, "
-      "Sebastian Dröge <slomo@circular-chaos.org>");
   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
 
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&src_factory));
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&sink_factory));
-  gst_element_class_set_details (element_class, &plugin_details);
+  gst_element_class_set_details_simple (element_class, "Wavpack audio decoder",
+      "Codec/Decoder/Audio",
+      "Decodes Wavpack audio data",
+      "Arwed v. Merkatz <v.merkatz@gmx.net>, "
+      "Sebastian Dröge <slomo@circular-chaos.org>");
 }
 
 static void
 gst_wavpack_dec_class_init (GstWavpackDecClass * klass)
 {
   GObjectClass *gobject_class = (GObjectClass *) klass;
-  GstElementClass *gstelement_class = (GstElementClass *) klass;
+  GstAudioDecoderClass *base_class = (GstAudioDecoderClass *) (klass);
 
-  gstelement_class->change_state =
-      GST_DEBUG_FUNCPTR (gst_wavpack_dec_change_state);
-  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wavpack_dec_finalize);
+  gobject_class->finalize = gst_wavpack_dec_finalize;
+
+  base_class->start = GST_DEBUG_FUNCPTR (gst_wavpack_dec_start);
+  base_class->stop = GST_DEBUG_FUNCPTR (gst_wavpack_dec_stop);
+  base_class->set_format = GST_DEBUG_FUNCPTR (gst_wavpack_dec_set_format);
+  base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_wavpack_dec_handle_frame);
 }
 
 static void
-gst_wavpack_dec_init (GstWavpackDec * dec, GstWavpackDecClass * gklass)
+gst_wavpack_dec_reset (GstWavpackDec * dec)
 {
-  dec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
-  gst_pad_set_chain_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_wavpack_dec_chain));
-  gst_pad_set_event_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_wavpack_dec_sink_event));
-  gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
-
-  dec->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
-  gst_pad_use_fixed_caps (dec->srcpad);
-  gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
-
-  dec->context = NULL;
-  dec->stream_reader = gst_wavpack_stream_reader_new ();
-
   dec->wv_id.buffer = NULL;
   dec->wv_id.position = dec->wv_id.length = 0;
 
-  dec->error_count = 0;
-
   dec->channels = 0;
+  dec->channel_mask = 0;
   dec->sample_rate = 0;
-  dec->width = 0;
+  dec->depth = 0;
+}
 
-  gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
+static void
+gst_wavpack_dec_init (GstWavpackDec * dec, GstWavpackDecClass * gklass)
+{
+  dec->context = NULL;
+  dec->stream_reader = gst_wavpack_stream_reader_new ();
+
+  gst_wavpack_dec_reset (dec);
 }
 
 static void
@@ -136,97 +162,147 @@ gst_wavpack_dec_finalize (GObject * object)
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
-static void
-gst_wavpack_dec_format_samples (GstWavpackDec * dec, guint8 * dst,
-    int32_t * samples, guint num_samples)
+static gboolean
+gst_wavpack_dec_start (GstAudioDecoder * dec)
 {
-  gint i;
-  int32_t temp;
-
-  switch (dec->width) {
-    case 8:
-      for (i = 0; i < num_samples * dec->channels; ++i)
-        *dst++ = (guint8) (*samples++);
-      break;
-    case 16:
-      for (i = 0; i < num_samples * dec->channels; ++i) {
-        *dst++ = (guint8) (temp = *samples++);
-        *dst++ = (guint8) (temp >> 8);
-      }
-      break;
-    case 24:
-      for (i = 0; i < num_samples * dec->channels; ++i) {
-        *dst++ = (guint8) (temp = *samples++);
-        *dst++ = (guint8) (temp >> 8);
-        *dst++ = (guint8) (temp >> 16);
-      }
-      break;
-    case 32:
-      for (i = 0; i < num_samples * dec->channels; ++i) {
-        *dst++ = (guint8) (temp = *samples++);
-        *dst++ = (guint8) (temp >> 8);
-        *dst++ = (guint8) (temp >> 16);
-        *dst++ = (guint8) (temp >> 24);
-      }
-      break;
-    default:
-      break;
-  }
+  GST_DEBUG_OBJECT (dec, "start");
+
+  /* never mind a few errors */
+  gst_audio_decoder_set_max_errors (dec, 16);
+  /* don't bother us with flushing */
+  gst_audio_decoder_set_drainable (dec, FALSE);
+
+  return TRUE;
 }
 
 static gboolean
-gst_wavpack_dec_clip_outgoing_buffer (GstWavpackDec * dec, GstBuffer * buf)
+gst_wavpack_dec_stop (GstAudioDecoder * dec)
 {
-  gint64 start, stop, cstart, cstop, diff;
+  GstWavpackDec *wpdec = GST_WAVPACK_DEC (dec);
 
-  if (dec->segment.format != GST_FORMAT_TIME)
-    return TRUE;
+  GST_DEBUG_OBJECT (dec, "stop");
 
-  start = GST_BUFFER_TIMESTAMP (buf);
-  stop = start + GST_BUFFER_DURATION (buf);
+  if (wpdec->context) {
+    WavpackCloseFile (wpdec->context);
+    wpdec->context = NULL;
+  }
 
-  if (gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
-          start, stop, &cstart, &cstop)) {
+  gst_wavpack_dec_reset (wpdec);
 
-    diff = cstart - start;
-    if (diff > 0) {
-      GST_BUFFER_TIMESTAMP (buf) = cstart;
-      GST_BUFFER_DURATION (buf) -= diff;
+  return TRUE;
+}
 
-      diff = ((dec->width + 7) >> 3) * dec->channels
-          * GST_CLOCK_TIME_TO_FRAMES (diff, dec->sample_rate);
-      GST_BUFFER_DATA (buf) += diff;
-      GST_BUFFER_SIZE (buf) -= diff;
-    }
+static void
+gst_wavpack_dec_negotiate (GstWavpackDec * dec)
+{
+  GstCaps *caps;
+
+  /* arrange for 1, 2 or 4-byte width == depth output */
+  if (dec->depth == 24)
+    dec->width = 32;
+  else
+    dec->width = dec->depth;
+
+  caps = gst_caps_new_simple ("audio/x-raw-int",
+      "rate", G_TYPE_INT, dec->sample_rate,
+      "channels", G_TYPE_INT, dec->channels,
+      "depth", G_TYPE_INT, dec->width,
+      "width", G_TYPE_INT, dec->width,
+      "endianness", G_TYPE_INT, G_BYTE_ORDER,
+      "signed", G_TYPE_BOOLEAN, TRUE, NULL);
+
+  /* Only set the channel layout for more than two channels
+   * otherwise things break unfortunately */
+  if (dec->channel_mask != 0 && dec->channels > 2)
+    if (!gst_wavpack_set_channel_layout (caps, dec->channel_mask))
+      GST_WARNING_OBJECT (dec, "Failed to set channel layout");
+
+  GST_DEBUG_OBJECT (dec, "setting caps %" GST_PTR_FORMAT, caps);
+
+  /* should always succeed */
+  gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps);
+  gst_caps_unref (caps);
+}
 
-    diff = cstop - stop;
-    if (diff > 0) {
-      GST_BUFFER_DURATION (buf) -= diff;
+static gboolean
+gst_wavpack_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
+{
+  GstWavpackDec *dec = GST_WAVPACK_DEC (bdec);
+  GstStructure *structure = gst_caps_get_structure (caps, 0);
+
+  /* Check if we can set the caps here already */
+  if (gst_structure_get_int (structure, "channels", &dec->channels) &&
+      gst_structure_get_int (structure, "rate", &dec->sample_rate) &&
+      gst_structure_get_int (structure, "width", &dec->depth)) {
+    GstAudioChannelPosition *pos;
+
+    /* If we already have the channel layout set from upstream
+     * take this */
+    if (gst_structure_has_field (structure, "channel-positions")) {
+      pos = gst_audio_get_channel_positions (structure);
+      if (pos != NULL && dec->channels > 2) {
+        GstStructure *new_str = gst_caps_get_structure (caps, 0);
+
+        gst_audio_set_channel_positions (new_str, pos);
+        dec->channel_mask =
+            gst_wavpack_get_channel_mask_from_positions (pos, dec->channels);
+      }
 
-      diff = ((dec->width + 7) >> 3) * dec->channels
-          * GST_CLOCK_TIME_TO_FRAMES (diff, dec->sample_rate);
-      GST_BUFFER_SIZE (buf) -= diff;
+      if (pos != NULL)
+        g_free (pos);
     }
-  } else {
-    GST_DEBUG_OBJECT (dec, "buffer is outside configured segment");
-    return FALSE;
+
+    gst_wavpack_dec_negotiate (dec);
+
+    /* send GST_TAG_AUDIO_CODEC and GST_TAG_BITRATE tags before something
+     * is decoded or after the format has changed */
+    gst_wavpack_dec_post_tags (dec);
   }
 
   return TRUE;
 }
 
+static void
+gst_wavpack_dec_post_tags (GstWavpackDec * dec)
+{
+  GstTagList *list;
+  GstFormat format_time = GST_FORMAT_TIME, format_bytes = GST_FORMAT_BYTES;
+  gint64 duration, size;
+
+  /* try to estimate the average bitrate */
+  if (gst_pad_query_peer_duration (GST_AUDIO_DECODER_SINK_PAD (dec),
+          &format_bytes, &size) &&
+      gst_pad_query_peer_duration (GST_AUDIO_DECODER_SINK_PAD (dec),
+          &format_time, &duration) && size > 0 && duration > 0) {
+    guint64 bitrate;
+
+    list = gst_tag_list_new ();
+
+    bitrate = gst_util_uint64_scale (size, 8 * GST_SECOND, duration);
+    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
+        (guint) bitrate, NULL);
+
+    gst_element_post_message (GST_ELEMENT (dec),
+        gst_message_new_tag (GST_OBJECT (dec), list));
+  }
+}
+
 static GstFlowReturn
-gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buf)
+gst_wavpack_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
 {
   GstWavpackDec *dec;
-  GstBuffer *outbuf;
+  GstBuffer *outbuf = NULL;
   GstFlowReturn ret = GST_FLOW_OK;
   WavpackHeader wph;
-  int32_t *unpack_buf = NULL;
   int32_t decoded, unpacked_size;
   gboolean format_changed;
+  gint width, depth, i, max;
+  gint32 *dec_data = NULL;
+  guint8 *out_data;
+
+  dec = GST_WAVPACK_DEC (bdec);
 
-  dec = GST_WAVPACK_DEC (GST_PAD_PARENT (pad));
+  g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
 
   /* check input, we only accept framed input with complete chunks */
   if (GST_BUFFER_SIZE (buf) < sizeof (WavpackHeader))
@@ -235,7 +311,10 @@ gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buf)
   if (!gst_wavpack_read_header (&wph, GST_BUFFER_DATA (buf)))
     goto invalid_header;
 
-  if (GST_BUFFER_SIZE (buf) != wph.ckSize + 4 * 1 + 4)
+  if (GST_BUFFER_SIZE (buf) < wph.ckSize + 4 * 1 + 4)
+    goto input_not_framed;
+
+  if (!(wph.flags & INITIAL_BLOCK))
     goto input_not_framed;
 
   dec->wv_id.buffer = GST_BUFFER_DATA (buf);
@@ -251,186 +330,148 @@ gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buf)
     dec->context = WavpackOpenFileInputEx (dec->stream_reader,
         &dec->wv_id, NULL, error_msg, OPEN_STREAMING, 0);
 
+    /* expect this to work */
     if (!dec->context) {
-      GST_WARNING ("Couldn't decode buffer: %s", error_msg);
-      dec->error_count++;
-      if (dec->error_count <= WAVPACK_DEC_MAX_ERRORS) {
-        goto out;               /* just return OK for now */
-      } else {
-        goto decode_error;
-      }
+      GST_WARNING_OBJECT (dec, "Couldn't decode buffer: %s", error_msg);
+      goto context_failed;
     }
   }
 
   g_assert (dec->context != NULL);
 
-  dec->error_count = 0;
-
   format_changed =
       (dec->sample_rate != WavpackGetSampleRate (dec->context)) ||
       (dec->channels != WavpackGetNumChannels (dec->context)) ||
-      (dec->width != WavpackGetBitsPerSample (dec->context));
+      (dec->depth != WavpackGetBytesPerSample (dec->context) * 8) ||
+#ifdef WAVPACK_OLD_API
+      (dec->channel_mask != dec->context->config.channel_mask);
+#else
+      (dec->channel_mask != WavpackGetChannelMask (dec->context));
+#endif
 
-  if (!GST_PAD_CAPS (dec->srcpad) || format_changed) {
-    GstCaps *caps;
+  if (!GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)) || format_changed) {
+    gint channel_mask;
 
     dec->sample_rate = WavpackGetSampleRate (dec->context);
     dec->channels = WavpackGetNumChannels (dec->context);
-    dec->width = WavpackGetBitsPerSample (dec->context);
+    dec->depth = WavpackGetBytesPerSample (dec->context) * 8;
 
-    caps = gst_caps_new_simple ("audio/x-raw-int",
-        "rate", G_TYPE_INT, dec->sample_rate,
-        "channels", G_TYPE_INT, dec->channels,
-        "depth", G_TYPE_INT, dec->width,
-        "width", G_TYPE_INT, dec->width,
-        "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
-        "signed", G_TYPE_BOOLEAN, TRUE, NULL);
+#ifdef WAVPACK_OLD_API
+    channel_mask = dec->context->config.channel_mask;
+#else
+    channel_mask = WavpackGetChannelMask (dec->context);
+#endif
+    if (channel_mask == 0)
+      channel_mask = gst_wavpack_get_default_channel_mask (dec->channels);
 
-    GST_DEBUG_OBJECT (dec, "setting caps %" GST_PTR_FORMAT, caps);
+    dec->channel_mask = channel_mask;
 
-    /* should always succeed */
-    gst_pad_set_caps (dec->srcpad, caps);
-    gst_caps_unref (caps);
-  }
+    gst_wavpack_dec_negotiate (dec);
 
-  unpacked_size = wph.block_samples * ((dec->width + 7) >> 3) * dec->channels;
+    /* send GST_TAG_AUDIO_CODEC and GST_TAG_BITRATE tags before something
+     * is decoded or after the format has changed */
+    gst_wavpack_dec_post_tags (dec);
+  }
 
-  /* alloc buffer */
-  ret = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET (buf),
-      unpacked_size, GST_PAD_CAPS (dec->srcpad), &outbuf);
+  /* alloc output buffer */
+  unpacked_size = (dec->width / 8) * wph.block_samples * dec->channels;
+  ret = gst_pad_alloc_buffer (GST_AUDIO_DECODER_SRC_PAD (dec),
+      GST_BUFFER_OFFSET (buf), unpacked_size,
+      GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
 
   if (ret != GST_FLOW_OK)
     goto out;
 
+  dec_data = g_malloc (4 * wph.block_samples * dec->channels);
+  out_data = GST_BUFFER_DATA (outbuf);
+
   /* decode */
-  unpack_buf = g_new (int32_t, wph.block_samples * dec->channels);
-  decoded = WavpackUnpackSamples (dec->context, unpack_buf, wph.block_samples);
+  decoded = WavpackUnpackSamples (dec->context, dec_data, wph.block_samples);
   if (decoded != wph.block_samples)
     goto decode_error;
 
-  /* put samples into outbuf buffer */
-  gst_wavpack_dec_format_samples (dec, GST_BUFFER_DATA (outbuf),
-      unpack_buf, wph.block_samples);
-  gst_buffer_stamp (outbuf, buf);
+  width = dec->width;
+  depth = dec->depth;
+  max = dec->channels * wph.block_samples;
+  if (width == 8) {
+    gint8 *outbuffer = (gint8 *) out_data;
 
-  if (gst_wavpack_dec_clip_outgoing_buffer (dec, outbuf)) {
-    GST_LOG_OBJECT (dec, "pushing buffer with time %" GST_TIME_FORMAT,
-        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
-    ret = gst_pad_push (dec->srcpad, outbuf);
+    for (i = 0; i < max; i--) {
+      *outbuffer++ = (gint8) (dec_data[i]);
+    }
+  } else if (width == 16) {
+    gint16 *outbuffer = (gint16 *) out_data;
+
+    for (i = 0; i < max; i++) {
+      *outbuffer++ = (gint8) (dec_data[i]);
+    }
+  } else if (dec->width == 32) {
+    gint32 *outbuffer = (gint32 *) out_data;
+
+    if (width != depth) {
+      for (i = 0; i < max; i++) {
+        *outbuffer++ = (gint32) (dec_data[i] << (width - depth));
+      }
+    } else {
+      for (i = 0; i < max; i++) {
+        *outbuffer++ = (gint32) dec_data[i];
+      }
+    }
   } else {
-    gst_buffer_unref (outbuf);
+    g_assert_not_reached ();
   }
 
+  g_free (dec_data);
+
+  ret = gst_audio_decoder_finish_frame (bdec, outbuf, 1);
+
 out:
 
   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
     GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (ret));
   }
 
-  g_free (unpack_buf);
-  gst_buffer_unref (buf);
-
   return ret;
 
 /* ERRORS */
 input_not_framed:
   {
     GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Expected framed input"));
-    gst_buffer_unref (buf);
     return GST_FLOW_ERROR;
   }
 invalid_header:
   {
     GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Invalid wavpack header"));
-    gst_buffer_unref (buf);
     return GST_FLOW_ERROR;
   }
-decode_error:
+context_failed:
   {
-    GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
-        ("Failed to decode wavpack stream"));
-    g_free (unpack_buf);
-    gst_buffer_unref (buf);
-    return GST_FLOW_ERROR;
+    GST_AUDIO_DECODER_ERROR (bdec, 1, LIBRARY, INIT, (NULL),
+        ("error creating Wavpack context"), ret);
+    goto out;
   }
-}
-
-static gboolean
-gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event)
-{
-  GstWavpackDec *dec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
-
-  GST_LOG_OBJECT (dec, "Received %s event", GST_EVENT_TYPE_NAME (event));
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_NEWSEGMENT:{
-      GstFormat fmt;
-      gboolean is_update;
-      gint64 start, end, base;
-      gdouble rate;
-
-      gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
-          &end, &base);
-      if (fmt == GST_FORMAT_TIME) {
-        GST_DEBUG ("Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
-            GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
-            GST_TIME_ARGS (end));
-        gst_segment_set_newsegment (&dec->segment, is_update, rate, fmt,
-            start, end, base);
-      } else {
-        gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
-      }
-      break;
+decode_error:
+  {
+    const gchar *reason = "unknown";
+
+    if (dec->context) {
+#ifdef WAVPACK_OLD_API
+      reason = dec->context->error_message;
+#else
+      reason = WavpackGetErrorMessage (dec->context);
+#endif
+    } else {
+      reason = "couldn't create decoder context";
     }
-    default:
-      break;
-  }
-
-  gst_object_unref (dec);
-  return gst_pad_event_default (pad, event);
-}
-
-static GstStateChangeReturn
-gst_wavpack_dec_change_state (GstElement * element, GstStateChange transition)
-{
-  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
-  GstWavpackDec *dec = GST_WAVPACK_DEC (element);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_NULL_TO_READY:
-      break;
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-      gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      break;
-    default:
-      break;
+    GST_AUDIO_DECODER_ERROR (bdec, 1, STREAM, DECODE, (NULL),
+        ("decoding error: %s", reason), ret);
+    g_free (dec_data);
+    if (outbuf)
+      gst_buffer_unref (outbuf);
+    if (ret == GST_FLOW_OK)
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
+    return ret;
   }
-
-  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_READY:
-      if (dec->context) {
-        WavpackCloseFile (dec->context);
-        dec->context = NULL;
-      }
-      dec->wv_id.buffer = NULL;
-      dec->wv_id.position = 0;
-      dec->wv_id.length = 0;
-      dec->channels = 0;
-      dec->sample_rate = 0;
-      dec->width = 0;
-      break;
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      break;
-    default:
-      break;
-  }
-
-  return ret;
 }
 
 gboolean
@@ -439,7 +480,7 @@ gst_wavpack_dec_plugin_init (GstPlugin * plugin)
   if (!gst_element_register (plugin, "wavpackdec",
           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_DEC))
     return FALSE;
-  GST_DEBUG_CATEGORY_INIT (gst_wavpack_dec_debug, "wavpackdec", 0,
-      "wavpack decoder");
+  GST_DEBUG_CATEGORY_INIT (gst_wavpack_dec_debug, "wavpack_dec", 0,
+      "Wavpack decoder");
   return TRUE;
 }