diff --git a/gst/mpegtsdemux/Makefile.am b/gst/mpegtsdemux/Makefile.am
index 55b57d9..fe9c865 100644
--- a/gst/mpegtsdemux/Makefile.am
+++ b/gst/mpegtsdemux/Makefile.am
@@ -1,4 +1,5 @@
-plugin_LTLIBRARIES = libgstmpegtsdemux.la
+plugin_LTLIBRARIES = libgstmpegtsdemux.la libgstmpegpesdepay.la \
+	libgstmpegpesparse.la
 
 libgstmpegtsdemux_la_SOURCES = \
 	gsttsdemux.c \
@@ -19,15 +20,36 @@ libgstmpegtsdemux_la_LIBADD = \
 libgstmpegtsdemux_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 libgstmpegtsdemux_la_LIBTOOLFLAGS = --tag=disable-static
 
+libgstmpegpesdepay_la_SOURCES = \
+	mpegpesdepay.c \
+	mpegpesdepkt.c \
+	pesparse.c
+
+libgstmpegpesdepay_la_CFLAGS = $(GST_CFLAGS)
+libgstmpegpesdepay_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+libgstmpegpesdepay_la_LIBADD = $(GST_BASE_LIBS)
+libgstmpegpesdepay_la_LIBTOOLFLAGS = --tag=disable-static
+
+libgstmpegpesparse_la_SOURCES = \
+	mpegpesparse.c \
+	pesparse.c
+
+libgstmpegpesparse_la_CFLAGS = $(GST_CFLAGS)
+libgstmpegpesparse_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+libgstmpegpesparse_la_LIBADD = $(GST_BASE_LIBS)
+libgstmpegpesparse_la_LIBTOOLFLAGS = --tag=disable-static
+
 noinst_HEADERS = \
 	gstmpegdefs.h   \
 	gstmpegdesc.h   \
-	mpegtsbase.h	\
+	mpegtsbase.h \
 	mpegtspacketizer.h \
 	mpegtsparse.h \
 	payload_parsers.h \
-	tsdemux.h	\
-	pesparse.h
+	tsdemux.h \
+	pesparse.h \
+	mpegpesdepay.h \
+	mpegpesparse.h
 
 Android.mk: Makefile.am $(BUILT_SOURCES)
 	androgenizer \
