Home | History | Annotate | Download | only in encoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2018 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 /**
     21 *******************************************************************************
     22 * @file
     23 *  ihevc_rdoq_macros.h
     24 *
     25 * @brief
     26 *  Macros used for RDOQ algorthm
     27 *
     28 * @author
     29 *  Ittiam
     30 *
     31 * @remarks
     32 *  None
     33 *
     34 *******************************************************************************
     35 */
     36 #ifndef IHEVC_RDOQ_MACROS_H_
     37 #define IHEVC_RDOQ_MACROS_H_
     38 
     39 /*****************************************************************************/
     40 /* Constant Macros                                                           */
     41 /*****************************************************************************/
     42 /*Used for calculating the distortion in the transform domain*/
     43 #define CALC_SSD_IN_TRANS_DOMAIN(a, b, i4_round_val, i4_shift_val)                                 \
     44     (SHR_NEG(((((a) - (b)) * ((a) - (b))) + i4_round_val), i4_shift_val))
     45 #define CALC_CUMMUL_SSD_IN_TRANS_DOMAIN(a, b, i4_round_val, i4_shift_val)                          \
     46     (SHR_NEG((((a) - (b)) + i4_round_val), i4_shift_val))
     47 
     48 #define MAX_INT 0x7FFFFFFF
     49 
     50 #define COMPUTE_RATE_COST_CLIP30_RDOQ(r, l, qshift)                                                \
     51     ((WORD32)CLIP30((((ULWORD64)r) * ((ULWORD64)l)) >> (qshift)))
     52 
     53 /*This macro is required to test the RDOQ changes*/
     54 /*1 implies cabac context validation using the test-bench*/
     55 /*Also prints some debug information*/
     56 #define TEST_BENCH_RDOQ 0
     57 
     58 /*Macro to enable and disable coefficient RDOQ. When 1, coefficient RDOQ is enabled*/
     59 #define COEFF_RDOQ 0
     60 
     61 /*Macro to optimize the copying of cabac states across various temp/scratch cabac contexts
     62   Should always be 0 when COEFF_RDOQ is 1*/
     63 #define OPT_MEMCPY 1
     64 
     65 /** Macro which accounts subtracts 4096 bits from the total bits generated per TU in the RDOPT stage
     66     if SBH is on*/
     67 #define ACCOUNT_SIGN_BITS 0
     68 
     69 /*Macro defining the maximum number of context elements in the cabac state*/
     70 //#define MAX_NUM_CONTEXT_ELEMENTS 5
     71 
     72 /*****************************************************************************/
     73 /* Enum                                                                      */
     74 /*****************************************************************************/
     75 /*Enum to indicate which context element in the cabac state is currently being altered*/
     76 typedef enum
     77 {
     78     LASTXY,
     79     SUB_BLK_CODED_FLAG,
     80     SIG_COEFF,
     81     GRTR_THAN_1,
     82     GRTR_THAN_2,
     83     MAX_NUM_CONTEXT_ELEMENTS
     84 } BACKUP_CTXT_ELEMENTS;
     85 
     86 /*****************************************************************************/
     87 /* Structures                                                                */
     88 /*****************************************************************************/
     89 
     90 /*Structure defined to optimize copying of cabac states across various temporary/scratch cabac states*/
     91 typedef struct
     92 {
     93     // clang-format off
     94     /**
     95     ai4_ctxt_to_backup[x] tells us if xth element has been altered. where
     96     x       context element                 Meaning
     97     0       IHEVC_CAB_COEFFX_PREFIX         lastx last y has been coded
     98     1       IHEVC_CAB_CODED_SUBLK_IDX       sub-blk coded or not flag has been coded
     99     2       IHEVC_CAB_COEFF_FLAG            sigcoeff has been coded
    100     3       IHEVC_CAB_COEFABS_GRTR1_FLAG    greater than 1 bin has been coded
    101     4       IHEVC_CAB_COEFABS_GRTR2_FLAG    greater than 2 bin has been coded
    102     */
    103     // clang-format on
    104     UWORD8 au1_ctxt_to_backup[MAX_NUM_CONTEXT_ELEMENTS];
    105 
    106     /** Number of bits generated */
    107     WORD32 i4_num_bits;
    108 } backup_ctxt_t;
    109 
    110 /**
    111 ******************************************************************************
    112 * @brief Structure to store the position of the coefficient to be changed
    113          through SBH
    114 ******************************************************************************
    115  */
    116 typedef struct
    117 {
    118     UWORD8 x;
    119     UWORD8 y;
    120     UWORD8 is_valid_pos;
    121     WORD16 i2_old_coeff;
    122 } s_sbh_coeff_pos_t;
    123 
    124 /**
    125 ******************************************************************************
    126  *  @brief  RDOQ SBH context for cabac bit estimation etc
    127 ******************************************************************************
    128  */
    129 
    130 typedef struct
    131 {
    132     /** TU size */
    133     WORD32 i4_trans_size;
    134 
    135     /** Log 2 TU size */
    136     WORD32 i4_log2_trans_size;
    137 
    138     /**
    139      * Boolean value representing if the current TU is luma or not.
    140      * 1 => Luma
    141      */
    142     WORD32 i4_is_luma;
    143 
    144     /**
    145      * Calculate rounding and shifting values required for normalizing original
    146      * and inverse quantized transform coefficients (for calculation of SSD in
    147      * transform domain)
    148      */
    149     WORD32 i4_round_val_ssd_in_td;
    150     WORD32 i4_shift_val_ssd_in_td;
    151 
    152     /** Matrix used in inverse quantization */
    153     WORD32 quant_scale_mat_offset;
    154 
    155     /** Index of the csb within the TU*/
    156     WORD32 i4_trans_idx;
    157 
    158     /** value of lambda used in the D+Rlambda metric*/
    159     LWORD64 i8_cl_ssd_lambda_qf;
    160 
    161     /** Used while inverse quantizing*/
    162     WORD16 i2_qp_rem;
    163     WORD32 i4_qp_div;
    164 
    165     /** Scan index of the csbs within the TU   */
    166     WORD32 i4_scan_idx;
    167 
    168     /** Pointer to the csbf buf. This buffer will contain 1 if the csb is coded
    169      *  and 0 if it is not*/
    170     UWORD8 *pu1_csbf_buf;
    171 
    172     /** Boolean value which is 1 if any of the csbs in the current TU are
    173      * coded*/
    174     UWORD8 i1_tu_is_coded;
    175 
    176     /**
    177      * Pointer to an array of pointer to store the scaling matrices for
    178      * all transform sizes and qp % 6 (pre computed)
    179      */
    180     WORD16 *pi2_dequant_coeff;
    181 
    182     /** Pointer to the quantized coeffs*/
    183     WORD16 *pi2_quant_coeffs;
    184 
    185     /** Pointer to the inverse quantized values*/
    186     WORD16 *pi2_iquant_coeffs;
    187 
    188     /** Pointer ot the transformed values(before quantization) */
    189     WORD16 *pi2_trans_values;
    190 
    191     /** Stride of the inverse quant data*/
    192     WORD32 i4_iq_data_strd;
    193 
    194     /** Stride of the quant data*/
    195     WORD32 i4_q_data_strd;
    196 
    197     /** Intermediate array to store transform output for RDOQ*/
    198     WORD16 ai2_trans_values[MAX_TRANS_SIZE];
    199 
    200     /** Pointer to zero rows and zero cols*/
    201     WORD32 *pi4_zero_row;
    202     WORD32 *pi4_zero_col;
    203 
    204     /** Array containing information about the position of the coefficient
    205      * to be altered during SBH
    206      */
    207     s_sbh_coeff_pos_t s_best_pos[(MAX_TU_SIZE * MAX_TU_SIZE / 4 / 4) + 1];
    208 
    209     /** SSD cost for this particular TU*/
    210     LWORD64 i8_ssd_cost;
    211 
    212     WORD32 i4_perform_all_cand_rdoq;
    213     WORD32 i4_perform_best_cand_rdoq;
    214     WORD32 i4_perform_all_cand_sbh;
    215     WORD32 i4_perform_best_cand_sbh;
    216 
    217     WORD32 i4_bit_depth;
    218 
    219     WORD32 *pi4_subBlock2csbfId_map;
    220 
    221 } rdoq_sbh_ctxt_t;
    222 
    223 /*****************************************************************************/
    224 /* Extern Function Declarations                                              */
    225 /*****************************************************************************/
    226 
    227 void ihevce_sign_data_hiding(rdoq_sbh_ctxt_t *ps_rdoq_sbh_params);
    228 
    229 #endif /* IHEVC_RDOQ_MACROS_H_ */
    230