GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/jpeg2000.c Lines: 310 343 90.4 %
Date: 2020-10-23 17:01:47 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 "avcodec.h"
34
#include "internal.h"
35
#include "jpeg2000.h"
36
37
#define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
38
39
/* tag tree routines */
40
41
/* allocate the memory for tag tree */
42
446734
int32_t ff_tag_tree_size(int w, int h)
43
{
44
446734
    int64_t res = 0;
45

803628
    while (w > 1 || h > 1) {
46
356894
        res += w * (int64_t)h;
47
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
157534
static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
55
{
56
157534
    int pw = w, ph = h;
57
    Jpeg2000TgtNode *res, *t, *t2;
58
    int32_t tt_size;
59
60
157534
    tt_size = ff_tag_tree_size(w, h);
61
62
157534
    t = res = av_mallocz_array(tt_size, sizeof(*t));
63
157534
    if (!res)
64
        return NULL;
65
66

284028
    while (w > 1 || h > 1) {
67
        int i, j;
68
126494
        pw = w;
69
126494
        ph = h;
70
71
126494
        w  = (w + 1) >> 1;
72
126494
        h  = (h + 1) >> 1;
73
126494
        t2 = t + pw * ph;
74
75
445158
        for (i = 0; i < ph; i++)
76
1408754
            for (j = 0; j < pw; j++)
77
1090090
                t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
78
79
126494
        t = t2;
80
    }
81
157534
    t[0].parent = NULL;
82
157534
    return res;
83
}
84
85
289200
void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
86
{
87
289200
    int i, siz = ff_tag_tree_size(w, h);
88
89
2569200
    for (i = 0; i < siz; i++) {
90
2280000
        t[i].val = val;
91
2280000
        t[i].temp_val = 0;
92
2280000
        t[i].vis = 0;
93
    }
94
289200
}
95
96
uint8_t ff_jpeg2000_sigctxno_lut[256][4];
97
98
18432
static int getsigctxno(int flag, int bandno)
99
{
100
    int h, v, d;
101
102
18432
    h = ((flag & JPEG2000_T1_SIG_E)  ? 1 : 0) +
103
18432
        ((flag & JPEG2000_T1_SIG_W)  ? 1 : 0);
104
18432
    v = ((flag & JPEG2000_T1_SIG_N)  ? 1 : 0) +
105
18432
        ((flag & JPEG2000_T1_SIG_S)  ? 1 : 0);
106
18432
    d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
107
18432
        ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
108
18432
        ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
109
18432
        ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
110
111
18432
    if (bandno < 3) {
112
13824
        if (bandno == 1)
113
4608
            FFSWAP(int, h, v);
114
13824
        if (h == 2) return 8;
115
10368
        if (h == 1) {
116
6912
            if (v >= 1) return 7;
117
1728
            if (d >= 1) return 6;
118
108
            return 5;
119
        }
120
3456
        if (v == 2) return 4;
121
2592
        if (v == 1) return 3;
122
864
        if (d >= 2) return 2;
123
270
        if (d == 1) return 1;
124
    } else {
125
4608
        if (d >= 3) return 8;
126
3168
        if (d == 2) {
127
1728
            if (h+v >= 1) return 7;
128
108
            return 6;
129
        }
130
1440
        if (d == 1) {
131
1152
            if (h+v >= 2) return 5;
132
360
            if (h+v == 1) return 4;
133
72
            return 3;
134
        }
135
288
        if (h+v >= 2) return 2;
136
90
        if (h+v == 1) return 1;
137
    }
138
72
    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
4608
static int getsgnctxno(int flag, uint8_t *xorbit)
148
{
149
    int vcontrib, hcontrib;
150
151

4608
    hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
152

4608
                         [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
153

4608
    vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
154

4608
                         [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
155
4608
    *xorbit = xorbittab[hcontrib][vcontrib];
156
157
4608
    return ctxlbltab[hcontrib][vcontrib];
158
}
159
160
18
void av_cold ff_jpeg2000_init_tier1_luts(void)
161
{
162
    int i, j;
163
4626
    for (i = 0; i < 256; i++)
164
23040
        for (j = 0; j < 4; j++)
165
18432
            ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
166
306
    for (i = 0; i < 16; i++)
167
4896
        for (j = 0; j < 16; j++)
168
4608
            ff_jpeg2000_sgnctxno_lut[i][j] =
169
4608
                getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
170
18
}
171
172
97353026
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
173
                                  int negative)
174
{
175
97353026
    x++;
176
97353026
    y++;
177
97353026
    t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
178
97353026
    if (negative) {
179
46169688
        t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
180
46169688
        t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
181
46169688
        t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
182
46169688
        t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
183
    } else {
184
51183338
        t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
185
51183338
        t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
186
51183338
        t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
187
51183338
        t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
188
    }
189
97353026
    t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
190
97353026
    t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
191
97353026
    t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
192
97353026
    t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
193
97353026
}
194
195
// static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
196
197
77336
static void init_band_stepsize(AVCodecContext *avctx,
198
                               Jpeg2000Band *band,
199
                               Jpeg2000CodingStyle *codsty,
200
                               Jpeg2000QuantStyle *qntsty,
201
                               int bandno, int gbandno, int reslevelno,
202
                               int cbps)
203
{
204
    /* TODO: Implementation of quantization step not finished,
205
     * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
206
77336
    switch (qntsty->quantsty) {
207
        uint8_t gain;
208
38532
    case JPEG2000_QSTY_NONE:
209
        /* TODO: to verify. No quantization in this case */
210
38532
        band->f_stepsize = 1;
211
38532
        break;
212
38804
    case JPEG2000_QSTY_SI:
213
        /*TODO: Compute formula to implement. */
214
//         numbps = cbps +
215
//                  lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
216
//         band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
217
//                                2 + numbps - qntsty->expn[gbandno]);
218
//         break;
219
    case JPEG2000_QSTY_SE:
220
        /* Exponent quantization step.
221
         * Formula:
222
         * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
223
         * R_b = R_I + log2 (gain_b )
224
         * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
225
38804
        gain            = cbps;
226
38804
        band->f_stepsize  = ff_exp2fi(gain - qntsty->expn[gbandno]);
227
38804
        band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
228
38804
        break;
229
    default:
230
        band->f_stepsize = 0;
231
        av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
232
        break;
233
    }
234
77336
    if (codsty->transform != FF_DWT53) {
235
38804
        int lband = 0;
236
38804
        switch (bandno + (reslevelno > 0)) {
237
24506
            case 1:
238
            case 2:
239
24506
                band->f_stepsize *= F_LFTG_X * 2;
240
24506
                lband = 1;
241
24506
                break;
242
12253
            case 3:
243
12253
                band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
244
12253
                break;
245
        }
246
38804
        if (codsty->transform == FF_DWT97) {
247
128
            band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
248
        }
249
    }
250
251
77336
    if (band->f_stepsize > (INT_MAX >> 15)) {
252
        band->f_stepsize = 0;
253
        av_log(avctx, AV_LOG_ERROR, "stepsize out of range\n");
254
    }
255
256
77336
    band->i_stepsize = band->f_stepsize * (1 << 15);
257
258
    /* FIXME: In OpenJPEG code stepsize = stepsize * 0.5. Why?
259
     * If not set output of entropic decoder is not correct. */
260
77336
    if (!av_codec_is_encoder(avctx->codec))
261
75854
        band->f_stepsize *= 0.5;
262
77336
}
263
264
78767
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
78767
    Jpeg2000Prec *prec = band->prec + precno;
274
    int nb_codeblocks, cblkno;
275
276
78767
    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
78767
    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
78767
    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
78767
    prec->coord[0][1] = prec->coord[0][0] +
291
78767
                        (1 << log2_band_prec_width);
292
78767
    prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
293
78767
    prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
294
295
    /* Compute P_y1 */
296
78767
    prec->coord[1][1] = prec->coord[1][0] +
297
78767
                        (1 << log2_band_prec_height);
298
78767
    prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
299
78767
    prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
300
301
78767
    prec->nb_codeblocks_width =
302
78767
        ff_jpeg2000_ceildivpow2(prec->coord[0][1],
303
78767
                                band->log2_cblk_width)
304
78767
        - (prec->coord[0][0] >> band->log2_cblk_width);
305
78767
    prec->nb_codeblocks_height =
306
78767
        ff_jpeg2000_ceildivpow2(prec->coord[1][1],
307
78767
                                band->log2_cblk_height)
308
78767
        - (prec->coord[1][0] >> band->log2_cblk_height);
309
310
311
    /* Tag trees initialization */
312
78767
    prec->cblkincl =
313
78767
        ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
314
                                  prec->nb_codeblocks_height);
315
78767
    if (!prec->cblkincl)
316
        return AVERROR(ENOMEM);
317
318
78767
    prec->zerobits =
319
78767
        ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
320
                                  prec->nb_codeblocks_height);
321
78767
    if (!prec->zerobits)
322
        return AVERROR(ENOMEM);
323
324
78767
    if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
325
        prec->cblk = NULL;
326
        return AVERROR(ENOMEM);
327
    }
328
78767
    nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
329
78767
    prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
330
78767
    if (!prec->cblk)
331
        return AVERROR(ENOMEM);
332
533858
    for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
333
455091
        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
334
        int Cx0, Cy0;
335
336
        /* Compute coordinates of codeblocks */
337
        /* Compute Cx0*/
338
455091
        Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
339
455091
        Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << band->log2_cblk_width);
