1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*****************************************************************************
* *
* OpenNI 2.x Alpha *
* Copyright (C) 2012 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnPSCompressedDepthProcessor.h"
#include <XnProfiling.h>
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XnPSCompressedDepthProcessor::XnPSCompressedDepthProcessor(XnSensorDepthStream* pStream, XnSensorStreamHelper* pHelper, XnFrameBufferManager* pBufferManager) :
XnDepthProcessor(pStream, pHelper, pBufferManager)
{
}
XnStatus XnPSCompressedDepthProcessor::Init()
{
XnStatus nRetVal = XN_STATUS_OK;
nRetVal = XnDepthProcessor::Init();
XN_IS_STATUS_OK(nRetVal);
XN_VALIDATE_BUFFER_ALLOCATE(m_RawData, GetExpectedOutputSize());
return XN_STATUS_OK;
}
XnPSCompressedDepthProcessor::~XnPSCompressedDepthProcessor()
{
}
#define XN_CHECK_UNC_DEPTH_OUTPUT(x, y, z) \
if (x >= y) \
{ \
return (XN_STATUS_OUTPUT_BUFFER_OVERFLOW); \
} \
if (z >= XN_DEVICE_SENSOR_MAX_SHIFT_VALUE) \
{ \
z = XN_DEVICE_SENSOR_NO_DEPTH_VALUE; \
}
#define XN_DEPTH_OUTPUT(pDepthOutput, pOutputEnd, nValue) \
XN_CHECK_UNC_DEPTH_OUTPUT(pDepthOutput, pOutputEnd, nValue) \
*pDepthOutput = GetOutput(nValue); \
++pDepthOutput;
#define INIT_INPUT(pInput, nInputSize) \
const XnUInt8* __pInputOrig = pInput; \
const XnUInt8* __pCurrInput = pInput; \
const XnUInt8* __pInputEnd = pInput + nInputSize; \
XnBool __bShouldReadByte = TRUE; \
XnUInt32 __nLastByte = 0;
#define GET_NEXT_INPUT(nInput) \
if (__bShouldReadByte) \
{ \
if (__pCurrInput == __pInputEnd) \
break; \
\
/* read from input */ \
__nLastByte = *__pCurrInput; \
__bShouldReadByte = FALSE; \
\
/* take high 4-bits */ \
nInput = __nLastByte >> 4; \
\
__pCurrInput++; \
} \
else \
{ \
/* byte already read. take its low 4-bits */ \
nInput = __nLastByte & 0x0F; \
__bShouldReadByte = TRUE; \
}
/** True if input is in a steady state (not in the middle of a byte) */
#define CAN_INPUT_STOP_HERE __bShouldReadByte
/** Gets a pointer to n elements before current input */
#define GET_PREV_INPUT(n) __pCurrInput - n/2;
#define GET_INPUT_READ_BYTES (__pCurrInput - __pInputOrig);
XnStatus XnPSCompressedDepthProcessor::UncompressDepthPS(const XnUInt8* pInput, const XnUInt32 nInputSize,
XnUInt16* pDepthOutput, XnUInt32* pnOutputSize,
XnUInt32* pnActualRead, XnBool bLastPart)
{
// Input is made of 4-bit elements.
INIT_INPUT(pInput, nInputSize);
XnUInt16* pOutputEnd = pDepthOutput + (*pnOutputSize / sizeof(OniDepthPixel));
XnUInt16 nLastValue = 0;
const XnUInt8* pInputOrig = pInput;
XnUInt16* pOutputOrig = pDepthOutput;
const XnUInt8* pInputLastPossibleStop = pInputOrig;
XnUInt16* pOutputLastPossibleStop = pOutputOrig;
// NOTE: we use variables of type uint32 instead of uint8 as an optimization (better CPU usage)
XnUInt32 nInput;
XnUInt32 nLargeValue;
XnBool bCanStop;
for (;;)
{
bCanStop = CAN_INPUT_STOP_HERE;
GET_NEXT_INPUT(nInput);
switch (nInput)
{
case 0xd: // Dummy.
// Do nothing
break;
case 0xe: // RLE
// read count
GET_NEXT_INPUT(nInput);
// should repeat last value (nInput + 1) times
nInput++;
while (nInput != 0)
{
XN_DEPTH_OUTPUT(pDepthOutput, pOutputEnd, nLastValue);
--nInput;
}
break;
case 0xf: // Full (or large)
// read next element
GET_NEXT_INPUT(nInput);
// First bit tells us if it's a large diff (turned on) or a full value (turned off)
if (nInput & 0x8) // large diff (7-bit)
{
// turn off high bit, and shift left
nLargeValue = (nInput - 0x8) << 4;
// read low 4-bits
GET_NEXT_INPUT(nInput);
nLargeValue |= nInput;
// diff values are from -64 to 63 (0x00 to 0x7f)
nLastValue += ((XnInt16)nLargeValue - 64);
}
else // Full value (15-bit)
{
if (bCanStop)
{
// We can stop here. First input is a full value
pInputLastPossibleStop = GET_PREV_INPUT(2);
pOutputLastPossibleStop = pDepthOutput;
}
nLargeValue = (nInput << 12);
// read 3 more elements
GET_NEXT_INPUT(nInput);
nLargeValue |= nInput << 8;
GET_NEXT_INPUT(nInput);
nLargeValue |= nInput << 4;
GET_NEXT_INPUT(nInput);
nLastValue = (XnUInt16)(nLargeValue | nInput);
}
XN_DEPTH_OUTPUT(pDepthOutput, pOutputEnd, nLastValue);
break;
default: // all rest (smaller than 0xd) are diffs
// diff values are from -6 to 6 (0x0 to 0xc)
nLastValue += ((XnInt16)nInput - 6);
XN_DEPTH_OUTPUT(pDepthOutput, pOutputEnd, nLastValue);
}
}
if (bLastPart == TRUE)
{
*pnOutputSize = (XnUInt32)(pDepthOutput - pOutputOrig) * sizeof(XnUInt16);
*pnActualRead = (XnUInt32)GET_INPUT_READ_BYTES;
}
else
{
*pnOutputSize = (XnUInt32)(pOutputLastPossibleStop - pOutputOrig) * sizeof(XnUInt16);
*pnActualRead = (XnUInt32)(pInputLastPossibleStop - pInputOrig) * sizeof(XnUInt8);
}
// All is good...
return (XN_STATUS_OK);
}
void XnPSCompressedDepthProcessor::ProcessFramePacketChunk(const XnSensorProtocolResponseHeader* pHeader, const XnUChar* pData, XnUInt32 nDataOffset, XnUInt32 nDataSize)
{
XN_PROFILING_START_SECTION("XnPSCompressedDepthProcessor::ProcessFramePacketChunk")
XnBuffer* pWriteBuffer = GetWriteBuffer();
const XnUChar* pBuf = NULL;
XnUInt32 nBufSize = 0;
// check if we have bytes stored from previous calls
if (m_RawData.GetSize() > 0)
{
// we have no choice. We need to append current buffer to previous bytes
if (m_RawData.GetFreeSpaceInBuffer() < nDataSize)
{
xnLogWarning(XN_MASK_SENSOR_PROTOCOL_DEPTH, "Bad overflow depth! %d", m_RawData.GetSize());
FrameIsCorrupted();
}
else
{
m_RawData.UnsafeWrite(pData, nDataSize);
}
pBuf = m_RawData.GetData();
nBufSize = m_RawData.GetSize();
}
else
{
// we can process the data directly
pBuf = pData;
nBufSize = nDataSize;
}
XnUInt32 nOutputSize = pWriteBuffer->GetFreeSpaceInBuffer();
XnUInt32 nWrittenOutput = nOutputSize;
XnUInt32 nActualRead = 0;
XnBool bLastPart = pHeader->nType == XN_SENSOR_PROTOCOL_RESPONSE_DEPTH_END && (nDataOffset + nDataSize) == pHeader->nBufSize;
XnStatus nRetVal = UncompressDepthPS(pBuf, nBufSize, (XnUInt16*)pWriteBuffer->GetUnsafeWritePointer(),
&nWrittenOutput, &nActualRead, bLastPart);
if (nRetVal != XN_STATUS_OK)
{
FrameIsCorrupted();
static XnUInt64 nLastPrinted = 0;
XnUInt64 nCurrTime;
xnOSGetTimeStamp(&nCurrTime);
if (nOutputSize != 0 || (nCurrTime - nLastPrinted) > 1000)
{
xnLogWarning(XN_MASK_SENSOR_PROTOCOL_DEPTH, "Uncompress depth failed: %s. Input Size: %u, Output Space: %u, Last Part: %d.", xnGetStatusString(nRetVal), nBufSize, nOutputSize, bLastPart);
xnOSGetTimeStamp(&nLastPrinted);
}
}
pWriteBuffer->UnsafeUpdateSize(nWrittenOutput);
nBufSize -= nActualRead;
m_RawData.Reset();
// if we have any bytes left, keep them for next time
if (nBufSize > 0)
{
pBuf += nActualRead;
m_RawData.UnsafeWrite(pBuf, nBufSize);
}
XN_PROFILING_END_SECTION
}
void XnPSCompressedDepthProcessor::OnStartOfFrame(const XnSensorProtocolResponseHeader* pHeader)
{
XnDepthProcessor::OnStartOfFrame(pHeader);
m_RawData.Reset();
}
void XnPSCompressedDepthProcessor::OnEndOfFrame(const XnSensorProtocolResponseHeader* pHeader)
{
XnDepthProcessor::OnEndOfFrame(pHeader);
m_RawData.Reset();
}
|