Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / ext / speex / gstspeexenc.c
index bd84ea7..f427f08 100644 (file)
@@ -1,4 +1,4 @@
-/* GStreamer
+/* GStreamer Speex Encoder
  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
  *
  * This library is free software; you can redistribute it and/or
  * Boston, MA 02111-1307, USA.
  */
 
+/**
+ * SECTION:element-speexenc
+ * @see_also: speexdec, oggmux
+ *
+ * This element encodes audio as a Speex stream.
+ * <ulink url="http://www.speex.org/">Speex</ulink> is a royalty-free
+ * audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
+ * Foundation</ulink>.
+ *
+ * <refsect2>
+ * <title>Example pipelines</title>
+ * |[
+ * gst-launch audiotestsrc num-buffers=100 ! speexenc ! oggmux ! filesink location=beep.ogg
+ * ]| Encode an Ogg/Speex file.
+ * </refsect2>
+ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 
 #include <gst/gsttagsetter.h>
 #include <gst/tag/tag.h>
+#include <gst/audio/audio.h>
 #include "gstspeexenc.h"
 
-GST_DEBUG_CATEGORY (speexenc_debug);
+GST_DEBUG_CATEGORY_STATIC (speexenc_debug);
 #define GST_CAT_DEFAULT speexenc_debug
 
-static GstPadTemplate *gst_speexenc_src_template, *gst_speexenc_sink_template;
-
-/* elementfactory information */
-GstElementDetails speexenc_details = {
-  "Speex encoder",
-  "Codec/Encoder/Audio",
-  "Encodes audio in Speex format",
-  "Wim Taymans <wim@fluendo.com>",
-};
-
-/* GstSpeexEnc signals and args */
-enum
-{
-  /* FILL ME */
-  LAST_SIGNAL
-};
-
-#define DEFAULT_QUALITY                8.0
-#define DEFAULT_BITRATE                0
-#define DEFAULT_VBR            FALSE
-#define DEFAULT_ABR            0
-#define DEFAULT_VAD            FALSE
-#define DEFAULT_DTX            FALSE
-#define DEFAULT_COMPLEXITY     3
-#define DEFAULT_NFRAMES                1
+#define FORMAT_STR GST_AUDIO_NE(S16)
+
+static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("audio/x-raw, "
+        "format = (string) " FORMAT_STR ", "
+        "rate = (int) [ 6000, 48000 ], " "channels = (int) [ 1, 2 ]")
+    );
+
+static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("audio/x-speex, "
+        "rate = (int) [ 6000, 48000 ], " "channels = (int) [ 1, 2]")
+    );
+
+#define DEFAULT_QUALITY         8.0
+#define DEFAULT_BITRATE         0
+#define DEFAULT_MODE            GST_SPEEX_ENC_MODE_AUTO
+#define DEFAULT_VBR             FALSE
+#define DEFAULT_ABR             0
+#define DEFAULT_VAD             FALSE
+#define DEFAULT_DTX             FALSE
+#define DEFAULT_COMPLEXITY      3
+#define DEFAULT_NFRAMES         1
 
 enum
 {
-  ARG_0,
-  ARG_QUALITY,
-  ARG_BITRATE,
-  ARG_VBR,
-  ARG_ABR,
-  ARG_VAD,
-  ARG_DTX,
-  ARG_COMPLEXITY,
-  ARG_NFRAMES,
-  ARG_LAST_MESSAGE
+  PROP_0,
+  PROP_QUALITY,
+  PROP_BITRATE,
+  PROP_MODE,
+  PROP_VBR,
+  PROP_ABR,
+  PROP_VAD,
+  PROP_DTX,
+  PROP_COMPLEXITY,
+  PROP_NFRAMES,
+  PROP_LAST_MESSAGE
 };
 
