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"
41 #include "opj_common.h"
44 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
47 /** @name Local static functions */
50 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
52 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio);
54 Variable length code for signalling delta Zil (truncation point)
55 @param bio Bit Input/Output component
58 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
59 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
62 Encode a packet of a tile to a destination buffer
63 @param tileno Number of the tile encoded
64 @param tile Tile for which to write the packets
65 @param tcp Tile coding parameters
66 @param pi Packet identity
67 @param dest Destination buffer
68 @param p_data_written FIXME DOC
69 @param len Length of the destination buffer
70 @param cstr_info Codestream information structure
71 @param p_manager the user event manager
74 static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
77 opj_pi_iterator_t *pi,
79 OPJ_UINT32 * p_data_written,
81 opj_codestream_info_t *cstr_info,
82 opj_event_mgr_t *p_manager);
85 Decode a packet of a tile from a source buffer
87 @param tile Tile for which to write the packets
88 @param tcp Tile coding parameters
89 @param pi Packet identity
90 @param src Source buffer
91 @param data_read FIXME DOC
92 @param max_length FIXME DOC
93 @param pack_info Packet information
94 @param p_manager the user event manager
98 static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* t2,
101 opj_pi_iterator_t *pi,
103 OPJ_UINT32 * data_read,
104 OPJ_UINT32 max_length,
105 opj_packet_info_t *pack_info,
106 opj_event_mgr_t *p_manager);
108 static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2,
109 opj_tcd_tile_t *p_tile,
111 opj_pi_iterator_t *p_pi,
113 OPJ_UINT32 * p_data_read,
114 OPJ_UINT32 p_max_length,
115 opj_packet_info_t *p_pack_info,
116 opj_event_mgr_t *p_manager);
118 static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2,
119 opj_tcd_tile_t *p_tile,
121 opj_pi_iterator_t *p_pi,
122 OPJ_BOOL * p_is_data_present,
123 OPJ_BYTE *p_src_data,
124 OPJ_UINT32 * p_data_read,
125 OPJ_UINT32 p_max_length,
126 opj_packet_info_t *p_pack_info,
127 opj_event_mgr_t *p_manager);
129 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
130 opj_tcd_tile_t *p_tile,
131 opj_pi_iterator_t *p_pi,
132 OPJ_BYTE *p_src_data,
133 OPJ_UINT32 * p_data_read,
134 OPJ_UINT32 p_max_length,
135 opj_packet_info_t *pack_info,
136 opj_event_mgr_t *p_manager);
138 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
139 opj_tcd_tile_t *p_tile,
140 opj_pi_iterator_t *p_pi,
141 OPJ_UINT32 * p_data_read,
142 OPJ_UINT32 p_max_length,
143 opj_packet_info_t *pack_info,
144 opj_event_mgr_t *p_manager);
152 static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
161 /* ----------------------------------------------------------------------- */
163 /* #define RESTART 0x04 */
164 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n)
167 opj_bio_write(bio, 1, 1);
169 opj_bio_write(bio, 0, 1);
172 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
175 while (opj_bio_read(bio, 1)) {
181 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n)
184 opj_bio_write(bio, 0, 1);
186 opj_bio_write(bio, 2, 2);
188 opj_bio_write(bio, 0xc | (n - 3), 4);
189 } else if (n <= 36) {
190 opj_bio_write(bio, 0x1e0 | (n - 6), 9);
191 } else if (n <= 164) {
192 opj_bio_write(bio, 0xff80 | (n - 37), 16);
196 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio)
199 if (!opj_bio_read(bio, 1)) {
202 if (!opj_bio_read(bio, 1)) {
205 if ((n = opj_bio_read(bio, 2)) != 3) {
208 if ((n = opj_bio_read(bio, 5)) != 31) {
211 return (37 + opj_bio_read(bio, 7));
214 /* ----------------------------------------------------------------------- */
216 OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2,
217 OPJ_UINT32 p_tile_no,
218 opj_tcd_tile_t *p_tile,
219 OPJ_UINT32 p_maxlayers,
221 OPJ_UINT32 * p_data_written,
222 OPJ_UINT32 p_max_len,
223 opj_codestream_info_t *cstr_info,
227 J2K_T2_MODE p_t2_mode,
228 opj_event_mgr_t *p_manager)
230 OPJ_BYTE *l_current_data = p_dest;
231 OPJ_UINT32 l_nb_bytes = 0;
234 opj_pi_iterator_t *l_pi = 00;
235 opj_pi_iterator_t *l_current_pi = 00;
236 opj_image_t *l_image = p_t2->image;
237 opj_cp_t *l_cp = p_t2->cp;
238 opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
239 OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K) ? 2 : 1;
240 OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ?
241 l_image->numcomps : 1;
242 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
244 l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
249 * p_data_written = 0;
251 if (p_t2_mode == THRESH_CALC) { /* Calculating threshold */
254 for (compno = 0; compno < l_max_comp; ++compno) {
255 OPJ_UINT32 l_comp_len = 0;
258 for (poc = 0; poc < pocno ; ++poc) {
259 OPJ_UINT32 l_tp_num = compno;
261 /* TODO MSD : check why this function cannot fail (cf. v1) */
262 opj_pi_create_encode(l_pi, l_cp, p_tile_no, poc, l_tp_num, p_tp_pos, p_t2_mode);
264 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
265 /* TODO ADE : add an error */
266 opj_pi_destroy(l_pi, l_nb_pocs);
269 while (opj_pi_next(l_current_pi)) {
270 if (l_current_pi->layno < p_maxlayers) {
273 if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
274 l_current_data, &l_nb_bytes,
275 p_max_len, cstr_info,
277 opj_pi_destroy(l_pi, l_nb_pocs);
281 l_comp_len += l_nb_bytes;
282 l_current_data += l_nb_bytes;
283 p_max_len -= l_nb_bytes;
285 * p_data_written += l_nb_bytes;
289 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
290 if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
291 opj_pi_destroy(l_pi, l_nb_pocs);
299 } else { /* t2_mode == FINAL_PASS */
300 opj_pi_create_encode(l_pi, l_cp, p_tile_no, p_pino, p_tp_num, p_tp_pos,
303 l_current_pi = &l_pi[p_pino];
304 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
305 /* TODO ADE : add an error */
306 opj_pi_destroy(l_pi, l_nb_pocs);
309 while (opj_pi_next(l_current_pi)) {
310 if (l_current_pi->layno < p_maxlayers) {
313 if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
314 l_current_data, &l_nb_bytes, p_max_len, cstr_info, p_manager)) {
315 opj_pi_destroy(l_pi, l_nb_pocs);
319 l_current_data += l_nb_bytes;
320 p_max_len -= l_nb_bytes;
322 * p_data_written += l_nb_bytes;
326 if (cstr_info->index_write) {
327 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
328 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
329 if (!cstr_info->packno) {
330 info_PK->start_pos = info_TL->end_header + 1;
332 info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC) &&
333 info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno -
336 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
337 info_PK->end_ph_pos += info_PK->start_pos -
338 1; /* End of packet header which now only represents the distance
339 to start of packet is incremented by value of start of packet*/
350 opj_pi_destroy(l_pi, l_nb_pocs);
357 #define JAS_FPRINTF fprintf
360 static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
365 #define JAS_FPRINTF opj_null_jas_fprintf
368 OPJ_BOOL opj_t2_decode_packets(opj_t2_t *p_t2,
369 OPJ_UINT32 p_tile_no,
370 opj_tcd_tile_t *p_tile,
372 OPJ_UINT32 * p_data_read,
373 OPJ_UINT32 p_max_len,
374 opj_codestream_index_t *p_cstr_index,
375 opj_event_mgr_t *p_manager)
377 OPJ_BYTE *l_current_data = p_src;
378 opj_pi_iterator_t *l_pi = 00;
380 opj_image_t *l_image = p_t2->image;
381 opj_cp_t *l_cp = p_t2->cp;
382 opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
383 OPJ_UINT32 l_nb_bytes_read;
384 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
385 opj_pi_iterator_t *l_current_pi = 00;
387 OPJ_UINT32 curtp = 0;
388 OPJ_UINT32 tp_start_packno;
390 opj_packet_info_t *l_pack_info = 00;
391 opj_image_comp_t* l_img_comp = 00;
393 OPJ_ARG_NOT_USED(p_cstr_index);
397 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
401 /* create a packet iterator */
402 l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
410 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
412 /* if the resolution needed is too low, one dim of the tilec could be equal to zero
413 * and no packets are used to decode this resolution and
414 * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
415 * and no l_img_comp->resno_decoded are computed
417 OPJ_BOOL* first_pass_failed = NULL;
419 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
420 /* TODO ADE : add an error */
421 opj_pi_destroy(l_pi, l_nb_pocs);
425 first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
426 if (!first_pass_failed) {
427 opj_pi_destroy(l_pi, l_nb_pocs);
430 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
432 while (opj_pi_next(l_current_pi)) {
434 "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
435 l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno,
436 l_current_pi->precno, l_current_pi->layno);
438 if (l_tcp->num_layers_to_decode > l_current_pi->layno
439 && l_current_pi->resno <
440 p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
443 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
445 if (! opj_t2_decode_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data,
446 &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) {
447 opj_pi_destroy(l_pi, l_nb_pocs);
448 opj_free(first_pass_failed);
452 l_img_comp = &(l_image->comps[l_current_pi->compno]);
453 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno,
454 l_img_comp->resno_decoded);
457 if (! opj_t2_skip_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data,
458 &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) {
459 opj_pi_destroy(l_pi, l_nb_pocs);
460 opj_free(first_pass_failed);
465 if (first_pass_failed[l_current_pi->compno]) {
466 l_img_comp = &(l_image->comps[l_current_pi->compno]);
467 if (l_img_comp->resno_decoded == 0) {
468 l_img_comp->resno_decoded =
469 p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
473 l_current_data += l_nb_bytes_read;
474 p_max_len -= l_nb_bytes_read;
479 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
480 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
482 if (!p_cstr_info->packno) {
483 info_PK->start_pos = info_TL->end_header + 1;
484 } else if (info_TL->packet[p_cstr_info->packno - 1].end_pos >=
486 p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos) { /* New tile part */
487 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno -
488 tp_start_packno; /* Number of packets in previous tile-part */
489 tp_start_packno = p_cstr_info->packno;
491 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header + 1;
493 info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on &&
494 info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno -
497 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
498 info_PK->end_ph_pos += info_PK->start_pos -
499 1; /* End of packet header which now only represents the distance */
500 ++p_cstr_info->packno;
507 opj_free(first_pass_failed);
513 p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno -
514 tp_start_packno; /* Number of packets in last tile-part */
519 /* don't forget to release pi */
520 opj_pi_destroy(l_pi, l_nb_pocs);
521 *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
525 /* ----------------------------------------------------------------------- */
528 * Creates a Tier 2 handle
530 * @param p_image Source or destination image
531 * @param p_cp Image coding parameters.
532 * @return a new T2 handle if successful, NULL otherwise.
534 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
536 /* create the t2 structure */
537 opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1, sizeof(opj_t2_t));
542 l_t2->image = p_image;
548 void opj_t2_destroy(opj_t2_t *t2)
555 static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* p_t2,
556 opj_tcd_tile_t *p_tile,
558 opj_pi_iterator_t *p_pi,
560 OPJ_UINT32 * p_data_read,
561 OPJ_UINT32 p_max_length,
562 opj_packet_info_t *p_pack_info,
563 opj_event_mgr_t *p_manager)
565 OPJ_BOOL l_read_data;
566 OPJ_UINT32 l_nb_bytes_read = 0;
567 OPJ_UINT32 l_nb_total_bytes_read = 0;
571 if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src,
572 &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) {
576 p_src += l_nb_bytes_read;
577 l_nb_total_bytes_read += l_nb_bytes_read;
578 p_max_length -= l_nb_bytes_read;
580 /* we should read data for the packet */
584 if (! opj_t2_read_packet_data(p_t2, p_tile, p_pi, p_src, &l_nb_bytes_read,
585 p_max_length, p_pack_info, p_manager)) {
589 l_nb_total_bytes_read += l_nb_bytes_read;
592 *p_data_read = l_nb_total_bytes_read;
597 static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
598 opj_tcd_tile_t * tile,
600 opj_pi_iterator_t *pi,
602 OPJ_UINT32 * p_data_written,
604 opj_codestream_info_t *cstr_info,
605 opj_event_mgr_t *p_manager)
607 OPJ_UINT32 bandno, cblkno;
609 OPJ_UINT32 l_nb_bytes;
610 OPJ_UINT32 compno = pi->compno; /* component value */
611 OPJ_UINT32 resno = pi->resno; /* resolution level value */
612 OPJ_UINT32 precno = pi->precno; /* precinct value */
613 OPJ_UINT32 layno = pi->layno; /* quality layer value */
614 OPJ_UINT32 l_nb_blocks;
615 opj_tcd_band_t *band = 00;
616 opj_tcd_cblk_enc_t* cblk = 00;
617 opj_tcd_pass_t *pass = 00;
619 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
620 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
622 opj_bio_t *bio = 00; /* BIO component */
623 OPJ_BOOL packet_empty = OPJ_TRUE;
626 if (tcp->csty & J2K_CP_CSTY_SOP) {
632 c[4] = (tile->packno % 65536) / 256;
633 c[5] = (tile->packno % 65536) % 256;
635 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
636 c[5] = tile->packno & 0xff;
646 for (bandno = 0; bandno < res->numbands; ++bandno, ++band) {
647 opj_tcd_precinct_t *prc;
649 /* Skip empty bands */
650 if (opj_tcd_is_band_empty(band)) {
654 prc = &band->precincts[precno];
655 opj_tgt_reset(prc->incltree);
656 opj_tgt_reset(prc->imsbtree);
658 l_nb_blocks = prc->cw * prc->ch;
659 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
660 cblk = &prc->cblks.enc[cblkno];
663 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
668 bio = opj_bio_create();
670 /* FIXME event manager error callback */
673 opj_bio_init_enc(bio, c, length);
675 /* Check if the packet is empty */
676 /* Note: we could also skip that step and always write a packet header */
678 for (bandno = 0; bandno < res->numbands; ++bandno, ++band) {
679 opj_tcd_precinct_t *prc;
680 /* Skip empty bands */
681 if (opj_tcd_is_band_empty(band)) {
685 prc = &band->precincts[precno];
686 l_nb_blocks = prc->cw * prc->ch;
687 cblk = prc->cblks.enc;
688 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++, ++cblk) {
689 opj_tcd_layer_t *layer = &cblk->layers[layno];
691 /* if cblk not included, go to the next cblk */
692 if (!layer->numpasses) {
695 packet_empty = OPJ_FALSE;
703 opj_bio_write(bio, packet_empty ? 0 : 1, 1); /* Empty header bit */
706 /* Writing Packet header */
708 for (bandno = 0; !packet_empty &&
709 bandno < res->numbands; ++bandno, ++band) {
710 opj_tcd_precinct_t *prc;
712 /* Skip empty bands */
713 if (opj_tcd_is_band_empty(band)) {
717 prc = &band->precincts[precno];
718 l_nb_blocks = prc->cw * prc->ch;
719 cblk = prc->cblks.enc;
721 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
722 opj_tcd_layer_t *layer = &cblk->layers[layno];
724 if (!cblk->numpasses && layer->numpasses) {
725 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
731 cblk = prc->cblks.enc;
732 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
733 opj_tcd_layer_t *layer = &cblk->layers[layno];
734 OPJ_UINT32 increment = 0;
736 OPJ_UINT32 len = 0, passno;
737 OPJ_UINT32 l_nb_passes;
739 /* cblk inclusion bits */
740 if (!cblk->numpasses) {
741 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
743 opj_bio_write(bio, layer->numpasses != 0, 1);
746 /* if cblk not included, go to the next cblk */
747 if (!layer->numpasses) {
752 /* if first instance of cblk --> zero bit-planes information */
753 if (!cblk->numpasses) {
754 cblk->numlenbits = 3;
755 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
758 /* number of coding passes included */
759 opj_t2_putnumpasses(bio, layer->numpasses);
760 l_nb_passes = cblk->numpasses + layer->numpasses;
761 pass = cblk->passes + cblk->numpasses;
763 /* computation of the increase of the length indicator and insertion in the header */
764 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
768 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
769 increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment,
770 opj_int_floorlog2((OPJ_INT32)len) + 1
771 - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
778 opj_t2_putcommacode(bio, (OPJ_INT32)increment);
780 /* computation of the new Length indicator */
781 cblk->numlenbits += increment;
783 pass = cblk->passes + cblk->numpasses;
784 /* insertion of the codeword segment length */
785 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
789 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
790 opj_bio_write(bio, (OPJ_UINT32)len,
791 cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
802 if (!opj_bio_flush(bio)) {
803 opj_bio_destroy(bio);
804 return OPJ_FALSE; /* modified to eliminate longjmp !! */
807 l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
809 length -= l_nb_bytes;
811 opj_bio_destroy(bio);
814 if (tcp->csty & J2K_CP_CSTY_EPH) {
823 /* End of packet header position. Currently only represents the distance to start of packet
824 Will be updated later by incrementing with packet start value*/
825 if (cstr_info && cstr_info->index_write) {
826 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
827 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
831 /* Writing the packet body */
833 for (bandno = 0; !packet_empty && bandno < res->numbands; bandno++, ++band) {
834 opj_tcd_precinct_t *prc;
836 /* Skip empty bands */
837 if (opj_tcd_is_band_empty(band)) {
841 prc = &band->precincts[precno];
842 l_nb_blocks = prc->cw * prc->ch;
843 cblk = prc->cblks.enc;
845 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
846 opj_tcd_layer_t *layer = &cblk->layers[layno];
848 if (!layer->numpasses) {
853 if (layer->len > length) {
854 opj_event_msg(p_manager, EVT_ERROR,
855 "opj_t2_encode_packet(): only %u bytes remaining in "
856 "output buffer. %u needed.\n",
861 memcpy(c, layer->data, layer->len);
862 cblk->numpasses += layer->numpasses;
864 length -= layer->len;
867 if (cstr_info && cstr_info->index_write) {
868 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
869 info_PK->disto += layer->disto;
870 if (cstr_info->D_max < info_PK->disto) {
871 cstr_info->D_max = info_PK->disto;
881 * p_data_written += (OPJ_UINT32)(c - dest);
886 static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2,
887 opj_tcd_tile_t *p_tile,
889 opj_pi_iterator_t *p_pi,
891 OPJ_UINT32 * p_data_read,
892 OPJ_UINT32 p_max_length,
893 opj_packet_info_t *p_pack_info,
894 opj_event_mgr_t *p_manager)
896 OPJ_BOOL l_read_data;
897 OPJ_UINT32 l_nb_bytes_read = 0;
898 OPJ_UINT32 l_nb_total_bytes_read = 0;
902 if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src,
903 &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) {
907 p_src += l_nb_bytes_read;
908 l_nb_total_bytes_read += l_nb_bytes_read;
909 p_max_length -= l_nb_bytes_read;
911 /* we should read data for the packet */
915 if (! opj_t2_skip_packet_data(p_t2, p_tile, p_pi, &l_nb_bytes_read,
916 p_max_length, p_pack_info, p_manager)) {
920 l_nb_total_bytes_read += l_nb_bytes_read;
922 *p_data_read = l_nb_total_bytes_read;
928 static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2,
929 opj_tcd_tile_t *p_tile,
931 opj_pi_iterator_t *p_pi,
932 OPJ_BOOL * p_is_data_present,
933 OPJ_BYTE *p_src_data,
934 OPJ_UINT32 * p_data_read,
935 OPJ_UINT32 p_max_length,
936 opj_packet_info_t *p_pack_info,
937 opj_event_mgr_t *p_manager)
941 OPJ_UINT32 bandno, cblkno;
942 OPJ_UINT32 l_nb_code_blocks;
943 OPJ_UINT32 l_remaining_length;
944 OPJ_UINT32 l_header_length;
945 OPJ_UINT32 * l_modified_length_ptr = 00;
946 OPJ_BYTE *l_current_data = p_src_data;
947 opj_cp_t *l_cp = p_t2->cp;
948 opj_bio_t *l_bio = 00; /* BIO component */
949 opj_tcd_band_t *l_band = 00;
950 opj_tcd_cblk_dec_t* l_cblk = 00;
951 opj_tcd_resolution_t* l_res =
952 &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
954 OPJ_BYTE *l_header_data = 00;
955 OPJ_BYTE **l_header_data_start = 00;
957 OPJ_UINT32 l_present;
959 if (p_pi->layno == 0) {
960 l_band = l_res->bands;
963 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
964 if (!opj_tcd_is_band_empty(l_band)) {
965 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
966 if (!(p_pi->precno < (l_band->precincts_data_size / sizeof(
967 opj_tcd_precinct_t)))) {
968 opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct\n");
973 opj_tgt_reset(l_prc->incltree);
974 opj_tgt_reset(l_prc->imsbtree);
975 l_cblk = l_prc->cblks.dec;
977 l_nb_code_blocks = l_prc->cw * l_prc->ch;
978 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
980 l_cblk->real_num_segs = 0;
991 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
992 if (p_max_length < 6) {
993 opj_event_msg(p_manager, EVT_WARNING,
994 "Not enough space for expected SOP marker\n");
995 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
996 opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n");
1001 /** TODO : check the Nsop value */
1005 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1006 This part deal with this caracteristic
1007 step 1: Read packet header in the saved structure
1008 step 2: Return to codestream for decoding
1011 l_bio = opj_bio_create();
1016 if (l_cp->ppm == 1) { /* PPM */
1017 l_header_data_start = &l_cp->ppm_data;
1018 l_header_data = *l_header_data_start;
1019 l_modified_length_ptr = &(l_cp->ppm_len);
1021 } else if (p_tcp->ppt == 1) { /* PPT */
1022 l_header_data_start = &(p_tcp->ppt_data);
1023 l_header_data = *l_header_data_start;
1024 l_modified_length_ptr = &(p_tcp->ppt_len);
1025 } else { /* Normal Case */
1026 l_header_data_start = &(l_current_data);
1027 l_header_data = *l_header_data_start;
1028 l_remaining_length = (OPJ_UINT32)(p_src_data + p_max_length - l_header_data);
1029 l_modified_length_ptr = &(l_remaining_length);
1032 opj_bio_init_dec(l_bio, l_header_data, *l_modified_length_ptr);
1034 l_present = opj_bio_read(l_bio, 1);
1035 JAS_FPRINTF(stderr, "present=%d \n", l_present);
1037 /* TODO MSD: no test to control the output of this function*/
1038 opj_bio_inalign(l_bio);
1039 l_header_data += opj_bio_numbytes(l_bio);
1040 opj_bio_destroy(l_bio);
1043 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1044 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
1045 *l_header_data_start)) < 2U) {
1046 opj_event_msg(p_manager, EVT_WARNING,
1047 "Not enough space for expected EPH marker\n");
1048 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1049 opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
1055 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1056 *l_modified_length_ptr -= l_header_length;
1057 *l_header_data_start += l_header_length;
1060 /* End of packet header position. Currently only represents the distance to start of packet
1061 Will be updated later by incrementing with packet start value */
1063 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1067 * p_is_data_present = OPJ_FALSE;
1068 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1072 l_band = l_res->bands;
1073 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band) {
1074 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
1076 if (opj_tcd_is_band_empty(l_band)) {
1080 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1081 l_cblk = l_prc->cblks.dec;
1082 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
1083 OPJ_UINT32 l_included, l_increment, l_segno;
1086 /* if cblk not yet included before --> inclusion tagtree */
1087 if (!l_cblk->numsegs) {
1088 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno,
1089 (OPJ_INT32)(p_pi->layno + 1));
1092 l_included = opj_bio_read(l_bio, 1);
1095 /* if cblk not included */
1097 l_cblk->numnewpasses = 0;
1099 JAS_FPRINTF(stderr, "included=%d \n", l_included);
1103 /* if cblk not yet included --> zero-bitplane tagtree */
1104 if (!l_cblk->numsegs) {
1107 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
1111 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
1112 l_cblk->numlenbits = 3;
1115 /* number of coding passes */
1116 l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
1117 l_increment = opj_t2_getcommacode(l_bio);
1119 /* length indicator increment */
1120 l_cblk->numlenbits += l_increment;
1123 if (!l_cblk->numsegs) {
1124 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1125 opj_bio_destroy(l_bio);
1129 l_segno = l_cblk->numsegs - 1;
1130 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1132 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1133 opj_bio_destroy(l_bio);
1138 n = (OPJ_INT32)l_cblk->numnewpasses;
1141 OPJ_UINT32 bit_number;
1142 l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(
1143 l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
1144 bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
1145 l_cblk->segs[l_segno].numnewpasses);
1146 if (bit_number > 32) {
1147 opj_event_msg(p_manager, EVT_ERROR,
1148 "Invalid bit number %d in opj_t2_read_packet_header()\n",
1150 opj_bio_destroy(l_bio);
1153 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
1154 JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
1155 l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
1156 l_cblk->segs[l_segno].newlen);
1158 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1162 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1163 opj_bio_destroy(l_bio);
1173 if (!opj_bio_inalign(l_bio)) {
1174 opj_bio_destroy(l_bio);
1178 l_header_data += opj_bio_numbytes(l_bio);
1179 opj_bio_destroy(l_bio);
1182 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1183 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
1184 *l_header_data_start)) < 2U) {
1185 opj_event_msg(p_manager, EVT_WARNING,
1186 "Not enough space for expected EPH marker\n");
1187 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1188 opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
1194 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1195 JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length);
1196 JAS_FPRINTF(stderr, "packet body\n");
1197 *l_modified_length_ptr -= l_header_length;
1198 *l_header_data_start += l_header_length;
1201 /* End of packet header position. Currently only represents the distance to start of packet
1202 Will be updated later by incrementing with packet start value */
1204 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1208 *p_is_data_present = OPJ_TRUE;
1209 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1214 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
1215 opj_tcd_tile_t *p_tile,
1216 opj_pi_iterator_t *p_pi,
1217 OPJ_BYTE *p_src_data,
1218 OPJ_UINT32 * p_data_read,
1219 OPJ_UINT32 p_max_length,
1220 opj_packet_info_t *pack_info,
1221 opj_event_mgr_t* p_manager)
1223 OPJ_UINT32 bandno, cblkno;
1224 OPJ_UINT32 l_nb_code_blocks;
1225 OPJ_BYTE *l_current_data = p_src_data;
1226 opj_tcd_band_t *l_band = 00;
1227 opj_tcd_cblk_dec_t* l_cblk = 00;
1228 opj_tcd_resolution_t* l_res =
1229 &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1231 OPJ_ARG_NOT_USED(p_t2);
1232 OPJ_ARG_NOT_USED(pack_info);
1234 l_band = l_res->bands;
1235 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1236 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1238 if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
1243 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1244 l_cblk = l_prc->cblks.dec;
1246 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1247 opj_tcd_seg_t *l_seg = 00;
1249 if (!l_cblk->numnewpasses) {
1255 if (!l_cblk->numsegs) {
1256 l_seg = l_cblk->segs;
1259 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1261 if (l_seg->numpasses == l_seg->maxpasses) {
1268 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
1269 if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) <
1270 (OPJ_SIZE_T)l_current_data) ||
1271 (l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
1272 opj_event_msg(p_manager, EVT_ERROR,
1273 "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1274 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
1280 /* we need here a j2k handle to verify if making a check to
1281 the validity of cblocks parameters is selected from user (-W) */
1283 /* let's check that we are not exceeding */
1284 if ((l_cblk->len + l_seg->newlen) > 8192) {
1285 opj_event_msg(p_manager, EVT_WARNING,
1286 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1287 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1289 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1292 l_seg->newlen = 8192 - l_cblk->len;
1293 opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
1298 #endif /* USE_JPWL */
1300 if (l_cblk->numchunks == l_cblk->numchunksalloc) {
1301 OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1;
1302 opj_tcd_seg_data_chunk_t* l_chunks =
1303 (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks,
1304 l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t));
1305 if (l_chunks == NULL) {
1306 opj_event_msg(p_manager, EVT_ERROR,
1307 "cannot allocate opj_tcd_seg_data_chunk_t* array");
1310 l_cblk->chunks = l_chunks;
1311 l_cblk->numchunksalloc = l_numchunksalloc;
1314 l_cblk->chunks[l_cblk->numchunks].data = l_current_data;
1315 l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen;
1316 l_cblk->numchunks ++;
1318 l_current_data += l_seg->newlen;
1319 l_seg->len += l_seg->newlen;
1320 l_seg->numpasses += l_seg->numnewpasses;
1321 l_cblk->numnewpasses -= l_seg->numnewpasses;
1323 l_seg->real_num_passes = l_seg->numpasses;
1325 if (l_cblk->numnewpasses > 0) {
1329 } while (l_cblk->numnewpasses > 0);
1331 l_cblk->real_num_segs = l_cblk->numsegs;
1333 } /* next code_block */
1338 *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1344 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
1345 opj_tcd_tile_t *p_tile,
1346 opj_pi_iterator_t *p_pi,
1347 OPJ_UINT32 * p_data_read,
1348 OPJ_UINT32 p_max_length,
1349 opj_packet_info_t *pack_info,
1350 opj_event_mgr_t *p_manager)
1352 OPJ_UINT32 bandno, cblkno;
1353 OPJ_UINT32 l_nb_code_blocks;
1354 opj_tcd_band_t *l_band = 00;
1355 opj_tcd_cblk_dec_t* l_cblk = 00;
1356 opj_tcd_resolution_t* l_res =
1357 &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1359 OPJ_ARG_NOT_USED(p_t2);
1360 OPJ_ARG_NOT_USED(pack_info);
1363 l_band = l_res->bands;
1365 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1366 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1368 if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
1373 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1374 l_cblk = l_prc->cblks.dec;
1376 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1377 opj_tcd_seg_t *l_seg = 00;
1379 if (!l_cblk->numnewpasses) {
1385 if (!l_cblk->numsegs) {
1386 l_seg = l_cblk->segs;
1389 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1391 if (l_seg->numpasses == l_seg->maxpasses) {
1398 /* Check possible overflow then size */
1399 if (((*p_data_read + l_seg->newlen) < (*p_data_read)) ||
1400 ((*p_data_read + l_seg->newlen) > p_max_length)) {
1401 opj_event_msg(p_manager, EVT_ERROR,
1402 "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1403 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
1409 /* we need here a j2k handle to verify if making a check to
1410 the validity of cblocks parameters is selected from user (-W) */
1412 /* let's check that we are not exceeding */
1413 if ((l_cblk->len + l_seg->newlen) > 8192) {
1414 opj_event_msg(p_manager, EVT_WARNING,
1415 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1416 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1418 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1421 l_seg->newlen = 8192 - l_cblk->len;
1422 opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
1427 #endif /* USE_JPWL */
1428 JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read,
1430 *(p_data_read) += l_seg->newlen;
1432 l_seg->numpasses += l_seg->numnewpasses;
1433 l_cblk->numnewpasses -= l_seg->numnewpasses;
1434 if (l_cblk->numnewpasses > 0) {
1438 } while (l_cblk->numnewpasses > 0);
1450 static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
1455 opj_tcd_seg_t* seg = 00;
1456 OPJ_UINT32 l_nb_segs = index + 1;
1458 if (l_nb_segs > cblk->m_current_max_segs) {
1459 opj_tcd_seg_t* new_segs;
1460 OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs +
1461 OPJ_J2K_DEFAULT_NB_SEGS;
1463 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs,
1464 l_m_current_max_segs * sizeof(opj_tcd_seg_t));
1466 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1469 cblk->segs = new_segs;
1470 memset(new_segs + cblk->m_current_max_segs,
1471 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));
1472 cblk->m_current_max_segs = l_m_current_max_segs;
1475 seg = &cblk->segs[index];
1476 opj_tcd_reinit_segment(seg);
1478 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1480 } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1482 seg->maxpasses = 10;
1484 seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
1485 ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1488 /* See paragraph "B.10.6 Number of coding passes" of the standard.
1489 * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37,
1490 * Mb being the maximum number of bit-planes available for the
1491 * representation of coefficients in the sub-band */
1492 seg->maxpasses = 109;