Beispiel #1
0
        public void ParseNALUTest2()
        {
            JT1078Package Package         = null;
            var           lines           = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_1.txt"));
            int           mergeBodyLength = 0;

            foreach (var line in lines)
            {
                var           data    = line.Split(',');
                var           bytes   = data[6].ToHexBytes();
                JT1078Package package = JT1078Serializer.Deserialize(bytes);
                mergeBodyLength += package.DataBodyLength;
                Package          = JT1078Serializer.Merge(package);
            }
            H264Decoder decoder = new H264Decoder();
            var         nalus   = decoder.ParseNALU(Package);

            Assert.Equal(4, nalus.Count);

            //SPS -> 7
            var spsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 7);

            Assert.NotNull(spsNALU);
            spsNALU.RawData = decoder.DiscardEmulationPreventionBytes(spsNALU.RawData);
            //"Z00AFJWoWCWQ"
            var             spsRawDataHex    = JsonConvert.SerializeObject(spsNALU.RawData);
            ExpGolombReader h264GolombReader = new ExpGolombReader(spsNALU.RawData);
            //(77, 20, 0, 352, 288)
            var spsInfo = h264GolombReader.ReadSPS();

            Assert.Equal(77, spsInfo.profileIdc);
            Assert.Equal(20, spsInfo.levelIdc);
            Assert.Equal(0u, spsInfo.profileCompat);
            Assert.Equal(288, spsInfo.height);
            Assert.Equal(352, spsInfo.width);

            //PPS -> 8
            var ppsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 8);

            Assert.NotNull(ppsNALU);

            //IDR -> 5  关键帧
            var idrNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 5);

            Assert.NotNull(idrNALU);

            //SEI -> 6
            var seiNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 6);

            Assert.NotNull(seiNALU);
        }
        public void Setup()
        {
            var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JT1078_1.txt"));

            foreach (var line in lines)
            {
                var           data    = line.Split(',');
                var           bytes   = data[6].ToHexBytes();
                JT1078Package package = JT1078Serializer.Deserialize(bytes);
                Package = JT1078Serializer.Merge(package);
            }
            H264NALUs       = h264Decoder.ParseNALU(Package);
            SPSNALu         = H264NALUs.FirstOrDefault(f => f.NALUHeader.NalUnitType == 7);
            SPSNALu.RawData = h264Decoder.DiscardEmulationPreventionBytes(SPSNALu.RawData);
        }
Beispiel #3
0
 /// <summary>
 /// 编码首帧视频,即videoTag[0]
 /// <para>
 /// 注意:本方法已写入<see cref="previousTagSize"/>
 /// </para>
 /// </summary>
 /// <param name="sps"></param>
 /// <param name="pps"></param>
 /// <param name="sei"></param>
 /// <returns></returns>
 public byte[] EncoderFirstVideoTag(H264NALU sps, H264NALU pps, H264NALU sei)
 {
     byte[] buffer = FlvArrayPool.Rent(2048);
     try
     {
         FlvMessagePackWriter flvMessagePackWriter = new FlvMessagePackWriter(buffer);
         var             rawData          = h264Decoder.DiscardEmulationPreventionBytes(sps.RawData);
         ExpGolombReader h264GolombReader = new ExpGolombReader(rawData);
         SPSInfo         spsInfo          = h264GolombReader.ReadSPS();
         //flv body video tag
         //flv body tag header
         FlvTags flvTags = new FlvTags
         {
             Type         = TagType.Video,
             Timestamp    = (uint)sps.Timestamp,
             TimestampExt = 0,
             StreamId     = 0,
             //flv body tag body
             VideoTagsData = new VideoTags()
         };
         flvTags.VideoTagsData.FrameType = FrameType.KeyFrame;
         flvTags.VideoTagsData.VideoData = new AvcVideoPacke
         {
             AvcPacketType   = AvcPacketType.SequenceHeader,
             CompositionTime = 0
         };
         AVCDecoderConfigurationRecord aVCDecoderConfigurationRecord = new AVCDecoderConfigurationRecord
         {
             AVCProfileIndication      = spsInfo.profileIdc,
             ProfileCompatibility      = (byte)spsInfo.profileCompat,
             AVCLevelIndication        = spsInfo.levelIdc,
             NumOfPictureParameterSets = 1,
             PPSBuffer = pps.RawData,
             SPSBuffer = sps.RawData
         };
         flvTags.VideoTagsData.VideoData.AVCDecoderConfiguration = aVCDecoderConfigurationRecord;
         flvMessagePackWriter.WriteUInt32(previousTagSize);
         flvMessagePackWriter.WriteFlvTag(flvTags);
         var data = flvMessagePackWriter.FlushAndGetArray();
         previousTagSize = (uint)(flvTags.DataSize + 11);
         return(data);
     }
     finally
     {
         FlvArrayPool.Return(buffer);
     }
 }