-#if 0
-static const GstFormat *
-gst_speexenc_get_formats (GstPad * pad)
+#define GST_TYPE_SPEEX_ENC_MODE (gst_speex_enc_mode_get_type())
+static GType
+gst_speex_enc_mode_get_type (void)
 {
-  static const GstFormat src_formats[] = {
-    GST_FORMAT_BYTES,
-    GST_FORMAT_TIME,
-    0
-  };
-  static const GstFormat sink_formats[] = {
-    GST_FORMAT_BYTES,
-    GST_FORMAT_DEFAULT,
-    GST_FORMAT_TIME,
-    0
+  static GType speex_enc_mode_type = 0;
+  static const GEnumValue speex_enc_modes[] = {
+    {GST_SPEEX_ENC_MODE_AUTO, "Auto", "auto"},
+    {GST_SPEEX_ENC_MODE_UWB, "Ultra Wide Band", "uwb"},
+    {GST_SPEEX_ENC_MODE_WB, "Wide Band", "wb"},
+    {GST_SPEEX_ENC_MODE_NB, "Narrow Band", "nb"},
+    {0, NULL, NULL},
   };
-
-  return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
+  if (G_UNLIKELY (speex_enc_mode_type == 0)) {
+    speex_enc_mode_type = g_enum_register_static ("GstSpeexEncMode",
+        speex_enc_modes);
+  }
+  return speex_enc_mode_type;
 }
-#endif
 
-static void gst_speexenc_base_init (gpointer g_class);
-static void gst_speexenc_class_init (GstSpeexEncClass * klass);
-static void gst_speexenc_init (GstSpeexEnc * speexenc);
+static void gst_speex_enc_finalize (GObject * object);
 
-static gboolean gst_speexenc_sinkevent (GstPad * pad, GstEvent * event);
-static GstFlowReturn gst_speexenc_chain (GstPad * pad, GstBuffer * buf);
-static gboolean gst_speexenc_setup (GstSpeexEnc * speexenc);
+static gboolean gst_speex_enc_setup (GstSpeexEnc * enc);
 
-static void gst_speexenc_get_property (GObject * object, guint prop_id,
+static void gst_speex_enc_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
-static void gst_speexenc_set_property (GObject * object, guint prop_id,
+static void gst_speex_enc_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
-static GstStateChangeReturn gst_speexenc_change_state (GstElement * element,
-    GstStateChange transition);
-
-static GstElementClass *parent_class = NULL;
 
-/*static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 }; */
+static GstFlowReturn gst_speex_enc_encode (GstSpeexEnc * enc, GstBuffer * buf);
 
-GType
-gst_speexenc_get_type (void)
-{
-  static GType speexenc_type = 0;
-
-  if (!speexenc_type) {
-    static const GTypeInfo speexenc_info = {
-      sizeof (GstSpeexEncClass),
-      gst_speexenc_base_init,
-      NULL,
-      (GClassInitFunc) gst_speexenc_class_init,
-      NULL,
-      NULL,
-      sizeof (GstSpeexEnc),
-      0,
-      (GInstanceInitFunc) gst_speexenc_init,
-    };
-    static const GInterfaceInfo tag_setter_info = {
-      NULL,
-      NULL,
-      NULL
-    };
-
-    speexenc_type =
-        g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info,
-        0);
-
-    g_type_add_interface_static (speexenc_type, GST_TYPE_TAG_SETTER,
-        &tag_setter_info);
-
-    GST_DEBUG_CATEGORY_INIT (speexenc_debug, "speexenc", 0, "Speex encoder");
-  }
-  return speexenc_type;
-}
-
-static GstCaps *
-speex_caps_factory (void)
-{
-  return gst_caps_new_simple ("audio/x-speex", NULL);
-}
-
-static GstCaps *
-raw_caps_factory (void)
-{
-  return
-      gst_caps_new_simple ("audio/x-raw-int",
-      "rate", GST_TYPE_INT_RANGE, 6000, 48000,
-      "channels", GST_TYPE_INT_RANGE, 1, 2,
-      "endianness", G_TYPE_INT, G_BYTE_ORDER,
-      "signed", G_TYPE_BOOLEAN, TRUE,
-      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
-}
+static gboolean gst_speex_enc_start (GstAudioEncoder * enc);
+static gboolean gst_speex_enc_stop (GstAudioEncoder * enc);
+static gboolean gst_speex_enc_set_format (GstAudioEncoder * enc,
+    GstAudioInfo * info);
+static GstFlowReturn gst_speex_enc_handle_frame (GstAudioEncoder * enc,
+    GstBuffer * in_buf);
+static gboolean gst_speex_enc_sink_event (GstAudioEncoder * enc,
+    GstEvent * event);
+static GstFlowReturn
+gst_speex_enc_pre_push (GstAudioEncoder * benc, GstBuffer ** buffer);
 
-static void
-gst_speexenc_base_init (gpointer g_class)
-{
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-  GstCaps *raw_caps, *speex_caps;
-
-  raw_caps = raw_caps_factory ();
-  speex_caps = speex_caps_factory ();
-
-  gst_speexenc_sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
-      GST_PAD_ALWAYS, raw_caps);
-  gst_speexenc_src_template = gst_pad_template_new ("src", GST_PAD_SRC,
-      GST_PAD_ALWAYS, speex_caps);
-  gst_element_class_add_pad_template (element_class,
-      gst_speexenc_sink_template);
-  gst_element_class_add_pad_template (element_class, gst_speexenc_src_template);
-  gst_element_class_set_details (element_class, &speexenc_details);
-}
+#define gst_speex_enc_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstSpeexEnc, gst_speex_enc, GST_TYPE_AUDIO_ENCODER,
+    G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL);
+    G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
 
 static void
-gst_speexenc_class_init (GstSpeexEncClass * klass)
+gst_speex_enc_class_init (GstSpeexEncClass * klass)
 {
   GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
+  GstAudioEncoderClass *base_class;
 
   gobject_class = (GObjectClass *) klass;
   gstelement_class = (GstElementClass *) klass;
+  base_class = (GstAudioEncoderClass *) klass;
+
+  gobject_class->finalize = gst_speex_enc_finalize;
+  gobject_class->set_property = gst_speex_enc_set_property;
+  gobject_class->get_property = gst_speex_enc_get_property;
 
-  gobject_class->set_property = gst_speexenc_set_property;
-  gobject_class->get_property = gst_speexenc_get_property;
+  base_class->start = GST_DEBUG_FUNCPTR (gst_speex_enc_start);
+  base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_enc_stop);
+  base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_enc_set_format);
+  base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_enc_handle_frame);
+  base_class->event = GST_DEBUG_FUNCPTR (gst_speex_enc_sink_event);
+  base_class->pre_push = GST_DEBUG_FUNCPTR (gst_speex_enc_pre_push);
 
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
       g_param_spec_float ("quality", "Quality", "Encoding quality",
-          0.0, 10.0, DEFAULT_QUALITY, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
+          0.0, 10.0, DEFAULT_QUALITY,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BITRATE,
       g_param_spec_int ("bitrate", "Encoding Bit-rate",
           "Specify an encoding bit-rate (in bps). (0 = automatic)",
-          0, G_MAXINT, DEFAULT_BITRATE, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR,
+          0, G_MAXINT, DEFAULT_BITRATE,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (gobject_class, PROP_MODE,
+      g_param_spec_enum ("mode", "Mode", "The encoding mode",
+          GST_TYPE_SPEEX_ENC_MODE, GST_SPEEX_ENC_MODE_AUTO,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VBR,
       g_param_spec_boolean ("vbr", "VBR",
-          "Enable variable bit-rate", DEFAULT_VBR, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ABR,
+          "Enable variable bit-rate", DEFAULT_VBR,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ABR,
       g_param_spec_int ("abr", "ABR",
           "Enable average bit-rate (0 = disabled)",
-          0, G_MAXINT, DEFAULT_ABR, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VAD,
+          0, G_MAXINT, DEFAULT_ABR,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VAD,
       g_param_spec_boolean ("vad", "VAD",
-          "Enable voice activity detection", DEFAULT_VAD, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DTX,
+          "Enable voice activity detection", DEFAULT_VAD,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DTX,
       g_param_spec_boolean ("dtx", "DTX",
-          "Enable discontinuous transmission", DEFAULT_DTX, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_COMPLEXITY,
+          "Enable discontinuous transmission", DEFAULT_DTX,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_COMPLEXITY,
       g_param_spec_int ("complexity", "Complexity",
           "Set encoding complexity",
-          0, G_MAXINT, DEFAULT_COMPLEXITY, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NFRAMES,
+          0, G_MAXINT, DEFAULT_COMPLEXITY,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_NFRAMES,
       g_param_spec_int ("nframes", "NFrames",
           "Number of frames per buffer",
-          0, G_MAXINT, DEFAULT_NFRAMES, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
+          0, G_MAXINT, DEFAULT_NFRAMES,
+          G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE,
       g_param_spec_string ("last-message", "last-message",
-          "The last status message", NULL, G_PARAM_READABLE));
-
-  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-
-  gstelement_class->change_state = gst_speexenc_change_state;
+          "The last status message", NULL,
+          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&src_factory));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&sink_factory));
+  gst_element_class_set_details_simple (gstelement_class, "Speex audio encoder",
+      "Codec/Encoder/Audio",
+      "Encodes audio in Speex format", "Wim Taymans <wim@fluendo.com>");
+
+  GST_DEBUG_CATEGORY_INIT (speexenc_debug, "speexenc", 0, "Speex encoder");
 }
 
-static gboolean
-gst_speexenc_sink_setcaps (GstPad * pad, GstCaps * caps)
+static void
+gst_speex_enc_finalize (GObject * object)
 {
-  GstSpeexEnc *speexenc;
-  GstStructure *structure;
+  GstSpeexEnc *enc;
 
-  speexenc = GST_SPEEXENC (GST_PAD_PARENT (pad));
-  speexenc->setup = FALSE;
+  enc = GST_SPEEX_ENC (object);
 
-  structure = gst_caps_get_structure (caps, 0);
-  gst_structure_get_int (structure, "channels", &speexenc->channels);
-  gst_structure_get_int (structure, "rate", &speexenc->rate);
+  g_free (enc->last_message);
 
-  gst_speexenc_setup (speexenc);
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
 
-  if (speexenc->setup)
-    return TRUE;
+static void
+gst_speex_enc_init (GstSpeexEnc * enc)
+{
+  GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc);
 
-  return FALSE;
+  /* arrange granulepos marking (and required perfect ts) */
+  gst_audio_encoder_set_mark_granule (benc, TRUE);
+  gst_audio_encoder_set_perfect_timestamp (benc, TRUE);
 }
 
 static gboolean
-gst_speexenc_convert_src (GstPad * pad, GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value)
+gst_speex_enc_start (GstAudioEncoder * benc)
 {
-  gboolean res = TRUE;
-  GstSpeexEnc *speexenc;
-  gint64 avg;
-
-  speexenc = GST_SPEEXENC (GST_PAD_PARENT (pad));
-
-  if (speexenc->samples_in == 0 ||
-      speexenc->bytes_out == 0 || speexenc->rate == 0)
-    return FALSE;
+  GstSpeexEnc *enc = GST_SPEEX_ENC (benc);
 
-  avg = (speexenc->bytes_out * speexenc->rate) / (speexenc->samples_in);
+  GST_DEBUG_OBJECT (enc, "start");
+  speex_bits_init (&enc->bits);
+  enc->tags = gst_tag_list_new_empty ();
+  enc->header_sent = FALSE;
 
-  switch (src_format) {
-    case GST_FORMAT_BYTES:
-      switch (*dest_format) {
-        case GST_FORMAT_TIME:
-          *dest_value = src_value * GST_SECOND / avg;
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    case GST_FORMAT_TIME:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          *dest_value = src_value * avg / GST_SECOND;
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    default:
-      res = FALSE;
-  }
-  return res;
+  return TRUE;
 }
 
 static gboolean
-gst_speexenc_convert_sink (GstPad * pad, GstFormat src_format,
-    gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
+gst_speex_enc_stop (GstAudioEncoder * benc)
 {
-  gboolean res = TRUE;
-  guint scale = 1;
-  gint bytes_per_sample;
-  GstSpeexEnc *speexenc;
-
-  speexenc = GST_SPEEXENC (GST_PAD_PARENT (pad));
-
-  bytes_per_sample = speexenc->channels * 2;
-
-  switch (src_format) {
-    case GST_FORMAT_BYTES:
-      switch (*dest_format) {
-        case GST_FORMAT_DEFAULT:
-          if (bytes_per_sample == 0)
-            return FALSE;
-          *dest_value = src_value / bytes_per_sample;
-          break;
-        case GST_FORMAT_TIME:
-        {
-          gint byterate = bytes_per_sample * speexenc->rate;
-
-          if (byterate == 0)
-            return FALSE;
-          *dest_value = src_value * GST_SECOND / byterate;
-          break;
-        }
-        default:
-          res = FALSE;
-      }
-      break;
-    case GST_FORMAT_DEFAULT:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          *dest_value = src_value * bytes_per_sample;
-          break;
-        case GST_FORMAT_TIME:
-          if (speexenc->rate == 0)
-            return FALSE;
-          *dest_value = src_value * GST_SECOND / speexenc->rate;
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    case GST_FORMAT_TIME:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          scale = bytes_per_sample;
-          /* fallthrough */
-        case GST_FORMAT_DEFAULT:
-          *dest_value = src_value * scale * speexenc->rate / GST_SECOND;
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    default:
-      res = FALSE;
+  GstSpeexEnc *enc = GST_SPEEX_ENC (benc);
+
+  GST_DEBUG_OBJECT (enc, "stop");
+  enc->header_sent = FALSE;
+  if (enc->state) {
+    speex_encoder_destroy (enc->state);
+    enc->state = NULL;
   }
-  return res;
+  speex_bits_destroy (&enc->bits);
+  gst_tag_list_free (enc->tags);
+  enc->tags = NULL;
+  g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
+  enc->headers = NULL;
+
+  gst_tag_setter_reset_tags (GST_TAG_SETTER (enc));
+
+  return TRUE;
 }
 
-static const GstQueryType *
-gst_speexenc_get_query_types (GstPad * pad)
+static gint64
+gst_speex_enc_get_latency (GstSpeexEnc * enc)
 {
-  static const GstQueryType gst_speexenc_src_query_types[] = {
-    GST_QUERY_POSITION,
-    GST_QUERY_DURATION,
-    GST_QUERY_CONVERT,
-    0
-  };
-
-  return gst_speexenc_src_query_types;
+  /* See the Speex manual section "Latency and algorithmic delay" */
+  if (enc->rate == 8000)
+    return 30 * GST_MSECOND;
+  else
+    return 34 * GST_MSECOND;
 }
 
 static gboolean
-gst_speexenc_src_query (GstPad * pad, GstQuery * query)
+gst_speex_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
 {
-  gboolean res = TRUE;
-  GstSpeexEnc *speexenc;
-  GstPad *peerpad;
-
-  speexenc = GST_SPEEXENC (gst_pad_get_parent (pad));
-  peerpad = gst_pad_get_peer (GST_PAD (speexenc->sinkpad));
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_POSITION:
-    {
-      GstFormat fmt, req_fmt;
-      gint64 pos, val;
-
-      gst_query_parse_position (query, &req_fmt, NULL);
-      if ((res = gst_pad_query_position (peerpad, &req_fmt, &val))) {
-        gst_query_set_position (query, req_fmt, val);
-        break;
-      }
-
-      fmt = GST_FORMAT_TIME;
-      if (!(res = gst_pad_query_position (peerpad, &fmt, &pos)))
-        break;
-
-      if ((res = gst_pad_query_convert (peerpad, fmt, pos, &req_fmt, &val)))
-        gst_query_set_position (query, req_fmt, val);
+  GstSpeexEnc *enc;
 
-      break;
-    }
-    case GST_QUERY_DURATION:
-    {
-      GstFormat fmt, req_fmt;
-      gint64 dur, val;
-
-      gst_query_parse_duration (query, &req_fmt, NULL);
-      if ((res = gst_pad_query_duration (peerpad, &req_fmt, &val))) {
-        gst_query_set_duration (query, req_fmt, val);
-        break;
-      }
+  enc = GST_SPEEX_ENC (benc);
 
-      fmt = GST_FORMAT_TIME;
-      if (!(res = gst_pad_query_duration (peerpad, &fmt, &dur)))
-        break;
+  enc->channels = GST_AUDIO_INFO_CHANNELS (info);
+  enc->rate = GST_AUDIO_INFO_RATE (info);
 
-      if ((res = gst_pad_query_convert (peerpad, fmt, dur, &req_fmt, &val))) {
-        gst_query_set_duration (query, req_fmt, val);
-      }
-      break;
-    }
-    case GST_QUERY_CONVERT:
-    {
-      GstFormat src_fmt, dest_fmt;
-      gint64 src_val, dest_val;
-
-      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      if (!(res = gst_speexenc_convert_src (pad, src_fmt, src_val, &dest_fmt,
-                  &dest_val)))
-        goto error;
-      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
-      break;
-    }
-    default:
-      res = FALSE;
-      break;
+  /* handle reconfigure */
+  if (enc->state) {
+    speex_encoder_destroy (enc->state);
+    enc->state = NULL;
   }
 
-error:
-  gst_object_unref (peerpad);
-  gst_object_unref (speexenc);
-  return res;
-}
-
-static gboolean
-gst_speexenc_sink_query (GstPad * pad, GstQuery * query)
-{
-  gboolean res = TRUE;
-  GstSpeexEnc *speexenc;
+  if (!gst_speex_enc_setup (enc))
+    return FALSE;
 
-  speexenc = GST_SPEEXENC (GST_PAD_PARENT (pad));
+  /* feedback to base class */
+  gst_audio_encoder_set_latency (benc,
+      gst_speex_enc_get_latency (enc), gst_speex_enc_get_latency (enc));
+  gst_audio_encoder_set_lookahead (benc, enc->lookahead);
 
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_CONVERT:
-    {
-      GstFormat src_fmt, dest_fmt;
-      gint64 src_val, dest_val;
-
-      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      if (!(res =
-              gst_speexenc_convert_sink (pad, src_fmt, src_val, &dest_fmt,
-                  &dest_val)))
-        goto error;
-      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
-      break;
-    }
-    default:
-      res = FALSE;
-      break;
+  if (enc->nframes == 0) {
+    /* as many frames as available input allows */
+    gst_audio_encoder_set_frame_samples_min (benc, enc->frame_size);
+    gst_audio_encoder_set_frame_samples_max (benc, enc->frame_size);
+    gst_audio_encoder_set_frame_max (benc, 0);
+  } else {
+    /* exactly as many frames as configured */
+    gst_audio_encoder_set_frame_samples_min (benc,
+        enc->frame_size * enc->nframes);
+    gst_audio_encoder_set_frame_samples_max (benc,
+        enc->frame_size * enc->nframes);
+    gst_audio_encoder_set_frame_max (benc, 1);
   }
 
-error:
-  return res;
-}
-
-static void
-gst_speexenc_init (GstSpeexEnc * speexenc)
-{
-  speexenc->sinkpad =
-      gst_pad_new_from_template (gst_speexenc_sink_template, "sink");
-  gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->sinkpad);
-  gst_pad_set_event_function (speexenc->sinkpad, gst_speexenc_sinkevent);
-  gst_pad_set_chain_function (speexenc->sinkpad, gst_speexenc_chain);
-  gst_pad_set_setcaps_function (speexenc->sinkpad, gst_speexenc_sink_setcaps);
-  gst_pad_set_query_function (speexenc->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_speexenc_sink_query));
-
-  speexenc->srcpad =
-      gst_pad_new_from_template (gst_speexenc_src_template, "src");
-  gst_pad_set_query_function (speexenc->srcpad,
-      GST_DEBUG_FUNCPTR (gst_speexenc_src_query));
-  gst_pad_set_query_type_function (speexenc->srcpad,
-      GST_DEBUG_FUNCPTR (gst_speexenc_get_query_types));
-  gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->srcpad);
-
-  speexenc->channels = -1;
-  speexenc->rate = -1;
-
-  speexenc->quality = DEFAULT_QUALITY;
-  speexenc->bitrate = DEFAULT_BITRATE;
-  speexenc->vbr = DEFAULT_VBR;
-  speexenc->abr = DEFAULT_ABR;
-  speexenc->vad = DEFAULT_VAD;
-  speexenc->dtx = DEFAULT_DTX;
-  speexenc->complexity = DEFAULT_COMPLEXITY;
-  speexenc->nframes = DEFAULT_NFRAMES;
-
-  speexenc->setup = FALSE;
-  speexenc->header_sent = FALSE;
-
-  speexenc->adapter = gst_adapter_new ();
+  return TRUE;
 }
 
-
-/* FIXME: why are we not using the from/to vorbiscomment 
- * functions that are in -lgsttagedit-0.9 here? */
-
-static gchar *
-gst_speexenc_get_tag_value (const GstTagList * list, const gchar * tag,
-    int index)
+static GstBuffer *
+gst_speex_enc_create_metadata_buffer (GstSpeexEnc * enc)
 {
-  GType tag_type;
-  gchar *speexvalue = NULL;
+  const GstTagList *user_tags;
+  GstTagList *merged_tags;
+  GstBuffer *comments = NULL;
 
-  if (tag == NULL)
-    return NULL;
+  user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (enc));
 
-  tag_type = gst_tag_get_type (tag);
+  GST_DEBUG_OBJECT (enc, "upstream tags = %" GST_PTR_FORMAT, enc->tags);
+  GST_DEBUG_OBJECT (enc, "user-set tags = %" GST_PTR_FORMAT, user_tags);
 
-  /* get tag name right */
-  if ((strcmp (tag, GST_TAG_TRACK_NUMBER) == 0)
-      || (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0)
-      || (strcmp (tag, GST_TAG_TRACK_COUNT) == 0)
-      || (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0)) {
-    guint track_no;
+  /* gst_tag_list_merge() will handle NULL for either or both lists fine */
+  merged_tags = gst_tag_list_merge (user_tags, enc->tags,
+      gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (enc)));
 
-    if (gst_tag_list_get_uint_index (list, tag, index, &track_no)) {
-      speexvalue = g_strdup_printf ("%u", track_no);
-    } else {
-      GST_WARNING ("Failed to extract int tag %d for '%s'", index, tag);
-    }
-  } else if (tag_type == GST_TYPE_DATE) {
-    /* FIXME: how are dates represented in speex files? */
-    GDate *date;
-
-    if (gst_tag_list_get_date_index (list, tag, index, &date)) {
-      speexvalue =
-          g_strdup_printf ("%04d-%02d-%02d", (gint) g_date_get_year (date),
-          (gint) g_date_get_month (date), (gint) g_date_get_day (date));
-      g_date_free (date);
-    } else {
-      GST_WARNING ("Failed to extract date tag %d for '%s'", index, tag);
-    }
-  } else if (tag_type == G_TYPE_STRING) {
-    if (!gst_tag_list_get_string_index (list, tag, index, &speexvalue))
-      GST_WARNING ("Failed to extract string tag %d for '%s'", index, tag);
-  }
+  if (merged_tags == NULL)
+    merged_tags = gst_tag_list_new_empty ();
 
-  return speexvalue;
-}
-
-/*
- *  Comments will be stored in the Vorbis style.
- *  It is describled in the "Structure" section of
- *  http://www.xiph.org/ogg/vorbis/doc/v-comment.html
- *
- *  The comment header is decoded as follows:
- *  1) [vendor_length] = read an unsigned integer of 32 bits
- *  2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets
- *  3) [user_comment_list_length] = read an unsigned integer of 32 bits
- *  4) iterate [user_comment_list_length] times {
- *     5) [length] = read an unsigned integer of 32 bits
- *     6) this iteration's user comment = read a UTF-8 vector as [length] octets
- *     }
- *  7) [framing_bit] = read a single bit as boolean
- *  8) if ( [framing_bit]  unset or end of packet ) then ERROR
- *  9) done.
- *
- *  If you have troubles, please write to ymnk@jcraft.com.
- */
-static void
-comment_init (guint8 ** comments, int *length, char *vendor_string)
-{
-  int vendor_length = strlen (vendor_string);
-  int user_comment_list_length = 0;
-  int len = 4 + vendor_length + 4;
-  guint8 *p = g_malloc (len);
-
-  GST_WRITE_UINT32_LE (p, vendor_length);
-  memcpy (p + 4, vendor_string, vendor_length);
-  GST_WRITE_UINT32_LE (p + 4 + vendor_length, user_comment_list_length);
-  *length = len;
-  *comments = p;
-}
-static void
-comment_add (guint8 ** comments, int *length, const char *tag, char *val)
-{
-  guint8 *p = *comments;
-  int vendor_length = GST_READ_UINT32_LE (p);
-  int user_comment_list_length = GST_READ_UINT32_LE (p + 4 + vendor_length);
-  int tag_len = (tag ? strlen (tag) : 0);
-  int val_len = strlen (val);
-  int len = (*length) + 4 + tag_len + val_len;
-
-  p = g_realloc (p, len);
-
-  GST_WRITE_UINT32_LE (p + *length, tag_len + val_len); /* length of comment */
-  if (tag)
-    memcpy (p + *length + 4, (guint8 *) tag, tag_len);  /* comment */
-  memcpy (p + *length + 4 + tag_len, val, val_len);     /* comment */
-  GST_WRITE_UINT32_LE (p + 4 + vendor_length, user_comment_list_length + 1);
-
-  *comments = p;
-  *length = len;
-}
-
-static void
-gst_speexenc_metadata_set1 (const GstTagList * list, const gchar * tag,
-    gpointer speexenc)
-{
-  const gchar *speextag = NULL;
-  gchar *speexvalue = NULL;
-  guint i, count;
-  GstSpeexEnc *enc = GST_SPEEXENC (speexenc);
-
-  speextag = gst_tag_to_vorbis_tag (tag);
-  if (speextag == NULL) {
-    return;
-  }
+  GST_DEBUG_OBJECT (enc, "merged   tags = %" GST_PTR_FORMAT, merged_tags);
+  comments = gst_tag_list_to_vorbiscomment_buffer (merged_tags, NULL,
+      0, "Encoded with GStreamer Speexenc");
+  gst_tag_list_free (merged_tags);
 
-  count = gst_tag_list_get_tag_size (list, tag);
-  for (i = 0; i < count; i++) {
-    speexvalue = gst_speexenc_get_tag_value (list, tag, i);
+  GST_BUFFER_OFFSET (comments) = 0;
+  GST_BUFFER_OFFSET_END (comments) = 0;
 
-    if (speexvalue != NULL) {
-      comment_add (&enc->comments, &enc->comment_len, speextag, speexvalue);
-    }
-  }
+  return comments;
 }
 
 static void
-gst_speexenc_set_metadata (GstSpeexEnc * speexenc)
+gst_speex_enc_set_last_msg (GstSpeexEnc * enc, const gchar * msg)
 {
-  GstTagList *copy;
-  const GstTagList *user_tags;
-
-  user_tags = gst_tag_setter_get_list (GST_TAG_SETTER (speexenc));
-  if (!(speexenc->tags || user_tags))
-    return;
-
-  comment_init (&speexenc->comments, &speexenc->comment_len,
-      "Encoded with GStreamer Speexenc");
-  copy =
-      gst_tag_list_merge (user_tags, speexenc->tags,
-      gst_tag_setter_get_merge_mode (GST_TAG_SETTER (speexenc)));
-  gst_tag_list_foreach (copy, gst_speexenc_metadata_set1, speexenc);
-  gst_tag_list_free (copy);
+  g_free (enc->last_message);
+  enc->last_message = g_strdup (msg);
+  GST_WARNING_OBJECT (enc, "%s", msg);
+  g_object_notify (G_OBJECT (enc), "last-message");
 }
 
 static gboolean
-gst_speexenc_setup (GstSpeexEnc * speexenc)
+gst_speex_enc_setup (GstSpeexEnc * enc)
 {
-  speexenc->setup = FALSE;
-
-  switch (speexenc->mode) {
-    case GST_SPEEXENC_MODE_UWB:
-      speexenc->speex_mode = (SpeexMode *) & speex_uwb_mode;
+  switch (enc->mode) {
+    case GST_SPEEX_ENC_MODE_UWB:
+      GST_LOG_OBJECT (enc, "configuring for requested UWB mode");
+      enc->speex_mode = speex_lib_get_mode (SPEEX_MODEID_UWB);
       break;
-    case GST_SPEEXENC_MODE_WB:
-      speexenc->speex_mode = (SpeexMode *) & speex_wb_mode;
+    case GST_SPEEX_ENC_MODE_WB:
+      GST_LOG_OBJECT (enc, "configuring for requested WB mode");
+      enc->speex_mode = speex_lib_get_mode (SPEEX_MODEID_WB);
       break;
-    case GST_SPEEXENC_MODE_NB:
-      speexenc->speex_mode = (SpeexMode *) & speex_nb_mode;
+    case GST_SPEEX_ENC_MODE_NB:
+      GST_LOG_OBJECT (enc, "configuring for requested NB mode");
+      enc->speex_mode = speex_lib_get_mode (SPEEX_MODEID_NB);
       break;
-    case GST_SPEEXENC_MODE_AUTO:
+    case GST_SPEEX_ENC_MODE_AUTO:
       /* fall through */
+      GST_LOG_OBJECT (enc, "finding best mode");
     default:
       break;
   }
 
-  if (speexenc->rate > 25000) {
-    if (speexenc->mode == GST_SPEEXENC_MODE_AUTO) {
-      speexenc->speex_mode = (SpeexMode *) & speex_uwb_mode;
+  if (enc->rate > 25000) {
+    if (enc->mode == GST_SPEEX_ENC_MODE_AUTO) {
+      GST_LOG_OBJECT (enc, "selected UWB mode for samplerate %d", enc->rate);
+      enc->speex_mode = speex_lib_get_mode (SPEEX_MODEID_UWB);
     } else {
-      if (speexenc->speex_mode != &speex_uwb_mode) {
-        speexenc->last_message =
-            g_strdup_printf
-            ("Warning: suggest to use ultra wide band mode for this rate");
-        g_object_notify (G_OBJECT (speexenc), "last_message");
+      if (enc->speex_mode != speex_lib_get_mode (SPEEX_MODEID_UWB)) {
+        gst_speex_enc_set_last_msg (enc,
+            "Warning: suggest to use ultra wide band mode for this rate");
       }
     }
-  } else if (speexenc->rate > 12500) {
-    if (speexenc->mode == GST_SPEEXENC_MODE_AUTO) {
-      speexenc->speex_mode = (SpeexMode *) & speex_wb_mode;
+  } else if (enc->rate > 12500) {
+    if (enc->mode == GST_SPEEX_ENC_MODE_AUTO) {
+      GST_LOG_OBJECT (enc, "selected WB mode for samplerate %d", enc->rate);
+      enc->speex_mode = speex_lib_get_mode (SPEEX_MODEID_WB);
     } else {
-      if (speexenc->speex_mode != &speex_wb_mode) {
-        speexenc->last_message =
-            g_strdup_printf
-            ("Warning: suggest to use wide band mode for this rate");
-        g_object_notify (G_OBJECT (speexenc), "last_message");
+      if (enc->speex_mode != speex_lib_get_mode (SPEEX_MODEID_WB)) {
+        gst_speex_enc_set_last_msg (enc,
+            "Warning: suggest to use wide band mode for this rate");
       }
     }
   } else {
-    if (speexenc->mode == GST_SPEEXENC_MODE_AUTO) {
-      speexenc->speex_mode = (SpeexMode *) & speex_nb_mode;
+    if (enc->mode == GST_SPEEX_ENC_MODE_AUTO) {
+      GST_LOG_OBJECT (enc, "selected NB mode for samplerate %d", enc->rate);
+      enc->speex_mode = speex_lib_get_mode (SPEEX_MODEID_NB);
     } else {
-      if (speexenc->speex_mode != &speex_nb_mode) {
-        speexenc->last_message =
-            g_strdup_printf
-            ("Warning: suggest to use narrow band mode for this rate");
-        g_object_notify (G_OBJECT (speexenc), "last_message");
+      if (enc->speex_mode != speex_lib_get_mode (SPEEX_MODEID_NB)) {
+        gst_speex_enc_set_last_msg (enc,
+            "Warning: suggest to use narrow band mode for this rate");
       }
     }
   }
 
-  if (speexenc->rate != 8000 && speexenc->rate != 16000
-      && speexenc->rate != 32000) {
-    speexenc->last_message =
-        g_strdup_printf ("Warning: speex is optimized for 8, 16 and 32 KHz");
-    g_object_notify (G_OBJECT (speexenc), "last_message");
+  if (enc->rate != 8000 && enc->rate != 16000 && enc->rate != 32000) {
+    gst_speex_enc_set_last_msg (enc,
+        "Warning: speex is optimized for 8, 16 and 32 KHz");
   }
 
-  speex_init_header (&speexenc->header, speexenc->rate, 1,
-      speexenc->speex_mode);
-  speexenc->header.frames_per_packet = speexenc->nframes;
-  speexenc->header.vbr = speexenc->vbr;
-  speexenc->header.nb_channels = speexenc->channels;
+  speex_init_header (&enc->header, enc->rate, 1, enc->speex_mode);
+  enc->header.frames_per_packet = enc->nframes;
+  enc->header.vbr = enc->vbr;
+  enc->header.nb_channels = enc->channels;
 
   /*Initialize Speex encoder */
-  speexenc->state = speex_encoder_init (speexenc->speex_mode);
+  enc->state = speex_encoder_init (enc->speex_mode);
 
-  speex_encoder_ctl (speexenc->state, SPEEX_GET_FRAME_SIZE,
-      &speexenc->frame_size);
-  speex_encoder_ctl (speexenc->state, SPEEX_SET_COMPLEXITY,
-      &speexenc->complexity);
-  speex_encoder_ctl (speexenc->state, SPEEX_SET_SAMPLING_RATE, &speexenc->rate);
+  speex_encoder_ctl (enc->state, SPEEX_GET_FRAME_SIZE, &enc->frame_size);
+  speex_encoder_ctl (enc->state, SPEEX_SET_COMPLEXITY, &enc->complexity);
+  speex_encoder_ctl (enc->state, SPEEX_SET_SAMPLING_RATE, &enc->rate);
 
-  if (speexenc->vbr)
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_VBR_QUALITY,
-        &speexenc->quality);
+  if (enc->vbr)
+    speex_encoder_ctl (enc->state, SPEEX_SET_VBR_QUALITY, &enc->quality);
   else {
-    gint tmp = floor (speexenc->quality);
+    gint tmp = floor (enc->quality);
 
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_QUALITY, &tmp);
+    speex_encoder_ctl (enc->state, SPEEX_SET_QUALITY, &tmp);
   }
-  if (speexenc->bitrate) {
-    if (speexenc->quality >= 0.0 && speexenc->vbr) {
-      speexenc->last_message =
-          g_strdup_printf ("Warning: bitrate option is overriding quality");
-      g_object_notify (G_OBJECT (speexenc), "last_message");
+  if (enc->bitrate) {
+    if (enc->quality >= 0.0 && enc->vbr) {
+      gst_speex_enc_set_last_msg (enc,
+          "Warning: bitrate option is overriding quality");
     }
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_BITRATE, &speexenc->bitrate);
+    speex_encoder_ctl (enc->state, SPEEX_SET_BITRATE, &enc->bitrate);
   }
-  if (speexenc->vbr) {
+  if (enc->vbr) {
     gint tmp = 1;
 
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_VBR, &tmp);
-  } else if (speexenc->vad) {
+    speex_encoder_ctl (enc->state, SPEEX_SET_VBR, &tmp);
+  } else if (enc->vad) {
     gint tmp = 1;
 
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_VAD, &tmp);
+    speex_encoder_ctl (enc->state, SPEEX_SET_VAD, &tmp);
   }
 
-  if (speexenc->dtx) {
+  if (enc->dtx) {
     gint tmp = 1;
 
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_DTX, &tmp);
+    speex_encoder_ctl (enc->state, SPEEX_SET_DTX, &tmp);
   }
 
-  if (speexenc->dtx && !(speexenc->vbr || speexenc->abr || speexenc->vad)) {
-    speexenc->last_message =
-        g_strdup_printf ("Warning: dtx is useless without vad, vbr or abr");
-    g_object_notify (G_OBJECT (speexenc), "last_message");
-  } else if ((speexenc->vbr || speexenc->abr) && (speexenc->vad)) {
-    speexenc->last_message =
-        g_strdup_printf ("Warning: vad is already implied by vbr or abr");
-    g_object_notify (G_OBJECT (speexenc), "last_message");
+  if (enc->dtx && !(enc->vbr || enc->abr || enc->vad)) {
+    gst_speex_enc_set_last_msg (enc,
+        "Warning: dtx is useless without vad, vbr or abr");
+  } else if ((enc->vbr || enc->abr) && (enc->vad)) {
+    gst_speex_enc_set_last_msg (enc,
+        "Warning: vad is already implied by vbr or abr");
   }
 
-  if (speexenc->abr) {
-    speex_encoder_ctl (speexenc->state, SPEEX_SET_ABR, &speexenc->abr);
+  if (enc->abr) {
+    speex_encoder_ctl (enc->state, SPEEX_SET_ABR, &enc->abr);
   }
 
-  speex_encoder_ctl (speexenc->state, SPEEX_GET_LOOKAHEAD,
-      &speexenc->lookahead);
+  speex_encoder_ctl (enc->state, SPEEX_GET_LOOKAHEAD, &enc->lookahead);
 
-  speexenc->setup = TRUE;
+  GST_LOG_OBJECT (enc, "we have frame size %d, lookahead %d", enc->frame_size,
+      enc->lookahead);
 
   return TRUE;
 }
 
-/* prepare a buffer for transmission */
-static GstBuffer *
-gst_speexenc_buffer_from_data (GstSpeexEnc * speexenc, guchar * data,
-    gint data_len, guint64 granulepos)
-{
-  GstBuffer *outbuf;
-
-  outbuf = gst_buffer_new_and_alloc (data_len);
-  memcpy (GST_BUFFER_DATA (outbuf), data, data_len);
-  GST_BUFFER_OFFSET (outbuf) = speexenc->bytes_out;
-  GST_BUFFER_OFFSET_END (outbuf) = granulepos;
-
-  GST_DEBUG ("encoded buffer of %d bytes", GST_BUFFER_SIZE (outbuf));
-  return outbuf;
-}
-
-/* push out the buffer and do internal bookkeeping */
-static void
-gst_speexenc_push_buffer (GstSpeexEnc * speexenc, GstBuffer * buffer)
-{
-  speexenc->bytes_out += GST_BUFFER_SIZE (buffer);
-
-  if (GST_PAD_IS_USABLE (speexenc->srcpad)) {
-    gst_pad_push (speexenc->srcpad, buffer);
-  } else {
-    gst_buffer_unref (buffer);
-  }
-}
-
-static GstCaps *
-gst_speexenc_set_header_on_caps (GstCaps * caps, GstBuffer * buf1,
-    GstBuffer * buf2)
+/* push out the buffer */
+static GstFlowReturn
+gst_speex_enc_push_buffer (GstSpeexEnc * enc, GstBuffer * buffer)
 {
-  caps = gst_caps_make_writable (caps);
-  GstStructure *structure = gst_caps_get_structure (caps, 0);
-  GValue list = { 0 };
-  GValue value = { 0 };
-
-  /* mark buffers */
-  GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS);
-  GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS);
+  GST_DEBUG_OBJECT (enc, "pushing output buffer of size %u",
+      gst_buffer_get_size (buffer));
 
-  /* put buffers in a fixed list */
-  g_value_init (&list, GST_TYPE_ARRAY);
-  g_value_init (&value, GST_TYPE_BUFFER);
-  gst_value_set_buffer (&value, buf1);
-  gst_value_list_append_value (&list, &value);
-  g_value_unset (&value);
-  g_value_init (&value, GST_TYPE_BUFFER);
-  gst_value_set_buffer (&value, buf2);
-  gst_value_list_append_value (&list, &value);
-  gst_structure_set_value (structure, "streamheader", &list);
-  g_value_unset (&value);
-  g_value_unset (&list);
-
-  return caps;
+  return gst_pad_push (GST_AUDIO_ENCODER_SRC_PAD (enc), buffer);
 }
 
-
 static gboolean
-gst_speexenc_sinkevent (GstPad * pad, GstEvent * event)
+gst_speex_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
 {
-  gboolean res = TRUE;
-  GstSpeexEnc *speexenc;
+  GstSpeexEnc *enc;
 
-  speexenc = GST_SPEEXENC (GST_PAD_PARENT (pad));
+  enc = GST_SPEEX_ENC (benc);
 
   switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_EOS:
-      speexenc->eos = TRUE;
-      res = gst_pad_event_default (pad, event);
-      break;
     case GST_EVENT_TAG:
     {
-      GstTagList *list;
+      if (enc->tags) {
+        GstTagList *list;
 
-      gst_event_parse_tag (event, &list);
-      if (speexenc->tags) {
-        gst_tag_list_insert (speexenc->tags, list,
-            gst_tag_setter_get_merge_mode (GST_TAG_SETTER (speexenc)));
+        gst_event_parse_tag (event, &list);
+        gst_tag_list_insert (enc->tags, list,
+            gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (enc)));
       } else {
         g_assert_not_reached ();
       }
-      res = gst_pad_event_default (pad, event);
       break;
     }
     default:
-      res = gst_pad_event_default (pad, event);
       break;
   }
-  return res;
+
+  /* we only peeked, let base class handle it */
+  return FALSE;
 }
 
 static GstFlowReturn
-gst_speexenc_chain (GstPad * pad, GstBuffer * buf)
+gst_speex_enc_encode (GstSpeexEnc * enc, GstBuffer * buf)
 {
-  GstSpeexEnc *speexenc;
+  gint frame_size = enc->frame_size;
+  gint bytes = frame_size * 2 * enc->channels, samples;
+  gint outsize, written, dtx_ret = 0;
+  guint8 *data, *data0 = NULL, *bdata, *outdata;
+  gsize bsize, size;
+  GstBuffer *outbuf;
+  GstFlowReturn ret = GST_FLOW_OK;
 
-  speexenc = GST_SPEEXENC (GST_PAD_PARENT (pad));
+  if (G_LIKELY (buf)) {
+    bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
 
-  if (!speexenc->setup) {
+    if (G_UNLIKELY (bsize % bytes)) {
+      GST_DEBUG_OBJECT (enc, "draining; adding silence samples");
+
+      size = ((bsize / bytes) + 1) * bytes;
+      data0 = data = g_malloc0 (size);
+      memcpy (data, bdata, bsize);
+      gst_buffer_unmap (buf, bdata, bsize);
+      bdata = NULL;
+    } else {
+      data = bdata;
+      size = bsize;
+    }
+  } else {
+    GST_DEBUG_OBJECT (enc, "nothing to drain");
+    goto done;
+  }
+
+  samples = size / (2 * enc->channels);
+  speex_bits_reset (&enc->bits);
+
+  /* FIXME what about dropped samples if DTS enabled ?? */
+
+  while (size) {
+    GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", frame_size, bytes);
+
+    if (enc->channels == 2) {
+      speex_encode_stereo_int ((gint16 *) data, frame_size, &enc->bits);
+    }
+    dtx_ret += speex_encode_int (enc->state, (gint16 *) data, &enc->bits);
+
+    data += bytes;
+    size -= bytes;
+  }
+
+  speex_bits_insert_terminator (&enc->bits);
+  outsize = speex_bits_nbytes (&enc->bits);
+
+  if (bdata)
+    gst_buffer_unmap (buf, bdata, bsize);
+
+#if 0
+  ret = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc),
+      GST_BUFFER_OFFSET_NONE, outsize,
+      GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (enc)), &outbuf);
+
+  if ((GST_FLOW_OK != ret))
+    goto done;
+#endif
+  outbuf = gst_buffer_new_allocate (NULL, outsize, 0);
+  outdata = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
+
+  written = speex_bits_write (&enc->bits, (gchar *) outdata, outsize);
+
+  if (G_UNLIKELY (written < outsize)) {
+    GST_ERROR_OBJECT (enc, "short write: %d < %d bytes", written, outsize);
+  } else if (G_UNLIKELY (written > outsize)) {
+    GST_ERROR_OBJECT (enc, "overrun: %d > %d bytes", written, outsize);
+    written = outsize;
+  }
+  gst_buffer_unmap (outbuf, outdata, written);
+
+  if (!dtx_ret)
+    GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
+
+  ret = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc),
+      outbuf, samples);
+
+done:
+  g_free (data0);
+  return ret;
+}
+
+/*
+ * (really really) FIXME: move into core (dixit tpm)
+ */
+/**
+ * _gst_caps_set_buffer_array:
+ * @caps: a #GstCaps
+ * @field: field in caps to set
+ * @buf: header buffers
+ *
+ * Adds given buffers to an array of buffers set as the given @field
+ * on the given @caps.  List of buffer arguments must be NULL-terminated.
+ *
+ * Returns: input caps with a streamheader field added, or NULL if some error
+ */
+static GstCaps *
+_gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
+    GstBuffer * buf, ...)
+{
+  GstStructure *structure = NULL;
+  va_list va;
+  GValue array = { 0 };
+  GValue value = { 0 };
+
+  g_return_val_if_fail (caps != NULL, NULL);
+  g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
+  g_return_val_if_fail (field != NULL, NULL);
+
+  caps = gst_caps_make_writable (caps);
+  structure = gst_caps_get_structure (caps, 0);
+
+  g_value_init (&array, GST_TYPE_ARRAY);
+
+  va_start (va, buf);
+  /* put buffers in a fixed list */
+  while (buf) {
+    g_assert (gst_buffer_is_writable (buf));
+
+    /* mark buffer */
+    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
+
+    g_value_init (&value, GST_TYPE_BUFFER);
+    buf = gst_buffer_copy (buf);
+    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
+    gst_value_set_buffer (&value, buf);
     gst_buffer_unref (buf);
-    GST_ELEMENT_ERROR (speexenc, CORE, NEGOTIATION, (NULL),
-        ("encoder not initialized (input is not audio?)"));
-    return GST_FLOW_UNEXPECTED;
+    gst_value_array_append_value (&array, &value);
+    g_value_unset (&value);
+
+    buf = va_arg (va, GstBuffer *);
   }
 
-  if (!speexenc->header_sent) {
+  gst_structure_set_value (structure, field, &array);
+  g_value_unset (&array);
+
+  return caps;
+}
+
+static GstFlowReturn
+gst_speex_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
+{
+  GstSpeexEnc *enc;
+  GstFlowReturn ret = GST_FLOW_OK;
+
+  enc = GST_SPEEX_ENC (benc);
+
+  if (!enc->header_sent) {
     /* Speex streams begin with two headers; the initial header (with
        most of the codec setup parameters) which is mandated by the Ogg
        bitstream spec.  The second header holds any comment fields.
@@ -916,134 +689,113 @@ gst_speexenc_chain (GstPad * pad, GstBuffer * buf)
     guchar *data;
     gint data_len;
 
-    gst_speexenc_set_metadata (speexenc);
-
     /* create header buffer */
-    data = (guint8 *) speex_header_to_packet (&speexenc->header, &data_len);
-    buf1 = gst_speexenc_buffer_from_data (speexenc, data, data_len, 0);
+    data = (guint8 *) speex_header_to_packet (&enc->header, &data_len);
+    buf1 = gst_buffer_new_wrapped (data, data_len);
+    GST_BUFFER_OFFSET_END (buf1) = 0;
+    GST_BUFFER_OFFSET (buf1) = 0;
 
     /* create comment buffer */
-    buf2 =
-        gst_speexenc_buffer_from_data (speexenc, speexenc->comments,
-        speexenc->comment_len, 0);
+    buf2 = gst_speex_enc_create_metadata_buffer (enc);
 
     /* mark and put on caps */
-    caps = gst_pad_get_caps (speexenc->srcpad);
-    caps = gst_speexenc_set_header_on_caps (caps, buf1, buf2);
+    caps = gst_caps_new_simple ("audio/x-speex", "rate", G_TYPE_INT, enc->rate,
+        "channels", G_TYPE_INT, enc->channels, NULL);
+    caps = _gst_caps_set_buffer_array (caps, "streamheader", buf1, buf2, NULL);
 
     /* negotiate with these caps */
-    GST_DEBUG ("here are the caps: %" GST_PTR_FORMAT, caps);
-    gst_pad_set_caps (speexenc->srcpad, caps);
+    GST_DEBUG_OBJECT (enc, "here are the caps: %" GST_PTR_FORMAT, caps);
 
-    gst_buffer_set_caps (buf1, caps);
-    gst_buffer_set_caps (buf2, caps);
+    gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), caps);
+    gst_caps_unref (caps);
 
     /* push out buffers */
-    gst_speexenc_push_buffer (speexenc, buf1);
-    gst_speexenc_push_buffer (speexenc, buf2);
-
-    speex_bits_init (&speexenc->bits);
-    speex_bits_reset (&speexenc->bits);
-
-    speexenc->header_sent = TRUE;
+    /* store buffers for later pre_push sending */
+    g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
+    enc->headers = NULL;
+    GST_DEBUG_OBJECT (enc, "storing header buffers");
+    enc->headers = g_slist_prepend (enc->headers, buf2);
+    enc->headers = g_slist_prepend (enc->headers, buf1);
+
+    enc->header_sent = TRUE;
   }
 
-  {
-    gint frame_size = speexenc->frame_size;
-    gint bytes = frame_size * 2 * speexenc->channels;
-
-    /* push buffer to adapter */
-    gst_adapter_push (speexenc->adapter, buf);
-
-    while (gst_adapter_available (speexenc->adapter) >= bytes) {
-      gint16 *data;
-      gint i;
-      gint outsize, written;
-      GstBuffer *outbuf;
-
-      data = (gint16 *) gst_adapter_peek (speexenc->adapter, bytes);
-
-      for (i = 0; i < frame_size * speexenc->channels; i++) {
-        speexenc->input[i] = (gfloat) data[i];
-      }
-      gst_adapter_flush (speexenc->adapter, bytes);
-
-      speexenc->samples_in += frame_size;
-
-      if (speexenc->channels == 2) {
-        speex_encode_stereo (speexenc->input, frame_size, &speexenc->bits);
-      }
-      speex_encode (speexenc->state, speexenc->input, &speexenc->bits);
-
-      speexenc->frameno++;
-
-      if ((speexenc->frameno % speexenc->nframes) != 0)
-        continue;
-
-      speex_bits_insert_terminator (&speexenc->bits);
-      outsize = speex_bits_nbytes (&speexenc->bits);
+  GST_DEBUG_OBJECT (enc, "received buffer %p of %u bytes", buf,
+      buf ? gst_buffer_get_size (buf) : 0);
 
-      gst_pad_alloc_buffer (speexenc->srcpad,
-          GST_BUFFER_OFFSET_NONE, outsize, GST_PAD_CAPS (speexenc->srcpad),
-          &outbuf);
+  ret = gst_speex_enc_encode (enc, buf);
 
-      written = speex_bits_write (&speexenc->bits,
-          (gchar *) GST_BUFFER_DATA (outbuf), outsize);
-      g_assert (written == outsize);
-      speex_bits_reset (&speexenc->bits);
-
-      GST_BUFFER_TIMESTAMP (outbuf) =
-          (speexenc->frameno * frame_size -
-          speexenc->lookahead) * GST_SECOND / speexenc->rate;
-      GST_BUFFER_DURATION (outbuf) = frame_size * GST_SECOND / speexenc->rate;
-      GST_BUFFER_OFFSET (outbuf) = speexenc->bytes_out;
-      GST_BUFFER_OFFSET_END (outbuf) =
-          speexenc->frameno * frame_size - speexenc->lookahead;
+  return ret;
+}
 
-      gst_speexenc_push_buffer (speexenc, outbuf);
+static GstFlowReturn
+gst_speex_enc_pre_push (GstAudioEncoder * benc, GstBuffer ** buffer)
+{
+  GstSpeexEnc *enc;
+  GstFlowReturn ret = GST_FLOW_OK;
+
+  enc = GST_SPEEX_ENC (benc);
+
+  /* FIXME 0.11 ? get rid of this special ogg stuff and have it
+   * put and use 'codec data' in caps like anything else,
+   * with all the usual out-of-band advantage etc */
+  if (G_UNLIKELY (enc->headers)) {
+    GSList *header = enc->headers;
+
+    /* try to push all of these, if we lose one, might as well lose all */
+    while (header) {
+      if (ret == GST_FLOW_OK)
+        ret = gst_speex_enc_push_buffer (enc, header->data);
+      else
+        gst_speex_enc_push_buffer (enc, header->data);
+      header = g_slist_next (header);
     }
+
+    g_slist_free (enc->headers);
+    enc->headers = NULL;
   }
 
-  return GST_FLOW_OK;
+  return ret;
 }
 
 static void
-gst_speexenc_get_property (GObject * object, guint prop_id, GValue * value,
+gst_speex_enc_get_property (GObject * object, guint prop_id, GValue * value,
     GParamSpec * pspec)
 {
-  GstSpeexEnc *speexenc;
-
-  g_return_if_fail (GST_IS_SPEEXENC (object));
+  GstSpeexEnc *enc;
 
-  speexenc = GST_SPEEXENC (object);
+  enc = GST_SPEEX_ENC (object);
 
   switch (prop_id) {
-    case ARG_QUALITY:
-      g_value_set_float (value, speexenc->quality);
+    case PROP_QUALITY:
+      g_value_set_float (value, enc->quality);
       break;
-    case ARG_BITRATE:
-      g_value_set_int (value, speexenc->bitrate);
+    case PROP_BITRATE:
+      g_value_set_int (value, enc->bitrate);
       break;
-    case ARG_VBR:
-      g_value_set_boolean (value, speexenc->vbr);
+    case PROP_MODE:
+      g_value_set_enum (value, enc->mode);
       break;
-    case ARG_ABR:
-      g_value_set_int (value, speexenc->abr);
+    case PROP_VBR:
+      g_value_set_boolean (value, enc->vbr);
       break;
-    case ARG_VAD:
-      g_value_set_boolean (value, speexenc->vad);
+    case PROP_ABR:
+      g_value_set_int (value, enc->abr);
       break;
-    case ARG_DTX:
-      g_value_set_boolean (value, speexenc->dtx);
+    case PROP_VAD:
+      g_value_set_boolean (value, enc->vad);
       break;
-    case ARG_COMPLEXITY:
-      g_value_set_int (value, speexenc->complexity);
+    case PROP_DTX:
+      g_value_set_boolean (value, enc->dtx);
       break;
-    case ARG_NFRAMES:
-      g_value_set_int (value, speexenc->nframes);
+    case PROP_COMPLEXITY:
+      g_value_set_int (value, enc->complexity);
       break;
-    case ARG_LAST_MESSAGE:
-      g_value_set_string (value, speexenc->last_message);
+    case PROP_NFRAMES:
+      g_value_set_int (value, enc->nframes);
+      break;
+    case PROP_LAST_MESSAGE:
+      g_value_set_string (value, enc->last_message);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -1052,81 +804,43 @@ gst_speexenc_get_property (GObject * object, guint prop_id, GValue * value,
 }
 
 static void
-gst_speexenc_set_property (GObject * object, guint prop_id,
+gst_speex_enc_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec)
 {
-  GstSpeexEnc *speexenc;
-
-  g_return_if_fail (GST_IS_SPEEXENC (object));
+  GstSpeexEnc *enc;
 
-  speexenc = GST_SPEEXENC (object);
+  enc = GST_SPEEX_ENC (object);
 
   switch (prop_id) {
-    case ARG_QUALITY:
-      speexenc->quality = g_value_get_float (value);
+    case PROP_QUALITY:
+      enc->quality = g_value_get_float (value);
       break;
-    case ARG_BITRATE:
-      speexenc->bitrate = g_value_get_int (value);
+    case PROP_BITRATE:
+      enc->bitrate = g_value_get_int (value);
       break;
-    case ARG_VBR:
-      speexenc->vbr = g_value_get_boolean (value);
+    case PROP_MODE:
+      enc->mode = g_value_get_enum (value);
       break;
-    case ARG_ABR:
-      speexenc->abr = g_value_get_int (value);
+    case PROP_VBR:
+      enc->vbr = g_value_get_boolean (value);
       break;
-    case ARG_VAD:
-      speexenc->vad = g_value_get_boolean (value);
+    case PROP_ABR:
+      enc->abr = g_value_get_int (value);
       break;
-    case ARG_DTX:
-      speexenc->dtx = g_value_get_boolean (value);
+    case PROP_VAD:
+      enc->vad = g_value_get_boolean (value);
       break;
-    case ARG_COMPLEXITY:
-      speexenc->complexity = g_value_get_int (value);
+    case PROP_DTX:
+      enc->dtx = g_value_get_boolean (value);
       break;
-    case ARG_NFRAMES:
-      speexenc->nframes = g_value_get_int (value);
+    case PROP_COMPLEXITY:
+      enc->complexity = g_value_get_int (value);
       break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    case PROP_NFRAMES:
+      enc->nframes = g_value_get_int (value);
       break;
-  }
-}
-
-static GstStateChangeReturn
-gst_speexenc_change_state (GstElement * element, GstStateChange transition)
-{
-  GstSpeexEnc *speexenc = GST_SPEEXENC (element);
-  GstStateChangeReturn res;
-
-  switch (transition) {
-    case GST_STATE_CHANGE_NULL_TO_READY:
-      speexenc->tags = gst_tag_list_new ();
-      break;
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-      speexenc->frameno = 0;
-      speexenc->samples_in = 0;
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      /* fall through */
-    default:
-      break;
-  }
-
-  res = 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:
-      speexenc->setup = FALSE;
-      speexenc->header_sent = FALSE;
-      break;
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      gst_tag_list_free (speexenc->tags);
-      speexenc->tags = NULL;
     default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
-
-  return res;
 }