Exemple #1
0
        public static SeqParameterSetExt read(MemoryStream iss)
        {
            BitReader inb = new BitReader(iss);

            SeqParameterSetExt spse = new SeqParameterSetExt();

            spse.seq_parameter_set_id = CAVLCReader.readUE(inb, "SPSE: seq_parameter_set_id");
            spse.aux_format_idc       = CAVLCReader.readUE(inb, "SPSE: aux_format_idc");
            if (spse.aux_format_idc != 0)
            {
                spse.bit_depth_aux_minus8    = CAVLCReader.readUE(inb, "SPSE: bit_depth_aux_minus8");
                spse.alpha_incr_flag         = CAVLCReader.readBool(inb, "SPSE: alpha_incr_flag");
                spse.alpha_opaque_value      = CAVLCReader.readU(inb, spse.bit_depth_aux_minus8 + 9, "SPSE: alpha_opaque_value");
                spse.alpha_transparent_value = CAVLCReader.readU(inb, spse.bit_depth_aux_minus8 + 9, "SPSE: alpha_transparent_value");
            }
            spse.additional_extension_flag = CAVLCReader.readBool(inb, "SPSE: additional_extension_flag");

            return(spse);
        }
        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);
        }
Exemple #3
0
        public int readCoeffs(BitReader inb, VLC coeffTokenTab, VLC[] totalZerosTab, int[] coeffLevel, int firstCoeff,
                              int nCoeff, int[] zigzag)
        {
            int coeffToken    = coeffTokenTab.readVLC(inb);
            int totalCoeffx   = totalCoeff(coeffToken);
            int trailingOnesx = trailingOnes(coeffToken);

//        System.out.println("Coeff token. Total: " + totalCoeff + ", trailOne: " + trailingOnes);

            // blockType.getMaxCoeffs();
            // if (blockType == BlockType.BLOCK_CHROMA_DC)
            // maxCoeff = 16 / (color.compWidth[1] * color.compHeight[1]);

            if (totalCoeffx > 0)
            {
                int suffixLength = totalCoeffx > 10 && trailingOnesx < 3 ? 1 : 0;

                int[] level = new int[totalCoeffx];
                int   i;
                for (i = 0; i < trailingOnesx; i++)
                {
                    level[i] = 1 - 2 * inb.read1Bit();
                }

                for (; i < totalCoeffx; i++)
                {
                    int level_prefix    = CAVLCReader.readZeroBitCount(inb, "");
                    int levelSuffixSize = suffixLength;
                    if (level_prefix == 14 && suffixLength == 0)
                    {
                        levelSuffixSize = 4;
                    }
                    if (level_prefix >= 15)
                    {
                        levelSuffixSize = level_prefix - 3;
                    }

                    int levelCode = (Min(15, level_prefix) << suffixLength);
                    if (levelSuffixSize > 0)
                    {
                        int level_suffix = CAVLCReader.readU(inb, levelSuffixSize, "RB: level_suffix");
                        levelCode += level_suffix;
                    }
                    if (level_prefix >= 15 && suffixLength == 0)
                    {
                        levelCode += 15;
                    }
                    if (level_prefix >= 16)
                    {
                        levelCode += (1 << (level_prefix - 3)) - 4096;
                    }
                    if (i == trailingOnesx && trailingOnesx < 3)
                    {
                        levelCode += 2;
                    }

                    if (levelCode % 2 == 0)
                    {
                        level[i] = (levelCode + 2) >> 1;
                    }
                    else
                    {
                        level[i] = (-levelCode - 1) >> 1;
                    }

                    if (suffixLength == 0)
                    {
                        suffixLength = 1;
                    }
                    if (Abs(level[i]) > (3 << (suffixLength - 1)) && suffixLength < 6)
                    {
                        suffixLength++;
                    }
                }

                int zerosLeft;
                if (totalCoeffx < nCoeff)
                {
                    if (coeffLevel.Length == 4)
                    {
                        zerosLeft = H264Const.totalZeros4[totalCoeffx - 1].readVLC(inb);
                    }
                    else if (coeffLevel.Length == 8)
                    {
                        zerosLeft = H264Const.totalZeros8[totalCoeffx - 1].readVLC(inb);
                    }
                    else
                    {
                        zerosLeft = H264Const.totalZeros16[totalCoeffx - 1].readVLC(inb);
                    }
                }
                else
                {
                    zerosLeft = 0;
                }

                int[] runs = new int[totalCoeffx];
                int   r;
                for (r = 0; r < totalCoeffx - 1 && zerosLeft > 0; r++)
                {
                    int run = H264Const.run[Math.Min(6, zerosLeft - 1)].readVLC(inb);
                    zerosLeft -= run;
                    runs[r]    = run;
                }
                runs[r] = zerosLeft;

                for (int j = totalCoeffx - 1, cn = 0; j >= 0 && cn < nCoeff; j--, cn++)
                {
                    cn += runs[j];
                    coeffLevel[zigzag[cn + firstCoeff]] = level[j];
                }
            }

            // System.out.print("[");
            // for (int i = 0; i < nCoeff; i++)
            // System.out.print(coeffLevel[i + firstCoeff] + ", ");
            // System.out.println("]");

            return(coeffToken);
        }
        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);
        }