--- gstreamer/gst-plugins-bad-0.10.18/gst/mpegdemux/mpegtspacketizer.c	2009-11-29 13:31:50.000000000 +0100
+++ ./magisterka-svn/programs/gst-plugins-bad-0.10/gst/mpegdemux/mpegtspacketizer.c	2010-06-08 21:17:20.869622819 +0200
@@ -89,14 +89,15 @@ mpegts_packetizer_stream_subtable_compar
   bsub = (MpegTSPacketizerStreamSubtable *) b;
 
   if (asub->table_id == bsub->table_id &&
-      asub->subtable_extension == bsub->subtable_extension)
+      asub->subtable_extension == bsub->subtable_extension &&
+      asub->section_number == bsub->section_number)
     return 0;
   return -1;
 }
 
 static MpegTSPacketizerStreamSubtable *
 mpegts_packetizer_stream_subtable_new (guint8 table_id,
-    guint16 subtable_extension)
+    guint16 subtable_extension, guint8 section_number)
 {
   MpegTSPacketizerStreamSubtable *subtable;
 
@@ -104,6 +105,7 @@ mpegts_packetizer_stream_subtable_new (g
   subtable->version_number = VERSION_NUMBER_UNSET;
   subtable->table_id = table_id;
   subtable->subtable_extension = subtable_extension;
+  subtable->section_number = section_number;
   return subtable;
 }
 
@@ -266,6 +268,7 @@ mpegts_packetizer_parse_section_header (
 {
   guint8 tmp;
   guint8 *data;
+  guint8 section_syntax_indicator;
   MpegTSPacketizerStreamSubtable *subtable;
   GSList *subtable_list = NULL;
 
@@ -276,39 +279,52 @@ mpegts_packetizer_parse_section_header (
   data = GST_BUFFER_DATA (section->buffer);
 
   section->table_id = *data++;
-  /* if table_id is 0 (pat) then ignore the subtable extension */
-  if ((data[0] & 0x80) == 0 || section->table_id == 0)
-    section->subtable_extension = 0;
-  else
-    section->subtable_extension = GST_READ_UINT16_BE (data + 2);
-
-  subtable = mpegts_packetizer_stream_subtable_new (section->table_id,
-      section->subtable_extension);
-
-  subtable_list = g_slist_find_custom (stream->subtables, subtable,
-      mpegts_packetizer_stream_subtable_compare);
-  if (subtable_list) {
-    g_free (subtable);
-    subtable = (MpegTSPacketizerStreamSubtable *) (subtable_list->data);
+  section_syntax_indicator = data [0] & 0x80;
+  section->section_length = GST_READ_UINT16_BE (data) && 0x0FFF;
+  data += 2;
+  /* ignore other fields is this is TDT or TOT table */
+  if (section->table_id != 0x70 && section->table_id != 0x73)
+  {
+    /* if table_id is 0 (pat) then ignore the subtable extension */
+    if (section_syntax_indicator == 0 || section->table_id == 0)
+      section->subtable_extension = 0;
+    else
+      section->subtable_extension = GST_READ_UINT16_BE (data);
+    data += 2;
+    tmp = *data++;
+    section->version_number = (tmp >> 1) & 0x1F;
+    section->current_next_indicator = tmp & 0x01;
+    section->section_number = *data;
   } else {
-    stream->subtables = g_slist_prepend (stream->subtables, subtable);
+    section->subtable_extension = 0;
+    section->version_number = 0;
+    section->current_next_indicator = 0;
+    section->section_number = 0;
   }
 
-  section->section_length = GST_READ_UINT16_BE (data) & 0x0FFF;
-  data += 2;
+  /* TDT and TOT do not have versions */
+  if (section->table_id != 0x70 && section->table_id != 0x73)
+  {
+    subtable = mpegts_packetizer_stream_subtable_new (section->table_id,
+        section->subtable_extension, section->section_number);
+    subtable_list = g_slist_find_custom (stream->subtables, subtable,
+        mpegts_packetizer_stream_subtable_compare);
+    if (subtable_list) {
+      g_free (subtable);
+      subtable = (MpegTSPacketizerStreamSubtable *) (subtable_list->data);
+    } else {
+      stream->subtables = g_slist_prepend (stream->subtables, subtable);
+    }
 
-  /* skip to the version byte */
-  data += 2;
+    if (!section->current_next_indicator)
+      goto not_applicable;
 
-  tmp = *data++;
-  section->version_number = (tmp >> 1) & 0x1F;
-  section->current_next_indicator = tmp & 0x01;
-  if (!section->current_next_indicator)
-    goto not_applicable;
+    if (section->version_number == subtable->version_number)
+      goto not_applicable;
+
+    subtable->version_number = section->version_number;
+  }
 
-  if (section->version_number == subtable->version_number)
-    goto not_applicable;
-  subtable->version_number = section->version_number;
   stream->section_table_id = section->table_id;
 
   return TRUE;
@@ -368,6 +384,66 @@ error:
 }
 
 GstStructure *
+mpegts_packetizer_parse_tdt (MpegTSPacketizer * packetizer,
+    MpegTSPacketizerSection * section)
+{
+  GstStructure *tdt = NULL;
+  guint16 mjd;
+  guint8 *data, *utc_ptr;
+  guint year, month, day, hour, minute, second;
+
+  if (GST_BUFFER_SIZE (section->buffer) < 8) {
+    GST_WARNING ("PID %d invalid TDT size %d",
+        section->pid, section->section_length);
+    goto error;
+  }
+
+  data = GST_BUFFER_DATA (section->buffer);
+
+  section->table_id = *data++;
+  section->section_length = GST_READ_UINT16_BE (data) && 0x0FFF;
+  data += 2;
+
+  utc_ptr = data + 2;
+  mjd = GST_READ_UINT16_BE (data);
+  if (mjd == G_MAXUINT16) {
+    year = 1900;
+    month = day = hour = minute = second = 0;
+  } else {
+    /* See EN 300 468 Annex C */
+    year = (guint32) (((mjd - 15078.2) / 365.25));
+    month = (guint8) ((mjd - 14956.1 - (guint) (year * 365.25)) / 30.6001);
+    day = mjd - 14956 - (guint) (year * 365.25) - (guint) (month * 30.6001);
+    if (month == 14 || month == 15) {
+      year++;
+      month = month - 1 - 12;
+    } else {
+      month--;
+    }
+    year += 1900;
+    hour = ((utc_ptr[0] & 0xF0) >> 4) * 10 + (utc_ptr[0] & 0x0F);
+    minute = ((utc_ptr[1] & 0xF0) >> 4) * 10 + (utc_ptr[1] & 0x0F);
+    second = ((utc_ptr[2] & 0xF0) >> 4) * 10 + (utc_ptr[2] & 0x0F);
+  }
+
+  tdt = gst_structure_new ("tdt",
+      "year", G_TYPE_UINT, year,
+      "month", G_TYPE_UINT, month,
+      "day", G_TYPE_UINT, day,
+      "hour", G_TYPE_UINT, hour,
+      "minute", G_TYPE_UINT, minute,
+      "second", G_TYPE_UINT, second, NULL);
+
+  return tdt;
+
+error:
+  if (tdt)
+    gst_structure_free (tdt);
+
+  return NULL;
+}
+
+GstStructure *
 mpegts_packetizer_parse_pat (MpegTSPacketizer * packetizer,
     MpegTSPacketizerSection * section)
 {
@@ -2029,9 +2105,14 @@ mpegts_try_discover_packet_size (MpegTSP
   }
   GST_DEBUG ("have packetsize detected: %d of %u bytes",
       packetizer->know_packet_size, packetizer->packet_size);
-  /* flush to sync byte */
-  if (pos > 0)
+
+  if (pos > 0) {
+    /* flush to sync byte */
     gst_adapter_flush (packetizer->adapter, pos);
+  } else if (!packetizer->know_packet_size) {
+    /* drop some invalid and move to the next possible packets */
+    gst_adapter_flush (packetizer->adapter, MPEGTS_MAX_PACKETSIZE);
+  }
   g_free (dest);
 }
 
@@ -2103,7 +2184,7 @@ mpegts_packetizer_push_section (MpegTSPa
 
   data = packet->data;
   section->pid = packet->pid;
-
+  
   if (packet->payload_unit_start_indicator == 1) {
     pointer = *data++;
     if (data + pointer > packet->data_end) {
@@ -2155,7 +2236,6 @@ mpegts_packetizer_push_section (MpegTSPa
     stream->section_length = section_length;
     stream->section_table_id = table_id;
     gst_adapter_push (stream->section_adapter, sub_buf);
-
     res = TRUE;
   } else if (stream->continuity_counter != CONTINUITY_UNSET &&
       (packet->continuity_counter == stream->continuity_counter + 1 ||
