FFmpeg
Data Structures | Macros | Functions | Variables
jpeg2000htdec.c File Reference
#include <stdint.h>
#include "libavutil/attributes.h"
#include "libavutil/common.h"
#include "libavutil/avassert.h"
#include "jpeg2000htdec.h"
#include "jpeg2000.h"
#include "jpeg2000dec.h"

Go to the source code of this file.

Data Structures

struct  StateVars
 
struct  MelDecoderState
 

Macros

#define J2K_Q1   0
 
#define J2K_Q2   1
 
#define HT_SHIFT_SIGMA   0
 
#define HT_SHIFT_SCAN   4
 
#define HT_SHIFT_REF   3
 
#define HT_SHIFT_REF_IND   2
 

Functions

static av_always_inline uint32_t is_divisible (uint32_t n, uint64_t c)
 Given a precomputed c, checks whether n % d == 0. More...
 
static av_always_inline uint64_t precompute_c (uint32_t d)
 Precompute the number c used by is_divisible(). More...
 
static void jpeg2000_init_zero (StateVars *s)
 
static void jpeg2000_init_mel (StateVars *s, uint32_t Pcup)
 
static void jpeg2000_init_mag_ref (StateVars *s, uint32_t Lref)
 
static void jpeg2000_init_mel_decoder (MelDecoderState *mel_state)
 
static int jpeg2000_bitbuf_refill_backwards (StateVars *buffer, const uint8_t *array)
 Refill the buffer backwards in little endian while skipping over stuffing bits. More...
 
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. More...
 
static av_always_inline void jpeg2000_bitbuf_drop_bits_lsb (StateVars *buf, uint8_t nbits)
 Drops bits from lower bits in the bit buffer. More...
 
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 significant bits. More...
 
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 significant bits. More...
 
static av_always_inline uint64_t jpeg2000_bitbuf_peek_bits_lsb (StateVars *stream, uint8_t nbits)
 Look ahead bit buffer without discarding bits. More...
 
static void jpeg2000_init_vlc (StateVars *s, uint32_t Lcup, uint32_t Pcup, const uint8_t *Dcup)
 
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. More...
 
static av_always_inline uint8_t vlc_decode_u_prefix (StateVars *vlc_stream, const uint8_t *refill_array)
 Decode variable length u-vlc prefix. More...
 
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. More...
 
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. More...
 
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. More...
 
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)
 
static int jpeg2000_import_bit (StateVars *stream, const uint8_t *array, uint32_t length)
 
static int jpeg2000_peek_bit (StateVars *stream, const uint8_t *array, uint32_t length)
 
static int jpeg2000_decode_mel_sym (MelDecoderState *mel_state, StateVars *mel_stream, const uint8_t *Dcup, uint32_t Lcup)
 
static av_always_inline int jpeg2000_import_magref_bit (StateVars *stream, const uint8_t *array, uint32_t length)
 Magref decoding procedures. More...
 
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. More...
 
static av_always_inline int jpeg2000_get_state (int x1, int x2, int width, int shift_by, const uint8_t *block_states)
 
static av_always_inline void jpeg2000_modify_state (int x1, int x2, int width, int value, uint8_t *block_states)
 
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)
 
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)
 
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)
 
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. More...
 
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. More...
 
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. More...
 

Variables

const static uint8_t mel_e [13] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5 }
 
