/src/libavc/encoder/ih264e_cabac_encode.c
Line | Count | Source (jump to first uncovered line) |
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 | | */ |
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 | 96.7k | { |
179 | 96.7k | UWORD8 u4_ctx_inc; |
180 | 96.7k | WORD8 a, b; |
181 | | |
182 | 96.7k | a = ((ps_cabac_ctxt->ps_left_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? |
183 | 66.4k | 0 : 1); |
184 | 96.7k | b = ((ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ? |
185 | 64.0k | 0 : 1); |
186 | | |
187 | 96.7k | u4_ctx_inc = a + b; |
188 | | |
189 | | /* Encode the bin */ |
190 | 96.7k | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
191 | 96.7k | (UWORD32) u1_mb_skip_flag, |
192 | 96.7k | ps_cabac_ctxt->au1_cabac_ctxt_table + |
193 | 96.7k | u4_ctxidx_offset + u4_ctx_inc); |
194 | 96.7k | } |
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 | | */ |
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 | 3.18M | { |
225 | 3.18M | encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env); |
226 | 3.18M | bin_ctxt_model *pu1_mb_bin_ctxt, *pu1_bin_ctxt; |
227 | 3.18M | UWORD8 u1_bin; |
228 | 3.18M | mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; |
229 | 3.18M | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
230 | 3.18M | UWORD32 u4_bins; |
231 | 3.18M | UWORD32 u4_ctx_inc; |
232 | 3.18M | WORD8 i1_bins_len; |
233 | 3.18M | UWORD32 u4_code_int_range; |
234 | 3.18M | UWORD32 u4_code_int_low; |
235 | 3.18M | UWORD16 u2_quant_code_int_range; |
236 | 3.18M | UWORD16 u4_code_int_range_lps; |
237 | 3.18M | WORD8 i; |
238 | 3.18M | UWORD8 u1_ctx_inc; |
239 | 3.18M | UWORD32 u4_table_val; |
240 | | |
241 | 3.18M | pu1_mb_bin_ctxt = ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset; |
242 | | |
243 | 3.18M | u4_bins = u4_mb_type_intra[u4_intra_mb_type]; |
244 | 3.18M | i1_bins_len = (WORD8) ((u4_bins >> 8) & 0x0f); |
245 | 3.18M | u4_ctx_inc = u4_mb_ctxinc[(u4_slice_type != ISLICE)][u4_intra_mb_type]; |
246 | 3.18M | u1_ctx_inc = 0; |
247 | 3.18M | if (u4_slice_type == ISLICE) |
248 | 3.14M | { |
249 | 3.14M | if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
250 | 3.10M | u1_ctx_inc += ((ps_left_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0); |
251 | 3.14M | if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
252 | 3.09M | u1_ctx_inc += ((ps_top_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0); |
253 | | |
254 | 3.14M | u4_ctx_inc = (u4_ctx_inc | (u1_ctx_inc << ((i1_bins_len - 1) << 2))); |
255 | 3.14M | } |
256 | 37.9k | else |
257 | 37.9k | { |
258 | 37.9k | pu1_mb_bin_ctxt += 3; |
259 | 37.9k | if (u4_slice_type == BSLICE) |
260 | 15.4k | pu1_mb_bin_ctxt += 2; |
261 | | |
262 | 37.9k | } |
263 | | |
264 | 3.18M | u4_code_int_range = ps_cab_enc_env->u4_code_int_range; |
265 | 3.18M | u4_code_int_low = ps_cab_enc_env->u4_code_int_low; |
266 | | |
267 | 14.4M | for (i = (i1_bins_len - 1); i >= 0; i--) |
268 | 11.2M | { |
269 | 11.2M | WORD32 shift; |
270 | | |
271 | 11.2M | u1_ctx_inc = ((u4_ctx_inc >> (i << 2)) & 0x0f); |
272 | 11.2M | u1_bin = ((u4_bins >> i) & 0x01); |
273 | | /* Encode the bin */ |
274 | 11.2M | pu1_bin_ctxt = pu1_mb_bin_ctxt + u1_ctx_inc; |
275 | 11.2M | if (i != (i1_bins_len - 2)) |
276 | 9.66M | { |
277 | 9.66M | WORD8 i1_mps = !!((*pu1_bin_ctxt) & (0x40)); |
278 | 9.66M | WORD8 i1_state = (*pu1_bin_ctxt) & 0x3F; |
279 | | |
280 | 9.66M | u2_quant_code_int_range = ((u4_code_int_range >> 6) & 0x03); |
281 | 9.66M | u4_table_val = gau4_ih264_cabac_table[i1_state][u2_quant_code_int_range]; |
282 | 9.66M | u4_code_int_range_lps = u4_table_val & 0xFF; |
283 | | |
284 | 9.66M | u4_code_int_range -= u4_code_int_range_lps; |
285 | 9.66M | if (u1_bin != i1_mps) |
286 | 57.6k | { |
287 | 57.6k | u4_code_int_low += u4_code_int_range; |
288 | 57.6k | u4_code_int_range = u4_code_int_range_lps; |
289 | 57.6k | if (i1_state == 0) |
290 | 5.94k | { |
291 | | /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */ |
292 | 5.94k | i1_mps = 1 - i1_mps; |
293 | 5.94k | } |
294 | | |
295 | 57.6k | i1_state = (u4_table_val >> 15) & 0x3F; |
296 | 57.6k | } |
297 | 9.60M | else |
298 | 9.60M | { |
299 | 9.60M | i1_state = (u4_table_val >> 8) & 0x3F; |
300 | | |
301 | 9.60M | } |
302 | | |
303 | 9.66M | (*pu1_bin_ctxt) = (i1_mps << 6) | i1_state; |
304 | 9.66M | } |
305 | 1.61M | else |
306 | 1.61M | { |
307 | 1.61M | u4_code_int_range -= 2; |
308 | 1.61M | } |
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 | 11.2M | GETRANGE(shift, u4_code_int_range); |
316 | 11.2M | shift = 9 - shift; |
317 | 11.2M | u4_code_int_low <<= shift; |
318 | 11.2M | u4_code_int_range <<= shift; |
319 | | |
320 | | /* bits to be inserted in the bitstream */ |
321 | 11.2M | ps_cab_enc_env->u4_bits_gen += shift; |
322 | 11.2M | ps_cab_enc_env->u4_code_int_range = u4_code_int_range; |
323 | 11.2M | ps_cab_enc_env->u4_code_int_low = u4_code_int_low; |
324 | | |
325 | | /* generate stream when a byte is ready */ |
326 | 11.2M | if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS) |
327 | 65.5k | { |
328 | 65.5k | ih264e_cabac_put_byte(ps_cabac_ctxt); |
329 | 65.5k | u4_code_int_range = ps_cab_enc_env->u4_code_int_range; |
330 | 65.5k | u4_code_int_low = ps_cab_enc_env->u4_code_int_low; |
331 | 65.5k | } |
332 | 11.2M | } |
333 | 3.18M | } |
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 | | */ |
355 | | static void ih264e_cabac_enc_4x4mb_modes(cabac_ctxt_t *ps_cabac_ctxt, |
356 | | UWORD8 *pu1_intra_4x4_modes) |
357 | 1.56M | { |
358 | 1.56M | WORD32 i; |
359 | 1.56M | WORD8 byte; |
360 | | |
361 | 14.1M | for (i = 0; i < 16; i += 2) |
362 | 12.5M | { |
363 | | /* sub blk idx 1 */ |
364 | 12.5M | byte = pu1_intra_4x4_modes[i >> 1]; |
365 | 12.5M | if (byte & 0x1) |
366 | 12.3M | { |
367 | 12.3M | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
368 | 12.3M | 1, |
369 | 12.3M | ps_cabac_ctxt->au1_cabac_ctxt_table |
370 | 12.3M | + PREV_INTRA4X4_PRED_MODE_FLAG); |
371 | 12.3M | } |
372 | 189k | else |
373 | 189k | { |
374 | | /* Binarization is FL and Cmax=7 */ |
375 | 189k | ih264e_encode_decision_bins(byte & 0xF, |
376 | 189k | 4, |
377 | 189k | 0x05554, |
378 | 189k | 4, |
379 | 189k | ps_cabac_ctxt->au1_cabac_ctxt_table |
380 | 189k | + REM_INTRA4X4_PRED_MODE - 5, |
381 | 189k | ps_cabac_ctxt); |
382 | 189k | } |
383 | | /* sub blk idx 2 */ |
384 | 12.5M | byte >>= 4; |
385 | 12.5M | if (byte & 0x1) |
386 | 12.4M | { |
387 | 12.4M | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
388 | 12.4M | 1, |
389 | 12.4M | ps_cabac_ctxt->au1_cabac_ctxt_table |
390 | 12.4M | + PREV_INTRA4X4_PRED_MODE_FLAG); |
391 | 12.4M | } |
392 | 138k | else |
393 | 138k | { |
394 | 138k | ih264e_encode_decision_bins(byte & 0xF, |
395 | 138k | 4, |
396 | 138k | 0x05554, |
397 | 138k | 4, |
398 | 138k | ps_cabac_ctxt->au1_cabac_ctxt_table |
399 | 138k | + REM_INTRA4X4_PRED_MODE - 5, |
400 | 138k | ps_cabac_ctxt); |
401 | 138k | } |
402 | 12.5M | } |
403 | 1.56M | } |
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 | | */ |
423 | | static void ih264e_cabac_enc_chroma_predmode(UWORD8 u1_chroma_pred_mode, |
424 | | cabac_ctxt_t *ps_cabac_ctxt) |
425 | 3.18M | { |
426 | 3.18M | WORD8 i1_temp; |
427 | 3.18M | mb_info_ctxt_t *ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
428 | 3.18M | mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; |
429 | 3.18M | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
430 | 3.18M | UWORD32 u4_bins = 0; |
431 | 3.18M | WORD8 i1_bins_len = 1; |
432 | 3.18M | UWORD32 u4_ctx_inc = 0; |
433 | 3.18M | UWORD8 a, b; |
434 | | |
435 | 3.18M | a = ((ps_left_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0); |
436 | 3.18M | b = ((ps_top_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0); |
437 | | |
438 | | /* Binarization is TU and Cmax=3 */ |
439 | 3.18M | ps_curr_ctxt->u1_intrapred_chroma_mode = u1_chroma_pred_mode; |
440 | | |
441 | 3.18M | u4_ctx_inc = a + b; |
442 | 3.18M | u4_ctx_inc = (u4_ctx_inc | 0x330); |
443 | 3.18M | if (u1_chroma_pred_mode) |
444 | 66.4k | { |
445 | 66.4k | u4_bins = 1; |
446 | 66.4k | i1_temp = u1_chroma_pred_mode; |
447 | 66.4k | i1_temp--; |
448 | | /* Put a stream of 1's of length Chromaps_pred_mode_ctxt value */ |
449 | 112k | while (i1_temp) |
450 | 45.8k | { |
451 | 45.8k | u4_bins = (u4_bins | (1 << i1_bins_len)); |
452 | 45.8k | i1_bins_len++; |
453 | 45.8k | i1_temp--; |
454 | 45.8k | } |
455 | | /* If Chromaps_pred_mode_ctxt < Cmax i.e 3. Terminate put a zero */ |
456 | 66.4k | if (u1_chroma_pred_mode < 3) |
457 | 62.0k | { |
458 | 62.0k | i1_bins_len++; |
459 | 62.0k | } |
460 | 66.4k | } |
461 | | |
462 | 3.18M | ih264e_encode_decision_bins(u4_bins, |
463 | 3.18M | i1_bins_len, |
464 | 3.18M | u4_ctx_inc, |
465 | 3.18M | 3, |
466 | 3.18M | ps_cabac_ctxt->au1_cabac_ctxt_table |
467 | 3.18M | + INTRA_CHROMA_PRED_MODE, |
468 | 3.18M | ps_cabac_ctxt); |
469 | 3.18M | } |
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 | | */ |
488 | | static void ih264e_cabac_enc_cbp(UWORD32 u4_cbp, cabac_ctxt_t *ps_cabac_ctxt) |
489 | 1.62M | { |
490 | 1.62M | mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; |
491 | 1.62M | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
492 | 1.62M | WORD8 i2_cbp_chroma, i, j; |
493 | 1.62M | UWORD8 u1_ctxt_inc, u1_bin; |
494 | 1.62M | UWORD8 a, b; |
495 | 1.62M | UWORD32 u4_ctx_inc; |
496 | 1.62M | UWORD32 u4_bins; |
497 | 1.62M | WORD8 i1_bins_len; |
498 | | |
499 | | /* CBP Luma, FL, Cmax = 15, L = 4 */ |
500 | 1.62M | u4_ctx_inc = 0; |
501 | 1.62M | u4_bins = 0; |
502 | 1.62M | i1_bins_len = 5; |
503 | 8.10M | for (i = 0; i < 4; i++) |
504 | 6.48M | { |
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 | 6.48M | if (i >> 1) |
511 | 3.24M | { |
512 | 3.24M | j = i - 2; |
513 | | /* Top is available always and it's current MB */ |
514 | 3.24M | b = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1); |
515 | 3.24M | } |
516 | 3.24M | else |
517 | 3.24M | { |
518 | | /* for blocks whose top reference is in another MB */ |
519 | 3.24M | { |
520 | 3.24M | j = i + 2; |
521 | 3.24M | b = ((ps_top_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1; |
522 | 3.24M | } |
523 | 3.24M | } |
524 | | |
525 | | /* Check for Left availability */ |
526 | 6.48M | if (i & 0x01) |
527 | 3.24M | { |
528 | | /* Left is available always and it's current MB */ |
529 | 3.24M | j = i - 1; |
530 | 3.24M | a = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1); |
531 | 3.24M | } |
532 | 3.24M | else |
533 | 3.24M | { |
534 | 3.24M | { |
535 | 3.24M | j = i + 1; |
536 | 3.24M | a = ((ps_left_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1; |
537 | 3.24M | } |
538 | 3.24M | } |
539 | 6.48M | u1_ctxt_inc = a + 2 * b; |
540 | 6.48M | u1_bin = ((u4_cbp >> i) & 0x01); |
541 | 6.48M | u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i << 2))); |
542 | 6.48M | u4_bins = (u4_bins | (u1_bin << i)); |
543 | 6.48M | } |
544 | | |
545 | | /* CBP Chroma, TU, Cmax = 2 */ |
546 | 1.62M | i2_cbp_chroma = u4_cbp >> 4; |
547 | | /* calulate ctxtInc, depending on neighbour availability */ |
548 | 1.62M | a = (ps_left_ctxt->u1_cbp > 15) ? 1 : 0; |
549 | 1.62M | b = (ps_top_ctxt->u1_cbp > 15) ? 1 : 0; |
550 | | |
551 | 1.62M | u1_ctxt_inc = a + 2 * b; |
552 | 1.62M | if (i2_cbp_chroma) |
553 | 75.4k | { |
554 | 75.4k | u4_ctx_inc = u4_ctx_inc | ((4 + u1_ctxt_inc) << 16); |
555 | 75.4k | u4_bins = (u4_bins | 0x10); |
556 | | /* calulate ctxtInc, depending on neighbour availability */ |
557 | 75.4k | a = (ps_left_ctxt->u1_cbp > 31) ? 1 : 0; |
558 | 75.4k | b = (ps_top_ctxt->u1_cbp > 31) ? 1 : 0; |
559 | 75.4k | u1_ctxt_inc = a + 2 * b; |
560 | 75.4k | u4_ctx_inc = u4_ctx_inc | ((8 + u1_ctxt_inc) << 20); |
561 | 75.4k | u4_bins = (u4_bins | (((i2_cbp_chroma >> 1) & 0x01) << i1_bins_len)); |
562 | 75.4k | i1_bins_len++; |
563 | 75.4k | } |
564 | 1.54M | else |
565 | 1.54M | { |
566 | 1.54M | u4_ctx_inc = (u4_ctx_inc | ((4 + u1_ctxt_inc) << 16)); |
567 | 1.54M | } |
568 | 1.62M | ih264e_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 8, |
569 | 1.62M | ps_cabac_ctxt->au1_cabac_ctxt_table + CBP_LUMA, |
570 | 1.62M | ps_cabac_ctxt); |
571 | 1.62M | } |
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 | | */ |
590 | | static void ih264e_cabac_enc_mb_qp_delta(WORD8 i1_mb_qp_delta, |
591 | | cabac_ctxt_t *ps_cabac_ctxt) |
592 | 1.70M | { |
593 | 1.70M | UWORD8 u1_code_num; |
594 | 1.70M | UWORD8 u1_ctxt_inc; |
595 | 1.70M | UWORD32 u4_ctx_inc; |
596 | 1.70M | UWORD32 u4_bins; |
597 | 1.70M | WORD8 i1_bins_len; |
598 | 1.70M | UWORD8 u1_ctx_inc, u1_bin; |
599 | | |
600 | | /* Range of ps_mb_qp_delta_ctxt= -26 to +25 inclusive */ |
601 | 1.70M | ASSERT((i1_mb_qp_delta < 26) && (i1_mb_qp_delta > -27)); |
602 | | |
603 | | /* if ps_mb_qp_delta_ctxt=0, then codeNum=0 */ |
604 | 1.70M | u1_code_num = 0; |
605 | 1.70M | if (i1_mb_qp_delta > 0) |
606 | 0 | u1_code_num = (i1_mb_qp_delta << 1) - 1; |
607 | 1.70M | else if (i1_mb_qp_delta < 0) |
608 | 0 | u1_code_num = (ABS(i1_mb_qp_delta)) << 1; |
609 | | |
610 | 1.70M | u4_ctx_inc = 0; |
611 | 1.70M | u4_bins = 0; |
612 | 1.70M | i1_bins_len = 1; |
613 | | /* calculate ctxtInc, depending on neighbour availability */ |
614 | 1.70M | u1_ctxt_inc = (!(!(ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt))); |
615 | 1.70M | ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = i1_mb_qp_delta; |
616 | | |
617 | 1.70M | if (u1_code_num == 0) |
618 | 1.70M | { |
619 | | /* b0 */ |
620 | 1.70M | u1_bin = (UWORD8) (u4_bins); |
621 | 1.70M | u1_ctx_inc = u1_ctxt_inc & 0x0f; |
622 | | /* Encode the bin */ |
623 | 1.70M | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
624 | 1.70M | u1_bin, |
625 | 1.70M | ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA |
626 | 1.70M | + u1_ctx_inc); |
627 | | |
628 | 1.70M | } |
629 | 0 | else |
630 | 0 | { |
631 | | /* b0 */ |
632 | 0 | u4_ctx_inc = u1_ctxt_inc; |
633 | 0 | u4_bins = 1; |
634 | 0 | u1_code_num--; |
635 | 0 | if (u1_code_num == 0) |
636 | 0 | { |
637 | | /* b1 */ |
638 | 0 | u4_ctx_inc = (u4_ctx_inc | 0x20); |
639 | 0 | i1_bins_len++; |
640 | 0 | ih264e_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 3, |
641 | 0 | ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA, |
642 | 0 | ps_cabac_ctxt); |
643 | 0 | } |
644 | 0 | else |
645 | 0 | { |
646 | | /* b1 */ |
647 | 0 | u4_ctx_inc = (u4_ctx_inc | 0x20); |
648 | 0 | u4_bins = (u4_bins | (1 << i1_bins_len)); |
649 | 0 | i1_bins_len++; |
650 | 0 | u1_code_num--; |
651 | | /* BinIdx from b2 onwards */ |
652 | 0 | if (u1_code_num < 30) |
653 | 0 | { /* maximum i1_bins_len = 31 */ |
654 | 0 | while (u1_code_num) |
655 | 0 | { |
656 | 0 | u4_bins = (u4_bins | (1 << i1_bins_len)); |
657 | 0 | i1_bins_len++; |
658 | 0 | u1_code_num--; |
659 | 0 | }; |
660 | 0 | u4_ctx_inc = (u4_ctx_inc | 0x300); |
661 | 0 | i1_bins_len++; |
662 | 0 | ih264e_encode_decision_bins(u4_bins, |
663 | 0 | i1_bins_len, |
664 | 0 | u4_ctx_inc, |
665 | 0 | 2, |
666 | 0 | ps_cabac_ctxt->au1_cabac_ctxt_table |
667 | 0 | + MB_QP_DELTA, |
668 | 0 | ps_cabac_ctxt); |
669 | 0 | } |
670 | 0 | else |
671 | 0 | { |
672 | | /* maximum i1_bins_len = 53 */ |
673 | 0 | u4_bins = 0xffffffff; |
674 | 0 | i1_bins_len = 32; |
675 | 0 | u4_ctx_inc = (u4_ctx_inc | 0x300); |
676 | 0 | u1_code_num -= 30; |
677 | 0 | ih264e_encode_decision_bins(u4_bins, |
678 | 0 | i1_bins_len, |
679 | 0 | u4_ctx_inc, |
680 | 0 | 2, |
681 | 0 | ps_cabac_ctxt->au1_cabac_ctxt_table |
682 | 0 | + MB_QP_DELTA, |
683 | 0 | ps_cabac_ctxt); |
684 | 0 | u4_bins = 0; |
685 | 0 | i1_bins_len = 0; |
686 | 0 | u4_ctx_inc = 0x033; |
687 | 0 | while (u1_code_num) |
688 | 0 | { |
689 | 0 | u4_bins = (u4_bins | (1 << i1_bins_len)); |
690 | 0 | i1_bins_len++; |
691 | 0 | u1_code_num--; |
692 | 0 | }; |
693 | |
|
694 | 0 | u4_ctx_inc = (u4_ctx_inc | 0x300); |
695 | 0 | i1_bins_len++; |
696 | 0 | ih264e_encode_decision_bins(u4_bins, |
697 | 0 | i1_bins_len, |
698 | 0 | u4_ctx_inc, |
699 | 0 | 1, |
700 | 0 | ps_cabac_ctxt->au1_cabac_ctxt_table |
701 | 0 | + MB_QP_DELTA, |
702 | 0 | ps_cabac_ctxt); |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | 1.70M | } |
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 | | */ |
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 | 1.57M | { |
747 | 1.57M | WORD8 i; |
748 | 1.57M | WORD16 *pi16_coeffs; |
749 | 1.57M | UWORD32 u4_sig_coeff, u4_bins; |
750 | 1.57M | UWORD32 u4_ctx_inc; |
751 | 1.57M | UWORD8 u1_last_sig_coef_index = (31 - CLZ(u2_sig_coeff_map)); |
752 | | |
753 | | /* Always put Coded Block Flag as 1 */ |
754 | 1.57M | pi16_coeffs = pi2_res_block; |
755 | 1.57M | { |
756 | 1.57M | bin_ctxt_model *pu1_bin_ctxt; |
757 | 1.57M | UWORD8 u1_bin, uc_last; |
758 | | |
759 | 1.57M | i = 0; |
760 | 1.57M | pu1_bin_ctxt = pu1_ctxt_sig_coeff; |
761 | 1.57M | u4_sig_coeff = 0; |
762 | 1.57M | u1_bin = 1; |
763 | 1.57M | if ((u1_last_sig_coef_index)) |
764 | 1.36M | { |
765 | 1.36M | u1_bin = !!(u2_sig_coeff_map & 01); |
766 | 1.36M | } |
767 | 1.57M | uc_last = 1; |
768 | | |
769 | 1.57M | do |
770 | 28.0M | { |
771 | | /* Encode Decision */ |
772 | 28.0M | ih264e_cabac_encode_bin(ps_cabac_ctxt, u1_bin, pu1_bin_ctxt); |
773 | | |
774 | 28.0M | if (u1_bin & uc_last) |
775 | 12.3M | { |
776 | 12.3M | u4_sig_coeff = (u4_sig_coeff | (1 << i)); |
777 | 12.3M | pu1_bin_ctxt = pu1_ctxt_sig_coeff + i |
778 | 12.3M | + LAST_SIGNIFICANT_COEFF_FLAG_FRAME |
779 | 12.3M | - SIGNIFICANT_COEFF_FLAG_FRAME; |
780 | 12.3M | u1_bin = (i == u1_last_sig_coef_index); |
781 | 12.3M | uc_last = 0; |
782 | 12.3M | } |
783 | 15.6M | else |
784 | 15.6M | { |
785 | 15.6M | i = i + 1; |
786 | 15.6M | pu1_bin_ctxt = pu1_ctxt_sig_coeff + i; |
787 | 15.6M | u1_bin = (i == u1_last_sig_coef_index); |
788 | 15.6M | uc_last = 1; |
789 | 15.6M | if ((i != u1_last_sig_coef_index)) |
790 | 14.2M | { |
791 | 14.2M | u1_bin = !!((u2_sig_coeff_map >> i) & 01); |
792 | 14.2M | } |
793 | 15.6M | } |
794 | 28.0M | } while (!((i > u1_last_sig_coef_index) || (i > (u1_max_num_coeffs - 1)))); |
795 | 1.57M | } |
796 | | |
797 | | /* Encode coeff_abs_level_minus1 and coeff_sign_flag */ |
798 | 1.57M | { |
799 | 1.57M | UWORD8 u1_sign; |
800 | 1.57M | UWORD16 u2_abs_level; |
801 | 1.57M | UWORD8 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0; |
802 | 1.57M | UWORD8 u1_ctx_inc; |
803 | 1.57M | UWORD8 u1_coff; |
804 | 1.57M | WORD16 i2_sufs; |
805 | 1.57M | WORD8 i1_bins_len; |
806 | | |
807 | 1.57M | i = u1_last_sig_coef_index; |
808 | 1.57M | pi16_coeffs = pi2_res_block + u1_nnz - 1; |
809 | 1.57M | do |
810 | 13.1M | { |
811 | 13.1M | { |
812 | 13.1M | u4_sig_coeff = u4_sig_coeff & ((1 << i) - 1); |
813 | 13.1M | u4_bins = 0; |
814 | 13.1M | u4_ctx_inc = 0; |
815 | 13.1M | i1_bins_len = 1; |
816 | | /* Encode the AbsLevelMinus1 */ |
817 | 13.1M | u2_abs_level = ABS(*(pi16_coeffs)) - 1; |
818 | | /* CtxInc for bin0 */ |
819 | 13.1M | u4_ctx_inc = MIN(u1_abs_level_equal1, 4); |
820 | | /* CtxInc for remaining */ |
821 | 13.1M | u1_ctx_inc = 5 + MIN(u1_abs_level_gt1, 4); |
822 | 13.1M | u4_ctx_inc = u4_ctx_inc + (u1_ctx_inc << 4); |
823 | 13.1M | if (u2_abs_level) |
824 | 10.8M | { |
825 | 10.8M | u1_abs_level_gt1++; |
826 | 10.8M | u1_abs_level_equal1 = 0; |
827 | 10.8M | } |
828 | 13.1M | if (!u1_abs_level_gt1) |
829 | 1.20M | u1_abs_level_equal1++; |
830 | | |
831 | 13.1M | u1_coff = 14; |
832 | 13.1M | if (u2_abs_level >= u1_coff) |
833 | 5.79M | { |
834 | | /* Prefix TU i.e string of 14 1's */ |
835 | 5.79M | u4_bins = 0x3fff; |
836 | 5.79M | i1_bins_len = 14; |
837 | 5.79M | ih264e_encode_decision_bins( |
838 | 5.79M | u4_bins, |
839 | 5.79M | i1_bins_len, |
840 | 5.79M | u4_ctx_inc, |
841 | 5.79M | 1, |
842 | 5.79M | ps_cabac_ctxt->au1_cabac_ctxt_table |
843 | 5.79M | + u4_ctx_cat_offset, |
844 | 5.79M | ps_cabac_ctxt); |
845 | | |
846 | | /* Suffix, uses EncodeBypass */ |
847 | 5.79M | i2_sufs = u2_abs_level - u1_coff; |
848 | | |
849 | 5.79M | u4_bins = ih264e_cabac_UEGk0_binarization(i2_sufs, |
850 | 5.79M | &i1_bins_len); |
851 | | |
852 | 5.79M | ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, |
853 | 5.79M | i1_bins_len); |
854 | 5.79M | } |
855 | 7.35M | else |
856 | 7.35M | { |
857 | | /* Prefix only */ |
858 | 7.35M | u4_bins = (1 << u2_abs_level) - 1; |
859 | 7.35M | i1_bins_len = u2_abs_level + 1; |
860 | | /* Encode Terminating bit */ |
861 | 7.35M | ih264e_encode_decision_bins( |
862 | 7.35M | u4_bins, |
863 | 7.35M | i1_bins_len, |
864 | 7.35M | u4_ctx_inc, |
865 | 7.35M | 1, |
866 | 7.35M | ps_cabac_ctxt->au1_cabac_ctxt_table |
867 | 7.35M | + u4_ctx_cat_offset, |
868 | 7.35M | ps_cabac_ctxt); |
869 | 7.35M | } |
870 | 13.1M | } |
871 | | /* encode coeff_sign_flag[i] */ |
872 | 13.1M | u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0; |
873 | 13.1M | ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, u1_sign); |
874 | 13.1M | i = CLZ(u4_sig_coeff); |
875 | 13.1M | i = 31 - i; |
876 | 13.1M | pi16_coeffs--; |
877 | 13.1M | } while (u4_sig_coeff); |
878 | 1.57M | } |
879 | 1.57M | } |
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 | | */ |
895 | | static void ih264e_cabac_encode_residue_luma_dc(entropy_ctxt_t *ps_ent_ctxt) |
896 | 1.61M | { |
897 | 1.61M | cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
898 | 1.61M | tu_sblk_coeff_data_t *ps_mb_coeff_data; |
899 | 1.61M | void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data; |
900 | 1.61M | UWORD16 u2_sig_coeff_map; |
901 | 1.61M | WORD16 *pi2_res_block; |
902 | 1.61M | UWORD8 u1_nnz; |
903 | 1.61M | UWORD8 u1_cbf; |
904 | 1.61M | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
905 | 1.61M | mb_info_ctxt_t *p_CurCtxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
906 | | |
907 | 1.61M | PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz, |
908 | 1.61M | u2_sig_coeff_map, pi2_res_block); |
909 | | |
910 | 1.61M | u1_cbf = !!(u1_nnz); |
911 | | |
912 | 1.61M | { |
913 | 1.61M | UWORD32 u4_ctx_inc; |
914 | 1.61M | UWORD8 u1_a, u1_b; |
915 | | |
916 | 1.61M | u1_a = ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] & 0x1; |
917 | 1.61M | u1_b = ps_top_ctxt->u1_yuv_dc_csbp & 0x1; |
918 | 1.61M | u4_ctx_inc = u1_a + (u1_b << 1); |
919 | | |
920 | 1.61M | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
921 | 1.61M | u1_cbf, |
922 | 1.61M | ps_cabac_ctxt->au1_cabac_ctxt_table + CBF |
923 | 1.61M | + (LUMA_DC_CTXCAT << 2) + u4_ctx_inc); |
924 | 1.61M | } |
925 | | |
926 | | /* Write coded_block_flag */ |
927 | 1.61M | if (u1_cbf) |
928 | 18.5k | { |
929 | 18.5k | ih264e_cabac_write_coeff4x4(pi2_res_block, |
930 | 18.5k | u1_nnz, |
931 | 18.5k | 15, |
932 | 18.5k | u2_sig_coeff_map, |
933 | 18.5k | COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET, |
934 | 18.5k | ps_cabac_ctxt->au1_cabac_ctxt_table |
935 | 18.5k | + SIGNIFICANT_COEFF_FLAG_FRAME |
936 | 18.5k | + SIG_COEFF_CTXT_CAT_0_OFFSET, |
937 | 18.5k | ps_cabac_ctxt); |
938 | | |
939 | 18.5k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= 0x1; |
940 | 18.5k | p_CurCtxt->u1_yuv_dc_csbp |= 0x1; |
941 | 18.5k | } |
942 | 1.59M | else |
943 | 1.59M | { |
944 | 1.59M | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; |
945 | 1.59M | p_CurCtxt->u1_yuv_dc_csbp &= 0x6; |
946 | 1.59M | } |
947 | | |
948 | 1.61M | ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data; |
949 | 1.61M | } |
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 | | */ |
968 | | static void ih264e_cabac_write_chroma_residue(entropy_ctxt_t *ps_ent_ctxt, |
969 | | UWORD8 u1_chroma_cbp) |
970 | 91.7k | { |
971 | 91.7k | cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
972 | 91.7k | tu_sblk_coeff_data_t *ps_mb_coeff_data; |
973 | 91.7k | void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data; |
974 | 91.7k | UWORD16 u2_sig_coeff_map; |
975 | 91.7k | UWORD8 u1_nnz; |
976 | 91.7k | mb_info_ctxt_t *ps_top_ctxt_mb_info, *ps_curr_ctxt; |
977 | | |
978 | 91.7k | ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
979 | 91.7k | ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
980 | | |
981 | | /********************/ |
982 | | /* Write Chroma DC */ |
983 | | /********************/ |
984 | 91.7k | { |
985 | 91.7k | WORD16 *pi2_res_block; |
986 | 91.7k | UWORD8 u1_left_dc_csbp, u1_top_dc_csbp, u1_uv, u1_cbf; |
987 | | |
988 | 91.7k | u1_left_dc_csbp = (ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0]) >> 1; |
989 | 91.7k | u1_top_dc_csbp = (ps_top_ctxt_mb_info->u1_yuv_dc_csbp) >> 1; |
990 | | |
991 | 275k | for (u1_uv = 0; u1_uv < 2; u1_uv++) |
992 | 183k | { |
993 | 183k | PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, |
994 | 183k | u1_nnz, u2_sig_coeff_map, pi2_res_block); |
995 | 183k | u1_cbf = !!(u1_nnz); |
996 | 183k | { |
997 | 183k | UWORD8 u1_a, u1_b; |
998 | 183k | UWORD32 u4_ctx_inc; |
999 | 183k | u1_a = (u1_left_dc_csbp >> u1_uv) & 0x01; |
1000 | 183k | u1_b = (u1_top_dc_csbp >> u1_uv) & 0x01; |
1001 | 183k | u4_ctx_inc = (u1_a + (u1_b << 1)); |
1002 | | |
1003 | 183k | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
1004 | 183k | u1_cbf, |
1005 | 183k | ps_cabac_ctxt->au1_cabac_ctxt_table + CBF |
1006 | 183k | + (CHROMA_DC_CTXCAT << 2) |
1007 | 183k | + u4_ctx_inc); |
1008 | 183k | } |
1009 | | |
1010 | 183k | if (u1_cbf) |
1011 | 167k | { |
1012 | 167k | ih264e_cabac_write_coeff4x4(pi2_res_block, |
1013 | 167k | u1_nnz, |
1014 | 167k | 3, |
1015 | 167k | u2_sig_coeff_map, |
1016 | 167k | COEFF_ABS_LEVEL_MINUS1 |
1017 | 167k | + COEFF_ABS_LEVEL_CAT_3_OFFSET, |
1018 | 167k | ps_cabac_ctxt->au1_cabac_ctxt_table |
1019 | 167k | + SIGNIFICANT_COEFF_FLAG_FRAME |
1020 | 167k | + SIG_COEFF_CTXT_CAT_3_OFFSET, |
1021 | 167k | ps_cabac_ctxt); |
1022 | | |
1023 | 167k | SETBIT(u1_top_dc_csbp, u1_uv); |
1024 | 167k | SETBIT(u1_left_dc_csbp, u1_uv); |
1025 | 167k | } |
1026 | 16.3k | else |
1027 | 16.3k | { |
1028 | 16.3k | CLEARBIT(u1_top_dc_csbp, u1_uv); |
1029 | 16.3k | CLEARBIT(u1_left_dc_csbp, u1_uv); |
1030 | 16.3k | } |
1031 | 183k | } |
1032 | | /*************************************************************/ |
1033 | | /* Update the DC csbp */ |
1034 | | /*************************************************************/ |
1035 | 91.7k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1; |
1036 | 91.7k | ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1; |
1037 | 91.7k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= (u1_left_dc_csbp << 1); |
1038 | 91.7k | ps_curr_ctxt->u1_yuv_dc_csbp |= (u1_top_dc_csbp << 1); |
1039 | 91.7k | } |
1040 | | /*******************/ |
1041 | | /* Write Chroma AC */ |
1042 | | /*******************/ |
1043 | 91.7k | { |
1044 | 91.7k | if (u1_chroma_cbp == 2) |
1045 | 74.0k | { |
1046 | 74.0k | UWORD8 u1_uv_blkno, u1_left_ac_csbp, u1_top_ac_csbp; |
1047 | 74.0k | WORD16 *pi2_res_block; |
1048 | 74.0k | u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_uv_ac_csbp[0]; |
1049 | 74.0k | u1_top_ac_csbp = ps_top_ctxt_mb_info->u1_yuv_ac_csbp >> 4; |
1050 | | |
1051 | 666k | for (u1_uv_blkno = 0; u1_uv_blkno < 8; u1_uv_blkno++) |
1052 | 592k | { |
1053 | 592k | UWORD8 u1_cbf; |
1054 | 592k | UWORD8 u1_b2b0, u1_b2b1; |
1055 | 592k | PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, |
1056 | 592k | u1_nnz, u2_sig_coeff_map, |
1057 | 592k | pi2_res_block); |
1058 | | |
1059 | 592k | u1_cbf = !!(u1_nnz); |
1060 | 592k | u1_b2b0 = ((u1_uv_blkno & 0x4) >> 1) | (u1_uv_blkno & 0x1); |
1061 | 592k | u1_b2b1 = ((u1_uv_blkno & 0x4) >> 1) |
1062 | 592k | | ((u1_uv_blkno & 0x2) >> 1); |
1063 | | |
1064 | 592k | { |
1065 | 592k | UWORD8 u1_a, u1_b; |
1066 | 592k | UWORD32 u4_ctx_inc; |
1067 | | /* write coded_block_flag */ |
1068 | 592k | u1_a = (u1_left_ac_csbp >> u1_b2b1) & 0x1; |
1069 | 592k | u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x1; |
1070 | 592k | u4_ctx_inc = u1_a + (u1_b << 1); |
1071 | | |
1072 | 592k | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
1073 | 592k | u1_cbf, |
1074 | 592k | ps_cabac_ctxt->au1_cabac_ctxt_table + CBF |
1075 | 592k | + (CHROMA_AC_CTXCAT << 2) |
1076 | 592k | + u4_ctx_inc); |
1077 | | |
1078 | 592k | } |
1079 | 592k | if (u1_cbf) |
1080 | 457k | { |
1081 | 457k | ih264e_cabac_write_coeff4x4(pi2_res_block, |
1082 | 457k | u1_nnz, |
1083 | 457k | 14, |
1084 | 457k | u2_sig_coeff_map, |
1085 | 457k | COEFF_ABS_LEVEL_MINUS1 |
1086 | 457k | + COEFF_ABS_LEVEL_CAT_4_OFFSET, |
1087 | 457k | ps_cabac_ctxt->au1_cabac_ctxt_table |
1088 | 457k | + +SIGNIFICANT_COEFF_FLAG_FRAME |
1089 | 457k | + SIG_COEFF_CTXT_CAT_4_OFFSET, |
1090 | 457k | ps_cabac_ctxt); |
1091 | | |
1092 | 457k | SETBIT(u1_left_ac_csbp, u1_b2b1); |
1093 | 457k | SETBIT(u1_top_ac_csbp, u1_b2b0); |
1094 | 457k | } |
1095 | 134k | else |
1096 | 134k | { |
1097 | 134k | CLEARBIT(u1_left_ac_csbp, u1_b2b1); |
1098 | 134k | CLEARBIT(u1_top_ac_csbp, u1_b2b0); |
1099 | | |
1100 | 134k | } |
1101 | 592k | } |
1102 | | /*************************************************************/ |
1103 | | /* Update the AC csbp */ |
1104 | | /*************************************************************/ |
1105 | 74.0k | ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = u1_left_ac_csbp; |
1106 | 74.0k | ps_curr_ctxt->u1_yuv_ac_csbp &= 0x0f; |
1107 | 74.0k | ps_curr_ctxt->u1_yuv_ac_csbp |= (u1_top_ac_csbp << 4); |
1108 | 74.0k | } |
1109 | 17.7k | else |
1110 | 17.7k | { |
1111 | 17.7k | ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0; |
1112 | 17.7k | ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf; |
1113 | 17.7k | } |
1114 | 91.7k | } |
1115 | 91.7k | ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data; |
1116 | 91.7k | } |
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 | | */ |
1138 | | static void ih264e_cabac_encode_residue(entropy_ctxt_t *ps_ent_ctxt, |
1139 | | UWORD32 u4_cbp, UWORD8 u1_ctx_cat) |
1140 | 1.70M | { |
1141 | 1.70M | cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
1142 | 1.70M | tu_sblk_coeff_data_t *ps_mb_coeff_data; |
1143 | 1.70M | void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data; |
1144 | 1.70M | UWORD16 u2_sig_coeff_map; |
1145 | 1.70M | UWORD8 u1_nnz; |
1146 | 1.70M | mb_info_ctxt_t *ps_curr_ctxt; |
1147 | 1.70M | mb_info_ctxt_t *ps_top_ctxt; |
1148 | 1.70M | UWORD8 u1_left_ac_csbp; |
1149 | 1.70M | UWORD8 u1_top_ac_csbp; |
1150 | 1.70M | UWORD32 u4_ctx_idx_offset_sig_coef, u4_ctx_idx_offset_abs_lvl; |
1151 | | |
1152 | 1.70M | ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
1153 | 1.70M | ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
1154 | 1.70M | u1_left_ac_csbp = ps_cabac_ctxt->pu1_left_y_ac_csbp[0]; |
1155 | 1.70M | u1_top_ac_csbp = ps_top_ctxt->u1_yuv_ac_csbp; |
1156 | | |
1157 | 1.70M | if (u4_cbp & 0xf) |
1158 | 89.3k | { |
1159 | | /* Write luma residue */ |
1160 | 89.3k | UWORD8 u1_offset; |
1161 | 89.3k | WORD16 *pi2_res_block; |
1162 | 89.3k | UWORD8 u1_subblk_num; |
1163 | 89.3k | if (u1_ctx_cat == LUMA_AC_CTXCAT) |
1164 | 18.1k | { |
1165 | 18.1k | u1_offset = 1; |
1166 | 18.1k | u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_1_OFFSET; |
1167 | 18.1k | u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 |
1168 | 18.1k | + COEFF_ABS_LEVEL_CAT_1_OFFSET; |
1169 | 18.1k | } |
1170 | 71.1k | else |
1171 | 71.1k | { |
1172 | 71.1k | u1_offset = 0; |
1173 | 71.1k | u4_ctx_idx_offset_sig_coef = SIG_COEFF_CTXT_CAT_2_OFFSET; |
1174 | 71.1k | u4_ctx_idx_offset_abs_lvl = COEFF_ABS_LEVEL_MINUS1 |
1175 | 71.1k | + COEFF_ABS_LEVEL_CAT_2_OFFSET; |
1176 | 71.1k | } |
1177 | | |
1178 | 1.35M | for (u1_subblk_num = 0; u1_subblk_num < 16; u1_subblk_num++) |
1179 | 1.26M | { |
1180 | 1.26M | UWORD8 u1_b0, u1_b1, u1_b2, u1_b3, u1_b2b0, u1_b3b1, u1_b3b2; |
1181 | 1.26M | u1_b0 = (u1_subblk_num & 0x1); |
1182 | 1.26M | u1_b1 = (u1_subblk_num & 0x2) >> 1; |
1183 | 1.26M | u1_b2 = (u1_subblk_num & 0x4) >> 2; |
1184 | 1.26M | u1_b3 = (u1_subblk_num & 0x8) >> 3; |
1185 | 1.26M | u1_b2b0 = (u1_b2 << 1) | (u1_b0); |
1186 | 1.26M | u1_b3b1 = (u1_b3 << 1) | (u1_b1); |
1187 | 1.26M | u1_b3b2 = (u1_b3 << 1) | (u1_b2); |
1188 | | |
1189 | 1.26M | if (!((u4_cbp >> u1_b3b2) & 0x1)) |
1190 | 54.9k | { |
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 | 54.9k | CLEARBIT(u1_top_ac_csbp, u1_b2b0); |
1196 | 54.9k | CLEARBIT(u1_top_ac_csbp, (u1_b2b0 + 1)); |
1197 | 54.9k | CLEARBIT(u1_left_ac_csbp, u1_b3b1); |
1198 | 54.9k | CLEARBIT(u1_left_ac_csbp, (u1_b3b1 + 1)); |
1199 | | |
1200 | 54.9k | u1_subblk_num += 3; |
1201 | 54.9k | } |
1202 | 1.20M | else |
1203 | 1.20M | { |
1204 | 1.20M | UWORD8 u1_csbf; |
1205 | | |
1206 | 1.20M | PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, |
1207 | 1.20M | u1_nnz, u2_sig_coeff_map, |
1208 | 1.20M | pi2_res_block); |
1209 | | |
1210 | 1.20M | u1_csbf = !!(u1_nnz); |
1211 | 1.20M | { |
1212 | 1.20M | UWORD8 u1_a, u1_b; |
1213 | 1.20M | UWORD32 u4_ctx_inc; |
1214 | 1.20M | u1_b = (u1_top_ac_csbp >> u1_b2b0) & 0x01; |
1215 | 1.20M | u1_a = (u1_left_ac_csbp >> u1_b3b1) & 0x01; |
1216 | 1.20M | u4_ctx_inc = u1_a + (u1_b << 1); |
1217 | | |
1218 | | /* Encode the bin */ |
1219 | 1.20M | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
1220 | 1.20M | u1_csbf, |
1221 | 1.20M | ps_cabac_ctxt->au1_cabac_ctxt_table + CBF |
1222 | 1.20M | + (u1_ctx_cat << 2) + u4_ctx_inc); |
1223 | | |
1224 | 1.20M | } |
1225 | | /**************************/ |
1226 | | /* Write coded_block_flag */ |
1227 | | /**************************/ |
1228 | 1.20M | if (u1_csbf) |
1229 | 931k | { |
1230 | 931k | ih264e_cabac_write_coeff4x4(pi2_res_block, |
1231 | 931k | u1_nnz, |
1232 | 931k | (UWORD8) (15 - u1_offset), |
1233 | 931k | u2_sig_coeff_map, |
1234 | 931k | u4_ctx_idx_offset_abs_lvl, |
1235 | 931k | ps_cabac_ctxt->au1_cabac_ctxt_table |
1236 | 931k | + SIGNIFICANT_COEFF_FLAG_FRAME |
1237 | 931k | + u4_ctx_idx_offset_sig_coef, |
1238 | 931k | ps_cabac_ctxt); |
1239 | | |
1240 | 931k | SETBIT(u1_top_ac_csbp, u1_b2b0); |
1241 | 931k | SETBIT(u1_left_ac_csbp, u1_b3b1); |
1242 | 931k | } |
1243 | 277k | else |
1244 | 277k | { |
1245 | 277k | CLEARBIT(u1_top_ac_csbp, u1_b2b0); |
1246 | 277k | CLEARBIT(u1_left_ac_csbp, u1_b3b1); |
1247 | 277k | } |
1248 | 1.20M | } |
1249 | 1.26M | } |
1250 | | /**************************************************************************/ |
1251 | | /* Update the AC csbp */ |
1252 | | /**************************************************************************/ |
1253 | 89.3k | ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = u1_left_ac_csbp & 0xf; |
1254 | 89.3k | u1_top_ac_csbp &= 0x0f; |
1255 | 89.3k | ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0; |
1256 | 89.3k | ps_curr_ctxt->u1_yuv_ac_csbp |= u1_top_ac_csbp; |
1257 | 89.3k | } |
1258 | 1.61M | else |
1259 | 1.61M | { |
1260 | 1.61M | ps_cabac_ctxt->pu1_left_y_ac_csbp[0] = 0; |
1261 | 1.61M | ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf0; |
1262 | 1.61M | } |
1263 | | |
1264 | | /* Write chroma residue */ |
1265 | 1.70M | ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data; |
1266 | 1.70M | { |
1267 | 1.70M | UWORD8 u1_cbp_chroma; |
1268 | 1.70M | u1_cbp_chroma = u4_cbp >> 4; |
1269 | 1.70M | if (u1_cbp_chroma) |
1270 | 91.7k | { |
1271 | 91.7k | ih264e_cabac_write_chroma_residue(ps_ent_ctxt, u1_cbp_chroma); |
1272 | 91.7k | } |
1273 | 1.61M | else |
1274 | 1.61M | { |
1275 | 1.61M | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x1; |
1276 | 1.61M | ps_curr_ctxt->u1_yuv_dc_csbp &= 0x1; |
1277 | 1.61M | ps_cabac_ctxt->pu1_left_uv_ac_csbp[0] = 0; |
1278 | 1.61M | ps_curr_ctxt->u1_yuv_ac_csbp &= 0xf; |
1279 | 1.61M | } |
1280 | 1.70M | } |
1281 | 1.70M | } |
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 | | */ |
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 | 99.7k | { |
1310 | 99.7k | UWORD8 u1_bin, u1_ctxt_inc; |
1311 | 99.7k | WORD8 k = 3, u1_coff = 9; |
1312 | 99.7k | WORD16 i2_abs_mvd, i2_sufs; |
1313 | 99.7k | UWORD32 u4_ctx_inc; |
1314 | 99.7k | UWORD32 u4_bins; |
1315 | 99.7k | WORD8 i1_bins_len; |
1316 | | |
1317 | 99.7k | if (ui2_abs_mvd < 3) |
1318 | 65.1k | u4_ctx_inc = 0; |
1319 | 34.6k | else if (ui2_abs_mvd > 32) |
1320 | 16.0k | u4_ctx_inc = 2; |
1321 | 18.6k | else |
1322 | 18.6k | u4_ctx_inc = 1; |
1323 | | |
1324 | 99.7k | u4_bins = 0; |
1325 | 99.7k | i1_bins_len = 1; |
1326 | | |
1327 | 99.7k | if (u1_mvd == 0) |
1328 | 54.4k | { |
1329 | 54.4k | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
1330 | 54.4k | 0, |
1331 | 54.4k | ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset |
1332 | 54.4k | + u4_ctx_inc); |
1333 | 54.4k | } |
1334 | 45.3k | else |
1335 | 45.3k | { |
1336 | 45.3k | i2_abs_mvd = ABS(u1_mvd); |
1337 | 45.3k | if (i2_abs_mvd >= u1_coff) |
1338 | 29.8k | { |
1339 | | /* Prefix TU i.e string of 9 1's */ |
1340 | 29.8k | u4_bins = 0x1ff; |
1341 | 29.8k | i1_bins_len = 9; |
1342 | 29.8k | u4_ctx_inc = (u4_ctx_inc | 0x065430); |
1343 | | |
1344 | 29.8k | ih264e_encode_decision_bins(u4_bins, |
1345 | 29.8k | i1_bins_len, |
1346 | 29.8k | u4_ctx_inc, |
1347 | 29.8k | 4, |
1348 | 29.8k | ps_cabac_ctxt->au1_cabac_ctxt_table |
1349 | 29.8k | + u4_ctx_idx_offset, |
1350 | 29.8k | ps_cabac_ctxt); |
1351 | | |
1352 | | /* Suffix, uses EncodeBypass */ |
1353 | 29.8k | u4_bins = 0; |
1354 | 29.8k | i1_bins_len = 0; |
1355 | 29.8k | i2_sufs = i2_abs_mvd - u1_coff; |
1356 | 77.7k | while (1) |
1357 | 77.7k | { |
1358 | 77.7k | if (i2_sufs >= (1 << k)) |
1359 | 47.8k | { |
1360 | 47.8k | u4_bins = (u4_bins | (1 << (31 - i1_bins_len))); |
1361 | 47.8k | i1_bins_len++; |
1362 | 47.8k | i2_sufs = i2_sufs - (1 << k); |
1363 | 47.8k | k++; |
1364 | 47.8k | } |
1365 | 29.8k | else |
1366 | 29.8k | { |
1367 | 29.8k | i1_bins_len++; |
1368 | 167k | while (k--) |
1369 | 137k | { |
1370 | 137k | u1_bin = ((i2_sufs >> k) & 0x01); |
1371 | 137k | u4_bins = (u4_bins | (u1_bin << (31 - i1_bins_len))); |
1372 | 137k | i1_bins_len++; |
1373 | 137k | } |
1374 | 29.8k | break; |
1375 | 29.8k | } |
1376 | 77.7k | } |
1377 | 29.8k | u4_bins >>= (32 - i1_bins_len); |
1378 | 29.8k | ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, |
1379 | 29.8k | i1_bins_len); |
1380 | 29.8k | } |
1381 | 15.4k | else |
1382 | 15.4k | { |
1383 | | /* Prefix only */ |
1384 | | /* b0 */ |
1385 | 15.4k | u4_bins = 1; |
1386 | 15.4k | i2_abs_mvd--; |
1387 | 15.4k | u1_ctxt_inc = 3; |
1388 | 67.3k | while (i2_abs_mvd) |
1389 | 51.8k | { |
1390 | 51.8k | i2_abs_mvd--; |
1391 | 51.8k | u4_bins = (u4_bins | (1 << i1_bins_len)); |
1392 | 51.8k | if (u1_ctxt_inc <= 6) |
1393 | 41.3k | { |
1394 | 41.3k | u4_ctx_inc = (u4_ctx_inc |
1395 | 41.3k | | (u1_ctxt_inc << (i1_bins_len << 2))); |
1396 | 41.3k | u1_ctxt_inc++; |
1397 | 41.3k | } |
1398 | 51.8k | i1_bins_len++; |
1399 | 51.8k | } |
1400 | | /* Encode Terminating bit */ |
1401 | 15.4k | if (i1_bins_len <= 4) |
1402 | 10.5k | u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i1_bins_len << 2))); |
1403 | 15.4k | i1_bins_len++; |
1404 | 15.4k | ih264e_encode_decision_bins(u4_bins, |
1405 | 15.4k | i1_bins_len, |
1406 | 15.4k | u4_ctx_inc, |
1407 | 15.4k | 4, |
1408 | 15.4k | ps_cabac_ctxt->au1_cabac_ctxt_table |
1409 | 15.4k | + u4_ctx_idx_offset, |
1410 | 15.4k | ps_cabac_ctxt); |
1411 | 15.4k | } |
1412 | | /* sign bit, uses EncodeBypass */ |
1413 | 45.3k | if (u1_mvd > 0) |
1414 | 21.0k | ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, 0); |
1415 | 24.3k | else |
1416 | 24.3k | ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, 1); |
1417 | 45.3k | } |
1418 | 99.7k | } |
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 | | */ |
1437 | | static void ih264e_cabac_enc_mvds_p16x16(cabac_ctxt_t *ps_cabac_ctxt, |
1438 | | WORD16 *pi2_mv_ptr) |
1439 | 23.7k | { |
1440 | | /* Encode the differential component of the motion vectors */ |
1441 | 23.7k | { |
1442 | 23.7k | UWORD8 u1_abs_mvd_x, u1_abs_mvd_y; |
1443 | 23.7k | UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt; |
1444 | 23.7k | WORD16 u2_mv; |
1445 | | |
1446 | 23.7k | u1_abs_mvd_x = 0; |
1447 | 23.7k | u1_abs_mvd_y = 0; |
1448 | 23.7k | pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0]; |
1449 | 23.7k | pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0]; |
1450 | 23.7k | { |
1451 | 23.7k | UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, |
1452 | 23.7k | u2_abs_mvd_y_b; |
1453 | 23.7k | u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0]; |
1454 | 23.7k | u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1]; |
1455 | 23.7k | u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0]; |
1456 | 23.7k | u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1]; |
1457 | 23.7k | u2_mv = *(pi2_mv_ptr++); |
1458 | | |
1459 | 23.7k | ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_X, |
1460 | 23.7k | (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b), |
1461 | 23.7k | ps_cabac_ctxt); |
1462 | | |
1463 | 23.7k | u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv)); |
1464 | 23.7k | u2_mv = *(pi2_mv_ptr++); |
1465 | | |
1466 | 23.7k | ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_Y, |
1467 | 23.7k | (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b), |
1468 | 23.7k | ps_cabac_ctxt); |
1469 | | |
1470 | 23.7k | u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv)); |
1471 | 23.7k | } |
1472 | | /***************************************************************/ |
1473 | | /* Store abs_mvd_values cabac contexts */ |
1474 | | /***************************************************************/ |
1475 | 23.7k | pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x; |
1476 | 23.7k | pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y; |
1477 | 23.7k | } |
1478 | 23.7k | } |
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 | | */ |
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 | 21.9k | { |
1502 | | /* Encode the differential component of the motion vectors */ |
1503 | 21.9k | { |
1504 | 21.9k | UWORD8 u1_abs_mvd_x, u1_abs_mvd_y; |
1505 | 21.9k | UWORD8 *pu1_top_mv_ctxt, *pu1_lft_mv_ctxt; |
1506 | 21.9k | WORD16 u2_mv; |
1507 | | |
1508 | 21.9k | u1_abs_mvd_x = 0; |
1509 | 21.9k | u1_abs_mvd_y = 0; |
1510 | 21.9k | pu1_top_mv_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv[0]; |
1511 | 21.9k | pu1_lft_mv_ctxt = ps_cabac_ctxt->pu1_left_mv_ctxt_inc[0]; |
1512 | 21.9k | if (i4_mb_part_pred_mode != PRED_L1)/* || PRED_BI */ |
1513 | 14.9k | { |
1514 | 14.9k | UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, |
1515 | 14.9k | u2_abs_mvd_y_b; |
1516 | 14.9k | u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[0]; |
1517 | 14.9k | u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[1]; |
1518 | 14.9k | u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[0]; |
1519 | 14.9k | u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[1]; |
1520 | 14.9k | u2_mv = pi2_mv_ptr[0]; |
1521 | | |
1522 | 14.9k | ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_X, |
1523 | 14.9k | (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b), |
1524 | 14.9k | ps_cabac_ctxt); |
1525 | | |
1526 | 14.9k | u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv)); |
1527 | 14.9k | u2_mv = pi2_mv_ptr[1]; |
1528 | | |
1529 | 14.9k | ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_Y, |
1530 | 14.9k | (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b), |
1531 | 14.9k | ps_cabac_ctxt); |
1532 | | |
1533 | 14.9k | u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv)); |
1534 | 14.9k | } |
1535 | | |
1536 | | /***************************************************************/ |
1537 | | /* Store abs_mvd_values cabac contexts */ |
1538 | | /***************************************************************/ |
1539 | 21.9k | pu1_top_mv_ctxt[0] = pu1_lft_mv_ctxt[0] = u1_abs_mvd_x; |
1540 | 21.9k | pu1_top_mv_ctxt[1] = pu1_lft_mv_ctxt[1] = u1_abs_mvd_y; |
1541 | | |
1542 | 21.9k | u1_abs_mvd_x = 0; |
1543 | 21.9k | u1_abs_mvd_y = 0; |
1544 | 21.9k | if (i4_mb_part_pred_mode != PRED_L0)/* || PRED_BI */ |
1545 | 11.1k | { |
1546 | 11.1k | UWORD16 u2_abs_mvd_x_a, u2_abs_mvd_x_b, u2_abs_mvd_y_a, |
1547 | 11.1k | u2_abs_mvd_y_b; |
1548 | 11.1k | u2_abs_mvd_x_b = (UWORD16) pu1_top_mv_ctxt[2]; |
1549 | 11.1k | u2_abs_mvd_y_b = (UWORD16) pu1_top_mv_ctxt[3]; |
1550 | 11.1k | u2_abs_mvd_x_a = (UWORD16) pu1_lft_mv_ctxt[2]; |
1551 | 11.1k | u2_abs_mvd_y_a = (UWORD16) pu1_lft_mv_ctxt[3]; |
1552 | 11.1k | u2_mv = pi2_mv_ptr[2]; |
1553 | | |
1554 | 11.1k | ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_X, |
1555 | 11.1k | (UWORD16) (u2_abs_mvd_x_a + u2_abs_mvd_x_b), |
1556 | 11.1k | ps_cabac_ctxt); |
1557 | | |
1558 | 11.1k | u1_abs_mvd_x = CLIP3(0, 127, ABS(u2_mv)); |
1559 | 11.1k | u2_mv = pi2_mv_ptr[3]; |
1560 | | |
1561 | 11.1k | ih264e_cabac_enc_ctx_mvd(u2_mv, MVD_Y, |
1562 | 11.1k | (UWORD16) (u2_abs_mvd_y_a + u2_abs_mvd_y_b), |
1563 | 11.1k | ps_cabac_ctxt); |
1564 | | |
1565 | 11.1k | u1_abs_mvd_y = CLIP3(0, 127, ABS(u2_mv)); |
1566 | 11.1k | } |
1567 | | /***************************************************************/ |
1568 | | /* Store abs_mvd_values cabac contexts */ |
1569 | | /***************************************************************/ |
1570 | 21.9k | pu1_top_mv_ctxt[2] = pu1_lft_mv_ctxt[2] = u1_abs_mvd_x; |
1571 | 21.9k | pu1_top_mv_ctxt[3] = pu1_lft_mv_ctxt[3] = u1_abs_mvd_y; |
1572 | 21.9k | } |
1573 | 21.9k | } |
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 | | */ |
1595 | | IH264E_ERROR_T ih264e_write_islice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt) |
1596 | 3.15M | { |
1597 | 3.15M | bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm; |
1598 | 3.15M | cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
1599 | 3.15M | UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data; |
1600 | 3.15M | mb_hdr_common_t *ps_mb_hdr = (mb_hdr_common_t *)ps_ent_ctxt->pv_mb_header_data; |
1601 | 3.15M | mb_info_ctxt_t *ps_curr_ctxt; |
1602 | 3.15M | WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode; |
1603 | 3.15M | WORD8 mb_qp_delta; |
1604 | 3.15M | UWORD32 u4_cbp_l, u4_cbp_c; |
1605 | 3.15M | WORD32 bitstream_start_offset, bitstream_end_offset; |
1606 | | |
1607 | 3.15M | if ((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) |
1608 | 3.15M | >= ps_bitstream->u4_max_strm_size) |
1609 | 3.32k | { |
1610 | | /* return without corrupting the buffer beyond its size */ |
1611 | 3.32k | return (IH264E_BITSTREAM_BUFFER_OVERFLOW); |
1612 | 3.32k | } |
1613 | | /* mb header info */ |
1614 | 3.14M | mb_tpm = ps_mb_hdr->u1_mb_type_mode; |
1615 | 3.14M | cbp = ps_mb_hdr->u1_cbp; |
1616 | 3.14M | mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta; |
1617 | | |
1618 | | /* mb type */ |
1619 | 3.14M | mb_type = mb_tpm & 0xF; |
1620 | | |
1621 | 3.14M | ih264e_get_cabac_context(ps_ent_ctxt, mb_type); |
1622 | 3.14M | ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
1623 | | |
1624 | | /* Starting bitstream offset for header in bits */ |
1625 | 3.14M | bitstream_start_offset = GET_NUM_BITS(ps_bitstream); |
1626 | 3.14M | u4_cbp_c = (cbp >> 4); |
1627 | 3.14M | u4_cbp_l = (cbp & 0xF); |
1628 | 3.14M | if (mb_type == I16x16) |
1629 | 1.60M | { |
1630 | 1.60M | luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u4_cbp_c << 2) |
1631 | 1.60M | + (u4_cbp_l == 15) * 12; |
1632 | 1.60M | } |
1633 | 1.54M | else |
1634 | 1.54M | { |
1635 | 1.54M | luma_intra_mode = 0; |
1636 | 1.54M | } |
1637 | | |
1638 | 3.14M | chroma_intra_mode = (mb_tpm >> 6); |
1639 | | |
1640 | | /* Encode Intra pred mode, Luma */ |
1641 | 3.14M | ih264e_cabac_enc_intra_mb_type(ISLICE, luma_intra_mode, ps_cabac_ctxt, |
1642 | 3.14M | MB_TYPE_I_SLICE); |
1643 | | |
1644 | 3.14M | if (mb_type == I4x4) |
1645 | 1.54M | { |
1646 | | /* Encode 4x4 MB modes */ |
1647 | 1.54M | mb_hdr_i4x4_t *ps_mb_hdr_i4x4 = (mb_hdr_i4x4_t *)ps_ent_ctxt->pv_mb_header_data; |
1648 | 1.54M | ih264e_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes); |
1649 | 1.54M | } |
1650 | | /* Encode chroma mode */ |
1651 | 3.14M | ih264e_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt); |
1652 | | |
1653 | 3.14M | if (mb_type != I16x16) |
1654 | 1.54M | { /* Encode MB cbp */ |
1655 | 1.54M | ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); |
1656 | 1.54M | } |
1657 | | |
1658 | 3.14M | if ((cbp > 0) || (mb_type == I16x16)) |
1659 | 1.62M | { |
1660 | | /* Encode mb_qp_delta */ |
1661 | 1.62M | ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); |
1662 | | /* Ending bitstream offset for header in bits */ |
1663 | 1.62M | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1664 | 1.62M | ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset |
1665 | 1.62M | - bitstream_start_offset; |
1666 | | /* Starting bitstream offset for residue */ |
1667 | 1.62M | bitstream_start_offset = bitstream_end_offset; |
1668 | 1.62M | if (mb_type == I16x16) |
1669 | 1.60M | { |
1670 | 1.60M | ps_curr_ctxt->u1_mb_type = CAB_I16x16; |
1671 | 1.60M | ps_curr_ctxt->u1_cbp = cbp; |
1672 | 1.60M | ih264e_cabac_encode_residue_luma_dc(ps_ent_ctxt); |
1673 | 1.60M | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_AC_CTXCAT); |
1674 | 1.60M | } |
1675 | 17.0k | else |
1676 | 17.0k | { |
1677 | 17.0k | ps_curr_ctxt->u1_cbp = cbp; |
1678 | 17.0k | ps_curr_ctxt->u1_mb_type = I4x4; |
1679 | 17.0k | ps_curr_ctxt->u1_mb_type = CAB_I4x4; |
1680 | 17.0k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); |
1681 | 17.0k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; |
1682 | 17.0k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6; |
1683 | 17.0k | } |
1684 | | /* Ending bitstream offset for reside in bits */ |
1685 | 1.62M | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1686 | 1.62M | ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset |
1687 | 1.62M | - bitstream_start_offset; |
1688 | 1.62M | } |
1689 | 1.52M | else |
1690 | 1.52M | { |
1691 | 1.52M | ps_curr_ctxt->u1_yuv_ac_csbp = 0; |
1692 | 1.52M | ps_curr_ctxt->u1_yuv_dc_csbp = 0; |
1693 | 1.52M | *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; |
1694 | 1.52M | *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; |
1695 | 1.52M | *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; |
1696 | 1.52M | ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = 0; |
1697 | | /* Ending bitstream offset for header in bits */ |
1698 | 1.52M | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1699 | 1.52M | ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset |
1700 | 1.52M | - bitstream_start_offset; |
1701 | | |
1702 | | /* Computing the number of used used for encoding the MB syntax */ |
1703 | 1.52M | } |
1704 | 3.14M | memset(ps_curr_ctxt->u1_mv, 0, 16); |
1705 | 3.14M | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
1706 | 3.14M | ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_cbp = cbp; |
1707 | | |
1708 | 3.14M | if (mb_type == I16x16) |
1709 | 1.60M | { |
1710 | 1.60M | ps_curr_ctxt->u1_mb_type = CAB_I16x16; |
1711 | 1.60M | pu1_byte += sizeof(mb_hdr_i16x16_t); |
1712 | 1.60M | } |
1713 | 1.54M | else |
1714 | 1.54M | { |
1715 | 1.54M | ps_curr_ctxt->u1_mb_type = CAB_I4x4; |
1716 | 1.54M | pu1_byte += sizeof(mb_hdr_i4x4_t); |
1717 | 1.54M | } |
1718 | 3.14M | ps_ent_ctxt->pv_mb_header_data = pu1_byte; |
1719 | 3.14M | return IH264E_SUCCESS; |
1720 | 3.15M | } |
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 | | */ |
1742 | | IH264E_ERROR_T ih264e_write_pslice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt) |
1743 | 54.8k | { |
1744 | 54.8k | bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm; |
1745 | 54.8k | cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
1746 | 54.8k | mb_info_ctxt_t *ps_curr_ctxt; |
1747 | 54.8k | WORD32 bitstream_start_offset, bitstream_end_offset; |
1748 | 54.8k | WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode; |
1749 | 54.8k | WORD8 mb_qp_delta; |
1750 | 54.8k | UWORD32 u4_cbp_l, u4_cbp_c; |
1751 | 54.8k | UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data; |
1752 | 54.8k | mb_hdr_common_t *ps_mb_hdr = (mb_hdr_common_t *)ps_ent_ctxt->pv_mb_header_data; |
1753 | | |
1754 | 54.8k | if ((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) |
1755 | 54.8k | >= ps_bitstream->u4_max_strm_size) |
1756 | 3.84k | { |
1757 | | /* return without corrupting the buffer beyond its size */ |
1758 | 3.84k | return (IH264E_BITSTREAM_BUFFER_OVERFLOW); |
1759 | 3.84k | } |
1760 | | /* mb header info */ |
1761 | 51.0k | mb_tpm = ps_mb_hdr->u1_mb_type_mode; |
1762 | | |
1763 | | /* mb type */ |
1764 | 51.0k | mb_type = mb_tpm & 0xF; |
1765 | | /* CABAC contexts for the MB */ |
1766 | 51.0k | ih264e_get_cabac_context(ps_ent_ctxt, mb_type); |
1767 | 51.0k | ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
1768 | | |
1769 | | /* if Intra MB */ |
1770 | 51.0k | if (mb_type == I16x16 || mb_type == I4x4) |
1771 | 22.4k | { |
1772 | 22.4k | cbp = ps_mb_hdr->u1_cbp; |
1773 | 22.4k | mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta; |
1774 | | |
1775 | | /* Starting bitstream offset for header in bits */ |
1776 | 22.4k | bitstream_start_offset = GET_NUM_BITS(ps_bitstream); |
1777 | | |
1778 | | /* Encode mb_skip_flag */ |
1779 | 22.4k | ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE); |
1780 | 22.4k | u4_cbp_c = (cbp >> 4); |
1781 | 22.4k | u4_cbp_l = (cbp & 0xF); |
1782 | 22.4k | if (mb_type == I16x16) |
1783 | 6.99k | { |
1784 | 6.99k | luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u4_cbp_c << 2) |
1785 | 6.99k | + (u4_cbp_l == 15) * 12; |
1786 | 6.99k | } |
1787 | 15.4k | else |
1788 | 15.4k | { |
1789 | 15.4k | luma_intra_mode = 0; |
1790 | 15.4k | } |
1791 | | /* Encode intra mb type */ |
1792 | 22.4k | { |
1793 | 22.4k | ih264e_cabac_encode_bin(ps_cabac_ctxt, |
1794 | 22.4k | 1, |
1795 | 22.4k | ps_cabac_ctxt->au1_cabac_ctxt_table |
1796 | 22.4k | + MB_TYPE_P_SLICE); |
1797 | | |
1798 | 22.4k | ih264e_cabac_enc_intra_mb_type(PSLICE, (UWORD8) luma_intra_mode, |
1799 | 22.4k | ps_cabac_ctxt, MB_TYPE_P_SLICE); |
1800 | 22.4k | } |
1801 | | |
1802 | 22.4k | if (mb_type == I4x4) |
1803 | 15.4k | { |
1804 | | /* Intra 4x4 modes */ |
1805 | 15.4k | mb_hdr_i4x4_t *ps_mb_hdr_i4x4 = (mb_hdr_i4x4_t *)ps_ent_ctxt->pv_mb_header_data; |
1806 | 15.4k | ih264e_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes); |
1807 | 15.4k | } |
1808 | 22.4k | chroma_intra_mode = (mb_tpm >> 6); |
1809 | | |
1810 | 22.4k | ih264e_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt); |
1811 | | |
1812 | 22.4k | if (mb_type != I16x16) |
1813 | 15.4k | { |
1814 | | /* encode CBP */ |
1815 | 15.4k | ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); |
1816 | 15.4k | } |
1817 | | |
1818 | 22.4k | if ((cbp > 0) || (mb_type == I16x16)) |
1819 | 18.6k | { |
1820 | 18.6k | ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); |
1821 | | |
1822 | | /* Ending bitstream offset for header in bits */ |
1823 | 18.6k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1824 | 18.6k | ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset |
1825 | 18.6k | - bitstream_start_offset; |
1826 | | /* Starting bitstream offset for residue */ |
1827 | 18.6k | bitstream_start_offset = bitstream_end_offset; |
1828 | | |
1829 | | /* Encoding Residue */ |
1830 | 18.6k | if (mb_type == I16x16) |
1831 | 6.99k | { |
1832 | 6.99k | ps_curr_ctxt->u1_mb_type = CAB_I16x16; |
1833 | 6.99k | ps_curr_ctxt->u1_cbp = (UWORD8) cbp; |
1834 | 6.99k | ih264e_cabac_encode_residue_luma_dc(ps_ent_ctxt); |
1835 | 6.99k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_AC_CTXCAT); |
1836 | 6.99k | } |
1837 | 11.6k | else |
1838 | 11.6k | { |
1839 | 11.6k | ps_curr_ctxt->u1_cbp = (UWORD8) cbp; |
1840 | 11.6k | ps_curr_ctxt->u1_mb_type = I4x4; |
1841 | 11.6k | ps_curr_ctxt->u1_mb_type = CAB_I4x4; |
1842 | 11.6k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); |
1843 | 11.6k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; |
1844 | 11.6k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6; |
1845 | 11.6k | } |
1846 | | |
1847 | | /* Ending bitstream offset for reside in bits */ |
1848 | 18.6k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1849 | 18.6k | ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset |
1850 | 18.6k | - bitstream_start_offset; |
1851 | 18.6k | } |
1852 | 3.83k | else |
1853 | 3.83k | { |
1854 | 3.83k | ps_curr_ctxt->u1_yuv_ac_csbp = 0; |
1855 | 3.83k | ps_curr_ctxt->u1_yuv_dc_csbp = 0; |
1856 | 3.83k | *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; |
1857 | 3.83k | *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; |
1858 | 3.83k | *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; |
1859 | 3.83k | ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = 0; |
1860 | | /* Ending bitstream offset for header in bits */ |
1861 | 3.83k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1862 | 3.83k | ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset |
1863 | 3.83k | - bitstream_start_offset; |
1864 | 3.83k | } |
1865 | | |
1866 | 22.4k | memset(ps_curr_ctxt->u1_mv, 0, 16); |
1867 | 22.4k | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
1868 | 22.4k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_cbp = (UWORD8) cbp; |
1869 | | |
1870 | 22.4k | if (mb_type == I16x16) |
1871 | 6.99k | { |
1872 | 6.99k | ps_curr_ctxt->u1_mb_type = CAB_I16x16; |
1873 | 6.99k | pu1_byte += sizeof(mb_hdr_i16x16_t); |
1874 | 6.99k | } |
1875 | 15.4k | else |
1876 | 15.4k | { |
1877 | 15.4k | ps_curr_ctxt->u1_mb_type = CAB_I4x4; |
1878 | 15.4k | pu1_byte += sizeof(mb_hdr_i4x4_t); |
1879 | 15.4k | } |
1880 | | |
1881 | 22.4k | ps_ent_ctxt->pv_mb_header_data = pu1_byte; |
1882 | | |
1883 | 22.4k | return IH264E_SUCCESS; |
1884 | 22.4k | } |
1885 | 28.5k | else /* Inter MB */ |
1886 | 28.5k | { |
1887 | | /* Starting bitstream offset for header in bits */ |
1888 | 28.5k | bitstream_start_offset = GET_NUM_BITS(ps_bitstream); |
1889 | | /* Encoding P16x16 */ |
1890 | 28.5k | if (mb_type != PSKIP) |
1891 | 23.7k | { |
1892 | 23.7k | mb_hdr_p16x16_t *ps_mb_hdr_p16x16 = (mb_hdr_p16x16_t *)ps_ent_ctxt->pv_mb_header_data; |
1893 | 23.7k | cbp = ps_mb_hdr->u1_cbp; |
1894 | 23.7k | mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta; |
1895 | | |
1896 | | /* Encoding mb_skip */ |
1897 | 23.7k | ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE); |
1898 | | |
1899 | | /* Encoding mb_type as P16x16 */ |
1900 | 23.7k | { |
1901 | 23.7k | UWORD32 u4_ctx_inc_p; |
1902 | 23.7k | u4_ctx_inc_p = (0x010 + ((2) << 8)); |
1903 | | |
1904 | 23.7k | ih264e_encode_decision_bins(0, 3, u4_ctx_inc_p, 3, |
1905 | 23.7k | &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_P_SLICE]), |
1906 | 23.7k | ps_cabac_ctxt); |
1907 | 23.7k | } |
1908 | 23.7k | ps_curr_ctxt->u1_mb_type = CAB_P; |
1909 | 23.7k | { |
1910 | 23.7k | WORD16 *pi2_mv_ptr = (WORD16 *) ps_mb_hdr_p16x16->ai2_mv; |
1911 | | |
1912 | 23.7k | ps_curr_ctxt->u1_mb_type = (ps_curr_ctxt->u1_mb_type |
1913 | 23.7k | | CAB_NON_BD16x16); |
1914 | | /* Encoding motion vector for P16x16 */ |
1915 | 23.7k | ih264e_cabac_enc_mvds_p16x16(ps_cabac_ctxt, pi2_mv_ptr); |
1916 | 23.7k | } |
1917 | | /* Encode CBP */ |
1918 | 23.7k | ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); |
1919 | | |
1920 | 23.7k | if (cbp) |
1921 | 21.7k | { |
1922 | | /* encode mb_qp_delta */ |
1923 | 21.7k | ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); |
1924 | 21.7k | } |
1925 | | |
1926 | | /* Ending bitstream offset for header in bits */ |
1927 | 23.7k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1928 | 23.7k | ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset |
1929 | 23.7k | - bitstream_start_offset; |
1930 | | /* Starting bitstream offset for residue */ |
1931 | 23.7k | bitstream_start_offset = bitstream_end_offset; |
1932 | | |
1933 | 23.7k | pu1_byte += sizeof(mb_hdr_p16x16_t); |
1934 | | |
1935 | 23.7k | } |
1936 | 4.74k | else/* MB = PSKIP */ |
1937 | 4.74k | { |
1938 | 4.74k | ih264e_cabac_enc_mb_skip(1, ps_cabac_ctxt, MB_SKIP_FLAG_P_SLICE); |
1939 | | |
1940 | 4.74k | ps_curr_ctxt->u1_mb_type = CAB_P_SKIP; |
1941 | 4.74k | (*ps_ent_ctxt->pi4_mb_skip_run)++; |
1942 | | |
1943 | 4.74k | memset(ps_curr_ctxt->u1_mv, 0, 16); |
1944 | 4.74k | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
1945 | 4.74k | cbp = 0; |
1946 | | |
1947 | | /* Ending bitstream offset for header in bits */ |
1948 | 4.74k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1949 | 4.74k | ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset |
1950 | 4.74k | - bitstream_start_offset; |
1951 | | /* Starting bitstream offset for residue */ |
1952 | | |
1953 | 4.74k | pu1_byte += sizeof(mb_hdr_pskip_t); |
1954 | 4.74k | } |
1955 | | |
1956 | 28.5k | if (cbp > 0) |
1957 | 21.7k | { |
1958 | | /* Encode residue */ |
1959 | 21.7k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); |
1960 | | /* Ending bitstream offset for reside in bits */ |
1961 | 21.7k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
1962 | 21.7k | ps_ent_ctxt->u4_residue_bits[1] += bitstream_end_offset |
1963 | 21.7k | - bitstream_start_offset; |
1964 | | |
1965 | 21.7k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; |
1966 | 21.7k | ps_curr_ctxt->u1_yuv_dc_csbp &= 0x6; |
1967 | 21.7k | } |
1968 | 6.82k | else |
1969 | 6.82k | { |
1970 | 6.82k | ps_curr_ctxt->u1_yuv_ac_csbp = 0; |
1971 | 6.82k | ps_curr_ctxt->u1_yuv_dc_csbp = 0; |
1972 | 6.82k | *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; |
1973 | 6.82k | *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; |
1974 | 6.82k | *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; |
1975 | 6.82k | ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = 0; |
1976 | 6.82k | } |
1977 | 28.5k | ps_curr_ctxt->u1_intrapred_chroma_mode = 0; |
1978 | 28.5k | ps_curr_ctxt->u1_cbp = cbp; |
1979 | 28.5k | ps_ent_ctxt->pv_mb_header_data = pu1_byte; |
1980 | | |
1981 | 28.5k | return IH264E_SUCCESS; |
1982 | 28.5k | } |
1983 | 51.0k | } |
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 | | */ |
2005 | | IH264E_ERROR_T ih264e_write_bslice_mb_cabac(entropy_ctxt_t *ps_ent_ctxt) |
2006 | 50.7k | { |
2007 | 50.7k | bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm; |
2008 | 50.7k | cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac; |
2009 | 50.7k | mb_info_ctxt_t *ps_curr_ctxt; |
2010 | 50.7k | WORD32 bitstream_start_offset, bitstream_end_offset; |
2011 | 50.7k | WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode; |
2012 | 50.7k | WORD8 mb_qp_delta; |
2013 | 50.7k | UWORD32 u4_cbp_l, u4_cbp_c; |
2014 | 50.7k | UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data; |
2015 | 50.7k | mb_hdr_common_t *ps_mb_hdr = (mb_hdr_common_t *)ps_ent_ctxt->pv_mb_header_data; |
2016 | | |
2017 | 50.7k | if ((ps_bitstream->u4_strm_buf_offset + MIN_STREAM_SIZE_MB) |
2018 | 50.7k | >= ps_bitstream->u4_max_strm_size) |
2019 | 4.99k | { |
2020 | | /* return without corrupting the buffer beyond its size */ |
2021 | 4.99k | return (IH264E_BITSTREAM_BUFFER_OVERFLOW); |
2022 | 4.99k | } |
2023 | | /* mb header info */ |
2024 | 45.7k | mb_tpm = ps_mb_hdr->u1_mb_type_mode; |
2025 | | |
2026 | | /* mb type */ |
2027 | 45.7k | mb_type = mb_tpm & 0xF; |
2028 | | /* CABAC contexts for the MB */ |
2029 | 45.7k | ih264e_get_cabac_context(ps_ent_ctxt, mb_type); |
2030 | 45.7k | ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info; |
2031 | | |
2032 | | /* if Intra MB */ |
2033 | 45.7k | if (mb_type == I16x16 || mb_type == I4x4) |
2034 | 15.4k | { |
2035 | 15.4k | cbp = ps_mb_hdr->u1_cbp; |
2036 | 15.4k | mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta; |
2037 | | |
2038 | | /* Starting bitstream offset for header in bits */ |
2039 | 15.4k | bitstream_start_offset = GET_NUM_BITS(ps_bitstream); |
2040 | | |
2041 | | /* Encode mb_skip_flag */ |
2042 | 15.4k | ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); |
2043 | 15.4k | u4_cbp_c = (cbp >> 4); |
2044 | 15.4k | u4_cbp_l = (cbp & 0xF); |
2045 | 15.4k | if (mb_type == I16x16) |
2046 | 4.38k | { |
2047 | 4.38k | luma_intra_mode = ((mb_tpm >> 4) & 3) + 1 + (u4_cbp_c << 2) |
2048 | 4.38k | + (u4_cbp_l == 15) * 12; |
2049 | 4.38k | } |
2050 | 11.0k | else |
2051 | 11.0k | { |
2052 | 11.0k | luma_intra_mode = 0; |
2053 | 11.0k | } |
2054 | | /* Encode intra mb type */ |
2055 | 15.4k | { |
2056 | 15.4k | mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info; |
2057 | 15.4k | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
2058 | 15.4k | UWORD32 u4_ctx_inc = 0; |
2059 | | |
2060 | 15.4k | if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
2061 | 12.9k | u4_ctx_inc += ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) |
2062 | 12.9k | != CAB_BD16x16) ? 1 : 0; |
2063 | 15.4k | if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
2064 | 10.8k | u4_ctx_inc += ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) |
2065 | 10.8k | != CAB_BD16x16) ? 1 : 0; |
2066 | | |
2067 | | /* Intra Prefix Only "111101" */ |
2068 | 15.4k | u4_ctx_inc = (u4_ctx_inc | 0x05555430); |
2069 | 15.4k | ih264e_encode_decision_bins(0x2f, |
2070 | 15.4k | 6, |
2071 | 15.4k | u4_ctx_inc, |
2072 | 15.4k | 3, |
2073 | 15.4k | ps_cabac_ctxt->au1_cabac_ctxt_table |
2074 | 15.4k | + MB_TYPE_B_SLICE, |
2075 | 15.4k | ps_cabac_ctxt); |
2076 | | |
2077 | 15.4k | ih264e_cabac_enc_intra_mb_type(BSLICE, (UWORD8) luma_intra_mode, |
2078 | 15.4k | ps_cabac_ctxt, MB_TYPE_B_SLICE); |
2079 | | |
2080 | 15.4k | } |
2081 | | |
2082 | 15.4k | if (mb_type == I4x4) |
2083 | 11.0k | { |
2084 | | /* Intra 4x4 modes */ |
2085 | 11.0k | mb_hdr_i4x4_t *ps_mb_hdr_i4x4 = (mb_hdr_i4x4_t *)ps_ent_ctxt->pv_mb_header_data; |
2086 | 11.0k | ih264e_cabac_enc_4x4mb_modes(ps_cabac_ctxt, ps_mb_hdr_i4x4->au1_sub_blk_modes); |
2087 | 11.0k | } |
2088 | 15.4k | chroma_intra_mode = (mb_tpm >> 6); |
2089 | | |
2090 | 15.4k | ih264e_cabac_enc_chroma_predmode(chroma_intra_mode, ps_cabac_ctxt); |
2091 | | |
2092 | 15.4k | if (mb_type != I16x16) |
2093 | 11.0k | { |
2094 | | /* encode CBP */ |
2095 | 11.0k | ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); |
2096 | 11.0k | } |
2097 | | |
2098 | 15.4k | if ((cbp > 0) || (mb_type == I16x16)) |
2099 | 14.0k | { |
2100 | 14.0k | ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); |
2101 | | |
2102 | | /* Ending bitstream offset for header in bits */ |
2103 | 14.0k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2104 | 14.0k | ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset |
2105 | 14.0k | - bitstream_start_offset; |
2106 | | /* Starting bitstream offset for residue */ |
2107 | 14.0k | bitstream_start_offset = bitstream_end_offset; |
2108 | | |
2109 | | /* Encoding Residue */ |
2110 | 14.0k | if (mb_type == I16x16) |
2111 | 4.38k | { |
2112 | 4.38k | ps_curr_ctxt->u1_mb_type = CAB_I16x16; |
2113 | 4.38k | ps_curr_ctxt->u1_cbp = (UWORD8) cbp; |
2114 | 4.38k | ih264e_cabac_encode_residue_luma_dc(ps_ent_ctxt); |
2115 | 4.38k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_AC_CTXCAT); |
2116 | 4.38k | } |
2117 | 9.70k | else |
2118 | 9.70k | { |
2119 | 9.70k | ps_curr_ctxt->u1_cbp = (UWORD8) cbp; |
2120 | 9.70k | ps_curr_ctxt->u1_mb_type = I4x4; |
2121 | 9.70k | ps_curr_ctxt->u1_mb_type = CAB_I4x4; |
2122 | 9.70k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); |
2123 | 9.70k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; |
2124 | 9.70k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_yuv_dc_csbp &= 0x6; |
2125 | 9.70k | } |
2126 | | |
2127 | | /* Ending bitstream offset for reside in bits */ |
2128 | 14.0k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2129 | 14.0k | ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset |
2130 | 14.0k | - bitstream_start_offset; |
2131 | 14.0k | } |
2132 | 1.38k | else |
2133 | 1.38k | { |
2134 | 1.38k | ps_curr_ctxt->u1_yuv_ac_csbp = 0; |
2135 | 1.38k | ps_curr_ctxt->u1_yuv_dc_csbp = 0; |
2136 | 1.38k | *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; |
2137 | 1.38k | *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; |
2138 | 1.38k | *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; |
2139 | 1.38k | ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = 0; |
2140 | | /* Ending bitstream offset for header in bits */ |
2141 | 1.38k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2142 | 1.38k | ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset |
2143 | 1.38k | - bitstream_start_offset; |
2144 | 1.38k | } |
2145 | | |
2146 | 15.4k | memset(ps_curr_ctxt->u1_mv, 0, 16); |
2147 | 15.4k | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
2148 | 15.4k | ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_cbp = (UWORD8) cbp; |
2149 | | |
2150 | 15.4k | if (mb_type == I16x16) |
2151 | 4.38k | { |
2152 | 4.38k | ps_curr_ctxt->u1_mb_type = CAB_I16x16; |
2153 | 4.38k | pu1_byte += sizeof(mb_hdr_i16x16_t); |
2154 | 4.38k | } |
2155 | 11.0k | else |
2156 | 11.0k | { |
2157 | 11.0k | ps_curr_ctxt->u1_mb_type = CAB_I4x4; |
2158 | 11.0k | pu1_byte += sizeof(mb_hdr_i4x4_t); |
2159 | 11.0k | } |
2160 | | |
2161 | 15.4k | ps_ent_ctxt->pv_mb_header_data = pu1_byte; |
2162 | | |
2163 | 15.4k | return IH264E_SUCCESS; |
2164 | 15.4k | } |
2165 | 30.2k | else /* Inter MB */ |
2166 | 30.2k | { |
2167 | | /* Starting bitstream offset for header in bits */ |
2168 | 30.2k | bitstream_start_offset = GET_NUM_BITS(ps_bitstream); |
2169 | | /* Encoding B_Direct_16x16 */ |
2170 | 30.2k | if (mb_type == BDIRECT) |
2171 | 6.88k | { |
2172 | 6.88k | cbp = ps_mb_hdr->u1_cbp; |
2173 | 6.88k | mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta; |
2174 | | |
2175 | | |
2176 | | /* Encoding mb_skip */ |
2177 | 6.88k | ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); |
2178 | | |
2179 | | /* Encoding mb_type as B_Direct_16x16 */ |
2180 | 6.88k | { |
2181 | | |
2182 | 6.88k | mb_info_ctxt_t *ps_left_ctxt = |
2183 | 6.88k | ps_cabac_ctxt->ps_left_ctxt_mb_info; |
2184 | 6.88k | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
2185 | 6.88k | UWORD32 u4_ctx_inc = 0; |
2186 | | |
2187 | 6.88k | if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
2188 | 5.67k | u4_ctx_inc += ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) |
2189 | 5.67k | != CAB_BD16x16) ? 1 : 0; |
2190 | 6.88k | if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
2191 | 3.83k | u4_ctx_inc += ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) |
2192 | 3.83k | != CAB_BD16x16) ? 1 : 0; |
2193 | | /* Encode the bin */ |
2194 | 6.88k | ih264e_cabac_encode_bin( |
2195 | 6.88k | ps_cabac_ctxt, |
2196 | 6.88k | 0, |
2197 | 6.88k | ps_cabac_ctxt->au1_cabac_ctxt_table |
2198 | 6.88k | + MB_TYPE_B_SLICE + u4_ctx_inc); |
2199 | | |
2200 | 6.88k | } |
2201 | 6.88k | ps_curr_ctxt->u1_mb_type = CAB_BD16x16; |
2202 | 6.88k | memset(ps_curr_ctxt->u1_mv, 0, 16); |
2203 | 6.88k | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
2204 | | |
2205 | | /* Encode CBP */ |
2206 | 6.88k | ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); |
2207 | | |
2208 | 6.88k | if (cbp) |
2209 | 6.88k | { |
2210 | | /* encode mb_qp_delta */ |
2211 | 6.88k | ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); |
2212 | 6.88k | } |
2213 | | |
2214 | | /* Ending bitstream offset for header in bits */ |
2215 | 6.88k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2216 | 6.88k | ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset |
2217 | 6.88k | - bitstream_start_offset; |
2218 | | /* Starting bitstream offset for residue */ |
2219 | 6.88k | bitstream_start_offset = bitstream_end_offset; |
2220 | | /* Starting bitstream offset for residue */ |
2221 | | |
2222 | 6.88k | pu1_byte += sizeof(mb_hdr_bdirect_t); |
2223 | 6.88k | } |
2224 | 23.3k | else if (mb_type == BSKIP)/* MB = BSKIP */ |
2225 | 1.39k | { |
2226 | 1.39k | ih264e_cabac_enc_mb_skip(1, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); |
2227 | | |
2228 | 1.39k | ps_curr_ctxt->u1_mb_type = CAB_B_SKIP; |
2229 | | |
2230 | 1.39k | memset(ps_curr_ctxt->u1_mv, 0, 16); |
2231 | 1.39k | memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16); |
2232 | 1.39k | cbp = 0; |
2233 | | |
2234 | | /* Ending bitstream offset for header in bits */ |
2235 | 1.39k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2236 | 1.39k | ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset |
2237 | 1.39k | - bitstream_start_offset; |
2238 | | /* Starting bitstream offset for residue */ |
2239 | | |
2240 | 1.39k | pu1_byte += sizeof(mb_hdr_bskip_t); |
2241 | 1.39k | } |
2242 | 21.9k | else /* mbype is B_L0_16x16, B_L1_16x16 or B_Bi_16x16 */ |
2243 | 21.9k | { |
2244 | 21.9k | mb_hdr_b16x16_t *ps_mb_hdr_b16x16 = (mb_hdr_b16x16_t *)ps_ent_ctxt->pv_mb_header_data; |
2245 | | |
2246 | 21.9k | WORD32 i4_mb_part_pred_mode = (mb_tpm >> 4); |
2247 | 21.9k | UWORD32 u4_mb_type = mb_type - B16x16 + B_L0_16x16 |
2248 | 21.9k | + i4_mb_part_pred_mode; |
2249 | 21.9k | cbp = ps_mb_hdr->u1_cbp; |
2250 | 21.9k | mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta; |
2251 | | |
2252 | | /* Encoding mb_skip */ |
2253 | 21.9k | ih264e_cabac_enc_mb_skip(0, ps_cabac_ctxt, MB_SKIP_FLAG_B_SLICE); |
2254 | | |
2255 | | /* Encoding mb_type as B16x16 */ |
2256 | 21.9k | { |
2257 | 21.9k | mb_info_ctxt_t *ps_left_ctxt = |
2258 | 21.9k | ps_cabac_ctxt->ps_left_ctxt_mb_info; |
2259 | 21.9k | mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info; |
2260 | 21.9k | UWORD32 u4_ctx_inc = 0; |
2261 | | |
2262 | 21.9k | UWORD32 u4_mb_type_bins = u4_b_mb_type[u4_mb_type]; |
2263 | 21.9k | UWORD32 u4_bin_len = (u4_mb_type_bins >> 8) & 0x0F; |
2264 | 21.9k | u4_mb_type_bins = u4_mb_type_bins & 0xFF; |
2265 | | |
2266 | 21.9k | if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
2267 | 13.3k | u4_ctx_inc += ((ps_left_ctxt->u1_mb_type & CAB_BD16x16_MASK) |
2268 | 13.3k | != CAB_BD16x16) ? 1 : 0; |
2269 | 21.9k | if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info) |
2270 | 13.7k | u4_ctx_inc += ((ps_top_ctxt->u1_mb_type & CAB_BD16x16_MASK) |
2271 | 13.7k | != CAB_BD16x16) ? 1 : 0; |
2272 | | |
2273 | 21.9k | u4_ctx_inc = u4_ctx_inc | ui_b_mb_type_ctx_inc[u4_mb_type]; |
2274 | | |
2275 | 21.9k | ih264e_encode_decision_bins(u4_mb_type_bins, |
2276 | 21.9k | u4_bin_len, |
2277 | 21.9k | u4_ctx_inc, |
2278 | 21.9k | u4_bin_len, |
2279 | 21.9k | &(ps_cabac_ctxt->au1_cabac_ctxt_table[MB_TYPE_B_SLICE]), |
2280 | 21.9k | ps_cabac_ctxt); |
2281 | 21.9k | } |
2282 | | |
2283 | 21.9k | ps_curr_ctxt->u1_mb_type = CAB_NON_BD16x16; |
2284 | 21.9k | { |
2285 | 21.9k | WORD16 *pi2_mv_ptr = (WORD16 *) ps_mb_hdr_b16x16->ai2_mv; |
2286 | | |
2287 | | /* Get the pred modes */ |
2288 | 21.9k | ps_curr_ctxt->u1_mb_type = (ps_curr_ctxt->u1_mb_type |
2289 | 21.9k | | CAB_NON_BD16x16); |
2290 | | /* Encoding motion vector for B16x16 */ |
2291 | 21.9k | ih264e_cabac_enc_mvds_b16x16(ps_cabac_ctxt, pi2_mv_ptr, |
2292 | 21.9k | i4_mb_part_pred_mode); |
2293 | 21.9k | } |
2294 | | /* Encode CBP */ |
2295 | 21.9k | ih264e_cabac_enc_cbp(cbp, ps_cabac_ctxt); |
2296 | | |
2297 | 21.9k | if (cbp) |
2298 | 20.6k | { |
2299 | | /* encode mb_qp_delta */ |
2300 | 20.6k | ih264e_cabac_enc_mb_qp_delta(mb_qp_delta, ps_cabac_ctxt); |
2301 | 20.6k | } |
2302 | | |
2303 | | /* Ending bitstream offset for header in bits */ |
2304 | 21.9k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2305 | 21.9k | ps_ent_ctxt->u4_header_bits[1] += bitstream_end_offset |
2306 | 21.9k | - bitstream_start_offset; |
2307 | | /* Starting bitstream offset for residue */ |
2308 | 21.9k | bitstream_start_offset = bitstream_end_offset; |
2309 | | |
2310 | 21.9k | pu1_byte += sizeof(mb_hdr_b16x16_t); |
2311 | 21.9k | } |
2312 | | |
2313 | 30.2k | if (cbp > 0) |
2314 | 27.5k | { |
2315 | | /* Encode residue */ |
2316 | 27.5k | ih264e_cabac_encode_residue(ps_ent_ctxt, cbp, LUMA_4X4_CTXCAT); |
2317 | | /* Ending bitstream offset for reside in bits */ |
2318 | 27.5k | bitstream_end_offset = GET_NUM_BITS(ps_bitstream); |
2319 | 27.5k | ps_ent_ctxt->u4_residue_bits[1] += bitstream_end_offset |
2320 | 27.5k | - bitstream_start_offset; |
2321 | | |
2322 | 27.5k | ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6; |
2323 | 27.5k | ps_curr_ctxt->u1_yuv_dc_csbp &= 0x6; |
2324 | 27.5k | } |
2325 | 2.70k | else |
2326 | 2.70k | { |
2327 | 2.70k | ps_curr_ctxt->u1_yuv_ac_csbp = 0; |
2328 | 2.70k | ps_curr_ctxt->u1_yuv_dc_csbp = 0; |
2329 | 2.70k | *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = 0; |
2330 | 2.70k | *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = 0; |
2331 | 2.70k | *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = 0; |
2332 | 2.70k | ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = 0; |
2333 | 2.70k | } |
2334 | 30.2k | ps_curr_ctxt->u1_intrapred_chroma_mode = 0; |
2335 | 30.2k | ps_curr_ctxt->u1_cbp = cbp; |
2336 | 30.2k | ps_ent_ctxt->pv_mb_header_data = pu1_byte; |
2337 | 30.2k | return IH264E_SUCCESS; |
2338 | 30.2k | } |
2339 | 45.7k | } |