tsmux: Add negative DTS support

Use the saved DTS, make it signed and pass that to the stream muxer. This
preserves the running time sign. All usage of -1 as invalid TS are now
replaced with G_MININT64. Negative values will be seen as wrap-around
point, but the delta between PTS and DTS will remain correct. Demuxers
don't care about absolute values, they only cares about deltas.

https://bugzilla.gnome.org/show_bug.cgi?id=740575
This commit is contained in:
Nicolas Dufresne 2015-06-10 12:32:34 -04:00
parent 8432116df2
commit e000a6f0a4
4 changed files with 77 additions and 59 deletions

View file

@ -331,7 +331,8 @@ static void
mpegtsmux_pad_reset (MpegTsPadData * pad_data) mpegtsmux_pad_reset (MpegTsPadData * pad_data)
{ {
pad_data->pid = 0; pad_data->pid = 0;
pad_data->min_dts = GST_CLOCK_TIME_NONE; pad_data->min_dts = GST_CLOCK_STIME_NONE;
pad_data->dts = GST_CLOCK_STIME_NONE;
pad_data->prog_id = -1; pad_data->prog_id = -1;
#if 0 #if 0
pad_data->prog_id = -1; pad_data->prog_id = -1;
@ -1035,7 +1036,7 @@ mpegtsmux_clip_inc_running_time (GstCollectPads * pads,
*outbuf = buf; *outbuf = buf;
/* PTS */ /* PTS */
time = GST_BUFFER_TIMESTAMP (buf); time = GST_BUFFER_PTS (buf);
/* invalid left alone and passed */ /* invalid left alone and passed */
if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) { if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
@ -1046,11 +1047,11 @@ mpegtsmux_clip_inc_running_time (GstCollectPads * pads,
*outbuf = NULL; *outbuf = NULL;
goto beach; goto beach;
} else { } else {
GST_LOG_OBJECT (cdata->pad, "buffer pts %" GST_TIME_FORMAT " -> %" GST_LOG_OBJECT (cdata->pad, "buffer pts %" GST_TIME_FORMAT " -> %"
GST_TIME_FORMAT " running time", GST_TIME_FORMAT " running time",
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_TIME_ARGS (time)); GST_TIME_ARGS (GST_BUFFER_PTS (buf)), GST_TIME_ARGS (time));
buf = *outbuf = gst_buffer_make_writable (buf); buf = *outbuf = gst_buffer_make_writable (buf);
GST_BUFFER_TIMESTAMP (*outbuf) = time; GST_BUFFER_PTS (*outbuf) = time;
} }
} }
@ -1059,28 +1060,39 @@ mpegtsmux_clip_inc_running_time (GstCollectPads * pads,
/* invalid left alone and passed */ /* invalid left alone and passed */
if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) { if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
time = gst_segment_to_running_time (&cdata->segment, GST_FORMAT_TIME, time); gint sign;
/* may have to decode out-of-segment, so pass INVALID */ gint64 dts;
if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
GST_DEBUG_OBJECT (cdata->pad, "running dts outside segment");
} else {
GST_LOG_OBJECT (cdata->pad, "buffer dts %" GST_TIME_FORMAT " -> %"
GST_TIME_FORMAT " running time",
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_TIME_ARGS (time));
if (!GST_CLOCK_TIME_IS_VALID (pad_data->min_dts)) sign = gst_segment_to_running_time_full (&cdata->segment, GST_FORMAT_TIME,
pad_data->min_dts = time; time, &time);
if (time < pad_data->min_dts) { GST_LOG_OBJECT (cdata->pad, "buffer dts %" GST_TIME_FORMAT " -> %"
/* Ignore DTS going backward */ GST_STIME_FORMAT " running time", GST_STIME_ARGS (GST_BUFFER_DTS (buf)),
GST_WARNING_OBJECT (cdata->pad, "ignoring DTS going backward"); GST_STIME_ARGS (time));
time = pad_data->min_dts;
}
*outbuf = gst_buffer_make_writable (buf); if (sign > 0)
GST_BUFFER_DTS (*outbuf) = time; dts = (gint64) time;
pad_data->min_dts = time; else
dts = -((gint64) time);
if (GST_CLOCK_STIME_IS_VALID (pad_data))
pad_data->min_dts = dts;
if (dts < pad_data->min_dts) {
/* Ignore DTS going backward */
GST_WARNING_OBJECT (cdata->pad, "ignoring DTS going backward");
dts = pad_data->min_dts;
} }
*outbuf = gst_buffer_make_writable (buf);
if (sign > 0)
GST_BUFFER_DTS (*outbuf) = time;
else
GST_BUFFER_DTS (*outbuf) = GST_CLOCK_TIME_NONE;
pad_data->dts = pad_data->min_dts = dts;
} else {
pad_data->dts = GST_CLOCK_STIME_NONE;
} }
buf = *outbuf; buf = *outbuf;
@ -1103,8 +1115,8 @@ mpegtsmux_collected_buffer (GstCollectPads * pads, GstCollectData * data,
GstFlowReturn ret = GST_FLOW_OK; GstFlowReturn ret = GST_FLOW_OK;
MpegTsPadData *best = (MpegTsPadData *) data; MpegTsPadData *best = (MpegTsPadData *) data;
TsMuxProgram *prog; TsMuxProgram *prog;
gint64 pts = -1; gint64 pts = GST_CLOCK_STIME_NONE;
guint64 dts = -1; gint64 dts = GST_CLOCK_STIME_NONE;
gboolean delta = TRUE, header = FALSE; gboolean delta = TRUE, header = FALSE;
StreamData *stream_data; StreamData *stream_data;
@ -1140,7 +1152,7 @@ mpegtsmux_collected_buffer (GstCollectPads * pads, GstCollectData * data,
GstEvent *event; GstEvent *event;
event = check_pending_key_unit_event (mux->force_key_unit_event, event = check_pending_key_unit_event (mux->force_key_unit_event,
&best->collect.segment, GST_BUFFER_TIMESTAMP (buf), &best->collect.segment, GST_BUFFER_PTS (buf),
GST_BUFFER_FLAGS (buf), mux->pending_key_unit_ts); GST_BUFFER_FLAGS (buf), mux->pending_key_unit_ts);
if (event) { if (event) {
GstClockTime running_time; GstClockTime running_time;
@ -1186,18 +1198,19 @@ mpegtsmux_collected_buffer (GstCollectPads * pads, GstCollectData * data,
if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buf))) { if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buf))) {
pts = GSTTIME_TO_MPEGTIME (GST_BUFFER_PTS (buf)); pts = GSTTIME_TO_MPEGTIME (GST_BUFFER_PTS (buf));
GST_DEBUG_OBJECT (mux, "Buffer has PTS %" GST_TIME_FORMAT " pts %" GST_DEBUG_OBJECT (mux, "Buffer has PTS %" GST_TIME_FORMAT " pts %"
G_GINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (buf)), pts); G_GINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (buf)), pts);
} }
if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buf))) { if (GST_CLOCK_STIME_IS_VALID (best->dts)) {
dts = GSTTIME_TO_MPEGTIME (GST_BUFFER_DTS (buf)); dts = GSTTIME_TO_MPEGTIME (best->dts);
GST_DEBUG_OBJECT (mux, "Buffer has DTS %" GST_TIME_FORMAT " dts %" GST_DEBUG_OBJECT (mux, "Buffer has DTS %s%" GST_TIME_FORMAT " dts %"
G_GINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (buf)), dts); G_GINT64_FORMAT, best->dts >= 0 ? " " : "-",
GST_TIME_ARGS (ABS (best->dts)), dts);
} }
/* should not have a DTS without PTS */ /* should not have a DTS without PTS */
if (pts == -1 && dts != -1) { if (!GST_CLOCK_STIME_IS_VALID (pts) && GST_CLOCK_STIME_IS_VALID (dts)) {
GST_DEBUG_OBJECT (mux, "using DTS for unknown PTS"); GST_DEBUG_OBJECT (mux, "using DTS for unknown PTS");
pts = dts; pts = dts;
} }

