Index: configure.ac
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/configure.ac,v
retrieving revision 1.681
diff -u -p -r1.681 configure.ac
--- configure.ac	10 Jul 2006 14:42:15 -0000	1.681
+++ configure.ac	14 Jul 2006 12:13:05 -0000
@@ -647,9 +647,14 @@ int main () { return 0; }
 dnl *** wavpack ***
 translit(dnm, m, l) AM_CONDITIONAL(USE_WAVPACK, true)
 GST_CHECK_FEATURE(WAVPACK, [wavpack plug-in], wavpack, [
-  PKG_CHECK_MODULES(WAVPACK, wavpack >= 4.2, HAVE_WAVPACK=yes, [
-    HAVE_WAVPACK=no
-    AC_MSG_RESULT(no)
+  PKG_CHECK_MODULES(WAVPACK, wavpack >= 4.33, HAVE_WAVPACK=yes, [
+    PKG_CHECK_MODULES(WAVPACK, wavpack >= 4.20, [
+      HAVE_WAVPACK=yes
+      AC_DEFINE(WAVPACK_OLD_API, 1, [old wavpack API])
+    ],[
+      HAVE_WAVPACK=no
+      AC_MSG_RESULT(no)
+    ])
   ])
   AC_SUBST(WAVPACK_CFLAGS)
   AC_SUBST(WAVPACK_LIBS)
Index: ext/wavpack/Makefile.am
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/Makefile.am,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile.am
--- ext/wavpack/Makefile.am	10 Jun 2006 15:33:18 -0000	1.4
+++ ext/wavpack/Makefile.am	14 Jul 2006 12:13:06 -0000
@@ -7,6 +7,7 @@ libgstwavpack_la_SOURCES = \
 				gstwavpackparse.c \
 				gstwavpackdec.c \
 				gstwavpackenc.c \
+				gstwavpackstreamreader.c \
 				md5.c
 
 libgstwavpack_la_CFLAGS = $(GST_CFLAGS) $(WAVPACK_CFLAGS)
@@ -18,5 +19,6 @@ noinst_HEADERS = \
 		gstwavpackdec.h \
 		gstwavpackenc.h \
 		gstwavpackcommon.h \
+		gstwavpackstreamreader.h \
 		md5.h
 
Index: ext/wavpack/gstwavpack.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpack.c,v
retrieving revision 1.2
diff -u -p -r1.2 gstwavpack.c
--- ext/wavpack/gstwavpack.c	10 Jun 2006 15:33:18 -0000	1.2
+++ ext/wavpack/gstwavpack.c	14 Jul 2006 12:13:06 -0000
@@ -1,5 +1,6 @@
 /* GStreamer wavpack plugin
- * (c) 2004 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2004 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
  *
  * gstwavpack.c: plugin loader
  *
Index: ext/wavpack/gstwavpackcommon.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackcommon.c,v
retrieving revision 1.1
diff -u -p -r1.1 gstwavpackcommon.c
--- ext/wavpack/gstwavpackcommon.c	25 Nov 2005 18:15:51 -0000	1.1
+++ ext/wavpack/gstwavpackcommon.c	14 Jul 2006 12:13:06 -0000
@@ -1,3 +1,27 @@
+/* GStreamer Wavpack plugin
+ * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 1998 - 2005 Conifer Software
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
+ *
+ * gstwavpackcommon.c: common helper functions
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
 #include "gstwavpackcommon.h"
 #include <string.h>
 
@@ -5,10 +29,62 @@ gboolean
 gst_wavpack_read_header (WavpackHeader * header, guint8 * buf)
 {
   g_memmove (header, buf, sizeof (WavpackHeader));
+#ifndef WAVPACK_OLD_API
+  WavpackLittleEndianToNative (header, WavpackHeaderFormat);
+#else
   little_endian_to_native (header, WavpackHeaderFormat);
+#endif
 
-  if (strncmp (header->ckID, "wvpk", 4))
-    return FALSE;
-  else
+  if ((header->ckID[0] == 'w') && (header->ckID[1] == 'v')
+      && (header->ckID[2] == 'p')
+      && (header->ckID[3] == 'k'))
     return TRUE;
+  else
+    return FALSE;
+}
+
+/* copied from the wavpack source and adjusted to our needs. */
+int
+gst_wavpack_read_metadata (GstWavpackMetadata * wpmd, uchar * blockbuff,
+    uchar ** buffptr)
+{
+  WavpackHeader wphdr;
+  uchar *buffend;
+
+  gst_wavpack_read_header (&wphdr, blockbuff);
+  buffend = blockbuff + wphdr.ckSize + 8;
+
+  if (buffend - *buffptr < 2)
+    return FALSE;
+
+  wpmd->id = *(*buffptr)++;
+  wpmd->byte_length = *(*buffptr)++ << 1;
+
+  if (wpmd->id & ID_LARGE) {
+    wpmd->id &= ~ID_LARGE;
+
+    if (buffend - *buffptr < 2)
+      return FALSE;
+
+    wpmd->byte_length += *(*buffptr)++ << 9;
+    wpmd->byte_length += *(*buffptr)++ << 17;
+  }
+
+  if (wpmd->id & ID_ODD_SIZE) {
+    wpmd->id &= ~ID_ODD_SIZE;
+    wpmd->byte_length--;
+  }
+
+  if (wpmd->byte_length) {
+    if (buffend - *buffptr < wpmd->byte_length + (wpmd->byte_length & 1)) {
+      wpmd->data = NULL;
+      return FALSE;
+    }
+
+    wpmd->data = *buffptr;
+    (*buffptr) += wpmd->byte_length + (wpmd->byte_length & 1);
+  } else
+    wpmd->data = NULL;
+
+  return TRUE;
 }
Index: ext/wavpack/gstwavpackcommon.h
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackcommon.h,v
retrieving revision 1.2
diff -u -p -r1.2 gstwavpackcommon.h
--- ext/wavpack/gstwavpackcommon.h	10 Jun 2006 15:33:18 -0000	1.2
+++ ext/wavpack/gstwavpackcommon.h	14 Jul 2006 12:13:06 -0000
@@ -1,4 +1,69 @@
+/* GStreamer Wavpack plugin
+ * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
+ *
+ * gstwavpackcommon.h: common helper functions
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_WAVPACK_COMMON_H__
+#define __GST_WAVPACK_COMMON_H__
+
 #include <gst/gst.h>
 #include <wavpack/wavpack.h>
 
+typedef struct
+{
+  int32_t byte_length;
+  void *data;
+  uchar id;
+} GstWavpackMetadata;
+
+#define ID_UNIQUE               0x3f
+#define ID_OPTIONAL_DATA        0x20
+#define ID_ODD_SIZE             0x40
+#define ID_LARGE                0x80
+
+#define ID_DUMMY                0x0
+#define ID_ENCODER_INFO         0x1
+#define ID_DECORR_TERMS         0x2
+#define ID_DECORR_WEIGHTS       0x3
+#define ID_DECORR_SAMPLES       0x4
+#define ID_ENTROPY_VARS         0x5
+#define ID_HYBRID_PROFILE       0x6
+#define ID_SHAPING_WEIGHTS      0x7
+#define ID_FLOAT_INFO           0x8
+#define ID_INT32_INFO           0x9
+#define ID_WV_BITSTREAM         0xa
+#define ID_WVC_BITSTREAM        0xb
+#define ID_WVX_BITSTREAM        0xc
+#define ID_CHANNEL_INFO         0xd
+
+#define ID_RIFF_HEADER          (ID_OPTIONAL_DATA | 0x1)
+#define ID_RIFF_TRAILER         (ID_OPTIONAL_DATA | 0x2)
+#define ID_REPLAY_GAIN          (ID_OPTIONAL_DATA | 0x3)
+#define ID_CUESHEET             (ID_OPTIONAL_DATA | 0x4)
+#define ID_CONFIG_BLOCK         (ID_OPTIONAL_DATA | 0x5)
+#define ID_MD5_CHECKSUM         (ID_OPTIONAL_DATA | 0x6)
+#define ID_SAMPLE_RATE          (ID_OPTIONAL_DATA | 0x7)
+
+
 gboolean gst_wavpack_read_header (WavpackHeader * header, guint8 * buf);
+int gst_wavpack_read_metadata (GstWavpackMetadata * wpmd, uchar * blockbuff,
+    uchar ** buffptr);
+
+#endif
Index: ext/wavpack/gstwavpackdec.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackdec.c,v
retrieving revision 1.9
diff -u -p -r1.9 gstwavpackdec.c
--- ext/wavpack/gstwavpackdec.c	6 May 2006 00:14:09 -0000	1.9
+++ ext/wavpack/gstwavpackdec.c	14 Jul 2006 12:13:06 -0000
@@ -1,5 +1,7 @@
 /* GStreamer Wavpack plugin
- * (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2006 Tim-Philipp Müller <tim centricular net>
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
  *
  * gstwavpackdec.c: raw Wavpack bitstream decoder
  *
@@ -20,6 +22,7 @@
  */
 
 #include <gst/gst.h>
+#include <gst/audio/audio.h>
 
 #include <math.h>
 #include <string.h>
@@ -27,6 +30,10 @@
 #include <wavpack/wavpack.h>
 #include "gstwavpackdec.h"
 #include "gstwavpackcommon.h"
+#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
@@ -36,7 +43,7 @@ static GstStaticPadTemplate sink_factory
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS ("audio/x-wavpack, "
         "width = (int) { 8, 16, 24, 32 }, "
-        "channels = (int) { 1, 2 }, "
+        "channels = (int) [ 1, 2 ], "
         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
     );
 
@@ -53,66 +60,24 @@ static GstStaticPadTemplate src_factory 
     GST_STATIC_CAPS ("audio/x-raw-int, "
         "width = (int) { 8, 16, 24, 32 }, "
         "depth = (int) { 8, 16, 24, 32 }, "
-        "channels = (int) { 1, 2 }, "
+        "channels = (int) [ 1, 2 ], "
         "rate = (int) [ 6000, 192000 ], "
         "endianness = (int) LITTLE_ENDIAN, " "signed = (boolean) true")
-/*
-        "audio/x-raw-float, "
-        "width = (int) 32, "
-        "channels = (int) { 1, 2 }, "
-        "rate = (int) [ 6000, 192000 ], " "endianness = (int) LITTLE_ENDIAN"
-*/
     );
 
 static GstFlowReturn gst_wavpack_dec_chain (GstPad * pad, GstBuffer * buffer);
 static gboolean gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event);
+static void gst_wavpack_dec_dispose (GObject * object);
+static GstStateChangeReturn gst_wavpack_dec_change_state (GstElement * element,
+    GstStateChange transition);
+static gboolean gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event);
 
-GST_BOILERPLATE (GstWavpackDec, gst_wavpack_dec, GstElement, GST_TYPE_ELEMENT);
-
-static gboolean
-gst_wavpack_dec_setcaps (GstPad * pad, GstCaps * caps)
-{
-  GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
-  GstStructure *structure;
-  GstCaps *srccaps;
-  gint bits, rate, channels;
-
-  structure = gst_caps_get_structure (caps, 0);
-  if (!gst_structure_get_int (structure, "rate", &rate) ||
-      !gst_structure_get_int (structure, "channels", &channels) ||
-      !gst_structure_get_int (structure, "width", &bits)) {
-    return FALSE;
-  }
-
-  wavpackdec->samplerate = rate;
-  wavpackdec->channels = channels;
-  wavpackdec->width = bits;
-
-/* 32-bit output seems to be in fact 32 bit int (e.g. Prod_Girls.wv) */
-/*  if (bits != 32) { */
-  srccaps = gst_caps_new_simple ("audio/x-raw-int",
-      "rate", G_TYPE_INT, wavpackdec->samplerate,
-      "channels", G_TYPE_INT, wavpackdec->channels,
-      "depth", G_TYPE_INT, bits,
-      "width", G_TYPE_INT, bits,
-      "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
-      "signed", G_TYPE_BOOLEAN, TRUE, NULL);
-/*
-  } else {
-    srccaps = gst_caps_new_simple ("audio/x-raw-float",
-        "rate", G_TYPE_INT, wavpackdec->samplerate,
-        "channels", G_TYPE_INT, wavpackdec->channels,
-        "width", G_TYPE_INT, 32,
-        "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL);
-  }
-*/
-/*  gst_pad_set_caps (wavpackdec->sinkpad, caps); */
-
-  gst_pad_set_caps (wavpackdec->srcpad, srccaps);
-  gst_pad_use_fixed_caps (wavpackdec->srcpad);
+#if 0
+static GstPad *gst_wavpack_dec_request_new_pad (GstElement * element,
+    GstPadTemplate * templ, const gchar * name);
+#endif
 
-  return TRUE;
-}
+GST_BOILERPLATE (GstWavpackDec, gst_wavpack_dec, GstElement, GST_TYPE_ELEMENT);
 
 #if 0
 static GstPadLinkReturn
@@ -132,7 +97,7 @@ gst_wavpack_dec_base_init (gpointer klas
       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,
@@ -145,57 +110,18 @@ gst_wavpack_dec_base_init (gpointer klas
 }
 
 static void
-gst_wavpack_dec_dispose (GObject * object)
-{
-  GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (object);
-
-  g_free (wavpackdec->decodebuf);
-  wavpackdec->decodebuf = NULL;
-
-  /* FIXME: what about wavpackdec->stream and wavpackdec->context? (tpm) */
-
-  G_OBJECT_CLASS (parent_class)->dispose (object);
-}
-
-static void
 gst_wavpack_dec_class_init (GstWavpackDecClass * klass)
 {
-  GObjectClass *gobject_class;
-  GstElementClass *gstelement_class;
-
-  gobject_class = (GObjectClass *) klass;
-  gstelement_class = (GstElementClass *) klass;
-
-  gobject_class->dispose = gst_wavpack_dec_dispose;
-}
-
-static gboolean
-gst_wavpack_dec_src_query (GstPad * pad, GstQuery * query)
-{
-  return gst_pad_query_default (pad, query);
-}
-
-static gboolean
-gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event)
-{
-  GstWavpackDec *dec;
-
-  dec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
+  GObjectClass *gobject_class = (GObjectClass *) klass;
+  GstElementClass *gstelement_class = (GstElementClass *) klass;
 
-  GST_LOG_OBJECT (dec, "Received %s event", GST_EVENT_TYPE_NAME (event));
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_NEWSEGMENT:{
-      /* TODO: save current segment so we can do clipping, for now
-       * we'll just leave the clipping to the audio sink */
-      break;
-    }
-    default:
-      break;
-  }
-
-  gst_object_unref (dec);
-
-  return gst_pad_event_default (pad, event);
+  gstelement_class->change_state =
+      GST_DEBUG_FUNCPTR (gst_wavpack_dec_change_state);
+#if 0
+  gstelement_class->request_new_pad =
+      GST_DEBUG_FUNCPTR (gst_wavpack_dec_request_new_pad);
+#endif
+  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_wavpack_dec_dispose);
 }
 
 static void