Beispiel #4
0
        public void Test3()
        {
            H264Decoder decoder  = new H264Decoder();
            var         packages = ParseNALUTests();
            //10M
            FMp4MessagePackWriter writer = new FMp4MessagePackWriter(new byte[10 * 1024 * 1024]);
            var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.mp4");

            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            using var fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            var jT1078Package = packages.FirstOrDefault();
            var nalus         = decoder.ParseNALU(jT1078Package);
            var spsNALU       = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 7);

            //SPS
            spsNALU.RawData = decoder.DiscardEmulationPreventionBytes(spsNALU.RawData);
            var ppsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 8);

            ppsNALU.RawData = decoder.DiscardEmulationPreventionBytes(ppsNALU.RawData);
            ExpGolombReader h264GolombReader = new ExpGolombReader(spsNALU.RawData);
            var             spsInfo          = h264GolombReader.ReadSPS();
            //ftyp
            FileTypeBox fileTypeBox = new FileTypeBox();

            fileTypeBox.MajorBrand   = "isom";
            fileTypeBox.MinorVersion = "\0\0\u0002\0";
            fileTypeBox.CompatibleBrands.Add("isom");
            fileTypeBox.CompatibleBrands.Add("iso6");
            fileTypeBox.CompatibleBrands.Add("iso2");
            fileTypeBox.CompatibleBrands.Add("avc1");
            fileTypeBox.CompatibleBrands.Add("mp41");
            //moov
            MovieBox movieBox = new MovieBox();

            movieBox.MovieHeaderBox = new MovieHeaderBox(0, 0);
            movieBox.MovieHeaderBox.CreationTime     = 0;
            movieBox.MovieHeaderBox.ModificationTime = 0;
            movieBox.MovieHeaderBox.Duration         = 0;
            movieBox.MovieHeaderBox.Timescale        = 1000;
            movieBox.MovieHeaderBox.NextTrackID      = 2;
            movieBox.TrackBox = new TrackBox();
            movieBox.TrackBox.TrackHeaderBox = new TrackHeaderBox(0, 3);
            movieBox.TrackBox.TrackHeaderBox.CreationTime     = 0;
            movieBox.TrackBox.TrackHeaderBox.ModificationTime = 0;
            movieBox.TrackBox.TrackHeaderBox.TrackID          = 1;
            movieBox.TrackBox.TrackHeaderBox.Duration         = 0;
            movieBox.TrackBox.TrackHeaderBox.TrackIsAudio     = false;
            movieBox.TrackBox.TrackHeaderBox.Width            = (uint)spsInfo.width;
            movieBox.TrackBox.TrackHeaderBox.Height           = (uint)spsInfo.height;
            movieBox.TrackBox.MediaBox = new MediaBox();
            movieBox.TrackBox.MediaBox.MediaHeaderBox = new MediaHeaderBox();
            movieBox.TrackBox.MediaBox.MediaHeaderBox.CreationTime     = 0;
            movieBox.TrackBox.MediaBox.MediaHeaderBox.ModificationTime = 0;
            movieBox.TrackBox.MediaBox.MediaHeaderBox.Timescale        = 1200000;
            movieBox.TrackBox.MediaBox.MediaHeaderBox.Duration         = 0;
            movieBox.TrackBox.MediaBox.HandlerBox             = new HandlerBox();
            movieBox.TrackBox.MediaBox.HandlerBox.HandlerType = HandlerType.vide;
            movieBox.TrackBox.MediaBox.HandlerBox.Name        = "VideoHandler";
            movieBox.TrackBox.MediaBox.MediaInformationBox    = new MediaInformationBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.VideoMediaHeaderBox = new VideoMediaHeaderBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox  = new DataInformationBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox = new DataReferenceBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes = new List <DataEntryBox>();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes.Add(new DataEntryUrlBox(1));
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox = new SampleTableBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox = new SampleDescriptionBox(movieBox.TrackBox.MediaBox.HandlerBox.HandlerType);
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries = new List <SampleEntry>();
            AVC1SampleEntry avc1 = new AVC1SampleEntry();

            avc1.AVCConfigurationBox = new AVCConfigurationBox();
            //h264
            avc1.Width  = (ushort)movieBox.TrackBox.TrackHeaderBox.Width;
            avc1.Height = (ushort)movieBox.TrackBox.TrackHeaderBox.Height;
            avc1.AVCConfigurationBox.AVCLevelIndication   = spsInfo.levelIdc;
            avc1.AVCConfigurationBox.AVCProfileIndication = spsInfo.profileIdc;
            avc1.AVCConfigurationBox.ProfileCompatibility = (byte)spsInfo.profileCompat;
            avc1.AVCConfigurationBox.PPSs = new List <byte[]>()
            {
                ppsNALU.RawData
            };
            avc1.AVCConfigurationBox.SPSs = new List <byte[]>()
            {
                spsNALU.RawData
            };
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries.Add(avc1);
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox  = new TimeToSampleBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleToChunkBox = new SampleToChunkBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox    = new SampleSizeBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.ChunkOffsetBox   = new ChunkOffsetBox();
            movieBox.MovieExtendsBox = new MovieExtendsBox();
            movieBox.MovieExtendsBox.TrackExtendsBoxs = new List <TrackExtendsBox>();
            TrackExtendsBox trex = new TrackExtendsBox();

            trex.TrackID = 1;
            trex.DefaultSampleDescriptionIndex = 1;
            trex.DefaultSampleDuration         = 0;
            trex.DefaultSampleSize             = 0;
            trex.DefaultSampleFlags            = 0;
            movieBox.MovieExtendsBox.TrackExtendsBoxs.Add(trex);
            fileTypeBox.ToBuffer(ref writer);
            movieBox.ToBuffer(ref writer);
            //fragment moof n
            foreach (var package in packages)
            {
                ulong       moofOffset    = (ulong)writer.GetCurrentPosition();
                var         package_nalus = decoder.ParseNALU(package);
                FragmentBox fragmentBox   = new FragmentBox();
                fragmentBox.MovieFragmentBox = new MovieFragmentBox();
                fragmentBox.MovieFragmentBox.MovieFragmentHeaderBox = new MovieFragmentHeaderBox();
                fragmentBox.MovieFragmentBox.MovieFragmentHeaderBox.SequenceNumber = package.SN;
                fragmentBox.MovieFragmentBox.TrackFragmentBox = new TrackFragmentBox();
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox                                  = new TrackFragmentHeaderBox(0x39);
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.TrackID                          = 1;
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.BaseDataOffset                   = moofOffset;
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleDuration            = 48000;
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleSize                = (uint)package.Bodies.Length;
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleFlags               = 0x1010000;
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox                     = new TrackFragmentBaseMediaDecodeTimeBox();
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox.BaseMediaDecodeTime = package.Timestamp * 1000;
                //trun
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox = new TrackRunBox(flags: 0x5);
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.FirstSampleFlags = 0;
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos    = new List <TrackRunBox.TrackRunInfo>();
                fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo());
                fragmentBox.MediaDataBox      = new MediaDataBox();
                fragmentBox.MediaDataBox.Data = package_nalus.Select(s => s.RawData).ToList();
                fragmentBox.ToBuffer(ref writer);
            }
            var buffer = writer.FlushAndGetArray();

            fileStream.Write(buffer);
            fileStream.Close();
        }
