2011-02-16 16:57:42 +00:00
|
|
|
/*
|
|
|
|
* tsdemux.c
|
|
|
|
* Copyright (C) 2009 Zaheer Abbas Merali
|
|
|
|
* 2010 Edward Hervey
|
2011-09-08 22:13:19 +00:00
|
|
|
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
|
|
|
|
* Author: Youness Alaoui <youness.alaoui@collabora.co.uk>, Collabora Ltd.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
|
|
|
|
* Author: Edward Hervey <bilboed@bilboed.com>, Collabora Ltd.
|
2011-02-16 16:57:42 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Zaheer Abbas Merali <zaheerabbas at merali dot org>
|
|
|
|
* Edward Hervey <edward.hervey@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* 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.
|
2011-02-16 16:57:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
#include <glib.h>
|
2012-04-10 10:36:15 +00:00
|
|
|
#include <gst/tag/tag.h>
|
2013-06-14 05:28:29 +00:00
|
|
|
#include <gst/pbutils/pbutils.h>
|
2015-10-24 11:27:43 +00:00
|
|
|
#include <gst/base/base.h>
|
2015-11-02 16:02:55 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2011-02-22 11:33:56 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
#include "mpegtsbase.h"
|
|
|
|
#include "tsdemux.h"
|
|
|
|
#include "gstmpegdesc.h"
|
|
|
|
#include "gstmpegdefs.h"
|
|
|
|
#include "mpegtspacketizer.h"
|
2011-07-18 07:12:16 +00:00
|
|
|
#include "pesparse.h"
|
2014-05-30 14:52:09 +00:00
|
|
|
#include <gst/codecparsers/gsth264parser.h>
|
|
|
|
#include <gst/codecparsers/gstmpegvideoparser.h>
|
2017-07-19 14:14:21 +00:00
|
|
|
#include <gst/video/video-color.h>
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-02-15 14:06:57 +00:00
|
|
|
/*
|
2011-11-16 11:46:04 +00:00
|
|
|
* tsdemux
|
|
|
|
*
|
|
|
|
* See TODO for explanations on improvements needed
|
|
|
|
*/
|
|
|
|
|
2013-04-11 11:08:57 +00:00
|
|
|
#define CONTINUITY_UNSET 255
|
|
|
|
#define MAX_CONTINUITY 15
|
|
|
|
|
2023-03-30 09:06:33 +00:00
|
|
|
/* Length of metadata_AU_cell header, see ISO/IEC 13818-1:2018 Section 2.12.4 */
|
|
|
|
#define PES_PACKET_METADATA_AU_HEADER_LEN 5
|
|
|
|
|
2012-02-29 09:33:00 +00:00
|
|
|
/* Seeking/Scanning related variables */
|
|
|
|
|
2011-03-23 17:50:25 +00:00
|
|
|
/* seek to SEEK_TIMESTAMP_OFFSET before the desired offset and search then
|
|
|
|
* either accurately or for the next timestamp
|
|
|
|
*/
|
2014-05-30 14:52:09 +00:00
|
|
|
#define SEEK_TIMESTAMP_OFFSET (2500 * GST_MSECOND)
|
2011-03-23 17:50:25 +00:00
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
#define GST_FLOW_REWINDING GST_FLOW_CUSTOM_ERROR
|
2012-02-29 09:33:00 +00:00
|
|
|
|
2019-08-23 21:55:00 +00:00
|
|
|
/* latency in msecs */
|
|
|
|
#define DEFAULT_LATENCY (700)
|
2015-04-17 19:55:26 +00:00
|
|
|
|
2019-06-28 05:04:29 +00:00
|
|
|
/* Limit PES packet collection to a maximum of 32MB
|
|
|
|
* which is more than large enough to support an H264 frame at
|
|
|
|
* maximum profile/level/bitrate at 30fps or above.
|
|
|
|
* PES bigger than this limit will be output in buffers of
|
|
|
|
* up to this size */
|
|
|
|
#define MAX_PES_PAYLOAD (32 * 1024 * 1024)
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (ts_demux_debug);
|
|
|
|
#define GST_CAT_DEFAULT ts_demux_debug
|
|
|
|
|
2012-01-10 10:37:13 +00:00
|
|
|
#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PENDING_PACKET_EMPTY = 0, /* No pending packet/buffer
|
|
|
|
* Push incoming buffers to the array */
|
|
|
|
PENDING_PACKET_HEADER, /* PES header needs to be parsed
|
|
|
|
* Push incoming buffers to the array */
|
|
|
|
PENDING_PACKET_BUFFER, /* Currently filling up output buffer
|
|
|
|
* Push incoming buffers to the bufferlist */
|
|
|
|
PENDING_PACKET_DISCONT /* Discontinuity in incoming packets
|
|
|
|
* Drop all incoming buffers */
|
|
|
|
} PendingPacketState;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Pending buffer */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* The fully reconstructed buffer */
|
|
|
|
GstBuffer *buffer;
|
|
|
|
|
|
|
|
/* Raw PTS/DTS (in 90kHz units) */
|
|
|
|
guint64 pts, dts;
|
|
|
|
} PendingBuffer;
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
typedef struct _TSDemuxStream TSDemuxStream;
|
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
typedef struct _TSDemuxH264ParsingInfos TSDemuxH264ParsingInfos;
|
2017-07-19 14:14:21 +00:00
|
|
|
typedef struct _TSDemuxJP2KParsingInfos TSDemuxJP2KParsingInfos;
|
2020-03-20 11:27:05 +00:00
|
|
|
typedef struct _TSDemuxADTSParsingInfos TSDemuxADTSParsingInfos;
|
2014-05-30 14:52:09 +00:00
|
|
|
|
2014-06-22 11:32:35 +00:00
|
|
|
/* Returns TRUE if a keyframe was found */
|
2014-05-30 14:52:09 +00:00
|
|
|
typedef gboolean (*GstTsDemuxKeyFrameScanFunction) (TSDemuxStream * stream,
|
|
|
|
guint8 * data, const gsize data_size, const gsize max_frame_offset);
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
guint8 *data;
|
|
|
|
gsize size;
|
|
|
|
} SimpleBuffer;
|
|
|
|
|
|
|
|
struct _TSDemuxH264ParsingInfos
|
|
|
|
{
|
|
|
|
/* H264 parsing data */
|
|
|
|
GstH264NalParser *parser;
|
|
|
|
GstByteWriter *sps;
|
|
|
|
GstByteWriter *pps;
|
|
|
|
GstByteWriter *sei;
|
|
|
|
SimpleBuffer framedata;
|
|
|
|
};
|
|
|
|
|
2017-07-19 14:14:21 +00:00
|
|
|
struct _TSDemuxJP2KParsingInfos
|
|
|
|
{
|
|
|
|
/* J2K parsing data */
|
|
|
|
gboolean interlace;
|
|
|
|
};
|
2020-03-20 11:27:05 +00:00
|
|
|
|
|
|
|
struct _TSDemuxADTSParsingInfos
|
|
|
|
{
|
|
|
|
guint mpegversion;
|
|
|
|
};
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
struct _TSDemuxStream
|
|
|
|
{
|
|
|
|
MpegTSBaseStream stream;
|
|
|
|
|
|
|
|
GstPad *pad;
|
2013-07-29 06:10:07 +00:00
|
|
|
|
2012-02-29 14:14:15 +00:00
|
|
|
/* Whether the pad was added or not */
|
|
|
|
gboolean active;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-08-02 10:29:37 +00:00
|
|
|
/* Whether this is a sparse stream (subtitles or metadata) */
|
|
|
|
gboolean sparse;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* TRUE if we are waiting for a valid timestamp */
|
|
|
|
gboolean pending_ts;
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
/* Output data */
|
|
|
|
PendingPacketState state;
|
2012-05-21 15:53:37 +00:00
|
|
|
|
2022-03-04 13:17:47 +00:00
|
|
|
/* PES header being reconstructed (optional, allocated) */
|
|
|
|
guint8 *pending_header_data;
|
|
|
|
guint pending_header_size;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Data being reconstructed (allocated) */
|
2012-05-21 15:53:37 +00:00
|
|
|
guint8 *data;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Size of data being reconstructed (if known, else 0) */
|
2012-03-05 08:38:57 +00:00
|
|
|
guint expected_size;
|
2012-05-21 15:53:37 +00:00
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Amount of bytes in current ->data */
|
2012-03-05 08:38:57 +00:00
|
|
|
guint current_size;
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Size of ->data */
|
2012-05-21 15:53:37 +00:00
|
|
|
guint allocated_size;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Current PTS/DTS for this stream (in running time) */
|
2011-02-16 16:57:42 +00:00
|
|
|
GstClockTime pts;
|
2012-01-10 10:37:13 +00:00
|
|
|
GstClockTime dts;
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
/* Reference PTS used to detect gaps */
|
|
|
|
GstClockTime gap_ref_pts;
|
|
|
|
/* Number of outputted buffers */
|
|
|
|
guint32 nb_out_buffers;
|
|
|
|
/* Reference number of buffers for gaps */
|
|
|
|
guint32 gap_ref_buffers;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* Current PTS/DTS for this stream (in 90kHz unit) */
|
|
|
|
guint64 raw_pts, raw_dts;
|
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* Whether this stream needs to send a newsegment */
|
|
|
|
gboolean need_newsegment;
|
2012-04-10 10:36:15 +00:00
|
|
|
|
2014-03-28 17:11:21 +00:00
|
|
|
/* Whether the next output buffer should be DISCONT */
|
|
|
|
gboolean discont;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* The value to use when calculating the newsegment */
|
2015-06-10 21:05:52 +00:00
|
|
|
GstClockTime first_pts;
|
2013-07-29 06:10:07 +00:00
|
|
|
|
2012-04-10 10:36:15 +00:00
|
|
|
GstTagList *taglist;
|
2013-04-11 11:08:57 +00:00
|
|
|
|
|
|
|
gint continuity_counter;
|
2013-07-29 06:10:07 +00:00
|
|
|
|
|
|
|
/* List of pending buffers */
|
|
|
|
GList *pending;
|
2014-03-03 12:07:47 +00:00
|
|
|
|
|
|
|
/* if != 0, output only PES from that substream */
|
|
|
|
guint8 target_pes_substream;
|
2014-05-30 14:52:09 +00:00
|
|
|
gboolean needs_keyframe;
|
|
|
|
|
|
|
|
GstClockTime seeked_pts, seeked_dts;
|
|
|
|
|
|
|
|
GstTsDemuxKeyFrameScanFunction scan_function;
|
|
|
|
TSDemuxH264ParsingInfos h264infos;
|
2017-07-19 14:14:21 +00:00
|
|
|
TSDemuxJP2KParsingInfos jp2kInfos;
|
2020-03-20 11:27:05 +00:00
|
|
|
TSDemuxADTSParsingInfos atdsInfos;
|
2011-02-16 16:57:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define VIDEO_CAPS \
|
|
|
|
GST_STATIC_CAPS (\
|
|
|
|
"video/mpeg, " \
|
|
|
|
"mpegversion = (int) { 1, 2, 4 }, " \
|
|
|
|
"systemstream = (boolean) FALSE; " \
|
2018-10-10 18:36:57 +00:00
|
|
|
"video/x-h264,stream-format=(string)byte-stream;" \
|
|
|
|
"video/x-h265,stream-format=(string)byte-stream;" \
|
2011-02-16 16:57:42 +00:00
|
|
|
"video/x-dirac;" \
|
2015-03-24 14:17:16 +00:00
|
|
|
"video/x-cavs;" \
|
2011-02-16 16:57:42 +00:00
|
|
|
"video/x-wmv," \
|
|
|
|
"wmvversion = (int) 3, " \
|
2017-07-19 14:14:21 +00:00
|
|
|
"format = (string) WVC1;" \
|
|
|
|
"image/x-jpc;" \
|
2024-07-15 14:10:10 +00:00
|
|
|
"image/x-jxsc;" \
|
2017-07-19 14:14:21 +00:00
|
|
|
)
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
#define AUDIO_CAPS \
|
|
|
|
GST_STATIC_CAPS ( \
|
|
|
|
"audio/mpeg, " \
|
2012-02-02 11:37:55 +00:00
|
|
|
"mpegversion = (int) 1;" \
|
2012-02-21 14:12:53 +00:00
|
|
|
"audio/mpeg, " \
|
2020-03-20 11:27:05 +00:00
|
|
|
"mpegversion = (int) { 2, 4 }, " \
|
2012-02-21 14:12:53 +00:00
|
|
|
"stream-format = (string) adts; " \
|
2012-02-02 11:37:55 +00:00
|
|
|
"audio/mpeg, " \
|
|
|
|
"mpegversion = (int) 4, " \
|
2012-06-28 15:40:16 +00:00
|
|
|
"stream-format = (string) loas; " \
|
2011-02-16 16:57:42 +00:00
|
|
|
"audio/x-lpcm, " \
|
|
|
|
"width = (int) { 16, 20, 24 }, " \
|
|
|
|
"rate = (int) { 48000, 96000 }, " \
|
|
|
|
"channels = (int) [ 1, 8 ], " \
|
|
|
|
"dynamic_range = (int) [ 0, 255 ], " \
|
|
|
|
"emphasis = (boolean) { FALSE, TRUE }, " \
|
|
|
|
"mute = (boolean) { FALSE, TRUE }; " \
|
|
|
|
"audio/x-ac3; audio/x-eac3;" \
|
2020-03-09 09:00:01 +00:00
|
|
|
"audio/x-ac4;" \
|
2011-02-16 16:57:42 +00:00
|
|
|
"audio/x-dts;" \
|
2015-10-24 11:27:43 +00:00
|
|
|
"audio/x-opus;" \
|
2011-02-16 16:57:42 +00:00
|
|
|
"audio/x-private-ts-lpcm" \
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Can also use the subpicture pads for text subtitles? */
|
|
|
|
#define SUBPICTURE_CAPS \
|
2015-10-20 07:49:16 +00:00
|
|
|
GST_STATIC_CAPS ("subpicture/x-pgs; subpicture/x-dvd; subpicture/x-dvb")
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate video_template =
|
2015-09-22 16:51:57 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("video_%01x_%05x", GST_PAD_SRC,
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
VIDEO_CAPS);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate audio_template =
|
2015-09-22 16:51:57 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("audio_%01x_%05x",
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
AUDIO_CAPS);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate subpicture_template =
|
2015-09-22 16:51:57 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("subpicture_%01x_%05x",
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
SUBPICTURE_CAPS);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate private_template =
|
2015-09-22 16:51:57 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("private_%01x_%05x",
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2015-04-27 09:55:13 +00:00
|
|
|
PROP_0,
|
2011-02-16 16:57:42 +00:00
|
|
|
PROP_PROGRAM_NUMBER,
|
|
|
|
PROP_EMIT_STATS,
|
2019-08-23 21:55:00 +00:00
|
|
|
PROP_LATENCY,
|
2021-04-05 22:36:43 +00:00
|
|
|
PROP_SEND_SCTE35_EVENTS,
|
2011-02-16 16:57:42 +00:00
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Pad functions */
|
|
|
|
|
|
|
|
|
|
|
|
/* mpegtsbase methods */
|
|
|
|
static void
|
2016-10-11 09:11:16 +00:00
|
|
|
gst_ts_demux_update_program (MpegTSBase * base, MpegTSBaseProgram * program);
|
|
|
|
static void
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_ts_demux_program_started (MpegTSBase * base, MpegTSBaseProgram * program);
|
2012-10-12 13:26:20 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_program_stopped (MpegTSBase * base, MpegTSBaseProgram * program);
|
2015-09-10 12:55:05 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ts_demux_can_remove_program (MpegTSBase * base,
|
|
|
|
MpegTSBaseProgram * program);
|
2011-02-22 11:33:56 +00:00
|
|
|
static void gst_ts_demux_reset (MpegTSBase * base);
|
2011-02-16 16:57:42 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_ts_demux_push (MpegTSBase * base, MpegTSPacketizerPacket * packet,
|
2014-06-25 12:50:05 +00:00
|
|
|
GstMpegtsSection * section);
|
2013-05-24 08:59:59 +00:00
|
|
|
static void gst_ts_demux_flush (MpegTSBase * base, gboolean hard);
|
2014-03-28 16:32:56 +00:00
|
|
|
static GstFlowReturn gst_ts_demux_drain (MpegTSBase * base);
|
2015-07-29 13:52:05 +00:00
|
|
|
static gboolean
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_ts_demux_stream_added (MpegTSBase * base, MpegTSBaseStream * stream,
|
|
|
|
MpegTSBaseProgram * program);
|
|
|
|
static void
|
|
|
|
gst_ts_demux_stream_removed (MpegTSBase * base, MpegTSBaseStream * stream);
|
2012-02-29 09:33:00 +00:00
|
|
|
static GstFlowReturn gst_ts_demux_do_seek (MpegTSBase * base, GstEvent * event);
|
2011-02-16 16:57:42 +00:00
|
|
|
static void gst_ts_demux_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_ts_demux_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2015-02-03 13:59:16 +00:00
|
|
|
static void gst_ts_demux_flush_streams (GstTSDemux * tsdemux, gboolean hard);
|
2011-08-01 08:54:16 +00:00
|
|
|
static GstFlowReturn
|
2016-07-13 09:02:23 +00:00
|
|
|
gst_ts_demux_push_pending_data (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
MpegTSBaseProgram * program);
|
2014-05-23 21:26:57 +00:00
|
|
|
static void gst_ts_demux_stream_flush (TSDemuxStream * stream,
|
2015-02-03 13:59:16 +00:00
|
|
|
GstTSDemux * demux, gboolean hard);
|
2011-08-01 08:54:16 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static gboolean push_event (MpegTSBase * base, GstEvent * event);
|
2018-05-28 15:06:09 +00:00
|
|
|
static gboolean sink_query (MpegTSBase * base, GstQuery * query);
|
2014-10-20 10:30:50 +00:00
|
|
|
static void gst_ts_demux_check_and_sync_streams (GstTSDemux * demux,
|
|
|
|
GstClockTime time);
|
2021-04-05 22:36:43 +00:00
|
|
|
static void handle_psi (MpegTSBase * base, GstMpegtsSection * section);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
#define gst_ts_demux_parent_class parent_class
|
2024-08-09 07:41:57 +00:00
|
|
|
G_DEFINE_TYPE (GstTSDemux, gst_ts_demux, GST_TYPE_MPEGTS_BASE);
|
2021-02-25 14:22:15 +00:00
|
|
|
#define _do_element_init \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (ts_demux_debug, "tsdemux", 0, \
|
|
|
|
"MPEG transport stream demuxer");\
|
|
|
|
init_pes_parser ();
|
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (tsdemux, "tsdemux",
|
|
|
|
GST_RANK_PRIMARY, GST_TYPE_TS_DEMUX, _do_element_init);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-05-23 21:26:57 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX_CAST (object);
|
|
|
|
|
|
|
|
gst_flow_combiner_free (demux->flowcombiner);
|
|
|
|
|
|
|
|
GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
|
|
|
|
}
|
|
|
|
|
2021-04-05 07:28:51 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX_CAST (object);
|
|
|
|
|
2021-04-05 07:29:37 +00:00
|
|
|
gst_event_replace (&demux->segment_event, NULL);
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_clear (&demux->lock);
|
|
|
|
|
|
|
|
GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_class_init (GstTSDemuxClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-04-19 12:20:52 +00:00
|
|
|
GstElementClass *element_class;
|
2011-02-16 16:57:42 +00:00
|
|
|
MpegTSBaseClass *ts_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
gobject_class->set_property = gst_ts_demux_set_property;
|
|
|
|
gobject_class->get_property = gst_ts_demux_get_property;
|
2014-05-23 21:26:57 +00:00
|
|
|
gobject_class->dispose = gst_ts_demux_dispose;
|
2021-04-05 07:28:51 +00:00
|
|
|
gobject_class->finalize = gst_ts_demux_finalize;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PROGRAM_NUMBER,
|
|
|
|
g_param_spec_int ("program-number", "Program number",
|
|
|
|
"Program Number to demux for (-1 to ignore)", -1, G_MAXINT,
|
|
|
|
-1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_EMIT_STATS,
|
|
|
|
g_param_spec_boolean ("emit-stats", "Emit statistics",
|
|
|
|
"Emit messages for every pcr/opcr/pts/dts", FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2021-04-05 22:36:43 +00:00
|
|
|
/**
|
2021-09-24 22:09:00 +00:00
|
|
|
* tsdemux:send-scte35-events:
|
2021-04-05 22:36:43 +00:00
|
|
|
*
|
|
|
|
* Whether SCTE 35 sections should be forwarded as events.
|
|
|
|
*
|
|
|
|
* When forwarding those, potential splice times are converted
|
|
|
|
* to running time, and can be used by a downstream muxer to reinject
|
|
|
|
* the sections.
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_SEND_SCTE35_EVENTS,
|
|
|
|
g_param_spec_boolean ("send-scte35-events", "Send SCTE 35 events",
|
|
|
|
"Whether SCTE 35 sections should be forwarded as events", FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2019-08-23 21:55:00 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_LATENCY,
|
|
|
|
g_param_spec_int ("latency", "Latency",
|
|
|
|
"Latency to add for smooth demuxing (in ms)", -1,
|
|
|
|
G_MAXINT, DEFAULT_LATENCY,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&video_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&audio_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&subpicture_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&private_template));
|
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2012-04-19 12:20:52 +00:00
|
|
|
"MPEG transport stream demuxer",
|
|
|
|
"Codec/Demuxer",
|
|
|
|
"Demuxes MPEG2 transport streams",
|
2023-05-19 13:50:11 +00:00
|
|
|
"Zaheer Abbas Merali <zaheerabbas at merali dot org>, "
|
2012-04-19 12:20:52 +00:00
|
|
|
"Edward Hervey <edward.hervey@collabora.co.uk>");
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
ts_class = GST_MPEGTS_BASE_CLASS (klass);
|
2011-02-22 11:33:56 +00:00
|
|
|
ts_class->reset = GST_DEBUG_FUNCPTR (gst_ts_demux_reset);
|
2011-02-16 16:57:42 +00:00
|
|
|
ts_class->push = GST_DEBUG_FUNCPTR (gst_ts_demux_push);
|
|
|
|
ts_class->push_event = GST_DEBUG_FUNCPTR (push_event);
|
2021-04-05 22:36:43 +00:00
|
|
|
ts_class->handle_psi = GST_DEBUG_FUNCPTR (handle_psi);
|
2018-05-28 15:06:09 +00:00
|
|
|
ts_class->sink_query = GST_DEBUG_FUNCPTR (sink_query);
|
2011-02-16 16:57:42 +00:00
|
|
|
ts_class->program_started = GST_DEBUG_FUNCPTR (gst_ts_demux_program_started);
|
2012-10-12 13:26:20 +00:00
|
|
|
ts_class->program_stopped = GST_DEBUG_FUNCPTR (gst_ts_demux_program_stopped);
|
2016-10-11 09:11:16 +00:00
|
|
|
ts_class->update_program = GST_DEBUG_FUNCPTR (gst_ts_demux_update_program);
|
2015-09-10 12:55:05 +00:00
|
|
|
ts_class->can_remove_program = gst_ts_demux_can_remove_program;
|
2011-02-16 16:57:42 +00:00
|
|
|
ts_class->stream_added = gst_ts_demux_stream_added;
|
|
|
|
ts_class->stream_removed = gst_ts_demux_stream_removed;
|
2011-02-22 11:33:56 +00:00
|
|
|
ts_class->seek = GST_DEBUG_FUNCPTR (gst_ts_demux_do_seek);
|
2011-08-26 22:44:08 +00:00
|
|
|
ts_class->flush = GST_DEBUG_FUNCPTR (gst_ts_demux_flush);
|
2014-03-28 16:32:56 +00:00
|
|
|
ts_class->drain = GST_DEBUG_FUNCPTR (gst_ts_demux_drain);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_reset (MpegTSBase * base)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = (GstTSDemux *) base;
|
2011-07-16 11:47:45 +00:00
|
|
|
|
2013-07-24 13:50:14 +00:00
|
|
|
demux->rate = 1.0;
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
2021-04-05 07:29:37 +00:00
|
|
|
gst_event_replace (&demux->segment_event, NULL);
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2014-02-25 01:43:56 +00:00
|
|
|
if (demux->global_tags) {
|
|
|
|
gst_tag_list_unref (demux->global_tags);
|
|
|
|
demux->global_tags = NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-11 16:00:14 +00:00
|
|
|
if (demux->previous_program) {
|
|
|
|
mpegts_base_deactivate_and_free_program (base, demux->previous_program);
|
|
|
|
demux->previous_program = NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-23 08:24:22 +00:00
|
|
|
demux->have_group_id = FALSE;
|
|
|
|
demux->group_id = G_MAXUINT;
|
2014-05-30 14:52:09 +00:00
|
|
|
|
|
|
|
demux->last_seek_offset = -1;
|
2015-09-22 16:51:57 +00:00
|
|
|
demux->program_generation = 0;
|
2021-04-22 23:15:08 +00:00
|
|
|
|
|
|
|
demux->mpeg_pts_offset = 0;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2012-02-24 16:52:33 +00:00
|
|
|
static void
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_ts_demux_init (GstTSDemux * demux)
|
2012-02-24 16:52:33 +00:00
|
|
|
{
|
2013-07-06 12:50:52 +00:00
|
|
|
MpegTSBase *base = (MpegTSBase *) demux;
|
2012-02-24 16:52:33 +00:00
|
|
|
|
2013-07-06 12:50:52 +00:00
|
|
|
base->stream_size = sizeof (TSDemuxStream);
|
|
|
|
base->parse_private_sections = TRUE;
|
2013-07-07 09:42:50 +00:00
|
|
|
/* We are not interested in sections (all handled by mpegtsbase) */
|
|
|
|
base->push_section = FALSE;
|
|
|
|
|
2014-05-23 21:26:57 +00:00
|
|
|
demux->flowcombiner = gst_flow_combiner_new ();
|
2013-03-09 23:07:40 +00:00
|
|
|
demux->requested_program_number = -1;
|
2013-03-06 11:15:47 +00:00
|
|
|
demux->program_number = -1;
|
2019-08-23 21:55:00 +00:00
|
|
|
demux->latency = DEFAULT_LATENCY;
|
2013-07-06 12:50:52 +00:00
|
|
|
gst_ts_demux_reset (base);
|
2021-04-05 07:28:51 +00:00
|
|
|
|
|
|
|
g_mutex_init (&demux->lock);
|
2012-02-24 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ts_demux_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PROGRAM_NUMBER:
|
|
|
|
/* FIXME: do something if program is switched as opposed to set at
|
|
|
|
* beginning */
|
2013-03-09 23:07:40 +00:00
|
|
|
demux->requested_program_number = g_value_get_int (value);
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
case PROP_EMIT_STATS:
|
|
|
|
demux->emit_statistics = g_value_get_boolean (value);
|
|
|
|
break;
|
2021-04-05 22:36:43 +00:00
|
|
|
case PROP_SEND_SCTE35_EVENTS:
|
|
|
|
demux->send_scte35_events = g_value_get_boolean (value);
|
|
|
|
break;
|
2019-08-23 21:55:00 +00:00
|
|
|
case PROP_LATENCY:
|
|
|
|
demux->latency = g_value_get_int (value);
|
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ts_demux_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PROGRAM_NUMBER:
|
2013-03-09 23:07:40 +00:00
|
|
|
g_value_set_int (value, demux->requested_program_number);
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
case PROP_EMIT_STATS:
|
|
|
|
g_value_set_boolean (value, demux->emit_statistics);
|
|
|
|
break;
|
2021-04-05 22:36:43 +00:00
|
|
|
case PROP_SEND_SCTE35_EVENTS:
|
|
|
|
g_value_set_boolean (value, demux->send_scte35_events);
|
|
|
|
break;
|
2019-08-23 21:55:00 +00:00
|
|
|
case PROP_LATENCY:
|
|
|
|
g_value_set_int (value, demux->latency);
|
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:30:29 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ts_demux_get_duration (GstTSDemux * demux, GstClockTime * dur)
|
|
|
|
{
|
|
|
|
MpegTSBase *base = (MpegTSBase *) demux;
|
|
|
|
gboolean res = FALSE;
|
|
|
|
gint64 val;
|
|
|
|
|
2018-06-12 09:14:25 +00:00
|
|
|
if (!demux->program) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "No active program yet, can't provide duration");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:30:29 +00:00
|
|
|
/* Get total size in bytes */
|
|
|
|
if (gst_pad_peer_query_duration (base->sinkpad, GST_FORMAT_BYTES, &val)) {
|
|
|
|
/* Convert it to duration */
|
|
|
|
*dur =
|
|
|
|
mpegts_packetizer_offset_to_ts (base->packetizer, val,
|
|
|
|
demux->program->pcr_pid);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (*dur))
|
|
|
|
res = TRUE;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static gboolean
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_ts_demux_srcpad_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
|
|
|
gboolean res = TRUE;
|
2011-02-22 11:33:56 +00:00
|
|
|
GstFormat format;
|
2011-02-16 16:57:42 +00:00
|
|
|
GstTSDemux *demux;
|
2011-08-23 22:40:25 +00:00
|
|
|
MpegTSBase *base;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
demux = GST_TS_DEMUX (parent);
|
2011-08-23 22:40:25 +00:00
|
|
|
base = GST_MPEGTS_BASE (demux);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_DURATION:
|
2012-02-29 09:33:00 +00:00
|
|
|
{
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "query duration");
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_query_parse_duration (query, &format, NULL);
|
2011-02-22 11:33:56 +00:00
|
|
|
if (format == GST_FORMAT_TIME) {
|
2012-03-01 17:05:17 +00:00
|
|
|
if (!gst_pad_peer_query (base->sinkpad, query)) {
|
2015-04-17 13:30:29 +00:00
|
|
|
GstClockTime dur;
|
|
|
|
if (gst_ts_demux_get_duration (demux, &dur))
|
|
|
|
gst_query_set_duration (query, GST_FORMAT_TIME, dur);
|
|
|
|
else
|
2012-03-01 17:05:17 +00:00
|
|
|
res = FALSE;
|
|
|
|
}
|
2011-02-22 11:33:56 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux, "only query duration on TIME is supported");
|
|
|
|
res = FALSE;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-29 09:33:00 +00:00
|
|
|
}
|
2011-11-08 19:12:50 +00:00
|
|
|
case GST_QUERY_LATENCY:
|
|
|
|
{
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "query latency");
|
2011-11-08 19:12:50 +00:00
|
|
|
res = gst_pad_peer_query (base->sinkpad, query);
|
2015-04-17 19:55:26 +00:00
|
|
|
if (res) {
|
2011-11-08 19:12:50 +00:00
|
|
|
GstClockTime min_lat, max_lat;
|
|
|
|
gboolean live;
|
2019-08-23 21:55:00 +00:00
|
|
|
gint latency;
|
2011-11-08 19:12:50 +00:00
|
|
|
|
|
|
|
/* According to H.222.0
|
|
|
|
Annex D.0.3 (System Time Clock recovery in the decoder)
|
|
|
|
and D.0.2 (Audio and video presentation synchronization)
|
|
|
|
|
|
|
|
We can end up with an interval of up to 700ms between valid
|
2014-06-04 11:39:48 +00:00
|
|
|
PTS/DTS. We therefore allow a latency of 700ms for that.
|
2011-11-08 19:12:50 +00:00
|
|
|
*/
|
2019-08-23 21:55:00 +00:00
|
|
|
latency = demux->latency;
|
|
|
|
if (latency < 0)
|
|
|
|
latency = 700;
|
2011-11-08 19:12:50 +00:00
|
|
|
gst_query_parse_latency (query, &live, &min_lat, &max_lat);
|
2019-08-23 21:55:00 +00:00
|
|
|
min_lat += latency * GST_MSECOND;
|
2015-04-17 19:55:26 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (max_lat))
|
2019-08-23 21:55:00 +00:00
|
|
|
max_lat += latency * GST_MSECOND;
|
2011-11-08 19:12:50 +00:00
|
|
|
gst_query_set_latency (query, live, min_lat, max_lat);
|
|
|
|
}
|
|
|
|
break;
|
2012-02-29 09:33:00 +00:00
|
|
|
}
|
2011-02-22 11:33:56 +00:00
|
|
|
case GST_QUERY_SEEKING:
|
2012-02-29 09:33:00 +00:00
|
|
|
{
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "query seeking");
|
2011-02-22 11:33:56 +00:00
|
|
|
gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "asked for format %s",
|
|
|
|
gst_format_get_name (format));
|
2011-02-22 11:33:56 +00:00
|
|
|
if (format == GST_FORMAT_TIME) {
|
2011-08-23 07:40:16 +00:00
|
|
|
gboolean seekable = FALSE;
|
|
|
|
|
2011-08-23 22:40:25 +00:00
|
|
|
if (gst_pad_peer_query (base->sinkpad, query))
|
2011-08-23 07:40:16 +00:00
|
|
|
gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
|
|
|
|
|
|
|
|
/* If upstream is not seekable in TIME format we use
|
|
|
|
* our own values here */
|
2015-04-17 13:30:29 +00:00
|
|
|
if (!seekable) {
|
|
|
|
GstClockTime dur;
|
|
|
|
if (gst_ts_demux_get_duration (demux, &dur)) {
|
|
|
|
gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, dur);
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "Gave duration: %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (dur));
|
2015-04-17 13:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-22 11:33:56 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (demux, "only TIME is supported for query seeking");
|
|
|
|
res = FALSE;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2012-02-29 09:33:00 +00:00
|
|
|
}
|
2013-07-29 09:01:09 +00:00
|
|
|
case GST_QUERY_SEGMENT:{
|
|
|
|
GstFormat format;
|
|
|
|
gint64 start, stop;
|
|
|
|
|
2018-08-31 12:43:46 +00:00
|
|
|
format = base->out_segment.format;
|
2013-07-29 09:01:09 +00:00
|
|
|
|
|
|
|
start =
|
2018-08-31 12:43:46 +00:00
|
|
|
gst_segment_to_stream_time (&base->out_segment, format,
|
|
|
|
base->out_segment.start);
|
|
|
|
if ((stop = base->out_segment.stop) == -1)
|
|
|
|
stop = base->out_segment.duration;
|
2013-07-29 09:01:09 +00:00
|
|
|
else
|
2018-08-31 12:43:46 +00:00
|
|
|
stop = gst_segment_to_stream_time (&base->out_segment, format, stop);
|
2013-07-29 09:01:09 +00:00
|
|
|
|
2018-08-31 12:43:46 +00:00
|
|
|
gst_query_set_segment (query, base->out_segment.rate, format, start,
|
|
|
|
stop);
|
2013-07-29 09:01:09 +00:00
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
default:
|
2012-04-19 12:20:52 +00:00
|
|
|
res = gst_pad_query_default (pad, parent, query);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
static void
|
|
|
|
clear_simple_buffer (SimpleBuffer * sbuf)
|
|
|
|
{
|
|
|
|
if (!sbuf->data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (sbuf->data);
|
|
|
|
sbuf->size = 0;
|
|
|
|
sbuf->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
scan_keyframe_h264 (TSDemuxStream * stream, const guint8 * data,
|
|
|
|
const gsize data_size, const gsize max_frame_offset)
|
|
|
|
{
|
|
|
|
gint offset = 0;
|
|
|
|
GstH264NalUnit unit, frame_unit = { 0, };
|
|
|
|
GstH264ParserResult res = GST_H264_PARSER_OK;
|
|
|
|
TSDemuxH264ParsingInfos *h264infos = &stream->h264infos;
|
|
|
|
|
|
|
|
GstH264NalParser *parser = h264infos->parser;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (parser == NULL)) {
|
|
|
|
parser = h264infos->parser = gst_h264_nal_parser_new ();
|
|
|
|
h264infos->sps = gst_byte_writer_new ();
|
|
|
|
h264infos->pps = gst_byte_writer_new ();
|
|
|
|
h264infos->sei = gst_byte_writer_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (res == GST_H264_PARSER_OK) {
|
|
|
|
res =
|
|
|
|
gst_h264_parser_identify_nalu (parser, data, offset, data_size, &unit);
|
|
|
|
|
|
|
|
if (res != GST_H264_PARSER_OK && res != GST_H264_PARSER_NO_NAL_END) {
|
|
|
|
GST_INFO_OBJECT (stream->pad, "Error identifying nalu: %i", res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = gst_h264_parser_parse_nal (parser, &unit);
|
|
|
|
if (res != GST_H264_PARSER_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (unit.type) {
|
|
|
|
case GST_H264_NAL_SEI:
|
|
|
|
if (frame_unit.size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (gst_byte_writer_put_data (h264infos->sei,
|
|
|
|
unit.data + unit.sc_offset,
|
|
|
|
unit.size + unit.offset - unit.sc_offset)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "adding SEI %u",
|
|
|
|
unit.size + unit.offset - unit.sc_offset);
|
2014-05-30 14:52:09 +00:00
|
|
|
} else {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (stream->pad, "Could not write SEI");
|
2014-05-30 14:52:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_H264_NAL_PPS:
|
|
|
|
if (frame_unit.size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (gst_byte_writer_put_data (h264infos->pps,
|
|
|
|
unit.data + unit.sc_offset,
|
|
|
|
unit.size + unit.offset - unit.sc_offset)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "adding PPS %u",
|
|
|
|
unit.size + unit.offset - unit.sc_offset);
|
2014-05-30 14:52:09 +00:00
|
|
|
} else {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (stream->pad, "Could not write PPS");
|
2014-05-30 14:52:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_H264_NAL_SPS:
|
|
|
|
if (frame_unit.size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (gst_byte_writer_put_data (h264infos->sps,
|
|
|
|
unit.data + unit.sc_offset,
|
|
|
|
unit.size + unit.offset - unit.sc_offset)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "adding SPS %u",
|
|
|
|
unit.size + unit.offset - unit.sc_offset);
|
2014-05-30 14:52:09 +00:00
|
|
|
} else {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (stream->pad, "Could not write SPS");
|
2014-05-30 14:52:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* these units are considered keyframes in h264parse */
|
|
|
|
case GST_H264_NAL_SLICE:
|
|
|
|
case GST_H264_NAL_SLICE_DPA:
|
|
|
|
case GST_H264_NAL_SLICE_DPB:
|
|
|
|
case GST_H264_NAL_SLICE_DPC:
|
|
|
|
case GST_H264_NAL_SLICE_IDR:
|
|
|
|
{
|
|
|
|
GstH264SliceHdr slice;
|
|
|
|
|
|
|
|
if (h264infos->framedata.size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
res = gst_h264_parser_parse_slice_hdr (parser, &unit, &slice,
|
|
|
|
FALSE, FALSE);
|
|
|
|
|
|
|
|
if (GST_H264_IS_I_SLICE (&slice) || GST_H264_IS_SI_SLICE (&slice)) {
|
|
|
|
if (*(unit.data + unit.offset + 1) & 0x80) {
|
|
|
|
/* means first_mb_in_slice == 0 */
|
|
|
|
/* real frame data */
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Found keyframe at: %u",
|
|
|
|
unit.sc_offset);
|
|
|
|
frame_unit = unit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset == unit.sc_offset + unit.size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset = unit.sc_offset + unit.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've got all the infos we need (SPS / PPS and a keyframe, plus
|
|
|
|
* and possibly SEI units. We can stop rewinding the stream
|
|
|
|
*/
|
|
|
|
if (gst_byte_writer_get_size (h264infos->sps) &&
|
|
|
|
gst_byte_writer_get_size (h264infos->pps) &&
|
|
|
|
(h264infos->framedata.size || frame_unit.size)) {
|
|
|
|
guint8 *data = NULL;
|
|
|
|
|
|
|
|
gsize tmpsize = gst_byte_writer_get_size (h264infos->pps);
|
|
|
|
|
|
|
|
/* We know that the SPS is first so just put all our data in there */
|
|
|
|
data = gst_byte_writer_reset_and_get_data (h264infos->pps);
|
|
|
|
gst_byte_writer_put_data (h264infos->sps, data, tmpsize);
|
|
|
|
g_free (data);
|
|
|
|
|
|
|
|
tmpsize = gst_byte_writer_get_size (h264infos->sei);
|
|
|
|
if (tmpsize) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Adding SEI");
|
2014-05-30 14:52:09 +00:00
|
|
|
data = gst_byte_writer_reset_and_get_data (h264infos->sei);
|
|
|
|
gst_byte_writer_put_data (h264infos->sps, data, tmpsize);
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_unit.size) { /* We found the everything in one go! */
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Adding Keyframe");
|
2014-05-30 14:52:09 +00:00
|
|
|
gst_byte_writer_put_data (h264infos->sps,
|
|
|
|
frame_unit.data + frame_unit.sc_offset,
|
|
|
|
stream->current_size - frame_unit.sc_offset);
|
|
|
|
} else {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Adding Keyframe");
|
2014-05-30 14:52:09 +00:00
|
|
|
gst_byte_writer_put_data (h264infos->sps,
|
|
|
|
h264infos->framedata.data, h264infos->framedata.size);
|
|
|
|
clear_simple_buffer (&h264infos->framedata);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->current_size = gst_byte_writer_get_size (h264infos->sps);
|
|
|
|
stream->data = gst_byte_writer_reset_and_get_data (h264infos->sps);
|
|
|
|
gst_byte_writer_init (h264infos->sps);
|
|
|
|
gst_byte_writer_init (h264infos->pps);
|
|
|
|
gst_byte_writer_init (h264infos->sei);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_unit.size) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Keep the keyframe as this is the one"
|
|
|
|
" we will push later");
|
|
|
|
|
|
|
|
h264infos->framedata.data =
|
2021-05-23 18:15:25 +00:00
|
|
|
g_memdup2 (frame_unit.data + frame_unit.sc_offset,
|
2014-05-30 14:52:09 +00:00
|
|
|
stream->current_size - frame_unit.sc_offset);
|
|
|
|
h264infos->framedata.size = stream->current_size - frame_unit.sc_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We merge data from TS packets so that the scanning methods get a continuous chunk,
|
|
|
|
however the scanning method will return keyframe offset which needs to be translated
|
|
|
|
back to actual offset in file */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint64 real_offset; /* offset of TS packet */
|
|
|
|
gint merged_offset; /* offset of merged data in buffer */
|
|
|
|
} OffsetInfo;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_ts_demux_adjust_seek_offset_for_keyframe (TSDemuxStream * stream,
|
|
|
|
guint8 * data, guint64 size)
|
|
|
|
{
|
|
|
|
int scan_pid = -1;
|
|
|
|
|
|
|
|
if (!stream->scan_function)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
scan_pid = ((MpegTSBaseStream *) stream)->pid;
|
|
|
|
|
|
|
|
if (scan_pid != -1) {
|
|
|
|
return stream->scan_function (stream, data, size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
static GstFlowReturn
|
2012-02-29 09:33:00 +00:00
|
|
|
gst_ts_demux_do_seek (MpegTSBase * base, GstEvent * event)
|
2011-02-22 11:33:56 +00:00
|
|
|
{
|
2014-05-30 14:52:09 +00:00
|
|
|
GList *tmp;
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
GstTSDemux *demux = (GstTSDemux *) base;
|
|
|
|
GstFlowReturn res = GST_FLOW_ERROR;
|
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
2012-03-01 17:05:17 +00:00
|
|
|
guint64 start_offset;
|
2018-09-19 15:05:52 +00:00
|
|
|
gboolean update = FALSE;
|
|
|
|
GstSegment seeksegment;
|
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "seek event, %" GST_PTR_FORMAT, event);
|
2011-02-22 11:33:56 +00:00
|
|
|
|
2021-04-05 07:29:37 +00:00
|
|
|
if (base->out_segment.format == GST_FORMAT_UNDEFINED) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Cannot process seek event now, delaying");
|
|
|
|
gst_event_replace (&base->seek_event, event);
|
|
|
|
res = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
|
|
|
|
&stop_type, &stop);
|
|
|
|
|
2013-08-21 12:44:38 +00:00
|
|
|
if (rate <= 0.0) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (demux, "Negative rate not supported");
|
2013-08-21 12:44:38 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
/* configure the segment with the seek variables */
|
2018-09-19 15:05:52 +00:00
|
|
|
memcpy (&seeksegment, &base->out_segment, sizeof (GstSegment));
|
|
|
|
GST_LOG_OBJECT (demux, "Before seek, output segment %" GST_SEGMENT_FORMAT,
|
|
|
|
&seeksegment);
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2018-09-19 15:05:52 +00:00
|
|
|
/* record offset and rate */
|
|
|
|
demux->rate = rate;
|
|
|
|
if (!gst_segment_do_seek (&seeksegment, rate, format, flags, start_type,
|
|
|
|
start, stop_type, stop, &update)) {
|
|
|
|
GST_DEBUG_OBJECT (demux, "Seek failed in gst_segment_do_seek()");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"After seek, update %d output segment now %" GST_SEGMENT_FORMAT, update,
|
|
|
|
&seeksegment);
|
2014-08-20 12:46:12 +00:00
|
|
|
|
2018-09-19 15:05:52 +00:00
|
|
|
/* If the position actually changed, update == TRUE */
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
2018-09-19 15:05:52 +00:00
|
|
|
if (update) {
|
2019-12-30 18:50:29 +00:00
|
|
|
GstClockTime target = seeksegment.start;
|
2018-09-19 15:05:52 +00:00
|
|
|
if (target >= SEEK_TIMESTAMP_OFFSET)
|
|
|
|
target -= SEEK_TIMESTAMP_OFFSET;
|
|
|
|
else
|
|
|
|
target = 0;
|
|
|
|
|
|
|
|
start_offset =
|
|
|
|
mpegts_packetizer_ts_to_offset (base->packetizer, target,
|
|
|
|
demux->program->pcr_pid);
|
2014-08-20 12:46:12 +00:00
|
|
|
if (G_UNLIKELY (start_offset == -1)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Couldn't convert start position to an offset");
|
2022-02-14 11:57:44 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
2014-08-20 12:46:12 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2018-09-19 15:05:52 +00:00
|
|
|
|
|
|
|
base->seek_offset = start_offset;
|
|
|
|
demux->last_seek_offset = base->seek_offset;
|
|
|
|
/* Reset segment if we're not doing an accurate seek */
|
|
|
|
demux->reset_segment = (!(flags & GST_SEEK_FLAG_ACCURATE));
|
|
|
|
|
|
|
|
/* Clear any existing segment - it will be recalculated after streaming recommences */
|
|
|
|
gst_event_replace (&demux->segment_event, NULL);
|
|
|
|
|
2014-08-20 12:46:12 +00:00
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = tmp->data;
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2018-09-19 15:05:52 +00:00
|
|
|
if (flags & GST_SEEK_FLAG_ACCURATE)
|
|
|
|
stream->needs_keyframe = TRUE;
|
|
|
|
|
|
|
|
stream->seeked_pts = GST_CLOCK_TIME_NONE;
|
|
|
|
stream->seeked_dts = GST_CLOCK_TIME_NONE;
|
|
|
|
stream->first_pts = GST_CLOCK_TIME_NONE;
|
2014-08-20 12:46:12 +00:00
|
|
|
stream->need_newsegment = TRUE;
|
|
|
|
}
|
2018-09-19 15:05:52 +00:00
|
|
|
} else {
|
|
|
|
/* Position didn't change, just update the output segment based on
|
|
|
|
* our new one */
|
2021-04-05 07:29:37 +00:00
|
|
|
gst_event_take (&demux->segment_event,
|
|
|
|
gst_event_new_segment (&seeksegment));
|
2018-09-19 15:05:52 +00:00
|
|
|
if (base->last_seek_seqnum)
|
|
|
|
gst_event_set_seqnum (demux->segment_event, base->last_seek_seqnum);
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = tmp->data;
|
|
|
|
stream->need_newsegment = TRUE;
|
2014-08-20 12:46:12 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
2011-02-22 11:33:56 +00:00
|
|
|
|
2018-09-19 15:05:52 +00:00
|
|
|
/* Commit the new segment */
|
|
|
|
memcpy (&base->out_segment, &seeksegment, sizeof (GstSegment));
|
2024-01-24 16:02:59 +00:00
|
|
|
/* And prepare to restart */
|
|
|
|
gst_flow_combiner_reset (demux->flowcombiner);
|
2012-03-01 17:05:17 +00:00
|
|
|
res = GST_FLOW_OK;
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
done:
|
|
|
|
return res;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
static gboolean
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_ts_demux_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2011-02-22 11:33:56 +00:00
|
|
|
{
|
|
|
|
gboolean res = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (parent);
|
2011-02-22 11:33:56 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "Got event %s",
|
|
|
|
gst_event_type_get_name (GST_EVENT_TYPE (event)));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_SEEK:
|
|
|
|
res = mpegts_base_handle_seek_event ((MpegTSBase *) demux, pad, event);
|
2011-08-26 22:44:08 +00:00
|
|
|
if (!res)
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (pad, "seeking failed");
|
2011-02-22 11:33:56 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-19 12:20:52 +00:00
|
|
|
res = gst_pad_event_default (pad, parent, event);
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-02-25 01:43:56 +00:00
|
|
|
static void
|
|
|
|
clean_global_taglist (GstTagList * taglist)
|
|
|
|
{
|
|
|
|
gst_tag_list_remove_tag (taglist, GST_TAG_CONTAINER_FORMAT);
|
|
|
|
gst_tag_list_remove_tag (taglist, GST_TAG_CODEC);
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static gboolean
|
|
|
|
push_event (MpegTSBase * base, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = (GstTSDemux *) base;
|
2011-07-19 07:34:37 +00:00
|
|
|
GList *tmp;
|
2014-02-25 01:43:56 +00:00
|
|
|
gboolean early_ret = FALSE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-03-30 12:39:49 +00:00
|
|
|
if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
|
2021-09-05 09:57:18 +00:00
|
|
|
if (base->segment.format == GST_FORMAT_TIME && base->ignore_pcr) {
|
|
|
|
/* Shift start/stop values by 2s */
|
|
|
|
base->packetizer->extra_shift = 2 * GST_SECOND;
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (base->segment.start))
|
|
|
|
base->segment.start += 2 * GST_SECOND;
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (base->segment.stop))
|
|
|
|
base->segment.stop += 2 * GST_SECOND;
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (base->segment.position))
|
|
|
|
base->segment.position += 2 * GST_SECOND;
|
|
|
|
}
|
2013-03-30 12:39:49 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "Ignoring segment event (recreated later)");
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
2014-02-25 01:43:56 +00:00
|
|
|
|
|
|
|
} else if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
|
|
|
|
/* In case we receive tags before data, store them to send later
|
|
|
|
* If we already have the program, send it right away */
|
|
|
|
GstTagList *taglist;
|
|
|
|
|
|
|
|
gst_event_parse_tag (event, &taglist);
|
|
|
|
|
|
|
|
if (demux->global_tags == NULL) {
|
|
|
|
demux->global_tags = gst_tag_list_copy (taglist);
|
|
|
|
|
|
|
|
/* Tags that are stream specific for the container should be considered
|
|
|
|
* global for the container streams */
|
|
|
|
if (gst_tag_list_get_scope (taglist) == GST_TAG_SCOPE_STREAM) {
|
|
|
|
gst_tag_list_set_scope (demux->global_tags, GST_TAG_SCOPE_GLOBAL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
demux->global_tags = gst_tag_list_make_writable (demux->global_tags);
|
|
|
|
gst_tag_list_insert (demux->global_tags, taglist, GST_TAG_MERGE_REPLACE);
|
|
|
|
}
|
|
|
|
clean_global_taglist (demux->global_tags);
|
|
|
|
|
|
|
|
/* tags are stored to be used after if there are no streams yet,
|
|
|
|
* so we should never reject */
|
|
|
|
early_ret = TRUE;
|
2013-03-30 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
2012-04-19 11:59:37 +00:00
|
|
|
if (G_UNLIKELY (demux->program == NULL)) {
|
|
|
|
gst_event_unref (event);
|
2014-02-25 01:43:56 +00:00
|
|
|
return early_ret;
|
2012-04-19 11:59:37 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-19 07:34:37 +00:00
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
if (stream->pad) {
|
2024-01-30 18:16:39 +00:00
|
|
|
/* If we are pushing out EOS or segment-done, flush out pending data first */
|
|
|
|
if ((GST_EVENT_TYPE (event) == GST_EVENT_EOS ||
|
|
|
|
GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT_DONE) &&
|
|
|
|
gst_pad_is_active (stream->pad)) {
|
2016-07-13 09:02:23 +00:00
|
|
|
gst_ts_demux_push_pending_data (demux, stream, NULL);
|
2024-01-30 18:16:39 +00:00
|
|
|
}
|
2013-07-29 06:20:10 +00:00
|
|
|
|
2011-07-19 07:34:37 +00:00
|
|
|
gst_event_ref (event);
|
|
|
|
gst_pad_push_event (stream->pad, event);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 11:59:37 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:36:43 +00:00
|
|
|
static void
|
|
|
|
handle_psi (MpegTSBase * base, GstMpegtsSection * section)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = (GstTSDemux *) base;
|
|
|
|
|
|
|
|
if (section->section_type == GST_MPEGTS_SECTION_SCTE_SIT) {
|
|
|
|
GList *tmp;
|
|
|
|
gboolean forward = FALSE;
|
|
|
|
|
|
|
|
if (demux->send_scte35_events) {
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
|
|
|
|
if (stream->stream.pid == section->pid) {
|
|
|
|
forward = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new section to travel through the pipeline, with splice
|
|
|
|
* times translated from local time to running time */
|
|
|
|
if (forward) {
|
|
|
|
GstEvent *event;
|
2021-04-22 23:15:08 +00:00
|
|
|
GstStructure *s;
|
|
|
|
GstStructure *rtime_map;
|
2021-04-05 22:36:43 +00:00
|
|
|
GstClockTime pts;
|
|
|
|
guint i = 0;
|
2021-04-22 23:15:08 +00:00
|
|
|
GstMpegtsSection *new_section =
|
|
|
|
(GstMpegtsSection *) gst_mini_object_copy ((GstMiniObject *) section);
|
|
|
|
GstMpegtsSCTESIT *sit =
|
|
|
|
(GstMpegtsSCTESIT *) gst_mpegts_section_get_scte_sit (new_section);
|
2021-04-05 22:36:43 +00:00
|
|
|
|
2024-08-09 07:41:57 +00:00
|
|
|
rtime_map = gst_structure_new_static_str_empty ("running-time-map");
|
2021-04-22 23:15:08 +00:00
|
|
|
|
|
|
|
if (sit->fully_parsed) {
|
|
|
|
if (sit->splice_time_specified) {
|
2021-04-05 22:36:43 +00:00
|
|
|
pts =
|
|
|
|
mpegts_packetizer_pts_to_ts (base->packetizer,
|
2021-04-22 23:15:08 +00:00
|
|
|
MPEGTIME_TO_GSTTIME (sit->splice_time +
|
|
|
|
sit->pts_adjustment), demux->program->pcr_pid);
|
|
|
|
gst_structure_set (rtime_map, "splice-time", G_TYPE_UINT64,
|
2021-04-05 22:36:43 +00:00
|
|
|
gst_segment_to_running_time (&base->out_segment, GST_FORMAT_TIME,
|
2021-04-22 23:15:08 +00:00
|
|
|
pts), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sit->splices->len; i++) {
|
|
|
|
gchar *field_name;
|
|
|
|
GstMpegtsSCTESpliceEvent *sevent =
|
|
|
|
g_ptr_array_index (sit->splices, i);
|
|
|
|
|
|
|
|
if (sevent->program_splice_time_specified) {
|
|
|
|
pts =
|
2022-03-24 17:55:48 +00:00
|
|
|
mpegts_packetizer_pts_to_ts_unchecked (base->packetizer,
|
2021-04-22 23:15:08 +00:00
|
|
|
MPEGTIME_TO_GSTTIME (sevent->program_splice_time +
|
|
|
|
sit->pts_adjustment), demux->program->pcr_pid);
|
|
|
|
field_name =
|
|
|
|
g_strdup_printf ("event-%u-splice-time",
|
|
|
|
sevent->splice_event_id);
|
|
|
|
gst_structure_set (rtime_map, field_name, G_TYPE_UINT64,
|
|
|
|
gst_segment_to_running_time (&base->out_segment,
|
|
|
|
GST_FORMAT_TIME, pts), NULL);
|
|
|
|
g_free (field_name);
|
2021-04-05 22:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event = gst_event_new_mpegts_section (new_section);
|
|
|
|
gst_mpegts_section_unref (new_section);
|
|
|
|
|
2021-04-22 23:15:08 +00:00
|
|
|
s = gst_event_writable_structure (event);
|
|
|
|
gst_structure_set (s, "mpeg-pts-offset", G_TYPE_UINT64,
|
|
|
|
demux->mpeg_pts_offset, "running-time-map", GST_TYPE_STRUCTURE,
|
|
|
|
rtime_map, NULL);
|
|
|
|
gst_structure_free (rtime_map);
|
|
|
|
|
2021-04-05 22:36:43 +00:00
|
|
|
push_event (base, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 15:06:09 +00:00
|
|
|
static gboolean
|
|
|
|
sink_query (MpegTSBase * base, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = (GstTSDemux *) base;
|
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_BITRATE:{
|
|
|
|
gint64 size_bytes;
|
|
|
|
GstClockTime duration;
|
|
|
|
|
|
|
|
if (gst_pad_peer_query_duration (base->sinkpad, GST_FORMAT_BYTES,
|
|
|
|
&size_bytes) && size_bytes > 0) {
|
|
|
|
if (gst_ts_demux_get_duration (demux, &duration) && duration > 0
|
|
|
|
&& duration != GST_CLOCK_TIME_NONE) {
|
|
|
|
guint bitrate =
|
|
|
|
gst_util_uint64_scale (8 * size_bytes, GST_SECOND, duration);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (demux, "bitrate query byte length: %" G_GINT64_FORMAT
|
|
|
|
" duration %" GST_TIME_FORMAT " resulting in a bitrate of %u",
|
|
|
|
size_bytes, GST_TIME_ARGS (duration), bitrate);
|
|
|
|
gst_query_set_bitrate (query, bitrate);
|
|
|
|
res = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
res = GST_MPEGTS_BASE_CLASS (parent_class)->sink_query (base, query);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-11 08:29:20 +00:00
|
|
|
static inline void
|
|
|
|
add_iso639_language_to_tags (TSDemuxStream * stream, gchar * lang_code)
|
|
|
|
{
|
|
|
|
const gchar *lc;
|
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (stream->pad, "Add language code for stream: '%s'", lang_code);
|
2013-10-11 08:29:20 +00:00
|
|
|
|
|
|
|
if (!stream->taglist)
|
|
|
|
stream->taglist = gst_tag_list_new_empty ();
|
|
|
|
|
|
|
|
/* descriptor contains ISO 639-2 code, we want the ISO 639-1 code */
|
|
|
|
lc = gst_tag_get_language_code (lang_code);
|
|
|
|
|
|
|
|
/* Only set tag if we have a valid one */
|
|
|
|
if (lc || (lang_code[0] && lang_code[1]))
|
|
|
|
gst_tag_list_add (stream->taglist, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_LANGUAGE_CODE, (lc) ? lc : lang_code, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-10 10:36:15 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_create_tags (TSDemuxStream * stream)
|
|
|
|
{
|
2013-06-23 06:43:23 +00:00
|
|
|
MpegTSBaseStream *bstream = (MpegTSBaseStream *) stream;
|
2014-06-25 12:50:05 +00:00
|
|
|
const GstMpegtsDescriptor *desc = NULL;
|
2013-10-11 08:29:20 +00:00
|
|
|
int i, nb;
|
2012-04-10 10:36:15 +00:00
|
|
|
|
2023-05-25 10:02:09 +00:00
|
|
|
if (bstream->stream_type == ST_PS_AUDIO_AC3 &&
|
|
|
|
!mpegts_get_descriptor_from_stream (bstream,
|
|
|
|
GST_MTS_DESC_DVB_ENHANCED_AC3)) {
|
|
|
|
const GstMpegtsDescriptor *ac3_desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
|
|
|
GST_MTS_DESC_AC3_AUDIO_STREAM);
|
|
|
|
if (ac3_desc && DESC_AC_AUDIO_STREAM_has_lang1 (ac3_desc->data)) {
|
|
|
|
gchar lang_code[4];
|
|
|
|
|
|
|
|
memcpy (lang_code, DESC_AC_AUDIO_STREAM_lang1_code (ac3_desc->data), 3);
|
|
|
|
lang_code[3] = '\0';
|
|
|
|
add_iso639_language_to_tags (stream, lang_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
|
|
|
GST_MTS_DESC_ISO_639_LANGUAGE);
|
2013-10-11 08:29:20 +00:00
|
|
|
if (desc) {
|
2014-06-01 08:44:23 +00:00
|
|
|
gchar *lang_code;
|
2012-07-26 18:38:28 +00:00
|
|
|
|
2013-10-11 08:29:20 +00:00
|
|
|
nb = gst_mpegts_descriptor_parse_iso_639_language_nb (desc);
|
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Found ISO 639 descriptor (%d entries)", nb);
|
2013-10-11 08:29:20 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nb; i++)
|
|
|
|
if (gst_mpegts_descriptor_parse_iso_639_language_idx (desc, i, &lang_code,
|
2014-06-01 08:44:23 +00:00
|
|
|
NULL)) {
|
2013-10-11 08:29:20 +00:00
|
|
|
add_iso639_language_to_tags (stream, lang_code);
|
2014-06-01 08:44:23 +00:00
|
|
|
g_free (lang_code);
|
|
|
|
}
|
2013-10-11 08:29:20 +00:00
|
|
|
|
|
|
|
return;
|
2012-07-26 18:38:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 08:29:20 +00:00
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream, GST_MTS_DESC_DVB_SUBTITLING);
|
|
|
|
|
2012-04-10 10:36:15 +00:00
|
|
|
if (desc) {
|
2014-06-01 08:44:23 +00:00
|
|
|
gchar *lang_code;
|
2012-04-10 10:36:15 +00:00
|
|
|
|
2013-10-11 08:29:20 +00:00
|
|
|
nb = gst_mpegts_descriptor_parse_dvb_subtitling_nb (desc);
|
2012-04-10 10:36:15 +00:00
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Found SUBTITLING descriptor (%d entries)",
|
|
|
|
nb);
|
2012-04-10 10:36:15 +00:00
|
|
|
|
2013-10-11 08:29:20 +00:00
|
|
|
for (i = 0; i < nb; i++)
|
|
|
|
if (gst_mpegts_descriptor_parse_dvb_subtitling_idx (desc, i, &lang_code,
|
2014-06-01 08:44:23 +00:00
|
|
|
NULL, NULL, NULL)) {
|
2013-10-11 08:29:20 +00:00
|
|
|
add_iso639_language_to_tags (stream, lang_code);
|
2014-06-01 08:44:23 +00:00
|
|
|
g_free (lang_code);
|
|
|
|
}
|
2012-04-10 10:36:15 +00:00
|
|
|
}
|
2020-09-04 15:34:16 +00:00
|
|
|
|
|
|
|
if (bstream->stream_type == GST_MPEGTS_STREAM_TYPE_PRIVATE_PES_PACKETS) {
|
|
|
|
desc = mpegts_get_descriptor_from_stream_with_extension (bstream,
|
|
|
|
GST_MTS_DESC_DVB_EXTENSION, GST_MTS_DESC_EXT_DVB_AUDIO_PRESELECTION);
|
|
|
|
|
|
|
|
if (desc) {
|
|
|
|
GPtrArray *list;
|
|
|
|
GstMpegtsAudioPreselectionDescriptor *item;
|
|
|
|
|
|
|
|
if (gst_mpegts_descriptor_parse_audio_preselection_list (desc, &list)) {
|
|
|
|
GST_DEBUG ("Found AUDIO PRESELECTION descriptor (%d entries)",
|
|
|
|
list->len);
|
|
|
|
|
|
|
|
for (i = 0; i < list->len; i++) {
|
|
|
|
item = g_ptr_array_index (list, i);
|
|
|
|
gst_mpegts_descriptor_parse_audio_preselection_dump (item);
|
|
|
|
|
|
|
|
if (item->language_code_present) {
|
|
|
|
add_iso639_language_to_tags (stream, item->language_code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_ptr_array_unref (list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-10 10:36:15 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static GstPad *
|
2011-02-21 10:44:01 +00:00
|
|
|
create_pad_for_stream (MpegTSBase * base, MpegTSBaseStream * bstream,
|
2011-02-16 16:57:42 +00:00
|
|
|
MpegTSBaseProgram * program)
|
|
|
|
{
|
2013-07-23 08:24:22 +00:00
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (base);
|
2011-02-16 16:57:42 +00:00
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) bstream;
|
|
|
|
gchar *name = NULL;
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
GstPadTemplate *template = NULL;
|
2014-06-25 12:50:05 +00:00
|
|
|
const GstMpegtsDescriptor *desc = NULL;
|
2011-02-16 16:57:42 +00:00
|
|
|
GstPad *pad = NULL;
|
2014-08-02 10:29:37 +00:00
|
|
|
gboolean sparse = FALSE;
|
2015-06-04 16:36:32 +00:00
|
|
|
gboolean is_audio = FALSE, is_video = FALSE, is_subpicture = FALSE,
|
|
|
|
is_private = FALSE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-10 10:36:15 +00:00
|
|
|
gst_ts_demux_create_tags (stream);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"Attempting to create pad for stream 0x%04x with stream_type %d",
|
2011-02-16 16:57:42 +00:00
|
|
|
bstream->pid, bstream->stream_type);
|
|
|
|
|
2013-04-17 20:45:19 +00:00
|
|
|
/* First handle BluRay-specific stream types since there is some overlap
|
|
|
|
* between BluRay and non-BluRay streay type identifiers */
|
2013-07-06 08:36:33 +00:00
|
|
|
if (program->registration_id == DRF_ID_HDMV) {
|
|
|
|
switch (bstream->stream_type) {
|
|
|
|
case ST_BD_AUDIO_AC3:
|
|
|
|
{
|
2014-06-25 12:50:05 +00:00
|
|
|
const GstMpegtsDescriptor *ac3_desc;
|
2013-07-06 08:36:33 +00:00
|
|
|
|
|
|
|
/* ATSC ac3 audio descriptor */
|
|
|
|
ac3_desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
|
|
|
GST_MTS_DESC_AC3_AUDIO_STREAM);
|
2013-10-05 12:45:32 +00:00
|
|
|
if (ac3_desc && DESC_AC_AUDIO_STREAM_bsid (ac3_desc->data) != 16) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "ac3 audio");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-ac3");
|
|
|
|
} else {
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-eac3");
|
|
|
|
}
|
|
|
|
break;
|
2013-04-17 20:45:19 +00:00
|
|
|
}
|
2013-07-06 08:36:33 +00:00
|
|
|
case ST_BD_AUDIO_EAC3:
|
2014-03-03 12:07:47 +00:00
|
|
|
case ST_BD_AUDIO_AC3_PLUS:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-eac3");
|
|
|
|
break;
|
|
|
|
case ST_BD_AUDIO_AC3_TRUE_HD:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-true-hd");
|
2014-03-03 12:07:47 +00:00
|
|
|
stream->target_pes_substream = 0x72;
|
2013-07-06 08:36:33 +00:00
|
|
|
break;
|
|
|
|
case ST_BD_AUDIO_LPCM:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-private-ts-lpcm");
|
|
|
|
break;
|
|
|
|
case ST_BD_PGS_SUBPICTURE:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_subpicture = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("subpicture/x-pgs");
|
2014-08-02 10:29:37 +00:00
|
|
|
sparse = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
break;
|
2014-03-03 12:07:47 +00:00
|
|
|
case ST_BD_AUDIO_DTS_HD:
|
|
|
|
case ST_BD_AUDIO_DTS_HD_MASTER_AUDIO:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2014-03-03 12:07:47 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-dts");
|
|
|
|
stream->target_pes_substream = 0x71;
|
|
|
|
break;
|
2013-04-17 20:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 16:36:32 +00:00
|
|
|
|
|
|
|
if (caps)
|
2013-04-17 20:45:19 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Handle non-BluRay stream types */
|
2011-02-16 16:57:42 +00:00
|
|
|
switch (bstream->stream_type) {
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_MPEG1:
|
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_MPEG2:
|
2013-04-17 20:45:19 +00:00
|
|
|
case ST_PS_VIDEO_MPEG2_DCII:
|
2013-06-23 06:43:23 +00:00
|
|
|
/* FIXME : Use DCII registration code (ETV1 ?) to handle that special
|
|
|
|
* Stream type (ST_PS_VIDEO_MPEG2_DCII) */
|
2019-09-02 19:08:44 +00:00
|
|
|
/* FIXME : Use video descriptor (0x1) to refine caps with:
|
2013-07-04 05:49:37 +00:00
|
|
|
* * frame_rate
|
|
|
|
* * profile_and_level
|
|
|
|
*/
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "mpeg video");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
caps = gst_caps_new_simple ("video/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT,
|
2014-06-25 12:50:05 +00:00
|
|
|
bstream->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_MPEG1 ? 1 : 2,
|
2013-07-04 05:49:37 +00:00
|
|
|
"systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_AUDIO_MPEG1:
|
|
|
|
case GST_MPEGTS_STREAM_TYPE_AUDIO_MPEG2:
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "mpeg audio");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
caps =
|
|
|
|
gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
|
|
|
|
NULL);
|
2013-07-27 10:17:34 +00:00
|
|
|
/* HDV is always mpeg 1 audio layer 2 */
|
|
|
|
if (program->registration_id == DRF_ID_TSHV)
|
|
|
|
gst_caps_set_simple (caps, "layer", G_TYPE_INT, 2, NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_PRIVATE_PES_PACKETS:
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "private data");
|
2013-06-23 06:43:23 +00:00
|
|
|
/* FIXME: Move all of this into a common method (there might be other
|
|
|
|
* types also, depending on registratino descriptors also
|
|
|
|
*/
|
2020-03-09 09:00:01 +00:00
|
|
|
|
2020-09-04 15:28:58 +00:00
|
|
|
desc = mpegts_get_descriptor_from_stream_with_extension (bstream,
|
|
|
|
GST_MTS_DESC_DVB_EXTENSION, GST_MTS_DESC_EXT_DVB_AC4);
|
|
|
|
if (desc) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "ac4 audio");
|
2020-03-09 09:00:01 +00:00
|
|
|
is_audio = TRUE;
|
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-ac4");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-06 08:36:33 +00:00
|
|
|
desc = mpegts_get_descriptor_from_stream (bstream, GST_MTS_DESC_DVB_AC3);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (desc) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "ac3 audio");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-ac3");
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-02-20 20:32:21 +00:00
|
|
|
|
2013-07-06 08:36:33 +00:00
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_MTS_DESC_DVB_ENHANCED_AC3);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (desc) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "ac3 audio");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-eac3");
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-06 08:36:33 +00:00
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_MTS_DESC_DVB_TELETEXT);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (desc) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "teletext");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_private = TRUE;
|
2013-07-30 15:08:17 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("application/x-teletext");
|
2014-08-02 10:29:37 +00:00
|
|
|
sparse = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
desc =
|
2013-07-06 08:36:33 +00:00
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_MTS_DESC_DVB_SUBTITLING);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (desc) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "subtitling");
|
2015-10-20 07:49:16 +00:00
|
|
|
is_subpicture = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("subpicture/x-dvb");
|
2014-08-02 10:29:37 +00:00
|
|
|
sparse = TRUE;
|
2012-01-23 13:10:43 +00:00
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2012-02-20 20:32:21 +00:00
|
|
|
|
2013-07-06 08:36:33 +00:00
|
|
|
switch (bstream->registration_id) {
|
|
|
|
case DRF_ID_DTS1:
|
|
|
|
case DRF_ID_DTS2:
|
|
|
|
case DRF_ID_DTS3:
|
|
|
|
/* SMPTE registered DTS */
|
2015-06-04 16:36:32 +00:00
|
|
|
is_private = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-dts");
|
|
|
|
break;
|
|
|
|
case DRF_ID_S302M:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-smpte-302m");
|
|
|
|
break;
|
2015-10-24 11:27:43 +00:00
|
|
|
case DRF_ID_OPUS:
|
|
|
|
desc = mpegts_get_descriptor_from_stream (bstream,
|
|
|
|
GST_MTS_DESC_DVB_EXTENSION);
|
|
|
|
if (desc != NULL && desc->tag_extension == 0x80 && desc->length >= 1) { /* User defined (provisional Opus) */
|
|
|
|
guint8 channel_config_code;
|
|
|
|
GstByteReader br;
|
|
|
|
|
|
|
|
/* skip tag, length and tag_extension */
|
|
|
|
gst_byte_reader_init (&br, desc->data + 3, desc->length - 1);
|
|
|
|
channel_config_code = gst_byte_reader_get_uint8_unchecked (&br);
|
|
|
|
|
2023-03-16 12:21:21 +00:00
|
|
|
if (channel_config_code < 0x89) {
|
2015-10-24 11:27:43 +00:00
|
|
|
static const guint8 coupled_stream_counts[9] = {
|
|
|
|
1, 0, 1, 1, 2, 2, 2, 3, 3
|
|
|
|
};
|
|
|
|
static const guint8 channel_map_a[8][8] = {
|
|
|
|
{0},
|
|
|
|
{0, 1},
|
|
|
|
{0, 2, 1},
|
|
|
|
{0, 1, 2, 3},
|
|
|
|
{0, 4, 1, 2, 3},
|
|
|
|
{0, 4, 1, 2, 3, 5},
|
|
|
|
{0, 4, 1, 2, 3, 5, 6},
|
|
|
|
{0, 6, 1, 2, 3, 4, 5, 7},
|
|
|
|
};
|
|
|
|
static const guint8 channel_map_b[8][8] = {
|
|
|
|
{0},
|
|
|
|
{0, 1},
|
|
|
|
{0, 1, 2},
|
|
|
|
{0, 1, 2, 3},
|
|
|
|
{0, 1, 2, 3, 4},
|
|
|
|
{0, 1, 2, 3, 4, 5},
|
|
|
|
{0, 1, 2, 3, 4, 5, 6},
|
|
|
|
{0, 1, 2, 3, 4, 5, 6, 7},
|
|
|
|
};
|
|
|
|
|
2015-11-03 17:51:03 +00:00
|
|
|
gint channels = -1, stream_count, coupled_count, mapping_family;
|
2023-03-16 12:21:21 +00:00
|
|
|
guint8 channel_mapping[255] = { 0, };
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
channels = channel_config_code ? (channel_config_code & 0x0f) : 2;
|
|
|
|
if (channel_config_code == 0 || channel_config_code == 0x80) {
|
|
|
|
/* Dual Mono */
|
2023-04-18 11:55:23 +00:00
|
|
|
channels = 2;
|
2015-11-03 17:51:03 +00:00
|
|
|
mapping_family = 255;
|
2015-10-24 11:27:43 +00:00
|
|
|
if (channel_config_code == 0) {
|
2015-11-03 17:51:03 +00:00
|
|
|
stream_count = 1;
|
|
|
|
coupled_count = 1;
|
2015-10-24 11:27:43 +00:00
|
|
|
} else {
|
2015-11-03 17:51:03 +00:00
|
|
|
stream_count = 2;
|
|
|
|
coupled_count = 0;
|
2015-10-24 11:27:43 +00:00
|
|
|
}
|
2015-11-03 17:51:03 +00:00
|
|
|
memcpy (channel_mapping, &channel_map_a[1], channels);
|
2015-10-24 11:27:43 +00:00
|
|
|
} else if (channel_config_code <= 8) {
|
2015-11-03 17:51:03 +00:00
|
|
|
mapping_family = (channels > 2) ? 1 : 0;
|
|
|
|
stream_count =
|
2015-10-24 11:27:43 +00:00
|
|
|
channel_config_code -
|
|
|
|
coupled_stream_counts[channel_config_code];
|
2015-11-03 17:51:03 +00:00
|
|
|
coupled_count = coupled_stream_counts[channel_config_code];
|
|
|
|
if (mapping_family != 0) {
|
|
|
|
memcpy (channel_mapping, &channel_map_a[channels - 1],
|
|
|
|
channels);
|
|
|
|
}
|
2015-10-24 11:27:43 +00:00
|
|
|
} else if (channel_config_code >= 0x82
|
|
|
|
&& channel_config_code <= 0x88) {
|
2015-11-03 17:51:03 +00:00
|
|
|
mapping_family = 1;
|
|
|
|
stream_count = channels;
|
|
|
|
coupled_count = 0;
|
|
|
|
memcpy (channel_mapping, &channel_map_b[channels - 1],
|
|
|
|
channels);
|
2015-10-24 11:27:43 +00:00
|
|
|
} else if (channel_config_code == 0x81) {
|
|
|
|
if (gst_byte_reader_get_remaining (&br) < 2) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Invalid Opus descriptor with extended channel configuration");
|
2015-11-03 17:51:03 +00:00
|
|
|
channels = -1;
|
2015-10-24 11:27:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:51:03 +00:00
|
|
|
channels = gst_byte_reader_get_uint8_unchecked (&br);
|
2015-10-24 11:27:43 +00:00
|
|
|
mapping_family = gst_byte_reader_get_uint8_unchecked (&br);
|
|
|
|
|
|
|
|
/* Overwrite values from above */
|
2015-11-03 17:51:03 +00:00
|
|
|
if (channels == 0) {
|
2015-10-24 11:27:43 +00:00
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Invalid Opus descriptor with extended channel configuration");
|
2015-11-03 17:51:03 +00:00
|
|
|
channels = -1;
|
2015-10-24 11:27:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:51:03 +00:00
|
|
|
if (mapping_family == 0 && channels <= 2) {
|
|
|
|
stream_count = channels - coupled_stream_counts[channels];
|
|
|
|
coupled_count = coupled_stream_counts[channels];
|
2015-10-24 11:27:43 +00:00
|
|
|
} else {
|
|
|
|
GstBitReader breader;
|
|
|
|
guint8 stream_count_minus_one, coupled_stream_count;
|
|
|
|
gint stream_count_minus_one_len, coupled_stream_count_len;
|
|
|
|
gint channel_mapping_len, i;
|
2023-03-17 13:51:53 +00:00
|
|
|
guint remaining_bytes;
|
2015-10-24 11:27:43 +00:00
|
|
|
|
2023-03-17 13:51:53 +00:00
|
|
|
remaining_bytes = gst_byte_reader_get_remaining (&br);
|
2015-10-24 11:27:43 +00:00
|
|
|
gst_bit_reader_init (&breader,
|
|
|
|
gst_byte_reader_get_data_unchecked
|
2023-03-17 13:51:53 +00:00
|
|
|
(&br, remaining_bytes), remaining_bytes);
|
2015-10-24 11:27:43 +00:00
|
|
|
|
2023-03-16 12:21:21 +00:00
|
|
|
stream_count_minus_one_len = g_bit_storage (channels);
|
2015-10-24 11:27:43 +00:00
|
|
|
if (!gst_bit_reader_get_bits_uint8 (&breader,
|
|
|
|
&stream_count_minus_one,
|
|
|
|
stream_count_minus_one_len)) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Invalid Opus descriptor with extended channel configuration");
|
2015-11-03 17:51:03 +00:00
|
|
|
channels = -1;
|
2015-10-24 11:27:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:51:03 +00:00
|
|
|
stream_count = stream_count_minus_one + 1;
|
2023-03-16 12:21:21 +00:00
|
|
|
coupled_stream_count_len = g_bit_storage (stream_count + 1);
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
if (!gst_bit_reader_get_bits_uint8 (&breader,
|
|
|
|
&coupled_stream_count, coupled_stream_count_len)) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Invalid Opus descriptor with extended channel configuration");
|
2015-11-03 17:51:03 +00:00
|
|
|
channels = -1;
|
2015-10-24 11:27:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:51:03 +00:00
|
|
|
coupled_count = coupled_stream_count;
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
channel_mapping_len =
|
2023-03-16 12:21:21 +00:00
|
|
|
g_bit_storage (stream_count + coupled_stream_count + 1);
|
2015-11-03 17:51:03 +00:00
|
|
|
for (i = 0; i < channels; i++) {
|
2015-10-24 11:27:43 +00:00
|
|
|
if (!gst_bit_reader_get_bits_uint8 (&breader,
|
2015-11-03 17:51:03 +00:00
|
|
|
&channel_mapping[i], channel_mapping_len)) {
|
2015-10-24 11:27:43 +00:00
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"Invalid Opus descriptor with extended channel configuration");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* error above */
|
2015-11-03 17:51:03 +00:00
|
|
|
if (i != channels) {
|
|
|
|
channels = -1;
|
2015-10-24 11:27:43 +00:00
|
|
|
break;
|
2015-11-03 17:51:03 +00:00
|
|
|
}
|
2015-10-24 11:27:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:51:03 +00:00
|
|
|
if (channels != -1) {
|
2015-10-24 11:27:43 +00:00
|
|
|
is_audio = TRUE;
|
2015-11-03 17:53:00 +00:00
|
|
|
caps =
|
|
|
|
gst_codec_utils_opus_create_caps (48000, channels,
|
|
|
|
mapping_family, stream_count, coupled_count,
|
|
|
|
channel_mapping);
|
2015-10-24 11:27:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"unexpected channel config code 0x%02x", channel_config_code);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (demux, "Opus, but no extension descriptor");
|
|
|
|
}
|
|
|
|
break;
|
2014-01-06 12:51:01 +00:00
|
|
|
case DRF_ID_HEVC:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2014-01-06 12:51:01 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-h265",
|
2018-10-10 18:36:57 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
|
2014-01-06 12:51:01 +00:00
|
|
|
break;
|
2014-06-30 12:20:12 +00:00
|
|
|
case DRF_ID_KLVA:
|
|
|
|
sparse = TRUE;
|
|
|
|
is_private = TRUE;
|
|
|
|
caps = gst_caps_new_simple ("meta/x-klv",
|
|
|
|
"parsed", G_TYPE_BOOLEAN, TRUE, NULL);
|
|
|
|
break;
|
2020-03-10 07:32:59 +00:00
|
|
|
case DRF_ID_AC4:
|
|
|
|
is_audio = TRUE;
|
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-ac4");
|
|
|
|
break;
|
2024-09-06 08:44:46 +00:00
|
|
|
case DRF_ID_VANC:
|
|
|
|
is_private = TRUE;
|
|
|
|
caps =
|
|
|
|
gst_caps_new_simple ("meta/x-st-2038", "alignment", G_TYPE_STRING,
|
|
|
|
"line", NULL);
|
|
|
|
break;
|
2012-02-20 20:32:21 +00:00
|
|
|
}
|
2015-06-08 13:16:22 +00:00
|
|
|
if (caps)
|
2015-06-08 13:14:41 +00:00
|
|
|
break;
|
2012-02-20 20:32:21 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
/* hack for itv hd (sid 10510, video pid 3401 */
|
|
|
|
if (program->program_number == 10510 && bstream->pid == 3401) {
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2011-04-24 23:42:03 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-h264",
|
2018-10-10 18:36:57 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ST_HDV_AUX_V:
|
2013-06-23 06:43:23 +00:00
|
|
|
/* FIXME : Should only be used with specific PMT registration_descriptor */
|
2011-02-16 16:57:42 +00:00
|
|
|
/* We don't expose those streams since they're only helper streams */
|
|
|
|
/* template = gst_static_pad_template_get (&private_template); */
|
|
|
|
/* name = g_strdup_printf ("private_%04x", bstream->pid); */
|
|
|
|
/* caps = gst_caps_new_simple ("hdv/aux-v", NULL); */
|
|
|
|
break;
|
|
|
|
case ST_HDV_AUX_A:
|
2013-06-23 06:43:23 +00:00
|
|
|
/* FIXME : Should only be used with specific PMT registration_descriptor */
|
2011-02-16 16:57:42 +00:00
|
|
|
/* We don't expose those streams since they're only helper streams */
|
|
|
|
/* template = gst_static_pad_template_get (&private_template); */
|
|
|
|
/* name = g_strdup_printf ("private_%04x", bstream->pid); */
|
|
|
|
/* caps = gst_caps_new_simple ("hdv/aux-a", NULL); */
|
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_AUDIO_AAC_ADTS:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2020-03-20 11:27:05 +00:00
|
|
|
/* prefer mpegversion 4 since it's more commonly supported one */
|
2011-02-16 16:57:42 +00:00
|
|
|
caps = gst_caps_new_simple ("audio/mpeg",
|
2020-03-20 11:27:05 +00:00
|
|
|
"mpegversion", G_TYPE_INT, 4,
|
2012-02-02 11:37:55 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "adts", NULL);
|
2020-03-20 11:27:05 +00:00
|
|
|
/* we will set caps later once parsing adts header is done */
|
|
|
|
stream->atdsInfos.mpegversion = 4;
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_AUDIO_AAC_LATM:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-02-14 14:44:48 +00:00
|
|
|
caps = gst_caps_new_simple ("audio/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT, 4,
|
2012-06-28 15:40:16 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "loas", NULL);
|
2012-02-14 14:44:48 +00:00
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_MPEG4:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
caps = gst_caps_new_simple ("video/mpeg",
|
|
|
|
"mpegversion", G_TYPE_INT, 4,
|
|
|
|
"systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
|
|
|
|
break;
|
2023-03-30 09:09:59 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_METADATA_PES_PACKETS:
|
|
|
|
desc = mpegts_get_descriptor_from_stream (bstream, GST_MTS_DESC_METADATA);
|
|
|
|
if (desc) {
|
|
|
|
GstMpegtsMetadataDescriptor *metadataDescriptor;
|
|
|
|
if (gst_mpegts_descriptor_parse_metadata (desc, &metadataDescriptor)) {
|
|
|
|
if ((metadataDescriptor->metadata_format ==
|
|
|
|
GST_MPEGTS_METADATA_FORMAT_IDENTIFIER_FIELD)
|
|
|
|
&& (metadataDescriptor->metadata_format_identifier ==
|
|
|
|
DRF_ID_KLVA)) {
|
|
|
|
sparse = TRUE;
|
|
|
|
is_private = TRUE;
|
|
|
|
/* registration_id is not correctly set or parsed for some streams */
|
|
|
|
bstream->registration_id = DRF_ID_KLVA;
|
|
|
|
|
|
|
|
caps = gst_caps_new_simple ("meta/x-klv",
|
2023-03-30 16:57:10 +00:00
|
|
|
"parsed", G_TYPE_BOOLEAN, TRUE, NULL);
|
2023-03-30 09:09:59 +00:00
|
|
|
}
|
|
|
|
g_free (metadataDescriptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_H264:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2011-04-24 23:42:03 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-h264",
|
2018-10-10 18:36:57 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
|
2014-01-06 12:51:01 +00:00
|
|
|
break;
|
2014-06-25 12:50:05 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_HEVC:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2014-01-06 12:51:01 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-h265",
|
2018-10-10 18:36:57 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2017-07-19 14:14:21 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_JP2K:
|
|
|
|
is_video = TRUE;
|
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream, GST_MTS_DESC_J2K_VIDEO);
|
|
|
|
if (desc == NULL) {
|
|
|
|
caps = gst_caps_new_empty_simple ("image/x-jpc");
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
GstByteReader br;
|
|
|
|
guint16 DEN_frame_rate = 0;
|
|
|
|
guint16 NUM_frame_rate = 0;
|
|
|
|
guint8 color_specification = 0;
|
|
|
|
guint8 remaining_8b = 0;
|
|
|
|
gboolean interlaced_video = 0;
|
|
|
|
const gchar *interlace_mode = NULL;
|
|
|
|
const gchar *colorspace = NULL;
|
|
|
|
const gchar *colorimetry_mode = NULL;
|
|
|
|
guint16 profile_and_level G_GNUC_UNUSED;
|
|
|
|
guint32 horizontal_size G_GNUC_UNUSED;
|
|
|
|
guint32 vertical_size G_GNUC_UNUSED;
|
|
|
|
guint32 max_bit_rate G_GNUC_UNUSED;
|
|
|
|
guint32 max_buffer_size G_GNUC_UNUSED;
|
|
|
|
const guint desc_min_length = 24;
|
|
|
|
|
|
|
|
if (desc->length < desc_min_length) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_ERROR_OBJECT (demux,
|
|
|
|
"GST_MPEGTS_STREAM_TYPE_VIDEO_JP2K: descriptor length %d too short",
|
2017-07-19 14:14:21 +00:00
|
|
|
desc->length);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the descriptor tag and length */
|
|
|
|
gst_byte_reader_init (&br, desc->data + 2, desc->length);
|
|
|
|
|
|
|
|
profile_and_level = gst_byte_reader_get_uint16_be_unchecked (&br);
|
|
|
|
horizontal_size = gst_byte_reader_get_uint32_be_unchecked (&br);
|
|
|
|
vertical_size = gst_byte_reader_get_uint32_be_unchecked (&br);
|
|
|
|
max_bit_rate = gst_byte_reader_get_uint32_be_unchecked (&br);
|
|
|
|
max_buffer_size = gst_byte_reader_get_uint32_be_unchecked (&br);
|
|
|
|
DEN_frame_rate = gst_byte_reader_get_uint16_be_unchecked (&br);
|
|
|
|
NUM_frame_rate = gst_byte_reader_get_uint16_be_unchecked (&br);
|
|
|
|
color_specification = gst_byte_reader_get_uint8_unchecked (&br);
|
|
|
|
remaining_8b = gst_byte_reader_get_uint8_unchecked (&br);
|
|
|
|
interlaced_video = remaining_8b & 0x40;
|
|
|
|
/* we don't support demuxing interlaced at the moment */
|
|
|
|
if (interlaced_video) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_ERROR_OBJECT (demux,
|
|
|
|
"GST_MPEGTS_STREAM_TYPE_VIDEO_JP2K: interlaced video not supported");
|
2017-07-19 14:14:21 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
interlace_mode = "progressive";
|
|
|
|
stream->jp2kInfos.interlace = FALSE;
|
|
|
|
}
|
|
|
|
switch (color_specification) {
|
|
|
|
case GST_MPEGTSDEMUX_JPEG2000_COLORSPEC_SRGB:
|
|
|
|
colorspace = "sRGB";
|
|
|
|
colorimetry_mode = GST_VIDEO_COLORIMETRY_SRGB;
|
|
|
|
break;
|
|
|
|
case GST_MPEGTSDEMUX_JPEG2000_COLORSPEC_REC601:
|
|
|
|
colorspace = "sYUV";
|
|
|
|
colorimetry_mode = GST_VIDEO_COLORIMETRY_BT601;
|
|
|
|
break;
|
|
|
|
case GST_MPEGTSDEMUX_JPEG2000_COLORSPEC_REC709:
|
|
|
|
case GST_MPEGTSDEMUX_JPEG2000_COLORSPEC_CIELUV:
|
|
|
|
colorspace = "sYUV";
|
|
|
|
colorimetry_mode = GST_VIDEO_COLORIMETRY_BT709;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
caps = gst_caps_new_simple ("image/x-jpc",
|
|
|
|
"framerate", GST_TYPE_FRACTION, NUM_frame_rate, DEN_frame_rate,
|
|
|
|
"interlace-mode", G_TYPE_STRING, interlace_mode,
|
|
|
|
"colorimetry", G_TYPE_STRING, colorimetry_mode,
|
|
|
|
"colorspace", G_TYPE_STRING, colorspace, NULL);
|
|
|
|
}
|
|
|
|
break;
|
2024-07-15 14:10:10 +00:00
|
|
|
case GST_MPEGTS_STREAM_TYPE_VIDEO_JPEG_XS:
|
|
|
|
{
|
|
|
|
GstMpegtsJpegXsDescriptor jpegxs;
|
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream_with_extension (bstream,
|
|
|
|
GST_MTS_DESC_EXTENSION, GST_MTS_DESC_EXT_JXS_VIDEO);
|
|
|
|
if (!desc) {
|
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"image/x-jxsc stream does not have mandatory descriptor");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!gst_mpegts_descriptor_parse_jpeg_xs (desc, &jpegxs)) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Invalid JPEG XS descriptor");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (jpegxs.frat >> 30) {
|
|
|
|
GST_WARNING_OBJECT (demux, "Interlaced JPEG-XS not supported yet");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((jpegxs.schar >> 15) == 0) {
|
|
|
|
GST_WARNING_OBJECT (demux, "JPEG-XS sampling properties are required");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
is_video = TRUE;
|
|
|
|
caps =
|
|
|
|
gst_caps_from_string
|
|
|
|
("image/x-jxsc, alignment=(string)frame, interlace-mode=(string)progressive");
|
|
|
|
|
|
|
|
/* interlace-mode, sampling, depth */
|
|
|
|
gst_caps_set_simple (caps, "width", G_TYPE_INT, jpegxs.horizontal_size,
|
|
|
|
"height", G_TYPE_INT, jpegxs.vertical_size, "depth", G_TYPE_INT,
|
|
|
|
(jpegxs.schar >> 4) & 0xf, NULL);
|
|
|
|
switch (jpegxs.schar & 0xf) {
|
|
|
|
case 0:
|
|
|
|
gst_caps_set_simple (caps, "sampling", G_TYPE_STRING, "YCbCr-4:2:2",
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
gst_caps_set_simple (caps, "sampling", G_TYPE_STRING, "YCbCr-4:4:4",
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_WARNING_OBJECT (demux, "Unsupported JPEG-XS sampling format");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
case ST_VIDEO_DIRAC:
|
2013-07-06 08:36:33 +00:00
|
|
|
if (bstream->registration_id == 0x64726163) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "dirac");
|
2013-07-06 08:36:33 +00:00
|
|
|
/* dirac in hex */
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2013-07-06 08:36:33 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("video/x-dirac");
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ST_PRIVATE_EA: /* Try to detect a VC1 stream */
|
|
|
|
{
|
2013-02-05 22:07:12 +00:00
|
|
|
gboolean is_vc1 = FALSE;
|
2013-07-06 08:36:33 +00:00
|
|
|
|
|
|
|
/* Note/FIXME: RP-227 specifies that the registration descriptor
|
|
|
|
* for vc1 can also contain other information, such as profile,
|
|
|
|
* level, alignment, buffer_size, .... */
|
|
|
|
if (bstream->registration_id == DRF_ID_VC1)
|
|
|
|
is_vc1 = TRUE;
|
2013-02-05 22:07:12 +00:00
|
|
|
if (!is_vc1) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (demux, "0xea private stream type found but "
|
|
|
|
"no descriptor for VC1. Assuming plain VC1.");
|
2013-02-05 22:07:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2013-02-05 22:07:12 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-wmv",
|
|
|
|
"wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-17 20:45:19 +00:00
|
|
|
case ST_PS_AUDIO_AC3:
|
2011-02-16 16:57:42 +00:00
|
|
|
/* DVB_ENHANCED_AC3 */
|
2013-07-06 08:36:33 +00:00
|
|
|
desc =
|
|
|
|
mpegts_get_descriptor_from_stream (bstream,
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_MTS_DESC_DVB_ENHANCED_AC3);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (desc) {
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-eac3");
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
/* If stream has ac3 descriptor
|
2013-07-29 06:23:30 +00:00
|
|
|
* OR program is ATSC (GA94)
|
|
|
|
* OR stream registration is AC-3
|
|
|
|
* then it's regular AC3 */
|
|
|
|
if (bstream->registration_id == DRF_ID_AC3 ||
|
|
|
|
program->registration_id == DRF_ID_GA94 ||
|
|
|
|
mpegts_get_descriptor_from_stream (bstream, GST_MTS_DESC_DVB_AC3)) {
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2013-07-29 06:23:30 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-ac3");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_WARNING_OBJECT (demux,
|
|
|
|
"AC3 stream type found but no guaranteed "
|
2013-07-29 06:23:30 +00:00
|
|
|
"way found to differentiate between AC3 and EAC3. "
|
|
|
|
"Assuming plain AC3.");
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-ac3");
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2016-08-29 02:40:54 +00:00
|
|
|
case ST_PS_AUDIO_EAC3:
|
2016-09-13 13:07:05 +00:00
|
|
|
{
|
2016-08-29 02:40:54 +00:00
|
|
|
/* ATSC_ENHANCED_AC3 */
|
2016-09-13 13:07:05 +00:00
|
|
|
if (bstream->registration_id == DRF_ID_EAC3 ||
|
|
|
|
mpegts_get_descriptor_from_stream (bstream, GST_MTS_DESC_ATSC_EAC3)) {
|
|
|
|
is_audio = TRUE;
|
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-eac3");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_ELEMENT_WARNING (demux, STREAM, DEMUX,
|
|
|
|
("Assuming ATSC E-AC3 audio stream."),
|
|
|
|
("ATSC E-AC3 stream type found but no guarantee way found to "
|
|
|
|
"differentiate among other standards (DVB, ISDB and etc..)"));
|
|
|
|
|
2016-08-29 02:40:54 +00:00
|
|
|
is_audio = TRUE;
|
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-eac3");
|
|
|
|
break;
|
2016-09-13 13:07:05 +00:00
|
|
|
}
|
2016-04-29 12:42:34 +00:00
|
|
|
case ST_PS_AUDIO_LPCM2:
|
|
|
|
is_audio = TRUE;
|
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-private2-lpcm");
|
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
case ST_PS_AUDIO_DTS:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-dts");
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
case ST_PS_AUDIO_LPCM:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_audio = TRUE;
|
2012-04-19 12:20:52 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("audio/x-lpcm");
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
case ST_PS_DVD_SUBPICTURE:
|
2015-06-04 16:36:32 +00:00
|
|
|
is_subpicture = TRUE;
|
2012-08-20 22:37:00 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
|
2014-08-02 10:29:37 +00:00
|
|
|
sparse = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2015-03-24 14:17:16 +00:00
|
|
|
case 0x42:
|
|
|
|
/* hack for Chinese AVS video stream which use 0x42 as stream_id
|
|
|
|
* NOTE: this is unofficial and within the ISO reserved range. */
|
2015-06-04 16:36:32 +00:00
|
|
|
is_video = TRUE;
|
2015-03-24 14:17:16 +00:00
|
|
|
caps = gst_caps_new_empty_simple ("video/x-cavs");
|
|
|
|
break;
|
2011-07-21 11:26:55 +00:00
|
|
|
default:
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Non-media stream (stream_type:0x%x). Not creating pad",
|
2011-07-21 11:26:55 +00:00
|
|
|
bstream->stream_type);
|
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2013-04-17 20:45:19 +00:00
|
|
|
|
|
|
|
done:
|
2015-06-04 16:36:32 +00:00
|
|
|
if (caps) {
|
|
|
|
if (is_audio) {
|
|
|
|
template = gst_static_pad_template_get (&audio_template);
|
2015-09-22 16:51:57 +00:00
|
|
|
name =
|
|
|
|
g_strdup_printf ("audio_%01x_%04x", demux->program_generation,
|
|
|
|
bstream->pid);
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_stream_set_stream_type (bstream->stream_object,
|
|
|
|
GST_STREAM_TYPE_AUDIO);
|
2015-06-04 16:36:32 +00:00
|
|
|
} else if (is_video) {
|
|
|
|
template = gst_static_pad_template_get (&video_template);
|
2015-09-22 16:51:57 +00:00
|
|
|
name =
|
|
|
|
g_strdup_printf ("video_%01x_%04x", demux->program_generation,
|
|
|
|
bstream->pid);
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_stream_set_stream_type (bstream->stream_object,
|
|
|
|
GST_STREAM_TYPE_VIDEO);
|
2015-06-04 16:36:32 +00:00
|
|
|
} else if (is_private) {
|
|
|
|
template = gst_static_pad_template_get (&private_template);
|
2015-09-22 16:51:57 +00:00
|
|
|
name =
|
|
|
|
g_strdup_printf ("private_%01x_%04x", demux->program_generation,
|
|
|
|
bstream->pid);
|
2015-06-04 16:36:32 +00:00
|
|
|
} else if (is_subpicture) {
|
|
|
|
template = gst_static_pad_template_get (&subpicture_template);
|
2015-09-22 16:51:57 +00:00
|
|
|
name =
|
|
|
|
g_strdup_printf ("subpicture_%01x_%04x", demux->program_generation,
|
|
|
|
bstream->pid);
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_stream_set_stream_type (bstream->stream_object, GST_STREAM_TYPE_TEXT);
|
2015-06-04 16:36:32 +00:00
|
|
|
} else
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
if (template && name && caps) {
|
2013-07-23 08:24:22 +00:00
|
|
|
GstEvent *event;
|
2015-07-29 13:52:05 +00:00
|
|
|
const gchar *stream_id;
|
2012-08-06 11:59:27 +00:00
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux,
|
|
|
|
"stream:%p creating pad with name %s and caps %" GST_PTR_FORMAT,
|
2013-04-23 18:19:14 +00:00
|
|
|
stream, name, caps);
|
2011-02-16 16:57:42 +00:00
|
|
|
pad = gst_pad_new_from_template (template, name);
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_pad_set_active (pad, TRUE);
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_pad_use_fixed_caps (pad);
|
2015-07-29 13:52:05 +00:00
|
|
|
stream_id = gst_stream_get_stream_id (bstream->stream_object);
|
2013-07-23 08:24:22 +00:00
|
|
|
|
|
|
|
event = gst_pad_get_sticky_event (base->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);
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_event_set_stream (event, bstream->stream_object);
|
2013-07-23 08:24:22 +00:00
|
|
|
if (demux->have_group_id)
|
|
|
|
gst_event_set_group_id (event, demux->group_id);
|
2015-07-29 13:52:05 +00:00
|
|
|
if (sparse) {
|
2014-08-02 10:29:37 +00:00
|
|
|
gst_event_set_stream_flags (event, GST_STREAM_FLAG_SPARSE);
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_stream_set_stream_flags (bstream->stream_object,
|
|
|
|
GST_STREAM_FLAG_SPARSE);
|
|
|
|
}
|
2014-08-02 10:29:37 +00:00
|
|
|
stream->sparse = sparse;
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_stream_set_caps (bstream->stream_object, caps);
|
2013-06-14 05:28:29 +00:00
|
|
|
if (!stream->taglist)
|
|
|
|
stream->taglist = gst_tag_list_new_empty ();
|
|
|
|
gst_pb_utils_add_codec_description_to_tag_list (stream->taglist, NULL,
|
|
|
|
caps);
|
2015-07-29 13:52:05 +00:00
|
|
|
gst_stream_set_tags (bstream->stream_object, stream->taglist);
|
2017-01-24 21:10:19 +00:00
|
|
|
|
|
|
|
gst_pad_push_event (pad, event);
|
|
|
|
gst_pad_set_caps (pad, caps);
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_pad_set_query_function (pad, gst_ts_demux_srcpad_query);
|
2011-02-22 11:33:56 +00:00
|
|
|
gst_pad_set_event_function (pad, gst_ts_demux_srcpad_event);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 23:23:17 +00:00
|
|
|
g_free (name);
|
2012-04-19 11:59:37 +00:00
|
|
|
if (template)
|
|
|
|
gst_object_unref (template);
|
2012-02-24 18:26:21 +00:00
|
|
|
if (caps)
|
|
|
|
gst_caps_unref (caps);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
return pad;
|
|
|
|
}
|
|
|
|
|
2015-07-29 13:52:05 +00:00
|
|
|
static gboolean
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_ts_demux_stream_added (MpegTSBase * base, MpegTSBaseStream * bstream,
|
|
|
|
MpegTSBaseProgram * program)
|
|
|
|
{
|
2014-05-27 13:21:45 +00:00
|
|
|
GstTSDemux *demux = (GstTSDemux *) base;
|
2011-02-16 16:57:42 +00:00
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) bstream;
|
|
|
|
|
|
|
|
if (!stream->pad) {
|
|
|
|
/* Create the pad */
|
2014-05-27 13:21:45 +00:00
|
|
|
if (bstream->stream_type != 0xff) {
|
2011-02-21 10:44:01 +00:00
|
|
|
stream->pad = create_pad_for_stream (base, bstream, program);
|
2014-05-27 13:21:45 +00:00
|
|
|
if (stream->pad)
|
|
|
|
gst_flow_combiner_add_pad (demux->flowcombiner, stream->pad);
|
|
|
|
}
|
2014-05-30 14:52:09 +00:00
|
|
|
|
2014-07-21 16:22:18 +00:00
|
|
|
if (base->mode != BASE_MODE_PUSHING
|
|
|
|
&& bstream->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_H264) {
|
2014-05-30 14:52:09 +00:00
|
|
|
stream->scan_function =
|
|
|
|
(GstTsDemuxKeyFrameScanFunction) scan_keyframe_h264;
|
|
|
|
} else {
|
|
|
|
stream->scan_function = NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-29 14:14:15 +00:00
|
|
|
stream->active = FALSE;
|
2012-01-10 10:37:13 +00:00
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
stream->need_newsegment = TRUE;
|
2016-03-08 10:41:49 +00:00
|
|
|
/* Reset segment if we're not doing an accurate seek */
|
2018-08-31 12:43:46 +00:00
|
|
|
demux->reset_segment =
|
|
|
|
(!(base->out_segment.flags & GST_SEEK_FLAG_ACCURATE));
|
2014-07-21 16:21:41 +00:00
|
|
|
stream->needs_keyframe = FALSE;
|
2014-03-28 17:11:21 +00:00
|
|
|
stream->discont = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
stream->pts = GST_CLOCK_TIME_NONE;
|
2012-01-10 10:37:13 +00:00
|
|
|
stream->dts = GST_CLOCK_TIME_NONE;
|
2015-06-10 21:05:52 +00:00
|
|
|
stream->first_pts = GST_CLOCK_TIME_NONE;
|
2013-07-29 06:10:07 +00:00
|
|
|
stream->raw_pts = -1;
|
|
|
|
stream->raw_dts = -1;
|
|
|
|
stream->pending_ts = TRUE;
|
2014-10-20 10:30:50 +00:00
|
|
|
stream->nb_out_buffers = 0;
|
|
|
|
stream->gap_ref_buffers = 0;
|
|
|
|
stream->gap_ref_pts = GST_CLOCK_TIME_NONE;
|
2015-05-09 13:52:22 +00:00
|
|
|
/* Only wait for a valid timestamp if we have a PCR_PID */
|
|
|
|
stream->pending_ts = program->pcr_pid < 0x1fff;
|
2013-04-11 11:08:57 +00:00
|
|
|
stream->continuity_counter = CONTINUITY_UNSET;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2015-07-29 13:52:05 +00:00
|
|
|
|
|
|
|
return (stream->pad != NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
static void
|
|
|
|
tsdemux_h264_parsing_info_clear (TSDemuxH264ParsingInfos * h264infos)
|
|
|
|
{
|
|
|
|
clear_simple_buffer (&h264infos->framedata);
|
|
|
|
|
|
|
|
if (h264infos->parser) {
|
|
|
|
gst_h264_nal_parser_free (h264infos->parser);
|
|
|
|
gst_byte_writer_free (h264infos->sps);
|
|
|
|
gst_byte_writer_free (h264infos->pps);
|
|
|
|
gst_byte_writer_free (h264infos->sei);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_stream_removed (MpegTSBase * base, MpegTSBaseStream * bstream)
|
|
|
|
{
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) bstream;
|
2011-07-21 11:26:55 +00:00
|
|
|
|
2011-08-01 08:54:16 +00:00
|
|
|
if (stream->pad) {
|
2014-05-27 13:21:45 +00:00
|
|
|
gst_flow_combiner_remove_pad (GST_TS_DEMUX_CAST (base)->flowcombiner,
|
|
|
|
stream->pad);
|
2014-08-19 03:01:28 +00:00
|
|
|
if (stream->active) {
|
|
|
|
|
|
|
|
if (gst_pad_is_active (stream->pad)) {
|
|
|
|
/* Flush out all data */
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Flushing out pending data");
|
2016-07-13 09:02:23 +00:00
|
|
|
gst_ts_demux_push_pending_data ((GstTSDemux *) base, stream, NULL);
|
2014-08-19 03:01:28 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Pushing out EOS");
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_eos ());
|
|
|
|
gst_pad_set_active (stream->pad, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Removing pad");
|
2011-08-01 08:54:16 +00:00
|
|
|
gst_element_remove_pad (GST_ELEMENT_CAST (base), stream->pad);
|
2012-02-29 14:14:15 +00:00
|
|
|
stream->active = FALSE;
|
2015-06-10 12:18:00 +00:00
|
|
|
} else {
|
|
|
|
gst_object_unref (stream->pad);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2011-08-01 08:54:16 +00:00
|
|
|
stream->pad = NULL;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2014-05-30 14:52:09 +00:00
|
|
|
|
2015-02-03 13:59:16 +00:00
|
|
|
gst_ts_demux_stream_flush (stream, GST_TS_DEMUX_CAST (base), TRUE);
|
2014-05-30 14:52:09 +00:00
|
|
|
|
2015-04-28 08:13:35 +00:00
|
|
|
if (stream->taglist != NULL) {
|
|
|
|
gst_tag_list_unref (stream->taglist);
|
|
|
|
stream->taglist = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
tsdemux_h264_parsing_info_clear (&stream->h264infos);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
activate_pad_for_stream (GstTSDemux * tsdemux, TSDemuxStream * stream)
|
|
|
|
{
|
|
|
|
if (stream->pad) {
|
|
|
|
GST_DEBUG_OBJECT (tsdemux, "Activating pad %s:%s for stream %p",
|
|
|
|
GST_DEBUG_PAD_NAME (stream->pad), stream);
|
|
|
|
gst_element_add_pad ((GstElement *) tsdemux, stream->pad);
|
2012-02-29 14:14:15 +00:00
|
|
|
stream->active = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "done adding pad");
|
2014-12-01 10:47:29 +00:00
|
|
|
} else if (((MpegTSBaseStream *) stream)->stream_type != 0xff) {
|
2015-09-15 16:20:38 +00:00
|
|
|
GST_DEBUG_OBJECT (tsdemux,
|
2014-12-01 10:47:29 +00:00
|
|
|
"stream %p (pid 0x%04x, type:0x%02x) has no pad", stream,
|
2011-07-21 11:26:55 +00:00
|
|
|
((MpegTSBaseStream *) stream)->pid,
|
|
|
|
((MpegTSBaseStream *) stream)->stream_type);
|
2014-10-20 10:30:50 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
static void
|
2015-02-03 13:59:16 +00:00
|
|
|
gst_ts_demux_stream_flush (TSDemuxStream * stream, GstTSDemux * tsdemux,
|
|
|
|
gboolean hard)
|
2011-02-22 11:33:56 +00:00
|
|
|
{
|
2012-05-21 15:53:37 +00:00
|
|
|
GST_DEBUG ("flushing stream %p", stream);
|
2012-03-30 17:19:12 +00:00
|
|
|
|
2015-11-17 23:23:17 +00:00
|
|
|
g_free (stream->data);
|
2012-05-21 15:53:37 +00:00
|
|
|
stream->data = NULL;
|
2022-03-04 13:17:47 +00:00
|
|
|
g_free (stream->pending_header_data);
|
|
|
|
stream->pending_header_data = NULL;
|
|
|
|
stream->pending_header_size = 0;
|
2012-04-30 10:28:42 +00:00
|
|
|
stream->state = PENDING_PACKET_EMPTY;
|
2012-03-05 08:38:57 +00:00
|
|
|
stream->expected_size = 0;
|
2012-05-21 15:53:37 +00:00
|
|
|
stream->allocated_size = 0;
|
2012-03-05 08:38:57 +00:00
|
|
|
stream->current_size = 0;
|
2014-03-28 17:11:21 +00:00
|
|
|
stream->discont = TRUE;
|
2012-03-01 17:05:17 +00:00
|
|
|
stream->pts = GST_CLOCK_TIME_NONE;
|
|
|
|
stream->dts = GST_CLOCK_TIME_NONE;
|
2013-07-29 06:10:07 +00:00
|
|
|
stream->raw_pts = -1;
|
|
|
|
stream->raw_dts = -1;
|
2014-05-23 05:07:19 +00:00
|
|
|
stream->pending_ts = TRUE;
|
2014-10-20 10:30:50 +00:00
|
|
|
stream->nb_out_buffers = 0;
|
|
|
|
stream->gap_ref_buffers = 0;
|
|
|
|
stream->gap_ref_pts = GST_CLOCK_TIME_NONE;
|
2013-04-11 11:08:57 +00:00
|
|
|
stream->continuity_counter = CONTINUITY_UNSET;
|
2015-11-10 15:32:37 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (stream->pending)) {
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
GST_DEBUG ("clearing pending %p", stream);
|
|
|
|
for (tmp = stream->pending; tmp; tmp = tmp->next) {
|
|
|
|
PendingBuffer *pend = (PendingBuffer *) tmp->data;
|
|
|
|
gst_buffer_unref (pend->buffer);
|
2023-01-08 18:46:03 +00:00
|
|
|
g_free (pend);
|
2015-11-10 15:32:37 +00:00
|
|
|
}
|
|
|
|
g_list_free (stream->pending);
|
|
|
|
stream->pending = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-03 13:59:16 +00:00
|
|
|
if (hard) {
|
2015-06-10 21:05:52 +00:00
|
|
|
stream->first_pts = GST_CLOCK_TIME_NONE;
|
2015-02-03 13:59:16 +00:00
|
|
|
stream->need_newsegment = TRUE;
|
|
|
|
}
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-03 13:59:16 +00:00
|
|
|
gst_ts_demux_flush_streams (GstTSDemux * demux, gboolean hard)
|
2011-02-22 11:33:56 +00:00
|
|
|
{
|
2015-02-03 13:59:16 +00:00
|
|
|
GList *walk;
|
2014-03-28 16:24:46 +00:00
|
|
|
if (!demux->program)
|
|
|
|
return;
|
|
|
|
|
2015-02-03 13:59:16 +00:00
|
|
|
for (walk = demux->program->stream_list; walk; walk = g_list_next (walk))
|
|
|
|
gst_ts_demux_stream_flush (walk->data, demux, hard);
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 12:55:05 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ts_demux_can_remove_program (MpegTSBase * base, MpegTSBaseProgram * program)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (base);
|
|
|
|
|
|
|
|
/* If it's our current active program, we return FALSE, we'll deactivate it
|
|
|
|
* ourselves when the next program gets activated */
|
|
|
|
if (demux->program == program) {
|
|
|
|
GST_DEBUG
|
|
|
|
("Attempting to remove current program, delaying until new program gets activated");
|
|
|
|
demux->previous_program = program;
|
|
|
|
demux->program_number = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:11:16 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_update_program (MpegTSBase * base, MpegTSBaseProgram * program)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (base);
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
GST_DEBUG ("Updating program %d", program->program_number);
|
|
|
|
/* Emit collection message */
|
|
|
|
gst_element_post_message ((GstElement *) base,
|
|
|
|
gst_message_new_stream_collection ((GstObject *) base,
|
|
|
|
program->collection));
|
|
|
|
|
|
|
|
/* Add all streams, then fire no-more-pads */
|
|
|
|
for (tmp = program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
if (!stream->pad) {
|
|
|
|
activate_pad_for_stream (demux, stream);
|
|
|
|
if (stream->sparse) {
|
|
|
|
/* force sending of pending sticky events which have been stored on the
|
|
|
|
* pad already and which otherwise would only be sent on the first buffer
|
|
|
|
* or serialized event (which means very late in case of subtitle streams),
|
|
|
|
* and playsink waits for stream-start or another serialized event */
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "sparse stream, pushing GAP event");
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_gap (0, 0));
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 13:45:51 +00:00
|
|
|
if (stream->pad)
|
|
|
|
gst_pad_push_event (stream->pad,
|
|
|
|
gst_event_new_stream_collection (program->collection));
|
2016-10-11 09:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 12:55:05 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_program_started (MpegTSBase * base, MpegTSBaseProgram * program)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (base);
|
|
|
|
|
2013-03-09 23:07:40 +00:00
|
|
|
GST_DEBUG ("Current program %d, new program %d requested program %d",
|
|
|
|
(gint) demux->program_number, program->program_number,
|
|
|
|
demux->requested_program_number);
|
2011-07-21 11:26:55 +00:00
|
|
|
|
2013-03-09 23:07:40 +00:00
|
|
|
if (demux->requested_program_number == program->program_number ||
|
|
|
|
(demux->requested_program_number == -1 && demux->program_number == -1)) {
|
2014-10-20 10:30:50 +00:00
|
|
|
GList *tmp;
|
2016-11-28 08:27:56 +00:00
|
|
|
gboolean have_pads = FALSE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
GST_LOG ("program %d started", program->program_number);
|
|
|
|
demux->program_number = program->program_number;
|
|
|
|
demux->program = program;
|
|
|
|
|
2015-09-22 16:51:57 +00:00
|
|
|
/* Increment the program_generation counter */
|
|
|
|
demux->program_generation = (demux->program_generation + 1) & 0xf;
|
|
|
|
|
2015-07-29 13:52:05 +00:00
|
|
|
/* Emit collection message */
|
|
|
|
gst_element_post_message ((GstElement *) base,
|
|
|
|
gst_message_new_stream_collection ((GstObject *) base,
|
|
|
|
program->collection));
|
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* If this is not the initial program, we need to calculate
|
2015-01-26 23:42:25 +00:00
|
|
|
* a new segment */
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
2021-04-05 07:29:37 +00:00
|
|
|
gst_event_replace (&demux->segment_event, NULL);
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2015-09-15 16:20:38 +00:00
|
|
|
/* DRAIN ALL STREAMS FIRST ! */
|
|
|
|
if (demux->previous_program) {
|
|
|
|
GList *tmp;
|
|
|
|
GST_DEBUG_OBJECT (demux, "Draining previous program");
|
|
|
|
for (tmp = demux->previous_program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
if (stream->pad)
|
2016-07-13 09:02:23 +00:00
|
|
|
gst_ts_demux_push_pending_data (demux, stream,
|
|
|
|
demux->previous_program);
|
2015-09-15 16:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
/* Add all streams, then fire no-more-pads */
|
|
|
|
for (tmp = program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
activate_pad_for_stream (demux, stream);
|
2016-11-28 08:27:56 +00:00
|
|
|
if (stream->pad)
|
|
|
|
have_pads = TRUE;
|
2014-10-20 10:30:50 +00:00
|
|
|
}
|
2015-10-20 15:22:23 +00:00
|
|
|
|
|
|
|
/* If there was a previous program, now is the time to deactivate it
|
|
|
|
* and remove old pads (including pushing EOS) */
|
2015-09-10 12:55:05 +00:00
|
|
|
if (demux->previous_program) {
|
|
|
|
GST_DEBUG ("Deactivating previous program");
|
|
|
|
mpegts_base_deactivate_and_free_program (base, demux->previous_program);
|
|
|
|
demux->previous_program = NULL;
|
|
|
|
}
|
2016-11-28 08:27:56 +00:00
|
|
|
|
|
|
|
if (!have_pads) {
|
|
|
|
/* If we had no pads, this stream is likely corrupted or unsupported and
|
|
|
|
* there's not much we can do at this point */
|
|
|
|
GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE,
|
|
|
|
("This stream contains no valid or supported streams."),
|
|
|
|
("activating program but got no pads"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-20 15:22:23 +00:00
|
|
|
/* If any of the stream is sparse, push a GAP event before anything else
|
|
|
|
* This is done here, and not in activate_pad_for_stream() because pushing
|
|
|
|
* a GAP event *is* considering data, and we want to ensure the (potential)
|
|
|
|
* old pads are all removed before we push any data on the new ones */
|
|
|
|
for (tmp = program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
if (stream->sparse) {
|
|
|
|
/* force sending of pending sticky events which have been stored on the
|
|
|
|
* pad already and which otherwise would only be sent on the first buffer
|
|
|
|
* or serialized event (which means very late in case of subtitle streams),
|
|
|
|
* and playsink waits for stream-start or another serialized event */
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "sparse stream, pushing GAP event");
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_gap (0, 0));
|
|
|
|
}
|
2022-11-02 13:45:51 +00:00
|
|
|
if (stream->pad)
|
|
|
|
gst_pad_push_event (stream->pad,
|
|
|
|
gst_event_new_stream_collection (program->collection));
|
2015-10-20 15:22:23 +00:00
|
|
|
}
|
2016-11-28 08:27:56 +00:00
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
gst_element_no_more_pads ((GstElement *) demux);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 13:26:20 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_program_stopped (MpegTSBase * base, MpegTSBaseProgram * program)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX (base);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-10-12 13:26:20 +00:00
|
|
|
if (demux->program == program) {
|
|
|
|
demux->program = NULL;
|
|
|
|
demux->program_number = -1;
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gst_ts_demux_record_pts (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
guint64 pts, guint64 offset)
|
|
|
|
{
|
|
|
|
MpegTSBaseStream *bs = (MpegTSBaseStream *) stream;
|
2021-04-22 23:15:08 +00:00
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (demux);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
stream->raw_pts = pts;
|
2012-07-13 14:56:11 +00:00
|
|
|
if (pts == -1) {
|
|
|
|
stream->pts = GST_CLOCK_TIME_NONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 13:39:30 +00:00
|
|
|
GST_LOG ("pid 0x%04x raw pts:%" G_GUINT64_FORMAT " at offset %"
|
2012-01-10 10:37:13 +00:00
|
|
|
G_GUINT64_FORMAT, bs->pid, pts, offset);
|
|
|
|
|
|
|
|
/* Compute PTS in GstClockTime */
|
2013-04-26 13:39:30 +00:00
|
|
|
stream->pts =
|
|
|
|
mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (pts), demux->program->pcr_pid);
|
2012-01-10 10:37:13 +00:00
|
|
|
|
2021-09-05 09:55:55 +00:00
|
|
|
if (base->out_segment.format == GST_FORMAT_TIME)
|
2021-04-22 23:15:08 +00:00
|
|
|
demux->mpeg_pts_offset =
|
|
|
|
(GSTTIME_TO_MPEGTIME (gst_segment_to_running_time (&base->out_segment,
|
|
|
|
GST_FORMAT_TIME, stream->pts)) - pts) & 0x1ffffffff;
|
2021-09-05 09:55:55 +00:00
|
|
|
|
|
|
|
/* Sanity check, some stream have completely different PTS vs DTS. If that
|
|
|
|
* happens we only keep the DTS */
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (stream->pts) &&
|
|
|
|
GST_CLOCK_TIME_IS_VALID (stream->dts) &&
|
|
|
|
ABSDIFF (stream->pts, stream->dts) > 5 * GST_SECOND) {
|
|
|
|
GST_WARNING ("pid 0x%04x PTS %" GST_TIME_FORMAT
|
|
|
|
" differs too much against DTS %" GST_TIME_FORMAT ", discarding it",
|
|
|
|
bs->pid, GST_TIME_ARGS (stream->pts), GST_TIME_ARGS (stream->dts));
|
|
|
|
stream->raw_pts = stream->raw_dts;
|
|
|
|
stream->pts = stream->dts;
|
|
|
|
return;
|
2021-04-22 23:15:08 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 13:39:30 +00:00
|
|
|
GST_LOG ("pid 0x%04x Stored PTS %" G_GUINT64_FORMAT, bs->pid, stream->pts);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (demux->emit_statistics)) {
|
|
|
|
GstStructure *st;
|
2024-08-09 07:41:57 +00:00
|
|
|
st = gst_structure_new_static_str ("tsdemux",
|
|
|
|
"pid", G_TYPE_UINT, bs->pid,
|
|
|
|
"offset", G_TYPE_UINT64, offset, "pts", G_TYPE_UINT64, pts, NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_element (GST_OBJECT (demux), st));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gst_ts_demux_record_dts (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
guint64 dts, guint64 offset)
|
|
|
|
{
|
|
|
|
MpegTSBaseStream *bs = (MpegTSBaseStream *) stream;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
stream->raw_dts = dts;
|
2012-07-13 14:56:11 +00:00
|
|
|
if (dts == -1) {
|
|
|
|
stream->dts = GST_CLOCK_TIME_NONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 13:39:30 +00:00
|
|
|
GST_LOG ("pid 0x%04x raw dts:%" G_GUINT64_FORMAT " at offset %"
|
2012-01-10 10:37:13 +00:00
|
|
|
G_GUINT64_FORMAT, bs->pid, dts, offset);
|
|
|
|
|
|
|
|
/* Compute DTS in GstClockTime */
|
2013-04-26 13:39:30 +00:00
|
|
|
stream->dts =
|
|
|
|
mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (dts), demux->program->pcr_pid);
|
2012-01-10 10:37:13 +00:00
|
|
|
|
2013-04-26 13:39:30 +00:00
|
|
|
GST_LOG ("pid 0x%04x Stored DTS %" G_GUINT64_FORMAT, bs->pid, stream->dts);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (demux->emit_statistics)) {
|
|
|
|
GstStructure *st;
|
2024-08-09 07:41:57 +00:00
|
|
|
st = gst_structure_new_static_str ("tsdemux",
|
|
|
|
"pid", G_TYPE_UINT, bs->pid,
|
|
|
|
"offset", G_TYPE_UINT64, offset, "dts", G_TYPE_UINT64, dts, NULL);
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (demux),
|
|
|
|
gst_message_new_element (GST_OBJECT (demux), st));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* This is called when we haven't got a valid initial PTS/DTS on all streams */
|
|
|
|
static gboolean
|
2014-06-04 11:39:48 +00:00
|
|
|
check_pending_buffers (GstTSDemux * demux)
|
2013-07-29 06:10:07 +00:00
|
|
|
{
|
|
|
|
gboolean have_observation = FALSE;
|
|
|
|
/* The biggest offset */
|
|
|
|
guint64 offset = 0;
|
|
|
|
GList *tmp;
|
2015-03-16 15:20:44 +00:00
|
|
|
gboolean have_only_sparse = TRUE;
|
2021-07-22 13:14:26 +00:00
|
|
|
gboolean exceeded_threshold = FALSE;
|
2015-03-16 15:20:44 +00:00
|
|
|
|
|
|
|
/* 0. Do we only have sparse stream */
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *tmpstream = (TSDemuxStream *) tmp->data;
|
|
|
|
|
|
|
|
if (!tmpstream->sparse) {
|
|
|
|
have_only_sparse = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 06:10:07 +00:00
|
|
|
|
|
|
|
/* 1. Go over all streams */
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *tmpstream = (TSDemuxStream *) tmp->data;
|
|
|
|
/* 1.1 check if at least one stream got a valid DTS */
|
2015-03-16 15:20:44 +00:00
|
|
|
if (have_only_sparse || !tmpstream->sparse) {
|
|
|
|
if ((tmpstream->raw_dts != -1 && tmpstream->dts != GST_CLOCK_TIME_NONE) ||
|
|
|
|
(tmpstream->raw_pts != -1 && tmpstream->pts != GST_CLOCK_TIME_NONE)) {
|
|
|
|
have_observation = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 13:14:26 +00:00
|
|
|
/* 1.2 Check if we exceeded the maximum threshold of pending data */
|
|
|
|
if (tmpstream->pending && (tmpstream->raw_dts != -1
|
|
|
|
|| tmpstream->raw_pts != -1)) {
|
|
|
|
PendingBuffer *pend = tmpstream->pending->data;
|
|
|
|
guint64 lastval =
|
|
|
|
tmpstream->raw_dts != -1 ? tmpstream->raw_dts : tmpstream->raw_pts;
|
|
|
|
guint64 firstval = pend->dts != -1 ? pend->dts : pend->pts;
|
|
|
|
GstClockTime dur;
|
|
|
|
g_assert (firstval != -1);
|
|
|
|
dur = MPEGTIME_TO_GSTTIME (lastval - firstval);
|
|
|
|
GST_DEBUG_OBJECT (tmpstream->pad,
|
|
|
|
"Pending content duration: %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
|
|
|
|
if (dur > 500 * GST_MSECOND) {
|
|
|
|
exceeded_threshold = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 06:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:14:26 +00:00
|
|
|
if (have_observation == FALSE) {
|
|
|
|
/* 2. If we don't have a valid value yet, break out */
|
|
|
|
if (!exceeded_threshold)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Except if we've exceed the maximum amount of pending buffers, in which
|
|
|
|
* case we ignore PCR from now on */
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Saw more than 500ms of data without PCR. Ignoring PCR from now on");
|
|
|
|
GST_MPEGTS_BASE (demux)->ignore_pcr = TRUE;
|
|
|
|
demux->program->pcr_pid = 0x1fff;
|
2021-07-26 14:43:47 +00:00
|
|
|
g_object_notify (G_OBJECT (demux), "ignore-pcr");
|
2021-07-22 13:14:26 +00:00
|
|
|
}
|
2013-07-29 06:10:07 +00:00
|
|
|
|
|
|
|
/* 3. Go over all streams that have current/pending data */
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *tmpstream = (TSDemuxStream *) tmp->data;
|
|
|
|
PendingBuffer *pend;
|
|
|
|
guint64 firstval, lastval, ts;
|
|
|
|
|
|
|
|
/* 3.1 Calculate the offset between current DTS and first DTS */
|
|
|
|
if (tmpstream->pending == NULL || tmpstream->state == PENDING_PACKET_EMPTY)
|
|
|
|
continue;
|
|
|
|
/* If we don't have any pending data, the offset is 0 for this stream */
|
|
|
|
if (tmpstream->pending == NULL)
|
|
|
|
break;
|
|
|
|
if (tmpstream->raw_dts != -1)
|
|
|
|
lastval = tmpstream->raw_dts;
|
|
|
|
else if (tmpstream->raw_pts != -1)
|
|
|
|
lastval = tmpstream->raw_pts;
|
|
|
|
else {
|
|
|
|
GST_WARNING ("Don't have a last DTS/PTS to use for offset recalculation");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pend = tmpstream->pending->data;
|
|
|
|
if (pend->dts != -1)
|
|
|
|
firstval = pend->dts;
|
|
|
|
else if (pend->pts != -1)
|
|
|
|
firstval = pend->pts;
|
|
|
|
else {
|
|
|
|
GST_WARNING
|
|
|
|
("Don't have a first DTS/PTS to use for offset recalculation");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* 3.2 Add to the offset the report TS for the current DTS */
|
|
|
|
ts = mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (lastval), demux->program->pcr_pid);
|
|
|
|
if (ts == GST_CLOCK_TIME_NONE) {
|
|
|
|
GST_WARNING ("THIS SHOULD NOT HAPPEN !");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ts += MPEGTIME_TO_GSTTIME (lastval - firstval);
|
|
|
|
/* 3.3 If that offset is bigger than the current offset, store it */
|
|
|
|
if (ts > offset)
|
|
|
|
offset = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG ("New initial pcr_offset %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (offset));
|
|
|
|
|
|
|
|
/* 4. Set the offset on the packetizer */
|
|
|
|
mpegts_packetizer_set_current_pcr_offset (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
offset, demux->program->pcr_pid);
|
|
|
|
|
|
|
|
/* 4. Go over all streams */
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
|
|
|
|
stream->pending_ts = FALSE;
|
|
|
|
/* 4.1 Set pending_ts for FALSE */
|
|
|
|
|
|
|
|
/* 4.2 Recalculate PTS/DTS (in running time) for pending data */
|
|
|
|
if (stream->pending) {
|
|
|
|
GList *tmp2;
|
|
|
|
for (tmp2 = stream->pending; tmp2; tmp2 = tmp2->next) {
|
|
|
|
PendingBuffer *pend = (PendingBuffer *) tmp2->data;
|
|
|
|
if (pend->pts != -1)
|
|
|
|
GST_BUFFER_PTS (pend->buffer) =
|
|
|
|
mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (pend->pts), demux->program->pcr_pid);
|
|
|
|
if (pend->dts != -1)
|
|
|
|
GST_BUFFER_DTS (pend->buffer) =
|
|
|
|
mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (pend->dts), demux->program->pcr_pid);
|
2015-06-10 21:05:52 +00:00
|
|
|
/* 4.2.2 Set first_pts to TS of lowest PTS (for segment) */
|
|
|
|
if (stream->first_pts == GST_CLOCK_TIME_NONE) {
|
|
|
|
if (GST_BUFFER_PTS (pend->buffer) != GST_CLOCK_TIME_NONE)
|
|
|
|
stream->first_pts = GST_BUFFER_PTS (pend->buffer);
|
|
|
|
else if (GST_BUFFER_DTS (pend->buffer) != GST_CLOCK_TIME_NONE)
|
|
|
|
stream->first_pts = GST_BUFFER_DTS (pend->buffer);
|
2013-07-29 06:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Recalculate PTS/DTS (in running time) for current data */
|
|
|
|
if (stream->state != PENDING_PACKET_EMPTY) {
|
|
|
|
if (stream->raw_pts != -1) {
|
|
|
|
stream->pts =
|
|
|
|
mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (stream->raw_pts), demux->program->pcr_pid);
|
2015-06-10 21:05:52 +00:00
|
|
|
if (stream->first_pts == GST_CLOCK_TIME_NONE)
|
|
|
|
stream->first_pts = stream->pts;
|
|
|
|
}
|
|
|
|
if (stream->raw_dts != -1) {
|
|
|
|
stream->dts =
|
|
|
|
mpegts_packetizer_pts_to_ts (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
MPEGTIME_TO_GSTTIME (stream->raw_dts), demux->program->pcr_pid);
|
|
|
|
if (stream->first_pts == GST_CLOCK_TIME_NONE)
|
|
|
|
stream->first_pts = stream->dts;
|
2013-07-29 06:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
static void
|
|
|
|
gst_ts_demux_parse_pes_header (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
guint8 * data, guint32 length, guint64 bufferoffset)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2011-07-18 07:12:16 +00:00
|
|
|
PESHeader header;
|
|
|
|
PESParsingResult parseres;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
GST_MEMDUMP ("Header buffer", data, MIN (length, 32));
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2022-03-04 13:17:47 +00:00
|
|
|
if (G_UNLIKELY (stream->pending_header_data)) {
|
|
|
|
/* Accumulate with previous header if present */
|
|
|
|
stream->pending_header_data =
|
|
|
|
g_realloc (stream->pending_header_data,
|
|
|
|
stream->pending_header_size + length);
|
|
|
|
memcpy (stream->pending_header_data + stream->pending_header_size, data,
|
|
|
|
length);
|
|
|
|
data = stream->pending_header_data;
|
|
|
|
length = stream->pending_header_size + length;
|
|
|
|
}
|
|
|
|
|
2013-08-14 08:33:14 +00:00
|
|
|
parseres = mpegts_parse_pes_header (data, length, &header);
|
2022-03-04 13:17:47 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (parseres == PES_PARSING_NEED_MORE)) {
|
|
|
|
/* This can happen if PES header is bigger than a packet. */
|
|
|
|
if (!stream->pending_header_data)
|
|
|
|
stream->pending_header_data = g_memdup2 (data, length);
|
|
|
|
stream->pending_header_size = length;
|
|
|
|
return;
|
|
|
|
}
|
2011-07-18 07:12:16 +00:00
|
|
|
if (G_UNLIKELY (parseres == PES_PARSING_BAD)) {
|
|
|
|
GST_WARNING ("Error parsing PES header. pid: 0x%x stream_type: 0x%x",
|
2011-02-21 10:44:01 +00:00
|
|
|
stream->stream.pid, stream->stream.stream_type);
|
2011-02-16 16:57:42 +00:00
|
|
|
goto discont;
|
|
|
|
}
|
2011-02-22 11:33:56 +00:00
|
|
|
|
2014-03-03 12:07:47 +00:00
|
|
|
if (stream->target_pes_substream != 0
|
|
|
|
&& header.stream_id_extension != stream->target_pes_substream) {
|
|
|
|
GST_DEBUG ("Skipping unwanted substream");
|
|
|
|
goto discont;
|
|
|
|
}
|
|
|
|
|
2012-07-13 14:56:11 +00:00
|
|
|
gst_ts_demux_record_dts (demux, stream, header.DTS, bufferoffset);
|
|
|
|
gst_ts_demux_record_pts (demux, stream, header.PTS, bufferoffset);
|
2013-07-29 06:10:07 +00:00
|
|
|
if (G_UNLIKELY (stream->pending_ts &&
|
|
|
|
(stream->pts != GST_CLOCK_TIME_NONE
|
|
|
|
|| stream->dts != GST_CLOCK_TIME_NONE))) {
|
|
|
|
GST_DEBUG ("Got pts/dts update, rechecking all streams");
|
2014-06-04 11:39:48 +00:00
|
|
|
check_pending_buffers (demux);
|
2015-06-10 21:05:52 +00:00
|
|
|
} else if (stream->first_pts == GST_CLOCK_TIME_NONE) {
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (stream->pts))
|
|
|
|
stream->first_pts = stream->pts;
|
|
|
|
else if (GST_CLOCK_TIME_IS_VALID (stream->dts))
|
|
|
|
stream->first_pts = stream->dts;
|
2013-07-29 06:10:07 +00:00
|
|
|
}
|
2011-07-18 07:12:16 +00:00
|
|
|
|
2013-06-05 13:51:58 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
2012-07-13 14:56:11 +00:00
|
|
|
"stream PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (stream->pts), GST_TIME_ARGS (stream->dts));
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
/* Remove PES headers */
|
2012-05-21 15:53:37 +00:00
|
|
|
GST_DEBUG ("Moving data forward by %d bytes (packet_size:%d, have:%d)",
|
|
|
|
header.header_size, header.packet_length, length);
|
2012-07-12 08:13:50 +00:00
|
|
|
stream->expected_size = header.packet_length;
|
|
|
|
if (stream->expected_size) {
|
|
|
|
if (G_LIKELY (stream->expected_size > header.header_size)) {
|
|
|
|
stream->expected_size -= header.header_size;
|
|
|
|
} else {
|
|
|
|
/* next packet will have to complete this one */
|
2014-06-30 12:20:12 +00:00
|
|
|
GST_WARNING ("invalid header and packet size combination, empty packet");
|
2012-07-12 08:13:50 +00:00
|
|
|
stream->expected_size = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-05-21 15:53:37 +00:00
|
|
|
data += header.header_size;
|
|
|
|
length -= header.header_size;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
/* Create the output buffer */
|
|
|
|
if (stream->expected_size)
|
2013-09-16 09:42:48 +00:00
|
|
|
stream->allocated_size = MAX (stream->expected_size, length);
|
2012-05-21 15:53:37 +00:00
|
|
|
else
|
2013-09-16 09:42:48 +00:00
|
|
|
stream->allocated_size = MAX (8192, length);
|
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
g_assert (stream->data == NULL);
|
|
|
|
stream->data = g_malloc (stream->allocated_size);
|
|
|
|
memcpy (stream->data, data, length);
|
|
|
|
stream->current_size = length;
|
|
|
|
|
|
|
|
stream->state = PENDING_PACKET_BUFFER;
|
|
|
|
|
2022-03-04 13:17:47 +00:00
|
|
|
if (stream->pending_header_data) {
|
|
|
|
g_free (stream->pending_header_data);
|
|
|
|
stream->pending_header_data = NULL;
|
|
|
|
stream->pending_header_size = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
return;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
discont:
|
2022-03-04 13:17:47 +00:00
|
|
|
if (stream->pending_header_data) {
|
|
|
|
g_free (stream->pending_header_data);
|
|
|
|
stream->pending_header_data = NULL;
|
|
|
|
stream->pending_header_size = 0;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
stream->state = PENDING_PACKET_DISCONT;
|
2012-05-21 15:53:37 +00:00
|
|
|
return;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ONLY CALL THIS:
|
|
|
|
* * WITH packet->payload != NULL
|
|
|
|
* * WITH pending/current flushed out if beginning of new PES packet
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
gst_ts_demux_queue_data (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
MpegTSPacketizerPacket * packet)
|
|
|
|
{
|
2012-05-21 15:53:37 +00:00
|
|
|
guint8 *data;
|
|
|
|
guint size;
|
2013-06-23 06:43:23 +00:00
|
|
|
guint8 cc = FLAGS_CONTINUITY_COUNTER (packet->scram_afc_cc);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "pid: 0x%04x state:%d", stream->stream.pid,
|
|
|
|
stream->state);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
size = packet->data_end - packet->payload;
|
|
|
|
data = packet->payload;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-04-11 11:08:57 +00:00
|
|
|
if (stream->continuity_counter == CONTINUITY_UNSET) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "CONTINUITY: Initialize to %d", cc);
|
2013-06-23 06:43:23 +00:00
|
|
|
} else if ((cc == stream->continuity_counter + 1 ||
|
|
|
|
(stream->continuity_counter == MAX_CONTINUITY && cc == 0))) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "CONTINUITY: Got expected %d", cc);
|
2013-04-11 11:08:57 +00:00
|
|
|
} else {
|
2019-11-06 13:22:07 +00:00
|
|
|
if (stream->state != PENDING_PACKET_EMPTY) {
|
|
|
|
if (packet->payload_unit_start_indicator) {
|
|
|
|
/* A mismatch is fatal, except if this is the beginning of a new
|
|
|
|
* frame (from which we can recover) */
|
|
|
|
if (G_UNLIKELY (stream->data)) {
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
}
|
2022-03-04 13:17:47 +00:00
|
|
|
if (G_UNLIKELY (stream->pending_header_data)) {
|
|
|
|
g_free (stream->pending_header_data);
|
|
|
|
stream->pending_header_data = NULL;
|
|
|
|
}
|
2019-11-06 13:22:07 +00:00
|
|
|
stream->state = PENDING_PACKET_HEADER;
|
|
|
|
} else {
|
2021-10-20 10:02:31 +00:00
|
|
|
gchar *pad_name = gst_pad_get_name (stream->pad);
|
2021-10-08 09:36:58 +00:00
|
|
|
GST_ELEMENT_WARNING_WITH_DETAILS (demux, STREAM, DEMUX,
|
|
|
|
("CONTINUITY: Mismatch packet %d, stream %d (pid 0x%04x)", cc,
|
|
|
|
stream->continuity_counter, stream->stream.pid), (NULL),
|
|
|
|
("warning-type", G_TYPE_STRING, "continuity-mismatch",
|
|
|
|
"packet", G_TYPE_INT, cc,
|
|
|
|
"stream", G_TYPE_INT, stream->continuity_counter,
|
2021-10-20 10:02:31 +00:00
|
|
|
"pid", G_TYPE_UINT, stream->stream.pid,
|
|
|
|
"pad-name", G_TYPE_STRING, pad_name, NULL));
|
|
|
|
g_free (pad_name);
|
2019-11-06 13:22:07 +00:00
|
|
|
stream->state = PENDING_PACKET_DISCONT;
|
|
|
|
}
|
|
|
|
}
|
2013-04-11 11:08:57 +00:00
|
|
|
}
|
2013-06-23 06:43:23 +00:00
|
|
|
stream->continuity_counter = cc;
|
2013-04-11 11:08:57 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
if (stream->state == PENDING_PACKET_EMPTY) {
|
|
|
|
if (G_UNLIKELY (!packet->payload_unit_start_indicator)) {
|
|
|
|
stream->state = PENDING_PACKET_DISCONT;
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Didn't get the first packet of this PES");
|
2011-02-16 16:57:42 +00:00
|
|
|
} else {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "EMPTY=>HEADER");
|
2011-02-16 16:57:42 +00:00
|
|
|
stream->state = PENDING_PACKET_HEADER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 17:19:12 +00:00
|
|
|
switch (stream->state) {
|
|
|
|
case PENDING_PACKET_HEADER:
|
|
|
|
{
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "HEADER: Parsing PES header");
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-03-30 17:19:12 +00:00
|
|
|
/* parse the header */
|
2012-05-21 15:53:37 +00:00
|
|
|
gst_ts_demux_parse_pes_header (demux, stream, data, size, packet->offset);
|
2012-03-30 17:19:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PENDING_PACKET_BUFFER:
|
|
|
|
{
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "BUFFER: appending data");
|
2012-05-21 15:53:37 +00:00
|
|
|
if (G_UNLIKELY (stream->current_size + size > stream->allocated_size)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "resizing buffer");
|
2013-09-16 09:42:48 +00:00
|
|
|
do {
|
2019-06-28 05:04:29 +00:00
|
|
|
stream->allocated_size = MAX (8192, 2 * stream->allocated_size);
|
2013-09-16 09:42:48 +00:00
|
|
|
} while (stream->current_size + size > stream->allocated_size);
|
2012-05-21 15:53:37 +00:00
|
|
|
stream->data = g_realloc (stream->data, stream->allocated_size);
|
|
|
|
}
|
|
|
|
memcpy (stream->data + stream->current_size, data, size);
|
|
|
|
stream->current_size += size;
|
2012-03-30 17:19:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PENDING_PACKET_DISCONT:
|
|
|
|
{
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "DISCONT: not storing/pushing");
|
2012-05-21 15:53:37 +00:00
|
|
|
if (G_UNLIKELY (stream->data)) {
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
}
|
2022-03-04 13:17:47 +00:00
|
|
|
if (G_UNLIKELY (stream->pending_header_data)) {
|
|
|
|
g_free (stream->pending_header_data);
|
|
|
|
stream->pending_header_data = NULL;
|
|
|
|
}
|
2013-04-11 11:08:57 +00:00
|
|
|
stream->continuity_counter = CONTINUITY_UNSET;
|
2012-03-30 17:19:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-16 11:44:00 +00:00
|
|
|
static void
|
2016-07-13 09:02:23 +00:00
|
|
|
calculate_and_push_newsegment (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
MpegTSBaseProgram * target_program)
|
2011-07-16 11:44:00 +00:00
|
|
|
{
|
|
|
|
MpegTSBase *base = (MpegTSBase *) demux;
|
2012-03-01 17:05:17 +00:00
|
|
|
GstClockTime lowest_pts = GST_CLOCK_TIME_NONE;
|
2012-03-02 16:11:33 +00:00
|
|
|
GstClockTime firstts = 0;
|
2011-08-16 22:05:14 +00:00
|
|
|
GList *tmp;
|
2011-07-16 11:44:00 +00:00
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Creating new newsegment for stream %p", stream);
|
2011-07-16 11:44:00 +00:00
|
|
|
|
2016-07-13 09:02:23 +00:00
|
|
|
if (target_program == NULL)
|
|
|
|
target_program = demux->program;
|
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* Speedup : if we don't need to calculate anything, go straight to pushing */
|
2021-04-05 07:29:37 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
|
|
|
if (demux->segment_event) {
|
|
|
|
g_mutex_unlock (&demux->lock);
|
2012-03-01 17:05:17 +00:00
|
|
|
goto push_new_segment;
|
2021-04-05 07:29:37 +00:00
|
|
|
}
|
|
|
|
g_mutex_unlock (&demux->lock);
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2015-01-26 23:42:25 +00:00
|
|
|
/* Calculate the 'new_start' value, used for newsegment */
|
2016-07-13 09:02:23 +00:00
|
|
|
for (tmp = target_program->stream_list; tmp; tmp = tmp->next) {
|
2011-08-16 22:05:14 +00:00
|
|
|
TSDemuxStream *pstream = (TSDemuxStream *) tmp->data;
|
|
|
|
|
2015-06-10 21:05:52 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (pstream->first_pts)) {
|
2013-07-29 06:10:07 +00:00
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (lowest_pts)
|
2015-06-10 21:05:52 +00:00
|
|
|
|| pstream->first_pts < lowest_pts)
|
|
|
|
lowest_pts = pstream->first_pts;
|
2011-11-08 19:19:41 +00:00
|
|
|
}
|
2012-03-01 17:05:17 +00:00
|
|
|
}
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (lowest_pts))
|
2013-04-26 13:39:30 +00:00
|
|
|
firstts = lowest_pts;
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "lowest_pts %" G_GUINT64_FORMAT " => clocktime %"
|
|
|
|
GST_TIME_FORMAT, lowest_pts, GST_TIME_ARGS (firstts));
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2018-08-31 12:43:46 +00:00
|
|
|
if (base->out_segment.format != GST_FORMAT_TIME || demux->reset_segment) {
|
2013-05-28 14:14:42 +00:00
|
|
|
/* It will happen only if it's first program or after flushes. */
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux, "Calculating actual segment");
|
2013-05-28 14:14:42 +00:00
|
|
|
if (base->segment.format == GST_FORMAT_TIME) {
|
|
|
|
/* Try to recover segment info from base if it's in TIME format */
|
2018-08-31 12:43:46 +00:00
|
|
|
base->out_segment = base->segment;
|
2013-05-28 14:14:42 +00:00
|
|
|
} else {
|
2012-04-19 12:20:52 +00:00
|
|
|
/* Start from the first ts/pts */
|
2018-08-31 12:43:46 +00:00
|
|
|
GstSegment *seg = &base->out_segment;
|
2018-09-19 15:05:52 +00:00
|
|
|
GstClockTime base =
|
|
|
|
seg->base + seg->position - (seg->start + seg->offset);
|
2019-01-29 20:57:44 +00:00
|
|
|
GstClockTime stop = seg->stop;
|
|
|
|
|
2018-08-31 12:43:46 +00:00
|
|
|
gst_segment_init (seg, GST_FORMAT_TIME);
|
|
|
|
seg->start = firstts;
|
2019-01-29 20:57:44 +00:00
|
|
|
seg->stop = MAX (seg->start, stop);
|
2018-08-31 12:43:46 +00:00
|
|
|
seg->position = firstts;
|
|
|
|
seg->time = firstts;
|
|
|
|
seg->rate = demux->rate;
|
|
|
|
seg->base = base;
|
2012-04-05 04:18:28 +00:00
|
|
|
}
|
2018-08-31 12:43:46 +00:00
|
|
|
} else if (base->out_segment.start < firstts) {
|
2014-05-30 14:52:09 +00:00
|
|
|
/* Take into account the offset to the first buffer timestamp */
|
2018-08-31 12:43:46 +00:00
|
|
|
if (base->out_segment.rate > 0) {
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (base->out_segment.stop))
|
|
|
|
base->out_segment.stop += firstts - base->out_segment.start;
|
2021-04-21 19:43:59 +00:00
|
|
|
base->out_segment.start = firstts;
|
2018-08-31 12:43:46 +00:00
|
|
|
base->out_segment.position = firstts;
|
2014-08-05 15:13:13 +00:00
|
|
|
}
|
2012-03-01 17:05:17 +00:00
|
|
|
}
|
2011-07-16 11:44:00 +00:00
|
|
|
|
2018-09-19 15:05:52 +00:00
|
|
|
GST_LOG_OBJECT (demux, "Output segment now %" GST_SEGMENT_FORMAT,
|
|
|
|
&base->out_segment);
|
|
|
|
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
2013-07-15 09:15:11 +00:00
|
|
|
if (!demux->segment_event) {
|
2021-04-05 07:29:37 +00:00
|
|
|
gst_event_take (&demux->segment_event,
|
|
|
|
gst_event_new_segment (&base->out_segment));
|
2018-06-19 03:06:18 +00:00
|
|
|
|
|
|
|
if (base->last_seek_seqnum != GST_SEQNUM_INVALID)
|
|
|
|
gst_event_set_seqnum (demux->segment_event, base->last_seek_seqnum);
|
2013-07-15 09:15:11 +00:00
|
|
|
}
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
2013-05-28 14:14:42 +00:00
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
push_new_segment:
|
2016-07-13 09:02:23 +00:00
|
|
|
for (tmp = target_program->stream_list; tmp; tmp = tmp->next) {
|
2014-10-20 10:30:50 +00:00
|
|
|
stream = (TSDemuxStream *) tmp->data;
|
|
|
|
if (stream->pad == NULL)
|
|
|
|
continue;
|
2011-07-16 11:44:00 +00:00
|
|
|
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
2014-10-20 10:30:50 +00:00
|
|
|
if (demux->segment_event) {
|
2021-04-05 07:29:37 +00:00
|
|
|
GstEvent *evt = gst_event_ref (demux->segment_event);
|
2014-10-20 10:30:50 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Pushing newsegment event");
|
2020-03-20 11:27:05 +00:00
|
|
|
|
2021-04-05 07:29:37 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
|
|
|
gst_pad_push_event (stream->pad, evt);
|
|
|
|
} else {
|
|
|
|
g_mutex_unlock (&demux->lock);
|
2014-10-20 10:30:50 +00:00
|
|
|
}
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
if (demux->global_tags) {
|
|
|
|
gst_pad_push_event (stream->pad,
|
|
|
|
gst_event_new_tag (gst_tag_list_ref (demux->global_tags)));
|
|
|
|
}
|
2014-02-25 01:43:56 +00:00
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
/* Push pending tags */
|
|
|
|
if (stream->taglist) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Sending tags %" GST_PTR_FORMAT,
|
|
|
|
stream->taglist);
|
|
|
|
gst_pad_push_event (stream->pad, gst_event_new_tag (stream->taglist));
|
|
|
|
stream->taglist = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->need_newsegment = FALSE;
|
2012-04-10 10:36:15 +00:00
|
|
|
}
|
2021-04-05 07:29:37 +00:00
|
|
|
if (base->seek_event) {
|
|
|
|
g_assert (base->out_segment.format != GST_FORMAT_UNDEFINED);
|
|
|
|
gst_ts_demux_do_seek (base, base->seek_event);
|
|
|
|
gst_event_replace (&base->seek_event, NULL);
|
|
|
|
}
|
2014-10-20 10:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ts_demux_check_and_sync_streams (GstTSDemux * demux, GstClockTime time)
|
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Recheck streams and sync to at least: %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (time));
|
2012-04-10 10:36:15 +00:00
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
if (G_UNLIKELY (demux->program == NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Go over each stream and update it to at least 'time' time.
|
|
|
|
* For each stream, the pad stores the buffer counter the last time
|
|
|
|
* a gap check occurred (gap_ref_buffers) and a gap_ref_pts timestamp
|
|
|
|
* that is either the PTS from the stream or the PCR the pad was updated
|
|
|
|
* to.
|
|
|
|
*
|
|
|
|
* We can check nb_out_buffers to see if any buffers were pushed since then.
|
|
|
|
* This means we can detect buffers passing without PTSes fine and still generate
|
|
|
|
* gaps.
|
|
|
|
*
|
2021-10-01 12:36:48 +00:00
|
|
|
* If there haven't been any buffers pushed on this stream since the last gap
|
|
|
|
* check *AND* there is no pending data (stream->current_size), push a gap
|
|
|
|
* event updating to the indicated input PCR time and update the pad's
|
|
|
|
* tracking.
|
2014-10-20 10:30:50 +00:00
|
|
|
*
|
|
|
|
* If there have been buffers pushed, update the reference buffer count
|
|
|
|
* and but don't push a gap event
|
|
|
|
*/
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *ps = (TSDemuxStream *) tmp->data;
|
|
|
|
GST_DEBUG_OBJECT (ps->pad,
|
|
|
|
"0x%04x, PTS:%" GST_TIME_FORMAT " REFPTS:%" GST_TIME_FORMAT " Gap:%"
|
2021-10-01 12:36:48 +00:00
|
|
|
GST_TIME_FORMAT " nb_buffers: %d (ref:%d) pending_data size %u",
|
2014-10-20 10:30:50 +00:00
|
|
|
((MpegTSBaseStream *) ps)->pid, GST_TIME_ARGS (ps->pts),
|
|
|
|
GST_TIME_ARGS (ps->gap_ref_pts),
|
|
|
|
GST_TIME_ARGS (ps->pts - ps->gap_ref_pts), ps->nb_out_buffers,
|
2021-10-01 12:36:48 +00:00
|
|
|
ps->gap_ref_buffers, ps->current_size);
|
2014-10-20 10:30:50 +00:00
|
|
|
if (ps->pad == NULL)
|
|
|
|
continue;
|
|
|
|
|
2021-10-01 12:36:48 +00:00
|
|
|
if (ps->nb_out_buffers == ps->gap_ref_buffers && ps->current_size == 0
|
|
|
|
&& ps->gap_ref_pts != ps->pts) {
|
2014-10-20 10:30:50 +00:00
|
|
|
/* Do initial setup of pad if needed - segment etc */
|
|
|
|
GST_DEBUG_OBJECT (ps->pad,
|
|
|
|
"Stream needs update. Pushing GAP event to TS %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (time));
|
|
|
|
if (G_UNLIKELY (ps->need_newsegment))
|
2016-07-13 09:02:23 +00:00
|
|
|
calculate_and_push_newsegment (demux, ps, NULL);
|
2014-10-20 10:30:50 +00:00
|
|
|
|
|
|
|
/* Now send gap event */
|
|
|
|
gst_pad_push_event (ps->pad, gst_event_new_gap (time, 0));
|
2024-01-30 18:16:39 +00:00
|
|
|
|
|
|
|
/* And do an EOS check */
|
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (demux);
|
|
|
|
if (base->out_segment.stop != -1 && time >= base->out_segment.stop) {
|
|
|
|
gst_flow_combiner_update_pad_flow (demux->flowcombiner, ps->pad,
|
|
|
|
GST_FLOW_EOS);
|
|
|
|
}
|
2014-10-20 10:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update GAP tracking vars so we don't re-check this stream for a while */
|
|
|
|
ps->gap_ref_pts = time;
|
|
|
|
if (ps->pts != GST_CLOCK_TIME_NONE && ps->pts > time)
|
|
|
|
ps->gap_ref_pts = ps->pts;
|
|
|
|
ps->gap_ref_buffers = ps->nb_out_buffers;
|
|
|
|
}
|
2011-07-16 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 11:27:43 +00:00
|
|
|
static GstBufferList *
|
|
|
|
parse_opus_access_unit (TSDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstByteReader reader;
|
|
|
|
GstBufferList *buffer_list = NULL;
|
|
|
|
|
|
|
|
buffer_list = gst_buffer_list_new ();
|
|
|
|
gst_byte_reader_init (&reader, stream->data, stream->current_size);
|
|
|
|
|
|
|
|
do {
|
|
|
|
GstBuffer *buffer;
|
|
|
|
guint16 id;
|
|
|
|
guint au_size = 0;
|
|
|
|
guint8 b;
|
|
|
|
gboolean start_trim_flag, end_trim_flag, control_extension_flag;
|
|
|
|
guint16 start_trim = 0, end_trim = 0;
|
|
|
|
guint8 *packet_data;
|
|
|
|
guint packet_size;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint16_be (&reader, &id))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* No control header */
|
|
|
|
if ((id >> 5) != 0x3ff)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!gst_byte_reader_get_uint8 (&reader, &b))
|
|
|
|
goto error;
|
|
|
|
au_size += b;
|
|
|
|
} while (b == 0xff);
|
|
|
|
|
|
|
|
start_trim_flag = (id >> 4) & 0x1;
|
|
|
|
end_trim_flag = (id >> 3) & 0x1;
|
|
|
|
control_extension_flag = (id >> 2) & 0x1;
|
|
|
|
|
|
|
|
if (start_trim_flag) {
|
|
|
|
if (!gst_byte_reader_get_uint16_be (&reader, &start_trim))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_trim_flag) {
|
|
|
|
if (!gst_byte_reader_get_uint16_be (&reader, &end_trim))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (control_extension_flag) {
|
|
|
|
if (!gst_byte_reader_get_uint8 (&reader, &b))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_skip (&reader, b))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_size = au_size;
|
|
|
|
|
|
|
|
/* FIXME: this should be
|
|
|
|
* packet_size = au_size - gst_byte_reader_get_pos (&reader);
|
|
|
|
* but ffmpeg and the only available sample stream from obe.tv
|
|
|
|
* are not including the control header size in au_size
|
|
|
|
*/
|
|
|
|
if (gst_byte_reader_get_remaining (&reader) < packet_size)
|
|
|
|
goto error;
|
|
|
|
if (!gst_byte_reader_dup_data (&reader, packet_size, &packet_data))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
buffer = gst_buffer_new_wrapped (packet_data, packet_size);
|
|
|
|
|
2015-11-02 16:02:55 +00:00
|
|
|
if (start_trim != 0 || end_trim != 0) {
|
|
|
|
gst_buffer_add_audio_clipping_meta (buffer, GST_FORMAT_DEFAULT,
|
2015-10-24 11:27:43 +00:00
|
|
|
start_trim, end_trim);
|
2015-11-02 16:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_list_add (buffer_list, buffer);
|
2015-10-24 11:27:43 +00:00
|
|
|
} while (gst_byte_reader_get_remaining (&reader) > 0);
|
|
|
|
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
|
|
|
|
|
|
|
return buffer_list;
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Failed to parse Opus access unit");
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
2017-07-19 14:14:21 +00:00
|
|
|
if (buffer_list)
|
|
|
|
gst_buffer_list_unref (buffer_list);
|
2015-10-24 11:27:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:10:10 +00:00
|
|
|
static GstBuffer *
|
|
|
|
parse_jpegxs_access_unit (TSDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstByteReader br;
|
|
|
|
guint32 header_tag;
|
|
|
|
guint32 header_size;
|
|
|
|
GstBuffer *retbuf;
|
|
|
|
|
|
|
|
if (stream->current_size < 30) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Not enough data for header");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_byte_reader_init (&br, stream->data, stream->current_size);
|
|
|
|
|
|
|
|
/* Should start with `jxes` box header */
|
|
|
|
header_size = gst_byte_reader_get_uint32_be_unchecked (&br);
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&br);
|
|
|
|
if (header_size != 30 || header_tag != 0x6a786573) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad,
|
|
|
|
"Invalid 'jxes' header (size:%u, tag:%" GST_FOURCC_FORMAT ")",
|
|
|
|
header_size, GST_FOURCC_ARGS (header_tag));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME : Parse/extract timecode */
|
|
|
|
|
|
|
|
/* Ignore the rest of that box */
|
|
|
|
retbuf =
|
|
|
|
gst_buffer_new_wrapped_full (0, stream->data, stream->current_size,
|
|
|
|
header_size, stream->current_size - header_size, stream->data, g_free);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
|
|
|
return retbuf;
|
|
|
|
|
|
|
|
error:
|
|
|
|
GST_ERROR ("Failed to parse JPEG-XS access unit");
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:14:21 +00:00
|
|
|
/* interlaced mode is disabled at the moment */
|
|
|
|
/*#define TSDEMUX_JP2K_SUPPORT_INTERLACE */
|
|
|
|
static GstBuffer *
|
|
|
|
parse_jp2k_access_unit (TSDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstByteReader reader;
|
|
|
|
/* header tag */
|
|
|
|
guint32 header_tag;
|
|
|
|
/* Framerate box */
|
|
|
|
guint16 den G_GNUC_UNUSED;
|
|
|
|
guint16 num G_GNUC_UNUSED;
|
|
|
|
/* Maximum bitrate box */
|
|
|
|
guint32 MaxBr G_GNUC_UNUSED;
|
|
|
|
guint32 AUF[2] = { 0, 0 };
|
|
|
|
#ifdef TSDEMUX_JP2K_SUPPORT_INTERLACE
|
|
|
|
/* Field Coding Box */
|
|
|
|
guint8 Fic G_GNUC_UNUSED = 1;
|
|
|
|
guint8 Fio G_GNUC_UNUSED = 0;
|
|
|
|
/* header size equals 38 for non-interlaced, and 48 for interlaced */
|
|
|
|
guint header_size = stream->jp2kInfos.interlace ? 48 : 38;
|
|
|
|
#else
|
|
|
|
/* header size equals 38 for non-interlaced, and 48 for interlaced */
|
|
|
|
guint header_size = 38;
|
|
|
|
#endif
|
|
|
|
/* Time Code box */
|
|
|
|
guint32 HHMMSSFF G_GNUC_UNUSED;
|
|
|
|
/* Broadcast color box */
|
|
|
|
guint8 CollC G_GNUC_UNUSED;
|
|
|
|
guint8 b G_GNUC_UNUSED;
|
|
|
|
|
2017-08-30 06:37:04 +00:00
|
|
|
guint data_location;
|
|
|
|
GstBuffer *retbuf = NULL;
|
2017-07-19 14:14:21 +00:00
|
|
|
|
|
|
|
if (stream->current_size < header_size) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Not enough data for header");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_byte_reader_init (&reader, stream->data, stream->current_size);
|
|
|
|
|
2017-08-30 06:37:04 +00:00
|
|
|
/* Check for the location of the jp2k magic */
|
|
|
|
data_location =
|
|
|
|
gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff, 0xff4fff51, 0,
|
|
|
|
stream->current_size);
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "data location %d", data_location);
|
|
|
|
if (data_location == -1) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Stream does not contain jp2k magic header");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:14:21 +00:00
|
|
|
/* Elementary stream header box 'elsm' == 0x656c736d */
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
if (header_tag != 0x656c736d) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Expected ELSM box but found box %x instead",
|
|
|
|
header_tag);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* Frame rate box 'frat' == 0x66726174 */
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
if (header_tag != 0x66726174) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad,
|
|
|
|
"Expected frame rate box, but found box %x instead", header_tag);
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
}
|
|
|
|
den = gst_byte_reader_get_uint16_be_unchecked (&reader);
|
|
|
|
num = gst_byte_reader_get_uint16_be_unchecked (&reader);
|
|
|
|
/* Maximum bit rate box 'brat' == 0x62726174 */
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
if (header_tag != 0x62726174) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Expected brat box but read box %x instead",
|
|
|
|
header_tag);
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
}
|
|
|
|
MaxBr = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
AUF[0] = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
if (stream->jp2kInfos.interlace) {
|
|
|
|
#ifdef TSDEMUX_JP2K_SUPPORT_INTERLACE
|
|
|
|
AUF[1] = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
/* Field Coding Box 'fiel' == 0x6669656c */
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
if (header_tag != 0x6669656c) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad,
|
|
|
|
"Expected Field Coding box but found box %x instead", header_tag);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Fic = gst_byte_reader_get_uint8_unchecked (&reader);
|
|
|
|
Fio = gst_byte_reader_get_uint8_unchecked (&reader);
|
|
|
|
#else
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "interlaced mode not supported");
|
|
|
|
goto error;
|
|
|
|
#endif
|
|
|
|
}
|
2017-08-29 09:14:59 +00:00
|
|
|
|
2017-07-19 14:14:21 +00:00
|
|
|
/* Time Code Box 'tcod' == 0x74636f64 */
|
2017-08-29 09:14:59 +00:00
|
|
|
/* Some progressive streams might have a AUF[1] of value 0 present */
|
2017-07-19 14:14:21 +00:00
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
2017-08-29 09:14:59 +00:00
|
|
|
if (header_tag == 0 && !stream->jp2kInfos.interlace) {
|
|
|
|
AUF[1] = header_tag;
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
/* Bump up header size and recheck */
|
|
|
|
header_size += 4;
|
|
|
|
if (stream->current_size < header_size) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad, "Not enough data for header");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 14:14:21 +00:00
|
|
|
if (header_tag != 0x74636f64) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad,
|
|
|
|
"Expected Time code box but found %d box instead", header_tag);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
HHMMSSFF = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
/* Broadcast Color Box 'bcol' == 0x6263686c */
|
|
|
|
header_tag = gst_byte_reader_get_uint32_be_unchecked (&reader);
|
|
|
|
if (header_tag != 0x62636f6c) {
|
|
|
|
GST_ERROR_OBJECT (stream->pad,
|
|
|
|
"Expected Broadcast color box but found %x box instead", header_tag);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
CollC = gst_byte_reader_get_uint8_unchecked (&reader);
|
|
|
|
b = gst_byte_reader_get_uint8_unchecked (&reader);
|
|
|
|
|
2017-08-30 06:37:04 +00:00
|
|
|
/* Check if we have enough data to create a valid buffer */
|
|
|
|
if ((stream->current_size - data_location) < (AUF[0] + AUF[1])) {
|
2019-08-08 20:54:32 +00:00
|
|
|
GST_ERROR_OBJECT
|
|
|
|
(stream->pad,
|
|
|
|
"Required size (%d) greater than remaining size in buffer (%d)",
|
2017-08-30 06:37:04 +00:00
|
|
|
AUF[0] + AUF[1], (stream->current_size - data_location));
|
2019-08-08 20:54:32 +00:00
|
|
|
if (stream->expected_size && stream->current_size != stream->expected_size) {
|
|
|
|
/* warn if buffer is truncated due to draining */
|
|
|
|
GST_WARNING_OBJECT
|
|
|
|
(stream->pad,
|
|
|
|
"Truncated buffer: current size (%d) doesn't match expected size (%d)",
|
|
|
|
stream->current_size, stream->expected_size);
|
|
|
|
} else {
|
|
|
|
/* kill pipeline if either we don't know what expected size is, or
|
|
|
|
* we know the expected size, and thus are sure that the buffer is not
|
|
|
|
* truncated due to draining */
|
|
|
|
goto error;
|
|
|
|
}
|
2017-07-19 14:14:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 06:37:04 +00:00
|
|
|
retbuf = gst_buffer_new_wrapped_full (0, stream->data, stream->current_size,
|
|
|
|
data_location, stream->current_size - data_location,
|
|
|
|
stream->data, g_free);
|
2017-07-19 14:14:21 +00:00
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
2017-08-30 06:37:04 +00:00
|
|
|
return retbuf;
|
2017-07-19 14:14:21 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
GST_ERROR ("Failed to parse JP2K access unit");
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-20 11:27:05 +00:00
|
|
|
static GstBuffer *
|
|
|
|
parse_aac_adts_frame (TSDemuxStream * stream)
|
|
|
|
{
|
|
|
|
gint data_location = -1;
|
|
|
|
guint frame_len;
|
|
|
|
guint crc_size;
|
|
|
|
guint mpegversion = 4;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (stream->current_size < 6) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Not enough data for header");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check syncword */
|
|
|
|
for (i = 0; i < stream->current_size - 2; i++) {
|
|
|
|
if ((stream->data[i] == 0xff) && ((stream->data[i + 1] & 0xf6) == 0xf0)) {
|
|
|
|
data_location = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_TRACE_OBJECT (stream->pad, "data location %d", data_location);
|
|
|
|
|
|
|
|
if (data_location == -1) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Stream does not contain adts syncword");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->current_size - data_location < 6) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Not enough data for header");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_len = ((stream->data[data_location + 3] & 0x03) << 11) |
|
|
|
|
(stream->data[data_location + 4] << 3) | ((stream->data[data_location +
|
|
|
|
5] & 0xe0) >> 5);
|
|
|
|
|
|
|
|
crc_size = (stream->data[data_location + 1] & 0x01) ? 0 : 2;
|
|
|
|
|
|
|
|
if (frame_len < 7 + crc_size) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Invalid frame len %d", frame_len);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this seems to be valid adts header, check mpeg version now
|
|
|
|
*
|
|
|
|
* TODO: check channels, rate, and profile and then update caps too?
|
|
|
|
*/
|
|
|
|
mpegversion = (stream->data[data_location + 1] & 0x08) ? 2 : 4;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (mpegversion != stream->atdsInfos.mpegversion) {
|
|
|
|
GstCaps *caps;
|
|
|
|
MpegTSBaseStream *bstream = (MpegTSBaseStream *) stream;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Update mpegversion from %d to %d",
|
|
|
|
stream->atdsInfos.mpegversion, mpegversion);
|
|
|
|
stream->atdsInfos.mpegversion = mpegversion;
|
|
|
|
|
|
|
|
caps = gst_stream_get_caps (bstream->stream_object);
|
|
|
|
caps = gst_caps_make_writable (caps);
|
|
|
|
|
|
|
|
gst_caps_set_simple (caps, "mpegversion", G_TYPE_INT, mpegversion, NULL);
|
|
|
|
gst_stream_set_caps (bstream->stream_object, caps);
|
|
|
|
gst_pad_set_caps (stream->pad, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_buffer_new_wrapped (stream->data, stream->current_size);
|
|
|
|
}
|
|
|
|
|
2023-03-30 09:06:33 +00:00
|
|
|
static GstBufferList *
|
|
|
|
parse_pes_metadata_frame (TSDemuxStream * stream)
|
|
|
|
{
|
|
|
|
GstByteReader reader;
|
|
|
|
GstBufferList *buffer_list = NULL;
|
|
|
|
|
|
|
|
buffer_list = gst_buffer_list_new ();
|
|
|
|
gst_byte_reader_init (&reader, stream->data, stream->current_size);
|
|
|
|
|
|
|
|
do {
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstMpegtsPESMetadataMeta *meta;
|
|
|
|
guint8 *au_data;
|
|
|
|
guint16 au_size;
|
|
|
|
guint8 service_id;
|
|
|
|
guint8 sequence_number;
|
|
|
|
guint8 flags;
|
|
|
|
|
|
|
|
if (gst_byte_reader_get_remaining (&reader) <
|
|
|
|
PES_PACKET_METADATA_AU_HEADER_LEN)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint8 (&reader, &service_id))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint8 (&reader, &sequence_number))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint8 (&reader, &flags))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint16_be (&reader, &au_size))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (gst_byte_reader_get_remaining (&reader) < au_size)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_dup_data (&reader, au_size, &au_data))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
buffer = gst_buffer_new_wrapped (au_data, au_size);
|
|
|
|
meta = gst_buffer_add_mpegts_pes_metadata_meta (buffer);
|
|
|
|
meta->metadata_service_id = service_id;
|
|
|
|
meta->flags = flags;
|
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"metadata_service_id: 0x%02x, flags: 0x%02x, cell_data_length: 0x%04x",
|
|
|
|
meta->metadata_service_id, meta->flags, au_size);
|
|
|
|
|
|
|
|
gst_buffer_list_add (buffer_list, buffer);
|
|
|
|
} while (gst_byte_reader_get_remaining (&reader) > 0);
|
|
|
|
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
|
|
|
|
|
|
|
return buffer_list;
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Failed to parse PES metadata access units");
|
|
|
|
g_free (stream->data);
|
|
|
|
stream->data = NULL;
|
|
|
|
stream->current_size = 0;
|
|
|
|
if (buffer_list)
|
|
|
|
gst_buffer_list_unref (buffer_list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 11:27:05 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static GstFlowReturn
|
2016-07-13 09:02:23 +00:00
|
|
|
gst_ts_demux_push_pending_data (GstTSDemux * demux, TSDemuxStream * stream,
|
|
|
|
MpegTSBaseProgram * target_program)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2018-08-31 12:43:46 +00:00
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (demux);
|
2011-02-16 16:57:42 +00:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
MpegTSBaseStream *bs = (MpegTSBaseStream *) stream;
|
2012-05-21 15:53:37 +00:00
|
|
|
GstBuffer *buffer = NULL;
|
2015-10-24 11:27:43 +00:00
|
|
|
GstBufferList *buffer_list = NULL;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2016-07-13 09:02:23 +00:00
|
|
|
|
2011-11-08 19:19:41 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"stream:%p, pid:0x%04x stream_type:%d state:%d", stream, bs->pid,
|
|
|
|
bs->stream_type, stream->state);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
if (G_UNLIKELY (stream->data == NULL)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (stream->pad, "stream->data == NULL");
|
2011-02-16 16:57:42 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_UNLIKELY (stream->state == PENDING_PACKET_EMPTY)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (stream->pad, "EMPTY: returning");
|
2011-02-16 16:57:42 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2012-03-30 17:19:12 +00:00
|
|
|
if (G_UNLIKELY (stream->state != PENDING_PACKET_BUFFER)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (stream->pad, "state:%d, returning", stream->state);
|
2011-07-16 11:44:00 +00:00
|
|
|
goto beach;
|
2012-03-30 17:19:12 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
if (G_UNLIKELY (demux->program == NULL)) {
|
|
|
|
GST_LOG_OBJECT (demux, "No program");
|
2012-05-21 15:53:37 +00:00
|
|
|
g_free (stream->data);
|
2011-07-16 11:44:00 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
if (stream->needs_keyframe) {
|
|
|
|
MpegTSBase *base = (MpegTSBase *) demux;
|
2013-07-29 06:10:07 +00:00
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
if ((gst_ts_demux_adjust_seek_offset_for_keyframe (stream, stream->data,
|
|
|
|
stream->current_size)) || demux->last_seek_offset == 0) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Got Keyframe, ready to go at %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (stream->pts));
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_PRIVATE_PES_PACKETS &&
|
|
|
|
bs->registration_id == DRF_ID_OPUS) {
|
|
|
|
buffer_list = parse_opus_access_unit (stream);
|
2017-07-19 14:14:21 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_JP2K) {
|
|
|
|
buffer = parse_jp2k_access_unit (stream);
|
2023-03-30 09:09:59 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_METADATA_PES_PACKETS
|
|
|
|
&& bs->registration_id == DRF_ID_KLVA) {
|
|
|
|
buffer_list = parse_pes_metadata_frame (stream);
|
2024-07-15 14:10:10 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_JPEG_XS) {
|
|
|
|
buffer = parse_jpegxs_access_unit (stream);
|
2015-10-24 11:27:43 +00:00
|
|
|
} else {
|
|
|
|
buffer = gst_buffer_new_wrapped (stream->data, stream->current_size);
|
|
|
|
}
|
|
|
|
|
2023-04-04 14:41:39 +00:00
|
|
|
if (buffer == NULL && buffer_list == NULL) {
|
|
|
|
res = GST_FLOW_ERROR;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
stream->seeked_pts = stream->pts;
|
|
|
|
stream->seeked_dts = stream->dts;
|
|
|
|
stream->needs_keyframe = FALSE;
|
|
|
|
} else {
|
2021-06-03 09:24:53 +00:00
|
|
|
GList *tmp;
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Rewinding after keyframe seek failure");
|
2014-05-30 14:52:09 +00:00
|
|
|
base->seek_offset = demux->last_seek_offset - 200 * base->packetsize;
|
|
|
|
if (demux->last_seek_offset < 200 * base->packetsize)
|
|
|
|
base->seek_offset = 0;
|
|
|
|
demux->last_seek_offset = base->seek_offset;
|
|
|
|
mpegts_packetizer_flush (base->packetizer, FALSE);
|
2021-06-03 09:24:53 +00:00
|
|
|
|
|
|
|
/* Reset all streams accordingly */
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *cand = tmp->data;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (cand->pad, "Clearing stream");
|
|
|
|
cand->continuity_counter = CONTINUITY_UNSET;
|
|
|
|
cand->state = PENDING_PACKET_EMPTY;
|
|
|
|
if (cand->data)
|
|
|
|
g_free (cand->data);
|
|
|
|
cand->data = NULL;
|
|
|
|
cand->allocated_size = 0;
|
|
|
|
cand->current_size = 0;
|
|
|
|
}
|
2014-05-30 14:52:09 +00:00
|
|
|
base->mode = BASE_MODE_SEEKING;
|
|
|
|
|
|
|
|
res = GST_FLOW_REWINDING;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-24 11:27:43 +00:00
|
|
|
if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_PRIVATE_PES_PACKETS &&
|
|
|
|
bs->registration_id == DRF_ID_OPUS) {
|
|
|
|
buffer_list = parse_opus_access_unit (stream);
|
2017-07-19 14:14:21 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_JP2K) {
|
|
|
|
buffer = parse_jp2k_access_unit (stream);
|
2020-03-20 11:27:05 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_AUDIO_AAC_ADTS) {
|
|
|
|
buffer = parse_aac_adts_frame (stream);
|
2023-03-30 09:09:59 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_METADATA_PES_PACKETS
|
|
|
|
&& bs->registration_id == DRF_ID_KLVA) {
|
|
|
|
buffer_list = parse_pes_metadata_frame (stream);
|
2024-07-15 14:10:10 +00:00
|
|
|
} else if (bs->stream_type == GST_MPEGTS_STREAM_TYPE_VIDEO_JPEG_XS) {
|
|
|
|
buffer = parse_jpegxs_access_unit (stream);
|
2015-10-24 11:27:43 +00:00
|
|
|
} else {
|
|
|
|
buffer = gst_buffer_new_wrapped (stream->data, stream->current_size);
|
|
|
|
}
|
2023-04-04 14:41:39 +00:00
|
|
|
if (buffer == NULL && buffer_list == NULL) {
|
|
|
|
res = GST_FLOW_ERROR;
|
|
|
|
goto beach;
|
|
|
|
}
|
2014-05-30 14:52:09 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (stream->pending_ts && !check_pending_buffers (demux))) {
|
2015-10-24 11:27:43 +00:00
|
|
|
if (buffer) {
|
|
|
|
PendingBuffer *pend;
|
2023-01-08 18:46:03 +00:00
|
|
|
pend = g_new0 (PendingBuffer, 1);
|
2015-10-24 11:27:43 +00:00
|
|
|
pend->buffer = buffer;
|
|
|
|
pend->pts = stream->raw_pts;
|
|
|
|
pend->dts = stream->raw_dts;
|
|
|
|
stream->pending = g_list_append (stream->pending, pend);
|
|
|
|
} else {
|
|
|
|
guint i, n;
|
|
|
|
|
|
|
|
n = gst_buffer_list_length (buffer_list);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
PendingBuffer *pend;
|
2023-01-08 18:46:03 +00:00
|
|
|
pend = g_new0 (PendingBuffer, 1);
|
2015-10-24 11:27:43 +00:00
|
|
|
pend->buffer = gst_buffer_ref (gst_buffer_list_get (buffer_list, i));
|
|
|
|
pend->pts = i == 0 ? stream->raw_pts : -1;
|
|
|
|
pend->dts = i == 0 ? stream->raw_dts : -1;
|
|
|
|
stream->pending = g_list_append (stream->pending, pend);
|
|
|
|
}
|
|
|
|
gst_buffer_list_unref (buffer_list);
|
|
|
|
}
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_DEBUG_OBJECT (demux,
|
|
|
|
"Not enough information to push buffers yet, storing buffer");
|
2014-05-30 14:52:09 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
2012-10-17 23:47:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 09:09:59 +00:00
|
|
|
|
|
|
|
if (buffer_list != NULL && gst_buffer_list_length (buffer_list) == 1) {
|
|
|
|
buffer = gst_buffer_ref (gst_buffer_list_get (buffer_list, 0));
|
|
|
|
gst_buffer_list_unref (buffer_list);
|
|
|
|
buffer_list = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
if (G_UNLIKELY (stream->need_newsegment))
|
2016-07-13 09:02:23 +00:00
|
|
|
calculate_and_push_newsegment (demux, stream, target_program);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
/* FIXME : Push pending buffers if any */
|
|
|
|
if (G_UNLIKELY (stream->pending)) {
|
|
|
|
GList *tmp;
|
|
|
|
for (tmp = stream->pending; tmp; tmp = tmp->next) {
|
|
|
|
PendingBuffer *pend = (PendingBuffer *) tmp->data;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Pushing pending buffer PTS:%" GST_TIME_FORMAT " DTS:%"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (pend->buffer)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DTS (pend->buffer)));
|
|
|
|
|
2014-03-28 17:11:21 +00:00
|
|
|
if (stream->discont)
|
|
|
|
GST_BUFFER_FLAG_SET (pend->buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
stream->discont = FALSE;
|
|
|
|
|
2013-07-29 06:10:07 +00:00
|
|
|
res = gst_pad_push (stream->pad, pend->buffer);
|
2014-10-20 10:30:50 +00:00
|
|
|
stream->nb_out_buffers += 1;
|
2023-01-08 18:46:03 +00:00
|
|
|
g_free (pend);
|
2013-07-29 06:10:07 +00:00
|
|
|
}
|
|
|
|
g_list_free (stream->pending);
|
|
|
|
stream->pending = NULL;
|
|
|
|
}
|
2011-11-08 19:19:41 +00:00
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
if ((GST_CLOCK_TIME_IS_VALID (stream->seeked_pts)
|
|
|
|
&& stream->pts < stream->seeked_pts) ||
|
|
|
|
(GST_CLOCK_TIME_IS_VALID (stream->seeked_dts) &&
|
|
|
|
stream->pts < stream->seeked_dts)) {
|
|
|
|
GST_INFO_OBJECT (stream->pad,
|
|
|
|
"Droping with PTS: %" GST_TIME_FORMAT " DTS: %" GST_TIME_FORMAT
|
|
|
|
" after seeking as other stream needed to be seeked further"
|
|
|
|
"(seeked PTS: %" GST_TIME_FORMAT " DTS: %" GST_TIME_FORMAT ")",
|
|
|
|
GST_TIME_ARGS (stream->pts), GST_TIME_ARGS (stream->dts),
|
|
|
|
GST_TIME_ARGS (stream->seeked_pts), GST_TIME_ARGS (stream->seeked_dts));
|
2015-10-24 11:27:43 +00:00
|
|
|
if (buffer)
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
if (buffer_list)
|
|
|
|
gst_buffer_list_unref (buffer_list);
|
2014-05-30 14:52:09 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2012-02-24 16:53:52 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "stream->pts %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (stream->pts));
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
/* Decorate buffer or first buffer of the buffer list */
|
|
|
|
if (buffer_list)
|
|
|
|
buffer = gst_buffer_list_get (buffer_list, 0);
|
|
|
|
|
2012-05-21 15:53:37 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (stream->pts))
|
2019-10-11 14:25:04 +00:00
|
|
|
GST_BUFFER_PTS (buffer) = GST_BUFFER_DTS (buffer) = stream->pts;
|
|
|
|
/* DTS = PTS by default, we override it if there's a real DTS */
|
2012-05-21 15:53:37 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (stream->dts))
|
2013-04-26 13:39:30 +00:00
|
|
|
GST_BUFFER_DTS (buffer) = stream->dts;
|
2011-11-08 19:19:41 +00:00
|
|
|
|
2014-03-28 17:11:21 +00:00
|
|
|
if (stream->discont)
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
stream->discont = FALSE;
|
|
|
|
|
2015-10-24 11:27:43 +00:00
|
|
|
if (buffer_list)
|
|
|
|
buffer = NULL;
|
2015-03-09 09:56:38 +00:00
|
|
|
|
2015-10-24 11:27:43 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad,
|
|
|
|
"Pushing buffer%s with PTS: %" GST_TIME_FORMAT " , DTS: %"
|
|
|
|
GST_TIME_FORMAT, (buffer_list ? "list" : ""), GST_TIME_ARGS (stream->pts),
|
|
|
|
GST_TIME_ARGS (stream->dts));
|
|
|
|
|
2018-09-19 15:07:34 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (stream->dts)) {
|
|
|
|
if (stream->dts > base->out_segment.position)
|
|
|
|
base->out_segment.position = stream->dts;
|
|
|
|
} else if (GST_CLOCK_TIME_IS_VALID (stream->pts)) {
|
|
|
|
if (stream->pts > base->out_segment.position)
|
|
|
|
base->out_segment.position = stream->pts;
|
|
|
|
}
|
2015-10-24 11:27:43 +00:00
|
|
|
|
|
|
|
if (buffer) {
|
|
|
|
res = gst_pad_push (stream->pad, buffer);
|
|
|
|
/* Record that a buffer was pushed */
|
|
|
|
stream->nb_out_buffers += 1;
|
|
|
|
} else {
|
|
|
|
guint n = gst_buffer_list_length (buffer_list);
|
|
|
|
res = gst_pad_push_list (stream->pad, buffer_list);
|
|
|
|
/* Record that a buffer was pushed */
|
|
|
|
stream->nb_out_buffers += n;
|
|
|
|
}
|
2024-01-30 18:16:39 +00:00
|
|
|
|
|
|
|
/* If this proceeded past the end of the configured segment, mark it as EOS */
|
|
|
|
if (res == GST_FLOW_OK) {
|
|
|
|
/* If the pad returned anything other than GST_FLOW_OK, the flow combiner
|
|
|
|
* will already handle that, we only need to check for EOS */
|
|
|
|
GstClockTime ts = stream->dts;
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (ts))
|
|
|
|
ts = stream->pts;
|
|
|
|
|
|
|
|
if (base->out_segment.stop != -1 &&
|
|
|
|
GST_CLOCK_TIME_IS_VALID (ts) && ts >= base->out_segment.stop) {
|
|
|
|
GST_DEBUG_OBJECT (stream->pad, "DTS %" GST_TIMEP_FORMAT
|
|
|
|
" is past the segment stop %" GST_TIMEP_FORMAT ". Marking pad as EOS",
|
|
|
|
&ts, &base->out_segment.stop);
|
|
|
|
res = GST_FLOW_EOS;
|
|
|
|
}
|
|
|
|
}
|
2011-07-16 11:44:00 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "Returned %s", gst_flow_get_name (res));
|
2024-01-30 18:16:39 +00:00
|
|
|
res =
|
|
|
|
gst_flow_combiner_update_pad_flow (demux->flowcombiner, stream->pad, res);
|
2011-07-16 11:44:00 +00:00
|
|
|
GST_DEBUG_OBJECT (stream->pad, "combined %s", gst_flow_get_name (res));
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-10-20 10:30:50 +00:00
|
|
|
/* GAP / sparse stream tracking */
|
|
|
|
if (G_UNLIKELY (stream->gap_ref_pts == GST_CLOCK_TIME_NONE))
|
|
|
|
stream->gap_ref_pts = stream->pts;
|
|
|
|
else {
|
|
|
|
/* Look if the stream PTS has advanced 2 seconds since the last
|
|
|
|
* gap check, and sync streams if it has. The first stream to
|
|
|
|
* hit this will trigger a gap check */
|
|
|
|
if (G_UNLIKELY (stream->pts != GST_CLOCK_TIME_NONE &&
|
|
|
|
stream->pts > stream->gap_ref_pts + 2 * GST_SECOND)) {
|
2016-07-12 13:58:40 +00:00
|
|
|
if (demux->program->pcr_pid != 0x1fff) {
|
|
|
|
GstClockTime curpcr =
|
|
|
|
mpegts_packetizer_get_current_time (MPEG_TS_BASE_PACKETIZER (demux),
|
|
|
|
demux->program->pcr_pid);
|
|
|
|
if (curpcr == GST_CLOCK_TIME_NONE || curpcr < 800 * GST_MSECOND)
|
|
|
|
goto beach;
|
|
|
|
curpcr -= 800 * GST_MSECOND;
|
|
|
|
/* Use the current PCR (with a safety margin) to sync against */
|
|
|
|
gst_ts_demux_check_and_sync_streams (demux, curpcr);
|
|
|
|
} else {
|
|
|
|
/* If we don't have a PCR track, just use the current stream PTS */
|
|
|
|
gst_ts_demux_check_and_sync_streams (demux, stream->pts);
|
|
|
|
}
|
2014-10-20 10:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
beach:
|
2019-06-28 05:04:29 +00:00
|
|
|
/* Reset the PES payload collection, but don't clear the state,
|
|
|
|
* we might want to keep collecting this PES */
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "Cleared PES data. returning %s",
|
|
|
|
gst_flow_get_name (res));
|
2019-06-28 05:04:29 +00:00
|
|
|
if (stream->expected_size) {
|
|
|
|
if (stream->current_size > stream->expected_size)
|
|
|
|
stream->expected_size = 0;
|
|
|
|
else
|
|
|
|
stream->expected_size -= stream->current_size;
|
|
|
|
}
|
2012-05-21 15:53:37 +00:00
|
|
|
stream->data = NULL;
|
2019-06-28 05:04:29 +00:00
|
|
|
stream->allocated_size = 0;
|
2012-03-05 08:38:57 +00:00
|
|
|
stream->current_size = 0;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_ts_demux_handle_packet (GstTSDemux * demux, TSDemuxStream * stream,
|
2014-06-25 12:50:05 +00:00
|
|
|
MpegTSPacketizerPacket * packet, GstMpegtsSection * section)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "pid 0x%04x pusi:%d, afc:%d, cont:%d, payload:%p",
|
|
|
|
packet->pid, packet->payload_unit_start_indicator,
|
|
|
|
packet->scram_afc_cc & 0x30,
|
2013-06-23 06:43:23 +00:00
|
|
|
FLAGS_CONTINUITY_COUNTER (packet->scram_afc_cc), packet->payload);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-05-16 14:15:58 +00:00
|
|
|
if (G_UNLIKELY (packet->payload_unit_start_indicator) &&
|
2019-06-28 05:04:29 +00:00
|
|
|
FLAGS_HAS_PAYLOAD (packet->scram_afc_cc)) {
|
2011-02-16 16:57:42 +00:00
|
|
|
/* Flush previous data */
|
2016-07-13 09:02:23 +00:00
|
|
|
res = gst_ts_demux_push_pending_data (demux, stream, NULL);
|
2021-06-03 09:24:53 +00:00
|
|
|
if (res != GST_FLOW_REWINDING) {
|
|
|
|
/* Tell the data collecting to expect this header. We don't do this when
|
|
|
|
* rewinding since the states will have been resetted accordingly */
|
|
|
|
stream->state = PENDING_PACKET_HEADER;
|
|
|
|
}
|
2019-06-28 05:04:29 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-05-18 08:15:15 +00:00
|
|
|
if (packet->payload && (res == GST_FLOW_OK || res == GST_FLOW_NOT_LINKED)
|
|
|
|
&& stream->pad) {
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_ts_demux_queue_data (demux, stream, packet);
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "current_size:%d, expected_size:%d",
|
2012-03-05 08:38:57 +00:00
|
|
|
stream->current_size, stream->expected_size);
|
2019-06-28 05:04:29 +00:00
|
|
|
/* Finally check if the data we queued completes a packet, or got too
|
|
|
|
* large and needs output now */
|
|
|
|
if ((stream->expected_size && stream->current_size >= stream->expected_size)
|
|
|
|
|| (stream->current_size >= MAX_PES_PAYLOAD)) {
|
2021-10-08 09:38:04 +00:00
|
|
|
GST_LOG_OBJECT (demux, "pushing packet of size %u", stream->current_size);
|
2016-07-13 09:02:23 +00:00
|
|
|
res = gst_ts_demux_push_pending_data (demux, stream, NULL);
|
2012-07-11 18:34:05 +00:00
|
|
|
}
|
2012-05-21 15:53:37 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-05-30 14:52:09 +00:00
|
|
|
/* We are rewinding to find a keyframe,
|
|
|
|
* and didn't want the data to be queued
|
|
|
|
*/
|
|
|
|
if (res == GST_FLOW_REWINDING)
|
|
|
|
res = GST_FLOW_OK;
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-08-26 22:44:08 +00:00
|
|
|
static void
|
2013-05-24 08:59:59 +00:00
|
|
|
gst_ts_demux_flush (MpegTSBase * base, gboolean hard)
|
2011-08-26 22:44:08 +00:00
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX_CAST (base);
|
|
|
|
|
2015-02-03 13:59:16 +00:00
|
|
|
gst_ts_demux_flush_streams (demux, hard);
|
2012-11-13 21:40:25 +00:00
|
|
|
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_lock (&demux->lock);
|
2021-04-05 07:29:37 +00:00
|
|
|
gst_event_replace (&demux->segment_event, NULL);
|
2021-04-05 07:28:51 +00:00
|
|
|
g_mutex_unlock (&demux->lock);
|
2014-02-25 01:43:56 +00:00
|
|
|
if (demux->global_tags) {
|
|
|
|
gst_tag_list_unref (demux->global_tags);
|
|
|
|
demux->global_tags = NULL;
|
|
|
|
}
|
2013-05-24 08:59:59 +00:00
|
|
|
if (hard) {
|
|
|
|
/* For pull mode seeks the current segment needs to be preserved */
|
2013-07-24 13:50:14 +00:00
|
|
|
demux->rate = 1.0;
|
2018-08-31 12:43:46 +00:00
|
|
|
gst_segment_init (&base->out_segment, GST_FORMAT_UNDEFINED);
|
2013-05-24 08:59:59 +00:00
|
|
|
}
|
2011-08-26 22:44:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 16:32:56 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_ts_demux_drain (MpegTSBase * base)
|
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX_CAST (base);
|
|
|
|
GList *tmp;
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
|
|
|
|
if (!demux->program)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
for (tmp = demux->program->stream_list; tmp; tmp = tmp->next) {
|
|
|
|
TSDemuxStream *stream = (TSDemuxStream *) tmp->data;
|
|
|
|
if (stream->pad) {
|
2016-07-13 09:02:23 +00:00
|
|
|
res = gst_ts_demux_push_pending_data (demux, stream, NULL);
|
2014-03-28 16:32:56 +00:00
|
|
|
if (G_UNLIKELY (res != GST_FLOW_OK))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_ts_demux_push (MpegTSBase * base, MpegTSPacketizerPacket * packet,
|
2014-06-25 12:50:05 +00:00
|
|
|
GstMpegtsSection * section)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
|
|
|
GstTSDemux *demux = GST_TS_DEMUX_CAST (base);
|
|
|
|
TSDemuxStream *stream = NULL;
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
|
|
|
|
if (G_LIKELY (demux->program)) {
|
|
|
|
stream = (TSDemuxStream *) demux->program->streams[packet->pid];
|
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
res = gst_ts_demux_handle_packet (demux, stream, packet, section);
|
2012-05-21 15:53:37 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|