2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2002 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.h,v 1.1 2005/05/22 18:33:04 malaterre Exp $
71 /******************************************************************************\
73 \******************************************************************************/
75 #include "jasper/jas_seq.h"
77 #include "jpc_t2cod.h"
78 #include "jpc_mqenc.h"
80 #include "jpc_tagtree.h"
85 /******************************************************************************\
87 \******************************************************************************/
89 /* The number of bits used in various lookup tables. */
90 #define JPC_NUMEXTRABITS JPC_NMSEDEC_FRACBITS
92 /* An invalid R-D slope value. */
93 #define JPC_BADRDSLOPE (-1)
95 /******************************************************************************\
96 * Coding parameters types.
97 \******************************************************************************/
99 /* Per-component coding paramters. */
103 /* The horizontal sampling period. */
104 uint_fast8_t sampgrdstepx;
106 /* The vertical sampling period. */
107 uint_fast8_t sampgrdstepy;
109 /* The sample alignment horizontal offset. */
110 uint_fast8_t sampgrdsubstepx;
112 /* The sample alignment vertical offset. */
113 uint_fast8_t sampgrdsubstepy;
115 /* The precision of the samples. */
118 /* The signedness of the samples. */
121 /* The number of step sizes. */
122 uint_fast16_t numstepsizes;
124 /* The quantizer step sizes. */
125 uint_fast16_t stepsizes[JPC_MAXBANDS];
129 /* Per-tile coding parameters. */
133 /* The coding mode. */
136 /* The coding style (i.e., SOP, EPH). */
139 /* The progression order. */
142 /* The multicomponent transform. */
145 /* The number of layers. */
146 uint_fast16_t numlyrs;
148 /* The normalized bit rates associated with the various
149 intermediate layers. */
150 jpc_fix_t *ilyrrates;
154 /* Per tile-component coding parameters. */
158 /* The coding style (i.e., explicit precinct sizes). */
161 /* The maximum number of resolution levels allowed. */
162 uint_fast8_t maxrlvls;
164 /* The exponent for the nominal code block width. */
165 uint_fast16_t cblkwidthexpn;
167 /* The exponent for the nominal code block height. */
168 uint_fast16_t cblkheightexpn;
170 /* The code block style parameters (e.g., lazy, terminate all,
171 segmentation symbols, causal, reset probability models). */
172 uint_fast8_t cblksty;
177 /* The precinct width values. */
178 uint_fast16_t prcwidthexpns[JPC_MAXRLVLS];
180 /* The precinct height values. */
181 uint_fast16_t prcheightexpns[JPC_MAXRLVLS];
183 /* The number of guard bits. */
184 uint_fast8_t numgbits;
188 /* Coding parameters. */
192 /* The debug level. */
195 /* The horizontal offset from the origin of the reference grid to the
196 left edge of the image area. */
197 uint_fast32_t imgareatlx;
199 /* The vertical offset from the origin of the reference grid to the
200 top edge of the image area. */
201 uint_fast32_t imgareatly;
203 /* The horizontal offset from the origin of the reference grid to the
204 right edge of the image area (plus one). */
205 uint_fast32_t refgrdwidth;
207 /* The vertical offset from the origin of the reference grid to the
208 bottom edge of the image area (plus one). */
209 uint_fast32_t refgrdheight;
211 /* The horizontal offset from the origin of the tile grid to the
212 origin of the reference grid. */
213 uint_fast32_t tilegrdoffx;
215 /* The vertical offset from the origin of the tile grid to the
216 origin of the reference grid. */
217 uint_fast32_t tilegrdoffy;
219 /* The nominal tile width in units of the image reference grid. */
220 uint_fast32_t tilewidth;
222 /* The nominal tile height in units of the image reference grid. */
223 uint_fast32_t tileheight;
225 /* The number of tiles spanning the image area in the horizontal
227 uint_fast32_t numhtiles;
229 /* The number of tiles spanning the image area in the vertical
231 uint_fast32_t numvtiles;
233 /* The number of tiles. */
234 uint_fast32_t numtiles;
236 /* The number of components. */
237 uint_fast16_t numcmpts;
239 /* The per-component coding parameters. */
242 /* The per-tile coding parameters. */
245 /* The per-tile-component coding parameters. */
248 /* The target code stream length in bytes. */
249 uint_fast32_t totalsize;
251 /* The raw (i.e., uncompressed) size of the image in bytes. */
252 uint_fast32_t rawsize;
256 /******************************************************************************\
258 \******************************************************************************/
260 /* Encoder per-coding-pass state information. */
264 /* The starting offset for this pass. */
267 /* The ending offset for this pass. */
270 /* The type of data in this pass (i.e., MQ or raw). */
273 /* Flag indicating that this pass is terminated. */
276 /* The entropy coder state after coding this pass. */
277 jpc_mqencstate_t mqencstate;
279 /* The layer to which this pass has been assigned. */
282 /* The R-D slope for this pass. */
285 /* The weighted MSE reduction associated with this pass. */
288 /* The cumulative weighted MSE reduction. */
289 jpc_flt_t cumwmsedec;
291 /* The normalized MSE reduction. */
296 /* Encoder per-code-block state information. */
300 /* The number of passes. */
303 /* The per-pass information. */
304 jpc_enc_pass_t *passes;
306 /* The number of passes encoded so far. */
309 /* The number of insignificant MSBs. */
312 /* The number of bits used to encode pass data lengths. */
315 /* The byte stream for this code block. */
316 jas_stream_t *stream;
318 /* The entropy encoder. */
321 /* The data for this code block. */
324 /* The state for this code block. */
327 /* The number of bit planes required for this code block. */
330 /* The next pass to be encoded. */
331 jpc_enc_pass_t *curpass;
333 /* The per-code-block-group state information. */
334 struct jpc_enc_prc_s *prc;
336 /* The saved current pass. */
337 /* This is used by the rate control code. */
338 jpc_enc_pass_t *savedcurpass;
340 /* The saved length indicator size. */
341 /* This is used by the rate control code. */
344 /* The saved number of encoded passes. */
345 /* This is used by the rate control code. */
346 int savednumencpasses;
350 /* Encoder per-code-block-group state information. */
352 typedef struct jpc_enc_prc_s {
354 /* The x-coordinate of the top-left corner of the precinct. */
357 /* The y-coordinate of the top-left corner of the precinct. */
360 /* The x-coordinate of the bottom-right corner of the precinct
364 /* The y-coordinate of the bottom-right corner of the precinct
368 /* The number of code blocks spanning the precinct in the horizontal
372 /* The number of code blocks spanning the precinct in the vertical
376 /* The total number of code blocks. */
379 /* The per-code-block information. */
380 jpc_enc_cblk_t *cblks;
382 /* The inclusion tag tree. */
383 jpc_tagtree_t *incltree;
385 /* The insignifcant MSBs tag tree. */
386 jpc_tagtree_t *nlibtree;
388 /* The per-band information. */
389 struct jpc_enc_band_s *band;
391 /* The saved inclusion tag tree. */
392 /* This is used by rate control. */
393 jpc_tagtree_t *savincltree;
395 /* The saved leading-insignificant-bit-planes tag tree. */
396 /* This is used by rate control. */
397 jpc_tagtree_t *savnlibtree;
401 /* Encoder per-band state information. */
403 typedef struct jpc_enc_band_s {
405 /* The per precinct information. */
408 /* The coefficient data for this band. */
411 /* The orientation of this band (i.e., LL, LH, HL, or HH). */
414 /* The number of bit planes associated with this band. */
417 /* The quantizer step size. */
418 jpc_fix_t absstepsize;
420 /* The encoded quantizer step size. */
423 /* The L2 norm of the synthesis basis functions associated with
424 this band. (The MCT is not considered in this value.) */
427 /* The analysis gain for this band. */
430 /* The per-resolution-level information. */
431 struct jpc_enc_rlvl_s *rlvl;
435 /* Encoder per-resolution-level state information. */
437 typedef struct jpc_enc_rlvl_s {
439 /* The x-coordinate of the top-left corner of the tile-component
440 at this resolution. */
443 /* The y-coordinate of the top-left corner of the tile-component
444 at this resolution. */
447 /* The x-coordinate of the bottom-right corner of the tile-component
448 at this resolution (plus one). */
451 /* The y-coordinate of the bottom-right corner of the tile-component
452 at this resolution (plus one). */
455 /* The exponent value for the nominal precinct width measured
456 relative to the associated LL band. */
459 /* The exponent value for the nominal precinct height measured
460 relative to the associated LL band. */
463 /* The number of precincts spanning the resolution level in the
464 horizontal direction. */
467 /* The number of precincts spanning the resolution level in the
468 vertical direction. */
471 /* The total number of precincts. */
474 /* The exponent value for the nominal code block group width.
475 This quantity is associated with the next lower resolution level
476 (assuming that there is one). */
479 /* The exponent value for the nominal code block group height.
480 This quantity is associated with the next lower resolution level
481 (assuming that there is one). */
484 /* The exponent value for the code block width. */
485 uint_fast16_t cblkwidthexpn;
487 /* The exponent value for the code block height. */
488 uint_fast16_t cblkheightexpn;
490 /* The number of bands associated with this resolution level. */
493 /* The per-band information. */
494 jpc_enc_band_t *bands;
496 /* The parent tile-component. */
497 struct jpc_enc_tcmpt_s *tcmpt;
501 /* Encoder per-tile-component state information. */
503 typedef struct jpc_enc_tcmpt_s {
505 /* The number of resolution levels. */
508 /* The per-resolution-level information. */
509 jpc_enc_rlvl_t *rlvls;
511 /* The tile-component data. */
517 /* The number of bands. */
523 /* The synthesis energy weight (for the MCT). */
526 /* The precinct width exponents. */
527 int prcwidthexpns[JPC_MAXRLVLS];
529 /* The precinct height exponents. */
530 int prcheightexpns[JPC_MAXRLVLS];
532 /* The code block width exponent. */
535 /* The code block height exponent. */
538 /* Coding style (i.e., explicit precinct sizes). */
541 /* Code block style. */
544 /* The number of quantizer step sizes. */
547 /* The encoded quantizer step sizes. */
548 uint_fast16_t stepsizes[JPC_MAXBANDS];
550 /* The parent tile. */
551 struct jpc_enc_tile_s *tile;
555 /* Encoder per-tile state information. */
557 typedef struct jpc_enc_tile_s {
559 /* The tile number. */
560 uint_fast32_t tileno;
562 /* The x-coordinate of the top-left corner of the tile measured with
563 respect to the reference grid. */
566 /* The y-coordinate of the top-left corner of the tile measured with
567 respect to the reference grid. */
570 /* The x-coordinate of the bottom-right corner of the tile measured
571 with respect to the reference grid (plus one). */
574 /* The y-coordinate of the bottom-right corner of the tile measured
575 with respect to the reference grid (plus one). */
578 /* The coding style. */
581 /* The progression order. */
584 /* The number of layers. */
587 /* The MCT to employ (if any). */
590 /* The packet iterator (used to determine the order of packet
594 /* The coding mode (i.e., integer or real). */
597 /* The number of bytes to allocate to the various layers. */
598 uint_fast32_t *lyrsizes;
600 /* The number of tile-components. */
603 /* The per tile-component information. */
604 jpc_enc_tcmpt_t *tcmpts;
606 /* The raw (i.e., uncompressed) size of this tile. */
607 uint_fast32_t rawsize;
613 typedef struct jpc_enc_s {
615 /* The image being encoded. */
618 /* The output stream. */
621 /* The coding parameters. */
624 /* The tile currently being processed. */
625 jpc_enc_tile_t *curtile;
627 /* The code stream state. */
628 jpc_cstate_t *cstate;
630 /* The number of bytes output so far. */
633 /* The number of bytes available for the main body of the code stream. */
634 /* This is used for rate allocation purposes. */
635 uint_fast32_t mainbodysize;
637 /* The marker segment currently being processed. */
638 /* This member is a convenience for making cleanup easier. */
641 /* The stream used to temporarily hold tile-part data. */
642 jas_stream_t *tmpstream;