diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
index 5dc65af..9fa7d00 100644
--- a/mythtv/libs/libmythtv/avformatdecoder.cpp
+++ b/mythtv/libs/libmythtv/avformatdecoder.cpp
@@ -3117,7 +3117,7 @@ int AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
     {
         buf += m_h264_parser->addBytes(buf, buf_end - buf, 0);
 
-        if (m_h264_parser->stateChanged())
+        if (m_h264_parser->stateChanged() && m_h264_parser->seen_SPS())
         {
             if (m_h264_parser->FieldType() != H264Parser::FIELD_BOTTOM)
             {
diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.cpp b/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
index 3e45c60..af2e137 100644
--- a/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
+++ b/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
@@ -116,14 +116,16 @@ void H264Parser::Reset(void)
     prev_pic_parameter_set_id = pic_parameter_set_id = -1;
     prev_field_pic_flag = field_pic_flag = -1;
     prev_bottom_field_flag = bottom_field_flag = -1;
-    prev_nal_ref_idc = nal_ref_idc = 0;
+    prev_nal_ref_idc = nal_ref_idc = 111;  //  != [0|1|2|3]
     prev_pic_order_cnt_type = pic_order_cnt_type =
     prev_pic_order_cnt_lsb = pic_order_cnt_lsb = 0;
     prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom = 0;
     prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0] = 0;
     prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1] = 0;
     prev_nal_unit_type = nal_unit_type = UNKNOWN;
-    prev_idr_pic_id = idr_pic_id = 0;
+
+    // The value of idr_pic_id shall be in the range of 0 to 65535, inclusive.
+    prev_idr_pic_id = idr_pic_id = 65536;
 
     log2_max_frame_num = log2_max_pic_order_cnt_lsb = 0;
     seq_parameter_set_id = 0;
@@ -401,6 +403,7 @@ uint32_t H264Parser::addBytes(const uint8_t  *bytes,
     const uint8_t *startP = bytes;
     const uint8_t *endP;
     bool           found_start_code;
+    bool           good_nal_unit;
 
     state_changed = false;
     on_frame      = false;
@@ -474,23 +477,49 @@ uint32_t H264Parser::addBytes(const uint8_t  *bytes,
             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))
+            good_nal_unit = true;
+            if (nal_ref_idc)
             {
-                /* 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 set have_unfinished_NAL, so that we
-                 * start filling the rbsp buffer */
-                have_unfinished_NAL = true;
+                /* nal_ref_idc shall be equal to 0 for all NAL units having
+                 * nal_unit_type equal to 6, 9, 10, 11, or 12.
+                 */
+                if (nal_unit_type == SEI ||
+                                (nal_unit_type >= AU_DELIMITER &&
+                                 nal_unit_type <= FILLER_DATA))
+                    good_nal_unit = false;
             }
-            else if (nal_unit_type == AU_DELIMITER ||
-                    (nal_unit_type > SPS_EXT &&
-                     nal_unit_type < AUXILIARY_SLICE))
+            else
             {
-                set_AU_pending();
+                /* nal_ref_idc shall not be equal to 0 for NAL units with
+                 * nal_unit_type equal to 5
+                 */
+                if (nal_unit_type == SLICE_IDR)
+                    good_nal_unit = false;
             }
-        }
+
+            if (good_nal_unit)
+            {
+                if (nal_unit_type == SPS || nal_unit_type == PPS ||
+                    nal_unit_type == SEI || NALisSlice(nal_unit_type))
+                {
+                    /* 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 set have_unfinished_NAL, so that we
+                     * start filling the rbsp buffer */
+                      have_unfinished_NAL = true;
+                }
+                else if (nal_unit_type == AU_DELIMITER ||
+                        (nal_unit_type > SPS_EXT &&
+                         nal_unit_type < AUXILIARY_SLICE))
+                {
+                    set_AU_pending();
+                }
+            }
+            else
+                LOG(VB_GENERAL, LOG_ERR,
+                    "H264Parser::addbytes: malformed NAL units");
+        } //found start code
     }
 
     return startP - bytes;
@@ -523,6 +552,9 @@ void H264Parser::processRBSP(bool rbsp_complete)
 
         set_AU_pending();
 
+        if (!seen_sps)
+            SPS_offset = pkt_offset;
+
         decode_SPS(&gb);
     }
     else if (nal_unit_type == PPS)
@@ -576,6 +608,8 @@ void H264Parser::processRBSP(bool rbsp_complete)
 */
 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)
