Cleanup code related to quality layer allocation, and add a few safety checks
[openjpeg.git] / src / lib / openjp2 / j2k.h
index fe65cd87b8591acc1532d7db556cbe8807f3f555..e0b9688a353f76e80236f4a1a1947fe74996f8fb 100644 (file)
@@ -39,8 +39,8 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-#ifndef __J2K_H
-#define __J2K_H
+#ifndef OPJ_J2K_H
+#define OPJ_J2K_H
 /**
 @file j2k.h
 @brief The JPEG-2000 Codestream Reader/Writer (J2K)
@@ -61,21 +61,23 @@ The functions in J2K.C have for goal to read/write the several parts of the code
 #define J2K_CCP_CBLKSTY_VSC 0x08      /**< Vertically stripe causal context */
 #define J2K_CCP_CBLKSTY_PTERM 0x10    /**< Predictable termination */
 #define J2K_CCP_CBLKSTY_SEGSYM 0x20   /**< Segmentation symbols are used */
+#define J2K_CCP_CBLKSTY_HT 0x40       /**< (high throughput) HT codeblocks */
+#define J2K_CCP_CBLKSTY_HTMIXED 0x80  /**< MIXED mode HT codeblocks */
 #define J2K_CCP_QNTSTY_NOQNT 0
 #define J2K_CCP_QNTSTY_SIQNT 1
 #define J2K_CCP_QNTSTY_SEQNT 2
 
-#define OPJ_J2K_DEFAULT_CBLK_DATA_SIZE 8192
-
 /* ----------------------------------------------------------------------- */
 
 #define J2K_MS_SOC 0xff4f   /**< SOC marker value */
 #define J2K_MS_SOT 0xff90   /**< SOT marker value */
 #define J2K_MS_SOD 0xff93   /**< SOD marker value */
 #define J2K_MS_EOC 0xffd9   /**< EOC marker value */
+#define J2K_MS_CAP 0xff50   /**< CAP marker value */
 #define J2K_MS_SIZ 0xff51   /**< SIZ marker value */
 #define J2K_MS_COD 0xff52   /**< COD marker value */
 #define J2K_MS_COC 0xff53   /**< COC marker value */
+#define J2K_MS_CPF 0xff59   /**< CPF marker value */
 #define J2K_MS_RGN 0xff5e   /**< RGN marker value */
 #define J2K_MS_QCD 0xff5c   /**< QCD marker value */
 #define J2K_MS_QCC 0xff5d   /**< QCC marker value */
@@ -109,6 +111,11 @@ The functions in J2K.C have for goal to read/write the several parts of the code
 #endif /* USE_JPSEC */
 /* <<UniPG */
 
+#define J2K_MAX_POCS    32      /**< Maximum number of POCs */
+
+#define J2K_TCD_MATRIX_MAX_LAYER_COUNT 10
+#define J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT 10
+
 /* ----------------------------------------------------------------------- */
 
 /**
@@ -124,6 +131,7 @@ typedef enum J2K_STATUS {
     J2K_STATE_TPH    = 0x0010, /**< the decoding process is in a tile part header */
     J2K_STATE_MT     = 0x0020, /**< the EOC marker has just been read */
     J2K_STATE_NEOC   = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
+    J2K_STATE_DATA   = 0x0080, /**< a tile header has been successfully read and codestream is expected */
 
     J2K_STATE_EOC    = 0x0100, /**< the decoding process has encountered the EOC marker */
     J2K_STATE_ERR    = 0x8000  /**< the decoding process has encountered an error (FIXME warning V1 = 0x0080)*/
