-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathcamm_parser.py
More file actions
529 lines (413 loc) · 15.1 KB
/
camm_parser.py
File metadata and controls
529 lines (413 loc) · 15.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# pyre-ignore-all-errors[5, 11, 16, 21, 24, 58]
from __future__ import annotations
import abc
import dataclasses
import io
import logging
import typing as T
from enum import Enum
import construct as C
from typing_extensions import TypeIs
from .. import geo, telemetry
from ..mp4 import simple_mp4_parser as sparser
from ..mp4.mp4_sample_parser import MovieBoxParser, Sample, TrackBoxParser
LOG = logging.getLogger(__name__)
TelemetryMeasurement = T.Union[
geo.Point,
telemetry.TelemetryMeasurement,
]
# Camera Motion Metadata Spec https://developers.google.com/streetview/publish/camm-spec
class CAMMType(Enum):
ANGLE_AXIS = 0
EXPOSURE_TIME = 1
GYRO = 2
ACCELERATION = 3
POSITION = 4
MIN_GPS = 5
GPS = 6
MAGNETIC_FIELD = 7
# All fields are little-endian
Float = C.Float32l
Double = C.Float64l
TTelemetry = T.TypeVar("TTelemetry", bound=TelemetryMeasurement)
class CAMMSampleEntry(abc.ABC, T.Generic[TTelemetry]):
serialized_camm_type: CAMMType
telemetry_cls_type: T.Type[TTelemetry]
construct: C.Struct
@classmethod
def serializable(cls, data: T.Any, throw: bool = False) -> TypeIs[TTelemetry]:
# Use "is" for exact type match, instead of isinstance
if type(data) is cls.telemetry_cls_type:
return True
if throw:
raise TypeError(
f"{cls} can not serialize {type(data)}: expect {cls.telemetry_cls_type}"
)
return False
@classmethod
@abc.abstractmethod
def serialize(cls, data: TTelemetry) -> bytes:
raise NotImplementedError
@classmethod
@abc.abstractmethod
def deserialize(cls, sample: Sample, data: T.Any) -> TTelemetry:
raise NotImplementedError
class MinGPSSampleEntry(CAMMSampleEntry):
serialized_camm_type = CAMMType.MIN_GPS
telemetry_cls_type = geo.Point
construct = Double[3] # type: ignore
@classmethod
def deserialize(cls, sample: Sample, data: T.Any) -> geo.Point:
return geo.Point(
time=sample.exact_time,
lat=data[0],
lon=data[1],
alt=data[2],
angle=None,
)
@classmethod
def serialize(cls, data: geo.Point) -> bytes:
cls.serializable(data, throw=True)
return CAMMSampleData.build(
{
"type": cls.serialized_camm_type.value,
"data": [
data.lat,
data.lon,
-1.0 if data.alt is None else data.alt,
],
}
)
class GPSSampleEntry(CAMMSampleEntry):
serialized_camm_type: CAMMType = CAMMType.GPS
telemetry_cls_type = telemetry.CAMMGPSPoint
construct = C.Struct(
"time_gps_epoch" / Double, # type: ignore
"gps_fix_type" / C.Int32sl, # type: ignore
"latitude" / Double, # type: ignore
"longitude" / Double, # type: ignore
"altitude" / Float, # type: ignore
"horizontal_accuracy" / Float, # type: ignore
"vertical_accuracy" / Float, # type: ignore
"velocity_east" / Float, # type: ignore
"velocity_north" / Float, # type: ignore
"velocity_up" / Float, # type: ignore
"speed_accuracy" / Float, # type: ignore
)
@classmethod
def deserialize(cls, sample: Sample, data: T.Any) -> telemetry.CAMMGPSPoint:
return telemetry.CAMMGPSPoint(
time=sample.exact_time,
lat=data.latitude,
lon=data.longitude,
alt=data.altitude,
angle=None,
time_gps_epoch=data.time_gps_epoch,
gps_fix_type=data.gps_fix_type,
horizontal_accuracy=data.horizontal_accuracy,
vertical_accuracy=data.vertical_accuracy,
velocity_east=data.velocity_east,
velocity_north=data.velocity_north,
velocity_up=data.velocity_up,
speed_accuracy=data.speed_accuracy,
)
@classmethod
def serialize(cls, data: telemetry.CAMMGPSPoint) -> bytes:
cls.serializable(data, throw=True)
return CAMMSampleData.build(
{
"type": cls.serialized_camm_type.value,
"data": {
"time_gps_epoch": data.time_gps_epoch,
"gps_fix_type": data.gps_fix_type,
"latitude": data.lat,
"longitude": data.lon,
"altitude": -1.0 if data.alt is None else data.alt,
"horizontal_accuracy": data.horizontal_accuracy,
"vertical_accuracy": data.vertical_accuracy,
"velocity_east": data.velocity_east,
"velocity_north": data.velocity_north,
"velocity_up": data.velocity_up,
"speed_accuracy": data.speed_accuracy,
},
}
)
class GoProGPSSampleEntry(CAMMSampleEntry):
serialized_camm_type: CAMMType = CAMMType.MIN_GPS
telemetry_cls_type = telemetry.GPSPoint
construct = Double[3] # type: ignore
@classmethod
def deserialize(cls, sample: Sample, data: T.Any) -> telemetry.GPSPoint:
raise NotImplementedError("Deserializing GoPro GPS Point is not supported")
@classmethod
def serialize(cls, data: telemetry.GPSPoint) -> bytes:
cls.serializable(data, throw=True)
return CAMMSampleData.build(
{
"type": cls.serialized_camm_type.value,
"data": [
data.lat,
data.lon,
-1.0 if data.alt is None else data.alt,
],
}
)
class AccelerationSampleEntry(CAMMSampleEntry):
serialized_camm_type: CAMMType = CAMMType.ACCELERATION
telemetry_cls_type = telemetry.AccelerationData
construct: C.Struct = Float[3] # type: ignore
@classmethod
def deserialize(cls, sample: Sample, data: T.Any) -> telemetry.AccelerationData:
return telemetry.AccelerationData(
time=sample.exact_time,
x=data[0],
y=data[1],
z=data[2],
)
@classmethod
def serialize(cls, data: telemetry.AccelerationData) -> bytes:
cls.serializable(data, throw=True)
return CAMMSampleData.build(
{
"type": cls.serialized_camm_type.value,
"data": [data.x, data.y, data.z],
}
)
class GyroscopeSampleEntry(CAMMSampleEntry):
serialized_camm_type: CAMMType = CAMMType.GYRO
telemetry_cls_type = telemetry.GyroscopeData
construct: C.Struct = Float[3] # type: ignore
@classmethod
def deserialize(cls, sample: Sample, data: T.Any) -> telemetry.GyroscopeData:
return telemetry.GyroscopeData(
time=sample.exact_time,
x=data[0],
y=data[1],
z=data[2],
)
@classmethod
def serialize(cls, data: telemetry.GyroscopeData) -> bytes:
cls.serializable(data)
return CAMMSampleData.build(
{
"type": cls.serialized_camm_type.value,
"data": [data.x, data.y, data.z],
}
)
class MagnetometerSampleEntry(CAMMSampleEntry):
serialized_camm_type: CAMMType = CAMMType.MAGNETIC_FIELD
telemetry_cls_type = telemetry.MagnetometerData
construct: C.Struct = Float[3] # type: ignore
@classmethod
def deserialize(cls, sample: Sample, data: T.Any) -> telemetry.MagnetometerData:
return telemetry.MagnetometerData(
time=sample.exact_time,
x=data[0],
y=data[1],
z=data[2],
)
@classmethod
def serialize(cls, data: telemetry.MagnetometerData) -> bytes:
cls.serializable(data)
return CAMMSampleData.build(
{
"type": cls.serialized_camm_type.value,
"data": [data.x, data.y, data.z],
}
)
SAMPLE_ENTRY_CLS_BY_CAMM_TYPE = {
sample_entry_cls.serialized_camm_type: sample_entry_cls
for sample_entry_cls in CAMMSampleEntry.__subclasses__()
if sample_entry_cls not in [GoProGPSSampleEntry]
}
assert len(SAMPLE_ENTRY_CLS_BY_CAMM_TYPE) == 5, SAMPLE_ENTRY_CLS_BY_CAMM_TYPE.keys()
_SWITCH: T.Dict[int, C.Struct] = {
# angle_axis
CAMMType.ANGLE_AXIS.value: Float[3], # type: ignore
CAMMType.EXPOSURE_TIME.value: C.Struct(
"pixel_exposure_time" / C.Int32sl, # type: ignore
"rolling_shutter_skew_time" / C.Int32sl, # type: ignore
),
# position
CAMMType.POSITION.value: Float[3], # type: ignore
**{t.value: cls.construct for t, cls in SAMPLE_ENTRY_CLS_BY_CAMM_TYPE.items()},
}
CAMMSampleData = C.Struct(
C.Padding(2),
"type" / C.Int16ul,
"data" / C.Switch(C.this.type, _SWITCH),
)
def _parse_telemetry_from_sample(
fp: T.BinaryIO, sample: Sample
) -> T.Optional[TelemetryMeasurement]:
fp.seek(sample.raw_sample.offset, io.SEEK_SET)
data = fp.read(sample.raw_sample.size)
box = CAMMSampleData.parse(data)
camm_type = CAMMType(box.type) # type: ignore
SampleKlass = SAMPLE_ENTRY_CLS_BY_CAMM_TYPE.get(camm_type)
if SampleKlass is None:
return None
return SampleKlass.deserialize(sample, box.data)
def _filter_telemetry_by_elst_segments(
measurements: T.Iterable[TelemetryMeasurement],
elst: T.Sequence[T.Tuple[float, float]],
) -> T.Generator[TelemetryMeasurement, None, None]:
empty_elst = [entry for entry in elst if entry[0] == -1]
if empty_elst:
offset = empty_elst[-1][1]
else:
offset = 0
elst = [entry for entry in elst if entry[0] != -1]
if not elst:
for m in measurements:
yield dataclasses.replace(m, time=m.time + offset)
return
elst.sort(key=lambda entry: entry[0])
elst_idx = 0
for m in measurements:
if len(elst) <= elst_idx:
break
media_time, duration = elst[elst_idx]
if m.time < media_time:
pass
elif m.time <= media_time + duration:
yield dataclasses.replace(m, time=m.time + offset)
else:
elst_idx += 1
def elst_entry_to_seconds(
entry: T.Dict, movie_timescale: int, media_timescale: int
) -> T.Tuple[float, float]:
assert movie_timescale > 0, "expected positive movie_timescale"
assert media_timescale > 0, "expected positive media_timescale"
media_time, duration = entry["media_time"], entry["segment_duration"]
if media_time != -1:
media_time = media_time / media_timescale
duration = duration / movie_timescale
return (media_time, duration)
def _is_camm_description(description: T.Dict) -> bool:
return description["format"] == b"camm"
def _contains_camm_description(track: TrackBoxParser) -> bool:
descriptions = track.extract_sample_descriptions()
return any(_is_camm_description(d) for d in descriptions)
def _filter_telemetry_by_track_elst(
moov: MovieBoxParser,
track: TrackBoxParser,
measurements: T.Iterable[TelemetryMeasurement],
) -> T.List[TelemetryMeasurement]:
elst_boxdata = track.extract_elst_boxdata()
if elst_boxdata is not None:
elst_entries = elst_boxdata["entries"]
if elst_entries:
# media_timescale
mdhd_boxdata = track.extract_mdhd_boxdata()
media_timescale = mdhd_boxdata["timescale"]
# movie_timescale
mvhd_boxdata = moov.extract_mvhd_boxdata()
movie_timescale = mvhd_boxdata["timescale"]
segments = [
elst_entry_to_seconds(
entry,
movie_timescale=movie_timescale,
media_timescale=media_timescale,
)
for entry in elst_entries
]
return list(_filter_telemetry_by_elst_segments(measurements, segments))
return list(measurements)
def extract_points(fp: T.BinaryIO) -> T.Optional[T.List[geo.Point]]:
"""
Return a list of points (could be empty) if it is a valid CAMM video,
otherwise None
"""
moov = MovieBoxParser.parse_stream(fp)
for track in moov.extract_tracks():
if _contains_camm_description(track):
maybe_measurements = (
_parse_telemetry_from_sample(fp, sample)
for sample in track.extract_samples()
if _is_camm_description(sample.description)
)
points = [m for m in maybe_measurements if isinstance(m, geo.Point)]
return T.cast(
T.List[geo.Point], _filter_telemetry_by_track_elst(moov, track, points)
)
return None
def extract_telemetry_data(fp: T.BinaryIO) -> T.Optional[T.List[TelemetryMeasurement]]:
moov = MovieBoxParser.parse_stream(fp)
for track in moov.extract_tracks():
if _contains_camm_description(track):
maybe_measurements = (
_parse_telemetry_from_sample(fp, sample)
for sample in track.extract_samples()
if _is_camm_description(sample.description)
)
measurements = [m for m in maybe_measurements if m is not None]
measurements = _filter_telemetry_by_track_elst(moov, track, measurements)
return measurements
return None
MakeOrModel = C.Struct(
"size" / C.Int16ub,
C.Padding(2),
"data" / C.FixedSized(C.this.size, C.GreedyBytes),
)
def _decode_quietly(data: bytes, h: sparser.Header) -> str:
try:
return data.decode("utf-8")
except UnicodeDecodeError:
LOG.warning("Failed to decode %s: %s", h, data[:512])
return ""
def _parse_quietly(data: bytes, h: sparser.Header) -> bytes:
try:
parsed = MakeOrModel.parse(data)
except C.ConstructError:
LOG.warning("Failed to parse %s: %s", h, data[:512])
return b""
if parsed is None:
return b""
return parsed["data"]
def extract_camera_make_and_model(fp: T.BinaryIO) -> tuple[str, str]:
header_and_stream = sparser.parse_path(
fp,
[
b"moov",
b"udta",
[
# Insta360 Titan
b"\xa9mak",
b"\xa9mod",
# RICHO THETA V
b"@mod",
b"@mak",
# RICHO THETA V
b"manu",
b"modl",
],
],
)
make: str = ""
model: str = ""
try:
for h, s in header_and_stream:
data = s.read(h.maxsize)
if h.type == b"\xa9mak":
make_data = _parse_quietly(data, h)
make_data = make_data.rstrip(b"\x00")
make = _decode_quietly(make_data, h)
elif h.type == b"\xa9mod":
model_data = _parse_quietly(data, h)
model_data = model_data.rstrip(b"\x00")
model = _decode_quietly(model_data, h)
elif h.type in [b"@mak", b"manu"]:
make = _decode_quietly(data, h)
elif h.type in [b"@mod", b"modl"]:
model = _decode_quietly(data, h)
# quit when both found
if make and model:
break
except sparser.ParsingError:
pass
if make:
make = make.strip()
if model:
model = model.strip()
return make, model