File: OniDataRecords.cpp

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 22,232 kB
  • sloc: cpp: 111,183; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (454 lines) | stat: -rw-r--r-- 15,774 bytes parent folder | download | duplicates (4)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*****************************************************************************
*                                                                            *
*  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.                                            *
*                                                                            *
*****************************************************************************/
#include "OniDataRecords.h"

ONI_NAMESPACE_IMPLEMENTATION_BEGIN

// NOTE: XnLib does not define UINT*_C like macros for some reason...
#define XN_UINT64_C(x) ((x) + (XN_MAX_UINT64 - XN_MAX_UINT64))
#define XN_UINT32_C(x) ((x) + (XN_MAX_UINT32 - XN_MAX_UINT32))

namespace {

// Converts NodeType into a string.
const XnChar* AsString(XnUInt32 nodeType)
{
    switch (nodeType) {
    case NODE_TYPE_DEVICE: return "Device";
    case NODE_TYPE_DEPTH:  return "Depth";
    case NODE_TYPE_IMAGE:  return "Image";
    case NODE_TYPE_IR:     return "IR";
    default:
        ;
    }
    return "Invalid";
}

} // namespace

RecordAssembler::RecordAssembler()
        : m_pBuffer(NULL),
          m_bufferSize_bytes(0),
          m_pEmitPtr(NULL)
{

}

RecordAssembler::~RecordAssembler()
{
    if (NULL != m_pBuffer)
    {
        XN_DELETE_ARR(m_pBuffer);
    }
}

void RecordAssembler::initialize()
{
    XnSizeT maxHeaderSize_bytes = 
        /* size of header POD   = */ sizeof(RecordHeaderData) + 
        /* size of node name    = */ (ONI_MAX_STR + 1) +
        /* size of time stamp   = */ sizeof(XnUInt64) +
        /* size of frame number = */ sizeof(XnUInt32);

    m_bufferSize_bytes = (XnSizeT)(maxHeaderSize_bytes +
        /* max video mode width (pixels)  = */ 1600 *
        /* max video mode height (pixels) = */ 1200 *
        /* max video mode bits per pixel  = */ 3    *
        /* worst case compression rate    = */ 1.2);

    m_pEmitPtr = m_pBuffer = XN_NEW_ARR(XnUInt8, m_bufferSize_bytes);
    if (NULL == m_pBuffer)
    {
        m_bufferSize_bytes = 0;
    } 
}

OniStatus RecordAssembler::serialize(XN_FILE_HANDLE file)
{
    // NOTE(oleksii): strange, but fieldsSize includes the size of header as
    // well...
    XnUInt32 serializedSize_bytes = 
        m_header->fieldsSize +
        m_header->payloadSize;
    XnStatus status = xnOSWriteFile(file, m_pBuffer, serializedSize_bytes);
    return XN_STATUS_OK == status ? ONI_STATUS_OK : ONI_STATUS_ERROR;
}

// A handy macro that helps to reduce code duplicate and level up readability.
#define MUST_BE_INITIALIZED(...) \
    if (0 == m_bufferSize_bytes) { return __VA_ARGS__; } else {}

void RecordAssembler::emitCommonHeader(XnUInt32 recordType, XnUInt32 nodeId, XnUInt64 undoRecordPos)
{
    MUST_BE_INITIALIZED()
    xnOSMemSet(m_header, 0, sizeof(*m_header));
    // Reads as: "NIR\0"
    m_header->magic        = 0x0052494E;
    m_header->recordType   = recordType;
    m_header->nodeId       = nodeId;
    m_header->fieldsSize   = sizeof(*m_header);
    m_header->payloadSize  = XN_UINT32_C(0);
    m_header->undoRecordPos = undoRecordPos;
    // Reset emit pointer.
    m_pEmitPtr = m_pBuffer + sizeof(*m_header);
}

