//template<typename BitPacker> public bool encodeOmegaSigned(ref BitPacker packer, int val) { if (!packer.encodeSmall((ushort)((val < 0) ? 1 : 0), 1)) return false; if (val < 0) val = -val - 1; return encodeOmega(ref packer, (uint)val); }
//template<typename BitPacker> public bool encodeOmega(ref BitPacker packer, uint val) { // Most of the time (94% in a few tests) val was 8 bits or less. if (val < OmegaCodeTableSize) return packer.encodeSmall(mOmegaCodeTable[val].mCode, mOmegaCodeTable[val].mLen); int len; UInt64 code = getOmegaCode(out len, val); return packer.encode(code, len); }
public bool DecompressFrame(BTimelineSampleCodec decomp, ref BRetiredProfileSample[] decodedSamplesBuffer) { BProfileSection[] sections = mSections; if (mCompressedFrame == null) { return(false); } ICoderBuffer buffer = mCompressedFrame.GetBuffer(); decomp.resetModel(); buffer.setPos(0); BitPacker bitPacker = new BitPacker(buffer); bool success = bitPacker.decodeStart(); BRetiredProfileSample decodedSample; int i; try { for (i = 0; i < mNumSamples; i++) { decodedSample = decodedSamplesBuffer[i]; success = decomp.decodeSample(bitPacker, ref decodedSample, sections); if (success == false) { ErrorHandler.Error(String.Format("Error decompressing sample {0} from frame# {1}", i, this.mFrameNumber)); // return false; } ProcessSample(decodedSample, i); } mDutyCycle.BuildDutyCycle(ref decodedSamplesBuffer, mNumSamples, false); mDutyCycle.mStartTime = mStartTime; if (this.mbHasGPUSamples == true) { mGpuDutyCycle.BuildDutyCycle(ref decodedSamplesBuffer, mNumSamples, true); mGpuDutyCycle.mStartTime = mStartTime; } mSamples = decodedSamplesBuffer; } catch (System.Exception ex) { ErrorHandler.Error(ex.ToString()); return(false); } return(true); }
//template<typename BitPacker> public bool decodeOmegaSigned(ref BitPacker packer, out int Value) { Value = 0; uint signFlag; if (!packer.decode(out signFlag, 1)) return false; uint uvalue; if (!decodeOmega(ref packer, out uvalue)) return false; Value = (int)uvalue; if (signFlag != 0) Value = -Value - 1; return true; }
//template<typename BitPacker> public bool decodeOmega(ref BitPacker packer, out uint Value) { // if (!(packer.decodeLookahead(1) != 0)) // { // Value = 0; // return packer.decodeRemoveBits(1); // } uint lookaheadTableOfs = packer.decodeLookahead(LookaheadTableBits); if (-1 != mLookaheadTable[lookaheadTableOfs].mCodeLen) { packer.decodeRemoveBits(mLookaheadTable[lookaheadTableOfs].mCodeLen); Value = mLookaheadTable[lookaheadTableOfs].mCodeVal; return true; } //////////////////// Value = 0; uint n; if (!packer.decode(out n, 2)) return false; n = mReverseByteTable[n << 6]; while (packer.decodeLookahead(1) != 0) { /*const*/ int len = (int)(n + 1); if (!packer.decode(out n, len)) return false; n = reverseDWORD(n << (32 - len)); } BDEBUG_ASSERT(n != 0); Value = n - 1; return packer.decodeRemoveBits(1); }
// false on failure //template<typename BitPackerType> public bool decodeSample(BitPacker bitPacker, ref BRetiredProfileSample decodedSample, /*const*/ /*BAlignedArray<const BProfileSection*>&*/ BProfileSection[] sections) { if (sections == null) { ErrorHandler.Error("Can't decompress frame, sections == null!"); return(false); } uint sectionID; if (-1 == mPrevLevel) { if (!mCoder.decodeOmega(ref bitPacker, out sectionID)) { return(false); } } else { int sectionIDDelta; if (!mCoder.decodeOmegaSigned(ref bitPacker, out sectionIDDelta)) { return(false); } sectionID = /*static_cast<BSectionID>*/ (ushort)(sectionIDDelta + mSectionPredDataArray[mPrevSectionID].mNextSectionID); } decodedSample.mSectionID = /*static_cast<BSectionID>*/ (ushort)(sectionID); // Sanity check, we should never have more than a few hundred sections! if (sectionID >= 512) { return(false); } if (sectionID >= size(mSectionPredDataArray)) { resize(ref mSectionPredDataArray, (int)sectionID + 100);//the current resize operation is slow... } // if this fails the passed in section array wasn't completely up to date if (sectionID >= size(sections)) { return(false); } bool cpuOnly = false; if (sections[sectionID] != null) { /*const*/ cpuOnly = sections[sectionID].cpuOnly(); } uint /*BCoderTime*/ cpuStartTime = 0; uint /*BCoderTime*/ cpuEndTime; uint /*BCoderTime*/ cpuDuration; uint /*BCoderTime*/ gpuStartTime = 0; uint /*BCoderTime*/ gpuEndTime = 0; uint /*BCoderTime*/ gpuDuration = 0; if (-1 == mPrevLevel) { uint level; if (!mCoder.decodeOmega(ref bitPacker, out level)) { return(false); } decodedSample.mLevel = /*static_cast<uchar>*/ (byte)(level); // sanity check if (level >= 128) { return(false); } if (!mCoder.decodeOmega(ref bitPacker, out decodedSample.mUserID)) { return(false); } if (!bitPacker.decode(out cpuStartTime, 32)) { return(false); } if (!bitPacker.decode(out cpuEndTime, 32)) { return(false); } if (!cpuOnly) { if (!bitPacker.decode(out gpuStartTime, 32)) { return(false); } if (!bitPacker.decode(out gpuEndTime, 32)) { return(false); } } cpuDuration = cpuEndTime - cpuStartTime; gpuDuration = gpuEndTime - gpuStartTime; } else { int levelDelta; if (!mCoder.decodeOmegaSigned(ref bitPacker, out levelDelta)) { return(false); } decodedSample.mLevel = /*static_cast<uchar>*/ (byte)(levelDelta + mPrevLevel); // Decode user ID - almost always 0, so read a single bit to determine if we need to omega decode the delta. int userIDDelta = 0; uint userIDDeltaNonZeroFlag; if (!bitPacker.decode(out userIDDeltaNonZeroFlag, 1)) { return(false); } if (userIDDeltaNonZeroFlag > 0) { if (!mCoder.decodeOmegaSigned(ref bitPacker, out userIDDelta)) { return(false); } } decodedSample.mUserID = (uint)(userIDDelta + mPrevUserID); // Decode the CPU start time. uint predStartCPUTime = mPrevCPUEndTime; if (decodedSample.mLevel > mPrevLevel) { predStartCPUTime = mPrevCPUStartTime; } uint deltaStartCPUTime; if (!mCoder.decodeOmega(ref bitPacker, out deltaStartCPUTime)) { return(false); } cpuStartTime = deltaStartCPUTime + mPrevCPUStartTime; // Decode the CPU duration. int deltaCPUDuration; if (!mCoder.decodeOmegaSigned(ref bitPacker, out deltaCPUDuration)) { return(false); } cpuDuration = (uint)(deltaCPUDuration + mSectionPredDataArray[sectionID].mPrevCPUDuration); cpuEndTime = cpuStartTime + cpuDuration; if (!cpuOnly) { // Decode the GPU start time. uint predStartGPUTime = mPrevGPUEndTime; if (decodedSample.mLevel > mPrevLevel) { predStartGPUTime = mPrevGPUStartTime; } uint deltaStartGPUTime; if (!mCoder.decodeOmega(ref bitPacker, out deltaStartGPUTime)) { return(false); } gpuStartTime = deltaStartGPUTime + mPrevGPUStartTime; // Decode the GPU end time. int deltaGPUDuration; if (!mCoder.decodeOmegaSigned(ref bitPacker, out deltaGPUDuration)) { return(false); } gpuDuration = (uint)(deltaGPUDuration + mSectionPredDataArray[sectionID].mPrevGPUDuration); gpuEndTime = gpuStartTime + gpuDuration; } } // Update the model - only record those things we need for speed mSectionPredDataArray[sectionID].mPrevCPUDuration = cpuDuration; mSectionPredDataArray[sectionID].mPrevGPUDuration = gpuDuration; mSectionPredDataArray[mPrevSectionID].mNextSectionID = sectionID; mPrevLevel = decodedSample.mLevel; mPrevUserID = decodedSample.mUserID; mPrevCPUStartTime = cpuStartTime; mPrevCPUEndTime = cpuEndTime; mPrevGPUStartTime = gpuStartTime; mPrevGPUEndTime = gpuEndTime; mPrevSectionID = sectionID; decodedSample.mCPUStartTime = cpuStartTime; decodedSample.mCPUEndTime = cpuEndTime; decodedSample.mGPUStartTime = 0; decodedSample.mGPUEndTime = 0; if (!cpuOnly) { decodedSample.mGPUStartTime = gpuStartTime; decodedSample.mGPUEndTime = gpuEndTime; } return(true); }
// false on failure //template<typename BitPackerType> public bool codeSample(BitPacker bitPacker, /*const*/ ref BRetiredProfileSample sample, /*const*/ ref BProfileSection pSection) { if (sample.mSectionID >= size(mSectionPredDataArray)) { resize(ref mSectionPredDataArray, sample.mSectionID + 1); } /*const*/ uint /*BCoderTime*/ cpuStartTime = sample.mCPUStartTime; /*const*/ uint /*BCoderTime*/ cpuEndTime = sample.mCPUEndTime; uint /*BCoderTime*/ gpuStartTime = 0; uint /*BCoderTime*/ gpuEndTime = 0; if (!pSection.cpuOnly()) { gpuStartTime = sample.mGPUStartTime; gpuEndTime = sample.mGPUEndTime; } uint /*BCoderTime*/ cpuDuration = cpuEndTime - cpuStartTime; uint /*BCoderTime*/ gpuDuration = gpuEndTime - gpuStartTime; if (-1 == mPrevLevel) { if (!mCoder.encodeOmega(ref bitPacker, sample.mSectionID)) { return(false); } // Model is empty - send uncompressed sample if (!mCoder.encodeOmega(ref bitPacker, sample.mLevel)) { return(false); } if (!mCoder.encodeOmega(ref bitPacker, sample.mUserID)) { return(false); } if (!bitPacker.encode(cpuStartTime, 32)) { return(false); } if (!bitPacker.encode(cpuEndTime, 32)) { return(false); } if (!pSection.cpuOnly()) { if (!bitPacker.encode(gpuStartTime, 32)) { return(false); } if (!bitPacker.encode(gpuEndTime, 32)) { return(false); } } } else { // Compress sample // Code the section ID delta int sectionIDDelta = (int)(sample.mSectionID - mSectionPredDataArray[mPrevSectionID].mNextSectionID); if (!mCoder.encodeOmegaSigned(ref bitPacker, sectionIDDelta)) { return(false); } // Code the level delta using 1 or 2 bits [-1, 0, or 1] /*const*/ int levelDelta = sample.mLevel - mPrevLevel; if (!mCoder.encodeOmegaSigned(ref bitPacker, levelDelta)) { return(false); } /*const*/ int userIDDelta = (int)(sample.mUserID - mPrevUserID); if (userIDDelta == 0) { // Encode single bit to represent userIDDelta == 0. if (!bitPacker.encode((uint)(0), (int)(1))) { return(false); } } else { if (!bitPacker.encode((uint)(1), (int)(1))) { return(false); } if (!mCoder.encodeOmegaSigned(ref bitPacker, userIDDelta)) { return(false); } } uint predStartCPUTime = mPrevCPUEndTime; if (sample.mLevel > mPrevLevel) { predStartCPUTime = mPrevCPUStartTime; } //BDEBUG_ASSERT(predStartCPUTime <= cpuStartTime); /*const*/ uint deltaStartCPUTime = cpuStartTime - mPrevCPUStartTime; if (!mCoder.encodeOmega(ref bitPacker, deltaStartCPUTime)) { return(false); } /*const*/ int deltaCPUDuration = (int)(cpuDuration - mSectionPredDataArray[sample.mSectionID].mPrevCPUDuration); if (!mCoder.encodeOmegaSigned(ref bitPacker, deltaCPUDuration)) { return(false); } if (!pSection.cpuOnly()) { uint predStartGPUTime = mPrevGPUEndTime; if (sample.mLevel > mPrevLevel) { predStartGPUTime = mPrevGPUStartTime; } if (!sample.hasGPUTimes()) { // this should be very rare-- only when the gpu samples time out // we don't want to try encoding a negative start gpu time delta // instead, ensure the gpu times are both equal, which means the sample is invalid gpuStartTime = predStartGPUTime; gpuEndTime = predStartGPUTime; gpuDuration = 0; } //BDEBUG_ASSERT(predStartGPUTime <= gpuStartTime); /*const*/ uint deltaStartGPUTime = gpuStartTime - mPrevGPUStartTime; if (!mCoder.encodeOmega(ref bitPacker, deltaStartGPUTime)) { return(false); } /*const*/ int deltaGPUDuration = (int)(gpuDuration - mSectionPredDataArray[sample.mSectionID].mPrevGPUDuration); if (!mCoder.encodeOmegaSigned(ref bitPacker, deltaGPUDuration)) { return(false); } } } // Update the model - only record those things we need for speed mSectionPredDataArray[sample.mSectionID].mPrevCPUDuration = cpuDuration; mSectionPredDataArray[sample.mSectionID].mPrevGPUDuration = gpuDuration; mSectionPredDataArray[mPrevSectionID].mNextSectionID = sample.mSectionID; mPrevLevel = sample.mLevel; mPrevUserID = sample.mUserID; mPrevSectionID = sample.mSectionID; mPrevCPUStartTime = cpuStartTime; mPrevCPUEndTime = cpuEndTime; mPrevGPUStartTime = gpuStartTime; mPrevGPUEndTime = gpuEndTime; return(true); }