Coverage Report

Created: 2025-06-16 07:00

/src/openjpeg/src/lib/openjp2/tcd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The copyright in this software is being made available under the 2-clauses
3
 * BSD License, included below. This software may be subject to other third
4
 * party and contributor rights, including patent rights, and no such rights
5
 * are granted under this license.
6
 *
7
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8
 * Copyright (c) 2002-2014, Professor Benoit Macq
9
 * Copyright (c) 2001-2003, David Janssens
10
 * Copyright (c) 2002-2003, Yannick Verschueren
11
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12
 * Copyright (c) 2003-2014, Antonin Descampe
13
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14
 * Copyright (c) 2006-2007, Parvatha Elangovan
15
 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16
 * Copyright (c) 2012, CS Systemes d'Information, France
17
 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
18
 * All rights reserved.
19
 *
20
 * Redistribution and use in source and binary forms, with or without
21
 * modification, are permitted provided that the following conditions
22
 * are met:
23
 * 1. Redistributions of source code must retain the above copyright
24
 *    notice, this list of conditions and the following disclaimer.
25
 * 2. Redistributions in binary form must reproduce the above copyright
26
 *    notice, this list of conditions and the following disclaimer in the
27
 *    documentation and/or other materials provided with the distribution.
28
 *
29
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
30
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39
 * POSSIBILITY OF SUCH DAMAGE.
40
 */
41
42
#include "opj_includes.h"
43
#include "opj_common.h"
44
45
// #define DEBUG_RATE_ALLOC
46
47
/* ----------------------------------------------------------------------- */
48
49
/* TODO MSD: */
50
#ifdef TODO_MSD
51
void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
52
{
53
    int tileno, compno, resno, bandno, precno;/*, cblkno;*/
54
55
    fprintf(fd, "image {\n");
56
    fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
57
            img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
58
            tcd->image->y1);
59
60
    for (tileno = 0; tileno < img->th * img->tw; tileno++) {
61
        opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
62
        fprintf(fd, "  tile {\n");
63
        fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
64
                tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
65
        for (compno = 0; compno < tile->numcomps; compno++) {
66
            opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
67
            fprintf(fd, "    tilec {\n");
68
            fprintf(fd,
69
                    "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
70
                    tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
71
            for (resno = 0; resno < tilec->numresolutions; resno++) {
72
                opj_tcd_resolution_t *res = &tilec->resolutions[resno];
73
                fprintf(fd, "\n   res {\n");
74
                fprintf(fd,
75
                        "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
76
                        res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
77
                for (bandno = 0; bandno < res->numbands; bandno++) {
78
                    opj_tcd_band_t *band = &res->bands[bandno];
79
                    fprintf(fd, "        band {\n");
80
                    fprintf(fd,
81
                            "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
82
                            band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
83
                    for (precno = 0; precno < res->pw * res->ph; precno++) {
84
                        opj_tcd_precinct_t *prec = &band->precincts[precno];
85
                        fprintf(fd, "          prec {\n");
86
                        fprintf(fd,
87
                                "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
88
                                prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
89
                        /*
90
                        for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
91
                                opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
92
                                fprintf(fd, "            cblk {\n");
93
                                fprintf(fd,
94
                                        "              x0=%d, y0=%d, x1=%d, y1=%d\n",
95
                                        cblk->x0, cblk->y0, cblk->x1, cblk->y1);
96
                                fprintf(fd, "            }\n");
97
                        }
98
                        */
99
                        fprintf(fd, "          }\n");
100
                    }
101
                    fprintf(fd, "        }\n");
102
                }
103
                fprintf(fd, "      }\n");
104
            }
105
            fprintf(fd, "    }\n");
106
        }
107
        fprintf(fd, "  }\n");
108
    }
109
    fprintf(fd, "}\n");
110
}
111
#endif
112
113
/**
114
 * Initializes tile coding/decoding
115
 */
116
static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
117
        OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block,
118
        opj_event_mgr_t* manager);
119
120
/**
121
* Allocates memory for a decoding code block.
122
*/
123
static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
124
        p_code_block);
125
126
/**
127
 * Deallocates the decoding data of the given precinct.
128
 */
129
static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
130
131
/**
132
 * Allocates memory for an encoding code block (but not data).
133
 */
134
static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
135
        p_code_block);
136
137
/**
138
 * Allocates data for an encoding code block
139
 */
140
static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
141
        p_code_block);
142
143
/**
144
 * Deallocates the encoding data of the given precinct.
145
 */
146
static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
147
148
static
149
void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
150
                             OPJ_UINT32 final);
151
152
/**
153
Free the memory allocated for encoding
154
@param tcd TCD handle
155
*/
156
static void opj_tcd_free_tile(opj_tcd_t *tcd);
157
158
159
static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
160
                                  OPJ_BYTE * p_src_data,
161
                                  OPJ_UINT32 * p_data_read,
162
                                  OPJ_UINT32 p_max_src_size,
163
                                  opj_codestream_index_t *p_cstr_index,
164
                                  opj_event_mgr_t *p_manager);
165
166
static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd,
167
                                  opj_event_mgr_t *p_manager);
168
169
static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
170
171
static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
172
                                   opj_event_mgr_t *p_manager);
173
174
static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
175
176
177
static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
178
179
static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
180
181
static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
182
183
static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
184
185
static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
186
                                  OPJ_BYTE * p_dest_data,
187
                                  OPJ_UINT32 * p_data_written,
188
                                  OPJ_UINT32 p_max_dest_size,
189
                                  opj_codestream_info_t *p_cstr_info,
190
                                  opj_tcd_marker_info_t* p_marker_info,
191
                                  opj_event_mgr_t *p_manager);
192
193
static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
194
        OPJ_BYTE * p_dest_data,
195
        OPJ_UINT32 p_max_dest_size,
196
        opj_codestream_info_t *p_cstr_info,
197
        opj_event_mgr_t *p_manager);
198
199
200
static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd,
201
        OPJ_UINT32 compno);
202
203
/* ----------------------------------------------------------------------- */
204
205
/**
206
Create a new TCD handle
207
*/
208
opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
209
62.0k
{
210
62.0k
    opj_tcd_t *l_tcd = 00;
211
212
    /* create the tcd structure */
213
62.0k
    l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
214
62.0k
    if (!l_tcd) {
215
0
        return 00;
216
0
    }
217
218
62.0k
    l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
219
220
62.0k
    l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
221
62.0k
    if (!l_tcd->tcd_image) {
222
0
        opj_free(l_tcd);
223
0
        return 00;
224
0
    }
225
226
62.0k
    return l_tcd;
227
62.0k
}
228
229
230
/* ----------------------------------------------------------------------- */
231
232
static
233
void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
234
0
{
235
0
    OPJ_UINT32 layno;
236
237
0
    for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
238
0
        opj_tcd_makelayer_fixed(tcd, layno, 1);
239
0
    }
240
0
}
241
242
243
/* ----------------------------------------------------------------------- */
244
245
/** Returns OPJ_TRUE if the layer allocation is unchanged w.r.t to the previous
246
 * invocation with a different threshold */
247
static
248
OPJ_BOOL opj_tcd_makelayer(opj_tcd_t *tcd,
249
                           OPJ_UINT32 layno,
250
                           OPJ_FLOAT64 thresh,
251
                           OPJ_UINT32 final)
252
118k
{
253
118k
    OPJ_UINT32 compno, resno, bandno, precno, cblkno;
254
118k
    OPJ_UINT32 passno;
255
256
118k
    opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
257
118k
    OPJ_BOOL layer_allocation_is_same = OPJ_TRUE;
258
259
118k
    tcd_tile->distolayer[layno] = 0;
260
261
238k
    for (compno = 0; compno < tcd_tile->numcomps; compno++) {
262
120k
        opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
263
264
821k
        for (resno = 0; resno < tilec->numresolutions; resno++) {
265
700k
            opj_tcd_resolution_t *res = &tilec->resolutions[resno];
266
267
2.56M
            for (bandno = 0; bandno < res->numbands; bandno++) {
268
1.86M
                opj_tcd_band_t *band = &res->bands[bandno];
269
270
                /* Skip empty bands */
271
1.86M
                if (opj_tcd_is_band_empty(band)) {
272
0
                    continue;
273
0
                }
274
275
15.8M
                for (precno = 0; precno < res->pw * res->ph; precno++) {
276
14.0M
                    opj_tcd_precinct_t *prc = &band->precincts[precno];
277
278
172M
                    for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
279
158M
                        opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
280
158M
                        opj_tcd_layer_t *layer = &cblk->layers[layno];
281
158M
                        OPJ_UINT32 n;
282
283
158M
                        if (layno == 0) {
284
158M
                            cblk->numpassesinlayers = 0;
285
158M
                        }
286
287
158M
                        n = cblk->numpassesinlayers;
288
289
158M
                        if (thresh < 0) {
290
                            /* Special value to indicate to use all passes */
291
1.21M
                            n = cblk->totalpasses;
292
156M
                        } else {
293
1.31G
                            for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
294
1.15G
                                OPJ_UINT32 dr;
295
1.15G
                                OPJ_FLOAT64 dd;
296
1.15G
                                opj_tcd_pass_t *pass = &cblk->passes[passno];
297
298
1.15G
                                if (n == 0) {
299
185M
                                    dr = pass->rate;
300
185M
                                    dd = pass->distortiondec;
301
968M
                                } else {
302
968M
                                    dr = pass->rate - cblk->passes[n - 1].rate;
303
968M
                                    dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
304
968M
                                }
305
306
1.15G
                                if (!dr) {
307
167M
                                    if (dd != 0) {
308
26.4M
                                        n = passno + 1;
309
26.4M
                                    }
310
167M
                                    continue;
311
167M
                                }
312
987M
                                if (thresh - (dd / dr) <
313
987M
                                        DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
314
684M
                                    n = passno + 1;
315
684M
                                }
316
987M
                            }
317
156M
                        }
318
319
158M
                        if (layer->numpasses != n - cblk->numpassesinlayers) {
320
6.47M
                            layer_allocation_is_same = OPJ_FALSE;
321
6.47M
                            layer->numpasses = n - cblk->numpassesinlayers;
322
6.47M
                        }
323
324
158M
                        if (!layer->numpasses) {
325
120M
                            layer->disto = 0;
326
120M
                            continue;
327
120M
                        }
328
329
38.0M
                        if (cblk->numpassesinlayers == 0) {
330
38.0M
                            layer->len = cblk->passes[n - 1].rate;
331
38.0M
                            layer->data = cblk->data;
332
38.0M
                            layer->disto = cblk->passes[n - 1].distortiondec;
333
38.0M
                        } else {
334
0
                            layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
335
0
                                         1].rate;
336
0
                            layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
337
0
                            layer->disto = cblk->passes[n - 1].distortiondec -
338
0
                                           cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
339
0
                        }
340
341
38.0M
                        tcd_tile->distolayer[layno] += layer->disto;
342
343
38.0M
                        if (final) {
344
875k
                            cblk->numpassesinlayers = n;
345
875k
                        }
346
38.0M
                    }
347
14.0M
                }
348
1.86M
            }
349
700k
        }
350
120k
    }
351
118k
    return layer_allocation_is_same;
352
118k
}
353
354
/** For m_quality_layer_alloc_strategy == FIXED_LAYER */
355
static
356
void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
357
                             OPJ_UINT32 final)
358
0
{
359
0
    OPJ_UINT32 compno, resno, bandno, precno, cblkno;
360
0
    OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
361
0
    OPJ_INT32 matrice[J2K_TCD_MATRIX_MAX_LAYER_COUNT][J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT][3];
362
0
    OPJ_UINT32 i, j, k;
363
364
0
    opj_cp_t *cp = tcd->cp;
365
0
    opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
366
0
    opj_tcp_t *tcd_tcp = tcd->tcp;
367
368
0
    for (compno = 0; compno < tcd_tile->numcomps; compno++) {
369
0
        opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
370
371
0
        for (i = 0; i < tcd_tcp->numlayers; i++) {
372
0
            for (j = 0; j < tilec->numresolutions; j++) {
373
0
                for (k = 0; k < 3; k++) {
374
0
                    matrice[i][j][k] =
375
0
                        (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
376
0
                                      tilec->numresolutions * 3 + j * 3 + k]
377
0
                                    * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
378
0
                }
379
0
            }
380
0
        }
381
382
0
        for (resno = 0; resno < tilec->numresolutions; resno++) {
383
0
            opj_tcd_resolution_t *res = &tilec->resolutions[resno];
384
385
0
            for (bandno = 0; bandno < res->numbands; bandno++) {
386
0
                opj_tcd_band_t *band = &res->bands[bandno];
387
388
                /* Skip empty bands */
389
0
                if (opj_tcd_is_band_empty(band)) {
390
0
                    continue;
391
0
                }
392
393
0
                for (precno = 0; precno < res->pw * res->ph; precno++) {
394
0
                    opj_tcd_precinct_t *prc = &band->precincts[precno];
395
396
0
                    for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
397
0
                        opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
398
0
                        opj_tcd_layer_t *layer = &cblk->layers[layno];
399
0
                        OPJ_UINT32 n;
400
0
                        OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
401
0
                                                     cblk->numbps); /* number of bit-plan equal to zero */
402
403
                        /* Correction of the matrix of coefficient to include the IMSB information */
404
0
                        if (layno == 0) {
405
0
                            value = matrice[layno][resno][bandno];
406
0
                            if (imsb >= value) {
407
0
                                value = 0;
408
0
                            } else {
409
0
                                value -= imsb;
410
0
                            }
411
0
                        } else {
412
0
                            value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
413
0
                            if (imsb >= matrice[layno - 1][resno][bandno]) {
414
0
                                value -= (imsb - matrice[layno - 1][resno][bandno]);
415
0
                                if (value < 0) {
416
0
                                    value = 0;
417
0
                                }
418
0
                            }
419
0
                        }
420
421
0
                        if (layno == 0) {
422
0
                            cblk->numpassesinlayers = 0;
423
0
                        }
424
425
0
                        n = cblk->numpassesinlayers;
426
0
                        if (cblk->numpassesinlayers == 0) {
427
0
                            if (value != 0) {
428
0
                                n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
429
0
                            } else {
430
0
                                n = cblk->numpassesinlayers;
431
0
                            }
432
0
                        } else {
433
0
                            n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
434
0
                        }
435
436
0
                        layer->numpasses = n - cblk->numpassesinlayers;
437
438
0
                        if (!layer->numpasses) {
439
0
                            continue;
440
0
                        }
441
442
0
                        if (cblk->numpassesinlayers == 0) {
443
0
                            layer->len = cblk->passes[n - 1].rate;
444
0
                            layer->data = cblk->data;
445
0
                        } else {
446
0
                            layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
447
0
                                         1].rate;
448
0
                            layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
449
0
                        }
450
451
0
                        if (final) {
452
0
                            cblk->numpassesinlayers = n;
453
0
                        }
454
0
                    }
455
0
                }
456
0
            }
