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
9 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include "opj_includes.h"
41 #include "opj_malloc.h"
50 * Deallocates the encoding data of the given precinct.
52 static void tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
54 * Allocates memory for an encoding code block.
56 static bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
58 * Allocates memory for a decoding code block.
60 static bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
62 Free the memory allocated for encoding
65 static void tcd_free_tile(opj_tcd_t *tcd);
67 /* ----------------------------------------------------------------------- */
70 Create a new TCD handle
72 opj_tcd_t* tcd_create(bool p_is_decoder)
74 opj_tcd_t *l_tcd = 00;
76 /* create the tcd structure */
77 l_tcd = (opj_tcd_t*) opj_malloc(sizeof(opj_tcd_t));
83 memset(l_tcd,0,sizeof(opj_tcd_t));
84 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
85 l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
92 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
97 Destroy a previously created TCD handle
99 void tcd_destroy(opj_tcd_t *tcd) {
107 opj_free(tcd->tcd_image);
114 /* ----------------------------------------------------------------------- */
116 * Initialize the tile coder and may reuse some meory.
117 * @param p_tcd TCD handle.
118 * @param p_image raw image.
119 * @param p_cp coding parameters.
120 * @param p_tile_no current tile index to encode.
122 * @return true if the encoding values could be set (false otherwise).
124 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
128 OPJ_UINT32 p_tile_no \
131 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
132 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
133 opj_tcp_t * l_tcp = 00; \
134 opj_cp_t * l_cp = 00; \
135 opj_tcd_tile_t * l_tile = 00; \
136 opj_tccp_t *l_tccp = 00; \
137 opj_tcd_tilecomp_t *l_tilec = 00; \
138 opj_image_comp_t * l_image_comp = 00; \
139 opj_tcd_resolution_t *l_res = 00; \
140 opj_tcd_band_t *l_band = 00; \
141 opj_stepsize_t * l_step_size = 00; \
142 opj_tcd_precinct_t *l_current_precinct = 00; \
143 TYPE* l_code_block = 00; \
144 opj_image_t * l_image = 00; \
146 OPJ_UINT32 l_level_no; \
147 OPJ_UINT32 l_pdx, l_pdy; \
149 OPJ_INT32 l_x0b, l_y0b; \
150 /* extent of precincts , top left, bottom right**/ \
151 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
152 /* number of precinct for a resolution */ \
153 OPJ_UINT32 l_nb_precincts; \
154 /* room needed to store l_nb_precinct precinct for a resolution */ \
155 OPJ_UINT32 l_nb_precinct_size; \
156 /* number of code blocks for a precinct*/ \
157 OPJ_UINT32 l_nb_code_blocks; \
158 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
159 OPJ_UINT32 l_nb_code_blocks_size; \
160 /* size of data for a tile */ \
161 OPJ_UINT32 l_data_size; \
163 l_tcp = &(l_cp->tcps[p_tile_no]); \
164 l_tile = p_tcd->tcd_image->tiles; \
165 l_tccp = l_tcp->tccps; \
166 l_tilec = l_tile->comps; \
167 l_image = p_tcd->image; \
168 l_image_comp = p_tcd->image->comps; \
170 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
171 q = p_tile_no / l_cp->tw; \
173 /* 4 borders of the tile rescale on the image if necessary */ \
174 l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \
175 l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \
176 l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \
177 l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \
178 /*tile->numcomps = image->numcomps; */ \
180 (compno = 0; compno < l_tile->numcomps; ++compno) \
182 /* border of each l_tile component (global) */ \
183 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \
184 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \
185 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \
186 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \
188 l_data_size = (l_tilec->x1 - l_tilec->x0) \
189 * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \
190 l_tilec->numresolutions = l_tccp->numresolutions; \
192 (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce)\
194 l_tilec->minimum_num_resolutions = 1; \
198 l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;\
201 (l_tilec->data == 00) \
203 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_data_size); \
209 l_tilec->data_size = l_data_size; \
212 (l_data_size > l_tilec->data_size) \
214 l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);\
220 l_tilec->data_size = l_data_size; \
222 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_t);\
224 (l_tilec->resolutions == 00) \
226 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);\
228 (! l_tilec->resolutions ) \
232 l_tilec->resolutions_size = l_data_size; \
233 memset(l_tilec->resolutions,0,l_data_size); \
236 (l_data_size > l_tilec->resolutions_size) \
238 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);\
240 (! l_tilec->resolutions) \
244 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);\
245 l_tilec->resolutions_size = l_data_size; \
247 l_level_no = l_tilec->numresolutions - 1; \
248 l_res = l_tilec->resolutions; \
249 l_step_size = l_tccp->stepsizes; \
251 (l_tccp->qmfbid == 0) \
253 l_gain_ptr = &dwt_getgain_real; \
257 l_gain_ptr = &dwt_getgain; \
260 (resno = 0; resno < l_tilec->numresolutions; ++resno) \
262 OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \
263 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
264 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
265 /* border for each resolution level (global) */ \
266 l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
267 l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
268 l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
269 l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
270 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */\
271 l_pdx = l_tccp->prcw[resno]; \
272 l_pdy = l_tccp->prch[resno]; \
273 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
274 l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \
275 l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \
276 l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \
277 l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \
279 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);\
280 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);\
281 l_nb_precincts = l_res->pw * l_res->ph; \
282 l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_t);\
286 tlcbgxstart = l_tl_prc_x_start; \
287 tlcbgystart = l_tl_prc_y_start; \
288 brcbgxend = l_br_prc_x_end; \
289 brcbgyend = l_br_prc_y_end; \
290 cbgwidthexpn = l_pdx; \
291 cbgheightexpn = l_pdy; \
292 l_res->numbands = 1; \
296 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \
297 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \
298 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \
299 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \
300 cbgwidthexpn = l_pdx - 1; \
301 cbgheightexpn = l_pdy - 1; \
302 l_res->numbands = 3; \
305 cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \
306 cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \
307 l_band = l_res->bands; \
309 (bandno = 0; bandno < l_res->numbands; ++bandno) \
315 l_band->bandno = 0 ; \
316 l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
317 l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
318 l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
319 l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
323 l_band->bandno = bandno + 1; \
324 /* x0b = 1 if bandno = 1 or 3 */ \
325 l_x0b = l_band->bandno&1; \
326 /* y0b = 1 if bandno = 2 or 3 */ \
327 l_y0b = (l_band->bandno)>>1; \
328 /* l_band border (global) */ \
329 l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1);\
330 l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1);\
331 l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1);\
332 l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1);\
334 /** avoid an if with storing function pointer */ \
335 l_gain = (*l_gain_ptr) (l_band->bandno); \
336 numbps = l_image_comp->prec + l_gain; \
337 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
338 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */\
340 (! l_band->precincts) \
342 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */ l_nb_precinct_size);\
344 (! l_band->precincts) \
348 memset(l_band->precincts,0,l_nb_precinct_size); \
349 l_band->precincts_data_size = l_nb_precinct_size; \
352 (l_band->precincts_data_size < l_nb_precinct_size) \
354 l_band->precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
356 (! l_band->precincts) \
360 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
361 l_band->precincts_data_size = l_nb_precinct_size; \
363 l_current_precinct = l_band->precincts; \
365 (precno = 0; precno < l_nb_precincts; ++precno) \
367 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
368 OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn);\
369 OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn);\
370 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
371 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
372 /* precinct size (global) */ \
373 l_current_precinct->x0 = int_max(cbgxstart, l_band->x0);\
374 l_current_precinct->y0 = int_max(cbgystart, l_band->y0);\
375 l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \
376 l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \
377 tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn;\
378 tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn;\
379 brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn;\
380 brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn;\
381 l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\
382 l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;\
383 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;\
384 l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE);\
386 (! l_current_precinct->cblks.ELEMENT) \
388 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
390 (! l_current_precinct->cblks.ELEMENT ) \
394 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
395 l_current_precinct->block_size = l_nb_code_blocks_size;\
398 (l_nb_code_blocks_size > l_current_precinct->block_size)\
400 l_current_precinct->cblks.ELEMENT = (TYPE*) \
401 opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);\
403 (! l_current_precinct->cblks.ELEMENT ) \
407 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size\
409 ,l_nb_code_blocks_size - l_current_precinct->block_size);\
410 l_current_precinct->block_size = l_nb_code_blocks_size;\
413 (! l_current_precinct->incltree) \
415 l_current_precinct->incltree = tgt_create(l_current_precinct->cw,\
416 l_current_precinct->ch);\
420 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,\
421 l_current_precinct->cw, \
422 l_current_precinct->ch);\
425 (! l_current_precinct->incltree) \
430 (! l_current_precinct->imsbtree) \
432 l_current_precinct->imsbtree = tgt_create( \
433 l_current_precinct->cw,\
434 l_current_precinct->ch);\
438 l_current_precinct->imsbtree = tgt_init( \
439 l_current_precinct->imsbtree,\
440 l_current_precinct->cw,\
441 l_current_precinct->ch);\
444 (! l_current_precinct->imsbtree) \
448 l_code_block = l_current_precinct->cblks.ELEMENT; \
450 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) \
452 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);\
453 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);\
454 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
455 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
456 /* code-block size (global) */ \
457 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0);\
458 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0);\
459 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1);\
460 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1);\
462 (! FUNCTION_ELEMENT(l_code_block)) \
468 ++l_current_precinct; \
483 MACRO_TCD_ALLOCATE(tcd_init_encode_tile,opj_tcd_cblk_enc_t,1.f,enc,tcd_code_block_enc_allocate)
484 MACRO_TCD_ALLOCATE(tcd_init_decode_tile,opj_tcd_cblk_dec_t,0.5f,dec,tcd_code_block_dec_allocate)
486 #undef MACRO_TCD_ALLOCATE
489 * Allocates memory for an encoding code block.
491 bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
494 (! p_code_block->data)
496 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
498 (! p_code_block->data)
502 p_code_block->data+=1;
503 /* no memset since data */
504 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
506 (! p_code_block->layers)
510 p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
512 (! p_code_block->passes)
517 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
518 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
523 * Allocates memory for a decoding code block.
525 bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
527 OPJ_UINT32 l_seg_size;
530 (! p_code_block->data)
532 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
534 (! p_code_block->data)
538 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
539 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
541 (! p_code_block->segs)
545 memset(p_code_block->segs,0,l_seg_size);
546 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
549 //p_code_block->numsegs = 0;
554 * Deallocates the encoding data of the given precinct.
556 void tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
558 OPJ_UINT32 cblkno , l_nb_code_blocks;
560 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
564 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
566 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
571 opj_free(l_code_block->data-1);
572 l_code_block->data = 00;
575 (l_code_block->layers)
577 opj_free(l_code_block->layers );
578 l_code_block->layers = 00;
581 (l_code_block->passes)
583 opj_free(l_code_block->passes );
584 l_code_block->passes = 00;
588 opj_free(p_precinct->cblks.enc);
589 p_precinct->cblks.enc = 00;
594 * Deallocates the encoding data of the given precinct.
596 void tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
598 OPJ_UINT32 cblkno , l_nb_code_blocks;
600 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
604 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
606 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
611 opj_free(l_code_block->data);
612 l_code_block->data = 00;
617 opj_free(l_code_block->segs );
618 l_code_block->segs = 00;
622 opj_free(p_precinct->cblks.dec);
623 p_precinct->cblks.dec = 00;
627 void tcd_free_tile(opj_tcd_t *p_tcd)
629 OPJ_UINT32 compno, resno, bandno, precno;
630 opj_tcd_tile_t *l_tile = 00;
631 opj_tcd_tilecomp_t *l_tile_comp = 00;
632 opj_tcd_resolution_t *l_res = 00;
633 opj_tcd_band_t *l_band = 00;
634 opj_tcd_precinct_t *l_precinct = 00;
635 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
636 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
649 (p_tcd->m_is_decoder)
651 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
655 l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate;
659 l_tile = p_tcd->tcd_image->tiles;
665 l_tile_comp = l_tile->comps;
668 (compno = 0; compno < l_tile->numcomps; ++compno)
670 l_res = l_tile_comp->resolutions;
674 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
676 (resno = 0; resno < l_nb_resolutions; ++resno)
678 l_band = l_res->bands;
680 (bandno = 0; bandno < 3; ++bandno)
682 l_precinct = l_band->precincts;
686 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
688 (precno = 0; precno < l_nb_precincts; ++precno)
690 tgt_destroy(l_precinct->incltree);
691 l_precinct->incltree = 00;
692 tgt_destroy(l_precinct->imsbtree);
693 l_precinct->imsbtree = 00;
694 (*l_tcd_code_block_deallocate) (l_precinct);
697 opj_free(l_band->precincts);
698 l_band->precincts = 00;
704 opj_free(l_tile_comp->resolutions);
705 l_tile_comp->resolutions = 00;
710 opj_aligned_free(l_tile_comp->data);
711 l_tile_comp->data = 00;
715 opj_free(l_tile->comps);
717 opj_free(p_tcd->tcd_image->tiles);
718 p_tcd->tcd_image->tiles = 00;
723 opj_image_t * p_image,
727 OPJ_UINT32 l_tile_comp_size;
729 p_tcd->image = p_image;
731 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
734 (! p_tcd->tcd_image->tiles)
738 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
740 l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_t);
741 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size);
743 (! p_tcd->tcd_image->tiles->comps )
747 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
748 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
749 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
753 void tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
754 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
755 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
756 OPJ_INT32 matrice[10][10][3];
759 opj_cp_t *cp = tcd->cp;
760 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
761 opj_tcp_t *tcd_tcp = tcd->tcp;
763 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
764 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
765 for (i = 0; i < tcd_tcp->numlayers; i++) {
766 for (j = 0; j < tilec->numresolutions; j++) {
767 for (k = 0; k < 3; k++) {
769 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
770 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
775 for (resno = 0; resno < tilec->numresolutions; resno++) {
776 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
777 for (bandno = 0; bandno < res->numbands; bandno++) {
778 opj_tcd_band_t *band = &res->bands[bandno];
779 for (precno = 0; precno < res->pw * res->ph; precno++) {
780 opj_tcd_precinct_t *prc = &band->precincts[precno];
781 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
782 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
783 opj_tcd_layer_t *layer = &cblk->layers[layno];
785 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
786 /* Correction of the matrix of coefficient to include the IMSB information */
788 value = matrice[layno][resno][bandno];
795 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
796 if (imsb >= matrice[layno - 1][resno][bandno]) {
797 value -= (imsb - matrice[layno - 1][resno][bandno]);
805 cblk->numpassesinlayers = 0;
808 n = cblk->numpassesinlayers;
809 if (cblk->numpassesinlayers == 0) {
811 n = 3 * value - 2 + cblk->numpassesinlayers;
813 n = cblk->numpassesinlayers;
816 n = 3 * value + cblk->numpassesinlayers;
819 layer->numpasses = n - cblk->numpassesinlayers;
821 if (!layer->numpasses)
824 if (cblk->numpassesinlayers == 0) {
825 layer->len = cblk->passes[n - 1].rate;
826 layer->data = cblk->data;
828 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
829 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
832 cblk->numpassesinlayers = n;
840 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
842 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
843 tcd_makelayer_fixed(tcd, layno, 1);
847 void tcd_makelayer(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_FLOAT64 thresh, OPJ_UINT32 final) {
848 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
851 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
853 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
855 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
856 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
857 for (resno = 0; resno < tilec->numresolutions; resno++) {
858 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
859 for (bandno = 0; bandno < res->numbands; bandno++) {
860 opj_tcd_band_t *band = &res->bands[bandno];
861 for (precno = 0; precno < res->pw * res->ph; precno++) {
862 opj_tcd_precinct_t *prc = &band->precincts[precno];
863 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
864 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
865 opj_tcd_layer_t *layer = &cblk->layers[layno];
869 cblk->numpassesinlayers = 0;
871 n = cblk->numpassesinlayers;
872 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
875 opj_tcd_pass_t *pass = &cblk->passes[passno];
878 dd = pass->distortiondec;
880 dr = pass->rate - cblk->passes[n - 1].rate;
881 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
888 if (dd / dr >= thresh)
891 layer->numpasses = n - cblk->numpassesinlayers;
893 if (!layer->numpasses) {
897 if (cblk->numpassesinlayers == 0) {
898 layer->len = cblk->passes[n - 1].rate;
899 layer->data = cblk->data;
900 layer->disto = cblk->passes[n - 1].distortiondec;
902 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
903 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
904 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
907 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
910 cblk->numpassesinlayers = n;
918 bool tcd_rateallocate(opj_tcd_t *tcd, OPJ_BYTE *dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 len, opj_codestream_info_t *cstr_info) {
919 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
921 OPJ_FLOAT64 min, max;
922 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
923 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
924 OPJ_FLOAT64 maxSE = 0;
926 opj_cp_t *cp = tcd->cp;
927 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
928 opj_tcp_t *tcd_tcp = tcd->tcp;
933 tcd_tile->numpix = 0; /* fixed_quality */
935 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
936 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
939 for (resno = 0; resno < tilec->numresolutions; resno++) {
940 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
942 for (bandno = 0; bandno < res->numbands; bandno++) {
943 opj_tcd_band_t *band = &res->bands[bandno];
945 for (precno = 0; precno < res->pw * res->ph; precno++) {
946 opj_tcd_precinct_t *prc = &band->precincts[precno];
948 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
949 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
951 for (passno = 0; passno < cblk->totalpasses; passno++) {
952 opj_tcd_pass_t *pass = &cblk->passes[passno];
954 OPJ_FLOAT64 dd, rdslope;
957 dd = pass->distortiondec;
959 dr = pass->rate - cblk->passes[passno - 1].rate;
960 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
975 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
976 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
982 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
983 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
984 * ((OPJ_FLOAT64)(tilec->numpix));
989 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
990 tile_info->numpix = tcd_tile->numpix;
991 tile_info->distotile = tcd_tile->distotile;
992 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
995 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
996 OPJ_FLOAT64 lo = min;
997 OPJ_FLOAT64 hi = max;
998 bool success = false;
999 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
1000 OPJ_FLOAT64 goodthresh = 0;
1001 OPJ_FLOAT64 stable_thresh = 0;
1003 OPJ_FLOAT64 distotarget; /* fixed_quality */
1006 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
1008 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1009 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1010 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1011 ==> possible to have some lossy layers and the last layer for sure lossless */
1012 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))) {
1013 opj_t2_t *t2 = t2_create(tcd->image, cp);
1014 OPJ_FLOAT64 thresh = 0;
1022 (i = 0; i < 32; ++i)
1024 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
1025 thresh = (lo + hi) / 2;
1027 tcd_makelayer(tcd, layno, thresh, 0);
1029 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
1030 if(cp->m_specific_param.m_enc.m_cinema){
1032 (! 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))
1039 distoachieved = layno == 0 ?
1040 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1041 if (distoachieved < distotarget) {
1043 stable_thresh = thresh;
1050 distoachieved = (layno == 0) ?
1051 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1052 if (distoachieved < distotarget) {
1054 stable_thresh = thresh;
1061 (! 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))
1063 /* TODO: what to do with l ??? seek / tell ??? */
1064 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1069 stable_thresh = thresh;
1073 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1084 if(cstr_info) { /* Threshold for Marcela Index */
1085 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1087 tcd_makelayer(tcd, layno, goodthresh, 1);
1090 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1096 OPJ_UINT32 tcd_get_encoded_tile_size (
1100 OPJ_UINT32 i,l_data_size = 0;
1101 opj_image_comp_t * l_img_comp = 00;
1102 opj_tcd_tilecomp_t * l_tilec = 00;
1103 OPJ_UINT32 l_size_comp, l_remaining;
1105 l_tilec = p_tcd->tcd_image->tiles->comps;
1106 l_img_comp = p_tcd->image->comps;
1108 (i=0;i<p_tcd->image->numcomps;++i)
1110 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1111 l_remaining = l_img_comp->prec & 7; /* (%8) */
1122 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
1129 bool tcd_copy_tile_data (
1132 OPJ_UINT32 p_src_length
1135 OPJ_UINT32 i,j,l_data_size = 0;
1136 opj_image_comp_t * l_img_comp = 00;
1137 opj_tcd_tilecomp_t * l_tilec = 00;
1138 OPJ_UINT32 l_size_comp, l_remaining;
1139 OPJ_UINT32 l_nb_elem;
1141 l_data_size = tcd_get_encoded_tile_size(p_tcd);
1143 (l_data_size != p_src_length)
1147 l_tilec = p_tcd->tcd_image->tiles->comps;
1148 l_img_comp = p_tcd->image->comps;
1150 (i=0;i<p_tcd->image->numcomps;++i)
1152 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1153 l_remaining = l_img_comp->prec & 7; /* (%8) */
1154 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
1170 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
1171 OPJ_INT32 * l_dest_ptr = l_tilec->data;
1176 (j=0;j<l_nb_elem;++j)
1178 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
1184 (j=0;j<l_nb_elem;++j)
1186 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
1189 p_src = (OPJ_BYTE*) l_src_ptr;
1194 OPJ_INT32 * l_dest_ptr = l_tilec->data;
1195 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
1200 (j=0;j<l_nb_elem;++j)
1202 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
1208 (j=0;j<l_nb_elem;++j)
1210 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
1214 p_src = (OPJ_BYTE*) l_src_ptr;
1219 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
1220 OPJ_INT32 * l_dest_ptr = l_tilec->data;
1222 (j=0;j<l_nb_elem;++j)
1224 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
1226 p_src = (OPJ_BYTE*) l_src_ptr;
1236 bool tcd_update_tile_data (
1239 OPJ_UINT32 p_dest_length
1242 OPJ_UINT32 i,j,k,l_data_size = 0;
1243 opj_image_comp_t * l_img_comp = 00;
1244 opj_tcd_tilecomp_t * l_tilec = 00;
1245 opj_tcd_resolution_t * l_res;
1246 OPJ_UINT32 l_size_comp, l_remaining;
1247 OPJ_UINT32 l_stride, l_width,l_height;
1249 l_data_size = tcd_get_decoded_tile_size(p_tcd);
1251 (l_data_size > p_dest_length)
1256 l_tilec = p_tcd->tcd_image->tiles->comps;
1257 l_img_comp = p_tcd->image->comps;
1259 (i=0;i<p_tcd->image->numcomps;++i)
1261 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1262 l_remaining = l_img_comp->prec & 7; /* (%8) */
1263 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1264 l_width = (l_res->x1 - l_res->x0);
1265 l_height = (l_res->y1 - l_res->y0);
1266 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
1282 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1283 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1288 (j=0;j<l_height;++j)
1293 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1295 l_src_ptr += l_stride;
1301 (j=0;j<l_height;++j)
1306 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
1308 l_src_ptr += l_stride;
1311 p_dest = (OPJ_BYTE *)l_dest_ptr;
1317 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1318 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1323 (j=0;j<l_height;++j)
1328 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1330 l_src_ptr += l_stride;
1336 (j=0;j<l_height;++j)
1341 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
1343 l_src_ptr += l_stride;
1346 p_dest = (OPJ_BYTE*) l_dest_ptr;
1351 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1352 OPJ_INT32 * l_src_ptr = l_tilec->data;
1354 (j=0;j<l_height;++j)
1359 *(l_dest_ptr++) = (*(l_src_ptr++));
1361 l_src_ptr += l_stride;
1363 p_dest = (OPJ_BYTE*) l_dest_ptr;
1373 OPJ_UINT32 tcd_get_decoded_tile_size (
1378 OPJ_UINT32 l_data_size = 0;
1379 opj_image_comp_t * l_img_comp = 00;
1380 opj_tcd_tilecomp_t * l_tile_comp = 00;
1381 opj_tcd_resolution_t * l_res = 00;
1382 OPJ_UINT32 l_size_comp, l_remaining;
1384 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1385 l_img_comp = p_tcd->image->comps;
1387 (i=0;i<p_tcd->image->numcomps;++i)
1389 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1390 l_remaining = l_img_comp->prec & 7; /* (%8) */
1401 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1402 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
1409 bool tcd_dc_level_shift_encode (
1414 opj_tcd_tilecomp_t * l_tile_comp = 00;
1415 opj_tccp_t * l_tccp = 00;
1416 opj_image_comp_t * l_img_comp = 00;
1417 opj_tcp_t * l_tcp = 00;
1418 opj_tcd_tile_t * l_tile;
1419 OPJ_UINT32 l_nb_elem,i;
1420 OPJ_INT32 * l_current_ptr;
1422 l_tile = p_tcd->tcd_image->tiles;
1423 l_tile_comp = l_tile->comps;
1425 l_tccp = p_tcd->tcp->tccps;
1426 l_img_comp = p_tcd->image->comps;
1428 (compno = 0; compno < l_tile->numcomps; compno++)
1430 l_current_ptr = l_tile_comp->data;
1431 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1433 (l_tccp->qmfbid == 1)
1436 (i = 0; i < l_nb_elem; ++i)
1438 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1445 (i = 0; i < l_nb_elem; ++i)
1447 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1458 bool tcd_mct_encode (
1462 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1463 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1464 OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1466 OPJ_BYTE ** l_data = 00;
1467 opj_tcp_t * l_tcp = p_tcd->tcp;
1475 (p_tcd->tcp->mct == 2)
1478 (! p_tcd->tcp->m_mct_coding_matrix)
1482 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1489 (i=0;i<l_tile->numcomps;++i)
1491 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1495 (! mct_encode_custom(// MCT data
1496 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1497 // size of components
1501 // nb of components (i.e. size of pData)
1503 // tells if the data is signed
1504 p_tcd->image->comps->sgnd)
1512 else if (l_tcp->tccps->qmfbid == 0)
1514 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1518 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1523 bool tcd_dwt_encode (
1527 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1528 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1529 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1534 (compno = 0; compno < l_tile->numcomps; ++compno)
1537 (l_tccp->qmfbid == 1)
1540 (! dwt_encode(l_tile_comp))
1546 (l_tccp->qmfbid == 0)
1549 (! dwt_encode_real(l_tile_comp))
1560 bool tcd_t1_encode (
1565 const OPJ_FLOAT64 * l_mct_norms;
1566 opj_tcp_t * l_tcp = p_tcd->tcp;
1577 // irreversible encoding
1579 (l_tcp->tccps->qmfbid == 0)
1581 l_mct_norms = get_mct_norms_real();
1585 l_mct_norms = get_mct_norms();
1590 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1594 (! t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms))
1603 bool tcd_t2_encode (
1605 OPJ_BYTE * p_dest_data,
1606 OPJ_UINT32 * p_data_written,
1607 OPJ_UINT32 p_max_dest_size,
1608 opj_codestream_info_t *p_cstr_info
1613 l_t2 = t2_create(p_tcd->image, p_tcd->cp);
1621 (! t2_encode_packets(
1624 p_tcd->tcd_image->tiles,
1625 p_tcd->tcp->numlayers,
1640 /*---------------CLEAN-------------------*/
1644 bool tcd_rate_allocate_encode(
1646 OPJ_BYTE * p_dest_data,
1647 OPJ_UINT32 p_max_dest_size,
1648 opj_codestream_info_t *p_cstr_info
1651 opj_cp_t * l_cp = p_tcd->cp;
1652 OPJ_UINT32 l_nb_written = 0;
1657 p_cstr_info->index_write = 0;
1660 (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)
1661 { /* fixed_quality */
1662 /* Normal Rate/distortion allocation */
1664 (! tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info))
1671 /* Fixed layer allocation */
1672 tcd_rateallocate_fixed(p_tcd);
1677 bool tcd_t2_decode (
1679 OPJ_BYTE * p_src_data,
1680 OPJ_UINT32 * p_data_read,
1681 OPJ_UINT32 p_max_src_size,
1682 opj_codestream_info_t *p_cstr_info
1687 l_t2 = t2_create(p_tcd->image, p_tcd->cp);
1695 (! t2_decode_packets(
1698 p_tcd->tcd_image->tiles,
1709 /*---------------CLEAN-------------------*/
1713 bool tcd_t1_decode (
1719 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1720 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1721 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1731 (compno = 0; compno < l_tile->numcomps; ++compno)
1733 /* The +3 is headroom required by the vectorized DWT */
1734 t1_decode_cblks(l_t1, l_tile_comp, l_tccp);
1742 bool tcd_dwt_decode (
1747 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1748 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1749 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1750 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1753 (compno = 0; compno < l_tile->numcomps; compno++)
1756 if (tcd->cp->reduce != 0) {
1757 tcd->image->comps[compno].resno_decoded =
1758 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1759 if (tcd->image->comps[compno].resno_decoded < 0)
1764 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1765 if(numres2decode > 0){
1768 (l_tccp->qmfbid == 1)
1771 (! dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1))
1779 (! dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1))
1790 bool tcd_mct_decode (
1794 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1795 opj_tcp_t * l_tcp = p_tcd->tcp;
1796 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1797 OPJ_UINT32 l_samples,i;
1804 l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1810 (! l_tcp->m_mct_decoding_matrix)
1814 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1821 (i=0;i<l_tile->numcomps;++i)
1823 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1827 (! mct_decode_custom( // MCT data
1828 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1829 // size of components
1833 // nb of components (i.e. size of pData)
1835 // tells if the data is signed
1836 p_tcd->image->comps->sgnd))
1846 (l_tcp->tccps->qmfbid == 1)
1849 l_tile->comps[0].data,
1850 l_tile->comps[1].data,
1851 l_tile->comps[2].data,
1857 (float*)l_tile->comps[0].data,
1858 (float*)l_tile->comps[1].data,
1859 (float*)l_tile->comps[2].data,
1866 bool tcd_dc_level_shift_decode (
1871 opj_tcd_tilecomp_t * l_tile_comp = 00;
1872 opj_tccp_t * l_tccp = 00;
1873 opj_image_comp_t * l_img_comp = 00;
1874 opj_tcd_resolution_t* l_res = 00;
1875 opj_tcp_t * l_tcp = 00;
1876 opj_tcd_tile_t * l_tile;
1877 OPJ_UINT32 l_width,l_height,i,j;
1878 OPJ_INT32 * l_current_ptr;
1879 OPJ_INT32 l_min, l_max;
1880 OPJ_UINT32 l_stride;
1882 l_tile = p_tcd->tcd_image->tiles;
1883 l_tile_comp = l_tile->comps;
1885 l_tccp = p_tcd->tcp->tccps;
1886 l_img_comp = p_tcd->image->comps;
1889 (compno = 0; compno < l_tile->numcomps; compno++)
1891 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1892 l_width = (l_res->x1 - l_res->x0);
1893 l_height = (l_res->y1 - l_res->y0);
1894 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1898 l_min = -(1 << (l_img_comp->prec - 1));
1899 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1904 l_max = (1 << l_img_comp->prec) - 1;
1906 l_current_ptr = l_tile_comp->data;
1908 (l_tccp->qmfbid == 1)
1911 (j=0;j<l_height;++j)
1914 (i = 0; i < l_width; ++i)
1916 *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1919 l_current_ptr += l_stride;
1925 (j=0;j<l_height;++j)
1928 (i = 0; i < l_width; ++i)
1930 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1931 *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1934 l_current_ptr += l_stride;
1944 bool tcd_encode_tile(
1946 OPJ_UINT32 p_tile_no,
1948 OPJ_UINT32 * p_data_written,
1949 OPJ_UINT32 p_max_length,
1950 opj_codestream_info_t *p_cstr_info)
1954 (p_tcd->cur_tp_num == 0)
1956 p_tcd->tcd_tileno = p_tile_no;
1957 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1958 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1961 OPJ_UINT32 l_num_packs = 0;
1963 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1964 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1965 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1966 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1968 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
1969 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
1971 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1972 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
1973 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
1975 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));
1978 _ProfStart(PGROUP_DC_SHIFT);
1979 /*---------------TILE-------------------*/
1981 (! tcd_dc_level_shift_encode(p_tcd))
1985 _ProfStop(PGROUP_DC_SHIFT);
1987 _ProfStart(PGROUP_MCT);
1989 (! tcd_mct_encode(p_tcd))
1993 _ProfStop(PGROUP_MCT);
1995 _ProfStart(PGROUP_DWT);
1997 (! tcd_dwt_encode(p_tcd))
2001 _ProfStop(PGROUP_DWT);
2003 _ProfStart(PGROUP_T1);
2005 (! tcd_t1_encode(p_tcd))
2009 _ProfStop(PGROUP_T1);
2011 _ProfStart(PGROUP_RATE);
2013 (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info))
2017 _ProfStop(PGROUP_RATE);
2020 /*--------------TIER2------------------*/
2026 p_cstr_info->index_write = 1;
2028 _ProfStart(PGROUP_T2);
2030 (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info))
2034 _ProfStop(PGROUP_T2);
2035 /*---------------CLEAN-------------------*/
2039 bool tcd_decode_tile(
2042 OPJ_UINT32 p_max_length,
2043 OPJ_UINT32 p_tile_no,
2044 opj_codestream_info_t *p_cstr_info)
2046 OPJ_UINT32 l_data_read;
2047 p_tcd->tcd_tileno = p_tile_no;
2048 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2052 OPJ_UINT32 resno, compno, numprec = 0;
2053 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2054 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
2055 opj_tccp_t *tccp = &tcp->tccps[compno];
2056 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2057 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2058 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
2059 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2060 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2061 numprec += res_idx->pw * res_idx->ph;
2062 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2063 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2066 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2067 p_cstr_info->packno = 0;
2071 /*--------------TIER2------------------*/
2072 _ProfStart(PGROUP_T2);
2075 (! tcd_t2_decode(p_tcd,p_src,&l_data_read,p_max_length,p_cstr_info))
2079 _ProfStop(PGROUP_T2);
2081 /*------------------TIER1-----------------*/
2083 _ProfStart(PGROUP_T1);
2085 (! tcd_t1_decode(p_tcd))
2089 _ProfStop(PGROUP_T1);
2091 /*----------------DWT---------------------*/
2093 _ProfStart(PGROUP_DWT);
2095 (! tcd_dwt_decode(p_tcd))
2099 _ProfStop(PGROUP_DWT);
2101 /*----------------MCT-------------------*/
2102 _ProfStart(PGROUP_MCT);
2104 (! tcd_mct_decode(p_tcd))
2108 _ProfStop(PGROUP_MCT);
2110 _ProfStart(PGROUP_DC_SHIFT);
2112 (! tcd_dc_level_shift_decode(p_tcd))
2116 _ProfStop(PGROUP_DC_SHIFT);
2119 /*---------------TILE-------------------*/