diff --git a/gst/mpegtsdemux/mpegpesdepay.c b/gst/mpegtsdemux/mpegpesdepay.c
new file mode 100644
index 0000000..542f6d6
--- /dev/null
+++ b/gst/mpegtsdemux/mpegpesdepay.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+
+#include "mpegpesdepkt.h"
+#include "mpegpesdepay.h"
+
+GST_DEBUG_CATEGORY (mpegpesdepay_debug);
+#define GST_CAT_DEFAULT mpegpesdepay_debug
+
+static void mpegpesdepay_dispose (GObject * object);
+
+static void mpegpesdepay_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void mpegpesdepay_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+
+GST_BOILERPLATE (MpegPesDepay, mpegpesdepay, GstBin, GST_TYPE_BIN);
+
+enum
+{
+  PROP_0,
+  PROP_PEDANTIC,
+};
+
+static void
+mpegpesdepay_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  gst_element_class_set_details_simple (element_class,
+      "MPEG PES Depayloader", "Codec/Depayloader",
+      "Extracts Elementary Stream from PES",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+  mpegpesdepkt_plugin_init ();
+}
+
+static void
+mpegpesdepay_class_init (MpegPesDepayClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->dispose = mpegpesdepay_dispose;
+  gobject_class->set_property = mpegpesdepay_set_property;
+  gobject_class->get_property = mpegpesdepay_get_property;
+  g_object_class_install_property (gobject_class, PROP_PEDANTIC,
+      g_param_spec_boolean ("pedantic", "Pedantic parsing",
+          "Pedantically treat garbage data in the input stream as an error",
+          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+mpegpesdepay_init (MpegPesDepay * depay, MpegPesDepayClass * g_class)
+{
+  GstBin *bin = GST_BIN (depay);
+  GstPad *srcpad, *sinkpad;
+  depay->depkt = gst_element_factory_make ("mpegpesdepkt", "depacketizer");
+  depay->parser = gst_element_factory_make ("mpegpesparse", "parser");
+  gst_bin_add_many (bin, depay->parser, depay->depkt, NULL);
+  srcpad = gst_ghost_pad_new ("src",
+      gst_element_get_static_pad (depay->depkt, "src"));
+  sinkpad = gst_ghost_pad_new ("sink",
+      gst_element_get_static_pad (depay->parser, "sink"));
+  gst_pad_link (gst_element_get_static_pad (depay->parser, "src"),
+      gst_element_get_static_pad (depay->depkt, "sink"));
+  gst_element_add_pad (GST_ELEMENT (depay), srcpad);
+  gst_element_add_pad (GST_ELEMENT (depay), sinkpad);
+}
+
+static void
+mpegpesdepay_dispose (GObject * object)
+{
+  MpegPesDepay *depay = GST_MPEG_PES_DEPAY (object);
+  GstBin *bin = GST_BIN (object);
+  gst_bin_remove (bin, depay->parser);
+  gst_bin_remove (bin, depay->depkt);
+
+  GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
+}
+
+static void
+mpegpesdepay_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  MpegPesDepay *depay = GST_MPEG_PES_DEPAY (object);
+  g_object_set_property (G_OBJECT (depay->parser), pspec->name, value);
+}
+
+static void
+mpegpesdepay_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  MpegPesDepay *depay = GST_MPEG_PES_DEPAY (object);
+  g_object_get_property (G_OBJECT (depay->parser), pspec->name, value);
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "mpegpesdepay", GST_RANK_PRIMARY,
+          mpegpesdepay_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (mpegpesdepay_debug, "mpegpesdepay", 0,
+      "MPEG PES depayloader");
+
+  return TRUE;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+    "mpegpesdepay", "MPEG PES depayloader",
+    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
diff --git a/gst/mpegtsdemux/mpegpesdepay.h b/gst/mpegtsdemux/mpegpesdepay.h
new file mode 100644
index 0000000..4b96890
--- /dev/null
+++ b/gst/mpegtsdemux/mpegpesdepay.h
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __MPEGPESDEPAY_H__
+#define __MPEGPESDEPAY_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+#define GST_TYPE_MPEG_PES_DEPAY  (mpegpesdepay_get_type())
+#define GST_MPEG_PES_DEPAY(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPEG_PES_DEPAY, MpegPesDepay))
+typedef struct MpegPesDepay MpegPesDepay;
+typedef struct MpegPesDepayClass MpegPesDepayClass;
+
+struct MpegPesDepay
+{
+  GstBin parent;
+  GstElement *parser;
+  GstElement *depkt;
+};
+
+struct MpegPesDepayClass
+{
+  GstBinClass parent_class;
+};
+
+GType mpegpesdepay_get_type (void);
+
+G_END_DECLS
+#endif
diff --git a/gst/mpegtsdemux/mpegpesdepkt.c b/gst/mpegtsdemux/mpegpesdepkt.c
new file mode 100644
index 0000000..ae26b9c
--- /dev/null
+++ b/gst/mpegtsdemux/mpegpesdepkt.c
@@ -0,0 +1,263 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+
+#include "pesparse.h"
+#include "mpegpesdepkt.h"
+
+GST_DEBUG_CATEGORY (mpegpesdepkt_debug);
+#define GST_CAT_DEFAULT mpegpesdepay_debug
+
+static GstStaticPadTemplate mpegpesdepkt_src_factory =
+    GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/mpeg, "
+        "systemstream = (boolean) false, "
+        "parsed = (boolean) false; "
+        "video/x-dirac, "
+        "parsed = (boolean) false; "
+        "video/x-h264, "
+        "parsed = (boolean) false; "
+        "audio/mpeg, "
+        "parsed = (boolean) false; "
+        "audio/x-lpcm, "
+        "parsed = (boolean) false; "
+        "audio/x-ac3, "
+        "parsed = (boolean) false; "
+        "audio/x-dts, " "parsed = (boolean) false"));
+
+static GstStaticPadTemplate mpegpesdepkt_sink_factory =
+    GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/mpegpes, "
+        "framed = (boolean) true, "
+        "stream-type = (string) { \"dirac\", \"h264\", "
+        "\"mp1\", \"mp2\", \"mp4\" }; "
+        "audio/mpegpes, "
+        "stream-type = (string) { \"mp1\", \"mp2\", \"mpeg\", "
+        "\"lpcm\", \"ac3\" }"));
+
+static void mpegpesdepkt_dispose (GObject * object);
+static gboolean mpegpesdepkt_sink_setcaps (GstPad * pad, GstCaps * caps);
+static GstFlowReturn mpegpesdepkt_chain (GstPad * pad, GstBuffer * buf);
+
+GST_BOILERPLATE (MpegPesDepkt, mpegpesdepkt, GstElement, GST_TYPE_ELEMENT);
+
+static void
+mpegpesdepkt_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&mpegpesdepkt_sink_factory));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&mpegpesdepkt_src_factory));
+
+  gst_element_class_set_details_simple (element_class,
+      "MPEG PES Depacketizer", "Codec/Depayloader",
+      "Extracts Elementary Stream from PES",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+}
+
+static void
+mpegpesdepkt_class_init (MpegPesDepktClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->dispose = mpegpesdepkt_dispose;
+}
+
+static void
+mpegpesdepkt_init (MpegPesDepkt * depkt, MpegPesDepktClass * g_class)
+{
+  depkt->srcpad =
+      gst_pad_new_from_template (gst_static_pad_template_get
+      (&mpegpesdepkt_src_factory), "src");
+  gst_pad_use_fixed_caps (depkt->srcpad);
+  gst_element_add_pad (GST_ELEMENT (depkt), depkt->srcpad);
+
+  depkt->sinkpad =
+      gst_pad_new_from_template (gst_static_pad_template_get
+      (&mpegpesdepkt_sink_factory), "sink");
+  gst_pad_set_setcaps_function (depkt->sinkpad, mpegpesdepkt_sink_setcaps);
+  gst_pad_set_chain_function (depkt->sinkpad, mpegpesdepkt_chain);
+  gst_element_add_pad (GST_ELEMENT (depkt), depkt->sinkpad);
+
+  depkt->srccaps = NULL;
+}
+
+static void
+mpegpesdepkt_dispose (GObject * object)
+{
+  MpegPesDepkt *depkt = GST_MPEG_PES_DEPKT (object);
+  if (depkt->srccaps)
+    gst_caps_unref (depkt->srccaps);
+
+  GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
+}
+
+static gboolean
+mpegpesdepkt_sink_setcaps (GstPad * pad, GstCaps * caps)
+{
+  MpegPesDepkt *depkt = GST_MPEG_PES_DEPKT (GST_OBJECT_PARENT (pad));
+  GstCaps *scaps = gst_pad_get_caps_reffed (depkt->srcpad);
+  GstStructure *s = gst_caps_get_structure (caps, 0);
+  const gchar *streamtype = gst_structure_get_string (s, "stream-type");
+  GstCaps *dcaps;
+  GString *str;
+
+  if (!streamtype)
+    return FALSE;
+  if (gst_structure_has_name (s, "video/mpegpes")) {
+    str = g_string_new ("video/");
+  } else if (gst_structure_has_name (s, "audio/mpegpes")) {
+    str = g_string_new ("audio/");
+  } else {
+    g_critical ("wrong caps\n");
+    return FALSE;
+  }
+  if (strcmp (streamtype, "mpeg") != 0)
+    g_string_append (str, "x-");
+  g_string_append (str, streamtype);
+  dcaps = gst_caps_from_string (str->str);
+  depkt->srccaps = gst_caps_intersect (scaps, dcaps);
+  gst_caps_unref (scaps);
+  gst_caps_unref (dcaps);
+  g_string_free (str, TRUE);
+  return TRUE;
+}
+
+static GstFlowReturn
+mpegpesdepkt_chain (GstPad * pad, GstBuffer * buf)
+{
+  MpegPesDepkt *depkt = GST_MPEG_PES_DEPKT (GST_OBJECT_PARENT (pad));
+  guint8 *data;
+  guint32 length;
+  PESParsingResult parseres;
+  PESHeader header;
+
+  data = GST_BUFFER_DATA (buf);
+  length = GST_BUFFER_SIZE (buf);
+  parseres = mpegts_parse_pes_header (data, length, &header, 0);
+  switch (parseres) {
+    case PES_PARSING_OK:
+      /* Remove PES header */
+      GST_BUFFER_DATA (buf) += header.header_size;
+      GST_BUFFER_SIZE (buf) -= header.header_size;
+      break;
+    case PES_PARSING_NEED_MORE:
+      /* cannot be here! */
+      return GST_FLOW_ERROR;
+    case PES_PARSING_BAD:
+      return GST_FLOW_ERROR;
+  }
+
+  gst_buffer_set_caps (buf, depkt->srccaps);
+  return gst_pad_push (depkt->srcpad, buf);
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "mpegpesdepkt", GST_RANK_PRIMARY,
+          mpegpesdepkt_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (mpegpesdepkt_debug, "mpegpesdepkt", 0,
+      "MPEG PES depacketizer");
+
+  return TRUE;
+}
+
+gboolean
+mpegpesdepkt_plugin_init (void)
+{
+  return gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+      "mpegpesdepkt", "MPEG PES depacketizer",
+      plugin_init, VERSION, "LGPL", "mpegpesdepay", GST_PACKAGE_NAME,
+      GST_PACKAGE_ORIGIN);
+}
diff --git a/gst/mpegtsdemux/mpegpesdepkt.h b/gst/mpegtsdemux/mpegpesdepkt.h
new file mode 100644
index 0000000..01fc3d1
--- /dev/null
+++ b/gst/mpegtsdemux/mpegpesdepkt.h
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __MPEGPESDEPKT_H__
+#define __MPEGPESDEPKT_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+#define GST_TYPE_MPEG_PES_DEPKT  (mpegpesdepkt_get_type())
+#define GST_MPEG_PES_DEPKT(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPEG_PES_DEPKT, MpegPesDepkt))
+typedef struct MpegPesDepkt MpegPesDepkt;
+typedef struct MpegPesDepktClass MpegPesDepktClass;
+
+struct MpegPesDepkt
+{
+  GstElement parent;
+  GstPad *srcpad;
+  GstPad *sinkpad;
+  GstCaps *srccaps;
+};
+
+struct MpegPesDepktClass
+{
+  GstElementClass parent_class;
+};
+
+GType mpegpesdepkt_get_type (void);
+
+gboolean mpegpesdepkt_plugin_init (void);
+
+G_END_DECLS
+#endif
diff --git a/gst/mpegtsdemux/mpegpesparse.c b/gst/mpegtsdemux/mpegpesparse.c
new file mode 100644
index 0000000..c0c419f
--- /dev/null
+++ b/gst/mpegtsdemux/mpegpesparse.c
@@ -0,0 +1,376 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+
+#include <gst/base/gstbytereader.h>
+#include "pesparse.h"
+#include "mpegpesparse.h"
+
+GST_DEBUG_CATEGORY (mpegpesparse_debug);
+#define GST_CAT_DEFAULT mpegpesparse_debug
+
+static GstStaticPadTemplate mpegpesparse_src_factory =
+    GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/mpegpes, "
+        "stream-type = (string) { \"dirac\", \"h264\", "
+        "\"mp1\", \"mp2\", \"mp4\" }, "
+        "parsed = (boolean) true, "
+        "framed = (boolean) true; "
+        "audio/mpegpes, "
+        "stream-type = (string) { \"mp1\", \"mp2\", \"mpeg\", "
+        "\"lpcm\", \"ac3\" }, "
+        "parsed = (boolean) true, " "framed = (boolean) true"));
+
+static GstStaticPadTemplate mpegpesparse_sink_factory =
+    GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/mpegpes; audio/mpegpes"));
+
+static gboolean mpegpesparse_sink_setcaps (GstBaseParse * parse,
+    GstCaps * caps);
+static gboolean mpegpesparse_check_valid_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
+static GstFlowReturn mpegpesparse_parse_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame);
+
+GST_BOILERPLATE (MpegPesParse, mpegpesparse, GstBaseParse, GST_TYPE_BASE_PARSE);
+
+static void
+mpegpesparse_base_init (gpointer g_class)
+{
+
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&mpegpesparse_sink_factory));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&mpegpesparse_src_factory));
+
+  gst_element_class_set_details_simple (element_class,
+      "MPEG PES Parser", "Codec/Parser",
+      "Parses packetized elementary stream",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+}
+
+static void
+mpegpesparse_class_init (MpegPesParseClass * klass)
+{
+  GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
+
+  parse_class->check_valid_frame = mpegpesparse_check_valid_frame;
+  parse_class->parse_frame = mpegpesparse_parse_frame;
+  parse_class->set_sink_caps = mpegpesparse_sink_setcaps;
+}
+
+static void
+mpegpesparse_init (MpegPesParse * pesparse, MpegPesParseClass * g_class)
+{
+}
+
+static gboolean
+mpegpesparse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
+{
+  GstStructure *s = gst_caps_get_structure (caps, 0);
+  gboolean parsed;
+  gboolean ok = gst_structure_get_boolean (s, "parsed", &parsed);
+  gst_base_parse_set_passthrough (parse, ok && parsed);
+  gst_base_parse_set_min_frame_size (parse, 4);
+  return TRUE;
+}
+
+static gint
+find_sync (GstBuffer * buf, guint pos, PESParsingResult * r_res)
+{
+  GstByteReader *reader = gst_byte_reader_new_from_buffer (buf);
+  PESParsingResult parseres = PES_PARSING_BAD;
+  PESHeader header;
+
+  while (1) {
+    gint len = GST_BUFFER_SIZE (buf) - pos;
+    if (len <= 0) {
+      pos = -1;
+      break;
+    }
+    pos = gst_byte_reader_masked_scan_uint32 (reader,
+        0xffffff00, 0x00000100, pos, len);
+    if (pos == -1)
+      break;
+    parseres = mpegts_parse_pes_header (GST_BUFFER_DATA (buf),
+        GST_BUFFER_SIZE (buf), &header, pos);
+    if (parseres == PES_PARSING_BAD) {
+      pos += 4;
+      continue;
+    }
+    break;
+  }
+  gst_byte_reader_free (reader);
+  if (r_res)
+    *r_res = parseres;
+  return pos;
+}
+
+#define NEED_MORE_SKIP_VAL(l) (GST_BASE_PARSE_DRAINING (parse) ? (l) : 0)
+
+static gboolean
+mpegpesparse_check_valid_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
+{
+  MpegPesParse *pesparse = GST_MPEG_PES_PARSE (parse);
+  guint length;
+  const guint8 *data;
+  gint sync_pos;
+  PESParsingResult parseres, sync_res;
+  PESHeader header;
+
+  length = GST_BUFFER_SIZE (frame->buffer);
+  data = GST_BUFFER_DATA (frame->buffer);
+  parseres = mpegts_parse_pes_header (data, length, &header, 0);
+
+  switch (parseres) {
+    case PES_PARSING_BAD:
+      sync_pos = find_sync (frame->buffer, 0, NULL);
+      g_assert (sync_pos != 0);
+      if (sync_pos == -1)
+        *skipsize = length;
+      else
+        *skipsize = sync_pos;
+      return FALSE;
+    case PES_PARSING_NEED_MORE:
+      *framesize = length + 4;
+      *skipsize = NEED_MORE_SKIP_VAL (length);
+      return FALSE;
+    case PES_PARSING_OK:
+      break;
+  }
+  pesparse->header = header;
+  if (!header.packet_length) {
+    sync_pos = find_sync (frame->buffer, header.header_size, &sync_res);
+    if (sync_pos == -1 || sync_res == PES_PARSING_NEED_MORE) {
+      if (GST_BASE_PARSE_DRAINING (parse)) {
+        /* take last frame as-is */
+        *framesize = length;
+        return TRUE;
+      }
+      *framesize = length + 4;
+      *skipsize = 0;
+      return FALSE;
+    }
+    *framesize = sync_pos;
+    return TRUE;
+  }
+
+  *framesize = header.packet_length;
+  return TRUE;
+}
+
+static GstFlowReturn
+mpegpesparse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
+{
+  MpegPesParse *pesparse = GST_MPEG_PES_PARSE (parse);
+  GstCaps *caps = gst_static_pad_template_get_caps (&mpegpesparse_src_factory);
+  GstCaps *dcaps, *bcaps;
+
+  if (!pesparse->header.packet_length)
+    GST_BUFFER_FLAG_SET (frame->buffer, MPEGPES_FLAG_UNBOUND);
+
+  if (pesparse->header.PTS != -1) {
+    GST_DEBUG_OBJECT (pesparse, "PES has pts %" G_GINT64_FORMAT,
+        pesparse->header.PTS);
+    pesparse->cur_pts = MPEGTIME_TO_GSTTIME (pesparse->header.PTS);
+  } else {
+    pesparse->cur_pts = GST_CLOCK_TIME_NONE;
+  }
+  if (pesparse->header.DTS != -1) {
+    GST_DEBUG_OBJECT (pesparse, "PES has dts %" G_GINT64_FORMAT
+        ", great, but gstreamer doesnt support those", pesparse->header.DTS);
+    pesparse->cur_dts = MPEGTIME_TO_GSTTIME (pesparse->header.DTS);
+  } else {
+    pesparse->cur_dts = GST_CLOCK_TIME_NONE;
+  }
+  GST_BUFFER_TIMESTAMP (frame->buffer) = pesparse->cur_pts;
+
+  if ((pesparse->header.stream_id & ID_ISO_IEC_MPEG12_VIDEO_STREAM_MASK) ==
+      ID_ISO_IEC_MPEG12_VIDEO_STREAM_0) {
+    switch (pesparse->header.stream_id - ID_ISO_IEC_MPEG12_VIDEO_STREAM_0) {
+      case 0:
+        dcaps = gst_caps_from_string ("video/mpegpes, "
+            "stream-type = (string) \"h264\"");
+        break;
+      case 1:
+        dcaps = gst_caps_from_string ("video/mpegpes, "
+            "stream-type = (string) \"mp1\"");
+        break;
+      case 2:
+        dcaps = gst_caps_from_string ("video/mpegpes, "
+            "stream-type = (string) \"mp2\"");
+        break;
+      case 4:
+        dcaps = gst_caps_from_string ("video/mpegpes, "
+            "stream-type = (string) \"mp4\"");
+        break;
+      default:
+        g_critical ("Unsupported stream ID %#x\n", pesparse->header.stream_id);
+        return GST_FLOW_ERROR;
+    }
+  } else if ((pesparse->header.stream_id &
+          ID_ISO_IEC_MPEG12_AUDIO_STREAM_MASK) ==
+      ID_ISO_IEC_MPEG12_AUDIO_STREAM_0) {
+    switch (pesparse->header.stream_id - ID_ISO_IEC_MPEG12_AUDIO_STREAM_0) {
+      case 0:
+        dcaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) \"mpeg\"");
+        break;
+      case 1:
+        dcaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) \"mp1\"");
+        break;
+      case 2:
+        dcaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) \"mp2\"");
+        break;
+      default:
+        g_critical ("Unsupported stream ID %#x\n", pesparse->header.stream_id);
+        return GST_FLOW_ERROR;
+    }
+  } else if (pesparse->header.stream_id == ID_EXTENDED_STREAM_ID) {
+    if (!pesparse->header.extension_field_length)
+      return GST_FLOW_ERROR;
+    switch (pesparse->header.stream_id_extension_data[0]) {
+      case 0x60:
+        /* dirac */
+        dcaps = gst_caps_from_string ("video/mpegpes, "
+            "stream-type = (string) \"dirac\"");
+        break;
+      case 0x71:
+        /* ac3 */
+        dcaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) \"ac3\"");
+        break;
+      case 0x80:
+        /* lpcm */
+        dcaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) \"lpcm\"");
+        break;
+      case 0x82:
+        /* dts */
+        dcaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) \"dts\"");
+        break;
+      default:
+        /* ??? */
+        g_critical ("Unsupported ext stream ID %#x\n",
+            pesparse->header.stream_id_extension_data[0]);
+        return GST_FLOW_ERROR;
+    }
+  } else {
+    g_critical ("Unsupported stream ID %#x\n", pesparse->header.stream_id);
+    return GST_FLOW_ERROR;
+  }
+  bcaps = gst_caps_intersect (caps, dcaps);
+  gst_caps_unref (caps);
+  gst_caps_unref (dcaps);
+  gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), bcaps);
+  return GST_FLOW_OK;
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "mpegpesparse", GST_RANK_PRIMARY,
+          mpegpesparse_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (mpegpesparse_debug, "mpegpesparse", 0,
+      "MPEG PES parser");
+
+  return TRUE;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+    "mpegpesparse", "MPEG PES parser",
+    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
diff --git a/gst/mpegtsdemux/mpegpesparse.h b/gst/mpegtsdemux/mpegpesparse.h
new file mode 100644
index 0000000..90bf7dd
--- /dev/null
+++ b/gst/mpegtsdemux/mpegpesparse.h
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __MPEGPESPARSE_H__
+#define __MPEGPESPARSE_H__
+
+#include <gst/gst.h>
+#include <gst/base/gstbaseparse.h>
+
+G_BEGIN_DECLS
+#define GST_TYPE_MPEG_PES_PARSE  (mpegpesparse_get_type())
+#define GST_MPEG_PES_PARSE(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPEG_PES_PARSE, MpegPesParse))
+typedef struct MpegPesParse MpegPesParse;
+typedef struct MpegPesParseClass MpegPesParseClass;
+
+struct MpegPesParse
+{
+  GstBaseParse parent;
+  GstClockTime cur_pts;
+  GstClockTime cur_dts;
+  PESHeader header;
+};
+
+struct MpegPesParseClass
+{
+  GstBaseParseClass parent_class;
+};
+
+GType mpegpesparse_get_type (void);
+
+#define MPEGPES_FLAG_UNBOUND            GST_BUFFER_FLAG_MEDIA1
+
+G_END_DECLS
+#endif
diff --git a/gst/mpegtsmux/Makefile.am b/gst/mpegtsmux/Makefile.am
index c433af3..c90056f 100644
--- a/gst/mpegtsmux/Makefile.am
+++ b/gst/mpegtsmux/Makefile.am
@@ -1,4 +1,4 @@
-plugin_LTLIBRARIES = libgstmpegtsmux.la
+plugin_LTLIBRARIES = libgstmpegtsmux.la libgstmpegpespay.la
 
 SUBDIRS = tsmux
 
