Coverage Report

Created: 2026-05-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/exiv2/src/quicktimevideo.cpp
Line
Count
Source
1
// ***************************************************************** -*- C++ -*-
2
/*
3
 * Copyright (C) 2004-2021 Exiv2 authors
4
 * This program is part of the Exiv2 distribution.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19
 */
20
// *****************************************************************************
21
// included header files
22
#include "quicktimevideo.hpp"
23
#include "basicio.hpp"
24
#include "config.h"
25
#include "enforce.hpp"
26
#include "error.hpp"
27
#include "futils.hpp"
28
#include "helper_functions.hpp"
29
#include "image_int.hpp"
30
#include "properties.hpp"
31
#include "safe_op.hpp"
32
#include "tags.hpp"
33
#include "tags_int.hpp"
34
// + standard includes
35
#include <array>
36
#include <cmath>
37
#include <string>
38
// *****************************************************************************
39
// class member definitions
40
namespace Exiv2::Internal {
41
42
static constexpr TagVocabulary qTimeFileType[] = {
43
    {"3g2a", "3GPP2 Media (.3G2) compliant with 3GPP2 C.S0050-0 V1.0"},
44
    {"3g2b", "3GPP2 Media (.3G2) compliant with 3GPP2 C.S0050-A V1.0.0"},
45
    {"3g2c", "3GPP2 Media (.3G2) compliant with 3GPP2 C.S0050-B v1.0"},
46
    {"3ge6", "3GPP (.3GP) Release 6 MBMS Extended Presentations"},
47
    {"3ge7", "3GPP (.3GP) Release 7 MBMS Extended Presentations"},
48
    {"3gg6", "3GPP Release 6 General Profile"},
49
    {"3gp1", "3GPP Media (.3GP) Release 1 (probably non-existent)"},
50
    {"3gp2", "3GPP Media (.3GP) Release 2 (probably non-existent)"},
51
    {"3gp3", "3GPP Media (.3GP) Release 3 (probably non-existent)"},
52
    {"3gp4", "3GPP Media (.3GP) Release 4"},
53
    {"3gp5", "3GPP Media (.3GP) Release 5"},
54
    {"3gp6", "3GPP Media (.3GP) Release 6 Streaming Servers"},
55
    {"3gs7", "3GPP Media (.3GP) Release 7 Streaming Servers"},
56
    {"CAEP", "Canon Digital Camera"},
57
    {"CDes", "Convergent Design"},
58
    {"F4A ", "Audio for Adobe Flash Player 9+ (.F4A)"},
59
    {"F4B ", "Audio Book for Adobe Flash Player 9+ (.F4B)"},
60
    {"F4P ", "Protected Video for Adobe Flash Player 9+ (.F4P)"},
61
    {"F4V ", "Video for Adobe Flash Player 9+ (.F4V)"},
62
    {"JP2 ", "JPEG 2000 Image (.JP2) [ISO 15444-1 ?]"},
63
    {"JP20", "Unknown, from GPAC samples (prob non-existent)"},
64
    {"KDDI", "3GPP2 EZmovie for KDDI 3G cellphones"},
65
    {"M4A ", "Apple iTunes AAC-LC (.M4A) Audio"},
66
    {"M4B ", "Apple iTunes AAC-LC (.M4B) Audio Book"},
67
    {"M4P ", "Apple iTunes AAC-LC (.M4P) AES Protected Audio"},
68
    {"M4V ", "Apple iTunes Video (.M4V) Video"},
69
    {"M4VH", "Apple TV (.M4V)"},
70
    {"M4VP", "Apple iPhone (.M4V)"},
71
    {"MPPI", "Photo Player, MAF [ISO/IEC 23000-3]"},
72
    {"MSNV", "MPEG-4 (.MP4) for SonyPSP"},
73
    {"NDAS", "MP4 v2 [ISO 14496-14] Nero Digital AAC Audio"},
74
    {"NDSC", "MPEG-4 (.MP4) Nero Cinema Profile"},
75
    {"NDSH", "MPEG-4 (.MP4) Nero HDTV Profile"},
76
    {"NDSM", "MPEG-4 (.MP4) Nero Mobile Profile"},
77
    {"NDSP", "MPEG-4 (.MP4) Nero Portable Profile"},
78
    {"NDSS", "MPEG-4 (.MP4) Nero Standard Profile"},
79
    {"NDXC", "H.264/MPEG-4 AVC (.MP4) Nero Cinema Profile"},
80
    {"NDXH", "H.264/MPEG-4 AVC (.MP4) Nero HDTV Profile"},
81
    {"NDXM", "H.264/MPEG-4 AVC (.MP4) Nero Mobile Profile"},
82
    {"NDXP", "H.264/MPEG-4 AVC (.MP4) Nero Portable Profile"},
83
    {"NDXS", "H.264/MPEG-4 AVC (.MP4) Nero Standard Profile"},
84
    {"NIKO", "Nikon"},
85
    {"ROSS", "Ross Video"},
86
    {"avc1", "MP4 Base w/ AVC ext [ISO 14496-12:2005]"},
87
    {"caqv", "Casio Digital Camera"},
88
    {"da0a", "DMB MAF w/ MPEG Layer II aud, MOT slides, DLS, JPG/PNG/MNG images"},
89
    {"da0b", "DMB MAF, extending DA0A, with 3GPP timed text, DID, TVA, REL, IPMP"},
90
    {"da1a", "DMB MAF audio with ER-BSAC audio, JPG/PNG/MNG images"},
91
    {"da1b", "DMB MAF, extending da1a, with 3GPP timed text, DID, TVA, REL, IPMP"},
92
    {"da2a", "DMB MAF aud w/ HE-AAC v2 aud, MOT slides, DLS, JPG/PNG/MNG images"},
93
    {"da2b", "DMB MAF, extending da2a, with 3GPP timed text, DID, TVA, REL, IPMP"},
94
    {"da3a", "DMB MAF aud with HE-AAC aud, JPG/PNG/MNG images"},
95
    {"da3b", "DMB MAF, extending da3a w/ BIFS, 3GPP timed text, DID, TVA, REL, IPMP"},
96
    {"dmb1", "DMB MAF supporting all the components defined in the specification"},
97
    {"dmpf", "Digital Media Project"},
98
    {"drc1", "Dirac (wavelet compression), encapsulated in ISO base media (MP4)"},
99
    {"dv1a", "DMB MAF vid w/ AVC vid, ER-BSAC aud, BIFS, JPG/PNG/MNG images, TS"},
100
    {"dv1b", "DMB MAF, extending dv1a, with 3GPP timed text, DID, TVA, REL, IPMP"},
101
    {"dv2a", "DMB MAF vid w/ AVC vid, HE-AAC v2 aud, BIFS, JPG/PNG/MNG images, TS"},
102
    {"dv2b", "DMB MAF, extending dv2a, with 3GPP timed text, DID, TVA, REL, IPMP"},
103
    {"dv3a", "DMB MAF vid w/ AVC vid, HE-AAC aud, BIFS, JPG/PNG/MNG images, TS"},
104
    {"dv3b", "DMB MAF, extending dv3a, with 3GPP timed text, DID, TVA, REL, IPMP"},
105
    {"dvr1", "DVB (.DVB) over RTP"},
106
    {"dvt1", "DVB (.DVB) over MPEG-2 Transport Stream"},
107
    {"isc2", "ISMACryp 2.0 Encrypted File"},
108
    {"iso2", "MP4 Base Media v2 [ISO 14496-12:2005]"},
109
    {"isom", "MP4 Base Media v1 [IS0 14496-12:2003]"},
110
    {"jpm ", "JPEG 2000 Compound Image (.JPM) [ISO 15444-6]"},
111
    {"jpx ", "JPEG 2000 with extensions (.JPX) [ISO 15444-2]"},
112
    {"mj2s", "Motion JPEG 2000 [ISO 15444-3] Simple Profile"},
113
    {"mjp2", "Motion JPEG 2000 [ISO 15444-3] General Profile"},
114
    {"mmp4", "MPEG-4/3GPP Mobile Profile (.MP4/3GP) (for NTT)"},
115
    {"mp21", "MPEG-21 [ISO/IEC 21000-9]"},
116
    {"mp41", "MP4 v1 [ISO 14496-1:ch13]"},
117
    {"mp42", "MP4 v2 [ISO 14496-14]"},
118
    {"mp71", "MP4 w/ MPEG-7 Metadata [per ISO 14496-12]"},
119
    {"mqt ", "Sony / Mobile QuickTime (.MQV) US Patent 7,477,830 (Sony Corp)"},
120
    {"niko", "Nikon"},
121
    {"odcf", "OMA DCF DRM Format 2.0 (OMA-TS-DRM-DCF-V2_0-20060303-A)"},
122
    {"opf2", "OMA PDCF DRM Format 2.1 (OMA-TS-DRM-DCF-V2_1-20070724-C)"},
123
    {"opx2", "OMA PDCF DRM + XBS extensions (OMA-TS-DRM_XBS-V1_0-20070529-C)"},
124
    {"pana", "Panasonic Digital Camera"},
125
    {"qt  ", "Apple QuickTime (.MOV/QT)"},
126
    {"sdv ", "SD Memory Card Video"},
127
    {"ssc1", "Samsung stereoscopic, single stream"},
128
    {"ssc2", "Samsung stereoscopic, dual stream"},
129
};
130
131
static constexpr TagVocabulary handlerClassTags[] = {
132
    {"dhlr", "Data Handler"},
133
    {"mhlr", "Media Handler"},
134
};
135
136
static constexpr TagVocabulary handlerTypeTags[] = {
137
    {"alis", "Alias Data"},
138
    {"crsm", "Clock Reference"},
139
    {"hint", "Hint Track"},
140
    {"ipsm", "IPMP"},
141
    {"m7sm", "MPEG-7 Stream"},
142
    {"mdir", "Metadata"},
143
    {"mdta", "Metadata Tags"},
144
    {"mjsm", "MPEG-J"},
145
    {"ocsm", "Object Content"},
146
    {"odsm", "Object Descriptor"},
147
    {"sdsm", "Scene Description"},
148
    {"soun", "Audio Track"},
149
    {"text", "Text"},
150
    {"tmcd", "Time Code"},
151
    {"url ", "URL"},
152
    {"vide", "Video Track"},
153
};
154
155
static constexpr TagVocabulary vendorIDTags[] = {
156
    {"FFMP", "FFmpeg"},
157
    {"appl", "Apple"},
158
    {"olym", "Olympus"},
159
    {"GIC ", "General Imaging Co."},
160
    {"fe20", "Olympus (fe20)"},
161
    {"pana", "Panasonic"},
162
    {"KMPI", "Konica-Minolta"},
163
    {"kdak", "Kodak"},
164
    {"pent", "Pentax"},
165
    {"NIKO", "Nikon"},
166
    {"leic", "Leica"},
167
    {"pr01", "Olympus (pr01)"},
168
    {"SMI ", "Sorenson Media Inc."},
169
    {"mino", "Minolta"},
170
    {"sany", "Sanyo"},
171
    {"ZORA", "Zoran Corporation"},
172
    {"niko", "Nikon"},
173
};
174
175
static constexpr TagVocabulary cameraByteOrderTags[] = {
176
    {"II", "Little-endian (Intel, II)"},
177
    {"MM", "Big-endian (Motorola, MM)"},
178
};
179
180
static constexpr TagDetails graphicsModetags[] = {
181
    {0x0, "srcCopy"},
182
    {0x1, "srcOr"},
183
    {0x2, "srcXor"},
184
    {0x3, "srcBic"},
185
    {0x4, "notSrcCopy"},
186
    {0x5, "notSrcOr"},
187
    {0x6, "notSrcXor"},
188
    {0x7, "notSrcBic"},
189
    {0x8, "patCopy"},
190
    {0x9, "patOr"},
191
    {0xa, "patXor"},
192
    {0xb, "patBic"},
193
    {0xc, "notPatCopy"},
194
    {0xd, "notPatOr"},
195
    {0xe, "notPatXor"},
196
    {0xf, "notPatBic"},
197
    {0x20, "blend"},
198
    {0x21, "addPin"},
199
    {0x22, "addOver"},
200
    {0x23, "subPin"},
201
    {0x24, "transparent"},
202
    {0x25, "addMax"},
203
    {0x26, "subOver"},
204
    {0x27, "addMin"},
205
    {0x31, "grayishTextOr"},
206
    {0x32, "hilite"},
207
    {0x40, "ditherCopy"},
208
    {0x100, "Alpha"},
209
    {0x101, "White Alpha"},
210
    {0x102, "Pre-multiplied Black Alpha"},
211
    {0x110, "Component Alpha"},
212
};
213
214
static constexpr TagVocabulary userDatatags[] = {
215
    {"AllF", "PlayAllFrames"},
216
    {"CNCV", "CompressorVersion"},
217
    {"CNFV", "FirmwareVersion"},
218
    {"CNMN", "Model"},
219
    {"CNTH", "CanonCNTH"},
220
    {"DcMD", "DcMD"},
221
    {"FFMV", "FujiFilmFFMV"},
222
    {"INFO", "SamsungINFO"},
223
    {"LOOP", "LoopStyle"},
224
    {"MMA0", "MinoltaMMA0"},
225
    {"MMA1", "MinoltaMMA1"},
226
    {"MVTG", "FujiFilmMVTG"},
227
    {"NCDT", "NikonNCDT"},
228
    {"PANA", "PanasonicPANA"},
229
    {"PENT", "PentaxPENT"},
230
    {"PXMN", "MakerNotePentax5b"},
231
    {"PXTH", "PentaxPreview"},
232
    {"QVMI", "CasioQVMI"},
233
    {"SDLN", "PlayMode"},
234
    {"SelO", "PlaySelection"},
235
    {"TAGS", "KodakTags/KonicaMinoltaTags/MinoltaTags/NikonTags/OlympusTags/PentaxTags/SamsungTags/SanyoMOV/SanyoMP4"},
236
    {"WLOC", "WindowLocation"},
237
    {"XMP_", "XMP"},
238
    {"Xtra", "Xtra"},
239
    {"hinf", "HintTrackInfo"},
240
    {"hinv", "HintVersion"},
241
    {"hnti", "Hint"},
242
    {"meta", "Meta"},
243
    {"name", "Name"},
244
    {"ptv ", "PrintToVideo"},
245
    {"scrn", "OlympusPreview"},
246
    {"thmb", "MakerNotePentax5a/OlympusThumbnail"},
247
};
248
249
static constexpr TagVocabulary userDataReferencetags[] = {
250
    {"CNCV", "Xmp.video.CompressorVersion"},
251
    {"CNFV", "Xmp.video.FirmwareVersion"},
252
    {"CNMN", "Xmp.video.Model"},
253
    {"NCHD", "Xmp.video.MakerNoteType"},
254
    {"WLOC", "Xmp.video.WindowLocation"},
255
    {"SDLN", "Xmp.video.PlayMode"},
256
    {"FFMV", "Xmp.video.StreamName"},
257
    {"SelO", "Xmp.video.PlaySelection"},
258
    {"name", "Xmp.video.Name"},
259
    {"vndr", "Xmp.video.Vendor"},
260
    {" ART", "Xmp.video.Artist"},
261
    {" alb", "Xmp.video.Album"},
262
    {" arg", "Xmp.video.Arranger"},
263
    {" ark", "Xmp.video.ArrangerKeywords"},
264
    {" cmt", "Xmp.video.Comment"},
265
    {" cok", "Xmp.video.ComposerKeywords"},
266
    {" com", "Xmp.video.Composer"},
267
    {" cpy", "Xmp.video.Copyright"},
268
    {" day", "Xmp.video.CreateDate"},
269
    {" dir", "Xmp.video.Director"},
270
    {" ed1", "Xmp.video.Edit1"},
271
    {" ed2", "Xmp.video.Edit2"},
272
    {" ed3", "Xmp.video.Edit3"},
273
    {" ed4", "Xmp.video.Edit4"},
274
    {" ed5", "Xmp.video.Edit5"},
275
    {" ed6", "Xmp.video.Edit6"},
276
    {" ed7", "Xmp.video.Edit7"},
277
    {" ed8", "Xmp.video.Edit8"},
278
    {" ed9", "Xmp.video.Edit9"},
279
    {" enc", "Xmp.video.Encoder"},
280
    {" fmt", "Xmp.video.Format"},
281
    {" gen", "Xmp.video.Genre"},
282
    {" grp", "Xmp.video.Grouping"},
283
    {" inf", "Xmp.video.Information"},
284
    {" isr", "Xmp.video.ISRCCode"},
285
    {" lab", "Xmp.video.RecordLabelName"},
286
    {" lal", "Xmp.video.RecordLabelURL"},
287
    {" lyr", "Xmp.video.Lyrics"},
288
    {" mak", "Xmp.video.Make"},
289
    {" mal", "Xmp.video.MakerURL"},
290
    {" mod", "Xmp.video.Model"},
291
    {" nam", "Xmp.video.Title"},
292
    {" pdk", "Xmp.video.ProducerKeywords"},
293
    {" phg", "Xmp.video.RecordingCopyright"},
294
    {" prd", "Xmp.video.Producer"},
295
    {" prf", "Xmp.video.Performers"},
296
    {" prk", "Xmp.video.PerformerKeywords"},
297
    {" prl", "Xmp.video.PerformerURL"},
298
    {" req", "Xmp.video.Requirements"},
299
    {" snk", "Xmp.video.SubtitleKeywords"},
300
    {" snm", "Xmp.video.Subtitle"},
301
    {" src", "Xmp.video.SourceCredits"},
302
    {" swf", "Xmp.video.SongWriter"},
303
    {" swk", "Xmp.video.SongWriterKeywords"},
304
    {" swr", "Xmp.video.SoftwareVersion"},
305
    {" too", "Xmp.video.Encoder"},
306
    {" trk", "Xmp.video.Track"},
307
    {" wrt", "Xmp.video.Composer"},
308
    {" xyz", "Xmp.video.GPSCoordinates"},
309
    {"CMbo", "Xmp.video.CameraByteOrder"},
310
    {"Cmbo", "Xmp.video.CameraByteOrder"},
311
};
312
313
static constexpr TagDetails NikonNCTGTags[] = {
314
    {0x0001, "Xmp.video.Make"},
315
    {0x0002, "Xmp.video.Model"},
316
    {0x0003, "Xmp.video.Software"},
317
    {0x0011, "Xmp.video.CreationDate"},
318
    {0x0012, "Xmp.video.DateTimeOriginal"},
319
    {0x0013, "Xmp.video.FrameCount"},
320
    {0x0016, "Xmp.video.FrameRate"},
321
    {0x0022, "Xmp.video.FrameWidth"},
322
    {0x0023, "Xmp.video.FrameHeight"},
323
    {0x0032, "Xmp.audio.channelType"},
324
    {0x0033, "Xmp.audio.BitsPerSample"},
325
    {0x0034, "Xmp.audio.sampleRate"},
326
    {0x1108822, "Xmp.video.ExposureProgram"},
327
    {0x1109204, "Xmp.video.ExposureCompensation"},
328
    {0x1109207, "Xmp.video.MeteringMode"},
329
    {0x110a434, "Xmp.video.LensModel"},
330
    {0x1200000, "Xmp.video.GPSVersionID"},
331
    {0x1200001, "Xmp.video.GPSLatitudeRef"},
332
    {0x1200002, "Xmp.video.GPSLatitude"},
333
    {0x1200003, "Xmp.video.GPSLongitudeRef"},
334
    {0x1200004, "Xmp.video.GPSLongitude"},
335
    {0x1200005, "Xmp.video.GPSAltitudeRef"},
336
    {0x1200006, "Xmp.video.GPSAltitude"},
337
    {0x1200007, "Xmp.video.GPSTimeStamp"},
338
    {0x1200008, "Xmp.video.GPSSatellites"},
339
    {0x1200010, "Xmp.video.GPSImgDirectionRef"},
340
    {0x1200011, "Xmp.video.GPSImgDirection"},
341
    {0x1200012, "Xmp.video.GPSMapDatum"},
342
    {0x120001d, "Xmp.video.GPSDateStamp"},
343
    {0x2000001, "Xmp.video.MakerNoteVersion"},
344
    {0x2000005, "Xmp.video.WhiteBalance"},
345
    {0x200000b, "Xmp.video.WhiteBalanceFineTune"},
346
    {0x200001e, "Xmp.video.ColorSpace"},
347
    {0x2000023, "Xmp.video.PictureControlData"},
348
    {0x2000024, "Xmp.video.WorldTime"},
349
    {0x200002c, "Xmp.video.UnknownInfo"},
350
    {0x2000032, "Xmp.video.UnknownInfo2"},
351
    {0x2000039, "Xmp.video.LocationInfo"},
352
    {0x2000083, "Xmp.video.LensType"},
353
    {0x2000084, "Xmp.video.LensModel"},
354
    {0x20000ab, "Xmp.video.VariProgram"},
355
};
356
357
[[maybe_unused]] static constexpr TagDetails NikonColorSpace[] = {
358
    {1, "sRGB"},
359
    {2, "Adobe RGB"},
360
};
361
362
[[maybe_unused]] static constexpr TagVocabulary NikonGPS_Latitude_Longitude_ImgDirection_Reference[] = {
363
    {"N", "North"}, {"S", "South"}, {"E", "East"}, {"W", "West"}, {"M", "Magnetic North"}, {"T", "True North"},
364
};
365
366
[[maybe_unused]] static constexpr TagDetails NikonGPSAltitudeRef[] = {
367
    {0, "Above Sea Level"},
368
    {1, "Below Sea Level"},
369
};
370
371
[[maybe_unused]] static constexpr TagDetails NikonExposureProgram[] = {
372
    {0, "Not Defined"},
373
    {1, "Manual"},
374
    {2, "Program AE"},
375
    {3, "Aperture-priority AE"},
376
    {4, "Shutter speed priority AE"},
377
    {5, "Creative (Slow speed)"},
378
    {6, "Action (High speed)"},
379
    {7, "Portrait"},
380
    {8, "Landscape"},
381
};
382
383
[[maybe_unused]] static constexpr TagDetails NikonMeteringMode[] = {
384
    {0, "Unknown"}, {1, "Average"},    {2, "Center-weighted average"},
385
    {3, "Spot"},    {4, "Multi-spot"}, {5, "Multi-segment"},
386
    {6, "Partial"}, {255, "Other"},
387
};
388
389
static constexpr TagDetails PictureControlAdjust[] = {
390
    {0, "Default Settings"},
391
    {1, "Quick Adjust"},
392
    {2, "Full Control"},
393
};
394
395
//! Contrast and Sharpness
396
static constexpr TagDetails NormalSoftHard[] = {
397
    {0, "Normal"},
398
    {1, "Soft"},
399
    {2, "Hard"},
400
};
401
402
//! Saturation
403
static constexpr TagDetails Saturation[] = {
404
    {0, "Normal"},
405
    {1, "Low"},
406
    {2, "High"},
407
};
408
409
//! YesNo, used for DaylightSavings
410
static constexpr TagDetails YesNo[] = {
411
    {0, "No"},
412
    {1, "Yes"},
413
};
414
415
//! DateDisplayFormat
416
static constexpr TagDetails DateDisplayFormat[] = {
417
    {0, "Y/M/D"},
418
    {1, "M/D/Y"},
419
    {2, "D/M/Y"},
420
};
421
422
static constexpr TagDetails FilterEffect[] = {
423
    {0x80, "Off"}, {0x81, "Yellow"}, {0x82, "Orange"}, {0x83, "Red"}, {0x84, "Green"}, {0xff, "n/a"},
424
};
425
426
static constexpr TagDetails ToningEffect[] = {
427
    {0x80, "B&W"},         {0x81, "Sepia"},      {0x82, "Cyanotype"},  {0x83, "Red"},
428
    {0x84, "Yellow"},      {0x85, "Green"},      {0x86, "Blue-green"}, {0x87, "Blue"},
429
    {0x88, "Purple-blue"}, {0x89, "Red-purple"}, {0xff, "n/a"},
430
};
431
432
static constexpr TagDetails whiteBalance[] = {
433
    {0, "Auto"}, {1, "Daylight"}, {2, "Shade"}, {3, "Fluorescent"}, {4, "Tungsten"}, {5, "Manual"},
434
};
435
436
enum movieHeaderTags {
437
  MovieHeaderVersion,
438
  CreateDate,
439
  ModifyDate,
440
  TimeScale,
441
  Duration,
442
  PreferredRate,
443
  PreferredVolume,
444
  PreviewTime = 18,
445
  PreviewDuration,
446
  PosterTime,
447
  SelectionTime,
448
  SelectionDuration,
449
  CurrentTime,
450
  NextTrackID
451
};
452
enum trackHeaderTags {
453
  TrackHeaderVersion,
454
  TrackCreateDate,
455
  TrackModifyDate,
456
  TrackID,
457
  TrackDuration = 5,
458
  TrackLayer = 8,
459
  TrackVolume,
460
  ImageWidth = 19,
461
  ImageHeight
462
};
463
enum mediaHeaderTags {
464
  MediaHeaderVersion,
465
  MediaCreateDate,
466
  MediaModifyDate,
467
  MediaTimeScale,
468
  MediaDuration,
469
  MediaLanguageCode
470
};
471
enum handlerTags { HandlerClass = 1, HandlerType, HandlerVendorID };
472
enum videoHeaderTags { GraphicsMode = 2, OpColor };
473
enum stream { Video, Audio, Hint, Null, GenMediaHeader };
474
enum imageDescTags {
475
  codec,
476
  VendorID = 4,
477
  SourceImageWidth_Height = 7,
478
  XResolution,
479
  YResolution,
480
  CompressorName = 10,
481
  BitDepth
482
};
483
enum audioDescTags { AudioFormat, AudioVendorID = 4, AudioChannels, AudioSampleRate = 7, MOV_AudioFormat = 13 };
484
485
/*!
486
  @brief Function used to check equality of a Tags with a
487
      particular string (ignores case while comparing).
488
  @param buf Data buffer that will contain Tag to compare
489
  @param str char* Pointer to string
490
  @return Returns true if the buffer value is equal to string.
491
 */
492
3.21M
static bool equalsQTimeTag(Exiv2::DataBuf& buf, const char str[5]) {
493
3.21M
  return std::equal(buf.begin(), buf.begin() + 4, str,
494
3.77M
                    [](auto b, auto s) { return std::tolower(b) == std::tolower(s); });
495
3.21M
}
496
497
/*!
498
  @brief Function used to ignore Tags and values stored in them,
499
      since they are not necessary as metadata information
500
  @param buf Data buffer that will contain Tag to compare
501
  @return Returns true, if Tag is found in the ignoreList[]
502
 */
503
148k
static bool ignoreList(Exiv2::DataBuf& buf) {
504
148k
  const char ignoreList[13][5] = {
505
148k
      "mdat", "edts", "junk", "iods", "alis", "stsc", "stsz", "stco", "ctts", "stss", "skip", "wide", "cmvd",
506
148k
  };
507
508
148k
  for (auto i : ignoreList)
509
1.85M
    if (equalsQTimeTag(buf, i))
510
12.2k
      return true;
511
512
136k
  return false;
513
148k
}
514
515
/*!
516
  @brief Function used to ignore Tags, basically Tags which
517
      contain other tags inside them, since they are not necessary
518
      as metadata information
519
  @param buf Data buffer that will contain Tag to compare
520
  @return Returns true, if Tag is found in the ignoreList[]
521
 */
522
69.5k
static bool dataIgnoreList(Exiv2::DataBuf& buf) {
523
69.5k
  const char ignoreList[8][5] = {
524
69.5k
      "moov", "mdia", "minf", "dinf", "alis", "stbl", "cmov", "meta",
525
69.5k
  };
526
527
69.5k
  for (auto i : ignoreList)
528
524k
    if (equalsQTimeTag(buf, i))
529
10.8k
      return true;
530
531
58.7k
  return false;
532
69.5k
}
533
}  // namespace Exiv2::Internal
534
535
namespace Exiv2 {
536
537
using namespace Exiv2::Internal;
538
539
QuickTimeVideo::QuickTimeVideo(BasicIo::UniquePtr io, size_t max_recursion_depth) :
540
3.06k
    Image(ImageType::qtime, mdNone, std::move(io)),
541
3.06k
    mvhdTimeScale_(1),
542
3.06k
    mdhdTimeScale_(1),
543
3.06k
    currentStream_(Null),
544
3.06k
    max_recursion_depth_(max_recursion_depth) {
545
3.06k
}  // QuickTimeVideo::QuickTimeVideo
546
547
3.05k
std::string QuickTimeVideo::mimeType() const {
548
3.05k
  return "video/quicktime";
549
3.05k
}
550
551
256
void QuickTimeVideo::writeMetadata() {
552
256
}
553
554
3.05k
void QuickTimeVideo::readMetadata() {
555
3.05k
  if (io_->open() != 0)
556
0
    throw Error(ErrorCode::kerDataSourceOpenFailed, io_->path(), strError());
557
558
  // Ensure that this is the correct image type
559
3.05k
  if (!isQTimeType(*io_, false)) {
560
0
    if (io_->error() || io_->eof())
561
0
      throw Error(ErrorCode::kerFailedToReadImageData);
562
0
    throw Error(ErrorCode::kerNotAnImage, "QuickTime");
563
0
  }
564
565
3.05k
  IoCloser closer(*io_);
566
3.05k
  clearMetadata();
567
3.05k
  continueTraversing_ = true;
568
3.05k
  height_ = width_ = 1;
569
570
3.05k
  xmpData_["Xmp.video.FileSize"] = static_cast<double>(io_->size()) / 1048576.0;
571
3.05k
  xmpData_["Xmp.video.MimeType"] = mimeType();
572
573
59.2k
  while (continueTraversing_)
574
56.2k
    decodeBlock(0);
575
576
3.05k
  xmpData_["Xmp.video.AspectRatio"] = getAspectRatio(width_, height_);
577
3.05k
}  // QuickTimeVideo::readMetadata
578
579
81.2k
void QuickTimeVideo::decodeBlock(size_t recursion_depth, std::string const& entered_from) {
580
81.2k
  enforce(recursion_depth < max_recursion_depth_, Exiv2::ErrorCode::kerCorruptedMetadata);
581
582
81.2k
  const long bufMinSize = 4;
583
81.2k
  DataBuf buf(bufMinSize + 1);
584
81.2k
  uint64_t size = 0;
585
81.2k
  buf.data()[4] = '\0';
586
587
81.2k
  io_->read(buf.data(), 4);
588
81.2k
  if (io_->eof()) {
589
367
    continueTraversing_ = false;
590
367
    return;
591
367
  }
592
593
80.8k
  size = buf.read_uint32(0, bigEndian);
594
595
80.8k
  io_->readOrThrow(buf.data(), 4);
596
597
  // we have read 2x 4 bytes
598
80.8k
  size_t hdrsize = 8;
599
600
80.8k
  if (size == 1) {
601
    // The box size is encoded as a uint64_t, so we need to read another 8 bytes.
602
137
    DataBuf data(8);
603
137
    hdrsize += 8;
604
137
    io_->readOrThrow(data.data(), data.size());
605
137
    size = data.read_uint64(0, bigEndian);
606
80.7k
  } else if (size == 0 && entered_from == "meta") {
607
2.40k
    size = buf.read_uint32(0, bigEndian);
608
2.40k
    io_->readOrThrow(buf.data(), 4, Exiv2::ErrorCode::kerCorruptedMetadata);
609
2.40k
  }
610
611
80.8k
  enforce(size >= hdrsize, Exiv2::ErrorCode::kerCorruptedMetadata);
612
80.8k
  enforce(size - hdrsize <= io_->size() - io_->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
613
80.8k
  enforce(size - hdrsize <= std::numeric_limits<size_t>::max(), Exiv2::ErrorCode::kerCorruptedMetadata);
614
615
  // std::cerr<<"Tag=>"<<buf.data()<<"     size=>"<<size-hdrsize << '\n';
616
80.8k
  const auto newsize = static_cast<size_t>(size - hdrsize);
617
80.8k
  if (ignoreList(buf)) {
618
12.2k
    discard(newsize);
619
12.2k
    return;
620
12.2k
  }
621
68.6k
  if (newsize > buf.size()) {
622
56.9k
    buf.resize(newsize);
623
56.9k
  }
624
68.6k
  tagDecoder(buf, newsize, recursion_depth + 1);
625
68.6k
}  // QuickTimeVideo::decodeBlock
626
627
4.15k
static std::string readString(BasicIo& io, size_t size) {
628
4.15k
  enforce(size <= io.size() - io.tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
629
4.15k
  Exiv2::DataBuf str(size + 1);
630
4.15k
  io.readOrThrow(str.data(), size);
631
4.15k
  str.write_uint8(size, 0);  // nul-terminate string
632
4.15k
  return Exiv2::toString(str.data());
633
4.15k
}
634
635
69.5k
void QuickTimeVideo::tagDecoder(Exiv2::DataBuf& buf, size_t size, size_t recursion_depth) {
636
69.5k
  enforce(recursion_depth < max_recursion_depth_, Exiv2::ErrorCode::kerCorruptedMetadata);
637
69.5k
  assert(buf.size() > 4);
638
639
69.5k
  if (ignoreList(buf))
640
0
    discard(size);
641
642
69.5k
  else if (dataIgnoreList(buf)) {
643
10.8k
    decodeBlock(recursion_depth + 1, Exiv2::toString(buf.data()));
644
58.7k
  } else if (equalsQTimeTag(buf, "ftyp"))
645
3.22k
    fileTypeDecoder(size);
646
647
55.4k
  else if (equalsQTimeTag(buf, "trak"))
648
3.35k
    setMediaStream();
649
650
52.1k
  else if (equalsQTimeTag(buf, "mvhd"))
651
196
    movieHeaderDecoder(size);
652
653
51.9k
  else if (equalsQTimeTag(buf, "tkhd"))
654
2.23k
    trackHeaderDecoder(size);
655
656
49.7k
  else if (equalsQTimeTag(buf, "mdhd"))
657
3.34k
    mediaHeaderDecoder(size);
658
659
46.3k
  else if (equalsQTimeTag(buf, "hdlr"))
660
8.94k
    handlerDecoder(size);
661
662
37.4k
  else if (equalsQTimeTag(buf, "vmhd"))
663
4.08k
    videoHeaderDecoder(size);
664
665
33.3k
  else if (equalsQTimeTag(buf, "udta"))
666
9.42k
    userDataDecoder(size, recursion_depth + 1);
667
668
23.9k
  else if (equalsQTimeTag(buf, "dref"))
669
2.38k
    multipleEntriesDecoder(recursion_depth + 1);
670
671
21.5k
  else if (equalsQTimeTag(buf, "stsd"))
672
2.23k
    sampleDesc(size);
673
674
19.2k
  else if (equalsQTimeTag(buf, "stts"))
675
2.11k
    timeToSampleDecoder();
676
677
17.1k
  else if (equalsQTimeTag(buf, "pnot"))
678
1.14k
    previewTagDecoder(size);
679
680
16.0k
  else if (equalsQTimeTag(buf, "tapt"))
681
873
    trackApertureTagDecoder(size);
682
683
15.1k
  else if (equalsQTimeTag(buf, "keys"))
684
800
    keysTagDecoder(size);
685
686
14.3k
  else if (equalsQTimeTag(buf, "url ")) {
687
227
    if (currentStream_ == Video)
688
95
      xmpData_["Xmp.video.URL"] = readString(*io_, size);
689
132
    else if (currentStream_ == Audio)
690
87
      xmpData_["Xmp.audio.URL"] = readString(*io_, size);
691
45
    else
692
45
      discard(size);
693
227
  }
694
695
14.1k
  else if (equalsQTimeTag(buf, "urn ")) {
696
3.50k
    if (currentStream_ == Video)
697
2.45k
      xmpData_["Xmp.video.URN"] = readString(*io_, size);
698
1.05k
    else if (currentStream_ == Audio)
699
780
      xmpData_["Xmp.audio.URN"] = readString(*io_, size);
700
275
    else
701
275
      discard(size);
702
3.50k
  }
703
704
10.6k
  else if (equalsQTimeTag(buf, "dcom")) {
705
176
    xmpData_["Xmp.video.Compressor"] = readString(*io_, size);
706
176
  }
707
708
10.4k
  else if (equalsQTimeTag(buf, "smhd")) {
709
502
    io_->readOrThrow(buf.data(), 4);
710
502
    io_->readOrThrow(buf.data(), 4);
711
502
    xmpData_["Xmp.audio.Balance"] = buf.read_uint16(0, bigEndian);
712
502
  }
713
714
9.94k
  else {
715
9.94k
    discard(size);
716
9.94k
  }
717
69.5k
}  // QuickTimeVideo::tagDecoder
718
719
22.4k
void QuickTimeVideo::discard(size_t size) {
720
22.4k
  size_t cur_pos = io_->tell();
721
22.4k
  io_->seek(cur_pos + size, BasicIo::beg);
722
22.4k
}  // QuickTimeVideo::discard
723
724
1.14k
void QuickTimeVideo::previewTagDecoder(size_t size) {
725
1.14k
  DataBuf buf(4);
726
1.14k
  size_t cur_pos = io_->tell();
727
1.14k
  io_->readOrThrow(buf.data(), 4);
728
1.14k
  xmpData_["Xmp.video.PreviewDate"] = buf.read_uint32(0, bigEndian);
729
1.14k
  io_->readOrThrow(buf.data(), 2);
730
1.14k
  xmpData_["Xmp.video.PreviewVersion"] = getShort(buf.data(), bigEndian);
731
732
1.14k
  io_->readOrThrow(buf.data(), 4);
733
1.14k
  if (equalsQTimeTag(buf, "PICT"))
734
6
    xmpData_["Xmp.video.PreviewAtomType"] = "QuickDraw Picture";
735
1.14k
  else
736
1.14k
    xmpData_["Xmp.video.PreviewAtomType"] = std::string{buf.c_str(), 4};
737
738
1.14k
  io_->seek(cur_pos + size, BasicIo::beg);
739
1.14k
}  // QuickTimeVideo::previewTagDecoder
740
741
800
void QuickTimeVideo::keysTagDecoder(size_t size) {
742
800
  DataBuf buf(4);
743
800
  size_t cur_pos = io_->tell();
744
800
  io_->readOrThrow(buf.data(), 4);
745
800
  xmpData_["Xmp.video.PreviewDate"] = buf.read_uint32(0, bigEndian);
746
800
  io_->readOrThrow(buf.data(), 2);
747
800
  xmpData_["Xmp.video.PreviewVersion"] = getShort(buf.data(), bigEndian);
748
749
800
  io_->readOrThrow(buf.data(), 4);
750
800
  if (equalsQTimeTag(buf, "PICT"))
751
6
    xmpData_["Xmp.video.PreviewAtomType"] = "QuickDraw Picture";
752
794
  else
753
794
    xmpData_["Xmp.video.PreviewAtomType"] = std::string{buf.c_str(), 4};
754
755
800
  io_->seek(cur_pos + size, BasicIo::beg);
756
800
}  // QuickTimeVideo::keysTagDecoder
757
758
873
void QuickTimeVideo::trackApertureTagDecoder(size_t size) {
759
873
  DataBuf buf(4);
760
873
  DataBuf buf2(2);
761
873
  size_t cur_pos = io_->tell();
762
873
  byte n = 3;
763
764
3.48k
  while (n--) {
765
2.61k
    io_->seek(4L, BasicIo::cur);
766
2.61k
    io_->readOrThrow(buf.data(), 4);
767
768
2.61k
    if (equalsQTimeTag(buf, "clef")) {
769
43
      io_->seek(4L, BasicIo::cur);
770
43
      io_->readOrThrow(buf.data(), 2);
771
43
      io_->readOrThrow(buf2.data(), 2);
772
43
      xmpData_["Xmp.video.CleanApertureWidth"] =
773
43
          stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
774
43
      io_->readOrThrow(buf.data(), 2);
775
43
      io_->readOrThrow(buf2.data(), 2);
776
43
      xmpData_["Xmp.video.CleanApertureHeight"] =
777
43
          stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
778
43
    }
779
780
2.56k
    else if (equalsQTimeTag(buf, "prof")) {
781
93
      io_->seek(4L, BasicIo::cur);
782
93
      io_->readOrThrow(buf.data(), 2);
783
93
      io_->readOrThrow(buf2.data(), 2);
784
93
      xmpData_["Xmp.video.ProductionApertureWidth"] =
785
93
          stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
786
93
      io_->readOrThrow(buf.data(), 2);
787
93
      io_->readOrThrow(buf2.data(), 2);
788
93
      xmpData_["Xmp.video.ProductionApertureHeight"] =
789
93
          stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
790
93
    }
791
792
2.47k
    else if (equalsQTimeTag(buf, "enof")) {
793
36
      io_->seek(4L, BasicIo::cur);
794
36
      io_->readOrThrow(buf.data(), 2);
795
36
      io_->readOrThrow(buf2.data(), 2);
796
36
      xmpData_["Xmp.video.EncodedPixelsWidth"] =
797
36
          stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
798
36
      io_->readOrThrow(buf.data(), 2);
799
36
      io_->readOrThrow(buf2.data(), 2);
800
36
      xmpData_["Xmp.video.EncodedPixelsHeight"] =
801
36
          stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
802
36
    }
803
2.61k
  }
804
873
  io_->seek(cur_pos + size, BasicIo::beg);
805
873
}  // QuickTimeVideo::trackApertureTagDecoder
806
807
1.60k
void QuickTimeVideo::CameraTagsDecoder(size_t size) {
808
1.60k
  size_t cur_pos = io_->tell();
809
1.60k
  DataBuf buf(50);
810
1.60k
  DataBuf buf2(4);
811
812
1.60k
  io_->readOrThrow(buf.data(), 4);
813
1.60k
  if (equalsQTimeTag(buf, "NIKO")) {
814
1.33k
    io_->seek(cur_pos, BasicIo::beg);
815
816
1.33k
    io_->readOrThrow(buf.data(), 24);
817
1.33k
    xmpData_["Xmp.video.Make"] = buf.data();
818
1.33k
    io_->readOrThrow(buf.data(), 14);
819
1.33k
    xmpData_["Xmp.video.Model"] = buf.data();
820
1.33k
    io_->readOrThrow(buf.data(), 4);
821
1.33k
    xmpData_["Xmp.video.ExposureTime"] = stringFormat("1/{}", std::ceil(buf.read_uint32(0, littleEndian) / 10.0));
822
1.33k
    io_->readOrThrow(buf.data(), 4);
823
1.33k
    io_->readOrThrow(buf2.data(), 4);
824
1.33k
    xmpData_["Xmp.video.FNumber"] =
825
1.33k
        buf.read_uint32(0, littleEndian) / static_cast<double>(buf2.read_uint32(0, littleEndian));
826
1.33k
    io_->readOrThrow(buf.data(), 4);
827
1.33k
    io_->readOrThrow(buf2.data(), 4);
828
1.33k
    xmpData_["Xmp.video.ExposureCompensation"] =
829
1.33k
        buf.read_uint32(0, littleEndian) / static_cast<double>(buf2.read_uint32(0, littleEndian));
830
1.33k
    io_->readOrThrow(buf.data(), 10);
831
1.33k
    io_->readOrThrow(buf.data(), 4);
832
1.33k
    if (auto td = Exiv2::find(whiteBalance, buf.read_uint32(0, littleEndian)))
833
35
      xmpData_["Xmp.video.WhiteBalance"] = _(td->label_);
834
1.33k
    io_->readOrThrow(buf.data(), 4);
835
1.33k
    io_->readOrThrow(buf2.data(), 4);
836
1.33k
    xmpData_["Xmp.video.FocalLength"] =
837
1.33k
        buf.read_uint32(0, littleEndian) / static_cast<double>(buf2.read_uint32(0, littleEndian));
838
1.33k
    io_->seek(95L, BasicIo::cur);
839
1.33k
    io_->readOrThrow(buf.data(), 48);
840
1.33k
    buf.write_uint8(48, 0);
841
1.33k
    xmpData_["Xmp.video.Software"] = buf.data();
842
1.33k
    io_->readOrThrow(buf.data(), 4);
843
1.33k
    xmpData_["Xmp.video.ISO"] = buf.read_uint32(0, littleEndian);
844
1.33k
  }
845
846
1.60k
  io_->seek(cur_pos + size, BasicIo::beg);
847
1.60k
}  // QuickTimeVideo::CameraTagsDecoder
848
849
9.55k
void QuickTimeVideo::userDataDecoder(size_t outer_size, size_t recursion_depth) {
850
9.55k
  enforce(recursion_depth < max_recursion_depth_, Exiv2::ErrorCode::kerCorruptedMetadata);
851
9.55k
  size_t cur_pos = io_->tell();
852
9.55k
  const TagVocabulary* td;
853
9.55k
  const TagVocabulary* tv;
854
9.55k
  const TagVocabulary* tv_internal;
855
856
9.55k
  const long bufMinSize = 100;
857
9.55k
  DataBuf buf(bufMinSize);
858
9.55k
  size_t size_internal = outer_size;
859
9.55k
  std::memset(buf.data(), 0x0, buf.size());
860
861
17.3k
  while ((size_internal / 4 != 0) && (size_internal > 0)) {
862
13.5k
    buf.data()[4] = '\0';
863
13.5k
    io_->readOrThrow(buf.data(), 4);
864
13.5k
    const size_t size = buf.read_uint32(0, bigEndian);
865
13.5k
    if (size > size_internal)
866
2.86k
      break;
867
10.6k
    size_internal -= size;
868
10.6k
    io_->readOrThrow(buf.data(), 4);
869
870
10.6k
    if (buf.data()[0] == 169)
871
70
      buf.data()[0] = ' ';
872
10.6k
    td = Exiv2::find(userDatatags, Exiv2::toString(buf.data()));
873
874
10.6k
    tv = Exiv2::find(userDataReferencetags, Exiv2::toString(buf.data()));
875
876
10.6k
    if (size <= 12)
877
2.87k
      break;
878
879
7.76k
    if (equalsQTimeTag(buf, "DcMD") || equalsQTimeTag(buf, "NCDT"))
880
124
      userDataDecoder(size - 8, recursion_depth + 1);
881
882
7.64k
    else if (equalsQTimeTag(buf, "NCTG"))
883
1.59k
      NikonTagsDecoder(size - 8);
884
885
6.05k
    else if (equalsQTimeTag(buf, "TAGS"))
886
1.60k
      CameraTagsDecoder(size - 8);
887
888
4.45k
    else if (equalsQTimeTag(buf, "CNCV") || equalsQTimeTag(buf, "CNFV") || equalsQTimeTag(buf, "CNMN") ||
889
4.40k
             equalsQTimeTag(buf, "NCHD") || equalsQTimeTag(buf, "FFMV")) {
890
157
      enforce(tv, Exiv2::ErrorCode::kerCorruptedMetadata);
891
157
      xmpData_[_(tv->label_)] = readString(*io_, size - 8);
892
157
    }
893
894
4.29k
    else if (equalsQTimeTag(buf, "CMbo") || equalsQTimeTag(buf, "Cmbo")) {
895
91
      enforce(tv, Exiv2::ErrorCode::kerCorruptedMetadata);
896
91
      io_->readOrThrow(buf.data(), 2);
897
91
      buf.data()[2] = '\0';
898
91
      tv_internal = Exiv2::find(cameraByteOrderTags, Exiv2::toString(buf.data()));
899
900
91
      if (tv_internal)
901
35
        xmpData_[_(tv->label_)] = _(tv_internal->label_);
902
56
      else
903
56
        xmpData_[_(tv->label_)] = buf.data();
904
91
    }
905
906
4.20k
    else if (tv) {
907
405
      io_->readOrThrow(buf.data(), 4);
908
405
      xmpData_[_(tv->label_)] = readString(*io_, size - 12);
909
405
    }
910
911
3.79k
    else if (td)
912
2.39k
      tagDecoder(buf, size - 8, recursion_depth + 1);
913
7.76k
  }
914
915
9.55k
  io_->seek(cur_pos + outer_size, BasicIo::beg);
916
9.55k
}  // QuickTimeVideo::userDataDecoder
917
918
1.59k
void QuickTimeVideo::NikonTagsDecoder(size_t size) {
919
1.59k
  size_t cur_pos = io_->tell();
920
1.59k
  DataBuf buf(201);
921
1.59k
  DataBuf buf2(4 + 1);
922
1.59k
  uint32_t TagID = 0;
923
1.59k
  uint16_t dataLength = 0;
924
1.59k
  uint16_t dataType = 2;
925
1.59k
  const TagDetails* td;
926
1.59k
  const TagDetails* td2;
927
928
115k
  for (int i = 0; i < 100; i++) {
929
114k
    io_->readOrThrow(buf.data(), 4);
930
114k
    TagID = buf.read_uint32(0, bigEndian);
931
114k
    td = Exiv2::find(NikonNCTGTags, TagID);
932
933
114k
    io_->readOrThrow(buf.data(), 2);
934
114k
    dataType = buf.read_uint16(0, bigEndian);
935
936
114k
    std::memset(buf.data(), 0x0, buf.size());
937
114k
    io_->readOrThrow(buf.data(), 2);
938
939
114k
    if (TagID == 0x2000023) {
940
4.62k
      size_t local_pos = io_->tell();
941
4.62k
      dataLength = buf.read_uint16(0, bigEndian);
942
4.62k
      std::memset(buf.data(), 0x0, buf.size());
943
944
4.62k
      io_->readOrThrow(buf.data(), 4);
945
4.62k
      xmpData_["Xmp.video.PictureControlVersion"] = buf.data();
946
4.62k
      io_->readOrThrow(buf.data(), 20);
947
4.62k
      xmpData_["Xmp.video.PictureControlName"] = buf.data();
948
4.62k
      io_->readOrThrow(buf.data(), 20);
949
4.62k
      xmpData_["Xmp.video.PictureControlBase"] = buf.data();
950
4.62k
      io_->readOrThrow(buf.data(), 4);
951
4.62k
      std::memset(buf.data(), 0x0, buf.size());
952
953
4.62k
      io_->readOrThrow(buf.data(), 1);
954
4.62k
      td2 = Exiv2::find(PictureControlAdjust, static_cast<int>(buf.data()[0]) & 7);
955
4.62k
      if (td2)
956
2.84k
        xmpData_["Xmp.video.PictureControlAdjust"] = _(td2->label_);
957
1.78k
      else
958
1.78k
        xmpData_["Xmp.video.PictureControlAdjust"] = static_cast<int>(buf.data()[0]) & 7;
959
960
4.62k
      io_->readOrThrow(buf.data(), 1);
961
4.62k
      td2 = Exiv2::find(NormalSoftHard, static_cast<int>(buf.data()[0]) & 7);
962
4.62k
      if (td2)
963
2.88k
        xmpData_["Xmp.video.PictureControlQuickAdjust"] = _(td2->label_);
964
965
4.62k
      io_->readOrThrow(buf.data(), 1);
966
4.62k
      td2 = Exiv2::find(NormalSoftHard, static_cast<int>(buf.data()[0]) & 7);
967
4.62k
      if (td2)
968
2.93k
        xmpData_["Xmp.video.Sharpness"] = _(td2->label_);
969
1.68k
      else
970
1.68k
        xmpData_["Xmp.video.Sharpness"] = static_cast<int>(buf.data()[0]) & 7;
971
972
4.62k
      io_->readOrThrow(buf.data(), 1);
973
4.62k
      td2 = Exiv2::find(NormalSoftHard, static_cast<int>(buf.data()[0]) & 7);
974
4.62k
      if (td2)
975
1.51k
        xmpData_["Xmp.video.Contrast"] = _(td2->label_);
976
3.10k
      else
977
3.10k
        xmpData_["Xmp.video.Contrast"] = static_cast<int>(buf.data()[0]) & 7;
978
979
4.62k
      io_->readOrThrow(buf.data(), 1);
980
4.62k
      td2 = Exiv2::find(NormalSoftHard, static_cast<int>(buf.data()[0]) & 7);
981
4.62k
      if (td2)
982
2.54k
        xmpData_["Xmp.video.Brightness"] = _(td2->label_);
983
2.07k
      else
984
2.07k
        xmpData_["Xmp.video.Brightness"] = static_cast<int>(buf.data()[0]) & 7;
985
986
4.62k
      io_->readOrThrow(buf.data(), 1);
987
4.62k
      td2 = Exiv2::find(Saturation, static_cast<int>(buf.data()[0]) & 7);
988
4.62k
      if (td2)
989
1.72k
        xmpData_["Xmp.video.Saturation"] = _(td2->label_);
990
2.90k
      else
991
2.90k
        xmpData_["Xmp.video.Saturation"] = static_cast<int>(buf.data()[0]) & 7;
992
993
4.62k
      io_->readOrThrow(buf.data(), 1);
994
4.62k
      xmpData_["Xmp.video.HueAdjustment"] = static_cast<int>(buf.data()[0]) & 7;
995
996
4.62k
      io_->readOrThrow(buf.data(), 1);
997
4.62k
      td2 = Exiv2::find(FilterEffect, static_cast<int>(buf.data()[0]));
998
4.62k
      if (td2)
999
990
        xmpData_["Xmp.video.FilterEffect"] = _(td2->label_);
1000
3.63k
      else
1001
3.63k
        xmpData_["Xmp.video.FilterEffect"] = static_cast<int>(buf.data()[0]);
1002
1003
4.62k
      io_->readOrThrow(buf.data(), 1);
1004
4.62k
      td2 = Exiv2::find(ToningEffect, static_cast<int>(buf.data()[0]));
1005
4.62k
      if (td2)
1006
1.39k
        xmpData_["Xmp.video.ToningEffect"] = _(td2->label_);
1007
3.23k
      else
1008
3.23k
        xmpData_["Xmp.video.ToningEffect"] = static_cast<int>(buf.data()[0]);
1009
1010
4.62k
      io_->readOrThrow(buf.data(), 1);
1011
4.62k
      xmpData_["Xmp.video.ToningSaturation"] = static_cast<int>(buf.data()[0]);
1012
1013
4.62k
      io_->seek(local_pos + dataLength, BasicIo::beg);
1014
4.62k
    }
1015
1016
109k
    else if (TagID == 0x2000024) {
1017
474
      size_t local_pos = io_->tell();
1018
474
      dataLength = buf.read_uint16(0, bigEndian);
1019
474
      std::memset(buf.data(), 0x0, buf.size());
1020
1021
474
      io_->readOrThrow(buf.data(), 2);
1022
474
      xmpData_["Xmp.video.TimeZone"] = Exiv2::getShort(buf.data(), bigEndian);
1023
474
      io_->readOrThrow(buf.data(), 1);
1024
474
      td2 = Exiv2::find(YesNo, static_cast<int>(buf.data()[0]));
1025
474
      if (td2)
1026
273
        xmpData_["Xmp.video.DayLightSavings"] = _(td2->label_);
1027
1028
474
      io_->readOrThrow(buf.data(), 1);
1029
474
      td2 = Exiv2::find(DateDisplayFormat, static_cast<int>(buf.data()[0]));
1030
474
      if (td2)
1031
330
        xmpData_["Xmp.video.DateDisplayFormat"] = _(td2->label_);
1032
1033
474
      io_->seek(local_pos + dataLength, BasicIo::beg);
1034
474
    }
1035
1036
109k
    else if (dataType == 2 || dataType == 7) {
1037
1.42k
      dataLength = buf.read_uint16(0, bigEndian);
1038
1.42k
      std::memset(buf.data(), 0x0, buf.size());
1039
1040
      // Sanity check with an "unreasonably" large number
1041
1.42k
      if (dataLength >= buf.size()) {
1042
496
#ifndef SUPPRESS_WARNINGS
1043
496
        EXV_ERROR << "Xmp.video Nikon Tags, dataLength was found to be larger than 200."
1044
0
                  << " Entries considered invalid. Not Processed.\n";
1045
496
#endif
1046
496
        io_->seek(io_->tell() + dataLength, BasicIo::beg);
1047
496
        buf.data()[0] = '\0';
1048
929
      } else {
1049
929
        io_->readOrThrow(buf.data(), dataLength);
1050
929
        buf.data()[dataLength] = '\0';
1051
929
      }
1052
1053
1.42k
      if (td) {
1054
327
        xmpData_[_(td->label_)] = buf.data();
1055
327
      }
1056
107k
    } else if (dataType == 4) {
1057
985
      dataLength = buf.read_uint16(0, bigEndian) * 4;
1058
985
      std::memset(buf.data(), 0x0, buf.size());
1059
985
      io_->readOrThrow(buf.data(), 4);
1060
985
      if (td)
1061
290
        xmpData_[_(td->label_)] = buf.read_uint32(0, bigEndian);
1062
1063
      // Sanity check with an "unreasonably" large number
1064
985
      if (dataLength > 200 || dataLength < 4) {
1065
752
#ifndef SUPPRESS_WARNINGS
1066
752
        EXV_ERROR << "Xmp.video Nikon Tags, dataLength was found to be of inappropriate size."
1067
0
                  << " Entries considered invalid. Not Processed.\n";
1068
752
#endif
1069
752
        io_->seek(io_->tell() + dataLength - 4, BasicIo::beg);
1070
752
      } else
1071
233
        io_->readOrThrow(buf.data(), dataLength - 4);
1072
106k
    } else if (dataType == 3) {
1073
2.23k
      dataLength = buf.read_uint16(0, bigEndian) * 2;
1074
2.23k
      std::memset(buf.data(), 0x0, buf.size());
1075
2.23k
      io_->readOrThrow(buf.data(), 2);
1076
2.23k
      if (td)
1077
951
        xmpData_[_(td->label_)] = buf.read_uint16(0, bigEndian);
1078
1079
      // Sanity check with an "unreasonably" large number
1080
2.23k
      if (dataLength > 200 || dataLength < 2) {
1081
1.92k
#ifndef SUPPRESS_WARNINGS
1082
1.92k
        EXV_ERROR << "Xmp.video Nikon Tags, dataLength was found to be of inappropriate size."
1083
0
                  << " Entries considered invalid. Not Processed.\n";
1084
1.92k
#endif
1085
1.92k
        io_->seek(io_->tell() + dataLength - 2, BasicIo::beg);
1086
1.92k
      } else
1087
310
        io_->readOrThrow(buf.data(), dataLength - 2);
1088
104k
    } else if (dataType == 5) {
1089
601
      dataLength = buf.read_uint16(0, bigEndian) * 8;
1090
601
      std::memset(buf.data(), 0x0, buf.size());
1091
601
      io_->readOrThrow(buf.data(), 4);
1092
601
      io_->readOrThrow(buf2.data(), 4);
1093
601
      if (td)
1094
342
        xmpData_[_(td->label_)] =
1095
342
            static_cast<double>(buf.read_uint32(0, bigEndian)) / static_cast<double>(buf2.read_uint32(0, bigEndian));
1096
1097
      // Sanity check with an "unreasonably" large number
1098
601
      if (dataLength > 200 || dataLength < 8) {
1099
521
#ifndef SUPPRESS_WARNINGS
1100
521
        EXV_ERROR << "Xmp.video Nikon Tags, dataLength was found to be of inappropriate size."
1101
0
                  << " Entries considered invalid. Not Processed.\n";
1102
521
#endif
1103
521
        io_->seek(io_->tell() + dataLength - 8, BasicIo::beg);
1104
521
      } else
1105
80
        io_->readOrThrow(buf.data(), dataLength - 8);
1106
103k
    } else if (dataType == 8) {
1107
1.65k
      dataLength = buf.read_uint16(0, bigEndian) * 2;
1108
1.65k
      std::memset(buf.data(), 0x0, buf.size());
1109
1.65k
      io_->readOrThrow(buf.data(), 2);
1110
1.65k
      io_->readOrThrow(buf2.data(), 2);
1111
1.65k
      if (td)
1112
848
        xmpData_[_(td->label_)] = stringFormat("{}.{}", buf.read_uint16(0, bigEndian), buf2.read_uint16(0, bigEndian));
1113
1114
      // Sanity check with an "unreasonably" large number
1115
1.65k
      if (dataLength > 200 || dataLength < 4) {
1116
1.04k
#ifndef SUPPRESS_WARNINGS
1117
1.04k
        EXV_ERROR << "Xmp.video Nikon Tags, dataLength was found to be of inappropriate size."
1118
0
                  << " Entries considered invalid. Not Processed.\n";
1119
1.04k
#endif
1120
1.04k
        io_->seek(io_->tell() + dataLength - 4, BasicIo::beg);
1121
1.04k
      } else
1122
610
        io_->readOrThrow(buf.data(), dataLength - 4);
1123
1.65k
    }
1124
114k
  }
1125
1126
1.59k
  io_->seek(cur_pos + size, BasicIo::beg);
1127
1.59k
}  // QuickTimeVideo::NikonTagsDecoder
1128
1129
3.35k
void QuickTimeVideo::setMediaStream() {
1130
3.35k
  size_t current_position = io_->tell();
1131
3.35k
  DataBuf buf(4 + 1);
1132
1133
211k
  while (!io_->eof()) {
1134
211k
    io_->readOrThrow(buf.data(), 4);
1135
211k
    if (equalsQTimeTag(buf, "hdlr")) {
1136
3.33k
      io_->readOrThrow(buf.data(), 4);
1137
3.33k
      io_->readOrThrow(buf.data(), 4);
1138
3.33k
      io_->readOrThrow(buf.data(), 4);
1139
1140
3.33k
      if (equalsQTimeTag(buf, "vide"))
1141
76
        currentStream_ = Video;
1142
3.25k
      else if (equalsQTimeTag(buf, "soun"))
1143
1.18k
        currentStream_ = Audio;
1144
2.07k
      else if (equalsQTimeTag(buf, "hint"))
1145
113
        currentStream_ = Hint;
1146
1.95k
      else
1147
1.95k
        currentStream_ = GenMediaHeader;
1148
3.33k
      break;
1149
3.33k
    }
1150
211k
  }
1151
1152
3.35k
  io_->seek(current_position, BasicIo::beg);
1153
3.35k
}  // QuickTimeVideo::setMediaStream
1154
1155
2.11k
void QuickTimeVideo::timeToSampleDecoder() {
1156
2.11k
  DataBuf buf(4 + 1);
1157
2.11k
  io_->readOrThrow(buf.data(), 4);
1158
2.11k
  io_->readOrThrow(buf.data(), 4);
1159
2.11k
  uint64_t totalframes = 0;
1160
2.11k
  uint64_t timeOfFrames = 0;
1161
2.11k
  const uint32_t noOfEntries = buf.read_uint32(0, bigEndian);
1162
1163
4.64k
  for (uint32_t i = 0; i < noOfEntries; i++) {
1164
2.53k
    io_->readOrThrow(buf.data(), 4);
1165
2.53k
    const uint64_t temp = buf.read_uint32(0, bigEndian);
1166
2.53k
    totalframes = Safe::add(totalframes, temp);
1167
2.53k
    io_->readOrThrow(buf.data(), 4);
1168
2.53k
    timeOfFrames = Safe::add(timeOfFrames, temp * buf.read_uint32(0, bigEndian));
1169
2.53k
  }
1170
2.11k
  if (currentStream_ == Video) {
1171
300
    if (timeOfFrames == 0)
1172
105
      timeOfFrames = 1;
1173
300
    xmpData_["Xmp.video.FrameRate"] =
1174
300
        static_cast<double>(totalframes) * static_cast<double>(mdhdTimeScale_) / static_cast<double>(timeOfFrames);
1175
300
  }
1176
2.11k
}  // QuickTimeVideo::timeToSampleDecoder
1177
1178
2.23k
void QuickTimeVideo::sampleDesc(size_t size) {
1179
2.23k
  DataBuf buf(100);
1180
2.23k
  size_t cur_pos = io_->tell();
1181
2.23k
  io_->readOrThrow(buf.data(), 4);
1182
2.23k
  io_->readOrThrow(buf.data(), 4);
1183
2.23k
  const uint32_t noOfEntries = buf.read_uint32(0, bigEndian);
1184
1185
103k
  for (uint32_t i = 0; i < noOfEntries; i++) {
1186
102k
    if (currentStream_ == Video)
1187
59.6k
      imageDescDecoder();
1188
42.9k
    else if (currentStream_ == Audio)
1189
41.9k
      audioDescDecoder();
1190
954
    else
1191
954
      break;
1192
102k
  }
1193
2.23k
  io_->seek(Safe::add(cur_pos, size), BasicIo::beg);
1194
2.23k
}  // QuickTimeVideo::sampleDesc
1195
1196
41.9k
void QuickTimeVideo::audioDescDecoder() {
1197
41.9k
  DataBuf buf(40);
1198
41.9k
  std::memset(buf.data(), 0x0, buf.size());
1199
41.9k
  buf.data()[4] = '\0';
1200
41.9k
  io_->readOrThrow(buf.data(), 4);
1201
41.9k
  size_t size = 82;
1202
1203
41.9k
  const TagVocabulary* td;
1204
1205
880k
  for (int i = 0; size / 4 != 0; size -= 4, i++) {
1206
838k
    io_->readOrThrow(buf.data(), 4);
1207
838k
    switch (i) {
1208
41.9k
      case AudioFormat:
1209
41.9k
        td = Exiv2::find(qTimeFileType, Exiv2::toString(buf.data()));
1210
41.9k
        if (td)
1211
388
          xmpData_["Xmp.audio.Compressor"] = _(td->label_);
1212
41.5k
        else
1213
41.5k
          xmpData_["Xmp.audio.Compressor"] = buf.data();
1214
41.9k
        break;
1215
41.9k
      case AudioVendorID:
1216
41.9k
        td = Exiv2::find(vendorIDTags, Exiv2::toString(buf.data()));
1217
41.9k
        if (td)
1218
831
          xmpData_["Xmp.audio.VendorID"] = _(td->label_);
1219
41.9k
        break;
1220
41.9k
      case AudioChannels:
1221
41.9k
        xmpData_["Xmp.audio.ChannelType"] = buf.read_uint16(0, bigEndian);
1222
41.9k
        xmpData_["Xmp.audio.BitsPerSample"] = ((buf.data()[2] * 256) + buf.data()[3]);
1223
41.9k
        break;
1224
41.9k
      case AudioSampleRate:
1225
41.9k
        xmpData_["Xmp.audio.SampleRate"] =
1226
41.9k
            buf.read_uint16(0, bigEndian) + ((buf.data()[2] * 256 + buf.data()[3]) * 0.01);
1227
41.9k
        break;
1228
670k
      default:
1229
670k
        break;
1230
838k
    }
1231
838k
  }
1232
41.8k
  io_->readOrThrow(buf.data(), static_cast<long>(size % 4));  // cause size is so small, this cast should be right.
1233
41.8k
}  // QuickTimeVideo::audioDescDecoder
1234
1235
59.6k
void QuickTimeVideo::imageDescDecoder() {
1236
59.6k
  DataBuf buf(40);
1237
59.6k
  std::memset(buf.data(), 0x0, buf.size());
1238
59.6k
  buf.data()[4] = '\0';
1239
59.6k
  io_->readOrThrow(buf.data(), 4);
1240
59.6k
  size_t size = 82;
1241
1242
59.6k
  const TagVocabulary* td;
1243
1244
714k
  for (int i = 0; size / 4 != 0; size -= 4, i++) {
1245
655k
    io_->readOrThrow(buf.data(), 4);
1246
1247
655k
    switch (i) {
1248
59.5k
      case codec:
1249
59.5k
        td = Exiv2::find(qTimeFileType, Exiv2::toString(buf.data()));
1250
59.5k
        if (td)
1251
536
          xmpData_["Xmp.video.Codec"] = _(td->label_);
1252
59.0k
        else
1253
59.0k
          xmpData_["Xmp.video.Codec"] = buf.data();
1254
59.5k
        break;
1255
59.5k
      case VendorID:
1256
59.5k
        td = Exiv2::find(vendorIDTags, Exiv2::toString(buf.data()));
1257
59.5k
        if (td)
1258
1.35k
          xmpData_["Xmp.video.VendorID"] = _(td->label_);
1259
59.5k
        break;
1260
59.5k
      case SourceImageWidth_Height:
1261
59.5k
        xmpData_["Xmp.video.SourceImageWidth"] = buf.read_uint16(0, bigEndian);
1262
59.5k
        xmpData_["Xmp.video.SourceImageHeight"] = ((buf.data()[2] * 256) + buf.data()[3]);
1263
59.5k
        break;
1264
59.5k
      case XResolution:
1265
59.5k
        xmpData_["Xmp.video.XResolution"] =
1266
59.5k
            buf.read_uint16(0, bigEndian) + ((buf.data()[2] * 256 + buf.data()[3]) * 0.01);
1267
59.5k
        break;
1268
59.5k
      case YResolution:
1269
59.5k
        xmpData_["Xmp.video.YResolution"] =
1270
59.5k
            buf.read_uint16(0, bigEndian) + ((buf.data()[2] * 256 + buf.data()[3]) * 0.01);
1271
59.5k
        io_->readOrThrow(buf.data(), 3);
1272
59.5k
        size -= 3;
1273
59.5k
        break;
1274
59.5k
      case CompressorName:
1275
59.5k
        io_->readOrThrow(buf.data(), 32);
1276
59.5k
        size -= 32;
1277
59.5k
        xmpData_["Xmp.video.Compressor"] = buf.data();
1278
59.5k
        break;
1279
297k
      default:
1280
297k
        break;
1281
655k
    }
1282
655k
  }
1283
59.5k
  io_->readOrThrow(buf.data(), static_cast<long>(size % 4));
1284
59.5k
  xmpData_["Xmp.video.BitDepth"] = static_cast<int>(buf.read_uint8(0));
1285
59.5k
}  // QuickTimeVideo::imageDescDecoder
1286
1287
2.38k
void QuickTimeVideo::multipleEntriesDecoder(size_t recursion_depth) {
1288
2.38k
  enforce(recursion_depth < max_recursion_depth_, Exiv2::ErrorCode::kerCorruptedMetadata);
1289
2.38k
  DataBuf buf(4 + 1);
1290
2.38k
  io_->readOrThrow(buf.data(), 4);
1291
2.38k
  io_->readOrThrow(buf.data(), 4);
1292
2.38k
  uint32_t noOfEntries;
1293
1294
2.38k
  noOfEntries = buf.read_uint32(0, bigEndian);
1295
1296
16.5k
  for (uint32_t i = 0; i < noOfEntries && continueTraversing_; i++) {
1297
14.1k
    decodeBlock(recursion_depth + 1);
1298
14.1k
  }
1299
2.38k
}  // QuickTimeVideo::multipleEntriesDecoder
1300
1301
4.08k
void QuickTimeVideo::videoHeaderDecoder(size_t size) {
1302
4.08k
  DataBuf buf(3);
1303
4.08k
  std::memset(buf.data(), 0x0, buf.size());
1304
4.08k
  buf.data()[2] = '\0';
1305
4.08k
  currentStream_ = Video;
1306
1307
4.08k
  const TagDetails* td;
1308
1309
22.8k
  for (int i = 0; size / 2 != 0; size -= 2, i++) {
1310
18.8k
    io_->readOrThrow(buf.data(), 2);
1311
1312
18.8k
    switch (i) {
1313
3.49k
      case GraphicsMode:
1314
3.49k
        td = Exiv2::find(graphicsModetags, buf.read_uint16(0, bigEndian));
1315
3.49k
        if (td)
1316
2.91k
          xmpData_["Xmp.video.GraphicsMode"] = _(td->label_);
1317
3.49k
        break;
1318
3.49k
      case OpColor:
1319
3.49k
        xmpData_["Xmp.video.OpColor"] = buf.read_uint16(0, bigEndian);
1320
3.49k
        break;
1321
11.8k
      default:
1322
11.8k
        break;
1323
18.8k
    }
1324
18.8k
  }
1325
4.08k
  io_->readOrThrow(buf.data(), size % 2);
1326
4.08k
}  // QuickTimeVideo::videoHeaderDecoder
1327
1328
8.94k
void QuickTimeVideo::handlerDecoder(size_t size) {
1329
8.94k
  size_t cur_pos = io_->tell();
1330
8.94k
  DataBuf buf(100);
1331
8.94k
  std::memset(buf.data(), 0x0, buf.size());
1332
8.94k
  buf.data()[4] = '\0';
1333
1334
8.94k
  const TagVocabulary* tv;
1335
1336
53.6k
  for (int i = 0; i < 5; i++) {
1337
44.6k
    io_->readOrThrow(buf.data(), 4);
1338
1339
44.6k
    switch (i) {
1340
8.93k
      case HandlerClass:
1341
8.93k
        tv = Exiv2::find(handlerClassTags, Exiv2::toString(buf.data()));
1342
8.93k
        if (tv) {
1343
4.34k
          if (currentStream_ == Video)
1344
1.56k
            xmpData_["Xmp.video.HandlerClass"] = _(tv->label_);
1345
2.77k
          else if (currentStream_ == Audio)
1346
1.02k
            xmpData_["Xmp.audio.HandlerClass"] = _(tv->label_);
1347
4.34k
        }
1348
8.93k
        break;
1349
8.93k
      case HandlerType:
1350
8.93k
        tv = Exiv2::find(handlerTypeTags, Exiv2::toString(buf.data()));
1351
8.93k
        if (tv) {
1352
6.70k
          if (currentStream_ == Video)
1353
2.23k
            xmpData_["Xmp.video.HandlerType"] = _(tv->label_);
1354
4.47k
          else if (currentStream_ == Audio)
1355
1.84k
            xmpData_["Xmp.audio.HandlerType"] = _(tv->label_);
1356
6.70k
        }
1357
8.93k
        break;
1358
8.93k
      case HandlerVendorID:
1359
8.93k
        tv = Exiv2::find(vendorIDTags, Exiv2::toString(buf.data()));
1360
8.93k
        if (tv) {
1361
3.49k
          if (currentStream_ == Video)
1362
303
            xmpData_["Xmp.video.HandlerVendorID"] = _(tv->label_);
1363
3.18k
          else if (currentStream_ == Audio)
1364
1.00k
            xmpData_["Xmp.audio.HandlerVendorID"] = _(tv->label_);
1365
3.49k
        }
1366
8.93k
        break;
1367
44.6k
    }
1368
44.6k
  }
1369
8.93k
  io_->seek(cur_pos + size, BasicIo::beg);
1370
8.93k
}  // QuickTimeVideo::handlerDecoder
1371
1372
3.22k
void QuickTimeVideo::fileTypeDecoder(size_t size) {
1373
3.22k
  DataBuf buf(5);
1374
3.22k
  std::memset(buf.data(), 0x0, buf.size());
1375
3.22k
  buf.data()[4] = '\0';
1376
3.22k
  Exiv2::Value::UniquePtr v = Exiv2::Value::create(Exiv2::xmpSeq);
1377
3.22k
  const TagVocabulary* td;
1378
1379
387k
  for (int i = 0; size / 4 != 0; size -= 4, i++) {
1380
384k
    io_->readOrThrow(buf.data(), 4);
1381
384k
    td = Exiv2::find(qTimeFileType, Exiv2::toString(buf.data()));
1382
1383
384k
    switch (i) {
1384
3.20k
      case 0:
1385
3.20k
        if (td)
1386
1.85k
          xmpData_["Xmp.video.MajorBrand"] = _(td->label_);
1387
3.20k
        break;
1388
1.00k
      case 1:
1389
1.00k
        xmpData_["Xmp.video.MinorVersion"] = buf.read_uint32(0, bigEndian);
1390
1.00k
        break;
1391
380k
      default:
1392
380k
        if (td)
1393
979
          v->read(_(td->label_));
1394
379k
        else
1395
379k
          v->read(Exiv2::toString(buf.data()));
1396
380k
        break;
1397
384k
    }
1398
384k
  }
1399
3.22k
  xmpData_.add(Exiv2::XmpKey("Xmp.video.CompatibleBrands"), v.get());
1400
3.22k
  io_->readOrThrow(buf.data(), size % 4);
1401
3.22k
}  // QuickTimeVideo::fileTypeDecoder
1402
1403
3.34k
void QuickTimeVideo::mediaHeaderDecoder(size_t size) {
1404
3.34k
  DataBuf buf(5);
1405
3.34k
  std::memset(buf.data(), 0x0, buf.size());
1406
3.34k
  buf.data()[4] = '\0';
1407
3.34k
  int64_t time_scale = 1;
1408
1409
23.3k
  for (int i = 0; size / 4 != 0; size -= 4, i++) {
1410
19.9k
    io_->readOrThrow(buf.data(), 4);
1411
1412
19.9k
    switch (i) {
1413
3.29k
      case MediaHeaderVersion:
1414
3.29k
        if (currentStream_ == Video)
1415
1.42k
          xmpData_["Xmp.video.MediaHeaderVersion"] = static_cast<int>(buf.read_uint8(0));
1416
1.87k
        else if (currentStream_ == Audio)
1417
496
          xmpData_["Xmp.audio.MediaHeaderVersion"] = static_cast<int>(buf.read_uint8(0));
1418
3.29k
        break;
1419
3.29k
      case MediaCreateDate:
1420
        // A 32-bit integer that specifies (in seconds since midnight, January 1, 1904) when the movie atom was created.
1421
3.29k
        if (currentStream_ == Video)
1422
1.42k
          xmpData_["Xmp.video.MediaCreateDate"] = buf.read_uint32(0, bigEndian);
1423
1.86k
        else if (currentStream_ == Audio)
1424
496
          xmpData_["Xmp.audio.MediaCreateDate"] = buf.read_uint32(0, bigEndian);
1425
3.29k
        break;
1426
3.28k
      case MediaModifyDate:
1427
        // A 32-bit integer that specifies (in seconds since midnight, January 1, 1904) when the movie atom was created.
1428
3.28k
        if (currentStream_ == Video)
1429
1.41k
          xmpData_["Xmp.video.MediaModifyDate"] = buf.read_uint32(0, bigEndian);
1430
1.86k
        else if (currentStream_ == Audio)
1431
496
          xmpData_["Xmp.audio.MediaModifyDate"] = buf.read_uint32(0, bigEndian);
1432
3.28k
        break;
1433
3.28k
      case MediaTimeScale:
1434
3.28k
        if (currentStream_ == Video)
1435
1.41k
          xmpData_["Xmp.video.MediaTimeScale"] = buf.read_uint32(0, bigEndian);
1436
1.86k
        else if (currentStream_ == Audio)
1437
496
          xmpData_["Xmp.audio.MediaTimeScale"] = buf.read_uint32(0, bigEndian);
1438
3.28k
        time_scale = std::max(1U, buf.read_uint32(0, bigEndian));
1439
3.28k
        mdhdTimeScale_ = time_scale;
1440
3.28k
        break;
1441
3.28k
      case MediaDuration:
1442
3.28k
        if (currentStream_ == Video)
1443
1.41k
          xmpData_["Xmp.video.MediaDuration"] = time_scale ? buf.read_uint32(0, bigEndian) / time_scale : 0;
1444
1.86k
        else if (currentStream_ == Audio)
1445
496
          xmpData_["Xmp.audio.MediaDuration"] = time_scale ? buf.read_uint32(0, bigEndian) / time_scale : 0;
1446
3.28k
        break;
1447
3.28k
      case MediaLanguageCode:
1448
3.28k
        if (currentStream_ == Video)
1449
1.41k
          xmpData_["Xmp.video.MediaLangCode"] = buf.read_uint16(0, bigEndian);
1450
1.86k
        else if (currentStream_ == Audio)
1451
496
          xmpData_["Xmp.audio.MediaLangCode"] = buf.read_uint16(0, bigEndian);
1452
3.28k
        break;
1453
1454
240
      default:
1455
240
        break;
1456
19.9k
    }
1457
19.9k
  }
1458
3.34k
  io_->readOrThrow(buf.data(), size % 4);
1459
3.34k
}  // QuickTimeVideo::mediaHeaderDecoder
1460
1461
2.23k
void QuickTimeVideo::trackHeaderDecoder(size_t size) {
1462
2.23k
  DataBuf buf(5);
1463
2.23k
  std::memset(buf.data(), 0x0, buf.size());
1464
2.23k
  buf.data()[4] = '\0';
1465
2.23k
  int64_t temp = 0;
1466
1467
47.4k
  for (int i = 0; size / 4 != 0; size -= 4, i++) {
1468
45.2k
    io_->readOrThrow(buf.data(), 4);
1469
1470
45.2k
    switch (i) {
1471
2.17k
      case TrackHeaderVersion:
1472
2.17k
        if (currentStream_ == Video)
1473
112
          xmpData_["Xmp.video.TrackHeaderVersion"] = static_cast<int>(buf.read_uint8(0));
1474
2.05k
        else if (currentStream_ == Audio)
1475
858
          xmpData_["Xmp.audio.TrackHeaderVersion"] = static_cast<int>(buf.read_uint8(0));
1476
2.17k
        break;
1477
2.16k
      case TrackCreateDate:
1478
        // A 32-bit integer that specifies (in seconds since midnight, January 1, 1904) when the movie atom was created.
1479
2.16k
        if (currentStream_ == Video)
1480
112
          xmpData_["Xmp.video.TrackCreateDate"] = buf.read_uint32(0, bigEndian);
1481
2.05k
        else if (currentStream_ == Audio)
1482
858
          xmpData_["Xmp.audio.TrackCreateDate"] = buf.read_uint32(0, bigEndian);
1483
2.16k
        break;
1484
2.16k
      case TrackModifyDate:
1485
        // A 32-bit integer that specifies (in seconds since midnight, January 1, 1904) when the movie atom was created.
1486
2.16k
        if (currentStream_ == Video)
1487
112
          xmpData_["Xmp.video.TrackModifyDate"] = buf.read_uint32(0, bigEndian);
1488
2.05k
        else if (currentStream_ == Audio)
1489
858
          xmpData_["Xmp.audio.TrackModifyDate"] = buf.read_uint32(0, bigEndian);
1490
2.16k
        break;
1491
2.16k
      case TrackID:
1492
2.16k
        if (currentStream_ == Video)
1493
112
          xmpData_["Xmp.video.TrackID"] = buf.read_uint32(0, bigEndian);
1494
2.05k
        else if (currentStream_ == Audio)
1495
858
          xmpData_["Xmp.audio.TrackID"] = buf.read_uint32(0, bigEndian);
1496
2.16k
        break;
1497
2.16k
      case TrackDuration:
1498
2.16k
        if (currentStream_ == Video)
1499
111
          xmpData_["Xmp.video.TrackDuration"] = mvhdTimeScale_ ? buf.read_uint32(0, bigEndian) / mvhdTimeScale_ : 0;
1500
2.05k
        else if (currentStream_ == Audio)
1501
858
          xmpData_["Xmp.audio.TrackDuration"] = mvhdTimeScale_ ? buf.read_uint32(0, bigEndian) / mvhdTimeScale_ : 0;
1502
2.16k
        break;
1503
2.11k
      case TrackLayer:
1504
2.11k
        if (currentStream_ == Video)
1505
74
          xmpData_["Xmp.video.TrackLayer"] = buf.read_uint16(0, bigEndian);
1506
2.04k
        else if (currentStream_ == Audio)
1507
858
          xmpData_["Xmp.audio.TrackLayer"] = buf.read_uint16(0, bigEndian);
1508
2.11k
        break;
1509
2.11k
      case TrackVolume:
1510
2.11k
        if (currentStream_ == Video)
1511
73
          xmpData_["Xmp.video.TrackVolume"] = (static_cast<int>(buf.read_uint8(0)) + (buf.data()[2] * 0.1)) * 100;
1512
2.04k
        else if (currentStream_ == Audio)
1513
858
          xmpData_["Xmp.video.TrackVolume"] = (static_cast<int>(buf.read_uint8(0)) + (buf.data()[2] * 0.1)) * 100;
1514
2.11k
        break;
1515
2.10k
      case ImageWidth:
1516
2.10k
        if (currentStream_ == Video) {
1517
71
          temp = buf.read_uint16(0, bigEndian) + static_cast<int64_t>((buf.data()[2] * 256 + buf.data()[3]) * 0.01);
1518
71
          xmpData_["Xmp.video.Width"] = temp;
1519
71
          width_ = temp;
1520
71
        }
1521
2.10k
        break;
1522
2.10k
      case ImageHeight:
1523
2.10k
        if (currentStream_ == Video) {
1524
71
          temp = buf.read_uint16(0, bigEndian) + static_cast<int64_t>((buf.data()[2] * 256 + buf.data()[3]) * 0.01);
1525
71
          xmpData_["Xmp.video.Height"] = temp;
1526
71
          height_ = temp;
1527
71
        }
1528
2.10k
        break;
1529
25.9k
      default:
1530
25.9k
        break;
1531
45.2k
    }
1532
45.2k
  }
1533
2.23k
  io_->readOrThrow(buf.data(), size % 4);
1534
2.23k
}  // QuickTimeVideo::trackHeaderDecoder
1535
1536
196
void QuickTimeVideo::movieHeaderDecoder(size_t size) {
1537
196
  DataBuf buf(5);
1538
196
  std::memset(buf.data(), 0x0, buf.size());
1539
196
  buf.data()[4] = '\0';
1540
1541
2.64k
  for (int i = 0; size / 4 != 0; size -= 4, i++) {
1542
2.45k
    io_->readOrThrow(buf.data(), 4);
1543
1544
2.45k
    switch (i) {
1545
154
      case MovieHeaderVersion:
1546
154
        xmpData_["Xmp.video.MovieHeaderVersion"] = static_cast<int>(buf.read_uint8(0));
1547
154
        break;
1548
153
      case CreateDate:
1549
        // A 32-bit integer that specifies (in seconds since midnight, January 1, 1904) when the movie atom was created.
1550
153
        xmpData_["Xmp.video.DateUTC"] = buf.read_uint32(0, bigEndian);
1551
153
        break;
1552
125
      case ModifyDate:
1553
        // A 32-bit integer that specifies (in seconds since midnight, January 1, 1904) when the movie atom was created.
1554
125
        xmpData_["Xmp.video.ModificationDate"] = buf.read_uint32(0, bigEndian);
1555
125
        break;
1556
124
      case TimeScale:
1557
124
        xmpData_["Xmp.video.TimeScale"] = buf.read_uint32(0, bigEndian);
1558
124
        mvhdTimeScale_ = std::max(1U, buf.read_uint32(0, bigEndian));
1559
124
        break;
1560
117
      case Duration:
1561
117
        if (mvhdTimeScale_ != 0) {  // To prevent division by zero
1562
117
          xmpData_["Xmp.video.Duration"] = buf.read_uint32(0, bigEndian) * 1000 / mvhdTimeScale_;
1563
117
        }
1564
117
        break;
1565
117
      case PreferredRate:
1566
117
        xmpData_["Xmp.video.PreferredRate"] =
1567
117
            buf.read_uint16(0, bigEndian) + ((buf.data()[2] * 256 + buf.data()[3]) * 0.01);
1568
117
        break;
1569
117
      case PreferredVolume:
1570
117
        xmpData_["Xmp.video.PreferredVolume"] = (static_cast<int>(buf.read_uint8(0)) + (buf.data()[2] * 0.1)) * 100;
1571
117
        break;
1572
81
      case PreviewTime:
1573
81
        xmpData_["Xmp.video.PreviewTime"] = buf.read_uint32(0, bigEndian);
1574
81
        break;
1575
81
      case PreviewDuration:
1576
81
        xmpData_["Xmp.video.PreviewDuration"] = buf.read_uint32(0, bigEndian);
1577
81
        break;
1578
81
      case PosterTime:
1579
81
        xmpData_["Xmp.video.PosterTime"] = buf.read_uint32(0, bigEndian);
1580
81
        break;
1581
81
      case SelectionTime:
1582
81
        xmpData_["Xmp.video.SelectionTime"] = buf.read_uint32(0, bigEndian);
1583
81
        break;
1584
81
      case SelectionDuration:
1585
81
        xmpData_["Xmp.video.SelectionDuration"] = buf.read_uint32(0, bigEndian);
1586
81
        break;
1587
81
      case CurrentTime:
1588
81
        xmpData_["Xmp.video.CurrentTime"] = buf.read_uint32(0, bigEndian);
1589
81
        break;
1590
81
      case NextTrackID:
1591
81
        xmpData_["Xmp.video.NextTrackID"] = buf.read_uint32(0, bigEndian);
1592
81
        break;
1593
979
      default:
1594
979
        break;
1595
2.45k
    }
1596
2.45k
  }
1597
196
  io_->readOrThrow(buf.data(), size % 4);
1598
196
}  // QuickTimeVideo::movieHeaderDecoder
1599
1600
3.06k
Image::UniquePtr newQTimeInstance(BasicIo::UniquePtr io, bool /*create*/) {
1601
3.06k
  auto image = std::make_unique<QuickTimeVideo>(std::move(io));
1602
3.06k
  if (!image->good()) {
1603
8
    return nullptr;
1604
8
  }
1605
3.05k
  return image;
1606
3.06k
}
1607
1608
16.6k
bool isQTimeType(BasicIo& iIo, bool advance) {
1609
16.6k
  auto buf = DataBuf(12);
1610
16.6k
  iIo.read(buf.data(), 12);
1611
1612
16.6k
  if (iIo.error() || iIo.eof()) {
1613
342
    return false;
1614
342
  }
1615
16.3k
  auto qTimeTags = std::array{"PICT", "free", "ftyp", "junk", "mdat", "moov", "pict", "pnot", "skip", "uuid", "wide"};
1616
1617
16.3k
  bool matched = false;
1618
1619
114k
  for (auto const& tag : qTimeTags) {
1620
114k
    auto tmp = buf.cmpBytes(4, tag, 4);
1621
114k
    if (tmp == 0) {
1622
      // we only match if we actually know the video type. This is done
1623
      // to avoid matching just on ftyp because bmffimage also has that
1624
      // header.
1625
11.4k
      if (Exiv2::find(qTimeFileType, std::string{buf.c_str(8), 4})) {
1626
9.18k
        matched = true;
1627
9.18k
      }
1628
11.4k
      break;
1629
11.4k
    }
1630
114k
  }
1631
1632
16.3k
  if (!advance || !matched) {
1633
16.3k
    iIo.seek(0L, BasicIo::beg);
1634
16.3k
  }
1635
1636
16.3k
  return matched;
1637
16.6k
}
1638
1639
}  // namespace Exiv2