Example #1
0
        public static ScalingList read(BitReader inb, int sizeOfScalingList)
        {
            ScalingList sl = new ScalingList();

            sl.scalingList = new int[sizeOfScalingList];
            int lastScale = 8;
            int nextScale = 8;

            for (int j = 0; j < sizeOfScalingList; j++)
            {
                if (nextScale != 0)
                {
                    int deltaScale = CAVLCReader.readSE(inb, "deltaScale");
                    nextScale = (lastScale + deltaScale + 256) % 256;
                    sl.useDefaultScalingMatrixFlag = (j == 0 && nextScale == 0);
                }
                sl.scalingList[j] = nextScale == 0 ? lastScale : nextScale;
                lastScale         = sl.scalingList[j];
            }
            return(sl);
        }
Example #2
0
 private static void readWeightOffset(SeqParameterSet sps, PictureParameterSet pps, SliceHeader sh, BitReader inb,
                                      int[] numRefs, int list)
 {
     for (int i = 0; i < numRefs[list]; i++)
     {
         bool luma_weight_l0_flag = CAVLCReader.readBool(inb, "SH: luma_weight_l0_flag");
         if (luma_weight_l0_flag)
         {
             sh.pred_weight_table.luma_weight[list][i] = CAVLCReader.readSE(inb, "SH: weight");
             sh.pred_weight_table.luma_offset[list][i] = CAVLCReader.readSE(inb, "SH: offset");
         }
         if (sps.chroma_format_idc != ColorSpace.MONO)
         {
             bool chroma_weight_l0_flag = CAVLCReader.readBool(inb, "SH: chroma_weight_l0_flag");
             if (chroma_weight_l0_flag)
             {
                 sh.pred_weight_table.chroma_weight[list][0][i] = CAVLCReader.readSE(inb, "SH: weight");
                 sh.pred_weight_table.chroma_offset[list][0][i] = CAVLCReader.readSE(inb, "SH: offset");
                 sh.pred_weight_table.chroma_weight[list][1][i] = CAVLCReader.readSE(inb, "SH: weight");
                 sh.pred_weight_table.chroma_offset[list][1][i] = CAVLCReader.readSE(inb, "SH: offset");
             }
         }
     }
 }
