jpegxl_parser.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. #include <errno.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/error.h"
  26. #include "libavutil/intmath.h"
  27. #include "libavutil/macros.h"
  28. #include "libavutil/mem.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "bytestream.h"
  31. #include "codec_id.h"
  32. #define UNCHECKED_BITSTREAM_READER 0
  33. #define BITSTREAM_READER_LE
  34. #include "get_bits.h"
  35. #include "jpegxl.h"
  36. #include "jpegxl_parse.h"
  37. #include "parser.h"
  38. #include "vlc.h"
  39. #define JXL_FLAG_NOISE 1
  40. #define JXL_FLAG_PATCHES 2
  41. #define JXL_FLAG_SPLINES 16
  42. #define JXL_FLAG_USE_LF_FRAME 32
  43. #define JXL_FLAG_SKIP_ADAPTIVE_LF_SMOOTH 128
  44. #define MAX_PREFIX_ALPHABET_SIZE (1u << 15)
  45. #define clog1p(x) (ff_log2(x) + !!(x))
  46. #define unpack_signed(x) (((x) & 1 ? -(x)-1 : (x))/2)
  47. #define div_ceil(x, y) (((x) - 1) / (y) + 1)
  48. #define vlm(a,b) {.sym = (a), .len = (b)}
  49. typedef struct JXLHybridUintConf {
  50. int split_exponent;
  51. uint32_t msb_in_token;
  52. uint32_t lsb_in_token;
  53. } JXLHybridUintConf;
  54. typedef struct JXLSymbolDistribution {
  55. JXLHybridUintConf config;
  56. int log_bucket_size;
  57. /* this is the actual size of the alphabet */
  58. int alphabet_size;
  59. /* ceil(log(alphabet_size)) */
  60. int log_alphabet_size;
  61. /* for prefix code distributions */
  62. VLC vlc;
  63. /* in case bits == 0 */
  64. uint32_t default_symbol;
  65. /*
  66. * each (1 << log_alphabet_size) length
  67. * with log_alphabet_size <= 8
  68. */
  69. /* frequencies associated with this Distribution */
  70. uint32_t freq[258];
  71. /* cutoffs for using the symbol table */
  72. uint16_t cutoffs[258];
  73. /* the symbol table for this distribution */
  74. uint16_t symbols[258];
  75. /* the offset for symbols */
  76. uint16_t offsets[258];
  77. /* if this distribution contains only one symbol this is its index */
  78. int uniq_pos;
  79. } JXLSymbolDistribution;
  80. typedef struct JXLDistributionBundle {
  81. /* lz77 flags */
  82. int lz77_enabled;
  83. uint32_t lz77_min_symbol;
  84. uint32_t lz77_min_length;
  85. JXLHybridUintConf lz_len_conf;
  86. /* one entry for each distribution */
  87. uint8_t *cluster_map;
  88. /* length of cluster_map */
  89. int num_dist;
  90. /* one for each cluster */
  91. JXLSymbolDistribution *dists;
  92. int num_clusters;
  93. /* whether to use brotli prefixes or ans */
  94. int use_prefix_code;
  95. /* bundle log alphabet size, dist ones may be smaller */
  96. int log_alphabet_size;
  97. } JXLDistributionBundle;
  98. typedef struct JXLEntropyDecoder {
  99. /* state is a positive 32-bit integer, or -1 if unset */
  100. int64_t state;
  101. /* lz77 values */
  102. uint32_t num_to_copy;
  103. uint32_t copy_pos;
  104. uint32_t num_decoded;
  105. /* length is (1 << 20) */
  106. /* if lz77 is enabled for this bundle */
  107. /* if lz77 is disabled it's NULL */
  108. uint32_t *window;
  109. /* primary bundle associated with this distribution */
  110. JXLDistributionBundle bundle;
  111. /* for av_log */
  112. void *logctx;
  113. } JXLEntropyDecoder;
  114. typedef struct JXLFrame {
  115. FFJXLFrameType type;
  116. FFJXLFrameEncoding encoding;
  117. int is_last;
  118. int full_frame;
  119. uint32_t total_length;
  120. uint32_t body_length;
  121. } JXLFrame;
  122. typedef struct JXLCodestream {
  123. FFJXLMetadata meta;
  124. JXLFrame frame;
  125. } JXLCodestream;
  126. typedef struct JXLParseContext {
  127. ParseContext pc;
  128. JXLCodestream codestream;
  129. /* using ISOBMFF-based container */
  130. int container;
  131. int skip;
  132. int copied;
  133. int collected_size;
  134. int codestream_length;
  135. int skipped_icc;
  136. int next;
  137. uint8_t cs_buffer[4096 + AV_INPUT_BUFFER_PADDING_SIZE];
  138. } JXLParseContext;
  139. /* used for reading brotli prefixes */
  140. static const VLCElem level0_table[16] = {
  141. vlm(0, 2), vlm(4, 2), vlm(3, 2), vlm(2, 3), vlm(0, 2), vlm(4, 2), vlm(3, 2), vlm(1, 4),
  142. vlm(0, 2), vlm(4, 2), vlm(3, 2), vlm(2, 3), vlm(0, 2), vlm(4, 2), vlm(3, 2), vlm(5, 4),
  143. };
  144. /* prefix table for populating ANS distribution */
  145. static const VLCElem dist_prefix_table[128] = {
  146. vlm(10, 3), vlm(12, 7), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  147. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  148. vlm(10, 3), vlm(0, 5), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  149. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  150. vlm(10, 3), vlm(11, 6), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  151. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  152. vlm(10, 3), vlm(0, 5), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  153. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  154. vlm(10, 3), vlm(13, 7), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  155. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  156. vlm(10, 3), vlm(0, 5), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  157. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  158. vlm(10, 3), vlm(11, 6), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  159. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  160. vlm(10, 3), vlm(0, 5), vlm(7, 3), vlm(3, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(5, 4),
  161. vlm(10, 3), vlm(4, 4), vlm(7, 3), vlm(1, 4), vlm(6, 3), vlm(8, 3), vlm(9, 3), vlm(2, 4),
  162. };
  163. static const uint8_t prefix_codelen_map[18] = {
  164. 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  165. };
  166. /**
  167. * Read a variable-length 8-bit integer.
  168. * Used when populating the ANS frequency tables.
  169. */
  170. static av_always_inline uint8_t jxl_u8(GetBitContext *gb)
  171. {
  172. int n;
  173. if (!get_bits1(gb))
  174. return 0;
  175. n = get_bits(gb, 3);
  176. return get_bitsz(gb, n) | (1 << n);
  177. }
  178. /* read a U32(c_i + u(u_i)) */
  179. static av_always_inline uint32_t jxl_u32(GetBitContext *gb,
  180. uint32_t c0, uint32_t c1, uint32_t c2, uint32_t c3,
  181. uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3)
  182. {
  183. const uint32_t constants[4] = {c0, c1, c2, c3};
  184. const uint32_t ubits [4] = {u0, u1, u2, u3};
  185. uint32_t ret, choice = get_bits(gb, 2);
  186. ret = constants[choice];
  187. if (ubits[choice])
  188. ret += get_bits_long(gb, ubits[choice]);
  189. return ret;
  190. }
  191. /* read a U64() */
  192. static uint64_t jxl_u64(GetBitContext *gb)
  193. {
  194. uint64_t shift = 12, ret;
  195. switch (get_bits(gb, 2)) {
  196. case 1:
  197. ret = 1 + get_bits(gb, 4);
  198. break;
  199. case 2:
  200. ret = 17 + get_bits(gb, 8);
  201. break;
  202. case 3:
  203. ret = get_bits(gb, 12);
  204. while (get_bits1(gb)) {
  205. if (shift < 60) {
  206. ret |= (uint64_t)get_bits(gb, 8) << shift;
  207. shift += 8;
  208. } else {
  209. ret |= (uint64_t)get_bits(gb, 4) << shift;
  210. break;
  211. }
  212. }
  213. break;
  214. default:
  215. ret = 0;
  216. }
  217. return ret;
  218. }
  219. static int read_hybrid_uint_conf(GetBitContext *gb, JXLHybridUintConf *conf, int log_alphabet_size)
  220. {
  221. conf->split_exponent = get_bitsz(gb, clog1p(log_alphabet_size));
  222. if (conf->split_exponent == log_alphabet_size) {
  223. conf->msb_in_token = conf->lsb_in_token = 0;
  224. return 0;
  225. }
  226. conf->msb_in_token = get_bitsz(gb, clog1p(conf->split_exponent));
  227. if (conf->msb_in_token > conf->split_exponent)
  228. return AVERROR_INVALIDDATA;
  229. conf->lsb_in_token = get_bitsz(gb, clog1p(conf->split_exponent - conf->msb_in_token));
  230. if (conf->msb_in_token + conf->lsb_in_token > conf->split_exponent)
  231. return AVERROR_INVALIDDATA;
  232. return 0;
  233. }
  234. static int read_hybrid_uint(GetBitContext *gb, const JXLHybridUintConf *conf, uint32_t token, uint32_t *hybrid_uint)
  235. {
  236. uint32_t n, low, split = 1 << conf->split_exponent;
  237. if (token < split) {
  238. *hybrid_uint = token;
  239. return 0;
  240. }
  241. n = conf->split_exponent - conf->lsb_in_token - conf->msb_in_token +
  242. ((token - split) >> (conf->msb_in_token + conf->lsb_in_token));
  243. if (n >= 32)
  244. return AVERROR_INVALIDDATA;
  245. low = token & ((1 << conf->lsb_in_token) - 1);
  246. token >>= conf->lsb_in_token;
  247. token &= (1 << conf->msb_in_token) - 1;
  248. token |= 1 << conf->msb_in_token;
  249. *hybrid_uint = (((token << n) | get_bits_long(gb, n)) << conf->lsb_in_token ) | low;
  250. return 0;
  251. }
  252. static inline uint32_t read_prefix_symbol(GetBitContext *gb, const JXLSymbolDistribution *dist)
  253. {
  254. if (!dist->vlc.bits)
  255. return dist->default_symbol;
  256. return get_vlc2(gb, dist->vlc.table, dist->vlc.bits, 1);
  257. }
  258. static uint32_t read_ans_symbol(GetBitContext *gb, JXLEntropyDecoder *dec, const JXLSymbolDistribution *dist)
  259. {
  260. uint32_t index, i, pos, symbol, offset;
  261. if (dec->state < 0)
  262. dec->state = get_bits_long(gb, 32);
  263. index = dec->state & 0xFFF;
  264. i = index >> dist->log_bucket_size;
  265. pos = index & ((1 << dist->log_bucket_size) - 1);
  266. symbol = pos >= dist->cutoffs[i] ? dist->symbols[i] : i;
  267. offset = pos >= dist->cutoffs[i] ? dist->offsets[i] + pos : pos;
  268. dec->state = dist->freq[symbol] * (dec->state >> 12) + offset;
  269. if (dec->state < (1 << 16))
  270. dec->state = (dec->state << 16) | get_bits(gb, 16);
  271. dec->state &= 0xFFFFFFFF;
  272. return symbol;
  273. }
  274. static int decode_hybrid_varlen_uint(GetBitContext *gb, JXLEntropyDecoder *dec,
  275. const JXLDistributionBundle *bundle,
  276. uint32_t context, uint32_t *hybrid_uint)
  277. {
  278. int ret;
  279. uint32_t token, distance;
  280. const JXLSymbolDistribution *dist;
  281. if (dec->num_to_copy > 0) {
  282. *hybrid_uint = dec->window[dec->copy_pos++ & 0xFFFFF];
  283. dec->num_to_copy--;
  284. dec->window[dec->num_decoded++ & 0xFFFFF] = *hybrid_uint;
  285. return 0;
  286. }
  287. if (context >= bundle->num_dist)
  288. return AVERROR(EINVAL);
  289. if (bundle->cluster_map[context] >= bundle->num_clusters)
  290. return AVERROR_INVALIDDATA;
  291. dist = &bundle->dists[bundle->cluster_map[context]];
  292. if (bundle->use_prefix_code)
  293. token = read_prefix_symbol(gb, dist);
  294. else
  295. token = read_ans_symbol(gb, dec, dist);
  296. if (bundle->lz77_enabled && token >= bundle->lz77_min_symbol) {
  297. const JXLSymbolDistribution *lz77dist = &bundle->dists[bundle->cluster_map[bundle->num_dist - 1]];
  298. if (!dec->num_decoded)
  299. return AVERROR_INVALIDDATA;
  300. ret = read_hybrid_uint(gb, &bundle->lz_len_conf, token - bundle->lz77_min_symbol, &dec->num_to_copy);
  301. if (ret < 0)
  302. return ret;
  303. dec->num_to_copy += bundle->lz77_min_length;
  304. if (bundle->use_prefix_code)
  305. token = read_prefix_symbol(gb, lz77dist);
  306. else
  307. token = read_ans_symbol(gb, dec, lz77dist);
  308. ret = read_hybrid_uint(gb, &lz77dist->config, token, &distance);
  309. if (ret < 0)
  310. return ret;
  311. distance++;
  312. distance = FFMIN3(distance, dec->num_decoded, 1 << 20);
  313. dec->copy_pos = dec->num_decoded - distance;
  314. return decode_hybrid_varlen_uint(gb, dec, bundle, context, hybrid_uint);
  315. }
  316. ret = read_hybrid_uint(gb, &dist->config, token, hybrid_uint);
  317. if (ret < 0)
  318. return ret;
  319. if (bundle->lz77_enabled)
  320. dec->window[dec->num_decoded++ & 0xFFFFF] = *hybrid_uint;
  321. return 0;
  322. }
  323. static int populate_distribution(GetBitContext *gb, JXLSymbolDistribution *dist, int log_alphabet_size)
  324. {
  325. int len = 0, shift, omit_log = -1, omit_pos = -1;
  326. int prev = 0, num_same = 0;
  327. uint32_t total_count = 0;
  328. uint8_t logcounts[258] = { 0 };
  329. uint8_t same[258] = { 0 };
  330. const int table_size = 1 << log_alphabet_size;
  331. dist->uniq_pos = -1;
  332. if (get_bits1(gb)) {
  333. /* simple code */
  334. if (get_bits1(gb)) {
  335. uint8_t v1 = jxl_u8(gb);
  336. uint8_t v2 = jxl_u8(gb);
  337. if (v1 == v2)
  338. return AVERROR_INVALIDDATA;
  339. dist->freq[v1] = get_bits(gb, 12);
  340. dist->freq[v2] = (1 << 12) - dist->freq[v1];
  341. if (!dist->freq[v1])
  342. dist->uniq_pos = v2;
  343. dist->alphabet_size = 1 + FFMAX(v1, v2);
  344. } else {
  345. uint8_t x = jxl_u8(gb);
  346. dist->freq[x] = 1 << 12;
  347. dist->uniq_pos = x;
  348. dist->alphabet_size = 1 + x;
  349. }
  350. if (dist->alphabet_size > table_size)
  351. return AVERROR_INVALIDDATA;
  352. return 0;
  353. }
  354. if (get_bits1(gb)) {
  355. /* flat code */
  356. dist->alphabet_size = jxl_u8(gb) + 1;
  357. if (dist->alphabet_size > table_size)
  358. return AVERROR_INVALIDDATA;
  359. for (int i = 0; i < dist->alphabet_size; i++)
  360. dist->freq[i] = (1 << 12) / dist->alphabet_size;
  361. for (int i = 0; i < (1 << 12) % dist->alphabet_size; i++)
  362. dist->freq[i]++;
  363. return 0;
  364. }
  365. do {
  366. if (!get_bits1(gb))
  367. break;
  368. } while (++len < 3);
  369. shift = (get_bitsz(gb, len) | (1 << len)) - 1;
  370. if (shift > 13)
  371. return AVERROR_INVALIDDATA;
  372. dist->alphabet_size = jxl_u8(gb) + 3;
  373. if (dist->alphabet_size > table_size)
  374. return AVERROR_INVALIDDATA;
  375. for (int i = 0; i < dist->alphabet_size; i++) {
  376. logcounts[i] = get_vlc2(gb, dist_prefix_table, 7, 1);
  377. if (logcounts[i] == 13) {
  378. int rle = jxl_u8(gb);
  379. same[i] = rle + 5;
  380. i += rle + 3;
  381. continue;
  382. }
  383. if (logcounts[i] > omit_log) {
  384. omit_log = logcounts[i];
  385. omit_pos = i;
  386. }
  387. }
  388. if (omit_pos < 0 || omit_pos + 1 < dist->alphabet_size && logcounts[omit_pos + 1] == 13)
  389. return AVERROR_INVALIDDATA;
  390. for (int i = 0; i < dist->alphabet_size; i++) {
  391. if (same[i]) {
  392. num_same = same[i] - 1;
  393. prev = i > 0 ? dist->freq[i - 1] : 0;
  394. }
  395. if (num_same) {
  396. dist->freq[i] = prev;
  397. num_same--;
  398. } else {
  399. if (i == omit_pos || !logcounts[i])
  400. continue;
  401. if (logcounts[i] == 1) {
  402. dist->freq[i] = 1;
  403. } else {
  404. int bitcount = FFMIN(FFMAX(0, shift - ((12 - logcounts[i] + 1) >> 1)), logcounts[i] - 1);
  405. dist->freq[i] = (1 << (logcounts[i] - 1)) + (get_bitsz(gb, bitcount) << (logcounts[i] - 1 - bitcount));
  406. }
  407. }
  408. total_count += dist->freq[i];
  409. }
  410. dist->freq[omit_pos] = (1 << 12) - total_count;
  411. return 0;
  412. }
  413. static void dist_bundle_close(JXLDistributionBundle *bundle)
  414. {
  415. if (bundle->use_prefix_code && bundle->dists)
  416. for (int i = 0; i < bundle->num_clusters; i++)
  417. ff_vlc_free(&bundle->dists[i].vlc);
  418. av_freep(&bundle->dists);
  419. av_freep(&bundle->cluster_map);
  420. }
  421. static int read_distribution_bundle(GetBitContext *gb, JXLEntropyDecoder *dec,
  422. JXLDistributionBundle *bundle, int num_dist, int disallow_lz77);
  423. static int read_dist_clustering(GetBitContext *gb, JXLEntropyDecoder *dec, JXLDistributionBundle *bundle)
  424. {
  425. int ret;
  426. bundle->cluster_map = av_malloc(bundle->num_dist);
  427. if (!bundle->cluster_map)
  428. return AVERROR(ENOMEM);
  429. if (bundle->num_dist == 1) {
  430. bundle->cluster_map[0] = 0;
  431. bundle->num_clusters = 1;
  432. return 0;
  433. }
  434. if (get_bits1(gb)) {
  435. /* simple clustering */
  436. uint32_t nbits = get_bits(gb, 2);
  437. for (int i = 0; i < bundle->num_dist; i++)
  438. bundle->cluster_map[i] = get_bitsz(gb, nbits);
  439. } else {
  440. /* complex clustering */
  441. int use_mtf = get_bits1(gb);
  442. JXLDistributionBundle nested = { 0 };
  443. /* num_dist == 1 prevents this from recursing again */
  444. ret = read_distribution_bundle(gb, dec, &nested, 1, bundle->num_dist <= 2);
  445. if (ret < 0) {
  446. dist_bundle_close(&nested);
  447. return ret;
  448. }
  449. for (int i = 0; i < bundle->num_dist; i++) {
  450. uint32_t clust;
  451. ret = decode_hybrid_varlen_uint(gb, dec, &nested, 0, &clust);
  452. if (ret < 0) {
  453. dist_bundle_close(&nested);
  454. return ret;
  455. }
  456. bundle->cluster_map[i] = clust;
  457. }
  458. dec->state = -1;
  459. /* it's not going to necessarily be zero after reading */
  460. dec->num_to_copy = 0;
  461. dec->num_decoded = 0;
  462. dist_bundle_close(&nested);
  463. if (use_mtf) {
  464. uint8_t mtf[256];
  465. for (int i = 0; i < 256; i++)
  466. mtf[i] = i;
  467. for (int i = 0; i < bundle->num_dist; i++) {
  468. int index = bundle->cluster_map[i];
  469. bundle->cluster_map[i] = mtf[index];
  470. if (index) {
  471. int value = mtf[index];
  472. for (int j = index; j > 0; j--)
  473. mtf[j] = mtf[j - 1];
  474. mtf[0] = value;
  475. }
  476. }
  477. }
  478. }
  479. for (int i = 0; i < bundle->num_dist; i++) {
  480. if (bundle->cluster_map[i] >= bundle->num_clusters)
  481. bundle->num_clusters = bundle->cluster_map[i] + 1;
  482. }
  483. if (bundle->num_clusters > bundle->num_dist)
  484. return AVERROR_INVALIDDATA;
  485. return 0;
  486. }
  487. static int gen_alias_map(JXLEntropyDecoder *dec, JXLSymbolDistribution *dist, int log_alphabet_size)
  488. {
  489. uint32_t bucket_size, table_size;
  490. uint8_t overfull[256], underfull[256];
  491. int overfull_pos = 0, underfull_pos = 0;
  492. dist->log_bucket_size = 12 - log_alphabet_size;
  493. bucket_size = 1 << dist->log_bucket_size;
  494. table_size = 1 << log_alphabet_size;
  495. if (dist->uniq_pos >= 0) {
  496. for (int i = 0; i < table_size; i++) {
  497. dist->symbols[i] = dist->uniq_pos;
  498. dist->offsets[i] = bucket_size * i;
  499. dist->cutoffs[i] = 0;
  500. }
  501. return 0;
  502. }
  503. for (int i = 0; i < dist->alphabet_size; i++) {
  504. dist->cutoffs[i] = dist->freq[i];
  505. dist->symbols[i] = i;
  506. if (dist->cutoffs[i] > bucket_size)
  507. overfull[overfull_pos++] = i;
  508. else if (dist->cutoffs[i] < bucket_size)
  509. underfull[underfull_pos++] = i;
  510. }
  511. for (int i = dist->alphabet_size; i < table_size; i++) {
  512. dist->cutoffs[i] = 0;
  513. underfull[underfull_pos++] = i;
  514. }
  515. while (overfull_pos) {
  516. int o, u, by;
  517. /* this should be impossible */
  518. if (!underfull_pos)
  519. return AVERROR_INVALIDDATA;
  520. u = underfull[--underfull_pos];
  521. o = overfull[--overfull_pos];
  522. by = bucket_size - dist->cutoffs[u];
  523. dist->cutoffs[o] -= by;
  524. dist->symbols[u] = o;
  525. dist->offsets[u] = dist->cutoffs[o];
  526. if (dist->cutoffs[o] < bucket_size)
  527. underfull[underfull_pos++] = o;
  528. else if (dist->cutoffs[o] > bucket_size)
  529. overfull[overfull_pos++] = o;
  530. }
  531. for (int i = 0; i < table_size; i++) {
  532. if (dist->cutoffs[i] == bucket_size) {
  533. dist->symbols[i] = i;
  534. dist->offsets[i] = 0;
  535. dist->cutoffs[i] = 0;
  536. } else {
  537. dist->offsets[i] -= dist->cutoffs[i];
  538. }
  539. }
  540. return 0;
  541. }
  542. static int read_simple_vlc_prefix(GetBitContext *gb, JXLEntropyDecoder *dec, JXLSymbolDistribution *dist)
  543. {
  544. int nsym, tree_select, bits;
  545. int8_t lens[4];
  546. int16_t symbols[4];
  547. nsym = 1 + get_bits(gb, 2);
  548. for (int i = 0; i < nsym; i++)
  549. symbols[i] = get_bitsz(gb, dist->log_alphabet_size);
  550. if (nsym == 4)
  551. tree_select = get_bits1(gb);
  552. switch (nsym) {
  553. case 1:
  554. dist->vlc.bits = 0;
  555. dist->default_symbol = symbols[0];
  556. return 0;
  557. case 2:
  558. bits = 1;
  559. lens[0] = 1, lens[1] = 1, lens[2] = 0, lens[3] = 0;
  560. if (symbols[1] < symbols[0])
  561. FFSWAP(int16_t, symbols[0], symbols[1]);
  562. break;
  563. case 3:
  564. bits = 2;
  565. lens[0] = 1, lens[1] = 2, lens[2] = 2, lens[3] = 0;
  566. if (symbols[2] < symbols[1])
  567. FFSWAP(int16_t, symbols[1], symbols[2]);
  568. break;
  569. case 4:
  570. if (tree_select) {
  571. bits = 3;
  572. lens[0] = 1, lens[1] = 2, lens[2] = 3, lens[3] = 3;
  573. if (symbols[3] < symbols[2])
  574. FFSWAP(int16_t, symbols[2], symbols[3]);
  575. } else {
  576. bits = 2;
  577. lens[0] = 2, lens[1] = 2, lens[2] = 2, lens[3] = 2;
  578. while (1) {
  579. if (symbols[1] < symbols[0])
  580. FFSWAP(int16_t, symbols[0], symbols[1]);
  581. if (symbols[3] < symbols[2])
  582. FFSWAP(int16_t, symbols[2], symbols[3]);
  583. if (symbols[1] <= symbols[2])
  584. break;
  585. FFSWAP(int16_t, symbols[1], symbols[2]);
  586. }
  587. }
  588. break;
  589. default:
  590. // Challenge Complete! How did we get here?
  591. return AVERROR_BUG;
  592. }
  593. return ff_vlc_init_from_lengths(&dist->vlc, bits, nsym, lens, 1, symbols,
  594. 2, 2, 0, VLC_INIT_LE, dec->logctx);
  595. }
  596. static int read_vlc_prefix(GetBitContext *gb, JXLEntropyDecoder *dec, JXLSymbolDistribution *dist)
  597. {
  598. int8_t level1_lens[18] = { 0 };
  599. int8_t level1_lens_s[18] = { 0 };
  600. int16_t level1_syms[18] = { 0 };
  601. uint32_t level1_codecounts[19] = { 0 };
  602. uint8_t *buf = NULL;
  603. int8_t *level2_lens, *level2_lens_s;
  604. int16_t *level2_syms;
  605. uint32_t *level2_codecounts;
  606. int repeat_count_prev = 0, repeat_count_zero = 0, prev = 8;
  607. int total_code = 0, len, hskip, num_codes = 0, ret;
  608. VLC level1_vlc = { 0 };
  609. if (dist->alphabet_size == 1) {
  610. dist->vlc.bits = 0;
  611. dist->default_symbol = 0;
  612. return 0;
  613. }
  614. hskip = get_bits(gb, 2);
  615. if (hskip == 1)
  616. return read_simple_vlc_prefix(gb, dec, dist);
  617. level1_codecounts[0] = hskip;
  618. for (int i = hskip; i < 18; i++) {
  619. len = level1_lens[prefix_codelen_map[i]] = get_vlc2(gb, level0_table, 4, 1);
  620. if (len < 0) {
  621. ret = AVERROR_INVALIDDATA;
  622. goto end;
  623. }
  624. level1_codecounts[len]++;
  625. if (len) {
  626. total_code += (32 >> len);
  627. num_codes++;
  628. }
  629. if (total_code >= 32) {
  630. level1_codecounts[0] += 18 - i - 1;
  631. break;
  632. }
  633. }
  634. if (total_code != 32 && num_codes >= 2 || num_codes < 1) {
  635. ret = AVERROR_INVALIDDATA;
  636. goto end;
  637. }
  638. for (int i = 1; i < 19; i++)
  639. level1_codecounts[i] += level1_codecounts[i - 1];
  640. for (int i = 17; i >= 0; i--) {
  641. int idx = --level1_codecounts[level1_lens[i]];
  642. level1_lens_s[idx] = level1_lens[i];
  643. level1_syms[idx] = i;
  644. }
  645. ret = ff_vlc_init_from_lengths(&level1_vlc, 5, 18, level1_lens_s, 1, level1_syms, 2, 2,
  646. 0, VLC_INIT_LE, dec->logctx);
  647. if (ret < 0)
  648. goto end;
  649. buf = av_mallocz(MAX_PREFIX_ALPHABET_SIZE * (2 * sizeof(int8_t) + sizeof(int16_t) + sizeof(uint32_t))
  650. + sizeof(uint32_t));
  651. if (!buf) {
  652. ret = AVERROR(ENOMEM);
  653. goto end;
  654. }
  655. level2_lens = (int8_t *)buf;
  656. level2_lens_s = (int8_t *)(buf + MAX_PREFIX_ALPHABET_SIZE * sizeof(int8_t));
  657. level2_syms = (int16_t *)(buf + MAX_PREFIX_ALPHABET_SIZE * (2 * sizeof(int8_t)));
  658. level2_codecounts = (uint32_t *)(buf + MAX_PREFIX_ALPHABET_SIZE * (2 * sizeof(int8_t) + sizeof(int16_t)));
  659. total_code = 0;
  660. for (int i = 0; i < dist->alphabet_size; i++) {
  661. len = get_vlc2(gb, level1_vlc.table, 5, 1);
  662. if (len < 0) {
  663. ret = AVERROR_INVALIDDATA;
  664. goto end;
  665. }
  666. if (get_bits_left(gb) < 0) {
  667. ret = AVERROR_BUFFER_TOO_SMALL;
  668. goto end;
  669. }
  670. if (len == 16) {
  671. int extra = 3 + get_bits(gb, 2);
  672. if (repeat_count_prev)
  673. extra += 4 * (repeat_count_prev - 2) - repeat_count_prev;
  674. extra = FFMIN(extra, dist->alphabet_size - i);
  675. for (int j = 0; j < extra; j++)
  676. level2_lens[i + j] = prev;
  677. total_code += (32768 >> prev) * extra;
  678. i += extra - 1;
  679. repeat_count_prev += extra;
  680. repeat_count_zero = 0;
  681. level2_codecounts[prev] += extra;
  682. } else if (len == 17) {
  683. int extra = 3 + get_bits(gb, 3);
  684. if (repeat_count_zero > 0)
  685. extra += 8 * (repeat_count_zero - 2) - repeat_count_zero;
  686. extra = FFMIN(extra, dist->alphabet_size - i);
  687. i += extra - 1;
  688. repeat_count_prev = 0;
  689. repeat_count_zero += extra;
  690. level2_codecounts[0] += extra;
  691. } else {
  692. level2_lens[i] = len;
  693. repeat_count_prev = repeat_count_zero = 0;
  694. if (len) {
  695. total_code += (32768 >> len);
  696. prev = len;
  697. }
  698. level2_codecounts[len]++;
  699. }
  700. if (total_code >= 32768) {
  701. level2_codecounts[0] += dist->alphabet_size - i - 1;
  702. break;
  703. }
  704. }
  705. if (total_code != 32768 && level2_codecounts[0] < dist->alphabet_size - 1) {
  706. ret = AVERROR_INVALIDDATA;
  707. goto end;
  708. }
  709. for (int i = 1; i < dist->alphabet_size + 1; i++)
  710. level2_codecounts[i] += level2_codecounts[i - 1];
  711. for (int i = dist->alphabet_size - 1; i >= 0; i--) {
  712. int idx = --level2_codecounts[level2_lens[i]];
  713. level2_lens_s[idx] = level2_lens[i];
  714. level2_syms[idx] = i;
  715. }
  716. ret = ff_vlc_init_from_lengths(&dist->vlc, 15, dist->alphabet_size, level2_lens_s,
  717. 1, level2_syms, 2, 2, 0, VLC_INIT_LE, dec->logctx);
  718. end:
  719. av_freep(&buf);
  720. ff_vlc_free(&level1_vlc);
  721. return ret;
  722. }
  723. static int read_distribution_bundle(GetBitContext *gb, JXLEntropyDecoder *dec,
  724. JXLDistributionBundle *bundle, int num_dist, int disallow_lz77)
  725. {
  726. int ret;
  727. if (num_dist <= 0)
  728. return AVERROR(EINVAL);
  729. bundle->num_dist = num_dist;
  730. bundle->lz77_enabled = get_bits1(gb);
  731. if (bundle->lz77_enabled) {
  732. if (disallow_lz77)
  733. return AVERROR_INVALIDDATA;
  734. bundle->lz77_min_symbol = jxl_u32(gb, 224, 512, 4096, 8, 0, 0, 0, 15);
  735. bundle->lz77_min_length = jxl_u32(gb, 3, 4, 5, 9, 0, 0, 2, 8);
  736. bundle->num_dist++;
  737. ret = read_hybrid_uint_conf(gb, &bundle->lz_len_conf, 8);
  738. if (ret < 0)
  739. return ret;
  740. }
  741. if (bundle->lz77_enabled && !dec->window) {
  742. dec->window = av_malloc_array(1 << 20, sizeof(uint32_t));
  743. if (!dec->window)
  744. return AVERROR(ENOMEM);
  745. }
  746. ret = read_dist_clustering(gb, dec, bundle);
  747. if (ret < 0)
  748. return ret;
  749. if (get_bits_left(gb) < 0)
  750. return AVERROR_BUFFER_TOO_SMALL;
  751. bundle->dists = av_calloc(bundle->num_clusters, sizeof(JXLSymbolDistribution));
  752. if (!bundle->dists)
  753. return AVERROR(ENOMEM);
  754. bundle->use_prefix_code = get_bits1(gb);
  755. bundle->log_alphabet_size = bundle->use_prefix_code ? 15 : 5 + get_bits(gb, 2);
  756. for (int i = 0; i < bundle->num_clusters; i++) {
  757. ret = read_hybrid_uint_conf(gb, &bundle->dists[i].config, bundle->log_alphabet_size);
  758. if (ret < 0)
  759. return ret;
  760. if (get_bits_left(gb) < 0)
  761. return AVERROR_BUFFER_TOO_SMALL;
  762. }
  763. if (bundle->use_prefix_code) {
  764. for (int i = 0; i < bundle->num_clusters; i++) {
  765. JXLSymbolDistribution *dist = &bundle->dists[i];
  766. if (get_bits1(gb)) {
  767. int n = get_bits(gb, 4);
  768. dist->alphabet_size = 1 + (1 << n) + get_bitsz(gb, n);
  769. if (dist->alphabet_size > MAX_PREFIX_ALPHABET_SIZE)
  770. return AVERROR_INVALIDDATA;
  771. } else {
  772. dist->alphabet_size = 1;
  773. }
  774. dist->log_alphabet_size = clog1p(dist->alphabet_size - 1);
  775. }
  776. for (int i = 0; i < bundle->num_clusters; i++) {
  777. ret = read_vlc_prefix(gb, dec, &bundle->dists[i]);
  778. if (ret < 0)
  779. return ret;
  780. if (get_bits_left(gb) < 0)
  781. return AVERROR_BUFFER_TOO_SMALL;
  782. }
  783. } else {
  784. for (int i = 0; i < bundle->num_clusters; i++) {
  785. ret = populate_distribution(gb, &bundle->dists[i], bundle->log_alphabet_size);
  786. if (ret < 0)
  787. return ret;
  788. if (get_bits_left(gb) < 0)
  789. return AVERROR_BUFFER_TOO_SMALL;
  790. }
  791. for (int i = 0; i < bundle->num_clusters; i++) {
  792. ret = gen_alias_map(dec, &bundle->dists[i], bundle->log_alphabet_size);
  793. if (ret < 0)
  794. return ret;
  795. }
  796. }
  797. return 0;
  798. }
  799. static void entropy_decoder_close(JXLEntropyDecoder *dec)
  800. {
  801. if (!dec)
  802. return;
  803. av_freep(&dec->window);
  804. dist_bundle_close(&dec->bundle);
  805. }
  806. static int entropy_decoder_init(void *avctx, GetBitContext *gb, JXLEntropyDecoder *dec, int num_dist)
  807. {
  808. int ret;
  809. memset(dec, 0, sizeof(*dec));
  810. dec->logctx = avctx;
  811. dec->state = -1;
  812. ret = read_distribution_bundle(gb, dec, &dec->bundle, num_dist, 0);
  813. if (ret < 0) {
  814. entropy_decoder_close(dec);
  815. return ret;
  816. }
  817. return 0;
  818. }
  819. static int64_t entropy_decoder_read_symbol(GetBitContext *gb, JXLEntropyDecoder *dec, uint32_t context)
  820. {
  821. int ret;
  822. uint32_t hybrid_uint;
  823. ret = decode_hybrid_varlen_uint(gb, dec, &dec->bundle, context, &hybrid_uint);
  824. if (ret < 0)
  825. return ret;
  826. return hybrid_uint;
  827. }
  828. static inline uint32_t icc_context(uint64_t i, uint32_t b1, uint32_t b2)
  829. {
  830. uint32_t p1, p2;
  831. if (i <= 128)
  832. return 0;
  833. if (b1 >= 'a' && b1 <= 'z' || b1 >= 'A' && b1 <= 'Z')
  834. p1 = 0;
  835. else if (b1 >= '0' && b1 <= '9' || b1 == '.' || b1 == ',')
  836. p1 = 1;
  837. else if (b1 <= 1)
  838. p1 = b1 + 2;
  839. else if (b1 > 1 && b1 < 16)
  840. p1 = 4;
  841. else if (b1 > 240 && b1 < 255)
  842. p1 = 5;
  843. else if (b1 == 255)
  844. p1 = 6;
  845. else
  846. p1 = 7;
  847. if (b2 >= 'a' && b2 <= 'z' || b2 >= 'A' && b2 <= 'Z')
  848. p2 = 0;
  849. else if (b2 >= '0' && b2 <= '9' || b2 == '.' || b2 == ',')
  850. p2 = 1;
  851. else if (b2 < 16)
  852. p2 = 2;
  853. else if (b2 > 240)
  854. p2 = 3;
  855. else
  856. p2 = 4;
  857. return 1 + p1 + p2 * 8;
  858. }
  859. static inline uint32_t toc_context(uint32_t x)
  860. {
  861. return FFMIN(7, clog1p(x));
  862. }
  863. static void populate_fields(AVCodecParserContext *s, AVCodecContext *avctx, const FFJXLMetadata *meta)
  864. {
  865. s->width = meta->width;
  866. s->height = meta->height;
  867. switch (meta->csp) {
  868. case JPEGXL_CS_RGB:
  869. case JPEGXL_CS_XYB:
  870. avctx->colorspace = AVCOL_SPC_RGB;
  871. break;
  872. default:
  873. avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
  874. }
  875. if (meta->wp == JPEGXL_WP_D65) {
  876. switch (meta->primaries) {
  877. case JPEGXL_PR_SRGB:
  878. avctx->color_primaries = AVCOL_PRI_BT709;
  879. break;
  880. case JPEGXL_PR_P3:
  881. avctx->color_primaries = AVCOL_PRI_SMPTE432;
  882. break;
  883. case JPEGXL_PR_2100:
  884. avctx->color_primaries = AVCOL_PRI_BT2020;
  885. break;
  886. default:
  887. avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
  888. }
  889. } else if (meta->wp == JPEGXL_WP_DCI && meta->primaries == JPEGXL_PR_P3) {
  890. avctx->color_primaries = AVCOL_PRI_SMPTE431;
  891. } else {
  892. avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
  893. }
  894. if (meta->trc > JPEGXL_TR_GAMMA) {
  895. FFJXLTransferCharacteristic trc = meta->trc - JPEGXL_TR_GAMMA;
  896. switch (trc) {
  897. case JPEGXL_TR_BT709:
  898. avctx->color_trc = AVCOL_TRC_BT709;
  899. break;
  900. case JPEGXL_TR_LINEAR:
  901. avctx->color_trc = AVCOL_TRC_LINEAR;
  902. break;
  903. case JPEGXL_TR_SRGB:
  904. avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
  905. break;
  906. case JPEGXL_TR_PQ:
  907. avctx->color_trc = AVCOL_TRC_SMPTEST2084;
  908. break;
  909. case JPEGXL_TR_DCI:
  910. avctx->color_trc = AVCOL_TRC_SMPTE428;
  911. break;
  912. case JPEGXL_TR_HLG:
  913. avctx->color_trc = AVCOL_TRC_ARIB_STD_B67;
  914. break;
  915. default:
  916. avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  917. }
  918. } else if (meta->trc > 0) {
  919. if (meta->trc > 45355 && meta->trc < 45555)
  920. avctx->color_trc = AVCOL_TRC_GAMMA22;
  921. else if (meta->trc > 35614 && meta->trc < 35814)
  922. avctx->color_trc = AVCOL_TRC_GAMMA28;
  923. else
  924. avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  925. } else {
  926. avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  927. }
  928. if (meta->csp == JPEGXL_CS_GRAY) {
  929. if (meta->bit_depth <= 8)
  930. s->format = meta->have_alpha ? AV_PIX_FMT_YA8 : AV_PIX_FMT_GRAY8;
  931. else if (meta->bit_depth <= 16)
  932. s->format = meta->have_alpha ? AV_PIX_FMT_YA16 : AV_PIX_FMT_GRAY16;
  933. else
  934. s->format = meta->have_alpha ? AV_PIX_FMT_NONE : AV_PIX_FMT_GRAYF32;
  935. } else {
  936. if (meta->bit_depth <= 8)
  937. s->format = meta->have_alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;
  938. else if (meta->bit_depth <= 16)
  939. s->format = meta->have_alpha ? AV_PIX_FMT_RGBA64 : AV_PIX_FMT_RGB48;
  940. else
  941. s->format = meta->have_alpha ? AV_PIX_FMT_RGBAF32 : AV_PIX_FMT_RGBF32;
  942. }
  943. }
  944. static int skip_icc_profile(void *avctx, JXLParseContext *ctx, GetBitContext *gb)
  945. {
  946. int64_t ret;
  947. uint32_t last = 0, last2 = 0;
  948. JXLEntropyDecoder dec = { 0 };
  949. uint64_t enc_size = jxl_u64(gb);
  950. uint64_t output_size = 0;
  951. int out_size_shift = 0;
  952. if (!enc_size || enc_size > (1 << 22))
  953. return AVERROR_INVALIDDATA;
  954. ret = entropy_decoder_init(avctx, gb, &dec, 41);
  955. if (ret < 0)
  956. goto end;
  957. if (get_bits_left(gb) < 0) {
  958. ret = AVERROR_BUFFER_TOO_SMALL;
  959. goto end;
  960. }
  961. for (uint64_t read = 0; read < enc_size; read++) {
  962. ret = entropy_decoder_read_symbol(gb, &dec, icc_context(read, last, last2));
  963. if (ret < 0)
  964. goto end;
  965. if (ret > 255) {
  966. ret = AVERROR_INVALIDDATA;
  967. goto end;
  968. }
  969. if (get_bits_left(gb) < 0) {
  970. ret = AVERROR_BUFFER_TOO_SMALL;
  971. goto end;
  972. }
  973. last2 = last;
  974. last = ret;
  975. if (out_size_shift < 63) {
  976. output_size += (ret & UINT64_C(0x7F)) << out_size_shift;
  977. if (!(ret & 0x80)) {
  978. out_size_shift = 63;
  979. } else {
  980. out_size_shift += 7;
  981. if (out_size_shift > 56) {
  982. ret = AVERROR_INVALIDDATA;
  983. goto end;
  984. }
  985. }
  986. } else if (output_size < 132) {
  987. ret = AVERROR_INVALIDDATA;
  988. goto end;
  989. }
  990. }
  991. ret = 0;
  992. end:
  993. entropy_decoder_close(&dec);
  994. return ret;
  995. }
  996. static int skip_extensions(GetBitContext *gb)
  997. {
  998. uint64_t extensions = jxl_u64(gb), extensions_len = 0;
  999. if (get_bits_left(gb) < 0)
  1000. return AVERROR_BUFFER_TOO_SMALL;
  1001. if (!extensions)
  1002. return 0;
  1003. for (int i = 0; i < 64; i++) {
  1004. if (extensions & (UINT64_C(1) << i))
  1005. extensions_len += jxl_u64(gb);
  1006. if (get_bits_left(gb) < 0)
  1007. return AVERROR_BUFFER_TOO_SMALL;
  1008. }
  1009. if (extensions_len > INT_MAX || get_bits_left(gb) < extensions_len)
  1010. return AVERROR_BUFFER_TOO_SMALL;
  1011. skip_bits_long(gb, extensions_len);
  1012. return 0;
  1013. }
  1014. static int parse_frame_header(void *avctx, JXLParseContext *ctx, GetBitContext *gb)
  1015. {
  1016. int all_default, do_yCbCr = 0, num_passes = 1, ret;
  1017. int group_size_shift = 1, lf_level = 0, save_as_ref = 0;
  1018. int have_crop = 0, full_frame = 1, resets_canvas = 1, upsampling = 1;
  1019. JXLFrame *frame = &ctx->codestream.frame;
  1020. const FFJXLMetadata *meta = &ctx->codestream.meta;
  1021. int32_t x0 = 0, y0 = 0;
  1022. uint32_t duration = 0, width = meta->coded_width, height = meta->coded_height;
  1023. uint32_t name_len, num_groups, num_lf_groups, group_dim, lf_group_dim, toc_count;
  1024. uint64_t flags = 0;
  1025. int start_len = get_bits_count(gb);
  1026. memset(frame, 0, sizeof(*frame));
  1027. frame->is_last = 1;
  1028. all_default = get_bits1(gb);
  1029. if (!all_default) {
  1030. frame->type = get_bits(gb, 2);
  1031. frame->encoding = get_bits1(gb);
  1032. flags = jxl_u64(gb);
  1033. if (!meta->xyb_encoded)
  1034. do_yCbCr = get_bits1(gb);
  1035. if (!(flags & JXL_FLAG_USE_LF_FRAME)) {
  1036. if (do_yCbCr)
  1037. skip_bits(gb, 6); // jpeg upsampling
  1038. upsampling = jxl_u32(gb, 1, 2, 4, 8, 0, 0, 0, 0);
  1039. skip_bits_long(gb, 2 * meta->num_extra_channels);
  1040. if (get_bits_left(gb) < 0)
  1041. return AVERROR_BUFFER_TOO_SMALL;
  1042. }
  1043. if (frame->encoding == JPEGXL_ENC_MODULAR)
  1044. group_size_shift = get_bits(gb, 2);
  1045. else if (meta->xyb_encoded)
  1046. skip_bits(gb, 6); // xqm and bqm scales
  1047. if (frame->type != JPEGXL_FRAME_REFERENCE_ONLY) {
  1048. num_passes = jxl_u32(gb, 1, 2, 3, 4, 0, 0, 0, 3);
  1049. if (num_passes != 1) {
  1050. int num_ds = jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 1);
  1051. skip_bits(gb, 2 * (num_passes - 1)); // shift
  1052. skip_bits(gb, 2 * num_ds); // downsample
  1053. for (int i = 0; i < num_ds; i++)
  1054. jxl_u32(gb, 0, 1, 2, 0, 0, 0, 0, 3);
  1055. }
  1056. }
  1057. if (frame->type == JPEGXL_FRAME_LF)
  1058. lf_level = 1 + get_bits(gb, 2);
  1059. else
  1060. have_crop = get_bits1(gb);
  1061. if (have_crop) {
  1062. if (frame->type != JPEGXL_FRAME_REFERENCE_ONLY) {
  1063. uint32_t ux0 = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
  1064. uint32_t uy0 = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
  1065. x0 = unpack_signed(ux0);
  1066. y0 = unpack_signed(uy0);
  1067. }
  1068. width = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
  1069. height = jxl_u32(gb, 0, 256, 2304, 18688, 8, 11, 14, 30);
  1070. full_frame = x0 <= 0 && y0 <= 0 && width + x0 >= meta->coded_width
  1071. && height + y0 >= meta->coded_height;
  1072. }
  1073. if (get_bits_left(gb) < 0)
  1074. return AVERROR_BUFFER_TOO_SMALL;
  1075. if (frame->type == JPEGXL_FRAME_REGULAR || frame->type == JPEGXL_FRAME_SKIP_PROGRESSIVE) {
  1076. for (int i = 0; i <= meta->num_extra_channels; i++) {
  1077. int mode = jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 2);
  1078. if (meta->num_extra_channels && (mode == JPEGXL_BM_BLEND || mode == JPEGXL_BM_MULADD))
  1079. jxl_u32(gb, 0, 1, 2, 3, 0, 0, 0, 2);
  1080. if (meta->num_extra_channels && (mode == JPEGXL_BM_BLEND || mode == JPEGXL_BM_MULADD
  1081. || mode == JPEGXL_BM_MUL))
  1082. skip_bits1(gb);
  1083. if (!i)
  1084. resets_canvas = mode == JPEGXL_BM_REPLACE && full_frame;
  1085. if (!resets_canvas)
  1086. skip_bits(gb, 2);
  1087. if (get_bits_left(gb) < 0)
  1088. return AVERROR_BUFFER_TOO_SMALL;
  1089. }
  1090. if (meta->animation_offset)
  1091. duration = jxl_u32(gb, 0, 1, 0, 0, 0, 0, 8, 32);
  1092. if (meta->have_timecodes)
  1093. skip_bits_long(gb, 32);
  1094. frame->is_last = get_bits1(gb);
  1095. } else {
  1096. frame->is_last = 0;
  1097. }
  1098. if (frame->type != JPEGXL_FRAME_LF && !frame->is_last)
  1099. save_as_ref = get_bits(gb, 2);
  1100. if (frame->type == JPEGXL_FRAME_REFERENCE_ONLY ||
  1101. (resets_canvas && !frame->is_last && (!duration || save_as_ref)
  1102. && frame->type != JPEGXL_FRAME_LF))
  1103. skip_bits1(gb); // save before color transform
  1104. name_len = 8 * jxl_u32(gb, 0, 0, 16, 48, 0, 4, 5, 10);
  1105. if (get_bits_left(gb) < name_len)
  1106. return AVERROR_BUFFER_TOO_SMALL;
  1107. skip_bits_long(gb, name_len);
  1108. }
  1109. if (!all_default) {
  1110. int restd = get_bits1(gb), gab = 1;
  1111. if (!restd)
  1112. gab = get_bits1(gb);
  1113. if (gab && !restd && get_bits1(gb))
  1114. // gab custom
  1115. skip_bits_long(gb, 16 * 6);
  1116. if (get_bits_left(gb) < 0)
  1117. return AVERROR_BUFFER_TOO_SMALL;
  1118. if (!restd) {
  1119. int epf = get_bits(gb, 2);
  1120. if (epf) {
  1121. if (frame->encoding == JPEGXL_ENC_VARDCT && get_bits1(gb)) {
  1122. skip_bits_long(gb, 16 * 8); // custom epf sharpness
  1123. if (get_bits_left(gb) < 0)
  1124. return AVERROR_BUFFER_TOO_SMALL;
  1125. }
  1126. if (get_bits1(gb)) {
  1127. skip_bits_long(gb, 3 * 16 + 32); // custom epf weight
  1128. if (get_bits_left(gb) < 0)
  1129. return AVERROR_BUFFER_TOO_SMALL;
  1130. }
  1131. if (get_bits1(gb)) { // custom epf sigma
  1132. if (frame->encoding == JPEGXL_ENC_VARDCT)
  1133. skip_bits(gb, 16);
  1134. skip_bits_long(gb, 16 * 3);
  1135. if (get_bits_left(gb) < 0)
  1136. return AVERROR_BUFFER_TOO_SMALL;
  1137. }
  1138. if (frame->encoding == JPEGXL_ENC_MODULAR)
  1139. skip_bits(gb, 16);
  1140. }
  1141. ret = skip_extensions(gb);
  1142. if (ret < 0)
  1143. return ret;
  1144. }
  1145. ret = skip_extensions(gb);
  1146. if (ret < 0)
  1147. return ret;
  1148. }
  1149. width = div_ceil(div_ceil(width, upsampling), 1 << (3 * lf_level));
  1150. height = div_ceil(div_ceil(height, upsampling), 1 << (3 * lf_level));
  1151. group_dim = 128 << group_size_shift;
  1152. lf_group_dim = group_dim << 3;
  1153. num_groups = div_ceil(width, group_dim) * div_ceil(height, group_dim);
  1154. num_lf_groups = div_ceil(width, lf_group_dim) * div_ceil(height, lf_group_dim);
  1155. if (num_groups == 1 && num_passes == 1)
  1156. toc_count = 1;
  1157. else
  1158. toc_count = 2 + num_lf_groups + num_groups * num_passes;
  1159. // permuted toc
  1160. if (get_bits1(gb)) {
  1161. JXLEntropyDecoder dec;
  1162. int64_t end, lehmer = 0;
  1163. ret = entropy_decoder_init(avctx, gb, &dec, 8);
  1164. if (ret < 0)
  1165. return ret;
  1166. if (get_bits_left(gb) < 0) {
  1167. entropy_decoder_close(&dec);
  1168. return AVERROR_BUFFER_TOO_SMALL;
  1169. }
  1170. end = entropy_decoder_read_symbol(gb, &dec, toc_context(toc_count));
  1171. if (end < 0 || end > toc_count) {
  1172. entropy_decoder_close(&dec);
  1173. return AVERROR_INVALIDDATA;
  1174. }
  1175. for (uint32_t i = 0; i < end; i++) {
  1176. lehmer = entropy_decoder_read_symbol(gb, &dec, toc_context(lehmer));
  1177. if (lehmer < 0 || get_bits_left(gb) < 0) {
  1178. entropy_decoder_close(&dec);
  1179. return AVERROR_BUFFER_TOO_SMALL;
  1180. }
  1181. }
  1182. entropy_decoder_close(&dec);
  1183. }
  1184. align_get_bits(gb);
  1185. for (uint32_t i = 0; i < toc_count; i++) {
  1186. frame->body_length += 8 * jxl_u32(gb, 0, 1024, 17408, 4211712, 10, 14, 22, 30);
  1187. if (get_bits_left(gb) < 0)
  1188. return AVERROR_BUFFER_TOO_SMALL;
  1189. }
  1190. align_get_bits(gb);
  1191. frame->total_length = frame->body_length + get_bits_count(gb) - start_len;
  1192. return 0;
  1193. }
  1194. static int skip_boxes(JXLParseContext *ctx, const uint8_t *buf, int buf_size)
  1195. {
  1196. GetByteContext gb;
  1197. if (ctx->skip > buf_size)
  1198. return AVERROR_BUFFER_TOO_SMALL;
  1199. buf += ctx->skip;
  1200. buf_size -= ctx->skip;
  1201. bytestream2_init(&gb, buf, buf_size);
  1202. while (1) {
  1203. uint64_t size;
  1204. int head_size = 8;
  1205. if (bytestream2_peek_le16(&gb) == FF_JPEGXL_CODESTREAM_SIGNATURE_LE)
  1206. break;
  1207. if (bytestream2_peek_le64(&gb) == FF_JPEGXL_CONTAINER_SIGNATURE_LE)
  1208. break;
  1209. if (bytestream2_get_bytes_left(&gb) < 8)
  1210. return AVERROR_BUFFER_TOO_SMALL;
  1211. size = bytestream2_get_be32(&gb);
  1212. bytestream2_skip(&gb, 4); // tag
  1213. if (size == 1) {
  1214. if (bytestream2_get_bytes_left(&gb) < 8)
  1215. return AVERROR_BUFFER_TOO_SMALL;
  1216. size = bytestream2_get_be64(&gb);
  1217. head_size = 16;
  1218. }
  1219. if (!size)
  1220. return AVERROR_INVALIDDATA;
  1221. /* invalid ISOBMFF size */
  1222. if (size <= head_size || size > INT_MAX - ctx->skip)
  1223. return AVERROR_INVALIDDATA;
  1224. ctx->skip += size;
  1225. bytestream2_skip(&gb, size - head_size);
  1226. if (bytestream2_get_bytes_left(&gb) <= 0)
  1227. return AVERROR_BUFFER_TOO_SMALL;
  1228. }
  1229. return 0;
  1230. }
  1231. static int try_parse(AVCodecParserContext *s, AVCodecContext *avctx, JXLParseContext *ctx,
  1232. const uint8_t *buf, int buf_size)
  1233. {
  1234. int ret, cs_buflen, header_skip;
  1235. const uint8_t *cs_buffer;
  1236. GetBitContext gb;
  1237. if (ctx->skip > buf_size)
  1238. return AVERROR_BUFFER_TOO_SMALL;
  1239. buf += ctx->skip;
  1240. buf_size -= ctx->skip;
  1241. if (ctx->container || AV_RL64(buf) == FF_JPEGXL_CONTAINER_SIGNATURE_LE) {
  1242. ctx->container = 1;
  1243. ret = ff_jpegxl_collect_codestream_header(buf, buf_size, ctx->cs_buffer,
  1244. sizeof(ctx->cs_buffer) - AV_INPUT_BUFFER_PADDING_SIZE, &ctx->copied);
  1245. if (ret < 0)
  1246. return ret;
  1247. ctx->collected_size = ret;
  1248. if (!ctx->copied) {
  1249. ctx->skip += ret;
  1250. return AVERROR_BUFFER_TOO_SMALL;
  1251. }
  1252. cs_buffer = ctx->cs_buffer;
  1253. cs_buflen = FFMIN(sizeof(ctx->cs_buffer) - AV_INPUT_BUFFER_PADDING_SIZE, ctx->copied);
  1254. } else {
  1255. cs_buffer = buf;
  1256. cs_buflen = buf_size;
  1257. }
  1258. if (!ctx->codestream_length) {
  1259. header_skip = ff_jpegxl_parse_codestream_header(cs_buffer, cs_buflen, &ctx->codestream.meta, 0);
  1260. if (header_skip < 0)
  1261. return header_skip;
  1262. ctx->codestream_length = header_skip;
  1263. populate_fields(s, avctx, &ctx->codestream.meta);
  1264. }
  1265. if (ctx->container)
  1266. return ctx->collected_size;
  1267. ret = init_get_bits8(&gb, cs_buffer, cs_buflen);
  1268. if (ret < 0)
  1269. return ret;
  1270. skip_bits_long(&gb, ctx->codestream_length);
  1271. if (!ctx->skipped_icc && ctx->codestream.meta.have_icc_profile) {
  1272. ret = skip_icc_profile(avctx, ctx, &gb);
  1273. if (ret < 0)
  1274. return ret;
  1275. ctx->skipped_icc = 1;
  1276. align_get_bits(&gb);
  1277. ctx->codestream_length = get_bits_count(&gb);
  1278. }
  1279. if (get_bits_left(&gb) <= 0)
  1280. return AVERROR_BUFFER_TOO_SMALL;
  1281. while (1) {
  1282. ret = parse_frame_header(avctx, ctx, &gb);
  1283. if (ret < 0)
  1284. return ret;
  1285. ctx->codestream_length += ctx->codestream.frame.total_length;
  1286. if (ctx->codestream.frame.is_last)
  1287. return ctx->codestream_length / 8;
  1288. if (get_bits_left(&gb) <= ctx->codestream.frame.body_length)
  1289. return AVERROR_BUFFER_TOO_SMALL;
  1290. skip_bits_long(&gb, ctx->codestream.frame.body_length);
  1291. }
  1292. }
  1293. static int jpegxl_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  1294. const uint8_t **poutbuf, int *poutbuf_size,
  1295. const uint8_t *buf, int buf_size)
  1296. {
  1297. JXLParseContext *ctx = s->priv_data;
  1298. int next = END_NOT_FOUND, ret;
  1299. const uint8_t *pbuf = ctx->pc.buffer;
  1300. int pindex = ctx->pc.index;
  1301. *poutbuf_size = 0;
  1302. *poutbuf = NULL;
  1303. if (!ctx->pc.index) {
  1304. if (ctx->pc.overread)
  1305. goto flush;
  1306. pbuf = buf;
  1307. pindex = buf_size;
  1308. }
  1309. if ((!ctx->container || !ctx->codestream_length) && !ctx->next) {
  1310. ret = try_parse(s, avctx, ctx, pbuf, pindex);
  1311. if (ret < 0)
  1312. goto flush;
  1313. ctx->next = ret;
  1314. if (ctx->container)
  1315. ctx->skip += ctx->next;
  1316. }
  1317. if (ctx->container && ctx->next >= 0) {
  1318. ret = skip_boxes(ctx, pbuf, pindex);
  1319. if (ret < 0) {
  1320. if (ret == AVERROR_INVALIDDATA)
  1321. ctx->next = -1;
  1322. goto flush;
  1323. }
  1324. ctx->next = ret + ctx->skip;
  1325. }
  1326. if (ctx->next >= 0)
  1327. next = ctx->next - ctx->pc.index;
  1328. flush:
  1329. if (next > buf_size)
  1330. next = END_NOT_FOUND;
  1331. ret = ff_combine_frame(&ctx->pc, next, &buf, &buf_size);
  1332. if (ret < 0)
  1333. return buf_size;
  1334. *poutbuf = buf;
  1335. *poutbuf_size = buf_size;
  1336. ctx->codestream_length = 0;
  1337. ctx->collected_size = 0;
  1338. ctx->container = 0;
  1339. ctx->copied = 0;
  1340. ctx->skip = 0;
  1341. ctx->skipped_icc = 0;
  1342. ctx->next = 0;
  1343. memset(&ctx->codestream, 0, sizeof(ctx->codestream));
  1344. return next;
  1345. }
  1346. const AVCodecParser ff_jpegxl_parser = {
  1347. .codec_ids = { AV_CODEC_ID_JPEGXL, AV_CODEC_ID_JPEGXL_ANIM },
  1348. .priv_data_size = sizeof(JXLParseContext),
  1349. .parser_parse = jpegxl_parse,
  1350. .parser_close = ff_parse_close,
  1351. };