Beispiel #5
0
        public void Test1()
        {
            var         jT1078Package = ParseNALUTest();
            H264Decoder decoder       = new H264Decoder();
            var         nalus         = decoder.ParseNALU(jT1078Package);
            var         spsNALU       = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 7);

            //SPS
            spsNALU.RawData = decoder.DiscardEmulationPreventionBytes(spsNALU.RawData);
            var ppsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 8);

            ppsNALU.RawData = decoder.DiscardEmulationPreventionBytes(ppsNALU.RawData);
            //ftyp
            FileTypeBox fileTypeBox = new FileTypeBox();

            fileTypeBox.MajorBrand   = "isom";
            fileTypeBox.MinorVersion = "\0\0\u0002\0";
            fileTypeBox.CompatibleBrands.Add("isom");
            fileTypeBox.CompatibleBrands.Add("iso6");
            fileTypeBox.CompatibleBrands.Add("iso2");
            fileTypeBox.CompatibleBrands.Add("avc1");
            fileTypeBox.CompatibleBrands.Add("mp41");
            //moov
            MovieBox movieBox = new MovieBox();

            movieBox.MovieHeaderBox = new MovieHeaderBox(0, 0);
            movieBox.MovieHeaderBox.CreationTime     = 0;
            movieBox.MovieHeaderBox.ModificationTime = 0;
            movieBox.MovieHeaderBox.Duration         = 0;
            movieBox.MovieHeaderBox.Timescale        = 1000;
            movieBox.MovieHeaderBox.NextTrackID      = 2;
            movieBox.TrackBox = new TrackBox();
            movieBox.TrackBox.TrackHeaderBox = new TrackHeaderBox(0, 3);
            movieBox.TrackBox.TrackHeaderBox.CreationTime     = 0;
            movieBox.TrackBox.TrackHeaderBox.ModificationTime = 0;
            movieBox.TrackBox.TrackHeaderBox.TrackID          = 1;
            movieBox.TrackBox.TrackHeaderBox.Duration         = 0;
            movieBox.TrackBox.TrackHeaderBox.TrackIsAudio     = false;
            movieBox.TrackBox.TrackHeaderBox.Width            = 352;
            movieBox.TrackBox.TrackHeaderBox.Height           = 288;
            movieBox.TrackBox.MediaBox = new MediaBox();
            movieBox.TrackBox.MediaBox.MediaHeaderBox = new MediaHeaderBox();
            movieBox.TrackBox.MediaBox.MediaHeaderBox.CreationTime     = 0;
            movieBox.TrackBox.MediaBox.MediaHeaderBox.ModificationTime = 0;
            movieBox.TrackBox.MediaBox.MediaHeaderBox.Timescale        = 1200000;
            movieBox.TrackBox.MediaBox.MediaHeaderBox.Duration         = 0;
            movieBox.TrackBox.MediaBox.HandlerBox             = new HandlerBox();
            movieBox.TrackBox.MediaBox.HandlerBox.HandlerType = HandlerType.vide;
            movieBox.TrackBox.MediaBox.HandlerBox.Name        = "VideoHandler";
            movieBox.TrackBox.MediaBox.MediaInformationBox    = new MediaInformationBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.VideoMediaHeaderBox = new VideoMediaHeaderBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox  = new DataInformationBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox = new DataReferenceBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes = new List <DataEntryBox>();
            movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes.Add(new DataEntryUrlBox(1));
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox = new SampleTableBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox = new SampleDescriptionBox(movieBox.TrackBox.MediaBox.HandlerBox.HandlerType);
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries = new List <SampleEntry>();
            AVC1SampleEntry avc1 = new AVC1SampleEntry();

            avc1.AVCConfigurationBox = new AVCConfigurationBox();
            //h264
            avc1.Width  = (ushort)movieBox.TrackBox.TrackHeaderBox.Width;
            avc1.Height = (ushort)movieBox.TrackBox.TrackHeaderBox.Height;
            //MSE codecs avc1.4D0014
            //4D 00 14
            //AVCProfileIndication profile_compability AVCLevelIndication
            avc1.AVCConfigurationBox.AVCLevelIndication   = 20;
            avc1.AVCConfigurationBox.AVCProfileIndication = 77;
            avc1.AVCConfigurationBox.PPSs = new List <byte[]>()
            {
                ppsNALU.RawData
            };
            avc1.AVCConfigurationBox.SPSs = new List <byte[]>()
            {
                spsNALU.RawData
            };
            avc1.AVCConfigurationBox.ProfileCompatibility = 0;
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries.Add(avc1);
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox  = new TimeToSampleBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleToChunkBox = new SampleToChunkBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox    = new SampleSizeBox();
            movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.ChunkOffsetBox   = new ChunkOffsetBox();
            movieBox.MovieExtendsBox = new MovieExtendsBox();
            movieBox.MovieExtendsBox.TrackExtendsBoxs = new List <TrackExtendsBox>();
            TrackExtendsBox trex = new TrackExtendsBox();

            trex.TrackID = 1;
            trex.DefaultSampleDescriptionIndex = 1;
            trex.DefaultSampleDuration         = 0;
            trex.DefaultSampleSize             = 0;
            trex.DefaultSampleFlags            = 0;
            movieBox.MovieExtendsBox.TrackExtendsBoxs.Add(trex);
            //用户自定义可以不用
            //movieBox.UserDataBox = new UserDataBox();
            //movieBox.UserDataBox.Data = "0000005a6d657461000000000000002168646c7200000000000000006d6469726170706c0000000000000000000000002d696c737400000025a9746f6f0000001d6461746100000001000000004c61766635382e34352e313030".ToHexBytes();
            //fragment moof n
            List <FragmentBox> moofs       = new List <FragmentBox>();
            FragmentBox        fragmentBox = new FragmentBox();

            fragmentBox.MovieFragmentBox = new MovieFragmentBox();
            fragmentBox.MovieFragmentBox.MovieFragmentHeaderBox = new MovieFragmentHeaderBox();
            fragmentBox.MovieFragmentBox.MovieFragmentHeaderBox.SequenceNumber = 1;
            fragmentBox.MovieFragmentBox.TrackFragmentBox = new TrackFragmentBox();
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox         = new TrackFragmentHeaderBox(0x39);
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.TrackID = 1;
            //todo:BaseDataOffset
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.BaseDataOffset        = 0x000000000000028b;
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleDuration = 48000;
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleSize     = (uint)jT1078Package.Bodies.Length;
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleFlags    = 0x1010000;
            //fragmentBox.MovieFragmentBox.TrackFragmentBox.SampleDependencyTypeBox = new SampleDependencyTypeBox();
            //fragmentBox.MovieFragmentBox.TrackFragmentBox.SampleDependencyTypeBox.SampleDependencyTypes = new List<SampleDependencyTypeBox.SampleDependencyType>();
            //todo:fragmentBox.MovieFragmentBox.TrackFragmentBox.SampleDependencyTypeBox.SampleDependencyTypes
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox = new TrackFragmentBaseMediaDecodeTimeBox();
            //todo:BaseMediaDecodeTime
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox.BaseMediaDecodeTime = 0;
            //trun
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox                  = new TrackRunBox(flags: 0x5);
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.DataOffset       = 120;
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.FirstSampleFlags = 0;
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos    = new List <TrackRunBox.TrackRunInfo>();
            fragmentBox.MovieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo());
            fragmentBox.MediaDataBox      = new MediaDataBox();
            fragmentBox.MediaDataBox.Data = nalus
                                            .Select(s => s.RawData)
                                            .ToList();
            moofs.Add(fragmentBox);
            //mfra
            MovieFragmentRandomAccessBox movieFragmentRandomAccessBox = new MovieFragmentRandomAccessBox();

            //mfra->tfra
            movieFragmentRandomAccessBox.TrackFragmentRandomAccessBox         = new TrackFragmentRandomAccessBox(1);
            movieFragmentRandomAccessBox.TrackFragmentRandomAccessBox.TrackID = 0x01;
            movieFragmentRandomAccessBox.TrackFragmentRandomAccessBox.TrackFragmentRandomAccessInfos = new List <TrackFragmentRandomAccessBox.TrackFragmentRandomAccessInfo>();
            TrackFragmentRandomAccessBox.TrackFragmentRandomAccessInfo trackFragmentRandomAccessInfo1 = new TrackFragmentRandomAccessBox.TrackFragmentRandomAccessInfo();
            trackFragmentRandomAccessInfo1.Time = 0;
            //todo:MoofOffset
            trackFragmentRandomAccessInfo1.MoofOffset   = 0x000000000000028b;
            trackFragmentRandomAccessInfo1.TrafNumber   = 0x01;
            trackFragmentRandomAccessInfo1.TrunNumber   = 0x01;
            trackFragmentRandomAccessInfo1.SampleNumber = 0x01;
            movieFragmentRandomAccessBox.TrackFragmentRandomAccessBox.TrackFragmentRandomAccessInfos.Add(trackFragmentRandomAccessInfo1);
            //mfra->mfro
            movieFragmentRandomAccessBox.MovieFragmentRandomAccessOffsetBox = new MovieFragmentRandomAccessOffsetBox(0);
            //todo:MfraSize
            movieFragmentRandomAccessBox.MovieFragmentRandomAccessOffsetBox.MfraSize = 0x00000043;
            FMp4Box fMp4Box = new FMp4Box();

            fMp4Box.FileTypeBox  = fileTypeBox;
            fMp4Box.MovieBox     = movieBox;
            fMp4Box.FragmentBoxs = moofs;
            fMp4Box.MovieFragmentRandomAccessBox = movieFragmentRandomAccessBox;
            FMp4MessagePackWriter writer = new FMp4MessagePackWriter(new byte[65535]);

            fMp4Box.ToBuffer(ref writer);
            var buffer   = writer.FlushAndGetArray();
            var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_1.mp4");

            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            using var fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            fileStream.Write(buffer);
            fileStream.Close();
        }