static const uint16_t dec_cxt_vlc_table1 [1024]
 CtxVLC tables (see Rec. More...
 
static const uint16_t dec_cxt_vlc_table0 [1024]
 

Macro Definition Documentation

◆ J2K_Q1

#define J2K_Q1   0

Definition at line 58 of file jpeg2000htdec.c.

◆ J2K_Q2

#define J2K_Q2   1

Definition at line 59 of file jpeg2000htdec.c.

◆ HT_SHIFT_SIGMA

#define HT_SHIFT_SIGMA   0

Definition at line 61 of file jpeg2000htdec.c.

◆ HT_SHIFT_SCAN

#define HT_SHIFT_SCAN   4

Definition at line 62 of file jpeg2000htdec.c.

◆ HT_SHIFT_REF

#define HT_SHIFT_REF   3

Definition at line 63 of file jpeg2000htdec.c.

◆ HT_SHIFT_REF_IND

#define HT_SHIFT_REF_IND   2

Definition at line 64 of file jpeg2000htdec.c.

Function Documentation

◆ is_divisible()

static av_always_inline uint32_t is_divisible ( uint32_t  n,
uint64_t  c 
)
static

Given a precomputed c, checks whether n % d == 0.

c is precomputed from d using precompute_c().

Definition at line 92 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ precompute_c()

static av_always_inline uint64_t precompute_c ( uint32_t  d)
static

Precompute the number c used by is_divisible().

Definition at line 101 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ jpeg2000_init_zero()

static void jpeg2000_init_zero ( StateVars s)
static

◆ jpeg2000_init_mel()

static void jpeg2000_init_mel ( StateVars s,
uint32_t  Pcup 
)
static

Definition at line 116 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k().

◆ jpeg2000_init_mag_ref()

static void jpeg2000_init_mag_ref ( StateVars s,
uint32_t  Lref 
)
static

Definition at line 122 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_magref_segment().

◆ jpeg2000_init_mel_decoder()

static void jpeg2000_init_mel_decoder ( MelDecoderState mel_state)
static

Definition at line 132 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k().

◆ jpeg2000_bitbuf_refill_backwards()

static int jpeg2000_bitbuf_refill_backwards ( StateVars buffer,
const uint8_t *  array 
)
static

Refill the buffer backwards in little endian while skipping over stuffing bits.

Stuffing bits are those that appear in the position of any byte whose LSBs are all 1's if the last consumed byte was larger than 0x8F.

Unstuff bits. Load a temporary byte, which precedes the position we currently at, to ensure that we can also un-stuff if the stuffed bit is the bottom most bits.

Definition at line 144 of file jpeg2000htdec.c.

Referenced by jpeg2000_bitbuf_get_bits_lsb(), jpeg2000_decode_ctx_vlc(), jpeg2000_decode_ht_cleanup_segment(), jpeg2000_init_vlc(), vlc_decode_u_prefix(), and vlc_decode_u_suffix().

◆ jpeg2000_bitbuf_refill_forward()

static void jpeg2000_bitbuf_refill_forward ( StateVars buffer,
const uint8_t *  array,
uint32_t  length 
)
static

Refill the bit-buffer reading new bits going forward in the stream while skipping over stuffed bits.

Definition at line 192 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k(), and jpeg2000_bitbuf_get_bits_lsb_forward().

◆ jpeg2000_bitbuf_drop_bits_lsb()

static av_always_inline void jpeg2000_bitbuf_drop_bits_lsb ( StateVars buf,
uint8_t  nbits 
)
static

Drops bits from lower bits in the bit buffer.

buf contains the bit buffers. nbits is the number of bits to remove.

Definition at line 213 of file jpeg2000htdec.c.

Referenced by jpeg2000_bitbuf_get_bits_lsb(), jpeg2000_bitbuf_get_bits_lsb_forward(), jpeg2000_decode_ctx_vlc(), jpeg2000_init_vlc(), vlc_decode_u_prefix(), and vlc_decode_u_suffix().

◆ 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 
)
static

Get bits from the bit buffer reading them from the least significant bits moving to the most significant bits.

In case there are fewer bits, refill from buf moving backwards.

Definition at line 226 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment(), jpeg2000_import_magref_bit(), and vlc_decode_u_extension().

◆ 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 
)
static

Get bits from the bit buffer reading them from the least significant bits moving to the most significant bits.

In case there are fewer bits, refill from buf moving forward.

Definition at line 244 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_mag_sgn().

◆ jpeg2000_bitbuf_peek_bits_lsb()

static av_always_inline uint64_t jpeg2000_bitbuf_peek_bits_lsb ( StateVars stream,
uint8_t  nbits 
)
static

Look ahead bit buffer without discarding bits.

Definition at line 262 of file jpeg2000htdec.c.

Referenced by vlc_decode_u_prefix(), and vlc_decode_u_suffix().

◆ jpeg2000_init_vlc()

static void jpeg2000_init_vlc ( StateVars s,
uint32_t  Lcup,
uint32_t  Pcup,
const uint8_t *  Dcup 
)
static

Definition at line 268 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k().

◆ 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 
)
static

Decode prefix codes for VLC segment.

See Rec. ITU-T T.814, 7.3.5.

Definition at line 286 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_sig_emb().

