private static void WriteScalingList(H264BitStreamWriter Writer, byte[] List, int Start, int Count) { byte[] Scan = Count == 16 ? ZigZagScan : ZigZagDirect; int LastScale = 8; for (int Index = 0; Index < Count; Index++) { byte Value = List[Start + Scan[Index]]; int DeltaScale = Value - LastScale; Writer.WriteSe(DeltaScale); LastScale = Value; } }
private static void WriteScalingList(H264BitStreamWriter writer, byte[] list, int start, int count) { byte[] scan = count == 16 ? ZigZagScan : ZigZagDirect; int lastScale = 8; for (int index = 0; index < count; index++) { byte value = list[start + scan[index]]; int deltaScale = value - lastScale; writer.WriteSe(deltaScale); lastScale = value; } }
private byte[] EncodeHeader() { using (MemoryStream Data = new MemoryStream()) { H264BitStreamWriter Writer = new H264BitStreamWriter(Data); //Sequence Parameter Set. Writer.WriteU(1, 24); Writer.WriteU(0, 1); Writer.WriteU(3, 2); Writer.WriteU(7, 5); Writer.WriteU(100, 8); Writer.WriteU(0, 8); Writer.WriteU(31, 8); Writer.WriteUe(0); Writer.WriteUe(ChromaFormatIdc); if (ChromaFormatIdc == 3) { Writer.WriteBit(false); } Writer.WriteUe(0); Writer.WriteUe(0); Writer.WriteBit(false); Writer.WriteBit(false); //Scaling matrix present flag Writer.WriteUe(Log2MaxFrameNumMinus4); Writer.WriteUe(PicOrderCntType); if (PicOrderCntType == 0) { Writer.WriteUe(Log2MaxPicOrderCntLsbMinus4); } else if (PicOrderCntType == 1) { Writer.WriteBit(DeltaPicOrderAlwaysZeroFlag); Writer.WriteSe(0); Writer.WriteSe(0); Writer.WriteUe(0); } int PicHeightInMbs = PicHeightInMapUnits / (FrameMbsOnlyFlag ? 1 : 2); Writer.WriteUe(16); Writer.WriteBit(false); Writer.WriteUe(PicWidthInMbs - 1); Writer.WriteUe(PicHeightInMbs - 1); Writer.WriteBit(FrameMbsOnlyFlag); if (!FrameMbsOnlyFlag) { Writer.WriteBit(MbAdaptiveFrameFieldFlag); } Writer.WriteBit(Direct8x8InferenceFlag); Writer.WriteBit(false); //Frame cropping flag Writer.WriteBit(false); //VUI parameter present flag Writer.End(); //Picture Parameter Set. Writer.WriteU(1, 24); Writer.WriteU(0, 1); Writer.WriteU(3, 2); Writer.WriteU(8, 5); Writer.WriteUe(0); Writer.WriteUe(0); Writer.WriteBit(EntropyCodingModeFlag); Writer.WriteBit(false); Writer.WriteUe(0); Writer.WriteUe(NumRefIdxL0DefaultActiveMinus1); Writer.WriteUe(NumRefIdxL1DefaultActiveMinus1); Writer.WriteBit(WeightedPredFlag); Writer.WriteU(WeightedBipredIdc, 2); Writer.WriteSe(PicInitQpMinus26); Writer.WriteSe(0); Writer.WriteSe(ChromaQpIndexOffset); Writer.WriteBit(DeblockingFilterControlPresentFlag); Writer.WriteBit(ConstrainedIntraPredFlag); Writer.WriteBit(RedundantPicCntPresentFlag); Writer.WriteBit(Transform8x8ModeFlag); Writer.WriteBit(true); for (int Index = 0; Index < 6; Index++) { Writer.WriteBit(true); WriteScalingList(Writer, ScalingMatrix4, Index * 16, 16); } if (Transform8x8ModeFlag) { for (int Index = 0; Index < 2; Index++) { Writer.WriteBit(true); WriteScalingList(Writer, ScalingMatrix8, Index * 64, 64); } } Writer.WriteSe(ChromaQpIndexOffset2); Writer.End(); return(Data.ToArray()); } }
private byte[] EncodeHeader() { using (MemoryStream data = new MemoryStream()) { H264BitStreamWriter writer = new H264BitStreamWriter(data); // Sequence Parameter Set. writer.WriteU(1, 24); writer.WriteU(0, 1); writer.WriteU(3, 2); writer.WriteU(7, 5); writer.WriteU(100, 8); writer.WriteU(0, 8); writer.WriteU(31, 8); writer.WriteUe(0); writer.WriteUe(_chromaFormatIdc); if (_chromaFormatIdc == 3) { writer.WriteBit(false); } writer.WriteUe(0); writer.WriteUe(0); writer.WriteBit(false); writer.WriteBit(false); //Scaling matrix present flag writer.WriteUe(_log2MaxFrameNumMinus4); writer.WriteUe(_picOrderCntType); if (_picOrderCntType == 0) { writer.WriteUe(_log2MaxPicOrderCntLsbMinus4); } else if (_picOrderCntType == 1) { writer.WriteBit(_deltaPicOrderAlwaysZeroFlag); writer.WriteSe(0); writer.WriteSe(0); writer.WriteUe(0); } int picHeightInMbs = _picHeightInMapUnits / (_frameMbsOnlyFlag ? 1 : 2); writer.WriteUe(16); writer.WriteBit(false); writer.WriteUe(_picWidthInMbs - 1); writer.WriteUe(picHeightInMbs - 1); writer.WriteBit(_frameMbsOnlyFlag); if (!_frameMbsOnlyFlag) { writer.WriteBit(_mbAdaptiveFrameFieldFlag); } writer.WriteBit(_direct8x8InferenceFlag); writer.WriteBit(false); //Frame cropping flag writer.WriteBit(false); //VUI parameter present flag writer.End(); // Picture Parameter Set. writer.WriteU(1, 24); writer.WriteU(0, 1); writer.WriteU(3, 2); writer.WriteU(8, 5); writer.WriteUe(0); writer.WriteUe(0); writer.WriteBit(_entropyCodingModeFlag); writer.WriteBit(false); writer.WriteUe(0); writer.WriteUe(_numRefIdxL0DefaultActiveMinus1); writer.WriteUe(_numRefIdxL1DefaultActiveMinus1); writer.WriteBit(_weightedPredFlag); writer.WriteU(_weightedBipredIdc, 2); writer.WriteSe(_picInitQpMinus26); writer.WriteSe(0); writer.WriteSe(_chromaQpIndexOffset); writer.WriteBit(_deblockingFilterControlPresentFlag); writer.WriteBit(_constrainedIntraPredFlag); writer.WriteBit(_redundantPicCntPresentFlag); writer.WriteBit(_transform8x8ModeFlag); writer.WriteBit(true); for (int index = 0; index < 6; index++) { writer.WriteBit(true); WriteScalingList(writer, _scalingMatrix4, index * 16, 16); } if (_transform8x8ModeFlag) { for (int index = 0; index < 2; index++) { writer.WriteBit(true); WriteScalingList(writer, _scalingMatrix8, index * 64, 64); } } writer.WriteSe(_chromaQpIndexOffset2); writer.End(); return(data.ToArray()); } }