Coverage Report

Created: 2025-12-31 07:57

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