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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
37 /** @name Local static functions */
40 static void t2_putcommacode(opj_bio_t *bio, int n);
42 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio);
44 Variable length code for signalling delta Zil (truncation point)
45 @param bio Bit Input/Output component
48 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
49 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
52 Encode a packet of a tile to a destination buffer
53 @param tileno Number of the tile encoded
54 @param tile Tile for which to write the packets
55 @param tcp Tile coding parameters
56 @param pi Packet identity
57 @param dest Destination buffer
58 @param p_data_written FIXME DOC
59 @param len Length of the destination buffer
60 @param cstr_info Codestream information structure
63 static opj_bool opj_t2_encode_packet( OPJ_UINT32 tileno,
66 opj_pi_iterator_t *pi,
68 OPJ_UINT32 * p_data_written,
70 opj_codestream_info_t *cstr_info);
73 Decode a packet of a tile from a source buffer
75 @param tile Tile for which to write the packets
76 @param tcp Tile coding parameters
77 @param pi Packet identity
78 @param src Source buffer
79 @param data_read FIXME DOC
80 @param max_length FIXME DOC
81 @param pack_info Packet information
85 static opj_bool opj_t2_decode_packet( opj_t2_t* t2,
88 opj_pi_iterator_t *pi,
90 OPJ_UINT32 * data_read,
91 OPJ_UINT32 max_length,
92 opj_packet_info_t *pack_info);
94 static opj_bool opj_t2_skip_packet( opj_t2_t* p_t2,
95 opj_tcd_tile_t *p_tile,
97 opj_pi_iterator_t *p_pi,
99 OPJ_UINT32 * p_data_read,
100 OPJ_UINT32 p_max_length,
101 opj_packet_info_t *p_pack_info);
103 static opj_bool opj_t2_read_packet_header( opj_t2_t* p_t2,
104 opj_tcd_tile_t *p_tile,
106 opj_pi_iterator_t *p_pi,
107 opj_bool * p_is_data_present,
108 OPJ_BYTE *p_src_data,
109 OPJ_UINT32 * p_data_read,
110 OPJ_UINT32 p_max_length,
111 opj_packet_info_t *p_pack_info);
113 static opj_bool opj_t2_read_packet_data(opj_t2_t* p_t2,
114 opj_tcd_tile_t *p_tile,
115 opj_pi_iterator_t *p_pi,
116 OPJ_BYTE *p_src_data,
117 OPJ_UINT32 * p_data_read,
118 OPJ_UINT32 p_max_length,
119 opj_packet_info_t *pack_info);
121 static opj_bool opj_t2_skip_packet_data(opj_t2_t* p_t2,
122 opj_tcd_tile_t *p_tile,
123 opj_pi_iterator_t *p_pi,
124 OPJ_UINT32 * p_data_read,
125 OPJ_UINT32 p_max_length,
126 opj_packet_info_t *pack_info);
134 static opj_bool opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
143 /* ----------------------------------------------------------------------- */
145 /* #define RESTART 0x04 */
147 static void t2_putcommacode(opj_bio_t *bio, int n) {
149 opj_bio_write(bio, 1, 1);
151 opj_bio_write(bio, 0, 1);
154 OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
157 while (opj_bio_read(bio, 1)) {
163 void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) {
165 opj_bio_write(bio, 0, 1);
167 opj_bio_write(bio, 2, 2);
169 opj_bio_write(bio, 0xc | (n - 3), 4);
170 } else if (n <= 36) {
171 opj_bio_write(bio, 0x1e0 | (n - 6), 9);
172 } else if (n <= 164) {
173 opj_bio_write(bio, 0xff80 | (n - 37), 16);
177 OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) {
179 if (!opj_bio_read(bio, 1))
181 if (!opj_bio_read(bio, 1))
183 if ((n = opj_bio_read(bio, 2)) != 3)
185 if ((n = opj_bio_read(bio, 5)) != 31)
187 return (37 + opj_bio_read(bio, 7));
190 /* ----------------------------------------------------------------------- */
192 opj_bool opj_t2_encode_packets( opj_t2_t* p_t2,
193 OPJ_UINT32 p_tile_no,
194 opj_tcd_tile_t *p_tile,
195 OPJ_UINT32 p_maxlayers,
197 OPJ_UINT32 * p_data_written,
198 OPJ_UINT32 p_max_len,
199 opj_codestream_info_t *cstr_info,
203 J2K_T2_MODE p_t2_mode)
205 OPJ_BYTE *l_current_data = p_dest;
206 OPJ_UINT32 l_nb_bytes = 0;
209 opj_pi_iterator_t *l_pi = 00;
210 opj_pi_iterator_t *l_current_pi = 00;
211 opj_image_t *l_image = p_t2->image;
212 opj_cp_t *l_cp = p_t2->cp;
213 opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
214 OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1;
215 OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
216 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
218 l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
223 * p_data_written = 0;
225 if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
228 for (compno = 0; compno < l_max_comp; ++compno) {
229 OPJ_UINT32 l_comp_len = 0;
232 for (poc = 0; poc < pocno ; ++poc) {
233 OPJ_UINT32 l_tp_num = compno;
235 /* TODO MSD : check why this function cannot fail (cf. v1) */
236 opj_pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
238 while (opj_pi_next(l_current_pi)) {
239 if (l_current_pi->layno < p_maxlayers) {
242 if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
243 opj_pi_destroy(l_pi, l_nb_pocs);
247 l_comp_len += l_nb_bytes;
248 l_current_data += l_nb_bytes;
249 p_max_len -= l_nb_bytes;
251 * p_data_written += l_nb_bytes;
255 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
256 if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
257 opj_pi_destroy(l_pi, l_nb_pocs);
266 else { /* t2_mode == FINAL_PASS */
267 opj_pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
269 l_current_pi = &l_pi[p_pino];
271 while (opj_pi_next(l_current_pi)) {
272 if (l_current_pi->layno < p_maxlayers) {
275 if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
276 opj_pi_destroy(l_pi, l_nb_pocs);
280 l_current_data += l_nb_bytes;
281 p_max_len -= l_nb_bytes;
283 * p_data_written += l_nb_bytes;
287 if(cstr_info->index_write) {
288 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
289 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
290 if (!cstr_info->packno) {
291 info_PK->start_pos = info_TL->end_header + 1;
293 info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
295 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
296 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
297 to start of packet is incremented by value of start of packet*/
308 opj_pi_destroy(l_pi, l_nb_pocs);
313 opj_bool opj_t2_decode_packets( opj_t2_t *p_t2,
314 OPJ_UINT32 p_tile_no,
315 opj_tcd_tile_t *p_tile,
317 OPJ_UINT32 * p_data_read,
318 OPJ_UINT32 p_max_len,
319 opj_codestream_index_t *p_cstr_index)
321 OPJ_BYTE *l_current_data = p_src;
322 opj_pi_iterator_t *l_pi = 00;
324 opj_image_t *l_image = p_t2->image;
325 opj_cp_t *l_cp = p_t2->cp;
326 opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
327 OPJ_UINT32 l_nb_bytes_read;
328 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
329 opj_pi_iterator_t *l_current_pi = 00;
331 OPJ_UINT32 curtp = 0;
332 OPJ_UINT32 tp_start_packno;
334 opj_packet_info_t *l_pack_info = 00;
335 opj_image_comp_t* l_img_comp = 00;
337 OPJ_ARG_NOT_USED(p_cstr_index);
341 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
345 /* create a packet iterator */
346 l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
354 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
356 /* if the resolution needed is to low, one dim of the tilec could be equal to zero
357 * and no packets are used to encode this resolution and
358 * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
359 * and no l_img_comp->resno_decoded are computed
361 opj_bool* first_pass_failed = (opj_bool*)opj_malloc(l_image->numcomps * sizeof(opj_bool));
362 if (!first_pass_failed)
364 opj_pi_destroy(l_pi,l_nb_pocs);
367 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(opj_bool));
369 while (opj_pi_next(l_current_pi)) {
372 if (l_tcp->num_layers_to_decode > l_current_pi->layno
373 && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
376 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
378 if (! opj_t2_decode_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
379 opj_pi_destroy(l_pi,l_nb_pocs);
380 opj_free(first_pass_failed);
384 l_img_comp = &(l_image->comps[l_current_pi->compno]);
385 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
389 if (! opj_t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
390 opj_pi_destroy(l_pi,l_nb_pocs);
391 opj_free(first_pass_failed);
396 if (first_pass_failed[l_current_pi->compno]) {
397 l_img_comp = &(l_image->comps[l_current_pi->compno]);
398 if (l_img_comp->resno_decoded == 0)
399 l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
402 l_current_data += l_nb_bytes_read;
403 p_max_len -= l_nb_bytes_read;
408 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
409 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
411 if (!p_cstr_info->packno) {
412 info_PK->start_pos = info_TL->end_header + 1;
413 } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ /* New tile part */
414 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
415 tp_start_packno = p_cstr_info->packno;
417 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
419 info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1;
421 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
422 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */
423 ++p_cstr_info->packno;
430 opj_free(first_pass_failed);
436 p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part */
441 /* don't forget to release pi */
442 opj_pi_destroy(l_pi,l_nb_pocs);
443 *p_data_read = l_current_data - p_src;
447 /* ----------------------------------------------------------------------- */
450 * Creates a Tier 2 handle
452 * @param p_image Source or destination image
453 * @param p_cp Image coding parameters.
454 * @return a new T2 handle if successful, NULL otherwise.
456 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
458 /* create the t2 structure */
459 opj_t2_t *l_t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
463 memset(l_t2,0,sizeof(opj_t2_t));
465 l_t2->image = p_image;
471 void opj_t2_destroy(opj_t2_t *t2) {
477 opj_bool opj_t2_decode_packet( opj_t2_t* p_t2,
478 opj_tcd_tile_t *p_tile,
480 opj_pi_iterator_t *p_pi,
482 OPJ_UINT32 * p_data_read,
483 OPJ_UINT32 p_max_length,
484 opj_packet_info_t *p_pack_info)
486 opj_bool l_read_data;
487 OPJ_UINT32 l_nb_bytes_read = 0;
488 OPJ_UINT32 l_nb_total_bytes_read = 0;
492 if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
496 p_src += l_nb_bytes_read;
497 l_nb_total_bytes_read += l_nb_bytes_read;
498 p_max_length -= l_nb_bytes_read;
500 /* we should read data for the packet */
504 if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
508 l_nb_total_bytes_read += l_nb_bytes_read;
511 *p_data_read = l_nb_total_bytes_read;
516 opj_bool opj_t2_encode_packet( OPJ_UINT32 tileno,
517 opj_tcd_tile_t * tile,
519 opj_pi_iterator_t *pi,
521 OPJ_UINT32 * p_data_written,
523 opj_codestream_info_t *cstr_info)
525 OPJ_UINT32 bandno, cblkno;
527 OPJ_UINT32 l_nb_bytes;
528 OPJ_UINT32 compno = pi->compno; /* component value */
529 OPJ_UINT32 resno = pi->resno; /* resolution level value */
530 OPJ_UINT32 precno = pi->precno; /* precinct value */
531 OPJ_UINT32 layno = pi->layno; /* quality layer value */
532 OPJ_UINT32 l_nb_blocks;
533 opj_tcd_band_t *band = 00;
534 opj_tcd_cblk_enc_t* cblk = 00;
535 opj_tcd_pass_t *pass = 00;
537 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
538 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
540 opj_bio_t *bio = 00; /* BIO component */
543 if (tcp->csty & J2K_CP_CSTY_SOP) {
548 c[4] = (tile->packno % 65536) / 256;
549 c[5] = (tile->packno % 65536) % 256;
558 for(bandno = 0; bandno < res->numbands; ++bandno) {
559 opj_tcd_precinct_t *prc = &band->precincts[precno];
561 opj_tgt_reset(prc->incltree);
562 opj_tgt_reset(prc->imsbtree);
564 l_nb_blocks = prc->cw * prc->ch;
565 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
566 cblk = &prc->cblks.enc[cblkno];
569 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
575 bio = opj_bio_create();
576 opj_bio_init_enc(bio, c, length);
577 opj_bio_write(bio, 1, 1); /* Empty header bit */
579 /* Writing Packet header */
581 for (bandno = 0; bandno < res->numbands; ++bandno) {
582 opj_tcd_precinct_t *prc = &band->precincts[precno];
584 l_nb_blocks = prc->cw * prc->ch;
585 cblk = prc->cblks.enc;
587 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
588 opj_tcd_layer_t *layer = &cblk->layers[layno];
590 if (!cblk->numpasses && layer->numpasses) {
591 opj_tgt_setvalue(prc->incltree, cblkno, layno);
597 cblk = prc->cblks.enc;
598 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
599 opj_tcd_layer_t *layer = &cblk->layers[layno];
600 OPJ_UINT32 increment = 0;
602 OPJ_UINT32 len = 0, passno;
603 OPJ_UINT32 l_nb_passes;
605 /* cblk inclusion bits */
606 if (!cblk->numpasses) {
607 opj_tgt_encode(bio, prc->incltree, cblkno, layno + 1);
609 opj_bio_write(bio, layer->numpasses != 0, 1);
612 /* if cblk not included, go to the next cblk */
613 if (!layer->numpasses) {
618 /* if first instance of cblk --> zero bit-planes information */
619 if (!cblk->numpasses) {
620 cblk->numlenbits = 3;
621 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
624 /* number of coding passes included */
625 opj_t2_putnumpasses(bio, layer->numpasses);
626 l_nb_passes = cblk->numpasses + layer->numpasses;
627 pass = cblk->passes + cblk->numpasses;
629 /* computation of the increase of the length indicator and insertion in the header */
630 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
634 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
635 increment = opj_int_max(increment, opj_int_floorlog2(len) + 1 - (cblk->numlenbits + opj_int_floorlog2(nump)));
642 t2_putcommacode(bio, increment);
644 /* computation of the new Length indicator */
645 cblk->numlenbits += increment;
647 pass = cblk->passes + cblk->numpasses;
648 /* insertion of the codeword segment length */
649 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
653 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
654 opj_bio_write(bio, len, cblk->numlenbits + opj_int_floorlog2(nump));
667 if (!opj_bio_flush(bio)) {
668 opj_bio_destroy(bio);
669 return OPJ_FALSE; /* modified to eliminate longjmp !! */
672 l_nb_bytes = opj_bio_numbytes(bio);
674 length -= l_nb_bytes;
676 opj_bio_destroy(bio);
679 if (tcp->csty & J2K_CP_CSTY_EPH) {
688 /* End of packet header position. Currently only represents the distance to start of packet
689 Will be updated later by incrementing with packet start value*/
690 if(cstr_info && cstr_info->index_write) {
691 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
692 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
696 /* Writing the packet body */
698 for (bandno = 0; bandno < res->numbands; bandno++) {
699 opj_tcd_precinct_t *prc = &band->precincts[precno];
701 l_nb_blocks = prc->cw * prc->ch;
702 cblk = prc->cblks.enc;
704 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
705 opj_tcd_layer_t *layer = &cblk->layers[layno];
707 if (!layer->numpasses) {
712 if (layer->len > length) {
716 memcpy(c, layer->data, layer->len);
717 cblk->numpasses += layer->numpasses;
719 length -= layer->len;
722 if(cstr_info && cstr_info->index_write) {
723 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
724 info_PK->disto += layer->disto;
725 if (cstr_info->D_max < info_PK->disto) {
726 cstr_info->D_max = info_PK->disto;
736 * p_data_written += (c - dest);
741 static opj_bool opj_t2_skip_packet( opj_t2_t* p_t2,
742 opj_tcd_tile_t *p_tile,
744 opj_pi_iterator_t *p_pi,
746 OPJ_UINT32 * p_data_read,
747 OPJ_UINT32 p_max_length,
748 opj_packet_info_t *p_pack_info)
750 opj_bool l_read_data;
751 OPJ_UINT32 l_nb_bytes_read = 0;
752 OPJ_UINT32 l_nb_total_bytes_read = 0;
756 if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
760 p_src += l_nb_bytes_read;
761 l_nb_total_bytes_read += l_nb_bytes_read;
762 p_max_length -= l_nb_bytes_read;
764 /* we should read data for the packet */
768 if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
772 l_nb_total_bytes_read += l_nb_bytes_read;
774 *p_data_read = l_nb_total_bytes_read;
781 opj_bool opj_t2_read_packet_header( opj_t2_t* p_t2,
782 opj_tcd_tile_t *p_tile,
784 opj_pi_iterator_t *p_pi,
785 opj_bool * p_is_data_present,
786 OPJ_BYTE *p_src_data,
787 OPJ_UINT32 * p_data_read,
788 OPJ_UINT32 p_max_length,
789 opj_packet_info_t *p_pack_info)
793 OPJ_UINT32 bandno, cblkno;
794 OPJ_UINT32 l_nb_code_blocks;
795 OPJ_UINT32 l_remaining_length;
796 OPJ_UINT32 l_header_length;
797 OPJ_UINT32 * l_modified_length_ptr = 00;
798 OPJ_BYTE *l_current_data = p_src_data;
799 opj_cp_t *l_cp = p_t2->cp;
800 opj_bio_t *l_bio = 00; /* BIO component */
801 opj_tcd_band_t *l_band = 00;
802 opj_tcd_cblk_dec_t* l_cblk = 00;
803 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
805 OPJ_BYTE *l_header_data = 00;
806 OPJ_BYTE **l_header_data_start = 00;
808 OPJ_UINT32 l_present;
810 if (p_pi->layno == 0) {
811 l_band = l_res->bands;
814 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
815 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
817 if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
818 opj_tgt_reset(l_prc->incltree);
819 opj_tgt_reset(l_prc->imsbtree);
820 l_cblk = l_prc->cblks.dec;
822 l_nb_code_blocks = l_prc->cw * l_prc->ch;
823 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
825 l_cblk->real_num_segs = 0;
836 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
837 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
838 /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
843 /** TODO : check the Nsop value */
847 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
848 This part deal with this caracteristic
849 step 1: Read packet header in the saved structure
850 step 2: Return to codestream for decoding
853 l_bio = opj_bio_create();
858 if (l_cp->ppm == 1) { /* PPM */
859 l_header_data_start = &l_cp->ppm_data;
860 l_header_data = *l_header_data_start;
861 l_modified_length_ptr = &(l_cp->ppm_len);
864 else if (p_tcp->ppt == 1) { /* PPT */
865 l_header_data_start = &(p_tcp->ppt_data);
866 l_header_data = *l_header_data_start;
867 l_modified_length_ptr = &(p_tcp->ppt_len);
869 else { /* Normal Case */
870 l_header_data_start = &(l_current_data);
871 l_header_data = *l_header_data_start;
872 l_remaining_length = p_src_data+p_max_length-l_header_data;
873 l_modified_length_ptr = &(l_remaining_length);
876 opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
878 l_present = opj_bio_read(l_bio, 1);
880 /* TODO MSD: no test to control the output of this function*/
881 opj_bio_inalign(l_bio);
882 l_header_data += opj_bio_numbytes(l_bio);
883 opj_bio_destroy(l_bio);
886 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
887 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
888 printf("Error : expected EPH marker\n");
894 l_header_length = (l_header_data - *l_header_data_start);
895 *l_modified_length_ptr -= l_header_length;
896 *l_header_data_start += l_header_length;
899 /* End of packet header position. Currently only represents the distance to start of packet
900 Will be updated later by incrementing with packet start value */
902 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
906 * p_is_data_present = OPJ_FALSE;
907 *p_data_read = l_current_data - p_src_data;
911 l_band = l_res->bands;
912 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
913 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
915 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
920 l_nb_code_blocks = l_prc->cw * l_prc->ch;
921 l_cblk = l_prc->cblks.dec;
922 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
923 OPJ_UINT32 l_included,l_increment, l_segno;
926 /* if cblk not yet included before --> inclusion tagtree */
927 if (!l_cblk->numsegs) {
928 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
932 l_included = opj_bio_read(l_bio, 1);
935 /* if cblk not included */
937 l_cblk->numnewpasses = 0;
942 /* if cblk not yet included --> zero-bitplane tagtree */
943 if (!l_cblk->numsegs) {
946 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) {
950 l_cblk->numbps = l_band->numbps + 1 - i;
951 l_cblk->numlenbits = 3;
954 /* number of coding passes */
955 l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
956 l_increment = opj_t2_getcommacode(l_bio);
958 /* length indicator increment */
959 l_cblk->numlenbits += l_increment;
962 if (!l_cblk->numsegs) {
963 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
964 opj_bio_destroy(l_bio);
969 l_segno = l_cblk->numsegs - 1;
970 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
972 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
973 opj_bio_destroy(l_bio);
978 n = l_cblk->numnewpasses;
981 l_cblk->segs[l_segno].numnewpasses = opj_int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
982 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
984 n -= l_cblk->segs[l_segno].numnewpasses;
988 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
989 opj_bio_destroy(l_bio);
1001 if (!opj_bio_inalign(l_bio)) {
1002 opj_bio_destroy(l_bio);
1006 l_header_data += opj_bio_numbytes(l_bio);
1007 opj_bio_destroy(l_bio);
1010 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1011 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1012 /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1018 l_header_length = (l_header_data - *l_header_data_start);
1019 *l_modified_length_ptr -= l_header_length;
1020 *l_header_data_start += l_header_length;
1023 /* End of packet header position. Currently only represents the distance to start of packet
1024 Will be updated later by incrementing with packet start value */
1026 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1030 *p_is_data_present = OPJ_TRUE;
1031 *p_data_read = l_current_data - p_src_data;
1036 opj_bool opj_t2_read_packet_data( opj_t2_t* p_t2,
1037 opj_tcd_tile_t *p_tile,
1038 opj_pi_iterator_t *p_pi,
1039 OPJ_BYTE *p_src_data,
1040 OPJ_UINT32 * p_data_read,
1041 OPJ_UINT32 p_max_length,
1042 opj_packet_info_t *pack_info)
1044 OPJ_UINT32 bandno, cblkno;
1045 OPJ_UINT32 l_nb_code_blocks;
1046 OPJ_BYTE *l_current_data = p_src_data;
1047 opj_tcd_band_t *l_band = 00;
1048 opj_tcd_cblk_dec_t* l_cblk = 00;
1049 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1051 OPJ_ARG_NOT_USED(p_t2);
1052 OPJ_ARG_NOT_USED(pack_info);
1054 l_band = l_res->bands;
1055 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1056 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1058 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1063 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1064 l_cblk = l_prc->cblks.dec;
1066 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1067 opj_tcd_seg_t *l_seg = 00;
1069 if (!l_cblk->numnewpasses) {
1075 if (!l_cblk->numsegs) {
1076 l_seg = l_cblk->segs;
1081 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1083 if (l_seg->numpasses == l_seg->maxpasses) {
1090 if (l_current_data + l_seg->newlen > p_src_data + p_max_length) {
1095 /* we need here a j2k handle to verify if making a check to
1096 the validity of cblocks parameters is selected from user (-W) */
1098 /* let's check that we are not exceeding */
1099 if ((l_cblk->len + l_seg->newlen) > 8192) {
1100 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1101 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1102 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1104 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1107 l_seg->newlen = 8192 - l_cblk->len;
1108 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1112 #endif /* USE_JPWL */
1114 if ((l_cblk->len + l_seg->newlen) > 8192) {
1118 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
1120 if (l_seg->numpasses == 0) {
1121 l_seg->data = &l_cblk->data;
1122 l_seg->dataindex = l_cblk->len;
1125 l_current_data += l_seg->newlen;
1126 l_seg->numpasses += l_seg->numnewpasses;
1127 l_cblk->numnewpasses -= l_seg->numnewpasses;
1129 l_seg->real_num_passes = l_seg->numpasses;
1130 l_cblk->len += l_seg->newlen;
1131 l_seg->len += l_seg->newlen;
1133 if (l_cblk->numnewpasses > 0) {
1137 } while (l_cblk->numnewpasses > 0);
1139 l_cblk->real_num_segs = l_cblk->numsegs;
1146 *(p_data_read) = l_current_data - p_src_data;
1151 opj_bool opj_t2_skip_packet_data( opj_t2_t* p_t2,
1152 opj_tcd_tile_t *p_tile,
1153 opj_pi_iterator_t *p_pi,
1154 OPJ_UINT32 * p_data_read,
1155 OPJ_UINT32 p_max_length,
1156 opj_packet_info_t *pack_info)
1158 OPJ_UINT32 bandno, cblkno;
1159 OPJ_UINT32 l_nb_code_blocks;
1160 opj_tcd_band_t *l_band = 00;
1161 opj_tcd_cblk_dec_t* l_cblk = 00;
1162 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1164 OPJ_ARG_NOT_USED(p_t2);
1165 OPJ_ARG_NOT_USED(pack_info);
1168 l_band = l_res->bands;
1170 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1171 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1173 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1178 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1179 l_cblk = l_prc->cblks.dec;
1181 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1182 opj_tcd_seg_t *l_seg = 00;
1184 if (!l_cblk->numnewpasses) {
1190 if (!l_cblk->numsegs) {
1191 l_seg = l_cblk->segs;
1196 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1198 if (l_seg->numpasses == l_seg->maxpasses) {
1205 if (* p_data_read + l_seg->newlen > p_max_length) {
1210 /* we need here a j2k handle to verify if making a check to
1211 the validity of cblocks parameters is selected from user (-W) */
1213 /* let's check that we are not exceeding */
1214 if ((l_cblk->len + l_seg->newlen) > 8192) {
1215 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1216 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1217 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1219 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1222 l_seg->newlen = 8192 - l_cblk->len;
1223 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1227 #endif /* USE_JPWL */
1228 *(p_data_read) += l_seg->newlen;
1230 l_seg->numpasses += l_seg->numnewpasses;
1231 l_cblk->numnewpasses -= l_seg->numnewpasses;
1232 if (l_cblk->numnewpasses > 0)
1237 } while (l_cblk->numnewpasses > 0);
1249 opj_bool opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
1254 opj_tcd_seg_t* seg = 00;
1255 OPJ_UINT32 l_nb_segs = index + 1;
1257 if (l_nb_segs > cblk->m_current_max_segs) {
1258 opj_tcd_seg_t* new_segs;
1259 cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
1261 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1263 opj_free(cblk->segs);
1265 cblk->m_current_max_segs = 0;
1266 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1269 cblk->segs = new_segs;
1272 seg = &cblk->segs[index];
1273 memset(seg,0,sizeof(opj_tcd_seg_t));
1275 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1278 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1280 seg->maxpasses = 10;
1282 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1285 seg->maxpasses = 109;