3 * Copyright (C) 2007, 2008 Alessandro Decina, Zaheer Merali
6 * Zaheer Merali <zaheerabbas at merali dot org>
7 * Alessandro Decina <alessandro@nnva.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
31 /* Skew calculation pameters */
32 #define MAX_TIME (2 * GST_SECOND)
34 /* maximal PCR time */
35 #define PCR_MAX_VALUE (((((guint64)1)<<33) * 300) + 298)
36 #define PCR_GST_MAX_VALUE (PCR_MAX_VALUE * GST_MSECOND / (PCR_MSECOND))
37 #define PTS_DTS_MAX_VALUE (((guint64)1) << 33)
39 #include "mpegtspacketizer.h"
40 #include "gstmpegdesc.h"
42 GST_DEBUG_CATEGORY_STATIC (mpegts_packetizer_debug);
43 #define GST_CAT_DEFAULT mpegts_packetizer_debug
45 static void _init_local (void);
46 G_DEFINE_TYPE_EXTENDED (MpegTSPacketizer2, mpegts_packetizer, G_TYPE_OBJECT, 0,
49 #define ABSDIFF(a,b) ((a) < (b) ? (b) - (a) : (a) - (b))
51 #define PACKETIZER_GROUP_LOCK(p) g_mutex_lock(&((p)->group_lock))
52 #define PACKETIZER_GROUP_UNLOCK(p) g_mutex_unlock(&((p)->group_lock))
54 static void mpegts_packetizer_dispose (GObject * object);
55 static void mpegts_packetizer_finalize (GObject * object);
56 static GstClockTime calculate_skew (MpegTSPacketizer2 * packetizer,
57 MpegTSPCR * pcr, guint64 pcrtime, GstClockTime time);
58 static void _close_current_group (MpegTSPCR * pcrtable);
59 static void record_pcr (MpegTSPacketizer2 * packetizer, MpegTSPCR * pcrtable,
60 guint64 pcr, guint64 offset);
62 #define CONTINUITY_UNSET 255
63 #define VERSION_NUMBER_UNSET 255
64 #define TABLE_ID_UNSET 0xFF
65 #define PACKET_SYNC_BYTE 0x47
67 static inline MpegTSPCR *
68 get_pcr_table (MpegTSPacketizer2 * packetizer, guint16 pid)
72 res = packetizer->observations[packetizer->pcrtablelut[pid]];
74 if (G_UNLIKELY (res == NULL)) {
75 /* If we don't have a PCR table for the requested PID, create one .. */
76 res = g_new0 (MpegTSPCR, 1);
77 /* Add it to the last table position */
78 packetizer->observations[packetizer->lastobsid] = res;
79 /* Update the pcrtablelut */
80 packetizer->pcrtablelut[pid] = packetizer->lastobsid;
81 /* And increment the last know slot */
82 packetizer->lastobsid++;
84 /* Finally set the default values */
86 res->base_time = GST_CLOCK_TIME_NONE;
87 res->base_pcrtime = GST_CLOCK_TIME_NONE;
88 res->last_pcrtime = GST_CLOCK_TIME_NONE;
90 res->window_filling = TRUE;
93 res->prev_send_diff = GST_CLOCK_TIME_NONE;
94 res->prev_out_time = GST_CLOCK_TIME_NONE;
97 res->current = g_slice_new0 (PCROffsetCurrent);
104 pcr_offset_group_free (PCROffsetGroup * group)
106 g_free (group->values);
107 g_slice_free (PCROffsetGroup, group);
111 flush_observations (MpegTSPacketizer2 * packetizer)
115 for (i = 0; i < packetizer->lastobsid; i++) {
116 g_list_free_full (packetizer->observations[i]->groups,
117 (GDestroyNotify) pcr_offset_group_free);
118 if (packetizer->observations[i]->current)
119 g_slice_free (PCROffsetCurrent, packetizer->observations[i]->current);
120 g_free (packetizer->observations[i]);
121 packetizer->observations[i] = NULL;
123 memset (packetizer->pcrtablelut, 0xff, 0x2000);
124 packetizer->lastobsid = 0;
128 mpegts_packetizer_get_current_time (MpegTSPacketizer2 * packetizer,
131 MpegTSPCR *pcrtable = get_pcr_table (packetizer, pcr_pid);
133 if (pcrtable == NULL)
134 return GST_CLOCK_TIME_NONE;
136 return mpegts_packetizer_pts_to_ts (packetizer, pcrtable->last_pcrtime,
140 static inline MpegTSPacketizerStreamSubtable *
141 find_subtable (GSList * subtables, guint8 table_id, guint16 subtable_extension)
145 /* FIXME: Make this an array ! */
146 for (tmp = subtables; tmp; tmp = tmp->next) {
147 MpegTSPacketizerStreamSubtable *sub =
148 (MpegTSPacketizerStreamSubtable *) tmp->data;
149 if (sub->table_id == table_id
150 && sub->subtable_extension == subtable_extension)
158 seen_section_before (MpegTSPacketizerStream * stream, guint8 table_id,
159 guint16 subtable_extension, guint8 version_number, guint8 section_number,
160 guint8 last_section_number, guint8 * data_start, gsize to_read)
162 MpegTSPacketizerStreamSubtable *subtable;
164 /* Check if we've seen this table_id/subtable_extension first */
165 subtable = find_subtable (stream->subtables, table_id, subtable_extension);
167 GST_DEBUG ("Haven't seen subtable");
170 /* If we have, check it has the same version_number */
171 if (subtable->version_number != version_number) {
172 GST_DEBUG ("Different version number");
175 /* Did the number of sections change ? */
176 if (subtable->last_section_number != last_section_number) {
177 GST_DEBUG ("Different last_section_number");
180 /* Finally return whether we saw that section or not */
181 if (!MPEGTS_BIT_IS_SET (subtable->seen_section, section_number)) {
182 GST_DEBUG ("Different section_number");
186 if (stream->section_data) {
187 /* Everything else is the same, fall back to memcmp */
188 return (memcmp (stream->section_data, data_start, to_read) != 0);
194 static MpegTSPacketizerStreamSubtable *
195 mpegts_packetizer_stream_subtable_new (guint8 table_id,
196 guint16 subtable_extension, guint8 last_section_number)
198 MpegTSPacketizerStreamSubtable *subtable;
200 subtable = g_new0 (MpegTSPacketizerStreamSubtable, 1);
201 subtable->version_number = VERSION_NUMBER_UNSET;
202 subtable->table_id = table_id;
203 subtable->subtable_extension = subtable_extension;
204 subtable->last_section_number = last_section_number;
208 static MpegTSPacketizerStream *
209 mpegts_packetizer_stream_new (guint16 pid)
211 MpegTSPacketizerStream *stream;
213 stream = (MpegTSPacketizerStream *) g_new0 (MpegTSPacketizerStream, 1);
214 stream->continuity_counter = CONTINUITY_UNSET;
215 stream->subtables = NULL;
216 stream->table_id = TABLE_ID_UNSET;
222 mpegts_packetizer_clear_section (MpegTSPacketizerStream * stream)
224 stream->continuity_counter = CONTINUITY_UNSET;
225 stream->section_length = 0;
226 stream->section_offset = 0;
227 stream->table_id = TABLE_ID_UNSET;
228 g_free (stream->section_data);
229 stream->section_data = NULL;
233 mpegts_packetizer_stream_subtable_free (MpegTSPacketizerStreamSubtable *
240 mpegts_packetizer_stream_free (MpegTSPacketizerStream * stream)
242 mpegts_packetizer_clear_section (stream);
243 g_slist_foreach (stream->subtables,
244 (GFunc) mpegts_packetizer_stream_subtable_free, NULL);
245 g_slist_free (stream->subtables);
250 mpegts_packetizer_class_init (MpegTSPacketizer2Class * klass)
252 GObjectClass *gobject_class;
254 gobject_class = G_OBJECT_CLASS (klass);
256 gobject_class->dispose = mpegts_packetizer_dispose;
257 gobject_class->finalize = mpegts_packetizer_finalize;
261 mpegts_packetizer_init (MpegTSPacketizer2 * packetizer)
263 g_mutex_init (&packetizer->group_lock);
265 packetizer->adapter = gst_adapter_new ();
266 packetizer->offset = 0;
267 packetizer->empty = TRUE;
268 packetizer->streams = g_new0 (MpegTSPacketizerStream *, 8192);
269 packetizer->packet_size = 0;
270 packetizer->calculate_skew = FALSE;
271 packetizer->calculate_offset = FALSE;
273 packetizer->map_data = NULL;
274 packetizer->map_size = 0;
275 packetizer->map_offset = 0;
276 packetizer->need_sync = FALSE;
278 memset (packetizer->pcrtablelut, 0xff, 0x2000);
279 memset (packetizer->observations, 0x0, sizeof (packetizer->observations));
280 packetizer->lastobsid = 0;
282 packetizer->nb_seen_offsets = 0;
283 packetizer->refoffset = -1;
284 packetizer->last_in_time = GST_CLOCK_TIME_NONE;
285 packetizer->pcr_discont_threshold = GST_SECOND;
286 packetizer->last_pts = GST_CLOCK_TIME_NONE;
287 packetizer->last_dts = GST_CLOCK_TIME_NONE;
288 packetizer->extra_shift = 0;
292 mpegts_packetizer_dispose (GObject * object)
294 MpegTSPacketizer2 *packetizer = GST_MPEGTS_PACKETIZER (object);
296 if (!packetizer->disposed) {
297 if (packetizer->packet_size)
298 packetizer->packet_size = 0;
299 if (packetizer->streams) {
301 for (i = 0; i < 8192; i++) {
302 if (packetizer->streams[i])
303 mpegts_packetizer_stream_free (packetizer->streams[i]);
305 g_free (packetizer->streams);
308 gst_adapter_clear (packetizer->adapter);
309 g_object_unref (packetizer->adapter);
310 g_mutex_clear (&packetizer->group_lock);
311 packetizer->disposed = TRUE;
312 packetizer->offset = 0;
313 packetizer->empty = TRUE;
315 flush_observations (packetizer);
318 if (G_OBJECT_CLASS (mpegts_packetizer_parent_class)->dispose)
319 G_OBJECT_CLASS (mpegts_packetizer_parent_class)->dispose (object);
323 mpegts_packetizer_finalize (GObject * object)
325 if (G_OBJECT_CLASS (mpegts_packetizer_parent_class)->finalize)
326 G_OBJECT_CLASS (mpegts_packetizer_parent_class)->finalize (object);
329 static inline guint64
330 mpegts_packetizer_compute_pcr (const guint8 * data)
334 guint64 pcr, pcr_ext;
336 pcr1 = GST_READ_UINT32_BE (data);
337 pcr2 = GST_READ_UINT16_BE (data + 4);
338 pcr = ((guint64) pcr1) << 1;
339 pcr |= (pcr2 & 0x8000) >> 15;
340 pcr_ext = (pcr2 & 0x01ff);
341 return pcr * 300 + pcr_ext % 300;
345 mpegts_packetizer_parse_adaptation_field_control (MpegTSPacketizer2 *
346 packetizer, MpegTSPacketizerPacket * packet)
348 guint8 length, afcflags;
351 length = *packet->data++;
353 /* an adaptation field with length 0 is valid and
354 * can be used to insert a single stuffing byte */
356 packet->afc_flags = 0;
360 if ((packet->scram_afc_cc & 0x30) == 0x20) {
361 /* no payload, adaptation field of 183 bytes */
363 GST_WARNING ("PID 0x%04x afc == 0x%02x and length %d > 183",
364 packet->pid, packet->scram_afc_cc & 0x30, length);
368 GST_WARNING ("PID 0x%04x afc == 0x%02x and length %d != 183",
369 packet->pid, packet->scram_afc_cc & 0x30, length);
370 GST_MEMDUMP ("Unknown payload", packet->data + length,
371 packet->data_end - packet->data - length);
373 } else if (length == 183) {
374 /* Note: According to the specification, the adaptation field length
375 * must be 183 if there is no payload data and < 183 if the packet
376 * contains an adaptation field and payload data.
377 * Some payloaders always set the flag for payload data, even if the
378 * adaptation field length is 183. This just means a zero length
379 * payload so we clear the payload flag here and continue.
381 GST_DEBUG ("PID 0x%04x afc == 0x%02x and length %d == 183 (ignored)",
382 packet->pid, packet->scram_afc_cc & 0x30, length);
383 packet->scram_afc_cc &= ~0x10;
384 } else if (length > 182) {
385 GST_WARNING ("PID 0x%04x afc == 0x%02x and length %d > 182",
386 packet->pid, packet->scram_afc_cc & 0x30, length);
390 if (packet->data + length > packet->data_end) {
392 ("PID 0x%04x afc length %d overflows the buffer current %d max %d",
393 packet->pid, length, (gint) (packet->data - packet->data_start),
394 (gint) (packet->data_end - packet->data_start));
399 packet->data += length;
401 afcflags = packet->afc_flags = *data++;
403 GST_DEBUG ("flags: %s%s%s%s%s%s%s%s%s",
404 afcflags & 0x80 ? "discontinuity " : "",
405 afcflags & 0x40 ? "random_access " : "",
406 afcflags & 0x20 ? "elementary_stream_priority " : "",
407 afcflags & 0x10 ? "PCR " : "",
408 afcflags & 0x08 ? "OPCR " : "",
409 afcflags & 0x04 ? "splicing_point " : "",
410 afcflags & 0x02 ? "transport_private_data " : "",
411 afcflags & 0x01 ? "extension " : "", afcflags == 0x00 ? "<none>" : "");
414 if (afcflags & MPEGTS_AFC_PCR_FLAG) {
415 MpegTSPCR *pcrtable = NULL;
416 packet->pcr = mpegts_packetizer_compute_pcr (data);
418 GST_DEBUG ("pcr 0x%04x %" G_GUINT64_FORMAT " (%" GST_TIME_FORMAT
419 ") offset:%" G_GUINT64_FORMAT, packet->pid, packet->pcr,
420 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (packet->pcr)), packet->offset);
422 PACKETIZER_GROUP_LOCK (packetizer);
423 if (packetizer->calculate_skew
424 && GST_CLOCK_TIME_IS_VALID (packetizer->last_in_time)) {
425 pcrtable = get_pcr_table (packetizer, packet->pid);
426 calculate_skew (packetizer, pcrtable, packet->pcr,
427 packetizer->last_in_time);
429 if (packetizer->calculate_offset) {
431 pcrtable = get_pcr_table (packetizer, packet->pid);
432 record_pcr (packetizer, pcrtable, packet->pcr, packet->offset);
434 PACKETIZER_GROUP_UNLOCK (packetizer);
436 #ifndef GST_DISABLE_GST_DEBUG
438 if (afcflags & MPEGTS_AFC_OPCR_FLAG) {
439 /* Note: We don't use/need opcr for the time being */
440 guint64 opcr = mpegts_packetizer_compute_pcr (data);
442 GST_DEBUG ("opcr %" G_GUINT64_FORMAT " (%" GST_TIME_FORMAT ")",
443 opcr, GST_TIME_ARGS (PCRTIME_TO_GSTTIME (opcr)));
446 if (afcflags & MPEGTS_AFC_SPLICING_POINT_FLAG) {
447 GST_DEBUG ("splice_countdown: %u", *data++);
450 if (afcflags & MPEGTS_AFC_TRANSPORT_PRIVATE_DATA_FLAG) {
451 guint8 len = *data++;
452 GST_MEMDUMP ("private data", data, len);
456 if (afcflags & MPEGTS_AFC_EXTENSION_FLAG) {
457 guint8 extlen = *data++;
458 guint8 flags = *data++;
459 GST_DEBUG ("extension size:%d flags : %s%s%s", extlen,
460 flags & 0x80 ? "ltw " : "",
461 flags & 0x40 ? "piecewise_rate " : "",
462 flags & 0x20 ? "seamless_splice " : "");
464 GST_DEBUG ("legal time window: valid_flag:%d offset:%d", *data >> 7,
465 GST_READ_UINT16_BE (data) & 0x7fff);
474 static MpegTSPacketizerPacketReturn
475 mpegts_packetizer_parse_packet (MpegTSPacketizer2 * packetizer,
476 MpegTSPacketizerPacket * packet)
481 data = packet->data_start;
485 /* transport_error_indicator 1 */
486 if (G_UNLIKELY (tmp & 0x80))
489 /* payload_unit_start_indicator 1 */
490 packet->payload_unit_start_indicator = tmp & 0x40;
492 /* transport_priority 1 */
494 packet->pid = GST_READ_UINT16_BE (data) & 0x1FFF;
497 packet->scram_afc_cc = tmp = *data++;
498 /* transport_scrambling_control 2 */
499 if (G_UNLIKELY (tmp & 0xc0))
504 packet->afc_flags = 0;
505 packet->pcr = G_MAXUINT64;
507 if (FLAGS_HAS_AFC (tmp)) {
508 if (!mpegts_packetizer_parse_adaptation_field_control (packetizer, packet))
512 if (FLAGS_HAS_PAYLOAD (packet->scram_afc_cc))
513 packet->payload = packet->data;
515 packet->payload = NULL;
520 static GstMpegtsSection *
521 mpegts_packetizer_parse_section_header (MpegTSPacketizer2 * packetizer,
522 MpegTSPacketizerStream * stream)
524 MpegTSPacketizerStreamSubtable *subtable;
525 GstMpegtsSection *res;
528 find_subtable (stream->subtables, stream->table_id,
529 stream->subtable_extension);
531 GST_DEBUG ("Found previous subtable_extension:0x%04x",
532 stream->subtable_extension);
533 if (G_UNLIKELY (stream->version_number != subtable->version_number)) {
534 /* If the version number changed, reset the subtable */
535 subtable->version_number = stream->version_number;
536 subtable->last_section_number = stream->last_section_number;
537 memset (subtable->seen_section, 0, 32);
540 GST_DEBUG ("Appending new subtable_extension: 0x%04x",
541 stream->subtable_extension);
542 subtable = mpegts_packetizer_stream_subtable_new (stream->table_id,
543 stream->subtable_extension, stream->last_section_number);
544 subtable->version_number = stream->version_number;
546 stream->subtables = g_slist_prepend (stream->subtables, subtable);
549 GST_MEMDUMP ("Full section data", stream->section_data,
550 stream->section_length);
551 /* TODO ? : Replace this by an efficient version (where we provide all
552 * pre-parsed header data) */
554 gst_mpegts_section_new (stream->pid, stream->section_data,
555 stream->section_length);
556 stream->section_data = NULL;
557 mpegts_packetizer_clear_section (stream);
560 /* NOTE : Due to the new mpegts-si system, There is a insanely low probability
561 * that we might have gotten a section that was corrupted (i.e. wrong crc)
562 * and that we consider it as seen.
564 * The reason why we consider this as acceptable is because all the previous
565 * checks were already done:
566 * * transport layer checks (DVB)
568 * * continuity counter validation
569 * * subtable validation
570 * * section_number validation
571 * * section_length validation
573 * The probability of this happening vs the overhead of doing CRC checks
574 * on all sections (including those we would not use) is just not worth it.
576 MPEGTS_BIT_SET (subtable->seen_section, stream->section_number);
577 res->offset = stream->offset;
584 mpegts_packetizer_clear (MpegTSPacketizer2 * packetizer)
589 packetizer->packet_size = 0;
591 if (packetizer->streams) {
593 for (i = 0; i < 8192; i++) {
594 if (packetizer->streams[i]) {
595 mpegts_packetizer_stream_free (packetizer->streams[i]);
598 memset (packetizer->streams, 0, 8192 * sizeof (MpegTSPacketizerStream *));
601 gst_adapter_clear (packetizer->adapter);
602 packetizer->offset = 0;
603 packetizer->empty = TRUE;
604 packetizer->need_sync = FALSE;
605 packetizer->map_data = NULL;
606 packetizer->map_size = 0;
607 packetizer->map_offset = 0;
608 packetizer->last_in_time = GST_CLOCK_TIME_NONE;
609 packetizer->last_pts = GST_CLOCK_TIME_NONE;
610 packetizer->last_dts = GST_CLOCK_TIME_NONE;
612 pcrtable = packetizer->observations[packetizer->pcrtablelut[0x1fff]];
614 pcrtable->base_time = GST_CLOCK_TIME_NONE;
616 /* Close current PCR group */
617 PACKETIZER_GROUP_LOCK (packetizer);
619 for (i = 0; i < MAX_PCR_OBS_CHANNELS; i++) {
620 if (packetizer->observations[i])
621 _close_current_group (packetizer->observations[i]);
625 PACKETIZER_GROUP_UNLOCK (packetizer);
629 mpegts_packetizer_flush (MpegTSPacketizer2 * packetizer, gboolean hard)
633 GST_DEBUG ("Flushing");
635 if (packetizer->streams) {
636 for (i = 0; i < 8192; i++) {
637 if (packetizer->streams[i]) {
638 mpegts_packetizer_clear_section (packetizer->streams[i]);
642 gst_adapter_clear (packetizer->adapter);
644 packetizer->offset = 0;
645 packetizer->empty = TRUE;
646 packetizer->need_sync = FALSE;
647 packetizer->map_data = NULL;
648 packetizer->map_size = 0;
649 packetizer->map_offset = 0;
650 packetizer->last_in_time = GST_CLOCK_TIME_NONE;
651 packetizer->last_pts = GST_CLOCK_TIME_NONE;
652 packetizer->last_dts = GST_CLOCK_TIME_NONE;
654 pcrtable = packetizer->observations[packetizer->pcrtablelut[0x1fff]];
656 pcrtable->base_time = GST_CLOCK_TIME_NONE;
658 /* Close current PCR group */
659 PACKETIZER_GROUP_LOCK (packetizer);
660 for (i = 0; i < MAX_PCR_OBS_CHANNELS; i++) {
661 if (packetizer->observations[i])
662 _close_current_group (packetizer->observations[i]);
666 PACKETIZER_GROUP_UNLOCK (packetizer);
669 /* For pull mode seeks in tsdemux the observation must be preserved */
670 flush_observations (packetizer);
675 mpegts_packetizer_remove_stream (MpegTSPacketizer2 * packetizer, gint16 pid)
677 MpegTSPacketizerStream *stream = packetizer->streams[pid];
679 GST_INFO ("Removing stream for PID 0x%04x", pid);
680 mpegts_packetizer_stream_free (stream);
681 packetizer->streams[pid] = NULL;
686 mpegts_packetizer_new (void)
688 MpegTSPacketizer2 *packetizer;
691 GST_MPEGTS_PACKETIZER (g_object_new (GST_TYPE_MPEGTS_PACKETIZER, NULL));
697 mpegts_packetizer_push (MpegTSPacketizer2 * packetizer, GstBuffer * buffer)
700 if (G_UNLIKELY (packetizer->empty)) {
701 packetizer->empty = FALSE;
702 packetizer->offset = GST_BUFFER_OFFSET (buffer);
705 GST_DEBUG ("Pushing %" G_GSIZE_FORMAT " byte from offset %"
706 G_GUINT64_FORMAT, gst_buffer_get_size (buffer),
707 GST_BUFFER_OFFSET (buffer));
708 gst_adapter_push (packetizer->adapter, buffer);
709 /* If the buffer has a valid timestamp, store it - preferring DTS,
710 * which is where upstream arrival times should be stored */
711 ts = GST_BUFFER_DTS_OR_PTS (buffer);
712 if (GST_CLOCK_TIME_IS_VALID (ts))
713 packetizer->last_in_time = ts;
714 packetizer->last_pts = GST_BUFFER_PTS (buffer);
715 packetizer->last_dts = GST_BUFFER_DTS (buffer);
719 mpegts_packetizer_flush_bytes (MpegTSPacketizer2 * packetizer, gsize size)
722 GST_LOG ("flushing %" G_GSIZE_FORMAT " bytes from adapter", size);
723 gst_adapter_flush (packetizer->adapter, size);
726 packetizer->map_data = NULL;
727 packetizer->map_size = 0;
728 packetizer->map_offset = 0;
732 mpegts_packetizer_map (MpegTSPacketizer2 * packetizer, gsize size)
736 if (packetizer->map_size - packetizer->map_offset >= size)
739 mpegts_packetizer_flush_bytes (packetizer, packetizer->map_offset);
741 available = gst_adapter_available (packetizer->adapter);
742 if (available < size)
745 packetizer->map_data =
746 (guint8 *) gst_adapter_map (packetizer->adapter, available);
747 if (!packetizer->map_data)
750 packetizer->map_size = available;
751 packetizer->map_offset = 0;
753 GST_LOG ("mapped %" G_GSIZE_FORMAT " bytes from adapter", available);
759 mpegts_try_discover_packet_size (MpegTSPacketizer2 * packetizer)
764 static const guint psizes[] = {
765 MPEGTS_NORMAL_PACKETSIZE,
766 MPEGTS_M2TS_PACKETSIZE,
767 MPEGTS_DVB_ASI_PACKETSIZE,
768 MPEGTS_ATSC_PACKETSIZE
771 if (!mpegts_packetizer_map (packetizer, 4 * MPEGTS_MAX_PACKETSIZE))
774 size = packetizer->map_size - packetizer->map_offset;
775 data = packetizer->map_data + packetizer->map_offset;
777 for (i = 0; i + 3 * MPEGTS_MAX_PACKETSIZE < size; i++) {
778 /* find a sync byte */
779 if (data[i] != PACKET_SYNC_BYTE)
782 /* check for 4 consecutive sync bytes with each possible packet size */
783 for (j = 0; j < G_N_ELEMENTS (psizes); j++) {
784 guint packet_size = psizes[j];
786 if (data[i + packet_size] == PACKET_SYNC_BYTE &&
787 data[i + 2 * packet_size] == PACKET_SYNC_BYTE &&
788 data[i + 3 * packet_size] == PACKET_SYNC_BYTE) {
789 packetizer->packet_size = packet_size;
796 packetizer->map_offset += i;
798 if (packetizer->packet_size == 0) {
799 GST_DEBUG ("Could not determine packet size in %" G_GSIZE_FORMAT
800 " bytes buffer, flush %" G_GSIZE_FORMAT " bytes", size, i);
801 mpegts_packetizer_flush_bytes (packetizer, packetizer->map_offset);
805 GST_INFO ("have packetsize detected: %u bytes", packetizer->packet_size);
807 if (packetizer->packet_size == MPEGTS_M2TS_PACKETSIZE &&
808 packetizer->map_offset >= 4)
809 packetizer->map_offset -= 4;
815 mpegts_packetizer_sync (MpegTSPacketizer2 * packetizer)
817 gboolean found = FALSE;
820 gsize size, sync_offset, i;
822 packet_size = packetizer->packet_size;
824 if (!mpegts_packetizer_map (packetizer, 3 * packet_size))
827 size = packetizer->map_size - packetizer->map_offset;
828 data = packetizer->map_data + packetizer->map_offset;
830 if (packet_size == MPEGTS_M2TS_PACKETSIZE)
835 for (i = sync_offset; i + 2 * packet_size < size; i++) {
836 if (data[i] == PACKET_SYNC_BYTE &&
837 data[i + packet_size] == PACKET_SYNC_BYTE &&
838 data[i + 2 * packet_size] == PACKET_SYNC_BYTE) {
844 packetizer->map_offset += i - sync_offset;
847 mpegts_packetizer_flush_bytes (packetizer, packetizer->map_offset);
852 MpegTSPacketizerPacketReturn
853 mpegts_packetizer_next_packet (MpegTSPacketizer2 * packetizer,
854 MpegTSPacketizerPacket * packet)
860 packet_size = packetizer->packet_size;
861 if (G_UNLIKELY (!packet_size)) {
862 if (!mpegts_try_discover_packet_size (packetizer))
863 return PACKET_NEED_MORE;
864 packet_size = packetizer->packet_size;
867 /* M2TS packets don't start with the sync byte, all other variants do */
868 if (packet_size == MPEGTS_M2TS_PACKETSIZE)
874 if (packetizer->need_sync) {
875 if (!mpegts_packetizer_sync (packetizer))
876 return PACKET_NEED_MORE;
877 packetizer->need_sync = FALSE;
880 if (!mpegts_packetizer_map (packetizer, packet_size))
881 return PACKET_NEED_MORE;
883 packet_data = &packetizer->map_data[packetizer->map_offset + sync_offset];
885 /* Check sync byte */
886 if (G_UNLIKELY (*packet_data != PACKET_SYNC_BYTE)) {
887 GST_DEBUG ("lost sync");
888 packetizer->need_sync = TRUE;
890 /* ALL mpeg-ts variants contain 188 bytes of data. Those with bigger
891 * packet sizes contain either extra data (timesync, FEC, ..) either
892 * before or after the data */
893 packet->data_start = packet_data;
894 packet->data_end = packet->data_start + 188;
895 packet->offset = packetizer->offset;
896 GST_LOG ("offset %" G_GUINT64_FORMAT, packet->offset);
897 packetizer->offset += packet_size;
898 GST_MEMDUMP ("data_start", packet->data_start, 16);
900 return mpegts_packetizer_parse_packet (packetizer, packet);
905 MpegTSPacketizerPacketReturn
906 mpegts_packetizer_process_next_packet (MpegTSPacketizer2 * packetizer)
908 MpegTSPacketizerPacket packet;
909 MpegTSPacketizerPacketReturn ret;
911 ret = mpegts_packetizer_next_packet (packetizer, &packet);
912 if (ret != PACKET_NEED_MORE)
913 mpegts_packetizer_clear_packet (packetizer, &packet);
919 mpegts_packetizer_clear_packet (MpegTSPacketizer2 * packetizer,
920 MpegTSPacketizerPacket * packet)
922 guint8 packet_size = packetizer->packet_size;
924 if (packetizer->map_data) {
925 packetizer->map_offset += packet_size;
926 if (packetizer->map_size - packetizer->map_offset < packet_size)
927 mpegts_packetizer_flush_bytes (packetizer, packetizer->map_offset);
932 mpegts_packetizer_has_packets (MpegTSPacketizer2 * packetizer)
934 if (G_UNLIKELY (!packetizer->packet_size)) {
935 if (!mpegts_try_discover_packet_size (packetizer))
938 return gst_adapter_available (packetizer->adapter) >= packetizer->packet_size;
942 * Ideally it should just return a section if:
943 * * The section is complete
944 * * The section is valid (sanity checks for length for example)
945 * * The section applies now (current_next_indicator)
946 * * The section is an update or was never seen
948 * The section should be a new GstMpegtsSection:
949 * * properly initialized
950 * * With pid, table_id AND section_type set (move logic from mpegtsbase)
951 * * With data copied into it (yes, minor overhead)
953 * In all other cases it should just return NULL
955 * If more than one section is available, the 'remaining' field will
956 * be set to the beginning of a valid GList containing other sections.
959 mpegts_packetizer_push_section (MpegTSPacketizer2 * packetizer,
960 MpegTSPacketizerPacket * packet, GList ** remaining)
962 GstMpegtsSection *section;
963 GstMpegtsSection *res = NULL;
964 MpegTSPacketizerStream *stream;
965 gboolean long_packet;
966 guint8 pointer = 0, table_id;
967 guint16 subtable_extension;
969 guint section_length;
970 /* data points to the current read location
971 * data_start points to the beginning of the data to accumulate */
972 guint8 *data, *data_start;
974 GList *others = NULL;
975 guint8 version_number, section_number, last_section_number;
978 packet_cc = FLAGS_CONTINUITY_COUNTER (packet->scram_afc_cc);
981 stream = packetizer->streams[packet->pid];
982 if (G_UNLIKELY (stream == NULL)) {
983 if (!packet->payload_unit_start_indicator) {
984 /* Early exit (we need to start with a section start) */
985 GST_DEBUG ("PID 0x%04x waiting for section start", packet->pid);
988 stream = mpegts_packetizer_stream_new (packet->pid);
989 packetizer->streams[packet->pid] = stream;
992 GST_MEMDUMP ("Full packet data", packet->data,
993 packet->data_end - packet->data);
995 /* This function is split into several parts:
997 * Pre checks (packet-wide). Determines where we go next
998 * accumulate_data: store data and check if section is complete
999 * section_start: handle beginning of a section, if needed loop back to
1002 * The trigger that makes the loop stop and return is if:
1003 * 1) We do not have enough data for the current packet
1004 * 2) There is remaining data after a packet which is only made
1005 * of stuffing bytes (0xff).
1007 * Pre-loop checks, related to the whole incoming packet:
1009 * If there is a CC-discont:
1010 * If it is a PUSI, skip the pointer and handle section_start
1011 * If not a PUSI, reset and return nothing
1012 * If there is not a CC-discont:
1014 * If pointer, accumulate that data and check for complete section
1016 * If it is not a PUSI
1017 * Accumulate the expected data and check for complete section
1022 if (packet->payload_unit_start_indicator) {
1024 /* If the pointer is zero, we're guaranteed to be able to handle it */
1027 ("PID 0x%04x PUSI and pointer == 0, skipping straight to section_start parsing",
1029 mpegts_packetizer_clear_section (stream);
1034 if (stream->continuity_counter == CONTINUITY_UNSET ||
1035 (stream->continuity_counter + 1) % 16 != packet_cc) {
1036 if (stream->continuity_counter != CONTINUITY_UNSET)
1037 GST_WARNING ("PID 0x%04x section discontinuity (%d vs %d)", packet->pid,
1038 stream->continuity_counter, packet_cc);
1039 mpegts_packetizer_clear_section (stream);
1040 /* If not a PUSI, not much we can do */
1041 if (!packet->payload_unit_start_indicator) {
1042 GST_LOG ("PID 0x%04x continuity discont/unset and not PUSI, bailing out",
1046 /* If PUSI, skip pointer data and carry on to section start */
1049 GST_LOG ("discont, but PUSI, skipped %d bytes and doing section start",
1054 GST_LOG ("Accumulating data from beginning of packet");
1059 /* If not the beginning of a new section, accumulate what we have */
1060 stream->continuity_counter = packet_cc;
1061 to_read = MIN (stream->section_length - stream->section_offset,
1062 packet->data_end - data_start);
1063 memcpy (stream->section_data + stream->section_offset, data_start, to_read);
1064 stream->section_offset += to_read;
1065 /* Point data to after the data we accumulated */
1066 data = data_start + to_read;
1067 GST_DEBUG ("Appending data (need %d, have %d)", stream->section_length,
1068 stream->section_offset);
1070 /* Check if we have enough */
1071 if (stream->section_offset < stream->section_length) {
1072 GST_DEBUG ("PID 0x%04x, section not complete (Got %d, need %d)",
1073 stream->pid, stream->section_offset, stream->section_length);
1077 /* Small sanity check. We should have collected *exactly* the right amount */
1078 if (G_UNLIKELY (stream->section_offset != stream->section_length))
1079 GST_WARNING ("PID 0x%04x Accumulated too much data (%d vs %d) !",
1080 stream->pid, stream->section_offset, stream->section_length);
1081 GST_DEBUG ("PID 0x%04x Section complete", stream->pid);
1083 if ((section = mpegts_packetizer_parse_section_header (packetizer, stream))) {
1085 others = g_list_append (others, section);
1091 subtable_extension = 0;
1093 last_section_number = 0;
1097 /* FIXME : We need at least 3 bytes (or 8 for long packets) with current algorithm :(
1098 * We might end up losing sections that start across two packets (srsl...) */
1099 if (data > packet->data_end - 3 || *data == 0xff) {
1100 /* flush stuffing bytes and leave */
1101 mpegts_packetizer_clear_section (stream);
1105 /* We have more data to process ... */
1106 GST_DEBUG ("PID 0x%04x, More section present in packet (remaining bytes:%"
1107 G_GSIZE_FORMAT ")", stream->pid, (gsize) (packet->data_end - data));
1108 GST_MEMDUMP ("section_start", data, packet->data_end - data);
1110 /* Beginning of a new section */
1112 * section_syntax_indicator means that the header is of the following format:
1114 * * section_syntax_indicator (1bit) == 0
1115 * * reserved/private fields (3bit)
1116 * * section_length (12bit)
1117 * * data (of size section_length)
1120 long_packet = data[1] & 0x80;
1122 /* Fast path for short packets */
1124 /* We can create the section now (function will check for size) */
1125 GST_DEBUG ("Short packet");
1126 section_length = (GST_READ_UINT16_BE (data + 1) & 0xfff) + 3;
1127 /* Only do fast-path if we have enough byte */
1128 if (data + section_length <= packet->data_end) {
1130 gst_mpegts_section_new (packet->pid, g_memdup2 (data,
1131 section_length), section_length))) {
1132 GST_DEBUG ("PID 0x%04x Short section complete !", packet->pid);
1133 section->offset = packet->offset;
1135 others = g_list_append (others, section);
1139 /* Advance reader and potentially read another section */
1140 data += section_length;
1141 if (data < packet->data_end && *data != 0xff)
1146 /* We don't have enough bytes to do short section shortcut */
1149 /* Beginning of a new section, do as much pre-parsing as possible */
1150 /* table_id : 8 bit */
1153 /* section_syntax_indicator : 1 bit
1154 * other_fields (reserved) : 3 bit
1155 * section_length : 12 bit */
1156 section_length = (GST_READ_UINT16_BE (data) & 0x0FFF) + 3;
1160 /* Do we have enough data for a long packet? */
1161 if (data > packet->data_end - 5)
1164 /* subtable_extension (always present, we are in a long section) */
1165 /* subtable extension : 16 bit */
1166 subtable_extension = GST_READ_UINT16_BE (data);
1170 * version_number : 5 bit
1171 * current_next_indicator : 1 bit */
1172 /* Bail out now if current_next_indicator == 0 */
1173 if (G_UNLIKELY (!(*data & 0x01))) {
1175 ("PID 0x%04x table_id 0x%02x section does not apply (current_next_indicator == 0)",
1176 packet->pid, table_id);
1180 version_number = *data++ >> 1 & 0x1f;
1181 /* section_number : 8 bit */
1182 section_number = *data++;
1183 /* last_section_number : 8 bit */
1184 last_section_number = *data++;
1186 subtable_extension = 0;
1189 last_section_number = 0;
1192 ("PID 0x%04x length:%d table_id:0x%02x subtable_extension:0x%04x version_number:%d section_number:%d(last:%d)",
1193 packet->pid, section_length, table_id, subtable_extension, version_number,
1194 section_number, last_section_number);
1196 to_read = MIN (section_length, packet->data_end - data_start);
1198 /* Check as early as possible whether we already saw this section
1199 * i.e. that we saw a subtable with:
1200 * * same subtable_extension (might be zero)
1201 * * same version_number
1202 * * same last_section_number
1203 * * same section_number was seen
1205 if (seen_section_before (stream, table_id, subtable_extension,
1206 version_number, section_number, last_section_number, data_start,
1209 ("PID 0x%04x Already processed table_id:0x%02x subtable_extension:0x%04x, version_number:%d, section_number:%d",
1210 packet->pid, table_id, subtable_extension, version_number,
1212 /* skip data and see if we have more sections after */
1213 data = data_start + to_read;
1214 if (data == packet->data_end || *data == 0xff)
1218 if (G_UNLIKELY (section_number > last_section_number)) {
1220 ("PID 0x%04x corrupted packet (section_number:%d > last_section_number:%d)",
1221 packet->pid, section_number, last_section_number);
1226 /* Copy over already parsed values */
1227 stream->table_id = table_id;
1228 stream->section_length = section_length;
1229 stream->version_number = version_number;
1230 stream->subtable_extension = subtable_extension;
1231 stream->section_number = section_number;
1232 stream->last_section_number = last_section_number;
1233 stream->offset = packet->offset;
1235 /* Create enough room to store chunks of sections */
1236 stream->section_data = g_malloc (stream->section_length);
1237 stream->section_offset = 0;
1239 /* Finally, accumulate and check if we parsed enough */
1240 goto accumulate_data;
1243 packet->data = data;
1244 *remaining = others;
1246 GST_DEBUG ("result: %p", res);
1254 GST_DEBUG_CATEGORY_INIT (mpegts_packetizer_debug, "mpegtspacketizer", 0,
1255 "MPEG transport stream parser");
1260 mpegts_packetizer_resync (MpegTSPCR * pcr, GstClockTime time,
1261 GstClockTime gstpcrtime, gboolean reset_skew)
1263 pcr->base_time = time;
1264 pcr->base_pcrtime = gstpcrtime;
1265 pcr->prev_out_time = GST_CLOCK_TIME_NONE;
1266 pcr->prev_send_diff = GST_CLOCK_TIME_NONE;
1268 pcr->window_filling = TRUE;
1269 pcr->window_pos = 0;
1270 pcr->window_min = 0;
1271 pcr->window_size = 0;
1277 /* Code mostly copied from -good/gst/rtpmanager/rtpjitterbuffer.c */
1279 /* For the clock skew we use a windowed low point averaging algorithm as can be
1280 * found in Fober, Orlarey and Letz, 2005, "Real Time Clock Skew Estimation
1281 * over Network Delays":
1282 * http://www.grame.fr/Ressources/pub/TR-050601.pdf
1283 * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.102.1546
1285 * The idea is that the jitter is composed of:
1289 * D : a constant network delay.
1290 * n : random added noise. The noise is concentrated around 0
1292 * In the receiver we can track the elapsed time at the sender with:
1294 * send_diff(i) = (Tsi - Ts0);
1296 * Tsi : The time at the sender at packet i
1297 * Ts0 : The time at the sender at the first packet
1299 * This is the difference between the RTP timestamp in the first received packet
1300 * and the current packet.
1302 * At the receiver we have to deal with the jitter introduced by the network.
1304 * recv_diff(i) = (Tri - Tr0)
1306 * Tri : The time at the receiver at packet i
1307 * Tr0 : The time at the receiver at the first packet
1309 * Both of these values contain a jitter Ji, a jitter for packet i, so we can
1312 * recv_diff(i) = (Cri + D + ni) - (Cr0 + D + n0))
1314 * Cri : The time of the clock at the receiver for packet i
1315 * D + ni : The jitter when receiving packet i
1317 * We see that the network delay is irrelevant here as we can eliminate D:
1319 * recv_diff(i) = (Cri + ni) - (Cr0 + n0))
1321 * The drift is now expressed as:
1323 * Drift(i) = recv_diff(i) - send_diff(i);
1325 * We now keep the W latest values of Drift and find the minimum (this is the
1326 * one with the lowest network jitter and thus the one which is least affected
1327 * by it). We average this lowest value to smooth out the resulting network skew.
1329 * Both the window and the weighting used for averaging influence the accuracy
1330 * of the drift estimation. Finding the correct parameters turns out to be a
1331 * compromise between accuracy and inertia.
1333 * We use a 2 second window or up to 512 data points, which is statistically big
1334 * enough to catch spikes (FIXME, detect spikes).
1335 * We also use a rather large weighting factor (125) to smoothly adapt. During
1336 * startup, when filling the window, we use a parabolic weighting factor, the
1337 * more the window is filled, the faster we move to the detected possible skew.
1339 * Returns: @time adjusted with the clock skew.
1342 calculate_skew (MpegTSPacketizer2 * packetizer,
1343 MpegTSPCR * pcr, guint64 pcrtime, GstClockTime time)
1345 guint64 send_diff, recv_diff;
1349 GstClockTime gstpcrtime, out_time;
1350 #ifndef GST_DISABLE_GST_DEBUG
1354 gstpcrtime = PCRTIME_TO_GSTTIME (pcrtime) + pcr->pcroffset;
1356 /* first time, lock on to time and gstpcrtime */
1357 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (pcr->base_time))) {
1358 pcr->base_time = time;
1359 pcr->prev_out_time = GST_CLOCK_TIME_NONE;
1360 GST_DEBUG ("Taking new base time %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
1363 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (pcr->base_pcrtime))) {
1364 pcr->base_pcrtime = gstpcrtime;
1365 pcr->prev_send_diff = -1;
1366 GST_DEBUG ("Taking new base pcrtime %" GST_TIME_FORMAT,
1367 GST_TIME_ARGS (gstpcrtime));
1370 /* Handle PCR wraparound and resets */
1371 if (GST_CLOCK_TIME_IS_VALID (pcr->last_pcrtime) &&
1372 gstpcrtime < pcr->last_pcrtime) {
1373 if (pcr->last_pcrtime - gstpcrtime > PCR_GST_MAX_VALUE / 2) {
1374 /* PCR wraparound */
1375 GST_DEBUG ("PCR wrap");
1376 pcr->pcroffset += PCR_GST_MAX_VALUE;
1377 gstpcrtime = PCRTIME_TO_GSTTIME (pcrtime) + pcr->pcroffset;
1378 send_diff = gstpcrtime - pcr->base_pcrtime;
1379 } else if (GST_CLOCK_TIME_IS_VALID (time)
1380 && pcr->last_pcrtime - gstpcrtime > 15 * GST_SECOND) {
1381 /* Time jumped backward by > 15 seconds, and we have a timestamp
1382 * to use to close the discont. Assume a reset */
1383 GST_DEBUG ("PCR reset");
1384 /* Calculate PCR we would have expected for the given input time,
1385 * essentially applying the reverse correction process
1387 * We want to find the PCR offset to apply
1388 * pcroffset = (corrected) gstpcrtime - (received) gstpcrtime
1390 * send_diff = (corrected) gstpcrtime - pcr->base_pcrtime
1391 * recv_diff = time - pcr->base_time
1392 * out_time = pcr->base_time + send_diff
1394 * We are assuming that send_diff == recv_diff
1395 * (corrected) gstpcrtime - pcr->base_pcrtime = time - pcr->base_time
1397 * (corrected) gstpcrtime = time - pcr->base_time + pcr->base_pcrtime
1400 * pcroffset = time - pcr->base_time + pcr->base_pcrtime - (received) gstpcrtime
1402 pcr->pcroffset += time - pcr->base_time + pcr->base_pcrtime - gstpcrtime;
1403 gstpcrtime = PCRTIME_TO_GSTTIME (pcrtime) + pcr->pcroffset;
1404 send_diff = gstpcrtime - pcr->base_pcrtime;
1405 GST_DEBUG ("Introduced offset is now %" GST_TIME_FORMAT
1406 " corrected pcr time %" GST_TIME_FORMAT,
1407 GST_TIME_ARGS (pcr->pcroffset), GST_TIME_ARGS (gstpcrtime));
1409 /* Small jumps backward, assume some arrival jitter and skip it */
1412 /* The following code are the different ways we deal with small-ish
1413 * jitter, ranging in severity from "can be ignored" to "this needs a full
1416 if (time == pcr->base_time) {
1417 /* If this comes from a non-fully-timestamped source (i.e. adaptive
1418 * streams), then cope with the fact that some producers generate utter
1419 * PCR garbage on fragment ends.
1421 * We detect this comes from a non-fully-timestamped source by the fact
1422 * that the buffer time never changes */
1423 GST_DEBUG ("Ignoring PCR resets on non-fully timestamped stream");
1424 } else if (pcr->last_pcrtime - gstpcrtime < GST_SECOND) {
1426 ("(small) backward timestamps at server or no buffer timestamps. Ignoring.");
1427 /* This will trigger the no_skew logic before but leave other state
1429 time = GST_CLOCK_TIME_NONE;
1431 /* A bigger backward step than packet out-of-order can account for. Reset base PCR time
1432 * to be resynched the next time we see a PCR */
1434 ("backward timestamps at server or no buffer timestamps. Resync base PCR");
1435 pcr->base_pcrtime = GST_CLOCK_TIME_NONE;
1439 send_diff = gstpcrtime - pcr->base_pcrtime;
1441 GST_DEBUG ("gstpcr %" GST_TIME_FORMAT ", buftime %" GST_TIME_FORMAT
1442 ", base %" GST_TIME_FORMAT ", send_diff %" GST_TIME_FORMAT,
1443 GST_TIME_ARGS (gstpcrtime), GST_TIME_ARGS (time),
1444 GST_TIME_ARGS (pcr->base_pcrtime), GST_TIME_ARGS (send_diff));
1446 /* keep track of the last extended pcrtime */
1447 pcr->last_pcrtime = gstpcrtime;
1449 /* we don't have an arrival timestamp so we can't do skew detection. we
1450 * should still apply a timestamp based on RTP timestamp and base_time */
1451 if (!GST_CLOCK_TIME_IS_VALID (time)
1452 || !GST_CLOCK_TIME_IS_VALID (pcr->base_time))
1455 /* elapsed time at receiver, includes the jitter */
1456 recv_diff = time - pcr->base_time;
1458 /* Ignore packets received at 100% the same time (i.e. from the same input buffer) */
1459 if (G_UNLIKELY (time == pcr->prev_in_time
1460 && GST_CLOCK_TIME_IS_VALID (pcr->prev_in_time)))
1463 /* measure the diff */
1464 delta = ((gint64) recv_diff) - ((gint64) send_diff);
1466 #ifndef GST_DISABLE_GST_DEBUG
1467 /* measure the slope, this gives a rought estimate between the sender speed
1468 * and the receiver speed. This should be approximately 8, higher values
1469 * indicate a burst (especially when the connection starts) */
1470 slope = recv_diff > 0 ? (send_diff * 8) / recv_diff : 8;
1473 GST_DEBUG ("time %" GST_TIME_FORMAT ", base %" GST_TIME_FORMAT
1474 ", recv_diff %" GST_TIME_FORMAT ", slope %" G_GUINT64_FORMAT,
1475 GST_TIME_ARGS (time), GST_TIME_ARGS (pcr->base_time),
1476 GST_TIME_ARGS (recv_diff), slope);
1478 /* if the difference between the sender timeline and the receiver timeline
1479 * changed too quickly we have to resync because the server likely restarted
1480 * its timestamps. */
1481 if (ABS (delta - pcr->skew) > packetizer->pcr_discont_threshold) {
1482 GST_WARNING ("delta - skew: %" GST_STIME_FORMAT " too big, reset skew",
1483 GST_STIME_ARGS (delta - pcr->skew));
1484 mpegts_packetizer_resync (pcr, time, gstpcrtime, TRUE);
1489 pos = pcr->window_pos;
1491 if (G_UNLIKELY (pcr->window_filling)) {
1492 /* we are filling the window */
1493 GST_DEBUG ("filling %d, delta %" G_GINT64_FORMAT, pos, delta);
1494 pcr->window[pos++] = delta;
1495 /* calc the min delta we observed */
1496 if (G_UNLIKELY (pos == 1 || delta < pcr->window_min))
1497 pcr->window_min = delta;
1499 if (G_UNLIKELY (send_diff >= MAX_TIME || pos >= MAX_WINDOW)) {
1500 pcr->window_size = pos;
1503 GST_DEBUG ("min %" G_GINT64_FORMAT, pcr->window_min);
1505 /* the skew is now the min */
1506 pcr->skew = pcr->window_min;
1507 pcr->window_filling = FALSE;
1509 gint perc_time, perc_window, perc;
1511 /* figure out how much we filled the window, this depends on the amount of
1512 * time we have or the max number of points we keep. */
1513 perc_time = send_diff * 100 / MAX_TIME;
1514 perc_window = pos * 100 / MAX_WINDOW;
1515 perc = MAX (perc_time, perc_window);
1517 /* make a parabolic function, the closer we get to the MAX, the more value
1518 * we give to the scaling factor of the new value */
1521 /* quickly go to the min value when we are filling up, slowly when we are
1522 * just starting because we're not sure it's a good value yet. */
1524 (perc * pcr->window_min + ((10000 - perc) * pcr->skew)) / 10000;
1525 pcr->window_size = pos + 1;
1528 /* pick old value and store new value. We keep the previous value in order
1529 * to quickly check if the min of the window changed */
1530 old = pcr->window[pos];
1531 pcr->window[pos++] = delta;
1533 if (G_UNLIKELY (delta <= pcr->window_min)) {
1534 /* if the new value we inserted is smaller or equal to the current min,
1535 * it becomes the new min */
1536 pcr->window_min = delta;
1537 } else if (G_UNLIKELY (old == pcr->window_min)) {
1538 gint64 min = G_MAXINT64;
1540 /* if we removed the old min, we have to find a new min */
1541 for (i = 0; i < pcr->window_size; i++) {
1542 /* we found another value equal to the old min, we can stop searching now */
1543 if (pcr->window[i] == old) {
1547 if (pcr->window[i] < min)
1548 min = pcr->window[i];
1550 pcr->window_min = min;
1552 /* average the min values */
1553 pcr->skew = (pcr->window_min + (124 * pcr->skew)) / 125;
1554 GST_DEBUG ("delta %" G_GINT64_FORMAT ", new min: %" G_GINT64_FORMAT,
1555 delta, pcr->window_min);
1557 /* wrap around in the window */
1558 if (G_UNLIKELY (pos >= pcr->window_size))
1561 pcr->window_pos = pos;
1564 /* the output time is defined as the base timestamp plus the PCR time
1565 * adjusted for the clock skew .*/
1566 if (pcr->base_time != -1) {
1567 out_time = pcr->base_time + send_diff;
1568 /* skew can be negative and we don't want to make invalid timestamps */
1569 if (pcr->skew < 0 && out_time < -pcr->skew) {
1572 out_time += pcr->skew;
1574 /* check if timestamps are not going backwards, we can only check this if we
1575 * have a previous out time and a previous send_diff */
1576 if (G_LIKELY (pcr->prev_out_time != -1 && pcr->prev_send_diff != -1)) {
1577 /* now check for backwards timestamps */
1579 /* if the server timestamps went up and the out_time backwards */
1580 (send_diff > pcr->prev_send_diff
1581 && out_time < pcr->prev_out_time) ||
1582 /* if the server timestamps went backwards and the out_time forwards */
1583 (send_diff < pcr->prev_send_diff
1584 && out_time > pcr->prev_out_time) ||
1585 /* if the server timestamps did not change */
1586 send_diff == pcr->prev_send_diff)) {
1587 GST_DEBUG ("backwards timestamps, using previous time");
1588 out_time = GSTTIME_TO_MPEGTIME (out_time);
1592 /* We simply use the pcrtime without applying any skew compensation */
1596 pcr->prev_out_time = out_time;
1597 pcr->prev_in_time = time;
1598 pcr->prev_send_diff = send_diff;
1600 GST_DEBUG ("skew %" G_GINT64_FORMAT ", out %" GST_TIME_FORMAT,
1601 pcr->skew, GST_TIME_ARGS (out_time));
1607 _reevaluate_group_pcr_offset (MpegTSPCR * pcrtable, PCROffsetGroup * group)
1609 PCROffsetGroup *prev = NULL;
1610 #ifndef GST_DISABLE_GST_DEBUG
1611 PCROffsetGroup *first = pcrtable->groups->data;
1613 PCROffsetCurrent *current = pcrtable->current;
1616 /* Go over all ESTIMATED groups until the target group */
1617 for (tmp = pcrtable->groups; tmp; tmp = tmp->next) {
1618 PCROffsetGroup *cur = (PCROffsetGroup *) tmp->data;
1620 /* Skip groups that don't need re-evaluation */
1621 if (!(cur->flags & PCR_GROUP_FLAG_ESTIMATED)) {
1622 GST_DEBUG ("Skipping group %p pcr_offset (currently %" GST_TIME_FORMAT
1623 ")", cur, GST_TIME_ARGS (PCRTIME_TO_GSTTIME (cur->pcr_offset)));
1628 /* This should not happen ! The first group is *always* correct (zero) */
1629 if (G_UNLIKELY (prev == NULL)) {
1630 GST_ERROR ("First PCR Group was not estimated (bug). Setting to zero");
1631 cur->pcr_offset = 0;
1632 cur->flags &= ~PCR_GROUP_FLAG_ESTIMATED;
1636 /* Finally do the estimation of this group's PCR offset based on the
1637 * previous group information */
1639 GST_DEBUG ("Re-evaluating group %p pcr_offset (currently %" GST_TIME_FORMAT
1640 ")", group, GST_TIME_ARGS (PCRTIME_TO_GSTTIME (cur->pcr_offset)));
1642 GST_DEBUG ("cur->first_pcr:%" GST_TIME_FORMAT " prev->first_pcr:%"
1643 GST_TIME_FORMAT, GST_TIME_ARGS (PCRTIME_TO_GSTTIME (cur->first_pcr)),
1644 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (prev->first_pcr)));
1646 if (G_UNLIKELY (cur->first_pcr < prev->first_pcr)) {
1647 guint64 prevbr, lastbr;
1649 guint64 prevoffset, lastoffset;
1651 /* Take the previous group pcr_offset and figure out how much to add
1652 * to it for the current group */
1654 /* Right now we do a dumb bitrate estimation
1655 * estimate bitrate (prev - first) : bitrate from the start
1656 * estimate bitrate (prev) : bitrate of previous group
1657 * estimate bitrate (last - first) : bitrate from previous group
1659 * We will use raw (non-corrected/non-absolute) PCR values in a first time
1660 * to detect wraparound/resets/gaps...
1662 * We will use the corrected/absolute PCR values to calculate
1663 * bitrate and estimate the target group pcr_offset.
1666 /* If the current window estimator is over the previous group, used those
1667 * values as the latest (since they are more recent) */
1668 if (current->group == prev && current->pending[current->last].offset) {
1670 current->pending[current->last].offset + prev->first_offset;
1671 prevpcr = current->pending[current->last].pcr + prev->first_pcr;
1672 /* prevbr: bitrate(prev) */
1674 gst_util_uint64_scale (PCR_SECOND,
1675 current->pending[current->last].offset,
1676 current->pending[current->last].pcr);
1677 GST_DEBUG ("Previous group bitrate (%" G_GUINT64_FORMAT " / %"
1678 GST_TIME_FORMAT ") : %" G_GUINT64_FORMAT,
1679 current->pending[current->last].offset,
1680 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (current->pending[current->last].
1682 } else if (prev->values[prev->last_value].offset) {
1683 prevoffset = prev->values[prev->last_value].offset + prev->first_offset;
1684 prevpcr = prev->values[prev->last_value].pcr + prev->first_pcr;
1685 /* prevbr: bitrate(prev) (FIXME : Cache) */
1687 gst_util_uint64_scale (PCR_SECOND,
1688 prev->values[prev->last_value].offset,
1689 prev->values[prev->last_value].pcr);
1690 GST_DEBUG ("Previous group bitrate (%" G_GUINT64_FORMAT " / %"
1691 GST_TIME_FORMAT ") : %" G_GUINT64_FORMAT,
1692 prev->values[prev->last_value].offset,
1693 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (prev->values[prev->last_value].
1696 GST_DEBUG ("Using overall bitrate");
1697 prevoffset = prev->values[prev->last_value].offset + prev->first_offset;
1698 prevpcr = prev->values[prev->last_value].pcr + prev->first_pcr;
1699 prevbr = gst_util_uint64_scale (PCR_SECOND,
1700 prev->first_offset, prev->pcr_offset);
1702 lastoffset = cur->values[cur->last_value].offset + cur->first_offset;
1704 GST_DEBUG ("Offset first:%" G_GUINT64_FORMAT " prev:%" G_GUINT64_FORMAT
1705 " cur:%" G_GUINT64_FORMAT, first->first_offset, prevoffset,
1707 GST_DEBUG ("PCR first:%" GST_TIME_FORMAT " prev:%" GST_TIME_FORMAT
1708 " cur:%" GST_TIME_FORMAT,
1709 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (first->first_pcr)),
1710 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (prevpcr)),
1711 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (cur->values[cur->last_value].pcr +
1714 if (prevpcr - cur->first_pcr > (PCR_MAX_VALUE * 9 / 10)) {
1716 guint64 guess_offset;
1718 /* Let's assume there is a PCR wraparound between the previous and current
1720 * [ prev ]... PCR_MAX | 0 ...[ current ]
1721 * The estimated pcr_offset would therefore be:
1722 * current.first + (PCR_MAX_VALUE - prev.first)
1724 * 1) Check if bitrate(prev) would be consistent with bitrate (cur - prev)
1726 guess_offset = PCR_MAX_VALUE - prev->first_pcr + cur->first_pcr;
1727 lastbr = gst_util_uint64_scale (PCR_SECOND, lastoffset - prevoffset,
1728 guess_offset + cur->values[cur->last_value].pcr - (prevpcr -
1730 GST_DEBUG ("Wraparound prev-cur (guess_offset:%" GST_TIME_FORMAT
1731 ") bitrate:%" G_GUINT64_FORMAT,
1732 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (guess_offset)), lastbr);
1733 diffprev = (float) 100.0 *(ABSDIFF (prevbr, lastbr)) / (float) prevbr;
1734 GST_DEBUG ("Difference with previous bitrate:%f", diffprev);
1735 if (diffprev < 10.0) {
1736 GST_DEBUG ("Difference < 10.0, Setting pcr_offset to %"
1737 G_GUINT64_FORMAT, guess_offset);
1738 cur->pcr_offset = guess_offset;
1739 if (diffprev < 1.0) {
1740 GST_DEBUG ("Difference < 1.0, Removing ESTIMATED flags");
1741 cur->flags &= ~PCR_GROUP_FLAG_ESTIMATED;
1744 /* Indicate the the previous group is before a wrapover */
1745 prev->flags |= PCR_GROUP_FLAG_WRAPOVER;
1748 /* Let's assume there was a PCR reset between the previous and current
1750 * [ prev ] ... x | x - reset ... [ current ]
1752 * The estimated pcr_offset would then be
1753 * = current.first - (x - reset) + (x - prev.first) + 100ms (for safety)
1754 * = current.first + reset - prev.first + 100ms (for safety)
1756 /* In order to calculate the reset, we estimate what the PCR would have
1757 * been by using prevbr */
1758 /* FIXME : Which bitrate should we use ??? */
1759 GST_DEBUG ("Using prevbr:%" G_GUINT64_FORMAT " and taking offsetdiff:%"
1760 G_GUINT64_FORMAT, prevbr, cur->first_offset - prev->first_offset);
1762 gst_util_uint64_scale (PCR_SECOND,
1763 cur->first_offset - prev->first_offset, prevbr);
1764 GST_DEBUG ("Estimated full PCR for offset %" G_GUINT64_FORMAT
1766 GST_TIME_FORMAT, cur->first_offset,
1767 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (resetprev)));
1768 cur->pcr_offset = prev->pcr_offset + resetprev + 100 * PCR_MSECOND;
1769 GST_DEBUG ("Adjusted group PCR_offset to %" GST_TIME_FORMAT,
1770 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (cur->pcr_offset)));
1771 /* Indicate the the previous group is before a reset */
1772 prev->flags |= PCR_GROUP_FLAG_RESET;
1775 /* FIXME : Detect gaps if bitrate difference is really too big ? */
1776 cur->pcr_offset = prev->pcr_offset + cur->first_pcr - prev->first_pcr;
1777 GST_DEBUG ("Assuming there is no gap, setting pcr_offset to %"
1779 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (cur->pcr_offset)));
1780 /* Remove the reset and wrapover flag (if it was previously there) */
1781 prev->flags &= ~PCR_GROUP_FLAG_RESET;
1782 prev->flags &= ~PCR_GROUP_FLAG_WRAPOVER;
1786 /* Remember prev for the next group evaluation */
1791 static PCROffsetGroup *
1792 _new_group (guint64 pcr, guint64 offset, guint64 pcr_offset, guint flags)
1794 PCROffsetGroup *group = g_slice_new0 (PCROffsetGroup);
1796 GST_DEBUG ("Input PCR %" GST_TIME_FORMAT " offset:%" G_GUINT64_FORMAT
1797 " pcr_offset:%" G_GUINT64_FORMAT " flags:%d",
1798 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (pcr)), offset, pcr_offset, flags);
1800 group->flags = flags;
1801 group->values = g_new0 (PCROffset, DEFAULT_ALLOCATED_OFFSET);
1802 /* The first pcr/offset diff is always 0/0 */
1803 group->values[0].pcr = group->values[0].offset = 0;
1804 group->nb_allocated = DEFAULT_ALLOCATED_OFFSET;
1806 /* Store the full values */
1807 group->first_pcr = pcr;
1808 group->first_offset = offset;
1809 group->pcr_offset = pcr_offset;
1811 GST_DEBUG ("Created group starting with pcr:%" GST_TIME_FORMAT " offset:%"
1812 G_GUINT64_FORMAT " pcr_offset:%" GST_TIME_FORMAT,
1813 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->first_pcr)),
1814 group->first_offset,
1815 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->pcr_offset)));
1821 _insert_group_after (MpegTSPCR * pcrtable, PCROffsetGroup * group,
1822 PCROffsetGroup * prev)
1826 pcrtable->groups = g_list_prepend (pcrtable->groups, group);
1828 GList *tmp, *toinsert, *prevlist = NULL, *nextlist = NULL;
1829 /* Insert before next and prev */
1830 for (tmp = pcrtable->groups; tmp; tmp = tmp->next) {
1831 if (tmp->data == prev) {
1833 nextlist = tmp->next;
1838 /* The non NULL prev given isn't in the list */
1839 GST_WARNING ("Request to insert before a group which isn't in the list");
1840 pcrtable->groups = g_list_prepend (pcrtable->groups, group);
1842 toinsert = g_list_append (NULL, group);
1843 toinsert->next = nextlist;
1844 toinsert->prev = prevlist;
1845 prevlist->next = toinsert;
1847 nextlist->prev = toinsert;
1853 _use_group (MpegTSPCR * pcrtable, PCROffsetGroup * group)
1855 PCROffsetCurrent *current = pcrtable->current;
1857 memset (current, 0, sizeof (PCROffsetCurrent));
1858 current->group = group;
1859 current->pending[0] = group->values[group->last_value];
1860 current->last_value = current->pending[0];
1862 current->prev = group->values[group->last_value];
1863 current->first_pcr = group->first_pcr;
1864 current->first_offset = group->first_offset;
1867 /* Create a new group with the specified values after prev
1868 * Set current to that new group */
1870 _set_current_group (MpegTSPCR * pcrtable,
1871 PCROffsetGroup * prev, guint64 pcr, guint64 offset, gboolean contiguous)
1873 PCROffsetGroup *group;
1875 guint64 pcr_offset = 0;
1877 /* Handle wraparound/gap (only if contiguous with previous group) */
1879 guint64 lastpcr = prev->first_pcr + prev->values[prev->last_value].pcr;
1881 /* Set CLOSED flag on previous group and remember pcr_offset */
1882 prev->flags |= PCR_GROUP_FLAG_CLOSED;
1883 pcr_offset = prev->pcr_offset;
1886 if (lastpcr > pcr) {
1887 /* In offset-mode, a PCR wraparound is only actually consistent if
1888 * we have a very high confidence (99% right now, might need to change
1890 if (lastpcr - pcr > (PCR_MAX_VALUE * 99 / 100)) {
1891 GST_WARNING ("WRAPAROUND detected. diff %" GST_TIME_FORMAT,
1892 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (lastpcr - pcr)));
1893 /* The previous group closed at PCR_MAX_VALUE */
1894 pcr_offset += PCR_MAX_VALUE - prev->first_pcr + pcr;
1896 GST_WARNING ("RESET detected. diff %" GST_TIME_FORMAT,
1897 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (lastpcr - pcr)));
1898 /* The previous group closed at the raw last_pcr diff (+100ms for safety) */
1899 pcr_offset += prev->values[prev->last_value].pcr + 100 * PCR_MSECOND;
1901 } else if (lastpcr < pcr - 500 * PCR_MSECOND) {
1902 GST_WARNING ("GAP detected. diff %" GST_TIME_FORMAT,
1903 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (pcr - lastpcr)));
1904 /* The previous group closed at the raw last_pcr diff (+500ms for safety) */
1905 pcr_offset += prev->values[prev->last_value].pcr + 500 * PCR_MSECOND;
1907 /* Normal continuation (contiguous in time) */
1908 pcr_offset += pcr - prev->first_pcr;
1910 } else if (prev != NULL)
1911 /* If we are not contiguous and it's not the first group, the pcr_offset
1912 * will be estimated */
1913 flags = PCR_GROUP_FLAG_ESTIMATED;
1915 group = _new_group (pcr, offset, pcr_offset, flags);
1916 _use_group (pcrtable, group);
1917 _insert_group_after (pcrtable, group, prev);
1919 _reevaluate_group_pcr_offset (pcrtable, group);
1923 _append_group_values (PCROffsetGroup * group, PCROffset pcroffset)
1925 /* Only append if new values */
1926 if (group->values[group->last_value].offset == pcroffset.offset &&
1927 group->values[group->last_value].pcr == pcroffset.pcr) {
1928 GST_DEBUG ("Same values, ignoring");
1930 group->last_value++;
1931 /* Resize values if needed */
1932 if (G_UNLIKELY (group->nb_allocated == group->last_value)) {
1933 group->nb_allocated += DEFAULT_ALLOCATED_OFFSET;
1935 g_realloc (group->values, group->nb_allocated * sizeof (PCROffset));
1937 group->values[group->last_value] = pcroffset;
1940 GST_DEBUG ("First PCR:%" GST_TIME_FORMAT " offset:%" G_GUINT64_FORMAT
1941 " PCR_offset:%" GST_TIME_FORMAT,
1942 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->first_pcr)),
1943 group->first_offset,
1944 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->pcr_offset)));
1945 GST_DEBUG ("Last PCR: +%" GST_TIME_FORMAT " offset: +%" G_GUINT64_FORMAT,
1946 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (pcroffset.pcr)), pcroffset.offset);
1949 /* Move last values from current (if any) to the current group
1950 * and reset current.
1951 * Note: This does not set the CLOSED flag (since we have no next
1952 * contiguous group) */
1954 _close_current_group (MpegTSPCR * pcrtable)
1956 PCROffsetCurrent *current = pcrtable->current;
1957 PCROffsetGroup *group = current->group;
1961 GST_DEBUG ("Closing group and resetting current");
1963 /* Store last values */
1964 _append_group_values (group, current->pending[current->last]);
1965 memset (current, 0, sizeof (PCROffsetCurrent));
1966 /* And re-evaluate all groups */
1970 record_pcr (MpegTSPacketizer2 * packetizer, MpegTSPCR * pcrtable,
1971 guint64 pcr, guint64 offset)
1973 PCROffsetCurrent *current = pcrtable->current;
1974 gint64 corpcr, coroffset;
1976 packetizer->nb_seen_offsets += 1;
1978 pcrtable->last_pcrtime = PCRTIME_TO_GSTTIME (pcr);
1979 /* FIXME : Invert logic later (probability is higher that we have a
1980 * current estimator) */
1982 /* Check for current */
1983 if (G_UNLIKELY (current->group == NULL)) {
1984 PCROffsetGroup *prev = NULL;
1986 /* No current estimator. This happens for the initial value, or after
1987 * discont and flushes. Figure out where we need to record this position.
1990 * 1) No groups at all:
1991 * Create a new group with pcr/offset
1992 * Initialize current to that group
1993 * 2) Entirely within an existing group
1994 * bail out (FIXME: Make this detection faster)
1995 * 3) Not in any group
1996 * Create a new group with pcr/offset at the right position
1997 * Initialize current to that group
1999 GST_DEBUG ("No current window estimator, Checking for group to use");
2000 for (tmp = pcrtable->groups; tmp; tmp = tmp->next) {
2001 PCROffsetGroup *group = (PCROffsetGroup *) tmp->data;
2003 GST_DEBUG ("First PCR:%" GST_TIME_FORMAT " offset:%" G_GUINT64_FORMAT
2004 " PCR_offset:%" GST_TIME_FORMAT,
2005 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->first_pcr)),
2006 group->first_offset,
2007 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->pcr_offset)));
2008 GST_DEBUG ("Last PCR: +%" GST_TIME_FORMAT " offset: +%" G_GUINT64_FORMAT,
2009 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->values[group->last_value].
2010 pcr)), group->values[group->last_value].offset);
2011 /* Check if before group */
2012 if (offset < group->first_offset) {
2013 GST_DEBUG ("offset is before that group");
2016 /* Check if within group */
2018 (group->values[group->last_value].offset + group->first_offset)) {
2019 GST_DEBUG ("Already observed PCR offset %" G_GUINT64_FORMAT, offset);
2022 /* Check if just after group (i.e. continuation of it) */
2023 if (!(group->flags & PCR_GROUP_FLAG_CLOSED) &&
2024 pcr - group->first_pcr - group->values[group->last_value].pcr <=
2025 100 * PCR_MSECOND) {
2026 GST_DEBUG ("Continuation of existing group");
2027 _use_group (pcrtable, group);
2030 /* Else after group */
2033 _set_current_group (pcrtable, prev, pcr, offset, FALSE);
2037 corpcr = pcr - current->first_pcr;
2038 coroffset = offset - current->first_offset;
2040 /* FIXME : Detect if we've gone into the next group !
2041 * FIXME : Close group when that happens */
2042 GST_DEBUG ("first:%d, last:%d, write:%d", current->first, current->last,
2044 GST_DEBUG ("First PCR:%" GST_TIME_FORMAT " offset:%" G_GUINT64_FORMAT,
2045 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (current->first_pcr)),
2046 current->first_offset);
2047 GST_DEBUG ("Last PCR: +%" GST_TIME_FORMAT " offset: +%" G_GUINT64_FORMAT,
2048 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (current->pending[current->last].pcr)),
2049 current->pending[current->last].offset);
2050 GST_DEBUG ("To add (corrected) PCR:%" GST_TIME_FORMAT " offset:%"
2051 G_GINT64_FORMAT, GST_TIME_ARGS (PCRTIME_TO_GSTTIME (corpcr)), coroffset);
2053 /* Do we need to close the current group ? */
2054 /* Check for wrapover/discont */
2055 if (G_UNLIKELY (corpcr < current->pending[current->last].pcr)) {
2056 /* FIXME : ignore very small deltas (< 500ms ?) which are most likely
2059 ("PCR smaller than previously observed one, handling discont/wrapover");
2060 /* Take values from current and put them in the current group (closing it) */
2061 /* Create new group with new pcr/offset just after the current group
2062 * and mark it as a wrapover */
2063 /* Initialize current to that group with new values */
2064 _append_group_values (current->group, current->pending[current->last]);
2065 _set_current_group (pcrtable, current->group, pcr, offset, TRUE);
2068 /* If PCR diff is greater than 500ms, create new group */
2069 if (G_UNLIKELY (corpcr - current->pending[current->last].pcr >
2070 500 * PCR_MSECOND)) {
2071 GST_DEBUG ("New PCR more than 500ms away, handling discont");
2072 /* Take values from current and put them in the current group (closing it) */
2073 /* Create new group with pcr/offset just after the current group
2074 * and mark it as a discont */
2075 /* Initialize current to that group with new values */
2076 _append_group_values (current->group, current->pending[current->last]);
2077 _set_current_group (pcrtable, current->group, pcr, offset, TRUE);
2081 if (G_UNLIKELY (corpcr == current->last_value.pcr)) {
2082 GST_DEBUG ("Ignoring same PCR (stream is drunk)");
2086 /* update current window */
2087 current->pending[current->write].pcr = corpcr;
2088 current->pending[current->write].offset = coroffset;
2089 current->last_value = current->pending[current->write];
2090 current->last = (current->last + 1) % PCR_BITRATE_NEEDED;
2091 current->write = (current->write + 1) % PCR_BITRATE_NEEDED;
2093 GST_DEBUG ("first:%d, last:%d, write:%d", current->first, current->last,
2095 GST_DEBUG ("First PCR:%" GST_TIME_FORMAT " offset:%" G_GUINT64_FORMAT,
2096 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (current->first_pcr)),
2097 current->first_offset);
2098 GST_DEBUG ("Last PCR: +%" GST_TIME_FORMAT " offset: +%" G_GUINT64_FORMAT,
2099 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (current->pending[current->last].pcr)),
2100 current->pending[current->last].offset);
2102 /* If we haven't stored enough values, bail out */
2103 if (current->write != current->first) {
2105 ("Not enough observations to calculate bitrate (first:%d, last:%d)",
2106 current->first, current->last);
2110 /* If we are at least 1s away from reference value AND we have filled our
2111 * window, we can start comparing bitrates */
2112 if (current->pending[current->first].pcr - current->prev.pcr > PCR_SECOND) {
2113 /* Calculate window bitrate */
2114 current->cur_bitrate = gst_util_uint64_scale (PCR_SECOND,
2115 current->pending[current->last].offset -
2116 current->pending[current->first].offset,
2117 current->pending[current->last].pcr -
2118 current->pending[current->first].pcr);
2119 GST_DEBUG ("Current bitrate is now %" G_GUINT64_FORMAT,
2120 current->cur_bitrate);
2122 /* Calculate previous bitrate */
2123 current->prev_bitrate =
2124 gst_util_uint64_scale (PCR_SECOND,
2125 current->pending[current->first].offset - current->prev.offset,
2126 current->pending[current->first].pcr - current->prev.pcr);
2127 GST_DEBUG ("Previous group bitrate now %" G_GUINT64_FORMAT,
2128 current->prev_bitrate);
2130 /* FIXME : Better bitrate changes ? Currently 10% changes */
2131 if (ABSDIFF (current->cur_bitrate,
2132 current->prev_bitrate) * 10 > current->prev_bitrate) {
2133 GST_DEBUG ("Current bitrate changed by more than 10%% (old:%"
2134 G_GUINT64_FORMAT " new:%" G_GUINT64_FORMAT ")", current->prev_bitrate,
2135 current->cur_bitrate);
2136 /* If we detected a change in bitrate, this means that
2137 * d(first - prev) is a different bitrate than d(last - first).
2139 * Two conclusions can be made:
2140 * 1) d(first - prev) is a complete bitrate "chain" (values between the
2141 * reference value and first pending value have consistent bitrate).
2142 * 2) next values (from second pending value onwards) will no longer have
2145 * The question remains as to how long the new bitrate change is going to
2146 * last for (it might be short or longer term). For this we need to restart
2147 * bitrate estimation.
2149 * * We move over first to the last value of group (a new chain ends and
2150 * starts from there)
2151 * * We remember that last group value as our new window reference
2152 * * We restart our window filing from the last observed value
2154 * Once our new window is filled we will end up in two different scenarios:
2155 * 1) Either the bitrate change was consistent, and therefore the bitrate
2156 * will have remained constant over at least 2 window length
2157 * 2) The bitrate change was very short (1 window duration) and we will
2158 * close that chain and restart again.
2159 * X) And of course if any discont/gaps/wrapover happen in the meantime they
2160 * will also close the group.
2162 _append_group_values (current->group, current->pending[current->first]);
2163 current->prev = current->pending[current->first];
2164 current->first = current->last;
2165 current->write = (current->first + 1) % PCR_BITRATE_NEEDED;
2170 /* Update read position */
2171 current->first = (current->first + 1) % PCR_BITRATE_NEEDED;
2175 /* convert specified offset into stream time */
2177 mpegts_packetizer_offset_to_ts (MpegTSPacketizer2 * packetizer,
2178 guint64 offset, guint16 pid)
2180 PCROffsetGroup *last;
2181 MpegTSPCR *pcrtable;
2184 guint64 lastpcr, lastoffset;
2186 GST_DEBUG ("offset %" G_GUINT64_FORMAT, offset);
2188 if (G_UNLIKELY (!packetizer->calculate_offset))
2189 return GST_CLOCK_TIME_NONE;
2191 if (G_UNLIKELY (packetizer->refoffset == -1))
2192 return GST_CLOCK_TIME_NONE;
2194 if (G_UNLIKELY (offset < packetizer->refoffset))
2195 return GST_CLOCK_TIME_NONE;
2197 PACKETIZER_GROUP_LOCK (packetizer);
2199 pcrtable = get_pcr_table (packetizer, pid);
2201 if (g_list_length (pcrtable->groups) < 1) {
2202 PACKETIZER_GROUP_UNLOCK (packetizer);
2203 GST_WARNING ("Not enough observations to return a duration estimate");
2204 return GST_CLOCK_TIME_NONE;
2207 if (g_list_length (pcrtable->groups) > 1) {
2208 GST_LOG ("Using last group");
2210 /* FIXME : Refine this later to use neighbouring groups */
2211 tmp = g_list_last (pcrtable->groups);
2214 if (G_UNLIKELY (last->flags & PCR_GROUP_FLAG_ESTIMATED))
2215 _reevaluate_group_pcr_offset (pcrtable, last);
2217 /* lastpcr is the full value in PCR from the first first chunk of data */
2218 lastpcr = last->values[last->last_value].pcr + last->pcr_offset;
2219 /* lastoffset is the full offset from the first chunk of data */
2221 last->values[last->last_value].offset + last->first_offset -
2222 packetizer->refoffset;
2224 PCROffsetCurrent *current = pcrtable->current;
2226 if (!current->group) {
2227 PACKETIZER_GROUP_UNLOCK (packetizer);
2228 GST_LOG ("No PCR yet");
2229 return GST_CLOCK_TIME_NONE;
2231 /* If doing progressive read, use current */
2232 GST_LOG ("Using current group");
2233 lastpcr = current->group->pcr_offset + current->pending[current->last].pcr;
2234 lastoffset = current->first_offset + current->pending[current->last].offset;
2236 GST_DEBUG ("lastpcr:%" GST_TIME_FORMAT " lastoffset:%" G_GUINT64_FORMAT
2237 " refoffset:%" G_GUINT64_FORMAT,
2238 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (lastpcr)), lastoffset,
2239 packetizer->refoffset);
2241 /* Convert byte difference into time difference (and transformed from 27MHz to 1GHz) */
2243 PCRTIME_TO_GSTTIME (gst_util_uint64_scale (offset - packetizer->refoffset,
2244 lastpcr, lastoffset));
2246 PACKETIZER_GROUP_UNLOCK (packetizer);
2248 GST_DEBUG ("Returning timestamp %" GST_TIME_FORMAT " for offset %"
2249 G_GUINT64_FORMAT, GST_TIME_ARGS (res), offset);
2254 /* Input : local PTS (in GHz units)
2255 * Return : Stream time (in GHz units) */
2257 mpegts_packetizer_pts_to_ts_internal (MpegTSPacketizer2 * packetizer,
2258 GstClockTime pts, guint16 pcr_pid, gboolean check_diff)
2260 GstClockTime res = GST_CLOCK_TIME_NONE;
2261 MpegTSPCR *pcrtable;
2263 PACKETIZER_GROUP_LOCK (packetizer);
2264 pcrtable = get_pcr_table (packetizer, pcr_pid);
2266 if (!GST_CLOCK_TIME_IS_VALID (pcrtable->base_time) && pcr_pid == 0x1fff &&
2267 GST_CLOCK_TIME_IS_VALID (packetizer->last_in_time)) {
2268 pcrtable->base_time = packetizer->last_in_time;
2269 pcrtable->base_pcrtime = pts;
2272 /* Use clock skew if present */
2273 if (packetizer->calculate_skew
2274 && GST_CLOCK_TIME_IS_VALID (pcrtable->base_time)) {
2275 GST_DEBUG ("pts %" GST_TIME_FORMAT " base_pcrtime:%" GST_TIME_FORMAT
2276 " base_time:%" GST_TIME_FORMAT " pcroffset:%" GST_TIME_FORMAT,
2277 GST_TIME_ARGS (pts),
2278 GST_TIME_ARGS (pcrtable->base_pcrtime),
2279 GST_TIME_ARGS (pcrtable->base_time),
2280 GST_TIME_ARGS (pcrtable->pcroffset));
2281 res = pts + pcrtable->pcroffset + packetizer->extra_shift;
2283 /* Don't return anything if we differ too much against last seen PCR */
2284 if (G_UNLIKELY (check_diff && pcr_pid != 0x1fff &&
2285 ABSDIFF (res, pcrtable->last_pcrtime) > 15 * GST_SECOND)) {
2286 res = GST_CLOCK_TIME_NONE;
2288 GstClockTime tmp = pcrtable->base_time + pcrtable->skew;
2289 if (tmp + res >= pcrtable->base_pcrtime) {
2290 res += tmp - pcrtable->base_pcrtime;
2291 } else if (!check_diff || ABSDIFF (tmp + res + PCR_GST_MAX_VALUE,
2292 pcrtable->base_pcrtime) < PCR_GST_MAX_VALUE / 2) {
2293 /* Handle wrapover */
2294 res += tmp + PCR_GST_MAX_VALUE - pcrtable->base_pcrtime;
2296 /* Fallback for values that differ way too much */
2297 res = GST_CLOCK_TIME_NONE;
2300 } else if (packetizer->calculate_offset && pcrtable->groups) {
2301 gint64 refpcr = G_MAXINT64, refpcroffset;
2302 PCROffsetGroup *group = pcrtable->current->group;
2304 /* Generic calculation:
2305 * Stream Time = PTS - first group PCR + group PCR_offset
2307 * In case of wrapover:
2308 * Stream Time = PTS + MAX_PCR - first group PCR + group PCR_offset
2309 * (which we actually do by using first group PCR -= MAX_PCR in order
2310 * to end up with the same calculation as for non-wrapover) */
2313 /* If we have a current group the value is pretty much guaranteed */
2314 GST_DEBUG ("Using current First PCR:%" GST_TIME_FORMAT " offset:%"
2315 G_GUINT64_FORMAT " PCR_offset:%" GST_TIME_FORMAT,
2316 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->first_pcr)),
2317 group->first_offset,
2318 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->pcr_offset)));
2319 refpcr = group->first_pcr;
2320 refpcroffset = group->pcr_offset;
2321 if (pts < PCRTIME_TO_GSTTIME (refpcr)) {
2322 /* Only apply wrapover if we're certain it is, and avoid
2323 * returning bogus values if it's a PTS/DTS which is *just*
2324 * before the start of the current group
2326 if (PCRTIME_TO_GSTTIME (refpcr) - pts > GST_SECOND) {
2327 pts += PCR_GST_MAX_VALUE;
2329 refpcr = G_MAXINT64;
2333 /* Otherwise, find a suitable group */
2335 GST_DEBUG ("Find group for current offset %" G_GUINT64_FORMAT,
2336 packetizer->offset);
2338 for (tmp = pcrtable->groups; tmp; tmp = tmp->next) {
2339 PCROffsetGroup *tgroup = tmp->data;
2340 GST_DEBUG ("Trying First PCR:%" GST_TIME_FORMAT " offset:%"
2341 G_GUINT64_FORMAT " PCR_offset:%" GST_TIME_FORMAT,
2342 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (tgroup->first_pcr)),
2343 tgroup->first_offset,
2344 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (tgroup->pcr_offset)));
2345 /* Gone too far ? */
2346 if (tgroup->first_offset > packetizer->offset) {
2347 /* If there isn't a pending reset, use that value */
2349 GST_DEBUG ("PTS is %" GST_TIME_FORMAT " into group",
2350 GST_TIME_ARGS (pts - PCRTIME_TO_GSTTIME (group->first_pcr)));
2355 /* In that group ? */
2356 if (group->first_offset + group->values[group->last_value].offset >
2357 packetizer->offset) {
2358 GST_DEBUG ("PTS is %" GST_TIME_FORMAT " into group",
2359 GST_TIME_ARGS (pts - PCRTIME_TO_GSTTIME (group->first_pcr)));
2363 if (group && !(group->flags & PCR_GROUP_FLAG_RESET)) {
2364 GST_DEBUG ("Using group !");
2365 refpcr = group->first_pcr;
2366 refpcroffset = group->pcr_offset;
2367 if (pts < PCRTIME_TO_GSTTIME (refpcr)) {
2368 if (PCRTIME_TO_GSTTIME (refpcr) - pts > GST_SECOND)
2369 pts += PCR_GST_MAX_VALUE;
2371 refpcr = G_MAXINT64;
2375 if (refpcr != G_MAXINT64)
2377 pts - PCRTIME_TO_GSTTIME (refpcr) + PCRTIME_TO_GSTTIME (refpcroffset);
2379 GST_WARNING ("No groups, can't calculate timestamp");
2381 GST_WARNING ("Not enough information to calculate proper timestamp");
2383 PACKETIZER_GROUP_UNLOCK (packetizer);
2385 GST_DEBUG ("Returning timestamp %" GST_TIME_FORMAT " for pts %"
2386 GST_TIME_FORMAT " pcr_pid:0x%04x", GST_TIME_ARGS (res),
2387 GST_TIME_ARGS (pts), pcr_pid);
2391 /* Input : local PTS (in GHz units)
2392 * Return : Stream time (in GHz units) */
2394 mpegts_packetizer_pts_to_ts_unchecked (MpegTSPacketizer2 * packetizer,
2395 GstClockTime pts, guint16 pcr_pid)
2397 return mpegts_packetizer_pts_to_ts_internal (packetizer, pts, pcr_pid, FALSE);
2400 /* Input : local PTS (in GHz units)
2401 * Return : Stream time (in GHz units) */
2403 mpegts_packetizer_pts_to_ts (MpegTSPacketizer2 * packetizer,
2404 GstClockTime pts, guint16 pcr_pid)
2406 return mpegts_packetizer_pts_to_ts_internal (packetizer, pts, pcr_pid, TRUE);
2409 /* Stream time to offset */
2411 mpegts_packetizer_ts_to_offset (MpegTSPacketizer2 * packetizer,
2412 GstClockTime ts, guint16 pcr_pid)
2414 MpegTSPCR *pcrtable;
2416 PCROffsetGroup *nextgroup = NULL, *prevgroup = NULL;
2417 guint64 querypcr, firstpcr, lastpcr, firstoffset, lastoffset;
2418 PCROffsetCurrent *current;
2421 if (!packetizer->calculate_offset)
2424 PACKETIZER_GROUP_LOCK (packetizer);
2425 pcrtable = get_pcr_table (packetizer, pcr_pid);
2427 if (pcrtable->groups == NULL) {
2428 PACKETIZER_GROUP_UNLOCK (packetizer);
2432 querypcr = GSTTIME_TO_PCRTIME (ts);
2434 GST_DEBUG ("Searching offset for ts %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
2436 /* First check if we're within the current pending group */
2437 current = pcrtable->current;
2438 if (current && current->group && (querypcr >= current->group->pcr_offset) &&
2439 querypcr - current->group->pcr_offset <=
2440 current->pending[current->last].pcr) {
2441 GST_DEBUG ("pcr is in current group");
2442 nextgroup = current->group;
2443 goto calculate_points;
2446 /* Find the neighbouring groups */
2447 for (tmp = pcrtable->groups; tmp; tmp = tmp->next) {
2448 nextgroup = (PCROffsetGroup *) tmp->data;
2450 GST_DEBUG ("Trying group PCR %" GST_TIME_FORMAT " (offset %"
2451 G_GUINT64_FORMAT " pcr_offset %" GST_TIME_FORMAT,
2452 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (nextgroup->first_pcr)),
2453 nextgroup->first_offset,
2454 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (nextgroup->pcr_offset)));
2456 /* Check if we've gone too far */
2457 if (nextgroup->pcr_offset > querypcr) {
2458 GST_DEBUG ("pcr is before that group");
2462 if (tmp->next == NULL) {
2463 GST_DEBUG ("pcr is beyond last group");
2467 prevgroup = nextgroup;
2469 /* Maybe it's in this group */
2470 if (nextgroup->values[nextgroup->last_value].pcr +
2471 nextgroup->pcr_offset >= querypcr) {
2472 GST_DEBUG ("pcr is in that group");
2479 GST_DEBUG ("nextgroup:%p, prevgroup:%p", nextgroup, prevgroup);
2481 if (nextgroup == prevgroup || prevgroup == NULL) {
2482 /* We use the current group to calculate position:
2483 * * if the PCR is within this group
2484 * * if there is only one group to use for calculation
2486 GST_DEBUG ("In group or after last one");
2487 lastoffset = firstoffset = nextgroup->first_offset;
2488 lastpcr = firstpcr = nextgroup->pcr_offset;
2489 if (current && nextgroup == current->group) {
2490 lastoffset += current->pending[current->last].offset;
2491 lastpcr += current->pending[current->last].pcr;
2493 lastoffset += nextgroup->values[nextgroup->last_value].offset;
2494 lastpcr += nextgroup->values[nextgroup->last_value].pcr;
2497 GST_DEBUG ("Between group");
2498 lastoffset = nextgroup->first_offset;
2499 lastpcr = nextgroup->pcr_offset;
2501 prevgroup->values[prevgroup->last_value].offset +
2502 prevgroup->first_offset;
2504 prevgroup->values[prevgroup->last_value].pcr + prevgroup->pcr_offset;
2507 PACKETIZER_GROUP_UNLOCK (packetizer);
2509 GST_DEBUG ("Using prev PCR %" G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT,
2510 firstpcr, firstoffset);
2511 GST_DEBUG ("Using last PCR %" G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT,
2512 lastpcr, lastoffset);
2515 if (lastpcr != firstpcr)
2516 res += gst_util_uint64_scale (querypcr - firstpcr,
2517 lastoffset - firstoffset, lastpcr - firstpcr);
2519 GST_DEBUG ("Returning offset %" G_GUINT64_FORMAT " for ts %"
2520 GST_TIME_FORMAT, res, GST_TIME_ARGS (ts));
2526 mpegts_packetizer_set_reference_offset (MpegTSPacketizer2 * packetizer,
2529 GST_DEBUG ("Setting reference offset to %" G_GUINT64_FORMAT, refoffset);
2531 PACKETIZER_GROUP_LOCK (packetizer);
2532 packetizer->refoffset = refoffset;
2533 PACKETIZER_GROUP_UNLOCK (packetizer);
2537 mpegts_packetizer_set_pcr_discont_threshold (MpegTSPacketizer2 * packetizer,
2538 GstClockTime threshold)
2540 PACKETIZER_GROUP_LOCK (packetizer);
2541 packetizer->pcr_discont_threshold = threshold;
2542 PACKETIZER_GROUP_UNLOCK (packetizer);
2546 mpegts_packetizer_set_current_pcr_offset (MpegTSPacketizer2 * packetizer,
2547 GstClockTime offset, guint16 pcr_pid)
2551 MpegTSPCR *pcrtable;
2552 PCROffsetGroup *group;
2554 gboolean apply = FALSE;
2557 PACKETIZER_GROUP_LOCK (packetizer);
2558 pcrtable = get_pcr_table (packetizer, pcr_pid);
2560 if (pcrtable == NULL || pcrtable->current->group == NULL) {
2561 PACKETIZER_GROUP_UNLOCK (packetizer);
2565 pcr_offset = GSTTIME_TO_PCRTIME (offset);
2567 /* Pick delta from *first* group */
2568 if (pcrtable->groups)
2569 group = pcrtable->groups->data;
2571 group = pcrtable->current->group;
2572 GST_DEBUG ("Current group PCR %" GST_TIME_FORMAT " (offset %"
2573 G_GUINT64_FORMAT " pcr_offset %" GST_TIME_FORMAT,
2574 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->first_pcr)),
2575 group->first_offset,
2576 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (group->pcr_offset)));
2578 /* Remember the difference between previous initial pcr_offset and
2579 * new initial pcr_offset */
2580 delta = pcr_offset - group->pcr_offset;
2582 GST_DEBUG ("No shift to apply");
2583 PACKETIZER_GROUP_UNLOCK (packetizer);
2586 GST_DEBUG ("Shifting groups by %" GST_TIME_FORMAT
2587 " for new initial pcr_offset %" GST_TIME_FORMAT,
2588 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (delta)), GST_TIME_ARGS (offset));
2590 for (tmp = pcrtable->groups; tmp; tmp = tmp->next) {
2591 PCROffsetGroup *tgroup = (tmp->data);
2592 if (tgroup == group)
2595 tgroup->pcr_offset += delta;
2596 GST_DEBUG ("Update group PCR %" GST_TIME_FORMAT " (offset %"
2597 G_GUINT64_FORMAT " pcr_offset %" GST_TIME_FORMAT,
2598 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (tgroup->first_pcr)),
2599 tgroup->first_offset,
2600 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (tgroup->pcr_offset)));
2602 GST_DEBUG ("Not modifying group PCR %" GST_TIME_FORMAT " (offset %"
2603 G_GUINT64_FORMAT " pcr_offset %" GST_TIME_FORMAT,
2604 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (tgroup->first_pcr)),
2605 tgroup->first_offset,
2606 GST_TIME_ARGS (PCRTIME_TO_GSTTIME (tgroup->pcr_offset)));
2608 PACKETIZER_GROUP_UNLOCK (packetizer);