PageRenderTime 171ms CodeModel.GetById 67ms app.highlight 96ms RepoModel.GetById 1ms app.codeStats 0ms

/Avc/vlc.cpp

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