From 61bad24f8139ffe1111eed970a98928ce3b1db05 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim-Philipp=20M=C3=BCller?= Date: Sat, 16 Jun 2012 16:30:17 +0100 Subject: [PATCH] mpegdemux: remove unused TS demuxer bits that have moved to tsdemux --- gst/mpegdemux/Makefile.am | 18 +- gst/mpegdemux/flumpegdemux.c | 19 +- gst/mpegdemux/flutspatinfo.c | 139 -- gst/mpegdemux/flutspatinfo.h | 72 - gst/mpegdemux/flutspmtinfo.c | 227 --- gst/mpegdemux/flutspmtinfo.h | 81 - gst/mpegdemux/flutspmtstreaminfo.c | 212 --- gst/mpegdemux/flutspmtstreaminfo.h | 80 - gst/mpegdemux/gstmpegdesc.c | 209 --- gst/mpegdemux/gstmpegdesc.h | 342 ---- gst/mpegdemux/gstmpegtsdemux.c | 3558 ------------------------------------ gst/mpegdemux/gstmpegtsdemux.h | 257 --- gst/mpegdemux/gstsectionfilter.c | 164 -- gst/mpegdemux/gstsectionfilter.h | 69 - gst/mpegdemux/mpegtspacketizer.c | 2612 -------------------------- gst/mpegdemux/mpegtspacketizer.h | 156 -- gst/mpegdemux/mpegtsparse.c | 1472 --------------- gst/mpegdemux/mpegtsparse.h | 85 - 18 files changed, 4 insertions(+), 9768 deletions(-) delete mode 100644 gst/mpegdemux/flutspatinfo.c delete mode 100644 gst/mpegdemux/flutspatinfo.h delete mode 100644 gst/mpegdemux/flutspmtinfo.c delete mode 100644 gst/mpegdemux/flutspmtinfo.h delete mode 100644 gst/mpegdemux/flutspmtstreaminfo.c delete mode 100644 gst/mpegdemux/flutspmtstreaminfo.h delete mode 100644 gst/mpegdemux/gstmpegdesc.c delete mode 100644 gst/mpegdemux/gstmpegdesc.h delete mode 100644 gst/mpegdemux/gstmpegtsdemux.c delete mode 100644 gst/mpegdemux/gstmpegtsdemux.h delete mode 100644 gst/mpegdemux/gstsectionfilter.c delete mode 100644 gst/mpegdemux/gstsectionfilter.h delete mode 100644 gst/mpegdemux/mpegtspacketizer.c delete mode 100644 gst/mpegdemux/mpegtspacketizer.h delete mode 100644 gst/mpegdemux/mpegtsparse.c delete mode 100644 gst/mpegdemux/mpegtsparse.h diff --git a/gst/mpegdemux/Makefile.am b/gst/mpegdemux/Makefile.am index d1372fb..34706db 100644 --- a/gst/mpegdemux/Makefile.am +++ b/gst/mpegdemux/Makefile.am @@ -4,14 +4,6 @@ libgstmpegdemux_la_SOURCES = \ flumpegdemux.c \ gstmpegdemux.c \ gstpesfilter.c -# flutspatinfo.c -# flutspmtinfo.c -# flutspmtstreaminfo.c -# gstmpegdesc.c \ -# gstmpegtsdemux.c \ -# gstsectionfilter.c \ -# mpegtsparse.c \ -# mpegtspacketizer.c libgstmpegdemux_la_CFLAGS = \ $(GST_PLUGINS_BASE_CFLAGS) \ @@ -24,16 +16,8 @@ libgstmpegdemux_la_LIBTOOLFLAGS = --tag=disable-static noinst_HEADERS = \ gstmpegdefs.h \ - gstmpegdesc.h \ gstmpegdemux.h \ - gstpesfilter.h \ - gstmpegtsdemux.h \ - flutspatinfo.h \ - flutspmtinfo.h \ - flutspmtstreaminfo.h \ - gstsectionfilter.h \ - mpegtspacketizer.h \ - mpegtsparse.h + gstpesfilter.h Android.mk: Makefile.am $(BUILT_SOURCES) androgenizer \ diff --git a/gst/mpegdemux/flumpegdemux.c b/gst/mpegdemux/flumpegdemux.c index ae3d734..38a3520 100644 --- a/gst/mpegdemux/flumpegdemux.c +++ b/gst/mpegdemux/flumpegdemux.c @@ -46,31 +46,18 @@ #endif #include "gstmpegdemux.h" -#include "gstmpegtsdemux.h" -#include "mpegtsparse.h" -#include "mpegtspacketizer.h" GST_DEBUG_CATEGORY_EXTERN (gstflupesfilter_debug); -#if 0 -GST_DEBUG_CATEGORY_EXTERN (gstflusectionfilter_debug); -#endif + static gboolean plugin_init (GstPlugin * plugin) { GST_DEBUG_CATEGORY_INIT (gstflupesfilter_debug, "mpegpesfilter", 0, "MPEG-TS/PS PES filter output"); -#if 0 - GST_DEBUG_CATEGORY_INIT (gstflusectionfilter_debug, "mpegsectionfilter", 0, - "MPEG-TS Section filter output"); -#endif + if (!gst_flups_demux_plugin_init (plugin)) return FALSE; -#if 0 - if (!gst_mpegts_demux_plugin_init (plugin)) - return FALSE; - if (!gst_mpegtsparse_plugin_init (plugin)) - return FALSE; -#endif + return TRUE; } diff --git a/gst/mpegdemux/flutspatinfo.c b/gst/mpegdemux/flutspatinfo.c deleted file mode 100644 index 12b93af..0000000 --- a/gst/mpegdemux/flutspatinfo.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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 - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -#include "flutspatinfo.h" - -enum -{ - PROP_0, - PROP_PROGRAM_NO, - PROP_PID -}; - -static void mpegts_pat_info_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * spec); -static void mpegts_pat_info_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * spec); - -GST_BOILERPLATE (MpegTsPatInfo, mpegts_pat_info, GObject, G_TYPE_OBJECT); - -MpegTsPatInfo * -mpegts_pat_info_new (guint16 program_no, guint16 pid) -{ - MpegTsPatInfo *info; - - info = g_object_new (MPEGTS_TYPE_PAT_INFO, NULL); - - info->program_no = program_no; - info->pid = pid; - - return info; -} - -static void -mpegts_pat_info_base_init (gpointer klass) -{ -} - -static void -mpegts_pat_info_class_init (MpegTsPatInfoClass * klass) -{ - GObjectClass *gobject_klass = (GObjectClass *) klass; - - gobject_klass->set_property = mpegts_pat_info_set_property; - gobject_klass->get_property = mpegts_pat_info_get_property; - - g_object_class_install_property (gobject_klass, PROP_PROGRAM_NO, - g_param_spec_uint ("program-number", "Program Number", - "Program Number for this program", 0, G_MAXUINT16, 1, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - - 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 | G_PARAM_STATIC_STRINGS)); -} - -static void -mpegts_pat_info_init (MpegTsPatInfo * pat_info, MpegTsPatInfoClass * klass) -{ -} - -static void -mpegts_pat_info_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * spec) -{ - g_return_if_fail (MPEGTS_IS_PAT_INFO (object)); - - /* No settable properties */ - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); -} - -static void -mpegts_pat_info_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * spec) -{ - MpegTsPatInfo *pat_info; - - g_return_if_fail (MPEGTS_IS_PAT_INFO (object)); - - pat_info = MPEGTS_PAT_INFO (object); - - switch (prop_id) { - case PROP_PROGRAM_NO: - g_value_set_uint (value, pat_info->program_no); - break; - case PROP_PID: - g_value_set_uint (value, pat_info->pid); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); - break; - } -} diff --git a/gst/mpegdemux/flutspatinfo.h b/gst/mpegdemux/flutspatinfo.h deleted file mode 100644 index 565454c..0000000 --- a/gst/mpegdemux/flutspatinfo.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 - */ - -#ifndef __FLUTS_PAT_INFO_H__ -#define __FLUTS_PAT_INFO_H__ - -#include - -G_BEGIN_DECLS - -typedef struct FluTsPatInfoClass { - GObjectClass parent_class; -} MpegTsPatInfoClass; - -typedef struct FluTsPatInfo { - GObject parent; - - guint16 pid; - guint16 program_no; -} MpegTsPatInfo; - -#define MPEGTS_TYPE_PAT_INFO (mpegts_pat_info_get_type ()) -#define MPEGTS_IS_PAT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MPEGTS_TYPE_PAT_INFO)) -#define MPEGTS_PAT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MPEGTS_TYPE_PAT_INFO, MpegTsPatInfo)) - -GType mpegts_pat_info_get_type (void); - -MpegTsPatInfo *mpegts_pat_info_new (guint16 program_no, guint16 pid); - -G_END_DECLS - -#endif diff --git a/gst/mpegdemux/flutspmtinfo.c b/gst/mpegdemux/flutspmtinfo.c deleted file mode 100644 index 5b25e25..0000000 --- a/gst/mpegdemux/flutspmtinfo.c +++ /dev/null @@ -1,227 +0,0 @@ - /* - * 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 - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex - * with newer GLib versions (>= 2.31.0) */ -#define GLIB_DISABLE_DEPRECATION_WARNINGS - -#include - -#include "flutspmtinfo.h" - -enum -{ - PROP_0, - PROP_PROGRAM_NO, - PROP_VERSION_NO, - PROP_PCR_PID, - PROP_DESCRIPTORS, - PROP_STREAMINFO -}; - -GST_BOILERPLATE (MpegTsPmtInfo, mpegts_pmt_info, GObject, G_TYPE_OBJECT); - -static void mpegts_pmt_info_finalize (GObject * object); -static void mpegts_pmt_info_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * spec); -static void mpegts_pmt_info_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * spec); - -static void -mpegts_pmt_info_base_init (gpointer klass) -{ -} - -static void -mpegts_pmt_info_class_init (MpegTsPmtInfoClass * klass) -{ - GObjectClass *gobject_klass = (GObjectClass *) klass; - - gobject_klass->finalize = mpegts_pmt_info_finalize; - gobject_klass->set_property = mpegts_pmt_info_set_property; - gobject_klass->get_property = mpegts_pmt_info_get_property; - - g_object_class_install_property (gobject_klass, PROP_PROGRAM_NO, - g_param_spec_uint ("program-number", "Program Number", - "Program Number for this program", 0, G_MAXUINT16, 1, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - - 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_PARAM_STATIC_STRINGS)); - - g_object_class_install_property (gobject_klass, PROP_STREAMINFO, - g_param_spec_value_array ("stream-info", - "GValueArray containing GObjects with properties", - "Array of GObjects containing information about the program streams", - g_param_spec_object ("flu-pmt-streaminfo", "FluPMTStreamInfo", - "Fluendo TS Demuxer PMT Stream info object", - MPEGTS_TYPE_PMT_STREAM_INFO, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS), - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - - 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_PARAM_STATIC_STRINGS)); - - 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_STATIC_STRINGS), - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); -} - -static void -mpegts_pmt_info_init (MpegTsPmtInfo * pmt_info, MpegTsPmtInfoClass * klass) -{ - pmt_info->streams = g_value_array_new (0); - pmt_info->descriptors = g_value_array_new (0); -} - -MpegTsPmtInfo * -mpegts_pmt_info_new (guint16 program_no, guint16 pcr_pid, guint8 version_no) -{ - MpegTsPmtInfo *info; - - info = g_object_new (MPEGTS_TYPE_PMT_INFO, NULL); - - info->program_no = program_no; - info->pcr_pid = pcr_pid; - info->version_no = version_no; - - return info; -} - -static void -mpegts_pmt_info_finalize (GObject * object) -{ - MpegTsPmtInfo *info = MPEGTS_PMT_INFO (object); - - g_value_array_free (info->streams); - g_value_array_free (info->descriptors); - - G_OBJECT_CLASS (parent_class)->finalize (object); -} - -static void -mpegts_pmt_info_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * spec) -{ - g_return_if_fail (MPEGTS_IS_PMT_INFO (object)); - - /* No settable properties */ - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); -} - -static void -mpegts_pmt_info_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * spec) -{ - MpegTsPmtInfo *pmt_info; - - g_return_if_fail (MPEGTS_IS_PMT_INFO (object)); - - pmt_info = MPEGTS_PMT_INFO (object); - - switch (prop_id) { - case PROP_PROGRAM_NO: - g_value_set_uint (value, pmt_info->program_no); - break; - case PROP_PCR_PID: - g_value_set_uint (value, pmt_info->pcr_pid); - break; - case PROP_STREAMINFO: - g_value_set_boxed (value, pmt_info->streams); - break; - case PROP_VERSION_NO: - g_value_set_uint (value, pmt_info->version_no); - break; - case PROP_DESCRIPTORS: - g_value_set_boxed (value, pmt_info->descriptors); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); - break; - } -} - -void -mpegts_pmt_info_add_descriptor (MpegTsPmtInfo * pmt_info, - const gchar * descriptor, guint length) -{ - GValue value = { 0 }; - GString *string; - - g_return_if_fail (MPEGTS_IS_PMT_INFO (pmt_info)); - - string = g_string_new_len (descriptor, length); - - g_value_init (&value, G_TYPE_GSTRING); - g_value_take_boxed (&value, string); - g_value_array_append (pmt_info->descriptors, &value); - g_value_unset (&value); -} - -void -mpegts_pmt_info_add_stream (MpegTsPmtInfo * pmt_info, - MpegTsPmtStreamInfo * stream) -{ - GValue v = { 0, }; - - g_return_if_fail (MPEGTS_IS_PMT_INFO (pmt_info)); - g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (stream)); - - g_value_init (&v, G_TYPE_OBJECT); - g_value_take_object (&v, stream); - g_value_array_append (pmt_info->streams, &v); - g_value_unset (&v); -} diff --git a/gst/mpegdemux/flutspmtinfo.h b/gst/mpegdemux/flutspmtinfo.h deleted file mode 100644 index 82d10c5..0000000 --- a/gst/mpegdemux/flutspmtinfo.h +++ /dev/null @@ -1,81 +0,0 @@ - /* - * 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 - */ - -#ifndef __FLUTS_PMT_INFO_H__ -#define __FLUTS_PMT_INFO_H__ - -#include -#include "flutspmtstreaminfo.h" - -G_BEGIN_DECLS - -typedef struct MpegTsPmtInfoClass { - GObjectClass parent_class; -} MpegTsPmtInfoClass; - -typedef struct MpegTsPmtInfo { - GObject parent; - - guint16 program_no; - guint16 pcr_pid; - - guint8 version_no; - - GValueArray *descriptors; - GValueArray *streams; -} MpegTsPmtInfo; - -MpegTsPmtInfo *mpegts_pmt_info_new (guint16 program_no, guint16 pcr_pid, guint8 version); -void mpegts_pmt_info_add_stream (MpegTsPmtInfo *pmt_info, MpegTsPmtStreamInfo *stream); -void mpegts_pmt_info_add_descriptor (MpegTsPmtInfo *pmt_info, - const gchar *descriptor, guint length); - -GType mpegts_pmt_info_get_type (void); - -#define MPEGTS_TYPE_PMT_INFO (mpegts_pmt_info_get_type ()) -#define MPEGTS_IS_PMT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MPEGTS_TYPE_PMT_INFO)) -#define MPEGTS_PMT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MPEGTS_TYPE_PMT_INFO, MpegTsPmtInfo)) - -G_END_DECLS - -#endif diff --git a/gst/mpegdemux/flutspmtstreaminfo.c b/gst/mpegdemux/flutspmtstreaminfo.c deleted file mode 100644 index 95177f8..0000000 --- a/gst/mpegdemux/flutspmtstreaminfo.c +++ /dev/null @@ -1,212 +0,0 @@ - /* - * 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 - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex - * with newer GLib versions (>= 2.31.0) */ -#define GLIB_DISABLE_DEPRECATION_WARNINGS - -#include - -#include "flutspmtstreaminfo.h" - -enum -{ - PROP_0, - PROP_PID, - PROP_LANGUAGES, - PROP_STREAM_TYPE, - PROP_DESCRIPTORS, -}; - -GST_BOILERPLATE (MpegTsPmtStreamInfo, mpegts_pmt_stream_info, GObject, - G_TYPE_OBJECT); - -static void mpegts_pmt_stream_info_set_property (GObject * object, - guint prop_id, const GValue * value, GParamSpec * spec); -static void mpegts_pmt_stream_info_get_property (GObject * object, - guint prop_id, GValue * value, GParamSpec * spec); -static void mpegts_pmt_stream_info_finalize (GObject * object); - -static void -mpegts_pmt_stream_info_base_init (gpointer klass) -{ -} - -static void -mpegts_pmt_stream_info_class_init (MpegTsPmtStreamInfoClass * klass) -{ - GObjectClass *gobject_klass = (GObjectClass *) klass; - - gobject_klass->set_property = mpegts_pmt_stream_info_set_property; - gobject_klass->get_property = mpegts_pmt_stream_info_get_property; - gobject_klass->finalize = mpegts_pmt_stream_info_finalize; - - g_object_class_install_property (gobject_klass, PROP_PID, - g_param_spec_uint ("pid", "PID carrying this stream", - "PID which carries this stream", 1, G_MAXUINT16, 1, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - 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_STATIC_STRINGS), - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - - 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_PARAM_STATIC_STRINGS)); - - 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_STATIC_STRINGS), - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); -} - -static void -mpegts_pmt_stream_info_init (MpegTsPmtStreamInfo * pmt_stream_info, - MpegTsPmtStreamInfoClass * klass) -{ - pmt_stream_info->languages = g_value_array_new (0); - pmt_stream_info->descriptors = g_value_array_new (0); -} - -static void -mpegts_pmt_stream_info_finalize (GObject * object) -{ - MpegTsPmtStreamInfo *info = MPEGTS_PMT_STREAM_INFO (object); - - g_value_array_free (info->languages); - g_value_array_free (info->descriptors); - - GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object)); -} - -MpegTsPmtStreamInfo * -mpegts_pmt_stream_info_new (guint16 pid, guint8 type) -{ - MpegTsPmtStreamInfo *info; - info = g_object_new (MPEGTS_TYPE_PMT_STREAM_INFO, NULL); - - info->pid = pid; - info->stream_type = type; - return info; -} - -void -mpegts_pmt_stream_info_add_language (MpegTsPmtStreamInfo * pmt_info, - gchar * language) -{ - GValue v = { 0, }; - - g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (pmt_info)); - - g_value_init (&v, G_TYPE_STRING); - g_value_take_string (&v, language); - g_value_array_append (pmt_info->languages, &v); - g_value_unset (&v); -} - -void -mpegts_pmt_stream_info_add_descriptor (MpegTsPmtStreamInfo * pmt_info, - const gchar * descriptor, guint length) -{ - GValue value = { 0 }; - GString *string; - - g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (pmt_info)); - - string = g_string_new_len (descriptor, length); - - g_value_init (&value, G_TYPE_GSTRING); - g_value_take_boxed (&value, string); - g_value_array_append (pmt_info->descriptors, &value); - g_value_unset (&value); -} - -static void -mpegts_pmt_stream_info_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * spec) -{ - g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (object)); - - /* No settable properties */ - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); -} - -static void -mpegts_pmt_stream_info_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * spec) -{ - MpegTsPmtStreamInfo *si; - - g_return_if_fail (MPEGTS_IS_PMT_STREAM_INFO (object)); - - si = MPEGTS_PMT_STREAM_INFO (object); - - switch (prop_id) { - case PROP_STREAM_TYPE: - g_value_set_uint (value, si->stream_type); - break; - case PROP_PID: - g_value_set_uint (value, si->pid); - break; - case PROP_LANGUAGES: - g_value_set_boxed (value, si->languages); - break; - case PROP_DESCRIPTORS: - g_value_set_boxed (value, si->descriptors); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec); - break; - } -} diff --git a/gst/mpegdemux/flutspmtstreaminfo.h b/gst/mpegdemux/flutspmtstreaminfo.h deleted file mode 100644 index 6c7d42f..0000000 --- a/gst/mpegdemux/flutspmtstreaminfo.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 - */ - -#ifndef __FLUTS_PMT_STREAM_INFO_H__ -#define __FLUTS_PMT_STREAM_INFO_H__ - -#include - -G_BEGIN_DECLS - - -typedef struct MpegTsPmtStreamInfoClass { - GObjectClass parent_class; -} MpegTsPmtStreamInfoClass; - -typedef struct MpegTsPmtStreamInfo { - GObject parent; - - guint16 pid; - GValueArray *languages; /* null terminated 3 character ISO639 language code */ - guint8 stream_type; - GValueArray *descriptors; -} MpegTsPmtStreamInfo; - -MpegTsPmtStreamInfo *mpegts_pmt_stream_info_new (guint16 pid, guint8 type); -void mpegts_pmt_stream_info_add_language(MpegTsPmtStreamInfo* si, - gchar* language); -void mpegts_pmt_stream_info_add_descriptor (MpegTsPmtStreamInfo *pmt_info, - const gchar *descriptor, guint length); - -GType mpegts_pmt_stream_info_get_type (void); - -#define MPEGTS_TYPE_PMT_STREAM_INFO (mpegts_pmt_stream_info_get_type ()) - -#define MPEGTS_IS_PMT_STREAM_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MPEGTS_TYPE_PMT_STREAM_INFO)) -#define MPEGTS_PMT_STREAM_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MPEGTS_TYPE_PMT_STREAM_INFO, MpegTsPmtStreamInfo)) - -G_END_DECLS - -#endif diff --git a/gst/mpegdemux/gstmpegdesc.c b/gst/mpegdemux/gstmpegdesc.c deleted file mode 100644 index 7e80483..0000000 --- a/gst/mpegdemux/gstmpegdesc.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * The contents of this file are subject to the Mozilla Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/. - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Fluendo MPEG Demuxer plugin. - * - * The Initial Developer of the Original Code is Fluendo, S.L. - * Portions created by Fluendo, S.L. are Copyright (C) 2005 - * Fluendo, S.L. All Rights Reserved. - * - * Contributor(s): Wim Taymans - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU Lesser General Public License Version 2 or later (the "LGPL"), - * in which case the provisions of the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of the MPL or the LGPL. - */ - -#include - -#include - -#include "gstmpegdesc.h" - -GST_DEBUG_CATEGORY (gstmpegtsdesc_debug); -#define GST_CAT_DEFAULT (gstmpegtsdesc_debug) - -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; - - GST_MEMDUMP ("tag contents:", data, length); - - 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) -{ - guint8 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; -} - -/* array needs freeing afterwards */ -GArray * -gst_mpeg_descriptor_find_all (GstMPEGDescriptor * desc, gint tag) -{ - GArray *all; - - guint8 length; - guint8 *current; - guint size; - - g_return_val_if_fail (desc != NULL, NULL); - all = g_array_new (TRUE, TRUE, sizeof (guint8 *)); - - current = desc->data; - length = desc->data_length; - - while (length > 0) { - if (DESC_TAG (current) == tag) - g_array_append_val (all, current); - size = DESC_LENGTH (current) + 2; - - current += size; - length -= size; - } - - GST_DEBUG ("found tag 0x%02x %d times", tag, all->len); - - return all; -} - -guint8 * -gst_mpeg_descriptor_nth (GstMPEGDescriptor * desc, guint i) -{ - guint8 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; -} - -void -gst_mpegtsdesc_init_debug (void) -{ - GST_DEBUG_CATEGORY_INIT (gstmpegtsdesc_debug, "mpegtsdesc", 0, - "MPEG transport stream parser (descriptor)"); -} diff --git a/gst/mpegdemux/gstmpegdesc.h b/gst/mpegdemux/gstmpegdesc.h deleted file mode 100644 index 9e1ae75..0000000 --- a/gst/mpegdemux/gstmpegdesc.h +++ /dev/null @@ -1,342 +0,0 @@ -/* - * The contents of this file are subject to the Mozilla Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/. - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Fluendo MPEG Demuxer plugin. - * - * The Initial Developer of the Original Code is Fluendo, S.L. - * Portions created by Fluendo, S.L. are Copyright (C) 2005 - * Fluendo, S.L. All Rights Reserved. - * - * Contributor(s): Wim Taymans - * - * Alternatively, the contents of this file may be used under the terms of - * the GNU Lesser General Public License Version 2 or later (the "LGPL"), - * in which case the provisions of the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of the MPL or the LGPL. - */ - -#ifndef __GST_MPEG_DESC_H__ -#define __GST_MPEG_DESC_H__ - -#include -/* - * 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_CAROUSEL_IDENTIFIER 0x13 -#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 */ -#define DESC_DTG_LOGICAL_CHANNEL 0x83 /* from DTG D-Book */ -/* 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) - -/* DVB Service Descriptor */ -#define DESC_DVB_SERVICE_type(desc) (desc[2]) -#define DESC_DVB_SERVICE_provider_name_length(desc) (desc[3]) -#define DESC_DVB_SERVICE_provider_name_text(desc) (desc+4) -#define DESC_DVB_SERVICE_name_length(desc) (desc[4 + DESC_DVB_SERVICE_provider_name_length(desc)]) -#define DESC_DVB_SERVICE_name_text(desc) (desc + 5 + DESC_DVB_SERVICE_provider_name_length(desc)) - -/* DVB Component Descriptor */ -#define DESC_DVB_COMPONENT_stream_content(desc) (desc[2] & 0x0F) -#define DESC_DVB_COMPONENT_type(desc) (desc[3]) -#define DESC_DVB_COMPONENT_tag(desc) (desc[4]) -#define DESC_DVB_COMPONENT_language(desc) (desc + 5) - -/* DVB Bouquet Name Descriptor */ -#define DESC_DVB_BOUQUET_NAME_text(desc) (desc + 2) - -/* DVB Short Event Descriptor */ -#define DESC_DVB_SHORT_EVENT_name_text(desc) (desc + 6) -#define DESC_DVB_SHORT_EVENT_name_length(desc) (desc[5]) -#define DESC_DVB_SHORT_EVENT_description_text(desc) (desc + 6 + DESC_DVB_SHORT_EVENT_name_length(desc) + 1) -#define DESC_DVB_SHORT_EVENT_description_length(desc) (desc[6 + DESC_DVB_SHORT_EVENT_name_length(desc)]) - -/* DVB Extended Event Descriptor */ -#define DESC_DVB_EXTENDED_EVENT_descriptor_number(desc) ((desc[2] & 0xF0) >> 4) -#define DESC_DVB_EXTENDED_EVENT_last_descriptor_number(desc) (desc[2] & 0x0F) -#define DESC_DVB_EXTENDED_EVENT_iso639_language_code(desc) (desc + 3) -#define DESC_DVB_EXTENDED_EVENT_items_length(desc) (desc[6]) -#define DESC_DVB_EXTENDED_EVENT_items(desc) (desc + 7) -#define DESC_DVB_EXTENDED_EVENT_text_length(desc) (desc[7 + DESC_DVB_EXTENDED_EVENT_items_length(desc)]) -#define DESC_DVB_EXTENDED_EVENT_text(desc) (desc + 7 + DESC_DVB_EXTENDED_EVENT_items_length(desc) + 1) - -/* DVB Satellite Delivery System Descriptor */ -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_frequency(desc) (desc + 2) -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_orbital_position(desc) (desc + 6) -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_west_east_flag(desc) ((desc[8] & 0x80) == 0x80) -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_polarization(desc) ((desc[8] >> 5) & 0x3) -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_modulation(desc) (desc[8] & 0x1F) -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_symbol_rate(desc) (desc + 9) -#define DESC_DVB_SATELLITE_DELIVERY_SYSTEM_fec_inner(desc) (desc[12] & 0x0F) - -/* DVB Terrestrial Delivery System Descriptor */ -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_frequency(desc) (GST_READ_UINT32_BE((desc) + 2)) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_bandwidth(desc) ((desc[6] >> 5) & 0x7) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_constellation(desc) ((desc[7] >> 6) & 0x3) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_hierarchy(desc) ((desc[7] >> 3) & 0x7) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_code_rate_hp(desc) (desc[7] & 0x7) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_code_rate_lp(desc) ((desc[8] >> 5) & 0x7) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_guard_interval(desc) ((desc[8] >> 3) & 0x3) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_transmission_mode(desc) ((desc[8] >> 1) & 0x3) -#define DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_other_frequency(desc) ((desc[8] & 0x01) == 0x01) - -/* DVB Cable Delivery System Descriptor */ -#define DESC_DVB_CABLE_DELIVERY_SYSTEM_frequency(desc) (desc + 2) -#define DESC_DVB_CABLE_DELIVERY_SYSTEM_fec_outer(desc) (desc[7] & 0x0F) -#define DESC_DVB_CABLE_DELIVERY_SYSTEM_modulation(desc) (desc[8]) -#define DESC_DVB_CABLE_DELIVERY_SYSTEM_symbol_rate(desc) (desc + 9) -#define DESC_DVB_CABLE_DELIVERY_SYSTEM_fec_inner(desc) (desc[12] & 0x0F) - -/* DVB Data Broadcast Descriptor */ -#define DESC_DVB_DATA_BROADCAST_data_broadcast_id(desc) (GST_READ_UINT16_BE((desc) + 2)) -#define DESC_DVB_DATA_BROADCAST_component_tag(desc) (desc[4]) -#define DESC_DVB_DATA_BROADCAST_selector_length(desc) (desc[5]) -#define DESC_DVB_DATA_BROADCAST_selector(desc) (desc + 6) -#define DESC_DVB_DATA_BROADCAST_iso639_language_code(desc) (desc + 6 + DESC_DVB_DATA_BROADCAST_selector_length(desc)) -#define DESC_DVB_DATA_BROADCAST_text_length(desc) (desc + 9 + DESC_DVB_DATA_BROADCAST_selector_length(desc)) -#define DESC_DVB_DATA_BROADCAST_text(desc) (desc + 10 + DESC_DVB_DATA_BROADCAST_selector_length(desc)) - -/* DVB Data Broadcast Id Descriptor */ -#define DESC_DVB_DATA_BROADCAST_ID_data_broadcast_id(desc) (GST_READ_UINT16_BE((desc) + 2)) -#define DESC_DVB_DATA_BROADCAST_ID_id_selector_byte(desc) (desc + 4) - -/* DVB Carousel Identifier Descriptor */ -#define DESC_DVB_CAROUSEL_IDENTIFIER_carousel_id(desc) (GST_READ_UINT32_BE((desc) + 2)) - -/* registration_descriptor format IDs */ -#define DRF_ID_HDMV 0x48444d56 -#define DRF_ID_VC1 0x56432D31 /* defined in RP227 */ - -typedef struct { - guint n_desc; - guint8 data_length; - guint8 *data; -} GstMPEGDescriptor; - -void gst_mpegtsdesc_init_debug (void); -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); -GArray* gst_mpeg_descriptor_find_all (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 deleted file mode 100644 index 78bf656..0000000 --- a/gst/mpegdemux/gstmpegtsdemux.c +++ /dev/null @@ -1,3558 +0,0 @@ - /* - * 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.A. - * Portions created by Fluendo, S.L. are Copyright (C) 2005,2006,2007,2008,2009 - * Fluendo, S.A. All Rights Reserved. - * - * Contributor(s): Wim Taymans - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex - * with newer GLib versions (>= 2.31.0) */ -#define GLIB_DISABLE_DEPRECATION_WARNINGS - -#include -#include - -#include - -#include "gstmpegdefs.h" -#include "gstmpegtsdemux.h" -#include "flutspatinfo.h" -#include "flutspmtinfo.h" - -GST_DEBUG_CATEGORY_STATIC (gstmpegtsdemux_debug); -#define GST_CAT_DEFAULT (gstmpegtsdemux_debug) - -/* elementfactory information */ - -#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 - -/* 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 - -/* threshold at which we deem PTS difference to be a discontinuity */ -#define DISCONT_THRESHOLD_AV (GST_SECOND * 2) /* 2 seconds */ -#define DISCONT_THRESHOLD_OTHER (GST_SECOND * 60 * 10) /* 10 minutes */ - -enum -{ - PROP_0, - PROP_ES_PIDS, - PROP_CHECK_CRC, - PROP_PROGRAM_NUMBER, - PROP_PAT_INFO, - PROP_PMT_INFO, -}; - -#define GSTTIME_TO_BYTES(time) \ - ((time != -1) ? gst_util_uint64_scale (MAX(0,(gint64) ((time))), \ - demux->bitrate, GST_SECOND) : -1) -#define BYTES_TO_GSTTIME(bytes) \ - ((bytes != -1) ? (gst_util_uint64_scale (bytes, GST_SECOND, \ - demux->bitrate)) : -1) - -#define VIDEO_CAPS \ - GST_STATIC_CAPS (\ - "video/mpeg, " \ - "mpegversion = (int) { 1, 2, 4 }, " \ - "systemstream = (boolean) FALSE; " \ - "video/x-h264,stream-format=(string)byte-stream," \ - "alignment=(string)nal;" \ - "video/x-dirac;" \ - "video/x-wmv," \ - "wmvversion = (int) 3, " \ - "format = (fourcc) WVC1" \ - ) - -#define AUDIO_CAPS \ - GST_STATIC_CAPS ( \ - "audio/mpeg, " \ - "mpegversion = (int) 1;" \ - "audio/mpeg, " \ - "mpegversion = (int) 4, " \ - "stream-format = (string) { adts, loas };" \ - "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-eac3;" \ - "audio/x-dts;" \ - "audio/x-private-ts-lpcm" \ - ) - -/* Can also use the subpicture pads for text subtitles? */ -#define SUBPICTURE_CAPS \ - GST_STATIC_CAPS ("subpicture/x-pgs; video/x-dvd-subpicture") - -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 subpicture_template = -GST_STATIC_PAD_TEMPLATE ("subpicture_%04x", - GST_PAD_SRC, - GST_PAD_SOMETIMES, - SUBPICTURE_CAPS); - -static GstStaticPadTemplate private_template = -GST_STATIC_PAD_TEMPLATE ("private_%04x", - GST_PAD_SRC, - GST_PAD_SOMETIMES, - GST_STATIC_CAPS_ANY); - -static void gst_mpegts_demux_base_init (GstMpegTSDemuxClass * klass); -static void gst_mpegts_demux_class_init (GstMpegTSDemuxClass * klass); -static void gst_mpegts_demux_init (GstMpegTSDemux * demux); -static void gst_mpegts_demux_finalize (GstMpegTSDemux * demux); -static void gst_mpegts_demux_reset (GstMpegTSDemux * demux); - -//static void gst_mpegts_demux_remove_pads (GstMpegTSDemux * demux); -static void gst_mpegts_demux_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec); -static void gst_mpegts_demux_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec); - -static gboolean gst_mpegts_demux_is_PMT (GstMpegTSDemux * demux, guint16 PID); - -static gboolean gst_mpegts_demux_sink_event (GstPad * pad, GstEvent * event); -static gboolean gst_mpegts_demux_src_event (GstPad * pad, GstEvent * event); -static GstFlowReturn gst_mpegts_demux_chain (GstPad * pad, GstBuffer * buffer); -static gboolean gst_mpegts_demux_sink_setcaps (GstPad * pad, GstCaps * caps); - -static gboolean gst_mpegts_demux_is_live (GstMpegTSDemux * demux); -static GstClock *gst_mpegts_demux_provide_clock (GstElement * element); -static gboolean gst_mpegts_demux_src_pad_query (GstPad * pad, GstQuery * query); -static const GstQueryType *gst_mpegts_demux_src_pad_query_type (GstPad * pad); - -static GstStateChangeReturn gst_mpegts_demux_change_state (GstElement * element, - GstStateChange transition); - -static MpegTsPmtInfo *mpegts_demux_build_pmt_info (GstMpegTSDemux * demux, - guint16 pmt_pid); - -static GstElementClass *parent_class = NULL; - -/*static guint gst_mpegts_demux_signals[LAST_SIGNAL] = { 0 };*/ - -GType -gst_mpegts_demux_get_type (void) -{ - static GType mpegts_demux_type = 0; - - if (G_UNLIKELY (!mpegts_demux_type)) { - static const GTypeInfo mpegts_demux_info = { - sizeof (GstMpegTSDemuxClass), - (GBaseInitFunc) gst_mpegts_demux_base_init, - NULL, - (GClassInitFunc) gst_mpegts_demux_class_init, - NULL, - NULL, - sizeof (GstMpegTSDemux), - 0, - (GInstanceInitFunc) gst_mpegts_demux_init, - }; - - mpegts_demux_type = - g_type_register_static (GST_TYPE_ELEMENT, "GstMpegTSDemux", - &mpegts_demux_info, 0); - - GST_DEBUG_CATEGORY_INIT (gstmpegtsdemux_debug, "mpegtsdemux", 0, - "MPEG program stream demultiplexer element"); - } - - return mpegts_demux_type; -} - -static void -gst_mpegts_demux_base_init (GstMpegTSDemuxClass * 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->subpicture_template = - gst_static_pad_template_get (&subpicture_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->subpicture_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_simple (element_class, - "The Fluendo MPEG Transport stream demuxer", "Codec/Demuxer", - "Demultiplexes MPEG2 Transport Streams", "Wim Taymans "); -} - -static void -gst_mpegts_demux_class_init (GstMpegTSDemuxClass * 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_mpegts_demux_finalize; - gobject_class->set_property = gst_mpegts_demux_set_property; - gobject_class->get_property = gst_mpegts_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_PARAM_STATIC_STRINGS)); - 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_PARAM_STATIC_STRINGS)); - 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_PARAM_STATIC_STRINGS)); - - g_object_class_install_property (gobject_class, PROP_PAT_INFO, - g_param_spec_value_array ("pat-info", - "GValueArray containing GObjects with properties", - "Array of GObjects containing information from the TS PAT " - "about all programs listed in the current Program Association " - "Table (PAT)", - g_param_spec_object ("flu-pat-streaminfo", "FluPATStreamInfo", - "Fluendo TS Demuxer PAT Stream info object", - MPEGTS_TYPE_PAT_INFO, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS), - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - - 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", - MPEGTS_TYPE_PMT_INFO, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - - gstelement_class->change_state = gst_mpegts_demux_change_state; - gstelement_class->provide_clock = gst_mpegts_demux_provide_clock; -} - -static void -gst_mpegts_demux_init (GstMpegTSDemux * demux) -{ - GstMpegTSDemuxClass *klass = GST_MPEGTS_DEMUX_GET_CLASS (demux); - - demux->streams = - g_malloc0 (sizeof (GstMpegTSStream *) * (MPEGTS_MAX_PID + 1)); - demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink"); - gst_pad_set_chain_function (demux->sinkpad, gst_mpegts_demux_chain); - gst_pad_set_event_function (demux->sinkpad, gst_mpegts_demux_sink_event); - gst_pad_set_setcaps_function (demux->sinkpad, gst_mpegts_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->sync_lut = NULL; - demux->sync_lut_len = 0; - demux->bitrate = -1; - demux->num_packets = 0; - demux->pcr[0] = -1; - demux->pcr[1] = -1; - demux->cache_duration = GST_CLOCK_TIME_NONE; - demux->base_pts = GST_CLOCK_TIME_NONE; - demux->in_gap = GST_CLOCK_TIME_NONE; - demux->first_buf_ts = GST_CLOCK_TIME_NONE; - demux->last_buf_ts = GST_CLOCK_TIME_NONE; -} - -static void -gst_mpegts_demux_finalize (GstMpegTSDemux * demux) -{ - gst_mpegts_demux_reset (demux); - g_free (demux->streams); - - G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux)); -} - -static void -gst_mpegts_demux_reset (GstMpegTSDemux * demux) -{ - /* Clean up the streams and pads we allocated */ - gint i; - - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *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); - - gst_pes_filter_uninit (&stream->filter); - gst_section_filter_uninit (&stream->section_filter); - - if (stream->pes_buffer) { - gst_buffer_unref (stream->pes_buffer); - stream->pes_buffer = NULL; - } - g_free (stream); - demux->streams[i] = NULL; - } - } - - if (demux->clock) { - g_object_unref (demux->clock); - demux->clock = NULL; - } - - demux->in_gap = GST_CLOCK_TIME_NONE; - demux->first_buf_ts = GST_CLOCK_TIME_NONE; - demux->last_buf_ts = GST_CLOCK_TIME_NONE; -} - -static void -gst_mpegts_demux_no_more_pads (GstElement * demux) -{ - /* We should really call no-more-pads here, but we don't as - this would preclude addition of more pads if/when new streams - are added. */ -} - -#if 0 -static void -gst_mpegts_demux_remove_pads (GstMpegTSDemux * 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 < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *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 const 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_mpegts_demux_sink_setcaps (GstPad * pad, GstCaps * caps) -{ - GstMpegTSDemux *demux = GST_MPEGTS_DEMUX (gst_pad_get_parent (pad)); - GstStructure *structure = NULL; - - 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", &demux->packetsize)) { - GST_DEBUG_OBJECT (demux, "packetsize parameter not found in sink caps"); - } - - gst_object_unref (demux); - return TRUE; -} - -static FORCE_INLINE guint32 -gst_mpegts_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_mpegts_is_dirac_stream (GstMpegTSStream * 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_mpegts_stream_is_video (GstMpegTSStream * 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_mpegts_is_dirac_stream (stream); - } - - return FALSE; -} - -static FORCE_INLINE gboolean -gst_mpegts_stream_is_audio (GstMpegTSStream * stream) -{ - switch (stream->stream_type) { - case ST_AUDIO_MPEG1: - case ST_AUDIO_MPEG2: - case ST_AUDIO_AAC_ADTS: - case ST_AUDIO_AAC_LOAS: - return TRUE; - } - - return FALSE; -} - -static gboolean -gst_mpegts_demux_is_reserved_PID (GstMpegTSDemux * demux, guint16 PID) -{ - return (PID >= PID_RESERVED_FIRST) && (PID < PID_RESERVED_LAST); -} - -/* This function assumes that provided PID never will be greater than - * MPEGTS_MAX_PID (13 bits), this is currently guaranteed as everywhere in - * the code recovered PID at maximum is 13 bits long. - */ -static FORCE_INLINE GstMpegTSStream * -gst_mpegts_demux_get_stream_for_PID (GstMpegTSDemux * demux, guint16 PID) -{ - GstMpegTSStream *stream = NULL; - - stream = demux->streams[PID]; - - if (G_UNLIKELY (stream == NULL)) { - stream = g_new0 (GstMpegTSStream, 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 = MPEGTS_MAX_PID + 1; - stream->flags |= MPEGTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; - stream->pes_buffer_in_sync = FALSE; - 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_mpegts_demux_is_reserved_PID (demux, PID)) { - stream->PID_type = PID_TYPE_RESERVED; - } else { - /* check if PMT found in PAT */ - if (gst_mpegts_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; -} - -static gboolean -gst_mpegts_demux_fill_stream (GstMpegTSStream * stream, guint8 id, - guint8 stream_type) -{ - GstPadTemplate *template; - gchar *name; - GstMpegTSDemuxClass *klass; - GstMpegTSDemux *demux; - GstCaps *caps; - - if (stream->stream_type && stream->stream_type != stream_type) - goto wrong_type; - - demux = stream->demux; - klass = GST_MPEGTS_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_ENHANCED_AC3)) { - template = klass->private_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/x-eac3", 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); - } else if (gst_mpeg_descriptor_find (stream->ES_info, - DESC_DVB_SUBTITLING)) { - template = klass->private_template; - name = g_strdup_printf ("private_%04x", stream->PID); - caps = gst_caps_new_simple ("subpicture/x-dvb", NULL); - } - break; - case ST_HDV_AUX_V: - template = klass->private_template; - name = g_strdup_printf ("private_%04x", stream->PID); - caps = gst_caps_new_simple ("hdv/aux-v", NULL); - break; - case ST_HDV_AUX_A: - template = klass->private_template; - name = g_strdup_printf ("private_%04x", stream->PID); - caps = gst_caps_new_simple ("hdv/aux-a", NULL); - break; - case ST_PRIVATE_SECTIONS: - case ST_MHEG: - case ST_DSMCC: - break; - case ST_AUDIO_AAC_ADTS: - template = klass->audio_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/mpeg", - "mpegversion", G_TYPE_INT, 4, - "stream-format", G_TYPE_STRING, "adts", NULL); - break; - case ST_AUDIO_AAC_LOAS: // LATM/LOAS AAC syntax - template = klass->audio_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/mpeg", - "mpegversion", G_TYPE_INT, 4, - "stream-format", G_TYPE_STRING, "loas", 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", - "stream-format", G_TYPE_STRING, "byte-stream", - "alignment", G_TYPE_STRING, "nal", NULL); - break; - case ST_VIDEO_DIRAC: - if (gst_mpegts_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_PRIVATE_EA: /* Try to detect a VC1 stream */ - { - guint8 *desc = NULL; - - if (stream->ES_info) - desc = gst_mpeg_descriptor_find (stream->ES_info, DESC_REGISTRATION); - if (!(desc && DESC_REGISTRATION_format_identifier (desc) == DRF_ID_VC1)) { - GST_WARNING ("0xea private stream type found but no descriptor " - "for VC1. Assuming plain VC1."); - } - template = klass->video_template; - name = g_strdup_printf ("video_%04x", stream->PID); - caps = gst_caps_new_simple ("video/x-wmv", - "wmvversion", G_TYPE_INT, 3, - "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('W', 'V', 'C', '1'), - NULL); - break; - } - case ST_BD_AUDIO_AC3: - { - GstMpegTSStream *PMT_stream = - gst_mpegts_demux_get_stream_for_PID (stream->demux, stream->PMT_pid); - GstMPEGDescriptor *program_info = PMT_stream->PMT.program_info; - guint8 *desc = NULL; - - if (program_info) - desc = gst_mpeg_descriptor_find (program_info, DESC_REGISTRATION); - - if (desc && DESC_REGISTRATION_format_identifier (desc) == DRF_ID_HDMV) { - template = klass->audio_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/x-eac3", NULL); - } else if (stream->ES_info && gst_mpeg_descriptor_find (stream->ES_info, - DESC_DVB_ENHANCED_AC3)) { - template = klass->audio_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/x-eac3", NULL); - } else { - if (!stream->ES_info || - !gst_mpeg_descriptor_find (stream->ES_info, DESC_DVB_AC3)) { - GST_WARNING ("AC3 stream type found but no corresponding " - "descriptor to differentiate between AC3 and EAC3. " - "Assuming plain 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_BD_AUDIO_EAC3: - template = klass->audio_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/x-eac3", NULL); - break; - case ST_PS_AUDIO_DTS: - case ST_BD_AUDIO_DTS: - case ST_BD_AUDIO_DTS_HD: - case ST_BD_AUDIO_DTS_HD_MASTER_AUDIO: - 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_BD_AUDIO_LPCM: - template = klass->audio_template; - name = g_strdup_printf ("audio_%04x", stream->PID); - caps = gst_caps_new_simple ("audio/x-private-ts-lpcm", NULL); - break; - case ST_PS_DVD_SUBPICTURE: - template = klass->subpicture_template; - name = g_strdup_printf ("subpicture_%04x", stream->PID); - caps = gst_caps_new_simple ("video/x-dvd-subpicture", NULL); - break; - case ST_BD_PGS_SUBPICTURE: - template = klass->subpicture_template; - name = g_strdup_printf ("subpicture_%04x", stream->PID); - caps = gst_caps_new_simple ("subpicture/x-pgs", NULL); - break; - default: - break; - } - if (name == NULL || template == NULL || caps == NULL) { - if (name) - g_free (name); - if (caps) - gst_caps_unref (caps); - return FALSE; - } - - stream->stream_type = stream_type; - stream->id = id; - GST_DEBUG ("creating new pad %s", name); - 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); - gst_pad_set_query_function (stream->pad, - GST_DEBUG_FUNCPTR (gst_mpegts_demux_src_pad_query)); - gst_pad_set_query_type_function (stream->pad, - GST_DEBUG_FUNCPTR (gst_mpegts_demux_src_pad_query_type)); - gst_pad_set_event_function (stream->pad, - GST_DEBUG_FUNCPTR (gst_mpegts_demux_src_event)); - g_free (name); - - return TRUE; - -wrong_type: - { - return FALSE; - } -} - -static FORCE_INLINE gboolean -mpegts_is_elem_pid (GstMpegTSDemux * 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_mpegts_demux_setup_base_pts (GstMpegTSDemux * demux, gint64 pts) -{ - GstMpegTSStream *PCR_stream; - GstMpegTSStream *PMT_stream; - 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_stream; - - 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; - - demux->base_pts = MPEGTIME_TO_GSTTIME (base_PCR); - - if (demux->base_pts == GST_CLOCK_TIME_NONE) - return FALSE; - - 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; - } -} - -static gboolean -gst_mpegts_demux_send_new_segment (GstMpegTSDemux * demux, - GstMpegTSStream * stream, gint64 pts) -{ - GstClockTime time; - - /* base_pts needs to have been set up by a call to - * gst_mpegts_demux_setup_base_pts() before calling this function */ - if (demux->base_pts == GST_CLOCK_TIME_NONE) - goto no_base_time; - - time = demux->base_pts; - - GST_DEBUG_OBJECT (demux, "segment PTS to time: %" - GST_TIME_FORMAT, GST_TIME_ARGS (time)); - - 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); - } - - gst_pad_push_event (stream->pad, - gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, time, -1, 0)); - - return TRUE; - - /* ERRORS */ -no_base_time: - { - /* check if it's in our partial ts pid list */ - if (mpegts_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; - } - - } - return FALSE; -} - -static void -gst_mpegts_demux_send_tags_for_stream (GstMpegTSDemux * demux, - GstMpegTSStream * stream) -{ - GstTagList *list = NULL; - gint i; - - if (stream->ES_info) { - static const guint8 lang_descs[] = - { DESC_ISO_639_LANGUAGE, DESC_DVB_SUBTITLING }; - for (i = 0; i < G_N_ELEMENTS (lang_descs); i++) { - guint8 *iso639_languages = - gst_mpeg_descriptor_find (stream->ES_info, lang_descs[i]); - if (iso639_languages) { - if (DESC_ISO_639_LANGUAGE_codes_n (iso639_languages)) { - const gchar *lc; - gchar lang_code[4]; - gchar *language_n; - - language_n = (gchar *) - DESC_ISO_639_LANGUAGE_language_code_nth (iso639_languages, 0); - - lang_code[0] = language_n[0]; - lang_code[1] = language_n[1]; - lang_code[2] = language_n[2]; - lang_code[3] = 0; - - if (!list) - list = gst_tag_list_new (); - - /* descriptor contains ISO 639-2 code, we want the ISO 639-1 code */ - lc = gst_tag_get_language_code (lang_code); - gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, - GST_TAG_LANGUAGE_CODE, (lc) ? lc : lang_code, NULL); - } - } - } - } - - if (list) { - GST_DEBUG_OBJECT (demux, "Sending tags %p for pad %s:%s", - list, GST_DEBUG_PAD_NAME (stream->pad)); - gst_element_found_tags_for_pad (GST_ELEMENT (demux), stream->pad, list); - } -} - -static GstFlowReturn -gst_mpegts_demux_combine_flows (GstMpegTSDemux * demux, - GstMpegTSStream * stream, GstFlowReturn ret) -{ - gint i; - - /* store the value */ - stream->last_ret = ret; - - /* if it's success we can return the value right away */ - if (ret == GST_FLOW_OK) - 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 < MPEGTS_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 void -gst_mpegts_demux_sync_streams (GstMpegTSDemux * demux, GstClockTime time) -{ - gint i; - - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *stream = demux->streams[i]; - if (!stream) - continue; - - /* Theoretically, we should be doing this for all streams, but we're only - * doing it for non A/V streams, for which data might not be forthcoming. */ - if (stream->flags & (MPEGTS_STREAM_FLAG_IS_AUDIO | - MPEGTS_STREAM_FLAG_IS_VIDEO)) - continue; - - /* at start, lock all streams onto the first timestamp */ - if (G_UNLIKELY (stream->last_time == 0)) - stream->last_time = time; - - /* Does this stream lag? Random threshold of 2 seconds */ - if (GST_CLOCK_DIFF (stream->last_time, time) > (2 * GST_SECOND)) { - /* If the pad was not added yet, do not wait any longer for - any pad that might be waiting for data */ - if (!stream->pad && demux->pending_pads > 0) { - demux->pending_pads = 0; - gst_mpegts_demux_no_more_pads (GST_ELEMENT (demux)); - } - - if (stream->pad) { - GST_DEBUG_OBJECT (stream, "synchronizing stream with others by " - "advancing time from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT, - GST_TIME_ARGS (stream->last_time), GST_TIME_ARGS (time)); - stream->last_time = time; - /* advance stream time (FIXME: is this right, esp. time_pos?) */ - gst_pad_push_event (stream->pad, - gst_event_new_new_segment (TRUE, 1.0, - GST_FORMAT_TIME, stream->last_time, -1, stream->last_time)); - } - } - } -} - -/* Attempts to add all known streams. - Returns TRUE if all could be added, FALSE otherwise. - */ -static gboolean -gst_mpegts_demux_add_all_streams (GstMpegTSDemux * demux, GstClockTime pts) -{ - guint i; - GstPad *srcpad; - gboolean all_added = TRUE; - - GST_DEBUG_OBJECT (demux, "Adding streams early fixes a wedge in some low " - "bitrate streams, but causes deadlocks - disabled for now"); - return FALSE; - - /* When adding a stream, require either a valid base PCR, or a valid PTS */ - if (!gst_mpegts_demux_setup_base_pts (demux, pts)) { - GST_ERROR ("Can't set base pts"); - return FALSE; - } - - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *stream = demux->streams[i]; - if (!stream || stream->pad) - continue; - - GST_DEBUG_OBJECT (demux, "Trying to add pad for PID 0x%04x", stream->PID); - - if (demux->current_PMT == 0) { - if (G_UNLIKELY (stream->flags & MPEGTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN)) { - GST_DEBUG_OBJECT (demux, - "Stream flagged as unknown, cannot be added now"); - all_added = FALSE; - continue; - } - } - if (!gst_mpegts_demux_fill_stream (stream, stream->filter.id, - stream->stream_type)) { - GST_WARNING_OBJECT (demux, "Unknown type for PID 0x%04x", stream->PID); - /* ignore */ - continue; - } - - GST_DEBUG_OBJECT (demux, - "New stream 0x%04x of type 0x%02x 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; - - stream->discont = TRUE; - - /* send new_segment */ - gst_mpegts_demux_send_new_segment (demux, stream, pts); - - /* send tags */ - gst_mpegts_demux_send_tags_for_stream (demux, stream); - - } - - return all_added; -} - -static GstFlowReturn -gst_mpegts_demux_data_cb (GstPESFilter * filter, gboolean first, - GstBuffer * buffer, GstMpegTSStream * stream) -{ - GstMpegTSDemux *demux; - GstFlowReturn ret; - GstPad *srcpad; - gint64 pts; - GstClockTime time; - - demux = stream->demux; - srcpad = stream->pad; - - GST_DEBUG_OBJECT (demux, "got data on PID 0x%04x (flags %x)", stream->PID, - stream->flags); - - if (first && filter->pts != -1) { - gint64 discont_threshold = - ((stream->flags & (MPEGTS_STREAM_FLAG_IS_AUDIO | - MPEGTS_STREAM_FLAG_IS_VIDEO))) ? DISCONT_THRESHOLD_AV : - DISCONT_THRESHOLD_OTHER; - pts = filter->pts; - time = MPEGTIME_TO_GSTTIME (pts) + stream->base_time; - - if ((stream->last_time > 0 && stream->last_time < time && - time - stream->last_time > discont_threshold) - || (stream->last_time > time - && stream->last_time - time > discont_threshold)) { - /* 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 <= MPEGTS_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 + discont_threshold) { - 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 <= MPEGTS_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) + - discont_threshold < 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; - } - - if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (demux->in_gap))) { - if (GST_CLOCK_TIME_IS_VALID (demux->first_buf_ts) - && GST_CLOCK_TIME_IS_VALID (filter->pts) - && gst_mpegts_demux_is_live (demux)) { - int i; - GstClockTime pts = GST_CLOCK_TIME_NONE; - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *stream = demux->streams[i]; - if (stream && stream->last_time > 0 && (pts == GST_CLOCK_TIME_NONE - || stream->last_time < pts)) { - pts = stream->last_time; - } - } - if (pts == GST_CLOCK_TIME_NONE) - pts = 0; - demux->in_gap = demux->first_buf_ts - pts; - GST_INFO_OBJECT (demux, "Setting interpolation gap to %" GST_TIME_FORMAT, - GST_TIME_ARGS (demux->in_gap)); - } else { - demux->in_gap = 0; - } - } - - if (GST_CLOCK_TIME_IS_VALID (time)) { - time += demux->in_gap; - } - - GST_DEBUG_OBJECT (demux, "setting PTS to (%" G_GUINT64_FORMAT ") time: %" - GST_TIME_FORMAT " on buffer %p first buffer: %d base_time: %" - GST_TIME_FORMAT, pts, GST_TIME_ARGS (time + demux->in_gap), - buffer, first, GST_TIME_ARGS (stream->base_time)); - - GST_BUFFER_TIMESTAMP (buffer) = time; - - /* check if we have a pad already */ - if (!demux->tried_adding_pads) { - GST_DEBUG_OBJECT (demux, "Trying to add all pads now"); - if (gst_mpegts_demux_add_all_streams (demux, pts)) { - /* We managed to add all pads, so we can signal no-more-pads safely. - If not, we'll add pads as we get data for them, and will end up - hitting decodebin2's overrun threshold (if using decodebin2) */ - GST_DEBUG_OBJECT (demux, "All pads added, we can signal no-more-pads"); - gst_mpegts_demux_no_more_pads (GST_ELEMENT (demux)); - } else { - GST_DEBUG_OBJECT (demux, - "All pads could not be added, we will not signal no-more-pads"); - } - demux->tried_adding_pads = TRUE; - } - - - srcpad = stream->pad; - if (srcpad == NULL) { - GST_DEBUG_OBJECT (demux, "srcpad is NULL, trying to add pad"); - /* When adding a stream, require either a valid base PCR, or a valid PTS */ - if (!gst_mpegts_demux_setup_base_pts (demux, pts)) - goto bad_timestamp; - - /* 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 & MPEGTS_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 &= ~MPEGTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN; - stream->flags |= MPEGTS_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 &= ~MPEGTS_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_mpegts_demux_fill_stream (stream, filter->id, stream->stream_type)) - goto unknown_type; - - GST_DEBUG_OBJECT (demux, - "New stream 0x%04x of type 0x%02x 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->pending_pads--; - GST_DEBUG_OBJECT (demux, - "Adding pad due to received data, decreasing pending pads to %d", - demux->pending_pads); - if (demux->pending_pads == 0) - gst_mpegts_demux_no_more_pads (GST_ELEMENT (demux)); - - stream->discont = TRUE; - - /* send new_segment */ - gst_mpegts_demux_send_new_segment (demux, stream, pts); - - /* send tags */ - gst_mpegts_demux_send_tags_for_stream (demux, stream); - } - - GST_DEBUG_OBJECT (srcpad, "pushing buffer ts %" GST_TIME_FORMAT, - GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer))); - gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad)); - if (stream->discont) { - GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT); - stream->discont = FALSE; - } - ret = gst_pad_push (srcpad, buffer); - ret = gst_mpegts_demux_combine_flows (demux, stream, ret); - - if (GST_CLOCK_TIME_IS_VALID (time)) - gst_mpegts_demux_sync_streams (demux, time); - - return ret; - - /* ERROR */ -unknown_type: - { - GST_DEBUG_OBJECT (demux, "got unknown stream id 0x%02x, type 0x%02x", - filter->id, stream->stream_type); - gst_buffer_unref (buffer); - return gst_mpegts_demux_combine_flows (demux, stream, GST_FLOW_NOT_LINKED); - } -bad_timestamp: - { - gst_buffer_unref (buffer); - return gst_mpegts_demux_combine_flows (demux, stream, GST_FLOW_OK); - } - -} - -static void -gst_mpegts_demux_resync_cb (GstPESFilter * filter, GstMpegTSStream * stream) -{ - /* does nothing for now */ -} - -/* - * 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; idemux; - - GST_DEBUG_OBJECT (demux, "parsing CA section"); - return TRUE; -} - -static void -gst_mpegts_activate_pmt (GstMpegTSDemux * demux, GstMpegTSStream * stream) -{ - GST_DEBUG_OBJECT (demux, "activating PMT 0x%08x", stream->PID); - - /* gst_mpegts_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; idemux; - - if (G_UNLIKELY (*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 (G_UNLIKELY (gst_mpegts_demux_calc_crc32 (data - 3, datalen) != 0)) - goto wrong_crc; - - GST_LOG_OBJECT (demux, "PMT section_length: %d", datalen - 3); - - PMT = &stream->PMT; - - /* check if version number changed */ - version_number = (data[2] & 0x3e) >> 1; - GST_LOG_OBJECT (demux, "PMT version_number: %d", version_number); - - current_next_indicator = (data[2] & 0x01); - GST_LOG_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_mpegts_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 (GstMpegTSPMTEntry)); - - GST_DEBUG_OBJECT (demux, "Resetting pending pads due to parsing the PMT"); - demux->pending_pads = 0; - while (entries > 0) { - GstMpegTSPMTEntry entry; - GstMpegTSStream *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_mpegts_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 */ - /* hack for ITV HD (sid 10510, video pid 3401 */ - if (program_number == 10510 && entry.PID == 3401 && - stream_type == ST_PRIVATE_DATA) - stream_type = ST_VIDEO_H264; - ES_stream->stream_type = stream_type; - ES_stream->flags &= ~MPEGTS_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 */ - GstCaps *caps; - gchar name[13]; - - g_snprintf (name, sizeof (name), "private_%04x", entry.PID); - 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, - name); - gst_pad_set_active (ES_stream->pad, TRUE); - caps = gst_caps_new_simple ("application/x-mpegts-private-section", - NULL); - gst_pad_use_fixed_caps (ES_stream->pad); - gst_pad_set_caps (ES_stream->pad, caps); - gst_caps_unref (caps); - - gst_element_add_pad (GST_ELEMENT_CAST (demux), ES_stream->pad); - } else { - /* Recognise video streams based on stream_type */ - if (gst_mpegts_stream_is_video (ES_stream)) - ES_stream->flags |= MPEGTS_STREAM_FLAG_IS_VIDEO; - /* likewise for audio */ - if (gst_mpegts_stream_is_audio (ES_stream)) - ES_stream->flags |= MPEGTS_STREAM_FLAG_IS_AUDIO; - - /* set adaptor */ - GST_LOG ("Initializing PES filter for PID %u", ES_stream->PID); - gst_pes_filter_init (&ES_stream->filter, NULL, NULL); - - if (ES_stream->stream_type == ST_PRIVATE_DATA) { - guint8 *dvb_sub_desc = gst_mpeg_descriptor_find (ES_stream->ES_info, - DESC_DVB_SUBTITLING); - - /* enable gather PES for DVB subtitles since the dvbsuboverlay - * expects complete PES packets */ - if (dvb_sub_desc) { - /* FIXME: There's another place where pes filters could get - * initialized. Might need similar temporary hack there as well */ - ES_stream->filter.gather_pes = TRUE; - } - } - - ++demux->pending_pads; - GST_DEBUG_OBJECT (demux, - "Setting data callback, increasing pending pads to %d", - demux->pending_pads); - - gst_pes_filter_set_callbacks (&ES_stream->filter, - (GstPESFilterData) gst_mpegts_demux_data_cb, - (GstPESFilterResync) gst_mpegts_demux_resync_cb, ES_stream); - if (ES_stream->flags & MPEGTS_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_mpegts_activate_pmt (demux, stream); - } else { - /* Program specified, activate this if it matches */ - if (demux->program_number == PMT->program_number) - gst_mpegts_activate_pmt (demux, stream); - } - - GST_DEBUG_OBJECT (demux, "Done parsing PMT, pending pads now %d", - demux->pending_pads); - if (demux->pending_pads == 0) - gst_mpegts_demux_no_more_pads (GST_ELEMENT (demux)); - - 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;idemux; - - if (demux->check_crc) - if (gst_mpegts_demux_calc_crc32 (data, datalen) != 0) - goto wrong_crc; - - /* just dump this down the pad */ - buffer = gst_buffer_new_and_alloc (datalen); - memcpy (buffer->data, data, datalen); - 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; idemux; - - 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_LOG_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 "(%" GST_TIME_FORMAT ") on PID 0x%04x " - "and last pcr is %" G_GUINT64_FORMAT " (%" GST_TIME_FORMAT ")", pcr, - GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (pcr)), stream->PID, - stream->last_PCR, - GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (stream->last_PCR))); - /* pcr has been converted into units of 90Khz ticks - * so assume discont if last pcr was > 90000 (1 second) lower */ - if (stream->last_PCR != -1 && - (pcr - stream->last_PCR > 90000 || 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) * (MPEGTS_MAX_PID + 1)); - - for (j = 0; j < MPEGTS_MAX_PID + 1; j++) { - if (demux->streams[j] - && demux->streams[j]->PMT_pid <= MPEGTS_MAX_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++) { - GstMpegTSPMTEntry *cur_entry = - &g_array_index (pmt_stream->PMT.entries, - GstMpegTSPMTEntry, 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 <= MPEGTS_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) { - GstMpegTSStream *PMT_stream = demux->streams[demux->current_PMT]; - - if (PMT_stream && PMT_stream->PMT.PCR_PID == stream->PID) { - if (demux->pcr[0] == -1) { - GST_DEBUG ("RECORDING pcr[0]:%" G_GUINT64_FORMAT, pcr); - demux->pcr[0] = pcr; - demux->num_packets = 0; - } /* Considering a difference of 1 sec ie 90000 ticks */ - else if (G_UNLIKELY (demux->pcr[1] == -1 - && ((pcr - demux->pcr[0]) >= 90000))) { - GST_DEBUG ("RECORDING pcr[1]:%" G_GUINT64_FORMAT, pcr); - demux->pcr[1] = pcr; - } - } - stream->last_PCR = pcr; - - if (demux->clock && demux->clock_base != GST_CLOCK_TIME_NONE) { - gdouble r_squared; - GstMpegTSStream *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); - } - } - } - 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; idemux; - - if (datalen < 8) - return FALSE; - - if (*data++ != 0x00) - goto wrong_id; - if ((data[0] & 0xc0) != 0x80) - goto wrong_sync; - if (G_UNLIKELY ((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_mpegts_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 (G_UNLIKELY (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 (GstMpegTSPATEntry), entries); - - while (entries--) { - GstMpegTSPATEntry entry; - GstMpegTSStream *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_mpegts_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_mpegts_demux_is_PMT (GstMpegTSDemux * demux, guint16 PID) -{ - GstMpegTSStream *stream; - GstMpegTSPAT *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++) { - GstMpegTSPATEntry *entry; - - entry = &g_array_index (PAT->entries, GstMpegTSPATEntry, i); - if (!entry) - continue; - - if (entry->PID == PID) - return TRUE; - } - return FALSE; -} - -static FORCE_INLINE GstFlowReturn -gst_mpegts_stream_pes_buffer_flush (GstMpegTSStream * stream, gboolean discard) -{ - GstFlowReturn ret = GST_FLOW_OK; - - if (stream->pes_buffer) { - if (discard) { - gst_buffer_unref (stream->pes_buffer); - stream->pes_buffer_in_sync = FALSE; - } else { - GST_BUFFER_SIZE (stream->pes_buffer) = stream->pes_buffer_used; - ret = gst_pes_filter_push (&stream->filter, stream->pes_buffer); - if (ret == GST_FLOW_LOST_SYNC) - stream->pes_buffer_in_sync = FALSE; - } - stream->pes_buffer = NULL; - } - return ret; -} - -static FORCE_INLINE GstFlowReturn -gst_mpegts_stream_pes_buffer_push (GstMpegTSStream * stream, - const guint8 * in_data, guint in_size) -{ - GstFlowReturn ret = GST_FLOW_OK; - guint8 *out_data; - - if (G_UNLIKELY (stream->pes_buffer - && stream->pes_buffer_used + in_size > stream->pes_buffer_size)) { - GST_DEBUG ("stream with PID 0x%04x have PES buffer full at %u bytes." - " Flushing and growing the buffer", - stream->PID, stream->pes_buffer_size); - stream->pes_buffer_overflow = TRUE; - if (stream->pes_buffer_size < (MPEGTS_MAX_PES_BUFFER_SIZE >> 1)) - stream->pes_buffer_size <<= 1; - - ret = gst_mpegts_stream_pes_buffer_flush (stream, FALSE); - if (ret == GST_FLOW_LOST_SYNC) - goto done; - } - - if (G_UNLIKELY (!stream->pes_buffer)) { - /* set initial size of PES buffer */ - if (G_UNLIKELY (stream->pes_buffer_size == 0)) - stream->pes_buffer_size = MPEGTS_MIN_PES_BUFFER_SIZE; - - stream->pes_buffer = gst_buffer_new_and_alloc (stream->pes_buffer_size); - stream->pes_buffer_used = 0; - } - out_data = GST_BUFFER_DATA (stream->pes_buffer) + stream->pes_buffer_used; - memcpy (out_data, in_data, in_size); - stream->pes_buffer_used += in_size; -done: - return ret; -} - -static FORCE_INLINE GstFlowReturn -gst_mpegts_demux_pes_buffer_flush (GstMpegTSDemux * demux, gboolean discard) -{ - gint i; - GstFlowReturn ret = GST_FLOW_OK; - - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *stream = demux->streams[i]; - if (stream && stream->pad) { - gst_mpegts_stream_pes_buffer_flush (stream, discard); - stream->pes_buffer_in_sync = FALSE; - } - } - return ret; -} - -static FORCE_INLINE GstFlowReturn -gst_mpegts_demux_push_fragment (GstMpegTSStream * stream, - const guint8 * in_data, guint in_size) -{ - GstFlowReturn ret; - GstBuffer *es_buf = gst_buffer_new_and_alloc (in_size); - memcpy (GST_BUFFER_DATA (es_buf), in_data, in_size); - ret = gst_pes_filter_push (&stream->filter, es_buf); - - /* If PES filter return ok then PES fragment buffering - * can be enabled */ - if (ret == GST_FLOW_OK) - stream->pes_buffer_in_sync = TRUE; - else if (ret == GST_FLOW_LOST_SYNC) - stream->pes_buffer_in_sync = FALSE; - return ret; -} - -/* - * 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;iPID; - transport_scrambling_control = (data[2] & 0xc0) >> 6; - adaptation_field_control = (data[2] & 0x30) >> 4; - continuity_counter = data[2] & 0x0f; - - data += 3; - datalen -= 3; - - GST_LOG_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_mpegts_demux_parse_adaptation_field (stream, data, - datalen, &consumed)) - goto done; - - if (datalen <= consumed) - goto too_small; - - data += consumed; - datalen -= consumed; - GST_LOG_OBJECT (demux, "consumed: %u datalen: %u", consumed, datalen); - } - - /* If this packet has a payload, handle it */ - if (adaptation_field_control & 0x1) { - GST_LOG_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 (G_UNLIKELY (stream->PID_type == PID_TYPE_UNKNOWN)) { - if (mpegts_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 |= MPEGTS_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_LOG ("Initializing PES filter for PID %u", stream->PID); - gst_pes_filter_init (&stream->filter, NULL, NULL); - gst_pes_filter_set_callbacks (&stream->filter, - (GstPESFilterData) gst_mpegts_demux_data_cb, - (GstPESFilterResync) gst_mpegts_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); - memcpy (GST_BUFFER_DATA (sec_buf), data, datalen); - 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_mpegts_stream_parse_pat (stream, section_data, - section_length + 3); - break; - case PID_TYPE_CONDITIONAL_ACCESS: - gst_mpegts_stream_parse_cat (stream, section_data, - section_length + 3); - break; - case PID_TYPE_PROGRAM_MAP: - gst_mpegts_stream_parse_pmt (stream, section_data, - section_length + 3); - break; - case PID_TYPE_PRIVATE_SECTION: - gst_mpegts_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: - { - if (payload_unit_start_indicator) { - GST_DEBUG_OBJECT (demux, "new PES start for PID 0x%04x, used %u " - "bytes of %u bytes in the PES buffer", - PID, stream->pes_buffer_used, stream->pes_buffer_size); - /* Flush buffered PES data */ - gst_mpegts_stream_pes_buffer_flush (stream, FALSE); - gst_pes_filter_drain (&stream->filter); - /* Resize the buffer to half if no overflow detected and - * had been used less than half of it */ - if (stream->pes_buffer_overflow == FALSE - && stream->pes_buffer_used < (stream->pes_buffer_size >> 1)) { - stream->pes_buffer_size >>= 1; - if (stream->pes_buffer_size < MPEGTS_MIN_PES_BUFFER_SIZE) - stream->pes_buffer_size = MPEGTS_MIN_PES_BUFFER_SIZE; - GST_DEBUG_OBJECT (demux, "PES buffer size reduced to %u bytes", - stream->pes_buffer_size); - } - /* mark the stream not in sync to give a chance on PES filter to - * detect lost sync */ - stream->pes_buffer_in_sync = FALSE; - stream->pes_buffer_overflow = FALSE; - } - GST_LOG_OBJECT (demux, "Elementary packet of size %u for PID 0x%04x", - datalen, PID); - - if (datalen > 0) { - if (!stream->pes_buffer_in_sync) { - /* Push the first fragment to PES filter to have a chance to - * detect GST_FLOW_LOST_SYNC. - */ - GST_LOG_OBJECT (demux, "fragment directly pushed to PES filter"); - ret = gst_mpegts_demux_push_fragment (stream, data, datalen); - } else { - /* Otherwhise we buffer the PES fragment */ - ret = gst_mpegts_stream_pes_buffer_push (stream, data, datalen); - /* If sync is lost here is due a pes_buffer_flush and we can try - * to resync in the PES filter with the current fragment - */ - if (ret == GST_FLOW_LOST_SYNC) { - GST_LOG_OBJECT (demux, "resync, fragment pushed to PES filter"); - ret = gst_mpegts_demux_push_fragment (stream, data, datalen); - } - } - - 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_mpegts_demux_parse_transport_packet (GstMpegTSDemux * demux, - const guint8 * data) -{ - GstFlowReturn ret = GST_FLOW_OK; - guint16 PID; - GstMpegTSStream *stream; - - /* skip sync byte */ - data++; - - /* get PID */ - PID = ((data[0] & 0x1f) << 8) | data[1]; - - /* Skip NULL packets */ - if (G_UNLIKELY (PID == 0x1fff)) - goto beach; - - /* get the stream. */ - stream = gst_mpegts_demux_get_stream_for_PID (demux, PID); - - /* parse the stream */ - ret = gst_mpegts_demux_parse_stream (demux, stream, data, - MPEGTS_NORMAL_TS_PACKETSIZE - 1); - - if (demux->pcr[1] != -1 && demux->bitrate == -1) { - guint64 bitrate; - GST_DEBUG_OBJECT (demux, "pcr[0]:%" G_GUINT64_FORMAT, demux->pcr[0]); - GST_DEBUG_OBJECT (demux, "pcr[1]:%" G_GUINT64_FORMAT, demux->pcr[1]); - GST_DEBUG_OBJECT (demux, "diff in time %" GST_TIME_FORMAT, - GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->pcr[1] - demux->pcr[0]))); - GST_DEBUG_OBJECT (demux, "stream->last_PCR_difference: %" G_GUINT64_FORMAT - ", demux->num_packets %" G_GUINT64_FORMAT, - demux->pcr[1] - demux->pcr[0], demux->num_packets); - bitrate = gst_util_uint64_scale (GST_SECOND, - MPEGTS_NORMAL_TS_PACKETSIZE * demux->num_packets, - MPEGTIME_TO_GSTTIME (demux->pcr[1] - demux->pcr[0])); - /* somehow... I doubt a bitrate below one packet per second is valid */ - if (bitrate > MPEGTS_NORMAL_TS_PACKETSIZE - 1) { - demux->bitrate = bitrate; - GST_DEBUG_OBJECT (demux, "bitrate is %" G_GINT64_FORMAT - " bytes per second", demux->bitrate); - } else { - GST_WARNING_OBJECT (demux, "Couldn't compute valid bitrate, recomputing"); - demux->pcr[0] = demux->pcr[1] = -1; - demux->num_packets = -1; - } - } - -beach: - demux->num_packets++; - return ret; - - /* ERRORS */ -} - -static gboolean -gst_mpegts_demux_handle_seek_push (GstMpegTSDemux * demux, GstEvent * event) -{ - gboolean res = FALSE; - gdouble rate; - GstFormat format; - GstSeekFlags flags; - GstSeekType start_type, stop_type; - gint64 start, stop, 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 beach; - } - - GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first"); - - /* first try original format seek */ - res = gst_pad_push_event (demux->sinkpad, gst_event_ref (event)); - if (res == TRUE) - goto beach; - GST_DEBUG_OBJECT (demux, "seek - no upstream"); - - 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 beach; - } - - /* We need to convert to byte based seek and we need a scr_rate for that. */ - if (demux->bitrate == -1) { - GST_DEBUG_OBJECT (demux, "seek not possible, no bitrate"); - goto beach; - } - - GST_DEBUG_OBJECT (demux, "try with bitrate"); - - 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); - -beach: - gst_event_unref (event); - return res; -} - -static gboolean -gst_mpegts_demux_src_event (GstPad * pad, GstEvent * event) -{ - GstMpegTSDemux *demux = GST_MPEGTS_DEMUX (gst_pad_get_parent (pad)); - gboolean res = FALSE; - - GST_DEBUG_OBJECT (demux, "got event %s", - gst_event_type_get_name (GST_EVENT_TYPE (event))); - - switch (GST_EVENT_TYPE (event)) { - case GST_EVENT_SEEK: - res = gst_mpegts_demux_handle_seek_push (demux, event); - break; - default: - res = gst_pad_push_event (demux->sinkpad, event); - break; - } - - gst_object_unref (demux); - - return res; -} - -static void -gst_mpegts_demux_flush (GstMpegTSDemux * demux, gboolean discard) -{ - gint i; - GstMpegTSStream *PCR_stream; - GstMpegTSStream *PMT_stream; - - GST_DEBUG_OBJECT (demux, "flushing MPEG TS demuxer (discard %d)", discard); - - /* Start by flushing internal buffers */ - gst_mpegts_demux_pes_buffer_flush (demux, discard); - - /* Clear adapter */ - gst_adapter_clear (demux->adapter); - - /* Try resetting the last_PCR value as we will have a discont */ - if (demux->current_PMT == 0) - goto beach; - - PMT_stream = demux->streams[demux->current_PMT]; - if (PMT_stream == NULL) - goto beach; - - PCR_stream = demux->streams[PMT_stream->PMT.PCR_PID]; - if (PCR_stream == NULL) - goto beach; - - PCR_stream->last_PCR = -1; - - /* Reset last time of all streams */ - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *stream = demux->streams[i]; - - if (stream) { - stream->last_time = 0; - stream->discont = TRUE; - } - } - - -beach: - return; -} - -static gboolean -gst_mpegts_demux_send_event (GstMpegTSDemux * demux, GstEvent * event) -{ - gint i; - gboolean have_stream = FALSE, res = TRUE; - - for (i = 0; i < MPEGTS_MAX_PID + 1; i++) { - GstMpegTSStream *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_mpegts_demux_sink_event (GstPad * pad, GstEvent * event) -{ - GstMpegTSDemux *demux = GST_MPEGTS_DEMUX (gst_pad_get_parent (pad)); - gboolean res = FALSE; - - 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_mpegts_demux_send_event (demux, event); - break; - case GST_EVENT_FLUSH_STOP: - gst_adapter_clear (demux->adapter); - gst_mpegts_demux_flush (demux, TRUE); - res = gst_mpegts_demux_send_event (demux, event); - demux->in_gap = GST_CLOCK_TIME_NONE; - demux->first_buf_ts = GST_CLOCK_TIME_NONE; - demux->last_buf_ts = GST_CLOCK_TIME_NONE; - break; - case GST_EVENT_EOS: - gst_mpegts_demux_flush (demux, FALSE); - /* Send the EOS event on each stream */ - if (!(res = gst_mpegts_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: - { - gboolean update; - gdouble rate; - GstFormat format; - gint64 start, stop, time; - - gst_event_parse_new_segment (event, &update, &rate, &format, - &start, &stop, &time); - - gst_event_unref (event); - 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); - if (format == GST_FORMAT_BYTES && demux->bitrate != -1) { - gint64 tstart = 0, tstop = 0, pos = 0; - - if (demux->base_pts != GST_CLOCK_TIME_NONE) { - tstart = tstop = demux->base_pts; - } - tstart += BYTES_TO_GSTTIME (start); - tstop += BYTES_TO_GSTTIME (stop); - pos = BYTES_TO_GSTTIME (time); - - event = gst_event_new_new_segment (update, rate, - GST_FORMAT_TIME, tstart, tstop, pos); - GST_DEBUG_OBJECT (demux, "pushing time newsegment from %" - GST_TIME_FORMAT " to %" GST_TIME_FORMAT " pos %" GST_TIME_FORMAT, - GST_TIME_ARGS (tstart), GST_TIME_ARGS (tstop), GST_TIME_ARGS (pos)); - - res = gst_mpegts_demux_send_event (demux, event); - } - break; - } - default: - res = gst_mpegts_demux_send_event (demux, event); - break; - } - gst_object_unref (demux); - - return res; -} - -static gboolean -gst_mpegts_demux_is_live (GstMpegTSDemux * demux) -{ - GstQuery *query; - gboolean is_live = FALSE; - GstPad *peer; - - 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 gboolean -gst_mpegts_demux_provides_clock (GstElement * element) -{ - return gst_mpegts_demux_is_live (GST_MPEGTS_DEMUX (element)); -} - -static GstClock * -gst_mpegts_demux_provide_clock (GstElement * element) -{ - GstMpegTSDemux *demux = GST_MPEGTS_DEMUX (element); - - if (gst_mpegts_demux_provides_clock (element)) { - if (demux->clock == NULL) { - demux->clock = g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", - "MpegTSClock", NULL); - demux->clock_base = GST_CLOCK_TIME_NONE; - } - return gst_object_ref (demux->clock); - } - - return NULL; -} - -static const GstQueryType * -gst_mpegts_demux_src_pad_query_type (GstPad * pad) -{ - static const GstQueryType types[] = { - GST_QUERY_LATENCY, - GST_QUERY_DURATION, - GST_QUERY_SEEKING, - 0 - }; - - return types; -} - -static gboolean -gst_mpegts_demux_src_pad_query (GstPad * pad, GstQuery * query) -{ - GstMpegTSDemux *demux = GST_MPEGTS_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; - } - case GST_QUERY_DURATION: - { - GstFormat format; - GstPad *peer; - - gst_query_parse_duration (query, &format, NULL); - - /* Try query upstream first */ - peer = gst_pad_get_peer (demux->sinkpad); - if (peer) { - res = gst_pad_query (peer, query); - /* Try doing something with that query if it failed */ - if (!res && format == GST_FORMAT_TIME && demux->bitrate != -1) { - /* Try using cache first */ - if (GST_CLOCK_TIME_IS_VALID (demux->cache_duration)) { - GST_LOG_OBJECT (demux, "replying duration query from cache %" - GST_TIME_FORMAT, GST_TIME_ARGS (demux->cache_duration)); - gst_query_set_duration (query, GST_FORMAT_TIME, - demux->cache_duration); - res = TRUE; - } else { /* Query upstream and approximate */ - GstQuery *bquery = gst_query_new_duration (GST_FORMAT_BYTES); - gint64 duration = 0; - - /* Query peer for duration in bytes */ - res = gst_pad_query (peer, bquery); - if (res) { - /* Convert to time format */ - gst_query_parse_duration (bquery, &format, &duration); - GST_DEBUG_OBJECT (demux, "query on peer pad reported bytes %" - G_GUINT64_FORMAT, duration); - demux->cache_duration = BYTES_TO_GSTTIME (duration); - GST_DEBUG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT, - GST_TIME_ARGS (demux->cache_duration)); - gst_query_set_duration (query, GST_FORMAT_TIME, - demux->cache_duration); - } - gst_query_unref (bquery); - } - } else { - GST_WARNING_OBJECT (demux, "unsupported query format or no bitrate " - "yet to approximate duration from bytes"); - } - gst_object_unref (peer); - } - break; - } - case GST_QUERY_SEEKING:{ - GstFormat fmt; - - gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL); - if (fmt == GST_FORMAT_BYTES) { - /* Seeking in BYTES format not supported at all */ - gst_query_set_seeking (query, fmt, FALSE, -1, -1); - } else { - GstQuery *peerquery; - gboolean seekable; - - /* Then ask upstream */ - res = gst_pad_peer_query (demux->sinkpad, query); - if (res) { - /* If upstream can handle seeks we're done, if it - * can't we still have our TIME->BYTES conversion seek - */ - gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL); - if (seekable || fmt != GST_FORMAT_TIME) - goto beach; - } - - /* We can't say anything about seekability if we didn't - * have a second PCR yet because the bitrate is calculated - * from this - */ - if (demux->bitrate == -1 && demux->pcr[1] == -1) - goto beach; - - /* We can seek if upstream supports BYTES seeks and we - * have a bitrate - */ - peerquery = gst_query_new_seeking (GST_FORMAT_BYTES); - res = gst_pad_peer_query (demux->sinkpad, peerquery); - if (!res || demux->bitrate == -1) { - gst_query_set_seeking (query, fmt, FALSE, -1, -1); - } else { - gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL); - if (seekable) - gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1); - else - gst_query_set_seeking (query, fmt, FALSE, -1, -1); - } - - gst_query_unref (peerquery); - res = TRUE; - } - break; - } - default: - res = gst_pad_query_default (pad, query); - break; - } - -beach: - gst_object_unref (demux); - - return res; -} - -static FORCE_INLINE gint -is_mpegts_sync (const guint8 * in_data, const guint8 * end_data, - guint packetsize) -{ - guint ret = 0; - if (G_LIKELY (IS_MPEGTS_SYNC (in_data))) - return 100; - - if (in_data + packetsize < end_data - 5) { - if (G_LIKELY (IS_MPEGTS_SYNC (in_data + packetsize))) - ret += 50; - } - - if (in_data[0] == 0x47) { - ret += 25; - - if ((in_data[1] & 0x80) == 0x00) - ret += 10; - - if ((in_data[3] & 0x10) == 0x10) - ret += 5; - } - return ret; -} - -static inline void -gst_mpegts_demux_detect_packet_size (GstMpegTSDemux * demux, guint len) -{ - guint i, packetsize = 0; - - for (i = 1; i < len; i++) { - packetsize = demux->sync_lut[i] - demux->sync_lut[i - 1]; - if (packetsize == MPEGTS_NORMAL_TS_PACKETSIZE || - packetsize == MPEGTS_M2TS_TS_PACKETSIZE || - packetsize == MPEGTS_DVB_ASI_TS_PACKETSIZE || - packetsize == MPEGTS_ATSC_TS_PACKETSIZE) - goto done; - else - packetsize = 0; - } - -done: - demux->packetsize = (packetsize ? packetsize : MPEGTS_NORMAL_TS_PACKETSIZE); - GST_DEBUG_OBJECT (demux, "packet_size set to %d bytes", demux->packetsize); -} - -static FORCE_INLINE guint -gst_mpegts_demux_sync_scan (GstMpegTSDemux * demux, const guint8 * in_data, - guint size, guint * flush) -{ - guint sync_count = 0; - guint8 *ptr_data = (guint8 *) in_data; - guint packetsize = - (demux->packetsize ? demux->packetsize : MPEGTS_NORMAL_TS_PACKETSIZE); - const guint8 *end_scan = in_data + size - packetsize; - - /* Check if the LUT table is big enough */ - if (G_UNLIKELY (demux->sync_lut_len < (size / packetsize))) { - demux->sync_lut_len = size / packetsize; - if (demux->sync_lut) - g_free (demux->sync_lut); - demux->sync_lut = g_new0 (guint8 *, demux->sync_lut_len); - GST_DEBUG_OBJECT (demux, "created sync LUT table with %u entries", - demux->sync_lut_len); - } - - while (ptr_data <= end_scan && sync_count < demux->sync_lut_len) { - /* if sync code is found try to store it in the LUT */ - guint chance = is_mpegts_sync (ptr_data, end_scan, packetsize); - if (G_LIKELY (chance > 50)) { - /* skip paketsize bytes and try find next */ - demux->sync_lut[sync_count] = ptr_data; - sync_count++; - ptr_data += packetsize; - } else { - ptr_data++; - } - } - - if (G_UNLIKELY (!demux->packetsize)) - gst_mpegts_demux_detect_packet_size (demux, sync_count); - - *flush = MIN (ptr_data - in_data, size); - - return sync_count; -} - -static GstFlowReturn -gst_mpegts_demux_chain (GstPad * pad, GstBuffer * buffer) -{ - GstMpegTSDemux *demux = GST_MPEGTS_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_TIMESTAMP_IS_VALID (buffer)) { - GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer); - GST_DEBUG_OBJECT (demux, "Got chained buffer ts %" GST_TIME_FORMAT, - GST_TIME_ARGS (timestamp)); - - /* if we did not get a buffer for a while, assume the source has dried up, - and flush any stale data */ - if (GST_CLOCK_TIME_IS_VALID (demux->last_buf_ts)) { - GstClockTimeDiff dt = timestamp - demux->last_buf_ts; - if (dt < 0 || dt > GST_SECOND / 2) { - GST_INFO_OBJECT (demux, - "Input timestamp discontinuity (%" GST_TIME_FORMAT - "), flushing stale data", GST_TIME_ARGS (dt)); - gst_mpegts_demux_flush (demux, FALSE); - } - } - demux->last_buf_ts = timestamp; - - /* lock on the first valid buffer timestamp */ - if (G_UNLIKELY (demux->first_buf_ts == GST_CLOCK_TIME_NONE)) { - demux->first_buf_ts = timestamp; - GST_DEBUG_OBJECT (demux, "First timestamp is %" GST_TIME_FORMAT, - GST_TIME_ARGS (demux->first_buf_ts)); - } - } - - if (GST_BUFFER_IS_DISCONT (buffer)) { - GST_DEBUG_OBJECT (demux, - "Input buffer has DISCONT flag set, flushing data"); - gst_mpegts_demux_flush (demux, FALSE); - } - /* 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_mpegts_demux_sync_scan (demux, data, avail, &flush); - - /* process all packets */ - for (i = 0; i < sync_count; i++) { - ret = gst_mpegts_demux_parse_transport_packet (demux, demux->sync_lut[i]); - if (G_UNLIKELY (ret == GST_FLOW_LOST_SYNC - || ret == GST_FLOW_NEED_MORE_DATA)) { - ret = GST_FLOW_OK; - continue; - } - if (G_UNLIKELY (ret != GST_FLOW_OK)) { - flush = demux->sync_lut[i] - data + demux->packetsize; - flush = MIN (avail, flush); - goto done; - } - } - -done: - /* flush processed data */ - if (flush) { - GST_DEBUG_OBJECT (demux, "flushing %d/%d", flush, avail); - gst_adapter_flush (demux->adapter, flush); - } - - gst_object_unref (demux); - - return ret; -} - -static GstStateChangeReturn -gst_mpegts_demux_change_state (GstElement * element, GstStateChange transition) -{ - GstMpegTSDemux *demux = GST_MPEGTS_DEMUX (element); - GstStateChangeReturn result; - - - switch (transition) { - case GST_STATE_CHANGE_NULL_TO_READY: - demux->adapter = gst_adapter_new (); - 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_mpegts_demux_reset (demux); - break; - case GST_STATE_CHANGE_READY_TO_NULL: - g_object_unref (demux->adapter); - if (demux->sync_lut) - g_free (demux->sync_lut); - demux->sync_lut = NULL; - demux->sync_lut_len = 0; - break; - default: - break; - } - - return result; -} - -static GValueArray * -mpegts_demux_build_pat_info (GstMpegTSDemux * demux) -{ - GValueArray *vals = NULL; - GstMpegTSPAT *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++) { - GstMpegTSPATEntry *cur_entry = - &g_array_index (PAT->entries, GstMpegTSPATEntry, i); - GValue v = { 0, }; - MpegTsPatInfo *info_obj; - - info_obj = mpegts_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); - g_value_unset (&v); - } - return vals; -} - -static MpegTsPmtInfo * -mpegts_demux_build_pmt_info (GstMpegTSDemux * demux, guint16 pmt_pid) -{ - MpegTsPmtInfo *info_obj; - GstMpegTSPMT *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 = mpegts_pmt_info_new (PMT->program_number, PMT->PCR_PID, - PMT->version_number); - - for (i = 0; i < PMT->entries->len; i++) { - GstMpegTSStream *stream; - MpegTsPmtStreamInfo *stream_info; - GstMpegTSPMTEntry *cur_entry = - &g_array_index (PMT->entries, GstMpegTSPMTEntry, i); - - stream = demux->streams[cur_entry->PID]; - stream_info = - mpegts_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); - mpegts_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 */ - mpegts_pmt_stream_info_add_descriptor (stream_info, - (gchar *) desc, 2 + DESC_LENGTH (desc)); - } - } - mpegts_pmt_info_add_stream (info_obj, stream_info); - } - return info_obj; -} - -static void -gst_mpegts_demux_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec) -{ - GstMpegTSDemux *demux = GST_MPEGTS_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; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -gst_mpegts_demux_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec) -{ - GstMpegTSDemux *demux = GST_MPEGTS_DEMUX (object); - int i; - - switch (prop_id) { - case PROP_ES_PIDS: - if (demux->nb_elementary_pids == 0) { - g_value_set_static_string (value, ""); - } else { - GString *ts_pids; - - ts_pids = g_string_sized_new (32); - /* FIXME: align with property description which uses hex numbers? */ - g_string_append_printf (ts_pids, "%d", demux->elementary_pids[0]); - for (i = 1; i < demux->nb_elementary_pids; i++) { - g_string_append_printf (ts_pids, ":%d", demux->elementary_pids[i]); - } - g_value_take_string (value, g_string_free (ts_pids, FALSE)); - } - 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, mpegts_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, mpegts_demux_build_pmt_info (demux, - demux->current_PMT)); - } - break; - } - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -gboolean -gst_mpegts_demux_plugin_init (GstPlugin * plugin) -{ - if (!gst_element_register (plugin, "mpegtsdemux", - GST_RANK_SECONDARY, GST_TYPE_MPEGTS_DEMUX)) - return FALSE; - - return TRUE; -} diff --git a/gst/mpegdemux/gstmpegtsdemux.h b/gst/mpegdemux/gstmpegtsdemux.h deleted file mode 100644 index 935ab40..0000000 --- a/gst/mpegdemux/gstmpegtsdemux.h +++ /dev/null @@ -1,257 +0,0 @@ -/* - * 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.A. - * Portions created by Fluendo, S.A. are Copyright (C) 2005,2006,2007,2008,2009 - * Fluendo, S.A. All Rights Reserved. - * - * Contributor(s): Wim Taymans - */ - -#ifndef __GST_MPEGTS_DEMUX_H__ -#define __GST_MPEGTS_DEMUX_H__ - -#include -#include - -#include "gstmpegdesc.h" -#include "gstpesfilter.h" -#include "gstsectionfilter.h" - -G_BEGIN_DECLS - -#define MPEGTS_MIN_PES_BUFFER_SIZE 4 * 1024 -#define MPEGTS_MAX_PES_BUFFER_SIZE 256 * 1024 - -#define MPEGTS_MAX_PID 0x1fff -#define MPEGTS_NORMAL_TS_PACKETSIZE 188 -#define MPEGTS_M2TS_TS_PACKETSIZE 192 -#define MPEGTS_DVB_ASI_TS_PACKETSIZE 204 -#define MPEGTS_ATSC_TS_PACKETSIZE 208 - -#define IS_MPEGTS_SYNC(data) (((data)[0] == 0x47) && \ - (((data)[1] & 0x80) == 0x00) && \ - (((data)[3] & 0x10) == 0x10)) - -#define GST_TYPE_MPEGTS_DEMUX (gst_mpegts_demux_get_type()) -#define GST_MPEGTS_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\ - GST_TYPE_MPEGTS_DEMUX,GstMpegTSDemux)) -#define GST_MPEGTS_DEMUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),\ - GST_TYPE_MPEGTS_DEMUX,GstMpegTSDemuxClass)) -#define GST_MPEGTS_DEMUX_GET_CLASS(klass) (G_TYPE_INSTANCE_GET_CLASS((klass),\ - GST_TYPE_MPEGTS_DEMUX,GstMpegTSDemuxClass)) -#define GST_IS_MPEGTS_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),\ - GST_TYPE_MPEGTS_DEMUX)) -#define GST_IS_MPEGTS_DEMUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),\ - GST_TYPE_MPEGTS_DEMUX)) - -typedef struct _GstMpegTSStream GstMpegTSStream; -typedef struct _GstMpegTSPMTEntry GstMpegTSPMTEntry; -typedef struct _GstMpegTSPMT GstMpegTSPMT; -typedef struct _GstMpegTSPATEntry GstMpegTSPATEntry; -typedef struct _GstMpegTSPAT GstMpegTSPAT; -typedef struct _GstMpegTSDemux GstMpegTSDemux; -typedef struct _GstMpegTSDemuxClass GstMpegTSDemuxClass; - -struct _GstMpegTSPMTEntry { - guint16 PID; -}; - -struct _GstMpegTSPMT { - 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 _GstMpegTSPATEntry { - guint16 program_number; - guint16 PID; -}; - -struct _GstMpegTSPAT { - guint16 transport_stream_id; - guint8 version_number; - gboolean current_next_indicator; - guint8 section_number; - guint8 last_section_number; - - GArray * entries; -}; - -typedef enum _MpegTsStreamFlags { - MPEGTS_STREAM_FLAG_STREAM_TYPE_UNKNOWN = 0x01, - MPEGTS_STREAM_FLAG_PMT_VALID = 0x02, - MPEGTS_STREAM_FLAG_IS_VIDEO = 0x04, - MPEGTS_STREAM_FLAG_IS_AUDIO = 0x08 -} MpegTsStreamFlags; - -/* Information associated to a single MPEG stream. */ -struct _GstMpegTSStream { - GstMpegTSDemux * demux; - - MpegTsStreamFlags 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 */ - GstMpegTSPAT PAT; - - /* for PMT streams */ - GstMpegTSPMT PMT; - - /* for CA streams */ - - /* for PAT, PMT, CA and private streams */ - GstSectionFilter section_filter; - - /* for PES streams */ - guint8 id; - guint8 stream_type; - GstBuffer * pes_buffer; - guint32 pes_buffer_size; - guint32 pes_buffer_used; - gboolean pes_buffer_overflow; - gboolean pes_buffer_in_sync; - 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; - gboolean discont; -}; - -struct _GstMpegTSDemux { - GstElement parent; - - /* properties */ - gboolean check_crc; - - /* sink pad and adapter */ - GstPad * sinkpad; - GstAdapter * adapter; - guint8 ** sync_lut; - guint sync_lut_len; - - /* current PMT PID */ - guint16 current_PMT; - - /* Array of MPEGTS_MAX_PID + 1 stream entries */ - GstMpegTSStream ** streams; - /* Array to perform pmts checks at gst_mpegts_demux_parse_adaptation_field */ - gboolean pmts_checked[MPEGTS_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; - gint packetsize; - gboolean m2ts_mode; - /* clocking */ - GstClock * clock; - GstClockTime clock_base; - - /* Additional information required for seeking. - * num_packets: Number of packets outputted - * bitrate: estimated bitrate (based on pcr and num_packets */ - guint64 num_packets; - gint64 bitrate; - - /* Two PCRs observations to calculate bitrate */ - guint64 pcr[2]; - - /* Cached duration estimation */ - GstClockTime cache_duration; - - /* Cached base_PCR in GStreamer time. */ - GstClockTime base_pts; - - /* base timings on first buffer timestamp */ - GstClockTime first_buf_ts; - GstClockTime in_gap; - - /* Detect when the source stops for a while, we will resync the interpolation gap */ - GstClockTime last_buf_ts; - - /* Number of expected pads which have not been added yet */ - gint pending_pads; - - gboolean tried_adding_pads; -}; - -struct _GstMpegTSDemuxClass { - GstElementClass parent_class; - - GstPadTemplate * sink_template; - GstPadTemplate * video_template; - GstPadTemplate * audio_template; - GstPadTemplate * subpicture_template; - GstPadTemplate * private_template; -}; - -GType gst_mpegts_demux_get_type (void); - -gboolean gst_mpegts_demux_plugin_init (GstPlugin *plugin); - -G_END_DECLS - -#endif /* __GST_MPEGTS_DEMUX_H__ */ diff --git a/gst/mpegdemux/gstsectionfilter.c b/gst/mpegdemux/gstsectionfilter.c deleted file mode 100644 index 6c8d7f4..0000000 --- a/gst/mpegdemux/gstsectionfilter.c +++ /dev/null @@ -1,164 +0,0 @@ - /* - * 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 - */ - -#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 > 4093) { - GST_DEBUG ("section length too big"); - goto failure; - } - 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); - goto failure; - } -failure: - gst_buffer_unref (buf); - return FALSE; -} diff --git a/gst/mpegdemux/gstsectionfilter.h b/gst/mpegdemux/gstsectionfilter.h deleted file mode 100644 index e26e3b2..0000000 --- a/gst/mpegdemux/gstsectionfilter.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 - */ - -#ifndef __GST_SECTION_FILTER_H__ -#define __GST_SECTION_FILTER_H__ - -#include -#include - -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__ */ diff --git a/gst/mpegdemux/mpegtspacketizer.c b/gst/mpegdemux/mpegtspacketizer.c deleted file mode 100644 index 089254d..0000000 --- a/gst/mpegdemux/mpegtspacketizer.c +++ /dev/null @@ -1,2612 +0,0 @@ -/* - * mpegtspacketizer.c - - * Copyright (C) 2007, 2008 Alessandro Decina, Zaheer Merali - * - * Authors: - * Zaheer Merali - * Alessandro Decina - * - * 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. - */ - -/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex - * with newer GLib versions (>= 2.31.0) */ -#define GLIB_DISABLE_DEPRECATION_WARNINGS - -#include - -#include "mpegtspacketizer.h" -#include "gstmpegdesc.h" - -GST_DEBUG_CATEGORY_STATIC (mpegts_packetizer_debug); -#define GST_CAT_DEFAULT mpegts_packetizer_debug - -static GQuark QUARK_PAT; -static GQuark QUARK_TRANSPORT_STREAM_ID; -static GQuark QUARK_PROGRAM_NUMBER; -static GQuark QUARK_PID; -static GQuark QUARK_PROGRAMS; - -static GQuark QUARK_PMT; -static GQuark QUARK_PCR_PID; -static GQuark QUARK_VERSION_NUMBER; -static GQuark QUARK_DESCRIPTORS; -static GQuark QUARK_STREAM_TYPE; -static GQuark QUARK_STREAMS; - -static GQuark QUARK_NIT; -static GQuark QUARK_NETWORK_ID; -static GQuark QUARK_CURRENT_NEXT_INDICATOR; -static GQuark QUARK_ACTUAL_NETWORK; -static GQuark QUARK_NETWORK_NAME; -static GQuark QUARK_ORIGINAL_NETWORK_ID; -static GQuark QUARK_TRANSPORTS; - -static GQuark QUARK_SDT; -static GQuark QUARK_ACTUAL_TRANSPORT_STREAM; -static GQuark QUARK_SERVICES; - -static GQuark QUARK_EIT; -static GQuark QUARK_SERVICE_ID; -static GQuark QUARK_PRESENT_FOLLOWING; -static GQuark QUARK_SEGMENT_LAST_SECTION_NUMBER; -static GQuark QUARK_LAST_TABLE_ID; -static GQuark QUARK_EVENTS; - -static void _init_local (void); -G_DEFINE_TYPE_EXTENDED (MpegTSPacketizer, mpegts_packetizer, G_TYPE_OBJECT, 0, - _init_local ()); - -static void mpegts_packetizer_dispose (GObject * object); -static void mpegts_packetizer_finalize (GObject * object); -static gchar *convert_to_utf8 (const gchar * text, gint length, guint start, - const gchar * encoding, gboolean is_multibyte, GError ** error); -static gchar *get_encoding (const gchar * text, guint * start_text, - gboolean * is_multibyte); -static gchar *get_encoding_and_convert (const gchar * text, guint length); - -#define CONTINUITY_UNSET 255 -#define MAX_CONTINUITY 15 -#define VERSION_NUMBER_UNSET 255 -#define TABLE_ID_UNSET 0xFF - -static gint -mpegts_packetizer_stream_subtable_compare (gconstpointer a, gconstpointer b) -{ - MpegTSPacketizerStreamSubtable *asub, *bsub; - - asub = (MpegTSPacketizerStreamSubtable *) a; - bsub = (MpegTSPacketizerStreamSubtable *) b; - - if (asub->table_id == bsub->table_id && - asub->subtable_extension == bsub->subtable_extension) - return 0; - return -1; -} - -static MpegTSPacketizerStreamSubtable * -mpegts_packetizer_stream_subtable_new (guint8 table_id, - guint16 subtable_extension) -{ - MpegTSPacketizerStreamSubtable *subtable; - - subtable = g_new0 (MpegTSPacketizerStreamSubtable, 1); - subtable->version_number = VERSION_NUMBER_UNSET; - subtable->table_id = table_id; - subtable->subtable_extension = subtable_extension; - subtable->crc = 0; - return subtable; -} - -static MpegTSPacketizerStream * -mpegts_packetizer_stream_new (void) -{ - MpegTSPacketizerStream *stream; - - stream = (MpegTSPacketizerStream *) g_new0 (MpegTSPacketizerStream, 1); - stream->section_adapter = gst_adapter_new (); - stream->continuity_counter = CONTINUITY_UNSET; - stream->subtables = NULL; - stream->section_table_id = TABLE_ID_UNSET; - return stream; -} - -static void -mpegts_packetizer_stream_free (MpegTSPacketizerStream * stream) -{ - gst_adapter_clear (stream->section_adapter); - g_object_unref (stream->section_adapter); - g_slist_foreach (stream->subtables, (GFunc) g_free, NULL); - g_slist_free (stream->subtables); - g_free (stream); -} - -static void -mpegts_packetizer_clear_section (MpegTSPacketizer * packetizer, - MpegTSPacketizerStream * stream) -{ - gst_adapter_clear (stream->section_adapter); - stream->continuity_counter = CONTINUITY_UNSET; - stream->section_length = 0; - stream->section_table_id = TABLE_ID_UNSET; -} - -static void -mpegts_packetizer_class_init (MpegTSPacketizerClass * klass) -{ - GObjectClass *gobject_class; - - gobject_class = G_OBJECT_CLASS (klass); - - gobject_class->dispose = mpegts_packetizer_dispose; - gobject_class->finalize = mpegts_packetizer_finalize; -} - -static void -mpegts_packetizer_init (MpegTSPacketizer * packetizer) -{ - packetizer->adapter = gst_adapter_new (); - packetizer->streams = g_new0 (MpegTSPacketizerStream *, 8192); - packetizer->know_packet_size = FALSE; -} - -static void -mpegts_packetizer_dispose (GObject * object) -{ - MpegTSPacketizer *packetizer = GST_MPEGTS_PACKETIZER (object); - - if (!packetizer->disposed) { - if (packetizer->know_packet_size && packetizer->caps != NULL) { - gst_caps_unref (packetizer->caps); - packetizer->caps = NULL; - packetizer->know_packet_size = FALSE; - } - if (packetizer->streams) { - int i; - for (i = 0; i < 8192; i++) { - if (packetizer->streams[i]) - mpegts_packetizer_stream_free (packetizer->streams[i]); - } - g_free (packetizer->streams); - } - - gst_adapter_clear (packetizer->adapter); - g_object_unref (packetizer->adapter); - packetizer->disposed = TRUE; - } - - if (G_OBJECT_CLASS (mpegts_packetizer_parent_class)->dispose) - G_OBJECT_CLASS (mpegts_packetizer_parent_class)->dispose (object); -} - -static void -mpegts_packetizer_finalize (GObject * object) -{ - if (G_OBJECT_CLASS (mpegts_packetizer_parent_class)->finalize) - G_OBJECT_CLASS (mpegts_packetizer_parent_class)->finalize (object); -} - -static gboolean -mpegts_packetizer_parse_adaptation_field_control (MpegTSPacketizer * packetizer, - MpegTSPacketizerPacket * packet) -{ - guint8 length; - - length = *packet->data; - packet->data += 1; - - if (packet->adaptation_field_control == 0x02) { - /* no payload, adaptation field of 183 bytes */ - if (length != 183) { - GST_DEBUG ("PID %d afc == 0x%x and length %d != 183", - packet->pid, packet->adaptation_field_control, length); - } - } else if (length > 182) { - GST_DEBUG ("PID %d afc == 0x%01x and length %d > 182", - packet->pid, packet->adaptation_field_control, length); - } - - /* skip the adaptation field body for now */ - if (packet->data + length > packet->data_end) { - GST_DEBUG ("PID %d afc length %d overflows the buffer current %d max %d", - packet->pid, length, (gint) (packet->data - packet->data_start), - (gint) (packet->data_end - packet->data_start)); - return FALSE; - } - - packet->data += length; - - return TRUE; -} - -static gboolean -mpegts_packetizer_parse_packet (MpegTSPacketizer * packetizer, - MpegTSPacketizerPacket * packet) -{ - guint8 *data; - - data = GST_BUFFER_DATA (packet->buffer); - /* skip sync_byte */ - data++; - - packet->payload_unit_start_indicator = (*data >> 6) & 0x01; - packet->pid = GST_READ_UINT16_BE (data) & 0x1FFF; - data += 2; - - packet->adaptation_field_control = (*data >> 4) & 0x03; - packet->continuity_counter = *data & 0x0F; - data += 1; - - packet->data = data; - - if (packet->adaptation_field_control & 0x02) - if (!mpegts_packetizer_parse_adaptation_field_control (packetizer, packet)) - return FALSE; - - if (packet->adaptation_field_control & 0x01) - packet->payload = packet->data; - else - packet->payload = NULL; - - return TRUE; -} - -static gboolean -mpegts_packetizer_parse_section_header (MpegTSPacketizer * packetizer, - MpegTSPacketizerStream * stream, MpegTSPacketizerSection * section) -{ - guint8 tmp; - guint8 *data, *crc_data; - MpegTSPacketizerStreamSubtable *subtable; - GSList *subtable_list = NULL; - - section->complete = TRUE; - /* get the section buffer, pass the ownership to the caller */ - section->buffer = gst_adapter_take_buffer (stream->section_adapter, - 3 + stream->section_length); - data = GST_BUFFER_DATA (section->buffer); - - section->table_id = *data++; - /* if table_id is 0 (pat) then ignore the subtable extension */ - if ((data[0] & 0x80) == 0 || section->table_id == 0) - section->subtable_extension = 0; - else - section->subtable_extension = GST_READ_UINT16_BE (data + 2); - - subtable = mpegts_packetizer_stream_subtable_new (section->table_id, - section->subtable_extension); - - subtable_list = g_slist_find_custom (stream->subtables, subtable, - mpegts_packetizer_stream_subtable_compare); - if (subtable_list) { - g_free (subtable); - subtable = (MpegTSPacketizerStreamSubtable *) (subtable_list->data); - } else { - stream->subtables = g_slist_prepend (stream->subtables, subtable); - } - - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - /* skip to the version byte */ - data += 2; - - tmp = *data++; - section->version_number = (tmp >> 1) & 0x1F; - section->current_next_indicator = tmp & 0x01; - - if (!section->current_next_indicator) - goto not_applicable; - - /* CRC is at the end of the section */ - crc_data = - GST_BUFFER_DATA (section->buffer) + GST_BUFFER_SIZE (section->buffer) - 4; - section->crc = GST_READ_UINT32_BE (crc_data); - - if (section->version_number == subtable->version_number && - section->crc == subtable->crc) - goto not_applicable; - - subtable->version_number = section->version_number; - subtable->crc = section->crc; - stream->section_table_id = section->table_id; - - return TRUE; - -not_applicable: - GST_LOG - ("not applicable pid %d table_id %d subtable_extension %d, current_next %d version %d, crc 0x%x", - section->pid, section->table_id, section->subtable_extension, - section->current_next_indicator, section->version_number, section->crc); - section->complete = FALSE; - gst_buffer_unref (section->buffer); - return TRUE; -} - -static gboolean -mpegts_packetizer_parse_descriptors (MpegTSPacketizer * packetizer, - guint8 ** buffer, guint8 * buffer_end, GValueArray * descriptors) -{ - guint8 length; - guint8 *data; - GValue value = { 0 }; - GString *desc; - - data = *buffer; - - while (data < buffer_end) { - data++; /* skip tag */ - length = *data++; - - if (data + length > buffer_end) { - GST_WARNING ("invalid descriptor length %d now at %d max %d", length, - (gint) (data - *buffer), (gint) (buffer_end - *buffer)); - goto error; - } - - /* include length */ - desc = g_string_new_len ((gchar *) data - 2, length + 2); - data += length; - /* G_TYPE_GSTRING is a GBoxed type and is used so properly marshalled from - * python (FIXME: should either be G_TYPE_STRING or GST_TYPE_BUFFFER) */ - g_value_init (&value, G_TYPE_GSTRING); - g_value_take_boxed (&value, desc); - g_value_array_append (descriptors, &value); - g_value_unset (&value); - } - - if (data != buffer_end) { - GST_WARNING ("descriptors size %d expected %d", (gint) (data - *buffer), - (gint) (buffer_end - *buffer)); - goto error; - } - - *buffer = data; - - return TRUE; -error: - return FALSE; -} - -GstStructure * -mpegts_packetizer_parse_pat (MpegTSPacketizer * packetizer, - MpegTSPacketizerSection * section) -{ - GstStructure *pat_info = NULL; - guint8 *data, *end; - guint transport_stream_id; - guint8 tmp; - guint program_number; - guint pmt_pid; - GValue entries = { 0 }; - GValue value = { 0 }; - GstStructure *entry = NULL; - gchar *struct_name; - - data = GST_BUFFER_DATA (section->buffer); - - section->table_id = *data++; - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - transport_stream_id = GST_READ_UINT16_BE (data); - data += 2; - - tmp = *data++; - section->version_number = (tmp >> 1) & 0x1F; - section->current_next_indicator = tmp & 0x01; - - /* skip section_number and last_section_number */ - data += 2; - - pat_info = gst_structure_id_new (QUARK_PAT, - QUARK_TRANSPORT_STREAM_ID, G_TYPE_UINT, transport_stream_id, NULL); - g_value_init (&entries, GST_TYPE_LIST); - /* stop at the CRC */ - end = GST_BUFFER_DATA (section->buffer) + GST_BUFFER_SIZE (section->buffer); - while (data < end - 4) { - program_number = GST_READ_UINT16_BE (data); - data += 2; - - pmt_pid = GST_READ_UINT16_BE (data) & 0x1FFF; - data += 2; - - struct_name = g_strdup_printf ("program-%d", program_number); - entry = gst_structure_new (struct_name, NULL); - g_free (struct_name); - gst_structure_id_set (entry, QUARK_PROGRAM_NUMBER, G_TYPE_UINT, - program_number, QUARK_PID, G_TYPE_UINT, pmt_pid, NULL); - - g_value_init (&value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&value, entry); - gst_value_list_append_value (&entries, &value); - g_value_unset (&value); - } - - gst_structure_id_set_value (pat_info, QUARK_PROGRAMS, &entries); - g_value_unset (&entries); - - if (data != end - 4) { - /* FIXME: check the CRC before parsing the packet */ - GST_ERROR ("at the end of PAT data != end - 4"); - gst_structure_free (pat_info); - - return NULL; - } - - return pat_info; -} - -GstStructure * -mpegts_packetizer_parse_pmt (MpegTSPacketizer * packetizer, - MpegTSPacketizerSection * section) -{ - GstStructure *pmt = NULL; - guint8 *data, *end; - guint16 program_number; - guint8 tmp; - guint pcr_pid; - guint program_info_length; - guint8 stream_type; - guint16 pid; - guint stream_info_length; - GValueArray *descriptors; - GValue stream_value = { 0 }; - GValue programs = { 0 }; - GstStructure *stream_info = NULL; - gchar *struct_name; - - /* fixed header + CRC == 16 */ - if (GST_BUFFER_SIZE (section->buffer) < 16) { - GST_WARNING ("PID %d invalid PMT size %d", - section->pid, section->section_length); - goto error; - } - - data = GST_BUFFER_DATA (section->buffer); - end = data + GST_BUFFER_SIZE (section->buffer); - - section->table_id = *data++; - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - program_number = GST_READ_UINT16_BE (data); - data += 2; - - tmp = *data++; - section->version_number = (tmp >> 1) & 0x1F; - section->current_next_indicator = tmp & 0x01; - - /* skip section_number and last_section_number */ - data += 2; - - pcr_pid = GST_READ_UINT16_BE (data) & 0x1FFF; - data += 2; - - program_info_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - pmt = gst_structure_id_new (QUARK_PMT, - QUARK_PROGRAM_NUMBER, G_TYPE_UINT, program_number, - QUARK_PCR_PID, G_TYPE_UINT, pcr_pid, - QUARK_VERSION_NUMBER, G_TYPE_UINT, section->version_number, NULL); - - if (program_info_length) { - /* check that the buffer is large enough to contain at least - * program_info_length bytes + CRC */ - if (data + program_info_length + 4 > end) { - GST_WARNING ("PID %d invalid program info length %d left %d", - section->pid, program_info_length, (gint) (end - data)); - goto error; - } - - descriptors = g_value_array_new (0); - if (!mpegts_packetizer_parse_descriptors (packetizer, - &data, data + program_info_length, descriptors)) { - g_value_array_free (descriptors); - goto error; - } - - gst_structure_id_set (pmt, QUARK_DESCRIPTORS, G_TYPE_VALUE_ARRAY, - descriptors, NULL); - g_value_array_free (descriptors); - } - - g_value_init (&programs, GST_TYPE_LIST); - /* parse entries, cycle until there's space for another entry (at least 5 - * bytes) plus the CRC */ - while (data <= end - 4 - 5) { - stream_type = *data++; - - pid = GST_READ_UINT16_BE (data) & 0x1FFF; - data += 2; - - stream_info_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - if (data + stream_info_length + 4 > end) { - GST_WARNING ("PID %d invalid stream info length %d left %d", section->pid, - stream_info_length, (gint) (end - data)); - g_value_unset (&programs); - goto error; - } - - struct_name = g_strdup_printf ("pid-%d", pid); - stream_info = gst_structure_new (struct_name, NULL); - g_free (struct_name); - gst_structure_id_set (stream_info, - QUARK_PID, G_TYPE_UINT, pid, QUARK_STREAM_TYPE, G_TYPE_UINT, - stream_type, NULL); - - if (stream_info_length) { - /* check for AC3 descriptor */ - GstMPEGDescriptor *desc = - gst_mpeg_descriptor_parse (data, stream_info_length); - if (desc != NULL) { - guint8 *desc_data; - if (gst_mpeg_descriptor_find (desc, DESC_DVB_AC3)) { - gst_structure_set (stream_info, "has-ac3", G_TYPE_BOOLEAN, TRUE, - NULL); - } - desc_data = gst_mpeg_descriptor_find (desc, DESC_DVB_DATA_BROADCAST_ID); - if (desc_data) { - guint16 data_broadcast_id; - data_broadcast_id = - DESC_DVB_DATA_BROADCAST_ID_data_broadcast_id (desc_data); - gst_structure_set (stream_info, "data-broadcast-id", G_TYPE_UINT, - data_broadcast_id, NULL); - } - desc_data = gst_mpeg_descriptor_find (desc, DESC_DVB_DATA_BROADCAST); - if (desc_data) { - GstStructure *databroadcast_info; - guint16 data_broadcast_id; - guint8 component_tag; - data_broadcast_id = - DESC_DVB_DATA_BROADCAST_data_broadcast_id (desc_data); - component_tag = DESC_DVB_DATA_BROADCAST_component_tag (desc_data); - databroadcast_info = gst_structure_new ("data-broadcast", "id", - G_TYPE_UINT, data_broadcast_id, "component-tag", component_tag, - NULL); - gst_structure_set (stream_info, "data-broadcast", GST_TYPE_STRUCTURE, - databroadcast_info, NULL); - } - desc_data = - gst_mpeg_descriptor_find (desc, DESC_DVB_CAROUSEL_IDENTIFIER); - if (desc_data) { - guint32 carousel_id; - carousel_id = DESC_DVB_CAROUSEL_IDENTIFIER_carousel_id (desc_data); - gst_structure_set (stream_info, "carousel-id", G_TYPE_UINT, - carousel_id, NULL); - } - desc_data = gst_mpeg_descriptor_find (desc, DESC_DVB_STREAM_IDENTIFIER); - if (desc_data) { - guint8 component_tag; - component_tag = DESC_DVB_STREAM_IDENTIFIER_component_tag (desc_data); - gst_structure_set (stream_info, "component-tag", G_TYPE_UINT, - component_tag, NULL); - } - desc_data = gst_mpeg_descriptor_find (desc, DESC_ISO_639_LANGUAGE); - if (desc_data && DESC_ISO_639_LANGUAGE_codes_n (desc_data)) { - gchar *lang_code; - gchar *language_n = (gchar *) - DESC_ISO_639_LANGUAGE_language_code_nth (desc_data, 0); - lang_code = g_strndup (language_n, 3); - gst_structure_set (stream_info, "lang-code", G_TYPE_STRING, - lang_code, NULL); - g_free (lang_code); - } - - gst_mpeg_descriptor_free (desc); - } - - descriptors = g_value_array_new (0); - if (!mpegts_packetizer_parse_descriptors (packetizer, - &data, data + stream_info_length, descriptors)) { - g_value_unset (&programs); - gst_structure_free (stream_info); - g_value_array_free (descriptors); - goto error; - } - - gst_structure_id_set (stream_info, - QUARK_DESCRIPTORS, G_TYPE_VALUE_ARRAY, descriptors, NULL); - g_value_array_free (descriptors); - - } - - g_value_init (&stream_value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&stream_value, stream_info); - gst_value_list_append_value (&programs, &stream_value); - g_value_unset (&stream_value); - } - - gst_structure_id_set_value (pmt, QUARK_STREAMS, &programs); - g_value_unset (&programs); - - g_assert (data == end - 4); - - return pmt; - -error: - if (pmt) - gst_structure_free (pmt); - - return NULL; -} - -GstStructure * -mpegts_packetizer_parse_nit (MpegTSPacketizer * packetizer, - MpegTSPacketizerSection * section) -{ - GstStructure *nit = NULL, *transport = NULL, *delivery_structure = NULL; - guint8 *data, *end, *entry_begin; - guint16 network_id, transport_stream_id, original_network_id; - guint tmp; - guint16 descriptors_loop_length, transport_stream_loop_length; - GValue transports = { 0 }; - GValue transport_value = { 0 }; - GValueArray *descriptors = NULL; - - GST_DEBUG ("NIT"); - /* fixed header + CRC == 16 */ - if (GST_BUFFER_SIZE (section->buffer) < 23) { - GST_WARNING ("PID %d invalid NIT size %d", - section->pid, section->section_length); - goto error; - } - - data = GST_BUFFER_DATA (section->buffer); - end = data + GST_BUFFER_SIZE (section->buffer); - - section->table_id = *data++; - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - if (data + section->section_length != end) { - GST_WARNING ("PID %d invalid NIT section length %d expected %d", - section->pid, section->section_length, (gint) (end - data)); - goto error; - } - - network_id = GST_READ_UINT16_BE (data); - data += 2; - - tmp = *data++; - section->version_number = (tmp >> 1) & 0x1F; - section->current_next_indicator = tmp & 0x01; - - /* skip section_number and last_section_number */ - data += 2; - - descriptors_loop_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - nit = gst_structure_id_new (QUARK_NIT, - QUARK_NETWORK_ID, G_TYPE_UINT, network_id, - QUARK_VERSION_NUMBER, G_TYPE_UINT, section->version_number, - QUARK_CURRENT_NEXT_INDICATOR, G_TYPE_UINT, - section->current_next_indicator, QUARK_ACTUAL_NETWORK, G_TYPE_BOOLEAN, - section->table_id == 0x40, NULL); - - /* see if the buffer is large enough */ - if (descriptors_loop_length) { - guint8 *networkname_descriptor; - GstMPEGDescriptor *mpegdescriptor; - - if (data + descriptors_loop_length > end - 4) { - GST_WARNING ("PID %d invalid NIT descriptors loop length %d", - section->pid, descriptors_loop_length); - gst_structure_free (nit); - goto error; - } - mpegdescriptor = gst_mpeg_descriptor_parse (data, descriptors_loop_length); - networkname_descriptor = - gst_mpeg_descriptor_find (mpegdescriptor, DESC_DVB_NETWORK_NAME); - if (networkname_descriptor != NULL) { - gchar *networkname_tmp; - - /* No need to bounds check this value as it comes from the descriptor length itself */ - guint8 networkname_length = - DESC_DVB_NETWORK_NAME_length (networkname_descriptor); - gchar *networkname = - (gchar *) DESC_DVB_NETWORK_NAME_text (networkname_descriptor); - - networkname_tmp = - get_encoding_and_convert (networkname, networkname_length); - gst_structure_id_set (nit, QUARK_NETWORK_NAME, G_TYPE_STRING, - networkname_tmp, NULL); - g_free (networkname_tmp); - } - gst_mpeg_descriptor_free (mpegdescriptor); - - descriptors = g_value_array_new (0); - if (!mpegts_packetizer_parse_descriptors (packetizer, - &data, data + descriptors_loop_length, descriptors)) { - gst_structure_free (nit); - g_value_array_free (descriptors); - goto error; - } - - gst_structure_id_set (nit, QUARK_DESCRIPTORS, G_TYPE_VALUE_ARRAY, - descriptors, NULL); - g_value_array_free (descriptors); - } - - transport_stream_loop_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - g_value_init (&transports, GST_TYPE_LIST); - /* read up to the CRC */ - while (transport_stream_loop_length - 4 > 0) { - gchar *transport_name; - - entry_begin = data; - - if (transport_stream_loop_length < 10) { - /* each entry must be at least 6 bytes (+ 4bytes CRC) */ - GST_WARNING ("PID %d invalid NIT entry size %d", - section->pid, transport_stream_loop_length); - goto error; - } - - transport_stream_id = GST_READ_UINT16_BE (data); - data += 2; - - original_network_id = GST_READ_UINT16_BE (data); - data += 2; - - descriptors_loop_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - transport_name = g_strdup_printf ("transport-%d", transport_stream_id); - transport = gst_structure_new (transport_name, NULL); - g_free (transport_name); - gst_structure_id_set (transport, - QUARK_TRANSPORT_STREAM_ID, G_TYPE_UINT, transport_stream_id, - QUARK_ORIGINAL_NETWORK_ID, G_TYPE_UINT, original_network_id, NULL); - - if (descriptors_loop_length) { - GstMPEGDescriptor *mpegdescriptor; - guint8 *delivery; - - if (data + descriptors_loop_length > end - 4) { - GST_WARNING ("PID %d invalid NIT entry %d descriptors loop length %d", - section->pid, transport_stream_id, descriptors_loop_length); - gst_structure_free (transport); - goto error; - } - mpegdescriptor = - gst_mpeg_descriptor_parse (data, descriptors_loop_length); - - if ((delivery = - gst_mpeg_descriptor_find (mpegdescriptor, - DESC_DVB_SATELLITE_DELIVERY_SYSTEM))) { - - guint8 *frequency_bcd = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_frequency (delivery); - guint32 frequency = - 10 * ((frequency_bcd[3] & 0x0F) + - 10 * ((frequency_bcd[3] & 0xF0) >> 4) + - 100 * (frequency_bcd[2] & 0x0F) + - 1000 * ((frequency_bcd[2] & 0xF0) >> 4) + - 10000 * (frequency_bcd[1] & 0x0F) + - 100000 * ((frequency_bcd[1] & 0xF0) >> 4) + - 1000000 * (frequency_bcd[0] & 0x0F) + - 10000000 * ((frequency_bcd[0] & 0xF0) >> 4)); - guint8 *orbital_bcd = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_orbital_position (delivery); - gfloat orbital = - (orbital_bcd[1] & 0x0F) / 10. + ((orbital_bcd[1] & 0xF0) >> 4) + - 10 * (orbital_bcd[0] & 0x0F) + 100 * ((orbital_bcd[0] & 0xF0) >> 4); - gboolean east = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_west_east_flag (delivery); - guint8 polarization = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_polarization (delivery); - const gchar *polarization_str; - guint8 modulation = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_modulation (delivery); - const gchar *modulation_str; - guint8 *symbol_rate_bcd = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_symbol_rate (delivery); - guint32 symbol_rate = - (symbol_rate_bcd[2] & 0x0F) + - 10 * ((symbol_rate_bcd[2] & 0xF0) >> 4) + - 100 * (symbol_rate_bcd[1] & 0x0F) + - 1000 * ((symbol_rate_bcd[1] & 0xF0) >> 4) + - 10000 * (symbol_rate_bcd[0] & 0x0F) + - 100000 * ((symbol_rate_bcd[0] & 0xF0) >> 4); - guint8 fec_inner = - DESC_DVB_SATELLITE_DELIVERY_SYSTEM_fec_inner (delivery); - const gchar *fec_inner_str; - - switch (polarization) { - case 0: - polarization_str = "horizontal"; - break; - case 1: - polarization_str = "vertical"; - break; - case 2: - polarization_str = "left"; - break; - case 3: - polarization_str = "right"; - break; - default: - polarization_str = ""; - } - switch (fec_inner) { - case 0: - fec_inner_str = "undefined"; - break; - case 1: - fec_inner_str = "1/2"; - break; - case 2: - fec_inner_str = "2/3"; - break; - case 3: - fec_inner_str = "3/4"; - break; - case 4: - fec_inner_str = "5/6"; - break; - case 5: - fec_inner_str = "7/8"; - break; - case 6: - fec_inner_str = "8/9"; - break; - case 0xF: - fec_inner_str = "none"; - break; - default: - fec_inner_str = "reserved"; - } - switch (modulation) { - case 0x00: - modulation_str = "undefined"; - break; - case 0x01: - modulation_str = "QAM16"; - break; - case 0x02: - modulation_str = "QAM32"; - break; - case 0x03: - modulation_str = "QAM64"; - break; - case 0x04: - modulation_str = "QAM128"; - break; - case 0x05: - modulation_str = "QAM256"; - break; - default: - modulation_str = "reserved"; - } - delivery_structure = gst_structure_new ("satellite", - "orbital", G_TYPE_FLOAT, orbital, - "east-or-west", G_TYPE_STRING, east ? "east" : "west", - "modulation", G_TYPE_STRING, modulation_str, - "frequency", G_TYPE_UINT, frequency, - "polarization", G_TYPE_STRING, polarization_str, - "symbol-rate", G_TYPE_UINT, symbol_rate, - "inner-fec", G_TYPE_STRING, fec_inner_str, NULL); - gst_structure_set (transport, "delivery", GST_TYPE_STRUCTURE, - delivery_structure, NULL); - } else if ((delivery = - gst_mpeg_descriptor_find (mpegdescriptor, - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM))) { - - guint32 frequency = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_frequency (delivery) * 10; - guint8 bandwidth = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_bandwidth (delivery); - guint8 constellation = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_constellation (delivery); - guint8 hierarchy = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_hierarchy (delivery); - guint8 code_rate_hp = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_code_rate_hp (delivery); - guint8 code_rate_lp = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_code_rate_lp (delivery); - guint8 guard_interval = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_guard_interval (delivery); - guint8 transmission_mode = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_transmission_mode (delivery); - gboolean other_frequency = - DESC_DVB_TERRESTRIAL_DELIVERY_SYSTEM_other_frequency (delivery); - const gchar *constellation_str, *code_rate_hp_str, *code_rate_lp_str, - *transmission_mode_str; - /* do the stuff */ - /* bandwidth is 8 if 0, 7 if 1, 6 if 2, reserved otherwise */ - if (bandwidth <= 2) - bandwidth = 8 - bandwidth; - else - bandwidth = 0; - switch (constellation) { - case 0: - constellation_str = "QPSK"; - break; - case 1: - constellation_str = "QAM16"; - break; - case 2: - constellation_str = "QAM64"; - break; - default: - constellation_str = "reserved"; - } - /* hierarchy is 4 if 3, 2 if 2, 1 if 1, 0 if 0, reserved if > 3 */ - if (hierarchy <= 3) { - if (hierarchy == 3) - hierarchy = 4; - } else { - hierarchy = 0; - } - - switch (code_rate_hp) { - case 0: - code_rate_hp_str = "1/2"; - break; - case 1: - code_rate_hp_str = "2/3"; - break; - case 2: - code_rate_hp_str = "3/4"; - break; - case 3: - code_rate_hp_str = "5/6"; - break; - case 4: - code_rate_hp_str = "7/8"; - break; - default: - code_rate_hp_str = "reserved"; - } - - switch (code_rate_lp) { - case 0: - code_rate_lp_str = "1/2"; - break; - case 1: - code_rate_lp_str = "2/3"; - break; - case 2: - code_rate_lp_str = "3/4"; - break; - case 3: - code_rate_lp_str = "5/6"; - break; - case 4: - code_rate_lp_str = "7/8"; - break; - default: - code_rate_lp_str = "reserved"; - } - /* guard is 32 if 0, 16 if 1, 8 if 2, 4 if 3 */ - switch (guard_interval) { - case 0: - guard_interval = 32; - break; - case 1: - guard_interval = 16; - break; - case 2: - guard_interval = 8; - break; - case 3: - guard_interval = 4; - break; - default: /* make it default to 32 */ - guard_interval = 32; - } - switch (transmission_mode) { - case 0: - transmission_mode_str = "2k"; - break; - case 1: - transmission_mode_str = "8k"; - break; - default: - transmission_mode_str = "reserved"; - } - delivery_structure = gst_structure_new ("terrestrial", - "frequency", G_TYPE_UINT, frequency, - "bandwidth", G_TYPE_UINT, bandwidth, - "constellation", G_TYPE_STRING, constellation_str, - "hierarchy", G_TYPE_UINT, hierarchy, - "code-rate-hp", G_TYPE_STRING, code_rate_hp_str, - "code-rate-lp", G_TYPE_STRING, code_rate_lp_str, - "guard-interval", G_TYPE_UINT, guard_interval, - "transmission-mode", G_TYPE_STRING, transmission_mode_str, - "other-frequency", G_TYPE_BOOLEAN, other_frequency, NULL); - gst_structure_set (transport, "delivery", GST_TYPE_STRUCTURE, - delivery_structure, NULL); - } else if ((delivery = - gst_mpeg_descriptor_find (mpegdescriptor, - DESC_DVB_CABLE_DELIVERY_SYSTEM))) { - - guint8 *frequency_bcd = - DESC_DVB_CABLE_DELIVERY_SYSTEM_frequency (delivery); - /* see en 300 468 section 6.2.13.1 least significant bcd digit - * is measured in 100Hz units so multiplier needs to be 100 to get - * into Hz */ - guint32 frequency = 100 * - ((frequency_bcd[3] & 0x0F) + - 10 * ((frequency_bcd[3] & 0xF0) >> 4) + - 100 * (frequency_bcd[2] & 0x0F) + - 1000 * ((frequency_bcd[2] & 0xF0) >> 4) + - 10000 * (frequency_bcd[1] & 0x0F) + - 100000 * ((frequency_bcd[1] & 0xF0) >> 4) + - 1000000 * (frequency_bcd[0] & 0x0F) + - 10000000 * ((frequency_bcd[0] & 0xF0) >> 4)); - guint8 modulation = - DESC_DVB_CABLE_DELIVERY_SYSTEM_modulation (delivery); - const gchar *modulation_str; - guint8 *symbol_rate_bcd = - DESC_DVB_CABLE_DELIVERY_SYSTEM_symbol_rate (delivery); - guint32 symbol_rate = - (symbol_rate_bcd[2] & 0x0F) + - 10 * ((symbol_rate_bcd[2] & 0xF0) >> 4) + - 100 * (symbol_rate_bcd[1] & 0x0F) + - 1000 * ((symbol_rate_bcd[1] & 0xF0) >> 4) + - 10000 * (symbol_rate_bcd[0] & 0x0F) + - 100000 * ((symbol_rate_bcd[0] & 0xF0) >> 4); - guint8 fec_inner = DESC_DVB_CABLE_DELIVERY_SYSTEM_fec_inner (delivery); - const gchar *fec_inner_str; - - switch (fec_inner) { - case 0: - fec_inner_str = "undefined"; - break; - case 1: - fec_inner_str = "1/2"; - break; - case 2: - fec_inner_str = "2/3"; - break; - case 3: - fec_inner_str = "3/4"; - break; - case 4: - fec_inner_str = "5/6"; - break; - case 5: - fec_inner_str = "7/8"; - break; - case 6: - fec_inner_str = "8/9"; - break; - case 0xF: - fec_inner_str = "none"; - break; - default: - fec_inner_str = "reserved"; - } - switch (modulation) { - case 0x00: - modulation_str = "undefined"; - break; - case 0x01: - modulation_str = "QAM16"; - break; - case 0x02: - modulation_str = "QAM32"; - break; - case 0x03: - modulation_str = "QAM64"; - break; - case 0x04: - modulation_str = "QAM128"; - break; - case 0x05: - modulation_str = "QAM256"; - break; - default: - modulation_str = "reserved"; - } - delivery_structure = gst_structure_new ("cable", - "modulation", G_TYPE_STRING, modulation_str, - "frequency", G_TYPE_UINT, frequency, - "symbol-rate", G_TYPE_UINT, symbol_rate, - "inner-fec", G_TYPE_STRING, fec_inner_str, NULL); - gst_structure_set (transport, "delivery", GST_TYPE_STRUCTURE, - delivery_structure, NULL); - } - /* free the temporary delivery structure */ - if (delivery_structure != NULL) { - gst_structure_free (delivery_structure); - delivery_structure = NULL; - } - if ((delivery = - gst_mpeg_descriptor_find (mpegdescriptor, - DESC_DTG_LOGICAL_CHANNEL))) { - guint8 *current_pos = delivery + 2; - GValue channel_numbers = { 0 }; - - g_value_init (&channel_numbers, GST_TYPE_LIST); - while (current_pos < delivery + DESC_LENGTH (delivery)) { - GstStructure *channel; - GValue channel_value = { 0 }; - guint16 service_id = GST_READ_UINT16_BE (current_pos); - guint16 logical_channel_number; - - current_pos += 2; - logical_channel_number = GST_READ_UINT16_BE (current_pos) & 0x03ff; - channel = - gst_structure_new ("channels", "service-id", G_TYPE_UINT, - service_id, "logical-channel-number", G_TYPE_UINT, - logical_channel_number, NULL); - g_value_init (&channel_value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&channel_value, channel); - gst_value_list_append_value (&channel_numbers, &channel_value); - g_value_unset (&channel_value); - current_pos += 2; - } - gst_structure_set_value (transport, "channels", &channel_numbers); - g_value_unset (&channel_numbers); - } - if ((delivery = - gst_mpeg_descriptor_find (mpegdescriptor, - DESC_DVB_FREQUENCY_LIST))) { - guint8 *current_pos = delivery + 2; - GValue frequencies = { 0 }; - guint8 type; - - type = *current_pos & 0x03; - current_pos++; - - if (type) { - const gchar *fieldname = NULL; - g_value_init (&frequencies, GST_TYPE_LIST); - - while (current_pos < delivery + DESC_LENGTH (delivery) - 3) { - guint32 freq = 0; - guint8 *frequency_bcd = current_pos; - GValue frequency = { 0 }; - - switch (type) { - case 0x01: - /* satellite */ - freq = - 10 * ((frequency_bcd[3] & 0x0F) + - 10 * ((frequency_bcd[3] & 0xF0) >> 4) + - 100 * (frequency_bcd[2] & 0x0F) + - 1000 * ((frequency_bcd[2] & 0xF0) >> 4) + - 10000 * (frequency_bcd[1] & 0x0F) + - 100000 * ((frequency_bcd[1] & 0xF0) >> 4) + - 1000000 * (frequency_bcd[0] & 0x0F) + - 10000000 * ((frequency_bcd[0] & 0xF0) >> 4)); - break; - case 0x02: - /* cable */ - freq = 100 * - ((frequency_bcd[3] & 0x0F) + - 10 * ((frequency_bcd[3] & 0xF0) >> 4) + - 100 * (frequency_bcd[2] & 0x0F) + - 1000 * ((frequency_bcd[2] & 0xF0) >> 4) + - 10000 * (frequency_bcd[1] & 0x0F) + - 100000 * ((frequency_bcd[1] & 0xF0) >> 4) + - 1000000 * (frequency_bcd[0] & 0x0F) + - 10000000 * ((frequency_bcd[0] & 0xF0) >> 4)); - break; - case 0x03: - /* terrestrial */ - freq = GST_READ_UINT32_BE (current_pos) * 10; - break; - } - g_value_init (&frequency, G_TYPE_UINT); - g_value_set_uint (&frequency, freq); - gst_value_list_append_value (&frequencies, &frequency); - g_value_unset (&frequency); - current_pos += 4; - } - - switch (type) { - case 0x01: - fieldname = "frequency-list-satellite"; - break; - case 0x02: - fieldname = "frequency-list-cable"; - break; - case 0x03: - fieldname = "frequency-list-terrestrial"; - break; - } - - gst_structure_set_value (transport, fieldname, &frequencies); - g_value_unset (&frequencies); - } - } - gst_mpeg_descriptor_free (mpegdescriptor); - - descriptors = g_value_array_new (0); - if (!mpegts_packetizer_parse_descriptors (packetizer, - &data, data + descriptors_loop_length, descriptors)) { - gst_structure_free (transport); - g_value_array_free (descriptors); - goto error; - } - - gst_structure_id_set (transport, QUARK_DESCRIPTORS, G_TYPE_VALUE_ARRAY, - descriptors, NULL); - g_value_array_free (descriptors); - } - - g_value_init (&transport_value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&transport_value, transport); - gst_value_list_append_value (&transports, &transport_value); - g_value_unset (&transport_value); - - transport_stream_loop_length -= data - entry_begin; - } - - if (data != end - 4) { - GST_WARNING ("PID %d invalid NIT parsed %d length %d", - section->pid, (gint) (data - GST_BUFFER_DATA (section->buffer)), - GST_BUFFER_SIZE (section->buffer)); - goto error; - } - - gst_structure_id_set_value (nit, QUARK_TRANSPORTS, &transports); - g_value_unset (&transports); - - GST_DEBUG ("NIT %" GST_PTR_FORMAT, nit); - - return nit; - -error: - if (nit) - gst_structure_free (nit); - - if (GST_VALUE_HOLDS_LIST (&transports)) - g_value_unset (&transports); - - return NULL; -} - -GstStructure * -mpegts_packetizer_parse_sdt (MpegTSPacketizer * packetizer, - MpegTSPacketizerSection * section) -{ - GstStructure *sdt = NULL, *service = NULL; - guint8 *data, *end, *entry_begin; - guint16 transport_stream_id, original_network_id, service_id; - guint tmp; - guint sdt_info_length; - guint8 running_status; - gboolean scrambled; - guint descriptors_loop_length; - GValue services = { 0 }; - GValueArray *descriptors = NULL; - GValue service_value = { 0 }; - - GST_DEBUG ("SDT"); - /* fixed header + CRC == 16 */ - if (GST_BUFFER_SIZE (section->buffer) < 14) { - GST_WARNING ("PID %d invalid SDT size %d", - section->pid, section->section_length); - goto error; - } - - data = GST_BUFFER_DATA (section->buffer); - end = data + GST_BUFFER_SIZE (section->buffer); - - section->table_id = *data++; - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - if (data + section->section_length != end) { - GST_WARNING ("PID %d invalid SDT section length %d expected %d", - section->pid, section->section_length, (gint) (end - data)); - goto error; - } - - transport_stream_id = GST_READ_UINT16_BE (data); - data += 2; - - tmp = *data++; - section->version_number = (tmp >> 1) & 0x1F; - section->current_next_indicator = tmp & 0x01; - - /* skip section_number and last_section_number */ - data += 2; - - original_network_id = GST_READ_UINT16_BE (data); - data += 2; - - /* skip reserved byte */ - data += 1; - - sdt = gst_structure_id_new (QUARK_SDT, - QUARK_TRANSPORT_STREAM_ID, G_TYPE_UINT, transport_stream_id, - QUARK_VERSION_NUMBER, G_TYPE_UINT, section->version_number, - QUARK_CURRENT_NEXT_INDICATOR, G_TYPE_UINT, - section->current_next_indicator, QUARK_ORIGINAL_NETWORK_ID, G_TYPE_UINT, - original_network_id, QUARK_ACTUAL_TRANSPORT_STREAM, G_TYPE_BOOLEAN, - section->table_id == 0x42, NULL); - - sdt_info_length = section->section_length - 8; - g_value_init (&services, GST_TYPE_LIST); - /* read up to the CRC */ - while (sdt_info_length - 4 > 0) { - gchar *service_name; - - entry_begin = data; - - if (sdt_info_length < 9) { - /* each entry must be at least 5 bytes (+4 bytes for the CRC) */ - GST_WARNING ("PID %d invalid SDT entry size %d", - section->pid, sdt_info_length); - goto error; - } - - service_id = GST_READ_UINT16_BE (data); - data += 2; - - /* EIT_schedule = ((*data & 0x02) == 2); */ - /* EIT_present_following = (*data & 0x01) == 1; */ - - data += 1; - tmp = GST_READ_UINT16_BE (data); - - running_status = (*data >> 5) & 0x07; - scrambled = (*data >> 4) & 0x01; - descriptors_loop_length = tmp & 0x0FFF; - data += 2; - - /* TODO send tag event down relevant pad for channel name and provider */ - service_name = g_strdup_printf ("service-%d", service_id); - service = gst_structure_new (service_name, NULL); - g_free (service_name); - - if (descriptors_loop_length) { - guint8 *service_descriptor; - GstMPEGDescriptor *mpegdescriptor; - - if (data + descriptors_loop_length > end - 4) { - GST_WARNING ("PID %d invalid SDT entry %d descriptors loop length %d", - section->pid, service_id, descriptors_loop_length); - gst_structure_free (service); - goto error; - } - mpegdescriptor = - gst_mpeg_descriptor_parse (data, descriptors_loop_length); - service_descriptor = - gst_mpeg_descriptor_find (mpegdescriptor, DESC_DVB_SERVICE); - if (service_descriptor != NULL) { - gchar *servicename_tmp, *serviceprovider_name_tmp; - guint8 serviceprovider_name_length = - DESC_DVB_SERVICE_provider_name_length (service_descriptor); - gchar *serviceprovider_name = - (gchar *) DESC_DVB_SERVICE_provider_name_text (service_descriptor); - guint8 servicename_length = - DESC_DVB_SERVICE_name_length (service_descriptor); - gchar *servicename = - (gchar *) DESC_DVB_SERVICE_name_text (service_descriptor); - if (servicename_length + serviceprovider_name_length + 2 <= - DESC_LENGTH (service_descriptor)) { - const gchar *running_status_tmp; - switch (running_status) { - case 0: - running_status_tmp = "undefined"; - break; - case 1: - running_status_tmp = "not running"; - break; - case 2: - running_status_tmp = "starts in a few seconds"; - break; - case 3: - running_status_tmp = "pausing"; - break; - case 4: - running_status_tmp = "running"; - break; - default: - running_status_tmp = "reserved"; - } - servicename_tmp = - get_encoding_and_convert (servicename, servicename_length); - serviceprovider_name_tmp = - get_encoding_and_convert (serviceprovider_name, - serviceprovider_name_length); - - gst_structure_set (service, - "name", G_TYPE_STRING, servicename_tmp, - "provider-name", G_TYPE_STRING, serviceprovider_name_tmp, - "scrambled", G_TYPE_BOOLEAN, scrambled, - "running-status", G_TYPE_STRING, running_status_tmp, NULL); - - g_free (servicename_tmp); - g_free (serviceprovider_name_tmp); - } - } - gst_mpeg_descriptor_free (mpegdescriptor); - - descriptors = g_value_array_new (0); - if (!mpegts_packetizer_parse_descriptors (packetizer, - &data, data + descriptors_loop_length, descriptors)) { - gst_structure_free (service); - g_value_array_free (descriptors); - goto error; - } - - gst_structure_id_set (service, QUARK_DESCRIPTORS, G_TYPE_VALUE_ARRAY, - descriptors, NULL); - - g_value_array_free (descriptors); - } - - g_value_init (&service_value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&service_value, service); - gst_value_list_append_value (&services, &service_value); - g_value_unset (&service_value); - - sdt_info_length -= data - entry_begin; - } - - if (data != end - 4) { - GST_WARNING ("PID %d invalid SDT parsed %d length %d", - section->pid, (gint) (data - GST_BUFFER_DATA (section->buffer)), - GST_BUFFER_SIZE (section->buffer)); - goto error; - } - - gst_structure_id_set_value (sdt, QUARK_SERVICES, &services); - g_value_unset (&services); - - return sdt; - -error: - if (sdt) - gst_structure_free (sdt); - - if (GST_VALUE_HOLDS_LIST (&services)) - g_value_unset (&services); - - return NULL; -} - -GstStructure * -mpegts_packetizer_parse_eit (MpegTSPacketizer * packetizer, - MpegTSPacketizerSection * section) -{ - GstStructure *eit = NULL, *event = NULL; - guint service_id, last_table_id, segment_last_section_number; - guint transport_stream_id, original_network_id; - gboolean free_ca_mode; - guint event_id, running_status; - guint16 mjd; - guint year, month, day, hour, minute, second; - guint duration; - guint8 *data, *end, *duration_ptr, *utc_ptr; - guint16 descriptors_loop_length; - GValue events = { 0 }; - GValue event_value = { 0 }; - GValueArray *descriptors = NULL; - gchar *event_name; - guint tmp; - - /* fixed header + CRC == 16 */ - if (GST_BUFFER_SIZE (section->buffer) < 18) { - GST_WARNING ("PID %d invalid EIT size %d", - section->pid, section->section_length); - goto error; - } - - data = GST_BUFFER_DATA (section->buffer); - end = data + GST_BUFFER_SIZE (section->buffer); - - section->table_id = *data++; - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - if (data + section->section_length != end) { - GST_WARNING ("PID %d invalid EIT section length %d expected %d", - section->pid, section->section_length, (gint) (end - data)); - goto error; - } - - service_id = GST_READ_UINT16_BE (data); - data += 2; - - tmp = *data++; - section->version_number = (tmp >> 1) & 0x1F; - section->current_next_indicator = tmp & 0x01; - - /* skip section_number and last_section_number */ - data += 2; - - transport_stream_id = GST_READ_UINT16_BE (data); - data += 2; - original_network_id = GST_READ_UINT16_BE (data); - data += 2; - segment_last_section_number = *data; - data += 1; - last_table_id = *data; - data += 1; - - eit = gst_structure_id_new (QUARK_EIT, - QUARK_VERSION_NUMBER, G_TYPE_UINT, section->version_number, - QUARK_CURRENT_NEXT_INDICATOR, G_TYPE_UINT, - section->current_next_indicator, QUARK_SERVICE_ID, G_TYPE_UINT, - service_id, QUARK_ACTUAL_TRANSPORT_STREAM, G_TYPE_BOOLEAN, - (section->table_id == 0x4E || (section->table_id >= 0x50 - && section->table_id <= 0x5F)), QUARK_PRESENT_FOLLOWING, - G_TYPE_BOOLEAN, (section->table_id == 0x4E - || section->table_id == 0x4F), QUARK_TRANSPORT_STREAM_ID, G_TYPE_UINT, - transport_stream_id, QUARK_ORIGINAL_NETWORK_ID, G_TYPE_UINT, - original_network_id, QUARK_SEGMENT_LAST_SECTION_NUMBER, G_TYPE_UINT, - segment_last_section_number, QUARK_LAST_TABLE_ID, G_TYPE_UINT, - last_table_id, NULL); - - g_value_init (&events, GST_TYPE_LIST); - while (data < end - 4) { - /* 12 is the minimum entry size + CRC */ - if (end - data < 12 + 4) { - GST_WARNING ("PID %d invalid EIT entry length %d", - section->pid, (gint) (end - 4 - data)); - gst_structure_free (eit); - goto error; - } - - event_id = GST_READ_UINT16_BE (data); - data += 2; - /* start_and_duration = GST_READ_UINT64_BE (data); */ - duration_ptr = data + 5; - utc_ptr = data + 2; - mjd = GST_READ_UINT16_BE (data); - if (mjd == G_MAXUINT16) { - year = 1900; - month = day = hour = minute = second = 0; - } else { - /* See EN 300 468 Annex C */ - year = (guint32) (((mjd - 15078.2) / 365.25)); - month = (guint8) ((mjd - 14956.1 - (guint) (year * 365.25)) / 30.6001); - day = mjd - 14956 - (guint) (year * 365.25) - (guint) (month * 30.6001); - if (month == 14 || month == 15) { - year++; - month = month - 1 - 12; - } else { - month--; - } - year += 1900; - hour = ((utc_ptr[0] & 0xF0) >> 4) * 10 + (utc_ptr[0] & 0x0F); - minute = ((utc_ptr[1] & 0xF0) >> 4) * 10 + (utc_ptr[1] & 0x0F); - second = ((utc_ptr[2] & 0xF0) >> 4) * 10 + (utc_ptr[2] & 0x0F); - } - - duration = (((duration_ptr[0] & 0xF0) >> 4) * 10 + - (duration_ptr[0] & 0x0F)) * 60 * 60 + - (((duration_ptr[1] & 0xF0) >> 4) * 10 + - (duration_ptr[1] & 0x0F)) * 60 + - ((duration_ptr[2] & 0xF0) >> 4) * 10 + (duration_ptr[2] & 0x0F); - - data += 8; - running_status = *data >> 5; - free_ca_mode = (*data >> 4) & 0x01; - descriptors_loop_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - /* TODO: send tag event down relevant pad saying what is currently playing */ - event_name = g_strdup_printf ("event-%d", event_id); - event = gst_structure_new (event_name, - "event-id", G_TYPE_UINT, event_id, - "year", G_TYPE_UINT, year, - "month", G_TYPE_UINT, month, - "day", G_TYPE_UINT, day, - "hour", G_TYPE_UINT, hour, - "minute", G_TYPE_UINT, minute, - "second", G_TYPE_UINT, second, - "duration", G_TYPE_UINT, duration, - "running-status", G_TYPE_UINT, running_status, - "free-ca-mode", G_TYPE_BOOLEAN, free_ca_mode, NULL); - g_free (event_name); - - if (descriptors_loop_length) { - guint8 *event_descriptor; - GArray *component_descriptors; - GArray *extended_event_descriptors; - GstMPEGDescriptor *mpegdescriptor; - - if (data + descriptors_loop_length > end - 4) { - GST_WARNING ("PID %d invalid EIT descriptors loop length %d", - section->pid, descriptors_loop_length); - gst_structure_free (event); - goto error; - } - mpegdescriptor = - gst_mpeg_descriptor_parse (data, descriptors_loop_length); - event_descriptor = - gst_mpeg_descriptor_find (mpegdescriptor, DESC_DVB_SHORT_EVENT); - if (event_descriptor != NULL) { - gchar *eventname_tmp, *eventdescription_tmp; - guint8 eventname_length = - DESC_DVB_SHORT_EVENT_name_length (event_descriptor); - gchar *eventname = - (gchar *) DESC_DVB_SHORT_EVENT_name_text (event_descriptor); - guint8 eventdescription_length = - DESC_DVB_SHORT_EVENT_description_length (event_descriptor); - gchar *eventdescription = - (gchar *) DESC_DVB_SHORT_EVENT_description_text (event_descriptor); - if (eventname_length + eventdescription_length + 2 <= - DESC_LENGTH (event_descriptor)) { - - eventname_tmp = - get_encoding_and_convert (eventname, eventname_length); - eventdescription_tmp = - get_encoding_and_convert (eventdescription, - eventdescription_length); - - gst_structure_set (event, "name", G_TYPE_STRING, eventname_tmp, NULL); - gst_structure_set (event, "description", G_TYPE_STRING, - eventdescription_tmp, NULL); - g_free (eventname_tmp); - g_free (eventdescription_tmp); - } - } - extended_event_descriptors = gst_mpeg_descriptor_find_all (mpegdescriptor, - DESC_DVB_EXTENDED_EVENT); - if (extended_event_descriptors) { - int i; - guint8 *extended_descriptor; - /*GValue extended_items = { 0 }; */ - gchar *extended_text = NULL; - /*g_value_init (&extended_items, GST_TYPE_LIST); */ - for (i = 0; i < extended_event_descriptors->len; i++) { - extended_descriptor = g_array_index (extended_event_descriptors, - guint8 *, i); - if (DESC_DVB_EXTENDED_EVENT_descriptor_number (extended_descriptor) == - i) { - if (extended_text) { - gchar *tmp; - gchar *old_extended_text = extended_text; - tmp = get_encoding_and_convert ((gchar *) - DESC_DVB_EXTENDED_EVENT_text (extended_descriptor), - DESC_DVB_EXTENDED_EVENT_text_length (extended_descriptor)); - extended_text = g_strdup_printf ("%s%s", extended_text, tmp); - g_free (old_extended_text); - g_free (tmp); - } else { - extended_text = get_encoding_and_convert ((gchar *) - DESC_DVB_EXTENDED_EVENT_text (extended_descriptor), - DESC_DVB_EXTENDED_EVENT_text_length (extended_descriptor)); - } - } - } - if (extended_text) { - gst_structure_set (event, "extended-text", G_TYPE_STRING, - extended_text, NULL); - g_free (extended_text); - } - g_array_free (extended_event_descriptors, TRUE); - } - - component_descriptors = gst_mpeg_descriptor_find_all (mpegdescriptor, - DESC_DVB_COMPONENT); - if (component_descriptors) { - int i; - guint8 *comp_descriptor; - GValue components = { 0 }; - g_value_init (&components, GST_TYPE_LIST); - /* FIXME: do the component descriptor parsing less verbosely - * and better...a task for 0.10.6 */ - for (i = 0; i < component_descriptors->len; i++) { - GstStructure *component = NULL; - GValue component_value = { 0 }; - gint widescreen = 0; /* 0 for 4:3, 1 for 16:9, 2 for > 16:9 */ - gint freq = 25; /* 25 or 30 measured in Hertz */ - /* gboolean highdef = FALSE; */ - gboolean panvectors = FALSE; - const gchar *comptype = ""; - - comp_descriptor = g_array_index (component_descriptors, guint8 *, i); - switch (DESC_DVB_COMPONENT_stream_content (comp_descriptor)) { - case 0x01: - /* video */ - switch (DESC_DVB_COMPONENT_type (comp_descriptor)) { - case 0x01: - widescreen = 0; - freq = 25; - break; - case 0x02: - widescreen = 1; - panvectors = TRUE; - freq = 25; - break; - case 0x03: - widescreen = 1; - panvectors = FALSE; - freq = 25; - break; - case 0x04: - widescreen = 2; - freq = 25; - break; - case 0x05: - widescreen = 0; - freq = 30; - break; - case 0x06: - widescreen = 1; - panvectors = TRUE; - freq = 30; - break; - case 0x07: - widescreen = 1; - panvectors = FALSE; - freq = 30; - break; - case 0x08: - widescreen = 2; - freq = 30; - break; - case 0x09: - widescreen = 0; - /* highdef = TRUE; */ - freq = 25; - break; - case 0x0A: - widescreen = 1; - /* highdef = TRUE; */ - panvectors = TRUE; - freq = 25; - break; - case 0x0B: - widescreen = 1; - /* highdef = TRUE; */ - panvectors = FALSE; - freq = 25; - break; - case 0x0C: - widescreen = 2; - /* highdef = TRUE; */ - freq = 25; - break; - case 0x0D: - widescreen = 0; - /* highdef = TRUE; */ - freq = 30; - break; - case 0x0E: - widescreen = 1; - /* highdef = TRUE; */ - panvectors = TRUE; - freq = 30; - break; - case 0x0F: - widescreen = 1; - /* highdef = TRUE; */ - panvectors = FALSE; - freq = 30; - break; - case 0x10: - widescreen = 2; - /* highdef = TRUE; */ - freq = 30; - break; - } - component = gst_structure_new ("video", "high-definition", - G_TYPE_BOOLEAN, TRUE, "frequency", G_TYPE_INT, freq, - "tag", G_TYPE_INT, DESC_DVB_COMPONENT_tag (comp_descriptor), - NULL); - if (widescreen == 0) { - gst_structure_set (component, "aspect-ratio", - G_TYPE_STRING, "4:3", NULL); - } else if (widescreen == 2) { - gst_structure_set (component, "aspect-ratio", G_TYPE_STRING, - "> 16:9", NULL); - } else { - gst_structure_set (component, "aspect-ratio", G_TYPE_STRING, - "16:9", "pan-vectors", G_TYPE_BOOLEAN, panvectors, NULL); - } - break; - case 0x02: /* audio */ - comptype = "undefined"; - switch (DESC_DVB_COMPONENT_type (comp_descriptor)) { - case 0x01: - comptype = "single channel mono"; - break; - case 0x02: - comptype = "dual channel mono"; - break; - case 0x03: - comptype = "stereo"; - break; - case 0x04: - comptype = "multi-channel multi-lingual"; - break; - case 0x05: - comptype = "surround"; - break; - case 0x40: - comptype = "audio description for the visually impaired"; - break; - case 0x41: - comptype = "audio for the hard of hearing"; - break; - } - component = gst_structure_new ("audio", "type", G_TYPE_STRING, - comptype, "tag", G_TYPE_INT, - DESC_DVB_COMPONENT_tag (comp_descriptor), NULL); - break; - case 0x03: /* subtitles/teletext/vbi */ - comptype = "reserved"; - switch (DESC_DVB_COMPONENT_type (comp_descriptor)) { - case 0x01: - comptype = "EBU Teletext subtitles"; - break; - case 0x02: - comptype = "associated EBU Teletext"; - break; - case 0x03: - comptype = "VBI data"; - break; - case 0x10: - comptype = "Normal DVB subtitles"; - break; - case 0x11: - comptype = "Normal DVB subtitles for 4:3"; - break; - case 0x12: - comptype = "Normal DVB subtitles for 16:9"; - break; - case 0x13: - comptype = "Normal DVB subtitles for 2.21:1"; - break; - case 0x20: - comptype = "Hard of hearing DVB subtitles"; - break; - case 0x21: - comptype = "Hard of hearing DVB subtitles for 4:3"; - break; - case 0x22: - comptype = "Hard of hearing DVB subtitles for 16:9"; - break; - case 0x23: - comptype = "Hard of hearing DVB subtitles for 2.21:1"; - break; - } - component = gst_structure_new ("teletext", "type", G_TYPE_STRING, - comptype, "tag", G_TYPE_INT, - DESC_DVB_COMPONENT_tag (comp_descriptor), NULL); - break; - } - if (component) { - g_value_init (&component_value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&component_value, component); - gst_value_list_append_value (&components, &component_value); - g_value_unset (&component_value); - component = NULL; - } - } - gst_structure_set_value (event, "components", &components); - g_value_unset (&components); - g_array_free (component_descriptors, TRUE); - } - gst_mpeg_descriptor_free (mpegdescriptor); - - descriptors = g_value_array_new (0); - if (!mpegts_packetizer_parse_descriptors (packetizer, - &data, data + descriptors_loop_length, descriptors)) { - gst_structure_free (event); - g_value_array_free (descriptors); - goto error; - } - gst_structure_id_set (event, QUARK_DESCRIPTORS, G_TYPE_VALUE_ARRAY, - descriptors, NULL); - g_value_array_free (descriptors); - } - - g_value_init (&event_value, GST_TYPE_STRUCTURE); - g_value_take_boxed (&event_value, event); - gst_value_list_append_value (&events, &event_value); - g_value_unset (&event_value); - } - - if (data != end - 4) { - GST_WARNING ("PID %d invalid EIT parsed %d length %d", - section->pid, (gint) (data - GST_BUFFER_DATA (section->buffer)), - GST_BUFFER_SIZE (section->buffer)); - goto error; - } - - gst_structure_id_set_value (eit, QUARK_EVENTS, &events); - g_value_unset (&events); - - GST_DEBUG ("EIT %" GST_PTR_FORMAT, eit); - - return eit; - -error: - if (eit) - gst_structure_free (eit); - - if (GST_VALUE_HOLDS_LIST (&events)) - g_value_unset (&events); - - return NULL; -} - -GstStructure * -mpegts_packetizer_parse_tdt (MpegTSPacketizer * packetizer, - MpegTSPacketizerSection * section) -{ - GstStructure *tdt = NULL; - guint16 mjd; - guint year, month, day, hour, minute, second; - guint8 *data, *end, *utc_ptr; - - GST_DEBUG ("TDT"); - /* length always 8 */ - if (G_UNLIKELY (GST_BUFFER_SIZE (section->buffer) != 8)) { - GST_WARNING ("PID %d invalid TDT size %d", - section->pid, section->section_length); - goto error; - } - - data = GST_BUFFER_DATA (section->buffer); - end = data + GST_BUFFER_SIZE (section->buffer); - - section->table_id = *data++; - section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - data += 2; - - if (data + section->section_length != end) { - GST_WARNING ("PID %d invalid TDT section length %d expected %d", - section->pid, section->section_length, (gint) (end - data)); - goto error; - } - - mjd = GST_READ_UINT16_BE (data); - data += 2; - utc_ptr = data; - if (mjd == G_MAXUINT16) { - year = 1900; - month = day = hour = minute = second = 0; - } else { - /* See EN 300 468 Annex C */ - year = (guint32) (((mjd - 15078.2) / 365.25)); - month = (guint8) ((mjd - 14956.1 - (guint) (year * 365.25)) / 30.6001); - day = mjd - 14956 - (guint) (year * 365.25) - (guint) (month * 30.6001); - if (month == 14 || month == 15) { - year++; - month = month - 1 - 12; - } else { - month--; - } - year += 1900; - hour = ((utc_ptr[0] & 0xF0) >> 4) * 10 + (utc_ptr[0] & 0x0F); - minute = ((utc_ptr[1] & 0xF0) >> 4) * 10 + (utc_ptr[1] & 0x0F); - second = ((utc_ptr[2] & 0xF0) >> 4) * 10 + (utc_ptr[2] & 0x0F); - } - tdt = gst_structure_new ("tdt", - "year", G_TYPE_UINT, year, - "month", G_TYPE_UINT, month, - "day", G_TYPE_UINT, day, - "hour", G_TYPE_UINT, hour, - "minute", G_TYPE_UINT, minute, "second", G_TYPE_UINT, second, NULL); - - return tdt; - -error: - if (tdt) - gst_structure_free (tdt); - - return NULL; -} - - -void -mpegts_packetizer_clear (MpegTSPacketizer * packetizer) -{ - if (packetizer->know_packet_size) { - packetizer->know_packet_size = FALSE; - packetizer->packet_size = 0; - if (packetizer->caps != NULL) { - gst_caps_unref (packetizer->caps); - packetizer->caps = NULL; - } - } - if (packetizer->streams) { - int i; - for (i = 0; i < 8192; i++) { - if (packetizer->streams[i]) { - mpegts_packetizer_stream_free (packetizer->streams[i]); - packetizer->streams[i] = NULL; - } - } - } - - gst_adapter_clear (packetizer->adapter); -} - -void -mpegts_packetizer_remove_stream (MpegTSPacketizer * packetizer, gint16 pid) -{ - MpegTSPacketizerStream *stream = packetizer->streams[pid]; - if (stream) { - GST_INFO ("Removing stream for PID %d", pid); - mpegts_packetizer_stream_free (stream); - packetizer->streams[pid] = NULL; - } -} - -MpegTSPacketizer * -mpegts_packetizer_new (void) -{ - MpegTSPacketizer *packetizer; - - packetizer = - GST_MPEGTS_PACKETIZER (g_object_new (GST_TYPE_MPEGTS_PACKETIZER, NULL)); - - return packetizer; -} - -void -mpegts_packetizer_push (MpegTSPacketizer * packetizer, GstBuffer * buffer) -{ - gst_adapter_push (packetizer->adapter, buffer); -} - -static void -mpegts_try_discover_packet_size (MpegTSPacketizer * packetizer) -{ - guint8 *dest; - int i, pos, j; - const guint psizes[] = { MPEGTS_NORMAL_PACKETSIZE, - MPEGTS_M2TS_PACKETSIZE, - MPEGTS_DVB_ASI_PACKETSIZE, - MPEGTS_ATSC_PACKETSIZE - }; - /* wait for 3 sync bytes */ - /* so first return if there is not enough data for 4 * max packetsize */ - if (gst_adapter_available (packetizer->adapter) < MPEGTS_MAX_PACKETSIZE * 4) - return; - /* check for sync bytes */ - dest = g_malloc (MPEGTS_MAX_PACKETSIZE * 4); - gst_adapter_copy (packetizer->adapter, dest, 0, MPEGTS_MAX_PACKETSIZE * 4); - /* find first sync byte */ - pos = -1; - for (i = 0; i < MPEGTS_MAX_PACKETSIZE; i++) { - if (dest[i] == 0x47) { - for (j = 0; j < 4; j++) { - guint packetsize = psizes[j]; - /* check each of the packet size possibilities in turn */ - if (dest[i] == 0x47 && dest[i + packetsize] == 0x47 && - dest[i + packetsize * 2] == 0x47 && - dest[i + packetsize * 3] == 0x47) { - packetizer->know_packet_size = TRUE; - packetizer->packet_size = packetsize; - packetizer->caps = gst_caps_new_simple ("video/mpegts", - "systemstream", G_TYPE_BOOLEAN, TRUE, - "packetsize", G_TYPE_INT, packetsize, NULL); - pos = i; - break; - } - } - break; - } - } - GST_DEBUG ("have packetsize detected: %d of %u bytes", - packetizer->know_packet_size, packetizer->packet_size); - if (pos > 0) { - /* flush to sync byte */ - gst_adapter_flush (packetizer->adapter, pos); - } else if (!packetizer->know_packet_size) { - /* drop invalid data and move to the next possible packets */ - gst_adapter_flush (packetizer->adapter, MPEGTS_MAX_PACKETSIZE); - } - g_free (dest); -} - - -gboolean -mpegts_packetizer_has_packets (MpegTSPacketizer * packetizer) -{ - if (G_UNLIKELY (packetizer->know_packet_size == FALSE)) { - mpegts_try_discover_packet_size (packetizer); - if (!packetizer->know_packet_size) - return FALSE; - } - return gst_adapter_available (packetizer->adapter) >= packetizer->packet_size; -} - -MpegTSPacketizerPacketReturn -mpegts_packetizer_next_packet (MpegTSPacketizer * packetizer, - MpegTSPacketizerPacket * packet) -{ - guint8 sync_byte; - guint avail; - - packet->buffer = NULL; - if (G_UNLIKELY (!packetizer->know_packet_size)) { - mpegts_try_discover_packet_size (packetizer); - if (!packetizer->know_packet_size) - return PACKET_NEED_MORE; - } - while ((avail = gst_adapter_available (packetizer->adapter)) >= - packetizer->packet_size) { - sync_byte = *gst_adapter_peek (packetizer->adapter, 1); - if (G_UNLIKELY (sync_byte != 0x47)) { - GST_DEBUG ("lost sync %02x", sync_byte); - gst_adapter_flush (packetizer->adapter, 1); - continue; - } - - packet->buffer = gst_adapter_take_buffer (packetizer->adapter, - packetizer->packet_size); - packet->data_start = GST_BUFFER_DATA (packet->buffer); - packet->data_end = - GST_BUFFER_DATA (packet->buffer) + GST_BUFFER_SIZE (packet->buffer); - return mpegts_packetizer_parse_packet (packetizer, packet); - } - - return PACKET_NEED_MORE; -} - -void -mpegts_packetizer_clear_packet (MpegTSPacketizer * packetizer, - MpegTSPacketizerPacket * packet) -{ - if (packet->buffer) - gst_buffer_unref (packet->buffer); - memset (packet, 0, sizeof (MpegTSPacketizerPacket)); -} - -gboolean -mpegts_packetizer_push_section (MpegTSPacketizer * packetizer, - MpegTSPacketizerPacket * packet, MpegTSPacketizerSection * section) -{ - gboolean res = FALSE; - MpegTSPacketizerStream *stream; - guint8 pointer, table_id; - guint16 subtable_extension; - guint section_length; - GstBuffer *sub_buf; - guint8 *data; - - data = packet->data; - section->pid = packet->pid; - - if (packet->payload_unit_start_indicator == 1) { - pointer = *data++; - if (data + pointer > packet->data_end) { - GST_WARNING ("PID %d PSI section pointer points past the end " - "of the buffer", packet->pid); - goto out; - } - - data += pointer; - } - /* TDT and TOT sections (see ETSI EN 300 468 5.2.5) - * these sections do not extend to several packets so we don't need to use the - * sections filter. */ - if (packet->pid == 0x14) { - table_id = data[0]; - section->section_length = GST_READ_UINT24_BE (data) & 0x000FFF; - if (data - GST_BUFFER_DATA (packet->buffer) + section->section_length + 3 > - GST_BUFFER_SIZE (packet->buffer)) { - GST_WARNING ("PID %dd PSI section length extends past the end " - "of the buffer", packet->pid); - goto out; - } - section->buffer = gst_buffer_create_sub (packet->buffer, - data - GST_BUFFER_DATA (packet->buffer), section->section_length + 3); - section->table_id = table_id; - section->complete = TRUE; - res = TRUE; - GST_DEBUG ("TDT section pid:%d table_id:%d section_length: %d\n", - packet->pid, table_id, section->section_length); - goto out; - } - - /* create a sub buffer from the start of the section (table_id and - * section_length included) to the end */ - sub_buf = gst_buffer_create_sub (packet->buffer, - data - GST_BUFFER_DATA (packet->buffer), packet->data_end - data); - - - stream = packetizer->streams[packet->pid]; - if (stream == NULL) { - stream = mpegts_packetizer_stream_new (); - packetizer->streams[packet->pid] = stream; - } - - if (packet->payload_unit_start_indicator) { - table_id = *data++; - /* subtable_extension should be read from 4th and 5th bytes only if - * section_syntax_indicator is 1 */ - if ((data[0] & 0x80) == 0) - subtable_extension = 0; - else - subtable_extension = GST_READ_UINT16_BE (data + 2); - GST_DEBUG ("pid: %d table_id %d sub_table_extension %d", - packet->pid, table_id, subtable_extension); - - section_length = GST_READ_UINT16_BE (data) & 0x0FFF; - - if (stream->continuity_counter != CONTINUITY_UNSET) { - GST_DEBUG - ("PID %d table_id %d sub_table_extension %d payload_unit_start_indicator set but section " - "not complete (last_continuity: %d continuity: %d sec len %d buffer %d avail %d", - packet->pid, table_id, subtable_extension, stream->continuity_counter, - packet->continuity_counter, section_length, GST_BUFFER_SIZE (sub_buf), - gst_adapter_available (stream->section_adapter)); - mpegts_packetizer_clear_section (packetizer, stream); - } else { - GST_DEBUG - ("pusi set and new stream section is %d long and data we have is: %d", - section_length, (gint) (packet->data_end - packet->data)); - } - stream->continuity_counter = packet->continuity_counter; - stream->section_length = section_length; - stream->section_table_id = table_id; - gst_adapter_push (stream->section_adapter, sub_buf); - - res = TRUE; - } else if (stream->continuity_counter != CONTINUITY_UNSET && - (packet->continuity_counter == stream->continuity_counter + 1 || - (stream->continuity_counter == MAX_CONTINUITY && - packet->continuity_counter == 0))) { - stream->continuity_counter = packet->continuity_counter; - gst_adapter_push (stream->section_adapter, sub_buf); - - res = TRUE; - } else { - if (stream->continuity_counter == CONTINUITY_UNSET) - GST_DEBUG ("PID %d waiting for pusi", packet->pid); - else - GST_DEBUG ("PID %d section discontinuity " - "(last_continuity: %d continuity: %d", packet->pid, - stream->continuity_counter, packet->continuity_counter); - mpegts_packetizer_clear_section (packetizer, stream); - gst_buffer_unref (sub_buf); - } - - if (res) { - /* we pushed some data in the section adapter, see if the section is - * complete now */ - - /* >= as sections can be padded and padding is not included in - * section_length */ - if (gst_adapter_available (stream->section_adapter) >= - stream->section_length + 3) { - res = mpegts_packetizer_parse_section_header (packetizer, - stream, section); - - /* flush stuffing bytes */ - mpegts_packetizer_clear_section (packetizer, stream); - } else { - GST_DEBUG ("section not complete"); - /* section not complete yet */ - section->complete = FALSE; - } - } else { - GST_WARNING ("section not complete"); - section->complete = FALSE; - } - -out: - packet->data = data; - GST_DEBUG ("result: %d complete: %d", res, section->complete); - return res; -} - -static void -_init_local (void) -{ - GST_DEBUG_CATEGORY_INIT (mpegts_packetizer_debug, "mpegtspacketizer", 0, - "MPEG transport stream parser"); - - QUARK_PAT = g_quark_from_string ("pat"); - QUARK_TRANSPORT_STREAM_ID = g_quark_from_string ("transport-stream-id"); - QUARK_PROGRAM_NUMBER = g_quark_from_string ("program-number"); - QUARK_PID = g_quark_from_string ("pid"); - QUARK_PROGRAMS = g_quark_from_string ("programs"); - - QUARK_PMT = g_quark_from_string ("pmt"); - QUARK_PCR_PID = g_quark_from_string ("pcr-pid"); - QUARK_VERSION_NUMBER = g_quark_from_string ("version-number"); - QUARK_DESCRIPTORS = g_quark_from_string ("descriptors"); - QUARK_STREAM_TYPE = g_quark_from_string ("stream-type"); - QUARK_STREAMS = g_quark_from_string ("streams"); - - QUARK_NIT = g_quark_from_string ("nit"); - QUARK_NETWORK_ID = g_quark_from_string ("network-id"); - QUARK_CURRENT_NEXT_INDICATOR = g_quark_from_string ("current-next-indicator"); - QUARK_ACTUAL_NETWORK = g_quark_from_string ("actual-network"); - QUARK_NETWORK_NAME = g_quark_from_string ("network-name"); - QUARK_ORIGINAL_NETWORK_ID = g_quark_from_string ("original-network-id"); - QUARK_TRANSPORTS = g_quark_from_string ("transports"); - - QUARK_SDT = g_quark_from_string ("sdt"); - QUARK_ACTUAL_TRANSPORT_STREAM = - g_quark_from_string ("actual-transport-stream"); - QUARK_SERVICES = g_quark_from_string ("services"); - - QUARK_EIT = g_quark_from_string ("eit"); - QUARK_SERVICE_ID = g_quark_from_string ("service-id"); - QUARK_PRESENT_FOLLOWING = g_quark_from_string ("present-following"); - QUARK_SEGMENT_LAST_SECTION_NUMBER = - g_quark_from_string ("segment-last-section-number"); - QUARK_LAST_TABLE_ID = g_quark_from_string ("last-table-id"); - QUARK_EVENTS = g_quark_from_string ("events"); -} - -/** - * @text: The text you want to get the encoding from - * @start_text: Location where the beginning of the actual text is stored - * @is_multibyte: Location where information whether it's a multibyte encoding - * or not is stored - * @returns: Name of encoding or NULL of encoding could not be detected. - * - * The returned string should be freed with g_free () when no longer needed. - */ -static gchar * -get_encoding (const gchar * text, guint * start_text, gboolean * is_multibyte) -{ - gchar *encoding; - guint8 firstbyte; - - g_return_val_if_fail (text != NULL, NULL); - - firstbyte = (guint8) text[0]; - - /* ETSI EN 300 468, "Selection of character table" */ - if (firstbyte <= 0x0B) { - encoding = g_strdup_printf ("iso8859-%u", firstbyte + 4); - *start_text = 1; - *is_multibyte = FALSE; - } else if (firstbyte >= 0x20) { - encoding = g_strdup ("iso6937"); - *start_text = 0; - *is_multibyte = FALSE; - } else if (firstbyte == 0x10) { - guint16 table; - gchar table_str[6]; - - text++; - table = GST_READ_UINT16_BE (text); - g_snprintf (table_str, 6, "%d", table); - - encoding = g_strconcat ("iso8859-", table_str, NULL); - *start_text = 3; - *is_multibyte = FALSE; - } else if (firstbyte == 0x11) { - encoding = g_strdup ("ISO-10646/UCS2"); - *start_text = 1; - *is_multibyte = TRUE; - } else if (firstbyte == 0x12) { - /* EUC-KR implements KSX1001 */ - encoding = g_strdup ("EUC-KR"); - *start_text = 1; - *is_multibyte = TRUE; - } else if (firstbyte == 0x13) { - encoding = g_strdup ("GB2312"); - *start_text = 1; - *is_multibyte = FALSE; - } else if (firstbyte == 0x14) { - encoding = g_strdup ("UTF-16BE"); - *start_text = 1; - *is_multibyte = TRUE; - } else if (firstbyte == 0x15) { - encoding = g_strdup ("ISO-10646/UTF8"); - *start_text = 1; - *is_multibyte = FALSE; - } else { - /* reserved */ - encoding = NULL; - *start_text = 0; - *is_multibyte = FALSE; - } - - GST_DEBUG - ("Found encoding %s, first byte is 0x%02x, start_text: %u, is_multibyte: %d", - encoding, firstbyte, *start_text, *is_multibyte); - - return encoding; -} - -/** - * @text: The text to convert. It may include pango markup ( and ) - * @length: The length of the string -1 if it's nul-terminated - * @start: Where to start converting in the text - * @encoding: The encoding of text - * @is_multibyte: Whether the encoding is a multibyte encoding - * @error: The location to store the error, or NULL to ignore errors - * @returns: UTF-8 encoded string - * - * Convert text to UTF-8. - */ -static gchar * -convert_to_utf8 (const gchar * text, gint length, guint start, - const gchar * encoding, gboolean is_multibyte, GError ** error) -{ - gchar *new_text; - GByteArray *sb; - gint i; - - g_return_val_if_fail (text != NULL, NULL); - g_return_val_if_fail (encoding != NULL, NULL); - - text += start; - - sb = g_byte_array_sized_new (length * 1.1); - - if (is_multibyte) { - if (length == -1) { - while (*text != '\0') { - guint16 code = GST_READ_UINT16_BE (text); - - switch (code) { - case 0xE086: /* emphasis on */ - case 0xE087: /* emphasis off */ - /* skip it */ - break; - case 0xE08A:{ - guint8 nl[] = { 0x00, 0x0A }; /* new line */ - g_byte_array_append (sb, nl, 2); - break; - } - default: - g_byte_array_append (sb, (guint8 *) text, 2); - break; - } - - text += 2; - } - } else { - for (i = 0; i < length; i += 2) { - guint16 code = GST_READ_UINT16_BE (text); - - switch (code) { - case 0xE086: /* emphasis on */ - case 0xE087: /* emphasis off */ - /* skip it */ - break; - case 0xE08A:{ - guint8 nl[] = { 0x00, 0x0A }; /* new line */ - g_byte_array_append (sb, nl, 2); - break; - } - default: - g_byte_array_append (sb, (guint8 *) text, 2); - break; - } - - text += 2; - } - } - } else { - if (length == -1) { - while (*text != '\0') { - guint8 code = (guint8) (*text); - - switch (code) { - case 0x86: /* emphasis on */ - case 0x87: /* emphasis off */ - /* skip it */ - break; - case 0x8A: - g_byte_array_append (sb, (guint8 *) "\n", 1); - break; - default: - g_byte_array_append (sb, &code, 1); - break; - } - - text++; - } - } else { - for (i = 0; i < length; i++) { - guint8 code = (guint8) (*text); - - switch (code) { - case 0x86: /* emphasis on */ - case 0x87: /* emphasis off */ - /* skip it */ - break; - case 0x8A: - g_byte_array_append (sb, (guint8 *) "\n", 1); - break; - default: - g_byte_array_append (sb, &code, 1); - break; - } - - text++; - } - } - } - - if (sb->len > 0) { - new_text = - g_convert ((gchar *) sb->data, sb->len, "utf-8", encoding, NULL, NULL, - error); - } else { - new_text = g_strdup (""); - } - - g_byte_array_free (sb, TRUE); - - return new_text; -} - -static gchar * -get_encoding_and_convert (const gchar * text, guint length) -{ - GError *error = NULL; - gchar *converted_str; - gchar *encoding; - guint start_text = 0; - gboolean is_multibyte; - - g_return_val_if_fail (text != NULL, NULL); - - if (length == 0) - return g_strdup (""); - - encoding = get_encoding (text, &start_text, &is_multibyte); - - if (encoding == NULL) { - GST_WARNING ("Could not detect encoding"); - converted_str = g_strndup (text, length); - } else { - converted_str = convert_to_utf8 (text, length - start_text, start_text, - encoding, is_multibyte, &error); - if (error != NULL) { - GST_WARNING ("Could not convert string, encoding is %s: %s", - encoding, error->message); - g_error_free (error); - error = NULL; - - /* The first part of ISO 6937 is identical to ISO 8859-9, but - * they differ in the second part. Some channels don't - * provide the first byte that indicates ISO 8859-9 encoding. - * If decoding from ISO 6937 failed, we try ISO 8859-9 here. - */ - if (strcmp (encoding, "iso6937") == 0) { - GST_INFO ("Trying encoding ISO 8859-9"); - converted_str = convert_to_utf8 (text, length, 0, - "iso8859-9", FALSE, &error); - if (error != NULL) { - GST_WARNING - ("Could not convert string while assuming encoding ISO 8859-9: %s", - error->message); - g_error_free (error); - goto failed; - } - } else { - goto failed; - } - } - - g_free (encoding); - } - - return converted_str; - -failed: - { - g_free (encoding); - text += start_text; - return g_strndup (text, length - start_text); - } -} diff --git a/gst/mpegdemux/mpegtspacketizer.h b/gst/mpegdemux/mpegtspacketizer.h deleted file mode 100644 index 2a71d46..0000000 --- a/gst/mpegdemux/mpegtspacketizer.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - * mpegtspacketizer.h - - * Copyright (C) 2007 Alessandro Decina - * - * Authors: - * Alessandro Decina - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef GST_MPEGTS_PACKETIZER_H -#define GST_MPEGTS_PACKETIZER_H - -#include -#include -#include - -#define MPEGTS_NORMAL_PACKETSIZE 188 -#define MPEGTS_M2TS_PACKETSIZE 192 -#define MPEGTS_DVB_ASI_PACKETSIZE 204 -#define MPEGTS_ATSC_PACKETSIZE 208 - -#define MPEGTS_MIN_PACKETSIZE MPEGTS_NORMAL_PACKETSIZE -#define MPEGTS_MAX_PACKETSIZE MPEGTS_ATSC_PACKETSIZE - -G_BEGIN_DECLS - -#define GST_TYPE_MPEGTS_PACKETIZER \ - (mpegts_packetizer_get_type()) -#define GST_MPEGTS_PACKETIZER(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MPEGTS_PACKETIZER,MpegTSPacketizer)) -#define GST_MPEGTS_PACKETIZER_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MPEGTS_PACKETIZER,MpegTSPacketizerClass)) -#define GST_IS_MPEGTS_PACKETIZER(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MPEGTS_PACKETIZER)) -#define GST_IS_MPEGTS_PACKETIZER_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MPEGTS_PACKETIZER)) - - -typedef struct _MpegTSPacketizer MpegTSPacketizer; -typedef struct _MpegTSPacketizerClass MpegTSPacketizerClass; - -typedef struct -{ - guint continuity_counter; - GstAdapter *section_adapter; - guint8 section_table_id; - guint section_length; - GSList *subtables; -} MpegTSPacketizerStream; - -struct _MpegTSPacketizer { - GObject object; - - GstAdapter *adapter; - /* streams hashed by pid */ - MpegTSPacketizerStream **streams; - gboolean disposed; - gboolean know_packet_size; - guint16 packet_size; - GstCaps *caps; -}; - -struct _MpegTSPacketizerClass { - GObjectClass object_class; -}; - -typedef struct -{ - GstBuffer *buffer; - gint16 pid; - guint8 payload_unit_start_indicator; - guint8 adaptation_field_control; - guint8 continuity_counter; - guint8 *payload; - - guint8 *data_start; - guint8 *data_end; - guint8 *data; - -} MpegTSPacketizerPacket; - -typedef struct -{ - gboolean complete; - GstBuffer *buffer; - gint16 pid; - guint8 table_id; - guint16 subtable_extension; - guint section_length; - guint8 version_number; - guint8 current_next_indicator; - guint32 crc; -} MpegTSPacketizerSection; - -typedef struct -{ - guint8 table_id; - /* the spec says sub_table_extension is the fourth and fifth byte of a - * section when the section_syntax_indicator is set to a value of "1". If - * section_syntax_indicator is 0, sub_table_extension will be set to 0 */ - guint16 subtable_extension; - guint8 version_number; - guint32 crc; -} MpegTSPacketizerStreamSubtable; - -typedef enum { - PACKET_BAD = FALSE, - PACKET_OK = TRUE, - PACKET_NEED_MORE -} MpegTSPacketizerPacketReturn; - -GType mpegts_packetizer_get_type(void); - -MpegTSPacketizer *mpegts_packetizer_new (void); -void mpegts_packetizer_clear (MpegTSPacketizer *packetizer); -void mpegts_packetizer_push (MpegTSPacketizer *packetizer, GstBuffer *buffer); -gboolean mpegts_packetizer_has_packets (MpegTSPacketizer *packetizer); -MpegTSPacketizerPacketReturn mpegts_packetizer_next_packet (MpegTSPacketizer *packetizer, - MpegTSPacketizerPacket *packet); -void mpegts_packetizer_clear_packet (MpegTSPacketizer *packetizer, - MpegTSPacketizerPacket *packet); -void mpegts_packetizer_remove_stream(MpegTSPacketizer *packetizer, - gint16 pid); - -gboolean mpegts_packetizer_push_section (MpegTSPacketizer *packetzer, - MpegTSPacketizerPacket *packet, MpegTSPacketizerSection *section); -GstStructure *mpegts_packetizer_parse_pat (MpegTSPacketizer *packetizer, - MpegTSPacketizerSection *section); -GstStructure *mpegts_packetizer_parse_pmt (MpegTSPacketizer *packetizer, - MpegTSPacketizerSection *section); -GstStructure *mpegts_packetizer_parse_nit (MpegTSPacketizer *packetizer, - MpegTSPacketizerSection *section); -GstStructure *mpegts_packetizer_parse_sdt (MpegTSPacketizer *packetizer, - MpegTSPacketizerSection *section); -GstStructure *mpegts_packetizer_parse_eit (MpegTSPacketizer *packetizer, - MpegTSPacketizerSection *section); -GstStructure *mpegts_packetizer_parse_tdt (MpegTSPacketizer *packetizer, - MpegTSPacketizerSection *section); - -G_END_DECLS - -#endif /* GST_MPEGTS_PACKETIZER_H */ diff --git a/gst/mpegdemux/mpegtsparse.c b/gst/mpegdemux/mpegtsparse.c deleted file mode 100644 index f7ba07b..0000000 --- a/gst/mpegdemux/mpegtsparse.c +++ /dev/null @@ -1,1472 +0,0 @@ -/* - * mpegtsparse.c - - * Copyright (C) 2007 Alessandro Decina - * - * Authors: - * Alessandro Decina - * Zaheer Abbas Merali - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -#include "mpegtsparse.h" -#include "gstmpegdesc.h" - -/* latency in mseconds */ -#define TS_LATENCY 700 - -#define TABLE_ID_UNSET 0xFF -#define RUNNING_STATUS_RUNNING 4 - -GST_DEBUG_CATEGORY_STATIC (mpegts_parse_debug); -#define GST_CAT_DEFAULT mpegts_parse_debug - -typedef struct _MpegTSParsePad MpegTSParsePad; - -typedef struct -{ - guint16 pid; - guint8 stream_type; -} MpegTSParseStream; - -typedef struct -{ - gint program_number; - guint16 pmt_pid; - guint16 pcr_pid; - GstStructure *pmt_info; - GHashTable *streams; - gint patcount; - gint selected; - gboolean active; - MpegTSParsePad *tspad; -} MpegTSParseProgram; - -struct _MpegTSParsePad -{ - GstPad *pad; - - /* the program number that the peer wants on this pad */ - gint program_number; - MpegTSParseProgram *program; - - /* set to FALSE before a push and TRUE after */ - gboolean pushed; - - /* the return of the latest push */ - GstFlowReturn flow_return; - - GstTagList *tags; - guint event_id; -}; - -static GQuark QUARK_PROGRAMS; -static GQuark QUARK_PROGRAM_NUMBER; -static GQuark QUARK_PID; -static GQuark QUARK_PCR_PID; -static GQuark QUARK_STREAMS; -static GQuark QUARK_STREAM_TYPE; - -static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ") - ); - -static GstStaticPadTemplate src_template = -GST_STATIC_PAD_TEMPLATE ("src_%u", GST_PAD_SRC, - GST_PAD_REQUEST, - GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ") - ); - -static GstStaticPadTemplate program_template = -GST_STATIC_PAD_TEMPLATE ("program_%u", GST_PAD_SRC, - GST_PAD_SOMETIMES, - GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ") - ); - -enum -{ - ARG_0, - PROP_PROGRAM_NUMBERS, - /* FILL ME */ -}; - -static void mpegts_parse_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec); -static void mpegts_parse_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec); -static void mpegts_parse_dispose (GObject * object); -static void mpegts_parse_finalize (GObject * object); - -static MpegTSParsePad *mpegts_parse_create_tspad (MpegTSParse * parse, - const gchar * name); -static void mpegts_parse_destroy_tspad (MpegTSParse * parse, - MpegTSParsePad * tspad); -static GstPad *mpegts_parse_activate_program (MpegTSParse * parse, - MpegTSParseProgram * program); -static void mpegts_parse_free_program (MpegTSParseProgram * program); -static void mpegts_parse_free_stream (MpegTSParseStream * ptream); -static void mpegts_parse_reset_selected_programs (MpegTSParse * parse, - gchar * programs); - -static void mpegts_parse_pad_removed (GstElement * element, GstPad * pad); -static GstPad *mpegts_parse_request_new_pad (GstElement * element, - GstPadTemplate * templ, const gchar * name); -static void mpegts_parse_release_pad (GstElement * element, GstPad * pad); -static GstFlowReturn mpegts_parse_chain (GstPad * pad, GstBuffer * buf); -static gboolean mpegts_parse_sink_event (GstPad * pad, GstEvent * event); -static GstStateChangeReturn mpegts_parse_change_state (GstElement * element, - GstStateChange transition); -static gboolean mpegts_parse_src_pad_query (GstPad * pad, GstQuery * query); -static void _extra_init (GType type); -static void mpegts_parse_get_tags_from_sdt (MpegTSParse * parse, - GstStructure * sdt_info); -static void mpegts_parse_get_tags_from_eit (MpegTSParse * parse, - GstStructure * eit_info); - -GST_BOILERPLATE_FULL (MpegTSParse, mpegts_parse, GstElement, GST_TYPE_ELEMENT, - _extra_init); - -static const 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 -}; - -/* relicenced to LGPL from fluendo ts demuxer */ -static guint32 -mpegts_parse_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 void -_extra_init (GType type) -{ - QUARK_PROGRAMS = g_quark_from_string ("programs"); - QUARK_PROGRAM_NUMBER = g_quark_from_string ("program-number"); - QUARK_PID = g_quark_from_string ("pid"); - QUARK_PCR_PID = g_quark_from_string ("pcr-pid"); - QUARK_STREAMS = g_quark_from_string ("streams"); - QUARK_STREAM_TYPE = g_quark_from_string ("stream-type"); -} - -static void -mpegts_parse_base_init (gpointer klass) -{ - GstElementClass *element_class = GST_ELEMENT_CLASS (klass); - - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&sink_template)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&src_template)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&program_template)); - - gst_element_class_set_details_simple (element_class, - "MPEG transport stream parser", "Codec/Parser", - "Parses MPEG2 transport streams", - "Alessandro Decina , " - "Zaheer Abbas Merali "); -} - -static void -mpegts_parse_class_init (MpegTSParseClass * klass) -{ - GObjectClass *gobject_class; - GstElementClass *element_class; - - element_class = GST_ELEMENT_CLASS (klass); - element_class->pad_removed = mpegts_parse_pad_removed; - element_class->request_new_pad = mpegts_parse_request_new_pad; - element_class->release_pad = mpegts_parse_release_pad; - element_class->change_state = mpegts_parse_change_state; - - gobject_class = G_OBJECT_CLASS (klass); - gobject_class->set_property = mpegts_parse_set_property; - gobject_class->get_property = mpegts_parse_get_property; - gobject_class->dispose = mpegts_parse_dispose; - gobject_class->finalize = mpegts_parse_finalize; - - g_object_class_install_property (gobject_class, PROP_PROGRAM_NUMBERS, - g_param_spec_string ("program-numbers", - "Program Numbers", - "Colon separated list of programs", "", - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); -} - -static gboolean -foreach_psi_pid_remove (gpointer key, gpointer value, gpointer data) -{ - return TRUE; -} - -static void -mpegts_parse_reset (MpegTSParse * parse) -{ - mpegts_packetizer_clear (parse->packetizer); - g_hash_table_foreach_remove (parse->psi_pids, foreach_psi_pid_remove, NULL); - - /* PAT */ - g_hash_table_insert (parse->psi_pids, - GINT_TO_POINTER (0), GINT_TO_POINTER (1)); - /* pmt pids will be added and removed dynamically */ - -} - -static void -mpegts_parse_init (MpegTSParse * parse, MpegTSParseClass * klass) -{ - parse->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink"); - gst_pad_set_chain_function (parse->sinkpad, mpegts_parse_chain); - gst_pad_set_event_function (parse->sinkpad, mpegts_parse_sink_event); - gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad); - - parse->disposed = FALSE; - parse->need_sync_program_pads = FALSE; - parse->packetizer = mpegts_packetizer_new (); - parse->program_numbers = g_strdup (""); - parse->pads_to_add = NULL; - parse->pads_to_remove = NULL; - parse->programs = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, (GDestroyNotify) mpegts_parse_free_program); - parse->psi_pids = g_hash_table_new (g_direct_hash, g_direct_equal); - parse->pes_pids = g_hash_table_new (g_direct_hash, g_direct_equal); - mpegts_parse_reset (parse); - -} - -static void -mpegts_parse_dispose (GObject * object) -{ - MpegTSParse *parse = GST_MPEGTS_PARSE (object); - - if (!parse->disposed) { - g_object_unref (parse->packetizer); - parse->disposed = TRUE; - } - - if (G_OBJECT_CLASS (parent_class)->dispose) - G_OBJECT_CLASS (parent_class)->dispose (object); -} - -static void -mpegts_parse_finalize (GObject * object) -{ - MpegTSParse *parse = GST_MPEGTS_PARSE (object); - - g_free (parse->program_numbers); - if (parse->pat) { - gst_structure_free (parse->pat); - parse->pat = NULL; - } - g_hash_table_destroy (parse->programs); - g_hash_table_destroy (parse->psi_pids); - g_hash_table_destroy (parse->pes_pids); - - if (G_OBJECT_CLASS (parent_class)->finalize) - G_OBJECT_CLASS (parent_class)->finalize (object); -} - -static void -mpegts_parse_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec) -{ - MpegTSParse *parse = GST_MPEGTS_PARSE (object); - - switch (prop_id) { - case PROP_PROGRAM_NUMBERS: - mpegts_parse_reset_selected_programs (parse, g_value_dup_string (value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - } -} - -static void -mpegts_parse_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec) -{ - MpegTSParse *parse = GST_MPEGTS_PARSE (object); - - switch (prop_id) { - case PROP_PROGRAM_NUMBERS: - g_value_set_string (value, parse->program_numbers); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - } -} - -static MpegTSParseProgram * -mpegts_parse_add_program (MpegTSParse * parse, - gint program_number, guint16 pmt_pid) -{ - MpegTSParseProgram *program; - - program = g_new0 (MpegTSParseProgram, 1); - program->program_number = program_number; - program->pmt_pid = pmt_pid; - program->pcr_pid = G_MAXUINT16; - program->streams = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, (GDestroyNotify) mpegts_parse_free_stream); - program->patcount = 0; - program->selected = 0; - program->active = FALSE; - - g_hash_table_insert (parse->programs, - GINT_TO_POINTER (program_number), program); - - return program; -} - -static MpegTSParseProgram * -mpegts_parse_get_program (MpegTSParse * parse, gint program_number) -{ - MpegTSParseProgram *program; - - program = (MpegTSParseProgram *) g_hash_table_lookup (parse->programs, - GINT_TO_POINTER ((gint) program_number)); - - return program; -} - -static GstPad * -mpegts_parse_activate_program (MpegTSParse * parse, - MpegTSParseProgram * program) -{ - MpegTSParsePad *tspad; - gchar *pad_name; - - pad_name = g_strdup_printf ("program_%u", program->program_number); - - tspad = mpegts_parse_create_tspad (parse, pad_name); - tspad->program_number = program->program_number; - tspad->program = program; - program->tspad = tspad; - g_free (pad_name); - gst_pad_set_active (tspad->pad, TRUE); - program->active = TRUE; - - return tspad->pad; -} - -static GstPad * -mpegts_parse_deactivate_program (MpegTSParse * parse, - MpegTSParseProgram * program) -{ - MpegTSParsePad *tspad; - - tspad = program->tspad; - gst_pad_set_active (tspad->pad, FALSE); - program->active = FALSE; - - /* tspad will be destroyed in GstElementClass::pad_removed */ - - return tspad->pad; -} - -static void -mpegts_parse_free_program (MpegTSParseProgram * program) -{ - if (program->pmt_info) - gst_structure_free (program->pmt_info); - - g_hash_table_destroy (program->streams); - - g_free (program); -} - -static void -mpegts_parse_remove_program (MpegTSParse * parse, gint program_number) -{ - g_hash_table_remove (parse->programs, GINT_TO_POINTER (program_number)); -} - -static void -mpegts_parse_sync_program_pads (MpegTSParse * parse) -{ - GList *walk; - - GST_INFO_OBJECT (parse, "begin sync pads"); - for (walk = parse->pads_to_remove; walk; walk = walk->next) - gst_element_remove_pad (GST_ELEMENT (parse), GST_PAD (walk->data)); - - for (walk = parse->pads_to_add; walk; walk = walk->next) - gst_element_add_pad (GST_ELEMENT (parse), GST_PAD (walk->data)); - - if (parse->pads_to_add) - g_list_free (parse->pads_to_add); - - if (parse->pads_to_remove) - g_list_free (parse->pads_to_remove); - - GST_OBJECT_LOCK (parse); - parse->pads_to_remove = NULL; - parse->pads_to_add = NULL; - parse->need_sync_program_pads = FALSE; - GST_OBJECT_UNLOCK (parse); - - GST_INFO_OBJECT (parse, "end sync pads"); -} - - -static MpegTSParseStream * -mpegts_parse_program_add_stream (MpegTSParse * parse, - MpegTSParseProgram * program, guint16 pid, guint8 stream_type) -{ - MpegTSParseStream *stream; - - stream = g_new0 (MpegTSParseStream, 1); - stream->pid = pid; - stream->stream_type = stream_type; - - g_hash_table_insert (program->streams, GINT_TO_POINTER ((gint) pid), stream); - - return stream; -} - -static void -foreach_program_activate_or_deactivate (gpointer key, gpointer value, - gpointer data) -{ - MpegTSParse *parse = GST_MPEGTS_PARSE (data); - MpegTSParseProgram *program = (MpegTSParseProgram *) value; - - /* at this point selected programs have program->selected == 2, - * unselected programs thay may have to be deactivated have selected == 1 and - * unselected inactive programs have selected == 0 */ - - switch (--program->selected) { - case 1: - /* selected */ - if (!program->active && program->pmt_pid != G_MAXUINT16) - parse->pads_to_add = g_list_append (parse->pads_to_add, - mpegts_parse_activate_program (parse, program)); - break; - case 0: - /* unselected */ - if (program->active) - parse->pads_to_remove = g_list_append (parse->pads_to_remove, - mpegts_parse_deactivate_program (parse, program)); - break; - case -1: - /* was already unselected */ - program->selected = 0; - break; - default: - g_return_if_reached (); - } -} - -static void -mpegts_parse_reset_selected_programs (MpegTSParse * parse, - gchar * program_numbers) -{ - GST_OBJECT_LOCK (parse); - if (parse->program_numbers) - g_free (parse->program_numbers); - - parse->program_numbers = program_numbers; - - if (*parse->program_numbers != '\0') { - gint program_number; - MpegTSParseProgram *program; - gchar **progs, **walk; - - progs = g_strsplit (parse->program_numbers, ":", 0); - - walk = progs; - while (*walk != NULL) { - program_number = strtol (*walk, NULL, 0); - program = mpegts_parse_get_program (parse, program_number); - if (program == NULL) - /* create the program, it will get activated once we get a PMT for it */ - program = mpegts_parse_add_program (parse, program_number, G_MAXUINT16); - - program->selected = 2; - ++walk; - } - g_strfreev (progs); - } - - g_hash_table_foreach (parse->programs, - foreach_program_activate_or_deactivate, parse); - - if (parse->pads_to_remove || parse->pads_to_add) - parse->need_sync_program_pads = TRUE; - GST_OBJECT_UNLOCK (parse); -} - -static void -mpegts_parse_free_stream (MpegTSParseStream * stream) -{ - g_free (stream); -} - -static void -mpegts_parse_program_remove_stream (MpegTSParse * parse, - MpegTSParseProgram * program, guint16 pid) -{ - g_hash_table_remove (program->streams, GINT_TO_POINTER ((gint) pid)); -} - -static void -mpegts_parse_deactivate_pmt (MpegTSParse * parse, MpegTSParseProgram * program) -{ - gint i; - guint pid; - guint stream_type; - GstStructure *stream; - const GValue *streams; - const GValue *value; - - if (program->pmt_info) { - streams = gst_structure_id_get_value (program->pmt_info, QUARK_STREAMS); - - for (i = 0; i < gst_value_list_get_size (streams); ++i) { - value = gst_value_list_get_value (streams, i); - stream = g_value_get_boxed (value); - gst_structure_id_get (stream, QUARK_PID, G_TYPE_UINT, &pid, - QUARK_STREAM_TYPE, G_TYPE_UINT, &stream_type, NULL); - mpegts_parse_program_remove_stream (parse, program, (guint16) pid); - g_hash_table_remove (parse->pes_pids, GINT_TO_POINTER ((gint) pid)); - } - - /* remove pcr stream */ - mpegts_parse_program_remove_stream (parse, program, program->pcr_pid); - g_hash_table_remove (parse->pes_pids, - GINT_TO_POINTER ((gint) program->pcr_pid)); - } -} - -static MpegTSParsePad * -mpegts_parse_create_tspad (MpegTSParse * parse, const gchar * pad_name) -{ - GstPad *pad; - MpegTSParsePad *tspad; - - pad = gst_pad_new_from_static_template (&program_template, pad_name); - gst_pad_set_query_function (pad, - GST_DEBUG_FUNCPTR (mpegts_parse_src_pad_query)); - - /* create our wrapper */ - tspad = g_new0 (MpegTSParsePad, 1); - tspad->pad = pad; - tspad->program_number = -1; - tspad->program = NULL; - tspad->pushed = FALSE; - tspad->flow_return = GST_FLOW_NOT_LINKED; - gst_pad_set_element_private (pad, tspad); - - return tspad; -} - -static void -mpegts_parse_destroy_tspad (MpegTSParse * parse, MpegTSParsePad * tspad) -{ - if (tspad->tags) { - gst_tag_list_free (tspad->tags); - } - - /* free the wrapper */ - g_free (tspad); -} - -static void -mpegts_parse_pad_removed (GstElement * element, GstPad * pad) -{ - MpegTSParsePad *tspad; - MpegTSParse *parse = GST_MPEGTS_PARSE (element); - - if (gst_pad_get_direction (pad) == GST_PAD_SINK) - return; - - tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad); - mpegts_parse_destroy_tspad (parse, tspad); - - if (GST_ELEMENT_CLASS (parent_class)->pad_removed) - GST_ELEMENT_CLASS (parent_class)->pad_removed (element, pad); -} - -static GstPad * -mpegts_parse_request_new_pad (GstElement * element, GstPadTemplate * template, - const gchar * unused) -{ - MpegTSParse *parse; - gchar *name; - GstPad *pad; - - g_return_val_if_fail (template != NULL, NULL); - g_return_val_if_fail (GST_IS_MPEGTS_PARSE (element), NULL); - - parse = GST_MPEGTS_PARSE (element); - - GST_OBJECT_LOCK (element); - name = g_strdup_printf ("src_%u", parse->req_pads++); - GST_OBJECT_UNLOCK (element); - - pad = mpegts_parse_create_tspad (parse, name)->pad; - gst_pad_set_active (pad, TRUE); - gst_element_add_pad (element, pad); - g_free (name); - - return pad; -} - -static void -mpegts_parse_release_pad (GstElement * element, GstPad * pad) -{ - g_return_if_fail (GST_IS_MPEGTS_PARSE (element)); - - gst_pad_set_active (pad, FALSE); - /* we do the cleanup in GstElement::pad-removed */ - gst_element_remove_pad (element, pad); -} - -static GstFlowReturn -mpegts_parse_tspad_push_section (MpegTSParse * parse, MpegTSParsePad * tspad, - MpegTSPacketizerSection * section, GstBuffer * buffer) -{ - GstFlowReturn ret = GST_FLOW_NOT_LINKED; - gboolean to_push = TRUE; - - if (tspad->program_number != -1) { - if (tspad->program) { - /* we push all sections to all pads except PMTs which we - * only push to pads meant to receive that program number */ - if (section->table_id == 0x02) { - /* PMT */ - if (section->subtable_extension != tspad->program_number) - to_push = FALSE; - } - } else { - /* there's a program filter on the pad but the PMT for the program has not - * been parsed yet, ignore the pad until we get a PMT */ - to_push = FALSE; - ret = GST_FLOW_OK; - } - } - GST_DEBUG_OBJECT (parse, - "pushing section: %d program number: %d table_id: %d", to_push, - tspad->program_number, section->table_id); - if (to_push) { - ret = gst_pad_push (tspad->pad, buffer); - } else { - gst_buffer_unref (buffer); - if (gst_pad_is_linked (tspad->pad)) - ret = GST_FLOW_OK; - } - - return ret; -} - -static GstFlowReturn -mpegts_parse_tspad_push (MpegTSParse * parse, MpegTSParsePad * tspad, - guint16 pid, GstBuffer * buffer) -{ - GstFlowReturn ret = GST_FLOW_NOT_LINKED; - GHashTable *pad_pids = NULL; - - if (tspad->program_number != -1) { - if (tspad->program) { - pad_pids = tspad->program->streams; - - if (tspad->tags) { - gst_element_found_tags_for_pad (GST_ELEMENT_CAST (parse), - tspad->pad, tspad->tags); - tspad->tags = NULL; - } - } else { - /* there's a program filter on the pad but the PMT for the program has not - * been parsed yet, ignore the pad until we get a PMT */ - gst_buffer_unref (buffer); - ret = GST_FLOW_OK; - goto out; - } - } - - if (pad_pids == NULL || - g_hash_table_lookup (pad_pids, GINT_TO_POINTER ((gint) pid)) != NULL) { - /* push if there's no filter or if the pid is in the filter */ - ret = gst_pad_push (tspad->pad, buffer); - } else { - gst_buffer_unref (buffer); - if (gst_pad_is_linked (tspad->pad)) - ret = GST_FLOW_OK; - } - -out: - return ret; -} - -static void -pad_clear_for_push (GstPad * pad, MpegTSParse * parse) -{ - MpegTSParsePad *tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad); - - tspad->flow_return = GST_FLOW_NOT_LINKED; - tspad->pushed = FALSE; -} - -static GstFlowReturn -mpegts_parse_push (MpegTSParse * parse, MpegTSPacketizerPacket * packet, - MpegTSPacketizerSection * section) -{ - guint32 pads_cookie; - gboolean done = FALSE; - GstPad *pad = NULL; - MpegTSParsePad *tspad; - guint16 pid; - GstBuffer *buffer; - GstFlowReturn ret; - GList *srcpads; - - pid = packet->pid; - buffer = gst_buffer_make_metadata_writable (packet->buffer); - /* we have the same caps on all the src pads */ - gst_buffer_set_caps (buffer, parse->packetizer->caps); - - GST_OBJECT_LOCK (parse); - /* clear tspad->pushed on pads */ - g_list_foreach (GST_ELEMENT_CAST (parse)->srcpads, - (GFunc) pad_clear_for_push, parse); - if (GST_ELEMENT_CAST (parse)->srcpads) - ret = GST_FLOW_NOT_LINKED; - else - ret = GST_FLOW_OK; - - /* Get cookie and source pads list */ - pads_cookie = GST_ELEMENT_CAST (parse)->pads_cookie; - srcpads = GST_ELEMENT_CAST (parse)->srcpads; - if (G_LIKELY (srcpads)) { - pad = GST_PAD_CAST (srcpads->data); - g_object_ref (pad); - } - GST_OBJECT_UNLOCK (parse); - - while (pad && !done) { - tspad = gst_pad_get_element_private (pad); - - if (G_LIKELY (!tspad->pushed)) { - /* ref the buffer as gst_pad_push takes a ref but we want to reuse the - * same buffer for next pushes */ - gst_buffer_ref (buffer); - if (section) { - tspad->flow_return = - mpegts_parse_tspad_push_section (parse, tspad, section, buffer); - } else { - tspad->flow_return = - mpegts_parse_tspad_push (parse, tspad, pid, buffer); - } - tspad->pushed = TRUE; - - if (G_UNLIKELY (tspad->flow_return != GST_FLOW_OK - && tspad->flow_return != GST_FLOW_NOT_LINKED)) { - /* return the error upstream */ - ret = tspad->flow_return; - done = TRUE; - } - - } - - if (ret == GST_FLOW_NOT_LINKED) - ret = tspad->flow_return; - - g_object_unref (pad); - - if (G_UNLIKELY (!done)) { - GST_OBJECT_LOCK (parse); - if (G_UNLIKELY (pads_cookie != GST_ELEMENT_CAST (parse)->pads_cookie)) { - /* resync */ - GST_DEBUG ("resync"); - pads_cookie = GST_ELEMENT_CAST (parse)->pads_cookie; - srcpads = GST_ELEMENT_CAST (parse)->srcpads; - } else { - GST_DEBUG ("getting next pad"); - /* Get next pad */ - srcpads = g_list_next (srcpads); - } - - if (srcpads) { - pad = GST_PAD_CAST (srcpads->data); - g_object_ref (pad); - } else - done = TRUE; - GST_OBJECT_UNLOCK (parse); - } - } - - gst_buffer_unref (buffer); - packet->buffer = NULL; - - return ret; -} - -static gboolean -mpegts_parse_is_psi (MpegTSParse * parse, MpegTSPacketizerPacket * packet) -{ - gboolean retval = FALSE; - guint8 table_id; - guint8 *data; - guint8 pointer; - int i; - static const guint8 si_tables[] = - { 0x00, 0x01, 0x02, 0x03, 0x40, 0x41, 0x42, 0x46, 0x4A, 0x4E, 0x4F, 0x50, - 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, - 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, - 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x7E, - 0x7F, TABLE_ID_UNSET - }; - if (g_hash_table_lookup (parse->psi_pids, - GINT_TO_POINTER ((gint) packet->pid)) != NULL) - retval = TRUE; - /* check is it is a pes pid */ - if (g_hash_table_lookup (parse->pes_pids, - GINT_TO_POINTER ((gint) packet->pid)) != NULL) - return FALSE; - if (!retval) { - if (packet->payload_unit_start_indicator) { - data = packet->data; - pointer = *data++; - data += pointer; - /* 'pointer' value may be invalid on malformed packet - * so we need to avoid out of range - */ - if (!(data < packet->data_end)) { - GST_WARNING_OBJECT (parse, - "Wrong offset when retrieving table id: 0x%x", pointer); - return FALSE; - } - table_id = *data; - i = 0; - while (si_tables[i] != TABLE_ID_UNSET) { - if (G_UNLIKELY (si_tables[i] == table_id)) { - GST_DEBUG_OBJECT (parse, "Packet has table id 0x%x", table_id); - retval = TRUE; - break; - } - i++; - } - } else { - MpegTSPacketizerStream *stream = parse->packetizer->streams[packet->pid]; - if (stream) { - i = 0; - GST_DEBUG_OBJECT (parse, "section table id: 0x%x", - stream->section_table_id); - while (si_tables[i] != TABLE_ID_UNSET) { - if (G_UNLIKELY (si_tables[i] == stream->section_table_id)) { - retval = TRUE; - break; - } - i++; - } - } - } - } - - GST_DEBUG_OBJECT (parse, "Packet of pid 0x%x is psi: %d", packet->pid, - retval); - return retval; -} - -static void -mpegts_parse_apply_pat (MpegTSParse * parse, GstStructure * pat_info) -{ - const GValue *value; - GstStructure *old_pat; - GstStructure *program_info; - guint program_number; - guint pid; - MpegTSParseProgram *program; - gint i; - const GValue *programs; - - old_pat = parse->pat; - parse->pat = gst_structure_copy (pat_info); - - GST_INFO_OBJECT (parse, "PAT %" GST_PTR_FORMAT, pat_info); - - gst_element_post_message (GST_ELEMENT_CAST (parse), - gst_message_new_element (GST_OBJECT (parse), - gst_structure_copy (pat_info))); - - GST_OBJECT_LOCK (parse); - programs = gst_structure_id_get_value (pat_info, QUARK_PROGRAMS); - /* activate the new table */ - for (i = 0; i < gst_value_list_get_size (programs); ++i) { - value = gst_value_list_get_value (programs, i); - - program_info = g_value_get_boxed (value); - gst_structure_id_get (program_info, QUARK_PROGRAM_NUMBER, G_TYPE_UINT, - &program_number, QUARK_PID, G_TYPE_UINT, &pid, NULL); - - program = mpegts_parse_get_program (parse, program_number); - if (program) { - if (program->pmt_pid != pid) { - if (program->pmt_pid != G_MAXUINT16) { - /* pmt pid changed */ - g_hash_table_remove (parse->psi_pids, - GINT_TO_POINTER ((gint) program->pmt_pid)); - } - - program->pmt_pid = pid; - g_hash_table_insert (parse->psi_pids, - GINT_TO_POINTER ((gint) pid), GINT_TO_POINTER (1)); - } - } else { - g_hash_table_insert (parse->psi_pids, - GINT_TO_POINTER ((gint) pid), GINT_TO_POINTER (1)); - program = mpegts_parse_add_program (parse, program_number, pid); - } - program->patcount += 1; - if (program->selected && !program->active) - parse->pads_to_add = g_list_append (parse->pads_to_add, - mpegts_parse_activate_program (parse, program)); - } - - if (old_pat) { - /* deactivate the old table */ - - programs = gst_structure_id_get_value (old_pat, QUARK_PROGRAMS); - for (i = 0; i < gst_value_list_get_size (programs); ++i) { - value = gst_value_list_get_value (programs, i); - - program_info = g_value_get_boxed (value); - gst_structure_id_get (program_info, - QUARK_PROGRAM_NUMBER, G_TYPE_UINT, &program_number, - QUARK_PID, G_TYPE_UINT, &pid, NULL); - - program = mpegts_parse_get_program (parse, program_number); - if (program == NULL) { - GST_DEBUG_OBJECT (parse, "broken PAT, duplicated entry for program %d", - program_number); - continue; - } - - if (--program->patcount > 0) - /* the program has been referenced by the new pat, keep it */ - continue; - - GST_INFO_OBJECT (parse, "PAT removing program %" GST_PTR_FORMAT, - program_info); - - if (program->active) - parse->pads_to_remove = g_list_append (parse->pads_to_remove, - mpegts_parse_deactivate_program (parse, program)); - - mpegts_parse_deactivate_pmt (parse, program); - mpegts_parse_remove_program (parse, program_number); - g_hash_table_remove (parse->psi_pids, GINT_TO_POINTER ((gint) pid)); - mpegts_packetizer_remove_stream (parse->packetizer, pid); - } - - gst_structure_free (old_pat); - } - - GST_OBJECT_UNLOCK (parse); - - mpegts_parse_sync_program_pads (parse); -} - -static void -mpegts_parse_apply_pmt (MpegTSParse * parse, - guint16 pmt_pid, GstStructure * pmt_info) -{ - MpegTSParseProgram *program; - guint program_number; - guint pcr_pid; - guint pid = 0; - guint stream_type; - GstStructure *stream; - gint i; - const GValue *new_streams; - const GValue *value; - - gst_structure_id_get (pmt_info, - QUARK_PROGRAM_NUMBER, G_TYPE_UINT, &program_number, - QUARK_PCR_PID, G_TYPE_UINT, &pcr_pid, NULL); - new_streams = gst_structure_id_get_value (pmt_info, QUARK_STREAMS); - - GST_OBJECT_LOCK (parse); - program = mpegts_parse_get_program (parse, program_number); - if (program) { - /* deactivate old pmt */ - mpegts_parse_deactivate_pmt (parse, program); - if (program->pmt_info) - gst_structure_free (program->pmt_info); - program->pmt_info = NULL; - } else { - /* no PAT?? */ - g_hash_table_insert (parse->psi_pids, - GINT_TO_POINTER ((gint) pmt_pid), GINT_TO_POINTER (1)); - program = mpegts_parse_add_program (parse, program_number, pid); - } - - /* activate new pmt */ - program->pmt_info = gst_structure_copy (pmt_info); - program->pmt_pid = pmt_pid; - program->pcr_pid = pcr_pid; - mpegts_parse_program_add_stream (parse, program, (guint16) pcr_pid, -1); - g_hash_table_insert (parse->pes_pids, GINT_TO_POINTER ((gint) pcr_pid), - GINT_TO_POINTER (1)); - - for (i = 0; i < gst_value_list_get_size (new_streams); ++i) { - value = gst_value_list_get_value (new_streams, i); - stream = g_value_get_boxed (value); - - gst_structure_id_get (stream, QUARK_PID, G_TYPE_UINT, &pid, - QUARK_STREAM_TYPE, G_TYPE_UINT, &stream_type, NULL); - mpegts_parse_program_add_stream (parse, program, - (guint16) pid, (guint8) stream_type); - g_hash_table_insert (parse->pes_pids, GINT_TO_POINTER ((gint) pid), - GINT_TO_POINTER ((gint) 1)); - - } - GST_OBJECT_UNLOCK (parse); - - GST_DEBUG_OBJECT (parse, "new pmt %" GST_PTR_FORMAT, pmt_info); - - gst_element_post_message (GST_ELEMENT_CAST (parse), - gst_message_new_element (GST_OBJECT (parse), - gst_structure_copy (pmt_info))); -} - -static void -mpegts_parse_apply_nit (MpegTSParse * parse, - guint16 pmt_pid, GstStructure * nit_info) -{ - gst_element_post_message (GST_ELEMENT_CAST (parse), - gst_message_new_element (GST_OBJECT (parse), - gst_structure_copy (nit_info))); -} - -static void -mpegts_parse_apply_sdt (MpegTSParse * parse, - guint16 pmt_pid, GstStructure * sdt_info) -{ - mpegts_parse_get_tags_from_sdt (parse, sdt_info); - - gst_element_post_message (GST_ELEMENT_CAST (parse), - gst_message_new_element (GST_OBJECT (parse), - gst_structure_copy (sdt_info))); -} - -static void -mpegts_parse_apply_eit (MpegTSParse * parse, - guint16 pmt_pid, GstStructure * eit_info) -{ - mpegts_parse_get_tags_from_eit (parse, eit_info); - - gst_element_post_message (GST_ELEMENT_CAST (parse), - gst_message_new_element (GST_OBJECT (parse), - gst_structure_copy (eit_info))); -} - -static void -mpegts_parse_apply_tdt (MpegTSParse * parse, - guint16 tdt_pid, GstStructure * tdt_info) -{ - gst_element_post_message (GST_ELEMENT_CAST (parse), - gst_message_new_element (GST_OBJECT (parse), - gst_structure_copy (tdt_info))); - - gst_element_send_event (GST_ELEMENT_CAST (parse), - gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, - gst_structure_copy (tdt_info))); -} - -static gboolean -mpegts_parse_handle_psi (MpegTSParse * parse, MpegTSPacketizerSection * section) -{ - gboolean res = TRUE; - GstStructure *structure = NULL; - - /* table ids 0x70 - 0x73 do not have a crc */ - if (G_LIKELY (section->table_id < 0x70 || section->table_id > 0x73)) { - if (G_UNLIKELY (mpegts_parse_calc_crc32 (GST_BUFFER_DATA (section->buffer), - GST_BUFFER_SIZE (section->buffer)) != 0)) { - GST_WARNING_OBJECT (parse, "bad crc in psi pid 0x%x", section->pid); - return FALSE; - } - } - - switch (section->table_id) { - case 0x00: - /* PAT */ - structure = mpegts_packetizer_parse_pat (parse->packetizer, section); - if (G_LIKELY (structure)) - mpegts_parse_apply_pat (parse, structure); - else - res = FALSE; - - break; - case 0x02: - structure = mpegts_packetizer_parse_pmt (parse->packetizer, section); - if (G_LIKELY (structure)) - mpegts_parse_apply_pmt (parse, section->pid, structure); - else - res = FALSE; - - break; - case 0x40: - /* NIT, actual network */ - case 0x41: - /* NIT, other network */ - structure = mpegts_packetizer_parse_nit (parse->packetizer, section); - if (G_LIKELY (structure)) - mpegts_parse_apply_nit (parse, section->pid, structure); - else - res = FALSE; - - break; - case 0x42: - case 0x46: - structure = mpegts_packetizer_parse_sdt (parse->packetizer, section); - if (G_LIKELY (structure)) - mpegts_parse_apply_sdt (parse, section->pid, structure); - else - res = FALSE; - break; - case 0x4E: - case 0x4F: - /* EIT, present/following */ - case 0x50: - case 0x51: - case 0x52: - case 0x53: - case 0x54: - case 0x55: - case 0x56: - case 0x57: - case 0x58: - case 0x59: - case 0x5A: - case 0x5B: - case 0x5C: - case 0x5D: - case 0x5E: - case 0x5F: - case 0x60: - case 0x61: - case 0x62: - case 0x63: - case 0x64: - case 0x65: - case 0x66: - case 0x67: - case 0x68: - case 0x69: - case 0x6A: - case 0x6B: - case 0x6C: - case 0x6D: - case 0x6E: - case 0x6F: - /* EIT, schedule */ - structure = mpegts_packetizer_parse_eit (parse->packetizer, section); - if (G_LIKELY (structure)) - mpegts_parse_apply_eit (parse, section->pid, structure); - else - res = FALSE; - break; - case 0x70: - /* TDT (Time and Date table) */ - structure = mpegts_packetizer_parse_tdt (parse->packetizer, section); - if (G_LIKELY (structure)) - mpegts_parse_apply_tdt (parse, section->pid, structure); - else - res = FALSE; - break; - default: - break; - } - - if (structure) - gst_structure_free (structure); - - return res; -} - -static void -mpegts_parse_get_tags_from_sdt (MpegTSParse * parse, GstStructure * sdt_info) -{ - const GValue *services; - guint i; - - services = gst_structure_get_value (sdt_info, "services"); - - for (i = 0; i < gst_value_list_get_size (services); i++) { - const GstStructure *service; - const gchar *sid_str; - gchar *tmp; - gint program_number; - MpegTSParseProgram *program; - - service = gst_value_get_structure (gst_value_list_get_value (services, i)); - - /* get program_number from structure name - * which looks like service-%d */ - sid_str = gst_structure_get_name (service); - tmp = g_strstr_len (sid_str, -1, "-"); - if (!tmp) - continue; - program_number = atoi (++tmp); - - program = mpegts_parse_get_program (parse, program_number); - if (program && program->tspad && !program->tspad->tags) { - program->tspad->tags = gst_tag_list_new_full (GST_TAG_ARTIST, - gst_structure_get_string (service, "name"), NULL); - } - } -} - -static void -mpegts_parse_get_tags_from_eit (MpegTSParse * parse, GstStructure * eit_info) -{ - const GValue *events; - guint i; - guint program_number; - MpegTSParseProgram *program; - gboolean present_following; - - gst_structure_get_uint (eit_info, "service-id", &program_number); - program = mpegts_parse_get_program (parse, program_number); - - gst_structure_get_boolean (eit_info, "present-following", &present_following); - - if (program && program->tspad && present_following) { - events = gst_structure_get_value (eit_info, "events"); - - for (i = 0; i < gst_value_list_get_size (events); i++) { - const GstStructure *event; - const gchar *title; - guint status; - guint event_id; - guint duration; - - event = gst_value_get_structure (gst_value_list_get_value (events, i)); - - title = gst_structure_get_string (event, "name"); - gst_structure_get_uint (event, "event-id", &event_id); - gst_structure_get_uint (event, "running-status", &status); - - if (title && event_id != program->tspad->event_id - && status == RUNNING_STATUS_RUNNING) { - gst_structure_get_uint (event, "duration", &duration); - - program->tspad->event_id = event_id; - program->tspad->tags = gst_tag_list_new_full (GST_TAG_TITLE, - title, GST_TAG_DURATION, duration * GST_SECOND, NULL); - } - } - } -} - -static gboolean -mpegts_parse_sink_event (GstPad * pad, GstEvent * event) -{ - gboolean res; - MpegTSParse *parse = - GST_MPEGTS_PARSE (gst_object_get_parent (GST_OBJECT (pad))); - - switch (GST_EVENT_TYPE (event)) { - case GST_EVENT_FLUSH_STOP: - mpegts_packetizer_clear (parse->packetizer); - res = gst_pad_event_default (pad, event); - break; - default: - res = gst_pad_event_default (pad, event); - } - - gst_object_unref (parse); - return res; -} - -static GstFlowReturn -mpegts_parse_chain (GstPad * pad, GstBuffer * buf) -{ - GstFlowReturn res = GST_FLOW_OK; - MpegTSParse *parse; - gboolean parsed; - MpegTSPacketizerPacketReturn pret; - MpegTSPacketizer *packetizer; - MpegTSPacketizerPacket packet; - - parse = GST_MPEGTS_PARSE (gst_object_get_parent (GST_OBJECT (pad))); - packetizer = parse->packetizer; - - mpegts_packetizer_push (parse->packetizer, buf); - while (((pret = - mpegts_packetizer_next_packet (parse->packetizer, - &packet)) != PACKET_NEED_MORE) && res == GST_FLOW_OK) { - if (G_UNLIKELY (pret == PACKET_BAD)) - /* bad header, skip the packet */ - goto next; - - /* parse PSI data */ - if (packet.payload != NULL && mpegts_parse_is_psi (parse, &packet)) { - MpegTSPacketizerSection section; - - parsed = mpegts_packetizer_push_section (packetizer, &packet, §ion); - if (G_UNLIKELY (!parsed)) - /* bad section data */ - goto next; - - if (G_LIKELY (section.complete)) { - /* section complete */ - parsed = mpegts_parse_handle_psi (parse, §ion); - gst_buffer_unref (section.buffer); - - if (G_UNLIKELY (!parsed)) - /* bad PSI table */ - goto next; - } - /* we need to push section packet downstream */ - res = mpegts_parse_push (parse, &packet, §ion); - - } else { - /* push the packet downstream */ - res = mpegts_parse_push (parse, &packet, NULL); - } - - next: - mpegts_packetizer_clear_packet (parse->packetizer, &packet); - } - - if (parse->need_sync_program_pads) - mpegts_parse_sync_program_pads (parse); - - gst_object_unref (parse); - return res; -} - -static GstStateChangeReturn -mpegts_parse_change_state (GstElement * element, GstStateChange transition) -{ - MpegTSParse *parse; - GstStateChangeReturn ret; - - parse = GST_MPEGTS_PARSE (element); - ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); - - switch (transition) { - case GST_STATE_CHANGE_PAUSED_TO_READY: - mpegts_parse_reset (parse); - break; - default: - break; - } - - return ret; -} - -static gboolean -mpegts_parse_src_pad_query (GstPad * pad, GstQuery * query) -{ - MpegTSParse *parse = GST_MPEGTS_PARSE (gst_pad_get_parent (pad)); - gboolean res; - - switch (GST_QUERY_TYPE (query)) { - case GST_QUERY_LATENCY: - { - if ((res = gst_pad_peer_query (parse->sinkpad, query))) { - 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); - } - - break; - } - default: - res = gst_pad_query_default (pad, query); - } - gst_object_unref (parse); - return res; -} - -gboolean -gst_mpegtsparse_plugin_init (GstPlugin * plugin) -{ - GST_DEBUG_CATEGORY_INIT (mpegts_parse_debug, "mpegtsparse", 0, - "MPEG transport stream parser"); - - gst_mpegtsdesc_init_debug (); - - return gst_element_register (plugin, "mpegtsparse", - GST_RANK_NONE, GST_TYPE_MPEGTS_PARSE); -} diff --git a/gst/mpegdemux/mpegtsparse.h b/gst/mpegdemux/mpegtsparse.h deleted file mode 100644 index 149a917..0000000 --- a/gst/mpegdemux/mpegtsparse.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * mpegts_parse.h - GStreamer MPEG transport stream parser - * Copyright (C) 2007 Alessandro Decina - * - * Authors: - * Alessandro Decina - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - - -#ifndef GST_MPEG_TS_PARSE_H -#define GST_MPEG_TS_PARSE_H - -#include -#include "mpegtspacketizer.h" - -G_BEGIN_DECLS - -#define GST_TYPE_MPEGTS_PARSE \ - (mpegts_parse_get_type()) -#define GST_MPEGTS_PARSE(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MPEGTS_PARSE,MpegTSParse)) -#define GST_MPEGTS_PARSE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MPEGTS_PARSE,MpegTSParseClass)) -#define GST_IS_MPEGTS_PARSE(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MPEGTS_PARSE)) -#define GST_IS_MPEGTS_PARSE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MPEGTS_PARSE)) - -typedef struct _MpegTSParse MpegTSParse; -typedef struct _MpegTSParseClass MpegTSParseClass; - -struct _MpegTSParse { - GstElement element; - - GstPad *sinkpad; - - /* the following vars must be protected with the OBJECT_LOCK as they can be - * accessed from the application thread and the streaming thread */ - gchar *program_numbers; - GList *pads_to_add; - GList *pads_to_remove; - GHashTable *programs; - guint req_pads; - - GstStructure *pat; - MpegTSPacketizer *packetizer; - GHashTable *psi_pids; - GHashTable *pes_pids; - gboolean disposed; - gboolean need_sync_program_pads; -}; - -struct _MpegTSParseClass { - GstElementClass parent_class; - - /* signals */ - void (*pat_info) (GstStructure *pat); - void (*pmt_info) (GstStructure *pmt); - void (*nit_info) (GstStructure *nit); - void (*sdt_info) (GstStructure *sdt); - void (*eit_info) (GstStructure *eit); -}; - -GType mpegts_parse_get_type(void); - -gboolean gst_mpegtsparse_plugin_init (GstPlugin * plugin); - -G_END_DECLS - -#endif /* GST_MPEG_TS_PARSE_H */ -- 2.7.4