35 #define UNCHECKED_BITSTREAM_READER 0
36 #define BITSTREAM_READER_LE
43 #define JXL_FLAG_NOISE 1
44 #define JXL_FLAG_PATCHES 2
45 #define JXL_FLAG_SPLINES 16
46 #define JXL_FLAG_USE_LF_FRAME 32
47 #define JXL_FLAG_SKIP_ADAPTIVE_LF_SMOOTH 128
49 #define MAX_PREFIX_ALPHABET_SIZE (1u << 15)
51 #define clog1p(x) (ff_log2(x) + !!(x))
52 #define unpack_signed(x) (((x) & 1 ? -(x)-1 : (x))/2)
53 #define div_ceil(x, y) (((x) - 1) / (y) + 1)
54 #define vlm(a,b) {.sym = (a), .len = (b)}
170 vlm(0, 2),
vlm(4, 2),
vlm(3, 2),
vlm(2, 3),
vlm(0, 2),
vlm(4, 2),
vlm(3, 2),
vlm(1, 4),
171 vlm(0, 2),
vlm(4, 2),
vlm(3, 2),
vlm(2, 3),
vlm(0, 2),
vlm(4, 2),
vlm(3, 2),
vlm(5, 4),
176 vlm(10, 3),
vlm(12, 7),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
177 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
178 vlm(10, 3),
vlm(0, 5),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
179 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
180 vlm(10, 3),
vlm(11, 6),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
181 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
182 vlm(10, 3),
vlm(0, 5),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
183 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
184 vlm(10, 3),
vlm(13, 7),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
185 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
186 vlm(10, 3),
vlm(0, 5),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
187 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
188 vlm(10, 3),
vlm(11, 6),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
189 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
190 vlm(10, 3),
vlm(0, 5),
vlm(7, 3),
vlm(3, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(5, 4),
191 vlm(10, 3),
vlm(4, 4),
vlm(7, 3),
vlm(1, 4),
vlm(6, 3),
vlm(8, 3),
vlm(9, 3),
vlm(2, 4),
195 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
214 uint32_t c0, uint32_t
c1, uint32_t
c2, uint32_t c3,
215 uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3)
218 const uint32_t ubits [4] = {u0, u1, u2, u3};
282 *hybrid_uint = token;
320 if (dec->
state < (1 << 16))
322 dec->
state &= 0xFFFFFFFF;
329 uint32_t
context, uint32_t *hybrid_uint)
382 int len = 0,
shift, omit_log = -1, omit_pos = -1;
383 int prev = 0, num_same = 0;
384 uint32_t total_count = 0;
385 uint8_t logcounts[258] = { 0 };
386 uint8_t same[258] = { 0 };
387 const int table_size = 1 << log_alphabet_size;
398 dist->
freq[v2] = (1 << 12) - dist->
freq[v1];
404 dist->
freq[x] = 1 << 12;
441 if (logcounts[
i] == 13) {
447 if (logcounts[
i] > omit_log) {
448 omit_log = logcounts[
i];
452 if (omit_pos < 0 || omit_pos + 1 < dist->
alphabet_size && logcounts[omit_pos + 1] == 13)
457 num_same = same[
i] - 1;
458 prev =
i > 0 ? dist->
freq[
i - 1] : 0;
461 dist->
freq[
i] = prev;
464 if (
i == omit_pos || !logcounts[
i])
466 if (logcounts[
i] == 1) {
469 int bitcount =
FFMIN(
FFMAX(0,
shift - ((12 - logcounts[
i] + 1) >> 1)), logcounts[
i] - 1);
470 dist->
freq[
i] = (1 << (logcounts[
i] - 1)) + (
get_bitsz(gb, bitcount) << (logcounts[
i] - 1 - bitcount));
473 total_count += dist->
freq[
i];
475 dist->
freq[omit_pos] = (1 << 12) - total_count;
537 for (
int i = 0;
i < 256;
i++)
544 for (
int j =
index; j > 0; j--)
564 uint32_t bucket_size, table_size;
565 uint8_t overfull[256], underfull[256];
566 int overfull_pos = 0, underfull_pos = 0;
569 table_size = 1 << log_alphabet_size;
572 for (
int i = 0;
i < table_size;
i++) {
584 overfull[overfull_pos++] =
i;
585 else if (dist->
cutoffs[
i] < bucket_size)
586 underfull[underfull_pos++] =
i;
591 underfull[underfull_pos++] =
i;
594 while (overfull_pos) {
599 u = underfull[--underfull_pos];
600 o = overfull[--overfull_pos];
601 by = bucket_size - dist->
cutoffs[
u];
605 if (dist->
cutoffs[o] < bucket_size)
606 underfull[underfull_pos++] = o;
607 else if (dist->
cutoffs[o] > bucket_size)
608 overfull[overfull_pos++] = o;
611 for (
int i = 0;
i < table_size;
i++) {
612 if (dist->
cutoffs[
i] == bucket_size) {
626 int nsym, tree_select,
bits;
632 for (
int i = 0;
i < nsym;
i++)
643 lens[0] = 1, lens[1] = 1, lens[2] = 0, lens[3] = 0;
644 if (symbols[1] < symbols[0])
645 FFSWAP(int16_t, symbols[0], symbols[1]);
649 lens[0] = 1, lens[1] = 2, lens[2] = 2, lens[3] = 0;
650 if (symbols[2] < symbols[1])
651 FFSWAP(int16_t, symbols[1], symbols[2]);
656 lens[0] = 1, lens[1] = 2, lens[2] = 3, lens[3] = 3;
657 if (symbols[3] < symbols[2])
658 FFSWAP(int16_t, symbols[2], symbols[3]);
661 lens[0] = 2, lens[1] = 2, lens[2] = 2, lens[3] = 2;
663 if (symbols[1] < symbols[0])
664 FFSWAP(int16_t, symbols[0], symbols[1]);
665 if (symbols[3] < symbols[2])
666 FFSWAP(int16_t, symbols[2], symbols[3]);
667 if (symbols[1] <= symbols[2])
669 FFSWAP(int16_t, symbols[1], symbols[2]);
684 int8_t level1_lens[18] = { 0 };
685 int8_t level1_lens_s[18] = { 0 };
686 int16_t level1_syms[18] = { 0 };
687 uint32_t level1_codecounts[19] = { 0 };
689 int8_t *level2_lens, *level2_lens_s;
690 int16_t *level2_syms;
691 uint32_t *level2_codecounts;
693 int repeat_count_prev = 0, repeat_count_zero = 0, prev = 8;
694 int total_code = 0,
len, hskip, num_codes = 0,
ret;
696 VLC level1_vlc = { 0 };
708 level1_codecounts[0] = hskip;
709 for (
int i = hskip;
i < 18;
i++) {
715 level1_codecounts[
len]++;
717 total_code += (32 >>
len);
720 if (total_code >= 32) {
721 level1_codecounts[0] += 18 -
i - 1;
726 if (total_code != 32 && num_codes >= 2 || num_codes < 1) {
731 for (
int i = 1;
i < 19;
i++)
732 level1_codecounts[
i] += level1_codecounts[
i - 1];
734 for (
int i = 17;
i >= 0;
i--) {
735 int idx = --level1_codecounts[level1_lens[
i]];
736 level1_lens_s[idx] = level1_lens[
i];
737 level1_syms[idx] =
i;
752 level2_lens = (int8_t *)buf;
770 if (repeat_count_prev)
771 extra += 4 * (repeat_count_prev - 2) - repeat_count_prev;
773 for (
int j = 0; j < extra; j++)
774 level2_lens[
i + j] = prev;
775 total_code += (32768 >> prev) * extra;
777 repeat_count_prev += extra;
778 repeat_count_zero = 0;
779 level2_codecounts[prev] += extra;
780 }
else if (
len == 17) {
782 if (repeat_count_zero > 0)
783 extra += 8 * (repeat_count_zero - 2) - repeat_count_zero;
786 repeat_count_prev = 0;
787 repeat_count_zero += extra;
788 level2_codecounts[0] += extra;
790 level2_lens[
i] =
len;
791 repeat_count_prev = repeat_count_zero = 0;
793 total_code += (32768 >>
len);
796 level2_codecounts[
len]++;
798 if (total_code >= 32768) {
804 if (total_code != 32768 && level2_codecounts[0] < dist->
alphabet_size - 1) {
810 level2_codecounts[
i] += level2_codecounts[
i - 1];
813 int idx = --level2_codecounts[level2_lens[
i]];
814 level2_lens_s[idx] = level2_lens[
i];
815 level2_syms[idx] =
i;
926 memset(dec, 0,
sizeof(*dec));
942 uint32_t hybrid_uint;
956 if (
b1 >=
'a' && b1 <= 'z' || b1 >=
'A' &&
b1 <=
'Z')
958 else if (
b1 >=
'0' &&
b1 <=
'9' ||
b1 ==
'.' ||
b1 ==
',')
962 else if (
b1 > 1 &&
b1 < 16)
964 else if (
b1 > 240 &&
b1 < 255)
971 if (
b2 >=
'a' && b2 <= 'z' || b2 >=
'A' &&
b2 <=
'Z')
973 else if (
b2 >=
'0' &&
b2 <=
'9' ||
b2 ==
'.' ||
b2 ==
',')
982 return 1 + p1 + p2 * 8;
1048 }
else if (meta->
trc > 0) {
1049 if (meta->
trc > 45355 && meta->
trc < 45555)
1051 else if (meta->
trc > 35614 && meta->
trc < 35814)
1079 uint32_t last = 0, last2 = 0;
1081 uint64_t enc_size =
jxl_u64(gb);
1082 uint64_t output_size = 0;
1083 int out_size_shift = 0;
1085 if (!enc_size || enc_size > (1 << 22))
1111 if (out_size_shift < 63) {
1112 output_size += (
ret & UINT64_C(0x7F)) << out_size_shift;
1113 if (!(
ret & 0x80)) {
1114 out_size_shift = 63;
1116 out_size_shift += 7;
1117 if (out_size_shift > 56) {
1122 }
else if (output_size < 132) {
1138 uint64_t extensions =
jxl_u64(gb), extensions_len = 0;
1146 for (
int i = 0;
i < 64;
i++) {
1147 if (extensions & (UINT64_C(1) <<
i))
1148 extensions_len +=
jxl_u64(gb);
1153 if (extensions_len > INT_MAX ||
get_bits_left(gb) < extensions_len)
1163 int all_default, do_yCbCr = 0, num_passes = 1,
ret;
1164 int group_size_shift = 1, lf_level = 0, save_as_ref = 0;
1165 int have_crop = 0, full_frame = 1, resets_canvas = 1, upsampling = 1;
1170 uint32_t name_len, num_groups, num_lf_groups, group_dim, lf_group_dim, toc_count;
1187 upsampling =
jxl_u32(gb, 1, 2, 4, 8, 0, 0, 0, 0);
1193 group_size_shift =
get_bits(gb, 2);
1197 num_passes =
jxl_u32(gb, 1, 2, 3, 4, 0, 0, 0, 3);
1198 if (num_passes != 1) {
1199 int num_ds =
jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 1);
1202 for (
int i = 0;
i < num_ds;
i++)
1203 jxl_u32(gb, 0, 1, 2, 0, 0, 0, 0, 3);
1212 uint32_t ux0 =
jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1213 uint32_t uy0 =
jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1217 width =
jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1219 full_frame = x0 <= 0 && y0 <= 0 && width + x0 >= meta->
coded_width
1228 jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 2);
1250 (resets_canvas && !
frame->is_last && (!
duration || save_as_ref)
1253 name_len = 8 *
jxl_u32(gb, 0, 0, 16, 48, 0, 4, 5, 10);
1302 group_dim = 128 << group_size_shift;
1303 lf_group_dim = group_dim << 3;
1306 if (num_groups == 1 && num_passes == 1)
1309 toc_count = 2 + num_lf_groups + num_groups * num_passes;
1314 uint32_t end, lehmer = 0;
1323 if (end > toc_count) {
1327 for (uint32_t
i = 0;
i < end;
i++) {
1338 for (uint32_t
i = 0;
i < toc_count;
i++) {
1339 frame->body_length += 8 *
jxl_u32(gb, 0, 1024, 17408, 4211712, 10, 14, 22, 30);
1354 if (
ctx->skip > buf_size)
1358 buf_size -=
ctx->skip;
1373 size = bytestream2_get_be32(&gb);
1377 size = bytestream2_get_be64(&gb);
1383 if (size <= head_size + 4 || size > INT_MAX -
ctx->skip)
1396 const uint8_t *buf,
int buf_size)
1398 int ret, cs_buflen, header_skip;
1399 const uint8_t *cs_buffer;
1402 if (
ctx->skip > buf_size)
1406 buf_size -=
ctx->skip;
1414 ctx->collected_size =
ret;
1419 cs_buffer =
ctx->cs_buffer;
1423 cs_buflen = buf_size;
1426 if (!
ctx->codestream_length) {
1428 if (header_skip < 0)
1430 ctx->codestream_length = header_skip;
1435 return ctx->collected_size;
1443 if (!
ctx->skipped_icc &&
ctx->codestream.meta.have_icc_profile) {
1447 ctx->skipped_icc = 1;
1459 ctx->codestream_length +=
ctx->codestream.frame.total_length;
1460 if (
ctx->codestream.frame.is_last)
1461 return ctx->codestream_length / 8;
1469 const uint8_t **poutbuf,
int *poutbuf_size,
1470 const uint8_t *buf,
int buf_size)
1481 if ((!
ctx->container || !
ctx->codestream_length) && !
ctx->next) {
1490 if (
ctx->container &&
ctx->next >= 0) {
1501 next =
ctx->next -
ctx->pc.index;
1504 if (next > buf_size)
1512 *poutbuf_size = buf_size;
1514 ctx->codestream_length = 0;
1515 ctx->collected_size = 0;
1519 ctx->skipped_icc = 0;
1521 memset(&
ctx->codestream, 0,
sizeof(
ctx->codestream));