@@ -206,98 +132,58 @@ gst_wavpack_dec_init (GstWavpackDec * wa
   wavpackdec->sinkpad =
       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
           "sink"), "sink");
-  gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->sinkpad);
-
   gst_pad_set_chain_function (wavpackdec->sinkpad,
       GST_DEBUG_FUNCPTR (gst_wavpack_dec_chain));
-  gst_pad_set_setcaps_function (wavpackdec->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_wavpack_dec_setcaps));
   gst_pad_set_event_function (wavpackdec->sinkpad,
       GST_DEBUG_FUNCPTR (gst_wavpack_dec_sink_event));
-
-#if 0
-  wavpackdec->wvcsinkpad =
-      gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
-          "wvcsink"), "wvcsink");
-  gst_pad_set_link_function (wavpackdec->wvcsinkpad, gst_wavpack_dec_wvclink);
-  gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->wvcsinkpad);
-#endif
-
+  gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->sinkpad);
 
   wavpackdec->srcpad =
       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
           "src"), "src");
   gst_pad_use_fixed_caps (wavpackdec->srcpad);
-
-  gst_pad_set_query_function (wavpackdec->srcpad,
-      GST_DEBUG_FUNCPTR (gst_wavpack_dec_src_query));
-
   gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->srcpad);
 
