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.c 25 * 26 * @brief 27 * Contains all functions to encode in CABAC entropy mode 28 * 29 * 30 * @author 31 * Doney Alex 32 * 33 * @par List of Functions: 34 * 35 * 36 * @remarks 37 * None 38 * 39 ******************************************************************************* 40 */ 41 42 /*****************************************************************************/ 43 /* File Includes */ 44 /*****************************************************************************/ 45 46 /* System include files */ 47 #include <stdio.h> 48 #include <assert.h> 49 #include <limits.h> 50 #include <string.h> 51 52 /* User include files */ 53 #include "ih264e_config.h" 54 #include "ih264_typedefs.h" 55 #include "iv2.h" 56 #include "ive2.h" 57 #include "ih264_debug.h" 58 #include "ih264_defs.h" 59 #include "ih264e_defs.h" 60 #include "ih264_macros.h" 61 #include "ih264e_error.h" 62 #include "ih264e_bitstream.h" 63 #include "ime_distortion_metrics.h" 64 #include "ime_defs.h" 65 #include "ime_structs.h" 66 #include "ih264_error.h" 67 #include "ih264_structs.h" 68 #include "ih264_trans_quant_itrans_iquant.h" 69 #include "ih264_inter_pred_filters.h" 70 #include "ih264_mem_fns.h" 71 #include "ih264_padding.h" 72 #include "ih264_platform_macros.h" 73 #include "ih264_intra_pred_filters.h" 74 #include "ih264_deblk_edge_filters.h" 75 #include "ih264_cabac_tables.h" 76 #include "irc_cntrl_param.h" 77 #include "irc_frame_info_collector.h" 78 #include "ih264e_rate_control.h" 79 #include "ih264e_cabac_structs.h" 80 #include "ih264e_structs.h" 81 #include "ih264e_cabac.h" 82 #include "ih264e_encode_header.h" 83 #include "ih264_cavlc_tables.h" 84 #include "ih264e_cavlc.h" 85 #include "ih264e_statistics.h" 86 #include "ih264e_trace.h" 87 88 /*****************************************************************************/ 89 /* Function Definitions */ 90 /*****************************************************************************/ 91 92 93 94 95 /** 96 ******************************************************************************* 97 * 98 * @brief 99 * Encodes mb_skip_flag using CABAC entropy coding mode. 100 * 101 * @param[in] u1_mb_skip_flag 102 * mb_skip_flag 103 * 104 * @param[in] ps_cabac_ctxt 105 * Pointer to cabac context structure 106 * 107 * @param[in] u4_ctxidx_offset 108 * ctxIdxOffset for mb_skip_flag context 109 * 110 * @returns 111 * 112 * @remarks 113 * None 114 * 115 ******************************************************************************* 116 */ 117 static void ih264e_cabac_enc_mb_skip(UWORD8 u1_mb_skip_flag, 118 cabac_ctxt_t *ps_cabac_ctxt, 119 UWORD32 u4_ctxidx_offset) 120 { 121 122 UWORD8 u4_ctx_inc; 123 WORD8 a, b; 124 a = ((ps_cabac_ctxt->ps_left_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? 125 0 : 1); 126 b = ((ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? 127 0 : 1); 128 129 u4_ctx_inc = a + b; 130 /* Encode the bin */ 131 ih264e_cabac_encode_bin(ps_cabac_ctxt, 132 (UWORD32) u1_mb_skip_flag, 133 ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctxidx_offset 134 + u4_ctx_inc); 135 136 } 137 138 139 /* ! < Table 9-36 Binarization for macroblock types in I slices in ITU_T_H264-201402 140 * Bits 0-7 : binarised value 141 * Bits 8-15: length of binary sequence 142 */ 143 static const UWORD32 u4_mb_type_intra[26] = 144 { 0x0100, 0x0620, 0x0621, 0x0622, 0x0623, 0x0748, 0x0749, 0x074a, 0x074b, 145 0x074c, 0x074d, 0x074e, 0x074f, 0x0628, 0x0629, 0x062a, 0x062b, 0x0758, 146 0x0759, 0x075a, 0x075b, 0x075c, 0x075d, 0x075e, 0x075f, 0x0203 }; 147 148 149 /* CtxInc for mb types */ 150 static const UWORD32 u4_mb_ctxinc[2][26] = 151 { 152 /* Intra CtxInc's */ 153 { 0x00, 154 0x03467, 0x03467, 0x03467, 0x03467, 0x034567, 0x034567, 0x034567, 155 0x034567, 0x034567, 0x034567, 0x034567, 0x034567, 0x03467, 0x03467, 156 0x03467, 0x03467, 0x034567, 0x034567, 0x034567, 0x034567, 0x034567, 157 0x034567, 0x034567, 0x034567, 0x00}, 158 /* Inter CtxInc's */ 159 { 0x00, 160 0x001233, 0x001233, 0x001233, 0x001233, 0x0012233, 0x0012233, 0x0012233, 161 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x001233, 0x001233, 162 0x001233, 0x001233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 163 0x0012233, 0x0012233, 0x0012233, 0x00} 164 }; 165 166 167 /** 168 ******************************************************************************* 169 * 170 * @brief 171 * Encodes mb_type for an intra MB. 172 * 173 * @param[in] u4_slice_type 174 * slice type 175 * 176 * @param[in] u4_intra_mb_type 177 * MB type (Table 7-11) 178 * 179 * @param[in] ps_cabac_ctxt 180 * Pointer to cabac context structure 181 * 182 ** @param[in] u4_ctxidx_offset 183 * ctxIdxOffset for mb_type context 184 * 185 * @returns 186 * 187 * @remarks 188 * None 189 * 190 ******************************************************************************* 191 */ 192 193 static void ih264e_cabac_enc_intra_mb_type(UWORD32 u4_slice_type, 194 UWORD32 u4_intra_mb_type, 195 cabac_ctxt_t *ps_cabac_ctxt, 196 UWORD32 u4_ctx_idx_offset) 197 { 198 199 encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env); 200 bin_ctxt_model *pu1_mb_bin_ctxt, *pu1_bin_ctxt; 201 UWORD8 u1_bin; 202 mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; 203 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 204 UWORD32 u4_bins; 205 UWORD32 u4_ctx_inc; 206 WORD8 i1_bins_len; 207 UWORD32 u4_code_int_range; 208 UWORD32 u4_code_int_low; 209 UWORD16 u2_quant_code_int_range; 210 UWORD16 u4_code_int_range_lps; 211 WORD8 i; 212 UWORD8 u1_ctx_inc; 213 UWORD32 u4_table_val; 214 215 pu1_mb_bin_ctxt = ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset; 216 217 u4_bins = u4_mb_type_intra[u4_intra_mb_type]; 218 i1_bins_len = (WORD8) ((u4_bins >> 8) & 0x0f); 219 u4_ctx_inc = u4_mb_ctxinc[(u4_slice_type != ISLICE)][u4_intra_mb_type]; 220 u1_ctx_inc = 0; 221 if (u4_slice_type == ISLICE) 222 { 223 if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 224 u1_ctx_inc += ((ps_left_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0); 225 if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 226 u1_ctx_inc += ((ps_top_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0); 227 228 u4_ctx_inc = (u4_ctx_inc | (u1_ctx_inc << ((i1_bins_len - 1) << 2))); 229 } 230 else 231 { 232 pu1_mb_bin_ctxt += 3; 233 if (u4_slice_type == BSLICE) 234 pu1_mb_bin_ctxt += 2; 235 236 } 237 238 u4_code_int_range = ps_cab_enc_env->u4_code_int_range; 239 u4_code_int_low = ps_cab_enc_env->u4_code_int_low; 240 241 for (i = (i1_bins_len - 1); i >= 0; i--) 242 { 243 WORD32 shift; 244 245 u1_ctx_inc = ((u4_ctx_inc >> (i << 2)) & 0x0f); 246 u1_bin = ((u4_bins >> i) & 0x01); 247 /* Encode the bin */ 248 pu1_bin_ctxt = pu1_mb_bin_ctxt + u1_ctx_inc; 249 if (i != (i1_bins_len - 2)) 250 { 251 WORD8 i1_mps = !!((*pu1_bin_ctxt) & (0x40)); 252 WORD8 i1_state = (*pu1_bin_ctxt) & 0x3F; 253 254 u2_quant_code_int_range = ((u4_code_int_range >> 6) & 0x03); 255 u4_table_val = 256 gau4_ih264_cabac_table[i1_state][u2_quant_code_int_range]; 257 u4_code_int_range_lps = u4_table_val & 0xFF; 258 259 u4_code_int_range -= u4_code_int_range_lps; 260 if (u1_bin != i1_mps) 261 { 262 u4_code_int_low += u4_code_int_range; 263 u4_code_int_range = u4_code_int_range_lps; 264 if (i1_state == 0) 265 { 266 /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */ 267 i1_mps = 1 - i1_mps; 268 } 269 270 i1_state = (u4_table_val >> 15) & 0x3F; 271 } 272 else 273 { 274 i1_state = (u4_table_val >> 8) & 0x3F; 275 276 } 277 278 (*pu1_bin_ctxt) = (i1_mps << 6) | i1_state; 279 } 280 else 281 { 282 u4_code_int_range -= 2; 283 } 284 285 /* Renormalize */ 286 /*****************************************************************/ 287 /* Renormalization; calculate bits generated based on range(R) */ 288 /* Note : 6 <= R < 512; R is 2 only for terminating encode */ 289 /*****************************************************************/ 290 GETRANGE(shift, u4_code_int_range); 291 shift = 9 - shift; 292 u4_code_int_low <<= shift; 293 u4_code_int_range <<= shift; 294 295 /* bits to be inserted in the bitstream */ 296 ps_cab_enc_env->u4_bits_gen += shift; 297 ps_cab_enc_env->u4_code_int_range = u4_code_int_range; 298 ps_cab_enc_env->u4_code_int_low = u4_code_int_low; 299 300 /* generate stream when a byte is ready */ 301 if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS) 302 { 303 ih264e_cabac_put_byte(ps_cabac_ctxt); 304 u4_code_int_range = ps_cab_enc_env->u4_code_int_range; 305 u4_code_int_low = ps_cab_enc_env->u4_code_int_low; 306 307 } 308 } 309 } 310 311 312 313 /** 314 ******************************************************************************* 315 * 316 * @brief 317 * Encodes prev_intra4x4_pred_mode_flag and 318 * rem_intra4x4_pred_mode using CABAC entropy coding mode 319 * 320 * @param[in] ps_cabac_ctxt 321 * Pointer to cabac context structure 322 * 323 * @param[in] pu1_intra_4x4_modes 324 * Pointer to array containing prev_intra4x4_pred_mode_flag and 325 * rem_intra4x4_pred_mode 326 * 327 * @returns 328 * 329 * @remarks 330 * None 331 * 332 ******************************************************************************* 333 */ 334 static void ih264e_cabac_enc_4x4mb_modes(cabac_ctxt_t *ps_cabac_ctxt, 335 UWORD8 *pu1_intra_4x4_modes) 336 { 337 WORD32 i; 338 WORD8 byte; 339 for (i = 0; i < 16; i += 2) 340 { 341 /* sub blk idx 1 */ 342 byte = *pu1_intra_4x4_modes++; 343 if (byte & 0x1) 344 { 345 ih264e_cabac_encode_bin(ps_cabac_ctxt, 346 1, 347 ps_cabac_ctxt->au1_cabac_ctxt_table 348 + PREV_INTRA4X4_PRED_MODE_FLAG); 349 } 350 else 351 { 352 /* Binarization is FL and Cmax=7 */ 353 ih264e_encode_decision_bins(byte & 0xF, 354 4, 355 0x05554, 356 4, 357 ps_cabac_ctxt->au1_cabac_ctxt_table 358 + REM_INTRA4X4_PRED_MODE - 5, 359 ps_cabac_ctxt); 360 } 361 /* sub blk idx 2 */ 362 byte >>= 4; 363 if (byte & 0x1) 364 { 365 ih264e_cabac_encode_bin(ps_cabac_ctxt, 366 1, 367 ps_cabac_ctxt->au1_cabac_ctxt_table 368 + PREV_INTRA4X4_PRED_MODE_FLAG); 369 } 370 else 371 { 372 ih264e_encode_decision_bins(byte & 0xF, 373 4, 374 0x05554, 375 4, 376 ps_cabac_ctxt->au1_cabac_ctxt_table 377 + REM_INTRA4X4_PRED_MODE - 5, 378 ps_cabac_ctxt); 379 } 380 } 381 } 382 383 384 385 /** 386 ******************************************************************************* 387 * 388 * @brief 389 * Encodes chroma intrapred mode for the MB. 390 * 391 * @param[in] u1_chroma_pred_mode 392 * Chroma intr prediction mode 393 * 394 * @param[in] ps_cabac_ctxt 395 * Pointer to cabac context structure 396 * 397 * @returns 398 * 399 * @remarks 400 * None 401 * 402 ******************************************************************************* 403 */ 404 static void ih264e_cabac_enc_chroma_predmode(UWORD8 u1_chroma_pred_mode, 405 cabac_ctxt_t *ps_cabac_ctxt) 406 { 407 408 WORD8 i1_temp; 409 mb_info_ctxt_t *ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 410 mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; 411 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 412 UWORD32 u4_bins = 0; 413 WORD8 i1_bins_len = 1; 414 UWORD32 u4_ctx_inc = 0; 415 UWORD8 a, b; 416 a = ((ps_left_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0); 417 b = ((ps_top_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0); 418 419 /* Binarization is TU and Cmax=3 */ 420 ps_curr_ctxt->u1_intrapred_chroma_mode = u1_chroma_pred_mode; 421 422 u4_ctx_inc = a + b; 423 u4_ctx_inc = (u4_ctx_inc | 0x330); 424 if (u1_chroma_pred_mode) 425 { 426 u4_bins = 1; 427 i1_temp = u1_chroma_pred_mode; 428 i1_temp--; 429 /* Put a stream of 1's of length Chromaps_pred_mode_ctxt value */ 430 while (i1_temp) 431 { 432 u4_bins = (u4_bins | (1 << i1_bins_len)); 433 i1_bins_len++; 434 i1_temp--; 435 } 436 /* If Chromaps_pred_mode_ctxt < Cmax i.e 3. Terminate put a zero */ 437 if (u1_chroma_pred_mode < 3) 438 { 439 i1_bins_len++; 440 } 441 } 442 443 ih264e_encode_decision_bins(u4_bins, 444 i1_bins_len, 445 u4_ctx_inc, 446 3, 447 ps_cabac_ctxt->au1_cabac_ctxt_table 448 + INTRA_CHROMA_PRED_MODE, 449 ps_cabac_ctxt); 450 451 } 452 453 454 /** 455 ******************************************************************************* 456 * 457 * @brief 458 * Encodes CBP for the MB. 459 * 460 * @param[in] u1_cbp 461 * CBP for the MB 462 * 463 * @param[in] ps_cabac_ctxt 464 * Pointer to cabac context structure 465 * 466 * @returns 467 * 468 * @remarks 469 * None 470 * 471 ******************************************************************************* 472 */ 473 static void ih264e_cabac_enc_cbp(UWORD32 u4_cbp, cabac_ctxt_t *ps_cabac_ctxt) 474 { 475 mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; 476 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 477 WORD8 i2_cbp_chroma, i, j; 478 UWORD8 u1_ctxt_inc, u1_bin; 479 UWORD8 a, b; 480 UWORD32 u4_ctx_inc; 481 UWORD32 u4_bins; 482 WORD8 i1_bins_len; 483 484 /* CBP Luma, FL, Cmax = 15, L = 4 */ 485 u4_ctx_inc = 0; 486 u4_bins = 0; 487 i1_bins_len = 5; 488 for (i = 0; i < 4; i++) 489 { 490 /* calulate ctxtInc, depending on neighbour availability */ 491 /* u1_ctxt_inc = CondTerm(A) + 2 * CondTerm(B); 492 A: Left block and B: Top block */ 493 494 /* Check for Top availability */ 495 if (i >> 1) 496 { 497 j = i - 2; 498 /* Top is available always and it's current MB */ 499 b = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1); 500 } 501 else 502 { 503 /* for blocks whose top reference is in another MB */ 504 { 505 j = i + 2; 506 b = ((ps_top_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1; 507 } 508 } 509 510 /* Check for Left availability */ 511 if (i & 0x01) 512 { 513 /* Left is available always and it's current MB */ 514 j = i - 1; 515 a = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1); 516 } 517 else 518 { 519 { 520 j = i + 1; 521 a = ((ps_left_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1; 522 } 523 } 524 u1_ctxt_inc = a + 2 * b; 525 u1_bin = ((u4_cbp >> i) & 0x01); 526 u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i << 2))); 527 u4_bins = (u4_bins | (u1_bin << i)); 528 } 529 530 /* CBP Chroma, TU, Cmax = 2 */ 531 i2_cbp_chroma = u4_cbp >> 4; 532 /* calulate ctxtInc, depending on neighbour availability */ 533 a = (ps_left_ctxt->u1_cbp > 15) ? 1 : 0; 534 b = (ps_top_ctxt->u1_cbp > 15) ? 1 : 0; 535 536 u1_ctxt_inc = a + 2 * b; 537 if (i2_cbp_chroma) 538 { 539 u4_ctx_inc = u4_ctx_inc | ((4 + u1_ctxt_inc) << 16); 540 u4_bins = (u4_bins | 0x10); 541 /* calulate ctxtInc, depending on neighbour availability */ 542 a = (ps_left_ctxt->u1_cbp > 31) ? 1 : 0; 543 b = (ps_top_ctxt->u1_cbp > 31) ? 1 : 0; 544 u1_ctxt_inc = a + 2 * b; 545 u4_ctx_inc = u4_ctx_inc | ((8 + u1_ctxt_inc) << 20); 546 u4_bins = (u4_bins | (((i2_cbp_chroma >> 1) & 0x01) << i1_bins_len)); 547 i1_bins_len++; 548 } 549 else 550 { 551 u4_ctx_inc = (u4_ctx_inc | ((4 + u1_ctxt_inc) << 16)); 552 } 553 ih264e_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 8, 554 ps_cabac_ctxt->au1_cabac_ctxt_table + CBP_LUMA, 555 ps_cabac_ctxt); 556 } 557 558 559 /** 560 ******************************************************************************* 561 * 562 * @brief 563 * Encodes mb_qp_delta for the MB. 564 * 565 * @param[in] i1_mb_qp_delta 566 * mb_qp_delta 567 * 568 * @param[in] ps_cabac_ctxt 569 * Pointer to cabac context structure 570 * 571 * @returns 572 * 573 * @remarks 574 * None 575 * 576 ******************************************************************************* 577 */ 578 static void ih264e_cabac_enc_mb_qp_delta(WORD8 i1_mb_qp_delta, 579 cabac_ctxt_t *ps_cabac_ctxt) 580 { 581 UWORD8 u1_code_num; 582 UWORD8 u1_ctxt_inc; 583 584 UWORD32 u4_ctx_inc; 585 UWORD32 u4_bins; 586 WORD8 i1_bins_len; 587 UWORD8 u1_ctx_inc, u1_bin; 588 /* Range of ps_mb_qp_delta_ctxt= -26 to +25 inclusive */ 589 ASSERT((i1_mb_qp_delta < 26) && (i1_mb_qp_delta > -27)); 590 /* if ps_mb_qp_delta_ctxt=0, then codeNum=0 */ 591 u1_code_num = 0; 592 if (i1_mb_qp_delta > 0) 593 u1_code_num = (i1_mb_qp_delta << 1) - 1; 594 else if (i1_mb_qp_delta < 0) 595 u1_code_num = (ABS(i1_mb_qp_delta)) << 1; 596 597 u4_ctx_inc = 0; 598 u4_bins = 0; 599 i1_bins_len = 1; 600 /* calculate ctxtInc, depending on neighbour availability */ 601 u1_ctxt_inc = (!(!(ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt))); 602 ps_cabac_ctxt->i1_prevps_mb_qp_delta_ctxt = i1_mb_qp_delta; 603 604 if (u1_code_num == 0) 605 { 606 /* b0 */ 607 u1_bin = (UWORD8) (u4_bins); 608 u1_ctx_inc = u1_ctxt_inc & 0x0f; 609 /* Encode the bin */ 610 ih264e_cabac_encode_bin(ps_cabac_ctxt, 611 u1_bin, 612 ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA 613 + u1_ctx_inc); 614 615 } 616 else 617 { 618 /* b0 */ 619 u4_ctx_inc = u1_ctxt_inc; 620 u4_bins = 1; 621 u1_code_num--; 622 if (u1_code_num == 0) 623 { 624 /* b1 */ 625 u4_ctx_inc = (u4_ctx_inc | 0x20); 626 i1_bins_len++; 627 ih264e_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 3, 628 ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA, 629 ps_cabac_ctxt); 630 } 631 else 632 { 633 /* b1 */ 634 u4_ctx_inc = (u4_ctx_inc | 0x20); 635 u4_bins = (u4_bins | (1 << i1_bins_len)); 636 i1_bins_len++; 637 u1_code_num--; 638 /* BinIdx from b2 onwards */ 639 if (u1_code_num < 30) 640 { /* maximum i1_bins_len = 31 */ 641 while (u1_code_num) 642 { 643 u4_bins = (u4_bins | (1 << i1_bins_len)); 644 i1_bins_len++; 645 u1_code_num--; 646 }; 647 u4_ctx_inc = (u4_ctx_inc | 0x300); 648 i1_bins_len++; 649 ih264e_encode_decision_bins(u4_bins, 650 i1_bins_len, 651 u4_ctx_inc, 652 2, 653 ps_cabac_ctxt->au1_cabac_ctxt_table 654 + MB_QP_DELTA, 655 ps_cabac_ctxt); 656 } 657 else 658 { 659 /* maximum i1_bins_len = 53 */ 660 u4_bins = 0xffffffff; 661 i1_bins_len = 32; 662 u4_ctx_inc = (u4_ctx_inc | 0x300); 663 u1_code_num -= 30; 664 ih264e_encode_decision_bins(u4_bins, 665 i1_bins_len, 666 u4_ctx_inc, 667 2, 668 ps_cabac_ctxt->au1_cabac_ctxt_table 669 + MB_QP_DELTA, 670 ps_cabac_ctxt); 671 u4_bins = 0; 672 i1_bins_len = 0; 673 u4_ctx_inc = 0x033; 674 while (u1_code_num) 675 { 676 u4_bins = (u4_bins | (1 << i1_bins_len)); 677 i1_bins_len++; 678 u1_code_num--; 679 }; 680 681 u4_ctx_inc = (u4_ctx_inc | 0x300); 682 i1_bins_len++; 683 ih264e_encode_decision_bins(u4_bins, 684 i1_bins_len, 685 u4_ctx_inc, 686 1, 687 ps_cabac_ctxt->au1_cabac_ctxt_table 688 + MB_QP_DELTA, 689 ps_cabac_ctxt); 690 } 691 } 692 } 693 } 694 695 696 697 698 /** 699 ******************************************************************************* 700 * @brief 701 * Encodes 4residual_block_cabac as defined in 7.3.5.3.3. 702 * 703 * @param[in] pi2_res_block 704 * pointer to the array of residues 705 * 706 * @param[in] u1_nnz 707 * Number of non zero coeffs in the block 708 * 709 * @param[in] u1_max_num_coeffs 710 * Max number of coeffs that can be there in the block 711 * 712 * @param[in] u2_sig_coeff_map 713 * Significant coeff map 714 * 715 * @param[in] u4_ctx_cat_offset 716 * ctxIdxOffset for absolute value contexts 717 * 718 * @param[in] pu1_ctxt_sig_coeff 719 * Pointer to residual state variables 720 * 721 * @param[in] ps_cabac_ctxt 722 * Pointer to cabac context structure 723 * 724 * @returns 725 * 726 * @remarks 727 * None 728 * 729 ******************************************************************************* 730 */ 731 static void ih264e_cabac_write_coeff4x4(WORD16 *pi2_res_block, UWORD8 u1_nnz, 732 UWORD8 u1_max_num_coeffs, 733 UWORD16 u2_sig_coeff_map, 734 UWORD32 u4_ctx_cat_offset, 735 bin_ctxt_model *pu1_ctxt_sig_coeff, 736 cabac_ctxt_t *ps_cabac_ctxt) 737 { 738 739 WORD8 i; 740 WORD16 *pi16_coeffs; 741 UWORD32 u4_sig_coeff, u4_bins; 742 UWORD32 u4_ctx_inc; 743 UWORD8 u1_last_sig_coef_index = (31 - CLZ(u2_sig_coeff_map)); 744 745 /* Always put Coded Block Flag as 1 */ 746 747 pi16_coeffs = pi2_res_block; 748 { 749 bin_ctxt_model *pu1_bin_ctxt; 750 UWORD8 u1_bin, uc_last; 751 752 i = 0; 753 pu1_bin_ctxt = pu1_ctxt_sig_coeff; 754 u4_sig_coeff = 0; 755 u1_bin = 1; 756 if ((u1_last_sig_coef_index)) 757 { 758 u1_bin = !!(u2_sig_coeff_map & 01); 759 } 760 uc_last = 1; 761 762 do 763 { 764 /* Encode Decision */ 765 ih264e_cabac_encode_bin(ps_cabac_ctxt, u1_bin, pu1_bin_ctxt); 766 767 if (u1_bin & uc_last) 768 { 769 u4_sig_coeff = (u4_sig_coeff | (1 << i)); 770 pu1_bin_ctxt = pu1_ctxt_sig_coeff + i 771 + LAST_SIGNIFICANT_COEFF_FLAG_FRAME 772 - SIGNIFICANT_COEFF_FLAG_FRAME; 773 u1_bin = (i == u1_last_sig_coef_index); 774 uc_last = 0; 775 } 776 else 777 { 778 i = i + 1; 779 pu1_bin_ctxt = pu1_ctxt_sig_coeff + i; 780 u1_bin = (i == u1_last_sig_coef_index); 781 uc_last = 1; 782 if ((i != u1_last_sig_coef_index)) 783 { 784 u1_bin = !!((u2_sig_coeff_map >> i) & 01); 785 } 786 } 787 }while (!((i > u1_last_sig_coef_index) 788 || (i > (u1_max_num_coeffs - 1)))); 789 } 790 791 /* Encode coeff_abs_level_minus1 and coeff_sign_flag */ 792 { 793 UWORD8 u1_sign; 794 UWORD16 u2_abs_level; 795 UWORD8 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0; 796 UWORD8 u1_ctx_inc; 797 UWORD8 u1_coff; 798 WORD16 i2_sufs; 799 WORD8 i1_bins_len; 800 i = u1_last_sig_coef_index; 801 pi16_coeffs = pi2_res_block + u1_nnz - 1; 802 do 803 { 804 { 805 u4_sig_coeff = u4_sig_coeff & ((1 << i) - 1); 806 u4_bins = 0; 807 u4_ctx_inc = 0; 808 i1_bins_len = 1; 809 /* Encode the AbsLevelMinus1 */ 810 u2_abs_level = ABS(*(pi16_coeffs)) - 1; 811 /* CtxInc for bin0 */ 812 u4_ctx_inc = MIN(u1_abs_level_equal1, 4); 813 /* CtxInc for remaining */ 814 u1_ctx_inc = 5 + MIN(u1_abs_level_gt1, 4); 815 u4_ctx_inc = u4_ctx_inc + (u1_ctx_inc << 4); 816 if (u2_abs_level) 817 { 818 u1_abs_level_gt1++; 819 u1_abs_level_equal1 = 0; 820 } 821 if (!u1_abs_level_gt1) 822 u1_abs_level_equal1++; 823 824 u1_coff = 14; 825 if (u2_abs_level >= u1_coff) 826 { 827 /* Prefix TU i.e string of 14 1's */ 828 u4_bins = 0x3fff; 829 i1_bins_len = 14; 830 ih264e_encode_decision_bins(u4_bins, i1_bins_len, 831 u4_ctx_inc, 1, ps_cabac_ctxt->au1_cabac_ctxt_table 832 + u4_ctx_cat_offset, 833 ps_cabac_ctxt); 834 835 /* Suffix, uses EncodeBypass */ 836 i2_sufs = u2_abs_level - u1_coff; 837 838 u4_bins = ih264e_cabac_UEGk0_binarization(i2_sufs, 839 &i1_bins_len); 840 841 ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, 842 i1_bins_len); 843 844 } 845 else 846 { 847 /* Prefix only */ 848 u4_bins = (1 << u2_abs_level) - 1; 849 i1_bins_len = u2_abs_level + 1; 850 /* Encode Terminating bit */ 851 ih264e_encode_decision_bins(u4_bins, i1_bins_len, 852 u4_ctx_inc, 1, ps_cabac_ctxt->au1_cabac_ctxt_table 853 + u4_ctx_cat_offset, 854 ps_cabac_ctxt); 855 } 856 } 857 /* encode coeff_sign_flag[i] */ 858 u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0; 859 ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, u1_sign); 860 i = CLZ(u4_sig_coeff); 861 i = 31 - i; 862 pi16_coeffs--; 863 }while (u4_sig_coeff); 864 } 865 866 } 867 868 869 /** 870 ******************************************************************************* 871 * @brief 872 * Write DC coeffs for intra predicted luma block 873 * 874 * @param[in] ps_ent_ctxt 875 * Pointer to entropy context structure 876 * 877 * @returns 878 * 879 * @remarks 880 * None 881 * 882 ******************************************************************************* 883 */ 884 static void ih264e_cabac_encode_residue_luma_dc(entropy_ctxt_t *ps_ent_ctxt) 885 { 886 887 /* CABAC context */ 888 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; 889 tu_sblk_coeff_data_t *ps_mb_coeff_data; 890 891 /* packed residue */ 892 void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data; 893 UWORD16 u2_sig_coeff_map; 894 WORD16 *pi2_res_block; 895 UWORD8 u1_nnz; 896 UWORD8 u1_cbf; 897 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 898 mb_info_ctxt_t *p_CurCtxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 899 900 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz, 901 u2_sig_coeff_map, pi2_res_block); 902 903 u1_cbf = !!(u1_nnz); 904 905 { 906 UWORD32 u4_ctx_inc; 907 UWORD8 u1_a, u1_b; 908 909 u1_a = ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] & 0x1; 910 u1_b = ps_top_ctxt->u1_yuv_dc_csbp & 0x1; 911 u4_ctx_inc = u1_a + (u1_b << 1); 912 913 ih264e_cabac_encode_bin(ps_cabac_ctxt, 914 u1_cbf, 915 ps_cabac_ctxt->au1_cabac_ctxt_table + CBF 916 + (LUMA_DC_CTXCAT << 2) + u4_ctx_inc); 917 } 918 919 /* Write coded_block_flag */ 920 if (u1_cbf) 921 { 922 ih264e_cabac_write_coeff4x4(pi2_res_block, 923 u1_nnz, 924 15, 925 u2_sig_coeff_map, 926 COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET, 927 ps_cabac_ctxt->au1_cabac_ctxt_table 928 + SIGNIFICANT_COEFF_FLAG_FRAME 929 + SIG_COEFF_CTXT_CAT_0_OFFSET, 930 ps_cabac_ctxt); 931 932 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= 0x1; 933 p_CurCtxt->u1_yuv_dc_csbp |= 0x1; 934 } 935 else 936 { 937 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; 938 p_CurCtxt->u1_yuv_dc_csbp &= 0x6; 939 } 940 941 ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data; 942 } 943 944 945 946 947 /** 948 ******************************************************************************* 949 * @brief 950 * Write chroma residues to the bitstream 951 * 952 * @param[in] ps_ent_ctxt 953 * Pointer to entropy context structure 954 * 955 * @param[in] u1_chroma_cbp 956 * coded block pattern, chroma 957 * 958 * @returns 959 * 960 * @remarks 961 * None 962 * 963 ******************************************************************************* 964 */ 965 static void ih264e_cabac_write_chroma_residue(entropy_ctxt_t *ps_ent_ctxt, 966 UWORD8 u1_chroma_cbp) 967 { 968 /* CABAC context */ 969 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; 970 tu_sblk_coeff_data_t *ps_mb_coeff_data; 971 /* packed residue */ 972 void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data; 973 UWORD16 u2_sig_coeff_map; 974 UWORD8 u1_nnz; 975 mb_info_ctxt_t *ps_top_ctxt_mb_info, *ps_curr_ctxt; 976 977 ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_top_ctxt_mb_info; 978 ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 979 980 /********************/ 981 /* Write Chroma DC */ 982 /********************/ 983 { 984 WORD16 *pi2_res_block; 985 UWORD8 u1_left_dc_csbp, u1_top_dc_csbp, u1_uv, u1_cbf; 986 987 u1_left_dc_csbp = (ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0]) >> 1; 988 u1_top_dc_csbp = (ps_top_ctxt_mb_info->u1_yuv_dc_csbp) >> 1; 989 990 for (u1_uv = 0; u1_uv < 2; u1_uv++) 991 { 992 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, 993 u1_nnz, u2_sig_coeff_map, pi2_res_block); 994 u1_cbf = !!(u1_nnz); 995 { 996 UWORD8 u1_a, u1_b; 997 UWORD32 u4_ctx_inc; 998 u1_a = (u1_left_dc_csbp >> u1_uv) & 0x01; 999 u1_b = (u1_top_dc_csbp >> u1_uv) & 0x01; 1000 u4_ctx_inc = (u1_a + (u1_b << 1)); 1001 1002 ih264e_cabac_encode_bin(ps_cabac_ctxt, 1003 u1_cbf, 1004 ps_cabac_ctxt->au1_cabac_ctxt_table + CBF 1005 + (CHROMA_DC_CTXCAT << 2) 1006 + u4_ctx_inc); 1007 } 1008 1009 if (u1_cbf) 1010 { 1011 ih264e_cabac_write_coeff4x4(pi2_res_block, 1012 u1_nnz, 1013 3, 1014 u2_sig_coeff_map, 1015 COEFF_ABS_LEVEL_MINUS1 1016 + COEFF_ABS_LEVEL_CAT_3_OFFSET, 1017 ps_cabac_ctxt->au1_cabac_ctxt_table 1018 + SIGNIFICANT_COEFF_FLAG_FRAME 1019 + SIG_COEFF_CTXT_CAT_3_OFFSET, 1020 ps_cabac_ctxt); 1021 1022 SETBIT(u1_top_dc_csbp, u1_uv); 1023 SETBIT(u1_left_dc_csbp, u1_uv); 1024 } 1025 else 1026 { 1027 CLEARBIT(u1_top_dc_csbp, u1_uv); 1028 CLEARBIT(u1_left_dc_csbp, u1_uv); 1029 } 1030 } 1031 /*************************************************************/ 1032 /* Update the DC csbp */ 1033 /*************************************************************/ 1034 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1; 1035 ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1; 1036 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= (u1_left_dc_csbp << 1); 1037 ps_curr_ctxt->u1_yuv_dc_csbp |= (u1_top_dc_csbp << 1); 1038 } 1039 /*******************/ 1040 /* Write Chroma AC */ 1041 /*******************/ 1042 { 1043 if (u1_chroma_cbp == 2) 1044 { 1045 UWORD8 u1_uv_blkno, u1_left_ac_csbp, u1_top_ac_csbp; 1046 WORD16 *pi2_res_block; 1047 u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_uv_ac_csbp[0]; 1048 u1_top_ac_csbp = ps_top_ctxt_mb_info->u1_yuv_ac_csbp >> 4; 1049 1050 for (u1_uv_blkno = 0; u1_uv_blkno < 8; u1_uv_blkno++) 1051 { 1052 UWORD8 u1_cbf; 1053 UWORD8 u1_b2b0, u1_b2b1; 1054 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, 1055 u1_nnz, u2_sig_coeff_map, 1056 pi2_res_block); 1057 1058 u1_cbf = !!(u1_nnz); 1059 u1_b2b0 = ((u1_uv_blkno & 0x4) >> 1) | (u1_uv_blkno & 0x1); 1060 u1_b2b1 = ((u1_uv_blkno & 0x4) >> 1) 1061 | ((u1_uv_blkno & 0x2) >> 1); 1062 1063 { 1064 UWORD8 u1_a, u1_b; 1065 UWORD32 u4_ctx_inc; 1066 /* write coded_block_flag */ 1067 u1_a = (u1_left_ac_csbp >> u1_b2b1) & 0x1; 1068 u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x1; 1069 u4_ctx_inc = u1_a + (u1_b << 1); 1070 1071 ih264e_cabac_encode_bin(ps_cabac_ctxt, 1072 u1_cbf, 1073 ps_cabac_ctxt->au1_cabac_ctxt_table + CBF 1074 + (CHROMA_AC_CTXCAT << 2) 1075 + u4_ctx_inc); 1076 1077 } 1078 if (u1_cbf) 1079 { 1080 ih264e_cabac_write_coeff4x4(pi2_res_block, 1081 u1_nnz, 1082 14, 1083 u2_sig_coeff_map, 1084 COEFF_ABS_LEVEL_MINUS1 1085 + COEFF_ABS_LEVEL_CAT_4_OFFSET, 1086 ps_cabac_ctxt->au1_cabac_ctxt_table 1087 + +SIGNIFICANT_COEFF_FLAG_FRAME 1088 + SIG_COEFF_CTXT_CAT_4_OFFSET, 1089 ps_cabac_ctxt); 1090 1091 SETBIT(u1_left_ac_csbp, u1_b2b1); 1092 SETBIT(u1_top_ac_csbp, u1_b2b0); 1093 } 1094 else 1095 { 1096 CLEARBIT(u1_left_ac_csbp, u1_b2b1); 1097 CLEARBIT(u1_top_ac_csbp, u1_b2b0); 1098 1099 } 1100 } 1101 /*************************************************************/ 1102 /* Update the AC csbp */ 1103 /*************************************************************/ 1104 ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = u1_left_ac_csbp; 1105 ps_curr_ctxt->u1_yuv_ac_csbp &= 0x0f; 1106 ps_curr_ctxt->u1_yuv_ac_csbp |= (u1_top_ac_csbp << 4); 1107 } 1108 else 1109 { 1110 ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0; 1111 ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf; 1112 } 1113 } 1114 ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data; 1115 } 1116 1117 1118 1119 1120 /** 1121 ******************************************************************************* 1122 * @brief 1123 * Encodes Residues for the MB as defined in 7.3.5.3 1124 * 1125 * @param[in] ps_ent_ctxt 1126 * Pointer to entropy context structure 1127 * 1128 * @param[in] u1_cbp 1129 * coded block pattern 1130 * 1131 * @param[in] u1_ctx_cat 1132 * Context category, LUMA_AC_CTXCAT or LUMA_4x4_CTXCAT 1133 * 1134 * @returns 1135 * 1136 * @remarks 1137 * None 1138 * 1139 ******************************************************************************* 1140 */ 1141 static void ih264e_cabac_encode_residue(entropy_ctxt_t *ps_ent_ctxt, 1142 UWORD32 u4_cbp, UWORD8 u1_ctx_cat) 1143 { 1144 /* CABAC context */ 1145 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; 1146 1147 tu_sblk_coeff_data_t *ps_mb_coeff_data; 1148 /* packed residue */ 1149 void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data; 1150 UWORD16 u2_sig_coeff_map; 1151 UWORD8 u1_nnz; 1152 mb_info_ctxt_t *ps_curr_ctxt; 1153 mb_info_ctxt_t *ps_top_ctxt; 1154 UWORD8 u1_left_ac_csbp; 1155 UWORD8 u1_top_ac_csbp; 1156 UWORD32 u4_ctx_idx_offset_sig_coef, u4_ctx_idx_offset_abs_lvl; 1157 ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 1158 ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 1159 u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_y_ac_csbp[0]; 1160 u1_top_ac_csbp = ps_top_ctxt->u1_yuv_ac_csbp; 1161 1162 if (u4_cbp & 0xf) 1163 { 1164 /* Write luma residue */ 1165 UWORD8 u1_offset; 1166 WORD16 *pi2_res_block; 1167 UWORD8 u1_subblk_num; 1168 if (u1_ctx_cat == LUMA_AC_CTXCAT) 1169 { 1170 u1_offset = 1; 1171 u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_1_OFFSET; 1172 u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 1173 + COEFF_ABS_LEVEL_CAT_1_OFFSET; 1174 } 1175 else 1176 { 1177 u1_offset = 0; 1178 u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_2_OFFSET; 1179 u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 1180 + COEFF_ABS_LEVEL_CAT_2_OFFSET; 1181 } 1182 1183 for (u1_subblk_num = 0; u1_subblk_num < 16; u1_subblk_num++) 1184 { 1185 UWORD8 u1_b0, u1_b1, u1_b2, u1_b3, u1_b2b0, u1_b3b1, u1_b3b2; 1186 u1_b0 = (u1_subblk_num & 0x1); 1187 u1_b1 = (u1_subblk_num & 0x2) >> 1; 1188 u1_b2 = (u1_subblk_num & 0x4) >> 2; 1189 u1_b3 = (u1_subblk_num & 0x8) >> 3; 1190 u1_b2b0 = (u1_b2 << 1) | (u1_b0); 1191 u1_b3b1 = (u1_b3 << 1) | (u1_b1); 1192 u1_b3b2 = (u1_b3 << 1) | (u1_b2); 1193 1194 if (!((u4_cbp >> u1_b3b2) & 0x1)) 1195 { 1196 /* ---------------------------------------------------------- */ 1197 /* The current block is not coded so skip all the sub block */ 1198 /* and set the pointer of scan level, csbp accrodingly */ 1199 /* ---------------------------------------------------------- */ 1200 CLEARBIT(u1_top_ac_csbp, u1_b2b0); 1201 CLEARBIT(u1_top_ac_csbp, (u1_b2b0 + 1)); 1202 CLEARBIT(u1_left_ac_csbp, u1_b3b1); 1203 CLEARBIT(u1_left_ac_csbp, (u1_b3b1 + 1)); 1204 1205 u1_subblk_num += 3; 1206 } 1207 else 1208 { 1209 UWORD8 u1_csbf; 1210 1211 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, 1212 u1_nnz, u2_sig_coeff_map, 1213 pi2_res_block); 1214 1215 u1_csbf = !!(u1_nnz); 1216 { 1217 UWORD8 u1_a, u1_b; 1218 UWORD32 u4_ctx_inc; 1219 u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x01; 1220 u1_a = (u1_left_ac_csbp >> u1_b3b1) & 0x01; 1221 u4_ctx_inc = u1_a + (u1_b << 1); 1222 1223 /* Encode the bin */ 1224 ih264e_cabac_encode_bin(ps_cabac_ctxt, 1225 u1_csbf, 1226 ps_cabac_ctxt->au1_cabac_ctxt_table + CBF 1227 + (u1_ctx_cat << 2) + u4_ctx_inc); 1228 1229 } 1230 /**************************/ 1231 /* Write coded_block_flag */ 1232 /**************************/ 1233 if (u1_csbf) 1234 { 1235 ih264e_cabac_write_coeff4x4(pi2_res_block, 1236 u1_nnz, 1237 (UWORD8) (15 - u1_offset), 1238 u2_sig_coeff_map, 1239 u4_ctx_idx_offset_abs_lvl, 1240 ps_cabac_ctxt->au1_cabac_ctxt_table 1241 + SIGNIFICANT_COEFF_FLAG_FRAME 1242 + u4_ctx_idx_offset_sig_coef, 1243 ps_cabac_ctxt); 1244 1245 SETBIT(u1_top_ac_csbp, u1_b2b0); 1246 SETBIT(u1_left_ac_csbp, u1_b3b1); 1247 } 1248 else 1249 { 1250 CLEARBIT(u1_top_ac_csbp, u1_b2b0); 1251 CLEARBIT(u1_left_ac_csbp, u1_b3b1); 1252 } 1253 } 1254 } 1255 /**************************************************************************/ 1256 /* Update the AC csbp */ 1257 /**************************************************************************/ 1258 ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = u1_left_ac_csbp & 0xf; 1259 u1_top_ac_csbp &= 0x0f; 1260 ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0; 1261 ps_curr_ctxt->u1_yuv_ac_csbp |= u1_top_ac_csbp; 1262 } 1263 else 1264 { 1265 ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = 0; 1266 ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0; 1267 } 1268 1269 /* Write chroma residue */ 1270 1271 ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data; 1272 { 1273 UWORD8 u1_cbp_chroma; 1274 u1_cbp_chroma = u4_cbp >> 4; 1275 if (u1_cbp_chroma) 1276 { 1277 ih264e_cabac_write_chroma_residue(ps_ent_ctxt, u1_cbp_chroma); 1278 } 1279 else 1280 { 1281 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1; 1282 ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1; 1283 ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0; 1284 ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf; 1285 } 1286 } 1287 } 1288 1289 /** 1290 ******************************************************************************* 1291 * @brief 1292 * Encodes a Motion vector (9.3.3.1.1.7 ) 1293 * 1294 * @param[in] u1_mvd 1295 * Motion vector to be encoded 1296 * 1297 * @param[in] u4_ctx_idx_offset 1298 * * ctxIdxOffset for MV_X or MV_Ycontext 1299 * 1300 * @param[in] ui2_abs_mvd 1301 * sum of absolute value of corresponding neighboring motion vectors 1302 * 1303 * @param[in] ps_cabac_ctxt 1304 * Pointer to cabac context structure 1305 * 1306 * @returns 1307 * 1308 * @remarks 1309 * None 1310 * 1311 ******************************************************************************* 1312 */ 1313 static void ih264e_cabac_enc_ctx_mvd(WORD16 u1_mvd, UWORD32 u4_ctx_idx_offset, 1314 UWORD16 ui2_abs_mvd, 1315 cabac_ctxt_t *ps_cabac_ctxt) 1316 { 1317 1318 UWORD8 u1_bin, u1_ctxt_inc; 1319 WORD8 k = 3, u1_coff = 9; 1320 WORD16 i2_abs_mvd, i2_sufs; 1321 UWORD32 u4_ctx_inc; 1322 UWORD32 u4_bins; 1323 WORD8 i1_bins_len; 1324 1325 /* if mvd < u1_coff 1326 only Prefix 1327 else 1328 Prefix + Suffix 1329 1330 encode sign bit 1331 1332 Prefix TU encoding Cmax =u1_coff and Suffix 3rd order Exp-Golomb 1333 */ 1334 1335 if (ui2_abs_mvd < 3) 1336 u4_ctx_inc = 0; 1337 else if (ui2_abs_mvd > 32) 1338 u4_ctx_inc = 2; 1339 else 1340 u4_ctx_inc = 1; 1341 1342 u4_bins = 0; 1343 i1_bins_len = 1; 1344 1345 if (u1_mvd == 0) 1346 { 1347 ih264e_cabac_encode_bin(ps_cabac_ctxt, 1348 0, 1349 ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset 1350 + u4_ctx_inc); 1351 } 1352 else 1353 { 1354 i2_abs_mvd = ABS(u1_mvd); 1355 if (i2_abs_mvd >= u1_coff) 1356 { 1357 /* Prefix TU i.e string of 9 1's */ 1358 u4_bins = 0x1ff; 1359 i1_bins_len = 9; 1360 u4_ctx_inc = (u4_ctx_inc | 0x065430); 1361 1362 ih264e_encode_decision_bins(u4_bins, 1363 i1_bins_len, 1364 u4_ctx_inc, 1365 4, 1366 ps_cabac_ctxt->au1_cabac_ctxt_table 1367 + u4_ctx_idx_offset, 1368 ps_cabac_ctxt); 1369 1370 /* Suffix, uses EncodeBypass */ 1371 u4_bins = 0; 1372 i1_bins_len = 0; 1373 i2_sufs = i2_abs_mvd - u1_coff; 1374 while (1) 1375 { 1376 if (i2_sufs >= (1 << k)) 1377 { 1378 u4_bins = (u4_bins | (1 << (31 - i1_bins_len))); 1379 i1_bins_len++; 1380 i2_sufs = i2_sufs - (1 << k); 1381 k++; 1382 } 1383 else 1384 { 1385 i1_bins_len++; 1386 while (k--) 1387 { 1388 u1_bin = ((i2_sufs >> k) & 0x01); 1389 u4_bins = (u4_bins | (u1_bin << (31 - i1_bins_len))); 1390 i1_bins_len++; 1391 } 1392 break; 1393 } 1394 } 1395 u4_bins >>= (32 - i1_bins_len); 1396 ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, 1397 i1_bins_len); 1398 } 1399 else 1400 { 1401 /* Prefix only */ 1402 /* b0 */ 1403 u4_bins = 1; 1404 i2_abs_mvd--; 1405 u1_ctxt_inc = 3; 1406 while (i2_abs_mvd) 1407 { 1408 i2_abs_mvd--; 1409 u4_bins = (u4_bins | (1 << i1_bins_len)); 1410 if (u1_ctxt_inc <= 6) 1411 { 1412 u4_ctx_inc = (u4_ctx_inc 1413 | (u1_ctxt_inc << (i1_bins_len << 2))); 1414 u1_ctxt_inc++; 1415 } 1416 i1_bins_len++; 1417 } 1418 /* Encode Terminating bit */ 1419 if (i1_bins_len <= 4) 1420 u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i1_bins_len << 2))); 1421 i1_bins_len++; 1422 ih264e_encode_decision_bins(u4_bins, 1423 i1_bins_len, 1424 u4_ctx_inc, 1425 4, 1426 ps_cabac_ctxt->au1_cabac_ctxt_table 1427 + u4_ctx_idx_offset, 1428 ps_cabac_ctxt); 1429 } 1430 /* sign bit, uses EncodeBypass */ 1431 if (u1_mvd > 0) 1432 ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, 0); 1433 else 1434 ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, 1); 1435 } 1436 } 1437 1438 /** 1439 ******************************************************************************* 1440 * @brief 1441 * Encodes all motion vectors for a P16x16 MB 1442 * 1443 * @param[in] ps_cabac_ctxt 1444 * Pointer to cabac context structure 1445 * 1446 * @param[in] pi2_mv_ptr 1447 * Pointer to array of motion vectors 1448 * 1449 * @returns 1450 * 1451 * @remarks 1452 * None 1453 * 1454 ******************************************************************************* 1455 */ 1456 static void ih264e_cabac_enc_mvds_p16x16(cabac_ctxt_t *ps_cabac_ctxt, 1457 WORD16 *pi2_mv_ptr) 1458 { 1459 1460 1461 /* Encode the differential component of the motion vectors */ 1462 1463 { 1464 UWORD8 u1_abs_mvd_x, u1_abs_mvd_y; 1465 UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt; 1466 WORD16 u2_mv; 1467 u1_abs_mvd_x = 0; 1468 u1_abs_mvd_y = 0; 1469 pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0]; 1470 pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0]; 1471 { 1472 UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, 1473 u2_abs_mvd_y_b; 1474 u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0]; 1475 u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1]; 1476 u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0]; 1477 u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1]; 1478 u2_mv = *(pi2_mv_ptr++); 1479 1480 ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_X, 1481 (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b), 1482 ps_cabac_ctxt); 1483 1484 u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv)); 1485 u2_mv = *(pi2_mv_ptr++); 1486 1487 ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_Y, 1488 (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b), 1489 ps_cabac_ctxt); 1490 1491 u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv)); 1492 } 1493 /***************************************************************/ 1494 /* Store abs_mvd_values cabac contexts */ 1495 /***************************************************************/ 1496 pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x; 1497 pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y; 1498 } 1499 } 1500 1501 1502 /** 1503 ******************************************************************************* 1504 * @brief 1505 * Encodes all motion vectors for a B MB (Assues that mbype is B_L0_16x16, B_L1_16x16 or B_Bi_16x16 1506 * 1507 * @param[in] ps_cabac_ctxt 1508 * Pointer to cabac context structure 1509 * 1510 * @param[in] pi2_mv_ptr 1511 * Pointer to array of motion vectors 1512 * 1513 * @returns 1514 * 1515 * @remarks 1516 * None 1517 * 1518 ******************************************************************************* 1519 */ 1520 static void ih264e_cabac_enc_mvds_b16x16(cabac_ctxt_t *ps_cabac_ctxt, 1521 WORD16 *pi2_mv_ptr, 1522 WORD32 i4_mb_part_pred_mode ) 1523 { 1524 1525 /* Encode the differential component of the motion vectors */ 1526 1527 { 1528 UWORD8 u1_abs_mvd_x, u1_abs_mvd_y; 1529 UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt; 1530 WORD16 u2_mv; 1531 u1_abs_mvd_x = 0; 1532 u1_abs_mvd_y = 0; 1533 pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0]; 1534 pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0]; 1535 if (i4_mb_part_pred_mode != PRED_L1)/* || PRED_BI */ 1536 { 1537 UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, 1538 u2_abs_mvd_y_b; 1539 u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0]; 1540 u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1]; 1541 u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0]; 1542 u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1]; 1543 u2_mv = *(pi2_mv_ptr++); 1544 1545 ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_X, 1546 (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b), 1547 ps_cabac_ctxt); 1548 1549 u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv)); 1550 u2_mv = *(pi2_mv_ptr++); 1551 1552 ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_Y, 1553 (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b), 1554 ps_cabac_ctxt); 1555 1556 u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv)); 1557 } 1558 /***************************************************************/ 1559 /* Store abs_mvd_values cabac contexts */ 1560 /***************************************************************/ 1561 pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x; 1562 pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y; 1563 1564 u1_abs_mvd_x = 0; 1565 u1_abs_mvd_y = 0; 1566 if (i4_mb_part_pred_mode != PRED_L0)/* || PRED_BI */ 1567 { 1568 UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, 1569 u2_abs_mvd_y_b; 1570 u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[2]; 1571 u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[3]; 1572 u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[2]; 1573 u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[3]; 1574 u2_mv = *(pi2_mv_ptr++); 1575 1576 ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_X, 1577 (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b), 1578 ps_cabac_ctxt); 1579 1580 u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv)); 1581 u2_mv = *(pi2_mv_ptr++); 1582 1583 ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_Y, 1584 (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b), 1585 ps_cabac_ctxt); 1586 1587 u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv)); 1588 } 1589 /***************************************************************/ 1590 /* Store abs_mvd_values cabac contexts */ 1591 /***************************************************************/ 1592 pu1_top_mv_ctxt[2] = pu1_lft_mv_ctxt[2] = u1_abs_mvd_x; 1593 pu1_top_mv_ctxt[3] = pu1_lft_mv_ctxt[3] = u1_abs_mvd_y; 1594 } 1595 } 1596 1597 1598 1599 /** 1600 ******************************************************************************* 1601 * 1602 * @brief 1603 * This function generates CABAC coded bit stream for an Intra Slice. 1604 * 1605 * @description 1606 * The mb syntax layer for intra slices constitutes luma mb mode, mb qp delta, coded block pattern, chroma mb mode and 1607 * luma/chroma residue. These syntax elements are written as directed by table 1608 * 7.3.5 of h264 specification. 1609 * 1610 * @param[in] ps_ent_ctxt 1611 * pointer to entropy context 1612 * 1613 * @returns error code 1614 * 1615 * @remarks none 1616 * 1617 ******************************************************************************* 1618 */ 1619 IH264E_ERROR_T ih264e_write_islice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt) 1620 { 1621 /* bit stream ptr */ 1622 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm; 1623 /* CABAC context */ 1624 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; 1625 /* packed header data */ 1626 UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data; 1627 mb_info_ctxt_t *ps_curr_ctxt; 1628 WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode; 1629 WORD8 mb_qp_delta; 1630 UWORD32 u4_cbp_l, u4_cbp_c; 1631 WORD32 byte_count = 0; 1632 WORD32 bitstream_start_offset, bitstream_end_offset; 1633 1634 if ((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) 1635 >= ps_bitstream->u4_max_strm_size) 1636 { 1637 /* return without corrupting the buffer beyond its size */ 1638 return (IH264E_BITSTREAM_BUFFER_OVERFLOW); 1639 } 1640 /* mb header info */ 1641 mb_tpm = *pu1_byte++; 1642 byte_count++; 1643 cbp = *pu1_byte++; 1644 byte_count++; 1645 mb_qp_delta = *pu1_byte++; 1646 byte_count++; 1647 /* mb type */ 1648 mb_type = mb_tpm & 0xF; 1649 1650 ih264e_get_cabac_context(ps_ent_ctxt, mb_type); 1651 ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 1652 1653 /* Starting bitstream offset for header in bits */ 1654 bitstream_start_offset = GET_NUM_BITS(ps_bitstream); 1655 u4_cbp_c = (cbp >> 4); 1656 u4_cbp_l = (cbp & 0xF); 1657 if (mb_type == I16x16) 1658 { 1659 luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u4_cbp_c << 2) 1660 + (u4_cbp_l == 15) * 12; 1661 } 1662 else 1663 { 1664 luma_intra_mode = 0; 1665 } 1666 1667 chroma_intra_mode = (mb_tpm >> 6); 1668 1669 /* Encode Intra pred mode, Luma */ 1670 ih264e_cabac_enc_intra_mb_type(ISLICE, luma_intra_mode, ps_cabac_ctxt, 1671 MB_TYPE_I_SLICE); 1672 1673 if (mb_type == I4x4) 1674 { /* Encode 4x4 MB modes */ 1675 ih264e_cabac_enc_4x4mb_modes(ps_cabac_ctxt, pu1_byte); 1676 byte_count += 8; 1677 } 1678 /* Encode chroma mode */ 1679 ih264e_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt); 1680 1681 if (mb_type != I16x16) 1682 { /* Encode MB cbp */ 1683 ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); 1684 } 1685 1686 if ((cbp > 0) || (mb_type == I16x16)) 1687 { 1688 /* Encode mb_qp_delta */ 1689 ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); 1690 /* Ending bitstream offset for header in bits */ 1691 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1692 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset 1693 - bitstream_start_offset; 1694 /* Starting bitstream offset for residue */ 1695 bitstream_start_offset = bitstream_end_offset; 1696 if (mb_type == I16x16) 1697 { 1698 ps_curr_ctxt->u1_mb_type = CAB_I16x16; 1699 ps_curr_ctxt->u1_cbp = cbp; 1700 ih264e_cabac_encode_residue_luma_dc(ps_ent_ctxt); 1701 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_AC_CTXCAT); 1702 } 1703 else 1704 { 1705 ps_curr_ctxt->u1_cbp = cbp; 1706 ps_curr_ctxt->u1_mb_type = I4x4; 1707 ps_curr_ctxt->u1_mb_type = CAB_I4x4; 1708 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); 1709 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; 1710 ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6; 1711 } 1712 /* Ending bitstream offset for reside in bits */ 1713 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1714 ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset 1715 - bitstream_start_offset; 1716 } 1717 else 1718 { 1719 ps_curr_ctxt->u1_yuv_ac_csbp = 0; 1720 ps_curr_ctxt->u1_yuv_dc_csbp = 0; 1721 *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; 1722 *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; 1723 *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; 1724 /* Ending bitstream offset for header in bits */ 1725 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1726 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset 1727 - bitstream_start_offset; 1728 1729 /* Computing the number of used used for encoding the MB syntax */ 1730 } 1731 memset(ps_curr_ctxt->u1_mv, 0, 16); 1732 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); 1733 ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_cbp = cbp; 1734 ps_ent_ctxt->pv_mb_header_data = ((WORD8 *)ps_ent_ctxt->pv_mb_header_data) + byte_count; 1735 if (mb_type == I16x16) 1736 { 1737 ps_curr_ctxt->u1_mb_type = CAB_I16x16; 1738 1739 } 1740 else 1741 { 1742 ps_curr_ctxt->u1_mb_type = CAB_I4x4; 1743 1744 } 1745 return IH264E_SUCCESS; 1746 } 1747 1748 /** 1749 ******************************************************************************* 1750 * 1751 * @brief 1752 * This function generates CABAC coded bit stream for Inter slices 1753 * 1754 * @description 1755 * The mb syntax layer for inter slices constitutes luma mb mode, mb qp delta, coded block pattern, chroma mb mode and 1756 * luma/chroma residue. These syntax elements are written as directed by table 1757 * 7.3.5 of h264 specification 1758 * 1759 * @param[in] ps_ent_ctxt 1760 * pointer to entropy context 1761 * 1762 * @returns error code 1763 * 1764 * @remarks none 1765 * 1766 ******************************************************************************* 1767 */ 1768 IH264E_ERROR_T ih264e_write_pslice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt) 1769 { 1770 /* bit stream ptr */ 1771 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm; 1772 /* CABAC context */ 1773 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; 1774 1775 mb_info_ctxt_t *ps_curr_ctxt; 1776 1777 WORD32 bitstream_start_offset, bitstream_end_offset; 1778 WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode; 1779 WORD8 mb_qp_delta; 1780 UWORD32 u4_cbp_l, u4_cbp_c; 1781 WORD32 byte_count = 0; 1782 UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data; 1783 1784 if ((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) 1785 >= ps_bitstream->u4_max_strm_size) 1786 { 1787 /* return without corrupting the buffer beyond its size */ 1788 return (IH264E_BITSTREAM_BUFFER_OVERFLOW); 1789 } 1790 /* mb header info */ 1791 mb_tpm = *pu1_byte++; 1792 byte_count++; 1793 1794 /* mb type */ 1795 mb_type = mb_tpm & 0xF; 1796 /* CABAC contexts for the MB */ 1797 ih264e_get_cabac_context(ps_ent_ctxt, mb_type); 1798 ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 1799 1800 /* if Intra MB */ 1801 if (mb_type == I16x16 || mb_type == I4x4) 1802 { 1803 cbp = *pu1_byte++; 1804 byte_count++; 1805 mb_qp_delta = *pu1_byte++; 1806 byte_count++; 1807 1808 /* Starting bitstream offset for header in bits */ 1809 bitstream_start_offset = GET_NUM_BITS(ps_bitstream); 1810 1811 /* Encode mb_skip_flag */ 1812 ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE); 1813 u4_cbp_c = (cbp >> 4); 1814 u4_cbp_l = (cbp & 0xF); 1815 if (mb_type == I16x16) 1816 { 1817 luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u4_cbp_c << 2) 1818 + (u4_cbp_l == 15) * 12; 1819 } 1820 else 1821 { 1822 luma_intra_mode = 0; 1823 } 1824 /* Encode intra mb type */ 1825 { 1826 ih264e_cabac_encode_bin(ps_cabac_ctxt, 1827 1, 1828 ps_cabac_ctxt->au1_cabac_ctxt_table 1829 + MB_TYPE_P_SLICE); 1830 1831 ih264e_cabac_enc_intra_mb_type(PSLICE, (UWORD8) luma_intra_mode, 1832 ps_cabac_ctxt, MB_TYPE_P_SLICE); 1833 } 1834 1835 if (mb_type == I4x4) 1836 { /* Intra 4x4 modes */ 1837 ih264e_cabac_enc_4x4mb_modes(ps_cabac_ctxt, pu1_byte); 1838 byte_count += 8; 1839 } 1840 chroma_intra_mode = (mb_tpm >> 6); 1841 1842 ih264e_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt); 1843 1844 if (mb_type != I16x16) 1845 { 1846 /* encode CBP */ 1847 ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); 1848 } 1849 1850 if ((cbp > 0) || (mb_type == I16x16)) 1851 { 1852 ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); 1853 1854 /* Ending bitstream offset for header in bits */ 1855 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1856 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset 1857 - bitstream_start_offset; 1858 /* Starting bitstream offset for residue */ 1859 bitstream_start_offset = bitstream_end_offset; 1860 1861 /* Encoding Residue */ 1862 if (mb_type == I16x16) 1863 { 1864 ps_curr_ctxt->u1_mb_type = CAB_I16x16; 1865 ps_curr_ctxt->u1_cbp = (UWORD8) cbp; 1866 ih264e_cabac_encode_residue_luma_dc(ps_ent_ctxt); 1867 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_AC_CTXCAT); 1868 } 1869 else 1870 { 1871 ps_curr_ctxt->u1_cbp = (UWORD8) cbp; 1872 ps_curr_ctxt->u1_mb_type = I4x4; 1873 ps_curr_ctxt->u1_mb_type = CAB_I4x4; 1874 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); 1875 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; 1876 ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6; 1877 } 1878 1879 /* Ending bitstream offset for reside in bits */ 1880 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1881 ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset 1882 - bitstream_start_offset; 1883 } 1884 else 1885 { 1886 ps_curr_ctxt->u1_yuv_ac_csbp = 0; 1887 ps_curr_ctxt->u1_yuv_dc_csbp = 0; 1888 *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; 1889 *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; 1890 *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; 1891 /* Ending bitstream offset for header in bits */ 1892 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1893 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset 1894 - bitstream_start_offset; 1895 } 1896 1897 memset(ps_curr_ctxt->u1_mv, 0, 16); 1898 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); 1899 ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_cbp = (UWORD8) cbp; 1900 1901 if (mb_type == I16x16) 1902 { 1903 ps_curr_ctxt->u1_mb_type = CAB_I16x16; 1904 } 1905 else 1906 { 1907 ps_curr_ctxt->u1_mb_type = CAB_I4x4; 1908 } 1909 1910 ps_ent_ctxt->pv_mb_header_data = ((WORD8 *)ps_ent_ctxt->pv_mb_header_data) + byte_count; 1911 1912 return IH264E_SUCCESS; 1913 } 1914 else /* Inter MB */ 1915 { 1916 /* Starting bitstream offset for header in bits */ 1917 bitstream_start_offset = GET_NUM_BITS(ps_bitstream); 1918 /* Encoding P16x16 */ 1919 if (mb_type != PSKIP) 1920 { 1921 cbp = *pu1_byte++; 1922 byte_count++; 1923 mb_qp_delta = *pu1_byte++; 1924 byte_count++; 1925 1926 /* Encoding mb_skip */ 1927 ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE); 1928 1929 /* Encoding mb_type as P16x16 */ 1930 { 1931 UWORD32 u4_ctx_inc_p; 1932 u4_ctx_inc_p = (0x010 + ((2) << 8)); 1933 1934 ih264e_encode_decision_bins(0, 3, u4_ctx_inc_p, 3, 1935 &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_P_SLICE]), 1936 ps_cabac_ctxt); 1937 } 1938 ps_curr_ctxt->u1_mb_type = CAB_P; 1939 { 1940 WORD16 *pi2_mv_ptr = (WORD16 *) pu1_byte; 1941 byte_count += 4; 1942 ps_curr_ctxt->u1_mb_type = (ps_curr_ctxt->u1_mb_type 1943 | CAB_NON_BD16x16); 1944 /* Encoding motion vector for P16x16 */ 1945 ih264e_cabac_enc_mvds_p16x16(ps_cabac_ctxt, pi2_mv_ptr); 1946 } 1947 /* Encode CBP */ 1948 ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); 1949 1950 if (cbp) 1951 { 1952 /* encode mb_qp_delta */ 1953 ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); 1954 } 1955 1956 /* Ending bitstream offset for header in bits */ 1957 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1958 ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset 1959 - bitstream_start_offset; 1960 /* Starting bitstream offset for residue */ 1961 bitstream_start_offset = bitstream_end_offset; 1962 1963 } 1964 else/* MB = PSKIP */ 1965 { 1966 ih264e_cabac_enc_mb_skip(1, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE); 1967 1968 ps_curr_ctxt->u1_mb_type = CAB_P_SKIP; 1969 (*ps_ent_ctxt->pi4_mb_skip_run)++; 1970 1971 memset(ps_curr_ctxt->u1_mv, 0, 16); 1972 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); 1973 cbp = 0; 1974 1975 /* Ending bitstream offset for header in bits */ 1976 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1977 ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset 1978 - bitstream_start_offset; 1979 /* Starting bitstream offset for residue */ 1980 1981 } 1982 1983 if (cbp > 0) 1984 { 1985 /* Encode residue */ 1986 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); 1987 /* Ending bitstream offset for reside in bits */ 1988 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 1989 ps_ent_ctxt->u4_residue_bits[1] += bitstream_end_offset 1990 - bitstream_start_offset; 1991 1992 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; 1993 ps_curr_ctxt->u1_yuv_dc_csbp &= 0x6; 1994 } 1995 else 1996 { 1997 ps_curr_ctxt->u1_yuv_ac_csbp = 0; 1998 ps_curr_ctxt->u1_yuv_dc_csbp = 0; 1999 *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; 2000 *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; 2001 *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; 2002 } 2003 ps_curr_ctxt->u1_intrapred_chroma_mode = 0; 2004 ps_curr_ctxt->u1_cbp = cbp; 2005 ps_ent_ctxt->pv_mb_header_data = ((WORD8 *)ps_ent_ctxt->pv_mb_header_data) + byte_count; 2006 return IH264E_SUCCESS; 2007 } 2008 } 2009 2010 2011 /* ! < Table 9-37 Binarization for macroblock types in B slices in ITU_T_H264-201402 2012 * Bits 0-7 : binarised value 2013 * Bits 8-15: length of binary sequence */ 2014 2015 2016 static const UWORD32 u4_b_mb_type[27] = { 0x0100, 0x0301, 0x0305, 0x0603, 2017 0x0623, 0x0613, 0x0633, 0x060b, 2018 0x062b, 0x061b, 0x063b, 0x061f, 2019 0x0707, 0x0747, 0x0727, 0x0767, 2020 0x0717, 0x0757, 0x0737, 0x0777, 2021 0x070f, 0x074f, 0x063f }; 2022 /* CtxInc for mb types in B slices */ 2023 static const UWORD32 ui_b_mb_type_ctx_inc[27] = { 0x00, 0x0530, 0x0530, 2024 0x0555430, 0x0555430, 2025 0x0555430, 0x0555430, 2026 0x0555430, 0x0555430, 2027 0x0555430, 0x0555430, 2028 0x0555430, 0x05555430, 2029 0x05555430, 0x05555430, 2030 0x05555430, 0x05555430, 2031 0x05555430, 0x05555430, 2032 0x05555430, 0x05555430, 2033 0x05555430, 0x0555430 }; 2034 2035 /** 2036 ******************************************************************************* 2037 * 2038 * @brief 2039 * This function generates CABAC coded bit stream for B slices 2040 * 2041 * @description 2042 * The mb syntax layer for inter slices constitutes luma mb mode, 2043 * mb qp delta, coded block pattern, chroma mb mode and 2044 * luma/chroma residue. These syntax elements are written as directed by table 2045 * 7.3.5 of h264 specification 2046 * 2047 * @param[in] ps_ent_ctxt 2048 * pointer to entropy context 2049 * 2050 * @returns error code 2051 * 2052 * @remarks none 2053 * 2054 ******************************************************************************* 2055 */ 2056 IH264E_ERROR_T ih264e_write_bslice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt) 2057 { 2058 /* bit stream ptr */ 2059 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm; 2060 /* CABAC context */ 2061 cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; 2062 2063 mb_info_ctxt_t *ps_curr_ctxt; 2064 2065 WORD32 bitstream_start_offset, bitstream_end_offset; 2066 WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode; 2067 WORD8 mb_qp_delta; 2068 UWORD32 u4_cbp_l, u4_cbp_c; 2069 WORD32 byte_count = 0; 2070 UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data; 2071 2072 if ((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) 2073 >= ps_bitstream->u4_max_strm_size) 2074 { 2075 /* return without corrupting the buffer beyond its size */ 2076 return (IH264E_BITSTREAM_BUFFER_OVERFLOW); 2077 } 2078 /* mb header info */ 2079 mb_tpm = *pu1_byte++; 2080 byte_count++; 2081 2082 /* mb type */ 2083 mb_type = mb_tpm & 0xF; 2084 /* CABAC contexts for the MB */ 2085 ih264e_get_cabac_context(ps_ent_ctxt, mb_type); 2086 ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; 2087 2088 /* if Intra MB */ 2089 if (mb_type == I16x16 || mb_type == I4x4) 2090 { 2091 cbp = *pu1_byte++; 2092 byte_count++; 2093 mb_qp_delta = *pu1_byte++; 2094 byte_count++; 2095 2096 /* Starting bitstream offset for header in bits */ 2097 bitstream_start_offset = GET_NUM_BITS(ps_bitstream); 2098 2099 /* Encode mb_skip_flag */ 2100 ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); 2101 u4_cbp_c = (cbp >> 4); 2102 u4_cbp_l = (cbp & 0xF); 2103 if (mb_type == I16x16) 2104 { 2105 luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u4_cbp_c << 2) 2106 + (u4_cbp_l == 15) * 12; 2107 } 2108 else 2109 { 2110 luma_intra_mode = 0; 2111 } 2112 /* Encode intra mb type */ 2113 { 2114 mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; 2115 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 2116 UWORD32 u4_ctx_inc = 0; 2117 2118 if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 2119 u4_ctx_inc += ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) 2120 != CAB_BD16x16) ? 1 : 0; 2121 if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 2122 u4_ctx_inc += ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) 2123 != CAB_BD16x16) ? 1 : 0; 2124 2125 /* Intra Prefix Only "111101" */ 2126 u4_ctx_inc = (u4_ctx_inc | 0x05555430); 2127 ih264e_encode_decision_bins(0x2f, 2128 6, 2129 u4_ctx_inc, 2130 3, 2131 ps_cabac_ctxt->au1_cabac_ctxt_table 2132 + MB_TYPE_B_SLICE, 2133 ps_cabac_ctxt); 2134 2135 ih264e_cabac_enc_intra_mb_type(BSLICE, (UWORD8) luma_intra_mode, 2136 ps_cabac_ctxt, MB_TYPE_B_SLICE); 2137 2138 } 2139 2140 if (mb_type == I4x4) 2141 { /* Intra 4x4 modes */ 2142 ih264e_cabac_enc_4x4mb_modes(ps_cabac_ctxt, pu1_byte); 2143 byte_count += 8; 2144 } 2145 chroma_intra_mode = (mb_tpm >> 6); 2146 2147 ih264e_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt); 2148 2149 if (mb_type != I16x16) 2150 { 2151 /* encode CBP */ 2152 ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); 2153 } 2154 2155 if ((cbp > 0) || (mb_type == I16x16)) 2156 { 2157 ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); 2158 2159 /* Ending bitstream offset for header in bits */ 2160 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2161 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset 2162 - bitstream_start_offset; 2163 /* Starting bitstream offset for residue */ 2164 bitstream_start_offset = bitstream_end_offset; 2165 2166 /* Encoding Residue */ 2167 if (mb_type == I16x16) 2168 { 2169 ps_curr_ctxt->u1_mb_type = CAB_I16x16; 2170 ps_curr_ctxt->u1_cbp = (UWORD8) cbp; 2171 ih264e_cabac_encode_residue_luma_dc(ps_ent_ctxt); 2172 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_AC_CTXCAT); 2173 } 2174 else 2175 { 2176 ps_curr_ctxt->u1_cbp = (UWORD8) cbp; 2177 ps_curr_ctxt->u1_mb_type = I4x4; 2178 ps_curr_ctxt->u1_mb_type = CAB_I4x4; 2179 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); 2180 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; 2181 ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6; 2182 } 2183 2184 /* Ending bitstream offset for reside in bits */ 2185 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2186 ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset 2187 - bitstream_start_offset; 2188 } 2189 else 2190 { 2191 ps_curr_ctxt->u1_yuv_ac_csbp = 0; 2192 ps_curr_ctxt->u1_yuv_dc_csbp = 0; 2193 *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; 2194 *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; 2195 *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; 2196 /* Ending bitstream offset for header in bits */ 2197 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2198 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset 2199 - bitstream_start_offset; 2200 } 2201 2202 memset(ps_curr_ctxt->u1_mv, 0, 16); 2203 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); 2204 ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_cbp = (UWORD8) cbp; 2205 2206 if (mb_type == I16x16) 2207 { 2208 ps_curr_ctxt->u1_mb_type = CAB_I16x16; 2209 } 2210 else 2211 { 2212 ps_curr_ctxt->u1_mb_type = CAB_I4x4; 2213 } 2214 2215 ps_ent_ctxt->pv_mb_header_data = ((WORD8 *)ps_ent_ctxt->pv_mb_header_data) + byte_count; 2216 2217 return IH264E_SUCCESS; 2218 } 2219 2220 else /* Inter MB */ 2221 { 2222 /* Starting bitstream offset for header in bits */ 2223 bitstream_start_offset = GET_NUM_BITS(ps_bitstream); 2224 /* Encoding B_Direct_16x16 */ 2225 if (mb_type == BDIRECT) 2226 { 2227 cbp = *pu1_byte++; 2228 byte_count++; 2229 mb_qp_delta = *pu1_byte++; 2230 byte_count++; 2231 2232 /* Encoding mb_skip */ 2233 ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); 2234 2235 /* Encoding mb_type as B_Direct_16x16 */ 2236 { 2237 2238 mb_info_ctxt_t *ps_left_ctxt = 2239 ps_cabac_ctxt->ps_left_ctxt_mb_info; 2240 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 2241 UWORD32 u4_ctx_inc = 0; 2242 2243 if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 2244 u4_ctx_inc += ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) 2245 != CAB_BD16x16) ? 1 : 0; 2246 if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 2247 u4_ctx_inc += ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) 2248 != CAB_BD16x16) ? 1 : 0; 2249 /* Encode the bin */ 2250 ih264e_cabac_encode_bin( 2251 ps_cabac_ctxt, 2252 0, 2253 ps_cabac_ctxt->au1_cabac_ctxt_table 2254 + MB_TYPE_B_SLICE + u4_ctx_inc); 2255 2256 } 2257 ps_curr_ctxt->u1_mb_type = CAB_BD16x16; 2258 memset(ps_curr_ctxt->u1_mv, 0, 16); 2259 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); 2260 2261 /* Encode CBP */ 2262 ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); 2263 2264 if (cbp) 2265 { 2266 /* encode mb_qp_delta */ 2267 ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); 2268 } 2269 2270 /* Ending bitstream offset for header in bits */ 2271 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2272 ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset 2273 - bitstream_start_offset; 2274 /* Starting bitstream offset for residue */ 2275 bitstream_start_offset = bitstream_end_offset; 2276 /* Starting bitstream offset for residue */ 2277 2278 } 2279 2280 else if (mb_type == BSKIP)/* MB = BSKIP */ 2281 { 2282 ih264e_cabac_enc_mb_skip(1, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); 2283 2284 ps_curr_ctxt->u1_mb_type = CAB_B_SKIP; 2285 2286 memset(ps_curr_ctxt->u1_mv, 0, 16); 2287 memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); 2288 cbp = 0; 2289 2290 /* Ending bitstream offset for header in bits */ 2291 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2292 ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset 2293 - bitstream_start_offset; 2294 /* Starting bitstream offset for residue */ 2295 2296 } 2297 2298 else /* mbype is B_L0_16x16, B_L1_16x16 or B_Bi_16x16 */ 2299 { 2300 WORD32 i4_mb_part_pred_mode = (mb_tpm >> 4); 2301 UWORD32 u4_mb_type = mb_type - B16x16 + B_L0_16x16 2302 + i4_mb_part_pred_mode; 2303 cbp = *pu1_byte++; 2304 byte_count++; 2305 mb_qp_delta = *pu1_byte++; 2306 byte_count++; 2307 2308 /* Encoding mb_skip */ 2309 ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); 2310 2311 /* Encoding mb_type as B16x16 */ 2312 { 2313 mb_info_ctxt_t *ps_left_ctxt = 2314 ps_cabac_ctxt->ps_left_ctxt_mb_info; 2315 mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; 2316 UWORD32 u4_ctx_inc = 0; 2317 2318 UWORD32 u4_mb_type_bins = u4_b_mb_type[u4_mb_type]; 2319 UWORD32 u4_bin_len = (u4_mb_type_bins >> 8) & 0x0F; 2320 u4_mb_type_bins = u4_mb_type_bins & 0xFF; 2321 2322 if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 2323 u4_ctx_inc += ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) 2324 != CAB_BD16x16) ? 1 : 0; 2325 if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) 2326 u4_ctx_inc += ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) 2327 != CAB_BD16x16) ? 1 : 0; 2328 2329 u4_ctx_inc = u4_ctx_inc | ui_b_mb_type_ctx_inc[u4_mb_type]; 2330 2331 ih264e_encode_decision_bins(u4_mb_type_bins, 2332 u4_bin_len, 2333 u4_ctx_inc, 2334 u4_bin_len, 2335 &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_B_SLICE]), 2336 ps_cabac_ctxt); 2337 } 2338 2339 ps_curr_ctxt->u1_mb_type = CAB_NON_BD16x16; 2340 { 2341 WORD16 *pi2_mv_ptr = (WORD16 *) pu1_byte; 2342 /* Get the pred modes */ 2343 2344 byte_count += 4 * (1 + (i4_mb_part_pred_mode == PRED_BI)); 2345 2346 ps_curr_ctxt->u1_mb_type = (ps_curr_ctxt->u1_mb_type 2347 | CAB_NON_BD16x16); 2348 /* Encoding motion vector for B16x16 */ 2349 ih264e_cabac_enc_mvds_b16x16(ps_cabac_ctxt, pi2_mv_ptr, 2350 i4_mb_part_pred_mode); 2351 } 2352 /* Encode CBP */ 2353 ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); 2354 2355 if (cbp) 2356 { 2357 /* encode mb_qp_delta */ 2358 ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); 2359 } 2360 2361 /* Ending bitstream offset for header in bits */ 2362 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2363 ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset 2364 - bitstream_start_offset; 2365 /* Starting bitstream offset for residue */ 2366 bitstream_start_offset = bitstream_end_offset; 2367 } 2368 2369 if (cbp > 0) 2370 { 2371 /* Encode residue */ 2372 ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); 2373 /* Ending bitstream offset for reside in bits */ 2374 bitstream_end_offset = GET_NUM_BITS(ps_bitstream); 2375 ps_ent_ctxt->u4_residue_bits[1] += bitstream_end_offset 2376 - bitstream_start_offset; 2377 2378 ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; 2379 ps_curr_ctxt->u1_yuv_dc_csbp &= 0x6; 2380 } 2381 else 2382 { 2383 ps_curr_ctxt->u1_yuv_ac_csbp = 0; 2384 ps_curr_ctxt->u1_yuv_dc_csbp = 0; 2385 *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; 2386 *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; 2387 *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; 2388 } 2389 ps_curr_ctxt->u1_intrapred_chroma_mode = 0; 2390 ps_curr_ctxt->u1_cbp = cbp; 2391 ps_ent_ctxt->pv_mb_header_data = ((WORD8 *)ps_ent_ctxt->pv_mb_header_data) + byte_count; 2392 return IH264E_SUCCESS; 2393 } 2394 } 2395