1 /*
2 * JPEG XL Header Parser
3 * Copyright (c) 2023 Leo Izen <leo.izen@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
25 #define UNCHECKED_BITSTREAM_READER 0
26 #define BITSTREAM_READER_LE
30
31 /* read a U32(c_i + u(u_i)) */
33 uint32_t c0, uint32_t
c1, uint32_t
c2, uint32_t c3,
34 uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3)
35 {
37 const uint32_t ubits [4] = {u0, u1, u2, u3};
39
41 if (ubits[choice])
43
45 }
46
48 {
49 return jxl_u32(gb, 0, 1, 2, 18, 0, 0, 4, 6);
50 }
51
52 /* read a U64() */
54 {
56
58 case 1:
60 break;
61 case 2:
63 break;
64 case 3:
70 } else {
72 break;
73 }
74 }
75 break;
76 default:
78 }
79
81 }
82
84 {
85 uint64_t height64 =
height;
/* avoid integer overflow */
86 switch (ratio) {
87 case 1:
89 case 2:
90 return (uint32_t)((height64 * 12) / 10);
91 case 3:
92 return (uint32_t)((height64 * 4) / 3);
93 case 4:
94 return (uint32_t)((height64 * 3) / 2);
95 case 5:
96 return (uint32_t)((height64 * 16) / 9);
97 case 6:
98 return (uint32_t)((height64 * 5) / 4);
99 case 7:
100 return (uint32_t)(height64 * 2);
101 default:
102 break;
103 }
104
105 return 0; /* manual width */
106 }
107
108 /**
109 * validate a Jpeg XL Size Header
110 * @return >= 0 upon valid size, < 0 upon invalid size found
111 */
113 {
115
117 /* small size header */
122 } else {
123 /* large size header */
128 }
129 if (validate && (
width > (1 << 18) ||
height > (1 << 18)
132
133 if (meta) {
136 }
137
138 return 0;
139 }
140
141 /**
142 * validate a Jpeg XL Preview Header
143 * @return >= 0 upon valid size, < 0 upon invalid size found
144 */
146 {
148
150 /* coded height and width divided by eight */
155 } else {
156 /* full height and width coded */
161 }
164
165 return 0;
166 }
167
168 /**
169 * get a Jpeg XL BitDepth Header. These cannot be invalid.
170 */
172 {
175 /* float samples */
178 } else {
179 /* integer samples */
181 }
182 if (meta)
184 }
185
186 /**
187 * validate a Jpeg XL Extra Channel Info bundle
188 * @return >= 0 upon valid, < 0 upon invalid
189 */
191 {
193 int alpha_associated = 0;
194 uint32_t
type, name_len = 0;
195
196 if (!default_alpha) {
198 if (validate &&
type > 63)
203 jxl_u32(gb, 0, 3, 4, 1, 0, 0, 0, 3);
/* dim-shift */
204 /* max of name_len is 1071 = 48 + 2^10 - 1 */
205 name_len = 8 *
jxl_u32(gb, 0, 0, 16, 48, 0, 4, 5, 10);
206 } else {
208 }
209
212
213 /* skip over the name */
215
218
221
223 jxl_u32(gb, 1, 0, 3, 19, 0, 2, 4, 8);
224
228 }
229
230 return 0;
231 }
232
234 {
235 uint64_t extensions =
jxl_u64(gb), extensions_len = 0;
236
239
240 if (!extensions)
241 return 0;
242
243 for (
int i = 0;
i < 64;
i++) {
244 if (extensions & (UINT64_C(1) <<
i))
248 }
249
250 if (extensions_len > INT_MAX ||
get_bits_left(gb) <= extensions_len)
252
254
255 return 0;
256 }
257
259 {
261
262 int all_default, extra_fields = 0;
263 int xyb_encoded = 1, have_icc_profile = 0;
264 int animation_offset = 0, have_timecodes = 0;
265
270
272 uint32_t num_extra_channels = 0;
274
278
281
285
287 if (!all_default)
289
290 if (extra_fields) {
292 if (orientation > 3 && meta)
294
295 /*
296 * intrinstic size
297 * any size header here is valid, but as it
298 * is variable length we have to read it
299 */
302
303 /* preview header */
308 }
309
310 /* animation header */
313 tb.
den =
jxl_u32(gb, 100, 1000, 1, 1, 0, 0, 10, 30);
314 tb.
num =
jxl_u32(gb, 1, 1001, 1, 1, 0, 0, 8, 10);
315 jxl_u32(gb, 0, 0, 0, 0, 0, 3, 16, 32);
317 }
318 }
319
320 if (animation_offset && meta) {
324 }
325
328
329 if (!all_default) {
331
332 /* modular_16bit_buffers must equal 1 */
333 if (!
get_bits1(gb) && validate && validate < 10)
335
336 num_extra_channels =
jxl_u32(gb, 0, 1, 2, 1, 0, 0, 4, 12);
337 if (num_extra_channels > 4 && validate && validate < 10)
339 for (uint32_t
i = 0;
i < num_extra_channels;
i++) {
345 }
346
348
349 /* color encoding bundle */
353 if (color_space > 63 && validate)
355 if (!have_icc_profile) {
358 if (white_point > 63 && validate)
361 /* ux and uy values */
362 jxl_u32(gb, 0, 524288, 1048576, 2097152, 19, 19, 20, 21);
363 jxl_u32(gb, 0, 524288, 1048576, 2097152, 19, 19, 20, 21);
364 }
366 /* primaries */
371 /* ux/uy values for r,g,b */
372 for (
int i = 0;
i < 6;
i++) {
373 jxl_u32(gb, 0, 524288, 1048576, 2097152, 19, 19, 20, 21);
376 }
377 }
378 }
379 }
380
381 /* transfer characteristics */
383 /* gamma */
385 } else {
386 /* transfer function */
388 if (trc > 63 && validate)
391 }
392
393 /* rendering intent */
396 }
397 }
398
399 /* tone mapping bundle */
402
406 }
407
408 if (meta) {
411 meta->
csp = color_space;
413 meta->
wp = white_point;
418 }
419
420 /* default transform */
422 /* opsin inverse matrix */
425 /* cw_mask and default weights */
432 }
433
434 if (!have_icc_profile) {
436 if (bits_remaining &&
get_bits(gb, bits_remaining))
438 }
439
442
444 }
445
446 /*
447 * copies as much of the codestream into the buffer as possible
448 * pass a shorter buflen to request less
449 * returns the number of bytes consumed from input, may be greater than input_len
450 * if the input doesn't end on an ISOBMFF-box boundary
451 */
453 uint8_t *
buffer,
int buflen,
int *copied)
454 {
457 int last_box = 0;
459
460 while (1) {
463 int head_size = 8;
464
467
468 size = bytestream2_get_be32(&gb);
469 tag = bytestream2_get_le32(&gb);
470
474 size = bytestream2_get_be64(&gb);
475 head_size = 16;
476 }
477 /* invalid ISOBMFF size */
482
483 if (
tag ==
MKTAG(
'j',
'x',
'l',
'p')) {
484 uint32_t idx;
487 idx = bytestream2_get_be32(&gb);
488 if (idx >= UINT32_C(0x80000000))
489 last_box = 1;
494 }
495 }
497 last_box = 1;
498
499 /*
500 * size = 0 means "until EOF". this is legal but uncommon
501 * here we just set it to the remaining size of the probe buffer
502 */
505 else
507
509 if (
size > buflen - *copied)
510 size = buflen - *copied;
511 /*
512 * arbitrary chunking of the payload makes this memcpy hard to avoid
513 * in practice this will only be performed one or two times at most
514 */
516 } else {
518 }
520 break;
521 }
522
524 }