• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2018 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 *
24 * @file ihevce_cabac.h
25 *
26 * @brief
27 *  This file contains encoder cabac engine related structures and
28 *  interface prototypes
29 *
30 * @author
31 *  ittiam
32 *
33 ******************************************************************************
34 */
35 
36 #ifndef _IHEVCE_CABAC_H_
37 #define _IHEVCE_CABAC_H_
38 
39 #include "ihevc_debug.h"
40 #include "ihevc_macros.h"
41 
42 /*****************************************************************************/
43 /* Constant Macros                                                           */
44 /*****************************************************************************/
45 /**
46 *******************************************************************************
47 @brief Bit precision of cabac engine;
48 *******************************************************************************
49  */
50 #define CABAC_BITS 9
51 
52 /**
53 *******************************************************************************
54 @brief q format to account for the fractional bits encoded in cabac
55 *******************************************************************************
56  */
57 #define CABAC_FRAC_BITS_Q 12
58 
59 /**
60 *******************************************************************************
61 @brief Enables bit-efficient chroma cbf signalling by peeking into cbfs of
62        children nodes
63 *******************************************************************************
64  */
65 #define CABAC_BIT_EFFICIENT_CHROMA_PARENT_CBF 1
66 
67 /*****************************************************************************/
68 /* Function Macros                                                           */
69 /*****************************************************************************/
70 
71 /**
72 *******************************************************************************
73 @brief converts floating point number to CABAC_FRAC_BITS_Q q format and
74        rounds the results to 16 bit integer
75 *******************************************************************************
76  */
77 #define ROUND_Q12(x) ((UWORD16)(((x) * (1 << CABAC_FRAC_BITS_Q)) + 0.5))
78 
79 /*****************************************************************************/
80 /* Enums                                                                     */
81 /*****************************************************************************/
82 
83 /**
84 *******************************************************************************
85 @brief Enums for controlling the operating mode of cabac engine
86 *******************************************************************************
87  */
88 typedef enum
89 {
90     /** in this mode, bits are encoded in the bit stream buffer */
91     CABAC_MODE_ENCODE_BITS = 0,
92 
93     /** in this mode, only num bits gen are computed but not put in the stream */
94     CABAC_MODE_COMPUTE_BITS = 1
95 
96 } CABAC_OP_MODE;
97 
98 /*****************************************************************************/
99 /* Structures                                                                */
100 /*****************************************************************************/
101 
102 /**
103 ******************************************************************************
104  *  @brief      Cabac context for encoder
105 ******************************************************************************
106  */
107 typedef struct cab_ctxt
108 {
109     /**
110      * indicates if cabac encode works in put bits mode or bit compute mode
111      * In puts bits mode, bitstream and cabac engine fields L,R etc are used
112      * In bit compute mode, bitstream and cabac engine fields are not used
113      */
114     CABAC_OP_MODE e_cabac_op_mode;
115 
116     /**
117      * total bits estimated (for a cu) when mode is CABAC_MODE_COMPUTE_BITS
118      * This is in q12 format to account for the fractional bits as well
119      */
120     UWORD32 u4_bits_estimated_q12;
121 
122     /**
123      * total texture bits estimated (for a cu) when mode is CABAC_MODE_COMPUTE_BITS
124      * This is in q12 format to account for the fractional bits as well
125      */
126     UWORD32 u4_texture_bits_estimated_q12;
127 
128     /**
129      * total header bits estimated (for a cu) when mode is CABAC_MODE_COMPUTE_BITS
130      * This is in q12 format to account for the fractional bits as well
131      */
132     UWORD32 u4_header_bits_estimated_q12;
133 
134     UWORD32 u4_cbf_bits_q12;
135 
136     UWORD32 u4_true_tu_split_flag_q12;
137     /*********************************************************************/
138     /*  CABAC ENGINE related fields; not used in CABAC_MODE_COMPUTE_BITS */
139     /*********************************************************************/
140     /** cabac interval range R  */
141     UWORD32 u4_range;
142 
143     /** cabac interval start L  */
144     UWORD32 u4_low;
145 
146     /** bits generated during renormalization
147      *  A byte is put to stream/u4_out_standing_bytes from u4_low(L) when
148      *  u4_bits_gen exceeds 8
149      */
150     UWORD32 u4_bits_gen;
151 
152     /** bytes_outsanding; number of 0xFF bits that occur during renorm
153      *  These  will be accumulated till the carry bit is knwon
154      */
155     UWORD32 u4_out_standing_bytes;
156 
157     /*************************************************************************/
158     /*  OUTPUT Bitstream related fields; not used in CABAC_MODE_COMPUTE_BITS */
159     /*************************************************************************/
160     /** points to start of stream buffer.    */
161     UWORD8 *pu1_strm_buffer;
162 
163     /**
164      *  max bitstream size (in bytes).
165      *  Encoded stream shall not exceed this size.
166      */
167     UWORD32 u4_max_strm_size;
168 
169     /**
170     `*  byte offset (w.r.t pu1_strm_buffer) where next byte would be written
171      *  Bitstream engine makes sure it would not corrupt data beyond
172      *  u4_max_strm_size bytes
173      */
174     UWORD32 u4_strm_buf_offset;
175 
176     /**
177      *  signifies the number of consecutive zero bytes propogated from previous
178      *  word. It is used for emulation prevention byte insertion in the stream
179      */
180     WORD32 i4_zero_bytes_run;
181 
182     /*********************************************************************/
183     /*  CABAC context models                                             */
184     /*********************************************************************/
185     /** All Context models stored in packed form pState[bits6-1] | MPS[bit0] */
186     UWORD8 au1_ctxt_models[IHEVC_CAB_CTXT_END];
187 
188     /**
189      *Cabac context for start of every row which is same as top right ctxt
190      */
191     UWORD8 au1_ctxt_models_top_right[IHEVC_CAB_CTXT_END];
192 
193     /**
194      * copy of enable entropy coding sync flag in pps
195      */
196     WORD8 i1_entropy_coding_sync_enabled_flag;
197 
198     /**
199      * store the bitstream offset from which first slice data is generated by cabac
200      */
201     UWORD32 u4_first_slice_start_offset;
202 
203 } cab_ctxt_t;
204 
205 /*****************************************************************************/
206 /* Globals                                                                   */
207 /*****************************************************************************/
208 extern UWORD16 gau2_ihevce_cabac_bin_to_bits[64 * 2];
209 
210 /*****************************************************************************/
211 /* Extern Function Declarations                                              */
212 /*****************************************************************************/
213 WORD32
214     ihevce_cabac_reset(cab_ctxt_t *ps_cabac, bitstrm_t *ps_bitstrm, CABAC_OP_MODE e_cabac_op_mode);
215 
216 WORD32 ihevce_cabac_init(
217     cab_ctxt_t *ps_cabac,
218     bitstrm_t *ps_bitstrm,
219     WORD32 slice_qp,
220     WORD32 cabac_init_idc,
221     CABAC_OP_MODE e_cabac_op_mode);
222 
223 WORD32 ihevce_cabac_put_byte(cab_ctxt_t *ps_cabac);
224 
225 /**
226 ******************************************************************************
227 *
228 *  @brief Codes a bin based on probablilty and mps packed context model
229 *
230 *  @par   Description
231 *  1. Apart from encoding bin, context model is updated as per state transition
232 *  2. Range and Low renormalization is done based on bin and original state
233 *  3. After renorm bistream is updated (if required)
234 *
235 *  @param[inout]   ps_cabac
236 *  pointer to cabac context (handle)
237 *
238 *  @param[in]   bin
239 *  bin(boolean) to be encoded
240 *
241 *  @param[in]   ctxt_index
242 *  index of cabac context model containing pState[bits6-1] | MPS[bit0]
243 *
244 *  @return      success or failure error code
245 *
246 ******************************************************************************
247 */
ihevce_cabac_encode_bin(cab_ctxt_t * ps_cabac,WORD32 bin,WORD32 ctxt_index)248 static INLINE WORD32 ihevce_cabac_encode_bin(cab_ctxt_t *ps_cabac, WORD32 bin, WORD32 ctxt_index)
249 {
250     UWORD32 u4_range = ps_cabac->u4_range;
251     UWORD32 u4_low = ps_cabac->u4_low;
252     UWORD32 u4_rlps;
253     UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[ctxt_index];
254     WORD32 state_mps = *pu1_ctxt_model;
255     WORD32 shift;
256 
257     /* Sanity checks */
258     ASSERT((bin == 0) || (bin == 1));
259     ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
260     ASSERT(state_mps < 128);
261 
262     if(CABAC_MODE_ENCODE_BITS == ps_cabac->e_cabac_op_mode)
263     {
264         ASSERT((u4_range >= 256) && (u4_range < 512));
265 
266         /* Get the lps range from LUT based on quantized range and state */
267         u4_rlps = gau1_ihevc_cabac_rlps[state_mps >> 1][(u4_range >> 6) & 0x3];
268 
269         u4_range -= u4_rlps;
270 
271         /* check if bin is mps or lps */
272         if((state_mps & 0x1) ^ bin)
273         {
274             /* lps path;  L= L + R; R = RLPS */
275             u4_low += u4_range;
276             u4_range = u4_rlps;
277         }
278 
279         /*Compute bit always to populate the trace*/
280         /* increment bits generated based on state and bin encoded */
281         ps_cabac->u4_bits_estimated_q12 += gau2_ihevce_cabac_bin_to_bits[state_mps ^ bin];
282 
283         /* update the context model from state transition LUT */
284         *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | bin];
285 
286         /*****************************************************************/
287         /* Renormalization; calculate bits generated based on range(R)   */
288         /* Note : 6 <= R < 512; R is 2 only for terminating encode       */
289         /*****************************************************************/
290         GETRANGE(shift, u4_range);
291         shift = 9 - shift;
292         u4_low <<= shift;
293         u4_range <<= shift;
294 
295         /* bits to be inserted in the bitstream */
296         ps_cabac->u4_bits_gen += shift;
297         ps_cabac->u4_range = u4_range;
298         ps_cabac->u4_low = u4_low;
299 
300         /* generate stream when a byte is ready */
301         if(ps_cabac->u4_bits_gen > CABAC_BITS)
302         {
303             return (ihevce_cabac_put_byte(ps_cabac));
304         }
305     }
306     else /* (CABAC_MODE_COMPUTE_BITS == e_cabac_op_mode) */
307     {
308         /* increment bits generated based on state and bin encoded */
309         ps_cabac->u4_bits_estimated_q12 += gau2_ihevce_cabac_bin_to_bits[state_mps ^ bin];
310 
311         /* update the context model from state transition LUT */
312         *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | bin];
313     }
314 
315     return (IHEVCE_SUCCESS);
316 }
317 
318 WORD32 ihevce_cabac_encode_bypass_bin(cab_ctxt_t *ps_cabac, WORD32 bin);
319 
320 WORD32
321     ihevce_cabac_encode_terminate(cab_ctxt_t *ps_cabac, WORD32 term_bin, WORD32 i4_end_of_sub_strm);
322 
323 /**
324 ******************************************************************************
325 *
326 *  @brief Encodes a series of bypass bins (FLC bypass bins)
327 *
328 *  @par   Description
329 *  This function is more optimal than calling ihevce_cabac_encode_bypass_bin()
330 *  in a loop as cabac low, renorm and generating the stream (8bins at a time)
331 *  can be done in one operation
332 *
333 *  @param[inout]ps_cabac
334 *   pointer to cabac context (handle)
335 *
336 *  @param[in]   u4_sym
337 *   syntax element to be coded (as FLC bins)
338 *
339 *  @param[in]   num_bins
340 *   This is the FLC length for u4_sym
341 *
342 *
343 *  @return      success or failure error code
344 *
345 ******************************************************************************
346 */
347 static INLINE WORD32
ihevce_cabac_encode_bypass_bins(cab_ctxt_t * ps_cabac,UWORD32 u4_bins,WORD32 num_bins)348     ihevce_cabac_encode_bypass_bins(cab_ctxt_t *ps_cabac, UWORD32 u4_bins, WORD32 num_bins)
349 {
350     UWORD32 u4_range = ps_cabac->u4_range;
351     WORD32 next_byte;
352     WORD32 error = IHEVCE_SUCCESS;
353 
354     if(CABAC_MODE_ENCODE_BITS == ps_cabac->e_cabac_op_mode)
355     {
356         /* Sanity checks */
357         ASSERT((num_bins < 33) && (num_bins > 0));
358         ASSERT((u4_range >= 256) && (u4_range < 512));
359 
360         /*Compute bit always to populate the trace*/
361         /* increment bits generated by num_bins */
362         ps_cabac->u4_bits_estimated_q12 += (num_bins << CABAC_FRAC_BITS_Q);
363 
364         /* Encode 8bins at a time and put in the bit-stream */
365         while(num_bins > 8)
366         {
367             num_bins -= 8;
368 
369             /* extract the leading 8 bins */
370             next_byte = (u4_bins >> num_bins) & 0xff;
371 
372             /*  L = (L << 8) +  (R * next_byte) */
373             ps_cabac->u4_low <<= 8;
374             ps_cabac->u4_low += (next_byte * u4_range);
375             ps_cabac->u4_bits_gen += 8;
376 
377             if(ps_cabac->u4_bits_gen > CABAC_BITS)
378             {
379                 /*  insert the leading byte of low into stream */
380                 error |= ihevce_cabac_put_byte(ps_cabac);
381             }
382         }
383 
384         /* Update low with remaining bins and return */
385         next_byte = (u4_bins & ((1 << num_bins) - 1));
386 
387         ps_cabac->u4_low <<= num_bins;
388         ps_cabac->u4_low += (next_byte * u4_range);
389         ps_cabac->u4_bits_gen += num_bins;
390 
391         if(ps_cabac->u4_bits_gen > CABAC_BITS)
392         {
393             /*  insert the leading byte of low into stream */
394             error |= ihevce_cabac_put_byte(ps_cabac);
395         }
396     }
397     else
398     {
399         /* increment bits generated by num_bins */
400         ps_cabac->u4_bits_estimated_q12 += (num_bins << CABAC_FRAC_BITS_Q);
401     }
402 
403     return (error);
404 }
405 
406 WORD32 ihevce_cabac_encode_tunary(
407     cab_ctxt_t *ps_cabac,
408     WORD32 sym,
409     WORD32 c_max,
410     WORD32 ctxt_index,
411     WORD32 ctxt_shift,
412     WORD32 ctxt_inc_max);
413 
414 WORD32 ihevce_cabac_encode_tunary_bypass(cab_ctxt_t *ps_cabac, WORD32 sym, WORD32 c_max);
415 
416 WORD32 ihevce_cabac_encode_egk(cab_ctxt_t *ps_cabac, UWORD32 u4_sym, WORD32 k);
417 
418 WORD32 ihevce_cabac_encode_trunc_rice(
419     cab_ctxt_t *ps_cabac, UWORD32 u4_sym, WORD32 c_rice_param, WORD32 c_rice_max);
420 
421 WORD32 ihevce_cabac_flush(cab_ctxt_t *ps_cabac, WORD32 i4_end_of_sub_strm);
422 
423 WORD32 ihevce_cabac_ctxt_backup(cab_ctxt_t *ps_cabac);
424 
425 WORD32 ihevce_cabac_ctxt_row_init(cab_ctxt_t *ps_cabac);
426 
427 #endif /* _IHEVCE_CABAC_H_ */
428