FFmpeg
jpeg2000htdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Caleb Etemesi <etemesicaleb@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * Copyright 2019 - 2021, Osamu Watanabe
23  *
24  * Redistribution and use in source and binary forms, with or without modification,
25  * are permitted provided that the following conditions are met:
26  *
27  * 1. Redistributions of source code must retain the above copyright notice, this
28  * list of conditions and the following disclaimer.
29  *
30  * 2. Redistributions in binary form must reproduce the above copyright notice,
31  * this list of conditions and the following disclaimer in the documentation
32  * and/or other materials provided with the distribution.
33  *
34  * 3. Neither the name of the copyright holder nor the names of its contributors
35  * may be used to endorse or promote products derived from this software without
36  * specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
42  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
45  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49 
50 #include <stdint.h>
51 #include "libavutil/attributes.h"
52 #include "libavutil/common.h"
53 #include "libavutil/avassert.h"
54 #include "jpeg2000htdec.h"
55 #include "jpeg2000.h"
56 #include "jpeg2000dec.h"
57 
58 #define J2K_Q1 0
59 #define J2K_Q2 1
60 
61 #define HT_SHIFT_SIGMA 0
62 #define HT_SHIFT_SCAN 4
63 #define HT_SHIFT_REF 3
64 #define HT_SHIFT_REF_IND 2
65 
66 /* See Rec. ITU-T T.800, Table 2 */
67 const static uint8_t mel_e[13] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5 };
68 
69 static const uint16_t dec_cxt_vlc_table1[1024];
70 static const uint16_t dec_cxt_vlc_table0[1024];
71 
72 typedef struct StateVars {
74  uint32_t bits;
75  uint32_t tmp;
76  uint32_t last;
77  uint8_t bits_left;
78  uint64_t bit_buf;
79 } StateVars;
80 
81 typedef struct MelDecoderState {
82  uint8_t k;
83  uint8_t run;
84  uint8_t one;
86 
87 /**
88  * Given a precomputed c, checks whether n % d == 0. c is precomputed from d
89  * using precompute_c().
90  */
92 static uint32_t is_divisible(uint32_t n, uint64_t c)
93 {
94  return n * c <= c - 1;
95 }
96 
97 /**
98  * Precompute the number c used by is_divisible().
99  */
101 static uint64_t precompute_c(uint32_t d)
102 {
103  return 1 + (0xffffffffffffffffull / d);
104 }
105 
107 {
108  s->bits_left = 0;
109  s->bit_buf = 0;
110  s->tmp = 0;
111  s->bits = 0;
112  s->pos = 0;
113  s->last = 0;
114 }
115 
116 static void jpeg2000_init_mel(StateVars *s, uint32_t Pcup)
117 {
119  s->pos = Pcup;
120 }
121 
122 static void jpeg2000_init_mag_ref(StateVars *s, uint32_t Lref)
123 {
124  s->pos = Lref - 2;
125  s->bits = 0;
126  s->last = 0xFF;
127  s->tmp = 0;
128  s->bits_left = 0;
129  s->bit_buf = 0;
130 }
131 
133 {
134  mel_state->k = 0;
135  mel_state->run = 0;
136  mel_state->one = 0;
137 }
138 
139 /**
140  * Refill the buffer backwards in little endian while skipping over stuffing
141  * bits. Stuffing bits are those that appear in the position of any byte whose
142  * LSBs are all 1's if the last consumed byte was larger than 0x8F.
143  */
145 {
146  uint64_t tmp = 0;
147  int32_t position = buffer->pos - 4;
148  uint32_t new_bits = 32;
149 
150  if (buffer->bits_left >= 32)
151  return 0; // enough data, no need to pull in more bits
152 
153  /**
154  * Unstuff bits. Load a temporary byte, which precedes the position we
155  * currently at, to ensure that we can also un-stuff if the stuffed bit is
156  * the bottom most bits.
157  */
158 
159  for(int i = FFMAX(0, position + 1); i <= buffer->pos + 1; i++)
160  tmp = 256*tmp + array[i];
161 
162  if ((tmp & 0x7FFF000000) > 0x7F8F000000) {
163  tmp &= 0x7FFFFFFFFF;
164  new_bits--;
165  }
166  if ((tmp & 0x007FFF0000) > 0x007F8F0000) {
167  tmp = (tmp & 0x007FFFFFFF) + ((tmp & 0xFF00000000) >> 1);
168  new_bits--;
169  }
170  if ((tmp & 0x00007FFF00) > 0x00007F8F00) {
171  tmp = (tmp & 0x00007FFFFF) + ((tmp & 0xFFFF000000) >> 1);
172  new_bits--;
173  }
174  if ((tmp & 0x0000007FFF) > 0x0000007F8F) {
175  tmp = (tmp & 0x0000007FFF) + ((tmp & 0xFFFFFF0000) >> 1);
176  new_bits--;
177  }
178 
179  tmp >>= 8; // Remove temporary byte loaded
180 
181  /* Add bits to the MSB of the bit buffer */
182  buffer->bit_buf |= tmp << buffer->bits_left;
183  buffer->bits_left += new_bits;
184  buffer->pos = FFMAX(0, position);
185  return 0;
186 }
187 
188 /**
189  * Refill the bit-buffer reading new bits going forward
190  * in the stream while skipping over stuffed bits.
191  */
193  uint32_t length)
194 {
195  while (buffer->bits_left < 32) {
196  buffer->tmp = 0xFF;
197  buffer->bits = (buffer->last == 0xFF) ? 7 : 8;
198  if (buffer->pos <= length) {
199  buffer->tmp = array[buffer->pos];
200  buffer->pos += 1;
201  buffer->last = buffer->tmp;
202  }
203  buffer->bit_buf |= ((uint64_t) buffer->tmp) << buffer->bits_left;
204  buffer->bits_left += buffer->bits;
205  }
206 }
207 
208 /**
209  * Drops bits from lower bits in the bit buffer. buf contains the bit buffers.
210  * nbits is the number of bits to remove.
211  */
213 static void jpeg2000_bitbuf_drop_bits_lsb(StateVars *buf, uint8_t nbits)
214 {
215  av_assert2(buf->bits_left >= nbits); // cannot read more bits than available
216  buf->bit_buf >>= nbits;
217  buf->bits_left -= nbits;
218 }
219 
220 /**
221  * Get bits from the bit buffer reading them from the least significant bits
222  * moving to the most significant bits. In case there are fewer bits, refill
223  * from buf moving backwards.
224  */
226 static uint64_t jpeg2000_bitbuf_get_bits_lsb(StateVars *bit_stream, uint8_t nbits,
227  const uint8_t *buf)
228 {
229  uint64_t bits;
230  uint64_t mask = (1ull << nbits) - 1;
231  if (bit_stream->bits_left < nbits)
232  jpeg2000_bitbuf_refill_backwards(bit_stream, buf);
233  bits = bit_stream->bit_buf & mask;
234  jpeg2000_bitbuf_drop_bits_lsb(bit_stream, nbits);
235  return bits;
236 }
237 
238 /**
239  * Get bits from the bit buffer reading them from the least significant bits
240  * moving to the most significant bits. In case there are fewer bits, refill from
241  * buf moving forward.
242  */
245  uint8_t nbits, const uint8_t *buf,
246  uint32_t length)
247 {
248  uint64_t bits;
249  uint64_t mask = (1ull << nbits) - 1;
250 
251  if (bit_stream->bits_left <= nbits)
252  jpeg2000_bitbuf_refill_forward(bit_stream, buf, length);
253  bits = bit_stream->bit_buf & mask;
254  jpeg2000_bitbuf_drop_bits_lsb(bit_stream, nbits);
255  return bits;
256 }
257 
258 /**
259  * Look ahead bit buffer without discarding bits.
260  */
262 static uint64_t jpeg2000_bitbuf_peek_bits_lsb(StateVars *stream, uint8_t nbits)
263 {
264  uint64_t mask = (1ull << nbits) - 1;
265  return stream->bit_buf & mask;
266 }
267 
268 static void jpeg2000_init_vlc(StateVars *s, uint32_t Lcup, uint32_t Pcup,
269  const uint8_t *Dcup)
270 {
271  s->bits_left = 0;
272  s->bit_buf = 0;
273  s->pos = Lcup - 2 - Pcup;
274  s->last = Dcup[Lcup - 2];
275  s->tmp = (s->last) >> 4;
276  s->bits = ((s->tmp & 7) < 7) ? 4 : 3;
277 
278  jpeg2000_bitbuf_refill_backwards(s, Dcup + Pcup);
280 }
281 
282 /**
283  * Decode prefix codes for VLC segment. See Rec. ITU-T T.814, 7.3.5.
284  */
287  StateVars *vlc_stream, const uint16_t *table,
288  const uint8_t *Dcup, uint8_t *sig_pat,
289  uint8_t *res_off, uint8_t *emb_pat_k,
290  uint8_t *emb_pat_1, uint8_t pos,
291  uint32_t Pcup, uint16_t context)
292 {
293  uint32_t value;
294  uint8_t len;
295  uint64_t index;
296  uint64_t code_word;
297 
298  jpeg2000_bitbuf_refill_backwards(vlc_stream, Dcup + Pcup);
299 
300  code_word = vlc_stream->bit_buf & 0x7f;
301  index = code_word + (context << 7);
302 
303  av_assert0(index < 1024); // The CxtVLC table has 1024 entries.
304 
305  value = table[index];
306 
307  len = (value & 0x000F) >> 1;
308 
309  res_off[pos] = (uint8_t) (value & 1);
310  sig_pat[pos] = (uint8_t) ((value & 0x00F0) >> 4);
311  emb_pat_k[pos] = (uint8_t) ((value & 0x0F00) >> 8);
312  emb_pat_1[pos] = (uint8_t) ((value & 0xF000) >> 12);
313 
314  jpeg2000_bitbuf_drop_bits_lsb(vlc_stream, len);
315  return 0;
316 }
317 
318 /**
319  * Decode variable length u-vlc prefix. See decodeUPrefix procedure at Rec.
320  * ITU-T T.814, 7.3.6.
321  */
323 static uint8_t vlc_decode_u_prefix(StateVars *vlc_stream, const uint8_t *refill_array)
324 {
325  static const uint8_t return_value[8] = { 5, 1, 2, 1, 3, 1, 2, 1 };
326  static const uint8_t drop_bits[8] = { 3, 1, 2, 1, 3, 1, 2, 1 };
327 
328  uint8_t bits;
329 
330  if (vlc_stream->bits_left < 3)
331  jpeg2000_bitbuf_refill_backwards(vlc_stream, refill_array);
332 
333  bits = jpeg2000_bitbuf_peek_bits_lsb(vlc_stream, 3);
334 
335  jpeg2000_bitbuf_drop_bits_lsb(vlc_stream, drop_bits[bits]);
336  return return_value[bits];
337 }
338 
339 /**
340  * Decode variable length u-vlc suffix. See decodeUSuffix procedure at Rec.
341  * ITU-T T.814, 7.3.6.
342  */
344 static uint8_t vlc_decode_u_suffix(StateVars *vlc_stream, uint8_t suffix,
345  const uint8_t *refill_array)
346 {
347  static const int mask[] = { 1, 31 };
348  static const int drop_bits[] = { 1, 5 };
349 
350  uint8_t bits;
351  int cond = suffix != 3;
352  if (suffix < 3)
353  return 0;
354 
355  if (vlc_stream->bits_left < 5)
356  jpeg2000_bitbuf_refill_backwards(vlc_stream, refill_array);
357 
358  bits = jpeg2000_bitbuf_peek_bits_lsb(vlc_stream, 5);
359 
360  jpeg2000_bitbuf_drop_bits_lsb(vlc_stream, drop_bits[cond]);
361  return bits & mask[cond];
362 }
363 
364 /**
365  * Decode u-vlc extension values. See decodeUExtension procedure at Rec. ITU-T
366  * T.814, 7.3.6.
367  */
369 static uint8_t vlc_decode_u_extension(StateVars *vlc_stream, uint8_t suffix,
370  const uint8_t *refill_array)
371 {
372  return jpeg2000_bitbuf_get_bits_lsb(vlc_stream, 4 * (suffix >= 28), refill_array);
373 }
374 
375 /**
376  * Magnitude and Sign decode procedures. See decodeMagSgnValue procedure at Rec.
377  * ITU-T T.814, 7.3.8.
378  */
380 static int32_t jpeg2000_decode_mag_sgn(StateVars *mag_sgn_stream, int32_t m_n,
381  int32_t i_n, const uint8_t *buf, uint32_t length)
382 {
383  int32_t val = 0;
384  if (m_n > 0) {
385  val = jpeg2000_bitbuf_get_bits_lsb_forward(mag_sgn_stream,m_n,buf,length);
386  val += (i_n << m_n);
387  }
388  return val;
389 }
390 
392 static void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t m_n[2],
393  int32_t known_1[2], const uint8_t emb_pat_1[2],
394  int32_t v[2][4], int32_t m[2][4], uint8_t *E,
395  uint32_t *mu_n, const uint8_t *Dcup, uint32_t Pcup,
396  uint32_t pLSB)
397 {
398  for (int i = 0; i < 4; i++) {
399  int32_t n = 4 * q + i;
400  m_n[pos] = m[pos][i];
401  known_1[pos] = (emb_pat_1[pos] >> i) & 1;
402  v[pos][i] = jpeg2000_decode_mag_sgn(mag_sgn, m_n[pos], known_1[pos], Dcup, Pcup);
403 
404  if (m_n[pos] != 0) {
405  E[n] = 32 - ff_clz(v[pos][i] | 1);
406  mu_n[n] = (v[pos][i] >> 1) + 1;
407  mu_n[n] <<= pLSB;
408  mu_n[n] |= ((uint32_t) (v[pos][i] & 1)) << 31; // sign bit.
409  }
410  }
411 }
412 
413 static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t length)
414 {
415  int cond = stream->pos <= length;
416  int pos = FFMIN(stream->pos, length);
417  if (stream->bits == 0) {
418  stream->bits = (stream->tmp == 0xFF) ? 7 : 8;
419  stream->pos += cond;
420  stream->tmp = cond ? array[pos] : 0xFF;
421  }
422  stream->bits -= 1;
423  return (stream->tmp >> stream->bits) & 1;
424 }
425 
426 static int jpeg2000_peek_bit(StateVars *stream, const uint8_t *array, uint32_t length)
427 {
428  if (stream->bits == 0) {
429  int cond = stream->pos <= length;
430  int pos = FFMIN(stream->pos, length);
431  stream->bits = (stream->tmp == 0xFF) ? 7 : 8;
432  stream->pos += cond;
433  stream->tmp = cond ? array[pos] : 0xFF;
434  }
435  return (stream->tmp >> stream->bits) & 1;
436 }
437 
439  StateVars *mel_stream,
440  const uint8_t *Dcup,
441  uint32_t Lcup)
442 {
443 
444  if (mel_state->run == 0 && mel_state->one == 0) {
445  uint8_t eval;
446  uint8_t bit;
447 
448  eval = mel_e[mel_state->k];
449  bit = jpeg2000_import_bit(mel_stream, Dcup, Lcup);
450  if (bit == 1) {
451  mel_state->run = 1 << eval;
452  mel_state->k = FFMIN(12, mel_state->k + 1);
453  } else {
454  mel_state->run = 0;
455  while (eval > 0) {
456  bit = jpeg2000_import_bit(mel_stream, Dcup, Lcup);
457  mel_state->run = (2 * (mel_state->run)) + bit;
458  eval -= 1;
459  }
460  mel_state->k = FFMAX(0, mel_state->k - 1);
461  mel_state->one = 1;
462  }
463  }
464  if (mel_state->run > 0) {
465  mel_state->run -= 1;
466  return 0;
467  } else {
468  mel_state->one = 0;
469  return 1;
470  }
471 }
472 
473 /**
474  * Magref decoding procedures.
475  */
477 static int jpeg2000_import_magref_bit(StateVars *stream, const uint8_t *array,
478  uint32_t length)
479 {
480  return jpeg2000_bitbuf_get_bits_lsb(stream, 1, array);
481 }
482 
483 /**
484  * Signal EMB decode.
485  */
487  StateVars *mel_stream, StateVars *vlc_stream,
488  const uint16_t *vlc_table, const uint8_t *Dcup,
489  uint8_t *sig_pat, uint8_t *res_off, uint8_t *emb_pat_k,
490  uint8_t *emb_pat_1, uint8_t pos, uint16_t context,
491  uint32_t Lcup, uint32_t Pcup)
492 {
493  if (context == 0) {
494  uint8_t sym;
495  sym = jpeg2000_decode_mel_sym(mel_state, mel_stream, Dcup, Lcup);
496  if (sym == 0) {
497  sig_pat[pos] = 0;
498  res_off[pos] = 0;
499  emb_pat_k[pos] = 0;
500  emb_pat_1[pos] = 0;
501  return 0;
502  }
503  }
504  return jpeg2000_decode_ctx_vlc(s, vlc_stream, vlc_table, Dcup, sig_pat,
505  res_off, emb_pat_k, emb_pat_1, pos, Pcup,
506  context);
507 }
508 
510 static int jpeg2000_get_state(int x1, int x2, int width, int shift_by,
511  const uint8_t *block_states)
512 {
513  return (block_states[(x1 + 1) * (width + 2) + (x2 + 1)] >> shift_by) & 1;
514 }
515 
517 static void jpeg2000_modify_state(int x1, int x2, int width,
518  int value, uint8_t *block_states)
519 {
520  block_states[(x1 + 1) * (width + 2) + (x2 + 1)] |= value;
521 }
522 
526  MelDecoderState *mel_state,
527  StateVars *mel_stream, StateVars *vlc_stream,
528  StateVars *mag_sgn_stream, const uint8_t *Dcup,
529  uint32_t Lcup, uint32_t Pcup, uint8_t pLSB,
530  int width, int height, int32_t *sample_buf,
531  uint8_t *block_states)
532 {
533  uint16_t q = 0; // Represents current quad position
534  uint16_t q1, q2;
535  uint16_t context1, context2;
536  uint16_t context = 0;
537 
538  uint8_t sig_pat[2] = { 0 }; // significance pattern
539  uint8_t res_off[2] = { 0 }; // residual offset
540  uint8_t emb_pat_k[2] = { 0 }; // exponent Max Bound pattern K
541  uint8_t emb_pat_1[2] = { 0 }; // exponent Max Bound pattern 1
542  uint8_t gamma[2] = { 0 };
543 
544  uint8_t E_n[2] = { 0 };
545  uint8_t E_ne[2] = { 0 };
546  uint8_t E_nw[2] = { 0 };
547  uint8_t E_nf[2] = { 0 };
548 
549  uint8_t max_e[2] = { 0 };
550  uint8_t u_pfx[2] = { 0 };
551  uint8_t u_sfx[2] = { 0 };
552  uint8_t u_ext[2] = { 0 };
553 
554  int32_t u[2] = { 0 };
555  int32_t U[2] = { 0 }; // exponent bound
556  int32_t m_n[2] = { 0 };
557  int32_t known_1[2] = { 0 };
558 
559  int32_t m[2][4] = { 0 };
560  int32_t v[2][4] = { 0 };
561 
562  uint8_t kappa[2] = { 1, 1 };
563 
564  int ret = 0;
565 
566  int sp;
567 
568  uint64_t c;
569 
570  uint8_t *sigma, *sigma_n, *E;
571  uint32_t *mu, *mu_n;
572 
573  const uint8_t *vlc_buf = Dcup + Pcup;
574 
575  /*
576  * Bound on the precision needed to process the codeblock. The number of
577  * decoded bit planes is equal to at most cblk->zbp + 2 since S_blk = P if
578  * there are no placeholder passes or HT Sets and P = cblk->zbp. See Rec.
579  * ITU-T T.814, 7.6.
580  */
581  int maxbp = cblk->zbp + 2;
582 
583  /* convert to raster-scan */
584  const uint16_t is_border_x = width % 2;
585  const uint16_t is_border_y = height % 2;
586 
587  const uint16_t quad_width = ff_jpeg2000_ceildivpow2(width, 1);
588  const uint16_t quad_height = ff_jpeg2000_ceildivpow2(height, 1);
589 
590  size_t buf_size = 4 * quad_width * quad_height;
591 
592  /* do we have enough precision, assuming a 32-bit decoding path */
593  if (maxbp >= 32)
594  return AVERROR_INVALIDDATA;
595 
596  sigma_n = av_calloc(buf_size, sizeof(uint8_t));
597  E = av_calloc(buf_size, sizeof(uint8_t));
598  mu_n = av_calloc(buf_size, sizeof(uint32_t));
599 
600  if (!sigma_n || !E || !mu_n) {
601  ret = AVERROR(ENOMEM);
602  goto free;
603  }
604 
605  sigma = sigma_n;
606  mu = mu_n;
607 
608  while (q < quad_width - 1) {
609  q1 = q;
610  q2 = q1 + 1;
611 
612  if ((ret = jpeg2000_decode_sig_emb(s, mel_state, mel_stream, vlc_stream,
613  dec_cxt_vlc_table0, Dcup, sig_pat, res_off,
614  emb_pat_k, emb_pat_1, J2K_Q1, context, Lcup,
615  Pcup)) < 0)
616  goto free;
617 
618  for (int i = 0; i < 4; i++)
619  sigma_n[4 * q1 + i] = (sig_pat[J2K_Q1] >> i) & 1;
620 
621  /* calculate context */
622  context = sigma_n[4 * q1]; // f
623  context |= sigma_n[4 * q1 + 1]; // sf
624  context += sigma_n[4 * q1 + 2] << 1; // w << 1
625  context += sigma_n[4 * q1 + 3] << 2;
626 
627  if ((ret = jpeg2000_decode_sig_emb(s, mel_state, mel_stream, vlc_stream,
628  dec_cxt_vlc_table0, Dcup, sig_pat, res_off,
629  emb_pat_k, emb_pat_1, J2K_Q2, context, Lcup,
630  Pcup)) < 0)
631  goto free;
632 
633  for (int i = 0; i < 4; i++)
634  sigma_n[4 * q2 + i] = (sig_pat[J2K_Q2] >> i) & 1;
635 
636  /* calculate context for the next quad */
637  context = sigma_n[4 * q2]; // f
638  context |= sigma_n[4 * q2 + 1]; // sf
639  context += sigma_n[4 * q2 + 2] << 1; // w << 1
640  context += sigma_n[4 * q2 + 3] << 2; // sw << 2
641 
642  u[0] = 0;
643  u[1] = 0;
644 
645  jpeg2000_bitbuf_refill_backwards(vlc_stream, vlc_buf);
646 
647  if (res_off[J2K_Q1] == 1 && res_off[J2K_Q2] == 1) {
648 
649  if (jpeg2000_decode_mel_sym(mel_state, mel_stream, Dcup, Lcup) == 1) {
650 
651  u_pfx[J2K_Q1] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
652  u_pfx[J2K_Q2] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
653 
654  u_sfx[J2K_Q1] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q1], vlc_buf);
655  u_sfx[J2K_Q2] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q2], vlc_buf);
656 
657  u_ext[J2K_Q1] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q1], vlc_buf);
658  u_ext[J2K_Q2] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q2], vlc_buf);
659 
660  u[J2K_Q1] = 2 + u_pfx[J2K_Q1] + u_sfx[J2K_Q1] + (u_ext[J2K_Q1] * 4);
661  u[J2K_Q2] = 2 + u_pfx[J2K_Q2] + u_sfx[J2K_Q2] + (u_ext[J2K_Q2] * 4);
662 
663  } else {
664  u_pfx[J2K_Q1] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
665 
666  if (u_pfx[J2K_Q1] > 2) {
667  u[J2K_Q2] = jpeg2000_bitbuf_get_bits_lsb(vlc_stream, 1, vlc_buf) + 1;
668  u_sfx[J2K_Q1] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q1], vlc_buf);
669  u_ext[J2K_Q1] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q1], vlc_buf);
670  } else {
671  u_pfx[J2K_Q2] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
672  u_sfx[J2K_Q1] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q1], vlc_buf);
673  u_sfx[J2K_Q2] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q2], vlc_buf);
674  u_ext[J2K_Q1] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q1], vlc_buf);
675  u_ext[J2K_Q2] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q2], vlc_buf);
676  u[J2K_Q2] = u_pfx[J2K_Q2] + u_sfx[J2K_Q2] + (u_ext[J2K_Q2] * 4);
677  }
678  /* See Rec. ITU-T T.814, 7.3.6(3) */
679  u[J2K_Q1] = u_pfx[J2K_Q1] + u_sfx[J2K_Q1] + (u_ext[J2K_Q1] * 4);
680  }
681 
682  } else if (res_off[J2K_Q1] == 1 || res_off[J2K_Q2] == 1) {
683  uint8_t pos = res_off[J2K_Q1] == 1 ? 0 : 1;
684  u_pfx[pos] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
685  u_sfx[pos] = vlc_decode_u_suffix(vlc_stream, u_pfx[pos], vlc_buf);
686  u_ext[pos] = vlc_decode_u_extension(vlc_stream, u_sfx[pos], vlc_buf);
687  u[pos] = u_pfx[pos] + u_sfx[pos] + (u_ext[pos] * 4);
688  }
689  U[J2K_Q1] = kappa[J2K_Q1] + u[J2K_Q1];
690  U[J2K_Q2] = kappa[J2K_Q2] + u[J2K_Q2];
691  if (U[J2K_Q1] > maxbp || U[J2K_Q2] > maxbp) {
693  goto free;
694  }
695 
696  for (int i = 0; i < 4; i++) {
697  m[J2K_Q1][i] = sigma_n[4 * q1 + i] * U[J2K_Q1] - ((emb_pat_k[J2K_Q1] >> i) & 1);
698  m[J2K_Q2][i] = sigma_n[4 * q2 + i] * U[J2K_Q2] - ((emb_pat_k[J2K_Q2] >> i) & 1);
699  }
700 
701  recover_mag_sgn(mag_sgn_stream, J2K_Q1, q1, m_n, known_1, emb_pat_1, v, m,
702  E, mu_n, Dcup, Pcup, pLSB);
703 
704  recover_mag_sgn(mag_sgn_stream, J2K_Q2, q2, m_n, known_1, emb_pat_1, v, m,
705  E, mu_n, Dcup, Pcup, pLSB);
706 
707  q += 2; // Move to the next quad pair
708  }
709 
710  if (quad_width % 2 == 1) {
711  q1 = q;
712 
713  if ((ret = jpeg2000_decode_sig_emb(s, mel_state, mel_stream, vlc_stream,
714  dec_cxt_vlc_table0, Dcup, sig_pat, res_off,
715  emb_pat_k, emb_pat_1, J2K_Q1, context, Lcup,
716  Pcup)) < 0)
717  goto free;
718 
719  for (int i = 0; i < 4; i++)
720  sigma_n[4 * q1 + i] = (sig_pat[J2K_Q1] >> i) & 1;
721 
722  u[J2K_Q1] = 0;
723 
724  if (res_off[J2K_Q1] == 1) {
725  u_pfx[J2K_Q1] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
726  u_sfx[J2K_Q1] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q1], vlc_buf);
727  u_ext[J2K_Q1] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q1], vlc_buf);
728  u[J2K_Q1] = u_pfx[J2K_Q1] + u_sfx[J2K_Q1] + (u_ext[J2K_Q1] * 4);
729  }
730 
731  U[J2K_Q1] = kappa[J2K_Q1] + u[J2K_Q1];
732  if (U[J2K_Q1] > maxbp) {
734  goto free;
735  }
736 
737  for (int i = 0; i < 4; i++)
738  m[J2K_Q1][i] = sigma_n[4 * q1 + i] * U[J2K_Q1] - ((emb_pat_k[J2K_Q1] >> i) & 1);
739 
740  recover_mag_sgn(mag_sgn_stream, J2K_Q1, q1, m_n, known_1, emb_pat_1, v, m,
741  E, mu_n, Dcup, Pcup, pLSB);
742 
743  q++; // move to next quad pair
744  }
745 
746  /**
747  * Initial line pair end. As an optimization, we can replace modulo
748  * operations with checking if a number is divisible , since that's the only
749  * thing we need. This is paired with is_divisible. Credits to Daniel Lemire
750  * blog post [1].
751  *
752  * [1]
753  * https://lemire.me/blog/2019/02/08/faster-remainders-when-the-divisor-is-a-constant-beating-compilers-and-libdivide/
754  *
755  * It's UB on zero, but the spec doesn't allow a quad being zero, so we
756  * error out early in case that's the case.
757  */
758  c = precompute_c(quad_width);
759 
760  for (int row = 1; row < quad_height; row++) {
761  while ((q - (row * quad_width)) < quad_width - 1 && q < (quad_height * quad_width)) {
762  q1 = q;
763  q2 = q + 1;
764  context1 = sigma_n[4 * (q1 - quad_width) + 1];
765  context1 += sigma_n[4 * (q1 - quad_width) + 3] << 2; // ne
766 
767  if (!is_divisible(q1, c)) {
768  context1 |= sigma_n[4 * (q1 - quad_width) - 1]; // nw
769  context1 += (sigma_n[4 * q1 - 1] | sigma_n[4 * q1 - 2]) << 1; // sw | q
770  }
771  if (!is_divisible(q1 + 1, c))
772  context1 |= sigma_n[4 * (q1 - quad_width) + 5] << 2;
773 
774  if ((ret = jpeg2000_decode_sig_emb(s, mel_state, mel_stream, vlc_stream,
775  dec_cxt_vlc_table1, Dcup, sig_pat, res_off,
776  emb_pat_k, emb_pat_1, J2K_Q1, context1, Lcup,
777  Pcup))
778  < 0)
779  goto free;
780 
781  for (int i = 0; i < 4; i++)
782  sigma_n[4 * q1 + i] = (sig_pat[J2K_Q1] >> i) & 1;
783 
784  context2 = sigma_n[4 * (q2 - quad_width) + 1];
785  context2 += sigma_n[4 * (q2 - quad_width) + 3] << 2;
786 
787  if (!is_divisible(q2, c)) {
788  context2 |= sigma_n[4 * (q2 - quad_width) - 1];
789  context2 += (sigma_n[4 * q2 - 1] | sigma_n[4 * q2 - 2]) << 1;
790  }
791  if (!is_divisible(q2 + 1, c))
792  context2 |= sigma_n[4 * (q2 - quad_width) + 5] << 2;
793 
794  if ((ret = jpeg2000_decode_sig_emb(s, mel_state, mel_stream, vlc_stream,
795  dec_cxt_vlc_table1, Dcup, sig_pat, res_off,
796  emb_pat_k, emb_pat_1, J2K_Q2, context2, Lcup,
797  Pcup))
798  < 0)
799  goto free;
800 
801  for (int i = 0; i < 4; i++)
802  sigma_n[4 * q2 + i] = (sig_pat[J2K_Q2] >> i) & 1;
803 
804  u[J2K_Q1] = 0;
805  u[J2K_Q2] = 0;
806 
807  jpeg2000_bitbuf_refill_backwards(vlc_stream, vlc_buf);
808 
809  if (res_off[J2K_Q1] == 1 && res_off[J2K_Q2] == 1) {
810  u_pfx[J2K_Q1] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
811  u_pfx[J2K_Q2] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
812 
813  u_sfx[J2K_Q1] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q1], vlc_buf);
814  u_sfx[J2K_Q2] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q2], vlc_buf);
815 
816  u_ext[J2K_Q1] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q1], vlc_buf);
817  u_ext[J2K_Q2] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q2], vlc_buf);
818 
819  u[J2K_Q1] = u_pfx[J2K_Q1] + u_sfx[J2K_Q1] + (u_ext[J2K_Q1] << 2);
820  u[J2K_Q2] = u_pfx[J2K_Q2] + u_sfx[J2K_Q2] + (u_ext[J2K_Q2] << 2);
821 
822  } else if (res_off[J2K_Q1] == 1 || res_off[J2K_Q2] == 1) {
823  uint8_t pos = res_off[J2K_Q1] == 1 ? 0 : 1;
824 
825  u_pfx[pos] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
826  u_sfx[pos] = vlc_decode_u_suffix(vlc_stream, u_pfx[pos], vlc_buf);
827  u_ext[pos] = vlc_decode_u_extension(vlc_stream, u_sfx[pos], vlc_buf);
828 
829  u[pos] = u_pfx[pos] + u_sfx[pos] + (u_ext[pos] << 2);
830  }
831  sp = sig_pat[J2K_Q1];
832 
833  gamma[J2K_Q1] = 1;
834 
835  if (sp == 0 || sp == 1 || sp == 2 || sp == 4 || sp == 8)
836  gamma[J2K_Q1] = 0;
837 
838  sp = sig_pat[J2K_Q2];
839 
840  gamma[J2K_Q2] = 1;
841 
842  if (sp == 0 || sp == 1 || sp == 2 || sp == 4 || sp == 8)
843  gamma[J2K_Q2] = 0;
844 
845  E_n[J2K_Q1] = E[4 * (q1 - quad_width) + 1];
846  E_n[J2K_Q2] = E[4 * (q2 - quad_width) + 1];
847 
848  E_ne[J2K_Q1] = E[4 * (q1 - quad_width) + 3];
849  E_ne[J2K_Q2] = E[4 * (q2 - quad_width) + 3];
850 
851  E_nw[J2K_Q1] = (!is_divisible(q1, c)) * E[FFMAX((4 * (q1 - quad_width) - 1), 0)];
852  E_nw[J2K_Q2] = (!is_divisible(q2, c)) * E[FFMAX((4 * (q2 - quad_width) - 1), 0)];
853 
854  E_nf[J2K_Q1] = (!is_divisible(q1 + 1, c)) * E[4 * (q1 - quad_width) + 5];
855  E_nf[J2K_Q2] = (!is_divisible(q2 + 1, c)) * E[4 * (q2 - quad_width) + 5];
856 
857  max_e[J2K_Q1] = FFMAX(E_nw[J2K_Q1], FFMAX3(E_n[J2K_Q1], E_ne[J2K_Q1], E_nf[J2K_Q1]));
858  max_e[J2K_Q2] = FFMAX(E_nw[J2K_Q2], FFMAX3(E_n[J2K_Q2], E_ne[J2K_Q2], E_nf[J2K_Q2]));
859 
860  kappa[J2K_Q1] = FFMAX(1, gamma[J2K_Q1] * (max_e[J2K_Q1] - 1));
861  kappa[J2K_Q2] = FFMAX(1, gamma[J2K_Q2] * (max_e[J2K_Q2] - 1));
862 
863  U[J2K_Q1] = kappa[J2K_Q1] + u[J2K_Q1];
864  U[J2K_Q2] = kappa[J2K_Q2] + u[J2K_Q2];
865  if (U[J2K_Q1] > maxbp || U[J2K_Q2] > maxbp) {
867  goto free;
868  }
869 
870  for (int i = 0; i < 4; i++) {
871  m[J2K_Q1][i] = sigma_n[4 * q1 + i] * U[J2K_Q1] - ((emb_pat_k[J2K_Q1] >> i) & 1);
872  m[J2K_Q2][i] = sigma_n[4 * q2 + i] * U[J2K_Q2] - ((emb_pat_k[J2K_Q2] >> i) & 1);
873  }
874  recover_mag_sgn(mag_sgn_stream, J2K_Q1, q1, m_n, known_1, emb_pat_1, v, m,
875  E, mu_n, Dcup, Pcup, pLSB);
876 
877  recover_mag_sgn(mag_sgn_stream, J2K_Q2, q2, m_n, known_1, emb_pat_1, v, m,
878  E, mu_n, Dcup, Pcup, pLSB);
879 
880  q += 2; // Move to the next quad pair
881  }
882 
883  if (quad_width % 2 == 1) {
884  q1 = q;
885 
886  /* calculate context for current quad */
887  context1 = sigma_n[4 * (q1 - quad_width) + 1];
888  context1 += (sigma_n[4 * (q1 - quad_width) + 3] << 2);
889 
890  if (!is_divisible(q1, c)) {
891  context1 |= sigma_n[4 * (q1 - quad_width) - 1];
892  context1 += (sigma_n[4 * q1 - 1] | sigma_n[4 * q1 - 2]) << 1;
893  }
894  if (!is_divisible(q1 + 1, c))
895  context1 |= sigma_n[4 * (q1 - quad_width) + 5] << 2;
896 
897  if ((ret = jpeg2000_decode_sig_emb(s, mel_state, mel_stream, vlc_stream,
898  dec_cxt_vlc_table1, Dcup, sig_pat, res_off,
899  emb_pat_k, emb_pat_1, J2K_Q1, context1, Lcup,
900  Pcup)) < 0)
901  goto free;
902 
903  for (int i = 0; i < 4; i++)
904  sigma_n[4 * q1 + i] = (sig_pat[J2K_Q1] >> i) & 1;
905 
906  u[J2K_Q1] = 0;
907 
908  /* Recover mag_sgn value */
909  if (res_off[J2K_Q1] == 1) {
910  u_pfx[J2K_Q1] = vlc_decode_u_prefix(vlc_stream, vlc_buf);
911  u_sfx[J2K_Q1] = vlc_decode_u_suffix(vlc_stream, u_pfx[J2K_Q1], vlc_buf);
912  u_ext[J2K_Q1] = vlc_decode_u_extension(vlc_stream, u_sfx[J2K_Q1], vlc_buf);
913 
914  u[J2K_Q1] = u_pfx[J2K_Q1] + u_sfx[J2K_Q1] + (u_ext[J2K_Q1] << 2);
915  }
916 
917  sp = sig_pat[J2K_Q1];
918 
919  gamma[J2K_Q1] = 1;
920 
921  if (sp == 0 || sp == 1 || sp == 2 || sp == 4 || sp == 8)
922  gamma[J2K_Q1] = 0;
923 
924  E_n[J2K_Q1] = E[4 * (q1 - quad_width) + 1];
925 
926  E_ne[J2K_Q1] = E[4 * (q1 - quad_width) + 3];
927 
928  E_nw[J2K_Q1] = (!is_divisible(q1, c)) * E[FFMAX((4 * (q1 - quad_width) - 1), 0)];
929 
930  E_nf[J2K_Q1] = (!is_divisible(q1 + 1, c)) * E[4 * (q1 - quad_width) + 5];
931 
932  max_e[J2K_Q1] = FFMAX(E_nw[J2K_Q1], FFMAX3(E_n[J2K_Q1], E_ne[J2K_Q1], E_nf[J2K_Q1]));
933 
934  kappa[J2K_Q1] = FFMAX(1, gamma[J2K_Q1] * (max_e[J2K_Q1] - 1));
935 
936  U[J2K_Q1] = kappa[J2K_Q1] + u[J2K_Q1];
937  if (U[J2K_Q1] > maxbp) {
939  goto free;
940  }
941 
942  for (int i = 0; i < 4; i++)
943  m[J2K_Q1][i] = sigma_n[4 * q1 + i] * U[J2K_Q1] - ((emb_pat_k[J2K_Q1] >> i) & 1);
944 
945  recover_mag_sgn(mag_sgn_stream, J2K_Q1, q1, m_n, known_1, emb_pat_1, v, m,
946  E, mu_n, Dcup, Pcup, pLSB);
947  q += 1;
948  }
949  }
950 
951  // convert to raster-scan
952  for (int y = 0; y < quad_height; y++) {
953  for (int x = 0; x < quad_width; x++) {
954  int j1, j2;
955  int x1, x2 , x3;
956 
957  j1 = 2 * y;
958  j2 = 2 * x;
959 
960  sample_buf[j2 + (j1 * width)] = (int32_t)*mu;
961  jpeg2000_modify_state(j1, j2, width, *sigma, block_states);
962  sigma += 1;
963  mu += 1;
964 
965  x1 = y != quad_height - 1 || is_border_y == 0;
966  sample_buf[j2 + ((j1 + 1) * width)] = ((int32_t)*mu) * x1;
967  jpeg2000_modify_state(j1 + 1, j2, width, (*sigma) * x1, block_states);
968  sigma += 1;
969  mu += 1;
970 
971  x2 = x != quad_width - 1 || is_border_x == 0;
972  sample_buf[(j2 + 1) + (j1 * width)] = ((int32_t)*mu) * x2;
973  jpeg2000_modify_state(j1, j2 + 1, width, (*sigma) * x2, block_states);
974  sigma += 1;
975  mu += 1;
976 
977  x3 = x1 | x2;
978  sample_buf[(j2 + 1) + (j1 + 1) * width] = ((int32_t)*mu) * x3;
979  jpeg2000_modify_state(j1 + 1, j2 + 1, width, (*sigma) * x3, block_states);
980  sigma += 1;
981  mu += 1;
982  }
983  }
984  ret = 1;
985 free:
986  av_freep(&sigma_n);
987  av_freep(&E);
988  av_freep(&mu_n);
989  return ret;
990 }
991 
992 static void jpeg2000_calc_mbr(uint8_t *mbr, const uint16_t i, const uint16_t j,
993  const uint32_t mbr_info, uint8_t causal_cond,
994  uint8_t *block_states, int width)
995 {
996  int local_mbr = 0;
997 
998  local_mbr |= jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_SIGMA, block_states);
999  local_mbr |= jpeg2000_get_state(i - 1, j + 0, width, HT_SHIFT_SIGMA, block_states);
1000  local_mbr |= jpeg2000_get_state(i - 1, j + 1, width, HT_SHIFT_SIGMA, block_states);
1001 
1002  local_mbr |= jpeg2000_get_state(i + 0, j - 1, width, HT_SHIFT_SIGMA, block_states);
1003  local_mbr |= jpeg2000_get_state(i + 0, j + 1, width, HT_SHIFT_SIGMA, block_states);
1004 
1005  local_mbr |= jpeg2000_get_state(i + 1, j - 1, width, HT_SHIFT_SIGMA, block_states) * causal_cond;
1006  local_mbr |= jpeg2000_get_state(i + 1, j + 0, width, HT_SHIFT_SIGMA, block_states) * causal_cond;
1007  local_mbr |= jpeg2000_get_state(i + 1, j + 1, width, HT_SHIFT_SIGMA, block_states) * causal_cond;
1008 
1009  local_mbr |= jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_REF, block_states) *
1010  jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_SCAN, block_states);
1011  local_mbr |= jpeg2000_get_state(i - 1, j + 0, width, HT_SHIFT_REF, block_states) *
1012  jpeg2000_get_state(i - 1, j - 1, width, HT_SHIFT_SCAN, block_states);
1013  local_mbr |= jpeg2000_get_state(i - 1, j + 1, width, HT_SHIFT_REF, block_states) *
1014  jpeg2000_get_state(i - 1, j + 1, width, HT_SHIFT_SCAN, block_states);
1015 
1016  local_mbr |= jpeg2000_get_state(i + 0, j - 1, width, HT_SHIFT_REF, block_states) *
1017  jpeg2000_get_state(i + 0, j - 1, width, HT_SHIFT_SCAN, block_states);
1018  local_mbr |= jpeg2000_get_state(i + 0, j + 1, width, HT_SHIFT_REF, block_states) *
1019  jpeg2000_get_state(i + 0, j + 1, width, HT_SHIFT_SCAN, block_states);
1020 
1021  local_mbr |= jpeg2000_get_state(i + 1, j - 1, width, HT_SHIFT_REF, block_states) *
1022  jpeg2000_get_state(i + 1, j - 1, width, HT_SHIFT_SCAN, block_states) * causal_cond;
1023  local_mbr |= jpeg2000_get_state(i + 1, j + 0, width, HT_SHIFT_REF, block_states) *
1024  jpeg2000_get_state(i + 1, j + 0, width, HT_SHIFT_SCAN, block_states) * causal_cond;
1025  local_mbr |= jpeg2000_get_state(i + 1, j + 1, width, HT_SHIFT_REF, block_states) *
1026  jpeg2000_get_state(i + 1, j + 1, width, HT_SHIFT_SCAN, block_states) * causal_cond;
1027 
1028  *mbr |= local_mbr;
1029 }
1030 
1031 static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s,
1032  int width, int height, int stride, int pLSB,
1033  int32_t *sample_buf, uint8_t *block_states,
1034  uint8_t *magref_segment, uint32_t magref_length)
1035 {
1036  for (int j = j_s; j < j_s + width; j++) {
1037  uint32_t mbr_info = 0;
1038  for (int i = i_s; i < i_s + height; i++) {
1039  int modify_state, cond;
1040  uint8_t bit;
1041  uint8_t causal_cond = i != (i_s + height - 1);
1042  int32_t *sp = &sample_buf[j + (i * (stride - 2))];
1043  uint8_t mbr = 0;
1044 
1045  if (jpeg2000_get_state(i, j, stride - 2, HT_SHIFT_SIGMA, block_states) == 0)
1046  jpeg2000_calc_mbr(&mbr, i, j, mbr_info & 0x1EF, causal_cond, block_states, stride - 2);
1047  mbr_info >>= 3;
1048  cond = mbr != 0;
1049  bit = jpeg2000_peek_bit(sig_prop, magref_segment, magref_length);
1050  *sp |= (bit * cond) << pLSB;
1051  sig_prop->bits -= cond;
1052  modify_state = (((1 << HT_SHIFT_REF_IND) | (1 << HT_SHIFT_REF)) * cond) | 1 << HT_SHIFT_SCAN;
1053  jpeg2000_modify_state(i, j, stride - 2, modify_state, block_states);
1054  }
1055  }
1056 }
1057 
1058 /**
1059  * See procedure decodeSigPropMag at Rec. ITU-T T.814, 7.4.
1060 */
1063  uint16_t height, uint8_t *magref_segment,
1064  uint32_t magref_length, uint8_t pLSB,
1065  int32_t *sample_buf, uint8_t *block_states)
1066 {
1067  StateVars sp_dec;
1068 
1069  const uint16_t num_v_stripe = height / 4;
1070  const uint16_t num_h_stripe = width / 4;
1071  int b_width = 4;
1072  int b_height = 4;
1073  int stride = width + 2;
1074 
1075  int last_width;
1076  uint16_t i = 0, j = 0;
1077 
1078  jpeg2000_init_zero(&sp_dec);
1079 
1080  for (int n1 = 0; n1 < num_v_stripe; n1++) {
1081  j = 0;
1082  for (int n2 = 0; n2 < num_h_stripe; n2++) {
1083  jpeg2000_process_stripes_block(&sp_dec, i, j, b_width, b_height, stride,
1084  pLSB, sample_buf, block_states, magref_segment,
1085  magref_length);
1086  j += 4;
1087  }
1088  last_width = width % 4;
1089  if (last_width)
1090  jpeg2000_process_stripes_block(&sp_dec, i, j, last_width, b_height, stride,
1091  pLSB, sample_buf, block_states, magref_segment,
1092  magref_length);
1093  i += 4;
1094  }
1095 
1096  /* Decode remaining height stripes */
1097  b_height = height % 4;
1098  j = 0;
1099  for (int n2 = 0; n2 < num_h_stripe; n2++) {
1100  jpeg2000_process_stripes_block(&sp_dec, i, j, b_width, b_height, stride,
1101  pLSB, sample_buf, block_states, magref_segment,
1102  magref_length);
1103  j += 4;
1104  }
1105  last_width = width % 4;
1106  if (last_width)
1107  jpeg2000_process_stripes_block(&sp_dec, i, j, last_width, b_height, stride,
1108  pLSB, sample_buf, block_states, magref_segment,
1109  magref_length);
1110 }
1111 
1112 /**
1113  * See procedure decodeSigPropMag at Rec. ITU-T T.814, 7.5.
1114 */
1115 static int
1116 jpeg2000_decode_magref_segment( uint16_t width, uint16_t block_height,
1117  uint8_t *magref_segment,uint32_t magref_length,
1118  uint8_t pLSB, int32_t *sample_buf, uint8_t *block_states)
1119 {
1120 
1121  StateVars mag_ref = { 0 };
1122  const uint16_t num_v_stripe = block_height / 4;
1123  uint16_t height = 4;
1124  uint16_t i_start = 0;
1125  int32_t *sp;
1126 
1127  jpeg2000_init_mag_ref(&mag_ref, magref_length);
1128 
1129  for (int n1 = 0; n1 < num_v_stripe; n1++) {
1130  for (int j = 0; j < width; j++) {
1131  for (int i = i_start; i < i_start + height; i++) {
1132  /**
1133  * We move column wise, going from one quad to another. See
1134  * Rec. ITU-T T.814, Figure 7.
1135  */
1136  sp = &sample_buf[j + i * width];
1137  if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) {
1138  jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states);
1139  *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB;
1140  }
1141  }
1142  }
1143  i_start += 4;
1144  }
1145  height = block_height % 4;
1146  for (int j = 0; j < width; j++) {
1147  for (int i = i_start; i < i_start + height; i++) {
1148  sp = &sample_buf[j + i * width];
1149  if (jpeg2000_get_state(i, j, width, HT_SHIFT_SIGMA, block_states) != 0) {
1150  jpeg2000_modify_state(i, j, width, 1 << HT_SHIFT_REF_IND, block_states);
1151  *sp |= jpeg2000_import_magref_bit(&mag_ref, magref_segment, magref_length) << pLSB;
1152  }
1153  }
1154  }
1155  return 1;
1156 }
1157 
1158 
1159 int
1161  int width, int height, int magp, uint8_t roi_shift)
1162 {
1163  uint8_t p0 = 0; // Number of placeholder passes
1164  uint32_t Lcup; // Length of HT cleanup segment
1165  uint32_t Lref; // Length of Refinement segment
1166  uint32_t Scup; // HT cleanup segment suffix length
1167  uint32_t Pcup; // HT cleanup segment prefix length
1168 
1169  uint8_t S_blk; // Number of skipped magnitude bitplanes
1170  uint8_t pLSB;
1171 
1172  uint8_t *Dcup; // Byte of an HT cleanup segment
1173  uint8_t *Dref; // Byte of an HT refinement segment
1174 
1175  int z_blk; // Number of ht coding pass
1176 
1177  uint8_t empty_passes;
1178 
1179  StateVars mag_sgn; // Magnitude and Sign
1180  StateVars mel; // Adaptive run-length coding
1181  StateVars vlc; // Variable Length coding
1182  StateVars sig_prop; // Significance propagation
1183 
1184  MelDecoderState mel_state;
1185 
1186  int ret;
1187 
1188  /* Temporary buffers */
1189  int32_t *sample_buf = NULL;
1190  uint8_t *block_states = NULL;
1191 
1192  int32_t n, val; // Post-processing
1193 
1194  int32_t M_b = magp;
1195 
1196  /* codeblock size as constrained by Rec. ITU-T T.800, Table A.18 */
1197  av_assert0(width <= 1024U && height <= 1024U);
1198  av_assert0(width * height <= 4096);
1199  av_assert0(width * height > 0);
1200 
1201  memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1202  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1203 
1204  if (cblk->npasses == 0)
1205  return 0;
1206 
1207  if (cblk->npasses > 3)
1208  p0 = 0;
1209  else if (cblk->length == 0)
1210  p0 = 1;
1211 
1212  empty_passes = p0 * 3;
1213  z_blk = cblk->npasses - empty_passes;
1214 
1215  if (z_blk <= 0)
1216  return 0; // No passes within this set, continue
1217 
1218  Lcup = cblk->pass_lengths[0];
1219  Lref = cblk->pass_lengths[1];
1220 
1221  if (Lcup < 2) {
1222  av_log(s->avctx, AV_LOG_ERROR,
1223  "Cleanup pass length must be at least 2 bytes in length\n");
1224  return AVERROR_INVALIDDATA;
1225  }
1226  Dcup = cblk->data;
1227  Dref = cblk->data + Lcup; // Dref comes after the refinement segment
1228  S_blk = p0 + cblk->zbp;
1229  pLSB = 30 - S_blk;
1230 
1231  Scup = (Dcup[Lcup - 1] << 4) + (Dcup[Lcup - 2] & 0x0F);
1232 
1233  if (Scup < 2 || Scup > Lcup || Scup > 4079) {
1234  av_log(s->avctx, AV_LOG_ERROR, "Cleanup pass suffix length is invalid %d\n",
1235  Scup);
1237  goto free;
1238  }
1239  Pcup = Lcup - Scup;
1240 
1241  /* modDcup shall be done before the creation of vlc instance. */
1242  Dcup[Lcup - 1] = 0xFF;
1243  Dcup[Lcup - 2] |= 0x0F;
1244 
1245  /* Magnitude and refinement */
1246  jpeg2000_init_zero(&mag_sgn);
1247  jpeg2000_bitbuf_refill_forward(&mag_sgn, Dcup, Pcup);
1248 
1249  /* Significance propagation */
1250  jpeg2000_init_zero(&sig_prop);
1251 
1252  /* Adaptive run length */
1253  jpeg2000_init_mel(&mel, Pcup);
1254 
1255  /* Variable Length coding */
1256  jpeg2000_init_vlc(&vlc, Lcup, Pcup, Dcup);
1257 
1258  jpeg2000_init_mel_decoder(&mel_state);
1259 
1260  sample_buf = av_calloc((width + 4) * (height + 4), sizeof(int32_t));
1261  block_states = av_calloc((width + 4) * (height + 4), sizeof(uint8_t));
1262 
1263  if (!sample_buf || !block_states) {
1264  ret = AVERROR(ENOMEM);
1265  goto free;
1266  }
1267  if ((ret = jpeg2000_decode_ht_cleanup_segment(s, cblk, t1, &mel_state, &mel, &vlc,
1268  &mag_sgn, Dcup, Lcup, Pcup, pLSB, width,
1269  height, sample_buf, block_states)) < 0) {
1270  av_log(s->avctx, AV_LOG_ERROR, "Bad HT cleanup segment\n");
1271  goto free;
1272  }
1273 
1274  if (cblk->npasses > 1)
1275  jpeg2000_decode_sigprop_segment(cblk, width, height, Dref, Lref,
1276  pLSB - 1, sample_buf, block_states);
1277 
1278  if (cblk->npasses > 2) {
1279 
1280  if (Lref < 2){
1281  av_log(s->avctx,AV_LOG_ERROR,"Invalid magnitude refinement length\n");
1283  goto free;
1284  }
1285  if ((ret = jpeg2000_decode_magref_segment(width, height, Dref, Lref,
1286  pLSB - 1, sample_buf, block_states)) < 0)
1287  goto free;
1288  }
1289 
1290  pLSB = 31 - M_b;
1291 
1292  /* Reconstruct the sample values */
1293  for (int y = 0; y < height; y++) {
1294  for (int x = 0; x < width; x++) {
1295  n = x + (y * t1->stride);
1296  val = sample_buf[x + (y * width)];
1297  /* Convert sign-magnitude to two's complement. */
1298  val = val >> 31 ? 0x80000000 - val : val;
1299  val >>= (pLSB - 1);
1300  t1->data[n] = val;
1301  }
1302  }
1303 free:
1304  av_freep(&sample_buf);
1305  av_freep(&block_states);
1306  return ret;
1307 }
1308 
1309 /**
1310  * CtxVLC tables (see Rec. ITU-T T.800, Annex C) as found at
1311  * https://github.com/osamu620/OpenHTJ2K (author: Osamu Watanabe)
1312  */
1313 static const uint16_t dec_cxt_vlc_table1[1024] = {
1314  0x0016, 0x006A, 0x0046, 0x00DD, 0x0086, 0x888B, 0x0026, 0x444D, 0x0016, 0x00AA, 0x0046, 0x88AD, 0x0086,
1315  0x003A, 0x0026, 0x00DE, 0x0016, 0x00CA, 0x0046, 0x009D, 0x0086, 0x005A, 0x0026, 0x222D, 0x0016, 0x009A,
1316  0x0046, 0x007D, 0x0086, 0x01FD, 0x0026, 0x007E, 0x0016, 0x006A, 0x0046, 0x88CD, 0x0086, 0x888B, 0x0026,
1317  0x111D, 0x0016, 0x00AA, 0x0046, 0x005D, 0x0086, 0x003A, 0x0026, 0x00EE, 0x0016, 0x00CA, 0x0046, 0x00BD,
1318  0x0086, 0x005A, 0x0026, 0x11FF, 0x0016, 0x009A, 0x0046, 0x003D, 0x0086, 0x04ED, 0x0026, 0x2AAF, 0x0016,
1319  0x006A, 0x0046, 0x00DD, 0x0086, 0x888B, 0x0026, 0x444D, 0x0016, 0x00AA, 0x0046, 0x88AD, 0x0086, 0x003A,
1320  0x0026, 0x44EF, 0x0016, 0x00CA, 0x0046, 0x009D, 0x0086, 0x005A, 0x0026, 0x222D, 0x0016, 0x009A, 0x0046,
1321  0x007D, 0x0086, 0x01FD, 0x0026, 0x00BE, 0x0016, 0x006A, 0x0046, 0x88CD, 0x0086, 0x888B, 0x0026, 0x111D,
1322  0x0016, 0x00AA, 0x0046, 0x005D, 0x0086, 0x003A, 0x0026, 0x4CCF, 0x0016, 0x00CA, 0x0046, 0x00BD, 0x0086,
1323  0x005A, 0x0026, 0x00FE, 0x0016, 0x009A, 0x0046, 0x003D, 0x0086, 0x04ED, 0x0026, 0x006F, 0x0002, 0x0088,
1324  0x0002, 0x005C, 0x0002, 0x0018, 0x0002, 0x00DE, 0x0002, 0x0028, 0x0002, 0x009C, 0x0002, 0x004A, 0x0002,
1325  0x007E, 0x0002, 0x0088, 0x0002, 0x00CC, 0x0002, 0x0018, 0x0002, 0x888F, 0x0002, 0x0028, 0x0002, 0x00FE,
1326  0x0002, 0x003A, 0x0002, 0x222F, 0x0002, 0x0088, 0x0002, 0x04FD, 0x0002, 0x0018, 0x0002, 0x00BE, 0x0002,
1327  0x0028, 0x0002, 0x00BF, 0x0002, 0x004A, 0x0002, 0x006E, 0x0002, 0x0088, 0x0002, 0x00AC, 0x0002, 0x0018,
1328  0x0002, 0x444F, 0x0002, 0x0028, 0x0002, 0x00EE, 0x0002, 0x003A, 0x0002, 0x113F, 0x0002, 0x0088, 0x0002,
1329  0x005C, 0x0002, 0x0018, 0x0002, 0x00CF, 0x0002, 0x0028, 0x0002, 0x009C, 0x0002, 0x004A, 0x0002, 0x006F,
1330  0x0002, 0x0088, 0x0002, 0x00CC, 0x0002, 0x0018, 0x0002, 0x009F, 0x0002, 0x0028, 0x0002, 0x00EF, 0x0002,
1331  0x003A, 0x0002, 0x233F, 0x0002, 0x0088, 0x0002, 0x04FD, 0x0002, 0x0018, 0x0002, 0x00AF, 0x0002, 0x0028,
1332  0x0002, 0x44FF, 0x0002, 0x004A, 0x0002, 0x005F, 0x0002, 0x0088, 0x0002, 0x00AC, 0x0002, 0x0018, 0x0002,
1333  0x007F, 0x0002, 0x0028, 0x0002, 0x00DF, 0x0002, 0x003A, 0x0002, 0x111F, 0x0002, 0x0028, 0x0002, 0x005C,
1334  0x0002, 0x008A, 0x0002, 0x00BF, 0x0002, 0x0018, 0x0002, 0x00FE, 0x0002, 0x00CC, 0x0002, 0x007E, 0x0002,
1335  0x0028, 0x0002, 0x8FFF, 0x0002, 0x004A, 0x0002, 0x007F, 0x0002, 0x0018, 0x0002, 0x00DF, 0x0002, 0x00AC,
1336  0x0002, 0x133F, 0x0002, 0x0028, 0x0002, 0x222D, 0x0002, 0x008A, 0x0002, 0x00BE, 0x0002, 0x0018, 0x0002,
1337  0x44EF, 0x0002, 0x2AAD, 0x0002, 0x006E, 0x0002, 0x0028, 0x0002, 0x15FF, 0x0002, 0x004A, 0x0002, 0x009E,
1338  0x0002, 0x0018, 0x0002, 0x00CF, 0x0002, 0x003C, 0x0002, 0x223F, 0x0002, 0x0028, 0x0002, 0x005C, 0x0002,
1339  0x008A, 0x0002, 0x2BBF, 0x0002, 0x0018, 0x0002, 0x04EF, 0x0002, 0x00CC, 0x0002, 0x006F, 0x0002, 0x0028,
1340  0x0002, 0x27FF, 0x0002, 0x004A, 0x0002, 0x009F, 0x0002, 0x0018, 0x0002, 0x00DE, 0x0002, 0x00AC, 0x0002,
1341  0x444F, 0x0002, 0x0028, 0x0002, 0x222D, 0x0002, 0x008A, 0x0002, 0x8AAF, 0x0002, 0x0018, 0x0002, 0x00EE,
1342  0x0002, 0x2AAD, 0x0002, 0x005F, 0x0002, 0x0028, 0x0002, 0x44FF, 0x0002, 0x004A, 0x0002, 0x888F, 0x0002,
1343  0x0018, 0x0002, 0xAAAF, 0x0002, 0x003C, 0x0002, 0x111F, 0x0004, 0x8FFD, 0x0028, 0x005C, 0x0004, 0x00BC,
1344  0x008A, 0x66FF, 0x0004, 0x00CD, 0x0018, 0x111D, 0x0004, 0x009C, 0x003A, 0x8AAF, 0x0004, 0x00FC, 0x0028,
1345  0x133D, 0x0004, 0x00AC, 0x004A, 0x3BBF, 0x0004, 0x2BBD, 0x0018, 0x5FFF, 0x0004, 0x006C, 0x157D, 0x455F,
1346  0x0004, 0x2FFD, 0x0028, 0x222D, 0x0004, 0x22AD, 0x008A, 0x44EF, 0x0004, 0x00CC, 0x0018, 0x4FFF, 0x0004,
1347  0x007C, 0x003A, 0x447F, 0x0004, 0x04DD, 0x0028, 0x233D, 0x0004, 0x009D, 0x004A, 0x00DE, 0x0004, 0x88BD,
1348  0x0018, 0xAFFF, 0x0004, 0x115D, 0x1FFD, 0x444F, 0x0004, 0x8FFD, 0x0028, 0x005C, 0x0004, 0x00BC, 0x008A,
1349  0x8CEF, 0x0004, 0x00CD, 0x0018, 0x111D, 0x0004, 0x009C, 0x003A, 0x888F, 0x0004, 0x00FC, 0x0028, 0x133D,
1350  0x0004, 0x00AC, 0x004A, 0x44DF, 0x0004, 0x2BBD, 0x0018, 0x8AFF, 0x0004, 0x006C, 0x157D, 0x006F, 0x0004,
1351  0x2FFD, 0x0028, 0x222D, 0x0004, 0x22AD, 0x008A, 0x00EE, 0x0004, 0x00CC, 0x0018, 0x2EEF, 0x0004, 0x007C,
1352  0x003A, 0x277F, 0x0004, 0x04DD, 0x0028, 0x233D, 0x0004, 0x009D, 0x004A, 0x1BBF, 0x0004, 0x88BD, 0x0018,
1353  0x37FF, 0x0004, 0x115D, 0x1FFD, 0x333F, 0x0002, 0x0088, 0x0002, 0x02ED, 0x0002, 0x00CA, 0x0002, 0x4CCF,
1354  0x0002, 0x0048, 0x0002, 0x23FF, 0x0002, 0x001A, 0x0002, 0x888F, 0x0002, 0x0088, 0x0002, 0x006C, 0x0002,
1355  0x002A, 0x0002, 0x00AF, 0x0002, 0x0048, 0x0002, 0x22EF, 0x0002, 0x00AC, 0x0002, 0x005F, 0x0002, 0x0088,
1356  0x0002, 0x444D, 0x0002, 0x00CA, 0x0002, 0xCCCF, 0x0002, 0x0048, 0x0002, 0x00FE, 0x0002, 0x001A, 0x0002,
1357  0x006F, 0x0002, 0x0088, 0x0002, 0x005C, 0x0002, 0x002A, 0x0002, 0x009F, 0x0002, 0x0048, 0x0002, 0x00DF,
1358  0x0002, 0x03FD, 0x0002, 0x222F, 0x0002, 0x0088, 0x0002, 0x02ED, 0x0002, 0x00CA, 0x0002, 0x8CCF, 0x0002,
1359  0x0048, 0x0002, 0x11FF, 0x0002, 0x001A, 0x0002, 0x007E, 0x0002, 0x0088, 0x0002, 0x006C, 0x0002, 0x002A,
1360  0x0002, 0x007F, 0x0002, 0x0048, 0x0002, 0x00EE, 0x0002, 0x00AC, 0x0002, 0x003E, 0x0002, 0x0088, 0x0002,
1361  0x444D, 0x0002, 0x00CA, 0x0002, 0x00BE, 0x0002, 0x0048, 0x0002, 0x00BF, 0x0002, 0x001A, 0x0002, 0x003F,
1362  0x0002, 0x0088, 0x0002, 0x005C, 0x0002, 0x002A, 0x0002, 0x009E, 0x0002, 0x0048, 0x0002, 0x00DE, 0x0002,
1363  0x03FD, 0x0002, 0x111F, 0x0004, 0x8AED, 0x0048, 0x888D, 0x0004, 0x00DC, 0x00CA, 0x3FFF, 0x0004, 0xCFFD,
1364  0x002A, 0x003D, 0x0004, 0x00BC, 0x005A, 0x8DDF, 0x0004, 0x8FFD, 0x0048, 0x006C, 0x0004, 0x027D, 0x008A,
1365  0x99FF, 0x0004, 0x00EC, 0x00FA, 0x003C, 0x0004, 0x00AC, 0x001A, 0x009F, 0x0004, 0x2FFD, 0x0048, 0x007C,
1366  0x0004, 0x44CD, 0x00CA, 0x67FF, 0x0004, 0x1FFD, 0x002A, 0x444D, 0x0004, 0x00AD, 0x005A, 0x8CCF, 0x0004,
1367  0x4FFD, 0x0048, 0x445D, 0x0004, 0x01BD, 0x008A, 0x4EEF, 0x0004, 0x45DD, 0x00FA, 0x111D, 0x0004, 0x009C,
1368  0x001A, 0x222F, 0x0004, 0x8AED, 0x0048, 0x888D, 0x0004, 0x00DC, 0x00CA, 0xAFFF, 0x0004, 0xCFFD, 0x002A,
1369  0x003D, 0x0004, 0x00BC, 0x005A, 0x11BF, 0x0004, 0x8FFD, 0x0048, 0x006C, 0x0004, 0x027D, 0x008A, 0x22EF,
1370  0x0004, 0x00EC, 0x00FA, 0x003C, 0x0004, 0x00AC, 0x001A, 0x227F, 0x0004, 0x2FFD, 0x0048, 0x007C, 0x0004,
1371  0x44CD, 0x00CA, 0x5DFF, 0x0004, 0x1FFD, 0x002A, 0x444D, 0x0004, 0x00AD, 0x005A, 0x006F, 0x0004, 0x4FFD,
1372  0x0048, 0x445D, 0x0004, 0x01BD, 0x008A, 0x11DF, 0x0004, 0x45DD, 0x00FA, 0x111D, 0x0004, 0x009C, 0x001A,
1373  0x155F, 0x0006, 0x00FC, 0x0018, 0x111D, 0x0048, 0x888D, 0x00AA, 0x4DDF, 0x0006, 0x2AAD, 0x005A, 0x67FF,
1374  0x0028, 0x223D, 0x00BC, 0xAAAF, 0x0006, 0x00EC, 0x0018, 0x5FFF, 0x0048, 0x006C, 0x008A, 0xCCCF, 0x0006,
1375  0x009D, 0x00CA, 0x44EF, 0x0028, 0x003C, 0x8FFD, 0x137F, 0x0006, 0x8EED, 0x0018, 0x1FFF, 0x0048, 0x007C,
1376  0x00AA, 0x4CCF, 0x0006, 0x227D, 0x005A, 0x1DDF, 0x0028, 0x444D, 0x4FFD, 0x155F, 0x0006, 0x00DC, 0x0018,
1377  0x2EEF, 0x0048, 0x445D, 0x008A, 0x22BF, 0x0006, 0x009C, 0x00CA, 0x8CDF, 0x0028, 0x222D, 0x2FFD, 0x226F,
1378  0x0006, 0x00FC, 0x0018, 0x111D, 0x0048, 0x888D, 0x00AA, 0x1BBF, 0x0006, 0x2AAD, 0x005A, 0x33FF, 0x0028,
1379  0x223D, 0x00BC, 0x8AAF, 0x0006, 0x00EC, 0x0018, 0x9BFF, 0x0048, 0x006C, 0x008A, 0x8ABF, 0x0006, 0x009D,
1380  0x00CA, 0x4EEF, 0x0028, 0x003C, 0x8FFD, 0x466F, 0x0006, 0x8EED, 0x0018, 0xCFFF, 0x0048, 0x007C, 0x00AA,
1381  0x8CCF, 0x0006, 0x227D, 0x005A, 0xAEEF, 0x0028, 0x444D, 0x4FFD, 0x477F, 0x0006, 0x00DC, 0x0018, 0xAFFF,
1382  0x0048, 0x445D, 0x008A, 0x2BBF, 0x0006, 0x009C, 0x00CA, 0x44DF, 0x0028, 0x222D, 0x2FFD, 0x133F, 0x00F6,
1383  0xAFFD, 0x1FFB, 0x003C, 0x0008, 0x23BD, 0x007A, 0x11DF, 0x00F6, 0x45DD, 0x2FFB, 0x4EEF, 0x00DA, 0x177D,
1384  0xCFFD, 0x377F, 0x00F6, 0x3FFD, 0x8FFB, 0x111D, 0x0008, 0x009C, 0x005A, 0x1BBF, 0x00F6, 0x00CD, 0x00BA,
1385  0x8DDF, 0x4FFB, 0x006C, 0x9BFD, 0x455F, 0x00F6, 0x67FD, 0x1FFB, 0x002C, 0x0008, 0x00AC, 0x007A, 0x009F,
1386  0x00F6, 0x00AD, 0x2FFB, 0x7FFF, 0x00DA, 0x004C, 0x5FFD, 0x477F, 0x00F6, 0x00EC, 0x8FFB, 0x001C, 0x0008,
1387  0x008C, 0x005A, 0x888F, 0x00F6, 0x00CC, 0x00BA, 0x2EEF, 0x4FFB, 0x115D, 0x8AED, 0x113F, 0x00F6, 0xAFFD,
1388  0x1FFB, 0x003C, 0x0008, 0x23BD, 0x007A, 0x1DDF, 0x00F6, 0x45DD, 0x2FFB, 0xBFFF, 0x00DA, 0x177D, 0xCFFD,
1389  0x447F, 0x00F6, 0x3FFD, 0x8FFB, 0x111D, 0x0008, 0x009C, 0x005A, 0x277F, 0x00F6, 0x00CD, 0x00BA, 0x22EF,
1390  0x4FFB, 0x006C, 0x9BFD, 0x444F, 0x00F6, 0x67FD, 0x1FFB, 0x002C, 0x0008, 0x00AC, 0x007A, 0x11BF, 0x00F6,
1391  0x00AD, 0x2FFB, 0xFFFF, 0x00DA, 0x004C, 0x5FFD, 0x233F, 0x00F6, 0x00EC, 0x8FFB, 0x001C, 0x0008, 0x008C,
1392  0x005A, 0x006F, 0x00F6, 0x00CC, 0x00BA, 0x8BBF, 0x4FFB, 0x115D, 0x8AED, 0x222F};
1393 
1394 static const uint16_t dec_cxt_vlc_table0[1024] = {
1395  0x0026, 0x00AA, 0x0046, 0x006C, 0x0086, 0x8AED, 0x0018, 0x8DDF, 0x0026, 0x01BD, 0x0046, 0x5FFF, 0x0086,
1396  0x027D, 0x005A, 0x155F, 0x0026, 0x003A, 0x0046, 0x444D, 0x0086, 0x4CCD, 0x0018, 0xCCCF, 0x0026, 0x2EFD,
1397  0x0046, 0x99FF, 0x0086, 0x009C, 0x00CA, 0x133F, 0x0026, 0x00AA, 0x0046, 0x445D, 0x0086, 0x8CCD, 0x0018,
1398  0x11DF, 0x0026, 0x4FFD, 0x0046, 0xCFFF, 0x0086, 0x009D, 0x005A, 0x007E, 0x0026, 0x003A, 0x0046, 0x1FFF,
1399  0x0086, 0x88AD, 0x0018, 0x00BE, 0x0026, 0x8FFD, 0x0046, 0x4EEF, 0x0086, 0x888D, 0x00CA, 0x111F, 0x0026,
1400  0x00AA, 0x0046, 0x006C, 0x0086, 0x8AED, 0x0018, 0x45DF, 0x0026, 0x01BD, 0x0046, 0x22EF, 0x0086, 0x027D,
1401  0x005A, 0x227F, 0x0026, 0x003A, 0x0046, 0x444D, 0x0086, 0x4CCD, 0x0018, 0x11BF, 0x0026, 0x2EFD, 0x0046,
1402  0x00FE, 0x0086, 0x009C, 0x00CA, 0x223F, 0x0026, 0x00AA, 0x0046, 0x445D, 0x0086, 0x8CCD, 0x0018, 0x00DE,
1403  0x0026, 0x4FFD, 0x0046, 0xABFF, 0x0086, 0x009D, 0x005A, 0x006F, 0x0026, 0x003A, 0x0046, 0x6EFF, 0x0086,
1404  0x88AD, 0x0018, 0x2AAF, 0x0026, 0x8FFD, 0x0046, 0x00EE, 0x0086, 0x888D, 0x00CA, 0x222F, 0x0004, 0x00CA,
1405  0x0088, 0x027D, 0x0004, 0x4CCD, 0x0028, 0x00FE, 0x0004, 0x2AFD, 0x0048, 0x005C, 0x0004, 0x009D, 0x0018,
1406  0x00DE, 0x0004, 0x01BD, 0x0088, 0x006C, 0x0004, 0x88AD, 0x0028, 0x11DF, 0x0004, 0x8AED, 0x0048, 0x003C,
1407  0x0004, 0x888D, 0x0018, 0x111F, 0x0004, 0x00CA, 0x0088, 0x006D, 0x0004, 0x88CD, 0x0028, 0x88FF, 0x0004,
1408  0x8BFD, 0x0048, 0x444D, 0x0004, 0x009C, 0x0018, 0x00BE, 0x0004, 0x4EFD, 0x0088, 0x445D, 0x0004, 0x00AC,
1409  0x0028, 0x00EE, 0x0004, 0x45DD, 0x0048, 0x222D, 0x0004, 0x003D, 0x0018, 0x007E, 0x0004, 0x00CA, 0x0088,
1410  0x027D, 0x0004, 0x4CCD, 0x0028, 0x1FFF, 0x0004, 0x2AFD, 0x0048, 0x005C, 0x0004, 0x009D, 0x0018, 0x11BF,
1411  0x0004, 0x01BD, 0x0088, 0x006C, 0x0004, 0x88AD, 0x0028, 0x22EF, 0x0004, 0x8AED, 0x0048, 0x003C, 0x0004,
1412  0x888D, 0x0018, 0x227F, 0x0004, 0x00CA, 0x0088, 0x006D, 0x0004, 0x88CD, 0x0028, 0x4EEF, 0x0004, 0x8BFD,
1413  0x0048, 0x444D, 0x0004, 0x009C, 0x0018, 0x2AAF, 0x0004, 0x4EFD, 0x0088, 0x445D, 0x0004, 0x00AC, 0x0028,
1414  0x8DDF, 0x0004, 0x45DD, 0x0048, 0x222D, 0x0004, 0x003D, 0x0018, 0x155F, 0x0004, 0x005A, 0x0088, 0x006C,
1415  0x0004, 0x88DD, 0x0028, 0x23FF, 0x0004, 0x11FD, 0x0048, 0x444D, 0x0004, 0x00AD, 0x0018, 0x00BE, 0x0004,
1416  0x137D, 0x0088, 0x155D, 0x0004, 0x00CC, 0x0028, 0x00DE, 0x0004, 0x02ED, 0x0048, 0x111D, 0x0004, 0x009D,
1417  0x0018, 0x007E, 0x0004, 0x005A, 0x0088, 0x455D, 0x0004, 0x44CD, 0x0028, 0x00EE, 0x0004, 0x1FFD, 0x0048,
1418  0x003C, 0x0004, 0x00AC, 0x0018, 0x555F, 0x0004, 0x47FD, 0x0088, 0x113D, 0x0004, 0x02BD, 0x0028, 0x477F,
1419  0x0004, 0x4CDD, 0x0048, 0x8FFF, 0x0004, 0x009C, 0x0018, 0x222F, 0x0004, 0x005A, 0x0088, 0x006C, 0x0004,
1420  0x88DD, 0x0028, 0x00FE, 0x0004, 0x11FD, 0x0048, 0x444D, 0x0004, 0x00AD, 0x0018, 0x888F, 0x0004, 0x137D,
1421  0x0088, 0x155D, 0x0004, 0x00CC, 0x0028, 0x8CCF, 0x0004, 0x02ED, 0x0048, 0x111D, 0x0004, 0x009D, 0x0018,
1422  0x006F, 0x0004, 0x005A, 0x0088, 0x455D, 0x0004, 0x44CD, 0x0028, 0x1DDF, 0x0004, 0x1FFD, 0x0048, 0x003C,
1423  0x0004, 0x00AC, 0x0018, 0x227F, 0x0004, 0x47FD, 0x0088, 0x113D, 0x0004, 0x02BD, 0x0028, 0x22BF, 0x0004,
1424  0x4CDD, 0x0048, 0x22EF, 0x0004, 0x009C, 0x0018, 0x233F, 0x0006, 0x4DDD, 0x4FFB, 0xCFFF, 0x0018, 0x113D,
1425  0x005A, 0x888F, 0x0006, 0x23BD, 0x008A, 0x00EE, 0x002A, 0x155D, 0xAAFD, 0x277F, 0x0006, 0x44CD, 0x8FFB,
1426  0x44EF, 0x0018, 0x467D, 0x004A, 0x2AAF, 0x0006, 0x00AC, 0x555B, 0x99DF, 0x1FFB, 0x003C, 0x5FFD, 0x266F,
1427  0x0006, 0x1DDD, 0x4FFB, 0x6EFF, 0x0018, 0x177D, 0x005A, 0x1BBF, 0x0006, 0x88AD, 0x008A, 0x5DDF, 0x002A,
1428  0x444D, 0x2FFD, 0x667F, 0x0006, 0x00CC, 0x8FFB, 0x2EEF, 0x0018, 0x455D, 0x004A, 0x119F, 0x0006, 0x009C,
1429  0x555B, 0x8CCF, 0x1FFB, 0x111D, 0x8CED, 0x006E, 0x0006, 0x4DDD, 0x4FFB, 0x3FFF, 0x0018, 0x113D, 0x005A,
1430  0x11BF, 0x0006, 0x23BD, 0x008A, 0x8DDF, 0x002A, 0x155D, 0xAAFD, 0x222F, 0x0006, 0x44CD, 0x8FFB, 0x00FE,
1431  0x0018, 0x467D, 0x004A, 0x899F, 0x0006, 0x00AC, 0x555B, 0x00DE, 0x1FFB, 0x003C, 0x5FFD, 0x446F, 0x0006,
1432  0x1DDD, 0x4FFB, 0x9BFF, 0x0018, 0x177D, 0x005A, 0x00BE, 0x0006, 0x88AD, 0x008A, 0xCDDF, 0x002A, 0x444D,
1433  0x2FFD, 0x007E, 0x0006, 0x00CC, 0x8FFB, 0x4EEF, 0x0018, 0x455D, 0x004A, 0x377F, 0x0006, 0x009C, 0x555B,
1434  0x8BBF, 0x1FFB, 0x111D, 0x8CED, 0x233F, 0x0004, 0x00AA, 0x0088, 0x047D, 0x0004, 0x01DD, 0x0028, 0x11DF,
1435  0x0004, 0x27FD, 0x0048, 0x005C, 0x0004, 0x8AAD, 0x0018, 0x2BBF, 0x0004, 0x009C, 0x0088, 0x006C, 0x0004,
1436  0x00CC, 0x0028, 0x00EE, 0x0004, 0x8CED, 0x0048, 0x222D, 0x0004, 0x888D, 0x0018, 0x007E, 0x0004, 0x00AA,
1437  0x0088, 0x006D, 0x0004, 0x88CD, 0x0028, 0x00FE, 0x0004, 0x19FD, 0x0048, 0x003C, 0x0004, 0x2AAD, 0x0018,
1438  0xAAAF, 0x0004, 0x8BFD, 0x0088, 0x005D, 0x0004, 0x00BD, 0x0028, 0x4CCF, 0x0004, 0x44ED, 0x0048, 0x4FFF,
1439  0x0004, 0x223D, 0x0018, 0x111F, 0x0004, 0x00AA, 0x0088, 0x047D, 0x0004, 0x01DD, 0x0028, 0x99FF, 0x0004,
1440  0x27FD, 0x0048, 0x005C, 0x0004, 0x8AAD, 0x0018, 0x00BE, 0x0004, 0x009C, 0x0088, 0x006C, 0x0004, 0x00CC,
1441  0x0028, 0x00DE, 0x0004, 0x8CED, 0x0048, 0x222D, 0x0004, 0x888D, 0x0018, 0x444F, 0x0004, 0x00AA, 0x0088,
1442  0x006D, 0x0004, 0x88CD, 0x0028, 0x2EEF, 0x0004, 0x19FD, 0x0048, 0x003C, 0x0004, 0x2AAD, 0x0018, 0x447F,
1443  0x0004, 0x8BFD, 0x0088, 0x005D, 0x0004, 0x00BD, 0x0028, 0x009F, 0x0004, 0x44ED, 0x0048, 0x67FF, 0x0004,
1444  0x223D, 0x0018, 0x133F, 0x0006, 0x00CC, 0x008A, 0x9DFF, 0x2FFB, 0x467D, 0x1FFD, 0x99BF, 0x0006, 0x2AAD,
1445  0x002A, 0x66EF, 0x4FFB, 0x005C, 0x2EED, 0x377F, 0x0006, 0x89BD, 0x004A, 0x00FE, 0x8FFB, 0x006C, 0x67FD,
1446  0x889F, 0x0006, 0x888D, 0x001A, 0x5DDF, 0x00AA, 0x222D, 0x89DD, 0x444F, 0x0006, 0x2BBD, 0x008A, 0xCFFF,
1447  0x2FFB, 0x226D, 0x009C, 0x00BE, 0x0006, 0xAAAD, 0x002A, 0x1DDF, 0x4FFB, 0x003C, 0x4DDD, 0x466F, 0x0006,
1448  0x8AAD, 0x004A, 0xAEEF, 0x8FFB, 0x445D, 0x8EED, 0x177F, 0x0006, 0x233D, 0x001A, 0x4CCF, 0x00AA, 0xAFFF,
1449  0x88CD, 0x133F, 0x0006, 0x00CC, 0x008A, 0x77FF, 0x2FFB, 0x467D, 0x1FFD, 0x3BBF, 0x0006, 0x2AAD, 0x002A,
1450  0x00EE, 0x4FFB, 0x005C, 0x2EED, 0x007E, 0x0006, 0x89BD, 0x004A, 0x4EEF, 0x8FFB, 0x006C, 0x67FD, 0x667F,
1451  0x0006, 0x888D, 0x001A, 0x00DE, 0x00AA, 0x222D, 0x89DD, 0x333F, 0x0006, 0x2BBD, 0x008A, 0x57FF, 0x2FFB,
1452  0x226D, 0x009C, 0x199F, 0x0006, 0xAAAD, 0x002A, 0x99DF, 0x4FFB, 0x003C, 0x4DDD, 0x155F, 0x0006, 0x8AAD,
1453  0x004A, 0xCEEF, 0x8FFB, 0x445D, 0x8EED, 0x277F, 0x0006, 0x233D, 0x001A, 0x1BBF, 0x00AA, 0x3FFF, 0x88CD,
1454  0x111F, 0x0006, 0x45DD, 0x2FFB, 0x111D, 0x0018, 0x467D, 0x8FFD, 0xCCCF, 0x0006, 0x19BD, 0x004A, 0x22EF,
1455  0x002A, 0x222D, 0x3FFD, 0x888F, 0x0006, 0x00CC, 0x008A, 0x00FE, 0x0018, 0x115D, 0xCFFD, 0x8AAF, 0x0006,
1456  0x00AC, 0x003A, 0x8CDF, 0x1FFB, 0x133D, 0x66FD, 0x466F, 0x0006, 0x8CCD, 0x2FFB, 0x5FFF, 0x0018, 0x006C,
1457  0x4FFD, 0xABBF, 0x0006, 0x22AD, 0x004A, 0x00EE, 0x002A, 0x233D, 0xAEFD, 0x377F, 0x0006, 0x2BBD, 0x008A,
1458  0x55DF, 0x0018, 0x005C, 0x177D, 0x119F, 0x0006, 0x009C, 0x003A, 0x4CCF, 0x1FFB, 0x333D, 0x8EED, 0x444F,
1459  0x0006, 0x45DD, 0x2FFB, 0x111D, 0x0018, 0x467D, 0x8FFD, 0x99BF, 0x0006, 0x19BD, 0x004A, 0x2EEF, 0x002A,
1460  0x222D, 0x3FFD, 0x667F, 0x0006, 0x00CC, 0x008A, 0x4EEF, 0x0018, 0x115D, 0xCFFD, 0x899F, 0x0006, 0x00AC,
1461  0x003A, 0x00DE, 0x1FFB, 0x133D, 0x66FD, 0x226F, 0x0006, 0x8CCD, 0x2FFB, 0x9BFF, 0x0018, 0x006C, 0x4FFD,
1462  0x00BE, 0x0006, 0x22AD, 0x004A, 0x1DDF, 0x002A, 0x233D, 0xAEFD, 0x007E, 0x0006, 0x2BBD, 0x008A, 0xCEEF,
1463  0x0018, 0x005C, 0x177D, 0x277F, 0x0006, 0x009C, 0x003A, 0x8BBF, 0x1FFB, 0x333D, 0x8EED, 0x455F, 0x1FF9,
1464  0x1DDD, 0xAFFB, 0x00DE, 0x8FF9, 0x001C, 0xFFFB, 0x477F, 0x4FF9, 0x177D, 0x3FFB, 0x3BBF, 0x2FF9, 0xAEEF,
1465  0x8EED, 0x444F, 0x1FF9, 0x22AD, 0x000A, 0x8BBF, 0x8FF9, 0x00FE, 0xCFFD, 0x007E, 0x4FF9, 0x115D, 0x5FFB,
1466  0x577F, 0x2FF9, 0x8DDF, 0x2EED, 0x333F, 0x1FF9, 0x2BBD, 0xAFFB, 0x88CF, 0x8FF9, 0xBFFF, 0xFFFB, 0x377F,
1467  0x4FF9, 0x006D, 0x3FFB, 0x00BE, 0x2FF9, 0x66EF, 0x9FFD, 0x133F, 0x1FF9, 0x009D, 0x000A, 0xABBF, 0x8FF9,
1468  0xDFFF, 0x6FFD, 0x006E, 0x4FF9, 0x002C, 0x5FFB, 0x888F, 0x2FF9, 0xCDDF, 0x4DDD, 0x222F, 0x1FF9, 0x1DDD,
1469  0xAFFB, 0x4CCF, 0x8FF9, 0x001C, 0xFFFB, 0x277F, 0x4FF9, 0x177D, 0x3FFB, 0x99BF, 0x2FF9, 0xCEEF, 0x8EED,
1470  0x004E, 0x1FF9, 0x22AD, 0x000A, 0x00AE, 0x8FF9, 0x7FFF, 0xCFFD, 0x005E, 0x4FF9, 0x115D, 0x5FFB, 0x009E,
1471  0x2FF9, 0x5DDF, 0x2EED, 0x003E, 0x1FF9, 0x2BBD, 0xAFFB, 0x00CE, 0x8FF9, 0xEFFF, 0xFFFB, 0x667F, 0x4FF9,
1472  0x006D, 0x3FFB, 0x8AAF, 0x2FF9, 0x00EE, 0x9FFD, 0x233F, 0x1FF9, 0x009D, 0x000A, 0x1BBF, 0x8FF9, 0x4EEF,
1473  0x6FFD, 0x455F, 0x4FF9, 0x002C, 0x5FFB, 0x008E, 0x2FF9, 0x99DF, 0x4DDD, 0x111F};
q1
static const uint8_t q1[256]
Definition: twofish.c:100
mel_e
const static uint8_t mel_e[13]
Definition: jpeg2000htdec.c:67
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
Jpeg2000Cblk::pass_lengths
int pass_lengths[2]
Definition: jpeg2000.h:194
vlc_decode_u_suffix
static av_always_inline uint8_t vlc_decode_u_suffix(StateVars *vlc_stream, uint8_t suffix, const uint8_t *refill_array)
Decode variable length u-vlc suffix.
Definition: jpeg2000htdec.c:344
jpeg2000_decode_sigprop_segment
static av_noinline void jpeg2000_decode_sigprop_segment(Jpeg2000Cblk *cblk, uint16_t width, uint16_t height, uint8_t *magref_segment, uint32_t magref_length, uint8_t pLSB, int32_t *sample_buf, uint8_t *block_states)
See procedure decodeSigPropMag at Rec.
Definition: jpeg2000htdec.c:1062
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:240
jpeg2000_get_state
static av_always_inline int jpeg2000_get_state(int x1, int x2, int width, int shift_by, const uint8_t *block_states)
Definition: jpeg2000htdec.c:510
ff_clz
#define ff_clz
Definition: intmath.h:143
jpeg2000_bitbuf_peek_bits_lsb
static av_always_inline uint64_t jpeg2000_bitbuf_peek_bits_lsb(StateVars *stream, uint8_t nbits)
Look ahead bit buffer without discarding bits.
Definition: jpeg2000htdec.c:262
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
jpeg2000_decode_magref_segment
static int jpeg2000_decode_magref_segment(uint16_t width, uint16_t block_height, uint8_t *magref_segment, uint32_t magref_length, uint8_t pLSB, int32_t *sample_buf, uint8_t *block_states)
See procedure decodeSigPropMag at Rec.
Definition: jpeg2000htdec.c:1116
MelDecoderState::one
uint8_t one
Definition: jpeg2000htdec.c:84
table
static const uint16_t table[]
Definition: prosumer.c:205
t1
#define t1
Definition: regdef.h:29
HT_SHIFT_REF_IND
#define HT_SHIFT_REF_IND
Definition: jpeg2000htdec.c:64
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
dec_cxt_vlc_table1
static const uint16_t dec_cxt_vlc_table1[1024]
CtxVLC tables (see Rec.
Definition: jpeg2000htdec.c:69
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
jpeg2000_bitbuf_get_bits_lsb_forward
static av_always_inline uint64_t jpeg2000_bitbuf_get_bits_lsb_forward(StateVars *bit_stream, uint8_t nbits, const uint8_t *buf, uint32_t length)
Get bits from the bit buffer reading them from the least significant bits moving to the most signific...
Definition: jpeg2000htdec.c:244
jpeg2000_decode_ctx_vlc
static av_always_inline int jpeg2000_decode_ctx_vlc(const Jpeg2000DecoderContext *s, StateVars *vlc_stream, const uint16_t *table, const uint8_t *Dcup, uint8_t *sig_pat, uint8_t *res_off, uint8_t *emb_pat_k, uint8_t *emb_pat_1, uint8_t pos, uint32_t Pcup, uint16_t context)
Decode prefix codes for VLC segment.
Definition: jpeg2000htdec.c:286
jpeg2000htdec.h
MelDecoderState::run
uint8_t run
Definition: jpeg2000htdec.c:83
val
static double val(void *priv, double ch)
Definition: aeval.c:78
Jpeg2000Cblk::zbp
int zbp
Definition: jpeg2000.h:193
av_noinline
#define av_noinline
Definition: attributes.h:72
Jpeg2000T1Context
Definition: jpeg2000.h:123
jpeg2000_modify_state
static av_always_inline void jpeg2000_modify_state(int x1, int x2, int width, int value, uint8_t *block_states)
Definition: jpeg2000htdec.c:517
avassert.h
StateVars::last
uint32_t last
Definition: jpeg2000htdec.c:76
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
HT_SHIFT_REF
#define HT_SHIFT_REF
Definition: jpeg2000htdec.c:63
jpeg2000_init_mel_decoder
static void jpeg2000_init_mel_decoder(MelDecoderState *mel_state)
Definition: jpeg2000htdec.c:132
StateVars
Definition: jpeg2000htdec.c:72
mask
static const uint16_t mask[17]
Definition: lzw.c:38
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
jpeg2000_peek_bit
static int jpeg2000_peek_bit(StateVars *stream, const uint8_t *array, uint32_t length)
Definition: jpeg2000htdec.c:426
MelDecoderState::k
uint8_t k
Definition: jpeg2000htdec.c:82
jpeg2000_init_mag_ref
static void jpeg2000_init_mag_ref(StateVars *s, uint32_t Lref)
Definition: jpeg2000htdec.c:122
jpeg2000.h
bits
uint8_t bits
Definition: vp3data.h:128
Jpeg2000Cblk::data
uint8_t * data
Definition: jpeg2000.h:184
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
HT_SHIFT_SIGMA
#define HT_SHIFT_SIGMA
Definition: jpeg2000htdec.c:61
J2K_Q1
#define J2K_Q1
Definition: jpeg2000htdec.c:58
vlc_decode_u_extension
static av_always_inline uint8_t vlc_decode_u_extension(StateVars *vlc_stream, uint8_t suffix, const uint8_t *refill_array)
Decode u-vlc extension values.
Definition: jpeg2000htdec.c:369
StateVars::bits
uint32_t bits
Definition: jpeg2000htdec.c:74
E
#define E
Definition: avdct.c:32
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
Jpeg2000Cblk::length
uint16_t length
Definition: jpeg2000.h:180
NULL
#define NULL
Definition: coverity.c:32
is_divisible
static av_always_inline uint32_t is_divisible(uint32_t n, uint64_t c)
Given a precomputed c, checks whether n % d == 0.
Definition: jpeg2000htdec.c:92
ff_jpeg2000_decode_htj2k
int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int magp, uint8_t roi_shift)
HT Block decoder as specified in Rec.
Definition: jpeg2000htdec.c:1160
jpeg2000_bitbuf_get_bits_lsb
static av_always_inline uint64_t jpeg2000_bitbuf_get_bits_lsb(StateVars *bit_stream, uint8_t nbits, const uint8_t *buf)
Get bits from the bit buffer reading them from the least significant bits moving to the most signific...
Definition: jpeg2000htdec.c:226
StateVars::tmp
uint32_t tmp
Definition: jpeg2000htdec.c:75
StateVars::bits_left
uint8_t bits_left
Definition: jpeg2000htdec.c:77
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
jpeg2000_import_bit
static int jpeg2000_import_bit(StateVars *stream, const uint8_t *array, uint32_t length)
Definition: jpeg2000htdec.c:413
StateVars::bit_buf
uint64_t bit_buf
Definition: jpeg2000htdec.c:78
jpeg2000_init_vlc
static void jpeg2000_init_vlc(StateVars *s, uint32_t Lcup, uint32_t Pcup, const uint8_t *Dcup)
Definition: jpeg2000htdec.c:268
sp
#define sp
Definition: regdef.h:63
Jpeg2000Cblk
Definition: jpeg2000.h:175
jpeg2000_bitbuf_drop_bits_lsb
static av_always_inline void jpeg2000_bitbuf_drop_bits_lsb(StateVars *buf, uint8_t nbits)
Drops bits from lower bits in the bit buffer.
Definition: jpeg2000htdec.c:213
height
#define height
attributes.h
jpeg2000_import_magref_bit
static av_always_inline int jpeg2000_import_magref_bit(StateVars *stream, const uint8_t *array, uint32_t length)
Magref decoding procedures.
Definition: jpeg2000htdec.c:477
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
jpeg2000dec.h
StateVars::pos
int32_t pos
Definition: jpeg2000htdec.c:73
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
precompute_c
static av_always_inline uint64_t precompute_c(uint32_t d)
Precompute the number c used by is_divisible().
Definition: jpeg2000htdec.c:101
vlc_decode_u_prefix
static av_always_inline uint8_t vlc_decode_u_prefix(StateVars *vlc_stream, const uint8_t *refill_array)
Decode variable length u-vlc prefix.
Definition: jpeg2000htdec.c:323
common.h
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
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
J2K_Q2
#define J2K_Q2
Definition: jpeg2000htdec.c:59
jpeg2000_init_mel
static void jpeg2000_init_mel(StateVars *s, uint32_t Pcup)
Definition: jpeg2000htdec.c:116
jpeg2000_decode_ht_cleanup_segment
static av_always_inline int jpeg2000_decode_ht_cleanup_segment(const Jpeg2000DecoderContext *s, Jpeg2000Cblk *cblk, Jpeg2000T1Context *t1, MelDecoderState *mel_state, StateVars *mel_stream, StateVars *vlc_stream, StateVars *mag_sgn_stream, const uint8_t *Dcup, uint32_t Lcup, uint32_t Pcup, uint8_t pLSB, int width, int height, int32_t *sample_buf, uint8_t *block_states)
Definition: jpeg2000htdec.c:524
len
int len
Definition: vorbis_enc_data.h:426
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
jpeg2000_decode_sig_emb
static int jpeg2000_decode_sig_emb(const Jpeg2000DecoderContext *s, MelDecoderState *mel_state, StateVars *mel_stream, StateVars *vlc_stream, const uint16_t *vlc_table, const uint8_t *Dcup, uint8_t *sig_pat, uint8_t *res_off, uint8_t *emb_pat_k, uint8_t *emb_pat_1, uint8_t pos, uint16_t context, uint32_t Lcup, uint32_t Pcup)
Signal EMB decode.
Definition: jpeg2000htdec.c:486
stride
#define stride
Definition: h264pred_template.c:537
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
ret
ret
Definition: filter_design.txt:187
HT_SHIFT_SCAN
#define HT_SHIFT_SCAN
Definition: jpeg2000htdec.c:62
jpeg2000_bitbuf_refill_forward
static void jpeg2000_bitbuf_refill_forward(StateVars *buffer, const uint8_t *array, uint32_t length)
Refill the bit-buffer reading new bits going forward in the stream while skipping over stuffed bits.
Definition: jpeg2000htdec.c:192
pos
unsigned int pos
Definition: spdifenc.c:413
U
#define U(x)
Definition: vpx_arith.h:37
suffix
const char * suffix
Definition: checkasm.c:252
ff_jpeg2000_ceildivpow2
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:234
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
MelDecoderState
Definition: jpeg2000htdec.c:81
dec_cxt_vlc_table0
static const uint16_t dec_cxt_vlc_table0[1024]
Definition: jpeg2000htdec.c:70
jpeg2000_calc_mbr
static void jpeg2000_calc_mbr(uint8_t *mbr, const uint16_t i, const uint16_t j, const uint32_t mbr_info, uint8_t causal_cond, uint8_t *block_states, int width)
Definition: jpeg2000htdec.c:992
jpeg2000_init_zero
static void jpeg2000_init_zero(StateVars *s)
Definition: jpeg2000htdec.c:106
jpeg2000_decode_mel_sym
static int jpeg2000_decode_mel_sym(MelDecoderState *mel_state, StateVars *mel_stream, const uint8_t *Dcup, uint32_t Lcup)
Definition: jpeg2000htdec.c:438
Jpeg2000Cblk::npasses
uint8_t npasses
Definition: jpeg2000.h:176
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
jpeg2000_bitbuf_refill_backwards
static int jpeg2000_bitbuf_refill_backwards(StateVars *buffer, const uint8_t *array)
Refill the buffer backwards in little endian while skipping over stuffing bits.
Definition: jpeg2000htdec.c:144
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
d
d
Definition: ffmpeg_filter.c:424
int32_t
int32_t
Definition: audioconvert.c:56
jpeg2000_process_stripes_block
static void jpeg2000_process_stripes_block(StateVars *sig_prop, int i_s, int j_s, int width, int height, int stride, int pLSB, int32_t *sample_buf, uint8_t *block_states, uint8_t *magref_segment, uint32_t magref_length)
Definition: jpeg2000htdec.c:1031
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
Jpeg2000CodingStyle
Definition: jpeg2000.h:137
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
Jpeg2000DecoderContext
Definition: jpeg2000dec.h:73
recover_mag_sgn
static av_always_inline void recover_mag_sgn(StateVars *mag_sgn, uint8_t pos, uint16_t q, int32_t m_n[2], int32_t known_1[2], const uint8_t emb_pat_1[2], int32_t v[2][4], int32_t m[2][4], uint8_t *E, uint32_t *mu_n, const uint8_t *Dcup, uint32_t Pcup, uint32_t pLSB)
Definition: jpeg2000htdec.c:392
jpeg2000_decode_mag_sgn
static av_always_inline int32_t jpeg2000_decode_mag_sgn(StateVars *mag_sgn_stream, int32_t m_n, int32_t i_n, const uint8_t *buf, uint32_t length)
Magnitude and Sign decode procedures.
Definition: jpeg2000htdec.c:380
cond
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28