forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox.cpp
More file actions
391 lines (347 loc) · 9.03 KB
/
box.cpp
File metadata and controls
391 lines (347 loc) · 9.03 KB
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
/*****************************************************************************
*
* Copyright 2016 Varol Okan. All rights reserved.
*
* 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.
*
****************************************************************************/
// Tool for loading mpeg4 files and manipulating atoms.
#include <string.h>
#include <iostream>
#include "constants.h"
#include "box.h"
Box::Box ( )
{
memset ( (char *)m_name, ' ', sizeof ( m_name ) );
m_iType = constants::Box;
m_iPosition = 0;
m_iHeaderSize = 0;
m_iContentSize = 0;
m_pContents = NULL;
}
Box::~Box ( )
{
if ( m_pContents )
delete m_pContents;
m_pContents = NULL;
m_iContentSize = m_iHeaderSize = m_iPosition = 0;
}
double Box::readDouble ( std::fstream &fs )
{
union {
double fVal;
int64_t iVal;
char bytes[8];
} buf;
fs.read ( buf.bytes, 8 );
// BigEndian to Host
buf.iVal = be64toh ( buf.iVal );
return buf.fVal;
}
uint8_t Box::readUint8 ( std::fstream &fs )
{
union {
uint8_t iVal;
char bytes[1];
} buf;
fs.read ( buf.bytes, 1 );
// BigEndian to Host
return buf.iVal; //be8toh ( buf.iVal );
}
int8_t Box::readInt8 ( std::fstream &fs )
{
union {
int8_t iVal;
char bytes[1];
} buf;
fs.read ( buf.bytes, 1 );
// BigEndian to Host
return buf.iVal; //be8toh ( buf.iVal );
}
int16_t Box::readInt16 ( std::fstream &fs )
{
union {
int16_t iVal;
char bytes[2];
} buf;
fs.read ( buf.bytes, 2 );
// BigEndian to Host
return be16toh ( buf.iVal );
}
int32_t Box::readInt32 ( std::fstream &fs )
{
union {
int32_t iVal;
char bytes[4];
} buf;
fs.read ( buf.bytes, 4 );
// BigEndian to Host
return be16toh ( buf.iVal );
}
uint32_t Box::readUint32 ( std::fstream &fs )
{
union {
uint32_t iVal;
char bytes[4];
} buf;
fs.read ( buf.bytes, 4 );
// BigEndian to Host
return be32toh ( buf.iVal );
}
uint64_t Box::readUint64 ( std::fstream &fs )
{
union {
uint64_t iVal;
char bytes[8];
} buf;
fs.read ( buf.bytes, 8 );
// BigEndian to Host
return be64toh ( buf.iVal );
}
void Box::writeUint8 ( std::fstream &fs, uint8_t iVal )
{
union {
uint8_t iVal;
char bytes[1];
} buf;
buf.iVal = iVal; // htobe8 ( iVal );
fs.write ( (char *)buf.bytes, 1 );
}
void Box::writeInt16 ( std::fstream &fs, int16_t iVal )
{
union {
int16_t iVal;
char bytes[2];
} buf;
buf.iVal = htobe16 ( iVal );
fs.write ( (char *)buf.bytes, 2 );
}
void Box::writeInt32 ( std::fstream &fs, int32_t iVal )
{
union {
int32_t iVal;
char bytes[4];
} buf;
buf.iVal = htobe32 ( iVal );
fs.write ( (char *)buf.bytes, 4 );
}
void Box::writeUint32 ( std::fstream &fs, uint32_t iVal )
{
union {
uint32_t iVal;
char bytes[4];
} buf;
buf.iVal = htobe32 ( iVal );
fs.write ( (char *)buf.bytes, 4 );
}
void Box::writeUint64 ( std::fstream &fs, uint64_t iVal )
{
union {
uint64_t iVal;
char bytes[8];
} buf;
buf.iVal = htobe64 ( iVal );
fs.write ( (char *)buf.bytes, 8 );
}
const char *Box::name ( )
{
static char name[5];
memcpy ( name, m_name, 4 );
name[4] = 0;
return name;
}
Box *Box::load ( std::fstream &fs, uint32_t iPos, uint32_t iEnd )
{
// Loads the box located at a position in a mp4 file
//
// if ( iPos < 1 ) // iPos is None:
// iPos = fs.tellg ( );
uint32_t iHeaderSize = 0;
uint64_t iSize = 0LL;
char name[4];
fs.seekg ( iPos );
iHeaderSize = 8;
iSize = (uint64_t)readUint32 ( fs );
fs.read ( name, 4 );
if ( iSize == 1 ) {
iSize = readUint64 ( fs );
iHeaderSize = 16;
}
if ( iSize < 8 ) {
std::cerr << "Error, invalid size " << iSize << " in " << name << " at " << iPos << std::endl;
return NULL;
}
if ( iPos + iSize > iEnd ) {
std::cerr << "Error: Leaf box size exceeds bounds." << std::endl;
return NULL;
}
Box *pNewBox = new Box ( );
memcpy ( pNewBox->m_name, name, sizeof ( name ) );
pNewBox->m_iPosition = iPos;
pNewBox->m_iHeaderSize = iHeaderSize;
pNewBox->m_iContentSize = iSize - iHeaderSize;
pNewBox->m_pContents = NULL;
return pNewBox;
}
int32_t Box::type ( )
{
return m_iType;
}
void Box::clear ( std::vector<Box *> &list )
{
std::vector<Box *>::iterator it = list.begin ( );
while (it != list.end ( ) ) {
delete *it++;
}
list.clear ( );
}
int32_t Box::content_start ( )
{
return m_iPosition + m_iHeaderSize;
}
void Box::save ( std::fstream &fsIn, std::fstream &fsOut, int32_t iDelta )
{
// Save box contents prioritizing set contents.
// iDelta = index update amount
if ( m_iHeaderSize == 16 ) {
uint64_t iBigSize = size ( );
writeUint32 ( fsOut, 1 );
fsOut.write ( m_name,4 );
writeUint64 ( fsOut, iBigSize );
}
else if ( m_iHeaderSize == 8 ) {
writeUint32 ( fsOut, size ( ) );
fsOut.write ( m_name, 4 );
}
if ( content_start ( ) )
fsIn.seekg ( content_start ( ) );
if ( memcmp ( m_name, constants::TAG_STCO, 4 ) == 0 )
stco_copy ( fsIn, fsOut, this, iDelta );
else if ( memcmp ( m_name, constants::TAG_CO64, 4 ) == 0 )
co64_copy ( fsIn, fsOut, this, iDelta );
else if ( m_pContents )
fsOut.write ( (char *)m_pContents, m_iContentSize );
else
tag_copy ( fsIn, fsOut, m_iContentSize );
}
void Box::set ( uint8_t *pNewContents, uint32_t iSize )
{
// sets / overwrites the box contents.
m_pContents = pNewContents;
m_iContentSize = iSize;
}
int32_t Box::size ( )
{
return m_iHeaderSize + m_iContentSize;
}
void Box::print_structure ( const char *pIndent )
{
std::cout << "{" << pIndent << "}" << "{" << name ( ) << "} ";
std::cout << "[{" << m_iHeaderSize << "}, {" << m_iContentSize << "}]" << std::endl;
}
void Box::tag_copy ( std::fstream &fsIn, std::fstream &fsOut, int32_t iSize )
{
// Copies a block of data from fsIn to fsOut.
// On 32-bit systems reading / writing is limited to 2GB chunks.
// To prevent overflow, read/write 64 MB chunks.
int32_t block_size = 64 * 1024 * 1024;
m_pContents = new uint8_t[block_size + 1];
while ( iSize > block_size ) {
fsIn.read ( (char *)m_pContents, block_size );
fsOut.write ( (char *)m_pContents, block_size );
iSize -= block_size;
}
fsIn.read ( (char *)m_pContents, iSize );
fsOut.write ( (char *)m_pContents, iSize );
}
void Box::index_copy ( std::fstream &fsIn, std::fstream &fsOut, Box *pBox, bool bBigMode, int32_t iDelta )
{
// Update and copy index table for stco/co64 files.
// pBox: box, stco/co64 box to copy.
// bBigMode: if true == BigEndian Uint64, else BigEndian Int32
// iDelta: int, offset change for index entries.
std::fstream &fs = fsIn;
if ( ! pBox->m_pContents )
fs.seekg ( pBox->content_start ( ) );
else {
//fs = StringIO.StringIO ( box->m_pContents );
return index_copy_from_contents ( fsOut, pBox, bBigMode, iDelta );
}
uint32_t iHeader = readUint32 ( fs );
uint32_t iValues = readUint32 ( fs );
writeUint32 ( fsOut, iHeader );
writeUint32 ( fsOut, iValues );
if ( bBigMode ) {
for ( auto i = 0U; i<iValues; i++ ) {
uint64_t iVal = readUint64 ( fsIn ) + iDelta;
writeUint64 ( fsOut, iVal );
}
}
else {
for ( auto i = 0U; i<iValues; i++ ) {
uint32_t iVal = readUint32 ( fsIn ) + iDelta;
writeUint32 ( fsOut, iVal );
}
}
}
uint32_t Box::uint32FromCont ( int32_t &iIDX )
{
union {
uint32_t iVal;
char bytes[4];
} buf;
memcpy ( buf.bytes, (char *)&m_pContents[iIDX], 4 );
iIDX += 4;
return be32toh ( buf.iVal );
}
uint64_t Box::uint64FromCont ( int32_t &iIDX )
{
union {
uint64_t iVal;
char bytes[8];
} buf;
memcpy ( buf.bytes, (char *)&m_pContents[iIDX], 8 );
iIDX += 8;
return be64toh ( buf.iVal );
}
void Box::index_copy_from_contents ( std::fstream &fsOut, Box *pBox, bool bBigMode, int32_t iDelta )
{
(void) pBox; // unused
int32_t iIDX = 0;
uint32_t iHeader = uint32FromCont ( iIDX );
uint32_t iValues = uint32FromCont ( iIDX );
writeUint32 ( fsOut, iHeader );
writeUint32 ( fsOut, iValues );
if ( bBigMode ) {
for ( auto i = 0U; i<iValues; i++ ) {
uint64_t iVal = uint64FromCont ( iIDX ) + iDelta;
writeUint64 ( fsOut, iVal );
}
}
else {
for ( auto i = 0U; i<iValues; i++ ) {
uint32_t iVal = uint32FromCont ( iIDX ) + iDelta;
writeUint32 ( fsOut, iVal );
}
}
}
void Box::stco_copy ( std::fstream &fsIn, std::fstream &fsOut, Box *pBox, int32_t iDelta )
{
// Copy for stco box.
index_copy ( fsIn, fsOut, pBox, false, iDelta );
}
void Box::co64_copy ( std::fstream &fsIn, std::fstream &fsOut, Box *pBox, int32_t iDelta )
{
// Copy for co64 box.
index_copy ( fsIn, fsOut, pBox, true, iDelta );
}