diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.cpp b/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
old mode 100644
new mode 100755
index ac805e7..96422e2
--- a/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
+++ b/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
@@ -92,9 +92,10 @@ static const float eps = 1E-5;
 
 H264Parser::H264Parser(void)
 {
+    rbsp_buffer      = NULL;
+    rbsp_buffer_size = 0;
     Reset();
     I_is_keyframe = true;
-    memset(&gb, 0, sizeof(gb));
 }
 
 void H264Parser::Reset(void)
@@ -144,6 +145,8 @@ void H264Parser::Reset(void)
 
     AU_offset = frame_start_offset = keyframe_start_offset = 0;
     on_frame = on_key_frame = false;
+
+    resetRBSP();
 }
 
 
@@ -183,28 +186,28 @@ bool H264Parser::new_AU(void)
       one or more of the following ways.
 
       - frame_num differs in value. The value of frame_num used to
-      test this condition is the value of frame_num that appears in
-      the syntax of the slice header, regardless of whether that value
-      is inferred to have been equal to 0 for subsequent use in the
-      decoding process due to the presence of
-      memory_management_control_operation equal to 5.
+        test this condition is the value of frame_num that appears in
+        the syntax of the slice header, regardless of whether that value
+        is inferred to have been equal to 0 for subsequent use in the
+        decoding process due to the presence of
+        memory_management_control_operation equal to 5.
           Note: If the current picture is an IDR picture FrameNum and
           PrevRefFrameNum are set equal to 0.
       - pic_parameter_set_id differs in value.
       - field_pic_flag differs in value.
       - bottom_field_flag is present in both and differs in value.
-      - nal_ref_idc differs in value with one of the nal_ref_idc values
-      being equal to 0.
+      - nal_ref_idc differs in value with one of the nal_ref_idc
+        values being equal to 0.
       - pic_order_cnt_type is equal to 0 for both and either
-      pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom
-      differs in value.
+        pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom
+        differs in value.
       - pic_order_cnt_type is equal to 1 for both and either
-      delta_pic_order_cnt[0] differs in value, or
-      delta_pic_order_cnt[1] differs in value.
+        delta_pic_order_cnt[0] differs in value, or
+        delta_pic_order_cnt[1] differs in value.
       - nal_unit_type differs in value with one of the nal_unit_type values
-      being equal to 5.
+        being equal to 5.
       - nal_unit_type is equal to 5 for both and idr_pic_id differs in
-      value.
+        value.
 
       NOTE – Some of the VCL NAL units in redundant coded pictures or some
       non-VCL NAL units (e.g. an access unit delimiter NAL unit) may also
@@ -230,6 +233,9 @@ bool H264Parser::new_AU(void)
         else if ((bottom_field_flag != -1 && prev_bottom_field_flag != -1) &&
                  bottom_field_flag != prev_bottom_field_flag)
             result = true;
+        else if ((nal_ref_idc == 0 || prev_nal_ref_idc == 0) &&
+                 nal_ref_idc != prev_nal_ref_idc)
+            result = true;
         else if ((pic_order_cnt_type == 0 && prev_pic_order_cnt_type == 0) &&
                  (pic_order_cnt_lsb != prev_pic_order_cnt_lsb ||
                   delta_pic_order_cnt_bottom !=
@@ -253,6 +259,7 @@ bool H264Parser::new_AU(void)
     prev_pic_parameter_set_id = pic_parameter_set_id;
     prev_field_pic_flag = field_pic_flag;
     prev_bottom_field_flag = bottom_field_flag;
+    prev_nal_ref_idc = nal_ref_idc;
     prev_pic_order_cnt_lsb = pic_order_cnt_lsb;
     prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom;
     prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0];
@@ -263,22 +270,111 @@ bool H264Parser::new_AU(void)
     return result;
 }
 