-  wavpackdec->decodebuf = NULL;
-  wavpackdec->decodebuf_size = 0;
-  wavpackdec->stream = (WavpackStream *) g_malloc0 (sizeof (WavpackStream));
-  wavpackdec->context = (WavpackContext *) g_malloc0 (sizeof (WavpackContext));
-}
+  wavpackdec->context = NULL;
+  wavpackdec->stream_reader = gst_wavpack_stream_reader_new ();
 
-static void
-gst_wavpack_dec_setup_context (GstWavpackDec * wavpackdec, guchar * data,
-    guchar * cdata)
-{
-  WavpackContext *context = wavpackdec->context;
-  WavpackStream *stream = wavpackdec->stream;
-  guint buffer_size;
+  wavpackdec->wv_id.buffer = NULL;
+  wavpackdec->wv_id.position = wavpackdec->wv_id.length = 0;
 
-  memset (context, 0, sizeof (context));
+  wavpackdec->wvc_id.buffer = NULL;
+  wavpackdec->wvc_id.position = wavpackdec->wvc_id.length = 0;
 
-  context->open_flags = 0;
-  context->current_stream = 0;
-  context->num_streams = 1;
+  wavpackdec->segment = gst_segment_new ();
+  wavpackdec->error_count = 0;
 
-  memset (stream, 0, sizeof (stream));
-  context->streams[0] = stream;
+  wavpackdec->wvcsinkpad = NULL;
 
-  gst_wavpack_read_header (&stream->wphdr, data);
-  stream->blockbuff = data;
+  wavpackdec->channels = wavpackdec->sample_rate = wavpackdec->width = 0;
+}
 
-  if (cdata) {
-    context->wvc_flag = TRUE;
-    gst_wavpack_read_header (&stream->wphdr, cdata);
-    stream->block2buff = cdata;
-  } else {
-    context->wvc_flag = FALSE;
-  }
+static void
+gst_wavpack_dec_dispose (GObject * object)
+{
+  GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (object);
 
-  buffer_size =
-      stream->wphdr.block_samples * wavpackdec->channels * sizeof (int32_t);
-  if (wavpackdec->decodebuf_size < buffer_size) {
-    wavpackdec->decodebuf =
-        (int32_t *) g_realloc (wavpackdec->decodebuf, buffer_size);
-    wavpackdec->decodebuf_size = buffer_size;
-  }
+  g_free (wavpackdec->stream_reader);
+  gst_segment_free (wavpackdec->segment);
+  wavpackdec->segment = NULL;
 
-  unpack_init (context);
+  G_OBJECT_CLASS (parent_class)->dispose (object);
 }
 
