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