@@ -12,7 +12,18 @@ libgstmpegtsmux_la_LIBADD = $(top_builddir)/gst/mpegtsmux/tsmux/libtsmux.la $(GS
 libgstmpegtsmux_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 libgstmpegtsmux_la_LIBTOOLFLAGS = --tag=disable-static
 
+libgstmpegpespay_la_SOURCES = \
+	mpegpespay.c \
+	mpegtsmux_h264.c \
+	mpegtsmux_aac.c
+
+libgstmpegpespay_la_CFLAGS = $(GST_CFLAGS)
+libgstmpegpespay_la_LIBADD = $(top_builddir)/gst/mpegtsmux/tsmux/libtsmux.la $(GST_LIBS) $(GST_BASE_LIBS)
+libgstmpegpespay_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+libgstmpegpespay_la_LIBTOOLFLAGS = --tag=disable-static
+
 noinst_HEADERS = \
 	mpegtsmux.h \
 	mpegtsmux_h264.h \
-	mpegtsmux_aac.h
+	mpegtsmux_aac.h \
+	mpegpespay.h
diff --git a/gst/mpegtsmux/mpegpespay.c b/gst/mpegtsmux/mpegpespay.c
new file mode 100644
index 0000000..3caba64
--- /dev/null
+++ b/gst/mpegtsmux/mpegpespay.c
@@ -0,0 +1,518 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * Based on the work of the following authors:
+ *  Authors: Jan Schmidt <jan@fluendo.com>
+ *           Kapil Agrawal <kapil@fluendo.com>
+ *           Julien Moutte <julien@fluendo.com>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+
+#include "mpegpespay.h"
+
+#include "mpegtsmux_h264.h"
+#include "mpegtsmux_aac.h"
+
+GST_DEBUG_CATEGORY (mpegpespay_debug);
+#define GST_CAT_DEFAULT mpegpespay_debug
+
+static GstStaticPadTemplate mpegpespay_sink_factory =
+    GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_REQUEST,
+    GST_STATIC_CAPS ("video/mpeg, "
+        "mpegversion = (int) { 1, 2, 4 }, "
+        "systemstream = (boolean) false, "
+        "parsed = (boolean) true; "
+        "video/x-dirac, "
+        "parsed = (boolean) true; "
+        "video/x-h264, "
+        "stream-format = (string) \"byte-stream\", "
+        "alignment = (string) \"au\"; "
+        "audio/mpeg, "
+        "mpegversion = (int) { 1, 2, 4 }, "
+        "framed = (boolean) true; "
+        "audio/x-lpcm, "
+        "width = (int) { 16, 20, 24 }, "
+        "rate = (int) { 48000, 96000 }, "
+        "channels = (int) [ 1, 8 ], "
+        "dynamic_range = (int) [ 0, 255 ], "
+        "emphasis = (boolean) { FALSE, TRUE }, "
+        "mute = (boolean) { FALSE, TRUE }; "
+        "audio/x-ac3, "
+        "framed = (boolean) true; " "audio/x-dts, " "framed = (boolean) true"));
+
+static GstStaticPadTemplate mpegpespay_src_factory =
+    GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/mpegpes, "
+        "stream-type = (string) { \"dirac\", \"h264\", \"mp1\", "
+        "\"mp2\", \"mp4\" }, "
+        "framed = (boolean) true; "
+        "audio/mpegpes, "
+        "stream-type = (string) { \"mp1\", \"mp2\", \"mpeg\", "
+        "\"lpcm\", \"ac3\" }, " "framed = (boolean) true"));
+
+static void mpegpespay_dispose (GObject * object);
+static void release_buffer_cb (guint8 * data, void *user_data);
+
+static void mpegpespay_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void mpegpespay_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+static gboolean mpegpespay_setcaps (GstPad * pad, GstCaps * caps);
+static GstFlowReturn mpegpespay_chain (GstPad * pad, GstBuffer * buf);
+
+GST_BOILERPLATE (MpegPesPay, mpegpespay, GstElement, GST_TYPE_ELEMENT);
+
+enum
+{
+  PROP_0,
+  PROP_NO_PTS,
+};
+
+static void
+mpegpespay_base_init (gpointer g_class)
+{
+
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&mpegpespay_sink_factory));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&mpegpespay_src_factory));
+
+  gst_element_class_set_details_simple (element_class,
+      "MPEG PES Payloader", "Codec/Payloader",
+      "Payload-encodes Elementary Stream into PES",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+}
+
+static void
+mpegpespay_class_init (MpegPesPayClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->dispose = mpegpespay_dispose;
+  gobject_class->set_property = mpegpespay_set_property;
+  gobject_class->get_property = mpegpespay_get_property;
+  g_object_class_install_property (gobject_class, PROP_NO_PTS,
+      g_param_spec_boolean ("no-pts", "No PTS timestamping",
+          "Produce PES without PTSes",
+          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+mpegpespay_init (MpegPesPay * pespay, MpegPesPayClass * g_class)
+{
+  pespay->srcpad =
+      gst_pad_new_from_template (gst_static_pad_template_get
+      (&mpegpespay_src_factory), "src");
+  gst_pad_use_fixed_caps (pespay->srcpad);
+  gst_element_add_pad (GST_ELEMENT (pespay), pespay->srcpad);
+
+  pespay->sinkpad =
+      gst_pad_new_from_template (gst_static_pad_template_get
+      (&mpegpespay_sink_factory), "sink");
+  gst_pad_set_setcaps_function (pespay->sinkpad, mpegpespay_setcaps);
+  gst_pad_set_chain_function (pespay->sinkpad, mpegpespay_chain);
+  gst_element_add_pad (GST_ELEMENT (pespay), pespay->sinkpad);
+
+  pespay->stream = NULL;
+  pespay->no_pts = FALSE;
+}
+
+static void
+mpegpespay_dispose (GObject * object)
+{
+  MpegPesPay *pespay = GST_MPEG_PES_PAY (object);
+
+  if (pespay->stream) {
+    tsmux_stream_free (pespay->stream);
+    gst_caps_unref (pespay->srccaps);
+  }
+
+  GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
+}
+
+static void
+mpegpespay_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  MpegPesPay *pespay = GST_MPEG_PES_PAY (object);
+
+  switch (prop_id) {
+    case PROP_NO_PTS:
+      pespay->no_pts = g_value_get_boolean (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+mpegpespay_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  MpegPesPay *pespay = GST_MPEG_PES_PAY (object);
+
+  switch (prop_id) {
+    case PROP_NO_PTS:
+      g_value_set_boolean (value, pespay->no_pts);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+release_buffer_cb (guint8 * data, void *user_data)
+{
+  GstBuffer *buf = (GstBuffer *) user_data;
+  gst_buffer_unref (buf);
+}
+
+static gboolean
+mpegpespay_setcaps (GstPad * pad, GstCaps * caps)
+{
+  MpegPesPay *pespay = GST_MPEG_PES_PAY (GST_OBJECT_PARENT (pad));
+  gboolean ret = FALSE;
+  GstStructure *s;
+  GstCaps *scaps = NULL, *tcaps;
+
+  s = gst_caps_get_structure (caps, 0);
+  g_return_val_if_fail (s != NULL, FALSE);
+
+  if (gst_structure_has_name (s, "video/x-dirac")) {
+    GST_DEBUG_OBJECT (pad, "Creating Dirac stream");
+    pespay->stream = tsmux_stream_new (0, TSMUX_ST_VIDEO_DIRAC);
+    scaps = gst_caps_from_string ("video/mpegpes, "
+        "stream-type = (string) dirac");
+  } else if (gst_structure_has_name (s, "audio/x-ac3")) {
+    if (!gst_structure_has_field (s, "framed")) {
+      g_critical ("Need framed stream to packetize\n");
+      return FALSE;
+    }
+    GST_DEBUG_OBJECT (pad, "Creating AC3 stream");
+    pespay->stream = tsmux_stream_new (0, TSMUX_ST_PS_AUDIO_AC3);
+    scaps = gst_caps_from_string ("audio/mpegpes, "
+        "stream-type = (string) ac3");
+  } else if (gst_structure_has_name (s, "audio/x-dts")) {
+    GST_DEBUG_OBJECT (pad, "Creating DTS stream");
+    pespay->stream = tsmux_stream_new (0, TSMUX_ST_PS_AUDIO_DTS);
+    scaps = gst_caps_from_string ("audio/mpegpes, "
+        "stream-type = (string) dts");
+  } else if (gst_structure_has_name (s, "audio/x-lpcm")) {
+    GST_DEBUG_OBJECT (pad, "Creating LPCM stream");
+    pespay->stream = tsmux_stream_new (0, TSMUX_ST_PS_AUDIO_LPCM);
+    scaps = gst_caps_from_string ("audio/mpegpes, "
+        "stream-type = (string) lpcm");
+  } else if (gst_structure_has_name (s, "video/x-h264")) {
+    const GValue *value;
+    if (!gst_structure_has_field (s, "alignment")) {
+      g_critical ("Need framed stream to packetize\n");
+      return FALSE;
+    }
+    GST_DEBUG_OBJECT (pad, "Creating H264 stream");
+    /* Codec data contains SPS/PPS which need to go in stream for valid ES */
+    value = gst_structure_get_value (s, "codec_data");
+    if (value) {
+      pespay->codec_data = gst_buffer_ref (gst_value_get_buffer (value));
+      GST_DEBUG_OBJECT (pad, "we have additional codec data (%d bytes)",
+          GST_BUFFER_SIZE (pespay->codec_data));
+      pespay->prepare_func = mpegtsmux_prepare_h264;
+      pespay->free_func = mpegtsmux_free_h264;
+    } else {
+      pespay->codec_data = NULL;
+    }
+    pespay->stream = tsmux_stream_new (0, TSMUX_ST_VIDEO_H264);
+    scaps = gst_caps_from_string ("video/mpegpes, "
+        "stream-type = (string) h264");
+  } else if (gst_structure_has_name (s, "audio/mpeg")) {
+    gint mpegversion;
+    if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
+      GST_ELEMENT_ERROR (pad, STREAM, FORMAT,
+          ("Invalid data format presented"),
+          ("Caps with type audio/mpeg did not have mpegversion"));
+      goto beach;
+    }
+
+    switch (mpegversion) {
+      case 1:
+        GST_DEBUG_OBJECT (pad, "Creating MPEG Audio, version 1 stream");
+        pespay->stream = tsmux_stream_new (0, TSMUX_ST_AUDIO_MPEG1);
+        scaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) mp1");
+        break;
+      case 2:
+        GST_DEBUG_OBJECT (pad, "Creating MPEG Audio, version 2 stream");
+        pespay->stream = tsmux_stream_new (0, TSMUX_ST_AUDIO_MPEG2);
+        scaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) mp2");
+        break;
+      case 4:
+      {
+        const GValue *value;
+        if (!gst_structure_has_field (s, "framed")) {
+          g_critical ("Need framed stream to packetize\n");
+          return FALSE;
+        }
+        /* Codec data contains SPS/PPS which need to go in stream for valid ES */
+        value = gst_structure_get_value (s, "codec_data");
+        if (value) {
+          pespay->codec_data = gst_buffer_ref (gst_value_get_buffer (value));
+          GST_DEBUG_OBJECT (pad, "we have additional codec data (%d bytes)",
+              GST_BUFFER_SIZE (pespay->codec_data));
+          pespay->prepare_func = mpegtsmux_prepare_aac;
+        } else {
+          pespay->codec_data = NULL;
+        }
+        GST_DEBUG_OBJECT (pad, "Creating MPEG Audio, version 4 stream");
+        pespay->stream = tsmux_stream_new (0, TSMUX_ST_AUDIO_AAC);
+        scaps = gst_caps_from_string ("audio/mpegpes, "
+            "stream-type = (string) mpeg");
+        break;
+      }
+      default:
+        GST_WARNING_OBJECT (pad, "unsupported mpegversion %d", mpegversion);
+        goto beach;
+    }
+  } else if (gst_structure_has_name (s, "video/mpeg")) {
+    gint mpegversion;
+    if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
+      GST_ELEMENT_ERROR (pespay, STREAM, FORMAT,
+          ("Invalid data format presented"),
+          ("Caps with type video/mpeg did not have mpegversion"));
+      goto beach;
+    }
+
+    if (mpegversion == 1) {
+      GST_DEBUG_OBJECT (pad, "Creating MPEG Video, version 1 stream");
+      pespay->stream = tsmux_stream_new (0, TSMUX_ST_VIDEO_MPEG1);
+      scaps = gst_caps_from_string ("video/mpegpes, "
+          "stream-type = (string) mp1");
+    } else if (mpegversion == 2) {
+      GST_DEBUG_OBJECT (pad, "Creating MPEG Video, version 2 stream");
+      pespay->stream = tsmux_stream_new (0, TSMUX_ST_VIDEO_MPEG2);
+      scaps = gst_caps_from_string ("video/mpegpes, "
+          "stream-type = (string) mp2");
+    } else {
+      GST_DEBUG_OBJECT (pad, "Creating MPEG Video, version 4 stream");
+      pespay->stream = tsmux_stream_new (0, TSMUX_ST_VIDEO_MPEG4);
+      scaps = gst_caps_from_string ("video/mpegpes, "
+          "stream-type = (string) mp4");
+    }
+  }
+
+  if (pespay->stream != NULL) {
+    gst_structure_get_int (s, "rate", &pespay->stream->audio_sampling);
+    gst_structure_get_int (s, "channels", &pespay->stream->audio_channels);
+    gst_structure_get_int (s, "bitrate", &pespay->stream->audio_bitrate);
+
+    tsmux_stream_set_buffer_release_func (pespay->stream, release_buffer_cb);
+
+    tcaps = gst_static_pad_template_get_caps (&mpegpespay_src_factory);
+    pespay->srccaps = gst_caps_intersect (scaps, tcaps);
+    gst_caps_unref (scaps);
+    gst_caps_unref (tcaps);
+
+    ret = TRUE;
+  }
+
+beach:
+  return ret;
+}
+
+static gboolean
+write_pes_packet (MpegPesPay * pespay, GstBuffer * buf)
+{
+  GstFlowReturn ret;
+
+  GST_LOG_OBJECT (pespay, "Outputting a packet of length %d",
+      GST_BUFFER_SIZE (buf));
+
+  GST_BUFFER_TIMESTAMP (buf) = pespay->last_ts;
+
+  if (pespay->is_delta) {
+    GST_LOG_OBJECT (pespay, "marking as delta unit");
+    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
+  } else {
+    GST_DEBUG_OBJECT (pespay, "marking as non-delta unit");
+    pespay->is_delta = TRUE;
+  }
+
+  gst_buffer_set_caps (buf, pespay->srccaps);
+  ret = gst_pad_push (pespay->srcpad, buf);
+  if (G_UNLIKELY (ret != GST_FLOW_OK)) {
+    pespay->last_flow_ret = ret;
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+GstFlowReturn
+mpegpespay_chain (GstPad * pad, GstBuffer * buf)
+{
+  MpegPesPay *pespay = GST_MPEG_PES_PAY (GST_OBJECT_PARENT (pad));
+  GstFlowReturn ret = GST_FLOW_OK;
+  gint64 pts = -1;
+  gboolean delta = TRUE;
+
+  g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
+  if (!GST_BUFFER_CAPS (buf)) {
+    g_critical ("Need parsed stream to packetize\n");
+    return GST_FLOW_NOT_NEGOTIATED;
+  }
+
+  if (pespay->stream->is_video_stream)
+    delta = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
+  GST_DEBUG_OBJECT (pespay, "delta: %d", delta);
+
+  if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
+    /* Ignore timestamps that go backward for now. FIXME: Handle all
+     * incoming PTS */
+    if (pespay->last_ts == GST_CLOCK_TIME_NONE ||
+        pespay->last_ts < GST_BUFFER_TIMESTAMP (buf)) {
+      pespay->cur_ts = pespay->last_ts = GST_BUFFER_TIMESTAMP (buf);
+    } else {
+      GST_DEBUG_OBJECT (pespay, "Ignoring PTS that has gone backward");
+    }
+  } else
+    pespay->cur_ts = GST_CLOCK_TIME_NONE;
+  /* FIXME: for video streams we may need DTS! */
+
+//  g_warning("ts=%li\n", pespay->cur_ts);
+  if (!pespay->no_pts && GST_CLOCK_TIME_IS_VALID (pespay->cur_ts)) {
+    pts = GSTTIME_TO_MPEGTIME (pespay->cur_ts);
+    GST_DEBUG_OBJECT (pespay, "Buffer has TS %" GST_TIME_FORMAT " pts %"
+        G_GINT64_FORMAT, GST_TIME_ARGS (pespay->cur_ts), pts);
+  }
+
+  tsmux_stream_add_data (pespay->stream, GST_BUFFER_DATA (buf),
+      GST_BUFFER_SIZE (buf), buf, pts, -1, !delta);
+
+  pespay->is_delta = delta;
+  while (tsmux_stream_bytes_in_buffer (pespay->stream) > 0) {
+    gboolean written = FALSE, got_data;
+    guint len;
+    GstBuffer *out_buf;
+    tsmux_stream_initialize_pes_packet (pespay->stream);
+    len = MIN (tsmux_stream_bytes_avail (pespay->stream), TSMUX_MAX_PES_LENGTH);
+    out_buf = gst_buffer_new_and_alloc (len);
+    got_data = tsmux_stream_get_data (pespay->stream,
+        GST_BUFFER_DATA (out_buf), len);
+    if (got_data)
+      written = write_pes_packet (pespay, out_buf);
+    if (!written) {
+      GST_DEBUG_OBJECT (pespay, "Failed to write data packet");
+      goto write_fail;
+    }
+  }
+  pespay->last_ts = pespay->last_ts;
+
+  return ret;
+write_fail:
+  /* FIXME: Failed writing data for some reason. Should set appropriate error */
+  return pespay->last_flow_ret;
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "mpegpespay", GST_RANK_PRIMARY,
+          mpegpespay_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (mpegpespay_debug, "mpegpespay", 0,
+      "MPEG PES payloader");
+
+  return TRUE;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+    "mpegpespay", "MPEG PES payloader",
+    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
diff --git a/gst/mpegtsmux/mpegpespay.h b/gst/mpegtsmux/mpegpespay.h
new file mode 100644
index 0000000..ddde998
--- /dev/null
+++ b/gst/mpegtsmux/mpegpespay.h
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __MPEGPESPAY_H__
+#define __MPEGPESPAY_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+#include <mpegtsmux.h>
+#define GST_TYPE_MPEG_PES_PAY  (mpegpespay_get_type())
+#define GST_MPEG_PES_PAY(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPEG_PES_PAY, MpegPesPay))
+typedef struct MpegPesPay MpegPesPay;
+typedef struct MpegPesPayClass MpegPesPayClass;
+
+struct MpegPesPay
+{
+  GstElement parent;
+  GstFlowReturn last_flow_ret;
+  GstClockTime cur_ts;          /* Adjusted TS for the pulled buffer */
+  GstClockTime last_ts;
+  gboolean is_delta;
+  gboolean no_pts;
+
+  GstPad *srcpad;
+  GstPad *sinkpad;
+  GstCaps *srccaps;
+
+  TsMuxStream *stream;
+  GstBuffer *codec_data;        /* Optional codec data available in the caps */
+  gpointer prepare_data;        /* Opaque data pointer to a structure used by the
+                                   prepare function */
+  MpegTsPadDataPrepareFunction prepare_func;    /* Handler to prepare input data */
+  MpegTsPadDataFreePrepareDataFunction free_func;       /* Handler to free the private data */
+};
+
+struct MpegPesPayClass
+{
+  GstElementClass parent_class;
+};
+
+GType mpegpespay_get_type (void);
+
+#define CLOCK_BASE 9LL
+#define CLOCK_FREQ (CLOCK_BASE * 10000)
+
+#define MPEGTIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), \
+                        GST_MSECOND/10, CLOCK_BASE))
+#define GSTTIME_TO_MPEGTIME(time) (gst_util_uint64_scale ((time), \
+                        CLOCK_BASE, GST_MSECOND/10))
+
+#define STANDARD_TIME_CLOCK     27000000
+/*33 bits as 1 ie 0x1ffffffff*/
+#define TWO_POW_33_MINUS1     ((0xffffffff * 2) - 1)
+
+G_END_DECLS
+#endif
diff --git a/gst/mpegtsmux/mpegtsmux_aac.c b/gst/mpegtsmux/mpegtsmux_aac.c
index c433664..c97b99e 100644
--- a/gst/mpegtsmux/mpegtsmux_aac.c
+++ b/gst/mpegtsmux/mpegtsmux_aac.c
@@ -87,8 +87,6 @@
 #include "mpegtsmux_aac.h"
 #include <string.h>
 
