• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2022 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  * @file
23  *  isvcd_parse_cavlc.c
24  *
25  * @brief
26  *  This file contains UVLC related functions.
27  *
28  * @author
29  *  Kishore
30  *
31  * @par List of Functions:
32  *  - isvcd_parse_bmb_ref_index_cavlc_range1()
33  *  - isvcd_parse_bmb_ref_index_cavlc()
34  *  - isvcd_parse_pmb_ref_index_cavlc()
35  *  - isvcd_parse_pmb_ref_index_cavlc_range1()
36  *
37  * @remarks
38  *  None
39  *
40  *******************************************************************************
41  */
42 
43 #include <string.h>
44 #include <stdio.h>
45 
46 #include "ih264d_bitstrm.h"
47 #include "isvcd_parse_cavlc.h"
48 #include "ih264d_error_handler.h"
49 #include "ih264d_defs.h"
50 #include "ih264d_debug.h"
51 #include "ih264d_cabac.h"
52 #include "isvcd_structs.h"
53 #include "ih264d_tables.h"
54 #include "ih264d_tables.h"
55 #include "ih264d_mb_utils.h"
56 #include "ih264d_parse_cavlc.h"
57 
58 /*****************************************************************************/
59 /*                                                                           */
60 /*  Function Name : isvcd_parse_bmb_ref_index_cavlc_range1                   */
61 /*                                                                           */
62 /*  Description   : This function does the Cavlc  TEV range > 1 parsing of   */
63 /*                  reference index  for a B MB.                             */
64 /*                  Range > 1 when num_ref_idx_active_minus1 > 0             */
65 /*                                                                           */
66 /*  Inputs        : <What inputs does the function take?>                    */
67 /*  Globals       : <Does it use any global variables?>                      */
68 /*  Processing    : <Describe how the function operates - include algorithm  */
69 /*                  description>                                             */
70 /*  Outputs       : <What does the function produce?>                        */
71 /*  Returns       : <What does the function return?>                         */
72 /*                                                                           */
73 /*  Issues        : <List any issues or problems with this function>         */
74 /*                                                                           */
75 /*  Revision History:                                                        */
76 /*                                                                           */
77 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
78 /*         19 09 2008   Jay          Draft                                   */
79 /*                                                                           */
80 /*****************************************************************************/
81 
isvcd_parse_bmb_ref_index_cavlc_range1(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1,UWORD8 * pu1_motion_prediction_flag)82 void isvcd_parse_bmb_ref_index_cavlc_range1(
83     UWORD32 u4_num_part,                  /* Number of partitions in MB      */
84     dec_bit_stream_t *ps_bitstrm,         /* Pointer to bitstream Structure. */
85     WORD8 *pi1_ref_idx,                   /* pointer to reference index array */
86     UWORD32 u4_num_ref_idx_active_minus1, /* Not used for range 1    */
87     UWORD8 *pu1_motion_prediction_flag    /*motion_pred_flag */
88 
89 )
90 {
91     UWORD32 u4_i;
92     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
93     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
94     UNUSED(u4_num_ref_idx_active_minus1);
95     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
96     {
97         if(pi1_ref_idx[u4_i] > -1 && (((*pu1_motion_prediction_flag >> u4_i) & 0x01) == 0))
98         {
99             UWORD32 u4_ref_idx;
100             u4_ref_idx = ih264d_tev_range1(pu4_bitstream_off, pu4_bitstrm_buf);
101 
102             /* Storing Reference Idx Information */
103             pi1_ref_idx[u4_i] = (WORD8) u4_ref_idx;
104         }
105     }
106 }
107 
108 /*****************************************************************************/
109 /*                                                                           */
110 /*  Function Name : isvcd_parse_bmb_ref_index_cavlc                          */
111 /*                                                                           */
112 /*  Description   : This function does the Cavlc  TEV range > 1 parsing of   */
113 /*                  reference index  for a B MB.                             */
114 /*                  Range > 1 when num_ref_idx_active_minus1 > 0             */
115 /*                                                                           */
116 /*  Inputs        : <What inputs does the function take?>                    */
117 /*  Globals       : <Does it use any global variables?>                      */
118 /*  Processing    : <Describe how the function operates - include algorithm  */
119 /*                  description>                                             */
120 /*  Outputs       : <What does the function produce?>                        */
121 /*  Returns       : <What does the function return?>                         */
122 /*                                                                           */
123 /*  Issues        : <List any issues or problems with this function>         */
124 /*                                                                           */
125 /*  Revision History:                                                        */
126 /*                                                                           */
127 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
128 /*         19 09 2008   Jay          Draft                                   */
129 /*                                                                           */
130 /*****************************************************************************/
isvcd_parse_bmb_ref_index_cavlc(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1,UWORD8 * pu1_motion_prediction_flag)131 WORD32 isvcd_parse_bmb_ref_index_cavlc(
132     UWORD32 u4_num_part,                  /* Number of partitions in MB      */
133     dec_bit_stream_t *ps_bitstrm,         /* Pointer to bitstream Structure. */
134     WORD8 *pi1_ref_idx,                   /* pointer to reference index array */
135     UWORD32 u4_num_ref_idx_active_minus1, /* Number of active references - 1  */
136     UWORD8 *pu1_motion_prediction_flag    /*motion_pred_flag */
137 )
138 {
139     UWORD32 u4_i;
140     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
141     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
142 
143     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
144     {
145         if(pi1_ref_idx[u4_i] > -1 && (((*pu1_motion_prediction_flag >> u4_i) & 0x01) == 0))
146         {
147             UWORD32 u4_ref_idx;
148             // inlining ih264d_uev
149             UWORD32 u4_bitstream_offset = *pu4_bitstream_off;
150             UWORD32 u4_word, u4_ldz;
151 
152             /***************************************************************/
153             /* Find leading zeros in next 32 bits                          */
154             /***************************************************************/
155             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
156             u4_ldz = CLZ(u4_word);
157             /* Flush the ps_bitstrm */
158             u4_bitstream_offset += (u4_ldz + 1);
159             /* Read the suffix from the ps_bitstrm */
160             u4_word = 0;
161             if(u4_ldz) GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
162             *pu4_bitstream_off = u4_bitstream_offset;
163             u4_ref_idx = ((1 << u4_ldz) + u4_word - 1);
164             // inlining ih264d_uev
165             if(u4_ref_idx > u4_num_ref_idx_active_minus1) return ERROR_REF_IDX;
166 
167             /* Storing Reference Idx Information */
168             pi1_ref_idx[u4_i] = (WORD8) u4_ref_idx;
169         }
170     }
171     return OK;
172 }
173 
174 /*****************************************************************************/
175 /*                                                                           */
176 /*  Function Name : isvcd_parse_pmb_ref_index_cavlc                          */
177 /*                                                                           */
178 /*  Description   : This function does the Cavlc  TEV range > 1 parsing of   */
179 /*                  reference index  for a P MB.                             */
180 /*                  Range > 1 when num_ref_idx_active_minus1 > 0             */
181 /*                                                                           */
182 /*  Inputs        : <What inputs does the function take?>                    */
183 /*  Globals       : <Does it use any global variables?>                      */
184 /*  Processing    : <Describe how the function operates - include algorithm  */
185 /*                  description>                                             */
186 /*  Outputs       : <What does the function produce?>                        */
187 /*  Returns       : <What does the function return?>                         */
188 /*                                                                           */
189 /*  Issues        : <List any issues or problems with this function>         */
190 /*                                                                           */
191 /*  Revision History:                                                        */
192 /*                                                                           */
193 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
194 /*         19 09 2008   Jay          Draft                                   */
195 /*                                                                           */
196 /*****************************************************************************/
197 
isvcd_parse_pmb_ref_index_cavlc(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1,UWORD8 * pu1_motion_prediction_flag)198 WORD32 isvcd_parse_pmb_ref_index_cavlc(
199     UWORD32 u4_num_part,                  /* Number of partitions in MB      */
200     dec_bit_stream_t *ps_bitstrm,         /* Pointer to bitstream Structure. */
201     WORD8 *pi1_ref_idx,                   /* pointer to reference index array */
202     UWORD32 u4_num_ref_idx_active_minus1, /* Number of active references - 1  */
203     UWORD8 *pu1_motion_prediction_flag    /*motion_pred_flag_l0 */
204 )
205 {
206     UWORD32 u4_i;
207     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
208     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
209 
210     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
211     {
212         if(((*pu1_motion_prediction_flag >> u4_i) & 0x01) == 0)
213         {
214             UWORD32 u4_ref_idx;
215             // Inlined ih264d_uev
216             UWORD32 u4_bitstream_offset = *pu4_bitstream_off;
217             UWORD32 u4_word, u4_ldz;
218 
219             /***************************************************************/
220             /* Find leading zeros in next 32 bits                          */
221             /***************************************************************/
222             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
223             u4_ldz = CLZ(u4_word);
224             /* Flush the ps_bitstrm */
225             u4_bitstream_offset += (u4_ldz + 1);
226             /* Read the suffix from the ps_bitstrm */
227             u4_word = 0;
228             if(u4_ldz) GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
229             *pu4_bitstream_off = u4_bitstream_offset;
230             u4_ref_idx = ((1 << u4_ldz) + u4_word - 1);
231 
232             // Inlined ih264d_uev
233             if(u4_ref_idx > u4_num_ref_idx_active_minus1) return ERROR_REF_IDX;
234 
235             /* Storing Reference Idx Information */
236             pi1_ref_idx[u4_i] = (WORD8) u4_ref_idx;
237         }
238     }
239     return OK;
240 }
241 
242 /*****************************************************************************/
243 /*                                                                           */
244 /*  Function Name : isvcd_parse_pmb_ref_index_cavlc_range1                   */
245 /*                                                                           */
246 /*  Description   : This function does the Cavlc  TEV range =1 parsing of    */
247 /*                  reference index  for a P MB. Range is 1 when             */
248 /*                  num_ref_idx_active_minus1 is 0                           */
249 /*                                                                           */
250 /*  Inputs        : <What inputs does the function take?>                    */
251 /*  Globals       : <Does it use any global variables?>                      */
252 /*  Processing    : <Describe how the function operates - include algorithm  */
253 /*                  description>                                             */
254 /*  Outputs       : <What does the function produce?>                        */
255 /*  Returns       : <What does the function return?>                         */
256 /*                                                                           */
257 /*  Issues        : <List any issues or problems with this function>         */
258 /*                                                                           */
259 /*  Revision History:                                                        */
260 /*                                                                           */
261 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
262 /*         19 09 2008   Jay          Draft                                   */
263 /*                                                                           */
264 /*****************************************************************************/
isvcd_parse_pmb_ref_index_cavlc_range1(UWORD32 u4_num_part,dec_bit_stream_t * ps_bitstrm,WORD8 * pi1_ref_idx,UWORD32 u4_num_ref_idx_active_minus1,UWORD8 * pu1_motion_prediction_flag)265 void isvcd_parse_pmb_ref_index_cavlc_range1(
266     UWORD32 u4_num_part,                  /* Number of partitions in MB      */
267     dec_bit_stream_t *ps_bitstrm,         /* Pointer to bitstream Structure. */
268     WORD8 *pi1_ref_idx,                   /* pointer to reference index array */
269     UWORD32 u4_num_ref_idx_active_minus1, /* Not used for range 1    */
270     UWORD8 *pu1_motion_prediction_flag    /*motion_pred_flag_l0 */
271 )
272 {
273     UWORD32 u4_i;
274     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
275     UWORD32 *pu4_bitstream_off = &ps_bitstrm->u4_ofst;
276     UNUSED(u4_num_ref_idx_active_minus1);
277     for(u4_i = 0; u4_i < u4_num_part; u4_i++)
278     {
279         if(((*pu1_motion_prediction_flag >> u4_i) & 0x01) == 0)
280         {
281             UWORD32 u4_ref_idx;
282             u4_ref_idx = ih264d_tev_range1(pu4_bitstream_off, pu4_bitstrm_buf);
283 
284             /* Storing Reference Idx Information */
285             pi1_ref_idx[u4_i] = (WORD8) u4_ref_idx;
286         }
287     }
288 }
289