2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
35 /* ----------------------------------------------------------------------- */
38 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
39 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
41 fprintf(fd, "image {\n");
42 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
43 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
45 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
46 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
47 fprintf(fd, " tile {\n");
48 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
49 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
50 for (compno = 0; compno < tile->numcomps; compno++) {
51 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
52 fprintf(fd, " tilec {\n");
54 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
55 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
56 for (resno = 0; resno < tilec->numresolutions; resno++) {
57 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
58 fprintf(fd, "\n res {\n");
60 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
61 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
62 for (bandno = 0; bandno < res->numbands; bandno++) {
63 opj_tcd_band_t *band = &res->bands[bandno];
64 fprintf(fd, " band {\n");
66 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
67 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
68 for (precno = 0; precno < res->pw * res->ph; precno++) {
69 opj_tcd_precinct_t *prec = &band->precincts[precno];
70 fprintf(fd, " prec {\n");
72 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
73 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
75 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
76 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
77 fprintf(fd, " cblk {\n");
79 " x0=%d, y0=%d, x1=%d, y1=%d\n",
80 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
97 * Allocates memory for a decoding code block.
99 static opj_bool opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block);
102 * Deallocates the decoding data of the given precinct.
104 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
107 * Allocates memory for an encoding code block.
109 static opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block);
112 * Deallocates the encoding data of the given precinct.
114 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct);
118 Free the memory allocated for encoding
119 @param tcd TCD handle
121 static void opj_tcd_free_tile(opj_tcd_v2_t *tcd);
124 static opj_bool opj_tcd_t2_decode ( opj_tcd_v2_t *p_tcd,
125 OPJ_BYTE * p_src_data,
126 OPJ_UINT32 * p_data_read,
127 OPJ_UINT32 p_max_src_size,
128 opj_codestream_index_t *p_cstr_index );
130 static opj_bool opj_tcd_t1_decode (opj_tcd_v2_t *p_tcd);
132 static opj_bool opj_tcd_dwt_decode (opj_tcd_v2_t *p_tcd);
134 static opj_bool opj_tcd_mct_decode (opj_tcd_v2_t *p_tcd);
136 static opj_bool opj_tcd_dc_level_shift_decode (opj_tcd_v2_t *p_tcd);
139 static opj_bool opj_tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd );
141 static opj_bool opj_tcd_mct_encode ( opj_tcd_v2_t *p_tcd );
143 static opj_bool opj_tcd_dwt_encode ( opj_tcd_v2_t *p_tcd );
145 static opj_bool opj_tcd_t1_encode ( opj_tcd_v2_t *p_tcd );
147 static opj_bool opj_tcd_t2_encode ( opj_tcd_v2_t *p_tcd,
148 OPJ_BYTE * p_dest_data,
149 OPJ_UINT32 * p_data_written,
150 OPJ_UINT32 p_max_dest_size,
151 opj_codestream_info_t *p_cstr_info );
153 static opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd,
154 OPJ_BYTE * p_dest_data,
155 OPJ_UINT32 p_max_dest_size,
156 opj_codestream_info_t *p_cstr_info );
158 /* ----------------------------------------------------------------------- */
161 Create a new TCD handle
163 opj_tcd_v2_t* opj_tcd_create(opj_bool p_is_decoder)
165 opj_tcd_v2_t *l_tcd = 00;
167 /* create the tcd structure */
168 l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
172 memset(l_tcd,0,sizeof(opj_tcd_v2_t));
174 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
176 l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
177 if (!l_tcd->tcd_image) {
181 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
187 /* ----------------------------------------------------------------------- */
189 void opj_tcd_rateallocate_fixed(opj_tcd_v2_t *tcd) {
192 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
193 opj_tcd_makelayer_fixed(tcd, layno, 1);
198 void opj_tcd_makelayer( opj_tcd_v2_t *tcd,
203 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
206 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
208 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
210 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
211 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
213 for (resno = 0; resno < tilec->numresolutions; resno++) {
214 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
216 for (bandno = 0; bandno < res->numbands; bandno++) {
217 opj_tcd_band_v2_t *band = &res->bands[bandno];
219 for (precno = 0; precno < res->pw * res->ph; precno++) {
220 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
222 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
223 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
224 opj_tcd_layer_t *layer = &cblk->layers[layno];
228 cblk->numpassesinlayers = 0;
231 n = cblk->numpassesinlayers;
233 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
236 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
240 dd = pass->distortiondec;
242 dr = pass->rate - cblk->passes[n - 1].rate;
243 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
251 if (dd / dr >= thresh)
255 layer->numpasses = n - cblk->numpassesinlayers;
257 if (!layer->numpasses) {
262 if (cblk->numpassesinlayers == 0) {
263 layer->len = cblk->passes[n - 1].rate;
264 layer->data = cblk->data;
265 layer->disto = cblk->passes[n - 1].distortiondec;
267 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
268 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
269 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
272 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
275 cblk->numpassesinlayers = n;
283 void opj_tcd_makelayer_fixed(opj_tcd_v2_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
284 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
285 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
286 OPJ_INT32 matrice[10][10][3];
289 opj_cp_v2_t *cp = tcd->cp;
290 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
291 opj_tcp_v2_t *tcd_tcp = tcd->tcp;
293 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
294 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
296 for (i = 0; i < tcd_tcp->numlayers; i++) {
297 for (j = 0; j < tilec->numresolutions; j++) {
298 for (k = 0; k < 3; k++) {
300 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
301 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
306 for (resno = 0; resno < tilec->numresolutions; resno++) {
307 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
309 for (bandno = 0; bandno < res->numbands; bandno++) {
310 opj_tcd_band_v2_t *band = &res->bands[bandno];
312 for (precno = 0; precno < res->pw * res->ph; precno++) {
313 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
315 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
316 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
317 opj_tcd_layer_t *layer = &cblk->layers[layno];
319 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
321 /* Correction of the matrix of coefficient to include the IMSB information */
323 value = matrice[layno][resno][bandno];
330 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
331 if (imsb >= matrice[layno - 1][resno][bandno]) {
332 value -= (imsb - matrice[layno - 1][resno][bandno]);
340 cblk->numpassesinlayers = 0;
343 n = cblk->numpassesinlayers;
344 if (cblk->numpassesinlayers == 0) {
346 n = 3 * value - 2 + cblk->numpassesinlayers;
348 n = cblk->numpassesinlayers;
351 n = 3 * value + cblk->numpassesinlayers;
354 layer->numpasses = n - cblk->numpassesinlayers;
356 if (!layer->numpasses)
359 if (cblk->numpassesinlayers == 0) {
360 layer->len = cblk->passes[n - 1].rate;
361 layer->data = cblk->data;
363 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
364 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
368 cblk->numpassesinlayers = n;
376 opj_bool opj_tcd_rateallocate( opj_tcd_v2_t *tcd,
378 OPJ_UINT32 * p_data_written,
380 opj_codestream_info_t *cstr_info)
382 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
384 OPJ_FLOAT64 min, max;
385 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
386 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
387 OPJ_FLOAT64 maxSE = 0;
389 opj_cp_v2_t *cp = tcd->cp;
390 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
391 opj_tcp_v2_t *tcd_tcp = tcd->tcp;
396 tcd_tile->numpix = 0; /* fixed_quality */
398 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
399 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
402 for (resno = 0; resno < tilec->numresolutions; resno++) {
403 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
405 for (bandno = 0; bandno < res->numbands; bandno++) {
406 opj_tcd_band_v2_t *band = &res->bands[bandno];
408 for (precno = 0; precno < res->pw * res->ph; precno++) {
409 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
411 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
412 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
414 for (passno = 0; passno < cblk->totalpasses; passno++) {
415 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
417 OPJ_FLOAT64 dd, rdslope;
421 dd = pass->distortiondec;
423 dr = pass->rate - cblk->passes[passno - 1].rate;
424 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
442 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
443 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
449 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
450 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
451 * ((OPJ_FLOAT64)(tilec->numpix));
456 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
457 tile_info->numpix = tcd_tile->numpix;
458 tile_info->distotile = tcd_tile->distotile;
459 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
462 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
463 OPJ_FLOAT64 lo = min;
464 OPJ_FLOAT64 hi = max;
465 opj_bool success = OPJ_FALSE;
466 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
467 OPJ_FLOAT64 goodthresh = 0;
468 OPJ_FLOAT64 stable_thresh = 0;
470 OPJ_FLOAT64 distotarget; /* fixed_quality */
473 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
475 /* Don't try to find an optimal threshold but rather take everything not included yet, if
476 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
477 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
478 ==> possible to have some lossy layers and the last layer for sure lossless */
479 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))) {
480 opj_t2_v2_t*t2 = opj_t2_create(tcd->image, cp);
481 OPJ_FLOAT64 thresh = 0;
487 for (i = 0; i < 128; ++i) {
488 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
490 thresh = (lo + hi) / 2;
492 opj_tcd_makelayer(tcd, layno, thresh, 0);
494 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
495 if(cp->m_specific_param.m_enc.m_cinema){
496 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)) {
502 distoachieved = layno == 0 ?
503 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
505 if (distoachieved < distotarget) {
507 stable_thresh = thresh;
514 distoachieved = (layno == 0) ?
515 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
517 if (distoachieved < distotarget) {
519 stable_thresh = thresh;
525 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))
527 /* TODO: what to do with l ??? seek / tell ??? */
528 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
534 stable_thresh = thresh;
539 goodthresh = stable_thresh == 0? thresh : stable_thresh;
551 if(cstr_info) { /* Threshold for Marcela Index */
552 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
555 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
558 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
564 opj_bool opj_tcd_init( opj_tcd_v2_t *p_tcd,
565 opj_image_t * p_image,
568 OPJ_UINT32 l_tile_comp_size;
570 p_tcd->image = p_image;
573 p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
574 if (! p_tcd->tcd_image->tiles) {
577 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
579 l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
580 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
581 if (! p_tcd->tcd_image->tiles->comps ) {
584 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
586 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
587 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
593 Destroy a previously created TCD handle
595 void opj_tcd_destroy(opj_tcd_v2_t *tcd) {
597 opj_tcd_free_tile(tcd);
599 if (tcd->tcd_image) {
600 opj_free(tcd->tcd_image);
607 /* ----------------------------------------------------------------------- */
608 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
609 opj_bool FUNCTION ( opj_tcd_v2_t *p_tcd, \
610 OPJ_UINT32 p_tile_no \
613 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
614 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
615 opj_tcp_v2_t * l_tcp = 00; \
616 opj_cp_v2_t * l_cp = 00; \
617 opj_tcd_tile_v2_t * l_tile = 00; \
618 opj_tccp_t *l_tccp = 00; \
619 opj_tcd_tilecomp_v2_t *l_tilec = 00; \
620 opj_image_comp_t * l_image_comp = 00; \
621 opj_tcd_resolution_v2_t *l_res = 00; \
622 opj_tcd_band_v2_t *l_band = 00; \
623 opj_stepsize_t * l_step_size = 00; \
624 opj_tcd_precinct_v2_t *l_current_precinct = 00; \
625 TYPE* l_code_block = 00; \
626 opj_image_t *l_image = 00; \
628 OPJ_UINT32 l_level_no; \
629 OPJ_UINT32 l_pdx, l_pdy; \
631 OPJ_INT32 l_x0b, l_y0b; \
632 /* extent of precincts , top left, bottom right**/ \
633 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
634 /* number of precinct for a resolution */ \
635 OPJ_UINT32 l_nb_precincts; \
636 /* room needed to store l_nb_precinct precinct for a resolution */ \
637 OPJ_UINT32 l_nb_precinct_size; \
638 /* number of code blocks for a precinct*/ \
639 OPJ_UINT32 l_nb_code_blocks; \
640 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
641 OPJ_UINT32 l_nb_code_blocks_size; \
642 /* size of data for a tile */ \
643 OPJ_UINT32 l_data_size; \
646 l_tcp = &(l_cp->tcps[p_tile_no]); \
647 l_tile = p_tcd->tcd_image->tiles; \
648 l_tccp = l_tcp->tccps; \
649 l_tilec = l_tile->comps; \
650 l_image = p_tcd->image; \
651 l_image_comp = p_tcd->image->comps; \
653 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
654 q = p_tile_no / l_cp->tw; \
655 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
657 /* 4 borders of the tile rescale on the image if necessary */ \
658 l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \
659 l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \
660 l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \
661 l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \
662 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \
664 /*tile->numcomps = image->numcomps; */ \
665 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
666 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
668 /* border of each l_tile component (global) */ \
669 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \
670 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \
671 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \
672 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \
673 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \
675 l_data_size = (l_tilec->x1 - l_tilec->x0) \
676 * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 );\
677 l_tilec->numresolutions = l_tccp->numresolutions; \
678 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
679 l_tilec->minimum_num_resolutions = 1; \
682 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
683 - l_cp->m_specific_param.m_dec.m_reduce; \
686 if (l_tilec->data == 00) { \
687 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
688 if (! l_tilec->data ) { \
691 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
693 l_tilec->data_size = l_data_size; \
695 else if (l_data_size > l_tilec->data_size) { \
696 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
697 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \
698 fprintf(stderr, "Not enough memory to handle tile data\n"); \
700 opj_free(l_tilec->data); \
701 l_tilec->data = NULL; \
702 l_tilec->data_size = 0; \
705 l_tilec->data = new_data; \
706 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
707 l_tilec->data_size = l_data_size; \
710 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t); \
712 if (l_tilec->resolutions == 00) { \
713 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
714 if (! l_tilec->resolutions ) { \
717 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/ \
718 l_tilec->resolutions_size = l_data_size; \
719 memset(l_tilec->resolutions,0,l_data_size); \
721 else if (l_data_size > l_tilec->resolutions_size) { \
722 opj_tcd_resolution_v2_t* new_resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
723 if (! new_resolutions) { \
724 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \
725 fprintf(stderr, "Not enough memory to tile resolutions\n"); \
726 opj_free(l_tilec->resolutions); \
727 l_tilec->resolutions = NULL; \
728 l_tilec->resolutions_size = 0; \
731 l_tilec->resolutions = new_resolutions; \
732 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
733 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
734 l_tilec->resolutions_size = l_data_size; \
737 l_level_no = l_tilec->numresolutions - 1; \
738 l_res = l_tilec->resolutions; \
739 l_step_size = l_tccp->stepsizes; \
740 if (l_tccp->qmfbid == 0) { \
741 l_gain_ptr = &opj_dwt_getgain_real; \
744 l_gain_ptr = &opj_dwt_getgain; \
746 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
748 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
749 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
750 OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \
751 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
752 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
754 /* border for each resolution level (global) */ \
755 l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
756 l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
757 l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
758 l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
759 /*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);*/ \
760 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
761 l_pdx = l_tccp->prcw[resno]; \
762 l_pdy = l_tccp->prch[resno]; \
763 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
764 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
765 l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \
766 l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \
767 l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \
768 l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \
769 /*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 );*/ \
771 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
772 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
773 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
775 l_nb_precincts = l_res->pw * l_res->ph; \
776 l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t); \
778 tlcbgxstart = l_tl_prc_x_start; \
779 tlcbgystart = l_tl_prc_y_start; \
780 brcbgxend = l_br_prc_x_end; \
781 brcbgyend = l_br_prc_y_end; \
782 cbgwidthexpn = l_pdx; \
783 cbgheightexpn = l_pdy; \
784 l_res->numbands = 1; \
787 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \
788 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \
789 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \
790 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \
791 cbgwidthexpn = l_pdx - 1; \
792 cbgheightexpn = l_pdy - 1; \
793 l_res->numbands = 3; \
796 cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \
797 cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \
798 l_band = l_res->bands; \
800 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
802 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
805 l_band->bandno = 0 ; \
806 l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
807 l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
808 l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
809 l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
812 l_band->bandno = bandno + 1; \
813 /* x0b = 1 if bandno = 1 or 3 */ \
814 l_x0b = l_band->bandno&1; \
815 /* y0b = 1 if bandno = 2 or 3 */ \
816 l_y0b = (l_band->bandno)>>1; \
817 /* l_band border (global) */ \
818 l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
819 l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
820 l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
821 l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
824 /** avoid an if with storing function pointer */ \
825 l_gain = (*l_gain_ptr) (l_band->bandno); \
826 numbps = l_image_comp->prec + l_gain; \
827 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \
828 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */ \
830 if (! l_band->precincts) { \
831 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \
832 if (! l_band->precincts) { \
835 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size); */ \
836 memset(l_band->precincts,0,l_nb_precinct_size); \
837 l_band->precincts_data_size = l_nb_precinct_size; \
839 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
841 opj_tcd_precinct_v2_t * new_precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \
842 if (! new_precincts) { \
843 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \
844 fprintf(stderr, "Not enough memory to handle band precints\n"); \
845 opj_free(l_band->precincts); \
846 l_band->precincts = NULL; \
847 l_band->precincts_data_size = 0; \
850 l_band->precincts = new_precincts; \
851 /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_v2_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/ \
852 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \
853 l_band->precincts_data_size = l_nb_precinct_size; \
856 l_current_precinct = l_band->precincts; \
857 for (precno = 0; precno < l_nb_precincts; ++precno) { \
858 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
859 OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
860 OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \
861 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
862 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
863 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \
864 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \
866 /* precinct size (global) */ \
867 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
869 l_current_precinct->x0 = int_max(cbgxstart, l_band->x0); \
870 l_current_precinct->y0 = int_max(cbgystart, l_band->y0); \
871 l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \
872 l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \
873 /*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);*/ \
875 tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \
876 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
877 tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \
878 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
879 brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \
880 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
881 brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
882 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
883 l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \
884 l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
886 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
887 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
888 l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \
890 if (! l_current_precinct->cblks.ELEMENT) { \
891 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \
892 if (! l_current_precinct->cblks.ELEMENT ) { \
895 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/ \
897 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \
899 l_current_precinct->block_size = l_nb_code_blocks_size; \
901 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
902 TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
903 if (! new_ELEMENT) { \
904 opj_free(l_current_precinct->cblks.ELEMENT); \
905 l_current_precinct->cblks.ELEMENT = NULL; \
906 l_current_precinct->block_size = 0; \
907 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \
908 fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \
911 l_current_precinct->cblks.ELEMENT = new_ELEMENT; \
912 /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */\
914 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
916 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
918 l_current_precinct->block_size = l_nb_code_blocks_size; \
921 if (! l_current_precinct->incltree) { \
922 l_current_precinct->incltree = tgt_create(l_current_precinct->cw, \
923 l_current_precinct->ch); \
926 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree, \
927 l_current_precinct->cw, \
928 l_current_precinct->ch); \
931 if (! l_current_precinct->incltree) { \
932 fprintf(stderr, "WARNING: No incltree created.\n"); \
933 /*return OPJ_FALSE;*/ \
936 if (! l_current_precinct->imsbtree) { \
937 l_current_precinct->imsbtree = tgt_create( \
938 l_current_precinct->cw, \
939 l_current_precinct->ch); \
942 l_current_precinct->imsbtree = tgt_init( \
943 l_current_precinct->imsbtree, \
944 l_current_precinct->cw, \
945 l_current_precinct->ch); \
948 if (! l_current_precinct->imsbtree) { \
949 fprintf(stderr, "WARNING: No imsbtree created.\n"); \
950 /*return OPJ_FALSE;*/ \
953 l_code_block = l_current_precinct->cblks.ELEMENT; \
955 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
956 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
957 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
958 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
959 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
961 /* code-block size (global) */ \
962 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
963 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
964 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1); \
965 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1); \
967 if (! FUNCTION_ELEMENT(l_code_block)) { \
972 ++l_current_precinct; \
988 MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
989 MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
991 #undef MACRO_TCD_ALLOCATE
994 * Allocates memory for an encoding code block.
996 opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block)
998 if (! p_code_block->data) {
1000 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
1001 if(! p_code_block->data) {
1005 p_code_block->data[0] = 0;
1006 p_code_block->data+=1;
1008 /* no memset since data */
1009 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
1010 if (! p_code_block->layers) {
1014 p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t));
1015 if (! p_code_block->passes) {
1020 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1021 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t));
1027 * Allocates memory for a decoding code block.
1029 opj_bool opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
1031 OPJ_UINT32 l_seg_size;
1033 if (! p_code_block->data) {
1035 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
1036 if (! p_code_block->data) {
1039 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1041 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1042 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1043 if (! p_code_block->segs) {
1046 memset(p_code_block->segs,0,l_seg_size);
1047 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1049 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
1050 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1053 /*p_code_block->numsegs = 0; */
1058 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
1061 OPJ_UINT32 l_data_size = 0;
1062 opj_image_comp_t * l_img_comp = 00;
1063 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
1064 opj_tcd_resolution_v2_t * l_res = 00;
1065 OPJ_UINT32 l_size_comp, l_remaining;
1067 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1068 l_img_comp = p_tcd->image->comps;
1070 for (i=0;i<p_tcd->image->numcomps;++i) {
1071 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1072 l_remaining = l_img_comp->prec & 7; /* (%8) */
1078 if (l_size_comp == 3) {
1082 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1083 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
1091 opj_bool opj_tcd_encode_tile( opj_tcd_v2_t *p_tcd,
1092 OPJ_UINT32 p_tile_no,
1094 OPJ_UINT32 * p_data_written,
1095 OPJ_UINT32 p_max_length,
1096 opj_codestream_info_t *p_cstr_info)
1099 if (p_tcd->cur_tp_num == 0) {
1101 p_tcd->tcd_tileno = p_tile_no;
1102 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1104 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1106 OPJ_UINT32 l_num_packs = 0;
1108 opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1109 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1111 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1112 opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i];
1114 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
1115 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
1117 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1118 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
1119 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
1121 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(p_cstr_info->numcomps * p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
1125 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1126 /*---------------TILE-------------------*/
1127 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1130 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1132 /* FIXME _ProfStart(PGROUP_MCT); */
1133 if (! opj_tcd_mct_encode(p_tcd)) {
1136 /* FIXME _ProfStop(PGROUP_MCT); */
1138 /* FIXME _ProfStart(PGROUP_DWT); */
1139 if (! opj_tcd_dwt_encode(p_tcd)) {
1142 /* FIXME _ProfStop(PGROUP_DWT); */
1144 /* FIXME _ProfStart(PGROUP_T1); */
1145 if (! opj_tcd_t1_encode(p_tcd)) {
1148 /* FIXME _ProfStop(PGROUP_T1); */
1150 /* FIXME _ProfStart(PGROUP_RATE); */
1151 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1154 /* FIXME _ProfStop(PGROUP_RATE); */
1157 /*--------------TIER2------------------*/
1161 p_cstr_info->index_write = 1;
1163 /* FIXME _ProfStart(PGROUP_T2); */
1165 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1168 /* FIXME _ProfStop(PGROUP_T2); */
1170 /*---------------CLEAN-------------------*/
1175 opj_bool opj_tcd_decode_tile( opj_tcd_v2_t *p_tcd,
1177 OPJ_UINT32 p_max_length,
1178 OPJ_UINT32 p_tile_no,
1179 opj_codestream_index_t *p_cstr_index
1182 OPJ_UINT32 l_data_read;
1183 p_tcd->tcd_tileno = p_tile_no;
1184 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1186 #ifdef TODO_MSD /* FIXME */
1189 OPJ_UINT32 resno, compno, numprec = 0;
1190 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1191 opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
1192 opj_tccp_t *tccp = &tcp->tccps[compno];
1193 opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1194 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1195 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
1196 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1197 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1198 numprec += res_idx->pw * res_idx->ph;
1199 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1200 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1203 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1204 p_cstr_info->packno = 0;
1209 /*--------------TIER2------------------*/
1210 /* FIXME _ProfStart(PGROUP_T2); */
1213 (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1217 /* FIXME _ProfStop(PGROUP_T2); */
1219 /*------------------TIER1-----------------*/
1221 /* FIXME _ProfStart(PGROUP_T1); */
1223 (! opj_tcd_t1_decode(p_tcd))
1227 /* FIXME _ProfStop(PGROUP_T1); */
1229 /*----------------DWT---------------------*/
1231 /* FIXME _ProfStart(PGROUP_DWT); */
1233 (! opj_tcd_dwt_decode(p_tcd))
1237 /* FIXME _ProfStop(PGROUP_DWT); */
1239 /*----------------MCT-------------------*/
1240 /* FIXME _ProfStart(PGROUP_MCT); */
1242 (! opj_tcd_mct_decode(p_tcd))
1246 /* FIXME _ProfStop(PGROUP_MCT); */
1248 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1250 (! opj_tcd_dc_level_shift_decode(p_tcd))
1254 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1257 /*---------------TILE-------------------*/
1261 opj_bool opj_tcd_update_tile_data ( opj_tcd_v2_t *p_tcd,
1263 OPJ_UINT32 p_dest_length
1266 OPJ_UINT32 i,j,k,l_data_size = 0;
1267 opj_image_comp_t * l_img_comp = 00;
1268 opj_tcd_tilecomp_v2_t * l_tilec = 00;
1269 opj_tcd_resolution_v2_t * l_res;
1270 OPJ_UINT32 l_size_comp, l_remaining;
1271 OPJ_UINT32 l_stride, l_width,l_height;
1273 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1274 if (l_data_size > p_dest_length) {
1278 l_tilec = p_tcd->tcd_image->tiles->comps;
1279 l_img_comp = p_tcd->image->comps;
1281 for (i=0;i<p_tcd->image->numcomps;++i) {
1282 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1283 l_remaining = l_img_comp->prec & 7; /* (%8) */
1284 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1285 l_width = (l_res->x1 - l_res->x0);
1286 l_height = (l_res->y1 - l_res->y0);
1287 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
1293 if (l_size_comp == 3) {
1297 switch (l_size_comp)
1301 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1302 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1304 if (l_img_comp->sgnd) {
1305 for (j=0;j<l_height;++j) {
1306 for (k=0;k<l_width;++k) {
1307 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1309 l_src_ptr += l_stride;
1313 for (j=0;j<l_height;++j) {
1314 for (k=0;k<l_width;++k) {
1315 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
1317 l_src_ptr += l_stride;
1321 p_dest = (OPJ_BYTE *)l_dest_ptr;
1326 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1327 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1329 if (l_img_comp->sgnd) {
1330 for (j=0;j<l_height;++j) {
1331 for (k=0;k<l_width;++k) {
1332 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1334 l_src_ptr += l_stride;
1338 for (j=0;j<l_height;++j) {
1339 for (k=0;k<l_width;++k) {
1340 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
1342 l_src_ptr += l_stride;
1346 p_dest = (OPJ_BYTE*) l_dest_ptr;
1351 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1352 OPJ_INT32 * l_src_ptr = l_tilec->data;
1354 for (j=0;j<l_height;++j) {
1355 for (k=0;k<l_width;++k) {
1356 *(l_dest_ptr++) = (*(l_src_ptr++));
1358 l_src_ptr += l_stride;
1361 p_dest = (OPJ_BYTE*) l_dest_ptr;
1376 void opj_tcd_free_tile(opj_tcd_v2_t *p_tcd)
1378 OPJ_UINT32 compno, resno, bandno, precno;
1379 opj_tcd_tile_v2_t *l_tile = 00;
1380 opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
1381 opj_tcd_resolution_v2_t *l_res = 00;
1382 opj_tcd_band_v2_t *l_band = 00;
1383 opj_tcd_precinct_v2_t *l_precinct = 00;
1384 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1385 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
1391 if (! p_tcd->tcd_image) {
1395 if (p_tcd->m_is_decoder) {
1396 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1399 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1402 l_tile = p_tcd->tcd_image->tiles;
1407 l_tile_comp = l_tile->comps;
1409 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1410 l_res = l_tile_comp->resolutions;
1413 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
1414 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1415 l_band = l_res->bands;
1416 for (bandno = 0; bandno < 3; ++bandno) {
1417 l_precinct = l_band->precincts;
1420 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
1421 for (precno = 0; precno < l_nb_precincts; ++precno) {
1422 tgt_destroy(l_precinct->incltree);
1423 l_precinct->incltree = 00;
1424 tgt_destroy(l_precinct->imsbtree);
1425 l_precinct->imsbtree = 00;
1426 (*l_tcd_code_block_deallocate) (l_precinct);
1430 opj_free(l_band->precincts);
1431 l_band->precincts = 00;
1438 opj_free(l_tile_comp->resolutions);
1439 l_tile_comp->resolutions = 00;
1442 if (l_tile_comp->data) {
1443 opj_free(l_tile_comp->data);
1444 l_tile_comp->data = 00;
1449 opj_free(l_tile->comps);
1451 opj_free(p_tcd->tcd_image->tiles);
1452 p_tcd->tcd_image->tiles = 00;
1456 opj_bool opj_tcd_t2_decode (opj_tcd_v2_t *p_tcd,
1457 OPJ_BYTE * p_src_data,
1458 OPJ_UINT32 * p_data_read,
1459 OPJ_UINT32 p_max_src_size,
1460 opj_codestream_index_t *p_cstr_index
1465 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1470 if (! opj_t2_decode_packets(
1473 p_tcd->tcd_image->tiles,
1478 opj_t2_destroy(l_t2);
1482 opj_t2_destroy(l_t2);
1484 /*---------------CLEAN-------------------*/
1488 opj_bool opj_tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
1492 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1493 opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
1494 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1497 l_t1 = opj_t1_create();
1502 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1503 /* The +3 is headroom required by the vectorized DWT */
1504 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1505 opj_t1_destroy(l_t1);
1512 opj_t1_destroy(l_t1);
1518 opj_bool opj_tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
1521 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1522 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
1523 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1524 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1526 for (compno = 0; compno < l_tile->numcomps; compno++) {
1528 if (tcd->cp->reduce != 0) {
1529 tcd->image->comps[compno].resno_decoded =
1530 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1531 if (tcd->image->comps[compno].resno_decoded < 0)
1536 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1537 if(numres2decode > 0){
1540 if (l_tccp->qmfbid == 1) {
1541 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1546 if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
1558 opj_bool opj_tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
1560 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1561 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
1562 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
1563 OPJ_UINT32 l_samples,i;
1569 l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1571 if (l_tile->numcomps >= 3 ){
1572 if (l_tcp->mct == 2) {
1575 if (! l_tcp->m_mct_decoding_matrix) {
1579 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1584 for (i=0;i<l_tile->numcomps;++i) {
1585 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1589 if (! opj_mct_decode_custom(/* MCT data */
1590 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1591 /* size of components */
1595 /* nb of components (i.e. size of pData) */
1597 /* tells if the data is signed */
1598 p_tcd->image->comps->sgnd)) {
1606 if (l_tcp->tccps->qmfbid == 1) {
1607 opj_mct_decode( l_tile->comps[0].data,
1608 l_tile->comps[1].data,
1609 l_tile->comps[2].data,
1613 opj_mct_decode_real( (float*)l_tile->comps[0].data,
1614 (float*)l_tile->comps[1].data,
1615 (float*)l_tile->comps[2].data,
1621 /* FIXME need to use opj_event_msg_v2 function */
1622 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1629 opj_bool opj_tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
1632 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
1633 opj_tccp_t * l_tccp = 00;
1634 opj_image_comp_t * l_img_comp = 00;
1635 opj_tcd_resolution_v2_t* l_res = 00;
1636 opj_tcp_v2_t * l_tcp = 00;
1637 opj_tcd_tile_v2_t * l_tile;
1638 OPJ_UINT32 l_width,l_height,i,j;
1639 OPJ_INT32 * l_current_ptr;
1640 OPJ_INT32 l_min, l_max;
1641 OPJ_UINT32 l_stride;
1643 l_tile = p_tcd->tcd_image->tiles;
1644 l_tile_comp = l_tile->comps;
1646 l_tccp = p_tcd->tcp->tccps;
1647 l_img_comp = p_tcd->image->comps;
1649 for (compno = 0; compno < l_tile->numcomps; compno++) {
1650 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1651 l_width = (l_res->x1 - l_res->x0);
1652 l_height = (l_res->y1 - l_res->y0);
1653 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1655 if (l_img_comp->sgnd) {
1656 l_min = -(1 << (l_img_comp->prec - 1));
1657 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1661 l_max = (1 << l_img_comp->prec) - 1;
1664 l_current_ptr = l_tile_comp->data;
1666 if (l_tccp->qmfbid == 1) {
1667 for (j=0;j<l_height;++j) {
1668 for (i = 0; i < l_width; ++i) {
1669 *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1672 l_current_ptr += l_stride;
1676 for (j=0;j<l_height;++j) {
1677 for (i = 0; i < l_width; ++i) {
1678 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1679 *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1682 l_current_ptr += l_stride;
1697 * Deallocates the encoding data of the given precinct.
1699 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
1701 OPJ_UINT32 cblkno , l_nb_code_blocks;
1703 opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
1705 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1706 /*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);*/
1707 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1708 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 );*/
1711 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
1712 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1714 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1716 if (l_code_block->data) {
1717 opj_free(l_code_block->data);
1718 l_code_block->data = 00;
1721 if (l_code_block->segs) {
1722 opj_free(l_code_block->segs );
1723 l_code_block->segs = 00;
1729 opj_free(p_precinct->cblks.dec);
1730 p_precinct->cblks.dec = 00;
1735 * Deallocates the encoding data of the given precinct.
1737 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct)
1739 OPJ_UINT32 cblkno , l_nb_code_blocks;
1741 opj_tcd_cblk_enc_v2_t * l_code_block = p_precinct->cblks.enc;
1743 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1745 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1746 if (l_code_block->data) {
1747 opj_free(l_code_block->data-1);
1748 l_code_block->data = 00;
1751 if (l_code_block->layers) {
1752 opj_free(l_code_block->layers );
1753 l_code_block->layers = 00;
1756 if (l_code_block->passes) {
1757 opj_free(l_code_block->passes );
1758 l_code_block->passes = 00;
1763 opj_free(p_precinct->cblks.enc);
1765 p_precinct->cblks.enc = 00;
1769 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd )
1771 OPJ_UINT32 i,l_data_size = 0;
1772 opj_image_comp_t * l_img_comp = 00;
1773 opj_tcd_tilecomp_v2_t * l_tilec = 00;
1774 OPJ_UINT32 l_size_comp, l_remaining;
1776 l_tilec = p_tcd->tcd_image->tiles->comps;
1777 l_img_comp = p_tcd->image->comps;
1778 for (i=0;i<p_tcd->image->numcomps;++i) {
1779 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1780 l_remaining = l_img_comp->prec & 7; /* (%8) */
1786 if (l_size_comp == 3) {
1790 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
1798 opj_bool opj_tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd )
1801 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
1802 opj_tccp_t * l_tccp = 00;
1803 opj_image_comp_t * l_img_comp = 00;
1804 opj_tcp_v2_t * l_tcp = 00;
1805 opj_tcd_tile_v2_t * l_tile;
1806 OPJ_UINT32 l_nb_elem,i;
1807 OPJ_INT32 * l_current_ptr;
1809 l_tile = p_tcd->tcd_image->tiles;
1810 l_tile_comp = l_tile->comps;
1812 l_tccp = p_tcd->tcp->tccps;
1813 l_img_comp = p_tcd->image->comps;
1815 for (compno = 0; compno < l_tile->numcomps; compno++) {
1816 l_current_ptr = l_tile_comp->data;
1817 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1819 if (l_tccp->qmfbid == 1) {
1820 for (i = 0; i < l_nb_elem; ++i) {
1821 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1826 for (i = 0; i < l_nb_elem; ++i) {
1827 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1840 opj_bool opj_tcd_mct_encode ( opj_tcd_v2_t *p_tcd )
1842 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1843 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1844 OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1846 OPJ_BYTE ** l_data = 00;
1847 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
1849 if(!p_tcd->tcp->mct) {
1853 if (p_tcd->tcp->mct == 2) {
1854 if (! p_tcd->tcp->m_mct_coding_matrix) {
1858 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1863 for (i=0;i<l_tile->numcomps;++i) {
1864 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1868 if (! opj_mct_encode_custom(/* MCT data */
1869 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1870 /* size of components */
1874 /* nb of components (i.e. size of pData) */
1876 /* tells if the data is signed */
1877 p_tcd->image->comps->sgnd) )
1885 else if (l_tcp->tccps->qmfbid == 0) {
1886 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1889 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1895 opj_bool opj_tcd_dwt_encode ( opj_tcd_v2_t *p_tcd )
1897 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1898 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1899 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1902 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1903 if (l_tccp->qmfbid == 1) {
1904 if (! opj_dwt_encode(l_tile_comp)) {
1908 else if (l_tccp->qmfbid == 0) {
1909 if (! opj_dwt_encode_real(l_tile_comp)) {
1921 opj_bool opj_tcd_t1_encode ( opj_tcd_v2_t *p_tcd )
1924 const OPJ_FLOAT64 * l_mct_norms;
1925 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
1927 l_t1 = opj_t1_create();
1932 if (l_tcp->mct == 1) {
1933 /* irreversible encoding */
1934 if (l_tcp->tccps->qmfbid == 0) {
1935 l_mct_norms = opj_mct_get_mct_norms_real();
1938 l_mct_norms = opj_mct_get_mct_norms();
1942 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1945 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1946 opj_t1_destroy(l_t1);
1950 opj_t1_destroy(l_t1);
1955 opj_bool opj_tcd_t2_encode (opj_tcd_v2_t *p_tcd,
1956 OPJ_BYTE * p_dest_data,
1957 OPJ_UINT32 * p_data_written,
1958 OPJ_UINT32 p_max_dest_size,
1959 opj_codestream_info_t *p_cstr_info )
1963 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1968 if (! opj_t2_encode_packets(
1971 p_tcd->tcd_image->tiles,
1972 p_tcd->tcp->numlayers,
1982 opj_t2_destroy(l_t2);
1986 opj_t2_destroy(l_t2);
1988 /*---------------CLEAN-------------------*/
1993 opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd,
1994 OPJ_BYTE * p_dest_data,
1995 OPJ_UINT32 p_max_dest_size,
1996 opj_codestream_info_t *p_cstr_info )
1998 opj_cp_v2_t * l_cp = p_tcd->cp;
1999 OPJ_UINT32 l_nb_written = 0;
2002 p_cstr_info->index_write = 0;
2005 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2007 /* Normal Rate/distortion allocation */
2008 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2013 /* Fixed layer allocation */
2014 opj_tcd_rateallocate_fixed(p_tcd);
2021 opj_bool opj_tcd_copy_tile_data ( opj_tcd_v2_t *p_tcd,
2023 OPJ_UINT32 p_src_length )
2025 OPJ_UINT32 i,j,l_data_size = 0;
2026 opj_image_comp_t * l_img_comp = 00;
2027 opj_tcd_tilecomp_v2_t * l_tilec = 00;
2028 OPJ_UINT32 l_size_comp, l_remaining;
2029 OPJ_UINT32 l_nb_elem;
2031 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2032 if (l_data_size != p_src_length) {
2036 l_tilec = p_tcd->tcd_image->tiles->comps;
2037 l_img_comp = p_tcd->image->comps;
2038 for (i=0;i<p_tcd->image->numcomps;++i) {
2039 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2040 l_remaining = l_img_comp->prec & 7; /* (%8) */
2041 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
2047 if (l_size_comp == 3) {
2051 switch (l_size_comp) {
2054 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2055 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2057 if (l_img_comp->sgnd) {
2058 for (j=0;j<l_nb_elem;++j) {
2059 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2063 for (j=0;j<l_nb_elem;++j) {
2064 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2068 p_src = (OPJ_BYTE*) l_src_ptr;
2073 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2074 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2076 if (l_img_comp->sgnd) {
2077 for (j=0;j<l_nb_elem;++j) {
2078 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2082 for (j=0;j<l_nb_elem;++j) {
2083 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2087 p_src = (OPJ_BYTE*) l_src_ptr;
2092 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2093 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2095 for (j=0;j<l_nb_elem;++j) {
2096 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2099 p_src = (OPJ_BYTE*) l_src_ptr;