340
455091
        cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
341
342
        /* Compute Cy0*/
343
455091
        Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
344
455091
        Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << band->log2_cblk_height);
345
455091
        cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
346
347
        /* Compute Cx1 */
348
455091
        cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
349
                                  prec->coord[0][1]);
350
351
        /* Compute Cy1 */
352
455091
        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
455091
        if ((bandno + !!reslevelno) & 1) {
356
300658
            cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
357
300658
                                 comp->reslevel[reslevelno-1].coord[0][0];
358
300658
            cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
359
300658
                                 comp->reslevel[reslevelno-1].coord[0][0];
360
        }
361
455091
        if ((bandno + !!reslevelno) & 2) {
362
300658
            cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
363
300658
                                 comp->reslevel[reslevelno-1].coord[1][0];
364
300658
            cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
365
300658
                                 comp->reslevel[reslevelno-1].coord[1][0];
366
        }
367
368
455091
        cblk->lblock    = 3;
369
455091
        cblk->length    = 0;
370
455091
        cblk->npasses   = 0;
371
455091
        if (av_codec_is_encoder(avctx->codec)) {
372
8340
            cblk->layers = av_mallocz_array(codsty->nlayers, sizeof(*cblk->layers));
373
8340
            if (!cblk->layers)
374
                return AVERROR(ENOMEM);
375
        }