457
0
        }
458
0
    }
459
0
}
460
461
/** Rate allocation for the following methods:
462
 * - allocation by rate/distortio (m_quality_layer_alloc_strategy == RATE_DISTORTION_RATIO)
463
 * - allocation by fixed quality  (m_quality_layer_alloc_strategy == FIXED_DISTORTION_RATIO)
464
 */
465
static
466
OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
467
                              OPJ_BYTE *dest,
468
                              OPJ_UINT32 * p_data_written,
469
                              OPJ_UINT32 len,
470
                              opj_codestream_info_t *cstr_info,
471
                              opj_event_mgr_t *p_manager)
472
13.8k
{
473
13.8k
    OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
474
13.8k
    OPJ_UINT32 passno;
475
13.8k
    OPJ_FLOAT64 min, max;
476
13.8k
    OPJ_FLOAT64 cumdisto[100];
477
13.8k
    const OPJ_FLOAT64 K = 1;
478
13.8k
    OPJ_FLOAT64 maxSE = 0;
479
480
13.8k
    opj_cp_t *cp = tcd->cp;
481
13.8k
    opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
482
13.8k
    opj_tcp_t *tcd_tcp = tcd->tcp;
483
484
13.8k
    min = DBL_MAX;
485
13.8k
    max = 0;
486
487
13.8k
    tcd_tile->numpix = 0;
488
489
29.1k
    for (compno = 0; compno < tcd_tile->numcomps; compno++) {
490
15.3k
        opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
491
15.3k
        tilec->numpix = 0;
492
493
86.8k
        for (resno = 0; resno < tilec->numresolutions; resno++) {
494
71.5k
            opj_tcd_resolution_t *res = &tilec->resolutions[resno];
495
496
255k
            for (bandno = 0; bandno < res->numbands; bandno++) {
497
183k
                opj_tcd_band_t *band = &res->bands[bandno];
498
499
                /* Skip empty bands */
500
183k
                if (opj_tcd_is_band_empty(band)) {
501
0
                    continue;
502
0
                }
503
504
484k
                for (precno = 0; precno < res->pw * res->ph; precno++) {
505
300k
                    opj_tcd_precinct_t *prc = &band->precincts[precno];
506
507
2.93M
                    for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
508
2.63M
                        opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
509
510
28.0M
                        for (passno = 0; passno < cblk->totalpasses; passno++) {
511
25.3M
                            opj_tcd_pass_t *pass = &cblk->passes[passno];
512
25.3M
                            OPJ_INT32 dr;
513
25.3M
                            OPJ_FLOAT64 dd, rdslope;
514
515
25.3M
                            if (passno == 0) {
516
876k
                                dr = (OPJ_INT32)pass->rate;
517
876k
                                dd = pass->distortiondec;
518
24.5M
                            } else {
519
24.5M
                                dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
520
24.5M
                                dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
521
24.5M
                            }
522
523
25.3M
                            if (dr == 0) {
524
6.26M
                                continue;
525
6.26M
                            }
526
527
19.1M
                            rdslope = dd / dr;
528
19.1M
                            if (rdslope < min) {
529
100k
                                min = rdslope;
530
100k
                            }
531
532
19.1M
                            if (rdslope > max) {
533
35.3k
                                max = rdslope;
534
35.3k
                            }
535
19.1M
                        } /* passno */
536
537
2.63M
                        {
538
2.63M
                            const OPJ_SIZE_T cblk_pix_count = (OPJ_SIZE_T)((cblk->x1 - cblk->x0) *
539
2.63M
                                                              (cblk->y1 - cblk->y0));
540
2.63M
                            tcd_tile->numpix += cblk_pix_count;
541
2.63M
                            tilec->numpix += cblk_pix_count;
542
2.63M
                        }
543
2.63M
                    } /* cbklno */
544
300k
                } /* precno */
545
183k
            } /* bandno */
546
71.5k
        } /* resno */
547
548
15.3k
        maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
549
15.3k
                  * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
550
15.3k
                 * ((OPJ_FLOAT64)(tilec->numpix));
551
15.3k
    } /* compno */
552
553
    /* index file */
554
13.8k
    if (cstr_info) {
555
0
        opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
556
0
        tile_info->numpix = (int)tcd_tile->numpix;
557
0
        tile_info->distotile = (int)tcd_tile->distotile;
558
0
        tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
559
0
                                OPJ_FLOAT64));
560
0
        if (!tile_info->thresh) {
561
            /* FIXME event manager error callback */
562
0
            return OPJ_FALSE;
563
0
        }
564
0
    }
565
566
27.6k
    for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
567
13.8k
        OPJ_FLOAT64 lo = min;
568
13.8k
        OPJ_FLOAT64 hi = max;
569
13.8k
        OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
570
12.8k
                                OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
571
13.8k
        OPJ_FLOAT64 goodthresh = 0;
572
13.8k
        OPJ_FLOAT64 stable_thresh = 0;
573
13.8k
        OPJ_UINT32 i;
574
13.8k
        OPJ_FLOAT64 distotarget;
575
576
13.8k
        distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
577
13.8k
                                             tcd_tcp->distoratio[layno] / 10));
578
579
        /* Don't try to find an optimal threshold but rather take everything not included yet, if
580
          -r xx,yy,zz,0   (m_quality_layer_alloc_strategy == RATE_DISTORTION_RATIO and rates == NULL)
581
          -q xx,yy,zz,0   (m_quality_layer_alloc_strategy == FIXED_DISTORTION_RATIO and distoratio == NULL)
582
          ==> possible to have some lossy layers and the last layer for sure lossless */
583
13.8k
        if (((cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
584
13.8k
                RATE_DISTORTION_RATIO) &&
585
13.8k
                (tcd_tcp->rates[layno] > 0.0f)) ||
586
13.8k
                ((cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
587
12.8k
                  FIXED_DISTORTION_RATIO) &&
588
12.8k
                 (tcd_tcp->distoratio[layno] > 0.0))) {
589
950
            opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
590
950
            OPJ_FLOAT64 thresh = 0;
591
950
            OPJ_BOOL last_layer_allocation_ok = OPJ_FALSE;
592
593
950
            if (t2 == 00) {
594
0
                return OPJ_FALSE;
595
0
            }
596
597
105k
            for (i = 0; i < 128; ++i) {
598
105k
                OPJ_FLOAT64 distoachieved = 0;
599
105k
                OPJ_BOOL layer_allocation_is_same;
600
601
105k
                OPJ_FLOAT64 new_thresh = (lo + hi) / 2;
602
                /* Stop iterating when the threshold has stabilized enough */
603
                /* 0.5 * 1e-5 is somewhat arbitrary, but has been selected */
604
                /* so that this doesn't change the results of the regression */
605
                /* test suite. */
606
105k
                if (fabs(new_thresh - thresh) <= 0.5 * 1e-5 * thresh) {
607
192
                    break;
608
192
                }
609
104k
                thresh = new_thresh;
610
#ifdef DEBUG_RATE_ALLOC
611
                opj_event_msg(p_manager, EVT_INFO, "layno=%u, iter=%u, thresh=%g",
612
                              layno, i, new_thresh);
613
#endif
614
615
104k
                layer_allocation_is_same = opj_tcd_makelayer(tcd, layno, thresh, 0) && i != 0;
616
#ifdef DEBUG_RATE_ALLOC
617
                opj_event_msg(p_manager, EVT_INFO, "--> layer_allocation_is_same = %d",
618
                              layer_allocation_is_same);
619
#endif
620
104k
                if (cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
621
104k
                        FIXED_DISTORTION_RATIO) {
622
0
                    if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
623
0
                        if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
624
0
                                                    p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
625
0
                                                    tcd->cur_pino,
626
0
                                                    THRESH_CALC, p_manager)) {
627
628
0
                            lo = thresh;
629
0
                            continue;
630
0
                        } else {
631
0
                            distoachieved = layno == 0 ?
632
0
                                            tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
633
634
0
                            if (distoachieved < distotarget) {
635
0
                                hi = thresh;
636
0
                                stable_thresh = thresh;
637
0
                                continue;
638
0
                            } else {
639
0
                                lo = thresh;
640
0
                            }
641
0
                        }
642
0
                    } else {
643
0
                        distoachieved = (layno == 0) ?
644
0
                                        tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
645
646
0
                        if (distoachieved < distotarget) {
647
0
                            hi = thresh;
648
0
                            stable_thresh = thresh;
649
0
                            continue;
650
0
                        }
651
0
                        lo = thresh;
652
0
                    }
653
104k
                } else { /* Disto/rate based optimization */
654
                    /* Check if the layer allocation done by opj_tcd_makelayer()
655
                     * is compatible of the maximum rate allocation. If not,
656
                     * retry with a higher threshold.
657
                     * If OK, try with a lower threshold.
658
                     * Call opj_t2_encode_packets() only if opj_tcd_makelayer()
659
                     * has resulted in different truncation points since its last
660
                     * call. */
661
104k
                    if ((layer_allocation_is_same && !last_layer_allocation_ok) ||
662
104k
                            (!layer_allocation_is_same &&
663
104k
                             ! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
664
27.7k
                                                     p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
665
27.7k
                                                     tcd->cur_pino,
666
27.7k
                                                     THRESH_CALC, p_manager))) {
667
668
#ifdef DEBUG_RATE_ALLOC
669
                        if (!layer_allocation_is_same) {
670
                            opj_event_msg(p_manager, EVT_INFO,
671
                                          "--> check rate alloc failed (> maxlen=%u)\n", maxlen);
672
                        }
673
#endif
674
892
                        last_layer_allocation_ok = OPJ_FALSE;
675
892
                        lo = thresh;
676
892
                        continue;
677
892
                    }
678
679
#ifdef DEBUG_RATE_ALLOC
680
                    if (!layer_allocation_is_same) {
681
                        opj_event_msg(p_manager, EVT_INFO,
682
                                      "--> check rate alloc success (len=%u <= maxlen=%u)\n", *p_data_written,
683
                                      maxlen);
684
                    }
685
#endif
686
687
104k
                    last_layer_allocation_ok = OPJ_TRUE;
688
104k
                    hi = thresh;
689
104k
                    stable_thresh = thresh;
690
104k
                }
691
104k
            }
692
693
950
            goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
694
695
950
            opj_t2_destroy(t2);
696
12.8k
        } else {
697
            /* Special value to indicate to use all passes */
698
12.8k
            goodthresh = -1;
699
12.8k
        }
700
701
13.8k
        if (cstr_info) { /* Threshold for Marcela Index */
702
0
            cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
703
0
        }
704
705
13.8k
        opj_tcd_makelayer(tcd, layno, goodthresh, 1);
706
707
13.8k
        cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
708
13.8k
                          (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
709
13.8k
    }
710
711
13.8k
    return OPJ_TRUE;
712
13.8k
}
713
714
OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
715
                      opj_image_t * p_image,
716
                      opj_cp_t * p_cp,
717
                      opj_thread_pool_t* p_tp)
718
62.0k
{
719
62.0k
    p_tcd->image = p_image;
720
62.0k
    p_tcd->cp = p_cp;
721
722
62.0k
    p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
723
62.0k
                              sizeof(opj_tcd_tile_t));
724
62.0k
    if (! p_tcd->tcd_image->tiles) {
725
0
        return OPJ_FALSE;
726
0
    }
727
728
62.0k
    p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
729
62.0k
                                         p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
730
62.0k
    if (! p_tcd->tcd_image->tiles->comps) {
731
0
        return OPJ_FALSE;
732
0
    }
733
734
62.0k
    p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
735
62.0k
    p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
736
62.0k
    p_tcd->thread_pool = p_tp;
737
738
62.0k
    return OPJ_TRUE;
739
62.0k
}
740
741
/**
742
Destroy a previously created TCD handle
743
*/
744
void opj_tcd_destroy(opj_tcd_t *tcd)
745
90.3k
{
746
90.3k
    if (tcd) {
747
62.0k
        opj_tcd_free_tile(tcd);
748
749
62.0k
        if (tcd->tcd_image) {
750
62.0k
            opj_free(tcd->tcd_image);
751
62.0k
            tcd->tcd_image = 00;
752
62.0k
        }
753
754
62.0k
        opj_free(tcd->used_component);
755
756
62.0k
        opj_free(tcd);
757
62.0k
    }
758
90.3k
}
759
760
OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
761
131k
{
762
131k
    if ((l_tilec->data == 00) ||
763
131k
            ((l_tilec->data_size_needed > l_tilec->data_size) &&
764
98.2k
             (l_tilec->ownsData == OPJ_FALSE))) {
765
98.2k
        l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
766
98.2k
        if (!l_tilec->data && l_tilec->data_size_needed != 0) {
767
0
            return OPJ_FALSE;
768
0
        }
769
        /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
770
98.2k
        l_tilec->data_size = l_tilec->data_size_needed;
771
98.2k
        l_tilec->ownsData = OPJ_TRUE;
772
98.2k
    } else if (l_tilec->data_size_needed > l_tilec->data_size) {
773
        /* We don't need to keep old data */
774
3.18k
        opj_image_data_free(l_tilec->data);
775
3.18k
        l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
776
3.18k
        if (! l_tilec->data) {
777
0
            l_tilec->data_size = 0;
778
0
            l_tilec->data_size_needed = 0;
779
0
            l_tilec->ownsData = OPJ_FALSE;
780
0
            return OPJ_FALSE;
781
0
        }
782
        /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
783
3.18k
        l_tilec->data_size = l_tilec->data_size_needed;
784
3.18k
        l_tilec->ownsData = OPJ_TRUE;
785
3.18k
    }
786
131k
    return OPJ_TRUE;
787
131k
}
788
789
/* ----------------------------------------------------------------------- */
790
791
static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
792
        OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block,
