Пример #1
0
        public Mapper getMapper(SliceHeader sh)
        {
            updateMap(sh);
            int firstMBInSlice = sh.first_mb_in_slice;

            if (pps.num_slice_groups_minus1 > 0)
            {
                return(new PrebuiltMBlockMapper(mbToSliceGroupMap, firstMBInSlice, sps.pic_width_in_mbs_minus1 + 1));
            }
            else
            {
                return(new FlatMBlockMapper(sps.pic_width_in_mbs_minus1 + 1, firstMBInSlice));
            }
        }
Пример #2
0
        private void updateMap(SliceHeader sh)
        {
            int mapType   = pps.slice_group_map_type;
            int numGroups = pps.num_slice_groups_minus1 + 1;

            if (numGroups > 1 && mapType >= 3 && mapType <= 5 &&
                (sh.slice_group_change_cycle != prevSliceGroupChangeCycle || mbToSliceGroupMap == null))
            {
                prevSliceGroupChangeCycle = sh.slice_group_change_cycle;

                int picWidthInMbs         = sps.pic_width_in_mbs_minus1 + 1;
                int picHeightInMbs        = Utility.getPicHeightInMbs(sps);
                int picSizeInMapUnits     = picWidthInMbs * picHeightInMbs;
                int mapUnitsInSliceGroup0 = sh.slice_group_change_cycle * (pps.slice_group_change_rate_minus1 + 1);
                mapUnitsInSliceGroup0 = mapUnitsInSliceGroup0 > picSizeInMapUnits ? picSizeInMapUnits
                        : mapUnitsInSliceGroup0;

                int sizeOfUpperLeftGroup = (pps.slice_group_change_direction_flag ? (picSizeInMapUnits - mapUnitsInSliceGroup0)
                        : mapUnitsInSliceGroup0);

                int[] map;
                if (mapType == 3)
                {
                    map = SliceGroupMapBuilder.buildBoxOutMap(picWidthInMbs, picHeightInMbs,
                                                              pps.slice_group_change_direction_flag, mapUnitsInSliceGroup0);
                }
                else if (mapType == 4)
                {
                    map = SliceGroupMapBuilder.buildRasterScanMap(picWidthInMbs, picHeightInMbs, sizeOfUpperLeftGroup,
                                                                  pps.slice_group_change_direction_flag);
                }
                else
                {
                    map = SliceGroupMapBuilder.buildWipeMap(picWidthInMbs, picHeightInMbs, sizeOfUpperLeftGroup,
                                                            pps.slice_group_change_direction_flag);
                }

                this.mbToSliceGroupMap = buildMapIndices(map, numGroups);
            }
        }
Пример #3
0
        private void SPS_PPS(byte[] data)
        {
            bool spsStartCode = data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x00 && data[3] == 0x01 && (data[4] == 0x67 || data[4] == 0x27);

            if (spsStartCode)
            {
                this.IsKeyFrame = 1;
            }
            else
            {
                this.IsKeyFrame = 0;
            }
            if (initialized)
            {
                return;
            }
            int nal_unit_type = data[4];

            if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1)
            {
                if (nal_unit_type == 0x67 || (nal_unit_type & 0x0F) == 7)
                {
                    var sps_pps = SliceHeader.GetSPS_PPS(data);
                    if (sps_pps == null)
                    {
                        return;
                    }
                    var sps = mp4parser.h264.model.SeqParameterSet.Read(new MemoryStream(sps_pps[0], 1, sps_pps[0].Length - 1));
                    var pps = mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(sps_pps[1], 1, sps_pps[1].Length - 1));
                    Width    = (ushort)((sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset);
                    Height   = (ushort)((sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset);
                    this.SPS = sps_pps[0];
                    this.PPS = sps_pps[1];

                    initialized = true;
                }
            }
        }
Пример #4
0
            private MediaFrame CreateVideoMediaFrame(byte[] data, long tick)
            {
                int keyFrameFlagOffset = 0;

                if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1 && data[4] == 0x09)
                {
                    keyFrameFlagOffset = 6;

                    var tmp = new byte[data.Length - 6];
                    Array.Copy(data, 6, tmp, 0, tmp.Length - 6);
                    data = tmp;
                    keyFrameFlagOffset = 0;
                }

                if (data[keyFrameFlagOffset + 4] == 0x67 || data[keyFrameFlagOffset + 4] == 0x27)
                {
                    if (this.SPS == null)
                    {
                        var tdata = data;
                        if (keyFrameFlagOffset > 0)
                        {
                            tdata = new byte[data.Length - keyFrameFlagOffset];
                            Array.Copy(data, keyFrameFlagOffset, tdata, 0, tdata.Length);
                        }
                        var sps_pps = SliceHeader.GetSPS_PPS(tdata);
                        var sps     = com.googlecode.mp4parser.h264.model.SeqParameterSet.read(new MemoryStream(sps_pps[0], 1, sps_pps[0].Length - 1));
                        var pps     = com.googlecode.mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(sps_pps[1], 1, sps_pps[1].Length - 1));
                        Width    = (sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset;
                        Height   = (sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset;
                        this.SPS = sps_pps[0];
                        this.PPS = sps_pps[1];
                    }
                    var mf = new MediaFrame()
                    {
                        nIsAudio    = 0,
                        nIsKeyFrame = 1,
                        Data        = data,
                        nSize       = data.Length,
                        nWidth      = Width,
                        nHeight     = Height,
                        nSPSLen     = (short)this.SPS.Length,
                        nPPSLen     = (short)this.PPS.Length,
                        nTimetick   = tick,
                        nOffset     = 0,
                        nEncoder    = MediaFrame.H264Encoder,
                        nEx         = 1,
                    };
                    mf.MediaFrameVersion = (byte)(mf.nIsKeyFrame == 1 ? 1 : 0);
                    VideoFrameCount++;
                    return(mf);
                }
                else
                {
                    if (this.SPS != null)
                    {
                        var mf = new MediaFrame()
                        {
                            nIsAudio    = 0,
                            nIsKeyFrame = 0,
                            Data        = data,
                            nSize       = data.Length,
                            nWidth      = Width,
                            nHeight     = Height,
                            nSPSLen     = (short)this.SPS.Length,
                            nPPSLen     = (short)this.PPS.Length,
                            nTimetick   = tick,
                            nOffset     = 0,
                            nEncoder    = MediaFrame.H264Encoder,
                            nEx         = 1,
                        };
                        mf.MediaFrameVersion = (byte)(mf.nIsKeyFrame == 1 ? 1 : 0);
                        VideoFrameCount++;
                        return(mf);
                    }
                }
                return(null);
            }