-#define GST_CAT_DEFAULT mpegtsmux_debug
-
 GstBuffer *
 mpegtsmux_prepare_aac (GstBuffer * buf, MpegTsPadData * data, MpegTsMux * mux)
 {
diff --git a/gst/mpegtsmux/mpegtsmux_h264.c b/gst/mpegtsmux/mpegtsmux_h264.c
index bdf7f2f..2c165e0 100644
--- a/gst/mpegtsmux/mpegtsmux_h264.c
+++ b/gst/mpegtsmux/mpegtsmux_h264.c
@@ -87,8 +87,6 @@
 #include "mpegtsmux_h264.h"
 #include <string.h>
 
-#define GST_CAT_DEFAULT mpegtsmux_debug
-
 #define SPS_PPS_PERIOD GST_SECOND
 
 typedef struct PrivDataH264 PrivDataH264;
diff --git a/gst/mpegtsmux/tsmux/tsmux.c b/gst/mpegtsmux/tsmux/tsmux.c
index 72095d0..e160197 100644
--- a/gst/mpegtsmux/tsmux/tsmux.c
+++ b/gst/mpegtsmux/tsmux/tsmux.c
@@ -87,8 +87,6 @@
 #include "tsmuxstream.h"
 #include "crc.h"
 
-#define GST_CAT_DEFAULT mpegtsmux_debug
-
 /* Maximum total data length for a PAT section is 1024 bytes, minus an 
  * 8 byte header, then the length of each program entry is 32 bits, 
  * then finally a 32 bit CRC. Thus the maximum number of programs in this mux
diff --git a/gst/mpegtsmux/tsmux/tsmuxcommon.h b/gst/mpegtsmux/tsmux/tsmuxcommon.h
index 9657b30..2401ed8 100644
--- a/gst/mpegtsmux/tsmux/tsmuxcommon.h
+++ b/gst/mpegtsmux/tsmux/tsmuxcommon.h
@@ -168,7 +174,6 @@ tsmux_put_ts (guint8 **pos, guint8 id, gint64 ts)
   tsmux_put16 (pos, ((ts << 1) & 0xfffe) | 0x01);
 }
 
-GST_DEBUG_CATEGORY_EXTERN (mpegtsmux_debug);
 #define TS_DEBUG GST_DEBUG
 
 G_END_DECLS
diff --git a/sys/dvb/Makefile.am b/sys/dvb/Makefile.am
index bceca29..1f9fc6e 100644
--- a/sys/dvb/Makefile.am
+++ b/sys/dvb/Makefile.am
@@ -1,5 +1,5 @@
-
-plugin_LTLIBRARIES = libgstdvb.la
+plugin_LTLIBRARIES = libgstdvb.la libgstdvbsink.la libgstdvbaudiosink.la \
+	libgstdvbvideosink.la
 
 libgstdvb_la_SOURCES = \
 	gstdvb.c \
@@ -22,6 +22,30 @@ libgstdvb_la_LIBADD = $(GST_BASE_LIBS) $(GST_PLUGINS_BASE_LIBS)
 libgstdvb_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 libgstdvb_la_LIBTOOLFLAGS = --tag=disable-static
 
+libgstdvbsink_la_SOURCES = \
+	dvbsink.c
+
+libgstdvbsink_la_CFLAGS = $(GST_CFLAGS)
+libgstdvbsink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+libgstdvbsink_la_LIBADD = $(GST_BASE_LIBS)
+libgstdvbsink_la_LIBTOOLFLAGS = --tag=disable-static
+
+libgstdvbaudiosink_la_SOURCES = \
+	dvbaudiosink.c
+
+libgstdvbaudiosink_la_CFLAGS = $(GST_CFLAGS)
+libgstdvbaudiosink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+libgstdvbaudiosink_la_LIBADD = $(GST_BASE_LIBS)
+libgstdvbaudiosink_la_LIBTOOLFLAGS = --tag=disable-static
+
+libgstdvbvideosink_la_SOURCES = \
+	dvbvideosink.c
+
+libgstdvbvideosink_la_CFLAGS = $(GST_CFLAGS)
+libgstdvbvideosink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+libgstdvbvideosink_la_LIBADD = $(GST_BASE_LIBS)
+libgstdvbvideosink_la_LIBTOOLFLAGS = --tag=disable-static
+
 noinst_HEADERS = \
 	gstdvbsrc.h  \
 	dvbbasebin.h \
@@ -35,5 +59,7 @@ noinst_HEADERS = \
 	camresourcemanager.h \
 	camapplicationinfo.h \
 	camconditionalaccess.h \
-	parsechannels.h
-	
+	parsechannels.h \
+	dvbsink.h \
+	dvbaudiosink.h \
+	dvbvideosink.h
diff --git a/sys/dvb/dvbaudiosink.c b/sys/dvb/dvbaudiosink.c
new file mode 100644
index 0000000..553d37c
--- /dev/null
+++ b/sys/dvb/dvbaudiosink.c
@@ -0,0 +1,314 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <linux/dvb/audio.h>
+
+#include "dvbaudiosink.h"
+
+GST_DEBUG_CATEGORY (dvbaudiosink_debug);
+#define GST_CAT_DEFAULT dvbaudiosink_debug
+
+static GstStaticPadTemplate dvbaudiosink_sink_factory =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("audio/mpegpes, " "framed = (boolean) true"));
+
+static void dvbaudiosink_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void dvbaudiosink_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+
+static gboolean dvbaudiosink_set_caps (GstBaseSink * sink, GstCaps * caps);
+static gboolean dvbaudiosink_start (GstBaseSink * sink);
+static gboolean dvbaudiosink_stop (GstBaseSink * sink);
+static gboolean dvbaudiosink_unlock (GstBaseSink * basesink);
+static GstFlowReturn dvbaudiosink_render (GstBaseSink * sink,
+    GstBuffer * buffer);
+
+GST_BOILERPLATE (DvbAudioSink, dvbaudiosink, GstBaseSink, GST_TYPE_BASE_SINK);
+
+enum
+{
+  PROP_0,
+  PROP_AVSYNC
+};
+
+static void
+dvbaudiosink_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&dvbaudiosink_sink_factory));
+
+  gst_element_class_set_details_simple (element_class,
+      "DVB audio sink", "Sink/Audio",
+      "Sink to audio decoder using Linux DVB API",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+}
+
+static void
+dvbaudiosink_class_init (DvbAudioSinkClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
+
+  gobject_class->set_property = dvbaudiosink_set_property;
+  gobject_class->get_property = dvbaudiosink_get_property;
+  g_object_class_install_property (gobject_class, PROP_AVSYNC,
+      g_param_spec_boolean ("avsync", "AV Sync",
+          "Audio/Video synchronization",
+          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  gstbasesink_class->set_caps = dvbaudiosink_set_caps;
+  gstbasesink_class->start = dvbaudiosink_start;
+  gstbasesink_class->stop = dvbaudiosink_stop;
+  gstbasesink_class->unlock = dvbaudiosink_unlock;
+  gstbasesink_class->render = dvbaudiosink_render;
+}
+
+static void
+dvbaudiosink_init (DvbAudioSink * asink, DvbAudioSinkClass * g_class)
+{
+  asink->fname = "/dev/dvb/adapter0/audio0";
+  asink->avsync = FALSE;
+  asink->playing = FALSE;
+}
+
+static void
+dvbaudiosink_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  DvbAudioSink *asink = GST_DVB_AUDIO_SINK (object);
+
+  switch (prop_id) {
+    case PROP_AVSYNC:
+      asink->avsync = g_value_get_boolean (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+dvbaudiosink_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  DvbAudioSink *asink = GST_DVB_AUDIO_SINK (object);
+
+  switch (prop_id) {
+    case PROP_AVSYNC:
+      g_value_set_boolean (value, asink->avsync);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static gboolean
+dvbaudiosink_set_caps (GstBaseSink * sink, GstCaps * caps)
+{
+  DvbAudioSink *asink = GST_DVB_AUDIO_SINK (sink);
+  GstStructure *s = gst_caps_get_structure (caps, 0);
+  const gchar *stream_type = gst_structure_get_string (s, "stream-type");
+  int type, err, cap;
+
+  if (!stream_type)
+    return TRUE;
+  else if (strcmp (stream_type, "mpeg") == 0)
+    type = AUDIO_CAP_AAC;
+  else if (strcmp (stream_type, "mp1") == 0)
+    type = AUDIO_CAP_MP1;
+  else if (strcmp (stream_type, "mp2") == 0)
+    type = AUDIO_CAP_MP2;
+  else if (strcmp (stream_type, "ac3") == 0)
+    type = AUDIO_CAP_AC3;
+  else if (strcmp (stream_type, "lpcm") == 0)
+    type = AUDIO_CAP_LPCM;
+  else if (strcmp (stream_type, "dts") == 0)
+    type = AUDIO_CAP_DTS;
+  else {
+    g_critical ("Unknown stream-type %s\n", stream_type);
+    return FALSE;
+  }
+  err = ioctl (asink->fd, AUDIO_GET_CAPABILITIES, &cap);
+  if (err)
+    return FALSE;
+  if (!(cap & type))
+    return FALSE;
+  err = ioctl (asink->fd, AUDIO_SET_STREAMTYPE, type);
+  if (err)
+    return FALSE;
+  return TRUE;
+}
+
+static gboolean
+dvbaudiosink_start (GstBaseSink * sink)
+{
+  DvbAudioSink *asink = GST_DVB_AUDIO_SINK (sink);
+  int err;
+
+  asink->fd = open (asink->fname, O_WRONLY);
+  if (asink->fd == -1) {
+    g_critical ("Failure opening %s", asink->fname);
+    return FALSE;
+  }
+  err = ioctl (asink->fd, AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY);
+  if (err)
+    return FALSE;
+  err = ioctl (asink->fd, AUDIO_SET_AV_SYNC, asink->avsync);
+  if (err)
+    return FALSE;
+  return TRUE;
+}
+
+static gboolean
+dvbaudiosink_stop (GstBaseSink * sink)
+{
+  DvbAudioSink *asink = GST_DVB_AUDIO_SINK (sink);
+  int err;
+  gboolean ret = TRUE;
+
+  if (asink->playing) {
+    err = ioctl (asink->fd, AUDIO_STOP);
+    if (err)
+      ret = FALSE;
+    asink->playing = FALSE;
+  }
+  close (asink->fd);
+  return ret;
+}
+
+static gboolean
+dvbaudiosink_unlock (GstBaseSink * basesink)
+{
+  /* how? */
+  return TRUE;
+}
+
+static GstFlowReturn
+dvbaudiosink_render (GstBaseSink * sink, GstBuffer * buffer)
+{
+  DvbAudioSink *asink = GST_DVB_AUDIO_SINK (sink);
+  guint8 *data;
+  guint32 length;
+  int ret;
+
+  if (!asink->playing) {
+    int err = ioctl (asink->fd, AUDIO_PLAY);
+    if (err)
+      return GST_FLOW_ERROR;
+    asink->playing = TRUE;
+  }
+  data = GST_BUFFER_DATA (buffer);
+  length = GST_BUFFER_SIZE (buffer);
+  ret = write (asink->fd, data, length);
+  return (ret == length ? GST_FLOW_OK : GST_FLOW_ERROR);
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "dvbaudiosink", GST_RANK_PRIMARY,
+          dvbaudiosink_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (dvbaudiosink_debug, "dvbaudiosink", 0,
+      "Sink to audio decoder");
+
+  return TRUE;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+    "dvbaudiosink", "Sink to audio decoder",
+    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
diff --git a/sys/dvb/dvbaudiosink.h b/sys/dvb/dvbaudiosink.h
new file mode 100644
index 0000000..daadbd6
--- /dev/null
+++ b/sys/dvb/dvbaudiosink.h
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __DVBAUDIOSINK_H__
+#define __DVBAUDIOSINK_H__
+
+#include <gst/gst.h>
+#include <gst/base/gstbasesink.h>
+
+G_BEGIN_DECLS
+#define GST_TYPE_DVB_AUDIO_SINK  (dvbaudiosink_get_type())
+#define GST_DVB_AUDIO_SINK(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_DVB_AUDIO_SINK, DvbAudioSink))
+typedef struct DvbAudioSink DvbAudioSink;
+typedef struct DvbAudioSinkClass DvbAudioSinkClass;
+
+struct DvbAudioSink
+{
+  GstBaseSink parent;
+  GstPad *sinkpad;
+  const gchar *fname;
+  int fd;
+  gboolean avsync;
+  gboolean playing;
+};
+
+struct DvbAudioSinkClass
+{
+  GstBaseSinkClass parent_class;
+};
+
+GType dvbaudiosink_get_type (void);
+
+gboolean dvbaudiosink_plugin_init (void);
+
+G_END_DECLS
+#endif
diff --git a/sys/dvb/dvbsink.c b/sys/dvb/dvbsink.c
new file mode 100644
index 0000000..c592e8e
--- /dev/null
+++ b/sys/dvb/dvbsink.c
@@ -0,0 +1,318 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+
+#include "dvbsink.h"
+
+GST_DEBUG_CATEGORY (dvbsink_debug);
+#define GST_CAT_DEFAULT dvbsink_debug
+
+static void dvbsink_dispose (GObject * object);
+static void dvbsink_caps_notify_cb (GstPad * pad, GParamSpec * unused,
+    DvbSink * sink);
+static void dvbsink_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void dvbsink_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+static gboolean dvbsink_event (GstPad * pad, GstEvent * event);
+
+GST_BOILERPLATE (DvbSink, dvbsink, GstBin, GST_TYPE_BIN);
+
+enum
+{
+  PROP_0,
+  PROP_AVSYNC,
+  PROP_SYNC,
+};
+
+static void
+dvbsink_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  gst_element_class_set_details_simple (element_class,
+      "DVB Sink", "Sink",
+      "Sink to MPEG decoder using Linux DVB API",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+}
+
+static void
+dvbsink_class_init (DvbSinkClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->dispose = dvbsink_dispose;
+  gobject_class->set_property = dvbsink_set_property;
+  gobject_class->get_property = dvbsink_get_property;
+  g_object_class_install_property (gobject_class, PROP_AVSYNC,
+      g_param_spec_boolean ("avsync", "AV Sync",
+          "Audio/Video synchronization",
+          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (gobject_class, PROP_SYNC,
+      g_param_spec_boolean ("sync", "Clock sync",
+          "Clock  synchronization",
+          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+dvbsink_init (DvbSink * sink, DvbSinkClass * g_class)
+{
+  GstBin *bin = GST_BIN (sink);
+  GstPad *sinkpad, *psinkpad, *psrcpad;
+
+  sink->parser = gst_element_factory_make ("mpegpesparse", "parser");
+  gst_bin_add (bin, sink->parser);
+  psinkpad = gst_element_get_static_pad (sink->parser, "sink");
+  psrcpad = gst_element_get_static_pad (sink->parser, "src");
+  sinkpad = gst_ghost_pad_new ("sink", psinkpad);
+  g_signal_connect (G_OBJECT (psrcpad), "notify::caps",
+      G_CALLBACK (dvbsink_caps_notify_cb), sink);
+  gst_pad_set_event_function (sinkpad, dvbsink_event);
+  gst_element_add_pad (GST_ELEMENT (sink), sinkpad);
+  sink->psinkpad = psinkpad;
+  sink->sink = NULL;
+  sink->ns_evt = NULL;
+  g_value_init (&sink->avsync, G_TYPE_BOOLEAN);
+  g_value_init (&sink->sync, G_TYPE_BOOLEAN);
+  g_value_set_boolean (&sink->avsync, FALSE);
+  g_value_set_boolean (&sink->sync, TRUE);
+}
+
+static void
+dvbsink_dispose (GObject * object)
+{
+  DvbSink *sink = GST_DVB_SINK (object);
+  GstBin *bin = GST_BIN (object);
+  gst_bin_remove (bin, sink->parser);
+  if (sink->sink)
+    gst_bin_remove (bin, sink->sink);
+  g_value_unset (&sink->avsync);
+  if (sink->ns_evt)
+    gst_event_unref (sink->ns_evt);
+
+  GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
+}
+
+static void
+dvbsink_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  DvbSink *sink = GST_DVB_SINK (object);
+
+  if (sink->sink) {
+    g_object_set_property (G_OBJECT (sink->sink), pspec->name, value);
+    return;
+  }
+
+  switch (prop_id) {
+    case PROP_AVSYNC:
+      g_value_copy (value, &sink->avsync);
+      break;
+    case PROP_SYNC:
+      g_value_copy (value, &sink->sync);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+dvbsink_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  DvbSink *sink = GST_DVB_SINK (object);
+
+  if (sink->sink) {
+    g_object_get_property (G_OBJECT (sink->sink), pspec->name, value);
+    return;
+  }
+
+  switch (prop_id) {
+    case PROP_AVSYNC:
+      g_value_copy (&sink->avsync, value);
+      break;
+    case PROP_SYNC:
+      g_value_copy (&sink->sync, value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+dvbsink_caps_notify_cb (GstPad * pad, GParamSpec * unused, DvbSink * sink)
+{
+  GstBin *bin = GST_BIN (sink);
+  GstCaps *caps = GST_PAD_CAPS (pad);
+  GstStructure *s;
+  GstPadLinkReturn lret;
+  GstPad *target;
+  gboolean ok, aud = FALSE;
+  if (sink->sink || !caps)
+    return;
+  s = gst_caps_get_structure (caps, 0);
+  if (gst_structure_has_name (s, "audio/mpegpes")) {
+    GST_DEBUG_OBJECT (sink, "AUDIO media type specified\n");
+    sink->sink = gst_element_factory_make ("dvbaudiosink", "asink");
+    aud = TRUE;
+  } else if (gst_structure_has_name (s, "video/mpegpes")) {
+    GST_DEBUG_OBJECT (sink, "VIDEO media type specified\n");
+    sink->sink = gst_element_factory_make ("dvbvideosink", "vsink");
+  } else {
+    g_critical ("bad media type specified: %s\n", gst_structure_get_name (s));
+    return;
+  }
+  gst_bin_add (bin, sink->sink);
+  ok = gst_element_sync_state_with_parent (sink->sink);
+  if (!ok) {
+    /* failed element must immediately be removed */
+    gst_element_set_state (sink->sink, GST_STATE_NULL);
+    gst_bin_remove (bin, sink->sink);
+    sink->sink = NULL;
+    return;
+  }
+  target = gst_element_get_static_pad (sink->sink, "sink");
+  lret = gst_pad_link (pad, target);
+  if (lret != GST_PAD_LINK_OK)
+    return;
+  if (aud)
+    g_object_set_property (G_OBJECT (sink->sink), "avsync", &sink->avsync);
+  g_object_set_property (G_OBJECT (sink->sink), "sync", &sink->sync);
+  if (sink->ns_evt) {
+    gst_pad_send_event (sink->psinkpad, sink->ns_evt);
+    sink->ns_evt = NULL;
+  }
+}
+
+static gboolean
+dvbsink_event (GstPad * pad, GstEvent * event)
+{
+  gboolean res = TRUE;
+  DvbSink *sink = GST_DVB_SINK (GST_OBJECT_PARENT (pad));
+  GstMessage *msg;
+
+  GST_WARNING_OBJECT (sink, "Got event %s",
+      gst_event_type_get_name (GST_EVENT_TYPE (event)));
+
+  if (sink->sink)
+    return gst_pad_send_event (sink->psinkpad, event);
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_NEWSEGMENT:
+      sink->ns_evt = event;
+      break;
+    case GST_EVENT_EOS:
+      msg = gst_message_new_eos (GST_OBJECT_CAST (sink));
+      gst_message_set_seqnum (msg, gst_event_get_seqnum (event));
+      gst_pad_send_event (sink->psinkpad, event);
+      res = gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
+      break;
+    default:
+      g_critical ("Unexpected event %s\n",
+          gst_event_type_get_name (GST_EVENT_TYPE (event)));
+      gst_event_unref (event);
+      res = FALSE;
+      break;
+  }
+
+  return res;
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "dvbsink", GST_RANK_PRIMARY,
+          dvbsink_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (dvbsink_debug, "dvbsink", 0,
+      "DVB sink to MPEG decoder");
+
+  return TRUE;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+    "dvbsink", "DVB sink to MPEG decoder",
+    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
diff --git a/sys/dvb/dvbsink.h b/sys/dvb/dvbsink.h
new file mode 100644
index 0000000..258ebe6
--- /dev/null
+++ b/sys/dvb/dvbsink.h
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __DVBSINK_H__
+#define __DVBSINK_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+#define GST_TYPE_DVB_SINK  (dvbsink_get_type())
+#define GST_DVB_SINK(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_DVB_SINK, DvbSink))
+typedef struct DvbSink DvbSink;
+typedef struct DvbSinkClass DvbSinkClass;
+
+struct DvbSink
+{
+  GstBin parent;
+  GstElement *parser;
+  GstElement *sink;
+  GstPad *psinkpad;
+  GValue avsync;
+  GValue sync;
+  GstEvent *ns_evt;
+};
+
+struct DvbSinkClass
+{
+  GstBinClass parent_class;
+};
+
+GType dvbsink_get_type (void);
+
+G_END_DECLS
+#endif
diff --git a/sys/dvb/dvbvideosink.c b/sys/dvb/dvbvideosink.c
new file mode 100644
index 0000000..3d29916
--- /dev/null
+++ b/sys/dvb/dvbvideosink.c
@@ -0,0 +1,263 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <linux/dvb/video.h>
+
+#include "dvbvideosink.h"
+
+#ifndef VIDEO_CAP_H264
+#define VIDEO_CAP_H264 128
+#endif
+#ifndef VIDEO_CAP_MPEG4
+#define VIDEO_CAP_MPEG4 512
+#endif
+
+GST_DEBUG_CATEGORY (dvbvideosink_debug);
+#define GST_CAT_DEFAULT dvbvideosink_debug
+
+static GstStaticPadTemplate dvbvideosink_sink_factory =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/mpegpes, " "framed = (boolean) true"));
+
+static gboolean dvbvideosink_set_caps (GstBaseSink * sink, GstCaps * caps);
+static gboolean dvbvideosink_start (GstBaseSink * sink);
+static gboolean dvbvideosink_stop (GstBaseSink * sink);
+static gboolean dvbvideosink_unlock (GstBaseSink * basesink);
+static GstFlowReturn dvbvideosink_render (GstBaseSink * sink,
+    GstBuffer * buffer);
+
+GST_BOILERPLATE (DvbVideoSink, dvbvideosink, GstBaseSink, GST_TYPE_BASE_SINK);
+
+static void
+dvbvideosink_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&dvbvideosink_sink_factory));
+
+  gst_element_class_set_details_simple (element_class,
+      "DVB video sink", "Sink/Video",
+      "Sink to video decoder using Linux DVB API",
+      "Stas Sergeev <stsp@users.sourceforge.net>");
+}
+
+static void
+dvbvideosink_class_init (DvbVideoSinkClass * klass)
+{
+  GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
+
+  gstbasesink_class->set_caps = dvbvideosink_set_caps;
+  gstbasesink_class->start = dvbvideosink_start;
+  gstbasesink_class->stop = dvbvideosink_stop;
+  gstbasesink_class->unlock = dvbvideosink_unlock;
+  gstbasesink_class->render = dvbvideosink_render;
+}
+
+static void
+dvbvideosink_init (DvbVideoSink * vsink, DvbVideoSinkClass * g_class)
+{
+  vsink->fname = "/dev/dvb/adapter0/video0";
+  vsink->playing = FALSE;
+}
+
+static gboolean
+dvbvideosink_set_caps (GstBaseSink * sink, GstCaps * caps)
+{
+  DvbVideoSink *vsink = GST_DVB_VIDEO_SINK (sink);
+  GstStructure *s = gst_caps_get_structure (caps, 0);
+  const gchar *stream_type = gst_structure_get_string (s, "stream-type");
+  int type, err, cap;
+
+  if (!stream_type)
+    return TRUE;
+  else if (strcmp (stream_type, "mp1") == 0)
+    type = VIDEO_CAP_MPEG1;
+  else if (strcmp (stream_type, "mp2") == 0)
+    type = VIDEO_CAP_MPEG2;
+  else if (strcmp (stream_type, "mp4") == 0)
+    type = VIDEO_CAP_MPEG4;
+  else if (strcmp (stream_type, "h264") == 0)
+    type = VIDEO_CAP_H264;
+  else {
+    g_critical ("Unknown stream-type %s\n", stream_type);
+    return FALSE;
+  }
+  err = ioctl (vsink->fd, VIDEO_GET_CAPABILITIES, &cap);
+  if (err)
+    return FALSE;
+  if (!(cap & type))
+    return FALSE;
+  err = ioctl (vsink->fd, VIDEO_SET_STREAMTYPE, type);
+  if (err)
+    return FALSE;
+  return TRUE;
+}
+
+static gboolean
+dvbvideosink_start (GstBaseSink * sink)
+{
+  DvbVideoSink *vsink = GST_DVB_VIDEO_SINK (sink);
+  int err;
+
+  vsink->fd = open (vsink->fname, O_WRONLY);
+  if (vsink->fd == -1) {
+    g_critical ("Failure opening %s", vsink->fname);
+    return FALSE;
+  }
+  err = ioctl (vsink->fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY);
+  if (err)
+    return FALSE;
+  return TRUE;
+}
+
+static gboolean
+dvbvideosink_stop (GstBaseSink * sink)
+{
+  DvbVideoSink *vsink = GST_DVB_VIDEO_SINK (sink);
+  int err;
+  gboolean ret = TRUE;
+
+  if (vsink->playing) {
+    err = ioctl (vsink->fd, VIDEO_STOP);
+    if (err)
+      ret = FALSE;
+    vsink->playing = FALSE;
+  }
+  close (vsink->fd);
+  return ret;
+}
+
+static gboolean
+dvbvideosink_unlock (GstBaseSink * basesink)
+{
+  /* how? */
+  return TRUE;
+}
+
+static GstFlowReturn
+dvbvideosink_render (GstBaseSink * sink, GstBuffer * buffer)
+{
+  DvbVideoSink *vsink = GST_DVB_VIDEO_SINK (sink);
+  guint8 *data;
+  guint32 length;
+  int ret;
+
+  if (!vsink->playing) {
+    int err = ioctl (vsink->fd, VIDEO_PLAY);
+    if (err)
+      return GST_FLOW_ERROR;
+    vsink->playing = TRUE;
+  }
+
+  data = GST_BUFFER_DATA (buffer);
+  length = GST_BUFFER_SIZE (buffer);
+  ret = write (vsink->fd, data, length);
+  return (ret == length ? GST_FLOW_OK : GST_FLOW_ERROR);
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_element_register (plugin, "dvbvideosink", GST_RANK_PRIMARY,
+          dvbvideosink_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (dvbvideosink_debug, "dvbvideosink", 0,
+      "Sink to video decoder");
+
+  return TRUE;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
+    "dvbvideosink", "Sink to video decoder",
+    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
diff --git a/sys/dvb/dvbvideosink.h b/sys/dvb/dvbvideosink.h
new file mode 100644
index 0000000..0449bc9
--- /dev/null
+++ b/sys/dvb/dvbvideosink.h
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2011 Rikor IMT, Inc
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This library is licensed under 4 different licenses and you
+ * can choose to use it under the terms of any one of them. The
+ * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
+ * license.
+ *
+ * MPL:
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/.
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * LGPL:
+ *
+ * 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.
+ *
+ * GPL:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * MIT:
+ *
+ * Unless otherwise indicated, Source Code is licensed under MIT license.
+ * See further explanation attached in License Statement (distributed in the file
+ * LICENSE).
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __DVBVIDEOSINK_H__
+#define __DVBVIDEOSINK_H__
+
+#include <gst/gst.h>
+#include <gst/base/gstbasesink.h>
+
+G_BEGIN_DECLS
+#define GST_TYPE_DVB_VIDEO_SINK  (dvbvideosink_get_type())
+#define GST_DVB_VIDEO_SINK(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_DVB_VIDEO_SINK, DvbVideoSink))
+typedef struct DvbVideoSink DvbVideoSink;
+typedef struct DvbVideoSinkClass DvbVideoSinkClass;
+
+struct DvbVideoSink
+{
+  GstBaseSink parent;
+  GstPad *sinkpad;
+  const gchar *fname;
+  int fd;
+  gboolean avsync;
+  gboolean playing;
+};
+
+struct DvbVideoSinkClass
+{
+  GstBaseSinkClass parent_class;
+};
+
+GType dvbvideosink_get_type (void);
+
+gboolean dvbvideosink_plugin_init (void);
+
+G_END_DECLS
+#endif
