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);
107 * Allocates memory for a decoding code block.
109 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
112 * Deallocates the decoding data of the given precinct.
114 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
117 * Allocates memory for an encoding code block.
119 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
122 * Deallocates the encoding data of the given precinct.
124 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
128 Free the memory allocated for encoding
129 @param tcd TCD handle
131 static void opj_tcd_free_tile(opj_tcd_t *tcd);
134 static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
135 OPJ_BYTE * p_src_data,
136 OPJ_UINT32 * p_data_read,
137 OPJ_UINT32 p_max_src_size,
138 opj_codestream_index_t *p_cstr_index );
140 static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
142 static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
144 static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd);
146 static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
149 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
151 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
153 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
155 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
157 static OPJ_BOOL opj_tcd_t2_encode ( opj_tcd_t *p_tcd,
158 OPJ_BYTE * p_dest_data,
159 OPJ_UINT32 * p_data_written,
160 OPJ_UINT32 p_max_dest_size,
161 opj_codestream_info_t *p_cstr_info );
163 static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
164 OPJ_BYTE * p_dest_data,
165 OPJ_UINT32 p_max_dest_size,
166 opj_codestream_info_t *p_cstr_info );
168 /* ----------------------------------------------------------------------- */
171 Create a new TCD handle
173 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
175 opj_tcd_t *l_tcd = 00;
177 /* create the tcd structure */
178 l_tcd = (opj_tcd_t*) opj_malloc(sizeof(opj_tcd_t));
182 memset(l_tcd,0,sizeof(opj_tcd_t));
184 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
186 l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
187 if (!l_tcd->tcd_image) {
191 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
197 /* ----------------------------------------------------------------------- */
199 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
202 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
203 opj_tcd_makelayer_fixed(tcd, layno, 1);
208 void opj_tcd_makelayer( opj_tcd_t *tcd,
213 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
216 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
218 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
220 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
221 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
223 for (resno = 0; resno < tilec->numresolutions; resno++) {
224 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
226 for (bandno = 0; bandno < res->numbands; bandno++) {
227 opj_tcd_band_t *band = &res->bands[bandno];
229 for (precno = 0; precno < res->pw * res->ph; precno++) {
230 opj_tcd_precinct_t *prc = &band->precincts[precno];
232 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
233 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
234 opj_tcd_layer_t *layer = &cblk->layers[layno];
238 cblk->numpassesinlayers = 0;
241 n = cblk->numpassesinlayers;
243 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
246 opj_tcd_pass_t *pass = &cblk->passes[passno];
250 dd = pass->distortiondec;
252 dr = pass->rate - cblk->passes[n - 1].rate;
253 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
261 if (dd / dr >= thresh)
265 layer->numpasses = n - cblk->numpassesinlayers;
267 if (!layer->numpasses) {
272 if (cblk->numpassesinlayers == 0) {
273 layer->len = cblk->passes[n - 1].rate;
274 layer->data = cblk->data;
275 layer->disto = cblk->passes[n - 1].distortiondec;
277 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
278 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
279 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
282 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
285 cblk->numpassesinlayers = n;
293 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
294 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
295 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
296 OPJ_INT32 matrice[10][10][3];
299 opj_cp_t *cp = tcd->cp;
300 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
301 opj_tcp_t *tcd_tcp = tcd->tcp;
303 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
304 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
306 for (i = 0; i < tcd_tcp->numlayers; i++) {
307 for (j = 0; j < tilec->numresolutions; j++) {
308 for (k = 0; k < 3; k++) {
310 (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
311 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
316 for (resno = 0; resno < tilec->numresolutions; resno++) {
317 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
319 for (bandno = 0; bandno < res->numbands; bandno++) {
320 opj_tcd_band_t *band = &res->bands[bandno];
322 for (precno = 0; precno < res->pw * res->ph; precno++) {
323 opj_tcd_precinct_t *prc = &band->precincts[precno];
325 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
326 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
327 opj_tcd_layer_t *layer = &cblk->layers[layno];
329 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
331 /* Correction of the matrix of coefficient to include the IMSB information */
333 value = matrice[layno][resno][bandno];
340 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
341 if (imsb >= matrice[layno - 1][resno][bandno]) {
342 value -= (imsb - matrice[layno - 1][resno][bandno]);
350 cblk->numpassesinlayers = 0;
353 n = cblk->numpassesinlayers;
354 if (cblk->numpassesinlayers == 0) {
356 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
358 n = cblk->numpassesinlayers;
361 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
364 layer->numpasses = n - cblk->numpassesinlayers;
366 if (!layer->numpasses)
369 if (cblk->numpassesinlayers == 0) {
370 layer->len = cblk->passes[n - 1].rate;
371 layer->data = cblk->data;
373 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
374 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
378 cblk->numpassesinlayers = n;
386 OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
388 OPJ_UINT32 * p_data_written,
390 opj_codestream_info_t *cstr_info)
392 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
394 OPJ_FLOAT64 min, max;
395 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
396 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
397 OPJ_FLOAT64 maxSE = 0;
399 opj_cp_t *cp = tcd->cp;
400 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
401 opj_tcp_t *tcd_tcp = tcd->tcp;
406 tcd_tile->numpix = 0; /* fixed_quality */
408 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
409 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
412 for (resno = 0; resno < tilec->numresolutions; resno++) {
413 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
415 for (bandno = 0; bandno < res->numbands; bandno++) {
416 opj_tcd_band_t *band = &res->bands[bandno];
418 for (precno = 0; precno < res->pw * res->ph; precno++) {
419 opj_tcd_precinct_t *prc = &band->precincts[precno];
421 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
422 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
424 for (passno = 0; passno < cblk->totalpasses; passno++) {
425 opj_tcd_pass_t *pass = &cblk->passes[passno];
427 OPJ_FLOAT64 dd, rdslope;
430 dr = (OPJ_INT32)pass->rate;
431 dd = pass->distortiondec;
433 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
434 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
452 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
453 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
459 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
460 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
461 * ((OPJ_FLOAT64)(tilec->numpix));
466 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
467 tile_info->numpix = tcd_tile->numpix;
468 tile_info->distotile = tcd_tile->distotile;
469 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
472 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
473 OPJ_FLOAT64 lo = min;
474 OPJ_FLOAT64 hi = max;
475 OPJ_BOOL success = OPJ_FALSE;
476 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
477 OPJ_FLOAT64 goodthresh = 0;
478 OPJ_FLOAT64 stable_thresh = 0;
480 OPJ_FLOAT64 distotarget; /* fixed_quality */
483 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
485 /* Don't try to find an optimal threshold but rather take everything not included yet, if
486 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
487 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
488 ==> possible to have some lossy layers and the last layer for sure lossless */
489 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))) {
490 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
491 OPJ_FLOAT64 thresh = 0;
497 for (i = 0; i < 128; ++i) {
498 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
500 thresh = (lo + hi) / 2;
502 opj_tcd_makelayer(tcd, layno, thresh, 0);
504 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
505 if(cp->m_specific_param.m_enc.m_cinema){
506 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)) {
512 distoachieved = layno == 0 ?
513 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
515 if (distoachieved < distotarget) {
517 stable_thresh = thresh;
524 distoachieved = (layno == 0) ?
525 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
527 if (distoachieved < distotarget) {
529 stable_thresh = thresh;
535 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))
537 /* TODO: what to do with l ??? seek / tell ??? */
538 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
544 stable_thresh = thresh;
549 goodthresh = stable_thresh == 0? thresh : stable_thresh;
561 if(cstr_info) { /* Threshold for Marcela Index */
562 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
565 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
568 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
574 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
575 opj_image_t * p_image,
578 OPJ_UINT32 l_tile_comp_size;
580 p_tcd->image = p_image;
583 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
584 if (! p_tcd->tcd_image->tiles) {
587 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
589 l_tile_comp_size = p_image->numcomps * (OPJ_UINT32)sizeof(opj_tcd_tilecomp_t);
590 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size);
591 if (! p_tcd->tcd_image->tiles->comps ) {
594 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
596 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
597 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
603 Destroy a previously created TCD handle
605 void opj_tcd_destroy(opj_tcd_t *tcd) {
607 opj_tcd_free_tile(tcd);
609 if (tcd->tcd_image) {
610 opj_free(tcd->tcd_image);
617 /* ----------------------------------------------------------------------- */
618 #define OPJ_MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
619 OPJ_BOOL FUNCTION ( opj_tcd_t *p_tcd, \
620 OPJ_UINT32 p_tile_no \
623 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
624 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
625 opj_tcp_t * l_tcp = 00; \
626 opj_cp_t * l_cp = 00; \
627 opj_tcd_tile_t * l_tile = 00; \
628 opj_tccp_t *l_tccp = 00; \
629 opj_tcd_tilecomp_t *l_tilec = 00; \
630 opj_image_comp_t * l_image_comp = 00; \
631 opj_tcd_resolution_t *l_res = 00; \
632 opj_tcd_band_t *l_band = 00; \
633 opj_stepsize_t * l_step_size = 00; \
634 opj_tcd_precinct_t *l_current_precinct = 00; \
635 TYPE* l_code_block = 00; \
636 opj_image_t *l_image = 00; \
638 OPJ_UINT32 l_level_no; \
639 OPJ_UINT32 l_pdx, l_pdy; \
641 OPJ_INT32 l_x0b, l_y0b; \
642 /* extent of precincts , top left, bottom right**/ \
643 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
644 /* number of precinct for a resolution */ \
645 OPJ_UINT32 l_nb_precincts; \
646 /* room needed to store l_nb_precinct precinct for a resolution */ \
647 OPJ_UINT32 l_nb_precinct_size; \
648 /* number of code blocks for a precinct*/ \
649 OPJ_UINT32 l_nb_code_blocks; \
650 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
651 OPJ_UINT32 l_nb_code_blocks_size; \
652 /* size of data for a tile */ \
653 OPJ_UINT32 l_data_size; \
656 l_tcp = &(l_cp->tcps[p_tile_no]); \
657 l_tile = p_tcd->tcd_image->tiles; \
658 l_tccp = l_tcp->tccps; \
659 l_tilec = l_tile->comps; \
660 l_image = p_tcd->image; \
661 l_image_comp = p_tcd->image->comps; \
663 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
664 q = p_tile_no / l_cp->tw; \
665 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
667 /* 4 borders of the tile rescale on the image if necessary */ \
668 l_tile->x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + p * l_cp->tdx), (OPJ_INT32)l_image->x0); \
669 l_tile->y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + q * l_cp->tdy), (OPJ_INT32)l_image->y0); \
670 l_tile->x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (p + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1); \
671 l_tile->y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (q + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1); \
672 /* testcase 1888.pdf.asan.35.988 */ \
673 if (l_tccp->numresolutions == 0) { \
674 fprintf(stderr, "tiles require at least one resolution\n"); \
677 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \
679 /*tile->numcomps = image->numcomps; */ \
680 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
681 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
683 /* border of each l_tile component (global) */ \
684 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx); \
685 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy); \
686 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx); \
687 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy); \
688 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \
689 if (l_tilec->x1 < 0 || l_tilec->x0 < 0 || l_tilec->y1 < 0 || l_tilec->y0 < 0) return OPJ_FALSE;\
690 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) \
691 * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0) * (OPJ_UINT32)sizeof(OPJ_UINT32 );\
692 l_tilec->numresolutions = l_tccp->numresolutions; \
693 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
694 l_tilec->minimum_num_resolutions = 1; \
697 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
698 - l_cp->m_specific_param.m_dec.m_reduce; \
701 if (l_tilec->data == 00) { \
702 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
703 if (! l_tilec->data ) { \
706 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
708 l_tilec->data_size = l_data_size; \
710 else if (l_data_size > l_tilec->data_size) { \
711 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
712 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \
713 fprintf(stderr, "Not enough memory to handle tile data\n"); \
715 opj_free(l_tilec->data); \
716 l_tilec->data = NULL; \
717 l_tilec->data_size = 0; \
720 l_tilec->data = new_data; \
721 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
722 l_tilec->data_size = l_data_size; \
725 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t); \
727 if (l_tilec->resolutions == 00) { \
728 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); \
729 if (! l_tilec->resolutions ) { \
732 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/ \
733 l_tilec->resolutions_size = l_data_size; \
734 memset(l_tilec->resolutions,0,l_data_size); \
736 else if (l_data_size > l_tilec->resolutions_size) { \
737 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
738 if (! new_resolutions) { \
739 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \
740 fprintf(stderr, "Not enough memory to tile resolutions\n"); \
741 opj_free(l_tilec->resolutions); \
742 l_tilec->resolutions = NULL; \
743 l_tilec->resolutions_size = 0; \
746 l_tilec->resolutions = new_resolutions; \
747 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
748 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
749 l_tilec->resolutions_size = l_data_size; \
752 l_level_no = l_tilec->numresolutions - 1; \
753 l_res = l_tilec->resolutions; \
754 l_step_size = l_tccp->stepsizes; \
755 if (l_tccp->qmfbid == 0) { \
756 l_gain_ptr = &opj_dwt_getgain_real; \
759 l_gain_ptr = &opj_dwt_getgain; \
761 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
763 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
764 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
765 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/; \
766 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
767 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
769 /* border for each resolution level (global) */ \
770 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
771 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
772 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
773 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
774 /*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);*/ \
775 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
776 l_pdx = l_tccp->prcw[resno]; \
777 l_pdy = l_tccp->prch[resno]; \
778 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
779 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
780 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx; \
781 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy; \
782 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx; \
783 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy; \
784 /*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 );*/ \
786 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
787 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
788 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
790 l_nb_precincts = l_res->pw * l_res->ph; \
791 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t); \
793 tlcbgxstart = l_tl_prc_x_start; \
794 tlcbgystart = l_tl_prc_y_start; \
795 /*brcbgxend = l_br_prc_x_end;*/ \
796 /* brcbgyend = l_br_prc_y_end;*/ \
797 cbgwidthexpn = l_pdx; \
798 cbgheightexpn = l_pdy; \
799 l_res->numbands = 1; \
802 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1); \
803 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1); \
804 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/ \
805 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/ \
806 cbgwidthexpn = l_pdx - 1; \
807 cbgheightexpn = l_pdy - 1; \
808 l_res->numbands = 3; \
811 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn); \
812 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn); \
813 l_band = l_res->bands; \
815 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
817 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
820 l_band->bandno = 0 ; \
821 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
822 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
823 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
824 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
827 l_band->bandno = bandno + 1; \
828 /* x0b = 1 if bandno = 1 or 3 */ \
829 l_x0b = l_band->bandno&1; \
830 /* y0b = 1 if bandno = 2 or 3 */ \
831 l_y0b = (OPJ_INT32)((l_band->bandno)>>1); \
832 /* l_band border (global) */ \
833 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
834 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
835 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
836 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
839 /** avoid an if with storing function pointer */ \
840 l_gain = (*l_gain_ptr) (l_band->bandno); \
841 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain); \
842 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \
843 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */ \
845 if (! l_band->precincts) { \
846 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \
847 if (! l_band->precincts) { \
850 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */ \
851 memset(l_band->precincts,0,l_nb_precinct_size); \
852 l_band->precincts_data_size = l_nb_precinct_size; \
854 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
856 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \
857 if (! new_precincts) { \
858 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \
859 fprintf(stderr, "Not enough memory to handle band precints\n"); \
860 opj_free(l_band->precincts); \
861 l_band->precincts = NULL; \
862 l_band->precincts_data_size = 0; \
865 l_band->precincts = new_precincts; \
866 /*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);*/ \
867 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \
868 l_band->precincts_data_size = l_nb_precinct_size; \
871 l_current_precinct = l_band->precincts; \
872 for (precno = 0; precno < l_nb_precincts; ++precno) { \
873 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
874 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn); \
875 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn); \
876 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
877 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
878 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \
879 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \
881 /* precinct size (global) */ \
882 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
884 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0); \
885 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0); \
886 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1); \
887 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1); \
888 /*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);*/ \
890 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
891 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
892 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
893 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
894 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
895 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
896 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
897 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
898 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn); \
899 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn); \
901 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
902 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
903 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof(TYPE); \
905 if (! l_current_precinct->cblks.ELEMENT) { \
906 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \
907 if (! l_current_precinct->cblks.ELEMENT ) { \
910 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/ \
912 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \
914 l_current_precinct->block_size = l_nb_code_blocks_size; \
916 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
917 TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
918 if (! new_ELEMENT) { \
919 opj_free(l_current_precinct->cblks.ELEMENT); \
920 l_current_precinct->cblks.ELEMENT = NULL; \
921 l_current_precinct->block_size = 0; \
922 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \
923 fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \
926 l_current_precinct->cblks.ELEMENT = new_ELEMENT; \
927 /*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); */\
929 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
931 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
933 l_current_precinct->block_size = l_nb_code_blocks_size; \
936 if (! l_current_precinct->incltree) { \
937 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, \
938 l_current_precinct->ch); \
941 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, \
942 l_current_precinct->cw, \
943 l_current_precinct->ch); \
946 if (! l_current_precinct->incltree) { \
947 fprintf(stderr, "WARNING: No incltree created.\n"); \
948 /*return OPJ_FALSE;*/ \
951 if (! l_current_precinct->imsbtree) { \
952 l_current_precinct->imsbtree = opj_tgt_create( \
953 l_current_precinct->cw, \
954 l_current_precinct->ch); \
957 l_current_precinct->imsbtree = opj_tgt_init( \
958 l_current_precinct->imsbtree, \
959 l_current_precinct->cw, \
960 l_current_precinct->ch); \
963 if (! l_current_precinct->imsbtree) { \
964 fprintf(stderr, "WARNING: No imsbtree created.\n"); \
965 /*return OPJ_FALSE;*/ \
968 l_code_block = l_current_precinct->cblks.ELEMENT; \
970 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
971 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
972 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
973 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
974 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
976 /* code-block size (global) */ \
977 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); \
978 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); \
979 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); \
980 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); \
982 if (! FUNCTION_ELEMENT(l_code_block)) { \
987 ++l_current_precinct; \
1003 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
1004 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
1006 #undef OPJ_MACRO_TCD_ALLOCATE
1009 * Allocates memory for an encoding code block.
1011 OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1013 if (! p_code_block->data) {
1015 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
1016 if(! p_code_block->data) {
1020 p_code_block->data[0] = 0;
1021 p_code_block->data+=1;
1023 /* no memset since data */
1024 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
1025 if (! p_code_block->layers) {
1029 p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
1030 if (! p_code_block->passes) {
1035 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1036 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
1042 * Allocates memory for a decoding code block.
1044 OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1046 OPJ_UINT32 l_seg_size;
1048 if (! p_code_block->data) {
1050 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1051 if (! p_code_block->data) {
1054 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1055 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1057 l_seg_size = OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1058 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1059 if (! p_code_block->segs) {
1062 memset(p_code_block->segs,0,l_seg_size);
1063 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1065 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1066 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1069 /*p_code_block->numsegs = 0; */
1074 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1077 OPJ_UINT32 l_data_size = 0;
1078 opj_image_comp_t * l_img_comp = 00;
1079 opj_tcd_tilecomp_t * l_tile_comp = 00;
1080 opj_tcd_resolution_t * l_res = 00;
1081 OPJ_UINT32 l_size_comp, l_remaining;
1083 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1084 l_img_comp = p_tcd->image->comps;
1086 for (i=0;i<p_tcd->image->numcomps;++i) {
1087 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1088 l_remaining = l_img_comp->prec & 7; /* (%8) */
1094 if (l_size_comp == 3) {
1098 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1099 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1107 OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1108 OPJ_UINT32 p_tile_no,
1110 OPJ_UINT32 * p_data_written,
1111 OPJ_UINT32 p_max_length,
1112 opj_codestream_info_t *p_cstr_info)
1115 if (p_tcd->cur_tp_num == 0) {
1117 p_tcd->tcd_tileno = p_tile_no;
1118 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1120 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1122 OPJ_UINT32 l_num_packs = 0;
1124 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1125 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1127 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1128 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1130 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1131 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1133 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1134 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1135 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1137 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));
1141 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1142 /*---------------TILE-------------------*/
1143 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1146 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1148 /* FIXME _ProfStart(PGROUP_MCT); */
1149 if (! opj_tcd_mct_encode(p_tcd)) {
1152 /* FIXME _ProfStop(PGROUP_MCT); */
1154 /* FIXME _ProfStart(PGROUP_DWT); */
1155 if (! opj_tcd_dwt_encode(p_tcd)) {
1158 /* FIXME _ProfStop(PGROUP_DWT); */
1160 /* FIXME _ProfStart(PGROUP_T1); */
1161 if (! opj_tcd_t1_encode(p_tcd)) {
1164 /* FIXME _ProfStop(PGROUP_T1); */
1166 /* FIXME _ProfStart(PGROUP_RATE); */
1167 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1170 /* FIXME _ProfStop(PGROUP_RATE); */
1173 /*--------------TIER2------------------*/
1177 p_cstr_info->index_write = 1;
1179 /* FIXME _ProfStart(PGROUP_T2); */
1181 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1184 /* FIXME _ProfStop(PGROUP_T2); */
1186 /*---------------CLEAN-------------------*/
1191 OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1193 OPJ_UINT32 p_max_length,
1194 OPJ_UINT32 p_tile_no,
1195 opj_codestream_index_t *p_cstr_index
1198 OPJ_UINT32 l_data_read;
1199 p_tcd->tcd_tileno = p_tile_no;
1200 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1202 #ifdef TODO_MSD /* FIXME */
1205 OPJ_UINT32 resno, compno, numprec = 0;
1206 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1207 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1208 opj_tccp_t *tccp = &tcp->tccps[compno];
1209 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1210 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1211 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1212 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1213 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1214 numprec += res_idx->pw * res_idx->ph;
1215 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1216 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1219 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1220 p_cstr_info->packno = 0;
1225 /*--------------TIER2------------------*/
1226 /* FIXME _ProfStart(PGROUP_T2); */
1228 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1232 /* FIXME _ProfStop(PGROUP_T2); */
1234 /*------------------TIER1-----------------*/
1236 /* FIXME _ProfStart(PGROUP_T1); */
1238 (! opj_tcd_t1_decode(p_tcd))
1242 /* FIXME _ProfStop(PGROUP_T1); */
1244 /*----------------DWT---------------------*/
1246 /* FIXME _ProfStart(PGROUP_DWT); */
1248 (! opj_tcd_dwt_decode(p_tcd))
1252 /* FIXME _ProfStop(PGROUP_DWT); */
1254 /*----------------MCT-------------------*/
1255 /* FIXME _ProfStart(PGROUP_MCT); */
1257 (! opj_tcd_mct_decode(p_tcd))
1261 /* FIXME _ProfStop(PGROUP_MCT); */
1263 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1265 (! opj_tcd_dc_level_shift_decode(p_tcd))
1269 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1272 /*---------------TILE-------------------*/
1276 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1278 OPJ_UINT32 p_dest_length
1281 OPJ_UINT32 i,j,k,l_data_size = 0;
1282 opj_image_comp_t * l_img_comp = 00;
1283 opj_tcd_tilecomp_t * l_tilec = 00;
1284 opj_tcd_resolution_t * l_res;
1285 OPJ_UINT32 l_size_comp, l_remaining;
1286 OPJ_UINT32 l_stride, l_width,l_height;
1288 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1289 if (l_data_size > p_dest_length) {
1293 l_tilec = p_tcd->tcd_image->tiles->comps;
1294 l_img_comp = p_tcd->image->comps;
1296 for (i=0;i<p_tcd->image->numcomps;++i) {
1297 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1298 l_remaining = l_img_comp->prec & 7; /* (%8) */
1299 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1300 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1301 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1302 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1308 if (l_size_comp == 3) {
1312 switch (l_size_comp)
1316 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1317 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1319 if (l_img_comp->sgnd) {
1320 for (j=0;j<l_height;++j) {
1321 for (k=0;k<l_width;++k) {
1322 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1324 l_src_ptr += l_stride;
1328 for (j=0;j<l_height;++j) {
1329 for (k=0;k<l_width;++k) {
1330 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1332 l_src_ptr += l_stride;
1336 p_dest = (OPJ_BYTE *)l_dest_ptr;
1341 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1342 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1344 if (l_img_comp->sgnd) {
1345 for (j=0;j<l_height;++j) {
1346 for (k=0;k<l_width;++k) {
1347 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1349 l_src_ptr += l_stride;
1353 for (j=0;j<l_height;++j) {
1354 for (k=0;k<l_width;++k) {
1355 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1357 l_src_ptr += l_stride;
1361 p_dest = (OPJ_BYTE*) l_dest_ptr;
1366 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1367 OPJ_INT32 * l_src_ptr = l_tilec->data;
1369 for (j=0;j<l_height;++j) {
1370 for (k=0;k<l_width;++k) {
1371 *(l_dest_ptr++) = (*(l_src_ptr++));
1373 l_src_ptr += l_stride;
1376 p_dest = (OPJ_BYTE*) l_dest_ptr;
1391 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1393 OPJ_UINT32 compno, resno, bandno, precno;
1394 opj_tcd_tile_t *l_tile = 00;
1395 opj_tcd_tilecomp_t *l_tile_comp = 00;
1396 opj_tcd_resolution_t *l_res = 00;
1397 opj_tcd_band_t *l_band = 00;
1398 opj_tcd_precinct_t *l_precinct = 00;
1399 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1400 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1406 if (! p_tcd->tcd_image) {
1410 if (p_tcd->m_is_decoder) {
1411 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1414 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1417 l_tile = p_tcd->tcd_image->tiles;
1422 l_tile_comp = l_tile->comps;
1424 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1425 l_res = l_tile_comp->resolutions;
1428 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1429 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1430 l_band = l_res->bands;
1431 for (bandno = 0; bandno < 3; ++bandno) {
1432 l_precinct = l_band->precincts;
1435 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1436 for (precno = 0; precno < l_nb_precincts; ++precno) {
1437 opj_tgt_destroy(l_precinct->incltree);
1438 l_precinct->incltree = 00;
1439 opj_tgt_destroy(l_precinct->imsbtree);
1440 l_precinct->imsbtree = 00;
1441 (*l_tcd_code_block_deallocate) (l_precinct);
1445 opj_free(l_band->precincts);
1446 l_band->precincts = 00;
1453 opj_free(l_tile_comp->resolutions);
1454 l_tile_comp->resolutions = 00;
1457 if (l_tile_comp->data) {
1458 opj_free(l_tile_comp->data);
1459 l_tile_comp->data = 00;
1464 opj_free(l_tile->comps);
1466 opj_free(p_tcd->tcd_image->tiles);
1467 p_tcd->tcd_image->tiles = 00;
1471 OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1472 OPJ_BYTE * p_src_data,
1473 OPJ_UINT32 * p_data_read,
1474 OPJ_UINT32 p_max_src_size,
1475 opj_codestream_index_t *p_cstr_index
1480 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1485 if (! opj_t2_decode_packets(
1488 p_tcd->tcd_image->tiles,
1493 opj_t2_destroy(l_t2);
1497 opj_t2_destroy(l_t2);
1499 /*---------------CLEAN-------------------*/
1503 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1507 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1508 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1509 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1512 l_t1 = opj_t1_create();
1517 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1518 /* The +3 is headroom required by the vectorized DWT */
1519 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1520 opj_t1_destroy(l_t1);
1527 opj_t1_destroy(l_t1);
1533 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1536 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1537 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1538 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1539 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1541 for (compno = 0; compno < l_tile->numcomps; compno++) {
1543 if (tcd->cp->reduce != 0) {
1544 tcd->image->comps[compno].resno_decoded =
1545 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1546 if (tcd->image->comps[compno].resno_decoded < 0)
1551 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1552 if(numres2decode > 0){
1555 if (l_tccp->qmfbid == 1) {
1556 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1561 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1573 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1575 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1576 opj_tcp_t * l_tcp = p_tcd->tcp;
1577 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1578 OPJ_UINT32 l_samples,i;
1584 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1586 if (l_tile->numcomps >= 3 ){
1587 /* testcase 1336.pdf.asan.47.376 */
1588 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 ||
1589 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1590 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1591 fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1594 else if (l_tcp->mct == 2) {
1597 if (! l_tcp->m_mct_decoding_matrix) {
1601 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1606 for (i=0;i<l_tile->numcomps;++i) {
1607 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1611 if (! opj_mct_decode_custom(/* MCT data */
1612 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1613 /* size of components */
1617 /* nb of components (i.e. size of pData) */
1619 /* tells if the data is signed */
1620 p_tcd->image->comps->sgnd)) {
1628 if (l_tcp->tccps->qmfbid == 1) {
1629 opj_mct_decode( l_tile->comps[0].data,
1630 l_tile->comps[1].data,
1631 l_tile->comps[2].data,
1635 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1636 (OPJ_FLOAT32*)l_tile->comps[1].data,
1637 (OPJ_FLOAT32*)l_tile->comps[2].data,
1643 /* FIXME need to use opj_event_msg function */
1644 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1651 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1654 opj_tcd_tilecomp_t * l_tile_comp = 00;
1655 opj_tccp_t * l_tccp = 00;
1656 opj_image_comp_t * l_img_comp = 00;
1657 opj_tcd_resolution_t* l_res = 00;
1658 opj_tcd_tile_t * l_tile;
1659 OPJ_UINT32 l_width,l_height,i,j;
1660 OPJ_INT32 * l_current_ptr;
1661 OPJ_INT32 l_min, l_max;
1662 OPJ_UINT32 l_stride;
1664 l_tile = p_tcd->tcd_image->tiles;
1665 l_tile_comp = l_tile->comps;
1666 l_tccp = p_tcd->tcp->tccps;
1667 l_img_comp = p_tcd->image->comps;
1669 for (compno = 0; compno < l_tile->numcomps; compno++) {
1670 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1671 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1672 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1673 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1675 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1677 if (l_img_comp->sgnd) {
1678 l_min = -(1 << (l_img_comp->prec - 1));
1679 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1683 l_max = (1 << l_img_comp->prec) - 1;
1686 l_current_ptr = l_tile_comp->data;
1688 if (l_tccp->qmfbid == 1) {
1689 for (j=0;j<l_height;++j) {
1690 for (i = 0; i < l_width; ++i) {
1691 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1694 l_current_ptr += l_stride;
1698 for (j=0;j<l_height;++j) {
1699 for (i = 0; i < l_width; ++i) {
1700 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1701 *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1704 l_current_ptr += l_stride;
1719 * Deallocates the encoding data of the given precinct.
1721 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1723 OPJ_UINT32 cblkno , l_nb_code_blocks;
1725 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1727 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1728 /*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);*/
1729 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1730 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 );*/
1733 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1734 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1736 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1738 if (l_code_block->data) {
1739 opj_free(l_code_block->data);
1740 l_code_block->data = 00;
1743 if (l_code_block->segs) {
1744 opj_free(l_code_block->segs );
1745 l_code_block->segs = 00;
1751 opj_free(p_precinct->cblks.dec);
1752 p_precinct->cblks.dec = 00;
1757 * Deallocates the encoding data of the given precinct.
1759 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1761 OPJ_UINT32 cblkno , l_nb_code_blocks;
1763 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1765 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1767 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1768 if (l_code_block->data) {
1769 opj_free(l_code_block->data - 1);
1770 l_code_block->data = 00;
1773 if (l_code_block->layers) {
1774 opj_free(l_code_block->layers );
1775 l_code_block->layers = 00;
1778 if (l_code_block->passes) {
1779 opj_free(l_code_block->passes );
1780 l_code_block->passes = 00;
1785 opj_free(p_precinct->cblks.enc);
1787 p_precinct->cblks.enc = 00;
1791 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1793 OPJ_UINT32 i,l_data_size = 0;
1794 opj_image_comp_t * l_img_comp = 00;
1795 opj_tcd_tilecomp_t * l_tilec = 00;
1796 OPJ_UINT32 l_size_comp, l_remaining;
1798 l_tilec = p_tcd->tcd_image->tiles->comps;
1799 l_img_comp = p_tcd->image->comps;
1800 for (i=0;i<p_tcd->image->numcomps;++i) {
1801 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1802 l_remaining = l_img_comp->prec & 7; /* (%8) */
1808 if (l_size_comp == 3) {
1812 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1820 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1823 opj_tcd_tilecomp_t * l_tile_comp = 00;
1824 opj_tccp_t * l_tccp = 00;
1825 opj_image_comp_t * l_img_comp = 00;
1826 opj_tcd_tile_t * l_tile;
1827 OPJ_UINT32 l_nb_elem,i;
1828 OPJ_INT32 * l_current_ptr;
1830 l_tile = p_tcd->tcd_image->tiles;
1831 l_tile_comp = l_tile->comps;
1832 l_tccp = p_tcd->tcp->tccps;
1833 l_img_comp = p_tcd->image->comps;
1835 for (compno = 0; compno < l_tile->numcomps; compno++) {
1836 l_current_ptr = l_tile_comp->data;
1837 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1839 if (l_tccp->qmfbid == 1) {
1840 for (i = 0; i < l_nb_elem; ++i) {
1841 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1846 for (i = 0; i < l_nb_elem; ++i) {
1847 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1860 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1862 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1863 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1864 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1866 OPJ_BYTE ** l_data = 00;
1867 opj_tcp_t * l_tcp = p_tcd->tcp;
1869 if(!p_tcd->tcp->mct) {
1873 if (p_tcd->tcp->mct == 2) {
1874 if (! p_tcd->tcp->m_mct_coding_matrix) {
1878 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1883 for (i=0;i<l_tile->numcomps;++i) {
1884 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1888 if (! opj_mct_encode_custom(/* MCT data */
1889 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1890 /* size of components */
1894 /* nb of components (i.e. size of pData) */
1896 /* tells if the data is signed */
1897 p_tcd->image->comps->sgnd) )
1905 else if (l_tcp->tccps->qmfbid == 0) {
1906 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1909 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1915 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1917 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1918 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1919 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1922 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1923 if (l_tccp->qmfbid == 1) {
1924 if (! opj_dwt_encode(l_tile_comp)) {
1928 else if (l_tccp->qmfbid == 0) {
1929 if (! opj_dwt_encode_real(l_tile_comp)) {
1941 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1944 const OPJ_FLOAT64 * l_mct_norms;
1945 opj_tcp_t * l_tcp = p_tcd->tcp;
1947 l_t1 = opj_t1_create();
1952 if (l_tcp->mct == 1) {
1953 /* irreversible encoding */
1954 if (l_tcp->tccps->qmfbid == 0) {
1955 l_mct_norms = opj_mct_get_mct_norms_real();
1958 l_mct_norms = opj_mct_get_mct_norms();
1962 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1965 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1966 opj_t1_destroy(l_t1);
1970 opj_t1_destroy(l_t1);
1975 OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
1976 OPJ_BYTE * p_dest_data,
1977 OPJ_UINT32 * p_data_written,
1978 OPJ_UINT32 p_max_dest_size,
1979 opj_codestream_info_t *p_cstr_info )
1983 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1988 if (! opj_t2_encode_packets(
1991 p_tcd->tcd_image->tiles,
1992 p_tcd->tcp->numlayers,
2002 opj_t2_destroy(l_t2);
2006 opj_t2_destroy(l_t2);
2008 /*---------------CLEAN-------------------*/
2013 OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
2014 OPJ_BYTE * p_dest_data,
2015 OPJ_UINT32 p_max_dest_size,
2016 opj_codestream_info_t *p_cstr_info )
2018 opj_cp_t * l_cp = p_tcd->cp;
2019 OPJ_UINT32 l_nb_written = 0;
2022 p_cstr_info->index_write = 0;
2025 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2027 /* Normal Rate/distortion allocation */
2028 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2033 /* Fixed layer allocation */
2034 opj_tcd_rateallocate_fixed(p_tcd);
2041 OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2043 OPJ_UINT32 p_src_length )
2045 OPJ_UINT32 i,j,l_data_size = 0;
2046 opj_image_comp_t * l_img_comp = 00;
2047 opj_tcd_tilecomp_t * l_tilec = 00;
2048 OPJ_UINT32 l_size_comp, l_remaining;
2049 OPJ_UINT32 l_nb_elem;
2051 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2052 if (l_data_size != p_src_length) {
2056 l_tilec = p_tcd->tcd_image->tiles->comps;
2057 l_img_comp = p_tcd->image->comps;
2058 for (i=0;i<p_tcd->image->numcomps;++i) {
2059 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2060 l_remaining = l_img_comp->prec & 7; /* (%8) */
2061 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2067 if (l_size_comp == 3) {
2071 switch (l_size_comp) {
2074 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2075 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2077 if (l_img_comp->sgnd) {
2078 for (j=0;j<l_nb_elem;++j) {
2079 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2083 for (j=0;j<l_nb_elem;++j) {
2084 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2088 p_src = (OPJ_BYTE*) l_src_ptr;
2093 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2094 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2096 if (l_img_comp->sgnd) {
2097 for (j=0;j<l_nb_elem;++j) {
2098 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2102 for (j=0;j<l_nb_elem;++j) {
2103 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2107 p_src = (OPJ_BYTE*) l_src_ptr;
2112 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2113 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2115 for (j=0;j<l_nb_elem;++j) {
2116 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2119 p_src = (OPJ_BYTE*) l_src_ptr;