@@ -252,7 +260,7 @@ typedef struct opj_tcp {
     /** number of progression order changes */
     OPJ_UINT32 numpocs;
     /** progression order changes */
-    opj_poc_t pocs[32];
+    opj_poc_t pocs[J2K_MAX_POCS];
 
     /** number of ppt markers (reserved size) */
     OPJ_UINT32 ppt_markers_count;
@@ -267,10 +275,12 @@ typedef struct opj_tcp {
     OPJ_UINT32 ppt_data_size;
     /** size of ppt_data*/
     OPJ_UINT32 ppt_len;
-    /** add fixed_quality */
+    /** PSNR values */
     OPJ_FLOAT32 distoratio[100];
     /** tile-component coding parameters */
     opj_tccp_t *tccps;
+    /** current tile part number or -1 if first time into this tile */
+    OPJ_INT32  m_current_tile_part_number;
     /** number of tile parts for the tile. */
     OPJ_UINT32 m_nb_tile_parts;
     /** data for the tile */
@@ -307,6 +317,14 @@ typedef struct opj_tcp {
 } opj_tcp_t;
 
 
+/**
+Rate allocation strategy
+*/
+typedef enum {
+    RATE_DISTORTION_RATIO = 0,    /** allocation by rate/distortion */
+    FIXED_DISTORTION_RATIO = 1,   /** allocation by fixed distortion ratio (PSNR) (fixed quality) */
+    FIXED_LAYER = 2,              /** allocation by fixed layer (number of passes per layer / resolution / subband) */
+} J2K_QUALITY_LAYER_ALLOCATION_STRATEGY;
 
 
 typedef struct opj_encoding_param {
@@ -318,12 +336,8 @@ typedef struct opj_encoding_param {
     OPJ_INT32 *m_matrice;
     /** Flag determining tile part generation*/
     OPJ_BYTE m_tp_flag;
-    /** allocation by rate/distortion */
-    OPJ_BITFIELD m_disto_alloc : 1;
-    /** allocation by fixed layer */
-    OPJ_BITFIELD m_fixed_alloc : 1;
-    /** add fixed_quality */
-    OPJ_BITFIELD m_fixed_quality : 1;
+    /** Quality layer allocation strategy */
+    J2K_QUALITY_LAYER_ALLOCATION_STRATEGY m_quality_layer_alloc_strategy;
     /** Enabling Tile part generation*/
     OPJ_BITFIELD m_tp_on : 1;
 }
@@ -358,7 +372,7 @@ typedef struct opj_cp {
     OPJ_CHAR *comment;
     /** number of tiles in width */
     OPJ_UINT32 tw;
-    /** number of tiles in heigth */
+    /** number of tiles in height */
     OPJ_UINT32 th;
 
     /** number of ppm markers (reserved size) */
@@ -395,6 +409,8 @@ typedef struct opj_cp {
     }
     m_specific_param;
 
+    /** OPJ_TRUE if entire bit stream must be decoded, OPJ_FALSE if partial bitstream decoding allowed */
+    OPJ_BOOL strict;
 
     /* UniPG>> */
 #ifdef USE_JPWL
@@ -445,6 +461,8 @@ typedef struct opj_cp {
     OPJ_BITFIELD ppm : 1;
     /** tells if the parameter is a coding or decoding one */
     OPJ_BITFIELD m_is_decoder : 1;
+    /** whether different bit depth or sign per component is allowed. Decoder only for ow */
+    OPJ_BITFIELD allow_different_bit_depth_sign : 1;
     /* <<UniPG */
 } opj_cp_t;
 
@@ -465,13 +483,6 @@ typedef struct opj_j2k_dec {
     OPJ_UINT32 m_start_tile_y;
     OPJ_UINT32 m_end_tile_x;
     OPJ_UINT32 m_end_tile_y;
-    /**
-     * Decoded area set by the user
-     */
-    OPJ_UINT32 m_DA_x0;
-    OPJ_UINT32 m_DA_y0;
-    OPJ_UINT32 m_DA_x1;
-    OPJ_UINT32 m_DA_y1;
 
     /** Index of the tile to decode (used in get_tile) */
     OPJ_INT32 m_tile_ind_to_dec;
@@ -484,6 +495,10 @@ typedef struct opj_j2k_dec {
      * SOD reader function. FIXME NOT USED for the moment
      */
     OPJ_BOOL   m_last_tile_part;
+
+    OPJ_UINT32   m_numcomps_to_decode;
+    OPJ_UINT32  *m_comps_indices_to_decode;
+
     /** to tell that a tile can be decoded. */
     OPJ_BITFIELD m_can_decode : 1;
     OPJ_BITFIELD m_discard_tiles : 1;
@@ -501,6 +516,12 @@ typedef struct opj_j2k_enc {
     /** 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.*/
     OPJ_UINT32 m_current_tile_part_number; /*cur_tp_num */
 
+    /* whether to generate TLM markers */
+    OPJ_BOOL   m_TLM;
+
+    /* whether the Ttlmi field in a TLM marker is a byte (otherwise a uint16) */
+    OPJ_BOOL   m_Ttlmi_is_byte;
+
     /**
     locate the start position of the TLM marker
     after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
@@ -529,8 +550,17 @@ typedef struct opj_j2k_enc {
     OPJ_BYTE * m_header_tile_data;
 
     /* size of the encoded_data */
+
     OPJ_UINT32 m_header_tile_data_size;
 
+    /* whether to generate PLT markers */
+    OPJ_BOOL   m_PLT;
+
+    /* reserved bytes in m_encoded_tile_size for PLT markers */
+    OPJ_UINT32 m_reserved_bytes_for_PLT;
+
+    /** Number of components */
+    OPJ_UINT32 m_nb_comps;
 
 } opj_j2k_enc_t;
 
@@ -569,17 +599,23 @@ typedef struct opj_j2k {
     /** helper used to write the index file */
     opj_codestream_index_t *cstr_index;
 
-    /** number of the tile curently concern by coding/decoding */
+    /** number of the tile currently concern by coding/decoding */
     OPJ_UINT32 m_current_tile_number;
 
     /** the current tile coder/decoder **/
     struct opj_tcd *    m_tcd;
 
-    /** Number of threads to use */
-    int m_num_threads;
-
     /** Thread pool */
     opj_thread_pool_t* m_tp;
+
+    /** Image width coming from JP2 IHDR box. 0 from a pure codestream */
+    OPJ_UINT32 ihdr_w;
+
+    /** Image height coming from JP2 IHDR box. 0 from a pure codestream */
+    OPJ_UINT32 ihdr_h;
+
+    /** Set to 1 by the decoder initialization if OPJ_DPARAMETERS_DUMP_FLAG is set */
+    unsigned int dump_state;
 }
 opj_j2k_t;
 
@@ -598,6 +634,8 @@ Decoding parameters are returned in j2k->cp.
 */
 void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
 
+void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict);
+
 OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads);
 
 /**
@@ -616,7 +654,7 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
 /**
 Converts an enum type progression order to string type
 */
-char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
+const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
 
 /* ----------------------------------------------------------------------- */
 /*@}*/
@@ -704,6 +742,21 @@ OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
                                   opj_event_mgr_t * p_manager);
 
 
+/** Sets the indices of the components to decode.
+ *
+ * @param p_j2k         the jpeg2000 codec.
+ * @param numcomps      Number of components to decode.
+ * @param comps_indices Array of num_compts indices (numbering starting at 0)
+ *                      corresponding to the components to decode.
+ * @param p_manager     Event manager
+ *
+ * @return OPJ_TRUE in case of success.
+ */
+OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
+                                        OPJ_UINT32 numcomps,
+                                        const OPJ_UINT32* comps_indices,
+                                        opj_event_mgr_t * p_manager);
+
 /**
  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
  *
@@ -735,7 +788,7 @@ opj_j2k_t* opj_j2k_create_decompress(void);
  * Dump some elements from the J2K decompression structure .
  *
  *@param p_j2k              the jpeg2000 codec.
- *@param flag               flag to describe what elments are dump.
+ *@param flag               flag to describe what elements are dump.
  *@param out_stream         output stream where dump the elements.
  *
 */
@@ -805,6 +858,19 @@ OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
         OPJ_UINT32 res_factor,
         opj_event_mgr_t * p_manager);
 
+/**
+ * Specify extra options for the encoder.
+ *
+ * @param  p_j2k        the jpeg2000 codec.
+ * @param  p_options    options
+ * @param  p_manager    the user event manager
+ *
+ * @see opj_encoder_set_extra_options() for more details.
+ */
+OPJ_BOOL opj_j2k_encoder_set_extra_options(
+    opj_j2k_t *p_j2k,
+    const char* const* p_options,
+    opj_event_mgr_t * p_manager);
 
 /**
  * Writes a tile.
@@ -855,4 +921,4 @@ OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
 OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image);
 
 
-#endif /* __J2K_H */
+#endif /* OPJ_J2K_H */