Ticket #11435: SPS-H264Parser.patch

File SPS-H264Parser.patch, 17.1 KB (added by jpoet, 11 years ago)

Fix parsing of SEI_TYPE_RECOVERY_POINT

  • mythtv/libs/libmythtv/avformatdecoder.cpp

    diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
    index 5dc65af..9fa7d00 100644
    a b int AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt) 
    31173117    {
    31183118        buf += m_h264_parser->addBytes(buf, buf_end - buf, 0);
    31193119
    3120         if (m_h264_parser->stateChanged())
     3120        if (m_h264_parser->stateChanged() && m_h264_parser->seen_SPS())
    31213121        {
    31223122            if (m_h264_parser->FieldType() != H264Parser::FIELD_BOTTOM)
    31233123            {
  • mythtv/libs/libmythtv/mpeg/H264Parser.cpp

    diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.cpp b/mythtv/libs/libmythtv/mpeg/H264Parser.cpp
    index 3e45c60..af2e137 100644
    a b void H264Parser::Reset(void) 
    116116    prev_pic_parameter_set_id = pic_parameter_set_id = -1;
    117117    prev_field_pic_flag = field_pic_flag = -1;
    118118    prev_bottom_field_flag = bottom_field_flag = -1;
    119     prev_nal_ref_idc = nal_ref_idc = 0;
     119    prev_nal_ref_idc = nal_ref_idc = 111;  //  != [0|1|2|3]
    120120    prev_pic_order_cnt_type = pic_order_cnt_type =
    121121    prev_pic_order_cnt_lsb = pic_order_cnt_lsb = 0;
    122122    prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom = 0;
    123123    prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0] = 0;
    124124    prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1] = 0;
    125125    prev_nal_unit_type = nal_unit_type = UNKNOWN;
    126     prev_idr_pic_id = idr_pic_id = 0;
     126
     127    // The value of idr_pic_id shall be in the range of 0 to 65535, inclusive.
     128    prev_idr_pic_id = idr_pic_id = 65536;
    127129
    128130    log2_max_frame_num = log2_max_pic_order_cnt_lsb = 0;
    129131    seq_parameter_set_id = 0;
    uint32_t H264Parser::addBytes(const uint8_t *bytes, 
    401403    const uint8_t *startP = bytes;
    402404    const uint8_t *endP;
    403405    bool           found_start_code;
     406    bool           good_nal_unit;
    404407
    405408    state_changed = false;
    406409    on_frame      = false;
    uint32_t H264Parser::addBytes(const uint8_t *bytes, 
    474477            nal_unit_type = sync_accumulator & 0x1f;
    475478            nal_ref_idc = (sync_accumulator >> 5) & 0x3;
    476479
    477             if (nal_unit_type == SPS || nal_unit_type == PPS ||
    478                 nal_unit_type == SEI || NALisSlice(nal_unit_type))
     480            good_nal_unit = true;
     481            if (nal_ref_idc)
    479482            {
    480                 /* This is a NAL we need to parse. We may have the body
    481                  * of it in the part of the stream past to us this call,
    482                  * or we may get the rest in subsequent calls to addBytes.
    483                  * Either way, we set have_unfinished_NAL, so that we
    484                  * start filling the rbsp buffer */
    485                 have_unfinished_NAL = true;
     483                /* nal_ref_idc shall be equal to 0 for all NAL units having
     484                 * nal_unit_type equal to 6, 9, 10, 11, or 12.
     485                 */
     486                if (nal_unit_type == SEI ||
     487                                (nal_unit_type >= AU_DELIMITER &&
     488                                 nal_unit_type <= FILLER_DATA))
     489                    good_nal_unit = false;
    486490            }
    487             else if (nal_unit_type == AU_DELIMITER ||
    488                     (nal_unit_type > SPS_EXT &&
    489                      nal_unit_type < AUXILIARY_SLICE))
     491            else
    490492            {
    491                 set_AU_pending();
     493                /* nal_ref_idc shall not be equal to 0 for NAL units with
     494                 * nal_unit_type equal to 5
     495                 */
     496                if (nal_unit_type == SLICE_IDR)
     497                    good_nal_unit = false;
    492498            }
    493         }
     499
     500            if (good_nal_unit)
     501            {
     502                if (nal_unit_type == SPS || nal_unit_type == PPS ||
     503                    nal_unit_type == SEI || NALisSlice(nal_unit_type))
     504                {
     505                    /* This is a NAL we need to parse. We may have the body
     506                     * of it in the part of the stream past to us this call,
     507                     * or we may get the rest in subsequent calls to addBytes.
     508                     * Either way, we set have_unfinished_NAL, so that we
     509                     * start filling the rbsp buffer */
     510                      have_unfinished_NAL = true;
     511                }
     512                else if (nal_unit_type == AU_DELIMITER ||
     513                        (nal_unit_type > SPS_EXT &&
     514                         nal_unit_type < AUXILIARY_SLICE))
     515                {
     516                    set_AU_pending();
     517                }
     518            }
     519            else
     520                LOG(VB_GENERAL, LOG_ERR,
     521                    "H264Parser::addbytes: malformed NAL units");
     522        } //found start code
    494523    }
    495524
    496525    return startP - bytes;
    void H264Parser::processRBSP(bool rbsp_complete) 
    523552
    524553        set_AU_pending();
    525554
     555        if (!seen_sps)
     556            SPS_offset = pkt_offset;
     557
    526558        decode_SPS(&gb);
    527559    }
    528560    else if (nal_unit_type == PPS)
    void H264Parser::processRBSP(bool rbsp_complete) 
    576608*/
    577609bool H264Parser::decode_Header(GetBitContext *gb)
    578610{
     611    // uint first_mb_in_slice;
     612
    579613    is_keyframe = false;
    580614
    581615    if (log2_max_frame_num == 0 || pic_order_present_flag == -1)
    bool H264Parser::decode_Header(GetBitContext *gb) 
    601635      that precedes the current slice in decoding order and has the
    602636      same value of colour_plane_id.
    603637     */
    604     //uint first_mb_in_slice = get_ue_golomb(gb);
    605     get_ue_golomb(gb); // Replaced above line
     638    /* first_mb_in_slice = */ get_ue_golomb_long(gb);
    606639
    607640    /*
    608641      slice_type specifies the coding type of the slice according to
    bool H264Parser::decode_Header(GetBitContext *gb) 
    611644      When nal_unit_type is equal to 5 (IDR picture), slice_type shall
    612645      be equal to 2, 4, 7, or 9 (I or SI)
    613646     */
    614     slice_type = get_ue_golomb(gb);
     647    slice_type = get_ue_golomb_31(gb);
     648
     649    /* s->pict_type = golomb_to_pict_type[slice_type % 5];
     650     */
    615651
    616652    /*
    617653      pic_parameter_set_id specifies the picture parameter set in
    bool H264Parser::decode_Header(GetBitContext *gb) 
    642678      bitstream....
    643679
    644680      If the current picture is an IDR picture, frame_num shall be equal to 0.
     681
     682      When max_num_ref_frames is equal to 0, slice_type shall be equal to 2, 4, 7, or 9.
    645683    */
    646684
    647685    frame_num = get_bits(gb, log2_max_frame_num);
    bool H264Parser::decode_Header(GetBitContext *gb) 
    685723        is_keyframe = true;
    686724    }
    687725    else
    688         is_keyframe |= I_is_keyframe && isKeySlice(slice_type);
     726        is_keyframe = (I_is_keyframe && isKeySlice(slice_type));
     727
    689728    /*
    690729      pic_order_cnt_lsb specifies the picture order count modulo
    691730      MaxPicOrderCntLsb for the top field of a coded frame or for a coded
    bool H264Parser::decode_Header(GetBitContext *gb) 
    702741    {
    703742        pic_order_cnt_lsb = get_bits(gb, log2_max_pic_order_cnt_lsb);
    704743
    705         if (pic_order_present_flag && !field_pic_flag)
     744        if ((pic_order_present_flag == 1) && !field_pic_flag)
    706745            delta_pic_order_cnt_bottom = get_se_golomb(gb);
    707746        else
    708747            delta_pic_order_cnt_bottom = 0;
    bool H264Parser::decode_Header(GetBitContext *gb) 
    711750        delta_pic_order_cnt_bottom = 0;
    712751
    713752    /*
    714       delta_pic_order_cnt[ 0 ] specifies the picture order count
    715       difference from the expected picture order count for the top
    716       field of a coded frame or for a coded field as specified in
    717       subclause 8.2.1. The value of delta_pic_order_cnt[ 0 ] shall be
    718       in the range of -231 to 231 - 1, inclusive. When this syntax
    719       element is not present in the bitstream for the current slice,
    720       it shall be inferred to be equal to 0.
    721 
    722       delta_pic_order_cnt[ 1 ] specifies the picture order count
    723       difference from the expected picture order count for the bottom
    724       field of a coded frame specified in subclause 8.2.1. The value
    725       of delta_pic_order_cnt[ 1 ] shall be in the range of -231 to 231
    726       - 1, inclusive. When this syntax element is not present in the
    727       bitstream for the current slice, it shall be inferred to be
    728       equal to 0.
    729      */
    730     if (pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag)
     753      delta_pic_order_always_zero_flag equal to 1 specifies that
     754      delta_pic_order_cnt[ 0 ] and delta_pic_order_cnt[ 1 ] are not
     755      present in the slice headers of the sequence and shall be
     756      inferred to be equal to 0. delta_pic_order_always_zero_flag
     757      equal to 0 specifies that delta_pic_order_cnt[ 0 ] is present in
     758      the slice headers of the sequence and delta_pic_order_cnt[ 1 ]
     759      may be present in the slice headers of the sequence.
     760    */
     761    if (delta_pic_order_always_zero_flag)
     762    {
     763        delta_pic_order_cnt[1] = delta_pic_order_cnt[0] = 0;
     764    }
     765    else if (pic_order_cnt_type == 1)
    731766    {
     767        /*
     768          delta_pic_order_cnt[ 0 ] specifies the picture order count
     769          difference from the expected picture order count for the top
     770          field of a coded frame or for a coded field as specified in
     771          subclause 8.2.1. The value of delta_pic_order_cnt[ 0 ] shall
     772          be in the range of -2^31 to 2^31 - 1, inclusive. When this
     773          syntax element is not present in the bitstream for the
     774          current slice, it shall be inferred to be equal to 0.
     775
     776          delta_pic_order_cnt[ 1 ] specifies the picture order count
     777          difference from the expected picture order count for the
     778          bottom field of a coded frame specified in subclause
     779          8.2.1. The value of delta_pic_order_cnt[ 1 ] shall be in the
     780          range of -2^31 to 2^31 - 1, inclusive. When this syntax
     781          element is not present in the bitstream for the current
     782          slice, it shall be inferred to be equal to 0.
     783        */
    732784        delta_pic_order_cnt[0] = get_se_golomb(gb);
    733785
    734         if (pic_order_present_flag && !field_pic_flag)
     786        if ((pic_order_present_flag == 1) && !field_pic_flag)
    735787            delta_pic_order_cnt[1] = get_se_golomb(gb);
    736788        else
    737789            delta_pic_order_cnt[1] = 0;
    738     }
    739     else
    740         delta_pic_order_cnt[0] = 0;
     790     }
    741791
    742792    /*
    743793      redundant_pic_cnt shall be equal to 0 for slices and slice data
    bool H264Parser::decode_Header(GetBitContext *gb) 
    748798      be equal to 0. The value of redundant_pic_cnt shall be in the
    749799      range of 0 to 127, inclusive.
    750800    */
    751 
    752801    redundant_pic_cnt = redundant_pic_cnt_present_flag ? get_ue_golomb(gb) : 0;
    753802
    754803    return true;
    bool H264Parser::decode_Header(GetBitContext *gb) 
    760809void H264Parser::decode_SPS(GetBitContext * gb)
    761810{
    762811    int profile_idc;
     812    int lastScale;
     813    int nextScale;
     814    int deltaScale;
    763815
    764816    seen_sps = true;
    765817
    766     profile_idc = get_bits(gb, 8); // profile_idc
     818    profile_idc = get_bits(gb, 8);
    767819    get_bits1(gb);      // constraint_set0_flag
    768820    get_bits1(gb);      // constraint_set1_flag
    769821    get_bits1(gb);      // constraint_set2_flag
    void H264Parser::decode_SPS(GetBitContext * gb) 
    772824    get_bits(gb, 8);    // level_idc
    773825    get_ue_golomb(gb);  // sps_id
    774826
    775     if (profile_idc >= 100)
     827    if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
     828        profile_idc == 244 || profile_idc == 44  || profile_idc == 83  ||
     829        profile_idc == 86  || profile_idc == 118 || profile_idc == 128 )
    776830    { // high profile
    777         if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc
     831        if ((chroma_format_idc = get_ue_golomb(gb)) == 3)
    778832            separate_colour_plane_flag = (get_bits1(gb) == 1);
    779833
    780834        get_ue_golomb(gb);     // bit_depth_luma_minus8
    void H264Parser::decode_SPS(GetBitContext * gb) 
    785839        {
    786840            for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx)
    787841            {
    788                 if (get_bits1(gb)) // Scaling list presnent
     842                if (get_bits1(gb)) // Scaling list present
    789843                {
     844                    lastScale = nextScale = 8;
    790845                    int sl_n = ((idx < 6) ? 16 : 64);
    791                     for(int sl_i = 0; sl_i < sl_n; sl_i++)
     846                    for(int sl_i = 0; sl_i < sl_n; ++sl_i)
    792847                    {
    793                         get_se_golomb(gb);
     848                        if (nextScale != 0)
     849                        {
     850                            deltaScale = get_se_golomb(gb);
     851                            nextScale = (lastScale + deltaScale + 256) % 256;
     852                        }
     853                        lastScale = (nextScale == 0) ? lastScale : nextScale;
    794854                    }
    795855                }
    796856            }
    void H264Parser::decode_SPS(GetBitContext * gb) 
    809869    int  offset_for_non_ref_pic;
    810870    int  offset_for_top_to_bottom_field;
    811871    uint tmp;
     872    // bool gaps_in_frame_num_allowed_flag;
    812873
    813874    /*
    814875      pic_order_cnt_type specifies the method to decode picture order
    void H264Parser::decode_SPS(GetBitContext * gb) 
    886947      decoding process in case of an inferred gap between values of
    887948      frame_num as specified in subclause 8.2.5.2.
    888949     */
    889     //bool gaps_in_frame_num_allowed_flag = get_bits1(gb);
    890     get_bits1(gb); // Replaced above line
     950    /* gaps_in_frame_num_allowed_flag = */ get_bits1(gb);
    891951
    892952    /*
    893953      pic_width_in_mbs_minus1 plus 1 specifies the width of each
    void H264Parser::decode_SEI(GetBitContext *gb) 
    10491109    {
    10501110        do {
    10511111            type += show_bits(gb, 8);
    1052         } while (get_bits(gb, 8) == 255);
     1112        } while (get_bits(gb, 8) == 0xFF);
    10531113
    10541114        do {
    10551115            size += show_bits(gb, 8);
    1056         } while (get_bits(gb, 8) == 255);
     1116        } while (get_bits(gb, 8) == 0xFF);
    10571117
    10581118        switch (type)
    10591119        {
    1060             case SEI_TYPE_RECOVERY_POINT:
    1061                 recovery_frame_cnt = get_ue_golomb(gb);
    1062                 exact_match_flag = get_bits1(gb);
    1063                 broken_link_flag = get_bits1(gb);
    1064                 changing_group_slice_idc = get_bits(gb, 2);
    1065                 au_contains_keyframe_message = (recovery_frame_cnt == 0);
    1066                 return;
    1067 
    1068             default:
    1069                 skip_bits(gb, size * 8);
    1070                 break;
     1120          case SEI_TYPE_RECOVERY_POINT:
     1121            recovery_frame_cnt = get_ue_golomb(gb);
     1122            exact_match_flag = get_bits1(gb);
     1123            broken_link_flag = get_bits1(gb);
     1124            changing_group_slice_idc = get_bits(gb, 2);
     1125            au_contains_keyframe_message = (recovery_frame_cnt == 0);
     1126            if ((size - 12) > 0)
     1127                skip_bits(gb, (size - 12) * 8);
     1128            return;
     1129
     1130          default:
     1131            skip_bits(gb, size * 8);
     1132            break;
    10711133        }
    10721134    }
    10731135
  • mythtv/libs/libmythtv/mpeg/H264Parser.h

    diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.h b/mythtv/libs/libmythtv/mpeg/H264Parser.h
    index 9c984e0..eb95779 100644
    a b class H264Parser { 
    6060    // ITU-T Rec. H.264 table 7-1
    6161    enum NAL_unit_type {
    6262        UNKNOWN         = 0,
    63         SLICE           = 1,
     63        SLICE           = 1,   // 1 - 5 are VCL NAL units
    6464        SLICE_DPA       = 2,
    6565        SLICE_DPB       = 3,
    6666        SLICE_DPC       = 4,
    class H264Parser { 
    7373        END_STREAM      = 11,
    7474        FILLER_DATA     = 12,
    7575        SPS_EXT         = 13,
    76         AUXILIARY_SLICE = 19
     76        NALU_prefix     = 14,
     77        SPS_subset      = 15,
     78        AUXILIARY_SLICE = 19,
     79        SLICE_EXTENSION = 20
    7780    };
    7881
    7982    enum SEI_type {
    8083        SEI_TYPE_PIC_TIMING             = 1,
     84        SEI_FILLER_PAYLOAD              = 3,
    8185        SEI_TYPE_USER_DATA_UNREGISTERED = 5,
    8286        SEI_TYPE_RECOVERY_POINT         = 6
    8387    };
    class H264Parser { 
    132136        }
    133137
    134138    bool onFrameStart(void) const { return on_frame; }
    135     bool onKeyFrameStart(void) const { return on_key_frame; }
     139    bool onKeyFrameStart(void) const { return seen_sps && on_key_frame; }
    136140
    137141    uint pictureWidth(void) const { return pic_width; }
    138142    uint pictureHeight(void) const { return pic_height; }
    class H264Parser { 
    147151
    148152    uint64_t frameAUstreamOffset(void) const {return frame_start_offset;}
    149153    uint64_t keyframeAUstreamOffset(void) const {return keyframe_start_offset;}
     154    uint64_t SPSstreamOffset(void) const {return SPS_offset;}
    150155
     156    // == NAL_type AU_delimiter: primary_pic_type = 5
    151157    static int isKeySlice(uint slice_type)
    152158        {
    153159            return (slice_type == SLICE_I   ||
    class H264Parser { 
    164170        }
    165171
    166172    void use_I_forKeyframes(bool val) { I_is_keyframe = val; }
     173    bool using_I_forKeyframes(void) const { return I_is_keyframe; }
    167174
    168175    uint32_t GetTimeScale(void) const { return timeScale; }
    169176
    class H264Parser { 
    172179    void parse_SPS(uint8_t *sps, uint32_t sps_size,
    173180                   bool& interlaced, int32_t& max_ref_frames);
    174181
     182    bool seen_SPS(void) const { return seen_sps; }
     183
     184    bool found_AU(void) const { return AU_pending; }
     185
    175186  private:
    176187    enum constants {EXTENDED_SAR = 255};
    177188
    class H264Parser { 
    247258    bool       fixedRate;
    248259
    249260    uint64_t   pkt_offset, AU_offset, frame_start_offset, keyframe_start_offset;
     261    uint64_t   SPS_offset;
    250262    bool       on_frame, on_key_frame;
    251263};
    252264