/// <summary> /// /// </summary> /// <param name="writer"></param> public void ToBuffer(ref FMp4MessagePackWriter writer) { if (MovieFragmentBox != null) { MovieFragmentBox.ToBuffer(ref writer); } if (MediaDataBox != null) { MediaDataBox.ToBuffer(ref writer); } }
/// <summary> /// 编码其他视频数据盒子 /// </summary> /// <param name="package">jt1078完整包</param> /// <param name="moofOffset">moof位置</param> /// <returns></returns> public byte[] EncoderOtherVideoBox(JT1078Package package, ulong moofOffset = 0) { byte[] buffer = FMp4ArrayPool.Rent(package.Bodies.Length + 4096); FMp4MessagePackWriter writer = new FMp4MessagePackWriter(buffer); try { var nalus = h264Decoder.ParseNALU(package); var movieFragmentBox = new MovieFragmentBox(); movieFragmentBox.MovieFragmentHeaderBox = new MovieFragmentHeaderBox(); movieFragmentBox.MovieFragmentHeaderBox.SequenceNumber = sn++; movieFragmentBox.TrackFragmentBox = new TrackFragmentBox(); //0x39 写文件 //0x02 分段 movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox = new TrackFragmentHeaderBox(2); movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.TrackID = 1; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.BaseDataOffset = moofOffset; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleDuration = 48000; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleSize = (uint)package.Bodies.Length; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleFlags = 0x1010000; movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox = new TrackFragmentBaseMediaDecodeTimeBox(); movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox.BaseMediaDecodeTime = package.Timestamp * 1000; //trun //0x39 写文件 //0x02 分段 uint flag = package.Label3.DataType == JT1078DataType.视频I帧 ? 1u : 0u; movieFragmentBox.TrackFragmentBox.TrackRunBox = new TrackRunBox(flags: 0x000400); movieFragmentBox.TrackFragmentBox.TrackRunBox.FirstSampleFlags = 0; movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos = new List <TrackRunBox.TrackRunInfo>(); //movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo()); foreach (var nalu in nalus) { movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo() { SampleSize = (uint)nalu.RawData.Length, SampleCompositionTimeOffset = package.Label3.DataType == JT1078DataType.视频I帧 ? package.LastIFrameInterval : package.LastFrameInterval, SampleFlags = flag }); } movieFragmentBox.ToBuffer(ref writer); var mediaDataBox = new MediaDataBox(); mediaDataBox.Data = nalus.Select(s => s.RawData).ToList(); mediaDataBox.ToBuffer(ref writer); var data = writer.FlushAndGetArray(); return(data); } finally { FMp4ArrayPool.Return(buffer); } }
/// <summary> /// 编码其他视频数据盒子 /// </summary> /// <param name="package">jt1078完整包</param> /// <param name="moofOffset">moof位置</param> /// <returns></returns> public byte[] EncoderOtherVideoBox(JT1078Package package, ulong moofOffset = 0) { byte[] buffer = FMp4ArrayPool.Rent(package.Bodies.Length + 4096); FMp4MessagePackWriter writer = new FMp4MessagePackWriter(buffer); try { var nalus = h264Decoder.ParseNALU(package); var movieFragmentBox = new MovieFragmentBox(); movieFragmentBox.MovieFragmentHeaderBox = new MovieFragmentHeaderBox(); movieFragmentBox.MovieFragmentHeaderBox.SequenceNumber = package.SN; movieFragmentBox.TrackFragmentBox = new TrackFragmentBox(); movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox = new TrackFragmentHeaderBox(0x39); movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.TrackID = 1; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.BaseDataOffset = moofOffset; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleDuration = 48000; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleSize = (uint)package.Bodies.Length; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleFlags = 0x1010000; movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox = new TrackFragmentBaseMediaDecodeTimeBox(); movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox.BaseMediaDecodeTime = package.Timestamp * 1000; //trun movieFragmentBox.TrackFragmentBox.TrackRunBox = new TrackRunBox(flags: 0x5); movieFragmentBox.TrackFragmentBox.TrackRunBox.FirstSampleFlags = 0; movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos = new List <TrackRunBox.TrackRunInfo>(); movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo()); movieFragmentBox.ToBuffer(ref writer); var mediaDataBox = new MediaDataBox(); mediaDataBox.Data = nalus.Select(s => s.RawData).ToList(); mediaDataBox.ToBuffer(ref writer); var data = writer.FlushAndGetArray(); return(data); } finally { FMp4ArrayPool.Return(buffer); } }
/// <summary> /// 编码Moof盒子 /// </summary> /// <returns></returns> public byte[] EncoderMoofBox(List <H264NALU> nalus, int naluLength, ulong timestamp, uint frameInterval, uint keyframeFlag, int moofOffset = 0) { byte[] buffer = FMp4ArrayPool.Rent(naluLength + 4096); FMp4MessagePackWriter writer = new FMp4MessagePackWriter(buffer); try { var movieFragmentBox = new MovieFragmentBox(); movieFragmentBox.MovieFragmentHeaderBox = new MovieFragmentHeaderBox(); movieFragmentBox.MovieFragmentHeaderBox.SequenceNumber = sn++; movieFragmentBox.TrackFragmentBox = new TrackFragmentBox(); //0x39 写文件 //0x02 分段 movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox = new TrackFragmentHeaderBox(0x20038); movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.TrackID = 1; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleDuration = 48000; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleSize = (uint)naluLength; movieFragmentBox.TrackFragmentBox.TrackFragmentHeaderBox.DefaultSampleFlags = 0x1010000; movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox = new TrackFragmentBaseMediaDecodeTimeBox(); //movieFragmentBox.TrackFragmentBox.SampleDependencyTypeBox = new SampleDependencyTypeBox() //{ // SampleDependencyTypes = new List<SampleDependencyTypeBox.SampleDependencyType>() //}; //trun //0x39 写文件 //0x02 分段 //uint flag = 0x000200 | 0x000800 | 0x000400 | 0x000100; uint flag = 4u; //var sdtp = new SampleDependencyTypeBox.SampleDependencyType(); //if (keyframeFlag==1) //{ // sdtp.SampleDependsOn = 2; // sdtp.SampleIsDependedOn = 1; //} //else //{ // sdtp.SampleDependsOn = 1; // sdtp.SampleIsDependedOn = 0; //} if (!first) { //sdtp.IsLeading = 1; //flag = 4u; movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox.BaseMediaDecodeTime = 0; movieFragmentBox.TrackFragmentBox.TrackRunBox = new TrackRunBox(flags: 0x205); first = true; } else { //flag = 0x000400; movieFragmentBox.TrackFragmentBox.TrackFragmentBaseMediaDecodeTimeBox.BaseMediaDecodeTime = BaseMediaDecodeTime; movieFragmentBox.TrackFragmentBox.TrackRunBox = new TrackRunBox(flags: 0x205); BaseMediaDecodeTime += BaseMediaDecodeTime; } //movieFragmentBox.TrackFragmentBox.SampleDependencyTypeBox.SampleDependencyTypes.Add(sdtp); movieFragmentBox.TrackFragmentBox.TrackRunBox.FirstSampleFlags = 33554432; movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos = new List <TrackRunBox.TrackRunInfo>(); //movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo()); movieFragmentBox.TrackFragmentBox.TrackRunBox.TrackRunInfos.Add(new TrackRunBox.TrackRunInfo() { SampleDuration = frameInterval, SampleSize = (uint)naluLength, SampleCompositionTimeOffset = frameInterval, SampleFlags = flag }); movieFragmentBox.ToBuffer(ref writer); var data = writer.FlushAndGetArray(); return(data); } finally { FMp4ArrayPool.Return(buffer); } }