Example #3
0
        public static SeqParameterSet read(MemoryStream isb)
        {
            BitReader       inb = new BitReader(isb);
            SeqParameterSet sps = new SeqParameterSet();

            sps.profile_idc           = CAVLCReader.readNBit(inb, 8, "SPS: profile_idc");
            sps.constraint_set_0_flag = CAVLCReader.readBool(inb, "SPS: constraint_set_0_flag");
            sps.constraint_set_1_flag = CAVLCReader.readBool(inb, "SPS: constraint_set_1_flag");
            sps.constraint_set_2_flag = CAVLCReader.readBool(inb, "SPS: constraint_set_2_flag");
            sps.constraint_set_3_flag = CAVLCReader.readBool(inb, "SPS: constraint_set_3_flag");
            CAVLCReader.readNBit(inb, 4, "SPS: reserved_zero_4bits");
            sps.level_idc            = (int)CAVLCReader.readNBit(inb, 8, "SPS: level_idc");
            sps.seq_parameter_set_id = CAVLCReader.readUE(inb, "SPS: seq_parameter_set_id");

            if (sps.profile_idc == 100 || sps.profile_idc == 110 || sps.profile_idc == 122 || sps.profile_idc == 144)
            {
                sps.chroma_format_idc = getColor(CAVLCReader.readUE(inb, "SPS: chroma_format_idc"));
                if (sps.chroma_format_idc == ColorSpace.YUV444)
                {
                    sps.residual_color_transform_flag = CAVLCReader.readBool(inb, "SPS: residual_color_transform_flag");
                }
                sps.bit_depth_luma_minus8   = CAVLCReader.readUE(inb, "SPS: bit_depth_luma_minus8");
                sps.bit_depth_chroma_minus8 = CAVLCReader.readUE(inb, "SPS: bit_depth_chroma_minus8");
                sps.qpprime_y_zero_transform_bypass_flag = CAVLCReader.readBool(inb, "SPS: qpprime_y_zero_transform_bypass_flag");
                bool seqScalingMatrixPresent = CAVLCReader.readBool(inb, "SPS: seq_scaling_matrix_present_lag");
                if (seqScalingMatrixPresent)
                {
                    readScalingListMatrix(inb, sps);
                }
            }
            else
            {
                sps.chroma_format_idc = ColorSpace.YUV420;
            }
            sps.log2_max_frame_num_minus4 = CAVLCReader.readUE(inb, "SPS: log2_max_frame_num_minus4");
            sps.pic_order_cnt_type        = CAVLCReader.readUE(inb, "SPS: pic_order_cnt_type");
            if (sps.pic_order_cnt_type == 0)
            {
                sps.log2_max_pic_order_cnt_lsb_minus4 = CAVLCReader.readUE(inb, "SPS: log2_max_pic_order_cnt_lsb_minus4");
            }
            else if (sps.pic_order_cnt_type == 1)
            {
                sps.delta_pic_order_always_zero_flag      = CAVLCReader.readBool(inb, "SPS: delta_pic_order_always_zero_flag");
                sps.offset_for_non_ref_pic                = CAVLCReader.readSE(inb, "SPS: offset_for_non_ref_pic");
                sps.offset_for_top_to_bottom_field        = CAVLCReader.readSE(inb, "SPS: offset_for_top_to_bottom_field");
                sps.num_ref_frames_in_pic_order_cnt_cycle = CAVLCReader.readUE(inb, "SPS: num_ref_frames_in_pic_order_cnt_cycle");
                sps.offsetForRefFrame = new int[sps.num_ref_frames_in_pic_order_cnt_cycle];
                for (int i = 0; i < sps.num_ref_frames_in_pic_order_cnt_cycle; i++)
                {
                    sps.offsetForRefFrame[i] = CAVLCReader.readSE(inb, "SPS: offsetForRefFrame [" + i + "]");
                }
            }
            sps.num_ref_frames = CAVLCReader.readUE(inb, "SPS: num_ref_frames");
            sps.gaps_in_frame_num_value_allowed_flag = CAVLCReader.readBool(inb, "SPS: gaps_in_frame_num_value_allowed_flag");
            sps.pic_width_in_mbs_minus1        = CAVLCReader.readUE(inb, "SPS: pic_width_in_mbs_minus1");
            sps.pic_height_in_map_units_minus1 = CAVLCReader.readUE(inb, "SPS: pic_height_in_map_units_minus1");
            sps.frame_mbs_only_flag            = CAVLCReader.readBool(inb, "SPS: frame_mbs_only_flag");
            if (!sps.frame_mbs_only_flag)
            {
                sps.mb_adaptive_frame_field_flag = CAVLCReader.readBool(inb, "SPS: mb_adaptive_frame_field_flag");
            }
            sps.direct_8x8_inference_flag = CAVLCReader.readBool(inb, "SPS: direct_8x8_inference_flag");
            sps.frame_cropping_flag       = CAVLCReader.readBool(inb, "SPS: frame_cropping_flag");
            if (sps.frame_cropping_flag)
            {
                sps.frame_crop_left_offset   = CAVLCReader.readUE(inb, "SPS: frame_crop_left_offset");
                sps.frame_crop_right_offset  = CAVLCReader.readUE(inb, "SPS: frame_crop_right_offset");
                sps.frame_crop_top_offset    = CAVLCReader.readUE(inb, "SPS: frame_crop_top_offset");
                sps.frame_crop_bottom_offset = CAVLCReader.readUE(inb, "SPS: frame_crop_bottom_offset");
            }
            bool vui_parameters_present_flag = CAVLCReader.readBool(inb, "SPS: vui_parameters_present_flag");

            if (vui_parameters_present_flag)
            {
                sps.vuiParams = readVUIParameters(inb);
            }

            return(sps);
        }
