videoeditorengine/avcedit/src/vld.cpp
branchRCL_3
changeset 3 e0b5df5c0969
parent 0 951a5db380a0
child 5 4c409de21d23
equal deleted inserted replaced
0:951a5db380a0 3:e0b5df5c0969
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //#include "epoclib.h"
       
    21 #include "globals.h"
       
    22 #include "bitbuffer.h"
       
    23 #include "vld.h"
       
    24 
       
    25 
       
    26 #define VLD_MAX_CW_LEN              33
       
    27 #define VLD_MAX_LONG_CW_LEN         65
       
    28 
       
    29 #define VLD_MAX_UVLC_CODE_NUM       65535         /* 2^16-1 */
       
    30 #define VLD_MAX_LONG_UVLC_CODE_NUM  4294967295U    /* 2^32-1 */
       
    31 
       
    32 #define VLD_NUM_MB_CODES_INTRA      (1+4*3*2+1)   /* 1 4x4, 24 16x16, 1 PCM */
       
    33 #define VLD_NUM_MB_CODES_INTER      (5+1+4*3*2+1) /* 5 inter, 1 4x4, 24 16x16, 1 PCM */
       
    34 
       
    35 #define VLD_MAX_SUB_MB_MODE         3
       
    36 
       
    37 #define VLD_MAX_IPR_CHROMA_MODE     3
       
    38 
       
    39 #define VLD_MAX_CBP_CODE            47
       
    40 #define VLD_MAX_DELTA_QP_CODE       52
       
    41 
       
    42 
       
    43 #ifdef VIDEOEDITORENGINE_AVC_EDITING
       
    44 /*
       
    45  * Static tables for VLD decoder
       
    46  */
       
    47 
       
    48 
       
    49 /* gives CBP value from codeword number, both for intra and inter */
       
    50 static const int8 code2cbp[VLD_MAX_CBP_CODE+1][2] = {  
       
    51   {47, 0},{31,16},{15, 1},{ 0, 2},{23, 4},{27, 8},{29,32},{30, 3},{ 7, 5},{11,10},{13,12},{14,15}, 
       
    52   {39,47},{43, 7},{45,11},{46,13},{16,14},{ 3, 6},{ 5, 9},{10,31},{12,35},{19,37},{21,42},{26,44},
       
    53   {28,33},{35,34},{37,36},{42,40},{44,39},{ 1,43},{ 2,45},{ 4,46},{ 8,17},{17,18},{18,20},{20,24},
       
    54   {24,19},{ 6,21},{ 9,26},{22,28},{25,23},{32,27},{33,29},{34,30},{36,22},{40,25},{38,38},{41,41}    
       
    55 };
       
    56 
       
    57 static const int numRefIndices[5] = {
       
    58   0, 1, 2, 2, 4
       
    59 };
       
    60 
       
    61 /* Look-up table for determining the number of leading zero bits in a 7-bit bumber */
       
    62 static const int8 numLeadZerosTab[128] = {
       
    63   7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
       
    64   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
       
    65   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       
    66   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       
    67 };
       
    68 
       
    69 
       
    70 /*
       
    71  * Structure of the code is XXYYYYZZ, where XX is the length of the code
       
    72  * minus (number of leading zero bits plus 1), YYYY is totalCoef-1 and
       
    73  * ZZ is numTrailingOnes. Code xx000010 means that there are no coefficients.
       
    74  * 0 is illegal.
       
    75  */
       
    76 static const u_int8 numCoefTrailTab[3][16][8] = {
       
    77   {
       
    78     {  2,  2,  2,  2,  2,  2,  2,  2}, {  1,  1,  1,  1,  1,  1,  1,  1},
       
    79     {  6,  6,  6,  6,  6,  6,  6,  6}, {133,133,128,128, 75, 75, 75, 75},
       
    80     {147,147,138,138, 79, 79, 79, 79}, {151,151,142,142,137,137,132,132},
       
    81     {155,155,146,146,141,141,136,136}, {159,159,150,150,145,145,140,140},
       
    82     {163,163,154,154,149,149,144,144}, {220,226,221,216,231,222,217,212},
       
    83     {239,234,229,228,235,230,225,224}, {247,242,237,236,243,238,233,232},
       
    84     {255,250,249,244,251,246,245,240}, {188,188,190,190,189,189,184,184},
       
    85     { 49, 49, 49, 49, 49, 49, 49, 49}, {  0,  0,  0,  0,  0,  0,  0,  0}
       
    86   },
       
    87   {
       
    88     { 65, 65, 65, 65, 66, 66, 66, 66}, {143,143,139,139, 70, 70, 70, 70},
       
    89     {215,202,201,192,147,147,133,133}, {155,155,142,142,141,141,132,132},
       
    90     {159,159,146,146,145,145,136,136}, {144,144,150,150,149,149,140,140},
       
    91     {163,163,154,154,153,153,148,148}, {235,226,225,220,231,222,221,216},
       
    92     {232,234,233,228,239,230,229,224}, {247,242,241,240,243,238,237,236},
       
    93     {249,248,250,245,182,182,180,180}, {191,191,190,190,189,189,188,188},
       
    94     { 59, 59, 59, 59, 59, 59, 59, 59}, {  0,  0,  0,  0,  0,  0,  0,  0},
       
    95     {  0,  0,  0,  0,  0,  0,  0,  0}, {  0,  0,  0,  0,  0,  0,  0,  0}
       
    96   },
       
    97   {
       
    98     {219,215,211,207,203,198,193,194}, {209,210,205,206,201,223,202,197},
       
    99     {200,218,217,196,227,214,213,192}, {216,212,226,208,231,222,221,204},
       
   100     {239,234,229,224,235,230,225,220}, {236,242,237,232,243,238,233,228},
       
   101     {249,244,247,246,245,240,177,177}, {189,189,184,184,187,187,186,186},
       
   102     {127,127,127,127,126,126,126,126}, { 60, 60, 60, 60, 60, 60, 60, 60},
       
   103     {  0,  0,  0,  0,  0,  0,  0,  0}, {  0,  0,  0,  0,  0,  0,  0,  0},
       
   104     {  0,  0,  0,  0,  0,  0,  0,  0}, {  0,  0,  0,  0,  0,  0,  0,  0},
       
   105     {  0,  0,  0,  0,  0,  0,  0,  0}, {  0,  0,  0,  0,  0,  0,  0,  0}
       
   106   }
       
   107 };
       
   108 
       
   109 
       
   110 /*
       
   111  * The structure of the code is XXXYYYXX where XXX is the length of the code
       
   112  * minus 1, YYY is total number of non-zero coefficients and XX is
       
   113  * the number of trailing ones.
       
   114  */
       
   115 static const u_int8 numCoefTrailTabChroma[8][4] = {
       
   116   {  5,  5,  5,  5},
       
   117   { 32, 32, 32, 32},
       
   118   { 74, 74, 74, 74},
       
   119   {168,175,169,164},
       
   120   {176,176,172,172},
       
   121   {206,206,205,205},
       
   122   {242,242,241,241},
       
   123   {211,211,211,211}
       
   124 };
       
   125 
       
   126 
       
   127 /* Offsets for 15 Huffman tables in totalZerosTab */
       
   128 static const int totalZerosTabOffset[15] = {
       
   129   0, 40, 72, 104, 136, 168,
       
   130   184, 200, 216, 232, 248, 264, 280,
       
   131   288, 296
       
   132 };
       
   133 
       
   134 /*
       
   135  * The meaning of the code in the table is the following:
       
   136  *  If (code > 0xc0) then (code - 0xc0) is an offset to the next position 
       
   137  *  in the Huffman tree
       
   138  *  Otherwise, code structure is XXXXYYYY where XXXX is the length of the code
       
   139  *  and YYYY is the number of zero coefficients.
       
   140  */
       
   141 static const u_int8 totalZerosTab[304] = {
       
   142   0xC8, 0xD0, 0x32, 0x31, 0x10, 0x10, 0x10, 0x10,  /* totalCoef==1 */
       
   143   0xD8, 0xE0, 0x68, 0x67, 0x56, 0x56, 0x55, 0x55,  /* prefix 000 */
       
   144   0x44, 0x44, 0x44, 0x44, 0x43, 0x43, 0x43, 0x43,  /* prefix 001 */
       
   145   0x00, 0x9f, 0x9e, 0x9d, 0x8c, 0x8c, 0x8b, 0x8b,  /* prefix 000000 */
       
   146   0x7a, 0x7a, 0x7a, 0x7a, 0x79, 0x79, 0x79, 0x79,  /* prefix 000001 */
       
   147   0xC8, 0xD0, 0xD8, 0x34, 0x33, 0x32, 0x31, 0x30,  /* totalCoef==2 */
       
   148   0x6e, 0x6d, 0x6c, 0x6b, 0x5a, 0x5a, 0x59, 0x59,  /* prefix 000 */
       
   149   0x48, 0x48, 0x48, 0x48, 0x47, 0x47, 0x47, 0x47,  /* prefix 001 */
       
   150   0x46, 0x46, 0x46, 0x46, 0x45, 0x45, 0x45, 0x45,  /* prefix 010 */
       
   151   0xC8, 0xD0, 0xD8, 0x37, 0x36, 0x33, 0x32, 0x31,  /* totalCoef==3 */
       
   152   0x6d, 0x6b, 0x5c, 0x5c, 0x5a, 0x5a, 0x59, 0x59,  /* prefix 000 */
       
   153   0x48, 0x48, 0x48, 0x48, 0x45, 0x45, 0x45, 0x45,  /* prefix 001 */
       
   154   0x44, 0x44, 0x44, 0x44, 0x40, 0x40, 0x40, 0x40,  /* prefix 010 */
       
   155   0xC8, 0xD0, 0xD8, 0x38, 0x36, 0x35, 0x34, 0x31,  /* totalCoef==4 */
       
   156   0x5c, 0x5c, 0x5b, 0x5b, 0x5a, 0x5a, 0x50, 0x50,  /* prefix 000 */
       
   157   0x49, 0x49, 0x49, 0x49, 0x47, 0x47, 0x47, 0x47,  /* prefix 001 */
       
   158   0x43, 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x42,  /* prefix 010 */
       
   159   0xC8, 0xD0, 0xD8, 0x37, 0x36, 0x35, 0x34, 0x33,  /* totalCoef==5 */
       
   160   0x5b, 0x5b, 0x59, 0x59, 0x4a, 0x4a, 0x4a, 0x4a,  /* prefix 000 */
       
   161   0x48, 0x48, 0x48, 0x48, 0x42, 0x42, 0x42, 0x42,  /* prefix 001 */
       
   162   0x41, 0x41, 0x41, 0x41, 0x40, 0x40, 0x40, 0x40,  /* prefix 010 */
       
   163   0xC8, 0x39, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32,  /* totalCoef==6 */
       
   164   0x6a, 0x60, 0x51, 0x51, 0x48, 0x48, 0x48, 0x48,  /* prefix 000 */
       
   165   0xC8, 0x38, 0x36, 0x34, 0x33, 0x32, 0x25, 0x25,  /* totalCoef==7 */
       
   166   0x69, 0x60, 0x51, 0x51, 0x47, 0x47, 0x47, 0x47,  /* prefix 000 */
       
   167   0xC8, 0x37, 0x36, 0x33, 0x25, 0x25, 0x24, 0x24,  /* totalCoef==8 */
       
   168   0x68, 0x60, 0x52, 0x52, 0x41, 0x41, 0x41, 0x41,  /* prefix 000 */
       
   169   0xC8, 0x35, 0x26, 0x26, 0x24, 0x24, 0x23, 0x23,  /* totalCoef==9 */
       
   170   0x61, 0x60, 0x57, 0x57, 0x42, 0x42, 0x42, 0x42,  /* prefix 000 */
       
   171   0xC8, 0x32, 0x25, 0x25, 0x24, 0x24, 0x23, 0x23,  /* totalCoef==10 */
       
   172   0x51, 0x51, 0x50, 0x50, 0x46, 0x46, 0x46, 0x46,  /* prefix 000 */
       
   173   0xC8, 0x32, 0x33, 0x35, 0x14, 0x14, 0x14, 0x14,  /* totalCoef==11 */
       
   174   0x40, 0x40, 0x40, 0x40, 0x41, 0x41, 0x41, 0x41,  /* prefix 000 */
       
   175   0xC8, 0x34, 0x22, 0x22, 0x13, 0x13, 0x13, 0x13,  /* totalCoef==12 */
       
   176   0x40, 0x40, 0x40, 0x40, 0x41, 0x41, 0x41, 0x41,  /* prefix 000 */
       
   177   0x30, 0x31, 0x23, 0x23, 0x12, 0x12, 0x12, 0x12,  /* totalCoef==13 */
       
   178   0x20, 0x20, 0x21, 0x21, 0x12, 0x12, 0x12, 0x12,  /* totalCoef==14 */
       
   179   0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,  /* totalCoef==15 */
       
   180 };
       
   181 
       
   182 
       
   183 /*
       
   184  * The structure of the code is XXXXYYYY where XXXX is the length of the code
       
   185  * and YYYY is the number of zero coefficients.
       
   186  */
       
   187 static const u_int8 totalZerosTabChroma[3][8] = {
       
   188   {0x33, 0x32, 0x21, 0x21, 0x10, 0x10, 0x10, 0x10},
       
   189   {0x22, 0x22, 0x21, 0x21, 0x10, 0x10, 0x10, 0x10},
       
   190   {0x11, 0x11, 0x11, 0x11, 0x10, 0x10, 0x10, 0x10}
       
   191 };
       
   192 
       
   193 
       
   194 /*
       
   195  * The structure of the code is XXXXYYYY where XXXX is the length of the code
       
   196  * and YYYY is run length.
       
   197  */
       
   198 static const u_int8 runBeforeTab[7][8] = {
       
   199   {0x11, 0x11, 0x11, 0x11, 0x10, 0x10, 0x10, 0x10},
       
   200   {0x22, 0x22, 0x21, 0x21, 0x10, 0x10, 0x10, 0x10},
       
   201   {0x23, 0x23, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20},
       
   202   {0x34, 0x33, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20},
       
   203   {0x35, 0x34, 0x33, 0x32, 0x21, 0x21, 0x20, 0x20},
       
   204   {0x31, 0x32, 0x34, 0x33, 0x36, 0x35, 0x20, 0x20},
       
   205   {0x00, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30},
       
   206 };
       
   207 
       
   208 
       
   209 
       
   210 /*
       
   211  * Static function prototypes
       
   212  */
       
   213 
       
   214 static int getCoefLevelVLC0(bitbuffer_s *bitbuf);
       
   215 
       
   216 static int getCoefLevelVLCN(bitbuffer_s *bitbuf, int tabNum);
       
   217 
       
   218 static int get4x4coefs(bitbuffer_s *bitbuf, int coef[4][4],
       
   219                        int blkIdxX, int blkIdxY, int8 *numCoefUpPred,
       
   220                        int8 *numCoefLeftPred, int mbAvailbits, int dcSkip);
       
   221 
       
   222 static int get2x2coefsCDC(bitbuffer_s *bitbuf, int coef[4], int *numCoef);
       
   223 
       
   224 #endif  // VIDEOEDITORENGINE_AVC_EDITING
       
   225 
       
   226 /*
       
   227  * Functions begin here
       
   228  */
       
   229 
       
   230 /*
       
   231  *
       
   232  * vldGetFLC:
       
   233  *
       
   234  * Parameters:
       
   235  *      bitbuf                Bitbuffer object
       
   236  *      len                   Length of the code
       
   237  *
       
   238  * Function:
       
   239  *      Get Fixed Length Code (max length 16 bits).
       
   240  *
       
   241  * Returns:
       
   242  *      Code
       
   243  *
       
   244  */
       
   245 unsigned int vldGetFLC(bitbuffer_s *bitbuf, int len)
       
   246 {
       
   247   u_int32 code;
       
   248 
       
   249   bibGetMax16bits(bitbuf, len, &code);
       
   250 
       
   251   return (unsigned int)code;
       
   252 }
       
   253 
       
   254 /*
       
   255  *
       
   256  * vldGetUVLC:
       
   257  *
       
   258  * Parameters:
       
   259  *      bitbuf                Bitbuffer object
       
   260  *
       
   261  * Function:
       
   262  *      Decode an UVLC codeword, UVLC codeword is of the form:
       
   263  *      code          codeword
       
   264  *      0                1
       
   265  *      1               010
       
   266  *      2               011
       
   267  *      3              00100
       
   268  *      4              00101
       
   269  *      5              00110
       
   270  *      6              00111
       
   271  *      7             0001000
       
   272  *      8             0001001
       
   273  *      ...             ...
       
   274  *
       
   275  * Returns:
       
   276  *      Codenumber in the range [0, 65535]
       
   277  *
       
   278  */
       
   279 #if 1
       
   280 unsigned int vldGetUVLC(bitbuffer_s *bitbuf)
       
   281 {
       
   282   u_int32 c;
       
   283   int prefixLen;
       
   284   int bit;
       
   285 
       
   286   bibGetBit(bitbuf, &bit);
       
   287 
       
   288   if (bit == 1)
       
   289     return 0;
       
   290 
       
   291   prefixLen = 0;
       
   292 
       
   293   do {
       
   294     bibGetBit(bitbuf, &bit);
       
   295     prefixLen += 1;
       
   296 
       
   297     /* Maximum prefix length is VLD_MAX_CW_LEN/2 */
       
   298     if (prefixLen == VLD_MAX_CW_LEN/2) {
       
   299       bibGetMax16bits(bitbuf, VLD_MAX_CW_LEN/2, &c);
       
   300       if (bit != 1 || c != 0) {
       
   301         /* We encoutered overlong codeword or we encoutered too big code */
       
   302         bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR);
       
   303         return 0;
       
   304       }
       
   305       else
       
   306         return VLD_MAX_UVLC_CODE_NUM;  /* Otherwise, return maximum 16-bit code number */
       
   307     }
       
   308 
       
   309   } while (bit == 0);
       
   310 
       
   311   bibGetMax16bits(bitbuf, prefixLen, &c);
       
   312 
       
   313   return (unsigned int)(c + (1<<prefixLen)-1);
       
   314 }
       
   315 #else
       
   316 unsigned int vldGetUVLC(bitbuffer_s *bitbuf)
       
   317 {
       
   318   int prefixLen, len;
       
   319   u_int32 bits;
       
   320 
       
   321   bibShowMax16bits(bitbuf, 16, &bits);
       
   322 
       
   323   if (bits >= 0x0100) {
       
   324     prefixLen = numLeadZerosTab[bits>>9];
       
   325     len = 2*prefixLen+1;
       
   326     bibSkipBits(bitbuf, len);
       
   327     return (unsigned int)((bits >> (16-len)) - 1);
       
   328   }
       
   329   else {
       
   330     if (bits >= 0x0001)
       
   331       prefixLen = 8 + numLeadZerosTab[bits>>1];
       
   332     else
       
   333       prefixLen = 16;
       
   334 
       
   335     bibSkipBits(bitbuf, prefixLen);
       
   336     bibGetBits(bitbuf, prefixLen+1, &bits);
       
   337     return (unsigned int)(bits - 1);
       
   338   }
       
   339 }
       
   340 #endif
       
   341 
       
   342 /*
       
   343  *
       
   344  * vldGetSignedUVLClong:
       
   345  *
       
   346  * Parameters:
       
   347  *      bitbuf                Bitbuffer object
       
   348  *
       
   349  * Function:
       
   350  *      Decode an UVLC codeword and produce signed 32-bit integer
       
   351  *
       
   352  * Returns:
       
   353  *      Code in the range [-2^31, 2^31-1]
       
   354  *
       
   355  */
       
   356 int32 vldGetSignedUVLClong(bitbuffer_s *bitbuf)
       
   357 {
       
   358   u_int32 codeNum;
       
   359   int32 code;
       
   360 
       
   361   codeNum = vldGetUVLClong(bitbuf);
       
   362 
       
   363   /* Decode magnitude */
       
   364   code = (int32)((codeNum >> 1) + (codeNum & 1));
       
   365 
       
   366   /* Decode sign */
       
   367   if ((codeNum & 1) == 0)
       
   368     code = -code;
       
   369 
       
   370   return code;
       
   371 }
       
   372 
       
   373 /*
       
   374  *
       
   375  * vldGetUVLClong:
       
   376  *
       
   377  * Parameters:
       
   378  *      bitbuf                Bitbuffer object
       
   379  *      code                  Return pointer for code
       
   380  *
       
   381  * Function:
       
   382  *      Decode long UVLC codeword.
       
   383  *
       
   384  * Returns:
       
   385  *      Codenumber in the range [0, 2^32-1]
       
   386  *
       
   387  */
       
   388 u_int32 vldGetUVLClong(bitbuffer_s *bitbuf)
       
   389 {
       
   390   u_int32 c, cLo;
       
   391   int prefixLen, len0;
       
   392   int bits;
       
   393 
       
   394   prefixLen = -1;
       
   395 
       
   396   do {
       
   397     bibGetBit(bitbuf, &bits);
       
   398     prefixLen += 1;
       
   399 
       
   400     /* Is codeword too long? */
       
   401     if (prefixLen > VLD_MAX_LONG_CW_LEN/2) {
       
   402       bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR);
       
   403       return 0;
       
   404     }
       
   405 
       
   406   } while (bits == 0);
       
   407 
       
   408   len0 = min(24, prefixLen);
       
   409   bibGetBits(bitbuf, len0, &c);
       
   410 
       
   411   if (prefixLen > 24) {
       
   412     /* We have to read bits in two pieces because bibGetBits can only fetch */
       
   413     /* max. 24 bits */
       
   414     bibGetMax16bits(bitbuf, prefixLen-24, &cLo);
       
   415     c = (c << (prefixLen-24)) | cLo;  /* Combine two pieces */
       
   416 
       
   417     if (prefixLen == VLD_MAX_LONG_CW_LEN/2) {
       
   418       /* Is codeword too big? */
       
   419       if (c != 0) {
       
   420         bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR);
       
   421         return 0;
       
   422       }
       
   423       else
       
   424         return (u_int32)VLD_MAX_LONG_UVLC_CODE_NUM;  /* Otherwise, return maximum 32-bit code number */
       
   425     }
       
   426   }
       
   427 
       
   428   return (c + (1<<prefixLen)-1);
       
   429 }
       
   430     
       
   431 
       
   432 #ifdef VIDEOEDITORENGINE_AVC_EDITING
       
   433 
       
   434 /*
       
   435  *
       
   436  * vldGetSignedUVLC:
       
   437  *
       
   438  * Parameters:
       
   439  *      bitbuf                Bitbuffer object
       
   440  *
       
   441  * Function:
       
   442  *      Decode an UVLC codeword and produce signed integer.
       
   443  *
       
   444  * Returns:
       
   445  *      Code is in the range [-32768,32767].
       
   446  *
       
   447  */
       
   448 int vldGetSignedUVLC(bitbuffer_s *bitbuf)
       
   449 {
       
   450   unsigned int codeNum;
       
   451   int code;
       
   452 
       
   453   codeNum = vldGetUVLC(bitbuf);
       
   454 
       
   455   /* Decode magnitude */
       
   456   code = (int)(((int32)codeNum+1)>>1);
       
   457 
       
   458   /* Decode sign */
       
   459   if ((codeNum & 1) == 0)
       
   460     code = -code;
       
   461 
       
   462   return code;
       
   463 }
       
   464 
       
   465 /*
       
   466  *
       
   467  * setChromaCbp:
       
   468  *
       
   469  * Parameters:
       
   470  *      nc                    If 0, all CBP are zero
       
   471  *      cbpDC                 Coded Bit Pattern for chroma DC
       
   472  *      cbp                   Coded Bit Pattern for chroma AC
       
   473  *
       
   474  * Function:
       
   475  *      Set chroma DC and AC CBP values
       
   476  *      nc = 0:   No coefficients
       
   477  *      nc = 1:   Only nonzero DC coefficients
       
   478  *      nc = 2:   Nonzero AC and DC coefficients
       
   479  *
       
   480  * Returns:
       
   481  *      -
       
   482  */
       
   483 void setChromaCbp(int nc, int *cbpDC, int *cbp)
       
   484 {
       
   485   if (nc == 0)
       
   486     *cbpDC = *cbp = 0;
       
   487   else if (nc == 1)
       
   488     *cbpDC = 3, *cbp = 0;
       
   489   else
       
   490     *cbpDC = 3, *cbp = (1<<2*BLK_PER_MB/2*BLK_PER_MB/2)-1;
       
   491 }
       
   492 
       
   493 
       
   494 /*
       
   495  *
       
   496  * getLumaBlkCbp:
       
   497  *
       
   498  * Parameters:
       
   499  *      cbpY                  CBP for 8x8 blocks
       
   500  *
       
   501  * Function:
       
   502  *      Convert Codec Block Pattern of 8x8 blocks to Codec Block Pattern for
       
   503  *      4x4 blocks. If 8x8 block has nonzero coefficients then all 4x4 blocks
       
   504  *      within that 8x8 block are marked nonzero.
       
   505  *
       
   506  * Returns:
       
   507  *      CBP for 4x4 blocks
       
   508  *
       
   509  */
       
   510 int getLumaBlkCbp(int cbpY)
       
   511 {
       
   512   int cbp;
       
   513 
       
   514   cbp  = (cbpY & (1<<0)) == 0 ? 0 : 0x0033;
       
   515   cbp |= (cbpY & (1<<1)) == 0 ? 0 : 0x00cc;
       
   516   cbp |= (cbpY & (1<<2)) == 0 ? 0 : 0x3300;
       
   517   cbp |= (cbpY & (1<<3)) == 0 ? 0 : 0xcc00;
       
   518 
       
   519   return cbp;
       
   520 }
       
   521 
       
   522 
       
   523 /*
       
   524  *
       
   525  * vldGetRunIndicator:
       
   526  *
       
   527  * Parameters:
       
   528  *      bitbuf                Bitbuffer object
       
   529  *
       
   530  * Function:
       
   531  *      Get the amount of copy macroblocks.
       
   532  *
       
   533  * Returns:
       
   534  *      The number of copy MBs
       
   535  *
       
   536  */
       
   537 unsigned int vldGetRunIndicator(bitbuffer_s *bitbuf)
       
   538 {
       
   539   return vldGetUVLC(bitbuf);
       
   540 }
       
   541 
       
   542 
       
   543 /*
       
   544  *
       
   545  * vldGetMBtype:
       
   546  *
       
   547  * Parameters:
       
   548  *      bitbuf                Bitbuffer object
       
   549  *      hdr                   Return pointer for MB params
       
   550  *      picType               Type of current picture (intra/inter)
       
   551  *
       
   552  * Function:
       
   553  *      Get macroblock type. If MB is 16x16 intra then form cbp. Subblock
       
   554  *      mode are also fetched if main mode is 8x8 inter.
       
   555  *
       
   556  * Returns:
       
   557  *      VLD_OK for no error and VLD_ERROR for error.
       
   558  *
       
   559  */
       
   560 int vldGetMBtype(bitbuffer_s *bitbuf, vldMBtype_s *hdr, int picType)
       
   561 {
       
   562   unsigned int code;
       
   563   int nc;
       
   564   int i;
       
   565 
       
   566   /* Get macroblock mode */
       
   567   code = vldGetUVLC(bitbuf);
       
   568 
       
   569   if (IS_SLICE_I(picType)) {
       
   570     /* INTRA macroblock in INTRA slice */
       
   571     if (code >= VLD_NUM_MB_CODES_INTRA)
       
   572       return VLD_ERROR;
       
   573 
       
   574     hdr->type = MBK_INTRA;
       
   575   }
       
   576   else {
       
   577     if (code >= VLD_NUM_MB_CODES_INTER)
       
   578       return VLD_ERROR;
       
   579 
       
   580     if (code >= 5) {
       
   581       /* INTRA macroblock in INTER slice */
       
   582       hdr->type = MBK_INTRA;
       
   583       code -= 5;
       
   584     }
       
   585     else {
       
   586       /* INTER macroblock in INTER slice */
       
   587       hdr->type = MBK_INTER;
       
   588       hdr->interMode = code+1;
       
   589 
       
   590       if (hdr->interMode >= 4) {
       
   591 
       
   592         /*
       
   593          * Get sub-macroblock mode modes
       
   594          */
       
   595 
       
   596         for (i = 0; i < 4; i++) {
       
   597           code = vldGetUVLC(bitbuf);
       
   598 
       
   599           if (code > VLD_MAX_SUB_MB_MODE)
       
   600             return VLD_ERROR;
       
   601 
       
   602           hdr->inter8x8modes[i] = code;
       
   603         }
       
   604 
       
   605       }
       
   606 
       
   607       if (bibGetStatus(bitbuf) < 0)
       
   608         return VLD_ERROR;
       
   609 
       
   610       return VLD_OK;
       
   611     }
       
   612   }
       
   613 
       
   614   if (bibGetStatus(bitbuf) < 0)
       
   615     return VLD_ERROR;
       
   616 
       
   617   /*
       
   618    * This is INTRA macroblock, find out INTRA type
       
   619    */
       
   620 
       
   621   if (code == 0)
       
   622     hdr->intraType = MBK_INTRA_TYPE1;
       
   623   else if (code != VLD_NUM_MB_CODES_INTRA-1) {
       
   624     /* 16x16 INTRA - compose cbp value */
       
   625     hdr->intraType = MBK_INTRA_TYPE2;
       
   626     code -= 1;
       
   627     hdr->intraMode = code & 3;
       
   628     code >>= 2;
       
   629     if (code < 3) {
       
   630       nc = code;
       
   631       hdr->cbpY = 0;
       
   632     }
       
   633     else {
       
   634       nc = code-3;
       
   635       hdr->cbpY = 0xffff;
       
   636     }
       
   637     setChromaCbp(nc, &hdr->cbpChromaDC, &hdr->cbpC);
       
   638   }
       
   639   else {
       
   640     hdr->intraType = MBK_INTRA_TYPE_PCM;
       
   641     hdr->cbpY = 0xffff;
       
   642     hdr->cbpC = 0xff;
       
   643     hdr->cbpChromaDC = 0x3;
       
   644   }
       
   645 
       
   646 
       
   647   return VLD_OK;
       
   648 }
       
   649 
       
   650 
       
   651 /*
       
   652  *
       
   653  * vldGetIntraPred:
       
   654  *
       
   655  * Parameters:
       
   656  *      bitbuf                Bitbuffer object
       
   657  *      ipTab                 Return pointer for intra pred. modes
       
   658  *
       
   659  * Function:
       
   660  *      Get 4x4 intra prediction modes. First a one bit flag is read. If flag
       
   661  *      is 1, mode is predicted from neighborin blocks. If flag is 0,
       
   662  *      additional 3 bit mode code is read. Process is repeated for 16 blocks.
       
   663  *
       
   664  * Returns:
       
   665  *      VLD_OK for no error and VLD_ERROR for error.
       
   666  *
       
   667  */
       
   668 int vldGetIntraPred(bitbuffer_s *bitbuf, int8 *ipTab)
       
   669 {
       
   670   int i, j, k, l;
       
   671   int bit;
       
   672   int modeCode;
       
   673 
       
   674   for (j = 0; j < BLK_PER_MB; j+=2) {
       
   675     for (i = 0; i < BLK_PER_MB; i+=2) {
       
   676       for (k = 0; k < 2; k++) {
       
   677         for (l = 0; l < 2; l++) {
       
   678           bibGetBit(bitbuf, &bit);    /* Read flag */
       
   679           if (bit == 1)               /* If 1, predict from neighbors */
       
   680             ipTab[(j+k)*4+i+l] = -1;
       
   681           else {                      /* If 0, read mode code */
       
   682             bibGetMax8bits(bitbuf, 3, &modeCode);
       
   683             ipTab[(j+k)*4+i+l] = (int8)modeCode;
       
   684           }
       
   685         }
       
   686       }
       
   687     }
       
   688   }
       
   689 
       
   690   /* Check if error occured */
       
   691   if (bibGetStatus(bitbuf) < 0)
       
   692     return VLD_ERROR;
       
   693   else
       
   694     return VLD_OK;
       
   695 }
       
   696 
       
   697 
       
   698 /*
       
   699  *
       
   700  * vldGetChromaIntraPred:
       
   701  *
       
   702  * Parameters:
       
   703  *      bitbuf                Bitbuffer object
       
   704  *
       
   705  * Function:
       
   706  *      Get chroma intra prediction mode.
       
   707  *
       
   708  * Returns:
       
   709  *      Prediction mode.
       
   710  *
       
   711  */
       
   712 int vldGetChromaIntraPred(bitbuffer_s *bitbuf)
       
   713 {
       
   714   unsigned int mode;
       
   715 
       
   716   mode = vldGetUVLC(bitbuf);
       
   717 
       
   718   if (bibGetStatus(bitbuf) < 0 || mode > VLD_MAX_IPR_CHROMA_MODE)
       
   719     return VLD_ERROR;
       
   720 
       
   721   return (int)mode;
       
   722 }
       
   723 
       
   724 
       
   725 /*
       
   726  *
       
   727  * vldGetMotVecs:
       
   728  *
       
   729  * Parameters:
       
   730  *      bitbuf                Bitbuffer object
       
   731  *      interMode             Motion mode (16x16, 8x16, ...)
       
   732  *      hasRef                Flag for multiple ref. frames
       
   733  *      refNum                Return pointer for ref. indices
       
   734  *      predVecs              Return pointer for delta vectors
       
   735  *      numVecs               Number of vectors to read
       
   736  *
       
   737  * Function:
       
   738  *      Get reference indices and delta motion vectors for MB
       
   739  *
       
   740  * Returns:
       
   741  *      VLD_OK for no error and VLD_ERROR for error
       
   742  *
       
   743  */
       
   744 int vldGetMotVecs(bitbuffer_s *bitbuf, int interMode, int numRefFrames,
       
   745                   int *refNum, int predVecs[][2], int numVecs)
       
   746 {
       
   747   int i, j;
       
   748   int code;
       
   749   int refIdx;
       
   750 
       
   751   if (numRefFrames > 1 && interMode < 5) {
       
   752     if (numRefFrames == 2) {
       
   753       for (i = 0; i < numRefIndices[interMode]; i++) {
       
   754         bibGetBit(bitbuf, &refIdx);
       
   755         refNum[i] = 1 - refIdx;
       
   756       }
       
   757     }
       
   758     else {
       
   759       for (i = 0; i < numRefIndices[interMode]; i++) {
       
   760         if ((refNum[i] = (int)vldGetUVLC(bitbuf)) >= numRefFrames)
       
   761           return VLD_ERROR;
       
   762       }
       
   763     }
       
   764   }
       
   765   else
       
   766     refNum[0] = 0;
       
   767 
       
   768   for(j = 0; j < numVecs; j++) {
       
   769     for(i = 0; i < 2; i++) {
       
   770       code = vldGetUVLC(bitbuf);
       
   771       predVecs[j][i] = (code+1)>>1;
       
   772       if ((code & 1) == 0)
       
   773         predVecs[j][i] = -predVecs[j][i];
       
   774     }
       
   775   }
       
   776 
       
   777   if (bibGetStatus(bitbuf) < 0)
       
   778     return VLD_ERROR;
       
   779   else
       
   780     return VLD_OK;
       
   781 }
       
   782 
       
   783 
       
   784 /*
       
   785  *
       
   786  * vldGetCBP:
       
   787  *
       
   788  * Parameters:
       
   789  *      bitbuf                Bitbuffer object
       
   790  *      mbType                Macroblock type (intra/inter)
       
   791  *      cbpY                  Return pointer for luma CBP
       
   792  *      cbpChromaDC           Return pointer for chroma DC CBP
       
   793  *      cbpC                  Return pointer for chroma CBP
       
   794  *
       
   795  * Function:
       
   796  *      Get Coded Block Patterns for both luma and chroma
       
   797  *
       
   798  * Returns:
       
   799  *      VLD_OK for no error and VLD_ERROR for error
       
   800  *
       
   801  */
       
   802 int vldGetCBP(bitbuffer_s *bitbuf, int mbType,
       
   803               int *cbpY, int *cbpChromaDC, int *cbpC)
       
   804 {
       
   805   unsigned int code;
       
   806   int cbp;
       
   807 
       
   808   code = vldGetUVLC(bitbuf);
       
   809 
       
   810   if (bibGetStatus(bitbuf) < 0 || code > VLD_MAX_CBP_CODE)
       
   811     return VLD_ERROR;
       
   812 
       
   813   if (mbType ==  MBK_INTRA)
       
   814     cbp = code2cbp[code][0];
       
   815   else
       
   816     cbp = code2cbp[code][1];
       
   817 
       
   818   *cbpY = getLumaBlkCbp(cbp%16);
       
   819   setChromaCbp(cbp>>4, cbpChromaDC, cbpC);
       
   820 
       
   821   return VLD_OK;
       
   822 }
       
   823 
       
   824 
       
   825 /*
       
   826  *
       
   827  * vldGetDeltaqp:
       
   828  *
       
   829  * Parameters:
       
   830  *      bitbuf                Bitbuffer object
       
   831  *      delta_qp              Return pointer for delta QP
       
   832  *
       
   833  * Function:
       
   834  *      Get macroblock delta QP
       
   835  *
       
   836  * Returns:
       
   837  *      VLD_OK for no error and VLD_ERROR for error
       
   838  *
       
   839  */
       
   840 int vldGetDeltaqp(bitbuffer_s *bitbuf, int *delta_qp)
       
   841 {
       
   842   unsigned int code;
       
   843 
       
   844   code = vldGetUVLC(bitbuf);
       
   845 
       
   846   if (bibGetStatus(bitbuf) < 0 || code > VLD_MAX_DELTA_QP_CODE)
       
   847     return VLD_ERROR;
       
   848 
       
   849   *delta_qp = (code+1)>>1;
       
   850 
       
   851   if ((code & 1) == 0)
       
   852     *delta_qp = -(*delta_qp);
       
   853 
       
   854   return VLD_OK;
       
   855 }
       
   856 
       
   857 
       
   858 /*
       
   859  *
       
   860  * getCoefLevelVLC0:
       
   861  *
       
   862  * Parameters:
       
   863  *      bitbuf                Bitbuffer object
       
   864  *
       
   865  * Function:
       
   866  *      Get CAVLC coefficient level for VLC0 code format
       
   867  *
       
   868  * Returns:
       
   869  *      Coefficient level
       
   870  *
       
   871  */
       
   872 static int getCoefLevelVLC0(bitbuffer_s *bitbuf)
       
   873 {
       
   874   int numLeadingZeroBits;
       
   875   u_int32 bits;
       
   876   int len;
       
   877   int coef;
       
   878 
       
   879   bibShowMax16bits(bitbuf, 16, &bits);
       
   880 
       
   881   if (bits > 0x0003) {
       
   882     if (bits > 0x00ff)
       
   883       numLeadingZeroBits = numLeadZerosTab[(int)(bits>>(16-7))];
       
   884     else
       
   885       numLeadingZeroBits = 7 + numLeadZerosTab[(int)(bits>>(16-7-7))];
       
   886 
       
   887     coef = (numLeadingZeroBits >> 1) + 1;
       
   888     if (numLeadingZeroBits & 1)
       
   889       coef = -coef;
       
   890 
       
   891     len = numLeadingZeroBits + 1;
       
   892     bibSkipBits(bitbuf, len);
       
   893   }
       
   894   else if (bits > 0x0001) {
       
   895     bibGetBits(bitbuf, 19, &bits);
       
   896     coef = 8 + (((int)bits & 15) >> 1);
       
   897     if (bits & 1)
       
   898       coef = -coef;
       
   899   }
       
   900   else if (bits == 0x0001) {
       
   901     bibSkipBits(bitbuf, 16);
       
   902     bibGetMax16bits(bitbuf, 12, &bits);
       
   903     coef = 16 + ((int)bits >> 1);
       
   904     if (bits & 1)
       
   905       coef = -coef;
       
   906   }
       
   907   else {
       
   908     bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR);
       
   909     coef = 0;
       
   910   }
       
   911 
       
   912   return coef;
       
   913 }
       
   914 
       
   915 /*
       
   916  *
       
   917  * getCoefLevelVLCN:
       
   918  *
       
   919  * Parameters:
       
   920  *      bitbuf                Bitbuffer object
       
   921  *      tabNum                VLC table number to be used
       
   922  *
       
   923  * Function:
       
   924  *      Get CAVLC coefficient level for VLC1-6 code format
       
   925  *
       
   926  * Returns:
       
   927  *      Coefficient level
       
   928  *
       
   929  */
       
   930 static int getCoefLevelVLCN(bitbuffer_s *bitbuf, int tabNum)
       
   931 {
       
   932   int numLeadingZeroBits;
       
   933   u_int32 bits;
       
   934   int len;
       
   935   int coef;
       
   936 
       
   937   bibShowBits(bitbuf, 21, &bits);
       
   938 
       
   939   if (bits > 0x00003f) {
       
   940     if (bits > 0x001fff)
       
   941       numLeadingZeroBits = numLeadZerosTab[(int)(bits>>(21-7))];
       
   942     else
       
   943       numLeadingZeroBits = 7 + numLeadZerosTab[(int)(bits>>(21-7-7))];
       
   944 
       
   945     len = numLeadingZeroBits + 1 + tabNum;
       
   946     bits = (bits >> (21 - len)) & ((1 << tabNum) - 1);
       
   947     coef = (int)((numLeadingZeroBits << (tabNum - 1)) + (bits >> 1) +  1);
       
   948     if (bits & 1)
       
   949       coef = -coef;
       
   950 
       
   951     bibSkipBits(bitbuf, len);
       
   952   }
       
   953   else if (bits > 0x00001f) {
       
   954     bibSkipBits(bitbuf, 16);
       
   955     bibGetMax16bits(bitbuf, 12, &bits);
       
   956     coef = (15 << (tabNum - 1)) + ((int)bits >> 1) + 1;
       
   957     if (bits & 1)
       
   958       coef = -coef;
       
   959   }
       
   960   else {
       
   961     bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR);
       
   962     coef = 0;
       
   963   }
       
   964 
       
   965   return coef;
       
   966 }
       
   967 
       
   968 
       
   969 /*
       
   970  *
       
   971  * get4x4coefs:
       
   972  *
       
   973  * Parameters:
       
   974  *      bitbuf                Bitbuffer object
       
   975  *      coef                  Return pointer for 4x4 coefficients
       
   976  *      blkIdxX               Horizontal block pos. within MB
       
   977  *      blkIdxY               Vertical block pos. within MB
       
   978  *      numCoefUpPred         Block coefficients counts of upper block
       
   979  *      numCoefLeftPred       Block coefficients counts of left block
       
   980  *      mbAvailBits           Macroblock availability flags
       
   981  *      dcSkip                if 1, there's no DC coefficient
       
   982  *
       
   983  * Function:
       
   984  *      Decode coefficients for 4x4 block.
       
   985  *
       
   986  * Returns:
       
   987  *      VLD_OK for no error, VLD_ERROR for error
       
   988  *
       
   989  */
       
   990 static int get4x4coefs(bitbuffer_s *bitbuf, int coef[4][4],
       
   991                        int blkIdxX, int blkIdxY, int8 *numCoefUpPred,
       
   992                        int8 *numCoefLeftPred, int mbAvailBits, int dcSkip)
       
   993 {
       
   994   int numCoefPred;
       
   995   int tabNum;
       
   996   u_int32 bits;
       
   997   int code;
       
   998   int numTrailingOnes;
       
   999   int numLeadingZeroBits;
       
  1000   int totalCoef;
       
  1001   int numBits;
       
  1002   u_int32 flc;
       
  1003   int tmpLevel[16];
       
  1004   int level;
       
  1005   int numBigCoef;
       
  1006   u_int32 signs;
       
  1007   int s;
       
  1008   int i;
       
  1009   const u_int8 *totalZerosPtr;
       
  1010   int offset;
       
  1011   int totalZeros;
       
  1012   int zerosLeft;
       
  1013   int run;
       
  1014   int coefPos;
       
  1015   int coefNum;
       
  1016 
       
  1017   static const int vlcNumTab[8] = {
       
  1018     0, 0, 1, 1, 2, 2, 2, 2
       
  1019   };
       
  1020 
       
  1021   static const unsigned int incVlc[7] = {
       
  1022     0,3,6,12,24,48,32768          /* maximum vlc = 6 */
       
  1023   };
       
  1024 
       
  1025   static const int zigZagPos[16] = {
       
  1026     0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
       
  1027   };
       
  1028 
       
  1029   /*
       
  1030    * Decode number of coefficients and number of trailing ones
       
  1031    */
       
  1032 
       
  1033   /* Predict number of coefficients from neighboring blocks */
       
  1034   if (blkIdxX || (mbAvailBits & 1)) {
       
  1035     numCoefPred = numCoefLeftPred[0];
       
  1036     if (blkIdxY || (mbAvailBits & 2))
       
  1037       numCoefPred = (numCoefPred + numCoefUpPred[0] + 1) >> 1;
       
  1038   }
       
  1039   else
       
  1040     numCoefPred = (blkIdxY || (mbAvailBits & 2)) ? numCoefUpPred[0] : 0;
       
  1041 
       
  1042   /* Select variable length code or fixed length decoding */
       
  1043   if (numCoefPred < 8) {
       
  1044 
       
  1045     /* Use variable length code  */
       
  1046 
       
  1047     /* Select table number based on the prediction */
       
  1048     tabNum = vlcNumTab[numCoefPred];
       
  1049 
       
  1050     bibShowBits(bitbuf, 18, &bits);
       
  1051 
       
  1052     /* Compute number of leading zeros using look-up table */
       
  1053     if (bits >= 0x00400)
       
  1054       numLeadingZeroBits = numLeadZerosTab[(int)(bits>>(18-7))];
       
  1055     else
       
  1056       numLeadingZeroBits = 7 + numLeadZerosTab[(int)(bits>>(18-7-7))];
       
  1057 
       
  1058     /* Shift excess bits away */
       
  1059     bits >>= 18-4-numLeadingZeroBits;
       
  1060 
       
  1061     /* numTrailingOnes, totalCoef and length of the codeword are codec in one code */
       
  1062     code = numCoefTrailTab[tabNum][numLeadingZeroBits][(int)bits&7];
       
  1063 
       
  1064     /* Total number of bits in codeword is sum of the number of leading zero bits,     */
       
  1065     /* 1 one bit and the number of bits in postfix. We know the number of leading zero */
       
  1066     /* bits. Postfix length is extracted from 2 highest bits of code.                  */
       
  1067     numBits = numLeadingZeroBits + 1 + (code >> 6);
       
  1068 
       
  1069     bibSkipBits(bitbuf, numBits);
       
  1070 
       
  1071     if ((code & 0x3f) == 2) {
       
  1072       /* All coefficients are zero */
       
  1073       numCoefUpPred[0] = numCoefLeftPred[0] = 0;
       
  1074       return VLD_OK;
       
  1075     }
       
  1076     else {
       
  1077       /* 2 lowest bits contains number of trailing ones */
       
  1078       numTrailingOnes = code & 3;
       
  1079       /* 4 middle bits contain total number of coefficients minus 1 */
       
  1080       totalCoef = ((code >> 2) & 15) + 1;
       
  1081     }
       
  1082   }
       
  1083   else {
       
  1084 
       
  1085     /* Use fixed length code  */
       
  1086 
       
  1087     bibGetMax8bits(bitbuf, 6, &flc);
       
  1088 
       
  1089     if (flc != 3) {
       
  1090       /* 2 lowest bits contains number of trailing ones */
       
  1091       numTrailingOnes = (int)flc & 0x03;
       
  1092       /* high bits contain total number of coefficients minus 1 */
       
  1093       totalCoef = ((int)flc >> 2) + 1;
       
  1094       if (totalCoef < numTrailingOnes)
       
  1095         return VLD_ERROR;
       
  1096     }
       
  1097     else {
       
  1098       /* All coefficients are zero */
       
  1099       numCoefUpPred[0] = numCoefLeftPred[0] = 0;
       
  1100       return VLD_OK;
       
  1101     }
       
  1102   }
       
  1103 
       
  1104   numCoefUpPred[0] = numCoefLeftPred[0] = (int8) totalCoef;
       
  1105 
       
  1106 
       
  1107   /*
       
  1108    * Decode signs for trailing ones and store trailing ones in tmpLevel.
       
  1109    */
       
  1110 
       
  1111   numBigCoef = totalCoef - numTrailingOnes; 
       
  1112 
       
  1113   if (numTrailingOnes != 0) {
       
  1114 
       
  1115     /* Get signs for trailing ones. There can be maximum of 3 of them */
       
  1116     bibGetMax8bits(bitbuf, numTrailingOnes, &signs);
       
  1117 
       
  1118     for (i = numTrailingOnes-1; i >= 0; i--) {
       
  1119       s = ((int)signs >> i) & 1;
       
  1120       tmpLevel[numBigCoef+i] = 1 - 2*s;
       
  1121     }
       
  1122   }
       
  1123 
       
  1124   if (numBigCoef != 0) {
       
  1125 
       
  1126     /*
       
  1127      * Decode first "big" level
       
  1128      */
       
  1129 
       
  1130     if (totalCoef > 10 && numTrailingOnes < 3)
       
  1131       level = getCoefLevelVLCN(bitbuf, 1);
       
  1132     else
       
  1133       level = getCoefLevelVLC0(bitbuf);
       
  1134 
       
  1135     if (numTrailingOnes < 3)
       
  1136       level += (level > 0) ? 1 : -1;
       
  1137 
       
  1138     tmpLevel[numBigCoef-1] = level;
       
  1139 
       
  1140     tabNum = (Abs((int32)level) > 3) ? 2 : 1;
       
  1141 
       
  1142     /*
       
  1143      * Decode rest of the "big" levels
       
  1144      */
       
  1145 
       
  1146     for (i = numBigCoef - 2; i >= 0; i--) {
       
  1147 
       
  1148       level = getCoefLevelVLCN(bitbuf, tabNum);
       
  1149 
       
  1150       tmpLevel[i] = level;
       
  1151 
       
  1152       /* update VLC table number */
       
  1153       if (Abs((int32)level) > (int32)incVlc[tabNum])
       
  1154         tabNum++;
       
  1155     }
       
  1156 
       
  1157   }
       
  1158 
       
  1159   /*
       
  1160    * Get total number of zero coefficients
       
  1161    */
       
  1162 
       
  1163   if (totalCoef < 16-dcSkip) {
       
  1164 
       
  1165     bibShowMax16bits(bitbuf, 9, &bits);
       
  1166 
       
  1167     totalZerosPtr = &totalZerosTab[totalZerosTabOffset[totalCoef-1]];
       
  1168     code = totalZerosPtr[(int)bits>>6];
       
  1169 
       
  1170     if (code > 0xc0) {
       
  1171       offset = code - 0xc0;
       
  1172       code = totalZerosPtr[offset+(((int)bits>>3)&7)];
       
  1173       if (code > 0xc0) {
       
  1174         offset = code - 0xc0;
       
  1175         code = totalZerosPtr[offset+((int)bits&7)];
       
  1176       }
       
  1177     }
       
  1178 
       
  1179     totalZeros = code & 15;
       
  1180 
       
  1181     numBits = code >> 4;
       
  1182     bibSkipBits(bitbuf, numBits);
       
  1183   }
       
  1184   else
       
  1185     totalZeros = 0;
       
  1186 
       
  1187   if (dcSkip + totalCoef + totalZeros > 16)
       
  1188     return VLD_ERROR;
       
  1189 
       
  1190   /* All coefficients are initially zero */
       
  1191   for (i = 0; i < 4; i++) {
       
  1192     coef[i][0] = 0;
       
  1193     coef[i][1] = 0;
       
  1194     coef[i][2] = 0;
       
  1195     coef[i][3] = 0;
       
  1196   }
       
  1197 
       
  1198   /*
       
  1199    * Get run of zeros before each coefficient and store coeffs. in tmpCoef
       
  1200    */
       
  1201 
       
  1202   zerosLeft = totalZeros;
       
  1203   coefPos = dcSkip + totalCoef + totalZeros - 1;
       
  1204   coefNum = totalCoef - 1;
       
  1205 
       
  1206   while (zerosLeft > 0 && coefNum > 0) {
       
  1207 
       
  1208     coef[0][zigZagPos[coefPos]] = tmpLevel[coefNum];
       
  1209 
       
  1210     // select VLC for runbefore
       
  1211     tabNum = zerosLeft <= 7 ? zerosLeft-1 : 6;
       
  1212 
       
  1213     bibShowMax8bits(bitbuf, 3, &bits);
       
  1214 
       
  1215     if (tabNum == 6 && bits == 0) {
       
  1216       bibShowMax16bits(bitbuf, 11, &bits);
       
  1217       if (bits == 0)
       
  1218         return VLD_ERROR;
       
  1219       numLeadingZeroBits = numLeadZerosTab[(int)bits>>1];
       
  1220       run = 7 + numLeadingZeroBits;
       
  1221       if (run > zerosLeft)
       
  1222         return VLD_ERROR;
       
  1223       numBits = 4 + numLeadingZeroBits;
       
  1224     }
       
  1225     else {
       
  1226       code = runBeforeTab[tabNum][(int)bits];
       
  1227       run = code & 15;
       
  1228       numBits = code >> 4;
       
  1229     }
       
  1230 
       
  1231     bibSkipBits(bitbuf, numBits);
       
  1232 
       
  1233     zerosLeft -= run;
       
  1234     coefPos -= run + 1;
       
  1235     coefNum--;
       
  1236   }
       
  1237 
       
  1238   do {
       
  1239     coef[0][zigZagPos[coefPos]] = tmpLevel[coefNum];
       
  1240     coefPos--;
       
  1241     coefNum--;
       
  1242   } while (coefNum >= 0);
       
  1243 
       
  1244   return VLD_OK;
       
  1245 }
       
  1246 
       
  1247 
       
  1248 /*
       
  1249  *
       
  1250  * get2x2coefsCDC:
       
  1251  *
       
  1252  * Parameters:
       
  1253  *      bitbuf                Bitbuffer object
       
  1254  *      coef                  Return pointer for 2x2 coefficients
       
  1255  *      numCoef               Return pointer number of nonzero cefficients
       
  1256  *
       
  1257  * Function:
       
  1258  *      Decode coefficients for 2x2 DC block.
       
  1259  *
       
  1260  * Returns:
       
  1261  *      VLD_OK for no error, VLD_ERROR for error
       
  1262  *
       
  1263  */
       
  1264 static int get2x2coefsCDC(bitbuffer_s *bitbuf, int coef[4], int *numCoef)
       
  1265 {
       
  1266   int tabNum;
       
  1267   u_int32 bits;
       
  1268   int code;
       
  1269   int numTrailingOnes;
       
  1270   int numLeadingZeroBits;
       
  1271   int totalCoef;
       
  1272   int numBits;
       
  1273   int tmpLevel[4];
       
  1274   int level;
       
  1275   int numBigCoef;
       
  1276   u_int32 signs;
       
  1277   int s;
       
  1278   int i;
       
  1279   int totalZeros;
       
  1280   int zerosLeft;
       
  1281   int run;
       
  1282   int coefPos;
       
  1283   int coefNum;
       
  1284 
       
  1285   static const unsigned int incVlc[7] = {
       
  1286     0,3,6,12,24,48,32768          /* maximum vlc = 6 */
       
  1287   };
       
  1288 
       
  1289 
       
  1290   /*
       
  1291    * Decode number of coefficients and number of trailing ones
       
  1292    */
       
  1293 
       
  1294   bibShowMax16bits(bitbuf, 10, &bits);
       
  1295 
       
  1296   /* Compute number of leading zeros using look-up table */
       
  1297   numLeadingZeroBits = numLeadZerosTab[(int)bits>>(10-7)];
       
  1298 
       
  1299   /* Shift excess bits away */
       
  1300   bits >>= 10-3-numLeadingZeroBits;
       
  1301 
       
  1302   /* Fetch numTrailingOnes and totalCoef */
       
  1303   code = numCoefTrailTabChroma[numLeadingZeroBits][(int)bits&3];
       
  1304 
       
  1305   numTrailingOnes = code & 3;
       
  1306   totalCoef = (code >> 2) & 7;
       
  1307 
       
  1308   numBits = (code >> 5) + 1;
       
  1309 
       
  1310   bibSkipBits(bitbuf, numBits);
       
  1311 
       
  1312   *numCoef = totalCoef;
       
  1313 
       
  1314   if (totalCoef == 0)
       
  1315     return VLD_OK;
       
  1316 
       
  1317 
       
  1318   /*
       
  1319    * Decode signs for trailing ones
       
  1320    */
       
  1321 
       
  1322   numBigCoef = totalCoef - numTrailingOnes; 
       
  1323 
       
  1324   if (numTrailingOnes != 0) {
       
  1325 
       
  1326     /* Get signs for trailing ones. There can be maximum of 3 of them */
       
  1327     bibGetMax8bits(bitbuf, numTrailingOnes, &signs);
       
  1328 
       
  1329     for (i = numTrailingOnes-1; i >= 0; i--) {
       
  1330       s = ((int)signs >> i) & 1;
       
  1331       tmpLevel[numBigCoef+i] = 1 - 2*s;
       
  1332     }
       
  1333   }
       
  1334 
       
  1335   if (numBigCoef != 0) {
       
  1336 
       
  1337     /*
       
  1338      * Decode first level
       
  1339      */
       
  1340 
       
  1341     level = getCoefLevelVLC0(bitbuf);
       
  1342 
       
  1343     if (numTrailingOnes < 3)
       
  1344       level += (level > 0) ? 1 : -1;
       
  1345 
       
  1346     tmpLevel[numBigCoef-1] = level;
       
  1347 
       
  1348     tabNum = (Abs((int32)level) > 3) ? 2 : 1;
       
  1349 
       
  1350     /*
       
  1351      * Decode rest of the levels
       
  1352      */
       
  1353 
       
  1354     for (i = numBigCoef - 2; i >= 0; i--) {
       
  1355 
       
  1356       level = getCoefLevelVLCN(bitbuf, tabNum);
       
  1357 
       
  1358       tmpLevel[i] = level;
       
  1359 
       
  1360       /* update VLC table number */
       
  1361       if (Abs((int32)level) > (int32)incVlc[tabNum])
       
  1362         tabNum++;
       
  1363     }
       
  1364 
       
  1365   }
       
  1366 
       
  1367   /*
       
  1368    * Get total zeros
       
  1369    */
       
  1370 
       
  1371   if (totalCoef < 4) {
       
  1372     bibShowMax8bits(bitbuf, 3, &bits);
       
  1373     code = totalZerosTabChroma[totalCoef-1][(int)bits];
       
  1374     totalZeros = code & 15;
       
  1375     numBits = code >> 4;
       
  1376     bibSkipBits(bitbuf, numBits);
       
  1377   }
       
  1378   else
       
  1379     totalZeros = 0;
       
  1380 
       
  1381   /* All coefficients are initially zero */
       
  1382   coef[0] = 0;
       
  1383   coef[1] = 0;
       
  1384   coef[2] = 0;
       
  1385   coef[3] = 0;
       
  1386 
       
  1387   /*
       
  1388    * Get run before each coefficient
       
  1389    */
       
  1390 
       
  1391   zerosLeft = totalZeros;
       
  1392   coefPos = totalCoef + totalZeros - 1;
       
  1393   coefNum = totalCoef - 1;
       
  1394 
       
  1395   while (zerosLeft > 0 && coefNum > 0) {
       
  1396 
       
  1397     coef[coefPos] = tmpLevel[coefNum];
       
  1398 
       
  1399     bibShowMax8bits(bitbuf, 3, &bits);
       
  1400 
       
  1401     code = runBeforeTab[zerosLeft-1][(int)bits];
       
  1402     run = code & 15;
       
  1403     numBits = code >> 4;
       
  1404 
       
  1405     bibSkipBits(bitbuf, numBits);
       
  1406 
       
  1407     zerosLeft -= run;
       
  1408     coefPos -= run + 1;
       
  1409     coefNum--;
       
  1410   }
       
  1411 
       
  1412   do {
       
  1413     coef[coefPos] = tmpLevel[coefNum];
       
  1414     coefPos--;
       
  1415     coefNum--;
       
  1416   } while (coefNum >= 0);
       
  1417 
       
  1418 
       
  1419   return VLD_OK;
       
  1420 }
       
  1421 
       
  1422 
       
  1423 /*
       
  1424  *
       
  1425  * vldGetLumaDCcoeffs:
       
  1426  *
       
  1427  * Parameters:
       
  1428  *      bitbuf                Bitbuffer object
       
  1429  *      coef                  Return pointer for 4x4 coefficients
       
  1430  *      numCoefUpPred         Block coefficients counts of upper block
       
  1431  *      numCoefLeftPred       Block coefficient counts of blocks to the left
       
  1432  *      mbAvailBits           Macroblock availability flags
       
  1433  *
       
  1434  * Function:
       
  1435  *      Decode coefficients for 4x4 luma DC block.
       
  1436  *
       
  1437  * Returns:
       
  1438  *      VLD_OK for no error, negative value for error
       
  1439  *
       
  1440  */
       
  1441 int vldGetLumaDCcoeffs(bitbuffer_s *bitbuf, int coef[4][4],
       
  1442                        int8 *numCoefUpPred, int8 *numCoefLeftPred,
       
  1443                        int mbAvailBits)
       
  1444 {
       
  1445   int j;
       
  1446   int8 numCoef   = *numCoefUpPred;
       
  1447   int8 numCoefLP = *numCoefLeftPred;
       
  1448   int retCode;
       
  1449 
       
  1450   retCode = get4x4coefs(bitbuf, coef, 0, 0, &numCoef, &numCoefLP, mbAvailBits, 0);
       
  1451 
       
  1452   if (retCode < 0)
       
  1453     return retCode;
       
  1454 
       
  1455   if (numCoef == 0) {
       
  1456     for (j = 0; j < 4; j++) {
       
  1457       coef[j][0] = 0;
       
  1458       coef[j][1] = 0;
       
  1459       coef[j][2] = 0;
       
  1460       coef[j][3] = 0;
       
  1461     }
       
  1462   }
       
  1463 
       
  1464   if (bibGetStatus(bitbuf) < 0)
       
  1465     return VLD_ERROR;
       
  1466   else
       
  1467     return VLD_OK;
       
  1468 }
       
  1469 
       
  1470 
       
  1471 /*
       
  1472  *
       
  1473  * vldGetLumaCoeffs:
       
  1474  *
       
  1475  * Parameters:
       
  1476  *      bitbuf                Bitbuffer object
       
  1477  *      mbType                Macroblock type (intra/inter)
       
  1478  *      intraType             Intra type (16x16 or 4x4 intra pred)
       
  1479  *      cbpY                  Return pointer coded block pattern
       
  1480  *      coef                  Return pointer for 16x4x4 coefficients
       
  1481  *      numCoefUpPred         Block coefficients counts of upper block
       
  1482  *      numCoefLeftPred       Block coefficient counts of blocks to the left
       
  1483  *      mbAvailBits           Macroblock availability flags
       
  1484  *
       
  1485  * Function:
       
  1486  *      Decode coefficients for 16 4x4 luma blocks.
       
  1487  *
       
  1488  * Returns:
       
  1489  *      VLD_OK for no error, negative value for error
       
  1490  *
       
  1491  */
       
  1492 int vldGetLumaCoeffs(bitbuffer_s *bitbuf, int mbType, int intraType,
       
  1493                      int *cbpY, int coef[4][4][4][4], int8 *numCoefUpPred,
       
  1494                      int8 *numCoefLeftPred, int mbAvailBits)
       
  1495 {
       
  1496   int bx, by;
       
  1497   int blkIdxX, blkIdxY;
       
  1498   int i, j;
       
  1499   int cbp, cbpTemp;
       
  1500   int dcSkip;
       
  1501   int retCode;
       
  1502 
       
  1503   dcSkip = mbType == MBK_INTRA && intraType == MBK_INTRA_TYPE2 ? 1 : 0;
       
  1504 
       
  1505   cbp = cbpTemp = *cbpY;
       
  1506 
       
  1507   for (by = 0; by < 4; by+=2) {
       
  1508     for (bx = 0; bx < 4; bx+=2) {
       
  1509 
       
  1510       /* Check if any of the blocks in the current 8x8 block is codec */
       
  1511       if ((cbpTemp & 33) != 0) {
       
  1512         for (j = 0; j < 2; j++) {
       
  1513           for (i = 0; i < 2; i++) {
       
  1514 
       
  1515             blkIdxX = bx + i;
       
  1516             blkIdxY = by + j;
       
  1517 
       
  1518             retCode = get4x4coefs(bitbuf, coef[blkIdxY][blkIdxX], blkIdxX, blkIdxY,
       
  1519                                   &numCoefUpPred[blkIdxX], &numCoefLeftPred[blkIdxY],
       
  1520                                   mbAvailBits, dcSkip);
       
  1521 
       
  1522             if (retCode < 0)
       
  1523               return retCode;
       
  1524 
       
  1525             if (numCoefUpPred[blkIdxX] == 0)
       
  1526               cbp &= ~(1<<(blkIdxY*BLK_PER_MB+blkIdxX));
       
  1527           }
       
  1528 
       
  1529         }
       
  1530       }
       
  1531       else {
       
  1532         numCoefUpPred[bx]   = 0;
       
  1533         numCoefUpPred[bx+1] = 0;
       
  1534         numCoefLeftPred[by]   = 0;
       
  1535         numCoefLeftPred[by+1] = 0;
       
  1536       }
       
  1537 
       
  1538       cbpTemp >>= 2;
       
  1539     }
       
  1540     cbpTemp >>= 4;
       
  1541   }
       
  1542 
       
  1543   *cbpY = cbp;
       
  1544 
       
  1545   if (bibGetStatus(bitbuf) < 0)
       
  1546     return VLD_ERROR;
       
  1547   else
       
  1548     return VLD_OK;
       
  1549 }
       
  1550 
       
  1551 
       
  1552 /*
       
  1553  *
       
  1554  * vldGetChromaDCcoeffs:
       
  1555  *
       
  1556  * Parameters:
       
  1557  *      bitbuf                Bitbuffer object
       
  1558  *      coef                  Return pointer for 2x2x2 coefficients
       
  1559  *      cbpYDC                Return pointer chroma DC coded block pattern
       
  1560  *
       
  1561  * Function:
       
  1562  *      Decode coefficients for 2 2x2 chroma DC blocks.
       
  1563  *
       
  1564  * Returns:
       
  1565  *      VLD_OK for no error, negative value for error
       
  1566  *
       
  1567  */
       
  1568 int vldGetChromaDCcoeffs(bitbuffer_s *bitbuf, int coef[2][2][2], int *cbpDC)
       
  1569 {
       
  1570   int comp;
       
  1571   int numCoef;
       
  1572   int retCode;
       
  1573 
       
  1574   for (comp = 0; comp < 2; comp++) {
       
  1575 
       
  1576     retCode = get2x2coefsCDC(bitbuf, &coef[comp][0][0], &numCoef);
       
  1577 
       
  1578     if (retCode < 0)
       
  1579       return retCode;
       
  1580       
       
  1581     if (numCoef == 0)
       
  1582       *cbpDC &= ~(1<<comp);
       
  1583   }
       
  1584 
       
  1585   if (bibGetStatus(bitbuf) < 0)
       
  1586     return VLD_ERROR;
       
  1587   else
       
  1588     return VLD_OK;
       
  1589 }
       
  1590 
       
  1591 
       
  1592 /*
       
  1593  *
       
  1594  * vldGetChromaCoeffs:
       
  1595  *
       
  1596  * Parameters:
       
  1597  *      bitbuf                Bitbuffer object
       
  1598  *      coef                  Return pointer for 2x2x2x4x4 coefficients
       
  1599  *      cbp                   Return pointer chroma coded block pattern
       
  1600  *      numCoefUpPred         Block coefficients counts of upper block for U frame
       
  1601  *      numCoefUpPredV        Block coefficients counts of upper block for V frame
       
  1602  *      numCoefLeftPred       Block coefficient counts of blocks to the left for U frame
       
  1603  *      numCoefLeftPredV      Block coefficient counts of blocks to the left for V frame
       
  1604  *      mbAvailBits           Macroblock availability flags
       
  1605  *
       
  1606  * Function:
       
  1607  *      Decode coefficients for 8 4x4 chroma blocks.
       
  1608  *
       
  1609  * Returns:
       
  1610  *      VLD_OK for no error, negative value for error
       
  1611  *
       
  1612  */
       
  1613 int vldGetChromaCoeffs(bitbuffer_s *bitbuf, int coef[2][2][2][4][4], int *cbp,
       
  1614                        int8 *numCoefUpPred, int8 *numCoefUpPredV,
       
  1615                        int8 *numCoefLeftPred, int8 *numCoefLeftPredV,
       
  1616                        int mbAvailBits)
       
  1617 {
       
  1618   int comp;
       
  1619   int i, j;
       
  1620   int retCode;
       
  1621 
       
  1622   for (comp = 0; comp < 2; comp++) {
       
  1623 
       
  1624     for (j = 0; j < 2; j++) {
       
  1625       for (i = 0; i < 2; i++) {
       
  1626 
       
  1627         retCode = get4x4coefs(bitbuf, coef[comp][j][i], i, j, &numCoefUpPred[i],
       
  1628                               &numCoefLeftPred[j], mbAvailBits, 1);
       
  1629 
       
  1630         if (retCode < 0)
       
  1631           return retCode;
       
  1632 
       
  1633         if (numCoefUpPred[i] == 0)
       
  1634           *cbp &= ~(1<<(comp*4+j*2+i));
       
  1635       }
       
  1636 
       
  1637     }
       
  1638 
       
  1639     /* Switch to V frame */
       
  1640     numCoefUpPred = numCoefUpPredV;
       
  1641     numCoefLeftPred = numCoefLeftPredV;
       
  1642   }
       
  1643 
       
  1644   if (bibGetStatus(bitbuf) < 0)
       
  1645     return VLD_ERROR;
       
  1646   else
       
  1647     return VLD_OK;
       
  1648 }
       
  1649 
       
  1650 
       
  1651 /*
       
  1652  *
       
  1653  * vldGetZeroLumaCoeffs:
       
  1654  *
       
  1655  * Parameters:
       
  1656  *      numCoefUpPred         Block coefficient counts of upper block
       
  1657  *      numCoefLeftPred       Block coefficient counts of blocks to the left
       
  1658  *
       
  1659  * Function:
       
  1660  *      Called when there are no luma coefficients.
       
  1661  *      Sets luma cefficient counts to zero for current MB.
       
  1662  *
       
  1663  * Returns:
       
  1664  *      -
       
  1665  */
       
  1666 void vldGetZeroLumaCoeffs(int8 *numCoefUpPred, int8 *numCoefLeftPred)
       
  1667 {
       
  1668   int i;
       
  1669 
       
  1670   for (i = 0; i < 4; i++) {
       
  1671     numCoefUpPred[i] = 0;
       
  1672     numCoefLeftPred[i] = 0;
       
  1673   }
       
  1674 }
       
  1675 
       
  1676 
       
  1677 /*
       
  1678  *
       
  1679  * vldGetZeroChromaCoeffs:
       
  1680  *
       
  1681  * Parameters:
       
  1682  *      numCoefUpPredU        Block coefficient counts of upper block for U frame
       
  1683  *      numCoefUpPredV        Block coefficient counts of upper block for V frame
       
  1684  *      numCoefLeftPred       Block coefficient counts of blocks to the left
       
  1685  *
       
  1686  * Function:
       
  1687  *      Called when there are no chroma coefficients.
       
  1688  *      Sets chroma cefficient counts to zero for current MB.
       
  1689  *
       
  1690  * Returns:
       
  1691  *      -
       
  1692  */
       
  1693 void vldGetZeroChromaCoeffs(int8 *numCoefUpPredU, int8 *numCoefUpPredV,
       
  1694                             int8 numCoefLeftPred[2][2])
       
  1695 {
       
  1696   int i;
       
  1697 
       
  1698   for (i = 0; i < 2; i++) {
       
  1699     numCoefUpPredU[i] = 0;
       
  1700     numCoefUpPredV[i] = 0;
       
  1701     numCoefLeftPred[0][i] = 0;
       
  1702     numCoefLeftPred[1][i] = 0;
       
  1703   }
       
  1704 }
       
  1705 
       
  1706 
       
  1707 /*
       
  1708  *
       
  1709  * vldGetAllCoeffs:
       
  1710  *
       
  1711  * Parameters:
       
  1712  *      numCoefUpPredY        Block coefficient counts for Y frame
       
  1713  *      numCoefUpPredU        Block coefficient counts for U frame
       
  1714  *      numCoefUpPredV        Block coefficient counts for V frame
       
  1715  *      numCoefLeftPredY      Luma block coefficient counts of blocks to the left
       
  1716  *      numCoefLeftPredC      Chroma block coefficient counts of blocks to the left
       
  1717  *
       
  1718  * Function:
       
  1719  *      Called when all coefficients are non-zero (e.g. PCM mactroblock).
       
  1720  *      Sets cefficient counts to "all coded".
       
  1721  *
       
  1722  * Returns:
       
  1723  *      -
       
  1724  */
       
  1725 void vldGetAllCoeffs(int8 *numCoefUpPredY, int8 *numCoefUpPredU,
       
  1726                      int8 *numCoefUpPredV, int8 *numCoefLeftPredY,
       
  1727                      int8 numCoefLeftPredC[2][2])
       
  1728 {
       
  1729   int i;
       
  1730 
       
  1731   for (i = 0; i < 4; i++) {
       
  1732     numCoefUpPredY[i] = 16;
       
  1733     numCoefLeftPredY[i] = 16;
       
  1734   }
       
  1735 
       
  1736   for (i = 0; i < 2; i++) {
       
  1737     numCoefUpPredU[i] = 16;
       
  1738     numCoefUpPredV[i] = 16;
       
  1739     numCoefLeftPredC[0][i] = 16;
       
  1740     numCoefLeftPredC[1][i] = 16;
       
  1741   }
       
  1742 }
       
  1743 
       
  1744 
       
  1745 /*
       
  1746  *
       
  1747  * vldSetUVLC:
       
  1748  *
       
  1749  * Parameters:
       
  1750  *      codeNumber         value to encode - range [0, 65535]
       
  1751  *      codeword           output codeword
       
  1752  *      codewordLength     length of output codeword
       
  1753  *
       
  1754  * Function:
       
  1755  *      Encode a value to an UVLC codeword, UVLC codeword is of the form:
       
  1756  *      code          codeword
       
  1757  *      0                1
       
  1758  *      1               010
       
  1759  *      2               011
       
  1760  *      3              00100
       
  1761  *      4              00101
       
  1762  *      5              00110
       
  1763  *      6              00111
       
  1764  *      7             0001000
       
  1765  *      8             0001001
       
  1766  *      ...             ...
       
  1767  *
       
  1768  * Returns:
       
  1769  *      1 on error, 0 on none    
       
  1770  *
       
  1771  */
       
  1772 int vldSetUVLC(int codeNumber, int* codeword, int* codewordLength)
       
  1773 {
       
  1774   int c;
       
  1775   int bits;
       
  1776 
       
  1777   if (codeNumber < 0 || codeNumber > 65535)
       
  1778   	return 1; 
       
  1779   	
       
  1780   c = codeNumber+1; 
       
  1781   bits = 0;
       
  1782   
       
  1783   do 
       
  1784   {
       
  1785     c >>= 1;
       
  1786     bits++;
       
  1787   }
       
  1788   while (c);
       
  1789   
       
  1790   *codeword = codeNumber + 1;
       
  1791   *codewordLength = (bits << 1) - 1;
       
  1792   
       
  1793   return 0;
       
  1794 }
       
  1795  
       
  1796 #endif  //VIDEOEDITORENGINE_AVC_EDITING