376
    }
377
378
78767
    return 0;
379
}
380
381
77336
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
                     int cbps, int dx, int dy)
388
{
389
77336
    Jpeg2000Band *band = reslevel->band + bandno;
390
    uint8_t log2_band_prec_width, log2_band_prec_height;
391
77336
    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
77336
    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
77336
    if (reslevelno == 0) {
403
        /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
404
12219
        for (i = 0; i < 2; i++)
405
24438
            for (j = 0; j < 2; j++)
406
16292
                band->coord[i][j] =
407
16292
                    ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
408
                                            declvl - 1);
409
4073
        log2_band_prec_width  = reslevel->log2_prec_width;
410
4073
        log2_band_prec_height = reslevel->log2_prec_height;
411
        /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
412
4073
        band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
413
                                       reslevel->log2_prec_width);
414
4073
        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
219789
        for (i = 0; i < 2; i++)
420
439578
            for (j = 0; j < 2; j++)
421
                /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
422
293052
                band->coord[i][j] =
423
293052
                    ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
424
293052
                                            (((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
73263
        band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
431
                                       reslevel->log2_prec_width - 1);
432
73263
        band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
433
                                       reslevel->log2_prec_height - 1);
434
435
73263
        log2_band_prec_width  = reslevel->log2_prec_width  - 1;
436
73263
        log2_band_prec_height = reslevel->log2_prec_height - 1;
437
    }
438
439
77336
    if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
440
        band->prec = NULL;
441
        return AVERROR(ENOMEM);
442
    }
443
77336
    nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
444
77336
    band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
445
77336
    if (!band->prec)
446
        return AVERROR(ENOMEM);
447
448
156103
    for (precno = 0; precno < nb_precincts; precno++) {
449
78767
        ret = init_prec(avctx, band, reslevel, comp, codsty,
450
                        precno, bandno, reslevelno,
451
                        log2_band_prec_width, log2_band_prec_height);
452
78767
        if (ret < 0)
453
            return ret;
454
    }
455
456
77336
    return 0;
457
}
458
459
4073
int ff_jpeg2000_init_component(Jpeg2000Component *comp,
460
                               Jpeg2000CodingStyle *codsty,
461
                               Jpeg2000QuantStyle *qntsty,
462
                               int cbps, int dx, int dy,
463
                               AVCodecContext *avctx)
464
{
465
4073
    int reslevelno, bandno, gbandno = 0, ret, i, j;
466
    uint32_t csize;
467
468
4073
    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
4073
    if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
474
4073
                                   codsty->nreslevels2decode - 1,
475
4073
                                   codsty->transform))
476
        return ret;
477
478
4073
    if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
479
4073
                            comp->coord[1][1] - comp->coord[1][0], 0, avctx))
480
        return AVERROR_INVALIDDATA;
481
4073
    csize = (comp->coord[0][1] - comp->coord[0][0]) *
482
4073
            (comp->coord[1][1] - comp->coord[1][0]);
483
4073
    if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
484
4073
        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
4073
    if (codsty->transform == FF_DWT97) {
490
8
        csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
491
8
        comp->i_data = NULL;
492
8
        comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
493
8
        if (!comp->f_data)
494
            return AVERROR(ENOMEM);
495
    } else {
496
4065
        csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
497
4065
        comp->f_data = NULL;
498
4065
        comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
499
4065
        if (!comp->i_data)
500
            return AVERROR(ENOMEM);
501
    }
