FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000.c
Date: 2024-10-04 17:46:48
Exec Total Coverage
Lines: 310 348 89.1%
Functions: 15 15 100.0%
Branches: 164 198 82.8%

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