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.1 2005/05/22 18:33:04 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 \******************************************************************************/
870 static int jpc_calcssmant(jpc_fix_t stepsize)
876 n = jpc_firstone(stepsize);
877 e = n - JPC_FIX_FRACBITS;
879 m = (stepsize >> (n - 11)) & 0x7ff;
881 m = (stepsize & ((1 << n) - 1)) << (11 - n);
886 static int jpc_calcssexp(jpc_fix_t stepsize)
888 return jpc_firstone(stepsize) - JPC_FIX_FRACBITS;
891 static int jpc_enc_encodemainhdr(jpc_enc_t *enc)
901 jpc_enc_tccp_t *tccp;
902 uint_fast16_t cmptno;
903 jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
904 jpc_fix_t mctsynweight;
907 jpc_tsfb_band_t *bandinfo;
908 uint_fast16_t numbands;
909 uint_fast16_t bandno;
910 uint_fast16_t rlvlno;
911 uint_fast16_t analgain;
912 jpc_fix_t absstepsize;
918 startoff = jas_stream_getrwcount(enc->out);
920 /* Write SOC marker segment. */
921 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) {
924 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
925 fprintf(stderr, "cannot write SOC marker\n");
928 jpc_ms_destroy(enc->mrk);
931 /* Write SIZ marker segment. */
932 if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) {
935 siz = &enc->mrk->parms.siz;
937 siz->xoff = cp->imgareatlx;
938 siz->yoff = cp->imgareatly;
939 siz->width = cp->refgrdwidth;
940 siz->height = cp->refgrdheight;
941 siz->tilexoff = cp->tilegrdoffx;
942 siz->tileyoff = cp->tilegrdoffy;
943 siz->tilewidth = cp->tilewidth;
944 siz->tileheight = cp->tileheight;
945 siz->numcomps = cp->numcmpts;
946 siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t));
948 for (i = 0; i < JAS_CAST(int, cp->numcmpts); ++i) {
949 siz->comps[i].prec = cp->ccps[i].prec;
950 siz->comps[i].sgnd = cp->ccps[i].sgnd;
951 siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx;
952 siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy;
954 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
955 fprintf(stderr, "cannot write SIZ marker\n");
958 jpc_ms_destroy(enc->mrk);
961 if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) {
964 sprintf(buf, "Creator: JasPer Version %s", jas_getversion());
965 com = &enc->mrk->parms.com;
966 com->len = strlen(buf);
967 com->regid = JPC_COM_LATIN;
968 if (!(com->data = JAS_CAST(uchar *, jas_strdup(buf)))) {
971 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
972 fprintf(stderr, "cannot write COM marker\n");
975 jpc_ms_destroy(enc->mrk);
979 if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) {
982 crg = &enc->mrk->parms.crg;
983 crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t));
984 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
985 fprintf(stderr, "cannot write CRG marker\n");
988 jpc_ms_destroy(enc->mrk);
994 for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) {
995 tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1);
996 jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls,
998 jpc_tsfb_destroy(tsfb);
999 mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno);
1000 numbands = 3 * tccp->maxrlvls - 2;
1001 for (bandno = 0, bandinfo = bandinfos; bandno < numbands;
1002 ++bandno, ++bandinfo) {
1003 rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0;
1004 analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls,
1005 rlvlno, bandinfo->orient);
1006 if (!tcp->intmode) {
1007 absstepsize = jpc_fix_div(jpc_inttofix(1 <<
1008 (analgain + 1)), bandinfo->synenergywt);
1010 absstepsize = jpc_inttofix(1);
1012 cp->ccps[cmptno].stepsizes[bandno] =
1013 jpc_abstorelstepsize(absstepsize,
1014 cp->ccps[cmptno].prec + analgain);
1016 cp->ccps[cmptno].numstepsizes = numbands;
1019 if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
1022 cod = &enc->mrk->parms.cod;
1023 cod->csty = cp->tccp.csty | cp->tcp.csty;
1024 cod->compparms.csty = cp->tccp.csty | cp->tcp.csty;
1025 cod->compparms.numdlvls = cp->tccp.maxrlvls - 1;
1026 cod->compparms.numrlvls = cp->tccp.maxrlvls;
1027 cod->prg = cp->tcp.prg;
1028 cod->numlyrs = cp->tcp.numlyrs;
1029 cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn);
1030 cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn);
1031 cod->compparms.cblksty = cp->tccp.cblksty;
1032 cod->compparms.qmfbid = cp->tccp.qmfbid;
1033 cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE);
1034 if (tccp->csty & JPC_COX_PRT) {
1035 for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
1036 cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno];
1037 cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno];
1040 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1041 fprintf(stderr, "cannot write COD marker\n");
1044 jpc_ms_destroy(enc->mrk);
1047 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) {
1050 qcd = &enc->mrk->parms.qcd;
1051 qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
1052 JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1053 qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes;
1054 qcd->compparms.numguard = cp->tccp.numgbits;
1055 qcd->compparms.stepsizes = cp->ccps[0].stepsizes;
1056 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1059 /* We do not want the step size array to be freed! */
1060 qcd->compparms.stepsizes = 0;
1061 jpc_ms_destroy(enc->mrk);
1065 for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) {
1066 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
1069 qcc = &enc->mrk->parms.qcc;
1070 qcc->compno = cmptno;
1071 qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
1072 JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1073 qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes;
1074 qcc->compparms.numguard = cp->tccp.numgbits;
1075 qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes;
1076 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1079 /* We do not want the step size array to be freed! */
1080 qcc->compparms.stepsizes = 0;
1081 jpc_ms_destroy(enc->mrk);
1085 #define MAINTLRLEN 2
1086 mainhdrlen = jas_stream_getrwcount(enc->out) - startoff;
1087 enc->len += mainhdrlen;
1088 if (enc->cp->totalsize != UINT_FAST32_MAX) {
1089 uint_fast32_t overhead;
1090 overhead = mainhdrlen + MAINTLRLEN;
1091 enc->mainbodysize = (enc->cp->totalsize >= overhead) ?
1092 (enc->cp->totalsize - overhead) : 0;
1094 enc->mainbodysize = UINT_FAST32_MAX;
1100 static int jpc_enc_encodemainbody(jpc_enc_t *enc)
1107 jpc_enc_tcmpt_t *comp;
1108 jpc_enc_tcmpt_t *endcomps;
1109 jpc_enc_band_t *band;
1110 jpc_enc_band_t *endbands;
1111 jpc_enc_rlvl_t *lvl;
1121 jpc_enc_tile_t *tile;
1127 jpc_enc_ccp_t *ccps;
1128 jpc_enc_tccp_t *tccp;
1140 /* Avoid compile warnings. */
1143 for (tileno = 0; tileno < JAS_CAST(int, cp->numtiles); ++tileno) {
1144 tilex = tileno % cp->numhtiles;
1145 tiley = tileno / cp->numhtiles;
1147 if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) {
1151 tile = enc->curtile;
1153 if (jas_getdbglevel() >= 10) {
1157 endcomps = &tile->tcmpts[tile->numtcmpts];
1158 for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) {
1159 if (!cp->ccps[cmptno].sgnd) {
1160 adjust = 1 << (cp->ccps[cmptno].prec - 1);
1161 for (i = 0; i < jas_matrix_numrows(comp->data); ++i) {
1162 for (j = 0; j < jas_matrix_numcols(comp->data); ++j) {
1163 *jas_matrix_getref(comp->data, i, j) -= adjust;
1169 if (!tile->intmode) {
1170 endcomps = &tile->tcmpts[tile->numtcmpts];
1171 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1172 jas_matrix_asl(comp->data, JPC_FIX_FRACBITS);
1176 switch (tile->mctid) {
1178 assert(jas_image_numcmpts(enc->image) == 3);
1179 jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data,
1180 tile->tcmpts[2].data);
1183 assert(jas_image_numcmpts(enc->image) == 3);
1184 jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data,
1185 tile->tcmpts[2].data);
1191 for (i = 0; i < jas_image_numcmpts(enc->image); ++i) {
1192 comp = &tile->tcmpts[i];
1193 jpc_tsfb_analyze(comp->tsfb, ((comp->qmfbid == JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), comp->data);
1198 endcomps = &tile->tcmpts[tile->numtcmpts];
1199 for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) {
1202 /* All bands must have a corresponding quantizer step size,
1203 even if they contain no samples and are never coded. */
1204 /* Some bands may not be hit by the loop below, so we must
1205 initialize all of the step sizes to a sane value. */
1206 memset(comp->stepsizes, 0, sizeof(comp->stepsizes));
1207 for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) {
1209 absbandno += rlvlno ? 3 : 1;
1212 endbands = &lvl->bands[lvl->numbands];
1213 for (band = lvl->bands; band != endbands; ++band) {
1220 for (y = 0; y < JAS_CAST(uint_fast32_t, jas_matrix_numrows(band->data)); ++y) {
1221 for (x = 0; x < JAS_CAST(uint_fast32_t, jas_matrix_numcols(band->data)); ++x) {
1222 mag = abs(jas_matrix_get(band->data, y, x));
1228 if (tile->intmode) {
1229 actualnumbps = jpc_firstone(mxmag) + 1;
1231 actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS;
1233 numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 +
1236 fprintf(stderr, "%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band->analgain, mxmag, actualnumbps, numgbits);
1238 if (numgbits > mingbits) {
1239 mingbits = numgbits;
1241 if (!tile->intmode) {
1242 band->absstepsize = jpc_fix_div(jpc_inttofix(1
1243 << (band->analgain + 1)),
1246 band->absstepsize = jpc_inttofix(1);
1248 band->stepsize = jpc_abstorelstepsize(
1249 band->absstepsize, cp->ccps[cmptno].prec +
1251 band->numbps = cp->tccp.numgbits +
1252 JPC_QCX_GETEXPN(band->stepsize) - 1;
1254 if ((!tile->intmode) && band->data) {
1255 quantize(band->data, band->absstepsize);
1258 comp->stepsizes[absbandno] = band->stepsize;
1263 assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS);
1264 if (!tile->intmode) {
1265 jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS);
1267 jas_matrix_asl(comp->data, JPC_NUMEXTRABITS);
1271 fprintf(stderr, "mingbits %d\n", mingbits);
1273 if (mingbits > cp->tccp.numgbits) {
1274 fprintf(stderr, "error: too few guard bits (need at least %d)\n",
1280 if (!(enc->tmpstream = jas_stream_memopen(0, 0))) {
1281 fprintf(stderr, "cannot open tmp file\n");
1285 /* Write the tile header. */
1286 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) {
1289 sot = &enc->mrk->parms.sot;
1291 sot->tileno = tileno;
1294 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1295 fprintf(stderr, "cannot write SOT marker\n");
1298 jpc_ms_destroy(enc->mrk);
1301 /************************************************************************/
1302 /************************************************************************/
1303 /************************************************************************/
1306 for (cmptno = 0; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno) {
1307 comp = &tile->tcmpts[cmptno];
1308 if (comp->numrlvls != tccp->maxrlvls) {
1309 if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
1312 /* XXX = this is not really correct. we are using comp #0's precint sizes
1313 and other characteristics */
1314 comp = &tile->tcmpts[0];
1315 cod = &enc->mrk->parms.cod;
1316 cod->compparms.csty = 0;
1317 cod->compparms.numdlvls = comp->numrlvls - 1;
1318 cod->prg = tile->prg;
1319 cod->numlyrs = tile->numlyrs;
1320 cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn);
1321 cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn);
1322 cod->compparms.cblksty = comp->cblksty;
1323 cod->compparms.qmfbid = comp->qmfbid;
1324 cod->mctrans = (tile->mctid != JPC_MCT_NONE);
1325 for (i = 0; i < comp->numrlvls; ++i) {
1326 cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn;
1327 cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn;
1329 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1332 jpc_ms_destroy(enc->mrk);
1337 for (cmptno = 0, comp = tile->tcmpts; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno, ++comp) {
1338 ccps = &cp->ccps[cmptno];
1339 if (JAS_CAST(int, ccps->numstepsizes) == comp->numstepsizes) {
1341 for (bandno = 0; bandno < JAS_CAST(int, ccps->numstepsizes); ++bandno) {
1342 if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) {
1350 if (!samestepsizes) {
1351 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
1354 qcc = &enc->mrk->parms.qcc;
1355 qcc->compno = cmptno;
1356 qcc->compparms.numguard = cp->tccp.numgbits;
1357 qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ?
1358 JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1359 qcc->compparms.numstepsizes = comp->numstepsizes;
1360 qcc->compparms.stepsizes = comp->stepsizes;
1361 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1364 qcc->compparms.stepsizes = 0;
1365 jpc_ms_destroy(enc->mrk);
1370 /* Write a SOD marker to indicate the end of the tile header. */
1371 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) {
1374 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1375 fprintf(stderr, "cannot write SOD marker\n");
1378 jpc_ms_destroy(enc->mrk);
1380 tilehdrlen = jas_stream_getrwcount(enc->tmpstream);
1382 /************************************************************************/
1383 /************************************************************************/
1384 /************************************************************************/
1386 if (jpc_enc_enccblks(enc)) {
1392 rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) /
1393 ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight -
1395 tile->rawsize = cp->rawsize * rho;
1397 for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) {
1398 tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl(
1399 cp->tcp.ilyrrates[lyrno]);
1401 tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ?
1402 (rho * enc->mainbodysize) : UINT_FAST32_MAX;
1403 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1404 if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) {
1405 if (tilehdrlen <= JAS_CAST(long, tile->lyrsizes[lyrno])) {
1406 tile->lyrsizes[lyrno] -= tilehdrlen;
1408 tile->lyrsizes[lyrno] = 0;
1413 if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) {
1418 fprintf(stderr, "ENCODE TILE DATA\n");
1420 if (jpc_enc_encodetiledata(enc)) {
1421 fprintf(stderr, "dotile failed\n");
1425 /************************************************************************/
1426 /************************************************************************/
1427 /************************************************************************/
1429 /************************************************************************/
1430 /************************************************************************/
1431 /************************************************************************/
1433 tilelen = jas_stream_tell(enc->tmpstream);
1435 if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) {
1438 jpc_putuint32(enc->tmpstream, tilelen);
1440 if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) {
1443 if (jpc_putdata(enc->out, enc->tmpstream, -1)) {
1446 enc->len += tilelen;
1448 jas_stream_close(enc->tmpstream);
1451 jpc_enc_tile_destroy(enc->curtile);
1459 int jpc_enc_encodetiledata(jpc_enc_t *enc)
1461 assert(enc->tmpstream);
1462 if (jpc_enc_encpkts(enc, enc->tmpstream)) {
1468 int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk)
1470 jpc_enc_pass_t *pass;
1472 jas_stream_memobj_t *smo;
1474 smo = cblk->stream->obj_;
1477 for (i = 0; i < numpasses; ++i) {
1478 fprintf(stderr, "start=%d end=%d type=%d term=%d lyrno=%d firstchar=%02x size=%ld pos=%ld\n",
1479 (int)pass->start, (int)pass->end, (int)pass->type, (int)pass->term, (int)pass->lyrno,
1480 smo->buf_[pass->start], (long)smo->len_, (long)smo->pos_);
1482 jas_memdump(stderr, &smo->buf_[pass->start], pass->end - pass->start);
1489 void quantize(jas_matrix_t *data, jpc_fix_t stepsize)
1495 if (stepsize == jpc_inttofix(1)) {
1499 for (i = 0; i < jas_matrix_numrows(data); ++i) {
1500 for (j = 0; j < jas_matrix_numcols(data); ++j) {
1501 t = jas_matrix_get(data, i, j);
1505 t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize));
1507 t = jpc_fix_div(t, stepsize);
1511 jas_matrix_set(data, i, j, t);
1516 void calcrdslopes(jpc_enc_cblk_t *cblk)
1518 jpc_enc_pass_t *endpasses;
1519 jpc_enc_pass_t *pass0;
1520 jpc_enc_pass_t *pass1;
1521 jpc_enc_pass_t *pass2;
1527 endpasses = &cblk->passes[cblk->numpasses];
1528 pass2 = cblk->passes;
1530 while (pass2 != endpasses) {
1532 for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) {
1533 dd = pass1->cumwmsedec;
1536 dd -= pass0->cumwmsedec;
1540 pass1->rdslope = JPC_BADRDSLOPE;
1541 if (pass1 >= pass2) {
1546 if (pass1 < pass2 && pass1->rdslope <= 0) {
1555 if (pass0 && slope >= slope0) {
1559 pass1->rdslope = slope;
1560 if (pass1 >= pass2) {
1569 for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) {
1570 if (pass0->rdslope > 0.0) {
1571 fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes,
1572 fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope);
1578 void dump_layeringinfo(jpc_enc_t *enc)
1581 jpc_enc_tcmpt_t *tcmpt;
1583 jpc_enc_rlvl_t *rlvl;
1585 jpc_enc_band_t *band;
1589 jpc_enc_cblk_t *cblk;
1591 jpc_enc_pass_t *pass;
1594 jpc_enc_tile_t *tile;
1596 tile = enc->curtile;
1598 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1599 fprintf(stderr, "lyrno = %02d\n", lyrno);
1600 for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts;
1601 ++tcmptno, ++tcmpt) {
1602 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
1607 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
1612 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
1617 for (cblkno = 0, cblk = prc->cblks; cblkno <
1618 prc->numcblks; ++cblkno, ++cblk) {
1619 for (passno = 0, pass = cblk->passes; passno <
1620 cblk->numpasses && pass->lyrno == lyrno;
1622 fprintf(stderr, "lyrno=%02d cmptno=%02d rlvlno=%02d bandno=%02d prcno=%02d cblkno=%03d passno=%03d\n", lyrno, tcmptno, rlvlno, bandno, prcno, cblkno, passno);
1632 int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens)
1640 jpc_flt_t goodthresh;
1646 jpc_enc_tcmpt_t *comp;
1647 jpc_enc_tcmpt_t *endcomps;
1648 jpc_enc_rlvl_t *lvl;
1649 jpc_enc_rlvl_t *endlvls;
1650 jpc_enc_band_t *band;
1651 jpc_enc_band_t *endbands;
1652 jpc_enc_cblk_t *cblk;
1653 jpc_enc_cblk_t *endcblks;
1654 jpc_enc_pass_t *pass;
1655 jpc_enc_pass_t *endpasses;
1656 jpc_enc_pass_t *pass1;
1657 jpc_flt_t mxrdslope;
1658 jpc_flt_t mnrdslope;
1659 jpc_enc_tile_t *tile;
1663 tile = enc->curtile;
1665 for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) {
1666 if (cumlens[lyrno - 1] > cumlens[lyrno]) {
1671 if (!(out = jas_stream_memopen(0, 0))) {
1676 /* Find minimum and maximum R-D slope values. */
1677 mnrdslope = DBL_MAX;
1679 endcomps = &tile->tcmpts[tile->numtcmpts];
1680 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1681 endlvls = &comp->rlvls[comp->numrlvls];
1682 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1686 endbands = &lvl->bands[lvl->numbands];
1687 for (band = lvl->bands; band != endbands; ++band) {
1691 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1695 endcblks = &prc->cblks[prc->numcblks];
1696 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1698 endpasses = &cblk->passes[cblk->numpasses];
1699 for (pass = cblk->passes; pass != endpasses; ++pass) {
1700 if (pass->rdslope > 0) {
1701 if (pass->rdslope < mnrdslope) {
1702 mnrdslope = pass->rdslope;
1704 if (pass->rdslope > mxrdslope) {
1705 mxrdslope = pass->rdslope;
1714 if (jas_getdbglevel()) {
1715 fprintf(stderr, "min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope);
1718 jpc_init_t2state(enc, 1);
1720 for (lyrno = 0; lyrno < numlyrs; ++lyrno) {
1731 cumlen = cumlens[lyrno];
1732 if (cumlen == UINT_FAST32_MAX) {
1733 /* Only the last layer can be free of a rate
1734 constraint (e.g., for lossless coding). */
1735 assert(lyrno == numlyrs - 1);
1741 thresh = (lo + hi) / 2;
1743 /* Save the tier 2 coding state. */
1744 jpc_save_t2state(enc);
1745 oldpos = jas_stream_tell(out);
1746 assert(oldpos >= 0);
1748 /* Assign all passes with R-D slopes greater than or
1749 equal to the current threshold to this layer. */
1750 endcomps = &tile->tcmpts[tile->numtcmpts];
1751 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1752 endlvls = &comp->rlvls[comp->numrlvls];
1753 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1757 endbands = &lvl->bands[lvl->numbands];
1758 for (band = lvl->bands; band != endbands; ++band) {
1762 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1766 endcblks = &prc->cblks[prc->numcblks];
1767 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1768 if (cblk->curpass) {
1769 endpasses = &cblk->passes[cblk->numpasses];
1770 pass1 = cblk->curpass;
1771 for (pass = cblk->curpass; pass != endpasses; ++pass) {
1772 if (pass->rdslope >= thresh) {
1776 for (pass = cblk->curpass; pass != pass1; ++pass) {
1777 pass->lyrno = lyrno;
1779 for (; pass != endpasses; ++pass) {
1789 /* Perform tier 2 coding. */
1790 endcomps = &tile->tcmpts[tile->numtcmpts];
1791 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1792 endlvls = &comp->rlvls[comp->numrlvls];
1793 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1797 for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
1798 if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
1805 pos = jas_stream_tell(out);
1807 /* Check the rate constraint. */
1810 /* The rate is too high. */
1812 } else if (pos <= cumlen) {
1813 /* The rate is low enough, so try higher. */
1815 if (!success || thresh < goodthresh) {
1816 goodthresh = thresh;
1821 /* Save the tier 2 coding state. */
1822 jpc_restore_t2state(enc);
1823 if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) {
1827 if (jas_getdbglevel()) {
1828 fprintf(stderr, "maxlen=%08ld actuallen=%08ld thresh=%f\n", cumlen, pos, thresh);
1832 } while (lo < hi - 1e-3 && numiters < 32);
1835 fprintf(stderr, "warning: empty layer generated\n");
1838 if (jas_getdbglevel()) {
1839 fprintf(stderr, "success %d goodthresh %f\n", success, goodthresh);
1842 /* Assign all passes with R-D slopes greater than or
1843 equal to the selected threshold to this layer. */
1844 endcomps = &tile->tcmpts[tile->numtcmpts];
1845 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1846 endlvls = &comp->rlvls[comp->numrlvls];
1847 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1851 endbands = &lvl->bands[lvl->numbands];
1852 for (band = lvl->bands; band != endbands; ++band) {
1856 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1860 endcblks = &prc->cblks[prc->numcblks];
1861 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1862 if (cblk->curpass) {
1863 endpasses = &cblk->passes[cblk->numpasses];
1864 pass1 = cblk->curpass;
1866 for (pass = cblk->curpass; pass != endpasses; ++pass) {
1867 if (pass->rdslope >= goodthresh) {
1872 for (pass = cblk->curpass; pass != pass1; ++pass) {
1873 pass->lyrno = lyrno;
1875 for (; pass != endpasses; ++pass) {
1885 /* Perform tier 2 coding. */
1886 endcomps = &tile->tcmpts[tile->numtcmpts];
1887 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1888 endlvls = &comp->rlvls[comp->numrlvls];
1889 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1893 for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
1894 if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
1902 if (jas_getdbglevel() >= 5) {
1903 dump_layeringinfo(enc);
1906 jas_stream_close(out);
1908 JAS_DBGLOG(10, ("done doing rateallocation\n"));
1910 fprintf(stderr, "DONE RATE ALLOCATE\n");
1916 /******************************************************************************\
1917 * Tile constructors and destructors.
1918 \******************************************************************************/
1920 jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno)
1922 jpc_enc_tile_t *tile;
1923 uint_fast32_t htileno;
1924 uint_fast32_t vtileno;
1925 uint_fast16_t lyrno;
1926 uint_fast16_t cmptno;
1927 jpc_enc_tcmpt_t *tcmpt;
1929 if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) {
1933 /* Initialize a few members used in error recovery. */
1936 tile->numtcmpts = cp->numcmpts;
1939 tile->tileno = tileno;
1940 htileno = tileno % cp->numhtiles;
1941 vtileno = tileno / cp->numhtiles;
1943 /* Calculate the coordinates of the top-left and bottom-right
1944 corners of the tile. */
1945 tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth,
1947 tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight,
1949 tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth,
1951 tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight,
1954 /* Initialize some tile coding parameters. */
1955 tile->intmode = cp->tcp.intmode;
1956 tile->csty = cp->tcp.csty;
1957 tile->prg = cp->tcp.prg;
1958 tile->mctid = cp->tcp.mctid;
1960 tile->numlyrs = cp->tcp.numlyrs;
1961 if (!(tile->lyrsizes = jas_malloc(tile->numlyrs *
1962 sizeof(uint_fast32_t)))) {
1965 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1966 tile->lyrsizes[lyrno] = 0;
1969 /* Allocate an array for the per-tile-component information. */
1970 if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) {
1973 /* Initialize a few members critical for error recovery. */
1974 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
1975 ++cmptno, ++tcmpt) {
1980 /* Initialize the per-tile-component information. */
1981 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
1982 ++cmptno, ++tcmpt) {
1983 if (!tcmpt_create(tcmpt, cp, image, tile)) {
1988 /* Initialize the synthesis weights for the MCT. */
1989 switch (tile->mctid) {
1991 tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0));
1992 tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875));
1993 tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875));
1996 tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000));
1997 tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584));
1998 tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755));
2002 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
2003 ++cmptno, ++tcmpt) {
2004 tcmpt->synweight = JPC_FIX_ONE;
2009 if (!(tile->pi = jpc_enc_pi_create(cp, tile))) {
2018 jpc_enc_tile_destroy(tile);
2023 void jpc_enc_tile_destroy(jpc_enc_tile_t *tile)
2025 jpc_enc_tcmpt_t *tcmpt;
2026 uint_fast16_t cmptno;
2029 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno <
2030 tile->numtcmpts; ++cmptno, ++tcmpt) {
2031 tcmpt_destroy(tcmpt);
2033 jas_free(tile->tcmpts);
2035 if (tile->lyrsizes) {
2036 jas_free(tile->lyrsizes);
2039 jpc_pi_destroy(tile->pi);
2044 static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
2045 jas_image_t *image, jpc_enc_tile_t *tile)
2047 uint_fast16_t cmptno;
2048 uint_fast16_t rlvlno;
2049 jpc_enc_rlvl_t *rlvl;
2054 uint_fast32_t cmpttlx;
2055 uint_fast32_t cmpttly;
2057 jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
2064 /* Deduce the component number. */
2065 cmptno = tcmpt - tile->tcmpts;
2067 ccp = &cp->ccps[cmptno];
2069 /* Compute the coordinates of the top-left and bottom-right
2070 corners of this tile-component. */
2071 tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx);
2072 tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy);
2073 brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx);
2074 bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy);
2076 /* Create a sequence to hold the tile-component sample data. */
2077 if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) {
2081 /* Get the image data associated with this tile-component. */
2082 cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx);
2083 cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy);
2084 if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly,
2085 brx - tlx, bry - tly, tcmpt->data)) {
2089 tcmpt->synweight = 0;
2090 tcmpt->qmfbid = cp->tccp.qmfbid;
2091 tcmpt->numrlvls = cp->tccp.maxrlvls;
2092 tcmpt->numbands = 3 * tcmpt->numrlvls - 2;
2093 if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) {
2097 for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) {
2098 tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno];
2099 tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno];
2101 tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn;
2102 tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn;
2103 tcmpt->cblksty = cp->tccp.cblksty;
2104 tcmpt->csty = cp->tccp.csty;
2106 tcmpt->numstepsizes = tcmpt->numbands;
2107 assert(tcmpt->numstepsizes <= JPC_MAXBANDS);
2108 memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes *
2109 sizeof(uint_fast16_t)));
2111 /* Retrieve information about the various bands. */
2112 jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data),
2113 jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data),
2114 jas_seq2d_yend(tcmpt->data), bandinfos);
2116 if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) {
2119 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2122 rlvl->tcmpt = tcmpt;
2124 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2126 if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) {
2135 tcmpt_destroy(tcmpt);
2140 static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt)
2142 jpc_enc_rlvl_t *rlvl;
2143 uint_fast16_t rlvlno;
2146 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2150 jas_free(tcmpt->rlvls);
2154 jas_seq2d_destroy(tcmpt->data);
2157 jpc_tsfb_destroy(tcmpt->tsfb);
2161 static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
2162 jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos)
2164 uint_fast16_t rlvlno;
2165 uint_fast32_t tlprctlx;
2166 uint_fast32_t tlprctly;
2167 uint_fast32_t brprcbrx;
2168 uint_fast32_t brprcbry;
2169 uint_fast16_t bandno;
2170 jpc_enc_band_t *band;
2172 /* Deduce the resolution level. */
2173 rlvlno = rlvl - tcmpt->rlvls;
2175 /* Initialize members required for error recovery. */
2177 rlvl->tcmpt = tcmpt;
2179 /* Compute the coordinates of the top-left and bottom-right
2180 corners of the tile-component at this resolution. */
2181 rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls -
2183 rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls -
2185 rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls -
2187 rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls -
2190 if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) {
2197 rlvl->numbands = (!rlvlno) ? 1 : 3;
2198 rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno];
2199 rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno];
2201 rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
2202 rlvl->cbgheightexpn = rlvl->prcheightexpn;
2204 rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
2205 rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
2207 rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn);
2208 rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn);
2210 /* Compute the number of precincts. */
2211 tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
2212 tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
2213 brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn);
2214 brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn);
2215 rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn);
2216 rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn);
2217 rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
2219 if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) {
2222 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2228 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2230 if (!band_create(band, cp, rlvl, bandinfos)) {
2242 static void rlvl_destroy(jpc_enc_rlvl_t *rlvl)
2244 jpc_enc_band_t *band;
2245 uint_fast16_t bandno;
2248 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2252 jas_free(rlvl->bands);
2256 static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
2257 jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos)
2259 uint_fast16_t bandno;
2260 uint_fast16_t gblbandno;
2261 uint_fast16_t rlvlno;
2262 jpc_tsfb_band_t *bandinfo;
2263 jpc_enc_tcmpt_t *tcmpt;
2264 uint_fast32_t prcno;
2267 tcmpt = rlvl->tcmpt;
2272 /* Deduce the resolution level and band number. */
2273 rlvlno = rlvl - rlvl->tcmpt->rlvls;
2274 bandno = band - rlvl->bands;
2275 gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1);
2277 bandinfo = &bandinfos[gblbandno];
2279 if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) {
2280 if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
2283 jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart,
2284 bandinfo->locystart, bandinfo->locxend, bandinfo->locyend);
2285 jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart);
2287 band->orient = bandinfo->orient;
2288 band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno,
2291 band->absstepsize = 0;
2293 band->synweight = bandinfo->synenergywt;
2296 if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) {
2299 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
2304 prc->savincltree = 0;
2305 prc->savnlibtree = 0;
2308 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
2310 if (!prc_create(prc, cp, band)) {
2323 static void band_destroy(jpc_enc_band_t *band)
2326 jpc_enc_rlvl_t *rlvl;
2327 uint_fast32_t prcno;
2331 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
2335 jas_free(band->prcs);
2338 jas_seq2d_destroy(band->data);
2342 static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band)
2344 uint_fast32_t prcno;
2345 uint_fast32_t prcxind;
2346 uint_fast32_t prcyind;
2347 uint_fast32_t cbgtlx;
2348 uint_fast32_t cbgtly;
2349 uint_fast32_t tlprctlx;
2350 uint_fast32_t tlprctly;
2351 uint_fast32_t tlcbgtlx;
2352 uint_fast32_t tlcbgtly;
2353 uint_fast16_t rlvlno;
2354 jpc_enc_rlvl_t *rlvl;
2355 uint_fast32_t tlcblktlx;
2356 uint_fast32_t tlcblktly;
2357 uint_fast32_t brcblkbrx;
2358 uint_fast32_t brcblkbry;
2359 uint_fast32_t cblkno;
2360 jpc_enc_cblk_t *cblk;
2361 jpc_enc_tcmpt_t *tcmpt;
2365 prc->savincltree = 0;
2367 prc->savnlibtree = 0;
2370 tcmpt = rlvl->tcmpt;
2371 rlvlno = rlvl - tcmpt->rlvls;
2372 prcno = prc - band->prcs;
2373 prcxind = prcno % rlvl->numhprcs;
2374 prcyind = prcno / rlvl->numhprcs;
2377 tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
2378 tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
2380 tlcbgtlx = tlprctlx;
2381 tlcbgtly = tlprctly;
2383 tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1);
2384 tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1);
2387 /* Compute the coordinates of the top-left and bottom-right
2388 corners of the precinct. */
2389 cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn);
2390 cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn);
2391 prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx);
2392 prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly);
2393 prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx +
2394 (1 << rlvl->cbgwidthexpn));
2395 prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly +
2396 (1 << rlvl->cbgheightexpn));
2398 if (prc->tlx < prc->brx && prc->tly < prc->bry) {
2399 /* The precinct contains at least one code block. */
2401 tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
2402 tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
2403 brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn);
2404 brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn);
2405 prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx,
2406 rlvl->cblkwidthexpn);
2407 prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly,
2408 rlvl->cblkheightexpn);
2409 prc->numcblks = prc->numhcblks * prc->numvcblks;
2411 if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks,
2415 if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks,
2419 if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks,
2423 if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks,
2428 if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) {
2431 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2440 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2442 if (!cblk_create(cblk, cp, prc)) {
2447 /* The precinct does not contain any code blocks. */
2448 prc->tlx = prc->brx;
2449 prc->tly = prc->bry;
2456 prc->savincltree = 0;
2457 prc->savnlibtree = 0;
2467 static void prc_destroy(jpc_enc_prc_t *prc)
2469 jpc_enc_cblk_t *cblk;
2470 uint_fast32_t cblkno;
2473 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2477 jas_free(prc->cblks);
2479 if (prc->incltree) {
2480 jpc_tagtree_destroy(prc->incltree);
2482 if (prc->nlibtree) {
2483 jpc_tagtree_destroy(prc->nlibtree);
2485 if (prc->savincltree) {
2486 jpc_tagtree_destroy(prc->savincltree);
2488 if (prc->savnlibtree) {
2489 jpc_tagtree_destroy(prc->savnlibtree);
2493 static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc)
2495 jpc_enc_band_t *band;
2496 uint_fast32_t cblktlx;
2497 uint_fast32_t cblktly;
2498 uint_fast32_t cblkbrx;
2499 uint_fast32_t cblkbry;
2500 jpc_enc_rlvl_t *rlvl;
2501 uint_fast32_t cblkxind;
2502 uint_fast32_t cblkyind;
2503 uint_fast32_t cblkno;
2504 uint_fast32_t tlcblktlx;
2505 uint_fast32_t tlcblktly;
2507 cblkno = cblk - prc->cblks;
2508 cblkxind = cblkno % prc->numhcblks;
2509 cblkyind = cblkno / prc->numhcblks;
2510 rlvl = prc->band->rlvl;
2513 cblk->numpasses = 0;
2515 cblk->numencpasses = 0;
2517 cblk->numlenbits = 0;
2524 cblk->savedcurpass = 0;
2525 cblk->savednumlenbits = 0;
2526 cblk->savednumencpasses = 0;
2529 tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
2530 tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
2531 cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx);
2532 cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly);
2533 cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn),
2535 cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn),
2538 assert(cblktlx < cblkbrx && cblktly < cblkbry);
2539 if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) {
2542 jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry);
2551 static void cblk_destroy(jpc_enc_cblk_t *cblk)
2553 uint_fast16_t passno;
2554 jpc_enc_pass_t *pass;
2556 for (passno = 0, pass = cblk->passes; passno < cblk->numpasses;
2560 jas_free(cblk->passes);
2563 jas_stream_close(cblk->stream);
2566 jpc_mqenc_destroy(cblk->mqenc);
2569 jas_seq2d_destroy(cblk->data);
2572 jas_seq2d_destroy(cblk->flags);
2576 static void pass_destroy(jpc_enc_pass_t *pass)
2578 /* XXX - need to free resources here */
2581 void jpc_enc_dump(jpc_enc_t *enc)
2583 jpc_enc_tile_t *tile;
2584 jpc_enc_tcmpt_t *tcmpt;
2585 jpc_enc_rlvl_t *rlvl;
2586 jpc_enc_band_t *band;
2588 jpc_enc_cblk_t *cblk;
2589 uint_fast16_t cmptno;
2590 uint_fast16_t rlvlno;
2591 uint_fast16_t bandno;
2592 uint_fast32_t prcno;
2593 uint_fast32_t cblkno;
2595 tile = enc->curtile;
2597 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno,
2599 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));
2600 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2602 fprintf(stderr, " rlvl %5d %5d %5d %5d\n", rlvl->tlx, rlvl->tly, rlvl->brx, rlvl->bry);
2603 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2608 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));
2609 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
2611 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);
2615 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2617 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));