Beispiel #6
0
        /// <summary>
        /// 编码首个视频盒子
        /// </summary>
        /// <param name="package">jt1078完整包</param>
        /// <returns></returns>
        public byte[] EncoderFirstVideoBox(JT1078Package package)
        {
            byte[] buffer = FMp4ArrayPool.Rent(package.Bodies.Length + 4096);
            FMp4MessagePackWriter writer = new FMp4MessagePackWriter(new byte[10 * 1024 * 1024]);

            try
            {
                var nalus   = h264Decoder.ParseNALU(package);
                var spsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 7);
                //SPS
                spsNALU.RawData = h264Decoder.DiscardEmulationPreventionBytes(spsNALU.RawData);
                var ppsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 8);
                ppsNALU.RawData = h264Decoder.DiscardEmulationPreventionBytes(ppsNALU.RawData);
                ExpGolombReader h264GolombReader = new ExpGolombReader(spsNALU.RawData);
                var             spsInfo          = h264GolombReader.ReadSPS();
                //ftyp
                FileTypeBox fileTypeBox = new FileTypeBox();
                fileTypeBox.MajorBrand   = "isom";
                fileTypeBox.MinorVersion = "\0\0\u0002\0";
                fileTypeBox.CompatibleBrands.Add("isom");
                fileTypeBox.CompatibleBrands.Add("iso6");
                fileTypeBox.CompatibleBrands.Add("iso2");
                fileTypeBox.CompatibleBrands.Add("avc1");
                fileTypeBox.CompatibleBrands.Add("mp41");
                //moov
                MovieBox movieBox = new MovieBox();
                movieBox.MovieHeaderBox = new MovieHeaderBox(0, 0);
                movieBox.MovieHeaderBox.CreationTime     = 0;
                movieBox.MovieHeaderBox.ModificationTime = 0;
                movieBox.MovieHeaderBox.Duration         = 0;
                movieBox.MovieHeaderBox.Timescale        = 1000;
                movieBox.MovieHeaderBox.NextTrackID      = 2;
                movieBox.TrackBox = new TrackBox();
                movieBox.TrackBox.TrackHeaderBox = new TrackHeaderBox(0, 3);
                movieBox.TrackBox.TrackHeaderBox.CreationTime     = 0;
                movieBox.TrackBox.TrackHeaderBox.ModificationTime = 0;
                movieBox.TrackBox.TrackHeaderBox.TrackID          = 1;
                movieBox.TrackBox.TrackHeaderBox.Duration         = 0;
                movieBox.TrackBox.TrackHeaderBox.TrackIsAudio     = false;
                movieBox.TrackBox.TrackHeaderBox.Width            = (uint)spsInfo.width;
                movieBox.TrackBox.TrackHeaderBox.Height           = (uint)spsInfo.height;
                movieBox.TrackBox.MediaBox = new MediaBox();
                movieBox.TrackBox.MediaBox.MediaHeaderBox = new MediaHeaderBox();
                movieBox.TrackBox.MediaBox.MediaHeaderBox.CreationTime     = 0;
                movieBox.TrackBox.MediaBox.MediaHeaderBox.ModificationTime = 0;
                movieBox.TrackBox.MediaBox.MediaHeaderBox.Timescale        = 1200000;
                movieBox.TrackBox.MediaBox.MediaHeaderBox.Duration         = 0;
                movieBox.TrackBox.MediaBox.HandlerBox             = new HandlerBox();
                movieBox.TrackBox.MediaBox.HandlerBox.HandlerType = HandlerType.vide;
                movieBox.TrackBox.MediaBox.HandlerBox.Name        = "VideoHandler";
                movieBox.TrackBox.MediaBox.MediaInformationBox    = new MediaInformationBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.VideoMediaHeaderBox = new VideoMediaHeaderBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox  = new DataInformationBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox = new DataReferenceBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes = new List <DataEntryBox>();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes.Add(new DataEntryUrlBox(1));
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox = new SampleTableBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox = new SampleDescriptionBox(movieBox.TrackBox.MediaBox.HandlerBox.HandlerType);
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries = new List <SampleEntry>();
                AVC1SampleEntry avc1 = new AVC1SampleEntry();
                avc1.AVCConfigurationBox = new AVCConfigurationBox();
                //h264
                avc1.Width  = (ushort)movieBox.TrackBox.TrackHeaderBox.Width;
                avc1.Height = (ushort)movieBox.TrackBox.TrackHeaderBox.Height;
                avc1.AVCConfigurationBox.AVCLevelIndication   = spsInfo.levelIdc;
                avc1.AVCConfigurationBox.AVCProfileIndication = spsInfo.profileIdc;
                avc1.AVCConfigurationBox.ProfileCompatibility = (byte)spsInfo.profileCompat;
                avc1.AVCConfigurationBox.PPSs = new List <byte[]>()
                {
                    ppsNALU.RawData
                };
                avc1.AVCConfigurationBox.SPSs = new List <byte[]>()
                {
                    spsNALU.RawData
                };
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries.Add(avc1);
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox  = new TimeToSampleBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleToChunkBox = new SampleToChunkBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox    = new SampleSizeBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.ChunkOffsetBox   = new ChunkOffsetBox();
                movieBox.MovieExtendsBox = new MovieExtendsBox();
                movieBox.MovieExtendsBox.TrackExtendsBoxs = new List <TrackExtendsBox>();
                TrackExtendsBox trex = new TrackExtendsBox();
                trex.TrackID = 1;
                trex.DefaultSampleDescriptionIndex = 1;
                trex.DefaultSampleDuration         = 0;
                trex.DefaultSampleSize             = 0;
                trex.DefaultSampleFlags            = 0;
                movieBox.MovieExtendsBox.TrackExtendsBoxs.Add(trex);
                fileTypeBox.ToBuffer(ref writer);
                movieBox.ToBuffer(ref writer);
                var data = writer.FlushAndGetArray();
                return(data);
            }
            finally
            {
                FMp4ArrayPool.Return(buffer);
            }
        }
