jpc_enc.h Source File

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