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