793
        opj_event_mgr_t* manager)
794
64.9k
{
795
64.9k
    OPJ_UINT32 compno, resno, bandno, precno, cblkno;
796
64.9k
    opj_tcp_t * l_tcp = 00;
797
64.9k
    opj_cp_t * l_cp = 00;
798
64.9k
    opj_tcd_tile_t * l_tile = 00;
799
64.9k
    opj_tccp_t *l_tccp = 00;
800
64.9k
    opj_tcd_tilecomp_t *l_tilec = 00;
801
64.9k
    opj_image_comp_t * l_image_comp = 00;
802
64.9k
    opj_tcd_resolution_t *l_res = 00;
803
64.9k
    opj_tcd_band_t *l_band = 00;
804
64.9k
    opj_stepsize_t * l_step_size = 00;
805
64.9k
    opj_tcd_precinct_t *l_current_precinct = 00;
806
64.9k
    opj_image_t *l_image = 00;
807
64.9k
    OPJ_UINT32 p, q;
808
64.9k
    OPJ_UINT32 l_level_no;
809
64.9k
    OPJ_UINT32 l_pdx, l_pdy;
810
64.9k
    OPJ_INT32 l_x0b, l_y0b;
811
64.9k
    OPJ_UINT32 l_tx0, l_ty0;
812
    /* extent of precincts , top left, bottom right**/
813
64.9k
    OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
814
    /* number of precinct for a resolution */
815
64.9k
    OPJ_UINT32 l_nb_precincts;
816
    /* room needed to store l_nb_precinct precinct for a resolution */
817
64.9k
    OPJ_UINT32 l_nb_precinct_size;
818
    /* number of code blocks for a precinct*/
819
64.9k
    OPJ_UINT32 l_nb_code_blocks;
820
    /* room needed to store l_nb_code_blocks code blocks for a precinct*/
821
64.9k
    OPJ_UINT32 l_nb_code_blocks_size;
822
    /* size of data for a tile */
823
64.9k
    OPJ_UINT32 l_data_size;
824
825
64.9k
    l_cp = p_tcd->cp;
826
64.9k
    l_tcp = &(l_cp->tcps[p_tile_no]);
827
64.9k
    l_tile = p_tcd->tcd_image->tiles;
828
64.9k
    l_tccp = l_tcp->tccps;
829
64.9k
    l_tilec = l_tile->comps;
830
64.9k
    l_image = p_tcd->image;
831
64.9k
    l_image_comp = p_tcd->image->comps;
832
833
64.9k
    p = p_tile_no % l_cp->tw;       /* tile coordinates */
834
64.9k
    q = p_tile_no / l_cp->tw;
835
    /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
836
837
    /* 4 borders of the tile rescale on the image if necessary */
838
64.9k
    l_tx0 = l_cp->tx0 + p *
839
64.9k
            l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
840
64.9k
    l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
841
64.9k
    l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
842
64.9k
                                         l_image->x1);
843
    /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
844
64.9k
    if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
845
0
        opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
846
0
        return OPJ_FALSE;
847
0
    }
848
64.9k
    l_ty0 = l_cp->ty0 + q *
849
64.9k
            l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
850
64.9k
    l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
851
64.9k
    l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
852
64.9k
                                         l_image->y1);
853
    /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
854
64.9k
    if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
855
0
        opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
856
0
        return OPJ_FALSE;
857
0
    }
858
859
860
    /* testcase 1888.pdf.asan.35.988 */
861
64.9k
    if (l_tccp->numresolutions == 0) {
862
0
        opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
863
0
        return OPJ_FALSE;
864
0
    }
865
    /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
866
867
    /*tile->numcomps = image->numcomps; */
868
339k
    for (compno = 0; compno < l_tile->numcomps; ++compno) {
869
        /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
870
274k
        l_image_comp->resno_decoded = 0;
871
        /* border of each l_tile component (global) */
872
274k
        l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
873
274k
        l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
874
274k
        l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
875
274k
        l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
876
274k
        l_tilec->compno = compno;
877
        /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
878
879
274k
        l_tilec->numresolutions = l_tccp->numresolutions;
880
274k
        if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
881
1.19k
            l_tilec->minimum_num_resolutions = 1;
882
272k
        } else {
883
272k
            l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
884
272k
                                               l_cp->m_specific_param.m_dec.m_reduce;
885
272k
        }
886
887
274k
        if (isEncoder) {
888
15.3k
            OPJ_SIZE_T l_tile_data_size;
889
890
            /* compute l_data_size with overflow check */
891
15.3k
            OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0);
892
15.3k
            OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
893
894
            /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
895
15.3k
            if (h > 0 && w > SIZE_MAX / h) {
896
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
897
0
                return OPJ_FALSE;
898
0
            }
899
15.3k
            l_tile_data_size = w * h;
900
901
15.3k
            if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) {
902
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
903
0
                return OPJ_FALSE;
904
0
            }
905
15.3k
            l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32);
906
907
15.3k
            l_tilec->data_size_needed = l_tile_data_size;
908
15.3k
        }
909
910
274k
        l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
911
274k
                          opj_tcd_resolution_t);
912
913
274k
        opj_image_data_free(l_tilec->data_win);
914
274k
        l_tilec->data_win = NULL;
915
274k
        l_tilec->win_x0 = 0;
916
274k
        l_tilec->win_y0 = 0;
917
274k
        l_tilec->win_x1 = 0;
918
274k
        l_tilec->win_y1 = 0;
919
920
274k
        if (l_tilec->resolutions == 00) {
921
211k
            l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
922
211k
            if (! l_tilec->resolutions) {
923
0
                return OPJ_FALSE;
924
0
            }
925
            /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
926
211k
            l_tilec->resolutions_size = l_data_size;
927
211k
            memset(l_tilec->resolutions, 0, l_data_size);
928
211k
        } else if (l_data_size > l_tilec->resolutions_size) {
929
3
            opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
930
3
                    l_tilec->resolutions, l_data_size);
931
3
            if (! new_resolutions) {
932
0
                opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
933
0
                opj_free(l_tilec->resolutions);
934
0
                l_tilec->resolutions = NULL;
935
0
                l_tilec->resolutions_size = 0;
936
0
                return OPJ_FALSE;
937
0
            }
938
3
            l_tilec->resolutions = new_resolutions;
939
            /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
940
3
            memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
941
3
                   l_data_size - l_tilec->resolutions_size);
942
3
            l_tilec->resolutions_size = l_data_size;
943
3
        }
944
945
274k
        l_level_no = l_tilec->numresolutions;
946
274k
        l_res = l_tilec->resolutions;
947
274k
        l_step_size = l_tccp->stepsizes;
948
        /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
949
950
2.50M
        for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
951
            /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
952
2.23M
            OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
953
2.23M
            OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
954
2.23M
            OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
955
956
2.23M
            --l_level_no;
957
958
            /* border for each resolution level (global) */
959
2.23M
            l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
960
2.23M
            l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
961
2.23M
            l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
962
2.23M
            l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
963
964
            /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/
965
            /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
966
2.23M
            l_pdx = l_tccp->prcw[resno];
967
2.23M
            l_pdy = l_tccp->prch[resno];
968
            /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
969
            /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
970
2.23M
            l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
971
2.23M
            l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
972
2.23M
            {
973
2.23M
                OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->x1,
974
2.23M
                                  (OPJ_INT32)l_pdx)) << l_pdx;
975
2.23M
                if (tmp > (OPJ_UINT32)INT_MAX) {
976
0
                    opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
977
0
                    return OPJ_FALSE;
978
0
                }
979
2.23M
                l_br_prc_x_end = (OPJ_INT32)tmp;
980
2.23M
            }
981
0
            {
982
2.23M
                OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->y1,
983
2.23M
                                  (OPJ_INT32)l_pdy)) << l_pdy;
984
2.23M
                if (tmp > (OPJ_UINT32)INT_MAX) {
985
0
                    opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
986
0
                    return OPJ_FALSE;
987
0
                }
988
2.23M
                l_br_prc_y_end = (OPJ_INT32)tmp;
989
2.23M
            }
990
            /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/
991
992
2.23M
            l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
993
1.40M
                            l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
994
2.23M
            l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
995
1.21M
                            l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
996
            /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
997
998
2.23M
            if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
999
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
1000
0
                return OPJ_FALSE;
1001
0
            }
1002
2.23M
            l_nb_precincts = l_res->pw * l_res->ph;
1003
1004
2.23M
            if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
1005
2.23M
                    l_nb_precincts) {
1006
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
1007
0
                return OPJ_FALSE;
1008
0
            }
1009
2.23M
            l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
1010
1011
2.23M
            if (resno == 0) {
1012
274k
                tlcbgxstart = l_tl_prc_x_start;
1013
274k
                tlcbgystart = l_tl_prc_y_start;
1014
                /*brcbgxend = l_br_prc_x_end;*/
1015
                /* brcbgyend = l_br_prc_y_end;*/
1016
274k
                cbgwidthexpn = l_pdx;
1017
274k
                cbgheightexpn = l_pdy;
1018
274k
                l_res->numbands = 1;
1019
1.95M
            } else {
1020
1.95M
                tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
1021
1.95M
                tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
1022
                /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
1023
                /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
1024
1.95M
                cbgwidthexpn = l_pdx - 1;
1025
1.95M
                cbgheightexpn = l_pdy - 1;
1026
1.95M
                l_res->numbands = 3;
1027
1.95M
            }
1028
1029
2.23M
            cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
1030
2.23M
            cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
1031
2.23M
            l_band = l_res->bands;
1032
1033
8.37M
            for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
1034
                /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
1035
1036
6.14M
                if (resno == 0) {
1037
274k
                    l_band->bandno = 0 ;
1038
274k
                    l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
1039
274k
                    l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
1040
274k
                    l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
1041
274k
                    l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
1042
5.87M
                } else {
1043
5.87M
                    l_band->bandno = bandno + 1;
1044
                    /* x0b = 1 if bandno = 1 or 3 */
1045
5.87M
                    l_x0b = l_band->bandno & 1;
1046
                    /* y0b = 1 if bandno = 2 or 3 */
1047
5.87M
                    l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
1048
                    /* l_band border (global) */
1049
5.87M
                    l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
1050
5.87M
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
1051
5.87M
                    l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
1052
5.87M
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
1053
5.87M
                    l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
1054
5.87M
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
1055
5.87M
                    l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
1056
5.87M
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
1057
5.87M
                }
1058
1059
6.14M
                if (isEncoder) {
1060
                    /* Skip empty bands */
1061
183k
                    if (opj_tcd_is_band_empty(l_band)) {
1062
                        /* Do not zero l_band->precints to avoid leaks */
1063
                        /* but make sure we don't use it later, since */
1064
                        /* it will point to precincts of previous bands... */
1065
0
                        continue;
1066
0
                    }
1067
183k
                }
1068
1069
6.14M
                {
1070
                    /* Table E-1 - Sub-band gains */
1071
                    /* BUG_WEIRD_TWO_INVK (look for this identifier in dwt.c): */
1072
                    /* the test (!isEncoder && l_tccp->qmfbid == 0) is strongly */
1073
                    /* linked to the use of two_invK instead of invK */
1074
6.14M
                    const OPJ_INT32 log2_gain = (!isEncoder &&
1075
6.14M
                                                 l_tccp->qmfbid == 0) ? 0 : (l_band->bandno == 0) ? 0 :
1076
3.97M
                                                (l_band->bandno == 3) ? 2 : 1;
1077
1078
                    /* Nominal dynamic range. Equation E-4 */
1079
6.14M
                    const OPJ_INT32 Rb = (OPJ_INT32)l_image_comp->prec + log2_gain;
1080
1081
                    /* Delta_b value of Equation E-3 in "E.1 Inverse quantization
1082
                    * procedure" of the standard */
1083
6.14M
                    l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
1084
6.14M
                                                      (OPJ_INT32)(Rb - l_step_size->expn))));
1085
6.14M
                }
1086
1087
                /* Mb value of Equation E-2 in "E.1 Inverse quantization
1088
                 * procedure" of the standard */
1089
6.14M
                l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
1090
6.14M
                                 1;
1091
1092
6.14M
                if (!l_band->precincts && (l_nb_precincts > 0U)) {
1093
2.26M
                    l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
1094
2.26M
                                            l_nb_precinct_size);
1095
2.26M
                    if (! l_band->precincts) {
1096
0
                        opj_event_msg(manager, EVT_ERROR,
1097
0
                                      "Not enough memory to handle band precints\n");
1098
0
                        return OPJ_FALSE;
1099
0
                    }
1100
                    /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */
1101
2.26M
                    memset(l_band->precincts, 0, l_nb_precinct_size);
1102
2.26M
                    l_band->precincts_data_size = l_nb_precinct_size;
1103
3.88M
                } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1104
1105
1.12k
                    opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1106
1.12k
                            l_band->precincts,/*3 * */ l_nb_precinct_size);
1107
1.12k
                    if (! new_precincts) {
1108
0
                        opj_event_msg(manager, EVT_ERROR,
1109
0
                                      "Not enough memory to handle band precints\n");
1110
0
                        opj_free(l_band->precincts);
1111
0
                        l_band->precincts = NULL;
1112
0
                        l_band->precincts_data_size = 0;
1113
0
                        return OPJ_FALSE;
1114
0
                    }
1115
1.12k
                    l_band->precincts = new_precincts;
1116
                    /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/
1117
1.12k
                    memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1118
1.12k
                           l_nb_precinct_size - l_band->precincts_data_size);
1119
1.12k
                    l_band->precincts_data_size = l_nb_precinct_size;
1120
1.12k
                }
1121
1122
6.14M
                l_current_precinct = l_band->precincts;
