JasPer  2.0.28
jpc_enc.h
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  * British Columbia.
4  * Copyright (c) 2001-2002 Michael David Adams.
5  * All rights reserved.
6  */
7 
8 /* __START_OF_JASPER_LICENSE__
9  *
10  * JasPer License Version 2.0
11  *
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  *
16  * All rights reserved.
17  *
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:
25  *
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.
29  *
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
32  * written permission.
33  *
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.
60  *
61  * __END_OF_JASPER_LICENSE__
62  */
63 
64 /*
65  * $Id$
66  */
67 
68 #ifndef JPC_ENC_H
69 #define JPC_ENC_H
70 
71 /******************************************************************************\
72 * Includes.
73 \******************************************************************************/
74 
75 #include "jasper/jas_image.h"
76 #include "jasper/jas_seq.h"
77 #include "jasper/jas_stream.h"
78 
79 #include "jpc_t1cod.h"
80 #include "jpc_t2cod.h"
81 #include "jpc_mqenc.h"
82 #include "jpc_tagtree.h"
83 #include "jpc_cs.h"
84 #include "jpc_fix.h"
85 #include "jpc_flt.h"
86 #include "jpc_tsfb.h"
87 
88 /******************************************************************************\
89 * Constants.
90 \******************************************************************************/
91 
92 /* The number of bits used in various lookup tables. */
93 #define JPC_NUMEXTRABITS JPC_NMSEDEC_FRACBITS
94 
95 /* An invalid R-D slope value. */
96 #define JPC_BADRDSLOPE (-1)
97 
98 /******************************************************************************\
99 * Coding parameters types.
100 \******************************************************************************/
101 
102 /* Per-component coding paramters. */
103 
104 typedef struct {
105 
106  /* The horizontal sampling period. */
107  uint_fast8_t sampgrdstepx;
108 
109  /* The vertical sampling period. */
110  uint_fast8_t sampgrdstepy;
111 
112  /* The sample alignment horizontal offset. */
113  uint_fast8_t sampgrdsubstepx;
114 
115  /* The sample alignment vertical offset. */
116  uint_fast8_t sampgrdsubstepy;
117 
118  /* The precision of the samples. */
119  uint_fast8_t prec;
120 
121  /* The signedness of the samples. */
122  bool sgnd;
123 
124  /* The number of step sizes. */
125  uint_fast16_t numstepsizes;
126 
127  /* The quantizer step sizes. */
128  uint_fast16_t stepsizes[JPC_MAXBANDS];
129 
130 } jpc_enc_ccp_t;
131 
132 /* Per-tile coding parameters. */
133 
134 typedef struct {
135 
136  /* The coding mode. */
137  bool intmode;
138 
139  /* The coding style (i.e., SOP, EPH). */
140  uint_fast8_t csty;
141 
142  /* The progression order. */
143  uint_fast8_t prg;
144 
145  /* The multicomponent transform. */
146  uint_fast8_t mctid;
147 
148  /* The number of layers. */
149  uint_fast16_t numlyrs;
150 
151  /* The normalized bit rates associated with the various
152  intermediate layers. */
153  jpc_fix_t *ilyrrates;
154 
155 } jpc_enc_tcp_t;
156 
157 /* Per tile-component coding parameters. */
158 
159 typedef struct {
160 
161  /* The coding style (i.e., explicit precinct sizes). */
162  uint_fast8_t csty;
163 
164  /* The maximum number of resolution levels allowed. */
165  uint_fast8_t maxrlvls;
166 
167  /* The exponent for the nominal code block width. */
168  uint_fast16_t cblkwidthexpn;
169 
170  /* The exponent for the nominal code block height. */
171  uint_fast16_t cblkheightexpn;
172 
173  /* The code block style parameters (e.g., lazy, terminate all,
174  segmentation symbols, causal, reset probability models). */
175  uint_fast8_t cblksty;
176 
177  /* The QMFB. */
178  uint_fast8_t qmfbid;
179 
180  /* The precinct width values. */
181  uint_fast16_t prcwidthexpns[JPC_MAXRLVLS];
182 
183  /* The precinct height values. */
184  uint_fast16_t prcheightexpns[JPC_MAXRLVLS];
185 
186  /* The number of guard bits. */
187  uint_fast8_t numgbits;
188 
189 } jpc_enc_tccp_t;
190 
191 /* Coding parameters. */
192 
193 typedef struct {
194 
195  /* The debug level. */
196  int debug;
197 
198  /* The horizontal offset from the origin of the reference grid to the
199  left edge of the image area. */
200  uint_fast32_t imgareatlx;
201 
202  /* The vertical offset from the origin of the reference grid to the
203  top edge of the image area. */
204  uint_fast32_t imgareatly;
205 
206  /* The horizontal offset from the origin of the reference grid to the
207  right edge of the image area (plus one). */
208  uint_fast32_t refgrdwidth;
209 
210  /* The vertical offset from the origin of the reference grid to the
211  bottom edge of the image area (plus one). */
212  uint_fast32_t refgrdheight;
213 
214  /* The horizontal offset from the origin of the tile grid to the
215  origin of the reference grid. */
216  uint_fast32_t tilegrdoffx;
217 
218  /* The vertical offset from the origin of the tile grid to the
219  origin of the reference grid. */
220  uint_fast32_t tilegrdoffy;
221 
222  /* The nominal tile width in units of the image reference grid. */
223  uint_fast32_t tilewidth;
224 
225  /* The nominal tile height in units of the image reference grid. */
226  uint_fast32_t tileheight;
227 
228  /* The number of tiles spanning the image area in the horizontal
229  direction. */
230  uint_fast32_t numhtiles;
231 
232  /* The number of tiles spanning the image area in the vertical
233  direction. */
234  uint_fast32_t numvtiles;
235 
236  /* The number of tiles. */
237  uint_fast32_t numtiles;
238 
239  /* The number of components. */
240  uint_fast16_t numcmpts;
241 
242  /* The per-component coding parameters. */
243  jpc_enc_ccp_t *ccps;
244 
245  /* The per-tile coding parameters. */
246  jpc_enc_tcp_t tcp;
247 
248  /* The per-tile-component coding parameters. */
249  jpc_enc_tccp_t tccp;
250 
251  /* The target code stream length in bytes. */
252  uint_fast32_t totalsize;
253 
254  /* The raw (i.e., uncompressed) size of the image in bytes. */
255  uint_fast32_t rawsize;
256 
257 } jpc_enc_cp_t;
258 
259 /******************************************************************************\
260 * Encoder class.
261 \******************************************************************************/
262 
263 /* Encoder per-coding-pass state information. */
264 
265 typedef struct {
266 
267  /* The starting offset for this pass. */
268  int start;
269 
270  /* The ending offset for this pass. */
271  int end;
272 
273  /* The type of data in this pass (i.e., MQ or raw). */
274  enum jpc_segtype type;
275 
276  /* Flag indicating that this pass is terminated. */
277  int term;
278 
279  /* The entropy coder state after coding this pass. */
280  jpc_mqencstate_t mqencstate;
281 
282  /* The layer to which this pass has been assigned. */
283  unsigned lyrno;
284 
285  /* The R-D slope for this pass. */
286  jpc_flt_t rdslope;
287 
288  /* The weighted MSE reduction associated with this pass. */
289  jpc_flt_t wmsedec;
290 
291  /* The cumulative weighted MSE reduction. */
292  jpc_flt_t cumwmsedec;
293 
294  /* The normalized MSE reduction. */
295  long nmsedec;
296 
297 } jpc_enc_pass_t;
298 
299 /* Encoder per-code-block state information. */
300 
301 typedef struct {
302 
303  /* The number of passes. */
304  unsigned numpasses;
305 
306  /* The per-pass information. */
307  jpc_enc_pass_t *passes;
308 
309  /* The number of passes encoded so far. */
310  int numencpasses;
311 
312  /* The number of insignificant MSBs. */
313  int numimsbs;
314 
315  /* The number of bits used to encode pass data lengths. */
316  int numlenbits;
317 
318  /* The byte stream for this code block. */
319  jas_stream_t *stream;
320 
321  /* The entropy encoder. */
322  jpc_mqenc_t *mqenc;
323 
324  /* The data for this code block. */
325  jas_matrix_t *data;
326 
327  /* The state for this code block. */
328  jas_matrix_t *flags;
329 
330  /* The number of bit planes required for this code block. */
331  int numbps;
332 
333  /* The next pass to be encoded. */
334  jpc_enc_pass_t *curpass;
335 
336  /* The per-code-block-group state information. */
337  struct jpc_enc_prc_s *prc;
338 
339  /* The saved current pass. */
340  /* This is used by the rate control code. */
341  jpc_enc_pass_t *savedcurpass;
342 
343  /* The saved length indicator size. */
344  /* This is used by the rate control code. */
345  int savednumlenbits;
346 
347  /* The saved number of encoded passes. */
348  /* This is used by the rate control code. */
349  int savednumencpasses;
350 
351 } jpc_enc_cblk_t;
352 
353 /* Encoder per-code-block-group state information. */
354 
355 typedef struct jpc_enc_prc_s {
356 
357  /* The x-coordinate of the top-left corner of the precinct. */
358  uint_fast32_t tlx;
359 
360  /* The y-coordinate of the top-left corner of the precinct. */
361  uint_fast32_t tly;
362 
363  /* The x-coordinate of the bottom-right corner of the precinct
364  (plus one). */
365  uint_fast32_t brx;
366 
367  /* The y-coordinate of the bottom-right corner of the precinct
368  (plus one). */
369  uint_fast32_t bry;
370 
371  /* The number of code blocks spanning the precinct in the horizontal
372  direction. */
373  int numhcblks;
374 
375  /* The number of code blocks spanning the precinct in the vertical
376  direction. */
377  int numvcblks;
378 
379  /* The total number of code blocks. */
380  unsigned numcblks;
381 
382  /* The per-code-block information. */
383  jpc_enc_cblk_t *cblks;
384 
385  /* The inclusion tag tree. */
386  jpc_tagtree_t *incltree;
387 
388  /* The insignifcant MSBs tag tree. */
389  jpc_tagtree_t *nlibtree;
390 
391  /* The per-band information. */
392  struct jpc_enc_band_s *band;
393 
394  /* The saved inclusion tag tree. */
395  /* This is used by rate control. */
396  jpc_tagtree_t *savincltree;
397 
398  /* The saved leading-insignificant-bit-planes tag tree. */
399  /* This is used by rate control. */
400  jpc_tagtree_t *savnlibtree;
401 
402 } jpc_enc_prc_t;
403 
404 /* Encoder per-band state information. */
405 
406 typedef struct jpc_enc_band_s {
407 
408  /* The per precinct information. */
409  jpc_enc_prc_t *prcs;
410 
411  /* The coefficient data for this band. */
412  jas_matrix_t *data;
413 
414  /* The orientation of this band (i.e., LL, LH, HL, or HH). */
415  enum jpc_tsfb_orient orient;
416 
417  /* The number of bit planes associated with this band. */
418  int numbps;
419 
420  /* The quantizer step size. */
421  jpc_fix_t absstepsize;
422 
423  /* The encoded quantizer step size. */
424  int stepsize;
425 
426  /* The L2 norm of the synthesis basis functions associated with
427  this band. (The MCT is not considered in this value.) */
428  jpc_fix_t synweight;
429 
430  /* The analysis gain for this band. */
431  int analgain;
432 
433  /* The per-resolution-level information. */
434  struct jpc_enc_rlvl_s *rlvl;
435 
436 } jpc_enc_band_t;
437 
438 /* Encoder per-resolution-level state information. */
439 
440 typedef struct jpc_enc_rlvl_s {
441 
442  /* The x-coordinate of the top-left corner of the tile-component
443  at this resolution. */
444  uint_fast32_t tlx;
445 
446  /* The y-coordinate of the top-left corner of the tile-component
447  at this resolution. */
448  uint_fast32_t tly;
449 
450  /* The x-coordinate of the bottom-right corner of the tile-component
451  at this resolution (plus one). */
452  uint_fast32_t brx;
453 
454  /* The y-coordinate of the bottom-right corner of the tile-component
455  at this resolution (plus one). */
456  uint_fast32_t bry;
457 
458  /* The exponent value for the nominal precinct width measured
459  relative to the associated LL band. */
460  int prcwidthexpn;
461 
462  /* The exponent value for the nominal precinct height measured
463  relative to the associated LL band. */
464  int prcheightexpn;
465 
466  /* The number of precincts spanning the resolution level in the
467  horizontal direction. */
468  int numhprcs;
469 
470  /* The number of precincts spanning the resolution level in the
471  vertical direction. */
472  int numvprcs;
473 
474  /* The total number of precincts. */
475  unsigned numprcs;
476 
477  /* The exponent value for the nominal code block group width.
478  This quantity is associated with the next lower resolution level
479  (assuming that there is one). */
480  unsigned cbgwidthexpn;
481 
482  /* The exponent value for the nominal code block group height.
483  This quantity is associated with the next lower resolution level
484  (assuming that there is one). */
485  unsigned cbgheightexpn;
486 
487  /* The exponent value for the code block width. */
488  uint_fast16_t cblkwidthexpn;
489 
490  /* The exponent value for the code block height. */
491  uint_fast16_t cblkheightexpn;
492 
493  /* The number of bands associated with this resolution level. */
494  unsigned numbands;
495 
496  /* The per-band information. */
497  jpc_enc_band_t *bands;
498 
499  /* The parent tile-component. */
500  struct jpc_enc_tcmpt_s *tcmpt;
501 
502 } jpc_enc_rlvl_t;
503 
504 /* Encoder per-tile-component state information. */
505 
506 typedef struct jpc_enc_tcmpt_s {
507 
508  /* The number of resolution levels. */
509  unsigned numrlvls;
510 
511  /* The per-resolution-level information. */
512  jpc_enc_rlvl_t *rlvls;
513 
514  /* The tile-component data. */
515  jas_matrix_t *data;
516 
517  /* The QMFB. */
518  int qmfbid;
519 
520  /* The number of bands. */
521  int numbands;
522 
523  /* The TSFB. */
524  jpc_tsfb_t *tsfb;
525 
526  /* The synthesis energy weight (for the MCT). */
527  jpc_fix_t synweight;
528 
529  /* The precinct width exponents. */
530  int prcwidthexpns[JPC_MAXRLVLS];
531 
532  /* The precinct height exponents. */
533  int prcheightexpns[JPC_MAXRLVLS];
534 
535  /* The code block width exponent. */
536  int cblkwidthexpn;
537 
538  /* The code block height exponent. */
539  int cblkheightexpn;
540 
541  /* Coding style (i.e., explicit precinct sizes). */
542  int csty;
543 
544  /* Code block style. */
545  int cblksty;
546 
547  /* The number of quantizer step sizes. */
548  int numstepsizes;
549 
550  /* The encoded quantizer step sizes. */
551  uint_fast16_t stepsizes[JPC_MAXBANDS];
552 
553  /* The parent tile. */
554  struct jpc_enc_tile_s *tile;
555 
556 } jpc_enc_tcmpt_t;
557 
558 /* Encoder per-tile state information. */
559 
560 typedef struct jpc_enc_tile_s {
561 
562  /* The tile number. */
563  uint_fast32_t tileno;
564 
565  /* The x-coordinate of the top-left corner of the tile measured with
566  respect to the reference grid. */
567  uint_fast32_t tlx;
568 
569  /* The y-coordinate of the top-left corner of the tile measured with
570  respect to the reference grid. */
571  uint_fast32_t tly;
572 
573  /* The x-coordinate of the bottom-right corner of the tile measured
574  with respect to the reference grid (plus one). */
575  uint_fast32_t brx;
576 
577  /* The y-coordinate of the bottom-right corner of the tile measured
578  with respect to the reference grid (plus one). */
579  uint_fast32_t bry;
580 
581  /* The coding style. */
582  uint_fast8_t csty;
583 
584  /* The progression order. */
585  uint_fast8_t prg;
586 
587  /* The number of layers. */
588  unsigned numlyrs;
589 
590  /* The MCT to employ (if any). */
591  uint_fast8_t mctid;
592 
593  /* The packet iterator (used to determine the order of packet
594  generation). */
595  jpc_pi_t *pi;
596 
597  /* The coding mode (i.e., integer or real). */
598  bool intmode;
599 
600  /* The number of bytes to allocate to the various layers. */
601  uint_fast32_t *lyrsizes;
602 
603  /* The number of tile-components. */
604  unsigned numtcmpts;
605 
606  /* The per tile-component information. */
607  jpc_enc_tcmpt_t *tcmpts;
608 
609  /* The raw (i.e., uncompressed) size of this tile. */
610  uint_fast32_t rawsize;
611 
612 } jpc_enc_tile_t;
613 
614 /* Encoder class. */
615 
616 typedef struct jpc_enc_s {
617 
618  /* The image being encoded. */
619  jas_image_t *image;
620 
621  /* The output stream. */
622  jas_stream_t *out;
623 
624  /* The coding parameters. */
625  jpc_enc_cp_t *cp;
626 
627  /* The tile currently being processed. */
628  jpc_enc_tile_t *curtile;
629 
630  /* The code stream state. */
631  jpc_cstate_t *cstate;
632 
633  /* The number of bytes output so far. */
634  uint_fast32_t len;
635 
636  /* The number of bytes available for the main body of the code stream. */
637  /* This is used for rate allocation purposes. */
638  uint_fast32_t mainbodysize;
639 
640  /* The marker segment currently being processed. */
641  /* This member is a convenience for making cleanup easier. */
642  jpc_ms_t *mrk;
643 
644  /* The stream used to temporarily hold tile-part data. */
645  jas_stream_t *tmpstream;
646 
647 } jpc_enc_t;
648 
649 #endif
JasPer Image Class.
Sequence/Matrix Library.
I/O Stream Class.
Image class.
Definition: jas_image.h:202