videoeditorengine/h263decoder/src/viddemux.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 * H.263 bitstream parsing.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 /*
       
    23  * Includes
       
    24  */
       
    25 
       
    26 #include "h263dConfig.h"
       
    27 #include "viddemux.h"
       
    28 #include "vdxint.h"
       
    29 #include "debug.h"
       
    30 #include "biblin.h"
       
    31 /* MVE */
       
    32 #include "MPEG4Transcoder.h"
       
    33 
       
    34 /*
       
    35  * Defines
       
    36  */
       
    37 
       
    38 #ifdef DEB_STDOUT
       
    39 /* Use back-channel debug output file when printing back-channel related 
       
    40    messages. */
       
    41 #include <stdio.h>
       
    42 extern FILE *bcmDebFile;
       
    43 #endif
       
    44 
       
    45  
       
    46 /*
       
    47  * Module-scope typedefs
       
    48  */
       
    49 
       
    50 /* Another type for VLC (variable length code) lookup tables used in 
       
    51    Annex I implementation.*/
       
    52 typedef struct {
       
    53    u_char LAST;   /* see section 5.4.2 of the H.263 recommendation */
       
    54    u_char RUN;    /* to understand LAST, RUN and LEVEL */
       
    55    u_char LEVEL;
       
    56    u_char len;    /* actual length of code in bits */
       
    57 } vdxVLCTableNew_t;
       
    58 
       
    59 
       
    60 /*
       
    61  * Global constants
       
    62  */
       
    63 
       
    64 /* Used to convert a luminance block index defined in section 4.2.1
       
    65    of the H.263 recommendation to a coded block pattern mask (see sections
       
    66    5.3.4 and 5.3.5 of the H.263 recommendation. 
       
    67    See also macros section in viddemux.h. */
       
    68 const int vdxBlockIndexToCBPYMask[5] = {0, 8, 4, 2, 1};
       
    69 const int vdxYBlockIndexToCBPBMask[5] = {0, 32, 16, 8, 4};
       
    70 
       
    71 
       
    72 /*
       
    73  * Local function prototypes
       
    74  */
       
    75 
       
    76 /* Picture Layer */
       
    77 
       
    78 static int vdxActAfterIncorrectSEI(
       
    79    bibBuffer_t *inBuffer,
       
    80    int fPLUSPTYPE,
       
    81    int *fLast,
       
    82    int *bitErrorIndication);
       
    83 
       
    84 static void vdxStandardSourceFormatToFrameSize(int sourceFormat, int *width, int *height);
       
    85 
       
    86 /* Slice Layer */
       
    87 int vdxFrameSizeToPictureFormat(int width, int height);
       
    88 
       
    89 /* Macroblock Layer */
       
    90 
       
    91 static int vdxGetIntraMode(bibBuffer_t *inBuffer,int *index,
       
    92    int *bitErrorIndication);
       
    93 
       
    94 static int vdxUMVGetMVD(bibBuffer_t *inBuffer,int *mvdx10,
       
    95    int *bitErrorIndication);
       
    96 
       
    97 static int vdxGetNormalMODB(bibBuffer_t *inBuffer, int *index, 
       
    98    int *bitErrorIndication);
       
    99 
       
   100 static int vdxGetImpPBMODB(bibBuffer_t *inBuffer, int *index,
       
   101    int *bitErrorIndication);
       
   102 
       
   103 
       
   104 
       
   105 
       
   106 /*
       
   107  * Picture Layer Global Functions
       
   108  */
       
   109 
       
   110 /* {{-output"vdxGetPictureHeader.txt"}} */
       
   111 /*
       
   112  * vdxGetPictureHeader
       
   113  *    
       
   114  *
       
   115  * Parameters:
       
   116  *    inBuffer                   pointer to bit buffer instance
       
   117  *    inpParam                   input parameters
       
   118  *    header                     output parameters: picture header
       
   119  *    bitErrorIndication         non-zero if a bit error has been detected
       
   120  *                               within the bits accessed in this function,
       
   121  *                               see biterr.h for possible values
       
   122  *
       
   123  * Function:
       
   124  *    This function reads the H.263 picture header.
       
   125  *
       
   126  * Returns:
       
   127  *    VDX_OK                     the function was successful
       
   128  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
   129  *                               occured
       
   130  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
   131  *
       
   132  *    
       
   133  */
       
   134 
       
   135 int vdxGetPictureHeader(
       
   136    bibBuffer_t *inBuffer, 
       
   137    const vdxGetPictureHeaderInputParam_t *inpParam,
       
   138    vdxPictureHeader_t *header,
       
   139    int *bitErrorIndication)
       
   140 /* {{-output"vdxGetPictureHeader.txt"}} */
       
   141 {
       
   142    bibFlushBits_t
       
   143       flushBits;
       
   144    bibGetBits_t
       
   145       getBits;
       
   146    int
       
   147       numBitsGot,
       
   148       bits1To5OfPTYPE,
       
   149       sourceFormat;
       
   150    int16
       
   151       bibError = 0;
       
   152    u_int32 tmp = 0;          /* temporary variable for reading some redundant bits */
       
   153 
       
   154    vdxAssert(inBuffer != NULL);
       
   155    vdxAssert(inpParam != NULL);
       
   156    vdxAssert(inpParam->flushBits != NULL);
       
   157    vdxAssert(inpParam->getBits != NULL);
       
   158    vdxAssert(header != NULL);
       
   159    vdxAssert(bitErrorIndication != NULL);
       
   160 
       
   161    flushBits = inpParam->flushBits;
       
   162    getBits = inpParam->getBits;
       
   163 
       
   164    memset(header, 0, sizeof(vdxPictureHeader_t));
       
   165    *bitErrorIndication = 0;
       
   166 
       
   167    /* Assume that the existence of a PSC has been checked,
       
   168       it is the next code in the buffer, and
       
   169       its byte alignment has been checked */
       
   170 
       
   171    /* Flush stuffing (PSTUF) */
       
   172    if (inpParam->numStuffBits) {
       
   173       flushBits(inpParam->numStuffBits, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   174    }
       
   175 
       
   176    /* Flush PSC */
       
   177    flushBits(22, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   178    /* PSC cannot contain fatal bit errors (checked earlier) */
       
   179    header->numBits += 22;
       
   180 
       
   181    *bitErrorIndication = 0;   
       
   182 
       
   183    /* TR */
       
   184    header->tr = (int) getBits(8, inBuffer, &numBitsGot, bitErrorIndication,
       
   185       &bibError);
       
   186    header->numBits += 8;
       
   187 
       
   188 
       
   189    #ifdef DEB_STDOUT
       
   190       if (bcmDebFile)
       
   191          deb1f(bcmDebFile, "TR %d\n", header->tr);
       
   192    #endif
       
   193 
       
   194    /* The first 5 bits from PTYPE */
       
   195    bits1To5OfPTYPE = (int) getBits(5, inBuffer, &numBitsGot,
       
   196       bitErrorIndication, &bibError);
       
   197    header->numBits += 5;
       
   198 
       
   199 
       
   200    /* PTYPE bit 1 should be 1 */
       
   201    if ((bits1To5OfPTYPE & 16) == 0 ) {
       
   202       deb0p("ERROR. PTYPE bit 1 invalid.\n");
       
   203       goto exitAfterBitError;
       
   204    }
       
   205 
       
   206    /* PTYPE bit 2 should be 0 for distinction with H.261 */
       
   207    if (bits1To5OfPTYPE & 8) {
       
   208       deb0p("ERROR. PTYPE bit 2 indicates H.261.\n");
       
   209       goto exitAfterBitError;
       
   210    }
       
   211 
       
   212    /* PTYPE bit 3, Split Screen Indicator */
       
   213    header->fSplitScreen = ((bits1To5OfPTYPE & 4) > 0);
       
   214 
       
   215    /* PTYPE bit 4, Document camera indicator */
       
   216    header->fDocumentCamera = ((bits1To5OfPTYPE & 2) > 0);
       
   217 
       
   218    /* PTYPE bit 5, Freeze Picture Release */
       
   219    header->fFreezePictureRelease = (bits1To5OfPTYPE & 1);
       
   220 
       
   221    /* PTYPE bits 6 - 8, Source Format */
       
   222    sourceFormat = (int) getBits(3, inBuffer, &numBitsGot,
       
   223       bitErrorIndication, &bibError);
       
   224    header->numBits += 3;
       
   225 
       
   226 
       
   227    /* If H.263 version 1 */
       
   228    if (sourceFormat >= 1 && sourceFormat <= 5) {
       
   229       int bits9To13OfPTYPE;
       
   230 
       
   231       header->fPLUSPTYPE = 0;
       
   232 
       
   233       vdxStandardSourceFormatToFrameSize(sourceFormat, &header->lumWidth, &header->lumHeight);
       
   234 
       
   235       /* PTYPE bits 9 - 13 */
       
   236       bits9To13OfPTYPE = (int) getBits(5, inBuffer, &numBitsGot,
       
   237          bitErrorIndication, &bibError);
       
   238       header->numBits += 5;
       
   239 
       
   240 
       
   241       /* PTYPE bit 9, Picture coding type */
       
   242       header->pictureType = ((bits9To13OfPTYPE & 16) > 0);
       
   243 
       
   244       /* PTYPE bit 10, Unrestricted motion vector mode */
       
   245       header->fUMV = ((bits9To13OfPTYPE & 8) > 0);
       
   246 
       
   247       /* PTYPE bit 11, Syntax-based Arithmetic Coding mode */
       
   248       header->fSAC = ((bits9To13OfPTYPE & 4) > 0);
       
   249 
       
   250       /* PTYPE bit 12, Advanced prediction mode */
       
   251       header->fAP = ((bits9To13OfPTYPE & 2) > 0);
       
   252 
       
   253       /* PTYPE bit 13, PB-frames mode */
       
   254 
       
   255       /* If PTYPE bit 9 indicates a P-picture */
       
   256       if (header->pictureType) {
       
   257          /* Check if it is actually a PB-picture */
       
   258          if (bits9To13OfPTYPE & 1)
       
   259             header->pictureType = VDX_PIC_TYPE_PB;
       
   260       }
       
   261 
       
   262       /* Else PTYPE bit 9 indicates an I-picture */
       
   263       else {
       
   264          /* Check that bit 13 is 0 */
       
   265          if (bits9To13OfPTYPE & 1) {
       
   266             deb0p("ERROR. PTYPE bit 9 and 13 mismatch.\n");
       
   267             goto exitAfterBitError;
       
   268          }
       
   269       }
       
   270    }
       
   271 
       
   272    /* Else if H.263 version 2 (PLUSPTYPE) */
       
   273    else if (sourceFormat == 7) {
       
   274       int bits4To9OfMPPTYPE;
       
   275 
       
   276       header->fPLUSPTYPE = 1;
       
   277 
       
   278       /* UFEP */
       
   279       header->ufep = (int) getBits(3, inBuffer, &numBitsGot,
       
   280          bitErrorIndication, &bibError);
       
   281       header->numBits += 3;
       
   282 
       
   283 
       
   284       if (header->ufep > 1) {
       
   285          deb0p("ERROR. UFEP illegal.\n");
       
   286          goto exitAfterBitError;
       
   287       }
       
   288 
       
   289       /* If UFEP = '001' */
       
   290       if (header->ufep) {
       
   291          int bits4To18OfOPPTYPE;
       
   292 
       
   293          /* OPPTYPE bits 1 - 3, Source format */
       
   294          sourceFormat = (int) getBits(3, inBuffer, &numBitsGot,
       
   295             bitErrorIndication, &bibError);
       
   296          header->numBits += 3;
       
   297 
       
   298 
       
   299 
       
   300          if (sourceFormat >= 1 && sourceFormat <= 5) {
       
   301             header->fCustomSourceFormat = 0;
       
   302             vdxStandardSourceFormatToFrameSize(sourceFormat, 
       
   303                &header->lumWidth, &header->lumHeight);
       
   304          }
       
   305 
       
   306          else if (sourceFormat == 6)
       
   307             header->fCustomSourceFormat = 1;
       
   308 
       
   309          else {
       
   310             deb0p("ERROR. Source format illegal.\n");
       
   311             goto exitAfterBitError;
       
   312          }
       
   313 
       
   314          /* OPPTYPE bits 4 - 18 */
       
   315          bits4To18OfOPPTYPE = (int) getBits(15, inBuffer, &numBitsGot,
       
   316             bitErrorIndication, &bibError);
       
   317          header->numBits += 15;
       
   318 
       
   319 
       
   320          /* OPPTYPE bit 4, Custom PCF */
       
   321          header->fCustomPCF = ((bits4To18OfOPPTYPE & 0x4000) > 0);
       
   322 
       
   323          /* OPPTYPE bit 5, Unrestricted Motion Vector mode */
       
   324          header->fUMV = ((bits4To18OfOPPTYPE & 0x2000) > 0);
       
   325 
       
   326          /* OPPTYPE bit 6, Syntax-based Arithmetic Coding mode */
       
   327          header->fSAC = ((bits4To18OfOPPTYPE & 0x1000) > 0);
       
   328 
       
   329          /* OPPTYPE bit 7, Advanced Prediction mode */
       
   330          header->fAP = ((bits4To18OfOPPTYPE & 0x0800) > 0);
       
   331 
       
   332          /* OPPTYPE bit 8, Advanced INTRA Coding mode */
       
   333          header->fAIC = ((bits4To18OfOPPTYPE & 0x0400) > 0);
       
   334 
       
   335          /* OPPTYPE bit 9, Deblocking filter mode */
       
   336          header->fDF = ((bits4To18OfOPPTYPE & 0x0200) > 0);
       
   337 
       
   338          /* OPPTYPE bit 10, Slice Structured mode */
       
   339          header->fSS = ((bits4To18OfOPPTYPE & 0x0100) > 0);
       
   340 
       
   341          /* OPPTYPE bit 11, Reference Picture Selection mode */
       
   342          header->fRPS = ((bits4To18OfOPPTYPE & 0x0080) > 0);
       
   343 
       
   344          /* OPPTYPE bit 12, Independent Segment Decoding mode */
       
   345          header->fISD = ((bits4To18OfOPPTYPE & 0x0040) > 0);
       
   346 
       
   347          /* OPPTYPE bit 13, Alternative Inter VLC mode */
       
   348          header->fAIV = ((bits4To18OfOPPTYPE & 0x0020) > 0);
       
   349 
       
   350          /* OPPTYPE bit 14, Modified Quantization mode */
       
   351          header->fMQ = ((bits4To18OfOPPTYPE & 0x0010) > 0);
       
   352 
       
   353          /* OPPTYPE bits 15 - 18 must be '1000' */
       
   354          if ((bits4To18OfOPPTYPE & 0x000F) != 8) {
       
   355             deb0p("ERROR. OPPTYPE bits 15 - 18 illegal.\n");
       
   356             goto exitAfterBitError;
       
   357          }
       
   358 
       
   359          /* Mode interaction restrictions, see section 5.1.4.6 of 
       
   360             the H.263 recommendation */
       
   361          if (header->fSAC && (header->fAIV || header->fMQ || header->fUMV)) {
       
   362             deb0p("ERROR. Illegal bit pattern (section 5.1.4.6).\n");
       
   363             goto exitAfterBitError;
       
   364          }
       
   365       }
       
   366 
       
   367       /* MPPTYPE, bits 1 - 3, Picture type code */
       
   368       header->pictureType = (int) getBits(3, inBuffer, &numBitsGot,
       
   369          bitErrorIndication, &bibError);
       
   370       header->numBits += 3;
       
   371 
       
   372 
       
   373       if (header->pictureType >= 6) {
       
   374          deb0p("ERROR. Picture type code illegal.\n");
       
   375          goto exitAfterBitError;
       
   376       }
       
   377 
       
   378       /* MPPTYPE, bits 4 - 9 */
       
   379       bits4To9OfMPPTYPE = (int) getBits(6, inBuffer, &numBitsGot,
       
   380          bitErrorIndication, &bibError);
       
   381       header->numBits += 6;
       
   382 
       
   383 
       
   384       /* MPPTYPE bit 4, Reference Picture Resampling mode */
       
   385       header->fRPR = ((bits4To9OfMPPTYPE & 32) > 0);
       
   386 
       
   387       /* MPPTYPE bit 5, Reduced-Resolution Update mode */
       
   388       header->fRRU = ((bits4To9OfMPPTYPE & 16) > 0);
       
   389 
       
   390       /* RPR/RRU must not be set for I or EI-pictures.
       
   391          (See section 5.1.4.5 of the H.263 recommendation.) */
       
   392       if ((header->fRPR || header->fRRU) &&
       
   393          (header->pictureType == VDX_PIC_TYPE_I || 
       
   394             header->pictureType == VDX_PIC_TYPE_EI)) {
       
   395          deb0p("ERROR. RPR or RRU is set for I or EI.\n");
       
   396          goto exitAfterBitError;
       
   397       }
       
   398 
       
   399       /* MPPTYPE bit 6, Rounding type */
       
   400       header->rtype = ((bits4To9OfMPPTYPE & 8) > 0);
       
   401 
       
   402       /* RTYPE must be 0 if other than P, Improved PB or EP picture
       
   403          (see section 5.1.4.3 of the H.263 recommendation */
       
   404 
       
   405       /* MPPTYPE bits 7 - 9, must be '001' */
       
   406       if ((bits4To9OfMPPTYPE & 7) != 1) {
       
   407          deb0p("ERROR. MPPTYPE bits 7 - 9 illegal.\n");
       
   408          goto exitAfterBitError;
       
   409       }
       
   410 
       
   411       /* CPM */
       
   412       header->cpm = (int) getBits(1, inBuffer, &numBitsGot,
       
   413          bitErrorIndication, &bibError);
       
   414       header->numBits += 1;
       
   415 
       
   416 
       
   417       if (header->cpm) {
       
   418          /* PSBI */
       
   419          header->psbi = (int) getBits(2, inBuffer, &numBitsGot,
       
   420             bitErrorIndication, &bibError);
       
   421          header->numBits += 2;
       
   422 
       
   423 
       
   424       }
       
   425 
       
   426       if (header->fCustomSourceFormat) {
       
   427          int parCode, fExtendedPAR = 0, pwi, phi;
       
   428 
       
   429          /* CPFMT bits 1 - 4, Pixel Aspect Ratio code */
       
   430          parCode = (int) getBits(4, inBuffer, &numBitsGot,
       
   431             bitErrorIndication, &bibError);
       
   432          header->numBits += 4;
       
   433 
       
   434 
       
   435 
       
   436          switch (parCode) {
       
   437 
       
   438             case 1: /* Square */
       
   439                header->parWidth = 1;
       
   440                header->parHeight = 1;
       
   441                break;
       
   442 
       
   443             case 2: /* CIF for 4:3 picture */
       
   444                header->parWidth = 12;
       
   445                header->parHeight = 11;
       
   446                break;
       
   447 
       
   448             case 3: /* 525-type for 4:3 picture */
       
   449                header->parWidth = 10;
       
   450                header->parHeight = 11;
       
   451                break;
       
   452 
       
   453             case 4: /* CIF stretched for 16:9 picture */
       
   454                header->parWidth = 16;
       
   455                header->parHeight = 11;
       
   456                break;
       
   457 
       
   458             case 5: /* 525-type stretched for 16:9 picture */
       
   459                header->parWidth = 40;
       
   460                header->parHeight = 33;
       
   461                break;
       
   462 
       
   463             case 15: /* extended PAR */
       
   464                fExtendedPAR = 1;
       
   465                break;
       
   466 
       
   467             default:
       
   468                deb0p("ERROR. PAR code illegal.\n");
       
   469                goto exitAfterBitError;
       
   470          }
       
   471 
       
   472          /* CPFMT bits 5 - 13, picture width indication */
       
   473          pwi = (int) getBits(9, inBuffer, &numBitsGot,
       
   474             bitErrorIndication, &bibError);
       
   475          header->numBits += 9;
       
   476 
       
   477 
       
   478          header->lumWidth = ((pwi + 1) <<2 /** 4*/);
       
   479 
       
   480          /* CPFMT bit 14 must be 1 */
       
   481          tmp = getBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   482          header->numBits += 1;
       
   483 
       
   484 
       
   485          if ( tmp == 0 ) {
       
   486             deb0p("ERROR. CPFMT bit 14 is 0.\n");
       
   487             goto exitAfterBitError;
       
   488          }
       
   489 
       
   490          /* CPFMT bits 15 - 23, picture height indication */
       
   491          phi = (int) getBits(9, inBuffer, &numBitsGot,
       
   492             bitErrorIndication, &bibError);
       
   493          header->numBits += 9;
       
   494 
       
   495 
       
   496          if (phi == 0 || phi > 288) {
       
   497             deb0p("ERROR. PHI illegal.\n");
       
   498             goto exitAfterBitError;
       
   499          }
       
   500          header->lumHeight = (phi <<2 /** 4*/);
       
   501 
       
   502          if (fExtendedPAR) {
       
   503             /* EPAR bits 1 - 8, PAR Width */
       
   504             header->parWidth = (int) getBits(8, inBuffer, &numBitsGot,
       
   505                bitErrorIndication, &bibError);
       
   506             header->numBits += 8;
       
   507 
       
   508 
       
   509             if (header->parWidth == 0) {
       
   510                deb0p("ERROR. PAR width illegal.\n");
       
   511                goto exitAfterBitError;
       
   512             }
       
   513 
       
   514             /* EPAR bits 9 - 16, PAR Height */
       
   515             header->parHeight = (int) getBits(8, inBuffer, &numBitsGot,
       
   516                bitErrorIndication, &bibError);
       
   517             header->numBits += 8;
       
   518 
       
   519 
       
   520             if (header->parHeight == 0) {
       
   521                deb0p("ERROR. PAR height illegal.\n");
       
   522                goto exitAfterBitError;
       
   523             }
       
   524          }
       
   525       } /* endif (customSourceFormat) */
       
   526 
       
   527       if (header->fCustomPCF) {
       
   528          int clockConversionCode;
       
   529          u_int32 clockDivisor, conversionFactor;
       
   530 
       
   531          /* CPCFC bit 1, Clock conversion code */
       
   532          clockConversionCode = (int) getBits(1, inBuffer, &numBitsGot,
       
   533             bitErrorIndication, &bibError);
       
   534          header->numBits += 1;
       
   535 
       
   536 
       
   537          if (clockConversionCode)
       
   538             conversionFactor = 1001;
       
   539          else
       
   540             conversionFactor = 1000;
       
   541 
       
   542          /* CPCFC bits 2 - 8, Clock divisor */
       
   543          clockDivisor = getBits(7, inBuffer, &numBitsGot,
       
   544             bitErrorIndication, &bibError);
       
   545          header->numBits += 7;
       
   546 
       
   547 
       
   548          if (clockDivisor == 0) {
       
   549             deb0p("ERROR. Illegal clock divisor.\n");
       
   550             goto exitAfterBitError;
       
   551          }
       
   552  
       
   553          header->pcfRate = 1800000LU;
       
   554          header->pcfScale = clockDivisor * conversionFactor;
       
   555       }
       
   556 
       
   557       else {
       
   558          /* CIF clock frequency */
       
   559          header->pcfRate = 2997;
       
   560          header->pcfScale = 100;
       
   561       }
       
   562 
       
   563       if (header->fCustomPCF || (!header->ufep && inpParam->fCustomPCF)) {
       
   564          int etr;
       
   565 
       
   566          /* ETR */
       
   567          etr = (int) getBits(2, inBuffer, &numBitsGot,
       
   568             bitErrorIndication, &bibError);
       
   569          header->numBits += 2;
       
   570 
       
   571 
       
   572 
       
   573          header->tr |= (etr << 8);
       
   574       }
       
   575 
       
   576       if (header->fUMV) {
       
   577          /* UUI */
       
   578          tmp = getBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   579          header->numBits += 1;
       
   580 
       
   581 
       
   582          if ( tmp )
       
   583             header->fUMVLimited = 1;
       
   584 
       
   585          else {
       
   586             tmp = getBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   587             header->numBits += 1;
       
   588 
       
   589 
       
   590             if ( tmp )
       
   591                header->fUMVLimited = 0;
       
   592             else {
       
   593                /* '00' forbidden */
       
   594                deb0p("ERROR. Illegal UUI.\n");
       
   595                goto exitAfterBitError;
       
   596             }
       
   597          }
       
   598 
       
   599 
       
   600       }
       
   601 
       
   602       if (header->fSS) {
       
   603          /* SSS */
       
   604          tmp = (int) getBits(1, inBuffer, &numBitsGot,
       
   605             bitErrorIndication, &bibError);
       
   606 
       
   607          header->fRectangularSlices = tmp;
       
   608          header->numBits += 1;
       
   609 
       
   610          header->fArbitrarySliceOrdering = (int) getBits(1, inBuffer, &numBitsGot,
       
   611             bitErrorIndication, &bibError);
       
   612          header->numBits += 1;
       
   613 
       
   614 
       
   615          /* Mode interaction restriction, see section 5.1.4.6 of 
       
   616             the H.263 recommendation */
       
   617          if (header->fISD && !header->fRectangularSlices) {
       
   618             deb0p("ERROR. Illegal bit pattern (section 5.1.4.6).\n");
       
   619             goto exitAfterBitError;
       
   620          }
       
   621       }
       
   622 
       
   623       if (inpParam->fScalabilityMode) {
       
   624          /* ELNUM */
       
   625          header->elnum = (int) getBits(4, inBuffer, &numBitsGot,
       
   626             bitErrorIndication, &bibError);
       
   627          header->numBits += 4;
       
   628 
       
   629       }
       
   630 
       
   631       if (inpParam->fScalabilityMode && header->ufep) {
       
   632          /* RLNUM */
       
   633          header->rlnum = (int) getBits(4, inBuffer, &numBitsGot,
       
   634             bitErrorIndication, &bibError);
       
   635          header->numBits += 4;
       
   636 
       
   637       }
       
   638 
       
   639       if (header->fRPS) {
       
   640          /* RPSMF */
       
   641          header->rpsMode = (int) getBits(3, inBuffer, &numBitsGot,
       
   642             bitErrorIndication, &bibError);
       
   643          header->numBits += 3;
       
   644 
       
   645 
       
   646          if (header->rpsMode < 4) {
       
   647             deb0p("ERROR. Illegal RPSMF.\n");
       
   648             goto exitAfterBitError;
       
   649          }
       
   650 
       
   651          header->rpsMode -= 4; /* 4..7 --> 0..3 */
       
   652       }
       
   653 
       
   654       /* If no OPPTYPE but RPS was previously on or RPS signaled in OPPTYPE */
       
   655       if ((inpParam->fRPS && header->ufep == 0) || header->fRPS) {
       
   656          /* TRPI */
       
   657          header->trpi = (int) getBits(1, inBuffer, &numBitsGot,
       
   658             bitErrorIndication, &bibError);
       
   659          header->numBits += 1;
       
   660 
       
   661 
       
   662          if (header->trpi) {
       
   663             /* TRP */
       
   664             header->trp = (int) getBits(10, inBuffer, &numBitsGot,
       
   665                bitErrorIndication, &bibError);
       
   666             header->numBits += 10;
       
   667 
       
   668 
       
   669             deb2f(bcmDebFile, "TRPI in picture header. TR = %d, TRP = %d.\n", 
       
   670                header->tr, header->trp);            
       
   671          }
       
   672 
       
   673             /* Code following the standard */
       
   674 
       
   675             /* BCI */
       
   676             tmp = getBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   677             header->numBits += 1;
       
   678 
       
   679 
       
   680             if ( tmp ) {
       
   681                /* BCM not supported */
       
   682                deb0p("ERROR. BCM not supported.\n");
       
   683                goto exitAfterBitError;
       
   684             }
       
   685 
       
   686             else {
       
   687                tmp = getBits(1, inBuffer, &numBitsGot,bitErrorIndication, &bibError);
       
   688                header->numBits += 1;
       
   689 
       
   690 
       
   691                if ( !tmp ) {
       
   692                   /* BCI '00' is illegal */
       
   693                   deb0p("ERROR. Illegal BCI.\n");
       
   694                   goto exitAfterBitError;
       
   695                }
       
   696             }
       
   697 
       
   698       }
       
   699 
       
   700       if (header->fRPR) {
       
   701          /* RPRP not supported */
       
   702          deb0p("ERROR. RPRP not supported.\n");
       
   703          goto exitAfterBitError;
       
   704       }
       
   705    }
       
   706 
       
   707    else {
       
   708       deb0p("ERROR. Source format illegal.\n");
       
   709       goto exitAfterBitError;
       
   710    }
       
   711 
       
   712    /* PQUANT */
       
   713    header->pquantPosition = header->numBits;
       
   714    header->pquant = (int) getBits(5, inBuffer, &numBitsGot,
       
   715       bitErrorIndication, &bibError);
       
   716    header->numBits += 5;
       
   717 
       
   718 
       
   719    if (header->pquant == 0) {
       
   720       deb0p("ERROR. PQUANT illegal.\n");
       
   721       goto exitAfterBitError;
       
   722    }
       
   723 
       
   724    if (!header->fPLUSPTYPE) {
       
   725       /* CPM */
       
   726       header->cpm = (int) getBits(1, inBuffer, &numBitsGot,
       
   727          bitErrorIndication, &bibError);
       
   728       header->numBits += 1;
       
   729 
       
   730 
       
   731       if (header->cpm) {
       
   732          /* PSBI */
       
   733          header->psbi = (int) getBits(2, inBuffer, &numBitsGot,
       
   734             bitErrorIndication, &bibError); 
       
   735          header->numBits += 2;
       
   736 
       
   737 
       
   738       }
       
   739    }
       
   740 
       
   741    /* If PB-frame */
       
   742    if (header->pictureType == VDX_PIC_TYPE_PB || 
       
   743       header->pictureType == VDX_PIC_TYPE_IPB) {
       
   744 
       
   745       /* TRB */
       
   746 
       
   747       /* If custom picture clock frequence is used */
       
   748       if (header->fCustomPCF || inpParam->fCustomPCF) {
       
   749          header->trb = (int) getBits(5, inBuffer, &numBitsGot,
       
   750             bitErrorIndication, &bibError);
       
   751          header->numBits += 5;
       
   752 
       
   753 
       
   754       }
       
   755 
       
   756       else {
       
   757          header->trb = (int) getBits(3, inBuffer, &numBitsGot,
       
   758             bitErrorIndication, &bibError);
       
   759          header->numBits += 3;
       
   760 
       
   761 
       
   762       }
       
   763 
       
   764       if (header->trb == 0) {
       
   765          deb0p("ERROR. TRB illegal.\n");
       
   766          goto exitAfterBitError;
       
   767       }
       
   768 
       
   769       /* DBQUANT */
       
   770       header->dbquant = (int) getBits(2, inBuffer, &numBitsGot,
       
   771          bitErrorIndication, &bibError);
       
   772       header->numBits += 2;
       
   773 
       
   774 
       
   775    }
       
   776 
       
   777 
       
   778    /* Check success and return */
       
   779 
       
   780 
       
   781 
       
   782    /* If no error in bit buffer functions */
       
   783    if (!bibError)
       
   784       return VDX_OK;
       
   785 
       
   786    /* Else if ran out of data (i.e. decoding out of sync) */
       
   787    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
   788       return VDX_OK_BUT_BIT_ERROR;
       
   789    }
       
   790 
       
   791    /* Else other error in bit buffer functions */
       
   792    else
       
   793       return VDX_ERR_BIB;
       
   794 
       
   795    exitAfterBitError:
       
   796       if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
   797          return VDX_ERR_BIB;
       
   798       return VDX_OK_BUT_BIT_ERROR;
       
   799 }
       
   800 
       
   801 
       
   802 /* {{-output"vdxFlushSEI.txt"}} */
       
   803 /*
       
   804  * vdxFlushSEI
       
   805  *    
       
   806  *
       
   807  * Parameters:
       
   808  *    inBuffer                   pointer to bit buffer instance
       
   809  *    bitErrorIndication         non-zero if a bit error has been detected
       
   810  *                               within the bits accessed in this function,
       
   811  *                               see biterr.h for possible values
       
   812  *
       
   813  * Function:
       
   814  *    This function discards (flushes) all consequent PEI/PSUPP pairs.
       
   815  *
       
   816  * Returns:
       
   817  *    VDX_OK                     the function was successful
       
   818  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
   819  *                               occured
       
   820  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
   821  *
       
   822  *    
       
   823  */
       
   824 
       
   825 int vdxFlushSEI(
       
   826    bibBuffer_t *inBuffer,
       
   827    int *bitErrorIndication )
       
   828 /* {{-output"vdxFlushSEI.txt"}} */
       
   829 {
       
   830    int
       
   831       numBitsGot;
       
   832    int16
       
   833       bibError = 0;
       
   834    u_int32 
       
   835       pei,
       
   836       psupp;
       
   837 
       
   838    vdxAssert(inBuffer != NULL);
       
   839    vdxAssert(bitErrorIndication != NULL);
       
   840 
       
   841    do {
       
   842       /* PEI */
       
   843       pei = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   844 
       
   845 
       
   846       if (pei) {
       
   847          /* PSUPP */
       
   848          psupp = bibGetBits(8, inBuffer, &numBitsGot, bitErrorIndication, 
       
   849             &bibError);
       
   850          // ignore the value of psupp; this is flush-function
       
   851          if ( psupp )
       
   852             {
       
   853             }
       
   854 
       
   855 
       
   856       }
       
   857    } while (pei);   
       
   858 
       
   859    return VDX_OK;
       
   860 
       
   861 }
       
   862 
       
   863 
       
   864 /* {{-output"vdxGetSEI.txt"}} */
       
   865 /*
       
   866  * vdxGetSEI
       
   867  *    
       
   868  *
       
   869  * Parameters:
       
   870  *    inBuffer                   pointer to bit buffer instance
       
   871  *
       
   872  *    ftype                      FTYPE field as defined in table L.1 of H.263,
       
   873  *                               -1 if the value is not valid
       
   874  *
       
   875  *    dsize                      DSIZE as defined in section L.2 of H.263
       
   876  *
       
   877  *    parameterData              an array of (min) 16 entries,
       
   878  *                               filled with dsize octets of parameter data
       
   879  *
       
   880  *    fLast                      set to 1 if the first PEI indicates that
       
   881  *                               no PSUPPs follow. Otherwise 0.
       
   882  *
       
   883  *    bitErrorIndication         non-zero if a bit error has been detected
       
   884  *                               within the bits accessed in this function,
       
   885  *                               see biterr.h for possible values
       
   886  *
       
   887  * Function:
       
   888  *    This function gets supplemental enhancement information as defined
       
   889  *    in Annex L of H.263.
       
   890  *
       
   891  * Note:
       
   892  *    The start code emulation prevention necessity using "Do Nothing" function
       
   893  *    is not checked. See section L.3 of H.263 for more details.
       
   894  *    
       
   895  * Returns:
       
   896  *    VDX_OK                     the function was successful
       
   897  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
   898  *                               occured
       
   899  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
   900  *
       
   901  *    
       
   902  */
       
   903 
       
   904 int vdxGetSEI(
       
   905    bibBuffer_t *inBuffer,
       
   906    int *ftype,
       
   907    int *dsize,
       
   908    u_char *parameterData,
       
   909    int *fLast,
       
   910    int *bitErrorIndication)
       
   911 /* {{-output"vdxGetSEI.txt"}} */
       
   912 {
       
   913    int
       
   914       numBitsGot,
       
   915       paramNum,
       
   916       lftype, /* local FTYPE */
       
   917       ldsize; /* local DSZIE */
       
   918    int16
       
   919       bibError = 0;
       
   920    u_int32 
       
   921       pei;
       
   922 
       
   923    vdxAssert(inBuffer != NULL);
       
   924    vdxAssert(ftype != NULL);
       
   925    vdxAssert(dsize != NULL);
       
   926    vdxAssert(parameterData != NULL);
       
   927    vdxAssert(fLast != NULL);
       
   928    vdxAssert(bitErrorIndication != NULL);
       
   929 
       
   930    *ftype = -1;
       
   931    *dsize = 0;   
       
   932 
       
   933    /* PEI */
       
   934    pei = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   935 
       
   936 
       
   937    if (pei) {
       
   938       *fLast = 0;
       
   939 
       
   940       /* FTYPE */
       
   941       lftype = (int) bibGetBits(4, inBuffer, &numBitsGot, bitErrorIndication, 
       
   942          &bibError);
       
   943 
       
   944 
       
   945       /* DSIZE */
       
   946       ldsize = (int) bibGetBits(4, inBuffer, &numBitsGot, bitErrorIndication, 
       
   947          &bibError);
       
   948 
       
   949    }
       
   950 
       
   951    else {
       
   952       *fLast = 1;
       
   953       return VDX_OK;
       
   954    }
       
   955 
       
   956    for (paramNum = 0; paramNum < ldsize; paramNum++) {
       
   957       /* PEI */
       
   958       pei = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
   959 
       
   960 
       
   961       if (!pei) {
       
   962          deb0p("ERROR. DSIZE does not match with PEI.\n");
       
   963          *fLast = 1;
       
   964          return VDX_OK_BUT_BIT_ERROR;
       
   965       }
       
   966 
       
   967       /* PSUPP containing parameter data */
       
   968       parameterData[paramNum] = (u_char) bibGetBits(8, inBuffer, &numBitsGot, 
       
   969          bitErrorIndication, &bibError);
       
   970 
       
   971    }
       
   972 
       
   973    *ftype = lftype;
       
   974    *dsize = ldsize;
       
   975      
       
   976    return VDX_OK;
       
   977 
       
   978 }
       
   979 
       
   980 
       
   981 /* {{-output"vdxGetAndParseSEI.txt"}} */
       
   982 /*
       
   983  * vdxGetAndParseSEI
       
   984  *
       
   985  * Parameters:
       
   986  *    inBuffer                   pointer to bit buffer instance
       
   987  *
       
   988  *    fPLUSPTYPE                 indicates if PLUSPTYPE is in use
       
   989  *
       
   990  *    numScalabilityLayers       -1 if the very first picture,
       
   991  *                               0 if Annex N scalability layers not in use,
       
   992  *                               2..15 if Annex N scalability layers in use
       
   993  *
       
   994  *    sei                        parsed SEI data
       
   995  *
       
   996  *    bitErrorIndication         non-zero if a bit error has been detected
       
   997  *                               within the bits accessed in this function,
       
   998  *                               see biterr.h for possible values
       
   999  *
       
  1000  * Function:
       
  1001  *    This function gets supplemental enhancement information as defined
       
  1002  *    in Annex L and W of H.263 and returns the parsed data.
       
  1003  *
       
  1004  * Returns:
       
  1005  *    VDX_OK                     the function was successful
       
  1006  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1007  *                               occured
       
  1008  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1009  */
       
  1010 
       
  1011 int vdxGetAndParseSEI(
       
  1012    bibBuffer_t *inBuffer,
       
  1013    int fPLUSPTYPE,
       
  1014    int numScalabilityLayers,
       
  1015    vdxSEI_t *sei,
       
  1016    int *bitErrorIndication)
       
  1017 /* {{-output"vdxGetAndParseSEI.txt"}} */
       
  1018 {
       
  1019    int 
       
  1020        ret = VDX_OK,              /* Temporary variable to carry return values */
       
  1021       fSecondFTYPESet = 0,       /* 1 = Extended FTYPE in use, 
       
  1022                                     0 = Annex L/W FTYPE in use,
       
  1023                                     see L.15 for further details */
       
  1024       ftype,                     /* FTYPE as in section L.2 of H.263 */
       
  1025       dsize,                     /* DSIZE as in section L.2 of H.263 */
       
  1026       fLast,                     /* 1 if PEI is zero, 0 otherwise */
       
  1027       cont = 0,                  /* CONT as in section W.6 of H.263 */ 
       
  1028       ebit,                      /* EBIT as in section W.6 of H.263 */
       
  1029       mtype,                     /* MTYPE as in section W.6 of H.263 */
       
  1030       prevCONT = 0,              /* CONT field in the previous picture msg */
       
  1031       prevMTYPE = -1;            /* MTYPE in the previous picture msg */
       
  1032       
       
  1033    u_char
       
  1034       parameterData[16];
       
  1035 
       
  1036    /* Initialize output data */
       
  1037    sei->fFullPictureFreezeRequest = 0;
       
  1038    sei->fFullPictureSnapshot = 0;
       
  1039    sei->snapshotId = 0;
       
  1040    sei->snapshotStatus = 255;
       
  1041    sei->scalabilityLayer = -1;
       
  1042    sei->numScalabilityLayers = 0;
       
  1043    memset(sei->prevPicHeader, 0, VDX_MAX_BYTES_IN_PIC_HEADER);
       
  1044    sei->numBytesInPrevPicHeader = 0;
       
  1045    sei->numBitsInLastByteOfPrevPicHeader = 0;
       
  1046    sei->fPrevPicHeaderTooLarge = 0;
       
  1047 
       
  1048    do {
       
  1049       /* Get supplemental enhancement information */
       
  1050       ret = vdxGetSEI(inBuffer, &ftype, &dsize, parameterData, &fLast, 
       
  1051          bitErrorIndication);
       
  1052 
       
  1053       /* If fatal error or bit error */
       
  1054       if (ret != VDX_OK)
       
  1055          return ret;
       
  1056 
       
  1057       /* If the previous FTYPE indicated Extended Function Type */
       
  1058       if (fSecondFTYPESet) {
       
  1059 
       
  1060          /* Let's discard FTYPE/DSIZE and parameters as suggested in 
       
  1061             section L.15 of H.263 Recommendation to allow backward
       
  1062             compatibility to to-be-defined set of extended FTYPEs. */
       
  1063             
       
  1064          /* The next expected FTYPE is a normal one defined in Annex L/W. */
       
  1065          fSecondFTYPESet = 0;
       
  1066 
       
  1067          continue;
       
  1068       }
       
  1069 
       
  1070       switch (ftype) {
       
  1071 
       
  1072          /* If "Reserved" */
       
  1073          case 0:
       
  1074             ret = vdxActAfterIncorrectSEI(
       
  1075                inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1076             break;
       
  1077 
       
  1078          /* If "Do Nothing" */
       
  1079          case 1:
       
  1080 
       
  1081             if (dsize != 0) {
       
  1082                ret = vdxActAfterIncorrectSEI(
       
  1083                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1084             }
       
  1085 
       
  1086             break;
       
  1087 
       
  1088          /* If Full-Picture Freeze Request */
       
  1089          case 2:
       
  1090             if (dsize != 0) {
       
  1091                ret = vdxActAfterIncorrectSEI(
       
  1092                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1093             }
       
  1094 
       
  1095             else
       
  1096                sei->fFullPictureFreezeRequest = 1;
       
  1097 
       
  1098             break;
       
  1099 
       
  1100          /* If Partial-Picture Freeze Request */
       
  1101          case 3:
       
  1102             if (dsize != 4) {
       
  1103                ret = vdxActAfterIncorrectSEI(
       
  1104                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1105             }
       
  1106 
       
  1107             break;
       
  1108 
       
  1109          /* If Resizing Partial-Picture Freeze Request */
       
  1110          case 4:
       
  1111             if (dsize != 8) {
       
  1112                ret = vdxActAfterIncorrectSEI(
       
  1113                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1114             }
       
  1115 
       
  1116             break;
       
  1117 
       
  1118          /* If Partial-Picture Freeze-Release Request */
       
  1119          case 5:
       
  1120             if (dsize != 4) {
       
  1121                ret = vdxActAfterIncorrectSEI(
       
  1122                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1123             }
       
  1124 
       
  1125             break;
       
  1126 
       
  1127          /* If Full-Picture Snapshot Tag */
       
  1128          case 6:
       
  1129             if (dsize != 4) {
       
  1130                ret = vdxActAfterIncorrectSEI(
       
  1131                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1132             }
       
  1133 
       
  1134             else {
       
  1135                int i;
       
  1136                sei->fFullPictureSnapshot = 1;
       
  1137                /* store 32-bit snapshot ID, first byte is the least significant */
       
  1138                for (i = 0; i < 4; i++)
       
  1139                   sei->snapshotId |= (parameterData[i] << (i<<3 /**8*/));
       
  1140             }
       
  1141 
       
  1142             break;
       
  1143 
       
  1144          /* If Partial-Picture Snapshot Tag */
       
  1145          case 7:
       
  1146             if (dsize != 8) {
       
  1147                ret = vdxActAfterIncorrectSEI(
       
  1148                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1149             }
       
  1150 
       
  1151             break;
       
  1152 
       
  1153          /* If Video Time Segment Start Tag */
       
  1154          case 8:
       
  1155             if (dsize != 4) {
       
  1156                ret = vdxActAfterIncorrectSEI(
       
  1157                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1158             }
       
  1159 
       
  1160             break;
       
  1161 
       
  1162          /* If Video Time Segment End Tag */
       
  1163          case 9:
       
  1164             if (dsize != 4) {
       
  1165                ret = vdxActAfterIncorrectSEI(
       
  1166                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1167             }
       
  1168 
       
  1169             break;
       
  1170 
       
  1171          /* If Progressive Refinement Segment Start Tag */
       
  1172          case 10:
       
  1173             if (dsize != 4) {
       
  1174                ret = vdxActAfterIncorrectSEI(
       
  1175                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1176             }
       
  1177 
       
  1178             break;
       
  1179 
       
  1180          /* If Progressive Refinement Segment End Tag */
       
  1181          case 11:
       
  1182             if (dsize != 4) {
       
  1183                ret = vdxActAfterIncorrectSEI(
       
  1184                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1185             }
       
  1186 
       
  1187             break;
       
  1188 
       
  1189          /* If Chroma Keying Information */
       
  1190          case 12:
       
  1191             if (dsize < 1 || dsize > 9) {
       
  1192                ret = vdxActAfterIncorrectSEI(
       
  1193                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1194             }
       
  1195 
       
  1196             break;
       
  1197 
       
  1198          /* If Fixed-Point IDCT */
       
  1199          case 13:
       
  1200             if (dsize != 1) {
       
  1201                ret = vdxActAfterIncorrectSEI(
       
  1202                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1203             }
       
  1204 
       
  1205             break;
       
  1206 
       
  1207          /* If Picture Message */
       
  1208          case 14:
       
  1209             /* DSIZE shall be at least 1 to carry CONT, EBIT, and MTYPE */
       
  1210             if (dsize < 1) {
       
  1211                ret = vdxActAfterIncorrectSEI(
       
  1212                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1213             }
       
  1214 
       
  1215             else {
       
  1216                cont = ( (parameterData[0] & 0x80) >> 7 ); 
       
  1217                ebit = ( (parameterData[0] & 0x70) >> 4 ); 
       
  1218                mtype = (parameterData[0] & 0x0f);
       
  1219 
       
  1220                if (mtype < 1 || mtype > 5) {
       
  1221                   /* Non-text message, check restriction in W.6.2 */
       
  1222                   if (ebit != 0 && (cont == 1 || dsize == 1)) {
       
  1223                      ret = vdxActAfterIncorrectSEI(
       
  1224                         inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1225                      break;
       
  1226                   }
       
  1227                }
       
  1228 
       
  1229                /* If the previous picture message indicated that the data
       
  1230                   continues in the next picture message, but the current
       
  1231                   message type differs from the previous one
       
  1232                   (restricted in section W.6.1 of the H.263 Recommendation */
       
  1233                if (prevCONT && mtype != prevMTYPE) {
       
  1234                   ret = vdxActAfterIncorrectSEI(
       
  1235                      inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1236                   break;
       
  1237                }
       
  1238             
       
  1239                /* If arbitrary binary data */
       
  1240                if (mtype == 0) {
       
  1241 
       
  1242                   /* Proprietary snapshot status indication */
       
  1243                   if (parameterData[1] == 83 && parameterData[2] == 115) {
       
  1244                      if (dsize != 4) {
       
  1245                         ret = vdxActAfterIncorrectSEI(
       
  1246                            inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1247                         break;
       
  1248                      }
       
  1249                      sei->snapshotStatus = parameterData[3];
       
  1250                   }
       
  1251 
       
  1252                   /* Proprietary Annex N scalability layer 
       
  1253                      indication */
       
  1254                   else if (parameterData[1] == 83 && parameterData[2] == 108) {
       
  1255                      if (dsize != 4) {
       
  1256                         ret = vdxActAfterIncorrectSEI(
       
  1257                            inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1258                         break;
       
  1259                      }
       
  1260                      sei->scalabilityLayer = parameterData[3] >> 4;
       
  1261                      sei->numScalabilityLayers = parameterData[3] & 15;
       
  1262 
       
  1263                      /* If less than two scalability layers or
       
  1264                         max number of scalability layers changes during
       
  1265                         the sequence */
       
  1266                      if (sei->numScalabilityLayers < 2 ||
       
  1267                         (numScalabilityLayers >= 0 &&
       
  1268                            sei->numScalabilityLayers != 
       
  1269                            numScalabilityLayers)) {
       
  1270 
       
  1271                         ret = vdxActAfterIncorrectSEI(
       
  1272                            inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1273                         break;
       
  1274                      } 
       
  1275                   }
       
  1276                }
       
  1277 
       
  1278                /* Else if Previous Picture Header Repetition */
       
  1279                else if (mtype == 7) {
       
  1280                   int
       
  1281                      firstIndexToWrite,
       
  1282                      numBytesToWrite;
       
  1283                   
       
  1284                   if (sei->numBytesInPrevPicHeader == 0)
       
  1285                      /* The first two bytes of PSC = 0x00 0x00 */
       
  1286                      sei->numBytesInPrevPicHeader = 2;
       
  1287                      
       
  1288                   firstIndexToWrite = sei->numBytesInPrevPicHeader;
       
  1289                   numBytesToWrite = dsize - 1;
       
  1290                   sei->numBitsInLastByteOfPrevPicHeader = 8 - ebit;
       
  1291 
       
  1292                   /* If buffer would overflow */
       
  1293                   if (firstIndexToWrite + numBytesToWrite > 
       
  1294                      VDX_MAX_BYTES_IN_PIC_HEADER) {
       
  1295                      numBytesToWrite = 
       
  1296                         VDX_MAX_BYTES_IN_PIC_HEADER - firstIndexToWrite;
       
  1297                      sei->numBitsInLastByteOfPrevPicHeader = 8;
       
  1298                      sei->fPrevPicHeaderTooLarge = 1;
       
  1299                   }
       
  1300 
       
  1301                   if (numBytesToWrite) {
       
  1302                      memcpy(
       
  1303                         &sei->prevPicHeader[firstIndexToWrite], 
       
  1304                         &parameterData[1],
       
  1305                         numBytesToWrite);
       
  1306 
       
  1307                      sei->numBytesInPrevPicHeader += numBytesToWrite;
       
  1308                   }
       
  1309                }
       
  1310 
       
  1311                prevCONT = cont;
       
  1312                prevMTYPE = mtype;
       
  1313             }
       
  1314             break;
       
  1315 
       
  1316          /* If Extended Function Type */
       
  1317          case 15:
       
  1318             if (dsize != 0) {
       
  1319                ret = vdxActAfterIncorrectSEI(
       
  1320                   inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1321             }
       
  1322 
       
  1323             fSecondFTYPESet = 1;
       
  1324 
       
  1325             break;
       
  1326       }
       
  1327    } while (!fLast && ret == VDX_OK);
       
  1328 
       
  1329    /* If a picture message was not completed and the fault has not been
       
  1330       tracked earlier */
       
  1331    if (prevCONT && ret == VDX_OK) {
       
  1332       ret = vdxActAfterIncorrectSEI(
       
  1333          inBuffer, fPLUSPTYPE, &fLast, bitErrorIndication);
       
  1334    }
       
  1335 
       
  1336    return ret;
       
  1337 }
       
  1338 
       
  1339 
       
  1340 /*
       
  1341  * GOB Layer Global Functions
       
  1342  */
       
  1343 
       
  1344 /* {{-output"vdxGetGOBHeader.txt"}} */
       
  1345 /*
       
  1346  * vdxGetGOBHeader
       
  1347  *    
       
  1348  *
       
  1349  * Parameters:
       
  1350  *    inBuffer                   pointer to bit buffer instance
       
  1351  *    inpParam                   input parameters
       
  1352  *    header                     output parameters: GOB header
       
  1353  *    bitErrorIndication         non-zero if a bit error has been detected
       
  1354  *                               within the bits accessed in this function,
       
  1355  *                               see biterr.h for possible values
       
  1356  * Function:
       
  1357  *    This function reads the H.263 GOB header.
       
  1358  *
       
  1359  * Returns:
       
  1360  *    VDX_OK                     the function was successful
       
  1361  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1362  *                               occured
       
  1363  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1364  *
       
  1365  *    
       
  1366  */
       
  1367 
       
  1368 int vdxGetGOBHeader(
       
  1369    bibBuffer_t *inBuffer, 
       
  1370    const vdxGetGOBHeaderInputParam_t *inpParam,
       
  1371    vdxGOBHeader_t *header,
       
  1372    int *bitErrorIndication,
       
  1373    int aColorEffect,
       
  1374    int* aStartByteIndex,
       
  1375    int* aStartBitIndex, CMPEG4Transcoder *hTranscoder)
       
  1376 /* {{-output"vdxGetGOBHeader.txt"}} */
       
  1377 {
       
  1378    int
       
  1379       numBitsGot;
       
  1380    int16
       
  1381       bibError = 0;
       
  1382    u_int32 
       
  1383       tmp = 0;          /* temporary variable for reading some redundant bits */
       
  1384 
       
  1385    vdxAssert(inBuffer != NULL);
       
  1386    vdxAssert(inpParam != NULL);
       
  1387    vdxAssert(header != NULL);
       
  1388    vdxAssert(bitErrorIndication != NULL);
       
  1389 
       
  1390    memset(header, 0, sizeof(vdxGOBHeader_t));
       
  1391    *bitErrorIndication = 0;
       
  1392 
       
  1393    /* Assume that the existence of a GBSC has been checked, and
       
  1394       it is the next code in the buffer */
       
  1395 
       
  1396    /* Flush stuffing (GSTUF) */
       
  1397    if (inpParam->numStuffBits)
       
  1398    {
       
  1399       bibFlushBits(inpParam->numStuffBits, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  1400       // if chroma has been removed and GSTUF is there, skip GSTUF bits to point to byte-aligned GBSC
       
  1401       if(aColorEffect==1 || aColorEffect==2)
       
  1402       {
       
  1403          (*aStartByteIndex)++;
       
  1404          *aStartBitIndex = 7;
       
  1405       }
       
  1406    }
       
  1407 
       
  1408    /* MVE */
       
  1409      hTranscoder->H263GOBSliceHeaderBegin();
       
  1410 
       
  1411     /* Flush GBSC */
       
  1412    bibFlushBits(17, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  1413 
       
  1414 
       
  1415    /* GBSC cannot contain fatal bit errors (checked earlier) */
       
  1416 
       
  1417    /* GN */
       
  1418    header->gn = (int) bibGetBits(5, inBuffer, &numBitsGot, 
       
  1419       bitErrorIndication, &bibError);
       
  1420 
       
  1421 
       
  1422    /* If the GN field contains a fatal bit error or
       
  1423       the value of GN is not in the valid range 1..24.
       
  1424       This range is defined in section 5.2.3 of the standard.
       
  1425 
       
  1426       Notice that in general case one cannot assume that the maximum GN
       
  1427       of the picture is the same as in the previous picture (GOB) since
       
  1428       a picture header may have been lost and the header could have contained
       
  1429       an indication of a picture format change. Therefore we have to stick
       
  1430       to the overall maximum GN, equal to 24, here. */
       
  1431 
       
  1432    if ( header->gn == 0 /* PSC */ || 
       
  1433       header->gn > 24) {
       
  1434       deb0p("ERROR. GN illegal.\n");
       
  1435       goto exitAfterBitError;
       
  1436    }
       
  1437 
       
  1438    if (inpParam->fCPM) {
       
  1439       /* GSBI */
       
  1440       header->gsbi = (int) bibGetBits(2, inBuffer, &numBitsGot, 
       
  1441          bitErrorIndication, &bibError);
       
  1442 
       
  1443 
       
  1444    }
       
  1445    /* GFID */
       
  1446    header->gfid = (int) bibGetBits(2, inBuffer, &numBitsGot, 
       
  1447       bitErrorIndication, &bibError);
       
  1448 
       
  1449 
       
  1450    /* GQUANT */
       
  1451    header->gquant = (int) bibGetBits(5, inBuffer, &numBitsGot, 
       
  1452       bitErrorIndication, &bibError);
       
  1453 
       
  1454 
       
  1455    if (header->gquant == 0) {
       
  1456       deb0p("ERROR. Illegal GQUANT.\n");
       
  1457       goto exitAfterBitError;
       
  1458    }
       
  1459 
       
  1460    if (inpParam->fRPS) {
       
  1461       /* TRI */
       
  1462       header->tri = (int) bibGetBits(1, inBuffer, &numBitsGot, 
       
  1463          bitErrorIndication, &bibError);
       
  1464 
       
  1465 
       
  1466       /* If TR present */
       
  1467       if (header->tri) {
       
  1468          /* TR */
       
  1469          if (inpParam->fCustomPCF)
       
  1470             header->tr = (int) bibGetBits(10, inBuffer, &numBitsGot, 
       
  1471                bitErrorIndication, &bibError);
       
  1472          else
       
  1473             header->tr = (int) bibGetBits(8, inBuffer, &numBitsGot, 
       
  1474                bitErrorIndication, &bibError);
       
  1475 
       
  1476 
       
  1477       }
       
  1478 
       
  1479       /* TRPI */
       
  1480       header->trpi = (int) bibGetBits(1, inBuffer, &numBitsGot, 
       
  1481          bitErrorIndication, &bibError);
       
  1482 
       
  1483 
       
  1484       /* If TRP present */
       
  1485       if (header->trpi) {
       
  1486          /* TRP */
       
  1487          header->trp = (int) bibGetBits(10, inBuffer, &numBitsGot, 
       
  1488             bitErrorIndication, &bibError);
       
  1489 
       
  1490 
       
  1491          deb2f(bcmDebFile, "TRPI in GOB header. GN = %d, TRP = %d.\n", 
       
  1492             header->gn, header->trp);         
       
  1493       }
       
  1494 
       
  1495       /* BCI */
       
  1496          /* Code following the standard */
       
  1497 
       
  1498          /* BCI */
       
  1499          tmp = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  1500 
       
  1501 
       
  1502          if ( tmp ) {
       
  1503             /* BCM not supported */
       
  1504             deb0p("ERROR. BCM not supported.\n");
       
  1505             goto exitAfterBitError;
       
  1506          }
       
  1507 
       
  1508          else {
       
  1509             tmp = bibGetBits(1, inBuffer, &numBitsGot,bitErrorIndication, &bibError);
       
  1510 
       
  1511 
       
  1512             if ( !tmp ) {
       
  1513                /* BCI '00' is illegal */
       
  1514                deb0p("ERROR. Illegal BCI.\n");
       
  1515                goto exitAfterBitError;
       
  1516             }
       
  1517          }
       
  1518 
       
  1519 
       
  1520    }
       
  1521 
       
  1522    /* Check success and return */
       
  1523 
       
  1524 
       
  1525    /* If no error in bit buffer functions */
       
  1526    if (!bibError)
       
  1527       return VDX_OK;
       
  1528 
       
  1529    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1530    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1531       return VDX_OK_BUT_BIT_ERROR;
       
  1532    }
       
  1533 
       
  1534    /* Else other error in bit buffer functions */
       
  1535    else
       
  1536       return VDX_ERR_BIB;
       
  1537 
       
  1538    exitAfterBitError:
       
  1539       if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
  1540          return VDX_ERR_BIB;
       
  1541 
       
  1542       return VDX_OK_BUT_BIT_ERROR;
       
  1543 }
       
  1544 
       
  1545 /*
       
  1546  * Slice Layer Global Functions
       
  1547  */
       
  1548 
       
  1549 /* {{-output"vdxGetSliceHeader.txt"}} */
       
  1550 /*
       
  1551  * vdxGetSliceHeader
       
  1552  *    
       
  1553  *
       
  1554  * Parameters:
       
  1555  *    inBuffer                   pointer to bit buffer instance
       
  1556  *    inpParam                   input parameters
       
  1557  *    header                     output parameters: Slice header
       
  1558  *    bitErrorIndication         non-zero if a bit error has been detected
       
  1559  *                               within the bits accessed in this function,
       
  1560  *                               see biterr.h for possible values
       
  1561  * Function:
       
  1562  *    This function reads the H.263 Slice header.
       
  1563  *
       
  1564  * Returns:
       
  1565  *    VDX_OK                     the function was successful
       
  1566  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1567  *                               occured
       
  1568  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1569  *
       
  1570  */
       
  1571 
       
  1572 int vdxGetSliceHeader(
       
  1573    bibBuffer_t *inBuffer, 
       
  1574    const vdxGetSliceHeaderInputParam_t *inpParam,
       
  1575    vdxSliceHeader_t *header,
       
  1576    int *bitErrorIndication)
       
  1577 /* {{-output"vdxGetSliceHeader.txt"}} */
       
  1578 {
       
  1579    int
       
  1580       numBitsGot;
       
  1581    int16
       
  1582       bibError = 0;
       
  1583    u_int32 
       
  1584       tmp = 0;          /* temporary variable for reading some redundant bits */
       
  1585 
       
  1586 
       
  1587    vdxAssert(inBuffer != NULL);
       
  1588    vdxAssert(inpParam != NULL);
       
  1589    vdxAssert(header != NULL);
       
  1590    vdxAssert(bitErrorIndication != NULL);
       
  1591 
       
  1592    memset(header, 0, sizeof(vdxSliceHeader_t));
       
  1593    *bitErrorIndication = 0;
       
  1594 
       
  1595    if (!inpParam->sliceHeaderAfterPSC)  {
       
  1596       /* Flush stuffing (GSTUF) */
       
  1597       if (inpParam->numStuffBits)
       
  1598          bibFlushBits(inpParam->numStuffBits, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  1599 
       
  1600       /* Flush SSC */
       
  1601       /* Assume that the existence of a SSC has been checked, and
       
  1602          it is the next code in the buffer */
       
  1603 
       
  1604       bibFlushBits(17, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  1605 
       
  1606 
       
  1607       /* SSC cannot contain fatal bit errors (checked earlier) */
       
  1608    }
       
  1609 
       
  1610    /* Read SEPB1 (K.2.3) */
       
  1611    tmp = (int) bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  1612                            &bibError);
       
  1613    
       
  1614 
       
  1615    if (tmp != 1)  {
       
  1616       deb0p("ERROR. SEPB1 illegal.\n");
       
  1617       goto exitAfterBitError;
       
  1618    }
       
  1619 
       
  1620    /* SSBI */
       
  1621    if ((inpParam->fCPM) && (!inpParam->sliceHeaderAfterPSC))  {
       
  1622       header->ssbi = (int) bibGetBits(4, inBuffer, &numBitsGot, 
       
  1623          bitErrorIndication, &bibError);
       
  1624 
       
  1625 
       
  1626       /* If the SSBI field contains a fatal bit error or
       
  1627       the value of SSBI is not in the valid range 9..12.
       
  1628       This range is defined in Table K.1/H.263 */
       
  1629       if ( tmp < 9  || 
       
  1630          tmp > 12) {
       
  1631          deb0p("ERROR. SSBI illegal.\n");
       
  1632          goto exitAfterBitError;
       
  1633       }
       
  1634       /* Set emulated GN value */
       
  1635       header->gn = header->ssbi + 16;
       
  1636       /* Set sub-bitstream number */
       
  1637       if (header->gn < 29) 
       
  1638          header->sbn = header->ssbi - 9;
       
  1639       else
       
  1640          header->sbn = 3;
       
  1641    }
       
  1642 
       
  1643 
       
  1644    /* MBA */
       
  1645    header->mba = (int) bibGetBits(inpParam->mbaFieldWidth, inBuffer, &numBitsGot, 
       
  1646          bitErrorIndication, &bibError);
       
  1647 
       
  1648 
       
  1649    /* If the MBA field contains a fatal bit error or
       
  1650       the value of MBA is larger than Max Value defined in Table K.2/H.263 */
       
  1651    if ( header->mba > inpParam->mbaMaxValue) {
       
  1652       deb0p("ERROR. MBA illegal.\n");
       
  1653       goto exitAfterBitError;
       
  1654    }
       
  1655 
       
  1656    /* SEPB2 */
       
  1657    if (inpParam->sliceHeaderAfterPSC)  {
       
  1658       if (inpParam->fRectangularSlices)   {
       
  1659          tmp = (int) bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  1660                               &bibError);
       
  1661 
       
  1662 
       
  1663          if (tmp != 1)  {
       
  1664             deb0p("ERROR. SEPB1 illegal.\n");
       
  1665             goto exitAfterBitError;
       
  1666          }
       
  1667       }
       
  1668    }
       
  1669    else  {
       
  1670       if (inpParam->fCPM)  {
       
  1671          if (inpParam->mbaFieldWidth > 9) {
       
  1672             tmp = (int) bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  1673                               &bibError);
       
  1674 
       
  1675 
       
  1676             if (tmp != 1)  {
       
  1677                deb0p("ERROR. SEPB1 illegal.\n");
       
  1678                goto exitAfterBitError;
       
  1679             }
       
  1680          }
       
  1681       }
       
  1682       else  {
       
  1683          if (inpParam->mbaFieldWidth > 11) {
       
  1684             tmp = (int) bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  1685                               &bibError);
       
  1686 
       
  1687 
       
  1688             if (tmp != 1)  {
       
  1689                deb0p("ERROR. SEPB1 illegal.\n");
       
  1690                goto exitAfterBitError;
       
  1691             }
       
  1692          }
       
  1693       }
       
  1694    }
       
  1695 
       
  1696    /* SQUANT */
       
  1697    if (!inpParam->sliceHeaderAfterPSC)  {
       
  1698       header->squant = (int) bibGetBits(5, inBuffer, &numBitsGot, 
       
  1699          bitErrorIndication, &bibError);
       
  1700       if ( bibError )
       
  1701             return VDX_ERR_BIB;
       
  1702 
       
  1703       /* If the SQUANT field contains a fatal bit error or
       
  1704          the value of SQUANT is between 1 and 31 */
       
  1705       if ( header->squant == 0) {
       
  1706          deb0p("ERROR. SQUANT illegal.\n");
       
  1707          goto exitAfterBitError;
       
  1708       }
       
  1709    }
       
  1710 
       
  1711    /* SWI */
       
  1712    if (inpParam->fRectangularSlices)   {
       
  1713       header->swi = (int) bibGetBits(inpParam->swiFieldWidth, inBuffer, &numBitsGot, 
       
  1714          bitErrorIndication, &bibError);
       
  1715 
       
  1716 
       
  1717       /* If the MBA field contains a fatal bit error or
       
  1718          the value of MBA is larger than Max Value defined in Table K.2/H.263 */
       
  1719       if ( header->swi > inpParam->swiMaxValue) {
       
  1720          deb0p("ERROR. SWI illegal.\n");
       
  1721          goto exitAfterBitError;
       
  1722       }
       
  1723    }
       
  1724 
       
  1725    /* Read SEPB3 */
       
  1726    tmp = (int) bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  1727                            &bibError);
       
  1728 
       
  1729 
       
  1730    if (tmp != 1)  {
       
  1731       deb0p("ERROR. SEPB3 illegal.\n");
       
  1732       goto exitAfterBitError;
       
  1733    }
       
  1734 
       
  1735    /* GFID */
       
  1736    if (!inpParam->sliceHeaderAfterPSC)  {
       
  1737       header->gfid = (int) bibGetBits(2, inBuffer, &numBitsGot, 
       
  1738          bitErrorIndication, &bibError);
       
  1739 
       
  1740 
       
  1741    }
       
  1742 
       
  1743    if (!inpParam->sliceHeaderAfterPSC && inpParam->fRPS) {
       
  1744       /* TRI */
       
  1745       header->tri = (int) bibGetBits(1, inBuffer, &numBitsGot, 
       
  1746          bitErrorIndication, &bibError);
       
  1747 
       
  1748 
       
  1749       /* If TR present */
       
  1750       if (header->tri) {
       
  1751          /* TR */
       
  1752 /*         if (inpParam->fCustomPCF)
       
  1753             header->tr = (int) bibGetBits(10, inBuffer, &numBitsGot, 
       
  1754                bitErrorIndication, &bibError);
       
  1755          else*/
       
  1756             header->tr = (int) bibGetBits(8, inBuffer, &numBitsGot, 
       
  1757                bitErrorIndication, &bibError);
       
  1758 
       
  1759 
       
  1760       }
       
  1761 
       
  1762       /* TRPI */
       
  1763       header->trpi = (int) bibGetBits(1, inBuffer, &numBitsGot, 
       
  1764          bitErrorIndication, &bibError);
       
  1765 
       
  1766 
       
  1767       /* If TRP present */
       
  1768       if (header->trpi) {
       
  1769          /* TRP */
       
  1770          header->trp = (int) bibGetBits(10, inBuffer, &numBitsGot, 
       
  1771             bitErrorIndication, &bibError);
       
  1772 
       
  1773 
       
  1774          deb2f(bcmDebFile, "TRPI in GOB header. GN = %d, TRP = %d.\n", 
       
  1775             header->gn, header->trp);         
       
  1776       }
       
  1777 
       
  1778       /* BCI */
       
  1779          /* Code following the standard */
       
  1780 
       
  1781          /* BCI */
       
  1782          tmp = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  1783 
       
  1784 
       
  1785          if ( tmp ) {
       
  1786             /* BCM not supported */
       
  1787             deb0p("ERROR. BCM not supported.\n");
       
  1788             goto exitAfterBitError;
       
  1789          }
       
  1790 
       
  1791          else {
       
  1792             tmp = bibGetBits(1, inBuffer, &numBitsGot,bitErrorIndication, &bibError);
       
  1793 
       
  1794 
       
  1795             if ( !tmp ) {
       
  1796                /* BCI '00' is illegal */
       
  1797                deb0p("ERROR. Illegal BCI.\n");
       
  1798                goto exitAfterBitError;
       
  1799             }
       
  1800          }
       
  1801 
       
  1802 
       
  1803    }
       
  1804 
       
  1805    /* Check success and return */
       
  1806 
       
  1807 
       
  1808    /* If no error in bit buffer functions */
       
  1809    if (!bibError)
       
  1810       return VDX_OK;
       
  1811 
       
  1812    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1813    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1814       return VDX_OK_BUT_BIT_ERROR;
       
  1815    }
       
  1816 
       
  1817    /* Else other error in bit buffer functions */
       
  1818    else
       
  1819       return VDX_ERR_BIB;
       
  1820    exitAfterBitError:
       
  1821       if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
  1822          return VDX_ERR_BIB;
       
  1823 
       
  1824       return VDX_OK_BUT_BIT_ERROR;
       
  1825 }
       
  1826 /* {{-output"vdxGetMBAandSWIValues.txt"}} */
       
  1827 /*
       
  1828  * vdxGetIMBLayer
       
  1829  *    
       
  1830  *
       
  1831  * Parameters:
       
  1832  *    inBuffer                   pointer to bit buffer instance
       
  1833  *    inpParam                   input parameters
       
  1834  *    outParam                   output parameters
       
  1835  *    bitErrorIndication         non-zero if a bit error has been detected
       
  1836  *                               within the bits accessed in this function,
       
  1837  *                               see biterr.h for possible values
       
  1838  * Function:
       
  1839  *    This function reads the macroblock layer data of an INTRA macroblock.
       
  1840  *
       
  1841  * Returns:
       
  1842  *    Nothing
       
  1843  *
       
  1844  */
       
  1845 
       
  1846 void vdxGetMBAandSWIValues(
       
  1847    int width,
       
  1848    int height,
       
  1849    int fRRU,
       
  1850    int *mbaFieldWidth,
       
  1851    int *mbaMaxValue,
       
  1852    int *swiFieldWidth,
       
  1853    int *swiMaxValue
       
  1854    )
       
  1855 {
       
  1856    int 
       
  1857       pictureFormat;
       
  1858 
       
  1859    pictureFormat = vdxFrameSizeToPictureFormat(width, height);
       
  1860  
       
  1861    if (fRRU)  {
       
  1862       switch (pictureFormat)  {
       
  1863       case 0:
       
  1864          /* sub-QCIF */
       
  1865          *mbaFieldWidth = 5;
       
  1866          *mbaMaxValue = 11;
       
  1867          *swiFieldWidth = 3;
       
  1868          *swiMaxValue = 3;
       
  1869          break;
       
  1870       case 1:
       
  1871          /* QCIF */
       
  1872          *mbaFieldWidth = 6;
       
  1873          *mbaMaxValue = 29;
       
  1874          *swiFieldWidth = 3;
       
  1875          *swiMaxValue = 5;
       
  1876          break;
       
  1877       case 2:
       
  1878          /* CIF */
       
  1879          *mbaFieldWidth = 7;
       
  1880          *mbaMaxValue = 98;
       
  1881          *swiFieldWidth = 4;
       
  1882          *swiMaxValue = 10;
       
  1883          break;
       
  1884       case 3:
       
  1885          /* 4CIF */
       
  1886          *mbaFieldWidth = 9;
       
  1887          *mbaMaxValue = 395;
       
  1888          *swiFieldWidth = 5;
       
  1889          *swiMaxValue = 21;
       
  1890          break;
       
  1891       case 4:
       
  1892          /* 16CIF */
       
  1893          *mbaFieldWidth = 11;
       
  1894          *mbaMaxValue = 1583;
       
  1895          *swiFieldWidth = 6;
       
  1896          *swiMaxValue = 43;
       
  1897          break;
       
  1898       case 5:
       
  1899          /* 2048x1152 */
       
  1900          *mbaFieldWidth = 12;
       
  1901          *mbaMaxValue = 2303;
       
  1902          *swiFieldWidth = 6;
       
  1903          *swiMaxValue = 63;
       
  1904          break;
       
  1905       }
       
  1906    }
       
  1907    else  {
       
  1908       switch (pictureFormat)  {
       
  1909       case 0:
       
  1910          /* sub-QCIF */
       
  1911          *mbaFieldWidth = 6;
       
  1912          *mbaMaxValue = 47;
       
  1913          *swiFieldWidth = 4;
       
  1914          *swiMaxValue = 7;
       
  1915          break;
       
  1916       case 1:
       
  1917          /* QCIF */
       
  1918          *mbaFieldWidth = 7;
       
  1919          *mbaMaxValue = 98;
       
  1920          *swiFieldWidth = 4;
       
  1921          *swiMaxValue = 10;
       
  1922          break;
       
  1923       case 2:
       
  1924          /* CIF */
       
  1925          *mbaFieldWidth = 9;
       
  1926          *mbaMaxValue = 395;
       
  1927          *swiFieldWidth = 5;
       
  1928          *swiMaxValue = 21;
       
  1929          break;
       
  1930       case 3:
       
  1931          /* 4CIF */
       
  1932          *mbaFieldWidth = 11;
       
  1933          *mbaMaxValue = 1583;
       
  1934          *swiFieldWidth = 6;
       
  1935          *swiMaxValue = 43;
       
  1936          break;
       
  1937       case 4:
       
  1938          /* 16CIF */
       
  1939          *mbaFieldWidth = 13;
       
  1940          *mbaMaxValue = 6335;
       
  1941          *swiFieldWidth = 7;
       
  1942          *swiMaxValue = 87;
       
  1943          break;
       
  1944       case 5:
       
  1945          /* 2048x1152 */
       
  1946          *mbaFieldWidth = 14;
       
  1947          *mbaMaxValue = 9215;
       
  1948          *swiFieldWidth = 7;
       
  1949          *swiMaxValue = 127;
       
  1950          break;
       
  1951       }
       
  1952    }
       
  1953 }
       
  1954 
       
  1955 /*
       
  1956  * Macroblock Layer Global Functions
       
  1957  */
       
  1958 
       
  1959 /* {{-output"vdxGetIMBLayer.txt"}} */
       
  1960 /*
       
  1961  * vdxGetIMBLayer
       
  1962  *    
       
  1963  *
       
  1964  * Parameters:
       
  1965  *    inBuffer                   pointer to bit buffer instance
       
  1966  *    inpParam                   input parameters
       
  1967  *    outParam                   output parameters
       
  1968  *    bitErrorIndication         non-zero if a bit error has been detected
       
  1969  *                               within the bits accessed in this function,
       
  1970  *                               see biterr.h for possible values
       
  1971  * Function:
       
  1972  *    This function reads the macroblock layer data of an INTRA macroblock.
       
  1973  *
       
  1974  * Returns:
       
  1975  *    VDX_OK                     the function was successful
       
  1976  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1977  *                               occured
       
  1978  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1979  *
       
  1980  *    
       
  1981  */
       
  1982 
       
  1983    
       
  1984 int vdxGetIMBLayer(
       
  1985    bibBuffer_t *inBuffer, 
       
  1986    bibBuffer_t *outBuffer, 
       
  1987    bibBufferEdit_t *bufEdit,
       
  1988    int /*aColorEffect*/, 
       
  1989    int * /*aStartByteIndex*/, 
       
  1990    int * /*aStartBitIndex*/, 
       
  1991    TBool /*aGetDecodedFrame*/,
       
  1992    const vdxGetIMBLayerInputParam_t *inpParam,
       
  1993    vdxIMBLayer_t *outParam,
       
  1994    int *bitErrorIndication, CMPEG4Transcoder *hTranscoder)
       
  1995 
       
  1996 /* {{-output"vdxGetIMBLayer.txt"}} */
       
  1997 {
       
  1998    int
       
  1999       mcbpcIndex,
       
  2000       cbpyIndex,
       
  2001       retValue = VDX_OK,
       
  2002       fDQUANT = 0,
       
  2003       fINTRAMODE,
       
  2004       bitsGot = 0;
       
  2005    int16 bibError = 0;
       
  2006 
       
  2007    int StartByteIndex; 
       
  2008    int StartBitIndex;
       
  2009 
       
  2010    vdxAssert(inBuffer != NULL);
       
  2011    vdxAssert(inpParam != NULL);
       
  2012    vdxAssert(outParam != NULL);
       
  2013    vdxAssert(bitErrorIndication != NULL);
       
  2014      
       
  2015    /* MVE */
       
  2016      vdxIMBListItem_t *MBinstance;
       
  2017      // Create new MBInstance for the next MB 
       
  2018      MBinstance = (vdxIMBListItem_t *) malloc(sizeof(vdxIMBListItem_t));
       
  2019      if (!MBinstance)
       
  2020      {
       
  2021          deb("ERROR - vdxGetIMBLayer::MBinstance creation failed\n");
       
  2022          return H263D_ERROR;         
       
  2023      }
       
  2024      memset(MBinstance, 0, sizeof(vdxIMBListItem_t));
       
  2025      
       
  2026    /* Loop while MCBPC indicates stuffing */
       
  2027    do {
       
  2028      
       
  2029         StartByteIndex = inBuffer->getIndex; 
       
  2030         StartBitIndex  = inBuffer->bitIndex;
       
  2031          
       
  2032         /* MVE */
       
  2033         VDT_SET_START_POSITION(MBinstance,0,inBuffer->getIndex,inBuffer->bitIndex); // 0: mcbpc
       
  2034          
       
  2035         retValue = vdxGetMCBPCIntra(inBuffer, &mcbpcIndex, bitErrorIndication);
       
  2036         if (retValue != VDX_OK)
       
  2037              goto exitFunction;
       
  2038          
       
  2039         /* MVE */
       
  2040         /* remember to output the MB stufffing bits!! */
       
  2041         if (mcbpcIndex == 8) 
       
  2042         {
       
  2043             /* copy data from inbuffer to outbuffer  */
       
  2044             bufEdit->copyMode = CopyWhole; // copy whole
       
  2045             CopyStream(inBuffer,outBuffer,bufEdit,StartByteIndex,StartBitIndex);
       
  2046         }
       
  2047    } while (mcbpcIndex == 8);
       
  2048      
       
  2049     /* MVE */
       
  2050     VDT_SET_END_POSITION(MBinstance,0,inBuffer->getIndex,inBuffer->bitIndex); // 0: mcbpc
       
  2051      
       
  2052    /* CBPC (2 LSBs of MCBPC) */
       
  2053    outParam->cbpc = mcbpcIndex & 3;
       
  2054 
       
  2055    /* DQUANT is given for MCBPC indexes 4..7 */
       
  2056    fDQUANT = mcbpcIndex & 4;
       
  2057 
       
  2058    /* MB Type*/
       
  2059    outParam->mbType = (mcbpcIndex <4)?3:4;
       
  2060    
       
  2061    /* INTRA_MODE */
       
  2062    if (inpParam->fAIC)
       
  2063    {
       
  2064       retValue = vdxGetIntraMode(inBuffer,&fINTRAMODE,bitErrorIndication);
       
  2065       if (retValue != VDX_OK)
       
  2066          goto exitFunction;
       
  2067       outParam->predMode = fINTRAMODE;
       
  2068    }
       
  2069 
       
  2070    /* ac_pred_flag (1 bit) */
       
  2071    if (inpParam->fMPEG4) {
       
  2072       outParam->ac_pred_flag = (u_char) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  2073 
       
  2074    }
       
  2075 
       
  2076      /* MVE */
       
  2077    VDT_SET_START_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 2: cbpy
       
  2078 
       
  2079    /* CBPY */
       
  2080    retValue = vdxGetCBPY(inBuffer, &cbpyIndex, bitErrorIndication);
       
  2081    if (retValue != VDX_OK)
       
  2082       goto exitFunction;
       
  2083    outParam->cbpy = cbpyIndex;
       
  2084 
       
  2085      /* MVE */
       
  2086    VDT_SET_END_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 2: cbpy
       
  2087    VDT_SET_START_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  2088      
       
  2089    if (fDQUANT) {
       
  2090          retValue = vdxUpdateQuant(inBuffer, inpParam->fMQ, inpParam->quant,
       
  2091              &outParam->quant, bitErrorIndication);
       
  2092          if (retValue != VDX_OK)
       
  2093              goto exitFunction;
       
  2094    }
       
  2095      
       
  2096    /* Else no DQUANT */
       
  2097    else
       
  2098          outParam->quant = inpParam->quant;
       
  2099    
       
  2100    /* Color Toning */
       
  2101    hTranscoder->AfterMBLayer(outParam->quant);
       
  2102      
       
  2103 exitFunction:
       
  2104      
       
  2105      /* MVE */
       
  2106    VDT_SET_END_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  2107    outParam->mcbpc = mcbpcIndex;
       
  2108      
       
  2109    MBinstance->mcbpc = mcbpcIndex;
       
  2110    MBinstance->quant = outParam->quant;
       
  2111    MBinstance->cbpc  = outParam->cbpc;
       
  2112    MBinstance->cbpy  = outParam->cbpy;
       
  2113    MBinstance->ac_pred_flag = outParam->ac_pred_flag; 
       
  2114    MBinstance->dquant       = fDQUANT ?  outParam->quant - inpParam->quant : 0;
       
  2115      
       
  2116    hTranscoder->OneIMBDataStarted(MBinstance);
       
  2117    free(MBinstance);
       
  2118      
       
  2119    return retValue;
       
  2120 }
       
  2121 
       
  2122 
       
  2123 /* {{-output"vdxGetPPBMBLayer.txt"}} */
       
  2124 /*
       
  2125  * vdxGetPPBMBLayer
       
  2126  *    
       
  2127  *
       
  2128  * Parameters:
       
  2129  *    inBuffer                   pointer to bit buffer instance
       
  2130  *    inpParam                   input parameters
       
  2131  *    outParam                   output parameters
       
  2132  *    bitErrorIndication         non-zero if a bit error has been detected
       
  2133  *                               within the bits accessed in this function,
       
  2134  *                               see biterr.h for possible values
       
  2135  * Function:
       
  2136  *    This function reads the macroblock layer data of an INTER macroblock.
       
  2137  *
       
  2138  * Returns:
       
  2139  *    VDX_OK                     the function was successful
       
  2140  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  2141  *                               occured
       
  2142  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  2143  *
       
  2144  *    
       
  2145  */
       
  2146 
       
  2147 
       
  2148 int vdxGetPPBMBLayer(
       
  2149    bibBuffer_t *inBuffer, 
       
  2150    bibBuffer_t *outBuffer, 
       
  2151    bibBufferEdit_t *bufEdit,
       
  2152    int /*aColorEffect*/,
       
  2153    int */*aStartByteIndex*/, 
       
  2154    int */*aStartBitIndex*/, 
       
  2155    TBool /*aGetDecodedFrame*/, 
       
  2156    int * /*bwMBType*/, 
       
  2157    const vdxGetPPBMBLayerInputParam_t *inpParam,
       
  2158    vdxPPBMBLayer_t *outParam,
       
  2159    int *bitErrorIndication, CMPEG4Transcoder *hTranscoder)
       
  2160 
       
  2161 /* {{-output"vdxGetPPBMBLayer.txt"}} */
       
  2162 {
       
  2163    static const int mbTypeToMBClass[6] = 
       
  2164       {VDX_MB_INTER, VDX_MB_INTER, VDX_MB_INTER, 
       
  2165        VDX_MB_INTRA, VDX_MB_INTRA, VDX_MB_INTER};
       
  2166 
       
  2167    static const int mbTypeToDQUANTI[6] =
       
  2168       {0, 1, 0, 0, 1, 1};
       
  2169 
       
  2170    int
       
  2171       numBitsGot,
       
  2172       retValue = VDX_OK,
       
  2173       mcbpc,
       
  2174       mbType,
       
  2175       mbClass,
       
  2176       fDQUANT = 0,
       
  2177       fMVD,
       
  2178       numMVs,
       
  2179       fCBPB,
       
  2180       fMVDB,
       
  2181       cbpyIndex,
       
  2182       fINTRAMODE;
       
  2183    int16
       
  2184       bibError = 0;
       
  2185    u_int32 
       
  2186       bits;
       
  2187 
       
  2188    int StartByteIndex; 
       
  2189    int StartBitIndex;
       
  2190 
       
  2191     vdxAssert(inBuffer != NULL);
       
  2192     vdxAssert(inpParam != NULL);
       
  2193     vdxAssert(inpParam->pictureType == VDX_PIC_TYPE_P ||
       
  2194         inpParam->pictureType == VDX_PIC_TYPE_PB||
       
  2195         inpParam->pictureType == VDX_PIC_TYPE_IPB);
       
  2196     vdxAssert(outParam != NULL);
       
  2197     vdxAssert(bitErrorIndication != NULL);
       
  2198      
       
  2199     /* MVE */
       
  2200 //    int startByteIndex = inBuffer->getIndex;
       
  2201 //    int startBitIndex  = inBuffer->bitIndex;
       
  2202      
       
  2203     vdxPMBListItem_t *MBinstance;
       
  2204      // Create new MBInstance for the next MB 
       
  2205      MBinstance = (vdxPMBListItem_t *) malloc(sizeof(vdxPMBListItem_t));
       
  2206      if (!MBinstance)
       
  2207      {
       
  2208          deb("ERROR - vdxGetPMBLayer::MBinstance creation failed\n");
       
  2209          goto exitFunction;
       
  2210      }
       
  2211      memset(MBinstance, 0, sizeof(vdxPMBListItem_t));
       
  2212      VDT_SET_START_POSITION(MBinstance,11,0,7); // 11: MB stuffing bits
       
  2213      VDT_SET_END_POSITION(MBinstance,11,0,7); // 11: MB stuffing bits
       
  2214      
       
  2215    /* Set default output values */
       
  2216    memset(outParam, 0, sizeof(vdxPPBMBLayer_t));
       
  2217    outParam->quant = inpParam->quant;
       
  2218      
       
  2219    /* Loop while MCBPC indicates stuffing */
       
  2220    do {
       
  2221          
       
  2222          // note: stufffing includes COD+MCBPC in P frames
       
  2223          StartByteIndex = inBuffer->getIndex; 
       
  2224          StartBitIndex  = inBuffer->bitIndex;
       
  2225          
       
  2226          /* COD */
       
  2227          bits = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2228              &bibError);
       
  2229          
       
  2230          
       
  2231          outParam->fCodedMB = (int) bits ^ 1;
       
  2232          
       
  2233          /* If not coded (i.e. if COD == 1) */
       
  2234          if (bits)
       
  2235              goto exitFunction;
       
  2236          
       
  2237          /* Else coded (i.e. if COD == 0) */
       
  2238          else {
       
  2239             /* MCBPC */
       
  2240 
       
  2241             /* MVE */
       
  2242             VDT_SET_START_POSITION(MBinstance,0,inBuffer->getIndex,inBuffer->bitIndex); // 0: mcbpc
       
  2243 
       
  2244             retValue = vdxGetMCBPCInter(
       
  2245                  inBuffer, 
       
  2246                  inpParam->fPLUSPTYPE,
       
  2247                  inpParam->fAP || inpParam->fDF,
       
  2248                  inpParam->fFirstMBOfPicture,
       
  2249                  &mcbpc, 
       
  2250                  bitErrorIndication);
       
  2251 
       
  2252             if (retValue != VDX_OK)
       
  2253                  goto exitFunction;
       
  2254 
       
  2255             /* See section 5.3.2 of the H.263 recommendation to find out 
       
  2256             the details of this illegal codeword check. */
       
  2257             if (mcbpc == 20 &&
       
  2258                  inpParam->pictureType == VDX_PIC_TYPE_IPB &&
       
  2259                  inpParam->fCustomSourceFormat &&
       
  2260                  inpParam->fFirstMBOfPicture) 
       
  2261             {
       
  2262                  deb0p("ERROR. Illegal MCBPC stuffing.\n");
       
  2263                  retValue = VDX_OK_BUT_BIT_ERROR;
       
  2264                  goto exitFunction;
       
  2265             }
       
  2266 
       
  2267             /* MVE */
       
  2268             /* remember to output the MB stufffing bits!! */
       
  2269             if (mcbpc == 20) 
       
  2270             {
       
  2271                  // copy data from inbuffer to outbuffer 
       
  2272                  bufEdit->copyMode = CopyWhole; // copy whole
       
  2273                  CopyStream(inBuffer,outBuffer,bufEdit,StartByteIndex,StartBitIndex);
       
  2274             }
       
  2275          }
       
  2276    } while (mcbpc == 20);
       
  2277 
       
  2278    /* Decrease indexes > 20 to enable consistent MCBPC handling */
       
  2279    if (mcbpc > 20)
       
  2280          mcbpc--;
       
  2281      
       
  2282     /* MVE */
       
  2283     VDT_SET_END_POSITION(MBinstance,0,inBuffer->getIndex,inBuffer->bitIndex); // 0: mcbpc
       
  2284    
       
  2285    /* CBPC (2 LSBs of MCBPC) */
       
  2286    outParam->cbpc = mcbpc & 3;
       
  2287      
       
  2288    /* MCBPC --> MB type & included data elements */
       
  2289    mbType = outParam->mbType = mcbpc / 4;
       
  2290    mbClass = outParam->mbClass = mbTypeToMBClass[mbType];
       
  2291    fDQUANT = mbTypeToDQUANTI[mbType];
       
  2292    /* MVD is included always for PB-frames and always if MB type is INTER */
       
  2293    fMVD = inpParam->pictureType != VDX_PIC_TYPE_P || mbClass == VDX_MB_INTER;
       
  2294    numMVs = outParam->numMVs = 
       
  2295       (fMVD) ?
       
  2296          ((mbType == 2 || mbType == 5) ? 4 : 1) :
       
  2297          0;
       
  2298 
       
  2299    /* 4 MVs can be present only when in Advanced Prediction or Deblocking 
       
  2300       Filter mode */
       
  2301    if (numMVs == 4 && !inpParam->fAP && !inpParam->fDF) {
       
  2302       deb0p("ERROR. Illegal MCBPC.\n");
       
  2303       retValue = VDX_OK_BUT_BIT_ERROR;
       
  2304       goto exitFunction;
       
  2305    }
       
  2306 
       
  2307    /* INTRA_MODE */
       
  2308    if ((inpParam->fAIC)&&((mbType == 3) || (mbType == 4)))
       
  2309    {
       
  2310       retValue = vdxGetIntraMode(inBuffer,&fINTRAMODE,bitErrorIndication);
       
  2311       if (retValue != VDX_OK)
       
  2312          goto exitFunction;
       
  2313       outParam->predMode = fINTRAMODE;
       
  2314    }
       
  2315 
       
  2316    if (inpParam->pictureType == VDX_PIC_TYPE_PB) {
       
  2317       int modbIndex;
       
  2318       /* MODB */
       
  2319       retValue = vdxGetNormalMODB(inBuffer, &modbIndex, bitErrorIndication);
       
  2320       if (retValue != VDX_OK)
       
  2321          goto exitFunction;
       
  2322       fCBPB = (int) (modbIndex & 2);
       
  2323       fMVDB = outParam->fMVDB = (int) (modbIndex > 0);
       
  2324    }
       
  2325    else if (inpParam->pictureType == VDX_PIC_TYPE_IPB)
       
  2326    {
       
  2327       int modbIndex;
       
  2328       /* MODB in Improved PB mode */
       
  2329       retValue = vdxGetImpPBMODB(inBuffer,&modbIndex,bitErrorIndication);
       
  2330       fCBPB = (int) (modbIndex & 1);
       
  2331       fMVDB = outParam->fMVDB = (int) ((modbIndex & 2)>>1);
       
  2332       outParam->IPBPredMode = (int) (modbIndex / 2);
       
  2333    }
       
  2334    else {
       
  2335       fCBPB = 0;
       
  2336       fMVDB = outParam->fMVDB = 0;
       
  2337    }
       
  2338 
       
  2339    if (fCBPB) {
       
  2340       /* CBPB */
       
  2341       outParam->cbpb = (int) bibGetBits(6, inBuffer, &numBitsGot, 
       
  2342          bitErrorIndication, &bibError);
       
  2343 
       
  2344 
       
  2345    }
       
  2346    else
       
  2347       outParam->cbpb = 0;
       
  2348 
       
  2349    /* ac_pred_flag (1 bit) */
       
  2350    if (inpParam->fMPEG4) {
       
  2351       if (mbClass == VDX_MB_INTRA) {
       
  2352          outParam->ac_pred_flag = (u_char) bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2353 
       
  2354 
       
  2355       }
       
  2356    }
       
  2357 
       
  2358    /* MVE */
       
  2359    VDT_SET_START_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 2: cbpy
       
  2360 
       
  2361    /* CBPY */
       
  2362    retValue = vdxGetCBPY(inBuffer, &cbpyIndex, bitErrorIndication);
       
  2363    if (retValue != VDX_OK)
       
  2364       goto exitFunction;
       
  2365 
       
  2366    if (mbClass == VDX_MB_INTER)
       
  2367       /* Convert index to INTER CBPY */
       
  2368       outParam->cbpy = 15 - cbpyIndex;
       
  2369 
       
  2370    else
       
  2371       outParam->cbpy = cbpyIndex;
       
  2372 
       
  2373    /* MVE */
       
  2374    VDT_SET_END_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 2: cbpy
       
  2375    VDT_SET_START_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  2376 
       
  2377    if (fDQUANT) {
       
  2378       /* Get DQUANT and update QUANT */
       
  2379       retValue = vdxUpdateQuant(inBuffer, inpParam->fMQ, inpParam->quant,
       
  2380          &outParam->quant, bitErrorIndication);
       
  2381       if (retValue != VDX_OK)
       
  2382          goto exitFunction;
       
  2383    }
       
  2384 
       
  2385    /* MVE */
       
  2386    VDT_SET_END_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  2387    VDT_SET_START_POSITION(MBinstance,10,inBuffer->getIndex,inBuffer->bitIndex); // 10: mv
       
  2388 
       
  2389    /* Color Toning */
       
  2390    hTranscoder->AfterMBLayer(outParam->quant);
       
  2391      
       
  2392 
       
  2393    if (fMVD) {
       
  2394          int i;
       
  2395          
       
  2396          /* If no new Annex D is in use */
       
  2397          if (!inpParam->fPLUSPTYPE || !inpParam->fUMV) {
       
  2398              
       
  2399              if (inpParam->fMPEG4) {
       
  2400                  for (i = 0; i < numMVs; i++) {
       
  2401                      retValue = vdxGetScaledMVD(inBuffer,inpParam->f_code,&outParam->mvdx[i],bitErrorIndication);
       
  2402                      if (retValue != VDX_OK)
       
  2403                          goto exitFunction;
       
  2404                      
       
  2405                      retValue = vdxGetScaledMVD(inBuffer,inpParam->f_code,&outParam->mvdy[i],bitErrorIndication);
       
  2406                      if (retValue != VDX_OK)
       
  2407                          goto exitFunction;
       
  2408                  }
       
  2409              } else {
       
  2410                  for (i = 0; i < numMVs; i++) {
       
  2411                      retValue = vdxGetMVD(inBuffer, &outParam->mvdx[i], bitErrorIndication);
       
  2412                      if (retValue != VDX_OK)
       
  2413                          goto exitFunction;
       
  2414                      
       
  2415                      retValue = vdxGetMVD(inBuffer, &outParam->mvdy[i], bitErrorIndication);
       
  2416                      if (retValue != VDX_OK)
       
  2417                          goto exitFunction;
       
  2418                  }
       
  2419              }
       
  2420          }
       
  2421          
       
  2422          else if (inpParam->fPLUSPTYPE && inpParam->fUMV)   /* Annex D */
       
  2423          {
       
  2424              for (i = 0; i < numMVs; i++)
       
  2425              {
       
  2426                  retValue = vdxUMVGetMVD(inBuffer,&outParam->mvdx[i],bitErrorIndication);
       
  2427                  if (retValue != VDX_OK)
       
  2428                      goto exitFunction;
       
  2429                  
       
  2430                  retValue = vdxUMVGetMVD(inBuffer,&outParam->mvdy[i],bitErrorIndication);
       
  2431                  if (retValue != VDX_OK)
       
  2432                      goto exitFunction;
       
  2433                  
       
  2434                  if ((outParam->mvdx[i] == 5) && (outParam->mvdy[i] == 5))
       
  2435                      /* Read "Prevent Start Code Emulation bit" if 000000 occurs */
       
  2436                  {
       
  2437                      bits = bibGetBits(1,inBuffer,&numBitsGot,bitErrorIndication,
       
  2438                          &bibError);
       
  2439                      
       
  2440                      
       
  2441                      if (!bits) {
       
  2442                          retValue = VDX_OK_BUT_BIT_ERROR;
       
  2443                          goto exitFunction;
       
  2444                      }
       
  2445                  }
       
  2446              }
       
  2447          }
       
  2448    }
       
  2449      
       
  2450    if (fMVDB) {
       
  2451          int i;
       
  2452          if (!inpParam->fPLUSPTYPE || !inpParam->fUMV) 
       
  2453          {
       
  2454              retValue = vdxGetMVD(inBuffer,&outParam->mvdbx,bitErrorIndication);
       
  2455              if (retValue != VDX_OK)
       
  2456                  goto exitFunction;
       
  2457              
       
  2458              retValue = vdxGetMVD(inBuffer,&outParam->mvdby,bitErrorIndication);
       
  2459              if (retValue != VDX_OK)
       
  2460                  goto exitFunction;
       
  2461          }
       
  2462          else if (inpParam->fPLUSPTYPE && inpParam->fUMV)   /* Annex D */
       
  2463          {
       
  2464              for (i = 0; i < numMVs; i++)
       
  2465              {
       
  2466                  retValue = vdxUMVGetMVD(inBuffer,&outParam->mvdbx,bitErrorIndication);
       
  2467                  if (retValue != VDX_OK)
       
  2468                      goto exitFunction;
       
  2469                  
       
  2470                  retValue = vdxUMVGetMVD(inBuffer,&outParam->mvdby,bitErrorIndication);
       
  2471                  if (retValue != VDX_OK)
       
  2472                      goto exitFunction;
       
  2473                  
       
  2474                  if ((outParam->mvdbx == 5) && (outParam->mvdby == 5))
       
  2475                      /* Read "Prevent Start Code Emulation bit" if 000000 occurs */
       
  2476                  {
       
  2477                      bits = bibGetBits(1,inBuffer,&numBitsGot,bitErrorIndication,
       
  2478                          &bibError);
       
  2479                      
       
  2480                      if (!bits) {
       
  2481                          retValue = VDX_OK_BUT_BIT_ERROR;
       
  2482                          goto exitFunction;
       
  2483                      }
       
  2484                  }
       
  2485              }
       
  2486          }
       
  2487    }
       
  2488      
       
  2489      
       
  2490      
       
  2491 exitFunction:
       
  2492      
       
  2493    /* MVE */
       
  2494    /* PB frame is not allowed !!! */
       
  2495    VDT_SET_END_POSITION(MBinstance,10,inBuffer->getIndex,inBuffer->bitIndex); // 10: mv
       
  2496    outParam->mcbpc = mcbpc;   
       
  2497 
       
  2498    for (int i = 0; i < outParam->numMVs; i++)
       
  2499    {
       
  2500          MBinstance->mvx[i] = outParam->mvdx[i];
       
  2501          MBinstance->mvy[i] = outParam->mvdy[i];
       
  2502    }
       
  2503      
       
  2504    MBinstance->mcbpc        = outParam->mcbpc;
       
  2505    MBinstance->fCodedMB     = (unsigned char)outParam->fCodedMB ;           
       
  2506    MBinstance->mbType       = outParam->mbType;              
       
  2507    MBinstance->mbClass      = outParam->mbClass;             
       
  2508    MBinstance->quant        = outParam->quant;               
       
  2509    MBinstance->cbpc         = outParam->cbpc;                
       
  2510    MBinstance->cbpy         = outParam->cbpy;                
       
  2511    MBinstance->ac_pred_flag = outParam->ac_pred_flag;
       
  2512    MBinstance->numMVs       = outParam->numMVs; 
       
  2513    MBinstance->dquant       = fDQUANT ?  outParam->quant - inpParam->quant : 0;
       
  2514      
       
  2515    hTranscoder->OnePMBDataStarted(MBinstance);
       
  2516    free(MBinstance);
       
  2517      
       
  2518    return retValue;
       
  2519 }
       
  2520 
       
  2521 
       
  2522 /*
       
  2523  * Block Layer Global Functions
       
  2524  */
       
  2525 
       
  2526 /* {{-output"vdxGetIntraDCTBlock.txt"}} */
       
  2527 /*
       
  2528  * vdxGetIntraDCTBlock
       
  2529  *    
       
  2530  *
       
  2531  * Parameters:
       
  2532  *    inBuffer                   pointer to bit buffer instance
       
  2533  *    fCodedBlock                0 if COD is 1, 1 if COD is 0
       
  2534  *    block                      DCT coefficients of the block 
       
  2535  *                               in zigzag order
       
  2536  *    bitErrorIndication         non-zero if a bit error has been detected
       
  2537  *                               within the bits accessed in this function,
       
  2538  *                               see biterr.h for possible values
       
  2539  *    fMQ                        flag for Modified Quantization
       
  2540  *    qp                         quantization parameter
       
  2541  *
       
  2542  * Function:
       
  2543  *    This function gets the DCT coefficients for one INTRA block.
       
  2544  *
       
  2545  * Returns:
       
  2546  *    VDX_OK                     the function was successful
       
  2547  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  2548  *                               occured
       
  2549  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  2550  *
       
  2551  *    
       
  2552  */
       
  2553 
       
  2554 int vdxGetIntraDCTBlock(
       
  2555    bibBuffer_t *inBuffer, 
       
  2556    int fCodedBlock,
       
  2557    int *block,
       
  2558    int *bitErrorIndication,
       
  2559    int fMQ,
       
  2560    int qp)
       
  2561 /* {{-output"vdxGetIntraDCTBlock.txt"}} */
       
  2562 {
       
  2563    int
       
  2564       numBitsGot,
       
  2565       retValue = VDX_OK;
       
  2566    int16
       
  2567       bibError = 0;
       
  2568    u_int32 
       
  2569       bits;
       
  2570 
       
  2571    vdxAssert(inBuffer != NULL);
       
  2572    vdxAssert(block != NULL);
       
  2573    vdxAssert(bitErrorIndication != NULL);
       
  2574 
       
  2575    /* MVE */
       
  2576    int fEscapeCodeUsed = 0;
       
  2577 
       
  2578    /* INTRADC */
       
  2579    bits = bibGetBits(8, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2580 
       
  2581    block[0] = (int) (bits << 3);
       
  2582 
       
  2583    /* If (FLC == 1111 1111) */
       
  2584    if (block[0] == 2040)
       
  2585       block[0] = 1024;
       
  2586 
       
  2587    
       
  2588    if (fCodedBlock)
       
  2589       retValue = vdxGetDCTBlock(inBuffer, 1, 0, block, bitErrorIndication, fMQ,qp, &fEscapeCodeUsed);
       
  2590 
       
  2591    else
       
  2592       memset(block + 1, 0, 63 * sizeof(int));
       
  2593 
       
  2594    return retValue;
       
  2595 
       
  2596 
       
  2597 }
       
  2598 
       
  2599 
       
  2600 /* {{-output"vdxGetDCTBlock.txt"}} */
       
  2601 /*
       
  2602  * vdxGetDCTBlock
       
  2603  *    
       
  2604  *
       
  2605  * Parameters:
       
  2606  *    inBuffer                   pointer to bit buffer instance
       
  2607  *    startIndex                 the first index in block where to put data
       
  2608  *    fMPEG4EscapeCodes          use MPEG-4 escape codes (3 alternatives)
       
  2609  *    block                      array for block (length 64)
       
  2610  *    bitErrorIndication         non-zero if a bit error has been detected
       
  2611  *                               within the bits accessed in this function,
       
  2612  *                               see biterr.h for possible values
       
  2613  *    fMQ
       
  2614  *    qp
       
  2615  *    fEscapeCodeUsed            
       
  2616  * Function:
       
  2617  *    This function reads a block from bit buffer using Huffman codes listed
       
  2618  *    in TCOEF table. The place, where the block is read is given as a
       
  2619  *    pointer parameter.
       
  2620  *
       
  2621  * Returns:
       
  2622  *    VDX_OK                     the function was successful
       
  2623  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  2624  *                               occured
       
  2625  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  2626  *
       
  2627  *    
       
  2628  *
       
  2629  */
       
  2630 
       
  2631 int vdxGetDCTBlock(
       
  2632    bibBuffer_t *inBuffer, 
       
  2633    int startIndex,
       
  2634    u_char fMPEG4EscapeCodes,
       
  2635    int *block,
       
  2636    int *bitErrorIndication,
       
  2637    int fMQ,
       
  2638    int qp,
       
  2639    int *fEscapeCodeUsed)
       
  2640 /* {{-output"vdxGetDCTBlock.txt"}} */
       
  2641 {
       
  2642    /* Lookup tables: val field contains
       
  2643       1 bit for LAST, 8 bits for RUN and 4 bits for LEVEL */
       
  2644    static const vdxVLCTable_t tcoefTab0[] = {
       
  2645       {4225,7}, {4209,7}, {4193,7}, {4177,7}, {193,7}, {177,7},
       
  2646       {161,7}, {4,7}, {4161,6}, {4161,6}, {4145,6}, {4145,6},
       
  2647       {4129,6}, {4129,6}, {4113,6}, {4113,6}, {145,6}, {145,6},
       
  2648       {129,6}, {129,6}, {113,6}, {113,6}, {97,6}, {97,6},
       
  2649       {18,6}, {18,6}, {3,6}, {3,6}, {81,5}, {81,5},
       
  2650       {81,5}, {81,5}, {65,5}, {65,5}, {65,5}, {65,5},
       
  2651       {49,5}, {49,5}, {49,5}, {49,5}, {4097,4}, {4097,4},
       
  2652       {4097,4}, {4097,4}, {4097,4}, {4097,4}, {4097,4}, {4097,4},
       
  2653       {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
       
  2654       {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
       
  2655       {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
       
  2656       {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
       
  2657       {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
       
  2658       {1,2}, {1,2}, {17,3}, {17,3}, {17,3}, {17,3},
       
  2659       {17,3}, {17,3}, {17,3}, {17,3}, {17,3}, {17,3},
       
  2660       {17,3}, {17,3}, {17,3}, {17,3}, {17,3}, {17,3},
       
  2661       {33,4}, {33,4}, {33,4}, {33,4}, {33,4}, {33,4},
       
  2662       {33,4}, {33,4}, {2,4}, {2,4}, {2,4}, {2,4},
       
  2663       {2,4}, {2,4}, {2,4}, {2,4}
       
  2664    };
       
  2665 
       
  2666    static const vdxVLCTable_t tcoefTab1[] = {
       
  2667       {9,10}, {8,10}, {4481,9}, {4481,9}, {4465,9}, {4465,9},
       
  2668       {4449,9}, {4449,9}, {4433,9}, {4433,9}, {4417,9}, {4417,9},
       
  2669       {4401,9}, {4401,9}, {4385,9}, {4385,9}, {4369,9}, {4369,9},
       
  2670       {4098,9}, {4098,9}, {353,9}, {353,9}, {337,9}, {337,9},
       
  2671       {321,9}, {321,9}, {305,9}, {305,9}, {289,9}, {289,9},
       
  2672       {273,9}, {273,9}, {257,9}, {257,9}, {241,9}, {241,9},
       
  2673       {66,9}, {66,9}, {50,9}, {50,9}, {7,9}, {7,9},
       
  2674       {6,9}, {6,9}, {4353,8}, {4353,8}, {4353,8}, {4353,8},
       
  2675       {4337,8}, {4337,8}, {4337,8}, {4337,8}, {4321,8}, {4321,8},
       
  2676       {4321,8}, {4321,8}, {4305,8}, {4305,8}, {4305,8}, {4305,8},
       
  2677       {4289,8}, {4289,8}, {4289,8}, {4289,8}, {4273,8}, {4273,8},
       
  2678       {4273,8}, {4273,8}, {4257,8}, {4257,8}, {4257,8}, {4257,8},
       
  2679       {4241,8}, {4241,8}, {4241,8}, {4241,8}, {225,8}, {225,8},
       
  2680       {225,8}, {225,8}, {209,8}, {209,8}, {209,8}, {209,8},
       
  2681       {34,8}, {34,8}, {34,8}, {34,8}, {19,8}, {19,8},
       
  2682       {19,8}, {19,8}, {5,8}, {5,8}, {5,8}, {5,8}
       
  2683    };
       
  2684 
       
  2685    static const vdxVLCTable_t tcoefTab2[] = {
       
  2686       {4114,11}, {4114,11}, {4099,11}, {4099,11}, {11,11}, {11,11},
       
  2687       {10,11}, {10,11}, {4545,10}, {4545,10}, {4545,10}, {4545,10},
       
  2688       {4529,10}, {4529,10}, {4529,10}, {4529,10}, {4513,10}, {4513,10},
       
  2689       {4513,10}, {4513,10}, {4497,10}, {4497,10}, {4497,10}, {4497,10},
       
  2690       {146,10}, {146,10}, {146,10}, {146,10}, {130,10}, {130,10},
       
  2691       {130,10}, {130,10}, {114,10}, {114,10}, {114,10}, {114,10},
       
  2692       {98,10}, {98,10}, {98,10}, {98,10}, {82,10}, {82,10},
       
  2693       {82,10}, {82,10}, {51,10}, {51,10}, {51,10}, {51,10},
       
  2694       {35,10}, {35,10}, {35,10}, {35,10}, {20,10}, {20,10},
       
  2695       {20,10}, {20,10}, {12,11}, {12,11}, {21,11}, {21,11},
       
  2696       {369,11}, {369,11}, {385,11}, {385,11}, {4561,11}, {4561,11},
       
  2697       {4577,11}, {4577,11}, {4593,11}, {4593,11}, {4609,11}, {4609,11},
       
  2698       {22,12}, {36,12}, {67,12}, {83,12}, {99,12}, {162,12},
       
  2699       {401,12}, {417,12}, {4625,12}, {4641,12}, {4657,12}, {4673,12},
       
  2700       {4689,12}, {4705,12}, {4721,12}, {4737,12}, {7167,7},
       
  2701       {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
       
  2702       {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
       
  2703       {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
       
  2704       {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
       
  2705       {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
       
  2706       {7167,7}
       
  2707    };
       
  2708 
       
  2709    static const int inter_max_level[2][64] = {
       
  2710                                      {12,  6,  4,  3,  3,  3,  3,  2,
       
  2711                                        2,  2,  2,  1,  1,  1,  1,  1,
       
  2712                                        1,  1,  1,  1,  1,  1,  1,  1,
       
  2713                                        1,  1,  1,  0,  0,  0,  0,  0,
       
  2714                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2715                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2716                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2717                                        0,  0,  0,  0,  0,  0,  0,  0},
       
  2718  
       
  2719                                       {3,  2,  1,  1,  1,  1,  1,  1,
       
  2720                                        1,  1,  1,  1,  1,  1,  1,  1,
       
  2721                                        1,  1,  1,  1,  1,  1,  1,  1,
       
  2722                                        1,  1,  1,  1,  1,  1,  1,  1,
       
  2723                                        1,  1,  1,  1,  1,  1,  1,  1,
       
  2724                                        1,  0,  0,  0,  0,  0,  0,  0,
       
  2725                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2726                                        0,  0,  0,  0,  0,  0,  0,  0}
       
  2727                                     };
       
  2728 
       
  2729    static const int inter_max_run0[13] = { 999,
       
  2730                                    26, 10,  6,  2,  1,  1,
       
  2731                                     0,  0,  0,  0,  0,  0
       
  2732                                 };
       
  2733   
       
  2734    static const int inter_max_run1[4] = { 999, 40,  1,  0 };
       
  2735 
       
  2736    int
       
  2737       numBitsGot, /* number of bits got from bit buffer */
       
  2738       retValue = VDX_OK,
       
  2739       code,       /* bits got from bit buffer */
       
  2740       index,      /* index to zigzag table running from 1 to 63 */
       
  2741       run,        /* RUN code */
       
  2742       level;      /* LEVEL code */
       
  2743 
       
  2744    int16
       
  2745       bibError = 0;
       
  2746 
       
  2747    u_int32 
       
  2748       last,       /* LAST code (see standard) */
       
  2749       sign,       /* sign for level */
       
  2750       tmp;        /* temporary variable for reading some redundant bits */
       
  2751 
       
  2752    vdxVLCTable_t const *tab; /* pointer to lookup table */
       
  2753 
       
  2754    vdxAssert(inBuffer != NULL);
       
  2755    vdxAssert(startIndex == 0 || startIndex == 1);
       
  2756    vdxAssert(block != NULL);
       
  2757    vdxAssert(bitErrorIndication != NULL);
       
  2758 
       
  2759    /* Reset all coefficients to zero in order to avoid writing
       
  2760       zeros one by one during run-length decoding */
       
  2761    memset(&block[startIndex], 0, (64 - startIndex) * sizeof(int));
       
  2762 
       
  2763    index = startIndex;
       
  2764 
       
  2765    /* MVE */
       
  2766    *fEscapeCodeUsed = 0;
       
  2767 
       
  2768    do {
       
  2769       /* Read next codeword */
       
  2770       code = (int) bibShowBits(12, inBuffer, &numBitsGot, bitErrorIndication,
       
  2771          &bibError);
       
  2772 
       
  2773 
       
  2774       /* Select the right table and index for the codeword */
       
  2775       if (code >= 512)
       
  2776          tab = &tcoefTab0[(code >> 5) - 16];
       
  2777       else if (code >= 128)
       
  2778          tab = &tcoefTab1[(code >> 2) - 32];
       
  2779       else if (code >= 8)
       
  2780          tab = &tcoefTab2[code - 8];
       
  2781       else {
       
  2782          deb("ERROR - illegal TCOEF\n");
       
  2783          retValue = VDX_OK_BUT_BIT_ERROR;
       
  2784          goto exitFunction;
       
  2785       }
       
  2786 
       
  2787       /* Flush the codeword from the buffer */
       
  2788       bibFlushBits(tab->len, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2789 
       
  2790 
       
  2791       if (tab->val == 7167)     /* ESCAPE */
       
  2792       {
       
  2793 
       
  2794           
       
  2795         /* the following is modified for 3-mode escape */
       
  2796         if(fMPEG4EscapeCodes) {
       
  2797 
       
  2798           int run_offset=0,
       
  2799               level_offset=0;
       
  2800 
       
  2801           code = (int) bibShowBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2802 
       
  2803 
       
  2804           if (code<=2) {
       
  2805 
       
  2806               /* escape modes: level or run is offset */
       
  2807               if (code==2) run_offset=1;
       
  2808               else level_offset=1;
       
  2809 
       
  2810               /* Flush the escape code from the buffer */
       
  2811               if (run_offset)
       
  2812                   bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2813               else
       
  2814                   bibFlushBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2815 
       
  2816 
       
  2817               /* Read next codeword */
       
  2818               code = (int) bibShowBits(12, inBuffer, &numBitsGot, bitErrorIndication,
       
  2819                   &bibError);
       
  2820 
       
  2821 
       
  2822 
       
  2823               /* Select the right table and index for the codeword */
       
  2824               if (code >= 512)
       
  2825                   tab = &tcoefTab0[(code >> 5) - 16];
       
  2826               else if (code >= 128)
       
  2827                   tab = &tcoefTab1[(code >> 2) - 32];
       
  2828               else if (code >= 8)
       
  2829                   tab = &tcoefTab2[code - 8];
       
  2830               else {
       
  2831                   deb("ERROR - illegal TCOEF\n");
       
  2832                   retValue = VDX_OK_BUT_BIT_ERROR;
       
  2833                   goto exitFunction;
       
  2834               }
       
  2835 
       
  2836               bibFlushBits(tab->len, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2837 
       
  2838 
       
  2839               run = (tab->val >> 4) & 255;
       
  2840               level = tab->val & 15;
       
  2841               last = (tab->val >> 12) & 1;
       
  2842 
       
  2843               /* need to add back the max level */
       
  2844               if (level_offset)
       
  2845                   level = level + inter_max_level[last][run];
       
  2846               else if (last)
       
  2847                   run = run + inter_max_run1[level]+1;
       
  2848               else
       
  2849                   run = run + inter_max_run0[level]+1;
       
  2850 
       
  2851               sign = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,
       
  2852                   &bibError);
       
  2853 
       
  2854 
       
  2855               
       
  2856               if (sign)
       
  2857                   level = -level;
       
  2858 
       
  2859           } else {
       
  2860               
       
  2861               /* Flush the codeword from the buffer */
       
  2862               bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2863 
       
  2864 
       
  2865               /* LAST */
       
  2866               last = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2867                   &bibError);
       
  2868 
       
  2869 
       
  2870 
       
  2871               /* RUN */
       
  2872               run = (int) bibGetBits(6, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2873                   &bibError);
       
  2874 
       
  2875 
       
  2876               /* MARKER BIT */
       
  2877               tmp = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError);
       
  2878 
       
  2879 
       
  2880               if(!tmp) {
       
  2881                   retValue = VDX_OK_BUT_BIT_ERROR;
       
  2882                   goto exitFunction;
       
  2883               }
       
  2884               /* LEVEL */
       
  2885               level = (int) bibGetBits(12, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2886                   &bibError);
       
  2887 
       
  2888 
       
  2889               /* MARKER BIT */
       
  2890               tmp = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError);
       
  2891 
       
  2892 
       
  2893               if ( !tmp ) {
       
  2894                   retValue = VDX_OK_BUT_BIT_ERROR;
       
  2895                   goto exitFunction;
       
  2896               }
       
  2897 
       
  2898               /* 0000 0000 0000 and 1000 0000 0000 is forbidden unless in MQ mode */
       
  2899               if (level == 0 || level == 2048) {
       
  2900                   deb("ERROR - illegal level.\n");
       
  2901                   retValue = VDX_OK_BUT_BIT_ERROR;
       
  2902                   goto exitFunction;
       
  2903               }
       
  2904 
       
  2905               /* Codes 1000 0000 0001 .. 1111 1111 1111 */
       
  2906               if (level > 2048)
       
  2907                   level -= 4096;
       
  2908               
       
  2909           } /* flc */
       
  2910 
       
  2911         } else { /* !fMPEG4EscapeCodes */
       
  2912 
       
  2913             /* MVE */
       
  2914             *fEscapeCodeUsed = 1;
       
  2915             
       
  2916             /* LAST */
       
  2917             last = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2918                 &bibError);
       
  2919 
       
  2920 
       
  2921             /* RUN */
       
  2922             run = (int) bibGetBits(6, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2923                 &bibError);
       
  2924 
       
  2925 
       
  2926             /* LEVEL */
       
  2927             level = (int) bibGetBits(8, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2928                 &bibError);
       
  2929             
       
  2930 
       
  2931             /* Codes 1000 0001 .. 1111 1111 */
       
  2932             if (level > 128)
       
  2933                 level -= 256;
       
  2934 
       
  2935 
       
  2936             /* 0000 0000 is forbidden, 1000 0000 is forbidden unless in MQ mode */
       
  2937             if (level == 0) {
       
  2938                 deb("ERROR - illegal level.\n");
       
  2939                 retValue = VDX_OK_BUT_BIT_ERROR;
       
  2940                 goto exitFunction;
       
  2941             }
       
  2942             if (level == 128)
       
  2943             {
       
  2944                if (fMQ)
       
  2945                {
       
  2946                   level = (int) bibGetBits(11,inBuffer,&numBitsGot,
       
  2947                      bitErrorIndication,&bibError);
       
  2948 
       
  2949 
       
  2950                   level = ((level&0x003F) <<5) | (level >> 6);
       
  2951                   if (level>1023) level -= 2048;
       
  2952 
       
  2953                   /* See section T.5 of the H.263 recommendation to understand
       
  2954                      this restriction. */
       
  2955                     if (((level > -127) && (level < 127)) || (qp >= 8))
       
  2956                     {
       
  2957                         deb("ERROR - illegal extended level.\n");
       
  2958                         retValue = VDX_OK_BUT_BIT_ERROR;
       
  2959                         goto exitFunction;
       
  2960                     }
       
  2961                 }
       
  2962                 else
       
  2963                 {
       
  2964                     deb("ERROR - illegal level.\n");
       
  2965                     retValue = VDX_OK_BUT_BIT_ERROR;
       
  2966                     goto exitFunction;
       
  2967                 }
       
  2968             }
       
  2969         } /* End fMPEG4EscapeCodes switch */        
       
  2970       }
       
  2971 
       
  2972       else {
       
  2973          run = (tab->val >> 4) & 255;
       
  2974          level = tab->val & 15;
       
  2975          last = (tab->val >> 12) & 1;
       
  2976 
       
  2977          sign = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,
       
  2978             &bibError);
       
  2979 
       
  2980 
       
  2981          if (sign)
       
  2982             level = -level;
       
  2983       }
       
  2984 
       
  2985       /* If too many coefficients */
       
  2986       if (index + run > 63) {
       
  2987          deb("ERROR - too many TCOEFs.\n");
       
  2988          retValue = VDX_OK_BUT_BIT_ERROR;
       
  2989          goto exitFunction;
       
  2990       }
       
  2991 
       
  2992       /* Do run-length decoding */
       
  2993 
       
  2994       /* Note: No need to set coeffs to zero since they are already reset
       
  2995          in the beginning of the function */
       
  2996       index += run;
       
  2997 
       
  2998          block[index++] = level;
       
  2999 
       
  3000    } while (!last);
       
  3001 
       
  3002    exitFunction:
       
  3003 
       
  3004    /* Note: No need to set the rest of the coefficients to zero since
       
  3005       they are already reset in the beginning of the function */
       
  3006 
       
  3007    if (!bibError)
       
  3008       return retValue;
       
  3009 
       
  3010    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  3011       return VDX_OK_BUT_BIT_ERROR;
       
  3012    }
       
  3013    else if ( *bitErrorIndication ) {
       
  3014       return VDX_OK_BUT_BIT_ERROR;
       
  3015    }
       
  3016    else
       
  3017       return VDX_ERR_BIB;
       
  3018 }
       
  3019 
       
  3020 
       
  3021 
       
  3022 /*
       
  3023  * Picture Layer Local Functions
       
  3024  */
       
  3025 
       
  3026 /*
       
  3027  * vdxActAfterIncorrectSEI
       
  3028  *
       
  3029  * Parameters:
       
  3030  *    inBuffer                   B: Bit Buffer instance
       
  3031  *    fPLUSPTYPE                 I: signals the existence of PLUSPTYPE
       
  3032  *    fLast                      B: set to 1 if SEI flushed, 
       
  3033  *                                  otherwise not changed
       
  3034  *    bitErrorIndication         B: bit error indication, see biterr.h for
       
  3035  *                                  the possible values
       
  3036  *
       
  3037  * Function:
       
  3038  *    Problem: H.263 v1 did not specify FTYPE/DSIZE values, but rather
       
  3039  *    any data can be transfered in PSUPP. Section 5.1.3 of 
       
  3040  *    H.263 Recommendation specifies that Annex L and Annex W supplemental
       
  3041  *    enhancement functions can be used also if PLUSPTYPE is not in use.
       
  3042  *    Consequently, if PLUSPTYPE is not in use and if the bit-stream
       
  3043  *    violates FTYPE/DSIZE (or any other) rules defined in Annex L or 
       
  3044  *    Annex W, we do not know if the far-end encoder is transmitting
       
  3045  *    proprietary PSUPP values (allowed in H.263 v1) or if a bit error 
       
  3046  *    has hit the SEI data that actually follows Annex L/W. 
       
  3047  *
       
  3048  *    This function is called after an illegal Annex L/W parameter has
       
  3049  *    been detected. The function acts as follows:
       
  3050  *    - If PLUSPTYPE is in use, there must be
       
  3051  *      a bit error, and the function signals the error.
       
  3052  *    - If PLUSPTYPE is not in use, there is
       
  3053  *      a bit error or the far-end encoder uses proprietary PSUPP values.
       
  3054  *      The function flushes all PSUPP fields and returns without error
       
  3055  *      indication.
       
  3056  *      NOTE: This scheme could be improved if the decoder knows that
       
  3057  *      the far-end encoder is always following Annex L/W.
       
  3058  *
       
  3059  * Returns:
       
  3060  *    VDX_OK
       
  3061  *    VDX_OK_BUT_BIT_ERROR
       
  3062  *    VDX_ERR
       
  3063  */
       
  3064 
       
  3065 static int vdxActAfterIncorrectSEI(
       
  3066    bibBuffer_t *inBuffer,
       
  3067    int fPLUSPTYPE,
       
  3068    int *fLast,
       
  3069    int *bitErrorIndication)
       
  3070 {
       
  3071    /* If Annex L/W is in use for sure */
       
  3072    if (fPLUSPTYPE) {
       
  3073 
       
  3074       /* Return indication of bit error */
       
  3075       return VDX_OK_BUT_BIT_ERROR;
       
  3076    }
       
  3077 
       
  3078    /* Else Annex L/W is not necessarily on */
       
  3079    else {
       
  3080       
       
  3081       /* We are out of sync due to bit error or 
       
  3082          do not understand the non-standard PEI/PSUPP syntax
       
  3083          --> flush the rest of PEI/PSUPP pairs */
       
  3084 
       
  3085       *fLast = 1;
       
  3086 
       
  3087       return vdxFlushSEI(inBuffer, bitErrorIndication);
       
  3088    }
       
  3089 }
       
  3090 
       
  3091 
       
  3092 /*
       
  3093  * vdxStandardSourceFormatToFrameSize
       
  3094  *    
       
  3095  *
       
  3096  * Parameters:
       
  3097  *    sourceFormat               the three source format bits
       
  3098  *    width                      luminance image width
       
  3099  *    height                     luminance image height
       
  3100  *
       
  3101  * Function:
       
  3102  *    This function converts the source format bits to luminance image size.
       
  3103  *
       
  3104  * Returns:
       
  3105  *    Nothing.
       
  3106  *
       
  3107  *    
       
  3108  *
       
  3109  */
       
  3110 
       
  3111 static void vdxStandardSourceFormatToFrameSize(int sourceFormat, int *width, int *height)
       
  3112 {
       
  3113    vdxAssert(sourceFormat >= 1 && sourceFormat <= 5);
       
  3114 
       
  3115    switch (sourceFormat) {
       
  3116       case 1:  /* sub-QCIF */
       
  3117          *width = 128;
       
  3118          *height = 96;
       
  3119          break;
       
  3120       case 2:  /* QCIF */
       
  3121          *width = 176;
       
  3122          *height = 144;
       
  3123          break;
       
  3124       case 3:  /* CIF */
       
  3125          *width = 352;
       
  3126          *height = 288;
       
  3127          break;
       
  3128       case 4:  /* 4CIF */
       
  3129          *width = 704;
       
  3130          *height = 576;
       
  3131          break;
       
  3132       case 5:  /* 16 CIF */
       
  3133          *width = 1408;
       
  3134          *height = 1152;
       
  3135          break;
       
  3136    }
       
  3137 }
       
  3138 
       
  3139 
       
  3140 /*
       
  3141  * Slice Layer Local Functions
       
  3142  */
       
  3143 
       
  3144 /*
       
  3145  * vdxFrameSizeToPictureFormat
       
  3146  *    
       
  3147  *
       
  3148  * Parameters:
       
  3149  *    width                      luminance image width
       
  3150  *    height                     luminance image height
       
  3151  *
       
  3152  * Function:
       
  3153  *    This function converts luminance image size to picture format.
       
  3154  *
       
  3155  * Returns:
       
  3156  *    Source format
       
  3157  *
       
  3158  *
       
  3159  */
       
  3160 
       
  3161 int vdxFrameSizeToPictureFormat(int width, int /*height*/)
       
  3162 {
       
  3163    if (width <= 128) 
       
  3164       return 0;   /* sub-QCIF */
       
  3165    else if (width <= 176)
       
  3166       return 1;   /* QCIF */
       
  3167    else if (width <= 352)
       
  3168       return 2;   /* CIF */
       
  3169    else if (width <= 704)
       
  3170       return 3;   /* 4CIF */
       
  3171    else if (width <= 1408)
       
  3172       return 4;   /* 16CIF */
       
  3173    else if (width <= 2048)
       
  3174       return 5;   /* 2048x1152 */
       
  3175    else
       
  3176       return -1;  /* Should never happen */
       
  3177 }
       
  3178 
       
  3179 /*
       
  3180  * Macroblock Layer Local Functions
       
  3181  */
       
  3182 
       
  3183 /*
       
  3184  * vdxGetCBPY
       
  3185  *    
       
  3186  *
       
  3187  * Parameters:
       
  3188  *    inBuffer                   pointer to bit buffer instance
       
  3189  *    index                      index to the CBPY table of H.263
       
  3190  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3191  *                               within the bits accessed in this function,
       
  3192  *                               see biterr.h for possible values
       
  3193  *
       
  3194  * Function:
       
  3195  *    This function reads the CBPY code.
       
  3196  *
       
  3197  * Returns:
       
  3198  *    VDX_OK                     the function was successful
       
  3199  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  3200  *                               occured
       
  3201  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3202  *
       
  3203  *    
       
  3204  */
       
  3205 
       
  3206 int vdxGetCBPY(bibBuffer_t *inBuffer, int *index, 
       
  3207    int *bitErrorIndication)
       
  3208 {
       
  3209    static const vdxVLCTable_t tabCBPY[48] = {
       
  3210       {-1,0}, {-1,0}, {6,6}, {9,6}, {8,5}, {8,5}, {4,5}, {4,5},
       
  3211       {2,5}, {2,5}, {1,5}, {1,5}, {0,4}, {0,4}, {0,4}, {0,4},
       
  3212       {12,4}, {12,4}, {12,4}, {12,4}, {10,4},{10,4},{10,4},{10,4},
       
  3213       {14,4}, {14,4}, {14,4}, {14,4}, {5,4}, {5,4}, {5,4}, {5,4},
       
  3214       {13,4}, {13,4}, {13,4}, {13,4}, {3,4}, {3,4}, {3,4}, {3,4},
       
  3215       {11,4}, {11,4}, {11,4}, {11,4}, {7,4}, {7,4}, {7,4}, {7,4}
       
  3216    };
       
  3217 
       
  3218    int bitsGot, code;
       
  3219    int16 ownError = 0;
       
  3220 
       
  3221    vdxAssert(inBuffer != NULL);
       
  3222    vdxAssert(index != NULL);
       
  3223    vdxAssert(bitErrorIndication != NULL);
       
  3224 
       
  3225    code = (int) bibShowBits(6, inBuffer, &bitsGot, bitErrorIndication, 
       
  3226       &ownError);
       
  3227 
       
  3228 
       
  3229    if (code >= 48) {
       
  3230       /* bit pattern = 11xxxx */
       
  3231       bibFlushBits(2, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3232       *index = 15;
       
  3233       return VDX_OK;
       
  3234    }
       
  3235 
       
  3236    if (code < 2) {
       
  3237       deb("vlcGetCBPY: ERROR - illegal code.\n");
       
  3238       return VDX_OK_BUT_BIT_ERROR;
       
  3239    }
       
  3240 
       
  3241    bibFlushBits(tabCBPY[code].len, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3242 
       
  3243    *index = tabCBPY[code].val;
       
  3244 
       
  3245    if (ownError)
       
  3246       return VDX_ERR_BIB;
       
  3247 
       
  3248    return VDX_OK;
       
  3249 }
       
  3250 
       
  3251 
       
  3252 /*
       
  3253  * vdxGetMCBPCInter
       
  3254  *    
       
  3255  *
       
  3256  * Parameters:
       
  3257  *    inBuffer                   pointer to bit buffer instance
       
  3258  *    fPLUSPTYPE                 flag to indicate if PLUSPTYPE is present
       
  3259  *    fFourMVsPossible           flag to indicate if four motion vectors per
       
  3260  *                               macroblock is allowed
       
  3261  *    fFirstMBOfPicture          flag to indicate if the current macroblock
       
  3262  *                               is the first one of the picture in scan-order
       
  3263  *    index                      index to the MCBPC table for P-pictures
       
  3264  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3265  *                               within the bits accessed in this function,
       
  3266  *                               see biterr.h for possible values
       
  3267  *
       
  3268  * Function:
       
  3269  *    This function reads the MCBPC code for P-pictures.
       
  3270  *
       
  3271  * Returns:
       
  3272  *    VDX_OK                     the function was successful
       
  3273  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  3274  *                               occured
       
  3275  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3276  *
       
  3277  *    
       
  3278  *    
       
  3279  */
       
  3280 
       
  3281 int vdxGetMCBPCInter(
       
  3282    bibBuffer_t *inBuffer, 
       
  3283    int fPLUSPTYPE,
       
  3284    int fFourMVsPossible,
       
  3285    int fFirstMBOfPicture,
       
  3286    int *index, 
       
  3287    int *bitErrorIndication)
       
  3288 {
       
  3289    static const vdxVLCTable_t tabMCBPCInter[256] = {
       
  3290       {-1,0}, /* 0 = illegal */
       
  3291       {20,9}, /* 1 = 0000 0000 1 = stuffing */
       
  3292       {19,9}, /* 2 = 0000 0001 0 */
       
  3293       {18,9}, /* 3 = 0000 0001 1 */
       
  3294       {17,9}, /* 4 = 0000 0010 0 */
       
  3295       {7,9},  /* 5 = 0000 0010 1 */
       
  3296       {14,8}, {14,8}, /* 6..7 = 0000 0011 x */
       
  3297       {13,8}, {13,8}, /* 8..9 = 0000 0100 x */
       
  3298       {11,8}, {11,8}, /* 10..11 = 0000 0101 x */
       
  3299       {15,7}, {15,7}, {15,7}, {15,7}, /* 12..15 = 0000 011x x */
       
  3300       {10,7}, {10,7}, {10,7}, {10,7}, /* 16..19 = 0000 100x x */
       
  3301       { 9,7}, { 9,7}, { 9,7}, { 9,7}, /* 20..23 = 0000 101x x */
       
  3302       { 6,7}, { 6,7}, { 6,7}, { 6,7}, /* 24..27 = 0000 110x x */
       
  3303       { 5,7}, { 5,7}, { 5,7}, { 5,7}, /* 28..31 = 0000 111x x */
       
  3304       {16,6}, {16,6}, {16,6}, {16,6},
       
  3305       {16,6}, {16,6}, {16,6}, {16,6}, /* 32..39 = 0001 00xx x */
       
  3306       { 3,6}, { 3,6}, { 3,6}, { 3,6},
       
  3307       { 3,6}, { 3,6}, { 3,6}, { 3,6}, /* 40..47 = 0001 01xx x */
       
  3308       {12,5}, {12,5}, {12,5}, {12,5}, {12,5}, {12,5},
       
  3309       {12,5}, {12,5}, {12,5}, {12,5}, {12,5}, {12,5},
       
  3310       {12,5}, {12,5}, {12,5}, {12,5}, /* 48..63 = 0001 1xxx x */
       
  3311       { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4},
       
  3312       { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4},
       
  3313       { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4},
       
  3314       { 2,4}, { 2,4}, { 2,4}, { 2,4}, { 2,4}, /* 64..95 = 0010 xxxx x */
       
  3315       { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4},
       
  3316       { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4},
       
  3317       { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4},
       
  3318       { 1,4}, { 1,4}, { 1,4}, { 1,4}, { 1,4}, /* 96..127 = 0011 xxxx x */
       
  3319       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3320       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3321       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3322       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3323       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3324       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3325       {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3}, {8,3},
       
  3326       {8,3}, {8,3}, /* 128..191 = 010x xxxx x */
       
  3327       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3328       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3329       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3330       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3331       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3332       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3333       {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3}, {4,3},
       
  3334       {4,3}, {4,3}, {4,3} /* 192..255 = 011x xxxx x */
       
  3335    };
       
  3336 
       
  3337    /* Indices 21 - 24 of the MCBPC table for P-pictures,
       
  3338       4 least significant bits from the maximum of 13 are used for indexing */
       
  3339    static const vdxVLCTable_t tabMCBPCInterIndices21To24[16] = {
       
  3340       {-1,0}, {-1,0}, {-1,0}, {-1,0}, 
       
  3341       {-1,0}, {-1,0}, {-1,0}, {-1,0},        /* 0000 0000 00xx x, illegal */
       
  3342       {21,11}, {21,11}, {21,11}, {21,11},    /* 0000 0000 010x x */
       
  3343       {22,13},                               /* 0000 0000 0110 0 */
       
  3344       {-1,0},                                /* 0000 0000 0110 1, illegal */
       
  3345       {23,13},                               /* 0000 0000 0111 0 */
       
  3346       {24,13}                                /* 0000 0000 0111 1 */
       
  3347    };
       
  3348 
       
  3349    int bitsGot, code;
       
  3350    int16 ownError = 0;
       
  3351 
       
  3352    vdxAssert(inBuffer != NULL);
       
  3353    vdxAssert(index != NULL);
       
  3354    vdxAssert(bitErrorIndication != NULL);
       
  3355 
       
  3356    code = (int) bibShowBits(9, inBuffer, &bitsGot, bitErrorIndication, 
       
  3357       &ownError);
       
  3358 
       
  3359 
       
  3360    /* If index == 0 */
       
  3361    if (code >= 256) {
       
  3362       /* bit pattern = 1xxx xxxx x */
       
  3363       bibFlushBits(1, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3364       *index = 0;
       
  3365       return VDX_OK;
       
  3366    }
       
  3367 
       
  3368    /* If illegal code or indices 21 - 24 */
       
  3369    if (code == 0) {
       
  3370       /* 0000 0000 0 (indices 21 - 24 in MCBPC table for P-pictures)
       
  3371          can only be present if the conditions listed in  section 5.3.2 of 
       
  3372          the H.263 recommendation are fulfilled. Otherwise, the bit pattern
       
  3373          is considered an illegal codeword. */
       
  3374 
       
  3375       /* If indices 21 - 24 */
       
  3376       if (fPLUSPTYPE && fFourMVsPossible && !fFirstMBOfPicture) {
       
  3377 
       
  3378          /* Note: The following restriction is given in section 5.3.2 of 
       
  3379             the H.263 recommendation: "Also, encoders shall not allow an MCBPC
       
  3380             code for macroblock type 5 to immediately follow seven consecutive 
       
  3381             zeros in the bitstream (as can be caused by particular INTRADC codes
       
  3382             followed by COD=0), in order to prevent start code emulation."
       
  3383             This condition is not checked in the decoder, since it would require
       
  3384             relatively complex code structure and it is considered relatively
       
  3385             improbable. */
       
  3386 
       
  3387          /* Read 13 bits */
       
  3388          code = (int) bibShowBits(13, inBuffer, &bitsGot, bitErrorIndication, 
       
  3389             &ownError);
       
  3390 
       
  3391 
       
  3392          /* Note: We already know that the first 9 bits are zero, thus code
       
  3393             cannot be larger than 15. */
       
  3394 
       
  3395          *index = tabMCBPCInterIndices21To24[code].val;
       
  3396 
       
  3397          /* If illegal bit pattern */
       
  3398          if (*index == -1) {
       
  3399             deb("vlcGetMCBPCInter: ERROR - illegal code.\n");
       
  3400             return VDX_OK_BUT_BIT_ERROR;
       
  3401          }
       
  3402 
       
  3403          bibFlushBits(tabMCBPCInterIndices21To24[code].len, inBuffer, &bitsGot, 
       
  3404             bitErrorIndication, &ownError);
       
  3405       }
       
  3406 
       
  3407       /* Else illegal code */
       
  3408       else {
       
  3409          deb("vlcGetMCBPCInter: ERROR - illegal code.\n");
       
  3410          return VDX_OK_BUT_BIT_ERROR;
       
  3411       }
       
  3412    }
       
  3413 
       
  3414    /* Else indices 1 - 20 */
       
  3415    else {
       
  3416       bibFlushBits(tabMCBPCInter[code].len, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3417    
       
  3418       *index = tabMCBPCInter[code].val;
       
  3419    }
       
  3420 
       
  3421 
       
  3422    if (ownError)
       
  3423       return VDX_ERR_BIB;
       
  3424 
       
  3425    return VDX_OK;
       
  3426 }
       
  3427 
       
  3428 
       
  3429 /*
       
  3430  * vdxGetMCBPCIntra
       
  3431  *    
       
  3432  *
       
  3433  * Parameters:
       
  3434  *    inBuffer                   pointer to bit buffer instance
       
  3435  *    index                      index to the MCBPC table for I-pictures
       
  3436  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3437  *                               within the bits accessed in this function,
       
  3438  *                               see biterr.h for possible values
       
  3439  *
       
  3440  * Function:
       
  3441  *    This function reads the MCBPC code for I-pictures.
       
  3442  *
       
  3443  * Returns:
       
  3444  *    VDX_OK                     the function was successful
       
  3445  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  3446  *                               occured
       
  3447  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3448  *
       
  3449  *    
       
  3450  */
       
  3451 
       
  3452 int vdxGetMCBPCIntra(bibBuffer_t *inBuffer, int *index, 
       
  3453    int *bitErrorIndication)
       
  3454 {
       
  3455    static const vdxVLCTable_t tabMCBPCIntra[64] = {
       
  3456       {-1,0}, /* illegal */
       
  3457       {5,6},
       
  3458       {6,6},
       
  3459       {7,6},
       
  3460       {4,4}, {4,4}, {4,4}, {4,4},
       
  3461       {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
       
  3462       {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
       
  3463       {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}
       
  3464    };
       
  3465 
       
  3466    int bitsGot, code;
       
  3467    int16 ownError = 0;
       
  3468 
       
  3469    vdxAssert(inBuffer != NULL);
       
  3470    vdxAssert(index != NULL);
       
  3471    vdxAssert(bitErrorIndication != NULL);
       
  3472 
       
  3473    code = (int) bibShowBits(9, inBuffer, &bitsGot, bitErrorIndication, 
       
  3474       &ownError);
       
  3475 
       
  3476 
       
  3477    if (code == 1) {
       
  3478       /* macroblock stuffing */
       
  3479       bibFlushBits(9, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3480       *index = 8;
       
  3481       return VDX_OK;
       
  3482    }
       
  3483 
       
  3484    code >>= 3; /* remove unnecessary bits */
       
  3485 
       
  3486    if (code == 0) {
       
  3487       deb("vlcGetMCBPCIntra: ERROR - illegal code.\n");
       
  3488       return VDX_OK_BUT_BIT_ERROR;
       
  3489    }
       
  3490 
       
  3491    if (code >= 32) {
       
  3492       /* bit pattern = 1xxxxx */
       
  3493       bibFlushBits(1, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3494       *index = 0;
       
  3495       return VDX_OK;
       
  3496    }
       
  3497 
       
  3498    bibFlushBits(tabMCBPCIntra[code].len, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3499 
       
  3500    *index = tabMCBPCIntra[code].val;
       
  3501 
       
  3502    if (ownError)
       
  3503       return VDX_ERR_BIB;
       
  3504 
       
  3505    return VDX_OK;
       
  3506 }
       
  3507 
       
  3508 
       
  3509 /*
       
  3510  * vdxGetMVD
       
  3511  *    
       
  3512  *
       
  3513  * Parameters:
       
  3514  *    inBuffer                   pointer to bit buffer instance
       
  3515  *    mvdx10                     the leftmost vector value from the motion
       
  3516  *                               vector VLC table multiplied by 10 to avoid 
       
  3517  *                               non-integer numbers.
       
  3518  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3519  *                               within the bits accessed in this function,
       
  3520  *                               see biterr.h for possible values
       
  3521  *
       
  3522  * Function:
       
  3523  *    This function reads the MVD code.
       
  3524  *
       
  3525  * Returns:
       
  3526  *    VDX_OK                     the function was successful
       
  3527  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  3528  *                               occured
       
  3529  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3530  *
       
  3531  *    
       
  3532  */
       
  3533 
       
  3534 int vdxGetMVD(bibBuffer_t *inBuffer, int *mvdx10, 
       
  3535    int *bitErrorIndication)
       
  3536 {
       
  3537    static const vdxVLCTable_t tabMVD0[14] = {
       
  3538       {15,4},  /* 0001 0 */
       
  3539       {-15,4}, /* 0001 1 */
       
  3540       {10,3}, {10,3},   /* 0010 */
       
  3541       {-10,3}, {-10,3}, /* 0011 */
       
  3542       {5,2}, {5,2}, {5,2}, {5,2},    /* 010 */
       
  3543       {-5,2}, {-5,2}, {-5,2}, {-5,2} /* 011 */
       
  3544    };
       
  3545 
       
  3546    static const vdxVLCTable_t tabMVD1[96] = {
       
  3547       {60,10}, {-60,10},
       
  3548       {55,10}, {-55,10},
       
  3549       {50,9}, {50,9}, {-50,9}, {-50,9},
       
  3550       {45,9}, {45,9}, {-45,9}, {-45,9},
       
  3551       {40,9}, {40,9}, {-40,9}, {-40,9},
       
  3552       {35,7}, {35,7}, {35,7}, {35,7}, {35,7}, {35,7}, {35,7}, {35,7},
       
  3553       {-35,7}, {-35,7}, {-35,7}, {-35,7}, {-35,7}, {-35,7}, {-35,7}, {-35,7},
       
  3554       {30,7}, {30,7}, {30,7}, {30,7}, {30,7}, {30,7}, {30,7}, {30,7},
       
  3555       {-30,7}, {-30,7}, {-30,7}, {-30,7}, {-30,7}, {-30,7}, {-30,7}, {-30,7},
       
  3556       {25,7}, {25,7}, {25,7}, {25,7}, {25,7}, {25,7}, {25,7}, {25,7},
       
  3557       {-25,7}, {-25,7}, {-25,7}, {-25,7}, {-25,7}, {-25,7}, {-25,7}, {-25,7},
       
  3558       {20,6}, {20,6}, {20,6}, {20,6}, {20,6}, {20,6}, {20,6}, {20,6},
       
  3559       {20,6}, {20,6}, {20,6}, {20,6}, {20,6}, {20,6}, {20,6}, {20,6},
       
  3560       {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6},
       
  3561       {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}, {-20,6}
       
  3562    };
       
  3563 
       
  3564    static const vdxVLCTable_t tabMVD2[] = {
       
  3565       {160,12}, {-160,12}, {155,12}, {-155,12},
       
  3566       {150,11}, {150,11}, {-150,11}, {-150,11},
       
  3567       {145,11}, {145,11}, {-145,11}, {-145,11},
       
  3568       {140,11}, {140,11}, {-140,11}, {-140,11},
       
  3569       {135,11}, {135,11}, {-135,11}, {-135,11},
       
  3570       {130,11}, {130,11}, {-130,11}, {-130,11},
       
  3571       {125,11}, {125,11}, {-125,11}, {-125,11},
       
  3572       {120,10}, {120,10}, {120,10}, {120,10},
       
  3573       {-120,10}, {-120,10}, {-120,10}, {-120,10},
       
  3574       {115,10}, {115,10}, {115,10}, {115,10},
       
  3575       {-115,10}, {-115,10}, {-115,10}, {-115,10},
       
  3576       {110,10}, {110,10}, {110,10}, {110,10},
       
  3577       {-110,10}, {-110,10}, {-110,10}, {-110,10},
       
  3578       {105,10}, {105,10}, {105,10}, {105,10},
       
  3579       {-105,10}, {-105,10}, {-105,10}, {-105,10},
       
  3580       {100,10}, {100,10}, {100,10}, {100,10},
       
  3581       {-100,10}, {-100,10}, {-100,10}, {-100,10},
       
  3582       {95,10}, {95,10}, {95,10}, {95,10},
       
  3583       {-95,10}, {-95,10}, {-95,10}, {-95,10},
       
  3584       {90,10}, {90,10}, {90,10}, {90,10},
       
  3585       {-90,10}, {-90,10}, {-90,10}, {-90,10},
       
  3586       {85,10}, {85,10}, {85,10}, {85,10},
       
  3587       {-85,10}, {-85,10}, {-85,10}, {-85,10},
       
  3588       {80,10}, {80,10}, {80,10}, {80,10},
       
  3589       {-80,10}, {-80,10}, {-80,10}, {-80,10},
       
  3590       {75,10}, {75,10}, {75,10}, {75,10},
       
  3591       {-75,10}, {-75,10}, {-75,10}, {-75,10},
       
  3592       {70,10}, {70,10}, {70,10}, {70,10},
       
  3593       {-70,10}, {-70,10}, {-70,10}, {-70,10},
       
  3594       {65,10}, {65,10}, {65,10}, {65,10},
       
  3595       {-65,10}, {-65,10}, {-65,10}, {-65,10}
       
  3596    };
       
  3597    int code, bitsGot;
       
  3598    int16 ownError = 0;
       
  3599 
       
  3600    vdxAssert(inBuffer != NULL);
       
  3601    vdxAssert(mvdx10 != NULL);
       
  3602    vdxAssert(bitErrorIndication != NULL);
       
  3603 
       
  3604    code = (int) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3605 
       
  3606 
       
  3607    if (code) {
       
  3608       *mvdx10 = 0;
       
  3609       return VDX_OK;
       
  3610    }
       
  3611 
       
  3612    code = (int) bibShowBits(12, inBuffer, &bitsGot, bitErrorIndication, 
       
  3613       &ownError);
       
  3614 
       
  3615    
       
  3616    if (code >= 512) {
       
  3617       code = (code >> 8) - 2;
       
  3618       bibFlushBits(tabMVD0[code].len, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3619 
       
  3620       *mvdx10 = tabMVD0[code].val;
       
  3621       return VDX_OK;
       
  3622    }
       
  3623 
       
  3624    if (code >= 128) {
       
  3625       code = (code >> 2) - 32;
       
  3626       bibFlushBits(tabMVD1[code].len, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3627 
       
  3628 
       
  3629       *mvdx10 = tabMVD1[code].val;
       
  3630       return VDX_OK;
       
  3631    }
       
  3632 
       
  3633    /* If illegal code, return bit error.
       
  3634       In Table 14/H.263, the illegal codes are 0000 0000 000x x and 0000 0000 0010 0.
       
  3635       In Table B-12/MPEG-4 Visual, Section B.1.3, 0000 0000 0010 0 is legal.
       
  3636       To simplify the source code, 0000 0000 0010 0 is considered legal in  H.263 too */
       
  3637    if ((code -= 4) < 0) {
       
  3638       deb("vlcGetMVD: ERROR - illegal code.\n");
       
  3639       return VDX_OK_BUT_BIT_ERROR;
       
  3640    }
       
  3641 
       
  3642    bibFlushBits(tabMVD2[code].len, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3643 
       
  3644 
       
  3645    *mvdx10 = tabMVD2[code].val;
       
  3646    return VDX_OK;
       
  3647 }
       
  3648 
       
  3649 
       
  3650 /*
       
  3651  * vdxUMVGetMVD
       
  3652  *
       
  3653  * Parameters:
       
  3654  *    inBuffer                   pointer to bit buffer instance
       
  3655  *    mvdx10                     the leftmost vector value from the motion
       
  3656  *                               vector VLC table multiplied by 10 to avoid 
       
  3657  *                               non-integer numbers.
       
  3658  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3659  *                               within the bits accessed in this function,
       
  3660  *                               see biterr.h for possible values
       
  3661  *
       
  3662  * Function:
       
  3663  *    This function reads the MVD code when unrestricted motion vector mode
       
  3664  *    is in use and PLUSTYPE is present.
       
  3665  *
       
  3666  * Returns:
       
  3667  *    VDX_OK                     the function was successful
       
  3668  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally,but a bit error
       
  3669  *                               occured
       
  3670  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3671  *
       
  3672  */
       
  3673 
       
  3674 static int vdxUMVGetMVD(bibBuffer_t *inBuffer,int *mvdx10,
       
  3675    int *bitErrorIndication)
       
  3676 {
       
  3677    int code,bitsGot;
       
  3678    int16 ownError = 0;
       
  3679    int sign;
       
  3680 
       
  3681    vdxAssert(inBuffer != NULL);
       
  3682    vdxAssert(mvdx10 != NULL);
       
  3683    vdxAssert(bitErrorIndication != NULL);
       
  3684 
       
  3685    code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,&ownError);
       
  3686 
       
  3687 
       
  3688    if (code) {
       
  3689       *mvdx10 = 0;
       
  3690       return VDX_OK;
       
  3691    }
       
  3692 
       
  3693    code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,&ownError);
       
  3694 
       
  3695    code += 2;
       
  3696 
       
  3697    while (bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,&ownError) 
       
  3698    )
       
  3699    {
       
  3700       code <<=1;
       
  3701       code = code + bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,&ownError);
       
  3702    }
       
  3703 
       
  3704    
       
  3705    sign = (code & 0x0001)?-5:5;
       
  3706    code >>=1;
       
  3707    *mvdx10 = sign*code;
       
  3708    return VDX_OK;
       
  3709 }
       
  3710 
       
  3711 /*
       
  3712  * vdxGetNormalMODB
       
  3713  *    
       
  3714  *
       
  3715  * Parameters:
       
  3716  *    inBuffer                   pointer to bit buffer instance
       
  3717  *    index                      index to the MODB table
       
  3718  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3719  *                               within the bits accessed in this function,
       
  3720  *                               see biterr.h for possible values
       
  3721  *
       
  3722  * Function:
       
  3723  *    This function reads the MODB code. The function should be used
       
  3724  *    in PB-frames mode (Annex G).
       
  3725  *
       
  3726  * Returns:
       
  3727  *    VDX_OK                     the function was successful
       
  3728  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  3729  *                               occured
       
  3730  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3731  *
       
  3732  *    
       
  3733  */
       
  3734 
       
  3735 static int vdxGetNormalMODB(bibBuffer_t *inBuffer, int *index, 
       
  3736    int *bitErrorIndication)
       
  3737 {
       
  3738    int bitsGot, code;
       
  3739    int16 ownError = 0;
       
  3740 
       
  3741    vdxAssert(inBuffer != NULL);
       
  3742    vdxAssert(index != NULL);
       
  3743    vdxAssert(bitErrorIndication != NULL);
       
  3744 
       
  3745    code = (int) bibShowBits(2, inBuffer, &bitsGot, bitErrorIndication, 
       
  3746       &ownError);
       
  3747 
       
  3748 
       
  3749    if (code < 2) {
       
  3750       /* code = 0 */
       
  3751       bibFlushBits(1, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3752       *index = 0;
       
  3753       return VDX_OK;
       
  3754    }
       
  3755    else {
       
  3756       /* code = 10 or 11 */
       
  3757       bibFlushBits(2, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  3758       *index = code - 1;
       
  3759       return VDX_OK;
       
  3760    }
       
  3761 }
       
  3762 
       
  3763 /*
       
  3764  * vdxGetImpPBMODB
       
  3765  *
       
  3766  * Parameters:
       
  3767  *    inBuffer                   pointer to bit buffer instance
       
  3768  *    index                      index to the MODB table
       
  3769  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3770  *                               within the bits accessed in this function,
       
  3771  *                               see biterr.h for possible values
       
  3772  *
       
  3773  * Function:
       
  3774  *    This function reads the MODB code. This function should be used
       
  3775  *    in improved PB-frames mode (Annex M).
       
  3776  *
       
  3777  * Returns:
       
  3778  *    VDX_OK                     the function was successful
       
  3779  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally,but a bit error
       
  3780  *                               occured
       
  3781  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3782  *
       
  3783  */
       
  3784 
       
  3785 static int vdxGetImpPBMODB(bibBuffer_t *inBuffer,int *index,
       
  3786    int *bitErrorIndication)
       
  3787 {
       
  3788    int bitsGot,code;
       
  3789    int16 ownError = 0;
       
  3790 
       
  3791    vdxAssert(inBuffer != NULL);
       
  3792    vdxAssert(index != NULL);
       
  3793    vdxAssert(bitErrorIndication != NULL);
       
  3794 
       
  3795    code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,
       
  3796       &ownError);
       
  3797 
       
  3798 
       
  3799    
       
  3800    if (code == 0)
       
  3801    {
       
  3802       /* Bidirectional Prediction: code = 0 */
       
  3803       *index = 0;
       
  3804       return VDX_OK;      
       
  3805    }
       
  3806    else
       
  3807    {
       
  3808       code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,
       
  3809          &ownError);
       
  3810 
       
  3811       if (code == 0)
       
  3812       {
       
  3813          /* Bidirectional Prediction: code = 10 */
       
  3814          *index = 1;
       
  3815          return VDX_OK;
       
  3816       }
       
  3817       else
       
  3818       {
       
  3819          code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,
       
  3820             &ownError);
       
  3821 
       
  3822 
       
  3823          if (code == 0)
       
  3824          {
       
  3825             /* Forward Prediction: code = 110 */
       
  3826             *index = 2;
       
  3827             return VDX_OK;
       
  3828          }
       
  3829          else
       
  3830          {
       
  3831             code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,
       
  3832                &ownError);
       
  3833 
       
  3834 
       
  3835             if (code == 0)
       
  3836             {
       
  3837                /* Forward Prediction: code = 1110 */
       
  3838                *index = 3;
       
  3839                return VDX_OK;
       
  3840             }
       
  3841             else
       
  3842             {
       
  3843                code = (int) bibGetBits(1,inBuffer,&bitsGot,bitErrorIndication,
       
  3844                   &ownError);
       
  3845 
       
  3846 
       
  3847                /* Backward Prediction: code = 11110 or 11111 */
       
  3848                *index = 4+code;
       
  3849                return VDX_OK;
       
  3850             }
       
  3851          }
       
  3852       }
       
  3853     }
       
  3854 }
       
  3855 
       
  3856 /*
       
  3857  * vdxUpdateQuant
       
  3858  *    
       
  3859  *
       
  3860  * Parameters:
       
  3861  *    inBuffer                   pointer to bit buffer instance
       
  3862  *    fMQ                        non-zero if the Modified Quantization mode
       
  3863  *                               (Annex T) is in use, otherwise zero
       
  3864  *    quant                      the quantization parameter for the previous
       
  3865  *                               macroblock
       
  3866  *    newQuant                   the updated quantization parameter for
       
  3867  *                               the current macroblock
       
  3868  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3869  *                               within the bits accessed in this function,
       
  3870  *                               see biterr.h for possible values
       
  3871  *
       
  3872  * Function:
       
  3873  *    This function reads the DQUANT field and updated the current quantization
       
  3874  *    parameter appropriately.
       
  3875  *
       
  3876  * Returns:
       
  3877  *    VDX_OK                     the function was successful
       
  3878  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  3879  *                               occured
       
  3880  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3881  *
       
  3882  *    
       
  3883  *
       
  3884  */
       
  3885 
       
  3886 int vdxUpdateQuant(
       
  3887    bibBuffer_t *inBuffer, 
       
  3888    int fMQ,
       
  3889    int quant,
       
  3890    int *newQuant,
       
  3891    int *bitErrorIndication)
       
  3892 {
       
  3893    int
       
  3894       numBitsGot;
       
  3895    int16
       
  3896       bibError = 0;
       
  3897    u_int32 
       
  3898       bits;
       
  3899    static const int changeOfQuant[2][32] = 
       
  3900       {{0,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-2,
       
  3901         -2,-2,-2,-2,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3},
       
  3902        {0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,2,1,-5}};
       
  3903 
       
  3904    vdxAssert(inBuffer != NULL);
       
  3905    vdxAssert(newQuant != NULL);
       
  3906    vdxAssert(bitErrorIndication != NULL);
       
  3907 
       
  3908    /* DQUANT */
       
  3909    if (!fMQ) {
       
  3910       bits = bibGetBits(2, inBuffer, &numBitsGot, bitErrorIndication, 
       
  3911          &bibError);
       
  3912 
       
  3913 
       
  3914       switch (bits) {
       
  3915          case 0: *newQuant = quant - 1; break;
       
  3916          case 1: *newQuant = quant - 2; break;
       
  3917          case 2: *newQuant = quant + 1; break;
       
  3918          case 3: *newQuant = quant + 2; break;
       
  3919       }
       
  3920 
       
  3921       /* Clip QUANT to legal range */
       
  3922       if (*newQuant < 1)
       
  3923          *newQuant = 1;
       
  3924       else if (*newQuant > 31)
       
  3925          *newQuant = 31;
       
  3926    }
       
  3927    else
       
  3928    {
       
  3929       bits = bibGetBits(1,inBuffer,&numBitsGot,bitErrorIndication,
       
  3930          &bibError);
       
  3931 
       
  3932 
       
  3933       if (bits)
       
  3934       {
       
  3935          bits = bibGetBits(1,inBuffer,&numBitsGot,bitErrorIndication,
       
  3936             &bibError);
       
  3937 
       
  3938          *newQuant = quant + changeOfQuant[bits][quant];
       
  3939       }
       
  3940       else
       
  3941       {
       
  3942          bits = bibGetBits(5,inBuffer,&numBitsGot,bitErrorIndication,
       
  3943             &bibError);
       
  3944 
       
  3945          *newQuant = bits;
       
  3946       }
       
  3947    }
       
  3948 
       
  3949    return VDX_OK;
       
  3950 }
       
  3951 
       
  3952 
       
  3953 /*
       
  3954  * vdxGetIntraMode
       
  3955  *
       
  3956  * Parameters:
       
  3957  *    inBuffer                   pointer to bit buffer instance
       
  3958  *    index                      index to the INTRA_MODE parameter for I-pictures
       
  3959  *    bitErrorIndication         non-zero if a bit error has been detected
       
  3960  *                               within the bits accessed in this function,
       
  3961  *                               see biterr.h for possible values
       
  3962  *
       
  3963  * Function:
       
  3964  *    This function reads the INTRA_MODE field from the MB header..
       
  3965  *    This field exists iff annex I is in use..
       
  3966  *
       
  3967  * Returns:
       
  3968  *    VDX_OK                     the function was successful
       
  3969  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally,but a bit error
       
  3970  *                               occured
       
  3971  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  3972  *
       
  3973  */
       
  3974 
       
  3975 
       
  3976 static int vdxGetIntraMode(bibBuffer_t *inBuffer,int *index,
       
  3977    int *bitErrorIndication)
       
  3978 
       
  3979 {
       
  3980    int bitsGot,code;
       
  3981    int16 ownError = 0;
       
  3982 
       
  3983    vdxAssert(inBuffer != NULL);
       
  3984    vdxAssert(index != NULL);
       
  3985    vdxAssert(bitErrorIndication != NULL);
       
  3986 
       
  3987    code = (int) bibShowBits(2,inBuffer,&bitsGot,bitErrorIndication,
       
  3988       &ownError);
       
  3989 
       
  3990 
       
  3991    if (code > 1) {
       
  3992       /* Bitpattern 1x */
       
  3993       bibFlushBits(2,inBuffer,&bitsGot,bitErrorIndication,&ownError);
       
  3994       *index = code - 1;
       
  3995       return VDX_OK;
       
  3996    }
       
  3997 
       
  3998    bibFlushBits(1,inBuffer,&bitsGot,bitErrorIndication,&ownError);
       
  3999    *index = 0;
       
  4000    return VDX_OK;
       
  4001 }
       
  4002 
       
  4003 
       
  4004 /*
       
  4005  * Block Layer Local Functions
       
  4006  */
       
  4007 
       
  4008 
       
  4009 
       
  4010 
       
  4011 
       
  4012 int vdxChangeBlackAndWhiteHeaderIntraIMB(bibBufferEdit_t *bufEdit, 
       
  4013                                                  int mcbpcIndex,
       
  4014                                                  int StartByteIndex, 
       
  4015                                                  int StartBitIndex)
       
  4016 {
       
  4017     int k, cur_index, new_index, cur_len, new_len, new_val;
       
  4018     
       
  4019     const tVLCTable sCBPCIType[9] = 
       
  4020     {
       
  4021     {1, 1}, {1, 3}, {2, 3}, {3, 3}, {1, 4},
       
  4022     {1, 6}, {2, 6}, {3, 6}, {1, 9}
       
  4023     };
       
  4024     
       
  4025      // evaluate MCBPC parameters
       
  4026     int cur_cbpc = mcbpcIndex & 3;      
       
  4027     int new_cbpc = 0;       // cpbc=0 indicates chroma is 0
       
  4028     int mbType = (mcbpcIndex <4)?3:4;
       
  4029 
       
  4030     // evaluate indices in table
       
  4031     cur_index = (mbType == 3 ? 0 : 4) + cur_cbpc;   
       
  4032     new_index = (mbType == 3 ? 0 : 4) + new_cbpc;   
       
  4033     
       
  4034     // retrieve values
       
  4035     cur_len = sCBPCIType[cur_index].length;
       
  4036     new_len = sCBPCIType[new_index].length;
       
  4037     new_val = sCBPCIType[new_index].code;
       
  4038     
       
  4039     // allocate memory for storing the parameters
       
  4040     if (!bufEdit->editParams)
       
  4041     {
       
  4042                 bufEdit->editParams = (bibEditParams_t *) malloc(sizeof(bibEditParams_t));
       
  4043                 bufEdit->numChanges=1;
       
  4044                 bufEdit->copyMode = CopyWithEdit/*CopyWithEdit*/;
       
  4045     }
       
  4046     k=bufEdit->numChanges-1;
       
  4047     bufEdit->copyMode = CopyWithEdit; 
       
  4048     // store the new parameters
       
  4049     bufEdit->editParams[k].StartByteIndex = StartByteIndex; 
       
  4050     bufEdit->editParams[k].StartBitIndex = StartBitIndex; 
       
  4051     bufEdit->editParams[k].curNumBits = cur_len; 
       
  4052     bufEdit->editParams[k].newNumBits = new_len; 
       
  4053     bufEdit->editParams[k].newValue = new_val; 
       
  4054 
       
  4055     return 0;
       
  4056 }
       
  4057 
       
  4058 int vdxChangeBlackAndWhiteHeaderInterPMB(bibBufferEdit_t *bufEdit, 
       
  4059                                                  int mcbpcIndex,
       
  4060                                                  int StartByteIndex, 
       
  4061                                                  int StartBitIndex)
       
  4062 {
       
  4063     int k, cur_index, new_index, cur_len, new_len, new_val;
       
  4064         
       
  4065     const tVLCTable sCBPCPType[21] = 
       
  4066     {
       
  4067     {1, 1}, {3, 4}, {2, 4}, {5, 6},
       
  4068     {3, 3}, {7, 7}, {6, 7}, {5, 9}, 
       
  4069     {2, 3}, {5, 7}, {4, 7}, {5, 8},
       
  4070     {3, 5}, {4, 8}, {3, 8}, {3, 7},
       
  4071     {4, 6}, {4, 9}, {3, 9}, {2, 9}, 
       
  4072     {1, 9}
       
  4073     };
       
  4074     
       
  4075      // evaluate MCBPC parameters
       
  4076     int cur_cbpc = mcbpcIndex & 3;      
       
  4077     int new_cbpc = 0;       // cpbc=0 indicates chroma is 0
       
  4078     int mbType; 
       
  4079 
       
  4080     mbType = mcbpcIndex / 4;
       
  4081      
       
  4082     // evaluate indices in table
       
  4083     cur_index = mbType * 4 + cur_cbpc;  
       
  4084     new_index = mbType * 4 + new_cbpc;  
       
  4085     
       
  4086     // retrieve values
       
  4087     cur_len = sCBPCPType[cur_index].length;
       
  4088     new_len = sCBPCPType[new_index].length;
       
  4089     new_val = sCBPCPType[new_index].code;
       
  4090     
       
  4091     // allocate memory for storing the parameters
       
  4092     if (!bufEdit->editParams)
       
  4093     {
       
  4094                 bufEdit->editParams = (bibEditParams_t *) malloc(sizeof(bibEditParams_t));
       
  4095                 bufEdit->numChanges=1;
       
  4096                 bufEdit->copyMode = CopyWithEdit/*CopyWithEdit*/; 
       
  4097     }
       
  4098     k=bufEdit->numChanges-1;
       
  4099     bufEdit->copyMode = CopyWithEdit/*CopyWithEdit*/; 
       
  4100     // store the new parameters
       
  4101     bufEdit->editParams[k].StartByteIndex = StartByteIndex; 
       
  4102     bufEdit->editParams[k].StartBitIndex = StartBitIndex; 
       
  4103     bufEdit->editParams[k].curNumBits = cur_len; 
       
  4104     bufEdit->editParams[k].newNumBits = new_len; 
       
  4105     bufEdit->editParams[k].newValue = new_val; 
       
  4106 
       
  4107     return mbType;
       
  4108 }
       
  4109 
       
  4110 
       
  4111 // End of File