Пример #5
0
        private MediaFrame CreateVideoMediaFrame(byte[] data, long tick)
        {
            int keyFrameFlagOffset = 0;

            if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1 && data[4] == 0x09)
            {
                keyFrameFlagOffset = 6;
            }
            if (data[keyFrameFlagOffset + 4] == 0x67 || data[keyFrameFlagOffset + 4] == 0x68 | data[keyFrameFlagOffset + 4] == 0x27)
            {//sps ,这里sps及pps及载荷会被放置到不同的pes包里
                try
                {
                    #region
                    if (this.SPS == null || this.PPS == null)
                    {
                        var tdata = data;
                        if (keyFrameFlagOffset > 0)
                        {
                            tdata = new byte[data.Length - keyFrameFlagOffset];
                            Array.Copy(data, keyFrameFlagOffset, tdata, 0, tdata.Length);
                        }
                        var sps_pps = SliceHeader.GetSPS_PPS(tdata);
                        var sps     = mp4parser.h264.model.SeqParameterSet.Read(new MemoryStream(sps_pps[0], 1, sps_pps[0].Length - 1));
                        var pps     = mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(sps_pps[1], 1, sps_pps[1].Length - 1));
                        Width    = (sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset;
                        Height   = (sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset;
                        this.SPS = sps_pps[0];
                        this.PPS = sps_pps[1];
                    }
                    #endregion
                }
                catch
                {
                    if (this.SPS == null && data[keyFrameFlagOffset + 4] == 0x67)
                    {
                        SPS = data.Skip(4).ToArray();
                    }
                    if (this.PPS == null && data[keyFrameFlagOffset + 4] == 0x68)
                    {
                        PPS = data.Skip(4).ToArray();
                    }
                    if (this.SPS != null && this.PPS != null)
                    {
                        var sps = mp4parser.h264.model.SeqParameterSet.Read(new MemoryStream(this.SPS, 1, this.SPS.Length - 1));
                        // var pps = mp4parser.h264.model.PictureParameterSet.read(new MemoryStream(this.PPS, 1, this.PPS.Length - 1));
                        Width  = (sps.pic_width_in_mbs_minus1 + 1) * 16 - 2 * sps.frame_crop_left_offset - 2 * sps.frame_crop_right_offset;
                        Height = (sps.pic_height_in_map_units_minus1 + 1) * 16 - 2 * sps.frame_crop_top_offset - 2 * sps.frame_crop_bottom_offset;
                    }
                    return(null);
                }
                if (data[keyFrameFlagOffset + 4] == 0x67 && this.SPS != null && data.Length == this.SPS.Length + 4)
                {
                    return(null);
                }
                if (data[keyFrameFlagOffset + 4] == 0x68 && this.PPS != null && data.Length == this.PPS.Length + 4)
                {
                    return(null);
                }
            }
            if (this.SPS != null && this.PPS != null)
            {
                if (data[keyFrameFlagOffset + 4] == 0x65)
                {
                    var h264 = new MemoryStream();
                    h264.Write(new byte[] { 0, 0, 0, 1 }, 0, 4);
                    h264.Write(this.SPS, 0, this.SPS.Length);
                    h264.Write(new byte[] { 0, 0, 0, 1 }, 0, 4);
                    h264.Write(this.PPS, 0, this.PPS.Length);
                    // h264.Write(new byte[] { 0, 0, 0, 1 }, 0, 4);
                    h264.Write(data, 0, data.Length);

                    var mf = new MediaFrame()
                    {
                        IsAudio    = 0,
                        IsKeyFrame = 1,
                        Size       = (int)h264.Length,
                        Width      = Width,
                        Height     = Height,
                        SPSLen     = SPS.Length,
                        PPSLen     = PPS.Length,
                        NTimetick  = tick,
                        Offset     = 0,
                        Encoder    = MediaFrame.H264Encoder,
                        Ex         = 1,
                    };
                    mf.SetData(h264.ToArray());
                    mf.MediaFrameVersion = (byte)(mf.IsKeyFrame == 1 ? 1 : 0);
                    VideoFrameCount++;
                    return(mf);
                }
                else
                {
                    if (this.SPS != null)
                    {
                        var mf = new MediaFrame()
                        {
                            IsAudio    = 0,
                            IsKeyFrame = 0,
                            Size       = data.Length,
                            Width      = Width,
                            Height     = Height,
                            SPSLen     = (short)this.SPS.Length,
                            PPSLen     = (short)this.PPS.Length,
                            NTimetick  = tick,
                            Offset     = 0,
                            Encoder    = MediaFrame.H264Encoder,
                            Ex         = 1,
                        };
                        mf.SetData(data);
                        mf.MediaFrameVersion = (byte)(mf.IsKeyFrame == 1 ? 1 : 0);
                        VideoFrameCount++;
                        return(mf);
                    }
                }
            }
            return(null);
        }