public SliceHeader readPart2(
      SliceHeader sh, NALUnit nalUnit, SeqParameterSet sps, PictureParameterSet pps, BitReader in) {
    sh.pps = pps;
    sh.sps = sps;

    sh.frame_num = readU(in, sps.log2_max_frame_num_minus4 + 4, "SH: frame_num");
    if (!sps.frame_mbs_only_flag) {
      sh.field_pic_flag = readBool(in, "SH: field_pic_flag");
      if (sh.field_pic_flag) {
        sh.bottom_field_flag = readBool(in, "SH: bottom_field_flag");
      }
    }
    if (nalUnit.type == NALUnitType.IDR_SLICE) {
      sh.idr_pic_id = readUE(in, "SH: idr_pic_id");
    }
    if (sps.pic_order_cnt_type == 0) {
      sh.pic_order_cnt_lsb =
          readU(in, sps.log2_max_pic_order_cnt_lsb_minus4 + 4, "SH: pic_order_cnt_lsb");
      if (pps.pic_order_present_flag && !sps.field_pic_flag) {
        sh.delta_pic_order_cnt_bottom = readSE(in, "SH: delta_pic_order_cnt_bottom");
      }
    }
    sh.delta_pic_order_cnt = new int[2];
    if (sps.pic_order_cnt_type == 1 && !sps.delta_pic_order_always_zero_flag) {
      sh.delta_pic_order_cnt[0] = readSE(in, "SH: delta_pic_order_cnt[0]");
      if (pps.pic_order_present_flag && !sps.field_pic_flag)
        sh.delta_pic_order_cnt[1] = readSE(in, "SH: delta_pic_order_cnt[1]");
    }
    if (pps.redundant_pic_cnt_present_flag) {
      sh.redundant_pic_cnt = readUE(in, "SH: redundant_pic_cnt");
    }
    if (sh.slice_type == SliceType.B) {
      sh.direct_spatial_mv_pred_flag = readBool(in, "SH: direct_spatial_mv_pred_flag");
    }
    if (sh.slice_type == SliceType.P
        || sh.slice_type == SliceType.SP
        || sh.slice_type == SliceType.B) {
      sh.num_ref_idx_active_override_flag = readBool(in, "SH: num_ref_idx_active_override_flag");
      if (sh.num_ref_idx_active_override_flag) {
        sh.num_ref_idx_active_minus1[0] = readUE(in, "SH: num_ref_idx_l0_active_minus1");
        if (sh.slice_type == SliceType.B) {
          sh.num_ref_idx_active_minus1[1] = readUE(in, "SH: num_ref_idx_l1_active_minus1");
        }
      }
    }
    readRefPicListReordering(sh, in);
    if ((pps.weighted_pred_flag && (sh.slice_type == SliceType.P || sh.slice_type == SliceType.SP))
        || (pps.weighted_bipred_idc == 1 && sh.slice_type == SliceType.B))
      readPredWeightTable(sps, pps, sh, in);
    if (nalUnit.nal_ref_idc != 0) readDecoderPicMarking(nalUnit, sh, in);
    if (pps.entropy_coding_mode_flag && sh.slice_type.isInter()) {
      sh.cabac_init_idc = readUE(in, "SH: cabac_init_idc");
    }
    sh.slice_qp_delta = readSE(in, "SH: slice_qp_delta");
    if (sh.slice_type == SliceType.SP || sh.slice_type == SliceType.SI) {
      if (sh.slice_type == SliceType.SP) {
        sh.sp_for_switch_flag = readBool(in, "SH: sp_for_switch_flag");
      }
      sh.slice_qs_delta = readSE(in, "SH: slice_qs_delta");
    }
    if (pps.deblocking_filter_control_present_flag) {
      sh.disable_deblocking_filter_idc = readUE(in, "SH: disable_deblocking_filter_idc");
      if (sh.disable_deblocking_filter_idc != 1) {
        sh.slice_alpha_c0_offset_div2 = readSE(in, "SH: slice_alpha_c0_offset_div2");
        sh.slice_beta_offset_div2 = readSE(in, "SH: slice_beta_offset_div2");
      }
    }
    if (pps.num_slice_groups_minus1 > 0
        && pps.slice_group_map_type >= 3
        && pps.slice_group_map_type <= 5) {
      int len =
          getPicHeightInMbs(sps)
              * (sps.pic_width_in_mbs_minus1 + 1)
              / (pps.slice_group_change_rate_minus1 + 1);
      if ((getPicHeightInMbs(sps) * (sps.pic_width_in_mbs_minus1 + 1))
              % (pps.slice_group_change_rate_minus1 + 1)
          > 0) len += 1;

      len = CeilLog2(len + 1);
      sh.slice_group_change_cycle = readU(in, len, "SH: slice_group_change_cycle");
    }

    return sh;
  }