2008-09-02 12:04:32 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2008-09-02 12:04:32 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Fluendo MPEG Demuxer plugin.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Fluendo, S.L.
|
|
|
|
* Portions created by Fluendo, S.L. are Copyright (C) 2005
|
|
|
|
* Fluendo, S.L. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s): Wim Taymans <wim@fluendo.com>
|
2009-05-20 23:41:47 +00:00
|
|
|
* Jan Schmidt <thaytan@noraisin.net>
|
2008-09-02 12:04:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-08-02 18:37:30 +00:00
|
|
|
#include <gst/tag/tag.h>
|
|
|
|
#include <gst/pbutils/pbutils.h>
|
2017-02-01 03:25:32 +00:00
|
|
|
#include <gst/base/gstbytereader.h>
|
2013-08-02 18:37:30 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
#include "gstmpegdefs.h"
|
|
|
|
#include "gstmpegdemux.h"
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
#define BLOCK_SZ 32768
|
2009-02-04 15:11:23 +00:00
|
|
|
#define SCAN_SCR_SZ 12
|
|
|
|
#define SCAN_PTS_SZ 80
|
2009-02-02 22:12:07 +00:00
|
|
|
|
2009-05-21 10:13:54 +00:00
|
|
|
#define SEGMENT_THRESHOLD (300*GST_MSECOND)
|
|
|
|
#define VIDEO_SEGMENT_THRESHOLD (500*GST_MSECOND)
|
|
|
|
|
2012-01-18 11:04:52 +00:00
|
|
|
#define DURATION_SCAN_LIMIT 4 * 1024 * 1024
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SCAN_SCR,
|
|
|
|
SCAN_DTS,
|
|
|
|
SCAN_PTS
|
|
|
|
} SCAN_MODE;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* We clamp scr delta with 0 so negative bytes won't be possible */
|
|
|
|
#define GSTTIME_TO_BYTES(time) \
|
2008-12-29 17:30:03 +00:00
|
|
|
((time != -1) ? gst_util_uint64_scale (MAX(0,(gint64) (GSTTIME_TO_MPEGTIME(time))), demux->scr_rate_n, demux->scr_rate_d) : -1)
|
2008-09-02 12:04:32 +00:00
|
|
|
#define BYTES_TO_GSTTIME(bytes) ((bytes != -1) ? MPEGTIME_TO_GSTTIME(gst_util_uint64_scale (bytes, demux->scr_rate_d, demux->scr_rate_n)) : -1)
|
|
|
|
|
|
|
|
#define ADAPTER_OFFSET_FLUSH(_bytes_) demux->adapter_offset += (_bytes_)
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gstflupsdemux_debug);
|
|
|
|
#define GST_CAT_DEFAULT (gstflupsdemux_debug)
|
|
|
|
|
|
|
|
/* MPEG2Demux signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2015-04-27 09:55:13 +00:00
|
|
|
PROP_0,
|
2008-09-02 12:04:32 +00:00
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/mpeg, "
|
|
|
|
"mpegversion = (int) { 1, 2 }, "
|
|
|
|
"systemstream = (boolean) TRUE;" "video/x-cdxa")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate video_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("video_%02x",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
GST_STATIC_CAPS ("video/mpeg, "
|
2013-02-12 19:24:44 +00:00
|
|
|
"mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE, "
|
2019-01-24 09:32:14 +00:00
|
|
|
"parsed = (boolean) FALSE; " "video/x-h264, "
|
2019-01-24 09:32:46 +00:00
|
|
|
"stream-format=(string)byte-stream; " "video/x-h265, "
|
2019-01-24 09:32:14 +00:00
|
|
|
"stream-format=(string)byte-stream;")
|
2008-09-02 12:04:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate audio_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("audio_%02x",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
2012-02-02 11:20:38 +00:00
|
|
|
GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1;"
|
|
|
|
"audio/mpeg, mpegversion = (int) 4, stream-format = (string) { adts, loas };"
|
2008-09-02 12:04:32 +00:00
|
|
|
"audio/x-private1-lpcm; "
|
|
|
|
"audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3")
|
|
|
|
);
|
|
|
|
|
2009-05-20 23:41:47 +00:00
|
|
|
static GstStaticPadTemplate subpicture_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("subpicture_%02x",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
2012-08-20 22:37:00 +00:00
|
|
|
GST_STATIC_CAPS ("subpicture/x-dvd")
|
2009-05-20 23:41:47 +00:00
|
|
|
);
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
static GstStaticPadTemplate private_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("private_%d",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static void gst_ps_demux_base_init (GstPsDemuxClass * klass);
|
|
|
|
static void gst_ps_demux_class_init (GstPsDemuxClass * klass);
|
|
|
|
static void gst_ps_demux_init (GstPsDemux * demux);
|
|
|
|
static void gst_ps_demux_finalize (GstPsDemux * demux);
|
|
|
|
static void gst_ps_demux_reset (GstPsDemux * demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static gboolean gst_ps_demux_sink_event (GstPad * pad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstEvent * event);
|
2013-09-02 22:36:43 +00:00
|
|
|
static GstFlowReturn gst_ps_demux_chain (GstPad * pad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstBuffer * buffer);
|
2013-09-02 22:36:43 +00:00
|
|
|
static gboolean gst_ps_demux_sink_activate (GstPad * sinkpad,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstObject * parent);
|
2013-09-02 22:36:43 +00:00
|
|
|
static gboolean gst_ps_demux_sink_activate_mode (GstPad * pad,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstObject * parent, GstPadMode mode, gboolean active);
|
2013-09-02 22:36:43 +00:00
|
|
|
static void gst_ps_demux_loop (GstPad * pad);
|
2009-02-02 22:12:07 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static gboolean gst_ps_demux_src_event (GstPad * pad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstEvent * event);
|
2013-09-02 22:36:43 +00:00
|
|
|
static gboolean gst_ps_demux_src_query (GstPad * pad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstQuery * query);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static GstStateChangeReturn gst_ps_demux_change_state (GstElement * element,
|
2008-09-02 12:04:32 +00:00
|
|
|
GstStateChange transition);
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static inline gboolean gst_ps_demux_scan_forward_ts (GstPsDemux * demux,
|
2012-01-18 11:04:52 +00:00
|
|
|
guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
|
2013-09-02 22:36:43 +00:00
|
|
|
static inline gboolean gst_ps_demux_scan_backward_ts (GstPsDemux * demux,
|
2012-01-18 11:04:52 +00:00
|
|
|
guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
|
2009-02-04 15:11:23 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static inline void gst_ps_demux_send_gap_updates (GstPsDemux * demux,
|
2009-05-21 10:13:54 +00:00
|
|
|
GstClockTime new_time);
|
2013-09-02 22:36:43 +00:00
|
|
|
static inline void gst_ps_demux_clear_times (GstPsDemux * demux);
|
2009-05-21 10:13:54 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static void gst_ps_demux_reset_psm (GstPsDemux * demux);
|
|
|
|
static void gst_ps_demux_flush (GstPsDemux * demux);
|
2012-10-11 09:30:44 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
static void gst_segment_set_position (GstSegment * segment, GstFormat format,
|
|
|
|
guint64 position);
|
|
|
|
static void gst_segment_set_duration (GstSegment * segment, GstFormat format,
|
|
|
|
guint64 duration);
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
/*static guint gst_ps_demux_signals[LAST_SIGNAL] = { 0 };*/
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
GType
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_get_type (void)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
static GType ps_demux_type = 0;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
if (!ps_demux_type) {
|
|
|
|
static const GTypeInfo ps_demux_info = {
|
|
|
|
sizeof (GstPsDemuxClass),
|
|
|
|
(GBaseInitFunc) gst_ps_demux_base_init,
|
2008-09-02 12:04:32 +00:00
|
|
|
NULL,
|
2013-09-02 22:36:43 +00:00
|
|
|
(GClassInitFunc) gst_ps_demux_class_init,
|
2008-09-02 12:04:32 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-09-02 22:36:43 +00:00
|
|
|
sizeof (GstPsDemux),
|
2008-09-02 12:04:32 +00:00
|
|
|
0,
|
2013-09-02 22:36:43 +00:00
|
|
|
(GInstanceInitFunc) gst_ps_demux_init,
|
2012-01-26 12:13:06 +00:00
|
|
|
NULL
|
2008-09-02 12:04:32 +00:00
|
|
|
};
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
ps_demux_type =
|
2008-09-09 11:47:42 +00:00
|
|
|
g_type_register_static (GST_TYPE_ELEMENT, "GstMpegPSDemux",
|
2013-09-02 22:36:43 +00:00
|
|
|
&ps_demux_info, 0);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2008-09-09 11:47:42 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "mpegpsdemux", 0,
|
2008-09-02 12:04:32 +00:00
|
|
|
"MPEG program stream demultiplexer element");
|
|
|
|
}
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
return ps_demux_type;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_base_init (GstPsDemuxClass * klass)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
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);
|
2009-05-20 23:41:47 +00:00
|
|
|
klass->subpicture_template =
|
|
|
|
gst_static_pad_template_get (&subpicture_template);
|
2008-09-02 12:04:32 +00:00
|
|
|
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);
|
2009-05-20 23:41:47 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
klass->subpicture_template);
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_element_class_add_pad_template (element_class, klass->private_template);
|
|
|
|
gst_element_class_add_pad_template (element_class, klass->sink_template);
|
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2016-05-05 09:44:22 +00:00
|
|
|
"MPEG Program Stream Demuxer", "Codec/Demuxer",
|
2010-03-18 16:30:26 +00:00
|
|
|
"Demultiplexes MPEG Program Streams", "Wim Taymans <wim@fluendo.com>");
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_class_init (GstPsDemuxClass * klass)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
gobject_class->finalize = (GObjectFinalizeFunc) gst_ps_demux_finalize;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
gstelement_class->change_state = gst_ps_demux_change_state;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_init (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink");
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_pad_set_event_function (demux->sinkpad,
|
2013-09-02 22:36:43 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_event));
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_pad_set_chain_function (demux->sinkpad,
|
2013-09-02 22:36:43 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_ps_demux_chain));
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_pad_set_activate_function (demux->sinkpad,
|
2013-09-02 22:36:43 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate));
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_pad_set_activatemode_function (demux->sinkpad,
|
2013-09-02 22:36:43 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate_mode));
|
2009-02-02 22:12:07 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
|
|
|
|
|
|
|
|
demux->streams =
|
2013-09-02 22:36:43 +00:00
|
|
|
g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
|
2009-08-07 16:17:28 +00:00
|
|
|
demux->streams_found =
|
2013-09-02 22:36:43 +00:00
|
|
|
g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
|
2009-08-07 16:17:28 +00:00
|
|
|
demux->found_count = 0;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-10-11 09:30:44 +00:00
|
|
|
demux->adapter = gst_adapter_new ();
|
|
|
|
demux->rev_adapter = gst_adapter_new ();
|
2014-05-26 20:18:01 +00:00
|
|
|
demux->flowcombiner = gst_flow_combiner_new ();
|
2012-10-11 09:30:44 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_reset (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_finalize (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_reset (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
g_free (demux->streams);
|
2009-08-07 16:17:28 +00:00
|
|
|
g_free (demux->streams_found);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2014-05-26 20:18:01 +00:00
|
|
|
gst_flow_combiner_free (demux->flowcombiner);
|
2012-10-11 09:30:44 +00:00
|
|
|
g_object_unref (demux->adapter);
|
|
|
|
g_object_unref (demux->rev_adapter);
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_reset (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
/* Clean up the streams and pads we allocated */
|
|
|
|
gint i;
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
for (i = 0; i < GST_PS_DEMUX_MAX_STREAMS; i++) {
|
|
|
|
GstPsStream *stream = demux->streams[i];
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
if (stream != NULL) {
|
2014-05-26 20:18:01 +00:00
|
|
|
if (stream->pad && GST_PAD_PARENT (stream->pad)) {
|
|
|
|
gst_flow_combiner_remove_pad (demux->flowcombiner, stream->pad);
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
|
2015-04-16 12:35:00 +00:00
|
|
|
} else {
|
|
|
|
gst_object_unref (stream->pad);
|
2014-05-26 20:18:01 +00:00
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-10-12 20:33:27 +00:00
|
|
|
if (stream->pending_tags)
|
|
|
|
gst_tag_list_unref (stream->pending_tags);
|
2008-09-02 12:04:32 +00:00
|
|
|
g_free (stream);
|
|
|
|
demux->streams[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-08-07 16:17:28 +00:00
|
|
|
memset (demux->streams_found, 0,
|
2013-09-02 22:36:43 +00:00
|
|
|
sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
|
2009-08-07 16:17:28 +00:00
|
|
|
demux->found_count = 0;
|
2012-10-11 09:30:44 +00:00
|
|
|
|
|
|
|
gst_adapter_clear (demux->adapter);
|
|
|
|
gst_adapter_clear (demux->rev_adapter);
|
|
|
|
|
|
|
|
demux->adapter_offset = G_MAXUINT64;
|
|
|
|
demux->first_scr = G_MAXUINT64;
|
|
|
|
demux->last_scr = G_MAXUINT64;
|
|
|
|
demux->current_scr = G_MAXUINT64;
|
|
|
|
demux->base_time = G_MAXUINT64;
|
|
|
|
demux->scr_rate_n = G_MAXUINT64;
|
|
|
|
demux->scr_rate_d = G_MAXUINT64;
|
|
|
|
demux->first_pts = G_MAXUINT64;
|
|
|
|
demux->last_pts = G_MAXUINT64;
|
|
|
|
demux->mux_rate = G_MAXUINT64;
|
|
|
|
demux->next_pts = G_MAXUINT64;
|
|
|
|
demux->next_dts = G_MAXUINT64;
|
|
|
|
demux->need_no_more_pads = TRUE;
|
2012-10-11 13:27:12 +00:00
|
|
|
demux->adjust_segment = TRUE;
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_reset_psm (demux);
|
2012-10-11 09:30:44 +00:00
|
|
|
gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
|
|
|
|
gst_segment_init (&demux->src_segment, GST_FORMAT_TIME);
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_flush (demux);
|
2013-07-23 08:15:58 +00:00
|
|
|
demux->have_group_id = FALSE;
|
|
|
|
demux->group_id = G_MAXUINT;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static GstPsStream *
|
2019-08-03 09:28:33 +00:00
|
|
|
gst_ps_demux_create_stream (GstPsDemux * demux, gint id, gint stream_type,
|
|
|
|
gint layer)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *stream;
|
2008-09-02 12:04:32 +00:00
|
|
|
GstPadTemplate *template;
|
|
|
|
gchar *name;
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
GstCaps *caps;
|
2009-05-21 10:13:54 +00:00
|
|
|
GstClockTime threshold = SEGMENT_THRESHOLD;
|
2013-07-23 08:15:58 +00:00
|
|
|
GstEvent *event;
|
2013-03-30 09:25:24 +00:00
|
|
|
gchar *stream_id;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
name = NULL;
|
|
|
|
template = NULL;
|
|
|
|
caps = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id,
|
|
|
|
stream_type);
|
|
|
|
|
|
|
|
switch (stream_type) {
|
|
|
|
case ST_VIDEO_MPEG1:
|
|
|
|
case ST_VIDEO_MPEG2:
|
|
|
|
case ST_VIDEO_MPEG4:
|
|
|
|
case ST_GST_VIDEO_MPEG1_OR_2:
|
|
|
|
{
|
|
|
|
gint mpeg_version = 1;
|
|
|
|
if (stream_type == ST_VIDEO_MPEG2 ||
|
|
|
|
(stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) {
|
|
|
|
mpeg_version = 2;
|
|
|
|
}
|
|
|
|
if (stream_type == ST_VIDEO_MPEG4) {
|
|
|
|
mpeg_version = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
template = klass->video_template;
|
|
|
|
name = g_strdup_printf ("video_%02x", id);
|
|
|
|
caps = gst_caps_new_simple ("video/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT, mpeg_version,
|
2013-02-12 19:24:44 +00:00
|
|
|
"systemstream", G_TYPE_BOOLEAN, FALSE,
|
|
|
|
"parsed", G_TYPE_BOOLEAN, FALSE, NULL);
|
2009-05-21 10:13:54 +00:00
|
|
|
threshold = VIDEO_SEGMENT_THRESHOLD;
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ST_AUDIO_MPEG1:
|
|
|
|
case ST_AUDIO_MPEG2:
|
|
|
|
template = klass->audio_template;
|
|
|
|
name = g_strdup_printf ("audio_%02x", id);
|
2019-08-03 09:28:33 +00:00
|
|
|
if (layer) {
|
|
|
|
caps = gst_caps_new_simple ("audio/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
|
|
|
|
} else {
|
|
|
|
caps = gst_caps_new_simple ("audio/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT, 1, NULL);
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ST_PRIVATE_SECTIONS:
|
|
|
|
case ST_PRIVATE_DATA:
|
|
|
|
case ST_MHEG:
|
|
|
|
case ST_DSMCC:
|
2009-08-07 16:35:42 +00:00
|
|
|
break;
|
2011-05-20 07:58:50 +00:00
|
|
|
case ST_AUDIO_AAC_ADTS:
|
2009-08-07 16:35:42 +00:00
|
|
|
template = klass->audio_template;
|
|
|
|
name = g_strdup_printf ("audio_%02x", id);
|
|
|
|
caps = gst_caps_new_simple ("audio/mpeg",
|
2011-05-20 07:58:50 +00:00
|
|
|
"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_%02x", id);
|
|
|
|
caps = gst_caps_new_simple ("audio/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT, 4,
|
|
|
|
"stream-format", G_TYPE_STRING, "loas", NULL);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ST_VIDEO_H264:
|
|
|
|
template = klass->video_template;
|
|
|
|
name = g_strdup_printf ("video_%02x", id);
|
2017-02-01 03:44:32 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-h264",
|
|
|
|
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
|
2009-05-21 10:13:54 +00:00
|
|
|
threshold = VIDEO_SEGMENT_THRESHOLD;
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
2019-01-24 09:32:46 +00:00
|
|
|
case ST_VIDEO_H265:
|
|
|
|
template = klass->video_template;
|
|
|
|
name = g_strdup_printf ("video_%02x", id);
|
|
|
|
caps = gst_caps_new_simple ("video/x-h265",
|
|
|
|
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
|
|
|
|
threshold = VIDEO_SEGMENT_THRESHOLD;
|
|
|
|
break;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
case ST_PS_AUDIO_AC3:
|
|
|
|
template = klass->audio_template;
|
|
|
|
name = g_strdup_printf ("audio_%02x", id);
|
2012-01-26 12:13:06 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-private1-ac3");
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ST_PS_AUDIO_DTS:
|
|
|
|
template = klass->audio_template;
|
|
|
|
name = g_strdup_printf ("audio_%02x", id);
|
2012-01-26 12:13:06 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-private1-dts");
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ST_PS_AUDIO_LPCM:
|
|
|
|
template = klass->audio_template;
|
|
|
|
name = g_strdup_printf ("audio_%02x", id);
|
2012-01-26 12:13:06 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-private1-lpcm");
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ST_PS_DVD_SUBPICTURE:
|
2009-05-20 23:41:47 +00:00
|
|
|
template = klass->subpicture_template;
|
|
|
|
name = g_strdup_printf ("subpicture_%02x", id);
|
2012-08-20 22:37:00 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ST_GST_AUDIO_RAWA52:
|
|
|
|
template = klass->audio_template;
|
|
|
|
name = g_strdup_printf ("audio_%02x", id);
|
2012-01-26 12:13:06 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/ac3");
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-23 13:01:02 +00:00
|
|
|
if (name == NULL || template == NULL || caps == NULL) {
|
2012-08-31 00:51:50 +00:00
|
|
|
g_free (name);
|
2012-01-23 13:01:02 +00:00
|
|
|
if (caps)
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
stream = g_new0 (GstPsStream, 1);
|
2008-09-02 12:04:32 +00:00
|
|
|
stream->id = id;
|
|
|
|
stream->discont = TRUE;
|
|
|
|
stream->need_segment = TRUE;
|
|
|
|
stream->notlinked = FALSE;
|
|
|
|
stream->type = stream_type;
|
2012-10-12 20:33:27 +00:00
|
|
|
stream->pending_tags = NULL;
|
2008-09-02 12:04:32 +00:00
|
|
|
stream->pad = gst_pad_new_from_template (template, name);
|
2009-05-21 10:13:54 +00:00
|
|
|
stream->segment_thresh = threshold;
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_pad_set_event_function (stream->pad,
|
2013-09-02 22:36:43 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_ps_demux_src_event));
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_pad_set_query_function (stream->pad,
|
2013-09-02 22:36:43 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_ps_demux_src_query));
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_pad_use_fixed_caps (stream->pad);
|
2012-01-26 12:13:06 +00:00
|
|
|
|
|
|
|
/* needed for set_caps to work */
|
2014-01-16 09:14:48 +00:00
|
|
|
if (!gst_pad_set_active (stream->pad, TRUE)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Failed to activate pad %" GST_PTR_FORMAT,
|
|
|
|
stream->pad);
|
|
|
|
}
|
2012-01-26 12:13:06 +00:00
|
|
|
|
2013-03-30 09:25:24 +00:00
|
|
|
stream_id =
|
|
|
|
gst_pad_create_stream_id_printf (stream->pad, GST_ELEMENT_CAST (demux),
|
|
|
|
"%02x", id);
|
2013-07-23 08:15:58 +00:00
|
|
|
|
|
|
|
event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
|
|
|
|
if (event) {
|
|
|
|
if (gst_event_parse_group_id (event, &demux->group_id))
|
|
|
|
demux->have_group_id = TRUE;
|
|
|
|
else
|
|
|
|
demux->have_group_id = FALSE;
|
|
|
|
gst_event_unref (event);
|
|
|
|
} else if (!demux->have_group_id) {
|
|
|
|
demux->have_group_id = TRUE;
|
|
|
|
demux->group_id = gst_util_group_id_next ();
|
|
|
|
}
|
|
|
|
event = gst_event_new_stream_start (stream_id);
|
|
|
|
if (demux->have_group_id)
|
|
|
|
gst_event_set_group_id (event, demux->group_id);
|
|
|
|
|
|
|
|
gst_pad_push_event (stream->pad, event);
|
2013-03-30 09:25:24 +00:00
|
|
|
g_free (stream_id);
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_pad_set_caps (stream->pad, caps);
|
2013-08-02 18:37:30 +00:00
|
|
|
|
|
|
|
if (!stream->pending_tags)
|
|
|
|
stream->pending_tags = gst_tag_list_new_empty ();
|
|
|
|
gst_pb_utils_add_codec_description_to_tag_list (stream->pending_tags, NULL,
|
|
|
|
caps);
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps);
|
2014-01-16 09:14:48 +00:00
|
|
|
gst_caps_unref (caps);
|
2008-09-02 12:04:32 +00:00
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
static GstPsStream *
|
2019-08-03 09:28:33 +00:00
|
|
|
gst_ps_demux_get_stream (GstPsDemux * demux, gint id, gint type, gint layer)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *stream = demux->streams[id];
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
if (stream == NULL) {
|
2019-08-03 09:28:33 +00:00
|
|
|
if (!(stream = gst_ps_demux_create_stream (demux, id, type, layer)))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto unknown_stream;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id,
|
|
|
|
type);
|
|
|
|
|
2019-08-03 09:21:29 +00:00
|
|
|
demux->streams[id] = stream;
|
|
|
|
demux->streams_found[demux->found_count++] = stream;
|
|
|
|
|
2012-10-11 13:29:36 +00:00
|
|
|
if (demux->need_no_more_pads) {
|
|
|
|
gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
|
2014-05-26 20:18:01 +00:00
|
|
|
gst_flow_combiner_add_pad (demux->flowcombiner, stream->pad);
|
2012-10-11 13:29:36 +00:00
|
|
|
} else {
|
|
|
|
/* only likely to confuse decodebin etc, so discard */
|
|
|
|
/* FIXME should perform full switch protocol:
|
|
|
|
* add a whole new set of pads, drop old and no-more-pads again */
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"but already signalled no-more-pads; not adding");
|
2015-04-16 12:35:00 +00:00
|
|
|
gst_object_ref_sink (stream->pad);
|
2012-10-11 13:29:36 +00:00
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
unknown_stream:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 10:11:54 +00:00
|
|
|
static GstPsStream *
|
|
|
|
gst_ps_demux_get_stream_from_pad (GstPsDemux * demux, GstPad * srcpad)
|
|
|
|
{
|
|
|
|
gint i, count;
|
|
|
|
|
|
|
|
count = demux->found_count;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
GstPsStream *stream = demux->streams_found[i];
|
|
|
|
|
|
|
|
if (stream && stream->pad == srcpad)
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (srcpad, "no stream found for pad!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-11 09:51:50 +00:00
|
|
|
static inline void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_segment (GstPsDemux * demux, GstPsStream * stream,
|
2012-10-11 09:51:50 +00:00
|
|
|
GstClockTime pts)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
/* discont */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (stream->need_segment)) {
|
2012-01-26 12:13:06 +00:00
|
|
|
GstSegment segment;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-08-31 22:28:29 +00:00
|
|
|
GST_DEBUG ("PTS timestamp:%" GST_TIME_FORMAT " base_time %" GST_TIME_FORMAT
|
2008-10-20 14:19:17 +00:00
|
|
|
" src_segment.start:%" GST_TIME_FORMAT " .stop:%" GST_TIME_FORMAT,
|
2012-08-31 22:28:29 +00:00
|
|
|
GST_TIME_ARGS (pts), GST_TIME_ARGS (demux->base_time),
|
2008-10-20 14:19:17 +00:00
|
|
|
GST_TIME_ARGS (demux->src_segment.start),
|
|
|
|
GST_TIME_ARGS (demux->src_segment.stop));
|
|
|
|
|
2012-10-11 13:27:12 +00:00
|
|
|
/* adjust segment start if estimating a seek was off quite a bit,
|
|
|
|
* make sure to do for all streams though to preserve a/v sync */
|
|
|
|
/* FIXME such adjustment tends to be frowned upon */
|
|
|
|
if (pts != GST_CLOCK_TIME_NONE && demux->adjust_segment) {
|
2008-10-20 14:19:17 +00:00
|
|
|
if (demux->src_segment.rate > 0) {
|
2012-10-11 13:27:12 +00:00
|
|
|
if (GST_CLOCK_DIFF (demux->src_segment.start, pts) > GST_SECOND)
|
|
|
|
demux->src_segment.start = pts - demux->base_time;
|
2008-10-20 14:19:17 +00:00
|
|
|
} else {
|
2012-10-11 13:27:12 +00:00
|
|
|
if (GST_CLOCK_DIFF (demux->src_segment.stop, pts) > GST_SECOND)
|
|
|
|
demux->src_segment.stop = pts - demux->base_time;
|
2008-10-20 14:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 13:27:12 +00:00
|
|
|
demux->adjust_segment = FALSE;
|
|
|
|
|
|
|
|
/* we should be in sync with downstream, so start from our segment notion,
|
|
|
|
* which also includes proper base_time etc, tweak it a bit and send */
|
|
|
|
gst_segment_copy_into (&demux->src_segment, &segment);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (demux->base_time)) {
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (segment.start))
|
|
|
|
segment.start += demux->base_time;
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (segment.stop))
|
|
|
|
segment.stop += demux->base_time;
|
|
|
|
segment.time = segment.start - demux->base_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "sending segment event %" GST_SEGMENT_FORMAT
|
|
|
|
" to pad %" GST_PTR_FORMAT, &segment, stream->pad);
|
|
|
|
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_segment (&segment));
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
stream->need_segment = FALSE;
|
|
|
|
}
|
2012-10-12 20:33:27 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (stream->pending_tags)) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Sending pending_tags %p for pad %s:%s : %"
|
|
|
|
GST_PTR_FORMAT, stream->pending_tags,
|
|
|
|
GST_DEBUG_PAD_NAME (stream->pad), stream->pending_tags);
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
|
|
|
|
stream->pending_tags = NULL;
|
|
|
|
}
|
2012-10-11 09:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_data (GstPsDemux * demux, GstPsStream * stream,
|
2012-10-11 09:51:50 +00:00
|
|
|
GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstFlowReturn result;
|
|
|
|
GstClockTime pts = GST_CLOCK_TIME_NONE, dts = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
if (stream == NULL)
|
|
|
|
goto no_stream;
|
|
|
|
|
|
|
|
/* timestamps */
|
|
|
|
if (G_UNLIKELY (demux->next_pts != G_MAXUINT64))
|
|
|
|
pts = MPEGTIME_TO_GSTTIME (demux->next_pts);
|
|
|
|
if (G_UNLIKELY (demux->next_dts != G_MAXUINT64))
|
|
|
|
dts = MPEGTIME_TO_GSTTIME (demux->next_dts);
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_segment (demux, stream, pts);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* OK, sent new segment now prepare the buffer for sending */
|
2012-08-31 22:28:29 +00:00
|
|
|
GST_BUFFER_PTS (buf) = pts;
|
|
|
|
GST_BUFFER_DTS (buf) = dts;
|
2009-02-04 15:11:23 +00:00
|
|
|
|
|
|
|
/* update position in the segment */
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
|
2009-02-04 21:50:30 +00:00
|
|
|
MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr));
|
2012-01-26 12:13:06 +00:00
|
|
|
|
2009-02-04 21:50:30 +00:00
|
|
|
GST_LOG_OBJECT (demux, "last stop position is now %" GST_TIME_FORMAT
|
|
|
|
" current scr is %" GST_TIME_FORMAT,
|
2012-01-26 12:13:06 +00:00
|
|
|
GST_TIME_ARGS (demux->src_segment.position),
|
2009-02-04 21:50:30 +00:00
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->current_scr)));
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2010-07-02 13:28:41 +00:00
|
|
|
if (demux->src_segment.position != GST_CLOCK_TIME_NONE &&
|
|
|
|
demux->base_time != GST_CLOCK_TIME_NONE) {
|
2012-01-26 12:13:06 +00:00
|
|
|
GstClockTime new_time = demux->base_time + demux->src_segment.position;
|
2009-05-21 10:13:54 +00:00
|
|
|
|
|
|
|
if (stream->last_ts == GST_CLOCK_TIME_NONE || stream->last_ts < new_time) {
|
2012-10-11 09:03:21 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"last_ts update on pad %s to time %" GST_TIME_FORMAT,
|
|
|
|
GST_PAD_NAME (stream->pad), GST_TIME_ARGS (new_time));
|
2009-05-21 10:13:54 +00:00
|
|
|
stream->last_ts = new_time;
|
|
|
|
}
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_gap_updates (demux, new_time);
|
2009-05-21 10:13:54 +00:00
|
|
|
}
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* Set the buffer discont flag, and clear discont state on the stream */
|
|
|
|
if (stream->discont) {
|
2012-08-31 00:51:50 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "discont buffer to pad %" GST_PTR_FORMAT
|
2014-01-16 09:14:48 +00:00
|
|
|
" with PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT,
|
|
|
|
stream->pad, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
|
|
|
|
stream->discont = FALSE;
|
2013-07-17 15:12:59 +00:00
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
2013-07-17 15:12:59 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->next_pts = G_MAXUINT64;
|
|
|
|
demux->next_dts = G_MAXUINT64;
|
|
|
|
|
2013-11-10 11:15:33 +00:00
|
|
|
GST_LOG_OBJECT (demux, "pushing stream id 0x%02x type 0x%02x, pts time: %"
|
|
|
|
GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
|
|
|
|
stream->id, stream->type, GST_TIME_ARGS (pts), gst_buffer_get_size (buf));
|
2014-05-26 20:18:01 +00:00
|
|
|
result = gst_pad_push (stream->pad, buf);
|
2013-11-10 11:15:33 +00:00
|
|
|
GST_LOG_OBJECT (demux, "result: %s", gst_flow_get_name (result));
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
no_stream:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "no stream given");
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
static inline void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_mark_discont (GstPsDemux * demux, gboolean discont,
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean need_segment)
|
|
|
|
{
|
2009-08-07 16:17:28 +00:00
|
|
|
gint i, count = demux->found_count;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* mark discont on all streams */
|
2009-08-07 16:17:28 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *stream = demux->streams_found[i];
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_LIKELY (stream)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
stream->discont |= discont;
|
|
|
|
stream->need_segment |= need_segment;
|
2012-10-11 13:27:12 +00:00
|
|
|
demux->adjust_segment |= need_segment;
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d",
|
|
|
|
stream->discont, stream->need_segment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 13:27:12 +00:00
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (GstPsDemux * demux, GstEvent * event)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2009-08-07 16:17:28 +00:00
|
|
|
gint i, count = demux->found_count;
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *stream = demux->streams_found[i];
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-09-02 20:54:32 +00:00
|
|
|
if (stream) {
|
2013-09-02 22:28:38 +00:00
|
|
|
if (!gst_pad_push_event (stream->pad, gst_event_ref (event))) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "%s event was not handled",
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_EVENT_TYPE_NAME (event));
|
|
|
|
} else {
|
|
|
|
/* If at least one push returns TRUE, then we return TRUE. */
|
2013-09-02 22:28:38 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "%s event was handled",
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_EVENT_TYPE_NAME (event));
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_handle_dvd_event (GstPsDemux * demux, GstEvent * event)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
const GstStructure *structure = gst_event_get_structure (event);
|
|
|
|
const char *type = gst_structure_get_string (structure, "event");
|
|
|
|
gint i;
|
|
|
|
gchar cur_stream_name[32];
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *temp = NULL;
|
2012-10-12 20:33:27 +00:00
|
|
|
const gchar *lang_code;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
if (strcmp (type, "dvd-lang-codes") == 0) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Handling language codes event");
|
|
|
|
|
|
|
|
/* Create a video pad to ensure have it before emit no more pads */
|
2019-08-03 09:28:33 +00:00
|
|
|
(void) gst_ps_demux_get_stream (demux, 0xe0, ST_VIDEO_MPEG2, 0);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* Read out the languages for audio streams and request each one that
|
|
|
|
* is present */
|
|
|
|
for (i = 0; i < MAX_DVD_AUDIO_STREAMS; i++) {
|
|
|
|
gint stream_format;
|
|
|
|
gint stream_id;
|
|
|
|
|
|
|
|
g_snprintf (cur_stream_name, 32, "audio-%d-format", i);
|
|
|
|
if (!gst_structure_get_int (structure, cur_stream_name, &stream_format))
|
2012-08-31 00:51:50 +00:00
|
|
|
continue;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-10-12 12:50:34 +00:00
|
|
|
g_snprintf (cur_stream_name, 32, "audio-%d-stream", i);
|
|
|
|
if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
|
|
|
|
continue;
|
|
|
|
if (stream_id < 0 || stream_id >= MAX_DVD_AUDIO_STREAMS)
|
|
|
|
continue;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
switch (stream_format) {
|
|
|
|
case 0x0:
|
|
|
|
/* AC3 */
|
2012-10-12 12:50:34 +00:00
|
|
|
stream_id += 0x80;
|
2014-01-16 09:14:48 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Audio stream %d format %d ID 0x%02x - AC3", i,
|
|
|
|
stream_format, stream_id);
|
2019-08-03 09:28:33 +00:00
|
|
|
temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_AC3, 0);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case 0x2:
|
|
|
|
case 0x3:
|
2009-08-07 16:35:42 +00:00
|
|
|
/* MPEG audio without and with extension stream are
|
2008-09-02 12:04:32 +00:00
|
|
|
* treated the same */
|
2012-10-12 12:50:34 +00:00
|
|
|
stream_id += 0xC0;
|
2014-01-16 09:14:48 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Audio stream %d format %d ID 0x%02x - MPEG audio", i,
|
|
|
|
stream_format, stream_id);
|
2019-08-03 09:28:33 +00:00
|
|
|
temp = gst_ps_demux_get_stream (demux, stream_id, ST_AUDIO_MPEG1, 0);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case 0x4:
|
|
|
|
/* LPCM */
|
2012-10-12 12:50:34 +00:00
|
|
|
stream_id += 0xA0;
|
2014-01-16 09:14:48 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Audio stream %d format %d ID 0x%02x - DVD LPCM", i,
|
|
|
|
stream_format, stream_id);
|
2019-08-03 09:28:33 +00:00
|
|
|
temp =
|
|
|
|
gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_LPCM, 0);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case 0x6:
|
|
|
|
/* DTS */
|
2012-10-12 12:50:34 +00:00
|
|
|
stream_id += 0x88;
|
2014-01-16 09:14:48 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Audio stream %d format %d ID 0x%02x - DTS", i,
|
|
|
|
stream_format, stream_id);
|
2019-08-03 09:28:33 +00:00
|
|
|
temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_DTS, 0);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case 0x7:
|
|
|
|
/* FIXME: What range is SDDS? */
|
|
|
|
default:
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Unknown audio stream format in language code event: %d",
|
|
|
|
stream_format);
|
2012-10-16 09:38:08 +00:00
|
|
|
temp = NULL;
|
|
|
|
continue;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
2012-10-12 20:33:27 +00:00
|
|
|
|
2015-06-26 06:58:25 +00:00
|
|
|
if (temp == NULL)
|
|
|
|
continue;
|
|
|
|
|
2012-10-12 20:33:27 +00:00
|
|
|
g_snprintf (cur_stream_name, 32, "audio-%d-language", i);
|
|
|
|
lang_code = gst_structure_get_string (structure, cur_stream_name);
|
|
|
|
if (lang_code) {
|
2013-08-02 18:37:30 +00:00
|
|
|
GstTagList *list = temp->pending_tags;
|
2012-10-12 20:33:27 +00:00
|
|
|
|
2013-08-02 18:37:30 +00:00
|
|
|
if (!list)
|
|
|
|
list = gst_tag_list_new_empty ();
|
2012-10-12 20:33:27 +00:00
|
|
|
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_LANGUAGE_CODE, lang_code, NULL);
|
|
|
|
temp->pending_tags = list;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And subtitle streams */
|
|
|
|
for (i = 0; i < MAX_DVD_SUBPICTURE_STREAMS; i++) {
|
2012-10-12 12:50:34 +00:00
|
|
|
gint stream_id;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
g_snprintf (cur_stream_name, 32, "subpicture-%d-format", i);
|
2012-10-12 12:50:34 +00:00
|
|
|
if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_snprintf (cur_stream_name, 32, "subpicture-%d-stream", i);
|
|
|
|
if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
|
|
|
|
continue;
|
|
|
|
if (stream_id < 0 || stream_id >= MAX_DVD_SUBPICTURE_STREAMS)
|
|
|
|
continue;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-10-12 12:50:34 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Subpicture stream %d ID 0x%02x", i,
|
|
|
|
0x20 + stream_id);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* Retrieve the subpicture stream to force pad creation */
|
2013-09-02 22:36:43 +00:00
|
|
|
temp = gst_ps_demux_get_stream (demux, 0x20 + stream_id,
|
2019-08-03 09:28:33 +00:00
|
|
|
ST_PS_DVD_SUBPICTURE, 0);
|
2015-06-26 06:58:25 +00:00
|
|
|
if (temp == NULL)
|
|
|
|
continue;
|
2012-10-12 20:33:27 +00:00
|
|
|
|
|
|
|
g_snprintf (cur_stream_name, 32, "subpicture-%d-language", i);
|
|
|
|
lang_code = gst_structure_get_string (structure, cur_stream_name);
|
|
|
|
if (lang_code) {
|
2013-08-02 18:37:30 +00:00
|
|
|
GstTagList *list = temp->pending_tags;
|
2012-10-12 20:33:27 +00:00
|
|
|
|
2013-08-02 18:37:30 +00:00
|
|
|
if (!list)
|
|
|
|
list = gst_tag_list_new_empty ();
|
2012-10-12 20:33:27 +00:00
|
|
|
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_LANGUAGE_CODE, lang_code, NULL);
|
|
|
|
temp->pending_tags = list;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "Created all pads from Language Codes event, "
|
|
|
|
"signalling no-more-pads");
|
|
|
|
|
|
|
|
gst_element_no_more_pads (GST_ELEMENT (demux));
|
|
|
|
demux->need_no_more_pads = FALSE;
|
2012-10-18 20:23:07 +00:00
|
|
|
} else {
|
|
|
|
/* forward to all pads, e.g. dvd clut event */
|
|
|
|
gst_event_ref (event);
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, event);
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_flush (GstPsDemux * demux)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "flushing demuxer");
|
|
|
|
gst_adapter_clear (demux->adapter);
|
|
|
|
gst_adapter_clear (demux->rev_adapter);
|
|
|
|
gst_pes_filter_drain (&demux->filter);
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_clear_times (demux);
|
2009-02-02 22:12:07 +00:00
|
|
|
demux->adapter_offset = G_MAXUINT64;
|
|
|
|
demux->current_scr = G_MAXUINT64;
|
|
|
|
demux->bytes_since_scr = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
static inline void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_clear_times (GstPsDemux * demux)
|
2009-05-21 10:13:54 +00:00
|
|
|
{
|
2009-08-07 16:17:28 +00:00
|
|
|
gint i, count = demux->found_count;
|
2009-05-21 10:13:54 +00:00
|
|
|
|
2015-03-28 16:28:26 +00:00
|
|
|
gst_flow_combiner_reset (demux->flowcombiner);
|
2009-05-21 10:13:54 +00:00
|
|
|
/* Clear the last ts for all streams */
|
2009-08-07 16:17:28 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *stream = demux->streams_found[i];
|
2009-05-21 10:13:54 +00:00
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_LIKELY (stream)) {
|
2012-10-11 10:07:46 +00:00
|
|
|
stream->last_ts = GST_CLOCK_TIME_NONE;
|
2009-05-21 10:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
static inline void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_gap_updates (GstPsDemux * demux, GstClockTime new_start)
|
2009-05-21 10:13:54 +00:00
|
|
|
{
|
2012-10-11 10:07:46 +00:00
|
|
|
GstClockTime base_time, stop;
|
2009-08-07 16:17:28 +00:00
|
|
|
gint i, count = demux->found_count;
|
2009-05-21 10:13:54 +00:00
|
|
|
GstEvent *event = NULL;
|
|
|
|
|
2012-10-11 10:07:46 +00:00
|
|
|
/* Advance all lagging streams by sending a gap event */
|
2009-09-15 22:32:06 +00:00
|
|
|
if ((base_time = demux->base_time) == GST_CLOCK_TIME_NONE)
|
|
|
|
base_time = 0;
|
|
|
|
|
|
|
|
stop = demux->src_segment.stop;
|
|
|
|
if (stop != GST_CLOCK_TIME_NONE)
|
|
|
|
stop += base_time;
|
2009-05-21 10:13:54 +00:00
|
|
|
|
2009-09-15 22:32:06 +00:00
|
|
|
if (new_start > stop)
|
2009-05-21 10:13:54 +00:00
|
|
|
return;
|
|
|
|
|
2009-09-15 22:32:06 +00:00
|
|
|
/* FIXME: Handle reverse playback */
|
2009-08-07 16:17:28 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsStream *stream = demux->streams_found[i];
|
2009-05-21 10:13:54 +00:00
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
if (stream->last_ts == GST_CLOCK_TIME_NONE ||
|
2009-09-15 22:32:06 +00:00
|
|
|
stream->last_ts < demux->src_segment.start + base_time)
|
|
|
|
stream->last_ts = demux->src_segment.start + base_time;
|
|
|
|
|
|
|
|
if (stream->last_ts + stream->segment_thresh < new_start) {
|
2012-10-18 20:22:12 +00:00
|
|
|
/* should send segment info before gap event */
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_segment (demux, stream, GST_CLOCK_TIME_NONE);
|
2012-10-18 20:22:12 +00:00
|
|
|
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
2012-10-11 10:07:46 +00:00
|
|
|
"Sending gap update to pad %s time %" GST_TIME_FORMAT,
|
2009-09-15 22:32:06 +00:00
|
|
|
GST_PAD_NAME (stream->pad), GST_TIME_ARGS (new_start));
|
2012-10-11 10:07:46 +00:00
|
|
|
event =
|
|
|
|
gst_event_new_gap (stream->last_ts, new_start - stream->last_ts);
|
2009-05-21 10:13:54 +00:00
|
|
|
gst_pad_push_event (stream->pad, event);
|
2012-10-12 20:33:45 +00:00
|
|
|
stream->last_ts = new_start;
|
2009-05-21 10:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-21 14:08:55 +00:00
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
have_open_streams (GstPsDemux * demux)
|
2009-04-21 14:08:55 +00:00
|
|
|
{
|
2009-08-07 16:17:28 +00:00
|
|
|
return (demux->streams_found[0] != NULL);
|
2009-04-21 14:08:55 +00:00
|
|
|
}
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
gboolean res = TRUE;
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (parent);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_FLUSH_START:
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, event);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, event);
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_flush (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
2012-01-26 12:13:06 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2012-01-26 12:13:06 +00:00
|
|
|
const GstSegment *segment;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_event_parse_segment (event, &segment);
|
|
|
|
gst_segment_copy_into (segment, &demux->sink_segment);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-10-12 13:44:03 +00:00
|
|
|
GST_INFO_OBJECT (demux, "received segment %" GST_SEGMENT_FORMAT, segment);
|
|
|
|
|
|
|
|
/* we need to emit a new segment */
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_mark_discont (demux, TRUE, TRUE);
|
2012-10-12 13:44:03 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (segment->format == GST_FORMAT_BYTES
|
|
|
|
&& demux->scr_rate_n != G_MAXUINT64
|
|
|
|
&& demux->scr_rate_d != G_MAXUINT64) {
|
|
|
|
demux->src_segment.rate = segment->rate;
|
|
|
|
demux->src_segment.applied_rate = segment->applied_rate;
|
|
|
|
demux->src_segment.format = GST_FORMAT_TIME;
|
|
|
|
demux->src_segment.start = BYTES_TO_GSTTIME (segment->start);
|
|
|
|
demux->src_segment.stop = BYTES_TO_GSTTIME (segment->stop);
|
|
|
|
demux->src_segment.time = BYTES_TO_GSTTIME (segment->time);
|
2012-10-12 13:44:03 +00:00
|
|
|
} else if (segment->format == GST_FORMAT_TIME) {
|
|
|
|
/* we expect our timeline (SCR, PTS) to match the one from upstream,
|
|
|
|
* if not, will adjust with offset later on */
|
|
|
|
gst_segment_copy_into (segment, &demux->src_segment);
|
|
|
|
/* accept upstream segment without adjusting */
|
|
|
|
demux->adjust_segment = FALSE;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
GST_INFO_OBJECT (demux, "Received EOS");
|
2013-09-02 22:36:43 +00:00
|
|
|
if (!gst_ps_demux_send_event (demux, event)
|
2009-04-21 14:08:55 +00:00
|
|
|
&& !have_open_streams (demux)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "EOS and no streams open");
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, FAILED,
|
2009-04-21 14:08:55 +00:00
|
|
|
("Internal data stream error."), ("No valid streams detected"));
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_EVENT_CUSTOM_DOWNSTREAM:
|
|
|
|
case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
|
|
|
|
{
|
|
|
|
const GstStructure *structure = gst_event_get_structure (event);
|
|
|
|
|
|
|
|
if (structure != NULL
|
|
|
|
&& gst_structure_has_name (structure, "application/x-gst-dvd")) {
|
2013-09-02 22:36:43 +00:00
|
|
|
res = gst_ps_demux_handle_dvd_event (demux, event);
|
2008-09-02 12:04:32 +00:00
|
|
|
} else {
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, event);
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-08-31 00:51:50 +00:00
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
2008-09-02 12:04:32 +00:00
|
|
|
default:
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, event);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_handle_seek_push (GstPsDemux * demux, GstEvent * event)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
gboolean res = FALSE;
|
2009-02-02 22:12:07 +00:00
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
|
|
|
gint64 bstart, bstop;
|
|
|
|
GstEvent *bevent;
|
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
|
|
|
|
&stop_type, &stop);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
|
|
|
|
" stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
|
|
|
|
GST_TIME_ARGS (stop));
|
|
|
|
|
|
|
|
if (format == GST_FORMAT_BYTES) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
|
|
|
|
goto not_supported;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first");
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
/* first try original format seek */
|
|
|
|
(void) gst_event_ref (event);
|
|
|
|
if ((res = gst_pad_push_event (demux->sinkpad, event)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
|
|
|
/* From here down, we only support time based seeks */
|
|
|
|
GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
|
|
|
|
goto not_supported;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
/* We need to convert to byte based seek and we need a scr_rate for that. */
|
|
|
|
if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "seek not possible, no scr_rate");
|
|
|
|
goto not_supported;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "try with scr_rate interpolation");
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
bstart = GSTTIME_TO_BYTES ((guint64) start);
|
|
|
|
bstop = GSTTIME_TO_BYTES ((guint64) stop);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
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);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
res = gst_pad_push_event (demux->sinkpad, bevent);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
done:
|
|
|
|
gst_event_unref (event);
|
|
|
|
return res;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
not_supported:
|
|
|
|
{
|
|
|
|
gst_event_unref (event);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
#define MAX_RECURSION_COUNT 100
|
|
|
|
|
|
|
|
/* Binary search for requested SCR */
|
|
|
|
static inline guint64
|
2013-09-02 22:36:43 +00:00
|
|
|
find_offset (GstPsDemux * demux, guint64 scr,
|
2012-01-26 12:13:06 +00:00
|
|
|
guint64 min_scr, guint64 min_scr_offset,
|
|
|
|
guint64 max_scr, guint64 max_scr_offset, int recursion_count)
|
2009-02-04 15:11:23 +00:00
|
|
|
{
|
2012-01-26 12:13:06 +00:00
|
|
|
guint64 scr_rate_n = max_scr_offset - min_scr_offset;
|
|
|
|
guint64 scr_rate_d = max_scr - min_scr;
|
|
|
|
guint64 fscr = scr;
|
|
|
|
guint64 offset;
|
|
|
|
|
|
|
|
if (recursion_count > MAX_RECURSION_COUNT) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = min_scr_offset +
|
|
|
|
MIN (gst_util_uint64_scale (scr - min_scr, scr_rate_n,
|
|
|
|
scr_rate_d), demux->sink_segment.stop);
|
|
|
|
|
2013-09-02 22:36:43 +00:00
|
|
|
if (!gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0)) {
|
|
|
|
gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
|
2012-01-26 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fscr == scr || fscr == min_scr || fscr == max_scr) {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fscr < scr) {
|
|
|
|
return find_offset (demux, scr, fscr, offset, max_scr, max_scr_offset,
|
|
|
|
recursion_count + 1);
|
|
|
|
} else {
|
|
|
|
return find_offset (demux, scr, min_scr, min_scr_offset, fscr, offset,
|
|
|
|
recursion_count + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_do_seek (GstPsDemux * demux, GstSegment * seeksegment)
|
2012-01-26 12:13:06 +00:00
|
|
|
{
|
2015-01-12 12:50:33 +00:00
|
|
|
gboolean found;
|
2009-02-04 15:11:23 +00:00
|
|
|
guint64 fscr, offset;
|
2012-01-26 12:13:06 +00:00
|
|
|
guint64 scr = GSTTIME_TO_MPEGTIME (seeksegment->position + demux->base_time);
|
2009-02-04 15:11:23 +00:00
|
|
|
|
2009-09-04 10:53:36 +00:00
|
|
|
/* In some clips the PTS values are completely unaligned with SCR values.
|
|
|
|
* To improve the seek in that situation we apply a factor considering the
|
|
|
|
* relationship between last PTS and last SCR */
|
|
|
|
if (demux->last_scr > demux->last_pts)
|
|
|
|
scr = gst_util_uint64_scale (scr, demux->last_scr, demux->last_pts);
|
|
|
|
|
2009-02-04 21:17:36 +00:00
|
|
|
scr = MIN (demux->last_scr, scr);
|
|
|
|
scr = MAX (demux->first_scr, scr);
|
|
|
|
fscr = scr;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT
|
|
|
|
", trying to go at SCR: %" G_GUINT64_FORMAT, &demux->sink_segment, scr);
|
2009-02-04 15:11:23 +00:00
|
|
|
|
2011-09-21 13:53:01 +00:00
|
|
|
offset =
|
2012-01-26 12:13:06 +00:00
|
|
|
find_offset (demux, scr, demux->first_scr, demux->first_scr_offset,
|
|
|
|
demux->last_scr, demux->last_scr_offset, 0);
|
2009-02-04 15:11:23 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (offset == (guint64) - 1) {
|
|
|
|
return FALSE;
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 12:50:33 +00:00
|
|
|
found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
|
|
|
|
if (!found)
|
|
|
|
found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
|
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
while (found && fscr < scr) {
|
|
|
|
offset++;
|
2013-09-02 22:36:43 +00:00
|
|
|
found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 21:17:36 +00:00
|
|
|
while (found && fscr > scr && offset > 0) {
|
2009-02-04 15:11:23 +00:00
|
|
|
offset--;
|
2013-09-02 22:36:43 +00:00
|
|
|
found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (demux, "doing seek at offset %" G_GUINT64_FORMAT
|
2009-02-04 21:17:36 +00:00
|
|
|
" SCR: %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT,
|
2009-02-04 15:11:23 +00:00
|
|
|
offset, fscr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (fscr)));
|
2012-01-26 12:13:06 +00:00
|
|
|
|
|
|
|
gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
|
|
|
|
|
|
|
|
return TRUE;
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_handle_seek_pull (GstPsDemux * demux, GstEvent * event)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
|
|
|
gdouble rate;
|
2011-04-14 23:09:14 +00:00
|
|
|
gboolean update, flush;
|
2009-02-02 22:12:07 +00:00
|
|
|
GstSegment seeksegment;
|
|
|
|
GstClockTime first_pts = MPEGTIME_TO_GSTTIME (demux->first_pts);
|
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags,
|
|
|
|
&start_type, &start, &stop_type, &stop);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME)
|
|
|
|
goto wrong_format;
|
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Seek requested start %" GST_TIME_FORMAT " stop %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
/* We need to convert to byte based seek and we need a scr_rate for that. */
|
|
|
|
if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64)
|
|
|
|
goto no_scr_rate;
|
|
|
|
|
|
|
|
flush = flags & GST_SEEK_FLAG_FLUSH;
|
2011-04-14 23:09:14 +00:00
|
|
|
/* keyframe = flags & GST_SEEK_FLAG_KEY_UNIT; *//* FIXME */
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
if (flush) {
|
|
|
|
/* Flush start up and downstream to make sure data flow and loops are
|
|
|
|
idle */
|
2009-08-07 16:17:28 +00:00
|
|
|
demux->flushing = TRUE;
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, gst_event_new_flush_start ());
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
|
|
|
|
} else {
|
|
|
|
/* Pause the pulling task */
|
|
|
|
gst_pad_pause_task (demux->sinkpad);
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
/* Take the stream lock */
|
|
|
|
GST_PAD_STREAM_LOCK (demux->sinkpad);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
if (flush) {
|
|
|
|
/* Stop flushing upstream we need to pull */
|
2009-08-07 16:17:28 +00:00
|
|
|
demux->flushing = FALSE;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop (TRUE));
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
/* Work on a copy until we are sure the seek succeeded. */
|
|
|
|
memcpy (&seeksegment, &demux->src_segment, sizeof (GstSegment));
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
|
|
|
|
&demux->src_segment);
|
|
|
|
|
|
|
|
/* Apply the seek to our segment */
|
2012-01-26 12:13:06 +00:00
|
|
|
if (!gst_segment_do_seek (&seeksegment, rate, format, flags,
|
|
|
|
start_type, start, stop_type, stop, &update))
|
|
|
|
goto seek_error;
|
2009-02-02 22:12:07 +00:00
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "seek segment configured %" GST_SEGMENT_FORMAT,
|
|
|
|
&seeksegment);
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (flush || seeksegment.position != demux->src_segment.position) {
|
2009-02-04 15:11:23 +00:00
|
|
|
/* Do the actual seeking */
|
2013-09-02 22:36:43 +00:00
|
|
|
if (!gst_ps_demux_do_seek (demux, &seeksegment)) {
|
2012-01-26 12:13:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check the limits */
|
2016-05-13 12:27:37 +00:00
|
|
|
if (seeksegment.rate > 0.0 && first_pts != G_MAXUINT64) {
|
2009-02-04 15:11:23 +00:00
|
|
|
if (seeksegment.start < first_pts - demux->base_time) {
|
|
|
|
seeksegment.start = first_pts - demux->base_time;
|
2012-01-26 12:13:06 +00:00
|
|
|
seeksegment.position = seeksegment.start;
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
/* update the rate in our src segment */
|
|
|
|
demux->sink_segment.rate = rate;
|
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "seek segment adjusted %" GST_SEGMENT_FORMAT,
|
2009-02-02 22:12:07 +00:00
|
|
|
&seeksegment);
|
|
|
|
|
|
|
|
if (flush) {
|
|
|
|
/* Stop flushing, the sinks are at time 0 now */
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, gst_event_new_flush_stop (TRUE));
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (flush || seeksegment.position != demux->src_segment.position) {
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_flush (demux);
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ok seek succeeded, take the newly configured segment */
|
|
|
|
memcpy (&demux->src_segment, &seeksegment, sizeof (GstSegment));
|
|
|
|
|
|
|
|
/* Notify about the start of a new segment */
|
|
|
|
if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
|
|
|
|
gst_element_post_message (GST_ELEMENT (demux),
|
|
|
|
gst_message_new_segment_start (GST_OBJECT (demux),
|
2012-01-26 12:13:06 +00:00
|
|
|
demux->src_segment.format, demux->src_segment.position));
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell all the stream a new segment is needed */
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_mark_discont (demux, TRUE, TRUE);
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
gst_pad_start_task (demux->sinkpad,
|
2013-09-02 22:36:43 +00:00
|
|
|
(GstTaskFunction) gst_ps_demux_loop, demux->sinkpad, NULL);
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
GST_PAD_STREAM_UNLOCK (demux->sinkpad);
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_format:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (demux, "we only support seeking in TIME or BYTES "
|
|
|
|
"formats");
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_scr_rate:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (demux, "seek not possible, no scr_rate");
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-01-26 12:13:06 +00:00
|
|
|
seek_error:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (demux, "couldn't perform seek");
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
gboolean res = FALSE;
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (parent);
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_SEEK:
|
|
|
|
if (demux->random_access) {
|
2013-09-02 22:36:43 +00:00
|
|
|
res = gst_ps_demux_handle_seek_pull (demux, event);
|
2009-02-02 22:12:07 +00:00
|
|
|
} else {
|
2013-09-02 22:36:43 +00:00
|
|
|
res = gst_ps_demux_handle_seek_push (demux, event);
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
2013-09-03 10:11:54 +00:00
|
|
|
case GST_EVENT_RECONFIGURE:{
|
|
|
|
GstPsStream *stream;
|
|
|
|
|
|
|
|
stream = gst_ps_demux_get_stream_from_pad (demux, pad);
|
|
|
|
if (stream != NULL)
|
|
|
|
stream->notlinked = FALSE;
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
default:
|
|
|
|
res = gst_pad_push_event (demux->sinkpad, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
gboolean res = FALSE;
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (parent);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT,
|
|
|
|
GST_QUERY_TYPE (query), pad);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_POSITION:
|
|
|
|
{
|
2011-09-21 16:29:52 +00:00
|
|
|
GstClockTime pos;
|
2008-09-02 12:04:32 +00:00
|
|
|
GstFormat format;
|
|
|
|
|
2012-08-31 00:51:50 +00:00
|
|
|
/* See if upstream can immediately answer */
|
|
|
|
res = gst_pad_peer_query (demux->sinkpad, query);
|
|
|
|
if (res)
|
|
|
|
break;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_query_parse_position (query, &format, NULL);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
2013-10-08 05:56:11 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "position not supported for format: %s",
|
|
|
|
gst_format_get_name (format));
|
2008-09-02 12:04:32 +00:00
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
pos = demux->src_segment.position - demux->src_segment.start;
|
2011-09-21 16:29:52 +00:00
|
|
|
GST_LOG_OBJECT (demux, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
|
2012-08-31 00:51:50 +00:00
|
|
|
|
2011-09-21 16:29:52 +00:00
|
|
|
gst_query_set_position (query, format, pos);
|
2008-09-02 12:04:32 +00:00
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_QUERY_DURATION:
|
|
|
|
{
|
|
|
|
GstFormat format;
|
|
|
|
gint64 duration;
|
2012-01-26 12:13:06 +00:00
|
|
|
GstQuery *byte_query;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
gst_query_parse_duration (query, &format, NULL);
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
if (G_LIKELY (format == GST_FORMAT_TIME &&
|
|
|
|
GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))) {
|
|
|
|
gst_query_set_duration (query, GST_FORMAT_TIME,
|
|
|
|
demux->src_segment.duration);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* For any format other than bytes, see if upstream knows first */
|
|
|
|
if (format == GST_FORMAT_BYTES) {
|
2013-10-08 05:56:11 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
|
|
|
|
gst_format_get_name (format));
|
2008-09-02 12:04:32 +00:00
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
2012-08-31 00:51:50 +00:00
|
|
|
if (gst_pad_peer_query (demux->sinkpad, query)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:35:42 +00:00
|
|
|
/* Upstream didn't know, so we can only answer TIME queries from
|
2008-09-02 12:04:32 +00:00
|
|
|
* here on */
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
2013-10-08 05:56:11 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
|
|
|
|
gst_format_get_name (format));
|
2008-09-02 12:04:32 +00:00
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (demux->mux_rate == -1) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate");
|
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
byte_query = gst_query_new_duration (GST_FORMAT_BYTES);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-08-31 00:51:50 +00:00
|
|
|
if (!gst_pad_peer_query (demux->sinkpad, byte_query)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_LOG_OBJECT (demux, "query on peer pad failed");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_query_unref (byte_query);
|
2008-09-02 12:04:32 +00:00
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_query_parse_duration (byte_query, &format, &duration);
|
|
|
|
gst_query_unref (byte_query);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2008-12-29 18:22:26 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"query on peer pad reported bytes %" G_GUINT64_FORMAT, duration);
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
duration = BYTES_TO_GSTTIME ((guint64) duration);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2008-12-29 18:22:26 +00:00
|
|
|
GST_LOG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (duration));
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_query_set_duration (query, GST_FORMAT_TIME, duration);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-21 11:39:21 +00:00
|
|
|
case GST_QUERY_SEEKING:{
|
|
|
|
GstFormat fmt;
|
|
|
|
|
|
|
|
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
if (demux->random_access) {
|
|
|
|
/* In pull mode we can seek in TIME format if we have the SCR */
|
|
|
|
if (fmt != GST_FORMAT_TIME || demux->scr_rate_n == G_MAXUINT64
|
|
|
|
|| demux->scr_rate_d == G_MAXUINT64)
|
|
|
|
gst_query_set_seeking (query, fmt, FALSE, -1, -1);
|
|
|
|
else
|
|
|
|
gst_query_set_seeking (query, fmt, TRUE, 0, -1);
|
|
|
|
} else {
|
|
|
|
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 seek if upstream supports BYTES seeks and we
|
|
|
|
* have the SCR
|
|
|
|
*/
|
|
|
|
peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
|
2009-08-05 07:36:00 +00:00
|
|
|
res = gst_pad_peer_query (demux->sinkpad, peerquery);
|
2009-07-21 11:39:21 +00:00
|
|
|
if (!res || demux->scr_rate_n == G_MAXUINT64
|
|
|
|
|| demux->scr_rate_d == G_MAXUINT64) {
|
|
|
|
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;
|
|
|
|
}
|
2013-07-29 09:02:34 +00:00
|
|
|
case GST_QUERY_SEGMENT:{
|
|
|
|
GstFormat format;
|
|
|
|
gint64 start, stop;
|
|
|
|
|
|
|
|
format = demux->src_segment.format;
|
|
|
|
|
|
|
|
start =
|
|
|
|
gst_segment_to_stream_time (&demux->src_segment, format,
|
|
|
|
demux->src_segment.start);
|
|
|
|
if ((stop = demux->src_segment.stop) == -1)
|
|
|
|
stop = demux->src_segment.duration;
|
|
|
|
else
|
|
|
|
stop = gst_segment_to_stream_time (&demux->src_segment, format, stop);
|
|
|
|
|
|
|
|
gst_query_set_segment (query, demux->src_segment.rate, format, start,
|
|
|
|
stop);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
default:
|
2012-01-26 12:13:06 +00:00
|
|
|
res = gst_pad_query_default (pad, parent, query);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-21 11:39:21 +00:00
|
|
|
beach:
|
2008-09-02 12:04:32 +00:00
|
|
|
return res;
|
|
|
|
not_supported:
|
2012-06-10 22:33:36 +00:00
|
|
|
return FALSE;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_reset_psm (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
#define FILL_TYPE(start, stop, type) \
|
|
|
|
for (i=start; i <= stop; i++) \
|
|
|
|
demux->psm[i] = type;
|
|
|
|
|
2014-02-25 10:29:06 +00:00
|
|
|
/* Initialize all fields to -1 first */
|
2013-09-02 22:36:43 +00:00
|
|
|
FILL_TYPE (0x00, GST_PS_DEMUX_MAX_PSM - 1, -1);
|
2014-02-25 10:29:06 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE);
|
2014-02-25 10:29:06 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3);
|
|
|
|
FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS);
|
2009-05-26 17:47:32 +00:00
|
|
|
FILL_TYPE (0xa0, 0xaf, ST_PS_AUDIO_LPCM);
|
2014-02-25 10:29:06 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1);
|
|
|
|
FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2);
|
|
|
|
|
|
|
|
#undef FILL_TYPE
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
/* ISO/IEC 13818-1:
|
|
|
|
* pack_header() {
|
|
|
|
* pack_start_code 32 bslbf -+
|
|
|
|
* '01' 2 bslbf |
|
|
|
|
* system_clock_reference_base [32..30] 3 bslbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* system_clock_reference_base [29..15] 15 bslbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* system_clock_reference_base [14..0] 15 bslbf |
|
|
|
|
* marker_bit 1 bslbf | 112 bits
|
|
|
|
* system_clock_reference_extension 9 ubslbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* program_mux_rate 22 ubslbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* reserved 5 bslbf |
|
|
|
|
* pack_stuffing_length 3 ubslbf -+
|
|
|
|
*
|
|
|
|
* for (i = 0; i < pack_stuffing_length; i++) {
|
|
|
|
* stuffing_byte '1111 1111' 8 bslbf
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 112 bits = 14 bytes, as max value for pack_stuffing_length is 7, then
|
|
|
|
* in total it's needed 14 + 7 = 21 bytes.
|
|
|
|
*/
|
|
|
|
#define PACK_START_SIZE 21
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_parse_pack_start (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
const guint8 *data;
|
|
|
|
guint length;
|
|
|
|
guint32 scr1, scr2;
|
|
|
|
guint64 scr, scr_adjusted, new_rate;
|
2008-12-29 18:22:26 +00:00
|
|
|
guint64 scr_rate_n;
|
|
|
|
guint64 scr_rate_d;
|
2009-08-07 16:17:28 +00:00
|
|
|
guint avail = gst_adapter_available (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG ("parsing pack start");
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (avail < PACK_START_SIZE))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto need_more_data;
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
data = gst_adapter_map (demux->adapter, PACK_START_SIZE);
|
2009-08-07 16:17:28 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* skip start code */
|
|
|
|
data += 4;
|
|
|
|
|
2010-01-10 13:06:55 +00:00
|
|
|
scr1 = GST_READ_UINT32_BE (data);
|
|
|
|
scr2 = GST_READ_UINT32_BE (data + 4);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
/* fixed length to begin with, start code and two scr values */
|
|
|
|
length = 8 + 4;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* start parsing the stream */
|
|
|
|
if ((*data & 0xc0) == 0x40) {
|
|
|
|
guint32 scr_ext;
|
|
|
|
guint32 next32;
|
|
|
|
guint8 stuffing_bytes;
|
|
|
|
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG ("Found MPEG2 stream");
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->is_mpeg2_pack = TRUE;
|
|
|
|
|
|
|
|
/* mpeg2 has more data */
|
|
|
|
length += 2;
|
|
|
|
|
|
|
|
/* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
|
|
|
|
|
|
|
|
/* check markers */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY ((scr1 & 0xc4000400) != 0x44000400))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto lost_sync;
|
|
|
|
|
|
|
|
scr = ((guint64) scr1 & 0x38000000) << 3;
|
|
|
|
scr |= ((guint64) scr1 & 0x03fff800) << 4;
|
|
|
|
scr |= ((guint64) scr1 & 0x000003ff) << 5;
|
|
|
|
scr |= ((guint64) scr2 & 0xf8000000) >> 27;
|
|
|
|
|
|
|
|
/* marker:1==1 ! scr_ext:9 ! marker:1==1 */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY ((scr2 & 0x04010000) != 0x04010000))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto lost_sync;
|
|
|
|
|
|
|
|
scr_ext = (scr2 & 0x03fe0000) >> 17;
|
|
|
|
/* We keep the offset of this scr */
|
2009-02-04 15:11:23 +00:00
|
|
|
demux->cur_scr_offset = demux->adapter_offset + 12;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux, "SCR: 0x%08" G_GINT64_MODIFIER "x SCRE: 0x%08x",
|
2009-10-11 10:56:29 +00:00
|
|
|
scr, scr_ext);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
if (scr_ext) {
|
|
|
|
scr = (scr * 300 + scr_ext % 300) / 300;
|
|
|
|
}
|
|
|
|
/* SCR has been converted into units of 90Khz ticks to make it comparable
|
|
|
|
to DTS/PTS, that also implies 1 tick rounding error */
|
|
|
|
data += 6;
|
|
|
|
/* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
|
2010-01-10 13:06:55 +00:00
|
|
|
next32 = GST_READ_UINT32_BE (data);
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY ((next32 & 0x00000300) != 0x00000300))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto lost_sync;
|
|
|
|
|
|
|
|
new_rate = (next32 & 0xfffffc00) >> 10;
|
|
|
|
|
|
|
|
stuffing_bytes = (next32 & 0x07);
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
data += 4;
|
2009-08-07 16:17:28 +00:00
|
|
|
length += stuffing_bytes;
|
2008-09-02 12:04:32 +00:00
|
|
|
while (stuffing_bytes--) {
|
|
|
|
if (*data++ != 0xff)
|
|
|
|
goto lost_sync;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_DEBUG ("Found MPEG1 stream");
|
|
|
|
demux->is_mpeg2_pack = FALSE;
|
|
|
|
|
|
|
|
/* check markers */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY ((scr1 & 0xf1000100) != 0x21000100))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto lost_sync;
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY ((scr2 & 0x01800001) != 0x01800001))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto lost_sync;
|
|
|
|
|
|
|
|
/* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
|
|
|
|
scr = ((guint64) scr1 & 0x0e000000) << 5;
|
|
|
|
scr |= ((guint64) scr1 & 0x00fffe00) << 6;
|
|
|
|
scr |= ((guint64) scr1 & 0x000000ff) << 7;
|
|
|
|
scr |= ((guint64) scr2 & 0xfe000000) >> 25;
|
|
|
|
|
|
|
|
/* We keep the offset of this scr */
|
2009-02-04 15:11:23 +00:00
|
|
|
demux->cur_scr_offset = demux->adapter_offset + 8;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* marker:1==1 ! mux_rate:22 ! marker:1==1 */
|
|
|
|
new_rate = (scr2 & 0x007ffffe) >> 1;
|
|
|
|
|
|
|
|
data += 8;
|
|
|
|
}
|
|
|
|
new_rate *= MPEG_MUX_RATE_MULT;
|
|
|
|
|
|
|
|
/* scr adjusted is the new scr found + the colected adjustment */
|
|
|
|
scr_adjusted = scr + demux->scr_adjust;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %"
|
|
|
|
G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT,
|
|
|
|
scr, scr_adjusted, new_rate,
|
2009-02-04 21:50:30 +00:00
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr)));
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* keep the first src in order to calculate delta time */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (demux->first_scr == G_MAXUINT64)) {
|
2012-10-12 13:44:03 +00:00
|
|
|
gint64 diff;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->first_scr = scr;
|
2009-02-04 15:11:23 +00:00
|
|
|
demux->first_scr_offset = demux->cur_scr_offset;
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
|
2012-10-12 13:44:03 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "determined base_time %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (demux->base_time));
|
2008-09-02 12:04:32 +00:00
|
|
|
/* at begin consider the new_rate as the scr rate, bytes/clock ticks */
|
2008-12-29 18:22:26 +00:00
|
|
|
scr_rate_n = new_rate;
|
|
|
|
scr_rate_d = CLOCK_FREQ;
|
2012-10-12 13:44:03 +00:00
|
|
|
/* our SCR timeline might have offset wrt upstream timeline */
|
|
|
|
if (demux->sink_segment.format == GST_FORMAT_TIME) {
|
|
|
|
if (demux->sink_segment.start > demux->base_time)
|
|
|
|
diff = -(demux->sink_segment.start - demux->base_time);
|
|
|
|
else
|
|
|
|
diff = demux->base_time - demux->sink_segment.start;
|
|
|
|
if (diff > GST_SECOND) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "diff of %" GST_TIME_FORMAT
|
|
|
|
" wrt upstream start %" GST_TIME_FORMAT "; adjusting base",
|
|
|
|
GST_TIME_ARGS (diff), GST_TIME_ARGS (demux->sink_segment.start));
|
|
|
|
demux->base_time += diff;
|
|
|
|
}
|
|
|
|
}
|
2009-08-07 16:17:28 +00:00
|
|
|
} else if (G_LIKELY (demux->first_scr_offset != demux->cur_scr_offset)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
/* estimate byte rate related to the SCR */
|
2009-02-04 15:11:23 +00:00
|
|
|
scr_rate_n = demux->cur_scr_offset - demux->first_scr_offset;
|
2008-12-29 18:22:26 +00:00
|
|
|
scr_rate_d = scr_adjusted - demux->first_scr;
|
2009-01-02 01:12:35 +00:00
|
|
|
} else {
|
|
|
|
scr_rate_n = demux->scr_rate_n;
|
|
|
|
scr_rate_d = demux->scr_rate_d;
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %"
|
2008-09-02 12:04:32 +00:00
|
|
|
G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT
|
|
|
|
" at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT
|
|
|
|
"/%" G_GUINT64_FORMAT "(%f)",
|
|
|
|
((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"),
|
2009-02-04 15:11:23 +00:00
|
|
|
scr, demux->cur_scr_offset,
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->first_scr, demux->first_scr_offset,
|
2008-12-29 18:22:26 +00:00
|
|
|
scr_rate_n, scr_rate_d, (float) scr_rate_n / scr_rate_d);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* adjustment of the SCR */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_LIKELY (demux->current_scr != G_MAXUINT64)) {
|
2011-11-29 11:20:51 +00:00
|
|
|
guint64 diff;
|
2008-10-08 10:21:20 +00:00
|
|
|
guint64 old_scr, old_mux_rate, bss, adjust = 0;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* keep SCR of the previous packet */
|
|
|
|
old_scr = demux->current_scr;
|
|
|
|
old_mux_rate = demux->mux_rate;
|
|
|
|
|
|
|
|
/* Bytes since SCR is the amount we placed in the adapter since then
|
|
|
|
* (demux->bytes_since_scr) minus the amount remaining in the adapter,
|
|
|
|
* clamped to >= 0 */
|
2009-08-07 16:17:28 +00:00
|
|
|
bss = MAX (0, (gint) (demux->bytes_since_scr - avail));
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* estimate the new SCR using the previous one according the notes
|
|
|
|
on point 2.5.2.2 of the ISO/IEC 13818-1 document */
|
2008-10-08 10:21:20 +00:00
|
|
|
if (old_mux_rate != 0)
|
|
|
|
adjust = (bss * CLOCK_FREQ) / old_mux_rate;
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
if (demux->sink_segment.rate >= 0.0)
|
|
|
|
demux->next_scr = old_scr + adjust;
|
|
|
|
else
|
|
|
|
demux->next_scr = old_scr - adjust;
|
|
|
|
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
2008-09-02 12:04:32 +00:00
|
|
|
"bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT
|
|
|
|
", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT,
|
|
|
|
bss, demux->next_scr, old_scr, scr_adjusted);
|
|
|
|
|
|
|
|
/* calculate the absolute deference between the last scr and
|
|
|
|
the new one */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (old_scr > scr_adjusted))
|
2008-09-02 12:04:32 +00:00
|
|
|
diff = old_scr - scr_adjusted;
|
|
|
|
else
|
|
|
|
diff = scr_adjusted - old_scr;
|
|
|
|
|
|
|
|
/* if the difference is more than 1 second we need to reconfigure
|
|
|
|
adjustment */
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (diff > CLOCK_FREQ)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->scr_adjust = demux->next_scr - scr;
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT
|
2008-09-02 12:04:32 +00:00
|
|
|
", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust);
|
|
|
|
scr_adjusted = demux->next_scr;
|
2008-12-29 18:22:26 +00:00
|
|
|
/* don't update rate estimation on disconts */
|
|
|
|
scr_rate_n = demux->scr_rate_n;
|
|
|
|
scr_rate_d = demux->scr_rate_d;
|
2008-09-02 12:04:32 +00:00
|
|
|
} else {
|
|
|
|
demux->next_scr = scr_adjusted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update the current_scr and rate members */
|
|
|
|
demux->mux_rate = new_rate;
|
|
|
|
demux->current_scr = scr_adjusted;
|
2008-12-29 18:22:26 +00:00
|
|
|
demux->scr_rate_n = scr_rate_n;
|
|
|
|
demux->scr_rate_d = scr_rate_d;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* Reset the bytes_since_scr value to count the data remaining in the
|
|
|
|
* adapter */
|
2009-08-07 16:17:28 +00:00
|
|
|
demux->bytes_since_scr = avail;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_adapter_flush (demux->adapter, length);
|
|
|
|
ADAPTER_OFFSET_FLUSH (length);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
lost_sync:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "lost sync");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_LOST_SYNC;
|
|
|
|
}
|
|
|
|
need_more_data:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "need more data");
|
|
|
|
return GST_FLOW_NEED_MORE_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
/* ISO/IEC 13818-1:
|
|
|
|
* system_header () {
|
|
|
|
* system_header_start_code 32 bslbf -+
|
|
|
|
* header_length 16 uimsbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* rate_bound 22 uimsbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* audio_bound 6 uimsbf |
|
|
|
|
* fixed_flag 1 bslbf |
|
|
|
|
* CSPS_flag 1 bslbf | 96 bits
|
|
|
|
* system_audio_lock_flag 1 bslbf |
|
|
|
|
* system_video_lock_flag 1 bslbf |
|
|
|
|
* marker_bit 1 bslbf |
|
|
|
|
* video_bound 5 uimsbf |
|
|
|
|
* packet_rate_restriction_flag 1 bslbf |
|
|
|
|
* reserved_bits 7 bslbf -+
|
|
|
|
* while (nextbits () = = '1') {
|
|
|
|
* stream_id 8 uimsbf -+
|
|
|
|
* '11' 2 bslbf | 24 bits
|
|
|
|
* P-STD_buffer_bound_scale 1 bslbf |
|
|
|
|
* P-STD_buffer_size_bound 13 uimsbf -+
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* 96 bits = 12 bytes, 24 bits = 3 bytes.
|
|
|
|
*/
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_parse_sys_head (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
guint16 length;
|
|
|
|
const guint8 *data;
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean csps;
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (gst_adapter_available (demux->adapter) < 6)
|
2008-09-02 12:04:32 +00:00
|
|
|
goto need_more_data;
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
/* start code + length */
|
|
|
|
data = gst_adapter_map (demux->adapter, 6);
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* skip start code */
|
|
|
|
data += 4;
|
|
|
|
|
|
|
|
length = GST_READ_UINT16_BE (data);
|
|
|
|
GST_DEBUG_OBJECT (demux, "length %d", length);
|
|
|
|
|
|
|
|
length += 6;
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
|
|
|
if (gst_adapter_available (demux->adapter) < length)
|
2008-09-02 12:04:32 +00:00
|
|
|
goto need_more_data;
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
data = gst_adapter_map (demux->adapter, length);
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* skip start code and length */
|
|
|
|
data += 6;
|
|
|
|
|
|
|
|
/* marker:1==1 ! rate_bound:22 | marker:1==1 */
|
|
|
|
if ((*data & 0x80) != 0x80)
|
|
|
|
goto marker_expected;
|
|
|
|
|
|
|
|
{
|
|
|
|
guint32 rate_bound;
|
|
|
|
|
|
|
|
if ((data[2] & 0x01) != 0x01)
|
|
|
|
goto marker_expected;
|
|
|
|
|
|
|
|
rate_bound = ((guint32) data[0] & 0x7f) << 15;
|
|
|
|
rate_bound |= ((guint32) data[1]) << 7;
|
|
|
|
rate_bound |= ((guint32) data[2] & 0xfe) >> 1;
|
|
|
|
rate_bound *= MPEG_MUX_RATE_MULT;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound);
|
|
|
|
|
|
|
|
data += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* audio_bound:6==1 ! fixed:1 | constrained:1 */
|
|
|
|
{
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
guint8 audio_bound;
|
|
|
|
gboolean fixed;
|
|
|
|
|
|
|
|
/* max number of simultaneous audio streams active */
|
|
|
|
audio_bound = (data[0] & 0xfc) >> 2;
|
|
|
|
/* fixed or variable bitrate */
|
|
|
|
fixed = (data[0] & 0x02) == 0x02;
|
|
|
|
/* meeting constraints */
|
|
|
|
csps = (data[0] & 0x01) == 0x01;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d",
|
|
|
|
audio_bound, fixed, csps);
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
data += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */
|
|
|
|
{
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean audio_lock;
|
|
|
|
gboolean video_lock;
|
|
|
|
guint8 video_bound;
|
|
|
|
|
|
|
|
audio_lock = (data[0] & 0x80) == 0x80;
|
|
|
|
video_lock = (data[0] & 0x40) == 0x40;
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
if ((data[0] & 0x20) != 0x20)
|
|
|
|
goto marker_expected;
|
|
|
|
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
/* max number of simultaneous video streams active */
|
|
|
|
video_bound = (data[0] & 0x1f);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d",
|
|
|
|
audio_lock, video_lock, video_bound);
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
data += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* packet_rate_restriction:1 | reserved:7==0x7F */
|
|
|
|
{
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean packet_rate_restriction;
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
if ((data[0] & 0x7f) != 0x7f)
|
|
|
|
goto marker_expected;
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
/* only valid if csps is set */
|
|
|
|
if (csps) {
|
|
|
|
packet_rate_restriction = (data[0] & 0x80) == 0x80;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d",
|
|
|
|
packet_rate_restriction);
|
|
|
|
}
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
data += 1;
|
|
|
|
|
|
|
|
{
|
|
|
|
gint stream_count = (length - 12) / 3;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count);
|
|
|
|
|
|
|
|
for (i = 0; i < stream_count; i++) {
|
|
|
|
guint8 stream_id;
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean STD_buffer_bound_scale;
|
|
|
|
guint16 STD_buffer_size_bound;
|
|
|
|
guint32 buf_byte_size_bound;
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
stream_id = *data++;
|
|
|
|
if (!(stream_id & 0x80))
|
|
|
|
goto sys_len_error;
|
|
|
|
|
|
|
|
/* check marker bits */
|
|
|
|
if ((*data & 0xC0) != 0xC0)
|
|
|
|
goto no_placeholder_bits;
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
STD_buffer_bound_scale = *data & 0x20;
|
|
|
|
STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8;
|
|
|
|
STD_buffer_size_bound |= *data++;
|
|
|
|
|
|
|
|
if (STD_buffer_bound_scale == 0) {
|
|
|
|
buf_byte_size_bound = STD_buffer_size_bound * 128;
|
|
|
|
} else {
|
|
|
|
buf_byte_size_bound = STD_buffer_size_bound * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d",
|
|
|
|
STD_buffer_bound_scale);
|
|
|
|
GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes",
|
|
|
|
STD_buffer_size_bound, buf_byte_size_bound);
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_adapter_flush (demux->adapter, length);
|
|
|
|
ADAPTER_OFFSET_FLUSH (length);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
marker_expected:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "expecting marker");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_LOST_SYNC;
|
|
|
|
}
|
|
|
|
no_placeholder_bits:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "expecting placeholder bit values"
|
|
|
|
" '11' after stream id");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_LOST_SYNC;
|
|
|
|
}
|
|
|
|
sys_len_error:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "error in system header length");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_LOST_SYNC;
|
|
|
|
}
|
|
|
|
need_more_data:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "need more data");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_NEED_MORE_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_parse_psm (GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2017-02-01 03:25:32 +00:00
|
|
|
guint16 psm_length, info_length = 0, es_map_length = 0;
|
2008-09-02 12:04:32 +00:00
|
|
|
guint8 psm_version = 0;
|
2017-02-01 03:25:32 +00:00
|
|
|
GstByteReader br;
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean applicable;
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
/* Need at least 6 bytes for start code + length */
|
2012-01-26 12:13:06 +00:00
|
|
|
if (gst_adapter_available (demux->adapter) < 6)
|
2008-09-02 12:04:32 +00:00
|
|
|
goto need_more_data;
|
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
{
|
|
|
|
const guint8 *data;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
/* start code + length */
|
|
|
|
data = gst_adapter_map (demux->adapter, 6);
|
|
|
|
/* skip start code */
|
|
|
|
data += 4;
|
|
|
|
psm_length = GST_READ_UINT16_BE (data);
|
|
|
|
GST_DEBUG_OBJECT (demux, "PSM length %u", psm_length);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
if (G_UNLIKELY (psm_length > 0x3FA))
|
|
|
|
goto psm_len_error;
|
|
|
|
psm_length += 6; /* Add start code + size to length */
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
if (gst_adapter_available (demux->adapter) < psm_length)
|
|
|
|
goto need_more_data;
|
2012-01-26 12:13:06 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
data = gst_adapter_map (demux->adapter, psm_length);
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
gst_byte_reader_init (&br, data, psm_length);
|
|
|
|
}
|
2012-01-26 12:13:06 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
/* skip start code and length */
|
2017-02-01 03:25:32 +00:00
|
|
|
if (!gst_byte_reader_skip (&br, 6))
|
|
|
|
goto fail_invalid;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* Read PSM applicable bit together with version */
|
2017-02-01 03:25:32 +00:00
|
|
|
if (!gst_byte_reader_get_uint8 (&br, &psm_version))
|
|
|
|
goto fail_invalid;
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2008-09-02 12:04:32 +00:00
|
|
|
applicable = (psm_version & 0x80) >> 7;
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
2008-09-02 12:04:32 +00:00
|
|
|
psm_version &= 0x1F;
|
|
|
|
GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version,
|
|
|
|
applicable);
|
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
/* Jump over the next byte (marker bit) */
|
|
|
|
if (!gst_byte_reader_skip (&br, 1))
|
|
|
|
goto fail_invalid;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* Read PS info length */
|
2017-02-01 03:25:32 +00:00
|
|
|
if (!gst_byte_reader_get_uint16_be (&br, &info_length))
|
|
|
|
goto fail_invalid;
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length);
|
2017-02-01 03:25:32 +00:00
|
|
|
/* Skip the PS info, we don't use it */
|
|
|
|
if (!gst_byte_reader_skip (&br, info_length))
|
|
|
|
goto fail_invalid;
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* Read ES map length */
|
2017-02-01 03:25:32 +00:00
|
|
|
if (!gst_byte_reader_get_uint16_be (&br, &es_map_length))
|
|
|
|
goto fail_invalid;
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length);
|
|
|
|
|
|
|
|
/* Now read the ES map */
|
2017-02-01 03:25:32 +00:00
|
|
|
{
|
|
|
|
GstByteReader es_map_br;
|
|
|
|
if (!gst_byte_reader_get_sub_reader (&br, &es_map_br, es_map_length))
|
|
|
|
goto fail_invalid;
|
|
|
|
|
|
|
|
while (gst_byte_reader_get_remaining (&es_map_br) >= 4) {
|
|
|
|
guint8 stream_type = 0, stream_id = 0;
|
|
|
|
guint16 stream_info_length = 0;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint8 (&es_map_br, &stream_type) ||
|
|
|
|
!gst_byte_reader_get_uint8 (&es_map_br, &stream_id) ||
|
|
|
|
!gst_byte_reader_get_uint16_be (&es_map_br, &stream_info_length))
|
|
|
|
break;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Stream type %02X with id %02X and %u bytes info", stream_type,
|
|
|
|
stream_id, stream_info_length);
|
|
|
|
|
|
|
|
if (G_LIKELY (stream_id != 0xbd))
|
|
|
|
demux->psm[stream_id] = stream_type;
|
|
|
|
else {
|
|
|
|
/* Ignore stream type for private_stream_1 and discover it looking at
|
|
|
|
* the stream data.
|
|
|
|
* Fixes demuxing some clips with lpcm that was wrongly declared as
|
|
|
|
* mpeg audio */
|
|
|
|
GST_DEBUG_OBJECT (demux, "stream type for private_stream_1 ignored");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: We could use the descriptors instead of skipping them */
|
|
|
|
if (!gst_byte_reader_skip (&es_map_br, stream_info_length))
|
|
|
|
break;
|
2009-08-07 16:35:42 +00:00
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
2017-02-01 03:25:32 +00:00
|
|
|
/* We ignore the 4-byte CRC at the end */
|
2008-09-02 12:04:32 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2017-02-01 03:25:32 +00:00
|
|
|
gst_adapter_flush (demux->adapter, psm_length);
|
|
|
|
ADAPTER_OFFSET_FLUSH (psm_length);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2017-02-01 03:25:32 +00:00
|
|
|
fail_invalid:
|
|
|
|
GST_DEBUG_OBJECT (demux, "Failed to parse PSM. Skipping");
|
|
|
|
gst_adapter_unmap (demux->adapter);
|
|
|
|
gst_adapter_flush (demux->adapter, psm_length);
|
|
|
|
ADAPTER_OFFSET_FLUSH (psm_length);
|
|
|
|
return GST_FLOW_LOST_SYNC;
|
2008-09-02 12:04:32 +00:00
|
|
|
psm_len_error:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "error in PSM length");
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return GST_FLOW_LOST_SYNC;
|
|
|
|
}
|
|
|
|
need_more_data:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "need more data");
|
|
|
|
return GST_FLOW_NEED_MORE_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_resync_cb (GstPESFilter * filter, GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_data_cb (GstPESFilter * filter, gboolean first,
|
|
|
|
GstBuffer * buffer, GstPsDemux * demux)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
GstBuffer *out_buf;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
gint stream_type;
|
|
|
|
guint32 start_code;
|
|
|
|
guint8 id;
|
2012-01-26 12:13:06 +00:00
|
|
|
GstMapInfo map;
|
|
|
|
gsize datalen;
|
2008-09-02 12:04:32 +00:00
|
|
|
guint offset = 0;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_READ);
|
|
|
|
datalen = map.size;
|
2008-09-02 12:04:32 +00:00
|
|
|
start_code = filter->start_code;
|
|
|
|
id = filter->id;
|
|
|
|
if (first) {
|
2019-08-03 09:28:33 +00:00
|
|
|
gint layer = 0;
|
2008-09-02 12:04:32 +00:00
|
|
|
/* find the stream type */
|
|
|
|
stream_type = demux->psm[id];
|
|
|
|
if (stream_type == -1) {
|
|
|
|
/* no stream type, if PS1, get the new id */
|
|
|
|
if (start_code == ID_PRIVATE_STREAM_1 && datalen >= 2) {
|
2009-08-07 16:17:28 +00:00
|
|
|
/* VDR writes A52 streams without any header bytes
|
2008-09-02 12:04:32 +00:00
|
|
|
* (see ftp://ftp.mplayerhq.hu/MPlayer/samples/MPEG-VOB/vdr-AC3) */
|
|
|
|
if (datalen >= 4) {
|
2012-01-26 12:13:06 +00:00
|
|
|
guint hdr = GST_READ_UINT32_BE (map.data);
|
2008-09-02 12:04:32 +00:00
|
|
|
if (G_UNLIKELY ((hdr & 0xffff0000) == AC3_SYNC_WORD)) {
|
|
|
|
id = 0x80;
|
|
|
|
stream_type = demux->psm[id] = ST_GST_AUDIO_RAWA52;
|
|
|
|
GST_DEBUG_OBJECT (demux, "Found VDR raw A52 stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_LIKELY (stream_type == -1)) {
|
2009-05-20 23:41:47 +00:00
|
|
|
/* new id is in the first byte */
|
2012-01-26 12:13:06 +00:00
|
|
|
id = map.data[offset++];
|
2009-05-20 23:41:47 +00:00
|
|
|
datalen--;
|
2008-09-02 12:04:32 +00:00
|
|
|
/* and remap */
|
|
|
|
stream_type = demux->psm[id];
|
2009-05-20 23:41:47 +00:00
|
|
|
/* Now, if it's a subpicture stream - no more, otherwise
|
|
|
|
* take the first byte too, since it's the frame count in audio
|
|
|
|
* streams and our backwards compat convention is to strip it off */
|
|
|
|
if (stream_type != ST_PS_DVD_SUBPICTURE) {
|
|
|
|
/* Number of audio frames in this packet */
|
2013-07-29 07:35:08 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
guint8 nframes;
|
|
|
|
nframes = map.data[offset];
|
2013-04-11 20:15:07 +00:00
|
|
|
GST_LOG_OBJECT (demux, "private type 0x%02x, %d frames", id,
|
2009-05-20 23:41:47 +00:00
|
|
|
nframes);
|
2013-07-29 07:35:08 +00:00
|
|
|
#endif
|
|
|
|
offset++;
|
|
|
|
datalen--;
|
2009-05-20 23:41:47 +00:00
|
|
|
} else {
|
2017-02-01 03:25:32 +00:00
|
|
|
GST_LOG_OBJECT (demux, "private type 0x%02x, stream type %d",
|
|
|
|
id, stream_type);
|
2009-05-20 23:41:47 +00:00
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stream_type == -1)
|
|
|
|
goto unknown_stream_type;
|
2019-08-03 09:28:33 +00:00
|
|
|
} else if (stream_type == ST_AUDIO_MPEG1 || stream_type == ST_AUDIO_MPEG2) {
|
|
|
|
if (datalen >= 2) {
|
|
|
|
guint hdr = GST_READ_UINT16_BE (map.data);
|
|
|
|
if ((hdr & 0xfff0) == 0xfff0) {
|
|
|
|
switch (hdr & 0x06) {
|
|
|
|
case 0x6:
|
|
|
|
layer = 1;
|
|
|
|
break;
|
|
|
|
case 0x4:
|
|
|
|
layer = 2;
|
|
|
|
break;
|
|
|
|
case 0x2:
|
|
|
|
layer = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_WARNING_OBJECT (demux, "unknown mpeg audio layer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
2019-08-03 09:28:33 +00:00
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
if (filter->pts != -1) {
|
|
|
|
demux->next_pts = filter->pts + demux->scr_adjust;
|
2014-12-09 08:58:03 +00:00
|
|
|
GST_LOG_OBJECT (demux, "stream 0x%02x PTS = orig %" G_GUINT64_FORMAT
|
|
|
|
" (%" G_GUINT64_FORMAT ")", id, filter->pts, demux->next_pts);
|
2008-09-02 12:04:32 +00:00
|
|
|
} else
|
|
|
|
demux->next_pts = G_MAXUINT64;
|
|
|
|
if (filter->dts != -1) {
|
|
|
|
demux->next_dts = filter->dts + demux->scr_adjust;
|
2014-12-09 08:58:03 +00:00
|
|
|
GST_LOG_OBJECT (demux, "stream 0x%02x DTS = orig %" G_GUINT64_FORMAT
|
|
|
|
" (%" G_GUINT64_FORMAT ")", id, filter->dts, demux->next_dts);
|
2008-09-02 12:04:32 +00:00
|
|
|
} else {
|
|
|
|
demux->next_dts = demux->next_pts;
|
|
|
|
}
|
|
|
|
|
2019-08-03 09:28:33 +00:00
|
|
|
demux->current_stream =
|
|
|
|
gst_ps_demux_get_stream (demux, id, stream_type, layer);
|
2008-09-02 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (demux->current_stream == NULL)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Dropping buffer for unknown stream id 0x%02x",
|
|
|
|
id);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After 2 seconds of bitstream emit no more pads */
|
|
|
|
if (demux->need_no_more_pads
|
|
|
|
&& (demux->current_scr - demux->first_scr) > 2 * CLOCK_FREQ) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "no more pads, notifying");
|
|
|
|
gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
|
|
|
|
demux->need_no_more_pads = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the stream is not-linked, don't bother creating a sub-buffer
|
|
|
|
* to send to it, unless we're processing a discont (which resets
|
|
|
|
* the not-linked status and tries again */
|
|
|
|
if (demux->current_stream->discont) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "stream is discont");
|
|
|
|
demux->current_stream->notlinked = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (demux->current_stream->notlinked == FALSE) {
|
2012-01-26 12:13:06 +00:00
|
|
|
out_buf =
|
|
|
|
gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, datalen);
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_send_data (demux, demux->current_stream, out_buf);
|
2008-09-02 12:04:32 +00:00
|
|
|
if (ret == GST_FLOW_NOT_LINKED) {
|
|
|
|
demux->current_stream->notlinked = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_buffer_unmap (buffer, &map);
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
return ret;
|
|
|
|
/* ERRORS */
|
|
|
|
unknown_stream_type:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (demux, "unknown stream type %02x", id);
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_resync (GstPsDemux * demux, gboolean save)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
const guint8 *data;
|
|
|
|
gint avail;
|
|
|
|
guint32 code;
|
|
|
|
gint offset;
|
|
|
|
gboolean found;
|
|
|
|
avail = gst_adapter_available (demux->adapter);
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (avail < 4))
|
2008-09-02 12:04:32 +00:00
|
|
|
goto need_data;
|
|
|
|
/* Common case, read 4 bytes an check it */
|
2012-01-26 12:13:06 +00:00
|
|
|
data = gst_adapter_map (demux->adapter, 4);
|
2008-09-02 12:04:32 +00:00
|
|
|
/* read currect code */
|
|
|
|
code = GST_READ_UINT32_BE (data);
|
|
|
|
/* The common case is that the sync code is at 0 bytes offset */
|
|
|
|
if (G_LIKELY ((code & 0xffffff00) == 0x100L)) {
|
|
|
|
GST_LOG_OBJECT (demux, "Found resync code %08x after 0 bytes", code);
|
|
|
|
demux->last_sync_code = code;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-08-07 16:17:28 +00:00
|
|
|
/* Otherwise, we are starting at byte 4 and we need to search
|
2008-09-02 12:04:32 +00:00
|
|
|
the sync code in all available data in the adapter */
|
|
|
|
offset = 4;
|
|
|
|
if (offset >= avail)
|
|
|
|
goto need_data; /* Not enough data to find sync */
|
2012-01-26 12:13:06 +00:00
|
|
|
data = gst_adapter_map (demux->adapter, avail);
|
2008-09-02 12:04:32 +00:00
|
|
|
do {
|
|
|
|
code = (code << 8) | data[offset++];
|
|
|
|
found = (code & 0xffffff00) == 0x100L;
|
|
|
|
} while (offset < avail && !found);
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_adapter_unmap (demux->adapter);
|
2008-09-02 12:04:32 +00:00
|
|
|
if (!save || demux->sink_segment.rate >= 0.0) {
|
|
|
|
GST_LOG_OBJECT (demux, "flushing %d bytes", offset - 4);
|
|
|
|
/* forward playback, we can discard and flush the skipped bytes */
|
|
|
|
gst_adapter_flush (demux->adapter, offset - 4);
|
|
|
|
ADAPTER_OFFSET_FLUSH (offset - 4);
|
|
|
|
} else {
|
|
|
|
if (found) {
|
|
|
|
GST_LOG_OBJECT (demux, "reverse saving %d bytes", offset - 4);
|
|
|
|
/* reverse playback, we keep the flushed bytes and we will append them to
|
|
|
|
* the next buffer in the chain function, which is the previous buffer in
|
|
|
|
* the stream. */
|
|
|
|
gst_adapter_push (demux->rev_adapter,
|
|
|
|
gst_adapter_take_buffer (demux->adapter, offset - 4));
|
|
|
|
} else {
|
|
|
|
GST_LOG_OBJECT (demux, "reverse saving %d bytes", avail);
|
|
|
|
/* nothing found, keep all bytes */
|
|
|
|
gst_adapter_push (demux->rev_adapter,
|
|
|
|
gst_adapter_take_buffer (demux->adapter, avail));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
GST_LOG_OBJECT (demux, "Found resync code %08x after %d bytes",
|
|
|
|
code, offset - 4);
|
|
|
|
demux->last_sync_code = code;
|
|
|
|
} else {
|
|
|
|
GST_LOG_OBJECT (demux, "No resync after skipping %d", offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
need_data:
|
|
|
|
{
|
|
|
|
GST_LOG_OBJECT (demux, "we need more data for resync %d", avail);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_is_pes_sync (guint32 sync)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
|
|
|
return ((sync & 0xfc) == 0xbc) ||
|
|
|
|
((sync & 0xe0) == 0xc0) || ((sync & 0xf0) == 0xe0);
|
|
|
|
}
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_scan_ts (GstPsDemux * demux, const guint8 * data,
|
2016-05-15 09:05:48 +00:00
|
|
|
SCAN_MODE mode, guint64 * rts, const guint8 * end)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
guint32 scr1, scr2;
|
|
|
|
guint64 scr;
|
|
|
|
guint64 pts, dts;
|
|
|
|
guint32 code;
|
2016-05-15 09:05:48 +00:00
|
|
|
guint16 len;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* read the 4 bytes for the sync code */
|
|
|
|
code = GST_READ_UINT32_BE (data);
|
|
|
|
if (G_LIKELY (code != ID_PS_PACK_START_CODE))
|
|
|
|
goto beach;
|
2017-02-01 03:37:29 +00:00
|
|
|
if (data + 12 > end)
|
|
|
|
goto beach;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* skip start code */
|
|
|
|
data += 4;
|
2010-01-10 13:06:55 +00:00
|
|
|
scr1 = GST_READ_UINT32_BE (data);
|
|
|
|
scr2 = GST_READ_UINT32_BE (data + 4);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* start parsing the stream */
|
|
|
|
if ((*data & 0xc0) == 0x40) {
|
2016-05-15 09:05:48 +00:00
|
|
|
/* MPEG-2 PACK header */
|
2009-02-02 22:12:07 +00:00
|
|
|
guint32 scr_ext;
|
|
|
|
guint32 next32;
|
|
|
|
guint8 stuffing_bytes;
|
|
|
|
/* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
|
|
|
|
/* check markers */
|
|
|
|
if ((scr1 & 0xc4000400) != 0x44000400)
|
|
|
|
goto beach;
|
|
|
|
scr = ((guint64) scr1 & 0x38000000) << 3;
|
|
|
|
scr |= ((guint64) scr1 & 0x03fff800) << 4;
|
|
|
|
scr |= ((guint64) scr1 & 0x000003ff) << 5;
|
|
|
|
scr |= ((guint64) scr2 & 0xf8000000) >> 27;
|
|
|
|
/* marker:1==1 ! scr_ext:9 ! marker:1==1 */
|
|
|
|
if ((scr2 & 0x04010000) != 0x04010000)
|
|
|
|
goto beach;
|
|
|
|
scr_ext = (scr2 & 0x03fe0000) >> 17;
|
|
|
|
if (scr_ext) {
|
|
|
|
scr = (scr * 300 + scr_ext % 300) / 300;
|
|
|
|
}
|
|
|
|
/* SCR has been converted into units of 90Khz ticks to make it comparable
|
|
|
|
to DTS/PTS, that also implies 1 tick rounding error */
|
|
|
|
data += 6;
|
2017-02-01 03:37:29 +00:00
|
|
|
|
|
|
|
if (data + 4 > end)
|
|
|
|
goto beach;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
|
2010-01-10 13:06:55 +00:00
|
|
|
next32 = GST_READ_UINT32_BE (data);
|
2009-02-02 22:12:07 +00:00
|
|
|
if ((next32 & 0x00000300) != 0x00000300)
|
|
|
|
goto beach;
|
|
|
|
stuffing_bytes = (next32 & 0x07);
|
|
|
|
data += 4;
|
2017-02-01 03:37:29 +00:00
|
|
|
if (data + stuffing_bytes > end)
|
|
|
|
goto beach;
|
2009-02-02 22:12:07 +00:00
|
|
|
while (stuffing_bytes--) {
|
|
|
|
if (*data++ != 0xff)
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-15 09:05:48 +00:00
|
|
|
/* MPEG-1 pack header */
|
2009-02-02 22:12:07 +00:00
|
|
|
/* check markers */
|
|
|
|
if ((scr1 & 0xf1000100) != 0x21000100)
|
|
|
|
goto beach;
|
|
|
|
if ((scr2 & 0x01800001) != 0x01800001)
|
|
|
|
goto beach;
|
|
|
|
/* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
|
|
|
|
scr = ((guint64) scr1 & 0x0e000000) << 5;
|
|
|
|
scr |= ((guint64) scr1 & 0x00fffe00) << 6;
|
|
|
|
scr |= ((guint64) scr1 & 0x000000ff) << 7;
|
|
|
|
scr |= ((guint64) scr2 & 0xfe000000) >> 25;
|
|
|
|
data += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == SCAN_SCR) {
|
|
|
|
*rts = scr;
|
|
|
|
ret = TRUE;
|
2015-01-11 20:40:34 +00:00
|
|
|
goto beach;
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 09:05:48 +00:00
|
|
|
/* Possible optional System header here */
|
2017-02-01 03:37:29 +00:00
|
|
|
if (data + 8 > end)
|
|
|
|
goto beach;
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
code = GST_READ_UINT32_BE (data);
|
2016-05-15 09:05:48 +00:00
|
|
|
len = GST_READ_UINT16_BE (data + 4);
|
|
|
|
if (code == ID_PS_SYSTEM_HEADER_START_CODE) {
|
|
|
|
/* Found a system header, skip it */
|
|
|
|
/* Check for sufficient data - system header, plus enough
|
|
|
|
* left over for the PES packet header */
|
|
|
|
if (data + 6 + len + 6 > end)
|
|
|
|
return FALSE;
|
|
|
|
data += len + 6;
|
|
|
|
/* read the 4 bytes for the PES sync code */
|
|
|
|
code = GST_READ_UINT32_BE (data);
|
|
|
|
len = GST_READ_UINT16_BE (data + 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check we have enough data left for reading the PES packet */
|
|
|
|
if (data + 6 + len > end)
|
|
|
|
return FALSE;
|
2013-09-02 22:36:43 +00:00
|
|
|
if (!gst_ps_demux_is_pes_sync (code))
|
2009-02-02 22:12:07 +00:00
|
|
|
goto beach;
|
|
|
|
switch (code) {
|
|
|
|
case ID_PS_PROGRAM_STREAM_MAP:
|
|
|
|
case ID_PRIVATE_STREAM_2:
|
|
|
|
case ID_ECM_STREAM:
|
|
|
|
case ID_EMM_STREAM:
|
|
|
|
case ID_PROGRAM_STREAM_DIRECTORY:
|
|
|
|
case ID_DSMCC_STREAM:
|
|
|
|
case ID_ITU_TREC_H222_TYPE_E_STREAM:
|
|
|
|
case ID_PADDING_STREAM:
|
|
|
|
goto beach;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip sync code and size */
|
|
|
|
data += 6;
|
|
|
|
pts = dts = -1;
|
|
|
|
/* stuffing bits, first two bits are '10' for mpeg2 pes so this code is
|
|
|
|
* not triggered. */
|
|
|
|
while (TRUE) {
|
|
|
|
if (*data != 0xff)
|
|
|
|
break;
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STD buffer size, never for mpeg2 */
|
|
|
|
if ((*data & 0xc0) == 0x40)
|
2009-03-10 20:18:20 +00:00
|
|
|
data += 2;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* PTS but no DTS, never for mpeg2 */
|
|
|
|
if ((*data & 0xf0) == 0x20) {
|
|
|
|
READ_TS (data, pts, beach);
|
|
|
|
}
|
|
|
|
/* PTS and DTS, never for mpeg2 */
|
|
|
|
else if ((*data & 0xf0) == 0x30) {
|
|
|
|
READ_TS (data, pts, beach);
|
|
|
|
READ_TS (data, dts, beach);
|
|
|
|
} else if ((*data & 0xc0) == 0x80) {
|
|
|
|
/* mpeg2 case */
|
|
|
|
guchar flags;
|
|
|
|
/* 2: '10'
|
|
|
|
* 2: PES_scrambling_control
|
|
|
|
* 1: PES_priority
|
|
|
|
* 1: data_alignment_indicator
|
|
|
|
* 1: copyright
|
|
|
|
* 1: original_or_copy
|
|
|
|
*/
|
|
|
|
flags = *data++;
|
|
|
|
if ((flags & 0xc0) != 0x80)
|
|
|
|
goto beach;
|
|
|
|
/* 2: PTS_DTS_flags
|
|
|
|
* 1: ESCR_flag
|
|
|
|
* 1: ES_rate_flag
|
|
|
|
* 1: DSM_trick_mode_flag
|
|
|
|
* 1: additional_copy_info_flag
|
|
|
|
* 1: PES_CRC_flag
|
|
|
|
* 1: PES_extension_flag
|
|
|
|
*/
|
|
|
|
flags = *data++;
|
|
|
|
/* 8: PES_header_data_length */
|
|
|
|
data++;
|
|
|
|
/* only DTS: this is invalid */
|
|
|
|
if ((flags & 0xc0) == 0x40)
|
|
|
|
goto beach;
|
|
|
|
/* check for PTS */
|
|
|
|
if ((flags & 0x80)) {
|
|
|
|
READ_TS (data, pts, beach);
|
|
|
|
}
|
|
|
|
/* check for DTS */
|
|
|
|
if ((flags & 0x40)) {
|
|
|
|
READ_TS (data, dts, beach);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (mode == SCAN_DTS && dts != (guint64) - 1) {
|
2009-02-02 22:12:07 +00:00
|
|
|
*rts = dts;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
if (mode == SCAN_PTS && pts != (guint64) - 1) {
|
2009-02-02 22:12:07 +00:00
|
|
|
*rts = pts;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
beach:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_scan_forward_ts (GstPsDemux * demux, guint64 * pos,
|
2012-01-18 11:04:52 +00:00
|
|
|
SCAN_MODE mode, guint64 * rts, gint limit)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2012-03-16 20:47:39 +00:00
|
|
|
GstBuffer *buffer;
|
2009-02-02 22:12:07 +00:00
|
|
|
guint64 offset = *pos;
|
|
|
|
gboolean found = FALSE;
|
|
|
|
guint64 ts = 0;
|
2009-02-04 15:11:23 +00:00
|
|
|
guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
|
|
|
|
guint cursor, to_read = BLOCK_SZ;
|
2012-01-26 12:13:06 +00:00
|
|
|
guint end_scan;
|
|
|
|
GstMapInfo map;
|
2009-02-02 22:12:07 +00:00
|
|
|
do {
|
2016-05-15 09:04:20 +00:00
|
|
|
/* Check we can get at least scan_sz bytes */
|
2009-02-04 15:11:23 +00:00
|
|
|
if (offset + scan_sz > demux->sink_segment.stop)
|
|
|
|
return FALSE;
|
2016-05-15 09:04:20 +00:00
|
|
|
/* Don't go further than 'limit' bytes */
|
2012-01-18 11:04:52 +00:00
|
|
|
if (limit && offset > *pos + limit)
|
|
|
|
return FALSE;
|
2009-02-04 15:11:23 +00:00
|
|
|
if (offset + to_read > demux->sink_segment.stop)
|
|
|
|
to_read = demux->sink_segment.stop - offset;
|
|
|
|
/* read some data */
|
2012-03-16 20:47:39 +00:00
|
|
|
buffer = NULL;
|
2009-02-04 15:11:23 +00:00
|
|
|
ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
|
2009-02-25 10:34:45 +00:00
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
return FALSE;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_READ);
|
2012-01-10 00:22:38 +00:00
|
|
|
/* may get a short buffer at the end of the file */
|
2012-01-26 12:13:06 +00:00
|
|
|
if (G_UNLIKELY (map.size <= scan_sz)) {
|
|
|
|
gst_buffer_unmap (buffer, &map);
|
|
|
|
gst_buffer_unref (buffer);
|
2012-01-10 00:22:38 +00:00
|
|
|
return FALSE;
|
2012-01-26 12:13:06 +00:00
|
|
|
}
|
2012-01-10 00:22:38 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
end_scan = map.size - scan_sz;
|
2009-02-04 15:11:23 +00:00
|
|
|
/* scan the block */
|
|
|
|
for (cursor = 0; !found && cursor <= end_scan; cursor++) {
|
2016-05-15 09:05:48 +00:00
|
|
|
found = gst_ps_demux_scan_ts (demux, map.data + cursor, mode, &ts,
|
|
|
|
map.data + map.size);
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
/* done with the buffer, unref it */
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_buffer_unmap (buffer, &map);
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
if (found) {
|
|
|
|
*rts = ts;
|
2009-02-04 15:11:23 +00:00
|
|
|
*pos = offset + cursor - 1;
|
|
|
|
} else {
|
|
|
|
offset += cursor;
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
2009-02-04 15:11:23 +00:00
|
|
|
} while (!found && offset < demux->sink_segment.stop);
|
|
|
|
return found;
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_scan_backward_ts (GstPsDemux * demux, guint64 * pos,
|
2012-01-18 11:04:52 +00:00
|
|
|
SCAN_MODE mode, guint64 * rts, gint limit)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2012-03-16 20:47:39 +00:00
|
|
|
GstBuffer *buffer;
|
2009-02-02 22:12:07 +00:00
|
|
|
guint64 offset = *pos;
|
|
|
|
gboolean found = FALSE;
|
|
|
|
guint64 ts = 0;
|
2009-02-04 15:11:23 +00:00
|
|
|
guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
|
|
|
|
guint cursor, to_read = BLOCK_SZ;
|
2012-01-26 12:13:06 +00:00
|
|
|
guint start_scan;
|
2009-02-04 15:20:40 +00:00
|
|
|
guint8 *data;
|
2012-01-26 12:13:06 +00:00
|
|
|
GstMapInfo map;
|
2009-02-02 22:12:07 +00:00
|
|
|
do {
|
2016-05-15 09:04:20 +00:00
|
|
|
/* Check we have at least scan_sz bytes available */
|
2009-02-04 15:11:23 +00:00
|
|
|
if (offset < scan_sz - 1)
|
|
|
|
return FALSE;
|
2016-05-15 09:04:20 +00:00
|
|
|
/* Don't go backward past the start or 'limit' bytes */
|
|
|
|
if (limit && offset + limit < *pos)
|
2012-01-18 11:04:52 +00:00
|
|
|
return FALSE;
|
2009-02-04 15:11:23 +00:00
|
|
|
if (offset > BLOCK_SZ)
|
|
|
|
offset -= BLOCK_SZ;
|
|
|
|
else {
|
|
|
|
to_read = offset + 1;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
/* read some data */
|
2012-03-16 20:47:39 +00:00
|
|
|
buffer = NULL;
|
2009-02-04 15:11:23 +00:00
|
|
|
ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
|
2009-02-25 10:34:45 +00:00
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
return FALSE;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_READ);
|
2012-01-10 00:22:38 +00:00
|
|
|
/* may get a short buffer at the end of the file */
|
2012-01-26 12:13:06 +00:00
|
|
|
if (G_UNLIKELY (map.size <= scan_sz)) {
|
|
|
|
gst_buffer_unmap (buffer, &map);
|
|
|
|
gst_buffer_unref (buffer);
|
2012-01-10 00:22:38 +00:00
|
|
|
return FALSE;
|
2012-01-26 12:13:06 +00:00
|
|
|
}
|
2012-01-10 00:22:38 +00:00
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
start_scan = map.size - scan_sz;
|
|
|
|
data = map.data + start_scan;
|
2009-02-04 15:11:23 +00:00
|
|
|
/* scan the block */
|
|
|
|
for (cursor = (start_scan + 1); !found && cursor > 0; cursor--) {
|
2016-05-15 09:05:48 +00:00
|
|
|
found = gst_ps_demux_scan_ts (demux, data--, mode, &ts,
|
|
|
|
map.data + map.size);
|
2009-02-04 15:11:23 +00:00
|
|
|
}
|
2009-02-02 22:12:07 +00:00
|
|
|
|
|
|
|
/* done with the buffer, unref it */
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_buffer_unmap (buffer, &map);
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
if (found) {
|
|
|
|
*rts = ts;
|
2009-02-04 15:11:23 +00:00
|
|
|
*pos = offset + cursor;
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 15:11:23 +00:00
|
|
|
} while (!found && offset > 0);
|
|
|
|
return found;
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_sink_get_duration (GstPsDemux * demux)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
|
|
|
gboolean res = FALSE;
|
|
|
|
GstPad *peer;
|
|
|
|
GstFormat format = GST_FORMAT_BYTES;
|
|
|
|
gint64 length = 0;
|
|
|
|
guint64 offset;
|
2009-03-13 10:07:18 +00:00
|
|
|
guint i;
|
|
|
|
guint64 scr = 0;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* init the sink segment */
|
|
|
|
gst_segment_init (&demux->sink_segment, format);
|
|
|
|
/* get peer to figure out length */
|
|
|
|
if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL)
|
|
|
|
goto beach;
|
2012-01-26 12:13:06 +00:00
|
|
|
res = gst_pad_query_duration (peer, format, &length);
|
2009-02-02 22:12:07 +00:00
|
|
|
gst_object_unref (peer);
|
|
|
|
if (!res || length <= 0)
|
|
|
|
goto beach;
|
|
|
|
GST_DEBUG_OBJECT (demux, "file length %" G_GINT64_FORMAT, length);
|
|
|
|
/* update the sink segment */
|
|
|
|
demux->sink_segment.stop = length;
|
|
|
|
gst_segment_set_duration (&demux->sink_segment, format, length);
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_segment_set_position (&demux->sink_segment, format, 0);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* Scan for notorious SCR and PTS to calculate the duration */
|
|
|
|
/* scan for first SCR in the stream */
|
|
|
|
offset = demux->sink_segment.start;
|
2017-02-01 03:25:32 +00:00
|
|
|
gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR,
|
|
|
|
&demux->first_scr, DURATION_SCAN_LIMIT);
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"First SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
|
|
|
|
" in packet starting at %" G_GUINT64_FORMAT, demux->first_scr,
|
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
|
2009-02-04 15:11:23 +00:00
|
|
|
demux->first_scr_offset = offset;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* scan for last SCR in the stream */
|
2009-02-04 15:11:23 +00:00
|
|
|
offset = demux->sink_segment.stop;
|
2017-02-01 03:25:32 +00:00
|
|
|
gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR,
|
|
|
|
&demux->last_scr, DURATION_SCAN_LIMIT);
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Last SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
|
|
|
|
" in packet starting at %" G_GUINT64_FORMAT, demux->last_scr,
|
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_scr)), offset);
|
2009-02-04 15:11:23 +00:00
|
|
|
demux->last_scr_offset = offset;
|
2009-02-02 22:12:07 +00:00
|
|
|
/* scan for first PTS in the stream */
|
|
|
|
offset = demux->sink_segment.start;
|
2017-02-01 03:25:32 +00:00
|
|
|
gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_PTS,
|
|
|
|
&demux->first_pts, DURATION_SCAN_LIMIT);
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"First PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
|
|
|
|
" in packet starting at %" G_GUINT64_FORMAT, demux->first_pts,
|
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_pts)), offset);
|
2012-01-18 10:28:26 +00:00
|
|
|
if (demux->first_pts != G_MAXUINT64) {
|
|
|
|
/* scan for last PTS in the stream */
|
|
|
|
offset = demux->sink_segment.stop;
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_PTS,
|
2012-01-18 11:04:52 +00:00
|
|
|
&demux->last_pts, DURATION_SCAN_LIMIT);
|
2012-01-18 10:28:26 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Last PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
|
|
|
|
" in packet starting at %" G_GUINT64_FORMAT, demux->last_pts,
|
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_pts)), offset);
|
|
|
|
}
|
2009-03-13 10:07:18 +00:00
|
|
|
/* Detect wrong SCR values */
|
|
|
|
if (demux->first_scr > demux->last_scr) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Wrong SCR values detected, searching for "
|
|
|
|
"a better first SCR value");
|
|
|
|
offset = demux->first_scr_offset;
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
offset++;
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &scr, 0);
|
2009-03-13 10:07:18 +00:00
|
|
|
if (scr < demux->last_scr) {
|
|
|
|
demux->first_scr = scr;
|
|
|
|
demux->first_scr_offset = offset;
|
|
|
|
/* Start demuxing from the right place */
|
2012-01-26 12:13:06 +00:00
|
|
|
demux->sink_segment.position = offset;
|
2009-03-13 10:07:18 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Replaced First SCR: %" G_GINT64_FORMAT
|
2017-02-01 03:25:32 +00:00
|
|
|
" %" GST_TIME_FORMAT " in packet starting at %"
|
|
|
|
G_GUINT64_FORMAT, demux->first_scr,
|
2009-03-13 10:07:18 +00:00
|
|
|
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Set the base_time and avg rate */
|
|
|
|
demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
|
|
|
|
demux->scr_rate_n = demux->last_scr_offset - demux->first_scr_offset;
|
|
|
|
demux->scr_rate_d = demux->last_scr - demux->first_scr;
|
2009-02-04 21:50:30 +00:00
|
|
|
if (G_LIKELY (demux->first_pts != G_MAXUINT64 &&
|
|
|
|
demux->last_pts != G_MAXUINT64)) {
|
2009-02-02 22:12:07 +00:00
|
|
|
/* update the src segment */
|
2012-01-26 12:13:06 +00:00
|
|
|
demux->src_segment.format = GST_FORMAT_TIME;
|
2009-02-04 15:11:23 +00:00
|
|
|
demux->src_segment.start =
|
2009-02-04 21:50:30 +00:00
|
|
|
MPEGTIME_TO_GSTTIME (demux->first_pts) - demux->base_time;
|
2009-02-02 22:12:07 +00:00
|
|
|
demux->src_segment.stop = -1;
|
|
|
|
gst_segment_set_duration (&demux->src_segment, GST_FORMAT_TIME,
|
2009-02-04 21:50:30 +00:00
|
|
|
MPEGTIME_TO_GSTTIME (demux->last_pts - demux->first_pts));
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
|
2009-02-02 22:12:07 +00:00
|
|
|
demux->src_segment.start);
|
|
|
|
}
|
|
|
|
GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT,
|
|
|
|
&demux->sink_segment);
|
|
|
|
GST_INFO_OBJECT (demux, "src segment configured %" GST_SEGMENT_FORMAT,
|
|
|
|
&demux->src_segment);
|
|
|
|
res = TRUE;
|
|
|
|
beach:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_pull_block (GstPad * pad, GstPsDemux * demux,
|
2009-02-02 22:12:07 +00:00
|
|
|
guint64 offset, guint size)
|
|
|
|
{
|
2014-09-18 08:54:13 +00:00
|
|
|
GstFlowReturn ret;
|
2012-03-16 20:47:39 +00:00
|
|
|
GstBuffer *buffer = NULL;
|
2009-02-02 22:12:07 +00:00
|
|
|
ret = gst_pad_pull_range (pad, offset, size, &buffer);
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK)) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
|
|
|
|
" size %u failed", offset, size);
|
|
|
|
goto beach;
|
|
|
|
} else
|
|
|
|
GST_LOG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
|
|
|
|
" size %u done", offset, size);
|
|
|
|
if (demux->sink_segment.rate < 0) {
|
|
|
|
GST_LOG_OBJECT (demux, "setting discont flag on backward rate");
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
}
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_chain (pad, GST_OBJECT (demux), buffer);
|
2009-02-02 22:12:07 +00:00
|
|
|
beach:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_loop (GstPad * pad)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux;
|
2009-02-02 22:12:07 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2009-07-18 06:43:37 +00:00
|
|
|
guint64 offset = 0;
|
2013-09-02 22:36:43 +00:00
|
|
|
demux = GST_PS_DEMUX (gst_pad_get_parent (pad));
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (demux->flushing)) {
|
2012-02-08 15:37:13 +00:00
|
|
|
ret = GST_FLOW_FLUSHING;
|
2009-08-07 16:17:28 +00:00
|
|
|
goto pause;
|
|
|
|
}
|
|
|
|
|
2009-02-02 22:12:07 +00:00
|
|
|
if (G_UNLIKELY (demux->sink_segment.format == GST_FORMAT_UNDEFINED))
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_sink_get_duration (demux);
|
2012-01-26 12:13:06 +00:00
|
|
|
offset = demux->sink_segment.position;
|
2009-02-02 22:12:07 +00:00
|
|
|
if (demux->sink_segment.rate >= 0) {
|
|
|
|
guint size = BLOCK_SZ;
|
2012-01-26 12:13:06 +00:00
|
|
|
if (G_LIKELY (demux->sink_segment.stop != (guint64) - 1)) {
|
2009-02-02 22:12:07 +00:00
|
|
|
size = MIN (size, demux->sink_segment.stop - offset);
|
|
|
|
}
|
|
|
|
/* pull in data */
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_pull_block (pad, demux, offset, size);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* pause if something went wrong */
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
goto pause;
|
|
|
|
/* update our position */
|
|
|
|
offset += size;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* check EOS condition */
|
2017-07-02 09:55:13 +00:00
|
|
|
if ((demux->sink_segment.position >= demux->sink_segment.stop) ||
|
|
|
|
(demux->src_segment.stop != (guint64) - 1 &&
|
|
|
|
demux->src_segment.position >= demux->src_segment.stop)) {
|
2017-02-01 03:25:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"forward mode using segment reached end of " "segment pos %"
|
|
|
|
GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
|
|
|
|
G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
|
|
|
|
GST_TIME_ARGS (demux->src_segment.position),
|
2009-02-04 21:50:30 +00:00
|
|
|
GST_TIME_ARGS (demux->src_segment.stop),
|
2012-01-26 12:13:06 +00:00
|
|
|
demux->sink_segment.position, demux->sink_segment.stop);
|
|
|
|
ret = GST_FLOW_EOS;
|
2009-02-02 22:12:07 +00:00
|
|
|
goto pause;
|
|
|
|
}
|
|
|
|
} else { /* Reverse playback */
|
2009-07-18 06:43:37 +00:00
|
|
|
guint64 size = MIN (offset, BLOCK_SZ);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* pull in data */
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_pull_block (pad, demux, offset - size, size);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* pause if something went wrong */
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
goto pause;
|
|
|
|
/* update our position */
|
|
|
|
offset -= size;
|
2012-01-26 12:13:06 +00:00
|
|
|
gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
|
2009-02-02 22:12:07 +00:00
|
|
|
/* check EOS condition */
|
2012-01-26 12:13:06 +00:00
|
|
|
if (demux->sink_segment.position <= demux->sink_segment.start ||
|
|
|
|
demux->src_segment.position <= demux->src_segment.start) {
|
2017-02-01 03:25:32 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"reverse mode using segment reached end of " "segment pos %"
|
|
|
|
GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
|
|
|
|
G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
|
|
|
|
GST_TIME_ARGS (demux->src_segment.position),
|
2009-02-04 21:50:30 +00:00
|
|
|
GST_TIME_ARGS (demux->src_segment.start),
|
2012-01-26 12:13:06 +00:00
|
|
|
demux->sink_segment.position, demux->sink_segment.start);
|
|
|
|
ret = GST_FLOW_EOS;
|
2009-02-02 22:12:07 +00:00
|
|
|
goto pause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_unref (demux);
|
|
|
|
return;
|
|
|
|
pause:
|
|
|
|
{
|
|
|
|
const gchar *reason = gst_flow_get_name (ret);
|
|
|
|
GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
|
|
|
|
gst_pad_pause_task (pad);
|
2012-01-26 12:13:06 +00:00
|
|
|
if (ret == GST_FLOW_EOS) {
|
2010-08-27 16:41:06 +00:00
|
|
|
/* perform EOS logic */
|
|
|
|
gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
|
|
|
|
if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
|
|
|
|
gint64 stop;
|
|
|
|
/* for segment playback we need to post when (in stream time)
|
|
|
|
* we stopped, this is either stop (when set) or the duration. */
|
|
|
|
if ((stop = demux->src_segment.stop) == -1)
|
|
|
|
stop = demux->src_segment.duration;
|
|
|
|
if (demux->sink_segment.rate >= 0) {
|
|
|
|
GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_segment_done (GST_OBJECT_CAST (demux),
|
|
|
|
GST_FORMAT_TIME, stop));
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux,
|
2012-07-05 11:18:47 +00:00
|
|
|
gst_event_new_segment_done (GST_FORMAT_TIME, stop));
|
2010-08-27 16:41:06 +00:00
|
|
|
} else { /* Reverse playback */
|
2017-02-01 03:25:32 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"Sending segment done, at beginning of " "segment");
|
2010-08-27 16:41:06 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_segment_done (GST_OBJECT_CAST (demux),
|
|
|
|
GST_FORMAT_TIME, demux->src_segment.start));
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux,
|
2012-07-05 11:18:47 +00:00
|
|
|
gst_event_new_segment_done (GST_FORMAT_TIME,
|
|
|
|
demux->src_segment.start));
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-27 16:41:06 +00:00
|
|
|
/* normal playback, send EOS to all linked pads */
|
|
|
|
gst_element_no_more_pads (GST_ELEMENT (demux));
|
|
|
|
GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
|
2013-09-02 22:36:43 +00:00
|
|
|
if (!gst_ps_demux_send_event (demux, gst_event_new_eos ())
|
2010-08-27 16:41:06 +00:00
|
|
|
&& !have_open_streams (demux)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "EOS and no streams open");
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, FAILED,
|
|
|
|
("Internal data stream error."), ("No valid streams detected"));
|
|
|
|
}
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
2012-01-26 12:13:06 +00:00
|
|
|
} else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
|
2016-08-19 18:12:10 +00:00
|
|
|
GST_ELEMENT_FLOW_ERROR (demux, ret);
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_send_event (demux, gst_event_new_eos ());
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_unref (demux);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 19:08:44 +00:00
|
|
|
/* If we can pull that's preferred */
|
2009-02-02 22:12:07 +00:00
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
2012-01-26 12:13:06 +00:00
|
|
|
gboolean res = FALSE;
|
|
|
|
GstQuery *query = gst_query_new_scheduling ();
|
|
|
|
if (gst_pad_peer_query (sinkpad, query)) {
|
2012-09-11 15:47:16 +00:00
|
|
|
if (gst_query_has_scheduling_mode_with_flags (query,
|
|
|
|
GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE)) {
|
2012-01-26 12:13:06 +00:00
|
|
|
res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
|
|
|
|
} else {
|
|
|
|
res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
|
|
|
|
}
|
2012-07-18 08:19:30 +00:00
|
|
|
} else {
|
|
|
|
res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
2012-01-26 12:13:06 +00:00
|
|
|
|
2012-01-27 15:56:53 +00:00
|
|
|
gst_query_unref (query);
|
2012-01-26 12:13:06 +00:00
|
|
|
return res;
|
2009-02-02 22:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function gets called when we activate ourselves in push mode. */
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_sink_activate_push (GstPad * sinkpad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
gboolean active)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (parent);
|
2009-02-02 22:12:07 +00:00
|
|
|
demux->random_access = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this function gets called when we activate ourselves in pull mode.
|
|
|
|
* We can perform random access to the resource and we start a task
|
|
|
|
* to start reading */
|
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_sink_activate_pull (GstPad * sinkpad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
gboolean active)
|
2009-02-02 22:12:07 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (parent);
|
2009-02-02 22:12:07 +00:00
|
|
|
if (active) {
|
|
|
|
GST_DEBUG ("pull mode activated");
|
|
|
|
demux->random_access = TRUE;
|
2017-02-01 03:25:32 +00:00
|
|
|
return gst_pad_start_task (sinkpad,
|
|
|
|
(GstTaskFunction) gst_ps_demux_loop, sinkpad, NULL);
|
2009-02-02 22:12:07 +00:00
|
|
|
} else {
|
|
|
|
demux->random_access = FALSE;
|
|
|
|
return gst_pad_stop_task (sinkpad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
static gboolean
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_sink_activate_mode (GstPad * pad, GstObject * parent,
|
2012-01-26 12:13:06 +00:00
|
|
|
GstPadMode mode, gboolean active)
|
|
|
|
{
|
|
|
|
if (mode == GST_PAD_MODE_PUSH) {
|
2013-09-02 22:36:43 +00:00
|
|
|
return gst_ps_demux_sink_activate_push (pad, parent, active);
|
2012-01-26 12:13:06 +00:00
|
|
|
} else if (mode == GST_PAD_MODE_PULL) {
|
2013-09-02 22:36:43 +00:00
|
|
|
return gst_ps_demux_sink_activate_pull (pad, parent, active);
|
2012-01-26 12:13:06 +00:00
|
|
|
}
|
2012-08-31 00:51:50 +00:00
|
|
|
return FALSE;
|
2012-01-26 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 13:07:21 +00:00
|
|
|
/* EOS and NOT_LINKED need to be combined. This means that we return:
|
|
|
|
*
|
|
|
|
* GST_FLOW_NOT_LINKED: when all pads NOT_LINKED.
|
|
|
|
* GST_FLOW_EOS: when all pads EOS or NOT_LINKED.
|
|
|
|
*/
|
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_combine_flows (GstPsDemux * demux, GstFlowReturn ret)
|
2012-10-12 13:07:21 +00:00
|
|
|
{
|
|
|
|
GST_LOG_OBJECT (demux, "flow return: %s", gst_flow_get_name (ret));
|
2014-05-26 20:18:01 +00:00
|
|
|
ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
|
|
|
|
if (G_UNLIKELY (demux->need_no_more_pads && ret == GST_FLOW_NOT_LINKED))
|
|
|
|
ret = GST_FLOW_OK;
|
2012-10-12 13:07:21 +00:00
|
|
|
GST_LOG_OBJECT (demux, "combined flow return: %s", gst_flow_get_name (ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-02 12:04:32 +00:00
|
|
|
static GstFlowReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (parent);
|
2008-09-02 12:04:32 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
guint32 avail;
|
|
|
|
gboolean save, discont;
|
|
|
|
discont = GST_BUFFER_IS_DISCONT (buffer);
|
|
|
|
if (discont) {
|
2017-02-01 03:25:32 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"Received buffer with discont flag and" " offset %"
|
|
|
|
G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer));
|
2008-09-02 12:04:32 +00:00
|
|
|
gst_pes_filter_drain (&demux->filter);
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_mark_discont (demux, TRUE, FALSE);
|
2008-09-02 12:04:32 +00:00
|
|
|
/* mark discont on all streams */
|
|
|
|
if (demux->sink_segment.rate >= 0.0) {
|
|
|
|
demux->current_scr = G_MAXUINT64;
|
|
|
|
demux->bytes_since_scr = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-01 03:25:32 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"Received buffer with offset %" G_GUINT64_FORMAT,
|
2008-09-02 12:04:32 +00:00
|
|
|
GST_BUFFER_OFFSET (buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We keep the offset to interpolate SCR */
|
|
|
|
demux->adapter_offset = GST_BUFFER_OFFSET (buffer);
|
|
|
|
gst_adapter_push (demux->adapter, buffer);
|
2012-01-26 12:13:06 +00:00
|
|
|
demux->bytes_since_scr += gst_buffer_get_size (buffer);
|
2008-09-02 12:04:32 +00:00
|
|
|
avail = gst_adapter_available (demux->rev_adapter);
|
|
|
|
if (avail > 0) {
|
|
|
|
GST_LOG_OBJECT (demux, "appending %u saved bytes", avail);
|
|
|
|
/* if we have a previous reverse chunk, append this now */
|
|
|
|
/* FIXME this code assumes we receive discont buffers all thei
|
|
|
|
* time */
|
|
|
|
gst_adapter_push (demux->adapter,
|
|
|
|
gst_adapter_take_buffer (demux->rev_adapter, avail));
|
|
|
|
}
|
|
|
|
|
|
|
|
avail = gst_adapter_available (demux->adapter);
|
|
|
|
GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state);
|
|
|
|
switch (demux->filter.state) {
|
|
|
|
case STATE_DATA_SKIP:
|
|
|
|
case STATE_DATA_PUSH:
|
|
|
|
ret = gst_pes_filter_process (&demux->filter);
|
|
|
|
break;
|
|
|
|
case STATE_HEADER_PARSE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ret) {
|
2010-04-16 17:42:00 +00:00
|
|
|
case GST_FLOW_NEED_MORE_DATA:
|
|
|
|
/* Go and get more data */
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
case GST_FLOW_LOST_SYNC:
|
|
|
|
/* for FLOW_OK or lost-sync, carry onto resync */
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
break;
|
2008-09-02 12:04:32 +00:00
|
|
|
case GST_FLOW_OK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Any other return value should be sent upstream immediately */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* align adapter data to sync boundary, we keep the data up to the next sync
|
|
|
|
* point. */
|
|
|
|
save = TRUE;
|
2013-09-02 22:36:43 +00:00
|
|
|
while (gst_ps_demux_resync (demux, save)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
gboolean ps_sync = TRUE;
|
2009-08-07 16:17:28 +00:00
|
|
|
if (G_UNLIKELY (demux->flushing)) {
|
2012-02-08 15:37:13 +00:00
|
|
|
ret = GST_FLOW_FLUSHING;
|
2009-08-07 16:17:28 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2008-09-02 12:04:32 +00:00
|
|
|
|
|
|
|
/* now switch on last synced byte */
|
|
|
|
switch (demux->last_sync_code) {
|
|
|
|
case ID_PS_PACK_START_CODE:
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_parse_pack_start (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ID_PS_SYSTEM_HEADER_START_CODE:
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_parse_sys_head (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case ID_PS_END_CODE:
|
2014-04-23 08:42:45 +00:00
|
|
|
/* Skip final 4 bytes */
|
|
|
|
gst_adapter_flush (demux->adapter, 4);
|
|
|
|
ADAPTER_OFFSET_FLUSH (4);
|
2008-09-02 12:04:32 +00:00
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
case ID_PS_PROGRAM_STREAM_MAP:
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_parse_psm (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-09-02 22:36:43 +00:00
|
|
|
if (gst_ps_demux_is_pes_sync (demux->last_sync_code)) {
|
2008-09-02 12:04:32 +00:00
|
|
|
ret = gst_pes_filter_process (&demux->filter);
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found"
|
|
|
|
", continuing", demux->last_sync_code);
|
|
|
|
ps_sync = FALSE;
|
|
|
|
ret = GST_FLOW_LOST_SYNC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* if we found a ps sync, we stop saving the data, any non-ps sync gets
|
|
|
|
* saved up to the next ps sync. */
|
|
|
|
if (ps_sync)
|
|
|
|
save = FALSE;
|
|
|
|
switch (ret) {
|
2010-04-16 17:42:00 +00:00
|
|
|
case GST_FLOW_NEED_MORE_DATA:
|
|
|
|
GST_DEBUG_OBJECT (demux, "need more data");
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
case GST_FLOW_LOST_SYNC:
|
|
|
|
if (!save || demux->sink_segment.rate >= 0.0) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "flushing 3 bytes");
|
|
|
|
gst_adapter_flush (demux->adapter, 3);
|
|
|
|
ADAPTER_OFFSET_FLUSH (3);
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux, "saving 3 bytes");
|
|
|
|
gst_adapter_push (demux->rev_adapter,
|
|
|
|
gst_adapter_take_buffer (demux->adapter, 3));
|
2010-04-15 19:26:45 +00:00
|
|
|
}
|
2010-04-16 17:42:00 +00:00
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
break;
|
|
|
|
default:
|
2013-09-02 22:36:43 +00:00
|
|
|
ret = gst_ps_demux_combine_flows (demux, ret);
|
2012-10-12 13:07:21 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto done;
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_change_state (GstElement * element, GstStateChange transition)
|
2008-09-02 12:04:32 +00:00
|
|
|
{
|
2013-09-02 22:36:43 +00:00
|
|
|
GstPsDemux *demux = GST_PS_DEMUX (element);
|
2008-09-02 12:04:32 +00:00
|
|
|
GstStateChangeReturn result;
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
gst_pes_filter_init (&demux->filter, demux->adapter,
|
|
|
|
&demux->adapter_offset);
|
|
|
|
gst_pes_filter_set_callbacks (&demux->filter,
|
2013-09-02 22:36:43 +00:00
|
|
|
(GstPESFilterData) gst_ps_demux_data_cb,
|
|
|
|
(GstPESFilterResync) gst_ps_demux_resync_cb, demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
demux->filter.gather_pes = TRUE;
|
|
|
|
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:
|
2013-09-02 22:36:43 +00:00
|
|
|
gst_ps_demux_reset (demux);
|
2008-09-02 12:04:32 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
gst_pes_filter_uninit (&demux->filter);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-26 12:13:06 +00:00
|
|
|
static void
|
|
|
|
gst_segment_set_position (GstSegment * segment, GstFormat format,
|
|
|
|
guint64 position)
|
|
|
|
{
|
|
|
|
if (segment->format == GST_FORMAT_UNDEFINED) {
|
|
|
|
segment->format = format;
|
|
|
|
}
|
|
|
|
segment->position = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_segment_set_duration (GstSegment * segment, GstFormat format,
|
|
|
|
guint64 duration)
|
|
|
|
{
|
|
|
|
if (segment->format == GST_FORMAT_UNDEFINED) {
|
|
|
|
segment->format = format;
|
|
|
|
}
|
|
|
|
segment->duration = duration;
|
|
|
|
}
|