Exemplo n.º 1
0
        public override int probe(MemoryStream data)
        {
            bool avalidSps = false, avalidPps = false, avalidSh = false;

            foreach (var nalUnit in Utility.splitFrame(data.duplicate()))
            {
                NALUnit marker = NALUnit.read(nalUnit);
                if (marker.type == NALUnitType.IDR_SLICE || marker.type == NALUnitType.NON_IDR_SLICE)
                {
                    BitReader reader = new BitReader(nalUnit);
                    avalidSh = validSh(new SliceHeaderReader().readPart1(reader));
                    break;
                }
                else if (marker.type == NALUnitType.SPS)
                {
                    avalidSps = validSps(SeqParameterSet.read(nalUnit));
                }
                else if (marker.type == NALUnitType.PPS)
                {
                    avalidPps = validPps(PictureParameterSet.read(nalUnit));
                }
            }

            return((avalidSh ? 60 : 0) + (avalidSps ? 20 : 0) + (avalidPps ? 20 : 0));
        }
Exemplo n.º 2
0
        private void writeOffsetWeight(SliceHeader sliceHeader, BitWriter writer, int list)
        {
            SeqParameterSet sps       = sliceHeader.sps;
            int             defaultLW = 1 << sliceHeader.pred_weight_table.luma_log2_weight_denom;
            int             defaultCW = 1 << sliceHeader.pred_weight_table.chroma_log2_weight_denom;

            for (int i = 0; i < sliceHeader.pred_weight_table.luma_weight[list].Length; i++)
            {
                bool flagLuma = sliceHeader.pred_weight_table.luma_weight[list][i] != defaultLW ||
                                sliceHeader.pred_weight_table.luma_offset[list][i] != 0;
                CAVLCWriter.writeBool(writer, flagLuma, "SH: luma_weight_l0_flag");
                if (flagLuma)
                {
                    CAVLCWriter.writeSE(writer, sliceHeader.pred_weight_table.luma_weight[list][i], "SH: luma_weight_l" + list);
                    CAVLCWriter.writeSE(writer, sliceHeader.pred_weight_table.luma_offset[list][i], "SH: luma_offset_l" + list);
                }
                if (sps.chroma_format_idc != ColorSpace.MONO)
                {
                    bool flagChroma = sliceHeader.pred_weight_table.chroma_weight[list][0][i] != defaultCW ||
                                      sliceHeader.pred_weight_table.chroma_offset[list][0][i] != 0 ||
                                      sliceHeader.pred_weight_table.chroma_weight[list][1][i] != defaultCW ||
                                      sliceHeader.pred_weight_table.chroma_offset[list][1][i] != 0;
                    CAVLCWriter.writeBool(writer, flagChroma, "SH: chroma_weight_l0_flag");
                    if (flagChroma)
                    {
                        for (int j = 0; j < 2; j++)
                        {
                            CAVLCWriter.writeSE(writer, sliceHeader.pred_weight_table.chroma_weight[list][j][i], "SH: chroma_weight_l" + list);
                            CAVLCWriter.writeSE(writer, sliceHeader.pred_weight_table.chroma_offset[list][j][i], "SH: chroma_offset_l" + list);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static SeqParameterSet readSPS(System.IO.MemoryStream data)
        {
            System.IO.MemoryStream input = data.duplicate();
            Utility.unescapeNAL(input);
            SeqParameterSet sps = SeqParameterSet.read(input);

            return(sps);
        }
Exemplo n.º 4
0
 public static System.IO.MemoryStream writeSPS(SeqParameterSet sps, int approxSize)
 {
     System.IO.MemoryStream output = new System.IO.MemoryStream(approxSize + 8);
     sps.write(output);
     output.flip();
     Utility.escapeNAL(output);
     return(output);
 }
Exemplo n.º 5
0
 public static SampleEntry createMOVSampleEntry(SeqParameterSet initSPS, PictureParameterSet initPPS)
 {
     System.IO.MemoryStream bb1 = new System.IO.MemoryStream(512), bb2 = new System.IO.MemoryStream(512);
     initSPS.write(bb1);
     initPPS.write(bb2);
     bb1.flip();
     bb2.flip();
     return(createMOVSampleEntry(new System.IO.MemoryStream[] { bb1 }.ToList(), new System.IO.MemoryStream[] { bb2 }.ToList()));
 }
Exemplo n.º 6
0
 public void addSps(List <MemoryStream> spsList)
 {
     foreach (var byteBuffer in spsList)
     {
         MemoryStream dup = byteBuffer.duplicate();
         Utility.unescapeNAL(dup);
         SeqParameterSet s = SeqParameterSet.read(dup);
         sps.Add(s.seq_parameter_set_id, s);
     }
 }
Exemplo n.º 7
0
        public CAVLC(SeqParameterSet sps, PictureParameterSet pps, int mbW, int mbH)
        {
            this.color       = sps.chroma_format_idc;
            this.chromaDCVLC = codeTableChromaDC();
            this.mbWidth     = sps.pic_width_in_mbs_minus1 + 1;

            this.mbMask = (1 << mbH) - 1;

            tokensLeft = new int[4];
            tokensTop  = new int[mbWidth << mbW];
        }
Exemplo n.º 8
0
        public static Size getPicSize(SeqParameterSet sps)
        {
            int w = (sps.pic_width_in_mbs_minus1 + 1) << 4;
            int h = getPicHeightInMbs(sps) << 4;

            if (sps.frame_cropping_flag)
            {
                w -= (sps.frame_crop_left_offset + sps.frame_crop_right_offset) << sps.chroma_format_idc.compWidth[1];
                h -= (sps.frame_crop_top_offset + sps.frame_crop_bottom_offset) << sps.chroma_format_idc.compHeight[1];
            }
            return(new Size(w, h));
        }
Exemplo n.º 9
0
        private void writePredWeightTable(SliceHeader sliceHeader, BitWriter writer)
        {
            SeqParameterSet sps = sliceHeader.sps;

            CAVLCWriter.writeUE(writer, sliceHeader.pred_weight_table.luma_log2_weight_denom, "SH: luma_log2_weight_denom");
            if (sps.chroma_format_idc != ColorSpace.MONO)
            {
                CAVLCWriter.writeUE(writer, sliceHeader.pred_weight_table.chroma_log2_weight_denom, "SH: chroma_log2_weight_denom");
            }

            writeOffsetWeight(sliceHeader, writer, 0);
            if (sliceHeader.slice_type == SliceType.B)
            {
                writeOffsetWeight(sliceHeader, writer, 1);
            }
        }
Exemplo n.º 10
0
        public static Frame createFrame(SeqParameterSet sps, int[][] buffer, int frame_num, int[][][][] mvs,
                                        Frame[][][] refsUsed, int POC)
        {
            int width  = sps.pic_width_in_mbs_minus1 + 1 << 4;
            int height = Utility.getPicHeightInMbs(sps) << 4;

            Rect crop = null;

            if (sps.frame_cropping_flag)
            {
                int sX = sps.frame_crop_left_offset << 1;
                int sY = sps.frame_crop_top_offset << 1;
                int w  = width - (sps.frame_crop_right_offset << 1) - sX;
                int h  = height - (sps.frame_crop_bottom_offset << 1) - sY;
                crop = new Rect(sX, sY, w, h);
            }
            return(new Frame(width, height, buffer, ColorSpace.YUV420, crop, frame_num, mvs, refsUsed, POC));
        }
Exemplo n.º 11
0
            public Frame decodeFrame(List <MemoryStream> nalUnits, int[][] buffer)
            {
                Frame result = null;

                foreach (var nalUnit in nalUnits)
                {
                    NALUnit marker = NALUnit.read(nalUnit);

                    Utility.unescapeNAL(nalUnit);

                    switch (marker.type.getValue())
                    {
                    case 1:
                    case 5:
                        if (result == null)
                        {
                            result = init(buffer, nalUnit, marker);
                        }
                        decoder.decode(nalUnit, marker);
                        break;

                    case 8:
                        SeqParameterSet _sps = SeqParameterSet.read(nalUnit);
                        vdecoder.sps.Add(_sps.seq_parameter_set_id, _sps);
                        break;

                    case 7:
                        PictureParameterSet _pps = PictureParameterSet.read(nalUnit);
                        vdecoder.pps.Add(_pps.pic_parameter_set_id, _pps);
                        break;

                    default:
                        break;
                    }
                }

                filter.deblockFrame(result);

                updateReferences(result);

                return(result);
            }
Exemplo n.º 12
0
            private Frame init(int[][] buffer, MemoryStream segment, NALUnit marker)
            {
                firstNu = marker;

                shr = new SliceHeaderReader();
                BitReader br = new BitReader(segment.duplicate());

                firstSliceHeader = shr.readPart1(br);
                activePps        = vdecoder.pps[firstSliceHeader.pic_parameter_set_id];
                activeSps        = vdecoder.sps[activePps.seq_parameter_set_id];
                shr.readPart2(firstSliceHeader, marker, activeSps, activePps, br);
                int picWidthInMbs  = activeSps.pic_width_in_mbs_minus1 + 1;
                int picHeightInMbs = Utility.getPicHeightInMbs(activeSps);

                int[][] nCoeff = (int[][])Array.CreateInstance(typeof(int), new int[] { picHeightInMbs << 2, picWidthInMbs << 2 }); //new int[picHeightInMbs << 2][picWidthInMbs << 2];

                mvs = (int[][][][])Array.CreateInstance(typeof(int), new int[] { 2, picHeightInMbs << 2, picWidthInMbs << 2, 3 });  //new int[2][picHeightInMbs << 2][picWidthInMbs << 2][3];
                MBType[]      mbTypes   = new MBType[picHeightInMbs * picWidthInMbs];
                bool[]        tr8x8Used = new bool[picHeightInMbs * picWidthInMbs];
                int[][]       mbQps     = (int[][])Array.CreateInstance(typeof(int), new int[] { 3, picHeightInMbs * picWidthInMbs });//new int[3][picHeightInMbs * picWidthInMbs];
                SliceHeader[] shs       = new SliceHeader[picHeightInMbs * picWidthInMbs];
                Frame[][][]   refsUsed  = new Frame[picHeightInMbs * picWidthInMbs][][];

                if (vdecoder.sRefs == null)
                {
                    vdecoder.sRefs = new Frame[1 << (firstSliceHeader.sps.log2_max_frame_num_minus4 + 4)];
                    vdecoder.lRefs = new Dictionary <int, Frame>();
                }

                Frame result = createFrame(activeSps, buffer, firstSliceHeader.frame_num, mvs, refsUsed,
                                           vdecoder.poc.calcPOC(firstSliceHeader, firstNu));

                decoder = new SliceDecoder(activeSps, activePps, nCoeff, mvs, mbTypes, mbQps, shs, tr8x8Used, refsUsed,
                                           result, vdecoder.sRefs, vdecoder.lRefs);
                decoder.setDebug(vdecoder.debug);

                filter = new DeblockingFilter(picWidthInMbs, activeSps.bit_depth_chroma_minus8 + 8, nCoeff, mvs, mbTypes,
                                              mbQps, shs, tr8x8Used, refsUsed);

                return(result);
            }
Exemplo n.º 13
0
 private static void readScalingListMatrix(BitReader inb, SeqParameterSet sps)
 {
     sps.scalingMatrix = new ScalingMatrix();
     for (int i = 0; i < 8; i++)
     {
         bool seqScalingListPresentFlag = CAVLCReader.readBool(inb, "SPS: seqScalingListPresentFlag");
         if (seqScalingListPresentFlag)
         {
             sps.scalingMatrix.ScalingList4x4 = new ScalingList[8];
             sps.scalingMatrix.ScalingList8x8 = new ScalingList[8];
             if (i < 6)
             {
                 sps.scalingMatrix.ScalingList4x4[i] = ScalingList.read(inb, 16);
             }
             else
             {
                 sps.scalingMatrix.ScalingList8x8[i - 6] = ScalingList.read(inb, 64);
             }
         }
     }
 }
Exemplo n.º 14
0
        private static void readPredWeightTable(SeqParameterSet sps, PictureParameterSet pps, SliceHeader sh, BitReader inb)
        {
            sh.pred_weight_table = new PredictionWeightTable();
            int[] numRefsminus1 = sh.num_ref_idx_active_override_flag ? sh.num_ref_idx_active_minus1
                    : pps.num_ref_idx_active_minus1;
            int[] nr = new int[] { numRefsminus1[0] + 1, numRefsminus1[1] + 1 };

            sh.pred_weight_table.luma_log2_weight_denom = CAVLCReader.readUE(inb, "SH: luma_log2_weight_denom");
            if (sps.chroma_format_idc != ColorSpace.MONO)
            {
                sh.pred_weight_table.chroma_log2_weight_denom = CAVLCReader.readUE(inb, "SH: chroma_log2_weight_denom");
            }
            int defaultLW = 1 << sh.pred_weight_table.luma_log2_weight_denom;
            int defaultCW = 1 << sh.pred_weight_table.chroma_log2_weight_denom;

            for (int list = 0; list < 2; list++)
            {
                sh.pred_weight_table.luma_weight[list]   = new int[nr[list]];
                sh.pred_weight_table.luma_offset[list]   = new int[nr[list]];
                sh.pred_weight_table.chroma_weight[list] = (int[][])System.Array.CreateInstance(typeof(int), new int[] { 2, nr[list] });
                sh.pred_weight_table.chroma_offset[list] = (int[][])System.Array.CreateInstance(typeof(int), new int[] { 2, nr[list] });
                for (int i = 0; i < nr[list]; i++)
                {
                    sh.pred_weight_table.luma_weight[list][i]      = defaultLW;
                    sh.pred_weight_table.luma_offset[list][i]      = 0;
                    sh.pred_weight_table.chroma_weight[list][0][i] = defaultCW;
                    sh.pred_weight_table.chroma_offset[list][0][i] = 0;
                    sh.pred_weight_table.chroma_weight[list][1][i] = defaultCW;
                    sh.pred_weight_table.chroma_offset[list][1][i] = 0;
                }
            }

            readWeightOffset(sps, pps, sh, inb, nr, 0);
            if (sh.slice_type == SliceType.B)
            {
                readWeightOffset(sps, pps, sh, inb, nr, 1);
            }
        }
Exemplo n.º 15
0
        public static SampleEntry createMOVSampleEntry(List <System.IO.MemoryStream> spsList, List <System.IO.MemoryStream> ppsList)
        {
            SeqParameterSet sps  = readSPS(StreamExtensions.duplicate(spsList[0]));
            AvcCBox         avcC = new AvcCBox(sps.profile_idc, 0, sps.level_idc, spsList, ppsList);

            int codedWidth  = (sps.pic_width_in_mbs_minus1 + 1) << 4;
            int codedHeight = getPicHeightInMbs(sps) << 4;

            int width = sps.frame_cropping_flag ? codedWidth
                        - ((sps.frame_crop_right_offset + sps.frame_crop_left_offset) << sps.chroma_format_idc.compWidth[1])
                    : codedWidth;
            int height = sps.frame_cropping_flag ? codedHeight
                         - ((sps.frame_crop_bottom_offset + sps.frame_crop_top_offset) << sps.chroma_format_idc.compHeight[1])
                    : codedHeight;

            Size size = new Size(width, height);

            SampleEntry se = videoSampleEntry("avc1", size, "JCodec");

            se.Add(avcC);

            return(se);
        }
Exemplo n.º 16
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");
             }
         }
     }
 }
Exemplo n.º 17
0
        public void write(SliceHeader sliceHeader, bool idrSlice, int nalRefIdc, BitWriter writer)
        {
            SeqParameterSet     sps = sliceHeader.sps;
            PictureParameterSet pps = sliceHeader.pps;

            CAVLCWriter.writeUE(writer, sliceHeader.first_mb_in_slice, "SH: first_mb_in_slice");
            CAVLCWriter.writeUE(writer, (int)sliceHeader.slice_type + (sliceHeader.slice_type_restr ? 5 : 0), "SH: slice_type");
            CAVLCWriter.writeUE(writer, sliceHeader.pic_parameter_set_id, "SH: pic_parameter_set_id");
            CAVLCWriter.writeU(writer, sliceHeader.frame_num, sps.log2_max_frame_num_minus4 + 4, "SH: frame_num");
            if (!sps.frame_mbs_only_flag)
            {
                CAVLCWriter.writeBool(writer, sliceHeader.field_pic_flag, "SH: field_pic_flag");
                if (sliceHeader.field_pic_flag)
                {
                    CAVLCWriter.writeBool(writer, sliceHeader.bottom_field_flag, "SH: bottom_field_flag");
                }
            }
            if (idrSlice)
            {
                CAVLCWriter.writeUE(writer, sliceHeader.idr_pic_id, "SH: idr_pic_id");
            }
            if (sps.pic_order_cnt_type == 0)
            {
                CAVLCWriter.writeU(writer, sliceHeader.pic_order_cnt_lsb, sps.log2_max_pic_order_cnt_lsb_minus4 + 4);
                if (pps.pic_order_present_flag && !sps.field_pic_flag)
                {
                    CAVLCWriter.writeSE(writer, sliceHeader.delta_pic_order_cnt_bottom, "SH: delta_pic_order_cnt_bottom");
                }
            }
            if (sps.pic_order_cnt_type == 1 && !sps.delta_pic_order_always_zero_flag)
            {
                CAVLCWriter.writeSE(writer, sliceHeader.delta_pic_order_cnt[0], "SH: delta_pic_order_cnt");
                if (pps.pic_order_present_flag && !sps.field_pic_flag)
                {
                    CAVLCWriter.writeSE(writer, sliceHeader.delta_pic_order_cnt[1], "SH: delta_pic_order_cnt");
                }
            }
            if (pps.redundant_pic_cnt_present_flag)
            {
                CAVLCWriter.writeUE(writer, sliceHeader.redundant_pic_cnt, "SH: redundant_pic_cnt");
            }
            if (sliceHeader.slice_type == SliceType.B)
            {
                CAVLCWriter.writeBool(writer, sliceHeader.direct_spatial_mv_pred_flag, "SH: direct_spatial_mv_pred_flag");
            }
            if (sliceHeader.slice_type == SliceType.P || sliceHeader.slice_type == SliceType.SP ||
                sliceHeader.slice_type == SliceType.B)
            {
                CAVLCWriter.writeBool(writer, sliceHeader.num_ref_idx_active_override_flag, "SH: num_ref_idx_active_override_flag");
                if (sliceHeader.num_ref_idx_active_override_flag)
                {
                    CAVLCWriter.writeUE(writer, sliceHeader.num_ref_idx_active_minus1[0], "SH: num_ref_idx_l0_active_minus1");
                    if (sliceHeader.slice_type == SliceType.B)
                    {
                        CAVLCWriter.writeUE(writer, sliceHeader.num_ref_idx_active_minus1[1], "SH: num_ref_idx_l1_active_minus1");
                    }
                }
            }
            writeRefPicListReordering(sliceHeader, writer);
            if ((pps.weighted_pred_flag && (sliceHeader.slice_type == SliceType.P || sliceHeader.slice_type == SliceType.SP)) ||
                (pps.weighted_bipred_idc == 1 && sliceHeader.slice_type == SliceType.B))
            {
                writePredWeightTable(sliceHeader, writer);
            }
            if (nalRefIdc != 0)
            {
                writeDecRefPicMarking(sliceHeader, idrSlice, writer);
            }
            if (pps.entropy_coding_mode_flag && sliceHeader.slice_type.isInter())
            {
                CAVLCWriter.writeUE(writer, sliceHeader.cabac_init_idc, "SH: cabac_init_idc");
            }
            CAVLCWriter.writeSE(writer, sliceHeader.slice_qp_delta, "SH: slice_qp_delta");
            if (sliceHeader.slice_type == SliceType.SP || sliceHeader.slice_type == SliceType.SI)
            {
                if (sliceHeader.slice_type == SliceType.SP)
                {
                    CAVLCWriter.writeBool(writer, sliceHeader.sp_for_switch_flag, "SH: sp_for_switch_flag");
                }
                CAVLCWriter.writeSE(writer, sliceHeader.slice_qs_delta, "SH: slice_qs_delta");
            }
            if (pps.deblocking_filter_control_present_flag)
            {
                CAVLCWriter.writeUE(writer, sliceHeader.disable_deblocking_filter_idc, "SH: disable_deblocking_filter_idc");
                if (sliceHeader.disable_deblocking_filter_idc != 1)
                {
                    CAVLCWriter.writeSE(writer, sliceHeader.slice_alpha_c0_offset_div2, "SH: slice_alpha_c0_offset_div2");
                    CAVLCWriter.writeSE(writer, sliceHeader.slice_beta_offset_div2, "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 = (sps.pic_height_in_map_units_minus1 + 1) * (sps.pic_width_in_mbs_minus1 + 1)
                          / (pps.slice_group_change_rate_minus1 + 1);
                if (((sps.pic_height_in_map_units_minus1 + 1) * (sps.pic_width_in_mbs_minus1 + 1))
                    % (pps.slice_group_change_rate_minus1 + 1) > 0)
                {
                    len += 1;
                }

                len = CeilLog2(len + 1);
                CAVLCWriter.writeU(writer, sliceHeader.slice_group_change_cycle, len);
            }
        }
Exemplo n.º 18
0
 private bool validSps(SeqParameterSet sps)
 {
     return(sps.bit_depth_chroma_minus8 < 4 && sps.bit_depth_luma_minus8 < 4 && sps.chroma_format_idc != null &&
            sps.seq_parameter_set_id < 2 && sps.pic_order_cnt_type <= 2);
 }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
0
        public static int getPicHeightInMbs(SeqParameterSet sps)
        {
            int picHeightInMbs = (sps.pic_height_in_map_units_minus1 + 1) << (sps.frame_mbs_only_flag ? 0 : 1);

            return(picHeightInMbs);
        }
Exemplo n.º 21
0
            public SliceHeader run(System.IO.MemoryStream iss, System.IO.MemoryStream os, NALUnit nu, SeqParameterSet sps, PictureParameterSet pps)
            {
                System.IO.MemoryStream nal = os.duplicate();

                Utility.unescapeNAL(iss);

                BitReader   reader = new BitReader(iss);
                SliceHeader sh     = shr.readPart1(reader);

                return(part2(iss, os, nu, sps, pps, nal, reader, sh));
            }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
0
            private SliceHeader part2(System.IO.MemoryStream iss, System.IO.MemoryStream os, NALUnit nu, SeqParameterSet sps,
                                      PictureParameterSet pps, System.IO.MemoryStream nal, BitReader reader, SliceHeader sh)
            {
                BitWriter writer = new BitWriter(os);

                shr.readPart2(sh, nu, sps, pps, reader);

                tweak(sh);

                shw.write(sh, nu.type == NALUnitType.IDR_SLICE, nu.nal_ref_idc, writer);

                if (pps.entropy_coding_mode_flag)
                {
                    copyDataCABAC(iss, os, reader, writer);
                }
                else
                {
                    copyDataCAVLC(iss, os, reader, writer);
                }

                nal.limit(os.Position);

                Utility.escapeNAL(nal);

                os.position(nal.limit());

                return(sh);
            }