1 /*
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.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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.
32  */
33 
34 #include "tcd.h"
35 #include "openjpeg.h"
36 #include "j2k.h"
37 #include "opj_includes.h"
38 #include "event.h"
39 #include "t2.h"
40 #include "t1.h"
41 #include "opj_malloc.h"
42 #include "int.h"
43 #include "tgt.h"
44 #include "dwt.h"
45 #include "mct.h"
46 #include "j2k_lib.h"
47 #include "profile.h"
48 
49 /**
50  * Deallocates the encoding data of the given precinct.
51  */
52 static void tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
53 /**
54  * Allocates memory for an encoding code block.
55  */
56 static bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
57 /**
58  * Allocates memory for a decoding code block.
59  */
60 static bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
61 /**
62 Free the memory allocated for encoding
63 @param tcd TCD handle
64 */
65 static void tcd_free_tile(opj_tcd_t *tcd);
66 
67 /* ----------------------------------------------------------------------- */
68 
69 /**
70 Create a new TCD handle
71 */
tcd_create(bool p_is_decoder)72 opj_tcd_t* tcd_create(bool p_is_decoder)
73 {
74         opj_tcd_t *l_tcd = 00;
75 
76         /* create the tcd structure */
77         l_tcd = (opj_tcd_t*)        opj_malloc(sizeof(opj_tcd_t));
78         if
79                 (!l_tcd)
80         {
81                 return 00;
82         }
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));
86         if
87                 (!l_tcd->tcd_image)
88         {
89                 opj_free(l_tcd);
90                 return 00;
91         }
92         memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
93         return l_tcd;
94 }
95 
96 /**
97 Destroy a previously created TCD handle
98 */
tcd_destroy(opj_tcd_t * tcd)99 void tcd_destroy(opj_tcd_t *tcd) {
100         if
101                 (tcd)
102         {
103                 tcd_free_tile(tcd);
104                 if
105                         (tcd->tcd_image)
106                 {
107                         opj_free(tcd->tcd_image);
108                         tcd->tcd_image = 00;
109                 }
110                 opj_free(tcd);
111         }
112 }
113 
114 /* ----------------------------------------------------------------------- */
115 /**
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.
121  *
122  * @return true if the encoding values could be set (false otherwise).
123 */
124 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT)        \
125 bool FUNCTION                                                                                                                                \
126                         (                                                                                                                                \
127                                 opj_tcd_t *p_tcd,                                                                                        \
128                                 OPJ_UINT32 p_tile_no                                                                                \
129                         )                                                                                                                                \
130 {                                                                                                                                                        \
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;                                                                                        \
145         OPJ_UINT32 p,q;                                                                                                                        \
146         OPJ_UINT32 l_level_no;                                                                                                        \
147         OPJ_UINT32 l_pdx, l_pdy;                                                                                                \
148         OPJ_UINT32 l_gain;                                                                                                                \
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;                                                                                                        \
162         l_cp = p_tcd->cp;                                                                                                                \
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;                                                                                \
169                                                                                                                                                         \
170         p = p_tile_no % l_cp->tw;        /* tile coordinates */                                                \
171         q = p_tile_no / l_cp->tw;                                                                                                \
172                                                                                                                                                         \
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; */                                                                        \
179         for                                                                                                                                                \
180                 (compno = 0; compno < l_tile->numcomps; ++compno)                                        \
181         {                                                                                                                                                \
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);                        \
187                                                                                                                                                         \
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;                                        \
191                 if                                                                                                                                        \
192                         (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce)\
193                 {                                                                                                                                        \
194                         l_tilec->minimum_num_resolutions = 1;                                                        \
195                 }                                                                                                                                        \
196                 else                                                                                                                                \
197                 {                                                                                                                                        \
198                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;\
199                 }                                                                                                                                        \
200                 if                                                                                                                                        \
201                         (l_tilec->data == 00)                                                                                        \
202                 {                                                                                                                                        \
203             l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_data_size);        \
204                         if                                                                                                                                \
205                                 (! l_tilec->data )                                                                                        \
206                         {                                                                                                                                \
207                                 return false;                                                                                                \
208                         }                                                                                                                                \
209                         l_tilec->data_size = l_data_size;                                                                \
210                 }                                                                                                                                        \
211                 else if                                                                                                                                \
212                         (l_data_size > l_tilec->data_size)                                                                \
213                 {                                                                                                                                        \
214                         l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);\
215                         if                                                                                                                                \
216                                 (! l_tilec->data)                                                                                        \
217                         {                                                                                                                                \
218                                 return false;                                                                                                \
219                         }                                                                                                                                \
220                         l_tilec->data_size = l_data_size;                                                                \
221                 }                                                                                                                                        \
222                 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_t);\
223                 if                                                                                                                                        \
224                         (l_tilec->resolutions == 00)                                                                        \
225                 {                                                                                                                                        \
226             l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);\
227                         if                                                                                                                                \
228                                 (! l_tilec->resolutions )                                                                        \
229                         {                                                                                                                                \
230                                 return false;                                                                                                \
231                         }                                                                                                                                \
232                         l_tilec->resolutions_size = l_data_size;                                                \
233                         memset(l_tilec->resolutions,0,l_data_size);                                                \
234                 }                                                                                                                                        \
235                 else if                                                                                                                                \
236                         (l_data_size > l_tilec->resolutions_size)                                                \
237                 {                                                                                                                                        \
238                         l_tilec->resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);\
239                         if                                                                                                                                \
240                                 (! l_tilec->resolutions)                                                                        \
241                         {                                                                                                                                \
242                                 return false;                                                                                                \
243                         }                                                                                                                                \
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;                                                \
246                 }                                                                                                                                        \
247                 l_level_no = l_tilec->numresolutions - 1;                                                        \
248                 l_res = l_tilec->resolutions;                                                                                \
249                 l_step_size = l_tccp->stepsizes;                                                                        \
250                 if                                                                                                                                        \
251                         (l_tccp->qmfbid == 0)                                                                                        \
252                 {                                                                                                                                        \
253                         l_gain_ptr = &dwt_getgain_real;                                                                        \
254                 }                                                                                                                                        \
255                 else                                                                                                                                \
256                 {                                                                                                                                        \
257                         l_gain_ptr  = &dwt_getgain;                                                                                \
258                 }                                                                                                                                        \
259                 for                                                                                                                                        \
260                         (resno = 0; resno < l_tilec->numresolutions; ++resno)                        \
261                 {                                                                                                                                        \
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;        \
278                                                                                                                                                         \
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);\
283                         if                                                                                                                                \
284                                 (resno == 0)                                                                                                \
285                         {                                                                                                                                \
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;                                                                                \
293                         }                                                                                                                                \
294                         else                                                                                                                        \
295                         {                                                                                                                                \
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;                                                                                \
303                         }                                                                                                                                \
304                                                                                                                                                         \
305                         cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn);                        \
306                         cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn);                \
307                         l_band = l_res->bands;                                                                                        \
308                         for                                                                                                                                \
309                                 (bandno = 0; bandno < l_res->numbands; ++bandno)                        \
310                         {                                                                                                                                \
311                                 OPJ_INT32 numbps;                                                                                        \
312                                 if                                                                                                                        \
313                                         (resno == 0)                                                                                        \
314                                 {                                                                                                                        \
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);        \
320                                 }                                                                                                                        \
321                                 else                                                                                                                \
322                                 {                                                                                                                        \
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);\
333                                 }                                                                                                                        \
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 ? */\
339                                 if                                                                                                                        \
340                                         (! l_band->precincts)                                                                        \
341                                 {                                                                                                                        \
342                                         l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */ l_nb_precinct_size);\
343                                         if                                                                                                                \
344                                                 (! l_band->precincts)                                                                \
345                                         {                                                                                                                \
346                                                 return false;                                                                                \
347                                         }                                                                                                                \
348                                         memset(l_band->precincts,0,l_nb_precinct_size);                        \
349                                         l_band->precincts_data_size = l_nb_precinct_size;                \
350                                 }                                                                                                                        \
351                                 else if                                                                                                                \
352                                         (l_band->precincts_data_size < l_nb_precinct_size)                \
353                                 {                                                                                                                        \
354                                         l_band->precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
355                                         if                                                                                                                \
356                                                 (! l_band->precincts)                                                                \
357                                         {                                                                                                                \
358                                                 return false;                                                                                \
359                                         }                                                                                                                \
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;                \
362                                 }                                                                                                                        \
363                                 l_current_precinct = l_band->precincts;                                                \
364                                 for                                                                                                                        \
365                                         (precno = 0; precno < l_nb_precincts; ++precno)                        \
366                                 {                                                                                                                        \
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);\
385                                         if                                                                                                                \
386                                                 (! l_current_precinct->cblks.ELEMENT)                                \
387                                         {                                                                                                                \
388                                                 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
389                                                 if                                                                                                        \
390                                                         (! l_current_precinct->cblks.ELEMENT )                        \
391                                                 {                                                                                                        \
392                                                         return false;                                                                        \
393                                                 }                                                                                                        \
394                                                 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
395                                                 l_current_precinct->block_size = l_nb_code_blocks_size;\
396                                         }                                                                                                                \
397                                         else if                                                                                                        \
398                                                 (l_nb_code_blocks_size > l_current_precinct->block_size)\
399                                         {                                                                                                                \
400                                                 l_current_precinct->cblks.ELEMENT = (TYPE*)                        \
401                                                         opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);\
402                                                 if                                                                                                        \
403                                                         (! l_current_precinct->cblks.ELEMENT )                        \
404                                                 {                                                                                                        \
405                                                         return false;                                                                        \
406                                                 }                                                                                                        \
407                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size\
408                                                                                 ,0                                                                        \
409                                                                                 ,l_nb_code_blocks_size - l_current_precinct->block_size);\
410                                                 l_current_precinct->block_size = l_nb_code_blocks_size;\
411                                         }                                                                                                                \
412                                         if                                                                                                                \
413                                                 (! l_current_precinct->incltree)                                        \
414                                         {                                                                                                                \
415                         l_current_precinct->incltree = tgt_create(l_current_precinct->cw,\
416                                                                                                                                   l_current_precinct->ch);\
417                                         }                                                                                                                \
418                                         else                                                                                                        \
419                                         {                                                                                                                \
420                                                 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,\
421                                                                                                                                 l_current_precinct->cw, \
422                                                                                                                                 l_current_precinct->ch);\
423                                         }                                                                                                                \
424                                         if                                                                                                                \
425                                                 (! l_current_precinct->incltree)                                        \
426                                         {                                                                                                                \
427                                                 return false;                                                                                \
428                                         }                                                                                                                \
429                                         if                                                                                                                \
430                                                 (! l_current_precinct->imsbtree)                                        \
431                                         {                                                                                                                \
432                         l_current_precinct->imsbtree = tgt_create(                        \
433                                                                                                                 l_current_precinct->cw,\
434                                                                                                                 l_current_precinct->ch);\
435                                         }                                                                                                                \
436                                         else                                                                                                        \
437                                         {                                                                                                                \
438                                                 l_current_precinct->imsbtree = tgt_init(                        \
439                                                                                                                         l_current_precinct->imsbtree,\
440                                                                                                                         l_current_precinct->cw,\
441                                                                                                                         l_current_precinct->ch);\
442                                         }                                                                                                                \
443                                         if                                                                                                                \
444                                                 (! l_current_precinct->imsbtree)                                        \
445                                         {                                                                                                                \
446                                                 return false;                                                                                \
447                                         }                                                                                                                \
448                                         l_code_block = l_current_precinct->cblks.ELEMENT;                \
449                                         for                                                                                                                \
450                                                 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)        \
451                                         {                                                                                                                \
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);\
461                                                 if                                                                                                        \
462                                                         (! FUNCTION_ELEMENT(l_code_block))                                \
463                                                 {                                                                                                        \
464                                                         return false;                                                                        \
465                                                 }                                                                                                        \
466                                                 ++l_code_block;                                                                                \
467                                         }                                                                                                                \
468                                         ++l_current_precinct;                                                                        \
469                                 } /* precno */                                                                                                \
470                                 ++l_band;                                                                                                        \
471                                 ++l_step_size;                                                                                                \
472                         } /* bandno */                                                                                                        \
473                         ++l_res;                                                                                                                \
474                         --l_level_no;                                                                                                        \
475                 } /* resno */                                                                                                                \
476                 ++l_tccp;                                                                                                                        \
477                 ++l_tilec;                                                                                                                        \
478                 ++l_image_comp;                                                                                                                \
479         } /* compno */                                                                                                                        \
480         return true;                                                                                                                        \
481 }                                                                                                                                                        \
482 
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)
485 
486 #undef MACRO_TCD_ALLOCATE
487 
488 /**
489  * Allocates memory for an encoding code block.
490  */
tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t * p_code_block)491 bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
492 {
493         if
494                 (! p_code_block->data)
495         {
496                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
497                 if
498                         (! p_code_block->data)
499                 {
500                         return false;
501                 }
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));
505                 if
506                         (! p_code_block->layers)
507                 {
508                         return false;
509                 }
510                 p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
511                 if
512                         (! p_code_block->passes)
513                 {
514                         return false;
515                 }
516         }
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));
519         return true;
520 }
521 
522 /**
523  * Allocates memory for a decoding code block.
524  */
tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t * p_code_block)525 bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
526 {
527         OPJ_UINT32 l_seg_size;
528 
529         if
530                 (! p_code_block->data)
531         {
532                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
533                 if
534                         (! p_code_block->data)
535                 {
536                         return false;
537                 }
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);
540                 if
541                         (! p_code_block->segs)
542                 {
543                         return false;
544                 }
545                 memset(p_code_block->segs,0,l_seg_size);
546                 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
547         }
548         // TODO
549         //p_code_block->numsegs = 0;
550         return true;
551 }
552 
553 /**
554  * Deallocates the encoding data of the given precinct.
555  */
tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)556 void tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
557 {
558         OPJ_UINT32 cblkno , l_nb_code_blocks;
559 
560         opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
561         if
562                 (l_code_block)
563         {
564                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
565                 for
566                         (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
567                 {
568                         if
569                                 (l_code_block->data)
570                         {
571                                 opj_free(l_code_block->data-1);
572                                 l_code_block->data = 00;
573                         }
574                         if
575                                 (l_code_block->layers)
576                         {
577                                 opj_free(l_code_block->layers );
578                                 l_code_block->layers = 00;
579                         }
580                         if
581                                 (l_code_block->passes)
582                         {
583                                 opj_free(l_code_block->passes );
584                                 l_code_block->passes = 00;
585                         }
586                         ++l_code_block;
587                 }
588                 opj_free(p_precinct->cblks.enc);
589                 p_precinct->cblks.enc = 00;
590         }
591 }
592 
593 /**
594  * Deallocates the encoding data of the given precinct.
595  */
tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)596 void tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
597 {
598         OPJ_UINT32 cblkno , l_nb_code_blocks;
599 
600         opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
601         if
602                 (l_code_block)
603         {
604                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
605                 for
606                         (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
607                 {
608                         if
609                                 (l_code_block->data)
610                         {
611                                 opj_free(l_code_block->data);
612                                 l_code_block->data = 00;
613                         }
614                         if
615                                 (l_code_block->segs)
616                         {
617                                 opj_free(l_code_block->segs );
618                                 l_code_block->segs = 00;
619                         }
620                         ++l_code_block;
621                 }
622                 opj_free(p_precinct->cblks.dec);
623                 p_precinct->cblks.dec = 00;
624         }
625 }
626 
tcd_free_tile(opj_tcd_t * p_tcd)627 void tcd_free_tile(opj_tcd_t *p_tcd)
628 {
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;
637 
638         if
639                 (! p_tcd)
640         {
641                 return;
642         }
643         if
644                 (! p_tcd->tcd_image)
645         {
646                 return;
647         }
648         if
649                 (p_tcd->m_is_decoder)
650         {
651                 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
652         }
653         else
654         {
655                 l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate;
656         }
657 
658 
659         l_tile = p_tcd->tcd_image->tiles;
660         if
661                 (! l_tile)
662         {
663                 return;
664         }
665         l_tile_comp = l_tile->comps;
666 
667         for
668                 (compno = 0; compno < l_tile->numcomps; ++compno)
669         {
670                 l_res = l_tile_comp->resolutions;
671                 if
672                         (l_res)
673                 {
674                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
675                         for
676                                 (resno = 0; resno < l_nb_resolutions; ++resno)
677                         {
678                                 l_band = l_res->bands;
679                                 for
680                                         (bandno = 0; bandno < 3; ++bandno)
681                                 {
682                                         l_precinct = l_band->precincts;
683                                         if
684                                                 (l_precinct)
685                                         {
686                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
687                                                 for
688                                                         (precno = 0; precno < l_nb_precincts; ++precno)
689                                                 {
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);
695                                                         ++l_precinct;
696                                                 }
697                                                 opj_free(l_band->precincts);
698                                                 l_band->precincts = 00;
699                                         }
700                                         ++l_band;
701                                 } /* for (resno */
702                                 ++l_res;
703                         }
704                         opj_free(l_tile_comp->resolutions);
705                         l_tile_comp->resolutions = 00;
706                 }
707                 if
708                         (l_tile_comp->data)
709                 {
710                         opj_aligned_free(l_tile_comp->data);
711                         l_tile_comp->data = 00;
712                 }
713                 ++l_tile_comp;
714         }
715         opj_free(l_tile->comps);
716         l_tile->comps = 00;
717         opj_free(p_tcd->tcd_image->tiles);
718         p_tcd->tcd_image->tiles = 00;
719 }
720 
tcd_init(opj_tcd_t * p_tcd,opj_image_t * p_image,opj_cp_t * p_cp)721 bool tcd_init(
722                                            opj_tcd_t *p_tcd,
723                                            opj_image_t * p_image,
724                                            opj_cp_t * p_cp
725                                            )
726 {
727         OPJ_UINT32 l_tile_comp_size;
728 
729         p_tcd->image = p_image;
730         p_tcd->cp = p_cp;
731         p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
732 
733         if
734                 (! p_tcd->tcd_image->tiles)
735         {
736                 return false;
737         }
738         memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
739 
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);
742         if
743                 (! p_tcd->tcd_image->tiles->comps )
744         {
745                 return false;
746         }
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;
750         return true;
751 }
752 
tcd_makelayer_fixed(opj_tcd_t * tcd,OPJ_UINT32 layno,OPJ_UINT32 final)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];
757         OPJ_UINT32 i, j, k;
758 
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;
762 
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++) {
768                                         matrice[i][j][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));
771                                 }
772                         }
773                 }
774 
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];
784                                                 OPJ_UINT32 n;
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 */
787                                                 if (layno == 0) {
788                                                         value = matrice[layno][resno][bandno];
789                                                         if (imsb >= value) {
790                                                                 value = 0;
791                                                         } else {
792                                                                 value -= imsb;
793                                                         }
794                                                 } else {
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]);
798                                                                 if (value < 0) {
799                                                                         value = 0;
800                                                                 }
801                                                         }
802                                                 }
803 
804                                                 if (layno == 0) {
805                                                         cblk->numpassesinlayers = 0;
806                                                 }
807 
808                                                 if (cblk->numpassesinlayers == 0) {
809                                                         if (value != 0) {
810                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
811                                                         } else {
812                                                                 n = cblk->numpassesinlayers;
813                                                         }
814                                                 } else {
815                                                         n = 3 * value + cblk->numpassesinlayers;
816                                                 }
817 
818                                                 layer->numpasses = n - cblk->numpassesinlayers;
819 
820                                                 if (!layer->numpasses)
821                                                         continue;
822 
823                                                 if (cblk->numpassesinlayers == 0) {
824                                                         layer->len = cblk->passes[n - 1].rate;
825                                                         layer->data = cblk->data;
826                                                 } else {
827                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
828                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
829                                                 }
830                                                 if (final)
831                                                         cblk->numpassesinlayers = n;
832                                         }
833                                 }
834                         }
835                 }
836         }
837 }
838 
tcd_rateallocate_fixed(opj_tcd_t * tcd)839 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
840         OPJ_UINT32 layno;
841         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
842                 tcd_makelayer_fixed(tcd, layno, 1);
843         }
844 }
845 
tcd_makelayer(opj_tcd_t * tcd,OPJ_UINT32 layno,OPJ_FLOAT64 thresh,OPJ_UINT32 final)846 void tcd_makelayer(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_FLOAT64 thresh, OPJ_UINT32 final) {
847         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
848         OPJ_UINT32 passno;
849 
850         opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
851 
852         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
853 
854         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
855                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
856                 for (resno = 0; resno < tilec->numresolutions; resno++) {
857                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
858                         for (bandno = 0; bandno < res->numbands; bandno++) {
859                                 opj_tcd_band_t *band = &res->bands[bandno];
860                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
861                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
862                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
863                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
864                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
865 
866                                                 OPJ_UINT32 n;
867                                                 if (layno == 0) {
868                                                         cblk->numpassesinlayers = 0;
869                                                 }
870                                                 n = cblk->numpassesinlayers;
871                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
872                                                         OPJ_INT32 dr;
873                                                         OPJ_FLOAT64 dd;
874                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
875                                                         if (n == 0) {
876                                                                 dr = pass->rate;
877                                                                 dd = pass->distortiondec;
878                                                         } else {
879                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
880                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
881                                                         }
882                                                         if (!dr) {
883                                                                 if (dd != 0)
884                                                                         n = passno + 1;
885                                                                 continue;
886                                                         }
887                                                         if (dd / dr >= thresh)
888                                                                 n = passno + 1;
889                                                 }
890                                                 layer->numpasses = n - cblk->numpassesinlayers;
891 
892                                                 if (!layer->numpasses) {
893                                                         layer->disto = 0;
894                                                         continue;
895                                                 }
896                                                 if (cblk->numpassesinlayers == 0) {
897                                                         layer->len = cblk->passes[n - 1].rate;
898                                                         layer->data = cblk->data;
899                                                         layer->disto = cblk->passes[n - 1].distortiondec;
900                                                 } else {
901                                                         layer->len = cblk->passes[n - 1].rate -        cblk->passes[cblk->numpassesinlayers - 1].rate;
902                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
903                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
904                                                 }
905 
906                                                 tcd_tile->distolayer[layno] += layer->disto;        /* fixed_quality */
907 
908                                                 if (final)
909                                                         cblk->numpassesinlayers = n;
910                                         }
911                                 }
912                         }
913                 }
914         }
915 }
916 
tcd_rateallocate(opj_tcd_t * tcd,OPJ_BYTE * dest,OPJ_UINT32 * p_data_written,OPJ_UINT32 len,opj_codestream_info_t * cstr_info)917 bool tcd_rateallocate(opj_tcd_t *tcd, OPJ_BYTE *dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 len, opj_codestream_info_t *cstr_info) {
918         OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
919         OPJ_UINT32 passno;
920         OPJ_FLOAT64 min, max;
921         OPJ_FLOAT64 cumdisto[100];        /* fixed_quality */
922         const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
923         OPJ_FLOAT64 maxSE = 0;
924 
925         opj_cp_t *cp = tcd->cp;
926         opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
927         opj_tcp_t *tcd_tcp = tcd->tcp;
928 
929         min = DBL_MAX;
930         max = 0;
931 
932         tcd_tile->numpix = 0;                /* fixed_quality */
933 
934         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
935                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
936                 tilec->numpix = 0;
937 
938                 for (resno = 0; resno < tilec->numresolutions; resno++) {
939                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
940 
941                         for (bandno = 0; bandno < res->numbands; bandno++) {
942                                 opj_tcd_band_t *band = &res->bands[bandno];
943 
944                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
945                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
946 
947                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
948                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
949 
950                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
951                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
952                                                         OPJ_INT32 dr;
953                                                         OPJ_FLOAT64 dd, rdslope;
954                                                         if (passno == 0) {
955                                                                 dr = pass->rate;
956                                                                 dd = pass->distortiondec;
957                                                         } else {
958                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
959                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
960                                                         }
961                                                         if (dr == 0) {
962                                                                 continue;
963                                                         }
964                                                         rdslope = dd / dr;
965                                                         if (rdslope < min) {
966                                                                 min = rdslope;
967                                                         }
968                                                         if (rdslope > max) {
969                                                                 max = rdslope;
970                                                         }
971                                                 } /* passno */
972 
973                                                 /* fixed_quality */
974                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
975                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
976                                         } /* cbklno */
977                                 } /* precno */
978                         } /* bandno */
979                 } /* resno */
980 
981                 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
982                         * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
983                         * ((OPJ_FLOAT64)(tilec->numpix));
984         } /* compno */
985 
986         /* index file */
987         if(cstr_info) {
988                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
989                 tile_info->numpix = tcd_tile->numpix;
990                 tile_info->distotile = tcd_tile->distotile;
991                 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
992         }
993 
994         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
995                 OPJ_FLOAT64 lo = min;
996                 OPJ_FLOAT64 hi = max;
997                 bool success = false;
998                 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
999                 OPJ_FLOAT64 goodthresh = 0;
1000                 OPJ_FLOAT64 stable_thresh = 0;
1001                 OPJ_UINT32 i;
1002                 OPJ_FLOAT64 distotarget;                /* fixed_quality */
1003 
1004                 /* fixed_quality */
1005                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
1006 
1007                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1008                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1009                   -q xx,yy,zz,0          (fixed_quality == 1 and distoratio == 0)
1010                   ==> possible to have some lossy layers and the last layer for sure lossless */
1011                 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))) {
1012                         opj_t2_t *t2 = t2_create(tcd->image, cp);
1013                         OPJ_FLOAT64 thresh = 0;
1014                         if
1015                                 (t2 == 00)
1016                         {
1017                                 return false;
1018                         }
1019 
1020                         for
1021                                 (i = 0; i < 128; ++i)
1022                         {
1023                                 OPJ_FLOAT64 distoachieved = 0;        /* fixed_quality */
1024                                 thresh = (lo + hi) / 2;
1025 
1026                                 tcd_makelayer(tcd, layno, thresh, 0);
1027 
1028                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {        /* fixed_quality */
1029                                         if(cp->m_specific_param.m_enc.m_cinema){
1030                                                 if
1031                                                         (! 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))
1032                                                 {
1033                                                         lo = thresh;
1034                                                         continue;
1035                                                 }
1036                                                 else
1037                                                 {
1038                            distoachieved =        layno == 0 ?
1039                                                         tcd_tile->distolayer[0]        : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1040                                                         if (distoachieved < distotarget) {
1041                                                                 hi=thresh;
1042                                                                 stable_thresh = thresh;
1043                                                                 continue;
1044                                                         }else{
1045                                                                 lo=thresh;
1046                                                         }
1047                                                 }
1048                                         }else{
1049                                                 distoachieved =        (layno == 0) ?
1050                                                         tcd_tile->distolayer[0]        : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1051                                                 if (distoachieved < distotarget) {
1052                                                         hi = thresh;
1053                                                         stable_thresh = thresh;
1054                                                         continue;
1055                                                 }
1056                                                 lo = thresh;
1057                                         }
1058                                 } else {
1059                                         if
1060                                                 (! 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))
1061                                         {
1062                                                 /* TODO: what to do with l ??? seek / tell ??? */
1063                                                 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1064                                                 lo = thresh;
1065                                                 continue;
1066                                         }
1067                                         hi = thresh;
1068                                         stable_thresh = thresh;
1069                                 }
1070                         }
1071                         success = true;
1072                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1073                         t2_destroy(t2);
1074                 } else {
1075                         success = true;
1076                         goodthresh = min;
1077                 }
1078 
1079                 if (!success) {
1080                         return false;
1081                 }
1082 
1083                 if(cstr_info) {        /* Threshold for Marcela Index */
1084                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1085                 }
1086                 tcd_makelayer(tcd, layno, goodthresh, 1);
1087 
1088                 /* fixed_quality */
1089                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1090         }
1091 
1092         return true;
1093 }
1094 
tcd_get_encoded_tile_size(opj_tcd_t * p_tcd)1095 OPJ_UINT32 tcd_get_encoded_tile_size (
1096                                                  opj_tcd_t *p_tcd
1097                                                  )
1098 {
1099         OPJ_UINT32 i,l_data_size = 0;
1100         opj_image_comp_t * l_img_comp = 00;
1101         opj_tcd_tilecomp_t * l_tilec = 00;
1102         OPJ_UINT32 l_size_comp, l_remaining;
1103 
1104         l_tilec = p_tcd->tcd_image->tiles->comps;
1105         l_img_comp = p_tcd->image->comps;
1106         for
1107                 (i=0;i<p_tcd->image->numcomps;++i)
1108         {
1109                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1110                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1111                 if
1112                         (l_remaining)
1113                 {
1114                         ++l_size_comp;
1115                 }
1116                 if
1117                         (l_size_comp == 3)
1118                 {
1119                         l_size_comp = 4;
1120                 }
1121                 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
1122                 ++l_img_comp;
1123                 ++l_tilec;
1124         }
1125         return l_data_size;
1126 }
1127 
tcd_copy_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_src,OPJ_UINT32 p_src_length)1128 bool tcd_copy_tile_data (
1129                                                  opj_tcd_t *p_tcd,
1130                                                  OPJ_BYTE * p_src,
1131                                                  OPJ_UINT32 p_src_length
1132                                                  )
1133 {
1134         OPJ_UINT32 i,j,l_data_size = 0;
1135         opj_image_comp_t * l_img_comp = 00;
1136         opj_tcd_tilecomp_t * l_tilec = 00;
1137         OPJ_UINT32 l_size_comp, l_remaining;
1138         OPJ_UINT32 l_nb_elem;
1139 
1140         l_data_size = tcd_get_encoded_tile_size(p_tcd);
1141         if
1142                 (l_data_size != p_src_length)
1143         {
1144                 return false;
1145         }
1146         l_tilec = p_tcd->tcd_image->tiles->comps;
1147         l_img_comp = p_tcd->image->comps;
1148         for
1149                 (i=0;i<p_tcd->image->numcomps;++i)
1150         {
1151                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1152                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1153                 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
1154                 if
1155                         (l_remaining)
1156                 {
1157                         ++l_size_comp;
1158                 }
1159                 if
1160                         (l_size_comp == 3)
1161                 {
1162                         l_size_comp = 4;
1163                 }
1164                 switch
1165                         (l_size_comp)
1166                 {
1167                         case 1:
1168                                 {
1169                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
1170                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
1171                                         if
1172                                                 (l_img_comp->sgnd)
1173                                         {
1174                                                 for
1175                                                         (j=0;j<l_nb_elem;++j)
1176                                                 {
1177                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
1178                                                 }
1179                                         }
1180                                         else
1181                                         {
1182                                                 for
1183                                                         (j=0;j<l_nb_elem;++j)
1184                                                 {
1185                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
1186                                                 }
1187                                         }
1188                                         p_src = (OPJ_BYTE*) l_src_ptr;
1189                                 }
1190                                 break;
1191                         case 2:
1192                                 {
1193                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
1194                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
1195                                         if
1196                                                 (l_img_comp->sgnd)
1197                                         {
1198                                                 for
1199                                                         (j=0;j<l_nb_elem;++j)
1200                                                 {
1201                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
1202                                                 }
1203                                         }
1204                                         else
1205                                         {
1206                                                 for
1207                                                         (j=0;j<l_nb_elem;++j)
1208                                                 {
1209                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
1210                                                 }
1211 
1212                                         }
1213                                         p_src = (OPJ_BYTE*) l_src_ptr;
1214                                 }
1215                                 break;
1216                         case 4:
1217                                 {
1218                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
1219                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
1220                                         for
1221                                                 (j=0;j<l_nb_elem;++j)
1222                                         {
1223                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
1224                                         }
1225                                         p_src = (OPJ_BYTE*) l_src_ptr;
1226                                 }
1227                                 break;
1228                 }
1229                 ++l_img_comp;
1230                 ++l_tilec;
1231         }
1232         return true;
1233 }
1234 
tcd_update_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_dest,OPJ_UINT32 p_dest_length)1235 bool tcd_update_tile_data (
1236                                                  opj_tcd_t *p_tcd,
1237                                                  OPJ_BYTE * p_dest,
1238                                                  OPJ_UINT32 p_dest_length
1239                                                  )
1240 {
1241         OPJ_UINT32 i,j,k,l_data_size = 0;
1242         opj_image_comp_t * l_img_comp = 00;
1243         opj_tcd_tilecomp_t * l_tilec = 00;
1244         opj_tcd_resolution_t * l_res;
1245         OPJ_UINT32 l_size_comp, l_remaining;
1246         OPJ_UINT32 l_stride, l_width,l_height;
1247 
1248         l_data_size = tcd_get_decoded_tile_size(p_tcd);
1249         if
1250                 (l_data_size > p_dest_length)
1251         {
1252                 return false;
1253         }
1254 
1255         l_tilec = p_tcd->tcd_image->tiles->comps;
1256         l_img_comp = p_tcd->image->comps;
1257         for
1258                 (i=0;i<p_tcd->image->numcomps;++i)
1259         {
1260                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1261                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1262                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1263                 l_width = (l_res->x1 - l_res->x0);
1264                 l_height = (l_res->y1 - l_res->y0);
1265                 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
1266                 if
1267                         (l_remaining)
1268                 {
1269                         ++l_size_comp;
1270                 }
1271                 if
1272                         (l_size_comp == 3)
1273                 {
1274                         l_size_comp = 4;
1275                 }
1276                 switch
1277                         (l_size_comp)
1278                 {
1279                         case 1:
1280                                 {
1281                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1282                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1283                                         if
1284                                                 (l_img_comp->sgnd)
1285                                         {
1286                                                 for
1287                                                         (j=0;j<l_height;++j)
1288                                                 {
1289                                                         for
1290                                                                 (k=0;k<l_width;++k)
1291                                                         {
1292                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1293                                                         }
1294                                                         l_src_ptr += l_stride;
1295                                                 }
1296                                         }
1297                                         else
1298                                         {
1299                                                 for
1300                                                         (j=0;j<l_height;++j)
1301                                                 {
1302                                                         for
1303                                                                 (k=0;k<l_width;++k)
1304                                                         {
1305                                                                 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
1306                                                         }
1307                                                         l_src_ptr += l_stride;
1308                                                 }
1309                                         }
1310                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
1311 
1312                                 }
1313                                 break;
1314                         case 2:
1315                                 {
1316                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1317                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1318                                         if
1319                                                 (l_img_comp->sgnd)
1320                                         {
1321                                                 for
1322                                                         (j=0;j<l_height;++j)
1323                                                 {
1324                                                         for
1325                                                                 (k=0;k<l_width;++k)
1326                                                         {
1327                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1328                                                         }
1329                                                         l_src_ptr += l_stride;
1330                                                 }
1331                                         }
1332                                         else
1333                                         {
1334                                                 for
1335                                                         (j=0;j<l_height;++j)
1336                                                 {
1337                                                         for
1338                                                                 (k=0;k<l_width;++k)
1339                                                         {
1340                                                                 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
1341                                                         }
1342                                                         l_src_ptr += l_stride;
1343                                                 }
1344                                         }
1345                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1346                                 }
1347                                 break;
1348                         case 4:
1349                                 {
1350                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1351                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
1352                                         for
1353                                                 (j=0;j<l_height;++j)
1354                                         {
1355                                                 for
1356                                                         (k=0;k<l_width;++k)
1357                                                 {
1358                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
1359                                                 }
1360                                                 l_src_ptr += l_stride;
1361                                         }
1362                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1363                                 }
1364                                 break;
1365                 }
1366                 ++l_img_comp;
1367                 ++l_tilec;
1368         }
1369         return true;
1370 }
1371 
tcd_get_decoded_tile_size(opj_tcd_t * p_tcd)1372 OPJ_UINT32 tcd_get_decoded_tile_size (
1373                                                  opj_tcd_t *p_tcd
1374                                                  )
1375 {
1376         OPJ_UINT32 i;
1377         OPJ_UINT32 l_data_size = 0;
1378         opj_image_comp_t * l_img_comp = 00;
1379         opj_tcd_tilecomp_t * l_tile_comp = 00;
1380         opj_tcd_resolution_t * l_res = 00;
1381         OPJ_UINT32 l_size_comp, l_remaining;
1382 
1383         l_tile_comp = p_tcd->tcd_image->tiles->comps;
1384         l_img_comp = p_tcd->image->comps;
1385         for
1386                 (i=0;i<p_tcd->image->numcomps;++i)
1387         {
1388                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1389                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1390                 if
1391                         (l_remaining)
1392                 {
1393                         ++l_size_comp;
1394                 }
1395                 if
1396                         (l_size_comp == 3)
1397                 {
1398                         l_size_comp = 4;
1399                 }
1400                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1401                 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
1402                 ++l_img_comp;
1403                 ++l_tile_comp;
1404         }
1405         return l_data_size;
1406 }
1407 
tcd_dc_level_shift_encode(opj_tcd_t * p_tcd)1408 bool tcd_dc_level_shift_encode (
1409                                                  opj_tcd_t *p_tcd
1410                                                  )
1411 {
1412         OPJ_UINT32 compno;
1413         opj_tcd_tilecomp_t * l_tile_comp = 00;
1414         opj_tccp_t * l_tccp = 00;
1415         opj_image_comp_t * l_img_comp = 00;
1416         opj_tcd_tile_t * l_tile;
1417         OPJ_UINT32 l_nb_elem,i;
1418         OPJ_INT32 * l_current_ptr;
1419 
1420         l_tile = p_tcd->tcd_image->tiles;
1421         l_tile_comp = l_tile->comps;
1422         l_tccp = p_tcd->tcp->tccps;
1423         l_img_comp = p_tcd->image->comps;
1424         for
1425                 (compno = 0; compno < l_tile->numcomps; compno++)
1426         {
1427                 l_current_ptr = l_tile_comp->data;
1428                 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1429                 if
1430                         (l_tccp->qmfbid == 1)
1431                 {
1432                         for
1433                                 (i = 0; i < l_nb_elem; ++i)
1434                         {
1435                                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1436                                 ++l_current_ptr;
1437                         }
1438                 }
1439                 else
1440                 {
1441                         for
1442                                 (i = 0; i < l_nb_elem; ++i)
1443                         {
1444                                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1445                                 ++l_current_ptr;
1446                         }
1447                 }
1448                 ++l_img_comp;
1449                 ++l_tccp;
1450                 ++l_tile_comp;
1451         }
1452         return true;
1453 }
1454 
tcd_mct_encode(opj_tcd_t * p_tcd)1455 bool tcd_mct_encode (
1456                                          opj_tcd_t *p_tcd
1457                                          )
1458 {
1459         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1460         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1461         OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1462         OPJ_UINT32 i;
1463         OPJ_BYTE ** l_data = 00;
1464         opj_tcp_t * l_tcp = p_tcd->tcp;
1465         if
1466                 (!p_tcd->tcp->mct)
1467         {
1468                 return true;
1469         }
1470 
1471         if
1472                 (p_tcd->tcp->mct == 2)
1473         {
1474                 if
1475                         (! p_tcd->tcp->m_mct_coding_matrix)
1476                 {
1477                         return true;
1478                 }
1479         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1480                 if
1481                         (! l_data)
1482                 {
1483                         return false;
1484                 }
1485                 for
1486                         (i=0;i<l_tile->numcomps;++i)
1487                 {
1488                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1489                         ++l_tile_comp;
1490                 }
1491                 if
1492                         (! mct_encode_custom(// MCT data
1493                                         (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1494                                         // size of components
1495                                         samples,
1496                                         // components
1497                                         l_data,
1498                                         // nb of components (i.e. size of pData)
1499                                         l_tile->numcomps,
1500                                         // tells if the data is signed
1501                                         p_tcd->image->comps->sgnd)
1502                         )
1503                 {
1504             opj_free(l_data);
1505                         return false;
1506                 }
1507                 opj_free(l_data);
1508         }
1509         else if (l_tcp->tccps->qmfbid == 0)
1510         {
1511                 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1512         }
1513         else
1514         {
1515                 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1516         }
1517         return true;
1518 }
1519 
tcd_dwt_encode(opj_tcd_t * p_tcd)1520 bool tcd_dwt_encode (
1521                                           opj_tcd_t *p_tcd
1522                                          )
1523 {
1524         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1525         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1526         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1527         OPJ_UINT32 compno;
1528 
1529 
1530          for
1531                  (compno = 0; compno < l_tile->numcomps; ++compno)
1532         {
1533                 if
1534                         (l_tccp->qmfbid == 1)
1535                 {
1536                         if
1537                                 (! dwt_encode(l_tile_comp))
1538                         {
1539                                 return false;
1540                         }
1541                 }
1542                 else if
1543                         (l_tccp->qmfbid == 0)
1544                 {
1545                         if
1546                                 (! dwt_encode_real(l_tile_comp))
1547                         {
1548                                 return false;
1549                         }
1550                 }
1551                 ++l_tile_comp;
1552                 ++l_tccp;
1553         }
1554         return true;
1555 }
1556 
tcd_t1_encode(opj_tcd_t * p_tcd)1557 bool tcd_t1_encode (
1558                                           opj_tcd_t *p_tcd
1559                                          )
1560 {
1561         opj_t1_t * l_t1;
1562         const OPJ_FLOAT64 * l_mct_norms;
1563         opj_tcp_t * l_tcp = p_tcd->tcp;
1564 
1565         l_t1 = t1_create();
1566         if
1567                 (l_t1 == 00)
1568         {
1569                 return false;
1570         }
1571         if
1572                 (l_tcp->mct == 1)
1573         {
1574                 // irreversible encoding
1575                 if
1576                         (l_tcp->tccps->qmfbid == 0)
1577                 {
1578                         l_mct_norms = get_mct_norms_real();
1579                 }
1580                 else
1581                 {
1582                         l_mct_norms = get_mct_norms();
1583                 }
1584         }
1585         else
1586         {
1587                 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1588         }
1589 
1590         if
1591                 (! t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms))
1592         {
1593         t1_destroy(l_t1);
1594                 return false;
1595         }
1596         t1_destroy(l_t1);
1597         return true;
1598 }
1599 
tcd_t2_encode(opj_tcd_t * p_tcd,OPJ_BYTE * p_dest_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_max_dest_size,opj_codestream_info_t * p_cstr_info)1600 bool tcd_t2_encode (
1601                                         opj_tcd_t *p_tcd,
1602                                         OPJ_BYTE * p_dest_data,
1603                                         OPJ_UINT32 * p_data_written,
1604                                         OPJ_UINT32 p_max_dest_size,
1605                                         opj_codestream_info_t *p_cstr_info
1606                                         )
1607 {
1608         opj_t2_t * l_t2;
1609 
1610         l_t2 = t2_create(p_tcd->image, p_tcd->cp);
1611         if
1612                 (l_t2 == 00)
1613         {
1614                 return false;
1615         }
1616 
1617         if
1618                 (! t2_encode_packets(
1619                                         l_t2,
1620                                         p_tcd->tcd_tileno,
1621                                         p_tcd->tcd_image->tiles,
1622                                         p_tcd->tcp->numlayers,
1623                                         p_dest_data,
1624                                         p_data_written,
1625                                         p_max_dest_size,
1626                                         p_cstr_info,
1627                                         p_tcd->tp_num,
1628                                         p_tcd->tp_pos,
1629                                         p_tcd->cur_pino,
1630                                         FINAL_PASS))
1631         {
1632                 t2_destroy(l_t2);
1633                 return false;
1634         }
1635         t2_destroy(l_t2);
1636 
1637         /*---------------CLEAN-------------------*/
1638         return true;
1639 }
1640 
tcd_rate_allocate_encode(opj_tcd_t * p_tcd,OPJ_BYTE * p_dest_data,OPJ_UINT32 p_max_dest_size,opj_codestream_info_t * p_cstr_info)1641 bool tcd_rate_allocate_encode(
1642                                                 opj_tcd_t *p_tcd,
1643                                                 OPJ_BYTE * p_dest_data,
1644                                                 OPJ_UINT32 p_max_dest_size,
1645                                                 opj_codestream_info_t *p_cstr_info
1646                                                 )
1647 {
1648         opj_cp_t * l_cp = p_tcd->cp;
1649         OPJ_UINT32 l_nb_written = 0;
1650 
1651         if
1652                 (p_cstr_info)
1653         {
1654                 p_cstr_info->index_write = 0;
1655         }
1656         if
1657                 (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)
1658         {        /* fixed_quality */
1659                 /* Normal Rate/distortion allocation */
1660                 if
1661                         (! tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info))
1662                 {
1663                         return false;
1664                 }
1665         }
1666         else
1667         {
1668                 /* Fixed layer allocation */
1669                 tcd_rateallocate_fixed(p_tcd);
1670         }
1671         return true;
1672 }
1673 
tcd_t2_decode(opj_tcd_t * p_tcd,OPJ_BYTE * p_src_data,OPJ_UINT32 * p_data_read,OPJ_UINT32 p_max_src_size,opj_codestream_info_t * p_cstr_info)1674 bool tcd_t2_decode (
1675                                         opj_tcd_t *p_tcd,
1676                                         OPJ_BYTE * p_src_data,
1677                                         OPJ_UINT32 * p_data_read,
1678                                         OPJ_UINT32 p_max_src_size,
1679                                         opj_codestream_info_t *p_cstr_info
1680                                         )
1681 {
1682         opj_t2_t * l_t2;
1683 
1684         l_t2 = t2_create(p_tcd->image, p_tcd->cp);
1685         if
1686                 (l_t2 == 00)
1687         {
1688                 return false;
1689         }
1690 
1691         if
1692                 (! t2_decode_packets(
1693                                         l_t2,
1694                                         p_tcd->tcd_tileno,
1695                                         p_tcd->tcd_image->tiles,
1696                                         p_src_data,
1697                                         p_data_read,
1698                                         p_max_src_size,
1699                                         p_cstr_info))
1700         {
1701                 t2_destroy(l_t2);
1702                 return false;
1703         }
1704         t2_destroy(l_t2);
1705 
1706         /*---------------CLEAN-------------------*/
1707         return true;
1708 }
1709 
tcd_t1_decode(opj_tcd_t * p_tcd)1710 bool tcd_t1_decode (
1711                                           opj_tcd_t *p_tcd
1712                                          )
1713 {
1714         OPJ_UINT32 compno;
1715         opj_t1_t * l_t1;
1716         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1717         opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1718         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1719 
1720 
1721         l_t1 = t1_create();
1722         if
1723                 (l_t1 == 00)
1724         {
1725                 return false;
1726         }
1727         for
1728                 (compno = 0; compno < l_tile->numcomps; ++compno)
1729         {
1730                 /* The +3 is headroom required by the vectorized DWT */
1731                 t1_decode_cblks(l_t1, l_tile_comp, l_tccp);
1732                 ++l_tile_comp;
1733                 ++l_tccp;
1734         }
1735         t1_destroy(l_t1);
1736         return true;
1737 }
1738 
tcd_dwt_decode(opj_tcd_t * p_tcd)1739 bool tcd_dwt_decode (
1740                                           opj_tcd_t *p_tcd
1741                                          )
1742 {
1743         OPJ_UINT32 compno;
1744         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1745         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1746         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1747         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1748 
1749         for
1750                 (compno = 0; compno < l_tile->numcomps; compno++)
1751         {
1752                 /*
1753                 if (tcd->cp->reduce != 0) {
1754                         tcd->image->comps[compno].resno_decoded =
1755                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1756                         if (tcd->image->comps[compno].resno_decoded < 0)
1757                         {
1758                                 return false;
1759                         }
1760                 }
1761                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1762                 if(numres2decode > 0){
1763                 */
1764                 if
1765                         (l_tccp->qmfbid == 1)
1766                 {
1767                         if
1768                                 (! dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1))
1769                         {
1770                                 return false;
1771                         }
1772                 }
1773                 else
1774                 {
1775                         if
1776                                 (! dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1))
1777                         {
1778                                 return false;
1779                         }
1780                 }
1781                 ++l_tile_comp;
1782                 ++l_img_comp;
1783                 ++l_tccp;
1784         }
1785         return true;
1786 }
tcd_mct_decode(opj_tcd_t * p_tcd)1787 bool tcd_mct_decode (
1788                                           opj_tcd_t *p_tcd
1789                                          )
1790 {
1791         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1792         opj_tcp_t * l_tcp = p_tcd->tcp;
1793         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1794         OPJ_UINT32 l_samples,i;
1795 
1796         if
1797                 (! l_tcp->mct)
1798         {
1799                 return true;
1800         }
1801         l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
1802         if
1803                 (l_tcp->mct == 2)
1804         {
1805                 OPJ_BYTE ** l_data;
1806                 if
1807                         (! l_tcp->m_mct_decoding_matrix)
1808                 {
1809                         return true;
1810                 }
1811                 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1812                 if
1813                         (! l_data)
1814                 {
1815                         return false;
1816                 }
1817                 for
1818                         (i=0;i<l_tile->numcomps;++i)
1819                 {
1820                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1821                         ++l_tile_comp;
1822                 }
1823                 if
1824                         (! mct_decode_custom(        // MCT data
1825                                                                 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1826                                                                 // size of components
1827                                                                 l_samples,
1828                                                                 // components
1829                                                                 l_data,
1830                                                                 // nb of components (i.e. size of pData)
1831                                                                 l_tile->numcomps,
1832                                                                 // tells if the data is signed
1833                                                                 p_tcd->image->comps->sgnd))
1834                 {
1835                         opj_free(l_data);
1836                         return false;
1837                 }
1838                 opj_free(l_data);
1839         }
1840         else
1841         {
1842                 if
1843                         (l_tcp->tccps->qmfbid == 1)
1844                 {
1845                         mct_decode(
1846                                         l_tile->comps[0].data,
1847                                         l_tile->comps[1].data,
1848                                         l_tile->comps[2].data,
1849                                         l_samples);
1850                 }
1851                 else
1852                 {
1853                         mct_decode_real(
1854                                         (float*)l_tile->comps[0].data,
1855                                         (float*)l_tile->comps[1].data,
1856                                         (float*)l_tile->comps[2].data,
1857                                         l_samples);
1858                 }
1859         }
1860         return true;
1861 }
1862 
tcd_dc_level_shift_decode(opj_tcd_t * p_tcd)1863 bool tcd_dc_level_shift_decode (
1864                                                  opj_tcd_t *p_tcd
1865                                                  )
1866 {
1867         OPJ_UINT32 compno;
1868         opj_tcd_tilecomp_t * l_tile_comp = 00;
1869         opj_tccp_t * l_tccp = 00;
1870         opj_image_comp_t * l_img_comp = 00;
1871         opj_tcd_resolution_t* l_res = 00;
1872         opj_tcd_tile_t * l_tile;
1873         OPJ_UINT32 l_width,l_height,i,j;
1874         OPJ_INT32 * l_current_ptr;
1875         OPJ_INT32 l_min, l_max;
1876         OPJ_UINT32 l_stride;
1877 
1878         l_tile = p_tcd->tcd_image->tiles;
1879         l_tile_comp = l_tile->comps;
1880         l_tccp = p_tcd->tcp->tccps;
1881         l_img_comp = p_tcd->image->comps;
1882 
1883         for
1884                 (compno = 0; compno < l_tile->numcomps; compno++)
1885         {
1886                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1887                 l_width = (l_res->x1 - l_res->x0);
1888                 l_height = (l_res->y1 - l_res->y0);
1889                 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1890                 if
1891                         (l_img_comp->sgnd)
1892                 {
1893                         l_min = -(1 << (l_img_comp->prec - 1));
1894                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
1895                 }
1896                 else
1897                 {
1898             l_min = 0;
1899                         l_max = (1 << l_img_comp->prec) - 1;
1900                 }
1901                 l_current_ptr = l_tile_comp->data;
1902                 if
1903                         (l_tccp->qmfbid == 1)
1904                 {
1905                         for
1906                                 (j=0;j<l_height;++j)
1907                         {
1908                                 for
1909                                         (i = 0; i < l_width; ++i)
1910                                 {
1911                                         *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1912                                         ++l_current_ptr;
1913                                 }
1914                                 l_current_ptr += l_stride;
1915                         }
1916                 }
1917                 else
1918                 {
1919                         for
1920                                 (j=0;j<l_height;++j)
1921                         {
1922                                 for
1923                                         (i = 0; i < l_width; ++i)
1924                                 {
1925                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1926                                         *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1927                                         ++l_current_ptr;
1928                                 }
1929                                 l_current_ptr += l_stride;
1930                         }
1931                 }
1932                 ++l_img_comp;
1933                 ++l_tccp;
1934                 ++l_tile_comp;
1935         }
1936         return true;
1937 }
1938 
tcd_encode_tile(opj_tcd_t * p_tcd,OPJ_UINT32 p_tile_no,OPJ_BYTE * p_dest,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_max_length,opj_codestream_info_t * p_cstr_info)1939 bool tcd_encode_tile(
1940                                          opj_tcd_t *p_tcd,
1941                                          OPJ_UINT32 p_tile_no,
1942                                          OPJ_BYTE *p_dest,
1943                                          OPJ_UINT32 * p_data_written,
1944                                          OPJ_UINT32 p_max_length,
1945                                          opj_codestream_info_t *p_cstr_info)
1946 {
1947 
1948         if
1949                 (p_tcd->cur_tp_num == 0)
1950         {
1951                 p_tcd->tcd_tileno = p_tile_no;
1952                 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1953                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1954                 if(p_cstr_info)
1955                 {
1956                         OPJ_UINT32 l_num_packs = 0;
1957                         OPJ_UINT32 i;
1958                         opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1959                         opj_tccp_t *l_tccp = p_tcd->tcp->tccps;        /* based on component 0 */
1960                         for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1961                                 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1962 
1963                                 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
1964                                 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
1965 
1966                                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1967                                 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
1968                                 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
1969                         }
1970                         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));
1971                 }
1972                 /* << INDEX */
1973                 _ProfStart(PGROUP_DC_SHIFT);
1974                 /*---------------TILE-------------------*/
1975                 if
1976                         (! tcd_dc_level_shift_encode(p_tcd))
1977                 {
1978                         return false;
1979                 }
1980                 _ProfStop(PGROUP_DC_SHIFT);
1981 
1982                 _ProfStart(PGROUP_MCT);
1983                 if
1984                         (! tcd_mct_encode(p_tcd))
1985                 {
1986                         return false;
1987                 }
1988                 _ProfStop(PGROUP_MCT);
1989 
1990                 _ProfStart(PGROUP_DWT);
1991                 if
1992                         (! tcd_dwt_encode(p_tcd))
1993                 {
1994                         return false;
1995                 }
1996                 _ProfStop(PGROUP_DWT);
1997 
1998                 _ProfStart(PGROUP_T1);
1999                 if
2000                         (! tcd_t1_encode(p_tcd))
2001                 {
2002                         return false;
2003                 }
2004                 _ProfStop(PGROUP_T1);
2005 
2006                 _ProfStart(PGROUP_RATE);
2007                 if
2008                         (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info))
2009                 {
2010                         return false;
2011                 }
2012                 _ProfStop(PGROUP_RATE);
2013 
2014         }
2015         /*--------------TIER2------------------*/
2016 
2017         /* INDEX */
2018         if
2019                 (p_cstr_info)
2020         {
2021                 p_cstr_info->index_write = 1;
2022         }
2023         _ProfStart(PGROUP_T2);
2024         if
2025                 (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info))
2026         {
2027                 return false;
2028         }
2029         _ProfStop(PGROUP_T2);
2030         /*---------------CLEAN-------------------*/
2031         return true;
2032 }
2033 
tcd_decode_tile(opj_tcd_t * p_tcd,OPJ_BYTE * p_src,OPJ_UINT32 p_max_length,OPJ_UINT32 p_tile_no,opj_codestream_info_t * p_cstr_info)2034 bool tcd_decode_tile(
2035                                          opj_tcd_t *p_tcd,
2036                                          OPJ_BYTE *p_src,
2037                                          OPJ_UINT32 p_max_length,
2038                                          OPJ_UINT32 p_tile_no,
2039                                          opj_codestream_info_t *p_cstr_info)
2040 {
2041         OPJ_UINT32 l_data_read;
2042         p_tcd->tcd_tileno = p_tile_no;
2043         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2044 
2045         /* INDEX >>  */
2046         if(p_cstr_info) {
2047                 OPJ_UINT32 resno, compno, numprec = 0;
2048                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2049                         opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
2050                         opj_tccp_t *tccp = &tcp->tccps[compno];
2051                         opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2052                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2053                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
2054                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2055                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2056                                 numprec += res_idx->pw * res_idx->ph;
2057                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2058                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2059                         }
2060                 }
2061                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2062                 p_cstr_info->packno = 0;
2063         }
2064         /* << INDEX */
2065 
2066         /*--------------TIER2------------------*/
2067         _ProfStart(PGROUP_T2);
2068         l_data_read = 0;
2069         if
2070                 (! tcd_t2_decode(p_tcd,p_src,&l_data_read,p_max_length,p_cstr_info))
2071         {
2072                 return false;
2073         }
2074         _ProfStop(PGROUP_T2);
2075 
2076         /*------------------TIER1-----------------*/
2077 
2078         _ProfStart(PGROUP_T1);
2079         if
2080                 (! tcd_t1_decode(p_tcd))
2081         {
2082                 return false;
2083         }
2084         _ProfStop(PGROUP_T1);
2085 
2086         /*----------------DWT---------------------*/
2087 
2088         _ProfStart(PGROUP_DWT);
2089         if
2090                 (! tcd_dwt_decode(p_tcd))
2091         {
2092                 return false;
2093         }
2094         _ProfStop(PGROUP_DWT);
2095 
2096         /*----------------MCT-------------------*/
2097         _ProfStart(PGROUP_MCT);
2098         if
2099                 (! tcd_mct_decode(p_tcd))
2100         {
2101                 return false;
2102         }
2103         _ProfStop(PGROUP_MCT);
2104 
2105         _ProfStart(PGROUP_DC_SHIFT);
2106         if
2107                 (! tcd_dc_level_shift_decode(p_tcd))
2108         {
2109                 return false;
2110         }
2111         _ProfStop(PGROUP_DC_SHIFT);
2112 
2113 
2114         /*---------------TILE-------------------*/
2115         return true;
2116 }
2117