2 * JPEG 2000 common defines, structures and functions
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef AVCODEC_JPEG2000_H
24 #define AVCODEC_JPEG2000_H
28 * JPEG 2000 structures and defines common
29 * to encoder and decoder
36 #include "jpeg2000dwt.h"
38 enum Jpeg2000Markers {
39 JPEG2000_SOC = 0xff4f, // start of codestream
40 JPEG2000_SIZ = 0xff51, // image and tile size
41 JPEG2000_COD, // coding style default
42 JPEG2000_COC, // coding style component
43 JPEG2000_TLM = 0xff55, // packed packet headers, tile-part header
44 JPEG2000_PLM = 0xff57, // tile-part lengths
45 JPEG2000_PLT, // packet length, main header
46 JPEG2000_QCD = 0xff5c, // quantization default
47 JPEG2000_QCC, // quantization component
48 JPEG2000_RGN, // region of interest
49 JPEG2000_POC, // progression order change
50 JPEG2000_PPM, // packet length, tile-part header
51 JPEG2000_PPT, // packed packet headers, main header
52 JPEG2000_CRG = 0xff63, // component registration
53 JPEG2000_COM, // comment
54 JPEG2000_SOT = 0xff90, // start of tile-part
55 JPEG2000_SOP, // start of packet
56 JPEG2000_EPH, // end of packet header
57 JPEG2000_SOD, // start of data
58 JPEG2000_EOC = 0xffd9, // end of codestream
61 #define JPEG2000_SOP_FIXED_BYTES 0xFF910004
62 #define JPEG2000_SOP_BYTE_LENGTH 6
64 enum Jpeg2000Quantsty { // quantization style
65 JPEG2000_QSTY_NONE, // no quantization
66 JPEG2000_QSTY_SI, // scalar derived
67 JPEG2000_QSTY_SE // scalar expounded
70 #define JPEG2000_MAX_CBLKW 64
71 #define JPEG2000_MAX_CBLKH 64
74 #define JPEG2000_MAX_DECLEVELS 32
75 #define JPEG2000_MAX_RESLEVELS (JPEG2000_MAX_DECLEVELS + 1)
77 #define JPEG2000_MAX_PASSES 100
80 // flags determining significance of neighbor coefficients
81 #define JPEG2000_T1_SIG_N 0x0001
82 #define JPEG2000_T1_SIG_E 0x0002
83 #define JPEG2000_T1_SIG_W 0x0004
84 #define JPEG2000_T1_SIG_S 0x0008
85 #define JPEG2000_T1_SIG_NE 0x0010
86 #define JPEG2000_T1_SIG_NW 0x0020
87 #define JPEG2000_T1_SIG_SE 0x0040
88 #define JPEG2000_T1_SIG_SW 0x0080
89 #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | \
90 JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W | \
91 JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | \
92 JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
93 // flags determining sign bit of neighbor coefficients
94 #define JPEG2000_T1_SGN_N 0x0100
95 #define JPEG2000_T1_SGN_S 0x0200
96 #define JPEG2000_T1_SGN_W 0x0400
97 #define JPEG2000_T1_SGN_E 0x0800
99 #define JPEG2000_T1_VIS 0x1000
100 #define JPEG2000_T1_SIG 0x2000
101 #define JPEG2000_T1_REF 0x4000
103 #define JPEG2000_T1_SGN 0x8000
105 // Codeblock coding styles
106 #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
107 #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
108 #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
109 #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
110 #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
111 #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
114 #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
115 #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
116 #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
118 // Progression orders
119 #define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
120 #define JPEG2000_PGOD_RLCP 0x01 // Resolution level-layer-component-position progression
121 #define JPEG2000_PGOD_RPCL 0x02 // Resolution level-position-component-layer progression
122 #define JPEG2000_PGOD_PCRL 0x03 // Position-component-resolution level-layer progression
123 #define JPEG2000_PGOD_CPRL 0x04 // Component-position-resolution level-layer progression
125 typedef struct Jpeg2000T1Context {
126 int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
127 int flags[JPEG2000_MAX_CBLKW + 2][JPEG2000_MAX_CBLKH + 2];
131 typedef struct Jpeg2000TgtNode {
134 struct Jpeg2000TgtNode *parent;
137 typedef struct Jpeg2000CodingStyle {
138 int nreslevels; // number of resolution levels
139 int nreslevels2decode; // number of resolution levels to decode
140 uint8_t log2_cblk_width,
141 log2_cblk_height; // exponent of codeblock size
142 uint8_t transform; // DWT type
143 uint8_t csty; // coding style
144 uint8_t nlayers; // number of layers
145 uint8_t mct; // multiple component transformation
146 uint8_t cblk_style; // codeblock coding style
147 uint8_t prog_order; // progression order
148 uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]; // precincts size according resolution levels
149 uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
150 } Jpeg2000CodingStyle;
152 typedef struct Jpeg2000QuantStyle {
153 uint8_t expn[JPEG2000_MAX_DECLEVELS * 3]; // quantization exponent
154 uint16_t mant[JPEG2000_MAX_DECLEVELS * 3]; // quantization mantissa
155 uint8_t quantsty; // quantization style
156 uint8_t nguardbits; // number of guard bits
157 } Jpeg2000QuantStyle;
159 typedef struct Jpeg2000Pass {
166 typedef struct Jpeg2000Cblk {
168 uint8_t ninclpasses; // number coding of passes included in codestream
171 uint16_t lengthinc[JPEG2000_MAX_PASSES];
172 uint8_t nb_lengthinc;
177 int nb_terminationsinc;
178 int data_start[JPEG2000_MAX_PASSES];
179 Jpeg2000Pass passes[JPEG2000_MAX_PASSES];
180 uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
181 } Jpeg2000Cblk; // code block
183 typedef struct Jpeg2000Prec {
184 uint16_t nb_codeblocks_width;
185 uint16_t nb_codeblocks_height;
186 Jpeg2000TgtNode *zerobits;
187 Jpeg2000TgtNode *cblkincl;
189 uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
190 } Jpeg2000Prec; // precinct
192 typedef struct Jpeg2000Band {
193 uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
194 uint16_t log2_cblk_width, log2_cblk_height;
195 int i_stepsize; // quantization stepsize
196 float f_stepsize; // quantization stepsize
198 } Jpeg2000Band; // subband
200 typedef struct Jpeg2000ResLevel {
202 uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
203 uint16_t num_precincts_x, num_precincts_y; // number of precincts in x/y direction
204 uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
206 } Jpeg2000ResLevel; // resolution level
208 typedef struct Jpeg2000Component {
209 Jpeg2000ResLevel *reslevel;
213 uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
214 uint16_t coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
218 static inline int ff_jpeg2000_ceildivpow2(int a, int b)
220 return (a + (1 << b) - 1) >> b;
223 static inline int ff_jpeg2000_ceildiv(int a, int b)
225 return (a + b - 1) / b;
228 /* TIER-1 routines */
230 /* Set up lookup tables used in TIER-1. */
231 void ff_jpeg2000_init_tier1_luts(void);
233 /* Update significance of a coefficient at current position (x,y) and
235 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1,
236 int x, int y, int negative);
238 extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
240 /* Get context label (number in range[0..8]) of a coefficient for significance
241 * propagation and cleanup coding passes. */
242 static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
244 return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
247 static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
249 /* Get context label (number in range[14..16]) of a coefficient for magnitude
250 * refinement pass. */
251 static inline int ff_jpeg2000_getrefctxno(int flag)
253 return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
256 extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
257 extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
259 /* Get context label (number in range[9..13]) for sign decoding. */
260 static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
262 *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
263 return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
266 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
267 Jpeg2000CodingStyle *codsty,
268 Jpeg2000QuantStyle *qntsty,
269 int cbps, int dx, int dy,
270 AVCodecContext *ctx);
272 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
274 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
276 static inline int needs_termination(int style, int passno) {
277 if (style & JPEG2000_CBLK_BYPASS) {
278 int type = passno % 3;
280 if (type == 0 && passno > 2)
282 if (type == 2 && passno > 2)
284 if (style & JPEG2000_CBLK_TERMALL) {
285 return passno > 2 ? 2 : 1;
288 if (style & JPEG2000_CBLK_TERMALL)
293 #endif /* AVCODEC_JPEG2000_H */