-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediareader.cpp
More file actions
415 lines (339 loc) · 11.5 KB
/
mediareader.cpp
File metadata and controls
415 lines (339 loc) · 11.5 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
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
#include "mediareader.h"
#include "goproreader.h"
#include <QDebug>
#include <QDateTime>
MediaReader::MediaReader(AVIOContext *ctx, ExifData *exifData, int targetTrackID, double timeStamp) :
ctx(ctx), md(exifData), targetTrackID(targetTrackID), timeStamp(timeStamp), colorParams({2, 2, 2} /* undef */)
{
}
void MediaReader::extract()
{
if (avio_seek(ctx, 0, AVSEEK_FORCE) != 0)
return;
decend(ctx, avio_size(ctx));
return;
}
void MediaReader::gps2Exif(ExifData *exifData, QString lat, QString lon)
{
exifData->add("Exif.GPSInfo.GPSLatitudeRef", lat[0] == '-' ? "S" : "N");
exifData->add("Exif.GPSInfo.GPSLongitudeRef", lon[0] == '-' ? "W" : "E");
for (auto comp: {std::make_pair("Exif.GPSInfo.GPSLatitude", lat.toDouble()),
std::make_pair("Exif.GPSInfo.GPSLongitude", lon.toDouble())})
{
const auto decDeg = comp.second;
const auto deg = floor(decDeg);
const auto min = floor((decDeg - deg) * 60.0);
const auto sec = floor((decDeg - deg - min / 60.0) * 3600.0);
exifData->add(comp.first, std::make_pair(deg, 1), std::make_pair(min, 1), std::make_pair(sec, 1));
}
}
QString MediaReader::fourCCStr(int fourCC)
{
char fc[5];
fc[0] = (fourCC >> 24) & 255;
fc[1] = (fourCC >> 16) & 255;
fc[2] = (fourCC >> 8) & 255;
fc[3] = (fourCC >> 0) & 255;
fc[4] = 0;
return QString::fromLatin1(fc, 4);
}
void MediaReader::decend(AVIOContext *ctx, int64_t rangeEnd)
{
while (true) {
size_t atomSize = avio_rb32(ctx);
auto fourCC = avio_rb32(ctx);
if (fourCC == 0)
break;
if (atomSize == 0) {
atomSize = avio_size(ctx);
}
else if (atomSize == 1) {
atomSize = avio_rb64(ctx);
atomSize -= (sizeof(atomSize) + 2 * sizeof(fourCC));
}
else {
atomSize -= (2 * sizeof(fourCC));
}
char fc[5];
fc[0] = (fourCC >> 24) & 255;
fc[1] = (fourCC >> 16) & 255;
fc[2] = (fourCC >> 8) & 255;
fc[3] = (fourCC >> 0) & 255;
fc[4] = 0;
auto basePos = avio_tell(ctx);
qDebug() << basePos << QString::fromLatin1(fc, 4) << atomSize;
switch(fourCC) {
case 'moov':
case 'trak':
case 'minf':
case 'stbl':
decend(ctx, basePos + atomSize);
break;
case 'udta':
qInfo() << "udta";
handle_udta(ctx, basePos + atomSize);
break;
case 'tkhd':
handle_tkhd(ctx, basePos + atomSize);
break;
case 'stsd':
handle_stsd(ctx, basePos, basePos + atomSize);
break;
case 'colr':
handle_colr(ctx, basePos + atomSize);
break;
case 'hdlr':
handle_hdlr(ctx, basePos, basePos + atomSize);
break;
case 'stco':
handle_stco(ctx, basePos, basePos + atomSize);
break;
case 'stsz':
handle_stsz(ctx, basePos, basePos + atomSize);
break;
case 'stts':
handle_stts(ctx, basePos, basePos + atomSize);
break;
case 'mdia':
handle_mdia(ctx, basePos, basePos + atomSize);
break;
}
auto rc = avio_seek(ctx, basePos + atomSize, SEEK_SET);
if (rc <= 0) {
qDebug() << "seek: " << rc;
break;
}
if (avio_tell(ctx) >= rangeEnd || avio_feof(ctx))
break;
}
}
void MediaReader::handle_udta(AVIOContext *ctx, int64_t rangeEnd)
{
while(true) {
auto basePos = avio_tell(ctx);
auto itm_size = avio_rb32(ctx);
auto itm_type = static_cast<uint32_t>(avio_rb32(ctx));
if (itm_type == 0xA978797A /* xyz */) {
// GPS
auto str_size = avio_rb16(ctx);
avio_skip(ctx, 2); // language
std::string gps(str_size + 1, '\0');
avio_read(ctx, (unsigned char *) gps.data(), str_size);
auto gpsStr = QString::fromLatin1(gps.data(), gps.find('\0'));
if (gpsStr.endsWith("/"))
gpsStr.chop(1);
auto sep = gpsStr.lastIndexOf('-');
if (sep == -1)
sep = gpsStr.lastIndexOf('+');
gps2Exif(md, gpsStr.left(sep), gpsStr.mid(sep));
}
else if (itm_type == 0xA96D646C /* mdl */ || itm_type == 0xA963736E /* csn */)
{
// model, serial
const auto strSize = itm_size - sizeof(itm_type);
std::string str(strSize + 1, 0);
avio_read(ctx, (unsigned char *) str.data(), strSize);
str.resize(str.find('\0'));
std::string key;
if (itm_type == 0xA96D646C /* mdl */)
key = "Exif.Image.Model";
else if (itm_type == 0xA963736E /* csn */)
key = "Exif.Image.CameraSerialNumber";
md->add(key, str);
}
avio_seek(ctx, basePos + itm_size, SEEK_SET);
if (avio_tell(ctx) >= rangeEnd)
break;
}
}
void MediaReader::handle_tkhd(AVIOContext *ctx, int64_t rangeEnd)
{
auto pos = avio_tell(ctx);
if (pos + 4 > rangeEnd)
return;
// read Tracker Header
auto ver = avio_r8(ctx);
avio_skip(ctx, 3); // flags
uint64_t creat, mod;
if (ver == 0) {
if (pos + 4 + 2 * 4 + 4 > rangeEnd)
return;
creat = avio_rb32(ctx);
mod = avio_rb32(ctx);
}
else if (ver == 1) {
if (pos + 4 + 2 * 8 + 4 > rangeEnd)
return;
creat = avio_rb64(ctx);
mod = avio_rb64(ctx);
}
else {
return;
}
auto track = avio_rb32(ctx);
if (track != targetTrackID)
return;
// add time stamp inside the video to values read
double ofs;
uint32_t frac = modf(timeStamp, &ofs) * 100;
if (creat == mod) {
creat += ofs;
mod += ofs;
}
else
creat += ofs;
// add Exif fields
const auto origDate = QDateTime::fromString("01011904", "ddMMyyyy").addSecs(creat)
.toString("yyyy:MM:dd hh:mm:ss").toStdString();
const auto subSecTime = QString("%1").arg(frac).toStdString();
md->add("Exif.Image.DateTime", QDateTime::fromString("01011904", "ddMMyyyy").addSecs(mod)
.toString("yyyy:MM:dd hh:mm:ss").toStdString());
md->add("Exif.Image.DateTimeOriginal", origDate);
md->add("Exif.Photo.DateTimeOriginal", origDate);
md->add("Exif.Photo.SubSecTime", subSecTime);
md->add("Exif.Photo.SubSecTimeOriginal", subSecTime);
}
void MediaReader::handle_stsd(AVIOContext *ctx, int64_t rangeBase, int64_t rangeEnd)
{
// stsd reference:
// https://titanwolf.org/Network/Articles/Article?AID=b97d2313-9919-4a62-b4c5-d29d53b1bf71
// https://img-blog.csdn.net/20170205180429644
if (rangeBase + 8 > rangeEnd)
return;
auto ver = avio_r8(ctx);
if (ver != 0)
return;
avio_skip(ctx, 3); // flags
auto entries = avio_rb32(ctx);
for (decltype(entries) entry = 0; entry < entries; entry++) {
auto size = avio_rb32(ctx);
auto fmt = avio_rb32(ctx);
if (fmt == 'avc1') {
handle_avc1(ctx, size);
}
}
}
void MediaReader::handle_avc1(AVIOContext *ctx, int64_t atomSize)
{
auto pos = avio_tell(ctx);
if (atomSize <= 78)
return;
avio_skip(ctx, 78); // TODO: check entry count https://img-blog.csdn.net/20170205180429644
decend(ctx, pos - 8 + atomSize);
}
void MediaReader::handle_colr(AVIOContext *ctx, int64_t rangeEnd)
{
// colr spec: https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html#//apple_ref/doc/uid/TP40000939-CH205-125526
auto pos = avio_tell(ctx);
if (pos + 10 > rangeEnd)
return;
auto paramType = avio_rb32(ctx);
if (!(paramType == 'nclc' || paramType == 'nclx'))
return;
colorParams.primaries = avio_rb16(ctx);
colorParams.transfer = avio_rb16(ctx);
colorParams.matrix = avio_rb16(ctx);
}
void MediaReader::handle_hdlr(AVIOContext *ctx, int64_t rangeBase, int64_t rangeEnd)
{
if (rangeBase + 25 > rangeEnd)
return;
avio_skip(ctx, 4);
auto comp = avio_rb32(ctx);
if (comp != 'mhlr')
return;
auto sub = avio_rb32(ctx);
if (sub != 'meta')
return;
avio_skip(ctx, 13);
auto len = rangeEnd - rangeBase - 25;
QByteArray name(len + 1, '\0');
avio_read(ctx, reinterpret_cast<unsigned char *>(name.data()), len);
auto handler = QString::fromLatin1(name.constData(), name.indexOf('\0'));
if (handler == "GoPro MET ") {
readingGoProMeta = true;
}
else {
readingGoProMeta = false;
if (handler == "DJI.Meta") {
md->add("Exif.Image.Make", "DJI");
}
}
}
void MediaReader::handle_stco(AVIOContext *ctx, int64_t rangeBase, int64_t rangeEnd)
{
if (rangeBase + 8 > rangeEnd)
return;
if (readingGoProMeta) {
avio_skip(ctx, 4);
auto entries = avio_rb32(ctx);
metaTrackSamples.resize(entries);
for (auto &sample: metaTrackSamples)
sample.offset = avio_rb32(ctx);
}
}
void MediaReader::handle_stsz(AVIOContext *ctx, int64_t rangeBase, int64_t rangeEnd)
{
if (rangeBase + 8 > rangeEnd)
return;
if (readingGoProMeta) {
avio_skip(ctx, 8);
auto entries = avio_rb32(ctx);
metaTrackSamples.resize(entries);
for (auto &sample: metaTrackSamples)
sample.size = avio_rb32(ctx);
}
}
void MediaReader::handle_stts(AVIOContext *ctx, int64_t rangeBase, int64_t rangeEnd)
{
if (rangeBase + 8 > rangeEnd)
return;
if (readingGoProMeta) {
avio_skip(ctx, 4);
auto entries = avio_rb32(ctx);
decltype(entries) sample = 0;
for (decltype(entries) entry = 0; entry < entries; entry++)
{
auto sampleCount = avio_rb32(ctx);
auto sampleDuration = avio_rb32(ctx);
for (decltype(sampleCount) cntr = 0; cntr < sampleCount; cntr++) {
if (sample + 1 > metaTrackSamples.size())
metaTrackSamples.resize(sample + 1);
metaTrackSamples[sample++].duration = sampleDuration;
}
}
}
}
void MediaReader::handle_mdia(AVIOContext *ctx, int64_t rangeBase, int64_t rangeEnd)
{
readingGoProMeta = false;
metaTrackSamples.clear();
decend(ctx, rangeEnd);
if (!readingGoProMeta)
return;
uint64_t ts = timeStamp * 1000;
auto target = metaTrackSamples.end();
uint64_t trackTimestmp = 0;
for (auto sample = metaTrackSamples.begin(); sample != metaTrackSamples.end(); sample++) {
if (ts >= trackTimestmp && ts <= trackTimestmp + sample->duration) {
target = sample;
break;
}
trackTimestmp += sample->duration;
}
decltype(ts) timeOffset;
if (target == metaTrackSamples.end() && metaTrackSamples.size() > 0) {
target = metaTrackSamples.end() - 1;
timeOffset = 999; // end of last sample
}
else
timeOffset = ts - trackTimestmp;
if (target != metaTrackSamples.end()) {
QByteArray buf(target->size, Qt::Uninitialized);
avio_seek(ctx, target->offset, SEEK_SET);
avio_read(ctx, reinterpret_cast<unsigned char*>(buf.data()), target->size);
GoproReader gr(timeOffset, md);
gr.extract(buf);
}
}
static_assert('ftyp' == 1718909296);