Index: libs/libmythtv/avformatdecoder.h
===================================================================
--- libs/libmythtv/avformatdecoder.h	(revision 12631)
+++ libs/libmythtv/avformatdecoder.h	(working copy)
@@ -221,7 +221,7 @@
 
     bool gotvideo;
 
-    unsigned char prvpkt[3];
+    uint32_t lastpkt;
 
     long long lastvpts;
     long long lastapts;
Index: libs/libmythtv/avformatdecoder.cpp
===================================================================
--- libs/libmythtv/avformatdecoder.cpp	(revision 12631)
+++ libs/libmythtv/avformatdecoder.cpp	(working copy)
@@ -1,6 +1,7 @@
 // C headers
 #include <cassert>
 #include <unistd.h>
+#include <arpa/inet.h>
 
 // C++ headers
 #include <algorithm>
@@ -274,6 +275,7 @@
       gopset(false),                seen_gop(false),
       seq_count(0),                 firstgoppos(0),
       prevgoppos(0),                gotvideo(false),
+      lastpkt(0xFFFFFFFF),
       lastvpts(0),                  lastapts(0),
       lastccptsu(0),
       using_null_videoout(use_null_videoout),
@@ -296,7 +298,6 @@
       dvd_xvmc_enabled(false), dvd_video_codec_changed(false)
 {
     bzero(&params, sizeof(AVFormatParameters));
-    bzero(prvpkt, 3 * sizeof(char));
     bzero(audioSamples, AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof(short int));
     ccd608->SetIgnoreTimecode(true);
 
@@ -1973,112 +1974,138 @@
     }
 }
 
