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