-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMp4Box.cpp
More file actions
205 lines (165 loc) · 4.17 KB
/
Mp4Box.cpp
File metadata and controls
205 lines (165 loc) · 4.17 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
/**
*
* author Jose Mortensen
* brief Iso Mp4 Box
*
*/
#include "stdafx.h"
#include "Mp4File.h"
#include "Mp4Box.h"
#include "Mp4FileImpl.h"
#include "Mp4IStreamSource.h"
//#include "intrin.h"
/*
#define htons _byteswap_ushort
#define htonl _byteswap_ulong
#define ntohs _byteswap_ushort
#define ntohl _byteswap_ulong
#define ntohll _byteswap_uint64
#define hllton _byteswap_uint64
*/
#define _byteswap_ushort htons
#define _byteswap_ulong htonl
#define _byteswap_uint64 ntohll
Box::Box(std::string atype) {
type = atype;
siblings = 0;
children = 0;
consumed = 0;
sizexx = 8;
largesize = 8;
//std::cout << "creating box " << this << " " << type.c_str() << std::endl;
}
Box::~Box() {
if (children) delete children;
if (siblings) delete siblings;
//std::cout << "deleting box " << this << " " << type.c_str() << std::endl;
}
void Box::setDataSize(uint64_t newSize) {
newSize = newSize + 8;
sizexx = (uint32_t)(largesize = newSize);
if (largesize > 0xffffffff)
sizexx = 1;
}
void Box::increaseSize(uint64_t delta) {
sizexx = (uint32_t)(largesize += delta);
if (largesize > 0xffffffff)
sizexx = 1;
}
Box* Box::add(Box* box) {
if (children) {
Box* last = children;
while (last->siblings) {
last = last->siblings;
}
last->siblings = box;
} else {
children = box;
}
increaseSize(box->largesize);
return this;
}
void Box::readChar(large_istream& is, char* data, int size) {
is.read((char*)data, size);
data[size]=0;
}
uint32_t Box::read32(large_istream& is) {
uint32_t ret = 0;
is.read((char*)&ret,4);
ret = _byteswap_ulong(ret);
return ret;
}
uint16_t Box::read16(large_istream& is) {
uint16_t ret = 0;
is.read((char*)&ret,2);
ret = _byteswap_ushort(ret);
return ret;
}
uint64_t Box::read64(large_istream& is) {
uint64_t ret = 0;
is.read((char*)&ret, 8);
ret = _byteswap_uint64(ret);
return ret;
}
void Box::write32(large_ostream& os, uint32_t data) {
data = _byteswap_ulong(data);
os.write((char*)&data, 4);
}
void Box::write16(large_ostream& os, uint16_t data) {
data = _byteswap_ushort(data);
os.write((char*)&data, 2);
}
void Box::write64(large_ostream& os, uint64_t data) {
data = _byteswap_uint64(data);
os.write((char*)&data, 8);
}
std::string Box::readStr(large_istream& is) {
char byte;
std::string res;
do {
is.read(&byte, 1);
if (byte!=0) res.push_back(byte);
} while(byte!=0);
return res;
}
void Box::writeStr(large_ostream& os, std::string str) {
os.write(str.c_str(), str.size()+1);
}
void Box::write(large_ostream& os) {
if (sizexx != 0)
write32(os, sizexx);
os.write(type.c_str(), 4);
if (sizexx ==1)
write64(os, largesize);
}
void Box::read(large_istream& is) {
if (largesize-consumed) {
children = Mp4FileImpl::readBox(is);
consumed += children->largesize;
}
Box* child = 0;
while (largesize-consumed) {
if (children->siblings == 0) {
children->siblings = Mp4FileImpl::readBox(is);
child = children->siblings;
} else {
child->siblings = Mp4FileImpl::readBox(is);
child = child->siblings;
}
consumed += child->largesize;
}
}
void Box::print(std::ostream& os, std::string pre) const {
os << pre << name.c_str() << " [" << type.c_str() << "] size = " << largesize << std::endl ;
if (children)
children->print(os, std::string(" ").append(pre).c_str());
if (siblings)
siblings->print(os, pre);
}
std::string Box::isoLanguage(uint16_t encoded) {
std::string lang;
char byte = (encoded & 0x1f) + 0x60;
lang.append(&byte, 1);
byte = ((encoded>>5) & 0x1f) + 0x60;
lang.append(&byte, 1);
byte = ((encoded>>10) & 0x1f) + 0x60;
lang.append(&byte, 1);
return lang;
}
uint16_t Box::isoLanguage(std::string lang) {
uint16_t code = 0;
if (lang.length() != 3) throw std::runtime_error ("Bad ISO 639 lenguage code");
char byte = lang.at(0);
code |= (byte - 0x60);
code <<=5;
byte = lang.at(1);
code |= (byte - 0x60);
code <<=5;
byte = lang.at(2);
code |= (byte - 0x60);
return code;
}
std::ostream& operator<<(std::ostream& os, const Box& box) {
box.print(os, "+--");
return os;
}