-static GstBuffer *
-gst_wavpack_dec_format_samples (GstWavpackDec * wavpackdec, int32_t * samples,
-    guint num_samples)
+static void
+gst_wavpack_dec_format_samples (GstWavpackDec * wavpackdec, guint8 * dst,
+    int32_t * samples, guint num_samples)
 {
-  GstBuffer *buf;
   gint i;
-  guint8 *dst;
   int32_t temp;
 
-  buf =
-      gst_buffer_new_and_alloc (num_samples * wavpackdec->width / 8 *
-      wavpackdec->channels);
-
-  dst = (guint8 *) GST_BUFFER_DATA (buf);
-
   switch (wavpackdec->width) {
     case 8:
       for (i = 0; i < num_samples * wavpackdec->channels; ++i)
-        *dst++ = *samples++ + 128;
+        *dst++ = (guint8) (*samples++);
       break;
     case 16:
       for (i = 0; i < num_samples * wavpackdec->channels; ++i) {
@@ -323,8 +209,49 @@ gst_wavpack_dec_format_samples (GstWavpa
     default:
       break;
   }
+}
+
+static gboolean
+gst_wavpack_dec_clip_outgoing_buffer (GstWavpackDec * wavpackdec,
+    GstBuffer * buf)
+{
+
+  gint64 start, stop, cstart, cstop, diff;
 
-  return buf;
+  if (wavpackdec->segment->format != GST_FORMAT_TIME)
+    return TRUE;
+
+  start = GST_BUFFER_TIMESTAMP (buf);
+  stop = start + GST_BUFFER_DURATION (buf);
+
+  if (gst_segment_clip (wavpackdec->segment, GST_FORMAT_TIME,
+          start, stop, &cstart, &cstop)) {
+
+    diff = cstart - start;
+    if (diff > 0) {
+      GST_BUFFER_TIMESTAMP (buf) = cstart;
+      GST_BUFFER_DURATION (buf) -= diff;
+
+      diff = ((wavpackdec->width + 7) >> 3) * wavpackdec->channels
+          * GST_CLOCK_TIME_TO_FRAMES (diff, wavpackdec->sample_rate);
+      GST_BUFFER_DATA (buf) += diff;
+      GST_BUFFER_SIZE (buf) -= diff;
+    }
+
+    diff = cstop - stop;
+    if (diff > 0) {
+      GST_BUFFER_DURATION (buf) -= diff;
+
+      diff = ((wavpackdec->width + 7) >> 3) * wavpackdec->channels
+          * GST_CLOCK_TIME_TO_FRAMES (diff, wavpackdec->sample_rate);
+      GST_BUFFER_SIZE (buf) -= diff;
+    }
+  } else {
+    GST_DEBUG_OBJECT (wavpackdec, "buffer is outside configured segment");
+    return FALSE;
+  }
+
+  return TRUE;
 }
 
 static GstFlowReturn
@@ -334,56 +261,292 @@ gst_wavpack_dec_chain (GstPad * pad, Gst
   GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
   GstBuffer *outbuf, *cbuf = NULL;
   GstFlowReturn ret = GST_FLOW_OK;
+  WavpackHeader wph;
+  int32_t *unpack_buf;
+  int32_t unpacked_sample_count;
+
+  /* we only accept framed input with complete chunks */
+  g_assert (GST_BUFFER_SIZE (buf) >= sizeof (WavpackHeader));
+  gst_wavpack_read_header (&wph, GST_BUFFER_DATA (buf));
+  g_assert (GST_BUFFER_SIZE (buf) ==
+      wph.ckSize + 4 * sizeof (char) + sizeof (uint32_t));
+
+  wavpackdec->wv_id.buffer = GST_BUFFER_DATA (buf);
+  wavpackdec->wv_id.length = GST_BUFFER_SIZE (buf);
+  wavpackdec->wv_id.position = 0;
 
 #if 0
+  /* check whether the correction pad is linked and we can get
+   * the correction chunk that corresponds to our current data */
   if (gst_pad_is_linked (wavpackdec->wvcsinkpad)) {
     if (GST_FLOW_OK != gst_pad_pull_range (wavpackdec->wvcsinkpad,
-            wavpackdec->wvcflushed_bytes, -1, &cbuf)) {
+            GST_BUFFER_OFFSET (buf), -1, &cbuf)) {
       cbuf = NULL;
     } else {
-      wavpackdec->wvcflushed_bytes += GST_BUFFER_SIZE (cbuf);
+      if (!(GST_BUFFER_TIMESTAMP (cbuf) == GST_BUFFER_TIMESTAMP (buf)) ||
+          !(GST_BUFFER_DURATION (cbuf) == GST_BUFFER_DURATION (buf)) ||
+          !(GST_BUFFER_OFFSET (cbuf) == GST_BUFFER_OFFSET (buf)) ||
+          !(GST_BUFFER_OFFSET_END (cbuf) == GST_BUFFER_OFFSET (buf))) {
+        gst_buffer_unref (cbuf);
+        cbuf = NULL;
+      } else {
+        wavpackdec->wvc_id.buffer = GST_BUFFER_DATA (cbuf);
+        wavpackdec->wvc_id.length = GST_BUFFER_SIZE (cbuf);
+        wavpackdec->wvc_id.position = 0;
+      }
     }
-
   }
 #endif
 
-  gst_wavpack_dec_setup_context (wavpackdec, GST_BUFFER_DATA (buf),
-      cbuf ? GST_BUFFER_DATA (cbuf) : NULL);
-  unpack_samples (wavpackdec->context, wavpackdec->decodebuf,
-      wavpackdec->context->streams[0]->wphdr.block_samples);
-  outbuf =
-      gst_wavpack_dec_format_samples (wavpackdec, wavpackdec->decodebuf,
-      wavpackdec->context->streams[0]->wphdr.block_samples);
+  /* create a new wavpack context if there is none yet but if there
+   * was already one (i.e. caps were set on the srcpad) check whether
+   * the new one has the same caps */
+  if (!wavpackdec->context) {
+    gchar error_msg[80];
+
+    wavpackdec->context =
+        WavpackOpenFileInputEx (wavpackdec->stream_reader, &wavpackdec->wv_id,
+        (cbuf) ? &wavpackdec->wvc_id : NULL, error_msg, OPEN_STREAMING, 0);
+    if (!wavpackdec->context) {
+      wavpackdec->error_count++;
+      GST_ELEMENT_WARNING (wavpackdec, LIBRARY, INIT, (NULL),
+          ("Couldn't open buffer for decoding: %s", error_msg));
+      if (wavpackdec->error_count <= WAVPACK_DEC_MAX_ERRORS) {
+        ret = GST_FLOW_OK;
+      } else {
+        ret = GST_FLOW_ERROR;
+      }
+      gst_buffer_unref (buf);
+      if (cbuf) {
+        gst_buffer_unref (cbuf);
+      }
 
-  gst_buffer_stamp (outbuf, buf);
+      gst_object_unref (wavpackdec);
+      return ret;
+    }
+
+    if (GST_PAD_CAPS (wavpackdec->srcpad)) {
+      if ((wavpackdec->sample_rate !=
+              WavpackGetSampleRate (wavpackdec->context))
+          || (wavpackdec->channels !=
+              WavpackGetNumChannels (wavpackdec->context))
+          || (wavpackdec->width !=
+              WavpackGetBitsPerSample (wavpackdec->context))) {
+        gst_buffer_unref (buf);
+        if (cbuf) {
+          gst_buffer_unref (cbuf);
+        }
+
+        GST_ELEMENT_ERROR (wavpackdec, LIBRARY, INIT, (NULL),
+            ("Got Wavpack chunk with changed format settings!"));
 
+        gst_object_unref (wavpackdec);
+        return GST_FLOW_ERROR;
+      }
+    }
+  }
+  wavpackdec->error_count = 0;
+
+  if (!GST_PAD_CAPS (wavpackdec->srcpad)) {
+    GstCaps *caps;
+
+    g_assert (wavpackdec->context);
+
+    wavpackdec->sample_rate = WavpackGetSampleRate (wavpackdec->context);
+    wavpackdec->channels = WavpackGetNumChannels (wavpackdec->context);
+    wavpackdec->width = WavpackGetBitsPerSample (wavpackdec->context);
+    caps = gst_caps_new_simple ("audio/x-raw-int",
+        "rate", G_TYPE_INT, wavpackdec->sample_rate,
+        "channels", G_TYPE_INT, wavpackdec->channels,
+        "depth", G_TYPE_INT, wavpackdec->width,
+        "width", G_TYPE_INT, wavpackdec->width,
+        "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
+        "signed", G_TYPE_BOOLEAN, TRUE, NULL);
+    if (!gst_pad_set_caps (wavpackdec->srcpad, caps)) {
+      gst_caps_unref (caps);
+      WavpackCloseFile (wavpackdec->context);
+      wavpackdec->context = NULL;
+      gst_buffer_unref (buf);
+      if (cbuf) {
+        gst_buffer_unref (cbuf);
+      }
+      GST_ELEMENT_ERROR (wavpackdec, LIBRARY, INIT, (NULL),
+          ("Couldn't set caps on source pad: %" GST_PTR_FORMAT, caps));
+      gst_object_unref (wavpackdec);
+      return GST_FLOW_ERROR;
+    }
+    gst_caps_unref (caps);
+    gst_pad_use_fixed_caps (wavpackdec->srcpad);
+  }
+
+  g_assert (wavpackdec->context);
+  unpack_buf =
+      (int32_t *) g_malloc (sizeof (int32_t) * wph.block_samples *
+      wavpackdec->channels);
+  unpacked_sample_count =
+      WavpackUnpackSamples (wavpackdec->context, unpack_buf, wph.block_samples);
+  g_assert (unpacked_sample_count == wph.block_samples);
+
+  ret =
+      gst_pad_alloc_buffer_and_set_caps (wavpackdec->srcpad,
+      GST_BUFFER_OFFSET (buf),
+      wph.block_samples * ((wavpackdec->width +
+              7) >> 3) * wavpackdec->channels,
+      GST_PAD_CAPS (wavpackdec->srcpad), &outbuf);
+
+  if (GST_FLOW_IS_FATAL (ret)) {
+    WavpackCloseFile (wavpackdec->context);
+    wavpackdec->context = NULL;
+    g_free (unpack_buf);
+    gst_buffer_unref (buf);
+    if (cbuf) {
+      gst_buffer_unref (cbuf);
+    }
+
+    gst_object_unref (wavpackdec);
+    return ret;
+  } else if (ret != GST_FLOW_OK) {
+    g_free (unpack_buf);
+    gst_buffer_unref (buf);
+    if (cbuf) {
+      gst_buffer_unref (cbuf);
+    }
+
+    gst_object_unref (wavpackdec);
+    return ret;
+  }
+
+  gst_wavpack_dec_format_samples (wavpackdec, GST_BUFFER_DATA (outbuf),
+      unpack_buf, wph.block_samples);
+  g_free (unpack_buf);
+  gst_buffer_stamp (outbuf, buf);
   gst_buffer_unref (buf);
   if (cbuf) {
     gst_buffer_unref (cbuf);
   }
 
-  gst_buffer_set_caps (outbuf, GST_PAD_CAPS (wavpackdec->srcpad));
+  if (gst_wavpack_dec_clip_outgoing_buffer (wavpackdec, outbuf)) {
+    GST_LOG_OBJECT (wavpackdec, "pushing buffer with time %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
+    ret = gst_pad_push (wavpackdec->srcpad, outbuf);
+    if (ret != GST_FLOW_OK) {
+      GST_DEBUG_OBJECT (wavpackdec, "pad_push: %s", gst_flow_get_name (ret));
+    }
+  } else {
+    gst_buffer_unref (outbuf);
+  }
 
-  GST_LOG_OBJECT (wavpackdec, "pushing buffer with time %" GST_TIME_FORMAT,
-      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
+  gst_object_unref (wavpackdec);
+  return ret;
+}
+
+static gboolean
+gst_wavpack_dec_sink_event (GstPad * pad, GstEvent * event)
+{
+  GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (gst_pad_get_parent (pad));
+
+  GST_LOG_OBJECT (wavpackdec, "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 (wavpackdec->segment, is_update, rate, fmt,
+            start, end, base);
+      } else {
+        gst_segment_init (wavpackdec->segment, GST_FORMAT_UNDEFINED);
+      }
+      break;
+    }
+    default:
+      break;
+  }
+
+  gst_object_unref (wavpackdec);
+  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 *wavpackdec = GST_WAVPACK_DEC (element);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_READY:
+      break;
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+      gst_segment_init (wavpackdec->segment, GST_FORMAT_UNDEFINED);
+      break;
+    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
+      break;
+    default:
+      break;
+  }
 
