FFmpeg
jpegxl_parser.c
Go to the documentation of this file.
1 /**
2  * JPEG XL 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 <errno.h>
23 #include <stdint.h>
24 #include <string.h>
25 
26 #include "libavutil/attributes.h"
27 #include "libavutil/error.h"
28 #include "libavutil/intmath.h"
29 #include "libavutil/macros.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/pixfmt.h"
32 
33 #include "bytestream.h"
34 #include "codec_id.h"
35 #define UNCHECKED_BITSTREAM_READER 0
36 #define BITSTREAM_READER_LE
37 #include "get_bits.h"
38 #include "jpegxl.h"
39 #include "jpegxl_parse.h"
40 #include "parser.h"
41 #include "vlc.h"
42 
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
48 
49 #define MAX_PREFIX_ALPHABET_SIZE (1u << 15)
50 
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)}
55 
56 typedef struct JXLHybridUintConf {
58  uint32_t msb_in_token;
59  uint32_t lsb_in_token;
61 
62 typedef struct JXLSymbolDistribution {
65  /* this is the actual size of the alphabet */
67  /* ceil(log(alphabet_size)) */
69 
70  /* for prefix code distributions */
72  /* in case bits == 0 */
73  uint32_t default_symbol;
74 
75  /*
76  * each (1 << log_alphabet_size) length
77  * with log_alphabet_size <= 8
78  */
79  /* frequencies associated with this Distribution */
80  uint32_t freq[258];
81  /* cutoffs for using the symbol table */
82  uint16_t cutoffs[258];
83  /* the symbol table for this distribution */
84  uint16_t symbols[258];
85  /* the offset for symbols */
86  uint16_t offsets[258];
87 
88  /* if this distribution contains only one symbol this is its index */
89  int uniq_pos;
91 
92 typedef struct JXLDistributionBundle {
93  /* lz77 flags */
95  uint32_t lz77_min_symbol;
96  uint32_t lz77_min_length;
98 
99  /* one entry for each distribution */
100  uint8_t *cluster_map;
101  /* length of cluster_map */
102  int num_dist;
103 
104  /* one for each cluster */
107 
108  /* whether to use brotli prefixes or ans */
110  /* bundle log alphabet size, dist ones may be smaller */
113 
114 typedef struct JXLEntropyDecoder {
115 
116  /* state is a positive 32-bit integer, or -1 if unset */
117  int64_t state;
118 
119  /* lz77 values */
120  uint32_t num_to_copy;
121  uint32_t copy_pos;
122  uint32_t num_decoded;
123 
124  /* length is (1 << 20) */
125  /* if lz77 is enabled for this bundle */
126  /* if lz77 is disabled it's NULL */
127  uint32_t *window;
128 
129  /* primary bundle associated with this distribution */
131 
132  /* for av_log */
133  void *logctx;
135 
136 typedef struct JXLFrame {
139 
140  int is_last;
142 
143  uint32_t total_length;
144  uint32_t body_length;
145 } JXLFrame;
146 
147 typedef struct JXLCodestream {
150 } JXLCodestream;
151 
152 typedef struct JXLParseContext {
155 
156  /* using ISOBMFF-based container */
158  int skip;
159  int copied;
163  int next;
164 
165  uint8_t cs_buffer[4096];
167 
168 /* used for reading brotli prefixes */
169 static const VLCElem level0_table[16] = {
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),
172 };
173 
174 /* prefix table for populating ANS distribution */
175 static const VLCElem dist_prefix_table[128] = {
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),
192 };
193 
194 static const uint8_t prefix_codelen_map[18] = {
195  1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
196 };
197 
198 /**
199  * Read a variable-length 8-bit integer.
200  * Used when populating the ANS frequency tables.
201  */
203 {
204  int n;
205  if (!get_bits1(gb))
206  return 0;
207  n = get_bits(gb, 3);
208 
209  return get_bitsz(gb, n) | (1 << n);
210 }
211 
212 /* read a U32(c_i + u(u_i)) */
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)
216 {
217  const uint32_t constants[4] = {c0, c1, c2, c3};
218  const uint32_t ubits [4] = {u0, u1, u2, u3};
219  uint32_t ret, choice = get_bits(gb, 2);
220 
221  ret = constants[choice];
222  if (ubits[choice])
223  ret += get_bits_long(gb, ubits[choice]);
224 
225  return ret;
226 }
227 
228 /* read a U64() */
229 static uint64_t jxl_u64(GetBitContext *gb)
230 {
231  uint64_t shift = 12, ret;
232 
233  switch (get_bits(gb, 2)) {
234  case 1:
235  ret = 1 + get_bits(gb, 4);
236  break;
237  case 2:
238  ret = 17 + get_bits(gb, 8);
239  break;
240  case 3:
241  ret = get_bits(gb, 12);
242  while (get_bits1(gb)) {
243  if (shift < 60) {
244  ret |= (uint64_t)get_bits(gb, 8) << shift;
245  shift += 8;
246  } else {
247  ret |= (uint64_t)get_bits(gb, 4) << shift;
248  break;
249  }
250  }
251  break;
252  default:
253  ret = 0;
254  }
255 
256  return ret;
257 }
258 
259 static int read_hybrid_uint_conf(GetBitContext *gb, JXLHybridUintConf *conf, int log_alphabet_size)
260 {
261  conf->split_exponent = get_bitsz(gb, clog1p(log_alphabet_size));
262  if (conf->split_exponent == log_alphabet_size) {
263  conf->msb_in_token = conf->lsb_in_token = 0;
264  return 0;
265  }
266 
267  conf->msb_in_token = get_bitsz(gb, clog1p(conf->split_exponent));
268  if (conf->msb_in_token > conf->split_exponent)
269  return AVERROR_INVALIDDATA;
270  conf->lsb_in_token = get_bitsz(gb, clog1p(conf->split_exponent - conf->msb_in_token));
271  if (conf->msb_in_token + conf->lsb_in_token > conf->split_exponent)
272  return AVERROR_INVALIDDATA;
273 
274  return 0;
275 }
276 
277 static int read_hybrid_uint(GetBitContext *gb, const JXLHybridUintConf *conf, uint32_t token, uint32_t *hybrid_uint)
278 {
279  uint32_t n, low, split = 1 << conf->split_exponent;
280 
281  if (token < split) {
282  *hybrid_uint = token;
283  return 0;
284  }
285 
286  n = conf->split_exponent - conf->lsb_in_token - conf->msb_in_token +
287  ((token - split) >> (conf->msb_in_token + conf->lsb_in_token));
288  if (n >= 32)
289  return AVERROR_INVALIDDATA;
290  low = token & ((1 << conf->lsb_in_token) - 1);
291  token >>= conf->lsb_in_token;
292  token &= (1 << conf->msb_in_token) - 1;
293  token |= 1 << conf->msb_in_token;
294  *hybrid_uint = (((token << n) | get_bits_long(gb, n)) << conf->lsb_in_token ) | low;
295 
296  return 0;
297 }
298 
299 static inline uint32_t read_prefix_symbol(GetBitContext *gb, const JXLSymbolDistribution *dist)
300 {
301  if (!dist->vlc.bits)
302  return dist->default_symbol;
303 
304  return get_vlc2(gb, dist->vlc.table, dist->vlc.bits, 1);
305 }
306 
308 {
309  uint32_t index, i, pos, symbol, offset;
310 
311  if (dec->state < 0)
312  dec->state = get_bits_long(gb, 32);
313 
314  index = dec->state & 0xFFF;
315  i = index >> dist->log_bucket_size;
316  pos = index & ((1 << dist->log_bucket_size) - 1);
317  symbol = pos >= dist->cutoffs[i] ? dist->symbols[i] : i;
318  offset = pos >= dist->cutoffs[i] ? dist->offsets[i] + pos : pos;
319  dec->state = dist->freq[symbol] * (dec->state >> 12) + offset;
320  if (dec->state < (1 << 16))
321  dec->state = (dec->state << 16) | get_bits(gb, 16);
322  dec->state &= 0xFFFFFFFF;
323 
324  return symbol;
325 }
326 
328  const JXLDistributionBundle *bundle,
329  uint32_t context, uint32_t *hybrid_uint)
330 {
331  int ret;
332  uint32_t token, distance;
333  const JXLSymbolDistribution *dist;
334 
335  if (dec->num_to_copy > 0) {
336  *hybrid_uint = dec->window[dec->copy_pos++ & 0xFFFFF];
337  dec->num_to_copy--;
338  dec->window[dec->num_decoded++ & 0xFFFFF] = *hybrid_uint;
339  return 0;
340  }
341 
342  if (context >= bundle->num_dist)
343  return AVERROR(EINVAL);
344  if (bundle->cluster_map[context] >= bundle->num_clusters)
345  return AVERROR_INVALIDDATA;
346 
347  dist = &bundle->dists[bundle->cluster_map[context]];
348  if (bundle->use_prefix_code)
349  token = read_prefix_symbol(gb, dist);
350  else
351  token = read_ans_symbol(gb, dec, dist);
352 
353  if (bundle->lz77_enabled && token >= bundle->lz77_min_symbol) {
354  const JXLSymbolDistribution *lz77dist = &bundle->dists[bundle->cluster_map[bundle->num_dist - 1]];
355  ret = read_hybrid_uint(gb, &bundle->lz_len_conf, token - bundle->lz77_min_symbol, &dec->num_to_copy);
356  if (ret < 0)
357  return ret;
358  dec->num_to_copy += bundle->lz77_min_length;
359  if (bundle->use_prefix_code)
360  token = read_prefix_symbol(gb, lz77dist);
361  else
362  token = read_ans_symbol(gb, dec, lz77dist);
363  ret = read_hybrid_uint(gb, &lz77dist->config, token, &distance);
364  if (ret < 0)
365  return ret;
366  distance++;
367  distance = FFMIN3(distance, dec->num_decoded, 1 << 20);
368  dec->copy_pos = dec->num_decoded - distance;
369  return decode_hybrid_varlen_uint(gb, dec, bundle, context, hybrid_uint);
370  }
371  ret = read_hybrid_uint(gb, &dist->config, token, hybrid_uint);
372  if (ret < 0)
373  return ret;
374  if (bundle->lz77_enabled)
375  dec->window[dec->num_decoded++ & 0xFFFFF] = *hybrid_uint;
376 
377  return 0;
378 }
379 
380 static int populate_distribution(GetBitContext *gb, JXLSymbolDistribution *dist, int log_alphabet_size)
381 {
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  dist->uniq_pos = -1;
388 
389  if (get_bits1(gb)) {
390  /* simple code */
391  dist->alphabet_size = 256;
392  if (get_bits1(gb)) {
393  uint8_t v1 = jxl_u8(gb);
394  uint8_t v2 = jxl_u8(gb);
395  if (v1 == v2)
396  return AVERROR_INVALIDDATA;
397  dist->freq[v1] = get_bits(gb, 12);
398  dist->freq[v2] = (1 << 12) - dist->freq[v1];
399  if (!dist->freq[v1])
400  dist->uniq_pos = v2;
401  } else {
402  uint8_t x = jxl_u8(gb);
403  dist->freq[x] = 1 << 12;
404  dist->uniq_pos = x;
405  }
406  return 0;
407  }
408 
409  if (get_bits1(gb)) {
410  /* flat code */
411  dist->alphabet_size = jxl_u8(gb) + 1;
412  for (int i = 0; i < dist->alphabet_size; i++)
413  dist->freq[i] = (1 << 12) / dist->alphabet_size;
414  for (int i = 0; i < (1 << 12) % dist->alphabet_size; i++)
415  dist->freq[i]++;
416  return 0;
417  }
418 
419  do {
420  if (!get_bits1(gb))
421  break;
422  } while (++len < 3);
423 
424  shift = (get_bitsz(gb, len) | (1 << len)) - 1;
425  if (shift > 13)
426  return AVERROR_INVALIDDATA;
427 
428  dist->alphabet_size = jxl_u8(gb) + 3;
429  for (int i = 0; i < dist->alphabet_size; i++) {
430  logcounts[i] = get_vlc2(gb, dist_prefix_table, 7, 1);
431  if (logcounts[i] == 13) {
432  int rle = jxl_u8(gb);
433  same[i] = rle + 5;
434  i += rle + 3;
435  continue;
436  }
437  if (logcounts[i] > omit_log) {
438  omit_log = logcounts[i];
439  omit_pos = i;
440  }
441  }
442  if (omit_pos < 0 || omit_pos + 1 < dist->alphabet_size && logcounts[omit_pos + 1] == 13)
443  return AVERROR_INVALIDDATA;
444 
445  for (int i = 0; i < dist->alphabet_size; i++) {
446  if (same[i]) {
447  num_same = same[i] - 1;
448  prev = i > 0 ? dist->freq[i - 1] : 0;
449  }
450  if (num_same) {
451  dist->freq[i] = prev;
452  num_same--;
453  } else {
454  if (i == omit_pos || !logcounts[i])
455  continue;
456  if (logcounts[i] == 1) {
457  dist->freq[i] = 1;
458  } else {
459  int bitcount = FFMIN(FFMAX(0, shift - ((12 - logcounts[i] + 1) >> 1)), logcounts[i] - 1);
460  dist->freq[i] = (1 << (logcounts[i] - 1)) + (get_bitsz(gb, bitcount) << (logcounts[i] - 1 - bitcount));
461  }
462  }
463  total_count += dist->freq[i];
464  }
465  dist->freq[omit_pos] = (1 << 12) - total_count;
466 
467  return 0;
468 }
469 
471 {
472  if (bundle->use_prefix_code && bundle->dists)
473  for (int i = 0; i < bundle->num_clusters; i++)
474  ff_vlc_free(&bundle->dists[i].vlc);
475  av_freep(&bundle->dists);
476  av_freep(&bundle->cluster_map);
477 }
478 
479 
481  JXLDistributionBundle *bundle, int num_dist, int disallow_lz77);
482 
484 {
485  int ret;
486 
487  bundle->cluster_map = av_malloc(bundle->num_dist);
488  if (!bundle->cluster_map)
489  return AVERROR(ENOMEM);
490 
491  if (bundle->num_dist == 1) {
492  bundle->cluster_map[0] = 0;
493  bundle->num_clusters = 1;
494  return 0;
495  }
496 
497  if (get_bits1(gb)) {
498  /* simple clustering */
499  uint32_t nbits = get_bits(gb, 2);
500  for (int i = 0; i < bundle->num_dist; i++)
501  bundle->cluster_map[i] = get_bitsz(gb, nbits);
502  } else {
503  /* complex clustering */
504  int use_mtf = get_bits1(gb);
505  JXLDistributionBundle nested = { 0 };
506  /* num_dist == 1 prevents this from recursing again */
507  ret = read_distribution_bundle(gb, dec, &nested, 1, bundle->num_dist <= 2);
508  if (ret < 0) {
509  dist_bundle_close(&nested);
510  return ret;
511  }
512  for (int i = 0; i < bundle->num_dist; i++) {
513  uint32_t clust;
514  ret = decode_hybrid_varlen_uint(gb, dec, &nested, 0, &clust);
515  if (ret < 0) {
516  dist_bundle_close(&nested);
517  return ret;
518  }
519  bundle->cluster_map[i] = clust;
520  }
521  dec->state = -1;
522  /* it's not going to necessarily be zero after reading */
523  dec->num_to_copy = 0;
524  dist_bundle_close(&nested);
525  if (use_mtf) {
526  uint8_t mtf[256];
527  for (int i = 0; i < 256; i++)
528  mtf[i] = i;
529  for (int i = 0; i < bundle->num_dist; i++) {
530  int index = bundle->cluster_map[i];
531  bundle->cluster_map[i] = mtf[index];
532  if (index) {
533  int value = mtf[index];
534  for (int j = index; j > 0; j--)
535  mtf[j] = mtf[j - 1];
536  mtf[0] = value;
537  }
538  }
539  }
540  }
541  for (int i = 0; i < bundle->num_dist; i++) {
542  if (bundle->cluster_map[i] >= bundle->num_clusters)
543  bundle->num_clusters = bundle->cluster_map[i] + 1;
544  }
545 
546  if (bundle->num_clusters > bundle->num_dist)
547  return AVERROR_INVALIDDATA;
548 
549  return 0;
550 }
551 
552 static int gen_alias_map(JXLEntropyDecoder *dec, JXLSymbolDistribution *dist, int log_alphabet_size)
553 {
554  uint32_t bucket_size, table_size;
555  uint8_t overfull[256], underfull[256];
556  int overfull_pos = 0, underfull_pos = 0;
557  dist->log_bucket_size = 12 - log_alphabet_size;
558  bucket_size = 1 << dist->log_bucket_size;
559  table_size = 1 << log_alphabet_size;
560 
561  if (dist->uniq_pos >= 0) {
562  for (int i = 0; i < table_size; i++) {
563  dist->symbols[i] = dist->uniq_pos;
564  dist->offsets[i] = bucket_size * i;
565  dist->cutoffs[i] = 0;
566  }
567  return 0;
568  }
569 
570  for (int i = 0; i < dist->alphabet_size; i++) {
571  dist->cutoffs[i] = dist->freq[i];
572  dist->symbols[i] = i;
573  if (dist->cutoffs[i] > bucket_size)
574  overfull[overfull_pos++] = i;
575  else if (dist->cutoffs[i] < bucket_size)
576  underfull[underfull_pos++] = i;
577  }
578 
579  for (int i = dist->alphabet_size; i < table_size; i++) {
580  dist->cutoffs[i] = 0;
581  underfull[underfull_pos++] = i;
582  }
583 
584  while (overfull_pos) {
585  int o, u, by;
586  /* this should be impossible */
587  if (!underfull_pos)
588  return AVERROR_INVALIDDATA;
589  u = underfull[--underfull_pos];
590  o = overfull[--overfull_pos];
591  by = bucket_size - dist->cutoffs[u];
592  dist->cutoffs[o] -= by;
593  dist->symbols[u] = o;
594  dist->offsets[u] = dist->cutoffs[o];
595  if (dist->cutoffs[o] < bucket_size)
596  underfull[underfull_pos++] = o;
597  else if (dist->cutoffs[o] > bucket_size)
598  overfull[overfull_pos++] = o;
599  }
600 
601  for (int i = 0; i < table_size; i++) {
602  if (dist->cutoffs[i] == bucket_size) {
603  dist->symbols[i] = i;
604  dist->offsets[i] = 0;
605  dist->cutoffs[i] = 0;
606  } else {
607  dist->offsets[i] -= dist->cutoffs[i];
608  }
609  }
610 
611  return 0;
612 }
613 
615 {
616  int nsym, tree_select, bits;
617 
618  int8_t lens[4];
619  int16_t symbols[4];
620 
621  nsym = 1 + get_bits(gb, 2);
622  for (int i = 0; i < nsym; i++)
623  symbols[i] = get_bitsz(gb, dist->log_alphabet_size);
624  if (nsym == 4)
625  tree_select = get_bits1(gb);
626  switch (nsym) {
627  case 1:
628  dist->vlc.bits = 0;
629  dist->default_symbol = symbols[0];
630  return 0;
631  case 2:
632  bits = 1;
633  lens[0] = 1, lens[1] = 1, lens[2] = 0, lens[3] = 0;
634  if (symbols[1] < symbols[0])
635  FFSWAP(int16_t, symbols[0], symbols[1]);
636  break;
637  case 3:
638  bits = 2;
639  lens[0] = 1, lens[1] = 2, lens[2] = 2, lens[3] = 0;
640  if (symbols[2] < symbols[1])
641  FFSWAP(int16_t, symbols[1], symbols[2]);
642  break;
643  case 4:
644  if (tree_select) {
645  bits = 3;
646  lens[0] = 1, lens[1] = 2, lens[2] = 3, lens[3] = 3;
647  if (symbols[3] < symbols[2])
648  FFSWAP(int16_t, symbols[2], symbols[3]);
649  } else {
650  bits = 2;
651  lens[0] = 2, lens[1] = 2, lens[2] = 2, lens[3] = 2;
652  while (1) {
653  if (symbols[1] < symbols[0])
654  FFSWAP(int16_t, symbols[0], symbols[1]);
655  if (symbols[3] < symbols[2])
656  FFSWAP(int16_t, symbols[2], symbols[3]);
657  if (symbols[1] <= symbols[2])
658  break;
659  FFSWAP(int16_t, symbols[1], symbols[2]);
660  }
661  }
662  break;
663  default:
664  // Challenge Complete! How did we get here?
665  return AVERROR_BUG;
666  }
667 
668  return ff_vlc_init_from_lengths(&dist->vlc, bits, nsym, lens, 1, symbols,
669  2, 2, 0, VLC_INIT_LE, dec->logctx);
670 }
671 
673 {
674  int8_t level1_lens[18] = { 0 };
675  int8_t level1_lens_s[18] = { 0 };
676  int16_t level1_syms[18] = { 0 };
677  uint32_t level1_codecounts[19] = { 0 };
678  uint8_t *buf = NULL;
679  int8_t *level2_lens, *level2_lens_s;
680  int16_t *level2_syms;
681  uint32_t *level2_codecounts;
682 
683  int repeat_count_prev = 0, repeat_count_zero = 0, prev = 8;
684  int total_code = 0, len, hskip, num_codes = 0, ret;
685 
686  VLC level1_vlc = { 0 };
687 
688  if (dist->alphabet_size == 1) {
689  dist->vlc.bits = 0;
690  dist->default_symbol = 0;
691  return 0;
692  }
693 
694  hskip = get_bits(gb, 2);
695  if (hskip == 1)
696  return read_simple_vlc_prefix(gb, dec, dist);
697 
698  level1_codecounts[0] = hskip;
699  for (int i = hskip; i < 18; i++) {
700  len = level1_lens[prefix_codelen_map[i]] = get_vlc2(gb, level0_table, 4, 1);
701  level1_codecounts[len]++;
702  if (len) {
703  total_code += (32 >> len);
704  num_codes++;
705  }
706  if (total_code >= 32) {
707  level1_codecounts[0] += 18 - i - 1;
708  break;
709  }
710  }
711 
712  if (total_code != 32 && num_codes >= 2 || num_codes < 1) {
714  goto end;
715  }
716 
717  for (int i = 1; i < 19; i++)
718  level1_codecounts[i] += level1_codecounts[i - 1];
719 
720  for (int i = 17; i >= 0; i--) {
721  int idx = --level1_codecounts[level1_lens[i]];
722  level1_lens_s[idx] = level1_lens[i];
723  level1_syms[idx] = i;
724  }
725 
726  ret = ff_vlc_init_from_lengths(&level1_vlc, 5, 18, level1_lens_s, 1, level1_syms, 2, 2,
727  0, VLC_INIT_LE, dec->logctx);
728  if (ret < 0)
729  goto end;
730 
731  buf = av_mallocz(MAX_PREFIX_ALPHABET_SIZE * (2 * sizeof(int8_t) + sizeof(int16_t) + sizeof(uint32_t))
732  + sizeof(uint32_t));
733  if (!buf) {
734  ret = AVERROR(ENOMEM);
735  goto end;
736  }
737 
738  level2_lens = (int8_t *)buf;
739  level2_lens_s = (int8_t *)(buf + MAX_PREFIX_ALPHABET_SIZE * sizeof(int8_t));
740  level2_syms = (int16_t *)(buf + MAX_PREFIX_ALPHABET_SIZE * (2 * sizeof(int8_t)));
741  level2_codecounts = (uint32_t *)(buf + MAX_PREFIX_ALPHABET_SIZE * (2 * sizeof(int8_t) + sizeof(int16_t)));
742 
743  total_code = 0;
744  for (int i = 0; i < dist->alphabet_size; i++) {
745  len = get_vlc2(gb, level1_vlc.table, 5, 1);
746  if (get_bits_left(gb) < 0) {
748  goto end;
749  }
750  if (len == 16) {
751  int extra = 3 + get_bits(gb, 2);
752  if (repeat_count_prev)
753  extra += 4 * (repeat_count_prev - 2) - repeat_count_prev;
754  extra = FFMIN(extra, dist->alphabet_size - i);
755  for (int j = 0; j < extra; j++)
756  level2_lens[i + j] = prev;
757  total_code += (32768 >> prev) * extra;
758  i += extra - 1;
759  repeat_count_prev += extra;
760  repeat_count_zero = 0;
761  level2_codecounts[prev] += extra;
762  } else if (len == 17) {
763  int extra = 3 + get_bits(gb, 3);
764  if (repeat_count_zero > 0)
765  extra += 8 * (repeat_count_zero - 2) - repeat_count_zero;
766  extra = FFMIN(extra, dist->alphabet_size - i);
767  i += extra - 1;
768  repeat_count_prev = 0;
769  repeat_count_zero += extra;
770  level2_codecounts[0] += extra;
771  } else {
772  level2_lens[i] = len;
773  repeat_count_prev = repeat_count_zero = 0;
774  if (len) {
775  total_code += (32768 >> len);
776  prev = len;
777  }
778  level2_codecounts[len]++;
779  }
780  if (total_code >= 32768) {
781  level2_codecounts[0] += dist->alphabet_size - i - 1;
782  break;
783  }
784  }
785 
786  if (total_code != 32768 && level2_codecounts[0] < dist->alphabet_size - 1) {
788  goto end;
789  }
790 
791  for (int i = 1; i < dist->alphabet_size + 1; i++)
792  level2_codecounts[i] += level2_codecounts[i - 1];
793 
794  for (int i = dist->alphabet_size - 1; i >= 0; i--) {
795  int idx = --level2_codecounts[level2_lens[i]];
796  level2_lens_s[idx] = level2_lens[i];
797  level2_syms[idx] = i;
798  }
799 
800  ret = ff_vlc_init_from_lengths(&dist->vlc, 15, dist->alphabet_size, level2_lens_s,
801  1, level2_syms, 2, 2, 0, VLC_INIT_LE, dec->logctx);
802 
803 end:
804  av_freep(&buf);
805  ff_vlc_free(&level1_vlc);
806 
807  return ret;
808 }
809 
811  JXLDistributionBundle *bundle, int num_dist, int disallow_lz77)
812 {
813  int ret;
814 
815  if (num_dist <= 0)
816  return AVERROR(EINVAL);
817 
818  bundle->num_dist = num_dist;
819  bundle->lz77_enabled = get_bits1(gb);
820  if (bundle->lz77_enabled) {
821  if (disallow_lz77)
822  return AVERROR_INVALIDDATA;
823  bundle->lz77_min_symbol = jxl_u32(gb, 224, 512, 4096, 8, 0, 0, 0, 15);
824  bundle->lz77_min_length = jxl_u32(gb, 3, 4, 5, 9, 0, 0, 2, 8);
825  bundle->num_dist++;
826  ret = read_hybrid_uint_conf(gb, &bundle->lz_len_conf, 8);
827  if (ret < 0)
828  return ret;
829  }
830 
831  if (bundle->lz77_enabled && !dec->window) {
832  dec->window = av_malloc_array(1 << 20, sizeof(uint32_t));
833  if (!dec->window)
834  return AVERROR(ENOMEM);
835  }
836 
837  ret = read_dist_clustering(gb, dec, bundle);
838  if (ret < 0)
839  return ret;
840  if (get_bits_left(gb) < 0)
842 
843  bundle->dists = av_calloc(bundle->num_clusters, sizeof(JXLSymbolDistribution));
844  if (!bundle->dists)
845  return AVERROR(ENOMEM);
846 
847  bundle->use_prefix_code = get_bits1(gb);
848  bundle->log_alphabet_size = bundle->use_prefix_code ? 15 : 5 + get_bits(gb, 2);
849 
850  for (int i = 0; i < bundle->num_clusters; i++) {
851  ret = read_hybrid_uint_conf(gb, &bundle->dists[i].config, bundle->log_alphabet_size);
852  if (ret < 0)
853  return ret;
854  if (get_bits_left(gb) < 0)
856  }
857 
858  if (bundle->use_prefix_code) {
859  for (int i = 0; i < bundle->num_clusters; i++) {
860  JXLSymbolDistribution *dist = &bundle->dists[i];
861  if (get_bits1(gb)) {
862  int n = get_bits(gb, 4);
863  dist->alphabet_size = 1 + (1 << n) + get_bitsz(gb, n);
865  return AVERROR_INVALIDDATA;
866  } else {
867  dist->alphabet_size = 1;
868  }
869  dist->log_alphabet_size = clog1p(dist->alphabet_size - 1);
870  }
871  for (int i = 0; i < bundle->num_clusters; i++) {
872  ret = read_vlc_prefix(gb, dec, &bundle->dists[i]);
873  if (ret < 0)
874  return ret;
875  if (get_bits_left(gb) < 0)
877  }
878  } else {
879  for (int i = 0; i < bundle->num_clusters; i++) {
880  ret = populate_distribution(gb, &bundle->dists[i], bundle->log_alphabet_size);
881  if (ret < 0)
882  return ret;
883  if (get_bits_left(gb) < 0)
885  }
886  for (int i = 0; i < bundle->num_clusters; i++) {
887  ret = gen_alias_map(dec, &bundle->dists[i], bundle->log_alphabet_size);
888  if (ret < 0)
889  return ret;
890  }
891  }
892 
893  return 0;
894 }
895 
897 {
898  if (!dec)
899  return;
900  av_freep(&dec->window);
901  dist_bundle_close(&dec->bundle);
902 }
903 
904 static int entropy_decoder_init(void *avctx, GetBitContext *gb, JXLEntropyDecoder *dec, int num_dist)
905 {
906  int ret;
907 
908  memset(dec, 0, sizeof(*dec));
909  dec->logctx = avctx;
910  dec->state = -1;
911 
912  ret = read_distribution_bundle(gb, dec, &dec->bundle, num_dist, 0);
913  if (ret < 0) {
915  return ret;
916  }
917 
918  return 0;
919 }
920 
922 {
923  int ret;
924  uint32_t hybrid_uint;
925 
926  ret = decode_hybrid_varlen_uint(gb, dec, &dec->bundle, context, &hybrid_uint);
927  if (ret < 0)
928  return ret;
929 
930  return hybrid_uint;
931 }
932 
933 static inline uint32_t icc_context(uint64_t i, uint32_t b1, uint32_t b2)
934 {
935  uint32_t p1, p2;
936  if (i <= 128)
937  return 0;
938  if (b1 >= 'a' && b1 <= 'z' || b1 >= 'A' && b1 <= 'Z')
939  p1 = 0;
940  else if (b1 >= '0' && b1 <= '9' || b1 == '.' || b1 == ',')
941  p1 = 1;
942  else if (b1 <= 1)
943  p1 = b1 + 2;
944  else if (b1 > 1 && b1 < 16)
945  p1 = 4;
946  else if (b1 > 240 && b1 < 255)
947  p1 = 5;
948  else if (b1 == 255)
949  p1 = 6;
950  else
951  p1 = 7;
952 
953  if (b2 >= 'a' && b2 <= 'z' || b2 >= 'A' && b2 <= 'Z')
954  p2 = 0;
955  else if (b2 >= '0' && b2 <= '9' || b2 == '.' || b2 == ',')
956  p2 = 1;
957  else if (b2 < 16)
958  p2 = 2;
959  else if (b2 > 240)
960  p2 = 3;
961  else
962  p2 = 4;
963 
964  return 1 + p1 + p2 * 8;
965 }
966 
967 static inline uint32_t toc_context(uint32_t x)
968 {
969  return FFMIN(7, clog1p(x));
970 }
971 
973 {
974  s->width = meta->width;
975  s->height = meta->height;
976 
977  switch (meta->csp) {
978  case JPEGXL_CS_RGB:
979  case JPEGXL_CS_XYB:
980  avctx->colorspace = AVCOL_SPC_RGB;
981  break;
982  default:
984  }
985 
986  if (meta->wp == JPEGXL_WP_D65) {
987  switch (meta->primaries) {
988  case JPEGXL_PR_SRGB:
990  break;
991  case JPEGXL_PR_P3:
993  break;
994  case JPEGXL_PR_2100:
996  break;
997  default:
999  }
1000  } else if (meta->wp == JPEGXL_WP_DCI && meta->primaries == JPEGXL_PR_P3) {
1002  } else {
1004  }
1005 
1006  if (meta->trc > JPEGXL_TR_GAMMA) {
1008  switch (trc) {
1009  case JPEGXL_TR_BT709:
1010  avctx->color_trc = AVCOL_TRC_BT709;
1011  break;
1012  case JPEGXL_TR_LINEAR:
1013  avctx->color_trc = AVCOL_TRC_LINEAR;
1014  break;
1015  case JPEGXL_TR_SRGB:
1017  break;
1018  case JPEGXL_TR_PQ:
1020  break;
1021  case JPEGXL_TR_DCI:
1022  avctx->color_trc = AVCOL_TRC_SMPTE428;
1023  break;
1024  case JPEGXL_TR_HLG:
1026  break;
1027  default:
1029  }
1030  } else if (meta->trc > 0) {
1031  if (meta->trc > 45355 && meta->trc < 45555)
1032  avctx->color_trc = AVCOL_TRC_GAMMA22;
1033  else if (meta->trc > 35614 && meta->trc < 35814)
1034  avctx->color_trc = AVCOL_TRC_GAMMA28;
1035  else
1037  } else {
1039  }
1040 
1041  if (meta->csp == JPEGXL_CS_GRAY) {
1042  if (meta->bit_depth <= 8)
1043  s->format = meta->have_alpha ? AV_PIX_FMT_YA8 : AV_PIX_FMT_GRAY8;
1044  else if (meta->bit_depth <= 16)
1045  s->format = meta->have_alpha ? AV_PIX_FMT_YA16 : AV_PIX_FMT_GRAY16;
1046  else
1047  s->format = meta->have_alpha ? AV_PIX_FMT_NONE : AV_PIX_FMT_GRAYF32;
1048  } else {
1049  if (meta->bit_depth <= 8)
1050  s->format = meta->have_alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;
1051  else if (meta->bit_depth <= 16)
1052  s->format = meta->have_alpha ? AV_PIX_FMT_RGBA64 : AV_PIX_FMT_RGB48;
1053  else
1054  s->format = meta->have_alpha ? AV_PIX_FMT_RGBAF32 : AV_PIX_FMT_RGBF32;
1055  }
1056 }
1057 
1058 static int skip_icc_profile(void *avctx, JXLParseContext *ctx, GetBitContext *gb)
1059 {
1060  int64_t ret;
1061  uint32_t last = 0, last2 = 0;
1062  JXLEntropyDecoder dec = { 0 };
1063  uint64_t enc_size = jxl_u64(gb);
1064  uint64_t output_size = 0;
1065  int out_size_shift = 0;
1066 
1067  if (!enc_size || enc_size > (1 << 22))
1068  return AVERROR_INVALIDDATA;
1069 
1070  ret = entropy_decoder_init(avctx, gb, &dec, 41);
1071  if (ret < 0)
1072  goto end;
1073 
1074  if (get_bits_left(gb) < 0) {
1076  goto end;
1077  }
1078 
1079  for (uint64_t read = 0; read < enc_size; read++) {
1080  ret = entropy_decoder_read_symbol(gb, &dec, icc_context(read, last, last2));
1081  if (ret < 0)
1082  goto end;
1083  if (ret > 255) {
1085  goto end;
1086  }
1087  if (get_bits_left(gb) < 0) {
1089  goto end;
1090  }
1091  last2 = last;
1092  last = ret;
1093  if (out_size_shift < 63) {
1094  output_size += (ret & UINT64_C(0x7F)) << out_size_shift;
1095  if (!(ret & 0x80)) {
1096  out_size_shift = 63;
1097  } else {
1098  out_size_shift += 7;
1099  if (out_size_shift > 56) {
1101  goto end;
1102  }
1103  }
1104  } else if (output_size < 132) {
1106  goto end;
1107  }
1108  }
1109 
1110  ret = 0;
1111 
1112 end:
1113  entropy_decoder_close(&dec);
1114 
1115  return ret;
1116 }
1117 
1119 {
1120  uint64_t extensions = jxl_u64(gb), extensions_len = 0;
1121 
1122  if (get_bits_left(gb) < 0)
1123  return AVERROR_BUFFER_TOO_SMALL;
1124 
1125  if (!extensions)
1126  return 0;
1127 
1128  for (int i = 0; i < 64; i++) {
1129  if (extensions & (UINT64_C(1) << i))
1130  extensions_len += jxl_u64(gb);
1131  if (get_bits_left(gb) < 0)
1132  return AVERROR_BUFFER_TOO_SMALL;
1133  }
1134 
1135  if (extensions_len > INT_MAX || get_bits_left(gb) < extensions_len)
1136  return AVERROR_BUFFER_TOO_SMALL;
1137 
1138  skip_bits_long(gb, extensions_len);
1139 
1140  return 0;
1141 }
1142 
1143 static int parse_frame_header(void *avctx, JXLParseContext *ctx, GetBitContext *gb)
1144 {
1145  int all_default, do_yCbCr = 0, num_passes = 1, ret;
1146  int group_size_shift = 1, lf_level = 0, save_as_ref = 0;
1147  int have_crop = 0, full_frame = 1, resets_canvas = 1, upsampling = 1;
1148  JXLFrame *frame = &ctx->codestream.frame;
1149  const FFJXLMetadata *meta = &ctx->codestream.meta;
1150  int32_t x0 = 0, y0 = 0;
1151  uint32_t duration = 0, width = meta->coded_width, height = meta->coded_height;
1152  uint32_t name_len, num_groups, num_lf_groups, group_dim, lf_group_dim, toc_count;
1153  uint64_t flags = 0;
1154  int start_len = get_bits_count(gb);
1155 
1156  memset(frame, 0, sizeof(*frame));
1157  frame->is_last = 1;
1158 
1159  all_default = get_bits1(gb);
1160  if (!all_default) {
1161  frame->type = get_bits(gb, 2);
1162  frame->encoding = get_bits1(gb);
1163  flags = jxl_u64(gb);
1164  if (!meta->xyb_encoded)
1165  do_yCbCr = get_bits1(gb);
1166  if (!(flags & JXL_FLAG_USE_LF_FRAME)) {
1167  if (do_yCbCr)
1168  skip_bits(gb, 6); // jpeg upsampling
1169  upsampling = jxl_u32(gb, 1, 2, 4, 8, 0, 0, 0, 0);
1170  skip_bits_long(gb, 2 * meta->num_extra_channels);
1171  if (get_bits_left(gb) < 0)
1172  return AVERROR_BUFFER_TOO_SMALL;
1173  }
1174  if (frame->encoding == JPEGXL_ENC_MODULAR)
1175  group_size_shift = get_bits(gb, 2);
1176  else if (meta->xyb_encoded)
1177  skip_bits(gb, 6); // xqm and bqm scales
1178  if (frame->type != JPEGXL_FRAME_REFERENCE_ONLY) {
1179  num_passes = jxl_u32(gb, 1, 2, 3, 4, 0, 0, 0, 3);
1180  if (num_passes != 1) {
1181  int num_ds = jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 1);
1182  skip_bits(gb, 2 * (num_passes - 1)); // shift
1183  skip_bits(gb, 2 * num_ds); // downsample
1184  for (int i = 0; i < num_ds; i++)
1185  jxl_u32(gb, 0, 1, 2, 0, 0, 0, 0, 3);
1186  }
1187  }
1188  if (frame->type == JPEGXL_FRAME_LF)
1189  lf_level = 1 + get_bits(gb, 2);
1190  else
1191  have_crop = get_bits1(gb);
1192  if (have_crop) {
1193  if (frame->type != JPEGXL_FRAME_REFERENCE_ONLY) {
1194  uint32_t ux0 = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1195  uint32_t uy0 = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1196  x0 = unpack_signed(ux0);
1197  y0 = unpack_signed(uy0);
1198  }
1199  width = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1200  height = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
1201  full_frame = x0 <= 0 && y0 <= 0 && width + x0 >= meta->coded_width
1202  && height + y0 >= meta->coded_height;
1203  }
1204  if (get_bits_left(gb) < 0)
1205  return AVERROR_BUFFER_TOO_SMALL;
1207  for (int i = 0; i <= meta->num_extra_channels; i++) {
1208  int mode = jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 2);
1210  jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 2);
1212  || mode == JPEGXL_BM_MUL))
1213  skip_bits1(gb);
1214  if (!i)
1215  resets_canvas = mode == JPEGXL_BM_REPLACE && full_frame;
1216  if (!resets_canvas)
1217  skip_bits(gb, 2);
1218  if (get_bits_left(gb) < 0)
1219  return AVERROR_BUFFER_TOO_SMALL;
1220  }
1221  if (meta->animation_offset)
1222  duration = jxl_u32(gb, 0, 1, 0, 0, 0, 0, 8, 32);
1223  if (meta->have_timecodes)
1224  skip_bits_long(gb, 32);
1225  frame->is_last = get_bits1(gb);
1226  } else {
1227  frame->is_last = 0;
1228  }
1229  if (frame->type != JPEGXL_FRAME_LF && !frame->is_last)
1230  save_as_ref = get_bits(gb, 2);
1231  if (frame->type == JPEGXL_FRAME_REFERENCE_ONLY ||
1232  (resets_canvas && !frame->is_last && (!duration || save_as_ref)
1233  && frame->type != JPEGXL_FRAME_LF))
1234  skip_bits1(gb); // save before color transform
1235  name_len = 8 * jxl_u32(gb, 0, 0, 16, 48, 0, 4, 5, 10);
1236  if (get_bits_left(gb) < name_len)
1237  return AVERROR_BUFFER_TOO_SMALL;
1238  skip_bits_long(gb, name_len);
1239  }
1240 
1241  if (!all_default) {
1242  int restd = get_bits1(gb), gab = 1;
1243  if (!restd)
1244  gab = get_bits1(gb);
1245  if (gab && !restd && get_bits1(gb))
1246  // gab custom
1247  skip_bits_long(gb, 16 * 6);
1248  if (get_bits_left(gb) < 0)
1249  return AVERROR_BUFFER_TOO_SMALL;
1250  if (!restd) {
1251  int epf = get_bits(gb, 2);
1252  if (epf) {
1253  if (frame->encoding == JPEGXL_ENC_VARDCT && get_bits1(gb)) {
1254  skip_bits_long(gb, 16 * 8); // custom epf sharpness
1255  if (get_bits_left(gb) < 0)
1256  return AVERROR_BUFFER_TOO_SMALL;
1257  }
1258  if (get_bits1(gb)) {
1259  skip_bits_long(gb, 3 * 16 + 32); // custom epf weight
1260  if (get_bits_left(gb) < 0)
1261  return AVERROR_BUFFER_TOO_SMALL;
1262  }
1263  if (get_bits1(gb)) { // custom epf sigma
1264  if (frame->encoding == JPEGXL_ENC_VARDCT)
1265  skip_bits(gb, 16);
1266  skip_bits_long(gb, 16 * 3);
1267  if (get_bits_left(gb) < 0)
1268  return AVERROR_BUFFER_TOO_SMALL;
1269  }
1270  if (frame->encoding == JPEGXL_ENC_MODULAR)
1271  skip_bits(gb, 16);
1272  }
1273  ret = skip_extensions(gb);
1274  if (ret < 0)
1275  return ret;
1276  }
1277  ret = skip_extensions(gb);
1278  if (ret < 0)
1279  return ret;
1280  }
1281 
1282  width = div_ceil(div_ceil(width, upsampling), 1 << (3 * lf_level));
1283  height = div_ceil(div_ceil(height, upsampling), 1 << (3 * lf_level));
1284  group_dim = 128 << group_size_shift;
1285  lf_group_dim = group_dim << 3;
1286  num_groups = div_ceil(width, group_dim) * div_ceil(height, group_dim);
1287  num_lf_groups = div_ceil(width, lf_group_dim) * div_ceil(height, lf_group_dim);
1288  if (num_groups == 1 && num_passes == 1)
1289  toc_count = 1;
1290  else
1291  toc_count = 2 + num_lf_groups + num_groups * num_passes;
1292 
1293  // permuted toc
1294  if (get_bits1(gb)) {
1295  JXLEntropyDecoder dec;
1296  uint32_t end, lehmer = 0;
1297  ret = entropy_decoder_init(avctx, gb, &dec, 8);
1298  if (ret < 0)
1299  return ret;
1300  if (get_bits_left(gb) < 0) {
1301  entropy_decoder_close(&dec);
1302  return AVERROR_BUFFER_TOO_SMALL;
1303  }
1304  end = entropy_decoder_read_symbol(gb, &dec, toc_context(toc_count));
1305  if (end > toc_count) {
1306  entropy_decoder_close(&dec);
1307  return AVERROR_INVALIDDATA;
1308  }
1309  for (uint32_t i = 0; i < end; i++) {
1310  lehmer = entropy_decoder_read_symbol(gb, &dec, toc_context(lehmer));
1311  if (get_bits_left(gb) < 0) {
1312  entropy_decoder_close(&dec);
1313  return AVERROR_BUFFER_TOO_SMALL;
1314  }
1315  }
1316  entropy_decoder_close(&dec);
1317  }
1318  align_get_bits(gb);
1319 
1320  for (uint32_t i = 0; i < toc_count; i++) {
1321  frame->body_length += 8 * jxl_u32(gb, 0, 1024, 17408, 4211712, 10, 14, 22, 30);
1322  if (get_bits_left(gb) < 0)
1323  return AVERROR_BUFFER_TOO_SMALL;
1324  }
1325  align_get_bits(gb);
1326 
1327  frame->total_length = frame->body_length + get_bits_count(gb) - start_len;
1328 
1329  return 0;
1330 }
1331 
1332 static int skip_boxes(JXLParseContext *ctx, const uint8_t *buf, int buf_size)
1333 {
1334  GetByteContext gb;
1335 
1336  if (ctx->skip > buf_size)
1337  return AVERROR_BUFFER_TOO_SMALL;
1338 
1339  buf += ctx->skip;
1340  buf_size -= ctx->skip;
1341  bytestream2_init(&gb, buf, buf_size);
1342 
1343  while (1) {
1344  uint64_t size;
1345  int head_size = 4;
1346 
1347  if (bytestream2_peek_le16(&gb) == FF_JPEGXL_CODESTREAM_SIGNATURE_LE)
1348  break;
1349  if (bytestream2_peek_le64(&gb) == FF_JPEGXL_CONTAINER_SIGNATURE_LE)
1350  break;
1351 
1352  if (bytestream2_get_bytes_left(&gb) < 8)
1353  return AVERROR_BUFFER_TOO_SMALL;
1354 
1355  size = bytestream2_get_be32(&gb);
1356  if (size == 1) {
1357  if (bytestream2_get_bytes_left(&gb) < 12)
1358  return AVERROR_BUFFER_TOO_SMALL;
1359  size = bytestream2_get_be64(&gb);
1360  head_size = 12;
1361  }
1362  if (!size)
1363  return AVERROR_INVALIDDATA;
1364  /* invalid ISOBMFF size */
1365  if (size <= head_size + 4 || size > INT_MAX - ctx->skip)
1366  return AVERROR_INVALIDDATA;
1367 
1368  ctx->skip += size;
1369  bytestream2_skip(&gb, size - head_size);
1370  if (bytestream2_get_bytes_left(&gb) <= 0)
1371  return AVERROR_BUFFER_TOO_SMALL;
1372  }
1373 
1374  return 0;
1375 }
1376 
1378  const uint8_t *buf, int buf_size)
1379 {
1380  int ret, cs_buflen, header_skip;
1381  const uint8_t *cs_buffer;
1382  GetBitContext gb;
1383 
1384  if (ctx->skip > buf_size)
1385  return AVERROR_BUFFER_TOO_SMALL;
1386 
1387  buf += ctx->skip;
1388  buf_size -= ctx->skip;
1389 
1390  if (ctx->container || AV_RL64(buf) == FF_JPEGXL_CONTAINER_SIGNATURE_LE) {
1391  ctx->container = 1;
1392  ret = ff_jpegxl_collect_codestream_header(buf, buf_size, ctx->cs_buffer,
1393  sizeof(ctx->cs_buffer), &ctx->copied);
1394  if (ret < 0)
1395  return ret;
1396  ctx->collected_size = ret;
1397  if (!ctx->copied) {
1398  ctx->skip += ret;
1399  return AVERROR_BUFFER_TOO_SMALL;
1400  }
1401  cs_buffer = ctx->cs_buffer;
1402  cs_buflen = FFMIN(sizeof(ctx->cs_buffer), ctx->copied);
1403  } else {
1404  cs_buffer = buf;
1405  cs_buflen = buf_size;
1406  }
1407 
1408  if (!ctx->codestream_length) {
1409  header_skip = ff_jpegxl_parse_codestream_header(cs_buffer, cs_buflen, &ctx->codestream.meta, 0);
1410  if (header_skip < 0)
1411  return header_skip;
1412  ctx->codestream_length = header_skip;
1413  populate_fields(s, avctx, &ctx->codestream.meta);
1414  }
1415 
1416  if (ctx->container)
1417  return ctx->collected_size;
1418 
1419  ret = init_get_bits8(&gb, cs_buffer, cs_buflen);
1420  if (ret < 0)
1421  return ret;
1422 
1423  skip_bits_long(&gb, ctx->codestream_length);
1424 
1425  if (!ctx->skipped_icc && ctx->codestream.meta.have_icc_profile) {
1426  ret = skip_icc_profile(avctx, ctx, &gb);
1427  if (ret < 0)
1428  return ret;
1429  ctx->skipped_icc = 1;
1430  align_get_bits(&gb);
1431  ctx->codestream_length = get_bits_count(&gb);
1432  }
1433 
1434  if (get_bits_left(&gb) <= 0)
1435  return AVERROR_BUFFER_TOO_SMALL;
1436 
1437  while (1) {
1438  ret = parse_frame_header(avctx, ctx, &gb);
1439  if (ret < 0)
1440  return ret;
1441  ctx->codestream_length += ctx->codestream.frame.total_length;
1442  if (ctx->codestream.frame.is_last)
1443  return ctx->codestream_length / 8;
1444  if (get_bits_left(&gb) <= ctx->codestream.frame.body_length)
1445  return AVERROR_BUFFER_TOO_SMALL;
1446  skip_bits_long(&gb, ctx->codestream.frame.body_length);
1447  }
1448 }
1449 
1451  const uint8_t **poutbuf, int *poutbuf_size,
1452  const uint8_t *buf, int buf_size)
1453 {
1454  JXLParseContext *ctx = s->priv_data;
1455  int next = END_NOT_FOUND, ret;
1456 
1457  *poutbuf_size = 0;
1458  *poutbuf = NULL;
1459 
1460  if (!ctx->pc.index)
1461  goto flush;
1462 
1463  if ((!ctx->container || !ctx->codestream_length) && !ctx->next) {
1464  ret = try_parse(s, avctx, ctx, ctx->pc.buffer, ctx->pc.index);
1465  if (ret < 0)
1466  goto flush;
1467  ctx->next = ret;
1468  if (ctx->container)
1469  ctx->skip += ctx->next;
1470  }
1471 
1472  if (ctx->container && ctx->next >= 0) {
1473  ret = skip_boxes(ctx, ctx->pc.buffer, ctx->pc.index);
1474  if (ret < 0) {
1475  if (ret == AVERROR_INVALIDDATA)
1476  ctx->next = -1;
1477  goto flush;
1478  }
1479  ctx->next = ret + ctx->skip;
1480  }
1481 
1482  if (ctx->next >= 0)
1483  next = ctx->next - ctx->pc.index;
1484 
1485 flush:
1486  if (next > buf_size)
1487  next = END_NOT_FOUND;
1488 
1489  ret = ff_combine_frame(&ctx->pc, next, &buf, &buf_size);
1490  if (ret < 0)
1491  return buf_size;
1492 
1493  *poutbuf = buf;
1494  *poutbuf_size = buf_size;
1495 
1496  ctx->codestream_length = 0;
1497  ctx->collected_size = 0;
1498  ctx->container = 0;
1499  ctx->copied = 0;
1500  ctx->skip = 0;
1501  ctx->skipped_icc = 0;
1502  ctx->next = 0;
1503  memset(&ctx->codestream, 0, sizeof(ctx->codestream));
1504 
1505  return next;
1506 }
1507 
1510  .priv_data_size = sizeof(JXLParseContext),
1511  .parser_parse = jpegxl_parse,
1512  .parser_close = ff_parse_close,
1513 };
JXLParseContext::skip
int skip
Definition: jpegxl_parser.c:158
JPEGXL_BM_REPLACE
@ JPEGXL_BM_REPLACE
Definition: jpegxl.h:43
skip_extensions
static int skip_extensions(GetBitContext *gb)
Definition: jpegxl_parser.c:1118
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
ff_vlc_init_from_lengths
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: vlc.c:306
gen_alias_map
static int gen_alias_map(JXLEntropyDecoder *dec, JXLSymbolDistribution *dist, int log_alphabet_size)
Definition: jpegxl_parser.c:552
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_PIX_FMT_YA8
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:133
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1029
JXLHybridUintConf::lsb_in_token
uint32_t lsb_in_token
Definition: jpegxl_parser.c:59
JXLParseContext::container
int container
Definition: jpegxl_parser.c:157
ff_jpegxl_parse_codestream_header
int ff_jpegxl_parse_codestream_header(const uint8_t *buf, int buflen, FFJXLMetadata *meta, int validate)
Definition: jpegxl_parse.c:255
GetByteContext
Definition: bytestream.h:33
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:240
constants
static const struct @336 constants[]
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
FFJXLMetadata::coded_width
uint32_t coded_width
Definition: jpegxl_parse.h:34
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:579
JXLSymbolDistribution::symbols
uint16_t symbols[258]
Definition: jpegxl_parser.c:84
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
JXLDistributionBundle::lz77_min_length
uint32_t lz77_min_length
Definition: jpegxl_parser.c:96
JXLParseContext::codestream
JXLCodestream codestream
Definition: jpegxl_parser.c:154
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
JXLParseContext::cs_buffer
uint8_t cs_buffer[4096]
Definition: jpegxl_parser.c:165
populate_distribution
static int populate_distribution(GetBitContext *gb, JXLSymbolDistribution *dist, int log_alphabet_size)
Definition: jpegxl_parser.c:380
read_distribution_bundle
static int read_distribution_bundle(GetBitContext *gb, JXLEntropyDecoder *dec, JXLDistributionBundle *bundle, int num_dist, int disallow_lz77)
Definition: jpegxl_parser.c:810
JXLSymbolDistribution::cutoffs
uint16_t cutoffs[258]
Definition: jpegxl_parser.c:82
read_hybrid_uint
static int read_hybrid_uint(GetBitContext *gb, const JXLHybridUintConf *conf, uint32_t token, uint32_t *hybrid_uint)
Definition: jpegxl_parser.c:277
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1022
JPEGXL_BM_MULADD
@ JPEGXL_BM_MULADD
Definition: jpegxl.h:46
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:288
JXLFrame::is_last
int is_last
Definition: jpegxl_parser.c:140
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:573
jpegxl_parse
static int jpegxl_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: jpegxl_parser.c:1450
jxl_u64
static uint64_t jxl_u64(GetBitContext *gb)
Definition: jpegxl_parser.c:229
FFJXLMetadata::have_timecodes
int have_timecodes
Definition: jpegxl_parse.h:52
JXLEntropyDecoder::num_to_copy
uint32_t num_to_copy
Definition: jpegxl_parser.c:120
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:600
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
JPEGXL_CS_RGB
@ JPEGXL_CS_RGB
Definition: jpegxl.h:63
c1
static const uint64_t c1
Definition: murmur3.c:52
jxl_u8
static av_always_inline uint8_t jxl_u8(GetBitContext *gb)
Read a variable-length 8-bit integer.
Definition: jpegxl_parser.c:202
unpack_signed
#define unpack_signed(x)
Definition: jpegxl_parser.c:52
JPEGXL_PR_SRGB
@ JPEGXL_PR_SRGB
Definition: jpegxl.h:77
entropy_decoder_read_symbol
static int64_t entropy_decoder_read_symbol(GetBitContext *gb, JXLEntropyDecoder *dec, uint32_t context)
Definition: jpegxl_parser.c:921
rle
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:216
clog1p
#define clog1p(x)
Definition: jpegxl_parser.c:51
JPEGXL_FRAME_REGULAR
@ JPEGXL_FRAME_REGULAR
Definition: jpegxl.h:36
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
FFJXLMetadata::primaries
FFJXLPrimaries primaries
Definition: jpegxl_parse.h:46
FFJXLMetadata
Definition: jpegxl_parse.h:31
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
FFJXLFrameEncoding
FFJXLFrameEncoding
Definition: jpegxl.h:30
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
JPEGXL_WP_DCI
@ JPEGXL_WP_DCI
Definition: jpegxl.h:73
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
JXLFrame::body_length
uint32_t body_length
Definition: jpegxl_parser.c:144
FF_JPEGXL_CONTAINER_SIGNATURE_LE
#define FF_JPEGXL_CONTAINER_SIGNATURE_LE
Definition: jpegxl.h:26
ParseContext
Definition: parser.h:28
JXLDistributionBundle::cluster_map
uint8_t * cluster_map
Definition: jpegxl_parser.c:100
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:584
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2035
JXLFrame::encoding
FFJXLFrameEncoding encoding
Definition: jpegxl_parser.c:138
macros.h
JXLSymbolDistribution
Definition: jpegxl_parser.c:62
GetBitContext
Definition: get_bits.h:108
AVCOL_TRC_GAMMA28
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition: pixfmt.h:576
read_hybrid_uint_conf
static int read_hybrid_uint_conf(GetBitContext *gb, JXLHybridUintConf *conf, int log_alphabet_size)
Definition: jpegxl_parser.c:259
JPEGXL_TR_BT709
@ JPEGXL_TR_BT709
Definition: jpegxl.h:84
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:452
FFJXLMetadata::num_extra_channels
uint32_t num_extra_channels
Definition: jpegxl_parse.h:53
JXLDistributionBundle::lz77_enabled
int lz77_enabled
Definition: jpegxl_parser.c:94
JXLDistributionBundle::lz77_min_symbol
uint32_t lz77_min_symbol
Definition: jpegxl_parser.c:95
JXLDistributionBundle::num_dist
int num_dist
Definition: jpegxl_parser.c:102
JXLEntropyDecoder::num_decoded
uint32_t num_decoded
Definition: jpegxl_parser.c:122
AVCOL_TRC_GAMMA22
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:575
JXLCodestream::frame
JXLFrame frame
Definition: jpegxl_parser.c:149
JXLSymbolDistribution::uniq_pos
int uniq_pos
Definition: jpegxl_parser.c:89
JXLSymbolDistribution::vlc
VLC vlc
Definition: jpegxl_parser.c:71
JXLCodestream
Definition: jpegxl_parser.c:147
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1015
JXLFrame
Definition: jpegxl_parser.c:136
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
dist_bundle_close
static void dist_bundle_close(JXLDistributionBundle *bundle)
Definition: jpegxl_parser.c:470
JXLParseContext::collected_size
int collected_size
Definition: jpegxl_parser.c:160
codec_id.h
JPEGXL_CS_GRAY
@ JPEGXL_CS_GRAY
Definition: jpegxl.h:64
duration
int64_t duration
Definition: movenc.c:64
populate_fields
static void populate_fields(AVCodecParserContext *s, AVCodecContext *avctx, const FFJXLMetadata *meta)
Definition: jpegxl_parser.c:972
JXLParseContext::pc
ParseContext pc
Definition: jpegxl_parser.c:153
JXLCodestream::meta
FFJXLMetadata meta
Definition: jpegxl_parser.c:148
width
#define width
JPEGXL_ENC_VARDCT
@ JPEGXL_ENC_VARDCT
Definition: jpegxl.h:31
s
#define s(width, name)
Definition: cbs_vp9.c:198
VLC_INIT_LE
#define VLC_INIT_LE
Definition: vlc.h:186
JXLHybridUintConf::msb_in_token
uint32_t msb_in_token
Definition: jpegxl_parser.c:58
FFJXLMetadata::height
uint32_t height
Definition: jpegxl_parse.h:33
FFJXLFrameType
FFJXLFrameType
Definition: jpegxl.h:35
bits
uint8_t bits
Definition: vp3data.h:128
JPEGXL_FRAME_LF
@ JPEGXL_FRAME_LF
Definition: jpegxl.h:37
JXLFrame::type
FFJXLFrameType type
Definition: jpegxl_parser.c:137
div_ceil
#define div_ceil(x, y)
Definition: jpegxl_parser.c:53
FFJXLTransferCharacteristic
FFJXLTransferCharacteristic
Definition: jpegxl.h:83
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFJXLMetadata::coded_height
uint32_t coded_height
Definition: jpegxl_parse.h:35
get_bits.h
JXLParseContext::copied
int copied
Definition: jpegxl_parser.c:159
jpegxl.h
AV_PIX_FMT_RGBF32
#define AV_PIX_FMT_RGBF32
Definition: pixfmt.h:538
JXLParseContext::skipped_icc
int skipped_icc
Definition: jpegxl_parser.c:162
AV_PIX_FMT_GRAYF32
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:501
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:548
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
frame
static AVFrame * frame
Definition: demux_decode.c:54
JXLSymbolDistribution::freq
uint32_t freq[258]
Definition: jpegxl_parser.c:80
if
if(ret)
Definition: filter_design.txt:179
JXLDistributionBundle
Definition: jpegxl_parser.c:92
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
AV_PIX_FMT_RGBA64
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:458
JXLEntropyDecoder::bundle
JXLDistributionBundle bundle
Definition: jpegxl_parser.c:130
NULL
#define NULL
Definition: coverity.c:32
JXLSymbolDistribution::default_symbol
uint32_t default_symbol
Definition: jpegxl_parser.c:73
JPEGXL_FRAME_SKIP_PROGRESSIVE
@ JPEGXL_FRAME_SKIP_PROGRESSIVE
Definition: jpegxl.h:39
JPEGXL_TR_DCI
@ JPEGXL_TR_DCI
Definition: jpegxl.h:89
dist_prefix_table
static const VLCElem dist_prefix_table[128]
Definition: jpegxl_parser.c:175
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:547
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
JPEGXL_TR_LINEAR
@ JPEGXL_TR_LINEAR
Definition: jpegxl.h:86
JXLSymbolDistribution::config
JXLHybridUintConf config
Definition: jpegxl_parser.c:63
jxl_u32
static av_always_inline uint32_t jxl_u32(GetBitContext *gb, uint32_t c0, uint32_t c1, uint32_t c2, uint32_t c3, uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3)
Definition: jpegxl_parser.c:213
ff_jpegxl_parser
const AVCodecParser ff_jpegxl_parser
Definition: jpegxl_parser.c:1508
icc_context
static uint32_t icc_context(uint64_t i, uint32_t b1, uint32_t b2)
Definition: jpegxl_parser.c:933
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
JXLHybridUintConf::split_exponent
int split_exponent
Definition: jpegxl_parser.c:57
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
JXL_FLAG_USE_LF_FRAME
#define JXL_FLAG_USE_LF_FRAME
Definition: jpegxl_parser.c:46
JXLFrame::full_frame
int full_frame
Definition: jpegxl_parser.c:141
JXLEntropyDecoder::state
int64_t state
Definition: jpegxl_parser.c:117
index
int index
Definition: gxfenc.c:89
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
error.h
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:556
JPEGXL_BM_MUL
@ JPEGXL_BM_MUL
Definition: jpegxl.h:47
AVCOL_PRI_SMPTE431
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
Definition: pixfmt.h:559
JPEGXL_PR_2100
@ JPEGXL_PR_2100
Definition: jpegxl.h:79
JPEGXL_TR_HLG
@ JPEGXL_TR_HLG
Definition: jpegxl.h:90
read_ans_symbol
static uint32_t read_ans_symbol(GetBitContext *gb, JXLEntropyDecoder *dec, const JXLSymbolDistribution *dist)
Definition: jpegxl_parser.c:307
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
decode_hybrid_varlen_uint
static int decode_hybrid_varlen_uint(GetBitContext *gb, JXLEntropyDecoder *dec, const JXLDistributionBundle *bundle, uint32_t context, uint32_t *hybrid_uint)
Definition: jpegxl_parser.c:327
JXLParseContext::codestream_length
int codestream_length
Definition: jpegxl_parser.c:161
toc_context
static uint32_t toc_context(uint32_t x)
Definition: jpegxl_parser.c:967
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2934
JPEGXL_TR_SRGB
@ JPEGXL_TR_SRGB
Definition: jpegxl.h:87
shift
static int shift(int a, int b)
Definition: bonk.c:262
FFJXLMetadata::xyb_encoded
int xyb_encoded
Definition: jpegxl_parse.h:50
AVCOL_TRC_SMPTEST2084
@ AVCOL_TRC_SMPTEST2084
Definition: pixfmt.h:588
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:454
size
int size
Definition: twinvq_data.h:10344
VLCElem
Definition: vlc.h:32
JXLEntropyDecoder
Definition: jpegxl_parser.c:114
FFJXLMetadata::animation_offset
int animation_offset
Definition: jpegxl_parse.h:42
JXLDistributionBundle::use_prefix_code
int use_prefix_code
Definition: jpegxl_parser.c:109
split
static char * split(char *message, char delim)
Definition: af_channelmap.c:80
height
#define height
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:2036
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
FFJXLMetadata::csp
FFJXLColorSpace csp
Definition: jpegxl_parse.h:44
JXLSymbolDistribution::alphabet_size
int alphabet_size
Definition: jpegxl_parser.c:66
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:413
prefix_codelen_map
static const uint8_t prefix_codelen_map[18]
Definition: jpegxl_parser.c:194
FFJXLMetadata::width
uint32_t width
Definition: jpegxl_parse.h:32
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:203
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:572
skip_icc_profile
static int skip_icc_profile(void *avctx, JXLParseContext *ctx, GetBitContext *gb)
Definition: jpegxl_parser.c:1058
JXLDistributionBundle::lz_len_conf
JXLHybridUintConf lz_len_conf
Definition: jpegxl_parser.c:97
JXLFrame::total_length
uint32_t total_length
Definition: jpegxl_parser.c:143
JXLParseContext
Definition: jpegxl_parser.c:152
entropy_decoder_init
static int entropy_decoder_init(void *avctx, GetBitContext *gb, JXLEntropyDecoder *dec, int num_dist)
Definition: jpegxl_parser.c:904
skip_boxes
static int skip_boxes(JXLParseContext *ctx, const uint8_t *buf, int buf_size)
Definition: jpegxl_parser.c:1332
try_parse
static int try_parse(AVCodecParserContext *s, AVCodecContext *avctx, JXLParseContext *ctx, const uint8_t *buf, int buf_size)
Definition: jpegxl_parser.c:1377
AV_PIX_FMT_YA16
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:453
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
JXLSymbolDistribution::log_bucket_size
int log_bucket_size
Definition: jpegxl_parser.c:64
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
vlm
#define vlm(a, b)
Definition: jpegxl_parser.c:54
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
FF_JPEGXL_CODESTREAM_SIGNATURE_LE
#define FF_JPEGXL_CODESTREAM_SIGNATURE_LE
Definition: jpegxl.h:25
av_always_inline
#define av_always_inline
Definition: attributes.h:49
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AV_CODEC_ID_JPEGXL
@ AV_CODEC_ID_JPEGXL
Definition: codec_id.h:316
JXLHybridUintConf
Definition: jpegxl_parser.c:56
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
JPEGXL_FRAME_REFERENCE_ONLY
@ JPEGXL_FRAME_REFERENCE_ONLY
Definition: jpegxl.h:38
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
parser.h
len
int len
Definition: vorbis_enc_data.h:426
JXLDistributionBundle::dists
JXLSymbolDistribution * dists
Definition: jpegxl_parser.c:105
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:602
entropy_decoder_close
static void entropy_decoder_close(JXLEntropyDecoder *dec)
Definition: jpegxl_parser.c:896
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
JXLDistributionBundle::num_clusters
int num_clusters
Definition: jpegxl_parser.c:106
AVCodecParserContext
Definition: avcodec.h:2774
JPEGXL_WP_D65
@ JPEGXL_WP_D65
Definition: jpegxl.h:70
VLC::bits
int bits
Definition: vlc.h:37
FFJXLMetadata::wp
FFJXLWhitePoint wp
Definition: jpegxl_parse.h:45
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:577
ret
ret
Definition: filter_design.txt:187
pixfmt.h
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
JXLSymbolDistribution::log_alphabet_size
int log_alphabet_size
Definition: jpegxl_parser.c:68
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:561
pos
unsigned int pos
Definition: spdifenc.c:413
read_simple_vlc_prefix
static int read_simple_vlc_prefix(GetBitContext *gb, JXLEntropyDecoder *dec, JXLSymbolDistribution *dist)
Definition: jpegxl_parser.c:614
FFJXLMetadata::have_alpha
int have_alpha
Definition: jpegxl_parse.h:37
JPEGXL_PR_P3
@ JPEGXL_PR_P3
Definition: jpegxl.h:80
JPEGXL_TR_GAMMA
@ JPEGXL_TR_GAMMA
Definition: jpegxl.h:91
AVCodecContext
main external API structure.
Definition: avcodec.h:441
c2
static const uint64_t c2
Definition: murmur3.c:53
AVCOL_TRC_ARIB_STD_B67
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition: pixfmt.h:591
JXLDistributionBundle::log_alphabet_size
int log_alphabet_size
Definition: jpegxl_parser.c:111
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
VLC
Definition: vlc.h:36
JPEGXL_ENC_MODULAR
@ JPEGXL_ENC_MODULAR
Definition: jpegxl.h:32
parse_frame_header
static int parse_frame_header(void *avctx, JXLParseContext *ctx, GetBitContext *gb)
Definition: jpegxl_parser.c:1143
JXLSymbolDistribution::offsets
uint16_t offsets[258]
Definition: jpegxl_parser.c:86
JXLEntropyDecoder::window
uint32_t * window
Definition: jpegxl_parser.c:127
VLC::table
VLCElem * table
Definition: vlc.h:38
AV_PIX_FMT_RGBAF32
#define AV_PIX_FMT_RGBAF32
Definition: pixfmt.h:539
level0_table
static const VLCElem level0_table[16]
Definition: jpegxl_parser.c:169
JXLEntropyDecoder::logctx
void * logctx
Definition: jpegxl_parser.c:133
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:367
mem.h
JXLParseContext::next
int next
Definition: jpegxl_parser.c:163
get_bitsz
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:351
MAX_PREFIX_ALPHABET_SIZE
#define MAX_PREFIX_ALPHABET_SIZE
Definition: jpegxl_parser.c:49
JPEGXL_BM_BLEND
@ JPEGXL_BM_BLEND
Definition: jpegxl.h:45
AVCOL_PRI_SMPTE432
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition: pixfmt.h:560
JPEGXL_TR_PQ
@ JPEGXL_TR_PQ
Definition: jpegxl.h:88
read_dist_clustering
static int read_dist_clustering(GetBitContext *gb, JXLEntropyDecoder *dec, JXLDistributionBundle *bundle)
Definition: jpegxl_parser.c:483
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecParser
Definition: avcodec.h:2933
vlc.h
FFJXLMetadata::bit_depth
int bit_depth
Definition: jpegxl_parse.h:36
read_vlc_prefix
static int read_vlc_prefix(GetBitContext *gb, JXLEntropyDecoder *dec, JXLSymbolDistribution *dist)
Definition: jpegxl_parser.c:672
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:230
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
jpegxl_parse.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVCOL_TRC_SMPTE428
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
Definition: pixfmt.h:589
FFJXLMetadata::trc
FFJXLTransferCharacteristic trc
Definition: jpegxl_parse.h:47
JXLEntropyDecoder::copy_pos
uint32_t copy_pos
Definition: jpegxl_parser.c:121
JPEGXL_CS_XYB
@ JPEGXL_CS_XYB
Definition: jpegxl.h:65
ff_jpegxl_collect_codestream_header
int ff_jpegxl_collect_codestream_header(const uint8_t *input_buffer, int input_len, uint8_t *buffer, int buflen, int *copied)
Definition: jpegxl_parse.c:449
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231
read_prefix_symbol
static uint32_t read_prefix_symbol(GetBitContext *gb, const JXLSymbolDistribution *dist)
Definition: jpegxl_parser.c:299
intmath.h