2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
9 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 @brief The JPEG-2000 Codestream Reader/Writer (J2K)
39 The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
44 struct opj_dparameters;
45 struct opj_stream_private;
47 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
50 #define J2K_CP_CSTY_PRT 0x01
51 #define J2K_CP_CSTY_SOP 0x02
52 #define J2K_CP_CSTY_EPH 0x04
53 #define J2K_CCP_CSTY_PRT 0x01
54 #define J2K_CCP_CBLKSTY_LAZY 0x01
55 #define J2K_CCP_CBLKSTY_RESET 0x02
56 #define J2K_CCP_CBLKSTY_TERMALL 0x04
57 #define J2K_CCP_CBLKSTY_VSC 0x08
58 #define J2K_CCP_CBLKSTY_PTERM 0x10
59 #define J2K_CCP_CBLKSTY_SEGSYM 0x20
60 #define J2K_CCP_QNTSTY_NOQNT 0
61 #define J2K_CCP_QNTSTY_SIQNT 1
62 #define J2K_CCP_QNTSTY_SEQNT 2
64 /* ----------------------------------------------------------------------- */
66 #define J2K_MS_SOC 0xff4f /**< SOC marker value */
67 #define J2K_MS_SOT 0xff90 /**< SOT marker value */
68 #define J2K_MS_SOD 0xff93 /**< SOD marker value */
69 #define J2K_MS_EOC 0xffd9 /**< EOC marker value */
70 #define J2K_MS_SIZ 0xff51 /**< SIZ marker value */
71 #define J2K_MS_COD 0xff52 /**< COD marker value */
72 #define J2K_MS_COC 0xff53 /**< COC marker value */
73 #define J2K_MS_RGN 0xff5e /**< RGN marker value */
74 #define J2K_MS_QCD 0xff5c /**< QCD marker value */
75 #define J2K_MS_QCC 0xff5d /**< QCC marker value */
76 #define J2K_MS_POC 0xff5f /**< POC marker value */
77 #define J2K_MS_TLM 0xff55 /**< TLM marker value */
78 #define J2K_MS_PLM 0xff57 /**< PLM marker value */
79 #define J2K_MS_PLT 0xff58 /**< PLT marker value */
80 #define J2K_MS_PPM 0xff60 /**< PPM marker value */
81 #define J2K_MS_PPT 0xff61 /**< PPT marker value */
82 #define J2K_MS_SOP 0xff91 /**< SOP marker value */
83 #define J2K_MS_EPH 0xff92 /**< EPH marker value */
84 #define J2K_MS_CRG 0xff63 /**< CRG marker value */
85 #define J2K_MS_COM 0xff64 /**< COM marker value */
86 #define J2K_MS_CBD 0xff78 /**< CBD marker value */
87 #define J2K_MS_MCC 0xff75 /**< MCC marker value */
88 #define J2K_MS_MCT 0xff74 /**< MCT marker value */
89 #define J2K_MS_MCO 0xff77 /**< MCO marker value */
93 #define J2K_MS_EPC 0xff68 /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */
94 #define J2K_MS_EPB 0xff66 /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */
95 #define J2K_MS_ESD 0xff67 /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */
96 #define J2K_MS_RED 0xff69 /**< RED marker value (Part 11: JPEG 2000 for Wireless) */
99 #define J2K_MS_SEC 0xff65 /**< SEC marker value (Part 8: Secure JPEG 2000) */
100 #define J2K_MS_INSEC 0xff94 /**< INSEC marker value (Part 8: Secure JPEG 2000) */
101 #endif /* USE_JPSEC */
105 /* ----------------------------------------------------------------------- */
108 Values that specify the status of the decoding process when decoding the main header.
109 These values may be combined with a | operator.
113 J2K_DEC_STATE_NONE = 0x0000, /**< a SOC marker is expected */
114 J2K_DEC_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */
115 J2K_DEC_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */
116 J2K_DEC_STATE_MH = 0x0004, /**< the decoding process is in the main header */
117 J2K_DEC_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
118 J2K_DEC_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */
119 J2K_DEC_STATE_MT = 0x0020, /**< the EOC marker has just been read */
120 J2K_DEC_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
121 J2K_DEC_STATE_DATA = 0x0080, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
122 J2K_DEC_STATE_ERR = 0x8000, /**< the decoding process has encountered an error */
123 J2K_DEC_STATE_EOC = 0x0100
128 Values that specify the status of the decoding process when decoding the main header.
129 These values may be combined with a | operator.
133 J2K_ENC_STATE_NONE = 0x0000, /**< a SOC marker is expected */
134 J2K_ENC_STATE_ENCODE = 0x0001, /**< a SOC marker is expected */
139 * Type of data for storing the MCT data
141 typedef enum MCT_ELEMENT_TYPE
143 MCT_TYPE_INT16 = 0, /** MCT data is stored as signed shorts*/
144 MCT_TYPE_INT32 = 1, /** MCT data is stored as signed integers*/
145 MCT_TYPE_FLOAT = 2, /** MCT data is stored as floats*/
146 MCT_TYPE_DOUBLE = 3 /** MCT data is stored as doubles*/
147 } J2K_MCT_ELEMENT_TYPE;
150 * Type of data for storing the MCT data
152 typedef enum MCT_ARRAY_TYPE
154 MCT_TYPE_DEPENDENCY = 0,
155 MCT_TYPE_DECORRELATION = 1,
157 } J2K_MCT_ARRAY_TYPE;
160 /* ----------------------------------------------------------------------- */
165 Quantization stepsize
167 typedef struct opj_stepsize
176 typedef struct opj_mct_data
178 J2K_MCT_ELEMENT_TYPE m_element_type;
179 J2K_MCT_ARRAY_TYPE m_array_type;
182 OPJ_UINT32 m_data_size;
186 typedef struct opj_simple_mcc_decorrelation_data
189 OPJ_UINT32 m_nb_comps;
190 opj_mct_data_t * m_decorrelation_array;
191 opj_mct_data_t * m_offset_array;
192 OPJ_UINT32 m_is_irreversible : 1;
194 opj_simple_mcc_decorrelation_data_t;
198 Tile-component coding parameters
200 typedef struct opj_tccp
204 /** number of resolutions */
205 OPJ_UINT32 numresolutions;
206 /** code-blocks width */
208 /** code-blocks height */
210 /** code-block coding style */
212 /** discrete wavelet transform identifier */
214 /** quantisation style */
216 /** stepsizes used for quantization */
217 opj_stepsize_t stepsizes[J2K_MAXBANDS];
218 /** number of guard bits */
220 /** Region Of Interest shift */
222 /** precinct width */
223 OPJ_UINT32 prcw[J2K_MAXRLVLS];
224 /** precinct height */
225 OPJ_UINT32 prch[J2K_MAXRLVLS];
226 /** the dc_level_shift **/
227 OPJ_INT32 m_dc_level_shift;
232 Tile coding parameters :
233 this structure is used to store coding/decoding parameters common to all
234 tiles (information like COD, COC in main header)
236 typedef struct opj_tcp
240 /** progression order */
242 /** number of layers */
243 OPJ_UINT32 numlayers;
244 OPJ_UINT32 num_layers_to_decode;
245 /** multi-component transform identifier */
247 /** rates of layers */
248 OPJ_FLOAT32 rates[100];
249 /** number of progression order changes */
251 /** progression order changes */
253 /** packet header store there for futur use in t2_decode_packet */
255 /** used to keep a track of the allocated memory */
256 OPJ_BYTE *ppt_buffer;
257 /** Number of bytes stored inside ppt_data*/
258 OPJ_UINT32 ppt_data_size;
259 /** size of ppt_data*/
261 /** add fixed_quality */
262 OPJ_FLOAT32 distoratio[100];
263 /** tile-component coding parameters */
265 /** number of tile parts for the tile. */
266 OPJ_UINT32 m_nb_tile_parts;
267 /** data for the tile */
270 OPJ_UINT32 m_data_size;
271 /** encoding norms */
272 OPJ_FLOAT64 * mct_norms;
273 /** the mct decoding matrix */
274 OPJ_FLOAT32 * m_mct_decoding_matrix;
275 /** the mct coding matrix */
276 OPJ_FLOAT32 * m_mct_coding_matrix;
278 opj_mct_data_t * m_mct_records;
279 /** the number of mct records. */
280 OPJ_UINT32 m_nb_mct_records;
281 /** the max number of mct records. */
282 OPJ_UINT32 m_nb_max_mct_records;
284 opj_simple_mcc_decorrelation_data_t * m_mcc_records;
285 /** the number of mct records. */
286 OPJ_UINT32 m_nb_mcc_records;
287 /** the max number of mct records. */
288 OPJ_UINT32 m_nb_max_mcc_records;
292 /***** FLAGS *******/
293 /** If ppt == 1 --> there was a PPT marker for the present tile */
295 /** indicates if a POC marker has been used O:NO, 1:YES */
299 typedef struct opj_encoding_param
301 /** Digital cinema profile*/
302 OPJ_CINEMA_MODE m_cinema;
303 /** Maximum rate for each component. If == 0, component size limitation is not considered */
304 OPJ_UINT32 m_max_comp_size;
305 /** Position of tile part flag in progression order*/
308 OPJ_INT32 *m_matrice;
309 /** Flag determining tile part generation*/
311 /** allocation by rate/distortion */
312 OPJ_UINT32 m_disto_alloc : 1;
313 /** allocation by fixed layer */
314 OPJ_UINT32 m_fixed_alloc : 1;
315 /** add fixed_quality */
316 OPJ_UINT32 m_fixed_quality : 1;
317 /** Enabling Tile part generation*/
318 OPJ_UINT32 m_tp_on : 1;
320 opj_encoding_param_t;
322 typedef struct opj_decoding_param
324 /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
326 /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
329 opj_decoding_param_t;
334 typedef struct opj_cp
336 /** Size of the image in bits*/
339 OPJ_RSIZ_CAPABILITIES rsiz;
350 /** number of tiles in width */
352 /** number of tiles in heigth */
354 /** packet header storage original buffer */
355 OPJ_BYTE *ppm_buffer;
356 /** packet header store there for futur use in t2_decode_packet */
358 /** size of the ppm_data*/
360 /** Number of bytes actually stored inside the ppm_data */
361 OPJ_UINT32 ppm_data_size;
362 /** tile coding parameters */
366 opj_decoding_param_t m_dec;
367 opj_encoding_param_t m_enc;
374 /** enables writing of EPC in MH, thus activating JPWL */
376 /** enables writing of EPB, in case of activated JPWL */
378 /** enables writing of ESD, in case of activated JPWL */
380 /** enables writing of informative techniques of ESD, in case of activated JPWL */
382 /** enables writing of RED, in case of activated JPWL */
384 /** error protection method for MH (0,1,16,32,37-128) */
386 /** tile number of header protection specification (>=0) */
387 int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
388 /** error protection methods for TPHs (0,1,16,32,37-128) */
389 int hprot_TPH[JPWL_MAX_NO_TILESPECS];
390 /** tile number of packet protection specification (>=0) */
391 int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
392 /** packet number of packet protection specification (>=0) */
393 int pprot_packno[JPWL_MAX_NO_PACKSPECS];
394 /** error protection methods for packets (0,1,16,32,37-128) */
395 int pprot[JPWL_MAX_NO_PACKSPECS];
396 /** enables writing of ESD, (0/2/4 bytes) */
398 /** sensitivity addressing size (0=auto/2/4 bytes) */
400 /** sensitivity range (0-3) */
402 /** sensitivity method for MH (-1,0-7) */
404 /** tile number of sensitivity specification (>=0) */
405 int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
406 /** sensitivity methods for TPHs (-1,0-7) */
407 int sens_TPH[JPWL_MAX_NO_TILESPECS];
408 /** enables JPWL correction at the decoder */
410 /** expected number of components at the decoder */
412 /** maximum number of tiles at the decoder */
414 #endif /* USE_JPWL */
416 /******** FLAGS *********/
417 /** if ppm == 1 --> there was a PPM marker*/
419 /** tells if the parameter is a coding or decoding one */
420 OPJ_UINT32 m_is_decoder : 1;
424 typedef struct opj_j2k_dec
426 /** locate in which part of the codestream the decoder is (main header, tile header, end) */
429 * store decoding parameters common to all tiles (information like COD, COC in main header)
431 opj_tcp_t *m_default_tcp;
432 OPJ_BYTE *m_header_data;
433 OPJ_UINT32 m_header_data_size;
434 /** to tell the tile part length */
435 OPJ_UINT32 m_sot_length;
436 /** Only tiles index in the correct range will be decoded.*/
437 OPJ_UINT32 m_start_tile_x;
438 OPJ_UINT32 m_start_tile_y;
439 OPJ_UINT32 m_end_tile_x;
440 OPJ_UINT32 m_end_tile_y;
441 /** to tell that a tile can be decoded. */
442 OPJ_UINT32 m_can_decode : 1;
443 OPJ_UINT32 m_discard_tiles : 1;
444 OPJ_UINT32 m_skip_data : 1;
448 typedef struct opj_j2k_enc
450 /** Tile part number, regardless of poc, for each new poc, tp is reset to 1*/
451 OPJ_UINT32 m_current_poc_tile_part_number; // tp_num
453 /** Tile part number currently coding, taking into account POC. m_current_tile_part_number holds the total number of tile parts while encoding the last tile part.*/
454 OPJ_UINT32 m_current_tile_part_number; //cur_tp_num
457 locate the start position of the TLM marker
458 after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
460 OPJ_SIZE_T m_tlm_start;
462 * Stores the sizes of the tlm.
464 OPJ_BYTE * m_tlm_sot_offsets_buffer;
466 * The current offset of the tlm buffer.
468 OPJ_BYTE * m_tlm_sot_offsets_current;
470 /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
471 /** used in TLMmarker*/
472 OPJ_UINT32 m_total_tile_parts; // totnum_tp
474 /* encoded data for a tile */
475 OPJ_BYTE * m_encoded_tile_data;
477 /* size of the encoded_data */
478 OPJ_UINT32 m_encoded_tile_size;
480 /* encoded data for a tile */
481 OPJ_BYTE * m_header_tile_data;
483 /* size of the encoded_data */
484 OPJ_UINT32 m_header_tile_data_size;
493 JPEG-2000 codestream reader/writer
495 typedef struct opj_j2k
499 opj_j2k_dec_t m_decoder;
500 opj_j2k_enc_t m_encoder;
504 /** number of the tile curently concern by coding/decoding */
505 OPJ_UINT32 m_current_tile_number;
507 /** pointer to the encoded / decoded image */
508 opj_image_t *m_image;
510 /** Coding parameters */
513 /** the list of procedures to exec **/
514 struct opj_procedure_list * m_procedure_list;
516 /** the list of validation procedures to follow to make sure the code is valid **/
517 struct opj_procedure_list * m_validation_list;
519 /** helper used to write the index file */
520 opj_codestream_info_t *cstr_info;
522 /** the current tile coder/decoder **/
523 struct opj_tcd * m_tcd;
525 OPJ_UINT32 m_is_decoder : 1;
530 /** @name Exported functions */
532 /* ----------------------------------------------------------------------- */
534 * Creates a J2K decompression structure.
536 * @return a handle to a J2K decompressor if successful, NULL otherwise.
538 opj_j2k_t* j2k_create_decompress();
541 * Destroys a jpeg2000 codec.
543 * @param p_j2k the jpeg20000 structure to destroy.
545 void j2k_destroy (opj_j2k_t *p_j2k);
548 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
550 * @param p_j2k the jpeg2000 codec.
551 * @param cio the stream object.
552 * @param p_manager the user event manager.
554 * @return true if the codec is valid.
556 bool j2k_start_compress(
558 struct opj_stream_private *cio,
559 struct opj_image * p_image,
560 struct opj_event_mgr * p_manager
563 * Ends the compression procedures and possibiliy add data to be read after the
566 bool j2k_end_compress(opj_j2k_t *p_j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager);
569 * Sets up the decoder decoding parameters using user parameters.
570 * Decoding parameters are stored in j2k->cp.
572 * @param p_j2k J2K codec
573 * @param p_parameters decompression parameters
576 void j2k_setup_decoder(
578 struct opj_dparameters *p_parameters
581 * Reads a jpeg2000 codestream header structure.
583 * @param cio the stream to read data from.
584 * @param p_j2k the jpeg2000 codec.
585 * @param p_manager the user event manager.
587 * @return true if the box is valid.
589 bool j2k_read_header(
591 struct opj_image ** p_image,
592 OPJ_INT32 * p_tile_x0,
593 OPJ_INT32 * p_tile_y0,
594 OPJ_UINT32 * p_tile_width,
595 OPJ_UINT32 * p_tile_height,
596 OPJ_UINT32 * p_nb_tiles_x,
597 OPJ_UINT32 * p_nb_tiles_y,
598 struct opj_stream_private *cio,
599 struct opj_event_mgr * p_manager
602 Decode an image from a JPEG-2000 codestream
603 @param j2k J2K decompressor handle
604 @param cio Input buffer stream
605 @param cstr_info Codestream information structure if required, NULL otherwise
606 @return Returns a decoded image if successful, returns NULL otherwise
608 opj_image_t* j2k_decode(opj_j2k_t *j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager);
611 * Ends the decompression procedures and possibiliy add data to be read after the
614 bool j2k_end_decompress(opj_j2k_t *j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager);
617 Decode an image form a JPT-stream (JPEG 2000, JPIP)
618 @param j2k J2K decompressor handle
619 @param cio Input buffer stream
620 @param cstr_info Codestream information structure if required, NULL otherwise
621 @return Returns a decoded image if successful, returns NULL otherwise
623 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, struct opj_stream_private *cio, struct opj_codestream_info *cstr_info);
626 Creates a J2K compression structure
627 @param cinfo Codec context info
628 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
630 opj_j2k_t* j2k_create_compress();
633 Setup the encoder parameters using the current image and using user parameters.
634 Coding parameters are returned in j2k->cp.
635 @param j2k J2K compressor handle
636 @param parameters compression parameters
637 @param image input filled image
639 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image, struct opj_event_mgr * p_manager);
643 * @param p_j2k the jpeg2000 codec.
644 * @param p_stream the stream to write data to.
645 * @param p_manager the user event manager.
647 bool j2k_write_tile (
649 OPJ_UINT32 p_tile_index,
651 OPJ_UINT32 p_data_size,
652 struct opj_stream_private *p_stream,
653 struct opj_event_mgr * p_manager
657 * Converts an enum type progression order to string type.
659 * @param prg_order the progression order to get.
661 * @return the string representation of the gicen progression order.
663 const char * j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
667 * Encodes an image into a JPEG-2000 codestream
671 struct opj_stream_private *cio,
672 struct opj_event_mgr * p_manager
674 bool j2k_setup_mct_encoding (opj_tcp_t * p_tcp,opj_image_t * p_image);
678 * @param p_j2k the jpeg2000 codec.
679 * @param p_stream the stream to write data to.
680 * @param p_manager the user event manager.
682 bool j2k_decode_tile (
684 OPJ_UINT32 p_tile_index,
686 OPJ_UINT32 p_data_size,
687 struct opj_stream_private *p_stream,
688 struct opj_event_mgr * p_manager
691 * Reads a tile header.
692 * @param p_j2k the jpeg2000 codec.
693 * @param p_stream the stream to write data to.
694 * @param p_manager the user event manager.
696 bool j2k_read_tile_header (
698 OPJ_UINT32 * p_tile_index,
699 OPJ_UINT32 * p_data_size,
700 OPJ_INT32 * p_tile_x0,
701 OPJ_INT32 * p_tile_y0,
702 OPJ_INT32 * p_tile_x1,
703 OPJ_INT32 * p_tile_y1,
704 OPJ_UINT32 * p_nb_comps,
706 struct opj_stream_private *p_stream,
707 struct opj_event_mgr * p_manager
710 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
712 * @param p_j2k the jpeg2000 codec.
713 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
714 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
715 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
716 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
717 * @param p_manager the user event manager
719 * @return true if the area could be set.
721 bool j2k_set_decode_area(
727 struct opj_event_mgr * p_manager
731 /* ----------------------------------------------------------------------- */