]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
mpegts parser: speed & PCR extraction improvements
authorJaroslav Kysela <perex@perex.cz>
Mon, 8 Dec 2014 14:27:28 +0000 (15:27 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 8 Dec 2014 14:27:28 +0000 (15:27 +0100)
src/input/mpegts/mpegts_input.c
src/input/mpegts/tsdemux.c

index 601e3b90afb15902cd797aea30a65fad993e7722..12ff03bd2095a7edb0d80dccb3d54bed9d25ddd9 100644 (file)
@@ -602,10 +602,23 @@ static int inline
 ts_sync_count ( const uint8_t *tsb, int len )
 {
   int i = 0;
-  while (len >= 188 && *tsb == 0x47) {
-    ++i;
-    len -= 188;
-    tsb += 188;
+  while (len >= 188) {
+    if (len >= 1880 &&
+        tsb[0*188] == 0x47 && tsb[1*188] == 0x47 &&
+        tsb[2*188] == 0x47 && tsb[3*188] == 0x47 &&
+        tsb[4*188] == 0x47 && tsb[5*188] == 0x47 &&
+        tsb[6*188] == 0x47 && tsb[7*188] == 0x47 &&
+        tsb[8*188] == 0x47 && tsb[9*188] == 0x47) {
+      i += 10;
+      len -= 1880;
+      tsb += 1880;
+    } else if (*tsb == 0x47) {
+      ++i;
+      len -= 188;
+      tsb += 188;
+    } else {
+      break;
+    }
   }
   return i;
 }
@@ -615,7 +628,7 @@ mpegts_input_recv_packets
   ( mpegts_input_t *mi, mpegts_mux_instance_t *mmi, sbuf_t *sb,
     int64_t *pcr, uint16_t *pcr_pid )
 {
-  int i, p = 0, len2, off = 0;
+  int p = 0, len2, off = 0;
   mpegts_packet_t *mp;
   uint8_t *tsb = sb->sb_data;
   int     len  = sb->sb_ptr;
@@ -630,7 +643,6 @@ mpegts_input_recv_packets
   mi->mi_last_dispatch = dispatch_clock;
 
   /* Check for sync */
-// could be a bit more efficient
   while ( (len >= (MIN_TS_SYN * 188)) &&
           ((p = ts_sync_count(tsb, len)) < MIN_TS_SYN) ) {
     mmi->mmi_stats.unc++;
@@ -646,12 +658,12 @@ mpegts_input_recv_packets
 
   /* Extract PCR (used for tsfile playback) */
   if (pcr && pcr_pid) {
-    uint8_t *tmp = tsb;
-    for (i = 0; i < p; i++, tmp += 188) {
+    uint8_t *tmp, *end;
+    for (tmp = tsb, end = tsb + p * 188; tmp < end; tmp += 188) {
       uint16_t pid = ((tmp[1] & 0x1f) << 8) | tmp[2];
       if (*pcr_pid == MPEGTS_PID_NONE || *pcr_pid == pid) {
         ts_recv_packet1(NULL, tmp, pcr, 0);
-        if (*pcr != PTS_UNSET) *pcr_pid = pid;
+       if (*pcr != PTS_UNSET) *pcr_pid = pid;
       }
     }
   }
index 2b4f7e07212856f5a85e499fff736a31c510cdc6..816cf8ccff0ea1f0287f35642a27a07dbb35beb9 100644 (file)
@@ -129,8 +129,8 @@ ts_process_pcr(mpegts_service_t *t, elementary_stream_t *st, int64_t pcr)
     if(d < -90000LL || d > 90000LL) {
       st->es_pcr_recovery_fails++;
       if(st->es_pcr_recovery_fails > 10) {
-  st->es_pcr_recovery_fails = 0;
-  st->es_pcr_real_last = PTS_UNSET;
+        st->es_pcr_recovery_fails = 0;
+        st->es_pcr_real_last = PTS_UNSET;
       }
       return;
     }
@@ -139,8 +139,7 @@ ts_process_pcr(mpegts_service_t *t, elementary_stream_t *st, int64_t pcr)
     
     if(t->s_pcr_pid == st->es_pid) {
       /* This is the registered PCR PID, adjust service PCR drift
-   via an IIR filter */
-      
+         via an IIR filter */
       t->s_pcr_drift = (t->s_pcr_drift * 255 + st->es_pcr_drift) / 256;
     }
   }
@@ -170,7 +169,7 @@ ts_recv_packet1
 #endif
 
   /* Extract PCR (do this early for tsfile) */
-  if(((tsb[3] & 0x30) == 0x30) && (tsb[4] > 5) && (tsb[5] & 0x10) && !error) {
+  if((tsb[3] & 0x20) && (tsb[4] > 5) && (tsb[5] & 0x10) && !error) {
     pcr  = (uint64_t)tsb[6] << 25;
     pcr |= (uint64_t)tsb[7] << 17;
     pcr |= (uint64_t)tsb[8] << 9;