FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 306 338 90.5%
Functions: 14 14 100.0%
Branches: 158 188 84.0%

Line Branch Exec Source
1 /*
2 * JPEG 2000 encoder and decoder common 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 /**
24 * @file
25 * JPEG 2000 image encoder and decoder common functions
26 */
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intfloat.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/thread.h"
35 #include "avcodec.h"
36 #include "jpeg2000.h"
37
38 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
39
40 /* tag tree routines */
41
42 /* allocate the memory for tag tree */
43 348446 static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
44 {
45 348446 int pw = w, ph = h;
46 Jpeg2000TgtNode *res, *t, *t2;
47 int32_t tt_size;
48
49 348446 tt_size = ff_jpeg2000_tag_tree_size(w, h);
50
51 348446 t = res = av_calloc(tt_size, sizeof(*t));
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 348446 times.
348446 if (!res)
53 ✗ return NULL;
54
55
4/4
✓ Branch 0 taken 227018 times.
✓ Branch 1 taken 384756 times.
✓ Branch 2 taken 36310 times.
✓ Branch 3 taken 348446 times.
611774 while (w > 1 || h > 1) {
56 int i, j;
57 263328 pw = w;
58 263328 ph = h;
59
60 263328 w = (w + 1) >> 1;
61 263328 h = (h + 1) >> 1;
62 263328 t2 = t + pw * ph;
63
64
2/2
✓ Branch 0 taken 664154 times.
✓ Branch 1 taken 263328 times.
927482 for (i = 0; i < ph; i++)
65
2/2
✓ Branch 0 taken 2277764 times.
✓ Branch 1 taken 664154 times.
2941918 for (j = 0; j < pw; j++)
66 2277764 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
67
68 263328 t = t2;
69 }
70 348446 t[0].parent = NULL;
71 348446 return res;
72 }
73
74 626600 void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
75 {
76 626600 int i, siz = ff_jpeg2000_tag_tree_size(w, h);
77
78
2/2
✓ Branch 0 taken 4940000 times.
✓ Branch 1 taken 626600 times.
5566600 for (i = 0; i < siz; i++) {
79 4940000 t[i].val = val;
80 4940000 t[i].temp_val = 0;
81 4940000 t[i].vis = 0;
82 }
83 626600 }
84
85 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
86
87 52224 static int getsigctxno(int flag, int bandno)
88 {
89 int h, v, d;
90
91 52224 h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
92 52224 ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
93 52224 v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
94 52224 ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
95 52224 d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
96 52224 ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
97 52224 ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
98 52224 ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
99
100
2/2
✓ Branch 0 taken 39168 times.
✓ Branch 1 taken 13056 times.
52224 if (bandno < 3) {
101
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 26112 times.
39168 if (bandno == 1)
102 13056 FFSWAP(int, h, v);
103
2/2
✓ Branch 0 taken 9792 times.
✓ Branch 1 taken 29376 times.
39168 if (h == 2) return 8;
104
2/2
✓ Branch 0 taken 19584 times.
✓ Branch 1 taken 9792 times.
29376 if (h == 1) {
105
2/2
✓ Branch 0 taken 14688 times.
✓ Branch 1 taken 4896 times.
19584 if (v >= 1) return 7;
106
2/2
✓ Branch 0 taken 4590 times.
✓ Branch 1 taken 306 times.
4896 if (d >= 1) return 6;
107 306 return 5;
108 }
109
2/2
✓ Branch 0 taken 2448 times.
✓ Branch 1 taken 7344 times.
9792 if (v == 2) return 4;
110
2/2
✓ Branch 0 taken 4896 times.
✓ Branch 1 taken 2448 times.
7344 if (v == 1) return 3;
111
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 765 times.
2448 if (d >= 2) return 2;
112
2/2
✓ Branch 0 taken 612 times.
✓ Branch 1 taken 153 times.
765 if (d == 1) return 1;
113 } else {
114
2/2
✓ Branch 0 taken 4080 times.
✓ Branch 1 taken 8976 times.
13056 if (d >= 3) return 8;
115
2/2
✓ Branch 0 taken 4896 times.
✓ Branch 1 taken 4080 times.
8976 if (d == 2) {
116
2/2
✓ Branch 0 taken 4590 times.
✓ Branch 1 taken 306 times.
4896 if (h+v >= 1) return 7;
117 306 return 6;
118 }
119
2/2
✓ Branch 0 taken 3264 times.
✓ Branch 1 taken 816 times.
4080 if (d == 1) {
120
2/2
✓ Branch 0 taken 2244 times.
✓ Branch 1 taken 1020 times.
3264 if (h+v >= 2) return 5;
121
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 204 times.
1020 if (h+v == 1) return 4;
122 204 return 3;
123 }
124
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 255 times.
816 if (h+v >= 2) return 2;
125
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 51 times.
255 if (h+v == 1) return 1;
126 }
127 204 return 0;
128 }
129
130 uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
131
132 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
133 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
134 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
135
136 13056 static int getsgnctxno(int flag, uint8_t *xorbit)
137 {
138 int vcontrib, hcontrib;
139
140
4/4
✓ Branch 0 taken 6528 times.
✓ Branch 1 taken 6528 times.
✓ Branch 2 taken 3264 times.
✓ Branch 3 taken 3264 times.
13056 hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
141
4/4
✓ Branch 0 taken 6528 times.
✓ Branch 1 taken 6528 times.
✓ Branch 2 taken 3264 times.
✓ Branch 3 taken 3264 times.
13056 [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
142
4/4
✓ Branch 0 taken 6528 times.
✓ Branch 1 taken 6528 times.
✓ Branch 2 taken 3264 times.
✓ Branch 3 taken 3264 times.
13056 vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
143
4/4
✓ Branch 0 taken 6528 times.
✓ Branch 1 taken 6528 times.
✓ Branch 2 taken 3264 times.
✓ Branch 3 taken 3264 times.
13056 [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
144 13056 *xorbit = xorbittab[hcontrib][vcontrib];
145
146 13056 return ctxlbltab[hcontrib][vcontrib];
147 }
148
149 51 static void av_cold jpeg2000_init_tier1_luts(void)
150 {
151 int i, j;
152
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 51 times.
13107 for (i = 0; i < 256; i++)
153
2/2
✓ Branch 0 taken 52224 times.
✓ Branch 1 taken 13056 times.
65280 for (j = 0; j < 4; j++)
154 52224 ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
155
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 51 times.
867 for (i = 0; i < 16; i++)
156
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 816 times.
13872 for (j = 0; j < 16; j++)
157 13056 ff_jpeg2000_sgnctxno_lut[i][j] =
158 13056 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
159 51 }
160
161 85 void av_cold ff_jpeg2000_init_tier1_luts(void)
162 {
163 static AVOnce init_static_once = AV_ONCE_INIT;
164 85 ff_thread_once(&init_static_once, jpeg2000_init_tier1_luts);
165 85 }
166
167 270943980 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
168 int negative)
169 {
170 270943980 x++;
171 270943980 y++;
172 270943980 t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
173
2/2
✓ Branch 0 taken 132721627 times.
✓ Branch 1 taken 138222353 times.
270943980 if (negative) {
174 132721627 t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
175 132721627 t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
176 132721627 t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
177 132721627 t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
178 } else {
179 138222353 t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
180 138222353 t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
181 138222353 t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
182 138222353 t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
183 }
184 270943980 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
185 270943980 t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
186 270943980 t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
187 270943980 t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
188 270943980 }
189
190 /* TODO: Implementation of quantization step not finished, see
191 * ISO/IEC 15444-1:2002 E.1 and A.6.4. */
192 // static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
193 172090 float ff_jpeg2000_band_stepsize(AVCodecContext *avctx,
194 Jpeg2000CodingStyle *codsty,
195 Jpeg2000QuantStyle *qntsty,
196 int bandno, int gbandno,
197 int reslevelno, int cbps)
198 {
199 float fss;
200
201
2/3
✓ Branch 0 taken 133986 times.
✓ Branch 1 taken 38104 times.
✗ Branch 2 not taken.
172090 switch (qntsty->quantsty) {
202 uint8_t gain;
203 133986 case JPEG2000_QSTY_NONE:
204 /* TODO: to verify. No quantization in this case */
205 133986 fss = 1;
206 133986 break;
207 38104 case JPEG2000_QSTY_SI:
208 /*TODO: Compute formula to implement. */
209 // numbps = cbps +
210 // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
211 // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
212 // 2 + numbps - qntsty->expn[gbandno]);
213 // break;
214 case JPEG2000_QSTY_SE:
215 /* Exponent quantization step.
216 * Formula:
217 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
218 * R_b = R_I + log2 (gain_b )
219 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
220 38104 gain = cbps;
221 38104 fss = ff_jpeg2000_exp2fi(gain - qntsty->expn[gbandno]);
222 38104 fss *= qntsty->mant[gbandno] / 2048.0 + 1.0;
223 38104 break;
224 ✗ default:
225 ✗ av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
226 ✗ return 0;
227 }
228
2/2
✓ Branch 0 taken 38104 times.
✓ Branch 1 taken 133986 times.
172090 if (codsty->transform != FF_DWT53) {
229 38104 int lband = 0;
230
3/3
✓ Branch 0 taken 24064 times.
✓ Branch 1 taken 12032 times.
✓ Branch 2 taken 2008 times.
38104 switch (bandno + (reslevelno > 0)) {
231 24064 case 1:
232 case 2:
233 24064 fss *= F_LFTG_X * 2;
234 24064 lband = 1;
235 24064 break;
236 12032 case 3:
237 12032 fss *= F_LFTG_X * F_LFTG_X * 4;
238 12032 break;
239 }
240 38104 fss *= pow(F_LFTG_K, 2 * (codsty->nreslevels2decode - reslevelno) +
241 38104 lband - 2);
242 }
243
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172090 times.
172090 if (fss > (INT_MAX >> 15)) {
245 ✗ av_log(avctx, AV_LOG_ERROR, "stepsize out of range\n");
246 ✗ return 0;
247 }
248 172090 return fss;
249 }
250
251 172090 static void init_band_stepsize(AVCodecContext *avctx,
252 Jpeg2000Band *band,
253 Jpeg2000CodingStyle *codsty,
254 Jpeg2000QuantStyle *qntsty,
255 int bandno, int gbandno, int reslevelno,
256 int cbps)
257 {
258 172090 band->f_stepsize = ff_jpeg2000_band_stepsize(avctx, codsty, qntsty,
259 bandno, gbandno, reslevelno,
260 cbps);
261 172090 band->i_stepsize = (int)floorf(band->f_stepsize * (1 << 15));
262 172090 }
263
264 174223 static int init_prec(AVCodecContext *avctx,
265 Jpeg2000Band *band,
266 Jpeg2000ResLevel *reslevel,
267 Jpeg2000Component *comp,
268 Jpeg2000CodingStyle *codsty,
269 int precno, int bandno, int reslevelno,
270 int log2_band_prec_width,
271 int log2_band_prec_height)
272 {
273 174223 Jpeg2000Prec *prec = band->prec + precno;
274 int nb_codeblocks, cblkno;
275
276 174223 prec->decoded_layers = 0;
277
278 /* TODO: Explain formula for JPEG200 DCINEMA. */
279 /* TODO: Verify with previous count of codeblocks per band */
280
281 /* Compute P_x0 */
282 174223 prec->coord[0][0] = ((reslevel->coord[0][0] >> reslevel->log2_prec_width) + precno % reslevel->num_precincts_x) *
283 (1 << log2_band_prec_width);
284
285 /* Compute P_y0 */
286 174223 prec->coord[1][0] = ((reslevel->coord[1][0] >> reslevel->log2_prec_height) + precno / reslevel->num_precincts_x) *
287 (1 << log2_band_prec_height);
288
289 /* Compute P_x1 */
290 174223 prec->coord[0][1] = prec->coord[0][0] +
291 174223 (1 << log2_band_prec_width);
292 174223 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
293 174223 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
294
295 /* Compute P_y1 */
296 174223 prec->coord[1][1] = prec->coord[1][0] +
297 174223 (1 << log2_band_prec_height);
298 174223 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
299 174223 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
300
301 174223 prec->nb_codeblocks_width =
302 174223 ff_jpeg2000_ceildivpow2(prec->coord[0][1],
303 174223 band->log2_cblk_width)
304 174223 - (prec->coord[0][0] >> band->log2_cblk_width);
305 174223 prec->nb_codeblocks_height =
306 174223 ff_jpeg2000_ceildivpow2(prec->coord[1][1],
307 174223 band->log2_cblk_height)
308 174223 - (prec->coord[1][0] >> band->log2_cblk_height);
309
310
311 /* Tag trees initialization */
312 174223 prec->cblkincl =
313 174223 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
314 prec->nb_codeblocks_height);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174223 times.
174223 if (!prec->cblkincl)
316 ✗ return AVERROR(ENOMEM);
317
318 174223 prec->zerobits =
319 174223 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
320 prec->nb_codeblocks_height);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174223 times.
174223 if (!prec->zerobits)
322 ✗ return AVERROR(ENOMEM);
323
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174223 times.
174223 if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
325 ✗ prec->cblk = NULL;
326 ✗ return AVERROR(ENOMEM);
327 }
328 174223 nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
329 174223 prec->cblk = av_calloc(nb_codeblocks, sizeof(*prec->cblk));
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174223 times.
174223 if (!prec->cblk)
331 ✗ return AVERROR(ENOMEM);
332
2/2
✓ Branch 0 taken 960183 times.
✓ Branch 1 taken 174223 times.
1134406 for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
333 960183 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
334 int Cx0, Cy0;
335
336 /* Compute coordinates of codeblocks */
337 /* Compute Cx0*/
338 960183 Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
339 960183 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
340 960183 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
341
342 /* Compute Cy0*/
343 960183 Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
344 960183 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
345 960183 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
346
347 /* Compute Cx1 */
348 960183 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
349 prec->coord[0][1]);
350
351 /* Compute Cy1 */
352 960183 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
353 prec->coord[1][1]);
354 /* Update code-blocks coordinates according sub-band position */
355
2/2
✓ Branch 0 taken 633678 times.
✓ Branch 1 taken 326505 times.
960183 if ((bandno + !!reslevelno) & 1) {
356 633678 cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
357 633678 comp->reslevel[reslevelno-1].coord[0][0];
358 633678 cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
359 633678 comp->reslevel[reslevelno-1].coord[0][0];
360 }
361
2/2
✓ Branch 0 taken 633853 times.
✓ Branch 1 taken 326330 times.
960183 if ((bandno + !!reslevelno) & 2) {
362 633853 cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
363 633853 comp->reslevel[reslevelno-1].coord[1][0];
364 633853 cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
365 633853 comp->reslevel[reslevelno-1].coord[1][0];
366 }
367
368 960183 cblk->lblock = 3;
369 960183 cblk->length = 0;
370 960183 cblk->npasses = 0;
371
2/2
✓ Branch 1 taken 18070 times.
✓ Branch 2 taken 942113 times.
960183 if (av_codec_is_encoder(avctx->codec)) {
372 18070 cblk->layers = av_calloc(codsty->nlayers, sizeof(*cblk->layers));
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18070 times.
18070 if (!cblk->layers)
374 ✗ return AVERROR(ENOMEM);
375 }
376 }
377
378 174223 return 0;
379 }
380
381 172090 static int init_band(AVCodecContext *avctx,
382 Jpeg2000ResLevel *reslevel,
383 Jpeg2000Component *comp,
384 Jpeg2000CodingStyle *codsty,
385 Jpeg2000QuantStyle *qntsty,
386 int bandno, int gbandno, int reslevelno,
387 const int cbps, int dx, int dy)
388 {
389 172090 Jpeg2000Band *band = reslevel->band + bandno;
390 uint8_t log2_band_prec_width, log2_band_prec_height;
391 172090 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
392 int precno;
393 int nb_precincts;
394 int i, j, ret;
395
396 172090 init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
397
398 /* computation of tbx_0, tbx_1, tby_0, tby_1
399 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
400 * codeblock width and height is computed for
401 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
402
2/2
✓ Branch 0 taken 9439 times.
✓ Branch 1 taken 162651 times.
172090 if (reslevelno == 0) {
403 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
404
2/2
✓ Branch 0 taken 18878 times.
✓ Branch 1 taken 9439 times.
28317 for (i = 0; i < 2; i++)
405
2/2
✓ Branch 0 taken 37756 times.
✓ Branch 1 taken 18878 times.
56634 for (j = 0; j < 2; j++)
406 37756 band->coord[i][j] =
407 37756 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
408 declvl - 1);
409 9439 log2_band_prec_width = reslevel->log2_prec_width;
410 9439 log2_band_prec_height = reslevel->log2_prec_height;
411 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
412 9439 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
413 reslevel->log2_prec_width);
414 9439 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
415 reslevel->log2_prec_height);
416 } else {
417 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
418 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
419
2/2
✓ Branch 0 taken 325302 times.
✓ Branch 1 taken 162651 times.
487953 for (i = 0; i < 2; i++)
420
2/2
✓ Branch 0 taken 650604 times.
✓ Branch 1 taken 325302 times.
975906 for (j = 0; j < 2; j++)
421 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
422 650604 band->coord[i][j] =
423 650604 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
424 650604 (((bandno + 1 >> i) & 1LL) << declvl - 1),
425 declvl);
426 /* TODO: Manage case of 3 band offsets here or
427 * in coding/decoding function? */
428
429 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162651 times.
162651 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
431 reslevel->log2_prec_width - 1);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162651 times.
162651 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
433 reslevel->log2_prec_height - 1);
434
435 162651 log2_band_prec_width = reslevel->log2_prec_width - 1;
436 162651 log2_band_prec_height = reslevel->log2_prec_height - 1;
437 }
438
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172090 times.
172090 if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
440 ✗ band->prec = NULL;
441 ✗ return AVERROR(ENOMEM);
442 }
443 172090 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
444 172090 band->prec = av_calloc(nb_precincts, sizeof(*band->prec));
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172090 times.
172090 if (!band->prec)
446 ✗ return AVERROR(ENOMEM);
447
448
2/2
✓ Branch 0 taken 174223 times.
✓ Branch 1 taken 172090 times.
346313 for (precno = 0; precno < nb_precincts; precno++) {
449 174223 ret = init_prec(avctx, band, reslevel, comp, codsty,
450 precno, bandno, reslevelno,
451 log2_band_prec_width, log2_band_prec_height);
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174223 times.
174223 if (ret < 0)
453 ✗ return ret;
454 }
455
456 172090 return 0;
457 }
458
459 9439 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
460 Jpeg2000CodingStyle *codsty,
461 Jpeg2000QuantStyle *qntsty,
462 const int cbps, int dx, int dy,
463 AVCodecContext *avctx)
464 {
465 9439 int reslevelno, bandno, gbandno = 0, ret, i, j;
466 uint32_t csize;
467
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9439 times.
9439 if (codsty->nreslevels2decode <= 0) {
469 ✗ av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
470 ✗ return AVERROR_INVALIDDATA;
471 }
472
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9439 times.
9439 if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
474 9439 codsty->nreslevels2decode - 1,
475 9439 codsty->transform))
476 ✗ return ret;
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9439 times.
9439 if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
479 9439 comp->coord[1][1] - comp->coord[1][0], 0, avctx))
480 ✗ return AVERROR_INVALIDDATA;
481 9439 csize = (comp->coord[0][1] - comp->coord[0][0]) *
482 9439 (comp->coord[1][1] - comp->coord[1][0]);
483
1/2
✓ Branch 0 taken 9439 times.
✗ Branch 1 not taken.
9439 if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9439 times.
9439 comp->coord[1][1] - comp->coord[1][0] > 32768) {
485 ✗ av_log(avctx, AV_LOG_ERROR, "component size too large\n");
486 ✗ return AVERROR_PATCHWELCOME;
487 }
488
489
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9433 times.
9439 if (codsty->transform == FF_DWT97) {
490 6 csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
491 6 comp->i_data = NULL;
492 6 comp->f_data = av_calloc(csize, sizeof(*comp->f_data));
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!comp->f_data)
494 ✗ return AVERROR(ENOMEM);
495 } else {
496 9433 csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
497 9433 comp->f_data = NULL;
498 9433 comp->i_data = av_calloc(csize, sizeof(*comp->i_data));
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9433 times.
9433 if (!comp->i_data)
500 ✗ return AVERROR(ENOMEM);
501 }
502 9439 comp->reslevel = av_calloc(codsty->nreslevels, sizeof(*comp->reslevel));
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9439 times.
9439 if (!comp->reslevel)
504 ✗ return AVERROR(ENOMEM);
505 /* LOOP on resolution levels */
506
2/2
✓ Branch 0 taken 63656 times.
✓ Branch 1 taken 9439 times.
73095 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
507 63656 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
508 63656 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
509
510 /* Compute borders for each resolution level.
511 * Computation of trx_0, trx_1, try_0 and try_1.
512 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
513
2/2
✓ Branch 0 taken 127312 times.
✓ Branch 1 taken 63656 times.
190968 for (i = 0; i < 2; i++)
514
2/2
✓ Branch 0 taken 254624 times.
✓ Branch 1 taken 127312 times.
381936 for (j = 0; j < 2; j++)
515 254624 reslevel->coord[i][j] =
516 254624 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
517 // update precincts size: 2^n value
518 63656 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
519 63656 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
520
521 /* Number of bands for each resolution level */
522
2/2
✓ Branch 0 taken 9439 times.
✓ Branch 1 taken 54217 times.
63656 if (reslevelno == 0)
523 9439 reslevel->nbands = 1;
524 else
525 54217 reslevel->nbands = 3;
526
527 /* Number of precincts which span the tile for resolution level reslevelno
528 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
529 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
530 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
531 * for Dcinema profiles in JPEG 2000
532 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
533 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63656 times.
63656 if (reslevel->coord[0][1] == reslevel->coord[0][0])
535 ✗ reslevel->num_precincts_x = 0;
536 else
537 63656 reslevel->num_precincts_x =
538 63656 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
539 63656 reslevel->log2_prec_width) -
540 63656 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
541
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63656 times.
63656 if (reslevel->coord[1][1] == reslevel->coord[1][0])
543 ✗ reslevel->num_precincts_y = 0;
544 else
545 63656 reslevel->num_precincts_y =
546 63656 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
547 63656 reslevel->log2_prec_height) -
548 63656 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
549
550 63656 reslevel->band = av_calloc(reslevel->nbands, sizeof(*reslevel->band));
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63656 times.
63656 if (!reslevel->band)
552 ✗ return AVERROR(ENOMEM);
553
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63656 times.
63656 if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y * reslevel->nbands > avctx->max_pixels / sizeof(*reslevel->band->prec))
555 ✗ return AVERROR(ENOMEM);
556
557
2/2
✓ Branch 0 taken 172090 times.
✓ Branch 1 taken 63656 times.
235746 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
558 172090 ret = init_band(avctx, reslevel,
559 comp, codsty, qntsty,
560 bandno, gbandno, reslevelno,
561 cbps, dx, dy);
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172090 times.
172090 if (ret < 0)
563 ✗ return ret;
564 }
565 }
566 9439 return 0;
567 }
568
569 8450 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
570 {
571 int reslevelno, bandno, cblkno, precno;
572
2/2
✓ Branch 0 taken 59150 times.
✓ Branch 1 taken 8450 times.
67600 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
573 59150 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
574
2/2
✓ Branch 0 taken 160550 times.
✓ Branch 1 taken 59150 times.
219700 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
575 160550 Jpeg2000Band *band = rlevel->band + bandno;
576
2/2
✓ Branch 0 taken 160550 times.
✓ Branch 1 taken 160550 times.
321100 for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
577 160550 Jpeg2000Prec *prec = band->prec + precno;
578 160550 ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 0);
579 160550 ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 0);
580
2/2
✓ Branch 0 taken 903500 times.
✓ Branch 1 taken 160550 times.
1064050 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
581 903500 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
582 903500 cblk->length = 0;
583 903500 cblk->lblock = 3;
584 }
585 }
586 }
587 }
588 8450 }
589
590 10433 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
591 {
592 int reslevelno, bandno, precno;
593 10433 for (reslevelno = 0;
594
4/4
✓ Branch 0 taken 73095 times.
✓ Branch 1 taken 994 times.
✓ Branch 2 taken 63656 times.
✓ Branch 3 taken 9439 times.
74089 comp->reslevel && reslevelno < codsty->nreslevels;
595 63656 reslevelno++) {
596 Jpeg2000ResLevel *reslevel;
597
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63656 times.
63656 if (!comp->reslevel)
599 ✗ continue;
600
601 63656 reslevel = comp->reslevel + reslevelno;
602
2/2
✓ Branch 0 taken 172090 times.
✓ Branch 1 taken 63656 times.
235746 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
603 Jpeg2000Band *band;
604
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172090 times.
172090 if (!reslevel->band)
606 ✗ continue;
607
608 172090 band = reslevel->band + bandno;
609
2/2
✓ Branch 0 taken 174223 times.
✓ Branch 1 taken 172090 times.
346313 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
610
1/2
✓ Branch 0 taken 174223 times.
✗ Branch 1 not taken.
174223 if (band->prec) {
611 174223 Jpeg2000Prec *prec = band->prec + precno;
612 174223 int nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
613
614 174223 av_freep(&prec->zerobits);
615 174223 av_freep(&prec->cblkincl);
616
1/2
✓ Branch 0 taken 174223 times.
✗ Branch 1 not taken.
174223 if (prec->cblk) {
617 int cblkno;
618
2/2
✓ Branch 0 taken 960183 times.
✓ Branch 1 taken 174223 times.
1134406 for (cblkno = 0; cblkno < nb_code_blocks; cblkno ++) {
619 960183 Jpeg2000Cblk *cblk = &prec->cblk[cblkno];
620 960183 av_freep(&cblk->data);
621 960183 av_freep(&cblk->passes);
622 960183 av_freep(&cblk->lengthinc);
623 960183 av_freep(&cblk->data_start);
624 960183 av_freep(&cblk->layers);
625 }
626 174223 av_freep(&prec->cblk);
627 }
628 }
629 }
630
631 172090 av_freep(&band->prec);
632 }
633 63656 av_freep(&reslevel->band);
634 }
635
636 10433 ff_dwt_destroy(&comp->dwt);
637 10433 av_freep(&comp->reslevel);
638 10433 av_freep(&comp->i_data);
639 10433 av_freep(&comp->f_data);
640 10433 }
641