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