@@ -601,8 +635,7 @@ bool H264Parser::decode_Header(GetBitContext *gb)
       that precedes the current slice in decoding order and has the
       same value of colour_plane_id.
      */
-    //uint first_mb_in_slice = get_ue_golomb(gb);
-    get_ue_golomb(gb); // Replaced above line
+    /* first_mb_in_slice = */ get_ue_golomb_long(gb);
 
     /*
       slice_type specifies the coding type of the slice according to
@@ -611,7 +644,10 @@ bool H264Parser::decode_Header(GetBitContext *gb)
       When nal_unit_type is equal to 5 (IDR picture), slice_type shall
       be equal to 2, 4, 7, or 9 (I or SI)
      */
-    slice_type = get_ue_golomb(gb);
+    slice_type = get_ue_golomb_31(gb);
+
+    /* s->pict_type = golomb_to_pict_type[slice_type % 5];
+     */
 
     /*
       pic_parameter_set_id specifies the picture parameter set in
@@ -642,6 +678,8 @@ bool H264Parser::decode_Header(GetBitContext *gb)
       bitstream....
 
       If the current picture is an IDR picture, frame_num shall be equal to 0.
+
+      When max_num_ref_frames is equal to 0, slice_type shall be equal to 2, 4, 7, or 9.
     */
 
     frame_num = get_bits(gb, log2_max_frame_num);
@@ -685,7 +723,8 @@ bool H264Parser::decode_Header(GetBitContext *gb)
         is_keyframe = true;
     }
     else
-        is_keyframe |= I_is_keyframe && isKeySlice(slice_type);
+        is_keyframe = (I_is_keyframe && isKeySlice(slice_type));
+
     /*
       pic_order_cnt_lsb specifies the picture order count modulo
       MaxPicOrderCntLsb for the top field of a coded frame or for a coded
@@ -702,7 +741,7 @@ bool H264Parser::decode_Header(GetBitContext *gb)
     {
         pic_order_cnt_lsb = get_bits(gb, log2_max_pic_order_cnt_lsb);
 
-        if (pic_order_present_flag && !field_pic_flag)
+        if ((pic_order_present_flag == 1) && !field_pic_flag)
             delta_pic_order_cnt_bottom = get_se_golomb(gb);
         else
             delta_pic_order_cnt_bottom = 0;
@@ -711,33 +750,44 @@ bool H264Parser::decode_Header(GetBitContext *gb)
         delta_pic_order_cnt_bottom = 0;
 
     /*
-      delta_pic_order_cnt[ 0 ] specifies the picture order count
-      difference from the expected picture order count for the top
-      field of a coded frame or for a coded field as specified in
-      subclause 8.2.1. The value of delta_pic_order_cnt[ 0 ] shall be
-      in the range of -231 to 231 - 1, inclusive. When this syntax
-      element is not present in the bitstream for the current slice,
-      it shall be inferred to be equal to 0.
-
-      delta_pic_order_cnt[ 1 ] specifies the picture order count
-      difference from the expected picture order count for the bottom
-      field of a coded frame specified in subclause 8.2.1. The value
-      of delta_pic_order_cnt[ 1 ] shall be in the range of -231 to 231
-      - 1, inclusive. When this syntax element is not present in the
-      bitstream for the current slice, it shall be inferred to be
-      equal to 0.
-     */
-    if (pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag)
+      delta_pic_order_always_zero_flag equal to 1 specifies that
+      delta_pic_order_cnt[ 0 ] and delta_pic_order_cnt[ 1 ] are not
+      present in the slice headers of the sequence and shall be
+      inferred to be equal to 0. delta_pic_order_always_zero_flag
+      equal to 0 specifies that delta_pic_order_cnt[ 0 ] is present in
+      the slice headers of the sequence and delta_pic_order_cnt[ 1 ]
+      may be present in the slice headers of the sequence.
+    */
+    if (delta_pic_order_always_zero_flag)
+    {
+        delta_pic_order_cnt[1] = delta_pic_order_cnt[0] = 0;
+    }
+    else if (pic_order_cnt_type == 1)
     {
+        /*
+          delta_pic_order_cnt[ 0 ] specifies the picture order count
+          difference from the expected picture order count for the top
+          field of a coded frame or for a coded field as specified in
+          subclause 8.2.1. The value of delta_pic_order_cnt[ 0 ] shall
+          be in the range of -2^31 to 2^31 - 1, inclusive. When this
+          syntax element is not present in the bitstream for the
+          current slice, it shall be inferred to be equal to 0.
+
+          delta_pic_order_cnt[ 1 ] specifies the picture order count
+          difference from the expected picture order count for the
+          bottom field of a coded frame specified in subclause
+          8.2.1. The value of delta_pic_order_cnt[ 1 ] shall be in the
+          range of -2^31 to 2^31 - 1, inclusive. When this syntax
+          element is not present in the bitstream for the current
+          slice, it shall be inferred to be equal to 0.
+        */
         delta_pic_order_cnt[0] = get_se_golomb(gb);
 
-        if (pic_order_present_flag && !field_pic_flag)
+        if ((pic_order_present_flag == 1) && !field_pic_flag)
             delta_pic_order_cnt[1] = get_se_golomb(gb);
         else
             delta_pic_order_cnt[1] = 0;
-    }
-    else
-        delta_pic_order_cnt[0] = 0;
+     }
 
     /*
       redundant_pic_cnt shall be equal to 0 for slices and slice data
@@ -748,7 +798,6 @@ bool H264Parser::decode_Header(GetBitContext *gb)
       be equal to 0. The value of redundant_pic_cnt shall be in the
       range of 0 to 127, inclusive.
     */
