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) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
42 #include "opj_malloc.h"
46 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
49 /** @name Local static functions */
52 static void t2_putcommacode(opj_bio_t *bio, OPJ_UINT32 n);
53 static OPJ_UINT32 t2_getcommacode(opj_bio_t *bio);
55 Variable length code for signalling delta Zil (truncation point)
56 @param bio Bit Input/Output component
59 static void t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
60 static OPJ_UINT32 t2_getnumpasses(opj_bio_t *bio);
62 Encode a packet of a tile to a destination buffer
63 @param tile Tile for which to write the packets
64 @param tcp Tile coding parameters
65 @param pi Packet identity
66 @param dest Destination buffer
67 @param len Length of the destination buffer
68 @param cstr_info Codestream information structure
69 @param tileno Number of the tile encoded
72 static bool t2_encode_packet(
76 opj_pi_iterator_t *pi,
78 OPJ_UINT32 * p_data_written,
80 opj_codestream_info_t *cstr_info);
86 static bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first);
88 Decode a packet of a tile from a source buffer
90 @param src Source buffer
91 @param len Length of the source buffer
92 @param tile Tile for which to write the packets
93 @param tcp Tile coding parameters
94 @param pi Packet identity
97 static bool t2_decode_packet(
99 opj_tcd_tile_t *p_tile,
101 opj_pi_iterator_t *p_pi,
103 OPJ_UINT32 * p_data_read,
104 OPJ_UINT32 p_max_length,
105 opj_packet_info_t *p_pack_info);
111 /* ----------------------------------------------------------------------- */
113 /* #define RESTART 0x04 */
115 static void t2_putcommacode(opj_bio_t *bio, OPJ_UINT32 n) {
119 bio_write(bio, 1, 1);
121 bio_write(bio, 0, 1);
124 static OPJ_UINT32 t2_getcommacode(opj_bio_t *bio) {
134 static void t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) {
136 bio_write(bio, 0, 1);
138 bio_write(bio, 2, 2);
140 bio_write(bio, 0xc | (n - 3), 4);
141 } else if (n <= 36) {
142 bio_write(bio, 0x1e0 | (n - 6), 9);
143 } else if (n <= 164) {
144 bio_write(bio, 0xff80 | (n - 37), 16);
148 static OPJ_UINT32 t2_getnumpasses(opj_bio_t *bio) {
150 if (!bio_read(bio, 1))
152 if (!bio_read(bio, 1))
154 if ((n = bio_read(bio, 2)) != 3)
156 if ((n = bio_read(bio, 5)) != 31)
158 return (37 + bio_read(bio, 7));
161 static bool t2_encode_packet(
163 opj_tcd_tile_t * tile,
165 opj_pi_iterator_t *pi,
167 OPJ_UINT32 * p_data_written,
169 opj_codestream_info_t *cstr_info)
171 OPJ_UINT32 bandno, cblkno;
173 OPJ_UINT32 l_nb_bytes;
174 OPJ_UINT32 compno = pi->compno; /* component value */
175 OPJ_UINT32 resno = pi->resno; /* resolution level value */
176 OPJ_UINT32 precno = pi->precno; /* precinct value */
177 OPJ_UINT32 layno = pi->layno; /* quality layer value */
178 OPJ_UINT32 l_nb_blocks;
179 opj_tcd_band_t *band = 00;
180 opj_tcd_cblk_enc_t* cblk = 00;
181 opj_tcd_pass_t *pass = 00;
183 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
184 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
186 opj_bio_t *bio = 00; /* BIO component */
189 if (tcp->csty & J2K_CP_CSTY_SOP) {
194 c[4] = (tile->packno % 65536) / 256;
195 c[5] = (tile->packno % 65536) % 256;
204 (bandno = 0; bandno < res->numbands; ++bandno)
206 opj_tcd_precinct_t *prc = &band->precincts[precno];
207 tgt_reset(prc->incltree);
208 tgt_reset(prc->imsbtree);
209 l_nb_blocks = prc->cw * prc->ch;
211 (cblkno = 0; cblkno < l_nb_blocks; ++cblkno)
213 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
215 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
222 bio_init_enc(bio, c, length);
223 bio_write(bio, 1, 1); /* Empty header bit */
225 /* Writing Packet header */
228 (bandno = 0; bandno < res->numbands; ++bandno)
230 opj_tcd_precinct_t *prc = &band->precincts[precno];
231 l_nb_blocks = prc->cw * prc->ch;
232 cblk = prc->cblks.enc;
233 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno)
235 opj_tcd_layer_t *layer = &cblk->layers[layno];
237 (!cblk->numpasses && layer->numpasses)
239 tgt_setvalue(prc->incltree, cblkno, layno);
243 cblk = prc->cblks.enc;
245 (cblkno = 0; cblkno < l_nb_blocks; cblkno++)
247 opj_tcd_layer_t *layer = &cblk->layers[layno];
248 OPJ_UINT32 increment = 0;
250 OPJ_UINT32 len = 0, passno;
251 OPJ_UINT32 l_nb_passes;
252 /* cblk inclusion bits */
253 if (!cblk->numpasses) {
254 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
256 bio_write(bio, layer->numpasses != 0, 1);
258 /* if cblk not included, go to the next cblk */
265 /* if first instance of cblk --> zero bit-planes information */
269 cblk->numlenbits = 3;
270 tgt_encode(bio, prc->imsbtree, cblkno, 999);
272 /* number of coding passes included */
273 t2_putnumpasses(bio, layer->numpasses);
274 l_nb_passes = cblk->numpasses + layer->numpasses;
275 pass = cblk->passes + cblk->numpasses;
276 /* computation of the increase of the length indicator and insertion in the header */
278 (passno = cblk->numpasses; passno < l_nb_passes; ++passno)
283 (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1)
285 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
291 t2_putcommacode(bio, increment);
293 /* computation of the new Length indicator */
294 cblk->numlenbits += increment;
296 pass = cblk->passes + cblk->numpasses;
297 /* insertion of the codeword segment length */
299 (passno = cblk->numpasses; passno < l_nb_passes; ++passno)
304 (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1)
306 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
321 return false; /* modified to eliminate longjmp !! */
323 l_nb_bytes = bio_numbytes(bio);
325 length -= l_nb_bytes;
329 if (tcp->csty & J2K_CP_CSTY_EPH) {
338 // End of packet header position. Currently only represents the distance to start of packet
339 // Will be updated later by incrementing with packet start value
340 if(cstr_info && cstr_info->index_write) {
341 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
342 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
346 /* Writing the packet body */
349 (bandno = 0; bandno < res->numbands; bandno++)
351 opj_tcd_precinct_t *prc = &band->precincts[precno];
352 l_nb_blocks = prc->cw * prc->ch;
353 cblk = prc->cblks.enc;
355 (cblkno = 0; cblkno < l_nb_blocks; ++cblkno)
357 opj_tcd_layer_t *layer = &cblk->layers[layno];
365 (layer->len > length)
369 memcpy(c, layer->data, layer->len);
370 cblk->numpasses += layer->numpasses;
372 length -= layer->len;
374 if(cstr_info && cstr_info->index_write) {
375 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
376 info_PK->disto += layer->disto;
377 if (cstr_info->D_max < info_PK->disto) {
378 cstr_info->D_max = info_PK->disto;
386 * p_data_written += (c - dest);
390 static bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first)
392 opj_tcd_seg_t* seg = 00;
393 OPJ_UINT32 l_nb_segs = index + 1;
396 (l_nb_segs > cblk->m_current_max_segs)
398 cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS;
399 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
406 seg = &cblk->segs[index];
407 memset(seg,0,sizeof(opj_tcd_seg_t));
409 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
412 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
416 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
419 seg->maxpasses = 109;
424 static bool t2_read_packet_header(
426 opj_tcd_tile_t *p_tile,
428 opj_pi_iterator_t *p_pi,
429 bool * p_is_data_present,
430 OPJ_BYTE *p_src_data,
431 OPJ_UINT32 * p_data_read,
432 OPJ_UINT32 p_max_length,
433 opj_packet_info_t *p_pack_info)
436 OPJ_UINT32 bandno, cblkno;
437 OPJ_UINT32 l_nb_code_blocks;
438 OPJ_UINT32 l_remaining_length;
439 OPJ_UINT32 l_header_length;
440 OPJ_UINT32 * l_modified_length_ptr = 00;
441 OPJ_BYTE *l_current_data = p_src_data;
442 opj_cp_t *l_cp = p_t2->cp;
443 opj_bio_t *l_bio = 00; /* BIO component */
444 opj_tcd_band_t *l_band = 00;
445 opj_tcd_cblk_dec_t* l_cblk = 00;
446 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
448 OPJ_BYTE *l_header_data = 00;
449 OPJ_BYTE **l_header_data_start = 00;
451 OPJ_UINT32 l_present;
456 l_band = l_res->bands;
459 (bandno = 0; bandno < l_res->numbands; ++bandno)
461 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
464 ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)))
466 tgt_reset(l_prc->incltree);
467 tgt_reset(l_prc->imsbtree);
468 l_cblk = l_prc->cblks.dec;
469 l_nb_code_blocks = l_prc->cw * l_prc->ch;
471 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
474 l_cblk->real_num_segs = 0;
484 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
485 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
486 // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n");
491 /** TODO : check the Nsop value */
495 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
496 This part deal with this caracteristic
497 step 1: Read packet header in the saved structure
498 step 2: Return to codestream for decoding
501 l_bio = bio_create();
511 l_header_data_start = &l_cp->ppm_data;
512 l_header_data = *l_header_data_start;
513 l_modified_length_ptr = &(l_cp->ppm_len);
519 l_header_data_start = &(p_tcp->ppt_data);
520 l_header_data = *l_header_data_start;
521 l_modified_length_ptr = &(p_tcp->ppt_len);
525 l_header_data_start = &(l_current_data);
526 l_header_data = *l_header_data_start;
527 l_remaining_length = p_src_data+p_max_length-l_header_data;
528 l_modified_length_ptr = &(l_remaining_length);
530 bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
531 l_present = bio_read(l_bio, 1);
536 l_header_data += bio_numbytes(l_bio);
539 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
540 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
541 printf("Error : expected EPH marker\n");
546 l_header_length = (l_header_data - *l_header_data_start);
547 *l_modified_length_ptr -= l_header_length;
548 *l_header_data_start += l_header_length;
550 // End of packet header position. Currently only represents the distance to start of packet
551 // Will be updated later by incrementing with packet start value
555 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
558 * p_is_data_present = false;
559 *p_data_read = l_current_data - p_src_data;
563 l_band = l_res->bands;
565 (bandno = 0; bandno < l_res->numbands; ++bandno)
567 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
569 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
574 l_nb_code_blocks = l_prc->cw * l_prc->ch;
575 l_cblk = l_prc->cblks.dec;
577 (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++)
579 OPJ_UINT32 l_included,l_increment, l_segno;
581 /* if cblk not yet included before --> inclusion tagtree */
585 l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
590 l_included = bio_read(l_bio, 1);
592 /* if cblk not included */
596 l_cblk->numnewpasses = 0;
600 /* if cblk not yet included --> zero-bitplane tagtree */
606 (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i))
610 l_cblk->numbps = l_band->numbps + 1 - i;
611 l_cblk->numlenbits = 3;
613 /* number of coding passes */
614 l_cblk->numnewpasses = t2_getnumpasses(l_bio);
615 l_increment = t2_getcommacode(l_bio);
616 /* length indicator increment */
617 l_cblk->numlenbits += l_increment;
623 (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1))
632 l_segno = l_cblk->numsegs - 1;
634 (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses)
638 (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0))
645 n = l_cblk->numnewpasses;
648 l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
649 l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
650 n -= l_cblk->segs[l_segno].numnewpasses;
656 (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0))
676 l_header_data += bio_numbytes(l_bio);
680 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
681 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
682 // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n");
689 l_header_length = (l_header_data - *l_header_data_start);
690 *l_modified_length_ptr -= l_header_length;
691 *l_header_data_start += l_header_length;
693 // End of packet header position. Currently only represents the distance to start of packet
694 // Will be updated later by incrementing with packet start value
698 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
701 * p_is_data_present = true;
702 *p_data_read = l_current_data - p_src_data;
706 static bool t2_read_packet_data(
708 opj_tcd_tile_t *p_tile,
709 opj_pi_iterator_t *p_pi,
710 OPJ_BYTE *p_src_data,
711 OPJ_UINT32 * p_data_read,
712 OPJ_UINT32 p_max_length,
713 opj_packet_info_t *pack_info)
715 OPJ_UINT32 bandno, cblkno;
716 OPJ_UINT32 l_nb_code_blocks;
717 OPJ_BYTE *l_current_data = p_src_data;
718 opj_tcd_band_t *l_band = 00;
719 opj_tcd_cblk_dec_t* l_cblk = 00;
720 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
722 l_band = l_res->bands;
724 (bandno = 0; bandno < l_res->numbands; ++bandno)
726 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
729 ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
734 l_nb_code_blocks = l_prc->cw * l_prc->ch;
735 l_cblk = l_prc->cblks.dec;
737 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
739 opj_tcd_seg_t *l_seg = 00;
741 (!l_cblk->numnewpasses)
750 l_seg = l_cblk->segs;
756 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
758 (l_seg->numpasses == l_seg->maxpasses)
768 (l_current_data + l_seg->newlen > p_src_data + p_max_length)
774 /* we need here a j2k handle to verify if making a check to
775 the validity of cblocks parameters is selected from user (-W) */
777 /* let's check that we are not exceeding */
778 if ((cblk->len + seg->newlen) > 8192) {
779 opj_event_msg(t2->cinfo, EVT_WARNING,
780 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
781 seg->newlen, cblkno, precno, bandno, resno, compno);
783 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
786 seg->newlen = 8192 - cblk->len;
787 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
791 #endif /* USE_JPWL */
793 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
795 (l_seg->numpasses == 0)
797 l_seg->data = &l_cblk->data;
798 l_seg->dataindex = l_cblk->len;
800 l_current_data += l_seg->newlen;
801 l_seg->numpasses += l_seg->numnewpasses;
802 l_cblk->numnewpasses -= l_seg->numnewpasses;
804 l_seg->real_num_passes = l_seg->numpasses;
805 l_cblk->len += l_seg->newlen;
806 l_seg->len += l_seg->newlen;
808 (l_cblk->numnewpasses > 0)
814 while (l_cblk->numnewpasses > 0);
815 l_cblk->real_num_segs = l_cblk->numsegs;
820 *(p_data_read) = l_current_data - p_src_data;
825 static bool t2_skip_packet_data(
827 opj_tcd_tile_t *p_tile,
828 opj_pi_iterator_t *p_pi,
829 OPJ_UINT32 * p_data_read,
830 OPJ_UINT32 p_max_length,
831 opj_packet_info_t *pack_info)
833 OPJ_UINT32 bandno, cblkno;
834 OPJ_UINT32 l_nb_code_blocks;
835 opj_tcd_band_t *l_band = 00;
836 opj_tcd_cblk_dec_t* l_cblk = 00;
838 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
841 l_band = l_res->bands;
843 (bandno = 0; bandno < l_res->numbands; ++bandno)
845 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
848 ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
853 l_nb_code_blocks = l_prc->cw * l_prc->ch;
854 l_cblk = l_prc->cblks.dec;
856 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
858 opj_tcd_seg_t *l_seg = 00;
860 (!l_cblk->numnewpasses)
869 l_seg = l_cblk->segs;
875 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
877 (l_seg->numpasses == l_seg->maxpasses)
887 (* p_data_read + l_seg->newlen > p_max_length)
893 /* we need here a j2k handle to verify if making a check to
894 the validity of cblocks parameters is selected from user (-W) */
896 /* let's check that we are not exceeding */
897 if ((cblk->len + seg->newlen) > 8192) {
898 opj_event_msg(t2->cinfo, EVT_WARNING,
899 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
900 seg->newlen, cblkno, precno, bandno, resno, compno);
902 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
905 seg->newlen = 8192 - cblk->len;
906 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
910 #endif /* USE_JPWL */
911 *(p_data_read) += l_seg->newlen;
912 l_seg->numpasses += l_seg->numnewpasses;
913 l_cblk->numnewpasses -= l_seg->numnewpasses;
915 (l_cblk->numnewpasses > 0)
921 while (l_cblk->numnewpasses > 0);
928 static bool t2_decode_packet(
930 opj_tcd_tile_t *p_tile,
932 opj_pi_iterator_t *p_pi,
934 OPJ_UINT32 * p_data_read,
935 OPJ_UINT32 p_max_length,
936 opj_packet_info_t *p_pack_info)
939 OPJ_UINT32 l_nb_bytes_read = 0;
940 OPJ_UINT32 l_nb_total_bytes_read = 0;
945 (! 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))
949 p_src += l_nb_bytes_read;
950 l_nb_total_bytes_read += l_nb_bytes_read;
951 p_max_length -= l_nb_bytes_read;
952 /* we should read data for the packet */
958 (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info))
962 l_nb_total_bytes_read += l_nb_bytes_read;
964 *p_data_read = l_nb_total_bytes_read;
968 static bool t2_skip_packet(
970 opj_tcd_tile_t *p_tile,
972 opj_pi_iterator_t *p_pi,
974 OPJ_UINT32 * p_data_read,
975 OPJ_UINT32 p_max_length,
976 opj_packet_info_t *p_pack_info)
979 OPJ_UINT32 l_nb_bytes_read = 0;
980 OPJ_UINT32 l_nb_total_bytes_read = 0;
985 (! 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))
989 p_src += l_nb_bytes_read;
990 l_nb_total_bytes_read += l_nb_bytes_read;
991 p_max_length -= l_nb_bytes_read;
992 /* we should read data for the packet */
998 (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info))
1002 l_nb_total_bytes_read += l_nb_bytes_read;
1004 *p_data_read = l_nb_total_bytes_read;
1008 /* ----------------------------------------------------------------------- */
1010 bool t2_encode_packets(
1012 OPJ_UINT32 p_tile_no,
1013 opj_tcd_tile_t *p_tile,
1014 OPJ_UINT32 p_maxlayers,
1016 OPJ_UINT32 * p_data_written,
1017 OPJ_UINT32 p_max_len,
1018 opj_codestream_info_t *cstr_info,
1019 OPJ_UINT32 p_tp_num,
1022 J2K_T2_MODE p_t2_mode)
1024 OPJ_BYTE *l_current_data = p_dest;
1025 OPJ_UINT32 l_nb_bytes = 0;
1028 opj_pi_iterator_t *l_pi = 00;
1029 opj_pi_iterator_t *l_current_pi = 00;
1030 opj_image_t *l_image = p_t2->image;
1031 opj_cp_t *l_cp = p_t2->cp;
1032 opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
1033 OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1;
1034 OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
1035 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
1037 l_pi = pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
1043 * p_data_written = 0;
1045 (p_t2_mode == THRESH_CALC )
1046 { /* Calculating threshold */
1047 l_current_pi = l_pi;
1049 (compno = 0; compno < l_max_comp; ++compno)
1051 OPJ_UINT32 l_comp_len = 0;
1052 l_current_pi = l_pi;
1055 (poc = 0; poc < pocno ; ++poc)
1057 OPJ_UINT32 l_tp_num = compno;
1058 pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
1060 (pi_next(l_current_pi))
1063 (l_current_pi->layno < p_maxlayers)
1067 (! t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info))
1069 pi_destroy(l_pi, l_nb_pocs);
1072 l_comp_len += l_nb_bytes;
1073 l_current_data += l_nb_bytes;
1074 p_max_len -= l_nb_bytes;
1075 * p_data_written += l_nb_bytes;
1079 (l_cp->m_specific_param.m_enc.m_max_comp_size)
1082 (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size)
1084 pi_destroy(l_pi, l_nb_pocs);
1093 { /* t2_mode == FINAL_PASS */
1094 pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
1095 l_current_pi = &l_pi[p_pino];
1097 (pi_next(l_current_pi))
1100 (l_current_pi->layno < p_maxlayers)
1104 (! t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info))
1106 pi_destroy(l_pi, l_nb_pocs);
1109 l_current_data += l_nb_bytes;
1110 p_max_len -= l_nb_bytes;
1111 * p_data_written += l_nb_bytes;
1115 if(cstr_info->index_write) {
1116 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
1117 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
1118 if (!cstr_info->packno) {
1119 info_PK->start_pos = info_TL->end_header + 1;
1121 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;
1123 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
1124 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
1125 // to start of packet is incremented by value of start of packet
1128 cstr_info->packno++;
1135 pi_destroy(l_pi, l_nb_pocs);
1139 bool t2_decode_packets(
1141 OPJ_UINT32 p_tile_no,
1142 struct opj_tcd_tile *p_tile,
1144 OPJ_UINT32 * p_data_read,
1145 OPJ_UINT32 p_max_len,
1146 struct opj_codestream_info *p_cstr_info)
1148 OPJ_BYTE *l_current_data = p_src;
1149 opj_pi_iterator_t *l_pi = 00;
1151 opj_image_t *l_image = p_t2->image;
1152 opj_cp_t *l_cp = p_t2->cp;
1153 opj_cp_t *cp = p_t2->cp;
1154 opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
1155 OPJ_UINT32 l_nb_bytes_read;
1156 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
1157 opj_pi_iterator_t *l_current_pi = 00;
1158 OPJ_UINT32 curtp = 0;
1159 OPJ_UINT32 tp_start_packno;
1160 opj_packet_info_t *l_pack_info = 00;
1161 opj_image_comp_t* l_img_comp = 00;
1167 l_pack_info = p_cstr_info->tile[p_tile_no].packet;
1170 /* create a packet iterator */
1171 l_pi = pi_create_decode(l_image, l_cp, p_tile_no);
1178 tp_start_packno = 0;
1179 l_current_pi = l_pi;
1182 (pino = 0; pino <= l_tcp->numpocs; ++pino)
1185 (pi_next(l_current_pi))
1189 (l_tcp->num_layers_to_decode > l_current_pi->layno && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions)
1191 l_nb_bytes_read = 0;
1193 (! 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))
1195 pi_destroy(l_pi,l_nb_pocs);
1198 l_img_comp = &(l_image->comps[l_current_pi->compno]);
1199 l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
1203 l_nb_bytes_read = 0;
1205 (! 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))
1207 pi_destroy(l_pi,l_nb_pocs);
1211 l_current_data += l_nb_bytes_read;
1212 p_max_len -= l_nb_bytes_read;
1216 opj_tile_info_t *info_TL = &p_cstr_info->tile[p_tile_no];
1217 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
1218 if (!p_cstr_info->packno) {
1219 info_PK->start_pos = info_TL->end_header + 1;
1220 } 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
1221 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
1222 tp_start_packno = p_cstr_info->packno;
1224 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
1226 info_PK->start_pos = (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;
1228 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
1229 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
1230 ++p_cstr_info->packno;
1239 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
1243 /* don't forget to release pi */
1244 pi_destroy(l_pi,l_nb_pocs);
1245 *p_data_read = l_current_data - p_src;
1249 /* ----------------------------------------------------------------------- */
1251 * Creates a Tier 2 handle
1253 * @param p_image Source or destination image
1254 * @param p_cp Image coding parameters.
1255 * @return a new T2 handle if successful, NULL otherwise.
1257 opj_t2_t* t2_create(
1258 opj_image_t *p_image,
1261 /* create the tcd structure */
1262 opj_t2_t *l_t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
1268 memset(l_t2,0,sizeof(opj_t2_t));
1269 l_t2->image = p_image;
1275 * Destroys a Tier 2 handle.
1277 * @param p_t2 the Tier 2 handle to destroy
1279 void t2_destroy(opj_t2_t *p_t2)