-  ret = gst_pad_push (wavpackdec->srcpad, outbuf);
-  if (ret != GST_FLOW_OK) {
-    GST_DEBUG_OBJECT (wavpackdec, "pad_push: %s", gst_flow_get_name (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 (wavpackdec->context) {
+        WavpackCloseFile (wavpackdec->context);
+        wavpackdec->context = NULL;
+      }
+      wavpackdec->wv_id.buffer = NULL;
+      wavpackdec->wv_id.position = wavpackdec->wv_id.length = 0;
+      wavpackdec->wvc_id.buffer = NULL;
+      wavpackdec->wvc_id.position = wavpackdec->wvc_id.length = 0;
+      wavpackdec->error_count = 0;
+      wavpackdec->wvcsinkpad = NULL;
+      wavpackdec->channels = wavpackdec->sample_rate = wavpackdec->width = 0;
+      break;
+    case GST_STATE_CHANGE_READY_TO_NULL:
+      break;
+    default:
+      break;
   }
 
   return ret;
 }
 
+#if 0
+static GstPad *
+gst_wavpack_dec_request_new_pad (GstElement * element,
+    GstPadTemplate * templ, const gchar * name)
+{
+  GstWavpackDec *wavpackdec = GST_WAVPACK_DEC (element);
+  GstPad *pad;
+
+  if (wavpackdec->wvcsinkpad == NULL) {
+    wavpackdec->wvcsinkpad = gst_pad_new_from_template (template, name);
+    gst_pad_set_link_function (wavpackdec->wvcsinkpad, gst_wavpack_dec_wvclink);
+    gst_pad_use_fixed_caps (wavpackdec->wvcsinkpad);
+    gst_element_add_pad (GST_ELEMENT (wavpackdec), wavpackdec->wvcsinkpad);
+    gst_element_no_more_pads (GST_ELEMENT (wavpackdec));
+  } else {
+    pad = NULL;
+  }
+
+  return pad;
+}
+#endif
+
 gboolean
 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");
-
   return TRUE;
 }
Index: ext/wavpack/gstwavpackdec.h
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackdec.h,v
retrieving revision 1.3
diff -u -p -r1.3 gstwavpackdec.h
--- ext/wavpack/gstwavpackdec.h	1 Jun 2006 22:00:23 -0000	1.3
+++ ext/wavpack/gstwavpackdec.h	14 Jul 2006 12:13:06 -0000
@@ -1,5 +1,6 @@
 /* GStreamer Wavpack plugin
- * (c) 2004 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2004 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
  *
  * gstwavpackdec.h: raw Wavpack bitstream decoder
  *
@@ -26,8 +27,9 @@
 
 #include <wavpack/wavpack.h>
 
-G_BEGIN_DECLS
+#include "gstwavpackstreamreader.h"
 
+G_BEGIN_DECLS
 /* #define's don't like whitespacey bits */
 #define GST_TYPE_WAVPACK_DEC \
   (gst_wavpack_dec_get_type())
