diff options
author | Zaheer Abbas Merali <zaheerabbas@merali.org> | 2008-09-02 12:04:32 +0000 |
---|---|---|
committer | Zaheer Abbas Merali <zaheerabbas@merali.org> | 2008-09-02 12:04:32 +0000 |
commit | 555486f865d6933da9cf983fc161e56474468ec4 (patch) | |
tree | ebfbbef762ce08fa4beb43e2abfd95851d78ce76 /gst | |
parent | 0ce15bab49aca60f380e186c47bcb0ebe4999367 (diff) | |
download | gst-plugins-bad-555486f865d6933da9cf983fc161e56474468ec4.tar.gz gst-plugins-bad-555486f865d6933da9cf983fc161e56474468ec4.tar.bz2 gst-plugins-bad-555486f865d6933da9cf983fc161e56474468ec4.zip |
Add Fluendo MPEG PS and TS demuxers to gst-plugins-bad. This is now dual licensed MPL and LGPL.
Original commit message from CVS:
* configure.ac:
* gst/mpegdemux/Makefile.am:
* gst/mpegdemux/flumpegdemux.c:
* gst/mpegdemux/flutspatinfo.c:
* gst/mpegdemux/flutspatinfo.h:
* gst/mpegdemux/flutspmtinfo.c:
* gst/mpegdemux/flutspmtinfo.h:
* gst/mpegdemux/flutspmtstreaminfo.c:
* gst/mpegdemux/flutspmtstreaminfo.h:
* gst/mpegdemux/gstmpegdefs.h:
* gst/mpegdemux/gstmpegdemux.c:
* gst/mpegdemux/gstmpegdemux.h:
* gst/mpegdemux/gstmpegdesc.c:
* gst/mpegdemux/gstmpegdesc.h:
* gst/mpegdemux/gstmpegtsdemux.c:
* gst/mpegdemux/gstmpegtsdemux.h:
* gst/mpegdemux/gstpesfilter.c:
* gst/mpegdemux/gstpesfilter.h:
* gst/mpegdemux/gstsectionfilter.c:
* gst/mpegdemux/gstsectionfilter.h:
Add Fluendo MPEG PS and TS demuxers to gst-plugins-bad. This
is now dual licensed MPL and LGPL.
Diffstat (limited to 'gst')
-rw-r--r-- | gst/mpegdemux/Makefile.am | 28 | ||||
-rw-r--r-- | gst/mpegdemux/flumpegdemux.c | 74 | ||||
-rw-r--r-- | gst/mpegdemux/flutspatinfo.c | 139 | ||||
-rw-r--r-- | gst/mpegdemux/flutspatinfo.h | 72 | ||||
-rw-r--r-- | gst/mpegdemux/flutspmtinfo.c | 218 | ||||
-rw-r--r-- | gst/mpegdemux/flutspmtinfo.h | 81 | ||||
-rw-r--r-- | gst/mpegdemux/flutspmtstreaminfo.c | 203 | ||||
-rw-r--r-- | gst/mpegdemux/flutspmtstreaminfo.h | 80 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegdefs.h | 197 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegdemux.c | 1869 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegdemux.h | 152 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegdesc.c | 178 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegdesc.h | 271 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegtsdemux.c | 2630 | ||||
-rw-r--r-- | gst/mpegdemux/gstmpegtsdemux.h | 224 | ||||
-rw-r--r-- | gst/mpegdemux/gstpesfilter.c | 645 | ||||
-rw-r--r-- | gst/mpegdemux/gstpesfilter.h | 110 | ||||
-rw-r--r-- | gst/mpegdemux/gstsectionfilter.c | 161 | ||||
-rw-r--r-- | gst/mpegdemux/gstsectionfilter.h | 69 |
19 files changed, 7401 insertions, 0 deletions
diff --git a/gst/mpegdemux/Makefile.am b/gst/mpegdemux/Makefile.am new file mode 100644 index 00000000..e0784750 --- /dev/null +++ b/gst/mpegdemux/Makefile.am @@ -0,0 +1,28 @@ +plugin_LTLIBRARIES = libgstmpegdemux.la + +libgstmpegdemux_la_SOURCES = \ + flumpegdemux.c \ + flutspatinfo.c \ + flutspmtinfo.c \ + flutspmtstreaminfo.c \ + gstmpegdemux.c \ + gstmpegdesc.c \ + gstmpegtsdemux.c \ + gstpesfilter.c \ + gstsectionfilter.c + +libgstmpegdemux_la_CFLAGS = $(GST_CFLAGS) $(LIBOIL_CFLAGS) +libgstmpegdemux_la_LIBADD = $(GST_BASE_LIBS) $(GST_LIBS) $(LIBOIL_LIBS) +libgstmpegdemux_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + +noinst_HEADERS = \ + gstmpegdefs.h \ + gstmpegdesc.h \ + gstmpegdemux.h \ + gstpesfilter.h \ + gstmpegtsdemux.h \ + flutspatinfo.h \ + flutspmtinfo.h \ + flutspmtstreaminfo.h \ + gstsectionfilter.h + diff --git a/gst/mpegdemux/flumpegdemux.c b/gst/mpegdemux/flumpegdemux.c new file mode 100644 index 00000000..1121b18f --- /dev/null +++ b/gst/mpegdemux/flumpegdemux.c @@ -0,0 +1,74 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstmpegdemux.h" +#include "gstmpegtsdemux.h" + +GST_DEBUG_CATEGORY_EXTERN (gstflupesfilter_debug); +GST_DEBUG_CATEGORY_EXTERN (gstflusectionfilter_debug); +static gboolean +plugin_init (GstPlugin * plugin) +{ + GST_DEBUG_CATEGORY_INIT (gstflupesfilter_debug, "flupesfilter", 0, + "MPEG-TS/PS PES filter output"); + GST_DEBUG_CATEGORY_INIT (gstflusectionfilter_debug, "flusectionfilter", 0, + "MPEG-TS Section filter output"); + + if (!gst_flups_demux_plugin_init (plugin)) + return FALSE; + if (!gst_fluts_demux_plugin_init (plugin)) + return FALSE; + + return TRUE; +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "flumpegdemux", + "MPEG demuxers", + plugin_init, VERSION, + GST_LICENSE_UNKNOWN, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN); diff --git a/gst/mpegdemux/flutspatinfo.c b/gst/mpegdemux/flutspatinfo.c new file mode 100644 index 00000000..ec1a8b6e --- /dev/null +++ b/gst/mpegdemux/flutspatinfo.c @@ -0,0 +1,139 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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 fluts_pat_info_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * spec); +static void fluts_pat_info_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * spec); + +GST_BOILERPLATE (FluTsPatInfo, fluts_pat_info, GObject, G_TYPE_OBJECT); + +FluTsPatInfo * +fluts_pat_info_new (guint16 program_no, guint16 pid) +{ + FluTsPatInfo *info; + + info = g_object_new (FLUTS_TYPE_PAT_INFO, NULL); + + info->program_no = program_no; + info->pid = pid; + + return info; +} + +static void +fluts_pat_info_base_init (gpointer klass) +{ +} + +static void +fluts_pat_info_class_init (FluTsPatInfoClass * klass) +{ + GObjectClass *gobject_klass = (GObjectClass *) klass; + + gobject_klass->set_property = fluts_pat_info_set_property; + gobject_klass->get_property = fluts_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 +fluts_pat_info_init (FluTsPatInfo * pat_info, FluTsPatInfoClass * klass) +{ +} + +static void +fluts_pat_info_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * spec) +{ + g_return_if_fail (FLUTS_IS_PAT_INFO (object)); + + /* No settable properties */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); +} + +static void +fluts_pat_info_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * spec) +{ + FluTsPatInfo *pat_info; + + g_return_if_fail (FLUTS_IS_PAT_INFO (object)); + + pat_info = FLUTS_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/mpegdemux/flutspatinfo.h b/gst/mpegdemux/flutspatinfo.h new file mode 100644 index 00000000..3e93fac0 --- /dev/null +++ b/gst/mpegdemux/flutspatinfo.h @@ -0,0 +1,72 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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 __FLUTS_PAT_INFO_H__ +#define __FLUTS_PAT_INFO_H__ + +#include <glib.h> + +G_BEGIN_DECLS + +typedef struct FluTsPatInfoClass { + GObjectClass parent_class; +} FluTsPatInfoClass; + +typedef struct FluTsPatInfo { + GObject parent; + + guint16 pid; + guint16 program_no; +} FluTsPatInfo; + +#define FLUTS_TYPE_PAT_INFO (fluts_pat_info_get_type ()) +#define FLUTS_IS_PAT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), FLUTS_TYPE_PAT_INFO)) +#define FLUTS_PAT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),FLUTS_TYPE_PAT_INFO, FluTsPatInfo)) + +GType fluts_pat_info_get_type (void); + +FluTsPatInfo *fluts_pat_info_new (guint16 program_no, guint16 pid); + +G_END_DECLS + +#endif diff --git a/gst/mpegdemux/flutspmtinfo.c b/gst/mpegdemux/flutspmtinfo.c new file mode 100644 index 00000000..0f643bba --- /dev/null +++ b/gst/mpegdemux/flutspmtinfo.c @@ -0,0 +1,218 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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 (FluTsPmtInfo, fluts_pmt_info, GObject, G_TYPE_OBJECT); + +static void fluts_pmt_info_finalize (GObject * object); +static void fluts_pmt_info_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * spec); +static void fluts_pmt_info_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * spec); + +static void +fluts_pmt_info_base_init (gpointer klass) +{ +} + +static void +fluts_pmt_info_class_init (FluTsPmtInfoClass * klass) +{ + GObjectClass *gobject_klass = (GObjectClass *) klass; + + gobject_klass->finalize = fluts_pmt_info_finalize; + gobject_klass->set_property = fluts_pmt_info_set_property; + gobject_klass->get_property = fluts_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", + FLUTS_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 +fluts_pmt_info_init (FluTsPmtInfo * pmt_info, FluTsPmtInfoClass * klass) +{ + pmt_info->streams = g_value_array_new (0); + pmt_info->descriptors = g_value_array_new (0); +} + +FluTsPmtInfo * +fluts_pmt_info_new (guint16 program_no, guint16 pcr_pid, guint8 version_no) +{ + FluTsPmtInfo *info; + + info = g_object_new (FLUTS_TYPE_PMT_INFO, NULL); + + info->program_no = program_no; + info->pcr_pid = pcr_pid; + info->version_no = version_no; + + return info; +} + +static void +fluts_pmt_info_finalize (GObject * object) +{ + FluTsPmtInfo *info = FLUTS_PMT_INFO (object); + + g_value_array_free (info->streams); + g_value_array_free (info->descriptors); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +fluts_pmt_info_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * spec) +{ + g_return_if_fail (FLUTS_IS_PMT_INFO (object)); + + /* No settable properties */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); +} + +static void +fluts_pmt_info_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * spec) +{ + FluTsPmtInfo *pmt_info; + + g_return_if_fail (FLUTS_IS_PMT_INFO (object)); + + pmt_info = FLUTS_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 +fluts_pmt_info_add_descriptor (FluTsPmtInfo * pmt_info, + const gchar * descriptor, guint length) +{ + GValue value = { 0 }; + GString *string; + + g_return_if_fail (FLUTS_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 +fluts_pmt_info_add_stream (FluTsPmtInfo * pmt_info, FluTsPmtStreamInfo * stream) +{ + GValue v = { 0, }; + + g_return_if_fail (FLUTS_IS_PMT_INFO (pmt_info)); + g_return_if_fail (FLUTS_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/mpegdemux/flutspmtinfo.h b/gst/mpegdemux/flutspmtinfo.h new file mode 100644 index 00000000..2c4107e0 --- /dev/null +++ b/gst/mpegdemux/flutspmtinfo.h @@ -0,0 +1,81 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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 __FLUTS_PMT_INFO_H__ +#define __FLUTS_PMT_INFO_H__ + +#include <glib.h> +#include "flutspmtstreaminfo.h" + +G_BEGIN_DECLS + +typedef struct FluTsPmtInfoClass { + GObjectClass parent_class; +} FluTsPmtInfoClass; + +typedef struct FluTsPmtInfo { + GObject parent; + + guint16 program_no; + guint16 pcr_pid; + + guint8 version_no; + + GValueArray *descriptors; + GValueArray *streams; +} FluTsPmtInfo; + +FluTsPmtInfo *fluts_pmt_info_new (guint16 program_no, guint16 pcr_pid, guint8 version); +void fluts_pmt_info_add_stream (FluTsPmtInfo *pmt_info, FluTsPmtStreamInfo *stream); +void fluts_pmt_info_add_descriptor (FluTsPmtInfo *pmt_info, + const gchar *descriptor, guint length); + +GType fluts_pmt_info_get_type (void); + +#define FLUTS_TYPE_PMT_INFO (fluts_pmt_info_get_type ()) +#define FLUTS_IS_PMT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), FLUTS_TYPE_PMT_INFO)) +#define FLUTS_PMT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),FLUTS_TYPE_PMT_INFO, FluTsPmtInfo)) + +G_END_DECLS + +#endif diff --git a/gst/mpegdemux/flutspmtstreaminfo.c b/gst/mpegdemux/flutspmtstreaminfo.c new file mode 100644 index 00000000..f5b54962 --- /dev/null +++ b/gst/mpegdemux/flutspmtstreaminfo.c @@ -0,0 +1,203 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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 (FluTsPmtStreamInfo, fluts_pmt_stream_info, GObject, + G_TYPE_OBJECT); + +static void fluts_pmt_stream_info_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * spec); +static void fluts_pmt_stream_info_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * spec); +static void fluts_pmt_stream_info_finalize (GObject * object); + +static void +fluts_pmt_stream_info_base_init (gpointer klass) +{ +} + +static void +fluts_pmt_stream_info_class_init (FluTsPmtStreamInfoClass * klass) +{ + GObjectClass *gobject_klass = (GObjectClass *) klass; + + gobject_klass->set_property = fluts_pmt_stream_info_set_property; + gobject_klass->get_property = fluts_pmt_stream_info_get_property; + gobject_klass->finalize = fluts_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 +fluts_pmt_stream_info_init (FluTsPmtStreamInfo * pmt_stream_info, + FluTsPmtStreamInfoClass * klass) +{ + pmt_stream_info->languages = g_value_array_new (0); + pmt_stream_info->descriptors = g_value_array_new (0); +} + +static void +fluts_pmt_stream_info_finalize (GObject * object) +{ + FluTsPmtStreamInfo *info = FLUTS_PMT_STREAM_INFO (object); + + g_value_array_free (info->languages); + g_value_array_free (info->descriptors); +} + +FluTsPmtStreamInfo * +fluts_pmt_stream_info_new (guint16 pid, guint8 type) +{ + FluTsPmtStreamInfo *info; + info = g_object_new (FLUTS_TYPE_PMT_STREAM_INFO, NULL); + + info->pid = pid; + info->stream_type = type; + return info; +} + +void +fluts_pmt_stream_info_add_language (FluTsPmtStreamInfo * pmt_info, + gchar * language) +{ + GValue v = { 0, }; + + g_return_if_fail (FLUTS_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 +fluts_pmt_stream_info_add_descriptor (FluTsPmtStreamInfo * pmt_info, + const gchar * descriptor, guint length) +{ + GValue value = { 0 }; + GString *string; + + g_return_if_fail (FLUTS_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 +fluts_pmt_stream_info_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * spec) +{ + g_return_if_fail (FLUTS_IS_PMT_STREAM_INFO (object)); + + /* No settable properties */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); +} + +static void +fluts_pmt_stream_info_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * spec) +{ + FluTsPmtStreamInfo *si; + + g_return_if_fail (FLUTS_IS_PMT_STREAM_INFO (object)); + + si = FLUTS_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/mpegdemux/flutspmtstreaminfo.h b/gst/mpegdemux/flutspmtstreaminfo.h new file mode 100644 index 00000000..654daf9d --- /dev/null +++ b/gst/mpegdemux/flutspmtstreaminfo.h @@ -0,0 +1,80 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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 __FLUTS_PMT_STREAM_INFO_H__ +#define __FLUTS_PMT_STREAM_INFO_H__ + +#include <glib.h> + +G_BEGIN_DECLS + + +typedef struct FluTsPmtStreamInfoClass { + GObjectClass parent_class; +} FluTsPmtStreamInfoClass; + +typedef struct FluTsPmtStreamInfo { + GObject parent; + + guint16 pid; + GValueArray *languages; /* null terminated 3 character ISO639 language code */ + guint8 stream_type; + GValueArray *descriptors; +} FluTsPmtStreamInfo; + +FluTsPmtStreamInfo *fluts_pmt_stream_info_new (guint16 pid, guint8 type); +void fluts_pmt_stream_info_add_language(FluTsPmtStreamInfo* si, + gchar* language); +void fluts_pmt_stream_info_add_descriptor (FluTsPmtStreamInfo *pmt_info, + const gchar *descriptor, guint length); + +GType fluts_pmt_stream_info_get_type (void); + +#define FLUTS_TYPE_PMT_STREAM_INFO (fluts_pmt_stream_info_get_type ()) + +#define FLUTS_IS_PMT_STREAM_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), FLUTS_TYPE_PMT_STREAM_INFO)) +#define FLUTS_PMT_STREAM_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),FLUTS_TYPE_PMT_STREAM_INFO, FluTsPmtStreamInfo)) + +G_END_DECLS + +#endif diff --git a/gst/mpegdemux/gstmpegdefs.h b/gst/mpegdemux/gstmpegdefs.h new file mode 100644 index 00000000..c4d34b02 --- /dev/null +++ b/gst/mpegdemux/gstmpegdefs.h @@ -0,0 +1,197 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifndef __GST_MPEG_DEFS_H__ +#define __GST_MPEG_DEFS_H__ + +/* + * 1011 1100 program_stream_map + * 1011 1101 private_stream_1 + * 1011 1110 padding_stream + * 1011 1111 private_stream_2 + * 110x xxxx ISO/IEC 13818-3 or ISO/IEC 11172-3 audio stream number x xxxx + * 1110 xxxx ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 video stream number xxxx + * 1111 0000 ECM_stream + * 1111 0001 EMM_stream + * 1111 0010 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A or ISO/IEC 13818-6_DSMCC_stream + * 1111 0011 ISO/IEC_13522_stream + * 1111 0100 ITU-T Rec. H.222.1 type A + * 1111 0101 ITU-T Rec. H.222.1 type B + * 1111 0110 ITU-T Rec. H.222.1 type C + * 1111 0111 ITU-T Rec. H.222.1 type D + * 1111 1000 ITU-T Rec. H.222.1 type E + * 1111 1001 ancillary_stream + * 1111 1010 E 1111 1110 reserved data stream + * 1111 1111 program_stream_directory + */ + +#define ID_PS_END_CODE 0x000001B9 +#define ID_PS_PACK_START_CODE 0x000001BA +#define ID_PS_SYSTEM_HEADER_START_CODE 0x000001BB +#define ID_PS_PROGRAM_STREAM_MAP 0x000001BC +#define ID_PRIVATE_STREAM_1 0x000001BD +#define ID_PADDING_STREAM 0x000001BE +#define ID_PRIVATE_STREAM_2 0x000001BF +#define ID_ISO_IEC_MPEG12_AUDIO_STREAM_0 0x000001C0 +#define ID_ISO_IEC_MPEG12_AUDIO_STREAM_32 0x000001DF +#define ID_ISO_IEC_MPEG12_VIDEO_STREAM_0 0x000001E0 +#define ID_ISO_IEC_MPEG12_VIDEO_STREAM_16 0x000001EF +#define ID_ECM_STREAM 0x000001F0 +#define ID_EMM_STREAM 0x000001F1 +#define ID_DSMCC_STREAM 0x000001F2 +#define ID_ISO_IEC_13522_STREAM 0x000001F3 +#define ID_ITU_TREC_H222_TYPE_A_STREAM 0x000001F4 +#define ID_ITU_TREC_H222_TYPE_B_STREAM 0x000001F5 +#define ID_ITU_TREC_H222_TYPE_C_STREAM 0x000001F6 +#define ID_ITU_TREC_H222_TYPE_D_STREAM 0x000001F7 +#define ID_ITU_TREC_H222_TYPE_E_STREAM 0x000001F8 +#define ID_ANCILLARY_STREAM 0x000001F9 +#define ID_RESERVED_STREAM_1 0x000001FA +#define ID_RESERVED_STREAM_2 0x000001FB +#define ID_EXTENDED_METADATA 0x000001FC +#define ID_EXTENDED_STREAM_ID 0x000001FD +#define ID_RESERVED_STREAM_3 0x000001FE +#define ID_PROGRAM_STREAM_DIRECTORY 0x000001FF + +#define PACKET_VIDEO_START_CODE 0x000001E0 +#define PACKET_AUDIO_START_CODE 0x000001C0 +#define PICTURE_START_CODE 0x00000100 +#define USER_DATA_START_CODE 0x000001B2 +#define SEQUENCE_HEADER_CODE 0x000001B3 +#define SEQUENCE_ERROR_CODE 0x000001B4 +#define EXTENSION_START_CODE 0x000001B5 +#define SEQUENCE_END_CODE 0x000001B7 +#define GROUP_START_CODE 0x000001B8 + +#define AC3_SYNC_WORD 0x0b770000 + +#define MPEG_TS_SYNC_BYTE 0x00000047 + +#define PID_PROGRAM_ASSOCIATION_TABLE 0x0000 +#define PID_CONDITIONAL_ACCESS_TABLE 0x0001 +#define PID_RESERVED_FIRST 0x0002 +#define PID_RESERVED_LAST 0x0010 +#define PID_NULL_PACKET 0x1FFF + +#define PID_TYPE_UNKNOWN 0 +#define PID_TYPE_RESERVED 1 +#define PID_TYPE_PROGRAM_ASSOCIATION 2 +#define PID_TYPE_CONDITIONAL_ACCESS 3 +#define PID_TYPE_PROGRAM_MAP 4 +#define PID_TYPE_ELEMENTARY 5 +#define PID_TYPE_NULL_PACKET 6 +#define PID_TYPE_PRIVATE_SECTION 7 + +/* Stream type assignments + * + * 0x00 ITU-T | ISO/IEC Reserved + * 0x01 ISO/IEC 11172 Video + * 0x02 ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or + * ISO/IEC 11172-2 constrained parameter video + * stream + * 0x03 ISO/IEC 11172 Audio + * 0x04 ISO/IEC 13818-3 Audio + * 0x05 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 + * private_sections + * 0x06 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES + * packets containing private data + * 0x07 ISO/IEC 13522 MHEG + * 0x08 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A + * DSM CC + * 0x09 ITU-T Rec. H.222.1 + * 0x0A ISO/IEC 13818-6 type A + * 0x0B ISO/IEC 13818-6 type B + * 0x0C ISO/IEC 13818-6 type C + * 0x0D ISO/IEC 13818-6 type D + * 0x0E ISO/IEC 13818-1 auxiliary + * 0x0F-0x7F ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved + * 0x80-0xFF User Private + */ +#define ST_RESERVED 0x00 +#define ST_VIDEO_MPEG1 0x01 +#define ST_VIDEO_MPEG2 0x02 +#define ST_AUDIO_MPEG1 0x03 +#define ST_AUDIO_MPEG2 0x04 +#define ST_PRIVATE_SECTIONS 0x05 +#define ST_PRIVATE_DATA 0x06 +#define ST_MHEG 0x07 +#define ST_DSMCC 0x08 +#define ST_H222_1 0x09 + +/* later extensions */ +#define ST_AUDIO_AAC 0x0f +#define ST_VIDEO_MPEG4 0x10 +#define ST_VIDEO_H264 0x1b + +/* Un-official Dirac extension */ +#define ST_VIDEO_DIRAC 0xd1 + +/* private stream types */ +#define ST_PS_AUDIO_AC3 0x81 +#define ST_PS_AUDIO_DTS 0x8a +#define ST_PS_AUDIO_LPCM 0x8b +#define ST_PS_DVD_SUBPICTURE 0xff + +/* Un-official time-code stream */ +#define ST_PS_TIMECODE 0xd2 + +/* Internal stream types >= 0x100 */ +#define ST_GST_AUDIO_RAWA52 0x181 + /* Used when we don't yet know which stream type it will be in a PS stream */ +#define ST_GST_VIDEO_MPEG1_OR_2 0x102 + +#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 MPEG_MUX_RATE_MULT 50 + +/* some extra GstFlowReturn values used internally */ +#define GST_FLOW_NEED_MORE_DATA -100 +#define GST_FLOW_LOST_SYNC -101 + +#endif /* __GST_MPEG_DEFS_H__ */ diff --git a/gst/mpegdemux/gstmpegdemux.c b/gst/mpegdemux/gstmpegdemux.c new file mode 100644 index 00000000..a4b62d3c --- /dev/null +++ b/gst/mpegdemux/gstmpegdemux.c @@ -0,0 +1,1869 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "gstmpegdefs.h" +#include "gstmpegdemux.h" + +#define MAX_DVD_AUDIO_STREAMS 8 +#define MAX_DVD_SUBPICTURE_STREAMS 32 + +/* We clamp scr delta with 0 so negative bytes won't be possible */ +#define GSTTIME_TO_BYTES(time) \ + ((time != -1) ? gst_util_uint64_scale (MAX(0,(gint64) (GSTTIME_TO_MPEGTIME(time) - demux->first_scr)), demux->scr_rate_n, demux->scr_rate_d) : -1) +#define BYTES_TO_GSTTIME(bytes) ((bytes != -1) ? MPEGTIME_TO_GSTTIME(gst_util_uint64_scale (bytes, demux->scr_rate_d, demux->scr_rate_n)) : -1) + +#define ADAPTER_OFFSET_FLUSH(_bytes_) demux->adapter_offset += (_bytes_) + +GST_DEBUG_CATEGORY_STATIC (gstflupsdemux_debug); +#define GST_CAT_DEFAULT (gstflupsdemux_debug) + +#ifndef GST_CHECK_VERSION +#define GST_CHECK_VERSION(major,minor,micro) \ + (GST_VERSION_MAJOR > (major) || \ + (GST_VERSION_MAJOR == (major) && GST_VERSION_MINOR > (minor)) || \ + (GST_VERSION_MAJOR == (major) && GST_VERSION_MINOR == (minor) && \ + GST_VERSION_MICRO >= (micro))) +#endif + +#if !GST_CHECK_VERSION(0,10,9) +#define GST_BUFFER_IS_DISCONT(buffer) (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) +#endif + +#if GST_CHECK_VERSION(0,10,6) +#define HAVE_NEWSEG_FULL +#else +static GstBuffer * +gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes) +{ + GstBuffer *buf = NULL; + + if (G_UNLIKELY (nbytes > adapter->size)) + return NULL; + + buf = gst_buffer_new_and_alloc (nbytes); + + if (G_UNLIKELY (!buf)) + return NULL; + + /* Slow... */ + memcpy (GST_BUFFER_DATA (buf), gst_adapter_peek (adapter, nbytes), nbytes); + + return buf; +} +#endif + +/* elementfactory information */ +static GstElementDetails flups_demux_details = { + "MPEG Program Demuxer", + "Codec/Demuxer", + "Demultiplexes MPEG Program Streams", + "Wim Taymans <wim@fluendo.com>" +}; + +#define GST_TYPE_FLUPS_DEMUX_SYNC (gst_flups_demux_sync_get_type ()) +GType +gst_flups_demux_sync_get_type (void) +{ + static GType sync_type = 0; + static GEnumValue sync_types[] = { + {GST_FLUPS_DEMUX_SYNC_AUTO, "Auto-select method to synchronize streams", + "auto"}, + {GST_FLUPS_DEMUX_SYNC_SCR, "Use SCR to synchronize streams", "scr"}, + {GST_FLUPS_DEMUX_SYNC_DTS, "Use DTS to synchronize streams", "dts"}, + {0, NULL, NULL} + }; + + if (sync_type == 0) + sync_type = g_enum_register_static ("GstFluPSDemuxSync", sync_types); + + return sync_type; +} + +/* MPEG2Demux signals and args */ +enum +{ + /* FILL ME */ + LAST_SIGNAL +}; + +enum +{ + ARG_0, + ARG_SYNC, + /* FILL ME */ +}; + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/mpeg, " + "mpegversion = (int) { 1, 2 }, " + "systemstream = (boolean) TRUE;" "video/x-cdxa") + ); + +static GstStaticPadTemplate video_template = + GST_STATIC_PAD_TEMPLATE ("video_%02x", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + GST_STATIC_CAPS ("video/mpeg, " + "mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE;" + "video/x-h264") + ); + +static GstStaticPadTemplate audio_template = + GST_STATIC_PAD_TEMPLATE ("audio_%02x", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + GST_STATIC_CAPS ("audio/mpeg, " + "mpegversion = (int) 1;" + "audio/x-private1-lpcm; " + "audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3") + ); + +static GstStaticPadTemplate private_template = +GST_STATIC_PAD_TEMPLATE ("private_%d", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + GST_STATIC_CAPS_ANY); + +static void gst_flups_demux_base_init (GstFluPSDemuxClass * klass); +static void gst_flups_demux_class_init (GstFluPSDemuxClass * klass); +static void gst_flups_demux_init (GstFluPSDemux * demux); +static void gst_flups_demux_finalize (GstFluPSDemux * demux); +static void gst_flups_demux_reset (GstFluPSDemux * demux); + +static gboolean gst_flups_demux_sink_event (GstPad * pad, GstEvent * event); +static gboolean gst_flups_demux_src_event (GstPad * pad, GstEvent * event); +static gboolean gst_flups_demux_src_query (GstPad * pad, GstQuery * query); +static GstFlowReturn gst_flups_demux_chain (GstPad * pad, GstBuffer * buffer); + +static GstStateChangeReturn gst_flups_demux_change_state (GstElement * element, + GstStateChange transition); + +static GstElementClass *parent_class = NULL; + +/*static guint gst_flups_demux_signals[LAST_SIGNAL] = { 0 };*/ + +GType +gst_flups_demux_get_type (void) +{ + static GType flups_demux_type = 0; + + if (!flups_demux_type) { + static const GTypeInfo flups_demux_info = { + sizeof (GstFluPSDemuxClass), + (GBaseInitFunc) gst_flups_demux_base_init, + NULL, + (GClassInitFunc) gst_flups_demux_class_init, + NULL, + NULL, + sizeof (GstFluPSDemux), + 0, + (GInstanceInitFunc) gst_flups_demux_init, + }; + + flups_demux_type = + g_type_register_static (GST_TYPE_ELEMENT, "GstFluPSDemux", + &flups_demux_info, 0); + + GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "flupsdemux", 0, + "MPEG program stream demultiplexer element"); + } + + return flups_demux_type; +} + +static void +gst_flups_demux_base_init (GstFluPSDemuxClass * klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + klass->sink_template = gst_static_pad_template_get (&sink_template); + klass->video_template = gst_static_pad_template_get (&video_template); + klass->audio_template = gst_static_pad_template_get (&audio_template); + klass->private_template = gst_static_pad_template_get (&private_template); + + gst_element_class_add_pad_template (element_class, klass->video_template); + gst_element_class_add_pad_template (element_class, klass->audio_template); + gst_element_class_add_pad_template (element_class, klass->private_template); + gst_element_class_add_pad_template (element_class, klass->sink_template); + + gst_element_class_set_details (element_class, &flups_demux_details); +} + +static void +gst_flups_demux_class_init (GstFluPSDemuxClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + + parent_class = g_type_class_ref (GST_TYPE_ELEMENT); + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + + gobject_class->finalize = (GObjectFinalizeFunc) gst_flups_demux_finalize; + + gstelement_class->change_state = gst_flups_demux_change_state; +} + +static void +gst_flups_demux_init (GstFluPSDemux * demux) +{ + GstFluPSDemuxClass *klass = GST_FLUPS_DEMUX_GET_CLASS (demux); + + demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink"); + gst_pad_set_event_function (demux->sinkpad, gst_flups_demux_sink_event); + gst_pad_set_chain_function (demux->sinkpad, gst_flups_demux_chain); + gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad); + + demux->streams = + g_malloc0 (sizeof (GstFluPSStream *) * (GST_FLUPS_DEMUX_MAX_STREAMS)); + +} + +static void +gst_flups_demux_finalize (GstFluPSDemux * demux) +{ + gst_flups_demux_reset (demux); + g_free (demux->streams); + + G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux)); +} + +static void +gst_flups_demux_reset (GstFluPSDemux * demux) +{ + /* Clean up the streams and pads we allocated */ + gint i; + GstEvent **p_ev; + + for (i = 0; i < GST_FLUPS_DEMUX_MAX_STREAMS; i++) { + GstFluPSStream *stream = demux->streams[i]; + + if (stream != NULL) { + if (stream->pad) + gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad); + + g_free (stream); + demux->streams[i] = NULL; + } + } + p_ev = &demux->lang_codes; + + gst_event_replace (p_ev, NULL); +} + +static GstFluPSStream * +gst_flups_demux_create_stream (GstFluPSDemux * demux, gint id, gint stream_type) +{ + GstFluPSStream *stream; + GstPadTemplate *template; + gchar *name; + GstFluPSDemuxClass *klass = GST_FLUPS_DEMUX_GET_CLASS (demux); + GstCaps *caps; + + name = NULL; + template = NULL; + caps = NULL; + + GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id, + stream_type); + + switch (stream_type) { + case ST_VIDEO_MPEG1: + case ST_VIDEO_MPEG2: + case ST_VIDEO_MPEG4: + case ST_GST_VIDEO_MPEG1_OR_2: + { + gint mpeg_version = 1; + if (stream_type == ST_VIDEO_MPEG2 || + (stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) { + mpeg_version = 2; + } + if (stream_type == ST_VIDEO_MPEG4) { + mpeg_version = 4; + } + + template = klass->video_template; + name = g_strdup_printf ("video_%02x", id); + caps = gst_caps_new_simple ("video/mpeg", + "mpegversion", G_TYPE_INT, mpeg_version, + "systemstream", G_TYPE_BOOLEAN, FALSE, NULL); + break; + } + case ST_AUDIO_MPEG1: + case ST_AUDIO_MPEG2: + template = klass->audio_template; + name = g_strdup_printf ("audio_%02x", id); + caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, 1, NULL); + break; + case ST_PRIVATE_SECTIONS: + case ST_PRIVATE_DATA: + case ST_MHEG: + case ST_DSMCC: + case ST_AUDIO_AAC: + break; + case ST_VIDEO_H264: + template = klass->video_template; + name = g_strdup_printf ("video_%02x", id); + caps = gst_caps_new_simple ("video/x-h264", NULL); + break; + case ST_PS_AUDIO_AC3: + template = klass->audio_template; + name = g_strdup_printf ("audio_%02x", id); + caps = gst_caps_new_simple ("audio/x-private1-ac3", NULL); + break; + case ST_PS_AUDIO_DTS: + template = klass->audio_template; + name = g_strdup_printf ("audio_%02x", id); + caps = gst_caps_new_simple ("audio/x-private1-dts", NULL); + break; + case ST_PS_AUDIO_LPCM: + template = klass->audio_template; + name = g_strdup_printf ("audio_%02x", id); + caps = gst_caps_new_simple ("audio/x-private1-lpcm", NULL); + break; + case ST_PS_DVD_SUBPICTURE: + break; + case ST_GST_AUDIO_RAWA52: + template = klass->audio_template; + name = g_strdup_printf ("audio_%02x", id); + caps = gst_caps_new_simple ("audio/ac3", NULL); + break; + default: + break; + } + + if (name == NULL || template == NULL || caps == NULL) + return NULL; + + stream = g_new0 (GstFluPSStream, 1); + stream->id = id; + stream->discont = TRUE; + stream->need_segment = TRUE; + stream->notlinked = FALSE; + stream->type = stream_type; + stream->pad = gst_pad_new_from_template (template, name); + gst_pad_set_event_function (stream->pad, gst_flups_demux_src_event); + gst_pad_set_query_function (stream->pad, gst_flups_demux_src_query); + gst_pad_use_fixed_caps (stream->pad); + gst_pad_set_caps (stream->pad, caps); + gst_caps_unref (caps); + GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps); + g_free (name); + + + return stream; +} + +static GstFluPSStream * +gst_flups_demux_get_stream (GstFluPSDemux * demux, gint id, gint type) +{ + GstFluPSStream *stream = demux->streams[id]; + + if (stream == NULL) { + if (!(stream = gst_flups_demux_create_stream (demux, id, type))) + goto unknown_stream; + + GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id, + type); + + gst_pad_set_active (stream->pad, TRUE); + gst_element_add_pad (GST_ELEMENT (demux), stream->pad); + + demux->streams[id] = stream; + } + return stream; + + /* ERROR */ +unknown_stream: + { + GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type); + return NULL; + } +} + +static GstFlowReturn +gst_flups_demux_send_data (GstFluPSDemux * demux, GstFluPSStream * stream, + GstBuffer * buf) +{ + GstFlowReturn result; + guint64 timestamp; + guint size; + + if (stream == NULL) + goto no_stream; + + /* discont */ + if (stream->need_segment) { + gint64 time, start, stop; + GstEvent *newsegment; + + start = demux->base_time + demux->src_segment.start; + if (demux->src_segment.stop != -1) + stop = demux->base_time + demux->src_segment.stop; + else + stop = -1; + time = start; + +#ifdef HAVE_NEWSEG_FULL + GST_INFO_OBJECT (demux, "sending new segment: rate %g applied_rate %g " + "start: %" GST_TIME_FORMAT ", stop: %" GST_TIME_FORMAT + ", time: %" GST_TIME_FORMAT " to pad %" GST_PTR_FORMAT, + demux->sink_segment.rate, demux->sink_segment.applied_rate, + GST_TIME_ARGS (start), GST_TIME_ARGS (stop), + GST_TIME_ARGS (time), stream->pad); + + newsegment = gst_event_new_new_segment_full (FALSE, + demux->sink_segment.rate, demux->sink_segment.applied_rate, + GST_FORMAT_TIME, start, stop, time); +#else + GST_INFO_OBJECT (demux, "sending new segment: rate %g " + "start: %" GST_TIME_FORMAT ", stop: %" GST_TIME_FORMAT + ", time: %" GST_TIME_FORMAT " to pad %" GST_PTR_FORMAT, + demux->sink_segment.rate, GST_TIME_ARGS (start), + GST_TIME_ARGS (stop), GST_TIME_ARGS (time), stream->pad); + + newsegment = gst_event_new_new_segment (FALSE, + demux->sink_segment.rate, GST_FORMAT_TIME, start, stop, time); +#endif + + gst_pad_push_event (stream->pad, newsegment); + + stream->need_segment = FALSE; + } + + /* timestamps */ + if (demux->next_pts != G_MAXUINT64) + timestamp = MPEGTIME_TO_GSTTIME (demux->next_pts); + else + timestamp = GST_CLOCK_TIME_NONE; + + /* OK, sent new segment now prepare the buffer for sending */ + /* caps */ + gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad)); + GST_BUFFER_TIMESTAMP (buf) = timestamp; + + /* Set the buffer discont flag, and clear discont state on the stream */ + if (stream->discont) { + GST_DEBUG_OBJECT (demux, "marking discont buffer"); + GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT); + + stream->discont = FALSE; + } + size = GST_BUFFER_SIZE (buf); + + demux->next_pts = G_MAXUINT64; + demux->next_dts = G_MAXUINT64; + + result = gst_pad_push (stream->pad, buf); + GST_DEBUG_OBJECT (demux, "pushed stream id 0x%02x type 0x%02x, time: %" + GST_TIME_FORMAT ", size %d. result: %s", + stream->id, stream->type, GST_TIME_ARGS (timestamp), + size, gst_flow_get_name (result)); + + return result; + + /* ERROR */ +no_stream: + { + GST_DEBUG_OBJECT (demux, "no stream given"); + gst_buffer_unref (buf); + return GST_FLOW_OK; + } +} + +static void +gst_flups_demux_mark_discont (GstFluPSDemux * demux, gboolean discont, + gboolean need_segment) +{ + gint id; + + /* mark discont on all streams */ + for (id = 0; id < GST_FLUPS_DEMUX_MAX_STREAMS; id++) { + GstFluPSStream *stream = demux->streams[id]; + + if (stream) { + stream->discont |= discont; + stream->need_segment |= need_segment; + GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d", + stream->discont, stream->need_segment); + } + } +} + +static gboolean +gst_flups_demux_send_event (GstFluPSDemux * demux, GstEvent * event) +{ + gint id; + gboolean ret = FALSE; + + for (id = 0; id < GST_FLUPS_DEMUX_MAX_STREAMS; id++) { + GstFluPSStream *stream = demux->streams[id]; + + if (stream && !stream->notlinked) { + (void) gst_event_ref (event); + + if (!gst_pad_push_event (stream->pad, event)) { + GST_DEBUG_OBJECT (stream, "event %s was not handled correctly", + GST_EVENT_TYPE_NAME (event)); + } else { + /* If at least one push returns TRUE, then we return TRUE. */ + GST_DEBUG_OBJECT (stream, "event %s was handled correctly", + GST_EVENT_TYPE_NAME (event)); + ret = TRUE; + } + } + } + + gst_event_unref (event); + return ret; +} + +static gboolean +gst_flups_demux_handle_dvd_event (GstFluPSDemux * demux, GstEvent * event) +{ + const GstStructure *structure = gst_event_get_structure (event); + const char *type = gst_structure_get_string (structure, "event"); + gint i; + gchar cur_stream_name[32]; + GstFluPSStream *temp; + + if (strcmp (type, "dvd-lang-codes") == 0) { + GstEvent **p_ev; + /* Store the language codes event on the element, then iterate over the + * streams it specifies and retrieve them. The stream creation code then + * creates the pad appropriately and sends tag events as needed */ + p_ev = &demux->lang_codes, event; + gst_event_replace (p_ev, event); + + GST_DEBUG_OBJECT (demux, "Handling language codes event"); + + /* Create a video pad to ensure have it before emit no more pads */ + temp = gst_flups_demux_get_stream (demux, 0xe0, ST_VIDEO_MPEG2); + + /* Read out the languages for audio streams and request each one that + * is present */ + for (i = 0; i < MAX_DVD_AUDIO_STREAMS; i++) { + gint stream_format; + gint stream_id; + + g_snprintf (cur_stream_name, 32, "audio-%d-format", i); + if (!gst_structure_get_int (structure, cur_stream_name, &stream_format)) + break; + + switch (stream_format) { + case 0x0: + /* AC3 */ + stream_id = 0x80 + i; + temp = gst_flups_demux_get_stream (demux, stream_id, ST_PS_AUDIO_AC3); + break; + case 0x2: + case 0x3: + /* MPEG audio without and with extension stream are + * treated the same */ + stream_id = 0xC0 + i; + temp = gst_flups_demux_get_stream (demux, stream_id, ST_AUDIO_MPEG1); + break; + case 0x4: + /* LPCM */ + stream_id = 0xA0 + i; + temp = + gst_flups_demux_get_stream (demux, stream_id, ST_PS_AUDIO_LPCM); + break; + case 0x6: + /* DTS */ + stream_id = 0x88 + i; + temp = gst_flups_demux_get_stream (demux, stream_id, ST_PS_AUDIO_DTS); + break; + case 0x7: + /* FIXME: What range is SDDS? */ + break; + default: + GST_WARNING_OBJECT (demux, + "Unknown audio stream format in language code event: %d", + stream_format); + break; + } + } + + /* And subtitle streams */ + for (i = 0; i < MAX_DVD_SUBPICTURE_STREAMS; i++) { + g_snprintf (cur_stream_name, 32, "subpicture-%d-format", i); + + if (gst_structure_get_string (structure, cur_stream_name) == NULL) + break; + + /* Retrieve the subpicture stream to force pad creation */ + temp = gst_flups_demux_get_stream (demux, 0x20 + i, ST_PS_DVD_SUBPICTURE); + } + + GST_DEBUG_OBJECT (demux, "Created all pads from Language Codes event, " + "signalling no-more-pads"); + + gst_element_no_more_pads (GST_ELEMENT (demux)); + demux->need_no_more_pads = FALSE; + } + + gst_event_unref (event); + return TRUE; +} + +static gboolean +gst_flups_demux_sink_event (GstPad * pad, GstEvent * event) +{ + gboolean res = TRUE; + GstFluPSDemux *demux; + + demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad)); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_FLUSH_START: + gst_flups_demux_send_event (demux, event); + break; + case GST_EVENT_FLUSH_STOP: + gst_flups_demux_send_event (demux, event); + + gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED); + gst_adapter_clear (demux->adapter); + gst_adapter_clear (demux->rev_adapter); + demux->adapter_offset = G_MAXUINT64; + gst_pes_filter_drain (&demux->filter); + demux->current_scr = G_MAXUINT64; + demux->bytes_since_scr = 0; + break; + case GST_EVENT_NEWSEGMENT: + { + gboolean update; + gdouble rate; + GstFormat format; + gint64 start, stop, time; + +#ifdef HAVE_NEWSEG_FULL + { + gdouble arate; + + gst_event_parse_new_segment_full (event, &update, &rate, &arate, + &format, &start, &stop, &time); + gst_segment_set_newsegment_full (&demux->sink_segment, update, rate, + arate, format, start, stop, time); + + if (format == GST_FORMAT_BYTES && demux->scr_rate_n != G_MAXUINT64 + && demux->scr_rate_d != G_MAXUINT64) { + + gst_segment_set_newsegment_full (&demux->src_segment, update, rate, + arate, GST_FORMAT_TIME, BYTES_TO_GSTTIME (start), + BYTES_TO_GSTTIME (stop), BYTES_TO_GSTTIME (time)); + } + + } +#else + gst_event_parse_new_segment (event, &update, &rate, &format, + &start, &stop, &time); + gst_segment_set_newsegment (&demux->sink_segment, update, rate, + format, start, stop, time); + if (format == GST_FORMAT_BYTES && demux->scr_rate_n != G_MAXUINT64 + && demux->scr_rate_d != G_MAXUINT64) { + + gst_segment_set_newsegment (&demux->src_segment, update, rate, + GST_FORMAT_TIME, BYTES_TO_GSTTIME (start), BYTES_TO_GSTTIME (stop), + BYTES_TO_GSTTIME (time)); + } +#endif + + GST_INFO_OBJECT (demux, "received new segment: rate %g " + "format %d, start: %" G_GINT64_FORMAT ", stop: %" G_GINT64_FORMAT + ", time: %" G_GINT64_FORMAT, rate, format, start, stop, time); + + /* we need to emit a new segment */ + gst_flups_demux_mark_discont (demux, TRUE, TRUE); + gst_event_unref (event); + + break; + } + case GST_EVENT_EOS: + GST_INFO_OBJECT (demux, "Received EOS"); + if (!gst_flups_demux_send_event (demux, event)) { + GST_WARNING_OBJECT (demux, "failed pushing EOS on streams"); + GST_ELEMENT_ERROR (demux, STREAM, FAILED, + ("Internal data stream error."), ("Can't push EOS downstream")); + } + break; + case GST_EVENT_CUSTOM_DOWNSTREAM: + case GST_EVENT_CUSTOM_DOWNSTREAM_OOB: + { + const GstStructure *structure = gst_event_get_structure (event); + + if (structure != NULL + && gst_structure_has_name (structure, "application/x-gst-dvd")) { + res = gst_flups_demux_handle_dvd_event (demux, event); + } else { + gst_flups_demux_send_event (demux, event); + } + break; + } + default: + gst_flups_demux_send_event (demux, event); + break; + } + + gst_object_unref (demux); + + return res; +} + +static gboolean +gst_flups_demux_src_event (GstPad * pad, GstEvent * event) +{ + gboolean res = FALSE; + GstFluPSDemux *demux; + + demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad)); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_SEEK: + { + gdouble rate; + GstFormat format; + GstSeekFlags flags; + GstSeekType start_type, stop_type; + gint64 start, stop; + gint64 bstart, bstop; + GstEvent *bevent; + + gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start, + &stop_type, &stop); + + GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT + " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), + GST_TIME_ARGS (stop)); + + if (format == GST_FORMAT_BYTES) { + GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format); + goto not_supported; + } + + GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first"); + + /* first try original format seek */ + (void) gst_event_ref (event); + if ((res = gst_pad_push_event (demux->sinkpad, event))) + goto done; + + if (format != GST_FORMAT_TIME) { + /* From here down, we only support time based seeks */ + GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format); + goto not_supported; + } + + /* We need to convert to byte based seek and we need a scr_rate for that. */ + if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64) { + GST_DEBUG_OBJECT (demux, "seek not possible, no scr_rate"); + goto not_supported; + } + + GST_DEBUG_OBJECT (demux, "try with scr_rate interpolation"); + + bstart = GSTTIME_TO_BYTES (start); + bstop = GSTTIME_TO_BYTES (stop); + + GST_DEBUG_OBJECT (demux, "in bytes bstart %" G_GINT64_FORMAT " bstop %" + G_GINT64_FORMAT, bstart, bstop); + bevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type, + bstart, stop_type, bstop); + + res = gst_pad_push_event (demux->sinkpad, bevent); + + done: + gst_event_unref (event); + break; + } + default: + res = gst_pad_push_event (demux->sinkpad, event); + break; + } + + gst_object_unref (demux); + + return res; + +not_supported: + { + gst_object_unref (demux); + gst_event_unref (event); + + return FALSE; + } +} + +static gboolean +gst_flups_demux_src_query (GstPad * pad, GstQuery * query) +{ + gboolean res = FALSE; + GstFluPSDemux *demux; + + demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad)); + + GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT, + GST_QUERY_TYPE (query), pad); + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_POSITION: + { + GstFormat format; + gint64 position; + + gst_query_parse_position (query, &format, NULL); + + if (format != GST_FORMAT_TIME) { + GST_DEBUG_OBJECT (demux, "position not supported for format %d", + format); + goto not_supported; + } + if (demux->current_scr == G_MAXUINT64 || demux->first_scr == G_MAXUINT64) { + GST_DEBUG_OBJECT (demux, "position not possible, no current_scr"); + goto not_supported; + } + + position = demux->base_time + + MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr); + + GST_LOG_OBJECT (demux, "Position at GStreamer Time:%" GST_TIME_FORMAT, + GST_TIME_ARGS (position)); + + gst_query_set_position (query, format, position); + res = TRUE; + break; + } + case GST_QUERY_DURATION: + { + GstFormat format; + gint64 duration; + GstPad *peer; + + gst_query_parse_duration (query, &format, NULL); + + if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL) { + GST_DEBUG_OBJECT (demux, "duration not possible, no peer"); + goto not_supported; + } + + /* For any format other than bytes, see if upstream knows first */ + if (format == GST_FORMAT_BYTES) { + GST_DEBUG_OBJECT (demux, "duration not supported for format %d", + format); + gst_object_unref (peer); + goto not_supported; + } + + if (gst_pad_query (peer, query)) { + gst_object_unref (peer); + res = TRUE; + break; + } + + /* Upstream didn't know, so we can only answer TIME queries from + * here on */ + if (format != GST_FORMAT_TIME) { + GST_DEBUG_OBJECT (demux, "duration not supported for format %d", + format); + gst_object_unref (peer); + goto not_supported; + } + + if (demux->mux_rate == -1) { + GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate"); + gst_object_unref (peer); + goto not_supported; + } + + gst_query_set_duration (query, GST_FORMAT_BYTES, -1); + + if (!gst_pad_query (peer, query)) { + GST_LOG_OBJECT (demux, "query on peer pad failed"); + gst_object_unref (peer); + goto not_supported; + } + gst_object_unref (peer); + + gst_query_parse_duration (query, &format, &duration); + + duration = BYTES_TO_GSTTIME (duration); + + gst_query_set_duration (query, GST_FORMAT_TIME, duration); + res = TRUE; + break; + } + default: + res = gst_pad_query_default (pad, query); + break; + } + + gst_object_unref (demux); + + return res; + +not_supported: + { + gst_object_unref (demux); + + return FALSE; + } +} + +static void +gst_flups_demux_reset_psm (GstFluPSDemux * demux) +{ + gint i; + +#define FILL_TYPE(start, stop, type) \ + for (i=start; i <= stop; i++) \ + demux->psm[i] = type; + + FILL_TYPE (0x00, 0x1f, -1); + FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE); + FILL_TYPE (0x40, 0x7f, -1); + FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3); + FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS); + FILL_TYPE (0xa0, 0xbf, ST_PS_AUDIO_LPCM); + FILL_TYPE (0xbd, 0xbd, -1); + FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1); + FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2); + FILL_TYPE (0xf0, 0xff, -1); + +#undef FILL_TYPE +} + +static GstFlowReturn +gst_flups_demux_parse_pack_start (GstFluPSDemux * demux) +{ + const guint8 *data; + guint length; + guint32 scr1, scr2; + guint64 scr, scr_adjusted, new_rate; + + GST_DEBUG ("parsing pack start"); + + /* fixed length to begin with, start code and two scr values */ + length = 8 + 4; + + if (!(data = gst_adapter_peek (demux->adapter, length))) + goto need_more_data; + + /* skip start code */ + data += 4; + + scr1 = GUINT32_FROM_BE (*(guint32 *) data); + scr2 = GUINT32_FROM_BE (*(guint32 *) (data + 4)); + + /* start parsing the stream */ + if ((*data & 0xc0) == 0x40) { + guint32 scr_ext; + guint32 next32; + guint8 stuffing_bytes; + + GST_DEBUG ("Found MPEG2 stream"); + demux->is_mpeg2_pack = TRUE; + + /* mpeg2 has more data */ + length += 2; + if (gst_adapter_available (demux->adapter) < length) + goto need_more_data; + + /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */ + + /* check markers */ + if ((scr1 & 0xc4000400) != 0x44000400) + goto lost_sync; + + scr = ((guint64) scr1 & 0x38000000) << 3; + scr |= ((guint64) scr1 & 0x03fff800) << 4; + scr |= ((guint64) scr1 & 0x000003ff) << 5; + scr |= ((guint64) scr2 & 0xf8000000) >> 27; + + /* marker:1==1 ! scr_ext:9 ! marker:1==1 */ + if ((scr2 & 0x04010000) != 0x04010000) + goto lost_sync; + + scr_ext = (scr2 & 0x03fe0000) >> 17; + /* We keep the offset of this scr */ + demux->last_scr_offset = demux->adapter_offset + 12; + + GST_DEBUG_OBJECT (demux, "SCR: 0x%08x SCRE: 0x%08x", scr, scr_ext); + + if (scr_ext) { + scr = (scr * 300 + scr_ext % 300) / 300; + } + /* SCR has been converted into units of 90Khz ticks to make it comparable + to DTS/PTS, that also implies 1 tick rounding error */ + data += 6; + /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */ + next32 = (GUINT32_FROM_BE ((*(guint32 *) data))); + if ((next32 & 0x00000300) != 0x00000300) + goto lost_sync; + + new_rate = (next32 & 0xfffffc00) >> 10; + + stuffing_bytes = (next32 & 0x07); + GST_DEBUG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes); + + data += 4; + while (stuffing_bytes--) { + if (*data++ != 0xff) + goto lost_sync; + } + } else { + GST_DEBUG ("Found MPEG1 stream"); + demux->is_mpeg2_pack = FALSE; + + /* check markers */ + if ((scr1 & 0xf1000100) != 0x21000100) + goto lost_sync; + + if ((scr2 & 0x01800001) != 0x01800001) + goto lost_sync; + + /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */ + scr = ((guint64) scr1 & 0x0e000000) << 5; + scr |= ((guint64) scr1 & 0x00fffe00) << 6; + scr |= ((guint64) scr1 & 0x000000ff) << 7; + scr |= ((guint64) scr2 & 0xfe000000) >> 25; + + /* We keep the offset of this scr */ + demux->last_scr_offset = demux->adapter_offset + 8; + + /* marker:1==1 ! mux_rate:22 ! marker:1==1 */ + new_rate = (scr2 & 0x007ffffe) >> 1; + + data += 8; + } + new_rate *= MPEG_MUX_RATE_MULT; + + /* scr adjusted is the new scr found + the colected adjustment */ + scr_adjusted = scr + demux->scr_adjust; + + GST_LOG_OBJECT (demux, + "SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %" + G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT, + scr, scr_adjusted, new_rate, + GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr - demux->first_scr))); + + /* keep the first src in order to calculate delta time */ + if (demux->first_scr == G_MAXUINT64) { + demux->first_scr = scr; + demux->first_scr_offset = demux->last_scr_offset; + demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr); + /* at begin consider the new_rate as the scr rate, bytes/clock ticks */ + demux->scr_rate_n = new_rate; + demux->scr_rate_d = CLOCK_FREQ; + } else if (demux->first_scr_offset != demux->last_scr_offset) { + /* estimate byte rate related to the SCR */ + demux->scr_rate_n = demux->last_scr_offset - demux->first_scr_offset; + demux->scr_rate_d = scr - demux->first_scr; + } + + GST_DEBUG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %" + G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT + " at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT + "/%" G_GUINT64_FORMAT "(%f)", + ((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"), + scr, demux->last_scr_offset, + demux->first_scr, demux->first_scr_offset, + demux->scr_rate_n, demux->scr_rate_d, + (float) demux->scr_rate_n / demux->scr_rate_d); + + /* adjustment of the SCR */ + if (demux->current_scr != G_MAXUINT64) { + gint64 diff; + guint64 old_scr, old_mux_rate, bss, adjust; + + /* keep SCR of the previous packet */ + old_scr = demux->current_scr; + old_mux_rate = demux->mux_rate; + + /* Bytes since SCR is the amount we placed in the adapter since then + * (demux->bytes_since_scr) minus the amount remaining in the adapter, + * clamped to >= 0 */ + bss = MAX (0, (gint) (demux->bytes_since_scr - + gst_adapter_available (demux->adapter))); + + /* estimate the new SCR using the previous one according the notes + on point 2.5.2.2 of the ISO/IEC 13818-1 document */ + adjust = (bss * CLOCK_FREQ) / old_mux_rate; + if (demux->sink_segment.rate >= 0.0) + demux->next_scr = old_scr + adjust; + else + demux->next_scr = old_scr - adjust; + + GST_DEBUG_OBJECT (demux, + "bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT + ", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT, + bss, demux->next_scr, old_scr, scr_adjusted); + + /* calculate the absolute deference between the last scr and + the new one */ + if (old_scr > scr_adjusted) + diff = old_scr - scr_adjusted; + else + diff = scr_adjusted - old_scr; + + /* if the difference is more than 1 second we need to reconfigure + adjustment */ + if (diff > CLOCK_FREQ) { + demux->scr_adjust = demux->next_scr - scr; + GST_DEBUG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT + ", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust); + scr_adjusted = demux->next_scr; + } else { + demux->next_scr = scr_adjusted; + } + } + + /* update the current_scr and rate members */ + demux->mux_rate = new_rate; + demux->current_scr = scr_adjusted; + + /* Reset the bytes_since_scr value to count the data remaining in the + * adapter */ + demux->bytes_since_scr = gst_adapter_available (demux->adapter); + + gst_adapter_flush (demux->adapter, length); + ADAPTER_OFFSET_FLUSH (length); + return GST_FLOW_OK; + +lost_sync: + { + GST_DEBUG_OBJECT (demux, "lost sync"); + return GST_FLOW_LOST_SYNC; + } +need_more_data: + { + GST_DEBUG_OBJECT (demux, "need more data"); + return GST_FLOW_NEED_MORE_DATA; + } +} + +static GstFlowReturn +gst_flups_demux_parse_sys_head (GstFluPSDemux * demux) +{ + guint16 length; + const guint8 *data; + gboolean csps; + + /* start code + length */ + if (!(data = gst_adapter_peek (demux->adapter, 6))) + goto need_more_data; + + /* skip start code */ + data += 4; + + length = GST_READ_UINT16_BE (data); + GST_DEBUG_OBJECT (demux, "length %d", length); + + length += 6; + + if (!(data = gst_adapter_peek (demux->adapter, length))) + goto need_more_data; + + /* skip start code and length */ + data += 6; + + /* marker:1==1 ! rate_bound:22 | marker:1==1 */ + if ((*data & 0x80) != 0x80) + goto marker_expected; + + { + guint32 rate_bound; + + if ((data[2] & 0x01) != 0x01) + goto marker_expected; + + rate_bound = ((guint32) data[0] & 0x7f) << 15; + rate_bound |= ((guint32) data[1]) << 7; + rate_bound |= ((guint32) data[2] & 0xfe) >> 1; + rate_bound *= MPEG_MUX_RATE_MULT; + + GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound); + + data += 3; + } + + /* audio_bound:6==1 ! fixed:1 | constrained:1 */ + { + guint8 audio_bound; + gboolean fixed; + + /* max number of simultaneous audio streams active */ + audio_bound = (data[0] & 0xfc) >> 2; + /* fixed or variable bitrate */ + fixed = (data[0] & 0x02) == 0x02; + /* meeting constraints */ + csps = (data[0] & 0x01) == 0x01; + + GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d", + audio_bound, fixed, csps); + data += 1; + } + + /* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */ + { + gboolean audio_lock; + gboolean video_lock; + guint8 video_bound; + + audio_lock = (data[0] & 0x80) == 0x80; + video_lock = (data[0] & 0x40) == 0x40; + + if ((data[0] & 0x20) != 0x20) + goto marker_expected; + + /* max number of simultaneous video streams active */ + video_bound = (data[0] & 0x1f); + + GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d", + audio_lock, video_lock, video_bound); + data += 1; + } + + /* packet_rate_restriction:1 | reserved:7==0x7F */ + { + gboolean packet_rate_restriction; + + if ((data[0] & 0x7f) != 0x7f) + goto marker_expected; + + /* only valid if csps is set */ + if (csps) { + packet_rate_restriction = (data[0] & 0x80) == 0x80; + + GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d", + packet_rate_restriction); + } + } + data += 1; + + { + gint stream_count = (length - 12) / 3; + gint i; + + GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count); + + for (i = 0; i < stream_count; i++) { + guint8 stream_id; + gboolean STD_buffer_bound_scale; + guint16 STD_buffer_size_bound; + guint32 buf_byte_size_bound; + + stream_id = *data++; + if (!(stream_id & 0x80)) + goto sys_len_error; + + /* check marker bits */ + if ((*data & 0xC0) != 0xC0) + goto no_placeholder_bits; + + STD_buffer_bound_scale = *data & 0x20; + STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8; + STD_buffer_size_bound |= *data++; + + if (STD_buffer_bound_scale == 0) { + buf_byte_size_bound = STD_buffer_size_bound * 128; + } else { + buf_byte_size_bound = STD_buffer_size_bound * 1024; + } + + GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d", + STD_buffer_bound_scale); + GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes", + STD_buffer_size_bound, buf_byte_size_bound); + } + } + + gst_adapter_flush (demux->adapter, length); + ADAPTER_OFFSET_FLUSH (length); + return GST_FLOW_OK; + + /* ERRORS */ +marker_expected: + { + GST_DEBUG_OBJECT (demux, "expecting marker"); + return GST_FLOW_LOST_SYNC; + } +no_placeholder_bits: + { + GST_DEBUG_OBJECT (demux, "expecting placeholder bit values" + " '11' after stream id"); + return GST_FLOW_LOST_SYNC; + } +sys_len_error: + { + GST_DEBUG_OBJECT (demux, "error in system header length"); + return GST_FLOW_LOST_SYNC; + } +need_more_data: + { + GST_DEBUG_OBJECT (demux, "need more data"); + return GST_FLOW_NEED_MORE_DATA; + } +} + +static GstFlowReturn +gst_flups_demux_parse_psm (GstFluPSDemux * demux) +{ + guint16 length = 0, info_length = 0, es_map_length = 0; + guint8 psm_version = 0; + const guint8 *data, *es_map_base; + gboolean applicable; + + /* start code + length */ + if (!(data = gst_adapter_peek (demux->adapter, 6))) + goto need_more_data; + + /* skip start code */ + data += 4; + + length = GST_READ_UINT16_BE (data); + GST_DEBUG_OBJECT (demux, "length %u", length); + + if (G_UNLIKELY (length > 0x3FA)) + goto psm_len_error; + + length += 6; + + if (!(data = gst_adapter_peek (demux->adapter, length))) + goto need_more_data; + + /* skip start code and length */ + data += 6; + + /* Read PSM applicable bit together with version */ + psm_version = GST_READ_UINT8 (data); + applicable = (psm_version & 0x80) >> 7; + psm_version &= 0x1F; + GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version, + applicable); + + /* Jump over version and marker bit */ + data += 2; + + /* Read PS info length */ + info_length = GST_READ_UINT16_BE (data); + /* Cap it to PSM length - needed bytes for ES map length and CRC */ + info_length = MIN (length - 16, info_length); + GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length); + + /* Jump over that section */ + data += (2 + info_length); + + /* Read ES map length */ + es_map_length = GST_READ_UINT16_BE (data); + /* Cap it to PSM remaining length - CRC */ + es_map_length = MIN (length - (16 + info_length), es_map_length); + GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length); + + /* Jump over the size */ + data += 2; + + /* Now read the ES map */ + es_map_base = data; + while (es_map_base + 4 <= data + es_map_length) { + guint8 stream_type = 0, stream_id = 0; + guint16 stream_info_length = 0; + + stream_type = GST_READ_UINT8 (es_map_base); + es_map_base++; + stream_id = GST_READ_UINT8 (es_map_base); + es_map_base++; + stream_info_length = GST_READ_UINT16_BE (es_map_base); + es_map_base += 2; + /* Cap stream_info_length */ + stream_info_length = MIN (data + es_map_length - es_map_base, + stream_info_length); + + GST_DEBUG_OBJECT (demux, "Stream type %02X with id %02X and %u bytes info", + stream_type, stream_id, stream_info_length); + demux->psm[stream_id] = stream_type; + es_map_base += stream_info_length; + } + + gst_adapter_flush (demux->adapter, length); + ADAPTER_OFFSET_FLUSH (length); + return GST_FLOW_OK; + +psm_len_error: + { + GST_DEBUG_OBJECT (demux, "error in PSM length"); + return GST_FLOW_LOST_SYNC; + } +need_more_data: + { + GST_DEBUG_OBJECT (demux, "need more data"); + return GST_FLOW_NEED_MORE_DATA; + } +} + +static void +gst_flups_demux_resync_cb (GstPESFilter * filter, GstFluPSDemux * demux) +{ +} + +static GstFlowReturn +gst_flups_demux_data_cb (GstPESFilter * filter, gboolean first, + GstBuffer * buffer, GstFluPSDemux * demux) +{ + GstBuffer *out_buf; + GstFlowReturn ret = GST_FLOW_OK; + gint stream_type; + guint32 start_code; + guint8 id; + guint8 *data; + guint datalen; + guint offset = 0; + + data = GST_BUFFER_DATA (buffer); + datalen = GST_BUFFER_SIZE (buffer); + + start_code = filter->start_code; + id = filter->id; + + if (first) { + /* find the stream type */ + stream_type = demux->psm[id]; + if (stream_type == -1) { + /* no stream type, if PS1, get the new id */ + if (start_code == ID_PRIVATE_STREAM_1 && datalen >= 2) { + guint8 nframes; + + /* VDR writes A52 streams without any header bytes + * (see ftp://ftp.mplayerhq.hu/MPlayer/samples/MPEG-VOB/vdr-AC3) */ + if (datalen >= 4) { + guint hdr = GST_READ_UINT32_BE (data); + + if (G_UNLIKELY ((hdr & 0xffff0000) == AC3_SYNC_WORD)) { + id = 0x80; + stream_type = demux->psm[id] = ST_GST_AUDIO_RAWA52; + GST_DEBUG_OBJECT (demux, "Found VDR raw A52 stream"); + } + } + + if (G_LIKELY (stream_type == -1)) { + /* new id */ + id = data[offset++]; + /* Number of audio frames in this packet */ + nframes = data[offset++]; + + GST_DEBUG_OBJECT (demux, "private type 0x%02x, %d frames", id, + nframes); + + datalen -= 2; + + /* and remap */ + stream_type = demux->psm[id]; + } + } + if (stream_type == -1) + goto unknown_stream_type; + } + if (filter->pts != -1) { + demux->next_pts = filter->pts + demux->scr_adjust; + GST_DEBUG_OBJECT (demux, "PTS = %" G_GUINT64_FORMAT + "(%" G_GUINT64_FORMAT ")", filter->pts, demux->next_pts); + } else + demux->next_pts = G_MAXUINT64; + + if (filter->dts != -1) { + demux->next_dts = filter->dts + demux->scr_adjust; + } else { + demux->next_dts = demux->next_pts; + } + GST_DEBUG_OBJECT (demux, "DTS = orig %" G_GUINT64_FORMAT + " (%" G_GUINT64_FORMAT ")", filter->dts, demux->next_dts); + + demux->current_stream = gst_flups_demux_get_stream (demux, id, stream_type); + } + + if (demux->current_stream == NULL) { + GST_DEBUG_OBJECT (demux, "Dropping buffer for unknown stream id 0x%02x", + id); + goto done; + } + + /* After 2 seconds of bitstream emit no more pads */ + if (demux->need_no_more_pads + && (demux->current_scr - demux->first_scr) > 2 * CLOCK_FREQ) { + GST_DEBUG_OBJECT (demux, "no more pads, notifying"); + gst_element_no_more_pads (GST_ELEMENT_CAST (demux)); + demux->need_no_more_pads = FALSE; + } + + /* If the stream is not-linked, don't bother creating a sub-buffer + * to send to it, unless we're processing a discont (which resets + * the not-linked status and tries again */ + if (demux->current_stream->discont) { + GST_DEBUG_OBJECT (demux, "stream is discont"); + demux->current_stream->notlinked = FALSE; + } + + if (demux->current_stream->notlinked == FALSE) { + out_buf = gst_buffer_create_sub (buffer, offset, datalen); + + ret = gst_flups_demux_send_data (demux, demux->current_stream, out_buf); + if (ret == GST_FLOW_NOT_LINKED) { + demux->current_stream->notlinked = TRUE; + ret = GST_FLOW_OK; + } + } + +done: + gst_buffer_unref (buffer); + + return ret; + + /* ERRORS */ +unknown_stream_type: + { + GST_DEBUG_OBJECT (demux, "unknown stream type %02x", id); + ret = GST_FLOW_OK; + goto done; + } +} + +static gboolean +gst_flups_demux_resync (GstFluPSDemux * demux, gboolean save) +{ + const guint8 *data; + gint avail; + guint32 code; + gint offset; + gboolean found; + + avail = gst_adapter_available (demux->adapter); + if (avail < 4) + goto need_data; + + /* Common case, read 4 bytes an check it */ + data = gst_adapter_peek (demux->adapter, 4); + + /* read currect code */ + code = GST_READ_UINT32_BE (data); + + /* The common case is that the sync code is at 0 bytes offset */ + if (G_LIKELY ((code & 0xffffff00) == 0x100L)) { + GST_LOG_OBJECT (demux, "Found resync code %08x after 0 bytes", code); + demux->last_sync_code = code; + return TRUE; + } + + /* Otherwise, we are starting at byte 4 and we need to search + the sync code in all available data in the adapter */ + offset = 4; + if (offset >= avail) + goto need_data; /* Not enough data to find sync */ + + data = gst_adapter_peek (demux->adapter, avail); + + do { + code = (code << 8) | data[offset++]; + found = (code & 0xffffff00) == 0x100L; + } while (offset < avail && !found); + + if (!save || demux->sink_segment.rate >= 0.0) { + GST_LOG_OBJECT (demux, "flushing %d bytes", offset - 4); + /* forward playback, we can discard and flush the skipped bytes */ + gst_adapter_flush (demux->adapter, offset - 4); + ADAPTER_OFFSET_FLUSH (offset - 4); + } else { + if (found) { + GST_LOG_OBJECT (demux, "reverse saving %d bytes", offset - 4); + /* reverse playback, we keep the flushed bytes and we will append them to + * the next buffer in the chain function, which is the previous buffer in + * the stream. */ + gst_adapter_push (demux->rev_adapter, + gst_adapter_take_buffer (demux->adapter, offset - 4)); + } else { + GST_LOG_OBJECT (demux, "reverse saving %d bytes", avail); + /* nothing found, keep all bytes */ + gst_adapter_push (demux->rev_adapter, + gst_adapter_take_buffer (demux->adapter, avail)); + } + } + + if (found) { + GST_LOG_OBJECT (demux, "Found resync code %08x after %d bytes", + code, offset - 4); + demux->last_sync_code = code; + } else { + GST_LOG_OBJECT (demux, "No resync after skipping %d", offset); + } + + return found; + +need_data: + { + GST_LOG_OBJECT (demux, "we need more data for resync %d", avail); + return FALSE; + } +} + +static gboolean +gst_flups_demux_is_pes_sync (guint32 sync) +{ + return ((sync & 0xfc) == 0xbc) || + ((sync & 0xe0) == 0xc0) || ((sync & 0xf0) == 0xe0); +} + +static GstFlowReturn +gst_flups_demux_chain (GstPad * pad, GstBuffer * buffer) +{ + GstFluPSDemux *demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad)); + GstFlowReturn ret = GST_FLOW_OK; + guint32 avail; + gboolean save, discont; + + discont = GST_BUFFER_IS_DISCONT (buffer); + + if (discont) { + GST_LOG_OBJECT (demux, "Received buffer with discont flag and" + " offset %" G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer)); + + gst_pes_filter_drain (&demux->filter); + gst_flups_demux_mark_discont (demux, TRUE, FALSE); + + /* mark discont on all streams */ + if (demux->sink_segment.rate >= 0.0) { + demux->current_scr = G_MAXUINT64; + demux->bytes_since_scr = 0; + } + } else { + GST_LOG_OBJECT (demux, "Received buffer with offset %" G_GUINT64_FORMAT, + GST_BUFFER_OFFSET (buffer)); + } + + /* We keep the offset to interpolate SCR */ + demux->adapter_offset = GST_BUFFER_OFFSET (buffer); + + gst_adapter_push (demux->adapter, buffer); + demux->bytes_since_scr += GST_BUFFER_SIZE (buffer); + + avail = gst_adapter_available (demux->rev_adapter); + if (avail > 0) { + GST_LOG_OBJECT (demux, "appending %u saved bytes", avail); + /* if we have a previous reverse chunk, append this now */ + /* FIXME this code assumes we receive discont buffers all thei + * time */ + gst_adapter_push (demux->adapter, + gst_adapter_take_buffer (demux->rev_adapter, avail)); + } + + avail = gst_adapter_available (demux->adapter); + GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state); + + switch (demux->filter.state) { + case STATE_DATA_SKIP: + case STATE_DATA_PUSH: + ret = gst_pes_filter_process (&demux->filter); + break; + case STATE_HEADER_PARSE: + break; + default: + break; + } + + switch (ret) { + case GST_FLOW_NEED_MORE_DATA: + /* Go and get more data */ + ret = GST_FLOW_OK; + goto done; + case GST_FLOW_LOST_SYNC: + /* for FLOW_OK or lost-sync, carry onto resync */ + ret = GST_FLOW_OK; + break; + case GST_FLOW_OK: + break; + default: + /* Any other return value should be sent upstream immediately */ + goto done; + } + + /* align adapter data to sync boundary, we keep the data up to the next sync + * point. */ + save = TRUE; + while (gst_flups_demux_resync (demux, save)) { + gboolean ps_sync = TRUE; + + /* now switch on last synced byte */ + switch (demux->last_sync_code) { + case ID_PS_PACK_START_CODE: + ret = gst_flups_demux_parse_pack_start (demux); + break; + case ID_PS_SYSTEM_HEADER_START_CODE: + ret = gst_flups_demux_parse_sys_head (demux); + break; + case ID_PS_END_CODE: + ret = GST_FLOW_OK; + goto done; + case ID_PS_PROGRAM_STREAM_MAP: + ret = gst_flups_demux_parse_psm (demux); + break; + default: + if (gst_flups_demux_is_pes_sync (demux->last_sync_code)) { + ret = gst_pes_filter_process (&demux->filter); + } else { + GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found" + ", continuing", demux->last_sync_code); + ps_sync = FALSE; + ret = GST_FLOW_LOST_SYNC; + } + break; + } + /* if we found a ps sync, we stop saving the data, any non-ps sync gets + * saved up to the next ps sync. */ + if (ps_sync) + save = FALSE; + + switch (ret) { + case GST_FLOW_NEED_MORE_DATA: + GST_DEBUG_OBJECT (demux, "need more data"); + ret = GST_FLOW_OK; + goto done; + case GST_FLOW_LOST_SYNC: + if (!save || demux->sink_segment.rate >= 0.0) { + GST_DEBUG_OBJECT (demux, "flushing 3 bytes"); + gst_adapter_flush (demux->adapter, 3); + ADAPTER_OFFSET_FLUSH (3); + } else { + GST_DEBUG_OBJECT (demux, "saving 3 bytes"); + gst_adapter_push (demux->rev_adapter, + gst_adapter_take_buffer (demux->adapter, 3)); + } + ret = GST_FLOW_OK; + break; + default: + break; + } + } +done: + gst_object_unref (demux); + + return ret; +} + +static GstStateChangeReturn +gst_flups_demux_change_state (GstElement * element, GstStateChange transition) +{ + GstFluPSDemux *demux = GST_FLUPS_DEMUX (element); + GstStateChangeReturn result; + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + demux->adapter = gst_adapter_new (); + demux->rev_adapter = gst_adapter_new (); + demux->adapter_offset = G_MAXUINT64; + gst_pes_filter_init (&demux->filter, demux->adapter, + &demux->adapter_offset); + gst_pes_filter_set_callbacks (&demux->filter, + (GstPESFilterData) gst_flups_demux_data_cb, + (GstPESFilterResync) gst_flups_demux_resync_cb, demux); + demux->filter.gather_pes = TRUE; + demux->first_scr = G_MAXUINT64; + demux->current_scr = G_MAXUINT64; + demux->base_time = G_MAXUINT64; + demux->scr_rate_n = G_MAXUINT64; + demux->scr_rate_d = G_MAXUINT64; + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + demux->current_scr = G_MAXUINT64; + demux->mux_rate = G_MAXUINT64; + demux->next_pts = G_MAXUINT64; + demux->next_dts = G_MAXUINT64; + demux->first_scr = G_MAXUINT64; + demux->base_time = G_MAXUINT64; + demux->scr_rate_n = G_MAXUINT64; + demux->scr_rate_d = G_MAXUINT64; + demux->need_no_more_pads = TRUE; + + gst_flups_demux_reset_psm (demux); + gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED); + gst_segment_init (&demux->src_segment, GST_FORMAT_TIME); + break; + default: + break; + } + + result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + + switch (transition) { + case GST_STATE_CHANGE_PAUSED_TO_READY: + gst_flups_demux_reset (demux); + break; + case GST_STATE_CHANGE_READY_TO_NULL: + gst_pes_filter_uninit (&demux->filter); + g_object_unref (demux->adapter); + demux->adapter = NULL; + g_object_unref (demux->rev_adapter); + demux->rev_adapter = NULL; + break; + default: + break; + } + + return result; +} + +gboolean +gst_flups_demux_plugin_init (GstPlugin * plugin) +{ + if (!gst_element_register (plugin, "flupsdemux", + GST_RANK_PRIMARY, GST_TYPE_FLUPS_DEMUX)) + return FALSE; + + return TRUE; +} diff --git a/gst/mpegdemux/gstmpegdemux.h b/gst/mpegdemux/gstmpegdemux.h new file mode 100644 index 00000000..3730bac4 --- /dev/null +++ b/gst/mpegdemux/gstmpegdemux.h @@ -0,0 +1,152 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifndef __GST_FLUPS_DEMUX_H__ +#define __GST_FLUPS_DEMUX_H__ + +#include <gst/gst.h> +#include <gst/base/gstadapter.h> + +#include "gstpesfilter.h" + +G_BEGIN_DECLS + +#define GST_TYPE_FLUPS_DEMUX (gst_flups_demux_get_type()) +#define GST_FLUPS_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FLUPS_DEMUX,GstFluPSDemux)) +#define GST_FLUPS_DEMUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FLUPS_DEMUX,GstFluPSDemuxClass)) +#define GST_FLUPS_DEMUX_GET_CLASS(klass) (G_TYPE_INSTANCE_GET_CLASS((klass),GST_TYPE_FLUPS_DEMUX,GstFluPSDemuxClass)) +#define GST_IS_FLUPS_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FLUPS_DEMUX)) +#define GST_IS_FLUPS_DEMUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FLUPS_DEMUX)) + +typedef struct _GstFluPSStream GstFluPSStream; +typedef struct _GstFluPSDemux GstFluPSDemux; +typedef struct _GstFluPSDemuxClass GstFluPSDemuxClass; + +#define GST_FLUPS_DEMUX_MAX_STREAMS 256 +#define GST_FLUPS_DEMUX_MAX_PSM 256 + +typedef enum { + GST_FLUPS_DEMUX_SYNC_AUTO = 0, + GST_FLUPS_DEMUX_SYNC_SCR = 1, + GST_FLUPS_DEMUX_SYNC_DTS = 2 +} GstFluPSDemuxSync; + +typedef enum { + STATE_FLUPS_DEMUX_NEED_SYNC, + STATE_FLUPS_DEMUX_SYNCED, + STATE_FLUPS_DEMUX_NEED_MORE_DATA, +} GstFluPSDemuxState; + +/* Information associated with a single FluPS stream. */ +struct _GstFluPSStream { + GstPad * pad; + + gint id; + gint type; + gint size_bound; + + gboolean discont; + gboolean notlinked; + gboolean need_segment; +}; + +struct _GstFluPSDemux { + GstElement parent; + + GstPad * sinkpad; + + GstAdapter * adapter; + GstAdapter * rev_adapter; + guint64 adapter_offset; + guint32 last_sync_code; + GstPESFilter filter; + + gint64 mux_rate; + guint64 first_scr; + guint64 first_dts; + guint64 base_time; + guint64 current_scr; + guint64 next_scr; + guint64 bytes_since_scr; + gint64 scr_adjust; + guint64 scr_rate_n; + guint64 scr_rate_d; + guint64 first_scr_offset; + guint64 last_scr_offset; + + gint16 psm[GST_FLUPS_DEMUX_MAX_PSM]; + + GstSegment sink_segment; + GstSegment src_segment; + + /* stream output */ + GstFluPSStream * current_stream; + guint64 next_pts; + guint64 next_dts; + GstFluPSStream ** streams; + gboolean need_no_more_pads; + + /* Indicates an MPEG-2 stream */ + gboolean is_mpeg2_pack; + + /* Language codes event is stored when a dvd-lang-codes + * custom event arrives from upstream */ + GstEvent * lang_codes; +}; + +struct _GstFluPSDemuxClass { + GstElementClass parent_class; + + GstPadTemplate *sink_template; + GstPadTemplate *video_template; + GstPadTemplate *audio_template; + GstPadTemplate *private_template; +}; + +GType gst_flups_demux_get_type (void); + +gboolean gst_flups_demux_plugin_init (GstPlugin *plugin); + +G_END_DECLS + +#endif /* __GST_FLUPS_DEMUX_H__ */ diff --git a/gst/mpegdemux/gstmpegdesc.c b/gst/mpegdemux/gstmpegdesc.c new file mode 100644 index 00000000..1bab07ba --- /dev/null +++ b/gst/mpegdemux/gstmpegdesc.c @@ -0,0 +1,178 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#include <string.h> + +#include <gst/gst.h> + +#include "gstmpegdesc.h" + +void +gst_mpeg_descriptor_free (GstMPEGDescriptor * desc) +{ + g_return_if_fail (desc != NULL); + + g_free (desc); +} + +static guint +gst_mpeg_descriptor_parse_1 (guint8 * data, guint size) +{ + guint8 tag; + guint8 length; + + /* need at least 2 bytes for tag and length */ + if (size < 2) + return 0; + + tag = *data++; + length = *data++; + size -= 2; + + GST_DEBUG ("tag: 0x%02x, length: %d", tag, length); + + if (length > size) + return 0; + + return length + 2;; +} + +GstMPEGDescriptor * +gst_mpeg_descriptor_parse (guint8 * data, guint size) +{ + guint8 *current; + guint consumed, total, n_desc; + GstMPEGDescriptor *result; + + g_return_val_if_fail (data != NULL, NULL); + + current = data; + total = 0; + n_desc = 0; + + do { + consumed = gst_mpeg_descriptor_parse_1 (current, size); + + if (consumed > 0) { + current += consumed; + total += consumed; + size -= consumed; + n_desc++; + } + } + while (consumed > 0); + + GST_DEBUG ("parsed %d descriptors", n_desc); + + if (total == 0) + return NULL; + + result = g_malloc (sizeof (GstMPEGDescriptor) + total); + result->n_desc = n_desc; + result->data_length = total; + result->data = ((guint8 *) result) + sizeof (GstMPEGDescriptor); + + memcpy (result->data, data, total); + + return result; +} + +guint +gst_mpeg_descriptor_n_desc (GstMPEGDescriptor * desc) +{ + g_return_val_if_fail (desc != NULL, 0); + + return desc->n_desc; +} + +guint8 * +gst_mpeg_descriptor_find (GstMPEGDescriptor * desc, gint tag) +{ + gint length; + guint8 *current; + guint size; + + g_return_val_if_fail (desc != NULL, NULL); + + current = desc->data; + length = desc->data_length; + + while (length > 0) { + if (DESC_TAG (current) == tag) + return current; + + size = DESC_LENGTH (current) + 2; + + current += size; + length -= size; + } + return NULL; +} + +guint8 * +gst_mpeg_descriptor_nth (GstMPEGDescriptor * desc, guint i) +{ + gint length; + guint8 *current; + guint size; + + g_return_val_if_fail (desc != NULL, NULL); + + if (i > desc->n_desc) + return NULL; + + current = desc->data; + length = desc->data_length; + + while (length > 0) { + if (i == 0) + return current; + + size = DESC_LENGTH (current) + 2; + + current += size; + length -= size; + i--; + } + return NULL; +} diff --git a/gst/mpegdemux/gstmpegdesc.h b/gst/mpegdemux/gstmpegdesc.h new file mode 100644 index 00000000..91a42217 --- /dev/null +++ b/gst/mpegdemux/gstmpegdesc.h @@ -0,0 +1,271 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifndef __GST_MPEG_DESC_H__ +#define __GST_MPEG_DESC_H__ + +#include <glib.h> +/* + * descriptor_tag TS PS Identification + * 0 n/a n/a Reserved + * 1 n/a n/a Reserved + * 2 X X video_stream_descriptor + * 3 X X audio_stream_descriptor + * 4 X X hierarchy_descriptor + * 5 X X registration_descriptor + * 6 X X data_stream_alignment_descriptor + * 7 X X target_background_grid_descriptor + * 8 X X video_window_descriptor + * 9 X X CA_descriptor + * 10 X X ISO_639_language_descriptor + * 11 X X system_clock_descriptor + * 12 X X multiplex_buffer_utilization_descriptor + * 13 X X copyright_descriptor + * 14 X maximum bitrate descriptor + * 15 X X private data indicator descriptor + * 16 X X smoothing buffer descriptor + * 17 X STD_descriptor + * 18 X X IBP descriptor + * 19-63 n/a n/a ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved + * 64-255 n/a n/a User Private + */ +#define DESC_VIDEO_STREAM 2 +#define DESC_AUDIO_STREAM 3 +#define DESC_HIERARCHY 4 +#define DESC_REGISTRATION 5 +#define DESC_DATA_STREAM_ALIGNMENT 6 +#define DESC_TARGET_BACKGROUND_GRID 7 +#define DESC_VIDEO_WINDOW 8 +#define DESC_CA 9 +#define DESC_ISO_639_LANGUAGE 10 +#define DESC_SYSTEM_CLOCK 11 +#define DESC_MULTIPLEX_BUFFER_UTILISATION 12 +#define DESC_COPYRIGHT 13 +#define DESC_MAXIMUM_BITRATE 14 +#define DESC_PRIVATE_DATA_INDICATOR 15 +#define DESC_SMOOTHING_BUFFER 16 +#define DESC_STD 17 +#define DESC_IBP 18 + +#define DESC_DIRAC_TC_PRIVATE 0xAC + +/* DVB tags */ +#define DESC_DVB_NETWORK_NAME 0x40 +#define DESC_DVB_SERVICE_LIST 0x41 +#define DESC_DVB_STUFFING 0x42 +#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM 0x43 +#define DESC_DVB_CABLE_DELIVERY_SYSTEM 0x44 +#define DESC_DVB_VBI_DATA 0x45 +#define DESC_DVB_VBI_TELETEXT 0x46 +#define DESC_DVB_BOUQUET_NAME 0x47 +#define DESC_DVB_SERVICE 0x48 +#define DESC_DVB_COUNTRY_AVAILABILITY 0x49 +#define DESC_DVB_LINKAGE 0x4A +#define DESC_DVB_NVOD_REFERENCE 0x4B +#define DESC_DVB_TIME_SHIFTED_SERVICE 0x4C +#define DESC_DVB_SHORT_EVENT 0x4D +#define DESC_DVB_EXTENDED_EVENT 0x4E +#define DESC_DVB_TIME_SHIFTED_EVENT 0x4F +#define DESC_DVB_COMPONENT 0x50 +#define DESC_DVB_MOSAIC 0x51 +#define DESC_DVB_STREAM_IDENTIFIER 0x52 +#define DESC_DVB_CA_IDENTIFIER 0x53 +#define DESC_DVB_CONTENT 0x54 +#define DESC_DVB_PARENTAL_RATING 0x55 +#define DESC_DVB_TELETEXT 0x56 +#define DESC_DVB_TELEPHONE 0x57 +#define DESC_DVB_LOCAL_TIME_OFFSET 0x58 +#define DESC_DVB_SUBTITLING 0x59 +#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM 0x5A +#define DESC_DVB_MULTILINGUAL_NETWORK_NAME 0x5B +#define DESC_DVB_MULTILINGUAL_BOUQUET_NAME 0x5C +#define DESC_DVB_MULTILINGUAL_SERVICE_NAME 0x5D +#define DESC_DVB_MULTILINGUAL_COMPONENT 0x5E +#define DESC_DVB_PRIVATE_DATA 0x5F +#define DESC_DVB_SERVICE_MOVE 0x60 +#define DESC_DVB_SHORT_SMOOTHING_BUFFER 0x61 +#define DESC_DVB_FREQUENCY_LIST 0x62 +#define DESC_DVB_PARTIAL_TRANSPORT_STREAM 0x63 +#define DESC_DVB_DATA_BROADCAST 0x64 +#define DESC_DVB_SCRAMBLING 0x65 +#define DESC_DVB_DATA_BROADCAST_ID 0x66 +#define DESC_DVB_TRANSPORT_STREAM 0x67 +#define DESC_DVB_DSNG 0x68 +#define DESC_DVB_PDC 0x69 +#define DESC_DVB_AC3 0x6A +#define DESC_DVB_ANCILLARY_DATA 0x6B +#define DESC_DVB_CELL_LIST 0x6C +#define DESC_DVB_CELL_FREQUENCY_LINK 0x6D +#define DESC_DVB_ANNOUNCEMENT_SUPPORT 0x6E +#define DESC_DVB_APPLICATION_SIGNALLING 0x6F +#define DESC_DVB_ADAPTATION_FIELD_DATA 0x70 +#define DESC_DVB_SERVICE_IDENTIFIER 0x71 +#define DESC_DVB_SERVICE_AVAILABILITY 0x72 +#define DESC_DVB_DEFAULT_AUTHORITY 0x73 +#define DESC_DVB_RELATED_CONTENT 0x74 +#define DESC_DVB_TVA_ID 0x75 +#define DESC_DVB_CONTENT_IDENTIFIER 0x76 +#define DESC_DVB_TIMESLICE_FEC_IDENTIFIER 0x77 +#define DESC_DVB_ECM_REPETITION_RATE 0x78 +#define DESC_DVB_S2_SATELLITE_DELIVERY_SYSTEM 0x79 +#define DESC_DVB_ENHANCED_AC3 0x7A +#define DESC_DVB_DTS 0x7B +#define DESC_DVB_AAC 0x7C +/* 0x7D and 0x7E are reserved for future use */ +#define DESC_DVB_EXTENSION 0x7F +/* 0x80 - 0xFE are user defined */ +/* 0xFF is forbidden */ + +/* common for all descriptors */ +#define DESC_TAG(desc) (desc[0]) +#define DESC_LENGTH(desc) (desc[1]) + +/* video_stream_descriptor */ +#define DESC_VIDEO_STREAM_multiple_framerate_flag(desc) (((desc)[2] & 0x80) == 0x80) +#define DESC_VIDEO_STREAM_frame_rate_code(desc) (((desc)[2] & 0x38) >> 3) +#define DESC_VIDEO_STREAM_MPEG_1_only_flag(desc) (((desc)[2] & 0x04) == 0x04) +#define DESC_VIDEO_STREAM_constrained_parameter_flag(desc) (((desc)[2] & 0x02) == 0x02) +#define DESC_VIDEO_STREAM_still_picture_flag(desc) (((desc)[2] & 0x01) == 0x01) +/* if (MPEG_1_only_flag == 1) */ +#define DESC_VIDEO_STREAM_profile_and_level_indication(desc) ((desc)[3]) +#define DESC_VIDEO_STREAM_chroma_format(desc) (((desc)[4] & 0xc0) >> 6) +#define DESC_VIDEO_STREAM_frame_rate_extension_flag(desc) (((desc)[4] & 0x20) == 0x20) + +/* audio_stream_descriptor */ +#define DESC_AUDIO_STREAM_free_format_flag(desc) (((desc)[2] & 0x80) == 0x80) +#define DESC_AUDIO_STREAM_ID(desc) (((desc)[2] & 0x40) == 0x40) +#define DESC_AUDIO_STREAM_layer(desc) (((desc)[2] & 0x30) >> 4) +#define DESC_AUDIO_STREAM_variable_rate_audio_indicator(desc) (((desc)[2] & 0x08) == 0x08) + +/* hierarchy_descriptor */ +#define DESC_HIERARCHY_hierarchy_type(desc) (((desc)[2] & 0x0f)) +#define DESC_HIERARCHY_hierarchy_layer_index(desc) (((desc)[3] & 0x3f)) +#define DESC_HIERARCHY_hierarchy_embedded_layer_index(desc) (((desc)[4] & 0x3f)) +#define DESC_HIERARCHY_hierarchy_channel(desc) (((desc)[5] & 0x3f)) + +/* registration_descriptor */ +#define DESC_REGISTRATION_format_identifier(desc) (GST_READ_UINT32_BE ((desc)+2)) +#define DESC_REGISTRATION_additional_ident_info_len(desc) ((desc)[1] - 4) +#define DESC_REGISTRATION_additional_ident_info(desc) (&(desc)[6]) + +/* data_stream_alignment_descriptor */ +#define DESC_DATA_STREAM_ALIGNMENT_alignment_type(desc) ((desc)[2]) + +/* target_background_grid_descriptor */ +#define DESC_TARGET_BACKGROUND_GRID_horizontal_size(desc) (GST_READ_UINT16_BE ((desc)+2) >> 2) +#define DESC_TARGET_BACKGROUND_GRID_vertical_size(desc) ((GST_READ_UINT32_BE ((desc)+2) & 0x0003fff0) >> 4) +#define DESC_TARGET_BACKGROUND_GRID_aspect_ratio_information(desc) ((desc)[5] & 0x0f) + +/* video_window_descriptor */ +#define DESC_VIDEO_WINDOW_horizontal_offset(desc) (GST_READ_UINT16_BE ((desc)+2) >> 2) +#define DESC_VIDEO_WINDOW_vertical_offset(desc) ((GST_READ_UINT32_BE ((desc)+2) & 0x0003fff0) >> 4) +#define DESC_VIDEO_WINDOW_window_priority(desc) ((desc)[5] & 0x0f) + +/* CA_descriptor */ +#define DESC_CA_system_ID(desc) (GST_READ_UINT16_BE ((desc)+2)) +#define DESC_CA_PID(desc) (GST_READ_UINT16_BE ((desc)+2) & 0x1fff) + +/* ISO_639_language_descriptor */ +#define DESC_ISO_639_LANGUAGE_codes_n(desc) ((desc[1]) >> 2) +#define DESC_ISO_639_LANGUAGE_language_code_nth(desc,i) (&(desc[2 + (4*i)])) +#define DESC_ISO_639_LANGUAGE_audio_type_nth(desc,i) ((desc)[5 + (4*i)]) + +/* system_clock_descriptor */ +#define DESC_SYSTEM_CLOCK_external_clock_reference_indicator(desc) (((desc)[2] & 0x80) == 0x80) +#define DESC_SYSTEM_CLOCK_clock_accuracy_integer(desc) ((desc)[2] & 0x3f) +#define DESC_SYSTEM_CLOCK_clock_accuracy_exponent(desc) (((desc)[3] & 0xe0) >> 5) + +/* multiplex_buffer_utilization_descriptor */ +#define DESC_MULTIPLEX_BUFFER_UTILISATION_bound_valid_flag(desc) (((desc)[2] & 0x80) == 0x80) +#define DESC_MULTIPLEX_BUFFER_UTILISATION_LTW_offset_lower_bound(desc) (GST_READ_UINT16_BE ((desc)+2) & 0x7fff) +#define DESC_MULTIPLEX_BUFFER_UTILISATION_LTW_offset_upper_bound(desc) (GST_READ_UINT16_BE ((desc)+4) & 0x7fff) + +/* copyright_descriptor */ +#define DESC_COPYRIGHT_copyright_identifier(desc) (GST_READ_UINT32_BE ((desc)+2)) +#define DESC_COPYRIGHT_additional_copyright_info_len(desc) ((desc)[1] - 4) +#define DESC_COPYRIGHT_additional_copyright_info(desc) (&(desc)[6]) + +/* maximum_bitrate_descriptor */ +#define DESC_MAXIMUM_BITRAT_maximum_bitrate(desc) (((((guint32)desc[2]) & 0x3f) << 16) | \ + GST_READ_UINT16_BE ((desc)+3)) + +/* private_data_indicator_descriptor */ +#define DESC_PRIVATE_DATA_INDICATOR_indicator(desc) (GST_READ_UINT32_BE(&desc[2])) + +/* smoothing_buffer_descriptor */ +#define DESC_SMOOTHING_BUFFER_sb_leak_rate(desc) (((((guint32)desc[2]) & 0x3f) << 16) | \ + GST_READ_UINT16_BE ((desc)+3)) +#define DESC_SMOOTHING_BUFFER_sb_size(desc) (((((guint32)desc[5]) & 0x3f) << 16) | \ + GST_READ_UINT16_BE ((desc)+6)) +/* STD_descriptor */ +#define DESC_STD_leak_valid_flag(desc) (((desc)[2] & 0x01) == 0x01) + +/* ibp_descriptor */ +#define DESC_IBP_closed_gop_flag(desc) (((desc)[2] & 0x80) == 0x80) +#define DESC_IBP_identical_gop_flag(desc) (((desc)[2] & 0x40) == 0x40) +#define DESC_IBP_max_gop_length(desc) (GST_READ_UINT16_BE ((desc)+6) & 0x3fff) + +/* time_code descriptor */ +#define DESC_TIMECODE_video_pid(desc) (GST_READ_UINT16_BE ((desc) + 2) & 0x1fff) + +/* Stream identifier descriptor */ +#define DESC_DVB_STREAM_IDENTIFIER_component_tag(desc) (desc[2]) + +/* DVB Network Name descriptor */ +#define DESC_DVB_NETWORK_NAME_length(desc) (GST_READ_UINT8((desc)+1)) +#define DESC_DVB_NETWORK_NAME_text(desc) (desc+2) + +typedef struct { + guint n_desc; + guint8 data_length; + guint8 *data; +} GstMPEGDescriptor; + +GstMPEGDescriptor* gst_mpeg_descriptor_parse (guint8 *data, guint size); +void gst_mpeg_descriptor_free (GstMPEGDescriptor *desc); + +guint gst_mpeg_descriptor_n_desc (GstMPEGDescriptor *desc); +guint8* gst_mpeg_descriptor_find (GstMPEGDescriptor *desc, gint tag); +guint8* gst_mpeg_descriptor_nth (GstMPEGDescriptor *desc, guint i); + +#endif /* __GST_MPEG_DESC_H__ */ diff --git a/gst/mpegdemux/gstmpegtsdemux.c b/gst/mpegdemux/gstmpegtsdemux.c new file mode 100644 index 00000000..cbd7cc3a --- /dev/null +++ b/gst/mpegdemux/gstmpegtsdemux.c @@ -0,0 +1,2630 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> +#include <stdlib.h> + +#ifdef USE_LIBOIL +#include <liboil/liboil.h> +#endif + +#include "gstmpegdefs.h" +#include "gstmpegtsdemux.h" +#include "flutspatinfo.h" +#include "flutspmtinfo.h" + +#ifndef GST_CHECK_VERSION +#define GST_CHECK_VERSION(major,minor,micro) \ + (GST_VERSION_MAJOR > (major) || \ + (GST_VERSION_MAJOR == (major) && GST_VERSION_MINOR > (minor)) || \ + (GST_VERSION_MAJOR == (major) && GST_VERSION_MINOR == (minor) && \ + GST_VERSION_MICRO >= (micro))) +#endif + +#ifndef GST_BUFFER_IS_DISCONT +#define GST_BUFFER_IS_DISCONT(buffer) \ + (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) +#endif + +GST_DEBUG_CATEGORY_STATIC (gstflutsdemux_debug); +#define GST_CAT_DEFAULT (gstflutsdemux_debug) + +/* elementfactory information */ +#ifdef USE_LIBOIL +#define LONGNAME "MPEG Transport stream demuxer (liboil build)" +#else +#define LONGNAME "MPEG Transport stream demuxer" +#endif + +#ifndef __always_inline +#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) +#define __always_inline inline __attribute__((always_inline)) +#else +#define __always_inline inline +#endif +#endif + +#ifndef DISABLE_INLINE +#define FORCE_INLINE __always_inline +#else +#define FORCE_INLINE +#endif + +static GstElementDetails fluts_demux_details = { + LONGNAME, + "Codec/Demuxer", + "Demultiplexes MPEG2 Transport Streams", + "Wim Taymans <wim@fluendo.com>" +}; + +/* MPEG2Demux signals and args */ +enum +{ + /* FILL ME */ + LAST_SIGNAL +}; + +#define DEFAULT_PROP_ES_PIDS "" +#define DEFAULT_PROP_CHECK_CRC TRUE +#define DEFAULT_PROP_PROGRAM_NUMBER -1 + +/* latency in mseconds */ +#define TS_LATENCY 700 + +enum +{ + PROP_0, + PROP_ES_PIDS, + PROP_CHECK_CRC, + PROP_PROGRAM_NUMBER, + PROP_PAT_INFO, + PROP_PMT_INFO, + PROP_M2TS +}; + +#define VIDEO_CAPS \ + GST_STATIC_CAPS (\ + "video/mpeg, " \ + "mpegversion = (int) { 1, 2, 4 }, " \ + "systemstream = (boolean) FALSE; " \ + "video/x-h264;" \ + "video/x-dirac" \ + ) + +#define AUDIO_CAPS \ + GST_STATIC_CAPS ( \ + "audio/mpeg, " \ + "mpegversion = (int) { 1, 4 };" \ + "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;" \ + "audio/x-dts" \ + ) + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/mpegts") + ); + +static GstStaticPadTemplate video_template = +GST_STATIC_PAD_TEMPLATE ("video_%04x", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + VIDEO_CAPS); + +static GstStaticPadTemplate audio_template = +GST_STATIC_PAD_TEMPLATE ("audio_%04x", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + AUDIO_CAPS); + +static GstStaticPadTemplate private_template = +GST_STATIC_PAD_TEMPLATE ("private_%04x", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + GST_STATIC_CAPS_ANY); + +static void gst_fluts_demux_base_init (GstFluTSDemuxClass * klass); +static void gst_fluts_demux_class_init (GstFluTSDemuxClass * klass); +static void gst_fluts_demux_init (GstFluTSDemux * demux); +static void gst_fluts_demux_finalize (GstFluTSDemux * demux); +static void gst_fluts_demux_reset (GstFluTSDemux * demux); + +//static void gst_fluts_demux_remove_pads (GstFluTSDemux * demux); +static void gst_fluts_demux_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_fluts_demux_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static gboolean gst_fluts_demux_is_PMT (GstFluTSDemux * demux, guint16 PID); + +static gboolean gst_fluts_demux_sink_event (GstPad * pad, GstEvent * event); +static GstFlowReturn gst_fluts_demux_chain (GstPad * pad, GstBuffer * buffer); +static gboolean gst_fluts_demux_sink_setcaps (GstPad * pad, GstCaps * caps); + +#ifdef HAVE_LATENCY +static GstClock *gst_fluts_demux_provide_clock (GstElement * element); +static gboolean gst_fluts_demux_src_pad_query (GstPad * pad, GstQuery * query); +#endif + +static GstStateChangeReturn gst_fluts_demux_change_state (GstElement * element, + GstStateChange transition); + +static FluTsPmtInfo *fluts_demux_build_pmt_info (GstFluTSDemux * demux, + guint16 pmt_pid); + +static GstElementClass *parent_class = NULL; + +/*static guint gst_fluts_demux_signals[LAST_SIGNAL] = { 0 };*/ + +GType +gst_fluts_demux_get_type (void) +{ + static GType fluts_demux_type = 0; + + if (G_UNLIKELY (!fluts_demux_type)) { + static const GTypeInfo fluts_demux_info = { + sizeof (GstFluTSDemuxClass), + (GBaseInitFunc) gst_fluts_demux_base_init, + NULL, + (GClassInitFunc) gst_fluts_demux_class_init, + NULL, + NULL, + sizeof (GstFluTSDemux), + 0, + (GInstanceInitFunc) gst_fluts_demux_init, + }; + + fluts_demux_type = + g_type_register_static (GST_TYPE_ELEMENT, "GstFluTSDemux", + &fluts_demux_info, 0); + + GST_DEBUG_CATEGORY_INIT (gstflutsdemux_debug, "flutsdemux", 0, + "MPEG program stream demultiplexer element"); + } + + return fluts_demux_type; +} + +static void +gst_fluts_demux_base_init (GstFluTSDemuxClass * klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + klass->sink_template = gst_static_pad_template_get (&sink_template); + klass->video_template = gst_static_pad_template_get (&video_template); + klass->audio_template = gst_static_pad_template_get (&audio_template); + klass->private_template = gst_static_pad_template_get (&private_template); + + gst_element_class_add_pad_template (element_class, klass->video_template); + gst_element_class_add_pad_template (element_class, klass->audio_template); + gst_element_class_add_pad_template (element_class, klass->private_template); + gst_element_class_add_pad_template (element_class, klass->sink_template); + + gst_element_class_set_details (element_class, &fluts_demux_details); +} + +static void +gst_fluts_demux_class_init (GstFluTSDemuxClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + + parent_class = g_type_class_peek_parent (klass); + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + + gobject_class->finalize = (GObjectFinalizeFunc) gst_fluts_demux_finalize; + gobject_class->set_property = gst_fluts_demux_set_property; + gobject_class->get_property = gst_fluts_demux_get_property; + + g_object_class_install_property (gobject_class, PROP_ES_PIDS, + g_param_spec_string ("es-pids", + "Colon separated list of PIDs containing Elementary Streams", + "PIDs to treat as Elementary Streams in the absence of a PMT, " + "eg 0x10:0x11:0x20", DEFAULT_PROP_ES_PIDS, G_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, PROP_CHECK_CRC, + g_param_spec_boolean ("check-crc", "Check CRC", + "Enable CRC checking", DEFAULT_PROP_CHECK_CRC, G_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, PROP_PROGRAM_NUMBER, + g_param_spec_int ("program-number", "Program Number", + "Program number to demux for (-1 to ignore)", -1, G_MAXINT, + DEFAULT_PROP_PROGRAM_NUMBER, 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", + FLUTS_TYPE_PAT_INFO, G_PARAM_READABLE), G_PARAM_READABLE)); + + g_object_class_install_property (gobject_class, PROP_PMT_INFO, + g_param_spec_object ("pmt-info", + "Information about the current program", + "GObject with properties containing information from the TS PMT " + "about the currently selected program and its streams", + FLUTS_TYPE_PMT_INFO, G_PARAM_READABLE)); + + g_object_class_install_property (gobject_class, PROP_M2TS, + g_param_spec_boolean ("m2ts_mode", "M2TS(192 bytes) Mode", + "Defines if the input is normal TS ie .ts(188 bytes)" + "or Blue-Ray Format ie .m2ts(192 bytes).", FALSE, G_PARAM_READWRITE)); + + gstelement_class->change_state = gst_fluts_demux_change_state; +#ifdef HAVE_LATENCY + gstelement_class->provide_clock = gst_fluts_demux_provide_clock; +#endif +} + +static void +gst_fluts_demux_init (GstFluTSDemux * demux) +{ + GstFluTSDemuxClass *klass = GST_FLUTS_DEMUX_GET_CLASS (demux); + + demux->streams = g_malloc0 (sizeof (GstFluTSStream *) * (FLUTS_MAX_PID + 1)); + demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink"); + gst_pad_set_chain_function (demux->sinkpad, gst_fluts_demux_chain); + gst_pad_set_event_function (demux->sinkpad, gst_fluts_demux_sink_event); + gst_pad_set_setcaps_function (demux->sinkpad, gst_fluts_demux_sink_setcaps); + gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad); + + demux->elementary_pids = NULL; + demux->nb_elementary_pids = 0; + demux->check_crc = DEFAULT_PROP_CHECK_CRC; + demux->program_number = DEFAULT_PROP_PROGRAM_NUMBER; + demux->packetsize = FLUTS_NORMAL_TS_PACKETSIZE; + demux->m2ts_mode = FALSE; + +#ifdef USE_LIBOIL + oil_init (); +#endif +} + +static void +gst_fluts_demux_finalize (GstFluTSDemux * demux) +{ + gst_fluts_demux_reset (demux); + g_free (demux->streams); + + G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux)); +} + +static void +gst_fluts_demux_reset (GstFluTSDemux * demux) +{ + /* Clean up the streams and pads we allocated */ + gint i; + + for (i = 0; i < FLUTS_MAX_PID + 1; i++) { + GstFluTSStream *stream = demux->streams[i]; + + if (stream != NULL) { + if (stream->pad) + gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad); + if (stream->ES_info) + gst_mpeg_descriptor_free (stream->ES_info); + + if (stream->PMT.entries) + g_array_free (stream->PMT.entries, TRUE); + if (stream->PMT.program_info) + gst_mpeg_descriptor_free (stream->PMT.program_info); + + if (stream->PAT.entries) + g_array_free (stream->PAT.entries, TRUE); + + switch (stream->PID_type) { + case PID_TYPE_ELEMENTARY: + gst_pes_filter_uninit (&stream->filter); + break; + case PID_PROGRAM_ASSOCIATION_TABLE: + case PID_CONDITIONAL_ACCESS_TABLE: + case PID_TYPE_PROGRAM_MAP: + gst_section_filter_uninit (&stream->section_filter); + break; + } + + g_free (stream); + demux->streams[i] = NULL; + } + } + +#ifdef HAVE_LATENCY + if (demux->clock) { + g_object_unref (demux->clock); + demux->clock = NULL; + } +#endif +} + +#if 0 +static void +gst_fluts_demux_remove_pads (GstFluTSDemux * demux) +{ + /* remove pads we added in preparation for adding new ones */ + /* FIXME: instead of walking all streams, we should retain a list only + * of streams that have added pads */ + gint i; + + if (demux->need_no_more_pads) { + gst_element_no_more_pads ((GstElement *) demux); + demux->need_no_more_pads = FALSE; + } + + for (i = 0; i < FLUTS_MAX_PID + 1; i++) { + GstFluTSStream *stream = demux->streams[i]; + + if (stream != NULL) { + + if (GST_IS_PAD (stream->pad)) { + gst_pad_push_event (stream->pad, gst_event_new_eos ()); + gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad); + } + stream->pad = NULL; + + if (stream->PID_type == PID_TYPE_ELEMENTARY) + gst_pes_filter_drain (&stream->filter); + } + } +} +#endif + + +static guint32 crc_tab[256] = { + 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, + 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, + 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, + 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, + 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, + 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, + 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef, + 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, + 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, + 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, + 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0, + 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, + 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, + 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, + 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, + 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, + 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, + 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, + 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050, + 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, + 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, + 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, + 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, + 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, + 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, + 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, + 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9, + 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, + 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, + 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, + 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, + 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, + 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, + 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, + 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, + 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, + 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, + 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, + 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676, + 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, + 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, + 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, + 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 +}; + +/*This function fills the value of negotiated packetsize at sinkpad*/ +static gboolean +gst_fluts_demux_sink_setcaps (GstPad * pad, GstCaps * caps) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (gst_pad_get_parent (pad)); + gboolean ret = FALSE; + GstStructure *structure = NULL; + gint expected_packetsize = + (demux-> + m2ts_mode ? FLUTS_M2TS_TS_PACKETSIZE : FLUTS_NORMAL_TS_PACKETSIZE); + gint packetsize = expected_packetsize; + + structure = gst_caps_get_structure (caps, 0); + + GST_DEBUG_OBJECT (demux, "setcaps called with %" GST_PTR_FORMAT, caps); + + if (!gst_structure_get_int (structure, "packetsize", &packetsize)) { + GST_DEBUG_OBJECT (demux, "packetsize parameter not found in sink caps"); + } + + if (packetsize < expected_packetsize) { + GST_WARNING_OBJECT (demux, "packetsize = %" G_GINT32_FORMAT "is less then" + "expected packetsize of %d bytes", packetsize, expected_packetsize); + goto beach; + } + + /* here we my have a correct value for packet size */ + demux->packetsize = packetsize; + ret = TRUE; + +beach: + gst_object_unref (demux); + return ret; +} + +static FORCE_INLINE guint32 +gst_fluts_demux_calc_crc32 (guint8 * data, guint datalen) +{ + gint i; + guint32 crc = 0xffffffff; + + for (i = 0; i < datalen; i++) { + crc = (crc << 8) ^ crc_tab[((crc >> 24) ^ *data++) & 0xff]; + } + return crc; +} + +static FORCE_INLINE gboolean +gst_fluts_is_dirac_stream (GstFluTSStream * stream) +{ + gboolean is_dirac = FALSE; + + if (stream->stream_type != ST_VIDEO_DIRAC) + return FALSE; + + if (stream->ES_info != NULL) { + guint8 *dirac_desc; + + /* Check for a Registration Descriptor to confirm this is dirac */ + dirac_desc = gst_mpeg_descriptor_find (stream->ES_info, DESC_REGISTRATION); + if (dirac_desc != NULL && DESC_LENGTH (dirac_desc) >= 4) { + if (DESC_REGISTRATION_format_identifier (dirac_desc) == 0x64726163) { /* 'drac' in hex */ + is_dirac = TRUE; + } + } else { + /* Check for old mapping as originally specified too */ + dirac_desc = gst_mpeg_descriptor_find (stream->ES_info, + DESC_DIRAC_TC_PRIVATE); + if (dirac_desc != NULL && DESC_LENGTH (dirac_desc) == 0) + is_dirac = TRUE; + } + } + + return is_dirac; +} + +static FORCE_INLINE gboolean +gst_fluts_stream_is_video (GstFluTSStream * stream) +{ + switch (stream->stream_type) { + case ST_VIDEO_MPEG1: + case ST_VIDEO_MPEG2: + case ST_VIDEO_MPEG4: + case ST_VIDEO_H264: + return TRUE; + case ST_VIDEO_DIRAC: + return gst_fluts_is_dirac_stream (stream); + } + + return FALSE; +} + +static gboolean +gst_fluts_demux_fill_stream (GstFluTSStream * stream, guint8 id, + guint8 stream_type) +{ + GstPadTemplate *template; + gchar *name; + GstFluTSDemuxClass *klass; + GstFluTSDemux *demux; + GstCaps *caps; + + if (stream->stream_type && stream->stream_type != stream_type) + goto wrong_type; + + demux = stream->demux; + klass = GST_FLUTS_DEMUX_GET_CLASS (demux); + + name = NULL; + template = NULL; + caps = NULL; + + switch (stream_type) { + case ST_VIDEO_MPEG1: + case ST_VIDEO_MPEG2: + template = klass->video_template; + name = g_strdup_printf ("video_%04x", stream->PID); + caps = gst_caps_new_simple ("video/mpeg", + "mpegversion", G_TYPE_INT, stream_type == ST_VIDEO_MPEG1 ? 1 : 2, + "systemstream", G_TYPE_BOOLEAN, FALSE, NULL); + break; + case ST_AUDIO_MPEG1: + case ST_AUDIO_MPEG2: + template = klass->audio_template; + name = g_strdup_printf ("audio_%04x", stream->PID); + caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, 1, NULL); + break; + case ST_PRIVATE_DATA: + /* check if there is an AC3 descriptor associated with this stream + * from the PMT */ + if (gst_mpeg_descriptor_find (stream->ES_info, DESC_DVB_AC3)) { + template = klass->audio_template; + name = g_strdup_printf ("audio_%04x", stream->PID); + caps = gst_caps_new_simple ("audio/x-ac3", NULL); + } else if (gst_mpeg_descriptor_find (stream->ES_info, DESC_DVB_TELETEXT)) { + template = klass->private_template; + name = g_strdup_printf ("private_%04x", stream->PID); + caps = gst_caps_new_simple ("private/teletext", NULL); + } + break; + case ST_PRIVATE_SECTIONS: + case ST_MHEG: + case ST_DSMCC: + break; + case ST_AUDIO_AAC: + template = klass->audio_template; + name = g_strdup_printf ("audio_%04x", stream->PID); + caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, 4, NULL); + break; + case ST_VIDEO_MPEG4: + template = klass->video_template; + name = g_strdup_printf ("video_%04x", stream->PID); + caps = gst_caps_new_simple ("video/mpeg", + "mpegversion", G_TYPE_INT, 4, + "systemstream", G_TYPE_BOOLEAN, FALSE, NULL); + break; + case ST_VIDEO_H264: + template = klass->video_template; + name = g_strdup_printf ("video_%04x", stream->PID); + caps = gst_caps_new_simple ("video/x-h264", NULL); + break; + case ST_VIDEO_DIRAC: + if (gst_fluts_is_dirac_stream (stream)) { + template = klass->video_template; + name = g_strdup_printf ("video_%04x", stream->PID); + caps = gst_caps_new_simple ("video/x-dirac", NULL); + } + break; + case ST_PS_AUDIO_AC3: + template = klass->audio_template; + name = g_strdup_printf ("audio_%04x", stream->PID); + caps = gst_caps_new_simple ("audio/x-ac3", NULL); + break; + case ST_PS_AUDIO_DTS: + template = klass->audio_template; + name = g_strdup_printf ("audio_%04x", stream->PID); + caps = gst_caps_new_simple ("audio/x-dts", NULL); + break; + case ST_PS_AUDIO_LPCM: + template = klass->audio_template; + name = g_strdup_printf ("audio_%04x", stream->PID); + caps = gst_caps_new_simple ("audio/x-lpcm", NULL); + break; + case ST_PS_DVD_SUBPICTURE: + break; + default: + break; + } + if (name == NULL || template == NULL || caps == NULL) + return FALSE; + + stream->stream_type = stream_type; + stream->id = id; + stream->pad = gst_pad_new_from_template (template, name); + gst_pad_use_fixed_caps (stream->pad); + gst_pad_set_caps (stream->pad, caps); + gst_caps_unref (caps); +#ifdef HAVE_LATENCY + gst_pad_set_query_function (stream->pad, + GST_DEBUG_FUNCPTR (gst_fluts_demux_src_pad_query)); +#endif + g_free (name); + + return TRUE; + +wrong_type: + { + return FALSE; + } +} + +static FORCE_INLINE gboolean +fluts_is_elem_pid (GstFluTSDemux * demux, guint16 PID) +{ + int i; + + /* check if it's in our partial ts pid list */ + for (i = 0; i < demux->nb_elementary_pids; i++) { + if (demux->elementary_pids[i] == PID) { + return TRUE; + } + } + + return FALSE; +} + +static gboolean +gst_fluts_demux_send_new_segment (GstFluTSDemux * demux, + GstFluTSStream * stream, gint64 pts) +{ + GstFluTSStream *PCR_stream; + GstFluTSStream *PMT_stream; + GstClockTime time; + guint64 base_PCR; + + /* for the reference start time we need to consult the PCR_PID of the + * current PMT */ + if (demux->current_PMT == 0) + goto no_pmt; + + PMT_stream = demux->streams[demux->current_PMT]; + if (PMT_stream == NULL) + goto no_pmt_stream; + + PCR_stream = demux->streams[PMT_stream->PMT.PCR_PID]; + if (PCR_stream == NULL) + goto no_pcr_stream; + + if (PCR_stream->base_PCR == -1) { + GST_DEBUG_OBJECT (demux, "no base PCR, using last PCR %" G_GUINT64_FORMAT, + PCR_stream->last_PCR); + PCR_stream->base_PCR = PCR_stream->last_PCR; + } else { + GST_DEBUG_OBJECT (demux, "using base PCR %" G_GUINT64_FORMAT, + PCR_stream->base_PCR); + } + if (PCR_stream->last_PCR == -1) { + GST_DEBUG_OBJECT (demux, "no last PCR, using PTS %" G_GUINT64_FORMAT, pts); + PCR_stream->base_PCR = pts; + PCR_stream->last_PCR = pts; + } + base_PCR = PCR_stream->base_PCR; + + time = MPEGTIME_TO_GSTTIME (base_PCR); + + GST_DEBUG_OBJECT (demux, "segment PTS to (%" G_GUINT64_FORMAT ") time: %" + G_GUINT64_FORMAT, base_PCR, time); + +#ifdef HAVE_LATENCY + if (demux->clock && demux->clock_base == GST_CLOCK_TIME_NONE) { + demux->clock_base = gst_clock_get_time (demux->clock); + gst_clock_set_calibration (demux->clock, + gst_clock_get_internal_time (demux->clock), demux->clock_base, 1, 1); + } +#endif + + gst_pad_push_event (stream->pad, + gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, time, -1, 0)); + + return TRUE; + + /* ERRORS */ +no_pmt: + { + /* check if it's in our partial ts pid list */ + if (fluts_is_elem_pid (demux, stream->PID)) { + GST_DEBUG_OBJECT (demux, + "Elementary PID, using pts %" G_GUINT64_FORMAT, pts); + time = MPEGTIME_TO_GSTTIME (pts) + stream->base_time; + GST_DEBUG_OBJECT (demux, "segment PTS to (%" G_GUINT64_FORMAT ") time: %" + G_GUINT64_FORMAT, pts, time); + + gst_pad_push_event (stream->pad, + gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, time, -1, 0)); + return TRUE; + } + + } +no_pmt_stream: + { + GST_DEBUG_OBJECT (demux, "no PMT stream found"); + return FALSE; + } +no_pcr_stream: + { + GST_DEBUG_OBJECT (demux, "no PCR stream found"); + return FALSE; + } +} + +#ifndef GST_FLOW_IS_SUCCESS +#define GST_FLOW_IS_SUCCESS(ret) ((ret) >= GST_FLOW_OK) +#endif + +static GstFlowReturn +gst_fluts_demux_combine_flows (GstFluTSDemux * demux, GstFluTSStream * stream, + GstFlowReturn ret) +{ + gint i; + + /* store the value */ + stream->last_ret = ret; + + /* if it's success we can return the value right away */ + if (GST_FLOW_IS_SUCCESS (ret)) + goto done; + + /* any other error that is not-linked can be returned right + * away */ + if (ret != GST_FLOW_NOT_LINKED) + goto done; + + /* only return NOT_LINKED if all other pads returned NOT_LINKED */ + for (i = 0; i < FLUTS_MAX_PID + 1; i++) { + if (!(stream = demux->streams[i])) + continue; + + /* some other return value (must be SUCCESS but we can return + * other values as well) */ + ret = stream->last_ret; + if (ret != GST_FLOW_NOT_LINKED) + goto done; + } + /* if we get here, all other pads were unlinked and we return + * NOT_LINKED then */ +done: + return ret; +} + + +static GstFlowReturn +gst_fluts_demux_data_cb (GstPESFilter * filter, gboolean first, + GstBuffer * buffer, GstFluTSStream * stream) +{ + GstFluTSDemux *demux; + GstFlowReturn ret; + GstPad *srcpad; + gint64 pts; + GstClockTime time; + + demux = stream->demux; + srcpad = stream->pad; + + GST_LOG_OBJECT (demux, "got data on PID 0x%04x", stream->PID); + + if (first && filter->pts != -1) { + pts = filter->pts; + time = MPEGTIME_TO_GSTTIME (pts) + stream->base_time; + + if ((stream->last_time > 0 && stream->last_time < time && + time - stream->last_time > GST_SECOND * 60 * 10) + || (stream->last_time > time + && stream->last_time - time > GST_SECOND * 60 * 10)) { + /* check first to see if we're in middle of detecting a discont in PCR. + * if we are we're not sure what timestamp the buffer should have, best + * to drop. */ + if (stream->PMT_pid <= FLUTS_MAX_PID && demux->streams[stream->PMT_pid] && + demux->streams[demux->streams[stream->PMT_pid]->PMT.PCR_PID] && + demux->streams[demux->streams[stream->PMT_pid]->PMT.PCR_PID]-> + discont_PCR) { + GST_WARNING_OBJECT (demux, "middle of discont, dropping"); + goto bad_timestamp; + } + /* check for wraparounds */ + else if (stream->last_time > 0 && time < stream->last_time && + stream->last_time - time > MPEGTIME_TO_GSTTIME (G_MAXUINT32)) { + /* wrap around occurred */ + if (stream->base_time + MPEGTIME_TO_GSTTIME ((guint64) (1) << 33) + + MPEGTIME_TO_GSTTIME (pts) > + stream->last_time + GST_SECOND * 60 * 10) { + GST_DEBUG_OBJECT (demux, + "looks like we have a corrupt packet because its pts is a lot lower than" + " the previous pts but not a wraparound"); + goto bad_timestamp; + } + /* wraparound has occured but before we have detected in the pcr, + * so check we're actually getting pcr's...if we are, don't update + * the base time..just set the time and last_time correctly + */ + if (stream->PMT_pid <= FLUTS_MAX_PID && demux->streams[stream->PMT_pid] + && demux->streams[demux->streams[stream->PMT_pid]->PMT.PCR_PID] + && demux->streams[demux->streams[stream->PMT_pid]->PMT.PCR_PID]-> + last_PCR > 0) { + GST_DEBUG_OBJECT (demux, "timestamps wrapped before noticed in PCR"); + time = MPEGTIME_TO_GSTTIME (pts) + stream->base_time + + MPEGTIME_TO_GSTTIME ((guint64) (1) << 33); + stream->last_time = time; + } else { + stream->base_time = stream->base_time + + MPEGTIME_TO_GSTTIME ((guint64) (1) << 33); + time = MPEGTIME_TO_GSTTIME (pts) + stream->base_time; + GST_DEBUG_OBJECT (demux, + "timestamps wrapped around, compensating with new base time: %" + GST_TIME_FORMAT "last time: %" GST_TIME_FORMAT " time: %" + GST_TIME_FORMAT, GST_TIME_ARGS (stream->base_time), + GST_TIME_ARGS (stream->last_time), GST_TIME_ARGS (time)); + stream->last_time = time; + } + } else if (stream->last_time > 0 && time > stream->last_time && + time - stream->last_time > MPEGTIME_TO_GSTTIME (G_MAXUINT32) && + stream->base_time > 0) { + /* had a previous wrap around */ + if (time - MPEGTIME_TO_GSTTIME ((guint64) (1) << 33) + + GST_SECOND * 60 * 10 < stream->last_time) { + GST_DEBUG_OBJECT (demux, + "looks like we have a corrupt packet because its pts is a lot higher than" + " the previous pts but not because of a wraparound or pcr discont"); + goto bad_timestamp; + } + if (ABS ((time - MPEGTIME_TO_GSTTIME ((guint64) (1) << 33)) - + stream->last_time) < GST_SECOND) { + GST_DEBUG_OBJECT (demux, + "timestamps wrapped around earlier but we have an out of pts: %" + G_GUINT64_FORMAT ", as %" GST_TIME_FORMAT " translated to: %" + GST_TIME_FORMAT " and last_time of %" GST_TIME_FORMAT, pts, + GST_TIME_ARGS (time), + GST_TIME_ARGS (time - MPEGTIME_TO_GSTTIME ((guint64) (1) << 33)), + GST_TIME_ARGS (stream->last_time)); + time = time - MPEGTIME_TO_GSTTIME ((guint64) (1) << 33); + } else { + GST_DEBUG_OBJECT (demux, + "timestamp may have wrapped around recently but not sure and pts" + " is very different, dropping it timestamp of this packet: %" + GST_TIME_FORMAT " compared to last timestamp: %" GST_TIME_FORMAT, + GST_TIME_ARGS (time - + MPEGTIME_TO_GSTTIME ((guint64) (1) << (33))), + GST_TIME_ARGS (stream->last_time)); + goto bad_timestamp; + } + + } else { + /* we must have a corrupt packet */ + GST_WARNING_OBJECT (demux, "looks like we have a corrupt packet because" + " its timestamp is buggered timestamp: %" GST_TIME_FORMAT + " compared to" " last timestamp: %" GST_TIME_FORMAT, + GST_TIME_ARGS (time), GST_TIME_ARGS (stream->last_time)); + goto bad_timestamp; + } + } else { /* do not set last_time if a packet with pts from before wrap + around arrived after the wrap around occured */ + stream->last_time = time; + } + } else { + time = GST_CLOCK_TIME_NONE; + pts = -1; + } + + GST_LOG_OBJECT (demux, "setting PTS to (%" G_GUINT64_FORMAT ") time: %" + GST_TIME_FORMAT " on buffer 0x%x first buffer: %d base_time: %" + GST_TIME_FORMAT, pts, GST_TIME_ARGS (time), buffer, first, + GST_TIME_ARGS (stream->base_time)); + + GST_BUFFER_TIMESTAMP (buffer) = time; + + /* check if we have a pad already */ + if (srcpad == NULL) { + /* fill in the last bits of the stream */ + /* if no stream type, then assume it based on the PES start code, + * needed for partial ts streams without PMT */ + if (G_UNLIKELY (stream->flags & FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN)) { + if ((filter->start_code & 0xFFFFFFF0) == PACKET_VIDEO_START_CODE) { + /* it is mpeg2 video */ + stream->stream_type = ST_VIDEO_MPEG2; + stream->flags &= ~FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; + stream->flags |= FLUTS_STREAM_FLAG_IS_VIDEO; + GST_DEBUG_OBJECT (demux, "Found stream 0x%04x without PMT with video " + "start_code. Treating as video", stream->PID); + } else if ((filter->start_code & 0xFFFFFFE0) == PACKET_AUDIO_START_CODE) { + /* it is mpeg audio */ + stream->stream_type = ST_AUDIO_MPEG2; + stream->flags &= ~FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; + GST_DEBUG_OBJECT (demux, "Found stream 0x%04x without PMT with audio " + "start_code. Treating as audio", stream->PID); + } else { + GST_LOG_OBJECT (demux, "Stream start code on pid 0x%04x is: 0x%x", + stream->PID, filter->start_code); + } + } + if (!gst_fluts_demux_fill_stream (stream, filter->id, stream->stream_type)) + goto unknown_type; + + GST_DEBUG_OBJECT (demux, + "New stream 0x%04x of type %d with caps %" GST_PTR_FORMAT, stream->PID, + stream->stream_type, GST_PAD_CAPS (stream->pad)); + + srcpad = stream->pad; + + /* activate and add */ + gst_pad_set_active (srcpad, TRUE); + gst_element_add_pad (GST_ELEMENT_CAST (demux), srcpad); + demux->need_no_more_pads = TRUE; + + /* send new_segment */ + gst_fluts_demux_send_new_segment (demux, stream, pts); + } + + GST_DEBUG_OBJECT (demux, "pushing buffer"); + gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad)); + ret = gst_pad_push (srcpad, buffer); + ret = gst_fluts_demux_combine_flows (demux, stream, ret); + + return ret; + + /* ERROR */ +unknown_type: + { + GST_DEBUG_OBJECT (demux, "got unknown stream id 0x%02x, type 0x%02x", + filter->id, filter->type); + gst_buffer_unref (buffer); + return gst_fluts_demux_combine_flows (demux, stream, GST_FLOW_NOT_LINKED); + } +bad_timestamp: + { + gst_buffer_unref (buffer); + return gst_fluts_demux_combine_flows (demux, stream, GST_FLOW_OK); + } + +} + +static void +gst_fluts_demux_resync_cb (GstPESFilter * filter, GstFluTSStream * stream) +{ + /* does nothing for now */ +} + +static gboolean +gst_fluts_demux_is_reserved_PID (GstFluTSDemux * demux, guint16 PID) +{ + return (PID >= PID_RESERVED_FIRST) && (PID < PID_RESERVED_LAST); +} + +/* This function assumes that provided PID never will be greater than + * FLUTS_MAX_PID (13 bits), this is currently guaranteed as everywhere in + * the code recovered PID at maximum is 13 bits long. + */ +static FORCE_INLINE GstFluTSStream * +gst_fluts_demux_get_stream_for_PID (GstFluTSDemux * demux, guint16 PID) +{ + GstFluTSStream *stream = NULL; + + stream = demux->streams[PID]; + + if (G_UNLIKELY (stream == NULL)) { + stream = g_new0 (GstFluTSStream, 1); + + stream->demux = demux; + stream->PID = PID; + stream->pad = NULL; + stream->base_PCR = -1; + stream->last_PCR = -1; + stream->last_PCR_difference = -1; + stream->PMT.version_number = -1; + stream->PAT.version_number = -1; + stream->PMT_pid = FLUTS_MAX_PID + 1; + stream->flags |= FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; + + switch (PID) { + /* check for fixed mapping */ + case PID_PROGRAM_ASSOCIATION_TABLE: + stream->PID_type = PID_TYPE_PROGRAM_ASSOCIATION; + /* initialise section filter */ + gst_section_filter_init (&stream->section_filter); + break; + case PID_CONDITIONAL_ACCESS_TABLE: + stream->PID_type = PID_TYPE_CONDITIONAL_ACCESS; + /* initialise section filter */ + gst_section_filter_init (&stream->section_filter); + break; + case PID_NULL_PACKET: + stream->PID_type = PID_TYPE_NULL_PACKET; + break; + default: + /* mark reserved PIDs */ + if (gst_fluts_demux_is_reserved_PID (demux, PID)) { + stream->PID_type = PID_TYPE_RESERVED; + } else { + /* check if PMT found in PAT */ + if (gst_fluts_demux_is_PMT (demux, PID)) { + stream->PID_type = PID_TYPE_PROGRAM_MAP; + /* initialise section filter */ + gst_section_filter_init (&stream->section_filter); + } else + stream->PID_type = PID_TYPE_UNKNOWN; + } + break; + } + GST_DEBUG_OBJECT (demux, "creating stream %p for PID 0x%04x, PID_type %d", + stream, PID, stream->PID_type); + + demux->streams[PID] = stream; + } + + return stream; +} + +/* + * CA_section() { + * table_id 8 uimsbf == 0x01 + * section_syntax_indicator 1 bslbf == 1 + * '0' 1 bslbf == 0 + * reserved 2 bslbf + * section_length 12 uimsbf == 00xxxxx... + * reserved 18 bslbf + * version_number 5 uimsbf + * current_next_indicator 1 bslbf + * section_number 8 uimsbf + * last_section_number 8 uimsbf + * for (i=0; i<N;i++) { + * descriptor() + * } + * CRC_32 32 rpchof + * } + */ +static FORCE_INLINE gboolean +gst_fluts_stream_parse_cat (GstFluTSStream * stream, + guint8 * data, guint datalen) +{ + GstFluTSDemux *demux; + + demux = stream->demux; + + GST_DEBUG_OBJECT (demux, "parsing CA section"); + return TRUE; +} + +static void +gst_fluts_activate_pmt (GstFluTSDemux * demux, GstFluTSStream * stream) +{ + GST_DEBUG_OBJECT (demux, "activating PMT 0x%08x", stream->PID); + + /* gst_fluts_demux_remove_pads (demux); */ + + demux->current_PMT = stream->PID; + + /* PMT has been updated, signal the change */ + if (demux->current_PMT == stream->PID) + g_object_notify ((GObject *) (demux), "pmt-info"); +} + +/* + * TS_program_map_section() { + * table_id 8 uimsbf == 0x02 + * section_syntax_indicator 1 bslbf == 1 + * '0' 1 bslbf == 0 + * reserved 2 bslbf + * section_length 12 uimsbf == 00xxxxx... + * program_number 16 uimsbf + * reserved 2 bslbf + * version_number 5 uimsbf + * current_next_indicator 1 bslbf + * section_number 8 uimsbf + * last_section_number 8 uimsbf + * reserved 3 bslbf + * PCR_PID 13 uimsbf + * reserved 4 bslbf + * program_info_length 12 uimsbf == 00xxxxx... + * for (i=0; i<N; i++) { + * descriptor() + * } + * for (i=0;i<N1;i++) { + * stream_type 8 uimsbf + * reserved 3 bslbf + * elementary_PID 13 uimsnf + * reserved 4 bslbf + * ES_info_length 12 uimsbf == 00xxxxx... + * for (i=0; i<N2; i++) { + * descriptor() + * } + * } + * CRC_32 32 rpchof + * } + */ +static FORCE_INLINE gboolean +gst_fluts_stream_parse_pmt (GstFluTSStream * stream, + guint8 * data, guint datalen) +{ + GstFluTSDemux *demux; + gint entries; + guint32 CRC; + GstFluTSPMT *PMT; + guint version_number; + guint8 current_next_indicator; + guint16 program_number; + + demux = stream->demux; + + if (*data++ != 0x02) + goto wrong_id; + if ((data[0] & 0xc0) != 0x80) + goto wrong_sync; + if ((data[0] & 0x0c) != 0x00) + goto wrong_seclen; + + data += 2; + + if (demux->check_crc) + if (gst_fluts_demux_calc_crc32 (data - 3, datalen) != 0) + goto wrong_crc; + + GST_DEBUG_OBJECT (demux, "PMT section_length: %d", datalen - 3); + + PMT = &stream->PMT; + + /* check if version number changed */ + version_number = (data[2] & 0x3e) >> 1; + GST_DEBUG_OBJECT (demux, "PMT version_number: %d", version_number); + + current_next_indicator = (data[2] & 0x01); + GST_DEBUG_OBJECT (demux, "PMT current_next_indicator %d", + current_next_indicator); + if (current_next_indicator == 0) + goto not_yet_applicable; + program_number = GST_READ_UINT16_BE (data); + + if (demux->program_number != -1 && demux->program_number != program_number) { + goto wrong_program_number; + } + if (demux->program_number == -1) { + GST_INFO_OBJECT (demux, "No program number set, so using first parsed PMT" + "'s program number: %d", program_number); + demux->program_number = program_number; + } + + if (version_number == PMT->version_number) + goto same_version; + + PMT->version_number = version_number; + PMT->current_next_indicator = current_next_indicator; + + stream->PMT.program_number = program_number; + data += 3; + GST_DEBUG_OBJECT (demux, "PMT program_number: %d", PMT->program_number); + + PMT->section_number = *data++; + GST_DEBUG_OBJECT (demux, "PMT section_number: %d", PMT->section_number); + + PMT->last_section_number = *data++; + GST_DEBUG_OBJECT (demux, "PMT last_section_number: %d", + PMT->last_section_number); + + PMT->PCR_PID = GST_READ_UINT16_BE (data); + PMT->PCR_PID &= 0x1fff; + data += 2; + GST_DEBUG_OBJECT (demux, "PMT PCR_PID: 0x%04x", PMT->PCR_PID); + /* create or get stream, not much we can say about it except that when we get + * a data stream and we need a PCR, we can use the stream to get/store the + * base_PCR. */ + gst_fluts_demux_get_stream_for_PID (demux, PMT->PCR_PID); + + if ((data[0] & 0x0c) != 0x00) + goto wrong_pilen; + + PMT->program_info_length = GST_READ_UINT16_BE (data); + PMT->program_info_length &= 0x0fff; + /* FIXME: validate value of program_info_length */ + data += 2; + + /* FIXME: validate value of program_info_length, before using */ + + /* parse descriptor */ + if (G_UNLIKELY (PMT->program_info)) + gst_mpeg_descriptor_free (PMT->program_info); + PMT->program_info = + gst_mpeg_descriptor_parse (data, PMT->program_info_length); + + /* skip descriptor */ + data += PMT->program_info_length; + GST_DEBUG_OBJECT (demux, "PMT program_info_length: %d", + PMT->program_info_length); + + entries = datalen - 3 - PMT->program_info_length - 9 - 4; + + if (G_UNLIKELY (PMT->entries)) + g_array_free (PMT->entries, TRUE); + PMT->entries = g_array_new (FALSE, TRUE, sizeof (GstFluTSPMTEntry)); + + while (entries > 0) { + GstFluTSPMTEntry entry; + GstFluTSStream *ES_stream; + guint8 stream_type; + guint16 ES_info_length; + + stream_type = *data++; + + entry.PID = GST_READ_UINT16_BE (data); + entry.PID &= 0x1fff; + data += 2; + + if ((data[0] & 0x0c) != 0x00) + goto wrong_esilen; + + ES_info_length = GST_READ_UINT16_BE (data); + ES_info_length &= 0x0fff; + data += 2; + + /* get/create elementary stream */ + ES_stream = gst_fluts_demux_get_stream_for_PID (demux, entry.PID); + /* check if PID unknown */ + if (ES_stream->PID_type == PID_TYPE_UNKNOWN) { + /* set as elementary */ + ES_stream->PID_type = PID_TYPE_ELEMENTARY; + /* set stream type */ + ES_stream->stream_type = stream_type; + ES_stream->flags &= ~FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; + + /* init base and last time */ + ES_stream->base_time = 0; + ES_stream->last_time = 0; + + /* parse descriptor */ + ES_stream->ES_info = gst_mpeg_descriptor_parse (data, ES_info_length); + + if (stream_type == ST_PRIVATE_SECTIONS) { + /* not really an ES, so use section filter not pes filter */ + /* initialise section filter */ + gst_section_filter_init (&ES_stream->section_filter); + ES_stream->PID_type = PID_TYPE_PRIVATE_SECTION; + ES_stream->pad = gst_pad_new_from_static_template (&private_template, + g_strdup_printf ("private_%04x", entry.PID)); + gst_pad_set_active (ES_stream->pad, TRUE); + gst_element_add_pad (GST_ELEMENT_CAST (demux), ES_stream->pad); + } else { + /* Recognise video streams based on stream_type */ + if (gst_fluts_stream_is_video (ES_stream)) + ES_stream->flags |= FLUTS_STREAM_FLAG_IS_VIDEO; + + /* set adaptor */ + gst_pes_filter_init (&ES_stream->filter, NULL, NULL); + gst_pes_filter_set_callbacks (&ES_stream->filter, + (GstPESFilterData) gst_fluts_demux_data_cb, + (GstPESFilterResync) gst_fluts_demux_resync_cb, ES_stream); + if (ES_stream->flags & FLUTS_STREAM_FLAG_IS_VIDEO) + ES_stream->filter.allow_unbounded = TRUE; + ES_stream->PMT_pid = stream->PID; + } + } + /* skip descriptor */ + data += ES_info_length; + GST_DEBUG_OBJECT (demux, + " PMT stream_type: %02x, PID: 0x%04x (ES_info_len %d)", stream_type, + entry.PID, ES_info_length); + + g_array_append_val (PMT->entries, entry); + + entries -= 5 + ES_info_length; + } + CRC = GST_READ_UINT32_BE (data); + GST_DEBUG_OBJECT (demux, "PMT CRC: 0x%08x", CRC); + + if (demux->program_number == -1) { + /* No program specified, take the first PMT */ + if (demux->current_PMT == 0 || demux->current_PMT == stream->PID) + gst_fluts_activate_pmt (demux, stream); + } else { + /* Program specified, activate this if it matches */ + if (demux->program_number == PMT->program_number) + gst_fluts_activate_pmt (demux, stream); + } + + return TRUE; + + /* ERRORS */ +wrong_crc: + { + GST_DEBUG_OBJECT (demux, "wrong crc"); + return FALSE; + } +same_version: + { + GST_DEBUG_OBJECT (demux, "same version as existing PMT"); + return TRUE; + } +wrong_program_number: + { + GST_DEBUG_OBJECT (demux, "PMT is for program number we don't care about"); + return TRUE; + } + +not_yet_applicable: + { + GST_DEBUG_OBJECT (demux, "Ignoring PMT with current_next_indicator = 0"); + return TRUE; + } +wrong_id: + { + GST_DEBUG_OBJECT (demux, "expected table_id == 0, got 0x%02x", data[0]); + return FALSE; + } +wrong_sync: + { + GST_DEBUG_OBJECT (demux, "expected sync 10, got %02x", data[0]); + return FALSE; + } +wrong_seclen: + { + GST_DEBUG_OBJECT (demux, + "first two bits of section length must be 0, got %02x", data[0]); + return FALSE; + } +wrong_pilen: + { + GST_DEBUG_OBJECT (demux, + "first two bits of program_info length must be 0, got %02x", data[0]); + return FALSE; + } +wrong_esilen: + { + GST_DEBUG_OBJECT (demux, + "first two bits of ES_info length must be 0, got %02x", data[0]); + g_array_free (stream->PMT.entries, TRUE); + stream->PMT.entries = NULL; + gst_mpeg_descriptor_free (stream->PMT.program_info); + stream->PMT.program_info = NULL; + return FALSE; + } +} + +/* + * private_section() { + * table_id 8 uimsbf + * section_syntax_indicator 1 bslbf + * private_indicator 1 bslbf + * reserved 2 bslbf + * private_section_length 12 uimsbf + * if (section_syntax_indicator == '0') { + * for ( i=0;i<N;i++) { + * private_data_byte 8 bslbf + * } + * } + * else { + * table_id_extension 16 uimsbf + * reserved 2 bslbf + * version_number 5 uimsbf + * current_next_indicator 1 bslbf + * section_number 8 uimsbf + * last_section_number 8 uimsbf + * for ( i=0;i<private_section_length-9;i++) { + * private_data_byte 8 bslbf + * } + * CRC_32 32 rpchof + * } + * } + */ +static FORCE_INLINE gboolean +gst_fluts_stream_parse_private_section (GstFluTSStream * stream, + guint8 * data, guint datalen) +{ + GstFluTSDemux *demux; + GstBuffer *buffer; + demux = stream->demux; + + if (demux->check_crc) + if (gst_fluts_demux_calc_crc32 (data, datalen) != 0) + goto wrong_crc; + + /* just dump this down the pad */ + if (gst_pad_alloc_buffer (stream->pad, 0, datalen, NULL, &buffer) == + GST_FLOW_OK) { +#ifdef USE_LIBOIL + oil_memcpy (buffer->data, data, datalen); +#else + memcpy (buffer->data, data, datalen); +#endif + gst_pad_push (stream->pad, buffer); + } + + GST_DEBUG_OBJECT (demux, "parsing private section"); + return TRUE; + +wrong_crc: + { + GST_DEBUG_OBJECT (demux, "wrong crc"); + return FALSE; + } +} + +/* + * adaptation_field() { + * adaptation_field_length 8 uimsbf + * if(adaptation_field_length >0) { + * discontinuity_indicator 1 bslbf + * random_access_indicator 1 bslbf + * elementary_stream_priority_indicator 1 bslbf + * PCR_flag 1 bslbf + * OPCR_flag 1 bslbf + * splicing_point_flag 1 bslbf + * transport_private_data_flag 1 bslbf + * adaptation_field_extension_flag 1 bslbf + * if(PCR_flag == '1') { + * program_clock_reference_base 33 uimsbf + * reserved 6 bslbf + * program_clock_reference_extension 9 uimsbf + * } + * if(OPCR_flag == '1') { + * original_program_clock_reference_base 33 uimsbf + * reserved 6 bslbf + * original_program_clock_reference_extension 9 uimsbf + * } + * if (splicing_point_flag == '1') { + * splice_countdown 8 tcimsbf + * } + * if(transport_private_data_flag == '1') { + * transport_private_data_length 8 uimsbf + * for (i=0; i<transport_private_data_length;i++){ + * private_data_byte 8 bslbf + * } + * } + * if (adaptation_field_extension_flag == '1' ) { + * adaptation_field_extension_length 8 uimsbf + * ltw_flag 1 bslbf + * piecewise_rate_flag 1 bslbf + * seamless_splice_flag 1 bslbf + * reserved 5 bslbf + * if (ltw_flag == '1') { + * ltw_valid_flag 1 bslbf + * ltw_offset 15 uimsbf + * } + * if (piecewise_rate_flag == '1') { + * reserved 2 bslbf + * piecewise_rate 22 uimsbf + * } + * if (seamless_splice_flag == '1'){ + * splice_type 4 bslbf + * DTS_next_AU[32..30] 3 bslbf + * marker_bit 1 bslbf + * DTS_next_AU[29..15] 15 bslbf + * marker_bit 1 bslbf + * DTS_next_AU[14..0] 15 bslbf + * marker_bit 1 bslbf + * } + * for ( i=0;i<N;i++) { + * reserved 8 bslbf + * } + * } + * for (i=0;i<N;i++){ + * stuffing_byte 8 bslbf + * } + * } + * } + */ +static FORCE_INLINE gboolean +gst_fluts_demux_parse_adaptation_field (GstFluTSStream * stream, + const guint8 * data, guint data_len, guint * consumed) +{ + GstFluTSDemux *demux; + guint8 length; + guint8 *data_end; + gint i; + GstFluTSStream *pmt_stream; + + demux = stream->demux; + + data_end = ((guint8 *) data) + data_len; + + length = *data++; + if (G_UNLIKELY (length > data_len)) + goto wrong_length; + + GST_DEBUG_OBJECT (demux, "parsing adaptation field, length %d", length); + + if (length > 0) { + guint8 flags = *data++; + + GST_DEBUG_OBJECT (demux, "flags 0x%02x", flags); + /* discontinuity flag */ + if (flags & 0x80) { + GST_DEBUG_OBJECT (demux, "discontinuity flag set"); + } + /* PCR_flag */ + if (flags & 0x10) { + guint32 pcr1; + guint16 pcr2; + guint64 pcr, pcr_ext; + gboolean valid_pcr = TRUE; + + pcr1 = GST_READ_UINT32_BE (data); + pcr2 = GST_READ_UINT16_BE (data + 4); + pcr = ((guint64) pcr1) << 1; + pcr |= (pcr2 & 0x8000) >> 15; + pcr_ext = (pcr2 & 0x01ff); + if (pcr_ext) + pcr = (pcr * 300 + pcr_ext % 300) / 300; + GST_DEBUG_OBJECT (demux, "have PCR %" G_GUINT64_FORMAT " on PID 0x%04x " + "and last pcr is %" G_GUINT64_FORMAT, + pcr, stream->PID, stream->last_PCR); + /* pcr has been converted into units of 90Khz ticks + * so assume discont if last pcr was > 900000 (10 second) lower */ + if (stream->last_PCR != -1 && + (pcr - stream->last_PCR > 900000 || pcr < stream->last_PCR)) { + GstClockTimeDiff base_time_difference; + + GST_DEBUG_OBJECT (demux, + "looks like we have a discont, this pcr should really be approx: %" + G_GUINT64_FORMAT, stream->last_PCR + stream->last_PCR_difference); + if (stream->discont_PCR == FALSE) { + if (pcr > stream->last_PCR) { + base_time_difference = -MPEGTIME_TO_GSTTIME ((pcr - + (stream->last_PCR + stream->last_PCR_difference))); + } else { + base_time_difference = MPEGTIME_TO_GSTTIME ((stream->last_PCR + + stream->last_PCR_difference) - pcr); + } + stream->discont_PCR = TRUE; + stream->discont_difference = base_time_difference; + valid_pcr = FALSE; + } else { + GstClockTimeDiff base_time_difference; + + /* need to update all pmt streams in case this pcr is pcr + * for multiple programs */ + int j; + gboolean *pmts_checked = (gboolean *) & demux->pmts_checked; + memset (pmts_checked, 0, sizeof (gboolean) * (FLUTS_MAX_PID + 1)); + + for (j = 0; j < FLUTS_MAX_PID + 1; j++) { + if (demux->streams[j] && demux->streams[j]->PMT_pid) { + if (!pmts_checked[demux->streams[j]->PMT_pid]) { + /* check if this is correct pcr for pmt */ + if (demux->streams[demux->streams[j]->PMT_pid] && + stream->PID == + demux->streams[demux->streams[j]->PMT_pid]->PMT.PCR_PID) { + /* checking the pcr discont is similar this second time + * if similar, update the es pids + * if not, assume it's a false discont due to corruption + * or other */ + if (pcr > stream->last_PCR) { + base_time_difference = -MPEGTIME_TO_GSTTIME ((pcr - + (stream->last_PCR + stream->last_PCR_difference))); + } else { + base_time_difference = + MPEGTIME_TO_GSTTIME ((stream->last_PCR + + stream->last_PCR_difference) - pcr); + } + if ((base_time_difference - stream->discont_difference > 0 && + base_time_difference - stream->discont_difference < + GST_SECOND * 10) || + (stream->discont_difference - base_time_difference > 0 && + stream->discont_difference - base_time_difference < + GST_SECOND * 10)) { + pmt_stream = demux->streams[demux->streams[j]->PMT_pid]; + GST_DEBUG_OBJECT (demux, "Updating base_time on all es " + "pids belonging to PMT 0x%02x", stream->PMT_pid); + for (i = 0; i <= pmt_stream->PMT.entries->len; i++) { + GstFluTSPMTEntry *cur_entry = + &g_array_index (pmt_stream->PMT.entries, + GstFluTSPMTEntry, i); + GST_DEBUG_OBJECT (demux, + "Updating base time on " "pid 0x%02x by %" + G_GINT64_FORMAT, cur_entry->PID, + stream->discont_difference); + if (cur_entry->PID <= FLUTS_MAX_PID + && demux->streams[cur_entry->PID]) { + demux->streams[cur_entry->PID]->base_time += + stream->discont_difference; + } + } + } else { + GST_DEBUG_OBJECT (demux, "last PCR discont looked to be " + "bogus: previous discont difference %" G_GINT64_FORMAT + " now %" G_GINT64_FORMAT, stream->discont_difference, + base_time_difference); + valid_pcr = FALSE; + } + } + } + pmts_checked[demux->streams[j]->PMT_pid] = TRUE; + } + } + + stream->discont_PCR = FALSE; + stream->discont_difference = 0; + } + } else if (stream->last_PCR != -1) { + if (stream->discont_PCR) { + GST_DEBUG_OBJECT (demux, "last PCR discont looked to be bogus"); + stream->discont_PCR = FALSE; + stream->discont_difference = 0; + } + stream->last_PCR_difference = pcr - stream->last_PCR; + } + GST_DEBUG_OBJECT (demux, + "valid pcr: %d last PCR difference: %" G_GUINT64_FORMAT, valid_pcr, + stream->last_PCR_difference); + if (valid_pcr) { + + stream->last_PCR = pcr; + +#ifdef HAVE_LATENCY + if (demux->clock && demux->clock_base != GST_CLOCK_TIME_NONE) { + gdouble r_squared; + GstFluTSStream *PMT_stream; + + /* for the reference start time we need to consult the PCR_PID of the + * current PMT */ + PMT_stream = demux->streams[demux->current_PMT]; + if (PMT_stream->PMT.PCR_PID == stream->PID) { + GST_LOG_OBJECT (demux, + "internal %" GST_TIME_FORMAT " observation %" GST_TIME_FORMAT + " pcr: %" G_GUINT64_FORMAT " base_pcr: %" G_GUINT64_FORMAT + "pid: %d", + GST_TIME_ARGS (gst_clock_get_internal_time (demux->clock)), + GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (pcr) - + MPEGTIME_TO_GSTTIME (stream->base_PCR) + stream->base_time + + demux->clock_base), pcr, stream->base_PCR, stream->PID); + gst_clock_add_observation (demux->clock, + gst_clock_get_internal_time (demux->clock), + demux->clock_base + stream->base_time + + MPEGTIME_TO_GSTTIME (pcr) - + MPEGTIME_TO_GSTTIME (stream->base_PCR), &r_squared); + } + } +#endif + } + data += 6; + } + /* OPCR_flag */ + if (flags & 0x08) { + guint32 opcr1; + guint16 opcr2; + guint64 opcr, opcr_ext; + + opcr1 = GST_READ_UINT32_BE (data); + opcr2 = GST_READ_UINT16_BE (data + 4); + opcr = ((guint64) opcr1) << 1; + opcr |= (opcr2 & 0x8000) >> 15; + opcr_ext = (opcr2 & 0x01ff); + if (opcr_ext) + opcr = (opcr * 300 + opcr_ext % 300) / 300; + GST_DEBUG_OBJECT (demux, "have OPCR %" G_GUINT64_FORMAT " on PID 0x%04x", + opcr, stream->PID); + stream->last_OPCR = opcr; + data += 6; + } + /* splicing_point_flag */ + if (flags & 0x04) { + guint8 splice_countdown; + + splice_countdown = *data++; + GST_DEBUG_OBJECT (demux, "have splicing point, countdown %d", + splice_countdown); + } + /* transport_private_data_flag */ + if (flags & 0x02) { + guint8 plength = *data++; + + if (data + plength > data_end) + goto private_data_too_large; + + GST_DEBUG_OBJECT (demux, "have private data, length: %d", plength); + data += plength; + } + /* adaptation_field_extension_flag */ + if (flags & 0x01) { + GST_DEBUG_OBJECT (demux, "have field extension"); + } + } + + *consumed = length + 1; + return TRUE; + + /* ERRORS */ +wrong_length: + { + GST_DEBUG_OBJECT (demux, "length %d > %d", length, data_len); + return FALSE; + } +private_data_too_large: + { + GST_DEBUG_OBJECT (demux, "have too large a private data length"); + return FALSE; + } +} + +/* + * program_association_section() { + * table_id 8 uimsbf == 0x00 + * section_syntax_indicator 1 bslbf == 1 + * '0' 1 bslbf == 0 + * reserved 2 bslbf + * section_length 12 uimsbf == 00xxxxx... + * transport_stream_id 16 uimsbf + * reserved 2 bslbf + * version_number 5 uimsbf + * current_next_indicator 1 bslbf + * section_number 8 uimsbf + * last_section_number 8 uimsbf + * for (i=0; i<N;i++) { + * program_number 16 uimsbf + * reserved 3 bslbf + * if(program_number == '0') { + * network_PID 13 uimsbf + * } + * else { + * program_map_PID 13 uimsbf + * } + * } + * CRC_32 32 rpchof + * } + */ +static FORCE_INLINE gboolean +gst_fluts_stream_parse_pat (GstFluTSStream * stream, + guint8 * data, guint datalen) +{ + GstFluTSDemux *demux; + gint entries; + guint32 CRC; + guint version_number; + guint8 current_next_indicator; + GstFluTSPAT *PAT; + + demux = stream->demux; + + if (datalen < 8) + return FALSE; + + if (*data++ != 0x00) + goto wrong_id; + if ((data[0] & 0xc0) != 0x80) + goto wrong_sync; + if ((data[0] & 0x0c) != 0x00) + goto wrong_seclen; + + data += 2; + GST_DEBUG_OBJECT (demux, "PAT section_length: %d", datalen - 3); + + if (demux->check_crc) + if (gst_fluts_demux_calc_crc32 (data - 3, datalen) != 0) + goto wrong_crc; + + PAT = &stream->PAT; + + version_number = (data[2] & 0x3e) >> 1; + GST_DEBUG_OBJECT (demux, "PAT version_number: %d", version_number); + if (version_number == PAT->version_number) + goto same_version; + + current_next_indicator = (data[2] & 0x01); + GST_DEBUG_OBJECT (demux, "PAT current_next_indicator %d", + current_next_indicator); + if (current_next_indicator == 0) + goto not_yet_applicable; + + PAT->version_number = version_number; + PAT->current_next_indicator = current_next_indicator; + + PAT->transport_stream_id = GST_READ_UINT16_BE (data); + data += 3; + GST_DEBUG_OBJECT (demux, "PAT stream_id: %d", PAT->transport_stream_id); + + PAT->section_number = *data++; + PAT->last_section_number = *data++; + + GST_DEBUG_OBJECT (demux, "PAT current_next_indicator: %d", + PAT->current_next_indicator); + GST_DEBUG_OBJECT (demux, "PAT section_number: %d", PAT->section_number); + GST_DEBUG_OBJECT (demux, "PAT last_section_number: %d", + PAT->last_section_number); + + /* 5 bytes after section length and a 4 bytes CRC, + * the rest is 4 byte entries */ + entries = (datalen - 3 - 9) / 4; + + if (PAT->entries) + g_array_free (PAT->entries, TRUE); + PAT->entries = + g_array_sized_new (FALSE, TRUE, sizeof (GstFluTSPATEntry), entries); + + while (entries--) { + GstFluTSPATEntry entry; + GstFluTSStream *PMT_stream; + + entry.program_number = GST_READ_UINT16_BE (data); + data += 2; + entry.PID = GST_READ_UINT16_BE (data); + entry.PID &= 0x1fff; + data += 2; + + /* get/create stream for PMT */ + PMT_stream = gst_fluts_demux_get_stream_for_PID (demux, entry.PID); + if (PMT_stream->PID_type != PID_TYPE_PROGRAM_MAP) { + /* set as program map */ + PMT_stream->PID_type = PID_TYPE_PROGRAM_MAP; + /* initialise section filter */ + gst_section_filter_init (&PMT_stream->section_filter); + } + + g_array_append_val (PAT->entries, entry); + + GST_DEBUG_OBJECT (demux, " PAT program: %d, PID 0x%04x", + entry.program_number, entry.PID); + } + CRC = GST_READ_UINT32_BE (data); + GST_DEBUG_OBJECT (demux, "PAT CRC: 0x%08x", CRC); + + /* PAT has been updated, signal the change */ + g_object_notify ((GObject *) (demux), "pat-info"); + + return TRUE; + + /* ERRORS */ +wrong_crc: + { + GST_DEBUG_OBJECT (demux, "wrong crc"); + return FALSE; + } +same_version: + { + GST_DEBUG_OBJECT (demux, "same version as existing PAT"); + return TRUE; + } +not_yet_applicable: + { + GST_DEBUG_OBJECT (demux, "Ignoring PAT with current_next_indicator = 0"); + return TRUE; + } +wrong_id: + { + GST_DEBUG_OBJECT (demux, "expected table_id == 0, got %02x", data[0]); + return FALSE; + } +wrong_sync: + { + GST_DEBUG_OBJECT (demux, "expected sync 10, got %02x", data[0]); + return FALSE; + } +wrong_seclen: + { + GST_DEBUG_OBJECT (demux, + "first two bits of section length must be 0, got %02x", data[0]); + return FALSE; + } +} + +static gboolean +gst_fluts_demux_is_PMT (GstFluTSDemux * demux, guint16 PID) +{ + GstFluTSStream *stream; + GstFluTSPAT *PAT; + gint i; + + /* get the PAT */ + stream = demux->streams[PID_PROGRAM_ASSOCIATION_TABLE]; + if (stream == NULL || stream->PAT.entries == NULL) + return FALSE; + + PAT = &stream->PAT; + + for (i = 0; i < PAT->entries->len; i++) { + GstFluTSPATEntry *entry; + + entry = &g_array_index (PAT->entries, GstFluTSPATEntry, i); + if (!entry) + continue; + + if (entry->PID == PID) + return TRUE; + } + return FALSE; +} + +/* + * transport_packet(){ + * sync_byte 8 bslbf == 0x47 + * transport_error_indicator 1 bslbf + * payload_unit_start_indicator 1 bslbf + * transport _priority 1 bslbf + * PID 13 uimsbf + * transport_scrambling_control 2 bslbf + * adaptation_field_control 2 bslbf + * continuity_counter 4 uimsbf + * if(adaptation_field_control=='10' || adaptation_field_control=='11'){ + * adaptation_field() + * } + * if(adaptation_field_control=='01' || adaptation_field_control=='11') { + * for (i=0;i<N;i++){ + * data_byte 8 bslbf + * } + * } + * } + */ +static FORCE_INLINE GstFlowReturn +gst_fluts_demux_parse_stream (GstFluTSDemux * demux, GstFluTSStream * stream, + const guint8 * in_data, guint in_size) +{ + GstFlowReturn ret; + gboolean transport_error_indicator; + gboolean payload_unit_start_indicator; + gboolean transport_priority; + guint16 PID; + guint8 transport_scrambling_control; + guint8 adaptation_field_control; + guint8 continuity_counter; + const guint8 *data = in_data; + guint datalen = in_size; + + transport_error_indicator = (data[0] & 0x80) == 0x80; + payload_unit_start_indicator = (data[0] & 0x40) == 0x40; + transport_priority = (data[0] & 0x20) == 0x20; + PID = stream->PID; + transport_scrambling_control = (data[2] & 0xc0) >> 6; + adaptation_field_control = (data[2] & 0x30) >> 4; + continuity_counter = data[2] & 0x0f; + + data += 3; + datalen -= 3; + + GST_DEBUG_OBJECT (demux, "afc 0x%x, pusi %d, PID 0x%04x datalen %u", + adaptation_field_control, payload_unit_start_indicator, PID, datalen); + + ret = GST_FLOW_OK; + + /* packets with adaptation_field_control == 0 must be skipped */ + if (adaptation_field_control == 0) + goto skip; + + /* parse adaption field if any */ + if (adaptation_field_control & 0x2) { + guint consumed; + + if (!gst_fluts_demux_parse_adaptation_field (stream, data, + datalen, &consumed)) + goto done; + + if (datalen <= consumed) + goto too_small; + + data += consumed; + datalen -= consumed; + GST_DEBUG_OBJECT (demux, "consumed: %u datalen: %u", consumed, datalen); + } + + /* If this packet has a payload, handle it */ + if (adaptation_field_control & 0x1) { + GST_DEBUG_OBJECT (demux, "Packet payload %d bytes, PID 0x%04x", datalen, + PID); + + /* For unknown streams, check if the PID is in the partial PIDs + * list as an elementary stream and override the type if so + */ + if (stream->PID_type == PID_TYPE_UNKNOWN) { + if (fluts_is_elem_pid (demux, PID)) { + GST_DEBUG_OBJECT (demux, + "PID 0x%04x is an elementary stream in the PID list", PID); + stream->PID_type = PID_TYPE_ELEMENTARY; + stream->flags |= FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; + stream->base_time = 0; + stream->last_time = 0; + + /* Clear any existing descriptor */ + if (stream->ES_info) { + gst_mpeg_descriptor_free (stream->ES_info); + stream->ES_info = NULL; + } + + /* Initialise our PES filter */ + gst_pes_filter_init (&stream->filter, NULL, NULL); + gst_pes_filter_set_callbacks (&stream->filter, + (GstPESFilterData) gst_fluts_demux_data_cb, + (GstPESFilterResync) gst_fluts_demux_resync_cb, stream); + } + } + + /* now parse based on the stream type */ + switch (stream->PID_type) { + case PID_TYPE_PROGRAM_ASSOCIATION: + case PID_TYPE_CONDITIONAL_ACCESS: + case PID_TYPE_PROGRAM_MAP: + case PID_TYPE_PRIVATE_SECTION: + { + GstBuffer *sec_buf; + guint8 *section_data; + guint16 section_length; + guint8 pointer; + + /* do stuff with our section */ + if (payload_unit_start_indicator) { + pointer = *data++; + datalen -= 1; + if (pointer >= datalen) { + GST_DEBUG_OBJECT (demux, "pointer: 0x%02x too large", pointer); + return GST_FLOW_OK; + } + data += pointer; + datalen -= pointer; + } + + /* FIXME: try to use data directly instead of creating a buffer and + pushing in into adapter at section filter */ + sec_buf = gst_buffer_new_and_alloc (datalen); +#ifdef USE_LIBOIL + oil_memcpy (GST_BUFFER_DATA (sec_buf), data, datalen); +#else + memcpy (GST_BUFFER_DATA (sec_buf), data, datalen); +#endif + if (gst_section_filter_push (&stream->section_filter, + payload_unit_start_indicator, continuity_counter, sec_buf)) { + GST_DEBUG_OBJECT (demux, "section finished"); + /* section ready */ + section_length = stream->section_filter.section_length; + section_data = + (guint8 *) gst_adapter_peek (stream->section_filter.adapter, + section_length + 3); + + switch (stream->PID_type) { + case PID_TYPE_PROGRAM_ASSOCIATION: + gst_fluts_stream_parse_pat (stream, section_data, + section_length + 3); + break; + case PID_TYPE_CONDITIONAL_ACCESS: + gst_fluts_stream_parse_cat (stream, section_data, + section_length + 3); + break; + case PID_TYPE_PROGRAM_MAP: + gst_fluts_stream_parse_pmt (stream, section_data, + section_length + 3); + break; + case PID_TYPE_PRIVATE_SECTION: + gst_fluts_stream_parse_private_section (stream, section_data, + section_length + 3); + break; + } + + gst_section_filter_clear (&stream->section_filter); + + } else { + /* section still going, don't parse left */ + GST_DEBUG_OBJECT (demux, "section still going for PID 0x%04x", PID); + } + break; + } + case PID_TYPE_NULL_PACKET: + GST_DEBUG_OBJECT (demux, + "skipping PID 0x%04x, type 0x%04x (NULL packet)", PID, + stream->PID_type); + break; + case PID_TYPE_UNKNOWN: + GST_DEBUG_OBJECT (demux, "skipping unknown PID 0x%04x, type 0x%04x", + PID, stream->PID_type); + break; + case PID_TYPE_ELEMENTARY: + { + GstBuffer *es_buf; + + if (payload_unit_start_indicator) { + GST_DEBUG_OBJECT (demux, "new PES start for PID 0x%04x", PID); + gst_pes_filter_drain (&stream->filter); + } + GST_LOG_OBJECT (demux, "Elementary packet of size %u for PID 0x%04x", + datalen, PID); + + if (datalen > 0) { + es_buf = gst_buffer_new_and_alloc (datalen); +#ifdef USE_LIBOIL + oil_memcpy (GST_BUFFER_DATA (es_buf), data, datalen); +#else + memcpy (GST_BUFFER_DATA (es_buf), data, datalen); +#endif + ret = gst_pes_filter_push (&stream->filter, es_buf); + break; + } else { + GST_WARNING_OBJECT (demux, "overflow of datalen: %u so skipping", + datalen); + return GST_FLOW_OK; + } + + } + } + } + +done: + return ret; + +skip: + { + GST_DEBUG_OBJECT (demux, "skipping, adaptation_field_control == 0"); + return GST_FLOW_OK; + } +too_small: + { + GST_DEBUG_OBJECT (demux, "skipping, adaptation_field consumed all data"); + return GST_FLOW_OK; + } +} + +static FORCE_INLINE GstFlowReturn +gst_fluts_demux_parse_transport_packet (GstFluTSDemux * demux, + const guint8 * data) +{ + GstFlowReturn ret; + guint16 PID; + GstFluTSStream *stream; + + /* skip sync byte */ + data++; + + /* get PID */ + PID = ((data[0] & 0x1f) << 8) | data[1]; + + /* get the stream. */ + stream = gst_fluts_demux_get_stream_for_PID (demux, PID); + + /* parse the stream */ + ret = gst_fluts_demux_parse_stream (demux, stream, data, + FLUTS_NORMAL_TS_PACKETSIZE - 1); + + return ret; + + /* ERRORS */ +} + +static gboolean +gst_fluts_demux_send_event (GstFluTSDemux * demux, GstEvent * event) +{ + gint i; + gboolean have_stream = FALSE, res = TRUE; + + for (i = 0; i < FLUTS_MAX_PID + 1; i++) { + GstFluTSStream *stream = demux->streams[i]; + + if (stream && stream->pad) { + res &= gst_pad_push_event (stream->pad, gst_event_ref (event)); + have_stream = TRUE; + } + } + gst_event_unref (event); + + return have_stream; +} + +static gboolean +gst_fluts_demux_sink_event (GstPad * pad, GstEvent * event) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (gst_pad_get_parent (pad)); + gboolean res; + + GST_DEBUG_OBJECT (demux, "got event %s", + gst_event_type_get_name (GST_EVENT_TYPE (event))); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_FLUSH_START: + res = gst_fluts_demux_send_event (demux, event); + break; + case GST_EVENT_FLUSH_STOP: + gst_adapter_clear (demux->adapter); + res = gst_fluts_demux_send_event (demux, event); + break; + case GST_EVENT_EOS: + if (!(res = gst_fluts_demux_send_event (demux, event))) { + /* we have no streams */ + GST_ELEMENT_ERROR (demux, STREAM, TYPE_NOT_FOUND, + (NULL), ("No valid streams found at EOS")); + } + break; + case GST_EVENT_NEWSEGMENT: + res = gst_fluts_demux_send_event (demux, event); + break; + default: + res = gst_fluts_demux_send_event (demux, event); + break; + } + gst_object_unref (demux); + + return TRUE; +} + +#ifdef HAVE_LATENCY +static gboolean +gst_fluts_demux_provides_clock (GstElement * element) +{ + GstFluTSDemux *demux; + GstQuery *query; + gboolean is_live = FALSE; + GstPad *peer; + + demux = GST_FLUTS_DEMUX (element); + query = gst_query_new_latency (); + peer = gst_pad_get_peer (demux->sinkpad); + + if (peer) { + if (gst_pad_query (peer, query)) + gst_query_parse_latency (query, &is_live, NULL, NULL); + gst_object_unref (peer); + } + gst_query_unref (query); + + return is_live; +} + +static GstClock * +gst_fluts_demux_provide_clock (GstElement * element) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (element); + + if (gst_fluts_demux_provides_clock (element)) { + if (demux->clock == NULL) { + demux->clock = g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", + "FluTSClock", NULL); + demux->clock_base = GST_CLOCK_TIME_NONE; + } + + return demux->clock; + } + + return NULL; +} + +static gboolean +gst_fluts_demux_src_pad_query (GstPad * pad, GstQuery * query) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (gst_pad_get_parent (pad)); + gboolean res = FALSE; + GstPad *peer; + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_LATENCY: + { + peer = gst_pad_get_peer (demux->sinkpad); + if (peer) { + res = gst_pad_query (peer, query); + if (res) { + gboolean is_live; + GstClockTime min_latency, max_latency; + + gst_query_parse_latency (query, &is_live, &min_latency, &max_latency); + if (is_live) { + min_latency += TS_LATENCY * GST_MSECOND; + if (max_latency != GST_CLOCK_TIME_NONE) + max_latency += TS_LATENCY * GST_MSECOND; + } + + gst_query_set_latency (query, is_live, min_latency, max_latency); + } + gst_object_unref (peer); + } + break; + } + default: + res = gst_pad_query_default (pad, query); + } + + return res; +} +#endif + +static FORCE_INLINE guint +gst_fluts_demux_sync_scan (GstFluTSDemux * demux, const guint8 * in_data, + guint size, guint * flush) +{ + guint sync_count = 0; + const guint8 *end_scan = in_data + size - demux->packetsize; + guint8 *ptr_data = (guint8 *) in_data; + + while (ptr_data <= end_scan && sync_count < LENGHT_SYNC_LUT) { + /* if sync code is found try to store it in the LUT */ + if (G_LIKELY (IS_MPEGTS_SYNC (ptr_data))) { + demux->sync_lut[sync_count] = ptr_data; + sync_count++; + /* skip paketsize bytes and try find next */ + ptr_data += demux->packetsize; + } else { + ptr_data++; + } + } + + *flush = ptr_data - in_data; + + return sync_count; +} + +static GstFlowReturn +gst_fluts_demux_chain (GstPad * pad, GstBuffer * buffer) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (gst_pad_get_parent (pad)); + GstFlowReturn ret = GST_FLOW_OK; + const guint8 *data; + guint avail; + guint flush = 0; + gint i; + guint sync_count; + + if (GST_BUFFER_IS_DISCONT (buffer)) + gst_adapter_clear (demux->adapter); + + /* first push the new buffer into the adapter */ + gst_adapter_push (demux->adapter, buffer); + + /* check if there's enough data to parse a packet */ + avail = gst_adapter_available (demux->adapter); + if (G_UNLIKELY (avail < demux->packetsize)) + goto done; + + /* recover all data from adapter */ + data = gst_adapter_peek (demux->adapter, avail); + + /* scan for sync codes */ + sync_count = gst_fluts_demux_sync_scan (demux, data, avail, &flush); + + /* process all packets */ + for (i = 0; i < sync_count; i++) { + ret = gst_fluts_demux_parse_transport_packet (demux, demux->sync_lut[i]); + if (G_UNLIKELY (ret != GST_FLOW_OK)) { + if (ret == GST_FLOW_LOST_SYNC) { + ret = GST_FLOW_OK; + continue; + } + flush = demux->sync_lut[i] - data + demux->packetsize; + goto done; + } + } + +done: + /* flush processed data */ + gst_adapter_flush (demux->adapter, flush); + + gst_object_unref (demux); + + return ret; +} + +static GstStateChangeReturn +gst_fluts_demux_change_state (GstElement * element, GstStateChange transition) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (element); + GstStateChangeReturn result; + + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + demux->adapter = gst_adapter_new (); + demux->sync_lut = g_new0 (guint8 *, LENGHT_SYNC_LUT); + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + break; + default: + break; + } + + result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + + switch (transition) { + case GST_STATE_CHANGE_PAUSED_TO_READY: + gst_fluts_demux_reset (demux); + break; + case GST_STATE_CHANGE_READY_TO_NULL: + g_object_unref (demux->adapter); + g_free (demux->sync_lut); + break; + default: + break; + } + + return result; +} + +static GValueArray * +fluts_demux_build_pat_info (GstFluTSDemux * demux) +{ + GValueArray *vals = NULL; + GstFluTSPAT *PAT; + gint i; + + g_return_val_if_fail (demux->streams[0] != NULL, NULL); + g_return_val_if_fail (demux->streams[0]->PID_type == + PID_TYPE_PROGRAM_ASSOCIATION, NULL); + + PAT = &(demux->streams[0]->PAT); + vals = g_value_array_new (PAT->entries->len); + + for (i = 0; i < PAT->entries->len; i++) { + GstFluTSPATEntry *cur_entry = + &g_array_index (PAT->entries, GstFluTSPATEntry, i); + GValue v = { 0, }; + FluTsPatInfo *info_obj; + + info_obj = fluts_pat_info_new (cur_entry->program_number, cur_entry->PID); + + g_value_init (&v, G_TYPE_OBJECT); + g_value_take_object (&v, info_obj); + g_value_array_append (vals, &v); + } + return vals; +} + +static FluTsPmtInfo * +fluts_demux_build_pmt_info (GstFluTSDemux * demux, guint16 pmt_pid) +{ + FluTsPmtInfo *info_obj; + GstFluTSPMT *PMT; + gint i; + + g_return_val_if_fail (demux->streams[pmt_pid] != NULL, NULL); + g_return_val_if_fail (demux->streams[pmt_pid]->PID_type == + PID_TYPE_PROGRAM_MAP, NULL); + + PMT = &(demux->streams[pmt_pid]->PMT); + + info_obj = fluts_pmt_info_new (PMT->program_number, PMT->PCR_PID, + PMT->version_number); + + for (i = 0; i < PMT->entries->len; i++) { + GstFluTSStream *stream; + FluTsPmtStreamInfo *stream_info; + GstFluTSPMTEntry *cur_entry = + &g_array_index (PMT->entries, GstFluTSPMTEntry, i); + + stream = demux->streams[cur_entry->PID]; + stream_info = + fluts_pmt_stream_info_new (cur_entry->PID, stream->stream_type); + + if (stream->ES_info) { + int i; + + /* add languages */ + guint8 *iso639_languages = + gst_mpeg_descriptor_find (stream->ES_info, DESC_ISO_639_LANGUAGE); + if (iso639_languages) { + for (i = 0; i < DESC_ISO_639_LANGUAGE_codes_n (iso639_languages); i++) { + gchar *language_n = (gchar *) + DESC_ISO_639_LANGUAGE_language_code_nth (iso639_languages, i); + fluts_pmt_stream_info_add_language (stream_info, + g_strndup (language_n, 3)); + } + } + + for (i = 0; i < gst_mpeg_descriptor_n_desc (stream->ES_info); ++i) { + guint8 *desc = gst_mpeg_descriptor_nth (stream->ES_info, i); + + /* add the whole descriptor, tag + length + DESC_LENGTH bytes */ + fluts_pmt_stream_info_add_descriptor (stream_info, + (gchar *) desc, 2 + DESC_LENGTH (desc)); + } + } + fluts_pmt_info_add_stream (info_obj, stream_info); + } + return info_obj; +} + +static void +gst_fluts_demux_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (object); + gchar **pids; + guint num_pids; + int i; + + switch (prop_id) { + case PROP_ES_PIDS: + pids = g_strsplit (g_value_get_string (value), ":", -1); + num_pids = g_strv_length (pids); + if (num_pids > 0) { + demux->elementary_pids = g_new0 (guint16, num_pids); + demux->nb_elementary_pids = num_pids; + for (i = 0; i < num_pids; i++) { + demux->elementary_pids[i] = strtol (pids[i], NULL, 0); + GST_INFO ("partial TS ES pid %d", demux->elementary_pids[i]); + } + } + g_strfreev (pids); + break; + case PROP_CHECK_CRC: + demux->check_crc = g_value_get_boolean (value); + break; + case PROP_PROGRAM_NUMBER: + demux->program_number = g_value_get_int (value); + break; + case PROP_M2TS: + demux->m2ts_mode = g_value_get_boolean (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_fluts_demux_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstFluTSDemux *demux = GST_FLUTS_DEMUX (object); + int i; + + switch (prop_id) { + case PROP_ES_PIDS: + if (demux->nb_elementary_pids == 0) { + g_value_set_static_string (value, ""); + } else { + gchar **ts_pids; + + ts_pids = g_new0 (gchar *, demux->nb_elementary_pids + 1); + for (i = 0; i < demux->nb_elementary_pids; i++) { + ts_pids[i] = g_strdup_printf ("%d", demux->elementary_pids[i]); + } + + g_value_set_string (value, g_strjoinv (":", ts_pids)); + g_strfreev (ts_pids); + } + break; + case PROP_CHECK_CRC: + g_value_set_boolean (value, demux->check_crc); + break; + case PROP_PROGRAM_NUMBER: + g_value_set_int (value, demux->program_number); + break; + case PROP_PAT_INFO: + { + if (demux->streams[0] != NULL) { + g_value_take_boxed (value, fluts_demux_build_pat_info (demux)); + } + break; + } + case PROP_PMT_INFO: + { + if (demux->current_PMT != 0 && demux->streams[demux->current_PMT] != NULL) { + g_value_take_object (value, fluts_demux_build_pmt_info (demux, + demux->current_PMT)); + } + break; + } + case PROP_M2TS: + g_value_set_boolean (value, demux->m2ts_mode); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +gboolean +gst_fluts_demux_plugin_init (GstPlugin * plugin) +{ + if (!gst_element_register (plugin, "flutsdemux", + GST_RANK_PRIMARY, GST_TYPE_FLUTS_DEMUX)) + return FALSE; + + return TRUE; +} diff --git a/gst/mpegdemux/gstmpegtsdemux.h b/gst/mpegdemux/gstmpegtsdemux.h new file mode 100644 index 00000000..b35fb031 --- /dev/null +++ b/gst/mpegdemux/gstmpegtsdemux.h @@ -0,0 +1,224 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifndef __GST_FLUTS_DEMUX_H__ +#define __GST_FLUTS_DEMUX_H__ + +#include <gst/gst.h> +#include <gst/base/gstadapter.h> + +#include "gstmpegdesc.h" +#include "gstpesfilter.h" +#include "gstsectionfilter.h" + +G_BEGIN_DECLS + +#if (POST_10_12) +#define HAVE_LATENCY +#endif + +#define FLUTS_MAX_PID 0x1fff +#define FLUTS_NORMAL_TS_PACKETSIZE 188 +#define FLUTS_M2TS_TS_PACKETSIZE 192 + +#define LENGHT_SYNC_LUT 256 + +#define IS_MPEGTS_SYNC(data) (((data)[0] == 0x47) && \ + (((data)[1] & 0x80) == 0x00) && \ + (((data)[3] & 0x10) == 0x10)) + +#define GST_TYPE_FLUTS_DEMUX (gst_fluts_demux_get_type()) +#define GST_FLUTS_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\ + GST_TYPE_FLUTS_DEMUX,GstFluTSDemux)) +#define GST_FLUTS_DEMUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),\ + GST_TYPE_FLUTS_DEMUX,GstFluTSDemuxClass)) +#define GST_FLUTS_DEMUX_GET_CLASS(klass) (G_TYPE_INSTANCE_GET_CLASS((klass),\ + GST_TYPE_FLUTS_DEMUX,GstFluTSDemuxClass)) +#define GST_IS_FLUTS_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),\ + GST_TYPE_FLUTS_DEMUX)) +#define GST_IS_FLUTS_DEMUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),\ + GST_TYPE_FLUTS_DEMUX)) + +typedef struct _GstFluTSStream GstFluTSStream; +typedef struct _GstFluTSPMTEntry GstFluTSPMTEntry; +typedef struct _GstFluTSPMT GstFluTSPMT; +typedef struct _GstFluTSPATEntry GstFluTSPATEntry; +typedef struct _GstFluTSPAT GstFluTSPAT; +typedef struct _GstFluTSDemux GstFluTSDemux; +typedef struct _GstFluTSDemuxClass GstFluTSDemuxClass; + +struct _GstFluTSPMTEntry { + guint16 PID; +}; + +struct _GstFluTSPMT { + guint16 program_number; + guint8 version_number; + gboolean current_next_indicator; + guint8 section_number; + guint8 last_section_number; + guint16 PCR_PID; + guint16 program_info_length; + GstMPEGDescriptor * program_info; + + GArray * entries; +}; + +struct _GstFluTSPATEntry { + guint16 program_number; + guint16 PID; +}; + +struct _GstFluTSPAT { + guint16 transport_stream_id; + guint8 version_number; + gboolean current_next_indicator; + guint8 section_number; + guint8 last_section_number; + + GArray * entries; +}; + +typedef enum _FluTsStreamFlags { + FLUTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN = 0x01, + FLUTS_STREAM_FLAG_PMT_VALID = 0x02, + FLUTS_STREAM_FLAG_IS_VIDEO = 0x04 +} FluTsStreamFlags; + +/* Information associated to a single MPEG stream. */ +struct _GstFluTSStream { + GstFluTSDemux * demux; + + FluTsStreamFlags flags; + + /* PID and type */ + guint16 PID; + guint8 PID_type; + + /* adaptation_field data */ + guint64 last_PCR; + guint64 base_PCR; + guint64 last_OPCR; + guint64 last_PCR_difference; + gboolean discont_PCR; + GstClockTimeDiff discont_difference; + + /* for PAT streams */ + GstFluTSPAT PAT; + + /* for PMT streams */ + GstFluTSPMT PMT; + + /* for CA streams */ + + /* for PAT, PMT, CA and private streams */ + GstSectionFilter section_filter; + + /* for PES streams */ + guint8 id; + guint8 stream_type; + GstPESFilter filter; + GstPad * pad; + GstFlowReturn last_ret; + GstMPEGDescriptor *ES_info; + /* needed because 33bit mpeg timestamps wrap around every (approx) 26.5 hrs */ + GstClockTimeDiff base_time; + GstClockTime last_time; + /* pid of PMT that this stream belongs to */ + guint16 PMT_pid; +}; + +struct _GstFluTSDemux { + GstElement parent; + + /* properties */ + gboolean check_crc; + + /* sink pad and adapter */ + GstPad * sinkpad; + GstAdapter * adapter; + guint8 ** sync_lut; + + /* current PMT PID */ + guint16 current_PMT; + + /* Array of FLUTS_MAX_PID + 1 stream entries */ + GstFluTSStream ** streams; + /* Array to perform pmts checks at gst_fluts_demux_parse_adaptation_field */ + gboolean pmts_checked[FLUTS_MAX_PID + 1]; + + /* Array of Elementary Stream pids for ts with PMT */ + guint16 * elementary_pids; + guint nb_elementary_pids; + + /* Program number to use */ + gint program_number; + + /* indicates that we need to close our pad group, because we've added + * at least one pad */ + gboolean need_no_more_pads; + guint16 packetsize; + gboolean m2ts_mode; +#ifdef HAVE_LATENCY + /* clocking */ + GstClock * clock; + GstClockTime clock_base; +#endif +}; + +struct _GstFluTSDemuxClass { + GstElementClass parent_class; + + GstPadTemplate * sink_template; + GstPadTemplate * video_template; + GstPadTemplate * audio_template; + GstPadTemplate * private_template; +}; + +GType gst_fluts_demux_get_type (void); + +gboolean gst_fluts_demux_plugin_init (GstPlugin *plugin); + +G_END_DECLS + +#endif /* __GST_FLUTS_DEMUX_H__ */ diff --git a/gst/mpegdemux/gstpesfilter.c b/gst/mpegdemux/gstpesfilter.c new file mode 100644 index 00000000..8a2ee284 --- /dev/null +++ b/gst/mpegdemux/gstpesfilter.c @@ -0,0 +1,645 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstmpegdefs.h" +#include "gstpesfilter.h" + +GST_DEBUG_CATEGORY (gstflupesfilter_debug); +#define GST_CAT_DEFAULT (gstflupesfilter_debug) + +static GstFlowReturn gst_pes_filter_data_push (GstPESFilter * filter, + gboolean first, GstBuffer * buffer); + +#define ADAPTER_OFFSET_FLUSH(_bytes_) if (filter->adapter_offset) *filter->adapter_offset = *filter->adapter_offset + (_bytes_) + +/* May pass null for adapter to have the filter create one */ +void +gst_pes_filter_init (GstPESFilter * filter, GstAdapter * adapter, + guint64 * adapter_offset) +{ + g_return_if_fail (filter != NULL); + + if (adapter != NULL) + g_object_ref (adapter); + else + adapter = gst_adapter_new (); + + filter->adapter = adapter; + filter->adapter_offset = adapter_offset; + filter->state = STATE_HEADER_PARSE; + filter->gather_pes = FALSE; + filter->allow_unbounded = FALSE; +} + +void +gst_pes_filter_uninit (GstPESFilter * filter) +{ + g_return_if_fail (filter != NULL); + + if (filter->adapter) + g_object_unref (filter->adapter); + filter->adapter = NULL; + filter->adapter_offset = NULL; +} + +void +gst_pes_filter_set_callbacks (GstPESFilter * filter, + GstPESFilterData data_cb, GstPESFilterResync resync_cb, gpointer user_data) +{ + g_return_if_fail (filter != NULL); + + filter->data_cb = data_cb; + filter->resync_cb = resync_cb; + filter->user_data = user_data; +} + +/* sync:4 == 00xx ! pts:3 ! 1 ! pts:15 ! 1 | pts:15 ! 1 */ +#define READ_TS(data, target, lost_sync_label) \ + if ((*data & 0x01) != 0x01) goto lost_sync_label; \ + target = ((guint64) (*data++ & 0x0E)) << 29; \ + target |= ((guint64) (*data++ )) << 22; \ + if ((*data & 0x01) != 0x01) goto lost_sync_label; \ + target |= ((guint64) (*data++ & 0xFE)) << 14; \ + target |= ((guint64) (*data++ )) << 7; \ + if ((*data & 0x01) != 0x01) goto lost_sync_label; \ + target |= ((guint64) (*data++ & 0xFE)) >> 1; + +static gboolean +gst_pes_filter_is_sync (guint32 sync) +{ + return ((sync & 0xfffffffc) == 0x000001bc) || + ((sync & 0xffffffe0) == 0x000001c0) || + ((sync & 0xfffffff0) == 0x000001f0) || + ((sync & 0xfffffff0) == 0x000001e0); +} + +static GstFlowReturn +gst_pes_filter_parse (GstPESFilter * filter) +{ + GstFlowReturn ret; + guint32 start_code; + + gboolean STD_buffer_bound_scale; + guint16 STD_buffer_size_bound; + const guint8 *data; + gint avail, datalen; + gboolean have_size = FALSE; + + /* read start code and length */ + if (!(data = gst_adapter_peek (filter->adapter, 6))) + goto need_more_data; + + /* get start code */ + start_code = GST_READ_UINT32_BE (data); + if (!gst_pes_filter_is_sync (start_code)) + goto lost_sync; + + filter->start_code = start_code; + filter->id = data[3]; + + /* skip start code */ + data += 4; + + /* start parsing length */ + filter->length = GST_READ_UINT16_BE (data); + + /* see how much is available */ + avail = gst_adapter_available (filter->adapter); + + GST_DEBUG ("id 0x%02x length %d, avail %d start code 0x%02x", filter->id, + filter->length, avail, filter->start_code); + + /* A data length of 0 indicates an unbounded packet in transport + * streams, but actually a 0 sized packet in program streams or + * for anything except video packets */ + + /* FIXME: Remove this hack that is checking start_code. Instead, we need + * a callback that a start_code has been collected, giving the caller a chance + * to set the allow_unbounded flag if they want */ + if (filter->length == 0 && + ((filter->start_code & 0xFFFFFFF0) == PACKET_VIDEO_START_CODE || + filter->allow_unbounded)) { + GST_DEBUG ("id 0x%02x, unbounded length", filter->id); + filter->unbounded_packet = TRUE; + } else { + filter->unbounded_packet = FALSE; + + if (filter->gather_pes && avail < filter->length + 6) { + GST_DEBUG ("id 0x%02x, bounded length %d, only have %d", + filter->id, filter->length + 6, avail); + goto need_more_data; + } + + /* if we need more data from now on, we lost sync */ + avail = MIN (avail, filter->length + 6); + } + + /* read more data, either the whole packet if there is a length + * or whatever we have available if this in an unbounded packet. */ + if (!(data = gst_adapter_peek (filter->adapter, avail))) + goto need_more_data; + + /* This will make us flag LOST_SYNC if we run out of data from here onward */ + have_size = TRUE; + + /* skip start code and length */ + data += 6; + datalen = avail - 6; + + GST_DEBUG ("datalen %d", datalen); + + switch (filter->start_code) { + case ID_PS_PROGRAM_STREAM_MAP: + case ID_PRIVATE_STREAM_2: + case ID_ECM_STREAM: + case ID_EMM_STREAM: + case ID_PROGRAM_STREAM_DIRECTORY: + case ID_DSMCC_STREAM: + case ID_ITU_TREC_H222_TYPE_E_STREAM: + goto skip; + case ID_PADDING_STREAM: + GST_DEBUG ("skipping padding stream"); + goto skip; + default: + break; + } + + if (datalen < 1) + goto need_more_data; + + filter->pts = filter->dts = -1; + + /* stuffing bits, first two bits are '10' for mpeg2 pes so this code is + * not triggered. */ + while (TRUE) { + if (*data != 0xff) + break; + + data++; + datalen--; + + GST_DEBUG ("got stuffing bit"); + + if (datalen < 1) + goto need_more_data; + } + + /* STD buffer size, never for mpeg2 */ + if ((*data & 0xc0) == 0x40) { + GST_DEBUG ("have STD"); + + if (datalen < 3) + goto need_more_data; + + STD_buffer_bound_scale = *data & 0x20; + STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8; + STD_buffer_size_bound |= *data++; + + datalen -= 2; + } + + /* PTS but no DTS, never for mpeg2 */ + if ((*data & 0xf0) == 0x20) { + GST_DEBUG ("PTS without DTS"); + + if (datalen < 5) + goto need_more_data; + READ_TS (data, filter->pts, lost_sync); + GST_DEBUG ("PTS found %" G_GUINT64_FORMAT, filter->pts); + datalen -= 5; + } + /* PTS and DTS, never for mpeg2 */ + else if ((*data & 0xf0) == 0x30) { + GST_DEBUG ("PTS and DTS"); + + if (datalen < 10) + goto need_more_data; + READ_TS (data, filter->pts, lost_sync); + READ_TS (data, filter->dts, lost_sync); + GST_DEBUG ("PTS found %" G_GUINT64_FORMAT, filter->pts); + GST_DEBUG ("DTS found %" G_GUINT64_FORMAT, filter->dts); + datalen -= 10; + } else if ((*data & 0xc0) == 0x80) { + /* mpeg2 case */ + guchar flags; + guint8 header_data_length = 0; + + GST_DEBUG ("MPEG2 PES packet"); + + if (datalen < 3) + goto need_more_data; + /* 2: '10' + * 2: PES_scrambling_control + * 1: PES_priority + * 1: data_alignment_indicator + * 1: copyright + * 1: original_or_copy + */ + flags = *data++; + + GST_DEBUG ("flags: 0x%02x", flags); + if ((flags & 0xc0) != 0x80) + goto lost_sync; + + /* check PES scrambling control */ + if ((flags & 0x30) != 0) + goto encrypted; + + /* 2: PTS_DTS_flags + * 1: ESCR_flag + * 1: ES_rate_flag + * 1: DSM_trick_mode_flag + * 1: additional_copy_info_flag + * 1: PES_CRC_flag + * 1: PES_extension_flag + */ + flags = *data++; + + /* 8: PES_header_data_length */ + header_data_length = *data++; + datalen -= 3; + + GST_DEBUG ("header_data_length: %d, flags 0x%02x", + header_data_length, flags); + + if (header_data_length > datalen) + goto need_more_data; + + /* only DTS: this is invalid */ + if ((flags & 0xc0) == 0x40) + goto lost_sync; + + /* check for PTS */ + if ((flags & 0x80)) { + if (datalen < 5) + goto need_more_data; + + READ_TS (data, filter->pts, lost_sync); + GST_DEBUG ("PTS found %" G_GUINT64_FORMAT, filter->pts); + header_data_length -= 5; + datalen -= 5; + } + /* check for DTS */ + if ((flags & 0x40)) { + READ_TS (data, filter->dts, lost_sync); + if (datalen < 5) + goto need_more_data; + GST_DEBUG ("DTS found %" G_GUINT64_FORMAT, filter->dts); + header_data_length -= 5; + datalen -= 5; + } + /* ESCR_flag */ + if ((flags & 0x20)) { + GST_DEBUG ("%x ESCR found", filter->id); + if (datalen < 6) + goto need_more_data; + data += 6; + header_data_length -= 6; + datalen -= 6; + } + /* ES_rate_flag */ + if ((flags & 0x10)) { + guint32 es_rate; + + if (datalen < 3) + goto need_more_data; + + es_rate = ((guint32) (*data++ & 0x07)) << 14; + es_rate |= ((guint32) (*data++)) << 7; + es_rate |= ((guint32) (*data++ & 0xFE)) >> 1; + GST_DEBUG ("%x ES Rate found %u", filter->id, es_rate); + header_data_length -= 3; + datalen -= 3; + } + /* DSM_trick_mode_flag */ + if ((flags & 0x08)) { + guint8 trick_mode_flags; + + if (datalen < 1) + goto need_more_data; + + /* 3: trick_mode_control */ + trick_mode_flags = *data++; + GST_DEBUG ("%x DSM trick mode found, flags 0x%02x", filter->id, + trick_mode_flags); + + /* fast_forward */ + if ((trick_mode_flags & 0xe0) == 0x00) { + } + /* slow motion */ + else if ((trick_mode_flags & 0xe0) == 0x20) { + } + /* freeze frame */ + else if ((trick_mode_flags & 0xe0) == 0x40) { + } + /* fast reverse */ + else if ((trick_mode_flags & 0xe0) == 0x60) { + } + /* slow reverse */ + else if ((trick_mode_flags & 0xe0) == 0x80) { + } + /* reserved */ + else { + } + + header_data_length -= 1; + datalen -= 1; + } + /* additional_copy_info_flag */ + if ((flags & 0x04)) { + GST_DEBUG ("%x additional copy info, flags 0x%02x", filter->id, *data); + } + /* PES_CRC_flag */ + if ((flags & 0x02)) { + GST_DEBUG ("%x PES_CRC", filter->id); + } + /* PES_extension_flag */ + if ((flags & 0x01)) { + GST_DEBUG ("%x PES_extension", filter->id); + } + + /* calculate the amount of real data in this PES packet */ + data += header_data_length; + datalen -= header_data_length; + } else if (*data == 0x0f) { + /* Not sure what this clause is for */ + data++; + datalen--; + } else { + /* Data byte wasn't recognised as a flags byte */ + GST_DEBUG ("Unrecognised flags byte 0x%02x\n", *data); + goto lost_sync; + } + + { + GstBuffer *out; + guint16 consumed; + + consumed = avail - 6 - datalen; + + if (filter->unbounded_packet == FALSE) { + filter->length -= avail - 6; + GST_DEBUG ("pushing %d, need %d more, consumed %d", + datalen, filter->length, consumed); + } else { + GST_DEBUG ("pushing %d, unbounded packet, consumed %d", + datalen, consumed); + } + + if (datalen > 0) { + out = gst_buffer_new (); + GST_BUFFER_DATA (out) = g_memdup (data, datalen); + GST_BUFFER_SIZE (out) = datalen; + GST_BUFFER_MALLOCDATA (out) = GST_BUFFER_DATA (out); + + ret = gst_pes_filter_data_push (filter, TRUE, out); + filter->first = FALSE; + } else { + GST_LOG ("first being set to TRUE"); + filter->first = TRUE; + ret = GST_FLOW_OK; + } + + if (filter->length > 0 || filter->unbounded_packet) + filter->state = STATE_DATA_PUSH; + } + + gst_adapter_flush (filter->adapter, avail); + ADAPTER_OFFSET_FLUSH (avail); + + return ret; + +need_more_data: + { + if (filter->unbounded_packet == FALSE) { + if (have_size == TRUE) { + GST_DEBUG ("bounded need more data %d, lost sync", + gst_adapter_available (filter->adapter)); + ret = GST_FLOW_LOST_SYNC; + } else { + GST_DEBUG ("bounded need more data %d, breaking for more", + gst_adapter_available (filter->adapter)); + ret = GST_FLOW_NEED_MORE_DATA; + } + } else { + GST_DEBUG ("unbounded need more data %d", + gst_adapter_available (filter->adapter)); + ret = GST_FLOW_NEED_MORE_DATA; + } + + return ret; + } +skip: + { + GST_DEBUG ("skipping 0x%02x", filter->id); + gst_adapter_flush (filter->adapter, avail); + ADAPTER_OFFSET_FLUSH (avail); + + filter->length -= avail - 6; + if (filter->length > 0 || filter->unbounded_packet) + filter->state = STATE_DATA_SKIP; + return GST_FLOW_OK; + } +encrypted: + { + GST_DEBUG ("skipping encrypted 0x%02x", filter->id); + gst_adapter_flush (filter->adapter, avail); + ADAPTER_OFFSET_FLUSH (avail); + + filter->length -= avail - 6; + if (filter->length > 0 || filter->unbounded_packet) + filter->state = STATE_DATA_SKIP; + return GST_FLOW_OK; + } +lost_sync: + { + GST_DEBUG ("lost sync"); + gst_adapter_flush (filter->adapter, 4); + ADAPTER_OFFSET_FLUSH (4); + + return GST_FLOW_LOST_SYNC; + } +} + +static GstFlowReturn +gst_pes_filter_data_push (GstPESFilter * filter, gboolean first, + GstBuffer * buffer) +{ + GstFlowReturn ret; + + GST_LOG ("pushing, first: %d", first); + + if (filter->data_cb) { + ret = filter->data_cb (filter, first, buffer, filter->user_data); + } else { + gst_buffer_unref (buffer); + ret = GST_FLOW_OK; + } + return ret; +} + +GstFlowReturn +gst_pes_filter_push (GstPESFilter * filter, GstBuffer * buffer) +{ + GstFlowReturn ret; + + g_return_val_if_fail (filter != NULL, GST_FLOW_ERROR); + g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR); + + switch (filter->state) { + case STATE_HEADER_PARSE: + gst_adapter_push (filter->adapter, buffer); + ret = gst_pes_filter_parse (filter); + break; + case STATE_DATA_PUSH: + ret = gst_pes_filter_data_push (filter, filter->first, buffer); + filter->first = FALSE; + break; + case STATE_DATA_SKIP: + gst_buffer_unref (buffer); + ret = GST_FLOW_OK; + break; + default: + goto wrong_state; + } + return ret; + + /* ERROR */ +wrong_state: + { + GST_DEBUG ("wrong internal state %d", filter->state); + return GST_FLOW_ERROR; + } +} + +GstFlowReturn +gst_pes_filter_process (GstPESFilter * filter) +{ + GstFlowReturn ret; + gboolean skip = FALSE; + + g_return_val_if_fail (filter != NULL, GST_FLOW_ERROR); + + switch (filter->state) { + case STATE_HEADER_PARSE: + ret = gst_pes_filter_parse (filter); + break; + case STATE_DATA_SKIP: + skip = TRUE; + /* fallthrough */ + case STATE_DATA_PUSH: + if (filter->length > 0 || filter->unbounded_packet) { + gint avail; + + avail = gst_adapter_available (filter->adapter); + if (filter->unbounded_packet == FALSE) + avail = MIN (avail, filter->length); + + if (skip) { + gst_adapter_flush (filter->adapter, avail); + ADAPTER_OFFSET_FLUSH (avail); + ret = GST_FLOW_OK; + } else { + GstBuffer *out; + guint8 *data; + + data = gst_adapter_take (filter->adapter, avail); + + out = gst_buffer_new (); + GST_BUFFER_DATA (out) = data; + GST_BUFFER_SIZE (out) = avail; + GST_BUFFER_MALLOCDATA (out) = data; + + ret = gst_pes_filter_data_push (filter, filter->first, out); + filter->first = FALSE; + } + + if (filter->unbounded_packet == FALSE) { + filter->length -= avail; + if (filter->length == 0) + filter->state = STATE_HEADER_PARSE; + } + } else { + filter->state = STATE_HEADER_PARSE; + ret = GST_FLOW_OK; + } + break; + default: + goto wrong_state; + } + return ret; + + /* ERROR */ +wrong_state: + { + GST_DEBUG ("wrong internal state %d", filter->state); + return GST_FLOW_ERROR; + } +} + +void +gst_pes_filter_flush (GstPESFilter * filter) +{ + g_return_if_fail (filter != NULL); + + if (filter->adapter) { + gst_adapter_clear (filter->adapter); + if (filter->adapter_offset) + *filter->adapter_offset = G_MAXUINT64; + } + filter->state = STATE_HEADER_PARSE; +} + +GstFlowReturn +gst_pes_filter_drain (GstPESFilter * filter) +{ + g_return_val_if_fail (filter != NULL, GST_FLOW_ERROR); + + gst_pes_filter_flush (filter); + + return GST_FLOW_OK; +} diff --git a/gst/mpegdemux/gstpesfilter.h b/gst/mpegdemux/gstpesfilter.h new file mode 100644 index 00000000..b35d8746 --- /dev/null +++ b/gst/mpegdemux/gstpesfilter.h @@ -0,0 +1,110 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifndef __GST_PES_FILTER_H__ +#define __GST_PES_FILTER_H__ + +#include <gst/gst.h> +#include <gst/base/gstadapter.h> + +G_BEGIN_DECLS + +typedef struct _GstPESFilter GstPESFilter; + +typedef GstFlowReturn (*GstPESFilterData) (GstPESFilter * filter, gboolean first, GstBuffer * buffer, + gpointer user_data); +typedef void (*GstPESFilterResync) (GstPESFilter * filter, gpointer user_data); +typedef void (*GstPESFilterIndex) (GstPESFilter * filter, gpointer user_data); + +typedef enum { + STATE_HEADER_PARSE, + STATE_DATA_PUSH, + STATE_DATA_SKIP +} GstPESFilterState; + +struct _GstPESFilter { + GstAdapter * adapter; + guint64 * adapter_offset; + + GstPESFilterState state; + /* Whether to collect entire PES packets before + * outputting */ + gboolean gather_pes; + /* Whether unbounded packets are allowed in this + * stream */ + gboolean allow_unbounded; + + gboolean first; + GstPESFilterData data_cb; + GstPESFilterResync resync_cb; + gpointer user_data; + + guint32 start_code; + guint8 id; + gboolean unbounded_packet; + guint16 length; + + guint8 type; + + gint64 pts; + gint64 dts; +}; + +void gst_pes_filter_init (GstPESFilter * filter, GstAdapter * adapter, guint64 * adapter_offset); + +void gst_pes_filter_uninit (GstPESFilter * filter); + +void gst_pes_filter_set_callbacks (GstPESFilter * filter, + GstPESFilterData data_cb, + GstPESFilterResync resync_cb, + gpointer user_data); + +GstFlowReturn gst_pes_filter_push (GstPESFilter * filter, GstBuffer * buffer); +GstFlowReturn gst_pes_filter_process (GstPESFilter * filter); + +void gst_pes_filter_flush (GstPESFilter * filter); +GstFlowReturn gst_pes_filter_drain (GstPESFilter * filter); + +G_END_DECLS + +#endif /* __GST_PES_FILTER_H__ */ diff --git a/gst/mpegdemux/gstsectionfilter.c b/gst/mpegdemux/gstsectionfilter.c new file mode 100644 index 00000000..7749c7e7 --- /dev/null +++ b/gst/mpegdemux/gstsectionfilter.c @@ -0,0 +1,161 @@ + /* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstsectionfilter.h" + +#ifndef __always_inline +#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) +#define __always_inline inline __attribute__((always_inline)) +#else +#define __always_inline inline +#endif +#endif + +#ifndef DISABLE_INLINE +#define FORCE_INLINE __always_inline +#else +#define FORCE_INLINE +#endif + +GST_DEBUG_CATEGORY (gstflusectionfilter_debug); +#define GST_CAT_DEFAULT (gstflusectionfilter_debug) + +void +gst_section_filter_init (GstSectionFilter * filter) +{ + g_return_if_fail (filter != NULL); + filter->adapter = gst_adapter_new (); + /* continuity counter can at max be 15 + * we make 255 as an indication that + * there is no last continuity counter */ + filter->last_continuity_counter = 255; + filter->section_length = G_MAXUINT16; +} + +void +gst_section_filter_uninit (GstSectionFilter * filter) +{ + g_return_if_fail (filter != NULL); + if (filter->adapter) + g_object_unref (filter->adapter); + filter->adapter = NULL; +} + +void +gst_section_filter_clear (GstSectionFilter * filter) +{ + g_return_if_fail (filter != NULL); + if (filter->adapter) { + gst_adapter_clear (filter->adapter); + filter->last_continuity_counter = 255; + filter->section_length = G_MAXUINT16; + } +} + +static FORCE_INLINE gboolean +gst_section_is_complete (GstSectionFilter * filter) +{ + /* section length measures size of section from 3 bytes into section + * (ie after section length field finished) until end of section) + */ + guint avail_bytes = gst_adapter_available (filter->adapter); + if (filter->section_length == avail_bytes - 3) { + return TRUE; + } else if (filter->section_length < (int) (avail_bytes - 3)) { + GST_DEBUG ("section length seems to be less than available bytes for " + "rest of section."); + return TRUE; + } + return FALSE; +} + +/* returns True when section finished and ready to parse */ +/* FIXME: especially for multi-section tables, we need to handle pusi correctly + * and handle cases where a new section starts in the same transport packet. + */ +gboolean +gst_section_filter_push (GstSectionFilter * filter, gboolean pusi, /* determines whether start or not */ + guint8 continuity_counter, GstBuffer * buf) +{ + g_return_val_if_fail (filter != NULL, FALSE); + + /* check if it's the first packet of a section or + * if it continues the section */ + if (pusi) { + const guint8 *data = GST_BUFFER_DATA (buf); + if (filter->last_continuity_counter != 255) { + GST_WARNING ("section lost, last continuity counter: %d" + "we now have a pusi at continuity counter: %d", + filter->last_continuity_counter, continuity_counter); + gst_section_filter_clear (filter); + } + filter->section_length = GST_READ_UINT16_BE (data + 1); + filter->section_length &= 0x0fff; + if (filter->section_length > 1021) { + GST_DEBUG ("section length too big"); + return FALSE; + } + gst_adapter_push (filter->adapter, buf); + filter->last_continuity_counter = continuity_counter; + return gst_section_is_complete (filter); + } else if (filter->last_continuity_counter == continuity_counter - 1 || + (filter->last_continuity_counter == 15 && continuity_counter == 0)) { + GST_DEBUG ("section still going, no pusi"); + gst_adapter_push (filter->adapter, buf); + filter->last_continuity_counter = continuity_counter; + return gst_section_is_complete (filter); + } + /* we have lost the section and we are not a start + * section, so clear what was in it */ + else { + GST_WARNING ("section lost, last continuity counter: %d" + "new continuity counter but not pusi: %d", + filter->last_continuity_counter, continuity_counter); + gst_section_filter_clear (filter); + return FALSE; + } +} diff --git a/gst/mpegdemux/gstsectionfilter.h b/gst/mpegdemux/gstsectionfilter.h new file mode 100644 index 00000000..e26e3b28 --- /dev/null +++ b/gst/mpegdemux/gstsectionfilter.h @@ -0,0 +1,69 @@ +/* + * This library is licensed under 2 different licenses and you + * can choose to use it under the terms of either one of them. The + * two licenses are the MPL 1.1 and the LGPL. + * + * 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. + * + * 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): Wim Taymans <wim@fluendo.com> + */ + +#ifndef __GST_SECTION_FILTER_H__ +#define __GST_SECTION_FILTER_H__ + +#include <gst/gst.h> +#include <gst/base/gstadapter.h> + +G_BEGIN_DECLS + +typedef struct _GstSectionFilter GstSectionFilter; + +struct _GstSectionFilter { + GstAdapter *adapter; + guint8 last_continuity_counter; + guint16 section_length; +}; + +void gst_section_filter_init (GstSectionFilter *filter); +void gst_section_filter_uninit (GstSectionFilter *filter); +gboolean gst_section_filter_push (GstSectionFilter *filter, + gboolean pusi, + guint8 continuity_counter, + GstBuffer *buf); +void gst_section_filter_clear (GstSectionFilter *filter); +G_END_DECLS + +#endif /* __GST_SECTION_FILTER_H__ */ |