-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoproreader.cpp
More file actions
204 lines (170 loc) · 6.05 KB
/
goproreader.cpp
File metadata and controls
204 lines (170 loc) · 6.05 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
#include "goproreader.h"
#include <QString>
#include <QDebug>
#include <QtEndian>
#include <memory>
#include <QDateTime>
GoproReader::GoproReader(uint32_t timeStamp, ExifData *exifData) :
begin(GPMF_stream()), timeStamp(timeStamp), exifData(exifData)
{
}
QString 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);
}
bool GoproReader::gpsFix()
{
GPMF_stream strm;
bool ret;
GPMF_CopyState(&begin, &strm);
// 'FSPG' = 'GPSF' in GoPro byte order
if (GPMF_FindNext(&strm, 'FSPG', GPMF_RECURSE_LEVELS) == GPMF_OK) {
unsigned long fixType;
GPMF_ScaledData(&strm, &fixType, sizeof(fixType), 0, 1, GPMF_TYPE_UNSIGNED_LONG);
if (fixType == 0)
qWarning() << "no GPS fix";
ret = fixType != 0;
}
else
ret = false;
return ret;
}
bool GoproReader::handleGPS()
{
GPMF_stream strm;
GPMF_CopyState(&begin, &strm);
// '5SPG' = 'GPS5' in GoPro byte order
if (GPMF_FindNext(&strm, '5SPG', GPMF_RECURSE_LEVELS) == GPMF_OK) {
GPMF_stream prev;
GPMF_CopyState(&strm, &prev);
// get scaling info, 'LACS' = 'SCAL' in GoPro byte order
GPMF_FindPrev(&prev, 'LACS', GPMF_CURRENT_LEVEL);
struct {
long lat, lon, alt, speed2, speed3;
} scal;
Q_UNUSED(scal.speed3)
static_assert(sizeof(scal) == 5 * sizeof(long));
GPMF_ScaledData(&prev, &scal, sizeof(scal), 0, 4, GPMF_TYPE_SIGNED_LONG);
// get closest GPS sample
auto gpsSamples = GPMF_Repeat(&strm);
auto sample = floor(((double) timeStamp) / (1000.0 / gpsSamples));
if (sample > gpsSamples)
sample = gpsSamples;
// get GPS data
struct {
long lat, lon, alt, speed2, speed3;
} gps;
Q_UNUSED(gps.speed3)
static_assert(sizeof(gps) == 5 * sizeof(long));
GPMF_FormattedData(&strm, &gps, sizeof(gps), sample, 1);
// GPS lat/lon reference
if (gps.lon < 0) {
exifData->add("Exif.GPSInfo.GPSLatitudeRef", "S");
gps.lon *= -1;
}
else
exifData->add("Exif.GPSInfo.GPSLatitudeRef", "N");
if (gps.lat < 0) {
exifData->add("Exif.GPSInfo.GPSLongitudeRef", "W");
gps.lat *= -1;
}
else
exifData->add("Exif.GPSInfo.GPSLongitudeRef", "E");
// GPS lat/lon
for (auto comp: {std::make_pair("Exif.GPSInfo.GPSLatitude", ((float) gps.lat) / scal.lat),
std::make_pair("Exif.GPSInfo.GPSLongitude", ((float) gps.lon) / scal.lon)})
{
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));
}
// GPS altitude
if (gps.alt < 0) {
exifData->add("Exif.GPSInfo.GPSAltitudeRef", uint16_t(1)); // below sea level
gps.alt *= -1;
}
else
exifData->add("Exif.GPSInfo.GPSAltitudeRef", uint16_t(0)); // above sea level
exifData->add("Exif.GPSInfo.GPSAltitude", gps.alt, scal.alt);
// GPS speed
if (gps.speed2 > 0) {
// convert m/s to km/h.
// mulitply by 60 * 60, 1000 is shortened to 36, 10
exifData->add("Exif.GPSInfo.GPSSpeed", gps.speed2 * 36, scal.speed2 * 10);
exifData->add("Exif.GPSInfo.GPSSpeedRef", "K");
}
return true;
}
else
return false;
}
void GoproReader::handleDOP()
{
GPMF_stream strm;
GPMF_CopyState(&begin, &strm);
// 'PSPG' = GPSP in GoPro byte order
if (GPMF_FindNext(&strm, 'PSPG', GPMF_RECURSE_LEVELS) == GPMF_OK) {
GPMF_stream prev;
GPMF_CopyState(&strm, &prev);
// get scaling info
unsigned long scal;
GPMF_FindPrev(&prev, 'LACS', GPMF_CURRENT_LEVEL);
GPMF_ScaledData(&prev, &scal, sizeof(scal), 0, 1, GPMF_TYPE_SIGNED_LONG);
// get DOP
unsigned short dop;
GPMF_FormattedData(&strm, &dop, sizeof(dop), 0, 1);
exifData->add("Exif.GPSInfo.GPSDOP", dop, scal);
}
}
void GoproReader::handleGPSTime()
{
GPMF_stream strm;
GPMF_CopyState(&begin, &strm);
if (GPMF_FindNext(&strm, 'USPG', GPMF_RECURSE_LEVELS) == GPMF_OK) {
auto bufsize = GPMF_FormattedDataSize(&strm);
auto repeats = GPMF_Repeat(&strm);
QByteArray buf(bufsize, Qt::Uninitialized);
GPMF_FormattedData(&strm, buf.data(), buf.capacity(), 0, repeats);
auto dateStr = QString::fromLatin1(buf);
auto date = QDateTime::fromString(dateStr, "yyMMddhhmmss.zzz");
if (date.isValid()) {
exifData->add("Exif.GPSInfo.GPSDateStamp", date.toString("yyyy:MM:dd").toLatin1().data());
exifData->add("Exif.GPSInfo.GPSTimeStamp", date.toString("hh:mm:ss").toLatin1().data());
}
}
}
void GoproReader::handleDeviceName()
{
// 'MNVD' = 'DVNM' in GoPro byte order
if (GPMF_FindNext(&begin, 'MNVD', GPMF_RECURSE_LEVELS) == GPMF_OK) {
auto bufsize = GPMF_FormattedDataSize(&begin);
auto repeats = GPMF_Repeat(&begin);
QByteArray buf(bufsize, Qt::Uninitialized);
GPMF_FormattedData(&begin, buf.data(), buf.capacity(), 0, repeats);
auto model = QString::fromLatin1(buf).toStdString();
exifData->add("Exif.Image.Make", "GoPro");
exifData->add("Exif.Image.Model", model);
}
}
void GoproReader::extract(QByteArray &data)
{
if (GPMF_Init(&begin, reinterpret_cast<uint32_t *>(data.data()), data.size()) != GPMF_OK)
return;
// GPS
if (gpsFix()) {
if (handleGPS())
exifData->add("Exif.GPSInfo.GPSProcessingMethod", "GPS");
handleDOP();
handleGPSTime();
}
// device name
handleDeviceName();
}