videoeditorengine/h263decoder/src/vlb.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     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 * Implementation for MPEG4(H263) video transcoder. 
       
    17 * VLC encoding for RVLC -> VLC  AC/DCT coeficients in the transcoder.
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 #include "biblin.h"
       
    23 #include "common.h"
       
    24 
       
    25 
       
    26 /* Run-length coding */
       
    27 typedef struct{
       
    28     int16 run;
       
    29     int level;
       
    30     int16 last;
       
    31 } tEvent;
       
    32 
       
    33 /* Variable length coding */
       
    34 typedef struct{
       
    35     u_int16 code;
       
    36     u_int16 length;
       
    37 } tVLCTable;
       
    38 
       
    39 
       
    40 /* 
       
    41  * Function Declarations 
       
    42  */
       
    43 int32 vlbPutACCoeffSVH(int32 coeffStart, int16* block, bibBuffer_t * outBuf,
       
    44                        int32 svh, int32 lastPos);
       
    45 int32 vlbPutACVLCCoeff(bibBuffer_t *outBuf, tEvent *event, int32 svh);
       
    46 int32 vlbPutDCCoeffCMT(int val, bibBuffer_t * outBuf, int32  blkCnt);
       
    47 int32 vlbPutIntraACCoeffCMTDP(bibBuffer_t *outBuf, tEvent *event);
       
    48 int32 vlbPutIntraACCoeffCMT(int32 coeffStart, int* block, bibBuffer_t * outBuf);
       
    49 void  vdtPutBits (void *outBuf,  int numBits, unsigned int value);
       
    50 
       
    51 
       
    52 /* 
       
    53  * Function Definitions 
       
    54  */
       
    55 
       
    56 
       
    57 /* {{-output"vlbPutBits.txt"}} */
       
    58 /*
       
    59 * vlbPutBits
       
    60 *
       
    61 * Parameters:
       
    62 *     outBuf            output buffer
       
    63 *           numBits         number of bits to write
       
    64 *           value             value to write
       
    65 *
       
    66 * Function:
       
    67 *     This function puts some bits to the output buffer
       
    68 *
       
    69 * Returns:
       
    70 *     Nothing.
       
    71 *
       
    72 * Error codes:
       
    73 *     None.
       
    74 *
       
    75 */
       
    76 void vlbPutBits (bibBuffer_t *outBuf,
       
    77                  int32        numBits,
       
    78                  u_int32      value)
       
    79 {
       
    80     // To be compatible with the old codes, I don't change the interface
       
    81     vdtPutBits((void *)outBuf, numBits, value);
       
    82 }
       
    83 
       
    84 
       
    85 /* 
       
    86  * Look-up tables 
       
    87  */
       
    88 
       
    89 /* INTRA VLC tables */
       
    90 
       
    91 /* run == 0 && level < 28 */
       
    92 const tVLCTable vlbVLCTable1IntraAC[28] =
       
    93 {
       
    94     {NOT_VALID,NOT_VALID},    {0x04,0x03}, {0x0c,0x04}, 
       
    95         {0x1e,0x05}, {0x1a,0x06}, {0x18,0x06}, {0x2a,0x07}, 
       
    96         {0x26,0x07}, {0x24,0x07}, {0x2e,0x08}, {0x3e,0x09}, 
       
    97         {0x3c,0x09}, {0x3a,0x09}, {0x4a,0x0a}, {0x48,0x0a},
       
    98         {0x46,0x0a}, {0x42,0x0a}, {0x42,0x0b}, {0x40,0x0b},
       
    99         {0x1e,0x0b}, {0x1c,0x0b}, {0x0e,0x0c}, {0x0c,0x0c},
       
   100         {0x40,0x0c}, {0x42,0x0c}, {0xa0,0x0d}, {0xa2,0x0d},
       
   101         {0xa4,0x0d}
       
   102 };
       
   103 
       
   104 /* run == 1 && level < 11 */
       
   105 const tVLCTable vlbVLCTable2IntraAC[11] =
       
   106 {
       
   107     {NOT_VALID, NOT_VALID},   {0x1c,0x05}, {0x28,0x07}, 
       
   108         {0x2c,0x08}, {0x38,0x09}, {0x40,0x0a}, {0x3e,0x0a}, 
       
   109         {0x1a,0x0b}, {0x44,0x0c}, {0xa6,0x0d}, {0xaa,0x0d}
       
   110 };
       
   111 
       
   112 /* run == 2 && level < 6 */
       
   113 const tVLCTable vlbVLCTable3IntraAC[6] =
       
   114 {
       
   115     {NOT_VALID, NOT_VALID},   {0x16,0x06}, {0x2a,0x08}, 
       
   116         {0x3c,0x0a}, {0x18,0x0b}, {0xac,0x0d}
       
   117 };
       
   118 
       
   119 /* run == 3 && level < 5 */
       
   120 const tVLCTable vlbVLCTable4IntraAC[5] = 
       
   121 {
       
   122     {NOT_VALID, NOT_VALID},   {0x22,0x07}, {0x36,0x09}, 
       
   123         {0x3a,0x0a}, {0x16,0x0b}
       
   124 };
       
   125 
       
   126 /* 4 <= run < 8 && level < 4 */
       
   127 const tVLCTable vlbVLCTable5IntraAC[4][4] =
       
   128 {
       
   129   {
       
   130         {NOT_VALID, NOT_VALID},   {0x20,0x07}, {0x44,0x0a}, 
       
   131         {0x14,0x0b}
       
   132   },
       
   133   {
       
   134     {NOT_VALID, NOT_VALID},   {0x1a,0x07}, {0x38,0x0a}, 
       
   135         {0x10,0x0b}
       
   136   },
       
   137   {
       
   138     {NOT_VALID, NOT_VALID},   {0x24,0x08}, {0x36,0x0a}, 
       
   139         {0xa8,0x0d}
       
   140   },
       
   141   {
       
   142     {NOT_VALID, NOT_VALID},   {0x28,0x08}, {0x34,0x0a}, 
       
   143         {0xae,0x0d}
       
   144   }
       
   145 };
       
   146 
       
   147 /* 8 <= run < 10 && level < 3 */
       
   148 const tVLCTable vlbVLCTable6IntraAC[2][3] =
       
   149 {
       
   150   {
       
   151         {NOT_VALID, NOT_VALID},   {0x32,0x09}, {0x12,0x0b}
       
   152   },
       
   153   {
       
   154     {NOT_VALID, NOT_VALID},   {0x30,0x09}, {0x46,0x0c}
       
   155   }
       
   156 };
       
   157 
       
   158 /* 10 <= run < 15 && level < 2 */
       
   159 const tVLCTable vlbVLCTable7IntraAC[5][2] = 
       
   160 {
       
   161   {
       
   162     {NOT_VALID, NOT_VALID},   {0x2e,0x09}
       
   163   },
       
   164   {
       
   165     {NOT_VALID, NOT_VALID},   {0x32,0x0a}
       
   166   },
       
   167   {
       
   168     {NOT_VALID, NOT_VALID},   {0x30,0x0a}
       
   169   },
       
   170   {
       
   171     {NOT_VALID, NOT_VALID},   {0x0e,0x0b}
       
   172   },
       
   173   {
       
   174     {NOT_VALID, NOT_VALID},   {0xb0,0x0d}
       
   175   }
       
   176 };
       
   177 
       
   178 /* run == 0 && level < 9 */
       
   179 const tVLCTable vlbVLCTable8IntraAC[9] = 
       
   180 {
       
   181     {NOT_VALID, NOT_VALID},   {0x0e,0x05}, {0x18,0x07}, 
       
   182         {0x2c,0x09}, {0x2e,0x0a}, {0x0c,0x0b}, {0x0a,0x0c},
       
   183         {0x08,0x0c}, {0xb2,0x0d}
       
   184 };
       
   185 
       
   186 /* run == 1 && level < 4 */
       
   187 const tVLCTable vlbVLCTable9IntraAC[4] =
       
   188 {
       
   189     {NOT_VALID, NOT_VALID},   {0x1e,0x07}, {0x2c,0x0a}, 
       
   190         {0x0a,0x0b}
       
   191 };
       
   192 
       
   193 /* 2 <= run < 7 && level < 3 */
       
   194 const tVLCTable vlbVLCTable10IntraAC[5][3] =
       
   195 {
       
   196   {
       
   197     {NOT_VALID, NOT_VALID},   {0x1c,0x07}, {0x08,0x0b}
       
   198   },
       
   199   {
       
   200     {NOT_VALID, NOT_VALID},   {0x22,0x08}, {0x48,0x0c}
       
   201   },
       
   202   {
       
   203     {NOT_VALID, NOT_VALID},   {0x20,0x08}, {0x4a,0x0c}
       
   204   },
       
   205   {
       
   206     {NOT_VALID, NOT_VALID},   {0x26,0x08}, {0xb4,0x0d}
       
   207   },
       
   208   {
       
   209     {NOT_VALID, NOT_VALID},   {0x2a,0x09}, {0xb6,0x0d}
       
   210   }
       
   211 };
       
   212 
       
   213 /* 7 <= run < 21 && level == 1 */
       
   214 const tVLCTable vlbVLCTable11IntraAC[14][2] =
       
   215 {
       
   216   {
       
   217     {NOT_VALID, NOT_VALID},   {0x28,0x09}
       
   218   },
       
   219   {
       
   220     {NOT_VALID, NOT_VALID},   {0x26,0x09}
       
   221   },
       
   222   {
       
   223     {NOT_VALID, NOT_VALID},   {0x34,0x09}
       
   224   },
       
   225   {
       
   226     {NOT_VALID, NOT_VALID},   {0x2a,0x0a}
       
   227   },
       
   228   {
       
   229     {NOT_VALID, NOT_VALID},   {0x28,0x0a}
       
   230   },
       
   231   {
       
   232     {NOT_VALID, NOT_VALID},   {0x26,0x0a}
       
   233   },
       
   234   {
       
   235     {NOT_VALID, NOT_VALID},   {0x24,0x0a}
       
   236   },
       
   237   {
       
   238     {NOT_VALID, NOT_VALID},   {0x22,0x0a}
       
   239   },
       
   240   {
       
   241     {NOT_VALID, NOT_VALID},   {0x4c,0x0c}
       
   242   },
       
   243   {
       
   244     {NOT_VALID, NOT_VALID},   {0x4e,0x0c}
       
   245   },
       
   246   {
       
   247     {NOT_VALID, NOT_VALID},   {0xb8,0x0d}
       
   248   },
       
   249   {
       
   250     {NOT_VALID, NOT_VALID},   {0xba,0x0d}
       
   251   },
       
   252   {
       
   253     {NOT_VALID, NOT_VALID},   {0xbc,0x0d}
       
   254   },
       
   255   { 
       
   256     {NOT_VALID, NOT_VALID},   {0xbe,0x0d}
       
   257   }
       
   258 };
       
   259 
       
   260 
       
   261 /* Inter AC VLC tables */
       
   262 
       
   263 const tVLCTable vlbVLCTable1InterAC[13] =
       
   264 {
       
   265     {NOT_VALID, NOT_VALID},
       
   266     {0x04,0x03}, {0x1e,0x05}, {0x2a,0x07}, {0x2e,0x08},
       
   267     {0x3e,0x09}, {0x4a,0x0a}, {0x48,0x0a}, {0x42,0x0b},
       
   268     {0x40,0x0b}, {0x0e,0x0c}, {0x0c,0x0c}, {0x40,0x0c}
       
   269 };
       
   270 
       
   271 const tVLCTable vlbVLCTable2InterAC[7] =
       
   272 {
       
   273     {NOT_VALID, NOT_VALID},   {0x0c,0x04}, {0x28,0x07}, 
       
   274         {0x3c,0x09}, {0x1e,0x0b}, {0x42,0x0c}, {0xa0,0x0d}
       
   275 };
       
   276 
       
   277 const tVLCTable vlbVLCTable3InterAC[5] =
       
   278 {
       
   279     {NOT_VALID, NOT_VALID},   {0x1c,0x05}, {0x3a,0x09}, 
       
   280         {0x1c,0x0b}, {0xa2,0x0d}
       
   281 };
       
   282 
       
   283 const tVLCTable vlbVLCTable4InterAC[4][4] = 
       
   284 {
       
   285   {
       
   286     {NOT_VALID, NOT_VALID},   {0x1a,0x06}, {0x46,0x0a}, 
       
   287         {0x1a,0x0b}
       
   288   },
       
   289   {
       
   290     {NOT_VALID, NOT_VALID},   {0x18,0x06}, {0x44,0x0a}, 
       
   291         {0xa4,0x0d}
       
   292   },
       
   293   {
       
   294     {NOT_VALID, NOT_VALID},   {0x16,0x06}, {0x18,0x0b}, 
       
   295         {0xa6,0x0d}
       
   296   },
       
   297   {
       
   298     {NOT_VALID, NOT_VALID},   {0x26,0x07}, {0x16,0x0b}, 
       
   299         {0xa8,0x0d}
       
   300   }
       
   301 };
       
   302 
       
   303 const tVLCTable vlbVLCTable5InterAC[4][3] =
       
   304 {
       
   305   {
       
   306     {NOT_VALID, NOT_VALID},   {0x24,0x07}, {0x14,0x0b}
       
   307   },
       
   308   {
       
   309     {NOT_VALID, NOT_VALID},   {0x22,0x07}, {0x12,0x0b}
       
   310   },
       
   311   {
       
   312     {NOT_VALID, NOT_VALID},   {0x20,0x07}, {0x10,0x0b}
       
   313   },
       
   314   {
       
   315     {NOT_VALID, NOT_VALID},   {0x2c,0x08}, {0xaa,0x0d}
       
   316   }
       
   317 };
       
   318 
       
   319 const tVLCTable vlbVLCTable6InterAC[16] =
       
   320 {
       
   321     {0x2a,0x08}, {0x28,0x08}, {0x38,0x09}, {0x36,0x09},
       
   322     {0x42,0x0a}, {0x40,0x0a}, {0x3e,0x0a}, {0x3c,0x0a}, 
       
   323         {0x3a,0x0a}, {0x38,0x0a}, {0x36,0x0a}, {0x34,0x0a}, 
       
   324         {0x44,0x0c}, {0x46,0x0c}, {0xac,0x0d}, {0xae,0x0d}
       
   325 };
       
   326 
       
   327 const tVLCTable vlbVLCTable7InterAc[4] =
       
   328 {
       
   329     {NOT_VALID, NOT_VALID},   {0x0e,0x05}, {0x32,0x0a}, 
       
   330         {0x0a,0x0c}
       
   331 };
       
   332 
       
   333 const tVLCTable vlbVLCTable8InterAC[3] =
       
   334 {
       
   335     {NOT_VALID, NOT_VALID},   {0x1e,0x07}, {0x08,0x0c}
       
   336 };
       
   337 
       
   338 const tVLCTable vlbVLCTable9InterAC[39] =
       
   339 {
       
   340     {0x1c,0x07}, {0x1a,0x07}, {0x18,0x07}, {0x26,0x08},
       
   341     {0x24,0x08}, {0x22,0x08}, {0x20,0x08}, {0x34,0x09},
       
   342     {0x32,0x09}, {0x30,0x09}, {0x2e,0x09}, {0x2c,0x09},
       
   343     {0x2a,0x09}, {0x28,0x09}, {0x26,0x09}, {0x30,0x0a},
       
   344     {0x2e,0x0a}, {0x2c,0x0a}, {0x2a,0x0a}, {0x28,0x0a},
       
   345     {0x26,0x0a}, {0x24,0x0a}, {0x22,0x0a}, {0x0e,0x0b},
       
   346     {0x0c,0x0b}, {0x0a,0x0b}, {0x08,0x0b}, {0x48,0x0c},
       
   347     {0x4a,0x0c}, {0x4c,0x0c}, {0x4e,0x0c}, {0xb0,0x0d}, 
       
   348         {0xb2,0x0d}, {0xb4,0x0d}, {0xb6,0x0d}, {0xb8,0x0d}, 
       
   349         {0xba,0x0d}, {0xbc,0x0d}, {0xbe,0x0d}
       
   350 };
       
   351 
       
   352 
       
   353 const int8 vlbVLCRun0InterAC[64] =
       
   354 {
       
   355     0x0d, 0x07, 0x05, 0x04, 0x04, 0x04, 0x04, 0x03, 
       
   356       0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   357       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   358       0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
       
   359       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       
   360       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       
   361       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       
   362       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
       
   363 };
       
   364 
       
   365 const int8 vlbVLCRun1InterAC[64] =
       
   366 {
       
   367       0x04, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   368       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   369       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   370       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   371       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   372       0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   373       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   374       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
       
   375 };
       
   376 
       
   377 /* other tables */
       
   378 const int8 vlbLMaxTableIntra[36] =
       
   379 {
       
   380     0x1b, 0x0a, 0x05, 0x04, 0x03, 0x03, 
       
   381         0x03, 0x03, 0x02, 0x02, 0x01, 0x01, 
       
   382         0x01, 0x01, 0x01, 0x08, 0x03, 0x02, 
       
   383         0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 
       
   384         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   385         0x01, 0x01, 0x01, 0x01, 0x01, 0x01
       
   386 };
       
   387 
       
   388 const int8 vlbLMaxTableInter[68] =
       
   389 {
       
   390     0x0c, 0x06, 0x04, 0x03, 0x03, 0x03, 
       
   391         0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 
       
   392         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   393         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   394         0x01, 0x01, 0x01, 0x03, 0x02, 0x01, 
       
   395         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   396         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   397         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   398         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   399         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   400         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   401         0x01, 0x01
       
   402 };
       
   403 
       
   404 const int8 vlbRMaxTableIntra[35] =
       
   405 {
       
   406     0x0f, 0x0a, 0x08, 0x04, 0x03, 0x02, 
       
   407         0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 
       
   408         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   409     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
       
   410         0x01, 0x01, 0x01, 0x15, 0x07, 0x02,
       
   411     0x01, 0x01, 0x01, 0x01, 0x01
       
   412 };
       
   413 
       
   414 const int8 vlbRMaxTableInter[15] =
       
   415 {
       
   416     0x1b, 0x0b, 0x07, 0x03, 0x02, 0x02,
       
   417     0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
       
   418     0x29, 0x02, 0x01
       
   419 };
       
   420 
       
   421 
       
   422 const u_int32 vlbZigzagScan[64] = 
       
   423 {
       
   424     0x00, 0x01, 0x08, 0x10, 0x09, 0x02, 0x03, 0x0a,
       
   425     0x11, 0x18, 0x20, 0x19, 0x12, 0x0b, 0x04, 0x05,
       
   426     0x0c, 0x13, 0x1a, 0x21, 0x28, 0x30, 0x29, 0x22,
       
   427     0x1b, 0x14, 0x0d, 0x06, 0x07, 0x0e, 0x15, 0x1c,
       
   428     0x23, 0x2a, 0x31, 0x38, 0x39, 0x32, 0x2b, 0x24,
       
   429     0x1d, 0x16, 0x0f, 0x17, 0x1e, 0x25, 0x2c, 0x33,
       
   430     0x3a, 0x3b, 0x34, 0x2d, 0x26, 0x1f, 0x27, 0x2e,
       
   431     0x35, 0x3c, 0x3d, 0x36, 0x2f, 0x37, 0x3e, 0x3f
       
   432 };
       
   433 
       
   434 const u_int32 vlbInvZigzagScan[64] = 
       
   435 {
       
   436     0x00, 0x01, 0x05, 0x06, 0x0e, 0x0f, 0x1b, 0x1c,
       
   437     0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1d, 0x2a,
       
   438     0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2b,
       
   439     0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2c, 0x35,
       
   440     0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x36,
       
   441     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x37, 0x3c,
       
   442     0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3d,
       
   443     0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3e, 0x3f
       
   444 };
       
   445 
       
   446 
       
   447 const tVLCTable vlbLumaTableIntraDC[13] = 
       
   448 {
       
   449     {0x03,0x03}, {0x03,0x02}, {0x02,0x02}, {0x02,0x03}, 
       
   450         {0x01,0x03}, {0x01,0x04}, {0x01,0x05}, {0x01,0x06}, 
       
   451         {0x01,0x07}, {0x01,0x08}, {0x01,0x09}, {0x01,0x0a}, 
       
   452         {0x01,0x0b}
       
   453 };
       
   454 
       
   455 const tVLCTable vlbChromaTableIntraDC[13] =
       
   456 {
       
   457     {0x03,0x02}, {0x02,0x02}, {0x01,0x02}, {0x01,0x03}, 
       
   458         {0x01,0x04}, {0x01,0x05}, {0x01,0x06}, {0x01,0x07}, 
       
   459         {0x01,0x08}, {0x01,0x09}, {0x01,0x0a}, {0x01,0x0b}
       
   460 };
       
   461 
       
   462 /* Array of pointers for AC VLC (INTER)tables */
       
   463 const tVLCTable * const vlbVLCTablePointer0[27] =
       
   464 {
       
   465     vlbVLCTable1InterAC,
       
   466     vlbVLCTable2InterAC,
       
   467     vlbVLCTable3InterAC,
       
   468     vlbVLCTable4InterAC[0],
       
   469     vlbVLCTable4InterAC[1],
       
   470     vlbVLCTable4InterAC[2],
       
   471     vlbVLCTable4InterAC[3],
       
   472     vlbVLCTable5InterAC[0],
       
   473     vlbVLCTable5InterAC[1],
       
   474     vlbVLCTable5InterAC[2],
       
   475     vlbVLCTable5InterAC[3],
       
   476     vlbVLCTable6InterAC - 0x01, // this pointer is going outside the range, but it is used only with condition (level < vlbVLCRun0InterAC[run]) which results having indexes > 0 and hence the array is never indexed below its base address
       
   477     vlbVLCTable6InterAC ,
       
   478     vlbVLCTable6InterAC + 0x01,
       
   479     vlbVLCTable6InterAC + 0x02,
       
   480     vlbVLCTable6InterAC + 0x03,
       
   481     vlbVLCTable6InterAC + 0x04,
       
   482     vlbVLCTable6InterAC + 0x05,
       
   483     vlbVLCTable6InterAC + 0x06,
       
   484     vlbVLCTable6InterAC + 0x07,
       
   485     vlbVLCTable6InterAC + 0x08,
       
   486     vlbVLCTable6InterAC + 0x09,
       
   487     vlbVLCTable6InterAC + 0x0a,
       
   488     vlbVLCTable6InterAC + 0x0b,
       
   489     vlbVLCTable6InterAC + 0x0c,
       
   490     vlbVLCTable6InterAC + 0x0d,
       
   491     vlbVLCTable6InterAC + 0x0e
       
   492 
       
   493 };
       
   494 
       
   495 const tVLCTable * const vlbVLCTablePointer1[41] =
       
   496 {
       
   497     vlbVLCTable7InterAc,
       
   498     vlbVLCTable8InterAC,
       
   499     vlbVLCTable9InterAC - 0x01, // this pointer is going outside the range, but it is used only with condition (level < vlbVLCRun1InterAC[run]) which results having indexes > 0 and hence the array is never indexed below its base address
       
   500     vlbVLCTable9InterAC ,
       
   501     vlbVLCTable9InterAC + 0x01,
       
   502     vlbVLCTable9InterAC + 0x02,
       
   503     vlbVLCTable9InterAC + 0x03,
       
   504     vlbVLCTable9InterAC + 0x04,
       
   505     vlbVLCTable9InterAC + 0x05,
       
   506     vlbVLCTable9InterAC + 0x06,
       
   507     vlbVLCTable9InterAC + 0x07,
       
   508     vlbVLCTable9InterAC + 0x08,
       
   509     vlbVLCTable9InterAC + 0x09,
       
   510     vlbVLCTable9InterAC + 0x0a,
       
   511     vlbVLCTable9InterAC + 0x0b,
       
   512     vlbVLCTable9InterAC + 0x0c,
       
   513     vlbVLCTable9InterAC + 0x0d,
       
   514     vlbVLCTable9InterAC + 0x0e,
       
   515     vlbVLCTable9InterAC + 0x0f,
       
   516     vlbVLCTable9InterAC + 0x10,
       
   517     vlbVLCTable9InterAC + 0x11,
       
   518     vlbVLCTable9InterAC + 0x12,
       
   519     vlbVLCTable9InterAC + 0x13,
       
   520     vlbVLCTable9InterAC + 0x14,
       
   521     vlbVLCTable9InterAC + 0x15,
       
   522     vlbVLCTable9InterAC + 0x16,
       
   523     vlbVLCTable9InterAC + 0x17,
       
   524     vlbVLCTable9InterAC + 0x18,
       
   525     vlbVLCTable9InterAC + 0x19,
       
   526     vlbVLCTable9InterAC + 0x1a,
       
   527     vlbVLCTable9InterAC + 0x1b,
       
   528     vlbVLCTable9InterAC + 0x1c,
       
   529     vlbVLCTable9InterAC + 0x1d,
       
   530     vlbVLCTable9InterAC + 0x1e,
       
   531     vlbVLCTable9InterAC + 0x1f,
       
   532     vlbVLCTable9InterAC + 0x20,
       
   533     vlbVLCTable9InterAC + 0x21,
       
   534     vlbVLCTable9InterAC + 0x22,
       
   535     vlbVLCTable9InterAC + 0x23,
       
   536     vlbVLCTable9InterAC + 0x24,
       
   537     vlbVLCTable9InterAC + 0x25,
       
   538 };
       
   539 
       
   540 const int8 vlbVLCRun0IntraAC[64] =
       
   541 {
       
   542     0x1c, 0x0b, 0x06, 0x05, 0x04, 0x04, 0x04, 0x04, 
       
   543       0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 
       
   544       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   545       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   546       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   547       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   548       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   549       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
       
   550 };
       
   551 
       
   552 const int8 vlbVLCRun1IntraAC[64] =
       
   553 {
       
   554     0x09, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 
       
   555     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
       
   556       0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 
       
   557       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   558       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   559       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   560       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
       
   561       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
       
   562 };
       
   563 
       
   564 /* array of table pointers for AC VLC INTRA tables*/
       
   565 
       
   566 const tVLCTable * const vlbVLCTableIntraPointer0[15] =
       
   567 {
       
   568     vlbVLCTable1IntraAC,
       
   569     vlbVLCTable2IntraAC,
       
   570     vlbVLCTable3IntraAC,
       
   571     vlbVLCTable4IntraAC,
       
   572     vlbVLCTable5IntraAC[0],
       
   573     vlbVLCTable5IntraAC[1],
       
   574     vlbVLCTable5IntraAC[2],
       
   575     vlbVLCTable5IntraAC[3],
       
   576     vlbVLCTable6IntraAC[0],
       
   577     vlbVLCTable6IntraAC[1],
       
   578     vlbVLCTable7IntraAC[0],
       
   579     vlbVLCTable7IntraAC[1],
       
   580     vlbVLCTable7IntraAC[2],
       
   581     vlbVLCTable7IntraAC[3],
       
   582     vlbVLCTable7IntraAC[4],
       
   583 };
       
   584 
       
   585 const tVLCTable * const vlbVLCTableIntraPointer1[21] =
       
   586 {
       
   587     vlbVLCTable8IntraAC,
       
   588     vlbVLCTable9IntraAC,
       
   589     vlbVLCTable10IntraAC[0],
       
   590     vlbVLCTable10IntraAC[1],
       
   591     vlbVLCTable10IntraAC[2],
       
   592     vlbVLCTable10IntraAC[3],
       
   593     vlbVLCTable10IntraAC[4],
       
   594     vlbVLCTable11IntraAC[0],
       
   595     vlbVLCTable11IntraAC[1],
       
   596     vlbVLCTable11IntraAC[2],
       
   597     vlbVLCTable11IntraAC[3],
       
   598     vlbVLCTable11IntraAC[4],
       
   599     vlbVLCTable11IntraAC[5],
       
   600     vlbVLCTable11IntraAC[6],
       
   601     vlbVLCTable11IntraAC[7],
       
   602     vlbVLCTable11IntraAC[8],
       
   603     vlbVLCTable11IntraAC[9],
       
   604     vlbVLCTable11IntraAC[10],
       
   605     vlbVLCTable11IntraAC[11],
       
   606     vlbVLCTable11IntraAC[12],
       
   607     vlbVLCTable11IntraAC[13],
       
   608 };
       
   609 
       
   610 
       
   611 /* {{-output"vdtPutIntraMBCMT.txt"}} */
       
   612 /*
       
   613 * vdtPutIntraMBCMT
       
   614 *
       
   615 * Parameters:
       
   616 *     outBuf                output buffer
       
   617 *       coeff           pointer to the coefficients that must be in scanned order
       
   618 *       numTextureBits  pointer to store the number of bits taken to encode the macro block.
       
   619 *       index           block Index
       
   620 *       skipDC          whether to skip INTRA DC
       
   621 *       skipAC          whether to skip INTRA AC
       
   622 *
       
   623 * Function:
       
   624 *     This function forms the bitstream for INTRA macroblock in combined motion and 
       
   625 *     texture mode for the transcoding module
       
   626 *
       
   627 * Returns:
       
   628 *     Nothing.
       
   629 *
       
   630 * Error codes:
       
   631 *     None.
       
   632 *
       
   633 */
       
   634 void vdtPutIntraMBCMT(bibBuffer_t *outBuf, int *coeff, int *numTextureBits, int index, int skipDC, int skipAC)
       
   635 {
       
   636     int32   textureBits;
       
   637     textureBits = 0;
       
   638     
       
   639     // DC coefficient VLC
       
   640     if (!skipDC)
       
   641     {
       
   642        textureBits += vlbPutDCCoeffCMT(coeff[0], outBuf, index);
       
   643     }
       
   644     
       
   645     //AC coefficient VLC
       
   646     if (!skipAC)
       
   647     {
       
   648         textureBits += vlbPutIntraACCoeffCMT(1, coeff, outBuf);
       
   649         *numTextureBits = textureBits;
       
   650     }
       
   651     
       
   652     return;
       
   653 }
       
   654 
       
   655 
       
   656 /* {{-output"vdtPutInterMBCMT.txt"}} */
       
   657 /*
       
   658 * vdtPutInterMBCMT
       
   659 *
       
   660 * Parameters:
       
   661 *     outBuf                output buffer
       
   662 *       coeff           pointer to the coefficients that must be in scanned order
       
   663 *       numTextureBits  pointer to store the number of bits taken to encode the macro block.
       
   664 *       svh             flag to indicate short video header
       
   665 *
       
   666 * Function:
       
   667 *     This function forms the bitstream for INTER macroblock in combined motion and 
       
   668 *     texture mode for the transcoding module
       
   669 *
       
   670 * Returns:
       
   671 *     Nothing.
       
   672 *
       
   673 * Error codes:
       
   674 *     None.
       
   675 *
       
   676 */
       
   677 void vdtPutInterMBCMT(bibBuffer_t *outBuf, int coeffStart, int *coeff, int *numTextureBits, int svh)
       
   678 {
       
   679     int32   textureBits;
       
   680     int16 coeffInt16[64];
       
   681 
       
   682     /* encode texture coefficients */
       
   683     textureBits = 0;
       
   684     /* find out the last nonzero coeficients */
       
   685     int nonZeroDCT = 0;
       
   686     for (int i = 0; i < 64; i++)
       
   687     {
       
   688         nonZeroDCT = coeff[i] != 0 ? i : nonZeroDCT;
       
   689         coeffInt16[i] = (int16)coeff[i];
       
   690     }
       
   691     textureBits += vlbPutACCoeffSVH(coeffStart, coeffInt16, outBuf, svh, nonZeroDCT);
       
   692     *numTextureBits = textureBits;
       
   693     
       
   694     return;
       
   695 }
       
   696 
       
   697 
       
   698 
       
   699 /* {{-output"vlbPutDCCoeffCMT.txt"}} */
       
   700 /*
       
   701 * vlbPutDCCoeffCMT
       
   702 *
       
   703 * Parameters:
       
   704 *     outBuf                output buffer
       
   705 *       val             value of the DC coefficient
       
   706 *       blkCnt          block number in the macro block
       
   707 *
       
   708 * Function:
       
   709 *     This function puts DC coefficient in CMT mode into the bitstream
       
   710 *
       
   711 * Returns:
       
   712 *     number of bits outputted.
       
   713 *
       
   714 * Error codes:
       
   715 *     None.
       
   716 *
       
   717 */
       
   718 int32 vlbPutDCCoeffCMT(int val, bibBuffer_t * outBuf, int32  blkCnt)
       
   719 {
       
   720     int32   absVal;
       
   721     int32   numBits;
       
   722     int32   size;
       
   723     
       
   724     absVal = ABS(val);
       
   725     size = 0;
       
   726     
       
   727     while(absVal > 0)
       
   728     {
       
   729         size++;
       
   730         absVal >>= 1;
       
   731     }
       
   732     
       
   733     numBits = size;
       
   734     if(blkCnt < 4)
       
   735     {
       
   736         vlbPutBits(outBuf, vlbLumaTableIntraDC[size].length, vlbLumaTableIntraDC[size].code); 
       
   737         numBits += vlbLumaTableIntraDC[size].length;
       
   738     }
       
   739     else
       
   740     {
       
   741         vlbPutBits(outBuf, vlbChromaTableIntraDC[size].length, vlbChromaTableIntraDC[size].code); 
       
   742         numBits += vlbChromaTableIntraDC[size].length;
       
   743     }
       
   744     
       
   745     if(size)
       
   746     {
       
   747         if(val > 0)
       
   748         {
       
   749             vlbPutBits(outBuf, size, val); 
       
   750         }
       
   751         else
       
   752         {
       
   753             absVal = ((1 << size) - 1);
       
   754             vlbPutBits(outBuf, size, (absVal + val)); 
       
   755         }
       
   756     }
       
   757     
       
   758     if(size > 8)
       
   759     {
       
   760         vlbPutBits(outBuf, 1, 1);
       
   761         numBits++;
       
   762     }
       
   763     
       
   764     return numBits;
       
   765 }
       
   766 
       
   767 
       
   768 
       
   769 /* {{-output"vlbPutIntraACCoeffCMT.txt"}} */
       
   770 /*
       
   771 * vlbPutIntraACCoeffCMT
       
   772 *
       
   773 * Parameters:
       
   774 *     outBuf                output buffer
       
   775 *       coeffStart      coefficient number from where the coding is to start
       
   776 *       block           pointer to the texture block data
       
   777 *
       
   778 * Function:
       
   779 *     This function puts AC coefficients in CMT mode into the bitstream
       
   780 *
       
   781 * Returns:
       
   782 *     number of bits outputted.
       
   783 *
       
   784 * Error codes:
       
   785 *     None.
       
   786 *
       
   787 */
       
   788 int32 vlbPutIntraACCoeffCMT(int32 coeffStart, int* block, bibBuffer_t * outBuf) 
       
   789 {
       
   790     tEvent      prevEvent;
       
   791     tEvent      currEvent;
       
   792     int32       i;
       
   793     int32       numBits;
       
   794     tEvent      *prev = &prevEvent;
       
   795     tEvent      *curr = &currEvent;
       
   796     int     level;
       
   797     
       
   798     curr->run  = 0;
       
   799     prev->last = 0;
       
   800     numBits    = 0;
       
   801     
       
   802     for(i = coeffStart; i < 64; i++)
       
   803     {
       
   804         level = block[i]; 
       
   805         if(level == 0)
       
   806         {
       
   807             curr->run++;
       
   808         }
       
   809         else
       
   810         {
       
   811             prev->level = level;
       
   812             prev->run = curr->run;
       
   813             curr->run  = 0;
       
   814             break;
       
   815         }
       
   816     }
       
   817     
       
   818     for(i = i+1; i < 64; i++)
       
   819     {
       
   820         level = block[i]; 
       
   821         if(level == 0)
       
   822         {
       
   823             curr->run++;
       
   824         }
       
   825         else
       
   826         {
       
   827             numBits  += vlbPutIntraACCoeffCMTDP(outBuf, &prevEvent);
       
   828             prev->level = level;
       
   829             prev->run = curr->run;
       
   830             curr->run = 0;
       
   831         }
       
   832     }
       
   833     
       
   834     prev->last = 1;
       
   835     numBits  += vlbPutIntraACCoeffCMTDP(outBuf, &prevEvent);
       
   836     
       
   837     return numBits;
       
   838 }
       
   839 
       
   840 
       
   841 
       
   842 /* {{-output"vlbFindRMax.txt"}} */
       
   843 /*
       
   844 * vlbFindRMax
       
   845 *
       
   846 * Parameters:
       
   847 *     event           pointer to the event being coded
       
   848 *       mbType          macro block encoding type (INTER/INTRA)
       
   849 *
       
   850 * Function:
       
   851 *     This function finds RMAX value to subtract from run for escape coding 
       
   852 *     when reversible VLC is OFF
       
   853 *
       
   854 * Returns:
       
   855 *     RMAX value
       
   856 *
       
   857 * Error codes:
       
   858 *     None.
       
   859 *
       
   860 */
       
   861 int32 vlbFindRMax(tEvent *event, int16 mbType)
       
   862 {
       
   863     int32 level;
       
   864     
       
   865     level = ABS(event->level);
       
   866     
       
   867     if(mbType == INTRA)
       
   868     {
       
   869         if(event->last == 0)
       
   870         {
       
   871             return level <= 27 ? vlbRMaxTableIntra[level - 1] : 
       
   872             NOT_VALID;
       
   873         }
       
   874         else
       
   875         {
       
   876             return level <= 8 ? vlbRMaxTableIntra[level + 26] :
       
   877             NOT_VALID;
       
   878         }
       
   879     }
       
   880     else
       
   881     {
       
   882         if (event->last == 0)
       
   883         {
       
   884             return level <= 12 ? vlbRMaxTableInter[level - 1] : 
       
   885             NOT_VALID;
       
   886         }
       
   887         else
       
   888         {
       
   889             return level <= 3 ? vlbRMaxTableInter[level + 11]
       
   890                 : NOT_VALID; 
       
   891         }
       
   892     }
       
   893 }
       
   894 
       
   895 
       
   896 
       
   897 /* {{-output"vlbFindLMax.txt"}} */
       
   898 /*
       
   899 * vlbFindLMax
       
   900 *
       
   901 * Parameters:
       
   902 *     event           pointer to the event being coded
       
   903 *       mbType          macro block encoding type (INTER/INTRA)
       
   904 *
       
   905 * Function:
       
   906 *     This function finds LMAX value to subtract from run for escape coding 
       
   907 *     when reversible VLC is OFF
       
   908 *
       
   909 * Returns:
       
   910 *     LMAX value
       
   911 *
       
   912 * Error codes:
       
   913 *     None.
       
   914 *
       
   915 */
       
   916 int32 vlbFindLMax(tEvent *event, int16 mbType)
       
   917 {
       
   918     if (mbType == INTRA)
       
   919     {
       
   920         if (event->last == 0)
       
   921         {
       
   922             if (event->run <= 14)
       
   923             {
       
   924                 return vlbLMaxTableIntra[event->run];
       
   925             }
       
   926             else
       
   927             {
       
   928                 return NOT_VALID;
       
   929             }
       
   930         }
       
   931         else
       
   932         {
       
   933             if (event->run <= 20)
       
   934             {
       
   935                 return vlbLMaxTableIntra[event->run + 15];
       
   936             }
       
   937             else
       
   938             {
       
   939                 return NOT_VALID;
       
   940             }
       
   941         }
       
   942     }
       
   943     else
       
   944     {
       
   945         if (event->last == 0)
       
   946         {
       
   947             return event->run <= 26 ? vlbLMaxTableInter[event->run] : 
       
   948             NOT_VALID;
       
   949         }
       
   950         else
       
   951         {
       
   952             return event->run <= 40 ? vlbLMaxTableInter[event->run + 27] :
       
   953             NOT_VALID;
       
   954         }
       
   955     }
       
   956 }
       
   957 
       
   958 
       
   959 
       
   960 /* {{-output"vlbPutIntraACCoeffCMTDP.txt"}} */
       
   961 /*
       
   962 * vlbPutIntraACCoeffCMTDP
       
   963 *
       
   964 * Parameters:
       
   965 *     outBuf                output buffer
       
   966 *       event           pointer to the event being coded
       
   967 *
       
   968 * Function:
       
   969 *     This function encodes AC coefficient for Intra blocks in combined motion texture 
       
   970                     mode and data partitioned mode
       
   971 *
       
   972 * Returns:
       
   973 *     number of bits outputted.
       
   974 *
       
   975 * Error codes:
       
   976 *     None.
       
   977 *
       
   978 */
       
   979 int32 vlbPutIntraACCoeffCMTDP(bibBuffer_t *outBuf, tEvent *event)
       
   980 {
       
   981     int32       sign;
       
   982     int32       level;
       
   983     int32       run;
       
   984     int32       status;
       
   985     int32       count;
       
   986     const tVLCTable   *vlcTable=NULL;
       
   987     int32       lmax;
       
   988     int32       rmax;
       
   989     
       
   990     sign  = (event->level >> 15) & (0x1);
       
   991     level =  ABS(event->level);
       
   992     run   = event->run;
       
   993     count = 0;
       
   994     
       
   995     do
       
   996     {
       
   997         status = CODE_FOUND;
       
   998         if(event->last == 0)
       
   999         {
       
  1000             if(level < vlbVLCRun0IntraAC[run] )
       
  1001             {
       
  1002                 vlcTable = vlbVLCTableIntraPointer0[run];
       
  1003             }           
       
  1004             else 
       
  1005             {
       
  1006                 status = CODE_NOT_FOUND;
       
  1007             }
       
  1008         }
       
  1009         else
       
  1010         {
       
  1011             if(level < vlbVLCRun1IntraAC[run] )
       
  1012             {
       
  1013                 vlcTable = vlbVLCTableIntraPointer1[run];
       
  1014             }           
       
  1015             else 
       
  1016             {
       
  1017                 status = CODE_NOT_FOUND;
       
  1018             }
       
  1019         }
       
  1020         
       
  1021         if (status == CODE_NOT_FOUND)
       
  1022         {
       
  1023             switch (++count)
       
  1024             {
       
  1025             case 1:
       
  1026                 {
       
  1027                     lmax = vlbFindLMax(event, INTRA);
       
  1028                     if (lmax == NOT_VALID)
       
  1029                     {
       
  1030                         count = 2;
       
  1031                     }
       
  1032                     else
       
  1033                     {
       
  1034                         level -= lmax;
       
  1035                         break;
       
  1036                     }
       
  1037                 }
       
  1038             case 2:
       
  1039                 {
       
  1040                     rmax = vlbFindRMax(event, INTRA);
       
  1041                     if (rmax != NOT_VALID)
       
  1042                     {
       
  1043                         level = ABS(event->level);
       
  1044                         run   = event->run - rmax;
       
  1045                         break;
       
  1046                     }
       
  1047                     else
       
  1048                     {
       
  1049                         count = 3;
       
  1050                     }
       
  1051                 }
       
  1052             case 3:
       
  1053                 {
       
  1054                     status = CODE_FOUND;
       
  1055                 }
       
  1056             }
       
  1057         }
       
  1058         
       
  1059     } while (status != CODE_FOUND);
       
  1060     
       
  1061     switch (count)
       
  1062     {
       
  1063     case 0:
       
  1064         vlbPutBits(outBuf, vlcTable[level].length, vlcTable[level].code | sign);
       
  1065         return vlcTable[level].length;
       
  1066         
       
  1067     case 1:
       
  1068         vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1069         vlbPutBits(outBuf, 1, 0);
       
  1070         vlbPutBits(outBuf, vlcTable[level].length, vlcTable[level].code | sign);
       
  1071         return vlcTable[level].length + 1 + ESCAPE_CODE_LENGTH_VLC;
       
  1072         
       
  1073     case 2:
       
  1074         vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1075         vlbPutBits(outBuf, 2, 0x2);
       
  1076         vlbPutBits(outBuf, vlcTable[level].length, vlcTable[level].code | sign);
       
  1077         return vlcTable[level].length + 2 + ESCAPE_CODE_LENGTH_VLC;
       
  1078         
       
  1079     case 3:
       
  1080         vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1081         vlbPutBits(outBuf, 2, 0x3);
       
  1082         vlbPutBits(outBuf, 8, (event->last << 7) | (event->run << 1) | 1);
       
  1083         vlbPutBits(outBuf, 13, ((event->level & 0x0fff) << 1) | 1);
       
  1084         return 23 + ESCAPE_CODE_LENGTH_VLC;
       
  1085         
       
  1086     default:
       
  1087         return E_FAILURE;
       
  1088     }
       
  1089 }
       
  1090 
       
  1091 
       
  1092 
       
  1093 /* {{-output"vlbPutACCoeffSVH.txt"}} */
       
  1094 /*
       
  1095 * vlbPutACCoeffSVH
       
  1096 *
       
  1097 * Parameters:
       
  1098 *     outBuf                output buffer
       
  1099 *       coeffStart      coefficient number from where the coding to be started
       
  1100 *       block           pointer to the texture block data. Coeficients must be in zigzag order
       
  1101 *       svh             flag indicating whether short video header mode is ON
       
  1102 *       lastPos         value of last non-zero position of block
       
  1103 *
       
  1104 * Function:
       
  1105 *     This function puts AC coefficients in SVH mode into the bitstream
       
  1106 *
       
  1107 * Returns:
       
  1108 *     number of bits outputted.
       
  1109 *
       
  1110 * Error codes:
       
  1111 *     None.
       
  1112 *
       
  1113 */
       
  1114 int32 vlbPutACCoeffSVH(int32 coeffStart, int16* block, bibBuffer_t * outBuf,
       
  1115                        int32 svh, int32 lastPos)
       
  1116                                              
       
  1117 {
       
  1118     tEvent  prevEvent;
       
  1119     tEvent  currEvent;
       
  1120     int32   i;
       
  1121     int32   numBits;
       
  1122     tEvent  *prev ;
       
  1123     tEvent  *curr ;
       
  1124     int32       level;
       
  1125     int32   lastPosition = lastPos;
       
  1126     
       
  1127     prev = &prevEvent;
       
  1128     curr = &currEvent;
       
  1129     curr->run  = 0;
       
  1130     prev->last = 0;
       
  1131     numBits = 0;
       
  1132     
       
  1133     for(i = coeffStart; i <= lastPosition; i++)
       
  1134     {
       
  1135         level = (int16)block[i];
       
  1136         if(level == 0)
       
  1137         {
       
  1138             curr->run++;
       
  1139         }
       
  1140         else
       
  1141         {
       
  1142             // clip the coeff (MPEG-4 has larger range than H.263) requantization of the whole block is too complicated for this minor use case
       
  1143             if ( level < -127 )
       
  1144                 {
       
  1145                 level = -127;
       
  1146                 }
       
  1147             else if ( level > 127 )
       
  1148                 {
       
  1149                 level = 127;
       
  1150                 }
       
  1151             prev->level = level;
       
  1152             prev->run = curr->run;
       
  1153             curr->run  = 0;
       
  1154             break;
       
  1155         }
       
  1156     }
       
  1157     
       
  1158     for(i = i+1; i <= lastPosition; i++)
       
  1159     {
       
  1160         level = (int16)block[i];
       
  1161         if(level == 0)
       
  1162         {
       
  1163             curr->run++;
       
  1164         }
       
  1165         else
       
  1166         {
       
  1167             numBits  += vlbPutACVLCCoeff(outBuf, &prevEvent,svh);
       
  1168             
       
  1169             // clip the coeff (MPEG-4 has larger range than H.263) requantization of the whole block is too complicated for this minor use case
       
  1170             if ( level < -127 )
       
  1171                 {
       
  1172                 level = -127;
       
  1173                 }
       
  1174             else if ( level > 127 )
       
  1175                 {
       
  1176                 level = 127;
       
  1177                 }
       
  1178             prev->level = level;
       
  1179             prev->run = curr->run;
       
  1180             curr->run = 0;
       
  1181         }
       
  1182     }
       
  1183     
       
  1184     prev->last = 1;
       
  1185     numBits  += vlbPutACVLCCoeff(outBuf, &prevEvent,svh);
       
  1186     
       
  1187     return numBits;
       
  1188 }
       
  1189 
       
  1190 
       
  1191 
       
  1192 /* {{-output"vlbCodeACCoeffsSVHWithZigZag.txt"}} */
       
  1193 /*
       
  1194 * vlbCodeACCoeffsSVHWithZigZag
       
  1195 *
       
  1196 * Parameters:
       
  1197 *     outBuf                output buffer
       
  1198 *       coeffStart      coefficient number from where the coding to be started
       
  1199 *       block           pointer to the texture block data. Coeficients must be in zigzag order
       
  1200 *       svh             flag indicating whether short video header mode is ON
       
  1201 *       lastPos         value of last non-zero position of block
       
  1202 *
       
  1203 * Function:
       
  1204 *     This function puts AC coefficients in SVH mode with zigzag into the bitstream
       
  1205 *
       
  1206 * Returns:
       
  1207 *     number of bits outputted.
       
  1208 *
       
  1209 * Error codes:
       
  1210 *     None.
       
  1211 *
       
  1212 */
       
  1213 int32 vlbCodeACCoeffsSVHWithZigZag(int32 coeffStart, int16* block, bibBuffer_t * outBuf,
       
  1214                                                                  int32 svh, int32 lastPos)
       
  1215                                                                  
       
  1216 {
       
  1217     tEvent      prevEvent;
       
  1218     tEvent      currEvent;
       
  1219     int32       i;
       
  1220     int32       numBits;
       
  1221     tEvent      *prev ;
       
  1222     tEvent      *curr ;
       
  1223     int32       level;
       
  1224     int32       lastPosition = vlbInvZigzagScan[lastPos];
       
  1225     
       
  1226     prev = &prevEvent;
       
  1227     curr = &currEvent;
       
  1228     curr->run  = 0;
       
  1229     prev->last = 0;
       
  1230     numBits = 0;
       
  1231     
       
  1232     for(i = coeffStart; i <= lastPosition; i++)
       
  1233     {
       
  1234         level = block[vlbZigzagScan[i]]; 
       
  1235         if(level == 0)
       
  1236         {
       
  1237             curr->run++;
       
  1238         }
       
  1239         else
       
  1240         {
       
  1241             prev->level = level;
       
  1242             prev->run = curr->run;
       
  1243             curr->run  = 0;
       
  1244             break;
       
  1245         }
       
  1246     }
       
  1247     
       
  1248     for(i = i+1; i <= lastPosition; i++)
       
  1249     {
       
  1250         level = block[vlbZigzagScan[i]]; 
       
  1251         if(level == 0)
       
  1252         {
       
  1253             curr->run++;
       
  1254         }
       
  1255         else
       
  1256         {
       
  1257             numBits  += vlbPutACVLCCoeff(outBuf, &prevEvent,svh);
       
  1258             prev->level = level;
       
  1259             prev->run = curr->run;
       
  1260             curr->run = 0;
       
  1261         }
       
  1262     }
       
  1263     
       
  1264     prev->last = 1;
       
  1265     numBits  += vlbPutACVLCCoeff(outBuf, &prevEvent,svh);
       
  1266     
       
  1267     return numBits;
       
  1268 }
       
  1269 
       
  1270 
       
  1271 
       
  1272 
       
  1273 /* {{-output"vlbPutACVLCCoeff.txt"}} */
       
  1274 /*
       
  1275 * vlbPutACVLCCoeff
       
  1276 *
       
  1277 * Parameters:
       
  1278 *     outBuf                output buffer
       
  1279 *       event           pointer to the event being coded
       
  1280 *       svh             flag indicating whether short video header mode is ON
       
  1281 *
       
  1282 * Function:
       
  1283 *     This function encodes AC coefficients for Inter and Intra blocks in SVH mode and 
       
  1284 *     inter blocks in non-SVH mode
       
  1285 *
       
  1286 * Returns:
       
  1287 *     number of bits outputted.
       
  1288 *
       
  1289 * Error codes:
       
  1290 *     None.
       
  1291 *
       
  1292 */
       
  1293 int32 vlbPutACVLCCoeff(bibBuffer_t *outBuf, tEvent *event, int32 svh)
       
  1294 {
       
  1295     int32       sign;
       
  1296     int32       run;
       
  1297     int32       level;
       
  1298     int32       status;
       
  1299     int32       count;
       
  1300     const tVLCTable   *vlcTable= NULL;
       
  1301     int32       lmax;
       
  1302     int32       rmax;
       
  1303     
       
  1304     sign  = (event->level >> 15) & (0x1);
       
  1305     level = ABS(event->level);
       
  1306     run   = event->run;
       
  1307     count = 0;
       
  1308     
       
  1309     do
       
  1310     {
       
  1311         status = CODE_FOUND;
       
  1312         if(event->last == 0)
       
  1313         {
       
  1314             if(level < vlbVLCRun0InterAC[run] )
       
  1315             {
       
  1316                 vlcTable = vlbVLCTablePointer0[run];
       
  1317             }           
       
  1318             else 
       
  1319             {
       
  1320                 status = CODE_NOT_FOUND;
       
  1321             }
       
  1322         }
       
  1323         else
       
  1324         {
       
  1325             if(level < vlbVLCRun1InterAC[run] )
       
  1326             {
       
  1327                 vlcTable = vlbVLCTablePointer1[run];
       
  1328             }           
       
  1329             else 
       
  1330             {
       
  1331                 status = CODE_NOT_FOUND;
       
  1332             }
       
  1333         }
       
  1334         
       
  1335         if (status == CODE_NOT_FOUND)
       
  1336         {
       
  1337             if (svh == ON)
       
  1338             {
       
  1339                 count  = 4;
       
  1340                 status = CODE_FOUND;
       
  1341             }
       
  1342             else
       
  1343             {
       
  1344                 switch (++count)
       
  1345                 {
       
  1346                 case 1:
       
  1347                     {
       
  1348                         lmax = vlbFindLMax(event, INTER);
       
  1349                         if (lmax == NOT_VALID)
       
  1350                         {
       
  1351                             count = 2;
       
  1352                         }
       
  1353                         else
       
  1354                         {
       
  1355                             level = ABS(event->level) - lmax;
       
  1356                             break;
       
  1357                         }
       
  1358                     }
       
  1359                 case 2:
       
  1360                     {
       
  1361                         rmax = vlbFindRMax(event, INTER);
       
  1362                         if (rmax != NOT_VALID)
       
  1363                         {
       
  1364                             level = ABS(event->level);
       
  1365                             run   = event->run - rmax;
       
  1366                             break;
       
  1367                         }
       
  1368                         else
       
  1369                         {
       
  1370                             count = 3;
       
  1371                         }
       
  1372                     }
       
  1373                 case 3:
       
  1374                     {
       
  1375                         status = CODE_FOUND;
       
  1376                         break;
       
  1377                     }
       
  1378                 default:
       
  1379                     {
       
  1380                         return E_FAILURE;
       
  1381                     }
       
  1382                 }
       
  1383             }
       
  1384         }
       
  1385         
       
  1386     } while (status != CODE_FOUND);
       
  1387     
       
  1388     switch (count)
       
  1389     {
       
  1390     case 0:
       
  1391         {
       
  1392             vlbPutBits(outBuf, vlcTable[level].length, vlcTable[level].code | sign);
       
  1393             return vlcTable[level].length;
       
  1394         }
       
  1395     case 1:
       
  1396         {
       
  1397             vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1398             vlbPutBits(outBuf, 1, 0);
       
  1399             vlbPutBits(outBuf, vlcTable[level].length, vlcTable[level].code | sign);
       
  1400             return vlcTable[level].length + 1 + ESCAPE_CODE_LENGTH_VLC;
       
  1401         }
       
  1402     case 2:
       
  1403         {
       
  1404             vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1405             vlbPutBits(outBuf, 2, 0x2);
       
  1406             vlbPutBits(outBuf, vlcTable[level].length, vlcTable[level].code | sign);
       
  1407             return vlcTable[level].length + 2 + ESCAPE_CODE_LENGTH_VLC;
       
  1408         }
       
  1409     case 3:
       
  1410         {
       
  1411             vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1412             vlbPutBits(outBuf, 2, 0x3);
       
  1413             /* Fixed Length Coding of Events */
       
  1414             vlbPutBits(outBuf, 8, (event->last << 7) | (event->run << 1) | 1);
       
  1415             vlbPutBits(outBuf, 13, ((event->level & 0x0fff) << 1) | 1);
       
  1416             return 23 + ESCAPE_CODE_LENGTH_VLC;
       
  1417         }
       
  1418     case 4:
       
  1419         {
       
  1420             vlbPutBits(outBuf, ESCAPE_CODE_LENGTH_VLC, ESCAPE_CODE_VLC);
       
  1421             vlbPutBits(outBuf, 7, (event->last << 6) | event->run);
       
  1422             vlbPutBits(outBuf, 8, event->level & 0x0ff);
       
  1423             return 15 + ESCAPE_CODE_LENGTH_VLC;
       
  1424         }
       
  1425     default:
       
  1426         {
       
  1427             return E_FAILURE;
       
  1428         }
       
  1429     }
       
  1430 }
       
  1431 
       
  1432 /* End of vlb.cpp */
       
  1433 
       
  1434