| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2022 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
#include "dng_assertions.h" |
| 10 |
#include "dng_big_table.h" |
| 11 |
#include "dng_bmff.h" |
| 12 |
#include "dng_host.h" |
| 13 |
#include "dng_memory_stream.h" |
| 14 |
#include "dng_stream.h" |
| 15 |
|
| 16 |
#include <unordered_map> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
static dng_string ReadName (dng_stream &stream) |
| 22 |
{ |
| 23 |
|
| 24 |
char name [5]; |
| 25 |
|
| 26 |
stream.Get (name, 4); |
| 27 |
|
| 28 |
name [4] = 0; |
| 29 |
|
| 30 |
return dng_string (name); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
/*****************************************************************************/ |
| 35 |
|
| 36 |
dng_bmff_io::~dng_bmff_io () |
| 37 |
{ |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/*****************************************************************************/ |
| 42 |
|
| 43 |
void dng_bmff_io::Read (dng_host &host, |
| 44 |
dng_stream &stream) |
| 45 |
{ |
| 46 |
|
| 47 |
stream.SetBigEndian (); |
| 48 |
|
| 49 |
stream.SetReadPosition (0); |
| 50 |
|
| 51 |
// Skip first 4 bytes (length field of first box). |
| 52 |
|
| 53 |
stream.Skip (4); |
| 54 |
|
| 55 |
// Quick check to see if this looks like a valid ISO BMFF or JXL file. |
| 56 |
|
| 57 |
dng_string firstName (ReadName (stream)); |
| 58 |
|
| 59 |
if (!(firstName.Matches ("ftyp", true) || |
| 60 |
firstName.Matches ("JXL ", true))) // TODO(erichan): add compile flag |
| 61 |
{ |
| 62 |
ThrowBadFormat (); |
| 63 |
} |
| 64 |
|
| 65 |
// Reset to reading. |
| 66 |
|
| 67 |
stream.SetReadPosition (0); |
| 68 |
|
| 69 |
uint64 totalOffset = 0; |
| 70 |
|
| 71 |
while (stream.Position () < stream.Length ()) |
| 72 |
{ |
| 73 |
|
| 74 |
const uint32 storedLength = stream.Get_uint32 (); |
| 75 |
|
| 76 |
uint64 length = uint64 (storedLength); |
| 77 |
|
| 78 |
dng_string name = ReadName (stream); |
| 79 |
|
| 80 |
uint64 headerOffset = 0; |
| 81 |
|
| 82 |
if (storedLength == 1) |
| 83 |
{ |
| 84 |
|
| 85 |
// Large box. |
| 86 |
|
| 87 |
length = stream.Get_uint64 (); |
| 88 |
|
| 89 |
DNG_REQUIRE (length >= 16, |
| 90 |
"Box 64-bit length too small"); |
| 91 |
|
| 92 |
headerOffset = 16; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
else if (storedLength == 0) |
| 97 |
{ |
| 98 |
|
| 99 |
// Last box: extends to EOF. |
| 100 |
|
| 101 |
headerOffset = 8; |
| 102 |
|
| 103 |
length = headerOffset + (stream.Length () - stream.Position ()); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
else |
| 108 |
{ |
| 109 |
|
| 110 |
// Compact box. |
| 111 |
|
| 112 |
DNG_REQUIRE (length >= 8, |
| 113 |
"Box 32-bit length too small"); |
| 114 |
|
| 115 |
headerOffset = 8; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
DNG_REQUIRE (length >= headerOffset, |
| 120 |
"logic error in computing contentLength"); |
| 121 |
|
| 122 |
const uint64 contentLength = length - headerOffset; |
| 123 |
|
| 124 |
// Store the box data if desired. |
| 125 |
|
| 126 |
if (ShouldReadBox (name, length)) |
| 127 |
{ |
| 128 |
|
| 129 |
auto box = std::make_shared<dng_bmff_box> (); |
| 130 |
|
| 131 |
box->fStoredLength = storedLength; |
| 132 |
|
| 133 |
box->fRealLength = length; |
| 134 |
|
| 135 |
box->fOffset = totalOffset; |
| 136 |
|
| 137 |
box->fName = name; |
| 138 |
|
| 139 |
// Currently do not support contents above 4 GB. |
| 140 |
|
| 141 |
DNG_REQUIRE (contentLength <= uint64 (0xFFFFFFFF), |
| 142 |
"Unsupported contentLength too larget"); |
| 143 |
|
| 144 |
const uint32 contentLength32 = uint32 (contentLength); |
| 145 |
|
| 146 |
box->fContent.reset (host.Allocate (contentLength32)); |
| 147 |
|
| 148 |
fBoxes.push_back (box); |
| 149 |
|
| 150 |
stream.Get (box->fContent->Buffer (), |
| 151 |
contentLength32); |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
// Skip the box. |
| 156 |
|
| 157 |
else |
| 158 |
{ |
| 159 |
|
| 160 |
stream.Skip (contentLength); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
// Update total offset. |
| 165 |
|
| 166 |
totalOffset += length; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
/*****************************************************************************/ |
| 173 |
|
| 174 |
void dng_bmff_io::Write (dng_host & /* host */, |
| 175 |
dng_stream &stream) const |
| 176 |
{ |
| 177 |
|
| 178 |
stream.SetBigEndian (); |
| 179 |
|
| 180 |
for (const auto &box : fBoxes) |
| 181 |
{ |
| 182 |
|
| 183 |
// Skip missing/invalid boxes. |
| 184 |
|
| 185 |
if (!box) |
| 186 |
continue; |
| 187 |
|
| 188 |
DNG_REQUIRE (box->fName.Length () == 4, |
| 189 |
"name length wrong size"); |
| 190 |
|
| 191 |
const uint32 dataLen = box->fContent ? box->fContent->LogicalSize () : 0; |
| 192 |
|
| 193 |
bool useLargeSize = ((box->fStoredLength == 1) || |
| 194 |
uint64 (dataLen + 8) > uint64 (0xFFFFFFFF)); |
| 195 |
|
| 196 |
if (useLargeSize) |
| 197 |
{ |
| 198 |
|
| 199 |
stream.Put_uint32 (1); // use large size |
| 200 |
|
| 201 |
stream.Put (box->fName.Get (), 4); |
| 202 |
|
| 203 |
stream.Put_uint64 (uint64 (dataLen) + 16); |
| 204 |
|
| 205 |
if (box->fContent && (dataLen > 0)) |
| 206 |
stream.Put (box->fContent->Buffer (), |
| 207 |
dataLen); |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
else |
| 212 |
{ |
| 213 |
|
| 214 |
// Compact or last box. |
| 215 |
|
| 216 |
if (box->fStoredLength == 0) // last box |
| 217 |
stream.Put_uint32 (0); |
| 218 |
|
| 219 |
else // compact box |
| 220 |
stream.Put_uint32 (dataLen + 8); |
| 221 |
|
| 222 |
stream.Put (box->fName.Get (), 4); |
| 223 |
|
| 224 |
if (box->fContent && (dataLen > 0)) |
| 225 |
stream.Put (box->fContent->Buffer (), |
| 226 |
dataLen); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
} // all boxes |
| 231 |
|
| 232 |
stream.Flush (); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
/*****************************************************************************/ |
| 237 |
|
| 238 |
void dng_bmff_io::UpdateBigTables (dng_host &host, |
| 239 |
const dng_big_table_dictionary &newTables, |
| 240 |
const bool deleteUnused) |
| 241 |
{ |
| 242 |
|
| 243 |
// 1 box per table. |
| 244 |
// |
| 245 |
// Box tag: btbl (big table), registered here: https://mp4ra.org/#/request |
| 246 |
// |
| 247 |
// Box layout is big-endian. |
| 248 |
// |
| 249 |
// length: overall length of box 4 bytes |
| 250 |
// tag code: btbl 4 bytes |
| 251 |
// version: version identifier 4 bytes |
| 252 |
// data variable |
| 253 |
// |
| 254 |
// version 1 data: |
| 255 |
// md5 fingerprint 16 bytes |
| 256 |
// big table data variable |
| 257 |
|
| 258 |
// LogPrint ("Writing Big Tables to JXL boxes\n"); |
| 259 |
|
| 260 |
const dng_string kBigTableTagName ("btbl"); |
| 261 |
|
| 262 |
// Step 1: Find all existing tables in the file. Along the way, we clean |
| 263 |
// up duplicate tables and no-longer-used tables (tables that aren't in |
| 264 |
// the given 'metadata' object). |
| 265 |
|
| 266 |
const auto &dictMap = newTables.Map (); |
| 267 |
|
| 268 |
std::unordered_map<dng_fingerprint, |
| 269 |
dng_bmff_box_sptr, |
| 270 |
dng_fingerprint_hash> digests; |
| 271 |
|
| 272 |
for (auto &box : fBoxes) |
| 273 |
{ |
| 274 |
|
| 275 |
if (box && |
| 276 |
(box->fName == kBigTableTagName) && |
| 277 |
box->fContent && |
| 278 |
(box->fContent->LogicalSize () > 20)) |
| 279 |
{ |
| 280 |
|
| 281 |
dng_stream tableStream (box->fContent->Buffer (), |
| 282 |
box->fContent->LogicalSize ()); |
| 283 |
|
| 284 |
tableStream.SetBigEndian (); |
| 285 |
|
| 286 |
// Get version (4 bytes). |
| 287 |
|
| 288 |
uint32 version = tableStream.Get_uint32 (); |
| 289 |
|
| 290 |
if (version == 1) |
| 291 |
{ |
| 292 |
|
| 293 |
// Get fingerprint (16 bytes). |
| 294 |
|
| 295 |
dng_fingerprint digest; |
| 296 |
|
| 297 |
tableStream.Get (digest.data, |
| 298 |
uint32 (sizeof (digest.data))); |
| 299 |
|
| 300 |
if (digest.IsValid () && |
| 301 |
(digests.find (digest) == digests.end ())) |
| 302 |
{ |
| 303 |
|
| 304 |
// Not seen yet. |
| 305 |
|
| 306 |
if (dictMap.find (digest) == dictMap.end ()) |
| 307 |
{ |
| 308 |
|
| 309 |
// Not found in newTables, so we don't need this |
| 310 |
// table. If requested to delete unused tables, then |
| 311 |
// mark for delete. |
| 312 |
|
| 313 |
if (deleteUnused) |
| 314 |
box.reset (); |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
else |
| 319 |
{ |
| 320 |
|
| 321 |
// Found in newTables, so we do need this table. |
| 322 |
// Remember that we saw it. |
| 323 |
|
| 324 |
digests.insert (std::make_pair (digest, box)); |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
else |
| 331 |
{ |
| 332 |
|
| 333 |
// Already seen this table earlier in this loop, so |
| 334 |
// this is a duplicate. |
| 335 |
|
| 336 |
box.reset (); |
| 337 |
|
| 338 |
} |
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
// Step 2: Write new tables. |
| 347 |
|
| 348 |
for (auto it = dictMap.cbegin (); it != dictMap.cend (); ++it) |
| 349 |
{ |
| 350 |
|
| 351 |
const dng_fingerprint &fingerprint = it->first; |
| 352 |
|
| 353 |
// LogPrint ("big table: %s\n", |
| 354 |
// fingerprint.ToUtf8HexString ().Get ()); |
| 355 |
|
| 356 |
// Skip tables that we already have. |
| 357 |
|
| 358 |
if (digests.find (fingerprint) != digests.end ()) |
| 359 |
continue; |
| 360 |
|
| 361 |
// Add this table. |
| 362 |
|
| 363 |
auto temp = std::make_shared<dng_bmff_box> (); |
| 364 |
|
| 365 |
temp->fName = kBigTableTagName; |
| 366 |
|
| 367 |
const dng_ref_counted_block &block = it->second; |
| 368 |
|
| 369 |
const uint32 blockSize = block.LogicalSize (); |
| 370 |
|
| 371 |
dng_memory_stream memStream (host.Allocator ()); |
| 372 |
|
| 373 |
memStream.SetBigEndian (); |
| 374 |
|
| 375 |
// Write version. |
| 376 |
|
| 377 |
memStream.Put_uint32 (1); |
| 378 |
|
| 379 |
// Write fingerprint. |
| 380 |
|
| 381 |
memStream.Put (fingerprint.data, |
| 382 |
uint32 (sizeof (fingerprint.data))); |
| 383 |
|
| 384 |
// Write table data. |
| 385 |
|
| 386 |
memStream.Put (block.Buffer (), |
| 387 |
blockSize); |
| 388 |
|
| 389 |
temp->fContent.reset (memStream.AsMemoryBlock (host.Allocator ())); |
| 390 |
|
| 391 |
fBoxes.push_back (temp); |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
/*****************************************************************************/ |