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 ******************************************************************************
     23 * @file ihevce_entropy_structs.h
     24 *
     25 * @brief
     26 *  This file contains encoder entropy context related structures and
     27 *  interface prototypes
     28 *
     29 * @author
     30 *  Ittiam
     31 ******************************************************************************
     32 */
     33 
     34 #ifndef _IHEVCE_ENTROPY_STRUCTS_H_
     35 #define _IHEVCE_ENTROPY_STRUCTS_H_
     36 
     37 /*****************************************************************************/
     38 /* Constant Macros                                                           */
     39 /*****************************************************************************/
     40 /**
     41 ******************************************************************************
     42  *  @brief  defines maximum transform depth in HEVC (32 to 4)
     43 ******************************************************************************
     44  */
     45 #define MAX_TFR_DEPTH 5
     46 
     47 /**
     48 ******************************************************************************
     49  *  @brief  defines maximum qp delta to be coded as truncated unary code
     50 ******************************************************************************
     51  */
     52 #define TU_MAX_QP_DELTA_ABS 5
     53 
     54 /**
     55 ******************************************************************************
     56  *  @brief  defines maximum value of context increment used for qp delta encode
     57 ******************************************************************************
     58  */
     59 #define CTXT_MAX_QP_DELTA_ABS 1
     60 
     61 /**
     62 ******************************************************************************
     63  *  @brief  header length in the compressed scan coeff buffer of a TU
     64 ******************************************************************************
     65  */
     66 #define COEFF_BUF_HEADER_LEN 4
     67 
     68 /**
     69 ******************************************************************************
     70  *  @brief   extracts the "bitpos" bit of a input variable x
     71 ******************************************************************************
     72  */
     73 #define EXTRACT_BIT(val, x, bitpos)                                                                \
     74     {                                                                                              \
     75         val = ((((x) >> (bitpos)) & 0x1));                                                         \
     76     }
     77 
     78 /**
     79 ******************************************************************************
     80  *  @brief   inserts bit y in "bitpos' position of input varaible x
     81 ******************************************************************************
     82  */
     83 #define INSERT_BIT(x, bitpos, y) ((x) |= ((y) << (bitpos)))
     84 
     85 /**
     86 ******************************************************************************
     87  *  @brief   sets n bits starting from "bitpos' position of input varaible x
     88 ******************************************************************************
     89  */
     90 #define SET_BITS(x, bitpos, n) ((x) |= (((1 << (n)) - 1) << (bitpos)))
     91 
     92 /**
     93 ******************************************************************************
     94  *  @brief   clears n bits starting from "bitpos' position of input varaible x
     95 ******************************************************************************
     96  */
     97 #define CLEAR_BITS(x, bitpos, n) ((x) &= (~(((1 << (n)) - 1) << (bitpos))))
     98 
     99 /*****************************************************************************/
    100 /* Enums                                                                     */
    101 /*****************************************************************************/
    102 
    103 /**
    104 ******************************************************************************
    105  *  @brief      Enumeration for memory records requested by entropy module
    106 ******************************************************************************
    107  */
    108 typedef enum
    109 {
    110     ENTROPY_CTXT = 0,
    111     ENTROPY_TOP_SKIP_FLAGS,
    112     ENTROPY_TOP_CU_DEPTH,
    113     ENTROPY_DUMMY_OUT_BUF,
    114 
    115     /* should always be the last entry */
    116     NUM_ENTROPY_MEM_RECS
    117 
    118 } IHEVCE_ENTROPY_MEM_TABS_T;
    119 
    120 /*****************************************************************************/
    121 /* Structures                                                                */
    122 /*****************************************************************************/
    123 
    124 /**
    125 ******************************************************************************
    126  *  @brief      Entropy context for encoder
    127 ******************************************************************************
    128  */
    129 typedef struct entropy_context
    130 {
    131     /** cabac engine context                                        */
    132     cab_ctxt_t s_cabac_ctxt;
    133 
    134     /** bitstream context                                           */
    135     bitstrm_t s_bit_strm;
    136 
    137     /**
    138      * duplicate bitstream to generate entry offset
    139      * to support entropy sync
    140      */
    141     bitstrm_t s_dup_bit_strm_ent_offset;
    142 
    143     /** pointer to top row cu skip flags (1 bit per 8x8CU)          */
    144     UWORD8 *pu1_skip_cu_top;
    145 
    146     /** pointer to top row cu depth buffer (1 byte per 8x8CU)       */
    147     UWORD8 *pu1_cu_depth_top;
    148 
    149     /** pointer to parent coded block flags based on trasform depth */
    150     UWORD8 *apu1_cbf_cb[2];
    151 
    152     /** pointer to parent coded block flags based on trasform depth */
    153     UWORD8 *apu1_cbf_cr[2];
    154 
    155     /** left cu skip flags  (max of 8) (1 bit per 8x8)              */
    156     UWORD32 u4_skip_cu_left;
    157 
    158     /** array of left cu skip flags  (max of 8) (1 byte per 8x8)    */
    159     UWORD8 au1_cu_depth_left[8];
    160 
    161     /** scratch array of cb coded block flags for tu recursion      */
    162     UWORD8 au1_cbf_cb[2][MAX_TFR_DEPTH + 1];
    163 
    164     /** scratch array of cr coded block flags for tu recursion      */
    165     UWORD8 au1_cbf_cr[2][MAX_TFR_DEPTH + 1];
    166 
    167     /** current ctb x offset w.r.t frame start */
    168     WORD32 i4_ctb_x;
    169 
    170     /** current ctb y offset w.r.t frame start */
    171     WORD32 i4_ctb_y;
    172 
    173     //These values are never consumed apart from test-bench. Observed on June16 2014.
    174     /** current slice first ctb x offset w.r.t frame start */
    175     /** current slice first ctb y offset w.r.t frame start */
    176     WORD32 i4_ctb_slice_x;
    177     WORD32 i4_ctb_slice_y;
    178 
    179     /** Address of first CTB of next slice segment. In ctb unit */
    180     WORD32 i4_next_slice_seg_x;
    181 
    182     /** Address of first CTB of next slice segment. In ctb unit */
    183     WORD32 i4_next_slice_seg_y;
    184 
    185     /** sracth place holder for cu index of a ctb in context */
    186     WORD32 i4_cu_idx;
    187 
    188     /** sracth place holder for tu index of a cu in context  */
    189     WORD32 i4_tu_idx;
    190 
    191     /** pcm not supported currently; this parameter shall be 0 */
    192     WORD8 i1_ctb_num_pcm_blks;
    193 
    194     /** indicates if qp delta is to be coded in trasform unit of a cu */
    195     WORD8 i1_encode_qp_delta;
    196 
    197     /** place holder for current qp of a cu */
    198     WORD8 i1_cur_qp;
    199 
    200     /** log2ctbsize  indicated in SPS */
    201     WORD8 i1_log2_ctb_size;
    202 
    203     /**************************************************************************/
    204     /* Following are shared structures with the encoder loop                  */
    205     /* entropy context is not the owner of these and hence not allocated here */
    206     /**************************************************************************/
    207     /** pointer to current vps parameters    */
    208     vps_t *ps_vps;
    209 
    210     /** pointer to current sps parameters    */
    211     sps_t *ps_sps;
    212 
    213     /** pointer to current pps parameters    */
    214     pps_t *ps_pps;
    215 
    216     /** pointer to current sei parameters    */
    217     sei_params_t *ps_sei;
    218 
    219     /** pointer to current slice header parameters    */
    220     slice_header_t *ps_slice_hdr;
    221 
    222     /** pointer to frame level ctb structures prepared by main encode loop   */
    223     ctb_enc_loop_out_t *ps_frm_ctb;
    224 
    225     /**
    226      * array to store cu level qp for entire 64x64 ctb
    227      */
    228     WORD32 ai4_8x8_cu_qp[64];
    229 
    230     /**
    231      * flag to check if cbf all tu in a given cu is zero
    232      */
    233     WORD32 i4_is_cu_cbf_zero;
    234 
    235     /**
    236      * flag to enable / disbale residue encoding (used for RD opt bits estimate mode)
    237      */
    238     WORD32 i4_enable_res_encode;
    239 
    240     /*  flag to enable/disable insertion of SPS, VPS, PPS at CRA pictures   */
    241     WORD32 i4_sps_at_cdr_enable;
    242 
    243     /* quantization group position variables which stores the aligned position */
    244     WORD32 i4_qg_pos_x;
    245 
    246     WORD32 i4_qg_pos_y;
    247 
    248     void *pv_tile_params_base;
    249 
    250     s_pic_level_acc_info_t *ps_pic_level_info;
    251 
    252     void *pv_sys_api;
    253 
    254     /*  Flag to control dependent slices.
    255     0: Disable all slice segment limits
    256     1: Enforce max number of CTBs (not supported)
    257     2: Enforce max number of bytes */
    258     WORD32 i4_slice_segment_mode;
    259 
    260     /* Max number of CTBs/bytes in encoded slice. Will be used only when
    261     i4_slice_mode_enable is set to 1 or 2 in configuration file. This parameter is
    262     used for limiting the size of encoded slice under user-configured value */
    263     WORD32 i4_slice_segment_max_length;
    264 
    265     /* Accumulated number of CTBs/bytes in current slice */
    266     WORD32 i4_slice_seg_len;
    267 
    268     /** Number of slice segments generated per picture
    269         this parameter is to track the number of slices generated
    270         and comapre aganist MAX NUM VCL Nals allowed at a given level */
    271     WORD32 i4_num_slice_seg;
    272 
    273     /** Codec Level */
    274     WORD32 i4_codec_level;
    275 
    276     /**
    277     * number of neigbour cus coded as skips; Cannot exceed 2 (1 left, 1 top)
    278     */
    279     WORD32 i4_num_nbr_skip_cus;
    280 
    281     void *pv_dummy_out_buf;
    282 
    283     WORD32 i4_bitstream_buf_size;
    284 } entropy_context_t;
    285 
    286 /*****************************************************************************/
    287 /* Extern Function Declarations                                              */
    288 /*****************************************************************************/
    289 WORD32 ihevce_encode_transform_tree(
    290     entropy_context_t *ps_entropy_ctxt,
    291     WORD32 x0_ctb,
    292     WORD32 y0_ctb,
    293     WORD32 log2_tr_size,
    294     WORD32 tr_depth,
    295     WORD32 blk_num,
    296     cu_enc_loop_out_t *ps_enc_cu);
    297 
    298 WORD32 ihevce_cabac_residue_encode(
    299     entropy_context_t *ps_entropy_ctxt, void *pv_coeff, WORD32 log2_tr_size, WORD32 is_luma);
    300 
    301 WORD32 ihevce_cabac_residue_encode_rdopt(
    302     entropy_context_t *ps_entropy_ctxt,
    303     void *pv_coeff,
    304     WORD32 log2_tr_size,
    305     WORD32 is_luma,
    306     WORD32 perform_rdoq);
    307 
    308 WORD32 ihevce_cabac_residue_encode_rdoq(
    309     entropy_context_t *ps_entropy_ctxt,
    310     void *pv_coeff,
    311     WORD32 log2_tr_size,
    312     WORD32 is_luma,
    313     void *ps_rdoq_ctxt_1,
    314     LWORD64 *pi8_tu_coded_dist,
    315     LWORD64 *pi8_not_coded_dist,
    316     WORD32 perform_sbh);
    317 
    318 WORD32 ihevce_find_new_last_csb(
    319     WORD32 *pi4_subBlock2csbfId_map,
    320     WORD32 cur_last_csb_pos,
    321     void *pv_rdoq_ctxt,
    322     UWORD8 *pu1_trans_table,
    323     UWORD8 *pu1_csb_table,
    324     WORD16 *pi2_coeffs,
    325     WORD32 shift_value,
    326     WORD32 mask_value,
    327     UWORD8 **ppu1_addr);
    328 
    329 WORD32 ihevce_code_all_sig_coeffs_as_0_explicitly(
    330     void *pv_rdoq_ctxt,
    331     WORD32 i,
    332     UWORD8 *pu1_trans_table,
    333     WORD32 is_luma,
    334     WORD32 scan_type,
    335     WORD32 infer_coeff,
    336     WORD32 nbr_csbf,
    337     cab_ctxt_t *ps_cabac);
    338 
    339 void ihevce_copy_backup_ctxt(
    340     void *pv_dest, void *pv_src, void *pv_backup_ctxt_dest, void *pv_backup_ctxt_src);
    341 
    342 WORD32 ihevce_cabac_encode_coding_unit(
    343     entropy_context_t *ps_entropy_ctxt,
    344     cu_enc_loop_out_t *ps_enc_cu,
    345     WORD32 cu_depth,
    346     WORD32 top_avail,
    347     WORD32 left_avail);
    348 
    349 WORD32 ihevce_encode_slice_data(
    350     entropy_context_t *ps_entropy_ctxt,
    351     ihevce_tile_params_t *ps_tile_params,
    352     WORD32 *pi4_end_of_slice_flag);
    353 
    354 WORD32 ihevce_cabac_encode_sao(
    355     entropy_context_t *ps_entropy_ctxt, ctb_enc_loop_out_t *ps_ctb_enc_loop_out);
    356 
    357 #endif /* _IHEVCE_ENTROPY_STRUCTS_H_ */
    358