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.
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 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
42 #include "opj_common.h"
44 /* ----------------------------------------------------------------------- */
48 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
50 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
52 fprintf(fd, "image {\n");
53 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
54 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
57 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
58 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
59 fprintf(fd, " tile {\n");
60 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
61 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
62 for (compno = 0; compno < tile->numcomps; compno++) {
63 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
64 fprintf(fd, " tilec {\n");
66 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
67 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
68 for (resno = 0; resno < tilec->numresolutions; resno++) {
69 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
70 fprintf(fd, "\n res {\n");
72 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
73 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
74 for (bandno = 0; bandno < res->numbands; bandno++) {
75 opj_tcd_band_t *band = &res->bands[bandno];
76 fprintf(fd, " band {\n");
78 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
79 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
80 for (precno = 0; precno < res->pw * res->ph; precno++) {
81 opj_tcd_precinct_t *prec = &band->precincts[precno];
82 fprintf(fd, " prec {\n");
84 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
85 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
87 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
88 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
89 fprintf(fd, " cblk {\n");
91 " x0=%d, y0=%d, x1=%d, y1=%d\n",
92 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
111 * Initializes tile coding/decoding
113 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
114 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
115 opj_event_mgr_t* manager);
118 * Allocates memory for a decoding code block.
120 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
124 * Deallocates the decoding data of the given precinct.
126 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
129 * Allocates memory for an encoding code block (but not data).
131 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
135 * Allocates data for an encoding code block
137 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
141 * Deallocates the encoding data of the given precinct.
143 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
147 Free the memory allocated for encoding
148 @param tcd TCD handle
150 static void opj_tcd_free_tile(opj_tcd_t *tcd);
153 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
154 OPJ_BYTE * p_src_data,
155 OPJ_UINT32 * p_data_read,
156 OPJ_UINT32 p_max_src_size,
157 opj_codestream_index_t *p_cstr_index,
158 opj_event_mgr_t *p_manager);
160 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd);
162 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
164 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
165 opj_event_mgr_t *p_manager);
167 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
170 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
172 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
174 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
176 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
178 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
179 OPJ_BYTE * p_dest_data,
180 OPJ_UINT32 * p_data_written,
181 OPJ_UINT32 p_max_dest_size,
182 opj_codestream_info_t *p_cstr_info);
184 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
185 OPJ_BYTE * p_dest_data,
186 OPJ_UINT32 p_max_dest_size,
187 opj_codestream_info_t *p_cstr_info);
189 /* ----------------------------------------------------------------------- */
192 Create a new TCD handle
194 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
196 opj_tcd_t *l_tcd = 00;
198 /* create the tcd structure */
199 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
204 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
206 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
207 if (!l_tcd->tcd_image) {
216 /* ----------------------------------------------------------------------- */
218 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
222 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
223 opj_tcd_makelayer_fixed(tcd, layno, 1);
228 void opj_tcd_makelayer(opj_tcd_t *tcd,
233 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
236 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
238 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
240 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
241 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
243 for (resno = 0; resno < tilec->numresolutions; resno++) {
244 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
246 for (bandno = 0; bandno < res->numbands; bandno++) {
247 opj_tcd_band_t *band = &res->bands[bandno];
249 for (precno = 0; precno < res->pw * res->ph; precno++) {
250 opj_tcd_precinct_t *prc = &band->precincts[precno];
252 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
253 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
254 opj_tcd_layer_t *layer = &cblk->layers[layno];
258 cblk->numpassesinlayers = 0;
261 n = cblk->numpassesinlayers;
263 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
266 opj_tcd_pass_t *pass = &cblk->passes[passno];
270 dd = pass->distortiondec;
272 dr = pass->rate - cblk->passes[n - 1].rate;
273 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
282 if (thresh - (dd / dr) <
283 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
288 layer->numpasses = n - cblk->numpassesinlayers;
290 if (!layer->numpasses) {
295 if (cblk->numpassesinlayers == 0) {
296 layer->len = cblk->passes[n - 1].rate;
297 layer->data = cblk->data;
298 layer->disto = cblk->passes[n - 1].distortiondec;
300 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
302 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
303 layer->disto = cblk->passes[n - 1].distortiondec -
304 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
307 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
310 cblk->numpassesinlayers = n;
319 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
322 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
323 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
324 OPJ_INT32 matrice[10][10][3];
327 opj_cp_t *cp = tcd->cp;
328 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
329 opj_tcp_t *tcd_tcp = tcd->tcp;
331 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
332 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
334 for (i = 0; i < tcd_tcp->numlayers; i++) {
335 for (j = 0; j < tilec->numresolutions; j++) {
336 for (k = 0; k < 3; k++) {
338 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
339 tilec->numresolutions * 3 + j * 3 + k]
340 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
345 for (resno = 0; resno < tilec->numresolutions; resno++) {
346 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
348 for (bandno = 0; bandno < res->numbands; bandno++) {
349 opj_tcd_band_t *band = &res->bands[bandno];
351 for (precno = 0; precno < res->pw * res->ph; precno++) {
352 opj_tcd_precinct_t *prc = &band->precincts[precno];
354 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
355 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
356 opj_tcd_layer_t *layer = &cblk->layers[layno];
358 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
359 cblk->numbps); /* number of bit-plan equal to zero */
361 /* Correction of the matrix of coefficient to include the IMSB information */
363 value = matrice[layno][resno][bandno];
370 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
371 if (imsb >= matrice[layno - 1][resno][bandno]) {
372 value -= (imsb - matrice[layno - 1][resno][bandno]);
380 cblk->numpassesinlayers = 0;
383 n = cblk->numpassesinlayers;
384 if (cblk->numpassesinlayers == 0) {
386 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
388 n = cblk->numpassesinlayers;
391 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
394 layer->numpasses = n - cblk->numpassesinlayers;
396 if (!layer->numpasses) {
400 if (cblk->numpassesinlayers == 0) {
401 layer->len = cblk->passes[n - 1].rate;
402 layer->data = cblk->data;
404 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
406 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
410 cblk->numpassesinlayers = n;
419 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
421 OPJ_UINT32 * p_data_written,
423 opj_codestream_info_t *cstr_info)
425 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
427 OPJ_FLOAT64 min, max;
428 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
429 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
430 OPJ_FLOAT64 maxSE = 0;
432 opj_cp_t *cp = tcd->cp;
433 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
434 opj_tcp_t *tcd_tcp = tcd->tcp;
439 tcd_tile->numpix = 0; /* fixed_quality */
441 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
442 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
445 for (resno = 0; resno < tilec->numresolutions; resno++) {
446 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
448 for (bandno = 0; bandno < res->numbands; bandno++) {
449 opj_tcd_band_t *band = &res->bands[bandno];
451 for (precno = 0; precno < res->pw * res->ph; precno++) {
452 opj_tcd_precinct_t *prc = &band->precincts[precno];
454 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
455 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
457 for (passno = 0; passno < cblk->totalpasses; passno++) {
458 opj_tcd_pass_t *pass = &cblk->passes[passno];
460 OPJ_FLOAT64 dd, rdslope;
463 dr = (OPJ_INT32)pass->rate;
464 dd = pass->distortiondec;
466 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
467 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
485 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
486 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
492 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
493 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
494 * ((OPJ_FLOAT64)(tilec->numpix));
499 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
500 tile_info->numpix = tcd_tile->numpix;
501 tile_info->distotile = tcd_tile->distotile;
502 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
504 if (!tile_info->thresh) {
505 /* FIXME event manager error callback */
510 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
511 OPJ_FLOAT64 lo = min;
512 OPJ_FLOAT64 hi = max;
513 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
514 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
515 OPJ_FLOAT64 goodthresh = 0;
516 OPJ_FLOAT64 stable_thresh = 0;
518 OPJ_FLOAT64 distotarget; /* fixed_quality */
521 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
522 tcd_tcp->distoratio[layno] / 10));
524 /* Don't try to find an optimal threshold but rather take everything not included yet, if
525 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
526 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
527 ==> possible to have some lossy layers and the last layer for sure lossless */
528 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
529 (tcd_tcp->rates[layno] > 0.0f)) ||
530 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
531 (tcd_tcp->distoratio[layno] > 0.0))) {
532 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
533 OPJ_FLOAT64 thresh = 0;
539 for (i = 0; i < 128; ++i) {
540 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
542 thresh = (lo + hi) / 2;
544 opj_tcd_makelayer(tcd, layno, thresh, 0);
546 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
547 if (OPJ_IS_CINEMA(cp->rsiz)) {
548 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
549 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
555 distoachieved = layno == 0 ?
556 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
558 if (distoachieved < distotarget) {
560 stable_thresh = thresh;
567 distoachieved = (layno == 0) ?
568 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
570 if (distoachieved < distotarget) {
572 stable_thresh = thresh;
578 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
579 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
581 /* TODO: what to do with l ??? seek / tell ??? */
582 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
588 stable_thresh = thresh;
592 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
599 if (cstr_info) { /* Threshold for Marcela Index */
600 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
603 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
606 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
607 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
613 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
614 opj_image_t * p_image,
616 opj_thread_pool_t* p_tp)
618 p_tcd->image = p_image;
621 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
622 sizeof(opj_tcd_tile_t));
623 if (! p_tcd->tcd_image->tiles) {
627 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
628 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
629 if (! p_tcd->tcd_image->tiles->comps) {
633 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
634 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
635 p_tcd->thread_pool = p_tp;
641 Destroy a previously created TCD handle
643 void opj_tcd_destroy(opj_tcd_t *tcd)
646 opj_tcd_free_tile(tcd);
648 if (tcd->tcd_image) {
649 opj_free(tcd->tcd_image);
656 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
658 if ((l_tilec->data == 00) ||
659 ((l_tilec->data_size_needed > l_tilec->data_size) &&
660 (l_tilec->ownsData == OPJ_FALSE))) {
661 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
662 if (! l_tilec->data) {
665 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
666 l_tilec->data_size = l_tilec->data_size_needed;
667 l_tilec->ownsData = OPJ_TRUE;
668 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
669 /* We don't need to keep old data */
670 opj_aligned_free(l_tilec->data);
671 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
672 if (! l_tilec->data) {
673 l_tilec->data_size = 0;
674 l_tilec->data_size_needed = 0;
675 l_tilec->ownsData = OPJ_FALSE;
678 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
679 l_tilec->data_size = l_tilec->data_size_needed;
680 l_tilec->ownsData = OPJ_TRUE;
685 /* ----------------------------------------------------------------------- */
687 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
688 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
689 opj_event_mgr_t* manager)
691 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
692 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
693 opj_tcp_t * l_tcp = 00;
694 opj_cp_t * l_cp = 00;
695 opj_tcd_tile_t * l_tile = 00;
696 opj_tccp_t *l_tccp = 00;
697 opj_tcd_tilecomp_t *l_tilec = 00;
698 opj_image_comp_t * l_image_comp = 00;
699 opj_tcd_resolution_t *l_res = 00;
700 opj_tcd_band_t *l_band = 00;
701 opj_stepsize_t * l_step_size = 00;
702 opj_tcd_precinct_t *l_current_precinct = 00;
703 opj_image_t *l_image = 00;
705 OPJ_UINT32 l_level_no;
706 OPJ_UINT32 l_pdx, l_pdy;
708 OPJ_INT32 l_x0b, l_y0b;
709 OPJ_UINT32 l_tx0, l_ty0;
710 /* extent of precincts , top left, bottom right**/
711 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
712 /* number of precinct for a resolution */
713 OPJ_UINT32 l_nb_precincts;
714 /* room needed to store l_nb_precinct precinct for a resolution */
715 OPJ_UINT32 l_nb_precinct_size;
716 /* number of code blocks for a precinct*/
717 OPJ_UINT32 l_nb_code_blocks;
718 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
719 OPJ_UINT32 l_nb_code_blocks_size;
720 /* size of data for a tile */
721 OPJ_UINT32 l_data_size;
724 l_tcp = &(l_cp->tcps[p_tile_no]);
725 l_tile = p_tcd->tcd_image->tiles;
726 l_tccp = l_tcp->tccps;
727 l_tilec = l_tile->comps;
728 l_image = p_tcd->image;
729 l_image_comp = p_tcd->image->comps;
731 p = p_tile_no % l_cp->tw; /* tile coordinates */
732 q = p_tile_no / l_cp->tw;
733 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
735 /* 4 borders of the tile rescale on the image if necessary */
736 l_tx0 = l_cp->tx0 + p *
737 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
738 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
739 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
741 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
742 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
743 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
746 l_ty0 = l_cp->ty0 + q *
747 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
748 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
749 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
751 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
752 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
753 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
758 /* testcase 1888.pdf.asan.35.988 */
759 if (l_tccp->numresolutions == 0) {
760 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
763 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
765 /*tile->numcomps = image->numcomps; */
766 for (compno = 0; compno < l_tile->numcomps; ++compno) {
767 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
768 l_image_comp->resno_decoded = 0;
769 /* border of each l_tile component (global) */
770 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
771 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
772 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
773 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
774 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
776 /* compute l_data_size with overflow check */
777 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
778 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
779 if ((l_data_size > 0U) &&
780 ((((OPJ_UINT32) - 1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 -
782 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
785 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
787 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
788 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
791 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
792 l_tilec->numresolutions = l_tccp->numresolutions;
793 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
794 l_tilec->minimum_num_resolutions = 1;
796 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
797 l_cp->m_specific_param.m_dec.m_reduce;
800 l_tilec->data_size_needed = l_data_size;
801 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
802 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
806 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
807 opj_tcd_resolution_t);
809 if (l_tilec->resolutions == 00) {
810 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
811 if (! l_tilec->resolutions) {
814 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
815 l_tilec->resolutions_size = l_data_size;
816 memset(l_tilec->resolutions, 0, l_data_size);
817 } else if (l_data_size > l_tilec->resolutions_size) {
818 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
819 l_tilec->resolutions, l_data_size);
820 if (! new_resolutions) {
821 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
822 opj_free(l_tilec->resolutions);
823 l_tilec->resolutions = NULL;
824 l_tilec->resolutions_size = 0;
827 l_tilec->resolutions = new_resolutions;
828 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
829 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
830 l_data_size - l_tilec->resolutions_size);
831 l_tilec->resolutions_size = l_data_size;
834 l_level_no = l_tilec->numresolutions;
835 l_res = l_tilec->resolutions;
836 l_step_size = l_tccp->stepsizes;
837 if (l_tccp->qmfbid == 0) {
838 l_gain_ptr = &opj_dwt_getgain_real;
840 l_gain_ptr = &opj_dwt_getgain;
842 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
844 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
845 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
846 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
847 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
848 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
852 /* border for each resolution level (global) */
853 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
854 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
855 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
856 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
857 /*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);*/
858 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
859 l_pdx = l_tccp->prcw[resno];
860 l_pdy = l_tccp->prch[resno];
861 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
862 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
863 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
864 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
865 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
866 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
867 /*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 );*/
869 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
870 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
871 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
872 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
873 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
875 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
876 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
879 l_nb_precincts = l_res->pw * l_res->ph;
881 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
883 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
886 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
889 tlcbgxstart = l_tl_prc_x_start;
890 tlcbgystart = l_tl_prc_y_start;
891 /*brcbgxend = l_br_prc_x_end;*/
892 /* brcbgyend = l_br_prc_y_end;*/
893 cbgwidthexpn = l_pdx;
894 cbgheightexpn = l_pdy;
897 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
898 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
899 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
900 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
901 cbgwidthexpn = l_pdx - 1;
902 cbgheightexpn = l_pdy - 1;
906 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
907 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
908 l_band = l_res->bands;
910 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
912 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
916 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
917 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
918 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
919 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
921 l_band->bandno = bandno + 1;
922 /* x0b = 1 if bandno = 1 or 3 */
923 l_x0b = l_band->bandno & 1;
924 /* y0b = 1 if bandno = 2 or 3 */
925 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
926 /* l_band border (global) */
927 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
928 l_level_no), (OPJ_INT32)(l_level_no + 1));
929 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
930 l_level_no), (OPJ_INT32)(l_level_no + 1));
931 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
932 l_level_no), (OPJ_INT32)(l_level_no + 1));
933 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
934 l_level_no), (OPJ_INT32)(l_level_no + 1));
937 /** avoid an if with storing function pointer */
938 l_gain = (*l_gain_ptr)(l_band->bandno);
939 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
940 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
941 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
942 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
945 if (!l_band->precincts && (l_nb_precincts > 0U)) {
946 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
948 if (! l_band->precincts) {
949 opj_event_msg(manager, EVT_ERROR,
950 "Not enough memory to handle band precints\n");
953 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
954 memset(l_band->precincts, 0, l_nb_precinct_size);
955 l_band->precincts_data_size = l_nb_precinct_size;
956 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
958 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
959 l_band->precincts,/*3 * */ l_nb_precinct_size);
960 if (! new_precincts) {
961 opj_event_msg(manager, EVT_ERROR,
962 "Not enough memory to handle band precints\n");
963 opj_free(l_band->precincts);
964 l_band->precincts = NULL;
965 l_band->precincts_data_size = 0;
968 l_band->precincts = new_precincts;
969 /*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);*/
970 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
971 l_nb_precinct_size - l_band->precincts_data_size);
972 l_band->precincts_data_size = l_nb_precinct_size;
975 l_current_precinct = l_band->precincts;
976 for (precno = 0; precno < l_nb_precincts; ++precno) {
977 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
978 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
980 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
981 (1 << cbgheightexpn);
982 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
983 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
984 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
985 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
987 /* precinct size (global) */
988 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
990 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
991 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
992 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
993 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
994 /*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);*/
996 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
997 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
998 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
999 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1000 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1001 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1002 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1003 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1004 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1005 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1006 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1007 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1008 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1010 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1013 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1014 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1015 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1017 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1018 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1019 if (! l_current_precinct->cblks.blocks) {
1022 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1024 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1026 l_current_precinct->block_size = l_nb_code_blocks_size;
1027 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1028 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1029 l_nb_code_blocks_size);
1031 opj_free(l_current_precinct->cblks.blocks);
1032 l_current_precinct->cblks.blocks = NULL;
1033 l_current_precinct->block_size = 0;
1034 opj_event_msg(manager, EVT_ERROR,
1035 "Not enough memory for current precinct codeblock element\n");
1038 l_current_precinct->cblks.blocks = new_blocks;
1039 /*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); */
1041 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1042 l_current_precinct->block_size
1044 , l_nb_code_blocks_size - l_current_precinct->block_size);
1046 l_current_precinct->block_size = l_nb_code_blocks_size;
1049 if (! l_current_precinct->incltree) {
1050 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1051 l_current_precinct->ch, manager);
1053 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1054 l_current_precinct->cw, l_current_precinct->ch, manager);
1057 if (! l_current_precinct->imsbtree) {
1058 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1059 l_current_precinct->ch, manager);
1061 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1062 l_current_precinct->cw, l_current_precinct->ch, manager);
1065 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1066 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1067 l_current_precinct->cw) * (1 << cblkwidthexpn);
1068 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1069 l_current_precinct->cw) * (1 << cblkheightexpn);
1070 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1071 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1074 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1076 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1079 /* code-block size (global) */
1080 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1081 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1082 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1083 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1085 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1089 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1091 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1094 /* code-block size (global) */
1095 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1096 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1097 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1098 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1101 ++l_current_precinct;
1115 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1116 opj_event_mgr_t* p_manager)
1118 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1119 sizeof(opj_tcd_cblk_enc_t), p_manager);
1122 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1123 opj_event_mgr_t* p_manager)
1125 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1126 sizeof(opj_tcd_cblk_dec_t), p_manager);
1130 * Allocates memory for an encoding code block (but not data memory).
1132 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1135 if (! p_code_block->layers) {
1136 /* no memset since data */
1137 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1138 sizeof(opj_tcd_layer_t));
1139 if (! p_code_block->layers) {
1143 if (! p_code_block->passes) {
1144 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1145 sizeof(opj_tcd_pass_t));
1146 if (! p_code_block->passes) {
1154 * Allocates data memory for an encoding code block.
1156 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1159 OPJ_UINT32 l_data_size;
1161 l_data_size = (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1162 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1164 if (l_data_size > p_code_block->data_size) {
1165 if (p_code_block->data) {
1166 opj_free(p_code_block->data - 1); /* again, why -1 */
1168 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1169 if (! p_code_block->data) {
1170 p_code_block->data_size = 0U;
1173 p_code_block->data_size = l_data_size;
1175 p_code_block->data[0] = 0;
1176 p_code_block->data += 1; /*why +1 ?*/
1182 * Allocates memory for a decoding code block.
1184 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1187 if (! p_code_block->data) {
1189 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_COMMON_DEFAULT_CBLK_DATA_SIZE);
1190 if (! p_code_block->data) {
1193 p_code_block->data_max_size = OPJ_COMMON_DEFAULT_CBLK_DATA_SIZE;
1194 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1196 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1197 sizeof(opj_tcd_seg_t));
1198 if (! p_code_block->segs) {
1201 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1203 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1204 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1207 OPJ_BYTE* l_data = p_code_block->data;
1208 OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1209 opj_tcd_seg_t * l_segs = p_code_block->segs;
1210 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1212 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1213 p_code_block->data = l_data;
1214 p_code_block->data_max_size = l_data_max_size;
1215 p_code_block->segs = l_segs;
1216 p_code_block->m_current_max_segs = l_current_max_segs;
1222 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1225 OPJ_UINT32 l_data_size = 0;
1226 opj_image_comp_t * l_img_comp = 00;
1227 opj_tcd_tilecomp_t * l_tile_comp = 00;
1228 opj_tcd_resolution_t * l_res = 00;
1229 OPJ_UINT32 l_size_comp, l_remaining;
1231 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1232 l_img_comp = p_tcd->image->comps;
1234 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1235 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1236 l_remaining = l_img_comp->prec & 7; /* (%8) */
1242 if (l_size_comp == 3) {
1246 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1247 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) *
1248 (l_res->y1 - l_res->y0));
1256 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1257 OPJ_UINT32 p_tile_no,
1259 OPJ_UINT32 * p_data_written,
1260 OPJ_UINT32 p_max_length,
1261 opj_codestream_info_t *p_cstr_info)
1264 if (p_tcd->cur_tp_num == 0) {
1266 p_tcd->tcd_tileno = p_tile_no;
1267 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1269 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1271 OPJ_UINT32 l_num_packs = 0;
1273 opj_tcd_tilecomp_t *l_tilec_idx =
1274 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1275 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1277 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1278 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1280 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1281 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1283 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1284 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1285 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1287 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1288 size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1289 sizeof(opj_packet_info_t));
1290 if (!p_cstr_info->tile[p_tile_no].packet) {
1291 /* FIXME event manager error callback */
1297 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1298 /*---------------TILE-------------------*/
1299 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1302 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1304 /* FIXME _ProfStart(PGROUP_MCT); */
1305 if (! opj_tcd_mct_encode(p_tcd)) {
1308 /* FIXME _ProfStop(PGROUP_MCT); */
1310 /* FIXME _ProfStart(PGROUP_DWT); */
1311 if (! opj_tcd_dwt_encode(p_tcd)) {
1314 /* FIXME _ProfStop(PGROUP_DWT); */
1316 /* FIXME _ProfStart(PGROUP_T1); */
1317 if (! opj_tcd_t1_encode(p_tcd)) {
1320 /* FIXME _ProfStop(PGROUP_T1); */
1322 /* FIXME _ProfStart(PGROUP_RATE); */
1323 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info)) {
1326 /* FIXME _ProfStop(PGROUP_RATE); */
1329 /*--------------TIER2------------------*/
1333 p_cstr_info->index_write = 1;
1335 /* FIXME _ProfStart(PGROUP_T2); */
1337 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1341 /* FIXME _ProfStop(PGROUP_T2); */
1343 /*---------------CLEAN-------------------*/
1348 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1350 OPJ_UINT32 p_max_length,
1351 OPJ_UINT32 p_tile_no,
1352 opj_codestream_index_t *p_cstr_index,
1353 opj_event_mgr_t *p_manager
1356 OPJ_UINT32 l_data_read;
1357 p_tcd->tcd_tileno = p_tile_no;
1358 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1360 #ifdef TODO_MSD /* FIXME */
1363 OPJ_UINT32 resno, compno, numprec = 0;
1364 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1365 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1366 opj_tccp_t *tccp = &tcp->tccps[compno];
1367 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1368 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1369 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1370 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1371 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1372 numprec += res_idx->pw * res_idx->ph;
1373 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1374 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1377 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1378 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1379 p_cstr_info->packno = 0;
1384 /*--------------TIER2------------------*/
1385 /* FIXME _ProfStart(PGROUP_T2); */
1387 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1391 /* FIXME _ProfStop(PGROUP_T2); */
1393 /*------------------TIER1-----------------*/
1395 /* FIXME _ProfStart(PGROUP_T1); */
1397 (! opj_tcd_t1_decode(p_tcd)) {
1400 /* FIXME _ProfStop(PGROUP_T1); */
1402 /*----------------DWT---------------------*/
1404 /* FIXME _ProfStart(PGROUP_DWT); */
1406 (! opj_tcd_dwt_decode(p_tcd)) {
1409 /* FIXME _ProfStop(PGROUP_DWT); */
1411 /*----------------MCT-------------------*/
1412 /* FIXME _ProfStart(PGROUP_MCT); */
1414 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1417 /* FIXME _ProfStop(PGROUP_MCT); */
1419 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1421 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1424 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1427 /*---------------TILE-------------------*/
1431 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1433 OPJ_UINT32 p_dest_length
1436 OPJ_UINT32 i, j, k, l_data_size = 0;
1437 opj_image_comp_t * l_img_comp = 00;
1438 opj_tcd_tilecomp_t * l_tilec = 00;
1439 opj_tcd_resolution_t * l_res;
1440 OPJ_UINT32 l_size_comp, l_remaining;
1441 OPJ_UINT32 l_stride, l_width, l_height;
1443 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1444 if (l_data_size > p_dest_length) {
1448 l_tilec = p_tcd->tcd_image->tiles->comps;
1449 l_img_comp = p_tcd->image->comps;
1451 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1452 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1453 l_remaining = l_img_comp->prec & 7; /* (%8) */
1454 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1455 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1456 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1457 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1463 if (l_size_comp == 3) {
1467 switch (l_size_comp) {
1469 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1470 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1472 if (l_img_comp->sgnd) {
1473 for (j = 0; j < l_height; ++j) {
1474 for (k = 0; k < l_width; ++k) {
1475 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1477 l_src_ptr += l_stride;
1480 for (j = 0; j < l_height; ++j) {
1481 for (k = 0; k < l_width; ++k) {
1482 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1484 l_src_ptr += l_stride;
1488 p_dest = (OPJ_BYTE *)l_dest_ptr;
1492 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1493 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1495 if (l_img_comp->sgnd) {
1496 for (j = 0; j < l_height; ++j) {
1497 for (k = 0; k < l_width; ++k) {
1498 *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
1500 l_src_ptr += l_stride;
1503 for (j = 0; j < l_height; ++j) {
1504 for (k = 0; k < l_width; ++k) {
1505 *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1507 l_src_ptr += l_stride;
1511 p_dest = (OPJ_BYTE*) l_dest_ptr;
1515 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1516 OPJ_INT32 * l_src_ptr = l_tilec->data;
1518 for (j = 0; j < l_height; ++j) {
1519 for (k = 0; k < l_width; ++k) {
1520 *(l_dest_ptr++) = (*(l_src_ptr++));
1522 l_src_ptr += l_stride;
1525 p_dest = (OPJ_BYTE*) l_dest_ptr;
1540 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1542 OPJ_UINT32 compno, resno, bandno, precno;
1543 opj_tcd_tile_t *l_tile = 00;
1544 opj_tcd_tilecomp_t *l_tile_comp = 00;
1545 opj_tcd_resolution_t *l_res = 00;
1546 opj_tcd_band_t *l_band = 00;
1547 opj_tcd_precinct_t *l_precinct = 00;
1548 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1549 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1555 if (! p_tcd->tcd_image) {
1559 if (p_tcd->m_is_decoder) {
1560 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1562 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1565 l_tile = p_tcd->tcd_image->tiles;
1570 l_tile_comp = l_tile->comps;
1572 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1573 l_res = l_tile_comp->resolutions;
1576 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1577 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1578 l_band = l_res->bands;
1579 for (bandno = 0; bandno < 3; ++bandno) {
1580 l_precinct = l_band->precincts;
1583 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1584 for (precno = 0; precno < l_nb_precincts; ++precno) {
1585 opj_tgt_destroy(l_precinct->incltree);
1586 l_precinct->incltree = 00;
1587 opj_tgt_destroy(l_precinct->imsbtree);
1588 l_precinct->imsbtree = 00;
1589 (*l_tcd_code_block_deallocate)(l_precinct);
1593 opj_free(l_band->precincts);
1594 l_band->precincts = 00;
1601 opj_free(l_tile_comp->resolutions);
1602 l_tile_comp->resolutions = 00;
1605 if (l_tile_comp->ownsData && l_tile_comp->data) {
1606 opj_aligned_free(l_tile_comp->data);
1607 l_tile_comp->data = 00;
1608 l_tile_comp->ownsData = 0;
1609 l_tile_comp->data_size = 0;
1610 l_tile_comp->data_size_needed = 0;
1615 opj_free(l_tile->comps);
1617 opj_free(p_tcd->tcd_image->tiles);
1618 p_tcd->tcd_image->tiles = 00;
1622 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1623 OPJ_BYTE * p_src_data,
1624 OPJ_UINT32 * p_data_read,
1625 OPJ_UINT32 p_max_src_size,
1626 opj_codestream_index_t *p_cstr_index,
1627 opj_event_mgr_t *p_manager
1632 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1637 if (! opj_t2_decode_packets(
1640 p_tcd->tcd_image->tiles,
1646 opj_t2_destroy(l_t2);
1650 opj_t2_destroy(l_t2);
1652 /*---------------CLEAN-------------------*/
1656 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd)
1659 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1660 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1661 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1662 volatile OPJ_BOOL ret = OPJ_TRUE;
1664 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1665 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp);
1673 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1679 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1682 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1683 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1684 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1685 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1687 for (compno = 0; compno < l_tile->numcomps; compno++) {
1689 if (tcd->cp->reduce != 0) {
1690 tcd->image->comps[compno].resno_decoded =
1691 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1692 if (tcd->image->comps[compno].resno_decoded < 0)
1697 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1698 if(numres2decode > 0){
1701 if (l_tccp->qmfbid == 1) {
1702 if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp,
1703 l_img_comp->resno_decoded + 1)) {
1707 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded + 1)) {
1719 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1721 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1722 opj_tcp_t * l_tcp = p_tcd->tcp;
1723 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1724 OPJ_UINT32 l_samples, i;
1730 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1731 (l_tile_comp->y1 - l_tile_comp->y0));
1733 if (l_tile->numcomps >= 3) {
1734 /* testcase 1336.pdf.asan.47.376 */
1735 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1736 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1737 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1738 l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1739 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1740 l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1741 opj_event_msg(p_manager, EVT_ERROR,
1742 "Tiles don't all have the same dimension. Skip the MCT step.\n");
1744 } else if (l_tcp->mct == 2) {
1747 if (! l_tcp->m_mct_decoding_matrix) {
1751 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1756 for (i = 0; i < l_tile->numcomps; ++i) {
1757 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1761 if (! opj_mct_decode_custom(/* MCT data */
1762 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1763 /* size of components */
1767 /* nb of components (i.e. size of pData) */
1769 /* tells if the data is signed */
1770 p_tcd->image->comps->sgnd)) {
1777 if (l_tcp->tccps->qmfbid == 1) {
1778 opj_mct_decode(l_tile->comps[0].data,
1779 l_tile->comps[1].data,
1780 l_tile->comps[2].data,
1783 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1784 (OPJ_FLOAT32*)l_tile->comps[1].data,
1785 (OPJ_FLOAT32*)l_tile->comps[2].data,
1790 opj_event_msg(p_manager, EVT_ERROR,
1791 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1799 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1802 opj_tcd_tilecomp_t * l_tile_comp = 00;
1803 opj_tccp_t * l_tccp = 00;
1804 opj_image_comp_t * l_img_comp = 00;
1805 opj_tcd_resolution_t* l_res = 00;
1806 opj_tcd_tile_t * l_tile;
1807 OPJ_UINT32 l_width, l_height, i, j;
1808 OPJ_INT32 * l_current_ptr;
1809 OPJ_INT32 l_min, l_max;
1810 OPJ_UINT32 l_stride;
1812 l_tile = p_tcd->tcd_image->tiles;
1813 l_tile_comp = l_tile->comps;
1814 l_tccp = p_tcd->tcp->tccps;
1815 l_img_comp = p_tcd->image->comps;
1817 for (compno = 0; compno < l_tile->numcomps; compno++) {
1818 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1819 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1820 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1821 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1823 assert(l_height == 0 ||
1824 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1826 if (l_img_comp->sgnd) {
1827 l_min = -(1 << (l_img_comp->prec - 1));
1828 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1831 l_max = (1 << l_img_comp->prec) - 1;
1834 l_current_ptr = l_tile_comp->data;
1836 if (l_tccp->qmfbid == 1) {
1837 for (j = 0; j < l_height; ++j) {
1838 for (i = 0; i < l_width; ++i) {
1839 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1843 l_current_ptr += l_stride;
1846 for (j = 0; j < l_height; ++j) {
1847 for (i = 0; i < l_width; ++i) {
1848 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1849 *l_current_ptr = opj_int_clamp((OPJ_INT32)opj_lrintf(l_value) +
1850 l_tccp->m_dc_level_shift, l_min, l_max); ;
1853 l_current_ptr += l_stride;
1868 * Deallocates the encoding data of the given precinct.
1870 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1872 OPJ_UINT32 cblkno, l_nb_code_blocks;
1874 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1876 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1877 /*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);*/
1878 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1879 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 );*/
1882 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1883 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1885 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1887 if (l_code_block->data) {
1888 opj_free(l_code_block->data);
1889 l_code_block->data = 00;
1892 if (l_code_block->segs) {
1893 opj_free(l_code_block->segs);
1894 l_code_block->segs = 00;
1900 opj_free(p_precinct->cblks.dec);
1901 p_precinct->cblks.dec = 00;
1906 * Deallocates the encoding data of the given precinct.
1908 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
1910 OPJ_UINT32 cblkno, l_nb_code_blocks;
1912 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1914 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1916 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1917 if (l_code_block->data) {
1918 opj_free(l_code_block->data - 1);
1919 l_code_block->data = 00;
1922 if (l_code_block->layers) {
1923 opj_free(l_code_block->layers);
1924 l_code_block->layers = 00;
1927 if (l_code_block->passes) {
1928 opj_free(l_code_block->passes);
1929 l_code_block->passes = 00;
1934 opj_free(p_precinct->cblks.enc);
1936 p_precinct->cblks.enc = 00;
1940 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
1942 OPJ_UINT32 i, l_data_size = 0;
1943 opj_image_comp_t * l_img_comp = 00;
1944 opj_tcd_tilecomp_t * l_tilec = 00;
1945 OPJ_UINT32 l_size_comp, l_remaining;
1947 l_tilec = p_tcd->tcd_image->tiles->comps;
1948 l_img_comp = p_tcd->image->comps;
1949 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1950 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1951 l_remaining = l_img_comp->prec & 7; /* (%8) */
1957 if (l_size_comp == 3) {
1961 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
1962 (l_tilec->y1 - l_tilec->y0));
1970 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
1973 opj_tcd_tilecomp_t * l_tile_comp = 00;
1974 opj_tccp_t * l_tccp = 00;
1975 opj_image_comp_t * l_img_comp = 00;
1976 opj_tcd_tile_t * l_tile;
1977 OPJ_UINT32 l_nb_elem, i;
1978 OPJ_INT32 * l_current_ptr;
1980 l_tile = p_tcd->tcd_image->tiles;
1981 l_tile_comp = l_tile->comps;
1982 l_tccp = p_tcd->tcp->tccps;
1983 l_img_comp = p_tcd->image->comps;
1985 for (compno = 0; compno < l_tile->numcomps; compno++) {
1986 l_current_ptr = l_tile_comp->data;
1987 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1988 (l_tile_comp->y1 - l_tile_comp->y0));
1990 if (l_tccp->qmfbid == 1) {
1991 for (i = 0; i < l_nb_elem; ++i) {
1992 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1996 for (i = 0; i < l_nb_elem; ++i) {
1997 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2010 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2012 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2013 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2014 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2015 (l_tile_comp->y1 - l_tile_comp->y0));
2017 OPJ_BYTE ** l_data = 00;
2018 opj_tcp_t * l_tcp = p_tcd->tcp;
2020 if (!p_tcd->tcp->mct) {
2024 if (p_tcd->tcp->mct == 2) {
2025 if (! p_tcd->tcp->m_mct_coding_matrix) {
2029 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2034 for (i = 0; i < l_tile->numcomps; ++i) {
2035 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2039 if (! opj_mct_encode_custom(/* MCT data */
2040 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2041 /* size of components */
2045 /* nb of components (i.e. size of pData) */
2047 /* tells if the data is signed */
2048 p_tcd->image->comps->sgnd)) {
2054 } else if (l_tcp->tccps->qmfbid == 0) {
2055 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2056 l_tile->comps[2].data, samples);
2058 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2059 l_tile->comps[2].data, samples);
2065 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2067 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2068 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2069 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2072 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2073 if (l_tccp->qmfbid == 1) {
2074 if (! opj_dwt_encode(l_tile_comp)) {
2077 } else if (l_tccp->qmfbid == 0) {
2078 if (! opj_dwt_encode_real(l_tile_comp)) {
2090 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2093 const OPJ_FLOAT64 * l_mct_norms;
2094 OPJ_UINT32 l_mct_numcomps = 0U;
2095 opj_tcp_t * l_tcp = p_tcd->tcp;
2097 l_t1 = opj_t1_create(OPJ_TRUE);
2102 if (l_tcp->mct == 1) {
2103 l_mct_numcomps = 3U;
2104 /* irreversible encoding */
2105 if (l_tcp->tccps->qmfbid == 0) {
2106 l_mct_norms = opj_mct_get_mct_norms_real();
2108 l_mct_norms = opj_mct_get_mct_norms();
2111 l_mct_numcomps = p_tcd->image->numcomps;
2112 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2115 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2117 opj_t1_destroy(l_t1);
2121 opj_t1_destroy(l_t1);
2126 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2127 OPJ_BYTE * p_dest_data,
2128 OPJ_UINT32 * p_data_written,
2129 OPJ_UINT32 p_max_dest_size,
2130 opj_codestream_info_t *p_cstr_info)
2134 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2139 if (! opj_t2_encode_packets(
2142 p_tcd->tcd_image->tiles,
2143 p_tcd->tcp->numlayers,
2152 opj_t2_destroy(l_t2);
2156 opj_t2_destroy(l_t2);
2158 /*---------------CLEAN-------------------*/
2163 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2164 OPJ_BYTE * p_dest_data,
2165 OPJ_UINT32 p_max_dest_size,
2166 opj_codestream_info_t *p_cstr_info)
2168 opj_cp_t * l_cp = p_tcd->cp;
2169 OPJ_UINT32 l_nb_written = 0;
2172 p_cstr_info->index_write = 0;
2175 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2176 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2178 /* Normal Rate/distortion allocation */
2179 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2184 /* Fixed layer allocation */
2185 opj_tcd_rateallocate_fixed(p_tcd);
2192 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2194 OPJ_UINT32 p_src_length)
2196 OPJ_UINT32 i, j, l_data_size = 0;
2197 opj_image_comp_t * l_img_comp = 00;
2198 opj_tcd_tilecomp_t * l_tilec = 00;
2199 OPJ_UINT32 l_size_comp, l_remaining;
2200 OPJ_UINT32 l_nb_elem;
2202 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2203 if (l_data_size != p_src_length) {
2207 l_tilec = p_tcd->tcd_image->tiles->comps;
2208 l_img_comp = p_tcd->image->comps;
2209 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2210 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2211 l_remaining = l_img_comp->prec & 7; /* (%8) */
2212 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2219 if (l_size_comp == 3) {
2223 switch (l_size_comp) {
2225 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2226 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2228 if (l_img_comp->sgnd) {
2229 for (j = 0; j < l_nb_elem; ++j) {
2230 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2233 for (j = 0; j < l_nb_elem; ++j) {
2234 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2238 p_src = (OPJ_BYTE*) l_src_ptr;
2242 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2243 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2245 if (l_img_comp->sgnd) {
2246 for (j = 0; j < l_nb_elem; ++j) {
2247 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2250 for (j = 0; j < l_nb_elem; ++j) {
2251 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2255 p_src = (OPJ_BYTE*) l_src_ptr;
2259 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2260 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2262 for (j = 0; j < l_nb_elem; ++j) {
2263 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2266 p_src = (OPJ_BYTE*) l_src_ptr;