View file

@ -98,10 +98,9 @@ G_BEGIN_DECLS
#define CLOCK_FREQ (CLOCK_BASE * 10000) /* 90 kHz PTS clock */ #define CLOCK_FREQ (CLOCK_BASE * 10000) /* 90 kHz PTS clock */
#define CLOCK_FREQ_SCR (CLOCK_FREQ * 300) /* 27 MHz SCR clock */ #define CLOCK_FREQ_SCR (CLOCK_FREQ * 300) /* 27 MHz SCR clock */
#define MPEGTIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), \ #define GSTTIME_TO_MPEGTIME(time) \
GST_MSECOND/10, CLOCK_BASE)) (((time) > 0 ? (gint64) 1 : (gint64) -1) * \
#define GSTTIME_TO_MPEGTIME(time) (gst_util_uint64_scale ((time), \ (gint64) gst_util_uint64_scale (ABS(time), CLOCK_BASE, GST_MSECOND/10))
CLOCK_BASE, GST_MSECOND/10))
/* 27 MHz SCR conversions: */ /* 27 MHz SCR conversions: */
#define MPEG_SYS_TIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), \ #define MPEG_SYS_TIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), \
@ -183,8 +182,11 @@ struct MpegTsPadData {
gint pid; gint pid;
TsMuxStream *stream; TsMuxStream *stream;
/* most recent valid TS for this stream */ /* most recent valid DTS for this stream */
GstClockTime min_dts; gint64 min_dts;
/* most recent DTS */
gint64 dts;
#if 0 #if 0
/* (optional) index writing */ /* (optional) index writing */

View file

@ -143,11 +143,11 @@ tsmux_new (void)
mux->next_stream_pid = TSMUX_START_ES_PID; mux->next_stream_pid = TSMUX_START_ES_PID;
mux->pat_changed = TRUE; mux->pat_changed = TRUE;
mux->last_pat_ts = -1; mux->last_pat_ts = G_MININT64;
mux->pat_interval = TSMUX_DEFAULT_PAT_INTERVAL; mux->pat_interval = TSMUX_DEFAULT_PAT_INTERVAL;
mux->si_changed = TRUE; mux->si_changed = TRUE;
mux->last_si_ts = -1; mux->last_si_ts = G_MININT64;
mux->si_interval = TSMUX_DEFAULT_SI_INTERVAL; mux->si_interval = TSMUX_DEFAULT_SI_INTERVAL;
mux->si_sections = g_hash_table_new_full (g_direct_hash, g_direct_equal, mux->si_sections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
@ -363,7 +363,7 @@ tsmux_program_new (TsMux * mux, gint prog_id)
program = g_slice_new0 (TsMuxProgram); program = g_slice_new0 (TsMuxProgram);
program->pmt_changed = TRUE; program->pmt_changed = TRUE;
program->last_pmt_ts = -1; program->last_pmt_ts = G_MININT64;
program->pmt_interval = TSMUX_DEFAULT_PMT_INTERVAL; program->pmt_interval = TSMUX_DEFAULT_PMT_INTERVAL;
if (prog_id == 0) { if (prog_id == 0) {
@ -983,13 +983,13 @@ tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
GList *cur; GList *cur;
cur_pcr = 0; cur_pcr = 0;
if (cur_pts != -1) { if (cur_pts != G_MININT64) {
TS_DEBUG ("TS for PCR stream is %" G_GINT64_FORMAT, cur_pts); TS_DEBUG ("TS for PCR stream is %" G_GINT64_FORMAT, cur_pts);
} }
/* FIXME: The current PCR needs more careful calculation than just /* FIXME: The current PCR needs more careful calculation than just
* writing a fixed offset */ * writing a fixed offset */
if (cur_pts != -1) { if (cur_pts != G_MININT64) {
/* CLOCK_BASE >= TSMUX_PCR_OFFSET */ /* CLOCK_BASE >= TSMUX_PCR_OFFSET */
cur_pts += CLOCK_BASE; cur_pts += CLOCK_BASE;
cur_pcr = (cur_pts - TSMUX_PCR_OFFSET) * cur_pcr = (cur_pts - TSMUX_PCR_OFFSET) *
@ -1010,7 +1010,7 @@ tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
} }
/* check if we need to rewrite pat */ /* check if we need to rewrite pat */
if (mux->last_pat_ts == -1 || mux->pat_changed) if (mux->last_pat_ts == G_MININT64 || mux->pat_changed)
write_pat = TRUE; write_pat = TRUE;
else if (cur_pts >= mux->last_pat_ts + mux->pat_interval) else if (cur_pts >= mux->last_pat_ts + mux->pat_interval)
write_pat = TRUE; write_pat = TRUE;
@ -1024,7 +1024,7 @@ tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
} }
/* check if we need to rewrite sit */ /* check if we need to rewrite sit */
if (mux->last_si_ts == -1 || mux->si_changed) if (mux->last_si_ts == G_MININT64 || mux->si_changed)
write_si = TRUE; write_si = TRUE;
else if (cur_pts >= mux->last_si_ts + mux->si_interval) else if (cur_pts >= mux->last_si_ts + mux->si_interval)
write_si = TRUE; write_si = TRUE;
@ -1042,7 +1042,7 @@ tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
TsMuxProgram *program = (TsMuxProgram *) cur->data; TsMuxProgram *program = (TsMuxProgram *) cur->data;
gboolean write_pmt; gboolean write_pmt;
if (program->last_pmt_ts == -1 || program->pmt_changed) if (program->last_pmt_ts == G_MININT64 || program->pmt_changed)
write_pmt = TRUE; write_pmt = TRUE;
else if (cur_pts >= program->last_pmt_ts + program->pmt_interval) else if (cur_pts >= program->last_pmt_ts + program->pmt_interval)
write_pmt = TRUE; write_pmt = TRUE;
@ -1060,9 +1060,9 @@ tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
pi->packet_start_unit_indicator = tsmux_stream_at_pes_start (stream); pi->packet_start_unit_indicator = tsmux_stream_at_pes_start (stream);
if (pi->packet_start_unit_indicator) { if (pi->packet_start_unit_indicator) {
tsmux_stream_initialize_pes_packet (stream); tsmux_stream_initialize_pes_packet (stream);
if (stream->dts != -1) if (stream->dts != G_MININT64)
stream->dts += CLOCK_BASE; stream->dts += CLOCK_BASE;
if (stream->pts != -1) if (stream->pts != G_MININT64)
stream->pts += CLOCK_BASE; stream->pts += CLOCK_BASE;
} }
pi->stream_avail = tsmux_stream_bytes_avail (stream); pi->stream_avail = tsmux_stream_bytes_avail (stream);

View file

@ -196,8 +196,8 @@ tsmux_stream_new (guint16 pid, TsMuxStreamType stream_type)
break; break;
} }
stream->last_pts = -1; stream->last_pts = GST_CLOCK_STIME_NONE;
stream->last_dts = -1; stream->last_dts = GST_CLOCK_STIME_NONE;
stream->pcr_ref = 0; stream->pcr_ref = 0;
stream->last_pcr = -1; stream->last_pcr = -1;
@ -279,10 +279,10 @@ tsmux_stream_consume (TsMuxStream * stream, guint len)
if (stream->cur_buffer_consumed == 0) if (stream->cur_buffer_consumed == 0)
return; return;
if (stream->cur_buffer->pts != -1) { if (GST_CLOCK_STIME_IS_VALID (stream->cur_buffer->pts)) {
stream->last_pts = stream->cur_buffer->pts; stream->last_pts = stream->cur_buffer->pts;
stream->last_dts = stream->cur_buffer->dts; stream->last_dts = stream->cur_buffer->dts;
} else if (stream->cur_buffer->dts != -1) } else if (GST_CLOCK_STIME_IS_VALID (stream->cur_buffer->dts))
stream->last_dts = stream->cur_buffer->dts; stream->last_dts = stream->cur_buffer->dts;
if (stream->cur_buffer_consumed == stream->cur_buffer->size) { if (stream->cur_buffer_consumed == stream->cur_buffer->size) {
@ -407,10 +407,12 @@ tsmux_stream_initialize_pes_packet (TsMuxStream * stream)
stream->pi.flags &= ~(TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS | stream->pi.flags &= ~(TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS |
TSMUX_PACKET_FLAG_PES_WRITE_PTS); TSMUX_PACKET_FLAG_PES_WRITE_PTS);
if (stream->pts != -1 && stream->dts != -1 && stream->pts != stream->dts) if (GST_CLOCK_STIME_IS_VALID (stream->pts)
&& GST_CLOCK_STIME_IS_VALID (stream->dts)
&& stream->pts != stream->dts)
stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS; stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS;
else { else {
if (stream->pts != -1) if (GST_CLOCK_STIME_IS_VALID (stream->pts))
stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS; stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS;
} }
@ -546,8 +548,8 @@ tsmux_stream_find_pts_dts_within (TsMuxStream * stream, guint bound,
{ {
GList *cur; GList *cur;
*pts = -1; *pts = GST_CLOCK_STIME_NONE;
*dts = -1; *dts = GST_CLOCK_STIME_NONE;
for (cur = stream->buffers; cur; cur = cur->next) { for (cur = stream->buffers; cur; cur = cur->next) {
TsMuxStreamBuffer *curbuf = cur->data; TsMuxStreamBuffer *curbuf = cur->data;
@ -562,7 +564,8 @@ tsmux_stream_find_pts_dts_within (TsMuxStream * stream, guint bound,
} }
/* Have we found a buffer with pts/dts set? */ /* Have we found a buffer with pts/dts set? */
if (curbuf->pts != -1 || curbuf->dts != -1) { if (GST_CLOCK_STIME_IS_VALID (curbuf->pts)
|| GST_CLOCK_STIME_IS_VALID (curbuf->dts)) {
*pts = curbuf->pts; *pts = curbuf->pts;
*dts = curbuf->dts; *dts = curbuf->dts;
return; return;
@ -656,7 +659,7 @@ tsmux_stream_write_pes_header (TsMuxStream * stream, guint8 * data)
* *
* Submit @len bytes of @data into @stream. @pts and @dts can be set to the * Submit @len bytes of @data into @stream. @pts and @dts can be set to the
* timestamp (against a 90Hz clock) of the first access unit in @data. A * timestamp (against a 90Hz clock) of the first access unit in @data. A
* timestamp of -1 for @pts or @dts means unknown. * timestamp of GST_CLOCK_STIME_NNOE for @pts or @dts means unknown.
* *
* @user_data will be passed to the release function as set with * @user_data will be passed to the release function as set with
* tsmux_stream_set_buffer_release_func() when @data can be freed. * tsmux_stream_set_buffer_release_func() when @data can be freed.
@ -957,7 +960,7 @@ tsmux_stream_is_pcr (TsMuxStream * stream)
guint64 guint64
tsmux_stream_get_pts (TsMuxStream * stream) tsmux_stream_get_pts (TsMuxStream * stream)
{ {
g_return_val_if_fail (stream != NULL, -1); g_return_val_if_fail (stream != NULL, GST_CLOCK_STIME_NONE);
return stream->last_pts; return stream->last_pts;
} }