/src/libhevc/decoder/ihevcd_cabac.c
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore |
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 | | /** |
19 | | ****************************************************************************** |
20 | | * @file ihevcd_cabac.c |
21 | | * |
22 | | * @brief |
23 | | * This file contains function definitions related to CABAC parsing |
24 | | * |
25 | | * @author |
26 | | * Ittiam |
27 | | * |
28 | | * |
29 | | * List of Functions |
30 | | * |
31 | | * ihevcd_cabac_init() |
32 | | * ihevcd_cabac_decode_bin() |
33 | | * ihevcd_cabac_decode_bypass_bin() |
34 | | * ihevcd_cabac_decode_bypass_bins_tunary() |
35 | | * ihevcd_cabac_decode_terminate() |
36 | | * ihevcd_cabac_decode_bin_tunary() |
37 | | * ihevcd_cabac_decode_bypass_bins() |
38 | | * ihevcd_cabac_decode_bypass_bins_egk() |
39 | | * ihevcd_cabac_decode_trunc_rice() |
40 | | * ihevcd_cabac_flush() |
41 | | * |
42 | | ****************************************************************************** |
43 | | */ |
44 | | |
45 | | /*****************************************************************************/ |
46 | | /* File Includes */ |
47 | | /*****************************************************************************/ |
48 | | #include <stdio.h> |
49 | | #include <stddef.h> |
50 | | #include <assert.h> |
51 | | #include <stdlib.h> |
52 | | #include <string.h> |
53 | | |
54 | | #include "ihevc_typedefs.h" |
55 | | #include "iv.h" |
56 | | #include "ivd.h" |
57 | | #include "ihevcd_cxa.h" |
58 | | |
59 | | |
60 | | #include "ihevc_debug.h" |
61 | | #include "ihevc_macros.h" |
62 | | #include "ihevc_platform_macros.h" |
63 | | #include "ihevc_cabac_tables.h" |
64 | | #include "ihevc_defs.h" |
65 | | #include "ihevc_structs.h" |
66 | | #include "ihevc_cabac_tables.h" |
67 | | |
68 | | |
69 | | #include "ihevcd_defs.h" |
70 | | #include "ihevcd_function_selector.h" |
71 | | #include "ihevcd_structs.h" |
72 | | #include "ihevcd_error.h" |
73 | | #include "ihevcd_bitstream.h" |
74 | | #include "ihevcd_cabac.h" |
75 | | #include "ihevcd_trace.h" |
76 | | |
77 | | #ifdef TRACE |
78 | | extern trace_t g_trace; |
79 | | #endif |
80 | | #if DEBUG_CABAC_RANGE_OFST |
81 | | #if FULLRANGE |
82 | | #define DEBUG_RANGE_OFST(str, m_range, m_ofst ) \ |
83 | | {\ |
84 | | UWORD32 m_clz, m_range_shift, m_ofst_shift; \ |
85 | | m_clz = CLZ(m_range); \ |
86 | | m_clz -= (32 - RANGE_NUMBITS); \ |
87 | | m_range_shift = m_range << m_clz; \ |
88 | | m_range_shift = m_range_shift >> RANGE_SHIFT; \ |
89 | | m_ofst_shift = m_ofst << m_clz; \ |
90 | | m_ofst_shift = m_ofst_shift >> RANGE_SHIFT; \ |
91 | | fprintf( g_trace.fp, "%-40s R: %3d O: %3d\n", str, m_range_shift, m_ofst_shift); \ |
92 | | } |
93 | | |
94 | | #else |
95 | | #define DEBUG_RANGE_OFST(str, m_range, m_ofst) \ |
96 | | fprintf( g_trace.fp, "%-40s R: %3d O: %3d\n", str, m_range, m_ofst); |
97 | | #endif |
98 | | #else |
99 | | #define DEBUG_RANGE_OFST(str, m_range, m_ofst ) |
100 | | #endif |
101 | | /*****************************************************************************/ |
102 | | /* Function Definitions */ |
103 | | /*****************************************************************************/ |
104 | | |
105 | | /** |
106 | | ****************************************************************************** |
107 | | * |
108 | | * @brief Initializes the decoder cabac engine |
109 | | * |
110 | | * @par Description |
111 | | * This routine needs to be called at start of slice/frame decode |
112 | | * |
113 | | * @param[in,out] ps_cabac_ctxt |
114 | | * pointer to cabac context (handle) |
115 | | * |
116 | | * @param[in] ps_bitstrm |
117 | | * pointer to bitstream context (handle) |
118 | | * |
119 | | * @param[in] qp |
120 | | * current slice Qp |
121 | | * |
122 | | * @param[in] cabac_init_idc |
123 | | * current slice init idc (range [0 - 2]) |
124 | | * |
125 | | * @param[in] pu1_init_ctxt |
126 | | * Init cabac context to be used (range [0 - 2]) |
127 | | * |
128 | | * @return success or failure error code |
129 | | * |
130 | | ****************************************************************************** |
131 | | */ |
132 | | IHEVCD_ERROR_T ihevcd_cabac_init(cab_ctxt_t *ps_cabac, |
133 | | bitstrm_t *ps_bitstrm, |
134 | | WORD32 qp, |
135 | | WORD32 cabac_init_idc, |
136 | | const UWORD8 *pu1_init_ctxt) |
137 | 9.82k | { |
138 | | /* Sanity checks */ |
139 | 9.82k | ASSERT(ps_cabac != NULL); |
140 | 9.82k | ASSERT(ps_bitstrm != NULL); |
141 | 9.82k | ASSERT((qp >= 0) && (qp < 52)); |
142 | 9.82k | ASSERT((cabac_init_idc >= 0) && (cabac_init_idc < 3)); |
143 | 9.82k | UNUSED(qp); |
144 | 9.82k | UNUSED(cabac_init_idc); |
145 | | /* CABAC engine uses 32 bit range instead of 9 bits as specified by |
146 | | * the spec. This is done to reduce number of renormalizations |
147 | | */ |
148 | | /* cabac engine initialization */ |
149 | 9.82k | #if FULLRANGE |
150 | 9.82k | ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT; |
151 | 9.82k | BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
152 | 9.82k | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT)); |
153 | | |
154 | | #else |
155 | | ps_cabac->u4_range = (UWORD32)510; |
156 | | BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
157 | | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9); |
158 | | |
159 | | #endif |
160 | | |
161 | | /* cabac context initialization based on init idc and slice qp */ |
162 | 9.82k | memcpy(ps_cabac->au1_ctxt_models, |
163 | 9.82k | pu1_init_ctxt, |
164 | 9.82k | IHEVC_CAB_CTXT_END); |
165 | 9.82k | DEBUG_RANGE_OFST("init", ps_cabac->u4_range, ps_cabac->u4_ofst); |
166 | | |
167 | | /* |
168 | | * If the offset is greater than or equal to range, return fail. |
169 | | */ |
170 | 9.82k | if(ps_cabac->u4_ofst >= ps_cabac->u4_range) |
171 | 72 | { |
172 | 72 | return ((IHEVCD_ERROR_T)IHEVCD_FAIL); |
173 | 72 | } |
174 | | |
175 | 9.75k | return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS); |
176 | 9.82k | } |
177 | | |
178 | | IHEVCD_ERROR_T ihevcd_cabac_reset(cab_ctxt_t *ps_cabac, |
179 | | bitstrm_t *ps_bitstrm) |
180 | 428 | { |
181 | | /* Sanity checks */ |
182 | 428 | ASSERT(ps_cabac != NULL); |
183 | 428 | ASSERT(ps_bitstrm != NULL); |
184 | | |
185 | | /* CABAC engine uses 32 bit range instead of 9 bits as specified by |
186 | | * the spec. This is done to reduce number of renormalizations |
187 | | */ |
188 | | /* cabac engine initialization */ |
189 | 428 | #if FULLRANGE |
190 | 428 | ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT; |
191 | 428 | BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
192 | 428 | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT)); |
193 | | |
194 | | #else |
195 | | ps_cabac->u4_range = (UWORD32)510; |
196 | | BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
197 | | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9); |
198 | | |
199 | | #endif |
200 | | |
201 | 428 | return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS); |
202 | 428 | } |
203 | | |
204 | | /** |
205 | | ****************************************************************************** |
206 | | * |
207 | | * @brief Decodes a bin based on probablilty and mps packed context model |
208 | | * |
209 | | * @par Description |
210 | | * Decodes a bin as per Section : 9.3.3.2.1 and calls renormalization if required |
211 | | * as per section 9.3.3.2.2 |
212 | | * 1. Apart from decoding bin, context model is updated as per state transition |
213 | | * 2. Range and Low renormalization is done based on bin and original state |
214 | | * 3. After renorm bistream is updated (if required) |
215 | | * |
216 | | * @param[in,out] ps_cabac |
217 | | * pointer to cabac context (handle) |
218 | | * |
219 | | * @param[in] ctxt_index |
220 | | * index of cabac context model containing pState[bits6-1] | MPS[bit0] |
221 | | * |
222 | | * @param[in] ps_bitstrm |
223 | | * Bitstream context |
224 | | * |
225 | | * @return bin(boolean) to be decoded |
226 | | * |
227 | | ****************************************************************************** |
228 | | */ |
229 | | UWORD32 ihevcd_cabac_decode_bin(cab_ctxt_t *ps_cabac, |
230 | | bitstrm_t *ps_bitstrm, |
231 | | WORD32 ctxt_index |
232 | | |
233 | | ) |
234 | 47.6M | { |
235 | 47.6M | UWORD32 u4_range = ps_cabac->u4_range; |
236 | 47.6M | UWORD32 u4_ofst = ps_cabac->u4_ofst; |
237 | 47.6M | UWORD32 u4_rlps; |
238 | 47.6M | UWORD32 u4_bin; |
239 | 47.6M | UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[ctxt_index]; |
240 | 47.6M | WORD32 state_mps = *pu1_ctxt_model; |
241 | 47.6M | #if FULLRANGE |
242 | 47.6M | WORD32 clz; |
243 | 47.6M | #endif |
244 | 47.6M | UWORD32 u4_qnt_range; |
245 | | |
246 | | /* Sanity checks */ |
247 | 47.6M | ASSERT(u4_range >= 256); |
248 | 47.6M | ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END)); |
249 | 47.6M | ASSERT(state_mps < 128); |
250 | 47.6M | #if FULLRANGE |
251 | 47.6M | clz = CLZ(u4_range); |
252 | 47.6M | clz -= (32 - RANGE_NUMBITS); |
253 | 47.6M | u4_qnt_range = u4_range << clz; |
254 | 47.6M | u4_qnt_range = (u4_qnt_range >> (RANGE_SHIFT + 6)) & 0x3; |
255 | | #else |
256 | | u4_qnt_range = (u4_range >> 6) & 0x3; |
257 | | #endif |
258 | | /* Get the lps range from LUT based on quantized range and state */ |
259 | 47.6M | u4_rlps = gau1_ihevc_cabac_rlps[state_mps >> 1][u4_qnt_range]; |
260 | 47.6M | #if FULLRANGE |
261 | 47.6M | u4_rlps = u4_rlps << (RANGE_SHIFT - clz); |
262 | 47.6M | #endif |
263 | 47.6M | u4_range -= u4_rlps; |
264 | | |
265 | 47.6M | u4_bin = state_mps & 1; |
266 | | |
267 | 47.6M | if(u4_ofst >= u4_range) |
268 | 5.32M | { |
269 | 5.32M | u4_bin = 1 - u4_bin; |
270 | 5.32M | u4_ofst -= u4_range; |
271 | 5.32M | u4_range = u4_rlps; |
272 | 5.32M | } |
273 | | |
274 | 47.6M | *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | u4_bin]; |
275 | | |
276 | | /*****************************************************************/ |
277 | | /* Re-normalization; calculate bits generated based on range(R) */ |
278 | | /*****************************************************************/ |
279 | 47.6M | if(u4_range < (1 << 8)) |
280 | 536k | { |
281 | 536k | UWORD32 u4_bits; |
282 | 536k | WORD32 numbits; |
283 | 536k | numbits = CLZ(u4_range); |
284 | 536k | numbits -= (32 - RANGE_NUMBITS); |
285 | | #if !FULLRANGE |
286 | | numbits -= RANGE_SHIFT; |
287 | | #endif |
288 | 536k | BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
289 | 536k | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits); |
290 | | |
291 | 536k | u4_ofst <<= numbits; |
292 | 536k | u4_ofst |= u4_bits; |
293 | 536k | u4_range <<= numbits; |
294 | | |
295 | 536k | } |
296 | | /* Update the cabac context */ |
297 | 47.6M | ps_cabac->u4_range = u4_range; |
298 | 47.6M | ps_cabac->u4_ofst = u4_ofst; |
299 | 47.6M | DEBUG_RANGE_OFST("bin", ps_cabac->u4_range, ps_cabac->u4_ofst); |
300 | | |
301 | 47.6M | return (u4_bin); |
302 | | |
303 | | |
304 | 47.6M | } |
305 | | |
306 | | /** |
307 | | ****************************************************************************** |
308 | | * |
309 | | * @brief Decodes a bypass bin (equi-probable 0 / 1) |
310 | | * |
311 | | * @par Description |
312 | | * Decodes a bypss bin as per Section : 9.3.3.2.3 |
313 | | * |
314 | | * @param[in,out] ps_cabac |
315 | | * pointer to cabac context (handle) |
316 | | * |
317 | | * @param[in] ps_bitstrm |
318 | | * Bitstream context |
319 | | * |
320 | | * @return Decoded bypass bin |
321 | | * |
322 | | ****************************************************************************** |
323 | | */ |
324 | | UWORD32 ihevcd_cabac_decode_bypass_bin(cab_ctxt_t *ps_cabac, |
325 | | bitstrm_t *ps_bitstrm) |
326 | 1.31M | { |
327 | | |
328 | 1.31M | UWORD32 u4_bin; |
329 | 1.31M | UWORD32 u4_range = ps_cabac->u4_range; |
330 | 1.31M | UWORD32 u4_ofst = ps_cabac->u4_ofst; |
331 | 1.31M | UWORD32 u4_bits; |
332 | | |
333 | | /* Sanity checks */ |
334 | 1.31M | ASSERT(u4_range >= 256); |
335 | | |
336 | 1.31M | BIT_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
337 | 1.31M | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word); |
338 | | |
339 | 1.31M | u4_ofst <<= 1; |
340 | 1.31M | u4_ofst |= u4_bits; |
341 | | |
342 | 1.31M | u4_bin = 0; |
343 | 1.31M | if(u4_ofst >= u4_range) |
344 | 652k | { |
345 | 652k | u4_bin = 1; |
346 | 652k | u4_ofst -= u4_range; |
347 | 652k | } |
348 | | |
349 | | /* Update the cabac context */ |
350 | 1.31M | ps_cabac->u4_ofst = u4_ofst; |
351 | 1.31M | DEBUG_RANGE_OFST("bypass end", ps_cabac->u4_range, ps_cabac->u4_ofst); |
352 | 1.31M | return (u4_bin); |
353 | 1.31M | } |
354 | | |
355 | | /** |
356 | | ****************************************************************************** |
357 | | * |
358 | | * @brief Decodes a terminate bin (1:terminate 0:do not terminate) |
359 | | * |
360 | | * @par Description |
361 | | * Decodes a terminate bin to be called for end_of_slice_flag and pcm_flag |
362 | | * as per Section : 9.3.3.2.4 |
363 | | * |
364 | | * @param[in,out] ps_cabac |
365 | | * pointer to cabac context (handle) |
366 | | * |
367 | | * @param[in] ps_bitstrm |
368 | | * Bitstream context |
369 | | * |
370 | | * @return Decoded Bin to indicate whether to terminate or not |
371 | | * |
372 | | ****************************************************************************** |
373 | | */ |
374 | | UWORD32 ihevcd_cabac_decode_terminate(cab_ctxt_t *ps_cabac, |
375 | | bitstrm_t *ps_bitstrm) |
376 | 1.01M | { |
377 | 1.01M | UWORD32 u4_range = ps_cabac->u4_range; |
378 | 1.01M | UWORD32 u4_ofst = ps_cabac->u4_ofst; |
379 | 1.01M | UWORD32 u4_bin; |
380 | 1.01M | #if FULLRANGE |
381 | 1.01M | WORD32 clz; |
382 | 1.01M | #endif |
383 | | /* Sanity checks */ |
384 | 1.01M | ASSERT(u4_range >= 256); |
385 | 1.01M | #if FULLRANGE |
386 | 1.01M | clz = CLZ(u4_range); |
387 | 1.01M | clz -= (32 - RANGE_NUMBITS); |
388 | 1.01M | u4_range -= 2 << (RANGE_SHIFT - clz); |
389 | | #else |
390 | | u4_range -= 2; |
391 | | #endif |
392 | | |
393 | 1.01M | if(u4_ofst >= u4_range) |
394 | 1.43k | { |
395 | 1.43k | u4_bin = 1; |
396 | | |
397 | 1.43k | #if FULLRANGE |
398 | | /* In case of FULLRANGE extra bits read earlier need to pushed back to the bitstream */ |
399 | 1.43k | { |
400 | 1.43k | WORD32 clz; |
401 | 1.43k | WORD32 numbits; |
402 | 1.43k | clz = CLZ(ps_cabac->u4_range); |
403 | | |
404 | 1.43k | numbits = (32 - clz); |
405 | 1.43k | numbits -= 9; |
406 | | |
407 | 1.43k | ihevcd_bits_seek(ps_bitstrm, -numbits); |
408 | 1.43k | } |
409 | 1.43k | #endif |
410 | | |
411 | 1.43k | } |
412 | 1.01M | else |
413 | 1.01M | { |
414 | 1.01M | u4_bin = 0; |
415 | 1.01M | } |
416 | 1.01M | if(0 == u4_bin) |
417 | 1.01M | { |
418 | 1.01M | UWORD32 u4_bits; |
419 | 1.01M | WORD32 numbits; |
420 | 1.01M | numbits = CLZ(u4_range); |
421 | 1.01M | numbits -= (32 - RANGE_NUMBITS); |
422 | | #if !FULLRANGE |
423 | | numbits -= RANGE_SHIFT; |
424 | | #endif |
425 | | /* Renormalize if required */ |
426 | 1.01M | if(numbits) |
427 | 968k | { |
428 | 968k | BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
429 | 968k | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits); |
430 | | |
431 | 968k | u4_ofst <<= numbits; |
432 | 968k | u4_ofst |= u4_bits; |
433 | 968k | u4_range <<= numbits; |
434 | 968k | } |
435 | 1.01M | } |
436 | | /* bits to be inserted in the bitstream */ |
437 | 1.01M | ps_cabac->u4_range = u4_range; |
438 | 1.01M | ps_cabac->u4_ofst = u4_ofst; |
439 | 1.01M | DEBUG_RANGE_OFST("term", ps_cabac->u4_range, ps_cabac->u4_ofst); |
440 | | |
441 | 1.01M | return (u4_bin); |
442 | 1.01M | } |
443 | | |
444 | | /** |
445 | | ****************************************************************************** |
446 | | * |
447 | | * @brief Decodes a bypass bin (equi-probable 0 / 1) |
448 | | * |
449 | | * @par Description |
450 | | * Decodes a bypss bin as per Section : 9.3.3.2.3 |
451 | | * |
452 | | * @param[in,out] ps_cabac |
453 | | * pointer to cabac context (handle) |
454 | | * |
455 | | * @param[in] ps_bitstrm |
456 | | * Bitstream context |
457 | | * |
458 | | * @param[in] numbins |
459 | | * Number of bins to decoded |
460 | | * |
461 | | * @return Decoded bypass bin |
462 | | * |
463 | | * @remarks Tested only for numbins less than 17 |
464 | | * |
465 | | ****************************************************************************** |
466 | | */ |
467 | | |
468 | | UWORD32 ihevcd_cabac_decode_bypass_bins(cab_ctxt_t *ps_cabac, |
469 | | bitstrm_t *ps_bitstrm, |
470 | | WORD32 numbins) |
471 | 6.72M | { |
472 | 6.72M | UWORD32 u4_bins; |
473 | | |
474 | | |
475 | 6.72M | UWORD32 u4_range = ps_cabac->u4_range; |
476 | 6.72M | UWORD32 u4_ofst = ps_cabac->u4_ofst; |
477 | 6.72M | UWORD32 u4_bits; |
478 | 6.72M | ASSERT(u4_range >= 256); |
479 | 6.72M | ASSERT(numbins > 0); |
480 | | |
481 | | /* Sanity checks */ |
482 | 6.72M | ASSERT(numbins < 17); |
483 | | |
484 | 6.72M | u4_bins = 0; |
485 | | |
486 | 6.72M | BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
487 | 6.72M | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbins); |
488 | | |
489 | 6.72M | do |
490 | 18.0M | { |
491 | 18.0M | UWORD32 u4_bit; |
492 | 18.0M | numbins--; |
493 | 18.0M | u4_bit = (u4_bits >> numbins) & 1; |
494 | 18.0M | u4_ofst <<= 1; |
495 | 18.0M | u4_ofst |= u4_bit; |
496 | | |
497 | 18.0M | u4_bins <<= 1; |
498 | 18.0M | if(u4_ofst >= u4_range) |
499 | 6.14M | { |
500 | 6.14M | u4_bins += 1; |
501 | 6.14M | u4_ofst -= u4_range; |
502 | 6.14M | } |
503 | 18.0M | }while(numbins); |
504 | | |
505 | | /* Update the cabac context */ |
506 | 6.72M | ps_cabac->u4_ofst = u4_ofst; |
507 | 6.72M | DEBUG_RANGE_OFST("bypass", ps_cabac->u4_range, ps_cabac->u4_ofst); |
508 | 6.72M | return (u4_bins); |
509 | 6.72M | } |
510 | | |
511 | | /** |
512 | | ****************************************************************************** |
513 | | * |
514 | | * @brief Decodes a truncated unary symbol associated with context model(s) |
515 | | * |
516 | | * @par Description |
517 | | * Decodes symbols coded with TUnary binarization as per sec 9.3.2.2 |
518 | | * This is used for computing symbols like qp_delta, |
519 | | * last_sig_coeff_prefix_x, last_sig_coeff_prefix_y. |
520 | | * |
521 | | * The context models associated with each bin is computed as : |
522 | | * current bin context = "base context idx" + (bin_idx >> shift) |
523 | | * where |
524 | | * 1. "base context idx" is the base index for the syntax element |
525 | | * 2. "bin_idx" is the current bin index of the unary code |
526 | | * 3. "shift" is the shift factor associated with this syntax element |
527 | | * |
528 | | * @param[in,out] ps_cabac |
529 | | * pointer to cabac context (handle) |
530 | | * |
531 | | * @param[in] ps_bitstrm |
532 | | * Bitstream context |
533 | | * |
534 | | * @param[in] c_max |
535 | | * maximum value of sym (required for tunary binarization) |
536 | | * |
537 | | * @param[in] ctxt_index |
538 | | * base context model index for this syntax element |
539 | | * |
540 | | * @param[in] ctxt_shift |
541 | | * shift factor for context increments associated with this syntax element |
542 | | * |
543 | | * @param[in] ctxt_inc_max |
544 | | * max value of context increment beyond which all bins will use same ctxt |
545 | | * |
546 | | * @return syntax element decoded |
547 | | * |
548 | | ****************************************************************************** |
549 | | */ |
550 | | UWORD32 ihevcd_cabac_decode_bins_tunary(cab_ctxt_t *ps_cabac, |
551 | | bitstrm_t *ps_bitstrm, |
552 | | WORD32 c_max, |
553 | | WORD32 ctxt_index, |
554 | | WORD32 ctxt_shift, |
555 | | WORD32 ctxt_inc_max) |
556 | 15.9M | { |
557 | 15.9M | UWORD32 u4_sym; |
558 | 15.9M | WORD32 bin; |
559 | | |
560 | | /* Sanity checks */ |
561 | 15.9M | ASSERT(c_max > 0); |
562 | 15.9M | ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END)); |
563 | 15.9M | ASSERT((ctxt_index + (c_max >> ctxt_shift)) < IHEVC_CAB_CTXT_END); |
564 | | |
565 | 15.9M | u4_sym = 0; |
566 | 15.9M | do |
567 | 40.9M | { |
568 | 40.9M | WORD32 bin_index; |
569 | 40.9M | bin_index = ctxt_index + MIN((u4_sym >> ctxt_shift), (UWORD32)ctxt_inc_max); |
570 | 163M | IHEVCD_CABAC_DECODE_BIN(bin, ps_cabac, ps_bitstrm, bin_index); |
571 | 163M | u4_sym++; |
572 | 163M | }while(((WORD32)u4_sym < c_max) && bin); |
573 | | |
574 | 15.9M | u4_sym = u4_sym - 1 + bin; |
575 | | |
576 | 15.9M | return (u4_sym); |
577 | 15.9M | } |
578 | | |
579 | | /** |
580 | | ****************************************************************************** |
581 | | * |
582 | | * @brief Decodes a syntax element as truncated unary bypass bins |
583 | | * |
584 | | * @par Description |
585 | | * Decodes symbols coded with TUnary binarization as per sec 9.3.2.2 |
586 | | * These symbols are coded as bypass bins |
587 | | * This is used for computing symbols like merge_idx, |
588 | | * mpm_idx etc |
589 | | * |
590 | | * @param[in,out]ps_cabac |
591 | | * pointer to cabac context (handle) |
592 | | * |
593 | | * @param[in] ps_bitstrm |
594 | | * Bitstream context |
595 | | * |
596 | | * @param[in] c_max |
597 | | * maximum value of sym (required for tunary binarization) |
598 | | * |
599 | | * @return syntax element decoded |
600 | | * |
601 | | ****************************************************************************** |
602 | | */ |
603 | | UWORD32 ihevcd_cabac_decode_bypass_bins_tunary(cab_ctxt_t *ps_cabac, |
604 | | bitstrm_t *ps_bitstrm, |
605 | | WORD32 c_max) |
606 | 7.26M | { |
607 | | |
608 | 7.26M | UWORD32 u4_sym; |
609 | 7.26M | WORD32 bin; |
610 | 7.26M | UWORD32 u4_ofst = ps_cabac->u4_ofst; |
611 | 7.26M | UWORD32 u4_range = ps_cabac->u4_range; |
612 | 7.26M | UWORD32 u4_bits; |
613 | | /* Sanity checks */ |
614 | 7.26M | ASSERT(c_max > 0); |
615 | 7.26M | ASSERT(u4_range >= 256); |
616 | 7.26M | u4_sym = 0; |
617 | 7.26M | BITS_NXT(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
618 | 7.26M | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (UWORD32)c_max); |
619 | 7.26M | u4_bits <<= (32 - c_max); |
620 | 7.26M | do |
621 | 11.1M | { |
622 | 11.1M | u4_ofst <<= 1; |
623 | 11.1M | u4_ofst |= (u4_bits >> 31); |
624 | 11.1M | u4_bits <<= 1; |
625 | | |
626 | 11.1M | bin = 0; |
627 | 11.1M | if(u4_ofst >= u4_range) |
628 | 4.59M | { |
629 | 4.59M | bin = 1; |
630 | 4.59M | u4_ofst -= u4_range; |
631 | 4.59M | } |
632 | 11.1M | u4_sym++; |
633 | 11.1M | }while(((WORD32)u4_sym < c_max) && bin); |
634 | 7.26M | BITS_FLUSH(ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst, |
635 | 7.26M | ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, u4_sym); |
636 | | |
637 | 7.26M | u4_sym = u4_sym - 1 + bin; |
638 | | /* Update the cabac context */ |
639 | 7.26M | ps_cabac->u4_ofst = u4_ofst; |
640 | | |
641 | 7.26M | return (u4_sym); |
642 | 7.26M | } |
643 | | |
644 | | /** |
645 | | ****************************************************************************** |
646 | | * |
647 | | * @brief Decodes a syntax element as kth order Exp-Golomb code (EGK) |
648 | | * |
649 | | * @par Description |
650 | | * Decodes a syntax element binarized as kth order Exp-Golomb code (EGK) |
651 | | * Elements are coded as bypass bins |
652 | | * |
653 | | * @param[in,out] ps_cabac |
654 | | * pointer to cabac context (handle) |
655 | | * |
656 | | * @param[in] u4_sym |
657 | | * syntax element to be coded as EGK |
658 | | * |
659 | | * @param[in] k |
660 | | * order of EGk |
661 | | * |
662 | | * @return success or failure error code |
663 | | * |
664 | | ****************************************************************************** |
665 | | */ |
666 | | UWORD32 ihevcd_cabac_decode_bypass_bins_egk(cab_ctxt_t *ps_cabac, |
667 | | bitstrm_t *ps_bitstrm, |
668 | | WORD32 k) |
669 | 78.5k | { |
670 | | |
671 | 78.5k | UWORD32 u4_sym; |
672 | 78.5k | WORD32 numones; |
673 | 78.5k | WORD32 bin; |
674 | | |
675 | | /* Sanity checks */ |
676 | 78.5k | ASSERT((k >= 0)); |
677 | | |
678 | 78.5k | numones = k; |
679 | 78.5k | bin = 1; |
680 | 78.5k | u4_sym = 0; |
681 | 212k | while(bin && (numones <= 16)) |
682 | 133k | { |
683 | 267k | IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm); |
684 | 267k | u4_sym += bin << numones++; |
685 | 267k | } |
686 | | |
687 | 78.5k | numones -= 1; |
688 | | |
689 | 78.5k | if(numones) |
690 | 69.6k | { |
691 | 69.6k | UWORD32 u4_suffix; |
692 | | |
693 | 278k | IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, numones); |
694 | 278k | u4_sym += u4_suffix; |
695 | 278k | } |
696 | 78.5k | return (u4_sym); |
697 | 78.5k | } |
698 | | |
699 | | /** |
700 | | ****************************************************************************** |
701 | | * |
702 | | * @brief Decodes a syntax element as truncated rice code (TR) |
703 | | * |
704 | | * @par Description |
705 | | * Decodes a syntax element as truncated rice code (TR) |
706 | | * Elements are coded as bypass bins |
707 | | * This function ise used for coeff_abs_level_remaining coding when |
708 | | * level is less than c_rice_max |
709 | | * |
710 | | * @param[in,out] ps_cabac |
711 | | * pointer to cabac context (handle) |
712 | | * |
713 | | * @param[in] u4_sym |
714 | | * syntax element to be coded as truncated rice code |
715 | | * |
716 | | * @param[in] c_rice_param |
717 | | * shift factor for truncated unary prefix coding of (u4_sym >> c_rice_param) |
718 | | * |
719 | | * @param[in] c_rice_max |
720 | | * max symbol val below which a suffix is coded as (u4_sym%(1<<c_rice_param)) |
721 | | * This is currently (4 << c_rice_param) for coeff_abs_level_remaining |
722 | | * |
723 | | * @return success or failure error code |
724 | | * |
725 | | ****************************************************************************** |
726 | | */ |
727 | | UWORD32 ihevcd_cabac_decode_bypass_bins_trunc_rice(cab_ctxt_t *ps_cabac, |
728 | | bitstrm_t *ps_bitstrm, |
729 | | WORD32 c_rice_param, |
730 | | WORD32 c_rice_max) |
731 | 0 | { |
732 | 0 | UWORD32 u4_sym; |
733 | 0 | WORD32 bin; |
734 | 0 | WORD32 c_max; |
735 | 0 | UWORD32 u4_suffix; |
736 | | /* Sanity checks */ |
737 | 0 | ASSERT((c_rice_param >= 0)); |
738 | | |
739 | | |
740 | | /* Decode prefix coded as TUnary */ |
741 | 0 | c_max = c_rice_max >> c_rice_param; |
742 | 0 | u4_sym = 0; |
743 | 0 | do |
744 | 0 | { |
745 | 0 | IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm); |
746 | 0 | u4_sym++; |
747 | |
|
748 | 0 | }while(((WORD32)u4_sym < c_max) && bin); |
749 | 0 | u4_sym = u4_sym - 1 + bin; |
750 | | |
751 | | /* If suffix is present, then decode c_rice_param number of bins */ |
752 | 0 | if(c_rice_param) |
753 | 0 | { |
754 | 0 | IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, c_rice_param); |
755 | |
|
756 | 0 | u4_sym = (u4_sym << c_rice_param) | u4_suffix; |
757 | 0 | } |
758 | 0 | return (u4_sym); |
759 | 0 | } |