OniStatus RecordAssembler::emitString(const XnChar* pStr, XnSizeT& totalFieldsSize_bytes)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)
    if (NULL == pStr)
    {
        return ONI_STATUS_BAD_PARAMETER;
    }

    // Initialize field's data.
    struct
    {
        XnUInt32 dataSize;
        XnChar   data[ONI_MAX_STR];  
    } field = { 0 };
    xnOSStrCopy(field.data, pStr, sizeof(field.data));
    field.dataSize = XN_MIN(xnOSStrLen(pStr) + 1, sizeof(field.data));
    field.data[sizeof(field.data) - 1] = '\0';

    // Emit field's data.
    XnSizeT fieldSize_bytes = field.dataSize + sizeof(field.dataSize);
    OniStatus status = emitData(&field, fieldSize_bytes);
    if (ONI_STATUS_OK == status)
    {
        totalFieldsSize_bytes += fieldSize_bytes;
    }

    return status;
}

OniStatus RecordAssembler::emitData(const void* pData, XnSizeT dataSize_bytes)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)
    xnOSMemCopy(m_pEmitPtr, pData, dataSize_bytes);
    m_pEmitPtr += dataSize_bytes;
    return ONI_STATUS_OK;
}

template<typename T>
OniStatus RecordAssembler::emit(const T& field, XnSizeT& totalFieldsSize_bytes)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)
    XnSizeT fieldSize_bytes = sizeof(field);
    OniStatus status = emitData(&field, fieldSize_bytes);
    if (ONI_STATUS_OK == status)
    {
        totalFieldsSize_bytes += fieldSize_bytes;
    }
    return status;
}

OniStatus RecordAssembler::emit_RECORD_NODE_ADDED_1_0_0_5(
        XnUInt32 nodeType, 
        XnUInt32 nodeId,
        XnUInt32 codecId,
        XnUInt32 numberOfFrames,
        XnUInt64 minTimeStamp,
        XnUInt64 maxTimeStamp)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    // NOTE: fields size will be modified by EmitString, and Emit calls.
    emitCommonHeader(RECORD_NODE_ADDED_1_0_0_5, nodeId, /*undoRecordPos*/ 0);
    
    // Fields of v. 1.0.0.5 record (size & offset are in bytes [decimal notation]):
    // Size     Offset      Fields                    Notes 
    // ====     ======      ======                    =====
    //           0          +-------------------+
    //   4              +-->| Size of Node Name |\                                /
    //           4      |   +-------------------+ |-- emitted by EmitString
    //                  |   | Data of Node Name |/
    //       Y = 4 + X -+   +-------------------+
    //   4                  | Node Type         |
    //           Y + 4      +-------------------+
    //   4                  | Codec ID          |
    //           Y + 8      +-------------------+
    //   4                  | Number of Frames  |
    //           Y + 12     +-------------------+
    //   8                  | Min Time Stamp    |
    //           Y + 20     +-------------------+
    //   8                  | Max Time Stamp    |
    //           Y + 28     +-------------------+
	XnSizeT fieldsSize = m_header->fieldsSize;
    emitString(AsString(nodeType),  fieldsSize);
    emit(nodeType,                  fieldsSize);
    emit(codecId,                   fieldsSize);
    emit(numberOfFrames,            fieldsSize);
    emit(minTimeStamp,              fieldsSize);
    emit(maxTimeStamp,              fieldsSize);

	m_header->fieldsSize = (XnUInt32)fieldsSize;

    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_NODE_ADDED(
        XnUInt32 nodeType, 
        XnUInt32 nodeId,
        XnUInt32 codecId,
        XnUInt32 numberOfFrames,
        XnUInt64 minTimeStamp,
        XnUInt64 maxTimeStamp,
        XnUInt64 seekTablePosition)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    OniStatus status = emit_RECORD_NODE_ADDED_1_0_0_5(
            nodeType,
            nodeId,
            codecId,
            numberOfFrames,
            minTimeStamp,
            maxTimeStamp);
    m_header->recordType = RECORD_NODE_ADDED;

    // NOTE(oleksii): I'm not sure if it's really 1.0.0.6 version...
    //
    // NOTE: offset is given related to v. 1.0.0.5 fields.
    //
    // Fields of v. 1.0.0.6 record (size & offset are in bytes [decimal notation]):
    // Size     Offset      Fields                    Notes 
    // ====     ======      ======                    =====
    //                      +---------------------+
    //                      | V. 1.0.0.5 Fields   |
    //           0          +---------------------+
    //   8                  | Seek table position |
    //           8          +---------------------+
	XnSizeT fieldsSize = m_header->fieldsSize;
    emit(seekTablePosition, fieldsSize);
	m_header->fieldsSize = (XnUInt32)fieldsSize;

    return status;
}