1123
63.8M
                for (precno = 0; precno < l_nb_precincts; ++precno) {
1124
57.7M
                    OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1125
57.7M
                    OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1126
57.7M
                                          (1 << cbgwidthexpn);
1127
57.7M
                    OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1128
57.7M
                                          (1 << cbgheightexpn);
1129
57.7M
                    OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1130
57.7M
                    OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1131
                    /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1132
                    /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1133
1134
                    /* precinct size (global) */
1135
                    /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1136
1137
57.7M
                    l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1138
57.7M
                    l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1139
57.7M
                    l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1140
57.7M
                    l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1141
                    /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/
1142
1143
57.7M
                    tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1144
57.7M
                                                        (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1145
                    /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1146
57.7M
                    tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1147
57.7M
                                                        (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1148
                    /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1149
57.7M
                    brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1150
57.7M
                                                     (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1151
                    /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1152
57.7M
                    brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1153
57.7M
                                                     (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1154
                    /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1155
57.7M
                    l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1156
57.7M
                                                          cblkwidthexpn);
1157
57.7M
                    l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1158
57.7M
                                                          cblkheightexpn);
1159
1160
57.7M
                    l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1161
                    /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */
1162
57.7M
                    if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) <
1163
57.7M
                            l_nb_code_blocks) {
1164
0
                        opj_event_msg(manager, EVT_ERROR,
1165
0
                                      "Size of code block data exceeds system limits\n");
1166
0
                        return OPJ_FALSE;
1167
0
                    }
1168
57.7M
                    l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1169
1170
57.7M
                    if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1171
53.5M
                        l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1172
53.5M
                        if (! l_current_precinct->cblks.blocks) {
1173
0
                            return OPJ_FALSE;
1174
0
                        }
1175
                        /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1176
1177
53.5M
                        memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1178
1179
53.5M
                        l_current_precinct->block_size = l_nb_code_blocks_size;
1180
53.5M
                    } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1181
24.1k
                        void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1182
24.1k
                                                       l_nb_code_blocks_size);
1183
24.1k
                        if (! new_blocks) {
1184
0
                            opj_free(l_current_precinct->cblks.blocks);
1185
0
                            l_current_precinct->cblks.blocks = NULL;
1186
0
                            l_current_precinct->block_size = 0;
1187
0
                            opj_event_msg(manager, EVT_ERROR,
1188
0
                                          "Not enough memory for current precinct codeblock element\n");
1189
0
                            return OPJ_FALSE;
1190
0
                        }
1191
24.1k
                        l_current_precinct->cblks.blocks = new_blocks;
1192
                        /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size);     */
1193
1194
24.1k
                        memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1195
24.1k
                               l_current_precinct->block_size
1196
24.1k
                               , 0
1197
24.1k
                               , l_nb_code_blocks_size - l_current_precinct->block_size);
1198
1199
24.1k
                        l_current_precinct->block_size = l_nb_code_blocks_size;
1200
24.1k
                    }
1201
1202
57.7M
                    if (! l_current_precinct->incltree) {
1203
54.7M
                        l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1204
54.7M
                                                       l_current_precinct->ch, manager);
1205
54.7M
                    } else {
1206
3.03M
                        l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1207
3.03M
                                                       l_current_precinct->cw, l_current_precinct->ch, manager);
1208
3.03M
                    }
1209
1210
57.7M
                    if (! l_current_precinct->imsbtree) {
1211
54.7M
                        l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1212
54.7M
                                                       l_current_precinct->ch, manager);
1213
54.7M
                    } else {
1214
3.03M
                        l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1215
3.03M
                                                       l_current_precinct->cw, l_current_precinct->ch, manager);
1216
3.03M
                    }
1217
1218
139M
                    for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1219
81.9M
                        OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1220
81.9M
                                               l_current_precinct->cw) * (1 << cblkwidthexpn);
1221
81.9M
                        OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1222
81.9M
                                               l_current_precinct->cw) * (1 << cblkheightexpn);
1223
81.9M
                        OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1224
81.9M
                        OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1225
1226
81.9M
                        if (isEncoder) {
1227
2.63M
                            opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1228
1229
2.63M
                            if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1230
0
                                return OPJ_FALSE;
1231
0
                            }
1232
                            /* code-block size (global) */
1233
2.63M
                            l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1234
2.63M
                            l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1235
2.63M
                            l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1236
2.63M
                            l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1237
1238
2.63M
                            if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1239
0
                                return OPJ_FALSE;
1240
0
                            }
1241
79.2M
                        } else {
1242
79.2M
                            opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1243
1244
79.2M
                            if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1245
0
                                return OPJ_FALSE;
1246
0
                            }
1247
                            /* code-block size (global) */
1248
79.2M
                            l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1249
79.2M
                            l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1250
79.2M
                            l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1251
79.2M
                            l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1252
79.2M
                        }
1253
81.9M
                    }
1254
57.7M
                    ++l_current_precinct;
1255
57.7M
                } /* precno */
1256
6.14M
            } /* bandno */
1257
2.23M
            ++l_res;
1258
2.23M
        } /* resno */
1259
274k
        ++l_tccp;
1260
274k
        ++l_tilec;
1261
274k
        ++l_image_comp;
1262
274k
    } /* compno */
1263
64.9k
    return OPJ_TRUE;
1264
64.9k
}
1265
1266
OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1267
                                  opj_event_mgr_t* p_manager)
1268
13.8k
{
1269
13.8k
    return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE,
1270
13.8k
                             sizeof(opj_tcd_cblk_enc_t), p_manager);
1271
13.8k
}
1272
1273
OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1274
                                  opj_event_mgr_t* p_manager)
1275
51.1k
{
1276
51.1k
    return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE,
1277
51.1k
                             sizeof(opj_tcd_cblk_dec_t), p_manager);
1278
51.1k
}
1279
1280
/**
1281
 * Allocates memory for an encoding code block (but not data memory).
1282
 */
1283
static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1284
        p_code_block)
1285
2.63M
{
1286
2.63M
    if (! p_code_block->layers) {
1287
        /* no memset since data */
1288
2.63M
        p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1289
2.63M
                               sizeof(opj_tcd_layer_t));
1290
2.63M
        if (! p_code_block->layers) {
1291
0
            return OPJ_FALSE;
1292
0
        }
1293
2.63M
    }
1294
2.63M
    if (! p_code_block->passes) {
1295
2.63M
        p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1296
2.63M
                               sizeof(opj_tcd_pass_t));
1297
2.63M
        if (! p_code_block->passes) {
1298
0
            return OPJ_FALSE;
1299
0
        }
1300
2.63M
    }
1301
2.63M
    return OPJ_TRUE;
1302
2.63M
}
1303
1304
/**
1305
 * Allocates data memory for an encoding code block.
1306
 */
1307
static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1308
        p_code_block)
1309
2.63M
{
1310
2.63M
    OPJ_UINT32 l_data_size;
1311
1312
    /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1313
    /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1314
    /* and +7 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 3) */
1315
    /* and +26 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 7) */
1316
    /* and +28 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 44) */
1317
    /* and +33 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4) */
1318
    /* and +63 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -IMF 2K) */
1319
    /* and +74 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -n 8 -s 7,7 -I) */
1320
    /* TODO: is there a theoretical upper-bound for the compressed code */
1321
    /* block size ? */
1322
2.63M
    l_data_size = 74 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1323
2.63M
                                    (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1324
1325
2.63M
    if (l_data_size > p_code_block->data_size) {
1326
2.63M
        if (p_code_block->data) {
1327
            /* We refer to data - 1 since below we incremented it */
1328
0
            opj_free(p_code_block->data - 1);
1329
0
        }
1330
2.63M
        p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1331
2.63M
        if (! p_code_block->data) {
1332
0
            p_code_block->data_size = 0U;
1333
0
            return OPJ_FALSE;
1334
0
        }
1335
2.63M
        p_code_block->data_size = l_data_size;
1336
1337
        /* We reserve the initial byte as a fake byte to a non-FF value */
1338
        /* and increment the data pointer, so that opj_mqc_init_enc() */
1339
        /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1340
        /* it. */
1341
2.63M
        p_code_block->data[0] = 0;
1342
2.63M
        p_code_block->data += 1; /*why +1 ?*/
1343
2.63M
    }
1344
2.63M
    return OPJ_TRUE;
1345
2.63M
}
1346
1347
1348
void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1349
49.7M
{
1350
49.7M
    memset(seg, 0, sizeof(opj_tcd_seg_t));
1351
49.7M
}
1352
1353
/**
1354
 * Allocates memory for a decoding code block.
1355
 */
1356
static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1357
        p_code_block)
1358
79.2M
{
1359
79.2M
    if (! p_code_block->segs) {
1360
1361
74.3M
        p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1362
74.3M
                             sizeof(opj_tcd_seg_t));
1363
74.3M
        if (! p_code_block->segs) {
1364
0
            return OPJ_FALSE;
1365
0
        }
1366
        /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1367
1368
74.3M
        p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1369
        /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1370
74.3M
    } else {
1371
        /* sanitize */
1372
4.89M
        opj_tcd_seg_t * l_segs = p_code_block->segs;
1373
4.89M
        OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1374
4.89M
        opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1375
4.89M
        OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1376
4.89M
        OPJ_UINT32 i;
1377
1378
4.89M
        opj_aligned_free(p_code_block->decoded_data);
1379
4.89M
        p_code_block->decoded_data = 00;
1380
1381
4.89M
        memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1382
4.89M
        p_code_block->segs = l_segs;
1383
4.89M
        p_code_block->m_current_max_segs = l_current_max_segs;
1384
53.8M
        for (i = 0; i < l_current_max_segs; ++i) {
1385
48.9M
            opj_tcd_reinit_segment(&l_segs[i]);
1386
48.9M
        }
1387
4.89M
        p_code_block->chunks = l_chunks;
1388
4.89M
        p_code_block->numchunksalloc = l_numchunksalloc;
1389
4.89M
    }
1390
1391
79.2M
    return OPJ_TRUE;
1392
79.2M
}
1393
1394
OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
1395
        OPJ_BOOL take_into_account_partial_decoding)
1396
0
{
1397
0
    OPJ_UINT32 i;
1398
0
    OPJ_UINT32 l_data_size = 0;
1399
0
    opj_image_comp_t * l_img_comp = 00;
1400
0
    opj_tcd_tilecomp_t * l_tile_comp = 00;
1401
0
    opj_tcd_resolution_t * l_res = 00;
1402
0
    OPJ_UINT32 l_size_comp, l_remaining;
1403
0
    OPJ_UINT32 l_temp;
1404
1405
0
    l_tile_comp = p_tcd->tcd_image->tiles->comps;
1406
0
    l_img_comp = p_tcd->image->comps;
1407
1408
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
1409
0
        OPJ_UINT32 w, h;
1410
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1411
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
1412
1413
0
        if (l_remaining) {
1414
0
            ++l_size_comp;
1415
0
        }
1416
1417
0
        if (l_size_comp == 3) {
1418
0
            l_size_comp = 4;
1419
0
        }
1420
1421
0
        l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1422
0
        if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
1423
0
            w = l_res->win_x1 - l_res->win_x0;
1424
0
            h = l_res->win_y1 - l_res->win_y0;
1425
0
        } else {
1426
0
            w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1427
0
            h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1428
0
        }
1429
0
        if (h > 0 && UINT_MAX / w < h) {
1430
0
            return UINT_MAX;
1431
0
        }
1432
0
        l_temp = w * h;
1433
0
        if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1434
0
            return UINT_MAX;
1435
0
        }
1436
0
        l_temp *= l_size_comp;
1437
1438
0
        if (l_temp > UINT_MAX - l_data_size) {
1439
0
            return UINT_MAX;
1440
0
        }
1441
0
        l_data_size += l_temp;
1442
0
        ++l_img_comp;
1443
0
        ++l_tile_comp;
1444
0
    }
1445
1446
0
    return l_data_size;
1447
0
}
1448
1449
OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1450
                             OPJ_UINT32 p_tile_no,
1451
                             OPJ_BYTE *p_dest,
1452
                             OPJ_UINT32 * p_data_written,
1453
                             OPJ_UINT32 p_max_length,
1454
                             opj_codestream_info_t *p_cstr_info,
1455
                             opj_tcd_marker_info_t* p_marker_info,
1456
                             opj_event_mgr_t *p_manager)
1457
13.8k
{
1458
1459
13.8k
    if (p_tcd->cur_tp_num == 0) {
1460
1461
13.8k
        p_tcd->tcd_tileno = p_tile_no;
1462
13.8k
        p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1463
1464
        /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1465
13.8k
        if (p_cstr_info)  {
1466
0
            OPJ_UINT32 l_num_packs = 0;
1467
0
            OPJ_UINT32 i;
1468
0
            opj_tcd_tilecomp_t *l_tilec_idx =
1469
0
                &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1470
0
            opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1471
1472
0
            for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1473
0
                opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1474
1475
0
                p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1476
0
                p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1477
1478
0
                l_num_packs += l_res_idx->pw * l_res_idx->ph;
1479
0
                p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1480
0
                p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1481
0
            }
1482
0
            p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1483
0
                    OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
1484
0
                                                  l_num_packs,
1485
0
                                                  sizeof(opj_packet_info_t));
1486
0
            if (!p_cstr_info->tile[p_tile_no].packet) {
1487
                /* FIXME event manager error callback */
1488
0
                return OPJ_FALSE;
1489
0
            }
1490
0
        }
1491
        /* << INDEX */
1492
1493
        /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1494
        /*---------------TILE-------------------*/
1495
13.8k
        if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1496
0
            return OPJ_FALSE;
1497
0
        }
1498
        /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1499
1500
        /* FIXME _ProfStart(PGROUP_MCT); */
1501
13.8k
        if (! opj_tcd_mct_encode(p_tcd)) {
1502
0
            return OPJ_FALSE;
1503
0
        }
1504
        /* FIXME _ProfStop(PGROUP_MCT); */
1505
1506
        /* FIXME _ProfStart(PGROUP_DWT); */
1507
13.8k
        if (! opj_tcd_dwt_encode(p_tcd)) {
1508
0
            return OPJ_FALSE;
1509
0
        }
1510
        /* FIXME  _ProfStop(PGROUP_DWT); */
1511
1512
        /* FIXME  _ProfStart(PGROUP_T1); */
1513
13.8k
        if (! opj_tcd_t1_encode(p_tcd)) {
1514
0
            return OPJ_FALSE;
1515
0
        }
1516
        /* FIXME _ProfStop(PGROUP_T1); */
