2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
61 * __END_OF_JASPER_LICENSE__
65 * $Id: jpc_enc.c,v 1.3 2005/06/08 17:39:37 malaterre Exp $
68 /******************************************************************************\
70 \******************************************************************************/
78 #include "jasper/jas_string.h"
79 #include "jasper/jas_malloc.h"
80 #include "jasper/jas_image.h"
81 #include "jasper/jas_fix.h"
82 #include "jasper/jas_tvp.h"
83 #include "jasper/jas_version.h"
84 #include "jasper/jas_math.h"
85 #include "jasper/jas_debug.h"
89 #include "jpc_tagtree.h"
95 #include "jpc_t1enc.h"
96 #include "jpc_t2enc.h"
101 /******************************************************************************\
103 \******************************************************************************/
105 #define JPC_POW2(n) \
108 #define JPC_FLOORTOMULTPOW2(x, n) \
109 (((n) > 0) ? ((x) & (~((1 << n) - 1))) : (x))
110 /* Round to the nearest multiple of the specified power of two in the
111 direction of negative infinity. */
113 #define JPC_CEILTOMULTPOW2(x, n) \
114 (((n) > 0) ? JPC_FLOORTOMULTPOW2(((x) + (1 << (n)) - 1), n) : (x))
115 /* Round to the nearest multiple of the specified power of two in the
116 direction of positive infinity. */
118 #define JPC_POW2(n) \
121 jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno);
122 void jpc_enc_tile_destroy(jpc_enc_tile_t *tile);
124 static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
125 jas_image_t *image, jpc_enc_tile_t *tile);
126 static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt);
127 static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
128 jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos);
129 static void rlvl_destroy(jpc_enc_rlvl_t *rlvl);
130 static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
131 jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos);
132 static void band_destroy(jpc_enc_band_t *bands);
133 static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp,
134 jpc_enc_band_t *band);
135 static void prc_destroy(jpc_enc_prc_t *prcs);
136 static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp,
138 static void cblk_destroy(jpc_enc_cblk_t *cblks);
139 int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size);
140 static void pass_destroy(jpc_enc_pass_t *pass);
141 void jpc_enc_dump(jpc_enc_t *enc);
143 /******************************************************************************\
145 \******************************************************************************/
147 int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk);
148 void calcrdslopes(jpc_enc_cblk_t *cblk);
149 void dump_layeringinfo(jpc_enc_t *enc);
150 /*static int jpc_calcssexp(jpc_fix_t stepsize);*/
151 /*static int jpc_calcssmant(jpc_fix_t stepsize);*/
152 void quantize(jas_matrix_t *data, jpc_fix_t stepsize);
153 static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
154 static int jpc_enc_encodemainbody(jpc_enc_t *enc);
155 int jpc_enc_encodetiledata(jpc_enc_t *enc);
156 jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image);
157 void jpc_enc_destroy(jpc_enc_t *enc);
158 static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
159 static int jpc_enc_encodemainbody(jpc_enc_t *enc);
160 int jpc_enc_encodetiledata(jpc_enc_t *enc);
161 int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens);
162 int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value);
163 static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image);
164 void jpc_enc_cp_destroy(jpc_enc_cp_t *cp);
165 static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn);
167 static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
178 p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS;
179 n = 11 - jpc_firstone(absdelta);
180 mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff;
181 expn = scaleexpn - p;
185 return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant);
218 jas_taginfo_t encopts[] = {
219 {OPT_DEBUG, "debug"},
220 {OPT_IMGAREAOFFX, "imgareatlx"},
221 {OPT_IMGAREAOFFY, "imgareatly"},
222 {OPT_TILEGRDOFFX, "tilegrdtlx"},
223 {OPT_TILEGRDOFFY, "tilegrdtly"},
224 {OPT_TILEWIDTH, "tilewidth"},
225 {OPT_TILEHEIGHT, "tileheight"},
226 {OPT_PRCWIDTH, "prcwidth"},
227 {OPT_PRCHEIGHT, "prcheight"},
228 {OPT_CBLKWIDTH, "cblkwidth"},
229 {OPT_CBLKHEIGHT, "cblkheight"},
232 {OPT_NOMCT, "nomct"},
233 {OPT_MAXRLVLS, "numrlvls"},
237 {OPT_TERMALL, "termall"},
238 {OPT_SEGSYM, "segsym"},
239 {OPT_VCAUSAL, "vcausal"},
240 {OPT_PTERM, "pterm"},
241 {OPT_RESET, "resetprob"},
242 {OPT_NUMGBITS, "numgbits"},
244 {OPT_ILYRRATES, "ilyrrates"},
245 {OPT_JP2OVERHEAD, "_jp2overhead"},
255 jas_taginfo_t prgordtab[] = {
256 {JPC_COD_LRCPPRG, "lrcp"},
257 {JPC_COD_RLCPPRG, "rlcp"},
258 {JPC_COD_RPCLPRG, "rpcl"},
259 {JPC_COD_PCRLPRG, "pcrl"},
260 {JPC_COD_CPRLPRG, "cprl"},
269 jas_taginfo_t modetab[] = {
275 /******************************************************************************\
276 * The main encoder entry point.
277 \******************************************************************************/
279 int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
289 if (!(cp = cp_create(optstr, image))) {
290 fprintf(stderr, "invalid JP encoder options\n");
294 if (!(enc = jpc_enc_create(cp, out, image))) {
299 /* Encode the main header. */
300 if (jpc_enc_encodemainhdr(enc)) {
304 /* Encode the main body. This constitutes most of the encoding work. */
305 if (jpc_enc_encodemainbody(enc)) {
309 /* Write EOC marker segment. */
310 if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) {
313 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
314 fprintf(stderr, "cannot write EOI marker\n");
317 jpc_ms_destroy(enc->mrk);
320 if (jas_stream_flush(enc->out)) {
324 jpc_enc_destroy(enc);
330 jpc_enc_cp_destroy(cp);
333 jpc_enc_destroy(enc);
338 /******************************************************************************\
339 * Option parsing code.
340 \******************************************************************************/
342 static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
352 jpc_enc_tccp_t *tccp;
355 uint_fast16_t rlvlno;
356 uint_fast16_t prcwidthexpn;
357 uint_fast16_t prcheightexpn;
359 uint_fast32_t jp2overhead;
361 uint_fast32_t hsteplcm;
362 uint_fast32_t vsteplcm;
370 if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) {
381 cp->imgareatlx = UINT_FAST32_MAX;
382 cp->imgareatly = UINT_FAST32_MAX;
384 cp->refgrdheight = 0;
385 cp->tilegrdoffx = UINT_FAST32_MAX;
386 cp->tilegrdoffy = UINT_FAST32_MAX;
389 cp->numcmpts = jas_image_numcmpts(image);
393 for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
394 if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <=
395 jas_image_brx(image) || jas_image_cmptbry(image, cmptno) +
396 jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) {
397 fprintf(stderr, "unsupported image type\n");
400 /* Note: We ought to be calculating the LCMs here. Fix some day. */
401 hsteplcm *= jas_image_cmpthstep(image, cmptno);
402 vsteplcm *= jas_image_cmptvstep(image, cmptno);
405 if (!(cp->ccps = jas_malloc(cp->numcmpts * sizeof(jpc_enc_ccp_t)))) {
408 for (cmptno = 0, ccp = cp->ccps; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno,
410 ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno);
411 ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno);
412 /* XXX - this isn't quite correct for more general image */
413 ccp->sampgrdsubstepx = 0;
414 ccp->sampgrdsubstepx = 0;
415 ccp->prec = jas_image_cmptprec(image, cmptno);
416 ccp->sgnd = jas_image_cmptsgnd(image, cmptno);
417 ccp->numstepsizes = 0;
418 memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes));
421 cp->rawsize = jas_image_rawsize(image);
422 cp->totalsize = UINT_FAST32_MAX;
427 tcp->prg = JPC_COD_LRCPPRG;
434 tccp->cblkwidthexpn = 6;
435 tccp->cblkheightexpn = 6;
439 if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) {
443 while (!(ret = jas_tvparser_next(tvp))) {
444 switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts,
445 jas_tvparser_gettag(tvp)))->id) {
447 cp->debug = atoi(jas_tvparser_getval(tvp));
449 case OPT_IMGAREAOFFX:
450 cp->imgareatlx = atoi(jas_tvparser_getval(tvp));
452 case OPT_IMGAREAOFFY:
453 cp->imgareatly = atoi(jas_tvparser_getval(tvp));
455 case OPT_TILEGRDOFFX:
456 cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp));
458 case OPT_TILEGRDOFFY:
459 cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp));
462 cp->tilewidth = atoi(jas_tvparser_getval(tvp));
465 cp->tileheight = atoi(jas_tvparser_getval(tvp));
468 prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
471 prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
474 tccp->cblkwidthexpn =
475 jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
478 tccp->cblkheightexpn =
479 jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
482 if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab,
483 jas_tvparser_getval(tvp)))->id) < 0) {
485 "ignoring invalid mode %s\n",
486 jas_tvparser_getval(tvp));
488 tcp->intmode = (tagid == MODE_INT);
492 if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab,
493 jas_tvparser_getval(tvp)))->id) < 0) {
495 "ignoring invalid progression order %s\n",
496 jas_tvparser_getval(tvp));
505 tccp->maxrlvls = atoi(jas_tvparser_getval(tvp));
508 cp->tcp.csty |= JPC_COD_SOP;
511 cp->tcp.csty |= JPC_COD_EPH;
514 tccp->cblksty |= JPC_COX_LAZY;
517 tccp->cblksty |= JPC_COX_TERMALL;
520 tccp->cblksty |= JPC_COX_SEGSYM;
523 tccp->cblksty |= JPC_COX_VSC;
526 tccp->cblksty |= JPC_COX_RESET;
529 tccp->cblksty |= JPC_COX_PTERM;
532 cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp));
535 if (ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize,
538 "ignoring bad rate specifier %s\n",
539 jas_tvparser_getval(tvp));
543 if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates,
546 "warning: invalid intermediate layer rates specifier ignored (%s)\n",
547 jas_tvparser_getval(tvp));
551 case OPT_JP2OVERHEAD:
552 jp2overhead = atoi(jas_tvparser_getval(tvp));
555 fprintf(stderr, "warning: ignoring invalid option %s\n",
556 jas_tvparser_gettag(tvp));
561 jas_tvparser_destroy(tvp);
564 if (cp->totalsize != UINT_FAST32_MAX) {
565 cp->totalsize = (cp->totalsize > jp2overhead) ?
566 (cp->totalsize - jp2overhead) : 0;
569 if (cp->imgareatlx == UINT_FAST32_MAX) {
573 fprintf(stderr, "warning: overriding imgareatlx value\n");
575 cp->imgareatlx *= hsteplcm;
577 if (cp->imgareatly == UINT_FAST32_MAX) {
581 fprintf(stderr, "warning: overriding imgareatly value\n");
583 cp->imgareatly *= vsteplcm;
585 cp->refgrdwidth = cp->imgareatlx + jas_image_width(image);
586 cp->refgrdheight = cp->imgareatly + jas_image_height(image);
587 if (cp->tilegrdoffx == UINT_FAST32_MAX) {
588 cp->tilegrdoffx = cp->imgareatlx;
590 if (cp->tilegrdoffy == UINT_FAST32_MAX) {
591 cp->tilegrdoffy = cp->imgareatly;
593 if (!cp->tilewidth) {
594 cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx;
596 if (!cp->tileheight) {
597 cp->tileheight = cp->refgrdheight - cp->tilegrdoffy;
600 if (cp->numcmpts == 3) {
602 for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
603 if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) ||
604 jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) ||
605 jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) ||
606 jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) {
613 if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) != JAS_CLRSPC_FAM_RGB) {
614 fprintf(stderr, "warning: color space apparently not RGB\n");
616 if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) == JAS_CLRSPC_FAM_RGB) {
617 tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT);
619 tcp->mctid = JPC_MCT_NONE;
621 tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS);
623 for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
624 tccp->prcwidthexpns[rlvlno] = prcwidthexpn;
625 tccp->prcheightexpns[rlvlno] = prcheightexpn;
627 if (prcwidthexpn != 15 || prcheightexpn != 15) {
628 tccp->csty |= JPC_COX_PRT;
631 /* Ensure that the tile width and height is valid. */
632 if (!cp->tilewidth) {
633 fprintf(stderr, "invalid tile width %lu\n", (unsigned long)
637 if (!cp->tileheight) {
638 fprintf(stderr, "invalid tile height %lu\n", (unsigned long)
643 /* Ensure that the tile grid offset is valid. */
644 if (cp->tilegrdoffx > cp->imgareatlx ||
645 cp->tilegrdoffy > cp->imgareatly ||
646 cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx ||
647 cp->tilegrdoffy + cp->tileheight < cp->imgareatly) {
648 fprintf(stderr, "invalid tile grid offset (%lu, %lu)\n",
649 (unsigned long) cp->tilegrdoffx, (unsigned long)
654 cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx,
656 cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy,
658 cp->numtiles = cp->numhtiles * cp->numvtiles;
660 if (ilyrrates && numilyrrates > 0) {
661 tcp->numlyrs = numilyrrates + 1;
662 if (!(tcp->ilyrrates = jas_malloc((tcp->numlyrs - 1) *
663 sizeof(jpc_fix_t)))) {
666 for (i = 0; i < JAS_CAST(int, tcp->numlyrs - 1); ++i) {
667 tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]);
671 /* Ensure that the integer mode is used in the case of lossless
673 if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) {
674 fprintf(stderr, "cannot use real mode for lossless coding\n");
678 /* Ensure that the precinct width is valid. */
679 if (prcwidthexpn > 15) {
680 fprintf(stderr, "invalid precinct width\n");
684 /* Ensure that the precinct height is valid. */
685 if (prcheightexpn > 15) {
686 fprintf(stderr, "invalid precinct height\n");
690 /* Ensure that the code block width is valid. */
691 if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) {
692 fprintf(stderr, "invalid code block width %d\n",
693 JPC_POW2(cp->tccp.cblkwidthexpn));
697 /* Ensure that the code block height is valid. */
698 if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) {
699 fprintf(stderr, "invalid code block height %d\n",
700 JPC_POW2(cp->tccp.cblkheightexpn));
704 /* Ensure that the code block size is not too large. */
705 if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) {
706 fprintf(stderr, "code block size too large\n");
710 /* Ensure that the number of layers is valid. */
711 if (cp->tcp.numlyrs > 16384) {
712 fprintf(stderr, "too many layers\n");
716 /* There must be at least one resolution level. */
717 if (cp->tccp.maxrlvls < 1) {
718 fprintf(stderr, "must be at least one resolution level\n");
722 /* Ensure that the number of guard bits is valid. */
723 if (cp->tccp.numgbits > 8) {
724 fprintf(stderr, "invalid number of guard bits\n");
728 /* Ensure that the rate is within the legal range. */
729 if (cp->totalsize != UINT_FAST32_MAX && cp->totalsize > cp->rawsize) {
730 fprintf(stderr, "warning: specified rate is unreasonably large (%lu > %lu)\n", (unsigned long) cp->totalsize, (unsigned long) cp->rawsize);
733 /* Ensure that the intermediate layer rates are valid. */
734 if (tcp->numlyrs > 1) {
735 /* The intermediate layers rates must increase monotonically. */
736 for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) {
737 if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) {
738 fprintf(stderr, "intermediate layer rates must increase monotonically\n");
742 /* The intermediate layer rates must be less than the overall rate. */
743 if (cp->totalsize != UINT_FAST32_MAX) {
744 for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) {
745 if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize)
747 fprintf(stderr, "warning: intermediate layer rates must be less than overall rate\n");
766 jas_tvparser_destroy(tvp);
769 jpc_enc_cp_destroy(cp);
774 void jpc_enc_cp_destroy(jpc_enc_cp_t *cp)
777 if (cp->tcp.ilyrrates) {
778 jas_free(cp->tcp.ilyrrates);
785 int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size)
790 /* Note: This function must not modify output size on failure. */
791 if ((cp = strchr(s, 'B'))) {
797 } else if (f > 1.0) {
806 /******************************************************************************\
807 * Encoder constructor and destructor.
808 \******************************************************************************/
810 jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image)
816 if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) {
828 if (!(enc->cstate = jpc_cstate_create())) {
832 enc->mainbodysize = 0;
839 jpc_enc_destroy(enc);
844 void jpc_enc_destroy(jpc_enc_t *enc)
846 /* The image object (i.e., enc->image) and output stream object
847 (i.e., enc->out) are created outside of the encoder.
848 Therefore, they must not be destroyed here. */
851 jpc_enc_tile_destroy(enc->curtile);
854 jpc_enc_cp_destroy(enc->cp);
857 jpc_cstate_destroy(enc->cstate);
859 if (enc->tmpstream) {
860 jas_stream_close(enc->tmpstream);
866 /******************************************************************************\
868 \******************************************************************************/
871 static int jpc_calcssmant(jpc_fix_t stepsize)
877 n = jpc_firstone(stepsize);
878 e = n - JPC_FIX_FRACBITS;
880 m = (stepsize >> (n - 11)) & 0x7ff;
882 m = (stepsize & ((1 << n) - 1)) << (11 - n);
887 static int jpc_calcssexp(jpc_fix_t stepsize)
889 return jpc_firstone(stepsize) - JPC_FIX_FRACBITS;
893 static int jpc_enc_encodemainhdr(jpc_enc_t *enc)
903 jpc_enc_tccp_t *tccp;
904 uint_fast16_t cmptno;
905 jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
906 jpc_fix_t mctsynweight;
909 jpc_tsfb_band_t *bandinfo;
910 uint_fast16_t numbands;
911 uint_fast16_t bandno;
912 uint_fast16_t rlvlno;
913 uint_fast16_t analgain;
914 jpc_fix_t absstepsize;
920 startoff = jas_stream_getrwcount(enc->out);
922 /* Write SOC marker segment. */
923 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) {
926 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
927 fprintf(stderr, "cannot write SOC marker\n");
930 jpc_ms_destroy(enc->mrk);
933 /* Write SIZ marker segment. */
934 if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) {
937 siz = &enc->mrk->parms.siz;
939 siz->xoff = cp->imgareatlx;
940 siz->yoff = cp->imgareatly;
941 siz->width = cp->refgrdwidth;
942 siz->height = cp->refgrdheight;
943 siz->tilexoff = cp->tilegrdoffx;
944 siz->tileyoff = cp->tilegrdoffy;
945 siz->tilewidth = cp->tilewidth;
946 siz->tileheight = cp->tileheight;
947 siz->numcomps = cp->numcmpts;
948 siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t));
950 for (i = 0; i < JAS_CAST(int, cp->numcmpts); ++i) {
951 siz->comps[i].prec = cp->ccps[i].prec;
952 siz->comps[i].sgnd = cp->ccps[i].sgnd;
953 siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx;
954 siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy;
956 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
957 fprintf(stderr, "cannot write SIZ marker\n");
960 jpc_ms_destroy(enc->mrk);
963 if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) {
966 sprintf(buf, "Creator: JasPer Version %s", jas_getversion());
967 com = &enc->mrk->parms.com;
968 com->len = strlen(buf);
969 com->regid = JPC_COM_LATIN;
970 if (!(com->data = JAS_CAST(uchar *, jas_strdup(buf)))) {
973 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
974 fprintf(stderr, "cannot write COM marker\n");
977 jpc_ms_destroy(enc->mrk);
981 if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) {
984 crg = &enc->mrk->parms.crg;
985 crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t));
986 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
987 fprintf(stderr, "cannot write CRG marker\n");
990 jpc_ms_destroy(enc->mrk);
996 for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) {
997 tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1);
998 jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls,
1000 jpc_tsfb_destroy(tsfb);
1001 mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno);
1002 numbands = 3 * tccp->maxrlvls - 2;
1003 for (bandno = 0, bandinfo = bandinfos; bandno < numbands;
1004 ++bandno, ++bandinfo) {
1005 rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0;
1006 analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls,
1007 rlvlno, bandinfo->orient);
1008 if (!tcp->intmode) {
1009 absstepsize = jpc_fix_div(jpc_inttofix(1 <<
1010 (analgain + 1)), bandinfo->synenergywt);
1012 absstepsize = jpc_inttofix(1);
1014 cp->ccps[cmptno].stepsizes[bandno] =
1015 jpc_abstorelstepsize(absstepsize,
1016 cp->ccps[cmptno].prec + analgain);
1018 cp->ccps[cmptno].numstepsizes = numbands;
1021 if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
1024 cod = &enc->mrk->parms.cod;
1025 cod->csty = cp->tccp.csty | cp->tcp.csty;
1026 cod->compparms.csty = cp->tccp.csty | cp->tcp.csty;
1027 cod->compparms.numdlvls = cp->tccp.maxrlvls - 1;
1028 cod->compparms.numrlvls = cp->tccp.maxrlvls;
1029 cod->prg = cp->tcp.prg;
1030 cod->numlyrs = cp->tcp.numlyrs;
1031 cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn);
1032 cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn);
1033 cod->compparms.cblksty = cp->tccp.cblksty;
1034 cod->compparms.qmfbid = cp->tccp.qmfbid;
1035 cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE);
1036 if (tccp->csty & JPC_COX_PRT) {
1037 for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
1038 cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno];
1039 cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno];
1042 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1043 fprintf(stderr, "cannot write COD marker\n");
1046 jpc_ms_destroy(enc->mrk);
1049 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) {
1052 qcd = &enc->mrk->parms.qcd;
1053 qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
1054 JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1055 qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes;
1056 qcd->compparms.numguard = cp->tccp.numgbits;
1057 qcd->compparms.stepsizes = cp->ccps[0].stepsizes;
1058 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1061 /* We do not want the step size array to be freed! */
1062 qcd->compparms.stepsizes = 0;
1063 jpc_ms_destroy(enc->mrk);
1067 for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) {
1068 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
1071 qcc = &enc->mrk->parms.qcc;
1072 qcc->compno = cmptno;
1073 qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
1074 JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1075 qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes;
1076 qcc->compparms.numguard = cp->tccp.numgbits;
1077 qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes;
1078 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1081 /* We do not want the step size array to be freed! */
1082 qcc->compparms.stepsizes = 0;
1083 jpc_ms_destroy(enc->mrk);
1087 #define MAINTLRLEN 2
1088 mainhdrlen = jas_stream_getrwcount(enc->out) - startoff;
1089 enc->len += mainhdrlen;
1090 if (enc->cp->totalsize != UINT_FAST32_MAX) {
1091 uint_fast32_t overhead;
1092 overhead = mainhdrlen + MAINTLRLEN;
1093 enc->mainbodysize = (enc->cp->totalsize >= overhead) ?
1094 (enc->cp->totalsize - overhead) : 0;
1096 enc->mainbodysize = UINT_FAST32_MAX;
1102 static int jpc_enc_encodemainbody(jpc_enc_t *enc)
1109 jpc_enc_tcmpt_t *comp;
1110 jpc_enc_tcmpt_t *endcomps;
1111 jpc_enc_band_t *band;
1112 jpc_enc_band_t *endbands;
1113 jpc_enc_rlvl_t *lvl;
1123 jpc_enc_tile_t *tile;
1129 jpc_enc_ccp_t *ccps;
1130 jpc_enc_tccp_t *tccp;
1142 /* Avoid compile warnings. */
1145 for (tileno = 0; tileno < JAS_CAST(int, cp->numtiles); ++tileno) {
1146 tilex = tileno % cp->numhtiles;
1147 tiley = tileno / cp->numhtiles;
1149 if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) {
1153 tile = enc->curtile;
1155 if (jas_getdbglevel() >= 10) {
1159 endcomps = &tile->tcmpts[tile->numtcmpts];
1160 for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) {
1161 if (!cp->ccps[cmptno].sgnd) {
1162 adjust = 1 << (cp->ccps[cmptno].prec - 1);
1163 for (i = 0; i < jas_matrix_numrows(comp->data); ++i) {
1164 for (j = 0; j < jas_matrix_numcols(comp->data); ++j) {
1165 *jas_matrix_getref(comp->data, i, j) -= adjust;
1171 if (!tile->intmode) {
1172 endcomps = &tile->tcmpts[tile->numtcmpts];
1173 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1174 jas_matrix_asl(comp->data, JPC_FIX_FRACBITS);
1178 switch (tile->mctid) {
1180 assert(jas_image_numcmpts(enc->image) == 3);
1181 jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data,
1182 tile->tcmpts[2].data);
1185 assert(jas_image_numcmpts(enc->image) == 3);
1186 jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data,
1187 tile->tcmpts[2].data);
1193 for (i = 0; i < jas_image_numcmpts(enc->image); ++i) {
1194 comp = &tile->tcmpts[i];
1195 jpc_tsfb_analyze(comp->tsfb, ((comp->qmfbid == JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), comp->data);
1200 endcomps = &tile->tcmpts[tile->numtcmpts];
1201 for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) {
1204 /* All bands must have a corresponding quantizer step size,
1205 even if they contain no samples and are never coded. */
1206 /* Some bands may not be hit by the loop below, so we must
1207 initialize all of the step sizes to a sane value. */
1208 memset(comp->stepsizes, 0, sizeof(comp->stepsizes));
1209 for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) {
1211 absbandno += rlvlno ? 3 : 1;
1214 endbands = &lvl->bands[lvl->numbands];
1215 for (band = lvl->bands; band != endbands; ++band) {
1222 for (y = 0; y < JAS_CAST(uint_fast32_t, jas_matrix_numrows(band->data)); ++y) {
1223 for (x = 0; x < JAS_CAST(uint_fast32_t, jas_matrix_numcols(band->data)); ++x) {
1224 mag = abs(jas_matrix_get(band->data, y, x));
1230 if (tile->intmode) {
1231 actualnumbps = jpc_firstone(mxmag) + 1;
1233 actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS;
1235 numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 +
1238 fprintf(stderr, "%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band->analgain, mxmag, actualnumbps, numgbits);
1240 if (numgbits > mingbits) {
1241 mingbits = numgbits;
1243 if (!tile->intmode) {
1244 band->absstepsize = jpc_fix_div(jpc_inttofix(1
1245 << (band->analgain + 1)),
1248 band->absstepsize = jpc_inttofix(1);
1250 band->stepsize = jpc_abstorelstepsize(
1251 band->absstepsize, cp->ccps[cmptno].prec +
1253 band->numbps = cp->tccp.numgbits +
1254 JPC_QCX_GETEXPN(band->stepsize) - 1;
1256 if ((!tile->intmode) && band->data) {
1257 quantize(band->data, band->absstepsize);
1260 comp->stepsizes[absbandno] = band->stepsize;
1265 assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS);
1266 if (!tile->intmode) {
1267 jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS);
1269 jas_matrix_asl(comp->data, JPC_NUMEXTRABITS);
1273 fprintf(stderr, "mingbits %d\n", mingbits);
1275 if (mingbits > cp->tccp.numgbits) {
1276 fprintf(stderr, "error: too few guard bits (need at least %d)\n",
1282 if (!(enc->tmpstream = jas_stream_memopen(0, 0))) {
1283 fprintf(stderr, "cannot open tmp file\n");
1287 /* Write the tile header. */
1288 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) {
1291 sot = &enc->mrk->parms.sot;
1293 sot->tileno = tileno;
1296 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1297 fprintf(stderr, "cannot write SOT marker\n");
1300 jpc_ms_destroy(enc->mrk);
1303 /************************************************************************/
1304 /************************************************************************/
1305 /************************************************************************/
1308 for (cmptno = 0; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno) {
1309 comp = &tile->tcmpts[cmptno];
1310 if (comp->numrlvls != tccp->maxrlvls) {
1311 if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
1314 /* XXX = this is not really correct. we are using comp #0's precint sizes
1315 and other characteristics */
1316 comp = &tile->tcmpts[0];
1317 cod = &enc->mrk->parms.cod;
1318 cod->compparms.csty = 0;
1319 cod->compparms.numdlvls = comp->numrlvls - 1;
1320 cod->prg = tile->prg;
1321 cod->numlyrs = tile->numlyrs;
1322 cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn);
1323 cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn);
1324 cod->compparms.cblksty = comp->cblksty;
1325 cod->compparms.qmfbid = comp->qmfbid;
1326 cod->mctrans = (tile->mctid != JPC_MCT_NONE);
1327 for (i = 0; i < comp->numrlvls; ++i) {
1328 cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn;
1329 cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn;
1331 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1334 jpc_ms_destroy(enc->mrk);
1339 for (cmptno = 0, comp = tile->tcmpts; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno, ++comp) {
1340 ccps = &cp->ccps[cmptno];
1341 if (JAS_CAST(int, ccps->numstepsizes) == comp->numstepsizes) {
1343 for (bandno = 0; bandno < JAS_CAST(int, ccps->numstepsizes); ++bandno) {
1344 if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) {
1352 if (!samestepsizes) {
1353 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
1356 qcc = &enc->mrk->parms.qcc;
1357 qcc->compno = cmptno;
1358 qcc->compparms.numguard = cp->tccp.numgbits;
1359 qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ?
1360 JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1361 qcc->compparms.numstepsizes = comp->numstepsizes;
1362 qcc->compparms.stepsizes = comp->stepsizes;
1363 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1366 qcc->compparms.stepsizes = 0;
1367 jpc_ms_destroy(enc->mrk);
1372 /* Write a SOD marker to indicate the end of the tile header. */
1373 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) {
1376 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1377 fprintf(stderr, "cannot write SOD marker\n");
1380 jpc_ms_destroy(enc->mrk);
1382 tilehdrlen = jas_stream_getrwcount(enc->tmpstream);
1384 /************************************************************************/
1385 /************************************************************************/
1386 /************************************************************************/
1388 if (jpc_enc_enccblks(enc)) {
1394 rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) /
1395 ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight -
1397 tile->rawsize = cp->rawsize * rho;
1399 for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) {
1400 tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl(
1401 cp->tcp.ilyrrates[lyrno]);
1403 tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ?
1404 (rho * enc->mainbodysize) : UINT_FAST32_MAX;
1405 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1406 if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) {
1407 if (tilehdrlen <= JAS_CAST(long, tile->lyrsizes[lyrno])) {
1408 tile->lyrsizes[lyrno] -= tilehdrlen;
1410 tile->lyrsizes[lyrno] = 0;
1415 if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) {
1420 fprintf(stderr, "ENCODE TILE DATA\n");
1422 if (jpc_enc_encodetiledata(enc)) {
1423 fprintf(stderr, "dotile failed\n");
1427 /************************************************************************/
1428 /************************************************************************/
1429 /************************************************************************/
1431 /************************************************************************/
1432 /************************************************************************/
1433 /************************************************************************/
1435 tilelen = jas_stream_tell(enc->tmpstream);
1437 if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) {
1440 jpc_putuint32(enc->tmpstream, tilelen);
1442 if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) {
1445 if (jpc_putdata(enc->out, enc->tmpstream, -1)) {
1448 enc->len += tilelen;
1450 jas_stream_close(enc->tmpstream);
1453 jpc_enc_tile_destroy(enc->curtile);
1461 int jpc_enc_encodetiledata(jpc_enc_t *enc)
1463 assert(enc->tmpstream);
1464 if (jpc_enc_encpkts(enc, enc->tmpstream)) {
1470 int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk)
1472 jpc_enc_pass_t *pass;
1474 jas_stream_memobj_t *smo;
1476 smo = cblk->stream->obj_;
1479 for (i = 0; i < numpasses; ++i) {
1480 fprintf(stderr, "start=%d end=%d type=%d term=%d lyrno=%d firstchar=%02x size=%ld pos=%ld\n",
1481 (int)pass->start, (int)pass->end, (int)pass->type, (int)pass->term, (int)pass->lyrno,
1482 smo->buf_[pass->start], (long)smo->len_, (long)smo->pos_);
1484 jas_memdump(stderr, &smo->buf_[pass->start], pass->end - pass->start);
1491 void quantize(jas_matrix_t *data, jpc_fix_t stepsize)
1497 if (stepsize == jpc_inttofix(1)) {
1501 for (i = 0; i < jas_matrix_numrows(data); ++i) {
1502 for (j = 0; j < jas_matrix_numcols(data); ++j) {
1503 t = jas_matrix_get(data, i, j);
1507 t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize));
1509 t = jpc_fix_div(t, stepsize);
1513 jas_matrix_set(data, i, j, t);
1518 void calcrdslopes(jpc_enc_cblk_t *cblk)
1520 jpc_enc_pass_t *endpasses;
1521 jpc_enc_pass_t *pass0;
1522 jpc_enc_pass_t *pass1;
1523 jpc_enc_pass_t *pass2;
1529 endpasses = &cblk->passes[cblk->numpasses];
1530 pass2 = cblk->passes;
1532 while (pass2 != endpasses) {
1534 for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) {
1535 dd = pass1->cumwmsedec;
1538 dd -= pass0->cumwmsedec;
1542 pass1->rdslope = JPC_BADRDSLOPE;
1543 if (pass1 >= pass2) {
1548 if (pass1 < pass2 && pass1->rdslope <= 0) {
1557 if (pass0 && slope >= slope0) {
1561 pass1->rdslope = slope;
1562 if (pass1 >= pass2) {
1571 for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) {
1572 if (pass0->rdslope > 0.0) {
1573 fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes,
1574 fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope);
1580 void dump_layeringinfo(jpc_enc_t *enc)
1583 jpc_enc_tcmpt_t *tcmpt;
1585 jpc_enc_rlvl_t *rlvl;
1587 jpc_enc_band_t *band;
1591 jpc_enc_cblk_t *cblk;
1593 jpc_enc_pass_t *pass;
1596 jpc_enc_tile_t *tile;
1598 tile = enc->curtile;
1600 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1601 fprintf(stderr, "lyrno = %02d\n", lyrno);
1602 for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts;
1603 ++tcmptno, ++tcmpt) {
1604 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
1609 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
1614 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
1619 for (cblkno = 0, cblk = prc->cblks; cblkno <
1620 prc->numcblks; ++cblkno, ++cblk) {
1621 for (passno = 0, pass = cblk->passes; passno <
1622 cblk->numpasses && pass->lyrno == lyrno;
1624 fprintf(stderr, "lyrno=%02d cmptno=%02d rlvlno=%02d bandno=%02d prcno=%02d cblkno=%03d passno=%03d\n", lyrno, tcmptno, rlvlno, bandno, prcno, cblkno, passno);
1634 int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens)
1639 unsigned long cumlen;
1642 jpc_flt_t goodthresh;
1648 jpc_enc_tcmpt_t *comp;
1649 jpc_enc_tcmpt_t *endcomps;
1650 jpc_enc_rlvl_t *lvl;
1651 jpc_enc_rlvl_t *endlvls;
1652 jpc_enc_band_t *band;
1653 jpc_enc_band_t *endbands;
1654 jpc_enc_cblk_t *cblk;
1655 jpc_enc_cblk_t *endcblks;
1656 jpc_enc_pass_t *pass;
1657 jpc_enc_pass_t *endpasses;
1658 jpc_enc_pass_t *pass1;
1659 jpc_flt_t mxrdslope;
1660 jpc_flt_t mnrdslope;
1661 jpc_enc_tile_t *tile;
1665 tile = enc->curtile;
1667 for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) {
1668 if (cumlens[lyrno - 1] > cumlens[lyrno]) {
1673 if (!(out = jas_stream_memopen(0, 0))) {
1678 /* Find minimum and maximum R-D slope values. */
1679 mnrdslope = DBL_MAX;
1681 endcomps = &tile->tcmpts[tile->numtcmpts];
1682 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1683 endlvls = &comp->rlvls[comp->numrlvls];
1684 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1688 endbands = &lvl->bands[lvl->numbands];
1689 for (band = lvl->bands; band != endbands; ++band) {
1693 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1697 endcblks = &prc->cblks[prc->numcblks];
1698 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1700 endpasses = &cblk->passes[cblk->numpasses];
1701 for (pass = cblk->passes; pass != endpasses; ++pass) {
1702 if (pass->rdslope > 0) {
1703 if (pass->rdslope < mnrdslope) {
1704 mnrdslope = pass->rdslope;
1706 if (pass->rdslope > mxrdslope) {
1707 mxrdslope = pass->rdslope;
1716 if (jas_getdbglevel()) {
1717 fprintf(stderr, "min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope);
1720 jpc_init_t2state(enc, 1);
1722 for (lyrno = 0; lyrno < numlyrs; ++lyrno) {
1733 cumlen = cumlens[lyrno];
1734 if (cumlen == UINT_FAST32_MAX) {
1735 /* Only the last layer can be free of a rate
1736 constraint (e.g., for lossless coding). */
1737 assert(lyrno == numlyrs - 1);
1743 thresh = (lo + hi) / 2;
1745 /* Save the tier 2 coding state. */
1746 jpc_save_t2state(enc);
1747 oldpos = jas_stream_tell(out);
1748 assert(oldpos >= 0);
1750 /* Assign all passes with R-D slopes greater than or
1751 equal to the current threshold to this layer. */
1752 endcomps = &tile->tcmpts[tile->numtcmpts];
1753 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1754 endlvls = &comp->rlvls[comp->numrlvls];
1755 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1759 endbands = &lvl->bands[lvl->numbands];
1760 for (band = lvl->bands; band != endbands; ++band) {
1764 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1768 endcblks = &prc->cblks[prc->numcblks];
1769 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1770 if (cblk->curpass) {
1771 endpasses = &cblk->passes[cblk->numpasses];
1772 pass1 = cblk->curpass;
1773 for (pass = cblk->curpass; pass != endpasses; ++pass) {
1774 if (pass->rdslope >= thresh) {
1778 for (pass = cblk->curpass; pass != pass1; ++pass) {
1779 pass->lyrno = lyrno;
1781 for (; pass != endpasses; ++pass) {
1791 /* Perform tier 2 coding. */
1792 endcomps = &tile->tcmpts[tile->numtcmpts];
1793 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1794 endlvls = &comp->rlvls[comp->numrlvls];
1795 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1799 for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
1800 if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
1807 pos = jas_stream_tell(out);
1809 /* Check the rate constraint. */
1811 if (pos > (long)cumlen) {
1812 /* The rate is too high. */
1814 } else if (pos <= (long)cumlen) {
1815 /* The rate is low enough, so try higher. */
1817 if (!success || thresh < goodthresh) {
1818 goodthresh = thresh;
1823 /* Save the tier 2 coding state. */
1824 jpc_restore_t2state(enc);
1825 if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) {
1829 if (jas_getdbglevel()) {
1830 fprintf(stderr, "maxlen=%08ld actuallen=%08ld thresh=%f\n", cumlen, pos, thresh);
1834 } while (lo < hi - 1e-3 && numiters < 32);
1837 fprintf(stderr, "warning: empty layer generated\n");
1840 if (jas_getdbglevel()) {
1841 fprintf(stderr, "success %d goodthresh %f\n", success, goodthresh);
1844 /* Assign all passes with R-D slopes greater than or
1845 equal to the selected threshold to this layer. */
1846 endcomps = &tile->tcmpts[tile->numtcmpts];
1847 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1848 endlvls = &comp->rlvls[comp->numrlvls];
1849 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1853 endbands = &lvl->bands[lvl->numbands];
1854 for (band = lvl->bands; band != endbands; ++band) {
1858 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1862 endcblks = &prc->cblks[prc->numcblks];
1863 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1864 if (cblk->curpass) {
1865 endpasses = &cblk->passes[cblk->numpasses];
1866 pass1 = cblk->curpass;
1868 for (pass = cblk->curpass; pass != endpasses; ++pass) {
1869 if (pass->rdslope >= goodthresh) {
1874 for (pass = cblk->curpass; pass != pass1; ++pass) {
1875 pass->lyrno = lyrno;
1877 for (; pass != endpasses; ++pass) {
1887 /* Perform tier 2 coding. */
1888 endcomps = &tile->tcmpts[tile->numtcmpts];
1889 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1890 endlvls = &comp->rlvls[comp->numrlvls];
1891 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1895 for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
1896 if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
1904 if (jas_getdbglevel() >= 5) {
1905 dump_layeringinfo(enc);
1908 jas_stream_close(out);
1910 JAS_DBGLOG(10, ("done doing rateallocation\n"));
1912 fprintf(stderr, "DONE RATE ALLOCATE\n");
1918 /******************************************************************************\
1919 * Tile constructors and destructors.
1920 \******************************************************************************/
1922 jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno)
1924 jpc_enc_tile_t *tile;
1925 uint_fast32_t htileno;
1926 uint_fast32_t vtileno;
1928 uint_fast16_t cmptno;
1929 jpc_enc_tcmpt_t *tcmpt;
1931 if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) {
1935 /* Initialize a few members used in error recovery. */
1938 tile->numtcmpts = cp->numcmpts;
1941 tile->tileno = tileno;
1942 htileno = tileno % cp->numhtiles;
1943 vtileno = tileno / cp->numhtiles;
1945 /* Calculate the coordinates of the top-left and bottom-right
1946 corners of the tile. */
1947 tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth,
1949 tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight,
1951 tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth,
1953 tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight,
1956 /* Initialize some tile coding parameters. */
1957 tile->intmode = cp->tcp.intmode;
1958 tile->csty = cp->tcp.csty;
1959 tile->prg = cp->tcp.prg;
1960 tile->mctid = cp->tcp.mctid;
1962 tile->numlyrs = cp->tcp.numlyrs;
1963 if (!(tile->lyrsizes = jas_malloc(tile->numlyrs *
1964 sizeof(uint_fast32_t)))) {
1967 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1968 tile->lyrsizes[lyrno] = 0;
1971 /* Allocate an array for the per-tile-component information. */
1972 if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) {
1975 /* Initialize a few members critical for error recovery. */
1976 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
1977 ++cmptno, ++tcmpt) {
1982 /* Initialize the per-tile-component information. */
1983 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
1984 ++cmptno, ++tcmpt) {
1985 if (!tcmpt_create(tcmpt, cp, image, tile)) {
1990 /* Initialize the synthesis weights for the MCT. */
1991 switch (tile->mctid) {
1993 tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0));
1994 tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875));
1995 tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875));
1998 tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000));
1999 tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584));
2000 tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755));
2004 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
2005 ++cmptno, ++tcmpt) {
2006 tcmpt->synweight = JPC_FIX_ONE;
2011 if (!(tile->pi = jpc_enc_pi_create(cp, tile))) {
2020 jpc_enc_tile_destroy(tile);
2025 void jpc_enc_tile_destroy(jpc_enc_tile_t *tile)
2027 jpc_enc_tcmpt_t *tcmpt;
2028 int_fast16_t cmptno;
2031 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno <
2032 tile->numtcmpts; ++cmptno, ++tcmpt) {
2033 tcmpt_destroy(tcmpt);
2035 jas_free(tile->tcmpts);
2037 if (tile->lyrsizes) {
2038 jas_free(tile->lyrsizes);
2041 jpc_pi_destroy(tile->pi);
2046 static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
2047 jas_image_t *image, jpc_enc_tile_t *tile)
2049 uint_fast16_t cmptno;
2050 int_fast16_t rlvlno;
2051 jpc_enc_rlvl_t *rlvl;
2056 uint_fast32_t cmpttlx;
2057 uint_fast32_t cmpttly;
2059 jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
2066 /* Deduce the component number. */
2067 cmptno = tcmpt - tile->tcmpts;
2069 ccp = &cp->ccps[cmptno];
2071 /* Compute the coordinates of the top-left and bottom-right
2072 corners of this tile-component. */
2073 tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx);
2074 tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy);
2075 brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx);
2076 bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy);
2078 /* Create a sequence to hold the tile-component sample data. */
2079 if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) {
2083 /* Get the image data associated with this tile-component. */
2084 cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx);
2085 cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy);
2086 if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly,
2087 brx - tlx, bry - tly, tcmpt->data)) {
2091 tcmpt->synweight = 0;
2092 tcmpt->qmfbid = cp->tccp.qmfbid;
2093 tcmpt->numrlvls = cp->tccp.maxrlvls;
2094 tcmpt->numbands = 3 * tcmpt->numrlvls - 2;
2095 if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) {
2099 for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) {
2100 tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno];
2101 tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno];
2103 tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn;
2104 tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn;
2105 tcmpt->cblksty = cp->tccp.cblksty;
2106 tcmpt->csty = cp->tccp.csty;
2108 tcmpt->numstepsizes = tcmpt->numbands;
2109 assert(tcmpt->numstepsizes <= JPC_MAXBANDS);
2110 memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes *
2111 sizeof(uint_fast16_t)));
2113 /* Retrieve information about the various bands. */
2114 jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data),
2115 jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data),
2116 jas_seq2d_yend(tcmpt->data), bandinfos);
2118 if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) {
2121 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2124 rlvl->tcmpt = tcmpt;
2126 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2128 if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) {
2137 tcmpt_destroy(tcmpt);
2142 static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt)
2144 jpc_enc_rlvl_t *rlvl;
2145 int_fast16_t rlvlno;
2148 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2152 jas_free(tcmpt->rlvls);
2156 jas_seq2d_destroy(tcmpt->data);
2159 jpc_tsfb_destroy(tcmpt->tsfb);
2163 static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
2164 jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos)
2166 uint_fast16_t rlvlno;
2167 uint_fast32_t tlprctlx;
2168 uint_fast32_t tlprctly;
2169 uint_fast32_t brprcbrx;
2170 uint_fast32_t brprcbry;
2171 int_fast16_t bandno;
2172 jpc_enc_band_t *band;
2174 /* Deduce the resolution level. */
2175 rlvlno = rlvl - tcmpt->rlvls;
2177 /* Initialize members required for error recovery. */
2179 rlvl->tcmpt = tcmpt;
2181 /* Compute the coordinates of the top-left and bottom-right
2182 corners of the tile-component at this resolution. */
2183 rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls -
2185 rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls -
2187 rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls -
2189 rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls -
2192 if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) {
2199 rlvl->numbands = (!rlvlno) ? 1 : 3;
2200 rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno];
2201 rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno];
2203 rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
2204 rlvl->cbgheightexpn = rlvl->prcheightexpn;
2206 rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
2207 rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
2209 rlvl->cblkwidthexpn = JAS_MIN((int)cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn);
2210 rlvl->cblkheightexpn = JAS_MIN((int)cp->tccp.cblkheightexpn, rlvl->cbgheightexpn);
2212 /* Compute the number of precincts. */
2213 tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
2214 tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
2215 brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn);
2216 brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn);
2217 rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn);
2218 rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn);
2219 rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
2221 if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) {
2224 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2230 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2232 if (!band_create(band, cp, rlvl, bandinfos)) {
2244 static void rlvl_destroy(jpc_enc_rlvl_t *rlvl)
2246 jpc_enc_band_t *band;
2247 int_fast16_t bandno;
2250 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2254 jas_free(rlvl->bands);
2258 static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
2259 jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos)
2261 uint_fast16_t bandno;
2262 uint_fast16_t gblbandno;
2263 uint_fast16_t rlvlno;
2264 jpc_tsfb_band_t *bandinfo;
2265 jpc_enc_tcmpt_t *tcmpt;
2269 tcmpt = rlvl->tcmpt;
2274 /* Deduce the resolution level and band number. */
2275 rlvlno = rlvl - rlvl->tcmpt->rlvls;
2276 bandno = band - rlvl->bands;
2277 gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1);
2279 bandinfo = &bandinfos[gblbandno];
2281 if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) {
2282 if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
2285 jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart,
2286 bandinfo->locystart, bandinfo->locxend, bandinfo->locyend);
2287 jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart);
2289 band->orient = bandinfo->orient;
2290 band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno,
2293 band->absstepsize = 0;
2295 band->synweight = bandinfo->synenergywt;
2298 if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) {
2301 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
2306 prc->savincltree = 0;
2307 prc->savnlibtree = 0;
2310 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
2312 if (!prc_create(prc, cp, band)) {
2325 static void band_destroy(jpc_enc_band_t *band)
2328 jpc_enc_rlvl_t *rlvl;
2333 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
2337 jas_free(band->prcs);
2340 jas_seq2d_destroy(band->data);
2344 static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band)
2346 uint_fast32_t prcno;
2347 uint_fast32_t prcxind;
2348 uint_fast32_t prcyind;
2349 uint_fast32_t cbgtlx;
2350 uint_fast32_t cbgtly;
2351 uint_fast32_t tlprctlx;
2352 uint_fast32_t tlprctly;
2353 uint_fast32_t tlcbgtlx;
2354 uint_fast32_t tlcbgtly;
2355 uint_fast16_t rlvlno;
2356 jpc_enc_rlvl_t *rlvl;
2357 uint_fast32_t tlcblktlx;
2358 uint_fast32_t tlcblktly;
2359 uint_fast32_t brcblkbrx;
2360 uint_fast32_t brcblkbry;
2361 int_fast32_t cblkno;
2362 jpc_enc_cblk_t *cblk;
2363 jpc_enc_tcmpt_t *tcmpt;
2367 prc->savincltree = 0;
2369 prc->savnlibtree = 0;
2372 tcmpt = rlvl->tcmpt;
2373 rlvlno = rlvl - tcmpt->rlvls;
2374 prcno = prc - band->prcs;
2375 prcxind = prcno % rlvl->numhprcs;
2376 prcyind = prcno / rlvl->numhprcs;
2379 tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
2380 tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
2382 tlcbgtlx = tlprctlx;
2383 tlcbgtly = tlprctly;
2385 tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1);
2386 tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1);
2389 /* Compute the coordinates of the top-left and bottom-right
2390 corners of the precinct. */
2391 cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn);
2392 cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn);
2393 prc->tlx = JAS_MAX((uint_fast32_t)jas_seq2d_xstart(band->data), cbgtlx);
2394 prc->tly = JAS_MAX((uint_fast32_t)jas_seq2d_ystart(band->data), cbgtly);
2395 prc->brx = JAS_MIN((uint_fast32_t)jas_seq2d_xend(band->data), cbgtlx +
2396 (1 << rlvl->cbgwidthexpn));
2397 prc->bry = JAS_MIN((uint_fast32_t)jas_seq2d_yend(band->data), cbgtly +
2398 (1 << rlvl->cbgheightexpn));
2400 if (prc->tlx < prc->brx && prc->tly < prc->bry) {
2401 /* The precinct contains at least one code block. */
2403 tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
2404 tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
2405 brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn);
2406 brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn);
2407 prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx,
2408 rlvl->cblkwidthexpn);
2409 prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly,
2410 rlvl->cblkheightexpn);
2411 prc->numcblks = prc->numhcblks * prc->numvcblks;
2413 if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks,
2417 if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks,
2421 if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks,
2425 if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks,
2430 if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) {
2433 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2442 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2444 if (!cblk_create(cblk, cp, prc)) {
2449 /* The precinct does not contain any code blocks. */
2450 prc->tlx = prc->brx;
2451 prc->tly = prc->bry;
2458 prc->savincltree = 0;
2459 prc->savnlibtree = 0;
2469 static void prc_destroy(jpc_enc_prc_t *prc)
2471 jpc_enc_cblk_t *cblk;
2472 int_fast32_t cblkno;
2475 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2479 jas_free(prc->cblks);
2481 if (prc->incltree) {
2482 jpc_tagtree_destroy(prc->incltree);
2484 if (prc->nlibtree) {
2485 jpc_tagtree_destroy(prc->nlibtree);
2487 if (prc->savincltree) {
2488 jpc_tagtree_destroy(prc->savincltree);
2490 if (prc->savnlibtree) {
2491 jpc_tagtree_destroy(prc->savnlibtree);
2495 static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc)
2497 jpc_enc_band_t *band;
2498 uint_fast32_t cblktlx;
2499 uint_fast32_t cblktly;
2500 uint_fast32_t cblkbrx;
2501 uint_fast32_t cblkbry;
2502 jpc_enc_rlvl_t *rlvl;
2503 uint_fast32_t cblkxind;
2504 uint_fast32_t cblkyind;
2505 uint_fast32_t cblkno;
2506 uint_fast32_t tlcblktlx;
2507 uint_fast32_t tlcblktly;
2511 cblkno = cblk - prc->cblks;
2512 cblkxind = cblkno % prc->numhcblks;
2513 cblkyind = cblkno / prc->numhcblks;
2514 rlvl = prc->band->rlvl;
2517 cblk->numpasses = 0;
2519 cblk->numencpasses = 0;
2521 cblk->numlenbits = 0;
2528 cblk->savedcurpass = 0;
2529 cblk->savednumlenbits = 0;
2530 cblk->savednumencpasses = 0;
2533 tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
2534 tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
2535 cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx);
2536 cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly);
2537 cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn),
2539 cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn),
2542 assert(cblktlx < cblkbrx && cblktly < cblkbry);
2543 if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) {
2546 jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry);
2555 static void cblk_destroy(jpc_enc_cblk_t *cblk)
2557 int_fast16_t passno;
2558 jpc_enc_pass_t *pass;
2560 for (passno = 0, pass = cblk->passes; passno < cblk->numpasses;
2564 jas_free(cblk->passes);
2567 jas_stream_close(cblk->stream);
2570 jpc_mqenc_destroy(cblk->mqenc);
2573 jas_seq2d_destroy(cblk->data);
2576 jas_seq2d_destroy(cblk->flags);
2580 static void pass_destroy(jpc_enc_pass_t *pass)
2582 /* XXX - need to free resources here */
2586 void jpc_enc_dump(jpc_enc_t *enc)
2588 jpc_enc_tile_t *tile;
2589 jpc_enc_tcmpt_t *tcmpt;
2590 jpc_enc_rlvl_t *rlvl;
2591 jpc_enc_band_t *band;
2593 jpc_enc_cblk_t *cblk;
2594 int_fast16_t cmptno;
2595 int_fast16_t rlvlno;
2596 int_fast16_t bandno;
2598 int_fast32_t cblkno;
2600 tile = enc->curtile;
2602 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno,
2604 fprintf(stderr, " tcmpt %5d %5d %5d %5d\n", jas_seq2d_xstart(tcmpt->data), jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), jas_seq2d_yend(tcmpt->data));
2605 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2607 fprintf(stderr, " rlvl %5d %5d %5d %5d\n", rlvl->tlx, rlvl->tly, rlvl->brx, rlvl->bry);
2608 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2613 fprintf(stderr, " band %5d %5d %5d %5d\n", jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), jas_seq2d_yend(band->data));
2614 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
2616 fprintf(stderr, " prc %5d %5d %5d %5d (%5d %5d)\n", prc->tlx, prc->tly, prc->brx, prc->bry, prc->brx - prc->tlx, prc->bry - prc->tly);
2620 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2622 fprintf(stderr, " cblk %5d %5d %5d %5d\n", jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), jas_seq2d_yend(cblk->data));