OniStatus RecordAssembler::emit_RECORD_NODE_STATE_READY(XnUInt32 nodeId)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)
    emitCommonHeader(RECORD_NODE_STATE_READY, nodeId, /*undoRecordPos*/ 0);
    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_NODE_REMOVED(XnUInt32 nodeId, XnUInt64 nodeAddedPos)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)
    emitCommonHeader(RECORD_NODE_REMOVED, nodeId, /*undoRecordPos*/ nodeAddedPos);
    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_SEEK_TABLE(XnUInt32 nodeId, XnUInt32 numFrames, DataIndexEntryList dataIndexEntryList)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    // no fields today :-)
    emitCommonHeader(RECORD_SEEK_TABLE, nodeId, /*undoRecordPos*/ 0);

    XnSizeT entrySize    = sizeof(DataIndexEntry);
    XnSizeT nPayloadSize = (numFrames + 1) * entrySize;

    // Verify that there's enough room for the payload in the buffer.
    XnSizeT roomLeft = m_bufferSize_bytes - size_t(m_pEmitPtr - m_pBuffer);
    if (roomLeft < nPayloadSize)
    {
        return ONI_STATUS_ERROR;
    }
    
    // Emit payload data.
    int nWritten  = 0;

    // start with an empty entry for frame 0 (frames start with 1)
    DataIndexEntry emptyEntry;
    xnOSMemSet(&emptyEntry, 0, entrySize);
    emitData(&emptyEntry, entrySize); ++nWritten;

    // now the table itself
    for (DataIndexEntryList::ConstIterator it = dataIndexEntryList.Begin(); it != dataIndexEntryList.End(); ++it)
    {
        emitData(&(*it), entrySize); ++nWritten;
    }

    XN_ASSERT((numFrames + 1) == XnUInt32(nWritten));

    m_header->payloadSize = (XnUInt32)nPayloadSize;

    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_END()
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)
    emitCommonHeader(RECORD_END, /* nodeId (ignored) = */ XN_UINT32_C(0), /*undoRecordPos=*/0);
    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_NODE_DATA_BEGIN(
        XnUInt32 nodeId,
        XnUInt32 framesCount,
        XnUInt64 maxTimeStamp)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    // NOTE: fields size will be modified by EmitString, and Emit calls.
    emitCommonHeader(RECORD_NODE_DATA_BEGIN, nodeId, /*undoRecordPos*/ 0);

    // Fields of DATA_BEGIN record (size & offset are in bytes [decimal notation]):
    // Size     Offset      Fields                    Notes 
    // ====     ======      ======                    =====
    //           0          +------------------+
    //   4                  | Frames Count     |
    //           4          +------------------+
    //   8                  | Max Time Stamp   |
    //           12         +------------------+
	XnSizeT fieldsSize = m_header->fieldsSize;
    emit(framesCount,  fieldsSize);
    emit(maxTimeStamp, fieldsSize);
	m_header->fieldsSize = (XnUInt32)fieldsSize;

    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_NEW_DATA(
        XnUInt32    nodeId,
       	XnUInt64    undoRecordPos,
        XnUInt64    timeStamp,
        XnUInt32    frameId,
        const void* data, 
        XnSizeT     dataSize_bytes)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    // NOTE: fields size will be modified by EmitString, and Emit calls.
    emitCommonHeader(RECORD_NEW_DATA, nodeId, undoRecordPos);

    // Fields of NEW_DATA record (size & offset are in bytes [decimal notation]):
    // Size     Offset      Fields                    Notes 
    // ====     ======      ======                    =====
    //           0          +---------------------+
    //   8                  | Time Stamp          |
    //           8          +---------------------+
    //   4                  | Seek table position |
    //           12         +---------------------+
	XnSizeT fieldsSize = m_header->fieldsSize;
    emit(timeStamp, fieldsSize);
    emit(frameId,   fieldsSize);
	m_header->fieldsSize = (XnUInt32)fieldsSize;

    // Verify that there's enough room for the payload in the buffer.
    XnSizeT roomLeft = m_bufferSize_bytes - size_t(m_pEmitPtr - m_pBuffer);
    if (roomLeft < dataSize_bytes)
    {
        return ONI_STATUS_ERROR;
    }
    
    // Emit payload data.
    emitData(data, dataSize_bytes);
    m_header->payloadSize = (XnUInt32)dataSize_bytes;

    return ONI_STATUS_OK;
}

