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 while (opj_pi_next(l_current_pi)) {
246 if (l_current_pi->layno < p_maxlayers) {
249 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)) {
250 opj_pi_destroy(l_pi, l_nb_pocs);
254 l_comp_len += l_nb_bytes;
255 l_current_data += l_nb_bytes;
256 p_max_len -= l_nb_bytes;
258 * p_data_written += l_nb_bytes;
262 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
263 if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
264 opj_pi_destroy(l_pi, l_nb_pocs);
273 else { /* t2_mode == FINAL_PASS */
274 opj_pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
276 l_current_pi = &l_pi[p_pino];
278 while (opj_pi_next(l_current_pi)) {
279 if (l_current_pi->layno < p_maxlayers) {
282 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)) {
283 opj_pi_destroy(l_pi, l_nb_pocs);
287 l_current_data += l_nb_bytes;
288 p_max_len -= l_nb_bytes;
290 * p_data_written += l_nb_bytes;
294 if(cstr_info->index_write) {
295 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
296 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
297 if (!cstr_info->packno) {
298 info_PK->start_pos = info_TL->end_header + 1;
300 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;
302 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
303 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
304 to start of packet is incremented by value of start of packet*/
315 opj_pi_destroy(l_pi, l_nb_pocs);
322 #define JAS_FPRINTF fprintf
325 static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
330 #define JAS_FPRINTF opj_null_jas_fprintf
333 OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2,
334 OPJ_UINT32 p_tile_no,
335 opj_tcd_tile_t *p_tile,
337 OPJ_UINT32 * p_data_read,
338 OPJ_UINT32 p_max_len,
339 opj_codestream_index_t *p_cstr_index)
341 OPJ_BYTE *l_current_data = p_src;
342 opj_pi_iterator_t *l_pi = 00;
344 opj_image_t *l_image = p_t2->image;
345 opj_cp_t *l_cp = p_t2->cp;
346 opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
347 OPJ_UINT32 l_nb_bytes_read;
348 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
349 opj_pi_iterator_t *l_current_pi = 00;
351 OPJ_UINT32 curtp = 0;
352 OPJ_UINT32 tp_start_packno;
354 opj_packet_info_t *l_pack_info = 00;
355 opj_image_comp_t* l_img_comp = 00;
357 OPJ_ARG_NOT_USED(p_cstr_index);
361 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
365 /* create a packet iterator */
366 l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
374 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
376 /* if the resolution needed is too low, one dim of the tilec could be equal to zero
377 * and no packets are used to decode this resolution and
378 * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
379 * and no l_img_comp->resno_decoded are computed
381 OPJ_BOOL* first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
382 if (!first_pass_failed)
384 opj_pi_destroy(l_pi,l_nb_pocs);
387 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
389 while (opj_pi_next(l_current_pi)) {
390 JAS_FPRINTF( stderr, "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
391 l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, l_current_pi->precno, l_current_pi->layno );
393 if (l_tcp->num_layers_to_decode > l_current_pi->layno
394 && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
397 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
399 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)) {
400 opj_pi_destroy(l_pi,l_nb_pocs);
401 opj_free(first_pass_failed);
405 l_img_comp = &(l_image->comps[l_current_pi->compno]);
406 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
410 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)) {
411 opj_pi_destroy(l_pi,l_nb_pocs);
412 opj_free(first_pass_failed);
417 if (first_pass_failed[l_current_pi->compno]) {
418 l_img_comp = &(l_image->comps[l_current_pi->compno]);
419 if (l_img_comp->resno_decoded == 0)
420 l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
423 l_current_data += l_nb_bytes_read;
424 p_max_len -= l_nb_bytes_read;
429 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
430 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
432 if (!p_cstr_info->packno) {
433 info_PK->start_pos = info_TL->end_header + 1;
434 } 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 */
435 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
436 tp_start_packno = p_cstr_info->packno;
438 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
440 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;
442 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
443 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */
444 ++p_cstr_info->packno;
451 opj_free(first_pass_failed);
457 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 */
462 /* don't forget to release pi */
463 opj_pi_destroy(l_pi,l_nb_pocs);
464 *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
468 /* ----------------------------------------------------------------------- */
471 * Creates a Tier 2 handle
473 * @param p_image Source or destination image
474 * @param p_cp Image coding parameters.
475 * @return a new T2 handle if successful, NULL otherwise.
477 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
479 /* create the t2 structure */
480 opj_t2_t *l_t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
484 memset(l_t2,0,sizeof(opj_t2_t));
486 l_t2->image = p_image;
492 void opj_t2_destroy(opj_t2_t *t2) {
498 OPJ_BOOL opj_t2_decode_packet( opj_t2_t* p_t2,
499 opj_tcd_tile_t *p_tile,
501 opj_pi_iterator_t *p_pi,
503 OPJ_UINT32 * p_data_read,
504 OPJ_UINT32 p_max_length,
505 opj_packet_info_t *p_pack_info)
507 OPJ_BOOL l_read_data;
508 OPJ_UINT32 l_nb_bytes_read = 0;
509 OPJ_UINT32 l_nb_total_bytes_read = 0;
513 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)) {
517 p_src += l_nb_bytes_read;
518 l_nb_total_bytes_read += l_nb_bytes_read;
519 p_max_length -= l_nb_bytes_read;
521 /* we should read data for the packet */
525 if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
529 l_nb_total_bytes_read += l_nb_bytes_read;
532 *p_data_read = l_nb_total_bytes_read;
537 OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno,
538 opj_tcd_tile_t * tile,
540 opj_pi_iterator_t *pi,
542 OPJ_UINT32 * p_data_written,
544 opj_codestream_info_t *cstr_info)
546 OPJ_UINT32 bandno, cblkno;
548 OPJ_UINT32 l_nb_bytes;
549 OPJ_UINT32 compno = pi->compno; /* component value */
550 OPJ_UINT32 resno = pi->resno; /* resolution level value */
551 OPJ_UINT32 precno = pi->precno; /* precinct value */
552 OPJ_UINT32 layno = pi->layno; /* quality layer value */
553 OPJ_UINT32 l_nb_blocks;
554 opj_tcd_band_t *band = 00;
555 opj_tcd_cblk_enc_t* cblk = 00;
556 opj_tcd_pass_t *pass = 00;
558 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
559 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
561 opj_bio_t *bio = 00; /* BIO component */
564 if (tcp->csty & J2K_CP_CSTY_SOP) {
570 c[4] = (tile->packno % 65536) / 256;
571 c[5] = (tile->packno % 65536) % 256;
573 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
574 c[5] = tile->packno & 0xff;
584 for(bandno = 0; bandno < res->numbands; ++bandno) {
585 opj_tcd_precinct_t *prc = &band->precincts[precno];
587 opj_tgt_reset(prc->incltree);
588 opj_tgt_reset(prc->imsbtree);
590 l_nb_blocks = prc->cw * prc->ch;
591 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
592 cblk = &prc->cblks.enc[cblkno];
595 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
601 bio = opj_bio_create();
602 opj_bio_init_enc(bio, c, length);
603 opj_bio_write(bio, 1, 1); /* Empty header bit */
605 /* Writing Packet header */
607 for (bandno = 0; bandno < res->numbands; ++bandno) {
608 opj_tcd_precinct_t *prc = &band->precincts[precno];
610 l_nb_blocks = prc->cw * prc->ch;
611 cblk = prc->cblks.enc;
613 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
614 opj_tcd_layer_t *layer = &cblk->layers[layno];
616 if (!cblk->numpasses && layer->numpasses) {
617 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
623 cblk = prc->cblks.enc;
624 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
625 opj_tcd_layer_t *layer = &cblk->layers[layno];
626 OPJ_UINT32 increment = 0;
628 OPJ_UINT32 len = 0, passno;
629 OPJ_UINT32 l_nb_passes;
631 /* cblk inclusion bits */
632 if (!cblk->numpasses) {
633 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
635 opj_bio_write(bio, layer->numpasses != 0, 1);
638 /* if cblk not included, go to the next cblk */
639 if (!layer->numpasses) {
644 /* if first instance of cblk --> zero bit-planes information */
645 if (!cblk->numpasses) {
646 cblk->numlenbits = 3;
647 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
650 /* number of coding passes included */
651 opj_t2_putnumpasses(bio, layer->numpasses);
652 l_nb_passes = cblk->numpasses + layer->numpasses;
653 pass = cblk->passes + cblk->numpasses;
655 /* computation of the increase of the length indicator and insertion in the header */
656 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
660 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
661 increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, opj_int_floorlog2((OPJ_INT32)len) + 1
662 - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
669 opj_t2_putcommacode(bio, (OPJ_INT32)increment);
671 /* computation of the new Length indicator */
672 cblk->numlenbits += increment;
674 pass = cblk->passes + cblk->numpasses;
675 /* insertion of the codeword segment length */
676 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
680 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
681 opj_bio_write(bio, (OPJ_UINT32)len, cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
694 if (!opj_bio_flush(bio)) {
695 opj_bio_destroy(bio);
696 return OPJ_FALSE; /* modified to eliminate longjmp !! */
699 l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
701 length -= l_nb_bytes;
703 opj_bio_destroy(bio);
706 if (tcp->csty & J2K_CP_CSTY_EPH) {
715 /* End of packet header position. Currently only represents the distance to start of packet
716 Will be updated later by incrementing with packet start value*/
717 if(cstr_info && cstr_info->index_write) {
718 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
719 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
723 /* Writing the packet body */
725 for (bandno = 0; bandno < res->numbands; bandno++) {
726 opj_tcd_precinct_t *prc = &band->precincts[precno];
728 l_nb_blocks = prc->cw * prc->ch;
729 cblk = prc->cblks.enc;
731 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
732 opj_tcd_layer_t *layer = &cblk->layers[layno];
734 if (!layer->numpasses) {
739 if (layer->len > length) {
743 memcpy(c, layer->data, layer->len);
744 cblk->numpasses += layer->numpasses;
746 length -= layer->len;
749 if(cstr_info && cstr_info->index_write) {
750 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
751 info_PK->disto += layer->disto;
752 if (cstr_info->D_max < info_PK->disto) {
753 cstr_info->D_max = info_PK->disto;
764 * p_data_written += (OPJ_UINT32)(c - dest);
769 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
770 opj_tcd_tile_t *p_tile,
772 opj_pi_iterator_t *p_pi,
774 OPJ_UINT32 * p_data_read,
775 OPJ_UINT32 p_max_length,
776 opj_packet_info_t *p_pack_info)
778 OPJ_BOOL l_read_data;
779 OPJ_UINT32 l_nb_bytes_read = 0;
780 OPJ_UINT32 l_nb_total_bytes_read = 0;
784 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)) {
788 p_src += l_nb_bytes_read;
789 l_nb_total_bytes_read += l_nb_bytes_read;
790 p_max_length -= l_nb_bytes_read;
792 /* we should read data for the packet */
796 if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
800 l_nb_total_bytes_read += l_nb_bytes_read;
802 *p_data_read = l_nb_total_bytes_read;
808 OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
809 opj_tcd_tile_t *p_tile,
811 opj_pi_iterator_t *p_pi,
812 OPJ_BOOL * p_is_data_present,
813 OPJ_BYTE *p_src_data,
814 OPJ_UINT32 * p_data_read,
815 OPJ_UINT32 p_max_length,
816 opj_packet_info_t *p_pack_info)
820 OPJ_UINT32 bandno, cblkno;
821 OPJ_UINT32 l_nb_code_blocks;
822 OPJ_UINT32 l_remaining_length;
823 OPJ_UINT32 l_header_length;
824 OPJ_UINT32 * l_modified_length_ptr = 00;
825 OPJ_BYTE *l_current_data = p_src_data;
826 opj_cp_t *l_cp = p_t2->cp;
827 opj_bio_t *l_bio = 00; /* BIO component */
828 opj_tcd_band_t *l_band = 00;
829 opj_tcd_cblk_dec_t* l_cblk = 00;
830 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
832 OPJ_BYTE *l_header_data = 00;
833 OPJ_BYTE **l_header_data_start = 00;
835 OPJ_UINT32 l_present;
837 if (p_pi->layno == 0) {
838 l_band = l_res->bands;
841 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
842 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
844 if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
845 opj_tgt_reset(l_prc->incltree);
846 opj_tgt_reset(l_prc->imsbtree);
847 l_cblk = l_prc->cblks.dec;
849 l_nb_code_blocks = l_prc->cw * l_prc->ch;
850 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
852 l_cblk->real_num_segs = 0;
863 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
864 if (p_max_length < 6) {
865 /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Not enough space for expected SOP marker\n"); */
866 printf("Not enough space for expected SOP marker\n");
867 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
868 /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
869 printf("Expected SOP marker\n");
870 fprintf(stderr, "Error : expected SOP marker\n");
875 /** TODO : check the Nsop value */
879 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
880 This part deal with this caracteristic
881 step 1: Read packet header in the saved structure
882 step 2: Return to codestream for decoding
885 l_bio = opj_bio_create();
890 if (l_cp->ppm == 1) { /* PPM */
891 l_header_data_start = &l_cp->ppm_data;
892 l_header_data = *l_header_data_start;
893 l_modified_length_ptr = &(l_cp->ppm_len);
896 else if (p_tcp->ppt == 1) { /* PPT */
897 l_header_data_start = &(p_tcp->ppt_data);
898 l_header_data = *l_header_data_start;
899 l_modified_length_ptr = &(p_tcp->ppt_len);
901 else { /* Normal Case */
902 l_header_data_start = &(l_current_data);
903 l_header_data = *l_header_data_start;
904 l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
905 l_modified_length_ptr = &(l_remaining_length);
908 opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
910 l_present = opj_bio_read(l_bio, 1);
911 JAS_FPRINTF(stderr, "present=%d \n", l_present );
913 /* TODO MSD: no test to control the output of this function*/
914 opj_bio_inalign(l_bio);
915 l_header_data += opj_bio_numbytes(l_bio);
916 opj_bio_destroy(l_bio);
919 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
920 if (p_max_length < 2) {
921 fprintf(stderr, "Not enough space for expected EPH marker\n");
922 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
923 fprintf(stderr, "Error : expected EPH marker\n");
929 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
930 *l_modified_length_ptr -= l_header_length;
931 *l_header_data_start += l_header_length;
934 /* End of packet header position. Currently only represents the distance to start of packet
935 Will be updated later by incrementing with packet start value */
937 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
941 * p_is_data_present = OPJ_FALSE;
942 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
946 l_band = l_res->bands;
947 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
948 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
950 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
955 l_nb_code_blocks = l_prc->cw * l_prc->ch;
956 l_cblk = l_prc->cblks.dec;
957 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
958 OPJ_UINT32 l_included,l_increment, l_segno;
961 /* if cblk not yet included before --> inclusion tagtree */
962 if (!l_cblk->numsegs) {
963 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, (OPJ_INT32)(p_pi->layno + 1));
967 l_included = opj_bio_read(l_bio, 1);
970 /* if cblk not included */
972 l_cblk->numnewpasses = 0;
974 JAS_FPRINTF(stderr, "included=%d \n", l_included);
978 /* if cblk not yet included --> zero-bitplane tagtree */
979 if (!l_cblk->numsegs) {
982 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
986 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
987 l_cblk->numlenbits = 3;
990 /* number of coding passes */
991 l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
992 l_increment = opj_t2_getcommacode(l_bio);
994 /* length indicator increment */
995 l_cblk->numlenbits += l_increment;
998 if (!l_cblk->numsegs) {
999 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1000 opj_bio_destroy(l_bio);
1005 l_segno = l_cblk->numsegs - 1;
1006 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1008 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1009 opj_bio_destroy(l_bio);
1014 n = (OPJ_INT32)l_cblk->numnewpasses;
1017 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);
1018 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1019 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 );
1021 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1025 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1026 opj_bio_destroy(l_bio);
1038 if (!opj_bio_inalign(l_bio)) {
1039 opj_bio_destroy(l_bio);
1043 l_header_data += opj_bio_numbytes(l_bio);
1044 opj_bio_destroy(l_bio);
1047 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1048 if (p_max_length < 2) {
1049 fprintf(stderr, "Not enough space for expected EPH marker\n");
1050 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1051 /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1052 fprintf(stderr, "Error : expected EPH marker\n");
1058 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1059 JAS_FPRINTF( stderr, "hdrlen=%d \n", l_header_length );
1060 JAS_FPRINTF( stderr, "packet body\n");
1061 *l_modified_length_ptr -= l_header_length;
1062 *l_header_data_start += l_header_length;
1065 /* End of packet header position. Currently only represents the distance to start of packet
1066 Will be updated later by incrementing with packet start value */
1068 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1072 *p_is_data_present = OPJ_TRUE;
1073 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1078 OPJ_BOOL opj_t2_read_packet_data( opj_t2_t* p_t2,
1079 opj_tcd_tile_t *p_tile,
1080 opj_pi_iterator_t *p_pi,
1081 OPJ_BYTE *p_src_data,
1082 OPJ_UINT32 * p_data_read,
1083 OPJ_UINT32 p_max_length,
1084 opj_packet_info_t *pack_info)
1086 OPJ_UINT32 bandno, cblkno;
1087 OPJ_UINT32 l_nb_code_blocks;
1088 OPJ_BYTE *l_current_data = p_src_data;
1089 opj_tcd_band_t *l_band = 00;
1090 opj_tcd_cblk_dec_t* l_cblk = 00;
1091 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1093 OPJ_ARG_NOT_USED(p_t2);
1094 OPJ_ARG_NOT_USED(pack_info);
1096 l_band = l_res->bands;
1097 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1098 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1100 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1105 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1106 l_cblk = l_prc->cblks.dec;
1108 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1109 opj_tcd_seg_t *l_seg = 00;
1111 if (!l_cblk->numnewpasses) {
1117 if (!l_cblk->numsegs) {
1118 l_seg = l_cblk->segs;
1120 l_cblk->data_current_size = 0;
1123 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1125 if (l_seg->numpasses == l_seg->maxpasses) {
1132 if (l_current_data + l_seg->newlen > p_src_data + p_max_length) {
1133 fprintf(stderr, "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1134 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1139 /* we need here a j2k handle to verify if making a check to
1140 the validity of cblocks parameters is selected from user (-W) */
1142 /* let's check that we are not exceeding */
1143 if ((l_cblk->len + l_seg->newlen) > 8192) {
1144 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1145 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1146 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1148 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1151 l_seg->newlen = 8192 - l_cblk->len;
1152 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1156 #endif /* USE_JPWL */
1157 /* Check if the cblk->data have allocated enough memory */
1158 if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
1159 OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
1160 if(! new_cblk_data) {
1161 opj_free(l_cblk->data);
1162 l_cblk->data_max_size = 0;
1163 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
1166 l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
1167 l_cblk->data = new_cblk_data;
1170 memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
1172 if (l_seg->numpasses == 0) {
1173 l_seg->data = &l_cblk->data;
1174 l_seg->dataindex = l_cblk->data_current_size;
1177 l_current_data += l_seg->newlen;
1178 l_seg->numpasses += l_seg->numnewpasses;
1179 l_cblk->numnewpasses -= l_seg->numnewpasses;
1181 l_seg->real_num_passes = l_seg->numpasses;
1182 l_cblk->data_current_size += l_seg->newlen;
1183 l_seg->len += l_seg->newlen;
1185 if (l_cblk->numnewpasses > 0) {
1189 } while (l_cblk->numnewpasses > 0);
1191 l_cblk->real_num_segs = l_cblk->numsegs;
1193 } /* next code_block */
1198 *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1203 OPJ_BOOL opj_t2_skip_packet_data( opj_t2_t* p_t2,
1204 opj_tcd_tile_t *p_tile,
1205 opj_pi_iterator_t *p_pi,
1206 OPJ_UINT32 * p_data_read,
1207 OPJ_UINT32 p_max_length,
1208 opj_packet_info_t *pack_info)
1210 OPJ_UINT32 bandno, cblkno;
1211 OPJ_UINT32 l_nb_code_blocks;
1212 opj_tcd_band_t *l_band = 00;
1213 opj_tcd_cblk_dec_t* l_cblk = 00;
1214 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1216 OPJ_ARG_NOT_USED(p_t2);
1217 OPJ_ARG_NOT_USED(pack_info);
1220 l_band = l_res->bands;
1222 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1223 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1225 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1230 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1231 l_cblk = l_prc->cblks.dec;
1233 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1234 opj_tcd_seg_t *l_seg = 00;
1236 if (!l_cblk->numnewpasses) {
1242 if (!l_cblk->numsegs) {
1243 l_seg = l_cblk->segs;
1245 l_cblk->data_current_size = 0;
1248 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1250 if (l_seg->numpasses == l_seg->maxpasses) {
1257 if (* p_data_read + l_seg->newlen > p_max_length) {
1258 fprintf(stderr, "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1259 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1264 /* we need here a j2k handle to verify if making a check to
1265 the validity of cblocks parameters is selected from user (-W) */
1267 /* let's check that we are not exceeding */
1268 if ((l_cblk->len + l_seg->newlen) > 8192) {
1269 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1270 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1271 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1273 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1276 l_seg->newlen = 8192 - l_cblk->len;
1277 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1281 #endif /* USE_JPWL */
1282 JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, l_seg->newlen );
1283 *(p_data_read) += l_seg->newlen;
1285 l_seg->numpasses += l_seg->numnewpasses;
1286 l_cblk->numnewpasses -= l_seg->numnewpasses;
1287 if (l_cblk->numnewpasses > 0)
1292 } while (l_cblk->numnewpasses > 0);
1304 OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
1309 opj_tcd_seg_t* seg = 00;
1310 OPJ_UINT32 l_nb_segs = index + 1;
1312 if (l_nb_segs > cblk->m_current_max_segs) {
1313 opj_tcd_seg_t* new_segs;
1314 cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
1316 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1318 opj_free(cblk->segs);
1320 cblk->m_current_max_segs = 0;
1321 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1324 cblk->segs = new_segs;
1327 seg = &cblk->segs[index];
1328 memset(seg,0,sizeof(opj_tcd_seg_t));
1330 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1333 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1335 seg->maxpasses = 10;
1337 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1340 seg->maxpasses = 109;