Beispiel #7
0
        public void Setup()
        {
            var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JT1078_1.txt"));

            foreach (var line in lines)
            {
                var           data    = line.Split(',');
                var           bytes   = data[6].ToHexBytes();
                JT1078Package package = JT1078Serializer.Deserialize(bytes);
                Package = JT1078Serializer.Merge(package);
            }
            H264NALUs       = h264Decoder.ParseNALU(Package);
            SPSNALu         = H264NALUs.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS);
            SPSNALu.RawData = h264Decoder.DiscardEmulationPreventionBytes(SPSNALu.RawData);

            List <JT1078Package> packages = new List <JT1078Package>();
            var lines3          = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_3.txt"));
            int mergeBodyLength = 0;

            foreach (var line in lines3)
            {
                var           data    = line.Split(',');
                var           bytes   = data[6].ToHexBytes();
                JT1078Package package = JT1078Serializer.Deserialize(bytes);
                mergeBodyLength += package.DataBodyLength;
                var packageMerge = JT1078Serializer.Merge(package);
                if (packageMerge != null)
                {
                    packages.Add(packageMerge);
                }
            }
            List <H264NALU> nalus       = new List <H264NALU>();
            bool            segmentFlag = false;

            foreach (var package in packages)
            {
                if (segmentFlag)
                {
                    break;
                }
                List <H264NALU> h264NALUs = h264Decoder.ParseNALU(package);
                foreach (var nalu in h264NALUs)
                {
                    if (nalu.Slice)
                    {
                        //H264 NALU slice first_mb_in_slice
                        nalus.Add(nalu);
                    }
                    else
                    {
                        if (nalus.Count > 0)
                        {
                            FMp4H264NALUs = new List <H264NALU>(nalus);
                            segmentFlag   = true;
                            nalus.Clear();
                        }
                        nalus.Add(nalu);
                    }
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// 编码moov盒子
        /// </summary>
        /// <returns></returns>
        public byte[] EncoderMoovBox(List <H264NALU> nalus, int naluLength)
        {
            byte[] buffer = FMp4ArrayPool.Rent(naluLength + 4096);
            FMp4MessagePackWriter writer = new FMp4MessagePackWriter(buffer);

            try
            {
                var spsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 7);
                //SPS
                spsNALU.RawData = h264Decoder.DiscardEmulationPreventionBytes(spsNALU.RawData);
                var ppsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 8);
                ppsNALU.RawData = h264Decoder.DiscardEmulationPreventionBytes(ppsNALU.RawData);
                ExpGolombReader h264GolombReader = new ExpGolombReader(spsNALU.RawData);
                var             spsInfo          = h264GolombReader.ReadSPS();
                //moov
                MovieBox movieBox = new MovieBox();
                movieBox.MovieHeaderBox = new MovieHeaderBox(0, 2);
                movieBox.MovieHeaderBox.CreationTime     = 0;
                movieBox.MovieHeaderBox.ModificationTime = 0;
                movieBox.MovieHeaderBox.Duration         = 0;
                movieBox.MovieHeaderBox.Timescale        = 1000;
                movieBox.MovieHeaderBox.NextTrackID      = 99;
                movieBox.TrackBox = new TrackBox();
                movieBox.TrackBox.TrackHeaderBox = new TrackHeaderBox(0, 3);
                movieBox.TrackBox.TrackHeaderBox.CreationTime     = 0;
                movieBox.TrackBox.TrackHeaderBox.ModificationTime = 0;
                movieBox.TrackBox.TrackHeaderBox.TrackID          = 1;
                movieBox.TrackBox.TrackHeaderBox.Duration         = 0;
                movieBox.TrackBox.TrackHeaderBox.TrackIsAudio     = false;
                movieBox.TrackBox.TrackHeaderBox.Width            = (uint)spsInfo.width;
                movieBox.TrackBox.TrackHeaderBox.Height           = (uint)spsInfo.height;
                movieBox.TrackBox.MediaBox = new MediaBox();
                movieBox.TrackBox.MediaBox.MediaHeaderBox = new MediaHeaderBox();
                movieBox.TrackBox.MediaBox.MediaHeaderBox.CreationTime     = 0;
                movieBox.TrackBox.MediaBox.MediaHeaderBox.ModificationTime = 0;
                movieBox.TrackBox.MediaBox.MediaHeaderBox.Timescale        = 1200000;
                movieBox.TrackBox.MediaBox.MediaHeaderBox.Duration         = 0;
                movieBox.TrackBox.MediaBox.HandlerBox             = new HandlerBox();
                movieBox.TrackBox.MediaBox.HandlerBox.HandlerType = HandlerType.vide;
                movieBox.TrackBox.MediaBox.HandlerBox.Name        = "VideoHandler";
                movieBox.TrackBox.MediaBox.MediaInformationBox    = new MediaInformationBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.VideoMediaHeaderBox = new VideoMediaHeaderBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox  = new DataInformationBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox = new DataReferenceBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes = new List <DataEntryBox>();
                movieBox.TrackBox.MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryBoxes.Add(new DataEntryUrlBox(1));
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox = new SampleTableBox();
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox = new SampleDescriptionBox(movieBox.TrackBox.MediaBox.HandlerBox.HandlerType);
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries = new List <SampleEntry>();
                AVC1SampleEntry avc1 = new AVC1SampleEntry();
                avc1.AVCConfigurationBox = new AVCConfigurationBox();
                //h264
                avc1.Width  = (ushort)movieBox.TrackBox.TrackHeaderBox.Width;
                avc1.Height = (ushort)movieBox.TrackBox.TrackHeaderBox.Height;
                avc1.AVCConfigurationBox.AVCLevelIndication   = spsInfo.levelIdc;
                avc1.AVCConfigurationBox.AVCProfileIndication = spsInfo.profileIdc;
                avc1.AVCConfigurationBox.ProfileCompatibility = (byte)spsInfo.profileCompat;
                avc1.AVCConfigurationBox.PPSs = new List <byte[]>()
                {
                    ppsNALU.RawData
                };
                avc1.AVCConfigurationBox.SPSs = new List <byte[]>()
                {
                    spsNALU.RawData
                };
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.SampleEntries.Add(avc1);
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox = new TimeToSampleBox()
                {
                    //TimeToSampleInfos=new List<TimeToSampleBox.TimeToSampleInfo>
                    //{
                    //    new TimeToSampleBox.TimeToSampleInfo
                    //    {
                    //         SampleCount=0,
                    //         SampleDelta=0
                    //    }
                    //}
                };
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleToChunkBox = new SampleToChunkBox()
                {
                    //SampleToChunkInfos=new List<SampleToChunkBox.SampleToChunkInfo>()
                    //{
                    //     new SampleToChunkBox.SampleToChunkInfo
                    //     {

                    //     }
                    //}
                };
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox = new SampleSizeBox()
                {
                    //EntrySize = new List<uint>()
                    //{
                    //    0
                    //}
                };
                movieBox.TrackBox.MediaBox.MediaInformationBox.SampleTableBox.ChunkOffsetBox = new ChunkOffsetBox()
                {
                    //ChunkOffset=new List<uint>()
                    //{
                    //    0
                    //}
                };
                movieBox.MovieExtendsBox = new MovieExtendsBox();
                movieBox.MovieExtendsBox.TrackExtendsBoxs = new List <TrackExtendsBox>();
                TrackExtendsBox trex = new TrackExtendsBox();
                trex.TrackID = 1;
                trex.DefaultSampleDescriptionIndex = 1;
                trex.DefaultSampleDuration         = 0;
                trex.DefaultSampleSize             = 0;
                trex.DefaultSampleFlags            = 0;
                movieBox.MovieExtendsBox.TrackExtendsBoxs.Add(trex);
                movieBox.ToBuffer(ref writer);
                var data = writer.FlushAndGetArray();
                return(data);
            }
            finally
            {
                FMp4ArrayPool.Return(buffer);
            }
        }