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 Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A.  Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B.  Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C.  Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D.  User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E.  If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F.  This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113/*
114 * $Id: jpc_enc.h 14449 2005-10-20 12:15:56Z stippi $
115 */
116
117#ifndef JPC_ENC_H
118#define JPC_ENC_H
119
120/******************************************************************************\
121* Includes.
122\******************************************************************************/
123
124#include "jasper/jas_seq.h"
125
126#include "jpc_t2cod.h"
127#include "jpc_mqenc.h"
128#include "jpc_cod.h"
129#include "jpc_tagtree.h"
130#include "jpc_cs.h"
131#include "jpc_flt.h"
132#include "jpc_tsfb.h"
133
134/******************************************************************************\
135* Constants.
136\******************************************************************************/
137
138/* The number of bits used in various lookup tables. */
139#define	JPC_NUMEXTRABITS	JPC_NMSEDEC_FRACBITS
140
141/* An invalid R-D slope value. */
142#define	JPC_BADRDSLOPE	(-1)
143
144/******************************************************************************\
145* Coding parameters types.
146\******************************************************************************/
147
148/* Per-component coding paramters. */
149
150typedef struct {
151
152	/* The horizontal sampling period. */
153	uint_fast8_t sampgrdstepx;
154
155	/* The vertical sampling period. */
156	uint_fast8_t sampgrdstepy;
157
158	/* The sample alignment horizontal offset. */
159	uint_fast8_t sampgrdsubstepx;
160
161	/* The sample alignment vertical offset. */
162	uint_fast8_t sampgrdsubstepy;
163
164	/* The precision of the samples. */
165	uint_fast8_t prec;
166
167	/* The signedness of the samples. */
168	JPR_BOOL sgnd;
169
170	/* The number of step sizes. */
171	uint_fast16_t numstepsizes;
172
173	/* The quantizer step sizes. */
174	uint_fast16_t stepsizes[JPC_MAXBANDS];
175
176} jpc_enc_ccp_t;
177
178/* Per-tile coding parameters. */
179
180typedef struct {
181
182	/* The coding mode. */
183	JPR_BOOL intmode;
184
185	/* The coding style (i.e., SOP, EPH). */
186	uint_fast8_t csty;
187
188	/* The progression order. */
189	uint_fast8_t prg;
190
191	/* The multicomponent transform. */
192	uint_fast8_t mctid;
193
194	/* The number of layers. */
195	uint_fast16_t numlyrs;
196
197	/* The normalized bit rates associated with the various
198	  intermediate layers. */
199	jpc_fix_t *ilyrrates;
200
201} jpc_enc_tcp_t;
202
203/* Per tile-component coding parameters. */
204
205typedef struct {
206
207	/* The coding style (i.e., explicit precinct sizes). */
208	uint_fast8_t csty;
209
210	/* The maximum number of resolution levels allowed. */
211	uint_fast8_t maxrlvls;
212
213	/* The exponent for the nominal code block width. */
214	uint_fast16_t cblkwidthexpn;
215
216	/* The exponent for the nominal code block height. */
217	uint_fast16_t cblkheightexpn;
218
219	/* The code block style parameters (e.g., lazy, terminate all,
220	  segmentation symbols, causal, reset probability models). */
221	uint_fast8_t cblksty;
222
223	/* The QMFB. */
224	uint_fast8_t qmfbid;
225
226	/* The precinct width values. */
227	uint_fast16_t prcwidthexpns[JPC_MAXRLVLS];
228
229	/* The precinct height values. */
230	uint_fast16_t prcheightexpns[JPC_MAXRLVLS];
231
232	/* The number of guard bits. */
233	uint_fast8_t numgbits;
234
235} jpc_enc_tccp_t;
236
237/* Coding parameters. */
238
239typedef struct {
240
241	/* The debug level. */
242	int debug;
243
244	/* The horizontal offset from the origin of the reference grid to the
245	  left edge of the image area. */
246	uint_fast32_t imgareatlx;
247
248	/* The vertical offset from the origin of the reference grid to the
249	  top edge of the image area. */
250	uint_fast32_t imgareatly;
251
252	/* The horizontal offset from the origin of the reference grid to the
253	  right edge of the image area (plus one). */
254	uint_fast32_t refgrdwidth;
255
256	/* The vertical offset from the origin of the reference grid to the
257	  bottom edge of the image area (plus one). */
258	uint_fast32_t refgrdheight;
259
260	/* The horizontal offset from the origin of the tile grid to the
261	  origin of the reference grid. */
262	uint_fast32_t tilegrdoffx;
263
264	/* The vertical offset from the origin of the tile grid to the
265	  origin of the reference grid. */
266	uint_fast32_t tilegrdoffy;
267
268	/* The nominal tile width in units of the image reference grid. */
269	uint_fast32_t tilewidth;
270
271	/* The nominal tile height in units of the image reference grid. */
272	uint_fast32_t tileheight;
273
274	/* The number of tiles spanning the image area in the horizontal
275	  direction. */
276	uint_fast32_t numhtiles;
277
278	/* The number of tiles spanning the image area in the vertical
279	  direction. */
280	uint_fast32_t numvtiles;
281
282	/* The number of tiles. */
283	uint_fast32_t numtiles;
284
285	/* The number of components. */
286	uint_fast16_t numcmpts;
287
288	/* The per-component coding parameters. */
289	jpc_enc_ccp_t *ccps;
290
291	/* The per-tile coding parameters. */
292	jpc_enc_tcp_t tcp;
293
294	/* The per-tile-component coding parameters. */
295	jpc_enc_tccp_t tccp;
296
297	/* The target code stream length in bytes. */
298	uint_fast32_t totalsize;
299
300	/* The raw (i.e., uncompressed) size of the image in bytes. */
301	uint_fast32_t rawsize;
302
303} jpc_enc_cp_t;
304
305/******************************************************************************\
306* Encoder class.
307\******************************************************************************/
308
309/* Encoder per-coding-pass state information. */
310
311typedef struct {
312
313	/* The starting offset for this pass. */
314	int start;
315
316	/* The ending offset for this pass. */
317	int end;
318
319	/* The type of data in this pass (i.e., MQ or raw). */
320	int type;
321
322	/* Flag indicating that this pass is terminated. */
323	int term;
324
325	/* The entropy coder state after coding this pass. */
326	jpc_mqencstate_t mqencstate;
327
328	/* The layer to which this pass has been assigned. */
329	int lyrno;
330
331	/* The R-D slope for this pass. */
332	jpc_flt_t rdslope;
333
334	/* The weighted MSE reduction associated with this pass. */
335	jpc_flt_t wmsedec;
336
337	/* The cumulative weighted MSE reduction. */
338	jpc_flt_t cumwmsedec;
339
340	/* The normalized MSE reduction. */
341	long nmsedec;
342
343} jpc_enc_pass_t;
344
345/* Encoder per-code-block state information. */
346
347typedef struct {
348
349	/* The number of passes. */
350	int numpasses;
351
352	/* The per-pass information. */
353	jpc_enc_pass_t *passes;
354
355	/* The number of passes encoded so far. */
356	int numencpasses;
357
358	/* The number of insignificant MSBs. */
359	int numimsbs;
360
361	/* The number of bits used to encode pass data lengths. */
362	int numlenbits;
363
364	/* The byte stream for this code block. */
365	jas_stream_t *stream;
366
367	/* The entropy encoder. */
368	jpc_mqenc_t *mqenc;
369
370	/* The data for this code block. */
371	jas_matrix_t *data;
372
373	/* The state for this code block. */
374	jas_matrix_t *flags;
375
376	/* The number of bit planes required for this code block. */
377	int numbps;
378
379	/* The next pass to be encoded. */
380	jpc_enc_pass_t *curpass;
381
382	/* The per-code-block-group state information. */
383	struct jpc_enc_prc_s *prc;
384
385	/* The saved current pass. */
386	/* This is used by the rate control code. */
387	jpc_enc_pass_t *savedcurpass;
388
389	/* The saved length indicator size. */
390	/* This is used by the rate control code. */
391	int savednumlenbits;
392
393	/* The saved number of encoded passes. */
394	/* This is used by the rate control code. */
395	int savednumencpasses;
396
397} jpc_enc_cblk_t;
398
399/* Encoder per-code-block-group state information. */
400
401typedef struct jpc_enc_prc_s {
402
403	/* The x-coordinate of the top-left corner of the precinct. */
404	uint_fast32_t tlx;
405
406	/* The y-coordinate of the top-left corner of the precinct. */
407	uint_fast32_t tly;
408
409	/* The x-coordinate of the bottom-right corner of the precinct
410	  (plus one). */
411	uint_fast32_t brx;
412
413	/* The y-coordinate of the bottom-right corner of the precinct
414	  (plus one). */
415	uint_fast32_t bry;
416
417	/* The number of code blocks spanning the precinct in the horizontal
418	direction. */
419	int numhcblks;
420
421	/* The number of code blocks spanning the precinct in the vertical
422	direction. */
423	int numvcblks;
424
425	/* The total number of code blocks. */
426	int numcblks;
427
428	/* The per-code-block information. */
429	jpc_enc_cblk_t *cblks;
430
431	/* The inclusion tag tree. */
432	jpc_tagtree_t *incltree;
433
434	/* The insignifcant MSBs tag tree. */
435	jpc_tagtree_t *nlibtree;
436
437	/* The per-band information. */
438	struct jpc_enc_band_s *band;
439
440	/* The saved inclusion tag tree. */
441	/* This is used by rate control. */
442	jpc_tagtree_t *savincltree;
443
444	/* The saved leading-insignificant-bit-planes tag tree. */
445	/* This is used by rate control. */
446	jpc_tagtree_t *savnlibtree;
447
448} jpc_enc_prc_t;
449
450/* Encoder per-band state information. */
451
452typedef struct jpc_enc_band_s {
453
454	/* The per precinct information. */
455	jpc_enc_prc_t *prcs;
456
457	/* The coefficient data for this band. */
458	jas_matrix_t *data;
459
460	/* The orientation of this band (i.e., LL, LH, HL, or HH). */
461	int orient;
462
463	/* The number of bit planes associated with this band. */
464	int numbps;
465
466	/* The quantizer step size. */
467	jpc_fix_t absstepsize;
468
469	/* The encoded quantizer step size. */
470	int stepsize;
471
472	/* The L2 norm of the synthesis basis functions associated with
473	  this band.  (The MCT is not considered in this value.) */
474	jpc_fix_t synweight;
475
476	/* The analysis gain for this band. */
477	int analgain;
478
479	/* The per-resolution-level information. */
480	struct jpc_enc_rlvl_s *rlvl;
481
482} jpc_enc_band_t;
483
484/* Encoder per-resolution-level state information. */
485
486typedef struct jpc_enc_rlvl_s {
487
488	/* The x-coordinate of the top-left corner of the tile-component
489	  at this resolution. */
490	uint_fast32_t tlx;
491
492	/* The y-coordinate of the top-left corner of the tile-component
493	  at this resolution. */
494	uint_fast32_t tly;
495
496	/* The x-coordinate of the bottom-right corner of the tile-component
497	  at this resolution (plus one). */
498	uint_fast32_t brx;
499
500	/* The y-coordinate of the bottom-right corner of the tile-component
501	  at this resolution (plus one). */
502	uint_fast32_t bry;
503
504	/* The exponent value for the nominal precinct width measured
505	  relative to the associated LL band. */
506	int prcwidthexpn;
507
508	/* The exponent value for the nominal precinct height measured
509	  relative to the associated LL band. */
510	int prcheightexpn;
511
512	/* The number of precincts spanning the resolution level in the
513	  horizontal direction. */
514	int numhprcs;
515
516	/* The number of precincts spanning the resolution level in the
517	  vertical direction. */
518	int numvprcs;
519
520	/* The total number of precincts. */
521	int numprcs;
522
523	/* The exponent value for the nominal code block group width.
524	  This quantity is associated with the next lower resolution level
525	  (assuming that there is one). */
526	int cbgwidthexpn;
527
528	/* The exponent value for the nominal code block group height.
529	  This quantity is associated with the next lower resolution level
530	  (assuming that there is one). */
531	int cbgheightexpn;
532
533	/* The exponent value for the code block width. */
534	uint_fast16_t cblkwidthexpn;
535
536	/* The exponent value for the code block height. */
537	uint_fast16_t cblkheightexpn;
538
539	/* The number of bands associated with this resolution level. */
540	int numbands;
541
542	/* The per-band information. */
543	jpc_enc_band_t *bands;
544
545	/* The parent tile-component. */
546	struct jpc_enc_tcmpt_s *tcmpt;
547
548} jpc_enc_rlvl_t;
549
550/* Encoder per-tile-component state information. */
551
552typedef struct jpc_enc_tcmpt_s {
553
554	/* The number of resolution levels. */
555	uint_fast16_t numrlvls;
556
557	/* The per-resolution-level information. */
558	jpc_enc_rlvl_t *rlvls;
559
560	/* The tile-component data. */
561	jas_matrix_t *data;
562
563	/* The QMFB. */
564	int qmfbid;
565
566	/* The number of bands. */
567	int numbands;
568
569	/* The TSFB. */
570	jpc_tsfb_t *tsfb;
571
572	/* The synthesis energy weight (for the MCT). */
573	jpc_fix_t synweight;
574
575	/* The precinct width exponents. */
576	int prcwidthexpns[JPC_MAXRLVLS];
577
578	/* The precinct height exponents. */
579	int prcheightexpns[JPC_MAXRLVLS];
580
581	/* The code block width exponent. */
582	int cblkwidthexpn;
583
584	/* The code block height exponent. */
585	int cblkheightexpn;
586
587	/* Coding style (i.e., explicit precinct sizes). */
588	int csty;
589
590	/* Code block style. */
591	int cblksty;
592
593	/* The number of quantizer step sizes. */
594	uint_fast16_t numstepsizes;
595
596	/* The encoded quantizer step sizes. */
597	uint_fast16_t stepsizes[JPC_MAXBANDS];
598
599	/* The parent tile. */
600	struct jpc_enc_tile_s *tile;
601
602} jpc_enc_tcmpt_t;
603
604/* Encoder per-tile state information. */
605
606typedef struct jpc_enc_tile_s {
607
608	/* The tile number. */
609	uint_fast32_t tileno;
610
611	/* The x-coordinate of the top-left corner of the tile measured with
612	  respect to the reference grid. */
613	uint_fast32_t tlx;
614
615	/* The y-coordinate of the top-left corner of the tile measured with
616	  respect to the reference grid. */
617	uint_fast32_t tly;
618
619	/* The x-coordinate of the bottom-right corner of the tile measured
620	  with respect to the reference grid (plus one). */
621	uint_fast32_t brx;
622
623	/* The y-coordinate of the bottom-right corner of the tile measured
624	  with respect to the reference grid (plus one). */
625	uint_fast32_t bry;
626
627	/* The coding style. */
628	uint_fast8_t csty;
629
630	/* The progression order. */
631	uint_fast8_t prg;
632
633	/* The number of layers. */
634	uint_fast16_t numlyrs;
635
636	/* The MCT to employ (if any). */
637	uint_fast8_t mctid;
638
639	/* The packet iterator (used to determine the order of packet
640	  generation). */
641	jpc_pi_t *pi;
642
643	/* The coding mode (i.e., integer or real). */
644	JPR_BOOL intmode;
645
646	/* The number of bytes to allocate to the various layers. */
647	uint_fast32_t *lyrsizes;
648
649	/* The number of tile-components. */
650	int numtcmpts;
651
652	/* The per tile-component information. */
653	jpc_enc_tcmpt_t *tcmpts;
654
655	/* The raw (i.e., uncompressed) size of this tile. */
656	uint_fast32_t rawsize;
657
658} jpc_enc_tile_t;
659
660/* Encoder class. */
661
662typedef struct jpc_enc_s {
663
664	/* The image being encoded. */
665	jas_image_t *image;
666
667	/* The output stream. */
668	jas_stream_t *out;
669
670	/* The coding parameters. */
671	jpc_enc_cp_t *cp;
672
673	/* The tile currently being processed. */
674	jpc_enc_tile_t *curtile;
675
676	/* The code stream state. */
677	jpc_cstate_t *cstate;
678
679	/* The number of bytes output so far. */
680	uint_fast32_t len;
681
682	/* The number of bytes available for the main body of the code stream. */
683	/* This is used for rate allocation purposes. */
684	uint_fast32_t mainbodysize;
685
686	/* The marker segment currently being processed. */
687	/* This member is a convenience for making cleanup easier. */
688	jpc_ms_t *mrk;
689
690	/* The stream used to temporarily hold tile-part data. */
691	jas_stream_t *tmpstream;
692
693} jpc_enc_t;
694
695#endif
696