502
4073
    comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
503
4073
    if (!comp->reslevel)
504
        return AVERROR(ENOMEM);
505
    /* LOOP on resolution levels */
506
32567
    for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
507
28494
        int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 15444-1:2002 B.5
508
28494
        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
85482
        for (i = 0; i < 2; i++)
514
170964
            for (j = 0; j < 2; j++)
515
113976
                reslevel->coord[i][j] =
516
113976
                    ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
517
        // update precincts size: 2^n value
518
28494
        reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
519
28494
        reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
520
521
        /* Number of bands for each resolution level */
522
28494
        if (reslevelno == 0)
523
4073
            reslevel->nbands = 1;
524
        else
525
24421
            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
28494
        if (reslevel->coord[0][1] == reslevel->coord[0][0])
535
            reslevel->num_precincts_x = 0;
536
        else
537
28494
            reslevel->num_precincts_x =
538
28494
                ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
539
28494
                                        reslevel->log2_prec_width) -
540
28494
                (reslevel->coord[0][0] >> reslevel->log2_prec_width);
541
542
28494
        if (reslevel->coord[1][1] == reslevel->coord[1][0])
543
            reslevel->num_precincts_y = 0;
544
        else
545
28494
            reslevel->num_precincts_y =
546
28494
                ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
547
28494
                                        reslevel->log2_prec_height) -
548
28494
                (reslevel->coord[1][0] >> reslevel->log2_prec_height);
549
550
28494
        reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
551
28494
        if (!reslevel->band)
552
            return AVERROR(ENOMEM);
553
554
28494
        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
105830
        for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
558
77336
            ret = init_band(avctx, reslevel,
559
                            comp, codsty, qntsty,
560
                            bandno, gbandno, reslevelno,
561
                            cbps, dx, dy);
562
77336
            if (ret < 0)
563
                return ret;
564
        }
565
    }
566
4073
    return 0;
567
}
568
569
3900
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
570
{
571
    int reslevelno, bandno, cblkno, precno;
572
31200
    for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
573
27300
        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
574
101400
        for (bandno = 0; bandno < rlevel->nbands; bandno++) {
575
74100
            Jpeg2000Band *band = rlevel->band + bandno;
576
148200
            for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
577
74100
                Jpeg2000Prec *prec = band->prec + precno;
578
74100
                ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 0);
579
74100
                ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 0);
580
491100
                for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
581
417000
                    Jpeg2000Cblk *cblk = prec->cblk + cblkno;
582
417000
                    cblk->length = 0;
583
417000
                    cblk->lblock = 3;
584
                }
585
            }
586
        }
587
    }
588
3900
}
589
590
4073
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
591
{
592
    int reslevelno, bandno, precno;
593
4073
    for (reslevelno = 0;
594

32567
         comp->reslevel && reslevelno < codsty->nreslevels;
595
28494
         reslevelno++) {
596
        Jpeg2000ResLevel *reslevel;
597
598
28494
        if (!comp->reslevel)
599
            continue;
600
601
28494
        reslevel = comp->reslevel + reslevelno;
602
105830
        for (bandno = 0; bandno < reslevel->nbands; bandno++) {
603
            Jpeg2000Band *band;
604
605
77336
            if (!reslevel->band)
606
                continue;
607
608
77336
            band = reslevel->band + bandno;
609
156103
            for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
610
78767
                if (band->prec) {
611
78767
                    Jpeg2000Prec *prec = band->prec + precno;
612
78767
                    int nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
613
614
78767
                    av_freep(&prec->zerobits);
615
78767
                    av_freep(&prec->cblkincl);
616
78767
                    if (prec->cblk) {
617
                        int cblkno;
618
533858
                        for (cblkno = 0; cblkno < nb_code_blocks; cblkno ++) {
619
455091
                            Jpeg2000Cblk *cblk = &prec->cblk[cblkno];
620
455091
                            av_freep(&cblk->data);
621
455091
                            av_freep(&cblk->passes);
622
455091
                            av_freep(&cblk->lengthinc);
623
455091
                            av_freep(&cblk->data_start);
624
455091
                            av_freep(&cblk->layers);
625
                        }
626
78767
                        av_freep(&prec->cblk);
627
                    }
628
                }
629
            }
630
631
77336
            av_freep(&band->prec);
632
        }
633
28494
        av_freep(&reslevel->band);
634
    }
635
636
4073
    ff_dwt_destroy(&comp->dwt);
637
4073
    av_freep(&comp->reslevel);
638
4073
    av_freep(&comp->i_data);
639
4073
    av_freep(&comp->f_data);
640
4073
}