FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000.h
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 16 24 66.7%
Functions: 6 6 100.0%
Branches: 3 16 18.8%

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 "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, // tile-part length, main header
44 JPEG2000_PLM = 0xff57, // packet length, main header
45 JPEG2000_PLT, // packet length, tile-part 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, // packed packet headers, main header
51 JPEG2000_PPT, // packed packet headers, tile-part 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 #define JPEG2000_SOP_FIXED_BYTES 0xFF910004
62 #define JPEG2000_SOP_BYTE_LENGTH 6
63
64 enum Jpeg2000Quantsty { // quantization style
65 JPEG2000_QSTY_NONE, // no quantization
66 JPEG2000_QSTY_SI, // scalar derived
67 JPEG2000_QSTY_SE // scalar expounded
68 };
69
70 #define JPEG2000_MAX_DECLEVELS 33
71 #define JPEG2000_MAX_RESLEVELS (JPEG2000_MAX_DECLEVELS + 1)
72
73 #define JPEG2000_MAX_PASSES 100
74
75 // T1 flags
76 // flags determining significance of neighbor coefficients
77 #define JPEG2000_T1_SIG_N 0x0001
78 #define JPEG2000_T1_SIG_E 0x0002
79 #define JPEG2000_T1_SIG_W 0x0004
80 #define JPEG2000_T1_SIG_S 0x0008
81 #define JPEG2000_T1_SIG_NE 0x0010
82 #define JPEG2000_T1_SIG_NW 0x0020
83 #define JPEG2000_T1_SIG_SE 0x0040
84 #define JPEG2000_T1_SIG_SW 0x0080
85 #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | \
86 JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W | \
87 JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | \
88 JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
89 // flags determining sign bit of neighbor coefficients
90 #define JPEG2000_T1_SGN_N 0x0100
91 #define JPEG2000_T1_SGN_S 0x0200
92 #define JPEG2000_T1_SGN_W 0x0400
93 #define JPEG2000_T1_SGN_E 0x0800
94
95 #define JPEG2000_T1_VIS 0x1000
96 #define JPEG2000_T1_SIG 0x2000
97 #define JPEG2000_T1_REF 0x4000
98
99 #define JPEG2000_T1_SGN 0x8000
100
101 // Codeblock coding styles
102 #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
103 #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
104 #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
105 #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
106 #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
107 #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
108
109 // Coding styles
110 #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
111 #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
112 #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
113 #define JPEG2000_CTSY_HTJ2K_F 0x40 // Only HT code-blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) are present
114 #define JPEG2000_CTSY_HTJ2K_M 0xC0 // HT code-blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) can be present
115
116 // Progression orders
117 #define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
118 #define JPEG2000_PGOD_RLCP 0x01 // Resolution level-layer-component-position progression
119 #define JPEG2000_PGOD_RPCL 0x02 // Resolution level-position-component-layer progression
120 #define JPEG2000_PGOD_PCRL 0x03 // Position-component-resolution level-layer progression
121 #define JPEG2000_PGOD_CPRL 0x04 // Component-position-resolution level-layer progression
122
123 typedef struct Jpeg2000T1Context {
124 int data[6144];
125 uint16_t flags[6156];
126 MqcState mqc;
127 int stride;
128 } Jpeg2000T1Context;
129
130 typedef struct Jpeg2000TgtNode {
131 uint8_t val;
132 uint8_t temp_val;
133 uint8_t vis;
134 struct Jpeg2000TgtNode *parent;
135 } Jpeg2000TgtNode;
136
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 uint8_t init;
151 } Jpeg2000CodingStyle;
152
153 typedef struct Jpeg2000QuantStyle {
154 uint8_t expn[JPEG2000_MAX_DECLEVELS * 3]; // quantization exponent
155 uint16_t mant[JPEG2000_MAX_DECLEVELS * 3]; // quantization mantissa
156 uint8_t quantsty; // quantization style
157 uint8_t nguardbits; // number of guard bits
158 } Jpeg2000QuantStyle;
159
160 typedef struct Jpeg2000Pass {
161 uint16_t rate;
162 int64_t disto;
163 uint8_t flushed[4];
164 int flushed_len;
165 } Jpeg2000Pass;
166
167 typedef struct Jpeg2000Layer {
168 uint8_t *data_start;
169 int data_len;
170 int npasses;
171 double disto;
172 int cum_passes;
173 } Jpeg2000Layer;
174
175 typedef struct Jpeg2000Cblk {
176 uint8_t npasses;
177 uint8_t ninclpasses; // number coding of passes included in codestream
178 uint8_t nonzerobits;
179 uint8_t incl;
180 uint16_t length;
181 uint16_t *lengthinc;
182 uint8_t nb_lengthinc;
183 uint8_t lblock;
184 uint8_t *data;
185 size_t data_allocated;
186 int nb_terminations;
187 int nb_terminationsinc;
188 int *data_start;
189 Jpeg2000Pass *passes;
190 Jpeg2000Layer *layers;
191 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
192 /* specific to HT code-blocks */
193 int zbp;
194 int pass_lengths[2];
195 } Jpeg2000Cblk; // code block
196
197 typedef struct Jpeg2000Prec {
198 int nb_codeblocks_width;
199 int nb_codeblocks_height;
200 Jpeg2000TgtNode *zerobits;
201 Jpeg2000TgtNode *cblkincl;
202 Jpeg2000Cblk *cblk;
203 int decoded_layers;
204 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
205 } Jpeg2000Prec; // precinct
206
207 typedef struct Jpeg2000Band {
208 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
209 uint16_t log2_cblk_width, log2_cblk_height;
210 int i_stepsize; // quantization stepsize
211 float f_stepsize; // quantization stepsize
212 Jpeg2000Prec *prec;
213 } Jpeg2000Band; // subband
214
215 typedef struct Jpeg2000ResLevel {
216 uint8_t nbands;
217 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
218 int num_precincts_x, num_precincts_y; // number of precincts in x/y direction
219 uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
220 Jpeg2000Band *band;
221 } Jpeg2000ResLevel; // resolution level
222
223 typedef struct Jpeg2000Component {
224 Jpeg2000ResLevel *reslevel;
225 DWTContext dwt;
226 float *f_data;
227 int *i_data;
228 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
229 int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
230 uint8_t roi_shift; // ROI scaling value for the component
231 } Jpeg2000Component;
232
233 /* misc tools */
234 2157088 static inline int ff_jpeg2000_ceildivpow2(int a, int b)
235 {
236 2157088 return -((-(int64_t)a) >> b);
237 }
238
239 1336688 static inline int ff_jpeg2000_ceildiv(int a, int64_t b)
240 {
241 1336688 return (a + b - 1) / b;
242 }
243
244 /* TIER-1 routines */
245
246 /* Set up lookup tables used in TIER-1. */
247 void ff_jpeg2000_init_tier1_luts(void);
248
249 /* Update significance of a coefficient at current position (x,y) and
250 * for neighbors. */
251 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1,
252 int x, int y, int negative);
253
254 extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
255
256 /* Get context label (number in range[0..8]) of a coefficient for significance
257 * propagation and cleanup coding passes. */
258 976497114 static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
259 {
260 976497114 return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
261 }
262
263 static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
264
265 /* Get context label (number in range[14..16]) of a coefficient for magnitude
266 * refinement pass. */
267 966794392 static inline int ff_jpeg2000_getrefctxno(int flag)
268 {
269 966794392 return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
270 }
271
272 extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
273 extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
274
275 /* Get context label (number in range[9..13]) for sign decoding. */
276 264927998 static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
277 {
278 264927998 *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
279 264927998 return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
280 }
281
282 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
283 Jpeg2000CodingStyle *codsty,
284 Jpeg2000QuantStyle *qntsty,
285 int cbps, int dx, int dy,
286 AVCodecContext *ctx);
287
288 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
289
290 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
291
292 22747628 static inline int needs_termination(int style, int passno) {
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22747628 times.
22747628 if (style & JPEG2000_CBLK_BYPASS) {
294 int type = passno % 3;
295 passno /= 3;
296 if (type == 0 && passno > 2)
297 return 2;
298 if (type == 2 && passno > 2)
299 return 1;
300 if (style & JPEG2000_CBLK_TERMALL) {
301 return passno > 2 ? 2 : 1;
302 }
303 }
304
2/2
✓ Branch 0 taken 8023 times.
✓ Branch 1 taken 22739605 times.
22747628 if (style & JPEG2000_CBLK_TERMALL)
305 8023 return 1;
306 22739605 return 0;
307 }
308
309 void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val);
310
311 #endif /* AVCODEC_JPEG2000_H */
312