1517
1518
        /* FIXME _ProfStart(PGROUP_RATE); */
1519
13.8k
        if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1520
13.8k
                                           p_cstr_info, p_manager)) {
1521
0
            return OPJ_FALSE;
1522
0
        }
1523
        /* FIXME _ProfStop(PGROUP_RATE); */
1524
1525
13.8k
    }
1526
    /*--------------TIER2------------------*/
1527
1528
    /* INDEX */
1529
13.8k
    if (p_cstr_info) {
1530
0
        p_cstr_info->index_write = 1;
1531
0
    }
1532
    /* FIXME _ProfStart(PGROUP_T2); */
1533
1534
13.8k
    if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1535
13.8k
                            p_cstr_info, p_marker_info, p_manager)) {
1536
54
        return OPJ_FALSE;
1537
54
    }
1538
    /* FIXME _ProfStop(PGROUP_T2); */
1539
1540
    /*---------------CLEAN-------------------*/
1541
1542
13.7k
    return OPJ_TRUE;
1543
13.8k
}
1544
1545
OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1546
                             OPJ_UINT32 win_x0,
1547
                             OPJ_UINT32 win_y0,
1548
                             OPJ_UINT32 win_x1,
1549
                             OPJ_UINT32 win_y1,
1550
                             OPJ_UINT32 numcomps_to_decode,
1551
                             const OPJ_UINT32 *comps_indices,
1552
                             OPJ_BYTE *p_src,
1553
                             OPJ_UINT32 p_max_length,
1554
                             OPJ_UINT32 p_tile_no,
1555
                             opj_codestream_index_t *p_cstr_index,
1556
                             opj_event_mgr_t *p_manager
1557
                            )
1558
50.8k
{
1559
50.8k
    OPJ_UINT32 l_data_read;
1560
50.8k
    OPJ_UINT32 compno;
1561
1562
50.8k
    p_tcd->tcd_tileno = p_tile_no;
1563
50.8k
    p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1564
50.8k
    p_tcd->win_x0 = win_x0;
1565
50.8k
    p_tcd->win_y0 = win_y0;
1566
50.8k
    p_tcd->win_x1 = win_x1;
1567
50.8k
    p_tcd->win_y1 = win_y1;
1568
50.8k
    p_tcd->whole_tile_decoding = OPJ_TRUE;
1569
1570
50.8k
    opj_free(p_tcd->used_component);
1571
50.8k
    p_tcd->used_component = NULL;
1572
1573
50.8k
    if (numcomps_to_decode) {
1574
0
        OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
1575
0
                                   p_tcd->image->numcomps);
1576
0
        if (used_component == NULL) {
1577
0
            return OPJ_FALSE;
1578
0
        }
1579
0
        for (compno = 0; compno < numcomps_to_decode; compno++) {
1580
0
            used_component[ comps_indices[compno] ] = OPJ_TRUE;
1581
0
        }
1582
1583
0
        p_tcd->used_component = used_component;
1584
0
    }
1585
1586
184k
    for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1587
167k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1588
0
            continue;
1589
0
        }
1590
1591
167k
        if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
1592
34.1k
            p_tcd->whole_tile_decoding = OPJ_FALSE;
1593
34.1k
            break;
1594
34.1k
        }
1595
167k
    }
1596
1597
50.8k
    if (p_tcd->whole_tile_decoding) {
1598
148k
        for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1599
131k
            opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1600
131k
            opj_tcd_resolution_t *l_res = &
1601
131k
                                          (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
1602
131k
            OPJ_SIZE_T l_data_size;
1603
1604
            /* compute l_data_size with overflow check */
1605
131k
            OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
1606
131k
            OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
1607
1608
131k
            if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1609
0
                continue;
1610
0
            }
1611
1612
            /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
1613
131k
            if (res_h > 0 && res_w > SIZE_MAX / res_h) {
1614
0
                opj_event_msg(p_manager, EVT_ERROR,
1615
0
                              "Size of tile data exceeds system limits\n");
1616
0
                return OPJ_FALSE;
1617
0
            }
1618
131k
            l_data_size = res_w * res_h;
1619
1620
131k
            if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
1621
0
                opj_event_msg(p_manager, EVT_ERROR,
1622
0
                              "Size of tile data exceeds system limits\n");
1623
0
                return OPJ_FALSE;
1624
0
            }
1625
131k
            l_data_size *= sizeof(OPJ_UINT32);
1626
1627
131k
            tilec->data_size_needed = l_data_size;
1628
1629
131k
            if (!opj_alloc_tile_component_data(tilec)) {
1630
0
                opj_event_msg(p_manager, EVT_ERROR,
1631
0
                              "Size of tile data exceeds system limits\n");
1632
0
                return OPJ_FALSE;
1633
0
            }
1634
131k
        }
1635
34.1k
    } else {
1636
        /* Compute restricted tile-component and tile-resolution coordinates */
1637
        /* of the window of interest, but defer the memory allocation until */
1638
        /* we know the resno_decoded */
1639
160k
        for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1640
126k
            OPJ_UINT32 resno;
1641
126k
            opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1642
126k
            opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1643
1644
126k
            if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1645
0
                continue;
1646
0
            }
1647
1648
            /* Compute the intersection of the area of interest, expressed in tile coordinates */
1649
            /* with the tile coordinates */
1650
126k
            tilec->win_x0 = opj_uint_max(
1651
126k
                                (OPJ_UINT32)tilec->x0,
1652
126k
                                opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
1653
126k
            tilec->win_y0 = opj_uint_max(
1654
126k
                                (OPJ_UINT32)tilec->y0,
1655
126k
                                opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
1656
126k
            tilec->win_x1 = opj_uint_min(
1657
126k
                                (OPJ_UINT32)tilec->x1,
1658
126k
                                opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
1659
126k
            tilec->win_y1 = opj_uint_min(
1660
126k
                                (OPJ_UINT32)tilec->y1,
1661
126k
                                opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
1662
126k
            if (tilec->win_x1 < tilec->win_x0 ||
1663
126k
                    tilec->win_y1 < tilec->win_y0) {
1664
                /* We should not normally go there. The circumstance is when */
1665
                /* the tile coordinates do not intersect the area of interest */
1666
                /* Upper level logic should not even try to decode that tile */
1667
31
                opj_event_msg(p_manager, EVT_ERROR,
1668
31
                              "Invalid tilec->win_xxx values\n");
1669
31
                return OPJ_FALSE;
1670
31
            }
1671
1672
1.23M
            for (resno = 0; resno < tilec->numresolutions; ++resno) {
1673
1.10M
                opj_tcd_resolution_t *res = tilec->resolutions + resno;
1674
1.10M
                res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
1675
1.10M
                                                   tilec->numresolutions - 1 - resno);
1676
1.10M
                res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
1677
1.10M
                                                   tilec->numresolutions - 1 - resno);
1678
1.10M
                res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
1679
1.10M
                                                   tilec->numresolutions - 1 - resno);
1680
1.10M
                res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
1681
1.10M
                                                   tilec->numresolutions - 1 - resno);
1682
1.10M
            }
1683
126k
        }
1684
34.1k
    }
1685
1686
#ifdef TODO_MSD /* FIXME */
1687
    /* INDEX >>  */
1688
    if (p_cstr_info) {
1689
        OPJ_UINT32 resno, compno, numprec = 0;
1690
        for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1691
            opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1692
            opj_tccp_t *tccp = &tcp->tccps[compno];
1693
            opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1694
            for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1695
                opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1696
                p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1697
                p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1698
                numprec += res_idx->pw * res_idx->ph;
1699
                p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1700
                p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1701
            }
1702
        }
1703
        p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1704
                p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1705
        p_cstr_info->packno = 0;
1706
    }
1707
    /* << INDEX */
1708
#endif
1709
1710
    /*--------------TIER2------------------*/
1711
    /* FIXME _ProfStart(PGROUP_T2); */
1712
50.8k
    l_data_read = 0;
1713
50.8k
    if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1714
50.8k
                            p_manager)) {
1715
1.84k
        return OPJ_FALSE;
1716
1.84k
    }
1717
    /* FIXME _ProfStop(PGROUP_T2); */
1718
1719
    /*------------------TIER1-----------------*/
1720
1721
    /* FIXME _ProfStart(PGROUP_T1); */
1722
49.0k
    if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1723
2.61k
        return OPJ_FALSE;
1724
2.61k
    }
1725
    /* FIXME _ProfStop(PGROUP_T1); */
1726
1727
1728
    /* For subtile decoding, now we know the resno_decoded, we can allocate */
1729
    /* the tile data buffer */
1730
46.4k
    if (!p_tcd->whole_tile_decoding) {
1731
145k
        for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1732
115k
            opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1733
115k
            opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1734
115k
            opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
1735
115k
            OPJ_SIZE_T w = res->win_x1 - res->win_x0;
1736
115k
            OPJ_SIZE_T h = res->win_y1 - res->win_y0;
1737
115k
            OPJ_SIZE_T l_data_size;
1738
1739
115k
            opj_image_data_free(tilec->data_win);
1740
115k
            tilec->data_win = NULL;
1741
1742
115k
            if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1743
0
                continue;
1744
0
            }
1745
1746
115k
            if (w > 0 && h > 0) {
1747
86.8k
                if (w > SIZE_MAX / h) {
1748
0
                    opj_event_msg(p_manager, EVT_ERROR,
1749
0
                                  "Size of tile data exceeds system limits\n");
1750
0
                    return OPJ_FALSE;
1751
0
                }
1752
86.8k
                l_data_size = w * h;
1753
86.8k
                if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
1754
0
                    opj_event_msg(p_manager, EVT_ERROR,
1755
0
                                  "Size of tile data exceeds system limits\n");
1756
0
                    return OPJ_FALSE;
1757
0
                }
1758
86.8k
                l_data_size *= sizeof(OPJ_INT32);
1759
1760
86.8k
                tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
1761
86.8k
                if (tilec->data_win == NULL) {
1762
0
                    opj_event_msg(p_manager, EVT_ERROR,
1763
0
                                  "Size of tile data exceeds system limits\n");
1764
0
                    return OPJ_FALSE;
1765
0
                }
1766
86.8k
            }
1767
115k
        }
1768
30.3k
    }
1769
1770
    /*----------------DWT---------------------*/
1771
1772
    /* FIXME _ProfStart(PGROUP_DWT); */
1773
46.4k
    if
1774
46.4k
    (! opj_tcd_dwt_decode(p_tcd)) {
1775
0
        return OPJ_FALSE;
1776
0
    }
1777
    /* FIXME _ProfStop(PGROUP_DWT); */
1778
1779
    /*----------------MCT-------------------*/
1780
    /* FIXME _ProfStart(PGROUP_MCT); */
1781
46.4k
    if
1782
46.4k
    (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1783
906
        return OPJ_FALSE;
1784
906
    }
1785
    /* FIXME _ProfStop(PGROUP_MCT); */
1786
1787
    /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1788
45.4k
    if
1789
45.4k
    (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1790
0
        return OPJ_FALSE;
1791
0
    }
1792
    /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1793
1794
1795
    /*---------------TILE-------------------*/
1796
45.4k
    return OPJ_TRUE;
1797
45.4k
}
1798
1799
OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1800
                                  OPJ_BYTE * p_dest,
1801
                                  OPJ_UINT32 p_dest_length
1802
                                 )
1803
0
{
1804
0
    OPJ_UINT32 i, j, k, l_data_size = 0;
1805
0
    opj_image_comp_t * l_img_comp = 00;
1806
0
    opj_tcd_tilecomp_t * l_tilec = 00;
1807
0
    opj_tcd_resolution_t * l_res;
1808
0
    OPJ_UINT32 l_size_comp, l_remaining;
1809
0
    OPJ_UINT32 l_stride, l_width, l_height;
1810
1811
0
    l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
1812
0
    if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1813
0
        return OPJ_FALSE;
1814
0
    }
1815
1816
0
    l_tilec = p_tcd->tcd_image->tiles->comps;
1817
0
    l_img_comp = p_tcd->image->comps;
1818
1819
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
1820
0
        const OPJ_INT32* l_src_data;
1821
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1822
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
1823
0
        l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1824
0
        if (p_tcd->whole_tile_decoding) {
1825
0
            l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1826
0
            l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1827
0
            l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
1828
0
                                                                     1].x1 -
1829
0
                                    l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
1830
0
            l_src_data = l_tilec->data;
1831
0
        } else {
1832
0
            l_width = l_res->win_x1 - l_res->win_x0;
1833
0
            l_height = l_res->win_y1 - l_res->win_y0;
1834
0
            l_stride = 0;
1835
0
            l_src_data = l_tilec->data_win;
1836
0
        }
1837
1838
0
        if (l_remaining) {
1839
0
            ++l_size_comp;
1840
0
        }
1841
1842
0
        if (l_size_comp == 3) {
1843
0
            l_size_comp = 4;
1844
0
        }
1845
1846
0
        switch (l_size_comp) {
1847
0
        case 1: {
1848
0
            OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1849
0
            const OPJ_INT32 * l_src_ptr = l_src_data;
1850
1851
0
            if (l_img_comp->sgnd) {
1852
0
                for (j = 0; j < l_height; ++j) {
1853
0
                    for (k = 0; k < l_width; ++k) {
1854
0
                        *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1855
0
                    }
1856
0
                    l_src_ptr += l_stride;
1857
0
                }
1858
0
            } else {
1859
0
                for (j = 0; j < l_height; ++j) {
1860
0
                    for (k = 0; k < l_width; ++k) {
1861
0
                        *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1862
0
                    }
1863
0
                    l_src_ptr += l_stride;
1864
0
                }
1865
0
            }
1866
1867
0
            p_dest = (OPJ_BYTE *)l_dest_ptr;
1868
0
        }
1869
0
        break;
1870
0
        case 2: {
1871
0
            const OPJ_INT32 * l_src_ptr = l_src_data;
1872
0
            OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1873
1874
0
            if (l_img_comp->sgnd) {
1875
0
                for (j = 0; j < l_height; ++j) {
1876
0
                    for (k = 0; k < l_width; ++k) {
1877
0
                        OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1878
0
                        memcpy(l_dest_ptr, &val, sizeof(val));
1879
0
                        l_dest_ptr ++;
1880
0
                    }
1881
0
                    l_src_ptr += l_stride;
1882
0
                }
1883
0
            } else {
1884
0
                for (j = 0; j < l_height; ++j) {
1885
0
                    for (k = 0; k < l_width; ++k) {
1886
0
                        OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1887
0
                        memcpy(l_dest_ptr, &val, sizeof(val));
1888
0
                        l_dest_ptr ++;
1889
0
                    }
1890
0
                    l_src_ptr += l_stride;
1891
0
                }
1892
0
            }
1893
1894
0
            p_dest = (OPJ_BYTE*) l_dest_ptr;
1895
0
        }
1896
0
        break;
1897
0
        case 4: {
1898
0
            OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1899
0
            const OPJ_INT32 * l_src_ptr = l_src_data;
1900
1901
0
            for (j = 0; j < l_height; ++j) {
1902
0
                memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1903
0
                l_dest_ptr += l_width;
1904
0
                l_src_ptr += l_width + l_stride;
1905
0
            }
1906
1907
0
            p_dest = (OPJ_BYTE*) l_dest_ptr;
1908
0
        }
1909
0
        break;
1910
0
        }
