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 = t2_create_v2(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 (! t2_encode_packets_v2(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 (! t2_encode_packets_v2(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 /* ----------------------------------------------------------------------- */
609 * Initialize the tile coder and may reuse some meory.
610 * @param p_tcd TCD handle.
611 * @param p_image raw image.
612 * @param p_cp coding parameters.
613 * @param p_tile_no current tile index to encode.
615 * @return true if the encoding values could be set (false otherwise).
617 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
618 opj_bool FUNCTION ( opj_tcd_v2_t *p_tcd, \
619 OPJ_UINT32 p_tile_no \
622 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
623 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
624 opj_tcp_v2_t * l_tcp = 00; \
625 opj_cp_v2_t * l_cp = 00; \
626 opj_tcd_tile_v2_t * l_tile = 00; \
627 opj_tccp_t *l_tccp = 00; \
628 opj_tcd_tilecomp_v2_t *l_tilec = 00; \
629 opj_image_comp_t * l_image_comp = 00; \
630 opj_tcd_resolution_v2_t *l_res = 00; \
631 opj_tcd_band_v2_t *l_band = 00; \
632 opj_stepsize_t * l_step_size = 00; \
633 opj_tcd_precinct_v2_t *l_current_precinct = 00; \
634 TYPE* l_code_block = 00; \
635 opj_image_t *l_image = 00; \
637 OPJ_UINT32 l_level_no; \
638 OPJ_UINT32 l_pdx, l_pdy; \
640 OPJ_INT32 l_x0b, l_y0b; \
641 /* extent of precincts , top left, bottom right**/ \
642 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
643 /* number of precinct for a resolution */ \
644 OPJ_UINT32 l_nb_precincts; \
645 /* room needed to store l_nb_precinct precinct for a resolution */ \
646 OPJ_UINT32 l_nb_precinct_size; \
647 /* number of code blocks for a precinct*/ \
648 OPJ_UINT32 l_nb_code_blocks; \
649 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
650 OPJ_UINT32 l_nb_code_blocks_size; \
651 /* size of data for a tile */ \
652 OPJ_UINT32 l_data_size; \
655 l_tcp = &(l_cp->tcps[p_tile_no]); \
656 l_tile = p_tcd->tcd_image->tiles; \
657 l_tccp = l_tcp->tccps; \
658 l_tilec = l_tile->comps; \
659 l_image = p_tcd->image; \
660 l_image_comp = p_tcd->image->comps; \
662 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
663 q = p_tile_no / l_cp->tw; \
664 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
666 /* 4 borders of the tile rescale on the image if necessary */ \
667 l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \
668 l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \
669 l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \
670 l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \
671 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
673 /*tile->numcomps = image->numcomps; */ \
674 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
675 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
677 /* border of each l_tile component (global) */ \
678 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \
679 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \
680 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \
681 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \
682 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
684 l_data_size = (l_tilec->x1 - l_tilec->x0) \
685 * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \
686 l_tilec->numresolutions = l_tccp->numresolutions; \
687 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
688 l_tilec->minimum_num_resolutions = 1; \
691 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
692 - l_cp->m_specific_param.m_dec.m_reduce; \
695 if (l_tilec->data == 00) { \
696 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
697 if (! l_tilec->data ) { \
700 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
702 l_tilec->data_size = l_data_size; \
704 else if (l_data_size > l_tilec->data_size) { \
705 l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
706 if (! l_tilec->data) { \
709 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
710 l_tilec->data_size = l_data_size; \
713 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t); \
715 if (l_tilec->resolutions == 00) { \
716 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
717 if (! l_tilec->resolutions ) { \
720 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/ \
721 l_tilec->resolutions_size = l_data_size; \
722 memset(l_tilec->resolutions,0,l_data_size); \
724 else if (l_data_size > l_tilec->resolutions_size) { \
725 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
726 if (! l_tilec->resolutions) { \
729 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
730 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
731 l_tilec->resolutions_size = l_data_size; \
734 l_level_no = l_tilec->numresolutions - 1; \
735 l_res = l_tilec->resolutions; \
736 l_step_size = l_tccp->stepsizes; \
737 if (l_tccp->qmfbid == 0) { \
738 l_gain_ptr = &opj_dwt_getgain_real; \
741 l_gain_ptr = &opj_dwt_getgain; \
743 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
745 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
746 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
747 OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \
748 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
749 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
751 /* border for each resolution level (global) */ \
752 l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
753 l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
754 l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
755 l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
756 /*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);*/ \
757 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
758 l_pdx = l_tccp->prcw[resno]; \
759 l_pdy = l_tccp->prch[resno]; \
760 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
761 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
762 l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \
763 l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \
764 l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \
765 l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \
766 /*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 );*/ \
768 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
769 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
770 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
772 l_nb_precincts = l_res->pw * l_res->ph; \
773 l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t); \
775 tlcbgxstart = l_tl_prc_x_start; \
776 tlcbgystart = l_tl_prc_y_start; \
777 brcbgxend = l_br_prc_x_end; \
778 brcbgyend = l_br_prc_y_end; \
779 cbgwidthexpn = l_pdx; \
780 cbgheightexpn = l_pdy; \
781 l_res->numbands = 1; \
784 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \
785 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \
786 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \
787 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \
788 cbgwidthexpn = l_pdx - 1; \
789 cbgheightexpn = l_pdy - 1; \
790 l_res->numbands = 3; \
793 cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \
794 cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \
795 l_band = l_res->bands; \
797 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
799 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
802 l_band->bandno = 0 ; \
803 l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
804 l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
805 l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
806 l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
809 l_band->bandno = bandno + 1; \
810 /* x0b = 1 if bandno = 1 or 3 */ \
811 l_x0b = l_band->bandno&1; \
812 /* y0b = 1 if bandno = 2 or 3 */ \
813 l_y0b = (l_band->bandno)>>1; \
814 /* l_band border (global) */ \
815 l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
816 l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
817 l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
818 l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
821 /** avoid an if with storing function pointer */ \
822 l_gain = (*l_gain_ptr) (l_band->bandno); \
823 numbps = l_image_comp->prec + l_gain; \
824 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
825 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */\
827 if (! l_band->precincts) { \
828 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
829 if (! l_band->precincts) { \
832 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size); */ \
833 memset(l_band->precincts,0,l_nb_precinct_size); \
834 l_band->precincts_data_size = l_nb_precinct_size; \
836 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
838 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
839 if (! l_band->precincts) { \
842 /*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);*/\
843 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
844 l_band->precincts_data_size = l_nb_precinct_size; \
847 l_current_precinct = l_band->precincts; \
848 for (precno = 0; precno < l_nb_precincts; ++precno) { \
849 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
850 OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
851 OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \
852 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
853 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
854 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
855 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
857 /* precinct size (global) */ \
858 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
860 l_current_precinct->x0 = int_max(cbgxstart, l_band->x0); \
861 l_current_precinct->y0 = int_max(cbgystart, l_band->y0); \
862 l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \
863 l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \
864 /*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);*/ \
866 tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \
867 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
868 tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \
869 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
870 brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \
871 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
872 brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
873 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
874 l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \
875 l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
877 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
878 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
879 l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \
881 if (! l_current_precinct->cblks.ELEMENT) { \
882 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
883 if (! l_current_precinct->cblks.ELEMENT ) { \
886 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/ \
888 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
890 l_current_precinct->block_size = l_nb_code_blocks_size; \
892 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
893 l_current_precinct->cblks.ELEMENT = (TYPE*) \
894 opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
895 if (! l_current_precinct->cblks.ELEMENT ) { \
898 /*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); */ \
900 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
902 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
904 l_current_precinct->block_size = l_nb_code_blocks_size; \
907 if (! l_current_precinct->incltree) { \
908 l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw, \
909 l_current_precinct->ch); \
912 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree, \
913 l_current_precinct->cw, \
914 l_current_precinct->ch); \
917 if (! l_current_precinct->incltree) { \
918 fprintf(stderr, "WARNING: No incltree created.\n");\
919 /*return OPJ_FALSE;*/ \
922 if (! l_current_precinct->imsbtree) { \
923 l_current_precinct->imsbtree = tgt_create_v2( \
924 l_current_precinct->cw, \
925 l_current_precinct->ch);\
928 l_current_precinct->imsbtree = tgt_init( \
929 l_current_precinct->imsbtree, \
930 l_current_precinct->cw, \
931 l_current_precinct->ch); \
934 if (! l_current_precinct->imsbtree) { \
935 fprintf(stderr, "WARNING: No imsbtree created.\n");\
936 /*return OPJ_FALSE;*/ \
939 l_code_block = l_current_precinct->cblks.ELEMENT; \
941 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
942 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
943 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
944 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
945 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
947 /* code-block size (global) */ \
948 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
949 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
950 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1); \
951 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1); \
953 if (! FUNCTION_ELEMENT(l_code_block)) { \
958 ++l_current_precinct; \
974 MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
975 MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
977 #undef MACRO_TCD_ALLOCATE
980 * Allocates memory for an encoding code block.
982 opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block)
984 if (! p_code_block->data) {
986 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
987 if(! p_code_block->data) {
991 p_code_block->data[0] = 0;
992 p_code_block->data+=1;
994 /* no memset since data */
995 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
996 if (! p_code_block->layers) {
1000 p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t));
1001 if (! p_code_block->passes) {
1006 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1007 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t));
1013 * Allocates memory for a decoding code block.
1015 opj_bool opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
1017 OPJ_UINT32 l_seg_size;
1019 if (! p_code_block->data) {
1021 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
1022 if (! p_code_block->data) {
1025 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1027 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1028 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1029 if (! p_code_block->segs) {
1032 memset(p_code_block->segs,0,l_seg_size);
1033 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1035 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
1036 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1039 /*p_code_block->numsegs = 0; */
1044 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
1047 OPJ_UINT32 l_data_size = 0;
1048 opj_image_comp_t * l_img_comp = 00;
1049 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
1050 opj_tcd_resolution_v2_t * l_res = 00;
1051 OPJ_UINT32 l_size_comp, l_remaining;
1053 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1054 l_img_comp = p_tcd->image->comps;
1056 for (i=0;i<p_tcd->image->numcomps;++i) {
1057 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1058 l_remaining = l_img_comp->prec & 7; /* (%8) */
1064 if (l_size_comp == 3) {
1068 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1069 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
1077 opj_bool opj_tcd_encode_tile( opj_tcd_v2_t *p_tcd,
1078 OPJ_UINT32 p_tile_no,
1080 OPJ_UINT32 * p_data_written,
1081 OPJ_UINT32 p_max_length,
1082 opj_codestream_info_t *p_cstr_info)
1085 if (p_tcd->cur_tp_num == 0) {
1087 p_tcd->tcd_tileno = p_tile_no;
1088 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1090 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1092 OPJ_UINT32 l_num_packs = 0;
1094 opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1095 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1097 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1098 opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i];
1100 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
1101 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
1103 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1104 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
1105 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
1107 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));
1111 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1112 /*---------------TILE-------------------*/
1113 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1116 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1118 /* FIXME _ProfStart(PGROUP_MCT); */
1119 if (! opj_tcd_mct_encode(p_tcd)) {
1122 /* FIXME _ProfStop(PGROUP_MCT); */
1124 /* FIXME _ProfStart(PGROUP_DWT); */
1125 if (! opj_tcd_dwt_encode(p_tcd)) {
1128 /* FIXME _ProfStop(PGROUP_DWT); */
1130 /* FIXME _ProfStart(PGROUP_T1); */
1131 if (! opj_tcd_t1_encode(p_tcd)) {
1134 /* FIXME _ProfStop(PGROUP_T1); */
1136 /* FIXME _ProfStart(PGROUP_RATE); */
1137 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1140 /* FIXME _ProfStop(PGROUP_RATE); */
1143 /*--------------TIER2------------------*/
1147 p_cstr_info->index_write = 1;
1149 /* FIXME _ProfStart(PGROUP_T2); */
1151 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1154 /* FIXME _ProfStop(PGROUP_T2); */
1156 /*---------------CLEAN-------------------*/
1161 opj_bool opj_tcd_decode_tile( opj_tcd_v2_t *p_tcd,
1163 OPJ_UINT32 p_max_length,
1164 OPJ_UINT32 p_tile_no,
1165 opj_codestream_index_t *p_cstr_index
1168 OPJ_UINT32 l_data_read;
1169 p_tcd->tcd_tileno = p_tile_no;
1170 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1172 #ifdef TODO_MSD /* FIXME */
1175 OPJ_UINT32 resno, compno, numprec = 0;
1176 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1177 opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
1178 opj_tccp_t *tccp = &tcp->tccps[compno];
1179 opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1180 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1181 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
1182 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1183 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1184 numprec += res_idx->pw * res_idx->ph;
1185 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1186 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1189 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1190 p_cstr_info->packno = 0;
1195 /*--------------TIER2------------------*/
1196 /* FIXME _ProfStart(PGROUP_T2); */
1199 (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1203 /* FIXME _ProfStop(PGROUP_T2); */
1205 /*------------------TIER1-----------------*/
1207 /* FIXME _ProfStart(PGROUP_T1); */
1209 (! opj_tcd_t1_decode(p_tcd))
1213 /* FIXME _ProfStop(PGROUP_T1); */
1215 /*----------------DWT---------------------*/
1217 /* FIXME _ProfStart(PGROUP_DWT); */
1219 (! opj_tcd_dwt_decode(p_tcd))
1223 /* FIXME _ProfStop(PGROUP_DWT); */
1225 /*----------------MCT-------------------*/
1226 /* FIXME _ProfStart(PGROUP_MCT); */
1228 (! opj_tcd_mct_decode(p_tcd))
1232 /* FIXME _ProfStop(PGROUP_MCT); */
1234 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1236 (! opj_tcd_dc_level_shift_decode(p_tcd))
1240 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1243 /*---------------TILE-------------------*/
1247 opj_bool opj_tcd_update_tile_data ( opj_tcd_v2_t *p_tcd,
1249 OPJ_UINT32 p_dest_length
1252 OPJ_UINT32 i,j,k,l_data_size = 0;
1253 opj_image_comp_t * l_img_comp = 00;
1254 opj_tcd_tilecomp_v2_t * l_tilec = 00;
1255 opj_tcd_resolution_v2_t * l_res;
1256 OPJ_UINT32 l_size_comp, l_remaining;
1257 OPJ_UINT32 l_stride, l_width,l_height;
1259 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1260 if (l_data_size > p_dest_length) {
1264 l_tilec = p_tcd->tcd_image->tiles->comps;
1265 l_img_comp = p_tcd->image->comps;
1267 for (i=0;i<p_tcd->image->numcomps;++i) {
1268 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1269 l_remaining = l_img_comp->prec & 7; /* (%8) */
1270 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1271 l_width = (l_res->x1 - l_res->x0);
1272 l_height = (l_res->y1 - l_res->y0);
1273 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
1279 if (l_size_comp == 3) {
1283 switch (l_size_comp)
1287 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1288 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1290 if (l_img_comp->sgnd) {
1291 for (j=0;j<l_height;++j) {
1292 for (k=0;k<l_width;++k) {
1293 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1295 l_src_ptr += l_stride;
1299 for (j=0;j<l_height;++j) {
1300 for (k=0;k<l_width;++k) {
1301 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
1303 l_src_ptr += l_stride;
1307 p_dest = (OPJ_BYTE *)l_dest_ptr;
1312 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1313 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1315 if (l_img_comp->sgnd) {
1316 for (j=0;j<l_height;++j) {
1317 for (k=0;k<l_width;++k) {
1318 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1320 l_src_ptr += l_stride;
1324 for (j=0;j<l_height;++j) {
1325 for (k=0;k<l_width;++k) {
1326 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
1328 l_src_ptr += l_stride;
1332 p_dest = (OPJ_BYTE*) l_dest_ptr;
1337 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1338 OPJ_INT32 * l_src_ptr = l_tilec->data;
1340 for (j=0;j<l_height;++j) {
1341 for (k=0;k<l_width;++k) {
1342 *(l_dest_ptr++) = (*(l_src_ptr++));
1344 l_src_ptr += l_stride;
1347 p_dest = (OPJ_BYTE*) l_dest_ptr;
1362 void opj_tcd_free_tile(opj_tcd_v2_t *p_tcd)
1364 OPJ_UINT32 compno, resno, bandno, precno;
1365 opj_tcd_tile_v2_t *l_tile = 00;
1366 opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
1367 opj_tcd_resolution_v2_t *l_res = 00;
1368 opj_tcd_band_v2_t *l_band = 00;
1369 opj_tcd_precinct_v2_t *l_precinct = 00;
1370 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1371 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
1377 if (! p_tcd->tcd_image) {
1381 if (p_tcd->m_is_decoder) {
1382 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1385 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1388 l_tile = p_tcd->tcd_image->tiles;
1393 l_tile_comp = l_tile->comps;
1395 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1396 l_res = l_tile_comp->resolutions;
1399 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
1400 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1401 l_band = l_res->bands;
1402 for (bandno = 0; bandno < 3; ++bandno) {
1403 l_precinct = l_band->precincts;
1406 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
1407 for (precno = 0; precno < l_nb_precincts; ++precno) {
1408 tgt_destroy(l_precinct->incltree);
1409 l_precinct->incltree = 00;
1410 tgt_destroy(l_precinct->imsbtree);
1411 l_precinct->imsbtree = 00;
1412 (*l_tcd_code_block_deallocate) (l_precinct);
1416 opj_free(l_band->precincts);
1417 l_band->precincts = 00;
1424 opj_free(l_tile_comp->resolutions);
1425 l_tile_comp->resolutions = 00;
1428 if (l_tile_comp->data) {
1429 opj_free(l_tile_comp->data);
1430 l_tile_comp->data = 00;
1435 opj_free(l_tile->comps);
1437 opj_free(p_tcd->tcd_image->tiles);
1438 p_tcd->tcd_image->tiles = 00;
1442 opj_bool opj_tcd_t2_decode (opj_tcd_v2_t *p_tcd,
1443 OPJ_BYTE * p_src_data,
1444 OPJ_UINT32 * p_data_read,
1445 OPJ_UINT32 p_max_src_size,
1446 opj_codestream_index_t *p_cstr_index
1451 l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
1456 if (! t2_decode_packets_v2(
1459 p_tcd->tcd_image->tiles,
1464 t2_destroy_v2(l_t2);
1468 t2_destroy_v2(l_t2);
1470 /*---------------CLEAN-------------------*/
1474 opj_bool opj_tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
1478 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1479 opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
1480 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1483 l_t1 = opj_t1_create();
1488 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1489 /* The +3 is headroom required by the vectorized DWT */
1490 opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp);
1495 opj_t1_destroy(l_t1);
1501 opj_bool opj_tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
1504 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1505 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
1506 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1507 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1509 for (compno = 0; compno < l_tile->numcomps; compno++) {
1511 if (tcd->cp->reduce != 0) {
1512 tcd->image->comps[compno].resno_decoded =
1513 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1514 if (tcd->image->comps[compno].resno_decoded < 0)
1519 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1520 if(numres2decode > 0){
1523 if (l_tccp->qmfbid == 1) {
1524 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1529 if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
1541 opj_bool opj_tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
1543 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1544 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
1545 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
1546 OPJ_UINT32 l_samples,i;
1552 l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1554 if (l_tile->numcomps >= 3 ){
1555 if (l_tcp->mct == 2) {
1558 if (! l_tcp->m_mct_decoding_matrix) {
1562 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1567 for (i=0;i<l_tile->numcomps;++i) {
1568 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1572 if (! mct_decode_custom(/* MCT data */
1573 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1574 /* size of components */
1578 /* nb of components (i.e. size of pData) */
1580 /* tells if the data is signed */
1581 p_tcd->image->comps->sgnd)) {
1589 if (l_tcp->tccps->qmfbid == 1) {
1590 mct_decode( l_tile->comps[0].data,
1591 l_tile->comps[1].data,
1592 l_tile->comps[2].data,
1596 mct_decode_real( (float*)l_tile->comps[0].data,
1597 (float*)l_tile->comps[1].data,
1598 (float*)l_tile->comps[2].data,
1604 /* FIXME need to use opj_event_msg_v2 function */
1605 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1612 opj_bool opj_tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
1615 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
1616 opj_tccp_t * l_tccp = 00;
1617 opj_image_comp_t * l_img_comp = 00;
1618 opj_tcd_resolution_v2_t* l_res = 00;
1619 opj_tcp_v2_t * l_tcp = 00;
1620 opj_tcd_tile_v2_t * l_tile;
1621 OPJ_UINT32 l_width,l_height,i,j;
1622 OPJ_INT32 * l_current_ptr;
1623 OPJ_INT32 l_min, l_max;
1624 OPJ_UINT32 l_stride;
1626 l_tile = p_tcd->tcd_image->tiles;
1627 l_tile_comp = l_tile->comps;
1629 l_tccp = p_tcd->tcp->tccps;
1630 l_img_comp = p_tcd->image->comps;
1632 for (compno = 0; compno < l_tile->numcomps; compno++) {
1633 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1634 l_width = (l_res->x1 - l_res->x0);
1635 l_height = (l_res->y1 - l_res->y0);
1636 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1638 if (l_img_comp->sgnd) {
1639 l_min = -(1 << (l_img_comp->prec - 1));
1640 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1644 l_max = (1 << l_img_comp->prec) - 1;
1647 l_current_ptr = l_tile_comp->data;
1649 if (l_tccp->qmfbid == 1) {
1650 for (j=0;j<l_height;++j) {
1651 for (i = 0; i < l_width; ++i) {
1652 *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1655 l_current_ptr += l_stride;
1659 for (j=0;j<l_height;++j) {
1660 for (i = 0; i < l_width; ++i) {
1661 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1662 *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1665 l_current_ptr += l_stride;
1680 * Deallocates the encoding data of the given precinct.
1682 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
1684 OPJ_UINT32 cblkno , l_nb_code_blocks;
1686 opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
1688 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1689 /*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);*/
1690 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1691 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 );*/
1694 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
1695 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1697 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1699 if (l_code_block->data) {
1700 opj_free(l_code_block->data);
1701 l_code_block->data = 00;
1704 if (l_code_block->segs) {
1705 opj_free(l_code_block->segs );
1706 l_code_block->segs = 00;
1712 opj_free(p_precinct->cblks.dec);
1713 p_precinct->cblks.dec = 00;
1718 * Deallocates the encoding data of the given precinct.
1720 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct)
1722 OPJ_UINT32 cblkno , l_nb_code_blocks;
1724 opj_tcd_cblk_enc_v2_t * l_code_block = p_precinct->cblks.enc;
1726 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1728 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1729 if (l_code_block->data) {
1730 opj_free(l_code_block->data-1);
1731 l_code_block->data = 00;
1734 if (l_code_block->layers) {
1735 opj_free(l_code_block->layers );
1736 l_code_block->layers = 00;
1739 if (l_code_block->passes) {
1740 opj_free(l_code_block->passes );
1741 l_code_block->passes = 00;
1746 opj_free(p_precinct->cblks.enc);
1748 p_precinct->cblks.enc = 00;
1752 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd )
1754 OPJ_UINT32 i,l_data_size = 0;
1755 opj_image_comp_t * l_img_comp = 00;
1756 opj_tcd_tilecomp_v2_t * l_tilec = 00;
1757 OPJ_UINT32 l_size_comp, l_remaining;
1759 l_tilec = p_tcd->tcd_image->tiles->comps;
1760 l_img_comp = p_tcd->image->comps;
1761 for (i=0;i<p_tcd->image->numcomps;++i) {
1762 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1763 l_remaining = l_img_comp->prec & 7; /* (%8) */
1769 if (l_size_comp == 3) {
1773 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
1781 opj_bool opj_tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd )
1784 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
1785 opj_tccp_t * l_tccp = 00;
1786 opj_image_comp_t * l_img_comp = 00;
1787 opj_tcp_v2_t * l_tcp = 00;
1788 opj_tcd_tile_v2_t * l_tile;
1789 OPJ_UINT32 l_nb_elem,i;
1790 OPJ_INT32 * l_current_ptr;
1792 l_tile = p_tcd->tcd_image->tiles;
1793 l_tile_comp = l_tile->comps;
1795 l_tccp = p_tcd->tcp->tccps;
1796 l_img_comp = p_tcd->image->comps;
1798 for (compno = 0; compno < l_tile->numcomps; compno++) {
1799 l_current_ptr = l_tile_comp->data;
1800 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1802 if (l_tccp->qmfbid == 1) {
1803 for (i = 0; i < l_nb_elem; ++i) {
1804 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1809 for (i = 0; i < l_nb_elem; ++i) {
1810 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1823 opj_bool opj_tcd_mct_encode ( opj_tcd_v2_t *p_tcd )
1825 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1826 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1827 OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1829 OPJ_BYTE ** l_data = 00;
1830 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
1832 if(!p_tcd->tcp->mct) {
1836 if (p_tcd->tcp->mct == 2) {
1837 if (! p_tcd->tcp->m_mct_coding_matrix) {
1841 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1846 for (i=0;i<l_tile->numcomps;++i) {
1847 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1851 if (! mct_encode_custom(/* MCT data */
1852 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1853 /* size of components */
1857 /* nb of components (i.e. size of pData) */
1859 /* tells if the data is signed */
1860 p_tcd->image->comps->sgnd) )
1868 else if (l_tcp->tccps->qmfbid == 0) {
1869 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1872 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1878 opj_bool opj_tcd_dwt_encode ( opj_tcd_v2_t *p_tcd )
1880 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
1881 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1882 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1885 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1886 if (l_tccp->qmfbid == 1) {
1887 if (! opj_dwt_encode(l_tile_comp)) {
1891 else if (l_tccp->qmfbid == 0) {
1892 if (! opj_dwt_encode_real(l_tile_comp)) {
1904 opj_bool opj_tcd_t1_encode ( opj_tcd_v2_t *p_tcd )
1907 const OPJ_FLOAT64 * l_mct_norms;
1908 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
1910 l_t1 = opj_t1_create();
1915 if (l_tcp->mct == 1) {
1916 /* irreversible encoding */
1917 if (l_tcp->tccps->qmfbid == 0) {
1918 l_mct_norms = get_mct_norms_real();
1921 l_mct_norms = get_mct_norms();
1925 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1928 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1929 opj_t1_destroy(l_t1);
1933 opj_t1_destroy(l_t1);
1938 opj_bool opj_tcd_t2_encode (opj_tcd_v2_t *p_tcd,
1939 OPJ_BYTE * p_dest_data,
1940 OPJ_UINT32 * p_data_written,
1941 OPJ_UINT32 p_max_dest_size,
1942 opj_codestream_info_t *p_cstr_info )
1946 l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
1951 if (! t2_encode_packets_v2(
1954 p_tcd->tcd_image->tiles,
1955 p_tcd->tcp->numlayers,
1965 t2_destroy_v2(l_t2);
1969 t2_destroy_v2(l_t2);
1971 /*---------------CLEAN-------------------*/
1976 opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd,
1977 OPJ_BYTE * p_dest_data,
1978 OPJ_UINT32 p_max_dest_size,
1979 opj_codestream_info_t *p_cstr_info )
1981 opj_cp_v2_t * l_cp = p_tcd->cp;
1982 OPJ_UINT32 l_nb_written = 0;
1985 p_cstr_info->index_write = 0;
1988 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
1990 /* Normal Rate/distortion allocation */
1991 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
1996 /* Fixed layer allocation */
1997 opj_tcd_rateallocate_fixed(p_tcd);
2004 opj_bool opj_tcd_copy_tile_data ( opj_tcd_v2_t *p_tcd,
2006 OPJ_UINT32 p_src_length )
2008 OPJ_UINT32 i,j,l_data_size = 0;
2009 opj_image_comp_t * l_img_comp = 00;
2010 opj_tcd_tilecomp_v2_t * l_tilec = 00;
2011 OPJ_UINT32 l_size_comp, l_remaining;
2012 OPJ_UINT32 l_nb_elem;
2014 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2015 if (l_data_size != p_src_length) {
2019 l_tilec = p_tcd->tcd_image->tiles->comps;
2020 l_img_comp = p_tcd->image->comps;
2021 for (i=0;i<p_tcd->image->numcomps;++i) {
2022 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2023 l_remaining = l_img_comp->prec & 7; /* (%8) */
2024 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
2030 if (l_size_comp == 3) {
2034 switch (l_size_comp) {
2037 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2038 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2040 if (l_img_comp->sgnd) {
2041 for (j=0;j<l_nb_elem;++j) {
2042 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2046 for (j=0;j<l_nb_elem;++j) {
2047 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2051 p_src = (OPJ_BYTE*) l_src_ptr;
2056 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2057 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2059 if (l_img_comp->sgnd) {
2060 for (j=0;j<l_nb_elem;++j) {
2061 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2065 for (j=0;j<l_nb_elem;++j) {
2066 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2070 p_src = (OPJ_BYTE*) l_src_ptr;
2075 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2076 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2078 for (j=0;j<l_nb_elem;++j) {
2079 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2082 p_src = (OPJ_BYTE*) l_src_ptr;