/
ArwDecoder.cpp
472 lines (372 loc) · 15.8 KB
/
ArwDecoder.cpp
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
/*
RawSpeed - RAW file decoder.
Copyright (C) 2009-2014 Klaus Post
Copyright (C) 2014 Pedro Côrte-Real
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "decoders/ArwDecoder.h"
#include "common/Common.h" // for roundDown
#include "common/Point.h" // for iPoint2D
#include "common/RawspeedException.h" // for RawspeedException
#include "decoders/RawDecoderException.h" // for ThrowRDE
#include "decompressors/SonyArw1Decompressor.h" // for SonyArw1Decompre...
#include "decompressors/SonyArw2Decompressor.h" // for SonyArw2Decompre...
#include "decompressors/UncompressedDecompressor.h" // for UncompressedDeco...
#include "io/Buffer.h" // for Buffer, DataBuffer
#include "io/ByteStream.h" // for ByteStream
#include "io/Endianness.h" // for Endianness, Endi...
#include "metadata/Camera.h" // for Hints
#include "metadata/ColorFilterArray.h" // for CFAColor::GREEN, CFAColor::BLUE
#include "tiff/TiffEntry.h" // for TiffEntry
#include "tiff/TiffIFD.h" // for TiffRootIFD, Tif...
#include "tiff/TiffTag.h" // for DNGPRIVATEDATA
#include <array> // for array
#include <cassert> // for assert
#include <cstring> // for memcpy, size_t
#include <memory> // for unique_ptr
#include <set> // for set
#include <string> // for operator==, string
#include <vector> // for vector
using std::vector;
namespace rawspeed {
bool ArwDecoder::isAppropriateDecoder(const TiffRootIFD* rootIFD,
[[maybe_unused]] const Buffer& file) {
const auto id = rootIFD->getID();
const std::string& make = id.make;
// FIXME: magic
return make == "SONY";
}
RawImage ArwDecoder::decodeSRF(const TiffIFD* raw) {
raw = mRootIFD->getIFDWithTag(TiffTag::IMAGEWIDTH);
uint32_t width = raw->getEntry(TiffTag::IMAGEWIDTH)->getU32();
uint32_t height = raw->getEntry(TiffTag::IMAGELENGTH)->getU32();
if (width == 0 || height == 0 || width > 3360 || height > 2460)
ThrowRDE("Unexpected image dimensions found: (%u; %u)", width, height);
uint32_t len = width * height * 2;
// Constants taken from dcraw
uint32_t off = 862144;
uint32_t key_off = 200896;
uint32_t head_off = 164600;
// Replicate the dcraw contortions to get the "decryption" key
const uint8_t* keyData = mFile.getData(key_off, 1);
uint32_t offset = (*keyData) * 4;
keyData = mFile.getData(key_off + offset, 4);
uint32_t key = getU32BE(keyData);
static const size_t head_size = 40;
const uint8_t* head_orig = mFile.getData(head_off, head_size);
vector<uint8_t> head(head_size);
SonyDecrypt(reinterpret_cast<const uint32_t*>(head_orig),
reinterpret_cast<uint32_t*>(&head[0]), 10, key);
for (int i = 26; i > 22; i--)
key = key << 8 | head[i - 1];
// "Decrypt" the whole image buffer
const auto* image_data = mFile.getData(off, len);
auto image_decoded = Buffer::Create(len);
SonyDecrypt(reinterpret_cast<const uint32_t*>(image_data),
reinterpret_cast<uint32_t*>(image_decoded.get()), len / 4, key);
Buffer di(std::move(image_decoded), len);
// And now decode as a normal 16bit raw
mRaw->dim = iPoint2D(width, height);
mRaw->createData();
UncompressedDecompressor u(
ByteStream(DataBuffer(di.getSubView(0, len), Endianness::little)), mRaw);
u.decodeRawUnpacked<16, Endianness::big>(width, height);
return mRaw;
}
RawImage ArwDecoder::decodeRawInternal() {
const TiffIFD* raw = nullptr;
vector<const TiffIFD*> data = mRootIFD->getIFDsWithTag(TiffTag::STRIPOFFSETS);
if (data.empty()) {
if (const TiffEntry* model = mRootIFD->getEntryRecursive(TiffTag::MODEL);
model && model->getString() == "DSLR-A100") {
// We've caught the elusive A100 in the wild, a transitional format
// between the simple sanity of the MRW custom format and the wordly
// wonderfullness of the Tiff-based ARW format, let's shoot from the hip
raw = mRootIFD->getIFDWithTag(TiffTag::SUBIFDS);
uint32_t off = raw->getEntry(TiffTag::SUBIFDS)->getU32();
uint32_t width = 3881;
uint32_t height = 2608;
mRaw->dim = iPoint2D(width, height);
ByteStream input(DataBuffer(mFile.getSubView(off), Endianness::little));
SonyArw1Decompressor a(mRaw);
mRaw->createData();
a.decompress(input);
return mRaw;
}
if (hints.has("srf_format"))
return decodeSRF(raw);
ThrowRDE("No image data found");
}
raw = data[0];
int compression = raw->getEntry(TiffTag::COMPRESSION)->getU32();
if (1 == compression) {
DecodeUncompressed(raw);
return mRaw;
}
if (32767 != compression)
ThrowRDE("Unsupported compression %i", compression);
const TiffEntry* offsets = raw->getEntry(TiffTag::STRIPOFFSETS);
const TiffEntry* counts = raw->getEntry(TiffTag::STRIPBYTECOUNTS);
if (offsets->count != 1) {
ThrowRDE("Multiple Strips found: %u", offsets->count);
}
if (counts->count != offsets->count) {
ThrowRDE(
"Byte count number does not match strip size: count:%u, strips:%u ",
counts->count, offsets->count);
}
uint32_t width = raw->getEntry(TiffTag::IMAGEWIDTH)->getU32();
uint32_t height = raw->getEntry(TiffTag::IMAGELENGTH)->getU32();
uint32_t bitPerPixel = raw->getEntry(TiffTag::BITSPERSAMPLE)->getU32();
switch (bitPerPixel) {
case 8:
case 12:
case 14:
break;
default:
ThrowRDE("Unexpected bits per pixel: %u", bitPerPixel);
}
// Sony E-550 marks compressed 8bpp ARW with 12 bit per pixel
// this makes the compression detect it as a ARW v1.
// This camera has however another MAKER entry, so we MAY be able
// to detect it this way in the future.
data = mRootIFD->getIFDsWithTag(TiffTag::MAKE);
if (data.size() > 1) {
for (auto &i : data) {
std::string make = i->getEntry(TiffTag::MAKE)->getString();
/* Check for maker "SONY" without spaces */
if (make == "SONY")
bitPerPixel = 8;
}
}
if (width == 0 || height == 0 || height % 2 != 0 || width > 9600 ||
height > 6376)
ThrowRDE("Unexpected image dimensions found: (%u; %u)", width, height);
bool arw1 = uint64_t(counts->getU32()) * 8 != width * height * bitPerPixel;
if (arw1)
height += 8;
mRaw->dim = iPoint2D(width, height);
std::vector<uint16_t> curve(0x4001);
const TiffEntry* c = raw->getEntry(TiffTag::SONY_CURVE);
std::array<uint32_t, 6> sony_curve = {{0, 0, 0, 0, 0, 4095}};
for (uint32_t i = 0; i < 4; i++)
sony_curve[i+1] = (c->getU16(i) >> 2) & 0xfff;
for (uint32_t i = 0; i < 0x4001; i++)
curve[i] = i;
for (uint32_t i = 0; i < 5; i++)
for (uint32_t j = sony_curve[i] + 1; j <= sony_curve[i + 1]; j++)
curve[j] = curve[j-1] + (1 << i);
RawImageCurveGuard curveHandler(&mRaw, curve, uncorrectedRawValues);
uint32_t c2 = counts->getU32();
uint32_t off = offsets->getU32();
if (!mFile.isValid(off))
ThrowRDE("Data offset after EOF, file probably truncated");
if (!mFile.isValid(off, c2))
c2 = mFile.getSize() - off;
ByteStream input(DataBuffer(mFile.getSubView(off, c2), Endianness::little));
if (arw1) {
SonyArw1Decompressor a(mRaw);
mRaw->createData();
a.decompress(input);
} else
DecodeARW2(input, width, height, bitPerPixel);
return mRaw;
}
void ArwDecoder::DecodeUncompressed(const TiffIFD* raw) const {
uint32_t width = raw->getEntry(TiffTag::IMAGEWIDTH)->getU32();
uint32_t height = raw->getEntry(TiffTag::IMAGELENGTH)->getU32();
uint32_t off = raw->getEntry(TiffTag::STRIPOFFSETS)->getU32();
uint32_t c2 = raw->getEntry(TiffTag::STRIPBYTECOUNTS)->getU32();
mRaw->dim = iPoint2D(width, height);
if (width == 0 || height == 0 || width > 9600 || height > 6376)
ThrowRDE("Unexpected image dimensions found: (%u; %u)", width, height);
if (c2 == 0)
ThrowRDE("Strip is empty, nothing to decode!");
const Buffer buf(mFile.getSubView(off, c2));
mRaw->createData();
UncompressedDecompressor u(ByteStream(DataBuffer(buf, Endianness::little)),
mRaw);
if (hints.has("sr2_format"))
u.decodeRawUnpacked<14, Endianness::big>(width, height);
else
u.decodeRawUnpacked<16, Endianness::little>(width, height);
}
void ArwDecoder::DecodeARW2(const ByteStream& input, uint32_t w, uint32_t h,
uint32_t bpp) {
if (bpp == 8) {
SonyArw2Decompressor a2(mRaw, input);
mRaw->createData();
a2.decompress();
return;
} // End bpp = 8
if (bpp == 12) {
mRaw->createData();
UncompressedDecompressor u(
ByteStream(DataBuffer(input, Endianness::little)), mRaw);
u.decode12BitRaw<Endianness::little>(w, h);
// Shift scales, since black and white are the same as compressed precision
mShiftDownScale = 2;
return;
}
ThrowRDE("Unsupported bit depth");
}
void ArwDecoder::ParseA100WB() const {
if (!mRootIFD->hasEntryRecursive(TiffTag::DNGPRIVATEDATA))
return;
// only contains the offset, not the length!
const TiffEntry* priv = mRootIFD->getEntryRecursive(TiffTag::DNGPRIVATEDATA);
ByteStream bs = priv->getData();
bs.setByteOrder(Endianness::little);
const uint32_t off = bs.getU32();
bs = ByteStream(DataBuffer(mFile.getSubView(off), Endianness::little));
// MRW style, see MrwDecoder
bs.setByteOrder(Endianness::big);
uint32_t tag = bs.getU32();
if (0x4D5249 != tag) // MRI
ThrowRDE("Can not parse DNGPRIVATEDATA, invalid tag (0x%x).", tag);
bs.setByteOrder(Endianness::little);
uint32_t len = bs.getU32();
bs = bs.getSubStream(bs.getPosition(), len);
while (bs.getRemainSize() > 0) {
bs.setByteOrder(Endianness::big);
tag = bs.getU32();
bs.setByteOrder(Endianness::little);
len = bs.getU32();
(void)bs.check(len);
if (!len)
ThrowRDE("Found entry of zero length, corrupt.");
if (0x574247 != tag) { // WBG
// not the tag we are interested in, skip
bs.skipBytes(len);
continue;
}
bs.skipBytes(4);
bs.setByteOrder(Endianness::little);
std::array<uint16_t, 4> tmp;
for (auto& coeff : tmp)
coeff = bs.getU16();
mRaw->metadata.wbCoeffs[0] = static_cast<float>(tmp[0]);
mRaw->metadata.wbCoeffs[1] = static_cast<float>(tmp[1]);
mRaw->metadata.wbCoeffs[2] = static_cast<float>(tmp[3]);
// only need this one block, no need to process any further
break;
}
}
void ArwDecoder::decodeMetaDataInternal(const CameraMetaData* meta) {
//Default
int iso = 0;
mRaw->cfa.setCFA(iPoint2D(2, 2), CFAColor::RED, CFAColor::GREEN,
CFAColor::GREEN, CFAColor::BLUE);
if (mRootIFD->hasEntryRecursive(TiffTag::ISOSPEEDRATINGS))
iso = mRootIFD->getEntryRecursive(TiffTag::ISOSPEEDRATINGS)->getU32();
auto id = mRootIFD->getID();
setMetaData(meta, id, "", iso);
mRaw->whitePoint >>= mShiftDownScale;
mRaw->blackLevel >>= mShiftDownScale;
// Set the whitebalance
try {
if (id.model == "DSLR-A100") { // Handle the MRW style WB of the A100
ParseA100WB();
} else { // Everything else but the A100
GetWB();
}
} catch (const RawspeedException& e) {
mRaw->setError(e.what());
// We caught an exception reading WB, just ignore it
}
}
void ArwDecoder::SonyDecrypt(const uint32_t* ibuf, uint32_t* obuf, uint32_t len,
uint32_t key) {
if (0 == len)
return;
std::array<uint32_t, 128> pad;
// Initialize the decryption pad from the key
for (int p=0; p < 4; p++)
pad[p] = key = uint32_t(key * 48828125UL + 1UL);
pad[3] = pad[3] << 1 | (pad[0]^pad[2]) >> 31;
for (int p=4; p < 127; p++)
pad[p] = (pad[p-4]^pad[p-2]) << 1 | (pad[p-3]^pad[p-1]) >> 31;
for (int p=0; p < 127; p++)
pad[p] = getU32BE(&pad[p]);
int p = 127;
// Decrypt the buffer in place using the pad
for (; len > 0; len--) {
pad[p & 127] = pad[(p+1) & 127] ^ pad[(p+1+64) & 127];
uint32_t pv;
memcpy(&pv, &(pad[p & 127]), sizeof(uint32_t));
uint32_t bv;
memcpy(&bv, ibuf, sizeof(uint32_t));
bv ^= pv;
memcpy(obuf, &bv, sizeof(uint32_t));
ibuf++;
obuf++;
p++;
}
}
void ArwDecoder::GetWB() const {
// Set the whitebalance for all the modern ARW formats (everything after A100)
if (mRootIFD->hasEntryRecursive(TiffTag::DNGPRIVATEDATA)) {
NORangesSet<Buffer> ifds_undecoded;
const TiffEntry* priv =
mRootIFD->getEntryRecursive(TiffTag::DNGPRIVATEDATA);
TiffRootIFD makerNoteIFD(nullptr, &ifds_undecoded, priv->getRootIfdData(),
priv->getU32());
const TiffEntry* sony_offset =
makerNoteIFD.getEntryRecursive(TiffTag::SONY_OFFSET);
const TiffEntry* sony_length =
makerNoteIFD.getEntryRecursive(TiffTag::SONY_LENGTH);
const TiffEntry* sony_key =
makerNoteIFD.getEntryRecursive(TiffTag::SONY_KEY);
if(!sony_offset || !sony_length || !sony_key || sony_key->count != 4)
ThrowRDE("couldn't find the correct metadata for WB decoding");
assert(sony_offset != nullptr);
uint32_t off = sony_offset->getU32();
assert(sony_length != nullptr);
// The Decryption is done in blocks of 4 bytes.
uint32_t len = roundDown(sony_length->getU32(), 4);
assert(sony_key != nullptr);
uint32_t key = getU32LE(sony_key->getData().getData(4));
// "Decrypt" IFD
const auto& ifd_crypt = priv->getRootIfdData();
const auto EncryptedBuffer = ifd_crypt.getSubView(off, len);
// We do have to prepend 'off' padding, because TIFF uses absolute offsets.
const auto DecryptedBufferSize = off + EncryptedBuffer.getSize();
auto DecryptedBuffer = Buffer::Create(DecryptedBufferSize);
SonyDecrypt(reinterpret_cast<const uint32_t*>(EncryptedBuffer.begin()),
reinterpret_cast<uint32_t*>(DecryptedBuffer.get() + off),
len / 4, key);
NORangesSet<Buffer> ifds_decoded;
Buffer decIFD(std::move(DecryptedBuffer), DecryptedBufferSize);
const Buffer Padding(decIFD.getSubView(0, off));
// The Decrypted Root Ifd can not point to preceding padding buffer.
ifds_decoded.insert(Padding);
DataBuffer dbIDD(decIFD, priv->getRootIfdData().getByteOrder());
TiffRootIFD encryptedIFD(nullptr, &ifds_decoded, dbIDD, off);
if (encryptedIFD.hasEntry(TiffTag::SONYGRBGLEVELS)) {
const TiffEntry* wb = encryptedIFD.getEntry(TiffTag::SONYGRBGLEVELS);
if (wb->count != 4)
ThrowRDE("WB has %d entries instead of 4", wb->count);
mRaw->metadata.wbCoeffs[0] = wb->getFloat(1);
mRaw->metadata.wbCoeffs[1] = wb->getFloat(0);
mRaw->metadata.wbCoeffs[2] = wb->getFloat(2);
} else if (encryptedIFD.hasEntry(TiffTag::SONYRGGBLEVELS)) {
const TiffEntry* wb = encryptedIFD.getEntry(TiffTag::SONYRGGBLEVELS);
if (wb->count != 4)
ThrowRDE("WB has %d entries instead of 4", wb->count);
mRaw->metadata.wbCoeffs[0] = wb->getFloat(0);
mRaw->metadata.wbCoeffs[1] = wb->getFloat(1);
mRaw->metadata.wbCoeffs[2] = wb->getFloat(3);
}
}
}
} // namespace rawspeed