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 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
42 #include "opj_common.h"
45 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
48 /** @name Local static functions */
51 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
53 static OPJ_UINT32 opj_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 opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
60 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
63 Encode a packet of a tile to a destination buffer
64 @param tileno Number of the tile encoded
65 @param tile Tile for which to write the packets
66 @param tcp Tile coding parameters
67 @param pi Packet identity
68 @param dest Destination buffer
69 @param p_data_written FIXME DOC
70 @param len Length of the destination buffer
71 @param cstr_info Codestream information structure
72 @param p_t2_mode If == THRESH_CALC In Threshold calculation ,If == FINAL_PASS Final pass
73 @param p_manager the user event manager
76 static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
79 opj_pi_iterator_t *pi,
81 OPJ_UINT32 * p_data_written,
83 opj_codestream_info_t *cstr_info,
84 J2K_T2_MODE p_t2_mode,
85 opj_event_mgr_t *p_manager);
88 Decode a packet of a tile from a source buffer
90 @param tile Tile for which to write the packets
91 @param tcp Tile coding parameters
92 @param pi Packet identity
93 @param src Source buffer
94 @param data_read FIXME DOC
95 @param max_length FIXME DOC
96 @param pack_info Packet information
97 @param p_manager the user event manager
101 static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* t2,
102 opj_tcd_tile_t *tile,
104 opj_pi_iterator_t *pi,
106 OPJ_UINT32 * data_read,
107 OPJ_UINT32 max_length,
108 opj_packet_info_t *pack_info,
109 opj_event_mgr_t *p_manager);
111 static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2,
112 opj_tcd_tile_t *p_tile,
114 opj_pi_iterator_t *p_pi,
116 OPJ_UINT32 * p_data_read,
117 OPJ_UINT32 p_max_length,
118 opj_packet_info_t *p_pack_info,
119 opj_event_mgr_t *p_manager);
121 static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2,
122 opj_tcd_tile_t *p_tile,
124 opj_pi_iterator_t *p_pi,
125 OPJ_BOOL * p_is_data_present,
126 OPJ_BYTE *p_src_data,
127 OPJ_UINT32 * p_data_read,
128 OPJ_UINT32 p_max_length,
129 opj_packet_info_t *p_pack_info,
130 opj_event_mgr_t *p_manager);
132 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
133 opj_tcd_tile_t *p_tile,
134 opj_pi_iterator_t *p_pi,
135 OPJ_BYTE *p_src_data,
136 OPJ_UINT32 * p_data_read,
137 OPJ_UINT32 p_max_length,
138 opj_packet_info_t *pack_info,
139 opj_event_mgr_t *p_manager);
141 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
142 opj_tcd_tile_t *p_tile,
143 opj_pi_iterator_t *p_pi,
144 OPJ_UINT32 * p_data_read,
145 OPJ_UINT32 p_max_length,
146 opj_packet_info_t *pack_info,
147 opj_event_mgr_t *p_manager);
155 static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
164 /* ----------------------------------------------------------------------- */
166 /* #define RESTART 0x04 */
167 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n)
170 opj_bio_write(bio, 1, 1);
172 opj_bio_write(bio, 0, 1);
175 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
178 while (opj_bio_read(bio, 1)) {
184 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n)
187 opj_bio_write(bio, 0, 1);
189 opj_bio_write(bio, 2, 2);
191 opj_bio_write(bio, 0xc | (n - 3), 4);
192 } else if (n <= 36) {
193 opj_bio_write(bio, 0x1e0 | (n - 6), 9);
194 } else if (n <= 164) {
195 opj_bio_write(bio, 0xff80 | (n - 37), 16);
199 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio)
202 if (!opj_bio_read(bio, 1)) {
205 if (!opj_bio_read(bio, 1)) {
208 if ((n = opj_bio_read(bio, 2)) != 3) {
211 if ((n = opj_bio_read(bio, 5)) != 31) {
214 return (37 + opj_bio_read(bio, 7));
217 /* ----------------------------------------------------------------------- */
219 OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2,
220 OPJ_UINT32 p_tile_no,
221 opj_tcd_tile_t *p_tile,
222 OPJ_UINT32 p_maxlayers,
224 OPJ_UINT32 * p_data_written,
225 OPJ_UINT32 p_max_len,
226 opj_codestream_info_t *cstr_info,
230 J2K_T2_MODE p_t2_mode,
231 opj_event_mgr_t *p_manager)
233 OPJ_BYTE *l_current_data = p_dest;
234 OPJ_UINT32 l_nb_bytes = 0;
237 opj_pi_iterator_t *l_pi = 00;
238 opj_pi_iterator_t *l_current_pi = 00;
239 opj_image_t *l_image = p_t2->image;
240 opj_cp_t *l_cp = p_t2->cp;
241 opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
242 OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K) ? 2 : 1;
243 OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ?
244 l_image->numcomps : 1;
245 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
247 l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
252 * p_data_written = 0;
254 if (p_t2_mode == THRESH_CALC) { /* Calculating threshold */
257 for (compno = 0; compno < l_max_comp; ++compno) {
258 OPJ_UINT32 l_comp_len = 0;
261 for (poc = 0; poc < pocno ; ++poc) {
262 OPJ_UINT32 l_tp_num = compno;
264 /* TODO MSD : check why this function cannot fail (cf. v1) */
265 opj_pi_create_encode(l_pi, l_cp, p_tile_no, poc, l_tp_num, p_tp_pos, p_t2_mode);
267 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
268 /* TODO ADE : add an error */
269 opj_pi_destroy(l_pi, l_nb_pocs);
272 while (opj_pi_next(l_current_pi)) {
273 if (l_current_pi->layno < p_maxlayers) {
276 if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
277 l_current_data, &l_nb_bytes,
278 p_max_len, cstr_info,
281 opj_pi_destroy(l_pi, l_nb_pocs);
285 l_comp_len += l_nb_bytes;
286 l_current_data += l_nb_bytes;
287 p_max_len -= l_nb_bytes;
289 * p_data_written += l_nb_bytes;
293 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
294 if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
295 opj_pi_destroy(l_pi, l_nb_pocs);
303 } else { /* t2_mode == FINAL_PASS */
304 opj_pi_create_encode(l_pi, l_cp, p_tile_no, p_pino, p_tp_num, p_tp_pos,
307 l_current_pi = &l_pi[p_pino];
308 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
309 /* TODO ADE : add an error */
310 opj_pi_destroy(l_pi, l_nb_pocs);
313 while (opj_pi_next(l_current_pi)) {
314 if (l_current_pi->layno < p_maxlayers) {
317 if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
318 l_current_data, &l_nb_bytes, p_max_len,
319 cstr_info, p_t2_mode, p_manager)) {
320 opj_pi_destroy(l_pi, l_nb_pocs);
324 l_current_data += l_nb_bytes;
325 p_max_len -= l_nb_bytes;
327 * p_data_written += l_nb_bytes;
331 if (cstr_info->index_write) {
332 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
333 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
334 if (!cstr_info->packno) {
335 info_PK->start_pos = info_TL->end_header + 1;
337 info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC) &&
338 info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno -
341 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
342 info_PK->end_ph_pos += info_PK->start_pos -
343 1; /* End of packet header which now only represents the distance
344 to start of packet is incremented by value of start of packet*/
355 opj_pi_destroy(l_pi, l_nb_pocs);
362 #define JAS_FPRINTF fprintf
365 static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
370 #define JAS_FPRINTF opj_null_jas_fprintf
373 OPJ_BOOL opj_t2_decode_packets(opj_tcd_t* tcd,
375 OPJ_UINT32 p_tile_no,
376 opj_tcd_tile_t *p_tile,
378 OPJ_UINT32 * p_data_read,
379 OPJ_UINT32 p_max_len,
380 opj_codestream_index_t *p_cstr_index,
381 opj_event_mgr_t *p_manager)
383 OPJ_BYTE *l_current_data = p_src;
384 opj_pi_iterator_t *l_pi = 00;
386 opj_image_t *l_image = p_t2->image;
387 opj_cp_t *l_cp = p_t2->cp;
388 opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
389 OPJ_UINT32 l_nb_bytes_read;
390 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
391 opj_pi_iterator_t *l_current_pi = 00;
393 OPJ_UINT32 curtp = 0;
394 OPJ_UINT32 tp_start_packno;
396 opj_packet_info_t *l_pack_info = 00;
397 opj_image_comp_t* l_img_comp = 00;
399 OPJ_ARG_NOT_USED(p_cstr_index);
403 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
407 /* create a packet iterator */
408 l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
416 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
418 /* if the resolution needed is too low, one dim of the tilec could be equal to zero
419 * and no packets are used to decode this resolution and
420 * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
421 * and no l_img_comp->resno_decoded are computed
423 OPJ_BOOL* first_pass_failed = NULL;
425 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
426 /* TODO ADE : add an error */
427 opj_pi_destroy(l_pi, l_nb_pocs);
431 first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
432 if (!first_pass_failed) {
433 opj_pi_destroy(l_pi, l_nb_pocs);
436 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
438 while (opj_pi_next(l_current_pi)) {
439 OPJ_BOOL skip_packet = OPJ_FALSE;
441 "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
442 l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno,
443 l_current_pi->precno, l_current_pi->layno);
445 /* If the packet layer is greater or equal than the maximum */
446 /* number of layers, skip the packet */
447 if (l_current_pi->layno >= l_tcp->num_layers_to_decode) {
448 skip_packet = OPJ_TRUE;
450 /* If the packet resolution number is greater than the minimum */
451 /* number of resolution allowed, skip the packet */
452 else if (l_current_pi->resno >=
453 p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
454 skip_packet = OPJ_TRUE;
456 /* If no precincts of any band intersects the area of interest, */
457 /* skip the packet */
459 opj_tcd_tilecomp_t *tilec = &p_tile->comps[l_current_pi->compno];
460 opj_tcd_resolution_t *res = &tilec->resolutions[l_current_pi->resno];
462 skip_packet = OPJ_TRUE;
463 for (bandno = 0; bandno < res->numbands; ++bandno) {
464 opj_tcd_band_t* band = &res->bands[bandno];
465 opj_tcd_precinct_t* prec = &band->precincts[l_current_pi->precno];
467 if (opj_tcd_is_subband_area_of_interest(tcd,
468 l_current_pi->compno,
471 (OPJ_UINT32)prec->x0,
472 (OPJ_UINT32)prec->y0,
473 (OPJ_UINT32)prec->x1,
474 (OPJ_UINT32)prec->y1)) {
475 skip_packet = OPJ_FALSE;
480 printf("packet cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d -> %s\n",
481 l_current_pi->compno, l_current_pi->resno,
482 l_current_pi->precno, l_current_pi->layno, skip_packet ? "skipped" : "kept");
489 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
491 if (! opj_t2_decode_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data,
492 &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) {
493 opj_pi_destroy(l_pi, l_nb_pocs);
494 opj_free(first_pass_failed);
498 l_img_comp = &(l_image->comps[l_current_pi->compno]);
499 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno,
500 l_img_comp->resno_decoded);
503 if (! opj_t2_skip_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data,
504 &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) {
505 opj_pi_destroy(l_pi, l_nb_pocs);
506 opj_free(first_pass_failed);
511 if (first_pass_failed[l_current_pi->compno]) {
512 l_img_comp = &(l_image->comps[l_current_pi->compno]);
513 if (l_img_comp->resno_decoded == 0) {
514 l_img_comp->resno_decoded =
515 p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
519 l_current_data += l_nb_bytes_read;
520 p_max_len -= l_nb_bytes_read;
525 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
526 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
528 if (!p_cstr_info->packno) {
529 info_PK->start_pos = info_TL->end_header + 1;
530 } else if (info_TL->packet[p_cstr_info->packno - 1].end_pos >=
532 p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos) { /* New tile part */
533 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno -
534 tp_start_packno; /* Number of packets in previous tile-part */
535 tp_start_packno = p_cstr_info->packno;
537 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header + 1;
539 info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on &&
540 info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno -
543 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
544 info_PK->end_ph_pos += info_PK->start_pos -
545 1; /* End of packet header which now only represents the distance */
546 ++p_cstr_info->packno;
553 opj_free(first_pass_failed);
559 p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno -
560 tp_start_packno; /* Number of packets in last tile-part */
565 /* don't forget to release pi */
566 opj_pi_destroy(l_pi, l_nb_pocs);
567 *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
571 /* ----------------------------------------------------------------------- */
574 * Creates a Tier 2 handle
576 * @param p_image Source or destination image
577 * @param p_cp Image coding parameters.
578 * @return a new T2 handle if successful, NULL otherwise.
580 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
582 /* create the t2 structure */
583 opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1, sizeof(opj_t2_t));
588 l_t2->image = p_image;
594 void opj_t2_destroy(opj_t2_t *t2)
601 static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* p_t2,
602 opj_tcd_tile_t *p_tile,
604 opj_pi_iterator_t *p_pi,
606 OPJ_UINT32 * p_data_read,
607 OPJ_UINT32 p_max_length,
608 opj_packet_info_t *p_pack_info,
609 opj_event_mgr_t *p_manager)
611 OPJ_BOOL l_read_data;
612 OPJ_UINT32 l_nb_bytes_read = 0;
613 OPJ_UINT32 l_nb_total_bytes_read = 0;
617 if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src,
618 &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) {
622 p_src += l_nb_bytes_read;
623 l_nb_total_bytes_read += l_nb_bytes_read;
624 p_max_length -= l_nb_bytes_read;
626 /* we should read data for the packet */
630 if (! opj_t2_read_packet_data(p_t2, p_tile, p_pi, p_src, &l_nb_bytes_read,
631 p_max_length, p_pack_info, p_manager)) {
635 l_nb_total_bytes_read += l_nb_bytes_read;
638 *p_data_read = l_nb_total_bytes_read;
643 static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
644 opj_tcd_tile_t * tile,
646 opj_pi_iterator_t *pi,
648 OPJ_UINT32 * p_data_written,
650 opj_codestream_info_t *cstr_info,
651 J2K_T2_MODE p_t2_mode,
652 opj_event_mgr_t *p_manager)
654 OPJ_UINT32 bandno, cblkno;
656 OPJ_UINT32 l_nb_bytes;
657 OPJ_UINT32 compno = pi->compno; /* component value */
658 OPJ_UINT32 resno = pi->resno; /* resolution level value */
659 OPJ_UINT32 precno = pi->precno; /* precinct value */
660 OPJ_UINT32 layno = pi->layno; /* quality layer value */
661 OPJ_UINT32 l_nb_blocks;
662 opj_tcd_band_t *band = 00;
663 opj_tcd_cblk_enc_t* cblk = 00;
664 opj_tcd_pass_t *pass = 00;
666 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
667 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
669 opj_bio_t *bio = 00; /* BIO component */
670 #ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION
671 OPJ_BOOL packet_empty = OPJ_TRUE;
673 OPJ_BOOL packet_empty = OPJ_FALSE;
677 if (p_t2_mode == FINAL_PASS) {
679 "encode packet compono=%d, resno=%d, precno=%d, layno=%d\n",
680 compno, resno, precno, layno);
685 if (tcp->csty & J2K_CP_CSTY_SOP) {
687 if (p_t2_mode == FINAL_PASS) {
688 opj_event_msg(p_manager, EVT_ERROR,
689 "opj_t2_encode_packet(): only %u bytes remaining in "
690 "output buffer. %u needed.\n",
700 c[4] = (tile->packno % 65536) / 256;
701 c[5] = (tile->packno % 65536) % 256;
703 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
704 c[5] = tile->packno & 0xff;
714 for (bandno = 0; bandno < res->numbands; ++bandno, ++band) {
715 opj_tcd_precinct_t *prc;
717 /* Skip empty bands */
718 if (opj_tcd_is_band_empty(band)) {
722 prc = &band->precincts[precno];
723 opj_tgt_reset(prc->incltree);
724 opj_tgt_reset(prc->imsbtree);
726 l_nb_blocks = prc->cw * prc->ch;
727 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
728 cblk = &prc->cblks.enc[cblkno];
731 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
736 bio = opj_bio_create();
738 /* FIXME event manager error callback */
741 opj_bio_init_enc(bio, c, length);
743 #ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION
744 /* WARNING: this code branch is disabled, since it has been reported that */
745 /* such packets cause decoding issues with cinema J2K hardware */
746 /* decoders: https://groups.google.com/forum/#!topic/openjpeg/M7M_fLX_Bco */
748 /* Check if the packet is empty */
749 /* Note: we could also skip that step and always write a packet header */
751 for (bandno = 0; bandno < res->numbands; ++bandno, ++band) {
752 opj_tcd_precinct_t *prc;
753 /* Skip empty bands */
754 if (opj_tcd_is_band_empty(band)) {
758 prc = &band->precincts[precno];
759 l_nb_blocks = prc->cw * prc->ch;
760 cblk = prc->cblks.enc;
761 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++, ++cblk) {
762 opj_tcd_layer_t *layer = &cblk->layers[layno];
764 /* if cblk not included, go to the next cblk */
765 if (!layer->numpasses) {
768 packet_empty = OPJ_FALSE;
776 opj_bio_write(bio, packet_empty ? 0 : 1, 1); /* Empty header bit */
778 /* Writing Packet header */
780 for (bandno = 0; !packet_empty &&
781 bandno < res->numbands; ++bandno, ++band) {
782 opj_tcd_precinct_t *prc;
784 /* Skip empty bands */
785 if (opj_tcd_is_band_empty(band)) {
789 prc = &band->precincts[precno];
790 l_nb_blocks = prc->cw * prc->ch;
791 cblk = prc->cblks.enc;
793 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
794 opj_tcd_layer_t *layer = &cblk->layers[layno];
796 if (!cblk->numpasses && layer->numpasses) {
797 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
803 cblk = prc->cblks.enc;
804 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
805 opj_tcd_layer_t *layer = &cblk->layers[layno];
806 OPJ_UINT32 increment = 0;
808 OPJ_UINT32 len = 0, passno;
809 OPJ_UINT32 l_nb_passes;
811 /* cblk inclusion bits */
812 if (!cblk->numpasses) {
813 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
815 opj_bio_write(bio, layer->numpasses != 0, 1);
818 /* if cblk not included, go to the next cblk */
819 if (!layer->numpasses) {
824 /* if first instance of cblk --> zero bit-planes information */
825 if (!cblk->numpasses) {
826 cblk->numlenbits = 3;
827 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
830 /* number of coding passes included */
831 opj_t2_putnumpasses(bio, layer->numpasses);
832 l_nb_passes = cblk->numpasses + layer->numpasses;
833 pass = cblk->passes + cblk->numpasses;
835 /* computation of the increase of the length indicator and insertion in the header */
836 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
840 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
841 increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment,
842 opj_int_floorlog2((OPJ_INT32)len) + 1
843 - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
850 opj_t2_putcommacode(bio, (OPJ_INT32)increment);
852 /* computation of the new Length indicator */
853 cblk->numlenbits += increment;
855 pass = cblk->passes + cblk->numpasses;
856 /* insertion of the codeword segment length */
857 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
861 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
862 opj_bio_write(bio, (OPJ_UINT32)len,
863 cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
874 if (!opj_bio_flush(bio)) {
875 opj_bio_destroy(bio);
876 return OPJ_FALSE; /* modified to eliminate longjmp !! */
879 l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
881 length -= l_nb_bytes;
883 opj_bio_destroy(bio);
886 if (tcp->csty & J2K_CP_CSTY_EPH) {
888 if (p_t2_mode == FINAL_PASS) {
889 opj_event_msg(p_manager, EVT_ERROR,
890 "opj_t2_encode_packet(): only %u bytes remaining in "
891 "output buffer. %u needed.\n",
904 /* End of packet header position. Currently only represents the distance to start of packet
905 Will be updated later by incrementing with packet start value*/
906 if (cstr_info && cstr_info->index_write) {
907 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
908 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
912 /* Writing the packet body */
914 for (bandno = 0; !packet_empty && bandno < res->numbands; bandno++, ++band) {
915 opj_tcd_precinct_t *prc;
917 /* Skip empty bands */
918 if (opj_tcd_is_band_empty(band)) {
922 prc = &band->precincts[precno];
923 l_nb_blocks = prc->cw * prc->ch;
924 cblk = prc->cblks.enc;
926 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
927 opj_tcd_layer_t *layer = &cblk->layers[layno];
929 if (!layer->numpasses) {
934 if (layer->len > length) {
935 if (p_t2_mode == FINAL_PASS) {
936 opj_event_msg(p_manager, EVT_ERROR,
937 "opj_t2_encode_packet(): only %u bytes remaining in "
938 "output buffer. %u needed.\n",
944 memcpy(c, layer->data, layer->len);
945 cblk->numpasses += layer->numpasses;
947 length -= layer->len;
950 if (cstr_info && cstr_info->index_write) {
951 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
952 info_PK->disto += layer->disto;
953 if (cstr_info->D_max < info_PK->disto) {
954 cstr_info->D_max = info_PK->disto;
964 * p_data_written += (OPJ_UINT32)(c - dest);
969 static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2,
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,
977 opj_event_mgr_t *p_manager)
979 OPJ_BOOL l_read_data;
980 OPJ_UINT32 l_nb_bytes_read = 0;
981 OPJ_UINT32 l_nb_total_bytes_read = 0;
985 if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src,
986 &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) {
990 p_src += l_nb_bytes_read;
991 l_nb_total_bytes_read += l_nb_bytes_read;
992 p_max_length -= l_nb_bytes_read;
994 /* we should read data for the packet */
998 if (! opj_t2_skip_packet_data(p_t2, p_tile, p_pi, &l_nb_bytes_read,
999 p_max_length, p_pack_info, p_manager)) {
1003 l_nb_total_bytes_read += l_nb_bytes_read;
1005 *p_data_read = l_nb_total_bytes_read;
1011 static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2,
1012 opj_tcd_tile_t *p_tile,
1014 opj_pi_iterator_t *p_pi,
1015 OPJ_BOOL * p_is_data_present,
1016 OPJ_BYTE *p_src_data,
1017 OPJ_UINT32 * p_data_read,
1018 OPJ_UINT32 p_max_length,
1019 opj_packet_info_t *p_pack_info,
1020 opj_event_mgr_t *p_manager)
1024 OPJ_UINT32 bandno, cblkno;
1025 OPJ_UINT32 l_nb_code_blocks;
1026 OPJ_UINT32 l_remaining_length;
1027 OPJ_UINT32 l_header_length;
1028 OPJ_UINT32 * l_modified_length_ptr = 00;
1029 OPJ_BYTE *l_current_data = p_src_data;
1030 opj_cp_t *l_cp = p_t2->cp;
1031 opj_bio_t *l_bio = 00; /* BIO component */
1032 opj_tcd_band_t *l_band = 00;
1033 opj_tcd_cblk_dec_t* l_cblk = 00;
1034 opj_tcd_resolution_t* l_res =
1035 &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1037 OPJ_BYTE *l_header_data = 00;
1038 OPJ_BYTE **l_header_data_start = 00;
1040 OPJ_UINT32 l_present;
1042 if (p_pi->layno == 0) {
1043 l_band = l_res->bands;
1045 /* reset tagtrees */
1046 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1047 if (!opj_tcd_is_band_empty(l_band)) {
1048 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1049 if (!(p_pi->precno < (l_band->precincts_data_size / sizeof(
1050 opj_tcd_precinct_t)))) {
1051 opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct\n");
1056 opj_tgt_reset(l_prc->incltree);
1057 opj_tgt_reset(l_prc->imsbtree);
1058 l_cblk = l_prc->cblks.dec;
1060 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1061 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1062 l_cblk->numsegs = 0;
1063 l_cblk->real_num_segs = 0;
1074 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
1075 if (p_max_length < 6) {
1076 opj_event_msg(p_manager, EVT_WARNING,
1077 "Not enough space for expected SOP marker\n");
1078 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
1079 opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n");
1081 l_current_data += 6;
1084 /** TODO : check the Nsop value */
1088 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1089 This part deal with this caracteristic
1090 step 1: Read packet header in the saved structure
1091 step 2: Return to codestream for decoding
1094 l_bio = opj_bio_create();
1099 if (l_cp->ppm == 1) { /* PPM */
1100 l_header_data_start = &l_cp->ppm_data;
1101 l_header_data = *l_header_data_start;
1102 l_modified_length_ptr = &(l_cp->ppm_len);
1104 } else if (p_tcp->ppt == 1) { /* PPT */
1105 l_header_data_start = &(p_tcp->ppt_data);
1106 l_header_data = *l_header_data_start;
1107 l_modified_length_ptr = &(p_tcp->ppt_len);
1108 } else { /* Normal Case */
1109 l_header_data_start = &(l_current_data);
1110 l_header_data = *l_header_data_start;
1111 l_remaining_length = (OPJ_UINT32)(p_src_data + p_max_length - l_header_data);
1112 l_modified_length_ptr = &(l_remaining_length);
1115 opj_bio_init_dec(l_bio, l_header_data, *l_modified_length_ptr);
1117 l_present = opj_bio_read(l_bio, 1);
1118 JAS_FPRINTF(stderr, "present=%d \n", l_present);
1120 /* TODO MSD: no test to control the output of this function*/
1121 opj_bio_inalign(l_bio);
1122 l_header_data += opj_bio_numbytes(l_bio);
1123 opj_bio_destroy(l_bio);
1126 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1127 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
1128 *l_header_data_start)) < 2U) {
1129 opj_event_msg(p_manager, EVT_WARNING,
1130 "Not enough space for expected EPH marker\n");
1131 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1132 opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
1138 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1139 *l_modified_length_ptr -= l_header_length;
1140 *l_header_data_start += l_header_length;
1143 /* End of packet header position. Currently only represents the distance to start of packet
1144 Will be updated later by incrementing with packet start value */
1146 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1150 * p_is_data_present = OPJ_FALSE;
1151 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1155 l_band = l_res->bands;
1156 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band) {
1157 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
1159 if (opj_tcd_is_band_empty(l_band)) {
1163 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1164 l_cblk = l_prc->cblks.dec;
1165 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
1166 OPJ_UINT32 l_included, l_increment, l_segno;
1169 /* if cblk not yet included before --> inclusion tagtree */
1170 if (!l_cblk->numsegs) {
1171 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno,
1172 (OPJ_INT32)(p_pi->layno + 1));
1175 l_included = opj_bio_read(l_bio, 1);
1178 /* if cblk not included */
1180 l_cblk->numnewpasses = 0;
1182 JAS_FPRINTF(stderr, "included=%d \n", l_included);
1186 /* if cblk not yet included --> zero-bitplane tagtree */
1187 if (!l_cblk->numsegs) {
1190 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
1194 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
1195 l_cblk->numlenbits = 3;
1198 /* number of coding passes */
1199 l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
1200 l_increment = opj_t2_getcommacode(l_bio);
1202 /* length indicator increment */
1203 l_cblk->numlenbits += l_increment;
1206 if (!l_cblk->numsegs) {
1207 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1208 opj_bio_destroy(l_bio);
1212 l_segno = l_cblk->numsegs - 1;
1213 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1215 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1216 opj_bio_destroy(l_bio);
1221 n = (OPJ_INT32)l_cblk->numnewpasses;
1224 OPJ_UINT32 bit_number;
1225 l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(
1226 l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
1227 bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
1228 l_cblk->segs[l_segno].numnewpasses);
1229 if (bit_number > 32) {
1230 opj_event_msg(p_manager, EVT_ERROR,
1231 "Invalid bit number %d in opj_t2_read_packet_header()\n",
1233 opj_bio_destroy(l_bio);
1236 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
1237 JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
1238 l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
1239 l_cblk->segs[l_segno].newlen);
1241 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1245 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1246 opj_bio_destroy(l_bio);
1256 if (!opj_bio_inalign(l_bio)) {
1257 opj_bio_destroy(l_bio);
1261 l_header_data += opj_bio_numbytes(l_bio);
1262 opj_bio_destroy(l_bio);
1265 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1266 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
1267 *l_header_data_start)) < 2U) {
1268 opj_event_msg(p_manager, EVT_WARNING,
1269 "Not enough space for expected EPH marker\n");
1270 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1271 opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
1277 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1278 JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length);
1279 JAS_FPRINTF(stderr, "packet body\n");
1280 *l_modified_length_ptr -= l_header_length;
1281 *l_header_data_start += l_header_length;
1284 /* End of packet header position. Currently only represents the distance to start of packet
1285 Will be updated later by incrementing with packet start value */
1287 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1291 *p_is_data_present = OPJ_TRUE;
1292 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1297 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
1298 opj_tcd_tile_t *p_tile,
1299 opj_pi_iterator_t *p_pi,
1300 OPJ_BYTE *p_src_data,
1301 OPJ_UINT32 * p_data_read,
1302 OPJ_UINT32 p_max_length,
1303 opj_packet_info_t *pack_info,
1304 opj_event_mgr_t* p_manager)
1306 OPJ_UINT32 bandno, cblkno;
1307 OPJ_UINT32 l_nb_code_blocks;
1308 OPJ_BYTE *l_current_data = p_src_data;
1309 opj_tcd_band_t *l_band = 00;
1310 opj_tcd_cblk_dec_t* l_cblk = 00;
1311 opj_tcd_resolution_t* l_res =
1312 &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1314 OPJ_ARG_NOT_USED(p_t2);
1315 OPJ_ARG_NOT_USED(pack_info);
1317 l_band = l_res->bands;
1318 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1319 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1321 if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
1326 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1327 l_cblk = l_prc->cblks.dec;
1329 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1330 opj_tcd_seg_t *l_seg = 00;
1332 if (!l_cblk->numnewpasses) {
1338 if (!l_cblk->numsegs) {
1339 l_seg = l_cblk->segs;
1342 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1344 if (l_seg->numpasses == l_seg->maxpasses) {
1351 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
1352 if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) <
1353 (OPJ_SIZE_T)l_current_data) ||
1354 (l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
1355 opj_event_msg(p_manager, EVT_ERROR,
1356 "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1357 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
1363 /* we need here a j2k handle to verify if making a check to
1364 the validity of cblocks parameters is selected from user (-W) */
1366 /* let's check that we are not exceeding */
1367 if ((l_cblk->len + l_seg->newlen) > 8192) {
1368 opj_event_msg(p_manager, EVT_WARNING,
1369 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1370 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1372 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1375 l_seg->newlen = 8192 - l_cblk->len;
1376 opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
1381 #endif /* USE_JPWL */
1383 if (l_cblk->numchunks == l_cblk->numchunksalloc) {
1384 OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1;
1385 opj_tcd_seg_data_chunk_t* l_chunks =
1386 (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks,
1387 l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t));
1388 if (l_chunks == NULL) {
1389 opj_event_msg(p_manager, EVT_ERROR,
1390 "cannot allocate opj_tcd_seg_data_chunk_t* array");
1393 l_cblk->chunks = l_chunks;
1394 l_cblk->numchunksalloc = l_numchunksalloc;
1397 l_cblk->chunks[l_cblk->numchunks].data = l_current_data;
1398 l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen;
1399 l_cblk->numchunks ++;
1401 l_current_data += l_seg->newlen;
1402 l_seg->len += l_seg->newlen;
1403 l_seg->numpasses += l_seg->numnewpasses;
1404 l_cblk->numnewpasses -= l_seg->numnewpasses;
1406 l_seg->real_num_passes = l_seg->numpasses;
1408 if (l_cblk->numnewpasses > 0) {
1412 } while (l_cblk->numnewpasses > 0);
1414 l_cblk->real_num_segs = l_cblk->numsegs;
1416 } /* next code_block */
1421 *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1427 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
1428 opj_tcd_tile_t *p_tile,
1429 opj_pi_iterator_t *p_pi,
1430 OPJ_UINT32 * p_data_read,
1431 OPJ_UINT32 p_max_length,
1432 opj_packet_info_t *pack_info,
1433 opj_event_mgr_t *p_manager)
1435 OPJ_UINT32 bandno, cblkno;
1436 OPJ_UINT32 l_nb_code_blocks;
1437 opj_tcd_band_t *l_band = 00;
1438 opj_tcd_cblk_dec_t* l_cblk = 00;
1439 opj_tcd_resolution_t* l_res =
1440 &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1442 OPJ_ARG_NOT_USED(p_t2);
1443 OPJ_ARG_NOT_USED(pack_info);
1446 l_band = l_res->bands;
1448 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1449 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1451 if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
1456 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1457 l_cblk = l_prc->cblks.dec;
1459 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1460 opj_tcd_seg_t *l_seg = 00;
1462 if (!l_cblk->numnewpasses) {
1468 if (!l_cblk->numsegs) {
1469 l_seg = l_cblk->segs;
1472 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1474 if (l_seg->numpasses == l_seg->maxpasses) {
1481 /* Check possible overflow then size */
1482 if (((*p_data_read + l_seg->newlen) < (*p_data_read)) ||
1483 ((*p_data_read + l_seg->newlen) > p_max_length)) {
1484 opj_event_msg(p_manager, EVT_ERROR,
1485 "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1486 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
1492 /* we need here a j2k handle to verify if making a check to
1493 the validity of cblocks parameters is selected from user (-W) */
1495 /* let's check that we are not exceeding */
1496 if ((l_cblk->len + l_seg->newlen) > 8192) {
1497 opj_event_msg(p_manager, EVT_WARNING,
1498 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1499 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1501 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1504 l_seg->newlen = 8192 - l_cblk->len;
1505 opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
1510 #endif /* USE_JPWL */
1511 JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read,
1513 *(p_data_read) += l_seg->newlen;
1515 l_seg->numpasses += l_seg->numnewpasses;
1516 l_cblk->numnewpasses -= l_seg->numnewpasses;
1517 if (l_cblk->numnewpasses > 0) {
1521 } while (l_cblk->numnewpasses > 0);
1533 static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
1538 opj_tcd_seg_t* seg = 00;
1539 OPJ_UINT32 l_nb_segs = index + 1;
1541 if (l_nb_segs > cblk->m_current_max_segs) {
1542 opj_tcd_seg_t* new_segs;
1543 OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs +
1544 OPJ_J2K_DEFAULT_NB_SEGS;
1546 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs,
1547 l_m_current_max_segs * sizeof(opj_tcd_seg_t));
1549 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1552 cblk->segs = new_segs;
1553 memset(new_segs + cblk->m_current_max_segs,
1554 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));
1555 cblk->m_current_max_segs = l_m_current_max_segs;
1558 seg = &cblk->segs[index];
1559 opj_tcd_reinit_segment(seg);
1561 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1563 } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1565 seg->maxpasses = 10;
1567 seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
1568 ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1571 /* See paragraph "B.10.6 Number of coding passes" of the standard.
1572 * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37,
1573 * Mb being the maximum number of bit-planes available for the
1574 * representation of coefficients in the sub-band */
1575 seg->maxpasses = 109;