@@ -39,48 +41,35 @@ G_BEGIN_DECLS
   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WAVPACK_DEC))
 #define GST_IS_WAVPACK_DEC_CLASS(klass) \
   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WAVPACK_DEC))
-
-typedef struct _GstWavpackDec      GstWavpackDec;
+typedef struct _GstWavpackDec GstWavpackDec;
 typedef struct _GstWavpackDecClass GstWavpackDecClass;
 
 struct _GstWavpackDec
 {
   GstElement element;
 
-  GstPad *sinkpad, *srcpad;
-#if 0
-  GstPad *wvcsinkpad;
-#endif
+  GstPad *sinkpad, *wvcsinkpad, *srcpad;
 
   WavpackContext *context;
+  WavpackStreamReader *stream_reader;
 
-  int32_t *decodebuf;
-  guint decodebuf_size;
+  read_id wv_id, wvc_id;
 
-  WavpackStream *stream;
+  GstSegment *segment;
 
-  guint32 samplerate;
-  guint channels;
-  guint width;
-  long frame_length;
-
-  guint64 wvcflushed_bytes;
-  guint64 duration;
-  guint64 wvcduration;
+  guint sample_rate, width, channels;
 
-  guchar *decdata;
-  long *cache;
+  gint error_count;
 };
 
-struct _GstWavpackDecClass 
+struct _GstWavpackDecClass
 {
   GstElementClass parent;
 };
 
 GType gst_wavpack_dec_get_type (void);
 
-gboolean gst_wavpack_dec_plugin_init (GstPlugin *plugin);
+gboolean gst_wavpack_dec_plugin_init (GstPlugin * plugin);
 
 G_END_DECLS
-
 #endif /* __GST_WAVPACK_DEC_H__ */
Index: ext/wavpack/gstwavpackenc.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackenc.c,v
retrieving revision 1.2
diff -u -p -r1.2 gstwavpackenc.c
--- ext/wavpack/gstwavpackenc.c	14 Jun 2006 09:32:27 -0000	1.2
+++ ext/wavpack/gstwavpackenc.c	14 Jul 2006 12:13:07 -0000
@@ -270,15 +270,13 @@ gst_wavpack_enc_init (GstWavpackEnc * wa
       GST_DEBUG_FUNCPTR (gst_wavpack_enc_chain));
   gst_pad_set_event_function (wavpack_enc->sinkpad,
       GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_event));
-  gst_element_add_pad (GST_ELEMENT (wavpack_enc),
-      GST_DEBUG_FUNCPTR (wavpack_enc->sinkpad));
+  gst_element_add_pad (GST_ELEMENT (wavpack_enc), wavpack_enc->sinkpad);
 
   /* setup src pad */
   wavpack_enc->srcpad =
       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
           "src"), "src");
-  gst_element_add_pad (GST_ELEMENT (wavpack_enc),
-      GST_DEBUG_FUNCPTR (wavpack_enc->srcpad));
+  gst_element_add_pad (GST_ELEMENT (wavpack_enc), wavpack_enc->srcpad);
 
   /* initialize object attributes */
   wavpack_enc->wp_config = NULL;
@@ -410,29 +408,31 @@ gst_wavpack_enc_set_wp_config (GstWavpac
           (GST_ELEMENT_GET_CLASS (wavpack_enc), "wvcsrc"), "wvcsrc");
 
       /* try to add correction src pad, don't set correction mode on failure */
-      if (gst_element_add_pad (GST_ELEMENT (wavpack_enc),
-              GST_DEBUG_FUNCPTR (wavpack_enc->wvcsrcpad))) {
-        GstCaps *caps = gst_caps_new_simple ("audio/x-wavpack-correction",
-            "framed", G_TYPE_BOOLEAN, FALSE, NULL);
+      GstCaps *caps = gst_caps_new_simple ("audio/x-wavpack-correction",
+          "framed", G_TYPE_BOOLEAN, FALSE, NULL);
 
-        gst_element_no_more_pads (GST_ELEMENT (wavpack_enc));
+      gst_element_no_more_pads (GST_ELEMENT (wavpack_enc));
+
+      if (!gst_pad_set_caps (wavpack_enc->wvcsrcpad, caps)) {
+        wavpack_enc->correction_mode = 0;
+        GST_ELEMENT_WARNING (wavpack_enc, LIBRARY, INIT, (NULL),
+            ("setting correction caps failed: %", GST_PTR_FORMAT, caps));
+      } else {
+        gst_pad_use_fixed_caps (wavpack_enc->wvcsrcpad);
+
+        if (gst_element_add_pad (GST_ELEMENT (wavpack_enc),
+                wavpack_enc->wvcsrcpad)) {
 
-        if (!gst_pad_set_caps (wavpack_enc->wvcsrcpad, caps)) {
-          wavpack_enc->correction_mode = 0;
-          GST_ELEMENT_WARNING (wavpack_enc, LIBRARY, INIT, (NULL),
-              ("setting correction caps failed: %", GST_PTR_FORMAT, caps));
-        } else {
-          gst_pad_use_fixed_caps (wavpack_enc->wvcsrcpad);
           wavpack_enc->wp_config->flags |= CONFIG_CREATE_WVC;
           if (wavpack_enc->correction_mode == 2) {
             wavpack_enc->wp_config->flags |= CONFIG_OPTIMIZE_WVC;
           }
+        } else {
+          wavpack_enc->correction_mode = 0;
+          GST_ELEMENT_WARNING (wavpack_enc, LIBRARY, INIT, (NULL),
+              ("add correction pad failed. no correction file will be created."));
         }
         gst_caps_unref (caps);
-      } else {
-        wavpack_enc->correction_mode = 0;
-        GST_ELEMENT_WARNING (wavpack_enc, LIBRARY, INIT, (NULL),
-            ("add correction pad failed. no correction file will be created."));
       }
     }
   } else {
@@ -442,6 +442,7 @@ gst_wavpack_enc_set_wp_config (GstWavpac
           ("settings correction mode only has effect if a bitrate is provided."));
     }
   }