OniStatus RecordAssembler::emit_RECORD_GENERAL_PROPERTY(
        XnUInt32    nodeId,
       	XnUInt64    undoRecordPos,
        const char* propertyName,
        const void* data,
        XnSizeT     dataSize_bytes)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    // NOTE: fields size will be modified by EmitString, and Emit calls.
    emitCommonHeader(RECORD_GENERAL_PROPERTY, nodeId, undoRecordPos);

    // XXX(oleksii): A very odd thing: though the file format states to be 64-bit
    // compatible, the size of general property field is 32-bit :-(
    //
    // Fields of GENERAL_PROPERTY record (size & offset are in bytes [decimal notation]):
    // Size     Offset      Fields                    Notes 
    // ====     ======      ======                    =====
    //           0          +-----------------------+
    //   4              +-->| Size of Property Name |\                                  /
    //           4      |   +-----------------------+ |-- emitted by EmitString
    //                  |   | Data of Property Name |/
    //       Y = 4 + X -+   +-----------------------+
    //   4               +->| Size of Property Data |
    //           Y + 4   |  +-----------------------+
    //                   |  | Data                  |
    //       Z = Y + 4 + W  +-----------------------+
	XnSizeT fieldsSize = m_header->fieldsSize;
    emitString(propertyName,       fieldsSize);
    emit(XnUInt32(dataSize_bytes), fieldsSize);
	m_header->fieldsSize = (XnUInt32)fieldsSize;

    OniStatus status = emitData(data, dataSize_bytes);
    if (ONI_STATUS_OK == status)
    {
        m_header->fieldsSize += (XnUInt32)dataSize_bytes;
    }
    return status;
}

OniStatus RecordAssembler::emit_RECORD_INT_PROPERTY(
        XnUInt32    nodeId,
        XnUInt64    undoRecordPos,
        const char* propertyName,
        XnUInt64    data)
{
    MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

    OniStatus status = emit_RECORD_GENERAL_PROPERTY(
            nodeId,
            undoRecordPos,    
            propertyName,
            &data,
            sizeof(data));
    if (ONI_STATUS_OK == status)
    {
        m_header->recordType = RECORD_INT_PROPERTY;
    }
    return status;
}

OniStatus RecordAssembler::emit_RECORD_REAL_PROPERTY(
	XnUInt32    nodeId,
	XnUInt64    undoRecordPos,
	const char* propertyName,
	XnDouble    data)
{
	MUST_BE_INITIALIZED(ONI_STATUS_ERROR)

		OniStatus status = emit_RECORD_GENERAL_PROPERTY(
		nodeId,
		undoRecordPos,    
		propertyName,
		&data,
		sizeof(data));
	if (ONI_STATUS_OK == status)
	{
		m_header->recordType = RECORD_REAL_PROPERTY;
	}
	return status;
}

ONI_NAMESPACE_IMPLEMENTATION_END