1911
1912
0
        ++l_img_comp;
1913
0
        ++l_tilec;
1914
0
    }
1915
1916
0
    return OPJ_TRUE;
1917
0
}
1918
1919
1920
1921
1922
static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1923
62.0k
{
1924
62.0k
    OPJ_UINT32 compno, resno, bandno, precno;
1925
62.0k
    opj_tcd_tile_t *l_tile = 00;
1926
62.0k
    opj_tcd_tilecomp_t *l_tile_comp = 00;
1927
62.0k
    opj_tcd_resolution_t *l_res = 00;
1928
62.0k
    opj_tcd_band_t *l_band = 00;
1929
62.0k
    opj_tcd_precinct_t *l_precinct = 00;
1930
62.0k
    OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1931
62.0k
    void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1932
1933
62.0k
    if (! p_tcd) {
1934
0
        return;
1935
0
    }
1936
1937
62.0k
    if (! p_tcd->tcd_image) {
1938
0
        return;
1939
0
    }
1940
1941
62.0k
    if (p_tcd->m_is_decoder) {
1942
48.1k
        l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1943
48.1k
    } else {
1944
13.8k
        l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1945
13.8k
    }
1946
1947
62.0k
    l_tile = p_tcd->tcd_image->tiles;
1948
62.0k
    if (! l_tile) {
1949
0
        return;
1950
0
    }
1951
1952
62.0k
    l_tile_comp = l_tile->comps;
1953
1954
285k
    for (compno = 0; compno < l_tile->numcomps; ++compno) {
1955
223k
        l_res = l_tile_comp->resolutions;
1956
223k
        if (l_res) {
1957
1958
211k
            l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
1959
211k
                                   opj_tcd_resolution_t);
1960
1.82M
            for (resno = 0; resno < l_nb_resolutions; ++resno) {
1961
1.60M
                l_band = l_res->bands;
1962
6.43M
                for (bandno = 0; bandno < 3; ++bandno) {
1963
4.82M
                    l_precinct = l_band->precincts;
1964
4.82M
                    if (l_precinct) {
1965
1966
2.26M
                        l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
1967
2.26M
                                             opj_tcd_precinct_t);
1968
56.9M
                        for (precno = 0; precno < l_nb_precincts; ++precno) {
1969
54.6M
                            opj_tgt_destroy(l_precinct->incltree);
1970
54.6M
                            l_precinct->incltree = 00;
1971
54.6M
                            opj_tgt_destroy(l_precinct->imsbtree);
1972
54.6M
                            l_precinct->imsbtree = 00;
1973
54.6M
                            (*l_tcd_code_block_deallocate)(l_precinct);
1974
54.6M
                            ++l_precinct;
1975
54.6M
                        }
1976
1977
2.26M
                        opj_free(l_band->precincts);
1978
2.26M
                        l_band->precincts = 00;
1979
2.26M
                    }
1980
4.82M
                    ++l_band;
1981
4.82M
                } /* for (resno */
1982
1.60M
                ++l_res;
1983
1.60M
            }
1984
1985
211k
            opj_free(l_tile_comp->resolutions);
1986
211k
            l_tile_comp->resolutions = 00;
1987
211k
        }
1988
1989
223k
        if (l_tile_comp->ownsData && l_tile_comp->data) {
1990
61.9k
            opj_image_data_free(l_tile_comp->data);
1991
61.9k
            l_tile_comp->data = 00;
1992
61.9k
            l_tile_comp->ownsData = 0;
1993
61.9k
            l_tile_comp->data_size = 0;
1994
61.9k
            l_tile_comp->data_size_needed = 0;
1995
61.9k
        }
1996
1997
223k
        opj_image_data_free(l_tile_comp->data_win);
1998
1999
223k
        ++l_tile_comp;
2000
223k
    }
2001
2002
62.0k
    opj_free(l_tile->comps);
2003
62.0k
    l_tile->comps = 00;
2004
62.0k
    opj_free(p_tcd->tcd_image->tiles);
2005
62.0k
    p_tcd->tcd_image->tiles = 00;
2006
62.0k
}
2007
2008
2009
static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
2010
                                  OPJ_BYTE * p_src_data,
2011
                                  OPJ_UINT32 * p_data_read,
2012
                                  OPJ_UINT32 p_max_src_size,
2013
                                  opj_codestream_index_t *p_cstr_index,
2014
                                  opj_event_mgr_t *p_manager
2015
                                 )
2016
50.8k
{
2017
50.8k
    opj_t2_t * l_t2;
2018
2019
50.8k
    l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2020
50.8k
    if (l_t2 == 00) {
2021
0
        return OPJ_FALSE;
2022
0
    }
2023
2024
50.8k
    if (! opj_t2_decode_packets(
2025
50.8k
                p_tcd,
2026
50.8k
                l_t2,
2027
50.8k
                p_tcd->tcd_tileno,
2028
50.8k
                p_tcd->tcd_image->tiles,
2029
50.8k
                p_src_data,
2030
50.8k
                p_data_read,
2031
50.8k
                p_max_src_size,
2032
50.8k
                p_cstr_index,
2033
50.8k
                p_manager)) {
2034
1.84k
        opj_t2_destroy(l_t2);
2035
1.84k
        return OPJ_FALSE;
2036
1.84k
    }
2037
2038
49.0k
    opj_t2_destroy(l_t2);
2039
2040
    /*---------------CLEAN-------------------*/
2041
49.0k
    return OPJ_TRUE;
2042
50.8k
}
2043
2044
static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2045
49.0k
{
2046
49.0k
    OPJ_UINT32 compno;
2047
49.0k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2048
49.0k
    opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
2049
49.0k
    opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2050
49.0k
    volatile OPJ_BOOL ret = OPJ_TRUE;
2051
49.0k
    OPJ_BOOL check_pterm = OPJ_FALSE;
2052
49.0k
    opj_mutex_t* p_manager_mutex = NULL;
2053
2054
49.0k
    p_manager_mutex = opj_mutex_create();
2055
2056
    /* Only enable PTERM check if we decode all layers */
2057
49.0k
    if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
2058
49.0k
            (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
2059
14.5k
        check_pterm = OPJ_TRUE;
2060
14.5k
    }
2061
2062
291k
    for (compno = 0; compno < l_tile->numcomps;
2063
244k
            ++compno, ++l_tile_comp, ++l_tccp) {
2064
244k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2065
0
            continue;
2066
0
        }
2067
2068
244k
        opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
2069
244k
                            p_manager, p_manager_mutex, check_pterm);
2070
244k
        if (!ret) {
2071
2.61k
            break;
2072
2.61k
        }
2073
244k
    }
2074
2075
49.0k
    opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
2076
49.0k
    if (p_manager_mutex) {
2077
49.0k
        opj_mutex_destroy(p_manager_mutex);
2078
49.0k
    }
2079
49.0k
    return ret;
2080
49.0k
}
2081
2082
2083
static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
2084
46.4k
{
2085
46.4k
    OPJ_UINT32 compno;
2086
46.4k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2087
46.4k
    opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2088
46.4k
    opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2089
46.4k
    opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2090
2091
286k
    for (compno = 0; compno < l_tile->numcomps;
2092
239k
            compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
2093
239k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2094
0
            continue;
2095
0
        }
2096
2097
239k
        if (l_tccp->qmfbid == 1) {
2098
146k
            if (! opj_dwt_decode(p_tcd, l_tile_comp,
2099
146k
                                 l_img_comp->resno_decoded + 1)) {
2100
0
                return OPJ_FALSE;
2101
0
            }
2102
146k
        } else {
2103
93.2k
            if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
2104
93.2k
                                      l_img_comp->resno_decoded + 1)) {
2105
0
                return OPJ_FALSE;
2106
0
            }
2107
93.2k
        }
2108
2109
239k
    }
2110
2111
46.4k
    return OPJ_TRUE;
2112
46.4k
}
2113
2114
static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2115
46.4k
{
2116
46.4k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2117
46.4k
    opj_tcp_t * l_tcp = p_tcd->tcp;
2118
46.4k
    opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2119
46.4k
    OPJ_SIZE_T l_samples;
2120
46.4k
    OPJ_UINT32 i;
2121
2122
46.4k
    if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
2123
35.0k
        return OPJ_TRUE;
2124
35.0k
    }
