summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZaheer Abbas Merali <zaheerabbas@merali.org>2007-08-20 14:23:45 +0000
committerZaheer Abbas Merali <zaheerabbas@merali.org>2007-08-20 14:23:45 +0000
commite6c86a500d6e09439a54a82e2cfa2af9f13c5228 (patch)
tree7266ddeddc8651375461d8ec5257e853fef7b36b
parentcd2e9804d6a54744c445821d642f0254454e19bf (diff)
downloadgst-plugins-bad-e6c86a500d6e09439a54a82e2cfa2af9f13c5228.tar.gz
gst-plugins-bad-e6c86a500d6e09439a54a82e2cfa2af9f13c5228.tar.bz2
gst-plugins-bad-e6c86a500d6e09439a54a82e2cfa2af9f13c5228.zip
Add mpeg transport stream parser written by:
Original commit message from CVS: * configure.ac: * gst/mpegtsparse/Makefile.am: * gst/mpegtsparse/flutspatinfo.c: * gst/mpegtsparse/flutspatinfo.h: * gst/mpegtsparse/flutspmtinfo.c: * gst/mpegtsparse/flutspmtinfo.h: * gst/mpegtsparse/flutspmtstreaminfo.c: * gst/mpegtsparse/flutspmtstreaminfo.h: * gst/mpegtsparse/mpegtspacketizer.c: * gst/mpegtsparse/mpegtspacketizer.h: * gst/mpegtsparse/mpegtsparse.c: * gst/mpegtsparse/mpegtsparse.h: * gst/mpegtsparse/mpegtsparsemarshal.list: Add mpeg transport stream parser written by: Alessandro Decina. Includes a couple of files from the Fluendo transport stream demuxer that Fluendo have kindly allowed to be licenced under LGPL also.
-rw-r--r--ChangeLog20
m---------common0
-rw-r--r--configure.ac2
-rw-r--r--gst/mpegtsparse/Makefile.am38
-rw-r--r--gst/mpegtsparse/flutspatinfo.c116
-rw-r--r--gst/mpegtsparse/flutspatinfo.h49
-rw-r--r--gst/mpegtsparse/flutspmtinfo.c197
-rw-r--r--gst/mpegtsparse/flutspmtinfo.h58
-rw-r--r--gst/mpegtsparse/flutspmtstreaminfo.c181
-rw-r--r--gst/mpegtsparse/flutspmtstreaminfo.h55
-rw-r--r--gst/mpegtsparse/mpegtspacketizer.c619
-rw-r--r--gst/mpegtsparse/mpegtspacketizer.h107
-rw-r--r--gst/mpegtsparse/mpegtsparse.c970
-rw-r--r--gst/mpegtsparse/mpegtsparse.h77
-rw-r--r--gst/mpegtsparse/mpegtsparsemarshal.list1
15 files changed, 2490 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 1d425ee7..d3051ec5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2007-08-20 Zaheer Abbas Merali <zaheerabbas at merali dot org>
+
+ * configure.ac:
+ * gst/mpegtsparse/Makefile.am:
+ * gst/mpegtsparse/flutspatinfo.c:
+ * gst/mpegtsparse/flutspatinfo.h:
+ * gst/mpegtsparse/flutspmtinfo.c:
+ * gst/mpegtsparse/flutspmtinfo.h:
+ * gst/mpegtsparse/flutspmtstreaminfo.c:
+ * gst/mpegtsparse/flutspmtstreaminfo.h:
+ * gst/mpegtsparse/mpegtspacketizer.c:
+ * gst/mpegtsparse/mpegtspacketizer.h:
+ * gst/mpegtsparse/mpegtsparse.c:
+ * gst/mpegtsparse/mpegtsparse.h:
+ * gst/mpegtsparse/mpegtsparsemarshal.list:
+ Add mpeg transport stream parser written by:
+ Alessandro Decina. Includes a couple of files from the
+ Fluendo transport stream demuxer that Fluendo have
+ kindly allowed to be licenced under LGPL also.
+
2007-08-20 Tim-Philipp Müller <tim at centricular dot net>
Patch by: Bastien Nocera <hadess at hadess net>
diff --git a/common b/common
-Subproject b3fe2a25c1cd0f4b021795d0db7330aeb338da7
+Subproject 9c7f1a63dfed1b2770b5fdaa16e0ae114b2177c
diff --git a/configure.ac b/configure.ac
index a0f3bbc3..1005371e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -91,6 +91,7 @@ GST_PLUGINS_ALL="\
interleave \
librfb \
modplug \
+ mpegtsparse \
mpegvideoparse \
multifile \
mve \
@@ -989,6 +990,7 @@ gst/interleave/Makefile
gst/librfb/Makefile
gst/modplug/Makefile
gst/modplug/libmodplug/Makefile
+gst/mpegtsparse/Makefile
gst/mpegvideoparse/Makefile
gst/multifile/Makefile
gst/mve/Makefile
diff --git a/gst/mpegtsparse/Makefile.am b/gst/mpegtsparse/Makefile.am
new file mode 100644
index 00000000..3324a32b
--- /dev/null
+++ b/gst/mpegtsparse/Makefile.am
@@ -0,0 +1,38 @@
+plugin_LTLIBRARIES = libgstmpegtsparse.la
+
+libgstmpegtsparse_la_SOURCES = \
+ mpegtsparse.c \
+ mpegtspacketizer.c\
+ mpegtsparsemarshal.c \
+ flutspatinfo.c \
+ flutspmtinfo.c \
+ flutspmtstreaminfo.c
+
+BUILT_SOURCES = \
+ mpegtsparsemarshal.h \
+ mpegtsparsemarshal.c
+CLEANFILES = $(BUILT_SOURCES)
+
+EXTRA_libgstmpegtsparse_la_SOURCES = \
+ mpegtsparsemarshal.list
+
+mpegtsparsemarshal.h: mpegtsparsemarshal.list
+ glib-genmarshal --header --prefix=mpegts_parse_marshal $(srcdir)/mpegtsparsemarshal.list > mpegtsparsemarshal.h.tmp
+ mv mpegtsparsemarshal.h.tmp mpegtsparsemarshal.h
+
+mpegtsparsemarshal.c: mpegtsparsemarshal.list mpegtsparsemarshal.h
+ echo "#include \"mpegtsparsemarshal.h\"" >> mpegtsparsemarshal.c.tmp
+ glib-genmarshal --body --prefix=mpegts_parse_marshal $(srcdir)/mpegtsparsemarshal.list >> mpegtsparsemarshal.c.tmp
+ mv mpegtsparsemarshal.c.tmp mpegtsparsemarshal.c
+
+libgstmpegtsparse_la_CFLAGS = $(GST_CFLAGS)
+libgstmpegtsparse_la_LIBADD = $(GST_BASE_LIBS) $(GST_LIBS)
+libgstmpegtsparse_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+
+noinst_HEADERS = \
+ mpegtsparse.h \
+ mpegtsparsemarshal.h \
+ mpegtspacketizer.h \
+ flutspatinfo.h \
+ flutspmtinfo.h \
+ flutspmtstreaminfo.h
diff --git a/gst/mpegtsparse/flutspatinfo.c b/gst/mpegtsparse/flutspatinfo.c
new file mode 100644
index 00000000..47da4315
--- /dev/null
+++ b/gst/mpegtsparse/flutspatinfo.c
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ *
+ * The Original Code is Fluendo MPEG Demuxer plugin.
+ *
+ * The Initial Developer of the Original Code is Fluendo, S.L.
+ * Portions created by Fluendo, S.L. are Copyright (C) 2005
+ * Fluendo, S.L. All Rights Reserved.
+ *
+ * Contributor(s): Jan Schmidt <jan@fluendo.com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gst/gst.h>
+
+#include "flutspatinfo.h"
+
+enum
+{
+ PROP_0,
+ PROP_PROGRAM_NO,
+ PROP_PID
+};
+
+static void mpegts_pat_info_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * spec);
+static void mpegts_pat_info_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * spec);
+
+GST_BOILERPLATE (MpegTSPatInfo, mpegts_pat_info, GObject, G_TYPE_OBJECT);
+
+MpegTSPatInfo *
+mpegts_pat_info_new (guint16 program_no, guint16 pid)
+{
+ MpegTSPatInfo *info;
+
+ info = g_object_new (MPEGTS_TYPE_PAT_INFO, NULL);
+
+ info->program_no = program_no;
+ info->pid = pid;
+
+ return info;
+}
+
+static void
+mpegts_pat_info_base_init (gpointer klass)
+{
+}
+
+static void
+mpegts_pat_info_class_init (MpegTSPatInfoClass * klass)
+{
+ GObjectClass *gobject_klass = (GObjectClass *) klass;
+
+ gobject_klass->set_property = mpegts_pat_info_set_property;
+ gobject_klass->get_property = mpegts_pat_info_get_property;
+
+ g_object_class_install_property (gobject_klass, PROP_PROGRAM_NO,
+ g_param_spec_uint ("program-number", "Program Number",
+ "Program Number for this program", 0, G_MAXUINT16, 1,
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_PID,
+ g_param_spec_uint ("pid", "PID carrying PMT",
+ "PID which carries the PMT for this program", 1, G_MAXUINT16, 1,
+ G_PARAM_READABLE));
+}
+
+static void
+mpegts_pat_info_init (MpegTSPatInfo * pat_info, MpegTSPatInfoClass * klass)
+{
+}
+
+static void
+mpegts_pat_info_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * spec)
+{
+ g_return_if_fail (MPEGTS_IS_PAT_INFO (object));
+
+ /* No settable properties */
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+}
+
+static void
+mpegts_pat_info_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * spec)
+{
+ MpegTSPatInfo *pat_info;
+
+ g_return_if_fail (MPEGTS_IS_PAT_INFO (object));
+
+ pat_info = MPEGTS_PAT_INFO (object);
+
+ switch (prop_id) {
+ case PROP_PROGRAM_NO:
+ g_value_set_uint (value, pat_info->program_no);
+ break;
+ case PROP_PID:
+ g_value_set_uint (value, pat_info->pid);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+ break;
+ }
+}
diff --git a/gst/mpegtsparse/flutspatinfo.h b/gst/mpegtsparse/flutspatinfo.h
new file mode 100644
index 00000000..5f375d4c
--- /dev/null
+++ b/gst/mpegtsparse/flutspatinfo.h
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ *
+ * The Original Code is Fluendo MPEG Demuxer plugin.
+ *
+ * The Initial Developer of the Original Code is Fluendo, S.L.
+ * Portions created by Fluendo, S.L. are Copyright (C) 2005
+ * Fluendo, S.L. All Rights Reserved.
+ *
+ * Contributor(s): Jan Schmidt <jan@fluendo.com>
+ */
+
+#ifndef __MPEGTS_PAT_INFO_H__
+#define __MPEGTS_PAT_INFO_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct MpegTSPatInfoClass {
+ GObjectClass parent_class;
+} MpegTSPatInfoClass;
+
+typedef struct MpegTSPatInfo {
+ GObject parent;
+
+ guint16 pid;
+ guint16 program_no;
+} MpegTSPatInfo;
+
+#define MPEGTS_TYPE_PAT_INFO (mpegts_pat_info_get_type ())
+#define MPEGTS_IS_PAT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MPEGTS_TYPE_PAT_INFO))
+#define MPEGTS_PAT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MPEGTS_TYPE_PAT_INFO, MpegTSPatInfo))
+
+GType mpegts_pat_info_get_type (void);
+
+MpegTSPatInfo *mpegts_pat_info_new (guint16 program_no, guint16 pid);
+
+G_END_DECLS
+
+#endif
diff --git a/gst/mpegtsparse/flutspmtinfo.c b/gst/mpegtsparse/flutspmtinfo.c
new file mode 100644
index 00000000..96df0a2c
--- /dev/null
+++ b/gst/mpegtsparse/flutspmtinfo.c
@@ -0,0 +1,197 @@
+/*
+ * 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.
+ *
+ * The Original Code is Fluendo MPEG Demuxer plugin.
+ *
+ * The Initial Developer of the Original Code is Fluendo, S.L.
+ * Portions created by Fluendo, S.L. are Copyright (C) 2005
+ * Fluendo, S.L. All Rights Reserved.
+ *
+ * Contributor(s): Jan Schmidt <jan@fluendo.com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gst/gst.h>
+
+#include "flutspmtinfo.h"
+
+enum
+{
+ PROP_0,
+ PROP_PROGRAM_NO,
+ PROP_VERSION_NO,
+ PROP_PCR_PID,
+ PROP_DESCRIPTORS,
+ PROP_STREAMINFO
+};
+
+GST_BOILERPLATE (MpegTSPmtInfo, mpegts_pmt_info, GObject, G_TYPE_OBJECT);
+
+static void mpegts_pmt_info_finalize (GObject * object);
+static void mpegts_pmt_info_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * spec);
+static void mpegts_pmt_info_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * spec);
+
+static void
+mpegts_pmt_info_base_init (gpointer klass)
+{
+}
+
+static void
+mpegts_pmt_info_class_init (MpegTSPmtInfoClass * klass)
+{
+ GObjectClass *gobject_klass = (GObjectClass *) klass;
+
+ gobject_klass->finalize = mpegts_pmt_info_finalize;
+ gobject_klass->set_property = mpegts_pmt_info_set_property;
+ gobject_klass->get_property = mpegts_pmt_info_get_property;
+
+ g_object_class_install_property (gobject_klass, PROP_PROGRAM_NO,
+ g_param_spec_uint ("program-number", "Program Number",
+ "Program Number for this program", 0, G_MAXUINT16, 1,
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_PCR_PID,
+ g_param_spec_uint ("pcr-pid", "PID carrying the PCR for this program",
+ "PID which carries the PCR for this program", 1, G_MAXUINT16, 1,
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_STREAMINFO,
+ g_param_spec_value_array ("stream-info",
+ "GValueArray containing GObjects with properties",
+ "Array of GObjects containing information about the program streams",
+ g_param_spec_object ("flu-pmt-streaminfo", "FluPMTStreamInfo",
+ "Fluendo TS Demuxer PMT Stream info object",
+ MPEGTS_TYPE_PMT_STREAM_INFO, G_PARAM_READABLE),
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_VERSION_NO,
+ g_param_spec_uint ("version-number", "Version Number",
+ "Version number of this program information", 0, G_MAXUINT8, 1,
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_DESCRIPTORS,
+ g_param_spec_value_array ("descriptors",
+ "Descriptors",
+ "Value array of strings containing program descriptors",
+ g_param_spec_boxed ("descriptor",
+ "descriptor",
+ "", G_TYPE_GSTRING, G_PARAM_READABLE), G_PARAM_READABLE));
+}
+
+static void
+mpegts_pmt_info_init (MpegTSPmtInfo * pmt_info, MpegTSPmtInfoClass * klass)
+{
+ pmt_info->streams = g_value_array_new (0);
+ pmt_info->descriptors = g_value_array_new (0);
+}
+
+MpegTSPmtInfo *
+mpegts_pmt_info_new (guint16 program_no, guint16 pcr_pid, guint8 version_no)
+{
+ MpegTSPmtInfo *info;
+
+ info = g_object_new (MPEGTS_TYPE_PMT_INFO, NULL);
+
+ info->program_no = program_no;
+ info->pcr_pid = pcr_pid;
+ info->version_no = version_no;
+
+ return info;
+}
+
+static void
+mpegts_pmt_info_finalize (GObject * object)
+{
+ MpegTSPmtInfo *info = MPEGTS_PMT_INFO (object);
+
+ g_value_array_free (info->streams);
+ g_value_array_free (info->descriptors);
+
+ G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+mpegts_pmt_info_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * spec)
+{
+ g_return_if_fail (MPEGTS_IS_PMT_INFO (object));
+
+ /* No settable properties */
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+}
+
+static void
+mpegts_pmt_info_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * spec)
+{
+ MpegTSPmtInfo *pmt_info;
+
+ g_return_if_fail (MPEGTS_IS_PMT_INFO (object));
+
+ pmt_info = MPEGTS_PMT_INFO (object);
+
+ switch (prop_id) {
+ case PROP_PROGRAM_NO:
+ g_value_set_uint (value, pmt_info->program_no);
+ break;
+ case PROP_PCR_PID:
+ g_value_set_uint (value, pmt_info->pcr_pid);
+ break;
+ case PROP_STREAMINFO:
+ g_value_set_boxed (value, pmt_info->streams);
+ break;
+ case PROP_VERSION_NO:
+ g_value_set_uint (value, pmt_info->version_no);
+ break;
+ case PROP_DESCRIPTORS:
+ g_value_set_boxed (value, pmt_info->descriptors);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+ break;
+ }
+}
+
+void
+mpegts_pmt_info_add_descriptor (MpegTSPmtInfo * pmt_info,
+ const gchar * descriptor, guint length)
+{
+ GValue value = { 0 };
+ GString *string;
+
+ g_return_if_fail (MPEGTS_IS_PMT_INFO (pmt_info));
+
+ string = g_string_new_len (descriptor, length);
+
+ g_value_init (&value, G_TYPE_GSTRING);
+ g_value_take_boxed (&value, string);
+ g_value_array_append (pmt_info->descriptors, &value);
+ g_value_unset (&value);
+}
+
+void
+mpegts_pmt_info_add_stream (MpegTSPmtInfo * pmt_info,
+ MpegTSPmtStreamInfo * stream)
+{
+ GValue v = { 0, };
+
+ g_return_if_fail (MPEGTS_IS_PMT_INFO (pmt_info));
+ g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (stream));
+
+ g_value_init (&v, G_TYPE_OBJECT);
+ g_value_take_object (&v, stream);
+ g_value_array_append (pmt_info->streams, &v);
+}
diff --git a/gst/mpegtsparse/flutspmtinfo.h b/gst/mpegtsparse/flutspmtinfo.h
new file mode 100644
index 00000000..56c136de
--- /dev/null
+++ b/gst/mpegtsparse/flutspmtinfo.h
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ *
+ * The Original Code is Fluendo MPEG Demuxer plugin.
+ *
+ * The Initial Developer of the Original Code is Fluendo, S.L.
+ * Portions created by Fluendo, S.L. are Copyright (C) 2005
+ * Fluendo, S.L. All Rights Reserved.
+ *
+ * Contributor(s): Jan Schmidt <jan@fluendo.com>
+ */
+
+#ifndef __MPEGTS_PMT_INFO_H__
+#define __MPEGTS_PMT_INFO_H__
+
+#include <glib.h>
+#include "flutspmtstreaminfo.h"
+
+G_BEGIN_DECLS
+
+typedef struct MpegTSPmtInfoClass {
+ GObjectClass parent_class;
+} MpegTSPmtInfoClass;
+
+typedef struct MpegTSPmtInfo {
+ GObject parent;
+
+ guint16 program_no;
+ guint16 pcr_pid;
+
+ guint8 version_no;
+
+ GValueArray *descriptors;
+ GValueArray *streams;
+} MpegTSPmtInfo;
+
+MpegTSPmtInfo *mpegts_pmt_info_new (guint16 program_no, guint16 pcr_pid, guint8 version);
+void mpegts_pmt_info_add_stream (MpegTSPmtInfo *pmt_info, MpegTSPmtStreamInfo *stream);
+void mpegts_pmt_info_add_descriptor (MpegTSPmtInfo *pmt_info,
+ const gchar *descriptor, guint length);
+
+GType mpegts_pmt_info_get_type (void);
+
+#define MPEGTS_TYPE_PMT_INFO (mpegts_pmt_info_get_type ())
+#define MPEGTS_IS_PMT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MPEGTS_TYPE_PMT_INFO))
+#define MPEGTS_PMT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MPEGTS_TYPE_PMT_INFO, MpegTSPmtInfo))
+
+G_END_DECLS
+
+#endif
diff --git a/gst/mpegtsparse/flutspmtstreaminfo.c b/gst/mpegtsparse/flutspmtstreaminfo.c
new file mode 100644
index 00000000..862331e7
--- /dev/null
+++ b/gst/mpegtsparse/flutspmtstreaminfo.c
@@ -0,0 +1,181 @@
+/*
+ * 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.
+ *
+ * The Original Code is Fluendo MPEG Demuxer plugin.
+ *
+ * The Initial Developer of the Original Code is Fluendo, S.L.
+ * Portions created by Fluendo, S.L. are Copyright (C) 2005
+ * Fluendo, S.L. All Rights Reserved.
+ *
+ * Contributor(s): Jan Schmidt <jan@fluendo.com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gst/gst.h>
+
+#include "flutspmtstreaminfo.h"
+
+enum
+{
+ PROP_0,
+ PROP_PID,
+ PROP_LANGUAGES,
+ PROP_STREAM_TYPE,
+ PROP_DESCRIPTORS,
+};
+
+GST_BOILERPLATE (MpegTSPmtStreamInfo, mpegts_pmt_stream_info, GObject,
+ G_TYPE_OBJECT);
+
+static void mpegts_pmt_stream_info_set_property (GObject * object,
+ guint prop_id, const GValue * value, GParamSpec * spec);
+static void mpegts_pmt_stream_info_get_property (GObject * object,
+ guint prop_id, GValue * value, GParamSpec * spec);
+static void mpegts_pmt_stream_info_finalize (GObject * object);
+
+static void
+mpegts_pmt_stream_info_base_init (gpointer klass)
+{
+}
+
+static void
+mpegts_pmt_stream_info_class_init (MpegTSPmtStreamInfoClass * klass)
+{
+ GObjectClass *gobject_klass = (GObjectClass *) klass;
+
+ gobject_klass->set_property = mpegts_pmt_stream_info_set_property;
+ gobject_klass->get_property = mpegts_pmt_stream_info_get_property;
+ gobject_klass->finalize = mpegts_pmt_stream_info_finalize;
+
+ g_object_class_install_property (gobject_klass, PROP_PID,
+ g_param_spec_uint ("pid", "PID carrying this stream",
+ "PID which carries this stream", 1, G_MAXUINT16, 1,
+ G_PARAM_READABLE));
+ g_object_class_install_property (gobject_klass, PROP_LANGUAGES,
+ g_param_spec_value_array ("languages", "Languages of this stream",
+ "Value array of the languages of this stream",
+ g_param_spec_string ("language", "language", "language", "",
+ G_PARAM_READABLE), G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_STREAM_TYPE,
+ g_param_spec_uint ("stream-type",
+ "Stream type", "Stream type", 0, G_MAXUINT8, 0, G_PARAM_READABLE));
+
+ g_object_class_install_property (gobject_klass, PROP_DESCRIPTORS,
+ g_param_spec_value_array ("descriptors",
+ "Descriptors",
+ "Value array of strings containing stream descriptors",
+ g_param_spec_boxed ("descriptor",
+ "descriptor",
+ "", G_TYPE_GSTRING, G_PARAM_READABLE), G_PARAM_READABLE));
+}
+
+static void
+mpegts_pmt_stream_info_init (MpegTSPmtStreamInfo * pmt_stream_info,
+ MpegTSPmtStreamInfoClass * klass)
+{
+ pmt_stream_info->languages = g_value_array_new (0);
+ pmt_stream_info->descriptors = g_value_array_new (0);
+}
+
+static void
+mpegts_pmt_stream_info_finalize (GObject * object)
+{
+ MpegTSPmtStreamInfo *info = MPEGTS_PMT_STREAM_INFO (object);
+
+ g_value_array_free (info->languages);
+ g_value_array_free (info->descriptors);
+}
+
+MpegTSPmtStreamInfo *
+mpegts_pmt_stream_info_new (guint16 pid, guint8 type)
+{
+ MpegTSPmtStreamInfo *info;
+
+ info = g_object_new (MPEGTS_TYPE_PMT_STREAM_INFO, NULL);
+
+ info->pid = pid;
+ info->stream_type = type;
+ return info;
+}
+
+void
+mpegts_pmt_stream_info_add_language (MpegTSPmtStreamInfo * pmt_info,
+ gchar * language)
+{
+ GValue v = { 0, };
+
+ g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (pmt_info));
+
+ g_value_init (&v, G_TYPE_STRING);
+ g_value_take_string (&v, language);
+ g_value_array_append (pmt_info->languages, &v);
+ g_value_unset (&v);
+}
+
+void
+mpegts_pmt_stream_info_add_descriptor (MpegTSPmtStreamInfo * pmt_info,
+ const gchar * descriptor, guint length)
+{
+ GValue value = { 0 };
+ GString *string;
+
+ g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (pmt_info));
+
+ string = g_string_new_len (descriptor, length);
+
+ g_value_init (&value, G_TYPE_GSTRING);
+ g_value_take_boxed (&value, string);
+ g_value_array_append (pmt_info->descriptors, &value);
+ g_value_unset (&value);
+}
+
+static void
+mpegts_pmt_stream_info_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * spec)
+{
+ g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (object));
+
+ /* No settable properties */
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+}
+
+static void
+mpegts_pmt_stream_info_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * spec)
+{
+ MpegTSPmtStreamInfo *si;
+
+ g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (object));
+
+ si = MPEGTS_PMT_STREAM_INFO (object);
+
+ switch (prop_id) {
+ case PROP_STREAM_TYPE:
+ g_value_set_uint (value, si->stream_type);
+ break;
+ case PROP_PID:
+ g_value_set_uint (value, si->pid);
+ break;
+ case PROP_LANGUAGES:
+ g_value_set_boxed (value, si->languages);
+ break;
+ case PROP_DESCRIPTORS:
+ g_value_set_boxed (value, si->descriptors);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+ break;
+ }
+}
diff --git a/gst/mpegtsparse/flutspmtstreaminfo.h b/gst/mpegtsparse/flutspmtstreaminfo.h
new file mode 100644
index 00000000..a3d43917
--- /dev/null
+++ b/gst/mpegtsparse/flutspmtstreaminfo.h
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ *
+ * The Original Code is Fluendo MPEG Demuxer plugin.
+ *
+ * The Initial Developer of the Original Code is Fluendo, S.L.
+ * Portions created by Fluendo, S.L. are Copyright (C) 2005
+ * Fluendo, S.L. All Rights Reserved.
+ *
+ * Contributor(s): Jan Schmidt <jan@fluendo.com>
+ */
+
+#ifndef __MPEGTS_PMT_STREAM_INFO_H__
+#define __MPEGTS_PMT_STREAM_INFO_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+
+typedef struct MpegTSPmtStreamInfoClass {
+ GObjectClass parent_class;
+} MpegTSPmtStreamInfoClass;
+
+typedef struct MpegTSPmtStreamInfo {
+ GObject parent;
+
+ guint16 pid;
+ GValueArray *languages; /* null terminated 3 character ISO639 language code */
+ guint8 stream_type;
+ GValueArray *descriptors;
+} MpegTSPmtStreamInfo;
+
+MpegTSPmtStreamInfo *mpegts_pmt_stream_info_new (guint16 pid, guint8 type);
+void mpegts_pmt_stream_info_add_language(MpegTSPmtStreamInfo* si, gchar* language);
+void mpegts_pmt_stream_info_add_descriptor (MpegTSPmtStreamInfo *si, const gchar *descriptor, guint length);
+
+GType mpegts_pmt_stream_info_get_type (void);
+
+#define MPEGTS_TYPE_PMT_STREAM_INFO (mpegts_pmt_stream_info_get_type ())
+
+#define MPEGTS_IS_PMT_STREAM_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MPEGTS_TYPE_PMT_STREAM_INFO))
+#define MPEGTS_PMT_STREAM_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MPEGTS_TYPE_PMT_STREAM_INFO, MpegTSPmtStreamInfo))
+
+G_END_DECLS
+
+#endif
diff --git a/gst/mpegtsparse/mpegtspacketizer.c b/gst/mpegtsparse/mpegtspacketizer.c
new file mode 100644
index 00000000..7e51f1eb
--- /dev/null
+++ b/gst/mpegtsparse/mpegtspacketizer.c
@@ -0,0 +1,619 @@
+/*
+ * mpegtspacketizer.c -
+ * Copyright (C) 2007 Alessandro Decina
+ *
+ * Authors:
+ * Alessandro Decina <alessandro@nnva.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "mpegtspacketizer.h"
+#include "flutspatinfo.h"
+#include "flutspmtinfo.h"
+
+GST_DEBUG_CATEGORY_STATIC (mpegts_packetizer_debug);
+#define GST_CAT_DEFAULT mpegts_packetizer_debug
+
+G_DEFINE_TYPE (MpegTSPacketizer, mpegts_packetizer, G_TYPE_OBJECT);
+
+static void mpegts_packetizer_dispose (GObject * object);
+static void mpegts_packetizer_finalize (GObject * object);
+
+#define CONTINUITY_UNSET 255
+#define MAX_CONTINUITY 16
+
+typedef struct
+{
+ guint16 pid;
+ guint continuity_counter;
+ GstAdapter *section_adapter;
+ guint section_length;
+ guint8 section_version_number;
+} MpegTSPacketizerStream;
+
+static MpegTSPacketizerStream *
+mpegts_packetizer_stream_new (guint16 pid)
+{
+ MpegTSPacketizerStream *stream;
+
+ stream = (MpegTSPacketizerStream *) g_new0 (MpegTSPacketizerStream, 1);
+ stream->section_adapter = gst_adapter_new ();
+ stream->pid = pid;
+ stream->continuity_counter = CONTINUITY_UNSET;
+ return stream;
+}
+
+static void
+mpegts_packetizer_stream_free (MpegTSPacketizerStream * stream)
+{
+ gst_adapter_clear (stream->section_adapter);
+ g_object_unref (stream->section_adapter);
+ g_free (stream);
+}
+
+static void
+mpegts_packetizer_clear_section (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerStream * stream)
+{
+ gst_adapter_clear (stream->section_adapter);
+ stream->continuity_counter = CONTINUITY_UNSET;
+ stream->section_length = 0;
+}
+
+static void
+mpegts_packetizer_class_init (MpegTSPacketizerClass * klass)
+{
+ GObjectClass *gobject_class;
+
+ gobject_class = G_OBJECT_CLASS (klass);
+
+ gobject_class->dispose = mpegts_packetizer_dispose;
+ gobject_class->finalize = mpegts_packetizer_finalize;
+}
+
+static void
+mpegts_packetizer_init (MpegTSPacketizer * packetizer)
+{
+ packetizer->adapter = gst_adapter_new ();
+ packetizer->streams = g_hash_table_new (g_direct_hash, g_direct_equal);
+}
+
+static void
+mpegts_packetizer_dispose (GObject * object)
+{
+ MpegTSPacketizer *packetizer = GST_MPEGTS_PACKETIZER (object);
+
+ if (!packetizer->disposed) {
+ gst_adapter_clear (packetizer->adapter);
+ g_object_unref (packetizer->adapter);
+ packetizer->disposed = TRUE;
+ }
+
+ if (G_OBJECT_CLASS (mpegts_packetizer_parent_class)->dispose)
+ G_OBJECT_CLASS (mpegts_packetizer_parent_class)->dispose (object);
+}
+
+static gboolean
+stream_foreach_remove (gpointer key, gpointer value, gpointer data)
+{
+ MpegTSPacketizerStream *stream;
+
+ stream = (MpegTSPacketizerStream *) value;
+ mpegts_packetizer_stream_free (stream);
+
+ return TRUE;
+}
+
+static void
+mpegts_packetizer_finalize (GObject * object)
+{
+ MpegTSPacketizer *packetizer = GST_MPEGTS_PACKETIZER (object);
+
+ g_hash_table_foreach_remove (packetizer->streams,
+ stream_foreach_remove, packetizer);
+ g_hash_table_destroy (packetizer->streams);
+
+ if (G_OBJECT_CLASS (mpegts_packetizer_parent_class)->finalize)
+ G_OBJECT_CLASS (mpegts_packetizer_parent_class)->finalize (object);
+}
+
+static gboolean
+mpegts_packetizer_parse_adaptation_field_control (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerPacket * packet)
+{
+ guint8 length;
+
+ length = *packet->data++;
+
+ if (packet->adaptation_field_control == 0x02) {
+ /* no payload, adaptation field of 183 bytes */
+ if (length != 183) {
+ GST_WARNING ("PID %d afc == 0x%x and length %d != 183",
+ packet->pid, packet->adaptation_field_control, length);
+ }
+ } else if (length > 182) {
+ GST_WARNING ("PID %d afc == 0x%01x and length %d > 182",
+ packet->pid, packet->adaptation_field_control, length);
+ }
+
+ /* skip the adaptation field body for now */
+ if (packet->data + length > packet->data_end) {
+ GST_ERROR ("PID %d afc length overflows the buffer %d",
+ packet->pid, length);
+ return FALSE;
+ }
+ *packet->data += length;
+
+ return TRUE;
+}
+
+static gboolean
+mpegts_packetizer_parse_packet (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerPacket * packet)
+{
+ guint8 *data;
+
+ data = GST_BUFFER_DATA (packet->buffer);
+ /* skip sync_byte */
+ data++;
+
+ packet->payload_unit_start_indicator = (*data >> 6) & 0x01;
+ packet->pid = GST_READ_UINT16_BE (data) & 0x1FFF;
+ data += 2;
+
+ packet->adaptation_field_control = *data >> 4 & 0x03;
+ packet->continuity_counter = *data & 0x0F;
+ data += 1;
+
+ packet->data = data;
+
+ if (packet->adaptation_field_control & 0x02)
+ if (!mpegts_packetizer_parse_adaptation_field_control (packetizer, packet))
+ return FALSE;
+
+ if (packet->adaptation_field_control & 0x01)
+ packet->payload = packet->data;
+ else
+ packet->payload = NULL;
+
+ return TRUE;
+}
+
+static gboolean
+mpegts_packetizer_parse_section_header (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerStream * stream, MpegTSPacketizerSection * section)
+{
+ guint8 tmp;
+ guint8 *data;
+
+ section->complete = TRUE;
+ /* get the section buffer, pass the ownership to the caller */
+ section->buffer = gst_adapter_take_buffer (stream->section_adapter,
+ 3 + stream->section_length);
+ data = GST_BUFFER_DATA (section->buffer);
+
+ section->table_id = *data++;
+ section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF;
+ data += 2;
+
+ /* skip to the version byte */
+ data += 2;
+
+ tmp = *data++;
+ section->version_number = (tmp >> 1) & 0x1F;
+ section->current_next_indicator = tmp & 0x01;
+
+ if (!section->current_next_indicator)
+ goto not_applicable;
+
+ if (section->version_number == stream->section_version_number)
+ goto not_applicable;
+
+ stream->section_version_number = section->version_number;
+
+ return TRUE;
+
+not_applicable:
+ section->complete = FALSE;
+ gst_buffer_unref (section->buffer);
+ return TRUE;
+}
+
+GValueArray *
+mpegts_packetizer_parse_pat (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerSection * section)
+{
+ guint8 *data, *end;
+ guint16 transport_stream_id;
+ guint8 tmp;
+ guint program_number;
+ guint16 pmt_pid;
+ MpegTSPatInfo *info;
+ GValueArray *pat;
+ GValue value = { 0 };
+
+ data = GST_BUFFER_DATA (section->buffer);
+ pat = g_value_array_new (0);
+
+ section->table_id = *data++;
+ section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF;
+ data += 2;
+
+ transport_stream_id = GST_READ_UINT16_BE (data);
+ data += 2;
+
+ tmp = *data++;
+ section->version_number = (tmp >> 1) & 0x1F;
+ section->current_next_indicator = tmp & 0x01;
+
+ /* skip section_number and last_section_number */
+ data += 2;
+
+ /* stop at the CRC */
+ end = GST_BUFFER_DATA (section->buffer) + GST_BUFFER_SIZE (section->buffer);
+ while (data < end - 4) {
+ program_number = GST_READ_UINT16_BE (data);
+ data += 2;
+
+ pmt_pid = GST_READ_UINT16_BE (data) & 0x1FFF;
+ data += 2;
+
+ info = mpegts_pat_info_new (program_number, pmt_pid);
+
+ g_value_init (&value, G_TYPE_OBJECT);
+ g_value_take_object (&value, info);
+ g_value_array_append (pat, &value);
+ g_value_unset (&value);
+ }
+
+ g_assert (data == end - 4);
+
+ return pat;
+}
+
+GObject *
+mpegts_packetizer_parse_pmt (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerSection * section)
+{
+ MpegTSPmtInfo *pmt = NULL;
+ MpegTSPmtStreamInfo *stream_info;
+ guint8 *data, *end;
+ guint16 program_number;
+ guint8 tmp;
+ guint16 pcr_pid;
+ guint program_info_length;
+ guint8 tag, length;
+ guint8 stream_type;
+ guint16 pid;
+ guint stream_info_length;
+
+ /* fixed header + CRC == 16 */
+ if (GST_BUFFER_SIZE (section->buffer) < 16) {
+ GST_WARNING ("PID %d invalid PMT size %d",
+ section->pid, section->section_length);
+ goto error;
+ }
+
+ data = GST_BUFFER_DATA (section->buffer);
+ end = data + GST_BUFFER_SIZE (section->buffer);
+
+ section->table_id = *data++;
+ section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF;
+ data += 2;
+
+ program_number = GST_READ_UINT16_BE (data);
+ data += 2;
+
+ tmp = *data++;
+ section->version_number = (tmp >> 1) & 0x1F;
+ section->current_next_indicator = tmp & 0x01;
+
+ /* skip section_number and last_section_number */
+ data += 2;
+
+ pcr_pid = GST_READ_UINT16_BE (data) & 0x1FFF;
+ data += 2;
+
+ program_info_length = GST_READ_UINT16_BE (data) & 0x0FFF;
+ data += 2;
+
+ /* check that the buffer is large enough to contain at least
+ * program_info_length bytes + CRC */
+ if (data + program_info_length + 4 > end) {
+ GST_WARNING ("PID %d invalid program info length %d "
+ "left %d", section->pid, program_info_length, end - data);
+ goto error;
+ }
+
+ pmt = mpegts_pmt_info_new (program_number, pcr_pid, section->version_number);
+
+ /* parse program level descriptors */
+ while (program_info_length > 0) {
+ tag = *data++;
+ length = *data++;
+ program_info_length -= 2;
+
+ if (length > program_info_length) {
+ GST_WARNING ("PID %d invalid descriptor length %d left %d",
+ section->pid, length, program_info_length);
+ goto error;
+ }
+
+ mpegts_pmt_info_add_descriptor (pmt, (const gchar *) data - 2, 2 + length);
+ data += length;
+ program_info_length -= length;
+ }
+
+ g_assert (program_info_length == 0);
+
+ /* parse entries, cycle until there's space for another entry (at least 5
+ * bytes) plus the CRC */
+ while (data <= end - 4 - 5) {
+ stream_type = *data++;
+
+ pid = GST_READ_UINT16_BE (data) & 0x1FFF;
+ data += 2;
+
+ stream_info_length = GST_READ_UINT16_BE (data) & 0x0FFF;
+ data += 2;
+
+ if (data + stream_info_length + 4 > end) {
+ GST_WARNING ("PID %d invalid stream info length %d "
+ "left %d", section->pid, stream_info_length, end - data);
+ goto error;
+ }
+
+ GST_INFO ("PMT PID %d program_number %d pid %d",
+ section->pid, program_number, pid);
+
+ stream_info = mpegts_pmt_stream_info_new (pid, stream_type);
+
+ /* parse stream level descriptors */
+ while (stream_info_length > 0) {
+ tag = *data++;
+ length = *data++;
+ stream_info_length -= 2;
+
+ if (length > stream_info_length) {
+ GST_WARNING ("PID %d invalid descriptor length %d left %d",
+ section->pid, length, stream_info_length);
+ g_object_unref (stream_info);
+ goto error;
+ }
+
+ mpegts_pmt_stream_info_add_descriptor (stream_info,
+ (const gchar *) data - 2, 2 + length);
+ data += length;
+ stream_info_length -= length;
+ }
+
+ /* adds a ref to stream_info */
+ mpegts_pmt_info_add_stream (pmt, stream_info);
+ g_object_unref (stream_info);
+ }
+
+ g_assert (data == end - 4);
+
+ return G_OBJECT (pmt);
+
+error:
+ if (pmt)
+ g_object_unref (pmt);
+ return NULL;
+}
+
+static void
+foreach_stream_clear (gpointer key, gpointer value, gpointer data)
+{
+ MpegTSPacketizer *packetizer = (MpegTSPacketizer *) data;
+ MpegTSPacketizerStream *stream = (MpegTSPacketizerStream *) value;
+
+ mpegts_packetizer_clear_section (packetizer, stream);
+}
+
+void
+mpegts_packetizer_clear (MpegTSPacketizer * packetizer)
+{
+ g_hash_table_foreach (packetizer->streams, foreach_stream_clear, packetizer);
+ gst_adapter_clear (packetizer->adapter);
+}
+
+
+MpegTSPacketizer *
+mpegts_packetizer_new ()
+{
+ MpegTSPacketizer *packetizer;
+
+ packetizer =
+ GST_MPEGTS_PACKETIZER (g_object_new (GST_TYPE_MPEGTS_PACKETIZER, NULL));
+
+ return packetizer;
+}
+
+void
+mpegts_packetizer_push (MpegTSPacketizer * packetizer, GstBuffer * buffer)
+{
+ g_return_if_fail (GST_IS_MPEGTS_PACKETIZER (packetizer));
+ g_return_if_fail (GST_IS_BUFFER (buffer));
+
+ gst_adapter_push (packetizer->adapter, buffer);
+}
+
+gboolean
+mpegts_packetizer_has_packets (MpegTSPacketizer * packetizer)
+{
+ g_return_val_if_fail (GST_IS_MPEGTS_PACKETIZER (packetizer), FALSE);
+
+ return gst_adapter_available (packetizer->adapter) >= 188;
+}
+
+gboolean
+mpegts_packetizer_next_packet (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerPacket * packet)
+{
+ guint8 sync_byte;
+ gboolean ret = FALSE;
+
+ g_return_val_if_fail (GST_IS_MPEGTS_PACKETIZER (packetizer), FALSE);
+ g_return_val_if_fail (packet != NULL, FALSE);
+
+ packet->buffer = NULL;
+ while (gst_adapter_available (packetizer->adapter) >= 188) {
+ sync_byte = *gst_adapter_peek (packetizer->adapter, 1);
+ if (sync_byte != 0x47) {
+ GST_DEBUG ("lost sync %02x", sync_byte);
+ gst_adapter_flush (packetizer->adapter, 1);
+ continue;
+ }
+
+ packet->buffer = gst_adapter_take_buffer (packetizer->adapter, 188);
+ packet->data_start = GST_BUFFER_DATA (packet->buffer);
+ packet->data_end =
+ GST_BUFFER_DATA (packet->buffer) + GST_BUFFER_SIZE (packet->buffer);
+ ret = mpegts_packetizer_parse_packet (packetizer, packet);
+ break;
+ }
+
+ return ret;
+}
+
+void
+mpegts_packetizer_clear_packet (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerPacket * packet)
+{
+ g_return_if_fail (GST_IS_MPEGTS_PACKETIZER (packetizer));
+ g_return_if_fail (packet != NULL);
+
+ if (packet->buffer)
+ gst_buffer_unref (packet->buffer);
+ packet->buffer = NULL;
+ packet->continuity_counter = 0;
+ packet->payload_unit_start_indicator = 0;
+ packet->payload = NULL;
+ packet->data_start = NULL;
+ packet->data_end = NULL;
+}
+
+gboolean
+mpegts_packetizer_push_section (MpegTSPacketizer * packetizer,
+ MpegTSPacketizerPacket * packet, MpegTSPacketizerSection * section)
+{
+ gboolean res = FALSE;
+ MpegTSPacketizerStream *stream;
+ guint8 pointer, table_id;
+ guint section_length;
+ GstBuffer *sub_buf;
+ guint8 *data;
+
+ g_return_val_if_fail (GST_IS_MPEGTS_PACKETIZER (packetizer), FALSE);
+ g_return_val_if_fail (packet != NULL, FALSE);
+ g_return_val_if_fail (section != NULL, FALSE);
+
+ data = packet->data;
+ section->pid = packet->pid;
+
+ stream = (MpegTSPacketizerStream *) g_hash_table_lookup (packetizer->streams,
+ GINT_TO_POINTER ((gint) packet->pid));
+ if (stream == NULL) {
+ stream = mpegts_packetizer_stream_new (packet->pid);
+ g_hash_table_insert (packetizer->streams,
+ GINT_TO_POINTER ((gint) packet->pid), stream);
+ }
+
+ if (packet->payload_unit_start_indicator == 1) {
+ pointer = *data++;
+ if (data + pointer > packet->data_end) {
+ GST_WARNING ("PID %d PSI section pointer points past the end "
+ "of the buffer", packet->pid);
+ goto out;
+ }
+
+ data += pointer;
+ }
+
+ table_id = *data++;
+
+ section_length = GST_READ_UINT16_BE (data) & 0x0FFF;
+ data += 2;
+
+ /* create a sub buffer from the start of the section (table_id and
+ * section_length included) to the end */
+ sub_buf = gst_buffer_create_sub (packet->buffer,
+ data - 3 - GST_BUFFER_DATA (packet->buffer), packet->data_end - data + 3);
+
+ if (packet->payload_unit_start_indicator) {
+ if (stream->continuity_counter != CONTINUITY_UNSET) {
+ GST_WARNING ("PID %d payload_unit_start_indicator set but section "
+ "not complete (last_continuity: %d continuity: %d sec len %d buffer %d avail %d",
+ packet->pid, stream->continuity_counter, packet->continuity_counter,
+ section_length, GST_BUFFER_SIZE (sub_buf),
+ gst_adapter_available (stream->section_adapter));
+ mpegts_packetizer_clear_section (packetizer, stream);
+ }
+
+ stream->continuity_counter = packet->continuity_counter;
+ stream->section_length = section_length;
+ gst_adapter_push (stream->section_adapter, sub_buf);
+
+ res = TRUE;
+ } else if (packet->continuity_counter == stream->continuity_counter + 1 ||
+ (stream->continuity_counter == MAX_CONTINUITY &&
+ packet->continuity_counter == 0)) {
+ stream->continuity_counter = packet->continuity_counter;
+ gst_adapter_push (stream->section_adapter, sub_buf);
+
+ res = TRUE;
+ } else {
+ GST_WARNING ("PID %d section discontinuity "
+ "(last_continuity: %d continuity: %d", packet->pid,
+ stream->continuity_counter, packet->continuity_counter);
+ mpegts_packetizer_clear_section (packetizer, stream);
+ gst_buffer_unref (sub_buf);
+ }
+
+ if (res) {
+ /* we pushed some data in the section adapter, see if the section is
+ * complete now */
+
+ /* >= as sections can be padded and padding is not included in
+ * section_length */
+ if (gst_adapter_available (stream->section_adapter) >=
+ stream->section_length + 3) {
+ res = mpegts_packetizer_parse_section_header (packetizer,
+ stream, section);
+
+ /* flush stuffing bytes */
+ mpegts_packetizer_clear_section (packetizer, stream);
+ } else {
+ /* section not complete yet */
+ section->complete = FALSE;
+ }
+ } else {
+ section->complete = FALSE;
+ }
+
+out:
+ packet->data = data;
+ return res;
+}
+
+void
+mpegts_packetizer_init_debug ()
+{
+ GST_DEBUG_CATEGORY_INIT (mpegts_packetizer_debug, "mpegtspacketizer", 0,
+ "MPEG transport stream parser");
+}
diff --git a/gst/mpegtsparse/mpegtspacketizer.h b/gst/mpegtsparse/mpegtspacketizer.h
new file mode 100644
index 00000000..b617a7aa
--- /dev/null
+++ b/gst/mpegtsparse/mpegtspacketizer.h
@@ -0,0 +1,107 @@
+/*
+ * mpegtspacketizer.h -
+ * Copyright (C) 2007 Alessandro Decina
+ *
+ * Authors:
+ * Alessandro Decina <alessandro@nnva.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef GST_MPEGTS_PACKETIZER_H
+#define GST_MPEGTS_PACKETIZER_H
+
+#include <gst/gst.h>
+#include <gst/base/gstadapter.h>
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_MPEGTS_PACKETIZER \
+ (mpegts_packetizer_get_type())
+#define GST_MPEGTS_PACKETIZER(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MPEGTS_PACKETIZER,MpegTSPacketizer))
+#define GST_MPEGTS_PACKETIZER_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MPEGTS_PACKETIZER,MpegTSPacketizerClass))
+#define GST_IS_MPEGTS_PACKETIZER(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MPEGTS_PACKETIZER))
+#define GST_IS_MPEGTS_PACKETIZER_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MPEGTS_PACKETIZER))
+
+
+typedef struct _MpegTSPacketizer MpegTSPacketizer;
+typedef struct _MpegTSPacketizerClass MpegTSPacketizerClass;
+
+struct _MpegTSPacketizer {
+ GObject object;
+
+ GstAdapter *adapter;
+ GHashTable *streams;
+ gboolean disposed;
+};
+
+struct _MpegTSPacketizerClass {
+ GObjectClass object_class;
+};
+
+typedef struct
+{
+ GstBuffer *buffer;
+ gint16 pid;
+ guint8 payload_unit_start_indicator;
+ guint8 adaptation_field_control;
+ guint8 continuity_counter;
+ guint8 *payload;
+
+ guint8 *data_start;
+ guint8 *data_end;
+ guint8 *data;
+
+} MpegTSPacketizerPacket;
+
+typedef struct
+{
+ gboolean complete;
+ GstBuffer *buffer;
+ gint16 pid;
+ guint8 table_id;
+ guint section_length;
+ guint8 version_number;
+ guint8 current_next_indicator;
+} MpegTSPacketizerSection;
+
+GType gst_mpegts_packetizer_get_type(void);
+
+void mpegts_packetizer_init_debug ();
+MpegTSPacketizer *mpegts_packetizer_new ();
+void mpegts_packetizer_clear (MpegTSPacketizer *packetizer);
+void mpegts_packetizer_push (MpegTSPacketizer *packetizer, GstBuffer *buffer);
+gboolean mpegts_packetizer_has_packets (MpegTSPacketizer *packetizer);
+gboolean mpegts_packetizer_next_packet (MpegTSPacketizer *packetizer,
+ MpegTSPacketizerPacket *packet);
+void mpegts_packetizer_clear_packet (MpegTSPacketizer *packetizer,
+ MpegTSPacketizerPacket *packet);
+
+gboolean mpegts_packetizer_push_section (MpegTSPacketizer *packetzer,
+ MpegTSPacketizerPacket *packet, MpegTSPacketizerSection *section);
+GValueArray *mpegts_packetizer_parse_pat (MpegTSPacketizer *packetizer,
+ MpegTSPacketizerSection *section);
+GObject *mpegts_packetizer_parse_pmt (MpegTSPacketizer *packetizer,
+ MpegTSPacketizerSection *section);
+
+G_END_DECLS
+
+#endif /* GST_MPEGTS_PACKETIZER_H */
diff --git a/gst/mpegtsparse/mpegtsparse.c b/gst/mpegtsparse/mpegtsparse.c
new file mode 100644
index 00000000..9d344c27
--- /dev/null
+++ b/gst/mpegtsparse/mpegtsparse.c
@@ -0,0 +1,970 @@
+/*
+ * mpegtsparse.c -
+ * Copyright (C) 2007 Alessandro Decina
+ *
+ * Authors:
+ * Alessandro Decina <alessandro@nnva.org>
+ *
+ * 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "mpegtsparse.h"
+#include "mpegtsparsemarshal.h"
+#include "flutspatinfo.h"
+#include "flutspmtinfo.h"
+#include "flutspmtstreaminfo.h"
+
+GST_DEBUG_CATEGORY_STATIC (mpegts_parse_debug);
+#define GST_CAT_DEFAULT mpegts_parse_debug
+
+typedef struct _MpegTSParsePad MpegTSParsePad;
+
+typedef struct
+{
+ guint16 pid;
+ guint8 stream_type;
+} MpegTSParseStream;
+
+typedef struct
+{
+ gint program_number;
+ guint16 pmt_pid;
+ GObject *pmt_info;
+ GHashTable *streams;
+ gint patcount;
+ gint selected;
+ gboolean active;
+ MpegTSParsePad *tspad;
+} MpegTSParseProgram;
+
+struct _MpegTSParsePad
+{
+ GstPad *pad;
+
+ /* the program number that the peer wants on this pad */
+ gint program_number;
+ MpegTSParseProgram *program;
+
+ /* set to FALSE before a push and TRUE after */
+ gboolean pushed;
+
+ /* the return of the latest push */
+ GstFlowReturn flow_return;
+};
+
+static GstElementDetails mpegts_parse_details =
+GST_ELEMENT_DETAILS ("MPEG transport stream parser",
+ "Codec/Parser",
+ "Parses MPEG2 transport streams",
+ "Alessandro Decina <alessandro@nnva.org>");
+
+static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
+ GST_PAD_SINK,
+ GST_PAD_ALWAYS,
+ GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
+ );
+
+static GstStaticPadTemplate src_template =
+GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
+ GST_PAD_ALWAYS,
+ GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
+ );
+
+static GstStaticPadTemplate program_template =
+GST_STATIC_PAD_TEMPLATE ("program_%d", GST_PAD_SRC,
+ GST_PAD_SOMETIMES,
+ GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
+ );
+
+enum
+{
+ SIGNAL_PMT,
+ /* FILL ME */
+ LAST_SIGNAL
+};
+
+enum
+{
+ ARG_0,
+ PROP_PROGRAM_NUMBERS,
+ PROP_PAT_INFO
+ /* FILL ME */
+};
+
+static void mpegts_parse_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec);
+static void mpegts_parse_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec);
+static void mpegts_parse_dispose (GObject * object);
+static void mpegts_parse_finalize (GObject * object);
+
+static MpegTSParsePad *mpegts_parse_create_tspad (MpegTSParse * parse,
+ const gchar * name);
+static void mpegts_parse_destroy_tspad (MpegTSParse * parse,
+ MpegTSParsePad * tspad);
+static GstPad *mpegts_parse_activate_program (MpegTSParse * parse,
+ MpegTSParseProgram * program);
+static void mpegts_parse_free_program (MpegTSParseProgram * program);
+static void mpegts_parse_free_stream (MpegTSParseStream * ptream);
+static void mpegts_parse_reset_selected_programs (MpegTSParse * parse,
+ gchar * programs);
+
+static void mpegts_parse_pad_removed (GstElement * element, GstPad * pad);
+static GstFlowReturn mpegts_parse_chain (GstPad * pad, GstBuffer * buf);
+static gboolean mpegts_parse_sink_event (GstPad * pad, GstEvent * event);
+static GstStateChangeReturn mpegts_parse_change_state (GstElement * element,
+ GstStateChange transition);
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+GST_BOILERPLATE (MpegTSParse, mpegts_parse, GstElement, GST_TYPE_ELEMENT);
+
+static void
+mpegts_parse_base_init (gpointer klass)
+{
+ GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+
+ gst_element_class_add_pad_template (element_class,
+ gst_static_pad_template_get (&sink_template));
+ gst_element_class_add_pad_template (element_class,
+ gst_static_pad_template_get (&src_template));
+ gst_element_class_add_pad_template (element_class,
+ gst_static_pad_template_get (&program_template));
+
+ gst_element_class_set_details (element_class, &mpegts_parse_details);
+}
+
+static void
+mpegts_parse_class_init (MpegTSParseClass * klass)
+{
+ GObjectClass *gobject_class;
+ GstElementClass *element_class;
+
+ element_class = GST_ELEMENT_CLASS (klass);
+ element_class->pad_removed = mpegts_parse_pad_removed;
+ element_class->change_state = mpegts_parse_change_state;
+
+ gobject_class = G_OBJECT_CLASS (klass);
+ gobject_class->set_property = mpegts_parse_set_property;
+ gobject_class->get_property = mpegts_parse_get_property;
+ gobject_class->dispose = mpegts_parse_dispose;
+ gobject_class->finalize = mpegts_parse_finalize;
+
+ g_object_class_install_property (gobject_class, PROP_PROGRAM_NUMBERS,
+ g_param_spec_string ("program-numbers",
+ "Program Numbers",
+ "Colon separated list of programs", "", G_PARAM_READWRITE));
+
+ g_object_class_install_property (gobject_class, PROP_PAT_INFO,
+ g_param_spec_value_array ("pat-info",
+ "GValueArray containing GObjects with properties",
+ "Array of GObjects containing information from the TS PAT "
+ "about all programs listed in the current Program Association "
+ "Table (PAT)",
+ g_param_spec_object ("flu-pat-streaminfo", "FluPATStreamInfo",
+ "Fluendo TS Demuxer PAT Stream info object",
+ MPEGTS_TYPE_PAT_INFO, G_PARAM_READABLE), G_PARAM_READABLE));
+
+ signals[SIGNAL_PMT] =
+ g_signal_new ("pmt-info", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (MpegTSParseClass, pmt_info), NULL, NULL,
+ mpegts_parse_marshal_VOID__INT_OBJECT, G_TYPE_NONE, 2, G_TYPE_INT,
+ MPEGTS_TYPE_PMT_INFO);
+}
+
+static gboolean
+foreach_psi_pid_remove (gpointer key, gpointer value, gpointer data)
+{
+ return TRUE;
+}
+
+static void
+mpegts_parse_reset (MpegTSParse * parse)
+{
+ mpegts_packetizer_clear (parse->packetizer);
+ g_hash_table_foreach_remove (parse->psi_pids, foreach_psi_pid_remove, NULL);
+
+ /* PAT */
+ g_hash_table_insert (parse->psi_pids,
+ GINT_TO_POINTER (0), GINT_TO_POINTER (1));
+
+ /* pmt pids will be added and removed dinamically */
+}
+
+static void
+mpegts_parse_init (MpegTSParse * parse, MpegTSParseClass * klass)
+{
+ parse->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
+ gst_pad_set_chain_function (parse->sinkpad, mpegts_parse_chain);
+ gst_pad_set_event_function (parse->sinkpad, mpegts_parse_sink_event);
+ gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
+
+ parse->srcpad = mpegts_parse_create_tspad (parse, "src")->pad;
+ gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
+
+ parse->disposed = FALSE;
+ parse->packetizer = mpegts_packetizer_new ();
+ parse->program_numbers = g_strdup ("");
+ parse->pads_to_add = NULL;
+ parse->programs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
+ NULL, (GDestroyNotify) mpegts_parse_free_program);
+ parse->psi_pids = g_hash_table_new (g_direct_hash, g_direct_equal);
+ mpegts_parse_reset (parse);
+}
+
+static void
+mpegts_parse_dispose (GObject * object)
+{
+ MpegTSParse *parse = GST_MPEGTS_PARSE (object);
+
+ if (!parse->disposed) {
+ g_object_unref (parse->packetizer);
+ parse->disposed = TRUE;
+ }
+
+ if (G_OBJECT_CLASS (parent_class)->dispose)
+ G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+mpegts_parse_finalize (GObject * object)
+{
+ MpegTSParse *parse = GST_MPEGTS_PARSE (object);
+
+ g_free (parse->program_numbers);
+ if (parse->pat_info)
+ g_value_array_free (parse->pat_info);
+ g_hash_table_destroy (parse->programs);
+ g_hash_table_destroy (parse->psi_pids);
+
+ if (G_OBJECT_CLASS (parent_class)->finalize)
+ G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+mpegts_parse_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec)
+{
+ MpegTSParse *parse = GST_MPEGTS_PARSE (object);
+
+ switch (prop_id) {
+ case PROP_PROGRAM_NUMBERS:
+ mpegts_parse_reset_selected_programs (parse, g_value_dup_string (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+mpegts_parse_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec)
+{
+ MpegTSParse *parse = GST_MPEGTS_PARSE (object);
+
+ switch (prop_id) {
+ case PROP_PROGRAM_NUMBERS:
+ g_value_set_string (value, parse->program_numbers);
+ break;
+ case PROP_PAT_INFO:
+ g_value_set_boxed (value, parse->pat_info);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static MpegTSParseProgram *
+mpegts_parse_add_program (MpegTSParse * parse,
+ gint program_number, guint16 pmt_pid)
+{
+ MpegTSParseProgram *program;
+
+ program = g_new0 (MpegTSParseProgram, 1);
+ program->program_number = program_number;
+ program->pmt_pid = pmt_pid;
+ program->streams = g_hash_table_new_full (g_direct_hash, g_direct_equal,
+ NULL, (GDestroyNotify) mpegts_parse_free_stream);
+ program->patcount = 1;
+ program->selected = 0;
+ program->active = FALSE;
+
+ g_hash_table_insert (parse->programs,
+ GINT_TO_POINTER (program_number), program);
+
+ return program;
+}
+
+static MpegTSParseProgram *
+mpegts_parse_get_program (MpegTSParse * parse, gint program_number)
+{
+ MpegTSParseProgram *program;
+
+ program = (MpegTSParseProgram *) g_hash_table_lookup (parse->programs,
+ GINT_TO_POINTER ((gint) program_number));
+
+ return program;
+}
+
+static GstPad *
+mpegts_parse_activate_program (MpegTSParse * parse,
+ MpegTSParseProgram * program)
+{
+ MpegTSParsePad *tspad;
+ gchar *pad_name;
+
+ pad_name = g_strdup_printf ("program_%d", program->program_number);
+
+ tspad = mpegts_parse_create_tspad (parse, pad_name);
+ tspad->program_number = program->program_number;
+ tspad->program = program;
+ program->tspad = tspad;
+ g_free (pad_name);
+ gst_pad_set_active (tspad->pad, TRUE);
+ program->active = TRUE;
+
+ return tspad->pad;
+}
+
+static GstPad *
+mpegts_parse_deactivate_program (MpegTSParse * parse,
+ MpegTSParseProgram * program)
+{
+ MpegTSParsePad *tspad;
+
+ tspad = program->tspad;
+ gst_pad_set_active (tspad->pad, FALSE);
+ program->active = FALSE;
+
+ /* tspad will be destroyed in GstElementClass::pad_removed */
+
+ return tspad->pad;
+}
+
+static void
+mpegts_parse_free_program (MpegTSParseProgram * program)
+{
+ if (program->pmt_info)
+ g_object_unref (program->pmt_info);
+
+ g_hash_table_destroy (program->streams);
+
+ g_free (program);
+}
+
+static void
+mpegts_parse_remove_program (MpegTSParse * parse, gint program_number)
+{
+ g_hash_table_remove (parse->programs, GINT_TO_POINTER (program_number));
+}
+
+static void
+mpegts_parse_sync_program_pads (MpegTSParse * parse,
+ GList * to_add, GList * to_remove)
+{
+ GList *walk;
+
+ for (walk = to_remove; walk; walk = walk->next)
+ gst_element_remove_pad (GST_ELEMENT (parse), GST_PAD (walk->data));
+
+ for (walk = to_add; walk; walk = walk->next)
+ gst_element_add_pad (GST_ELEMENT (parse), GST_PAD (walk->data));
+
+ if (to_add)
+ g_list_free (to_add);
+
+ if (to_remove)
+ g_list_free (to_remove);
+}
+
+
+static MpegTSParseStream *
+mpegts_parse_program_add_stream (MpegTSParse * parse,
+ MpegTSParseProgram * program, guint16 pid, guint8 stream_type)
+{
+ MpegTSParseStream *stream;
+
+ stream = g_new0 (MpegTSParseStream, 1);
+ stream->pid = pid;
+ stream->stream_type = stream_type;
+
+ g_hash_table_insert (program->streams, GINT_TO_POINTER ((gint) pid), stream);
+
+ return stream;
+}
+
+static void
+foreach_program_activate_or_deactivate (gpointer key, gpointer value,
+ gpointer data)
+{
+ MpegTSParse *parse = GST_MPEGTS_PARSE (data);
+ MpegTSParseProgram *program = (MpegTSParseProgram *) value;
+
+ /* at this point selected programs have program->selected == 2,
+ * unselected programs thay may have to be deactivated have selected == 1 and
+ * unselected inactive programs have selected == 0 */
+
+ switch (--program->selected) {
+ case 1:
+ /* selected */
+ if (!program->active && program->pmt_pid != G_MAXUINT16)
+ parse->pads_to_add = g_list_append (parse->pads_to_add,
+ mpegts_parse_activate_program (parse, program));
+ break;
+ case 0:
+ /* unselected */
+ if (program->active)
+ parse->pads_to_remove = g_list_append (parse->pads_to_remove,
+ mpegts_parse_deactivate_program (parse, program));
+ break;
+ case -1:
+ /* was already unselected */
+ program->selected = 0;
+ break;
+ default:
+ g_return_if_reached ();
+ }
+}
+
+static void
+mpegts_parse_reset_selected_programs (MpegTSParse * parse,
+ gchar * program_numbers)
+{
+ GList *pads_to_add = NULL;
+ GList *pads_to_remove = NULL;
+
+ GST_OBJECT_LOCK (parse);
+ if (parse->program_numbers)
+ g_free (parse->program_numbers);
+
+ parse->program_numbers = program_numbers;
+
+ if (*parse->program_numbers != '\0') {
+ gint program_number;
+ MpegTSParseProgram *program;
+ gchar **progs, **walk;
+
+ progs = g_strsplit (parse->program_numbers, ":", 0);
+
+ walk = progs;
+ while (*walk != NULL) {
+ program_number = strtol (*walk, NULL, 0);
+ program = mpegts_parse_get_program (parse, program_number);
+ if (program == NULL)
+ /* create the program, it will get activated once we get a PMT for it */
+ program = mpegts_parse_add_program (parse, program_number, G_MAXUINT16);
+
+ program->selected = 2;
+ ++walk;
+ }
+ g_strfreev (progs);
+ }
+
+ g_hash_table_foreach (parse->programs,
+ foreach_program_activate_or_deactivate, parse);
+
+ pads_to_add = parse->pads_to_add;
+ parse->pads_to_add = NULL;
+ pads_to_remove = parse->pads_to_remove;
+ parse->pads_to_remove = NULL;
+ GST_OBJECT_UNLOCK (parse);
+
+ mpegts_parse_sync_program_pads (parse, pads_to_add, pads_to_remove);
+}
+
+static void
+mpegts_parse_free_stream (MpegTSParseStream * stream)
+{
+ g_free (stream);
+}
+
+static void
+mpegts_parse_program_remove_stream (MpegTSParse * parse,
+ MpegTSParseProgram * program, guint16 pid)
+{
+ g_hash_table_remove (program->streams, GINT_TO_POINTER ((gint) pid));
+}
+
+static MpegTSParsePad *
+mpegts_parse_create_tspad (MpegTSParse * parse, const gchar * pad_name)
+{
+ GstPad *pad;
+ MpegTSParsePad *tspad;
+
+ pad = gst_pad_new_from_static_template (&program_template, pad_name);
+
+ /* create our wrapper */
+ tspad = g_new0 (MpegTSParsePad, 1);
+ tspad->pad = pad;
+ tspad->program_number = -1;
+ tspad->program = NULL;
+ tspad->pushed = FALSE;
+ tspad->flow_return = GST_FLOW_NOT_LINKED;
+ gst_pad_set_element_private (pad, tspad);
+
+ return tspad;
+}
+
+static void
+mpegts_parse_destroy_tspad (MpegTSParse * parse, MpegTSParsePad * tspad)
+{
+ /* free the wrapper */
+ g_free (tspad);
+}
+
+static void
+mpegts_parse_pad_removed (GstElement * element, GstPad * pad)
+{
+ MpegTSParsePad *tspad;
+ MpegTSParse *parse = GST_MPEGTS_PARSE (element);
+
+ if (gst_pad_get_direction (pad) == GST_PAD_SINK)
+ return;
+
+ tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad);
+ mpegts_parse_destroy_tspad (parse, tspad);
+}
+
+static GstFlowReturn
+mpegts_parse_tspad_push (MpegTSParse * parse, MpegTSParsePad * tspad,
+ guint16 pid, GstBuffer * buffer)
+{
+ GstFlowReturn ret = GST_FLOW_NOT_LINKED;
+ GHashTable *pad_pids = NULL;
+ guint16 pmt_pid = G_MAXUINT16;
+
+ if (tspad->program_number != -1) {
+ if (tspad->program) {
+ pad_pids = tspad->program->streams;
+ pmt_pid = tspad->program->pmt_pid;
+ } else {
+ /* there's a program filter on the pad but the PMT for the program has not
+ * been parsed yet, ignore the pad until we get a PMT */
+ gst_buffer_unref (buffer);
+ ret = GST_FLOW_OK;
+ goto out;
+ }
+ }
+
+ /* FIXME: send all the SI pids not only PAT and PMT */
+ if (pad_pids == NULL || pid == 0 || pid == pmt_pid ||
+ g_hash_table_lookup (pad_pids, GINT_TO_POINTER ((gint) pid)) != NULL) {
+ /* push if there's no filter or if the pid is in the filter */
+ ret = gst_pad_push (tspad->pad, buffer);
+ } else {
+ gst_buffer_unref (buffer);
+ /* caps don't include this pid */
+ if (gst_pad_is_linked (tspad->pad))
+ ret = GST_FLOW_OK;
+ }
+
+out:
+ return ret;
+}
+
+static void
+pad_clear_for_push (GstPad * pad, MpegTSParse * parse)
+{
+ MpegTSParsePad *tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad);
+
+ tspad->flow_return = GST_FLOW_NOT_LINKED;
+ tspad->pushed = FALSE;
+}
+
+static GstFlowReturn
+mpegts_parse_push (MpegTSParse * parse, MpegTSPacketizerPacket * packet)
+{
+ GstIterator *iterator;
+ gboolean done = FALSE;
+ gpointer pad = NULL;
+ MpegTSParsePad *tspad;
+ guint16 pid;
+ GstBuffer *buffer;
+ GstFlowReturn ret = GST_FLOW_NOT_LINKED;
+ GstCaps *caps;
+
+ pid = packet->pid;
+ caps = gst_pad_get_caps (parse->srcpad);
+ buffer = packet->buffer;
+ gst_buffer_set_caps (buffer, caps);
+ gst_caps_unref (caps);
+
+ GST_OBJECT_LOCK (parse);
+ /* clear tspad->pushed on pads */
+ g_list_foreach (GST_ELEMENT_CAST (parse)->srcpads,
+ (GFunc) pad_clear_for_push, parse);
+ GST_OBJECT_UNLOCK (parse);
+
+ iterator = gst_element_iterate_src_pads (GST_ELEMENT_CAST (parse));
+ while (!done) {
+ switch (gst_iterator_next (iterator, &pad)) {
+ case GST_ITERATOR_OK:
+ tspad = gst_pad_get_element_private (GST_PAD (pad));
+
+ /* make sure to push only once if the iterator resyncs */
+ if (!tspad->pushed) {
+ /* ref the buffer as gst_pad_push takes a ref but we want to reuse the
+ * same buffer for next pushes */
+ gst_buffer_ref (buffer);
+ tspad->flow_return =
+ mpegts_parse_tspad_push (parse, tspad, pid, buffer);
+ tspad->pushed = TRUE;
+
+ if (GST_FLOW_IS_FATAL (tspad->flow_return)) {
+ /* return the error upstream */
+ ret = tspad->flow_return;
+ done = TRUE;
+ }
+ }
+
+ if (ret == GST_FLOW_NOT_LINKED)
+ ret = tspad->flow_return;
+
+ /* the iterator refs the pad */
+ g_object_unref (GST_PAD (pad));
+ break;
+ case GST_ITERATOR_RESYNC:
+ gst_iterator_resync (iterator);
+ break;
+ case GST_ITERATOR_DONE:
+ done = TRUE;
+ break;
+ default:
+ g_warning ("this should not be reached");
+ }
+ }
+
+ gst_iterator_free (iterator);
+
+ gst_buffer_unref (buffer);
+ packet->buffer = NULL;
+
+ return ret;
+}
+
+static gboolean
+mpegts_parse_is_psi_pid (MpegTSParse * parse, guint16 pid)
+{
+ return g_hash_table_lookup (parse->psi_pids,
+ GINT_TO_POINTER ((gint) pid)) != NULL;
+}
+
+static void
+mpegts_parse_apply_pat (MpegTSParse * parse, GValueArray * pat_info)
+{
+ GValue *value;
+ GValueArray *old_pat;
+ GObject *program_info;
+ gint program_number;
+ guint pid;
+ MpegTSParseProgram *program;
+ gint i;
+ GList *pads_to_add = NULL;
+ GList *pads_to_remove = NULL;
+
+ old_pat = parse->pat_info;
+ parse->pat_info = pat_info;
+ g_object_notify (G_OBJECT (parse), "pat-info");
+
+ GST_OBJECT_LOCK (parse);
+ /* activate the new table */
+ for (i = 0; i < pat_info->n_values; ++i) {
+ value = g_value_array_get_nth (pat_info, i);
+
+ program_info = g_value_get_object (value);
+ g_object_get (program_info,
+ "program-number", &program_number, "pid", &pid, NULL);
+
+ program = mpegts_parse_get_program (parse, program_number);
+ if (program) {
+ if (program->pmt_pid != pid) {
+ if (program->pmt_pid != G_MAXUINT16) {
+ /* pmt pid changed */
+ g_hash_table_remove (parse->psi_pids,
+ GINT_TO_POINTER ((gint) program->pmt_pid));
+ }
+
+ program->pmt_pid = pid;
+ g_hash_table_insert (parse->psi_pids,
+ GINT_TO_POINTER ((gint) pid), GINT_TO_POINTER (1));
+ }
+
+ program->patcount += 1;
+ } else {
+ GST_INFO_OBJECT (parse, "PAT adding program %d pmt_pid %d",
+ program_number, pid);
+
+ g_hash_table_insert (parse->psi_pids,
+ GINT_TO_POINTER ((gint) pid), GINT_TO_POINTER (1));
+ program = mpegts_parse_add_program (parse, program_number, pid);
+ }
+
+ if (program->selected && !program->active)
+ parse->pads_to_add = g_list_append (parse->pads_to_add,
+ mpegts_parse_activate_program (parse, program));
+ }
+
+ if (old_pat) {
+ /* deactivate the old table */
+
+ for (i = 0; i < old_pat->n_values; ++i) {
+ value = g_value_array_get_nth (old_pat, i);
+
+ program_info = g_value_get_object (value);
+ g_object_get (program_info,
+ "program-number", &program_number, "pid", &pid, NULL);
+
+ program = mpegts_parse_get_program (parse, program_number);
+ if (program->patcount-- == 1)
+ /* the program has been referenced by the new pat, keep it */
+ continue;
+
+ GST_INFO_OBJECT (parse, "PAT removing program %d pmt_pid %d",
+ program_number, pid);
+
+ if (program->active)
+ parse->pads_to_remove = g_list_append (parse->pads_to_remove,
+ mpegts_parse_deactivate_program (parse, program));
+
+ mpegts_parse_remove_program (parse, program_number);
+ g_hash_table_remove (parse->psi_pids, GINT_TO_POINTER ((gint) pid));
+ }
+
+ g_value_array_free (old_pat);
+ }
+
+ pads_to_add = parse->pads_to_add;
+ parse->pads_to_add = NULL;
+ pads_to_remove = parse->pads_to_remove;
+ parse->pads_to_remove = NULL;
+ GST_OBJECT_UNLOCK (parse);
+
+ mpegts_parse_sync_program_pads (parse, pads_to_add, pads_to_remove);
+}
+
+static void
+mpegts_parse_apply_pmt (MpegTSParse * parse,
+ guint16 pmt_pid, GObject * pmt_info)
+{
+ MpegTSParseProgram *program;
+ gint program_number;
+ guint pcr_pid;
+ guint pid;
+ guint stream_type;
+ GValueArray *old_streams;
+ GValueArray *new_streams;
+ GValue *value;
+ GObject *stream;
+ gint i;
+
+ g_object_get (pmt_info, "program_number", &program_number,
+ "pcr-pid", &pcr_pid, "stream-info", &new_streams, NULL);
+
+ GST_OBJECT_LOCK (parse);
+ program = mpegts_parse_get_program (parse, program_number);
+ if (program) {
+ if (program->pmt_info) {
+ /* deactivate old pmt */
+ g_object_get (program->pmt_info, "stream-info", &old_streams, NULL);
+
+ for (i = 0; i < old_streams->n_values; ++i) {
+ value = g_value_array_get_nth (old_streams, i);
+ stream = g_value_get_object (value);
+
+ g_object_get (stream, "pid", &pid, "stream-type", &stream_type, NULL);
+ mpegts_parse_program_remove_stream (parse, program, (guint16) pid);
+ }
+
+ g_value_array_free (old_streams);
+ g_object_unref (program->pmt_info);
+ }
+ } else {
+ /* no PAT?? */
+ g_hash_table_insert (parse->psi_pids,
+ GINT_TO_POINTER ((gint) pmt_pid), GINT_TO_POINTER (1));
+ program = mpegts_parse_add_program (parse, program_number, pid);
+ }
+
+ /* activate new pmt */
+ program->pmt_info = pmt_info;
+ program->pmt_pid = pmt_pid;
+ mpegts_parse_program_add_stream (parse, program, (guint16) pcr_pid, -1);
+
+ for (i = 0; i < new_streams->n_values; ++i) {
+ value = g_value_array_get_nth (new_streams, i);
+ stream = g_value_get_object (value);
+
+ g_object_get (stream, "pid", &pid, "stream-type", &stream_type, NULL);
+ mpegts_parse_program_add_stream (parse, program, (guint16) pid,
+ (guint8) stream_type);
+ }
+ GST_OBJECT_UNLOCK (parse);
+
+ g_value_array_free (new_streams);
+
+ g_signal_emit (parse, signals[SIGNAL_PMT], 0, program_number, pmt_info);
+}
+
+static gboolean
+mpegts_parse_handle_psi (MpegTSParse * parse, MpegTSPacketizerSection * section)
+{
+ gboolean res = TRUE;
+
+ switch (section->table_id) {
+ case 0x00:
+ {
+ /* PAT */
+ GValueArray *pat_info;
+
+ pat_info = mpegts_packetizer_parse_pat (parse->packetizer, section);
+ if (pat_info)
+ mpegts_parse_apply_pat (parse, pat_info);
+ else
+ res = FALSE;
+
+ break;
+ }
+ case 0x02:
+ {
+ /* PMT */
+ GObject *pmt_info;
+
+ pmt_info = mpegts_packetizer_parse_pmt (parse->packetizer, section);
+ if (pmt_info)
+ mpegts_parse_apply_pmt (parse, section->pid, pmt_info);
+ else
+ res = FALSE;
+
+ break;
+ }
+ default:
+ break;
+ }
+
+ return res;
+}
+
+static gboolean
+mpegts_parse_sink_event (GstPad * pad, GstEvent * event)
+{
+ gboolean res;
+ MpegTSParse *parse =
+ GST_MPEGTS_PARSE (gst_object_get_parent (GST_OBJECT (pad)));
+
+ switch (GST_EVENT_TYPE (event)) {
+ case GST_EVENT_FLUSH_STOP:
+ mpegts_packetizer_clear (parse->packetizer);
+ res = gst_pad_event_default (pad, event);
+ break;
+ default:
+ res = gst_pad_event_default (pad, event);
+ }
+
+ gst_object_unref (parse);
+ return res;
+}
+
+static GstFlowReturn
+mpegts_parse_chain (GstPad * pad, GstBuffer * buf)
+{
+ GstFlowReturn res = GST_FLOW_OK;
+ MpegTSParse *parse;
+ gboolean parsed;
+ MpegTSPacketizer *packetizer;
+ MpegTSPacketizerPacket packet;
+
+ parse = GST_MPEGTS_PARSE (gst_object_get_parent (GST_OBJECT (pad)));
+ packetizer = parse->packetizer;
+
+ mpegts_packetizer_push (parse->packetizer, buf);
+ while (mpegts_packetizer_has_packets (parse->packetizer) &&
+ !GST_FLOW_IS_FATAL (res)) {
+ /* get the next packet */
+ parsed = mpegts_packetizer_next_packet (packetizer, &packet);
+ if (!parsed)
+ /* bad header, skip the packet */
+ goto next;
+
+ /* parse PSI data */
+ if (packet.payload != NULL && mpegts_parse_is_psi_pid (parse, packet.pid)) {
+ MpegTSPacketizerSection section;
+
+ parsed = mpegts_packetizer_push_section (packetizer, &packet, &section);
+ if (!parsed)
+ /* bad section data */
+ goto next;
+
+ if (section.complete) {
+ /* section complete */
+ parsed = mpegts_parse_handle_psi (parse, &section);
+ gst_buffer_unref (section.buffer);
+ if (!parsed)
+ /* bad PSI table */
+ goto next;
+ }
+ }
+
+ /* push the packet downstream */
+ res = mpegts_parse_push (parse, &packet);
+
+ next:
+ mpegts_packetizer_clear_packet (parse->packetizer, &packet);
+ }
+
+ gst_object_unref (parse);
+ return res;
+}
+
+static GstStateChangeReturn
+mpegts_parse_change_state (GstElement * element, GstStateChange transition)
+{
+ MpegTSParse *parse;
+ GstStateChangeReturn ret;
+
+ parse = GST_MPEGTS_PARSE (element);
+ ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
+
+ switch (transition) {
+ case GST_STATE_CHANGE_PAUSED_TO_READY:
+ mpegts_parse_reset (parse);
+ break;
+ default:
+ break;
+ }
+
+ return ret;
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+ GST_DEBUG_CATEGORY_INIT (mpegts_parse_debug, "mpegtsparse", 0,
+ "MPEG transport stream parser");
+
+ mpegts_packetizer_init_debug ();
+
+ return gst_element_register (plugin, "mpegtsparse",
+ GST_RANK_NONE, GST_TYPE_MPEGTS_PARSE);
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
+ GST_VERSION_MINOR,
+ "mpegtsparse",
+ "MPEG-2 transport stream parser",
+ plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
diff --git a/gst/mpegtsparse/mpegtsparse.h b/gst/mpegtsparse/mpegtsparse.h
new file mode 100644
index 00000000..cace953a
--- /dev/null
+++ b/gst/mpegtsparse/mpegtsparse.h
@@ -0,0 +1,77 @@
+/*
+ * mpegts_parse.h - GStreamer MPEG transport stream parser
+ * Copyright (C) 2007 Alessandro Decina
+ *
+ * Authors:
+ * Alessandro Decina <alessandro@nnva.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+
+#ifndef GST_MPEG_TS_PARSE_H
+#define GST_MPEG_TS_PARSE_H
+
+#include <gst/gst.h>
+#include "mpegtspacketizer.h"
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_MPEGTS_PARSE \
+ (mpegts_parse_get_type())
+#define GST_MPEGTS_PARSE(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MPEGTS_PARSE,MpegTSParse))
+#define GST_MPEGTS_PARSE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MPEGTS_PARSE,MpegTSParseClass))
+#define GST_IS_MPEGTS_PARSE(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MPEGTS_PARSE))
+#define GST_IS_MPEGTS_PARSE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MPEGTS_PARSE))
+
+typedef struct _MpegTSParse MpegTSParse;
+typedef struct _MpegTSParseClass MpegTSParseClass;
+
+struct _MpegTSParse {
+ GstElement element;
+
+ GstPad *sinkpad;
+ GstPad *srcpad;
+
+ /* the following vars must be protected with the OBJECT_LOCK as they can be
+ * accessed from the application thread and the streaming thread */
+ gchar *program_numbers;
+ GList *pads_to_add;
+ GList *pads_to_remove;
+ GHashTable *programs;
+
+ GValueArray *pat_info;
+ MpegTSPacketizer *packetizer;
+ GHashTable *psi_pids;
+ gboolean disposed;
+};
+
+struct _MpegTSParseClass {
+ GstElementClass parent_class;
+
+ /* signals */
+ void (*pmt_info) (GObject *pmt_info);
+};
+
+GType gst_mpegts_parse_get_type(void);
+
+G_END_DECLS
+
+#endif /* GST_MPEG_TS_PARSE_H */
diff --git a/gst/mpegtsparse/mpegtsparsemarshal.list b/gst/mpegtsparse/mpegtsparsemarshal.list
new file mode 100644
index 00000000..41e7f2b5
--- /dev/null
+++ b/gst/mpegtsparse/mpegtsparsemarshal.list
@@ -0,0 +1 @@
+VOID:INT,OBJECT