-#define SEQ_START     0x000001b3
-#define GOP_START     0x000001b8
-#define PICTURE_START 0x00000100
-#define SLICE_MIN     0x00000101
-#define SLICE_MAX     0x000001af
-#define SEQ_END_CODE  0x000001b7
+#define SEQ_START     0xb3
+#define GOP_START     0xb8
+#define PICTURE_START 0x00
+#define SLICE_MIN     0x01
+#define SLICE_MAX     0xaf
+#define SEQ_END_CODE  0xb7
 
 void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
 {
     AVCodecContext *context = stream->codec;
     unsigned char *bufptr = pkt->data;
-    //unsigned char *bufend = pkt->data + pkt->size;
-    unsigned int state = 0xFFFFFFFF, v = 0;
-    int prvcount = -1;
+    const unsigned char* const end = pkt->data + pkt->size - 1;
+    unsigned int state = 0xFFFFFFFF;
 
-    while (bufptr < pkt->data + pkt->size)
+    int match = 0;
+    if (lastpkt << 8 == 0x00000100)
+        match = 3;
+    else if (!(lastpkt << 16) && bufptr[0] == 0x01)
+        match = 2;
+    else if (!(lastpkt << 24) && !bufptr[0] && bufptr[1] == 0x01)
+        match = 1;
+
+    if (match)
     {
-        if (++prvcount < 3)
-            v = prvpkt[prvcount];
+#if 0
+       VERBOSE(VB_PLAYBACK, LOC +
+          QString("AVF Matched!  %1  %2 %3%4%5%6").arg(match)
+          .arg(lastpkt).arg(bufptr[0],2,16).arg(bufptr[1],2,16)
+          .arg(bufptr[2],2,16).arg(bufptr[3],2,16));
+#endif
+       bufptr += 3 - match;
+       goto startcode;
+    }
+    bufptr += 2; // two zeros
+
+    while (bufptr < end)
+    {
+        if (*bufptr > 0x01)
+           bufptr += 3;
+        else if (!(*bufptr))
+           bufptr++;
+        else if (bufptr[-2] || bufptr[-1])
+           bufptr += 3;
         else
-            v = *bufptr++;
-
-        unsigned int last_state = state;
-        state = ((state << 8) | v) & 0xFFFFFF;
-
-        if (ringBuffer->isDVD() && pkt->size == 4 &&
-            state == SEQ_END_CODE && !dvdvideopause)
         {
-            dvdvideopause = true;
-            d->ResetMPEG2();
-            return;
-        }
+            // At this point we have seen the start code 0 0 1
+            ++bufptr;
+startcode:
+            state = *bufptr;
+            bufptr++;
 
-        if (last_state != 0x000001)
-            continue;
-        else if (state >= SLICE_MIN && state <= SLICE_MAX)
-            continue;
-        else if (SEQ_START == state)
-        {
-            if (bufptr + 11 >= pkt->data + pkt->size)
-                continue; // not enough valid data...
-            SequenceHeader *seq = reinterpret_cast<SequenceHeader*>(bufptr);
+            if (SEQ_START == state)
+            {
+                if (bufptr + 11 >= pkt->data + pkt->size)
+                    continue; // not enough valid data...
+                SequenceHeader *seq =
+                              reinterpret_cast<SequenceHeader*>(bufptr);
 
-            uint  width  = seq->width();
-            uint  height = seq->height();
-            float aspect = seq->aspect(context->sub_id == 1);
-            float seqFPS = seq->fps();
+                uint  width  = seq->width();
+                uint  height = seq->height();
+                float aspect = seq->aspect(context->sub_id == 1);
+                float seqFPS = seq->fps();
 
-            bool changed = (seqFPS > fps+0.01) || (seqFPS < fps-0.01);
-            changed |= (width  != (uint)current_width );
-            changed |= (height != (uint)current_height);
-            changed |= (aspect != current_aspect);
+                bool changed = (seqFPS > fps+0.01) ||
+                               (seqFPS < fps-0.01);
+                changed |= (width  != (uint)current_width );
+                changed |= (height != (uint)current_height);
+                changed |= (aspect != current_aspect);
 
-            if (changed)
-            {
-                uint awidth = width, aheight = height;
-                align_dimensions(context, awidth, aheight);
+                if (changed)
+                {
+                    uint awidth = width, aheight = height;
+                    align_dimensions(context, awidth, aheight);
 
-                GetNVP()->SetVideoParams(awidth, aheight, seqFPS,
-                                         keyframedist, aspect, 
-                                         kScan_Detect);
+                    GetNVP()->SetVideoParams(awidth, aheight, seqFPS,
+                                             keyframedist, aspect, 
+                                             kScan_Detect);
                 
-                if (ringBuffer->InDVDMenuOrStillFrame())
-                {
-                    ringBuffer->DVD()->SeekCellStart();
-                    av_read_frame_flush(ic);
+                    if (ringBuffer->InDVDMenuOrStillFrame())
+                    {
+                        ringBuffer->DVD()->SeekCellStart();
+                        av_read_frame_flush(ic);
+                    }
+                    
+                    current_width  = width;
+                    current_height = height;
+                    current_aspect = aspect;
+                    fps            = seqFPS;
+    
+                    d->ResetMPEG2();
+    
+                    gopset = false;
+                    prevgoppos = 0;
+                    lastapts = lastvpts = lastccptsu = 0;
+
+                    // fps debugging info
+                    float avFPS = normalized_fps(stream, context);
+                    if ((seqFPS > avFPS+0.01) || (seqFPS < avFPS-0.01))
+                    {
+                        VERBOSE(VB_PLAYBACK, LOC +
+                                QString("avFPS(%1) != seqFPS(%2)")
+                                .arg(avFPS).arg(seqFPS));
+                    }
                 }
-                
-                current_width  = width;
-                current_height = height;
-                current_aspect = aspect;
-                fps            = seqFPS;
 
-                d->ResetMPEG2();
+                seq_count++;
 
-                gopset = false;
-                prevgoppos = 0;
-                lastapts = lastvpts = lastccptsu = 0;
-
-                // fps debugging info
-                float avFPS = normalized_fps(stream, context);
-                if ((seqFPS > avFPS+0.01) || (seqFPS < avFPS-0.01))
+                if (!seen_gop && seq_count > 1)
                 {
-                    VERBOSE(VB_PLAYBACK, LOC +
-                            QString("avFPS(%1) != seqFPS(%2)")
-                            .arg(avFPS).arg(seqFPS));
+                    HandleGopStart(pkt);
+                    pkt->flags |= PKT_FLAG_KEY;
                 }
             }
-
-            seq_count++;
-
-            if (!seen_gop && seq_count > 1)
+            else if (GOP_START == state)
             {
                 HandleGopStart(pkt);
+                seen_gop = true;
                 pkt->flags |= PKT_FLAG_KEY;
             }
+            else if (SEQ_END_CODE == state && ringBuffer->isDVD() &&
+                     pkt->size == 4 && !dvdvideopause)
+            {
+                dvdvideopause = true;
+                d->ResetMPEG2();
+                return;
+            }
+            bufptr++;
         }
-        else if (GOP_START == state)
-        {
-            HandleGopStart(pkt);
-            seen_gop = true;
-            pkt->flags |= PKT_FLAG_KEY;
-        }
     }
-
-    memcpy(prvpkt, pkt->data + pkt->size - 3, 3);
+    // Save last 4 bytes for next time
+    unsigned long *last_word = (unsigned long *)(end - 3);
+    lastpkt = ntohl(*last_word);
 }
 
 void AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
