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