+  gst_element_no_more_pads (GST_ELEMENT (wavpack_enc));
 
   /* MD5, setup MD5 context */
   if ((wavpack_enc->md5) && !(wavpack_enc->md5_context)) {
Index: ext/wavpack/gstwavpackparse.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackparse.c,v
retrieving revision 1.7
diff -u -p -r1.7 gstwavpackparse.c
--- ext/wavpack/gstwavpackparse.c	25 Apr 2006 21:56:31 -0000	1.7
+++ ext/wavpack/gstwavpackparse.c	14 Jul 2006 12:13:07 -0000
@@ -1,6 +1,7 @@
 /* GStreamer wavpack plugin
- * (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
- * (c) 2006 Tim-Philipp Müller <tim centricular net>
+ * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2006 Tim-Philipp Müller <tim centricular net>
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
  *
  * gstwavpackparse.c: wavpack file parser
  *
@@ -27,6 +28,7 @@
 
 #include <wavpack/wavpack.h>
 #include "gstwavpackparse.h"
+#include "gstwavpackstreamreader.h"
 #include "gstwavpackcommon.h"
 
 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
@@ -45,7 +47,7 @@ static GstStaticPadTemplate src_factory 
     GST_PAD_SOMETIMES,
     GST_STATIC_CAPS ("audio/x-wavpack, "
         "width = (int) { 8, 16, 24, 32 }, "
-        "channels = (int) { 1, 2 }, "
+        "channels = (int) [ 1, 2 ], "
         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
     );
 
@@ -77,7 +79,7 @@ gst_wavpack_parse_base_init (gpointer kl
       GST_ELEMENT_DETAILS ("WavePack parser",
       "Codec/Demuxer/Audio",
       "Parses Wavpack files",
-      "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,
@@ -137,13 +139,19 @@ gst_wavpack_parse_index_get_entry_from_s
     entry = &g_array_index (wvparse->entries, GstWavpackParseIndexEntry, i);
 
     GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
-        " byte %" G_GINT64_FORMAT, entry->sample_offset, entry->byte_offset);
+        " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
 
     if (entry->sample_offset <= sample_offset &&
         sample_offset < entry->sample_offset_end) {
       GST_LOG_OBJECT (wvparse, "found match");
       return entry;
     }
+
+    /* as the list is sorted and we first look at the latest entry
+     * we can abort searching for an entry if the sample we want is
+     * after the latest one */
+    if (sample_offset >= entry->sample_offset_end)
+      break;
   }
   GST_LOG_OBJECT (wvparse, "no match in index");
   return NULL;
