]> git.sesse.net Git - ffmpeg/blob - libavcodec/j2k.h
j2k.h: Merges various cosmetics & unused defines
[ffmpeg] / libavcodec / j2k.h
1 /*
2  * JPEG 2000 common defines, structures and functions
3  * Copyright (c) 2007 Kamil Nowosad
4  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef AVCODEC_J2K_H
24 #define AVCODEC_J2K_H
25
26 /**
27  * JPEG2000 tables
28  * @file
29  * @author Kamil Nowosad
30  */
31
32 #include "mqc.h"
33 #include "j2k_dwt.h"
34
35 enum Jpeg2000Markers{
36     JPEG2000_SOC = 0xff4f, // start of codestream
37     JPEG2000_SIZ = 0xff51, // image and tile size
38     JPEG2000_COD,          // coding style default
39     JPEG2000_COC,          // coding style component
40     JPEG2000_TLM = 0xff55, // packed packet headers, tile-part header
41     JPEG2000_PLM = 0xff57, // tile-part lengths
42     JPEG2000_PLT,          // packet length, main header
43     JPEG2000_QCD = 0xff5c, // quantization default
44     JPEG2000_QCC,          // quantization component
45     JPEG2000_RGN,          // region of interest
46     JPEG2000_POC,          // progression order change
47     JPEG2000_PPM,          // packet length, tile-part header
48     JPEG2000_PPT,          // packed packet headers, main header
49     JPEG2000_CRG = 0xff63, // component registration
50     JPEG2000_COM,          // comment
51     JPEG2000_SOT = 0xff90, // start of tile-part
52     JPEG2000_SOP,          // start of packet
53     JPEG2000_EPH,          // end of packet header
54     JPEG2000_SOD,          // start of data
55     JPEG2000_EOC = 0xffd9, // end of codestream
56 };
57
58 enum Jpeg2000Quantsty{ // quantization style
59     JPEG2000_QSTY_NONE, // no quantization
60     JPEG2000_QSTY_SI,   // scalar derived
61     JPEG2000_QSTY_SE    // scalar expounded
62 };
63
64 #define JPEG2000_MAX_CBLKW 64
65 #define JPEG2000_MAX_CBLKH 64
66
67 #define JPEG2000_MAX_RESLEVELS 33
68
69 // T1 flags
70 // flags determining significance of neighbor coefficients
71 #define JPEG2000_T1_SIG_N  0x0001
72 #define JPEG2000_T1_SIG_E  0x0002
73 #define JPEG2000_T1_SIG_W  0x0004
74 #define JPEG2000_T1_SIG_S  0x0008
75 #define JPEG2000_T1_SIG_NE 0x0010
76 #define JPEG2000_T1_SIG_NW 0x0020
77 #define JPEG2000_T1_SIG_SE 0x0040
78 #define JPEG2000_T1_SIG_SW 0x0080
79 #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N  | JPEG2000_T1_SIG_E  |   \
80                             JPEG2000_T1_SIG_S  | JPEG2000_T1_SIG_W  |   \
81                             JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW |   \
82                             JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
83 // flags determining sign bit of neighbor coefficients
84 #define JPEG2000_T1_SGN_N  0x0100
85 #define JPEG2000_T1_SGN_S  0x0200
86 #define JPEG2000_T1_SGN_W  0x0400
87 #define JPEG2000_T1_SGN_E  0x0800
88
89 #define JPEG2000_T1_VIS    0x1000
90 #define JPEG2000_T1_SIG    0x2000
91 #define JPEG2000_T1_REF    0x4000
92
93 #define JPEG2000_T1_SGN    0x8000
94
95 // Codeblock coding styles
96 #define JPEG2000_CBLK_BYPASS    0x01 // Selective arithmetic coding bypass
97 #define JPEG2000_CBLK_RESET     0x02 // Reset context probabilities
98 #define JPEG2000_CBLK_TERMALL   0x04 // Terminate after each coding pass
99 #define JPEG2000_CBLK_VSC       0x08 // Vertical stripe causal context formation
100 #define JPEG2000_CBLK_PREDTERM  0x10 // Predictable termination
101 #define JPEG2000_CBLK_SEGSYM    0x20 // Segmentation symbols present
102
103 // Coding styles
104 #define JPEG2000_CSTY_PREC      0x01 // Precincts defined in coding style
105 #define JPEG2000_CSTY_SOP       0x02 // SOP marker present
106 #define JPEG2000_CSTY_EPH       0x04 // EPH marker present
107
108 // Progression orders
109 #define JPEG2000_PGOD_LRCP      0x00  // Layer-resolution level-component-position progression
110 #define JPEG2000_PGOD_RLCP      0x01  // Resolution level-layer-component-position progression
111 #define JPEG2000_PGOD_RPCL      0x02  // Resolution level-position-component-layer progression
112 #define JPEG2000_PGOD_PCRL      0x03  // Position-component-resolution level-layer progression
113 #define JPEG2000_PGOD_CPRL      0x04  // Component-position-resolution level-layer progression
114
115 typedef struct Jpeg2000T1Context {
116     int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
117     int flags[JPEG2000_MAX_CBLKW+2][JPEG2000_MAX_CBLKH+2];
118     MqcState mqc;
119 } Jpeg2000T1Context;
120
121 typedef struct Jpeg2000TgtNode {
122     uint8_t val;
123     uint8_t vis;
124     struct Jpeg2000TgtNode *parent;
125 } Jpeg2000TgtNode;
126
127 typedef struct Jpeg2000CodingStyle {
128     uint8_t nreslevels;       // number of resolution levels
129     uint8_t log2_cblk_width,
130             log2_cblk_height; // exponent of codeblock size
131     uint8_t transform;        // DWT type
132     uint8_t csty;             // coding style
133     uint8_t log2_prec_width,
134             log2_prec_height; // precinct size
135     uint8_t nlayers;          // number of layers
136     uint8_t mct;              // multiple component transformation
137     uint8_t cblk_style;       // codeblock coding style
138     uint8_t prog_order;       // progression order
139     uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS];  // precincts size according resolution levels
140     uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
141 } Jpeg2000CodingStyle;
142
143 typedef struct Jpeg2000QuantStyle {
144     uint8_t  expn[32 * 3]; // quantization exponent
145     uint16_t mant[32 * 3]; // quantization mantissa
146     uint8_t  quantsty;     // quantization style
147     uint8_t  nguardbits;   // number of guard bits
148 } Jpeg2000QuantStyle;
149
150 typedef struct Jpeg2000Pass {
151     uint16_t rate;
152     int64_t disto;
153 } Jpeg2000Pass;
154
155 typedef struct Jpeg2000Cblk  {
156     uint8_t npasses;
157     uint8_t ninclpasses; // number coding of passes included in codestream
158     uint8_t nonzerobits;
159     uint16_t length;
160     uint16_t lengthinc;
161     uint8_t lblock;
162     uint8_t zero;
163     uint8_t data[8192];
164     Jpeg2000Pass passes[100];
165 } Jpeg2000Cblk; // code block
166
167 typedef struct Jpeg2000Prec {
168     uint16_t xi0, xi1, yi0, yi1; // codeblock indexes ([xi0, xi1))
169     Jpeg2000TgtNode *zerobits;
170     Jpeg2000TgtNode *cblkincl;
171 } Jpeg2000Prec; // precinct
172
173 typedef struct Jpeg2000Band {
174     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
175     uint16_t codeblock_width, codeblock_height;
176     uint16_t cblknx, cblkny;
177     uint32_t stepsize; // quantization stepsize (* 2^13)
178     Jpeg2000Prec *prec;
179     Jpeg2000Cblk *cblk;
180 } Jpeg2000Band; // subband
181
182 typedef struct Jpeg2000ResLevel {
183     uint8_t nbands;
184     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
185     uint16_t num_precincts_x, num_precincts_y; // number of precincts in x/y direction
186     uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
187     Jpeg2000Band *band;
188 } Jpeg2000ResLevel; // resolution level
189
190 typedef struct Jpeg2000Component {
191    Jpeg2000ResLevel *reslevel;
192    DWTContext dwt;
193    int *data;
194    uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
195 } Jpeg2000Component;
196
197 /* debug routines */
198 #if 0
199 #undef fprintf
200 #undef printf
201 void ff_j2k_printv(int *tab, int l);
202 void ff_j2k_printu(uint8_t *tab, int l);
203 #endif
204
205 /* misc tools */
206 static inline int ff_jpeg2000_ceildivpow2(int a, int b)
207 {
208     return (a + (1 << b) - 1)>> b;
209 }
210
211 static inline int ff_jpeg2000_ceildiv(int a, int b)
212 {
213     return (a + b - 1) / b;
214 }
215
216 /* tag tree routines */
217 Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h);
218
219 /* TIER-1 routines */
220 void ff_jpeg2000_init_tier1_luts(void);
221
222 void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y, int negative);
223
224 extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
225
226 static inline int ff_j2k_getsigctxno(int flag, int bandno)
227 {
228     return ff_jpeg2000_sigctxno_lut[flag&255][bandno];
229 }
230
231 /* Get context label (number in range[14..16]) of a coefficient for magnitude
232  * refinement pass. */
233 static inline int ff_jpeg2000_getrefctxno(int flag)
234 {
235     static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
236     return refctxno_lut[(flag>>14)&1][(flag & 255) != 0];
237 }
238
239 extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
240 extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
241
242 /* Get context label (number in range[9..13]) for sign decoding. */
243 static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
244 {
245     *xorbit = ff_jpeg2000_xorbit_lut[flag&15][(flag>>8)&15];
246     return  ff_jpeg2000_sgnctxno_lut[flag&15][(flag>>8)&15];
247 }
248
249 int ff_j2k_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy);
250 void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
251 void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
252
253 #endif /* AVCODEC_J2K_H */