1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*------------------------------------------------------------------------------
18 
19     Table of contents
20 
21      1. Include headers
22      2. External compiler flags
23      3. Module defines
24      4. Local function prototypes
25      5. Functions
26           DecodeCoeffToken
27           DecodeLevelPrefix
28           DecodeTotalZeros
29           DecodeRunBefore
30           DecodeResidualBlockCavlc
31 
32 ------------------------------------------------------------------------------*/
33 
34 /*------------------------------------------------------------------------------
35     1. Include headers
36 ------------------------------------------------------------------------------*/
37 
38 #include "h264bsd_cavlc.h"
39 #include "h264bsd_util.h"
40 
41 /*------------------------------------------------------------------------------
42     2. External compiler flags
43 --------------------------------------------------------------------------------
44 
45 --------------------------------------------------------------------------------
46     3. Module defines
47 ------------------------------------------------------------------------------*/
48 
49 /* Following descriptions use term "information field" to represent combination
50  * of certain decoded symbol value and the length of the corresponding variable
51  * length code word. For example, total_zeros information field consists of
52  * 4 bits symbol value (bits [4,7]) along with four bits to represent length
53  * of the VLC code word (bits [0,3]) */
54 
55 /* macro to obtain length of the coeff token information field, bits [0,4]  */
56 #define LENGTH_TC(vlc) ((vlc) & 0x1F)
57 /* macro to obtain length of the other information fields, bits [0,3] */
58 #define LENGTH(vlc) ((vlc) & 0xF)
59 /* macro to obtain code word from the information fields, bits [4,7] */
60 #define INFO(vlc) (((vlc) >> 4) & 0xF)  /* 4 MSB bits contain information */
61 /* macro to obtain trailing ones from the coeff token information word,
62  * bits [5,10] */
63 #define TRAILING_ONES(coeffToken) ((coeffToken>>5) & 0x3F)
64 /* macro to obtain total coeff from the coeff token information word,
65  * bits [11,15] */
66 #define TOTAL_COEFF(coeffToken) (((coeffToken) >> 11) & 0x1F)
67 
68 #define VLC_NOT_FOUND 0xFFFFFFFEU
69 
70 /* VLC tables for coeff_token. Because of long codes (max. 16 bits) some of the
71  * tables have been splitted into multiple separate tables. Each array/table
72  * element has the following structure:
73  * [5 bits for tot.coeff.] [6 bits for tr.ones] [5 bits for VLC length]
74  * If there is a 0x0000 value, it means that there is not corresponding VLC
75  * codeword for that index. */
76 
77 /* VLC lengths up to 6 bits, 0 <= nC < 2 */
78 static const u16 coeffToken0_0[32] = {
79     0x0000,0x0000,0x0000,0x2066,0x1026,0x0806,0x1865,0x1865,
80     0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,
81     0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,
82     0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822};
83 
84 /* VLC lengths up to 10 bits, 0 <= nC < 2 */
85 static const u16 coeffToken0_1[48] = {
86     0x0000,0x0000,0x0000,0x0000,0x406a,0x304a,0x282a,0x200a,
87     0x3869,0x3869,0x2849,0x2849,0x2029,0x2029,0x1809,0x1809,
88     0x3068,0x3068,0x3068,0x3068,0x2048,0x2048,0x2048,0x2048,
89     0x1828,0x1828,0x1828,0x1828,0x1008,0x1008,0x1008,0x1008,
90     0x2867,0x2867,0x2867,0x2867,0x2867,0x2867,0x2867,0x2867,
91     0x1847,0x1847,0x1847,0x1847,0x1847,0x1847,0x1847,0x1847};
92 
93 /* VLC lengths up to 14 bits, 0 <= nC < 2 */
94 static const u16 coeffToken0_2[56] = {
95     0x606e,0x584e,0x502e,0x500e,0x586e,0x504e,0x482e,0x480e,
96     0x400d,0x400d,0x484d,0x484d,0x402d,0x402d,0x380d,0x380d,
97     0x506d,0x506d,0x404d,0x404d,0x382d,0x382d,0x300d,0x300d,
98     0x486b,0x486b,0x486b,0x486b,0x486b,0x486b,0x486b,0x486b,
99     0x384b,0x384b,0x384b,0x384b,0x384b,0x384b,0x384b,0x384b,
100     0x302b,0x302b,0x302b,0x302b,0x302b,0x302b,0x302b,0x302b,
101     0x280b,0x280b,0x280b,0x280b,0x280b,0x280b,0x280b,0x280b};
102 
103 /* VLC lengths up to 16 bits, 0 <= nC < 2 */
104 static const u16 coeffToken0_3[32] = {
105     0x0000,0x0000,0x682f,0x682f,0x8010,0x8050,0x8030,0x7810,
106     0x8070,0x7850,0x7830,0x7010,0x7870,0x7050,0x7030,0x6810,
107     0x706f,0x706f,0x684f,0x684f,0x602f,0x602f,0x600f,0x600f,
108     0x686f,0x686f,0x604f,0x604f,0x582f,0x582f,0x580f,0x580f};
109 
110 /* VLC lengths up to 6 bits, 2 <= nC < 4 */
111 static const u16 coeffToken2_0[32] = {
112     0x0000,0x0000,0x0000,0x0000,0x3866,0x2046,0x2026,0x1006,
113     0x3066,0x1846,0x1826,0x0806,0x2865,0x2865,0x1025,0x1025,
114     0x2064,0x2064,0x2064,0x2064,0x1864,0x1864,0x1864,0x1864,
115     0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043};
116 
117 /* VLC lengths up to 9 bits, 2 <= nC < 4 */
118 static const u16 coeffToken2_1[32] = {
119     0x0000,0x0000,0x0000,0x0000,0x4869,0x3849,0x3829,0x3009,
120     0x2808,0x2808,0x3048,0x3048,0x3028,0x3028,0x2008,0x2008,
121     0x4067,0x4067,0x4067,0x4067,0x2847,0x2847,0x2847,0x2847,
122     0x2827,0x2827,0x2827,0x2827,0x1807,0x1807,0x1807,0x1807};
123 
124 /* VLC lengths up to 14 bits, 2 <= nC < 4 */
125 static const u16 coeffToken2_2[128] = {
126     0x0000,0x0000,0x786d,0x786d,0x806e,0x804e,0x802e,0x800e,
127     0x782e,0x780e,0x784e,0x702e,0x704d,0x704d,0x700d,0x700d,
128     0x706d,0x706d,0x684d,0x684d,0x682d,0x682d,0x680d,0x680d,
129     0x686d,0x686d,0x604d,0x604d,0x602d,0x602d,0x600d,0x600d,
130     0x580c,0x580c,0x580c,0x580c,0x584c,0x584c,0x584c,0x584c,
131     0x582c,0x582c,0x582c,0x582c,0x500c,0x500c,0x500c,0x500c,
132     0x606c,0x606c,0x606c,0x606c,0x504c,0x504c,0x504c,0x504c,
133     0x502c,0x502c,0x502c,0x502c,0x480c,0x480c,0x480c,0x480c,
134     0x586b,0x586b,0x586b,0x586b,0x586b,0x586b,0x586b,0x586b,
135     0x484b,0x484b,0x484b,0x484b,0x484b,0x484b,0x484b,0x484b,
136     0x482b,0x482b,0x482b,0x482b,0x482b,0x482b,0x482b,0x482b,
137     0x400b,0x400b,0x400b,0x400b,0x400b,0x400b,0x400b,0x400b,
138     0x506b,0x506b,0x506b,0x506b,0x506b,0x506b,0x506b,0x506b,
139     0x404b,0x404b,0x404b,0x404b,0x404b,0x404b,0x404b,0x404b,
140     0x402b,0x402b,0x402b,0x402b,0x402b,0x402b,0x402b,0x402b,
141     0x380b,0x380b,0x380b,0x380b,0x380b,0x380b,0x380b,0x380b};
142 
143 /* VLC lengths up to 6 bits, 4 <= nC < 8 */
144 static const u16 coeffToken4_0[64] = {
145     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
146     0x1806,0x3846,0x3826,0x1006,0x4866,0x3046,0x3026,0x0806,
147     0x2825,0x2825,0x2845,0x2845,0x2025,0x2025,0x2045,0x2045,
148     0x1825,0x1825,0x4065,0x4065,0x1845,0x1845,0x1025,0x1025,
149     0x3864,0x3864,0x3864,0x3864,0x3064,0x3064,0x3064,0x3064,
150     0x2864,0x2864,0x2864,0x2864,0x2064,0x2064,0x2064,0x2064,
151     0x1864,0x1864,0x1864,0x1864,0x1044,0x1044,0x1044,0x1044,
152     0x0824,0x0824,0x0824,0x0824,0x0004,0x0004,0x0004,0x0004};
153 
154 /* VLC lengths up to 10 bits, 4 <= nC < 8 */
155 static const u16 coeffToken4_1[128] = {
156     0x0000,0x800a,0x806a,0x804a,0x802a,0x780a,0x786a,0x784a,
157     0x782a,0x700a,0x706a,0x704a,0x702a,0x680a,0x6829,0x6829,
158     0x6009,0x6009,0x6849,0x6849,0x6029,0x6029,0x5809,0x5809,
159     0x6869,0x6869,0x6049,0x6049,0x5829,0x5829,0x5009,0x5009,
160     0x6068,0x6068,0x6068,0x6068,0x5848,0x5848,0x5848,0x5848,
161     0x5028,0x5028,0x5028,0x5028,0x4808,0x4808,0x4808,0x4808,
162     0x5868,0x5868,0x5868,0x5868,0x5048,0x5048,0x5048,0x5048,
163     0x4828,0x4828,0x4828,0x4828,0x4008,0x4008,0x4008,0x4008,
164     0x3807,0x3807,0x3807,0x3807,0x3807,0x3807,0x3807,0x3807,
165     0x3007,0x3007,0x3007,0x3007,0x3007,0x3007,0x3007,0x3007,
166     0x4847,0x4847,0x4847,0x4847,0x4847,0x4847,0x4847,0x4847,
167     0x2807,0x2807,0x2807,0x2807,0x2807,0x2807,0x2807,0x2807,
168     0x5067,0x5067,0x5067,0x5067,0x5067,0x5067,0x5067,0x5067,
169     0x4047,0x4047,0x4047,0x4047,0x4047,0x4047,0x4047,0x4047,
170     0x4027,0x4027,0x4027,0x4027,0x4027,0x4027,0x4027,0x4027,
171     0x2007,0x2007,0x2007,0x2007,0x2007,0x2007,0x2007,0x2007};
172 
173 /* fixed 6 bit length VLC, nC <= 8 */
174 static const u16 coeffToken8[64] = {
175     0x0806,0x0826,0x0000,0x0006,0x1006,0x1026,0x1046,0x0000,
176     0x1806,0x1826,0x1846,0x1866,0x2006,0x2026,0x2046,0x2066,
177     0x2806,0x2826,0x2846,0x2866,0x3006,0x3026,0x3046,0x3066,
178     0x3806,0x3826,0x3846,0x3866,0x4006,0x4026,0x4046,0x4066,
179     0x4806,0x4826,0x4846,0x4866,0x5006,0x5026,0x5046,0x5066,
180     0x5806,0x5826,0x5846,0x5866,0x6006,0x6026,0x6046,0x6066,
181     0x6806,0x6826,0x6846,0x6866,0x7006,0x7026,0x7046,0x7066,
182     0x7806,0x7826,0x7846,0x7866,0x8006,0x8026,0x8046,0x8066};
183 
184 /* VLC lengths up to 3 bits, nC == -1 */
185 static const u16 coeffTokenMinus1_0[8] = {
186     0x0000,0x1043,0x0002,0x0002,0x0821,0x0821,0x0821,0x0821};
187 
188 /* VLC lengths up to 8 bits, nC == -1 */
189 static const u16 coeffTokenMinus1_1[32] = {
190     0x2067,0x2067,0x2048,0x2028,0x1847,0x1847,0x1827,0x1827,
191     0x2006,0x2006,0x2006,0x2006,0x1806,0x1806,0x1806,0x1806,
192     0x1006,0x1006,0x1006,0x1006,0x1866,0x1866,0x1866,0x1866,
193     0x1026,0x1026,0x1026,0x1026,0x0806,0x0806,0x0806,0x0806};
194 
195 /* VLC tables for total_zeros. One table containing longer code, totalZeros_1,
196  * has been broken into two separate tables. Table elements have the
197  * following structure:
198  * [4 bits for info] [4 bits for VLC length] */
199 
200 /* VLC lengths up to 5 bits */
201 static const u8 totalZeros_1_0[32] = {
202     0x00,0x00,0x65,0x55,0x44,0x44,0x34,0x34,
203     0x23,0x23,0x23,0x23,0x13,0x13,0x13,0x13,
204     0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
205     0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
206 
207 /* VLC lengths up to 9 bits */
208 static const u8 totalZeros_1_1[32] = {
209     0x00,0xf9,0xe9,0xd9,0xc8,0xc8,0xb8,0xb8,
210     0xa7,0xa7,0xa7,0xa7,0x97,0x97,0x97,0x97,
211     0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,
212     0x76,0x76,0x76,0x76,0x76,0x76,0x76,0x76};
213 
214 static const u8 totalZeros_2[64] = {
215     0xe6,0xd6,0xc6,0xb6,0xa5,0xa5,0x95,0x95,
216     0x84,0x84,0x84,0x84,0x74,0x74,0x74,0x74,
217     0x64,0x64,0x64,0x64,0x54,0x54,0x54,0x54,
218     0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,
219     0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
220     0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
221     0x13,0x13,0x13,0x13,0x13,0x13,0x13,0x13,
222     0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03};
223 
224 static const u8 totalZeros_3[64] = {
225     0xd6,0xb6,0xc5,0xc5,0xa5,0xa5,0x95,0x95,
226     0x84,0x84,0x84,0x84,0x54,0x54,0x54,0x54,
227     0x44,0x44,0x44,0x44,0x04,0x04,0x04,0x04,
228     0x73,0x73,0x73,0x73,0x73,0x73,0x73,0x73,
229     0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
230     0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
231     0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
232     0x13,0x13,0x13,0x13,0x13,0x13,0x13,0x13};
233 
234 static const u8 totalZeros_4[32] = {
235     0xc5,0xb5,0xa5,0x05,0x94,0x94,0x74,0x74,
236     0x34,0x34,0x24,0x24,0x83,0x83,0x83,0x83,
237     0x63,0x63,0x63,0x63,0x53,0x53,0x53,0x53,
238     0x43,0x43,0x43,0x43,0x13,0x13,0x13,0x13};
239 
240 static const u8 totalZeros_5[32] = {
241     0xb5,0x95,0xa4,0xa4,0x84,0x84,0x24,0x24,
242     0x14,0x14,0x04,0x04,0x73,0x73,0x73,0x73,
243     0x63,0x63,0x63,0x63,0x53,0x53,0x53,0x53,
244     0x43,0x43,0x43,0x43,0x33,0x33,0x33,0x33};
245 
246 static const u8 totalZeros_6[64] = {
247     0xa6,0x06,0x15,0x15,0x84,0x84,0x84,0x84,
248     0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
249     0x73,0x73,0x73,0x73,0x73,0x73,0x73,0x73,
250     0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
251     0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x53,
252     0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,
253     0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
254     0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23};
255 
256 static const u8 totalZeros_7[64] = {
257     0x96,0x06,0x15,0x15,0x74,0x74,0x74,0x74,
258     0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x83,
259     0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
260     0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,
261     0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
262     0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
263     0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
264     0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52};
265 
266 static const u8 totalZeros_8[64] = {
267     0x86,0x06,0x25,0x25,0x14,0x14,0x14,0x14,
268     0x73,0x73,0x73,0x73,0x73,0x73,0x73,0x73,
269     0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
270     0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
271     0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
272     0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
273     0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
274     0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42};
275 
276 static const u8 totalZeros_9[64] = {
277     0x16,0x06,0x75,0x75,0x24,0x24,0x24,0x24,
278     0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x53,
279     0x62,0x62,0x62,0x62,0x62,0x62,0x62,0x62,
280     0x62,0x62,0x62,0x62,0x62,0x62,0x62,0x62,
281     0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
282     0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
283     0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32,
284     0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32};
285 
286 static const u8 totalZeros_10[32] = {
287     0x15,0x05,0x64,0x64,0x23,0x23,0x23,0x23,
288     0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
289     0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
290     0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32};
291 
292 static const u8 totalZeros_11[16] = {
293     0x04,0x14,0x23,0x23,0x33,0x33,0x53,0x53,
294     0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41};
295 
296 static const u8 totalZeros_12[16] = {
297     0x04,0x14,0x43,0x43,0x22,0x22,0x22,0x22,
298     0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31};
299 
300 static const u8 totalZeros_13[8] = {0x03,0x13,0x32,0x32,0x21,0x21,0x21,0x21};
301 
302 static const u8 totalZeros_14[4] = {0x02,0x12,0x21,0x21};
303 
304 /* VLC tables for run_before. Table elements have the following structure:
305  * [4 bits for info] [4bits for VLC length]
306  */
307 
308 static const u8 runBefore_6[8] = {0x13,0x23,0x43,0x33,0x63,0x53,0x02,0x02};
309 
310 static const u8 runBefore_5[8] = {0x53,0x43,0x33,0x23,0x12,0x12,0x02,0x02};
311 
312 static const u8 runBefore_4[8] = {0x43,0x33,0x22,0x22,0x12,0x12,0x02,0x02};
313 
314 static const u8 runBefore_3[4] = {0x32,0x22,0x12,0x02};
315 
316 static const u8 runBefore_2[4] = {0x22,0x12,0x01,0x01};
317 
318 static const u8 runBefore_1[2] = {0x11,0x01};
319 
320 /* following four macros are used to handle stream buffer "cache" in the CAVLC
321  * decoding function */
322 
323 /* macro to initialize stream buffer cache, fills the buffer (32 bits) */
324 #define BUFFER_INIT(value, bits) \
325 { \
326     bits = 32; \
327     value = h264bsdShowBits32(pStrmData); \
328 }
329 
330 /* macro to read numBits bits from the buffer, bits will be written to
331  * outVal. Refills the buffer if not enough bits left */
332 #define BUFFER_SHOW(value, bits, outVal, numBits) \
333 { \
334     if (bits < (numBits)) \
335     { \
336         if(h264bsdFlushBits(pStrmData,32-bits) == END_OF_STREAM) \
337             return(HANTRO_NOK); \
338         value = h264bsdShowBits32(pStrmData); \
339         bits = 32; \
340     } \
341     (outVal) = value >> (32 - (numBits)); \
342 }
343 
344 /* macro to flush numBits bits from the buffer */
345 #define BUFFER_FLUSH(value, bits, numBits) \
346 { \
347     value <<= (numBits); \
348     bits -= (numBits); \
349 }
350 
351 /* macro to read and flush  numBits bits from the buffer, bits will be written
352  * to outVal. Refills the buffer if not enough bits left */
353 #define BUFFER_GET(value, bits, outVal, numBits) \
354 { \
355     if (bits < (numBits)) \
356     { \
357         if(h264bsdFlushBits(pStrmData,32-bits) == END_OF_STREAM) \
358             return(HANTRO_NOK); \
359         value = h264bsdShowBits32(pStrmData); \
360         bits = 32; \
361     } \
362     (outVal) = value >> (32 - (numBits)); \
363     value <<= (numBits); \
364     bits -= (numBits); \
365 }
366 
367 /*------------------------------------------------------------------------------
368     4. Local function prototypes
369 ------------------------------------------------------------------------------*/
370 
371 static u32 DecodeCoeffToken(u32 bits, u32 nc);
372 
373 static u32 DecodeLevelPrefix(u32 bits);
374 
375 static u32 DecodeTotalZeros(u32 bits, u32 totalCoeff, u32 isChromaDC);
376 
377 static u32 DecodeRunBefore(u32 bits,u32 zerosLeft);
378 
379 /*------------------------------------------------------------------------------
380 
381     Function: DecodeCoeffToken
382 
383         Functional description:
384           Function to decode coeff_token information field from the stream.
385 
386         Inputs:
387           u32 bits                  next 16 stream bits
388           u32 nc                    nC, see standard for details
389 
390         Outputs:
391           u32  information field (11 bits for value, 5 bits for length)
392 
393 ------------------------------------------------------------------------------*/
394 
DecodeCoeffToken(u32 bits,u32 nc)395 u32 DecodeCoeffToken(u32 bits, u32 nc)
396 {
397 
398 /* Variables */
399 
400     u32 value;
401 
402 /* Code */
403 
404     /* standard defines that nc for decoding of chroma dc coefficients is -1,
405      * represented by u32 here -> -1 maps to 2^32 - 1 */
406     ASSERT(nc <= 16 || nc == (u32)(-1));
407 
408     if (nc < 2)
409     {
410         if (bits >= 0x8000)
411         {
412             value = 0x0001;
413         }
414         else if (bits >= 0x0C00)
415             value = coeffToken0_0[bits >> 10];
416         else if (bits >= 0x0100)
417             value = coeffToken0_1[bits >> 6];
418         else if (bits >= 0x0020)
419             value = coeffToken0_2[(bits>>2)-8];
420         else
421             value = coeffToken0_3[bits];
422     }
423     else if (nc < 4)
424     {
425         if (bits >= 0x8000)
426         {
427             value = bits & 0x4000 ? 0x0002 : 0x0822;
428         }
429         else if (bits >= 0x1000)
430             value = coeffToken2_0[bits >> 10];
431         else if (bits >= 0x0200)
432             value = coeffToken2_1[bits >> 7];
433         else
434             value = coeffToken2_2[bits>>2];
435     }
436     else if (nc < 8)
437     {
438         value = coeffToken4_0[bits >> 10];
439         if (!value)
440             value = coeffToken4_1[bits>>6];
441     }
442     else if (nc <= 16)
443     {
444         value = coeffToken8[bits>>10];
445     }
446     else
447     {
448         value = coeffTokenMinus1_0[bits >> 13];
449         if (!value)
450             value = coeffTokenMinus1_1[bits>>8];
451     }
452 
453     return(value);
454 
455 }
456 
457 /*------------------------------------------------------------------------------
458 
459     Function: DecodeLevelPrefix
460 
461         Functional description:
462           Function to decode level_prefix information field from the stream
463 
464         Inputs:
465           u32 bits      next 16 stream bits
466 
467         Outputs:
468           u32  level_prefix information field or VLC_NOT_FOUND
469 
470 ------------------------------------------------------------------------------*/
471 
DecodeLevelPrefix(u32 bits)472 u32 DecodeLevelPrefix(u32 bits)
473 {
474 
475 /* Variables */
476 
477     u32 numZeros;
478 
479 /* Code */
480 
481     if (bits >= 0x8000)
482         numZeros = 0;
483     else if (bits >= 0x4000)
484         numZeros = 1;
485     else if (bits >= 0x2000)
486         numZeros = 2;
487     else if (bits >= 0x1000)
488         numZeros = 3;
489     else if (bits >= 0x0800)
490         numZeros = 4;
491     else if (bits >= 0x0400)
492         numZeros = 5;
493     else if (bits >= 0x0200)
494         numZeros = 6;
495     else if (bits >= 0x0100)
496         numZeros = 7;
497     else if (bits >= 0x0080)
498         numZeros = 8;
499     else if (bits >= 0x0040)
500         numZeros = 9;
501     else if (bits >= 0x0020)
502         numZeros = 10;
503     else if (bits >= 0x0010)
504         numZeros = 11;
505     else if (bits >= 0x0008)
506         numZeros = 12;
507     else if (bits >= 0x0004)
508         numZeros = 13;
509     else if (bits >= 0x0002)
510         numZeros = 14;
511     else if (bits >= 0x0001)
512         numZeros = 15;
513     else /* more than 15 zeros encountered which is an error */
514         return(VLC_NOT_FOUND);
515 
516     return(numZeros);
517 
518 }
519 
520 /*------------------------------------------------------------------------------
521 
522     Function: DecodeTotalZeros
523 
524         Functional description:
525           Function to decode total_zeros information field from the stream
526 
527         Inputs:
528           u32 bits                  next 9 stream bits
529           u32 totalCoeff            total number of coefficients for the block
530                                     being decoded
531           u32 isChromaDC           flag to indicate chroma DC block
532 
533         Outputs:
534           u32  information field (4 bits value, 4 bits length)
535 
536 ------------------------------------------------------------------------------*/
537 
DecodeTotalZeros(u32 bits,u32 totalCoeff,u32 isChromaDC)538 u32 DecodeTotalZeros(u32 bits, u32 totalCoeff, u32 isChromaDC)
539 {
540 
541 /* Variables */
542 
543     u32 value = 0x0;
544 
545 /* Code */
546 
547     ASSERT(totalCoeff);
548 
549     if (!isChromaDC)
550     {
551         ASSERT(totalCoeff < 16);
552         switch (totalCoeff)
553         {
554             case 1:
555                 value = totalZeros_1_0[bits >> 4];
556                 if (!value)
557                     value = totalZeros_1_1[bits];
558                 break;
559 
560             case 2:
561                 value = totalZeros_2[bits >> 3];
562                 break;
563 
564             case 3:
565                 value = totalZeros_3[bits >> 3];
566                 break;
567 
568             case 4:
569                 value = totalZeros_4[bits >> 4];
570                 break;
571 
572             case 5:
573                 value = totalZeros_5[bits >> 4];
574                 break;
575 
576             case 6:
577                 value = totalZeros_6[bits >> 3];
578                 break;
579 
580             case 7:
581                 value = totalZeros_7[bits >> 3];
582                 break;
583 
584             case 8:
585                 value = totalZeros_8[bits >> 3];
586                 break;
587 
588             case 9:
589                 value = totalZeros_9[bits >> 3];
590                 break;
591 
592             case 10:
593                 value = totalZeros_10[bits >> 4];
594                 break;
595 
596             case 11:
597                 value = totalZeros_11[bits >> 5];
598                 break;
599 
600             case 12:
601                 value = totalZeros_12[bits >> 5];
602                 break;
603 
604             case 13:
605                 value = totalZeros_13[bits >> 6];
606                 break;
607 
608             case 14:
609                 value = totalZeros_14[bits >> 7];
610                 break;
611 
612             default: /* case 15 */
613                 value = (bits >> 8) ? 0x11 : 0x01;
614                 break;
615         }
616     }
617     else
618     {
619         ASSERT(totalCoeff < 4);
620         bits >>= 6;
621         if (bits > 3)
622             value = 0x01;
623         else
624         {
625             if (totalCoeff == 3)
626                 value = 0x11;
627             else if (bits > 1)
628             {
629                 value = 0x12;
630             }
631             else if (totalCoeff == 2)
632                 value = 0x22;
633             else if (bits)
634                 value = 0x23;
635             else
636                 value = 0x33;
637         }
638     }
639 
640     return(value);
641 
642 }
643 
644 /*------------------------------------------------------------------------------
645 
646     Function: DecodeRunBefore
647 
648         Functional description:
649           Function to decode run_before information field from the stream
650 
651         Inputs:
652           u32 bits                  next 11 stream bits
653           u32 zerosLeft             number of zeros left for the current block
654 
655         Outputs:
656           u32  information field (4 bits value, 4 bits length)
657 
658 ------------------------------------------------------------------------------*/
659 
DecodeRunBefore(u32 bits,u32 zerosLeft)660 u32 DecodeRunBefore(u32 bits, u32 zerosLeft)
661 {
662 
663 /* Variables */
664 
665     u32 value = 0x0;
666 
667 /* Code */
668 
669     switch (zerosLeft)
670     {
671         case 1:
672             value = runBefore_1[bits>>10];
673             break;
674 
675         case 2:
676             value = runBefore_2[bits>>9];
677             break;
678 
679         case 3:
680             value = runBefore_3[bits>>9];
681             break;
682 
683         case 4:
684             value = runBefore_4[bits>>8];
685             break;
686 
687         case 5:
688             value = runBefore_5[bits>>8];
689             break;
690 
691         case 6:
692             value = runBefore_6[bits>>8];
693             break;
694 
695         default:
696             if (bits >= 0x100)
697                 value = ((7-(bits>>8))<<4)+0x3;
698             else if (bits >= 0x80)
699                 value = 0x74;
700             else if (bits >= 0x40)
701                 value = 0x85;
702             else if (bits >= 0x20)
703                 value = 0x96;
704             else if (bits >= 0x10)
705                 value = 0xa7;
706             else if (bits >= 0x8)
707                 value = 0xb8;
708             else if (bits >= 0x4)
709                 value = 0xc9;
710             else if (bits >= 0x2)
711                 value = 0xdA;
712             else if (bits)
713                 value = 0xeB;
714             if (INFO(value) > zerosLeft)
715                 value = 0;
716             break;
717     }
718 
719     return(value);
720 
721 }
722 
723 /*------------------------------------------------------------------------------
724 
725     Function: DecodeResidualBlockCavlc
726 
727         Functional description:
728           Function to decode one CAVLC coded block. This corresponds to
729           syntax elements residual_block_cavlc() in the standard.
730 
731         Inputs:
732           pStrmData             pointer to stream data structure
733           nc                    nC value
734           maxNumCoeff           maximum number of residual coefficients
735 
736         Outputs:
737           coeffLevel            stores decoded coefficient levels
738 
739         Returns:
740           numCoeffs             on bits [4,11] if successful
741           coeffMap              on bits [16,31] if successful, this is bit map
742                                 where each bit indicates if the corresponding
743                                 coefficient was zero (0) or non-zero (1)
744           HANTRO_NOK            end of stream or error in stream
745 
746 ------------------------------------------------------------------------------*/
747 
h264bsdDecodeResidualBlockCavlc(strmData_t * pStrmData,i32 * coeffLevel,i32 nc,u32 maxNumCoeff)748 u32 h264bsdDecodeResidualBlockCavlc(
749   strmData_t *pStrmData,
750   i32 *coeffLevel,
751   i32 nc,
752   u32 maxNumCoeff)
753 {
754 
755 /* Variables */
756 
757     u32 i, tmp, totalCoeff, trailingOnes, suffixLength, levelPrefix;
758     u32 levelSuffix, zerosLeft, bit;
759     i32 level[16];
760     u32 run[16];
761     /* stream "cache" */
762     u32 bufferValue;
763     u32 bufferBits;
764 
765 /* Code */
766 
767     ASSERT(pStrmData);
768     ASSERT(coeffLevel);
769     ASSERT(nc > -2);
770     ASSERT(maxNumCoeff == 4 || maxNumCoeff == 15 || maxNumCoeff == 16);
771     ASSERT(VLC_NOT_FOUND != END_OF_STREAM);
772 
773     /* assume that coeffLevel array has been "cleaned" by caller */
774 
775     BUFFER_INIT(bufferValue, bufferBits);
776 
777     /*lint -e774 disable lint warning on always false comparison */
778     BUFFER_SHOW(bufferValue, bufferBits, bit, 16);
779     /*lint +e774 */
780     tmp = DecodeCoeffToken(bit, (u32)nc);
781     if (!tmp)
782         return(HANTRO_NOK);
783     BUFFER_FLUSH(bufferValue, bufferBits, LENGTH_TC(tmp));
784 
785     totalCoeff = TOTAL_COEFF(tmp);
786     if (totalCoeff > maxNumCoeff)
787         return(HANTRO_NOK);
788     trailingOnes = TRAILING_ONES(tmp);
789 
790     if (totalCoeff != 0)
791     {
792         i = 0;
793         /* nonzero coefficients: +/- 1 */
794         if (trailingOnes)
795         {
796             BUFFER_GET(bufferValue, bufferBits, bit, trailingOnes);
797             tmp = 1 << (trailingOnes - 1);
798             for (; tmp; i++)
799             {
800                 level[i] = bit & tmp ? -1 : 1;
801                 tmp >>= 1;
802             }
803         }
804 
805         /* other levels */
806         if (totalCoeff > 10 && trailingOnes < 3)
807             suffixLength = 1;
808         else
809             suffixLength = 0;
810 
811         for (; i < totalCoeff; i++)
812         {
813             BUFFER_SHOW(bufferValue, bufferBits, bit, 16);
814             levelPrefix = DecodeLevelPrefix(bit);
815             if (levelPrefix == VLC_NOT_FOUND)
816                 return(HANTRO_NOK);
817             BUFFER_FLUSH(bufferValue, bufferBits, levelPrefix+1);
818 
819             if (levelPrefix < 14)
820                 tmp = suffixLength;
821             else if (levelPrefix == 14)
822             {
823                 tmp = suffixLength ? suffixLength : 4;
824             }
825             else
826             {
827                 /* setting suffixLength to 1 here corresponds to adding 15
828                  * to levelCode value if levelPrefix == 15 and
829                  * suffixLength == 0 */
830                 if (!suffixLength)
831                     suffixLength = 1;
832                 tmp = 12;
833             }
834 
835             if (suffixLength)
836                 levelPrefix <<= suffixLength;
837 
838             if (tmp)
839             {
840                 BUFFER_GET(bufferValue, bufferBits, levelSuffix, tmp);
841                 levelPrefix += levelSuffix;
842             }
843 
844             tmp = levelPrefix;
845 
846             if (i == trailingOnes && trailingOnes < 3)
847                 tmp += 2;
848 
849             level[i] = (tmp+2)>>1;
850 
851             if (suffixLength == 0)
852                 suffixLength = 1;
853 
854             if ((level[i] > (3 << (suffixLength - 1))) && suffixLength < 6)
855                 suffixLength++;
856 
857             if (tmp & 0x1)
858                 level[i] = -level[i];
859         }
860 
861         /* zero runs */
862         if (totalCoeff < maxNumCoeff)
863         {
864             BUFFER_SHOW(bufferValue, bufferBits, bit,9);
865             zerosLeft = DecodeTotalZeros(bit, totalCoeff,
866                                         (u32)(maxNumCoeff == 4));
867             if (!zerosLeft)
868                 return(HANTRO_NOK);
869             BUFFER_FLUSH(bufferValue, bufferBits, LENGTH(zerosLeft));
870             zerosLeft = INFO(zerosLeft);
871         }
872         else
873             zerosLeft = 0;
874 
875         for (i = 0; i < totalCoeff - 1; i++)
876         {
877             if (zerosLeft > 0)
878             {
879                 BUFFER_SHOW(bufferValue, bufferBits, bit,11);
880                 tmp = DecodeRunBefore(bit, zerosLeft);
881                 if (!tmp)
882                     return(HANTRO_NOK);
883                 BUFFER_FLUSH(bufferValue, bufferBits, LENGTH(tmp));
884                 run[i] = INFO(tmp);
885                 zerosLeft -= run[i]++;
886             }
887             else
888             {
889                 run[i] = 1;
890             }
891         }
892 
893         /* combining level and run, levelSuffix variable used to hold coeffMap,
894          * i.e. bit map indicating which coefficients had non-zero value. */
895 
896         /*lint -esym(771,level,run) level and run are always initialized */
897         tmp = zerosLeft;
898         coeffLevel[tmp] = level[totalCoeff-1];
899         levelSuffix = 1 << tmp;
900         for (i = totalCoeff-1; i--;)
901         {
902             tmp += run[i];
903             levelSuffix |= 1 << tmp;
904             coeffLevel[tmp] = level[i];
905         }
906 
907     }
908     else
909         levelSuffix = 0;
910 
911     if (h264bsdFlushBits(pStrmData, 32-bufferBits) != HANTRO_OK)
912         return(HANTRO_NOK);
913 
914     return((totalCoeff << 4) | (levelSuffix << 16));
915 }
916 
917