+void H264Parser::resetRBSP(void)
+{
+    rbsp_index = 0;
+    consecutive_zeros = 0;
+    have_unfinished_NAL = false;
+}
+
+void H264Parser::fillRBSP(const uint8_t *byteP, uint32_t byte_count)
+{
+    /*
+      bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE
+      bytes larger then the actual read bits
+    */
+    uint32_t required_size = rbsp_index + byte_count + FF_INPUT_BUFFER_PADDING_SIZE;
+    if(rbsp_buffer_size < required_size)
+    {
+        /* Need a bigger buffer */
+        uint8_t *new_buffer = new uint8_t[required_size];
+
+        if(new_buffer == NULL)
+        {
+            /* Discard the new bytes and allow parsing of
+             * the current NAL to fail VERBOSE */
+            return;
+        }
+
+        /* Copy across bytes from old buffer */
+        memcpy(new_buffer, rbsp_buffer, rbsp_index);
+
+        delete [] rbsp_buffer;
+        rbsp_buffer = new_buffer;
+        rbsp_buffer_size = required_size;
+    }
+
+    /* From rbsp while we have data and we don't run into a
+     * new start code */
+    while(byte_count && (consecutive_zeros < 2 || *byteP != 0x01))
+    {
+        /* Copy the byte into the rbsp, unless it
+         * is the 0x03 in a 0x000003 */
+        if(consecutive_zeros < 2 || *byteP != 0x03)
+            rbsp_buffer[rbsp_index++] = *byteP;
+
+        if(*byteP == 0)
+            consecutive_zeros += 1;
+        else
+            consecutive_zeros = 0;
+
+        byteP += 1;
+        byte_count -= 1;
+    }
+
+    /* Stick some zeros on the end to run into */
+    for(uint32_t i = 0; i < FF_INPUT_BUFFER_PADDING_SIZE; i++)
+        rbsp_buffer[rbsp_index + i] = 0;
+}
+
 uint32_t H264Parser::addBytes(const uint8_t  *bytes,
                               const uint32_t  byte_count,
                               const uint64_t  stream_offset)
 {
-    const uint8_t *byteP = bytes;
-    const uint8_t *endP = bytes + byte_count;
-    uint8_t        first_byte;
+    const uint8_t *startP = bytes;
 
-    state_changed = is_keyframe = false;
+    state_changed = false;
+    on_frame      = false;
+    on_key_frame  = false;
 
-    while (byteP < endP)
+    while (startP < bytes + byte_count && !on_frame)
     {
-        byteP = ff_find_start_code(byteP, endP, &sync_accumulator);
+        const uint8_t *endP;
+        bool           found_start_code;
+
+        endP = ff_find_start_code(startP, bytes + byte_count, &sync_accumulator);
+
+        found_start_code = ((sync_accumulator & 0xffffff00) == 0x00000100);
+
+        /* Between bytes and byteP we potentially have some more
+         * bytes of a NAL we've had insufficient of to parse so far
+         * (plus some bytes of start code) */
+        if(have_unfinished_NAL)
+        {
+            fillRBSP(startP, endP - startP);
+            processRBSP(found_start_code);
+        }
 
-        if ((sync_accumulator & 0xffffff00) == 0x00000100)
+        startP = endP;
+
+        if (found_start_code)
         {
+            if(have_unfinished_NAL)
+            {
+                /* We've found a new start code, without complete
+                 * parsing of the previous NAL. Either there's a
+                 * problem with the stream or with this parser.
+                 * VERBOSE */
+            }
+
+            /* Prepare for accepting the new NAL */
+            resetRBSP();
+
+            /* If we find the start of an AU somewhere from here
+             * to the next start code, the offset to associate with
+             * it is the one passed in to this call, not any of the
+             * subsequent calls. */
+            pkt_offset = stream_offset;
 /*
   nal_unit_type specifies the type of RBSP data structure contained in
   the NAL unit as specified in Table 7-1. VCL NAL units
@@ -299,92 +395,103 @@ uint32_t H264Parser::addBytes(const uint8_t  *bytes,
   10 End of sequence end_of_seq_rbsp( )
   11 End of stream end_of_stream_rbsp( )
 */
-            first_byte = *(byteP - 1);
-            nal_unit_type = first_byte & 0x1f;
-            nal_ref_idc = (first_byte >> 5) & 0x3;
+            nal_unit_type = sync_accumulator & 0x1f;
+            nal_ref_idc = (sync_accumulator >> 5) & 0x3;
 
             if (nal_unit_type == SPS || nal_unit_type == PPS ||
                 nal_unit_type == SEI || NALisSlice(nal_unit_type))
             {
-                /*
-                  bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE
-                  bytes larger then the actual read bits
-                */
-                if (byteP + 1 + FF_INPUT_BUFFER_PADDING_SIZE < endP)
-                {
-                    init_get_bits(&gb, byteP, 8 * (endP - byteP));
-
-                    if (nal_unit_type == SEI)
-                    {
-                        decode_SEI(&gb);
-                        set_AU_pending(stream_offset);
-                    }
-                    else if (nal_unit_type == SPS)
-                    {
-                        decode_SPS(&gb);
-                        set_AU_pending(stream_offset);
-                    }
-                    else if (nal_unit_type == PPS)
-                    {
-                        decode_PPS(&gb);
-                        set_AU_pending(stream_offset);
-                    }
-                    else
-                    {
-                        decode_Header(&gb);
-                        if (new_AU())
-                            set_AU_pending(stream_offset);
-                    }
-
-                    byteP += (get_bits_count(&gb) / 8);
-                }
+                /* This is a NAL we need to parse. We may have the body
+                 * of it in the part of the stream past to us this call,
+                 * or we may get the rest in subsequent calls to addBytes.
+                 * Either way, we have yet to fill the rbsp buffer. */
+                have_unfinished_NAL = true;
             }
-            else if (!AU_pending)
-            {
-                if (nal_unit_type == AU_DELIMITER ||
+            else if (nal_unit_type == AU_DELIMITER ||
                     (nal_unit_type > SPS_EXT &&
                      nal_unit_type < AUXILIARY_SLICE))
-                {
-                    AU_pending = true;
-                    AU_offset = stream_offset;
-                }
-                else if ((nal_ref_idc == 0 || prev_nal_ref_idc == 0) &&
-                         nal_ref_idc != prev_nal_ref_idc)
-                {
-                    AU_pending = true;
-                    AU_offset = stream_offset;
-                }
+            {
+                set_AU_pending();
             }
+        }
+    }
 
-            if (AU_pending && NALisSlice(nal_unit_type))
-            {
-                /* Once we know the slice type of a new AU, we can
-                 * determine if it is a keyframe or just a frame */
+    return startP - bytes;
+}
 
-                AU_pending = false;
-                state_changed = true;
 
-                on_frame = true;
-                frame_start_offset = AU_offset;
+void H264Parser::processRBSP(bool rbsp_complete)
+{
+    GetBitContext gb;
 
-                if (is_keyframe)
-                {
-                    on_key_frame = true;
-                    keyframe_start_offset = AU_offset;
-                }
-                else
-                    on_key_frame = false;
-            }
-            else
-                on_frame = on_key_frame = false;
+    init_get_bits(&gb, rbsp_buffer, 8 * rbsp_index);
+
+    if (nal_unit_type == SEI)
+    {
+        /* SEI cannot be parsed without knowing its size. If
+         * we haven't got the whole rbsp, return and wait for
+         * the rest */
+        if(!rbsp_complete)
+            return;
 
-            prev_nal_ref_idc = nal_ref_idc;
+        set_AU_pending();
 
-            return byteP - bytes;
-        }
+        decode_SEI(&gb);
+    }
+    else if (nal_unit_type == SPS)
+    {
+        /* Best wait until we have the whole thing */
+        if(!rbsp_complete)
+            return;
+
+        set_AU_pending();
+
+        decode_SPS(&gb);
+    }
+    else if (nal_unit_type == PPS)
+    {
+        /* Best wait until we have the whole thing */
+        if(!rbsp_complete)
+            return;
+
+        set_AU_pending();
+
+        decode_PPS(&gb);
+    }
+    else
+    {
+        /* Need only parse the header. So return only
+         * if we have insufficient bytes */
+        if(!rbsp_complete && rbsp_index < MAX_SLICE_HEADER_SIZE)
+            return;
+
+        decode_Header(&gb);
+
+        if (new_AU())
+            set_AU_pending();
     }
 
-    return byteP - bytes;
+    /* If we got this far, we managed to parse a sufficient
+     * prefix of the current NAL. We can go onto the next. */
+    have_unfinished_NAL = false;
+
+    if (AU_pending && NALisSlice(nal_unit_type))
+    {
+        /* Once we know the slice type of a new AU, we can
+         * determine if it is a keyframe or just a frame */
+
+        AU_pending = false;
+        state_changed = true;
+
+        on_frame = true;
+        frame_start_offset = AU_offset;
+
+        if (is_keyframe || au_contains_keyframe_message)
+        {
+            on_key_frame = true;
+            keyframe_start_offset = AU_offset;
+        }
+    }
 }
 
 /*
@@ -394,6 +501,8 @@ bool H264Parser::decode_Header(GetBitContext *gb)
 {
     uint first_mb_in_slice;
 
+    is_keyframe = false;
+
     if (log2_max_frame_num == 0 || pic_order_present_flag == -1)
     {
         // SPS or PPS has not been parsed yet
@@ -600,7 +709,14 @@ void H264Parser::decode_SPS(GetBitContext * gb)
         {
             for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx)
             {
-                get_bits1(gb);  // scaling_list
+                if(get_bits1(gb)) // Scaling list presnent
+                {
+                    int sl_n = ((idx < 6) ? 16 : 64);
+                    for(int sl_i = 0; sl_i < sl_n; sl_i++)
+                    {
+                        get_se_golomb(gb);
+                    }
+                }
             }
         }
     }
@@ -853,7 +969,7 @@ void H264Parser::decode_SEI(GetBitContext *gb)
             exact_match_flag = get_bits1(gb);
             broken_link_flag = get_bits1(gb);
             changing_group_slice_idc = get_bits(gb, 2);
-            is_keyframe |= (recovery_frame_cnt >= 0);
+            au_contains_keyframe_message = (recovery_frame_cnt >= 0);
             return;
 
         default:
@@ -1015,7 +1131,7 @@ void H264Parser::vui_parameters(GetBitContext * gb)
 
 uint H264Parser::frameRate(void) const
 {
-    uint64_t	num;
+    uint64_t    num;
     uint64_t    fps;
 
     num   = 500 * (uint64_t)timeScale; /* 1000 * 0.5 */
diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.h b/mythtv/libs/libmythtv/mpeg/H264Parser.h
old mode 100644
new mode 100755
index 2117a96..a1e6db8
--- a/mythtv/libs/libmythtv/mpeg/H264Parser.h
+++ b/mythtv/libs/libmythtv/mpeg/H264Parser.h
@@ -48,6 +48,10 @@ extern "C" {
 class H264Parser {
   public:
 
+    enum {
+        MAX_SLICE_HEADER_SIZE = 256
+    };
+
     // ITU-T Rec. H.264 table 7-1
     enum NAL_unit_type {
         UNKNOWN         = 0,
@@ -101,7 +105,7 @@ class H264Parser {
     };
 
     H264Parser(void);
-    ~H264Parser(void) {;}
+    ~H264Parser(void) {delete [] rbsp_buffer;}
 
     uint32_t addBytes(const uint8_t  *bytes,
                       const uint32_t  byte_count,
@@ -158,16 +162,20 @@ class H264Parser {
   private:
     enum constants {EXTENDED_SAR = 255};
 
-    inline void set_AU_pending(const uint64_t & stream_offset)
+    inline void set_AU_pending(void)
         {
             if (!AU_pending)
             {
                 AU_pending = true;
-                AU_offset = stream_offset;
+                AU_offset = pkt_offset;
+                au_contains_keyframe_message = false;
             }
         }
 
     bool new_AU(void);
+    void resetRBSP(void);
+    void fillRBSP(const uint8_t *byteP, uint32_t byte_count);
+    void processRBSP(bool rbsp_complete);
     bool decode_Header(GetBitContext *gb);
     void decode_SPS(GetBitContext *gb);
     void decode_PPS(GetBitContext * gb);
@@ -177,11 +185,16 @@ class H264Parser {
     bool       AU_pending;
     bool       state_changed;
     bool       seen_sps;
+    bool       au_contains_keyframe_message;
     bool       is_keyframe;
     bool       I_is_keyframe;
 
     uint32_t   sync_accumulator;
-    GetBitContext gb;
+    uint8_t   *rbsp_buffer;
+    uint32_t   rbsp_buffer_size;
+    uint32_t   rbsp_index;
+    uint32_t   consecutive_zeros;
+    bool       have_unfinished_NAL;
 
     int        prev_frame_num, frame_num;
     uint       slice_type;
@@ -218,7 +231,7 @@ class H264Parser {
     uint32_t   unitsInTick, timeScale;
     bool       fixedRate;
 
-    uint64_t   AU_offset, frame_start_offset, keyframe_start_offset;
+    uint64_t   pkt_offset, AU_offset, frame_start_offset, keyframe_start_offset;
     bool       on_frame, on_key_frame;
 };
 