2125
2126
11.3k
    if (p_tcd->whole_tile_decoding) {
2127
5.08k
        opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2128
5.08k
                                          l_tile_comp->minimum_num_resolutions - 1;
2129
2130
        /* A bit inefficient: we process more data than needed if */
2131
        /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
2132
        /* but we would need to take into account a stride then */
2133
5.08k
        l_samples = (OPJ_SIZE_T)(res_comp0->x1 - res_comp0->x0) *
2134
5.08k
                    (OPJ_SIZE_T)(res_comp0->y1 - res_comp0->y0);
2135
5.08k
        if (l_tile->numcomps >= 3) {
2136
3.01k
            if (l_tile_comp->minimum_num_resolutions !=
2137
3.01k
                    l_tile->comps[1].minimum_num_resolutions ||
2138
3.01k
                    l_tile_comp->minimum_num_resolutions !=
2139
3.00k
                    l_tile->comps[2].minimum_num_resolutions) {
2140
18
                opj_event_msg(p_manager, EVT_ERROR,
2141
18
                              "Tiles don't all have the same dimension. Skip the MCT step.\n");
2142
18
                return OPJ_FALSE;
2143
18
            }
2144
3.01k
        }
2145
5.07k
        if (l_tile->numcomps >= 3) {
2146
2.99k
            opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2147
2.99k
                                              l_tile_comp->minimum_num_resolutions - 1;
2148
2.99k
            opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2149
2.99k
                                              l_tile_comp->minimum_num_resolutions - 1;
2150
            /* testcase 1336.pdf.asan.47.376 */
2151
2.99k
            if (p_tcd->image->comps[0].resno_decoded !=
2152
2.99k
                    p_tcd->image->comps[1].resno_decoded ||
2153
2.99k
                    p_tcd->image->comps[0].resno_decoded !=
2154
2.95k
                    p_tcd->image->comps[2].resno_decoded ||
2155
2.99k
                    (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
2156
2.91k
                    (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
2157
2.99k
                    (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
2158
2.79k
                    (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
2159
271
                opj_event_msg(p_manager, EVT_ERROR,
2160
271
                              "Tiles don't all have the same dimension. Skip the MCT step.\n");
2161
271
                return OPJ_FALSE;
2162
271
            }
2163
2.99k
        }
2164
6.29k
    } else {
2165
6.29k
        opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2166
6.29k
                                          p_tcd->image->comps[0].resno_decoded;
2167
2168
6.29k
        l_samples = (OPJ_SIZE_T)(res_comp0->win_x1 - res_comp0->win_x0) *
2169
6.29k
                    (OPJ_SIZE_T)(res_comp0->win_y1 - res_comp0->win_y0);
2170
6.29k
        if (l_tile->numcomps >= 3) {
2171
3.52k
            opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2172
3.52k
                                              p_tcd->image->comps[1].resno_decoded;
2173
3.52k
            opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2174
3.52k
                                              p_tcd->image->comps[2].resno_decoded;
2175
            /* testcase 1336.pdf.asan.47.376 */
2176
3.52k
            if (p_tcd->image->comps[0].resno_decoded !=
2177
3.52k
                    p_tcd->image->comps[1].resno_decoded ||
2178
3.52k
                    p_tcd->image->comps[0].resno_decoded !=
2179
3.47k
                    p_tcd->image->comps[2].resno_decoded ||
2180
3.52k
                    (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
2181
3.44k
                    (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
2182
3.52k
                    (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
2183
2.96k
                    (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
2184
617
                opj_event_msg(p_manager, EVT_ERROR,
2185
617
                              "Tiles don't all have the same dimension. Skip the MCT step.\n");
2186
617
                return OPJ_FALSE;
2187
617
            }
2188
3.52k
        }
2189
6.29k
    }
2190
2191
10.4k
    if (l_tile->numcomps >= 3) {
2192
5.63k
        if (l_tcp->mct == 2) {
2193
0
            OPJ_BYTE ** l_data;
2194
2195
0
            if (! l_tcp->m_mct_decoding_matrix) {
2196
0
                return OPJ_TRUE;
2197
0
            }
2198
2199
0
            l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2200
0
            if (! l_data) {
2201
0
                return OPJ_FALSE;
2202
0
            }
2203
2204
0
            for (i = 0; i < l_tile->numcomps; ++i) {
2205
0
                if (p_tcd->whole_tile_decoding) {
2206
0
                    l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2207
0
                } else {
2208
0
                    l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
2209
0
                }
2210
0
                ++l_tile_comp;
2211
0
            }
2212
2213
0
            if (! opj_mct_decode_custom(/* MCT data */
2214
0
                        (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2215
                        /* size of components */
2216
0
                        l_samples,
2217
                        /* components */
2218
0
                        l_data,
2219
                        /* nb of components (i.e. size of pData) */
2220
0
                        l_tile->numcomps,
2221
                        /* tells if the data is signed */
2222
0
                        p_tcd->image->comps->sgnd)) {
2223
0
                opj_free(l_data);
2224
0
                return OPJ_FALSE;
2225
0
            }
2226
2227
0
            opj_free(l_data);
2228
5.63k
        } else {
2229
5.63k
            if (l_tcp->tccps->qmfbid == 1) {
2230
1.89k
                if (p_tcd->whole_tile_decoding) {
2231
898
                    opj_mct_decode(l_tile->comps[0].data,
2232
898
                                   l_tile->comps[1].data,
2233
898
                                   l_tile->comps[2].data,
2234
898
                                   l_samples);
2235
992
                } else {
2236
992
                    opj_mct_decode(l_tile->comps[0].data_win,
2237
992
                                   l_tile->comps[1].data_win,
2238
992
                                   l_tile->comps[2].data_win,
2239
992
                                   l_samples);
2240
992
                }
2241
3.74k
            } else {
2242
3.74k
                if (p_tcd->whole_tile_decoding) {
2243
1.82k
                    opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
2244
1.82k
                                        (OPJ_FLOAT32*)l_tile->comps[1].data,
2245
1.82k
                                        (OPJ_FLOAT32*)l_tile->comps[2].data,
2246
1.82k
                                        l_samples);
2247
1.91k
                } else {
2248
1.91k
                    opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
2249
1.91k
                                        (OPJ_FLOAT32*)l_tile->comps[1].data_win,
2250
1.91k
                                        (OPJ_FLOAT32*)l_tile->comps[2].data_win,
2251
1.91k
                                        l_samples);
2252
1.91k
                }
2253
3.74k
            }
2254
5.63k
        }
2255
5.63k
    } else {
2256
4.83k
        opj_event_msg(p_manager, EVT_ERROR,
2257
4.83k
                      "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
2258
4.83k
                      l_tile->numcomps);
2259
4.83k
    }
2260
2261
10.4k
    return OPJ_TRUE;
2262
10.4k
}
2263
2264
2265
static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
2266
45.4k
{
2267
45.4k
    OPJ_UINT32 compno;
2268
45.4k
    opj_tcd_tilecomp_t * l_tile_comp = 00;
2269
45.4k
    opj_tccp_t * l_tccp = 00;
2270
45.4k
    opj_image_comp_t * l_img_comp = 00;
2271
45.4k
    opj_tcd_resolution_t* l_res = 00;
2272
45.4k
    opj_tcd_tile_t * l_tile;
2273
45.4k
    OPJ_UINT32 l_width, l_height, i, j;
2274
45.4k
    OPJ_INT32 * l_current_ptr;
2275
45.4k
    OPJ_INT32 l_min, l_max;
2276
45.4k
    OPJ_UINT32 l_stride;
2277
2278
45.4k
    l_tile = p_tcd->tcd_image->tiles;
2279
45.4k
    l_tile_comp = l_tile->comps;
2280
45.4k
    l_tccp = p_tcd->tcp->tccps;
2281
45.4k
    l_img_comp = p_tcd->image->comps;
2282
2283
252k
    for (compno = 0; compno < l_tile->numcomps;
2284
207k
            compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
2285
2286
207k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2287
0
            continue;
2288
0
        }
2289
2290
207k
        l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2291
2292
207k
        if (!p_tcd->whole_tile_decoding) {
2293
99.6k
            l_width = l_res->win_x1 - l_res->win_x0;
2294
99.6k
            l_height = l_res->win_y1 - l_res->win_y0;
2295
99.6k
            l_stride = 0;
2296
99.6k
            l_current_ptr = l_tile_comp->data_win;
2297
107k
        } else {
2298
107k
            l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
2299
107k
            l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
2300
107k
            l_stride = (OPJ_UINT32)(
2301
107k
                           l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
2302
107k
                           l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
2303
107k
                       - l_width;
2304
107k
            l_current_ptr = l_tile_comp->data;
2305
2306
107k
            assert(l_height == 0 ||
2307
107k
                   l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
2308
107k
        }
2309
2310
207k
        if (l_img_comp->sgnd) {
2311
14.2k
            l_min = -(1 << (l_img_comp->prec - 1));
2312
14.2k
            l_max = (1 << (l_img_comp->prec - 1)) - 1;
2313
193k
        } else {
2314
193k
            l_min = 0;
2315
193k
            l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
2316
193k
        }
2317
2318
207k
        if (l_width == 0 || l_height == 0) {
2319
53.8k
            continue;
2320
53.8k
        }
2321
2322
153k
        if (l_tccp->qmfbid == 1) {
2323
14.4M
            for (j = 0; j < l_height; ++j) {
2324
9.40G
                for (i = 0; i < l_width; ++i) {
2325
                    /* TODO: do addition on int64 ? */
2326
9.39G
                    *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
2327
9.39G
                                                   l_max);
2328
9.39G
                    ++l_current_ptr;
2329
9.39G
                }
2330
14.3M
                l_current_ptr += l_stride;
2331
14.3M
            }
2332
100k
        } else {
2333
5.03M
            for (j = 0; j < l_height; ++j) {
2334
2.78G
                for (i = 0; i < l_width; ++i) {
2335
2.77G
                    OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2336
2.77G
                    if (l_value > (OPJ_FLOAT32)INT_MAX) {
2337
9.65M
                        *l_current_ptr = l_max;
2338
2.76G
                    } else if (l_value < INT_MIN) {
2339
9.86M
                        *l_current_ptr = l_min;
2340
2.75G
                    } else {
2341
                        /* Do addition on int64 to avoid overflows */
2342
2.75G
                        OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
2343
2.75G
                        *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
2344
2.75G
                                             l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
2345
2.75G
                    }
2346
2.77G
                    ++l_current_ptr;
2347
2.77G
                }
2348
4.98M
                l_current_ptr += l_stride;
2349
4.98M
            }
2350
53.3k
        }
2351
153k
    }
2352
2353
45.4k
    return OPJ_TRUE;
2354
45.4k
}
2355
2356
2357
2358
/**
2359
 * Deallocates the encoding data of the given precinct.
2360
 */
2361
static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
2362
54.3M
{
2363
54.3M
    OPJ_UINT32 cblkno, l_nb_code_blocks;
2364
2365
54.3M
    opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
2366
54.3M
    if (l_code_block) {
2367
        /*fprintf(stderr,"deallocate codeblock:{\n");*/
2368
        /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
2369
        /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2370
                        l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
2371
2372
2373
53.2M
        l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2374
53.2M
                               opj_tcd_cblk_dec_t);
2375
        /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2376
2377
127M
        for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2378
2379
74.3M
            if (l_code_block->segs) {
2380
74.3M
                opj_free(l_code_block->segs);
2381
74.3M
                l_code_block->segs = 00;
2382
74.3M
            }
2383
2384
74.3M
            if (l_code_block->chunks) {
2385
531k
                opj_free(l_code_block->chunks);
2386
531k
                l_code_block->chunks = 00;
2387
531k
            }
2388
2389
74.3M
            opj_aligned_free(l_code_block->decoded_data);
2390
74.3M
            l_code_block->decoded_data = NULL;
2391
2392
74.3M
            ++l_code_block;
2393
74.3M
        }
2394
2395
53.2M
        opj_free(p_precinct->cblks.dec);
2396
53.2M
        p_precinct->cblks.dec = 00;
2397
53.2M
    }
2398
54.3M
}
2399
2400
/**
2401
 * Deallocates the encoding data of the given precinct.
2402
 */
2403
static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2404
300k
{
2405
300k
    OPJ_UINT32 cblkno, l_nb_code_blocks;
2406
2407
300k
    opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2408
300k
    if (l_code_block) {
2409
298k
        l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2410
298k
                               opj_tcd_cblk_enc_t);
2411
2412
2.93M
        for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
2413
2.63M
            if (l_code_block->data) {
2414
                /* We refer to data - 1 since below we incremented it */
2415
                /* in opj_tcd_code_block_enc_allocate_data() */
2416
2.63M
                opj_free(l_code_block->data - 1);
2417
2.63M
                l_code_block->data = 00;
2418
2.63M
            }
2419
2420
2.63M
            if (l_code_block->layers) {
2421
2.63M
                opj_free(l_code_block->layers);
2422
2.63M
                l_code_block->layers = 00;
2423
2.63M
            }
2424
2425
2.63M
            if (l_code_block->passes) {
2426
2.63M
                opj_free(l_code_block->passes);
2427
2.63M
                l_code_block->passes = 00;
2428
2.63M
            }
2429
2.63M
            ++l_code_block;
2430
2.63M
        }
2431
2432
298k
        opj_free(p_precinct->cblks.enc);
2433
2434
298k
        p_precinct->cblks.enc = 00;
2435
298k
    }
2436
300k
}
2437
2438
OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd)
2439
13.8k
{
2440
13.8k
    OPJ_UINT32 i;
2441
13.8k
    OPJ_SIZE_T l_data_size = 0;
2442
13.8k
    opj_image_comp_t * l_img_comp = 00;
2443
13.8k
    opj_tcd_tilecomp_t * l_tilec = 00;
2444
13.8k
    OPJ_UINT32 l_size_comp, l_remaining;
2445
2446
13.8k
    l_tilec = p_tcd->tcd_image->tiles->comps;
2447
13.8k
    l_img_comp = p_tcd->image->comps;
2448
29.1k
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
2449
15.3k
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2450
15.3k
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
2451
2452
15.3k
        if (l_remaining) {
2453
8.66k
            ++l_size_comp;
2454
8.66k
        }
2455
2456
15.3k
        if (l_size_comp == 3) {
2457
863
            l_size_comp = 4;
2458
863
        }
2459
2460
15.3k
        l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2461
15.3k
                                      (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
2462
15.3k
        ++l_img_comp;
2463
15.3k
        ++l_tilec;
2464
15.3k
    }
2465
2466
13.8k
    return l_data_size;
2467
13.8k
}
2468
2469
static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2470
13.8k
{
2471
13.8k
    OPJ_UINT32 compno;
2472
13.8k
    opj_tcd_tilecomp_t * l_tile_comp = 00;
2473
13.8k
    opj_tccp_t * l_tccp = 00;
2474
13.8k
    opj_image_comp_t * l_img_comp = 00;
2475
13.8k
    opj_tcd_tile_t * l_tile;
2476
13.8k
    OPJ_SIZE_T l_nb_elem, i;
2477
13.8k
    OPJ_INT32 * l_current_ptr;
2478
2479
13.8k
    l_tile = p_tcd->tcd_image->tiles;
2480
13.8k
    l_tile_comp = l_tile->comps;
2481
13.8k
    l_tccp = p_tcd->tcp->tccps;
2482
13.8k
    l_img_comp = p_tcd->image->comps;
2483
2484
29.1k
    for (compno = 0; compno < l_tile->numcomps; compno++) {
2485
15.3k
        l_current_ptr = l_tile_comp->data;
2486
15.3k
        l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2487
15.3k
                    (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2488
2489
15.3k
        if (l_tccp->qmfbid == 1) {
2490
3.45G
            for (i = 0; i < l_nb_elem; ++i) {
2491
3.45G
                *l_current_ptr -= l_tccp->m_dc_level_shift ;
2492
3.45G
                ++l_current_ptr;
2493
3.45G
            }
2494
14.1k
        } else {
2495
1.41G
            for (i = 0; i < l_nb_elem; ++i) {
2496
1.41G
                *((OPJ_FLOAT32 *) l_current_ptr) = (OPJ_FLOAT32)(*l_current_ptr -
2497
1.41G
                                                   l_tccp->m_dc_level_shift);
2498
1.41G
                ++l_current_ptr;
2499
1.41G
            }
2500
1.19k
        }
2501
2502
15.3k
        ++l_img_comp;
2503
15.3k
        ++l_tccp;
2504
15.3k
        ++l_tile_comp;
2505
15.3k
    }
2506
2507
13.8k
    return OPJ_TRUE;
2508
13.8k
}
2509
2510
static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2511
13.8k
{
2512
13.8k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2513
13.8k
    opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2514
13.8k
    OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2515
13.8k
                         (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2516
13.8k
    OPJ_UINT32 i;
2517
13.8k
    OPJ_BYTE ** l_data = 00;
2518
13.8k
    opj_tcp_t * l_tcp = p_tcd->tcp;
2519
2520
13.8k
    if (!p_tcd->tcp->mct) {
2521
13.1k
        return OPJ_TRUE;
2522
13.1k
    }
2523
2524
654
    if (p_tcd->tcp->mct == 2) {
2525
0
        if (! p_tcd->tcp->m_mct_coding_matrix) {
2526
0
            return OPJ_TRUE;
2527
0
        }
2528
2529
0
        l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2530
0
        if (! l_data) {
2531
0
            return OPJ_FALSE;
2532
0
        }
2533
2534
0
        for (i = 0; i < l_tile->numcomps; ++i) {
2535
0
            l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2536
0
            ++l_tile_comp;
2537
0
        }
2538
2539
0
        if (! opj_mct_encode_custom(/* MCT data */
2540
0
                    (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2541
                    /* size of components */
2542
0
                    samples,
2543
                    /* components */
2544
0
                    l_data,
2545
                    /* nb of components (i.e. size of pData) */
2546
0
                    l_tile->numcomps,
2547
                    /* tells if the data is signed */
2548
0
                    p_tcd->image->comps->sgnd)) {
2549
0
            opj_free(l_data);
2550
0
            return OPJ_FALSE;
2551
0
        }
2552
2553
0
        opj_free(l_data);
2554
654
    } else if (l_tcp->tccps->qmfbid == 0) {
2555
0
        opj_mct_encode_real(
2556
0
            (OPJ_FLOAT32*)l_tile->comps[0].data,
2557
0
            (OPJ_FLOAT32*)l_tile->comps[1].data,
2558
0
            (OPJ_FLOAT32*)l_tile->comps[2].data,
2559
0
            samples);
2560
654
    } else {
2561
654
        opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2562
654
                       l_tile->comps[2].data, samples);
2563
654
    }
2564
2565
654
    return OPJ_TRUE;
2566
654
}
2567
2568
static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2569
13.8k
{
2570
13.8k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2571
13.8k
    opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2572
13.8k
    opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2573
13.8k
    OPJ_UINT32 compno;
2574
2575
29.1k
    for (compno = 0; compno < l_tile->numcomps; ++compno) {
2576
15.3k
        if (l_tccp->qmfbid == 1) {
2577
14.1k
            if (! opj_dwt_encode(p_tcd, l_tile_comp)) {
2578
0
                return OPJ_FALSE;
2579
0
            }
2580
14.1k
        } else if (l_tccp->qmfbid == 0) {
2581
1.19k
            if (! opj_dwt_encode_real(p_tcd, l_tile_comp)) {
2582
0
                return OPJ_FALSE;
2583
0
            }
2584
1.19k
        }
2585
2586
15.3k
        ++l_tile_comp;
2587
15.3k
        ++l_tccp;
2588
15.3k
    }
2589
2590
13.8k
    return OPJ_TRUE;
2591
13.8k
}
2592
2593
static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2594
13.8k
{
2595
13.8k
    const OPJ_FLOAT64 * l_mct_norms;
2596
13.8k
    OPJ_UINT32 l_mct_numcomps = 0U;
2597
13.8k
    opj_tcp_t * l_tcp = p_tcd->tcp;
2598
2599
13.8k
    if (l_tcp->mct == 1) {
2600
654
        l_mct_numcomps = 3U;
2601
        /* irreversible encoding */
2602
654
        if (l_tcp->tccps->qmfbid == 0) {
2603
0
            l_mct_norms = opj_mct_get_mct_norms_real();
2604
654
        } else {
2605
654
            l_mct_norms = opj_mct_get_mct_norms();
2606
654
        }
2607
13.1k
    } else {
2608
13.1k
        l_mct_numcomps = p_tcd->image->numcomps;
2609
13.1k
        l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2610
13.1k
    }
2611
2612
13.8k
    return opj_t1_encode_cblks(p_tcd,
2613
13.8k
                               p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2614
13.8k
                               l_mct_numcomps);
2615
2616
0
    return OPJ_TRUE;
2617
13.8k
}
2618
2619
static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2620
                                  OPJ_BYTE * p_dest_data,
2621
                                  OPJ_UINT32 * p_data_written,
2622
                                  OPJ_UINT32 p_max_dest_size,
2623
                                  opj_codestream_info_t *p_cstr_info,
2624
                                  opj_tcd_marker_info_t* p_marker_info,
2625
                                  opj_event_mgr_t *p_manager)
2626
13.8k
{
2627
13.8k
    opj_t2_t * l_t2;
2628
2629
13.8k
    l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2630
13.8k
    if (l_t2 == 00) {
2631
0
        return OPJ_FALSE;
2632
0
    }
2633
2634
13.8k
    if (! opj_t2_encode_packets(
2635
13.8k
                l_t2,
2636
13.8k
                p_tcd->tcd_tileno,
2637
13.8k
                p_tcd->tcd_image->tiles,
2638
13.8k
                p_tcd->tcp->numlayers,
2639
13.8k
                p_dest_data,
2640
13.8k
                p_data_written,
2641
13.8k
                p_max_dest_size,
2642
13.8k
                p_cstr_info,
2643
13.8k
                p_marker_info,
2644
13.8k
                p_tcd->tp_num,
2645
13.8k
                p_tcd->tp_pos,
2646
13.8k
                p_tcd->cur_pino,
2647
13.8k
                FINAL_PASS,
2648
13.8k
                p_manager)) {
2649
54
        opj_t2_destroy(l_t2);
2650
54
        return OPJ_FALSE;
2651
54
    }
2652
2653
13.7k
    opj_t2_destroy(l_t2);
2654
2655
    /*---------------CLEAN-------------------*/
2656
13.7k
    return OPJ_TRUE;
2657
13.8k
}
2658
2659
2660
static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2661
        OPJ_BYTE * p_dest_data,
2662
        OPJ_UINT32 p_max_dest_size,
2663
        opj_codestream_info_t *p_cstr_info,
2664
        opj_event_mgr_t *p_manager)
2665
13.8k
{
2666
13.8k
    opj_cp_t * l_cp = p_tcd->cp;
2667
13.8k
    OPJ_UINT32 l_nb_written = 0;
2668
2669
13.8k
    if (p_cstr_info)  {
2670
0
        p_cstr_info->index_write = 0;
2671
0
    }
2672
2673
13.8k
    if (l_cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
2674
13.8k
            RATE_DISTORTION_RATIO ||
2675
13.8k
            l_cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
2676
13.8k
            FIXED_DISTORTION_RATIO)  {
2677
13.8k
        if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2678
13.8k
                                   p_cstr_info, p_manager)) {
2679
0
            return OPJ_FALSE;
2680
0
        }
2681
13.8k
    } else {
2682
        /* Fixed layer allocation */
2683
0
        opj_tcd_rateallocate_fixed(p_tcd);
2684
0
    }
