Index: libs/libmythtv/dtvrecorder.h
===================================================================
--- libs/libmythtv/dtvrecorder.h	(revision 12631)
+++ libs/libmythtv/dtvrecorder.h	(working copy)
@@ -66,7 +71,7 @@
     QString _recording_type;
 
     // used for scanning pes headers for keyframes
-    uint      _header_pos;
+    uint32_t  _header_pos;
     int       _first_keyframe;
     unsigned long long _last_gop_seen;
     unsigned long long _last_seq_seen;
Index: libs/libmythtv/dtvrecorder.cpp
===================================================================
--- libs/libmythtv/dtvrecorder.cpp	(revision 12631)
+++ libs/libmythtv/dtvrecorder.cpp	(working copy)
@@ -13,6 +13,8 @@
 #include "dtvrecorder.h"
 #include "tv_rec.h"
 
+#include <arpa/inet.h>
+
 #define LOC QString("DTVRec(%1): ").arg(tvrec->GetCaptureCardNum())
 #define LOC_ERR QString("DTVRec(%1) Error: ").arg(tvrec->GetCaptureCardNum())
 
@@ -214,12 +217,6 @@
     if (!tspacket->HasPayload()) // no payload to scan
         return !haveBufferedData;
 
-    // if packet contains start of PES packet, start
-    // looking for first byte of MPEG start code (3 bytes 0 0 1)
-    // otherwise, pick up search where we left off.
-    const bool payloadStart = tspacket->PayloadStart();
-    _header_pos = (payloadStart) ? 0 : _header_pos;
-
     // Just make these local for efficiency reasons (gcc not so smart..)
     const uint maxKFD = kMaxKeyFrameDistance;
     bool hasFrame     = false;
@@ -231,22 +228,53 @@
     //   00 00 01 B8: group_start_code
     //   00 00 01 B3: seq_start_code
     //   (there are others that we don't care about)
-    uint i = tspacket->AFCOffset();
-    for (; i < TSPacket::SIZE; i++)
+
+    const unsigned char* pos = tspacket->data();
+    const unsigned char* const end = pos + TSPacket::SIZE - 1;
+    pos += tspacket->AFCOffset();
+
+    // If packet contains start of PES packet, start
+    // looking for first byte of MPEG start code (3 bytes 0 0 1)
+    // otherwise, check last 3 bytes of previous packet.
+    const bool payloadStart = tspacket->PayloadStart();
+     
+    if (!payloadStart)
     {
-        const unsigned char k = tspacket->data()[i];
-        if (0 == _header_pos)
-            _header_pos = (k == 0x00) ? 1 : 0;
-        else if (1 == _header_pos)
-            _header_pos = (k == 0x00) ? 2 : 0;
-        else if (2 == _header_pos)
-            _header_pos = (k == 0x00) ? 2 : ((k == 0x01) ? 3 : 0);
-        else if (3 == _header_pos)
+        int match = 0;
+        if (_header_pos << 8 == 0x00000100)                         // X 0 0 1 | K X X X
+            match = 3;
+        else if (!(_header_pos << 16) && pos[0] == 0x01)            // X X 0 0 | 1 K X X
+            match = 2;
+        else if (!(_header_pos << 24) && !pos[0] && pos[1] == 0x01) // X X X 0 | 0 1 K X
+            match = 1;
+
+        if (match)
         {
-            _header_pos = 0;
+           //VERBOSE(VB_RECORD, LOC + QString("Matched!  %1  %2 %3%4%5%6").arg(match)
+           //   .arg(_header_pos,0,16).arg(pos[0],2,16).arg(pos[1],2,16).arg(pos[2],2,16).arg(pos[3],2,16));
+           pos += 3 - match;
+           goto startcode;
+        }
+    }
+    pos += 2; // two zeros
+
+    while (pos < end)
+    {
+        if (*pos > 0x01)
+           pos += 3;
+        else if (!(*pos))
+           pos++;
+        else if (pos[-2] || pos[-1])
+           pos += 3;
+        else
+        {
             // At this point we have seen the start code 0 0 1
             // the next byte will be the PES packet stream id.
-            const int stream_id = k;
+            ++pos;
+startcode:
+            const int stream_id = *pos;
+            pos += 3;
+	    // if it matched then skip it, else it's not 0x00 so skip it
             if (PESStreamID::PictureStartCode == stream_id)
                 hasFrame = true;
             else if (PESStreamID::GOPStartCode == stream_id)
@@ -261,6 +289,9 @@
             }
         }
     }
+    // Save last 4 bytes for next time
+    unsigned long *last_word = (unsigned long *)(end - 3);
+    _header_pos = ntohl(*last_word);
 
     if (hasFrame && !hasKeyFrame)
     {
