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 #ifndef DISABLE_SEI 217 /** pointer to current sei parameters */ 218 sei_params_t *ps_sei; 219 #endif 220 221 /** pointer to current slice header parameters */ 222 slice_header_t *ps_slice_hdr; 223 224 /** pointer to frame level ctb structures prepared by main encode loop */ 225 ctb_enc_loop_out_t *ps_frm_ctb; 226 227 /** 228 * array to store cu level qp for entire 64x64 ctb 229 */ 230 WORD32 ai4_8x8_cu_qp[64]; 231 232 /** 233 * flag to check if cbf all tu in a given cu is zero 234 */ 235 WORD32 i4_is_cu_cbf_zero; 236 237 /** 238 * flag to enable / disbale residue encoding (used for RD opt bits estimate mode) 239 */ 240 WORD32 i4_enable_res_encode; 241 242 /* flag to enable/disable insertion of SPS, VPS, PPS at CRA pictures */ 243 WORD32 i4_sps_at_cdr_enable; 244 245 /* quantization group position variables which stores the aligned position */ 246 WORD32 i4_qg_pos_x; 247 248 WORD32 i4_qg_pos_y; 249 250 void *pv_tile_params_base; 251 252 s_pic_level_acc_info_t *ps_pic_level_info; 253 254 void *pv_sys_api; 255 256 /* Flag to control dependent slices. 257 0: Disable all slice segment limits 258 1: Enforce max number of CTBs (not supported) 259 2: Enforce max number of bytes */ 260 WORD32 i4_slice_segment_mode; 261 262 /* Max number of CTBs/bytes in encoded slice. Will be used only when 263 i4_slice_mode_enable is set to 1 or 2 in configuration file. This parameter is 264 used for limiting the size of encoded slice under user-configured value */ 265 WORD32 i4_slice_segment_max_length; 266 267 /* Accumulated number of CTBs/bytes in current slice */ 268 WORD32 i4_slice_seg_len; 269 270 /** Number of slice segments generated per picture 271 this parameter is to track the number of slices generated 272 and comapre aganist MAX NUM VCL Nals allowed at a given level */ 273 WORD32 i4_num_slice_seg; 274 275 /** Codec Level */ 276 WORD32 i4_codec_level; 277 278 /** 279 * number of neigbour cus coded as skips; Cannot exceed 2 (1 left, 1 top) 280 */ 281 WORD32 i4_num_nbr_skip_cus; 282 283 void *pv_dummy_out_buf; 284 285 WORD32 i4_bitstream_buf_size; 286 } entropy_context_t; 287 288 /*****************************************************************************/ 289 /* Extern Function Declarations */ 290 /*****************************************************************************/ 291 WORD32 ihevce_encode_transform_tree( 292 entropy_context_t *ps_entropy_ctxt, 293 WORD32 x0_ctb, 294 WORD32 y0_ctb, 295 WORD32 log2_tr_size, 296 WORD32 tr_depth, 297 WORD32 blk_num, 298 cu_enc_loop_out_t *ps_enc_cu); 299 300 WORD32 ihevce_cabac_residue_encode( 301 entropy_context_t *ps_entropy_ctxt, void *pv_coeff, WORD32 log2_tr_size, WORD32 is_luma); 302 303 WORD32 ihevce_cabac_residue_encode_rdopt( 304 entropy_context_t *ps_entropy_ctxt, 305 void *pv_coeff, 306 WORD32 log2_tr_size, 307 WORD32 is_luma, 308 WORD32 perform_rdoq); 309 310 WORD32 ihevce_cabac_residue_encode_rdoq( 311 entropy_context_t *ps_entropy_ctxt, 312 void *pv_coeff, 313 WORD32 log2_tr_size, 314 WORD32 is_luma, 315 void *ps_rdoq_ctxt_1, 316 LWORD64 *pi8_tu_coded_dist, 317 LWORD64 *pi8_not_coded_dist, 318 WORD32 perform_sbh); 319 320 WORD32 ihevce_find_new_last_csb( 321 WORD32 *pi4_subBlock2csbfId_map, 322 WORD32 cur_last_csb_pos, 323 void *pv_rdoq_ctxt, 324 UWORD8 *pu1_trans_table, 325 UWORD8 *pu1_csb_table, 326 WORD16 *pi2_coeffs, 327 WORD32 shift_value, 328 WORD32 mask_value, 329 UWORD8 **ppu1_addr); 330 331 WORD32 ihevce_code_all_sig_coeffs_as_0_explicitly( 332 void *pv_rdoq_ctxt, 333 WORD32 i, 334 UWORD8 *pu1_trans_table, 335 WORD32 is_luma, 336 WORD32 scan_type, 337 WORD32 infer_coeff, 338 WORD32 nbr_csbf, 339 cab_ctxt_t *ps_cabac); 340 341 void ihevce_copy_backup_ctxt( 342 void *pv_dest, void *pv_src, void *pv_backup_ctxt_dest, void *pv_backup_ctxt_src); 343 344 WORD32 ihevce_cabac_encode_coding_unit( 345 entropy_context_t *ps_entropy_ctxt, 346 cu_enc_loop_out_t *ps_enc_cu, 347 WORD32 cu_depth, 348 WORD32 top_avail, 349 WORD32 left_avail); 350 351 WORD32 ihevce_encode_slice_data( 352 entropy_context_t *ps_entropy_ctxt, 353 ihevce_tile_params_t *ps_tile_params, 354 WORD32 *pi4_end_of_slice_flag); 355 356 WORD32 ihevce_cabac_encode_sao( 357 entropy_context_t *ps_entropy_ctxt, ctb_enc_loop_out_t *ps_ctb_enc_loop_out); 358 359 #endif /* _IHEVCE_ENTROPY_STRUCTS_H_ */ 360