Example #4
0
        public static PictureParameterSet read(MemoryStream iss)
        {
            BitReader           inb = new BitReader(iss);
            PictureParameterSet pps = new PictureParameterSet();

            pps.pic_parameter_set_id     = CAVLCReader.readUE(inb, "PPS: pic_parameter_set_id");
            pps.seq_parameter_set_id     = CAVLCReader.readUE(inb, "PPS: seq_parameter_set_id");
            pps.entropy_coding_mode_flag = CAVLCReader.readBool(inb, "PPS: entropy_coding_mode_flag");
            pps.pic_order_present_flag   = CAVLCReader.readBool(inb, "PPS: pic_order_present_flag");
            pps.num_slice_groups_minus1  = CAVLCReader.readUE(inb, "PPS: num_slice_groups_minus1");
            if (pps.num_slice_groups_minus1 > 0)
            {
                pps.slice_group_map_type = CAVLCReader.readUE(inb, "PPS: slice_group_map_type");
                pps.top_left             = new int[pps.num_slice_groups_minus1 + 1];
                pps.bottom_right         = new int[pps.num_slice_groups_minus1 + 1];
                pps.run_length_minus1    = new int[pps.num_slice_groups_minus1 + 1];
                if (pps.slice_group_map_type == 0)
                {
                    for (int iGroup = 0; iGroup <= pps.num_slice_groups_minus1; iGroup++)
                    {
                        pps.run_length_minus1[iGroup] = CAVLCReader.readUE(inb, "PPS: run_length_minus1");
                    }
                }
                else if (pps.slice_group_map_type == 2)
                {
                    for (int iGroup = 0; iGroup < pps.num_slice_groups_minus1; iGroup++)
                    {
                        pps.top_left[iGroup]     = CAVLCReader.readUE(inb, "PPS: top_left");
                        pps.bottom_right[iGroup] = CAVLCReader.readUE(inb, "PPS: bottom_right");
                    }
                }
                else if (pps.slice_group_map_type == 3 || pps.slice_group_map_type == 4 || pps.slice_group_map_type == 5)
                {
                    pps.slice_group_change_direction_flag = CAVLCReader.readBool(inb, "PPS: slice_group_change_direction_flag");
                    pps.slice_group_change_rate_minus1    = CAVLCReader.readUE(inb, "PPS: slice_group_change_rate_minus1");
                }
                else if (pps.slice_group_map_type == 6)
                {
                    int NumberBitsPerSliceGroupId;
                    if (pps.num_slice_groups_minus1 + 1 > 4)
                    {
                        NumberBitsPerSliceGroupId = 3;
                    }
                    else if (pps.num_slice_groups_minus1 + 1 > 2)
                    {
                        NumberBitsPerSliceGroupId = 2;
                    }
                    else
                    {
                        NumberBitsPerSliceGroupId = 1;
                    }
                    int pic_size_in_map_units_minus1 = CAVLCReader.readUE(inb, "PPS: pic_size_in_map_units_minus1");
                    pps.slice_group_id = new int[pic_size_in_map_units_minus1 + 1];
                    for (int i = 0; i <= pic_size_in_map_units_minus1; i++)
                    {
                        pps.slice_group_id[i] = CAVLCReader.readU(inb, NumberBitsPerSliceGroupId, "PPS: slice_group_id [" + i + "]f");
                    }
                }
            }
            pps.num_ref_idx_active_minus1 = new int[] { CAVLCReader.readUE(inb, "PPS: num_ref_idx_l0_active_minus1"), CAVLCReader.readUE(inb, "PPS: num_ref_idx_l1_active_minus1") };
            pps.weighted_pred_flag        = CAVLCReader.readBool(inb, "PPS: weighted_pred_flag");
            pps.weighted_bipred_idc       = CAVLCReader.readNBit(inb, 2, "PPS: weighted_bipred_idc");
            pps.pic_init_qp_minus26       = CAVLCReader.readSE(inb, "PPS: pic_init_qp_minus26");
            pps.pic_init_qs_minus26       = CAVLCReader.readSE(inb, "PPS: pic_init_qs_minus26");
            pps.chroma_qp_index_offset    = CAVLCReader.readSE(inb, "PPS: chroma_qp_index_offset");
            pps.deblocking_filter_control_present_flag = CAVLCReader.readBool(inb, "PPS: deblocking_filter_control_present_flag");
            pps.constrained_intra_pred_flag            = CAVLCReader.readBool(inb, "PPS: constrained_intra_pred_flag");
            pps.redundant_pic_cnt_present_flag         = CAVLCReader.readBool(inb, "PPS: redundant_pic_cnt_present_flag");
            if (CAVLCReader.moreRBSPData(inb))
            {
                pps.extended = new PictureParameterSet.PPSExt();
                pps.extended.transform_8x8_mode_flag = CAVLCReader.readBool(inb, "PPS: transform_8x8_mode_flag");
                bool pic_scaling_matrix_present_flag = CAVLCReader.readBool(inb, "PPS: pic_scaling_matrix_present_flag");
                if (pic_scaling_matrix_present_flag)
                {
                    for (int i = 0; i < 6 + 2 * (pps.extended.transform_8x8_mode_flag ? 1 : 0); i++)
                    {
                        bool pic_scaling_list_present_flag = CAVLCReader.readBool(inb, "PPS: pic_scaling_list_present_flag");
                        if (pic_scaling_list_present_flag)
                        {
                            pps.extended.scalindMatrix.ScalingList4x4 = new ScalingList[8];
                            pps.extended.scalindMatrix.ScalingList8x8 = new ScalingList[8];
                            if (i < 6)
                            {
                                pps.extended.scalindMatrix.ScalingList4x4[i] = ScalingList.read(inb, 16);
                            }
                            else
                            {
                                pps.extended.scalindMatrix.ScalingList8x8[i - 6] = ScalingList.read(inb, 64);
                            }
                        }
                    }
                }
                pps.extended.second_chroma_qp_index_offset = CAVLCReader.readSE(inb, "PPS: second_chroma_qp_index_offset");
            }

            return(pps);
        }
