Home | History | Annotate | Download | only in libopenjpeg20
      1 /*
      2  * The copyright in this software is being made available under the 2-clauses
      3  * BSD License, included below. This software may be subject to other third
      4  * party and contributor rights, including patent rights, and no such rights
      5  * are granted under this license.
      6  *
      7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
      8  * Copyright (c) 2002-2014, Professor Benoit Macq
      9  * Copyright (c) 2001-2003, David Janssens
     10  * Copyright (c) 2002-2003, Yannick Verschueren
     11  * Copyright (c) 2003-2007, Francois-Olivier Devaux
     12  * Copyright (c) 2003-2014, Antonin Descampe
     13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
     14  * Copyright (c) 2012, Carl Hetherington
     15  * Copyright (c) 2017, IntoPIX SA <support (at) intopix.com>
     16  * All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
     28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 #ifndef OPJ_T1_H
     40 #define OPJ_T1_H
     41 /**
     42 @file t1.h
     43 @brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)
     44 
     45 The functions in T1.C have for goal to realize the tier-1 coding operation. The functions
     46 in T1.C are used by some function in TCD.C.
     47 */
     48 
     49 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
     50 /*@{*/
     51 
     52 /* ----------------------------------------------------------------------- */
     53 #define T1_NMSEDEC_BITS 7
     54 
     55 #define T1_NUMCTXS_ZC  9
     56 #define T1_NUMCTXS_SC  5
     57 #define T1_NUMCTXS_MAG 3
     58 #define T1_NUMCTXS_AGG 1
     59 #define T1_NUMCTXS_UNI 1
     60 
     61 #define T1_CTXNO_ZC  0
     62 #define T1_CTXNO_SC  (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
     63 #define T1_CTXNO_MAG (T1_CTXNO_SC+T1_NUMCTXS_SC)
     64 #define T1_CTXNO_AGG (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
     65 #define T1_CTXNO_UNI (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
     66 #define T1_NUMCTXS   (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
     67 
     68 #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
     69 
     70 #define T1_TYPE_MQ 0    /**< Normal coding using entropy coder */
     71 #define T1_TYPE_RAW 1   /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/
     72 
     73 /* BEGINNING of flags that apply to opj_flag_t */
     74 /** We hold the state of individual data points for the T1 encoder using
     75  *  a single 32-bit flags word to hold the state of 4 data points.  This corresponds
     76  *  to the 4-point-high columns that the data is processed in.
     77  *
     78  *  These \#defines declare the layout of a 32-bit flags word.
     79  *
     80  *  This is currently done for encoding only.
     81  *  The values must NOT be changed, otherwise this is going to break a lot of
     82  *  assumptions.
     83  */
     84 
     85 /* SIGMA: significance state (3 cols x 6 rows)
     86  * CHI:   state for negative sample value (1 col x 6 rows)
     87  * MU:    state for visited in refinement pass (1 col x 4 rows)
     88  * PI:    state for visited in significance pass (1 col * 4 rows)
     89  */
     90 
     91 #define T1_SIGMA_0  (1U << 0)
     92 #define T1_SIGMA_1  (1U << 1)
     93 #define T1_SIGMA_2  (1U << 2)
     94 #define T1_SIGMA_3  (1U << 3)
     95 #define T1_SIGMA_4  (1U << 4)
     96 #define T1_SIGMA_5  (1U << 5)
     97 #define T1_SIGMA_6  (1U << 6)
     98 #define T1_SIGMA_7  (1U << 7)
     99 #define T1_SIGMA_8  (1U << 8)
    100 #define T1_SIGMA_9  (1U << 9)
    101 #define T1_SIGMA_10 (1U << 10)
    102 #define T1_SIGMA_11 (1U << 11)
    103 #define T1_SIGMA_12 (1U << 12)
    104 #define T1_SIGMA_13 (1U << 13)
    105 #define T1_SIGMA_14 (1U << 14)
    106 #define T1_SIGMA_15 (1U << 15)
    107 #define T1_SIGMA_16 (1U << 16)
    108 #define T1_SIGMA_17 (1U << 17)
    109 
    110 #define T1_CHI_0    (1U << 18)
    111 #define T1_CHI_0_I  18
    112 #define T1_CHI_1    (1U << 19)
    113 #define T1_CHI_1_I  19
    114 #define T1_MU_0     (1U << 20)
    115 #define T1_PI_0     (1U << 21)
    116 #define T1_CHI_2    (1U << 22)
    117 #define T1_CHI_2_I  22
    118 #define T1_MU_1     (1U << 23)
    119 #define T1_PI_1     (1U << 24)
    120 #define T1_CHI_3    (1U << 25)
    121 #define T1_MU_2     (1U << 26)
    122 #define T1_PI_2     (1U << 27)
    123 #define T1_CHI_4    (1U << 28)
    124 #define T1_MU_3     (1U << 29)
    125 #define T1_PI_3     (1U << 30)
    126 #define T1_CHI_5    (1U << 31)
    127 #define T1_CHI_5_I  31
    128 
    129 /** As an example, the bits T1_SIGMA_3, T1_SIGMA_4 and T1_SIGMA_5
    130  *  indicate the significance state of the west neighbour of data point zero
    131  *  of our four, the point itself, and its east neighbour respectively.
    132  *  Many of the bits are arranged so that given a flags word, you can
    133  *  look at the values for the data point 0, then shift the flags
    134  *  word right by 3 bits and look at the same bit positions to see the
    135  *  values for data point 1.
    136  *
    137  *  The \#defines below help a bit with this; say you have a flags word
    138  *  f, you can do things like
    139  *
    140  *  (f & T1_SIGMA_THIS)
    141  *
    142  *  to see the significance bit of data point 0, then do
    143  *
    144  *  ((f >> 3) & T1_SIGMA_THIS)
    145  *
    146  *  to see the significance bit of data point 1.
    147  */
    148 
    149 #define T1_SIGMA_NW   T1_SIGMA_0
    150 #define T1_SIGMA_N    T1_SIGMA_1
    151 #define T1_SIGMA_NE   T1_SIGMA_2
    152 #define T1_SIGMA_W    T1_SIGMA_3
    153 #define T1_SIGMA_THIS T1_SIGMA_4
    154 #define T1_SIGMA_E    T1_SIGMA_5
    155 #define T1_SIGMA_SW   T1_SIGMA_6
    156 #define T1_SIGMA_S    T1_SIGMA_7
    157 #define T1_SIGMA_SE   T1_SIGMA_8
    158 #define T1_SIGMA_NEIGHBOURS (T1_SIGMA_NW | T1_SIGMA_N | T1_SIGMA_NE | T1_SIGMA_W | T1_SIGMA_E | T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE)
    159 
    160 #define T1_CHI_THIS   T1_CHI_1
    161 #define T1_CHI_THIS_I T1_CHI_1_I
    162 #define T1_MU_THIS    T1_MU_0
    163 #define T1_PI_THIS    T1_PI_0
    164 #define T1_CHI_S      T1_CHI_2
    165 
    166 #define T1_LUT_SGN_W (1U << 0)
    167 #define T1_LUT_SIG_N (1U << 1)
    168 #define T1_LUT_SGN_E (1U << 2)
    169 #define T1_LUT_SIG_W (1U << 3)
    170 #define T1_LUT_SGN_N (1U << 4)
    171 #define T1_LUT_SIG_E (1U << 5)
    172 #define T1_LUT_SGN_S (1U << 6)
    173 #define T1_LUT_SIG_S (1U << 7)
    174 /* END of flags that apply to opj_flag_t */
    175 
    176 /* ----------------------------------------------------------------------- */
    177 
    178 /** Flags for 4 consecutive rows of a column */
    179 typedef OPJ_UINT32 opj_flag_t;
    180 
    181 /**
    182 Tier-1 coding (coding of code-block coefficients)
    183 */
    184 typedef struct opj_t1 {
    185 
    186     /** MQC component */
    187     opj_mqc_t mqc;
    188 
    189     OPJ_INT32  *data;
    190     /** Flags used by decoder and encoder.
    191      * Such that flags[1+0] is for state of col=0,row=0..3,
    192        flags[1+1] for col=1, row=0..3, flags[1+flags_stride] for col=0,row=4..7, ...
    193        This array avoids too much cache trashing when processing by 4 vertical samples
    194        as done in the various decoding steps. */
    195     opj_flag_t *flags;
    196 
    197     OPJ_UINT32 w;
    198     OPJ_UINT32 h;
    199     OPJ_UINT32 datasize;
    200     OPJ_UINT32 flagssize;
    201     OPJ_UINT32 data_stride;
    202     OPJ_BOOL   encoder;
    203 
    204     /* Thre 3 variables below are only used by the decoder */
    205     /* set to TRUE in multithreaded context */
    206     OPJ_BOOL     mustuse_cblkdatabuffer;
    207     /* Temporary buffer to concatenate all chunks of a codebock */
    208     OPJ_BYTE    *cblkdatabuffer;
    209     /* Maximum size available in cblkdatabuffer */
    210     OPJ_UINT32   cblkdatabuffersize;
    211 } opj_t1_t;
    212 
    213 /** @name Exported functions */
    214 /*@{*/
    215 /* ----------------------------------------------------------------------- */
    216 
    217 /**
    218 Encode the code-blocks of a tile
    219 @param t1 T1 handle
    220 @param tile The tile to encode
    221 @param tcp Tile coding parameters
    222 @param mct_norms  FIXME DOC
    223 @param mct_numcomps Number of components used for MCT
    224 */
    225 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
    226                              opj_tcd_tile_t *tile,
    227                              opj_tcp_t *tcp,
    228                              const OPJ_FLOAT64 * mct_norms,
    229                              OPJ_UINT32 mct_numcomps);
    230 
    231 /**
    232 Decode the code-blocks of a tile
    233 @param tcd TCD handle
    234 @param pret Pointer to return value
    235 @param tilec The tile to decode
    236 @param tccp Tile coding parameters
    237 @param p_manager the event manager
    238 @param p_manager_mutex mutex for the event manager
    239 @param check_pterm whether PTERM correct termination should be checked
    240 */
    241 void opj_t1_decode_cblks(opj_tcd_t* tcd,
    242                          volatile OPJ_BOOL* pret,
    243                          opj_tcd_tilecomp_t* tilec,
    244                          opj_tccp_t* tccp,
    245                          opj_event_mgr_t *p_manager,
    246                          opj_mutex_t* p_manager_mutex,
    247                          OPJ_BOOL check_pterm);
    248 
    249 
    250 
    251 /**
    252  * Creates a new Tier 1 handle
    253  * and initializes the look-up tables of the Tier-1 coder/decoder
    254  * @return a new T1 handle if successful, returns NULL otherwise
    255 */
    256 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder);
    257 
    258 /**
    259  * Destroys a previously created T1 handle
    260  *
    261  * @param p_t1 Tier 1 handle to destroy
    262 */
    263 void opj_t1_destroy(opj_t1_t *p_t1);
    264 /* ----------------------------------------------------------------------- */
    265 /*@}*/
    266 
    267 /*@}*/
    268 
    269 #endif /* OPJ_T1_H */
    270