3 * Copyright (c) 2007 Kamil Nowosad
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * @author Kamil Nowosad
35 JPEG2000_SOC = 0xff4f, ///< start of codestream
36 JPEG2000_SIZ = 0xff51, ///< image and tile size
37 JPEG2000_COD, ///< coding style default
38 JPEG2000_COC, ///< coding style component
39 JPEG2000_TLM = 0xff55, ///< packed packet headers, tile-part header
40 JPEG2000_PLM = 0xff57, ///< tile-part lengths
41 JPEG2000_PLT, ///< packet length, main header
42 JPEG2000_QCD = 0xff5c, ///< quantization default
43 JPEG2000_QCC, ///< quantization component
44 JPEG2000_RGN, ///< region of interest
45 JPEG2000_POC, ///< progression order change
46 JPEG2000_PPM, ///< packet length, tile-part header
47 JPEG2000_PPT, ///< packed packet headers, main header
48 JPEG2000_CRG = 0xff63, ///< component registration
49 JPEG2000_COM, ///< comment
50 JPEG2000_SOT = 0xff90, ///< start of tile-part
51 JPEG2000_SOP, ///< start of packet
52 JPEG2000_EPH, ///< end of packet header
53 JPEG2000_SOD, ///< start of data
54 JPEG2000_EOC = 0xffd9, ///< end of codestream
57 enum Jpeg2000Quantsty{ ///< quantization style
58 JPEG2000_QSTY_NONE, ///< no quantization
59 JPEG2000_QSTY_SI, ///< scalar derived
60 JPEG2000_QSTY_SE ///< scalar expoounded
63 #define JPEG2000_MAX_CBLKW 64
64 #define JPEG2000_MAX_CBLKH 64
67 // flags determining significance of neighbour coefficients
68 #define JPEG2000_T1_SIG_N 0x0001
69 #define JPEG2000_T1_SIG_E 0x0002
70 #define JPEG2000_T1_SIG_W 0x0004
71 #define JPEG2000_T1_SIG_S 0x0008
72 #define JPEG2000_T1_SIG_NE 0x0010
73 #define JPEG2000_T1_SIG_NW 0x0020
74 #define JPEG2000_T1_SIG_SE 0x0040
75 #define JPEG2000_T1_SIG_SW 0x0080
76 #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W \
77 |JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
78 // flags determining sign bit of neighbour coefficients
79 #define JPEG2000_T1_SGN_N 0x0100
80 #define JPEG2000_T1_SGN_S 0x0200
81 #define JPEG2000_T1_SGN_W 0x0400
82 #define JPEG2000_T1_SGN_E 0x0800
84 #define JPEG2000_T1_VIS 0x1000
85 #define JPEG2000_T1_SIG 0x2000
86 #define JPEG2000_T1_REF 0x4000
88 #define JPEG2000_T1_SGN 0x8000
90 // Codeblock coding styles
91 #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
92 #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
93 #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
94 #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
95 #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
96 #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
99 #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
100 #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
101 #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
104 int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
105 int flags[JPEG2000_MAX_CBLKW+2][JPEG2000_MAX_CBLKH+2];
109 typedef struct Jpeg2000TgtNode {
112 struct Jpeg2000TgtNode *parent;
116 uint8_t nreslevels; ///< number of resolution levels
117 uint8_t log2_cblk_width,
118 log2_cblk_height; ///< exponent of codeblock size
119 uint8_t transform; ///< DWT type
120 uint8_t csty; ///< coding style
121 uint8_t log2_prec_width,
122 log2_prec_height; ///< precinct size
123 uint8_t nlayers; ///< number of layers
124 uint8_t mct; ///< multiple component transformation
125 uint8_t cblk_style; ///< codeblock coding style
126 } Jpeg2000CodingStyle;
129 uint8_t expn[32 * 3]; ///< quantization exponent
130 uint16_t mant[32 * 3]; ///< quantization mantissa
131 uint8_t quantsty; ///< quantization style
132 uint8_t nguardbits; ///< number of guard bits
133 } Jpeg2000QuantStyle;
142 uint8_t ninclpasses; ///< number coding of passes included in codestream
149 Jpeg2000Pass passes[100];
150 } Jpeg2000Cblk; ///< code block
153 uint16_t xi0, xi1, yi0, yi1; ///< codeblock indexes ([xi0, xi1))
154 Jpeg2000TgtNode *zerobits;
155 Jpeg2000TgtNode *cblkincl;
156 } Jpeg2000Prec; ///< precinct
159 uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
160 uint16_t codeblock_width, codeblock_height;
161 uint16_t cblknx, cblkny;
162 uint32_t stepsize; ///< quantization stepsize (* 2^13)
165 } Jpeg2000Band; ///< subband
169 uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
170 uint16_t num_precincts_x, num_precincts_y; ///< number of precincts in x/y direction
171 uint8_t log2_prec_width, log2_prec_height; ///< exponent of precinct size
173 } Jpeg2000ResLevel; ///< resolution level
176 Jpeg2000ResLevel *reslevel;
179 uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
186 void ff_j2k_printv(int *tab, int l);
187 void ff_j2k_printu(uint8_t *tab, int l);
191 static inline int ff_j2k_ceildivpow2(int a, int b)
193 return (a + (1 << b) - 1)>> b;
196 static inline int ff_j2k_ceildiv(int a, int b)
198 return (a + b - 1) / b;
201 /* tag tree routines */
202 Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h);
204 /* TIER-1 routines */
205 void ff_j2k_init_tier1_luts(void);
207 void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y, int negative);
209 extern uint8_t ff_j2k_nbctxno_lut[256][4];
211 static inline int ff_j2k_getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
213 return ff_j2k_nbctxno_lut[flag&255][bandno];
216 static inline int ff_j2k_getrefctxno(int flag)
218 static const uint8_t refctxno_lut[2][2] = {{14, 15}, {16, 16}};
219 return refctxno_lut[(flag>>14)&1][(flag & 255) != 0];
222 extern uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
224 static inline int ff_j2k_getsgnctxno(int flag, int *xorbit)
226 *xorbit = ff_j2k_xorbit_lut[flag&15][(flag>>8)&15];
227 return ff_j2k_sgnctxno_lut[flag&15][(flag>>8)&15];
230 int ff_j2k_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy);
231 void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
232 void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
234 #endif /* AVCODEC_J2K_H */