Example #5
0
        public SliceHeader readPart2(SliceHeader sh, NALUnit nalUnit, SeqParameterSet sps, PictureParameterSet pps,
                                     BitReader inb)
        {
            sh.pps = pps;
            sh.sps = sps;

            sh.frame_num = CAVLCReader.readU(inb, sps.log2_max_frame_num_minus4 + 4, "SH: frame_num");
            if (!sps.frame_mbs_only_flag)
            {
                sh.field_pic_flag = CAVLCReader.readBool(inb, "SH: field_pic_flag");
                if (sh.field_pic_flag)
                {
                    sh.bottom_field_flag = CAVLCReader.readBool(inb, "SH: bottom_field_flag");
                }
            }
            if (nalUnit.type == NALUnitType.IDR_SLICE)
            {
                sh.idr_pic_id = CAVLCReader.readUE(inb, "SH: idr_pic_id");
            }
            if (sps.pic_order_cnt_type == 0)
            {
                sh.pic_order_cnt_lsb = CAVLCReader.readU(inb, 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 = CAVLCReader.readSE(inb, "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] = CAVLCReader.readSE(inb, "SH: delta_pic_order_cnt[0]");
                if (pps.pic_order_present_flag && !sps.field_pic_flag)
                {
                    sh.delta_pic_order_cnt[1] = CAVLCReader.readSE(inb, "SH: delta_pic_order_cnt[1]");
                }
            }
            if (pps.redundant_pic_cnt_present_flag)
            {
                sh.redundant_pic_cnt = CAVLCReader.readUE(inb, "SH: redundant_pic_cnt");
            }
            if (sh.slice_type == SliceType.B)
            {
                sh.direct_spatial_mv_pred_flag = CAVLCReader.readBool(inb, "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 = CAVLCReader.readBool(inb, "SH: num_ref_idx_active_override_flag");
                if (sh.num_ref_idx_active_override_flag)
                {
                    sh.num_ref_idx_active_minus1[0] = CAVLCReader.readUE(inb, "SH: num_ref_idx_l0_active_minus1");
                    if (sh.slice_type == SliceType.B)
                    {
                        sh.num_ref_idx_active_minus1[1] = CAVLCReader.readUE(inb, "SH: num_ref_idx_l1_active_minus1");
                    }
                }
            }
            readRefPicListReorderinbg(sh, inb);
            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, inb);
            }
            if (nalUnit.nal_ref_idc != 0)
            {
                readDecoderPicMarkinbg(nalUnit, sh, inb);
            }
            if (pps.entropy_coding_mode_flag && sh.slice_type.isInter())
            {
                sh.cabac_init_idc = CAVLCReader.readUE(inb, "SH: cabac_inbit_idc");
            }
            sh.slice_qp_delta = CAVLCReader.readSE(inb, "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 = CAVLCReader.readBool(inb, "SH: sp_for_switch_flag");
                }
                sh.slice_qs_delta = CAVLCReader.readSE(inb, "SH: slice_qs_delta");
            }
            if (pps.deblocking_filter_control_present_flag)
            {
                sh.disable_deblocking_filter_idc = CAVLCReader.readUE(inb, "SH: disable_deblockinbg_filter_idc");
                if (sh.disable_deblocking_filter_idc != 1)
                {
                    sh.slice_alpha_c0_offset_div2 = CAVLCReader.readSE(inb, "SH: slice_alpha_c0_offset_div2");
                    sh.slice_beta_offset_div2     = CAVLCReader.readSE(inb, "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 = Utility.getPicHeightInMbs(sps) * (sps.pic_width_in_mbs_minus1 + 1)
                          / (pps.slice_group_change_rate_minus1 + 1);
                if ((Utility.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 = CAVLCReader.readU(inb, len, "SH: slice_group_change_cycle");
            }

            return(sh);
        }