FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000.h
Date: 2026-10-29 07:45:57
Exec Total Coverage
Lines: 26 34 76.5%
Functions: 8 8 100.0%
Branches: 8 22 36.4%

Line Branch Exec Source
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_JPEG2000_H
24 #define AVCODEC_JPEG2000_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 "libavutil/avassert.h"
35 #include "libavutil/intfloat.h"
36
37 #include "avcodec.h"
38 #include "mqc.h"
39 #include "jpeg2000dwt.h"
40
41 enum Jpeg2000Markers {
42 JPEG2000_SOC = 0xff4f, // start of codestream
43 JPEG2000_CAP = 0xff50, // extended capabilities
44 JPEG2000_SIZ = 0xff51, // image and tile size
45 JPEG2000_COD, // coding style default
46 JPEG2000_COC, // coding style component
47 JPEG2000_TLM = 0xff55, // tile-part length, main header
48 JPEG2000_PLM = 0xff57, // packet length, main header
49 JPEG2000_PLT, // packet length, tile-part header
50 JPEG2000_CPF, // corresponding profile
51 JPEG2000_QCD = 0xff5c, // quantization default
52 JPEG2000_QCC, // quantization component
53 JPEG2000_RGN, // region of interest
54 JPEG2000_POC, // progression order change
55 JPEG2000_PPM, // packed packet headers, main header
56 JPEG2000_PPT, // packed packet headers, tile-part header
57 JPEG2000_CRG = 0xff63, // component registration
58 JPEG2000_COM, // comment
59 JPEG2000_SOT = 0xff90, // start of tile-part
60 JPEG2000_SOP, // start of packet
61 JPEG2000_EPH, // end of packet header
62 JPEG2000_SOD, // start of data
63 JPEG2000_EOC = 0xffd9, // end of codestream
64 };
65
66 enum JPEG2000_Ccap15_b14_15_params {
67 HTJ2K_HTONLY = 0, // HTONLY, bit 14 and 15 are 0
68 HTJ2K_HTDECLARED, // HTDECLARED, bit 14 = 1 and bit 15 = 0
69 HTJ2K_MIXED = 3, // MIXED, bit 14 and 15 are 1
70 };
71
72 #define JPEG2000_SOP_FIXED_BYTES 0xFF910004
73 #define JPEG2000_SOP_BYTE_LENGTH 6
74
75 enum Jpeg2000Quantsty { // quantization style
76 JPEG2000_QSTY_NONE, // no quantization
77 JPEG2000_QSTY_SI, // scalar derived
78 JPEG2000_QSTY_SE // scalar expounded
79 };
80
81 #define JPEG2000_MAX_DECLEVELS 33
82 #define JPEG2000_MAX_RESLEVELS (JPEG2000_MAX_DECLEVELS + 1)
83
84 #define JPEG2000_MAX_PASSES 100
85
86 // T1 flags
87 // flags determining significance of neighbor coefficients
88 #define JPEG2000_T1_SIG_N 0x0001
89 #define JPEG2000_T1_SIG_E 0x0002
90 #define JPEG2000_T1_SIG_W 0x0004
91 #define JPEG2000_T1_SIG_S 0x0008
92 #define JPEG2000_T1_SIG_NE 0x0010
93 #define JPEG2000_T1_SIG_NW 0x0020
94 #define JPEG2000_T1_SIG_SE 0x0040
95 #define JPEG2000_T1_SIG_SW 0x0080
96 #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | \
97 JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W | \
98 JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | \
99 JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
100 // flags determining sign bit of neighbor coefficients
101 #define JPEG2000_T1_SGN_N 0x0100
102 #define JPEG2000_T1_SGN_S 0x0200
103 #define JPEG2000_T1_SGN_W 0x0400
104 #define JPEG2000_T1_SGN_E 0x0800
105
106 #define JPEG2000_T1_VIS 0x1000
107 #define JPEG2000_T1_SIG 0x2000
108 #define JPEG2000_T1_REF 0x4000
109
110 #define JPEG2000_T1_SGN 0x8000
111
112 // Codeblock coding styles
113 #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
114 #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
115 #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
116 #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
117 #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
118 #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
119
120 // Coding styles
121 #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
122 #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
123 #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
124 #define JPEG2000_CTSY_HTJ2K_F 0x40 // Only HT code-blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) are present
125 #define JPEG2000_CTSY_HTJ2K_M 0xC0 // HT code-blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) can be present
126
127 // Progression orders
128 #define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
129 #define JPEG2000_PGOD_RLCP 0x01 // Resolution level-layer-component-position progression
130 #define JPEG2000_PGOD_RPCL 0x02 // Resolution level-position-component-layer progression
131 #define JPEG2000_PGOD_PCRL 0x03 // Position-component-resolution level-layer progression
132 #define JPEG2000_PGOD_CPRL 0x04 // Component-position-resolution level-layer progression
133
134 typedef struct Jpeg2000T1Context {
135 int data[6144];
136 uint16_t flags[6156];
137 MqcState mqc;
138 int stride;
139 } Jpeg2000T1Context;
140
141 typedef struct Jpeg2000TgtNode {
142 uint8_t val;
143 uint8_t temp_val;
144 uint8_t vis;
145 struct Jpeg2000TgtNode *parent;
146 } Jpeg2000TgtNode;
147
148 typedef struct Jpeg2000CodingStyle {
149 int nreslevels; // number of resolution levels
150 int nreslevels2decode; // number of resolution levels to decode
151 uint8_t log2_cblk_width,
152 log2_cblk_height; // exponent of codeblock size
153 uint8_t transform; // DWT type
154 uint8_t csty; // coding style
155 uint8_t nlayers; // number of layers
156 uint8_t mct; // multiple component transformation
157 uint8_t cblk_style; // codeblock coding style
158 uint8_t prog_order; // progression order
159 uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]; // precincts size according resolution levels
160 uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
161 uint8_t init;
162 } Jpeg2000CodingStyle;
163
164 typedef struct Jpeg2000QuantStyle {
165 uint8_t expn[JPEG2000_MAX_DECLEVELS * 3]; // quantization exponent
166 uint16_t mant[JPEG2000_MAX_DECLEVELS * 3]; // quantization mantissa
167 uint8_t quantsty; // quantization style
168 uint8_t nguardbits; // number of guard bits
169 } Jpeg2000QuantStyle;
170
171 typedef struct Jpeg2000Pass {
172 uint16_t rate;
173 int64_t disto;
174 uint8_t flushed[4];
175 int flushed_len;
176 } Jpeg2000Pass;
177
178 typedef struct Jpeg2000Layer {
179 uint8_t *data_start;
180 int data_len;
181 int npasses;
182 double disto;
183 int cum_passes;
184 } Jpeg2000Layer;
185
186 typedef struct Jpeg2000Cblk {
187 uint8_t npasses;
188 uint8_t ninclpasses; // number coding of passes included in codestream
189 uint8_t nonzerobits;
190 uint8_t incl;
191 uint16_t length;
192 uint16_t *lengthinc;
193 uint8_t nb_lengthinc;
194 uint8_t lblock;
195 uint8_t *data;
196 size_t data_allocated;
197 int nb_terminations;
198 int nb_terminationsinc;
199 int *data_start;
200 Jpeg2000Pass *passes;
201 Jpeg2000Layer *layers;
202 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
203 /* specific to HT code-blocks */
204 int zbp;
205 int pass_lengths[2];
206 uint8_t modes; // copy of SPcod/SPcoc field to parse HT-MIXED mode
207 uint8_t ht_plhd; // are we looking for HT placeholder passes?
208 } Jpeg2000Cblk; // code block
209
210 typedef struct Jpeg2000Prec {
211 int nb_codeblocks_width;
212 int nb_codeblocks_height;
213 Jpeg2000TgtNode *zerobits;
214 Jpeg2000TgtNode *cblkincl;
215 Jpeg2000Cblk *cblk;
216 int decoded_layers;
217 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
218 } Jpeg2000Prec; // precinct
219
220 typedef struct Jpeg2000Band {
221 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
222 uint16_t log2_cblk_width, log2_cblk_height;
223 int i_stepsize; // quantization stepsize
224 float f_stepsize; // quantization stepsize
225 Jpeg2000Prec *prec;
226 } Jpeg2000Band; // subband
227
228 typedef struct Jpeg2000ResLevel {
229 uint8_t nbands;
230 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
231 int num_precincts_x, num_precincts_y; // number of precincts in x/y direction
232 uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
233 Jpeg2000Band *band;
234 } Jpeg2000ResLevel; // resolution level
235
236 typedef struct Jpeg2000Component {
237 Jpeg2000ResLevel *reslevel;
238 DWTContext dwt;
239 float *f_data;
240 int *i_data;
241 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
242 int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
243 uint8_t roi_shift; // ROI scaling value for the component
244 } Jpeg2000Component;
245
246 /* misc tools */
247 2162294 static inline int ff_jpeg2000_ceildivpow2(int a, int b)
248 {
249 2162294 return -((-(int64_t)a) >> b);
250 }
251
252 1343738 static inline int ff_jpeg2000_ceildiv(int a, int64_t b)
253 {
254 1343738 return (a + b - 1) / b;
255 }
256
257 /* TIER-1 routines */
258
259 /* Set up lookup tables used in TIER-1. */
260 void ff_jpeg2000_init_tier1_luts(void);
261
262 /* Update significance of a coefficient at current position (x,y) and
263 * for neighbors. */
264 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1,
265 int x, int y, int negative);
266
267 extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
268
269 /* Get context label (number in range[0..8]) of a coefficient for significance
270 * propagation and cleanup coding passes. */
271 994103390 static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
272 {
273 994103390 return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
274 }
275
276 static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
277
278 /* Get context label (number in range[14..16]) of a coefficient for magnitude
279 * refinement pass. */
280 975358077 static inline int ff_jpeg2000_getrefctxno(int flag)
281 {
282 975358077 return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
283 }
284
285 extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
286 extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
287
288 /* Get context label (number in range[9..13]) for sign decoding. */
289 270943980 static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
290 {
291 270943980 *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
292 270943980 return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
293 }
294
295 /* Node count of the tag-tree pyramid over a (w, h) leaf grid. */
296 975046 static inline int32_t ff_jpeg2000_tag_tree_size(int w, int h)
297 {
298 975046 int64_t res = 0;
299
4/4
✓ Branch 0 taken 656018 times.
✓ Branch 1 taken 1081556 times.
✓ Branch 2 taken 106510 times.
✓ Branch 3 taken 975046 times.
1737574 while (w > 1 || h > 1) {
300 762528 res += w * (int64_t)h;
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762528 times.
762528 av_assert0(res + 1 < INT32_MAX);
302 762528 w = (w + 1) >> 1;
303 762528 h = (h + 1) >> 1;
304 }
305 975046 return (int32_t)(res + 1);
306 }
307
308 /**
309 * 2^(x) for integer x in the range -126..128.
310 * @return correctly rounded float
311 */
312 38104 static av_always_inline float ff_jpeg2000_exp2fi(int x)
313 {
314 av_assert2(-126 <= x && x <= 128);
315 /* Normal range */
316 38104 return av_int2float((x + 127) << 23);
317 }
318
319 /* Band dequantization step size (ISO/IEC 15444-1:2002 E.1), shared between
320 * the software decoder's band setup and the Vulkan hwaccel's inline
321 * geometry. */
322 float ff_jpeg2000_band_stepsize(AVCodecContext *avctx,
323 Jpeg2000CodingStyle *codsty,
324 Jpeg2000QuantStyle *qntsty,
325 int bandno, int gbandno,
326 int reslevelno, int cbps);
327
328 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
329 Jpeg2000CodingStyle *codsty,
330 Jpeg2000QuantStyle *qntsty,
331 int cbps, int dx, int dy,
332 AVCodecContext *ctx);
333
334 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
335
336 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
337
338 23541527 static inline int needs_termination(int style, int passno) {
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23541527 times.
23541527 if (style & JPEG2000_CBLK_BYPASS) {
340 ✗ int type = passno % 3;
341 ✗ passno /= 3;
342 ✗ if (type == 0 && passno > 2)
343 ✗ return 2;
344 ✗ if (type == 2 && passno > 2)
345 ✗ return 1;
346 ✗ if (style & JPEG2000_CBLK_TERMALL) {
347 ✗ return passno > 2 ? 2 : 1;
348 }
349 }
350
2/2
✓ Branch 0 taken 8023 times.
✓ Branch 1 taken 23533504 times.
23541527 if (style & JPEG2000_CBLK_TERMALL)
351 8023 return 1;
352 23533504 return 0;
353 }
354
355 void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val);
356
357 #endif /* AVCODEC_JPEG2000_H */
358