]> git.sesse.net Git - ffmpeg/blob - libavcodec/j2k.h
Merge commit '5f30c6c8ed1f006c1b3a547dfe388f3a4a080e4d'
[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  * @file
28  * JPEG 2000 structures and defines common
29  * to encoder and decoder
30  */
31
32 #include <stdint.h>
33
34 #include "avcodec.h"
35 #include "mqc.h"
36 #include "jpeg2000dwt.h"
37
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
59 };
60
61 enum Jpeg2000Quantsty { // quantization style
62     JPEG2000_QSTY_NONE, // no quantization
63     JPEG2000_QSTY_SI,   // scalar derived
64     JPEG2000_QSTY_SE    // scalar expounded
65 };
66
67 #define JPEG2000_MAX_CBLKW 64
68 #define JPEG2000_MAX_CBLKH 64
69
70 #define JPEG2000_MAX_RESLEVELS 33
71
72 // T1 flags
73 // flags determining significance of neighbor coefficients
74 #define JPEG2000_T1_SIG_N  0x0001
75 #define JPEG2000_T1_SIG_E  0x0002
76 #define JPEG2000_T1_SIG_W  0x0004
77 #define JPEG2000_T1_SIG_S  0x0008
78 #define JPEG2000_T1_SIG_NE 0x0010
79 #define JPEG2000_T1_SIG_NW 0x0020
80 #define JPEG2000_T1_SIG_SE 0x0040
81 #define JPEG2000_T1_SIG_SW 0x0080
82 #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N  | JPEG2000_T1_SIG_E  |   \
83                             JPEG2000_T1_SIG_S  | JPEG2000_T1_SIG_W  |   \
84                             JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW |   \
85                             JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
86 // flags determining sign bit of neighbor coefficients
87 #define JPEG2000_T1_SGN_N  0x0100
88 #define JPEG2000_T1_SGN_S  0x0200
89 #define JPEG2000_T1_SGN_W  0x0400
90 #define JPEG2000_T1_SGN_E  0x0800
91
92 #define JPEG2000_T1_VIS    0x1000
93 #define JPEG2000_T1_SIG    0x2000
94 #define JPEG2000_T1_REF    0x4000
95
96 #define JPEG2000_T1_SGN    0x8000
97
98 // Codeblock coding styles
99 #define JPEG2000_CBLK_BYPASS    0x01 // Selective arithmetic coding bypass
100 #define JPEG2000_CBLK_RESET     0x02 // Reset context probabilities
101 #define JPEG2000_CBLK_TERMALL   0x04 // Terminate after each coding pass
102 #define JPEG2000_CBLK_VSC       0x08 // Vertical stripe causal context formation
103 #define JPEG2000_CBLK_PREDTERM  0x10 // Predictable termination
104 #define JPEG2000_CBLK_SEGSYM    0x20 // Segmentation symbols present
105
106 // Coding styles
107 #define JPEG2000_CSTY_PREC      0x01 // Precincts defined in coding style
108 #define JPEG2000_CSTY_SOP       0x02 // SOP marker present
109 #define JPEG2000_CSTY_EPH       0x04 // EPH marker present
110
111 // Progression orders
112 #define JPEG2000_PGOD_LRCP      0x00  // Layer-resolution level-component-position progression
113 #define JPEG2000_PGOD_RLCP      0x01  // Resolution level-layer-component-position progression
114 #define JPEG2000_PGOD_RPCL      0x02  // Resolution level-position-component-layer progression
115 #define JPEG2000_PGOD_PCRL      0x03  // Position-component-resolution level-layer progression
116 #define JPEG2000_PGOD_CPRL      0x04  // Component-position-resolution level-layer progression
117
118 typedef struct Jpeg2000T1Context {
119     int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
120     int flags[JPEG2000_MAX_CBLKW + 2][JPEG2000_MAX_CBLKH + 2];
121     MqcState mqc;
122 } Jpeg2000T1Context;
123
124 typedef struct Jpeg2000TgtNode {
125     uint8_t val;
126     uint8_t vis;
127     struct Jpeg2000TgtNode *parent;
128 } Jpeg2000TgtNode;
129
130 typedef struct Jpeg2000CodingStyle {
131     uint8_t nreslevels;       // number of resolution levels
132     uint8_t nreslevels2decode; // number of resolution levels to decode
133     uint8_t log2_cblk_width,
134             log2_cblk_height; // exponent of codeblock size
135     uint8_t transform;        // DWT type
136     uint8_t csty;             // coding style
137     uint8_t nlayers;          // number of layers
138     uint8_t mct;              // multiple component transformation
139     uint8_t cblk_style;       // codeblock coding style
140     uint8_t prog_order;       // progression order
141     uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS];  // precincts size according resolution levels
142     uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
143 } Jpeg2000CodingStyle;
144
145 typedef struct Jpeg2000QuantStyle {
146     uint8_t expn[32 * 3];  // quantization exponent
147     uint16_t mant[32 * 3]; // quantization mantissa
148     uint8_t quantsty;      // quantization style
149     uint8_t nguardbits;    // number of guard bits
150 } Jpeg2000QuantStyle;
151
152 typedef struct Jpeg2000Pass {
153     uint16_t rate;
154     int64_t disto;
155 } Jpeg2000Pass;
156
157 typedef struct Jpeg2000Cblk {
158     uint8_t npasses;
159     uint8_t ninclpasses; // number coding of passes included in codestream
160     uint8_t nonzerobits;
161     uint16_t length;
162     uint16_t lengthinc;
163     uint8_t lblock;
164     uint8_t zero;
165     uint8_t data[8192];
166     Jpeg2000Pass passes[100];
167     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
168 } Jpeg2000Cblk; // code block
169
170 typedef struct Jpeg2000Prec {
171     uint16_t nb_codeblocks_width;
172     uint16_t nb_codeblocks_height;
173     Jpeg2000TgtNode *zerobits;
174     Jpeg2000TgtNode *cblkincl;
175     Jpeg2000Cblk *cblk;
176     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
177 } Jpeg2000Prec; // precinct
178
179 typedef struct Jpeg2000Band {
180     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
181     uint16_t log2_cblk_width, log2_cblk_height;
182     uint32_t stepsize; // quantization stepsize (* 2^13)
183     Jpeg2000Prec *prec;
184 } Jpeg2000Band; // subband
185
186 typedef struct Jpeg2000ResLevel {
187     uint8_t nbands;
188     uint16_t coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}}
189     uint16_t num_precincts_x, num_precincts_y; // number of precincts in x/y direction
190     uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
191     Jpeg2000Band *band;
192 } Jpeg2000ResLevel; // resolution level
193
194 typedef struct Jpeg2000Component {
195     Jpeg2000ResLevel *reslevel;
196     DWTContext dwt;
197     int *data;
198     uint16_t coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
199     uint16_t coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
200 } Jpeg2000Component;
201
202 /* misc tools */
203 static inline int ff_jpeg2000_ceildivpow2(int a, int b)
204 {
205     return (a + (1 << b) - 1) >> b;
206 }
207
208 static inline int ff_jpeg2000_ceildiv(int a, int b)
209 {
210     return (a + b - 1) / b;
211 }
212
213 /* tag tree routines */
214 Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h);
215
216 /* TIER-1 routines */
217
218 /* Set up lookup tables used in TIER-1. */
219 void ff_jpeg2000_init_tier1_luts(void);
220
221 /* Update significance of a coefficient at current position (x,y) and
222  * for neighbors. */
223 void ff_j2k_set_significant(Jpeg2000T1Context *t1,
224                             int x, int y, int negative);
225
226 extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
227
228 /* Get context label (number in range[0..8]) of a coefficient for significance
229  * propagation and cleanup coding passes. */
230 static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
231 {
232     return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
233 }
234
235 static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
236
237 /* Get context label (number in range[14..16]) of a coefficient for magnitude
238  * refinement pass. */
239 static inline int ff_jpeg2000_getrefctxno(int flag)
240 {
241     return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
242 }
243
244 extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
245 extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
246
247 /* Get context label (number in range[9..13]) for sign decoding. */
248 static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
249 {
250     *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
251     return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
252 }
253
254 int ff_j2k_init_component(Jpeg2000Component *comp,
255                           Jpeg2000CodingStyle *codsty,
256                           Jpeg2000QuantStyle *qntsty,
257                           int cbps, int dx, int dy,
258                           AVCodecContext *avctx);
259 void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
260 void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
261
262 #endif /* AVCODEC_J2K_H */