-
Notifications
You must be signed in to change notification settings - Fork 6
/
avlib.cpp
executable file
·265 lines (225 loc) · 5.11 KB
/
avlib.cpp
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
/**
* @file avlib.hpp
* @brief Common admin methods for the AV Library
*
* @copyright Copyright 2015 Samir Sinha. All rights reserved.
* @license This project is released under the ISC license. See LICENSE
* for the full text.
*/
#include "avlib.hpp"
#include <cstdlib>
namespace cinekav {
static void* DefaultMalloc(void*, int, size_t sz)
{
return malloc(sz);
}
static void DefaultFree(void*, int, void* ptr)
{
free(ptr);
}
static AllocFn gAllocFn = &DefaultMalloc;
static FreeFn gFreeFn = &DefaultFree;
static void* gMemoryContext = nullptr;
void* Memory::allocate(size_t sz)
{
return gAllocFn(gMemoryContext, _region, sz);
}
void Memory::free(void* ptr)
{
gFreeFn(gMemoryContext, _region, ptr);
}
void initialize(AllocFn allocFn, FreeFn freeFn, void* context)
{
gAllocFn = allocFn;
gFreeFn = freeFn;
gMemoryContext = context;
}
////////////////////////////////////////////////////////////////////////////////
Buffer::Buffer(const Memory& memory) :
_memory(memory),
_buffer(nullptr),
_head(nullptr),
_tail(nullptr),
_limit(nullptr),
_overflow(false),
_owned(false)
{
}
Buffer::Buffer(int sz, const Memory& memory) :
_memory(memory),
_buffer(nullptr),
_head(nullptr),
_tail(nullptr),
_limit(nullptr),
_overflow(false),
_owned(true)
{
if (_owned)
{
_buffer = reinterpret_cast<uint8_t*>(_memory.allocate(sz));
if (_buffer)
{
_limit = _buffer + sz;
}
_head = _buffer;
_tail = _head;
}
}
Buffer::Buffer(uint8_t* buffer, int sz) :
_buffer(buffer),
_head(_buffer),
_tail(_buffer + sz),
_limit(_tail),
_overflow(false),
_owned(false)
{
}
Buffer::Buffer(uint8_t* buffer, int sz, int limit) :
_buffer(buffer),
_head(_buffer),
_tail(_buffer + sz),
_limit(_buffer + (sz > limit ? sz : limit)),
_overflow(false),
_owned(false)
{
}
Buffer::~Buffer()
{
if (_owned)
{
_memory.free(_buffer);
_buffer = nullptr;
}
}
Buffer::Buffer(Buffer&& other) :
_buffer(other._buffer),
_head(other._head),
_tail(other._tail),
_limit(other._limit),
_overflow(other._overflow),
_owned(other._owned)
{
other._buffer = nullptr;
other._head = nullptr;
other._tail = nullptr;
other._limit = nullptr;
other._overflow = false;
other._owned = false;
}
Buffer& Buffer::operator=(Buffer&& other)
{
if (_owned && _buffer)
{
_memory.free(_buffer);
}
_buffer = other._buffer;
_head = other._head;
_tail = other._tail;
_limit = other._limit;
_overflow = other._overflow;
_owned = other._owned;
other._buffer = nullptr;
other._head = nullptr;
other._tail = nullptr;
other._limit = nullptr;
other._overflow = false;
other._owned = false;
return *this;
}
void Buffer::reset()
{
_head = _tail = _buffer;
}
int Buffer::pushBytes(const uint8_t* bytes, int cnt)
{
if (available() < cnt)
cnt = available();
memcpy(_tail, bytes, cnt);
_tail += cnt;
return cnt;
}
#if CINEK_AVLIB_IOSTREAMS
int Buffer::pushBytesFromStream(std::basic_istream<char>& istr, int cnt)
{
if (available() < cnt)
cnt = available();
istr.read((char*)_tail, cnt);
if (istr.eof())
{
cnt = istr.gcount();
}
else if (istr.fail())
{
return -1;
}
_tail += cnt;
return cnt;
}
#endif
Buffer& Buffer::pullBytesFrom(Buffer& source, int cnt, int* pulled)
{
// clip cnt to the intersection of this buffer and the target's.
if (cnt > source.size())
cnt = source.size();
if (available() < cnt)
cnt = available();
if (cnt > 0)
{
memcpy(_tail, source._head, cnt);
_tail += cnt;
source._head += cnt;
}
if (pulled)
{
*pulled = cnt;
}
return *this;
}
// Generates a subuffer from the available space of the owning buffer.
// This is a buffer composed of [tail+offset, tail+offset+sz]
// If either the buffer offset or size result in a buffer falling outside
// this buffer's memory region, the returned buffer will reflect the
// difference
Buffer Buffer::createSubBuffer(int offset, int sz)
{
uint8_t* buffer = _tail + offset;
if (buffer > _limit)
return Buffer(_limit, 0);
if (buffer + sz > _limit)
sz = _limit - buffer;
return Buffer(buffer, 0, sz);
}
Buffer Buffer::createSubBufferFromUsed()
{
return Buffer(_head, _tail - _head);
}
StringBuffer::StringBuffer()
{
}
StringBuffer::StringBuffer(int sz, const Memory& memory) :
_buffer(sz, memory)
{
}
StringBuffer::StringBuffer(Buffer&& buffer) :
_buffer(std::move(buffer))
{
}
// skips null characters if delim != 0.
// else terminates on delim or end of buffer.
StringBuffer& StringBuffer::getline(std::string& str, char delim)
{
str.clear();
while (!_buffer.empty())
{
char ch = (char)_buffer.pullByte();
if (ch == delim)
break;
str.push_back(ch);
}
return *this;
}
bool StringBuffer::end() const
{
return _buffer.empty();
}
} /* namespace ckavlib */