My Project: jpc_enc.h Source File

My Project 2.0.19 |

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