@@ -331,9 +339,7 @@ gst_wavpack_parse_scan_to_find_sample (G
 
   /* now scan forward until we find the chunk we're looking for or hit EOS */
   do {
-    WavpackHeader header = { {0,}
-    , 0,
-    };
+    WavpackHeader header;
     GstBuffer *buf;
 
     buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
@@ -451,6 +457,11 @@ gst_wavpack_parse_handle_seek_event (Gst
       stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
   }
 
+  /* if seek is to something after the end of the stream seek only
+   * to the end. this can be caused by rounding errors */
+  if (start >= wvparse->total_samples)
+    start = wvparse->total_samples;
+
   flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
 
   if (start < 0) {
@@ -628,7 +639,7 @@ static gboolean
 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
     WavpackHeader * header)
 {
-  WavpackMetadata meta;
+  GstWavpackMetadata meta;
   GstCaps *caps = NULL;
   guchar *bufptr;
 
@@ -636,7 +647,7 @@ gst_wavpack_parse_create_src_pad (GstWav
 
   bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
 
-  while (read_metadata_buff (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
+  while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
     switch (meta.id) {
       case ID_WVC_BITSTREAM:{
         caps = gst_caps_new_simple ("audio/x-wavpack-correction",
@@ -646,23 +657,41 @@ gst_wavpack_parse_create_src_pad (GstWav
             (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
         break;
       }
-      case ID_RIFF_HEADER:{
-        WaveHeader wheader;
+      case ID_WV_BITSTREAM:
+      case ID_WVX_BITSTREAM:{
+        WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
+        WavpackContext *wpc;
+        gchar error_msg[80];
+        read_id rid;
+
+        rid.buffer = GST_BUFFER_DATA (buf);
+        rid.length = GST_BUFFER_SIZE (buf);
+        rid.position = 0;
 
-        /* skip RiffChunkHeader and ChunkHeader */
-        g_memmove (&wheader, meta.data + 20, sizeof (WaveHeader));
-        little_endian_to_native (&wheader, WaveHeaderFormat);
-        wvparse->samplerate = wheader.SampleRate;
-        wvparse->channels = wheader.NumChannels;
+        wpc =
+            WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
+
+        if (!wpc)
+          return FALSE;
+
+        wvparse->samplerate = WavpackGetSampleRate (wpc);
+        wvparse->channels = WavpackGetNumChannels (wpc);
         wvparse->total_samples = header->total_samples;
+        if (wvparse->total_samples == (int32_t) - 1)
+          wvparse->total_samples = 0;
+        else
+          wvparse->total_samples--;
+
         caps = gst_caps_new_simple ("audio/x-wavpack",
-            "width", G_TYPE_INT, wheader.BitsPerSample,
+            "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
             "channels", G_TYPE_INT, wvparse->channels,
             "rate", G_TYPE_INT, wvparse->samplerate,
             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
         wvparse->srcpad =
             gst_pad_new_from_template (gst_element_class_get_pad_template
             (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
+        WavpackCloseFile (wpc);
+        g_free (stream_reader);
         break;
       }
       default:{
@@ -685,6 +714,7 @@ gst_wavpack_parse_create_src_pad (GstWav
       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
 
   gst_pad_set_caps (wvparse->srcpad, caps);
+  gst_caps_unref (caps);
   gst_pad_use_fixed_caps (wvparse->srcpad);
 
   gst_object_ref (wvparse->srcpad);
@@ -699,7 +729,7 @@ gst_wavpack_parse_loop (GstElement * ele
 {
   GstWavpackParse *wavpackparse = GST_WAVPACK_PARSE (element);
   GstFlowReturn flow_ret;
-  WavpackHeader header = { {0,}, 0, };
+  WavpackHeader header;
   GstBuffer *buf = NULL;
 
   GST_LOG_OBJECT (wavpackparse, "Current offset: %" G_GINT64_FORMAT,
@@ -753,6 +783,7 @@ gst_wavpack_parse_loop (GstElement * ele
   GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header.block_samples,
       GST_SECOND, wavpackparse->samplerate);
   GST_BUFFER_OFFSET (buf) = header.block_index;
+  GST_BUFFER_OFFSET_END (buf) = header.block_index + header.block_samples;
   gst_buffer_set_caps (buf, GST_PAD_CAPS (wavpackparse->srcpad));
 
   GST_LOG_OBJECT (wavpackparse, "Pushing buffer with time %" GST_TIME_FORMAT,
Index: ext/wavpack/gstwavpackparse.h
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/ext/wavpack/gstwavpackparse.h,v
retrieving revision 1.4
diff -u -p -r1.4 gstwavpackparse.h
--- ext/wavpack/gstwavpackparse.h	1 Jun 2006 22:00:23 -0000	1.4
+++ ext/wavpack/gstwavpackparse.h	14 Jul 2006 12:13:07 -0000
@@ -1,5 +1,6 @@
 /* GStreamer wavpack plugin
- * (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
  *
  * gstwavpackparse.h: wavpack file parser
  *
@@ -25,7 +26,6 @@
 #include <gst/gst.h>
 
 G_BEGIN_DECLS
-
 /* #define's don't like whitespacey bits */
 #define GST_TYPE_WAVPACK_PARSE \
   (gst_wavpack_parse_get_type())
@@ -37,52 +37,51 @@ G_BEGIN_DECLS
   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WAVPACK_PARSE))
 #define GST_IS_WAVPACK_PARSE_CLASS(klass) \
   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WAVPACK_PARSE))
-
-typedef struct _GstWavpackParse           GstWavpackParse;
-typedef struct _GstWavpackParseClass      GstWavpackParseClass;
+typedef struct _GstWavpackParse GstWavpackParse;
+typedef struct _GstWavpackParseClass GstWavpackParseClass;
 typedef struct _GstWavpackParseIndexEntry GstWavpackParseIndexEntry;
 
-struct _GstWavpackParseIndexEntry {
-  gint64 byte_offset;          /* byte offset of this chunk  */
-  gint64 sample_offset;        /* first sample in this chunk */
-  gint64 sample_offset_end;    /* first sample in next chunk  */
+struct _GstWavpackParseIndexEntry
+{
+  gint64 byte_offset;           /* byte offset of this chunk  */
+  gint64 sample_offset;         /* first sample in this chunk */
+  gint64 sample_offset_end;     /* first sample in next chunk  */
 };
 
 struct _GstWavpackParse
 {
-  GstElement     element;
+  GstElement element;
 
-  GstPad        *sinkpad;
-  GstPad        *srcpad;
-  
-  guint          samplerate;
-  guint          channels;
-  guint          total_samples;
+  GstPad *sinkpad;
+  GstPad *srcpad;
 
-  gboolean       need_newsegment;
+  guint samplerate;
+  guint channels;
+  guint total_samples;
 
-  gint64         current_offset;   /* byte offset on sink pad */
-  gint64         upstream_length;  /* length of file in bytes */
+  gboolean need_newsegment;
 
-  GstSegment     segment;     /* the currently configured segment, in
-                               * samples/audio frames (DEFAULT format) */
+  gint64 current_offset;        /* byte offset on sink pad */
+  gint64 upstream_length;       /* length of file in bytes */
+
+  GstSegment segment;           /* the currently configured segment, in
+                                 * samples/audio frames (DEFAULT format) */
 
   /* Array of GstWavpackParseIndexEntry structs, mapping known
    * sample offsets to byte offsets. Is kept increasing without
    * gaps (ie. append only and consecutive entries must always
    * map to consecutive chunks in the file). */
-  GArray        *entries;
+  GArray *entries;
 };
 
-struct _GstWavpackParseClass 
+struct _GstWavpackParseClass
 {
   GstElementClass parent;
 };
 
 GType gst_wavpack_parse_get_type (void);
 
-gboolean gst_wavpack_parse_plugin_init (GstPlugin *plugin);
+gboolean gst_wavpack_parse_plugin_init (GstPlugin * plugin);
 
 G_END_DECLS
-
 #endif /* __GST_WAVPACK_PARSE_H__ */
Index: ext/wavpack/gstwavpackstreamreader.c
===================================================================
RCS file: ext/wavpack/gstwavpackstreamreader.c
diff -N ext/wavpack/gstwavpackstreamreader.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ext/wavpack/gstwavpackstreamreader.c	14 Jul 2006 12:13:07 -0000
@@ -0,0 +1,106 @@
+/* GStreamer Wavpack plugin
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
+ *
+ * gstwavpackstreamreader.c: stream reader used for decoding
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <string.h>
+#include <math.h>
+#include <gst/gst.h>
+
+#include "gstwavpackstreamreader.h"
+
+static int32_t
+gst_wavpack_stream_reader_read_bytes (void *id, void *data, int32_t bcount)
+{
+  read_id *rid = (read_id *) id;
+  uint32_t left = rid->length - rid->position;
+  uint32_t to_read = MIN (left, bcount);
+
+  if (to_read > 0) {
+    g_memmove (data, rid->buffer + rid->position, to_read);
+    rid->position += to_read;
+    return to_read;
+  } else {
+    return 0;
+  }
+}
+
+static uint32_t
+gst_wavpack_stream_reader_get_pos (void *id)
+{
+  return ((read_id *) id)->position;
+}
+
+static int
+gst_wavpack_stream_reader_set_pos_abs (void *id, uint32_t pos)
+{
+  return -1;
+}
+
+static int
+gst_wavpack_stream_reader_set_pos_rel (void *id, int32_t delta, int mode)
+{
+  return -1;
+}
+
+static int
+gst_wavpack_stream_reader_push_back_byte (void *id, int c)
+{
+  read_id *rid = (read_id *) id;
+
+  rid->position -= 1;
+  if (rid->position < 0)
+    rid->position = 0;
+  return rid->position;
+}
+
+static uint32_t
+gst_wavpack_stream_reader_get_length (void *id)
+{
+  return ((read_id *) id)->length;
+}
+
+static int
+gst_wavpack_stream_reader_can_seek (void *id)
+{
+  return FALSE;
+}
+
+static int32_t
+gst_wavpack_stream_reader_write_bytes (void *id, void *data, int32_t bcount)
+{
+  return 0;
+}
+
+WavpackStreamReader *
+gst_wavpack_stream_reader_new ()
+{
+  WavpackStreamReader *stream_reader =
+      (WavpackStreamReader *) g_malloc0 (sizeof (WavpackStreamReader));
+  stream_reader->read_bytes = gst_wavpack_stream_reader_read_bytes;
+  stream_reader->get_pos = gst_wavpack_stream_reader_get_pos;
+  stream_reader->set_pos_abs = gst_wavpack_stream_reader_set_pos_abs;
+  stream_reader->set_pos_rel = gst_wavpack_stream_reader_set_pos_rel;
+  stream_reader->push_back_byte = gst_wavpack_stream_reader_push_back_byte;
+  stream_reader->get_length = gst_wavpack_stream_reader_get_length;
+  stream_reader->can_seek = gst_wavpack_stream_reader_can_seek;
+  stream_reader->write_bytes = gst_wavpack_stream_reader_write_bytes;
+
+  return stream_reader;
+}
Index: ext/wavpack/gstwavpackstreamreader.h
===================================================================
RCS file: ext/wavpack/gstwavpackstreamreader.h
diff -N ext/wavpack/gstwavpackstreamreader.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ext/wavpack/gstwavpackstreamreader.h	14 Jul 2006 12:13:07 -0000
@@ -0,0 +1,36 @@
+/* GStreamer Wavpack plugin
+ * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
+ *
+ * gstwavpackstreamreader.h: stream reader used for decoding
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_WAVPACK_STREAM_READER_H__
+#define __GST_WAVPACK_STREAM_READER_H__
+
+#include <wavpack/wavpack.h>
+
+typedef struct
+{
+  guint8 *buffer;
+  uint32_t length;
+  uint32_t position;
+} read_id;
+
+WavpackStreamReader *gst_wavpack_stream_reader_new ();
+
+#endif
