From f062b780518dcb7628dabd66f4764ef9ca06f1fb Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Fri, 18 Apr 2014 16:23:43 +0200 Subject: [PATCH] mpegtspacketizer: Improve ts_to_offset code * Search in current pending values first. For CBR streams we can very easily end up having just one initial observations and then nothing else (since the bitrate doesn't change). * Use one group whether we are in that group *OR* if there is only one group. * If the group to use isn't closed (points are being accumulated in the PCROffsetCurrent), use the latest data available for calculation * If in the unlikelyness that all of this *still* didn't produce more than one data point, just return the initial offset --- gst/mpegtsdemux/mpegtspacketizer.c | 45 ++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/gst/mpegtsdemux/mpegtspacketizer.c b/gst/mpegtsdemux/mpegtspacketizer.c index 088028ce9c..8880905d37 100644 --- a/gst/mpegtsdemux/mpegtspacketizer.c +++ b/gst/mpegtsdemux/mpegtspacketizer.c @@ -2211,6 +2211,7 @@ mpegts_packetizer_ts_to_offset (MpegTSPacketizer2 * packetizer, guint64 res; PCROffsetGroup *nextgroup = NULL, *prevgroup = NULL; guint64 querypcr, firstpcr, lastpcr, firstoffset, lastoffset; + PCROffsetCurrent *current; GList *tmp; if (!packetizer->calculate_offset) @@ -2225,6 +2226,16 @@ mpegts_packetizer_ts_to_offset (MpegTSPacketizer2 * packetizer, GST_DEBUG ("Searching offset for ts %" GST_TIME_FORMAT, GST_TIME_ARGS (ts)); + /* First check if we're within the current pending group */ + current = pcrtable->current; + if (current && current->group && (querypcr >= current->group->pcr_offset) && + querypcr - current->group->pcr_offset <= + current->pending[current->last].pcr) { + GST_DEBUG ("pcr is in current group"); + nextgroup = current->group; + goto calculate_points; + } + /* Find the neighbouring groups */ for (tmp = pcrtable->groups; tmp; tmp = tmp->next) { nextgroup = (PCROffsetGroup *) tmp->data; @@ -2256,15 +2267,25 @@ mpegts_packetizer_ts_to_offset (MpegTSPacketizer2 * packetizer, } } - if (nextgroup == prevgroup) { - GST_DEBUG ("In group"); - firstoffset = prevgroup->first_offset; - firstpcr = prevgroup->pcr_offset; - lastoffset = - prevgroup->values[prevgroup->last_value].offset + - prevgroup->first_offset; - lastpcr = - prevgroup->values[prevgroup->last_value].pcr + prevgroup->pcr_offset; +calculate_points: + + GST_DEBUG ("nextgroup:%p, prevgroup:%p", nextgroup, prevgroup); + + if (nextgroup == prevgroup || prevgroup == NULL) { + /* We use the current group to calculate position: + * * if the PCR is within this group + * * if there is only one group to use for calculation + */ + GST_DEBUG ("In group or after last one"); + lastoffset = firstoffset = nextgroup->first_offset; + lastpcr = firstpcr = nextgroup->pcr_offset; + if (current && nextgroup == current->group) { + lastoffset += current->pending[current->last].offset; + lastpcr += current->pending[current->last].pcr; + } else { + lastoffset += nextgroup->values[nextgroup->last_value].offset; + lastpcr += nextgroup->values[nextgroup->last_value].pcr; + } } else if (prevgroup) { GST_DEBUG ("Between group"); lastoffset = nextgroup->first_offset; @@ -2284,8 +2305,10 @@ mpegts_packetizer_ts_to_offset (MpegTSPacketizer2 * packetizer, GST_DEBUG ("Using last PCR %" G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT, lastpcr, lastoffset); - res = firstoffset + gst_util_uint64_scale (querypcr - firstpcr, - lastoffset - firstoffset, lastpcr - firstpcr); + res = firstoffset; + if (lastpcr != firstpcr) + res += gst_util_uint64_scale (querypcr - firstpcr, + lastoffset - firstoffset, lastpcr - firstpcr); GST_DEBUG ("Returning offset %" G_GUINT64_FORMAT " for ts %" GST_TIME_FORMAT, res, GST_TIME_ARGS (ts));