FFmpeg coverage


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