-
     redundant_pic_cnt = redundant_pic_cnt_present_flag ? get_ue_golomb(gb) : 0;
 
     return true;
@@ -760,10 +809,13 @@ bool H264Parser::decode_Header(GetBitContext *gb)
 void H264Parser::decode_SPS(GetBitContext * gb)
 {
     int profile_idc;
+    int lastScale;
+    int nextScale;
+    int deltaScale;
 
     seen_sps = true;
 
-    profile_idc = get_bits(gb, 8); // profile_idc
+    profile_idc = get_bits(gb, 8);
     get_bits1(gb);      // constraint_set0_flag
     get_bits1(gb);      // constraint_set1_flag
     get_bits1(gb);      // constraint_set2_flag
@@ -772,9 +824,11 @@ void H264Parser::decode_SPS(GetBitContext * gb)
     get_bits(gb, 8);    // level_idc
     get_ue_golomb(gb);  // sps_id
 
-    if (profile_idc >= 100)
+    if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
+        profile_idc == 244 || profile_idc == 44  || profile_idc == 83  ||
+        profile_idc == 86  || profile_idc == 118 || profile_idc == 128 )
     { // high profile
-        if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc
+        if ((chroma_format_idc = get_ue_golomb(gb)) == 3)
             separate_colour_plane_flag = (get_bits1(gb) == 1);
 
         get_ue_golomb(gb);     // bit_depth_luma_minus8
@@ -785,12 +839,18 @@ void H264Parser::decode_SPS(GetBitContext * gb)
         {
             for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx)
             {
-                if (get_bits1(gb)) // Scaling list presnent
+                if (get_bits1(gb)) // Scaling list present
                 {
+                    lastScale = nextScale = 8;
                     int sl_n = ((idx < 6) ? 16 : 64);
-                    for(int sl_i = 0; sl_i < sl_n; sl_i++)
+                    for(int sl_i = 0; sl_i < sl_n; ++sl_i)
                     {
-                        get_se_golomb(gb);
+                        if (nextScale != 0)
+                        {
+                            deltaScale = get_se_golomb(gb);
+                            nextScale = (lastScale + deltaScale + 256) % 256;
+                        }
+                        lastScale = (nextScale == 0) ? lastScale : nextScale;
                     }
                 }
             }
@@ -809,6 +869,7 @@ void H264Parser::decode_SPS(GetBitContext * gb)
     int  offset_for_non_ref_pic;
     int  offset_for_top_to_bottom_field;
     uint tmp;
+    // bool gaps_in_frame_num_allowed_flag;
 
     /*
       pic_order_cnt_type specifies the method to decode picture order
@@ -886,8 +947,7 @@ void H264Parser::decode_SPS(GetBitContext * gb)
       decoding process in case of an inferred gap between values of
       frame_num as specified in subclause 8.2.5.2.
      */
-    //bool gaps_in_frame_num_allowed_flag = get_bits1(gb);
-    get_bits1(gb); // Replaced above line
+    /* gaps_in_frame_num_allowed_flag = */ get_bits1(gb);
 
     /*
       pic_width_in_mbs_minus1 plus 1 specifies the width of each
@@ -1049,25 +1109,27 @@ void H264Parser::decode_SEI(GetBitContext *gb)
     {
         do {
             type += show_bits(gb, 8);
-        } while (get_bits(gb, 8) == 255);
+        } while (get_bits(gb, 8) == 0xFF);
 
         do {
             size += show_bits(gb, 8);
-        } while (get_bits(gb, 8) == 255);
+        } while (get_bits(gb, 8) == 0xFF);
 
         switch (type)
         {
-            case SEI_TYPE_RECOVERY_POINT:
-                recovery_frame_cnt = get_ue_golomb(gb);
-                exact_match_flag = get_bits1(gb);
-                broken_link_flag = get_bits1(gb);
-                changing_group_slice_idc = get_bits(gb, 2);
-                au_contains_keyframe_message = (recovery_frame_cnt == 0);
-                return;
-
-            default:
-                skip_bits(gb, size * 8);
-                break;
+          case SEI_TYPE_RECOVERY_POINT:
+            recovery_frame_cnt = get_ue_golomb(gb);
+            exact_match_flag = get_bits1(gb);
+            broken_link_flag = get_bits1(gb);
+            changing_group_slice_idc = get_bits(gb, 2);
+            au_contains_keyframe_message = (recovery_frame_cnt == 0);
+            if ((size - 12) > 0)
+                skip_bits(gb, (size - 12) * 8);
+            return;
+
+          default:
+            skip_bits(gb, size * 8);
+            break;
         }
     }
 
diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.h b/mythtv/libs/libmythtv/mpeg/H264Parser.h
index 9c984e0..eb95779 100644
--- a/mythtv/libs/libmythtv/mpeg/H264Parser.h
+++ b/mythtv/libs/libmythtv/mpeg/H264Parser.h
@@ -60,7 +60,7 @@ class H264Parser {
     // ITU-T Rec. H.264 table 7-1
     enum NAL_unit_type {
         UNKNOWN         = 0,
-        SLICE           = 1,
+        SLICE           = 1,   // 1 - 5 are VCL NAL units
         SLICE_DPA       = 2,
         SLICE_DPB       = 3,
         SLICE_DPC       = 4,
@@ -73,11 +73,15 @@ class H264Parser {
         END_STREAM      = 11,
         FILLER_DATA     = 12,
         SPS_EXT         = 13,
-        AUXILIARY_SLICE = 19
+        NALU_prefix     = 14,
+        SPS_subset      = 15,
+        AUXILIARY_SLICE = 19,
+        SLICE_EXTENSION = 20
     };
 
     enum SEI_type {
         SEI_TYPE_PIC_TIMING             = 1,
+        SEI_FILLER_PAYLOAD              = 3,
         SEI_TYPE_USER_DATA_UNREGISTERED = 5,
         SEI_TYPE_RECOVERY_POINT         = 6
     };
@@ -132,7 +136,7 @@ class H264Parser {
         }
 
     bool onFrameStart(void) const { return on_frame; }
-    bool onKeyFrameStart(void) const { return on_key_frame; }
+    bool onKeyFrameStart(void) const { return seen_sps && on_key_frame; }
 
     uint pictureWidth(void) const { return pic_width; }
     uint pictureHeight(void) const { return pic_height; }
@@ -147,7 +151,9 @@ class H264Parser {
 
     uint64_t frameAUstreamOffset(void) const {return frame_start_offset;}
     uint64_t keyframeAUstreamOffset(void) const {return keyframe_start_offset;}
+    uint64_t SPSstreamOffset(void) const {return SPS_offset;}
 
+    // == NAL_type AU_delimiter: primary_pic_type = 5
     static int isKeySlice(uint slice_type)
         {
             return (slice_type == SLICE_I   ||
@@ -164,6 +170,7 @@ class H264Parser {
         }
 
     void use_I_forKeyframes(bool val) { I_is_keyframe = val; }
+    bool using_I_forKeyframes(void) const { return I_is_keyframe; }
 
     uint32_t GetTimeScale(void) const { return timeScale; }
 
@@ -172,6 +179,10 @@ class H264Parser {
     void parse_SPS(uint8_t *sps, uint32_t sps_size,
                    bool& interlaced, int32_t& max_ref_frames);
 
+    bool seen_SPS(void) const { return seen_sps; }
+
+    bool found_AU(void) const { return AU_pending; }
+
   private:
     enum constants {EXTENDED_SAR = 255};
 
@@ -247,6 +258,7 @@ class H264Parser {
     bool       fixedRate;
 
     uint64_t   pkt_offset, AU_offset, frame_start_offset, keyframe_start_offset;
+    uint64_t   SPS_offset;
     bool       on_frame, on_key_frame;
 };
 
