ih264e_cabac.h revision 3749f6f435e79624f72841e866245d84195551cd
1/****************************************************************************** 2 * 3 * Copyright (C) 2015 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 24 * ih264e_cabac_structs.h 25 * 26 * @brief 27 * This file contains cabac related macros, enums, tables and function declarations. 28 * 29 * @author 30 * Doney Alex 31 * 32 * @remarks 33 * none 34 * 35 ******************************************************************************* 36 */ 37 38#ifndef IH264E_CABAC_H_ 39#define IH264E_CABAC_H_ 40 41 42 43/******************************************************************************* 44@brief Bit precision of cabac engine; 45******************************************************************************* 46*/ 47#define CABAC_BITS 9 48 49 50 51 52/** 53****************************************************************************** 54 * @macro Count number of bits set 55****************************************************************************** 56*/ 57#define REV_NBITS(word, size, rev_word) \ 58{ \ 59 WORD32 i; \ 60 rev_word = 0; \ 61 for (i = 0; i < (size); i++) \ 62 { \ 63 UWORD32 bit = ((word) >> i) & 1; \ 64 rev_word += (1 << ((size) - i - 1)) * bit; \ 65 } \ 66} \ 67 68/** 69****************************************************************************** 70 * @macro Reverse bits in an unsigned integer 71****************************************************************************** 72*/ 73#define REV(u4_input, u4_output) \ 74{ \ 75 UWORD32 u4_temp = (u4_input); \ 76 WORD8 i; \ 77 u4_output = 0; \ 78 for (i = 0; i < 32; i++) \ 79 { \ 80 u4_output = (u4_output << 1) + \ 81 ((u4_temp >> i) & 0x01); \ 82 } \ 83} 84 85/** 86****************************************************************************** 87*! Bit manipulation macros 88****************************************************************************** 89*/ 90#define SETBIT(a, i) ((a) |= (1 << (i))) 91#define CLEARBIT(a, i) ((a) &= ~(1 << (i))) 92 93 94/** 95****************************************************************************** 96*! Cabac module expect atlesat MIN_STREAM_SIZE_MB bytes left in stream buffer 97*! for encoding an MB 98****************************************************************************** 99*/ 100#define MIN_STREAM_SIZE_MB 1024 101 102 103 104/*****************************************************************************/ 105/* Function Declarations */ 106/*****************************************************************************/ 107 108 109/** 110 ******************************************************************************* 111 * 112 * @brief 113 * Initialize default context values and pointers. 114 * 115 * @param[in] ps_ent_ctxt 116 * Pointer to entropy context structure 117 * 118 * @returns 119 * 120 * @remarks 121 * None 122 * 123 ******************************************************************************* 124 */ 125void ih264e_init_cabac_table(entropy_ctxt_t *ps_ent_ctxt); 126 127 128/** 129 ******************************************************************************* 130 * 131 * @brief 132 * Initialize cabac context: Intitalize all contest with init values given in the spec. 133 * Called at the beginning of entropy coding of each slice for CABAC encoding. 134 * 135 * @param[in] ps_ent_ctxt 136 * Pointer to entropy context structure 137 * 138 * @returns 139 * 140 * @remarks 141 * None 142 * 143 ******************************************************************************* 144 */ 145void ih264e_init_cabac_ctxt(entropy_ctxt_t *ps_ent_ctxt); 146 147 148 149/** 150 ******************************************************************************* 151 * 152 * @brief 153 * k-th order Exp-Golomb (UEGk) binarization process: Implements concatenated 154 * unary/ k-th order Exp-Golomb (UEGk) binarization process, 155 * where k = 0 as defined in 9.3.2.3 of ITU_T_H264-201402 156 * 157 * @param[in] i2_sufs 158 * Suffix bit string 159 * 160 * @param[in] pi1_bins_len 161 * Pointer to length of the string 162 * 163 * @returns Binarized value 164 * 165 * @remarks 166 * None 167 * 168 ******************************************************************************* 169 */ 170UWORD32 ih264e_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len); 171 172 173/** 174 ******************************************************************************* 175 * 176 * @brief 177 * Get cabac context for the MB :calculates the pointers to Top and left 178 * cabac neighbor context depending upon neighbor availability. 179 * 180 * @param[in] ps_ent_ctxt 181 * Pointer to entropy context structure 182 * 183 * @param[in] u4_mb_type 184 * Type of MB 185 * 186 * @returns 187 * 188 * @remarks 189 * None 190 * 191 ******************************************************************************* 192 */ 193void ih264e_get_cabac_context(entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type); 194 195 196/** 197 ******************************************************************************* 198 * @brief 199 * flushing at termination: Explained in flowchart 9-12(ITU_T_H264-201402). 200 * 201 * @param[in] ps_cabac_ctxt 202 * pointer to cabac context (handle) 203 * 204 * @returns success or failure error code 205 * 206 * @remarks 207 * None 208 * 209 ******************************************************************************* 210 */ 211WORD32 ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt); 212 213 214/** 215 ****************************************************************************** 216 * 217 * @brief Puts new byte (and outstanding bytes) into bitstream after cabac 218 * renormalization 219 * 220 * @par Description 221 * 1. Extract the leading byte of low(L) 222 * 2. If leading byte=0xff increment outstanding bytes and return 223 * (as the actual bits depend on carry propogation later) 224 * 3. If leading byte is not 0xff check for any carry propogation 225 * 4. Insert the carry (propogated in previous byte) along with outstanding 226 * bytes (if any) and leading byte 227 * 228 * 229 * @param[inout] ps_cabac_ctxt 230 * pointer to cabac context (handle) 231 * 232 * @return 233 * 234 ****************************************************************************** 235 */ 236void ih264e_cabac_put_byte(cabac_ctxt_t *ps_cabac_ctxt); 237 238 239/** 240 ****************************************************************************** 241 * 242 * @brief Codes a bin based on probablilty and mps packed context model 243 * 244 * @par Description 245 * 1. Apart from encoding bin, context model is updated as per state transition 246 * 2. Range and Low renormalization is done based on bin and original state 247 * 3. After renorm bistream is updated (if required) 248 * 249 * @param[inout] ps_cabac 250 * pointer to cabac context (handle) 251 * 252 * @param[in] bin 253 * bin(boolean) to be encoded 254 * 255 * @param[in] pu1_bin_ctxts 256 * index of cabac context model containing pState[bits 5-0] | MPS[bit6] 257 * 258 * @return 259 * 260 ****************************************************************************** 261 */ 262void ih264e_cabac_encode_bin(cabac_ctxt_t *ps_cabac, WORD32 bin, 263 bin_ctxt_model *pu1_bin_ctxts); 264 265 266 267/** 268 ******************************************************************************* 269 * 270 * @brief 271 * Encoding process for a binary decision :implements encoding process of a decision 272 * as defined in 9.3.4.2 . This function encodes multiple bins, of a symbol. Implements 273 * flowchart Figure 9-7( ITU_T_H264-201402) 274 * 275 * @param[in] u4_bins 276 * array of bin values 277 * 278 * @param[in] i1_bins_len 279 * Length of bins, maximum 32 280 * 281 * @param[in] u4_ctx_inc 282 * CtxInc, byte0- bin0, byte1-bin1 .. 283 * 284 * @param[in] i1_valid_len 285 * valid length of bins, after that CtxInc is constant 286 * 287 * @param[in] pu1_bin_ctxt_type 288 * Pointer to binary contexts 289 290 * @param[in] ps_cabac 291 * Pointer to cabac_context_structure 292 * 293 * @returns 294 * 295 * @remarks 296 * None 297 * 298 ******************************************************************************* 299 */ 300void ih264e_encode_decision_bins(UWORD32 u4_bins, WORD8 i1_bins_len, 301 UWORD32 u4_ctx_inc, WORD8 i1_valid_len, 302 bin_ctxt_model *pu1_bin_ctxt_type, 303 cabac_ctxt_t *ps_cabac); 304 305/** 306 ******************************************************************************* 307 * @brief 308 * Encoding process for a binary decision before termination:Encoding process 309 * of a termination(9.3.4.5 :ITU_T_H264-201402) . Explained in flowchart 9-11. 310 * 311 * @param[in] ps_cabac 312 * Pointer to cabac structure 313 * 314 * @param[in] term_bin 315 * Symbol value, end of slice or not, term_bin is binary 316 * 317 * @returns 318 * 319 * @remarks 320 * None 321 * 322 ******************************************************************************* 323 */ 324void ih264e_cabac_encode_terminate(cabac_ctxt_t *ps_cabac, WORD32 term_bin); 325 326 327/** 328 ******************************************************************************* 329 * @brief 330 * Bypass encoding process for binary decisions: Explained (9.3.4.4 :ITU_T_H264-201402) 331 * , flowchart 9-10. 332 * 333 * @param[in] ps_cabac : pointer to cabac context (handle) 334 * 335 * @param[in] bin : bypass bin(0/1) to be encoded 336 * 337 * @returns 338 * 339 * @remarks 340 * None 341 * 342 ******************************************************************************* 343 */ 344 345void ih264e_cabac_encode_bypass_bin(cabac_ctxt_t *ps_cabac, WORD32 bin); 346 347 348 349/** 350 ****************************************************************************** 351 * 352 * @brief Encodes a series of bypass bins (FLC bypass bins) 353 * 354 * @par Description 355 * This function is more optimal than calling ih264e_cabac_encode_bypass_bin() 356 * in a loop as cabac low, renorm and generating the stream (8bins at a time) 357 * can be done in one operation 358 * 359 * @param[inout]ps_cabac 360 * pointer to cabac context (handle) 361 * 362 * @param[in] u4_bins 363 * syntax element to be coded (as FLC bins) 364 * 365 * @param[in] num_bins 366 * This is the FLC length for u4_sym 367 * 368 * @return 369 * 370 ****************************************************************************** 371 */ 372 373void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins, 374 WORD32 num_bins); 375 376 377 378 379 380/** 381 ******************************************************************************* 382 * 383 * @brief 384 * This function generates CABAC coded bit stream for an Intra Slice. 385 * 386 * @description 387 * The mb syntax layer for intra slices constitutes luma mb mode, luma sub modes 388 * (if present), mb qp delta, coded block pattern, chroma mb mode and 389 * luma/chroma residue. These syntax elements are written as directed by table 390 * 7.3.5 of h264 specification. 391 * 392 * @param[in] ps_ent_ctxt 393 * pointer to entropy context 394 * 395 * @returns error code 396 * 397 * @remarks none 398 * 399 ******************************************************************************* 400 */ 401IH264E_ERROR_T ih264e_write_islice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt); 402 403 404/** 405 ******************************************************************************* 406 * 407 * @brief 408 * This function generates CABAC coded bit stream for Inter slices 409 * 410 * @description 411 * The mb syntax layer for inter slices constitutes luma mb mode, luma sub modes 412 * (if present), mb qp delta, coded block pattern, chroma mb mode and 413 * luma/chroma residue. These syntax elements are written as directed by table 414 * 7.3.5 of h264 specification 415 * 416 * @param[in] ps_ent_ctxt 417 * pointer to entropy context 418 * 419 * @returns error code 420 * 421 * @remarks none 422 * 423 ******************************************************************************* 424 */ 425IH264E_ERROR_T ih264e_write_pslice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt); 426 427 428/** 429 ******************************************************************************* 430 * 431 * @brief 432 * This function generates CABAC coded bit stream for B slices 433 * 434 * @description 435 * The mb syntax layer for inter slices constitutes luma mb mode, 436 * mb qp delta, coded block pattern, chroma mb mode and 437 * luma/chroma residue. These syntax elements are written as directed by table 438 * 7.3.5 of h264 specification 439 * 440 * @param[in] ps_ent_ctxt 441 * pointer to entropy context 442 * 443 * @returns error code 444 * 445 * @remarks none 446 * 447 ******************************************************************************* 448 */ 449IH264E_ERROR_T ih264e_write_bslice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt); 450 451 452#endif /* IH264E_CABAC_H_ */ 453