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"
43 /* ----------------------------------------------------------------------- */
47 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
48 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
50 fprintf(fd, "image {\n");
51 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
52 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
54 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
55 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
56 fprintf(fd, " tile {\n");
57 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
58 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
59 for (compno = 0; compno < tile->numcomps; compno++) {
60 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
61 fprintf(fd, " tilec {\n");
63 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
64 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
65 for (resno = 0; resno < tilec->numresolutions; resno++) {
66 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
67 fprintf(fd, "\n res {\n");
69 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
70 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
71 for (bandno = 0; bandno < res->numbands; bandno++) {
72 opj_tcd_band_t *band = &res->bands[bandno];
73 fprintf(fd, " band {\n");
75 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
76 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
77 for (precno = 0; precno < res->pw * res->ph; precno++) {
78 opj_tcd_precinct_t *prec = &band->precincts[precno];
79 fprintf(fd, " prec {\n");
81 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
82 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
84 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
85 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
86 fprintf(fd, " cblk {\n");
88 " x0=%d, y0=%d, x1=%d, y1=%d\n",
89 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
108 * Initializes tile coding/decoding
110 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block);
113 * Allocates memory for a decoding code block.
115 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
118 * Deallocates the decoding data of the given precinct.
120 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
123 * Allocates memory for an encoding code block (but not data).
125 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
128 * Allocates data for an encoding code block
130 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block);
133 * Deallocates the encoding data of the given precinct.
135 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
139 Free the memory allocated for encoding
140 @param tcd TCD handle
142 static void opj_tcd_free_tile(opj_tcd_t *tcd);
145 static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
146 OPJ_BYTE * p_src_data,
147 OPJ_UINT32 * p_data_read,
148 OPJ_UINT32 p_max_src_size,
149 opj_codestream_index_t *p_cstr_index );
151 static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
153 static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
155 static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd);
157 static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
160 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
162 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
164 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
166 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
168 static OPJ_BOOL opj_tcd_t2_encode ( opj_tcd_t *p_tcd,
169 OPJ_BYTE * p_dest_data,
170 OPJ_UINT32 * p_data_written,
171 OPJ_UINT32 p_max_dest_size,
172 opj_codestream_info_t *p_cstr_info );
174 static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
175 OPJ_BYTE * p_dest_data,
176 OPJ_UINT32 p_max_dest_size,
177 opj_codestream_info_t *p_cstr_info );
179 /* ----------------------------------------------------------------------- */
182 Create a new TCD handle
184 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
186 opj_tcd_t *l_tcd = 00;
188 /* create the tcd structure */
189 l_tcd = (opj_tcd_t*) opj_calloc(1,sizeof(opj_tcd_t));
194 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
196 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1,sizeof(opj_tcd_image_t));
197 if (!l_tcd->tcd_image) {
206 /* ----------------------------------------------------------------------- */
208 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
211 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
212 opj_tcd_makelayer_fixed(tcd, layno, 1);
217 void opj_tcd_makelayer( opj_tcd_t *tcd,
222 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
225 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
227 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
229 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
230 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
232 for (resno = 0; resno < tilec->numresolutions; resno++) {
233 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
235 for (bandno = 0; bandno < res->numbands; bandno++) {
236 opj_tcd_band_t *band = &res->bands[bandno];
238 for (precno = 0; precno < res->pw * res->ph; precno++) {
239 opj_tcd_precinct_t *prc = &band->precincts[precno];
241 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
242 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
243 opj_tcd_layer_t *layer = &cblk->layers[layno];
247 cblk->numpassesinlayers = 0;
250 n = cblk->numpassesinlayers;
252 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
255 opj_tcd_pass_t *pass = &cblk->passes[passno];
259 dd = pass->distortiondec;
261 dr = pass->rate - cblk->passes[n - 1].rate;
262 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
270 if (dd / dr >= thresh)
274 layer->numpasses = n - cblk->numpassesinlayers;
276 if (!layer->numpasses) {
281 if (cblk->numpassesinlayers == 0) {
282 layer->len = cblk->passes[n - 1].rate;
283 layer->data = cblk->data;
284 layer->disto = cblk->passes[n - 1].distortiondec;
286 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
287 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
288 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
291 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
294 cblk->numpassesinlayers = n;
302 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
303 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
304 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
305 OPJ_INT32 matrice[10][10][3];
308 opj_cp_t *cp = tcd->cp;
309 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
310 opj_tcp_t *tcd_tcp = tcd->tcp;
312 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
313 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
315 for (i = 0; i < tcd_tcp->numlayers; i++) {
316 for (j = 0; j < tilec->numresolutions; j++) {
317 for (k = 0; k < 3; k++) {
319 (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
320 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
325 for (resno = 0; resno < tilec->numresolutions; resno++) {
326 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
328 for (bandno = 0; bandno < res->numbands; bandno++) {
329 opj_tcd_band_t *band = &res->bands[bandno];
331 for (precno = 0; precno < res->pw * res->ph; precno++) {
332 opj_tcd_precinct_t *prc = &band->precincts[precno];
334 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
335 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
336 opj_tcd_layer_t *layer = &cblk->layers[layno];
338 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
340 /* Correction of the matrix of coefficient to include the IMSB information */
342 value = matrice[layno][resno][bandno];
349 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
350 if (imsb >= matrice[layno - 1][resno][bandno]) {
351 value -= (imsb - matrice[layno - 1][resno][bandno]);
359 cblk->numpassesinlayers = 0;
362 n = cblk->numpassesinlayers;
363 if (cblk->numpassesinlayers == 0) {
365 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
367 n = cblk->numpassesinlayers;
370 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
373 layer->numpasses = n - cblk->numpassesinlayers;
375 if (!layer->numpasses)
378 if (cblk->numpassesinlayers == 0) {
379 layer->len = cblk->passes[n - 1].rate;
380 layer->data = cblk->data;
382 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
383 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
387 cblk->numpassesinlayers = n;
395 OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
397 OPJ_UINT32 * p_data_written,
399 opj_codestream_info_t *cstr_info)
401 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
403 OPJ_FLOAT64 min, max;
404 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
405 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
406 OPJ_FLOAT64 maxSE = 0;
408 opj_cp_t *cp = tcd->cp;
409 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
410 opj_tcp_t *tcd_tcp = tcd->tcp;
415 tcd_tile->numpix = 0; /* fixed_quality */
417 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
418 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
421 for (resno = 0; resno < tilec->numresolutions; resno++) {
422 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
424 for (bandno = 0; bandno < res->numbands; bandno++) {
425 opj_tcd_band_t *band = &res->bands[bandno];
427 for (precno = 0; precno < res->pw * res->ph; precno++) {
428 opj_tcd_precinct_t *prc = &band->precincts[precno];
430 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
431 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
433 for (passno = 0; passno < cblk->totalpasses; passno++) {
434 opj_tcd_pass_t *pass = &cblk->passes[passno];
436 OPJ_FLOAT64 dd, rdslope;
439 dr = (OPJ_INT32)pass->rate;
440 dd = pass->distortiondec;
442 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
443 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
461 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
462 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
468 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
469 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
470 * ((OPJ_FLOAT64)(tilec->numpix));
475 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
476 tile_info->numpix = tcd_tile->numpix;
477 tile_info->distotile = tcd_tile->distotile;
478 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
479 if (!tile_info->thresh) {
480 /* FIXME event manager error callback */
485 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
486 OPJ_FLOAT64 lo = min;
487 OPJ_FLOAT64 hi = max;
488 OPJ_BOOL success = OPJ_FALSE;
489 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
490 OPJ_FLOAT64 goodthresh = 0;
491 OPJ_FLOAT64 stable_thresh = 0;
493 OPJ_FLOAT64 distotarget; /* fixed_quality */
496 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
498 /* Don't try to find an optimal threshold but rather take everything not included yet, if
499 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
500 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
501 ==> possible to have some lossy layers and the last layer for sure lossless */
502 if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
503 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
504 OPJ_FLOAT64 thresh = 0;
510 for (i = 0; i < 128; ++i) {
511 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
513 thresh = (lo + hi) / 2;
515 opj_tcd_makelayer(tcd, layno, thresh, 0);
517 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
518 if(OPJ_IS_CINEMA(cp->rsiz)){
519 if (! opj_t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) {
525 distoachieved = layno == 0 ?
526 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
528 if (distoachieved < distotarget) {
530 stable_thresh = thresh;
537 distoachieved = (layno == 0) ?
538 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
540 if (distoachieved < distotarget) {
542 stable_thresh = thresh;
548 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC))
550 /* TODO: what to do with l ??? seek / tell ??? */
551 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
557 stable_thresh = thresh;
562 goodthresh = stable_thresh == 0? thresh : stable_thresh;
574 if(cstr_info) { /* Threshold for Marcela Index */
575 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
578 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
581 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
587 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
588 opj_image_t * p_image,
591 p_tcd->image = p_image;
594 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,sizeof(opj_tcd_tile_t));
595 if (! p_tcd->tcd_image->tiles) {
599 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(p_image->numcomps,sizeof(opj_tcd_tilecomp_t));
600 if (! p_tcd->tcd_image->tiles->comps ) {
604 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
605 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
611 Destroy a previously created TCD handle
613 void opj_tcd_destroy(opj_tcd_t *tcd) {
615 opj_tcd_free_tile(tcd);
617 if (tcd->tcd_image) {
618 opj_free(tcd->tcd_image);
625 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
627 if ((l_tilec->data == 00) || ((l_tilec->data_size_needed > l_tilec->data_size) && (l_tilec->ownsData == OPJ_FALSE))) {
628 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_tilec->data_size_needed);
629 if (! l_tilec->data ) {
632 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
633 l_tilec->data_size = l_tilec->data_size_needed;
634 l_tilec->ownsData = OPJ_TRUE;
636 else if (l_tilec->data_size_needed > l_tilec->data_size) {
637 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_tilec->data_size_needed);
638 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */
639 /* fprintf(stderr, "Not enough memory to handle tile data"); */
641 opj_free(l_tilec->data);
642 l_tilec->data = NULL;
643 l_tilec->data_size = 0;
644 l_tilec->data_size_needed = 0;
645 l_tilec->ownsData = OPJ_FALSE;
648 l_tilec->data = new_data;
649 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
650 l_tilec->data_size = l_tilec->data_size_needed;
651 l_tilec->ownsData = OPJ_TRUE;
656 /* ----------------------------------------------------------------------- */
658 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block)
660 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;
661 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
662 opj_tcp_t * l_tcp = 00;
663 opj_cp_t * l_cp = 00;
664 opj_tcd_tile_t * l_tile = 00;
665 opj_tccp_t *l_tccp = 00;
666 opj_tcd_tilecomp_t *l_tilec = 00;
667 opj_image_comp_t * l_image_comp = 00;
668 opj_tcd_resolution_t *l_res = 00;
669 opj_tcd_band_t *l_band = 00;
670 opj_stepsize_t * l_step_size = 00;
671 opj_tcd_precinct_t *l_current_precinct = 00;
672 opj_image_t *l_image = 00;
674 OPJ_UINT32 l_level_no;
675 OPJ_UINT32 l_pdx, l_pdy;
677 OPJ_INT32 l_x0b, l_y0b;
678 /* extent of precincts , top left, bottom right**/
679 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
680 /* number of precinct for a resolution */
681 OPJ_UINT32 l_nb_precincts;
682 /* room needed to store l_nb_precinct precinct for a resolution */
683 OPJ_UINT32 l_nb_precinct_size;
684 /* number of code blocks for a precinct*/
685 OPJ_UINT32 l_nb_code_blocks;
686 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
687 OPJ_UINT32 l_nb_code_blocks_size;
688 /* size of data for a tile */
689 OPJ_UINT32 l_data_size;
692 l_tcp = &(l_cp->tcps[p_tile_no]);
693 l_tile = p_tcd->tcd_image->tiles;
694 l_tccp = l_tcp->tccps;
695 l_tilec = l_tile->comps;
696 l_image = p_tcd->image;
697 l_image_comp = p_tcd->image->comps;
699 p = p_tile_no % l_cp->tw; /* tile coordinates */
700 q = p_tile_no / l_cp->tw;
701 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
703 /* 4 borders of the tile rescale on the image if necessary */
704 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0);
705 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0);
706 l_tile->x1 = (OPJ_INT32)opj_uint_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1);
707 l_tile->y1 = (OPJ_INT32)opj_uint_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1);
709 /* testcase 1888.pdf.asan.35.988 */
710 if (l_tccp->numresolutions == 0) {
711 fprintf(stderr, "tiles require at least one resolution\n");
714 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
716 /*tile->numcomps = image->numcomps; */
717 for (compno = 0; compno < l_tile->numcomps; ++compno) {
718 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
719 l_image_comp->resno_decoded = 0;
720 /* border of each l_tile component (global) */
721 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
722 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
723 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
724 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
725 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
727 /* compute l_data_size with overflow check */
728 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
729 if ((((OPJ_UINT32)-1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0)) {
733 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
735 if ((((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
739 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
740 l_tilec->numresolutions = l_tccp->numresolutions;
741 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
742 l_tilec->minimum_num_resolutions = 1;
745 l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;
748 l_tilec->data_size_needed = l_data_size;
749 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
753 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t);
755 if (l_tilec->resolutions == 00) {
756 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
757 if (! l_tilec->resolutions ) {
760 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
761 l_tilec->resolutions_size = l_data_size;
762 memset(l_tilec->resolutions,0,l_data_size);
764 else if (l_data_size > l_tilec->resolutions_size) {
765 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);
766 if (! new_resolutions) {
767 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */
768 fprintf(stderr, "Not enough memory to tile resolutions\n");
769 opj_free(l_tilec->resolutions);
770 l_tilec->resolutions = NULL;
771 l_tilec->resolutions_size = 0;
774 l_tilec->resolutions = new_resolutions;
775 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
776 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);
777 l_tilec->resolutions_size = l_data_size;
780 l_level_no = l_tilec->numresolutions - 1;
781 l_res = l_tilec->resolutions;
782 l_step_size = l_tccp->stepsizes;
783 if (l_tccp->qmfbid == 0) {
784 l_gain_ptr = &opj_dwt_getgain_real;
787 l_gain_ptr = &opj_dwt_getgain;
789 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
791 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
792 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
793 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
794 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
795 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
797 /* border for each resolution level (global) */
798 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
799 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
800 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
801 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
802 /*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);*/
803 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
804 l_pdx = l_tccp->prcw[resno];
805 l_pdy = l_tccp->prch[resno];
806 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
807 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
808 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
809 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
810 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
811 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
812 /*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 );*/
814 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
815 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
816 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
818 l_nb_precincts = l_res->pw * l_res->ph;
819 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
821 tlcbgxstart = l_tl_prc_x_start;
822 tlcbgystart = l_tl_prc_y_start;
823 /*brcbgxend = l_br_prc_x_end;*/
824 /* brcbgyend = l_br_prc_y_end;*/
825 cbgwidthexpn = l_pdx;
826 cbgheightexpn = l_pdy;
830 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
831 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
832 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
833 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
834 cbgwidthexpn = l_pdx - 1;
835 cbgheightexpn = l_pdy - 1;
839 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
840 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
841 l_band = l_res->bands;
843 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
845 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
849 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
850 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
851 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
852 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
855 l_band->bandno = bandno + 1;
856 /* x0b = 1 if bandno = 1 or 3 */
857 l_x0b = l_band->bandno&1;
858 /* y0b = 1 if bandno = 2 or 3 */
859 l_y0b = (OPJ_INT32)((l_band->bandno)>>1);
860 /* l_band border (global) */
861 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));
862 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));
863 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));
864 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));
867 /** avoid an if with storing function pointer */
868 l_gain = (*l_gain_ptr) (l_band->bandno);
869 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
870 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * fraction;
871 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */
873 if (! l_band->precincts) {
874 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size);
875 if (! l_band->precincts) {
878 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
879 memset(l_band->precincts,0,l_nb_precinct_size);
880 l_band->precincts_data_size = l_nb_precinct_size;
882 else if (l_band->precincts_data_size < l_nb_precinct_size) {
884 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);
885 if (! new_precincts) {
886 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */
887 fprintf(stderr, "Not enough memory to handle band precints\n");
888 opj_free(l_band->precincts);
889 l_band->precincts = NULL;
890 l_band->precincts_data_size = 0;
893 l_band->precincts = new_precincts;
894 /*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);*/
895 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);
896 l_band->precincts_data_size = l_nb_precinct_size;
899 l_current_precinct = l_band->precincts;
900 for (precno = 0; precno < l_nb_precincts; ++precno) {
901 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
902 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn);
903 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn);
904 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
905 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
906 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
907 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
909 /* precinct size (global) */
910 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
912 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
913 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
914 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
915 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
916 /*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);*/
918 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
919 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
920 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
921 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
922 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
923 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
924 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
925 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
926 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn);
927 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn);
929 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
930 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
931 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
933 if (! l_current_precinct->cblks.blocks) {
934 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
935 if (! l_current_precinct->cblks.blocks ) {
938 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
940 memset(l_current_precinct->cblks.blocks,0,l_nb_code_blocks_size);
942 l_current_precinct->block_size = l_nb_code_blocks_size;
944 else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
945 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks, l_nb_code_blocks_size);
947 opj_free(l_current_precinct->cblks.blocks);
948 l_current_precinct->cblks.blocks = NULL;
949 l_current_precinct->block_size = 0;
950 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */
951 fprintf(stderr, "Not enough memory for current precinct codeblock element\n");
954 l_current_precinct->cblks.blocks = new_blocks;
955 /*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); */
957 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) + l_current_precinct->block_size
959 ,l_nb_code_blocks_size - l_current_precinct->block_size);
961 l_current_precinct->block_size = l_nb_code_blocks_size;
964 if (! l_current_precinct->incltree) {
965 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
966 l_current_precinct->ch);
969 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
970 l_current_precinct->cw,
971 l_current_precinct->ch);
974 if (! l_current_precinct->incltree) {
975 fprintf(stderr, "WARNING: No incltree created.\n");
976 /*return OPJ_FALSE;*/
979 if (! l_current_precinct->imsbtree) {
980 l_current_precinct->imsbtree = opj_tgt_create(
981 l_current_precinct->cw,
982 l_current_precinct->ch);
985 l_current_precinct->imsbtree = opj_tgt_init(
986 l_current_precinct->imsbtree,
987 l_current_precinct->cw,
988 l_current_precinct->ch);
991 if (! l_current_precinct->imsbtree) {
992 fprintf(stderr, "WARNING: No imsbtree created.\n");
993 /*return OPJ_FALSE;*/
996 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
997 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);
998 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);
999 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1000 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1003 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1005 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1008 /* code-block size (global) */
1009 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1010 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1011 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1012 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1014 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1018 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1020 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1023 /* code-block size (global) */
1024 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1025 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1026 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1027 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1030 ++l_current_precinct;
1045 OPJ_BOOL opj_tcd_init_encode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no)
1047 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F, sizeof(opj_tcd_cblk_enc_t));
1050 OPJ_BOOL opj_tcd_init_decode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no)
1052 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F, sizeof(opj_tcd_cblk_dec_t));
1056 * Allocates memory for an encoding code block (but not data memory).
1058 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1060 if (! p_code_block->layers) {
1061 /* no memset since data */
1062 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
1063 if (! p_code_block->layers) {
1067 if (! p_code_block->passes) {
1068 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
1069 if (! p_code_block->passes) {
1077 * Allocates data memory for an encoding code block.
1079 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block)
1081 OPJ_UINT32 l_data_size;
1083 l_data_size = (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) * (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1085 if (l_data_size > p_code_block->data_size) {
1086 if (p_code_block->data) {
1087 opj_free(p_code_block->data - 1); /* again, why -1 */
1089 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size);
1090 if(! p_code_block->data) {
1091 p_code_block->data_size = 0U;
1094 p_code_block->data_size = l_data_size;
1096 p_code_block->data[0] = 0;
1097 p_code_block->data+=1; /*why +1 ?*/
1103 * Allocates memory for a decoding code block.
1105 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1107 if (! p_code_block->data) {
1109 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1110 if (! p_code_block->data) {
1113 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1114 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1116 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t));
1117 if (! p_code_block->segs) {
1120 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1122 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1123 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1126 OPJ_BYTE* l_data = p_code_block->data;
1127 OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1128 opj_tcd_seg_t * l_segs = p_code_block->segs;
1129 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1131 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1132 p_code_block->data = l_data;
1133 p_code_block->data_max_size = l_data_max_size;
1134 p_code_block->segs = l_segs;
1135 p_code_block->m_current_max_segs = l_current_max_segs;
1141 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1144 OPJ_UINT32 l_data_size = 0;
1145 opj_image_comp_t * l_img_comp = 00;
1146 opj_tcd_tilecomp_t * l_tile_comp = 00;
1147 opj_tcd_resolution_t * l_res = 00;
1148 OPJ_UINT32 l_size_comp, l_remaining;
1150 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1151 l_img_comp = p_tcd->image->comps;
1153 for (i=0;i<p_tcd->image->numcomps;++i) {
1154 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1155 l_remaining = l_img_comp->prec & 7; /* (%8) */
1161 if (l_size_comp == 3) {
1165 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1166 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1174 OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1175 OPJ_UINT32 p_tile_no,
1177 OPJ_UINT32 * p_data_written,
1178 OPJ_UINT32 p_max_length,
1179 opj_codestream_info_t *p_cstr_info)
1182 if (p_tcd->cur_tp_num == 0) {
1184 p_tcd->tcd_tileno = p_tile_no;
1185 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1187 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1189 OPJ_UINT32 l_num_packs = 0;
1191 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1192 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1194 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1195 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1197 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1198 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1200 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1201 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1202 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1204 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
1205 if (!p_cstr_info->tile[p_tile_no].packet) {
1206 /* FIXME event manager error callback */
1212 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1213 /*---------------TILE-------------------*/
1214 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1217 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1219 /* FIXME _ProfStart(PGROUP_MCT); */
1220 if (! opj_tcd_mct_encode(p_tcd)) {
1223 /* FIXME _ProfStop(PGROUP_MCT); */
1225 /* FIXME _ProfStart(PGROUP_DWT); */
1226 if (! opj_tcd_dwt_encode(p_tcd)) {
1229 /* FIXME _ProfStop(PGROUP_DWT); */
1231 /* FIXME _ProfStart(PGROUP_T1); */
1232 if (! opj_tcd_t1_encode(p_tcd)) {
1235 /* FIXME _ProfStop(PGROUP_T1); */
1237 /* FIXME _ProfStart(PGROUP_RATE); */
1238 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1241 /* FIXME _ProfStop(PGROUP_RATE); */
1244 /*--------------TIER2------------------*/
1248 p_cstr_info->index_write = 1;
1250 /* FIXME _ProfStart(PGROUP_T2); */
1252 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1255 /* FIXME _ProfStop(PGROUP_T2); */
1257 /*---------------CLEAN-------------------*/
1262 OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1264 OPJ_UINT32 p_max_length,
1265 OPJ_UINT32 p_tile_no,
1266 opj_codestream_index_t *p_cstr_index
1269 OPJ_UINT32 l_data_read;
1270 p_tcd->tcd_tileno = p_tile_no;
1271 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1273 #ifdef TODO_MSD /* FIXME */
1276 OPJ_UINT32 resno, compno, numprec = 0;
1277 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1278 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1279 opj_tccp_t *tccp = &tcp->tccps[compno];
1280 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1281 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1282 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1283 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1284 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1285 numprec += res_idx->pw * res_idx->ph;
1286 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1287 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1290 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1291 p_cstr_info->packno = 0;
1296 /*--------------TIER2------------------*/
1297 /* FIXME _ProfStart(PGROUP_T2); */
1299 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1303 /* FIXME _ProfStop(PGROUP_T2); */
1305 /*------------------TIER1-----------------*/
1307 /* FIXME _ProfStart(PGROUP_T1); */
1309 (! opj_tcd_t1_decode(p_tcd))
1313 /* FIXME _ProfStop(PGROUP_T1); */
1315 /*----------------DWT---------------------*/
1317 /* FIXME _ProfStart(PGROUP_DWT); */
1319 (! opj_tcd_dwt_decode(p_tcd))
1323 /* FIXME _ProfStop(PGROUP_DWT); */
1325 /*----------------MCT-------------------*/
1326 /* FIXME _ProfStart(PGROUP_MCT); */
1328 (! opj_tcd_mct_decode(p_tcd))
1332 /* FIXME _ProfStop(PGROUP_MCT); */
1334 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1336 (! opj_tcd_dc_level_shift_decode(p_tcd))
1340 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1343 /*---------------TILE-------------------*/
1347 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1349 OPJ_UINT32 p_dest_length
1352 OPJ_UINT32 i,j,k,l_data_size = 0;
1353 opj_image_comp_t * l_img_comp = 00;
1354 opj_tcd_tilecomp_t * l_tilec = 00;
1355 opj_tcd_resolution_t * l_res;
1356 OPJ_UINT32 l_size_comp, l_remaining;
1357 OPJ_UINT32 l_stride, l_width,l_height;
1359 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1360 if (l_data_size > p_dest_length) {
1364 l_tilec = p_tcd->tcd_image->tiles->comps;
1365 l_img_comp = p_tcd->image->comps;
1367 for (i=0;i<p_tcd->image->numcomps;++i) {
1368 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1369 l_remaining = l_img_comp->prec & 7; /* (%8) */
1370 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1371 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1372 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1373 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1379 if (l_size_comp == 3) {
1383 switch (l_size_comp)
1387 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1388 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1390 if (l_img_comp->sgnd) {
1391 for (j=0;j<l_height;++j) {
1392 for (k=0;k<l_width;++k) {
1393 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1395 l_src_ptr += l_stride;
1399 for (j=0;j<l_height;++j) {
1400 for (k=0;k<l_width;++k) {
1401 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1403 l_src_ptr += l_stride;
1407 p_dest = (OPJ_BYTE *)l_dest_ptr;
1412 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1413 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1415 if (l_img_comp->sgnd) {
1416 for (j=0;j<l_height;++j) {
1417 for (k=0;k<l_width;++k) {
1418 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1420 l_src_ptr += l_stride;
1424 for (j=0;j<l_height;++j) {
1425 for (k=0;k<l_width;++k) {
1426 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1428 l_src_ptr += l_stride;
1432 p_dest = (OPJ_BYTE*) l_dest_ptr;
1437 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1438 OPJ_INT32 * l_src_ptr = l_tilec->data;
1440 for (j=0;j<l_height;++j) {
1441 for (k=0;k<l_width;++k) {
1442 *(l_dest_ptr++) = (*(l_src_ptr++));
1444 l_src_ptr += l_stride;
1447 p_dest = (OPJ_BYTE*) l_dest_ptr;
1462 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1464 OPJ_UINT32 compno, resno, bandno, precno;
1465 opj_tcd_tile_t *l_tile = 00;
1466 opj_tcd_tilecomp_t *l_tile_comp = 00;
1467 opj_tcd_resolution_t *l_res = 00;
1468 opj_tcd_band_t *l_band = 00;
1469 opj_tcd_precinct_t *l_precinct = 00;
1470 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1471 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1477 if (! p_tcd->tcd_image) {
1481 if (p_tcd->m_is_decoder) {
1482 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1485 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1488 l_tile = p_tcd->tcd_image->tiles;
1493 l_tile_comp = l_tile->comps;
1495 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1496 l_res = l_tile_comp->resolutions;
1499 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1500 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1501 l_band = l_res->bands;
1502 for (bandno = 0; bandno < 3; ++bandno) {
1503 l_precinct = l_band->precincts;
1506 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1507 for (precno = 0; precno < l_nb_precincts; ++precno) {
1508 opj_tgt_destroy(l_precinct->incltree);
1509 l_precinct->incltree = 00;
1510 opj_tgt_destroy(l_precinct->imsbtree);
1511 l_precinct->imsbtree = 00;
1512 (*l_tcd_code_block_deallocate) (l_precinct);
1516 opj_free(l_band->precincts);
1517 l_band->precincts = 00;
1524 opj_free(l_tile_comp->resolutions);
1525 l_tile_comp->resolutions = 00;
1528 if (l_tile_comp->ownsData && l_tile_comp->data) {
1529 opj_free(l_tile_comp->data);
1530 l_tile_comp->data = 00;
1531 l_tile_comp->ownsData = 0;
1532 l_tile_comp->data_size = 0;
1533 l_tile_comp->data_size_needed = 0;
1538 opj_free(l_tile->comps);
1540 opj_free(p_tcd->tcd_image->tiles);
1541 p_tcd->tcd_image->tiles = 00;
1545 OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1546 OPJ_BYTE * p_src_data,
1547 OPJ_UINT32 * p_data_read,
1548 OPJ_UINT32 p_max_src_size,
1549 opj_codestream_index_t *p_cstr_index
1554 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1559 if (! opj_t2_decode_packets(
1562 p_tcd->tcd_image->tiles,
1567 opj_t2_destroy(l_t2);
1571 opj_t2_destroy(l_t2);
1573 /*---------------CLEAN-------------------*/
1577 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1581 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1582 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1583 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1586 l_t1 = opj_t1_create(OPJ_FALSE);
1591 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1592 /* The +3 is headroom required by the vectorized DWT */
1593 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1594 opj_t1_destroy(l_t1);
1601 opj_t1_destroy(l_t1);
1607 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1610 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1611 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1612 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1613 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1615 for (compno = 0; compno < l_tile->numcomps; compno++) {
1617 if (tcd->cp->reduce != 0) {
1618 tcd->image->comps[compno].resno_decoded =
1619 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1620 if (tcd->image->comps[compno].resno_decoded < 0)
1625 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1626 if(numres2decode > 0){
1629 if (l_tccp->qmfbid == 1) {
1630 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1635 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1647 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1649 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1650 opj_tcp_t * l_tcp = p_tcd->tcp;
1651 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1652 OPJ_UINT32 l_samples,i;
1658 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1660 if (l_tile->numcomps >= 3 ){
1661 /* testcase 1336.pdf.asan.47.376 */
1662 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 - l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1663 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1664 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1665 fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1668 else if (l_tcp->mct == 2) {
1671 if (! l_tcp->m_mct_decoding_matrix) {
1675 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1680 for (i=0;i<l_tile->numcomps;++i) {
1681 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1685 if (! opj_mct_decode_custom(/* MCT data */
1686 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1687 /* size of components */
1691 /* nb of components (i.e. size of pData) */
1693 /* tells if the data is signed */
1694 p_tcd->image->comps->sgnd)) {
1702 if (l_tcp->tccps->qmfbid == 1) {
1703 opj_mct_decode( l_tile->comps[0].data,
1704 l_tile->comps[1].data,
1705 l_tile->comps[2].data,
1709 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1710 (OPJ_FLOAT32*)l_tile->comps[1].data,
1711 (OPJ_FLOAT32*)l_tile->comps[2].data,
1717 /* FIXME need to use opj_event_msg function */
1718 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1725 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1728 opj_tcd_tilecomp_t * l_tile_comp = 00;
1729 opj_tccp_t * l_tccp = 00;
1730 opj_image_comp_t * l_img_comp = 00;
1731 opj_tcd_resolution_t* l_res = 00;
1732 opj_tcd_tile_t * l_tile;
1733 OPJ_UINT32 l_width,l_height,i,j;
1734 OPJ_INT32 * l_current_ptr;
1735 OPJ_INT32 l_min, l_max;
1736 OPJ_UINT32 l_stride;
1738 l_tile = p_tcd->tcd_image->tiles;
1739 l_tile_comp = l_tile->comps;
1740 l_tccp = p_tcd->tcp->tccps;
1741 l_img_comp = p_tcd->image->comps;
1743 for (compno = 0; compno < l_tile->numcomps; compno++) {
1744 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1745 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1746 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1747 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1749 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1751 if (l_img_comp->sgnd) {
1752 l_min = -(1 << (l_img_comp->prec - 1));
1753 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1757 l_max = (1 << l_img_comp->prec) - 1;
1760 l_current_ptr = l_tile_comp->data;
1762 if (l_tccp->qmfbid == 1) {
1763 for (j=0;j<l_height;++j) {
1764 for (i = 0; i < l_width; ++i) {
1765 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1768 l_current_ptr += l_stride;
1772 for (j=0;j<l_height;++j) {
1773 for (i = 0; i < l_width; ++i) {
1774 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1775 *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1778 l_current_ptr += l_stride;
1793 * Deallocates the encoding data of the given precinct.
1795 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1797 OPJ_UINT32 cblkno , l_nb_code_blocks;
1799 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1801 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1802 /*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);*/
1803 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1804 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 );*/
1807 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1808 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1810 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1812 if (l_code_block->data) {
1813 opj_free(l_code_block->data);
1814 l_code_block->data = 00;
1817 if (l_code_block->segs) {
1818 opj_free(l_code_block->segs );
1819 l_code_block->segs = 00;
1825 opj_free(p_precinct->cblks.dec);
1826 p_precinct->cblks.dec = 00;
1831 * Deallocates the encoding data of the given precinct.
1833 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1835 OPJ_UINT32 cblkno , l_nb_code_blocks;
1837 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1839 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1841 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1842 if (l_code_block->data) {
1843 opj_free(l_code_block->data - 1);
1844 l_code_block->data = 00;
1847 if (l_code_block->layers) {
1848 opj_free(l_code_block->layers );
1849 l_code_block->layers = 00;
1852 if (l_code_block->passes) {
1853 opj_free(l_code_block->passes );
1854 l_code_block->passes = 00;
1859 opj_free(p_precinct->cblks.enc);
1861 p_precinct->cblks.enc = 00;
1865 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1867 OPJ_UINT32 i,l_data_size = 0;
1868 opj_image_comp_t * l_img_comp = 00;
1869 opj_tcd_tilecomp_t * l_tilec = 00;
1870 OPJ_UINT32 l_size_comp, l_remaining;
1872 l_tilec = p_tcd->tcd_image->tiles->comps;
1873 l_img_comp = p_tcd->image->comps;
1874 for (i=0;i<p_tcd->image->numcomps;++i) {
1875 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1876 l_remaining = l_img_comp->prec & 7; /* (%8) */
1882 if (l_size_comp == 3) {
1886 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1894 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1897 opj_tcd_tilecomp_t * l_tile_comp = 00;
1898 opj_tccp_t * l_tccp = 00;
1899 opj_image_comp_t * l_img_comp = 00;
1900 opj_tcd_tile_t * l_tile;
1901 OPJ_UINT32 l_nb_elem,i;
1902 OPJ_INT32 * l_current_ptr;
1904 l_tile = p_tcd->tcd_image->tiles;
1905 l_tile_comp = l_tile->comps;
1906 l_tccp = p_tcd->tcp->tccps;
1907 l_img_comp = p_tcd->image->comps;
1909 for (compno = 0; compno < l_tile->numcomps; compno++) {
1910 l_current_ptr = l_tile_comp->data;
1911 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1913 if (l_tccp->qmfbid == 1) {
1914 for (i = 0; i < l_nb_elem; ++i) {
1915 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1920 for (i = 0; i < l_nb_elem; ++i) {
1921 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1934 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1936 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1937 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1938 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1940 OPJ_BYTE ** l_data = 00;
1941 opj_tcp_t * l_tcp = p_tcd->tcp;
1943 if(!p_tcd->tcp->mct) {
1947 if (p_tcd->tcp->mct == 2) {
1948 if (! p_tcd->tcp->m_mct_coding_matrix) {
1952 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1957 for (i=0;i<l_tile->numcomps;++i) {
1958 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1962 if (! opj_mct_encode_custom(/* MCT data */
1963 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1964 /* size of components */
1968 /* nb of components (i.e. size of pData) */
1970 /* tells if the data is signed */
1971 p_tcd->image->comps->sgnd) )
1979 else if (l_tcp->tccps->qmfbid == 0) {
1980 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1983 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1989 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1991 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1992 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1993 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1996 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1997 if (l_tccp->qmfbid == 1) {
1998 if (! opj_dwt_encode(l_tile_comp)) {
2002 else if (l_tccp->qmfbid == 0) {
2003 if (! opj_dwt_encode_real(l_tile_comp)) {
2015 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
2018 const OPJ_FLOAT64 * l_mct_norms;
2019 OPJ_UINT32 l_mct_numcomps = 0U;
2020 opj_tcp_t * l_tcp = p_tcd->tcp;
2022 l_t1 = opj_t1_create(OPJ_TRUE);
2027 if (l_tcp->mct == 1) {
2028 l_mct_numcomps = 3U;
2029 /* irreversible encoding */
2030 if (l_tcp->tccps->qmfbid == 0) {
2031 l_mct_norms = opj_mct_get_mct_norms_real();
2034 l_mct_norms = opj_mct_get_mct_norms();
2038 l_mct_numcomps = p_tcd->image->numcomps;
2039 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
2042 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms, l_mct_numcomps)) {
2043 opj_t1_destroy(l_t1);
2047 opj_t1_destroy(l_t1);
2052 OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
2053 OPJ_BYTE * p_dest_data,
2054 OPJ_UINT32 * p_data_written,
2055 OPJ_UINT32 p_max_dest_size,
2056 opj_codestream_info_t *p_cstr_info )
2060 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2065 if (! opj_t2_encode_packets(
2068 p_tcd->tcd_image->tiles,
2069 p_tcd->tcp->numlayers,
2079 opj_t2_destroy(l_t2);
2083 opj_t2_destroy(l_t2);
2085 /*---------------CLEAN-------------------*/
2090 OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
2091 OPJ_BYTE * p_dest_data,
2092 OPJ_UINT32 p_max_dest_size,
2093 opj_codestream_info_t *p_cstr_info )
2095 opj_cp_t * l_cp = p_tcd->cp;
2096 OPJ_UINT32 l_nb_written = 0;
2099 p_cstr_info->index_write = 0;
2102 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2104 /* Normal Rate/distortion allocation */
2105 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2110 /* Fixed layer allocation */
2111 opj_tcd_rateallocate_fixed(p_tcd);
2118 OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2120 OPJ_UINT32 p_src_length )
2122 OPJ_UINT32 i,j,l_data_size = 0;
2123 opj_image_comp_t * l_img_comp = 00;
2124 opj_tcd_tilecomp_t * l_tilec = 00;
2125 OPJ_UINT32 l_size_comp, l_remaining;
2126 OPJ_UINT32 l_nb_elem;
2128 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2129 if (l_data_size != p_src_length) {
2133 l_tilec = p_tcd->tcd_image->tiles->comps;
2134 l_img_comp = p_tcd->image->comps;
2135 for (i=0;i<p_tcd->image->numcomps;++i) {
2136 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2137 l_remaining = l_img_comp->prec & 7; /* (%8) */
2138 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2144 if (l_size_comp == 3) {
2148 switch (l_size_comp) {
2151 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2152 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2154 if (l_img_comp->sgnd) {
2155 for (j=0;j<l_nb_elem;++j) {
2156 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2160 for (j=0;j<l_nb_elem;++j) {
2161 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2165 p_src = (OPJ_BYTE*) l_src_ptr;
2170 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2171 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2173 if (l_img_comp->sgnd) {
2174 for (j=0;j<l_nb_elem;++j) {
2175 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2179 for (j=0;j<l_nb_elem;++j) {
2180 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2184 p_src = (OPJ_BYTE*) l_src_ptr;
2189 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2190 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2192 for (j=0;j<l_nb_elem;++j) {
2193 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2196 p_src = (OPJ_BYTE*) l_src_ptr;