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
|
/*
Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
This file is part of libdcp.
libdcp is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
libdcp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with libdcp. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
/** @file src/verify_j2k.cc
* @brief Verification that JPEG2000 files meet requirements
*/
#include "compose.hpp"
#include "data.h"
#include "raw_convert.h"
#include "verify.h"
#include "verify_j2k.h"
#include <memory>
#include <vector>
using std::shared_ptr;
using std::map;
using std::runtime_error;
using std::string;
using std::vector;
using boost::optional;
using dcp::raw_convert;
class InvalidCodestream : public runtime_error
{
public:
InvalidCodestream (string note)
: runtime_error(note)
{}
};
void
dcp::verify_j2k(ConstantData const& j2k, int start_index, int frame_index, int frame_rate, vector<VerificationNote>& notes)
{
/* See ITU-T T800 (visible on https://github.com/Ymagis/ClairMeta/issues/130) */
unsigned int const max_tile_part_size = std::floor(200e6 / (8 * frame_rate));
try {
auto ptr = j2k.data();
auto end = ptr + j2k.size();
map<string, uint8_t> markers = {
{ "SOC", 0x4f },
{ "SIZ", 0x51 },
{ "COD", 0x52 },
{ "COC", 0x53 },
{ "TLM", 0x55 },
{ "QCD", 0x5c },
{ "QCC", 0x5d },
{ "POC", 0x5f },
{ "COM", 0x64 },
{ "SOT", 0x90 },
{ "SOD", 0x93 },
{ "EOC", 0xd9 },
};
auto marker_name_from_id = [&markers](uint8_t b) -> optional<string> {
for (auto const& i: markers) {
if (i.second == b) {
return i.first;
}
}
return {};
};
auto require_marker = [&](string name) {
if (ptr == end || *ptr != 0xff) {
throw InvalidCodestream ("missing marker start byte");
}
++ptr;
if (ptr == end || *ptr != markers[name]) {
throw InvalidCodestream ("missing_marker " + name);
}
++ptr;
};
auto get_8 = [&]() {
if (ptr >= end) {
throw InvalidCodestream ("unexpected end of file");
}
return *ptr++;
};
auto get_16 = [&]() {
if (ptr >= (end - 1)) {
throw InvalidCodestream ("unexpected end of file");
}
auto const a = *ptr++;
auto const b = *ptr++;
return b | (a << 8);
};
auto get_32 = [&]() -> uint32_t {
if (ptr >= (end - 3)) {
throw InvalidCodestream ("unexpected end of file");
}
auto const a = *ptr++;
auto const b = *ptr++;
auto const c = *ptr++;
auto const d = *ptr++;
return d | (c << 8) | (b << 16) | (a << 24);
};
auto require_8 = [&](uint8_t value, string note) {
auto v = get_8 ();
if (v != value) {
throw InvalidCodestream (String::compose(note, v));
}
};
auto require_16 = [&](uint16_t value, string note) {
auto v = get_16 ();
if (v != value) {
throw InvalidCodestream (String::compose(note, v));
}
};
auto require_32 = [&](uint32_t value, string note) {
auto v = get_32 ();
if (v != value) {
throw InvalidCodestream (String::compose(note, v));
}
};
require_marker ("SOC");
require_marker ("SIZ");
auto L_siz = get_16();
if (L_siz != 47) {
throw InvalidCodestream("unexpected SIZ size " + raw_convert<string>(L_siz));
}
get_16(); // CA: codestream capabilities
auto const image_width = get_32();
auto const image_height = get_32();
auto const fourk = image_width > 2048;
require_32 (0, "invalid top-left image x coordinate %1");
require_32 (0, "invalid top-left image y coordinate %1");
auto const tile_width = get_32();
auto const tile_height = get_32();
if (tile_width != image_width || tile_height != image_height) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_TILE_SIZE });
}
require_32 (0, "invalid tile anchor x coordinate %1");
require_32 (0, "invalid tile anchor y coordinate %1");
require_16 (3, "invalid component count %1");
for (auto i = 0; i < 3; ++i) {
require_8 (12 - 1, "invalid bit depth %1");
require_8 (1, "invalid horizontal subsampling factor %1");
require_8 (1, "invalid vertical subsampling factor %1");
}
auto num_COD = 0;
auto num_QCD = 0;
/** number of POC markers in the main header */
auto num_POC_in_main = 0;
/** number of POC markers after the main header */
auto num_POC_after_main = 0;
bool main_header_finished = false;
bool tlm = false;
while (ptr < end)
{
require_8(0xff, "missing marker start byte");
auto marker_id = get_8();
auto marker_name = marker_name_from_id (marker_id);
if (!marker_name) {
char buffer[16];
snprintf (buffer, 16, "%2x", marker_id);
throw InvalidCodestream(String::compose("unknown marker %1", buffer));
} else if (*marker_name == "SOT") {
require_16(10, "invalid SOT size %1");
get_16(); // tile index
auto const tile_part_length = get_32();
auto const tile_part_index = get_8();
auto tile_parts = get_8();
if (!fourk && tile_parts != 3) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_TILE_PARTS_FOR_2K, raw_convert<string>(tile_parts) });
}
if (fourk && tile_parts != 6) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_TILE_PARTS_FOR_4K, raw_convert<string>(tile_parts) });
}
if (tile_part_length > max_tile_part_size) {
VerificationNote note{VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_JPEG2000_TILE_PART_SIZE};
note.set_frame(frame_index);
note.set_component(tile_part_index);
note.set_size(tile_part_length);
notes.push_back(note);
}
main_header_finished = true;
} else if (*marker_name == "SOD") {
while (ptr < (end - 1) && (ptr[0] != 0xff || ptr[1] < 0x90)) {
++ptr;
}
} else if (*marker_name == "SIZ") {
throw InvalidCodestream ("duplicate SIZ marker");
} else if (*marker_name == "COD") {
num_COD++;
get_16(); // length
require_8(1, "invalid coding style %1");
require_8(4, "invalid progression order %1"); // CPRL
require_16(1, "invalid quality layers count %1");
require_8(1, "invalid multi-component transform flag %1");
require_8(fourk ? 6 : 5, "invalid number of transform levels %1");
auto log_code_block_width = get_8();
if (log_code_block_width != 3) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_CODE_BLOCK_WIDTH, raw_convert<string>(4 * (2 << log_code_block_width)) });
}
auto log_code_block_height = get_8();
if (log_code_block_height != 3) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_CODE_BLOCK_HEIGHT, raw_convert<string>(4 * (2 << log_code_block_height)) });
}
require_8(0, "invalid mode variations");
require_8(0, "invalid wavelet transform type %1"); // 9/7 irreversible
require_8(0x77, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
if (fourk) {
require_8(0x88, "invalid precinct size %1");
}
} else if (*marker_name == "QCD") {
num_QCD++;
auto const L_qcd = get_16();
auto quantization_style = get_8();
int guard_bits = (quantization_style >> 5) & 7;
if (fourk && guard_bits != 2) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_GUARD_BITS_FOR_4K, raw_convert<string>(guard_bits) });
}
if (!fourk && guard_bits != 1) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_GUARD_BITS_FOR_2K, raw_convert<string>(guard_bits) });
}
ptr += L_qcd - 3;
} else if (*marker_name == "COC") {
get_16(); // length
auto const coc_component_number = get_8();
/* I don't know if this is really a requirement, but it seems to make sense that there should only
* be components 0, 1 and 2. DoM bug #2395 is about a DCP with COC component number 1 which seems
* like it should be OK.
*/
if (coc_component_number > 2) {
throw InvalidCodestream(String::compose("invalid COC component number %1", coc_component_number));
}
require_8(1, "invalid coding style %1");
require_8(5, "invalid number of transform levels %1");
require_8(3, "invalid code block width exponent %1");
require_8(3, "invalid code block height exponent %1");
require_8(0, "invalid mode variations");
require_8(0x77, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
require_8(0x88, "invalid precinct size %1");
} else if (*marker_name == "TLM") {
auto const len = get_16();
ptr += len - 2;
tlm = true;
} else if (*marker_name == "QCC" || *marker_name == "COM") {
auto const len = get_16();
ptr += len - 2;
} else if (*marker_name == "POC") {
if (main_header_finished) {
num_POC_after_main++;
} else {
num_POC_in_main++;
}
auto require_8_poc = [&](uint16_t value, string note) {
if (get_8() != value) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INCORRECT_JPEG2000_POC_MARKER, String::compose(note, value) });
}
};
auto require_16_poc = [&](uint16_t value, string note) {
if (get_16() != value) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INCORRECT_JPEG2000_POC_MARKER, String::compose(note, value) });
}
};
require_16_poc(16, "invalid length %1");
require_8_poc(0, "invalid RSpoc %1");
require_8_poc(0, "invalid CSpoc %1");
require_16_poc(1, "invalid LYEpoc %1");
require_8_poc(6, "invalid REpoc %1");
require_8_poc(3, "invalid CEpoc %1");
require_8_poc(4, "invalid Ppoc %1");
require_8_poc(6, "invalid RSpoc %1");
require_8_poc(0, "invalid CSpoc %1");
require_16_poc(1, "invalid LYEpoc %1");
require_8_poc(7, "invalid REpoc %1");
require_8_poc(3, "invalid CEpoc %1");
require_8_poc(4, "invalid Ppoc %1");
}
}
if (num_COD == 0) {
throw InvalidCodestream("no COD marker found");
}
if (num_COD > 1) {
throw InvalidCodestream("more than one COD marker found");
}
if (num_QCD == 0) {
throw InvalidCodestream("no QCD marker found");
}
if (num_QCD > 1) {
throw InvalidCodestream("more than one QCD marker found");
}
if (num_POC_in_main != 0 && !fourk) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INCORRECT_JPEG2000_POC_MARKER_COUNT_FOR_2K, raw_convert<string>(num_POC_in_main) });
}
if (num_POC_in_main != 1 && fourk) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INCORRECT_JPEG2000_POC_MARKER_COUNT_FOR_4K, raw_convert<string>(num_POC_in_main) });
}
if (num_POC_after_main != 0) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_JPEG2000_POC_MARKER_LOCATION });
}
if (!tlm) {
notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_JPEG200_TLM_MARKER });
}
}
catch (InvalidCodestream const& e)
{
VerificationNote note({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_JPEG2000_CODESTREAM, string(e.what())});
note.set_frame(start_index + frame_index);
note.set_frame_rate(frame_rate);
notes.push_back(note);
}
}
|