1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18#include "avcdec_lib.h"
19#include "avcdec_bitstream.h"
20
21//#define PV_ARM_V5
22#ifdef PV_ARM_V5
23#define PV_CLZ(A,B) __asm{CLZ (A),(B)}  \
24    A -= 16;
25#else
26#define PV_CLZ(A,B) while (((B) & 0x8000) == 0) {(B) <<=1; A++;}
27#endif
28
29
30#define PV_NO_CLZ
31
32#ifndef PV_NO_CLZ
33typedef struct tagVLCNumCoeffTrail
34{
35    int trailing;
36    int total_coeff;
37    int length;
38} VLCNumCoeffTrail;
39
40typedef struct tagShiftOffset
41{
42    int shift;
43    int offset;
44} ShiftOffset;
45
46const VLCNumCoeffTrail NumCoeffTrailOnes[3][67] =
47{
48    {{0, 0, 1}, {1, 1, 2}, {2, 2, 3}, {1, 2, 6}, {0, 1, 6}, {3, 3, 5}, {3, 3, 5}, {3, 5, 7},
49        {2, 3, 7}, {3, 4, 6}, {3, 4, 6}, {3, 6, 8}, {2, 4, 8}, {1, 3, 8}, {0, 2, 8}, {3, 7, 9},
50        {2, 5, 9}, {1, 4, 9}, {0, 3, 9}, {3, 8, 10}, {2, 6, 10}, {1, 5, 10}, {0, 4, 10}, {3, 9, 11},
51        {2, 7, 11}, {1, 6, 11}, {0, 5, 11}, {0, 8, 13}, {2, 9, 13}, {1, 8, 13}, {0, 7, 13}, {3, 10, 13},
52        {2, 8, 13}, {1, 7, 13}, {0, 6, 13}, {3, 12, 14}, {2, 11, 14}, {1, 10, 14}, {0, 10, 14}, {3, 11, 14},
53        {2, 10, 14}, {1, 9, 14}, {0, 9, 14}, {3, 14, 15}, {2, 13, 15}, {1, 12, 15}, {0, 12, 15}, {3, 13, 15},
54        {2, 12, 15}, {1, 11, 15}, {0, 11, 15}, {3, 16, 16}, {2, 15, 16}, {1, 15, 16}, {0, 14, 16}, {3, 15, 16},
55        {2, 14, 16}, {1, 14, 16}, {0, 13, 16}, {0, 16, 16}, {2, 16, 16}, {1, 16, 16}, {0, 15, 16}, {1, 13, 15},
56        { -1, -1, -1}, { -1, -1, -1}, { -1, -1, -1}},
57
58    {{1, 1, 2}, {0, 0, 2}, {3, 4, 4}, {3, 3, 4}, {2, 2, 3}, {2, 2, 3}, {3, 6, 6}, {2, 3, 6},
59        {1, 3, 6}, {0, 1, 6}, {3, 5, 5}, {3, 5, 5}, {1, 2, 5}, {1, 2, 5}, {3, 7, 6}, {2, 4, 6},
60        {1, 4, 6}, {0, 2, 6}, {3, 8, 7}, {2, 5, 7}, {1, 5, 7}, {0, 3, 7}, {0, 5, 8}, {2, 6, 8},
61        {1, 6, 8}, {0, 4, 8}, {3, 9, 9}, {2, 7, 9}, {1, 7, 9}, {0, 6, 9}, {3, 11, 11}, {2, 9, 11},
62        {1, 9, 11}, {0, 8, 11}, {3, 10, 11}, {2, 8, 11}, {1, 8, 11}, {0, 7, 11}, {0, 11, 12}, {2, 11, 12},
63        {1, 11, 12}, {0, 10, 12}, {3, 12, 12}, {2, 10, 12}, {1, 10, 12}, {0, 9, 12}, {3, 14, 13}, {2, 13, 13},
64        {1, 13, 13}, {0, 13, 13}, {3, 13, 13}, {2, 12, 13}, {1, 12, 13}, {0, 12, 13}, {1, 15, 14}, {0, 15, 14},
65        {2, 15, 14}, {1, 14, 14}, {2, 14, 13}, {2, 14, 13}, {0, 14, 13}, {0, 14, 13}, {3, 16, 14}, {2, 16, 14},
66        {1, 16, 14}, {0, 16, 14}, {3, 15, 13}},
67
68    {{3, 7, 4}, {3, 6, 4}, {3, 5, 4}, {3, 4, 4}, {3, 3, 4}, {2, 2, 4}, {1, 1, 4}, {0, 0, 4},
69        {1, 5, 5}, {2, 5, 5}, {1, 4, 5}, {2, 4, 5}, {1, 3, 5}, {3, 8, 5}, {2, 3, 5}, {1, 2, 5},
70        {0, 3, 6}, {2, 7, 6}, {1, 7, 6}, {0, 2, 6}, {3, 9, 6}, {2, 6, 6}, {1, 6, 6}, {0, 1, 6},
71        {0, 7, 7}, {0, 6, 7}, {2, 9, 7}, {0, 5, 7}, {3, 10, 7}, {2, 8, 7}, {1, 8, 7}, {0, 4, 7},
72        {3, 12, 8}, {2, 11, 8}, {1, 10, 8}, {0, 9, 8}, {3, 11, 8}, {2, 10, 8}, {1, 9, 8}, {0, 8, 8},
73        {0, 12, 9}, {2, 13, 9}, {1, 12, 9}, {0, 11, 9}, {3, 13, 9}, {2, 12, 9}, {1, 11, 9}, {0, 10, 9},
74        {1, 15, 10}, {0, 14, 10}, {3, 14, 10}, {2, 14, 10}, {1, 14, 10}, {0, 13, 10}, {1, 13, 9}, {1, 13, 9},
75        {1, 16, 10}, {0, 15, 10}, {3, 15, 10}, {2, 15, 10}, {3, 16, 10}, {2, 16, 10}, {0, 16, 10}, { -1, -1, -1},
76        { -1, -1, -1}, { -1, -1, -1}, { -1, -1, -1}}
77};
78
79
80const ShiftOffset NumCoeffTrailOnes_indx[3][15] =
81{
82    {{15, -1}, {14, 0}, {13, 1}, {10, -1}, {9, 3}, {8, 7}, {7, 11}, {6, 15},
83        {5, 19}, {3, 19}, {2, 27}, {1, 35}, {0, 43}, {0, 55}, {1, 62}},
84
85    {{14, -2}, {12, -2}, {10, -2}, {10, 10}, {9, 14}, {8, 18}, {7, 22}, {5, 22},
86        {4, 30}, {3, 38}, {2, 46}, {2, 58}, {3, 65}, {16, 0}, {16, 0}},
87
88    {{12, -8}, {11, 0}, {10, 8}, {9, 16}, {8, 24}, {7, 32}, {6, 40}, {6, 52},
89        {6, 58}, {6, 61}, {16, 0}, {16, 0}, {16, 0}, {16, 0}, {16, 0}}
90};
91
92const static int nC_table[8] = {0, 0, 1, 1, 2, 2, 2, 2};
93
94#endif
95/**
96See algorithm in subclause 9.1, Table 9-1, Table 9-2. */
97AVCDec_Status ue_v(AVCDecBitstream *bitstream, uint *codeNum)
98{
99    uint temp, tmp_cnt;
100    int leading_zeros = 0;
101    BitstreamShowBits(bitstream, 16, &temp);
102    tmp_cnt = temp  | 0x1;
103
104    PV_CLZ(leading_zeros, tmp_cnt)
105
106    if (leading_zeros < 8)
107    {
108        *codeNum = (temp >> (15 - (leading_zeros << 1))) - 1;
109        BitstreamFlushBits(bitstream, (leading_zeros << 1) + 1);
110    }
111    else
112    {
113        BitstreamReadBits(bitstream, (leading_zeros << 1) + 1, &temp);
114        *codeNum = temp - 1;
115    }
116
117    return AVCDEC_SUCCESS;
118}
119
120/**
121See subclause 9.1.1, Table 9-3 */
122AVCDec_Status  se_v(AVCDecBitstream *bitstream, int *value)
123{
124    uint temp, tmp_cnt;
125    int leading_zeros = 0;
126    BitstreamShowBits(bitstream, 16, &temp);
127    tmp_cnt = temp | 0x1;
128
129    PV_CLZ(leading_zeros, tmp_cnt)
130
131    if (leading_zeros < 8)
132    {
133        temp >>= (15 - (leading_zeros << 1));
134        BitstreamFlushBits(bitstream, (leading_zeros << 1) + 1);
135    }
136    else
137    {
138        BitstreamReadBits(bitstream, (leading_zeros << 1) + 1, &temp);
139    }
140
141    *value = temp >> 1;
142
143    if (temp & 0x01)                          // lsb is signed bit
144        *value = -(*value);
145
146//  leading_zeros = temp >> 1;
147//  *value = leading_zeros - (leading_zeros*2*(temp&1));
148
149    return AVCDEC_SUCCESS;
150}
151
152AVCDec_Status  se_v32bit(AVCDecBitstream *bitstream, int32 *value)
153{
154    int leadingZeros;
155    uint32 infobits;
156    uint32 codeNum;
157
158    if (AVCDEC_SUCCESS != GetEGBitstring32bit(bitstream, &leadingZeros, &infobits))
159        return AVCDEC_FAIL;
160
161    codeNum = (1 << leadingZeros) - 1 + infobits;
162
163    *value = (codeNum + 1) / 2;
164
165    if ((codeNum & 0x01) == 0)                        // lsb is signed bit
166        *value = -(*value);
167
168    return AVCDEC_SUCCESS;
169}
170
171
172AVCDec_Status te_v(AVCDecBitstream *bitstream, uint *value, uint range)
173{
174    if (range > 1)
175    {
176        ue_v(bitstream, value);
177    }
178    else
179    {
180        BitstreamRead1Bit(bitstream, value);
181        *value = 1 - (*value);
182    }
183    return AVCDEC_SUCCESS;
184}
185
186
187
188/* This function is only used for syntax with range from -2^31 to 2^31-1 */
189/* only a few of them in the SPS and PPS */
190AVCDec_Status GetEGBitstring32bit(AVCDecBitstream *bitstream, int *leadingZeros, uint32 *infobits)
191{
192    int bit_value;
193    uint info_temp;
194
195    *leadingZeros = 0;
196
197    BitstreamRead1Bit(bitstream, (uint*)&bit_value);
198
199    while (!bit_value)
200    {
201        (*leadingZeros)++;
202        BitstreamRead1Bit(bitstream, (uint*)&bit_value);
203    }
204
205    if (*leadingZeros > 0)
206    {
207        if (sizeof(uint) == 4)  /* 32 bit machine */
208        {
209            BitstreamReadBits(bitstream, *leadingZeros, (uint*)&info_temp);
210            *infobits = (uint32)info_temp;
211        }
212        else if (sizeof(uint) == 2) /* 16 bit machine */
213        {
214            *infobits = 0;
215            if (*leadingZeros > 16)
216            {
217                BitstreamReadBits(bitstream, 16, (uint*)&info_temp);
218                (*leadingZeros) -= 16;
219                *infobits = ((uint32)info_temp) << (*leadingZeros);
220            }
221
222            BitstreamReadBits(bitstream, *leadingZeros, (uint*)&info_temp);
223            *infobits |= (uint32)info_temp ;
224        }
225    }
226    else
227        *infobits = 0;
228
229    return AVCDEC_SUCCESS;
230}
231
232/* see Table 9-4 assignment of codeNum to values of coded_block_pattern. */
233const static uint8 MapCBP[48][2] =
234{
235    {47, 0}, {31, 16}, {15, 1}, { 0, 2}, {23, 4}, {27, 8}, {29, 32}, {30, 3}, { 7, 5}, {11, 10}, {13, 12}, {14, 15},
236    {39, 47}, {43, 7}, {45, 11}, {46, 13}, {16, 14}, { 3, 6}, { 5, 9}, {10, 31}, {12, 35}, {19, 37}, {21, 42}, {26, 44},
237    {28, 33}, {35, 34}, {37, 36}, {42, 40}, {44, 39}, { 1, 43}, { 2, 45}, { 4, 46}, { 8, 17}, {17, 18}, {18, 20}, {20, 24},
238    {24, 19}, { 6, 21}, { 9, 26}, {22, 28}, {25, 23}, {32, 27}, {33, 29}, {34, 30}, {36, 22}, {40, 25}, {38, 38}, {41, 41},
239};
240
241AVCDec_Status DecodeCBP(AVCMacroblock *currMB, AVCDecBitstream *stream)
242{
243    uint codeNum;
244    uint coded_block_pattern;
245
246    ue_v(stream, &codeNum);
247
248    if (codeNum > 47)
249    {
250        return AVCDEC_FAIL;
251    }
252
253    /* can get rid of the if _OPTIMIZE */
254    if (currMB->mbMode == AVC_I4)
255    {
256        coded_block_pattern = MapCBP[codeNum][0];
257    }
258    else
259    {
260        coded_block_pattern = MapCBP[codeNum][1];
261    }
262
263//  currMB->cbpL = coded_block_pattern&0xF;  /* modulo 16 */
264//  currMB->cbpC = coded_block_pattern>>4;   /* divide 16 */
265    currMB->CBP = coded_block_pattern;
266
267    return AVCDEC_SUCCESS;
268}
269
270
271/* TO BE OPTIMIZED !!!!! */
272AVCDec_Status ce_TotalCoeffTrailingOnes(AVCDecBitstream *stream, int *TrailingOnes, int *TotalCoeff, int nC)
273{
274#ifdef PV_NO_CLZ
275    const static uint8 TotCofNTrail1[75][3] = {{0, 0, 16}/*error */, {0, 0, 16}/*error */, {1, 13, 15}, {1, 13, 15}, {0, 16, 16}, {2, 16, 16}, {1, 16, 16}, {0, 15, 16},
276        {3, 16, 16}, {2, 15, 16}, {1, 15, 16}, {0, 14, 16}, {3, 15, 16}, {2, 14, 16}, {1, 14, 16}, {0, 13, 16},
277        {3, 14, 15}, {2, 13, 15}, {1, 12, 15}, {0, 12, 15}, {3, 13, 15}, {2, 12, 15}, {1, 11, 15}, {0, 11, 15},
278        {3, 12, 14}, {2, 11, 14}, {1, 10, 14}, {0, 10, 14}, {3, 11, 14}, {2, 10, 14}, {1, 9, 14}, {0, 9, 14},
279        {0, 8, 13}, {2, 9, 13}, {1, 8, 13}, {0, 7, 13}, {3, 10, 13}, {2, 8, 13}, {1, 7, 13}, {0, 6, 13},
280        {3, 9, 11}, {2, 7, 11}, {1, 6, 11}, {0, 5, 11}, {3, 8, 10},
281        {2, 6, 10}, {1, 5, 10}, {0, 4, 10}, {3, 7, 9}, {2, 5, 9}, {1, 4, 9}, {0, 3, 9}, {3, 6, 8},
282        {2, 4, 8}, {1, 3, 8}, {0, 2, 8}, {3, 5, 7}, {2, 3, 7}, {3, 4, 6}, {3, 4, 6}, {1, 2, 6},
283        {1, 2, 6}, {0, 1, 6}, {0, 1, 6}, {3, 3, 5}, {3, 3, 5}, {3, 3, 5}, {3, 3, 5}, {2, 2, 3},
284        {1, 1, 2}, {1, 1, 2}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}
285    };
286
287    const static uint8 TotCofNTrail2[84][3] = {{0, 0, 14 /* error */}, {0, 0, 14/*error */}, {3, 15, 13}, {3, 15, 13}, {3, 16, 14}, {2, 16, 14}, {1, 16, 14}, {0, 16, 14},
288        {1, 15, 14}, {0, 15, 14}, {2, 15, 14}, {1, 14, 14}, {2, 14, 13}, {2, 14, 13}, {0, 14, 13}, {0, 14, 13},
289        {3, 14, 13}, {2, 13, 13}, {1, 13, 13}, {0, 13, 13}, {3, 13, 13}, {2, 12, 13}, {1, 12, 13}, {0, 12, 13},
290        {0, 11, 12}, {2, 11, 12}, {1, 11, 12}, {0, 10, 12}, {3, 12, 12}, {2, 10, 12}, {1, 10, 12}, {0, 9, 12},
291        {3, 11, 11}, {2, 9, 11}, {1, 9, 11}, {0, 8, 11}, {3, 10, 11}, {2, 8, 11}, {1, 8, 11}, {0, 7, 11},
292        {3, 9, 9}, {2, 7, 9}, {1, 7, 9}, {0, 6, 9}, {0, 5, 8}, {0, 5, 8}, {2, 6, 8}, {2, 6, 8},
293        {1, 6, 8}, {1, 6, 8}, {0, 4, 8}, {0, 4, 8}, {3, 8, 7}, {2, 5, 7}, {1, 5, 7}, {0, 3, 7},
294        {3, 7, 6}, {3, 7, 6}, {2, 4, 6}, {2, 4, 6}, {1, 4, 6}, {1, 4, 6}, {0, 2, 6}, {0, 2, 6},
295        {3, 6, 6}, {2, 3, 6}, {1, 3, 6}, {0, 1, 6}, {3, 5, 5}, {3, 5, 5}, {1, 2, 5}, {1, 2, 5},
296        {3, 4, 4}, {3, 3, 4}, {2, 2, 3}, {2, 2, 3}, {1, 1, 2}, {1, 1, 2}, {1, 1, 2}, {1, 1, 2},
297        {0, 0, 2}, {0, 0, 2}, {0, 0, 2}, {0, 0, 2}
298    };
299
300    const static uint8 TotCofNTrail3[64][3] = {{0, 0, 10/*error*/}, {0, 16, 10}, {3, 16, 10}, {2, 16, 10}, {1, 16, 10}, {0, 15, 10}, {3, 15, 10},
301        {2, 15, 10}, {1, 15, 10}, {0, 14, 10}, {3, 14, 10}, {2, 14, 10}, {1, 14, 10}, {0, 13, 10}, {1, 13, 9},
302        {1, 13, 9}, {0, 12, 9}, {2, 13, 9}, {1, 12, 9}, {0, 11, 9}, {3, 13, 9}, {2, 12, 9}, {1, 11, 9},
303        {0, 10, 9}, {3, 12, 8}, {2, 11, 8}, {1, 10, 8}, {0, 9, 8}, {3, 11, 8}, {2, 10, 8}, {1, 9, 8},
304        {0, 8, 8}, {0, 7, 7}, {0, 6, 7}, {2, 9, 7}, {0, 5, 7}, {3, 10, 7}, {2, 8, 7}, {1, 8, 7},
305        {0, 4, 7}, {0, 3, 6}, {2, 7, 6}, {1, 7, 6}, {0, 2, 6}, {3, 9, 6}, {2, 6, 6}, {1, 6, 6},
306        {0, 1, 6}, {1, 5, 5}, {2, 5, 5}, {1, 4, 5}, {2, 4, 5}, {1, 3, 5}, {3, 8, 5}, {2, 3, 5},
307        {1, 2, 5}, {3, 7, 4}, {3, 6, 4}, {3, 5, 4}, {3, 4, 4}, {3, 3, 4}, {2, 2, 4}, {1, 1, 4},
308        {0, 0, 4}
309    };
310#endif
311    uint code;
312
313#ifdef PV_NO_CLZ
314    uint8 *pcode;
315    if (nC < 2)
316    {
317        BitstreamShowBits(stream, 16, &code);
318
319        if (code >= 8192)
320        {
321            pcode = (uint8*) & (TotCofNTrail1[(code>>13)+65+2][0]);
322        }
323        else if (code >= 2048)
324        {
325            pcode = (uint8*) & (TotCofNTrail1[(code>>9)+50+2][0]);
326        }
327        else if (code >= 1024)
328        {
329            pcode = (uint8*) & (TotCofNTrail1[(code>>8)+46+2][0]);
330        }
331        else if (code >= 512)
332        {
333            pcode = (uint8*) & (TotCofNTrail1[(code>>7)+42+2][0]);
334        }
335        else if (code >= 256)
336        {
337            pcode = (uint8*) & (TotCofNTrail1[(code>>6)+38+2][0]);
338        }
339        else if (code >= 128)
340        {
341            pcode = (uint8*) & (TotCofNTrail1[(code>>5)+34+2][0]);
342        }
343        else if (code >= 64)
344        {
345            pcode = (uint8*) & (TotCofNTrail1[(code>>3)+22+2][0]);
346        }
347        else if (code >= 32)
348        {
349            pcode = (uint8*) & (TotCofNTrail1[(code>>2)+14+2][0]);
350        }
351        else if (code >= 16)
352        {
353            pcode = (uint8*) & (TotCofNTrail1[(code>>1)+6+2][0]);
354        }
355        else
356        {
357            pcode = (uint8*) & (TotCofNTrail1[(code-2)+2][0]);
358        }
359
360        *TrailingOnes = pcode[0];
361        *TotalCoeff = pcode[1];
362
363        BitstreamFlushBits(stream, pcode[2]);
364    }
365    else if (nC < 4)
366    {
367        BitstreamShowBits(stream, 14, &code);
368
369        if (code >= 4096)
370        {
371            pcode = (uint8*) & (TotCofNTrail2[(code>>10)+66+2][0]);
372        }
373        else if (code >= 2048)
374        {
375            pcode = (uint8*) & (TotCofNTrail2[(code>>8)+54+2][0]);
376        }
377        else if (code >= 512)
378        {
379            pcode = (uint8*) & (TotCofNTrail2[(code>>7)+46+2][0]);
380        }
381        else if (code >= 128)
382        {
383            pcode = (uint8*) & (TotCofNTrail2[(code>>5)+34+2][0]);
384        }
385        else if (code >= 64)
386        {
387            pcode = (uint8*) & (TotCofNTrail2[(code>>3)+22+2][0]);
388        }
389        else if (code >= 32)
390        {
391            pcode = (uint8*) & (TotCofNTrail2[(code>>2)+14+2][0]);
392        }
393        else if (code >= 16)
394        {
395            pcode = (uint8*) & (TotCofNTrail2[(code>>1)+6+2][0]);
396        }
397        else
398        {
399            pcode = (uint8*) & (TotCofNTrail2[code-2+2][0]);
400        }
401        *TrailingOnes = pcode[0];
402        *TotalCoeff = pcode[1];
403
404        BitstreamFlushBits(stream, pcode[2]);
405    }
406    else if (nC < 8)
407    {
408        BitstreamShowBits(stream, 10, &code);
409
410        if (code >= 512)
411        {
412            pcode = (uint8*) & (TotCofNTrail3[(code>>6)+47+1][0]);
413        }
414        else if (code >= 256)
415        {
416            pcode = (uint8*) & (TotCofNTrail3[(code>>5)+39+1][0]);
417        }
418        else if (code >= 128)
419        {
420            pcode = (uint8*) & (TotCofNTrail3[(code>>4)+31+1][0]);
421        }
422        else if (code >= 64)
423        {
424            pcode = (uint8*) & (TotCofNTrail3[(code>>3)+23+1][0]);
425        }
426        else if (code >= 32)
427        {
428            pcode = (uint8*) & (TotCofNTrail3[(code>>2)+15+1][0]);
429        }
430        else if (code >= 16)
431        {
432            pcode = (uint8*) & (TotCofNTrail3[(code>>1)+7+1][0]);
433        }
434        else
435        {
436            pcode = (uint8*) & (TotCofNTrail3[code-1+1][0]);
437        }
438        *TrailingOnes = pcode[0];
439        *TotalCoeff = pcode[1];
440
441        BitstreamFlushBits(stream, pcode[2]);
442    }
443    else
444    {
445        /* read 6 bit FLC */
446        BitstreamReadBits(stream, 6, &code);
447
448
449        *TrailingOnes = code & 3;
450        *TotalCoeff = (code >> 2) + 1;
451
452        if (*TotalCoeff > 16)
453        {
454            *TotalCoeff = 16;  // _ERROR
455        }
456
457        if (code == 3)
458        {
459            *TrailingOnes = 0;
460            (*TotalCoeff)--;
461        }
462    }
463#else
464    const VLCNumCoeffTrail *ptr;
465    const ShiftOffset *ptr_indx;
466    uint temp, leading_zeros = 0;
467
468    if (nC < 8)
469    {
470
471        BitstreamShowBits(stream, 16, &code);
472        temp = code | 1;
473
474        PV_CLZ(leading_zeros, temp)
475
476        temp = nC_table[nC];
477        ptr_indx = &NumCoeffTrailOnes_indx[temp][leading_zeros];
478        ptr = &NumCoeffTrailOnes[temp][(code >> ptr_indx->shift) + ptr_indx->offset];
479        *TrailingOnes = ptr->trailing;
480        *TotalCoeff = ptr->total_coeff;
481        BitstreamFlushBits(stream, ptr->length);
482    }
483    else
484    {
485        /* read 6 bit FLC */
486        BitstreamReadBits(stream, 6, &code);
487
488
489        *TrailingOnes = code & 3;
490        *TotalCoeff = (code >> 2) + 1;
491
492        if (*TotalCoeff > 16)
493        {
494            *TotalCoeff = 16;  // _ERROR
495        }
496
497        if (code == 3)
498        {
499            *TrailingOnes = 0;
500            (*TotalCoeff)--;
501        }
502    }
503#endif
504    return AVCDEC_SUCCESS;
505}
506
507/* TO BE OPTIMIZED !!!!! */
508AVCDec_Status ce_TotalCoeffTrailingOnesChromaDC(AVCDecBitstream *stream, int *TrailingOnes, int *TotalCoeff)
509{
510    AVCDec_Status status;
511
512    const static uint8 TotCofNTrail5[21][3] =
513    {
514        {3, 4, 7}, {3, 4, 7}, {2, 4, 8}, {1, 4, 8}, {2, 3, 7}, {2, 3, 7}, {1, 3, 7},
515        {1, 3, 7}, {0, 4, 6}, {0, 3, 6}, {0, 2, 6}, {3, 3, 6}, {1, 2, 6}, {0, 1, 6},
516        {2, 2, 3}, {0, 0, 2}, {0, 0, 2}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}
517    };
518
519    uint code;
520    uint8 *pcode;
521
522    status = BitstreamShowBits(stream, 8, &code);
523
524    if (code >= 32)
525    {
526        pcode = (uint8*) & (TotCofNTrail5[(code>>5)+13][0]);
527    }
528    else if (code >= 8)
529    {
530        pcode = (uint8*) & (TotCofNTrail5[(code>>2)+6][0]);
531    }
532    else
533    {
534        pcode = (uint8*) & (TotCofNTrail5[code][0]);
535    }
536
537    *TrailingOnes = pcode[0];
538    *TotalCoeff = pcode[1];
539
540    BitstreamFlushBits(stream, pcode[2]);
541
542    return status;
543}
544
545/* see Table 9-6 */
546AVCDec_Status ce_LevelPrefix(AVCDecBitstream *stream, uint *code)
547{
548    uint temp;
549    uint leading_zeros = 0;
550    BitstreamShowBits(stream, 16, &temp);
551    temp |= 1 ;
552
553    PV_CLZ(leading_zeros, temp)
554
555    BitstreamFlushBits(stream, leading_zeros + 1);
556    *code = leading_zeros;
557    return AVCDEC_SUCCESS;
558}
559
560/* see Table 9-7 and 9-8 */
561AVCDec_Status ce_TotalZeros(AVCDecBitstream *stream, int *code, int TotalCoeff)
562{
563    const static uint8 TotZero1[28][2] = {{15, 9}, {14, 9}, {13, 9}, {12, 8},
564        {12, 8}, {11, 8}, {11, 8}, {10, 7}, {9, 7}, {8, 6}, {8, 6}, {7, 6}, {7, 6}, {6, 5}, {6, 5},
565        {6, 5}, {6, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {4, 4}, {3, 4},
566        {2, 3}, {2, 3}, {1, 3}, {1, 3}, {0, 1}
567    };
568
569    const static uint8 TotZero2n3[2][18][2] = {{{14, 6}, {13, 6}, {12, 6}, {11, 6},
570            {10, 5}, {10, 5}, {9, 5}, {9, 5}, {8, 4}, {7, 4}, {6, 4}, {5, 4}, {4, 3}, {4, 3},
571            {3, 3}, {2, 3}, {1, 3}, {0, 3}},
572
573        /*const static uint8 TotZero3[18][2]=*/{{13, 6}, {11, 6}, {12, 5}, {12, 5}, {10, 5},
574            {10, 5}, {9, 5}, {9, 5}, {8, 4}, {5, 4}, {4, 4}, {0, 4}, {7, 3}, {7, 3}, {6, 3}, {3, 3},
575            {2, 3}, {1, 3}}
576    };
577
578    const static uint8 TotZero4[17][2] = {{12, 5}, {11, 5}, {10, 5}, {0, 5}, {9, 4},
579        {9, 4}, {7, 4}, {7, 4}, {3, 4}, {3, 4}, {2, 4}, {2, 4}, {8, 3}, {6, 3}, {5, 3}, {4, 3}, {1, 3}
580    };
581
582    const static uint8 TotZero5[13][2] = {{11, 5}, {9, 5}, {10, 4}, {8, 4}, {2, 4},
583        {1, 4}, {0, 4}, {7, 3}, {7, 3}, {6, 3}, {5, 3}, {4, 3}, {3, 3}
584    };
585
586    const static uint8 TotZero6to10[5][15][2] = {{{10, 6}, {0, 6}, {1, 5}, {1, 5}, {8, 4},
587            {8, 4}, {8, 4}, {8, 4}, {9, 3}, {7, 3}, {6, 3}, {5, 3}, {4, 3}, {3, 3}, {2, 3}},
588
589        /*const static uint8 TotZero7[15][2]=*/{{9, 6}, {0, 6}, {1, 5}, {1, 5}, {7, 4},
590            {7, 4}, {7, 4}, {7, 4}, {8, 3}, {6, 3}, {4, 3}, {3, 3}, {2, 3}, {5, 2}, {5, 2}},
591
592        /*const static uint8 TotZero8[15][2]=*/{{8, 6}, {0, 6}, {2, 5}, {2, 5}, {1, 4},
593            {1, 4}, {1, 4}, {1, 4}, {7, 3}, {6, 3}, {3, 3}, {5, 2}, {5, 2}, {4, 2}, {4, 2}},
594
595        /*const static uint8 TotZero9[15][2]=*/{{1, 6}, {0, 6}, {7, 5}, {7, 5}, {2, 4},
596            {2, 4}, {2, 4}, {2, 4}, {5, 3}, {6, 2}, {6, 2}, {4, 2}, {4, 2}, {3, 2}, {3, 2}},
597
598        /*const static uint8 TotZero10[11][2]=*/{{1, 5}, {0, 5}, {6, 4}, {6, 4}, {2, 3},
599            {2, 3}, {2, 3}, {2, 3}, {5, 2}, {4, 2}, {3, 2}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}
600    };
601
602    const static uint8 TotZero11[7][2] = {{0, 4}, {1, 4}, {2, 3}, {2, 3}, {3, 3}, {5, 3}, {4, 1}};
603
604    const static uint8 TotZero12to15[4][5][2] =
605    {
606        {{3, 1}, {2, 2}, {4, 3}, {1, 4}, {0, 4}},
607        {{2, 1}, {3, 2}, {1, 3}, {0, 3}, {0, 0}},
608        {{2, 1}, {1, 2}, {0, 2}, {0, 0}, {0, 0}},
609        {{1, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}}
610    };
611
612    uint temp, mask;
613    int indx;
614    uint8 *pcode;
615
616    if (TotalCoeff == 1)
617    {
618        BitstreamShowBits(stream, 9, &temp);
619
620        if (temp >= 256)
621        {
622            pcode = (uint8*) & (TotZero1[27][0]);
623        }
624        else if (temp >= 64)
625        {
626            pcode = (uint8*) & (TotZero1[(temp>>5)+19][0]);
627        }
628        else if (temp >= 8)
629        {
630            pcode = (uint8*) & (TotZero1[(temp>>2)+5][0]);
631        }
632        else
633        {
634            pcode = (uint8*) & (TotZero1[temp-1][0]);
635        }
636
637    }
638    else if (TotalCoeff == 2 || TotalCoeff == 3)
639    {
640        BitstreamShowBits(stream, 6, &temp);
641
642        if (temp >= 32)
643        {
644            pcode = (uint8*) & (TotZero2n3[TotalCoeff-2][(temp>>3)+10][0]);
645        }
646        else if (temp >= 8)
647        {
648            pcode = (uint8*) & (TotZero2n3[TotalCoeff-2][(temp>>2)+6][0]);
649        }
650        else
651        {
652            pcode = (uint8*) & (TotZero2n3[TotalCoeff-2][temp][0]);
653        }
654    }
655    else if (TotalCoeff == 4)
656    {
657        BitstreamShowBits(stream, 5, &temp);
658
659        if (temp >= 12)
660        {
661            pcode = (uint8*) & (TotZero4[(temp>>2)+9][0]);
662        }
663        else
664        {
665            pcode = (uint8*) & (TotZero4[temp][0]);
666        }
667    }
668    else if (TotalCoeff == 5)
669    {
670        BitstreamShowBits(stream, 5, &temp);
671
672        if (temp >= 16)
673        {
674            pcode = (uint8*) & (TotZero5[(temp>>2)+5][0]);
675        }
676        else if (temp >= 2)
677        {
678            pcode = (uint8*) & (TotZero5[(temp>>1)+1][0]);
679        }
680        else
681        {
682            pcode = (uint8*) & (TotZero5[temp][0]);
683        }
684    }
685    else if (TotalCoeff >= 6 && TotalCoeff <= 10)
686    {
687        if (TotalCoeff == 10)
688        {
689            BitstreamShowBits(stream, 5, &temp);
690        }
691        else
692        {
693            BitstreamShowBits(stream, 6, &temp);
694        }
695
696
697        if (temp >= 8)
698        {
699            pcode = (uint8*) & (TotZero6to10[TotalCoeff-6][(temp>>3)+7][0]);
700        }
701        else
702        {
703            pcode = (uint8*) & (TotZero6to10[TotalCoeff-6][temp][0]);
704        }
705    }
706    else if (TotalCoeff == 11)
707    {
708        BitstreamShowBits(stream, 4, &temp);
709
710
711        if (temp >= 8)
712        {
713            pcode = (uint8*) & (TotZero11[6][0]);
714        }
715        else if (temp >= 4)
716        {
717            pcode = (uint8*) & (TotZero11[(temp>>1)+2][0]);
718        }
719        else
720        {
721            pcode = (uint8*) & (TotZero11[temp][0]);
722        }
723    }
724    else
725    {
726        BitstreamShowBits(stream, (16 - TotalCoeff), &temp);
727        mask = 1 << (15 - TotalCoeff);
728        indx = 0;
729        while ((temp&mask) == 0 && indx < (16 - TotalCoeff)) /* search location of 1 bit */
730        {
731            mask >>= 1;
732            indx++;
733        }
734
735        pcode = (uint8*) & (TotZero12to15[TotalCoeff-12][indx]);
736    }
737
738    *code = pcode[0];
739    BitstreamFlushBits(stream, pcode[1]);
740
741    return AVCDEC_SUCCESS;
742}
743
744/* see Table 9-9 */
745AVCDec_Status ce_TotalZerosChromaDC(AVCDecBitstream *stream, int *code, int TotalCoeff)
746{
747    const static uint8 TotZeroChrom1to3[3][8][2] =
748    {
749        {{3, 3}, {2, 3}, {1, 2}, {1, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}},
750        {{2, 2}, {2, 2}, {1, 2}, {1, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}},
751        {{1, 1}, {1, 1}, {1, 1}, {1, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}},
752    };
753
754
755    uint temp;
756    uint8 *pcode;
757
758    BitstreamShowBits(stream, 3, &temp);
759    pcode = (uint8*) & (TotZeroChrom1to3[TotalCoeff-1][temp]);
760
761    *code = pcode[0];
762
763    BitstreamFlushBits(stream, pcode[1]);
764
765    return AVCDEC_SUCCESS;
766}
767
768/* see Table 9-10 */
769AVCDec_Status ce_RunBefore(AVCDecBitstream *stream, int *code, int zerosLeft)
770{
771    const static int codlen[6] = {1, 2, 2, 3, 3, 3}; /* num bits to read */
772    const static uint8 RunBeforeTab[6][8][2] = {{{1, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
773        /*const static int RunBefore2[4][2]=*/{{2, 2}, {1, 2}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
774        /*const static int RunBefore3[4][2]=*/{{3, 2}, {2, 2}, {1, 2}, {0, 2}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
775        /*const static int RunBefore4[7][2]=*/{{4, 3}, {3, 3}, {2, 2}, {2, 2}, {1, 2}, {1, 2}, {0, 2}, {0, 2}},
776        /*const static int RunBefore5[7][2]=*/{{5, 3}, {4, 3}, {3, 3}, {2, 3}, {1, 2}, {1, 2}, {0, 2}, {0, 2}},
777        /*const static int RunBefore6[7][2]=*/{{1, 3}, {2, 3}, {4, 3}, {3, 3}, {6, 3}, {5, 3}, {0, 2}, {0, 2}}
778    };
779
780    uint temp;
781    uint8 *pcode;
782    int indx;
783
784    if (zerosLeft <= 6)
785    {
786        BitstreamShowBits(stream, codlen[zerosLeft-1], &temp);
787
788        pcode = (uint8*) & (RunBeforeTab[zerosLeft-1][temp][0]);
789
790        *code = pcode[0];
791
792        BitstreamFlushBits(stream, pcode[1]);
793    }
794    else
795    {
796        BitstreamReadBits(stream, 3, &temp);
797        if (temp)
798        {
799            *code = 7 - temp;
800        }
801        else
802        {
803            BitstreamShowBits(stream, 9, &temp);
804            temp <<= 7;
805            temp |= 1;
806            indx = 0;
807            PV_CLZ(indx, temp)
808            *code = 7 + indx;
809            BitstreamFlushBits(stream, indx + 1);
810        }
811    }
812
813
814    return AVCDEC_SUCCESS;
815}
816