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 /* ----------------------------------------------------------------------- */
39 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
40 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
42 fprintf(fd, "image {\n");
43 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
44 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
46 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
47 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
48 fprintf(fd, " tile {\n");
49 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
50 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
51 for (compno = 0; compno < tile->numcomps; compno++) {
52 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
53 fprintf(fd, " tilec {\n");
55 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
56 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
57 for (resno = 0; resno < tilec->numresolutions; resno++) {
58 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
59 fprintf(fd, "\n res {\n");
61 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
62 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
63 for (bandno = 0; bandno < res->numbands; bandno++) {
64 opj_tcd_band_t *band = &res->bands[bandno];
65 fprintf(fd, " band {\n");
67 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
68 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
69 for (precno = 0; precno < res->pw * res->ph; precno++) {
70 opj_tcd_precinct_t *prec = &band->precincts[precno];
71 fprintf(fd, " prec {\n");
73 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
74 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
76 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
77 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
78 fprintf(fd, " cblk {\n");
80 " x0=%d, y0=%d, x1=%d, y1=%d\n",
81 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
99 * Allocates memory for a decoding code block.
101 static opj_bool opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
104 * Deallocates the decoding data of the given precinct.
106 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
109 * Allocates memory for an encoding code block.
111 static opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
114 * Deallocates the encoding data of the given precinct.
116 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
120 Free the memory allocated for encoding
121 @param tcd TCD handle
123 static void opj_tcd_free_tile(opj_tcd_t *tcd);
126 static opj_bool opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
127 OPJ_BYTE * p_src_data,
128 OPJ_UINT32 * p_data_read,
129 OPJ_UINT32 p_max_src_size,
130 opj_codestream_index_t *p_cstr_index );
132 static opj_bool opj_tcd_t1_decode (opj_tcd_t *p_tcd);
134 static opj_bool opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
136 static opj_bool opj_tcd_mct_decode (opj_tcd_t *p_tcd);
138 static opj_bool opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
141 static opj_bool opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
143 static opj_bool opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
145 static opj_bool opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
147 static opj_bool opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
149 static opj_bool opj_tcd_t2_encode ( opj_tcd_t *p_tcd,
150 OPJ_BYTE * p_dest_data,
151 OPJ_UINT32 * p_data_written,
152 OPJ_UINT32 p_max_dest_size,
153 opj_codestream_info_t *p_cstr_info );
155 static opj_bool opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
156 OPJ_BYTE * p_dest_data,
157 OPJ_UINT32 p_max_dest_size,
158 opj_codestream_info_t *p_cstr_info );
160 /* ----------------------------------------------------------------------- */
163 Create a new TCD handle
165 opj_tcd_t* opj_tcd_create(opj_bool p_is_decoder)
167 opj_tcd_t *l_tcd = 00;
169 /* create the tcd structure */
170 l_tcd = (opj_tcd_t*) opj_malloc(sizeof(opj_tcd_t));
174 memset(l_tcd,0,sizeof(opj_tcd_t));
176 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
178 l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
179 if (!l_tcd->tcd_image) {
183 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
189 /* ----------------------------------------------------------------------- */
191 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
194 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
195 opj_tcd_makelayer_fixed(tcd, layno, 1);
200 void opj_tcd_makelayer( opj_tcd_t *tcd,
205 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
208 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
210 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
212 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
213 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
215 for (resno = 0; resno < tilec->numresolutions; resno++) {
216 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
218 for (bandno = 0; bandno < res->numbands; bandno++) {
219 opj_tcd_band_t *band = &res->bands[bandno];
221 for (precno = 0; precno < res->pw * res->ph; precno++) {
222 opj_tcd_precinct_t *prc = &band->precincts[precno];
224 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
225 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
226 opj_tcd_layer_t *layer = &cblk->layers[layno];
230 cblk->numpassesinlayers = 0;
233 n = cblk->numpassesinlayers;
235 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
238 opj_tcd_pass_t *pass = &cblk->passes[passno];
242 dd = pass->distortiondec;
244 dr = pass->rate - cblk->passes[n - 1].rate;
245 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
253 if (dd / dr >= thresh)
257 layer->numpasses = n - cblk->numpassesinlayers;
259 if (!layer->numpasses) {
264 if (cblk->numpassesinlayers == 0) {
265 layer->len = cblk->passes[n - 1].rate;
266 layer->data = cblk->data;
267 layer->disto = cblk->passes[n - 1].distortiondec;
269 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
270 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
271 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
274 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
277 cblk->numpassesinlayers = n;
285 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
286 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
287 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
288 OPJ_INT32 matrice[10][10][3];
291 opj_cp_t *cp = tcd->cp;
292 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
293 opj_tcp_t *tcd_tcp = tcd->tcp;
295 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
296 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
298 for (i = 0; i < tcd_tcp->numlayers; i++) {
299 for (j = 0; j < tilec->numresolutions; j++) {
300 for (k = 0; k < 3; k++) {
302 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
303 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
308 for (resno = 0; resno < tilec->numresolutions; resno++) {
309 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
311 for (bandno = 0; bandno < res->numbands; bandno++) {
312 opj_tcd_band_t *band = &res->bands[bandno];
314 for (precno = 0; precno < res->pw * res->ph; precno++) {
315 opj_tcd_precinct_t *prc = &band->precincts[precno];
317 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
318 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
319 opj_tcd_layer_t *layer = &cblk->layers[layno];
321 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
323 /* Correction of the matrix of coefficient to include the IMSB information */
325 value = matrice[layno][resno][bandno];
332 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
333 if (imsb >= matrice[layno - 1][resno][bandno]) {
334 value -= (imsb - matrice[layno - 1][resno][bandno]);
342 cblk->numpassesinlayers = 0;
345 n = cblk->numpassesinlayers;
346 if (cblk->numpassesinlayers == 0) {
348 n = 3 * value - 2 + cblk->numpassesinlayers;
350 n = cblk->numpassesinlayers;
353 n = 3 * value + cblk->numpassesinlayers;
356 layer->numpasses = n - cblk->numpassesinlayers;
358 if (!layer->numpasses)
361 if (cblk->numpassesinlayers == 0) {
362 layer->len = cblk->passes[n - 1].rate;
363 layer->data = cblk->data;
365 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
366 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
370 cblk->numpassesinlayers = n;
378 opj_bool opj_tcd_rateallocate( opj_tcd_t *tcd,
380 OPJ_UINT32 * p_data_written,
382 opj_codestream_info_t *cstr_info)
384 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
386 OPJ_FLOAT64 min, max;
387 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
388 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
389 OPJ_FLOAT64 maxSE = 0;
391 opj_cp_t *cp = tcd->cp;
392 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
393 opj_tcp_t *tcd_tcp = tcd->tcp;
398 tcd_tile->numpix = 0; /* fixed_quality */
400 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
401 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
404 for (resno = 0; resno < tilec->numresolutions; resno++) {
405 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
407 for (bandno = 0; bandno < res->numbands; bandno++) {
408 opj_tcd_band_t *band = &res->bands[bandno];
410 for (precno = 0; precno < res->pw * res->ph; precno++) {
411 opj_tcd_precinct_t *prc = &band->precincts[precno];
413 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
414 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
416 for (passno = 0; passno < cblk->totalpasses; passno++) {
417 opj_tcd_pass_t *pass = &cblk->passes[passno];
419 OPJ_FLOAT64 dd, rdslope;
423 dd = pass->distortiondec;
425 dr = pass->rate - cblk->passes[passno - 1].rate;
426 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
444 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
445 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
451 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
452 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
453 * ((OPJ_FLOAT64)(tilec->numpix));
458 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
459 tile_info->numpix = tcd_tile->numpix;
460 tile_info->distotile = tcd_tile->distotile;
461 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
464 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
465 OPJ_FLOAT64 lo = min;
466 OPJ_FLOAT64 hi = max;
467 opj_bool success = OPJ_FALSE;
468 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
469 OPJ_FLOAT64 goodthresh = 0;
470 OPJ_FLOAT64 stable_thresh = 0;
472 OPJ_FLOAT64 distotarget; /* fixed_quality */
475 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
477 /* Don't try to find an optimal threshold but rather take everything not included yet, if
478 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
479 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
480 ==> possible to have some lossy layers and the last layer for sure lossless */
481 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))) {
482 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
483 OPJ_FLOAT64 thresh = 0;
489 for (i = 0; i < 128; ++i) {
490 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
492 thresh = (lo + hi) / 2;
494 opj_tcd_makelayer(tcd, layno, thresh, 0);
496 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
497 if(cp->m_specific_param.m_enc.m_cinema){
498 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)) {
504 distoachieved = layno == 0 ?
505 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
507 if (distoachieved < distotarget) {
509 stable_thresh = thresh;
516 distoachieved = (layno == 0) ?
517 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
519 if (distoachieved < distotarget) {
521 stable_thresh = thresh;
527 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))
529 /* TODO: what to do with l ??? seek / tell ??? */
530 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
536 stable_thresh = thresh;
541 goodthresh = stable_thresh == 0? thresh : stable_thresh;
553 if(cstr_info) { /* Threshold for Marcela Index */
554 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
557 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
560 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
566 opj_bool opj_tcd_init( opj_tcd_t *p_tcd,
567 opj_image_t * p_image,
570 OPJ_UINT32 l_tile_comp_size;
572 p_tcd->image = p_image;
575 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
576 if (! p_tcd->tcd_image->tiles) {
579 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
581 l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_t);
582 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size);
583 if (! p_tcd->tcd_image->tiles->comps ) {
586 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
588 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
589 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
595 Destroy a previously created TCD handle
597 void opj_tcd_destroy(opj_tcd_t *tcd) {
599 opj_tcd_free_tile(tcd);
601 if (tcd->tcd_image) {
602 opj_free(tcd->tcd_image);
609 /* ----------------------------------------------------------------------- */
610 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
611 opj_bool FUNCTION ( opj_tcd_t *p_tcd, \
612 OPJ_UINT32 p_tile_no \
615 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
616 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
617 opj_tcp_t * l_tcp = 00; \
618 opj_cp_t * l_cp = 00; \
619 opj_tcd_tile_t * l_tile = 00; \
620 opj_tccp_t *l_tccp = 00; \
621 opj_tcd_tilecomp_t *l_tilec = 00; \
622 opj_image_comp_t * l_image_comp = 00; \
623 opj_tcd_resolution_t *l_res = 00; \
624 opj_tcd_band_t *l_band = 00; \
625 opj_stepsize_t * l_step_size = 00; \
626 opj_tcd_precinct_t *l_current_precinct = 00; \
627 TYPE* l_code_block = 00; \
628 opj_image_t *l_image = 00; \
630 OPJ_UINT32 l_level_no; \
631 OPJ_UINT32 l_pdx, l_pdy; \
633 OPJ_INT32 l_x0b, l_y0b; \
634 /* extent of precincts , top left, bottom right**/ \
635 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
636 /* number of precinct for a resolution */ \
637 OPJ_UINT32 l_nb_precincts; \
638 /* room needed to store l_nb_precinct precinct for a resolution */ \
639 OPJ_UINT32 l_nb_precinct_size; \
640 /* number of code blocks for a precinct*/ \
641 OPJ_UINT32 l_nb_code_blocks; \
642 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
643 OPJ_UINT32 l_nb_code_blocks_size; \
644 /* size of data for a tile */ \
645 OPJ_UINT32 l_data_size; \
648 l_tcp = &(l_cp->tcps[p_tile_no]); \
649 l_tile = p_tcd->tcd_image->tiles; \
650 l_tccp = l_tcp->tccps; \
651 l_tilec = l_tile->comps; \
652 l_image = p_tcd->image; \
653 l_image_comp = p_tcd->image->comps; \
655 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
656 q = p_tile_no / l_cp->tw; \
657 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
659 /* 4 borders of the tile rescale on the image if necessary */ \
660 l_tile->x0 = opj_int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \
661 l_tile->y0 = opj_int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \
662 l_tile->x1 = opj_int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \
663 l_tile->y1 = opj_int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \
664 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \
666 /*tile->numcomps = image->numcomps; */ \
667 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
668 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
670 /* border of each l_tile component (global) */ \
671 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, l_image_comp->dx); \
672 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, l_image_comp->dy); \
673 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, l_image_comp->dx); \
674 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, l_image_comp->dy); \
675 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \
677 l_data_size = (l_tilec->x1 - l_tilec->x0) \
678 * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 );\
679 l_tilec->numresolutions = l_tccp->numresolutions; \
680 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
681 l_tilec->minimum_num_resolutions = 1; \
684 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
685 - l_cp->m_specific_param.m_dec.m_reduce; \
688 if (l_tilec->data == 00) { \
689 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
690 if (! l_tilec->data ) { \
693 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
695 l_tilec->data_size = l_data_size; \
697 else if (l_data_size > l_tilec->data_size) { \
698 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
699 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \
700 fprintf(stderr, "Not enough memory to handle tile data\n"); \
702 opj_free(l_tilec->data); \
703 l_tilec->data = NULL; \
704 l_tilec->data_size = 0; \
707 l_tilec->data = new_data; \
708 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
709 l_tilec->data_size = l_data_size; \
712 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_t); \
714 if (l_tilec->resolutions == 00) { \
715 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); \
716 if (! l_tilec->resolutions ) { \
719 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/ \
720 l_tilec->resolutions_size = l_data_size; \
721 memset(l_tilec->resolutions,0,l_data_size); \
723 else if (l_data_size > l_tilec->resolutions_size) { \
724 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
725 if (! new_resolutions) { \
726 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \
727 fprintf(stderr, "Not enough memory to tile resolutions\n"); \
728 opj_free(l_tilec->resolutions); \
729 l_tilec->resolutions = NULL; \
730 l_tilec->resolutions_size = 0; \
733 l_tilec->resolutions = new_resolutions; \
734 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
735 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
736 l_tilec->resolutions_size = l_data_size; \
739 l_level_no = l_tilec->numresolutions - 1; \
740 l_res = l_tilec->resolutions; \
741 l_step_size = l_tccp->stepsizes; \
742 if (l_tccp->qmfbid == 0) { \
743 l_gain_ptr = &opj_dwt_getgain_real; \
746 l_gain_ptr = &opj_dwt_getgain; \
748 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
750 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
751 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
752 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/; \
753 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
754 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
756 /* border for each resolution level (global) */ \
757 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, l_level_no); \
758 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, l_level_no); \
759 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, l_level_no); \
760 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, l_level_no); \
761 /*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);*/ \
762 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
763 l_pdx = l_tccp->prcw[resno]; \
764 l_pdy = l_tccp->prch[resno]; \
765 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
766 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
767 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \
768 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \
769 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \
770 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \
771 /*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 );*/ \
773 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
774 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
775 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
777 l_nb_precincts = l_res->pw * l_res->ph; \
778 l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_t); \
780 tlcbgxstart = l_tl_prc_x_start; \
781 tlcbgystart = l_tl_prc_y_start; \
782 /*brcbgxend = l_br_prc_x_end;*/ \
783 /* brcbgyend = l_br_prc_y_end;*/ \
784 cbgwidthexpn = l_pdx; \
785 cbgheightexpn = l_pdy; \
786 l_res->numbands = 1; \
789 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1); \
790 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1); \
791 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/ \
792 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/ \
793 cbgwidthexpn = l_pdx - 1; \
794 cbgheightexpn = l_pdy - 1; \
795 l_res->numbands = 3; \
798 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn); \
799 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn); \
800 l_band = l_res->bands; \
802 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
804 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
807 l_band->bandno = 0 ; \
808 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, l_level_no); \
809 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, l_level_no); \
810 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, l_level_no); \
811 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, l_level_no); \
814 l_band->bandno = bandno + 1; \
815 /* x0b = 1 if bandno = 1 or 3 */ \
816 l_x0b = l_band->bandno&1; \
817 /* y0b = 1 if bandno = 2 or 3 */ \
818 l_y0b = (l_band->bandno)>>1; \
819 /* l_band border (global) */ \
820 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
821 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
822 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
823 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
826 /** avoid an if with storing function pointer */ \
827 l_gain = (*l_gain_ptr) (l_band->bandno); \
828 numbps = l_image_comp->prec + l_gain; \
829 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \
830 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */ \
832 if (! l_band->precincts) { \
833 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \
834 if (! l_band->precincts) { \
837 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */ \
838 memset(l_band->precincts,0,l_nb_precinct_size); \
839 l_band->precincts_data_size = l_nb_precinct_size; \
841 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
843 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \
844 if (! new_precincts) { \
845 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \
846 fprintf(stderr, "Not enough memory to handle band precints\n"); \
847 opj_free(l_band->precincts); \
848 l_band->precincts = NULL; \
849 l_band->precincts_data_size = 0; \
852 l_band->precincts = new_precincts; \
853 /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/ \
854 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \
855 l_band->precincts_data_size = l_nb_precinct_size; \
858 l_current_precinct = l_band->precincts; \
859 for (precno = 0; precno < l_nb_precincts; ++precno) { \
860 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
861 OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
862 OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \
863 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
864 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
865 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \
866 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \
868 /* precinct size (global) */ \
869 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
871 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0); \
872 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0); \
873 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1); \
874 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1); \
875 /*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);*/ \
877 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \
878 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
879 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \
880 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
881 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \
882 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
883 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
884 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
885 l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \
886 l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
888 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
889 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
890 l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \
892 if (! l_current_precinct->cblks.ELEMENT) { \
893 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \
894 if (! l_current_precinct->cblks.ELEMENT ) { \
897 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/ \
899 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \
901 l_current_precinct->block_size = l_nb_code_blocks_size; \
903 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
904 TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
905 if (! new_ELEMENT) { \
906 opj_free(l_current_precinct->cblks.ELEMENT); \
907 l_current_precinct->cblks.ELEMENT = NULL; \
908 l_current_precinct->block_size = 0; \
909 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \
910 fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \
913 l_current_precinct->cblks.ELEMENT = new_ELEMENT; \
914 /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */\
916 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
918 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
920 l_current_precinct->block_size = l_nb_code_blocks_size; \
923 if (! l_current_precinct->incltree) { \
924 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, \
925 l_current_precinct->ch); \
928 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, \
929 l_current_precinct->cw, \
930 l_current_precinct->ch); \
933 if (! l_current_precinct->incltree) { \
934 fprintf(stderr, "WARNING: No incltree created.\n"); \
935 /*return OPJ_FALSE;*/ \
938 if (! l_current_precinct->imsbtree) { \
939 l_current_precinct->imsbtree = opj_tgt_create( \
940 l_current_precinct->cw, \
941 l_current_precinct->ch); \
944 l_current_precinct->imsbtree = opj_tgt_init( \
945 l_current_precinct->imsbtree, \
946 l_current_precinct->cw, \
947 l_current_precinct->ch); \
950 if (! l_current_precinct->imsbtree) { \
951 fprintf(stderr, "WARNING: No imsbtree created.\n"); \
952 /*return OPJ_FALSE;*/ \
955 l_code_block = l_current_precinct->cblks.ELEMENT; \
957 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
958 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
959 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
960 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
961 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
963 /* code-block size (global) */ \
964 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); \
965 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); \
966 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); \
967 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); \
969 if (! FUNCTION_ELEMENT(l_code_block)) { \
974 ++l_current_precinct; \
990 MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
991 MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
993 #undef MACRO_TCD_ALLOCATE
996 * Allocates memory for an encoding code block.
998 opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1000 if (! p_code_block->data) {
1002 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
1003 if(! p_code_block->data) {
1007 p_code_block->data[0] = 0;
1008 p_code_block->data+=1;
1010 /* no memset since data */
1011 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
1012 if (! p_code_block->layers) {
1016 p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
1017 if (! p_code_block->passes) {
1022 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1023 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
1029 * Allocates memory for a decoding code block.
1031 opj_bool opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1033 OPJ_UINT32 l_seg_size;
1035 if (! p_code_block->data) {
1037 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
1038 if (! p_code_block->data) {
1041 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1043 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1044 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1045 if (! p_code_block->segs) {
1048 memset(p_code_block->segs,0,l_seg_size);
1049 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1051 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
1052 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1055 /*p_code_block->numsegs = 0; */
1060 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1063 OPJ_UINT32 l_data_size = 0;
1064 opj_image_comp_t * l_img_comp = 00;
1065 opj_tcd_tilecomp_t * l_tile_comp = 00;
1066 opj_tcd_resolution_t * l_res = 00;
1067 OPJ_UINT32 l_size_comp, l_remaining;
1069 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1070 l_img_comp = p_tcd->image->comps;
1072 for (i=0;i<p_tcd->image->numcomps;++i) {
1073 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1074 l_remaining = l_img_comp->prec & 7; /* (%8) */
1080 if (l_size_comp == 3) {
1084 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1085 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
1093 opj_bool opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1094 OPJ_UINT32 p_tile_no,
1096 OPJ_UINT32 * p_data_written,
1097 OPJ_UINT32 p_max_length,
1098 opj_codestream_info_t *p_cstr_info)
1101 if (p_tcd->cur_tp_num == 0) {
1103 p_tcd->tcd_tileno = p_tile_no;
1104 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1106 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1108 OPJ_UINT32 l_num_packs = 0;
1110 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1111 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1113 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1114 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1116 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
1117 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
1119 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1120 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
1121 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
1123 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));
1127 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1128 /*---------------TILE-------------------*/
1129 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1132 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1134 /* FIXME _ProfStart(PGROUP_MCT); */
1135 if (! opj_tcd_mct_encode(p_tcd)) {
1138 /* FIXME _ProfStop(PGROUP_MCT); */
1140 /* FIXME _ProfStart(PGROUP_DWT); */
1141 if (! opj_tcd_dwt_encode(p_tcd)) {
1144 /* FIXME _ProfStop(PGROUP_DWT); */
1146 /* FIXME _ProfStart(PGROUP_T1); */
1147 if (! opj_tcd_t1_encode(p_tcd)) {
1150 /* FIXME _ProfStop(PGROUP_T1); */
1152 /* FIXME _ProfStart(PGROUP_RATE); */
1153 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1156 /* FIXME _ProfStop(PGROUP_RATE); */
1159 /*--------------TIER2------------------*/
1163 p_cstr_info->index_write = 1;
1165 /* FIXME _ProfStart(PGROUP_T2); */
1167 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1170 /* FIXME _ProfStop(PGROUP_T2); */
1172 /*---------------CLEAN-------------------*/
1177 opj_bool opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1179 OPJ_UINT32 p_max_length,
1180 OPJ_UINT32 p_tile_no,
1181 opj_codestream_index_t *p_cstr_index
1184 OPJ_UINT32 l_data_read;
1185 p_tcd->tcd_tileno = p_tile_no;
1186 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1188 #ifdef TODO_MSD /* FIXME */
1191 OPJ_UINT32 resno, compno, numprec = 0;
1192 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1193 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1194 opj_tccp_t *tccp = &tcp->tccps[compno];
1195 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1196 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1197 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1198 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1199 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1200 numprec += res_idx->pw * res_idx->ph;
1201 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1202 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1205 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1206 p_cstr_info->packno = 0;
1211 /*--------------TIER2------------------*/
1212 /* FIXME _ProfStart(PGROUP_T2); */
1215 (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1219 /* FIXME _ProfStop(PGROUP_T2); */
1221 /*------------------TIER1-----------------*/
1223 /* FIXME _ProfStart(PGROUP_T1); */
1225 (! opj_tcd_t1_decode(p_tcd))
1229 /* FIXME _ProfStop(PGROUP_T1); */
1231 /*----------------DWT---------------------*/
1233 /* FIXME _ProfStart(PGROUP_DWT); */
1235 (! opj_tcd_dwt_decode(p_tcd))
1239 /* FIXME _ProfStop(PGROUP_DWT); */
1241 /*----------------MCT-------------------*/
1242 /* FIXME _ProfStart(PGROUP_MCT); */
1244 (! opj_tcd_mct_decode(p_tcd))
1248 /* FIXME _ProfStop(PGROUP_MCT); */
1250 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1252 (! opj_tcd_dc_level_shift_decode(p_tcd))
1256 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1259 /*---------------TILE-------------------*/
1263 opj_bool opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1265 OPJ_UINT32 p_dest_length
1268 OPJ_UINT32 i,j,k,l_data_size = 0;
1269 opj_image_comp_t * l_img_comp = 00;
1270 opj_tcd_tilecomp_t * l_tilec = 00;
1271 opj_tcd_resolution_t * l_res;
1272 OPJ_UINT32 l_size_comp, l_remaining;
1273 OPJ_UINT32 l_stride, l_width,l_height;
1275 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1276 if (l_data_size > p_dest_length) {
1280 l_tilec = p_tcd->tcd_image->tiles->comps;
1281 l_img_comp = p_tcd->image->comps;
1283 for (i=0;i<p_tcd->image->numcomps;++i) {
1284 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1285 l_remaining = l_img_comp->prec & 7; /* (%8) */
1286 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1287 l_width = (l_res->x1 - l_res->x0);
1288 l_height = (l_res->y1 - l_res->y0);
1289 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
1295 if (l_size_comp == 3) {
1299 switch (l_size_comp)
1303 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1304 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1306 if (l_img_comp->sgnd) {
1307 for (j=0;j<l_height;++j) {
1308 for (k=0;k<l_width;++k) {
1309 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1311 l_src_ptr += l_stride;
1315 for (j=0;j<l_height;++j) {
1316 for (k=0;k<l_width;++k) {
1317 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
1319 l_src_ptr += l_stride;
1323 p_dest = (OPJ_BYTE *)l_dest_ptr;
1328 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1329 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1331 if (l_img_comp->sgnd) {
1332 for (j=0;j<l_height;++j) {
1333 for (k=0;k<l_width;++k) {
1334 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1336 l_src_ptr += l_stride;
1340 for (j=0;j<l_height;++j) {
1341 for (k=0;k<l_width;++k) {
1342 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
1344 l_src_ptr += l_stride;
1348 p_dest = (OPJ_BYTE*) l_dest_ptr;
1353 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1354 OPJ_INT32 * l_src_ptr = l_tilec->data;
1356 for (j=0;j<l_height;++j) {
1357 for (k=0;k<l_width;++k) {
1358 *(l_dest_ptr++) = (*(l_src_ptr++));
1360 l_src_ptr += l_stride;
1363 p_dest = (OPJ_BYTE*) l_dest_ptr;
1378 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1380 OPJ_UINT32 compno, resno, bandno, precno;
1381 opj_tcd_tile_t *l_tile = 00;
1382 opj_tcd_tilecomp_t *l_tile_comp = 00;
1383 opj_tcd_resolution_t *l_res = 00;
1384 opj_tcd_band_t *l_band = 00;
1385 opj_tcd_precinct_t *l_precinct = 00;
1386 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1387 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1393 if (! p_tcd->tcd_image) {
1397 if (p_tcd->m_is_decoder) {
1398 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1401 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1404 l_tile = p_tcd->tcd_image->tiles;
1409 l_tile_comp = l_tile->comps;
1411 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1412 l_res = l_tile_comp->resolutions;
1415 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1416 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1417 l_band = l_res->bands;
1418 for (bandno = 0; bandno < 3; ++bandno) {
1419 l_precinct = l_band->precincts;
1422 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1423 for (precno = 0; precno < l_nb_precincts; ++precno) {
1424 opj_tgt_destroy(l_precinct->incltree);
1425 l_precinct->incltree = 00;
1426 opj_tgt_destroy(l_precinct->imsbtree);
1427 l_precinct->imsbtree = 00;
1428 (*l_tcd_code_block_deallocate) (l_precinct);
1432 opj_free(l_band->precincts);
1433 l_band->precincts = 00;
1440 opj_free(l_tile_comp->resolutions);
1441 l_tile_comp->resolutions = 00;
1444 if (l_tile_comp->data) {
1445 opj_free(l_tile_comp->data);
1446 l_tile_comp->data = 00;
1451 opj_free(l_tile->comps);
1453 opj_free(p_tcd->tcd_image->tiles);
1454 p_tcd->tcd_image->tiles = 00;
1458 opj_bool opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1459 OPJ_BYTE * p_src_data,
1460 OPJ_UINT32 * p_data_read,
1461 OPJ_UINT32 p_max_src_size,
1462 opj_codestream_index_t *p_cstr_index
1467 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1472 if (! opj_t2_decode_packets(
1475 p_tcd->tcd_image->tiles,
1480 opj_t2_destroy(l_t2);
1484 opj_t2_destroy(l_t2);
1486 /*---------------CLEAN-------------------*/
1490 opj_bool opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1494 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1495 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1496 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1499 l_t1 = opj_t1_create();
1504 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1505 /* The +3 is headroom required by the vectorized DWT */
1506 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1507 opj_t1_destroy(l_t1);
1514 opj_t1_destroy(l_t1);
1520 opj_bool opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1523 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1524 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1525 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1526 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1528 for (compno = 0; compno < l_tile->numcomps; compno++) {
1530 if (tcd->cp->reduce != 0) {
1531 tcd->image->comps[compno].resno_decoded =
1532 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1533 if (tcd->image->comps[compno].resno_decoded < 0)
1538 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1539 if(numres2decode > 0){
1542 if (l_tccp->qmfbid == 1) {
1543 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1548 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1560 opj_bool opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1562 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1563 opj_tcp_t * l_tcp = p_tcd->tcp;
1564 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1565 OPJ_UINT32 l_samples,i;
1571 l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1573 if (l_tile->numcomps >= 3 ){
1574 if (l_tcp->mct == 2) {
1577 if (! l_tcp->m_mct_decoding_matrix) {
1581 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1586 for (i=0;i<l_tile->numcomps;++i) {
1587 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1591 if (! opj_mct_decode_custom(/* MCT data */
1592 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1593 /* size of components */
1597 /* nb of components (i.e. size of pData) */
1599 /* tells if the data is signed */
1600 p_tcd->image->comps->sgnd)) {
1608 if (l_tcp->tccps->qmfbid == 1) {
1609 opj_mct_decode( l_tile->comps[0].data,
1610 l_tile->comps[1].data,
1611 l_tile->comps[2].data,
1615 opj_mct_decode_real( (float*)l_tile->comps[0].data,
1616 (float*)l_tile->comps[1].data,
1617 (float*)l_tile->comps[2].data,
1623 /* FIXME need to use opj_event_msg function */
1624 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1631 opj_bool opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1634 opj_tcd_tilecomp_t * l_tile_comp = 00;
1635 opj_tccp_t * l_tccp = 00;
1636 opj_image_comp_t * l_img_comp = 00;
1637 opj_tcd_resolution_t* l_res = 00;
1638 opj_tcd_tile_t * l_tile;
1639 OPJ_UINT32 l_width,l_height,i,j;
1640 OPJ_INT32 * l_current_ptr;
1641 OPJ_INT32 l_min, l_max;
1642 OPJ_UINT32 l_stride;
1644 l_tile = p_tcd->tcd_image->tiles;
1645 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 = opj_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 = opj_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_t * p_precinct)
1701 OPJ_UINT32 cblkno , l_nb_code_blocks;
1703 opj_tcd_cblk_dec_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_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_t * p_precinct)
1739 OPJ_UINT32 cblkno , l_nb_code_blocks;
1741 opj_tcd_cblk_enc_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_t *p_tcd )
1771 OPJ_UINT32 i,l_data_size = 0;
1772 opj_image_comp_t * l_img_comp = 00;
1773 opj_tcd_tilecomp_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_t *p_tcd )
1801 opj_tcd_tilecomp_t * l_tile_comp = 00;
1802 opj_tccp_t * l_tccp = 00;
1803 opj_image_comp_t * l_img_comp = 00;
1804 opj_tcd_tile_t * l_tile;
1805 OPJ_UINT32 l_nb_elem,i;
1806 OPJ_INT32 * l_current_ptr;
1808 l_tile = p_tcd->tcd_image->tiles;
1809 l_tile_comp = l_tile->comps;
1810 l_tccp = p_tcd->tcp->tccps;
1811 l_img_comp = p_tcd->image->comps;
1813 for (compno = 0; compno < l_tile->numcomps; compno++) {
1814 l_current_ptr = l_tile_comp->data;
1815 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1817 if (l_tccp->qmfbid == 1) {
1818 for (i = 0; i < l_nb_elem; ++i) {
1819 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1824 for (i = 0; i < l_nb_elem; ++i) {
1825 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1838 opj_bool opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1840 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1841 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1842 OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1844 OPJ_BYTE ** l_data = 00;
1845 opj_tcp_t * l_tcp = p_tcd->tcp;
1847 if(!p_tcd->tcp->mct) {
1851 if (p_tcd->tcp->mct == 2) {
1852 if (! p_tcd->tcp->m_mct_coding_matrix) {
1856 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1861 for (i=0;i<l_tile->numcomps;++i) {
1862 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1866 if (! opj_mct_encode_custom(/* MCT data */
1867 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1868 /* size of components */
1872 /* nb of components (i.e. size of pData) */
1874 /* tells if the data is signed */
1875 p_tcd->image->comps->sgnd) )
1883 else if (l_tcp->tccps->qmfbid == 0) {
1884 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1887 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1893 opj_bool opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1895 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1896 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1897 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1900 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1901 if (l_tccp->qmfbid == 1) {
1902 if (! opj_dwt_encode(l_tile_comp)) {
1906 else if (l_tccp->qmfbid == 0) {
1907 if (! opj_dwt_encode_real(l_tile_comp)) {
1919 opj_bool opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1922 const OPJ_FLOAT64 * l_mct_norms;
1923 opj_tcp_t * l_tcp = p_tcd->tcp;
1925 l_t1 = opj_t1_create();
1930 if (l_tcp->mct == 1) {
1931 /* irreversible encoding */
1932 if (l_tcp->tccps->qmfbid == 0) {
1933 l_mct_norms = opj_mct_get_mct_norms_real();
1936 l_mct_norms = opj_mct_get_mct_norms();
1940 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1943 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1944 opj_t1_destroy(l_t1);
1948 opj_t1_destroy(l_t1);
1953 opj_bool opj_tcd_t2_encode (opj_tcd_t *p_tcd,
1954 OPJ_BYTE * p_dest_data,
1955 OPJ_UINT32 * p_data_written,
1956 OPJ_UINT32 p_max_dest_size,
1957 opj_codestream_info_t *p_cstr_info )
1961 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1966 if (! opj_t2_encode_packets(
1969 p_tcd->tcd_image->tiles,
1970 p_tcd->tcp->numlayers,
1980 opj_t2_destroy(l_t2);
1984 opj_t2_destroy(l_t2);
1986 /*---------------CLEAN-------------------*/
1991 opj_bool opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
1992 OPJ_BYTE * p_dest_data,
1993 OPJ_UINT32 p_max_dest_size,
1994 opj_codestream_info_t *p_cstr_info )
1996 opj_cp_t * l_cp = p_tcd->cp;
1997 OPJ_UINT32 l_nb_written = 0;
2000 p_cstr_info->index_write = 0;
2003 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2005 /* Normal Rate/distortion allocation */
2006 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2011 /* Fixed layer allocation */
2012 opj_tcd_rateallocate_fixed(p_tcd);
2019 opj_bool opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2021 OPJ_UINT32 p_src_length )
2023 OPJ_UINT32 i,j,l_data_size = 0;
2024 opj_image_comp_t * l_img_comp = 00;
2025 opj_tcd_tilecomp_t * l_tilec = 00;
2026 OPJ_UINT32 l_size_comp, l_remaining;
2027 OPJ_UINT32 l_nb_elem;
2029 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2030 if (l_data_size != p_src_length) {
2034 l_tilec = p_tcd->tcd_image->tiles->comps;
2035 l_img_comp = p_tcd->image->comps;
2036 for (i=0;i<p_tcd->image->numcomps;++i) {
2037 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2038 l_remaining = l_img_comp->prec & 7; /* (%8) */
2039 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
2045 if (l_size_comp == 3) {
2049 switch (l_size_comp) {
2052 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2053 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2055 if (l_img_comp->sgnd) {
2056 for (j=0;j<l_nb_elem;++j) {
2057 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2061 for (j=0;j<l_nb_elem;++j) {
2062 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2066 p_src = (OPJ_BYTE*) l_src_ptr;
2071 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2072 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2074 if (l_img_comp->sgnd) {
2075 for (j=0;j<l_nb_elem;++j) {
2076 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2080 for (j=0;j<l_nb_elem;++j) {
2081 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2085 p_src = (OPJ_BYTE*) l_src_ptr;
2090 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2091 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2093 for (j=0;j<l_nb_elem;++j) {
2094 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2097 p_src = (OPJ_BYTE*) l_src_ptr;