Coverage Report

Created: 2026-02-14 06:59

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