-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathAbstractLJpegDecoder.cpp
More file actions
293 lines (238 loc) · 9.06 KB
/
Copy pathAbstractLJpegDecoder.cpp
File metadata and controls
293 lines (238 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
RawSpeed - RAW file decoder.
Copyright (C) 2009-2014 Klaus Post
Copyright (C) 2017 Axel Waggershauser
Copyright (C) 2017 Roman Lebedev
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 "decompressors/AbstractLJpegDecoder.h"
#include "adt/Invariant.h"
#include "adt/Optional.h"
#include "adt/Point.h"
#include "codes/AbstractPrefixCode.h"
#include "codes/HuffmanCode.h"
#include "codes/PrefixCodeDecoder.h"
#include "common/RawImage.h"
#include "decoders/RawDecoderException.h"
#include "decompressors/JpegMarkers.h"
#include "io/ByteStream.h"
#include "io/Endianness.h"
#include <array>
#include <cassert>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
namespace rawspeed {
void AbstractLJpegDecoder::anchor() const {
// Empty out-of-line definition for the purpose of anchoring
// the class's vtable to this Translational Unit.
}
AbstractLJpegDecoder::AbstractLJpegDecoder(ByteStream bs, RawImage img)
: input(bs), mRaw(std::move(img)) {
input.setByteOrder(Endianness::big);
if (!mRaw->dim.hasPositiveArea())
ThrowRDE("Image has zero size");
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
// Yeah, sure, here it would be just dumb to leave this for production :)
if (mRaw->dim.x > 19440 || mRaw->dim.y > 6304) {
ThrowRDE("Unexpected image dimensions found: (%u; %u)", mRaw->dim.x,
mRaw->dim.y);
}
#endif
}
void AbstractLJpegDecoder::decodeSOI() {
if (getNextMarker(false) != JpegMarker::SOI)
ThrowRDE("Image did not start with SOI. Probably not an LJPEG");
struct {
bool DRI = false;
bool DHT = false;
bool SOF = false;
bool SOS = false;
} FoundMarkers;
for (JpegMarker m; (m = getNextMarker(true)) != JpegMarker::EOI;) {
ByteStream data(input.getStream(input.peekU16()));
data.skipBytes(2); // headerLength
switch (m) {
case JpegMarker::DHT:
if (FoundMarkers.SOS)
ThrowRDE("Found second DHT marker after SOS");
// there can be more than one DHT markers.
// FIXME: do we really want to reparse and use the last one?
parseDHT(data);
FoundMarkers.DHT = true;
break;
case JpegMarker::SOF3:
if (FoundMarkers.SOS)
ThrowRDE("Found second SOF marker after SOS");
if (FoundMarkers.SOF)
ThrowRDE("Found second SOF marker");
// SOF is not required to be after DHT
parseSOF(data, &frame);
FoundMarkers.SOF = true;
break;
case JpegMarker::SOS:
if (FoundMarkers.SOS)
ThrowRDE("Found second SOS marker");
if (!FoundMarkers.DHT)
ThrowRDE("Did not find DHT marker before SOS.");
if (!FoundMarkers.SOF)
ThrowRDE("Did not find SOF marker before SOS.");
parseSOS(data);
FoundMarkers.SOS = true;
if (erratumImplicitEOIMarkerAfterScan())
return;
break;
case JpegMarker::DQT:
ThrowRDE("Not a valid RAW file.");
case JpegMarker::DRI:
if (FoundMarkers.DRI)
ThrowRDE("Found second DRI marker");
parseDRI(data);
FoundMarkers.DRI = true;
break;
default: // Just let it skip to next marker
break;
}
}
if (!FoundMarkers.SOS)
ThrowRDE("Did not find SOS marker.");
}
void AbstractLJpegDecoder::parseSOF(ByteStream sofInput, SOFInfo* sof) {
sof->prec = sofInput.getByte();
sof->h = sofInput.getU16();
sof->w = sofInput.getU16();
sof->cps = sofInput.getByte();
if (sof->prec < 2 || sof->prec > 16)
ThrowRDE("Invalid precision (%u).", sof->prec);
if (sof->h == 0 || sof->w == 0)
ThrowRDE("Frame width or height set to zero");
if (sof->cps > 4 || sof->cps < 1)
ThrowRDE("Only from 1 to 4 components are supported.");
if (sof->cps < mRaw->getCpp()) {
ThrowRDE("Component count should be no less than sample count (%u vs %u).",
sof->cps, mRaw->getCpp());
}
if (sof->cps > static_cast<uint32_t>(mRaw->dim.x)) {
ThrowRDE("Component count should be no greater than row length (%u vs %u).",
sof->cps, mRaw->dim.x);
}
if (sofInput.getRemainSize() != 3 * sof->cps)
ThrowRDE("Header size mismatch.");
for (uint32_t i = 0; i < sof->cps; i++) {
sof->compInfo[i].componentId = sofInput.getByte();
uint32_t subs = sofInput.getByte();
frame.compInfo[i].superV = subs & 0xf;
frame.compInfo[i].superH = subs >> 4;
if (frame.compInfo[i].superV < 1 || frame.compInfo[i].superV > 4)
ThrowRDE("Horizontal sampling factor is invalid.");
if (frame.compInfo[i].superH < 1 || frame.compInfo[i].superH > 4)
ThrowRDE("Horizontal sampling factor is invalid.");
uint32_t Tq = sofInput.getByte();
if (Tq != 0)
ThrowRDE("Quantized components not supported.");
}
if (static_cast<int>(sof->compInfo[0].superH) !=
mRaw->metadata.subsampling.x ||
static_cast<int>(sof->compInfo[0].superV) != mRaw->metadata.subsampling.y)
ThrowRDE("LJpeg's subsampling does not match image's subsampling.");
sof->initialized = true;
}
void AbstractLJpegDecoder::parseSOS(ByteStream sos) {
invariant(frame.initialized);
if (sos.getRemainSize() != 1 + 2 * frame.cps + 3)
ThrowRDE("Invalid SOS header length.");
if (uint32_t soscps = sos.getByte(); frame.cps != soscps)
ThrowRDE("Component number mismatch.");
for (uint32_t i = 0; i < frame.cps; i++) {
uint32_t cs = sos.getByte();
uint32_t td = sos.getByte() >> 4;
if (td >= huff.size() || !huff[td])
ThrowRDE("Invalid Huffman table selection.");
int ciIndex = -1;
for (uint32_t j = 0; j < frame.cps; ++j) {
if (frame.compInfo[j].componentId == cs)
ciIndex = j;
}
if (ciIndex == -1)
ThrowRDE("Invalid Component Selector");
frame.compInfo[ciIndex].dcTblNo = td;
}
// Get predictor, see table H.1 from the JPEG spec
predictorMode = sos.getByte();
// The spec says predictorMode is in [0..7], but Hasselblad uses '8'.
if (predictorMode > 8)
ThrowRDE("Invalid predictor mode.");
// Se + Ah Not used in LJPEG
if (sos.getByte() != 0)
ThrowRDE("Se/Ah not zero.");
Pt = sos.getByte(); // Point Transform
if (Pt > 15)
ThrowRDE("Invalid Point transform.");
if (Pt != 0)
ThrowRDE("Point transform not supported.");
const auto scanLength = decodeScan();
// FIXME: invariant(scanLength != 0);
input.skipBytes(scanLength);
}
void AbstractLJpegDecoder::parseDHT(ByteStream dht) {
while (dht.getRemainSize() > 0) {
uint32_t b = dht.getByte();
if (uint32_t htClass = b >> 4; htClass != 0)
ThrowRDE("Unsupported Table class.");
uint32_t htIndex = b & 0xf;
if (htIndex >= huff.size())
ThrowRDE("Invalid huffman table destination id.");
if (huff[htIndex] != nullptr)
ThrowRDE("Duplicate table definition");
// Temporary table, used during parsing LJpeg.
HuffmanCode<BaselineCodeTag> hc;
// copy 16 bytes from input stream to number of codes per length table
uint32_t nCodes = hc.setNCodesPerLength(dht.getBuffer(16));
// spec says 16 different codes is max but Hasselblad violates that -> 17
if (nCodes > 17)
ThrowRDE("Invalid DHT table.");
// copy nCodes bytes from input stream to code values table
const auto codesBuf = dht.getBuffer(nCodes);
hc.setCodeValues(codesBuf.getAsArray1DRef());
// see if we already have a PrefixCodeDecoder with the same codes
assert(PrefixCodeDecoderStore.size() == huffmanCodeStore.size());
for (unsigned index = 0; index != PrefixCodeDecoderStore.size(); ++index) {
if (*huffmanCodeStore[index] == hc)
huff[htIndex] = PrefixCodeDecoderStore[index].get();
}
if (!huff[htIndex]) {
huffmanCodeStore.emplace_back(std::make_unique<decltype(hc)>(hc));
// setup new hc and put it into the store
auto dHT = std::make_unique<PrefixCodeDecoder<>>(std::move(hc));
dHT->setup(fullDecodeHT, fixDng16Bug);
huff[htIndex] = dHT.get();
PrefixCodeDecoderStore.emplace_back(std::move(dHT));
}
}
}
void AbstractLJpegDecoder::parseDRI(ByteStream dri) {
if (dri.getRemainSize() != 2)
ThrowRDE("Invalid DRI header length.");
numMCUsPerRestartInterval = dri.getU16();
}
JpegMarker AbstractLJpegDecoder::getNextMarker(bool allowskip) {
if (Optional<ByteStream> markerPos = advanceToNextMarker(input, allowskip))
input = *markerPos;
else
ThrowRDE("(Noskip) Expected marker not found. Probably corrupt file.");
JpegMarker m = *peekMarker(input);
input.skipBytes(2); // Skip the bytes we've just consumed.
return m;
}
} // namespace rawspeed