◆ vlc_decode_u_prefix()

static av_always_inline uint8_t vlc_decode_u_prefix ( StateVars vlc_stream,
const uint8_t *  refill_array 
)
static

Decode variable length u-vlc prefix.

See decodeUPrefix procedure at Rec. ITU-T T.814, 7.3.6.

Definition at line 323 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ 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 
)
static

Decode variable length u-vlc suffix.

See decodeUSuffix procedure at Rec. ITU-T T.814, 7.3.6.

Definition at line 344 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ 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 
)
static

Decode u-vlc extension values.

See decodeUExtension procedure at Rec. ITU-T T.814, 7.3.6.

Definition at line 369 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ 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 
)
static

Magnitude and Sign decode procedures.

See decodeMagSgnValue procedure at Rec. ITU-T T.814, 7.3.8.

Definition at line 380 of file jpeg2000htdec.c.

Referenced by recover_mag_sgn().

◆ 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 
)
static

Definition at line 392 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ jpeg2000_import_bit()

static int jpeg2000_import_bit ( StateVars stream,
const uint8_t *  array,
uint32_t  length 
)
static

Definition at line 413 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_mel_sym().

◆ jpeg2000_peek_bit()

static int jpeg2000_peek_bit ( StateVars stream,
const uint8_t *  array,
uint32_t  length 
)
static

Definition at line 426 of file jpeg2000htdec.c.

Referenced by jpeg2000_process_stripes_block().

◆ jpeg2000_decode_mel_sym()

static int jpeg2000_decode_mel_sym ( MelDecoderState mel_state,
StateVars mel_stream,
const uint8_t *  Dcup,
uint32_t  Lcup 
)
static

Definition at line 438 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment(), and jpeg2000_decode_sig_emb().

◆ jpeg2000_import_magref_bit()

static av_always_inline int jpeg2000_import_magref_bit ( StateVars stream,
const uint8_t *  array,
uint32_t  length 
)
static

Magref decoding procedures.

Definition at line 477 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_magref_segment().

◆ 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 
)
static

Signal EMB decode.

Definition at line 486 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ 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 
)
static

◆ jpeg2000_modify_state()

static av_always_inline void jpeg2000_modify_state ( int  x1,
int  x2,
int  width,
int  value,
uint8_t *  block_states 
)
static

◆ 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 
)
static

Initial line pair end. As an optimization, we can replace modulo operations with checking if a number is divisible , since that's the only thing we need. This is paired with is_divisible. Credits to Daniel Lemire blog post [1].

[1] https://lemire.me/blog/2019/02/08/faster-remainders-when-the-divisor-is-a-constant-beating-compilers-and-libdivide/

It's UB on zero, but the spec doesn't allow a quad being zero, so we error out early in case that's the case.

Definition at line 524 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k().

◆ 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 
)
static

Definition at line 992 of file jpeg2000htdec.c.

Referenced by jpeg2000_process_stripes_block().

◆ 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 
)
static

Definition at line 1031 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_sigprop_segment().

◆ 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 
)
static

See procedure decodeSigPropMag at Rec.

ITU-T T.814, 7.4.

Definition at line 1062 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k().

◆ 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 
)
static

See procedure decodeSigPropMag at Rec.

ITU-T T.814, 7.5.

We move column wise, going from one quad to another. See Rec. ITU-T T.814, Figure 7.

Definition at line 1116 of file jpeg2000htdec.c.

Referenced by ff_jpeg2000_decode_htj2k().

◆ 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.

ITU-T T.814 | ISO/IEC 15444-15

Definition at line 1160 of file jpeg2000htdec.c.

Referenced by tile_codeblocks().

Variable Documentation

◆ mel_e

const static uint8_t mel_e[13] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5 }
static

Definition at line 67 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_mel_sym().

◆ dec_cxt_vlc_table1

static const uint16_t dec_cxt_vlc_table1
static

CtxVLC tables (see Rec.

ITU-T T.800, Annex C) as found at https://github.com/osamu620/OpenHTJ2K (author: Osamu Watanabe)

Definition at line 69 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().

◆ dec_cxt_vlc_table0

static const uint16_t dec_cxt_vlc_table0
static

Definition at line 70 of file jpeg2000htdec.c.

Referenced by jpeg2000_decode_ht_cleanup_segment().