2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
15 * Copyright (c) 2012, CS Systemes d'Information, France
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include "opj_includes.h"
42 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
45 /** @name Local static functions */
48 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
50 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio);
52 Variable length code for signalling delta Zil (truncation point)
53 @param bio Bit Input/Output component
56 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
57 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
60 Encode a packet of a tile to a destination buffer
61 @param tileno Number of the tile encoded
62 @param tile Tile for which to write the packets
63 @param tcp Tile coding parameters
64 @param pi Packet identity
65 @param dest Destination buffer
66 @param p_data_written FIXME DOC
67 @param len Length of the destination buffer
68 @param cstr_info Codestream information structure
71 static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno,
74 opj_pi_iterator_t *pi,
76 OPJ_UINT32 * p_data_written,
78 opj_codestream_info_t *cstr_info);
81 Decode a packet of a tile from a source buffer
83 @param tile Tile for which to write the packets
84 @param tcp Tile coding parameters
85 @param pi Packet identity
86 @param src Source buffer
87 @param data_read FIXME DOC
88 @param max_length FIXME DOC
89 @param pack_info Packet information
93 static OPJ_BOOL opj_t2_decode_packet( opj_t2_t* t2,
96 opj_pi_iterator_t *pi,
98 OPJ_UINT32 * data_read,
99 OPJ_UINT32 max_length,
100 opj_packet_info_t *pack_info);
102 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
103 opj_tcd_tile_t *p_tile,
105 opj_pi_iterator_t *p_pi,
107 OPJ_UINT32 * p_data_read,
108 OPJ_UINT32 p_max_length,
109 opj_packet_info_t *p_pack_info);
111 static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
112 opj_tcd_tile_t *p_tile,
114 opj_pi_iterator_t *p_pi,
115 OPJ_BOOL * p_is_data_present,
116 OPJ_BYTE *p_src_data,
117 OPJ_UINT32 * p_data_read,
118 OPJ_UINT32 p_max_length,
119 opj_packet_info_t *p_pack_info);
121 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
122 opj_tcd_tile_t *p_tile,
123 opj_pi_iterator_t *p_pi,
124 OPJ_BYTE *p_src_data,
125 OPJ_UINT32 * p_data_read,
126 OPJ_UINT32 p_max_length,
127 opj_packet_info_t *pack_info);
129 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
130 opj_tcd_tile_t *p_tile,
131 opj_pi_iterator_t *p_pi,
132 OPJ_UINT32 * p_data_read,
133 OPJ_UINT32 p_max_length,
134 opj_packet_info_t *pack_info);
142 static OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
151 /* ----------------------------------------------------------------------- */
153 /* #define RESTART 0x04 */
154 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) {
156 opj_bio_write(bio, 1, 1);
158 opj_bio_write(bio, 0, 1);
161 OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
164 while (opj_bio_read(bio, 1)) {
170 void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) {
172 opj_bio_write(bio, 0, 1);
174 opj_bio_write(bio, 2, 2);
176 opj_bio_write(bio, 0xc | (n - 3), 4);
177 } else if (n <= 36) {
178 opj_bio_write(bio, 0x1e0 | (n - 6), 9);
179 } else if (n <= 164) {
180 opj_bio_write(bio, 0xff80 | (n - 37), 16);
184 OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) {
186 if (!opj_bio_read(bio, 1))
188 if (!opj_bio_read(bio, 1))
190 if ((n = opj_bio_read(bio, 2)) != 3)
192 if ((n = opj_bio_read(bio, 5)) != 31)
194 return (37 + opj_bio_read(bio, 7));
197 /* ----------------------------------------------------------------------- */
199 OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2,
200 OPJ_UINT32 p_tile_no,
201 opj_tcd_tile_t *p_tile,
202 OPJ_UINT32 p_maxlayers,
204 OPJ_UINT32 * p_data_written,
205 OPJ_UINT32 p_max_len,
206 opj_codestream_info_t *cstr_info,
210 J2K_T2_MODE p_t2_mode)
212 OPJ_BYTE *l_current_data = p_dest;
213 OPJ_UINT32 l_nb_bytes = 0;
216 opj_pi_iterator_t *l_pi = 00;
217 opj_pi_iterator_t *l_current_pi = 00;
218 opj_image_t *l_image = p_t2->image;
219 opj_cp_t *l_cp = p_t2->cp;
220 opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
221 OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K)? 2: 1;
222 OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
223 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
225 l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
230 * p_data_written = 0;
232 if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
235 for (compno = 0; compno < l_max_comp; ++compno) {
236 OPJ_UINT32 l_comp_len = 0;
239 for (poc = 0; poc < pocno ; ++poc) {
240 OPJ_UINT32 l_tp_num = compno;
242 /* TODO MSD : check why this function cannot fail (cf. v1) */
243 opj_pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
245 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
246 /* TODO ADE : add an error */
247 opj_pi_destroy(l_pi, l_nb_pocs);
250 while (opj_pi_next(l_current_pi)) {
251 if (l_current_pi->layno < p_maxlayers) {
254 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)) {
255 opj_pi_destroy(l_pi, l_nb_pocs);
259 l_comp_len += l_nb_bytes;
260 l_current_data += l_nb_bytes;
261 p_max_len -= l_nb_bytes;
263 * p_data_written += l_nb_bytes;
267 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
268 if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
269 opj_pi_destroy(l_pi, l_nb_pocs);
278 else { /* t2_mode == FINAL_PASS */
279 opj_pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
281 l_current_pi = &l_pi[p_pino];
282 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
283 /* TODO ADE : add an error */
284 opj_pi_destroy(l_pi, l_nb_pocs);
287 while (opj_pi_next(l_current_pi)) {
288 if (l_current_pi->layno < p_maxlayers) {
291 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)) {
292 opj_pi_destroy(l_pi, l_nb_pocs);
296 l_current_data += l_nb_bytes;
297 p_max_len -= l_nb_bytes;
299 * p_data_written += l_nb_bytes;
303 if(cstr_info->index_write) {
304 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
305 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
306 if (!cstr_info->packno) {
307 info_PK->start_pos = info_TL->end_header + 1;
309 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;
311 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
312 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
313 to start of packet is incremented by value of start of packet*/
324 opj_pi_destroy(l_pi, l_nb_pocs);
331 #define JAS_FPRINTF fprintf
334 static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
339 #define JAS_FPRINTF opj_null_jas_fprintf
342 OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2,
343 OPJ_UINT32 p_tile_no,
344 opj_tcd_tile_t *p_tile,
346 OPJ_UINT32 * p_data_read,
347 OPJ_UINT32 p_max_len,
348 opj_codestream_index_t *p_cstr_index)
350 OPJ_BYTE *l_current_data = p_src;
351 opj_pi_iterator_t *l_pi = 00;
353 opj_image_t *l_image = p_t2->image;
354 opj_cp_t *l_cp = p_t2->cp;
355 opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
356 OPJ_UINT32 l_nb_bytes_read;
357 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
358 opj_pi_iterator_t *l_current_pi = 00;
360 OPJ_UINT32 curtp = 0;
361 OPJ_UINT32 tp_start_packno;
363 opj_packet_info_t *l_pack_info = 00;
364 opj_image_comp_t* l_img_comp = 00;
366 OPJ_ARG_NOT_USED(p_cstr_index);
370 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
374 /* create a packet iterator */
375 l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
383 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
385 /* if the resolution needed is too low, one dim of the tilec could be equal to zero
386 * and no packets are used to decode this resolution and
387 * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
388 * and no l_img_comp->resno_decoded are computed
390 OPJ_BOOL* first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
391 if (!first_pass_failed)
393 opj_pi_destroy(l_pi,l_nb_pocs);
396 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
398 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
399 /* TODO ADE : add an error */
400 opj_pi_destroy(l_pi, l_nb_pocs);
403 while (opj_pi_next(l_current_pi)) {
404 JAS_FPRINTF( stderr, "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
405 l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, l_current_pi->precno, l_current_pi->layno );
407 if (l_tcp->num_layers_to_decode > l_current_pi->layno
408 && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
411 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
413 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)) {
414 opj_pi_destroy(l_pi,l_nb_pocs);
415 opj_free(first_pass_failed);
419 l_img_comp = &(l_image->comps[l_current_pi->compno]);
420 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
424 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)) {
425 opj_pi_destroy(l_pi,l_nb_pocs);
426 opj_free(first_pass_failed);
431 if (first_pass_failed[l_current_pi->compno]) {
432 l_img_comp = &(l_image->comps[l_current_pi->compno]);
433 if (l_img_comp->resno_decoded == 0)
434 l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
437 l_current_data += l_nb_bytes_read;
438 p_max_len -= l_nb_bytes_read;
443 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
444 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
446 if (!p_cstr_info->packno) {
447 info_PK->start_pos = info_TL->end_header + 1;
448 } 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 */
449 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
450 tp_start_packno = p_cstr_info->packno;
452 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
454 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;
456 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
457 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */
458 ++p_cstr_info->packno;
465 opj_free(first_pass_failed);
471 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 */
476 /* don't forget to release pi */
477 opj_pi_destroy(l_pi,l_nb_pocs);
478 *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
482 /* ----------------------------------------------------------------------- */
485 * Creates a Tier 2 handle
487 * @param p_image Source or destination image
488 * @param p_cp Image coding parameters.
489 * @return a new T2 handle if successful, NULL otherwise.
491 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
493 /* create the t2 structure */
494 opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1,sizeof(opj_t2_t));
499 l_t2->image = p_image;
505 void opj_t2_destroy(opj_t2_t *t2) {
511 OPJ_BOOL opj_t2_decode_packet( opj_t2_t* p_t2,
512 opj_tcd_tile_t *p_tile,
514 opj_pi_iterator_t *p_pi,
516 OPJ_UINT32 * p_data_read,
517 OPJ_UINT32 p_max_length,
518 opj_packet_info_t *p_pack_info)
520 OPJ_BOOL l_read_data;
521 OPJ_UINT32 l_nb_bytes_read = 0;
522 OPJ_UINT32 l_nb_total_bytes_read = 0;
526 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)) {
530 p_src += l_nb_bytes_read;
531 l_nb_total_bytes_read += l_nb_bytes_read;
532 p_max_length -= l_nb_bytes_read;
534 /* we should read data for the packet */
538 if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
542 l_nb_total_bytes_read += l_nb_bytes_read;
545 *p_data_read = l_nb_total_bytes_read;
550 OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno,
551 opj_tcd_tile_t * tile,
553 opj_pi_iterator_t *pi,
555 OPJ_UINT32 * p_data_written,
557 opj_codestream_info_t *cstr_info)
559 OPJ_UINT32 bandno, cblkno;
561 OPJ_UINT32 l_nb_bytes;
562 OPJ_UINT32 compno = pi->compno; /* component value */
563 OPJ_UINT32 resno = pi->resno; /* resolution level value */
564 OPJ_UINT32 precno = pi->precno; /* precinct value */
565 OPJ_UINT32 layno = pi->layno; /* quality layer value */
566 OPJ_UINT32 l_nb_blocks;
567 opj_tcd_band_t *band = 00;
568 opj_tcd_cblk_enc_t* cblk = 00;
569 opj_tcd_pass_t *pass = 00;
571 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
572 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
574 opj_bio_t *bio = 00; /* BIO component */
577 if (tcp->csty & J2K_CP_CSTY_SOP) {
583 c[4] = (tile->packno % 65536) / 256;
584 c[5] = (tile->packno % 65536) % 256;
586 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
587 c[5] = tile->packno & 0xff;
597 for(bandno = 0; bandno < res->numbands; ++bandno) {
598 opj_tcd_precinct_t *prc = &band->precincts[precno];
600 opj_tgt_reset(prc->incltree);
601 opj_tgt_reset(prc->imsbtree);
603 l_nb_blocks = prc->cw * prc->ch;
604 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
605 cblk = &prc->cblks.enc[cblkno];
608 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
614 bio = opj_bio_create();
616 /* FIXME event manager error callback */
619 opj_bio_init_enc(bio, c, length);
620 opj_bio_write(bio, 1, 1); /* Empty header bit */
622 /* Writing Packet header */
624 for (bandno = 0; bandno < res->numbands; ++bandno) {
625 opj_tcd_precinct_t *prc = &band->precincts[precno];
627 l_nb_blocks = prc->cw * prc->ch;
628 cblk = prc->cblks.enc;
630 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
631 opj_tcd_layer_t *layer = &cblk->layers[layno];
633 if (!cblk->numpasses && layer->numpasses) {
634 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
640 cblk = prc->cblks.enc;
641 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
642 opj_tcd_layer_t *layer = &cblk->layers[layno];
643 OPJ_UINT32 increment = 0;
645 OPJ_UINT32 len = 0, passno;
646 OPJ_UINT32 l_nb_passes;
648 /* cblk inclusion bits */
649 if (!cblk->numpasses) {
650 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
652 opj_bio_write(bio, layer->numpasses != 0, 1);
655 /* if cblk not included, go to the next cblk */
656 if (!layer->numpasses) {
661 /* if first instance of cblk --> zero bit-planes information */
662 if (!cblk->numpasses) {
663 cblk->numlenbits = 3;
664 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
667 /* number of coding passes included */
668 opj_t2_putnumpasses(bio, layer->numpasses);
669 l_nb_passes = cblk->numpasses + layer->numpasses;
670 pass = cblk->passes + cblk->numpasses;
672 /* computation of the increase of the length indicator and insertion in the header */
673 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
677 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
678 increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, opj_int_floorlog2((OPJ_INT32)len) + 1
679 - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
686 opj_t2_putcommacode(bio, (OPJ_INT32)increment);
688 /* computation of the new Length indicator */
689 cblk->numlenbits += increment;
691 pass = cblk->passes + cblk->numpasses;
692 /* insertion of the codeword segment length */
693 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
697 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
698 opj_bio_write(bio, (OPJ_UINT32)len, cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
711 if (!opj_bio_flush(bio)) {
712 opj_bio_destroy(bio);
713 return OPJ_FALSE; /* modified to eliminate longjmp !! */
716 l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
718 length -= l_nb_bytes;
720 opj_bio_destroy(bio);
723 if (tcp->csty & J2K_CP_CSTY_EPH) {
732 /* End of packet header position. Currently only represents the distance to start of packet
733 Will be updated later by incrementing with packet start value*/
734 if(cstr_info && cstr_info->index_write) {
735 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
736 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
740 /* Writing the packet body */
742 for (bandno = 0; bandno < res->numbands; bandno++) {
743 opj_tcd_precinct_t *prc = &band->precincts[precno];
745 l_nb_blocks = prc->cw * prc->ch;
746 cblk = prc->cblks.enc;
748 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
749 opj_tcd_layer_t *layer = &cblk->layers[layno];
751 if (!layer->numpasses) {
756 if (layer->len > length) {
760 memcpy(c, layer->data, layer->len);
761 cblk->numpasses += layer->numpasses;
763 length -= layer->len;
766 if(cstr_info && cstr_info->index_write) {
767 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
768 info_PK->disto += layer->disto;
769 if (cstr_info->D_max < info_PK->disto) {
770 cstr_info->D_max = info_PK->disto;
781 * p_data_written += (OPJ_UINT32)(c - dest);
786 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
787 opj_tcd_tile_t *p_tile,
789 opj_pi_iterator_t *p_pi,
791 OPJ_UINT32 * p_data_read,
792 OPJ_UINT32 p_max_length,
793 opj_packet_info_t *p_pack_info)
795 OPJ_BOOL l_read_data;
796 OPJ_UINT32 l_nb_bytes_read = 0;
797 OPJ_UINT32 l_nb_total_bytes_read = 0;
801 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)) {
805 p_src += l_nb_bytes_read;
806 l_nb_total_bytes_read += l_nb_bytes_read;
807 p_max_length -= l_nb_bytes_read;
809 /* we should read data for the packet */
813 if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
817 l_nb_total_bytes_read += l_nb_bytes_read;
819 *p_data_read = l_nb_total_bytes_read;
825 OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
826 opj_tcd_tile_t *p_tile,
828 opj_pi_iterator_t *p_pi,
829 OPJ_BOOL * p_is_data_present,
830 OPJ_BYTE *p_src_data,
831 OPJ_UINT32 * p_data_read,
832 OPJ_UINT32 p_max_length,
833 opj_packet_info_t *p_pack_info)
837 OPJ_UINT32 bandno, cblkno;
838 OPJ_UINT32 l_nb_code_blocks;
839 OPJ_UINT32 l_remaining_length;
840 OPJ_UINT32 l_header_length;
841 OPJ_UINT32 * l_modified_length_ptr = 00;
842 OPJ_BYTE *l_current_data = p_src_data;
843 opj_cp_t *l_cp = p_t2->cp;
844 opj_bio_t *l_bio = 00; /* BIO component */
845 opj_tcd_band_t *l_band = 00;
846 opj_tcd_cblk_dec_t* l_cblk = 00;
847 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
849 OPJ_BYTE *l_header_data = 00;
850 OPJ_BYTE **l_header_data_start = 00;
852 OPJ_UINT32 l_present;
854 if (p_pi->layno == 0) {
855 l_band = l_res->bands;
858 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
859 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
861 if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
862 opj_tgt_reset(l_prc->incltree);
863 opj_tgt_reset(l_prc->imsbtree);
864 l_cblk = l_prc->cblks.dec;
866 l_nb_code_blocks = l_prc->cw * l_prc->ch;
867 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
869 l_cblk->real_num_segs = 0;
880 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
881 if (p_max_length < 6) {
882 /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Not enough space for expected SOP marker\n"); */
883 fprintf(stderr, "Not enough space for expected SOP marker\n");
884 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
885 /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
886 fprintf(stderr, "Warning: expected SOP marker\n");
891 /** TODO : check the Nsop value */
895 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
896 This part deal with this caracteristic
897 step 1: Read packet header in the saved structure
898 step 2: Return to codestream for decoding
901 l_bio = opj_bio_create();
906 if (l_cp->ppm == 1) { /* PPM */
907 l_header_data_start = &l_cp->ppm_data;
908 l_header_data = *l_header_data_start;
909 l_modified_length_ptr = &(l_cp->ppm_len);
912 else if (p_tcp->ppt == 1) { /* PPT */
913 l_header_data_start = &(p_tcp->ppt_data);
914 l_header_data = *l_header_data_start;
915 l_modified_length_ptr = &(p_tcp->ppt_len);
917 else { /* Normal Case */
918 l_header_data_start = &(l_current_data);
919 l_header_data = *l_header_data_start;
920 l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
921 l_modified_length_ptr = &(l_remaining_length);
924 opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
926 l_present = opj_bio_read(l_bio, 1);
927 JAS_FPRINTF(stderr, "present=%d \n", l_present );
929 /* TODO MSD: no test to control the output of this function*/
930 opj_bio_inalign(l_bio);
931 l_header_data += opj_bio_numbytes(l_bio);
932 opj_bio_destroy(l_bio);
935 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
936 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
937 fprintf(stderr, "Not enough space for expected EPH marker\n");
938 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
939 fprintf(stderr, "Error : expected EPH marker\n");
945 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
946 *l_modified_length_ptr -= l_header_length;
947 *l_header_data_start += l_header_length;
950 /* End of packet header position. Currently only represents the distance to start of packet
951 Will be updated later by incrementing with packet start value */
953 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
957 * p_is_data_present = OPJ_FALSE;
958 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
962 l_band = l_res->bands;
963 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
964 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
966 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
971 l_nb_code_blocks = l_prc->cw * l_prc->ch;
972 l_cblk = l_prc->cblks.dec;
973 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
974 OPJ_UINT32 l_included,l_increment, l_segno;
977 /* if cblk not yet included before --> inclusion tagtree */
978 if (!l_cblk->numsegs) {
979 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, (OPJ_INT32)(p_pi->layno + 1));
983 l_included = opj_bio_read(l_bio, 1);
986 /* if cblk not included */
988 l_cblk->numnewpasses = 0;
990 JAS_FPRINTF(stderr, "included=%d \n", l_included);
994 /* if cblk not yet included --> zero-bitplane tagtree */
995 if (!l_cblk->numsegs) {
998 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
1002 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
1003 l_cblk->numlenbits = 3;
1006 /* number of coding passes */
1007 l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
1008 l_increment = opj_t2_getcommacode(l_bio);
1010 /* length indicator increment */
1011 l_cblk->numlenbits += l_increment;
1014 if (!l_cblk->numsegs) {
1015 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1016 opj_bio_destroy(l_bio);
1021 l_segno = l_cblk->numsegs - 1;
1022 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1024 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1025 opj_bio_destroy(l_bio);
1030 n = (OPJ_INT32)l_cblk->numnewpasses;
1033 l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
1034 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1035 JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, l_cblk->segs[l_segno].newlen );
1037 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1041 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1042 opj_bio_destroy(l_bio);
1054 if (!opj_bio_inalign(l_bio)) {
1055 opj_bio_destroy(l_bio);
1059 l_header_data += opj_bio_numbytes(l_bio);
1060 opj_bio_destroy(l_bio);
1063 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1064 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
1065 fprintf(stderr, "Not enough space for expected EPH marker\n");
1066 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1067 /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1068 fprintf(stderr, "Error : expected EPH marker\n");
1074 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1075 JAS_FPRINTF( stderr, "hdrlen=%d \n", l_header_length );
1076 JAS_FPRINTF( stderr, "packet body\n");
1077 *l_modified_length_ptr -= l_header_length;
1078 *l_header_data_start += l_header_length;
1081 /* End of packet header position. Currently only represents the distance to start of packet
1082 Will be updated later by incrementing with packet start value */
1084 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1088 *p_is_data_present = OPJ_TRUE;
1089 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1094 OPJ_BOOL opj_t2_read_packet_data( opj_t2_t* p_t2,
1095 opj_tcd_tile_t *p_tile,
1096 opj_pi_iterator_t *p_pi,
1097 OPJ_BYTE *p_src_data,
1098 OPJ_UINT32 * p_data_read,
1099 OPJ_UINT32 p_max_length,
1100 opj_packet_info_t *pack_info)
1102 OPJ_UINT32 bandno, cblkno;
1103 OPJ_UINT32 l_nb_code_blocks;
1104 OPJ_BYTE *l_current_data = p_src_data;
1105 opj_tcd_band_t *l_band = 00;
1106 opj_tcd_cblk_dec_t* l_cblk = 00;
1107 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1109 OPJ_ARG_NOT_USED(p_t2);
1110 OPJ_ARG_NOT_USED(pack_info);
1112 l_band = l_res->bands;
1113 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1114 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1116 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1121 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1122 l_cblk = l_prc->cblks.dec;
1124 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1125 opj_tcd_seg_t *l_seg = 00;
1127 if (!l_cblk->numnewpasses) {
1133 if (!l_cblk->numsegs) {
1134 l_seg = l_cblk->segs;
1136 l_cblk->data_current_size = 0;
1139 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1141 if (l_seg->numpasses == l_seg->maxpasses) {
1148 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
1149 if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) < (OPJ_SIZE_T)l_current_data) || (l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
1150 fprintf(stderr, "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1151 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1156 /* we need here a j2k handle to verify if making a check to
1157 the validity of cblocks parameters is selected from user (-W) */
1159 /* let's check that we are not exceeding */
1160 if ((l_cblk->len + l_seg->newlen) > 8192) {
1161 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1162 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1163 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1165 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1168 l_seg->newlen = 8192 - l_cblk->len;
1169 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1173 #endif /* USE_JPWL */
1174 /* Check possible overflow on size */
1175 if ((l_cblk->data_current_size + l_seg->newlen) < l_cblk->data_current_size) {
1176 fprintf(stderr, "read: segment too long (%d) with current size (%d > %d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1177 l_seg->newlen, l_cblk->data_current_size, 0xFFFFFFFF - l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1180 /* Check if the cblk->data have allocated enough memory */
1181 if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
1182 OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
1183 if(! new_cblk_data) {
1184 opj_free(l_cblk->data);
1185 l_cblk->data = NULL;
1186 l_cblk->data_max_size = 0;
1187 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
1190 l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
1191 l_cblk->data = new_cblk_data;
1194 memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
1196 if (l_seg->numpasses == 0) {
1197 l_seg->data = &l_cblk->data;
1198 l_seg->dataindex = l_cblk->data_current_size;
1201 l_current_data += l_seg->newlen;
1202 l_seg->numpasses += l_seg->numnewpasses;
1203 l_cblk->numnewpasses -= l_seg->numnewpasses;
1205 l_seg->real_num_passes = l_seg->numpasses;
1206 l_cblk->data_current_size += l_seg->newlen;
1207 l_seg->len += l_seg->newlen;
1209 if (l_cblk->numnewpasses > 0) {
1213 } while (l_cblk->numnewpasses > 0);
1215 l_cblk->real_num_segs = l_cblk->numsegs;
1217 } /* next code_block */
1222 *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1228 OPJ_BOOL opj_t2_skip_packet_data( opj_t2_t* p_t2,
1229 opj_tcd_tile_t *p_tile,
1230 opj_pi_iterator_t *p_pi,
1231 OPJ_UINT32 * p_data_read,
1232 OPJ_UINT32 p_max_length,
1233 opj_packet_info_t *pack_info)
1235 OPJ_UINT32 bandno, cblkno;
1236 OPJ_UINT32 l_nb_code_blocks;
1237 opj_tcd_band_t *l_band = 00;
1238 opj_tcd_cblk_dec_t* l_cblk = 00;
1239 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1241 OPJ_ARG_NOT_USED(p_t2);
1242 OPJ_ARG_NOT_USED(pack_info);
1245 l_band = l_res->bands;
1247 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1248 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1250 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1255 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1256 l_cblk = l_prc->cblks.dec;
1258 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1259 opj_tcd_seg_t *l_seg = 00;
1261 if (!l_cblk->numnewpasses) {
1267 if (!l_cblk->numsegs) {
1268 l_seg = l_cblk->segs;
1270 l_cblk->data_current_size = 0;
1273 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1275 if (l_seg->numpasses == l_seg->maxpasses) {
1282 /* Check possible overflow then size */
1283 if (((*p_data_read + l_seg->newlen) < (*p_data_read)) || ((*p_data_read + l_seg->newlen) > p_max_length)) {
1284 fprintf(stderr, "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1285 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1290 /* we need here a j2k handle to verify if making a check to
1291 the validity of cblocks parameters is selected from user (-W) */
1293 /* let's check that we are not exceeding */
1294 if ((l_cblk->len + l_seg->newlen) > 8192) {
1295 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1296 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1297 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1299 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1302 l_seg->newlen = 8192 - l_cblk->len;
1303 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1307 #endif /* USE_JPWL */
1308 JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, l_seg->newlen );
1309 *(p_data_read) += l_seg->newlen;
1311 l_seg->numpasses += l_seg->numnewpasses;
1312 l_cblk->numnewpasses -= l_seg->numnewpasses;
1313 if (l_cblk->numnewpasses > 0)
1318 } while (l_cblk->numnewpasses > 0);
1330 OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
1335 opj_tcd_seg_t* seg = 00;
1336 OPJ_UINT32 l_nb_segs = index + 1;
1338 if (l_nb_segs > cblk->m_current_max_segs) {
1339 opj_tcd_seg_t* new_segs;
1340 cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
1342 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1344 opj_free(cblk->segs);
1346 cblk->m_current_max_segs = 0;
1347 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1350 cblk->segs = new_segs;
1353 seg = &cblk->segs[index];
1354 memset(seg,0,sizeof(opj_tcd_seg_t));
1356 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1359 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1361 seg->maxpasses = 10;
1363 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1366 seg->maxpasses = 109;