-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
229 lines (183 loc) · 5.52 KB
/
util.h
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
/*
Original code by Mike Cancilla (https://github.com/mikecancilla)
2019
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once
#include <string>
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <cstdarg>
#include <cassert>
namespace util
{
inline uint16_t read2Bytes(uint8_t* p)
{
uint16_t ret = *p++;
ret <<= 8;
ret |= *p++;
return ret;
}
inline uint32_t read3Bytes(uint8_t* p)
{
uint32_t ret = 0;
uint32_t val = *p++;
ret |= val << 16;
val = *p++;
ret |= val << 8;
ret |= *p;
return ret;
}
inline uint32_t read4Bytes(uint8_t* p)
{
uint32_t ret = 0;
uint32_t val = *p++;
ret = val << 24;
val = *p++;
ret |= val << 16;
val = *p++;
ret |= val << 8;
ret |= *p;
return ret;
}
inline size_t incrementPtr(uint8_t*& p, size_t bytes)
{
p += bytes;
return bytes;
}
// Search for 0x00 00 01
size_t inline nextStartCode(uint8_t*& p, size_t dataLength = -1)
{
size_t count = 0;
uint8_t* pStart = p;
while (*p != 0 ||
*(p + 1) != 0 ||
*(p + 2) != 1 &&
(count < dataLength))
{
incrementPtr(p, 1);
count++;
}
if (0 == count)
return count;
return p - pStart;
}
// Search for 0x00 00 00 01
size_t inline nextNaluStartCode(uint8_t*& p, size_t dataLength = -1)
{
size_t count = 0;
uint8_t* pStart = p;
while (*p != 0 ||
*(p + 1) != 0 ||
*(p + 2) != 0 ||
*(p + 3) != 1 &&
count < dataLength)
{
incrementPtr(p, 1);
count++;
}
if (0 == count)
return count;
return p - pStart;
}
inline size_t skipToNextStartCode(uint8_t*& p)
{
uint8_t* pStart = p;
uint32_t fourBytes = read4Bytes(p);
incrementPtr(p, 4);
nextStartCode(p);
return p - pStart;
}
inline size_t validateStartCode(uint8_t*& p, uint32_t startCode)
{
uint32_t fourBytes = read4Bytes(p);
incrementPtr(p, 4);
uint32_t start_code_prefix = (fourBytes & 0xFFFFFF00) >> 8;
assert(0x000001 == start_code_prefix);
fourBytes &= 0x000000FF;
assert(fourBytes == startCode);
return 4;
}
static bool g_bXmlOut = false;
void inline setXmlOutput(bool tf)
{
g_bXmlOut = tf;
}
void inline printfXml(unsigned int indentLevel, const char* format, ...)
{
if (g_bXmlOut && format)
{
char outputBuffer[512] = "";
// See here: https://en.cppreference.com/w/cpp/io/c/vfprintf
for (unsigned int i = 0; i < indentLevel; i++)
strncpy(outputBuffer + (i * 2), " ", 2);
va_list arg_list;
va_start(arg_list, format);
vsnprintf(outputBuffer + (indentLevel * 2),
512,
format,
arg_list);
va_end(arg_list);
printf("%s", outputBuffer);
}
}
/*
// Singleton
class XmlOutput
{
private:
static XmlOutput* pInstance;
bool m_bXmlOutput = false;
XmlOutput() {}
public:
XmlOutput(const XmlOutput& obj) = delete; // No copy constructor
static XmlOutput* getInstance()
{
if (nullptr == pInstance)
{
pInstance = new XmlOutput;
return pInstance;
}
else
{
return pInstance;
}
}
void setOutput(bool bXmlOutput)
{
m_bXmlOutput = bXmlOutput;
}
void inline printfXml(unsigned int indentLevel, const char* format, ...)
{
if (m_bXmlOutput && format)
{
char outputBuffer[512] = "";
for (unsigned int i = 0; i < indentLevel; i++)
strcat_s(outputBuffer, sizeof(outputBuffer), " ");
va_list arg_list;
va_start(arg_list, format);
vsprintf_s(outputBuffer + (indentLevel * 2),
sizeof(outputBuffer) - (indentLevel * 2),
format,
arg_list);
va_end(arg_list);
printf(outputBuffer);
}
}
};
*/
}