2685
2686
13.8k
    return OPJ_TRUE;
2687
13.8k
}
2688
2689
2690
OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2691
                                OPJ_BYTE * p_src,
2692
                                OPJ_SIZE_T p_src_length)
2693
0
{
2694
0
    OPJ_UINT32 i;
2695
0
    OPJ_SIZE_T j;
2696
0
    OPJ_SIZE_T l_data_size = 0;
2697
0
    opj_image_comp_t * l_img_comp = 00;
2698
0
    opj_tcd_tilecomp_t * l_tilec = 00;
2699
0
    OPJ_UINT32 l_size_comp, l_remaining;
2700
0
    OPJ_SIZE_T l_nb_elem;
2701
2702
0
    l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd);
2703
0
    if (l_data_size != p_src_length) {
2704
0
        return OPJ_FALSE;
2705
0
    }
2706
2707
0
    l_tilec = p_tcd->tcd_image->tiles->comps;
2708
0
    l_img_comp = p_tcd->image->comps;
2709
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
2710
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2711
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
2712
0
        l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2713
0
                    (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
2714
2715
0
        if (l_remaining) {
2716
0
            ++l_size_comp;
2717
0
        }
2718
2719
0
        if (l_size_comp == 3) {
2720
0
            l_size_comp = 4;
2721
0
        }
2722
2723
0
        switch (l_size_comp) {
2724
0
        case 1: {
2725
0
            OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2726
0
            OPJ_INT32 * l_dest_ptr = l_tilec->data;
2727
2728
0
            if (l_img_comp->sgnd) {
2729
0
                for (j = 0; j < l_nb_elem; ++j) {
2730
0
                    *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2731
0
                }
2732
0
            } else {
2733
0
                for (j = 0; j < l_nb_elem; ++j) {
2734
0
                    *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2735
0
                }
2736
0
            }
2737
2738
0
            p_src = (OPJ_BYTE*) l_src_ptr;
2739
0
        }
2740
0
        break;
2741
0
        case 2: {
2742
0
            OPJ_INT32 * l_dest_ptr = l_tilec->data;
2743
0
            OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2744
2745
0
            if (l_img_comp->sgnd) {
2746
0
                for (j = 0; j < l_nb_elem; ++j) {
2747
0
                    *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2748
0
                }
2749
0
            } else {
2750
0
                for (j = 0; j < l_nb_elem; ++j) {
2751
0
                    *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2752
0
                }
2753
0
            }
2754
2755
0
            p_src = (OPJ_BYTE*) l_src_ptr;
2756
0
        }
2757
0
        break;
2758
0
        case 4: {
2759
0
            OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2760
0
            OPJ_INT32 * l_dest_ptr = l_tilec->data;
2761
2762
0
            for (j = 0; j < l_nb_elem; ++j) {
2763
0
                *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2764
0
            }
2765
2766
0
            p_src = (OPJ_BYTE*) l_src_ptr;
2767
0
        }
2768
0
        break;
2769
0
        }
2770
2771
0
        ++l_img_comp;
2772
0
        ++l_tilec;
2773
0
    }
2774
2775
0
    return OPJ_TRUE;
2776
0
}
2777
2778
OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2779
57.5M
{
2780
57.5M
    return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2781
57.5M
}
2782
2783
OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2784
        OPJ_UINT32 compno,
2785
        OPJ_UINT32 resno,
2786
        OPJ_UINT32 bandno,
2787
        OPJ_UINT32 band_x0,
2788
        OPJ_UINT32 band_y0,
2789
        OPJ_UINT32 band_x1,
2790
        OPJ_UINT32 band_y1)
2791
226M
{
2792
    /* Note: those values for filter_margin are in part the result of */
2793
    /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2794
    /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2795
    /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2796
    /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2797
    /* needed to be bumped to 4, in case inconsistencies are found while */
2798
    /* decoding parts of irreversible coded images. */
2799
    /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2800
226M
    OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2801
226M
    opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2802
226M
    opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2803
    /* Compute the intersection of the area of interest, expressed in tile coordinates */
2804
    /* with the tile coordinates */
2805
226M
    OPJ_UINT32 tcx0 = opj_uint_max(
2806
226M
                          (OPJ_UINT32)tilec->x0,
2807
226M
                          opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
2808
226M
    OPJ_UINT32 tcy0 = opj_uint_max(
2809
226M
                          (OPJ_UINT32)tilec->y0,
2810
226M
                          opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
2811
226M
    OPJ_UINT32 tcx1 = opj_uint_min(
2812
226M
                          (OPJ_UINT32)tilec->x1,
2813
226M
                          opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
2814
226M
    OPJ_UINT32 tcy1 = opj_uint_min(
2815
226M
                          (OPJ_UINT32)tilec->y1,
2816
226M
                          opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
2817
    /* Compute number of decomposition for this band. See table F-1 */
2818
226M
    OPJ_UINT32 nb = (resno == 0) ?
2819
199M
                    tilec->numresolutions - 1 :
2820
226M
                    tilec->numresolutions - resno;
2821
    /* Map above tile-based coordinates to sub-band-based coordinates per */
2822
    /* equation B-15 of the standard */
2823
226M
    OPJ_UINT32 x0b = bandno & 1;
2824
226M
    OPJ_UINT32 y0b = bandno >> 1;
2825
226M
    OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2826
226M
                      (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2827
27.5M
                      opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2828
226M
    OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2829
226M
                      (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2830
27.5M
                      opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2831
226M
    OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2832
226M
                      (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2833
27.5M
                      opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2834
226M
    OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2835
226M
                      (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2836
27.5M
                      opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2837
226M
    OPJ_BOOL intersects;
2838
2839
226M
    if (tbx0 < filter_margin) {
2840
121M
        tbx0 = 0;
2841
121M
    } else {
2842
105M
        tbx0 -= filter_margin;
2843
105M
    }
2844
226M
    if (tby0 < filter_margin) {
2845
90.6M
        tby0 = 0;
2846
135M
    } else {
2847
135M
        tby0 -= filter_margin;
2848
135M
    }
2849
226M
    tbx1 = opj_uint_adds(tbx1, filter_margin);
2850
226M
    tby1 = opj_uint_adds(tby1, filter_margin);
2851
2852
226M
    intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2853
226M
                 band_y1 > tby0;
2854
2855
#ifdef DEBUG_VERBOSE
2856
    printf("compno=%u resno=%u nb=%u bandno=%u x0b=%u y0b=%u band=%u,%u,%u,%u tb=%u,%u,%u,%u -> %u\n",
2857
           compno, resno, nb, bandno, x0b, y0b,
2858
           band_x0, band_y0, band_x1, band_y1,
2859
           tbx0, tby0, tbx1, tby1, intersects);
2860
#endif
2861
226M
    return intersects;
2862
226M
}
2863
2864
/** Returns whether a tile component is fully decoded, taking into account
2865
 * p_tcd->win_* members.
2866
 *
2867
 * @param p_tcd    TCD handle.
2868
 * @param compno Component number
2869
 * @return OPJ_TRUE whether the tile component is fully decoded
2870
 */
2871
static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
2872
        OPJ_UINT32 compno)
2873
167k
{
2874
167k
    opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
2875
167k
    opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
2876
    /* Compute the intersection of the area of interest, expressed in tile coordinates */
2877
    /* with the tile coordinates */
2878
167k
    OPJ_UINT32 tcx0 = opj_uint_max(
2879
167k
                          (OPJ_UINT32)tilec->x0,
2880
167k
                          opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
2881
167k
    OPJ_UINT32 tcy0 = opj_uint_max(
2882
167k
                          (OPJ_UINT32)tilec->y0,
2883
167k
                          opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
2884
167k
    OPJ_UINT32 tcx1 = opj_uint_min(
2885
167k
                          (OPJ_UINT32)tilec->x1,
2886
167k
                          opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
2887
167k
    OPJ_UINT32 tcy1 = opj_uint_min(
2888
167k
                          (OPJ_UINT32)tilec->y1,
2889
167k
                          opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
2890
2891
167k
    OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
2892
    /* Tolerate small margin within the reduced resolution factor to consider if */
2893
    /* the whole tile path must be taken */
2894
167k
    return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
2895
167k
            tcy0 >= (OPJ_UINT32)tilec->y0 &&
2896
167k
            tcx1 <= (OPJ_UINT32)tilec->x1 &&
2897
167k
            tcy1 <= (OPJ_UINT32)tilec->y1 &&
2898
167k
            (shift >= 32 ||
2899
167k
             (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
2900
167k
              ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
2901
167k
              (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
2902
167k
              (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));
2903
167k
}
2904
2905
/* ----------------------------------------------------------------------- */
2906
2907
opj_tcd_marker_info_t* opj_tcd_marker_info_create(OPJ_BOOL need_PLT)
2908
0
{
2909
0
    opj_tcd_marker_info_t *l_tcd_marker_info =
2910
0
        (opj_tcd_marker_info_t*) opj_calloc(1, sizeof(opj_tcd_marker_info_t));
2911
0
    if (!l_tcd_marker_info) {
2912
0
        return NULL;
2913
0
    }
2914
2915
0
    l_tcd_marker_info->need_PLT = need_PLT;
2916
2917
0
    return l_tcd_marker_info;
2918
0
}
2919
2920
/* ----------------------------------------------------------------------- */
2921
2922
void opj_tcd_marker_info_destroy(opj_tcd_marker_info_t *p_tcd_marker_info)
2923
13.8k
{
2924
13.8k
    if (p_tcd_marker_info) {
2925
0
        opj_free(p_tcd_marker_info->p_packet_size);
2926
0
        opj_free(p_tcd_marker_info);
2927
0
    }
2928
13.8k
}
2929
2930
/* ----------------------------------------------------------------------- */