Ticket #5443: h264-parse-field-flag-v2.patch

File h264-parse-field-flag-v2.patch, 7.7 KB (added by danielk, 4 years ago)

ready to apply version of this patch (formating/includes only changes)

  • libs/libmythtv/mpeg/h264utils.h

     
    3535 
    3636#include <stdint.h> 
    3737 
     38extern "C" { 
     39#include <limits.h> // golomb.h should include this... 
     40#include "golomb.h" 
     41} 
     42 
    3843namespace H264 
    3944{ 
    4045 
     
    174179 
    175180  private: 
    176181    void KeyframePredicate(const uint8_t new_first_NAL_byte); /* throw() */ 
     182    void decode_Header(GetBitContext *gb); 
     183    void decode_SPS(GetBitContext *gb); 
    177184 
    178185    bool    errored; 
    179186    bool    state_changed; 
     
    182189    int64_t  sync_stream_offset; 
    183190 
    184191    uint8_t first_NAL_byte; 
     192    int     log2_max_frame_num; 
     193    int     frame_num; 
     194    int     prev_frame_num; 
    185195 
    186196    bool    saw_AU_delimiter; 
    187197    bool    saw_first_VCL_NAL_unit; 
    188198    bool    saw_sps; 
     199    bool    new_VLC_NAL; 
    189200 
     201    bool    separate_colour_plane_flag; 
     202    bool    frame_mbs_only_flag; 
     203    bool    prev_field_pic_flag; 
     204    bool    prev_bottom_field_flag; 
     205    uint    prev_pic_parameter_set_id; 
     206 
     207 
    190208    bool    did_evaluate_once; 
    191209    bool    keyframe; 
    192210    int64_t keyframe_sync_stream_offset; 
     211 
     212    GetBitContext gb; 
    193213}; 
    194214 
    195215} // namespace H264 
  • libs/libmythtv/mpeg/h264utils.cpp

     
    3333// C headers 
    3434#include <cstring> 
    3535 
    36 // C++ headers 
    37 #include <iostream> 
    38  
    3936// MythTV headers 
     37#include "mythverbose.h" 
    4038#include "h264utils.h" 
    4139 
    4240extern "C" { 
    4341// from libavcodec 
    4442extern const uint8_t *ff_find_start_code(const uint8_t * p, const uint8_t *end, uint32_t * state); 
     43#include "avcodec.h" 
    4544} 
    4645 
    4746namespace H264 
     
    6261 
    6362    first_NAL_byte = H264::NALUnitType::UNKNOWN; 
    6463 
     64    log2_max_frame_num = -1; 
     65    frame_num = 0; 
     66    prev_frame_num = -1; 
     67 
    6568    saw_AU_delimiter = false; 
    6669    saw_first_VCL_NAL_unit = false; 
    6770    saw_sps = false; 
     71    separate_colour_plane_flag = false; 
     72    frame_mbs_only_flag = true; 
     73    new_VLC_NAL = false; 
    6874 
    6975    did_evaluate_once = false; 
    7076    keyframe = false; 
     
    127133    // stage 3: did we see the AU's first VCL NAL unit yet? 
    128134    if (!saw_first_VCL_NAL_unit && NALUnitType::IsVCLType(new_NAL_type)) 
    129135    { 
    130         saw_first_VCL_NAL_unit = true; 
     136        saw_first_VCL_NAL_unit = new_VLC_NAL; 
    131137        saw_AU_delimiter = false; 
    132138        state_changed = true; 
    133139        if (saw_sps) 
     
    154160        if ((sync_accumulator & 0xffffff00) == 0x00000100) 
    155161        { 
    156162            uint8_t k = *(local_bytes-1); 
     163            uint8_t NAL_type = k & 0x1f; 
    157164            sync_stream_offset = stream_offset; 
    158165            keyframe = false; 
    159166 
     167            if (NAL_type == NALUnitType::SPS || 
     168                NAL_type == NALUnitType::SLICE || 
     169                NAL_type == NALUnitType::SLICE_DPA) 
     170            { 
     171                /* 
     172                  bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE 
     173                  bytes larger then the actual read bits 
     174                */ 
     175                if (local_bytes + 20 + FF_INPUT_BUFFER_PADDING_SIZE < 
     176                    local_bytes_end) 
     177                { 
     178                    init_get_bits(&gb, local_bytes, 
     179                                  8 * (local_bytes_end - local_bytes)); 
     180 
     181                    if (NAL_type == NALUnitType::SPS) 
     182                        decode_SPS(&gb); 
     183                    else 
     184                        decode_Header(&gb); 
     185                } 
     186            } 
     187            else if (NAL_type == NALUnitType::SLICE_IDR) 
     188            { 
     189                frame_num = 0; 
     190            } 
     191 
    160192            KeyframePredicate(k); 
    161193            first_NAL_byte = k; 
    162194 
     
    175207    return saw_first_VCL_NAL_unit; 
    176208} 
    177209 
     210void KeyframeSequencer::decode_Header(GetBitContext *gb) 
     211{ 
     212    uint first_mb_in_slice; 
     213    uint slice_type; 
     214    uint pic_parameter_set_id; 
     215    bool field_pic_flag; 
     216    bool bottom_field_flag; 
     217 
     218    if (log2_max_frame_num < 1) 
     219    { 
     220        VERBOSE(VB_IMPORTANT, "KeyframeSequencer::decode_Header: " 
     221                "SPS has not been parsed!"); 
     222        return; 
     223    } 
     224 
     225    new_VLC_NAL = false; 
     226 
     227    prev_frame_num = frame_num; 
     228 
     229    first_mb_in_slice = get_ue_golomb(gb); 
     230    slice_type = get_ue_golomb(gb); 
     231 
     232    pic_parameter_set_id = get_ue_golomb(gb); 
     233    if (pic_parameter_set_id != prev_pic_parameter_set_id) 
     234    { 
     235        new_VLC_NAL = true; 
     236        prev_pic_parameter_set_id = pic_parameter_set_id; 
     237    } 
     238     
     239    if (separate_colour_plane_flag) 
     240        get_bits(gb, 2);  // colour_plane_id 
     241     
     242    frame_num = get_bits(gb, log2_max_frame_num); 
     243 
     244    if (frame_mbs_only_flag) 
     245    { 
     246        new_VLC_NAL = true; 
     247    } 
     248    else 
     249    { 
     250        /* From section 7.3.3 Slice header syntax 
     251           if (!frame_mbs_only_flag) 
     252           { 
     253               field_pic_flag = get_bits1(gb); 
     254               if (field_pic_flag) 
     255                   bottom_field_flag = get_bits1(gb); 
     256           } 
     257        */ 
     258 
     259        field_pic_flag = get_bits1(gb); 
     260        if (field_pic_flag != prev_field_pic_flag) 
     261        { 
     262            new_VLC_NAL = true; 
     263            prev_field_pic_flag = field_pic_flag; 
     264        } 
     265 
     266        if (field_pic_flag) 
     267        { 
     268            bottom_field_flag = get_bits1(gb); 
     269            if (bottom_field_flag != prev_bottom_field_flag) 
     270            { 
     271                new_VLC_NAL = !bottom_field_flag; 
     272                prev_bottom_field_flag = bottom_field_flag; 
     273            } 
     274        } 
     275    } 
     276} 
     277 
     278/* 
     279 * libavcodec used for example 
     280 */ 
     281void KeyframeSequencer::decode_SPS(GetBitContext * gb) 
     282{ 
     283    int profile_idc, chroma_format_idc; 
     284 
     285    profile_idc = get_bits(gb, 8); // profile_idc 
     286    get_bits1(gb);   // constraint_set0_flag 
     287    get_bits1(gb);   // constraint_set1_flag 
     288    get_bits1(gb);   // constraint_set2_flag 
     289    get_bits1(gb);   // constraint_set3_flag 
     290    get_bits(gb, 4); // reserved 
     291    get_bits(gb, 8); // level_idc 
     292    get_ue_golomb(gb);  // sps_id 
     293 
     294    if (profile_idc >= 100) 
     295    { // high profile 
     296        if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc 
     297            separate_colour_plane_flag = (get_bits1(gb) == 1); 
     298 
     299        get_ue_golomb(gb);  // bit_depth_luma_minus8 
     300        get_ue_golomb(gb);  // bit_depth_chroma_minus8 
     301        get_bits1(gb);      // qpprime_y_zero_transform_bypass_flag 
     302 
     303        if (get_bits1(gb))      // seq_scaling_matrix_present_flag 
     304        { 
     305            for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx) 
     306            { 
     307                get_bits1(gb);  // scaling_list 
     308            } 
     309        } 
     310    } 
     311 
     312    log2_max_frame_num = get_ue_golomb(gb) + 4; 
     313 
     314    uint pic_order_cnt_type; 
     315    uint log2_max_pic_order_cnt_lsb; 
     316    bool delta_pic_order_always_zero_flag; 
     317    int  offset_for_non_ref_pic; 
     318    int  offset_for_top_to_bottom_field; 
     319    uint tmp; 
     320    uint num_ref_frames; 
     321    bool gaps_in_frame_num_allowed_flag; 
     322    uint pic_width_in_mbs; 
     323    uint pic_height_in_map_units; 
     324 
     325    pic_order_cnt_type = get_ue_golomb(gb); 
     326    if (pic_order_cnt_type == 0) 
     327    { 
     328        log2_max_pic_order_cnt_lsb = get_ue_golomb(gb) + 4; 
     329    } 
     330    else if (pic_order_cnt_type == 1) 
     331    { 
     332        delta_pic_order_always_zero_flag = get_bits1(gb); 
     333        offset_for_non_ref_pic = get_se_golomb(gb); 
     334        offset_for_top_to_bottom_field = get_se_golomb(gb); 
     335        tmp = get_ue_golomb(gb); 
     336        for (uint idx = 0; idx < tmp; ++idx) 
     337            get_se_golomb(gb);  // offset_for_ref_frame[i] 
     338    } 
     339 
     340    num_ref_frames = get_ue_golomb(gb); 
     341    gaps_in_frame_num_allowed_flag = get_bits1(gb); 
     342    pic_width_in_mbs = get_ue_golomb(gb) + 1; 
     343    pic_height_in_map_units = get_ue_golomb(gb) + 1; 
     344    frame_mbs_only_flag = get_bits1(gb); 
     345} 
     346 
    178347} // namespace H264