private void DecodedReferencePictureMarking(BitReader bitReader) { if (_nalu.NALUType == NALUnitType.IDRSlice) { NoOutputOfPriorPicsFlag = bitReader.GetNextBit(); LongTermReferenceFlag = bitReader.GetNextBit(); } else { AdaptiveRefPicMarkingModeFlag = bitReader.GetNextBit(); if (AdaptiveRefPicMarkingModeFlag) { do { MemoryManagementControlOperation = bitReader.DecodeUnsignedExpGolomb(); if ((MemoryManagementControlOperation == 1) || (MemoryManagementControlOperation == 3)) { DifferenceOfPicNumsMinus1 = bitReader.DecodeUnsignedExpGolomb(); } if (MemoryManagementControlOperation == 2) { LongTermPicNum = bitReader.DecodeUnsignedExpGolomb(); } if ((MemoryManagementControlOperation == 3) || (MemoryManagementControlOperation == 6)) { LongTermFrameIdx = bitReader.DecodeUnsignedExpGolomb(); } if (MemoryManagementControlOperation == 4) { MaxLongTermFrameIdxPlus1 = bitReader.DecodeUnsignedExpGolomb(); } } while (MemoryManagementControlOperation != 0); } } }
public override void Read(BitReader bitReader) { base.Read(bitReader); // read-in the NALU // NOTE: this coded slice has no Header (it should remain null) SliceID = bitReader.DecodeUnsignedExpGolomb(); if (_pps.RedundantPICCountPresentFlag) RedundantPictureCount = bitReader.DecodeUnsignedExpGolomb(); Data.Read(bitReader); // FIXME: SliceData above is not complete, so for now let's just skip the rest of data Nalu.SkipToEndOfNALU(bitReader); Nalu.CheckSize(bitReader); }
public void Read(BitReader bitReader) { if ((_mbLayer.MbPartPredMode(_mbType, 0) == MacroBlockPartitionMode.Intra_4x4) || (_mbLayer.MbPartPredMode(_mbType, 0) == MacroBlockPartitionMode.Intra_16x16)) { if (_mbLayer.MbPartPredMode(_mbType, 0) == MacroBlockPartitionMode.Intra_4x4) { for (int luma4x4ids = 0; luma4x4ids < 16; luma4x4ids++) { PrevIntra4x4PredModeFlag[luma4x4ids] = bitReader.GetNextBit(); if (!PrevIntra4x4PredModeFlag[luma4x4ids]) { RemIntra4x4PredMode[luma4x4ids] = bitReader.GetByteFromNBits(3); } } } IntraChromaPredMode = (byte)bitReader.DecodeUnsignedExpGolomb(); } else if (_mbLayer.MbPartPredMode(_mbType, 0) != MacroBlockPartitionMode.Direct) { int numPart = (int)_mbLayer.NumMbPart(_mbType); for (int mbPartIdx = 0; mbPartIdx < numPart; mbPartIdx++) { } for (int mbPartIdx = 0; mbPartIdx < numPart; mbPartIdx++) { } for (int mbPartIdx = 0; mbPartIdx < numPart; mbPartIdx++) { } for (int mbPartIdx = 0; mbPartIdx < numPart; mbPartIdx++) { } } }
private SliceTypes ReadSliceType(BitReader bitReader) { uint st = bitReader.DecodeUnsignedExpGolomb(); if (st > 4) { if (_pps.SliceTypeVal == st) { _pps.SameCount++; } else { _pps.SliceTypeVal = st; _pps.SameCount = 0; } st = st - 5; } else { if ((_pps.SliceTypeVal > 4) && (st == (_pps.SliceTypeVal - 5))) { _pps.SameCount++; } else { _pps.SliceTypeVal = 0; } } return((SliceTypes)st); }
public override void Read(BitReader bitReader) { base.Read(bitReader); // read-in the NALU // NOTE: this coded slice has no Header (it should remain null) SliceID = bitReader.DecodeUnsignedExpGolomb(); if (_pps.RedundantPICCountPresentFlag) { RedundantPictureCount = bitReader.DecodeUnsignedExpGolomb(); } Data.Read(bitReader); // FIXME: SliceData above is not complete, so for now let's just skip the rest of data Nalu.SkipToEndOfNALU(bitReader); Nalu.CheckSize(bitReader); }
public byte TimeOffsetLength; // 0 u(5) //} public void Read(BitReader bitReader) { CpbCount = bitReader.DecodeUnsignedExpGolomb() + 1; BitRateScale = bitReader.GetByteFromNBits(4); CpbSizeScale = bitReader.GetByteFromNBits(4); BitRateValue = new uint[CpbCount]; CpbSizeValue = new uint[CpbCount]; Cbr = new bool[CpbCount]; for (int i = 0; i < CpbCount; i++) { BitRateValue[i] = bitReader.DecodeUnsignedExpGolomb() + 1; CpbSizeValue[i] = bitReader.DecodeUnsignedExpGolomb() + 1; Cbr[i] = bitReader.GetNextBit(); } InitialCpbRemovalDelayLength = (byte)(bitReader.GetByteFromNBits(5) + 1); CpbRemovalDelayLength = (byte)(bitReader.GetByteFromNBits(5) + 1); DpbOutputDelayLength = (byte)(bitReader.GetByteFromNBits(5) + 1); TimeOffsetLength = bitReader.GetByteFromNBits(5); }
public override void Read(BitReader bitReader) { base.Read(bitReader); Profile = bitReader.ReadByte(); Constraints = bitReader.ReadByte(); Level = bitReader.ReadByte(); gSPSID = bitReader.DecodeUnsignedExpGolomb(); gLog2MaxFrameNumMinus4 = bitReader.DecodeUnsignedExpGolomb(); gPicOrderCntType = bitReader.DecodeUnsignedExpGolomb(); if (gPicOrderCntType == 0) gMaxPicOrderCntLsbMinus4 = bitReader.DecodeUnsignedExpGolomb(); else if (gPicOrderCntType == 1) { DeltaPicOrderAlwaysZero = bitReader.GetNextBit(); gOffsetForNonRefPic = bitReader.DecodeSignedExpGolomb(); gOffsetForTopToBottomField = bitReader.DecodeSignedExpGolomb(); gNumRefFramesInPicOrderCntCycle = bitReader.DecodeUnsignedExpGolomb(); gOffsetsForRefFrames = new int[gNumRefFramesInPicOrderCntCycle]; for (int i = 0; i < gNumRefFramesInPicOrderCntCycle; i++) gOffsetsForRefFrames[i] = bitReader.DecodeSignedExpGolomb(); } gNumRefFrames = bitReader.DecodeUnsignedExpGolomb(); GapsInFrameNumValueAllowed = bitReader.GetNextBit(); gPicWidthInMBsMinus1 = bitReader.DecodeUnsignedExpGolomb(); Width = gPicWidthInMBsMinus1 + 1; gPicHeightInMBsMinus1 = bitReader.DecodeUnsignedExpGolomb(); Height = gPicHeightInMBsMinus1 + 1; PicSizeInMBs = Height * Width; FrameMBSOnly = bitReader.GetNextBit(); uint interlaceFactor = (FrameMBSOnly) ? 1U : 2U; Width *= 16; Height *= (16 * interlaceFactor); if (!FrameMBSOnly) MBAdaptiveFrameField = bitReader.GetNextBit(); Direct8x8Inference = bitReader.GetNextBit(); FrameCropping = bitReader.GetNextBit(); if (FrameCropping) { FrameCropLeftOffset = bitReader.DecodeUnsignedExpGolomb(); FrameCropRightOffset = bitReader.DecodeUnsignedExpGolomb(); FrameCropTopOffset = bitReader.DecodeUnsignedExpGolomb(); FrameCropBottomOffset = bitReader.DecodeUnsignedExpGolomb(); } VUIParametersPresent = bitReader.GetNextBit(); if (VUIParametersPresent) { vuiParams = new VUIParams(); vuiParams.Read(bitReader); } }
public override void Read(BitReader bitReader) { base.Read(bitReader); // read-in the NALU Header.Read(bitReader); SliceID = bitReader.DecodeUnsignedExpGolomb(); // this is not in base class Data.Read(bitReader); // FIXME: SliceData above is not complete, so for now let's just skip the rest of data Nalu.SkipToEndOfNALU(bitReader); Nalu.CheckSize(bitReader); }
private void BufferingPeriod() { SeqParameterSetID = _bitReader.DecodeUnsignedExpGolomb(); if (SeqParameterSetID != _sps.gSPSID) { throw new Exception("wrong sps ID in SEI"); } if (_sps.vuiParams.NALHRDParametersPresent) { ushort icpbBitLen = _sps.vuiParams.NalHRDParams.InitialCpbRemovalDelayLength; uint cpbCount = _sps.vuiParams.NalHRDParams.CpbCount; InitialCPBRemovalDelay = new uint[cpbCount]; InitialCPBRemovalDelayOffset = new uint[cpbCount]; for (int i = 0; i < cpbCount; i++) { InitialCPBRemovalDelay[i] = _bitReader.GetUIntFromNBits(icpbBitLen); InitialCPBRemovalDelayOffset[i] = _bitReader.GetUIntFromNBits(icpbBitLen); } } // FIXME: The following if statement should probably be "else if". // ITU-T Rec. H.264(05/2003) p. 259 // FIXME: set to true for now bool needForPresenceOfBufferingPeriods = true; if (_sps.vuiParams.VclHRDParametersPresent || needForPresenceOfBufferingPeriods) { ushort icpbBitLen = _sps.vuiParams.VclHRDParams.InitialCpbRemovalDelayLength; uint cpbCount = _sps.vuiParams.VclHRDParams.CpbCount; InitialCPBRemovalDelay = new uint[cpbCount]; InitialCPBRemovalDelayOffset = new uint[cpbCount]; for (int i = 0; i < cpbCount; i++) { InitialCPBRemovalDelay[i] = _bitReader.GetUIntFromNBits(icpbBitLen); InitialCPBRemovalDelayOffset[i] = _bitReader.GetUIntFromNBits(icpbBitLen); } } }
private void ReferencePictureListReordering(BitReader bitReader) { if ((SliceType != SliceTypes.I) && (SliceType != SliceTypes.SI)) { RefPicListReorderingFlagI0 = bitReader.GetNextBit(); if (RefPicListReorderingFlagI0) { do { ReorderingOfPicNumsIDC = bitReader.DecodeUnsignedExpGolomb(); if (ReorderingOfPicNumsIDC > 3) { throw new Exception("SliceHeader: ReferencePictureListReordering 1, invalid pic nums IDC"); } if ((ReorderingOfPicNumsIDC == 0) || (ReorderingOfPicNumsIDC == 1)) { AbsDiffPicNumMinus1 = bitReader.DecodeUnsignedExpGolomb(); } else if (ReorderingOfPicNumsIDC == 2) { LongTermPicNum = bitReader.DecodeUnsignedExpGolomb(); } } while (ReorderingOfPicNumsIDC != 3); } } if (SliceType == SliceTypes.B) { RefPicListReorderingFlagI1 = bitReader.GetNextBit(); if (RefPicListReorderingFlagI1) { do { ReorderingOfPicNumsIDC = bitReader.DecodeUnsignedExpGolomb(); if (ReorderingOfPicNumsIDC > 3) { throw new Exception("SliceHeader: ReferencePictureListReordering 2, invalid pic nums IDC"); } if ((ReorderingOfPicNumsIDC == 0) || (ReorderingOfPicNumsIDC == 1)) { AbsDiffPicNumMinus1 = bitReader.DecodeUnsignedExpGolomb(); } else if (ReorderingOfPicNumsIDC == 2) { LongTermPicNum = bitReader.DecodeUnsignedExpGolomb(); } } while (ReorderingOfPicNumsIDC != 3); } } }
public void Read(BitReader bitReader) { FirstMBInSlice = bitReader.DecodeUnsignedExpGolomb(); SliceType = ReadSliceType(bitReader); PICParameterSetID = bitReader.DecodeUnsignedExpGolomb(); FrameNum = bitReader.GetUIntFromNBits(_frameNumBits); if (!_sps.FrameMBSOnly) { FieldPicFlag = bitReader.GetNextBit(); if (FieldPicFlag) { BottomFieldFlag = bitReader.GetNextBit(); } } if (_nalu.NALUType == NALUnitType.IDRSlice) { IDRPictureID = bitReader.DecodeUnsignedExpGolomb(); PrevRefFrameNum = 0; } if (_sps.gPicOrderCntType == 0) { PictureOrderCountLSB = bitReader.GetUIntFromNBits((ushort)(_sps.gMaxPicOrderCntLsbMinus4 + 4u)); if (_pps.PICOrderPresentFlag && !FieldPicFlag) { DeltaPictureOrderCountBottom = bitReader.DecodeSignedExpGolomb(); } } if ((_sps.gPicOrderCntType == 1) && !_sps.DeltaPicOrderAlwaysZero) { DeltaPictureOrderCount = new int[2]; DeltaPictureOrderCount[0] = bitReader.DecodeSignedExpGolomb(); if (_pps.PICOrderPresentFlag && !FieldPicFlag) { DeltaPictureOrderCount[1] = bitReader.DecodeSignedExpGolomb(); } } if (_pps.RedundantPICCountPresentFlag) { RedundantPictureCount = bitReader.DecodeUnsignedExpGolomb(); } if (SliceType == SliceTypes.B) { DirectSpatialMVPredFlag = bitReader.GetNextBit(); } if ((SliceType == SliceTypes.P) || (SliceType == SliceTypes.SP) || (SliceType == SliceTypes.B)) { NumRefIdxActiveOverrideFlag = bitReader.GetNextBit(); if (NumRefIdxActiveOverrideFlag) { NumRefIdx10ActiveMinus1 = bitReader.DecodeUnsignedExpGolomb(); if (SliceType == SliceTypes.B) { NumRefIdx11ActiveMinus1 = bitReader.DecodeUnsignedExpGolomb(); } } } ReferencePictureListReordering(bitReader); if ((_pps.WeightedPredFlag && ((SliceType == SliceTypes.P) || (SliceType == SliceTypes.SP))) || ((_pps.WeightedBiPredIDC == 1) && (SliceType == SliceTypes.B))) { PredictionWeightTable(bitReader); } if (_nalu.NALRefIDC != 0) { DecodedReferencePictureMarking(bitReader); } if (_pps.EntropyCodingModeFlag && (SliceType != SliceTypes.I) && (SliceType != SliceTypes.SI)) { uint initIdc = bitReader.DecodeUnsignedExpGolomb(); if (initIdc < 4) { CABACInitIDC = (byte)initIdc; } else { throw new Exception("SliceHeader: bad CABACInitIDC"); } } SliceQPDelta = bitReader.DecodeSignedExpGolomb(); SliceQPy = 26 + _pps.PICInitQPMinus26 + SliceQPDelta; if ((SliceType == SliceTypes.SP) || (SliceType == SliceTypes.SI)) { if (SliceType == SliceTypes.SP) { SP4SwitchFlag = bitReader.GetNextBit(); } SliceQSDelta = bitReader.DecodeSignedExpGolomb(); SliceQSy = 26 + _pps.PICInitQSMinus26 + SliceQSDelta; } if (_pps.DeblockingFilterControlPresentFlag) { DisableDeblockingFilterIDC = bitReader.DecodeUnsignedExpGolomb(); if (DisableDeblockingFilterIDC != 1) { SliceAlphaC0OffsetDiv2 = bitReader.DecodeSignedExpGolomb(); SliceBetaOffsetDiv2 = bitReader.DecodeSignedExpGolomb(); } } if ((_pps.NumSliceGroupsMinus1 > 0) && (_pps.SliceGroupMapType >= 3) && (_pps.SliceGroupMapType <= 5)) { SliceGroupChangeCycle = bitReader.GetUIntFromNBits(BitReader.CalcBitsNeededToRepresent(_pps.NumSliceGroupsMinus1 + 1)); } // once the header is read, we can derive the MacroBlock to SliceGroup mapping (Section 8.2.2 of H264 Spec) if (_pps.NumSliceGroupsMinus1 > 0) { MbToSliceGroupMap = DeriveMB2SliceGroupMap(); } }
public void Read(BitReader bitReader) { if (_pps.EntropyCodingModeFlag) { while (!bitReader.ByteAligned) if ((CABACAlignmentOneBit = bitReader.GetNextBit()) == false) throw new Exception("Invalid entropy coding start for slice data"); } CurrMbAddr = _header.FirstMBInSlice * (1u + (_header.MBaffFrameFlag ? 1u : 0u)); moreDataFlag = true; prevMbSkipped = false; do { if ((_header.SliceType != SliceTypes.I) && (_header.SliceType != SliceTypes.SI)) { if (!_pps.EntropyCodingModeFlag) { MBSkipRun = bitReader.DecodeUnsignedExpGolomb(); prevMbSkipped = (MBSkipRun > 0); for (int i = 0; i < MBSkipRun; i++) { CurrMbAddr = NextMBAddress(CurrMbAddr); } moreDataFlag = _nalu.MoreRBSPData(bitReader); } else { mbSkipFlag = false; // FIXME: CABAC value bitReader.GetNextBit(); moreDataFlag = !mbSkipFlag; } } if (moreDataFlag) { if (_header.MBaffFrameFlag && (CurrMbAddr % 2 == 0 || (CurrMbAddr % 2 == 1 && prevMbSkipped))) { mbFieldDecodingFlag = false; // FIXME: CABAC value bitReader.GetNextBit(); } MacroBlockLayer mbLayer = new MacroBlockLayer(_sps, _pps, _header, this); mbLayer.Read(bitReader); } if (!_pps.EntropyCodingModeFlag) moreDataFlag = _nalu.MoreRBSPData(bitReader); else { if (_header.SliceType != SliceTypes.I && _header.SliceType != SliceTypes.SI) prevMbSkipped = mbSkipFlag; if (_header.MBaffFrameFlag && CurrMbAddr % 2 == 0) moreDataFlag = true; else { endOfSliceFlag = bitReader.GetNextBit(); // FIXME: random bit bec. residual is not read moreDataFlag = !endOfSliceFlag; } } CurrMbAddr = NextMBAddress(CurrMbAddr); } while (moreDataFlag); }
public void Read(BitReader bitReader) { AspectRatioInfoPresent = bitReader.GetNextBit(); if (AspectRatioInfoPresent) { AspectRatio = bitReader.GetByteFromNBits(8); if (AspectRatio == Extended_SAR) { SARWidth = (ushort)bitReader.GetUIntFromNBits(16); SARHeight = (ushort)bitReader.GetUIntFromNBits(16); } } OverscanInfoPresent = bitReader.GetNextBit(); if (OverscanInfoPresent) { OverscanAppropriate = bitReader.GetNextBit(); } VideoSignalTypePresent = bitReader.GetNextBit(); if (VideoSignalTypePresent) { VideoFormat = bitReader.GetByteFromNBits(3); VideoFullRange = bitReader.GetNextBit(); ColourDescriptionPresent = bitReader.GetNextBit(); if (ColourDescriptionPresent) { ColourPrimaries = bitReader.GetByteFromNBits(8); TransferCharacteristics = bitReader.GetByteFromNBits(8); MatrixCoefficients = bitReader.GetByteFromNBits(8); } } ChromaLocInfoPresent = bitReader.GetNextBit(); if (ChromaLocInfoPresent) { ChromaSampleLocTypeTopField = bitReader.DecodeUnsignedExpGolomb(); ChromaSampleLocTypeBottomField = bitReader.DecodeUnsignedExpGolomb(); } TimingInfoPresent = bitReader.GetNextBit(); if (TimingInfoPresent) { NumUnitsInTick = bitReader.GetUIntFromNBits(32); TimeScale = bitReader.GetUIntFromNBits(32); FixedFrameRate = bitReader.GetNextBit(); } NALHRDParametersPresent = bitReader.GetNextBit(); if (NALHRDParametersPresent) { NalHRDParams = new HRDParams(); NalHRDParams.Read(bitReader); } VclHRDParametersPresent = bitReader.GetNextBit(); if (VclHRDParametersPresent) { VclHRDParams = new HRDParams(); VclHRDParams.Read(bitReader); } if (NALHRDParametersPresent || VclHRDParametersPresent) { LowDelayHRDFlag = bitReader.GetNextBit(); } PicStructPresent = bitReader.GetNextBit(); BitstreamRestriction = bitReader.GetNextBit(); if (BitstreamRestriction) { MotionVectorsOverPicBoundaries = bitReader.GetNextBit(); MaxBytesPerPicDenom = bitReader.DecodeUnsignedExpGolomb(); MaxBitsPerMBDenom = bitReader.DecodeUnsignedExpGolomb(); Log2MaxMVLengthHorizontal = bitReader.DecodeUnsignedExpGolomb(); Log2MaxMVLengthVertical = bitReader.DecodeUnsignedExpGolomb(); NumReorderFrames = bitReader.DecodeUnsignedExpGolomb(); MaxDecFrameBuffering = bitReader.DecodeUnsignedExpGolomb(); } if ((AspectRatio > 0) && (AspectRatio < 17)) { AspectRatioX = vui_aspect_x[AspectRatio]; AspectRatioY = vui_aspect_y[AspectRatio]; } // gWidth = (uint)(gWidth * ((double)vui_aspect_x[vuiParams.AspectRatio]) / ((double)vui_aspect_y[vuiParams.AspectRatio])); }
public void Read(BitReader bitReader) { FirstMBInSlice = bitReader.DecodeUnsignedExpGolomb(); SliceType = ReadSliceType(bitReader); PICParameterSetID = bitReader.DecodeUnsignedExpGolomb(); FrameNum = bitReader.GetUIntFromNBits(_frameNumBits); if (!_sps.FrameMBSOnly) { FieldPicFlag = bitReader.GetNextBit(); if (FieldPicFlag) BottomFieldFlag = bitReader.GetNextBit(); } if (_nalu.NALUType == NALUnitType.IDRSlice) { IDRPictureID = bitReader.DecodeUnsignedExpGolomb(); PrevRefFrameNum = 0; } if (_sps.gPicOrderCntType == 0) { PictureOrderCountLSB = bitReader.GetUIntFromNBits((ushort)(_sps.gMaxPicOrderCntLsbMinus4 + 4u)); if (_pps.PICOrderPresentFlag && !FieldPicFlag) DeltaPictureOrderCountBottom = bitReader.DecodeSignedExpGolomb(); } if ((_sps.gPicOrderCntType == 1) && !_sps.DeltaPicOrderAlwaysZero) { DeltaPictureOrderCount = new int[2]; DeltaPictureOrderCount[0] = bitReader.DecodeSignedExpGolomb(); if (_pps.PICOrderPresentFlag && !FieldPicFlag) DeltaPictureOrderCount[1] = bitReader.DecodeSignedExpGolomb(); } if (_pps.RedundantPICCountPresentFlag) RedundantPictureCount = bitReader.DecodeUnsignedExpGolomb(); if (SliceType == SliceTypes.B) DirectSpatialMVPredFlag = bitReader.GetNextBit(); if ((SliceType == SliceTypes.P) || (SliceType == SliceTypes.SP) || (SliceType == SliceTypes.B)) { NumRefIdxActiveOverrideFlag = bitReader.GetNextBit(); if (NumRefIdxActiveOverrideFlag) { NumRefIdx10ActiveMinus1 = bitReader.DecodeUnsignedExpGolomb(); if (SliceType == SliceTypes.B) NumRefIdx11ActiveMinus1 = bitReader.DecodeUnsignedExpGolomb(); } } ReferencePictureListReordering(bitReader); if ((_pps.WeightedPredFlag && ((SliceType == SliceTypes.P) || (SliceType == SliceTypes.SP))) || ((_pps.WeightedBiPredIDC == 1) && (SliceType == SliceTypes.B))) { PredictionWeightTable(bitReader); } if (_nalu.NALRefIDC != 0) DecodedReferencePictureMarking(bitReader); if (_pps.EntropyCodingModeFlag && (SliceType != SliceTypes.I) && (SliceType != SliceTypes.SI)) { uint initIdc = bitReader.DecodeUnsignedExpGolomb(); if (initIdc < 4) CABACInitIDC = (byte)initIdc; else throw new Exception("SliceHeader: bad CABACInitIDC"); } SliceQPDelta = bitReader.DecodeSignedExpGolomb(); SliceQPy = 26 + _pps.PICInitQPMinus26 + SliceQPDelta; if ((SliceType == SliceTypes.SP) || (SliceType == SliceTypes.SI)) { if (SliceType == SliceTypes.SP) SP4SwitchFlag = bitReader.GetNextBit(); SliceQSDelta = bitReader.DecodeSignedExpGolomb(); SliceQSy = 26 + _pps.PICInitQSMinus26 + SliceQSDelta; } if (_pps.DeblockingFilterControlPresentFlag) { DisableDeblockingFilterIDC = bitReader.DecodeUnsignedExpGolomb(); if (DisableDeblockingFilterIDC != 1) { SliceAlphaC0OffsetDiv2 = bitReader.DecodeSignedExpGolomb(); SliceBetaOffsetDiv2 = bitReader.DecodeSignedExpGolomb(); } } if ((_pps.NumSliceGroupsMinus1 > 0) && (_pps.SliceGroupMapType >= 3) && (_pps.SliceGroupMapType <= 5)) SliceGroupChangeCycle = bitReader.GetUIntFromNBits(BitReader.CalcBitsNeededToRepresent(_pps.NumSliceGroupsMinus1 + 1)); // once the header is read, we can derive the MacroBlock to SliceGroup mapping (Section 8.2.2 of H264 Spec) if (_pps.NumSliceGroupsMinus1 > 0) MbToSliceGroupMap = DeriveMB2SliceGroupMap(); }
private SliceTypes ReadSliceType(BitReader bitReader) { uint st = bitReader.DecodeUnsignedExpGolomb(); if (st > 4) { if (_pps.SliceTypeVal == st) _pps.SameCount++; else { _pps.SliceTypeVal = st; _pps.SameCount = 0; } st = st - 5; } else { if ((_pps.SliceTypeVal > 4) && (st == (_pps.SliceTypeVal - 5))) _pps.SameCount++; else _pps.SliceTypeVal = 0; } return (SliceTypes)st; }
public static SliceType GetSliceTypeFromH264Payload(byte[] payload) { int countToZero = payload.Length; BinaryReader reader = new BinaryReader(new MemoryStream(payload)); while (countToZero > 4) { ulong naluLen = BitReader.GetUIntValue(reader.ReadUInt32()); long nextPos = reader.BaseStream.Position + (long)naluLen; //Table 7-1 – NAL unit type codes //nal_unit_type Content of NAL unit and RBSP syntax structure C //0 Unspecified //1 Coded slice of a non-IDR picture // slice_layer_without_partitioning_rbsp( ) //2 Coded slice data partition A // slice_data_partition_a_layer_rbsp( ) //3 Coded slice data partition B // slice_data_partition_b_layer_rbsp( ) //4 Coded slice data partition C // slice_data_partition_c_layer_rbsp( ) //5 Coded slice of an IDR picture // slice_layer_without_partitioning_rbsp( ) //6 Supplemental enhancement information (SEI) // sei_rbsp( ) //7 Sequence parameter set // seq_parameter_set_rbsp( ) //8 Picture parameter set // pic_parameter_set_rbsp( ) //9 Access unit delimiter // access_unit_delimiter_rbsp( ) //10 End of sequence // end_of_seq_rbsp( ) //11 End of stream // end_of_stream_rbsp( ) //12 Filler data // filler_data_rbsp( ) //13..23 Reserved //24..31 Unspecified uint typ = reader.ReadByte() & 0x1Fu; if ((naluLen > (ulong)countToZero) || (naluLen < 2)) { return(SliceType.Unknown); } // NALU type 5 is IDR picture if ((typ == 5u) || (typ == 1u)) { // when the type == 5 its an IDR picture, now slice_type shall be equal to // 2, 4, 7, or 9 (I or SI); // slice_type specified the coding type of the slice according to Table 7-6: e.g. P, B, I, SP, SI // // nalu header: // first_mb_in_slice = get_ue_golomb // slice_type = get_ue_golomb // // http://www.iitk.ac.in/mwn/vaibhav/Vaibhav%20Sharma_files/h.264_standard_document.pdf //Table 7-3 – Name association to slice_type //0 P (P slice) //1 B (B slice) //2 I (I slice) //3 SP (SP slice) //4 SI (SI slice) //5 P (P slice) //6 B (B slice) //7 I (I slice) //8 SP (SP slice) //9 SI (SI slice) BitReader br = new BitReader(reader.BaseStream); uint first_mb_in_slice = br.DecodeUnsignedExpGolomb(); uint slice_type = br.DecodeUnsignedExpGolomb(); uint pic_parameter_set_id = br.DecodeUnsignedExpGolomb(); uint frame_num = br.DecodeUnsignedExpGolomb(); if (typ == 5u) { return(SliceType.IFrame); } if (slice_type == 1 || slice_type == 6) { return(SliceType.BFrame); } return(SliceType.DFrame); } countToZero -= ((int)naluLen + 4); reader.BaseStream.Position = nextPos; } return(SliceType.Unknown); }
private void DecodedReferencePictureMarking(BitReader bitReader) { if (_nalu.NALUType == NALUnitType.IDRSlice) { NoOutputOfPriorPicsFlag = bitReader.GetNextBit(); LongTermReferenceFlag = bitReader.GetNextBit(); } else { AdaptiveRefPicMarkingModeFlag = bitReader.GetNextBit(); if (AdaptiveRefPicMarkingModeFlag) { do { MemoryManagementControlOperation = bitReader.DecodeUnsignedExpGolomb(); if ((MemoryManagementControlOperation == 1) || (MemoryManagementControlOperation == 3)) DifferenceOfPicNumsMinus1 = bitReader.DecodeUnsignedExpGolomb(); if (MemoryManagementControlOperation == 2) LongTermPicNum = bitReader.DecodeUnsignedExpGolomb(); if ((MemoryManagementControlOperation == 3) || (MemoryManagementControlOperation == 6)) LongTermFrameIdx = bitReader.DecodeUnsignedExpGolomb(); if (MemoryManagementControlOperation == 4) MaxLongTermFrameIdxPlus1 = bitReader.DecodeUnsignedExpGolomb(); } while (MemoryManagementControlOperation != 0); } } }
public static SliceType GetSliceTypeFromH264Payload(byte[] payload) { int countToZero = payload.Length; BinaryReader reader = new BinaryReader(new MemoryStream(payload)); while (countToZero > 4) { ulong naluLen = BitReader.GetUIntValue(reader.ReadUInt32()); long nextPos = reader.BaseStream.Position + (long)naluLen; //Table 7-1 – NAL unit type codes //nal_unit_type Content of NAL unit and RBSP syntax structure C //0 Unspecified //1 Coded slice of a non-IDR picture // slice_layer_without_partitioning_rbsp( ) //2 Coded slice data partition A // slice_data_partition_a_layer_rbsp( ) //3 Coded slice data partition B // slice_data_partition_b_layer_rbsp( ) //4 Coded slice data partition C // slice_data_partition_c_layer_rbsp( ) //5 Coded slice of an IDR picture // slice_layer_without_partitioning_rbsp( ) //6 Supplemental enhancement information (SEI) // sei_rbsp( ) //7 Sequence parameter set // seq_parameter_set_rbsp( ) //8 Picture parameter set // pic_parameter_set_rbsp( ) //9 Access unit delimiter // access_unit_delimiter_rbsp( ) //10 End of sequence // end_of_seq_rbsp( ) //11 End of stream // end_of_stream_rbsp( ) //12 Filler data // filler_data_rbsp( ) //13..23 Reserved //24..31 Unspecified uint typ = reader.ReadByte() & 0x1Fu; if ((naluLen > (ulong)countToZero) || (naluLen < 2)) return SliceType.Unknown; // NALU type 5 is IDR picture if ((typ == 5u) || (typ == 1u)) { // when the type == 5 its an IDR picture, now slice_type shall be equal to // 2, 4, 7, or 9 (I or SI); // slice_type specified the coding type of the slice according to Table 7-6: e.g. P, B, I, SP, SI // // nalu header: // first_mb_in_slice = get_ue_golomb // slice_type = get_ue_golomb // // http://www.iitk.ac.in/mwn/vaibhav/Vaibhav%20Sharma_files/h.264_standard_document.pdf //Table 7-3 – Name association to slice_type //0 P (P slice) //1 B (B slice) //2 I (I slice) //3 SP (SP slice) //4 SI (SI slice) //5 P (P slice) //6 B (B slice) //7 I (I slice) //8 SP (SP slice) //9 SI (SI slice) BitReader br = new BitReader(reader.BaseStream); uint first_mb_in_slice = br.DecodeUnsignedExpGolomb(); uint slice_type = br.DecodeUnsignedExpGolomb(); uint pic_parameter_set_id = br.DecodeUnsignedExpGolomb(); uint frame_num = br.DecodeUnsignedExpGolomb(); if (typ == 5u) return (SliceType.IFrame); if (slice_type == 1 || slice_type == 6) return (SliceType.BFrame); return (SliceType.DFrame); } countToZero -= ((int)naluLen + 4); reader.BaseStream.Position = nextPos; } return SliceType.Unknown; }
public void Read(BitReader bitReader) { if (_pps.EntropyCodingModeFlag) { while (!bitReader.ByteAligned) { if ((CABACAlignmentOneBit = bitReader.GetNextBit()) == false) { throw new Exception("Invalid entropy coding start for slice data"); } } } CurrMbAddr = _header.FirstMBInSlice * (1u + (_header.MBaffFrameFlag ? 1u : 0u)); moreDataFlag = true; prevMbSkipped = false; do { if ((_header.SliceType != SliceTypes.I) && (_header.SliceType != SliceTypes.SI)) { if (!_pps.EntropyCodingModeFlag) { MBSkipRun = bitReader.DecodeUnsignedExpGolomb(); prevMbSkipped = (MBSkipRun > 0); for (int i = 0; i < MBSkipRun; i++) { CurrMbAddr = NextMBAddress(CurrMbAddr); } moreDataFlag = _nalu.MoreRBSPData(bitReader); } else { mbSkipFlag = false; // FIXME: CABAC value bitReader.GetNextBit(); moreDataFlag = !mbSkipFlag; } } if (moreDataFlag) { if (_header.MBaffFrameFlag && (CurrMbAddr % 2 == 0 || (CurrMbAddr % 2 == 1 && prevMbSkipped))) { mbFieldDecodingFlag = false; // FIXME: CABAC value bitReader.GetNextBit(); } MacroBlockLayer mbLayer = new MacroBlockLayer(_sps, _pps, _header, this); mbLayer.Read(bitReader); } if (!_pps.EntropyCodingModeFlag) { moreDataFlag = _nalu.MoreRBSPData(bitReader); } else { if (_header.SliceType != SliceTypes.I && _header.SliceType != SliceTypes.SI) { prevMbSkipped = mbSkipFlag; } if (_header.MBaffFrameFlag && CurrMbAddr % 2 == 0) { moreDataFlag = true; } else { endOfSliceFlag = bitReader.GetNextBit(); // FIXME: random bit bec. residual is not read moreDataFlag = !endOfSliceFlag; } } CurrMbAddr = NextMBAddress(CurrMbAddr); } while (moreDataFlag); }
private void PredictionWeightTable(BitReader bitReader) { LumaLog2WeightDenominator = bitReader.DecodeUnsignedExpGolomb(); ChromaLog2WeightDenominator = bitReader.DecodeUnsignedExpGolomb(); uint cnt = (NumRefIdx10ActiveMinus1 + 1); if (cnt > 0) { LumaWeightI0 = new int[cnt]; LumaOffsetI0 = new int[cnt]; ChromaWeightI0 = new int[cnt][]; ChromaOffsetI0 = new int[cnt][]; for (int i = 0; i < cnt; i++) { LumaWeightI0Flag = bitReader.GetNextBit(); if (LumaWeightI0Flag) { LumaWeightI0[i] = bitReader.DecodeSignedExpGolomb(); LumaOffsetI0[i] = bitReader.DecodeSignedExpGolomb(); } ChromaWeightI0Flag = bitReader.GetNextBit(); if (ChromaWeightI0Flag) { ChromaWeightI0[i] = new int[2]; ChromaOffsetI0[i] = new int[2]; for (int j = 0; j < 2; j++) { ChromaWeightI0[i][j] = bitReader.DecodeSignedExpGolomb(); ChromaOffsetI0[i][j] = bitReader.DecodeSignedExpGolomb(); } } } } if (SliceType == SliceTypes.B) { cnt = (NumRefIdx11ActiveMinus1 + 1); if (cnt > 0) { LumaWeightI1 = new int[cnt]; LumaOffsetI1 = new int[cnt]; ChromaWeightI1 = new int[cnt][]; ChromaOffsetI1 = new int[cnt][]; for (int i = 0; i < cnt; i++) { LumaWeightI1Flag = bitReader.GetNextBit(); if (LumaWeightI1Flag) { LumaWeightI1[i] = bitReader.DecodeSignedExpGolomb(); LumaOffsetI1[i] = bitReader.DecodeSignedExpGolomb(); } ChromaWeightI1Flag = bitReader.GetNextBit(); if (ChromaWeightI1Flag) { ChromaWeightI1[i] = new int[2]; ChromaOffsetI1[i] = new int[2]; for (int j = 0; j < 2; j++) { ChromaWeightI1[i][j] = bitReader.DecodeSignedExpGolomb(); ChromaOffsetI1[i][j] = bitReader.DecodeSignedExpGolomb(); } } } } } }
public void Read(BitReader bitReader) { AspectRatioInfoPresent = bitReader.GetNextBit(); if (AspectRatioInfoPresent) { AspectRatio = bitReader.GetByteFromNBits(8); if (AspectRatio == Extended_SAR) { SARWidth = (ushort)bitReader.GetUIntFromNBits(16); SARHeight = (ushort)bitReader.GetUIntFromNBits(16); } } OverscanInfoPresent = bitReader.GetNextBit(); if (OverscanInfoPresent) OverscanAppropriate = bitReader.GetNextBit(); VideoSignalTypePresent = bitReader.GetNextBit(); if (VideoSignalTypePresent) { VideoFormat = bitReader.GetByteFromNBits(3); VideoFullRange = bitReader.GetNextBit(); ColourDescriptionPresent = bitReader.GetNextBit(); if (ColourDescriptionPresent) { ColourPrimaries = bitReader.GetByteFromNBits(8); TransferCharacteristics = bitReader.GetByteFromNBits(8); MatrixCoefficients = bitReader.GetByteFromNBits(8); } } ChromaLocInfoPresent = bitReader.GetNextBit(); if (ChromaLocInfoPresent) { ChromaSampleLocTypeTopField = bitReader.DecodeUnsignedExpGolomb(); ChromaSampleLocTypeBottomField = bitReader.DecodeUnsignedExpGolomb(); } TimingInfoPresent = bitReader.GetNextBit(); if (TimingInfoPresent) { NumUnitsInTick = bitReader.GetUIntFromNBits(32); TimeScale = bitReader.GetUIntFromNBits(32); FixedFrameRate = bitReader.GetNextBit(); } NALHRDParametersPresent = bitReader.GetNextBit(); if (NALHRDParametersPresent) { NalHRDParams = new HRDParams(); NalHRDParams.Read(bitReader); } VclHRDParametersPresent = bitReader.GetNextBit(); if (VclHRDParametersPresent) { VclHRDParams = new HRDParams(); VclHRDParams.Read(bitReader); } if (NALHRDParametersPresent || VclHRDParametersPresent) LowDelayHRDFlag = bitReader.GetNextBit(); PicStructPresent = bitReader.GetNextBit(); BitstreamRestriction = bitReader.GetNextBit(); if (BitstreamRestriction) { MotionVectorsOverPicBoundaries = bitReader.GetNextBit(); MaxBytesPerPicDenom = bitReader.DecodeUnsignedExpGolomb(); MaxBitsPerMBDenom = bitReader.DecodeUnsignedExpGolomb(); Log2MaxMVLengthHorizontal = bitReader.DecodeUnsignedExpGolomb(); Log2MaxMVLengthVertical = bitReader.DecodeUnsignedExpGolomb(); NumReorderFrames = bitReader.DecodeUnsignedExpGolomb(); MaxDecFrameBuffering = bitReader.DecodeUnsignedExpGolomb(); } if ((AspectRatio > 0) && (AspectRatio < 17)) { AspectRatioX = vui_aspect_x[AspectRatio]; AspectRatioY = vui_aspect_y[AspectRatio]; } // gWidth = (uint)(gWidth * ((double)vui_aspect_x[vuiParams.AspectRatio]) / ((double)vui_aspect_y[vuiParams.AspectRatio])); }
public override void Read(BitReader bitReader) { base.Read(bitReader); PICParamSetID = bitReader.DecodeUnsignedExpGolomb(); SEQParamSetID = bitReader.DecodeUnsignedExpGolomb(); EntropyCodingModeFlag = bitReader.GetNextBit(); PICOrderPresentFlag = bitReader.GetNextBit(); NumSliceGroupsMinus1 = bitReader.DecodeUnsignedExpGolomb(); if (NumSliceGroupsMinus1 > 0) { SliceGroupMapType = bitReader.DecodeUnsignedExpGolomb(); switch (SliceGroupMapType) { case 0: RunLengthMinus1 = new uint[NumSliceGroupsMinus1 + 1]; for (int grp = 0; grp <= NumSliceGroupsMinus1; grp++) { RunLengthMinus1[grp] = bitReader.DecodeUnsignedExpGolomb(); } break; case 1: break; case 2: TopLeft = new uint[NumSliceGroupsMinus1 + 1]; BottomRight = new uint[NumSliceGroupsMinus1 + 1]; for (int grp = 0; grp <= NumSliceGroupsMinus1; grp++) // standards doc, this reads grp < NumSliceGroupsMinus1 { TopLeft[grp] = bitReader.DecodeUnsignedExpGolomb(); BottomRight[grp] = bitReader.DecodeUnsignedExpGolomb(); } break; case 3: case 4: case 5: SliceGroupChangeDirectionFlag = bitReader.GetNextBit(); SliceGroupChangeRateMinus1 = bitReader.DecodeUnsignedExpGolomb(); break; case 6: PICSizeInMapUnitsMinus1 = bitReader.DecodeUnsignedExpGolomb(); SliceGroupID = new uint[PICSizeInMapUnitsMinus1 + 1]; ushort bitCount = BitReader.CalcBitsNeededToRepresent(NumSliceGroupsMinus1 + 1); for (int grp = 0; grp <= PICSizeInMapUnitsMinus1; grp++) { SliceGroupID[grp] = bitReader.GetUIntFromNBits(bitCount); } break; default: throw new Exception("BitReader: bad slice group map type"); } } NumRefIDx10ActiveMinus1 = bitReader.DecodeUnsignedExpGolomb(); NumRefIDx11ActiveMinus1 = bitReader.DecodeUnsignedExpGolomb(); WeightedPredFlag = bitReader.GetNextBit(); WeightedBiPredIDC = (ushort)bitReader.GetUIntFromNBits(2); PICInitQPMinus26 = bitReader.DecodeSignedExpGolomb(); PICInitQSMinus26 = bitReader.DecodeSignedExpGolomb(); ChromaQPIndexOffset = bitReader.DecodeSignedExpGolomb(); DeblockingFilterControlPresentFlag = bitReader.GetNextBit(); ConstrainedIntraPredFlag = bitReader.GetNextBit(); RedundantPICCountPresentFlag = bitReader.GetNextBit(); bitReader.DiscardTrailingBits(); }
public override void Read(BitReader bitReader) { base.Read(bitReader); Profile = bitReader.ReadByte(); Constraints = bitReader.ReadByte(); Level = bitReader.ReadByte(); gSPSID = bitReader.DecodeUnsignedExpGolomb(); gLog2MaxFrameNumMinus4 = bitReader.DecodeUnsignedExpGolomb(); gPicOrderCntType = bitReader.DecodeUnsignedExpGolomb(); if (gPicOrderCntType == 0) { gMaxPicOrderCntLsbMinus4 = bitReader.DecodeUnsignedExpGolomb(); } else if (gPicOrderCntType == 1) { DeltaPicOrderAlwaysZero = bitReader.GetNextBit(); gOffsetForNonRefPic = bitReader.DecodeSignedExpGolomb(); gOffsetForTopToBottomField = bitReader.DecodeSignedExpGolomb(); gNumRefFramesInPicOrderCntCycle = bitReader.DecodeUnsignedExpGolomb(); gOffsetsForRefFrames = new int[gNumRefFramesInPicOrderCntCycle]; for (int i = 0; i < gNumRefFramesInPicOrderCntCycle; i++) { gOffsetsForRefFrames[i] = bitReader.DecodeSignedExpGolomb(); } } gNumRefFrames = bitReader.DecodeUnsignedExpGolomb(); GapsInFrameNumValueAllowed = bitReader.GetNextBit(); gPicWidthInMBsMinus1 = bitReader.DecodeUnsignedExpGolomb(); Width = gPicWidthInMBsMinus1 + 1; gPicHeightInMBsMinus1 = bitReader.DecodeUnsignedExpGolomb(); Height = gPicHeightInMBsMinus1 + 1; PicSizeInMBs = Height * Width; FrameMBSOnly = bitReader.GetNextBit(); uint interlaceFactor = (FrameMBSOnly) ? 1U : 2U; Width *= 16; Height *= (16 * interlaceFactor); if (!FrameMBSOnly) { MBAdaptiveFrameField = bitReader.GetNextBit(); } Direct8x8Inference = bitReader.GetNextBit(); FrameCropping = bitReader.GetNextBit(); if (FrameCropping) { FrameCropLeftOffset = bitReader.DecodeUnsignedExpGolomb(); FrameCropRightOffset = bitReader.DecodeUnsignedExpGolomb(); FrameCropTopOffset = bitReader.DecodeUnsignedExpGolomb(); FrameCropBottomOffset = bitReader.DecodeUnsignedExpGolomb(); } VUIParametersPresent = bitReader.GetNextBit(); if (VUIParametersPresent) { vuiParams = new VUIParams(); vuiParams.Read(bitReader); } }
private void ReferencePictureListReordering(BitReader bitReader) { if ((SliceType != SliceTypes.I) && (SliceType != SliceTypes.SI)) { RefPicListReorderingFlagI0 = bitReader.GetNextBit(); if (RefPicListReorderingFlagI0) do { ReorderingOfPicNumsIDC = bitReader.DecodeUnsignedExpGolomb(); if (ReorderingOfPicNumsIDC > 3) throw new Exception("SliceHeader: ReferencePictureListReordering 1, invalid pic nums IDC"); if ((ReorderingOfPicNumsIDC == 0) || (ReorderingOfPicNumsIDC == 1)) AbsDiffPicNumMinus1 = bitReader.DecodeUnsignedExpGolomb(); else if (ReorderingOfPicNumsIDC == 2) LongTermPicNum = bitReader.DecodeUnsignedExpGolomb(); } while (ReorderingOfPicNumsIDC != 3); } if (SliceType == SliceTypes.B) { RefPicListReorderingFlagI1 = bitReader.GetNextBit(); if (RefPicListReorderingFlagI1) do { ReorderingOfPicNumsIDC = bitReader.DecodeUnsignedExpGolomb(); if (ReorderingOfPicNumsIDC > 3) throw new Exception("SliceHeader: ReferencePictureListReordering 2, invalid pic nums IDC"); if ((ReorderingOfPicNumsIDC == 0) || (ReorderingOfPicNumsIDC == 1)) AbsDiffPicNumMinus1 = bitReader.DecodeUnsignedExpGolomb(); else if (ReorderingOfPicNumsIDC == 2) LongTermPicNum = bitReader.DecodeUnsignedExpGolomb(); } while (ReorderingOfPicNumsIDC != 3); } }